blob: a00e00d9a227f4acc90f424f219f0abaf0aa4bc1 [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
21#include <android-base/stringprintf.h>
jiabine1284852019-09-11 10:15:46 -070022#include <media/AudioPort.h>
jiabin5740f082019-08-19 15:08:30 -070023#include <utils/Log.h>
24
25namespace android {
26
jiabin4ef93452019-09-10 14:29:54 -070027void AudioPort::importAudioPort(const sp<AudioPort>& port, bool force __unused)
28{
29 for (const auto& profileToImport : port->mProfiles) {
30 // Import only valid port, i.e. valid format, non empty rates and channels masks
31 if (!profileToImport->isValid()) {
32 continue;
33 }
34 if (std::find_if(mProfiles.begin(), mProfiles.end(),
35 [profileToImport](const auto &profile) {
36 return *profile == *profileToImport; }) == mProfiles.end()) {
37 addAudioProfile(profileToImport);
38 }
39 }
40}
41
jiabinb4fed192020-09-22 14:45:40 -070042void AudioPort::importAudioPort(const audio_port_v7 &port) {
43 for (size_t i = 0; i < port.num_audio_profiles; ++i) {
44 sp<AudioProfile> profile = new AudioProfile(port.audio_profiles[i].format,
45 ChannelMaskSet(port.audio_profiles[i].channel_masks,
46 port.audio_profiles[i].channel_masks +
47 port.audio_profiles->num_channel_masks),
48 SampleRateSet(port.audio_profiles[i].sample_rates,
49 port.audio_profiles[i].sample_rates +
jiabin82e56932021-03-05 06:35:19 +000050 port.audio_profiles[i].num_sample_rates),
51 port.audio_profiles[i].encapsulation_type);
jiabinb4fed192020-09-22 14:45:40 -070052 if (!mProfiles.contains(profile)) {
53 addAudioProfile(profile);
54 }
55 }
jiabin82e56932021-03-05 06:35:19 +000056
57 for (size_t i = 0; i < port.num_extra_audio_descriptors; ++i) {
58 auto convertedResult = legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(
59 port.extra_audio_descriptors[i]);
60 if (!convertedResult.ok()) {
61 ALOGE("%s, failed to convert extra audio descriptor", __func__);
62 continue;
63 }
64 if (std::find(mExtraAudioDescriptors.begin(),
65 mExtraAudioDescriptors.end(),
66 convertedResult.value()) == mExtraAudioDescriptors.end()) {
67 mExtraAudioDescriptors.push_back(std::move(convertedResult.value()));
68 }
69 }
jiabinb4fed192020-09-22 14:45:40 -070070}
71
jiabin4ef93452019-09-10 14:29:54 -070072void AudioPort::toAudioPort(struct audio_port *port) const {
jiabin5740f082019-08-19 15:08:30 -070073 // TODO: update this function once audio_port structure reflects the new profile definition.
74 // For compatibility reason: flatening the AudioProfile into audio_port structure.
75 FormatSet flatenedFormats;
76 SampleRateSet flatenedRates;
77 ChannelMaskSet flatenedChannels;
jiabin3e277cc2019-09-10 14:27:34 -070078 for (const auto& profile : mProfiles) {
jiabin5740f082019-08-19 15:08:30 -070079 if (profile->isValid()) {
80 audio_format_t formatToExport = profile->getFormat();
81 const SampleRateSet &ratesToExport = profile->getSampleRates();
82 const ChannelMaskSet &channelsToExport = profile->getChannels();
83
84 flatenedFormats.insert(formatToExport);
85 flatenedRates.insert(ratesToExport.begin(), ratesToExport.end());
86 flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end());
87
88 if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
89 flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
90 flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) {
91 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
92 return;
93 }
94 }
95 }
jiabinb4fed192020-09-22 14:45:40 -070096 toAudioPortBase(port);
jiabin5740f082019-08-19 15:08:30 -070097 port->num_sample_rates = flatenedRates.size();
98 port->num_channel_masks = flatenedChannels.size();
99 port->num_formats = flatenedFormats.size();
100 std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates);
101 std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks);
102 std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats);
jiabinb4fed192020-09-22 14:45:40 -0700103}
jiabin5740f082019-08-19 15:08:30 -0700104
jiabinb4fed192020-09-22 14:45:40 -0700105void AudioPort::toAudioPort(struct audio_port_v7 *port) const {
106 toAudioPortBase(port);
107 port->num_audio_profiles = 0;
108 for (const auto& profile : mProfiles) {
109 if (profile->isValid()) {
110 const SampleRateSet &sampleRates = profile->getSampleRates();
111 const ChannelMaskSet &channelMasks = profile->getChannels();
jiabin5740f082019-08-19 15:08:30 -0700112
jiabinb4fed192020-09-22 14:45:40 -0700113 if (sampleRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
114 channelMasks.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
115 port->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
116 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
jiabin82e56932021-03-05 06:35:19 +0000117 break;
jiabinb4fed192020-09-22 14:45:40 -0700118 }
119
120 auto& dstProfile = port->audio_profiles[port->num_audio_profiles++];
121 dstProfile.format = profile->getFormat();
122 dstProfile.num_sample_rates = sampleRates.size();
123 std::copy(sampleRates.begin(), sampleRates.end(),
124 std::begin(dstProfile.sample_rates));
125 dstProfile.num_channel_masks = channelMasks.size();
126 std::copy(channelMasks.begin(), channelMasks.end(),
127 std::begin(dstProfile.channel_masks));
jiabin82e56932021-03-05 06:35:19 +0000128 dstProfile.encapsulation_type = profile->getEncapsulationType();
jiabinb4fed192020-09-22 14:45:40 -0700129 }
jiabin5740f082019-08-19 15:08:30 -0700130 }
jiabin82e56932021-03-05 06:35:19 +0000131
132 port->num_extra_audio_descriptors = 0;
133 for (const auto& desc : mExtraAudioDescriptors) {
134 if (port->num_extra_audio_descriptors >= AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS) {
135 ALOGE("%s: bailing out: cannot export extra audio descriptor to port config", __func__);
136 return;
137 }
138
139 auto convertedResult = aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(desc);
140 if (!convertedResult.ok()) {
141 ALOGE("%s: failed to convert extra audio descriptor", __func__);
142 continue;
143 }
144 port->extra_audio_descriptors[port->num_extra_audio_descriptors++] =
145 std::move(convertedResult.value());
146 }
jiabin5740f082019-08-19 15:08:30 -0700147}
148
jiabin4ef93452019-09-10 14:29:54 -0700149void AudioPort::dump(std::string *dst, int spaces, bool verbose) const {
jiabin5740f082019-08-19 15:08:30 -0700150 if (!mName.empty()) {
151 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
152 }
153 if (verbose) {
154 std::string profilesStr;
jiabin3e277cc2019-09-10 14:27:34 -0700155 mProfiles.dump(&profilesStr, spaces);
jiabin5740f082019-08-19 15:08:30 -0700156 dst->append(profilesStr);
jiabin82e56932021-03-05 06:35:19 +0000157 if (!mExtraAudioDescriptors.empty()) {
158 dst->append(base::StringPrintf("%*s- extra audio descriptors: \n", spaces, ""));
159 const int eadSpaces = spaces + 4;
160 const int descSpaces = eadSpaces + 4;
161 for (size_t i = 0; i < mExtraAudioDescriptors.size(); i++) {
162 dst->append(
163 base::StringPrintf("%*s extra audio descriptor %zu:\n", eadSpaces, "", i));
164 dst->append(base::StringPrintf(
165 "%*s- standard: %u\n", descSpaces, "", mExtraAudioDescriptors[i].standard));
166 dst->append(base::StringPrintf("%*s- descriptor:", descSpaces, ""));
167 for (auto v : mExtraAudioDescriptors[i].audioDescriptor) {
168 dst->append(base::StringPrintf(" %02x", v));
169 }
170 dst->append("\n");
171 }
172 }
jiabin5740f082019-08-19 15:08:30 -0700173
174 if (mGains.size() != 0) {
175 dst->append(base::StringPrintf("%*s- gains:\n", spaces, ""));
176 for (size_t i = 0; i < mGains.size(); i++) {
177 std::string gainStr;
178 mGains[i]->dump(&gainStr, spaces + 2, i);
179 dst->append(gainStr);
180 }
181 }
182 }
183}
184
jiabin4ef93452019-09-10 14:29:54 -0700185void AudioPort::log(const char* indent) const
186{
187 ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole);
188}
189
jiabin49e69a12019-10-15 16:04:13 -0700190bool AudioPort::equals(const sp<AudioPort> &other) const
191{
192 return other != nullptr &&
193 mGains.equals(other->getGains()) &&
194 mName.compare(other->getName()) == 0 &&
195 mType == other->getType() &&
196 mRole == other->getRole() &&
jiabin82e56932021-03-05 06:35:19 +0000197 mProfiles.equals(other->getAudioProfiles()) &&
198 mExtraAudioDescriptors == other->getExtraAudioDescriptors();
jiabin49e69a12019-10-15 16:04:13 -0700199}
200
jiabin17058fa2019-10-08 17:33:38 -0700201status_t AudioPort::writeToParcel(Parcel *parcel) const
202{
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800203 media::AudioPort parcelable;
204 return writeToParcelable(&parcelable)
205 ?: parcelable.writeToParcel(parcel);
jiabin17058fa2019-10-08 17:33:38 -0700206}
207
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800208status_t AudioPort::writeToParcelable(media::AudioPort* parcelable) const {
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000209 parcelable->hal.name = mName;
210 parcelable->sys.type = VALUE_OR_RETURN_STATUS(
211 legacy2aidl_audio_port_type_t_AudioPortType(mType));
212 parcelable->sys.role = VALUE_OR_RETURN_STATUS(
213 legacy2aidl_audio_port_role_t_AudioPortRole(mRole));
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000214 auto aidlProfiles = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700215 legacy2aidl_AudioProfileVector(mProfiles, useInputChannelMask()));
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000216 parcelable->hal.profiles = aidlProfiles.first;
217 parcelable->sys.profiles = aidlProfiles.second;
218 parcelable->hal.extraAudioDescriptors = mExtraAudioDescriptors;
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000219 auto aidlGains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains));
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000220 parcelable->hal.gains = aidlGains.first;
221 parcelable->sys.gains = aidlGains.second;
Mikhail Naganov10506122021-10-19 10:14:23 -0700222 if (mType == AUDIO_PORT_TYPE_MIX) {
223 media::audio::common::AudioPortMixExt mixExt{};
224 mixExt.maxOpenStreamCount = maxOpenCount;
225 mixExt.maxActiveStreamCount = maxActiveCount;
226 mixExt.recommendedMuteDurationMs = recommendedMuteDurationMs;
227 parcelable->hal.ext = media::audio::common::AudioPortExt::make<
228 media::audio::common::AudioPortExt::mix>(mixExt);
229 }
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800230 return OK;
231}
232
233status_t AudioPort::readFromParcel(const Parcel *parcel) {
234 media::AudioPort parcelable;
235 return parcelable.readFromParcel(parcel)
236 ?: readFromParcelable(parcelable);
237}
238
239status_t AudioPort::readFromParcelable(const media::AudioPort& parcelable) {
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000240 mName = parcelable.hal.name;
241 mType = VALUE_OR_RETURN_STATUS(
242 aidl2legacy_AudioPortType_audio_port_type_t(parcelable.sys.type));
243 mRole = VALUE_OR_RETURN_STATUS(
244 aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.sys.role));
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700245 mProfiles = VALUE_OR_RETURN_STATUS(
Mikhail Naganov89818ba2021-09-21 20:37:13 +0000246 aidl2legacy_AudioProfileVector(
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000247 std::make_pair(parcelable.hal.profiles, parcelable.sys.profiles),
Mikhail Naganov89818ba2021-09-21 20:37:13 +0000248 useInputChannelMask()));
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000249 mExtraAudioDescriptors = parcelable.hal.extraAudioDescriptors;
Mikhail Naganov7d0b36b2021-09-22 23:58:41 +0000250 mGains = VALUE_OR_RETURN_STATUS(
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000251 aidl2legacy_AudioGains(std::make_pair(parcelable.hal.gains, parcelable.sys.gains)));
Mikhail Naganov10506122021-10-19 10:14:23 -0700252 if (mType == AUDIO_PORT_TYPE_MIX) {
253 const media::audio::common::AudioPortMixExt& mixExt =
254 parcelable.hal.ext.get<media::audio::common::AudioPortExt::mix>();
255 maxOpenCount = mixExt.maxOpenStreamCount;
256 maxActiveCount = mixExt.maxActiveStreamCount;
257 recommendedMuteDurationMs = mixExt.recommendedMuteDurationMs;
258 }
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800259 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700260}
261
jiabin4ef93452019-09-10 14:29:54 -0700262// --- AudioPortConfig class implementation
263
264status_t AudioPortConfig::applyAudioPortConfig(
265 const struct audio_port_config *config,
266 struct audio_port_config *backupConfig __unused)
267{
268 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
269 mSamplingRate = config->sample_rate;
270 }
271 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
272 mChannelMask = config->channel_mask;
273 }
274 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
275 mFormat = config->format;
276 }
277 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
278 mGain = config->gain;
279 }
280
281 return NO_ERROR;
282}
283
284namespace {
285
286template<typename T>
287void updateField(
288 const T& portConfigField, T audio_port_config::*port_config_field,
289 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
290 unsigned int configMask, T defaultValue)
291{
292 if (dstConfig->config_mask & configMask) {
293 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
294 dstConfig->*port_config_field = srcConfig->*port_config_field;
295 } else {
296 dstConfig->*port_config_field = portConfigField;
297 }
298 } else {
299 dstConfig->*port_config_field = defaultValue;
300 }
301}
302
303} // namespace
304
305void AudioPortConfig::toAudioPortConfig(
306 struct audio_port_config *dstConfig,
307 const struct audio_port_config *srcConfig) const
308{
309 updateField(mSamplingRate, &audio_port_config::sample_rate,
310 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
311 updateField(mChannelMask, &audio_port_config::channel_mask,
312 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
313 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
314 updateField(mFormat, &audio_port_config::format,
315 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
316 dstConfig->id = mId;
317
318 sp<AudioPort> audioport = getAudioPort();
319 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
320 dstConfig->gain = mGain;
321 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
322 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
323 dstConfig->gain = srcConfig->gain;
324 }
325 } else {
326 dstConfig->gain.index = -1;
327 }
328 if (dstConfig->gain.index != -1) {
329 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
330 } else {
331 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
332 }
333}
334
335bool AudioPortConfig::hasGainController(bool canUseForVolume) const
336{
337 sp<AudioPort> audioport = getAudioPort();
338 if (!audioport) {
339 return false;
340 }
341 return canUseForVolume ? audioport->getGains().canUseForVolume()
342 : audioport->getGains().size() > 0;
343}
344
jiabin49e69a12019-10-15 16:04:13 -0700345bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
346{
347 return other != nullptr &&
348 mSamplingRate == other->getSamplingRate() &&
349 mFormat == other->getFormat() &&
350 mChannelMask == other->getChannelMask() &&
351 // Compare audio gain config
352 mGain.index == other->mGain.index &&
353 mGain.mode == other->mGain.mode &&
354 mGain.channel_mask == other->mGain.channel_mask &&
355 std::equal(std::begin(mGain.values), std::end(mGain.values),
356 std::begin(other->mGain.values)) &&
357 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
358}
359
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700360status_t AudioPortConfig::writeToParcelable(
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000361 media::audio::common::AudioPortConfig* parcelable, bool isInput) const {
362 media::audio::common::Int aidl_sampleRate;
Mikhail Naganov9255d4d2021-09-23 18:39:38 +0000363 aidl_sampleRate.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate));
364 parcelable->sampleRate = aidl_sampleRate;
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700365 parcelable->format = VALUE_OR_RETURN_STATUS(
366 legacy2aidl_audio_format_t_AudioFormatDescription(mFormat));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800367 parcelable->channelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700368 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mChannelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800369 parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
Mikhail Naganov9255d4d2021-09-23 18:39:38 +0000370 media::audio::common::AudioGainConfig aidl_gain = VALUE_OR_RETURN_STATUS(
371 legacy2aidl_audio_gain_config_AudioGainConfig(mGain, isInput));
372 parcelable->gain = aidl_gain;
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800373 return OK;
374}
375
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700376status_t AudioPortConfig::readFromParcelable(
Mikhail Naganov0078ee52021-09-30 23:06:20 +0000377 const media::audio::common::AudioPortConfig& parcelable, bool isInput) {
Mikhail Naganov9255d4d2021-09-23 18:39:38 +0000378 if (parcelable.sampleRate.has_value()) {
379 mSamplingRate = VALUE_OR_RETURN_STATUS(
380 convertIntegral<unsigned int>(parcelable.sampleRate.value().value));
jiabin17058fa2019-10-08 17:33:38 -0700381 }
Mikhail Naganov9255d4d2021-09-23 18:39:38 +0000382 if (parcelable.format.has_value()) {
383 mFormat = VALUE_OR_RETURN_STATUS(
384 aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format.value()));
385 }
386 if (parcelable.channelMask.has_value()) {
387 mChannelMask = VALUE_OR_RETURN_STATUS(
388 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
389 parcelable.channelMask.value(), isInput));
390 }
391 mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id));
392 if (parcelable.gain.has_value()) {
393 mGain = VALUE_OR_RETURN_STATUS(
394 aidl2legacy_AudioGainConfig_audio_gain_config(parcelable.gain.value(), isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800395 }
396 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700397}
398
399} // namespace android