Effects.cpp revision 98232ab7c20bfbdfe176d2c25bb810dbb5e7b4f1
1/* 2** 3** Copyright 2012, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18 19#define LOG_TAG "AudioFlinger" 20//#define LOG_NDEBUG 0 21 22#include "Configuration.h" 23#include <utils/Log.h> 24#include <audio_effects/effect_visualizer.h> 25#include <audio_utils/primitives.h> 26#include <private/media/AudioEffectShared.h> 27#include <media/EffectsFactoryApi.h> 28 29#include "AudioFlinger.h" 30#include "ServiceUtilities.h" 31 32// ---------------------------------------------------------------------------- 33 34// Note: the following macro is used for extremely verbose logging message. In 35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to 36// 0; but one side effect of this is to turn all LOGV's as well. Some messages 37// are so verbose that we want to suppress them even when we have ALOG_ASSERT 38// turned on. Do not uncomment the #def below unless you really know what you 39// are doing and want to see all of the extremely verbose messages. 40//#define VERY_VERY_VERBOSE_LOGGING 41#ifdef VERY_VERY_VERBOSE_LOGGING 42#define ALOGVV ALOGV 43#else 44#define ALOGVV(a...) do { } while(0) 45#endif 46 47namespace android { 48 49// ---------------------------------------------------------------------------- 50// EffectModule implementation 51// ---------------------------------------------------------------------------- 52 53#undef LOG_TAG 54#define LOG_TAG "AudioFlinger::EffectModule" 55 56AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, 57 const wp<AudioFlinger::EffectChain>& chain, 58 effect_descriptor_t *desc, 59 int id, 60 int sessionId) 61 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX), 62 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), 63 mDescriptor(*desc), 64 // mConfig is set by configure() and not used before then 65 mEffectInterface(NULL), 66 mStatus(NO_INIT), mState(IDLE), 67 // mMaxDisableWaitCnt is set by configure() and not used before then 68 // mDisableWaitCnt is set by process() and updateState() and not used before then 69 mSuspended(false) 70{ 71 ALOGV("Constructor %p", this); 72 int lStatus; 73 74 // create effect engine from effect factory 75 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); 76 77 if (mStatus != NO_ERROR) { 78 return; 79 } 80 lStatus = init(); 81 if (lStatus < 0) { 82 mStatus = lStatus; 83 goto Error; 84 } 85 86 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); 87 return; 88Error: 89 EffectRelease(mEffectInterface); 90 mEffectInterface = NULL; 91 ALOGV("Constructor Error %d", mStatus); 92} 93 94AudioFlinger::EffectModule::~EffectModule() 95{ 96 ALOGV("Destructor %p", this); 97 if (mEffectInterface != NULL) { 98 remove_effect_from_hal_l(); 99 // release effect engine 100 EffectRelease(mEffectInterface); 101 } 102} 103 104status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle) 105{ 106 status_t status; 107 108 Mutex::Autolock _l(mLock); 109 int priority = handle->priority(); 110 size_t size = mHandles.size(); 111 EffectHandle *controlHandle = NULL; 112 size_t i; 113 for (i = 0; i < size; i++) { 114 EffectHandle *h = mHandles[i]; 115 if (h == NULL || h->destroyed_l()) { 116 continue; 117 } 118 // first non destroyed handle is considered in control 119 if (controlHandle == NULL) { 120 controlHandle = h; 121 } 122 if (h->priority() <= priority) { 123 break; 124 } 125 } 126 // if inserted in first place, move effect control from previous owner to this handle 127 if (i == 0) { 128 bool enabled = false; 129 if (controlHandle != NULL) { 130 enabled = controlHandle->enabled(); 131 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); 132 } 133 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); 134 status = NO_ERROR; 135 } else { 136 status = ALREADY_EXISTS; 137 } 138 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i); 139 mHandles.insertAt(handle, i); 140 return status; 141} 142 143size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle) 144{ 145 Mutex::Autolock _l(mLock); 146 size_t size = mHandles.size(); 147 size_t i; 148 for (i = 0; i < size; i++) { 149 if (mHandles[i] == handle) { 150 break; 151 } 152 } 153 if (i == size) { 154 return size; 155 } 156 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i); 157 158 mHandles.removeAt(i); 159 // if removed from first place, move effect control from this handle to next in line 160 if (i == 0) { 161 EffectHandle *h = controlHandle_l(); 162 if (h != NULL) { 163 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/); 164 } 165 } 166 167 // Prevent calls to process() and other functions on effect interface from now on. 168 // The effect engine will be released by the destructor when the last strong reference on 169 // this object is released which can happen after next process is called. 170 if (mHandles.size() == 0 && !mPinned) { 171 mState = DESTROYED; 172 } 173 174 return mHandles.size(); 175} 176 177// must be called with EffectModule::mLock held 178AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l() 179{ 180 // the first valid handle in the list has control over the module 181 for (size_t i = 0; i < mHandles.size(); i++) { 182 EffectHandle *h = mHandles[i]; 183 if (h != NULL && !h->destroyed_l()) { 184 return h; 185 } 186 } 187 188 return NULL; 189} 190 191size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast) 192{ 193 ALOGV("disconnect() %p handle %p", this, handle); 194 // keep a strong reference on this EffectModule to avoid calling the 195 // destructor before we exit 196 sp<EffectModule> keep(this); 197 { 198 sp<ThreadBase> thread = mThread.promote(); 199 if (thread != 0) { 200 thread->disconnectEffect(keep, handle, unpinIfLast); 201 } 202 } 203 return mHandles.size(); 204} 205 206void AudioFlinger::EffectModule::updateState() { 207 Mutex::Autolock _l(mLock); 208 209 switch (mState) { 210 case RESTART: 211 reset_l(); 212 // FALL THROUGH 213 214 case STARTING: 215 // clear auxiliary effect input buffer for next accumulation 216 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 217 memset(mConfig.inputCfg.buffer.raw, 218 0, 219 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); 220 } 221 if (start_l() == NO_ERROR) { 222 mState = ACTIVE; 223 } else { 224 mState = IDLE; 225 } 226 break; 227 case STOPPING: 228 if (stop_l() == NO_ERROR) { 229 mDisableWaitCnt = mMaxDisableWaitCnt; 230 } else { 231 mDisableWaitCnt = 1; // will cause immediate transition to IDLE 232 } 233 mState = STOPPED; 234 break; 235 case STOPPED: 236 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the 237 // turn off sequence. 238 if (--mDisableWaitCnt == 0) { 239 reset_l(); 240 mState = IDLE; 241 } 242 break; 243 default: //IDLE , ACTIVE, DESTROYED 244 break; 245 } 246} 247 248void AudioFlinger::EffectModule::process() 249{ 250 Mutex::Autolock _l(mLock); 251 252 if (mState == DESTROYED || mEffectInterface == NULL || 253 mConfig.inputCfg.buffer.raw == NULL || 254 mConfig.outputCfg.buffer.raw == NULL) { 255 return; 256 } 257 258 if (isProcessEnabled()) { 259 // do 32 bit to 16 bit conversion for auxiliary effect input buffer 260 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 261 ditherAndClamp(mConfig.inputCfg.buffer.s32, 262 mConfig.inputCfg.buffer.s32, 263 mConfig.inputCfg.buffer.frameCount/2); 264 } 265 266 // do the actual processing in the effect engine 267 int ret = (*mEffectInterface)->process(mEffectInterface, 268 &mConfig.inputCfg.buffer, 269 &mConfig.outputCfg.buffer); 270 271 // force transition to IDLE state when engine is ready 272 if (mState == STOPPED && ret == -ENODATA) { 273 mDisableWaitCnt = 1; 274 } 275 276 // clear auxiliary effect input buffer for next accumulation 277 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 278 memset(mConfig.inputCfg.buffer.raw, 0, 279 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); 280 } 281 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && 282 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { 283 // If an insert effect is idle and input buffer is different from output buffer, 284 // accumulate input onto output 285 sp<EffectChain> chain = mChain.promote(); 286 if (chain != 0 && chain->activeTrackCnt() != 0) { 287 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here 288 int16_t *in = mConfig.inputCfg.buffer.s16; 289 int16_t *out = mConfig.outputCfg.buffer.s16; 290 for (size_t i = 0; i < frameCnt; i++) { 291 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); 292 } 293 } 294 } 295} 296 297void AudioFlinger::EffectModule::reset_l() 298{ 299 if (mStatus != NO_ERROR || mEffectInterface == NULL) { 300 return; 301 } 302 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); 303} 304 305status_t AudioFlinger::EffectModule::configure() 306{ 307 status_t status; 308 sp<ThreadBase> thread; 309 uint32_t size; 310 audio_channel_mask_t channelMask; 311 312 if (mEffectInterface == NULL) { 313 status = NO_INIT; 314 goto exit; 315 } 316 317 thread = mThread.promote(); 318 if (thread == 0) { 319 status = DEAD_OBJECT; 320 goto exit; 321 } 322 323 // TODO: handle configuration of effects replacing track process 324 channelMask = thread->channelMask(); 325 326 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 327 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; 328 } else { 329 mConfig.inputCfg.channels = channelMask; 330 } 331 mConfig.outputCfg.channels = channelMask; 332 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 333 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 334 mConfig.inputCfg.samplingRate = thread->sampleRate(); 335 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; 336 mConfig.inputCfg.bufferProvider.cookie = NULL; 337 mConfig.inputCfg.bufferProvider.getBuffer = NULL; 338 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; 339 mConfig.outputCfg.bufferProvider.cookie = NULL; 340 mConfig.outputCfg.bufferProvider.getBuffer = NULL; 341 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; 342 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; 343 // Insert effect: 344 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, 345 // always overwrites output buffer: input buffer == output buffer 346 // - in other sessions: 347 // last effect in the chain accumulates in output buffer: input buffer != output buffer 348 // other effect: overwrites output buffer: input buffer == output buffer 349 // Auxiliary effect: 350 // accumulates in output buffer: input buffer != output buffer 351 // Therefore: accumulate <=> input buffer != output buffer 352 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { 353 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; 354 } else { 355 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; 356 } 357 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; 358 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; 359 mConfig.inputCfg.buffer.frameCount = thread->frameCount(); 360 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; 361 362 ALOGV("configure() %p thread %p buffer %p framecount %d", 363 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); 364 365 status_t cmdStatus; 366 size = sizeof(int); 367 status = (*mEffectInterface)->command(mEffectInterface, 368 EFFECT_CMD_SET_CONFIG, 369 sizeof(effect_config_t), 370 &mConfig, 371 &size, 372 &cmdStatus); 373 if (status == 0) { 374 status = cmdStatus; 375 } 376 377 if (status == 0 && 378 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) { 379 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; 380 effect_param_t *p = (effect_param_t *)buf32; 381 382 p->psize = sizeof(uint32_t); 383 p->vsize = sizeof(uint32_t); 384 size = sizeof(int); 385 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY; 386 387 uint32_t latency = 0; 388 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId); 389 if (pbt != NULL) { 390 latency = pbt->latency_l(); 391 } 392 393 *((int32_t *)p->data + 1)= latency; 394 (*mEffectInterface)->command(mEffectInterface, 395 EFFECT_CMD_SET_PARAM, 396 sizeof(effect_param_t) + 8, 397 &buf32, 398 &size, 399 &cmdStatus); 400 } 401 402 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / 403 (1000 * mConfig.outputCfg.buffer.frameCount); 404 405exit: 406 mStatus = status; 407 return status; 408} 409 410status_t AudioFlinger::EffectModule::init() 411{ 412 Mutex::Autolock _l(mLock); 413 if (mEffectInterface == NULL) { 414 return NO_INIT; 415 } 416 status_t cmdStatus; 417 uint32_t size = sizeof(status_t); 418 status_t status = (*mEffectInterface)->command(mEffectInterface, 419 EFFECT_CMD_INIT, 420 0, 421 NULL, 422 &size, 423 &cmdStatus); 424 if (status == 0) { 425 status = cmdStatus; 426 } 427 return status; 428} 429 430status_t AudioFlinger::EffectModule::start() 431{ 432 Mutex::Autolock _l(mLock); 433 return start_l(); 434} 435 436status_t AudioFlinger::EffectModule::start_l() 437{ 438 if (mEffectInterface == NULL) { 439 return NO_INIT; 440 } 441 if (mStatus != NO_ERROR) { 442 return mStatus; 443 } 444 status_t cmdStatus; 445 uint32_t size = sizeof(status_t); 446 status_t status = (*mEffectInterface)->command(mEffectInterface, 447 EFFECT_CMD_ENABLE, 448 0, 449 NULL, 450 &size, 451 &cmdStatus); 452 if (status == 0) { 453 status = cmdStatus; 454 } 455 if (status == 0 && 456 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || 457 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { 458 sp<ThreadBase> thread = mThread.promote(); 459 if (thread != 0) { 460 audio_stream_t *stream = thread->stream(); 461 if (stream != NULL) { 462 stream->add_audio_effect(stream, mEffectInterface); 463 } 464 } 465 } 466 return status; 467} 468 469status_t AudioFlinger::EffectModule::stop() 470{ 471 Mutex::Autolock _l(mLock); 472 return stop_l(); 473} 474 475status_t AudioFlinger::EffectModule::stop_l() 476{ 477 if (mEffectInterface == NULL) { 478 return NO_INIT; 479 } 480 if (mStatus != NO_ERROR) { 481 return mStatus; 482 } 483 status_t cmdStatus = NO_ERROR; 484 uint32_t size = sizeof(status_t); 485 status_t status = (*mEffectInterface)->command(mEffectInterface, 486 EFFECT_CMD_DISABLE, 487 0, 488 NULL, 489 &size, 490 &cmdStatus); 491 if (status == NO_ERROR) { 492 status = cmdStatus; 493 } 494 if (status == NO_ERROR) { 495 status = remove_effect_from_hal_l(); 496 } 497 return status; 498} 499 500status_t AudioFlinger::EffectModule::remove_effect_from_hal_l() 501{ 502 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || 503 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { 504 sp<ThreadBase> thread = mThread.promote(); 505 if (thread != 0) { 506 audio_stream_t *stream = thread->stream(); 507 if (stream != NULL) { 508 stream->remove_audio_effect(stream, mEffectInterface); 509 } 510 } 511 } 512 return NO_ERROR; 513} 514 515status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, 516 uint32_t cmdSize, 517 void *pCmdData, 518 uint32_t *replySize, 519 void *pReplyData) 520{ 521 Mutex::Autolock _l(mLock); 522 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); 523 524 if (mState == DESTROYED || mEffectInterface == NULL) { 525 return NO_INIT; 526 } 527 if (mStatus != NO_ERROR) { 528 return mStatus; 529 } 530 status_t status = (*mEffectInterface)->command(mEffectInterface, 531 cmdCode, 532 cmdSize, 533 pCmdData, 534 replySize, 535 pReplyData); 536 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { 537 uint32_t size = (replySize == NULL) ? 0 : *replySize; 538 for (size_t i = 1; i < mHandles.size(); i++) { 539 EffectHandle *h = mHandles[i]; 540 if (h != NULL && !h->destroyed_l()) { 541 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); 542 } 543 } 544 } 545 return status; 546} 547 548status_t AudioFlinger::EffectModule::setEnabled(bool enabled) 549{ 550 Mutex::Autolock _l(mLock); 551 return setEnabled_l(enabled); 552} 553 554// must be called with EffectModule::mLock held 555status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled) 556{ 557 558 ALOGV("setEnabled %p enabled %d", this, enabled); 559 560 if (enabled != isEnabled()) { 561 status_t status = AudioSystem::setEffectEnabled(mId, enabled); 562 if (enabled && status != NO_ERROR) { 563 return status; 564 } 565 566 switch (mState) { 567 // going from disabled to enabled 568 case IDLE: 569 mState = STARTING; 570 break; 571 case STOPPED: 572 mState = RESTART; 573 break; 574 case STOPPING: 575 mState = ACTIVE; 576 break; 577 578 // going from enabled to disabled 579 case RESTART: 580 mState = STOPPED; 581 break; 582 case STARTING: 583 mState = IDLE; 584 break; 585 case ACTIVE: 586 mState = STOPPING; 587 break; 588 case DESTROYED: 589 return NO_ERROR; // simply ignore as we are being destroyed 590 } 591 for (size_t i = 1; i < mHandles.size(); i++) { 592 EffectHandle *h = mHandles[i]; 593 if (h != NULL && !h->destroyed_l()) { 594 h->setEnabled(enabled); 595 } 596 } 597 } 598 return NO_ERROR; 599} 600 601bool AudioFlinger::EffectModule::isEnabled() const 602{ 603 switch (mState) { 604 case RESTART: 605 case STARTING: 606 case ACTIVE: 607 return true; 608 case IDLE: 609 case STOPPING: 610 case STOPPED: 611 case DESTROYED: 612 default: 613 return false; 614 } 615} 616 617bool AudioFlinger::EffectModule::isProcessEnabled() const 618{ 619 if (mStatus != NO_ERROR) { 620 return false; 621 } 622 623 switch (mState) { 624 case RESTART: 625 case ACTIVE: 626 case STOPPING: 627 case STOPPED: 628 return true; 629 case IDLE: 630 case STARTING: 631 case DESTROYED: 632 default: 633 return false; 634 } 635} 636 637status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) 638{ 639 Mutex::Autolock _l(mLock); 640 if (mStatus != NO_ERROR) { 641 return mStatus; 642 } 643 status_t status = NO_ERROR; 644 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume 645 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) 646 if (isProcessEnabled() && 647 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || 648 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { 649 status_t cmdStatus; 650 uint32_t volume[2]; 651 uint32_t *pVolume = NULL; 652 uint32_t size = sizeof(volume); 653 volume[0] = *left; 654 volume[1] = *right; 655 if (controller) { 656 pVolume = volume; 657 } 658 status = (*mEffectInterface)->command(mEffectInterface, 659 EFFECT_CMD_SET_VOLUME, 660 size, 661 volume, 662 &size, 663 pVolume); 664 if (controller && status == NO_ERROR && size == sizeof(volume)) { 665 *left = volume[0]; 666 *right = volume[1]; 667 } 668 } 669 return status; 670} 671 672status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device) 673{ 674 if (device == AUDIO_DEVICE_NONE) { 675 return NO_ERROR; 676 } 677 678 Mutex::Autolock _l(mLock); 679 if (mStatus != NO_ERROR) { 680 return mStatus; 681 } 682 status_t status = NO_ERROR; 683 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { 684 status_t cmdStatus; 685 uint32_t size = sizeof(status_t); 686 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE : 687 EFFECT_CMD_SET_INPUT_DEVICE; 688 status = (*mEffectInterface)->command(mEffectInterface, 689 cmd, 690 sizeof(uint32_t), 691 &device, 692 &size, 693 &cmdStatus); 694 } 695 return status; 696} 697 698status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) 699{ 700 Mutex::Autolock _l(mLock); 701 if (mStatus != NO_ERROR) { 702 return mStatus; 703 } 704 status_t status = NO_ERROR; 705 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { 706 status_t cmdStatus; 707 uint32_t size = sizeof(status_t); 708 status = (*mEffectInterface)->command(mEffectInterface, 709 EFFECT_CMD_SET_AUDIO_MODE, 710 sizeof(audio_mode_t), 711 &mode, 712 &size, 713 &cmdStatus); 714 if (status == NO_ERROR) { 715 status = cmdStatus; 716 } 717 } 718 return status; 719} 720 721status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source) 722{ 723 Mutex::Autolock _l(mLock); 724 if (mStatus != NO_ERROR) { 725 return mStatus; 726 } 727 status_t status = NO_ERROR; 728 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) { 729 uint32_t size = 0; 730 status = (*mEffectInterface)->command(mEffectInterface, 731 EFFECT_CMD_SET_AUDIO_SOURCE, 732 sizeof(audio_source_t), 733 &source, 734 &size, 735 NULL); 736 } 737 return status; 738} 739 740void AudioFlinger::EffectModule::setSuspended(bool suspended) 741{ 742 Mutex::Autolock _l(mLock); 743 mSuspended = suspended; 744} 745 746bool AudioFlinger::EffectModule::suspended() const 747{ 748 Mutex::Autolock _l(mLock); 749 return mSuspended; 750} 751 752bool AudioFlinger::EffectModule::purgeHandles() 753{ 754 bool enabled = false; 755 Mutex::Autolock _l(mLock); 756 for (size_t i = 0; i < mHandles.size(); i++) { 757 EffectHandle *handle = mHandles[i]; 758 if (handle != NULL && !handle->destroyed_l()) { 759 handle->effect().clear(); 760 if (handle->hasControl()) { 761 enabled = handle->enabled(); 762 } 763 } 764 } 765 return enabled; 766} 767 768status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io) 769{ 770 Mutex::Autolock _l(mLock); 771 if (mStatus != NO_ERROR) { 772 return mStatus; 773 } 774 status_t status = NO_ERROR; 775 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) { 776 status_t cmdStatus; 777 uint32_t size = sizeof(status_t); 778 effect_offload_param_t cmd; 779 780 cmd.isOffload = offloaded; 781 cmd.ioHandle = io; 782 status = (*mEffectInterface)->command(mEffectInterface, 783 EFFECT_CMD_OFFLOAD, 784 sizeof(effect_offload_param_t), 785 &cmd, 786 &size, 787 &cmdStatus); 788 if (status == NO_ERROR) { 789 status = cmdStatus; 790 } 791 mOffloaded = (status == NO_ERROR) ? offloaded : false; 792 } else { 793 if (offloaded) { 794 status = INVALID_OPERATION; 795 } 796 mOffloaded = false; 797 } 798 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status); 799 return status; 800} 801 802bool AudioFlinger::EffectModule::isOffloaded() const 803{ 804 Mutex::Autolock _l(mLock); 805 return mOffloaded; 806} 807 808void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) 809{ 810 const size_t SIZE = 256; 811 char buffer[SIZE]; 812 String8 result; 813 814 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); 815 result.append(buffer); 816 817 bool locked = AudioFlinger::dumpTryLock(mLock); 818 // failed to lock - AudioFlinger is probably deadlocked 819 if (!locked) { 820 result.append("\t\tCould not lock Fx mutex:\n"); 821 } 822 823 result.append("\t\tSession Status State Engine:\n"); 824 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", 825 mSessionId, mStatus, mState, (uint32_t)mEffectInterface); 826 result.append(buffer); 827 828 result.append("\t\tDescriptor:\n"); 829 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", 830 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, 831 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1], 832 mDescriptor.uuid.node[2], 833 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); 834 result.append(buffer); 835 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", 836 mDescriptor.type.timeLow, mDescriptor.type.timeMid, 837 mDescriptor.type.timeHiAndVersion, 838 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1], 839 mDescriptor.type.node[2], 840 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); 841 result.append(buffer); 842 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", 843 mDescriptor.apiVersion, 844 mDescriptor.flags); 845 result.append(buffer); 846 snprintf(buffer, SIZE, "\t\t- name: %s\n", 847 mDescriptor.name); 848 result.append(buffer); 849 snprintf(buffer, SIZE, "\t\t- implementor: %s\n", 850 mDescriptor.implementor); 851 result.append(buffer); 852 853 result.append("\t\t- Input configuration:\n"); 854 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); 855 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", 856 (uint32_t)mConfig.inputCfg.buffer.raw, 857 mConfig.inputCfg.buffer.frameCount, 858 mConfig.inputCfg.samplingRate, 859 mConfig.inputCfg.channels, 860 mConfig.inputCfg.format); 861 result.append(buffer); 862 863 result.append("\t\t- Output configuration:\n"); 864 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); 865 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", 866 (uint32_t)mConfig.outputCfg.buffer.raw, 867 mConfig.outputCfg.buffer.frameCount, 868 mConfig.outputCfg.samplingRate, 869 mConfig.outputCfg.channels, 870 mConfig.outputCfg.format); 871 result.append(buffer); 872 873 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); 874 result.append(buffer); 875 result.append("\t\t\tPid Priority Ctrl Locked client server\n"); 876 for (size_t i = 0; i < mHandles.size(); ++i) { 877 EffectHandle *handle = mHandles[i]; 878 if (handle != NULL && !handle->destroyed_l()) { 879 handle->dump(buffer, SIZE); 880 result.append(buffer); 881 } 882 } 883 884 result.append("\n"); 885 886 write(fd, result.string(), result.length()); 887 888 if (locked) { 889 mLock.unlock(); 890 } 891} 892 893// ---------------------------------------------------------------------------- 894// EffectHandle implementation 895// ---------------------------------------------------------------------------- 896 897#undef LOG_TAG 898#define LOG_TAG "AudioFlinger::EffectHandle" 899 900AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, 901 const sp<AudioFlinger::Client>& client, 902 const sp<IEffectClient>& effectClient, 903 int32_t priority) 904 : BnEffect(), 905 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), 906 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false) 907{ 908 ALOGV("constructor %p", this); 909 910 if (client == 0) { 911 return; 912 } 913 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); 914 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); 915 if (mCblkMemory != 0) { 916 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); 917 918 if (mCblk != NULL) { 919 new(mCblk) effect_param_cblk_t(); 920 mBuffer = (uint8_t *)mCblk + bufOffset; 921 } 922 } else { 923 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + 924 sizeof(effect_param_cblk_t)); 925 return; 926 } 927} 928 929AudioFlinger::EffectHandle::~EffectHandle() 930{ 931 ALOGV("Destructor %p", this); 932 933 if (mEffect == 0) { 934 mDestroyed = true; 935 return; 936 } 937 mEffect->lock(); 938 mDestroyed = true; 939 mEffect->unlock(); 940 disconnect(false); 941} 942 943status_t AudioFlinger::EffectHandle::enable() 944{ 945 ALOGV("enable %p", this); 946 if (!mHasControl) { 947 return INVALID_OPERATION; 948 } 949 if (mEffect == 0) { 950 return DEAD_OBJECT; 951 } 952 953 if (mEnabled) { 954 return NO_ERROR; 955 } 956 957 mEnabled = true; 958 959 sp<ThreadBase> thread = mEffect->thread().promote(); 960 if (thread != 0) { 961 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); 962 } 963 964 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled 965 if (mEffect->suspended()) { 966 return NO_ERROR; 967 } 968 969 status_t status = mEffect->setEnabled(true); 970 if (status != NO_ERROR) { 971 if (thread != 0) { 972 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); 973 } 974 mEnabled = false; 975 } else { 976 if (thread != 0 && !mEffect->isOffloadable()) { 977 if ((thread->type() == ThreadBase::OFFLOAD)) { 978 PlaybackThread *t = (PlaybackThread *)thread.get(); 979 t->invalidateTracks(AUDIO_STREAM_MUSIC); 980 } 981 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { 982 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable(); 983 } 984 } 985 } 986 return status; 987} 988 989status_t AudioFlinger::EffectHandle::disable() 990{ 991 ALOGV("disable %p", this); 992 if (!mHasControl) { 993 return INVALID_OPERATION; 994 } 995 if (mEffect == 0) { 996 return DEAD_OBJECT; 997 } 998 999 if (!mEnabled) { 1000 return NO_ERROR; 1001 } 1002 mEnabled = false; 1003 1004 if (mEffect->suspended()) { 1005 return NO_ERROR; 1006 } 1007 1008 status_t status = mEffect->setEnabled(false); 1009 1010 sp<ThreadBase> thread = mEffect->thread().promote(); 1011 if (thread != 0) { 1012 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); 1013 } 1014 1015 return status; 1016} 1017 1018void AudioFlinger::EffectHandle::disconnect() 1019{ 1020 disconnect(true); 1021} 1022 1023void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) 1024{ 1025 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); 1026 if (mEffect == 0) { 1027 return; 1028 } 1029 // restore suspended effects if the disconnected handle was enabled and the last one. 1030 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) { 1031 sp<ThreadBase> thread = mEffect->thread().promote(); 1032 if (thread != 0) { 1033 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); 1034 } 1035 } 1036 1037 // release sp on module => module destructor can be called now 1038 mEffect.clear(); 1039 if (mClient != 0) { 1040 if (mCblk != NULL) { 1041 // unlike ~TrackBase(), mCblk is never a local new, so don't delete 1042 mCblk->~effect_param_cblk_t(); // destroy our shared-structure. 1043 } 1044 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to 1045 // Client destructor must run with AudioFlinger mutex locked 1046 Mutex::Autolock _l(mClient->audioFlinger()->mLock); 1047 mClient.clear(); 1048 } 1049} 1050 1051status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, 1052 uint32_t cmdSize, 1053 void *pCmdData, 1054 uint32_t *replySize, 1055 void *pReplyData) 1056{ 1057 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", 1058 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); 1059 1060 // only get parameter command is permitted for applications not controlling the effect 1061 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { 1062 return INVALID_OPERATION; 1063 } 1064 if (mEffect == 0) { 1065 return DEAD_OBJECT; 1066 } 1067 if (mClient == 0) { 1068 return INVALID_OPERATION; 1069 } 1070 1071 // handle commands that are not forwarded transparently to effect engine 1072 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { 1073 // No need to trylock() here as this function is executed in the binder thread serving a 1074 // particular client process: no risk to block the whole media server process or mixer 1075 // threads if we are stuck here 1076 Mutex::Autolock _l(mCblk->lock); 1077 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || 1078 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { 1079 mCblk->serverIndex = 0; 1080 mCblk->clientIndex = 0; 1081 return BAD_VALUE; 1082 } 1083 status_t status = NO_ERROR; 1084 while (mCblk->serverIndex < mCblk->clientIndex) { 1085 int reply; 1086 uint32_t rsize = sizeof(int); 1087 int *p = (int *)(mBuffer + mCblk->serverIndex); 1088 int size = *p++; 1089 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { 1090 ALOGW("command(): invalid parameter block size"); 1091 break; 1092 } 1093 effect_param_t *param = (effect_param_t *)p; 1094 if (param->psize == 0 || param->vsize == 0) { 1095 ALOGW("command(): null parameter or value size"); 1096 mCblk->serverIndex += size; 1097 continue; 1098 } 1099 uint32_t psize = sizeof(effect_param_t) + 1100 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + 1101 param->vsize; 1102 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, 1103 psize, 1104 p, 1105 &rsize, 1106 &reply); 1107 // stop at first error encountered 1108 if (ret != NO_ERROR) { 1109 status = ret; 1110 *(int *)pReplyData = reply; 1111 break; 1112 } else if (reply != NO_ERROR) { 1113 *(int *)pReplyData = reply; 1114 break; 1115 } 1116 mCblk->serverIndex += size; 1117 } 1118 mCblk->serverIndex = 0; 1119 mCblk->clientIndex = 0; 1120 return status; 1121 } else if (cmdCode == EFFECT_CMD_ENABLE) { 1122 *(int *)pReplyData = NO_ERROR; 1123 return enable(); 1124 } else if (cmdCode == EFFECT_CMD_DISABLE) { 1125 *(int *)pReplyData = NO_ERROR; 1126 return disable(); 1127 } 1128 1129 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); 1130} 1131 1132void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) 1133{ 1134 ALOGV("setControl %p control %d", this, hasControl); 1135 1136 mHasControl = hasControl; 1137 mEnabled = enabled; 1138 1139 if (signal && mEffectClient != 0) { 1140 mEffectClient->controlStatusChanged(hasControl); 1141 } 1142} 1143 1144void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, 1145 uint32_t cmdSize, 1146 void *pCmdData, 1147 uint32_t replySize, 1148 void *pReplyData) 1149{ 1150 if (mEffectClient != 0) { 1151 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); 1152 } 1153} 1154 1155 1156 1157void AudioFlinger::EffectHandle::setEnabled(bool enabled) 1158{ 1159 if (mEffectClient != 0) { 1160 mEffectClient->enableStatusChanged(enabled); 1161 } 1162} 1163 1164status_t AudioFlinger::EffectHandle::onTransact( 1165 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 1166{ 1167 return BnEffect::onTransact(code, data, reply, flags); 1168} 1169 1170 1171void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) 1172{ 1173 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock); 1174 1175 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", 1176 (mClient == 0) ? getpid_cached : mClient->pid(), 1177 mPriority, 1178 mHasControl, 1179 !locked, 1180 mCblk ? mCblk->clientIndex : 0, 1181 mCblk ? mCblk->serverIndex : 0 1182 ); 1183 1184 if (locked) { 1185 mCblk->lock.unlock(); 1186 } 1187} 1188 1189#undef LOG_TAG 1190#define LOG_TAG "AudioFlinger::EffectChain" 1191 1192AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, 1193 int sessionId) 1194 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), 1195 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), 1196 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) 1197{ 1198 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 1199 if (thread == NULL) { 1200 return; 1201 } 1202 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / 1203 thread->frameCount(); 1204} 1205 1206AudioFlinger::EffectChain::~EffectChain() 1207{ 1208 if (mOwnInBuffer) { 1209 delete mInBuffer; 1210 } 1211 1212} 1213 1214// getEffectFromDesc_l() must be called with ThreadBase::mLock held 1215sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l( 1216 effect_descriptor_t *descriptor) 1217{ 1218 size_t size = mEffects.size(); 1219 1220 for (size_t i = 0; i < size; i++) { 1221 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { 1222 return mEffects[i]; 1223 } 1224 } 1225 return 0; 1226} 1227 1228// getEffectFromId_l() must be called with ThreadBase::mLock held 1229sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) 1230{ 1231 size_t size = mEffects.size(); 1232 1233 for (size_t i = 0; i < size; i++) { 1234 // by convention, return first effect if id provided is 0 (0 is never a valid id) 1235 if (id == 0 || mEffects[i]->id() == id) { 1236 return mEffects[i]; 1237 } 1238 } 1239 return 0; 1240} 1241 1242// getEffectFromType_l() must be called with ThreadBase::mLock held 1243sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( 1244 const effect_uuid_t *type) 1245{ 1246 size_t size = mEffects.size(); 1247 1248 for (size_t i = 0; i < size; i++) { 1249 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { 1250 return mEffects[i]; 1251 } 1252 } 1253 return 0; 1254} 1255 1256void AudioFlinger::EffectChain::clearInputBuffer() 1257{ 1258 Mutex::Autolock _l(mLock); 1259 sp<ThreadBase> thread = mThread.promote(); 1260 if (thread == 0) { 1261 ALOGW("clearInputBuffer(): cannot promote mixer thread"); 1262 return; 1263 } 1264 clearInputBuffer_l(thread); 1265} 1266 1267// Must be called with EffectChain::mLock locked 1268void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread) 1269{ 1270 memset(mInBuffer, 0, thread->frameCount() * thread->frameSize()); 1271} 1272 1273// Must be called with EffectChain::mLock locked 1274void AudioFlinger::EffectChain::process_l() 1275{ 1276 sp<ThreadBase> thread = mThread.promote(); 1277 if (thread == 0) { 1278 ALOGW("process_l(): cannot promote mixer thread"); 1279 return; 1280 } 1281 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || 1282 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); 1283 // never process effects when: 1284 // - on an OFFLOAD thread 1285 // - no more tracks are on the session and the effect tail has been rendered 1286 bool doProcess = (thread->type() != ThreadBase::OFFLOAD); 1287 if (!isGlobalSession) { 1288 bool tracksOnSession = (trackCnt() != 0); 1289 1290 if (!tracksOnSession && mTailBufferCount == 0) { 1291 doProcess = false; 1292 } 1293 1294 if (activeTrackCnt() == 0) { 1295 // if no track is active and the effect tail has not been rendered, 1296 // the input buffer must be cleared here as the mixer process will not do it 1297 if (tracksOnSession || mTailBufferCount > 0) { 1298 clearInputBuffer_l(thread); 1299 if (mTailBufferCount > 0) { 1300 mTailBufferCount--; 1301 } 1302 } 1303 } 1304 } 1305 1306 size_t size = mEffects.size(); 1307 if (doProcess) { 1308 for (size_t i = 0; i < size; i++) { 1309 mEffects[i]->process(); 1310 } 1311 } 1312 for (size_t i = 0; i < size; i++) { 1313 mEffects[i]->updateState(); 1314 } 1315} 1316 1317// addEffect_l() must be called with PlaybackThread::mLock held 1318status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) 1319{ 1320 effect_descriptor_t desc = effect->desc(); 1321 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; 1322 1323 Mutex::Autolock _l(mLock); 1324 effect->setChain(this); 1325 sp<ThreadBase> thread = mThread.promote(); 1326 if (thread == 0) { 1327 return NO_INIT; 1328 } 1329 effect->setThread(thread); 1330 1331 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 1332 // Auxiliary effects are inserted at the beginning of mEffects vector as 1333 // they are processed first and accumulated in chain input buffer 1334 mEffects.insertAt(effect, 0); 1335 1336 // the input buffer for auxiliary effect contains mono samples in 1337 // 32 bit format. This is to avoid saturation in AudoMixer 1338 // accumulation stage. Saturation is done in EffectModule::process() before 1339 // calling the process in effect engine 1340 size_t numSamples = thread->frameCount(); 1341 int32_t *buffer = new int32_t[numSamples]; 1342 memset(buffer, 0, numSamples * sizeof(int32_t)); 1343 effect->setInBuffer((int16_t *)buffer); 1344 // auxiliary effects output samples to chain input buffer for further processing 1345 // by insert effects 1346 effect->setOutBuffer(mInBuffer); 1347 } else { 1348 // Insert effects are inserted at the end of mEffects vector as they are processed 1349 // after track and auxiliary effects. 1350 // Insert effect order as a function of indicated preference: 1351 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if 1352 // another effect is present 1353 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the 1354 // last effect claiming first position 1355 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the 1356 // first effect claiming last position 1357 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last 1358 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is 1359 // already present 1360 1361 size_t size = mEffects.size(); 1362 size_t idx_insert = size; 1363 ssize_t idx_insert_first = -1; 1364 ssize_t idx_insert_last = -1; 1365 1366 for (size_t i = 0; i < size; i++) { 1367 effect_descriptor_t d = mEffects[i]->desc(); 1368 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; 1369 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; 1370 if (iMode == EFFECT_FLAG_TYPE_INSERT) { 1371 // check invalid effect chaining combinations 1372 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || 1373 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { 1374 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", 1375 desc.name, d.name); 1376 return INVALID_OPERATION; 1377 } 1378 // remember position of first insert effect and by default 1379 // select this as insert position for new effect 1380 if (idx_insert == size) { 1381 idx_insert = i; 1382 } 1383 // remember position of last insert effect claiming 1384 // first position 1385 if (iPref == EFFECT_FLAG_INSERT_FIRST) { 1386 idx_insert_first = i; 1387 } 1388 // remember position of first insert effect claiming 1389 // last position 1390 if (iPref == EFFECT_FLAG_INSERT_LAST && 1391 idx_insert_last == -1) { 1392 idx_insert_last = i; 1393 } 1394 } 1395 } 1396 1397 // modify idx_insert from first position if needed 1398 if (insertPref == EFFECT_FLAG_INSERT_LAST) { 1399 if (idx_insert_last != -1) { 1400 idx_insert = idx_insert_last; 1401 } else { 1402 idx_insert = size; 1403 } 1404 } else { 1405 if (idx_insert_first != -1) { 1406 idx_insert = idx_insert_first + 1; 1407 } 1408 } 1409 1410 // always read samples from chain input buffer 1411 effect->setInBuffer(mInBuffer); 1412 1413 // if last effect in the chain, output samples to chain 1414 // output buffer, otherwise to chain input buffer 1415 if (idx_insert == size) { 1416 if (idx_insert != 0) { 1417 mEffects[idx_insert-1]->setOutBuffer(mInBuffer); 1418 mEffects[idx_insert-1]->configure(); 1419 } 1420 effect->setOutBuffer(mOutBuffer); 1421 } else { 1422 effect->setOutBuffer(mInBuffer); 1423 } 1424 mEffects.insertAt(effect, idx_insert); 1425 1426 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, 1427 idx_insert); 1428 } 1429 effect->configure(); 1430 return NO_ERROR; 1431} 1432 1433// removeEffect_l() must be called with PlaybackThread::mLock held 1434size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) 1435{ 1436 Mutex::Autolock _l(mLock); 1437 size_t size = mEffects.size(); 1438 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; 1439 1440 for (size_t i = 0; i < size; i++) { 1441 if (effect == mEffects[i]) { 1442 // calling stop here will remove pre-processing effect from the audio HAL. 1443 // This is safe as we hold the EffectChain mutex which guarantees that we are not in 1444 // the middle of a read from audio HAL 1445 if (mEffects[i]->state() == EffectModule::ACTIVE || 1446 mEffects[i]->state() == EffectModule::STOPPING) { 1447 mEffects[i]->stop(); 1448 } 1449 if (type == EFFECT_FLAG_TYPE_AUXILIARY) { 1450 delete[] effect->inBuffer(); 1451 } else { 1452 if (i == size - 1 && i != 0) { 1453 mEffects[i - 1]->setOutBuffer(mOutBuffer); 1454 mEffects[i - 1]->configure(); 1455 } 1456 } 1457 mEffects.removeAt(i); 1458 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), 1459 this, i); 1460 break; 1461 } 1462 } 1463 1464 return mEffects.size(); 1465} 1466 1467// setDevice_l() must be called with PlaybackThread::mLock held 1468void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device) 1469{ 1470 size_t size = mEffects.size(); 1471 for (size_t i = 0; i < size; i++) { 1472 mEffects[i]->setDevice(device); 1473 } 1474} 1475 1476// setMode_l() must be called with PlaybackThread::mLock held 1477void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) 1478{ 1479 size_t size = mEffects.size(); 1480 for (size_t i = 0; i < size; i++) { 1481 mEffects[i]->setMode(mode); 1482 } 1483} 1484 1485// setAudioSource_l() must be called with PlaybackThread::mLock held 1486void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source) 1487{ 1488 size_t size = mEffects.size(); 1489 for (size_t i = 0; i < size; i++) { 1490 mEffects[i]->setAudioSource(source); 1491 } 1492} 1493 1494// setVolume_l() must be called with PlaybackThread::mLock held 1495bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) 1496{ 1497 uint32_t newLeft = *left; 1498 uint32_t newRight = *right; 1499 bool hasControl = false; 1500 int ctrlIdx = -1; 1501 size_t size = mEffects.size(); 1502 1503 // first update volume controller 1504 for (size_t i = size; i > 0; i--) { 1505 if (mEffects[i - 1]->isProcessEnabled() && 1506 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { 1507 ctrlIdx = i - 1; 1508 hasControl = true; 1509 break; 1510 } 1511 } 1512 1513 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { 1514 if (hasControl) { 1515 *left = mNewLeftVolume; 1516 *right = mNewRightVolume; 1517 } 1518 return hasControl; 1519 } 1520 1521 mVolumeCtrlIdx = ctrlIdx; 1522 mLeftVolume = newLeft; 1523 mRightVolume = newRight; 1524 1525 // second get volume update from volume controller 1526 if (ctrlIdx >= 0) { 1527 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); 1528 mNewLeftVolume = newLeft; 1529 mNewRightVolume = newRight; 1530 } 1531 // then indicate volume to all other effects in chain. 1532 // Pass altered volume to effects before volume controller 1533 // and requested volume to effects after controller 1534 uint32_t lVol = newLeft; 1535 uint32_t rVol = newRight; 1536 1537 for (size_t i = 0; i < size; i++) { 1538 if ((int)i == ctrlIdx) { 1539 continue; 1540 } 1541 // this also works for ctrlIdx == -1 when there is no volume controller 1542 if ((int)i > ctrlIdx) { 1543 lVol = *left; 1544 rVol = *right; 1545 } 1546 mEffects[i]->setVolume(&lVol, &rVol, false); 1547 } 1548 *left = newLeft; 1549 *right = newRight; 1550 1551 return hasControl; 1552} 1553 1554void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) 1555{ 1556 const size_t SIZE = 256; 1557 char buffer[SIZE]; 1558 String8 result; 1559 1560 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); 1561 result.append(buffer); 1562 1563 bool locked = AudioFlinger::dumpTryLock(mLock); 1564 // failed to lock - AudioFlinger is probably deadlocked 1565 if (!locked) { 1566 result.append("\tCould not lock mutex:\n"); 1567 } 1568 1569 result.append("\tNum fx In buffer Out buffer Active tracks:\n"); 1570 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", 1571 mEffects.size(), 1572 (uint32_t)mInBuffer, 1573 (uint32_t)mOutBuffer, 1574 mActiveTrackCnt); 1575 result.append(buffer); 1576 write(fd, result.string(), result.size()); 1577 1578 for (size_t i = 0; i < mEffects.size(); ++i) { 1579 sp<EffectModule> effect = mEffects[i]; 1580 if (effect != 0) { 1581 effect->dump(fd, args); 1582 } 1583 } 1584 1585 if (locked) { 1586 mLock.unlock(); 1587 } 1588} 1589 1590// must be called with ThreadBase::mLock held 1591void AudioFlinger::EffectChain::setEffectSuspended_l( 1592 const effect_uuid_t *type, bool suspend) 1593{ 1594 sp<SuspendedEffectDesc> desc; 1595 // use effect type UUID timelow as key as there is no real risk of identical 1596 // timeLow fields among effect type UUIDs. 1597 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); 1598 if (suspend) { 1599 if (index >= 0) { 1600 desc = mSuspendedEffects.valueAt(index); 1601 } else { 1602 desc = new SuspendedEffectDesc(); 1603 desc->mType = *type; 1604 mSuspendedEffects.add(type->timeLow, desc); 1605 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); 1606 } 1607 if (desc->mRefCount++ == 0) { 1608 sp<EffectModule> effect = getEffectIfEnabled(type); 1609 if (effect != 0) { 1610 desc->mEffect = effect; 1611 effect->setSuspended(true); 1612 effect->setEnabled(false); 1613 } 1614 } 1615 } else { 1616 if (index < 0) { 1617 return; 1618 } 1619 desc = mSuspendedEffects.valueAt(index); 1620 if (desc->mRefCount <= 0) { 1621 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); 1622 desc->mRefCount = 1; 1623 } 1624 if (--desc->mRefCount == 0) { 1625 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); 1626 if (desc->mEffect != 0) { 1627 sp<EffectModule> effect = desc->mEffect.promote(); 1628 if (effect != 0) { 1629 effect->setSuspended(false); 1630 effect->lock(); 1631 EffectHandle *handle = effect->controlHandle_l(); 1632 if (handle != NULL && !handle->destroyed_l()) { 1633 effect->setEnabled_l(handle->enabled()); 1634 } 1635 effect->unlock(); 1636 } 1637 desc->mEffect.clear(); 1638 } 1639 mSuspendedEffects.removeItemsAt(index); 1640 } 1641 } 1642} 1643 1644// must be called with ThreadBase::mLock held 1645void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) 1646{ 1647 sp<SuspendedEffectDesc> desc; 1648 1649 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); 1650 if (suspend) { 1651 if (index >= 0) { 1652 desc = mSuspendedEffects.valueAt(index); 1653 } else { 1654 desc = new SuspendedEffectDesc(); 1655 mSuspendedEffects.add((int)kKeyForSuspendAll, desc); 1656 ALOGV("setEffectSuspendedAll_l() add entry for 0"); 1657 } 1658 if (desc->mRefCount++ == 0) { 1659 Vector< sp<EffectModule> > effects; 1660 getSuspendEligibleEffects(effects); 1661 for (size_t i = 0; i < effects.size(); i++) { 1662 setEffectSuspended_l(&effects[i]->desc().type, true); 1663 } 1664 } 1665 } else { 1666 if (index < 0) { 1667 return; 1668 } 1669 desc = mSuspendedEffects.valueAt(index); 1670 if (desc->mRefCount <= 0) { 1671 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); 1672 desc->mRefCount = 1; 1673 } 1674 if (--desc->mRefCount == 0) { 1675 Vector<const effect_uuid_t *> types; 1676 for (size_t i = 0; i < mSuspendedEffects.size(); i++) { 1677 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { 1678 continue; 1679 } 1680 types.add(&mSuspendedEffects.valueAt(i)->mType); 1681 } 1682 for (size_t i = 0; i < types.size(); i++) { 1683 setEffectSuspended_l(types[i], false); 1684 } 1685 ALOGV("setEffectSuspendedAll_l() remove entry for %08x", 1686 mSuspendedEffects.keyAt(index)); 1687 mSuspendedEffects.removeItem((int)kKeyForSuspendAll); 1688 } 1689 } 1690} 1691 1692 1693// The volume effect is used for automated tests only 1694#ifndef OPENSL_ES_H_ 1695static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, 1696 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; 1697const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; 1698#endif //OPENSL_ES_H_ 1699 1700bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) 1701{ 1702 // auxiliary effects and visualizer are never suspended on output mix 1703 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && 1704 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || 1705 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || 1706 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { 1707 return false; 1708 } 1709 return true; 1710} 1711 1712void AudioFlinger::EffectChain::getSuspendEligibleEffects( 1713 Vector< sp<AudioFlinger::EffectModule> > &effects) 1714{ 1715 effects.clear(); 1716 for (size_t i = 0; i < mEffects.size(); i++) { 1717 if (isEffectEligibleForSuspend(mEffects[i]->desc())) { 1718 effects.add(mEffects[i]); 1719 } 1720 } 1721} 1722 1723sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( 1724 const effect_uuid_t *type) 1725{ 1726 sp<EffectModule> effect = getEffectFromType_l(type); 1727 return effect != 0 && effect->isEnabled() ? effect : 0; 1728} 1729 1730void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 1731 bool enabled) 1732{ 1733 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); 1734 if (enabled) { 1735 if (index < 0) { 1736 // if the effect is not suspend check if all effects are suspended 1737 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); 1738 if (index < 0) { 1739 return; 1740 } 1741 if (!isEffectEligibleForSuspend(effect->desc())) { 1742 return; 1743 } 1744 setEffectSuspended_l(&effect->desc().type, enabled); 1745 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); 1746 if (index < 0) { 1747 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); 1748 return; 1749 } 1750 } 1751 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", 1752 effect->desc().type.timeLow); 1753 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); 1754 // if effect is requested to suspended but was not yet enabled, supend it now. 1755 if (desc->mEffect == 0) { 1756 desc->mEffect = effect; 1757 effect->setEnabled(false); 1758 effect->setSuspended(true); 1759 } 1760 } else { 1761 if (index < 0) { 1762 return; 1763 } 1764 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", 1765 effect->desc().type.timeLow); 1766 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); 1767 desc->mEffect.clear(); 1768 effect->setSuspended(false); 1769 } 1770} 1771 1772bool AudioFlinger::EffectChain::isNonOffloadableEnabled() 1773{ 1774 Mutex::Autolock _l(mLock); 1775 size_t size = mEffects.size(); 1776 for (size_t i = 0; i < size; i++) { 1777 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) { 1778 return true; 1779 } 1780 } 1781 return false; 1782} 1783 1784}; // namespace android 1785