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