AudioOutputDescriptor.cpp revision ffbc80f5908eaf67a033c6e93a343c39dd6894eb
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::AudioOutputDescriptor" 18//#define LOG_NDEBUG 0 19 20#include <AudioPolicyInterface.h> 21#include "AudioOutputDescriptor.h" 22#include "IOProfile.h" 23#include "AudioGain.h" 24#include "Volume.h" 25#include "HwModule.h" 26#include <media/AudioPolicy.h> 27 28// A device mask for all audio output devices that are considered "remote" when evaluating 29// active output devices in isStreamActiveRemotely() 30#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX 31 32namespace android { 33 34AudioOutputDescriptor::AudioOutputDescriptor(const sp<AudioPort>& port, 35 AudioPolicyClientInterface *clientInterface) 36 : mPort(port), mDevice(AUDIO_DEVICE_NONE), 37 mPatchHandle(0), mClientInterface(clientInterface), mId(0) 38{ 39 // clear usage count for all stream types 40 for (int i = 0; i < AUDIO_STREAM_CNT; i++) { 41 mRefCount[i] = 0; 42 mCurVolume[i] = -1.0; 43 mMuteCount[i] = 0; 44 mStopTime[i] = 0; 45 } 46 for (int i = 0; i < NUM_STRATEGIES; i++) { 47 mStrategyMutedByDevice[i] = false; 48 } 49 if (port != NULL) { 50 mSamplingRate = port->pickSamplingRate(); 51 mFormat = port->pickFormat(); 52 mChannelMask = port->pickChannelMask(); 53 if (port->mGains.size() > 0) { 54 port->mGains[0]->getDefaultConfig(&mGain); 55 } 56 } 57} 58 59audio_module_handle_t AudioOutputDescriptor::getModuleHandle() const 60{ 61 return mPort->getModuleHandle(); 62} 63 64audio_port_handle_t AudioOutputDescriptor::getId() const 65{ 66 return mId; 67} 68 69audio_devices_t AudioOutputDescriptor::device() const 70{ 71 return mDevice; 72} 73 74audio_devices_t AudioOutputDescriptor::supportedDevices() 75{ 76 return mDevice; 77} 78 79bool AudioOutputDescriptor::sharesHwModuleWith( 80 const sp<AudioOutputDescriptor> outputDesc) 81{ 82 if (outputDesc->isDuplicated()) { 83 return sharesHwModuleWith(outputDesc->subOutput1()) || 84 sharesHwModuleWith(outputDesc->subOutput2()); 85 } else { 86 return (getModuleHandle() == outputDesc->getModuleHandle()); 87 } 88} 89 90void AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream, 91 int delta) 92{ 93 if ((delta + (int)mRefCount[stream]) < 0) { 94 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", 95 delta, stream, mRefCount[stream]); 96 mRefCount[stream] = 0; 97 return; 98 } 99 mRefCount[stream] += delta; 100 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); 101} 102 103bool AudioOutputDescriptor::isActive(uint32_t inPastMs) const 104{ 105 nsecs_t sysTime = 0; 106 if (inPastMs != 0) { 107 sysTime = systemTime(); 108 } 109 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { 110 if (i == AUDIO_STREAM_PATCH) { 111 continue; 112 } 113 if (isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { 114 return true; 115 } 116 } 117 return false; 118} 119 120bool AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream, 121 uint32_t inPastMs, 122 nsecs_t sysTime) const 123{ 124 if (mRefCount[stream] != 0) { 125 return true; 126 } 127 if (inPastMs == 0) { 128 return false; 129 } 130 if (sysTime == 0) { 131 sysTime = systemTime(); 132 } 133 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) { 134 return true; 135 } 136 return false; 137} 138 139 140bool AudioOutputDescriptor::isFixedVolume(audio_devices_t device __unused) 141{ 142 return false; 143} 144 145bool AudioOutputDescriptor::setVolume(float volume, 146 audio_stream_type_t stream, 147 audio_devices_t device __unused, 148 uint32_t delayMs, 149 bool force) 150{ 151 // We actually change the volume if: 152 // - the float value returned by computeVolume() changed 153 // - the force flag is set 154 if (volume != mCurVolume[stream] || force) { 155 ALOGV("setVolume() for stream %d, volume %f, delay %d", stream, volume, delayMs); 156 mCurVolume[stream] = volume; 157 return true; 158 } 159 return false; 160} 161 162void AudioOutputDescriptor::toAudioPortConfig( 163 struct audio_port_config *dstConfig, 164 const struct audio_port_config *srcConfig) const 165{ 166 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| 167 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; 168 if (srcConfig != NULL) { 169 dstConfig->config_mask |= srcConfig->config_mask; 170 } 171 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); 172 173 dstConfig->id = mId; 174 dstConfig->role = AUDIO_PORT_ROLE_SOURCE; 175 dstConfig->type = AUDIO_PORT_TYPE_MIX; 176 dstConfig->ext.mix.hw_module = getModuleHandle(); 177 dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; 178} 179 180void AudioOutputDescriptor::toAudioPort( 181 struct audio_port *port) const 182{ 183 mPort->toAudioPort(port); 184 port->id = mId; 185 port->ext.mix.hw_module = getModuleHandle(); 186} 187 188status_t AudioOutputDescriptor::dump(int fd) 189{ 190 const size_t SIZE = 256; 191 char buffer[SIZE]; 192 String8 result; 193 194 snprintf(buffer, SIZE, " ID: %d\n", mId); 195 result.append(buffer); 196 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); 197 result.append(buffer); 198 snprintf(buffer, SIZE, " Format: %08x\n", mFormat); 199 result.append(buffer); 200 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); 201 result.append(buffer); 202 snprintf(buffer, SIZE, " Devices %08x\n", device()); 203 result.append(buffer); 204 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); 205 result.append(buffer); 206 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { 207 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", 208 i, mCurVolume[i], mRefCount[i], mMuteCount[i]); 209 result.append(buffer); 210 } 211 write(fd, result.string(), result.size()); 212 213 return NO_ERROR; 214} 215 216void AudioOutputDescriptor::log(const char* indent) 217{ 218 ALOGI("%sID: %d,0x%X, [rt:%d fmt:0x%X ch:0x%X]", 219 indent, mId, mId, mSamplingRate, mFormat, mChannelMask); 220} 221 222// SwAudioOutputDescriptor implementation 223SwAudioOutputDescriptor::SwAudioOutputDescriptor( 224 const sp<IOProfile>& profile, AudioPolicyClientInterface *clientInterface) 225 : AudioOutputDescriptor(profile, clientInterface), 226 mProfile(profile), mIoHandle(0), mLatency(0), 227 mFlags((audio_output_flags_t)0), mPolicyMix(NULL), 228 mOutput1(0), mOutput2(0), mDirectOpenCount(0) 229{ 230 if (profile != NULL) { 231 mFlags = (audio_output_flags_t)profile->mFlags; 232 } 233} 234 235void SwAudioOutputDescriptor::setIoHandle(audio_io_handle_t ioHandle) 236{ 237 mId = AudioPort::getNextUniqueId(); 238 mIoHandle = ioHandle; 239} 240 241 242status_t SwAudioOutputDescriptor::dump(int fd) 243{ 244 const size_t SIZE = 256; 245 char buffer[SIZE]; 246 String8 result; 247 248 snprintf(buffer, SIZE, " Latency: %d\n", mLatency); 249 result.append(buffer); 250 snprintf(buffer, SIZE, " Flags %08x\n", mFlags); 251 result.append(buffer); 252 write(fd, result.string(), result.size()); 253 254 AudioOutputDescriptor::dump(fd); 255 256 return NO_ERROR; 257} 258 259audio_devices_t SwAudioOutputDescriptor::device() const 260{ 261 if (isDuplicated()) { 262 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice); 263 } else { 264 return mDevice; 265 } 266} 267 268bool SwAudioOutputDescriptor::sharesHwModuleWith( 269 const sp<AudioOutputDescriptor> outputDesc) 270{ 271 if (isDuplicated()) { 272 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc); 273 } else if (outputDesc->isDuplicated()){ 274 return sharesHwModuleWith(outputDesc->subOutput1()) || 275 sharesHwModuleWith(outputDesc->subOutput2()); 276 } else { 277 return AudioOutputDescriptor::sharesHwModuleWith(outputDesc); 278 } 279} 280 281audio_devices_t SwAudioOutputDescriptor::supportedDevices() 282{ 283 if (isDuplicated()) { 284 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices()); 285 } else { 286 return mProfile->mSupportedDevices.types() ; 287 } 288} 289 290uint32_t SwAudioOutputDescriptor::latency() 291{ 292 if (isDuplicated()) { 293 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency; 294 } else { 295 return mLatency; 296 } 297} 298 299void SwAudioOutputDescriptor::changeRefCount(audio_stream_type_t stream, 300 int delta) 301{ 302 // forward usage count change to attached outputs 303 if (isDuplicated()) { 304 mOutput1->changeRefCount(stream, delta); 305 mOutput2->changeRefCount(stream, delta); 306 } 307 AudioOutputDescriptor::changeRefCount(stream, delta); 308} 309 310 311bool SwAudioOutputDescriptor::isFixedVolume(audio_devices_t device) 312{ 313 // unit gain if rerouting to external policy 314 if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { 315 if (mPolicyMix != NULL) { 316 ALOGV("max gain when rerouting for output=%d", mIoHandle); 317 return true; 318 } 319 } 320 return false; 321} 322 323void SwAudioOutputDescriptor::toAudioPortConfig( 324 struct audio_port_config *dstConfig, 325 const struct audio_port_config *srcConfig) const 326{ 327 328 ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle); 329 AudioOutputDescriptor::toAudioPortConfig(dstConfig, srcConfig); 330 331 dstConfig->ext.mix.handle = mIoHandle; 332} 333 334void SwAudioOutputDescriptor::toAudioPort( 335 struct audio_port *port) const 336{ 337 ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle); 338 339 AudioOutputDescriptor::toAudioPort(port); 340 341 toAudioPortConfig(&port->active_config); 342 port->ext.mix.handle = mIoHandle; 343 port->ext.mix.latency_class = 344 mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL; 345} 346 347bool SwAudioOutputDescriptor::setVolume(float volume, 348 audio_stream_type_t stream, 349 audio_devices_t device, 350 uint32_t delayMs, 351 bool force) 352{ 353 bool changed = AudioOutputDescriptor::setVolume(volume, stream, device, delayMs, force); 354 355 if (changed) { 356 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is 357 // enabled 358 float volume = Volume::DbToAmpl(mCurVolume[stream]); 359 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { 360 mClientInterface->setStreamVolume( 361 AUDIO_STREAM_VOICE_CALL, volume, mIoHandle, delayMs); 362 } 363 mClientInterface->setStreamVolume(stream, volume, mIoHandle, delayMs); 364 } 365 return changed; 366} 367 368// SwAudioOutputCollection implementation 369 370bool SwAudioOutputCollection::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const 371{ 372 nsecs_t sysTime = systemTime(); 373 for (size_t i = 0; i < this->size(); i++) { 374 const sp<SwAudioOutputDescriptor> outputDesc = this->valueAt(i); 375 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) { 376 return true; 377 } 378 } 379 return false; 380} 381 382bool SwAudioOutputCollection::isStreamActiveRemotely(audio_stream_type_t stream, 383 uint32_t inPastMs) const 384{ 385 nsecs_t sysTime = systemTime(); 386 for (size_t i = 0; i < size(); i++) { 387 const sp<SwAudioOutputDescriptor> outputDesc = valueAt(i); 388 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && 389 outputDesc->isStreamActive(stream, inPastMs, sysTime)) { 390 // do not consider re routing (when the output is going to a dynamic policy) 391 // as "remote playback" 392 if (outputDesc->mPolicyMix == NULL) { 393 return true; 394 } 395 } 396 } 397 return false; 398} 399 400audio_io_handle_t SwAudioOutputCollection::getA2dpOutput() const 401{ 402 for (size_t i = 0; i < size(); i++) { 403 sp<SwAudioOutputDescriptor> outputDesc = valueAt(i); 404 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { 405 return this->keyAt(i); 406 } 407 } 408 return 0; 409} 410 411sp<SwAudioOutputDescriptor> SwAudioOutputCollection::getPrimaryOutput() const 412{ 413 for (size_t i = 0; i < size(); i++) { 414 const sp<SwAudioOutputDescriptor> outputDesc = valueAt(i); 415 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { 416 return outputDesc; 417 } 418 } 419 return NULL; 420} 421 422sp<SwAudioOutputDescriptor> SwAudioOutputCollection::getOutputFromId(audio_port_handle_t id) const 423{ 424 sp<SwAudioOutputDescriptor> outputDesc = NULL; 425 for (size_t i = 0; i < size(); i++) { 426 outputDesc = valueAt(i); 427 if (outputDesc->getId() == id) { 428 break; 429 } 430 } 431 return outputDesc; 432} 433 434bool SwAudioOutputCollection::isAnyOutputActive(audio_stream_type_t streamToIgnore) const 435{ 436 for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) { 437 if (s == (size_t) streamToIgnore) { 438 continue; 439 } 440 for (size_t i = 0; i < size(); i++) { 441 const sp<SwAudioOutputDescriptor> outputDesc = valueAt(i); 442 if (outputDesc->mRefCount[s] != 0) { 443 return true; 444 } 445 } 446 } 447 return false; 448} 449 450audio_devices_t SwAudioOutputCollection::getSupportedDevices(audio_io_handle_t handle) const 451{ 452 sp<SwAudioOutputDescriptor> outputDesc = valueFor(handle); 453 audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types(); 454 return devices; 455} 456 457 458status_t SwAudioOutputCollection::dump(int fd) const 459{ 460 const size_t SIZE = 256; 461 char buffer[SIZE]; 462 463 snprintf(buffer, SIZE, "\nOutputs dump:\n"); 464 write(fd, buffer, strlen(buffer)); 465 for (size_t i = 0; i < size(); i++) { 466 snprintf(buffer, SIZE, "- Output %d dump:\n", keyAt(i)); 467 write(fd, buffer, strlen(buffer)); 468 valueAt(i)->dump(fd); 469 } 470 471 return NO_ERROR; 472} 473 474}; //namespace android 475