Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mikhail Naganov | b6e5775 | 2023-03-08 18:12:47 -0800 | [diff] [blame^] | 17 | #include <sstream> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 18 | #include <utility> |
| 19 | |
Mikhail Naganov | b6e5775 | 2023-03-08 18:12:47 -0800 | [diff] [blame^] | 20 | #include <system/audio.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 21 | #define LOG_TAG "AidlConversionNdk" |
| 22 | //#define LOG_NDEBUG 0 |
| 23 | #include <utils/Log.h> |
Mikhail Naganov | b6e5775 | 2023-03-08 18:12:47 -0800 | [diff] [blame^] | 24 | #include <utils/Errors.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 25 | |
| 26 | #include <media/AidlConversionCppNdk.h> |
| 27 | #include <media/AidlConversionNdk.h> |
Mikhail Naganov | b6e5775 | 2023-03-08 18:12:47 -0800 | [diff] [blame^] | 28 | #include <Utils.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 29 | |
| 30 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 31 | // AIDL NDK backend to legacy audio data structure conversion utilities. |
| 32 | |
| 33 | namespace aidl { |
| 34 | namespace android { |
| 35 | |
Mikhail Naganov | b6e5775 | 2023-03-08 18:12:47 -0800 | [diff] [blame^] | 36 | using hardware::audio::common::PlaybackTrackMetadata; |
| 37 | using hardware::audio::common::RecordTrackMetadata; |
| 38 | using ::android::BAD_VALUE; |
| 39 | using ::android::OK; |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | ::android::status_t combineString( |
| 44 | const std::vector<std::string>& v, char separator, std::string* result) { |
| 45 | std::ostringstream oss; |
| 46 | for (const auto& s : v) { |
| 47 | if (oss.tellp() > 0) { |
| 48 | oss << separator; |
| 49 | } |
| 50 | if (s.find(separator) == std::string::npos) { |
| 51 | oss << s; |
| 52 | } else { |
| 53 | ALOGE("%s: string \"%s\" contains separator character \"%c\"", |
| 54 | __func__, s.c_str(), separator); |
| 55 | return BAD_VALUE; |
| 56 | } |
| 57 | } |
| 58 | *result = oss.str(); |
| 59 | return OK; |
| 60 | } |
| 61 | |
| 62 | std::vector<std::string> splitString(const std::string& s, char separator) { |
| 63 | std::istringstream iss(s); |
| 64 | std::string t; |
| 65 | std::vector<std::string> result; |
| 66 | while (std::getline(iss, t, separator)) { |
| 67 | result.push_back(std::move(t)); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | std::vector<std::string> filterOutNonVendorTags(const std::vector<std::string>& tags) { |
| 73 | std::vector<std::string> result; |
| 74 | std::copy_if(tags.begin(), tags.end(), std::back_inserter(result), |
| 75 | ::android::hardware::audio::common::maybeVendorExtension); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 81 | // buffer_provider_t is not supported thus skipped |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 82 | ConversionResult<buffer_config_t> aidl2legacy_AudioConfigBase_buffer_config_t( |
| 83 | const media::audio::common::AudioConfigBase& aidl, bool isInput) { |
| 84 | buffer_config_t legacy; |
| 85 | |
| 86 | legacy.samplingRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate)); |
| 87 | legacy.mask |= EFFECT_CONFIG_SMP_RATE; |
| 88 | |
| 89 | legacy.channels = VALUE_OR_RETURN( |
| 90 | aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput)); |
| 91 | legacy.mask |= EFFECT_CONFIG_CHANNELS; |
| 92 | |
| 93 | legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format)); |
| 94 | legacy.mask |= EFFECT_CONFIG_FORMAT; |
| 95 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 96 | // TODO: add accessMode and mask |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 97 | return legacy; |
| 98 | } |
| 99 | |
| 100 | ConversionResult<media::audio::common::AudioConfigBase> |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 101 | legacy2aidl_buffer_config_t_AudioConfigBase(const buffer_config_t& legacy, bool isInput) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 102 | media::audio::common::AudioConfigBase aidl; |
| 103 | |
| 104 | if (legacy.mask & EFFECT_CONFIG_SMP_RATE) { |
| 105 | aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.samplingRate)); |
| 106 | } |
| 107 | if (legacy.mask & EFFECT_CONFIG_CHANNELS) { |
| 108 | aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout( |
| 109 | static_cast<audio_channel_mask_t>(legacy.channels), isInput)); |
| 110 | } |
| 111 | if (legacy.mask & EFFECT_CONFIG_FORMAT) { |
| 112 | aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription( |
| 113 | static_cast<audio_format_t>(legacy.format))); |
| 114 | } |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 115 | |
| 116 | // TODO: add accessMode and mask |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 117 | return aidl; |
| 118 | } |
| 119 | |
Mikhail Naganov | b6e5775 | 2023-03-08 18:12:47 -0800 | [diff] [blame^] | 120 | ::android::status_t aidl2legacy_AudioAttributesTags( |
| 121 | const std::vector<std::string>& aidl, char* legacy) { |
| 122 | std::string aidlTags; |
| 123 | RETURN_STATUS_IF_ERROR(combineString( |
| 124 | filterOutNonVendorTags(aidl), AUDIO_ATTRIBUTES_TAGS_SEPARATOR, &aidlTags)); |
| 125 | RETURN_STATUS_IF_ERROR(aidl2legacy_string(aidlTags, legacy, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE)); |
| 126 | return OK; |
| 127 | } |
| 128 | |
| 129 | ConversionResult<std::vector<std::string>> legacy2aidl_AudioAttributesTags(const char* legacy) { |
| 130 | std::string legacyTags = VALUE_OR_RETURN(legacy2aidl_string( |
| 131 | legacy, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE)); |
| 132 | return filterOutNonVendorTags(splitString(legacyTags, AUDIO_ATTRIBUTES_TAGS_SEPARATOR)); |
| 133 | } |
| 134 | |
| 135 | ConversionResult<playback_track_metadata_v7> |
| 136 | aidl2legacy_PlaybackTrackMetadata_playback_track_metadata_v7(const PlaybackTrackMetadata& aidl) { |
| 137 | playback_track_metadata_v7 legacy; |
| 138 | legacy.base.usage = VALUE_OR_RETURN(aidl2legacy_AudioUsage_audio_usage_t(aidl.usage)); |
| 139 | legacy.base.content_type = VALUE_OR_RETURN(aidl2legacy_AudioContentType_audio_content_type_t( |
| 140 | aidl.contentType)); |
| 141 | legacy.base.gain = aidl.gain; |
| 142 | legacy.channel_mask = VALUE_OR_RETURN(aidl2legacy_AudioChannelLayout_audio_channel_mask_t( |
| 143 | aidl.channelMask, false /*isInput*/)); |
| 144 | RETURN_IF_ERROR(aidl2legacy_AudioAttributesTags(aidl.tags, legacy.tags)); |
| 145 | return legacy; |
| 146 | } |
| 147 | |
| 148 | ConversionResult<PlaybackTrackMetadata> |
| 149 | legacy2aidl_playback_track_metadata_v7_PlaybackTrackMetadata( |
| 150 | const playback_track_metadata_v7& legacy) { |
| 151 | PlaybackTrackMetadata aidl; |
| 152 | aidl.usage = VALUE_OR_RETURN(legacy2aidl_audio_usage_t_AudioUsage(legacy.base.usage)); |
| 153 | aidl.contentType = VALUE_OR_RETURN(legacy2aidl_audio_content_type_t_AudioContentType( |
| 154 | legacy.base.content_type)); |
| 155 | aidl.gain = legacy.base.gain; |
| 156 | aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout( |
| 157 | legacy.channel_mask, false /*isInput*/)); |
| 158 | aidl.tags = VALUE_OR_RETURN(legacy2aidl_AudioAttributesTags(legacy.tags)); |
| 159 | return aidl; |
| 160 | } |
| 161 | |
| 162 | ConversionResult<record_track_metadata_v7> |
| 163 | aidl2legacy_RecordTrackMetadata_record_track_metadata_v7(const RecordTrackMetadata& aidl) { |
| 164 | record_track_metadata_v7 legacy; |
| 165 | legacy.base.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source)); |
| 166 | legacy.base.gain = aidl.gain; |
| 167 | if (aidl.destinationDevice.has_value()) { |
| 168 | RETURN_IF_ERROR(aidl2legacy_AudioDevice_audio_device(aidl.destinationDevice.value(), |
| 169 | &legacy.base.dest_device, legacy.base.dest_device_address)); |
| 170 | } else { |
| 171 | legacy.base.dest_device = AUDIO_DEVICE_NONE; |
| 172 | } |
| 173 | legacy.channel_mask = VALUE_OR_RETURN(aidl2legacy_AudioChannelLayout_audio_channel_mask_t( |
| 174 | aidl.channelMask, true /*isInput*/)); |
| 175 | RETURN_IF_ERROR(aidl2legacy_AudioAttributesTags(aidl.tags, legacy.tags)); |
| 176 | return legacy; |
| 177 | } |
| 178 | |
| 179 | ConversionResult<RecordTrackMetadata> |
| 180 | legacy2aidl_record_track_metadata_v7_RecordTrackMetadata(const record_track_metadata_v7& legacy) { |
| 181 | RecordTrackMetadata aidl; |
| 182 | aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.base.source)); |
| 183 | aidl.gain = legacy.base.gain; |
| 184 | if (legacy.base.dest_device != AUDIO_DEVICE_NONE) { |
| 185 | aidl.destinationDevice = VALUE_OR_RETURN(legacy2aidl_audio_device_AudioDevice( |
| 186 | legacy.base.dest_device, legacy.base.dest_device_address)); |
| 187 | } |
| 188 | aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout( |
| 189 | legacy.channel_mask, true /*isInput*/)); |
| 190 | aidl.tags = VALUE_OR_RETURN(legacy2aidl_AudioAttributesTags(legacy.tags)); |
| 191 | return aidl; |
| 192 | } |
| 193 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 194 | } // namespace android |
| 195 | } // aidl |