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 | { |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 153 | media::AudioPort parcelable; |
| 154 | return writeToParcelable(&parcelable) |
| 155 | ?: parcelable.writeToParcel(parcel); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 158 | status_t AudioPort::writeToParcelable(media::AudioPort* parcelable) const { |
| 159 | parcelable->name = mName; |
| 160 | parcelable->type = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_type_t_AudioPortType(mType)); |
| 161 | parcelable->role = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_role_t_AudioPortRole(mRole)); |
| 162 | parcelable->profiles = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioProfileVector(mProfiles)); |
| 163 | parcelable->gains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains)); |
| 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | status_t AudioPort::readFromParcel(const Parcel *parcel) { |
| 168 | media::AudioPort parcelable; |
| 169 | return parcelable.readFromParcel(parcel) |
| 170 | ?: readFromParcelable(parcelable); |
| 171 | } |
| 172 | |
| 173 | status_t AudioPort::readFromParcelable(const media::AudioPort& parcelable) { |
| 174 | mName = parcelable.name; |
| 175 | mType = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortType_audio_port_type_t(parcelable.type)); |
| 176 | mRole = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.role)); |
| 177 | mProfiles = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioProfileVector(parcelable.profiles)); |
| 178 | mGains = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioGains(parcelable.gains)); |
| 179 | return OK; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 180 | } |
| 181 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 182 | // --- AudioPortConfig class implementation |
| 183 | |
| 184 | status_t AudioPortConfig::applyAudioPortConfig( |
| 185 | const struct audio_port_config *config, |
| 186 | struct audio_port_config *backupConfig __unused) |
| 187 | { |
| 188 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 189 | mSamplingRate = config->sample_rate; |
| 190 | } |
| 191 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 192 | mChannelMask = config->channel_mask; |
| 193 | } |
| 194 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 195 | mFormat = config->format; |
| 196 | } |
| 197 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
| 198 | mGain = config->gain; |
| 199 | } |
| 200 | |
| 201 | return NO_ERROR; |
| 202 | } |
| 203 | |
| 204 | namespace { |
| 205 | |
| 206 | template<typename T> |
| 207 | void updateField( |
| 208 | const T& portConfigField, T audio_port_config::*port_config_field, |
| 209 | struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig, |
| 210 | unsigned int configMask, T defaultValue) |
| 211 | { |
| 212 | if (dstConfig->config_mask & configMask) { |
| 213 | if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) { |
| 214 | dstConfig->*port_config_field = srcConfig->*port_config_field; |
| 215 | } else { |
| 216 | dstConfig->*port_config_field = portConfigField; |
| 217 | } |
| 218 | } else { |
| 219 | dstConfig->*port_config_field = defaultValue; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | } // namespace |
| 224 | |
| 225 | void AudioPortConfig::toAudioPortConfig( |
| 226 | struct audio_port_config *dstConfig, |
| 227 | const struct audio_port_config *srcConfig) const |
| 228 | { |
| 229 | updateField(mSamplingRate, &audio_port_config::sample_rate, |
| 230 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u); |
| 231 | updateField(mChannelMask, &audio_port_config::channel_mask, |
| 232 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK, |
| 233 | (audio_channel_mask_t)AUDIO_CHANNEL_NONE); |
| 234 | updateField(mFormat, &audio_port_config::format, |
| 235 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID); |
| 236 | dstConfig->id = mId; |
| 237 | |
| 238 | sp<AudioPort> audioport = getAudioPort(); |
| 239 | if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) { |
| 240 | dstConfig->gain = mGain; |
| 241 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) |
| 242 | && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) { |
| 243 | dstConfig->gain = srcConfig->gain; |
| 244 | } |
| 245 | } else { |
| 246 | dstConfig->gain.index = -1; |
| 247 | } |
| 248 | if (dstConfig->gain.index != -1) { |
| 249 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; |
| 250 | } else { |
| 251 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | bool AudioPortConfig::hasGainController(bool canUseForVolume) const |
| 256 | { |
| 257 | sp<AudioPort> audioport = getAudioPort(); |
| 258 | if (!audioport) { |
| 259 | return false; |
| 260 | } |
| 261 | return canUseForVolume ? audioport->getGains().canUseForVolume() |
| 262 | : audioport->getGains().size() > 0; |
| 263 | } |
| 264 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 265 | bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const |
| 266 | { |
| 267 | return other != nullptr && |
| 268 | mSamplingRate == other->getSamplingRate() && |
| 269 | mFormat == other->getFormat() && |
| 270 | mChannelMask == other->getChannelMask() && |
| 271 | // Compare audio gain config |
| 272 | mGain.index == other->mGain.index && |
| 273 | mGain.mode == other->mGain.mode && |
| 274 | mGain.channel_mask == other->mGain.channel_mask && |
| 275 | std::equal(std::begin(mGain.values), std::end(mGain.values), |
| 276 | std::begin(other->mGain.values)) && |
| 277 | mGain.ramp_duration_ms == other->mGain.ramp_duration_ms; |
| 278 | } |
| 279 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 280 | status_t AudioPortConfig::writeToParcel(Parcel *parcel) const { |
| 281 | media::AudioPortConfig parcelable; |
| 282 | return writeToParcelable(&parcelable) |
| 283 | ?: parcelable.writeToParcel(parcel); |
jiabin | e128485 | 2019-09-11 10:15:46 -0700 | [diff] [blame] | 284 | } |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 285 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 286 | status_t AudioPortConfig::writeToParcelable(media::AudioPortConfig* parcelable) const { |
| 287 | parcelable->sampleRate = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate)); |
| 288 | parcelable->format = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_format_t_AudioFormat(mFormat)); |
| 289 | parcelable->channelMask = VALUE_OR_RETURN_STATUS( |
| 290 | legacy2aidl_audio_channel_mask_t_int32_t(mChannelMask)); |
| 291 | parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId)); |
| 292 | parcelable->gain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.index)); |
| 293 | parcelable->gain.mode = VALUE_OR_RETURN_STATUS( |
Andy Hung | 973638a | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 294 | legacy2aidl_audio_gain_mode_t_int32_t_mask(mGain.mode)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 295 | parcelable->gain.channelMask = VALUE_OR_RETURN_STATUS( |
| 296 | legacy2aidl_audio_channel_mask_t_int32_t(mGain.channel_mask)); |
| 297 | parcelable->gain.rampDurationMs = VALUE_OR_RETURN_STATUS( |
| 298 | convertIntegral<int32_t>(mGain.ramp_duration_ms)); |
| 299 | parcelable->gain.values = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<int32_t>>( |
| 300 | mGain.values, convertIntegral<int32_t, int>)); |
| 301 | return OK; |
| 302 | } |
| 303 | |
| 304 | status_t AudioPortConfig::readFromParcel(const Parcel *parcel) { |
| 305 | media::AudioPortConfig parcelable; |
| 306 | return parcelable.readFromParcel(parcel) |
| 307 | ?: readFromParcelable(parcelable); |
| 308 | } |
| 309 | |
| 310 | status_t AudioPortConfig::readFromParcelable(const media::AudioPortConfig& parcelable) { |
| 311 | mSamplingRate = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.sampleRate)); |
| 312 | mFormat = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioFormat_audio_format_t(parcelable.format)); |
| 313 | mChannelMask = VALUE_OR_RETURN_STATUS( |
| 314 | aidl2legacy_int32_t_audio_channel_mask_t(parcelable.channelMask)); |
| 315 | mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id)); |
| 316 | mGain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.index)); |
| 317 | mGain.mode = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | 0cf9265 | 2020-11-23 13:23:00 -0800 | [diff] [blame] | 318 | aidl2legacy_int32_t_audio_gain_mode_t_mask(parcelable.gain.mode)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 319 | mGain.channel_mask = VALUE_OR_RETURN_STATUS( |
| 320 | aidl2legacy_int32_t_audio_channel_mask_t(parcelable.gain.channelMask)); |
| 321 | mGain.ramp_duration_ms = VALUE_OR_RETURN_STATUS( |
| 322 | convertIntegral<unsigned int>(parcelable.gain.rampDurationMs)); |
| 323 | if (parcelable.gain.values.size() > std::size(mGain.values)) { |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 324 | return BAD_VALUE; |
| 325 | } |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 326 | for (size_t i = 0; i < parcelable.gain.values.size(); ++i) { |
| 327 | mGain.values[i] = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.values[i])); |
| 328 | } |
| 329 | return OK; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | } // namespace android |