blob: 905a0caa5d46ae599385ff6a6dce93474dfa8d79 [file] [log] [blame]
jiabin5740f082019-08-19 15:08:30 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
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 */
jiabine1284852019-09-11 10:15:46 -070016#define LOG_TAG "AudioPort"
jiabin5740f082019-08-19 15:08:30 -070017
18#include <algorithm>
jiabin82e56932021-03-05 06:35:19 +000019#include <utility>
jiabin5740f082019-08-19 15:08:30 -070020
jiabin82e56932021-03-05 06:35:19 +000021#include <android/media/ExtraAudioDescriptor.h>
jiabin5740f082019-08-19 15:08:30 -070022#include <android-base/stringprintf.h>
jiabine1284852019-09-11 10:15:46 -070023#include <media/AudioPort.h>
jiabin5740f082019-08-19 15:08:30 -070024#include <utils/Log.h>
25
26namespace android {
27
jiabin4ef93452019-09-10 14:29:54 -070028void AudioPort::importAudioPort(const sp<AudioPort>& port, bool force __unused)
29{
30 for (const auto& profileToImport : port->mProfiles) {
31 // Import only valid port, i.e. valid format, non empty rates and channels masks
32 if (!profileToImport->isValid()) {
33 continue;
34 }
35 if (std::find_if(mProfiles.begin(), mProfiles.end(),
36 [profileToImport](const auto &profile) {
37 return *profile == *profileToImport; }) == mProfiles.end()) {
38 addAudioProfile(profileToImport);
39 }
40 }
41}
42
jiabinb4fed192020-09-22 14:45:40 -070043void AudioPort::importAudioPort(const audio_port_v7 &port) {
44 for (size_t i = 0; i < port.num_audio_profiles; ++i) {
45 sp<AudioProfile> profile = new AudioProfile(port.audio_profiles[i].format,
46 ChannelMaskSet(port.audio_profiles[i].channel_masks,
47 port.audio_profiles[i].channel_masks +
48 port.audio_profiles->num_channel_masks),
49 SampleRateSet(port.audio_profiles[i].sample_rates,
50 port.audio_profiles[i].sample_rates +
jiabin82e56932021-03-05 06:35:19 +000051 port.audio_profiles[i].num_sample_rates),
52 port.audio_profiles[i].encapsulation_type);
jiabinb4fed192020-09-22 14:45:40 -070053 if (!mProfiles.contains(profile)) {
54 addAudioProfile(profile);
55 }
56 }
jiabin82e56932021-03-05 06:35:19 +000057
58 for (size_t i = 0; i < port.num_extra_audio_descriptors; ++i) {
59 auto convertedResult = legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(
60 port.extra_audio_descriptors[i]);
61 if (!convertedResult.ok()) {
62 ALOGE("%s, failed to convert extra audio descriptor", __func__);
63 continue;
64 }
65 if (std::find(mExtraAudioDescriptors.begin(),
66 mExtraAudioDescriptors.end(),
67 convertedResult.value()) == mExtraAudioDescriptors.end()) {
68 mExtraAudioDescriptors.push_back(std::move(convertedResult.value()));
69 }
70 }
jiabinb4fed192020-09-22 14:45:40 -070071}
72
jiabin4ef93452019-09-10 14:29:54 -070073void AudioPort::toAudioPort(struct audio_port *port) const {
jiabin5740f082019-08-19 15:08:30 -070074 // TODO: update this function once audio_port structure reflects the new profile definition.
75 // For compatibility reason: flatening the AudioProfile into audio_port structure.
76 FormatSet flatenedFormats;
77 SampleRateSet flatenedRates;
78 ChannelMaskSet flatenedChannels;
jiabin3e277cc2019-09-10 14:27:34 -070079 for (const auto& profile : mProfiles) {
jiabin5740f082019-08-19 15:08:30 -070080 if (profile->isValid()) {
81 audio_format_t formatToExport = profile->getFormat();
82 const SampleRateSet &ratesToExport = profile->getSampleRates();
83 const ChannelMaskSet &channelsToExport = profile->getChannels();
84
85 flatenedFormats.insert(formatToExport);
86 flatenedRates.insert(ratesToExport.begin(), ratesToExport.end());
87 flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end());
88
89 if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
90 flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
91 flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) {
92 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
93 return;
94 }
95 }
96 }
jiabinb4fed192020-09-22 14:45:40 -070097 toAudioPortBase(port);
jiabin5740f082019-08-19 15:08:30 -070098 port->num_sample_rates = flatenedRates.size();
99 port->num_channel_masks = flatenedChannels.size();
100 port->num_formats = flatenedFormats.size();
101 std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates);
102 std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks);
103 std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats);
jiabinb4fed192020-09-22 14:45:40 -0700104}
jiabin5740f082019-08-19 15:08:30 -0700105
jiabinb4fed192020-09-22 14:45:40 -0700106void AudioPort::toAudioPort(struct audio_port_v7 *port) const {
107 toAudioPortBase(port);
108 port->num_audio_profiles = 0;
109 for (const auto& profile : mProfiles) {
110 if (profile->isValid()) {
111 const SampleRateSet &sampleRates = profile->getSampleRates();
112 const ChannelMaskSet &channelMasks = profile->getChannels();
jiabin5740f082019-08-19 15:08:30 -0700113
jiabinb4fed192020-09-22 14:45:40 -0700114 if (sampleRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
115 channelMasks.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
116 port->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
117 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
jiabin82e56932021-03-05 06:35:19 +0000118 break;
jiabinb4fed192020-09-22 14:45:40 -0700119 }
120
121 auto& dstProfile = port->audio_profiles[port->num_audio_profiles++];
122 dstProfile.format = profile->getFormat();
123 dstProfile.num_sample_rates = sampleRates.size();
124 std::copy(sampleRates.begin(), sampleRates.end(),
125 std::begin(dstProfile.sample_rates));
126 dstProfile.num_channel_masks = channelMasks.size();
127 std::copy(channelMasks.begin(), channelMasks.end(),
128 std::begin(dstProfile.channel_masks));
jiabin82e56932021-03-05 06:35:19 +0000129 dstProfile.encapsulation_type = profile->getEncapsulationType();
jiabinb4fed192020-09-22 14:45:40 -0700130 }
jiabin5740f082019-08-19 15:08:30 -0700131 }
jiabin82e56932021-03-05 06:35:19 +0000132
133 port->num_extra_audio_descriptors = 0;
134 for (const auto& desc : mExtraAudioDescriptors) {
135 if (port->num_extra_audio_descriptors >= AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS) {
136 ALOGE("%s: bailing out: cannot export extra audio descriptor to port config", __func__);
137 return;
138 }
139
140 auto convertedResult = aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(desc);
141 if (!convertedResult.ok()) {
142 ALOGE("%s: failed to convert extra audio descriptor", __func__);
143 continue;
144 }
145 port->extra_audio_descriptors[port->num_extra_audio_descriptors++] =
146 std::move(convertedResult.value());
147 }
jiabin5740f082019-08-19 15:08:30 -0700148}
149
jiabin4ef93452019-09-10 14:29:54 -0700150void AudioPort::dump(std::string *dst, int spaces, bool verbose) const {
jiabin5740f082019-08-19 15:08:30 -0700151 if (!mName.empty()) {
152 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
153 }
154 if (verbose) {
155 std::string profilesStr;
jiabin3e277cc2019-09-10 14:27:34 -0700156 mProfiles.dump(&profilesStr, spaces);
jiabin5740f082019-08-19 15:08:30 -0700157 dst->append(profilesStr);
jiabin82e56932021-03-05 06:35:19 +0000158 if (!mExtraAudioDescriptors.empty()) {
159 dst->append(base::StringPrintf("%*s- extra audio descriptors: \n", spaces, ""));
160 const int eadSpaces = spaces + 4;
161 const int descSpaces = eadSpaces + 4;
162 for (size_t i = 0; i < mExtraAudioDescriptors.size(); i++) {
163 dst->append(
164 base::StringPrintf("%*s extra audio descriptor %zu:\n", eadSpaces, "", i));
165 dst->append(base::StringPrintf(
166 "%*s- standard: %u\n", descSpaces, "", mExtraAudioDescriptors[i].standard));
167 dst->append(base::StringPrintf("%*s- descriptor:", descSpaces, ""));
168 for (auto v : mExtraAudioDescriptors[i].audioDescriptor) {
169 dst->append(base::StringPrintf(" %02x", v));
170 }
171 dst->append("\n");
172 }
173 }
jiabin5740f082019-08-19 15:08:30 -0700174
175 if (mGains.size() != 0) {
176 dst->append(base::StringPrintf("%*s- gains:\n", spaces, ""));
177 for (size_t i = 0; i < mGains.size(); i++) {
178 std::string gainStr;
179 mGains[i]->dump(&gainStr, spaces + 2, i);
180 dst->append(gainStr);
181 }
182 }
183 }
184}
185
jiabin4ef93452019-09-10 14:29:54 -0700186void AudioPort::log(const char* indent) const
187{
188 ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole);
189}
190
jiabin49e69a12019-10-15 16:04:13 -0700191bool AudioPort::equals(const sp<AudioPort> &other) const
192{
193 return other != nullptr &&
194 mGains.equals(other->getGains()) &&
195 mName.compare(other->getName()) == 0 &&
196 mType == other->getType() &&
197 mRole == other->getRole() &&
jiabin82e56932021-03-05 06:35:19 +0000198 mProfiles.equals(other->getAudioProfiles()) &&
199 mExtraAudioDescriptors == other->getExtraAudioDescriptors();
jiabin49e69a12019-10-15 16:04:13 -0700200}
201
jiabin17058fa2019-10-08 17:33:38 -0700202status_t AudioPort::writeToParcel(Parcel *parcel) const
203{
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800204 media::AudioPort parcelable;
205 return writeToParcelable(&parcelable)
206 ?: parcelable.writeToParcel(parcel);
jiabin17058fa2019-10-08 17:33:38 -0700207}
208
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800209status_t AudioPort::writeToParcelable(media::AudioPort* parcelable) const {
210 parcelable->name = mName;
211 parcelable->type = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_type_t_AudioPortType(mType));
212 parcelable->role = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_role_t_AudioPortRole(mRole));
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000213 auto aidlProfiles = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700214 legacy2aidl_AudioProfileVector(mProfiles, useInputChannelMask()));
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000215 parcelable->profiles = aidlProfiles.first;
216 parcelable->profilesSys = aidlProfiles.second;
jiabin82e56932021-03-05 06:35:19 +0000217 parcelable->extraAudioDescriptors = mExtraAudioDescriptors;
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000218 auto aidlGains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains));
219 parcelable->gains = aidlGains.first;
220 parcelable->gainsSys = aidlGains.second;
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800221 return OK;
222}
223
224status_t AudioPort::readFromParcel(const Parcel *parcel) {
225 media::AudioPort parcelable;
226 return parcelable.readFromParcel(parcel)
227 ?: readFromParcelable(parcelable);
228}
229
230status_t AudioPort::readFromParcelable(const media::AudioPort& parcelable) {
231 mName = parcelable.name;
232 mType = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortType_audio_port_type_t(parcelable.type));
233 mRole = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.role));
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700234 mProfiles = VALUE_OR_RETURN_STATUS(
Mikhail Naganov89818ba2021-09-21 20:37:13 +0000235 aidl2legacy_AudioProfileVector(
236 std::make_pair(parcelable.profiles, parcelable.profilesSys),
237 useInputChannelMask()));
jiabin82e56932021-03-05 06:35:19 +0000238 mExtraAudioDescriptors = parcelable.extraAudioDescriptors;
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000239 mGains = VALUE_OR_RETURN_STATUS(
240 aidl2legacy_AudioGains(std::make_pair(parcelable.gains, parcelable.gainsSys)));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800241 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700242}
243
jiabin4ef93452019-09-10 14:29:54 -0700244// --- AudioPortConfig class implementation
245
246status_t AudioPortConfig::applyAudioPortConfig(
247 const struct audio_port_config *config,
248 struct audio_port_config *backupConfig __unused)
249{
250 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
251 mSamplingRate = config->sample_rate;
252 }
253 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
254 mChannelMask = config->channel_mask;
255 }
256 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
257 mFormat = config->format;
258 }
259 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
260 mGain = config->gain;
261 }
262
263 return NO_ERROR;
264}
265
266namespace {
267
268template<typename T>
269void updateField(
270 const T& portConfigField, T audio_port_config::*port_config_field,
271 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
272 unsigned int configMask, T defaultValue)
273{
274 if (dstConfig->config_mask & configMask) {
275 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
276 dstConfig->*port_config_field = srcConfig->*port_config_field;
277 } else {
278 dstConfig->*port_config_field = portConfigField;
279 }
280 } else {
281 dstConfig->*port_config_field = defaultValue;
282 }
283}
284
285} // namespace
286
287void AudioPortConfig::toAudioPortConfig(
288 struct audio_port_config *dstConfig,
289 const struct audio_port_config *srcConfig) const
290{
291 updateField(mSamplingRate, &audio_port_config::sample_rate,
292 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
293 updateField(mChannelMask, &audio_port_config::channel_mask,
294 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
295 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
296 updateField(mFormat, &audio_port_config::format,
297 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
298 dstConfig->id = mId;
299
300 sp<AudioPort> audioport = getAudioPort();
301 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
302 dstConfig->gain = mGain;
303 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
304 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
305 dstConfig->gain = srcConfig->gain;
306 }
307 } else {
308 dstConfig->gain.index = -1;
309 }
310 if (dstConfig->gain.index != -1) {
311 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
312 } else {
313 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
314 }
315}
316
317bool AudioPortConfig::hasGainController(bool canUseForVolume) const
318{
319 sp<AudioPort> audioport = getAudioPort();
320 if (!audioport) {
321 return false;
322 }
323 return canUseForVolume ? audioport->getGains().canUseForVolume()
324 : audioport->getGains().size() > 0;
325}
326
jiabin49e69a12019-10-15 16:04:13 -0700327bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
328{
329 return other != nullptr &&
330 mSamplingRate == other->getSamplingRate() &&
331 mFormat == other->getFormat() &&
332 mChannelMask == other->getChannelMask() &&
333 // Compare audio gain config
334 mGain.index == other->mGain.index &&
335 mGain.mode == other->mGain.mode &&
336 mGain.channel_mask == other->mGain.channel_mask &&
337 std::equal(std::begin(mGain.values), std::end(mGain.values),
338 std::begin(other->mGain.values)) &&
339 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
340}
341
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700342status_t AudioPortConfig::writeToParcelable(
343 media::AudioPortConfig* parcelable, bool isInput) const {
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800344 parcelable->sampleRate = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate));
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700345 parcelable->format = VALUE_OR_RETURN_STATUS(
346 legacy2aidl_audio_format_t_AudioFormatDescription(mFormat));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800347 parcelable->channelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700348 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mChannelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800349 parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
350 parcelable->gain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.index));
351 parcelable->gain.mode = VALUE_OR_RETURN_STATUS(
Andy Hung973638a2020-12-08 20:47:45 -0800352 legacy2aidl_audio_gain_mode_t_int32_t_mask(mGain.mode));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800353 parcelable->gain.channelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700354 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mGain.channel_mask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800355 parcelable->gain.rampDurationMs = VALUE_OR_RETURN_STATUS(
356 convertIntegral<int32_t>(mGain.ramp_duration_ms));
357 parcelable->gain.values = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<int32_t>>(
358 mGain.values, convertIntegral<int32_t, int>));
359 return OK;
360}
361
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700362status_t AudioPortConfig::readFromParcelable(
363 const media::AudioPortConfig& parcelable, bool isInput) {
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800364 mSamplingRate = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.sampleRate));
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700365 mFormat = VALUE_OR_RETURN_STATUS(
366 aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800367 mChannelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700368 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(parcelable.channelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800369 mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id));
370 mGain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.index));
371 mGain.mode = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0cf92652020-11-23 13:23:00 -0800372 aidl2legacy_int32_t_audio_gain_mode_t_mask(parcelable.gain.mode));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800373 mGain.channel_mask = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700374 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
375 parcelable.gain.channelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800376 mGain.ramp_duration_ms = VALUE_OR_RETURN_STATUS(
377 convertIntegral<unsigned int>(parcelable.gain.rampDurationMs));
378 if (parcelable.gain.values.size() > std::size(mGain.values)) {
jiabin17058fa2019-10-08 17:33:38 -0700379 return BAD_VALUE;
380 }
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800381 for (size_t i = 0; i < parcelable.gain.values.size(); ++i) {
382 mGain.values[i] = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.values[i]));
383 }
384 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700385}
386
387} // namespace android