AudioPort.cpp revision 112b0af826aeca45855690b9c105b2cdf9938bbe
1/* 2 * Copyright (C) 2015 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 */ 16 17#define LOG_TAG "APM::AudioPort" 18//#define LOG_NDEBUG 0 19#include "TypeConverter.h" 20#include "AudioPort.h" 21#include "HwModule.h" 22#include "AudioGain.h" 23#include <policy.h> 24 25#ifndef ARRAY_SIZE 26#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 27#endif 28 29namespace android { 30 31int32_t volatile AudioPort::mNextUniqueId = 1; 32 33// --- AudioPort class implementation 34void AudioPort::attach(const sp<HwModule>& module) 35{ 36 mModule = module; 37} 38 39audio_port_handle_t AudioPort::getNextUniqueId() 40{ 41 return static_cast<audio_port_handle_t>(android_atomic_inc(&mNextUniqueId)); 42} 43 44audio_module_handle_t AudioPort::getModuleHandle() const 45{ 46 if (mModule == 0) { 47 return 0; 48 } 49 return mModule->mHandle; 50} 51 52uint32_t AudioPort::getModuleVersion() const 53{ 54 if (mModule == 0) { 55 return 0; 56 } 57 return mModule->getHalVersion(); 58} 59 60const char *AudioPort::getModuleName() const 61{ 62 if (mModule == 0) { 63 return "invalid module"; 64 } 65 return mModule->getName(); 66} 67 68void AudioPort::toAudioPort(struct audio_port *port) const 69{ 70 // TODO: update this function once audio_port structure reflects the new profile definition. 71 // For compatibility reason: flatening the AudioProfile into audio_port structure. 72 SortedVector<audio_format_t> flatenedFormats; 73 SampleRateVector flatenedRates; 74 ChannelsVector flatenedChannels; 75 for (size_t profileIndex = 0; profileIndex < mProfiles.size(); profileIndex++) { 76 if (mProfiles[profileIndex]->isValid()) { 77 audio_format_t formatToExport = mProfiles[profileIndex]->getFormat(); 78 const SampleRateVector &ratesToExport = mProfiles[profileIndex]->getSampleRates(); 79 const ChannelsVector &channelsToExport = mProfiles[profileIndex]->getChannels(); 80 81 if (flatenedFormats.indexOf(formatToExport) < 0) { 82 flatenedFormats.add(formatToExport); 83 } 84 for (size_t rateIndex = 0; rateIndex < ratesToExport.size(); rateIndex++) { 85 uint32_t rate = ratesToExport[rateIndex]; 86 if (flatenedRates.indexOf(rate) < 0) { 87 flatenedRates.add(rate); 88 } 89 } 90 for (size_t chanIndex = 0; chanIndex < channelsToExport.size(); chanIndex++) { 91 audio_channel_mask_t channels = channelsToExport[chanIndex]; 92 if (flatenedChannels.indexOf(channels) < 0) { 93 flatenedChannels.add(channels); 94 } 95 } 96 if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES || 97 flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS || 98 flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) { 99 ALOGE("%s: bailing out: cannot export profiles to port config", __FUNCTION__); 100 return; 101 } 102 } 103 } 104 port->role = mRole; 105 port->type = mType; 106 strlcpy(port->name, mName, AUDIO_PORT_MAX_NAME_LEN); 107 port->num_sample_rates = flatenedRates.size(); 108 port->num_channel_masks = flatenedChannels.size(); 109 port->num_formats = flatenedFormats.size(); 110 for (size_t i = 0; i < flatenedRates.size(); i++) { 111 port->sample_rates[i] = flatenedRates[i]; 112 } 113 for (size_t i = 0; i < flatenedChannels.size(); i++) { 114 port->channel_masks[i] = flatenedChannels[i]; 115 } 116 for (size_t i = 0; i < flatenedFormats.size(); i++) { 117 port->formats[i] = flatenedFormats[i]; 118 } 119 120 ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); 121 122 uint32_t i; 123 for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) { 124 port->gains[i] = mGains[i]->getGain(); 125 } 126 port->num_gains = i; 127} 128 129void AudioPort::importAudioPort(const sp<AudioPort> port) 130{ 131 size_t indexToImport; 132 for (indexToImport = 0; indexToImport < port->mProfiles.size(); indexToImport++) { 133 const sp<AudioProfile> &profileToImport = port->mProfiles[indexToImport]; 134 if (profileToImport->isValid()) { 135 // Import only valid port, i.e. valid format, non empty rates and channels masks 136 bool hasSameProfile = false; 137 for (size_t profileIndex = 0; profileIndex < mProfiles.size(); profileIndex++) { 138 if (*mProfiles[profileIndex] == *profileToImport) { 139 // never import a profile twice 140 hasSameProfile = true; 141 break; 142 } 143 } 144 if (hasSameProfile) { // never import a same profile twice 145 continue; 146 } 147 addAudioProfile(profileToImport); 148 } 149 } 150} 151 152void AudioPort::pickSamplingRate(uint32_t &pickedRate,const SampleRateVector &samplingRates) const 153{ 154 pickedRate = 0; 155 // For direct outputs, pick minimum sampling rate: this helps ensuring that the 156 // channel count / sampling rate combination chosen will be supported by the connected 157 // sink 158 if (isDirectOutput()) { 159 uint32_t samplingRate = UINT_MAX; 160 for (size_t i = 0; i < samplingRates.size(); i ++) { 161 if ((samplingRates[i] < samplingRate) && (samplingRates[i] > 0)) { 162 samplingRate = samplingRates[i]; 163 } 164 } 165 pickedRate = (samplingRate == UINT_MAX) ? 0 : samplingRate; 166 } else { 167 uint32_t maxRate = MAX_MIXER_SAMPLING_RATE; 168 169 // For mixed output and inputs, use max mixer sampling rates. Do not 170 // limit sampling rate otherwise 171 // For inputs, also see checkCompatibleSamplingRate(). 172 if (mType != AUDIO_PORT_TYPE_MIX) { 173 maxRate = UINT_MAX; 174 } 175 // TODO: should mSamplingRates[] be ordered in terms of our preference 176 // and we return the first (and hence most preferred) match? This is of concern if 177 // we want to choose 96kHz over 192kHz for USB driver stability or resource constraints. 178 for (size_t i = 0; i < samplingRates.size(); i ++) { 179 if ((samplingRates[i] > pickedRate) && (samplingRates[i] <= maxRate)) { 180 pickedRate = samplingRates[i]; 181 } 182 } 183 } 184} 185 186void AudioPort::pickChannelMask(audio_channel_mask_t &pickedChannelMask, 187 const ChannelsVector &channelMasks) const 188{ 189 pickedChannelMask = AUDIO_CHANNEL_NONE; 190 // For direct outputs, pick minimum channel count: this helps ensuring that the 191 // channel count / sampling rate combination chosen will be supported by the connected 192 // sink 193 if (isDirectOutput()) { 194 uint32_t channelCount = UINT_MAX; 195 for (size_t i = 0; i < channelMasks.size(); i ++) { 196 uint32_t cnlCount; 197 if (useInputChannelMask()) { 198 cnlCount = audio_channel_count_from_in_mask(channelMasks[i]); 199 } else { 200 cnlCount = audio_channel_count_from_out_mask(channelMasks[i]); 201 } 202 if ((cnlCount < channelCount) && (cnlCount > 0)) { 203 pickedChannelMask = channelMasks[i]; 204 channelCount = cnlCount; 205 } 206 } 207 } else { 208 uint32_t channelCount = 0; 209 uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT; 210 211 // For mixed output and inputs, use max mixer channel count. Do not 212 // limit channel count otherwise 213 if (mType != AUDIO_PORT_TYPE_MIX) { 214 maxCount = UINT_MAX; 215 } 216 for (size_t i = 0; i < channelMasks.size(); i ++) { 217 uint32_t cnlCount; 218 if (useInputChannelMask()) { 219 cnlCount = audio_channel_count_from_in_mask(channelMasks[i]); 220 } else { 221 cnlCount = audio_channel_count_from_out_mask(channelMasks[i]); 222 } 223 if ((cnlCount > channelCount) && (cnlCount <= maxCount)) { 224 pickedChannelMask = channelMasks[i]; 225 channelCount = cnlCount; 226 } 227 } 228 } 229} 230 231/* format in order of increasing preference */ 232const audio_format_t AudioPort::sPcmFormatCompareTable[] = { 233 AUDIO_FORMAT_DEFAULT, 234 AUDIO_FORMAT_PCM_16_BIT, 235 AUDIO_FORMAT_PCM_8_24_BIT, 236 AUDIO_FORMAT_PCM_24_BIT_PACKED, 237 AUDIO_FORMAT_PCM_32_BIT, 238 AUDIO_FORMAT_PCM_FLOAT, 239}; 240 241int AudioPort::compareFormats(audio_format_t format1, audio_format_t format2) 242{ 243 // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any 244 // compressed format and better than any PCM format. This is by design of pickFormat() 245 if (!audio_is_linear_pcm(format1)) { 246 if (!audio_is_linear_pcm(format2)) { 247 return 0; 248 } 249 return 1; 250 } 251 if (!audio_is_linear_pcm(format2)) { 252 return -1; 253 } 254 255 int index1 = -1, index2 = -1; 256 for (size_t i = 0; 257 (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1)); 258 i ++) { 259 if (sPcmFormatCompareTable[i] == format1) { 260 index1 = i; 261 } 262 if (sPcmFormatCompareTable[i] == format2) { 263 index2 = i; 264 } 265 } 266 // format1 not found => index1 < 0 => format2 > format1 267 // format2 not found => index2 < 0 => format2 < format1 268 return index1 - index2; 269} 270 271void AudioPort::pickAudioProfile(uint32_t &samplingRate, 272 audio_channel_mask_t &channelMask, 273 audio_format_t &format) const 274{ 275 format = AUDIO_FORMAT_DEFAULT; 276 samplingRate = 0; 277 channelMask = AUDIO_CHANNEL_NONE; 278 279 // special case for uninitialized dynamic profile 280 if (!mProfiles.hasValidProfile()) { 281 return; 282 } 283 audio_format_t bestFormat = sPcmFormatCompareTable[ARRAY_SIZE(sPcmFormatCompareTable) - 1]; 284 // For mixed output and inputs, use best mixer output format. 285 // Do not limit format otherwise 286 if ((mType != AUDIO_PORT_TYPE_MIX) || isDirectOutput()) { 287 bestFormat = AUDIO_FORMAT_INVALID; 288 } 289 290 for (size_t i = 0; i < mProfiles.size(); i ++) { 291 if (!mProfiles[i]->isValid()) { 292 continue; 293 } 294 audio_format_t formatToCompare = mProfiles[i]->getFormat(); 295 if ((compareFormats(formatToCompare, format) > 0) && 296 (compareFormats(formatToCompare, bestFormat) <= 0)) { 297 uint32_t pickedSamplingRate = 0; 298 audio_channel_mask_t pickedChannelMask = AUDIO_CHANNEL_NONE; 299 pickChannelMask(pickedChannelMask, mProfiles[i]->getChannels()); 300 pickSamplingRate(pickedSamplingRate, mProfiles[i]->getSampleRates()); 301 302 if (formatToCompare != AUDIO_FORMAT_DEFAULT && pickedChannelMask != AUDIO_CHANNEL_NONE 303 && pickedSamplingRate != 0) { 304 format = formatToCompare; 305 channelMask = pickedChannelMask; 306 samplingRate = pickedSamplingRate; 307 // TODO: shall we return on the first one or still trying to pick a better Profile? 308 } 309 } 310 } 311} 312 313status_t AudioPort::checkGain(const struct audio_gain_config *gainConfig, int index) const 314{ 315 if (index < 0 || (size_t)index >= mGains.size()) { 316 return BAD_VALUE; 317 } 318 return mGains[index]->checkConfig(gainConfig); 319} 320 321void AudioPort::dump(int fd, int spaces, bool verbose) const 322{ 323 const size_t SIZE = 256; 324 char buffer[SIZE]; 325 String8 result; 326 327 if (mName.length() != 0) { 328 snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string()); 329 result.append(buffer); 330 write(fd, result.string(), result.size()); 331 } 332 if (verbose) { 333 mProfiles.dump(fd, spaces); 334 335 if (mGains.size() != 0) { 336 snprintf(buffer, SIZE, "%*s- gains:\n", spaces, ""); 337 write(fd, buffer, strlen(buffer) + 1); 338 for (size_t i = 0; i < mGains.size(); i++) { 339 mGains[i]->dump(fd, spaces + 2, i); 340 } 341 } 342 } 343} 344 345void AudioPort::log(const char* indent) const 346{ 347 ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.string(), mType, mRole); 348} 349 350// --- AudioPortConfig class implementation 351 352AudioPortConfig::AudioPortConfig() 353{ 354 mSamplingRate = 0; 355 mChannelMask = AUDIO_CHANNEL_NONE; 356 mFormat = AUDIO_FORMAT_INVALID; 357 mGain.index = -1; 358} 359 360status_t AudioPortConfig::applyAudioPortConfig(const struct audio_port_config *config, 361 struct audio_port_config *backupConfig) 362{ 363 struct audio_port_config localBackupConfig; 364 status_t status = NO_ERROR; 365 366 localBackupConfig.config_mask = config->config_mask; 367 toAudioPortConfig(&localBackupConfig); 368 369 sp<AudioPort> audioport = getAudioPort(); 370 if (audioport == 0) { 371 status = NO_INIT; 372 goto exit; 373 } 374 status = audioport->checkExactAudioProfile(config->sample_rate, 375 config->channel_mask, 376 config->format); 377 if (status != NO_ERROR) { 378 goto exit; 379 } 380 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { 381 mSamplingRate = config->sample_rate; 382 } 383 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { 384 mChannelMask = config->channel_mask; 385 } 386 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { 387 mFormat = config->format; 388 } 389 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { 390 status = audioport->checkGain(&config->gain, config->gain.index); 391 if (status != NO_ERROR) { 392 goto exit; 393 } 394 mGain = config->gain; 395 } 396 397exit: 398 if (status != NO_ERROR) { 399 applyAudioPortConfig(&localBackupConfig); 400 } 401 if (backupConfig != NULL) { 402 *backupConfig = localBackupConfig; 403 } 404 return status; 405} 406 407void AudioPortConfig::toAudioPortConfig(struct audio_port_config *dstConfig, 408 const struct audio_port_config *srcConfig) const 409{ 410 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { 411 dstConfig->sample_rate = mSamplingRate; 412 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) { 413 dstConfig->sample_rate = srcConfig->sample_rate; 414 } 415 } else { 416 dstConfig->sample_rate = 0; 417 } 418 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { 419 dstConfig->channel_mask = mChannelMask; 420 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) { 421 dstConfig->channel_mask = srcConfig->channel_mask; 422 } 423 } else { 424 dstConfig->channel_mask = AUDIO_CHANNEL_NONE; 425 } 426 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) { 427 dstConfig->format = mFormat; 428 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) { 429 dstConfig->format = srcConfig->format; 430 } 431 } else { 432 dstConfig->format = AUDIO_FORMAT_INVALID; 433 } 434 sp<AudioPort> audioport = getAudioPort(); 435 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) { 436 dstConfig->gain = mGain; 437 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) 438 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) { 439 dstConfig->gain = srcConfig->gain; 440 } 441 } else { 442 dstConfig->gain.index = -1; 443 } 444 if (dstConfig->gain.index != -1) { 445 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; 446 } else { 447 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; 448 } 449} 450 451}; // namespace android 452