blob: c70a6c2f8b25d5e8730fb4587e3dacf26d30fa79 [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 Naganov9dec7012021-07-21 10:30:57 -0700213 parcelable->profiles = VALUE_OR_RETURN_STATUS(
214 legacy2aidl_AudioProfileVector(mProfiles, useInputChannelMask()));
jiabin82e56932021-03-05 06:35:19 +0000215 parcelable->extraAudioDescriptors = mExtraAudioDescriptors;
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800216 parcelable->gains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains));
217 return OK;
218}
219
220status_t AudioPort::readFromParcel(const Parcel *parcel) {
221 media::AudioPort parcelable;
222 return parcelable.readFromParcel(parcel)
223 ?: readFromParcelable(parcelable);
224}
225
226status_t AudioPort::readFromParcelable(const media::AudioPort& parcelable) {
227 mName = parcelable.name;
228 mType = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortType_audio_port_type_t(parcelable.type));
229 mRole = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.role));
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700230 mProfiles = VALUE_OR_RETURN_STATUS(
231 aidl2legacy_AudioProfileVector(parcelable.profiles, useInputChannelMask()));
jiabin82e56932021-03-05 06:35:19 +0000232 mExtraAudioDescriptors = parcelable.extraAudioDescriptors;
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800233 mGains = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioGains(parcelable.gains));
234 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700235}
236
jiabin4ef93452019-09-10 14:29:54 -0700237// --- AudioPortConfig class implementation
238
239status_t AudioPortConfig::applyAudioPortConfig(
240 const struct audio_port_config *config,
241 struct audio_port_config *backupConfig __unused)
242{
243 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
244 mSamplingRate = config->sample_rate;
245 }
246 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
247 mChannelMask = config->channel_mask;
248 }
249 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
250 mFormat = config->format;
251 }
252 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
253 mGain = config->gain;
254 }
255
256 return NO_ERROR;
257}
258
259namespace {
260
261template<typename T>
262void updateField(
263 const T& portConfigField, T audio_port_config::*port_config_field,
264 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
265 unsigned int configMask, T defaultValue)
266{
267 if (dstConfig->config_mask & configMask) {
268 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
269 dstConfig->*port_config_field = srcConfig->*port_config_field;
270 } else {
271 dstConfig->*port_config_field = portConfigField;
272 }
273 } else {
274 dstConfig->*port_config_field = defaultValue;
275 }
276}
277
278} // namespace
279
280void AudioPortConfig::toAudioPortConfig(
281 struct audio_port_config *dstConfig,
282 const struct audio_port_config *srcConfig) const
283{
284 updateField(mSamplingRate, &audio_port_config::sample_rate,
285 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
286 updateField(mChannelMask, &audio_port_config::channel_mask,
287 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
288 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
289 updateField(mFormat, &audio_port_config::format,
290 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
291 dstConfig->id = mId;
292
293 sp<AudioPort> audioport = getAudioPort();
294 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
295 dstConfig->gain = mGain;
296 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
297 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
298 dstConfig->gain = srcConfig->gain;
299 }
300 } else {
301 dstConfig->gain.index = -1;
302 }
303 if (dstConfig->gain.index != -1) {
304 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
305 } else {
306 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
307 }
308}
309
310bool AudioPortConfig::hasGainController(bool canUseForVolume) const
311{
312 sp<AudioPort> audioport = getAudioPort();
313 if (!audioport) {
314 return false;
315 }
316 return canUseForVolume ? audioport->getGains().canUseForVolume()
317 : audioport->getGains().size() > 0;
318}
319
jiabin49e69a12019-10-15 16:04:13 -0700320bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
321{
322 return other != nullptr &&
323 mSamplingRate == other->getSamplingRate() &&
324 mFormat == other->getFormat() &&
325 mChannelMask == other->getChannelMask() &&
326 // Compare audio gain config
327 mGain.index == other->mGain.index &&
328 mGain.mode == other->mGain.mode &&
329 mGain.channel_mask == other->mGain.channel_mask &&
330 std::equal(std::begin(mGain.values), std::end(mGain.values),
331 std::begin(other->mGain.values)) &&
332 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
333}
334
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700335status_t AudioPortConfig::writeToParcelable(
336 media::AudioPortConfig* parcelable, bool isInput) const {
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800337 parcelable->sampleRate = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate));
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700338 parcelable->format = VALUE_OR_RETURN_STATUS(
339 legacy2aidl_audio_format_t_AudioFormatDescription(mFormat));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800340 parcelable->channelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700341 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mChannelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800342 parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
343 parcelable->gain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.index));
344 parcelable->gain.mode = VALUE_OR_RETURN_STATUS(
Andy Hung973638a2020-12-08 20:47:45 -0800345 legacy2aidl_audio_gain_mode_t_int32_t_mask(mGain.mode));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800346 parcelable->gain.channelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700347 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mGain.channel_mask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800348 parcelable->gain.rampDurationMs = VALUE_OR_RETURN_STATUS(
349 convertIntegral<int32_t>(mGain.ramp_duration_ms));
350 parcelable->gain.values = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<int32_t>>(
351 mGain.values, convertIntegral<int32_t, int>));
352 return OK;
353}
354
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700355status_t AudioPortConfig::readFromParcelable(
356 const media::AudioPortConfig& parcelable, bool isInput) {
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800357 mSamplingRate = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.sampleRate));
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700358 mFormat = VALUE_OR_RETURN_STATUS(
359 aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800360 mChannelMask = VALUE_OR_RETURN_STATUS(
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700361 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(parcelable.channelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800362 mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id));
363 mGain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.index));
364 mGain.mode = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0cf92652020-11-23 13:23:00 -0800365 aidl2legacy_int32_t_audio_gain_mode_t_mask(parcelable.gain.mode));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800366 mGain.channel_mask = VALUE_OR_RETURN_STATUS(
Mikhail Naganov9dec7012021-07-21 10:30:57 -0700367 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
368 parcelable.gain.channelMask, isInput));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800369 mGain.ramp_duration_ms = VALUE_OR_RETURN_STATUS(
370 convertIntegral<unsigned int>(parcelable.gain.rampDurationMs));
371 if (parcelable.gain.values.size() > std::size(mGain.values)) {
jiabin17058fa2019-10-08 17:33:38 -0700372 return BAD_VALUE;
373 }
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800374 for (size_t i = 0; i < parcelable.gain.values.size(); ++i) {
375 mGain.values[i] = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.values[i]));
376 }
377 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700378}
379
380} // namespace android