jiabin | eaf09f0 | 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 | dd81cc2 | 2019-09-11 10:15:46 -0700 | [diff] [blame^] | 16 | #define LOG_TAG "AudioPort" |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 17 | |
| 18 | #include <algorithm> |
| 19 | |
| 20 | #include <android-base/stringprintf.h> |
jiabin | dd81cc2 | 2019-09-11 10:15:46 -0700 | [diff] [blame^] | 21 | #include <media/AudioPort.h> |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
jiabin | dff2a4f | 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 | |
| 41 | void AudioPort::toAudioPort(struct audio_port *port) const { |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 42 | // TODO: update this function once audio_port structure reflects the new profile definition. |
| 43 | // For compatibility reason: flatening the AudioProfile into audio_port structure. |
| 44 | FormatSet flatenedFormats; |
| 45 | SampleRateSet flatenedRates; |
| 46 | ChannelMaskSet flatenedChannels; |
jiabin | b9733bc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 47 | for (const auto& profile : mProfiles) { |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 48 | if (profile->isValid()) { |
| 49 | audio_format_t formatToExport = profile->getFormat(); |
| 50 | const SampleRateSet &ratesToExport = profile->getSampleRates(); |
| 51 | const ChannelMaskSet &channelsToExport = profile->getChannels(); |
| 52 | |
| 53 | flatenedFormats.insert(formatToExport); |
| 54 | flatenedRates.insert(ratesToExport.begin(), ratesToExport.end()); |
| 55 | flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end()); |
| 56 | |
| 57 | if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES || |
| 58 | flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS || |
| 59 | flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) { |
| 60 | ALOGE("%s: bailing out: cannot export profiles to port config", __func__); |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | port->role = mRole; |
| 66 | port->type = mType; |
| 67 | strlcpy(port->name, mName.c_str(), AUDIO_PORT_MAX_NAME_LEN); |
| 68 | port->num_sample_rates = flatenedRates.size(); |
| 69 | port->num_channel_masks = flatenedChannels.size(); |
| 70 | port->num_formats = flatenedFormats.size(); |
| 71 | std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates); |
| 72 | std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks); |
| 73 | std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats); |
| 74 | |
| 75 | ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); |
| 76 | |
| 77 | port->num_gains = std::min(mGains.size(), (size_t) AUDIO_PORT_MAX_GAINS); |
| 78 | for (size_t i = 0; i < port->num_gains; i++) { |
| 79 | port->gains[i] = mGains[i]->getGain(); |
| 80 | } |
| 81 | } |
| 82 | |
jiabin | dff2a4f | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 83 | void AudioPort::dump(std::string *dst, int spaces, bool verbose) const { |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 84 | if (!mName.empty()) { |
| 85 | dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str())); |
| 86 | } |
| 87 | if (verbose) { |
| 88 | std::string profilesStr; |
jiabin | b9733bc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 89 | mProfiles.dump(&profilesStr, spaces); |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 90 | dst->append(profilesStr); |
| 91 | |
| 92 | if (mGains.size() != 0) { |
| 93 | dst->append(base::StringPrintf("%*s- gains:\n", spaces, "")); |
| 94 | for (size_t i = 0; i < mGains.size(); i++) { |
| 95 | std::string gainStr; |
| 96 | mGains[i]->dump(&gainStr, spaces + 2, i); |
| 97 | dst->append(gainStr); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
jiabin | dff2a4f | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 103 | void AudioPort::log(const char* indent) const |
| 104 | { |
| 105 | ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole); |
| 106 | } |
| 107 | |
| 108 | // --- AudioPortConfig class implementation |
| 109 | |
| 110 | status_t AudioPortConfig::applyAudioPortConfig( |
| 111 | const struct audio_port_config *config, |
| 112 | struct audio_port_config *backupConfig __unused) |
| 113 | { |
| 114 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 115 | mSamplingRate = config->sample_rate; |
| 116 | } |
| 117 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 118 | mChannelMask = config->channel_mask; |
| 119 | } |
| 120 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 121 | mFormat = config->format; |
| 122 | } |
| 123 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
| 124 | mGain = config->gain; |
| 125 | } |
| 126 | |
| 127 | return NO_ERROR; |
| 128 | } |
| 129 | |
| 130 | namespace { |
| 131 | |
| 132 | template<typename T> |
| 133 | void updateField( |
| 134 | const T& portConfigField, T audio_port_config::*port_config_field, |
| 135 | struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig, |
| 136 | unsigned int configMask, T defaultValue) |
| 137 | { |
| 138 | if (dstConfig->config_mask & configMask) { |
| 139 | if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) { |
| 140 | dstConfig->*port_config_field = srcConfig->*port_config_field; |
| 141 | } else { |
| 142 | dstConfig->*port_config_field = portConfigField; |
| 143 | } |
| 144 | } else { |
| 145 | dstConfig->*port_config_field = defaultValue; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } // namespace |
| 150 | |
| 151 | void AudioPortConfig::toAudioPortConfig( |
| 152 | struct audio_port_config *dstConfig, |
| 153 | const struct audio_port_config *srcConfig) const |
| 154 | { |
| 155 | updateField(mSamplingRate, &audio_port_config::sample_rate, |
| 156 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u); |
| 157 | updateField(mChannelMask, &audio_port_config::channel_mask, |
| 158 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK, |
| 159 | (audio_channel_mask_t)AUDIO_CHANNEL_NONE); |
| 160 | updateField(mFormat, &audio_port_config::format, |
| 161 | dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID); |
| 162 | dstConfig->id = mId; |
| 163 | |
| 164 | sp<AudioPort> audioport = getAudioPort(); |
| 165 | if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) { |
| 166 | dstConfig->gain = mGain; |
| 167 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) |
| 168 | && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) { |
| 169 | dstConfig->gain = srcConfig->gain; |
| 170 | } |
| 171 | } else { |
| 172 | dstConfig->gain.index = -1; |
| 173 | } |
| 174 | if (dstConfig->gain.index != -1) { |
| 175 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; |
| 176 | } else { |
| 177 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | bool AudioPortConfig::hasGainController(bool canUseForVolume) const |
| 182 | { |
| 183 | sp<AudioPort> audioport = getAudioPort(); |
| 184 | if (!audioport) { |
| 185 | return false; |
| 186 | } |
| 187 | return canUseForVolume ? audioport->getGains().canUseForVolume() |
| 188 | : audioport->getGains().size() > 0; |
| 189 | } |
| 190 | |
jiabin | dd81cc2 | 2019-09-11 10:15:46 -0700 | [diff] [blame^] | 191 | } |