François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 1 | /* |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 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 | fa69dc6 | 2018-07-27 09:58:58 -0700 | [diff] [blame] | 17 | #include <set> |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 18 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 19 | #define LOG_TAG "AudioProfile" |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
| 21 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 23 | #include <media/AudioContainers.h> |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 24 | #include <media/AudioProfile.h> |
| 25 | #include <media/TypeConverter.h> |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
| 27 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 30 | using media::audio::common::AudioChannelLayout; |
| 31 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 32 | bool operator == (const AudioProfile &left, const AudioProfile &right) |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 33 | { |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 34 | return (left.getFormat() == right.getFormat()) && |
| 35 | (left.getChannels() == right.getChannels()) && |
| 36 | (left.getSampleRates() == right.getSampleRates()); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 37 | } |
| 38 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 39 | // static |
| 40 | sp<AudioProfile> AudioProfile::createFullDynamic(audio_format_t dynamicFormat) |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 41 | { |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 42 | AudioProfile* dynamicProfile = new AudioProfile(dynamicFormat, |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 43 | ChannelMaskSet(), SampleRateSet()); |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 44 | dynamicProfile->setDynamicFormat(true); |
| 45 | dynamicProfile->setDynamicChannels(true); |
| 46 | dynamicProfile->setDynamicRate(true); |
| 47 | return dynamicProfile; |
| 48 | } |
| 49 | |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 50 | AudioProfile::AudioProfile(audio_format_t format, |
| 51 | audio_channel_mask_t channelMasks, |
| 52 | uint32_t samplingRate) : |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 53 | mName(""), |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 54 | mFormat(format) |
| 55 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 56 | mChannelMasks.insert(channelMasks); |
| 57 | mSamplingRates.insert(samplingRate); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | AudioProfile::AudioProfile(audio_format_t format, |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 61 | const ChannelMaskSet &channelMasks, |
| 62 | const SampleRateSet &samplingRateCollection) : |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 63 | AudioProfile(format, channelMasks, samplingRateCollection, |
| 64 | AUDIO_ENCAPSULATION_TYPE_NONE) {} |
| 65 | |
| 66 | AudioProfile::AudioProfile(audio_format_t format, |
| 67 | const ChannelMaskSet &channelMasks, |
| 68 | const SampleRateSet &samplingRateCollection, |
| 69 | audio_encapsulation_type_t encapsulationType) : |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 70 | mName(""), |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 71 | mFormat(format), |
| 72 | mChannelMasks(channelMasks), |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 73 | mSamplingRates(samplingRateCollection), |
| 74 | mEncapsulationType(encapsulationType) {} |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 75 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 76 | void AudioProfile::setChannels(const ChannelMaskSet &channelMasks) |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 77 | { |
| 78 | if (mIsDynamicChannels) { |
| 79 | mChannelMasks = channelMasks; |
| 80 | } |
| 81 | } |
| 82 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 83 | void AudioProfile::setSampleRates(const SampleRateSet &sampleRates) |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 84 | { |
| 85 | if (mIsDynamicRate) { |
| 86 | mSamplingRates = sampleRates; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void AudioProfile::clear() |
| 91 | { |
| 92 | if (mIsDynamicChannels) { |
| 93 | mChannelMasks.clear(); |
| 94 | } |
| 95 | if (mIsDynamicRate) { |
| 96 | mSamplingRates.clear(); |
| 97 | } |
| 98 | } |
| 99 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 100 | void AudioProfile::dump(std::string *dst, int spaces) const |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 101 | { |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 102 | dst->append(base::StringPrintf("\"%s\"; ", mName.c_str())); |
| 103 | dst->append(base::StringPrintf("%s%s%s%s", mIsDynamicFormat ? "[dynamic format]" : "", |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 104 | mIsDynamicChannels ? "[dynamic channels]" : "", |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 105 | mIsDynamicRate ? "[dynamic rates]" : "", isDynamic() ? "; " : "")); |
| 106 | dst->append(base::StringPrintf("%s (0x%x)\n", audio_format_to_string(mFormat), mFormat)); |
| 107 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 108 | if (!mSamplingRates.empty()) { |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 109 | dst->append(base::StringPrintf("%*ssampling rates: ", spaces, "")); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 110 | for (auto it = mSamplingRates.begin(); it != mSamplingRates.end();) { |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 111 | dst->append(base::StringPrintf("%d", *it)); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 112 | dst->append(++it == mSamplingRates.end() ? "" : ", "); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 113 | } |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 114 | dst->append("\n"); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 115 | } |
| 116 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 117 | if (!mChannelMasks.empty()) { |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 118 | dst->append(base::StringPrintf("%*schannel masks: ", spaces, "")); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 119 | for (auto it = mChannelMasks.begin(); it != mChannelMasks.end();) { |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 120 | dst->append(base::StringPrintf("0x%04x", *it)); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 121 | dst->append(++it == mChannelMasks.end() ? "" : ", "); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 122 | } |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 123 | dst->append("\n"); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 124 | } |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 125 | |
| 126 | dst->append(base::StringPrintf( |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 127 | "%*s%s\n", spaces, "", audio_encapsulation_type_to_string(mEncapsulationType))); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 128 | } |
| 129 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 130 | bool AudioProfile::equals(const sp<AudioProfile>& other) const |
| 131 | { |
| 132 | return other != nullptr && |
| 133 | mName.compare(other->mName) == 0 && |
| 134 | mFormat == other->getFormat() && |
| 135 | mChannelMasks == other->getChannels() && |
| 136 | mSamplingRates == other->getSampleRates() && |
| 137 | mIsDynamicFormat == other->isDynamicFormat() && |
| 138 | mIsDynamicChannels == other->isDynamicChannels() && |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 139 | mIsDynamicRate == other->isDynamicRate() && |
| 140 | mEncapsulationType == other->getEncapsulationType(); |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 143 | AudioProfile& AudioProfile::operator=(const AudioProfile& other) { |
| 144 | mName = other.mName; |
| 145 | mFormat = other.mFormat; |
| 146 | mChannelMasks = other.mChannelMasks; |
| 147 | mSamplingRates = other.mSamplingRates; |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 148 | mEncapsulationType = other.mEncapsulationType; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 149 | mIsDynamicFormat = other.mIsDynamicFormat; |
| 150 | mIsDynamicChannels = other.mIsDynamicChannels; |
| 151 | mIsDynamicRate = other.mIsDynamicRate; |
| 152 | return *this; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 155 | ConversionResult<AudioProfile::Aidl> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 156 | AudioProfile::toParcelable(bool isInput) const { |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 157 | media::audio::common::AudioProfile parcelable; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 158 | parcelable.name = mName; |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 159 | parcelable.format = VALUE_OR_RETURN( |
| 160 | legacy2aidl_audio_format_t_AudioFormatDescription(mFormat)); |
| 161 | // Note: legacy 'audio_profile' imposes a limit on the number of |
| 162 | // channel masks and sampling rates. That's why it's not used here |
| 163 | // and conversions are performed directly on the fields instead |
| 164 | // of using 'legacy2aidl_audio_profile_AudioProfile' from AidlConversion. |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 165 | parcelable.channelMasks = VALUE_OR_RETURN( |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 166 | convertContainer<std::vector<AudioChannelLayout>>( |
Mikhail Naganov | 2d8df4e | 2021-06-03 13:59:13 -0700 | [diff] [blame] | 167 | mChannelMasks, |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 168 | [isInput](audio_channel_mask_t m) { |
| 169 | return legacy2aidl_audio_channel_mask_t_AudioChannelLayout(m, isInput); |
| 170 | })); |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 171 | parcelable.sampleRates = VALUE_OR_RETURN( |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 172 | convertContainer<std::vector<int32_t>>(mSamplingRates, |
| 173 | convertIntegral<int32_t, uint32_t>)); |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 174 | parcelable.encapsulationType = VALUE_OR_RETURN( |
| 175 | legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(mEncapsulationType)); |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 176 | media::AudioProfileSys parcelableSys; |
| 177 | parcelableSys.isDynamicFormat = mIsDynamicFormat; |
| 178 | parcelableSys.isDynamicChannels = mIsDynamicChannels; |
| 179 | parcelableSys.isDynamicRate = mIsDynamicRate; |
| 180 | return std::make_pair(parcelable, parcelableSys); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 183 | ConversionResult<sp<AudioProfile>> AudioProfile::fromParcelable( |
| 184 | const AudioProfile::Aidl& aidl, bool isInput) { |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 185 | sp<AudioProfile> legacy = new AudioProfile(); |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 186 | const auto& parcelable = aidl.first; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 187 | legacy->mName = parcelable.name; |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 188 | legacy->mFormat = VALUE_OR_RETURN( |
| 189 | aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 190 | legacy->mChannelMasks = VALUE_OR_RETURN( |
| 191 | convertContainer<ChannelMaskSet>(parcelable.channelMasks, |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 192 | [isInput](const AudioChannelLayout& l) { |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 193 | return aidl2legacy_AudioChannelLayout_audio_channel_mask_t(l, isInput); |
| 194 | })); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 195 | legacy->mSamplingRates = VALUE_OR_RETURN( |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 196 | convertContainer<SampleRateSet>(parcelable.sampleRates, |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 197 | convertIntegral<uint32_t, int32_t>)); |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 198 | legacy->mEncapsulationType = VALUE_OR_RETURN( |
| 199 | aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t( |
| 200 | parcelable.encapsulationType)); |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 201 | const auto& parcelableSys = aidl.second; |
| 202 | legacy->mIsDynamicFormat = parcelableSys.isDynamicFormat; |
| 203 | legacy->mIsDynamicChannels = parcelableSys.isDynamicChannels; |
| 204 | legacy->mIsDynamicRate = parcelableSys.isDynamicRate; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 205 | return legacy; |
| 206 | } |
| 207 | |
| 208 | ConversionResult<sp<AudioProfile>> |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 209 | aidl2legacy_AudioProfile(const AudioProfile::Aidl& aidl, bool isInput) { |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 210 | return AudioProfile::fromParcelable(aidl, isInput); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 213 | ConversionResult<AudioProfile::Aidl> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 214 | legacy2aidl_AudioProfile(const sp<AudioProfile>& legacy, bool isInput) { |
| 215 | return legacy->toParcelable(isInput); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 216 | } |
| 217 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 218 | ssize_t AudioProfileVector::add(const sp<AudioProfile> &profile) |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 219 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 220 | ssize_t index = size(); |
| 221 | push_back(profile); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 222 | return index; |
| 223 | } |
| 224 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 225 | void AudioProfileVector::clearProfiles() |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 226 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 227 | for (auto it = begin(); it != end();) { |
| 228 | if ((*it)->isDynamicFormat() && (*it)->hasValidFormat()) { |
| 229 | it = erase(it); |
| 230 | } else { |
| 231 | (*it)->clear(); |
| 232 | ++it; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 233 | } |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 237 | sp<AudioProfile> AudioProfileVector::getFirstValidProfile() const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 238 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 239 | for (const auto &profile : *this) { |
| 240 | if (profile->isValid()) { |
| 241 | return profile; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 244 | return nullptr; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 245 | } |
| 246 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 247 | sp<AudioProfile> AudioProfileVector::getFirstValidProfileFor(audio_format_t format) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 248 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 249 | for (const auto &profile : *this) { |
| 250 | if (profile->isValid() && profile->getFormat() == format) { |
| 251 | return profile; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 254 | return nullptr; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 255 | } |
| 256 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 257 | FormatVector AudioProfileVector::getSupportedFormats() const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 258 | { |
| 259 | FormatVector supportedFormats; |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 260 | for (const auto &profile : *this) { |
| 261 | if (profile->hasValidFormat()) { |
| 262 | supportedFormats.push_back(profile->getFormat()); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | return supportedFormats; |
| 266 | } |
| 267 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 268 | bool AudioProfileVector::hasDynamicChannelsFor(audio_format_t format) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 269 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 270 | for (const auto &profile : *this) { |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 271 | if (profile->getFormat() == format && profile->isDynamicChannels()) { |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 278 | bool AudioProfileVector::hasDynamicFormat() const |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 279 | { |
| 280 | for (const auto &profile : *this) { |
| 281 | if (profile->isDynamicFormat()) { |
| 282 | return true; |
| 283 | } |
| 284 | } |
| 285 | return false; |
| 286 | } |
| 287 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 288 | bool AudioProfileVector::hasDynamicProfile() const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 289 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 290 | for (const auto &profile : *this) { |
| 291 | if (profile->isDynamic()) { |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 298 | bool AudioProfileVector::hasDynamicRateFor(audio_format_t format) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 299 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 300 | for (const auto &profile : *this) { |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 301 | if (profile->getFormat() == format && profile->isDynamicRate()) { |
| 302 | return true; |
| 303 | } |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 308 | bool AudioProfileVector::contains(const sp<AudioProfile>& profile) const |
| 309 | { |
| 310 | for (const auto& audioProfile : *this) { |
| 311 | if (audioProfile->equals(profile)) { |
| 312 | return true; |
| 313 | } |
| 314 | } |
| 315 | return false; |
| 316 | } |
| 317 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 318 | void AudioProfileVector::dump(std::string *dst, int spaces) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 319 | { |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 320 | dst->append(base::StringPrintf("%*s- Profiles (%zu):\n", spaces - 2, "", size())); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 321 | for (size_t i = 0; i < size(); i++) { |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 322 | const std::string prefix = base::StringPrintf("%*s%zu. ", spaces + 1, "", i + 1); |
| 323 | dst->append(prefix); |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 324 | std::string profileStr; |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 325 | at(i)->dump(&profileStr, prefix.size()); |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 326 | dst->append(profileStr); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 330 | bool AudioProfileVector::equals(const AudioProfileVector& other) const |
| 331 | { |
| 332 | return std::equal(begin(), end(), other.begin(), other.end(), |
| 333 | [](const sp<AudioProfile>& left, const sp<AudioProfile>& right) { |
| 334 | return left->equals(right); |
| 335 | }); |
| 336 | } |
| 337 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 338 | ConversionResult<AudioProfileVector> |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 339 | aidl2legacy_AudioProfileVector(const AudioProfileVector::Aidl& aidl, bool isInput) { |
| 340 | return convertContainers<AudioProfileVector>(aidl.first, aidl.second, |
| 341 | [isInput](const media::audio::common::AudioProfile& p, |
| 342 | const media::AudioProfileSys& ps) { |
| 343 | return aidl2legacy_AudioProfile(std::make_pair(p, ps), isInput); |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 344 | }); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 347 | ConversionResult<AudioProfileVector::Aidl> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 348 | legacy2aidl_AudioProfileVector(const AudioProfileVector& legacy, bool isInput) { |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 349 | return convertContainerSplit< |
| 350 | std::vector<media::audio::common::AudioProfile>, |
| 351 | std::vector<media::AudioProfileSys>>(legacy, |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 352 | [isInput](const sp<AudioProfile>& p) { |
| 353 | return legacy2aidl_AudioProfile(p, isInput); |
| 354 | }); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 355 | } |
| 356 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 357 | AudioProfileVector intersectAudioProfiles(const AudioProfileVector& profiles1, |
| 358 | const AudioProfileVector& profiles2) |
| 359 | { |
| 360 | std::map<audio_format_t, std::pair<ChannelMaskSet, SampleRateSet>> infos2; |
| 361 | for (const auto& profile : profiles2) { |
| 362 | infos2.emplace(profile->getFormat(), |
| 363 | std::make_pair(profile->getChannels(), profile->getSampleRates())); |
| 364 | } |
| 365 | AudioProfileVector profiles; |
| 366 | for (const auto& profile : profiles1) { |
| 367 | const auto it = infos2.find(profile->getFormat()); |
| 368 | if (it == infos2.end()) { |
| 369 | continue; |
| 370 | } |
| 371 | ChannelMaskSet channelMasks = SetIntersection(profile->getChannels(), it->second.first); |
| 372 | if (channelMasks.empty()) { |
| 373 | continue; |
| 374 | } |
| 375 | SampleRateSet sampleRates = SetIntersection(profile->getSampleRates(), it->second.second); |
| 376 | if (sampleRates.empty()) { |
| 377 | continue; |
| 378 | } |
| 379 | profiles.push_back(new AudioProfile(profile->getFormat(), channelMasks, sampleRates)); |
| 380 | } |
| 381 | return profiles; |
| 382 | } |
| 383 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 384 | } // namespace android |