1 /*
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 */
16 #define LOG_TAG "AudioPort"
17
18 #include <algorithm>
19 #include <utility>
20
21 #include <android-base/stringprintf.h>
22 #include <media/AudioPort.h>
23 #include <utils/Log.h>
24
25 namespace android {
26
setFlags(uint32_t flags)27 void AudioPort::setFlags(uint32_t flags)
28 {
29 // force direct flag if offload flag is set: offloading implies a direct output stream
30 // and all common behaviors are driven by checking only the direct flag
31 // this should normally be set appropriately in the policy configuration file
32 if (mRole == AUDIO_PORT_ROLE_SOURCE &&
33 (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
34 flags |= AUDIO_OUTPUT_FLAG_DIRECT;
35 }
36 if (useInputChannelMask()) {
37 mFlags.input = static_cast<audio_input_flags_t>(flags);
38 } else {
39 mFlags.output = static_cast<audio_output_flags_t>(flags);
40 }
41 }
42
importAudioPort(const sp<AudioPort> & port,bool force __unused)43 void AudioPort::importAudioPort(const sp<AudioPort>& port, bool force __unused)
44 {
45 for (const auto& profileToImport : port->mProfiles) {
46 // Import only valid port, i.e. valid format, non empty rates and channels masks
47 if (!profileToImport->isValid()) {
48 continue;
49 }
50 if (std::find_if(mProfiles.begin(), mProfiles.end(),
51 [profileToImport](const auto &profile) {
52 return *profile == *profileToImport; }) == mProfiles.end()) {
53 addAudioProfile(profileToImport);
54 }
55 }
56 }
57
importAudioPort(const audio_port_v7 & port)58 void AudioPort::importAudioPort(const audio_port_v7 &port) {
59 for (size_t i = 0; i < port.num_audio_profiles; ++i) {
60 sp<AudioProfile> profile = new AudioProfile(port.audio_profiles[i].format,
61 ChannelMaskSet(port.audio_profiles[i].channel_masks,
62 port.audio_profiles[i].channel_masks +
63 port.audio_profiles->num_channel_masks),
64 SampleRateSet(port.audio_profiles[i].sample_rates,
65 port.audio_profiles[i].sample_rates +
66 port.audio_profiles[i].num_sample_rates),
67 port.audio_profiles[i].encapsulation_type);
68 if (!mProfiles.contains(profile)) {
69 addAudioProfile(profile);
70 }
71 }
72
73 for (size_t i = 0; i < port.num_extra_audio_descriptors; ++i) {
74 auto convertedResult = legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(
75 port.extra_audio_descriptors[i]);
76 if (!convertedResult.ok()) {
77 ALOGE("%s, failed to convert extra audio descriptor", __func__);
78 continue;
79 }
80 if (std::find(mExtraAudioDescriptors.begin(),
81 mExtraAudioDescriptors.end(),
82 convertedResult.value()) == mExtraAudioDescriptors.end()) {
83 mExtraAudioDescriptors.push_back(std::move(convertedResult.value()));
84 }
85 }
86 }
87
toAudioPort(struct audio_port * port) const88 void AudioPort::toAudioPort(struct audio_port *port) const {
89 // TODO: update this function once audio_port structure reflects the new profile definition.
90 // For compatibility reason: flatening the AudioProfile into audio_port structure.
91 FormatSet flatenedFormats;
92 SampleRateSet flatenedRates;
93 ChannelMaskSet flatenedChannels;
94 for (const auto& profile : mProfiles) {
95 if (profile->isValid()) {
96 audio_format_t formatToExport = profile->getFormat();
97 const SampleRateSet &ratesToExport = profile->getSampleRates();
98 const ChannelMaskSet &channelsToExport = profile->getChannels();
99
100 flatenedFormats.insert(formatToExport);
101 flatenedRates.insert(ratesToExport.begin(), ratesToExport.end());
102 flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end());
103
104 if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
105 flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
106 flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) {
107 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
108 return;
109 }
110 }
111 }
112 toAudioPortBase(port);
113 port->num_sample_rates = flatenedRates.size();
114 port->num_channel_masks = flatenedChannels.size();
115 port->num_formats = flatenedFormats.size();
116 std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates);
117 std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks);
118 std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats);
119 }
120
toAudioPort(struct audio_port_v7 * port) const121 void AudioPort::toAudioPort(struct audio_port_v7 *port) const {
122 toAudioPortBase(port);
123 port->num_audio_profiles = 0;
124 for (const auto& profile : mProfiles) {
125 if (profile->isValid()) {
126 const SampleRateSet &sampleRates = profile->getSampleRates();
127 const ChannelMaskSet &channelMasks = profile->getChannels();
128
129 if (sampleRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
130 channelMasks.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
131 port->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
132 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
133 break;
134 }
135
136 auto& dstProfile = port->audio_profiles[port->num_audio_profiles++];
137 dstProfile.format = profile->getFormat();
138 dstProfile.num_sample_rates = sampleRates.size();
139 std::copy(sampleRates.begin(), sampleRates.end(),
140 std::begin(dstProfile.sample_rates));
141 dstProfile.num_channel_masks = channelMasks.size();
142 std::copy(channelMasks.begin(), channelMasks.end(),
143 std::begin(dstProfile.channel_masks));
144 dstProfile.encapsulation_type = profile->getEncapsulationType();
145 }
146 }
147
148 port->num_extra_audio_descriptors = 0;
149 for (const auto& desc : mExtraAudioDescriptors) {
150 if (port->num_extra_audio_descriptors >= AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS) {
151 ALOGE("%s: bailing out: cannot export extra audio descriptor to port config", __func__);
152 return;
153 }
154
155 auto convertedResult = aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(desc);
156 if (!convertedResult.ok()) {
157 ALOGE("%s: failed to convert extra audio descriptor", __func__);
158 continue;
159 }
160 port->extra_audio_descriptors[port->num_extra_audio_descriptors++] =
161 std::move(convertedResult.value());
162 }
163 }
164
dump(std::string * dst,int spaces,const char * extraInfo,bool verbose) const165 void AudioPort::dump(std::string *dst, int spaces, const char* extraInfo, bool verbose) const {
166 if (!mName.empty()) {
167 dst->append(base::StringPrintf("\"%s\"%s", mName.c_str(),
168 extraInfo != nullptr ? "; " : ""));
169 }
170 if (extraInfo != nullptr) {
171 dst->append(base::StringPrintf("%s", extraInfo));
172 }
173 if (!mName.empty() || extraInfo != nullptr) {
174 dst->append("\n");
175 }
176 if (verbose) {
177 std::string profilesStr;
178 mProfiles.dump(&profilesStr, spaces);
179 dst->append(profilesStr);
180 if (!mExtraAudioDescriptors.empty()) {
181 dst->append(base::StringPrintf("%*s- extra audio descriptors: \n", spaces, ""));
182 const int eadSpaces = spaces + 4;
183 const int descSpaces = eadSpaces + 4;
184 for (size_t i = 0; i < mExtraAudioDescriptors.size(); i++) {
185 dst->append(
186 base::StringPrintf("%*s extra audio descriptor %zu:\n", eadSpaces, "", i));
187 dst->append(base::StringPrintf(
188 "%*s- standard: %u\n", descSpaces, "", mExtraAudioDescriptors[i].standard));
189 dst->append(base::StringPrintf("%*s- descriptor:", descSpaces, ""));
190 for (auto v : mExtraAudioDescriptors[i].audioDescriptor) {
191 dst->append(base::StringPrintf(" %02x", v));
192 }
193 dst->append("\n");
194 }
195 }
196
197 if (mGains.size() != 0) {
198 dst->append(base::StringPrintf("%*s- gains:\n", spaces, ""));
199 for (size_t i = 0; i < mGains.size(); i++) {
200 std::string gainStr;
201 mGains[i]->dump(&gainStr, spaces + 2, i);
202 dst->append(gainStr);
203 }
204 }
205 }
206 }
207
log(const char * indent) const208 void AudioPort::log(const char* indent) const
209 {
210 ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole);
211 }
212
equals(const sp<AudioPort> & other) const213 bool AudioPort::equals(const sp<AudioPort> &other) const
214 {
215 return other != nullptr &&
216 mGains.equals(other->getGains()) &&
217 mName.compare(other->getName()) == 0 &&
218 mType == other->getType() &&
219 mRole == other->getRole() &&
220 mProfiles.equals(other->getAudioProfiles()) &&
221 getFlags() == other->getFlags() &&
222 mExtraAudioDescriptors == other->getExtraAudioDescriptors();
223 }
224
writeToParcelable(media::AudioPortFw * parcelable) const225 status_t AudioPort::writeToParcelable(media::AudioPortFw* parcelable) const {
226 parcelable->hal.name = mName;
227 parcelable->sys.type = VALUE_OR_RETURN_STATUS(
228 legacy2aidl_audio_port_type_t_AudioPortType(mType));
229 parcelable->sys.role = VALUE_OR_RETURN_STATUS(
230 legacy2aidl_audio_port_role_t_AudioPortRole(mRole));
231 auto aidlProfiles = VALUE_OR_RETURN_STATUS(
232 legacy2aidl_AudioProfileVector(mProfiles, useInputChannelMask()));
233 parcelable->hal.profiles = aidlProfiles.first;
234 parcelable->sys.profiles = aidlProfiles.second;
235 parcelable->hal.flags = VALUE_OR_RETURN_STATUS(
236 legacy2aidl_audio_io_flags_AudioIoFlags(mFlags, useInputChannelMask()));
237 parcelable->hal.extraAudioDescriptors = mExtraAudioDescriptors;
238 auto aidlGains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains));
239 parcelable->hal.gains = aidlGains.first;
240 parcelable->sys.gains = aidlGains.second;
241 if (mType == AUDIO_PORT_TYPE_MIX) {
242 media::audio::common::AudioPortMixExt mixExt{};
243 mixExt.maxOpenStreamCount = maxOpenCount;
244 mixExt.maxActiveStreamCount = maxActiveCount;
245 mixExt.recommendedMuteDurationMs = recommendedMuteDurationMs;
246 parcelable->hal.ext = media::audio::common::AudioPortExt::make<
247 media::audio::common::AudioPortExt::mix>(mixExt);
248 }
249 return OK;
250 }
251
readFromParcelable(const media::AudioPortFw & parcelable)252 status_t AudioPort::readFromParcelable(const media::AudioPortFw& parcelable) {
253 mName = parcelable.hal.name;
254 mType = VALUE_OR_RETURN_STATUS(
255 aidl2legacy_AudioPortType_audio_port_type_t(parcelable.sys.type));
256 mRole = VALUE_OR_RETURN_STATUS(
257 aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.sys.role));
258 mProfiles = VALUE_OR_RETURN_STATUS(
259 aidl2legacy_AudioProfileVector(
260 std::make_pair(parcelable.hal.profiles, parcelable.sys.profiles),
261 useInputChannelMask()));
262 mFlags = VALUE_OR_RETURN_STATUS(
263 aidl2legacy_AudioIoFlags_audio_io_flags(parcelable.hal.flags, useInputChannelMask()));
264 mExtraAudioDescriptors = parcelable.hal.extraAudioDescriptors;
265 mGains = VALUE_OR_RETURN_STATUS(
266 aidl2legacy_AudioGains(std::make_pair(parcelable.hal.gains, parcelable.sys.gains)));
267 if (mType == AUDIO_PORT_TYPE_MIX) {
268 const media::audio::common::AudioPortMixExt& mixExt =
269 parcelable.hal.ext.get<media::audio::common::AudioPortExt::mix>();
270 maxOpenCount = mixExt.maxOpenStreamCount;
271 maxActiveCount = mixExt.maxActiveStreamCount;
272 recommendedMuteDurationMs = mixExt.recommendedMuteDurationMs;
273 }
274 return OK;
275 }
276
277 // --- AudioPortConfig class implementation
278
applyAudioPortConfig(const struct audio_port_config * config,struct audio_port_config * backupConfig __unused)279 status_t AudioPortConfig::applyAudioPortConfig(
280 const struct audio_port_config *config,
281 struct audio_port_config *backupConfig __unused)
282 {
283 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
284 mSamplingRate = config->sample_rate;
285 }
286 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
287 mChannelMask = config->channel_mask;
288 }
289 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
290 mFormat = config->format;
291 }
292 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
293 mGain = config->gain;
294 }
295 if (config->config_mask & AUDIO_PORT_CONFIG_FLAGS) {
296 mFlags = config->flags;
297 }
298
299 return NO_ERROR;
300 }
301
302 namespace {
303
304 template<typename T>
updateField(const T & portConfigField,T audio_port_config::* port_config_field,struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig,unsigned int configMask,T defaultValue)305 void updateField(
306 const T& portConfigField, T audio_port_config::*port_config_field,
307 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
308 unsigned int configMask, T defaultValue)
309 {
310 if (dstConfig->config_mask & configMask) {
311 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
312 dstConfig->*port_config_field = srcConfig->*port_config_field;
313 } else {
314 dstConfig->*port_config_field = portConfigField;
315 }
316 } else {
317 dstConfig->*port_config_field = defaultValue;
318 }
319 }
320
321 } // namespace
322
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const323 void AudioPortConfig::toAudioPortConfig(
324 struct audio_port_config *dstConfig,
325 const struct audio_port_config *srcConfig) const
326 {
327 updateField(mSamplingRate, &audio_port_config::sample_rate,
328 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
329 updateField(mChannelMask, &audio_port_config::channel_mask,
330 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
331 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
332 updateField(mFormat, &audio_port_config::format,
333 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
334 dstConfig->id = mId;
335
336 sp<AudioPort> audioport = getAudioPort();
337 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
338 dstConfig->gain = mGain;
339 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
340 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
341 dstConfig->gain = srcConfig->gain;
342 }
343 } else {
344 dstConfig->gain.index = -1;
345 }
346 if (dstConfig->gain.index != -1) {
347 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
348 } else {
349 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
350 }
351
352 updateField(mFlags, &audio_port_config::flags,
353 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FLAGS, { AUDIO_INPUT_FLAG_NONE });
354 }
355
hasGainController(bool canUseForVolume) const356 bool AudioPortConfig::hasGainController(bool canUseForVolume) const
357 {
358 sp<AudioPort> audioport = getAudioPort();
359 if (!audioport) {
360 return false;
361 }
362 return canUseForVolume ? audioport->getGains().canUseForVolume()
363 : audioport->getGains().size() > 0;
364 }
365
equals(const sp<AudioPortConfig> & other,bool isInput) const366 bool AudioPortConfig::equals(const sp<AudioPortConfig> &other, bool isInput) const
367 {
368 return other != nullptr &&
369 mSamplingRate == other->getSamplingRate() &&
370 mFormat == other->getFormat() &&
371 mChannelMask == other->getChannelMask() &&
372 (isInput ? mFlags.input == other->getFlags().input :
373 mFlags.output == other->getFlags().output )&&
374 // Compare audio gain config
375 mGain.index == other->mGain.index &&
376 mGain.mode == other->mGain.mode &&
377 mGain.channel_mask == other->mGain.channel_mask &&
378 std::equal(std::begin(mGain.values), std::end(mGain.values),
379 std::begin(other->mGain.values)) &&
380 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
381 }
382
writeToParcelable(media::audio::common::AudioPortConfig * parcelable,bool isInput) const383 status_t AudioPortConfig::writeToParcelable(
384 media::audio::common::AudioPortConfig* parcelable, bool isInput) const {
385 media::audio::common::Int aidl_sampleRate;
386 aidl_sampleRate.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate));
387 parcelable->sampleRate = aidl_sampleRate;
388 parcelable->format = VALUE_OR_RETURN_STATUS(
389 legacy2aidl_audio_format_t_AudioFormatDescription(mFormat));
390 parcelable->channelMask = VALUE_OR_RETURN_STATUS(
391 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mChannelMask, isInput));
392 parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
393 media::audio::common::AudioGainConfig aidl_gain = VALUE_OR_RETURN_STATUS(
394 legacy2aidl_audio_gain_config_AudioGainConfig(mGain, isInput));
395 parcelable->gain = aidl_gain;
396 parcelable->flags = VALUE_OR_RETURN_STATUS(
397 legacy2aidl_audio_io_flags_AudioIoFlags(mFlags, isInput));
398 return OK;
399 }
400
readFromParcelable(const media::audio::common::AudioPortConfig & parcelable,bool isInput)401 status_t AudioPortConfig::readFromParcelable(
402 const media::audio::common::AudioPortConfig& parcelable, bool isInput) {
403 if (parcelable.sampleRate.has_value()) {
404 mSamplingRate = VALUE_OR_RETURN_STATUS(
405 convertIntegral<unsigned int>(parcelable.sampleRate.value().value));
406 }
407 if (parcelable.format.has_value()) {
408 mFormat = VALUE_OR_RETURN_STATUS(
409 aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format.value()));
410 }
411 if (parcelable.channelMask.has_value()) {
412 mChannelMask = VALUE_OR_RETURN_STATUS(
413 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
414 parcelable.channelMask.value(), isInput));
415 }
416 mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id));
417 if (parcelable.gain.has_value()) {
418 mGain = VALUE_OR_RETURN_STATUS(
419 aidl2legacy_AudioGainConfig_audio_gain_config(parcelable.gain.value(), isInput));
420 }
421 if (parcelable.flags.has_value()) {
422 mFlags = VALUE_OR_RETURN_STATUS(
423 aidl2legacy_AudioIoFlags_audio_io_flags(parcelable.flags.value(), isInput));
424 }
425 return OK;
426 }
427
428 } // namespace android
429