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