AudioFlinger.cpp revision 2c3b2da3049627264b7c6b449a1622f002210f03
1/* 2** 3** Copyright 2007, 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 <math.h> 23#include <signal.h> 24#include <sys/time.h> 25#include <sys/resource.h> 26 27#include <binder/IPCThreadState.h> 28#include <binder/IServiceManager.h> 29#include <utils/Log.h> 30#include <utils/Trace.h> 31#include <binder/Parcel.h> 32#include <binder/IPCThreadState.h> 33#include <utils/String16.h> 34#include <utils/threads.h> 35#include <utils/Atomic.h> 36 37#include <cutils/bitops.h> 38#include <cutils/properties.h> 39#include <cutils/compiler.h> 40 41#undef ADD_BATTERY_DATA 42 43#ifdef ADD_BATTERY_DATA 44#include <media/IMediaPlayerService.h> 45#include <media/IMediaDeathNotifier.h> 46#endif 47 48#include <private/media/AudioTrackShared.h> 49#include <private/media/AudioEffectShared.h> 50 51#include <system/audio.h> 52#include <hardware/audio.h> 53 54#include "AudioMixer.h" 55#include "AudioFlinger.h" 56#include "ServiceUtilities.h" 57 58#include <media/EffectsFactoryApi.h> 59#include <audio_effects/effect_visualizer.h> 60#include <audio_effects/effect_ns.h> 61#include <audio_effects/effect_aec.h> 62 63#include <audio_utils/primitives.h> 64 65#include <powermanager/PowerManager.h> 66 67// #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds 68#ifdef DEBUG_CPU_USAGE 69#include <cpustats/CentralTendencyStatistics.h> 70#include <cpustats/ThreadCpuUsage.h> 71#endif 72 73#include <common_time/cc_helper.h> 74#include <common_time/local_clock.h> 75 76#include "FastMixer.h" 77 78// NBAIO implementations 79#include "AudioStreamOutSink.h" 80#include "MonoPipe.h" 81#include "MonoPipeReader.h" 82#include "Pipe.h" 83#include "PipeReader.h" 84#include "SourceAudioBufferProvider.h" 85 86#include "SchedulingPolicyService.h" 87 88// ---------------------------------------------------------------------------- 89 90// Note: the following macro is used for extremely verbose logging message. In 91// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to 92// 0; but one side effect of this is to turn all LOGV's as well. Some messages 93// are so verbose that we want to suppress them even when we have ALOG_ASSERT 94// turned on. Do not uncomment the #def below unless you really know what you 95// are doing and want to see all of the extremely verbose messages. 96//#define VERY_VERY_VERBOSE_LOGGING 97#ifdef VERY_VERY_VERBOSE_LOGGING 98#define ALOGVV ALOGV 99#else 100#define ALOGVV(a...) do { } while(0) 101#endif 102 103namespace android { 104 105static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; 106static const char kHardwareLockedString[] = "Hardware lock is taken\n"; 107 108static const float MAX_GAIN = 4096.0f; 109static const uint32_t MAX_GAIN_INT = 0x1000; 110 111// retry counts for buffer fill timeout 112// 50 * ~20msecs = 1 second 113static const int8_t kMaxTrackRetries = 50; 114static const int8_t kMaxTrackStartupRetries = 50; 115// allow less retry attempts on direct output thread. 116// direct outputs can be a scarce resource in audio hardware and should 117// be released as quickly as possible. 118static const int8_t kMaxTrackRetriesDirect = 2; 119 120static const int kDumpLockRetries = 50; 121static const int kDumpLockSleepUs = 20000; 122 123// don't warn about blocked writes or record buffer overflows more often than this 124static const nsecs_t kWarningThrottleNs = seconds(5); 125 126// RecordThread loop sleep time upon application overrun or audio HAL read error 127static const int kRecordThreadSleepUs = 5000; 128 129// maximum time to wait for setParameters to complete 130static const nsecs_t kSetParametersTimeoutNs = seconds(2); 131 132// minimum sleep time for the mixer thread loop when tracks are active but in underrun 133static const uint32_t kMinThreadSleepTimeUs = 5000; 134// maximum divider applied to the active sleep time in the mixer thread loop 135static const uint32_t kMaxThreadSleepTimeShift = 2; 136 137// minimum normal mix buffer size, expressed in milliseconds rather than frames 138static const uint32_t kMinNormalMixBufferSizeMs = 20; 139// maximum normal mix buffer size 140static const uint32_t kMaxNormalMixBufferSizeMs = 24; 141 142nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; 143 144// Whether to use fast mixer 145static const enum { 146 FastMixer_Never, // never initialize or use: for debugging only 147 FastMixer_Always, // always initialize and use, even if not needed: for debugging only 148 // normal mixer multiplier is 1 149 FastMixer_Static, // initialize if needed, then use all the time if initialized, 150 // multiplier is calculated based on min & max normal mixer buffer size 151 FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, 152 // multiplier is calculated based on min & max normal mixer buffer size 153 // FIXME for FastMixer_Dynamic: 154 // Supporting this option will require fixing HALs that can't handle large writes. 155 // For example, one HAL implementation returns an error from a large write, 156 // and another HAL implementation corrupts memory, possibly in the sample rate converter. 157 // We could either fix the HAL implementations, or provide a wrapper that breaks 158 // up large writes into smaller ones, and the wrapper would need to deal with scheduler. 159} kUseFastMixer = FastMixer_Static; 160 161static uint32_t gScreenState; // incremented by 2 when screen state changes, bit 0 == 1 means "off" 162 // AudioFlinger::setParameters() updates, other threads read w/o lock 163 164// Priorities for requestPriority 165static const int kPriorityAudioApp = 2; 166static const int kPriorityFastMixer = 3; 167 168// ---------------------------------------------------------------------------- 169 170#ifdef ADD_BATTERY_DATA 171// To collect the amplifier usage 172static void addBatteryData(uint32_t params) { 173 sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); 174 if (service == NULL) { 175 // it already logged 176 return; 177 } 178 179 service->addBatteryData(params); 180} 181#endif 182 183static int load_audio_interface(const char *if_name, audio_hw_device_t **dev) 184{ 185 const hw_module_t *mod; 186 int rc; 187 188 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); 189 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, 190 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 191 if (rc) { 192 goto out; 193 } 194 rc = audio_hw_device_open(mod, dev); 195 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, 196 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 197 if (rc) { 198 goto out; 199 } 200 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { 201 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); 202 rc = BAD_VALUE; 203 goto out; 204 } 205 return 0; 206 207out: 208 *dev = NULL; 209 return rc; 210} 211 212// ---------------------------------------------------------------------------- 213 214AudioFlinger::AudioFlinger() 215 : BnAudioFlinger(), 216 mPrimaryHardwareDev(NULL), 217 mHardwareStatus(AUDIO_HW_IDLE), 218 mMasterVolume(1.0f), 219 mMasterMute(false), 220 mNextUniqueId(1), 221 mMode(AUDIO_MODE_INVALID), 222 mBtNrecIsOff(false) 223{ 224} 225 226void AudioFlinger::onFirstRef() 227{ 228 int rc = 0; 229 230 Mutex::Autolock _l(mLock); 231 232 /* TODO: move all this work into an Init() function */ 233 char val_str[PROPERTY_VALUE_MAX] = { 0 }; 234 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { 235 uint32_t int_val; 236 if (1 == sscanf(val_str, "%u", &int_val)) { 237 mStandbyTimeInNsecs = milliseconds(int_val); 238 ALOGI("Using %u mSec as standby time.", int_val); 239 } else { 240 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; 241 ALOGI("Using default %u mSec as standby time.", 242 (uint32_t)(mStandbyTimeInNsecs / 1000000)); 243 } 244 } 245 246 mMode = AUDIO_MODE_NORMAL; 247} 248 249AudioFlinger::~AudioFlinger() 250{ 251 while (!mRecordThreads.isEmpty()) { 252 // closeInput() will remove first entry from mRecordThreads 253 closeInput_nonvirtual(mRecordThreads.keyAt(0)); 254 } 255 while (!mPlaybackThreads.isEmpty()) { 256 // closeOutput() will remove first entry from mPlaybackThreads 257 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); 258 } 259 260 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 261 // no mHardwareLock needed, as there are no other references to this 262 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); 263 delete mAudioHwDevs.valueAt(i); 264 } 265} 266 267static const char * const audio_interfaces[] = { 268 AUDIO_HARDWARE_MODULE_ID_PRIMARY, 269 AUDIO_HARDWARE_MODULE_ID_A2DP, 270 AUDIO_HARDWARE_MODULE_ID_USB, 271}; 272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) 273 274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l( 275 audio_module_handle_t module, 276 audio_devices_t devices) 277{ 278 // if module is 0, the request comes from an old policy manager and we should load 279 // well known modules 280 if (module == 0) { 281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); 282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { 283 loadHwModule_l(audio_interfaces[i]); 284 } 285 } else { 286 // check a match for the requested module handle 287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); 288 if (audioHwDevice != NULL) { 289 return audioHwDevice; 290 } 291 } 292 // then try to find a module supporting the requested device. 293 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 294 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); 295 audio_hw_device_t *dev = audioHwDevice->hwDevice(); 296 if ((dev->get_supported_devices(dev) & devices) == devices) 297 return audioHwDevice; 298 } 299 300 return NULL; 301} 302 303void AudioFlinger::dumpClients(int fd, const Vector<String16>& args) 304{ 305 const size_t SIZE = 256; 306 char buffer[SIZE]; 307 String8 result; 308 309 result.append("Clients:\n"); 310 for (size_t i = 0; i < mClients.size(); ++i) { 311 sp<Client> client = mClients.valueAt(i).promote(); 312 if (client != 0) { 313 snprintf(buffer, SIZE, " pid: %d\n", client->pid()); 314 result.append(buffer); 315 } 316 } 317 318 result.append("Global session refs:\n"); 319 result.append(" session pid count\n"); 320 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { 321 AudioSessionRef *r = mAudioSessionRefs[i]; 322 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt); 323 result.append(buffer); 324 } 325 write(fd, result.string(), result.size()); 326} 327 328 329void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) 330{ 331 const size_t SIZE = 256; 332 char buffer[SIZE]; 333 String8 result; 334 hardware_call_state hardwareStatus = mHardwareStatus; 335 336 snprintf(buffer, SIZE, "Hardware status: %d\n" 337 "Standby Time mSec: %u\n", 338 hardwareStatus, 339 (uint32_t)(mStandbyTimeInNsecs / 1000000)); 340 result.append(buffer); 341 write(fd, result.string(), result.size()); 342} 343 344void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) 345{ 346 const size_t SIZE = 256; 347 char buffer[SIZE]; 348 String8 result; 349 snprintf(buffer, SIZE, "Permission Denial: " 350 "can't dump AudioFlinger from pid=%d, uid=%d\n", 351 IPCThreadState::self()->getCallingPid(), 352 IPCThreadState::self()->getCallingUid()); 353 result.append(buffer); 354 write(fd, result.string(), result.size()); 355} 356 357static bool tryLock(Mutex& mutex) 358{ 359 bool locked = false; 360 for (int i = 0; i < kDumpLockRetries; ++i) { 361 if (mutex.tryLock() == NO_ERROR) { 362 locked = true; 363 break; 364 } 365 usleep(kDumpLockSleepUs); 366 } 367 return locked; 368} 369 370status_t AudioFlinger::dump(int fd, const Vector<String16>& args) 371{ 372 if (!dumpAllowed()) { 373 dumpPermissionDenial(fd, args); 374 } else { 375 // get state of hardware lock 376 bool hardwareLocked = tryLock(mHardwareLock); 377 if (!hardwareLocked) { 378 String8 result(kHardwareLockedString); 379 write(fd, result.string(), result.size()); 380 } else { 381 mHardwareLock.unlock(); 382 } 383 384 bool locked = tryLock(mLock); 385 386 // failed to lock - AudioFlinger is probably deadlocked 387 if (!locked) { 388 String8 result(kDeadlockedString); 389 write(fd, result.string(), result.size()); 390 } 391 392 dumpClients(fd, args); 393 dumpInternals(fd, args); 394 395 // dump playback threads 396 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 397 mPlaybackThreads.valueAt(i)->dump(fd, args); 398 } 399 400 // dump record threads 401 for (size_t i = 0; i < mRecordThreads.size(); i++) { 402 mRecordThreads.valueAt(i)->dump(fd, args); 403 } 404 405 // dump all hardware devs 406 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 407 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 408 dev->dump(dev, fd); 409 } 410 if (locked) mLock.unlock(); 411 } 412 return NO_ERROR; 413} 414 415sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) 416{ 417 // If pid is already in the mClients wp<> map, then use that entry 418 // (for which promote() is always != 0), otherwise create a new entry and Client. 419 sp<Client> client = mClients.valueFor(pid).promote(); 420 if (client == 0) { 421 client = new Client(this, pid); 422 mClients.add(pid, client); 423 } 424 425 return client; 426} 427 428// IAudioFlinger interface 429 430 431sp<IAudioTrack> AudioFlinger::createTrack( 432 pid_t pid, 433 audio_stream_type_t streamType, 434 uint32_t sampleRate, 435 audio_format_t format, 436 audio_channel_mask_t channelMask, 437 int frameCount, 438 IAudioFlinger::track_flags_t flags, 439 const sp<IMemory>& sharedBuffer, 440 audio_io_handle_t output, 441 pid_t tid, 442 int *sessionId, 443 status_t *status) 444{ 445 sp<PlaybackThread::Track> track; 446 sp<TrackHandle> trackHandle; 447 sp<Client> client; 448 status_t lStatus; 449 int lSessionId; 450 451 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, 452 // but if someone uses binder directly they could bypass that and cause us to crash 453 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { 454 ALOGE("createTrack() invalid stream type %d", streamType); 455 lStatus = BAD_VALUE; 456 goto Exit; 457 } 458 459 { 460 Mutex::Autolock _l(mLock); 461 PlaybackThread *thread = checkPlaybackThread_l(output); 462 PlaybackThread *effectThread = NULL; 463 if (thread == NULL) { 464 ALOGE("unknown output thread"); 465 lStatus = BAD_VALUE; 466 goto Exit; 467 } 468 469 client = registerPid_l(pid); 470 471 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); 472 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { 473 // check if an effect chain with the same session ID is present on another 474 // output thread and move it here. 475 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 476 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 477 if (mPlaybackThreads.keyAt(i) != output) { 478 uint32_t sessions = t->hasAudioSession(*sessionId); 479 if (sessions & PlaybackThread::EFFECT_SESSION) { 480 effectThread = t.get(); 481 break; 482 } 483 } 484 } 485 lSessionId = *sessionId; 486 } else { 487 // if no audio session id is provided, create one here 488 lSessionId = nextUniqueId(); 489 if (sessionId != NULL) { 490 *sessionId = lSessionId; 491 } 492 } 493 ALOGV("createTrack() lSessionId: %d", lSessionId); 494 495 track = thread->createTrack_l(client, streamType, sampleRate, format, 496 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus); 497 498 // move effect chain to this output thread if an effect on same session was waiting 499 // for a track to be created 500 if (lStatus == NO_ERROR && effectThread != NULL) { 501 Mutex::Autolock _dl(thread->mLock); 502 Mutex::Autolock _sl(effectThread->mLock); 503 moveEffectChain_l(lSessionId, effectThread, thread, true); 504 } 505 506 // Look for sync events awaiting for a session to be used. 507 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) { 508 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { 509 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { 510 if (lStatus == NO_ERROR) { 511 track->setSyncEvent(mPendingSyncEvents[i]); 512 } else { 513 mPendingSyncEvents[i]->cancel(); 514 } 515 mPendingSyncEvents.removeAt(i); 516 i--; 517 } 518 } 519 } 520 } 521 if (lStatus == NO_ERROR) { 522 trackHandle = new TrackHandle(track); 523 } else { 524 // remove local strong reference to Client before deleting the Track so that the Client 525 // destructor is called by the TrackBase destructor with mLock held 526 client.clear(); 527 track.clear(); 528 } 529 530Exit: 531 if (status != NULL) { 532 *status = lStatus; 533 } 534 return trackHandle; 535} 536 537uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const 538{ 539 Mutex::Autolock _l(mLock); 540 PlaybackThread *thread = checkPlaybackThread_l(output); 541 if (thread == NULL) { 542 ALOGW("sampleRate() unknown thread %d", output); 543 return 0; 544 } 545 return thread->sampleRate(); 546} 547 548int AudioFlinger::channelCount(audio_io_handle_t output) const 549{ 550 Mutex::Autolock _l(mLock); 551 PlaybackThread *thread = checkPlaybackThread_l(output); 552 if (thread == NULL) { 553 ALOGW("channelCount() unknown thread %d", output); 554 return 0; 555 } 556 return thread->channelCount(); 557} 558 559audio_format_t AudioFlinger::format(audio_io_handle_t output) const 560{ 561 Mutex::Autolock _l(mLock); 562 PlaybackThread *thread = checkPlaybackThread_l(output); 563 if (thread == NULL) { 564 ALOGW("format() unknown thread %d", output); 565 return AUDIO_FORMAT_INVALID; 566 } 567 return thread->format(); 568} 569 570size_t AudioFlinger::frameCount(audio_io_handle_t output) const 571{ 572 Mutex::Autolock _l(mLock); 573 PlaybackThread *thread = checkPlaybackThread_l(output); 574 if (thread == NULL) { 575 ALOGW("frameCount() unknown thread %d", output); 576 return 0; 577 } 578 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; 579 // should examine all callers and fix them to handle smaller counts 580 return thread->frameCount(); 581} 582 583uint32_t AudioFlinger::latency(audio_io_handle_t output) const 584{ 585 Mutex::Autolock _l(mLock); 586 PlaybackThread *thread = checkPlaybackThread_l(output); 587 if (thread == NULL) { 588 ALOGW("latency() unknown thread %d", output); 589 return 0; 590 } 591 return thread->latency(); 592} 593 594status_t AudioFlinger::setMasterVolume(float value) 595{ 596 status_t ret = initCheck(); 597 if (ret != NO_ERROR) { 598 return ret; 599 } 600 601 // check calling permissions 602 if (!settingsAllowed()) { 603 return PERMISSION_DENIED; 604 } 605 606 Mutex::Autolock _l(mLock); 607 mMasterVolume = value; 608 609 // Set master volume in the HALs which support it. 610 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 611 AutoMutex lock(mHardwareLock); 612 AudioHwDevice *dev = mAudioHwDevs.valueAt(i); 613 614 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; 615 if (dev->canSetMasterVolume()) { 616 dev->hwDevice()->set_master_volume(dev->hwDevice(), value); 617 } 618 mHardwareStatus = AUDIO_HW_IDLE; 619 } 620 621 // Now set the master volume in each playback thread. Playback threads 622 // assigned to HALs which do not have master volume support will apply 623 // master volume during the mix operation. Threads with HALs which do 624 // support master volume will simply ignore the setting. 625 for (size_t i = 0; i < mPlaybackThreads.size(); i++) 626 mPlaybackThreads.valueAt(i)->setMasterVolume(value); 627 628 return NO_ERROR; 629} 630 631status_t AudioFlinger::setMode(audio_mode_t mode) 632{ 633 status_t ret = initCheck(); 634 if (ret != NO_ERROR) { 635 return ret; 636 } 637 638 // check calling permissions 639 if (!settingsAllowed()) { 640 return PERMISSION_DENIED; 641 } 642 if (uint32_t(mode) >= AUDIO_MODE_CNT) { 643 ALOGW("Illegal value: setMode(%d)", mode); 644 return BAD_VALUE; 645 } 646 647 { // scope for the lock 648 AutoMutex lock(mHardwareLock); 649 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 650 mHardwareStatus = AUDIO_HW_SET_MODE; 651 ret = dev->set_mode(dev, mode); 652 mHardwareStatus = AUDIO_HW_IDLE; 653 } 654 655 if (NO_ERROR == ret) { 656 Mutex::Autolock _l(mLock); 657 mMode = mode; 658 for (size_t i = 0; i < mPlaybackThreads.size(); i++) 659 mPlaybackThreads.valueAt(i)->setMode(mode); 660 } 661 662 return ret; 663} 664 665status_t AudioFlinger::setMicMute(bool state) 666{ 667 status_t ret = initCheck(); 668 if (ret != NO_ERROR) { 669 return ret; 670 } 671 672 // check calling permissions 673 if (!settingsAllowed()) { 674 return PERMISSION_DENIED; 675 } 676 677 AutoMutex lock(mHardwareLock); 678 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 679 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; 680 ret = dev->set_mic_mute(dev, state); 681 mHardwareStatus = AUDIO_HW_IDLE; 682 return ret; 683} 684 685bool AudioFlinger::getMicMute() const 686{ 687 status_t ret = initCheck(); 688 if (ret != NO_ERROR) { 689 return false; 690 } 691 692 bool state = AUDIO_MODE_INVALID; 693 AutoMutex lock(mHardwareLock); 694 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 695 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; 696 dev->get_mic_mute(dev, &state); 697 mHardwareStatus = AUDIO_HW_IDLE; 698 return state; 699} 700 701status_t AudioFlinger::setMasterMute(bool muted) 702{ 703 status_t ret = initCheck(); 704 if (ret != NO_ERROR) { 705 return ret; 706 } 707 708 // check calling permissions 709 if (!settingsAllowed()) { 710 return PERMISSION_DENIED; 711 } 712 713 Mutex::Autolock _l(mLock); 714 mMasterMute = muted; 715 716 // Set master mute in the HALs which support it. 717 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 718 AutoMutex lock(mHardwareLock); 719 AudioHwDevice *dev = mAudioHwDevs.valueAt(i); 720 721 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; 722 if (dev->canSetMasterMute()) { 723 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted); 724 } 725 mHardwareStatus = AUDIO_HW_IDLE; 726 } 727 728 // Now set the master mute in each playback thread. Playback threads 729 // assigned to HALs which do not have master mute support will apply master 730 // mute during the mix operation. Threads with HALs which do support master 731 // mute will simply ignore the setting. 732 for (size_t i = 0; i < mPlaybackThreads.size(); i++) 733 mPlaybackThreads.valueAt(i)->setMasterMute(muted); 734 735 return NO_ERROR; 736} 737 738float AudioFlinger::masterVolume() const 739{ 740 Mutex::Autolock _l(mLock); 741 return masterVolume_l(); 742} 743 744bool AudioFlinger::masterMute() const 745{ 746 Mutex::Autolock _l(mLock); 747 return masterMute_l(); 748} 749 750float AudioFlinger::masterVolume_l() const 751{ 752 return mMasterVolume; 753} 754 755bool AudioFlinger::masterMute_l() const 756{ 757 return mMasterMute; 758} 759 760status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, 761 audio_io_handle_t output) 762{ 763 // check calling permissions 764 if (!settingsAllowed()) { 765 return PERMISSION_DENIED; 766 } 767 768 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 769 ALOGE("setStreamVolume() invalid stream %d", stream); 770 return BAD_VALUE; 771 } 772 773 AutoMutex lock(mLock); 774 PlaybackThread *thread = NULL; 775 if (output) { 776 thread = checkPlaybackThread_l(output); 777 if (thread == NULL) { 778 return BAD_VALUE; 779 } 780 } 781 782 mStreamTypes[stream].volume = value; 783 784 if (thread == NULL) { 785 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 786 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); 787 } 788 } else { 789 thread->setStreamVolume(stream, value); 790 } 791 792 return NO_ERROR; 793} 794 795status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) 796{ 797 // check calling permissions 798 if (!settingsAllowed()) { 799 return PERMISSION_DENIED; 800 } 801 802 if (uint32_t(stream) >= AUDIO_STREAM_CNT || 803 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { 804 ALOGE("setStreamMute() invalid stream %d", stream); 805 return BAD_VALUE; 806 } 807 808 AutoMutex lock(mLock); 809 mStreamTypes[stream].mute = muted; 810 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) 811 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); 812 813 return NO_ERROR; 814} 815 816float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const 817{ 818 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 819 return 0.0f; 820 } 821 822 AutoMutex lock(mLock); 823 float volume; 824 if (output) { 825 PlaybackThread *thread = checkPlaybackThread_l(output); 826 if (thread == NULL) { 827 return 0.0f; 828 } 829 volume = thread->streamVolume(stream); 830 } else { 831 volume = streamVolume_l(stream); 832 } 833 834 return volume; 835} 836 837bool AudioFlinger::streamMute(audio_stream_type_t stream) const 838{ 839 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 840 return true; 841 } 842 843 AutoMutex lock(mLock); 844 return streamMute_l(stream); 845} 846 847status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) 848{ 849 ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d", 850 ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); 851 // check calling permissions 852 if (!settingsAllowed()) { 853 return PERMISSION_DENIED; 854 } 855 856 // ioHandle == 0 means the parameters are global to the audio hardware interface 857 if (ioHandle == 0) { 858 Mutex::Autolock _l(mLock); 859 status_t final_result = NO_ERROR; 860 { 861 AutoMutex lock(mHardwareLock); 862 mHardwareStatus = AUDIO_HW_SET_PARAMETER; 863 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 864 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 865 status_t result = dev->set_parameters(dev, keyValuePairs.string()); 866 final_result = result ?: final_result; 867 } 868 mHardwareStatus = AUDIO_HW_IDLE; 869 } 870 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 871 AudioParameter param = AudioParameter(keyValuePairs); 872 String8 value; 873 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { 874 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); 875 if (mBtNrecIsOff != btNrecIsOff) { 876 for (size_t i = 0; i < mRecordThreads.size(); i++) { 877 sp<RecordThread> thread = mRecordThreads.valueAt(i); 878 audio_devices_t device = thread->device() & AUDIO_DEVICE_IN_ALL; 879 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; 880 // collect all of the thread's session IDs 881 KeyedVector<int, bool> ids = thread->sessionIds(); 882 // suspend effects associated with those session IDs 883 for (size_t j = 0; j < ids.size(); ++j) { 884 int sessionId = ids.keyAt(j); 885 thread->setEffectSuspended(FX_IID_AEC, 886 suspend, 887 sessionId); 888 thread->setEffectSuspended(FX_IID_NS, 889 suspend, 890 sessionId); 891 } 892 } 893 mBtNrecIsOff = btNrecIsOff; 894 } 895 } 896 String8 screenState; 897 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { 898 bool isOff = screenState == "off"; 899 if (isOff != (gScreenState & 1)) { 900 gScreenState = ((gScreenState & ~1) + 2) | isOff; 901 } 902 } 903 return final_result; 904 } 905 906 // hold a strong ref on thread in case closeOutput() or closeInput() is called 907 // and the thread is exited once the lock is released 908 sp<ThreadBase> thread; 909 { 910 Mutex::Autolock _l(mLock); 911 thread = checkPlaybackThread_l(ioHandle); 912 if (thread == 0) { 913 thread = checkRecordThread_l(ioHandle); 914 } else if (thread == primaryPlaybackThread_l()) { 915 // indicate output device change to all input threads for pre processing 916 AudioParameter param = AudioParameter(keyValuePairs); 917 int value; 918 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && 919 (value != 0)) { 920 for (size_t i = 0; i < mRecordThreads.size(); i++) { 921 mRecordThreads.valueAt(i)->setParameters(keyValuePairs); 922 } 923 } 924 } 925 } 926 if (thread != 0) { 927 return thread->setParameters(keyValuePairs); 928 } 929 return BAD_VALUE; 930} 931 932String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const 933{ 934// ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d", 935// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); 936 937 Mutex::Autolock _l(mLock); 938 939 if (ioHandle == 0) { 940 String8 out_s8; 941 942 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 943 char *s; 944 { 945 AutoMutex lock(mHardwareLock); 946 mHardwareStatus = AUDIO_HW_GET_PARAMETER; 947 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 948 s = dev->get_parameters(dev, keys.string()); 949 mHardwareStatus = AUDIO_HW_IDLE; 950 } 951 out_s8 += String8(s ? s : ""); 952 free(s); 953 } 954 return out_s8; 955 } 956 957 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); 958 if (playbackThread != NULL) { 959 return playbackThread->getParameters(keys); 960 } 961 RecordThread *recordThread = checkRecordThread_l(ioHandle); 962 if (recordThread != NULL) { 963 return recordThread->getParameters(keys); 964 } 965 return String8(""); 966} 967 968size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, 969 audio_channel_mask_t channelMask) const 970{ 971 status_t ret = initCheck(); 972 if (ret != NO_ERROR) { 973 return 0; 974 } 975 976 AutoMutex lock(mHardwareLock); 977 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; 978 struct audio_config config = { 979 sample_rate: sampleRate, 980 channel_mask: channelMask, 981 format: format, 982 }; 983 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 984 size_t size = dev->get_input_buffer_size(dev, &config); 985 mHardwareStatus = AUDIO_HW_IDLE; 986 return size; 987} 988 989unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const 990{ 991 Mutex::Autolock _l(mLock); 992 993 RecordThread *recordThread = checkRecordThread_l(ioHandle); 994 if (recordThread != NULL) { 995 return recordThread->getInputFramesLost(); 996 } 997 return 0; 998} 999 1000status_t AudioFlinger::setVoiceVolume(float value) 1001{ 1002 status_t ret = initCheck(); 1003 if (ret != NO_ERROR) { 1004 return ret; 1005 } 1006 1007 // check calling permissions 1008 if (!settingsAllowed()) { 1009 return PERMISSION_DENIED; 1010 } 1011 1012 AutoMutex lock(mHardwareLock); 1013 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 1014 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; 1015 ret = dev->set_voice_volume(dev, value); 1016 mHardwareStatus = AUDIO_HW_IDLE; 1017 1018 return ret; 1019} 1020 1021status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, 1022 audio_io_handle_t output) const 1023{ 1024 status_t status; 1025 1026 Mutex::Autolock _l(mLock); 1027 1028 PlaybackThread *playbackThread = checkPlaybackThread_l(output); 1029 if (playbackThread != NULL) { 1030 return playbackThread->getRenderPosition(halFrames, dspFrames); 1031 } 1032 1033 return BAD_VALUE; 1034} 1035 1036void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) 1037{ 1038 1039 Mutex::Autolock _l(mLock); 1040 1041 pid_t pid = IPCThreadState::self()->getCallingPid(); 1042 if (mNotificationClients.indexOfKey(pid) < 0) { 1043 sp<NotificationClient> notificationClient = new NotificationClient(this, 1044 client, 1045 pid); 1046 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); 1047 1048 mNotificationClients.add(pid, notificationClient); 1049 1050 sp<IBinder> binder = client->asBinder(); 1051 binder->linkToDeath(notificationClient); 1052 1053 // the config change is always sent from playback or record threads to avoid deadlock 1054 // with AudioSystem::gLock 1055 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 1056 mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); 1057 } 1058 1059 for (size_t i = 0; i < mRecordThreads.size(); i++) { 1060 mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); 1061 } 1062 } 1063} 1064 1065void AudioFlinger::removeNotificationClient(pid_t pid) 1066{ 1067 Mutex::Autolock _l(mLock); 1068 1069 mNotificationClients.removeItem(pid); 1070 1071 ALOGV("%d died, releasing its sessions", pid); 1072 size_t num = mAudioSessionRefs.size(); 1073 bool removed = false; 1074 for (size_t i = 0; i< num; ) { 1075 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); 1076 ALOGV(" pid %d @ %d", ref->mPid, i); 1077 if (ref->mPid == pid) { 1078 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); 1079 mAudioSessionRefs.removeAt(i); 1080 delete ref; 1081 removed = true; 1082 num--; 1083 } else { 1084 i++; 1085 } 1086 } 1087 if (removed) { 1088 purgeStaleEffects_l(); 1089 } 1090} 1091 1092// audioConfigChanged_l() must be called with AudioFlinger::mLock held 1093void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2) 1094{ 1095 size_t size = mNotificationClients.size(); 1096 for (size_t i = 0; i < size; i++) { 1097 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, 1098 param2); 1099 } 1100} 1101 1102// removeClient_l() must be called with AudioFlinger::mLock held 1103void AudioFlinger::removeClient_l(pid_t pid) 1104{ 1105 ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); 1106 mClients.removeItem(pid); 1107} 1108 1109// getEffectThread_l() must be called with AudioFlinger::mLock held 1110sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId) 1111{ 1112 sp<PlaybackThread> thread; 1113 1114 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 1115 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) { 1116 ALOG_ASSERT(thread == 0); 1117 thread = mPlaybackThreads.valueAt(i); 1118 } 1119 } 1120 1121 return thread; 1122} 1123 1124// ---------------------------------------------------------------------------- 1125 1126AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, 1127 audio_devices_t device, type_t type) 1128 : Thread(false), 1129 mType(type), 1130 mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mNormalFrameCount(0), 1131 // mChannelMask 1132 mChannelCount(0), 1133 mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), 1134 mParamStatus(NO_ERROR), 1135 mStandby(false), mDevice(device), mId(id), 1136 mDeathRecipient(new PMDeathRecipient(this)) 1137{ 1138} 1139 1140AudioFlinger::ThreadBase::~ThreadBase() 1141{ 1142 mParamCond.broadcast(); 1143 // do not lock the mutex in destructor 1144 releaseWakeLock_l(); 1145 if (mPowerManager != 0) { 1146 sp<IBinder> binder = mPowerManager->asBinder(); 1147 binder->unlinkToDeath(mDeathRecipient); 1148 } 1149} 1150 1151void AudioFlinger::ThreadBase::exit() 1152{ 1153 ALOGV("ThreadBase::exit"); 1154 { 1155 // This lock prevents the following race in thread (uniprocessor for illustration): 1156 // if (!exitPending()) { 1157 // // context switch from here to exit() 1158 // // exit() calls requestExit(), what exitPending() observes 1159 // // exit() calls signal(), which is dropped since no waiters 1160 // // context switch back from exit() to here 1161 // mWaitWorkCV.wait(...); 1162 // // now thread is hung 1163 // } 1164 AutoMutex lock(mLock); 1165 requestExit(); 1166 mWaitWorkCV.signal(); 1167 } 1168 // When Thread::requestExitAndWait is made virtual and this method is renamed to 1169 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" 1170 requestExitAndWait(); 1171} 1172 1173status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) 1174{ 1175 status_t status; 1176 1177 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); 1178 Mutex::Autolock _l(mLock); 1179 1180 mNewParameters.add(keyValuePairs); 1181 mWaitWorkCV.signal(); 1182 // wait condition with timeout in case the thread loop has exited 1183 // before the request could be processed 1184 if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { 1185 status = mParamStatus; 1186 mWaitWorkCV.signal(); 1187 } else { 1188 status = TIMED_OUT; 1189 } 1190 return status; 1191} 1192 1193void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) 1194{ 1195 Mutex::Autolock _l(mLock); 1196 sendConfigEvent_l(event, param); 1197} 1198 1199// sendConfigEvent_l() must be called with ThreadBase::mLock held 1200void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) 1201{ 1202 ConfigEvent configEvent; 1203 configEvent.mEvent = event; 1204 configEvent.mParam = param; 1205 mConfigEvents.add(configEvent); 1206 ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); 1207 mWaitWorkCV.signal(); 1208} 1209 1210void AudioFlinger::ThreadBase::processConfigEvents() 1211{ 1212 mLock.lock(); 1213 while (!mConfigEvents.isEmpty()) { 1214 ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); 1215 ConfigEvent configEvent = mConfigEvents[0]; 1216 mConfigEvents.removeAt(0); 1217 // release mLock before locking AudioFlinger mLock: lock order is always 1218 // AudioFlinger then ThreadBase to avoid cross deadlock 1219 mLock.unlock(); 1220 mAudioFlinger->mLock.lock(); 1221 audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); 1222 mAudioFlinger->mLock.unlock(); 1223 mLock.lock(); 1224 } 1225 mLock.unlock(); 1226} 1227 1228void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) 1229{ 1230 const size_t SIZE = 256; 1231 char buffer[SIZE]; 1232 String8 result; 1233 1234 bool locked = tryLock(mLock); 1235 if (!locked) { 1236 snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); 1237 write(fd, buffer, strlen(buffer)); 1238 } 1239 1240 snprintf(buffer, SIZE, "io handle: %d\n", mId); 1241 result.append(buffer); 1242 snprintf(buffer, SIZE, "TID: %d\n", getTid()); 1243 result.append(buffer); 1244 snprintf(buffer, SIZE, "standby: %d\n", mStandby); 1245 result.append(buffer); 1246 snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); 1247 result.append(buffer); 1248 snprintf(buffer, SIZE, "HAL frame count: %d\n", mFrameCount); 1249 result.append(buffer); 1250 snprintf(buffer, SIZE, "Normal frame count: %d\n", mNormalFrameCount); 1251 result.append(buffer); 1252 snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); 1253 result.append(buffer); 1254 snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); 1255 result.append(buffer); 1256 snprintf(buffer, SIZE, "Format: %d\n", mFormat); 1257 result.append(buffer); 1258 snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); 1259 result.append(buffer); 1260 1261 snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); 1262 result.append(buffer); 1263 result.append(" Index Command"); 1264 for (size_t i = 0; i < mNewParameters.size(); ++i) { 1265 snprintf(buffer, SIZE, "\n %02d ", i); 1266 result.append(buffer); 1267 result.append(mNewParameters[i]); 1268 } 1269 1270 snprintf(buffer, SIZE, "\n\nPending config events: \n"); 1271 result.append(buffer); 1272 snprintf(buffer, SIZE, " Index event param\n"); 1273 result.append(buffer); 1274 for (size_t i = 0; i < mConfigEvents.size(); i++) { 1275 snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam); 1276 result.append(buffer); 1277 } 1278 result.append("\n"); 1279 1280 write(fd, result.string(), result.size()); 1281 1282 if (locked) { 1283 mLock.unlock(); 1284 } 1285} 1286 1287void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) 1288{ 1289 const size_t SIZE = 256; 1290 char buffer[SIZE]; 1291 String8 result; 1292 1293 snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); 1294 write(fd, buffer, strlen(buffer)); 1295 1296 for (size_t i = 0; i < mEffectChains.size(); ++i) { 1297 sp<EffectChain> chain = mEffectChains[i]; 1298 if (chain != 0) { 1299 chain->dump(fd, args); 1300 } 1301 } 1302} 1303 1304void AudioFlinger::ThreadBase::acquireWakeLock() 1305{ 1306 Mutex::Autolock _l(mLock); 1307 acquireWakeLock_l(); 1308} 1309 1310void AudioFlinger::ThreadBase::acquireWakeLock_l() 1311{ 1312 if (mPowerManager == 0) { 1313 // use checkService() to avoid blocking if power service is not up yet 1314 sp<IBinder> binder = 1315 defaultServiceManager()->checkService(String16("power")); 1316 if (binder == 0) { 1317 ALOGW("Thread %s cannot connect to the power manager service", mName); 1318 } else { 1319 mPowerManager = interface_cast<IPowerManager>(binder); 1320 binder->linkToDeath(mDeathRecipient); 1321 } 1322 } 1323 if (mPowerManager != 0) { 1324 sp<IBinder> binder = new BBinder(); 1325 status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, 1326 binder, 1327 String16(mName)); 1328 if (status == NO_ERROR) { 1329 mWakeLockToken = binder; 1330 } 1331 ALOGV("acquireWakeLock_l() %s status %d", mName, status); 1332 } 1333} 1334 1335void AudioFlinger::ThreadBase::releaseWakeLock() 1336{ 1337 Mutex::Autolock _l(mLock); 1338 releaseWakeLock_l(); 1339} 1340 1341void AudioFlinger::ThreadBase::releaseWakeLock_l() 1342{ 1343 if (mWakeLockToken != 0) { 1344 ALOGV("releaseWakeLock_l() %s", mName); 1345 if (mPowerManager != 0) { 1346 mPowerManager->releaseWakeLock(mWakeLockToken, 0); 1347 } 1348 mWakeLockToken.clear(); 1349 } 1350} 1351 1352void AudioFlinger::ThreadBase::clearPowerManager() 1353{ 1354 Mutex::Autolock _l(mLock); 1355 releaseWakeLock_l(); 1356 mPowerManager.clear(); 1357} 1358 1359void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) 1360{ 1361 sp<ThreadBase> thread = mThread.promote(); 1362 if (thread != 0) { 1363 thread->clearPowerManager(); 1364 } 1365 ALOGW("power manager service died !!!"); 1366} 1367 1368void AudioFlinger::ThreadBase::setEffectSuspended( 1369 const effect_uuid_t *type, bool suspend, int sessionId) 1370{ 1371 Mutex::Autolock _l(mLock); 1372 setEffectSuspended_l(type, suspend, sessionId); 1373} 1374 1375void AudioFlinger::ThreadBase::setEffectSuspended_l( 1376 const effect_uuid_t *type, bool suspend, int sessionId) 1377{ 1378 sp<EffectChain> chain = getEffectChain_l(sessionId); 1379 if (chain != 0) { 1380 if (type != NULL) { 1381 chain->setEffectSuspended_l(type, suspend); 1382 } else { 1383 chain->setEffectSuspendedAll_l(suspend); 1384 } 1385 } 1386 1387 updateSuspendedSessions_l(type, suspend, sessionId); 1388} 1389 1390void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) 1391{ 1392 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); 1393 if (index < 0) { 1394 return; 1395 } 1396 1397 const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = 1398 mSuspendedSessions.valueAt(index); 1399 1400 for (size_t i = 0; i < sessionEffects.size(); i++) { 1401 sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); 1402 for (int j = 0; j < desc->mRefCount; j++) { 1403 if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { 1404 chain->setEffectSuspendedAll_l(true); 1405 } else { 1406 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", 1407 desc->mType.timeLow); 1408 chain->setEffectSuspended_l(&desc->mType, true); 1409 } 1410 } 1411 } 1412} 1413 1414void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, 1415 bool suspend, 1416 int sessionId) 1417{ 1418 ssize_t index = mSuspendedSessions.indexOfKey(sessionId); 1419 1420 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; 1421 1422 if (suspend) { 1423 if (index >= 0) { 1424 sessionEffects = mSuspendedSessions.valueAt(index); 1425 } else { 1426 mSuspendedSessions.add(sessionId, sessionEffects); 1427 } 1428 } else { 1429 if (index < 0) { 1430 return; 1431 } 1432 sessionEffects = mSuspendedSessions.valueAt(index); 1433 } 1434 1435 1436 int key = EffectChain::kKeyForSuspendAll; 1437 if (type != NULL) { 1438 key = type->timeLow; 1439 } 1440 index = sessionEffects.indexOfKey(key); 1441 1442 sp<SuspendedSessionDesc> desc; 1443 if (suspend) { 1444 if (index >= 0) { 1445 desc = sessionEffects.valueAt(index); 1446 } else { 1447 desc = new SuspendedSessionDesc(); 1448 if (type != NULL) { 1449 desc->mType = *type; 1450 } 1451 sessionEffects.add(key, desc); 1452 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); 1453 } 1454 desc->mRefCount++; 1455 } else { 1456 if (index < 0) { 1457 return; 1458 } 1459 desc = sessionEffects.valueAt(index); 1460 if (--desc->mRefCount == 0) { 1461 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); 1462 sessionEffects.removeItemsAt(index); 1463 if (sessionEffects.isEmpty()) { 1464 ALOGV("updateSuspendedSessions_l() restore removing session %d", 1465 sessionId); 1466 mSuspendedSessions.removeItem(sessionId); 1467 } 1468 } 1469 } 1470 if (!sessionEffects.isEmpty()) { 1471 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); 1472 } 1473} 1474 1475void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 1476 bool enabled, 1477 int sessionId) 1478{ 1479 Mutex::Autolock _l(mLock); 1480 checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); 1481} 1482 1483void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, 1484 bool enabled, 1485 int sessionId) 1486{ 1487 if (mType != RECORD) { 1488 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on 1489 // another session. This gives the priority to well behaved effect control panels 1490 // and applications not using global effects. 1491 // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect 1492 // global effects 1493 if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { 1494 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); 1495 } 1496 } 1497 1498 sp<EffectChain> chain = getEffectChain_l(sessionId); 1499 if (chain != 0) { 1500 chain->checkSuspendOnEffectEnabled(effect, enabled); 1501 } 1502} 1503 1504// ---------------------------------------------------------------------------- 1505 1506AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, 1507 AudioStreamOut* output, 1508 audio_io_handle_t id, 1509 audio_devices_t device, 1510 type_t type) 1511 : ThreadBase(audioFlinger, id, device, type), 1512 mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), 1513 // mStreamTypes[] initialized in constructor body 1514 mOutput(output), 1515 mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), 1516 mMixerStatus(MIXER_IDLE), 1517 mMixerStatusIgnoringFastTracks(MIXER_IDLE), 1518 standbyDelay(AudioFlinger::mStandbyTimeInNsecs), 1519 mScreenState(gScreenState), 1520 // index 0 is reserved for normal mixer's submix 1521 mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1) 1522{ 1523 snprintf(mName, kNameLength, "AudioOut_%X", id); 1524 1525 // Assumes constructor is called by AudioFlinger with it's mLock held, but 1526 // it would be safer to explicitly pass initial masterVolume/masterMute as 1527 // parameter. 1528 // 1529 // If the HAL we are using has support for master volume or master mute, 1530 // then do not attenuate or mute during mixing (just leave the volume at 1.0 1531 // and the mute set to false). 1532 mMasterVolume = audioFlinger->masterVolume_l(); 1533 mMasterMute = audioFlinger->masterMute_l(); 1534 if (mOutput && mOutput->audioHwDev) { 1535 if (mOutput->audioHwDev->canSetMasterVolume()) { 1536 mMasterVolume = 1.0; 1537 } 1538 1539 if (mOutput->audioHwDev->canSetMasterMute()) { 1540 mMasterMute = false; 1541 } 1542 } 1543 1544 readOutputParameters(); 1545 1546 // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor 1547 // There is no AUDIO_STREAM_MIN, and ++ operator does not compile 1548 for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; 1549 stream = (audio_stream_type_t) (stream + 1)) { 1550 mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); 1551 mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); 1552 } 1553 // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, 1554 // because mAudioFlinger doesn't have one to copy from 1555} 1556 1557AudioFlinger::PlaybackThread::~PlaybackThread() 1558{ 1559 delete [] mMixBuffer; 1560} 1561 1562void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) 1563{ 1564 dumpInternals(fd, args); 1565 dumpTracks(fd, args); 1566 dumpEffectChains(fd, args); 1567} 1568 1569void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) 1570{ 1571 const size_t SIZE = 256; 1572 char buffer[SIZE]; 1573 String8 result; 1574 1575 result.appendFormat("Output thread %p stream volumes in dB:\n ", this); 1576 for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { 1577 const stream_type_t *st = &mStreamTypes[i]; 1578 if (i > 0) { 1579 result.appendFormat(", "); 1580 } 1581 result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); 1582 if (st->mute) { 1583 result.append("M"); 1584 } 1585 } 1586 result.append("\n"); 1587 write(fd, result.string(), result.length()); 1588 result.clear(); 1589 1590 snprintf(buffer, SIZE, "Output thread %p tracks\n", this); 1591 result.append(buffer); 1592 Track::appendDumpHeader(result); 1593 for (size_t i = 0; i < mTracks.size(); ++i) { 1594 sp<Track> track = mTracks[i]; 1595 if (track != 0) { 1596 track->dump(buffer, SIZE); 1597 result.append(buffer); 1598 } 1599 } 1600 1601 snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); 1602 result.append(buffer); 1603 Track::appendDumpHeader(result); 1604 for (size_t i = 0; i < mActiveTracks.size(); ++i) { 1605 sp<Track> track = mActiveTracks[i].promote(); 1606 if (track != 0) { 1607 track->dump(buffer, SIZE); 1608 result.append(buffer); 1609 } 1610 } 1611 write(fd, result.string(), result.size()); 1612 1613 // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. 1614 FastTrackUnderruns underruns = getFastTrackUnderruns(0); 1615 fdprintf(fd, "Normal mixer raw underrun counters: partial=%u empty=%u\n", 1616 underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); 1617} 1618 1619void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) 1620{ 1621 const size_t SIZE = 256; 1622 char buffer[SIZE]; 1623 String8 result; 1624 1625 snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); 1626 result.append(buffer); 1627 snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); 1628 result.append(buffer); 1629 snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); 1630 result.append(buffer); 1631 snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); 1632 result.append(buffer); 1633 snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); 1634 result.append(buffer); 1635 snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); 1636 result.append(buffer); 1637 snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); 1638 result.append(buffer); 1639 write(fd, result.string(), result.size()); 1640 fdprintf(fd, "Fast track availMask=%#x\n", mFastTrackAvailMask); 1641 1642 dumpBase(fd, args); 1643} 1644 1645// Thread virtuals 1646status_t AudioFlinger::PlaybackThread::readyToRun() 1647{ 1648 status_t status = initCheck(); 1649 if (status == NO_ERROR) { 1650 ALOGI("AudioFlinger's thread %p ready to run", this); 1651 } else { 1652 ALOGE("No working audio driver found."); 1653 } 1654 return status; 1655} 1656 1657void AudioFlinger::PlaybackThread::onFirstRef() 1658{ 1659 run(mName, ANDROID_PRIORITY_URGENT_AUDIO); 1660} 1661 1662// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held 1663sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( 1664 const sp<AudioFlinger::Client>& client, 1665 audio_stream_type_t streamType, 1666 uint32_t sampleRate, 1667 audio_format_t format, 1668 audio_channel_mask_t channelMask, 1669 int frameCount, 1670 const sp<IMemory>& sharedBuffer, 1671 int sessionId, 1672 IAudioFlinger::track_flags_t flags, 1673 pid_t tid, 1674 status_t *status) 1675{ 1676 sp<Track> track; 1677 status_t lStatus; 1678 1679 bool isTimed = (flags & IAudioFlinger::TRACK_TIMED) != 0; 1680 1681 // client expresses a preference for FAST, but we get the final say 1682 if (flags & IAudioFlinger::TRACK_FAST) { 1683 if ( 1684 // not timed 1685 (!isTimed) && 1686 // either of these use cases: 1687 ( 1688 // use case 1: shared buffer with any frame count 1689 ( 1690 (sharedBuffer != 0) 1691 ) || 1692 // use case 2: callback handler and frame count is default or at least as large as HAL 1693 ( 1694 (tid != -1) && 1695 ((frameCount == 0) || 1696 (frameCount >= (int) (mFrameCount * 2))) // * 2 is due to SRC jitter, see below 1697 ) 1698 ) && 1699 // PCM data 1700 audio_is_linear_pcm(format) && 1701 // mono or stereo 1702 ( (channelMask == AUDIO_CHANNEL_OUT_MONO) || 1703 (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) && 1704#ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE 1705 // hardware sample rate 1706 (sampleRate == mSampleRate) && 1707#endif 1708 // normal mixer has an associated fast mixer 1709 hasFastMixer() && 1710 // there are sufficient fast track slots available 1711 (mFastTrackAvailMask != 0) 1712 // FIXME test that MixerThread for this fast track has a capable output HAL 1713 // FIXME add a permission test also? 1714 ) { 1715 // if frameCount not specified, then it defaults to fast mixer (HAL) frame count 1716 if (frameCount == 0) { 1717 frameCount = mFrameCount * 2; // FIXME * 2 is due to SRC jitter, should be computed 1718 } 1719 ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", 1720 frameCount, mFrameCount); 1721 } else { 1722 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " 1723 "mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%d mSampleRate=%d " 1724 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", 1725 isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, 1726 audio_is_linear_pcm(format), 1727 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); 1728 flags &= ~IAudioFlinger::TRACK_FAST; 1729 // For compatibility with AudioTrack calculation, buffer depth is forced 1730 // to be at least 2 x the normal mixer frame count and cover audio hardware latency. 1731 // This is probably too conservative, but legacy application code may depend on it. 1732 // If you change this calculation, also review the start threshold which is related. 1733 uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); 1734 uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); 1735 if (minBufCount < 2) { 1736 minBufCount = 2; 1737 } 1738 int minFrameCount = mNormalFrameCount * minBufCount; 1739 if (frameCount < minFrameCount) { 1740 frameCount = minFrameCount; 1741 } 1742 } 1743 } 1744 1745 if (mType == DIRECT) { 1746 if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { 1747 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { 1748 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" 1749 "for output %p with format %d", 1750 sampleRate, format, channelMask, mOutput, mFormat); 1751 lStatus = BAD_VALUE; 1752 goto Exit; 1753 } 1754 } 1755 } else { 1756 // Resampler implementation limits input sampling rate to 2 x output sampling rate. 1757 if (sampleRate > mSampleRate*2) { 1758 ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); 1759 lStatus = BAD_VALUE; 1760 goto Exit; 1761 } 1762 } 1763 1764 lStatus = initCheck(); 1765 if (lStatus != NO_ERROR) { 1766 ALOGE("Audio driver not initialized."); 1767 goto Exit; 1768 } 1769 1770 { // scope for mLock 1771 Mutex::Autolock _l(mLock); 1772 1773 // all tracks in same audio session must share the same routing strategy otherwise 1774 // conflicts will happen when tracks are moved from one output to another by audio policy 1775 // manager 1776 uint32_t strategy = AudioSystem::getStrategyForStream(streamType); 1777 for (size_t i = 0; i < mTracks.size(); ++i) { 1778 sp<Track> t = mTracks[i]; 1779 if (t != 0 && !t->isOutputTrack()) { 1780 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); 1781 if (sessionId == t->sessionId() && strategy != actual) { 1782 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", 1783 strategy, actual); 1784 lStatus = BAD_VALUE; 1785 goto Exit; 1786 } 1787 } 1788 } 1789 1790 if (!isTimed) { 1791 track = new Track(this, client, streamType, sampleRate, format, 1792 channelMask, frameCount, sharedBuffer, sessionId, flags); 1793 } else { 1794 track = TimedTrack::create(this, client, streamType, sampleRate, format, 1795 channelMask, frameCount, sharedBuffer, sessionId); 1796 } 1797 if (track == 0 || track->getCblk() == NULL || track->name() < 0) { 1798 lStatus = NO_MEMORY; 1799 goto Exit; 1800 } 1801 mTracks.add(track); 1802 1803 sp<EffectChain> chain = getEffectChain_l(sessionId); 1804 if (chain != 0) { 1805 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); 1806 track->setMainBuffer(chain->inBuffer()); 1807 chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); 1808 chain->incTrackCnt(); 1809 } 1810 } 1811 1812 if ((flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { 1813 pid_t callingPid = IPCThreadState::self()->getCallingPid(); 1814 // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, 1815 // so ask activity manager to do this on our behalf 1816 int err = requestPriority(callingPid, tid, kPriorityAudioApp); 1817 if (err != 0) { 1818 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 1819 kPriorityAudioApp, callingPid, tid, err); 1820 } 1821 } 1822 1823 lStatus = NO_ERROR; 1824 1825Exit: 1826 if (status) { 1827 *status = lStatus; 1828 } 1829 return track; 1830} 1831 1832uint32_t AudioFlinger::MixerThread::correctLatency(uint32_t latency) const 1833{ 1834 if (mFastMixer != NULL) { 1835 MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 1836 latency += (pipe->getAvgFrames() * 1000) / mSampleRate; 1837 } 1838 return latency; 1839} 1840 1841uint32_t AudioFlinger::PlaybackThread::correctLatency(uint32_t latency) const 1842{ 1843 return latency; 1844} 1845 1846uint32_t AudioFlinger::PlaybackThread::latency() const 1847{ 1848 Mutex::Autolock _l(mLock); 1849 return latency_l(); 1850} 1851uint32_t AudioFlinger::PlaybackThread::latency_l() const 1852{ 1853 if (initCheck() == NO_ERROR) { 1854 return correctLatency(mOutput->stream->get_latency(mOutput->stream)); 1855 } else { 1856 return 0; 1857 } 1858} 1859 1860void AudioFlinger::PlaybackThread::setMasterVolume(float value) 1861{ 1862 Mutex::Autolock _l(mLock); 1863 // Don't apply master volume in SW if our HAL can do it for us. 1864 if (mOutput && mOutput->audioHwDev && 1865 mOutput->audioHwDev->canSetMasterVolume()) { 1866 mMasterVolume = 1.0; 1867 } else { 1868 mMasterVolume = value; 1869 } 1870} 1871 1872void AudioFlinger::PlaybackThread::setMasterMute(bool muted) 1873{ 1874 Mutex::Autolock _l(mLock); 1875 // Don't apply master mute in SW if our HAL can do it for us. 1876 if (mOutput && mOutput->audioHwDev && 1877 mOutput->audioHwDev->canSetMasterMute()) { 1878 mMasterMute = false; 1879 } else { 1880 mMasterMute = muted; 1881 } 1882} 1883 1884void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) 1885{ 1886 Mutex::Autolock _l(mLock); 1887 mStreamTypes[stream].volume = value; 1888} 1889 1890void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) 1891{ 1892 Mutex::Autolock _l(mLock); 1893 mStreamTypes[stream].mute = muted; 1894} 1895 1896float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const 1897{ 1898 Mutex::Autolock _l(mLock); 1899 return mStreamTypes[stream].volume; 1900} 1901 1902// addTrack_l() must be called with ThreadBase::mLock held 1903status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) 1904{ 1905 status_t status = ALREADY_EXISTS; 1906 1907 // set retry count for buffer fill 1908 track->mRetryCount = kMaxTrackStartupRetries; 1909 if (mActiveTracks.indexOf(track) < 0) { 1910 // the track is newly added, make sure it fills up all its 1911 // buffers before playing. This is to ensure the client will 1912 // effectively get the latency it requested. 1913 track->mFillingUpStatus = Track::FS_FILLING; 1914 track->mResetDone = false; 1915 track->mPresentationCompleteFrames = 0; 1916 mActiveTracks.add(track); 1917 if (track->mainBuffer() != mMixBuffer) { 1918 sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 1919 if (chain != 0) { 1920 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); 1921 chain->incActiveTrackCnt(); 1922 } 1923 } 1924 1925 status = NO_ERROR; 1926 } 1927 1928 ALOGV("mWaitWorkCV.broadcast"); 1929 mWaitWorkCV.broadcast(); 1930 1931 return status; 1932} 1933 1934// destroyTrack_l() must be called with ThreadBase::mLock held 1935void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) 1936{ 1937 track->mState = TrackBase::TERMINATED; 1938 // active tracks are removed by threadLoop() 1939 if (mActiveTracks.indexOf(track) < 0) { 1940 removeTrack_l(track); 1941 } 1942} 1943 1944void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) 1945{ 1946 track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 1947 mTracks.remove(track); 1948 deleteTrackName_l(track->name()); 1949 // redundant as track is about to be destroyed, for dumpsys only 1950 track->mName = -1; 1951 if (track->isFastTrack()) { 1952 int index = track->mFastIndex; 1953 ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); 1954 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); 1955 mFastTrackAvailMask |= 1 << index; 1956 // redundant as track is about to be destroyed, for dumpsys only 1957 track->mFastIndex = -1; 1958 } 1959 sp<EffectChain> chain = getEffectChain_l(track->sessionId()); 1960 if (chain != 0) { 1961 chain->decTrackCnt(); 1962 } 1963} 1964 1965String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) 1966{ 1967 String8 out_s8 = String8(""); 1968 char *s; 1969 1970 Mutex::Autolock _l(mLock); 1971 if (initCheck() != NO_ERROR) { 1972 return out_s8; 1973 } 1974 1975 s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); 1976 out_s8 = String8(s); 1977 free(s); 1978 return out_s8; 1979} 1980 1981// audioConfigChanged_l() must be called with AudioFlinger::mLock held 1982void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { 1983 AudioSystem::OutputDescriptor desc; 1984 void *param2 = NULL; 1985 1986 ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); 1987 1988 switch (event) { 1989 case AudioSystem::OUTPUT_OPENED: 1990 case AudioSystem::OUTPUT_CONFIG_CHANGED: 1991 desc.channels = mChannelMask; 1992 desc.samplingRate = mSampleRate; 1993 desc.format = mFormat; 1994 desc.frameCount = mNormalFrameCount; // FIXME see AudioFlinger::frameCount(audio_io_handle_t) 1995 desc.latency = latency(); 1996 param2 = &desc; 1997 break; 1998 1999 case AudioSystem::STREAM_CONFIG_CHANGED: 2000 param2 = ¶m; 2001 case AudioSystem::OUTPUT_CLOSED: 2002 default: 2003 break; 2004 } 2005 mAudioFlinger->audioConfigChanged_l(event, mId, param2); 2006} 2007 2008void AudioFlinger::PlaybackThread::readOutputParameters() 2009{ 2010 mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); 2011 mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); 2012 mChannelCount = (uint16_t)popcount(mChannelMask); 2013 mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); 2014 mFrameSize = audio_stream_frame_size(&mOutput->stream->common); 2015 mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; 2016 if (mFrameCount & 15) { 2017 ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", 2018 mFrameCount); 2019 } 2020 2021 // Calculate size of normal mix buffer relative to the HAL output buffer size 2022 double multiplier = 1.0; 2023 if (mType == MIXER && (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) { 2024 size_t minNormalFrameCount = (kMinNormalMixBufferSizeMs * mSampleRate) / 1000; 2025 size_t maxNormalFrameCount = (kMaxNormalMixBufferSizeMs * mSampleRate) / 1000; 2026 // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer 2027 minNormalFrameCount = (minNormalFrameCount + 15) & ~15; 2028 maxNormalFrameCount = maxNormalFrameCount & ~15; 2029 if (maxNormalFrameCount < minNormalFrameCount) { 2030 maxNormalFrameCount = minNormalFrameCount; 2031 } 2032 multiplier = (double) minNormalFrameCount / (double) mFrameCount; 2033 if (multiplier <= 1.0) { 2034 multiplier = 1.0; 2035 } else if (multiplier <= 2.0) { 2036 if (2 * mFrameCount <= maxNormalFrameCount) { 2037 multiplier = 2.0; 2038 } else { 2039 multiplier = (double) maxNormalFrameCount / (double) mFrameCount; 2040 } 2041 } else { 2042 // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL SRC 2043 // (it would be unusual for the normal mix buffer size to not be a multiple of fast 2044 // track, but we sometimes have to do this to satisfy the maximum frame count constraint) 2045 // FIXME this rounding up should not be done if no HAL SRC 2046 uint32_t truncMult = (uint32_t) multiplier; 2047 if ((truncMult & 1)) { 2048 if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { 2049 ++truncMult; 2050 } 2051 } 2052 multiplier = (double) truncMult; 2053 } 2054 } 2055 mNormalFrameCount = multiplier * mFrameCount; 2056 // round up to nearest 16 frames to satisfy AudioMixer 2057 mNormalFrameCount = (mNormalFrameCount + 15) & ~15; 2058 ALOGI("HAL output buffer size %u frames, normal mix buffer size %u frames", mFrameCount, mNormalFrameCount); 2059 2060 delete[] mMixBuffer; 2061 mMixBuffer = new int16_t[mNormalFrameCount * mChannelCount]; 2062 memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); 2063 2064 // force reconfiguration of effect chains and engines to take new buffer size and audio 2065 // parameters into account 2066 // Note that mLock is not held when readOutputParameters() is called from the constructor 2067 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't 2068 // matter. 2069 // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains 2070 Vector< sp<EffectChain> > effectChains = mEffectChains; 2071 for (size_t i = 0; i < effectChains.size(); i ++) { 2072 mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); 2073 } 2074} 2075 2076 2077status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) 2078{ 2079 if (halFrames == NULL || dspFrames == NULL) { 2080 return BAD_VALUE; 2081 } 2082 Mutex::Autolock _l(mLock); 2083 if (initCheck() != NO_ERROR) { 2084 return INVALID_OPERATION; 2085 } 2086 *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); 2087 2088 return mOutput->stream->get_render_position(mOutput->stream, dspFrames); 2089} 2090 2091uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) 2092{ 2093 Mutex::Autolock _l(mLock); 2094 uint32_t result = 0; 2095 if (getEffectChain_l(sessionId) != 0) { 2096 result = EFFECT_SESSION; 2097 } 2098 2099 for (size_t i = 0; i < mTracks.size(); ++i) { 2100 sp<Track> track = mTracks[i]; 2101 if (sessionId == track->sessionId() && 2102 !(track->mCblk->flags & CBLK_INVALID_MSK)) { 2103 result |= TRACK_SESSION; 2104 break; 2105 } 2106 } 2107 2108 return result; 2109} 2110 2111uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) 2112{ 2113 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that 2114 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected 2115 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 2116 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 2117 } 2118 for (size_t i = 0; i < mTracks.size(); i++) { 2119 sp<Track> track = mTracks[i]; 2120 if (sessionId == track->sessionId() && 2121 !(track->mCblk->flags & CBLK_INVALID_MSK)) { 2122 return AudioSystem::getStrategyForStream(track->streamType()); 2123 } 2124 } 2125 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 2126} 2127 2128 2129AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const 2130{ 2131 Mutex::Autolock _l(mLock); 2132 return mOutput; 2133} 2134 2135AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() 2136{ 2137 Mutex::Autolock _l(mLock); 2138 AudioStreamOut *output = mOutput; 2139 mOutput = NULL; 2140 // FIXME FastMixer might also have a raw ptr to mOutputSink; 2141 // must push a NULL and wait for ack 2142 mOutputSink.clear(); 2143 mPipeSink.clear(); 2144 mNormalSink.clear(); 2145 return output; 2146} 2147 2148// this method must always be called either with ThreadBase mLock held or inside the thread loop 2149audio_stream_t* AudioFlinger::PlaybackThread::stream() const 2150{ 2151 if (mOutput == NULL) { 2152 return NULL; 2153 } 2154 return &mOutput->stream->common; 2155} 2156 2157uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const 2158{ 2159 return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); 2160} 2161 2162status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) 2163{ 2164 if (!isValidSyncEvent(event)) { 2165 return BAD_VALUE; 2166 } 2167 2168 Mutex::Autolock _l(mLock); 2169 2170 for (size_t i = 0; i < mTracks.size(); ++i) { 2171 sp<Track> track = mTracks[i]; 2172 if (event->triggerSession() == track->sessionId()) { 2173 track->setSyncEvent(event); 2174 return NO_ERROR; 2175 } 2176 } 2177 2178 return NAME_NOT_FOUND; 2179} 2180 2181bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) 2182{ 2183 switch (event->type()) { 2184 case AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE: 2185 return true; 2186 default: 2187 break; 2188 } 2189 return false; 2190} 2191 2192void AudioFlinger::PlaybackThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) 2193{ 2194 size_t count = tracksToRemove.size(); 2195 if (CC_UNLIKELY(count)) { 2196 for (size_t i = 0 ; i < count ; i++) { 2197 const sp<Track>& track = tracksToRemove.itemAt(i); 2198 if ((track->sharedBuffer() != 0) && 2199 (track->mState == TrackBase::ACTIVE || track->mState == TrackBase::RESUMING)) { 2200 AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); 2201 } 2202 } 2203 } 2204 2205} 2206 2207// ---------------------------------------------------------------------------- 2208 2209AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 2210 audio_io_handle_t id, audio_devices_t device, type_t type) 2211 : PlaybackThread(audioFlinger, output, id, device, type), 2212 // mAudioMixer below 2213 // mFastMixer below 2214 mFastMixerFutex(0) 2215 // mOutputSink below 2216 // mPipeSink below 2217 // mNormalSink below 2218{ 2219 ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); 2220 ALOGV("mSampleRate=%d, mChannelMask=%#x, mChannelCount=%d, mFormat=%d, mFrameSize=%d, " 2221 "mFrameCount=%d, mNormalFrameCount=%d", 2222 mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, 2223 mNormalFrameCount); 2224 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 2225 2226 // FIXME - Current mixer implementation only supports stereo output 2227 if (mChannelCount != FCC_2) { 2228 ALOGE("Invalid audio hardware channel count %d", mChannelCount); 2229 } 2230 2231 // create an NBAIO sink for the HAL output stream, and negotiate 2232 mOutputSink = new AudioStreamOutSink(output->stream); 2233 size_t numCounterOffers = 0; 2234 const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount)}; 2235 ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); 2236 ALOG_ASSERT(index == 0); 2237 2238 // initialize fast mixer depending on configuration 2239 bool initFastMixer; 2240 switch (kUseFastMixer) { 2241 case FastMixer_Never: 2242 initFastMixer = false; 2243 break; 2244 case FastMixer_Always: 2245 initFastMixer = true; 2246 break; 2247 case FastMixer_Static: 2248 case FastMixer_Dynamic: 2249 initFastMixer = mFrameCount < mNormalFrameCount; 2250 break; 2251 } 2252 if (initFastMixer) { 2253 2254 // create a MonoPipe to connect our submix to FastMixer 2255 NBAIO_Format format = mOutputSink->format(); 2256 // This pipe depth compensates for scheduling latency of the normal mixer thread. 2257 // When it wakes up after a maximum latency, it runs a few cycles quickly before 2258 // finally blocking. Note the pipe implementation rounds up the request to a power of 2. 2259 MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); 2260 const NBAIO_Format offers[1] = {format}; 2261 size_t numCounterOffers = 0; 2262 ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); 2263 ALOG_ASSERT(index == 0); 2264 monoPipe->setAvgFrames((mScreenState & 1) ? 2265 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 2266 mPipeSink = monoPipe; 2267 2268#ifdef TEE_SINK_FRAMES 2269 // create a Pipe to archive a copy of FastMixer's output for dumpsys 2270 Pipe *teeSink = new Pipe(TEE_SINK_FRAMES, format); 2271 numCounterOffers = 0; 2272 index = teeSink->negotiate(offers, 1, NULL, numCounterOffers); 2273 ALOG_ASSERT(index == 0); 2274 mTeeSink = teeSink; 2275 PipeReader *teeSource = new PipeReader(*teeSink); 2276 numCounterOffers = 0; 2277 index = teeSource->negotiate(offers, 1, NULL, numCounterOffers); 2278 ALOG_ASSERT(index == 0); 2279 mTeeSource = teeSource; 2280#endif 2281 2282 // create fast mixer and configure it initially with just one fast track for our submix 2283 mFastMixer = new FastMixer(); 2284 FastMixerStateQueue *sq = mFastMixer->sq(); 2285#ifdef STATE_QUEUE_DUMP 2286 sq->setObserverDump(&mStateQueueObserverDump); 2287 sq->setMutatorDump(&mStateQueueMutatorDump); 2288#endif 2289 FastMixerState *state = sq->begin(); 2290 FastTrack *fastTrack = &state->mFastTracks[0]; 2291 // wrap the source side of the MonoPipe to make it an AudioBufferProvider 2292 fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); 2293 fastTrack->mVolumeProvider = NULL; 2294 fastTrack->mGeneration++; 2295 state->mFastTracksGen++; 2296 state->mTrackMask = 1; 2297 // fast mixer will use the HAL output sink 2298 state->mOutputSink = mOutputSink.get(); 2299 state->mOutputSinkGen++; 2300 state->mFrameCount = mFrameCount; 2301 state->mCommand = FastMixerState::COLD_IDLE; 2302 // already done in constructor initialization list 2303 //mFastMixerFutex = 0; 2304 state->mColdFutexAddr = &mFastMixerFutex; 2305 state->mColdGen++; 2306 state->mDumpState = &mFastMixerDumpState; 2307 state->mTeeSink = mTeeSink.get(); 2308 sq->end(); 2309 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 2310 2311 // start the fast mixer 2312 mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); 2313 pid_t tid = mFastMixer->getTid(); 2314 int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 2315 if (err != 0) { 2316 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 2317 kPriorityFastMixer, getpid_cached, tid, err); 2318 } 2319 2320#ifdef AUDIO_WATCHDOG 2321 // create and start the watchdog 2322 mAudioWatchdog = new AudioWatchdog(); 2323 mAudioWatchdog->setDump(&mAudioWatchdogDump); 2324 mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); 2325 tid = mAudioWatchdog->getTid(); 2326 err = requestPriority(getpid_cached, tid, kPriorityFastMixer); 2327 if (err != 0) { 2328 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", 2329 kPriorityFastMixer, getpid_cached, tid, err); 2330 } 2331#endif 2332 2333 } else { 2334 mFastMixer = NULL; 2335 } 2336 2337 switch (kUseFastMixer) { 2338 case FastMixer_Never: 2339 case FastMixer_Dynamic: 2340 mNormalSink = mOutputSink; 2341 break; 2342 case FastMixer_Always: 2343 mNormalSink = mPipeSink; 2344 break; 2345 case FastMixer_Static: 2346 mNormalSink = initFastMixer ? mPipeSink : mOutputSink; 2347 break; 2348 } 2349} 2350 2351AudioFlinger::MixerThread::~MixerThread() 2352{ 2353 if (mFastMixer != NULL) { 2354 FastMixerStateQueue *sq = mFastMixer->sq(); 2355 FastMixerState *state = sq->begin(); 2356 if (state->mCommand == FastMixerState::COLD_IDLE) { 2357 int32_t old = android_atomic_inc(&mFastMixerFutex); 2358 if (old == -1) { 2359 __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 2360 } 2361 } 2362 state->mCommand = FastMixerState::EXIT; 2363 sq->end(); 2364 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 2365 mFastMixer->join(); 2366 // Though the fast mixer thread has exited, it's state queue is still valid. 2367 // We'll use that extract the final state which contains one remaining fast track 2368 // corresponding to our sub-mix. 2369 state = sq->begin(); 2370 ALOG_ASSERT(state->mTrackMask == 1); 2371 FastTrack *fastTrack = &state->mFastTracks[0]; 2372 ALOG_ASSERT(fastTrack->mBufferProvider != NULL); 2373 delete fastTrack->mBufferProvider; 2374 sq->end(false /*didModify*/); 2375 delete mFastMixer; 2376 if (mAudioWatchdog != 0) { 2377 mAudioWatchdog->requestExit(); 2378 mAudioWatchdog->requestExitAndWait(); 2379 mAudioWatchdog.clear(); 2380 } 2381 } 2382 delete mAudioMixer; 2383} 2384 2385class CpuStats { 2386public: 2387 CpuStats(); 2388 void sample(const String8 &title); 2389#ifdef DEBUG_CPU_USAGE 2390private: 2391 ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns 2392 CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns 2393 2394 CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles 2395 2396 int mCpuNum; // thread's current CPU number 2397 int mCpukHz; // frequency of thread's current CPU in kHz 2398#endif 2399}; 2400 2401CpuStats::CpuStats() 2402#ifdef DEBUG_CPU_USAGE 2403 : mCpuNum(-1), mCpukHz(-1) 2404#endif 2405{ 2406} 2407 2408void CpuStats::sample(const String8 &title) { 2409#ifdef DEBUG_CPU_USAGE 2410 // get current thread's delta CPU time in wall clock ns 2411 double wcNs; 2412 bool valid = mCpuUsage.sampleAndEnable(wcNs); 2413 2414 // record sample for wall clock statistics 2415 if (valid) { 2416 mWcStats.sample(wcNs); 2417 } 2418 2419 // get the current CPU number 2420 int cpuNum = sched_getcpu(); 2421 2422 // get the current CPU frequency in kHz 2423 int cpukHz = mCpuUsage.getCpukHz(cpuNum); 2424 2425 // check if either CPU number or frequency changed 2426 if (cpuNum != mCpuNum || cpukHz != mCpukHz) { 2427 mCpuNum = cpuNum; 2428 mCpukHz = cpukHz; 2429 // ignore sample for purposes of cycles 2430 valid = false; 2431 } 2432 2433 // if no change in CPU number or frequency, then record sample for cycle statistics 2434 if (valid && mCpukHz > 0) { 2435 double cycles = wcNs * cpukHz * 0.000001; 2436 mHzStats.sample(cycles); 2437 } 2438 2439 unsigned n = mWcStats.n(); 2440 // mCpuUsage.elapsed() is expensive, so don't call it every loop 2441 if ((n & 127) == 1) { 2442 long long elapsed = mCpuUsage.elapsed(); 2443 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { 2444 double perLoop = elapsed / (double) n; 2445 double perLoop100 = perLoop * 0.01; 2446 double perLoop1k = perLoop * 0.001; 2447 double mean = mWcStats.mean(); 2448 double stddev = mWcStats.stddev(); 2449 double minimum = mWcStats.minimum(); 2450 double maximum = mWcStats.maximum(); 2451 double meanCycles = mHzStats.mean(); 2452 double stddevCycles = mHzStats.stddev(); 2453 double minCycles = mHzStats.minimum(); 2454 double maxCycles = mHzStats.maximum(); 2455 mCpuUsage.resetElapsed(); 2456 mWcStats.reset(); 2457 mHzStats.reset(); 2458 ALOGD("CPU usage for %s over past %.1f secs\n" 2459 " (%u mixer loops at %.1f mean ms per loop):\n" 2460 " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" 2461 " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" 2462 " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", 2463 title.string(), 2464 elapsed * .000000001, n, perLoop * .000001, 2465 mean * .001, 2466 stddev * .001, 2467 minimum * .001, 2468 maximum * .001, 2469 mean / perLoop100, 2470 stddev / perLoop100, 2471 minimum / perLoop100, 2472 maximum / perLoop100, 2473 meanCycles / perLoop1k, 2474 stddevCycles / perLoop1k, 2475 minCycles / perLoop1k, 2476 maxCycles / perLoop1k); 2477 2478 } 2479 } 2480#endif 2481}; 2482 2483void AudioFlinger::PlaybackThread::checkSilentMode_l() 2484{ 2485 if (!mMasterMute) { 2486 char value[PROPERTY_VALUE_MAX]; 2487 if (property_get("ro.audio.silent", value, "0") > 0) { 2488 char *endptr; 2489 unsigned long ul = strtoul(value, &endptr, 0); 2490 if (*endptr == '\0' && ul != 0) { 2491 ALOGD("Silence is golden"); 2492 // The setprop command will not allow a property to be changed after 2493 // the first time it is set, so we don't have to worry about un-muting. 2494 setMasterMute_l(true); 2495 } 2496 } 2497 } 2498} 2499 2500bool AudioFlinger::PlaybackThread::threadLoop() 2501{ 2502 Vector< sp<Track> > tracksToRemove; 2503 2504 standbyTime = systemTime(); 2505 2506 // MIXER 2507 nsecs_t lastWarning = 0; 2508 2509 // DUPLICATING 2510 // FIXME could this be made local to while loop? 2511 writeFrames = 0; 2512 2513 cacheParameters_l(); 2514 sleepTime = idleSleepTime; 2515 2516 if (mType == MIXER) { 2517 sleepTimeShift = 0; 2518 } 2519 2520 CpuStats cpuStats; 2521 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); 2522 2523 acquireWakeLock(); 2524 2525 while (!exitPending()) 2526 { 2527 cpuStats.sample(myName); 2528 2529 Vector< sp<EffectChain> > effectChains; 2530 2531 processConfigEvents(); 2532 2533 { // scope for mLock 2534 2535 Mutex::Autolock _l(mLock); 2536 2537 if (checkForNewParameters_l()) { 2538 cacheParameters_l(); 2539 } 2540 2541 saveOutputTracks(); 2542 2543 // put audio hardware into standby after short delay 2544 if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || 2545 isSuspended())) { 2546 if (!mStandby) { 2547 2548 threadLoop_standby(); 2549 2550 mStandby = true; 2551 mBytesWritten = 0; 2552 } 2553 2554 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { 2555 // we're about to wait, flush the binder command buffer 2556 IPCThreadState::self()->flushCommands(); 2557 2558 clearOutputTracks(); 2559 2560 if (exitPending()) break; 2561 2562 releaseWakeLock_l(); 2563 // wait until we have something to do... 2564 ALOGV("%s going to sleep", myName.string()); 2565 mWaitWorkCV.wait(mLock); 2566 ALOGV("%s waking up", myName.string()); 2567 acquireWakeLock_l(); 2568 2569 mMixerStatus = MIXER_IDLE; 2570 mMixerStatusIgnoringFastTracks = MIXER_IDLE; 2571 2572 checkSilentMode_l(); 2573 2574 standbyTime = systemTime() + standbyDelay; 2575 sleepTime = idleSleepTime; 2576 if (mType == MIXER) { 2577 sleepTimeShift = 0; 2578 } 2579 2580 continue; 2581 } 2582 } 2583 2584 // mMixerStatusIgnoringFastTracks is also updated internally 2585 mMixerStatus = prepareTracks_l(&tracksToRemove); 2586 2587 // prevent any changes in effect chain list and in each effect chain 2588 // during mixing and effect process as the audio buffers could be deleted 2589 // or modified if an effect is created or deleted 2590 lockEffectChains_l(effectChains); 2591 } 2592 2593 if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) { 2594 threadLoop_mix(); 2595 } else { 2596 threadLoop_sleepTime(); 2597 } 2598 2599 if (isSuspended()) { 2600 sleepTime = suspendSleepTimeUs(); 2601 } 2602 2603 // only process effects if we're going to write 2604 if (sleepTime == 0) { 2605 for (size_t i = 0; i < effectChains.size(); i ++) { 2606 effectChains[i]->process_l(); 2607 } 2608 } 2609 2610 // enable changes in effect chain 2611 unlockEffectChains(effectChains); 2612 2613 // sleepTime == 0 means we must write to audio hardware 2614 if (sleepTime == 0) { 2615 2616 threadLoop_write(); 2617 2618if (mType == MIXER) { 2619 // write blocked detection 2620 nsecs_t now = systemTime(); 2621 nsecs_t delta = now - mLastWriteTime; 2622 if (!mStandby && delta > maxPeriod) { 2623 mNumDelayedWrites++; 2624 if ((now - lastWarning) > kWarningThrottleNs) { 2625#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 2626 ScopedTrace st(ATRACE_TAG, "underrun"); 2627#endif 2628 ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", 2629 ns2ms(delta), mNumDelayedWrites, this); 2630 lastWarning = now; 2631 } 2632 } 2633} 2634 2635 mStandby = false; 2636 } else { 2637 usleep(sleepTime); 2638 } 2639 2640 // Finally let go of removed track(s), without the lock held 2641 // since we can't guarantee the destructors won't acquire that 2642 // same lock. This will also mutate and push a new fast mixer state. 2643 threadLoop_removeTracks(tracksToRemove); 2644 tracksToRemove.clear(); 2645 2646 // FIXME I don't understand the need for this here; 2647 // it was in the original code but maybe the 2648 // assignment in saveOutputTracks() makes this unnecessary? 2649 clearOutputTracks(); 2650 2651 // Effect chains will be actually deleted here if they were removed from 2652 // mEffectChains list during mixing or effects processing 2653 effectChains.clear(); 2654 2655 // FIXME Note that the above .clear() is no longer necessary since effectChains 2656 // is now local to this block, but will keep it for now (at least until merge done). 2657 } 2658 2659 // for DuplicatingThread, standby mode is handled by the outputTracks, otherwise ... 2660 if (mType == MIXER || mType == DIRECT) { 2661 // put output stream into standby mode 2662 if (!mStandby) { 2663 mOutput->stream->common.standby(&mOutput->stream->common); 2664 } 2665 } 2666 2667 releaseWakeLock(); 2668 2669 ALOGV("Thread %p type %d exiting", this, mType); 2670 return false; 2671} 2672 2673void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) 2674{ 2675 PlaybackThread::threadLoop_removeTracks(tracksToRemove); 2676} 2677 2678void AudioFlinger::MixerThread::threadLoop_write() 2679{ 2680 // FIXME we should only do one push per cycle; confirm this is true 2681 // Start the fast mixer if it's not already running 2682 if (mFastMixer != NULL) { 2683 FastMixerStateQueue *sq = mFastMixer->sq(); 2684 FastMixerState *state = sq->begin(); 2685 if (state->mCommand != FastMixerState::MIX_WRITE && 2686 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { 2687 if (state->mCommand == FastMixerState::COLD_IDLE) { 2688 int32_t old = android_atomic_inc(&mFastMixerFutex); 2689 if (old == -1) { 2690 __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); 2691 } 2692 if (mAudioWatchdog != 0) { 2693 mAudioWatchdog->resume(); 2694 } 2695 } 2696 state->mCommand = FastMixerState::MIX_WRITE; 2697 sq->end(); 2698 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 2699 if (kUseFastMixer == FastMixer_Dynamic) { 2700 mNormalSink = mPipeSink; 2701 } 2702 } else { 2703 sq->end(false /*didModify*/); 2704 } 2705 } 2706 PlaybackThread::threadLoop_write(); 2707} 2708 2709// shared by MIXER and DIRECT, overridden by DUPLICATING 2710void AudioFlinger::PlaybackThread::threadLoop_write() 2711{ 2712 // FIXME rewrite to reduce number of system calls 2713 mLastWriteTime = systemTime(); 2714 mInWrite = true; 2715 int bytesWritten; 2716 2717 // If an NBAIO sink is present, use it to write the normal mixer's submix 2718 if (mNormalSink != 0) { 2719#define mBitShift 2 // FIXME 2720 size_t count = mixBufferSize >> mBitShift; 2721#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 2722 Tracer::traceBegin(ATRACE_TAG, "write"); 2723#endif 2724 // update the setpoint when gScreenState changes 2725 uint32_t screenState = gScreenState; 2726 if (screenState != mScreenState) { 2727 mScreenState = screenState; 2728 MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); 2729 if (pipe != NULL) { 2730 pipe->setAvgFrames((mScreenState & 1) ? 2731 (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); 2732 } 2733 } 2734 ssize_t framesWritten = mNormalSink->write(mMixBuffer, count); 2735#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 2736 Tracer::traceEnd(ATRACE_TAG); 2737#endif 2738 if (framesWritten > 0) { 2739 bytesWritten = framesWritten << mBitShift; 2740 } else { 2741 bytesWritten = framesWritten; 2742 } 2743 // otherwise use the HAL / AudioStreamOut directly 2744 } else { 2745 // Direct output thread. 2746 bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); 2747 } 2748 2749 if (bytesWritten > 0) mBytesWritten += mixBufferSize; 2750 mNumWrites++; 2751 mInWrite = false; 2752} 2753 2754void AudioFlinger::MixerThread::threadLoop_standby() 2755{ 2756 // Idle the fast mixer if it's currently running 2757 if (mFastMixer != NULL) { 2758 FastMixerStateQueue *sq = mFastMixer->sq(); 2759 FastMixerState *state = sq->begin(); 2760 if (!(state->mCommand & FastMixerState::IDLE)) { 2761 state->mCommand = FastMixerState::COLD_IDLE; 2762 state->mColdFutexAddr = &mFastMixerFutex; 2763 state->mColdGen++; 2764 mFastMixerFutex = 0; 2765 sq->end(); 2766 // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now 2767 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 2768 if (kUseFastMixer == FastMixer_Dynamic) { 2769 mNormalSink = mOutputSink; 2770 } 2771 if (mAudioWatchdog != 0) { 2772 mAudioWatchdog->pause(); 2773 } 2774 } else { 2775 sq->end(false /*didModify*/); 2776 } 2777 } 2778 PlaybackThread::threadLoop_standby(); 2779} 2780 2781// shared by MIXER and DIRECT, overridden by DUPLICATING 2782void AudioFlinger::PlaybackThread::threadLoop_standby() 2783{ 2784 ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); 2785 mOutput->stream->common.standby(&mOutput->stream->common); 2786} 2787 2788void AudioFlinger::MixerThread::threadLoop_mix() 2789{ 2790 // obtain the presentation timestamp of the next output buffer 2791 int64_t pts; 2792 status_t status = INVALID_OPERATION; 2793 2794 if (mNormalSink != 0) { 2795 status = mNormalSink->getNextWriteTimestamp(&pts); 2796 } else { 2797 status = mOutputSink->getNextWriteTimestamp(&pts); 2798 } 2799 2800 if (status != NO_ERROR) { 2801 pts = AudioBufferProvider::kInvalidPTS; 2802 } 2803 2804 // mix buffers... 2805 mAudioMixer->process(pts); 2806 // increase sleep time progressively when application underrun condition clears. 2807 // Only increase sleep time if the mixer is ready for two consecutive times to avoid 2808 // that a steady state of alternating ready/not ready conditions keeps the sleep time 2809 // such that we would underrun the audio HAL. 2810 if ((sleepTime == 0) && (sleepTimeShift > 0)) { 2811 sleepTimeShift--; 2812 } 2813 sleepTime = 0; 2814 standbyTime = systemTime() + standbyDelay; 2815 //TODO: delay standby when effects have a tail 2816} 2817 2818void AudioFlinger::MixerThread::threadLoop_sleepTime() 2819{ 2820 // If no tracks are ready, sleep once for the duration of an output 2821 // buffer size, then write 0s to the output 2822 if (sleepTime == 0) { 2823 if (mMixerStatus == MIXER_TRACKS_ENABLED) { 2824 sleepTime = activeSleepTime >> sleepTimeShift; 2825 if (sleepTime < kMinThreadSleepTimeUs) { 2826 sleepTime = kMinThreadSleepTimeUs; 2827 } 2828 // reduce sleep time in case of consecutive application underruns to avoid 2829 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer 2830 // duration we would end up writing less data than needed by the audio HAL if 2831 // the condition persists. 2832 if (sleepTimeShift < kMaxThreadSleepTimeShift) { 2833 sleepTimeShift++; 2834 } 2835 } else { 2836 sleepTime = idleSleepTime; 2837 } 2838 } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { 2839 memset (mMixBuffer, 0, mixBufferSize); 2840 sleepTime = 0; 2841 ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED)), "anticipated start"); 2842 } 2843 // TODO add standby time extension fct of effect tail 2844} 2845 2846// prepareTracks_l() must be called with ThreadBase::mLock held 2847AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( 2848 Vector< sp<Track> > *tracksToRemove) 2849{ 2850 2851 mixer_state mixerStatus = MIXER_IDLE; 2852 // find out which tracks need to be processed 2853 size_t count = mActiveTracks.size(); 2854 size_t mixedTracks = 0; 2855 size_t tracksWithEffect = 0; 2856 // counts only _active_ fast tracks 2857 size_t fastTracks = 0; 2858 uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset 2859 2860 float masterVolume = mMasterVolume; 2861 bool masterMute = mMasterMute; 2862 2863 if (masterMute) { 2864 masterVolume = 0; 2865 } 2866 // Delegate master volume control to effect in output mix effect chain if needed 2867 sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 2868 if (chain != 0) { 2869 uint32_t v = (uint32_t)(masterVolume * (1 << 24)); 2870 chain->setVolume_l(&v, &v); 2871 masterVolume = (float)((v + (1 << 23)) >> 24); 2872 chain.clear(); 2873 } 2874 2875 // prepare a new state to push 2876 FastMixerStateQueue *sq = NULL; 2877 FastMixerState *state = NULL; 2878 bool didModify = false; 2879 FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; 2880 if (mFastMixer != NULL) { 2881 sq = mFastMixer->sq(); 2882 state = sq->begin(); 2883 } 2884 2885 for (size_t i=0 ; i<count ; i++) { 2886 sp<Track> t = mActiveTracks[i].promote(); 2887 if (t == 0) continue; 2888 2889 // this const just means the local variable doesn't change 2890 Track* const track = t.get(); 2891 2892 // process fast tracks 2893 if (track->isFastTrack()) { 2894 2895 // It's theoretically possible (though unlikely) for a fast track to be created 2896 // and then removed within the same normal mix cycle. This is not a problem, as 2897 // the track never becomes active so it's fast mixer slot is never touched. 2898 // The converse, of removing an (active) track and then creating a new track 2899 // at the identical fast mixer slot within the same normal mix cycle, 2900 // is impossible because the slot isn't marked available until the end of each cycle. 2901 int j = track->mFastIndex; 2902 ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); 2903 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); 2904 FastTrack *fastTrack = &state->mFastTracks[j]; 2905 2906 // Determine whether the track is currently in underrun condition, 2907 // and whether it had a recent underrun. 2908 FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; 2909 FastTrackUnderruns underruns = ftDump->mUnderruns; 2910 uint32_t recentFull = (underruns.mBitFields.mFull - 2911 track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; 2912 uint32_t recentPartial = (underruns.mBitFields.mPartial - 2913 track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; 2914 uint32_t recentEmpty = (underruns.mBitFields.mEmpty - 2915 track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; 2916 uint32_t recentUnderruns = recentPartial + recentEmpty; 2917 track->mObservedUnderruns = underruns; 2918 // don't count underruns that occur while stopping or pausing 2919 // or stopped which can occur when flush() is called while active 2920 if (!(track->isStopping() || track->isPausing() || track->isStopped())) { 2921 track->mUnderrunCount += recentUnderruns; 2922 } 2923 2924 // This is similar to the state machine for normal tracks, 2925 // with a few modifications for fast tracks. 2926 bool isActive = true; 2927 switch (track->mState) { 2928 case TrackBase::STOPPING_1: 2929 // track stays active in STOPPING_1 state until first underrun 2930 if (recentUnderruns > 0) { 2931 track->mState = TrackBase::STOPPING_2; 2932 } 2933 break; 2934 case TrackBase::PAUSING: 2935 // ramp down is not yet implemented 2936 track->setPaused(); 2937 break; 2938 case TrackBase::RESUMING: 2939 // ramp up is not yet implemented 2940 track->mState = TrackBase::ACTIVE; 2941 break; 2942 case TrackBase::ACTIVE: 2943 if (recentFull > 0 || recentPartial > 0) { 2944 // track has provided at least some frames recently: reset retry count 2945 track->mRetryCount = kMaxTrackRetries; 2946 } 2947 if (recentUnderruns == 0) { 2948 // no recent underruns: stay active 2949 break; 2950 } 2951 // there has recently been an underrun of some kind 2952 if (track->sharedBuffer() == 0) { 2953 // were any of the recent underruns "empty" (no frames available)? 2954 if (recentEmpty == 0) { 2955 // no, then ignore the partial underruns as they are allowed indefinitely 2956 break; 2957 } 2958 // there has recently been an "empty" underrun: decrement the retry counter 2959 if (--(track->mRetryCount) > 0) { 2960 break; 2961 } 2962 // indicate to client process that the track was disabled because of underrun; 2963 // it will then automatically call start() when data is available 2964 android_atomic_or(CBLK_DISABLED_ON, &track->mCblk->flags); 2965 // remove from active list, but state remains ACTIVE [confusing but true] 2966 isActive = false; 2967 break; 2968 } 2969 // fall through 2970 case TrackBase::STOPPING_2: 2971 case TrackBase::PAUSED: 2972 case TrackBase::TERMINATED: 2973 case TrackBase::STOPPED: 2974 case TrackBase::FLUSHED: // flush() while active 2975 // Check for presentation complete if track is inactive 2976 // We have consumed all the buffers of this track. 2977 // This would be incomplete if we auto-paused on underrun 2978 { 2979 size_t audioHALFrames = 2980 (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 2981 size_t framesWritten = 2982 mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); 2983 if (!track->presentationComplete(framesWritten, audioHALFrames)) { 2984 // track stays in active list until presentation is complete 2985 break; 2986 } 2987 } 2988 if (track->isStopping_2()) { 2989 track->mState = TrackBase::STOPPED; 2990 } 2991 if (track->isStopped()) { 2992 // Can't reset directly, as fast mixer is still polling this track 2993 // track->reset(); 2994 // So instead mark this track as needing to be reset after push with ack 2995 resetMask |= 1 << i; 2996 } 2997 isActive = false; 2998 break; 2999 case TrackBase::IDLE: 3000 default: 3001 LOG_FATAL("unexpected track state %d", track->mState); 3002 } 3003 3004 if (isActive) { 3005 // was it previously inactive? 3006 if (!(state->mTrackMask & (1 << j))) { 3007 ExtendedAudioBufferProvider *eabp = track; 3008 VolumeProvider *vp = track; 3009 fastTrack->mBufferProvider = eabp; 3010 fastTrack->mVolumeProvider = vp; 3011 fastTrack->mSampleRate = track->mSampleRate; 3012 fastTrack->mChannelMask = track->mChannelMask; 3013 fastTrack->mGeneration++; 3014 state->mTrackMask |= 1 << j; 3015 didModify = true; 3016 // no acknowledgement required for newly active tracks 3017 } 3018 // cache the combined master volume and stream type volume for fast mixer; this 3019 // lacks any synchronization or barrier so VolumeProvider may read a stale value 3020 track->mCachedVolume = track->isMuted() ? 3021 0 : masterVolume * mStreamTypes[track->streamType()].volume; 3022 ++fastTracks; 3023 } else { 3024 // was it previously active? 3025 if (state->mTrackMask & (1 << j)) { 3026 fastTrack->mBufferProvider = NULL; 3027 fastTrack->mGeneration++; 3028 state->mTrackMask &= ~(1 << j); 3029 didModify = true; 3030 // If any fast tracks were removed, we must wait for acknowledgement 3031 // because we're about to decrement the last sp<> on those tracks. 3032 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 3033 } else { 3034 LOG_FATAL("fast track %d should have been active", j); 3035 } 3036 tracksToRemove->add(track); 3037 // Avoids a misleading display in dumpsys 3038 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; 3039 } 3040 continue; 3041 } 3042 3043 { // local variable scope to avoid goto warning 3044 3045 audio_track_cblk_t* cblk = track->cblk(); 3046 3047 // The first time a track is added we wait 3048 // for all its buffers to be filled before processing it 3049 int name = track->name(); 3050 // make sure that we have enough frames to mix one full buffer. 3051 // enforce this condition only once to enable draining the buffer in case the client 3052 // app does not call stop() and relies on underrun to stop: 3053 // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed 3054 // during last round 3055 uint32_t minFrames = 1; 3056 if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && 3057 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { 3058 if (t->sampleRate() == (int)mSampleRate) { 3059 minFrames = mNormalFrameCount; 3060 } else { 3061 // +1 for rounding and +1 for additional sample needed for interpolation 3062 minFrames = (mNormalFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; 3063 // add frames already consumed but not yet released by the resampler 3064 // because cblk->framesReady() will include these frames 3065 minFrames += mAudioMixer->getUnreleasedFrames(track->name()); 3066 // the minimum track buffer size is normally twice the number of frames necessary 3067 // to fill one buffer and the resampler should not leave more than one buffer worth 3068 // of unreleased frames after each pass, but just in case... 3069 ALOG_ASSERT(minFrames <= cblk->frameCount); 3070 } 3071 } 3072 if ((track->framesReady() >= minFrames) && track->isReady() && 3073 !track->isPaused() && !track->isTerminated()) 3074 { 3075 //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this); 3076 3077 mixedTracks++; 3078 3079 // track->mainBuffer() != mMixBuffer means there is an effect chain 3080 // connected to the track 3081 chain.clear(); 3082 if (track->mainBuffer() != mMixBuffer) { 3083 chain = getEffectChain_l(track->sessionId()); 3084 // Delegate volume control to effect in track effect chain if needed 3085 if (chain != 0) { 3086 tracksWithEffect++; 3087 } else { 3088 ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d", 3089 name, track->sessionId()); 3090 } 3091 } 3092 3093 3094 int param = AudioMixer::VOLUME; 3095 if (track->mFillingUpStatus == Track::FS_FILLED) { 3096 // no ramp for the first volume setting 3097 track->mFillingUpStatus = Track::FS_ACTIVE; 3098 if (track->mState == TrackBase::RESUMING) { 3099 track->mState = TrackBase::ACTIVE; 3100 param = AudioMixer::RAMP_VOLUME; 3101 } 3102 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); 3103 } else if (cblk->server != 0) { 3104 // If the track is stopped before the first frame was mixed, 3105 // do not apply ramp 3106 param = AudioMixer::RAMP_VOLUME; 3107 } 3108 3109 // compute volume for this track 3110 uint32_t vl, vr, va; 3111 if (track->isMuted() || track->isPausing() || 3112 mStreamTypes[track->streamType()].mute) { 3113 vl = vr = va = 0; 3114 if (track->isPausing()) { 3115 track->setPaused(); 3116 } 3117 } else { 3118 3119 // read original volumes with volume control 3120 float typeVolume = mStreamTypes[track->streamType()].volume; 3121 float v = masterVolume * typeVolume; 3122 uint32_t vlr = cblk->getVolumeLR(); 3123 vl = vlr & 0xFFFF; 3124 vr = vlr >> 16; 3125 // track volumes come from shared memory, so can't be trusted and must be clamped 3126 if (vl > MAX_GAIN_INT) { 3127 ALOGV("Track left volume out of range: %04X", vl); 3128 vl = MAX_GAIN_INT; 3129 } 3130 if (vr > MAX_GAIN_INT) { 3131 ALOGV("Track right volume out of range: %04X", vr); 3132 vr = MAX_GAIN_INT; 3133 } 3134 // now apply the master volume and stream type volume 3135 vl = (uint32_t)(v * vl) << 12; 3136 vr = (uint32_t)(v * vr) << 12; 3137 // assuming master volume and stream type volume each go up to 1.0, 3138 // vl and vr are now in 8.24 format 3139 3140 uint16_t sendLevel = cblk->getSendLevel_U4_12(); 3141 // send level comes from shared memory and so may be corrupt 3142 if (sendLevel > MAX_GAIN_INT) { 3143 ALOGV("Track send level out of range: %04X", sendLevel); 3144 sendLevel = MAX_GAIN_INT; 3145 } 3146 va = (uint32_t)(v * sendLevel); 3147 } 3148 // Delegate volume control to effect in track effect chain if needed 3149 if (chain != 0 && chain->setVolume_l(&vl, &vr)) { 3150 // Do not ramp volume if volume is controlled by effect 3151 param = AudioMixer::VOLUME; 3152 track->mHasVolumeController = true; 3153 } else { 3154 // force no volume ramp when volume controller was just disabled or removed 3155 // from effect chain to avoid volume spike 3156 if (track->mHasVolumeController) { 3157 param = AudioMixer::VOLUME; 3158 } 3159 track->mHasVolumeController = false; 3160 } 3161 3162 // Convert volumes from 8.24 to 4.12 format 3163 // This additional clamping is needed in case chain->setVolume_l() overshot 3164 vl = (vl + (1 << 11)) >> 12; 3165 if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT; 3166 vr = (vr + (1 << 11)) >> 12; 3167 if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT; 3168 3169 if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; // va is uint32_t, so no need to check for - 3170 3171 // XXX: these things DON'T need to be done each time 3172 mAudioMixer->setBufferProvider(name, track); 3173 mAudioMixer->enable(name); 3174 3175 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl); 3176 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr); 3177 mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va); 3178 mAudioMixer->setParameter( 3179 name, 3180 AudioMixer::TRACK, 3181 AudioMixer::FORMAT, (void *)track->format()); 3182 mAudioMixer->setParameter( 3183 name, 3184 AudioMixer::TRACK, 3185 AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); 3186 mAudioMixer->setParameter( 3187 name, 3188 AudioMixer::RESAMPLE, 3189 AudioMixer::SAMPLE_RATE, 3190 (void *)(cblk->sampleRate)); 3191 mAudioMixer->setParameter( 3192 name, 3193 AudioMixer::TRACK, 3194 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); 3195 mAudioMixer->setParameter( 3196 name, 3197 AudioMixer::TRACK, 3198 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); 3199 3200 // reset retry count 3201 track->mRetryCount = kMaxTrackRetries; 3202 3203 // If one track is ready, set the mixer ready if: 3204 // - the mixer was not ready during previous round OR 3205 // - no other track is not ready 3206 if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || 3207 mixerStatus != MIXER_TRACKS_ENABLED) { 3208 mixerStatus = MIXER_TRACKS_READY; 3209 } 3210 } else { 3211 // clear effect chain input buffer if an active track underruns to avoid sending 3212 // previous audio buffer again to effects 3213 chain = getEffectChain_l(track->sessionId()); 3214 if (chain != 0) { 3215 chain->clearInputBuffer(); 3216 } 3217 3218 //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); 3219 if ((track->sharedBuffer() != 0) || 3220 track->isStopped() || track->isPaused()) { 3221 // We have consumed all the buffers of this track. 3222 // Remove it from the list of active tracks. 3223 // TODO: use actual buffer filling status instead of latency when available from 3224 // audio HAL 3225 size_t audioHALFrames = 3226 (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 3227 size_t framesWritten = 3228 mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); 3229 if (track->presentationComplete(framesWritten, audioHALFrames)) { 3230 if (track->isStopped()) { 3231 track->reset(); 3232 } 3233 tracksToRemove->add(track); 3234 } 3235 } else { 3236 track->mUnderrunCount++; 3237 // No buffers for this track. Give it a few chances to 3238 // fill a buffer, then remove it from active list. 3239 if (--(track->mRetryCount) <= 0 || track->isTerminated()) { 3240 ALOGV_IF(track->mRetryCount <= 0, "BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); 3241 tracksToRemove->add(track); 3242 // indicate to client process that the track was disabled because of underrun; 3243 // it will then automatically call start() when data is available 3244 android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); 3245 // If one track is not ready, mark the mixer also not ready if: 3246 // - the mixer was ready during previous round OR 3247 // - no other track is ready 3248 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || 3249 mixerStatus != MIXER_TRACKS_READY) { 3250 mixerStatus = MIXER_TRACKS_ENABLED; 3251 } 3252 } 3253 mAudioMixer->disable(name); 3254 } 3255 3256 } // local variable scope to avoid goto warning 3257track_is_ready: ; 3258 3259 } 3260 3261 // Push the new FastMixer state if necessary 3262 bool pauseAudioWatchdog = false; 3263 if (didModify) { 3264 state->mFastTracksGen++; 3265 // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle 3266 if (kUseFastMixer == FastMixer_Dynamic && 3267 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { 3268 state->mCommand = FastMixerState::COLD_IDLE; 3269 state->mColdFutexAddr = &mFastMixerFutex; 3270 state->mColdGen++; 3271 mFastMixerFutex = 0; 3272 if (kUseFastMixer == FastMixer_Dynamic) { 3273 mNormalSink = mOutputSink; 3274 } 3275 // If we go into cold idle, need to wait for acknowledgement 3276 // so that fast mixer stops doing I/O. 3277 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; 3278 pauseAudioWatchdog = true; 3279 } 3280 sq->end(); 3281 } 3282 if (sq != NULL) { 3283 sq->end(didModify); 3284 sq->push(block); 3285 } 3286 if (pauseAudioWatchdog && mAudioWatchdog != 0) { 3287 mAudioWatchdog->pause(); 3288 } 3289 3290 // Now perform the deferred reset on fast tracks that have stopped 3291 while (resetMask != 0) { 3292 size_t i = __builtin_ctz(resetMask); 3293 ALOG_ASSERT(i < count); 3294 resetMask &= ~(1 << i); 3295 sp<Track> t = mActiveTracks[i].promote(); 3296 if (t == 0) continue; 3297 Track* track = t.get(); 3298 ALOG_ASSERT(track->isFastTrack() && track->isStopped()); 3299 track->reset(); 3300 } 3301 3302 // remove all the tracks that need to be... 3303 count = tracksToRemove->size(); 3304 if (CC_UNLIKELY(count)) { 3305 for (size_t i=0 ; i<count ; i++) { 3306 const sp<Track>& track = tracksToRemove->itemAt(i); 3307 mActiveTracks.remove(track); 3308 if (track->mainBuffer() != mMixBuffer) { 3309 chain = getEffectChain_l(track->sessionId()); 3310 if (chain != 0) { 3311 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); 3312 chain->decActiveTrackCnt(); 3313 } 3314 } 3315 if (track->isTerminated()) { 3316 removeTrack_l(track); 3317 } 3318 } 3319 } 3320 3321 // mix buffer must be cleared if all tracks are connected to an 3322 // effect chain as in this case the mixer will not write to 3323 // mix buffer and track effects will accumulate into it 3324 if ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || (mixedTracks == 0 && fastTracks > 0)) { 3325 // FIXME as a performance optimization, should remember previous zero status 3326 memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); 3327 } 3328 3329 // if any fast tracks, then status is ready 3330 mMixerStatusIgnoringFastTracks = mixerStatus; 3331 if (fastTracks > 0) { 3332 mixerStatus = MIXER_TRACKS_READY; 3333 } 3334 return mixerStatus; 3335} 3336 3337/* 3338The derived values that are cached: 3339 - mixBufferSize from frame count * frame size 3340 - activeSleepTime from activeSleepTimeUs() 3341 - idleSleepTime from idleSleepTimeUs() 3342 - standbyDelay from mActiveSleepTimeUs (DIRECT only) 3343 - maxPeriod from frame count and sample rate (MIXER only) 3344 3345The parameters that affect these derived values are: 3346 - frame count 3347 - frame size 3348 - sample rate 3349 - device type: A2DP or not 3350 - device latency 3351 - format: PCM or not 3352 - active sleep time 3353 - idle sleep time 3354*/ 3355 3356void AudioFlinger::PlaybackThread::cacheParameters_l() 3357{ 3358 mixBufferSize = mNormalFrameCount * mFrameSize; 3359 activeSleepTime = activeSleepTimeUs(); 3360 idleSleepTime = idleSleepTimeUs(); 3361} 3362 3363void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) 3364{ 3365 ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", 3366 this, streamType, mTracks.size()); 3367 Mutex::Autolock _l(mLock); 3368 3369 size_t size = mTracks.size(); 3370 for (size_t i = 0; i < size; i++) { 3371 sp<Track> t = mTracks[i]; 3372 if (t->streamType() == streamType) { 3373 android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); 3374 t->mCblk->cv.signal(); 3375 } 3376 } 3377} 3378 3379// getTrackName_l() must be called with ThreadBase::mLock held 3380int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask) 3381{ 3382 return mAudioMixer->getTrackName(channelMask); 3383} 3384 3385// deleteTrackName_l() must be called with ThreadBase::mLock held 3386void AudioFlinger::MixerThread::deleteTrackName_l(int name) 3387{ 3388 ALOGV("remove track (%d) and delete from mixer", name); 3389 mAudioMixer->deleteTrackName(name); 3390} 3391 3392// checkForNewParameters_l() must be called with ThreadBase::mLock held 3393bool AudioFlinger::MixerThread::checkForNewParameters_l() 3394{ 3395 // if !&IDLE, holds the FastMixer state to restore after new parameters processed 3396 FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; 3397 bool reconfig = false; 3398 3399 while (!mNewParameters.isEmpty()) { 3400 3401 if (mFastMixer != NULL) { 3402 FastMixerStateQueue *sq = mFastMixer->sq(); 3403 FastMixerState *state = sq->begin(); 3404 if (!(state->mCommand & FastMixerState::IDLE)) { 3405 previousCommand = state->mCommand; 3406 state->mCommand = FastMixerState::HOT_IDLE; 3407 sq->end(); 3408 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); 3409 } else { 3410 sq->end(false /*didModify*/); 3411 } 3412 } 3413 3414 status_t status = NO_ERROR; 3415 String8 keyValuePair = mNewParameters[0]; 3416 AudioParameter param = AudioParameter(keyValuePair); 3417 int value; 3418 3419 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 3420 reconfig = true; 3421 } 3422 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 3423 if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { 3424 status = BAD_VALUE; 3425 } else { 3426 reconfig = true; 3427 } 3428 } 3429 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 3430 if (value != AUDIO_CHANNEL_OUT_STEREO) { 3431 status = BAD_VALUE; 3432 } else { 3433 reconfig = true; 3434 } 3435 } 3436 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 3437 // do not accept frame count changes if tracks are open as the track buffer 3438 // size depends on frame count and correct behavior would not be guaranteed 3439 // if frame count is changed after track creation 3440 if (!mTracks.isEmpty()) { 3441 status = INVALID_OPERATION; 3442 } else { 3443 reconfig = true; 3444 } 3445 } 3446 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 3447#ifdef ADD_BATTERY_DATA 3448 // when changing the audio output device, call addBatteryData to notify 3449 // the change 3450 if (mDevice != value) { 3451 uint32_t params = 0; 3452 // check whether speaker is on 3453 if (value & AUDIO_DEVICE_OUT_SPEAKER) { 3454 params |= IMediaPlayerService::kBatteryDataSpeakerOn; 3455 } 3456 3457 audio_devices_t deviceWithoutSpeaker 3458 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; 3459 // check if any other device (except speaker) is on 3460 if (value & deviceWithoutSpeaker ) { 3461 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; 3462 } 3463 3464 if (params != 0) { 3465 addBatteryData(params); 3466 } 3467 } 3468#endif 3469 3470 // forward device change to effects that have requested to be 3471 // aware of attached audio device. 3472 mDevice = value; 3473 for (size_t i = 0; i < mEffectChains.size(); i++) { 3474 mEffectChains[i]->setDevice_l(mDevice); 3475 } 3476 } 3477 3478 if (status == NO_ERROR) { 3479 status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3480 keyValuePair.string()); 3481 if (!mStandby && status == INVALID_OPERATION) { 3482 mOutput->stream->common.standby(&mOutput->stream->common); 3483 mStandby = true; 3484 mBytesWritten = 0; 3485 status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3486 keyValuePair.string()); 3487 } 3488 if (status == NO_ERROR && reconfig) { 3489 delete mAudioMixer; 3490 // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't) 3491 mAudioMixer = NULL; 3492 readOutputParameters(); 3493 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); 3494 for (size_t i = 0; i < mTracks.size() ; i++) { 3495 int name = getTrackName_l(mTracks[i]->mChannelMask); 3496 if (name < 0) break; 3497 mTracks[i]->mName = name; 3498 // limit track sample rate to 2 x new output sample rate 3499 if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { 3500 mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); 3501 } 3502 } 3503 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); 3504 } 3505 } 3506 3507 mNewParameters.removeAt(0); 3508 3509 mParamStatus = status; 3510 mParamCond.signal(); 3511 // wait for condition with time out in case the thread calling ThreadBase::setParameters() 3512 // already timed out waiting for the status and will never signal the condition. 3513 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); 3514 } 3515 3516 if (!(previousCommand & FastMixerState::IDLE)) { 3517 ALOG_ASSERT(mFastMixer != NULL); 3518 FastMixerStateQueue *sq = mFastMixer->sq(); 3519 FastMixerState *state = sq->begin(); 3520 ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); 3521 state->mCommand = previousCommand; 3522 sq->end(); 3523 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); 3524 } 3525 3526 return reconfig; 3527} 3528 3529void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) 3530{ 3531 const size_t SIZE = 256; 3532 char buffer[SIZE]; 3533 String8 result; 3534 3535 PlaybackThread::dumpInternals(fd, args); 3536 3537 snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); 3538 result.append(buffer); 3539 write(fd, result.string(), result.size()); 3540 3541 // Make a non-atomic copy of fast mixer dump state so it won't change underneath us 3542 FastMixerDumpState copy = mFastMixerDumpState; 3543 copy.dump(fd); 3544 3545#ifdef STATE_QUEUE_DUMP 3546 // Similar for state queue 3547 StateQueueObserverDump observerCopy = mStateQueueObserverDump; 3548 observerCopy.dump(fd); 3549 StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; 3550 mutatorCopy.dump(fd); 3551#endif 3552 3553 // Write the tee output to a .wav file 3554 NBAIO_Source *teeSource = mTeeSource.get(); 3555 if (teeSource != NULL) { 3556 char teePath[64]; 3557 struct timeval tv; 3558 gettimeofday(&tv, NULL); 3559 struct tm tm; 3560 localtime_r(&tv.tv_sec, &tm); 3561 strftime(teePath, sizeof(teePath), "/data/misc/media/%T.wav", &tm); 3562 int teeFd = open(teePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); 3563 if (teeFd >= 0) { 3564 char wavHeader[44]; 3565 memcpy(wavHeader, 3566 "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0", 3567 sizeof(wavHeader)); 3568 NBAIO_Format format = teeSource->format(); 3569 unsigned channelCount = Format_channelCount(format); 3570 ALOG_ASSERT(channelCount <= FCC_2); 3571 unsigned sampleRate = Format_sampleRate(format); 3572 wavHeader[22] = channelCount; // number of channels 3573 wavHeader[24] = sampleRate; // sample rate 3574 wavHeader[25] = sampleRate >> 8; 3575 wavHeader[32] = channelCount * 2; // block alignment 3576 write(teeFd, wavHeader, sizeof(wavHeader)); 3577 size_t total = 0; 3578 bool firstRead = true; 3579 for (;;) { 3580#define TEE_SINK_READ 1024 3581 short buffer[TEE_SINK_READ * FCC_2]; 3582 size_t count = TEE_SINK_READ; 3583 ssize_t actual = teeSource->read(buffer, count, 3584 AudioBufferProvider::kInvalidPTS); 3585 bool wasFirstRead = firstRead; 3586 firstRead = false; 3587 if (actual <= 0) { 3588 if (actual == (ssize_t) OVERRUN && wasFirstRead) { 3589 continue; 3590 } 3591 break; 3592 } 3593 ALOG_ASSERT(actual <= (ssize_t)count); 3594 write(teeFd, buffer, actual * channelCount * sizeof(short)); 3595 total += actual; 3596 } 3597 lseek(teeFd, (off_t) 4, SEEK_SET); 3598 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8; 3599 write(teeFd, &temp, sizeof(temp)); 3600 lseek(teeFd, (off_t) 40, SEEK_SET); 3601 temp = total * channelCount * sizeof(short); 3602 write(teeFd, &temp, sizeof(temp)); 3603 close(teeFd); 3604 fdprintf(fd, "FastMixer tee copied to %s\n", teePath); 3605 } else { 3606 fdprintf(fd, "FastMixer unable to create tee %s: \n", strerror(errno)); 3607 } 3608 } 3609 3610 if (mAudioWatchdog != 0) { 3611 // Make a non-atomic copy of audio watchdog dump so it won't change underneath us 3612 AudioWatchdogDump wdCopy = mAudioWatchdogDump; 3613 wdCopy.dump(fd); 3614 } 3615} 3616 3617uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const 3618{ 3619 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; 3620} 3621 3622uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const 3623{ 3624 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); 3625} 3626 3627void AudioFlinger::MixerThread::cacheParameters_l() 3628{ 3629 PlaybackThread::cacheParameters_l(); 3630 3631 // FIXME: Relaxed timing because of a certain device that can't meet latency 3632 // Should be reduced to 2x after the vendor fixes the driver issue 3633 // increase threshold again due to low power audio mode. The way this warning 3634 // threshold is calculated and its usefulness should be reconsidered anyway. 3635 maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; 3636} 3637 3638// ---------------------------------------------------------------------------- 3639AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, 3640 AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) 3641 : PlaybackThread(audioFlinger, output, id, device, DIRECT) 3642 // mLeftVolFloat, mRightVolFloat 3643{ 3644} 3645 3646AudioFlinger::DirectOutputThread::~DirectOutputThread() 3647{ 3648} 3649 3650AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( 3651 Vector< sp<Track> > *tracksToRemove 3652) 3653{ 3654 sp<Track> trackToRemove; 3655 3656 mixer_state mixerStatus = MIXER_IDLE; 3657 3658 // find out which tracks need to be processed 3659 if (mActiveTracks.size() != 0) { 3660 sp<Track> t = mActiveTracks[0].promote(); 3661 // The track died recently 3662 if (t == 0) return MIXER_IDLE; 3663 3664 Track* const track = t.get(); 3665 audio_track_cblk_t* cblk = track->cblk(); 3666 3667 // The first time a track is added we wait 3668 // for all its buffers to be filled before processing it 3669 uint32_t minFrames; 3670 if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing()) { 3671 minFrames = mNormalFrameCount; 3672 } else { 3673 minFrames = 1; 3674 } 3675 if ((track->framesReady() >= minFrames) && track->isReady() && 3676 !track->isPaused() && !track->isTerminated()) 3677 { 3678 //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); 3679 3680 if (track->mFillingUpStatus == Track::FS_FILLED) { 3681 track->mFillingUpStatus = Track::FS_ACTIVE; 3682 mLeftVolFloat = mRightVolFloat = 0; 3683 if (track->mState == TrackBase::RESUMING) { 3684 track->mState = TrackBase::ACTIVE; 3685 } 3686 } 3687 3688 // compute volume for this track 3689 float left, right; 3690 if (track->isMuted() || mMasterMute || track->isPausing() || 3691 mStreamTypes[track->streamType()].mute) { 3692 left = right = 0; 3693 if (track->isPausing()) { 3694 track->setPaused(); 3695 } 3696 } else { 3697 float typeVolume = mStreamTypes[track->streamType()].volume; 3698 float v = mMasterVolume * typeVolume; 3699 uint32_t vlr = cblk->getVolumeLR(); 3700 float v_clamped = v * (vlr & 0xFFFF); 3701 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; 3702 left = v_clamped/MAX_GAIN; 3703 v_clamped = v * (vlr >> 16); 3704 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; 3705 right = v_clamped/MAX_GAIN; 3706 } 3707 3708 if (left != mLeftVolFloat || right != mRightVolFloat) { 3709 mLeftVolFloat = left; 3710 mRightVolFloat = right; 3711 3712 // Convert volumes from float to 8.24 3713 uint32_t vl = (uint32_t)(left * (1 << 24)); 3714 uint32_t vr = (uint32_t)(right * (1 << 24)); 3715 3716 // Delegate volume control to effect in track effect chain if needed 3717 // only one effect chain can be present on DirectOutputThread, so if 3718 // there is one, the track is connected to it 3719 if (!mEffectChains.isEmpty()) { 3720 // Do not ramp volume if volume is controlled by effect 3721 mEffectChains[0]->setVolume_l(&vl, &vr); 3722 left = (float)vl / (1 << 24); 3723 right = (float)vr / (1 << 24); 3724 } 3725 mOutput->stream->set_volume(mOutput->stream, left, right); 3726 } 3727 3728 // reset retry count 3729 track->mRetryCount = kMaxTrackRetriesDirect; 3730 mActiveTrack = t; 3731 mixerStatus = MIXER_TRACKS_READY; 3732 } else { 3733 // clear effect chain input buffer if an active track underruns to avoid sending 3734 // previous audio buffer again to effects 3735 if (!mEffectChains.isEmpty()) { 3736 mEffectChains[0]->clearInputBuffer(); 3737 } 3738 3739 //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); 3740 if ((track->sharedBuffer() != 0) || 3741 track->isStopped() || track->isPaused()) { 3742 // We have consumed all the buffers of this track. 3743 // Remove it from the list of active tracks. 3744 // TODO: implement behavior for compressed audio 3745 size_t audioHALFrames = 3746 (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; 3747 size_t framesWritten = 3748 mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); 3749 if (track->presentationComplete(framesWritten, audioHALFrames)) { 3750 if (track->isStopped()) { 3751 track->reset(); 3752 } 3753 trackToRemove = track; 3754 } 3755 } else { 3756 // No buffers for this track. Give it a few chances to 3757 // fill a buffer, then remove it from active list. 3758 if (--(track->mRetryCount) <= 0 || track->isTerminated()) { 3759 ALOGV_IF(track->mRetryCount <= 0, "BUFFER TIMEOUT: remove(%d) from active list", track->name()); 3760 trackToRemove = track; 3761 } else { 3762 mixerStatus = MIXER_TRACKS_ENABLED; 3763 } 3764 } 3765 } 3766 } 3767 3768 // FIXME merge this with similar code for removing multiple tracks 3769 // remove all the tracks that need to be... 3770 if (CC_UNLIKELY(trackToRemove != 0)) { 3771 tracksToRemove->add(trackToRemove); 3772 mActiveTracks.remove(trackToRemove); 3773 if (!mEffectChains.isEmpty()) { 3774 ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(), 3775 trackToRemove->sessionId()); 3776 mEffectChains[0]->decActiveTrackCnt(); 3777 } 3778 if (trackToRemove->isTerminated()) { 3779 removeTrack_l(trackToRemove); 3780 } 3781 } 3782 3783 return mixerStatus; 3784} 3785 3786void AudioFlinger::DirectOutputThread::threadLoop_mix() 3787{ 3788 AudioBufferProvider::Buffer buffer; 3789 size_t frameCount = mFrameCount; 3790 int8_t *curBuf = (int8_t *)mMixBuffer; 3791 // output audio to hardware 3792 while (frameCount) { 3793 buffer.frameCount = frameCount; 3794 mActiveTrack->getNextBuffer(&buffer); 3795 if (CC_UNLIKELY(buffer.raw == NULL)) { 3796 memset(curBuf, 0, frameCount * mFrameSize); 3797 break; 3798 } 3799 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); 3800 frameCount -= buffer.frameCount; 3801 curBuf += buffer.frameCount * mFrameSize; 3802 mActiveTrack->releaseBuffer(&buffer); 3803 } 3804 sleepTime = 0; 3805 standbyTime = systemTime() + standbyDelay; 3806 mActiveTrack.clear(); 3807 3808} 3809 3810void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() 3811{ 3812 if (sleepTime == 0) { 3813 if (mMixerStatus == MIXER_TRACKS_ENABLED) { 3814 sleepTime = activeSleepTime; 3815 } else { 3816 sleepTime = idleSleepTime; 3817 } 3818 } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { 3819 memset(mMixBuffer, 0, mFrameCount * mFrameSize); 3820 sleepTime = 0; 3821 } 3822} 3823 3824// getTrackName_l() must be called with ThreadBase::mLock held 3825int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask) 3826{ 3827 return 0; 3828} 3829 3830// deleteTrackName_l() must be called with ThreadBase::mLock held 3831void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) 3832{ 3833} 3834 3835// checkForNewParameters_l() must be called with ThreadBase::mLock held 3836bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() 3837{ 3838 bool reconfig = false; 3839 3840 while (!mNewParameters.isEmpty()) { 3841 status_t status = NO_ERROR; 3842 String8 keyValuePair = mNewParameters[0]; 3843 AudioParameter param = AudioParameter(keyValuePair); 3844 int value; 3845 3846 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 3847 // do not accept frame count changes if tracks are open as the track buffer 3848 // size depends on frame count and correct behavior would not be garantied 3849 // if frame count is changed after track creation 3850 if (!mTracks.isEmpty()) { 3851 status = INVALID_OPERATION; 3852 } else { 3853 reconfig = true; 3854 } 3855 } 3856 if (status == NO_ERROR) { 3857 status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3858 keyValuePair.string()); 3859 if (!mStandby && status == INVALID_OPERATION) { 3860 mOutput->stream->common.standby(&mOutput->stream->common); 3861 mStandby = true; 3862 mBytesWritten = 0; 3863 status = mOutput->stream->common.set_parameters(&mOutput->stream->common, 3864 keyValuePair.string()); 3865 } 3866 if (status == NO_ERROR && reconfig) { 3867 readOutputParameters(); 3868 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); 3869 } 3870 } 3871 3872 mNewParameters.removeAt(0); 3873 3874 mParamStatus = status; 3875 mParamCond.signal(); 3876 // wait for condition with time out in case the thread calling ThreadBase::setParameters() 3877 // already timed out waiting for the status and will never signal the condition. 3878 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); 3879 } 3880 return reconfig; 3881} 3882 3883uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const 3884{ 3885 uint32_t time; 3886 if (audio_is_linear_pcm(mFormat)) { 3887 time = PlaybackThread::activeSleepTimeUs(); 3888 } else { 3889 time = 10000; 3890 } 3891 return time; 3892} 3893 3894uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const 3895{ 3896 uint32_t time; 3897 if (audio_is_linear_pcm(mFormat)) { 3898 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; 3899 } else { 3900 time = 10000; 3901 } 3902 return time; 3903} 3904 3905uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const 3906{ 3907 uint32_t time; 3908 if (audio_is_linear_pcm(mFormat)) { 3909 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); 3910 } else { 3911 time = 10000; 3912 } 3913 return time; 3914} 3915 3916void AudioFlinger::DirectOutputThread::cacheParameters_l() 3917{ 3918 PlaybackThread::cacheParameters_l(); 3919 3920 // use shorter standby delay as on normal output to release 3921 // hardware resources as soon as possible 3922 standbyDelay = microseconds(activeSleepTime*2); 3923} 3924 3925// ---------------------------------------------------------------------------- 3926 3927AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, 3928 AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) 3929 : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING), 3930 mWaitTimeMs(UINT_MAX) 3931{ 3932 addOutputTrack(mainThread); 3933} 3934 3935AudioFlinger::DuplicatingThread::~DuplicatingThread() 3936{ 3937 for (size_t i = 0; i < mOutputTracks.size(); i++) { 3938 mOutputTracks[i]->destroy(); 3939 } 3940} 3941 3942void AudioFlinger::DuplicatingThread::threadLoop_mix() 3943{ 3944 // mix buffers... 3945 if (outputsReady(outputTracks)) { 3946 mAudioMixer->process(AudioBufferProvider::kInvalidPTS); 3947 } else { 3948 memset(mMixBuffer, 0, mixBufferSize); 3949 } 3950 sleepTime = 0; 3951 writeFrames = mNormalFrameCount; 3952 standbyTime = systemTime() + standbyDelay; 3953} 3954 3955void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() 3956{ 3957 if (sleepTime == 0) { 3958 if (mMixerStatus == MIXER_TRACKS_ENABLED) { 3959 sleepTime = activeSleepTime; 3960 } else { 3961 sleepTime = idleSleepTime; 3962 } 3963 } else if (mBytesWritten != 0) { 3964 if (mMixerStatus == MIXER_TRACKS_ENABLED) { 3965 writeFrames = mNormalFrameCount; 3966 memset(mMixBuffer, 0, mixBufferSize); 3967 } else { 3968 // flush remaining overflow buffers in output tracks 3969 writeFrames = 0; 3970 } 3971 sleepTime = 0; 3972 } 3973} 3974 3975void AudioFlinger::DuplicatingThread::threadLoop_write() 3976{ 3977 for (size_t i = 0; i < outputTracks.size(); i++) { 3978 outputTracks[i]->write(mMixBuffer, writeFrames); 3979 } 3980 mBytesWritten += mixBufferSize; 3981} 3982 3983void AudioFlinger::DuplicatingThread::threadLoop_standby() 3984{ 3985 // DuplicatingThread implements standby by stopping all tracks 3986 for (size_t i = 0; i < outputTracks.size(); i++) { 3987 outputTracks[i]->stop(); 3988 } 3989} 3990 3991void AudioFlinger::DuplicatingThread::saveOutputTracks() 3992{ 3993 outputTracks = mOutputTracks; 3994} 3995 3996void AudioFlinger::DuplicatingThread::clearOutputTracks() 3997{ 3998 outputTracks.clear(); 3999} 4000 4001void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) 4002{ 4003 Mutex::Autolock _l(mLock); 4004 // FIXME explain this formula 4005 int frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); 4006 OutputTrack *outputTrack = new OutputTrack(thread, 4007 this, 4008 mSampleRate, 4009 mFormat, 4010 mChannelMask, 4011 frameCount); 4012 if (outputTrack->cblk() != NULL) { 4013 thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); 4014 mOutputTracks.add(outputTrack); 4015 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); 4016 updateWaitTime_l(); 4017 } 4018} 4019 4020void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) 4021{ 4022 Mutex::Autolock _l(mLock); 4023 for (size_t i = 0; i < mOutputTracks.size(); i++) { 4024 if (mOutputTracks[i]->thread() == thread) { 4025 mOutputTracks[i]->destroy(); 4026 mOutputTracks.removeAt(i); 4027 updateWaitTime_l(); 4028 return; 4029 } 4030 } 4031 ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); 4032} 4033 4034// caller must hold mLock 4035void AudioFlinger::DuplicatingThread::updateWaitTime_l() 4036{ 4037 mWaitTimeMs = UINT_MAX; 4038 for (size_t i = 0; i < mOutputTracks.size(); i++) { 4039 sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); 4040 if (strong != 0) { 4041 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); 4042 if (waitTimeMs < mWaitTimeMs) { 4043 mWaitTimeMs = waitTimeMs; 4044 } 4045 } 4046 } 4047} 4048 4049 4050bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks) 4051{ 4052 for (size_t i = 0; i < outputTracks.size(); i++) { 4053 sp<ThreadBase> thread = outputTracks[i]->thread().promote(); 4054 if (thread == 0) { 4055 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); 4056 return false; 4057 } 4058 PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 4059 // see note at standby() declaration 4060 if (playbackThread->standby() && !playbackThread->isSuspended()) { 4061 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); 4062 return false; 4063 } 4064 } 4065 return true; 4066} 4067 4068uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const 4069{ 4070 return (mWaitTimeMs * 1000) / 2; 4071} 4072 4073void AudioFlinger::DuplicatingThread::cacheParameters_l() 4074{ 4075 // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first 4076 updateWaitTime_l(); 4077 4078 MixerThread::cacheParameters_l(); 4079} 4080 4081// ---------------------------------------------------------------------------- 4082 4083// TrackBase constructor must be called with AudioFlinger::mLock held 4084AudioFlinger::ThreadBase::TrackBase::TrackBase( 4085 ThreadBase *thread, 4086 const sp<Client>& client, 4087 uint32_t sampleRate, 4088 audio_format_t format, 4089 audio_channel_mask_t channelMask, 4090 int frameCount, 4091 const sp<IMemory>& sharedBuffer, 4092 int sessionId) 4093 : RefBase(), 4094 mThread(thread), 4095 mClient(client), 4096 mCblk(NULL), 4097 // mBuffer 4098 // mBufferEnd 4099 mFrameCount(0), 4100 mState(IDLE), 4101 mSampleRate(sampleRate), 4102 mFormat(format), 4103 mStepServerFailed(false), 4104 mSessionId(sessionId) 4105 // mChannelCount 4106 // mChannelMask 4107{ 4108 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); 4109 4110 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); 4111 size_t size = sizeof(audio_track_cblk_t); 4112 uint8_t channelCount = popcount(channelMask); 4113 size_t bufferSize = frameCount*channelCount*sizeof(int16_t); 4114 if (sharedBuffer == 0) { 4115 size += bufferSize; 4116 } 4117 4118 if (client != NULL) { 4119 mCblkMemory = client->heap()->allocate(size); 4120 if (mCblkMemory != 0) { 4121 mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); 4122 if (mCblk != NULL) { // construct the shared structure in-place. 4123 new(mCblk) audio_track_cblk_t(); 4124 // clear all buffers 4125 mCblk->frameCount = frameCount; 4126 mCblk->sampleRate = sampleRate; 4127// uncomment the following lines to quickly test 32-bit wraparound 4128// mCblk->user = 0xffff0000; 4129// mCblk->server = 0xffff0000; 4130// mCblk->userBase = 0xffff0000; 4131// mCblk->serverBase = 0xffff0000; 4132 mChannelCount = channelCount; 4133 mChannelMask = channelMask; 4134 if (sharedBuffer == 0) { 4135 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); 4136 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); 4137 // Force underrun condition to avoid false underrun callback until first data is 4138 // written to buffer (other flags are cleared) 4139 mCblk->flags = CBLK_UNDERRUN_ON; 4140 } else { 4141 mBuffer = sharedBuffer->pointer(); 4142 } 4143 mBufferEnd = (uint8_t *)mBuffer + bufferSize; 4144 } 4145 } else { 4146 ALOGE("not enough memory for AudioTrack size=%u", size); 4147 client->heap()->dump("AudioTrack"); 4148 return; 4149 } 4150 } else { 4151 mCblk = (audio_track_cblk_t *)(new uint8_t[size]); 4152 // construct the shared structure in-place. 4153 new(mCblk) audio_track_cblk_t(); 4154 // clear all buffers 4155 mCblk->frameCount = frameCount; 4156 mCblk->sampleRate = sampleRate; 4157// uncomment the following lines to quickly test 32-bit wraparound 4158// mCblk->user = 0xffff0000; 4159// mCblk->server = 0xffff0000; 4160// mCblk->userBase = 0xffff0000; 4161// mCblk->serverBase = 0xffff0000; 4162 mChannelCount = channelCount; 4163 mChannelMask = channelMask; 4164 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); 4165 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); 4166 // Force underrun condition to avoid false underrun callback until first data is 4167 // written to buffer (other flags are cleared) 4168 mCblk->flags = CBLK_UNDERRUN_ON; 4169 mBufferEnd = (uint8_t *)mBuffer + bufferSize; 4170 } 4171} 4172 4173AudioFlinger::ThreadBase::TrackBase::~TrackBase() 4174{ 4175 if (mCblk != NULL) { 4176 if (mClient == 0) { 4177 delete mCblk; 4178 } else { 4179 mCblk->~audio_track_cblk_t(); // destroy our shared-structure. 4180 } 4181 } 4182 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to 4183 if (mClient != 0) { 4184 // Client destructor must run with AudioFlinger mutex locked 4185 Mutex::Autolock _l(mClient->audioFlinger()->mLock); 4186 // If the client's reference count drops to zero, the associated destructor 4187 // must run with AudioFlinger lock held. Thus the explicit clear() rather than 4188 // relying on the automatic clear() at end of scope. 4189 mClient.clear(); 4190 } 4191} 4192 4193// AudioBufferProvider interface 4194// getNextBuffer() = 0; 4195// This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack 4196void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) 4197{ 4198 buffer->raw = NULL; 4199 mFrameCount = buffer->frameCount; 4200 // FIXME See note at getNextBuffer() 4201 (void) step(); // ignore return value of step() 4202 buffer->frameCount = 0; 4203} 4204 4205bool AudioFlinger::ThreadBase::TrackBase::step() { 4206 bool result; 4207 audio_track_cblk_t* cblk = this->cblk(); 4208 4209 result = cblk->stepServer(mFrameCount); 4210 if (!result) { 4211 ALOGV("stepServer failed acquiring cblk mutex"); 4212 mStepServerFailed = true; 4213 } 4214 return result; 4215} 4216 4217void AudioFlinger::ThreadBase::TrackBase::reset() { 4218 audio_track_cblk_t* cblk = this->cblk(); 4219 4220 cblk->user = 0; 4221 cblk->server = 0; 4222 cblk->userBase = 0; 4223 cblk->serverBase = 0; 4224 mStepServerFailed = false; 4225 ALOGV("TrackBase::reset"); 4226} 4227 4228int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { 4229 return (int)mCblk->sampleRate; 4230} 4231 4232void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { 4233 audio_track_cblk_t* cblk = this->cblk(); 4234 size_t frameSize = cblk->frameSize; 4235 int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; 4236 int8_t *bufferEnd = bufferStart + frames * frameSize; 4237 4238 // Check validity of returned pointer in case the track control block would have been corrupted. 4239 ALOG_ASSERT(!(bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd), 4240 "TrackBase::getBuffer buffer out of range:\n" 4241 " start: %p, end %p , mBuffer %p mBufferEnd %p\n" 4242 " server %u, serverBase %u, user %u, userBase %u, frameSize %d", 4243 bufferStart, bufferEnd, mBuffer, mBufferEnd, 4244 cblk->server, cblk->serverBase, cblk->user, cblk->userBase, frameSize); 4245 4246 return bufferStart; 4247} 4248 4249status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event) 4250{ 4251 mSyncEvents.add(event); 4252 return NO_ERROR; 4253} 4254 4255// ---------------------------------------------------------------------------- 4256 4257// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held 4258AudioFlinger::PlaybackThread::Track::Track( 4259 PlaybackThread *thread, 4260 const sp<Client>& client, 4261 audio_stream_type_t streamType, 4262 uint32_t sampleRate, 4263 audio_format_t format, 4264 audio_channel_mask_t channelMask, 4265 int frameCount, 4266 const sp<IMemory>& sharedBuffer, 4267 int sessionId, 4268 IAudioFlinger::track_flags_t flags) 4269 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId), 4270 mMute(false), 4271 mFillingUpStatus(FS_INVALID), 4272 // mRetryCount initialized later when needed 4273 mSharedBuffer(sharedBuffer), 4274 mStreamType(streamType), 4275 mName(-1), // see note below 4276 mMainBuffer(thread->mixBuffer()), 4277 mAuxBuffer(NULL), 4278 mAuxEffectId(0), mHasVolumeController(false), 4279 mPresentationCompleteFrames(0), 4280 mFlags(flags), 4281 mFastIndex(-1), 4282 mUnderrunCount(0), 4283 mCachedVolume(1.0) 4284{ 4285 if (mCblk != NULL) { 4286 // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of 4287 // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack 4288 mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); 4289 // to avoid leaking a track name, do not allocate one unless there is an mCblk 4290 mName = thread->getTrackName_l(channelMask); 4291 mCblk->mName = mName; 4292 if (mName < 0) { 4293 ALOGE("no more track names available"); 4294 return; 4295 } 4296 // only allocate a fast track index if we were able to allocate a normal track name 4297 if (flags & IAudioFlinger::TRACK_FAST) { 4298 mCblk->flags |= CBLK_FAST; // atomic op not needed yet 4299 ALOG_ASSERT(thread->mFastTrackAvailMask != 0); 4300 int i = __builtin_ctz(thread->mFastTrackAvailMask); 4301 ALOG_ASSERT(0 < i && i < (int)FastMixerState::kMaxFastTracks); 4302 // FIXME This is too eager. We allocate a fast track index before the 4303 // fast track becomes active. Since fast tracks are a scarce resource, 4304 // this means we are potentially denying other more important fast tracks from 4305 // being created. It would be better to allocate the index dynamically. 4306 mFastIndex = i; 4307 mCblk->mName = i; 4308 // Read the initial underruns because this field is never cleared by the fast mixer 4309 mObservedUnderruns = thread->getFastTrackUnderruns(i); 4310 thread->mFastTrackAvailMask &= ~(1 << i); 4311 } 4312 } 4313 ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid()); 4314} 4315 4316AudioFlinger::PlaybackThread::Track::~Track() 4317{ 4318 ALOGV("PlaybackThread::Track destructor"); 4319} 4320 4321void AudioFlinger::PlaybackThread::Track::destroy() 4322{ 4323 // NOTE: destroyTrack_l() can remove a strong reference to this Track 4324 // by removing it from mTracks vector, so there is a risk that this Tracks's 4325 // destructor is called. As the destructor needs to lock mLock, 4326 // we must acquire a strong reference on this Track before locking mLock 4327 // here so that the destructor is called only when exiting this function. 4328 // On the other hand, as long as Track::destroy() is only called by 4329 // TrackHandle destructor, the TrackHandle still holds a strong ref on 4330 // this Track with its member mTrack. 4331 sp<Track> keep(this); 4332 { // scope for mLock 4333 sp<ThreadBase> thread = mThread.promote(); 4334 if (thread != 0) { 4335 if (!isOutputTrack()) { 4336 if (mState == ACTIVE || mState == RESUMING) { 4337 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); 4338 4339#ifdef ADD_BATTERY_DATA 4340 // to track the speaker usage 4341 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 4342#endif 4343 } 4344 AudioSystem::releaseOutput(thread->id()); 4345 } 4346 Mutex::Autolock _l(thread->mLock); 4347 PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 4348 playbackThread->destroyTrack_l(this); 4349 } 4350 } 4351} 4352 4353/*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) 4354{ 4355 result.append(" Name Client Type Fmt Chn mask Session mFrCnt fCount S M F SRate L dB R dB " 4356 " Server User Main buf Aux Buf Flags Underruns\n"); 4357} 4358 4359void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) 4360{ 4361 uint32_t vlr = mCblk->getVolumeLR(); 4362 if (isFastTrack()) { 4363 sprintf(buffer, " F %2d", mFastIndex); 4364 } else { 4365 sprintf(buffer, " %4d", mName - AudioMixer::TRACK0); 4366 } 4367 track_state state = mState; 4368 char stateChar; 4369 switch (state) { 4370 case IDLE: 4371 stateChar = 'I'; 4372 break; 4373 case TERMINATED: 4374 stateChar = 'T'; 4375 break; 4376 case STOPPING_1: 4377 stateChar = 's'; 4378 break; 4379 case STOPPING_2: 4380 stateChar = '5'; 4381 break; 4382 case STOPPED: 4383 stateChar = 'S'; 4384 break; 4385 case RESUMING: 4386 stateChar = 'R'; 4387 break; 4388 case ACTIVE: 4389 stateChar = 'A'; 4390 break; 4391 case PAUSING: 4392 stateChar = 'p'; 4393 break; 4394 case PAUSED: 4395 stateChar = 'P'; 4396 break; 4397 case FLUSHED: 4398 stateChar = 'F'; 4399 break; 4400 default: 4401 stateChar = '?'; 4402 break; 4403 } 4404 char nowInUnderrun; 4405 switch (mObservedUnderruns.mBitFields.mMostRecent) { 4406 case UNDERRUN_FULL: 4407 nowInUnderrun = ' '; 4408 break; 4409 case UNDERRUN_PARTIAL: 4410 nowInUnderrun = '<'; 4411 break; 4412 case UNDERRUN_EMPTY: 4413 nowInUnderrun = '*'; 4414 break; 4415 default: 4416 nowInUnderrun = '?'; 4417 break; 4418 } 4419 snprintf(&buffer[7], size-7, " %6d %4u %3u 0x%08x %7u %6u %6u %1c %1d %1d %5u %5.2g %5.2g " 4420 "0x%08x 0x%08x 0x%08x 0x%08x %#5x %9u%c\n", 4421 (mClient == 0) ? getpid_cached : mClient->pid(), 4422 mStreamType, 4423 mFormat, 4424 mChannelMask, 4425 mSessionId, 4426 mFrameCount, 4427 mCblk->frameCount, 4428 stateChar, 4429 mMute, 4430 mFillingUpStatus, 4431 mCblk->sampleRate, 4432 20.0 * log10((vlr & 0xFFFF) / 4096.0), 4433 20.0 * log10((vlr >> 16) / 4096.0), 4434 mCblk->server, 4435 mCblk->user, 4436 (int)mMainBuffer, 4437 (int)mAuxBuffer, 4438 mCblk->flags, 4439 mUnderrunCount, 4440 nowInUnderrun); 4441} 4442 4443// AudioBufferProvider interface 4444status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( 4445 AudioBufferProvider::Buffer* buffer, int64_t pts) 4446{ 4447 audio_track_cblk_t* cblk = this->cblk(); 4448 uint32_t framesReady; 4449 uint32_t framesReq = buffer->frameCount; 4450 4451 // Check if last stepServer failed, try to step now 4452 if (mStepServerFailed) { 4453 // FIXME When called by fast mixer, this takes a mutex with tryLock(). 4454 // Since the fast mixer is higher priority than client callback thread, 4455 // it does not result in priority inversion for client. 4456 // But a non-blocking solution would be preferable to avoid 4457 // fast mixer being unable to tryLock(), and 4458 // to avoid the extra context switches if the client wakes up, 4459 // discovers the mutex is locked, then has to wait for fast mixer to unlock. 4460 if (!step()) goto getNextBuffer_exit; 4461 ALOGV("stepServer recovered"); 4462 mStepServerFailed = false; 4463 } 4464 4465 // FIXME Same as above 4466 framesReady = cblk->framesReady(); 4467 4468 if (CC_LIKELY(framesReady)) { 4469 uint32_t s = cblk->server; 4470 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; 4471 4472 bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; 4473 if (framesReq > framesReady) { 4474 framesReq = framesReady; 4475 } 4476 if (framesReq > bufferEnd - s) { 4477 framesReq = bufferEnd - s; 4478 } 4479 4480 buffer->raw = getBuffer(s, framesReq); 4481 buffer->frameCount = framesReq; 4482 return NO_ERROR; 4483 } 4484 4485getNextBuffer_exit: 4486 buffer->raw = NULL; 4487 buffer->frameCount = 0; 4488 ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); 4489 return NOT_ENOUGH_DATA; 4490} 4491 4492// Note that framesReady() takes a mutex on the control block using tryLock(). 4493// This could result in priority inversion if framesReady() is called by the normal mixer, 4494// as the normal mixer thread runs at lower 4495// priority than the client's callback thread: there is a short window within framesReady() 4496// during which the normal mixer could be preempted, and the client callback would block. 4497// Another problem can occur if framesReady() is called by the fast mixer: 4498// the tryLock() could block for up to 1 ms, and a sequence of these could delay fast mixer. 4499// FIXME Replace AudioTrackShared control block implementation by a non-blocking FIFO queue. 4500size_t AudioFlinger::PlaybackThread::Track::framesReady() const { 4501 return mCblk->framesReady(); 4502} 4503 4504// Don't call for fast tracks; the framesReady() could result in priority inversion 4505bool AudioFlinger::PlaybackThread::Track::isReady() const { 4506 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; 4507 4508 if (framesReady() >= mCblk->frameCount || 4509 (mCblk->flags & CBLK_FORCEREADY_MSK)) { 4510 mFillingUpStatus = FS_FILLED; 4511 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); 4512 return true; 4513 } 4514 return false; 4515} 4516 4517status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event, 4518 int triggerSession) 4519{ 4520 status_t status = NO_ERROR; 4521 ALOGV("start(%d), calling pid %d session %d", 4522 mName, IPCThreadState::self()->getCallingPid(), mSessionId); 4523 4524 sp<ThreadBase> thread = mThread.promote(); 4525 if (thread != 0) { 4526 Mutex::Autolock _l(thread->mLock); 4527 track_state state = mState; 4528 // here the track could be either new, or restarted 4529 // in both cases "unstop" the track 4530 if (mState == PAUSED) { 4531 mState = TrackBase::RESUMING; 4532 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); 4533 } else { 4534 mState = TrackBase::ACTIVE; 4535 ALOGV("? => ACTIVE (%d) on thread %p", mName, this); 4536 } 4537 4538 if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { 4539 thread->mLock.unlock(); 4540 status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId); 4541 thread->mLock.lock(); 4542 4543#ifdef ADD_BATTERY_DATA 4544 // to track the speaker usage 4545 if (status == NO_ERROR) { 4546 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); 4547 } 4548#endif 4549 } 4550 if (status == NO_ERROR) { 4551 PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 4552 playbackThread->addTrack_l(this); 4553 } else { 4554 mState = state; 4555 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 4556 } 4557 } else { 4558 status = BAD_VALUE; 4559 } 4560 return status; 4561} 4562 4563void AudioFlinger::PlaybackThread::Track::stop() 4564{ 4565 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); 4566 sp<ThreadBase> thread = mThread.promote(); 4567 if (thread != 0) { 4568 Mutex::Autolock _l(thread->mLock); 4569 track_state state = mState; 4570 if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) { 4571 // If the track is not active (PAUSED and buffers full), flush buffers 4572 PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 4573 if (playbackThread->mActiveTracks.indexOf(this) < 0) { 4574 reset(); 4575 mState = STOPPED; 4576 } else if (!isFastTrack()) { 4577 mState = STOPPED; 4578 } else { 4579 // prepareTracks_l() will set state to STOPPING_2 after next underrun, 4580 // and then to STOPPED and reset() when presentation is complete 4581 mState = STOPPING_1; 4582 } 4583 ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName, playbackThread); 4584 } 4585 if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { 4586 thread->mLock.unlock(); 4587 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); 4588 thread->mLock.lock(); 4589 4590#ifdef ADD_BATTERY_DATA 4591 // to track the speaker usage 4592 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 4593#endif 4594 } 4595 } 4596} 4597 4598void AudioFlinger::PlaybackThread::Track::pause() 4599{ 4600 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); 4601 sp<ThreadBase> thread = mThread.promote(); 4602 if (thread != 0) { 4603 Mutex::Autolock _l(thread->mLock); 4604 if (mState == ACTIVE || mState == RESUMING) { 4605 mState = PAUSING; 4606 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); 4607 if (!isOutputTrack()) { 4608 thread->mLock.unlock(); 4609 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); 4610 thread->mLock.lock(); 4611 4612#ifdef ADD_BATTERY_DATA 4613 // to track the speaker usage 4614 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); 4615#endif 4616 } 4617 } 4618 } 4619} 4620 4621void AudioFlinger::PlaybackThread::Track::flush() 4622{ 4623 ALOGV("flush(%d)", mName); 4624 sp<ThreadBase> thread = mThread.promote(); 4625 if (thread != 0) { 4626 Mutex::Autolock _l(thread->mLock); 4627 if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && mState != PAUSED && 4628 mState != PAUSING) { 4629 return; 4630 } 4631 // No point remaining in PAUSED state after a flush => go to 4632 // FLUSHED state 4633 mState = FLUSHED; 4634 // do not reset the track if it is still in the process of being stopped or paused. 4635 // this will be done by prepareTracks_l() when the track is stopped. 4636 // prepareTracks_l() will see mState == FLUSHED, then 4637 // remove from active track list, reset(), and trigger presentation complete 4638 PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 4639 if (playbackThread->mActiveTracks.indexOf(this) < 0) { 4640 reset(); 4641 } 4642 } 4643} 4644 4645void AudioFlinger::PlaybackThread::Track::reset() 4646{ 4647 // Do not reset twice to avoid discarding data written just after a flush and before 4648 // the audioflinger thread detects the track is stopped. 4649 if (!mResetDone) { 4650 TrackBase::reset(); 4651 // Force underrun condition to avoid false underrun callback until first data is 4652 // written to buffer 4653 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); 4654 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); 4655 mFillingUpStatus = FS_FILLING; 4656 mResetDone = true; 4657 if (mState == FLUSHED) { 4658 mState = IDLE; 4659 } 4660 } 4661} 4662 4663void AudioFlinger::PlaybackThread::Track::mute(bool muted) 4664{ 4665 mMute = muted; 4666} 4667 4668status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) 4669{ 4670 status_t status = DEAD_OBJECT; 4671 sp<ThreadBase> thread = mThread.promote(); 4672 if (thread != 0) { 4673 PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); 4674 sp<AudioFlinger> af = mClient->audioFlinger(); 4675 4676 Mutex::Autolock _l(af->mLock); 4677 4678 sp<PlaybackThread> srcThread = af->getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); 4679 4680 if (EffectId != 0 && srcThread != 0 && playbackThread != srcThread.get()) { 4681 Mutex::Autolock _dl(playbackThread->mLock); 4682 Mutex::Autolock _sl(srcThread->mLock); 4683 sp<EffectChain> chain = srcThread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 4684 if (chain == 0) { 4685 return INVALID_OPERATION; 4686 } 4687 4688 sp<EffectModule> effect = chain->getEffectFromId_l(EffectId); 4689 if (effect == 0) { 4690 return INVALID_OPERATION; 4691 } 4692 srcThread->removeEffect_l(effect); 4693 playbackThread->addEffect_l(effect); 4694 // removeEffect_l() has stopped the effect if it was active so it must be restarted 4695 if (effect->state() == EffectModule::ACTIVE || 4696 effect->state() == EffectModule::STOPPING) { 4697 effect->start(); 4698 } 4699 4700 sp<EffectChain> dstChain = effect->chain().promote(); 4701 if (dstChain == 0) { 4702 srcThread->addEffect_l(effect); 4703 return INVALID_OPERATION; 4704 } 4705 AudioSystem::unregisterEffect(effect->id()); 4706 AudioSystem::registerEffect(&effect->desc(), 4707 srcThread->id(), 4708 dstChain->strategy(), 4709 AUDIO_SESSION_OUTPUT_MIX, 4710 effect->id()); 4711 } 4712 status = playbackThread->attachAuxEffect(this, EffectId); 4713 } 4714 return status; 4715} 4716 4717void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) 4718{ 4719 mAuxEffectId = EffectId; 4720 mAuxBuffer = buffer; 4721} 4722 4723bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten, 4724 size_t audioHalFrames) 4725{ 4726 // a track is considered presented when the total number of frames written to audio HAL 4727 // corresponds to the number of frames written when presentationComplete() is called for the 4728 // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. 4729 if (mPresentationCompleteFrames == 0) { 4730 mPresentationCompleteFrames = framesWritten + audioHalFrames; 4731 ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d", 4732 mPresentationCompleteFrames, audioHalFrames); 4733 } 4734 if (framesWritten >= mPresentationCompleteFrames) { 4735 ALOGV("presentationComplete() session %d complete: framesWritten %d", 4736 mSessionId, framesWritten); 4737 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); 4738 return true; 4739 } 4740 return false; 4741} 4742 4743void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) 4744{ 4745 for (int i = 0; i < (int)mSyncEvents.size(); i++) { 4746 if (mSyncEvents[i]->type() == type) { 4747 mSyncEvents[i]->trigger(); 4748 mSyncEvents.removeAt(i); 4749 i--; 4750 } 4751 } 4752} 4753 4754// implement VolumeBufferProvider interface 4755 4756uint32_t AudioFlinger::PlaybackThread::Track::getVolumeLR() 4757{ 4758 // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs 4759 ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); 4760 uint32_t vlr = mCblk->getVolumeLR(); 4761 uint32_t vl = vlr & 0xFFFF; 4762 uint32_t vr = vlr >> 16; 4763 // track volumes come from shared memory, so can't be trusted and must be clamped 4764 if (vl > MAX_GAIN_INT) { 4765 vl = MAX_GAIN_INT; 4766 } 4767 if (vr > MAX_GAIN_INT) { 4768 vr = MAX_GAIN_INT; 4769 } 4770 // now apply the cached master volume and stream type volume; 4771 // this is trusted but lacks any synchronization or barrier so may be stale 4772 float v = mCachedVolume; 4773 vl *= v; 4774 vr *= v; 4775 // re-combine into U4.16 4776 vlr = (vr << 16) | (vl & 0xFFFF); 4777 // FIXME look at mute, pause, and stop flags 4778 return vlr; 4779} 4780 4781status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event) 4782{ 4783 if (mState == TERMINATED || mState == PAUSED || 4784 ((framesReady() == 0) && ((mSharedBuffer != 0) || 4785 (mState == STOPPED)))) { 4786 ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %d ", 4787 mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady()); 4788 event->cancel(); 4789 return INVALID_OPERATION; 4790 } 4791 TrackBase::setSyncEvent(event); 4792 return NO_ERROR; 4793} 4794 4795// timed audio tracks 4796 4797sp<AudioFlinger::PlaybackThread::TimedTrack> 4798AudioFlinger::PlaybackThread::TimedTrack::create( 4799 PlaybackThread *thread, 4800 const sp<Client>& client, 4801 audio_stream_type_t streamType, 4802 uint32_t sampleRate, 4803 audio_format_t format, 4804 audio_channel_mask_t channelMask, 4805 int frameCount, 4806 const sp<IMemory>& sharedBuffer, 4807 int sessionId) { 4808 if (!client->reserveTimedTrack()) 4809 return 0; 4810 4811 return new TimedTrack( 4812 thread, client, streamType, sampleRate, format, channelMask, frameCount, 4813 sharedBuffer, sessionId); 4814} 4815 4816AudioFlinger::PlaybackThread::TimedTrack::TimedTrack( 4817 PlaybackThread *thread, 4818 const sp<Client>& client, 4819 audio_stream_type_t streamType, 4820 uint32_t sampleRate, 4821 audio_format_t format, 4822 audio_channel_mask_t channelMask, 4823 int frameCount, 4824 const sp<IMemory>& sharedBuffer, 4825 int sessionId) 4826 : Track(thread, client, streamType, sampleRate, format, channelMask, 4827 frameCount, sharedBuffer, sessionId, IAudioFlinger::TRACK_TIMED), 4828 mQueueHeadInFlight(false), 4829 mTrimQueueHeadOnRelease(false), 4830 mFramesPendingInQueue(0), 4831 mTimedSilenceBuffer(NULL), 4832 mTimedSilenceBufferSize(0), 4833 mTimedAudioOutputOnTime(false), 4834 mMediaTimeTransformValid(false) 4835{ 4836 LocalClock lc; 4837 mLocalTimeFreq = lc.getLocalFreq(); 4838 4839 mLocalTimeToSampleTransform.a_zero = 0; 4840 mLocalTimeToSampleTransform.b_zero = 0; 4841 mLocalTimeToSampleTransform.a_to_b_numer = sampleRate; 4842 mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq; 4843 LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer, 4844 &mLocalTimeToSampleTransform.a_to_b_denom); 4845 4846 mMediaTimeToSampleTransform.a_zero = 0; 4847 mMediaTimeToSampleTransform.b_zero = 0; 4848 mMediaTimeToSampleTransform.a_to_b_numer = sampleRate; 4849 mMediaTimeToSampleTransform.a_to_b_denom = 1000000; 4850 LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer, 4851 &mMediaTimeToSampleTransform.a_to_b_denom); 4852} 4853 4854AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() { 4855 mClient->releaseTimedTrack(); 4856 delete [] mTimedSilenceBuffer; 4857} 4858 4859status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer( 4860 size_t size, sp<IMemory>* buffer) { 4861 4862 Mutex::Autolock _l(mTimedBufferQueueLock); 4863 4864 trimTimedBufferQueue_l(); 4865 4866 // lazily initialize the shared memory heap for timed buffers 4867 if (mTimedMemoryDealer == NULL) { 4868 const int kTimedBufferHeapSize = 512 << 10; 4869 4870 mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize, 4871 "AudioFlingerTimed"); 4872 if (mTimedMemoryDealer == NULL) 4873 return NO_MEMORY; 4874 } 4875 4876 sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size); 4877 if (newBuffer == NULL) { 4878 newBuffer = mTimedMemoryDealer->allocate(size); 4879 if (newBuffer == NULL) 4880 return NO_MEMORY; 4881 } 4882 4883 *buffer = newBuffer; 4884 return NO_ERROR; 4885} 4886 4887// caller must hold mTimedBufferQueueLock 4888void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() { 4889 int64_t mediaTimeNow; 4890 { 4891 Mutex::Autolock mttLock(mMediaTimeTransformLock); 4892 if (!mMediaTimeTransformValid) 4893 return; 4894 4895 int64_t targetTimeNow; 4896 status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) 4897 ? mCCHelper.getCommonTime(&targetTimeNow) 4898 : mCCHelper.getLocalTime(&targetTimeNow); 4899 4900 if (OK != res) 4901 return; 4902 4903 if (!mMediaTimeTransform.doReverseTransform(targetTimeNow, 4904 &mediaTimeNow)) { 4905 return; 4906 } 4907 } 4908 4909 size_t trimEnd; 4910 for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) { 4911 int64_t bufEnd; 4912 4913 if ((trimEnd + 1) < mTimedBufferQueue.size()) { 4914 // We have a next buffer. Just use its PTS as the PTS of the frame 4915 // following the last frame in this buffer. If the stream is sparse 4916 // (ie, there are deliberate gaps left in the stream which should be 4917 // filled with silence by the TimedAudioTrack), then this can result 4918 // in one extra buffer being left un-trimmed when it could have 4919 // been. In general, this is not typical, and we would rather 4920 // optimized away the TS calculation below for the more common case 4921 // where PTSes are contiguous. 4922 bufEnd = mTimedBufferQueue[trimEnd + 1].pts(); 4923 } else { 4924 // We have no next buffer. Compute the PTS of the frame following 4925 // the last frame in this buffer by computing the duration of of 4926 // this frame in media time units and adding it to the PTS of the 4927 // buffer. 4928 int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size() 4929 / mCblk->frameSize; 4930 4931 if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount, 4932 &bufEnd)) { 4933 ALOGE("Failed to convert frame count of %lld to media time" 4934 " duration" " (scale factor %d/%u) in %s", 4935 frameCount, 4936 mMediaTimeToSampleTransform.a_to_b_numer, 4937 mMediaTimeToSampleTransform.a_to_b_denom, 4938 __PRETTY_FUNCTION__); 4939 break; 4940 } 4941 bufEnd += mTimedBufferQueue[trimEnd].pts(); 4942 } 4943 4944 if (bufEnd > mediaTimeNow) 4945 break; 4946 4947 // Is the buffer we want to use in the middle of a mix operation right 4948 // now? If so, don't actually trim it. Just wait for the releaseBuffer 4949 // from the mixer which should be coming back shortly. 4950 if (!trimEnd && mQueueHeadInFlight) { 4951 mTrimQueueHeadOnRelease = true; 4952 } 4953 } 4954 4955 size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0; 4956 if (trimStart < trimEnd) { 4957 // Update the bookkeeping for framesReady() 4958 for (size_t i = trimStart; i < trimEnd; ++i) { 4959 updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim"); 4960 } 4961 4962 // Now actually remove the buffers from the queue. 4963 mTimedBufferQueue.removeItemsAt(trimStart, trimEnd); 4964 } 4965} 4966 4967void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l( 4968 const char* logTag) { 4969 ALOG_ASSERT(mTimedBufferQueue.size() > 0, 4970 "%s called (reason \"%s\"), but timed buffer queue has no" 4971 " elements to trim.", __FUNCTION__, logTag); 4972 4973 updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag); 4974 mTimedBufferQueue.removeAt(0); 4975} 4976 4977void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l( 4978 const TimedBuffer& buf, 4979 const char* logTag) { 4980 uint32_t bufBytes = buf.buffer()->size(); 4981 uint32_t consumedAlready = buf.position(); 4982 4983 ALOG_ASSERT(consumedAlready <= bufBytes, 4984 "Bad bookkeeping while updating frames pending. Timed buffer is" 4985 " only %u bytes long, but claims to have consumed %u" 4986 " bytes. (update reason: \"%s\")", 4987 bufBytes, consumedAlready, logTag); 4988 4989 uint32_t bufFrames = (bufBytes - consumedAlready) / mCblk->frameSize; 4990 ALOG_ASSERT(mFramesPendingInQueue >= bufFrames, 4991 "Bad bookkeeping while updating frames pending. Should have at" 4992 " least %u queued frames, but we think we have only %u. (update" 4993 " reason: \"%s\")", 4994 bufFrames, mFramesPendingInQueue, logTag); 4995 4996 mFramesPendingInQueue -= bufFrames; 4997} 4998 4999status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer( 5000 const sp<IMemory>& buffer, int64_t pts) { 5001 5002 { 5003 Mutex::Autolock mttLock(mMediaTimeTransformLock); 5004 if (!mMediaTimeTransformValid) 5005 return INVALID_OPERATION; 5006 } 5007 5008 Mutex::Autolock _l(mTimedBufferQueueLock); 5009 5010 uint32_t bufFrames = buffer->size() / mCblk->frameSize; 5011 mFramesPendingInQueue += bufFrames; 5012 mTimedBufferQueue.add(TimedBuffer(buffer, pts)); 5013 5014 return NO_ERROR; 5015} 5016 5017status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform( 5018 const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) { 5019 5020 ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d", 5021 xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom, 5022 target); 5023 5024 if (!(target == TimedAudioTrack::LOCAL_TIME || 5025 target == TimedAudioTrack::COMMON_TIME)) { 5026 return BAD_VALUE; 5027 } 5028 5029 Mutex::Autolock lock(mMediaTimeTransformLock); 5030 mMediaTimeTransform = xform; 5031 mMediaTimeTransformTarget = target; 5032 mMediaTimeTransformValid = true; 5033 5034 return NO_ERROR; 5035} 5036 5037#define min(a, b) ((a) < (b) ? (a) : (b)) 5038 5039// implementation of getNextBuffer for tracks whose buffers have timestamps 5040status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer( 5041 AudioBufferProvider::Buffer* buffer, int64_t pts) 5042{ 5043 if (pts == AudioBufferProvider::kInvalidPTS) { 5044 buffer->raw = NULL; 5045 buffer->frameCount = 0; 5046 mTimedAudioOutputOnTime = false; 5047 return INVALID_OPERATION; 5048 } 5049 5050 Mutex::Autolock _l(mTimedBufferQueueLock); 5051 5052 ALOG_ASSERT(!mQueueHeadInFlight, 5053 "getNextBuffer called without releaseBuffer!"); 5054 5055 while (true) { 5056 5057 // if we have no timed buffers, then fail 5058 if (mTimedBufferQueue.isEmpty()) { 5059 buffer->raw = NULL; 5060 buffer->frameCount = 0; 5061 return NOT_ENOUGH_DATA; 5062 } 5063 5064 TimedBuffer& head = mTimedBufferQueue.editItemAt(0); 5065 5066 // calculate the PTS of the head of the timed buffer queue expressed in 5067 // local time 5068 int64_t headLocalPTS; 5069 { 5070 Mutex::Autolock mttLock(mMediaTimeTransformLock); 5071 5072 ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid"); 5073 5074 if (mMediaTimeTransform.a_to_b_denom == 0) { 5075 // the transform represents a pause, so yield silence 5076 timedYieldSilence_l(buffer->frameCount, buffer); 5077 return NO_ERROR; 5078 } 5079 5080 int64_t transformedPTS; 5081 if (!mMediaTimeTransform.doForwardTransform(head.pts(), 5082 &transformedPTS)) { 5083 // the transform failed. this shouldn't happen, but if it does 5084 // then just drop this buffer 5085 ALOGW("timedGetNextBuffer transform failed"); 5086 buffer->raw = NULL; 5087 buffer->frameCount = 0; 5088 trimTimedBufferQueueHead_l("getNextBuffer; no transform"); 5089 return NO_ERROR; 5090 } 5091 5092 if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) { 5093 if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS, 5094 &headLocalPTS)) { 5095 buffer->raw = NULL; 5096 buffer->frameCount = 0; 5097 return INVALID_OPERATION; 5098 } 5099 } else { 5100 headLocalPTS = transformedPTS; 5101 } 5102 } 5103 5104 // adjust the head buffer's PTS to reflect the portion of the head buffer 5105 // that has already been consumed 5106 int64_t effectivePTS = headLocalPTS + 5107 ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate()); 5108 5109 // Calculate the delta in samples between the head of the input buffer 5110 // queue and the start of the next output buffer that will be written. 5111 // If the transformation fails because of over or underflow, it means 5112 // that the sample's position in the output stream is so far out of 5113 // whack that it should just be dropped. 5114 int64_t sampleDelta; 5115 if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) { 5116 ALOGV("*** head buffer is too far from PTS: dropped buffer"); 5117 trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from" 5118 " mix"); 5119 continue; 5120 } 5121 if (!mLocalTimeToSampleTransform.doForwardTransform( 5122 (effectivePTS - pts) << 32, &sampleDelta)) { 5123 ALOGV("*** too late during sample rate transform: dropped buffer"); 5124 trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample"); 5125 continue; 5126 } 5127 5128 ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld" 5129 " sampleDelta=[%d.%08x]", 5130 head.pts(), head.position(), pts, 5131 static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) 5132 + (sampleDelta >> 32)), 5133 static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF)); 5134 5135 // if the delta between the ideal placement for the next input sample and 5136 // the current output position is within this threshold, then we will 5137 // concatenate the next input samples to the previous output 5138 const int64_t kSampleContinuityThreshold = 5139 (static_cast<int64_t>(sampleRate()) << 32) / 250; 5140 5141 // if this is the first buffer of audio that we're emitting from this track 5142 // then it should be almost exactly on time. 5143 const int64_t kSampleStartupThreshold = 1LL << 32; 5144 5145 if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) || 5146 (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) { 5147 // the next input is close enough to being on time, so concatenate it 5148 // with the last output 5149 timedYieldSamples_l(buffer); 5150 5151 ALOGVV("*** on time: head.pos=%d frameCount=%u", 5152 head.position(), buffer->frameCount); 5153 return NO_ERROR; 5154 } 5155 5156 // Looks like our output is not on time. Reset our on timed status. 5157 // Next time we mix samples from our input queue, then should be within 5158 // the StartupThreshold. 5159 mTimedAudioOutputOnTime = false; 5160 if (sampleDelta > 0) { 5161 // the gap between the current output position and the proper start of 5162 // the next input sample is too big, so fill it with silence 5163 uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32; 5164 5165 timedYieldSilence_l(framesUntilNextInput, buffer); 5166 ALOGV("*** silence: frameCount=%u", buffer->frameCount); 5167 return NO_ERROR; 5168 } else { 5169 // the next input sample is late 5170 uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32)); 5171 size_t onTimeSamplePosition = 5172 head.position() + lateFrames * mCblk->frameSize; 5173 5174 if (onTimeSamplePosition > head.buffer()->size()) { 5175 // all the remaining samples in the head are too late, so 5176 // drop it and move on 5177 ALOGV("*** too late: dropped buffer"); 5178 trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer"); 5179 continue; 5180 } else { 5181 // skip over the late samples 5182 head.setPosition(onTimeSamplePosition); 5183 5184 // yield the available samples 5185 timedYieldSamples_l(buffer); 5186 5187 ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); 5188 return NO_ERROR; 5189 } 5190 } 5191 } 5192} 5193 5194// Yield samples from the timed buffer queue head up to the given output 5195// buffer's capacity. 5196// 5197// Caller must hold mTimedBufferQueueLock 5198void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l( 5199 AudioBufferProvider::Buffer* buffer) { 5200 5201 const TimedBuffer& head = mTimedBufferQueue[0]; 5202 5203 buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) + 5204 head.position()); 5205 5206 uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) / 5207 mCblk->frameSize); 5208 size_t framesRequested = buffer->frameCount; 5209 buffer->frameCount = min(framesLeftInHead, framesRequested); 5210 5211 mQueueHeadInFlight = true; 5212 mTimedAudioOutputOnTime = true; 5213} 5214 5215// Yield samples of silence up to the given output buffer's capacity 5216// 5217// Caller must hold mTimedBufferQueueLock 5218void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l( 5219 uint32_t numFrames, AudioBufferProvider::Buffer* buffer) { 5220 5221 // lazily allocate a buffer filled with silence 5222 if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) { 5223 delete [] mTimedSilenceBuffer; 5224 mTimedSilenceBufferSize = numFrames * mCblk->frameSize; 5225 mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize]; 5226 memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize); 5227 } 5228 5229 buffer->raw = mTimedSilenceBuffer; 5230 size_t framesRequested = buffer->frameCount; 5231 buffer->frameCount = min(numFrames, framesRequested); 5232 5233 mTimedAudioOutputOnTime = false; 5234} 5235 5236// AudioBufferProvider interface 5237void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer( 5238 AudioBufferProvider::Buffer* buffer) { 5239 5240 Mutex::Autolock _l(mTimedBufferQueueLock); 5241 5242 // If the buffer which was just released is part of the buffer at the head 5243 // of the queue, be sure to update the amt of the buffer which has been 5244 // consumed. If the buffer being returned is not part of the head of the 5245 // queue, its either because the buffer is part of the silence buffer, or 5246 // because the head of the timed queue was trimmed after the mixer called 5247 // getNextBuffer but before the mixer called releaseBuffer. 5248 if (buffer->raw == mTimedSilenceBuffer) { 5249 ALOG_ASSERT(!mQueueHeadInFlight, 5250 "Queue head in flight during release of silence buffer!"); 5251 goto done; 5252 } 5253 5254 ALOG_ASSERT(mQueueHeadInFlight, 5255 "TimedTrack::releaseBuffer of non-silence buffer, but no queue" 5256 " head in flight."); 5257 5258 if (mTimedBufferQueue.size()) { 5259 TimedBuffer& head = mTimedBufferQueue.editItemAt(0); 5260 5261 void* start = head.buffer()->pointer(); 5262 void* end = reinterpret_cast<void*>( 5263 reinterpret_cast<uint8_t*>(head.buffer()->pointer()) 5264 + head.buffer()->size()); 5265 5266 ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end), 5267 "released buffer not within the head of the timed buffer" 5268 " queue; qHead = [%p, %p], released buffer = %p", 5269 start, end, buffer->raw); 5270 5271 head.setPosition(head.position() + 5272 (buffer->frameCount * mCblk->frameSize)); 5273 mQueueHeadInFlight = false; 5274 5275 ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount, 5276 "Bad bookkeeping during releaseBuffer! Should have at" 5277 " least %u queued frames, but we think we have only %u", 5278 buffer->frameCount, mFramesPendingInQueue); 5279 5280 mFramesPendingInQueue -= buffer->frameCount; 5281 5282 if ((static_cast<size_t>(head.position()) >= head.buffer()->size()) 5283 || mTrimQueueHeadOnRelease) { 5284 trimTimedBufferQueueHead_l("releaseBuffer"); 5285 mTrimQueueHeadOnRelease = false; 5286 } 5287 } else { 5288 LOG_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no" 5289 " buffers in the timed buffer queue"); 5290 } 5291 5292done: 5293 buffer->raw = 0; 5294 buffer->frameCount = 0; 5295} 5296 5297size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const { 5298 Mutex::Autolock _l(mTimedBufferQueueLock); 5299 return mFramesPendingInQueue; 5300} 5301 5302AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer() 5303 : mPTS(0), mPosition(0) {} 5304 5305AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer( 5306 const sp<IMemory>& buffer, int64_t pts) 5307 : mBuffer(buffer), mPTS(pts), mPosition(0) {} 5308 5309// ---------------------------------------------------------------------------- 5310 5311// RecordTrack constructor must be called with AudioFlinger::mLock held 5312AudioFlinger::RecordThread::RecordTrack::RecordTrack( 5313 RecordThread *thread, 5314 const sp<Client>& client, 5315 uint32_t sampleRate, 5316 audio_format_t format, 5317 audio_channel_mask_t channelMask, 5318 int frameCount, 5319 int sessionId) 5320 : TrackBase(thread, client, sampleRate, format, 5321 channelMask, frameCount, 0 /*sharedBuffer*/, sessionId), 5322 mOverflow(false) 5323{ 5324 if (mCblk != NULL) { 5325 ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); 5326 if (format == AUDIO_FORMAT_PCM_16_BIT) { 5327 mCblk->frameSize = mChannelCount * sizeof(int16_t); 5328 } else if (format == AUDIO_FORMAT_PCM_8_BIT) { 5329 mCblk->frameSize = mChannelCount * sizeof(int8_t); 5330 } else { 5331 mCblk->frameSize = sizeof(int8_t); 5332 } 5333 } 5334} 5335 5336AudioFlinger::RecordThread::RecordTrack::~RecordTrack() 5337{ 5338 ALOGV("%s", __func__); 5339} 5340 5341// AudioBufferProvider interface 5342status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) 5343{ 5344 audio_track_cblk_t* cblk = this->cblk(); 5345 uint32_t framesAvail; 5346 uint32_t framesReq = buffer->frameCount; 5347 5348 // Check if last stepServer failed, try to step now 5349 if (mStepServerFailed) { 5350 if (!step()) goto getNextBuffer_exit; 5351 ALOGV("stepServer recovered"); 5352 mStepServerFailed = false; 5353 } 5354 5355 framesAvail = cblk->framesAvailable_l(); 5356 5357 if (CC_LIKELY(framesAvail)) { 5358 uint32_t s = cblk->server; 5359 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; 5360 5361 if (framesReq > framesAvail) { 5362 framesReq = framesAvail; 5363 } 5364 if (framesReq > bufferEnd - s) { 5365 framesReq = bufferEnd - s; 5366 } 5367 5368 buffer->raw = getBuffer(s, framesReq); 5369 buffer->frameCount = framesReq; 5370 return NO_ERROR; 5371 } 5372 5373getNextBuffer_exit: 5374 buffer->raw = NULL; 5375 buffer->frameCount = 0; 5376 return NOT_ENOUGH_DATA; 5377} 5378 5379status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, 5380 int triggerSession) 5381{ 5382 sp<ThreadBase> thread = mThread.promote(); 5383 if (thread != 0) { 5384 RecordThread *recordThread = (RecordThread *)thread.get(); 5385 return recordThread->start(this, event, triggerSession); 5386 } else { 5387 return BAD_VALUE; 5388 } 5389} 5390 5391void AudioFlinger::RecordThread::RecordTrack::stop() 5392{ 5393 sp<ThreadBase> thread = mThread.promote(); 5394 if (thread != 0) { 5395 RecordThread *recordThread = (RecordThread *)thread.get(); 5396 recordThread->mLock.lock(); 5397 bool doStop = recordThread->stop_l(this); 5398 if (doStop) { 5399 TrackBase::reset(); 5400 // Force overrun condition to avoid false overrun callback until first data is 5401 // read from buffer 5402 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); 5403 } 5404 recordThread->mLock.unlock(); 5405 if (doStop) { 5406 AudioSystem::stopInput(recordThread->id()); 5407 } 5408 } 5409} 5410 5411/*static*/ void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result) 5412{ 5413 result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n"); 5414} 5415 5416void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) 5417{ 5418 snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n", 5419 (mClient == 0) ? getpid_cached : mClient->pid(), 5420 mFormat, 5421 mChannelMask, 5422 mSessionId, 5423 mFrameCount, 5424 mState, 5425 mCblk->sampleRate, 5426 mCblk->server, 5427 mCblk->user); 5428} 5429 5430 5431// ---------------------------------------------------------------------------- 5432 5433AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( 5434 PlaybackThread *playbackThread, 5435 DuplicatingThread *sourceThread, 5436 uint32_t sampleRate, 5437 audio_format_t format, 5438 audio_channel_mask_t channelMask, 5439 int frameCount) 5440 : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, 5441 NULL, 0, IAudioFlinger::TRACK_DEFAULT), 5442 mActive(false), mSourceThread(sourceThread) 5443{ 5444 5445 if (mCblk != NULL) { 5446 mCblk->flags |= CBLK_DIRECTION_OUT; 5447 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); 5448 mOutBuffer.frameCount = 0; 5449 playbackThread->mTracks.add(this); 5450 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ 5451 "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", 5452 mCblk, mBuffer, mCblk->buffers, 5453 mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); 5454 } else { 5455 ALOGW("Error creating output track on thread %p", playbackThread); 5456 } 5457} 5458 5459AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() 5460{ 5461 clearBufferQueue(); 5462} 5463 5464status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, 5465 int triggerSession) 5466{ 5467 status_t status = Track::start(event, triggerSession); 5468 if (status != NO_ERROR) { 5469 return status; 5470 } 5471 5472 mActive = true; 5473 mRetryCount = 127; 5474 return status; 5475} 5476 5477void AudioFlinger::PlaybackThread::OutputTrack::stop() 5478{ 5479 Track::stop(); 5480 clearBufferQueue(); 5481 mOutBuffer.frameCount = 0; 5482 mActive = false; 5483} 5484 5485bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) 5486{ 5487 Buffer *pInBuffer; 5488 Buffer inBuffer; 5489 uint32_t channelCount = mChannelCount; 5490 bool outputBufferFull = false; 5491 inBuffer.frameCount = frames; 5492 inBuffer.i16 = data; 5493 5494 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); 5495 5496 if (!mActive && frames != 0) { 5497 start(); 5498 sp<ThreadBase> thread = mThread.promote(); 5499 if (thread != 0) { 5500 MixerThread *mixerThread = (MixerThread *)thread.get(); 5501 if (mCblk->frameCount > frames){ 5502 if (mBufferQueue.size() < kMaxOverFlowBuffers) { 5503 uint32_t startFrames = (mCblk->frameCount - frames); 5504 pInBuffer = new Buffer; 5505 pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; 5506 pInBuffer->frameCount = startFrames; 5507 pInBuffer->i16 = pInBuffer->mBuffer; 5508 memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); 5509 mBufferQueue.add(pInBuffer); 5510 } else { 5511 ALOGW ("OutputTrack::write() %p no more buffers in queue", this); 5512 } 5513 } 5514 } 5515 } 5516 5517 while (waitTimeLeftMs) { 5518 // First write pending buffers, then new data 5519 if (mBufferQueue.size()) { 5520 pInBuffer = mBufferQueue.itemAt(0); 5521 } else { 5522 pInBuffer = &inBuffer; 5523 } 5524 5525 if (pInBuffer->frameCount == 0) { 5526 break; 5527 } 5528 5529 if (mOutBuffer.frameCount == 0) { 5530 mOutBuffer.frameCount = pInBuffer->frameCount; 5531 nsecs_t startTime = systemTime(); 5532 if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { 5533 ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); 5534 outputBufferFull = true; 5535 break; 5536 } 5537 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); 5538 if (waitTimeLeftMs >= waitTimeMs) { 5539 waitTimeLeftMs -= waitTimeMs; 5540 } else { 5541 waitTimeLeftMs = 0; 5542 } 5543 } 5544 5545 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; 5546 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); 5547 mCblk->stepUser(outFrames); 5548 pInBuffer->frameCount -= outFrames; 5549 pInBuffer->i16 += outFrames * channelCount; 5550 mOutBuffer.frameCount -= outFrames; 5551 mOutBuffer.i16 += outFrames * channelCount; 5552 5553 if (pInBuffer->frameCount == 0) { 5554 if (mBufferQueue.size()) { 5555 mBufferQueue.removeAt(0); 5556 delete [] pInBuffer->mBuffer; 5557 delete pInBuffer; 5558 ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); 5559 } else { 5560 break; 5561 } 5562 } 5563 } 5564 5565 // If we could not write all frames, allocate a buffer and queue it for next time. 5566 if (inBuffer.frameCount) { 5567 sp<ThreadBase> thread = mThread.promote(); 5568 if (thread != 0 && !thread->standby()) { 5569 if (mBufferQueue.size() < kMaxOverFlowBuffers) { 5570 pInBuffer = new Buffer; 5571 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; 5572 pInBuffer->frameCount = inBuffer.frameCount; 5573 pInBuffer->i16 = pInBuffer->mBuffer; 5574 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); 5575 mBufferQueue.add(pInBuffer); 5576 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); 5577 } else { 5578 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); 5579 } 5580 } 5581 } 5582 5583 // Calling write() with a 0 length buffer, means that no more data will be written: 5584 // If no more buffers are pending, fill output track buffer to make sure it is started 5585 // by output mixer. 5586 if (frames == 0 && mBufferQueue.size() == 0) { 5587 if (mCblk->user < mCblk->frameCount) { 5588 frames = mCblk->frameCount - mCblk->user; 5589 pInBuffer = new Buffer; 5590 pInBuffer->mBuffer = new int16_t[frames * channelCount]; 5591 pInBuffer->frameCount = frames; 5592 pInBuffer->i16 = pInBuffer->mBuffer; 5593 memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); 5594 mBufferQueue.add(pInBuffer); 5595 } else if (mActive) { 5596 stop(); 5597 } 5598 } 5599 5600 return outputBufferFull; 5601} 5602 5603status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) 5604{ 5605 int active; 5606 status_t result; 5607 audio_track_cblk_t* cblk = mCblk; 5608 uint32_t framesReq = buffer->frameCount; 5609 5610// ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); 5611 buffer->frameCount = 0; 5612 5613 uint32_t framesAvail = cblk->framesAvailable(); 5614 5615 5616 if (framesAvail == 0) { 5617 Mutex::Autolock _l(cblk->lock); 5618 goto start_loop_here; 5619 while (framesAvail == 0) { 5620 active = mActive; 5621 if (CC_UNLIKELY(!active)) { 5622 ALOGV("Not active and NO_MORE_BUFFERS"); 5623 return NO_MORE_BUFFERS; 5624 } 5625 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); 5626 if (result != NO_ERROR) { 5627 return NO_MORE_BUFFERS; 5628 } 5629 // read the server count again 5630 start_loop_here: 5631 framesAvail = cblk->framesAvailable_l(); 5632 } 5633 } 5634 5635// if (framesAvail < framesReq) { 5636// return NO_MORE_BUFFERS; 5637// } 5638 5639 if (framesReq > framesAvail) { 5640 framesReq = framesAvail; 5641 } 5642 5643 uint32_t u = cblk->user; 5644 uint32_t bufferEnd = cblk->userBase + cblk->frameCount; 5645 5646 if (framesReq > bufferEnd - u) { 5647 framesReq = bufferEnd - u; 5648 } 5649 5650 buffer->frameCount = framesReq; 5651 buffer->raw = (void *)cblk->buffer(u); 5652 return NO_ERROR; 5653} 5654 5655 5656void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() 5657{ 5658 size_t size = mBufferQueue.size(); 5659 5660 for (size_t i = 0; i < size; i++) { 5661 Buffer *pBuffer = mBufferQueue.itemAt(i); 5662 delete [] pBuffer->mBuffer; 5663 delete pBuffer; 5664 } 5665 mBufferQueue.clear(); 5666} 5667 5668// ---------------------------------------------------------------------------- 5669 5670AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) 5671 : RefBase(), 5672 mAudioFlinger(audioFlinger), 5673 // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below 5674 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), 5675 mPid(pid), 5676 mTimedTrackCount(0) 5677{ 5678 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer 5679} 5680 5681// Client destructor must be called with AudioFlinger::mLock held 5682AudioFlinger::Client::~Client() 5683{ 5684 mAudioFlinger->removeClient_l(mPid); 5685} 5686 5687sp<MemoryDealer> AudioFlinger::Client::heap() const 5688{ 5689 return mMemoryDealer; 5690} 5691 5692// Reserve one of the limited slots for a timed audio track associated 5693// with this client 5694bool AudioFlinger::Client::reserveTimedTrack() 5695{ 5696 const int kMaxTimedTracksPerClient = 4; 5697 5698 Mutex::Autolock _l(mTimedTrackLock); 5699 5700 if (mTimedTrackCount >= kMaxTimedTracksPerClient) { 5701 ALOGW("can not create timed track - pid %d has exceeded the limit", 5702 mPid); 5703 return false; 5704 } 5705 5706 mTimedTrackCount++; 5707 return true; 5708} 5709 5710// Release a slot for a timed audio track 5711void AudioFlinger::Client::releaseTimedTrack() 5712{ 5713 Mutex::Autolock _l(mTimedTrackLock); 5714 mTimedTrackCount--; 5715} 5716 5717// ---------------------------------------------------------------------------- 5718 5719AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, 5720 const sp<IAudioFlingerClient>& client, 5721 pid_t pid) 5722 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) 5723{ 5724} 5725 5726AudioFlinger::NotificationClient::~NotificationClient() 5727{ 5728} 5729 5730void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) 5731{ 5732 sp<NotificationClient> keep(this); 5733 mAudioFlinger->removeNotificationClient(mPid); 5734} 5735 5736// ---------------------------------------------------------------------------- 5737 5738AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) 5739 : BnAudioTrack(), 5740 mTrack(track) 5741{ 5742} 5743 5744AudioFlinger::TrackHandle::~TrackHandle() { 5745 // just stop the track on deletion, associated resources 5746 // will be freed from the main thread once all pending buffers have 5747 // been played. Unless it's not in the active track list, in which 5748 // case we free everything now... 5749 mTrack->destroy(); 5750} 5751 5752sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { 5753 return mTrack->getCblk(); 5754} 5755 5756status_t AudioFlinger::TrackHandle::start() { 5757 return mTrack->start(); 5758} 5759 5760void AudioFlinger::TrackHandle::stop() { 5761 mTrack->stop(); 5762} 5763 5764void AudioFlinger::TrackHandle::flush() { 5765 mTrack->flush(); 5766} 5767 5768void AudioFlinger::TrackHandle::mute(bool e) { 5769 mTrack->mute(e); 5770} 5771 5772void AudioFlinger::TrackHandle::pause() { 5773 mTrack->pause(); 5774} 5775 5776status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) 5777{ 5778 return mTrack->attachAuxEffect(EffectId); 5779} 5780 5781status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size, 5782 sp<IMemory>* buffer) { 5783 if (!mTrack->isTimedTrack()) 5784 return INVALID_OPERATION; 5785 5786 PlaybackThread::TimedTrack* tt = 5787 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); 5788 return tt->allocateTimedBuffer(size, buffer); 5789} 5790 5791status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer, 5792 int64_t pts) { 5793 if (!mTrack->isTimedTrack()) 5794 return INVALID_OPERATION; 5795 5796 PlaybackThread::TimedTrack* tt = 5797 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); 5798 return tt->queueTimedBuffer(buffer, pts); 5799} 5800 5801status_t AudioFlinger::TrackHandle::setMediaTimeTransform( 5802 const LinearTransform& xform, int target) { 5803 5804 if (!mTrack->isTimedTrack()) 5805 return INVALID_OPERATION; 5806 5807 PlaybackThread::TimedTrack* tt = 5808 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); 5809 return tt->setMediaTimeTransform( 5810 xform, static_cast<TimedAudioTrack::TargetTimeline>(target)); 5811} 5812 5813status_t AudioFlinger::TrackHandle::onTransact( 5814 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 5815{ 5816 return BnAudioTrack::onTransact(code, data, reply, flags); 5817} 5818 5819// ---------------------------------------------------------------------------- 5820 5821sp<IAudioRecord> AudioFlinger::openRecord( 5822 pid_t pid, 5823 audio_io_handle_t input, 5824 uint32_t sampleRate, 5825 audio_format_t format, 5826 audio_channel_mask_t channelMask, 5827 int frameCount, 5828 IAudioFlinger::track_flags_t flags, 5829 pid_t tid, 5830 int *sessionId, 5831 status_t *status) 5832{ 5833 sp<RecordThread::RecordTrack> recordTrack; 5834 sp<RecordHandle> recordHandle; 5835 sp<Client> client; 5836 status_t lStatus; 5837 RecordThread *thread; 5838 size_t inFrameCount; 5839 int lSessionId; 5840 5841 // check calling permissions 5842 if (!recordingAllowed()) { 5843 lStatus = PERMISSION_DENIED; 5844 goto Exit; 5845 } 5846 5847 // add client to list 5848 { // scope for mLock 5849 Mutex::Autolock _l(mLock); 5850 thread = checkRecordThread_l(input); 5851 if (thread == NULL) { 5852 lStatus = BAD_VALUE; 5853 goto Exit; 5854 } 5855 5856 client = registerPid_l(pid); 5857 5858 // If no audio session id is provided, create one here 5859 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { 5860 lSessionId = *sessionId; 5861 } else { 5862 lSessionId = nextUniqueId(); 5863 if (sessionId != NULL) { 5864 *sessionId = lSessionId; 5865 } 5866 } 5867 // create new record track. The record track uses one track in mHardwareMixerThread by convention. 5868 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask, 5869 frameCount, lSessionId, flags, tid, &lStatus); 5870 } 5871 if (lStatus != NO_ERROR) { 5872 // remove local strong reference to Client before deleting the RecordTrack so that the Client 5873 // destructor is called by the TrackBase destructor with mLock held 5874 client.clear(); 5875 recordTrack.clear(); 5876 goto Exit; 5877 } 5878 5879 // return to handle to client 5880 recordHandle = new RecordHandle(recordTrack); 5881 lStatus = NO_ERROR; 5882 5883Exit: 5884 if (status) { 5885 *status = lStatus; 5886 } 5887 return recordHandle; 5888} 5889 5890// ---------------------------------------------------------------------------- 5891 5892AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) 5893 : BnAudioRecord(), 5894 mRecordTrack(recordTrack) 5895{ 5896} 5897 5898AudioFlinger::RecordHandle::~RecordHandle() { 5899 stop_nonvirtual(); 5900 mRecordTrack->destroy(); 5901} 5902 5903sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { 5904 return mRecordTrack->getCblk(); 5905} 5906 5907status_t AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event, int triggerSession) { 5908 ALOGV("RecordHandle::start()"); 5909 return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession); 5910} 5911 5912void AudioFlinger::RecordHandle::stop() { 5913 stop_nonvirtual(); 5914} 5915 5916void AudioFlinger::RecordHandle::stop_nonvirtual() { 5917 ALOGV("RecordHandle::stop()"); 5918 mRecordTrack->stop(); 5919} 5920 5921status_t AudioFlinger::RecordHandle::onTransact( 5922 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 5923{ 5924 return BnAudioRecord::onTransact(code, data, reply, flags); 5925} 5926 5927// ---------------------------------------------------------------------------- 5928 5929AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, 5930 AudioStreamIn *input, 5931 uint32_t sampleRate, 5932 audio_channel_mask_t channelMask, 5933 audio_io_handle_t id, 5934 audio_devices_t device) : 5935 ThreadBase(audioFlinger, id, device, RECORD), 5936 mInput(input), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL), 5937 // mRsmpInIndex and mInputBytes set by readInputParameters() 5938 mReqChannelCount(popcount(channelMask)), 5939 mReqSampleRate(sampleRate) 5940 // mBytesRead is only meaningful while active, and so is cleared in start() 5941 // (but might be better to also clear here for dump?) 5942{ 5943 snprintf(mName, kNameLength, "AudioIn_%X", id); 5944 5945 readInputParameters(); 5946} 5947 5948 5949AudioFlinger::RecordThread::~RecordThread() 5950{ 5951 delete[] mRsmpInBuffer; 5952 delete mResampler; 5953 delete[] mRsmpOutBuffer; 5954} 5955 5956void AudioFlinger::RecordThread::onFirstRef() 5957{ 5958 run(mName, PRIORITY_URGENT_AUDIO); 5959} 5960 5961status_t AudioFlinger::RecordThread::readyToRun() 5962{ 5963 status_t status = initCheck(); 5964 ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); 5965 return status; 5966} 5967 5968bool AudioFlinger::RecordThread::threadLoop() 5969{ 5970 AudioBufferProvider::Buffer buffer; 5971 sp<RecordTrack> activeTrack; 5972 Vector< sp<EffectChain> > effectChains; 5973 5974 nsecs_t lastWarning = 0; 5975 5976 inputStandBy(); 5977 acquireWakeLock(); 5978 5979 // start recording 5980 while (!exitPending()) { 5981 5982 processConfigEvents(); 5983 5984 { // scope for mLock 5985 Mutex::Autolock _l(mLock); 5986 checkForNewParameters_l(); 5987 if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { 5988 standby(); 5989 5990 if (exitPending()) break; 5991 5992 releaseWakeLock_l(); 5993 ALOGV("RecordThread: loop stopping"); 5994 // go to sleep 5995 mWaitWorkCV.wait(mLock); 5996 ALOGV("RecordThread: loop starting"); 5997 acquireWakeLock_l(); 5998 continue; 5999 } 6000 if (mActiveTrack != 0) { 6001 if (mActiveTrack->mState == TrackBase::PAUSING) { 6002 standby(); 6003 mActiveTrack.clear(); 6004 mStartStopCond.broadcast(); 6005 } else if (mActiveTrack->mState == TrackBase::RESUMING) { 6006 if (mReqChannelCount != mActiveTrack->channelCount()) { 6007 mActiveTrack.clear(); 6008 mStartStopCond.broadcast(); 6009 } else if (mBytesRead != 0) { 6010 // record start succeeds only if first read from audio input 6011 // succeeds 6012 if (mBytesRead > 0) { 6013 mActiveTrack->mState = TrackBase::ACTIVE; 6014 } else { 6015 mActiveTrack.clear(); 6016 } 6017 mStartStopCond.broadcast(); 6018 } 6019 mStandby = false; 6020 } else if (mActiveTrack->mState == TrackBase::TERMINATED) { 6021 removeTrack_l(mActiveTrack); 6022 mActiveTrack.clear(); 6023 } 6024 } 6025 lockEffectChains_l(effectChains); 6026 } 6027 6028 if (mActiveTrack != 0) { 6029 if (mActiveTrack->mState != TrackBase::ACTIVE && 6030 mActiveTrack->mState != TrackBase::RESUMING) { 6031 unlockEffectChains(effectChains); 6032 usleep(kRecordThreadSleepUs); 6033 continue; 6034 } 6035 for (size_t i = 0; i < effectChains.size(); i ++) { 6036 effectChains[i]->process_l(); 6037 } 6038 6039 buffer.frameCount = mFrameCount; 6040 if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { 6041 size_t framesOut = buffer.frameCount; 6042 if (mResampler == NULL) { 6043 // no resampling 6044 while (framesOut) { 6045 size_t framesIn = mFrameCount - mRsmpInIndex; 6046 if (framesIn) { 6047 int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; 6048 int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; 6049 if (framesIn > framesOut) 6050 framesIn = framesOut; 6051 mRsmpInIndex += framesIn; 6052 framesOut -= framesIn; 6053 if ((int)mChannelCount == mReqChannelCount || 6054 mFormat != AUDIO_FORMAT_PCM_16_BIT) { 6055 memcpy(dst, src, framesIn * mFrameSize); 6056 } else { 6057 if (mChannelCount == 1) { 6058 upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, 6059 (int16_t *)src, framesIn); 6060 } else { 6061 downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, 6062 (int16_t *)src, framesIn); 6063 } 6064 } 6065 } 6066 if (framesOut && mFrameCount == mRsmpInIndex) { 6067 if (framesOut == mFrameCount && 6068 ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { 6069 mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); 6070 framesOut = 0; 6071 } else { 6072 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); 6073 mRsmpInIndex = 0; 6074 } 6075 if (mBytesRead < 0) { 6076 ALOGE("Error reading audio input"); 6077 if (mActiveTrack->mState == TrackBase::ACTIVE) { 6078 // Force input into standby so that it tries to 6079 // recover at next read attempt 6080 inputStandBy(); 6081 usleep(kRecordThreadSleepUs); 6082 } 6083 mRsmpInIndex = mFrameCount; 6084 framesOut = 0; 6085 buffer.frameCount = 0; 6086 } 6087 } 6088 } 6089 } else { 6090 // resampling 6091 6092 memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); 6093 // alter output frame count as if we were expecting stereo samples 6094 if (mChannelCount == 1 && mReqChannelCount == 1) { 6095 framesOut >>= 1; 6096 } 6097 mResampler->resample(mRsmpOutBuffer, framesOut, this); 6098 // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() 6099 // are 32 bit aligned which should be always true. 6100 if (mChannelCount == 2 && mReqChannelCount == 1) { 6101 ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); 6102 // the resampler always outputs stereo samples: do post stereo to mono conversion 6103 downmix_to_mono_i16_from_stereo_i16(buffer.i16, (int16_t *)mRsmpOutBuffer, 6104 framesOut); 6105 } else { 6106 ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); 6107 } 6108 6109 } 6110 if (mFramestoDrop == 0) { 6111 mActiveTrack->releaseBuffer(&buffer); 6112 } else { 6113 if (mFramestoDrop > 0) { 6114 mFramestoDrop -= buffer.frameCount; 6115 if (mFramestoDrop <= 0) { 6116 clearSyncStartEvent(); 6117 } 6118 } else { 6119 mFramestoDrop += buffer.frameCount; 6120 if (mFramestoDrop >= 0 || mSyncStartEvent == 0 || 6121 mSyncStartEvent->isCancelled()) { 6122 ALOGW("Synced record %s, session %d, trigger session %d", 6123 (mFramestoDrop >= 0) ? "timed out" : "cancelled", 6124 mActiveTrack->sessionId(), 6125 (mSyncStartEvent != 0) ? mSyncStartEvent->triggerSession() : 0); 6126 clearSyncStartEvent(); 6127 } 6128 } 6129 } 6130 mActiveTrack->clearOverflow(); 6131 } 6132 // client isn't retrieving buffers fast enough 6133 else { 6134 if (!mActiveTrack->setOverflow()) { 6135 nsecs_t now = systemTime(); 6136 if ((now - lastWarning) > kWarningThrottleNs) { 6137 ALOGW("RecordThread: buffer overflow"); 6138 lastWarning = now; 6139 } 6140 } 6141 // Release the processor for a while before asking for a new buffer. 6142 // This will give the application more chance to read from the buffer and 6143 // clear the overflow. 6144 usleep(kRecordThreadSleepUs); 6145 } 6146 } 6147 // enable changes in effect chain 6148 unlockEffectChains(effectChains); 6149 effectChains.clear(); 6150 } 6151 6152 standby(); 6153 6154 { 6155 Mutex::Autolock _l(mLock); 6156 mActiveTrack.clear(); 6157 mStartStopCond.broadcast(); 6158 } 6159 6160 releaseWakeLock(); 6161 6162 ALOGV("RecordThread %p exiting", this); 6163 return false; 6164} 6165 6166void AudioFlinger::RecordThread::standby() 6167{ 6168 if (!mStandby) { 6169 inputStandBy(); 6170 mStandby = true; 6171 } 6172} 6173 6174void AudioFlinger::RecordThread::inputStandBy() 6175{ 6176 mInput->stream->common.standby(&mInput->stream->common); 6177} 6178 6179sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( 6180 const sp<AudioFlinger::Client>& client, 6181 uint32_t sampleRate, 6182 audio_format_t format, 6183 audio_channel_mask_t channelMask, 6184 int frameCount, 6185 int sessionId, 6186 IAudioFlinger::track_flags_t flags, 6187 pid_t tid, 6188 status_t *status) 6189{ 6190 sp<RecordTrack> track; 6191 status_t lStatus; 6192 6193 lStatus = initCheck(); 6194 if (lStatus != NO_ERROR) { 6195 ALOGE("Audio driver not initialized."); 6196 goto Exit; 6197 } 6198 6199 // FIXME use flags and tid similar to createTrack_l() 6200 6201 { // scope for mLock 6202 Mutex::Autolock _l(mLock); 6203 6204 track = new RecordTrack(this, client, sampleRate, 6205 format, channelMask, frameCount, sessionId); 6206 6207 if (track->getCblk() == 0) { 6208 lStatus = NO_MEMORY; 6209 goto Exit; 6210 } 6211 mTracks.add(track); 6212 6213 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 6214 bool suspend = audio_is_bluetooth_sco_device(mDevice & AUDIO_DEVICE_IN_ALL) && 6215 mAudioFlinger->btNrecIsOff(); 6216 setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); 6217 setEffectSuspended_l(FX_IID_NS, suspend, sessionId); 6218 } 6219 lStatus = NO_ERROR; 6220 6221Exit: 6222 if (status) { 6223 *status = lStatus; 6224 } 6225 return track; 6226} 6227 6228status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, 6229 AudioSystem::sync_event_t event, 6230 int triggerSession) 6231{ 6232 ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); 6233 sp<ThreadBase> strongMe = this; 6234 status_t status = NO_ERROR; 6235 6236 if (event == AudioSystem::SYNC_EVENT_NONE) { 6237 clearSyncStartEvent(); 6238 } else if (event != AudioSystem::SYNC_EVENT_SAME) { 6239 mSyncStartEvent = mAudioFlinger->createSyncEvent(event, 6240 triggerSession, 6241 recordTrack->sessionId(), 6242 syncStartEventCallback, 6243 this); 6244 // Sync event can be cancelled by the trigger session if the track is not in a 6245 // compatible state in which case we start record immediately 6246 if (mSyncStartEvent->isCancelled()) { 6247 clearSyncStartEvent(); 6248 } else { 6249 // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs 6250 mFramestoDrop = - ((AudioSystem::kSyncRecordStartTimeOutMs * mReqSampleRate) / 1000); 6251 } 6252 } 6253 6254 { 6255 AutoMutex lock(mLock); 6256 if (mActiveTrack != 0) { 6257 if (recordTrack != mActiveTrack.get()) { 6258 status = -EBUSY; 6259 } else if (mActiveTrack->mState == TrackBase::PAUSING) { 6260 mActiveTrack->mState = TrackBase::ACTIVE; 6261 } 6262 return status; 6263 } 6264 6265 recordTrack->mState = TrackBase::IDLE; 6266 mActiveTrack = recordTrack; 6267 mLock.unlock(); 6268 status_t status = AudioSystem::startInput(mId); 6269 mLock.lock(); 6270 if (status != NO_ERROR) { 6271 mActiveTrack.clear(); 6272 clearSyncStartEvent(); 6273 return status; 6274 } 6275 mRsmpInIndex = mFrameCount; 6276 mBytesRead = 0; 6277 if (mResampler != NULL) { 6278 mResampler->reset(); 6279 } 6280 mActiveTrack->mState = TrackBase::RESUMING; 6281 // signal thread to start 6282 ALOGV("Signal record thread"); 6283 mWaitWorkCV.signal(); 6284 // do not wait for mStartStopCond if exiting 6285 if (exitPending()) { 6286 mActiveTrack.clear(); 6287 status = INVALID_OPERATION; 6288 goto startError; 6289 } 6290 mStartStopCond.wait(mLock); 6291 if (mActiveTrack == 0) { 6292 ALOGV("Record failed to start"); 6293 status = BAD_VALUE; 6294 goto startError; 6295 } 6296 ALOGV("Record started OK"); 6297 return status; 6298 } 6299startError: 6300 AudioSystem::stopInput(mId); 6301 clearSyncStartEvent(); 6302 return status; 6303} 6304 6305void AudioFlinger::RecordThread::clearSyncStartEvent() 6306{ 6307 if (mSyncStartEvent != 0) { 6308 mSyncStartEvent->cancel(); 6309 } 6310 mSyncStartEvent.clear(); 6311 mFramestoDrop = 0; 6312} 6313 6314void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) 6315{ 6316 sp<SyncEvent> strongEvent = event.promote(); 6317 6318 if (strongEvent != 0) { 6319 RecordThread *me = (RecordThread *)strongEvent->cookie(); 6320 me->handleSyncStartEvent(strongEvent); 6321 } 6322} 6323 6324void AudioFlinger::RecordThread::handleSyncStartEvent(const sp<SyncEvent>& event) 6325{ 6326 if (event == mSyncStartEvent) { 6327 // TODO: use actual buffer filling status instead of 2 buffers when info is available 6328 // from audio HAL 6329 mFramestoDrop = mFrameCount * 2; 6330 } 6331} 6332 6333bool AudioFlinger::RecordThread::stop_l(RecordThread::RecordTrack* recordTrack) { 6334 ALOGV("RecordThread::stop"); 6335 if (recordTrack != mActiveTrack.get() || recordTrack->mState == TrackBase::PAUSING) { 6336 return false; 6337 } 6338 recordTrack->mState = TrackBase::PAUSING; 6339 // do not wait for mStartStopCond if exiting 6340 if (exitPending()) { 6341 return true; 6342 } 6343 mStartStopCond.wait(mLock); 6344 // if we have been restarted, recordTrack == mActiveTrack.get() here 6345 if (exitPending() || recordTrack != mActiveTrack.get()) { 6346 ALOGV("Record stopped OK"); 6347 return true; 6348 } 6349 return false; 6350} 6351 6352bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event) 6353{ 6354 return false; 6355} 6356 6357status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event) 6358{ 6359 if (!isValidSyncEvent(event)) { 6360 return BAD_VALUE; 6361 } 6362 6363 int eventSession = event->triggerSession(); 6364 status_t ret = NAME_NOT_FOUND; 6365 6366 Mutex::Autolock _l(mLock); 6367 6368 for (size_t i = 0; i < mTracks.size(); i++) { 6369 sp<RecordTrack> track = mTracks[i]; 6370 if (eventSession == track->sessionId()) { 6371 track->setSyncEvent(event); 6372 ret = NO_ERROR; 6373 } 6374 } 6375 return ret; 6376} 6377 6378void AudioFlinger::RecordThread::RecordTrack::destroy() 6379{ 6380 // see comments at AudioFlinger::PlaybackThread::Track::destroy() 6381 sp<RecordTrack> keep(this); 6382 { 6383 sp<ThreadBase> thread = mThread.promote(); 6384 if (thread != 0) { 6385 if (mState == ACTIVE || mState == RESUMING) { 6386 AudioSystem::stopInput(thread->id()); 6387 } 6388 AudioSystem::releaseInput(thread->id()); 6389 Mutex::Autolock _l(thread->mLock); 6390 RecordThread *recordThread = (RecordThread *) thread.get(); 6391 recordThread->destroyTrack_l(this); 6392 } 6393 } 6394} 6395 6396// destroyTrack_l() must be called with ThreadBase::mLock held 6397void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) 6398{ 6399 track->mState = TrackBase::TERMINATED; 6400 // active tracks are removed by threadLoop() 6401 if (mActiveTrack != track) { 6402 removeTrack_l(track); 6403 } 6404} 6405 6406void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) 6407{ 6408 mTracks.remove(track); 6409 // need anything related to effects here? 6410} 6411 6412void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) 6413{ 6414 dumpInternals(fd, args); 6415 dumpTracks(fd, args); 6416 dumpEffectChains(fd, args); 6417} 6418 6419void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) 6420{ 6421 const size_t SIZE = 256; 6422 char buffer[SIZE]; 6423 String8 result; 6424 6425 snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); 6426 result.append(buffer); 6427 6428 if (mActiveTrack != 0) { 6429 snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); 6430 result.append(buffer); 6431 snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); 6432 result.append(buffer); 6433 snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); 6434 result.append(buffer); 6435 snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); 6436 result.append(buffer); 6437 snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); 6438 result.append(buffer); 6439 } else { 6440 result.append("No active record client\n"); 6441 } 6442 6443 write(fd, result.string(), result.size()); 6444 6445 dumpBase(fd, args); 6446} 6447 6448void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args) 6449{ 6450 const size_t SIZE = 256; 6451 char buffer[SIZE]; 6452 String8 result; 6453 6454 snprintf(buffer, SIZE, "Input thread %p tracks\n", this); 6455 result.append(buffer); 6456 RecordTrack::appendDumpHeader(result); 6457 for (size_t i = 0; i < mTracks.size(); ++i) { 6458 sp<RecordTrack> track = mTracks[i]; 6459 if (track != 0) { 6460 track->dump(buffer, SIZE); 6461 result.append(buffer); 6462 } 6463 } 6464 6465 if (mActiveTrack != 0) { 6466 snprintf(buffer, SIZE, "\nInput thread %p active tracks\n", this); 6467 result.append(buffer); 6468 RecordTrack::appendDumpHeader(result); 6469 mActiveTrack->dump(buffer, SIZE); 6470 result.append(buffer); 6471 6472 } 6473 write(fd, result.string(), result.size()); 6474} 6475 6476// AudioBufferProvider interface 6477status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) 6478{ 6479 size_t framesReq = buffer->frameCount; 6480 size_t framesReady = mFrameCount - mRsmpInIndex; 6481 int channelCount; 6482 6483 if (framesReady == 0) { 6484 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); 6485 if (mBytesRead < 0) { 6486 ALOGE("RecordThread::getNextBuffer() Error reading audio input"); 6487 if (mActiveTrack->mState == TrackBase::ACTIVE) { 6488 // Force input into standby so that it tries to 6489 // recover at next read attempt 6490 inputStandBy(); 6491 usleep(kRecordThreadSleepUs); 6492 } 6493 buffer->raw = NULL; 6494 buffer->frameCount = 0; 6495 return NOT_ENOUGH_DATA; 6496 } 6497 mRsmpInIndex = 0; 6498 framesReady = mFrameCount; 6499 } 6500 6501 if (framesReq > framesReady) { 6502 framesReq = framesReady; 6503 } 6504 6505 if (mChannelCount == 1 && mReqChannelCount == 2) { 6506 channelCount = 1; 6507 } else { 6508 channelCount = 2; 6509 } 6510 buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; 6511 buffer->frameCount = framesReq; 6512 return NO_ERROR; 6513} 6514 6515// AudioBufferProvider interface 6516void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) 6517{ 6518 mRsmpInIndex += buffer->frameCount; 6519 buffer->frameCount = 0; 6520} 6521 6522bool AudioFlinger::RecordThread::checkForNewParameters_l() 6523{ 6524 bool reconfig = false; 6525 6526 while (!mNewParameters.isEmpty()) { 6527 status_t status = NO_ERROR; 6528 String8 keyValuePair = mNewParameters[0]; 6529 AudioParameter param = AudioParameter(keyValuePair); 6530 int value; 6531 audio_format_t reqFormat = mFormat; 6532 int reqSamplingRate = mReqSampleRate; 6533 int reqChannelCount = mReqChannelCount; 6534 6535 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { 6536 reqSamplingRate = value; 6537 reconfig = true; 6538 } 6539 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { 6540 reqFormat = (audio_format_t) value; 6541 reconfig = true; 6542 } 6543 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { 6544 reqChannelCount = popcount(value); 6545 reconfig = true; 6546 } 6547 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { 6548 // do not accept frame count changes if tracks are open as the track buffer 6549 // size depends on frame count and correct behavior would not be guaranteed 6550 // if frame count is changed after track creation 6551 if (mActiveTrack != 0) { 6552 status = INVALID_OPERATION; 6553 } else { 6554 reconfig = true; 6555 } 6556 } 6557 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { 6558 // forward device change to effects that have requested to be 6559 // aware of attached audio device. 6560 for (size_t i = 0; i < mEffectChains.size(); i++) { 6561 mEffectChains[i]->setDevice_l(value); 6562 } 6563 // store input device and output device but do not forward output device to audio HAL. 6564 // Note that status is ignored by the caller for output device 6565 // (see AudioFlinger::setParameters() 6566 audio_devices_t newDevice = mDevice; 6567 if (value & AUDIO_DEVICE_OUT_ALL) { 6568 newDevice &= ~(value & AUDIO_DEVICE_OUT_ALL); 6569 status = BAD_VALUE; 6570 } else { 6571 newDevice &= ~(value & AUDIO_DEVICE_IN_ALL); 6572 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 6573 if (mTracks.size() > 0) { 6574 bool suspend = audio_is_bluetooth_sco_device( 6575 (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); 6576 for (size_t i = 0; i < mTracks.size(); i++) { 6577 sp<RecordTrack> track = mTracks[i]; 6578 setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); 6579 setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); 6580 } 6581 } 6582 } 6583 newDevice |= value; 6584 mDevice = newDevice; // since mDevice is read by other threads, only write to it once 6585 } 6586 if (status == NO_ERROR) { 6587 status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); 6588 if (status == INVALID_OPERATION) { 6589 inputStandBy(); 6590 status = mInput->stream->common.set_parameters(&mInput->stream->common, 6591 keyValuePair.string()); 6592 } 6593 if (reconfig) { 6594 if (status == BAD_VALUE && 6595 reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && 6596 reqFormat == AUDIO_FORMAT_PCM_16_BIT && 6597 ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && 6598 popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && 6599 (reqChannelCount <= FCC_2)) { 6600 status = NO_ERROR; 6601 } 6602 if (status == NO_ERROR) { 6603 readInputParameters(); 6604 sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); 6605 } 6606 } 6607 } 6608 6609 mNewParameters.removeAt(0); 6610 6611 mParamStatus = status; 6612 mParamCond.signal(); 6613 // wait for condition with time out in case the thread calling ThreadBase::setParameters() 6614 // already timed out waiting for the status and will never signal the condition. 6615 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); 6616 } 6617 return reconfig; 6618} 6619 6620String8 AudioFlinger::RecordThread::getParameters(const String8& keys) 6621{ 6622 char *s; 6623 String8 out_s8 = String8(); 6624 6625 Mutex::Autolock _l(mLock); 6626 if (initCheck() != NO_ERROR) { 6627 return out_s8; 6628 } 6629 6630 s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); 6631 out_s8 = String8(s); 6632 free(s); 6633 return out_s8; 6634} 6635 6636void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { 6637 AudioSystem::OutputDescriptor desc; 6638 void *param2 = NULL; 6639 6640 switch (event) { 6641 case AudioSystem::INPUT_OPENED: 6642 case AudioSystem::INPUT_CONFIG_CHANGED: 6643 desc.channels = mChannelMask; 6644 desc.samplingRate = mSampleRate; 6645 desc.format = mFormat; 6646 desc.frameCount = mFrameCount; 6647 desc.latency = 0; 6648 param2 = &desc; 6649 break; 6650 6651 case AudioSystem::INPUT_CLOSED: 6652 default: 6653 break; 6654 } 6655 mAudioFlinger->audioConfigChanged_l(event, mId, param2); 6656} 6657 6658void AudioFlinger::RecordThread::readInputParameters() 6659{ 6660 delete mRsmpInBuffer; 6661 // mRsmpInBuffer is always assigned a new[] below 6662 delete mRsmpOutBuffer; 6663 mRsmpOutBuffer = NULL; 6664 delete mResampler; 6665 mResampler = NULL; 6666 6667 mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); 6668 mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); 6669 mChannelCount = (uint16_t)popcount(mChannelMask); 6670 mFormat = mInput->stream->common.get_format(&mInput->stream->common); 6671 mFrameSize = audio_stream_frame_size(&mInput->stream->common); 6672 mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); 6673 mFrameCount = mInputBytes / mFrameSize; 6674 mNormalFrameCount = mFrameCount; // not used by record, but used by input effects 6675 mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; 6676 6677 if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2) 6678 { 6679 int channelCount; 6680 // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid 6681 // stereo to mono post process as the resampler always outputs stereo. 6682 if (mChannelCount == 1 && mReqChannelCount == 2) { 6683 channelCount = 1; 6684 } else { 6685 channelCount = 2; 6686 } 6687 mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); 6688 mResampler->setSampleRate(mSampleRate); 6689 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); 6690 mRsmpOutBuffer = new int32_t[mFrameCount * 2]; 6691 6692 // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples 6693 if (mChannelCount == 1 && mReqChannelCount == 1) { 6694 mFrameCount >>= 1; 6695 } 6696 6697 } 6698 mRsmpInIndex = mFrameCount; 6699} 6700 6701unsigned int AudioFlinger::RecordThread::getInputFramesLost() 6702{ 6703 Mutex::Autolock _l(mLock); 6704 if (initCheck() != NO_ERROR) { 6705 return 0; 6706 } 6707 6708 return mInput->stream->get_input_frames_lost(mInput->stream); 6709} 6710 6711uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) 6712{ 6713 Mutex::Autolock _l(mLock); 6714 uint32_t result = 0; 6715 if (getEffectChain_l(sessionId) != 0) { 6716 result = EFFECT_SESSION; 6717 } 6718 6719 for (size_t i = 0; i < mTracks.size(); ++i) { 6720 if (sessionId == mTracks[i]->sessionId()) { 6721 result |= TRACK_SESSION; 6722 break; 6723 } 6724 } 6725 6726 return result; 6727} 6728 6729KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() 6730{ 6731 KeyedVector<int, bool> ids; 6732 Mutex::Autolock _l(mLock); 6733 for (size_t j = 0; j < mTracks.size(); ++j) { 6734 sp<RecordThread::RecordTrack> track = mTracks[j]; 6735 int sessionId = track->sessionId(); 6736 if (ids.indexOfKey(sessionId) < 0) { 6737 ids.add(sessionId, true); 6738 } 6739 } 6740 return ids; 6741} 6742 6743AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() 6744{ 6745 Mutex::Autolock _l(mLock); 6746 AudioStreamIn *input = mInput; 6747 mInput = NULL; 6748 return input; 6749} 6750 6751// this method must always be called either with ThreadBase mLock held or inside the thread loop 6752audio_stream_t* AudioFlinger::RecordThread::stream() const 6753{ 6754 if (mInput == NULL) { 6755 return NULL; 6756 } 6757 return &mInput->stream->common; 6758} 6759 6760 6761// ---------------------------------------------------------------------------- 6762 6763audio_module_handle_t AudioFlinger::loadHwModule(const char *name) 6764{ 6765 if (!settingsAllowed()) { 6766 return 0; 6767 } 6768 Mutex::Autolock _l(mLock); 6769 return loadHwModule_l(name); 6770} 6771 6772// loadHwModule_l() must be called with AudioFlinger::mLock held 6773audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) 6774{ 6775 for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 6776 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { 6777 ALOGW("loadHwModule() module %s already loaded", name); 6778 return mAudioHwDevs.keyAt(i); 6779 } 6780 } 6781 6782 audio_hw_device_t *dev; 6783 6784 int rc = load_audio_interface(name, &dev); 6785 if (rc) { 6786 ALOGI("loadHwModule() error %d loading module %s ", rc, name); 6787 return 0; 6788 } 6789 6790 mHardwareStatus = AUDIO_HW_INIT; 6791 rc = dev->init_check(dev); 6792 mHardwareStatus = AUDIO_HW_IDLE; 6793 if (rc) { 6794 ALOGI("loadHwModule() init check error %d for module %s ", rc, name); 6795 return 0; 6796 } 6797 6798 // Check and cache this HAL's level of support for master mute and master 6799 // volume. If this is the first HAL opened, and it supports the get 6800 // methods, use the initial values provided by the HAL as the current 6801 // master mute and volume settings. 6802 6803 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); 6804 { // scope for auto-lock pattern 6805 AutoMutex lock(mHardwareLock); 6806 6807 if (0 == mAudioHwDevs.size()) { 6808 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; 6809 if (NULL != dev->get_master_volume) { 6810 float mv; 6811 if (OK == dev->get_master_volume(dev, &mv)) { 6812 mMasterVolume = mv; 6813 } 6814 } 6815 6816 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; 6817 if (NULL != dev->get_master_mute) { 6818 bool mm; 6819 if (OK == dev->get_master_mute(dev, &mm)) { 6820 mMasterMute = mm; 6821 } 6822 } 6823 } 6824 6825 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; 6826 if ((NULL != dev->set_master_volume) && 6827 (OK == dev->set_master_volume(dev, mMasterVolume))) { 6828 flags = static_cast<AudioHwDevice::Flags>(flags | 6829 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); 6830 } 6831 6832 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; 6833 if ((NULL != dev->set_master_mute) && 6834 (OK == dev->set_master_mute(dev, mMasterMute))) { 6835 flags = static_cast<AudioHwDevice::Flags>(flags | 6836 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); 6837 } 6838 6839 mHardwareStatus = AUDIO_HW_IDLE; 6840 } 6841 6842 audio_module_handle_t handle = nextUniqueId(); 6843 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags)); 6844 6845 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", 6846 name, dev->common.module->name, dev->common.module->id, handle); 6847 6848 return handle; 6849 6850} 6851 6852audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module, 6853 audio_devices_t *pDevices, 6854 uint32_t *pSamplingRate, 6855 audio_format_t *pFormat, 6856 audio_channel_mask_t *pChannelMask, 6857 uint32_t *pLatencyMs, 6858 audio_output_flags_t flags) 6859{ 6860 status_t status; 6861 PlaybackThread *thread = NULL; 6862 struct audio_config config = { 6863 sample_rate: pSamplingRate ? *pSamplingRate : 0, 6864 channel_mask: pChannelMask ? *pChannelMask : 0, 6865 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, 6866 }; 6867 audio_stream_out_t *outStream = NULL; 6868 AudioHwDevice *outHwDev; 6869 6870 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", 6871 module, 6872 (pDevices != NULL) ? *pDevices : 0, 6873 config.sample_rate, 6874 config.format, 6875 config.channel_mask, 6876 flags); 6877 6878 if (pDevices == NULL || *pDevices == 0) { 6879 return 0; 6880 } 6881 6882 Mutex::Autolock _l(mLock); 6883 6884 outHwDev = findSuitableHwDev_l(module, *pDevices); 6885 if (outHwDev == NULL) 6886 return 0; 6887 6888 audio_hw_device_t *hwDevHal = outHwDev->hwDevice(); 6889 audio_io_handle_t id = nextUniqueId(); 6890 6891 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; 6892 6893 status = hwDevHal->open_output_stream(hwDevHal, 6894 id, 6895 *pDevices, 6896 (audio_output_flags_t)flags, 6897 &config, 6898 &outStream); 6899 6900 mHardwareStatus = AUDIO_HW_IDLE; 6901 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", 6902 outStream, 6903 config.sample_rate, 6904 config.format, 6905 config.channel_mask, 6906 status); 6907 6908 if (status == NO_ERROR && outStream != NULL) { 6909 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); 6910 6911 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) || 6912 (config.format != AUDIO_FORMAT_PCM_16_BIT) || 6913 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) { 6914 thread = new DirectOutputThread(this, output, id, *pDevices); 6915 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); 6916 } else { 6917 thread = new MixerThread(this, output, id, *pDevices); 6918 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); 6919 } 6920 mPlaybackThreads.add(id, thread); 6921 6922 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate; 6923 if (pFormat != NULL) *pFormat = config.format; 6924 if (pChannelMask != NULL) *pChannelMask = config.channel_mask; 6925 if (pLatencyMs != NULL) *pLatencyMs = thread->latency(); 6926 6927 // notify client processes of the new output creation 6928 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); 6929 6930 // the first primary output opened designates the primary hw device 6931 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { 6932 ALOGI("Using module %d has the primary audio interface", module); 6933 mPrimaryHardwareDev = outHwDev; 6934 6935 AutoMutex lock(mHardwareLock); 6936 mHardwareStatus = AUDIO_HW_SET_MODE; 6937 hwDevHal->set_mode(hwDevHal, mMode); 6938 mHardwareStatus = AUDIO_HW_IDLE; 6939 } 6940 return id; 6941 } 6942 6943 return 0; 6944} 6945 6946audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, 6947 audio_io_handle_t output2) 6948{ 6949 Mutex::Autolock _l(mLock); 6950 MixerThread *thread1 = checkMixerThread_l(output1); 6951 MixerThread *thread2 = checkMixerThread_l(output2); 6952 6953 if (thread1 == NULL || thread2 == NULL) { 6954 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); 6955 return 0; 6956 } 6957 6958 audio_io_handle_t id = nextUniqueId(); 6959 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); 6960 thread->addOutputTrack(thread2); 6961 mPlaybackThreads.add(id, thread); 6962 // notify client processes of the new output creation 6963 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); 6964 return id; 6965} 6966 6967status_t AudioFlinger::closeOutput(audio_io_handle_t output) 6968{ 6969 return closeOutput_nonvirtual(output); 6970} 6971 6972status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) 6973{ 6974 // keep strong reference on the playback thread so that 6975 // it is not destroyed while exit() is executed 6976 sp<PlaybackThread> thread; 6977 { 6978 Mutex::Autolock _l(mLock); 6979 thread = checkPlaybackThread_l(output); 6980 if (thread == NULL) { 6981 return BAD_VALUE; 6982 } 6983 6984 ALOGV("closeOutput() %d", output); 6985 6986 if (thread->type() == ThreadBase::MIXER) { 6987 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 6988 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { 6989 DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); 6990 dupThread->removeOutputTrack((MixerThread *)thread.get()); 6991 } 6992 } 6993 } 6994 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); 6995 mPlaybackThreads.removeItem(output); 6996 } 6997 thread->exit(); 6998 // The thread entity (active unit of execution) is no longer running here, 6999 // but the ThreadBase container still exists. 7000 7001 if (thread->type() != ThreadBase::DUPLICATING) { 7002 AudioStreamOut *out = thread->clearOutput(); 7003 ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); 7004 // from now on thread->mOutput is NULL 7005 out->hwDev()->close_output_stream(out->hwDev(), out->stream); 7006 delete out; 7007 } 7008 return NO_ERROR; 7009} 7010 7011status_t AudioFlinger::suspendOutput(audio_io_handle_t output) 7012{ 7013 Mutex::Autolock _l(mLock); 7014 PlaybackThread *thread = checkPlaybackThread_l(output); 7015 7016 if (thread == NULL) { 7017 return BAD_VALUE; 7018 } 7019 7020 ALOGV("suspendOutput() %d", output); 7021 thread->suspend(); 7022 7023 return NO_ERROR; 7024} 7025 7026status_t AudioFlinger::restoreOutput(audio_io_handle_t output) 7027{ 7028 Mutex::Autolock _l(mLock); 7029 PlaybackThread *thread = checkPlaybackThread_l(output); 7030 7031 if (thread == NULL) { 7032 return BAD_VALUE; 7033 } 7034 7035 ALOGV("restoreOutput() %d", output); 7036 7037 thread->restore(); 7038 7039 return NO_ERROR; 7040} 7041 7042audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module, 7043 audio_devices_t *pDevices, 7044 uint32_t *pSamplingRate, 7045 audio_format_t *pFormat, 7046 audio_channel_mask_t *pChannelMask) 7047{ 7048 status_t status; 7049 RecordThread *thread = NULL; 7050 struct audio_config config = { 7051 sample_rate: pSamplingRate ? *pSamplingRate : 0, 7052 channel_mask: pChannelMask ? *pChannelMask : 0, 7053 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, 7054 }; 7055 uint32_t reqSamplingRate = config.sample_rate; 7056 audio_format_t reqFormat = config.format; 7057 audio_channel_mask_t reqChannels = config.channel_mask; 7058 audio_stream_in_t *inStream = NULL; 7059 AudioHwDevice *inHwDev; 7060 7061 if (pDevices == NULL || *pDevices == 0) { 7062 return 0; 7063 } 7064 7065 Mutex::Autolock _l(mLock); 7066 7067 inHwDev = findSuitableHwDev_l(module, *pDevices); 7068 if (inHwDev == NULL) 7069 return 0; 7070 7071 audio_hw_device_t *inHwHal = inHwDev->hwDevice(); 7072 audio_io_handle_t id = nextUniqueId(); 7073 7074 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, 7075 &inStream); 7076 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, status %d", 7077 inStream, 7078 config.sample_rate, 7079 config.format, 7080 config.channel_mask, 7081 status); 7082 7083 // If the input could not be opened with the requested parameters and we can handle the conversion internally, 7084 // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo 7085 // or stereo to mono conversions on 16 bit PCM inputs. 7086 if (status == BAD_VALUE && 7087 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT && 7088 (config.sample_rate <= 2 * reqSamplingRate) && 7089 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) { 7090 ALOGV("openInput() reopening with proposed sampling rate and channel mask"); 7091 inStream = NULL; 7092 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream); 7093 } 7094 7095 if (status == NO_ERROR && inStream != NULL) { 7096 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); 7097 7098 // Start record thread 7099 // RecorThread require both input and output device indication to forward to audio 7100 // pre processing modules 7101 audio_devices_t device = (*pDevices) | primaryOutputDevice_l(); 7102 thread = new RecordThread(this, 7103 input, 7104 reqSamplingRate, 7105 reqChannels, 7106 id, 7107 device); 7108 mRecordThreads.add(id, thread); 7109 ALOGV("openInput() created record thread: ID %d thread %p", id, thread); 7110 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate; 7111 if (pFormat != NULL) *pFormat = config.format; 7112 if (pChannelMask != NULL) *pChannelMask = reqChannels; 7113 7114 // notify client processes of the new input creation 7115 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); 7116 return id; 7117 } 7118 7119 return 0; 7120} 7121 7122status_t AudioFlinger::closeInput(audio_io_handle_t input) 7123{ 7124 return closeInput_nonvirtual(input); 7125} 7126 7127status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) 7128{ 7129 // keep strong reference on the record thread so that 7130 // it is not destroyed while exit() is executed 7131 sp<RecordThread> thread; 7132 { 7133 Mutex::Autolock _l(mLock); 7134 thread = checkRecordThread_l(input); 7135 if (thread == 0) { 7136 return BAD_VALUE; 7137 } 7138 7139 ALOGV("closeInput() %d", input); 7140 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); 7141 mRecordThreads.removeItem(input); 7142 } 7143 thread->exit(); 7144 // The thread entity (active unit of execution) is no longer running here, 7145 // but the ThreadBase container still exists. 7146 7147 AudioStreamIn *in = thread->clearInput(); 7148 ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); 7149 // from now on thread->mInput is NULL 7150 in->hwDev()->close_input_stream(in->hwDev(), in->stream); 7151 delete in; 7152 7153 return NO_ERROR; 7154} 7155 7156status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output) 7157{ 7158 Mutex::Autolock _l(mLock); 7159 ALOGV("setStreamOutput() stream %d to output %d", stream, output); 7160 7161 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 7162 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); 7163 thread->invalidateTracks(stream); 7164 } 7165 7166 return NO_ERROR; 7167} 7168 7169 7170int AudioFlinger::newAudioSessionId() 7171{ 7172 return nextUniqueId(); 7173} 7174 7175void AudioFlinger::acquireAudioSessionId(int audioSession) 7176{ 7177 Mutex::Autolock _l(mLock); 7178 pid_t caller = IPCThreadState::self()->getCallingPid(); 7179 ALOGV("acquiring %d from %d", audioSession, caller); 7180 size_t num = mAudioSessionRefs.size(); 7181 for (size_t i = 0; i< num; i++) { 7182 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); 7183 if (ref->mSessionid == audioSession && ref->mPid == caller) { 7184 ref->mCnt++; 7185 ALOGV(" incremented refcount to %d", ref->mCnt); 7186 return; 7187 } 7188 } 7189 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); 7190 ALOGV(" added new entry for %d", audioSession); 7191} 7192 7193void AudioFlinger::releaseAudioSessionId(int audioSession) 7194{ 7195 Mutex::Autolock _l(mLock); 7196 pid_t caller = IPCThreadState::self()->getCallingPid(); 7197 ALOGV("releasing %d from %d", audioSession, caller); 7198 size_t num = mAudioSessionRefs.size(); 7199 for (size_t i = 0; i< num; i++) { 7200 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); 7201 if (ref->mSessionid == audioSession && ref->mPid == caller) { 7202 ref->mCnt--; 7203 ALOGV(" decremented refcount to %d", ref->mCnt); 7204 if (ref->mCnt == 0) { 7205 mAudioSessionRefs.removeAt(i); 7206 delete ref; 7207 purgeStaleEffects_l(); 7208 } 7209 return; 7210 } 7211 } 7212 ALOGW("session id %d not found for pid %d", audioSession, caller); 7213} 7214 7215void AudioFlinger::purgeStaleEffects_l() { 7216 7217 ALOGV("purging stale effects"); 7218 7219 Vector< sp<EffectChain> > chains; 7220 7221 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 7222 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 7223 for (size_t j = 0; j < t->mEffectChains.size(); j++) { 7224 sp<EffectChain> ec = t->mEffectChains[j]; 7225 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { 7226 chains.push(ec); 7227 } 7228 } 7229 } 7230 for (size_t i = 0; i < mRecordThreads.size(); i++) { 7231 sp<RecordThread> t = mRecordThreads.valueAt(i); 7232 for (size_t j = 0; j < t->mEffectChains.size(); j++) { 7233 sp<EffectChain> ec = t->mEffectChains[j]; 7234 chains.push(ec); 7235 } 7236 } 7237 7238 for (size_t i = 0; i < chains.size(); i++) { 7239 sp<EffectChain> ec = chains[i]; 7240 int sessionid = ec->sessionId(); 7241 sp<ThreadBase> t = ec->mThread.promote(); 7242 if (t == 0) { 7243 continue; 7244 } 7245 size_t numsessionrefs = mAudioSessionRefs.size(); 7246 bool found = false; 7247 for (size_t k = 0; k < numsessionrefs; k++) { 7248 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); 7249 if (ref->mSessionid == sessionid) { 7250 ALOGV(" session %d still exists for %d with %d refs", 7251 sessionid, ref->mPid, ref->mCnt); 7252 found = true; 7253 break; 7254 } 7255 } 7256 if (!found) { 7257 Mutex::Autolock _l (t->mLock); 7258 // remove all effects from the chain 7259 while (ec->mEffects.size()) { 7260 sp<EffectModule> effect = ec->mEffects[0]; 7261 effect->unPin(); 7262 t->removeEffect_l(effect); 7263 if (effect->purgeHandles()) { 7264 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); 7265 } 7266 AudioSystem::unregisterEffect(effect->id()); 7267 } 7268 } 7269 } 7270 return; 7271} 7272 7273// checkPlaybackThread_l() must be called with AudioFlinger::mLock held 7274AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const 7275{ 7276 return mPlaybackThreads.valueFor(output).get(); 7277} 7278 7279// checkMixerThread_l() must be called with AudioFlinger::mLock held 7280AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const 7281{ 7282 PlaybackThread *thread = checkPlaybackThread_l(output); 7283 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; 7284} 7285 7286// checkRecordThread_l() must be called with AudioFlinger::mLock held 7287AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const 7288{ 7289 return mRecordThreads.valueFor(input).get(); 7290} 7291 7292uint32_t AudioFlinger::nextUniqueId() 7293{ 7294 return android_atomic_inc(&mNextUniqueId); 7295} 7296 7297AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const 7298{ 7299 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 7300 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); 7301 AudioStreamOut *output = thread->getOutput(); 7302 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { 7303 return thread; 7304 } 7305 } 7306 return NULL; 7307} 7308 7309audio_devices_t AudioFlinger::primaryOutputDevice_l() const 7310{ 7311 PlaybackThread *thread = primaryPlaybackThread_l(); 7312 7313 if (thread == NULL) { 7314 return 0; 7315 } 7316 7317 return thread->device(); 7318} 7319 7320sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, 7321 int triggerSession, 7322 int listenerSession, 7323 sync_event_callback_t callBack, 7324 void *cookie) 7325{ 7326 Mutex::Autolock _l(mLock); 7327 7328 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); 7329 status_t playStatus = NAME_NOT_FOUND; 7330 status_t recStatus = NAME_NOT_FOUND; 7331 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 7332 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); 7333 if (playStatus == NO_ERROR) { 7334 return event; 7335 } 7336 } 7337 for (size_t i = 0; i < mRecordThreads.size(); i++) { 7338 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); 7339 if (recStatus == NO_ERROR) { 7340 return event; 7341 } 7342 } 7343 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { 7344 mPendingSyncEvents.add(event); 7345 } else { 7346 ALOGV("createSyncEvent() invalid event %d", event->type()); 7347 event.clear(); 7348 } 7349 return event; 7350} 7351 7352// ---------------------------------------------------------------------------- 7353// Effect management 7354// ---------------------------------------------------------------------------- 7355 7356 7357status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const 7358{ 7359 Mutex::Autolock _l(mLock); 7360 return EffectQueryNumberEffects(numEffects); 7361} 7362 7363status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const 7364{ 7365 Mutex::Autolock _l(mLock); 7366 return EffectQueryEffect(index, descriptor); 7367} 7368 7369status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, 7370 effect_descriptor_t *descriptor) const 7371{ 7372 Mutex::Autolock _l(mLock); 7373 return EffectGetDescriptor(pUuid, descriptor); 7374} 7375 7376 7377sp<IEffect> AudioFlinger::createEffect(pid_t pid, 7378 effect_descriptor_t *pDesc, 7379 const sp<IEffectClient>& effectClient, 7380 int32_t priority, 7381 audio_io_handle_t io, 7382 int sessionId, 7383 status_t *status, 7384 int *id, 7385 int *enabled) 7386{ 7387 status_t lStatus = NO_ERROR; 7388 sp<EffectHandle> handle; 7389 effect_descriptor_t desc; 7390 7391 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", 7392 pid, effectClient.get(), priority, sessionId, io); 7393 7394 if (pDesc == NULL) { 7395 lStatus = BAD_VALUE; 7396 goto Exit; 7397 } 7398 7399 // check audio settings permission for global effects 7400 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { 7401 lStatus = PERMISSION_DENIED; 7402 goto Exit; 7403 } 7404 7405 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects 7406 // that can only be created by audio policy manager (running in same process) 7407 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { 7408 lStatus = PERMISSION_DENIED; 7409 goto Exit; 7410 } 7411 7412 if (io == 0) { 7413 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { 7414 // output must be specified by AudioPolicyManager when using session 7415 // AUDIO_SESSION_OUTPUT_STAGE 7416 lStatus = BAD_VALUE; 7417 goto Exit; 7418 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 7419 // if the output returned by getOutputForEffect() is removed before we lock the 7420 // mutex below, the call to checkPlaybackThread_l(io) below will detect it 7421 // and we will exit safely 7422 io = AudioSystem::getOutputForEffect(&desc); 7423 } 7424 } 7425 7426 { 7427 Mutex::Autolock _l(mLock); 7428 7429 7430 if (!EffectIsNullUuid(&pDesc->uuid)) { 7431 // if uuid is specified, request effect descriptor 7432 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); 7433 if (lStatus < 0) { 7434 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); 7435 goto Exit; 7436 } 7437 } else { 7438 // if uuid is not specified, look for an available implementation 7439 // of the required type in effect factory 7440 if (EffectIsNullUuid(&pDesc->type)) { 7441 ALOGW("createEffect() no effect type"); 7442 lStatus = BAD_VALUE; 7443 goto Exit; 7444 } 7445 uint32_t numEffects = 0; 7446 effect_descriptor_t d; 7447 d.flags = 0; // prevent compiler warning 7448 bool found = false; 7449 7450 lStatus = EffectQueryNumberEffects(&numEffects); 7451 if (lStatus < 0) { 7452 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); 7453 goto Exit; 7454 } 7455 for (uint32_t i = 0; i < numEffects; i++) { 7456 lStatus = EffectQueryEffect(i, &desc); 7457 if (lStatus < 0) { 7458 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); 7459 continue; 7460 } 7461 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { 7462 // If matching type found save effect descriptor. If the session is 7463 // 0 and the effect is not auxiliary, continue enumeration in case 7464 // an auxiliary version of this effect type is available 7465 found = true; 7466 d = desc; 7467 if (sessionId != AUDIO_SESSION_OUTPUT_MIX || 7468 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 7469 break; 7470 } 7471 } 7472 } 7473 if (!found) { 7474 lStatus = BAD_VALUE; 7475 ALOGW("createEffect() effect not found"); 7476 goto Exit; 7477 } 7478 // For same effect type, chose auxiliary version over insert version if 7479 // connect to output mix (Compliance to OpenSL ES) 7480 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && 7481 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { 7482 desc = d; 7483 } 7484 } 7485 7486 // Do not allow auxiliary effects on a session different from 0 (output mix) 7487 if (sessionId != AUDIO_SESSION_OUTPUT_MIX && 7488 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 7489 lStatus = INVALID_OPERATION; 7490 goto Exit; 7491 } 7492 7493 // check recording permission for visualizer 7494 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && 7495 !recordingAllowed()) { 7496 lStatus = PERMISSION_DENIED; 7497 goto Exit; 7498 } 7499 7500 // return effect descriptor 7501 *pDesc = desc; 7502 7503 // If output is not specified try to find a matching audio session ID in one of the 7504 // output threads. 7505 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX 7506 // because of code checking output when entering the function. 7507 // Note: io is never 0 when creating an effect on an input 7508 if (io == 0) { 7509 // look for the thread where the specified audio session is present 7510 for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 7511 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { 7512 io = mPlaybackThreads.keyAt(i); 7513 break; 7514 } 7515 } 7516 if (io == 0) { 7517 for (size_t i = 0; i < mRecordThreads.size(); i++) { 7518 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { 7519 io = mRecordThreads.keyAt(i); 7520 break; 7521 } 7522 } 7523 } 7524 // If no output thread contains the requested session ID, default to 7525 // first output. The effect chain will be moved to the correct output 7526 // thread when a track with the same session ID is created 7527 if (io == 0 && mPlaybackThreads.size()) { 7528 io = mPlaybackThreads.keyAt(0); 7529 } 7530 ALOGV("createEffect() got io %d for effect %s", io, desc.name); 7531 } 7532 ThreadBase *thread = checkRecordThread_l(io); 7533 if (thread == NULL) { 7534 thread = checkPlaybackThread_l(io); 7535 if (thread == NULL) { 7536 ALOGE("createEffect() unknown output thread"); 7537 lStatus = BAD_VALUE; 7538 goto Exit; 7539 } 7540 } 7541 7542 sp<Client> client = registerPid_l(pid); 7543 7544 // create effect on selected output thread 7545 handle = thread->createEffect_l(client, effectClient, priority, sessionId, 7546 &desc, enabled, &lStatus); 7547 if (handle != 0 && id != NULL) { 7548 *id = handle->id(); 7549 } 7550 } 7551 7552Exit: 7553 if (status != NULL) { 7554 *status = lStatus; 7555 } 7556 return handle; 7557} 7558 7559status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, 7560 audio_io_handle_t dstOutput) 7561{ 7562 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", 7563 sessionId, srcOutput, dstOutput); 7564 Mutex::Autolock _l(mLock); 7565 if (srcOutput == dstOutput) { 7566 ALOGW("moveEffects() same dst and src outputs %d", dstOutput); 7567 return NO_ERROR; 7568 } 7569 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); 7570 if (srcThread == NULL) { 7571 ALOGW("moveEffects() bad srcOutput %d", srcOutput); 7572 return BAD_VALUE; 7573 } 7574 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); 7575 if (dstThread == NULL) { 7576 ALOGW("moveEffects() bad dstOutput %d", dstOutput); 7577 return BAD_VALUE; 7578 } 7579 7580 Mutex::Autolock _dl(dstThread->mLock); 7581 Mutex::Autolock _sl(srcThread->mLock); 7582 moveEffectChain_l(sessionId, srcThread, dstThread, false); 7583 7584 return NO_ERROR; 7585} 7586 7587// moveEffectChain_l must be called with both srcThread and dstThread mLocks held 7588status_t AudioFlinger::moveEffectChain_l(int sessionId, 7589 AudioFlinger::PlaybackThread *srcThread, 7590 AudioFlinger::PlaybackThread *dstThread, 7591 bool reRegister) 7592{ 7593 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", 7594 sessionId, srcThread, dstThread); 7595 7596 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); 7597 if (chain == 0) { 7598 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", 7599 sessionId, srcThread); 7600 return INVALID_OPERATION; 7601 } 7602 7603 // remove chain first. This is useful only if reconfiguring effect chain on same output thread, 7604 // so that a new chain is created with correct parameters when first effect is added. This is 7605 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is 7606 // removed. 7607 srcThread->removeEffectChain_l(chain); 7608 7609 // transfer all effects one by one so that new effect chain is created on new thread with 7610 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly 7611 audio_io_handle_t dstOutput = dstThread->id(); 7612 sp<EffectChain> dstChain; 7613 uint32_t strategy = 0; // prevent compiler warning 7614 sp<EffectModule> effect = chain->getEffectFromId_l(0); 7615 while (effect != 0) { 7616 srcThread->removeEffect_l(effect); 7617 dstThread->addEffect_l(effect); 7618 // removeEffect_l() has stopped the effect if it was active so it must be restarted 7619 if (effect->state() == EffectModule::ACTIVE || 7620 effect->state() == EffectModule::STOPPING) { 7621 effect->start(); 7622 } 7623 // if the move request is not received from audio policy manager, the effect must be 7624 // re-registered with the new strategy and output 7625 if (dstChain == 0) { 7626 dstChain = effect->chain().promote(); 7627 if (dstChain == 0) { 7628 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); 7629 srcThread->addEffect_l(effect); 7630 return NO_INIT; 7631 } 7632 strategy = dstChain->strategy(); 7633 } 7634 if (reRegister) { 7635 AudioSystem::unregisterEffect(effect->id()); 7636 AudioSystem::registerEffect(&effect->desc(), 7637 dstOutput, 7638 strategy, 7639 sessionId, 7640 effect->id()); 7641 } 7642 effect = chain->getEffectFromId_l(0); 7643 } 7644 7645 return NO_ERROR; 7646} 7647 7648 7649// PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held 7650sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( 7651 const sp<AudioFlinger::Client>& client, 7652 const sp<IEffectClient>& effectClient, 7653 int32_t priority, 7654 int sessionId, 7655 effect_descriptor_t *desc, 7656 int *enabled, 7657 status_t *status 7658 ) 7659{ 7660 sp<EffectModule> effect; 7661 sp<EffectHandle> handle; 7662 status_t lStatus; 7663 sp<EffectChain> chain; 7664 bool chainCreated = false; 7665 bool effectCreated = false; 7666 bool effectRegistered = false; 7667 7668 lStatus = initCheck(); 7669 if (lStatus != NO_ERROR) { 7670 ALOGW("createEffect_l() Audio driver not initialized."); 7671 goto Exit; 7672 } 7673 7674 // Do not allow effects with session ID 0 on direct output or duplicating threads 7675 // TODO: add rule for hw accelerated effects on direct outputs with non PCM format 7676 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { 7677 ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", 7678 desc->name, sessionId); 7679 lStatus = BAD_VALUE; 7680 goto Exit; 7681 } 7682 // Only Pre processor effects are allowed on input threads and only on input threads 7683 if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { 7684 ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", 7685 desc->name, desc->flags, mType); 7686 lStatus = BAD_VALUE; 7687 goto Exit; 7688 } 7689 7690 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); 7691 7692 { // scope for mLock 7693 Mutex::Autolock _l(mLock); 7694 7695 // check for existing effect chain with the requested audio session 7696 chain = getEffectChain_l(sessionId); 7697 if (chain == 0) { 7698 // create a new chain for this session 7699 ALOGV("createEffect_l() new effect chain for session %d", sessionId); 7700 chain = new EffectChain(this, sessionId); 7701 addEffectChain_l(chain); 7702 chain->setStrategy(getStrategyForSession_l(sessionId)); 7703 chainCreated = true; 7704 } else { 7705 effect = chain->getEffectFromDesc_l(desc); 7706 } 7707 7708 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); 7709 7710 if (effect == 0) { 7711 int id = mAudioFlinger->nextUniqueId(); 7712 // Check CPU and memory usage 7713 lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); 7714 if (lStatus != NO_ERROR) { 7715 goto Exit; 7716 } 7717 effectRegistered = true; 7718 // create a new effect module if none present in the chain 7719 effect = new EffectModule(this, chain, desc, id, sessionId); 7720 lStatus = effect->status(); 7721 if (lStatus != NO_ERROR) { 7722 goto Exit; 7723 } 7724 lStatus = chain->addEffect_l(effect); 7725 if (lStatus != NO_ERROR) { 7726 goto Exit; 7727 } 7728 effectCreated = true; 7729 7730 effect->setDevice(mDevice); 7731 effect->setMode(mAudioFlinger->getMode()); 7732 } 7733 // create effect handle and connect it to effect module 7734 handle = new EffectHandle(effect, client, effectClient, priority); 7735 lStatus = effect->addHandle(handle.get()); 7736 if (enabled != NULL) { 7737 *enabled = (int)effect->isEnabled(); 7738 } 7739 } 7740 7741Exit: 7742 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { 7743 Mutex::Autolock _l(mLock); 7744 if (effectCreated) { 7745 chain->removeEffect_l(effect); 7746 } 7747 if (effectRegistered) { 7748 AudioSystem::unregisterEffect(effect->id()); 7749 } 7750 if (chainCreated) { 7751 removeEffectChain_l(chain); 7752 } 7753 handle.clear(); 7754 } 7755 7756 if (status != NULL) { 7757 *status = lStatus; 7758 } 7759 return handle; 7760} 7761 7762sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) 7763{ 7764 Mutex::Autolock _l(mLock); 7765 return getEffect_l(sessionId, effectId); 7766} 7767 7768sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) 7769{ 7770 sp<EffectChain> chain = getEffectChain_l(sessionId); 7771 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; 7772} 7773 7774// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and 7775// PlaybackThread::mLock held 7776status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) 7777{ 7778 // check for existing effect chain with the requested audio session 7779 int sessionId = effect->sessionId(); 7780 sp<EffectChain> chain = getEffectChain_l(sessionId); 7781 bool chainCreated = false; 7782 7783 if (chain == 0) { 7784 // create a new chain for this session 7785 ALOGV("addEffect_l() new effect chain for session %d", sessionId); 7786 chain = new EffectChain(this, sessionId); 7787 addEffectChain_l(chain); 7788 chain->setStrategy(getStrategyForSession_l(sessionId)); 7789 chainCreated = true; 7790 } 7791 ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); 7792 7793 if (chain->getEffectFromId_l(effect->id()) != 0) { 7794 ALOGW("addEffect_l() %p effect %s already present in chain %p", 7795 this, effect->desc().name, chain.get()); 7796 return BAD_VALUE; 7797 } 7798 7799 status_t status = chain->addEffect_l(effect); 7800 if (status != NO_ERROR) { 7801 if (chainCreated) { 7802 removeEffectChain_l(chain); 7803 } 7804 return status; 7805 } 7806 7807 effect->setDevice(mDevice); 7808 effect->setMode(mAudioFlinger->getMode()); 7809 return NO_ERROR; 7810} 7811 7812void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { 7813 7814 ALOGV("removeEffect_l() %p effect %p", this, effect.get()); 7815 effect_descriptor_t desc = effect->desc(); 7816 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 7817 detachAuxEffect_l(effect->id()); 7818 } 7819 7820 sp<EffectChain> chain = effect->chain().promote(); 7821 if (chain != 0) { 7822 // remove effect chain if removing last effect 7823 if (chain->removeEffect_l(effect) == 0) { 7824 removeEffectChain_l(chain); 7825 } 7826 } else { 7827 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); 7828 } 7829} 7830 7831void AudioFlinger::ThreadBase::lockEffectChains_l( 7832 Vector< sp<AudioFlinger::EffectChain> >& effectChains) 7833{ 7834 effectChains = mEffectChains; 7835 for (size_t i = 0; i < mEffectChains.size(); i++) { 7836 mEffectChains[i]->lock(); 7837 } 7838} 7839 7840void AudioFlinger::ThreadBase::unlockEffectChains( 7841 const Vector< sp<AudioFlinger::EffectChain> >& effectChains) 7842{ 7843 for (size_t i = 0; i < effectChains.size(); i++) { 7844 effectChains[i]->unlock(); 7845 } 7846} 7847 7848sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) 7849{ 7850 Mutex::Autolock _l(mLock); 7851 return getEffectChain_l(sessionId); 7852} 7853 7854sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) 7855{ 7856 size_t size = mEffectChains.size(); 7857 for (size_t i = 0; i < size; i++) { 7858 if (mEffectChains[i]->sessionId() == sessionId) { 7859 return mEffectChains[i]; 7860 } 7861 } 7862 return 0; 7863} 7864 7865void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) 7866{ 7867 Mutex::Autolock _l(mLock); 7868 size_t size = mEffectChains.size(); 7869 for (size_t i = 0; i < size; i++) { 7870 mEffectChains[i]->setMode_l(mode); 7871 } 7872} 7873 7874void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, 7875 EffectHandle *handle, 7876 bool unpinIfLast) { 7877 7878 Mutex::Autolock _l(mLock); 7879 ALOGV("disconnectEffect() %p effect %p", this, effect.get()); 7880 // delete the effect module if removing last handle on it 7881 if (effect->removeHandle(handle) == 0) { 7882 if (!effect->isPinned() || unpinIfLast) { 7883 removeEffect_l(effect); 7884 AudioSystem::unregisterEffect(effect->id()); 7885 } 7886 } 7887} 7888 7889status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) 7890{ 7891 int session = chain->sessionId(); 7892 int16_t *buffer = mMixBuffer; 7893 bool ownsBuffer = false; 7894 7895 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); 7896 if (session > 0) { 7897 // Only one effect chain can be present in direct output thread and it uses 7898 // the mix buffer as input 7899 if (mType != DIRECT) { 7900 size_t numSamples = mNormalFrameCount * mChannelCount; 7901 buffer = new int16_t[numSamples]; 7902 memset(buffer, 0, numSamples * sizeof(int16_t)); 7903 ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); 7904 ownsBuffer = true; 7905 } 7906 7907 // Attach all tracks with same session ID to this chain. 7908 for (size_t i = 0; i < mTracks.size(); ++i) { 7909 sp<Track> track = mTracks[i]; 7910 if (session == track->sessionId()) { 7911 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); 7912 track->setMainBuffer(buffer); 7913 chain->incTrackCnt(); 7914 } 7915 } 7916 7917 // indicate all active tracks in the chain 7918 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 7919 sp<Track> track = mActiveTracks[i].promote(); 7920 if (track == 0) continue; 7921 if (session == track->sessionId()) { 7922 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); 7923 chain->incActiveTrackCnt(); 7924 } 7925 } 7926 } 7927 7928 chain->setInBuffer(buffer, ownsBuffer); 7929 chain->setOutBuffer(mMixBuffer); 7930 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect 7931 // chains list in order to be processed last as it contains output stage effects 7932 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before 7933 // session AUDIO_SESSION_OUTPUT_STAGE to be processed 7934 // after track specific effects and before output stage 7935 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and 7936 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX 7937 // Effect chain for other sessions are inserted at beginning of effect 7938 // chains list to be processed before output mix effects. Relative order between other 7939 // sessions is not important 7940 size_t size = mEffectChains.size(); 7941 size_t i = 0; 7942 for (i = 0; i < size; i++) { 7943 if (mEffectChains[i]->sessionId() < session) break; 7944 } 7945 mEffectChains.insertAt(chain, i); 7946 checkSuspendOnAddEffectChain_l(chain); 7947 7948 return NO_ERROR; 7949} 7950 7951size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) 7952{ 7953 int session = chain->sessionId(); 7954 7955 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); 7956 7957 for (size_t i = 0; i < mEffectChains.size(); i++) { 7958 if (chain == mEffectChains[i]) { 7959 mEffectChains.removeAt(i); 7960 // detach all active tracks from the chain 7961 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { 7962 sp<Track> track = mActiveTracks[i].promote(); 7963 if (track == 0) continue; 7964 if (session == track->sessionId()) { 7965 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", 7966 chain.get(), session); 7967 chain->decActiveTrackCnt(); 7968 } 7969 } 7970 7971 // detach all tracks with same session ID from this chain 7972 for (size_t i = 0; i < mTracks.size(); ++i) { 7973 sp<Track> track = mTracks[i]; 7974 if (session == track->sessionId()) { 7975 track->setMainBuffer(mMixBuffer); 7976 chain->decTrackCnt(); 7977 } 7978 } 7979 break; 7980 } 7981 } 7982 return mEffectChains.size(); 7983} 7984 7985status_t AudioFlinger::PlaybackThread::attachAuxEffect( 7986 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 7987{ 7988 Mutex::Autolock _l(mLock); 7989 return attachAuxEffect_l(track, EffectId); 7990} 7991 7992status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( 7993 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) 7994{ 7995 status_t status = NO_ERROR; 7996 7997 if (EffectId == 0) { 7998 track->setAuxBuffer(0, NULL); 7999 } else { 8000 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX 8001 sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); 8002 if (effect != 0) { 8003 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 8004 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); 8005 } else { 8006 status = INVALID_OPERATION; 8007 } 8008 } else { 8009 status = BAD_VALUE; 8010 } 8011 } 8012 return status; 8013} 8014 8015void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) 8016{ 8017 for (size_t i = 0; i < mTracks.size(); ++i) { 8018 sp<Track> track = mTracks[i]; 8019 if (track->auxEffectId() == effectId) { 8020 attachAuxEffect_l(track, 0); 8021 } 8022 } 8023} 8024 8025status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) 8026{ 8027 // only one chain per input thread 8028 if (mEffectChains.size() != 0) { 8029 return INVALID_OPERATION; 8030 } 8031 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); 8032 8033 chain->setInBuffer(NULL); 8034 chain->setOutBuffer(NULL); 8035 8036 checkSuspendOnAddEffectChain_l(chain); 8037 8038 mEffectChains.add(chain); 8039 8040 return NO_ERROR; 8041} 8042 8043size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) 8044{ 8045 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); 8046 ALOGW_IF(mEffectChains.size() != 1, 8047 "removeEffectChain_l() %p invalid chain size %d on thread %p", 8048 chain.get(), mEffectChains.size(), this); 8049 if (mEffectChains.size() == 1) { 8050 mEffectChains.removeAt(0); 8051 } 8052 return 0; 8053} 8054 8055// ---------------------------------------------------------------------------- 8056// EffectModule implementation 8057// ---------------------------------------------------------------------------- 8058 8059#undef LOG_TAG 8060#define LOG_TAG "AudioFlinger::EffectModule" 8061 8062AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, 8063 const wp<AudioFlinger::EffectChain>& chain, 8064 effect_descriptor_t *desc, 8065 int id, 8066 int sessionId) 8067 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX), 8068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), 8069 mDescriptor(*desc), 8070 // mConfig is set by configure() and not used before then 8071 mEffectInterface(NULL), 8072 mStatus(NO_INIT), mState(IDLE), 8073 // mMaxDisableWaitCnt is set by configure() and not used before then 8074 // mDisableWaitCnt is set by process() and updateState() and not used before then 8075 mSuspended(false) 8076{ 8077 ALOGV("Constructor %p", this); 8078 int lStatus; 8079 8080 // create effect engine from effect factory 8081 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); 8082 8083 if (mStatus != NO_ERROR) { 8084 return; 8085 } 8086 lStatus = init(); 8087 if (lStatus < 0) { 8088 mStatus = lStatus; 8089 goto Error; 8090 } 8091 8092 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); 8093 return; 8094Error: 8095 EffectRelease(mEffectInterface); 8096 mEffectInterface = NULL; 8097 ALOGV("Constructor Error %d", mStatus); 8098} 8099 8100AudioFlinger::EffectModule::~EffectModule() 8101{ 8102 ALOGV("Destructor %p", this); 8103 if (mEffectInterface != NULL) { 8104 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || 8105 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { 8106 sp<ThreadBase> thread = mThread.promote(); 8107 if (thread != 0) { 8108 audio_stream_t *stream = thread->stream(); 8109 if (stream != NULL) { 8110 stream->remove_audio_effect(stream, mEffectInterface); 8111 } 8112 } 8113 } 8114 // release effect engine 8115 EffectRelease(mEffectInterface); 8116 } 8117} 8118 8119status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle) 8120{ 8121 status_t status; 8122 8123 Mutex::Autolock _l(mLock); 8124 int priority = handle->priority(); 8125 size_t size = mHandles.size(); 8126 EffectHandle *controlHandle = NULL; 8127 size_t i; 8128 for (i = 0; i < size; i++) { 8129 EffectHandle *h = mHandles[i]; 8130 if (h == NULL || h->destroyed_l()) continue; 8131 // first non destroyed handle is considered in control 8132 if (controlHandle == NULL) 8133 controlHandle = h; 8134 if (h->priority() <= priority) break; 8135 } 8136 // if inserted in first place, move effect control from previous owner to this handle 8137 if (i == 0) { 8138 bool enabled = false; 8139 if (controlHandle != NULL) { 8140 enabled = controlHandle->enabled(); 8141 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); 8142 } 8143 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); 8144 status = NO_ERROR; 8145 } else { 8146 status = ALREADY_EXISTS; 8147 } 8148 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i); 8149 mHandles.insertAt(handle, i); 8150 return status; 8151} 8152 8153size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle) 8154{ 8155 Mutex::Autolock _l(mLock); 8156 size_t size = mHandles.size(); 8157 size_t i; 8158 for (i = 0; i < size; i++) { 8159 if (mHandles[i] == handle) break; 8160 } 8161 if (i == size) { 8162 return size; 8163 } 8164 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i); 8165 8166 mHandles.removeAt(i); 8167 // if removed from first place, move effect control from this handle to next in line 8168 if (i == 0) { 8169 EffectHandle *h = controlHandle_l(); 8170 if (h != NULL) { 8171 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/); 8172 } 8173 } 8174 8175 // Prevent calls to process() and other functions on effect interface from now on. 8176 // The effect engine will be released by the destructor when the last strong reference on 8177 // this object is released which can happen after next process is called. 8178 if (mHandles.size() == 0 && !mPinned) { 8179 mState = DESTROYED; 8180 } 8181 8182 return mHandles.size(); 8183} 8184 8185// must be called with EffectModule::mLock held 8186AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l() 8187{ 8188 // the first valid handle in the list has control over the module 8189 for (size_t i = 0; i < mHandles.size(); i++) { 8190 EffectHandle *h = mHandles[i]; 8191 if (h != NULL && !h->destroyed_l()) { 8192 return h; 8193 } 8194 } 8195 8196 return NULL; 8197} 8198 8199size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast) 8200{ 8201 ALOGV("disconnect() %p handle %p", this, handle); 8202 // keep a strong reference on this EffectModule to avoid calling the 8203 // destructor before we exit 8204 sp<EffectModule> keep(this); 8205 { 8206 sp<ThreadBase> thread = mThread.promote(); 8207 if (thread != 0) { 8208 thread->disconnectEffect(keep, handle, unpinIfLast); 8209 } 8210 } 8211 return mHandles.size(); 8212} 8213 8214void AudioFlinger::EffectModule::updateState() { 8215 Mutex::Autolock _l(mLock); 8216 8217 switch (mState) { 8218 case RESTART: 8219 reset_l(); 8220 // FALL THROUGH 8221 8222 case STARTING: 8223 // clear auxiliary effect input buffer for next accumulation 8224 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 8225 memset(mConfig.inputCfg.buffer.raw, 8226 0, 8227 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); 8228 } 8229 start_l(); 8230 mState = ACTIVE; 8231 break; 8232 case STOPPING: 8233 stop_l(); 8234 mDisableWaitCnt = mMaxDisableWaitCnt; 8235 mState = STOPPED; 8236 break; 8237 case STOPPED: 8238 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the 8239 // turn off sequence. 8240 if (--mDisableWaitCnt == 0) { 8241 reset_l(); 8242 mState = IDLE; 8243 } 8244 break; 8245 default: //IDLE , ACTIVE, DESTROYED 8246 break; 8247 } 8248} 8249 8250void AudioFlinger::EffectModule::process() 8251{ 8252 Mutex::Autolock _l(mLock); 8253 8254 if (mState == DESTROYED || mEffectInterface == NULL || 8255 mConfig.inputCfg.buffer.raw == NULL || 8256 mConfig.outputCfg.buffer.raw == NULL) { 8257 return; 8258 } 8259 8260 if (isProcessEnabled()) { 8261 // do 32 bit to 16 bit conversion for auxiliary effect input buffer 8262 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 8263 ditherAndClamp(mConfig.inputCfg.buffer.s32, 8264 mConfig.inputCfg.buffer.s32, 8265 mConfig.inputCfg.buffer.frameCount/2); 8266 } 8267 8268 // do the actual processing in the effect engine 8269 int ret = (*mEffectInterface)->process(mEffectInterface, 8270 &mConfig.inputCfg.buffer, 8271 &mConfig.outputCfg.buffer); 8272 8273 // force transition to IDLE state when engine is ready 8274 if (mState == STOPPED && ret == -ENODATA) { 8275 mDisableWaitCnt = 1; 8276 } 8277 8278 // clear auxiliary effect input buffer for next accumulation 8279 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 8280 memset(mConfig.inputCfg.buffer.raw, 0, 8281 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); 8282 } 8283 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && 8284 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { 8285 // If an insert effect is idle and input buffer is different from output buffer, 8286 // accumulate input onto output 8287 sp<EffectChain> chain = mChain.promote(); 8288 if (chain != 0 && chain->activeTrackCnt() != 0) { 8289 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here 8290 int16_t *in = mConfig.inputCfg.buffer.s16; 8291 int16_t *out = mConfig.outputCfg.buffer.s16; 8292 for (size_t i = 0; i < frameCnt; i++) { 8293 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); 8294 } 8295 } 8296 } 8297} 8298 8299void AudioFlinger::EffectModule::reset_l() 8300{ 8301 if (mEffectInterface == NULL) { 8302 return; 8303 } 8304 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); 8305} 8306 8307status_t AudioFlinger::EffectModule::configure() 8308{ 8309 if (mEffectInterface == NULL) { 8310 return NO_INIT; 8311 } 8312 8313 sp<ThreadBase> thread = mThread.promote(); 8314 if (thread == 0) { 8315 return DEAD_OBJECT; 8316 } 8317 8318 // TODO: handle configuration of effects replacing track process 8319 audio_channel_mask_t channelMask = thread->channelMask(); 8320 8321 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 8322 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; 8323 } else { 8324 mConfig.inputCfg.channels = channelMask; 8325 } 8326 mConfig.outputCfg.channels = channelMask; 8327 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 8328 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 8329 mConfig.inputCfg.samplingRate = thread->sampleRate(); 8330 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; 8331 mConfig.inputCfg.bufferProvider.cookie = NULL; 8332 mConfig.inputCfg.bufferProvider.getBuffer = NULL; 8333 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; 8334 mConfig.outputCfg.bufferProvider.cookie = NULL; 8335 mConfig.outputCfg.bufferProvider.getBuffer = NULL; 8336 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; 8337 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; 8338 // Insert effect: 8339 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, 8340 // always overwrites output buffer: input buffer == output buffer 8341 // - in other sessions: 8342 // last effect in the chain accumulates in output buffer: input buffer != output buffer 8343 // other effect: overwrites output buffer: input buffer == output buffer 8344 // Auxiliary effect: 8345 // accumulates in output buffer: input buffer != output buffer 8346 // Therefore: accumulate <=> input buffer != output buffer 8347 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { 8348 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; 8349 } else { 8350 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; 8351 } 8352 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; 8353 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; 8354 mConfig.inputCfg.buffer.frameCount = thread->frameCount(); 8355 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; 8356 8357 ALOGV("configure() %p thread %p buffer %p framecount %d", 8358 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); 8359 8360 status_t cmdStatus; 8361 uint32_t size = sizeof(int); 8362 status_t status = (*mEffectInterface)->command(mEffectInterface, 8363 EFFECT_CMD_SET_CONFIG, 8364 sizeof(effect_config_t), 8365 &mConfig, 8366 &size, 8367 &cmdStatus); 8368 if (status == 0) { 8369 status = cmdStatus; 8370 } 8371 8372 if (status == 0 && 8373 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) { 8374 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; 8375 effect_param_t *p = (effect_param_t *)buf32; 8376 8377 p->psize = sizeof(uint32_t); 8378 p->vsize = sizeof(uint32_t); 8379 size = sizeof(int); 8380 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY; 8381 8382 uint32_t latency = 0; 8383 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId); 8384 if (pbt != NULL) { 8385 latency = pbt->latency_l(); 8386 } 8387 8388 *((int32_t *)p->data + 1)= latency; 8389 (*mEffectInterface)->command(mEffectInterface, 8390 EFFECT_CMD_SET_PARAM, 8391 sizeof(effect_param_t) + 8, 8392 &buf32, 8393 &size, 8394 &cmdStatus); 8395 } 8396 8397 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / 8398 (1000 * mConfig.outputCfg.buffer.frameCount); 8399 8400 return status; 8401} 8402 8403status_t AudioFlinger::EffectModule::init() 8404{ 8405 Mutex::Autolock _l(mLock); 8406 if (mEffectInterface == NULL) { 8407 return NO_INIT; 8408 } 8409 status_t cmdStatus; 8410 uint32_t size = sizeof(status_t); 8411 status_t status = (*mEffectInterface)->command(mEffectInterface, 8412 EFFECT_CMD_INIT, 8413 0, 8414 NULL, 8415 &size, 8416 &cmdStatus); 8417 if (status == 0) { 8418 status = cmdStatus; 8419 } 8420 return status; 8421} 8422 8423status_t AudioFlinger::EffectModule::start() 8424{ 8425 Mutex::Autolock _l(mLock); 8426 return start_l(); 8427} 8428 8429status_t AudioFlinger::EffectModule::start_l() 8430{ 8431 if (mEffectInterface == NULL) { 8432 return NO_INIT; 8433 } 8434 status_t cmdStatus; 8435 uint32_t size = sizeof(status_t); 8436 status_t status = (*mEffectInterface)->command(mEffectInterface, 8437 EFFECT_CMD_ENABLE, 8438 0, 8439 NULL, 8440 &size, 8441 &cmdStatus); 8442 if (status == 0) { 8443 status = cmdStatus; 8444 } 8445 if (status == 0 && 8446 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || 8447 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { 8448 sp<ThreadBase> thread = mThread.promote(); 8449 if (thread != 0) { 8450 audio_stream_t *stream = thread->stream(); 8451 if (stream != NULL) { 8452 stream->add_audio_effect(stream, mEffectInterface); 8453 } 8454 } 8455 } 8456 return status; 8457} 8458 8459status_t AudioFlinger::EffectModule::stop() 8460{ 8461 Mutex::Autolock _l(mLock); 8462 return stop_l(); 8463} 8464 8465status_t AudioFlinger::EffectModule::stop_l() 8466{ 8467 if (mEffectInterface == NULL) { 8468 return NO_INIT; 8469 } 8470 status_t cmdStatus; 8471 uint32_t size = sizeof(status_t); 8472 status_t status = (*mEffectInterface)->command(mEffectInterface, 8473 EFFECT_CMD_DISABLE, 8474 0, 8475 NULL, 8476 &size, 8477 &cmdStatus); 8478 if (status == 0) { 8479 status = cmdStatus; 8480 } 8481 if (status == 0 && 8482 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || 8483 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { 8484 sp<ThreadBase> thread = mThread.promote(); 8485 if (thread != 0) { 8486 audio_stream_t *stream = thread->stream(); 8487 if (stream != NULL) { 8488 stream->remove_audio_effect(stream, mEffectInterface); 8489 } 8490 } 8491 } 8492 return status; 8493} 8494 8495status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, 8496 uint32_t cmdSize, 8497 void *pCmdData, 8498 uint32_t *replySize, 8499 void *pReplyData) 8500{ 8501 Mutex::Autolock _l(mLock); 8502// ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); 8503 8504 if (mState == DESTROYED || mEffectInterface == NULL) { 8505 return NO_INIT; 8506 } 8507 status_t status = (*mEffectInterface)->command(mEffectInterface, 8508 cmdCode, 8509 cmdSize, 8510 pCmdData, 8511 replySize, 8512 pReplyData); 8513 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { 8514 uint32_t size = (replySize == NULL) ? 0 : *replySize; 8515 for (size_t i = 1; i < mHandles.size(); i++) { 8516 EffectHandle *h = mHandles[i]; 8517 if (h != NULL && !h->destroyed_l()) { 8518 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); 8519 } 8520 } 8521 } 8522 return status; 8523} 8524 8525status_t AudioFlinger::EffectModule::setEnabled(bool enabled) 8526{ 8527 Mutex::Autolock _l(mLock); 8528 return setEnabled_l(enabled); 8529} 8530 8531// must be called with EffectModule::mLock held 8532status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled) 8533{ 8534 8535 ALOGV("setEnabled %p enabled %d", this, enabled); 8536 8537 if (enabled != isEnabled()) { 8538 status_t status = AudioSystem::setEffectEnabled(mId, enabled); 8539 if (enabled && status != NO_ERROR) { 8540 return status; 8541 } 8542 8543 switch (mState) { 8544 // going from disabled to enabled 8545 case IDLE: 8546 mState = STARTING; 8547 break; 8548 case STOPPED: 8549 mState = RESTART; 8550 break; 8551 case STOPPING: 8552 mState = ACTIVE; 8553 break; 8554 8555 // going from enabled to disabled 8556 case RESTART: 8557 mState = STOPPED; 8558 break; 8559 case STARTING: 8560 mState = IDLE; 8561 break; 8562 case ACTIVE: 8563 mState = STOPPING; 8564 break; 8565 case DESTROYED: 8566 return NO_ERROR; // simply ignore as we are being destroyed 8567 } 8568 for (size_t i = 1; i < mHandles.size(); i++) { 8569 EffectHandle *h = mHandles[i]; 8570 if (h != NULL && !h->destroyed_l()) { 8571 h->setEnabled(enabled); 8572 } 8573 } 8574 } 8575 return NO_ERROR; 8576} 8577 8578bool AudioFlinger::EffectModule::isEnabled() const 8579{ 8580 switch (mState) { 8581 case RESTART: 8582 case STARTING: 8583 case ACTIVE: 8584 return true; 8585 case IDLE: 8586 case STOPPING: 8587 case STOPPED: 8588 case DESTROYED: 8589 default: 8590 return false; 8591 } 8592} 8593 8594bool AudioFlinger::EffectModule::isProcessEnabled() const 8595{ 8596 switch (mState) { 8597 case RESTART: 8598 case ACTIVE: 8599 case STOPPING: 8600 case STOPPED: 8601 return true; 8602 case IDLE: 8603 case STARTING: 8604 case DESTROYED: 8605 default: 8606 return false; 8607 } 8608} 8609 8610status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) 8611{ 8612 Mutex::Autolock _l(mLock); 8613 status_t status = NO_ERROR; 8614 8615 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume 8616 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) 8617 if (isProcessEnabled() && 8618 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || 8619 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { 8620 status_t cmdStatus; 8621 uint32_t volume[2]; 8622 uint32_t *pVolume = NULL; 8623 uint32_t size = sizeof(volume); 8624 volume[0] = *left; 8625 volume[1] = *right; 8626 if (controller) { 8627 pVolume = volume; 8628 } 8629 status = (*mEffectInterface)->command(mEffectInterface, 8630 EFFECT_CMD_SET_VOLUME, 8631 size, 8632 volume, 8633 &size, 8634 pVolume); 8635 if (controller && status == NO_ERROR && size == sizeof(volume)) { 8636 *left = volume[0]; 8637 *right = volume[1]; 8638 } 8639 } 8640 return status; 8641} 8642 8643status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device) 8644{ 8645 Mutex::Autolock _l(mLock); 8646 status_t status = NO_ERROR; 8647 if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { 8648 // audio pre processing modules on RecordThread can receive both output and 8649 // input device indication in the same call 8650 audio_devices_t dev = device & AUDIO_DEVICE_OUT_ALL; 8651 if (dev) { 8652 status_t cmdStatus; 8653 uint32_t size = sizeof(status_t); 8654 8655 status = (*mEffectInterface)->command(mEffectInterface, 8656 EFFECT_CMD_SET_DEVICE, 8657 sizeof(uint32_t), 8658 &dev, 8659 &size, 8660 &cmdStatus); 8661 if (status == NO_ERROR) { 8662 status = cmdStatus; 8663 } 8664 } 8665 dev = device & AUDIO_DEVICE_IN_ALL; 8666 if (dev) { 8667 status_t cmdStatus; 8668 uint32_t size = sizeof(status_t); 8669 8670 status_t status2 = (*mEffectInterface)->command(mEffectInterface, 8671 EFFECT_CMD_SET_INPUT_DEVICE, 8672 sizeof(uint32_t), 8673 &dev, 8674 &size, 8675 &cmdStatus); 8676 if (status2 == NO_ERROR) { 8677 status2 = cmdStatus; 8678 } 8679 if (status == NO_ERROR) { 8680 status = status2; 8681 } 8682 } 8683 } 8684 return status; 8685} 8686 8687status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) 8688{ 8689 Mutex::Autolock _l(mLock); 8690 status_t status = NO_ERROR; 8691 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { 8692 status_t cmdStatus; 8693 uint32_t size = sizeof(status_t); 8694 status = (*mEffectInterface)->command(mEffectInterface, 8695 EFFECT_CMD_SET_AUDIO_MODE, 8696 sizeof(audio_mode_t), 8697 &mode, 8698 &size, 8699 &cmdStatus); 8700 if (status == NO_ERROR) { 8701 status = cmdStatus; 8702 } 8703 } 8704 return status; 8705} 8706 8707void AudioFlinger::EffectModule::setSuspended(bool suspended) 8708{ 8709 Mutex::Autolock _l(mLock); 8710 mSuspended = suspended; 8711} 8712 8713bool AudioFlinger::EffectModule::suspended() const 8714{ 8715 Mutex::Autolock _l(mLock); 8716 return mSuspended; 8717} 8718 8719bool AudioFlinger::EffectModule::purgeHandles() 8720{ 8721 bool enabled = false; 8722 Mutex::Autolock _l(mLock); 8723 for (size_t i = 0; i < mHandles.size(); i++) { 8724 EffectHandle *handle = mHandles[i]; 8725 if (handle != NULL && !handle->destroyed_l()) { 8726 handle->effect().clear(); 8727 if (handle->hasControl()) { 8728 enabled = handle->enabled(); 8729 } 8730 } 8731 } 8732 return enabled; 8733} 8734 8735void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) 8736{ 8737 const size_t SIZE = 256; 8738 char buffer[SIZE]; 8739 String8 result; 8740 8741 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); 8742 result.append(buffer); 8743 8744 bool locked = tryLock(mLock); 8745 // failed to lock - AudioFlinger is probably deadlocked 8746 if (!locked) { 8747 result.append("\t\tCould not lock Fx mutex:\n"); 8748 } 8749 8750 result.append("\t\tSession Status State Engine:\n"); 8751 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", 8752 mSessionId, mStatus, mState, (uint32_t)mEffectInterface); 8753 result.append(buffer); 8754 8755 result.append("\t\tDescriptor:\n"); 8756 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", 8757 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, 8758 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], 8759 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); 8760 result.append(buffer); 8761 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", 8762 mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, 8763 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], 8764 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); 8765 result.append(buffer); 8766 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", 8767 mDescriptor.apiVersion, 8768 mDescriptor.flags); 8769 result.append(buffer); 8770 snprintf(buffer, SIZE, "\t\t- name: %s\n", 8771 mDescriptor.name); 8772 result.append(buffer); 8773 snprintf(buffer, SIZE, "\t\t- implementor: %s\n", 8774 mDescriptor.implementor); 8775 result.append(buffer); 8776 8777 result.append("\t\t- Input configuration:\n"); 8778 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); 8779 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", 8780 (uint32_t)mConfig.inputCfg.buffer.raw, 8781 mConfig.inputCfg.buffer.frameCount, 8782 mConfig.inputCfg.samplingRate, 8783 mConfig.inputCfg.channels, 8784 mConfig.inputCfg.format); 8785 result.append(buffer); 8786 8787 result.append("\t\t- Output configuration:\n"); 8788 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); 8789 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", 8790 (uint32_t)mConfig.outputCfg.buffer.raw, 8791 mConfig.outputCfg.buffer.frameCount, 8792 mConfig.outputCfg.samplingRate, 8793 mConfig.outputCfg.channels, 8794 mConfig.outputCfg.format); 8795 result.append(buffer); 8796 8797 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); 8798 result.append(buffer); 8799 result.append("\t\t\tPid Priority Ctrl Locked client server\n"); 8800 for (size_t i = 0; i < mHandles.size(); ++i) { 8801 EffectHandle *handle = mHandles[i]; 8802 if (handle != NULL && !handle->destroyed_l()) { 8803 handle->dump(buffer, SIZE); 8804 result.append(buffer); 8805 } 8806 } 8807 8808 result.append("\n"); 8809 8810 write(fd, result.string(), result.length()); 8811 8812 if (locked) { 8813 mLock.unlock(); 8814 } 8815} 8816 8817// ---------------------------------------------------------------------------- 8818// EffectHandle implementation 8819// ---------------------------------------------------------------------------- 8820 8821#undef LOG_TAG 8822#define LOG_TAG "AudioFlinger::EffectHandle" 8823 8824AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, 8825 const sp<AudioFlinger::Client>& client, 8826 const sp<IEffectClient>& effectClient, 8827 int32_t priority) 8828 : BnEffect(), 8829 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), 8830 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false) 8831{ 8832 ALOGV("constructor %p", this); 8833 8834 if (client == 0) { 8835 return; 8836 } 8837 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); 8838 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); 8839 if (mCblkMemory != 0) { 8840 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); 8841 8842 if (mCblk != NULL) { 8843 new(mCblk) effect_param_cblk_t(); 8844 mBuffer = (uint8_t *)mCblk + bufOffset; 8845 } 8846 } else { 8847 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); 8848 return; 8849 } 8850} 8851 8852AudioFlinger::EffectHandle::~EffectHandle() 8853{ 8854 ALOGV("Destructor %p", this); 8855 8856 if (mEffect == 0) { 8857 mDestroyed = true; 8858 return; 8859 } 8860 mEffect->lock(); 8861 mDestroyed = true; 8862 mEffect->unlock(); 8863 disconnect(false); 8864} 8865 8866status_t AudioFlinger::EffectHandle::enable() 8867{ 8868 ALOGV("enable %p", this); 8869 if (!mHasControl) return INVALID_OPERATION; 8870 if (mEffect == 0) return DEAD_OBJECT; 8871 8872 if (mEnabled) { 8873 return NO_ERROR; 8874 } 8875 8876 mEnabled = true; 8877 8878 sp<ThreadBase> thread = mEffect->thread().promote(); 8879 if (thread != 0) { 8880 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); 8881 } 8882 8883 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled 8884 if (mEffect->suspended()) { 8885 return NO_ERROR; 8886 } 8887 8888 status_t status = mEffect->setEnabled(true); 8889 if (status != NO_ERROR) { 8890 if (thread != 0) { 8891 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); 8892 } 8893 mEnabled = false; 8894 } 8895 return status; 8896} 8897 8898status_t AudioFlinger::EffectHandle::disable() 8899{ 8900 ALOGV("disable %p", this); 8901 if (!mHasControl) return INVALID_OPERATION; 8902 if (mEffect == 0) return DEAD_OBJECT; 8903 8904 if (!mEnabled) { 8905 return NO_ERROR; 8906 } 8907 mEnabled = false; 8908 8909 if (mEffect->suspended()) { 8910 return NO_ERROR; 8911 } 8912 8913 status_t status = mEffect->setEnabled(false); 8914 8915 sp<ThreadBase> thread = mEffect->thread().promote(); 8916 if (thread != 0) { 8917 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); 8918 } 8919 8920 return status; 8921} 8922 8923void AudioFlinger::EffectHandle::disconnect() 8924{ 8925 disconnect(true); 8926} 8927 8928void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) 8929{ 8930 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); 8931 if (mEffect == 0) { 8932 return; 8933 } 8934 // restore suspended effects if the disconnected handle was enabled and the last one. 8935 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) { 8936 sp<ThreadBase> thread = mEffect->thread().promote(); 8937 if (thread != 0) { 8938 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); 8939 } 8940 } 8941 8942 // release sp on module => module destructor can be called now 8943 mEffect.clear(); 8944 if (mClient != 0) { 8945 if (mCblk != NULL) { 8946 // unlike ~TrackBase(), mCblk is never a local new, so don't delete 8947 mCblk->~effect_param_cblk_t(); // destroy our shared-structure. 8948 } 8949 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to 8950 // Client destructor must run with AudioFlinger mutex locked 8951 Mutex::Autolock _l(mClient->audioFlinger()->mLock); 8952 mClient.clear(); 8953 } 8954} 8955 8956status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, 8957 uint32_t cmdSize, 8958 void *pCmdData, 8959 uint32_t *replySize, 8960 void *pReplyData) 8961{ 8962// ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", 8963// cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); 8964 8965 // only get parameter command is permitted for applications not controlling the effect 8966 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { 8967 return INVALID_OPERATION; 8968 } 8969 if (mEffect == 0) return DEAD_OBJECT; 8970 if (mClient == 0) return INVALID_OPERATION; 8971 8972 // handle commands that are not forwarded transparently to effect engine 8973 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { 8974 // No need to trylock() here as this function is executed in the binder thread serving a particular client process: 8975 // no risk to block the whole media server process or mixer threads is we are stuck here 8976 Mutex::Autolock _l(mCblk->lock); 8977 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || 8978 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { 8979 mCblk->serverIndex = 0; 8980 mCblk->clientIndex = 0; 8981 return BAD_VALUE; 8982 } 8983 status_t status = NO_ERROR; 8984 while (mCblk->serverIndex < mCblk->clientIndex) { 8985 int reply; 8986 uint32_t rsize = sizeof(int); 8987 int *p = (int *)(mBuffer + mCblk->serverIndex); 8988 int size = *p++; 8989 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { 8990 ALOGW("command(): invalid parameter block size"); 8991 break; 8992 } 8993 effect_param_t *param = (effect_param_t *)p; 8994 if (param->psize == 0 || param->vsize == 0) { 8995 ALOGW("command(): null parameter or value size"); 8996 mCblk->serverIndex += size; 8997 continue; 8998 } 8999 uint32_t psize = sizeof(effect_param_t) + 9000 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + 9001 param->vsize; 9002 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, 9003 psize, 9004 p, 9005 &rsize, 9006 &reply); 9007 // stop at first error encountered 9008 if (ret != NO_ERROR) { 9009 status = ret; 9010 *(int *)pReplyData = reply; 9011 break; 9012 } else if (reply != NO_ERROR) { 9013 *(int *)pReplyData = reply; 9014 break; 9015 } 9016 mCblk->serverIndex += size; 9017 } 9018 mCblk->serverIndex = 0; 9019 mCblk->clientIndex = 0; 9020 return status; 9021 } else if (cmdCode == EFFECT_CMD_ENABLE) { 9022 *(int *)pReplyData = NO_ERROR; 9023 return enable(); 9024 } else if (cmdCode == EFFECT_CMD_DISABLE) { 9025 *(int *)pReplyData = NO_ERROR; 9026 return disable(); 9027 } 9028 9029 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); 9030} 9031 9032void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) 9033{ 9034 ALOGV("setControl %p control %d", this, hasControl); 9035 9036 mHasControl = hasControl; 9037 mEnabled = enabled; 9038 9039 if (signal && mEffectClient != 0) { 9040 mEffectClient->controlStatusChanged(hasControl); 9041 } 9042} 9043 9044void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, 9045 uint32_t cmdSize, 9046 void *pCmdData, 9047 uint32_t replySize, 9048 void *pReplyData) 9049{ 9050 if (mEffectClient != 0) { 9051 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); 9052 } 9053} 9054 9055 9056 9057void AudioFlinger::EffectHandle::setEnabled(bool enabled) 9058{ 9059 if (mEffectClient != 0) { 9060 mEffectClient->enableStatusChanged(enabled); 9061 } 9062} 9063 9064status_t AudioFlinger::EffectHandle::onTransact( 9065 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 9066{ 9067 return BnEffect::onTransact(code, data, reply, flags); 9068} 9069 9070 9071void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) 9072{ 9073 bool locked = mCblk != NULL && tryLock(mCblk->lock); 9074 9075 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", 9076 (mClient == 0) ? getpid_cached : mClient->pid(), 9077 mPriority, 9078 mHasControl, 9079 !locked, 9080 mCblk ? mCblk->clientIndex : 0, 9081 mCblk ? mCblk->serverIndex : 0 9082 ); 9083 9084 if (locked) { 9085 mCblk->lock.unlock(); 9086 } 9087} 9088 9089#undef LOG_TAG 9090#define LOG_TAG "AudioFlinger::EffectChain" 9091 9092AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, 9093 int sessionId) 9094 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), 9095 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), 9096 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) 9097{ 9098 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); 9099 if (thread == NULL) { 9100 return; 9101 } 9102 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / 9103 thread->frameCount(); 9104} 9105 9106AudioFlinger::EffectChain::~EffectChain() 9107{ 9108 if (mOwnInBuffer) { 9109 delete mInBuffer; 9110 } 9111 9112} 9113 9114// getEffectFromDesc_l() must be called with ThreadBase::mLock held 9115sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) 9116{ 9117 size_t size = mEffects.size(); 9118 9119 for (size_t i = 0; i < size; i++) { 9120 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { 9121 return mEffects[i]; 9122 } 9123 } 9124 return 0; 9125} 9126 9127// getEffectFromId_l() must be called with ThreadBase::mLock held 9128sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) 9129{ 9130 size_t size = mEffects.size(); 9131 9132 for (size_t i = 0; i < size; i++) { 9133 // by convention, return first effect if id provided is 0 (0 is never a valid id) 9134 if (id == 0 || mEffects[i]->id() == id) { 9135 return mEffects[i]; 9136 } 9137 } 9138 return 0; 9139} 9140 9141// getEffectFromType_l() must be called with ThreadBase::mLock held 9142sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( 9143 const effect_uuid_t *type) 9144{ 9145 size_t size = mEffects.size(); 9146 9147 for (size_t i = 0; i < size; i++) { 9148 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { 9149 return mEffects[i]; 9150 } 9151 } 9152 return 0; 9153} 9154 9155void AudioFlinger::EffectChain::clearInputBuffer() 9156{ 9157 Mutex::Autolock _l(mLock); 9158 sp<ThreadBase> thread = mThread.promote(); 9159 if (thread == 0) { 9160 ALOGW("clearInputBuffer(): cannot promote mixer thread"); 9161 return; 9162 } 9163 clearInputBuffer_l(thread); 9164} 9165 9166// Must be called with EffectChain::mLock locked 9167void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread) 9168{ 9169 size_t numSamples = thread->frameCount() * thread->channelCount(); 9170 memset(mInBuffer, 0, numSamples * sizeof(int16_t)); 9171 9172} 9173 9174// Must be called with EffectChain::mLock locked 9175void AudioFlinger::EffectChain::process_l() 9176{ 9177 sp<ThreadBase> thread = mThread.promote(); 9178 if (thread == 0) { 9179 ALOGW("process_l(): cannot promote mixer thread"); 9180 return; 9181 } 9182 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || 9183 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); 9184 // always process effects unless no more tracks are on the session and the effect tail 9185 // has been rendered 9186 bool doProcess = true; 9187 if (!isGlobalSession) { 9188 bool tracksOnSession = (trackCnt() != 0); 9189 9190 if (!tracksOnSession && mTailBufferCount == 0) { 9191 doProcess = false; 9192 } 9193 9194 if (activeTrackCnt() == 0) { 9195 // if no track is active and the effect tail has not been rendered, 9196 // the input buffer must be cleared here as the mixer process will not do it 9197 if (tracksOnSession || mTailBufferCount > 0) { 9198 clearInputBuffer_l(thread); 9199 if (mTailBufferCount > 0) { 9200 mTailBufferCount--; 9201 } 9202 } 9203 } 9204 } 9205 9206 size_t size = mEffects.size(); 9207 if (doProcess) { 9208 for (size_t i = 0; i < size; i++) { 9209 mEffects[i]->process(); 9210 } 9211 } 9212 for (size_t i = 0; i < size; i++) { 9213 mEffects[i]->updateState(); 9214 } 9215} 9216 9217// addEffect_l() must be called with PlaybackThread::mLock held 9218status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) 9219{ 9220 effect_descriptor_t desc = effect->desc(); 9221 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; 9222 9223 Mutex::Autolock _l(mLock); 9224 effect->setChain(this); 9225 sp<ThreadBase> thread = mThread.promote(); 9226 if (thread == 0) { 9227 return NO_INIT; 9228 } 9229 effect->setThread(thread); 9230 9231 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 9232 // Auxiliary effects are inserted at the beginning of mEffects vector as 9233 // they are processed first and accumulated in chain input buffer 9234 mEffects.insertAt(effect, 0); 9235 9236 // the input buffer for auxiliary effect contains mono samples in 9237 // 32 bit format. This is to avoid saturation in AudoMixer 9238 // accumulation stage. Saturation is done in EffectModule::process() before 9239 // calling the process in effect engine 9240 size_t numSamples = thread->frameCount(); 9241 int32_t *buffer = new int32_t[numSamples]; 9242 memset(buffer, 0, numSamples * sizeof(int32_t)); 9243 effect->setInBuffer((int16_t *)buffer); 9244 // auxiliary effects output samples to chain input buffer for further processing 9245 // by insert effects 9246 effect->setOutBuffer(mInBuffer); 9247 } else { 9248 // Insert effects are inserted at the end of mEffects vector as they are processed 9249 // after track and auxiliary effects. 9250 // Insert effect order as a function of indicated preference: 9251 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if 9252 // another effect is present 9253 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the 9254 // last effect claiming first position 9255 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the 9256 // first effect claiming last position 9257 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last 9258 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is 9259 // already present 9260 9261 size_t size = mEffects.size(); 9262 size_t idx_insert = size; 9263 ssize_t idx_insert_first = -1; 9264 ssize_t idx_insert_last = -1; 9265 9266 for (size_t i = 0; i < size; i++) { 9267 effect_descriptor_t d = mEffects[i]->desc(); 9268 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; 9269 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; 9270 if (iMode == EFFECT_FLAG_TYPE_INSERT) { 9271 // check invalid effect chaining combinations 9272 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || 9273 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { 9274 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); 9275 return INVALID_OPERATION; 9276 } 9277 // remember position of first insert effect and by default 9278 // select this as insert position for new effect 9279 if (idx_insert == size) { 9280 idx_insert = i; 9281 } 9282 // remember position of last insert effect claiming 9283 // first position 9284 if (iPref == EFFECT_FLAG_INSERT_FIRST) { 9285 idx_insert_first = i; 9286 } 9287 // remember position of first insert effect claiming 9288 // last position 9289 if (iPref == EFFECT_FLAG_INSERT_LAST && 9290 idx_insert_last == -1) { 9291 idx_insert_last = i; 9292 } 9293 } 9294 } 9295 9296 // modify idx_insert from first position if needed 9297 if (insertPref == EFFECT_FLAG_INSERT_LAST) { 9298 if (idx_insert_last != -1) { 9299 idx_insert = idx_insert_last; 9300 } else { 9301 idx_insert = size; 9302 } 9303 } else { 9304 if (idx_insert_first != -1) { 9305 idx_insert = idx_insert_first + 1; 9306 } 9307 } 9308 9309 // always read samples from chain input buffer 9310 effect->setInBuffer(mInBuffer); 9311 9312 // if last effect in the chain, output samples to chain 9313 // output buffer, otherwise to chain input buffer 9314 if (idx_insert == size) { 9315 if (idx_insert != 0) { 9316 mEffects[idx_insert-1]->setOutBuffer(mInBuffer); 9317 mEffects[idx_insert-1]->configure(); 9318 } 9319 effect->setOutBuffer(mOutBuffer); 9320 } else { 9321 effect->setOutBuffer(mInBuffer); 9322 } 9323 mEffects.insertAt(effect, idx_insert); 9324 9325 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); 9326 } 9327 effect->configure(); 9328 return NO_ERROR; 9329} 9330 9331// removeEffect_l() must be called with PlaybackThread::mLock held 9332size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) 9333{ 9334 Mutex::Autolock _l(mLock); 9335 size_t size = mEffects.size(); 9336 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; 9337 9338 for (size_t i = 0; i < size; i++) { 9339 if (effect == mEffects[i]) { 9340 // calling stop here will remove pre-processing effect from the audio HAL. 9341 // This is safe as we hold the EffectChain mutex which guarantees that we are not in 9342 // the middle of a read from audio HAL 9343 if (mEffects[i]->state() == EffectModule::ACTIVE || 9344 mEffects[i]->state() == EffectModule::STOPPING) { 9345 mEffects[i]->stop(); 9346 } 9347 if (type == EFFECT_FLAG_TYPE_AUXILIARY) { 9348 delete[] effect->inBuffer(); 9349 } else { 9350 if (i == size - 1 && i != 0) { 9351 mEffects[i - 1]->setOutBuffer(mOutBuffer); 9352 mEffects[i - 1]->configure(); 9353 } 9354 } 9355 mEffects.removeAt(i); 9356 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); 9357 break; 9358 } 9359 } 9360 9361 return mEffects.size(); 9362} 9363 9364// setDevice_l() must be called with PlaybackThread::mLock held 9365void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device) 9366{ 9367 size_t size = mEffects.size(); 9368 for (size_t i = 0; i < size; i++) { 9369 mEffects[i]->setDevice(device); 9370 } 9371} 9372 9373// setMode_l() must be called with PlaybackThread::mLock held 9374void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) 9375{ 9376 size_t size = mEffects.size(); 9377 for (size_t i = 0; i < size; i++) { 9378 mEffects[i]->setMode(mode); 9379 } 9380} 9381 9382// setVolume_l() must be called with PlaybackThread::mLock held 9383bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) 9384{ 9385 uint32_t newLeft = *left; 9386 uint32_t newRight = *right; 9387 bool hasControl = false; 9388 int ctrlIdx = -1; 9389 size_t size = mEffects.size(); 9390 9391 // first update volume controller 9392 for (size_t i = size; i > 0; i--) { 9393 if (mEffects[i - 1]->isProcessEnabled() && 9394 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { 9395 ctrlIdx = i - 1; 9396 hasControl = true; 9397 break; 9398 } 9399 } 9400 9401 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { 9402 if (hasControl) { 9403 *left = mNewLeftVolume; 9404 *right = mNewRightVolume; 9405 } 9406 return hasControl; 9407 } 9408 9409 mVolumeCtrlIdx = ctrlIdx; 9410 mLeftVolume = newLeft; 9411 mRightVolume = newRight; 9412 9413 // second get volume update from volume controller 9414 if (ctrlIdx >= 0) { 9415 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); 9416 mNewLeftVolume = newLeft; 9417 mNewRightVolume = newRight; 9418 } 9419 // then indicate volume to all other effects in chain. 9420 // Pass altered volume to effects before volume controller 9421 // and requested volume to effects after controller 9422 uint32_t lVol = newLeft; 9423 uint32_t rVol = newRight; 9424 9425 for (size_t i = 0; i < size; i++) { 9426 if ((int)i == ctrlIdx) continue; 9427 // this also works for ctrlIdx == -1 when there is no volume controller 9428 if ((int)i > ctrlIdx) { 9429 lVol = *left; 9430 rVol = *right; 9431 } 9432 mEffects[i]->setVolume(&lVol, &rVol, false); 9433 } 9434 *left = newLeft; 9435 *right = newRight; 9436 9437 return hasControl; 9438} 9439 9440void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) 9441{ 9442 const size_t SIZE = 256; 9443 char buffer[SIZE]; 9444 String8 result; 9445 9446 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); 9447 result.append(buffer); 9448 9449 bool locked = tryLock(mLock); 9450 // failed to lock - AudioFlinger is probably deadlocked 9451 if (!locked) { 9452 result.append("\tCould not lock mutex:\n"); 9453 } 9454 9455 result.append("\tNum fx In buffer Out buffer Active tracks:\n"); 9456 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", 9457 mEffects.size(), 9458 (uint32_t)mInBuffer, 9459 (uint32_t)mOutBuffer, 9460 mActiveTrackCnt); 9461 result.append(buffer); 9462 write(fd, result.string(), result.size()); 9463 9464 for (size_t i = 0; i < mEffects.size(); ++i) { 9465 sp<EffectModule> effect = mEffects[i]; 9466 if (effect != 0) { 9467 effect->dump(fd, args); 9468 } 9469 } 9470 9471 if (locked) { 9472 mLock.unlock(); 9473 } 9474} 9475 9476// must be called with ThreadBase::mLock held 9477void AudioFlinger::EffectChain::setEffectSuspended_l( 9478 const effect_uuid_t *type, bool suspend) 9479{ 9480 sp<SuspendedEffectDesc> desc; 9481 // use effect type UUID timelow as key as there is no real risk of identical 9482 // timeLow fields among effect type UUIDs. 9483 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); 9484 if (suspend) { 9485 if (index >= 0) { 9486 desc = mSuspendedEffects.valueAt(index); 9487 } else { 9488 desc = new SuspendedEffectDesc(); 9489 desc->mType = *type; 9490 mSuspendedEffects.add(type->timeLow, desc); 9491 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); 9492 } 9493 if (desc->mRefCount++ == 0) { 9494 sp<EffectModule> effect = getEffectIfEnabled(type); 9495 if (effect != 0) { 9496 desc->mEffect = effect; 9497 effect->setSuspended(true); 9498 effect->setEnabled(false); 9499 } 9500 } 9501 } else { 9502 if (index < 0) { 9503 return; 9504 } 9505 desc = mSuspendedEffects.valueAt(index); 9506 if (desc->mRefCount <= 0) { 9507 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); 9508 desc->mRefCount = 1; 9509 } 9510 if (--desc->mRefCount == 0) { 9511 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); 9512 if (desc->mEffect != 0) { 9513 sp<EffectModule> effect = desc->mEffect.promote(); 9514 if (effect != 0) { 9515 effect->setSuspended(false); 9516 effect->lock(); 9517 EffectHandle *handle = effect->controlHandle_l(); 9518 if (handle != NULL && !handle->destroyed_l()) { 9519 effect->setEnabled_l(handle->enabled()); 9520 } 9521 effect->unlock(); 9522 } 9523 desc->mEffect.clear(); 9524 } 9525 mSuspendedEffects.removeItemsAt(index); 9526 } 9527 } 9528} 9529 9530// must be called with ThreadBase::mLock held 9531void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) 9532{ 9533 sp<SuspendedEffectDesc> desc; 9534 9535 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); 9536 if (suspend) { 9537 if (index >= 0) { 9538 desc = mSuspendedEffects.valueAt(index); 9539 } else { 9540 desc = new SuspendedEffectDesc(); 9541 mSuspendedEffects.add((int)kKeyForSuspendAll, desc); 9542 ALOGV("setEffectSuspendedAll_l() add entry for 0"); 9543 } 9544 if (desc->mRefCount++ == 0) { 9545 Vector< sp<EffectModule> > effects; 9546 getSuspendEligibleEffects(effects); 9547 for (size_t i = 0; i < effects.size(); i++) { 9548 setEffectSuspended_l(&effects[i]->desc().type, true); 9549 } 9550 } 9551 } else { 9552 if (index < 0) { 9553 return; 9554 } 9555 desc = mSuspendedEffects.valueAt(index); 9556 if (desc->mRefCount <= 0) { 9557 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); 9558 desc->mRefCount = 1; 9559 } 9560 if (--desc->mRefCount == 0) { 9561 Vector<const effect_uuid_t *> types; 9562 for (size_t i = 0; i < mSuspendedEffects.size(); i++) { 9563 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { 9564 continue; 9565 } 9566 types.add(&mSuspendedEffects.valueAt(i)->mType); 9567 } 9568 for (size_t i = 0; i < types.size(); i++) { 9569 setEffectSuspended_l(types[i], false); 9570 } 9571 ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); 9572 mSuspendedEffects.removeItem((int)kKeyForSuspendAll); 9573 } 9574 } 9575} 9576 9577 9578// The volume effect is used for automated tests only 9579#ifndef OPENSL_ES_H_ 9580static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, 9581 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; 9582const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; 9583#endif //OPENSL_ES_H_ 9584 9585bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) 9586{ 9587 // auxiliary effects and visualizer are never suspended on output mix 9588 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && 9589 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || 9590 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || 9591 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { 9592 return false; 9593 } 9594 return true; 9595} 9596 9597void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects) 9598{ 9599 effects.clear(); 9600 for (size_t i = 0; i < mEffects.size(); i++) { 9601 if (isEffectEligibleForSuspend(mEffects[i]->desc())) { 9602 effects.add(mEffects[i]); 9603 } 9604 } 9605} 9606 9607sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( 9608 const effect_uuid_t *type) 9609{ 9610 sp<EffectModule> effect = getEffectFromType_l(type); 9611 return effect != 0 && effect->isEnabled() ? effect : 0; 9612} 9613 9614void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 9615 bool enabled) 9616{ 9617 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); 9618 if (enabled) { 9619 if (index < 0) { 9620 // if the effect is not suspend check if all effects are suspended 9621 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); 9622 if (index < 0) { 9623 return; 9624 } 9625 if (!isEffectEligibleForSuspend(effect->desc())) { 9626 return; 9627 } 9628 setEffectSuspended_l(&effect->desc().type, enabled); 9629 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); 9630 if (index < 0) { 9631 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); 9632 return; 9633 } 9634 } 9635 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", 9636 effect->desc().type.timeLow); 9637 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); 9638 // if effect is requested to suspended but was not yet enabled, supend it now. 9639 if (desc->mEffect == 0) { 9640 desc->mEffect = effect; 9641 effect->setEnabled(false); 9642 effect->setSuspended(true); 9643 } 9644 } else { 9645 if (index < 0) { 9646 return; 9647 } 9648 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", 9649 effect->desc().type.timeLow); 9650 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); 9651 desc->mEffect.clear(); 9652 effect->setSuspended(false); 9653 } 9654} 9655 9656#undef LOG_TAG 9657#define LOG_TAG "AudioFlinger" 9658 9659// ---------------------------------------------------------------------------- 9660 9661status_t AudioFlinger::onTransact( 9662 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 9663{ 9664 return BnAudioFlinger::onTransact(code, data, reply, flags); 9665} 9666 9667}; // namespace android 9668