blob: 47b2d5488e75162856c6fb50404bf5d725293b95 [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 -0800157ConversionResult<media::AudioProfile>
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700158AudioProfile::toParcelable(bool isInput) const {
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800159 media::AudioProfile parcelable;
160 parcelable.name = mName;
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700161 parcelable.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription(mFormat));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800162 parcelable.channelMasks = VALUE_OR_RETURN(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700163 convertContainer<std::vector<media::AudioChannelLayout>>(
Mikhail Naganov2d8df4e2021-06-03 13:59:13 -0700164 mChannelMasks,
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700165 [isInput](audio_channel_mask_t m) {
166 return legacy2aidl_audio_channel_mask_t_AudioChannelLayout(m, isInput);
167 }));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800168 parcelable.samplingRates = VALUE_OR_RETURN(
169 convertContainer<std::vector<int32_t>>(mSamplingRates,
170 convertIntegral<int32_t, uint32_t>));
171 parcelable.isDynamicFormat = mIsDynamicFormat;
172 parcelable.isDynamicChannels = mIsDynamicChannels;
173 parcelable.isDynamicRate = mIsDynamicRate;
jiabin82e56932021-03-05 06:35:19 +0000174 parcelable.encapsulationType = VALUE_OR_RETURN(
175 legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(mEncapsulationType));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800176 return parcelable;
177}
178
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800179ConversionResult<sp<AudioProfile>>
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700180AudioProfile::fromParcelable(const media::AudioProfile& parcelable, bool isInput) {
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800181 sp<AudioProfile> legacy = new AudioProfile();
182 legacy->mName = parcelable.name;
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700183 legacy->mFormat = VALUE_OR_RETURN(
184 aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800185 legacy->mChannelMasks = VALUE_OR_RETURN(
186 convertContainer<ChannelMaskSet>(parcelable.channelMasks,
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700187 [isInput](const media::AudioChannelLayout& l) {
188 return aidl2legacy_AudioChannelLayout_audio_channel_mask_t(l, isInput);
189 }));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800190 legacy->mSamplingRates = VALUE_OR_RETURN(
191 convertContainer<SampleRateSet>(parcelable.samplingRates,
192 convertIntegral<uint32_t, int32_t>));
193 legacy->mIsDynamicFormat = parcelable.isDynamicFormat;
194 legacy->mIsDynamicChannels = parcelable.isDynamicChannels;
195 legacy->mIsDynamicRate = parcelable.isDynamicRate;
jiabin82e56932021-03-05 06:35:19 +0000196 legacy->mEncapsulationType = VALUE_OR_RETURN(
197 aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(
198 parcelable.encapsulationType));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800199 return legacy;
200}
201
202ConversionResult<sp<AudioProfile>>
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700203aidl2legacy_AudioProfile(const media::AudioProfile& aidl, bool isInput) {
204 return AudioProfile::fromParcelable(aidl, isInput);
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800205}
206
207ConversionResult<media::AudioProfile>
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700208legacy2aidl_AudioProfile(const sp<AudioProfile>& legacy, bool isInput) {
209 return legacy->toParcelable(isInput);
jiabin17058fa2019-10-08 17:33:38 -0700210}
211
jiabin3e277cc2019-09-10 14:27:34 -0700212ssize_t AudioProfileVector::add(const sp<AudioProfile> &profile)
Mikhail Naganove50f6282018-07-26 16:20:43 -0700213{
jiabin06e4bab2019-07-29 10:13:34 -0700214 ssize_t index = size();
215 push_back(profile);
Mikhail Naganove50f6282018-07-26 16:20:43 -0700216 return index;
217}
218
jiabin3e277cc2019-09-10 14:27:34 -0700219void AudioProfileVector::clearProfiles()
Mikhail Naganove50f6282018-07-26 16:20:43 -0700220{
jiabin06e4bab2019-07-29 10:13:34 -0700221 for (auto it = begin(); it != end();) {
222 if ((*it)->isDynamicFormat() && (*it)->hasValidFormat()) {
223 it = erase(it);
224 } else {
225 (*it)->clear();
226 ++it;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700227 }
Mikhail Naganove50f6282018-07-26 16:20:43 -0700228 }
229}
230
jiabin3e277cc2019-09-10 14:27:34 -0700231sp<AudioProfile> AudioProfileVector::getFirstValidProfile() const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700232{
jiabin06e4bab2019-07-29 10:13:34 -0700233 for (const auto &profile : *this) {
234 if (profile->isValid()) {
235 return profile;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700236 }
237 }
jiabin06e4bab2019-07-29 10:13:34 -0700238 return nullptr;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700239}
240
jiabin3e277cc2019-09-10 14:27:34 -0700241sp<AudioProfile> AudioProfileVector::getFirstValidProfileFor(audio_format_t format) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700242{
jiabin06e4bab2019-07-29 10:13:34 -0700243 for (const auto &profile : *this) {
244 if (profile->isValid() && profile->getFormat() == format) {
245 return profile;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700246 }
247 }
jiabin06e4bab2019-07-29 10:13:34 -0700248 return nullptr;
Mikhail Naganove50f6282018-07-26 16:20:43 -0700249}
250
jiabin3e277cc2019-09-10 14:27:34 -0700251FormatVector AudioProfileVector::getSupportedFormats() const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700252{
253 FormatVector supportedFormats;
jiabin06e4bab2019-07-29 10:13:34 -0700254 for (const auto &profile : *this) {
255 if (profile->hasValidFormat()) {
256 supportedFormats.push_back(profile->getFormat());
Mikhail Naganove50f6282018-07-26 16:20:43 -0700257 }
258 }
259 return supportedFormats;
260}
261
jiabin3e277cc2019-09-10 14:27:34 -0700262bool AudioProfileVector::hasDynamicChannelsFor(audio_format_t format) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700263{
jiabin06e4bab2019-07-29 10:13:34 -0700264 for (const auto &profile : *this) {
Mikhail Naganove50f6282018-07-26 16:20:43 -0700265 if (profile->getFormat() == format && profile->isDynamicChannels()) {
266 return true;
267 }
268 }
269 return false;
270}
271
jiabin3e277cc2019-09-10 14:27:34 -0700272bool AudioProfileVector::hasDynamicFormat() const
jiabin9bb3a1e2019-08-19 10:10:17 -0700273{
274 for (const auto &profile : *this) {
275 if (profile->isDynamicFormat()) {
276 return true;
277 }
278 }
279 return false;
280}
281
jiabin3e277cc2019-09-10 14:27:34 -0700282bool AudioProfileVector::hasDynamicProfile() const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700283{
jiabin06e4bab2019-07-29 10:13:34 -0700284 for (const auto &profile : *this) {
285 if (profile->isDynamic()) {
Mikhail Naganove50f6282018-07-26 16:20:43 -0700286 return true;
287 }
288 }
289 return false;
290}
291
jiabin3e277cc2019-09-10 14:27:34 -0700292bool AudioProfileVector::hasDynamicRateFor(audio_format_t format) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700293{
jiabin06e4bab2019-07-29 10:13:34 -0700294 for (const auto &profile : *this) {
Mikhail Naganove50f6282018-07-26 16:20:43 -0700295 if (profile->getFormat() == format && profile->isDynamicRate()) {
296 return true;
297 }
298 }
299 return false;
300}
301
jiabinb4fed192020-09-22 14:45:40 -0700302bool AudioProfileVector::contains(const sp<AudioProfile>& profile) const
303{
304 for (const auto& audioProfile : *this) {
305 if (audioProfile->equals(profile)) {
306 return true;
307 }
308 }
309 return false;
310}
311
jiabin3e277cc2019-09-10 14:27:34 -0700312void AudioProfileVector::dump(std::string *dst, int spaces) const
Mikhail Naganove50f6282018-07-26 16:20:43 -0700313{
jiabin9bb3a1e2019-08-19 10:10:17 -0700314 dst->append(base::StringPrintf("%*s- Profiles:\n", spaces, ""));
Mikhail Naganove50f6282018-07-26 16:20:43 -0700315 for (size_t i = 0; i < size(); i++) {
jiabin9bb3a1e2019-08-19 10:10:17 -0700316 dst->append(base::StringPrintf("%*sProfile %zu:", spaces + 4, "", i));
317 std::string profileStr;
318 at(i)->dump(&profileStr, spaces + 8);
319 dst->append(profileStr);
Mikhail Naganove50f6282018-07-26 16:20:43 -0700320 }
321}
322
jiabin49e69a12019-10-15 16:04:13 -0700323bool AudioProfileVector::equals(const AudioProfileVector& other) const
324{
325 return std::equal(begin(), end(), other.begin(), other.end(),
326 [](const sp<AudioProfile>& left, const sp<AudioProfile>& right) {
327 return left->equals(right);
328 });
329}
330
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800331ConversionResult<AudioProfileVector>
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700332aidl2legacy_AudioProfileVector(const std::vector<media::AudioProfile>& aidl, bool isInput) {
333 return convertContainer<AudioProfileVector>(aidl,
334 [isInput](const media::AudioProfile& p) {
335 return aidl2legacy_AudioProfile(p, isInput);
336 });
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800337}
338
339ConversionResult<std::vector<media::AudioProfile>>
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700340legacy2aidl_AudioProfileVector(const AudioProfileVector& legacy, bool isInput) {
341 return convertContainer<std::vector<media::AudioProfile>>(legacy,
342 [isInput](const sp<AudioProfile>& p) {
343 return legacy2aidl_AudioProfile(p, isInput);
344 });
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800345}
346
jiabinbce0c1d2020-10-05 11:20:18 -0700347AudioProfileVector intersectAudioProfiles(const AudioProfileVector& profiles1,
348 const AudioProfileVector& profiles2)
349{
350 std::map<audio_format_t, std::pair<ChannelMaskSet, SampleRateSet>> infos2;
351 for (const auto& profile : profiles2) {
352 infos2.emplace(profile->getFormat(),
353 std::make_pair(profile->getChannels(), profile->getSampleRates()));
354 }
355 AudioProfileVector profiles;
356 for (const auto& profile : profiles1) {
357 const auto it = infos2.find(profile->getFormat());
358 if (it == infos2.end()) {
359 continue;
360 }
361 ChannelMaskSet channelMasks = SetIntersection(profile->getChannels(), it->second.first);
362 if (channelMasks.empty()) {
363 continue;
364 }
365 SampleRateSet sampleRates = SetIntersection(profile->getSampleRates(), it->second.second);
366 if (sampleRates.empty()) {
367 continue;
368 }
369 profiles.push_back(new AudioProfile(profile->getFormat(), channelMasks, sampleRates));
370 }
371 return profiles;
372}
373
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800374} // namespace android