jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 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 | */ |
jiabin | e128485 | 2019-09-11 10:15:46 -0700 | [diff] [blame] | 16 | #define LOG_TAG "AudioPort" |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 17 | |
| 18 | #include <algorithm> |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 19 | #include <utility> |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 20 | |
| 21 | #include <android-base/stringprintf.h> |
jiabin | e128485 | 2019-09-11 10:15:46 -0700 | [diff] [blame] | 22 | #include <media/AudioPort.h> |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 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 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 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 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 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 + |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 66 | port.audio_profiles[i].num_sample_rates), |
| 67 | port.audio_profiles[i].encapsulation_type); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 68 | if (!mProfiles.contains(profile)) { |
| 69 | addAudioProfile(profile); |
| 70 | } |
| 71 | } |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 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 | } |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 86 | } |
| 87 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 88 | void AudioPort::toAudioPort(struct audio_port *port) const { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 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; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 94 | for (const auto& profile : mProfiles) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 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 | } |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 112 | toAudioPortBase(port); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 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); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 119 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 120 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 121 | 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(); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 128 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 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__); |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 133 | break; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 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)); |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 144 | dstProfile.encapsulation_type = profile->getEncapsulationType(); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 145 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 146 | } |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 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 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 163 | } |
| 164 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 165 | void AudioPort::dump(std::string *dst, int spaces, bool verbose) const { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 166 | if (!mName.empty()) { |
| 167 | dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str())); |
| 168 | } |
| 169 | if (verbose) { |
| 170 | std::string profilesStr; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 171 | mProfiles.dump(&profilesStr, spaces); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 172 | dst->append(profilesStr); |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 173 | if (!mExtraAudioDescriptors.empty()) { |
| 174 | dst->append(base::StringPrintf("%*s- extra audio descriptors: \n", spaces, "")); |
| 175 | const int eadSpaces = spaces + 4; |
| 176 | const int descSpaces = eadSpaces + 4; |
| 177 | for (size_t i = 0; i < mExtraAudioDescriptors.size(); i++) { |
| 178 | dst->append( |
| 179 | base::StringPrintf("%*s extra audio descriptor %zu:\n", eadSpaces, "", i)); |
| 180 | dst->append(base::StringPrintf( |
| 181 | "%*s- standard: %u\n", descSpaces, "", mExtraAudioDescriptors[i].standard)); |
| 182 | dst->append(base::StringPrintf("%*s- descriptor:", descSpaces, "")); |
| 183 | for (auto v : mExtraAudioDescriptors[i].audioDescriptor) { |
| 184 | dst->append(base::StringPrintf(" %02x", v)); |
| 185 | } |
| 186 | dst->append("\n"); |
| 187 | } |
| 188 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 189 | |
| 190 | if (mGains.size() != 0) { |
| 191 | dst->append(base::StringPrintf("%*s- gains:\n", spaces, "")); |
| 192 | for (size_t i = 0; i < mGains.size(); i++) { |
| 193 | std::string gainStr; |
| 194 | mGains[i]->dump(&gainStr, spaces + 2, i); |
| 195 | dst->append(gainStr); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 201 | void AudioPort::log(const char* indent) const |
| 202 | { |
| 203 | ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole); |
| 204 | } |
| 205 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 206 | bool AudioPort::equals(const sp<AudioPort> &other) const |
| 207 | { |
| 208 | return other != nullptr && |
| 209 | mGains.equals(other->getGains()) && |
| 210 | mName.compare(other->getName()) == 0 && |
| 211 | mType == other->getType() && |
| 212 | mRole == other->getRole() && |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 213 | mProfiles.equals(other->getAudioProfiles()) && |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 214 | getFlags() == other->getFlags() && |
jiabin | 82e5693 | 2021-03-05 06:35:19 +0000 | [diff] [blame] | 215 | mExtraAudioDescriptors == other->getExtraAudioDescriptors(); |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 218 | status_t AudioPort::writeToParcelable(media::AudioPort* parcelable) const { |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 219 | parcelable->hal.name = mName; |
| 220 | parcelable->sys.type = VALUE_OR_RETURN_STATUS( |
| 221 | legacy2aidl_audio_port_type_t_AudioPortType(mType)); |
| 222 | parcelable->sys.role = VALUE_OR_RETURN_STATUS( |
| 223 | legacy2aidl_audio_port_role_t_AudioPortRole(mRole)); |
Mikhail Naganov | 7d0b36b | 2021-09-22 23:58:41 +0000 | [diff] [blame] | 224 | auto aidlProfiles = VALUE_OR_RETURN_STATUS( |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 225 | legacy2aidl_AudioProfileVector(mProfiles, useInputChannelMask())); |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 226 | parcelable->hal.profiles = aidlProfiles.first; |
| 227 | parcelable->sys.profiles = aidlProfiles.second; |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 228 | parcelable->hal.flags = VALUE_OR_RETURN_STATUS( |
| 229 | legacy2aidl_audio_io_flags_AudioIoFlags(mFlags, useInputChannelMask())); |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 230 | parcelable->hal.extraAudioDescriptors = mExtraAudioDescriptors; |
Mikhail Naganov | 7d0b36b | 2021-09-22 23:58:41 +0000 | [diff] [blame] | 231 | auto aidlGains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains)); |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 232 | parcelable->hal.gains = aidlGains.first; |
| 233 | parcelable->sys.gains = aidlGains.second; |
Mikhail Naganov | 1050612 | 2021-10-19 10:14:23 -0700 | [diff] [blame] | 234 | if (mType == AUDIO_PORT_TYPE_MIX) { |
| 235 | media::audio::common::AudioPortMixExt mixExt{}; |
| 236 | mixExt.maxOpenStreamCount = maxOpenCount; |
| 237 | mixExt.maxActiveStreamCount = maxActiveCount; |
| 238 | mixExt.recommendedMuteDurationMs = recommendedMuteDurationMs; |
| 239 | parcelable->hal.ext = media::audio::common::AudioPortExt::make< |
| 240 | media::audio::common::AudioPortExt::mix>(mixExt); |
| 241 | } |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 242 | return OK; |
| 243 | } |
| 244 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 245 | status_t AudioPort::readFromParcelable(const media::AudioPort& parcelable) { |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 246 | mName = parcelable.hal.name; |
| 247 | mType = VALUE_OR_RETURN_STATUS( |
| 248 | aidl2legacy_AudioPortType_audio_port_type_t(parcelable.sys.type)); |
| 249 | mRole = VALUE_OR_RETURN_STATUS( |
| 250 | aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.sys.role)); |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 251 | mProfiles = VALUE_OR_RETURN_STATUS( |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 252 | aidl2legacy_AudioProfileVector( |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 253 | std::make_pair(parcelable.hal.profiles, parcelable.sys.profiles), |
Mikhail Naganov | 89818ba | 2021-09-21 20:37:13 +0000 | [diff] [blame] | 254 | useInputChannelMask())); |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 255 | mFlags = VALUE_OR_RETURN_STATUS( |
| 256 | aidl2legacy_AudioIoFlags_audio_io_flags(parcelable.hal.flags, useInputChannelMask())); |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 257 | mExtraAudioDescriptors = parcelable.hal.extraAudioDescriptors; |
Mikhail Naganov | 7d0b36b | 2021-09-22 23:58:41 +0000 | [diff] [blame] | 258 | mGains = VALUE_OR_RETURN_STATUS( |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 259 | aidl2legacy_AudioGains(std::make_pair(parcelable.hal.gains, parcelable.sys.gains))); |
Mikhail Naganov | 1050612 | 2021-10-19 10:14:23 -0700 | [diff] [blame] | 260 | if (mType == AUDIO_PORT_TYPE_MIX) { |
| 261 | const media::audio::common::AudioPortMixExt& mixExt = |
| 262 | parcelable.hal.ext.get<media::audio::common::AudioPortExt::mix>(); |
| 263 | maxOpenCount = mixExt.maxOpenStreamCount; |
| 264 | maxActiveCount = mixExt.maxActiveStreamCount; |
| 265 | recommendedMuteDurationMs = mixExt.recommendedMuteDurationMs; |
| 266 | } |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 267 | return OK; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 268 | } |
| 269 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 270 | // --- AudioPortConfig class implementation |
| 271 | |
| 272 | status_t AudioPortConfig::applyAudioPortConfig( |
| 273 | const struct audio_port_config *config, |
| 274 | struct audio_port_config *backupConfig __unused) |
| 275 | { |
| 276 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 277 | mSamplingRate = config->sample_rate; |
| 278 | } |
| 279 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 280 | mChannelMask = config->channel_mask; |
| 281 | } |
| 282 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 283 | mFormat = config->format; |
| 284 | } |
| 285 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
| 286 | mGain = config->gain; |
| 287 | } |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 288 | if (config->config_mask & AUDIO_PORT_CONFIG_FLAGS) { |
| 289 | mFlags = config->flags; |
| 290 | } |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 291 | |
| 292 | return NO_ERROR; |
| 293 | } |
| 294 | |
| 295 | namespace { |
| 296 | |
| 297 | template<typename T> |
| 298 | void updateField( |
| 299 | const T& portConfigField, T audio_port_config::*port_config_field, |
| 300 | struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig, |
| 301 | unsigned int configMask, T defaultValue) |
| 302 | { |
| 303 | if (dstConfig->config_mask & configMask) { |
| 304 | if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) { |
| 305 | dstConfig->*port_config_field = srcConfig->*port_config_field; |
| 306 | } else { |
| 307 | dstConfig->*port_config_field = portConfigField; |
| 308 | } |
| 309 | } else { |
| 310 | dstConfig->*port_config_field = defaultValue; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | } // namespace |
| 315 | |
| 316 | void AudioPortConfig::toAudioPortConfig( |
| 317 | struct audio_port_config *dstConfig, |
| 318 | const struct audio_port_config *srcConfig) const |
| 319 | { |
| 320 | updateField(mSamplingRate, &audio_port_config::sample_rate, |
| 321 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u); |
| 322 | updateField(mChannelMask, &audio_port_config::channel_mask, |
| 323 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK, |
| 324 | (audio_channel_mask_t)AUDIO_CHANNEL_NONE); |
| 325 | updateField(mFormat, &audio_port_config::format, |
| 326 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID); |
| 327 | dstConfig->id = mId; |
| 328 | |
| 329 | sp<AudioPort> audioport = getAudioPort(); |
| 330 | if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) { |
| 331 | dstConfig->gain = mGain; |
| 332 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) |
| 333 | && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) { |
| 334 | dstConfig->gain = srcConfig->gain; |
| 335 | } |
| 336 | } else { |
| 337 | dstConfig->gain.index = -1; |
| 338 | } |
| 339 | if (dstConfig->gain.index != -1) { |
| 340 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; |
| 341 | } else { |
| 342 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; |
| 343 | } |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 344 | |
| 345 | updateField(mFlags, &audio_port_config::flags, |
| 346 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_FLAGS, { AUDIO_INPUT_FLAG_NONE }); |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | bool AudioPortConfig::hasGainController(bool canUseForVolume) const |
| 350 | { |
| 351 | sp<AudioPort> audioport = getAudioPort(); |
| 352 | if (!audioport) { |
| 353 | return false; |
| 354 | } |
| 355 | return canUseForVolume ? audioport->getGains().canUseForVolume() |
| 356 | : audioport->getGains().size() > 0; |
| 357 | } |
| 358 | |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 359 | bool AudioPortConfig::equals(const sp<AudioPortConfig> &other, bool isInput) const |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 360 | { |
| 361 | return other != nullptr && |
| 362 | mSamplingRate == other->getSamplingRate() && |
| 363 | mFormat == other->getFormat() && |
| 364 | mChannelMask == other->getChannelMask() && |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 365 | (isInput ? mFlags.input == other->getFlags().input : |
| 366 | mFlags.output == other->getFlags().output )&& |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 367 | // Compare audio gain config |
| 368 | mGain.index == other->mGain.index && |
| 369 | mGain.mode == other->mGain.mode && |
| 370 | mGain.channel_mask == other->mGain.channel_mask && |
| 371 | std::equal(std::begin(mGain.values), std::end(mGain.values), |
| 372 | std::begin(other->mGain.values)) && |
| 373 | mGain.ramp_duration_ms == other->mGain.ramp_duration_ms; |
| 374 | } |
| 375 | |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 376 | status_t AudioPortConfig::writeToParcelable( |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 377 | media::audio::common::AudioPortConfig* parcelable, bool isInput) const { |
| 378 | media::audio::common::Int aidl_sampleRate; |
Mikhail Naganov | 9255d4d | 2021-09-23 18:39:38 +0000 | [diff] [blame] | 379 | aidl_sampleRate.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate)); |
| 380 | parcelable->sampleRate = aidl_sampleRate; |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 381 | parcelable->format = VALUE_OR_RETURN_STATUS( |
| 382 | legacy2aidl_audio_format_t_AudioFormatDescription(mFormat)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 383 | parcelable->channelMask = VALUE_OR_RETURN_STATUS( |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 384 | legacy2aidl_audio_channel_mask_t_AudioChannelLayout(mChannelMask, isInput)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 385 | parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId)); |
Mikhail Naganov | 9255d4d | 2021-09-23 18:39:38 +0000 | [diff] [blame] | 386 | media::audio::common::AudioGainConfig aidl_gain = VALUE_OR_RETURN_STATUS( |
| 387 | legacy2aidl_audio_gain_config_AudioGainConfig(mGain, isInput)); |
| 388 | parcelable->gain = aidl_gain; |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 389 | parcelable->flags = VALUE_OR_RETURN_STATUS( |
| 390 | legacy2aidl_audio_io_flags_AudioIoFlags(mFlags, isInput)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 391 | return OK; |
| 392 | } |
| 393 | |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 394 | status_t AudioPortConfig::readFromParcelable( |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 395 | const media::audio::common::AudioPortConfig& parcelable, bool isInput) { |
Mikhail Naganov | 9255d4d | 2021-09-23 18:39:38 +0000 | [diff] [blame] | 396 | if (parcelable.sampleRate.has_value()) { |
| 397 | mSamplingRate = VALUE_OR_RETURN_STATUS( |
| 398 | convertIntegral<unsigned int>(parcelable.sampleRate.value().value)); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 399 | } |
Mikhail Naganov | 9255d4d | 2021-09-23 18:39:38 +0000 | [diff] [blame] | 400 | if (parcelable.format.has_value()) { |
| 401 | mFormat = VALUE_OR_RETURN_STATUS( |
| 402 | aidl2legacy_AudioFormatDescription_audio_format_t(parcelable.format.value())); |
| 403 | } |
| 404 | if (parcelable.channelMask.has_value()) { |
| 405 | mChannelMask = VALUE_OR_RETURN_STATUS( |
| 406 | aidl2legacy_AudioChannelLayout_audio_channel_mask_t( |
| 407 | parcelable.channelMask.value(), isInput)); |
| 408 | } |
| 409 | mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id)); |
| 410 | if (parcelable.gain.has_value()) { |
| 411 | mGain = VALUE_OR_RETURN_STATUS( |
| 412 | aidl2legacy_AudioGainConfig_audio_gain_config(parcelable.gain.value(), isInput)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 413 | } |
Mikhail Naganov | 9980902 | 2021-11-04 00:00:29 +0000 | [diff] [blame^] | 414 | if (parcelable.flags.has_value()) { |
| 415 | mFlags = VALUE_OR_RETURN_STATUS( |
| 416 | aidl2legacy_AudioIoFlags_audio_io_flags(parcelable.flags.value(), isInput)); |
| 417 | } |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 418 | return OK; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | } // namespace android |