blob: 8ac3f734cb18e4188b67cadd4b624bfa5ad007b9 [file] [log] [blame]
François Gaffie112b0af2015-11-19 16:13:25 +01001/*
jiabin3e277cc2019-09-10 14:27:34 -07002 * Copyright (C) 2019 The Android Open Source Project
François Gaffie112b0af2015-11-19 16:13:25 +01003 *
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 Naganovfa69dc62018-07-27 09:58:58 -070017#include <set>
Mikhail Naganove50f6282018-07-26 16:20:43 -070018
jiabin9bb3a1e2019-08-19 10:10:17 -070019#define LOG_TAG "AudioProfile"
François Gaffie112b0af2015-11-19 16:13:25 +010020//#define LOG_NDEBUG 0
21
jiabin9bb3a1e2019-08-19 10:10:17 -070022#include <android-base/stringprintf.h>
jiabin06e4bab2019-07-29 10:13:34 -070023#include <media/AudioContainers.h>
jiabin9bb3a1e2019-08-19 10:10:17 -070024#include <media/AudioProfile.h>
25#include <media/TypeConverter.h>
Mikhail Naganove50f6282018-07-26 16:20:43 -070026#include <utils/Errors.h>
27
François Gaffie112b0af2015-11-19 16:13:25 +010028namespace android {
29
jiabin9bb3a1e2019-08-19 10:10:17 -070030bool operator == (const AudioProfile &left, const AudioProfile &right)
Mikhail Naganove50f6282018-07-26 16:20:43 -070031{
jiabin9bb3a1e2019-08-19 10:10:17 -070032 return (left.getFormat() == right.getFormat()) &&
33 (left.getChannels() == right.getChannels()) &&
34 (left.getSampleRates() == right.getSampleRates());
Mikhail Naganove50f6282018-07-26 16:20:43 -070035}
36
jiabin9bb3a1e2019-08-19 10:10:17 -070037// static
38sp<AudioProfile> AudioProfile::createFullDynamic(audio_format_t dynamicFormat)
Mikhail Naganov21b43362018-06-04 10:37:09 -070039{
jiabin9bb3a1e2019-08-19 10:10:17 -070040 AudioProfile* dynamicProfile = new AudioProfile(dynamicFormat,
jiabin06e4bab2019-07-29 10:13:34 -070041 ChannelMaskSet(), SampleRateSet());
Mikhail Naganov21b43362018-06-04 10:37:09 -070042 dynamicProfile->setDynamicFormat(true);
43 dynamicProfile->setDynamicChannels(true);
44 dynamicProfile->setDynamicRate(true);
45 return dynamicProfile;
46}
47
Mikhail Naganove50f6282018-07-26 16:20:43 -070048AudioProfile::AudioProfile(audio_format_t format,
49 audio_channel_mask_t channelMasks,
50 uint32_t samplingRate) :
jiabin9bb3a1e2019-08-19 10:10:17 -070051 mName(""),
Mikhail Naganove50f6282018-07-26 16:20:43 -070052 mFormat(format)
53{
jiabin06e4bab2019-07-29 10:13:34 -070054 mChannelMasks.insert(channelMasks);
55 mSamplingRates.insert(samplingRate);
Mikhail Naganove50f6282018-07-26 16:20:43 -070056}
57
58AudioProfile::AudioProfile(audio_format_t format,
jiabin06e4bab2019-07-29 10:13:34 -070059 const ChannelMaskSet &channelMasks,
60 const SampleRateSet &samplingRateCollection) :
jiabin82e56932021-03-05 06:35:19 +000061 AudioProfile(format, channelMasks, samplingRateCollection,
62 AUDIO_ENCAPSULATION_TYPE_NONE) {}
63
64AudioProfile::AudioProfile(audio_format_t format,
65 const ChannelMaskSet &channelMasks,
66 const SampleRateSet &samplingRateCollection,
67 audio_encapsulation_type_t encapsulationType) :
jiabin9bb3a1e2019-08-19 10:10:17 -070068 mName(""),
Mikhail Naganove50f6282018-07-26 16:20:43 -070069 mFormat(format),
70 mChannelMasks(channelMasks),
jiabin82e56932021-03-05 06:35:19 +000071 mSamplingRates(samplingRateCollection),
72 mEncapsulationType(encapsulationType) {}
Mikhail Naganove50f6282018-07-26 16:20:43 -070073
jiabin06e4bab2019-07-29 10:13:34 -070074void AudioProfile::setChannels(const ChannelMaskSet &channelMasks)
Mikhail Naganove50f6282018-07-26 16:20:43 -070075{
76 if (mIsDynamicChannels) {
77 mChannelMasks = channelMasks;
78 }
79}
80
jiabin06e4bab2019-07-29 10:13:34 -070081void AudioProfile::setSampleRates(const SampleRateSet &sampleRates)
Mikhail Naganove50f6282018-07-26 16:20:43 -070082{
83 if (mIsDynamicRate) {
84 mSamplingRates = sampleRates;
85 }
86}
87
88void AudioProfile::clear()
89{
90 if (mIsDynamicChannels) {
91 mChannelMasks.clear();
92 }
93 if (mIsDynamicRate) {
94 mSamplingRates.clear();
95 }
96}
97
jiabin9bb3a1e2019-08-19 10:10:17 -070098void AudioProfile::dump(std::string *dst, int spaces) const
François Gaffie112b0af2015-11-19 16:13:25 +010099{
jiabin9bb3a1e2019-08-19 10:10:17 -0700100 dst->append(base::StringPrintf("%s%s%s\n", mIsDynamicFormat ? "[dynamic format]" : "",
François Gaffie112b0af2015-11-19 16:13:25 +0100101 mIsDynamicChannels ? "[dynamic channels]" : "",
jiabin9bb3a1e2019-08-19 10:10:17 -0700102 mIsDynamicRate ? "[dynamic rates]" : ""));
François Gaffie112b0af2015-11-19 16:13:25 +0100103 if (mName.length() != 0) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700104 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
François Gaffie112b0af2015-11-19 16:13:25 +0100105 }
106 std::string formatLiteral;
107 if (FormatConverter::toString(mFormat, formatLiteral)) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700108 dst->append(base::StringPrintf("%*s- format: %s\n", spaces, "", formatLiteral.c_str()));
François Gaffie112b0af2015-11-19 16:13:25 +0100109 }
jiabin06e4bab2019-07-29 10:13:34 -0700110 if (!mSamplingRates.empty()) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700111 dst->append(base::StringPrintf("%*s- sampling rates:", spaces, ""));
jiabin06e4bab2019-07-29 10:13:34 -0700112 for (auto it = mSamplingRates.begin(); it != mSamplingRates.end();) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700113 dst->append(base::StringPrintf("%d", *it));
jiabin06e4bab2019-07-29 10:13:34 -0700114 dst->append(++it == mSamplingRates.end() ? "" : ", ");
François Gaffie112b0af2015-11-19 16:13:25 +0100115 }
Andy Hungbb54e202018-10-05 11:42:02 -0700116 dst->append("\n");
François Gaffie112b0af2015-11-19 16:13:25 +0100117 }
118
jiabin06e4bab2019-07-29 10:13:34 -0700119 if (!mChannelMasks.empty()) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700120 dst->append(base::StringPrintf("%*s- channel masks:", spaces, ""));
jiabin06e4bab2019-07-29 10:13:34 -0700121 for (auto it = mChannelMasks.begin(); it != mChannelMasks.end();) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700122 dst->append(base::StringPrintf("0x%04x", *it));
jiabin06e4bab2019-07-29 10:13:34 -0700123 dst->append(++it == mChannelMasks.end() ? "" : ", ");
François Gaffie112b0af2015-11-19 16:13:25 +0100124 }
Andy Hungbb54e202018-10-05 11:42:02 -0700125 dst->append("\n");
François Gaffie112b0af2015-11-19 16:13:25 +0100126 }
jiabin82e56932021-03-05 06:35:19 +0000127
128 dst->append(base::StringPrintf(
129 "%*s- encapsulation type: %#x\n", spaces, "", mEncapsulationType));
François Gaffie112b0af2015-11-19 16:13:25 +0100130}
131
jiabin49e69a12019-10-15 16:04:13 -0700132bool 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() &&
jiabin82e56932021-03-05 06:35:19 +0000141 mIsDynamicRate == other->isDynamicRate() &&
142 mEncapsulationType == other->getEncapsulationType();
jiabin49e69a12019-10-15 16:04:13 -0700143}
144
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800145AudioProfile& AudioProfile::operator=(const AudioProfile& other) {
146 mName = other.mName;
147 mFormat = other.mFormat;
148 mChannelMasks = other.mChannelMasks;
149 mSamplingRates = other.mSamplingRates;
jiabin82e56932021-03-05 06:35:19 +0000150 mEncapsulationType = other.mEncapsulationType;
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800151 mIsDynamicFormat = other.mIsDynamicFormat;
152 mIsDynamicChannels = other.mIsDynamicChannels;
153 mIsDynamicRate = other.mIsDynamicRate;
154 return *this;
jiabin17058fa2019-10-08 17:33:38 -0700155}
156
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800157status_t AudioProfile::writeToParcel(Parcel *parcel) const {
158 media::AudioProfile parcelable = VALUE_OR_RETURN_STATUS(toParcelable());
159 return parcelable.writeToParcel(parcel);
160 }
161
162ConversionResult<media::AudioProfile>
163AudioProfile::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;
jiabin82e56932021-03-05 06:35:19 +0000176 parcelable.encapsulationType = VALUE_OR_RETURN(
177 legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(mEncapsulationType));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800178 return parcelable;
179}
180
181status_t AudioProfile::readFromParcel(const Parcel *parcel) {
182 media::AudioProfile parcelable;
183 if (status_t status = parcelable.readFromParcel(parcel); status != OK) {
jiabin17058fa2019-10-08 17:33:38 -0700184 return status;
185 }
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800186 *this = *VALUE_OR_RETURN_STATUS(fromParcelable(parcelable));
187 return OK;
188}
189
190ConversionResult<sp<AudioProfile>>
191AudioProfile::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;
jiabin82e56932021-03-05 06:35:19 +0000204 legacy->mEncapsulationType = VALUE_OR_RETURN(
205 aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(
206 parcelable.encapsulationType));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800207 return legacy;
208}
209
210ConversionResult<sp<AudioProfile>>
211aidl2legacy_AudioProfile(const media::AudioProfile& aidl) {
212 return AudioProfile::fromParcelable(aidl);
213}
214
215ConversionResult<media::AudioProfile>
216legacy2aidl_AudioProfile(const sp<AudioProfile>& legacy) {
217 return legacy->toParcelable();
jiabin17058fa2019-10-08 17:33:38 -0700218}
219
jiabin3e277cc2019-09-10 14:27:34 -0700220ssize_t AudioProfileVector::add(const sp<AudioProfile> &profile)
Mikhail Naganove50f6282018-07-26 16:20:43 -0700221{
jiabin06e4bab2019-07-29 10:13:34 -0700222 ssize_t index = size();
223 push_back(profile);
Mikhail Naganove50f6282018-07-26 16:20:43 -0700224 return index;
225}
226
jiabin3e277cc2019-09-10 14:27:34 -0700227void AudioProfileVector::clearProfiles()
Mikhail Naganove50f6282018-07-26 16:20:43 -0700228{
jiabin06e4bab2019-07-29 10:13:34 -0700229 for (auto it = begin(); it != end();) {
230 if ((*it)->isDynamicFormat() && (*it)->hasValidFormat()) {
231 it = erase(it);
232 } else {
233 (*it)->clear();
234 ++it;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700235 }
Mikhail Naganove50f6282018-07-26 16:20:43 -0700236 }
237}
238
jiabin3e277cc2019-09-10 14:27:34 -0700239sp<AudioProfile> AudioProfileVector::getFirstValidProfile() const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700240{
jiabin06e4bab2019-07-29 10:13:34 -0700241 for (const auto &profile : *this) {
242 if (profile->isValid()) {
243 return profile;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700244 }
245 }
jiabin06e4bab2019-07-29 10:13:34 -0700246 return nullptr;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700247}
248
jiabin3e277cc2019-09-10 14:27:34 -0700249sp<AudioProfile> AudioProfileVector::getFirstValidProfileFor(audio_format_t format) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700250{
jiabin06e4bab2019-07-29 10:13:34 -0700251 for (const auto &profile : *this) {
252 if (profile->isValid() && profile->getFormat() == format) {
253 return profile;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700254 }
255 }
jiabin06e4bab2019-07-29 10:13:34 -0700256 return nullptr;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700257}
258
jiabin3e277cc2019-09-10 14:27:34 -0700259FormatVector AudioProfileVector::getSupportedFormats() const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700260{
261 FormatVector supportedFormats;
jiabin06e4bab2019-07-29 10:13:34 -0700262 for (const auto &profile : *this) {
263 if (profile->hasValidFormat()) {
264 supportedFormats.push_back(profile->getFormat());
Mikhail Naganove50f6282018-07-26 16:20:43 -0700265 }
266 }
267 return supportedFormats;
268}
269
jiabin3e277cc2019-09-10 14:27:34 -0700270bool AudioProfileVector::hasDynamicChannelsFor(audio_format_t format) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700271{
jiabin06e4bab2019-07-29 10:13:34 -0700272 for (const auto &profile : *this) {
Mikhail Naganove50f6282018-07-26 16:20:43 -0700273 if (profile->getFormat() == format && profile->isDynamicChannels()) {
274 return true;
275 }
276 }
277 return false;
278}
279
jiabin3e277cc2019-09-10 14:27:34 -0700280bool AudioProfileVector::hasDynamicFormat() const
jiabin9bb3a1e2019-08-19 10:10:17 -0700281{
282 for (const auto &profile : *this) {
283 if (profile->isDynamicFormat()) {
284 return true;
285 }
286 }
287 return false;
288}
289
jiabin3e277cc2019-09-10 14:27:34 -0700290bool AudioProfileVector::hasDynamicProfile() const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700291{
jiabin06e4bab2019-07-29 10:13:34 -0700292 for (const auto &profile : *this) {
293 if (profile->isDynamic()) {
Mikhail Naganove50f6282018-07-26 16:20:43 -0700294 return true;
295 }
296 }
297 return false;
298}
299
jiabin3e277cc2019-09-10 14:27:34 -0700300bool AudioProfileVector::hasDynamicRateFor(audio_format_t format) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700301{
jiabin06e4bab2019-07-29 10:13:34 -0700302 for (const auto &profile : *this) {
Mikhail Naganove50f6282018-07-26 16:20:43 -0700303 if (profile->getFormat() == format && profile->isDynamicRate()) {
304 return true;
305 }
306 }
307 return false;
308}
309
jiabinb4fed192020-09-22 14:45:40 -0700310bool 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
jiabin3e277cc2019-09-10 14:27:34 -0700320void AudioProfileVector::dump(std::string *dst, int spaces) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700321{
jiabin9bb3a1e2019-08-19 10:10:17 -0700322 dst->append(base::StringPrintf("%*s- Profiles:\n", spaces, ""));
Mikhail Naganove50f6282018-07-26 16:20:43 -0700323 for (size_t i = 0; i < size(); i++) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700324 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 Naganove50f6282018-07-26 16:20:43 -0700328 }
329}
330
jiabin17058fa2019-10-08 17:33:38 -0700331status_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
343status_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
jiabin49e69a12019-10-15 16:04:13 -0700358bool 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-Tsvi50e016a2020-11-12 14:26:12 -0800366ConversionResult<AudioProfileVector>
367aidl2legacy_AudioProfileVector(const std::vector<media::AudioProfile>& aidl) {
368 return convertContainer<AudioProfileVector>(aidl, aidl2legacy_AudioProfile);
369}
370
371ConversionResult<std::vector<media::AudioProfile>>
372legacy2aidl_AudioProfileVector(const AudioProfileVector& legacy) {
373 return convertContainer<std::vector<media::AudioProfile>>(legacy, legacy2aidl_AudioProfile);
374}
375
jiabinbce0c1d2020-10-05 11:20:18 -0700376AudioProfileVector 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 Naganov1b2a7942017-12-08 10:18:09 -0800403} // namespace android