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> |
| 19 | |
| 20 | #include <android-base/stringprintf.h> |
jiabin | e128485 | 2019-09-11 10:15:46 -0700 | [diff] [blame] | 21 | #include <media/AudioPort.h> |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 26 | void AudioPort::importAudioPort(const sp<AudioPort>& port, bool force __unused) |
| 27 | { |
| 28 | for (const auto& profileToImport : port->mProfiles) { |
| 29 | // Import only valid port, i.e. valid format, non empty rates and channels masks |
| 30 | if (!profileToImport->isValid()) { |
| 31 | continue; |
| 32 | } |
| 33 | if (std::find_if(mProfiles.begin(), mProfiles.end(), |
| 34 | [profileToImport](const auto &profile) { |
| 35 | return *profile == *profileToImport; }) == mProfiles.end()) { |
| 36 | addAudioProfile(profileToImport); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 41 | void AudioPort::importAudioPort(const audio_port_v7 &port) { |
| 42 | for (size_t i = 0; i < port.num_audio_profiles; ++i) { |
| 43 | sp<AudioProfile> profile = new AudioProfile(port.audio_profiles[i].format, |
| 44 | ChannelMaskSet(port.audio_profiles[i].channel_masks, |
| 45 | port.audio_profiles[i].channel_masks + |
| 46 | port.audio_profiles->num_channel_masks), |
| 47 | SampleRateSet(port.audio_profiles[i].sample_rates, |
| 48 | port.audio_profiles[i].sample_rates + |
| 49 | port.audio_profiles[i].num_sample_rates)); |
| 50 | if (!mProfiles.contains(profile)) { |
| 51 | addAudioProfile(profile); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 56 | void AudioPort::toAudioPort(struct audio_port *port) const { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 57 | // TODO: update this function once audio_port structure reflects the new profile definition. |
| 58 | // For compatibility reason: flatening the AudioProfile into audio_port structure. |
| 59 | FormatSet flatenedFormats; |
| 60 | SampleRateSet flatenedRates; |
| 61 | ChannelMaskSet flatenedChannels; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 62 | for (const auto& profile : mProfiles) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 63 | if (profile->isValid()) { |
| 64 | audio_format_t formatToExport = profile->getFormat(); |
| 65 | const SampleRateSet &ratesToExport = profile->getSampleRates(); |
| 66 | const ChannelMaskSet &channelsToExport = profile->getChannels(); |
| 67 | |
| 68 | flatenedFormats.insert(formatToExport); |
| 69 | flatenedRates.insert(ratesToExport.begin(), ratesToExport.end()); |
| 70 | flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end()); |
| 71 | |
| 72 | if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES || |
| 73 | flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS || |
| 74 | flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) { |
| 75 | ALOGE("%s: bailing out: cannot export profiles to port config", __func__); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | } |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 80 | toAudioPortBase(port); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 81 | port->num_sample_rates = flatenedRates.size(); |
| 82 | port->num_channel_masks = flatenedChannels.size(); |
| 83 | port->num_formats = flatenedFormats.size(); |
| 84 | std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates); |
| 85 | std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks); |
| 86 | std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 87 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 88 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 89 | void AudioPort::toAudioPort(struct audio_port_v7 *port) const { |
| 90 | toAudioPortBase(port); |
| 91 | port->num_audio_profiles = 0; |
| 92 | for (const auto& profile : mProfiles) { |
| 93 | if (profile->isValid()) { |
| 94 | const SampleRateSet &sampleRates = profile->getSampleRates(); |
| 95 | const ChannelMaskSet &channelMasks = profile->getChannels(); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 96 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 97 | if (sampleRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES || |
| 98 | channelMasks.size() > AUDIO_PORT_MAX_CHANNEL_MASKS || |
| 99 | port->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) { |
| 100 | ALOGE("%s: bailing out: cannot export profiles to port config", __func__); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | auto& dstProfile = port->audio_profiles[port->num_audio_profiles++]; |
| 105 | dstProfile.format = profile->getFormat(); |
| 106 | dstProfile.num_sample_rates = sampleRates.size(); |
| 107 | std::copy(sampleRates.begin(), sampleRates.end(), |
| 108 | std::begin(dstProfile.sample_rates)); |
| 109 | dstProfile.num_channel_masks = channelMasks.size(); |
| 110 | std::copy(channelMasks.begin(), channelMasks.end(), |
| 111 | std::begin(dstProfile.channel_masks)); |
| 112 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 116 | void AudioPort::dump(std::string *dst, int spaces, bool verbose) const { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 117 | if (!mName.empty()) { |
| 118 | dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str())); |
| 119 | } |
| 120 | if (verbose) { |
| 121 | std::string profilesStr; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 122 | mProfiles.dump(&profilesStr, spaces); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 123 | dst->append(profilesStr); |
| 124 | |
| 125 | if (mGains.size() != 0) { |
| 126 | dst->append(base::StringPrintf("%*s- gains:\n", spaces, "")); |
| 127 | for (size_t i = 0; i < mGains.size(); i++) { |
| 128 | std::string gainStr; |
| 129 | mGains[i]->dump(&gainStr, spaces + 2, i); |
| 130 | dst->append(gainStr); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 136 | void AudioPort::log(const char* indent) const |
| 137 | { |
| 138 | ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole); |
| 139 | } |
| 140 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 141 | bool AudioPort::equals(const sp<AudioPort> &other) const |
| 142 | { |
| 143 | return other != nullptr && |
| 144 | mGains.equals(other->getGains()) && |
| 145 | mName.compare(other->getName()) == 0 && |
| 146 | mType == other->getType() && |
| 147 | mRole == other->getRole() && |
| 148 | mProfiles.equals(other->getAudioProfiles()); |
| 149 | } |
| 150 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 151 | status_t AudioPort::writeToParcel(Parcel *parcel) const |
| 152 | { |
| 153 | status_t status = NO_ERROR; |
| 154 | if ((status = parcel->writeUtf8AsUtf16(mName)) != NO_ERROR) return status; |
| 155 | if ((status = parcel->writeUint32(mType)) != NO_ERROR) return status; |
| 156 | if ((status = parcel->writeUint32(mRole)) != NO_ERROR) return status; |
| 157 | if ((status = parcel->writeParcelable(mProfiles)) != NO_ERROR) return status; |
| 158 | if ((status = parcel->writeParcelable(mGains)) != NO_ERROR) return status; |
| 159 | return status; |
| 160 | } |
| 161 | |
| 162 | status_t AudioPort::readFromParcel(const Parcel *parcel) |
| 163 | { |
| 164 | status_t status = NO_ERROR; |
| 165 | if ((status = parcel->readUtf8FromUtf16(&mName)) != NO_ERROR) return status; |
| 166 | static_assert(sizeof(mType) == sizeof(uint32_t)); |
| 167 | if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mType))) != NO_ERROR) { |
| 168 | return status; |
| 169 | } |
| 170 | static_assert(sizeof(mRole) == sizeof(uint32_t)); |
| 171 | if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mRole))) != NO_ERROR) { |
| 172 | return status; |
| 173 | } |
| 174 | mProfiles.clear(); |
| 175 | if ((status = parcel->readParcelable(&mProfiles)) != NO_ERROR) return status; |
| 176 | mGains.clear(); |
| 177 | if ((status = parcel->readParcelable(&mGains)) != NO_ERROR) return status; |
| 178 | return status; |
| 179 | } |
| 180 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 181 | // --- AudioPortConfig class implementation |
| 182 | |
| 183 | status_t AudioPortConfig::applyAudioPortConfig( |
| 184 | const struct audio_port_config *config, |
| 185 | struct audio_port_config *backupConfig __unused) |
| 186 | { |
| 187 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 188 | mSamplingRate = config->sample_rate; |
| 189 | } |
| 190 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 191 | mChannelMask = config->channel_mask; |
| 192 | } |
| 193 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 194 | mFormat = config->format; |
| 195 | } |
| 196 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
| 197 | mGain = config->gain; |
| 198 | } |
| 199 | |
| 200 | return NO_ERROR; |
| 201 | } |
| 202 | |
| 203 | namespace { |
| 204 | |
| 205 | template<typename T> |
| 206 | void updateField( |
| 207 | const T& portConfigField, T audio_port_config::*port_config_field, |
| 208 | struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig, |
| 209 | unsigned int configMask, T defaultValue) |
| 210 | { |
| 211 | if (dstConfig->config_mask & configMask) { |
| 212 | if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) { |
| 213 | dstConfig->*port_config_field = srcConfig->*port_config_field; |
| 214 | } else { |
| 215 | dstConfig->*port_config_field = portConfigField; |
| 216 | } |
| 217 | } else { |
| 218 | dstConfig->*port_config_field = defaultValue; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | } // namespace |
| 223 | |
| 224 | void AudioPortConfig::toAudioPortConfig( |
| 225 | struct audio_port_config *dstConfig, |
| 226 | const struct audio_port_config *srcConfig) const |
| 227 | { |
| 228 | updateField(mSamplingRate, &audio_port_config::sample_rate, |
| 229 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u); |
| 230 | updateField(mChannelMask, &audio_port_config::channel_mask, |
| 231 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK, |
| 232 | (audio_channel_mask_t)AUDIO_CHANNEL_NONE); |
| 233 | updateField(mFormat, &audio_port_config::format, |
| 234 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID); |
| 235 | dstConfig->id = mId; |
| 236 | |
| 237 | sp<AudioPort> audioport = getAudioPort(); |
| 238 | if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) { |
| 239 | dstConfig->gain = mGain; |
| 240 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) |
| 241 | && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) { |
| 242 | dstConfig->gain = srcConfig->gain; |
| 243 | } |
| 244 | } else { |
| 245 | dstConfig->gain.index = -1; |
| 246 | } |
| 247 | if (dstConfig->gain.index != -1) { |
| 248 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; |
| 249 | } else { |
| 250 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | bool AudioPortConfig::hasGainController(bool canUseForVolume) const |
| 255 | { |
| 256 | sp<AudioPort> audioport = getAudioPort(); |
| 257 | if (!audioport) { |
| 258 | return false; |
| 259 | } |
| 260 | return canUseForVolume ? audioport->getGains().canUseForVolume() |
| 261 | : audioport->getGains().size() > 0; |
| 262 | } |
| 263 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 264 | bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const |
| 265 | { |
| 266 | return other != nullptr && |
| 267 | mSamplingRate == other->getSamplingRate() && |
| 268 | mFormat == other->getFormat() && |
| 269 | mChannelMask == other->getChannelMask() && |
| 270 | // Compare audio gain config |
| 271 | mGain.index == other->mGain.index && |
| 272 | mGain.mode == other->mGain.mode && |
| 273 | mGain.channel_mask == other->mGain.channel_mask && |
| 274 | std::equal(std::begin(mGain.values), std::end(mGain.values), |
| 275 | std::begin(other->mGain.values)) && |
| 276 | mGain.ramp_duration_ms == other->mGain.ramp_duration_ms; |
| 277 | } |
| 278 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 279 | status_t AudioPortConfig::writeToParcel(Parcel *parcel) const |
| 280 | { |
| 281 | status_t status = NO_ERROR; |
| 282 | if ((status = parcel->writeUint32(mSamplingRate)) != NO_ERROR) return status; |
| 283 | if ((status = parcel->writeUint32(mFormat)) != NO_ERROR) return status; |
| 284 | if ((status = parcel->writeUint32(mChannelMask)) != NO_ERROR) return status; |
| 285 | if ((status = parcel->writeInt32(mId)) != NO_ERROR) return status; |
| 286 | // Write mGain to parcel. |
| 287 | if ((status = parcel->writeInt32(mGain.index)) != NO_ERROR) return status; |
| 288 | if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status; |
| 289 | if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status; |
| 290 | if ((status = parcel->writeUint32(mGain.ramp_duration_ms)) != NO_ERROR) return status; |
| 291 | std::vector<int> values(std::begin(mGain.values), std::end(mGain.values)); |
| 292 | if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status; |
| 293 | return status; |
jiabin | e128485 | 2019-09-11 10:15:46 -0700 | [diff] [blame] | 294 | } |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 295 | |
| 296 | status_t AudioPortConfig::readFromParcel(const Parcel *parcel) |
| 297 | { |
| 298 | status_t status = NO_ERROR; |
| 299 | if ((status = parcel->readUint32(&mSamplingRate)) != NO_ERROR) return status; |
| 300 | static_assert(sizeof(mFormat) == sizeof(uint32_t)); |
| 301 | if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mFormat))) != NO_ERROR) { |
| 302 | return status; |
| 303 | } |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 304 | uint32_t rawChannelMask; |
| 305 | if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status; |
| 306 | mChannelMask = static_cast<audio_channel_mask_t>(rawChannelMask); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 307 | if ((status = parcel->readInt32(&mId)) != NO_ERROR) return status; |
| 308 | // Read mGain from parcel. |
| 309 | if ((status = parcel->readInt32(&mGain.index)) != NO_ERROR) return status; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 310 | uint32_t rawGainMode; |
| 311 | if ((status = parcel->readUint32(&rawGainMode)) != NO_ERROR) return status; |
| 312 | mGain.mode = static_cast<audio_gain_mode_t>(rawGainMode); |
| 313 | if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status; |
| 314 | mGain.channel_mask = static_cast<audio_channel_mask_t>(rawChannelMask); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 315 | if ((status = parcel->readUint32(&mGain.ramp_duration_ms)) != NO_ERROR) return status; |
| 316 | std::vector<int> values; |
| 317 | if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status; |
| 318 | if (values.size() != std::size(mGain.values)) { |
| 319 | return BAD_VALUE; |
| 320 | } |
| 321 | std::copy(values.begin(), values.end(), mGain.values); |
| 322 | return status; |
| 323 | } |
| 324 | |
| 325 | } // namespace android |