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 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 159 | ConversionResult<media::AudioProfile> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 160 | AudioProfile::toParcelable(bool isInput) const { |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 161 | media::AudioProfile parcelable; |
| 162 | parcelable.name = mName; |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 163 | parcelable.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription(mFormat)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 164 | parcelable.channelMasks = VALUE_OR_RETURN( |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 165 | convertContainer<std::vector<AudioChannelLayout>>( |
Mikhail Naganov | 2d8df4e | 2021-06-03 13:59:13 -0700 | [diff] [blame] | 166 | mChannelMasks, |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 167 | [isInput](audio_channel_mask_t m) { |
| 168 | return legacy2aidl_audio_channel_mask_t_AudioChannelLayout(m, isInput); |
| 169 | })); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 170 | parcelable.samplingRates = VALUE_OR_RETURN( |
| 171 | convertContainer<std::vector<int32_t>>(mSamplingRates, |
| 172 | convertIntegral<int32_t, uint32_t>)); |
| 173 | parcelable.isDynamicFormat = mIsDynamicFormat; |
| 174 | parcelable.isDynamicChannels = mIsDynamicChannels; |
| 175 | parcelable.isDynamicRate = mIsDynamicRate; |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 176 | parcelable.encapsulationType = VALUE_OR_RETURN( |
| 177 | legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(mEncapsulationType)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 178 | return parcelable; |
| 179 | } |
| 180 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 181 | ConversionResult<sp<AudioProfile>> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 182 | AudioProfile::fromParcelable(const media::AudioProfile& parcelable, bool isInput) { |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 183 | sp<AudioProfile> legacy = new AudioProfile(); |
| 184 | legacy->mName = parcelable.name; |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 185 | legacy->mFormat = VALUE_OR_RETURN( |
| 186 | aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 187 | legacy->mChannelMasks = VALUE_OR_RETURN( |
| 188 | convertContainer<ChannelMaskSet>(parcelable.channelMasks, |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 189 | [isInput](const AudioChannelLayout& l) { |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 190 | return aidl2legacy_AudioChannelLayout_audio_channel_mask_t(l, isInput); |
| 191 | })); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 192 | legacy->mSamplingRates = VALUE_OR_RETURN( |
| 193 | convertContainer<SampleRateSet>(parcelable.samplingRates, |
| 194 | convertIntegral<uint32_t, int32_t>)); |
| 195 | legacy->mIsDynamicFormat = parcelable.isDynamicFormat; |
| 196 | legacy->mIsDynamicChannels = parcelable.isDynamicChannels; |
| 197 | legacy->mIsDynamicRate = parcelable.isDynamicRate; |
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)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 201 | return legacy; |
| 202 | } |
| 203 | |
| 204 | ConversionResult<sp<AudioProfile>> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 205 | aidl2legacy_AudioProfile(const media::AudioProfile& aidl, bool isInput) { |
| 206 | return AudioProfile::fromParcelable(aidl, isInput); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | ConversionResult<media::AudioProfile> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 210 | legacy2aidl_AudioProfile(const sp<AudioProfile>& legacy, bool isInput) { |
| 211 | return legacy->toParcelable(isInput); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 212 | } |
| 213 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 214 | ssize_t AudioProfileVector::add(const sp<AudioProfile> &profile) |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 215 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 216 | ssize_t index = size(); |
| 217 | push_back(profile); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 218 | return index; |
| 219 | } |
| 220 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 221 | void AudioProfileVector::clearProfiles() |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 222 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 223 | for (auto it = begin(); it != end();) { |
| 224 | if ((*it)->isDynamicFormat() && (*it)->hasValidFormat()) { |
| 225 | it = erase(it); |
| 226 | } else { |
| 227 | (*it)->clear(); |
| 228 | ++it; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 229 | } |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 233 | sp<AudioProfile> AudioProfileVector::getFirstValidProfile() const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 234 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 235 | for (const auto &profile : *this) { |
| 236 | if (profile->isValid()) { |
| 237 | return profile; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 240 | return nullptr; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 241 | } |
| 242 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 243 | sp<AudioProfile> AudioProfileVector::getFirstValidProfileFor(audio_format_t format) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 244 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 245 | for (const auto &profile : *this) { |
| 246 | if (profile->isValid() && profile->getFormat() == format) { |
| 247 | return profile; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 250 | return nullptr; |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 251 | } |
| 252 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 253 | FormatVector AudioProfileVector::getSupportedFormats() const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 254 | { |
| 255 | FormatVector supportedFormats; |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 256 | for (const auto &profile : *this) { |
| 257 | if (profile->hasValidFormat()) { |
| 258 | supportedFormats.push_back(profile->getFormat()); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | return supportedFormats; |
| 262 | } |
| 263 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 264 | bool AudioProfileVector::hasDynamicChannelsFor(audio_format_t format) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 265 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 266 | for (const auto &profile : *this) { |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 267 | if (profile->getFormat() == format && profile->isDynamicChannels()) { |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 274 | bool AudioProfileVector::hasDynamicFormat() const |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 275 | { |
| 276 | for (const auto &profile : *this) { |
| 277 | if (profile->isDynamicFormat()) { |
| 278 | return true; |
| 279 | } |
| 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 284 | bool AudioProfileVector::hasDynamicProfile() const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 285 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 286 | for (const auto &profile : *this) { |
| 287 | if (profile->isDynamic()) { |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 288 | return true; |
| 289 | } |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 294 | bool AudioProfileVector::hasDynamicRateFor(audio_format_t format) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 295 | { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 296 | for (const auto &profile : *this) { |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 297 | if (profile->getFormat() == format && profile->isDynamicRate()) { |
| 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 304 | bool AudioProfileVector::contains(const sp<AudioProfile>& profile) const |
| 305 | { |
| 306 | for (const auto& audioProfile : *this) { |
| 307 | if (audioProfile->equals(profile)) { |
| 308 | return true; |
| 309 | } |
| 310 | } |
| 311 | return false; |
| 312 | } |
| 313 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 314 | void AudioProfileVector::dump(std::string *dst, int spaces) const |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 315 | { |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 316 | dst->append(base::StringPrintf("%*s- Profiles:\n", spaces, "")); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 317 | for (size_t i = 0; i < size(); i++) { |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 318 | dst->append(base::StringPrintf("%*sProfile %zu:", spaces + 4, "", i)); |
| 319 | std::string profileStr; |
| 320 | at(i)->dump(&profileStr, spaces + 8); |
| 321 | dst->append(profileStr); |
Mikhail Naganov | e50f628 | 2018-07-26 16:20:43 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 325 | bool AudioProfileVector::equals(const AudioProfileVector& other) const |
| 326 | { |
| 327 | return std::equal(begin(), end(), other.begin(), other.end(), |
| 328 | [](const sp<AudioProfile>& left, const sp<AudioProfile>& right) { |
| 329 | return left->equals(right); |
| 330 | }); |
| 331 | } |
| 332 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 333 | ConversionResult<AudioProfileVector> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 334 | aidl2legacy_AudioProfileVector(const std::vector<media::AudioProfile>& aidl, bool isInput) { |
| 335 | return convertContainer<AudioProfileVector>(aidl, |
| 336 | [isInput](const media::AudioProfile& p) { |
| 337 | return aidl2legacy_AudioProfile(p, isInput); |
| 338 | }); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | ConversionResult<std::vector<media::AudioProfile>> |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 342 | legacy2aidl_AudioProfileVector(const AudioProfileVector& legacy, bool isInput) { |
| 343 | return convertContainer<std::vector<media::AudioProfile>>(legacy, |
| 344 | [isInput](const sp<AudioProfile>& p) { |
| 345 | return legacy2aidl_AudioProfile(p, isInput); |
| 346 | }); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 347 | } |
| 348 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 349 | AudioProfileVector intersectAudioProfiles(const AudioProfileVector& profiles1, |
| 350 | const AudioProfileVector& profiles2) |
| 351 | { |
| 352 | std::map<audio_format_t, std::pair<ChannelMaskSet, SampleRateSet>> infos2; |
| 353 | for (const auto& profile : profiles2) { |
| 354 | infos2.emplace(profile->getFormat(), |
| 355 | std::make_pair(profile->getChannels(), profile->getSampleRates())); |
| 356 | } |
| 357 | AudioProfileVector profiles; |
| 358 | for (const auto& profile : profiles1) { |
| 359 | const auto it = infos2.find(profile->getFormat()); |
| 360 | if (it == infos2.end()) { |
| 361 | continue; |
| 362 | } |
| 363 | ChannelMaskSet channelMasks = SetIntersection(profile->getChannels(), it->second.first); |
| 364 | if (channelMasks.empty()) { |
| 365 | continue; |
| 366 | } |
| 367 | SampleRateSet sampleRates = SetIntersection(profile->getSampleRates(), it->second.second); |
| 368 | if (sampleRates.empty()) { |
| 369 | continue; |
| 370 | } |
| 371 | profiles.push_back(new AudioProfile(profile->getFormat(), channelMasks, sampleRates)); |
| 372 | } |
| 373 | return profiles; |
| 374 | } |
| 375 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 376 | } // namespace android |