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