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