![](https://img-blog.csdnimg.cn/direct/9f3efd2c3bcb41139fd8f16151698a6f.png)
xref: /frameworks/av/media/libaudioprocessing/AudioMixer.cpp
979 void AudioMixer::process__validate()
980 {
981 // TODO: fix all16BitsStereNoResample logic to
982 // either properly handle muted tracks (it should ignore them)
983 // or remove altogether as an obsolete optimization.
984 bool all16BitsStereoNoResample = true;
985 bool resampling = false;
986 bool volumeRamp = false;
987
988 mEnabled.clear();
989 mGroups.clear();
990 for (const auto &pair : mTracks) {
991 const int name = pair.first;
992 const std::shared_ptr<Track> &t = pair.second;
993 if (!t->enabled) continue;
994
995 mEnabled.emplace_back(name); // we add to mEnabled in order of name.
996 mGroups[t->mainBuffer].emplace_back(name); // mGroups also in order of name.
997
998 uint32_t n = 0;
999 // FIXME can overflow (mask is only 3 bits)
1000 n |= NEEDS_CHANNEL_1 + t->channelCount - 1;
1001 if (t->doesResample()) {
1002 n |= NEEDS_RESAMPLE;
1003 }
1004 if (t->auxLevel != 0 && t->auxBuffer != NULL) {
1005 n |= NEEDS_AUX;
1006 }
1007
1008 if (t->volumeInc[0]|t->volumeInc[1]) {
1009 volumeRamp = true;
1010 } else if (!t->doesResample() && t->volumeRL == 0) {
1011 n |= NEEDS_MUTE;
1012 }
1013 t->needs = n;
1014
1015 if (n & NEEDS_MUTE) {
1016 t->hook = &Track::track__nop;
1017 } else {
1018 if (n & NEEDS_AUX) {
1019 all16BitsStereoNoResample = false;
1020 }
1021 if (n & NEEDS_RESAMPLE) {
1022 all16BitsStereoNoResample = false;
1023 resampling = true;
1024 t->hook = Track::getTrackHook(TRACKTYPE_RESAMPLE, t->mMixerChannelCount,
1025 t->mMixerInFormat, t->mMixerFormat);
1026 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
1027 "Track %d needs downmix + resample", name);
1028 } else {
1029 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
1030 t->hook = Track::getTrackHook(
1031 (t->mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO // TODO: MONO_HACK
1032 && t->channelMask == AUDIO_CHANNEL_OUT_MONO)
1033 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
1034 t->mMixerChannelCount,
1035 t->mMixerInFormat, t->mMixerFormat);
1036 all16BitsStereoNoResample = false;
1037 }
1038 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
1039 t->hook = Track::getTrackHook(TRACKTYPE_NORESAMPLE, t->mMixerChannelCount,
1040 t->mMixerInFormat, t->mMixerFormat);
1041 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
1042 "Track %d needs downmix", name);
1043 }
1044 }
1045 }
1046 }
1047
1048 // select the processing hooks
1049 mHook = &AudioMixer::process__nop;
1050 if (mEnabled.size() > 0) {
1051 if (resampling) {
1052 if (mOutputTemp.get() == nullptr) {
1053 mOutputTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]);
1054 }
1055 if (mResampleTemp.get() == nullptr) {
1056 mResampleTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]);
1057 }
1058 mHook = &AudioMixer::process__genericResampling;
1059 } else {
1060 // we keep temp arrays around.
1061 mHook = &AudioMixer::process__genericNoResampling;
1062 if (all16BitsStereoNoResample && !volumeRamp) {
1063 if (mEnabled.size() == 1) {
1064 const std::shared_ptr<Track> &t = mTracks[mEnabled[0]];
1065 if ((t->needs & NEEDS_MUTE) == 0) {
1066 // The check prevents a muted track from acquiring a process hook.
1067 //
1068 // This is dangerous if the track is MONO as that requires
1069 // special case handling due to implicit channel duplication.
1070 // Stereo or Multichannel should actually be fine here.
1071 mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1072 t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat);
1073 }
1074 }
1075 }
1076 }
1077 }
1078
所有的传过来的数据会在PlaybackThread的AudioMixer里面进行处理
1.hook
mHook是总的hook,分别指向不同的处理函数AudioMixer::process__nop、AudioMixer::process__genericResampling、AudioMixer::process__genericNoResampling。
t->hook是每个track里面的hook,指向Track::track__nop、Track::getTrackHook。
xref: /frameworks/av/media/libaudioprocessing/AudioMixer.cpp
1514 // generic code with resampling
1515 void AudioMixer::process__genericResampling()
1516 {
1517 ALOGVV("process__genericResampling\n");
1518 int32_t * const outTemp = mOutputTemp.get(); // naked ptr
1519 size_t numFrames = mFrameCount;
1520
1521 for (const auto &pair : mGroups) {
1522 const auto &group = pair.second;
1523 const std::shared_ptr<Track> &t1 = mTracks[group[0]];
1524
1525 // clear temp buffer
1526 memset(outTemp, 0, sizeof(*outTemp) * t1->mMixerChannelCount * mFrameCount);
1527 for (const int name : group) {
1528 const std::shared_ptr<Track> &t = mTracks[name];
1529 int32_t *aux = NULL;
1530 if (CC_UNLIKELY(t->needs & NEEDS_AUX)) {
1531 aux = t->auxBuffer;
1532 }
1533
1534 // this is a little goofy, on the resampling case we don't
1535 // acquire/release the buffers because it's done by
1536 // the resampler.
1537 if (t->needs & NEEDS_RESAMPLE) {
1538 (t.get()->*t->hook)(outTemp, numFrames, mResampleTemp.get() /* naked ptr */, aux);
1539 } else {
1540
1541 size_t outFrames = 0;
1542
1543 while (outFrames < numFrames) {
1544 t->buffer.frameCount = numFrames - outFrames;
1545 t->bufferProvider->getNextBuffer(&t->buffer);
1546 t->mIn = t->buffer.raw;
1547 // t->mIn == nullptr can happen if the track was flushed just after having
1548 // been enabled for mixing.
1549 if (t->mIn == nullptr) break;
1550
1551 (t.get()->*t->hook)(
1552 outTemp + outFrames * t->mMixerChannelCount, t->buffer.frameCount,
1553 mResampleTemp.get() /* naked ptr */,
1554 aux != nullptr ? aux + outFrames : nullptr);
1555 outFrames += t->buffer.frameCount;
1556
1557 t->bufferProvider->releaseBuffer(&t->buffer);
1558 }
1559 }
1560 }
1561 convertMixerFormat(t1->mainBuffer, t1->mMixerFormat,
1562 outTemp, t1->mMixerInFormat, numFrames * t1->mMixerChannelCount);
1563 }
1564 }
1536 // the resampler.
1537 if (t->needs & NEEDS_RESAMPLE) {
1538 (t.get()->*t->hook)(outTemp, numFrames, mResampleTemp.get() /* naked ptr */, aux);
1539 }
1561 convertMixerFormat(t1->mainBuffer, t1->mMixerFormat,
1562 outTemp, t1->mMixerInFormat, numFrames * t1->mMixerChannelCount);
1563 }
2.mOutputTemp
mOutputTemp是临时缓冲区,重采样后的数据会放在临时缓冲区mOutputTemp中,最终会被放到mainBuffer里面。
xref: /frameworks/av/services/audioflinger/Threads.cpp
3215 bool AudioFlinger::PlaybackThread::threadLoop()
3216 {
3217 tlNBLogWriter = mNBLogWriter.get();
3218
3219 Vector< sp<Track> > tracksToRemove;
3220
3221 mStandbyTimeNs = systemTime();
3222 int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0.
3223 int64_t lastFramesWritten = -1; // track changes in timestamp server frames written
3224
3225 // MIXER
3226 nsecs_t lastWarning = 0;
3227
3228 // DUPLICATING
3229 // FIXME could this be made local to while loop?
3230 writeFrames = 0;
3231
3232 cacheParameters_l();
3233 mSleepTimeUs = mIdleSleepTimeUs;
3234
3235 if (mType == MIXER) {
3236 sleepTimeShift = 0;
3237 }
3238
3239 CpuStats cpuStats;
3240 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
3241
3242 acquireWakeLock();
3243
3244 // mNBLogWriter logging APIs can only be called by a single thread, typically the
3245 // thread associated with this PlaybackThread.
3246 // If you want to share the mNBLogWriter with other threads (for example, binder threads)
3247 // then all such threads must agree to hold a common mutex before logging.
3248 // So if you need to log when mutex is unlocked, set logString to a non-NULL string,
3249 // and then that string will be logged at the next convenient opportunity.
3250 // See reference to logString below.
3251 const char *logString = NULL;
3252
3253 // Estimated time for next buffer to be written to hal. This is used only on
3254 // suspended mode (for now) to help schedule the wait time until next iteration.
3255 nsecs_t timeLoopNextNs = 0;
3256
3257 checkSilentMode_l();
3258
3259 // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
3260 // TODO: add confirmation checks:
3261 // 1) DIRECT threads and linear PCM format really resets to 0?
3262 // 2) Is frame count really valid if not linear pcm?
3263 // 3) Are all 64 bits of position returned, not just lowest 32 bits?
3264 if (mType == OFFLOAD || mType == DIRECT) {
3265 mTimestampVerifier.setDiscontinuityMode(mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
3266 }
3267 audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3268
3269 // loopCount is used for statistics and diagnostics.
3270 for (int64_t loopCount = 0; !exitPending(); ++loopCount)
3271 {
3272 // Log merge requests are performed during AudioFlinger binder transactions, but
3273 // that does not cover audio playback. It's requested here for that reason.
3274 mAudioFlinger->requestLogMerge();
3275
3276 cpuStats.sample(myName);
3277
3278 Vector< sp<EffectChain> > effectChains;
3279 audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE;
3280 std::vector<sp<Track>> activeTracks;
3281
3282 // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency.
3283 //
3284 // Note: we access outDevice() outside of mLock.
3285 if (isMsdDevice() && (outDevice() & AUDIO_DEVICE_OUT_BUS) != 0) {
3286 // Here, we try for the AF lock, but do not block on it as the latency
3287 // is more informational.
3288 if (mAudioFlinger->mLock.tryLock() == NO_ERROR) {
3289 std::vector<PatchPanel::SoftwarePatch> swPatches;
3290 double latencyMs;
3291 status_t status = INVALID_OPERATION;
3292 audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3293 if (mAudioFlinger->mPatchPanel.getDownstreamSoftwarePatches(id(), &swPatches) == OK
3294 && swPatches.size() > 0) {
3295 status = swPatches[0].getLatencyMs_l(&latencyMs);
3296 downstreamPatchHandle = swPatches[0].getPatchHandle();
3297 }
3298 if (downstreamPatchHandle != lastDownstreamPatchHandle) {
3299 mDownstreamLatencyStatMs.reset();
3300 lastDownstreamPatchHandle = downstreamPatchHandle;
3301 }
3302 if (status == OK) {
3303 // verify downstream latency (we assume a max reasonable
3304 // latency of 5 seconds).
3305 const double minLatency = 0., maxLatency = 5000.;
3306 if (latencyMs >= minLatency && latencyMs <= maxLatency) {
3307 ALOGV("new downstream latency %lf ms", latencyMs);
3308 } else {
3309 ALOGD("out of range downstream latency %lf ms", latencyMs);
3310 if (latencyMs < minLatency) latencyMs = minLatency;
3311 else if (latencyMs > maxLatency) latencyMs = maxLatency;
3312 }
3313 mDownstreamLatencyStatMs.add(latencyMs);
3314 }
3315 mAudioFlinger->mLock.unlock();
3316 }
3317 } else {
3318 if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
3319 // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats.
3320 mDownstreamLatencyStatMs.reset();
3321 lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3322 }
3323 }
3324
3325 { // scope for mLock
3326
3327 Mutex::Autolock _l(mLock);
3328
3329 processConfigEvents_l();
3330
3331 // See comment at declaration of logString for why this is done under mLock
3332 if (logString != NULL) {
3333 mNBLogWriter->logTimestamp();
3334 mNBLogWriter->log(logString);
3335 logString = NULL;
3336 }
3337
3338 // Collect timestamp statistics for the Playback Thread types that support it.
3339 if (mType == MIXER
3340 || mType == DUPLICATING
3341 || mType == DIRECT
3342 || mType == OFFLOAD) { // no indentation
3343 // Gather the framesReleased counters for all active tracks,
3344 // and associate with the sink frames written out. We need
3345 // this to convert the sink timestamp to the track timestamp.
3346 bool kernelLocationUpdate = false;
3347 ExtendedTimestamp timestamp; // use private copy to fetch
3348 if (mStandby) {
3349 mTimestampVerifier.discontinuity();
3350 } else if (threadloop_getHalTimestamp_l(×tamp) == OK) {
3351 mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
3352 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3353 mSampleRate);
3354
3355 if (isTimestampCorrectionEnabled()) {
3356 ALOGV("TS_BEFORE: %d %lld %lld", id(),
3357 (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3358 (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
3359 auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
3360 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3361 = correctedTimestamp.mFrames;
3362 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
3363 = correctedTimestamp.mTimeNs;
3364 ALOGV("TS_AFTER: %d %lld %lld", id(),
3365 (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3366 (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
3367
3368 // Note: Downstream latency only added if timestamp correction enabled.
3369 if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info.
3370 const int64_t newPosition =
3371 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3372 - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
3373 // prevent retrograde
3374 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max(
3375 newPosition,
3376 (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3377 - mSuspendedFrames));
3378 }
3379 }
3380
3381 // We always fetch the timestamp here because often the downstream
3382 // sink will block while writing.
3383
3384 // We keep track of the last valid kernel position in case we are in underrun
3385 // and the normal mixer period is the same as the fast mixer period, or there
3386 // is some error from the HAL.
3387 if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
3388 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
3389 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
3390 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
3391 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
3392
3393 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
3394 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER];
3395 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
3396 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER];
3397 }
3398
3399 if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
3400 kernelLocationUpdate = true;
3401 } else {
3402 ALOGVV("getTimestamp error - no valid kernel position");
3403 }
3404
3405 // copy over kernel info
3406 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
3407 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3408 + mSuspendedFrames; // add frames discarded when suspended
3409 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
3410 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
3411 } else {
3412 mTimestampVerifier.error();
3413 }
3414
3415 // mFramesWritten for non-offloaded tracks are contiguous
3416 // even after standby() is called. This is useful for the track frame
3417 // to sink frame mapping.
3418 bool serverLocationUpdate = false;
3419 if (mFramesWritten != lastFramesWritten) {
3420 serverLocationUpdate = true;
3421 lastFramesWritten = mFramesWritten;
3422 }
3423 // Only update timestamps if there is a meaningful change.
3424 // Either the kernel timestamp must be valid or we have written something.
3425 if (kernelLocationUpdate || serverLocationUpdate) {
3426 if (serverLocationUpdate) {
3427 // use the time before we called the HAL write - it is a bit more accurate
3428 // to when the server last read data than the current time here.
3429 //
3430 // If we haven't written anything, mLastIoBeginNs will be -1
3431 // and we use systemTime().
3432 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten;
3433 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1
3434 ? systemTime() : mLastIoBeginNs;
3435 }
3436
3437 for (const sp<Track> &t : mActiveTracks) {
3438 if (!t->isFastTrack()) {
3439 t->updateTrackFrameInfo(
3440 t->mAudioTrackServerProxy->framesReleased(),
3441 mFramesWritten,
3442 mSampleRate,
3443 mTimestamp);
3444 }
3445 }
3446 }
3447
3448 if (audio_has_proportional_frames(mFormat)) {
3449 const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate);
3450 if (latencyMs != 0.) { // note 0. means timestamp is empty.
3451 mLatencyMs.add(latencyMs);
3452 }
3453 }
3454
3455 } // if (mType ... ) { // no indentation
3456 #if 0
3457 // logFormat example
3458 if (z % 100 == 0) {
3459 timespec ts;
3460 clock_gettime(CLOCK_MONOTONIC, &ts);
3461 LOGT("This is an integer %d, this is a float %f, this is my "
3462 "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts);
3463 LOGT("A deceptive null-terminated string %\0");
3464 }
3465 ++z;
3466 #endif
3467 saveOutputTracks();
3468 if (mSignalPending) {
3469 // A signal was raised while we were unlocked
3470 mSignalPending = false;
3471 } else if (waitingAsyncCallback_l()) {
3472 if (exitPending()) {
3473 break;
3474 }
3475 bool released = false;
3476 if (!keepWakeLock()) {
3477 releaseWakeLock_l();
3478 released = true;
3479 }
3480
3481 const int64_t waitNs = computeWaitTimeNs_l();
3482 ALOGV("wait async completion (wait time: %lld)", (long long)waitNs);
3483 status_t status = mWaitWorkCV.waitRelative(mLock, waitNs);
3484 if (status == TIMED_OUT) {
3485 mSignalPending = true; // if timeout recheck everything
3486 }
3487 ALOGV("async completion/wake");
3488 if (released) {
3489 acquireWakeLock_l();
3490 }
3491 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
3492 mSleepTimeUs = 0;
3493
3494 continue;
3495 }
3496 if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) ||
3497 isSuspended()) {
3498 // put audio hardware into standby after short delay
3499 if (shouldStandby_l()) {
3500
3501 threadLoop_standby();
3502
3503 // This is where we go into standby
3504 if (!mStandby) {
3505 LOG_AUDIO_STATE();
3506 }
3507 mStandby = true;
3508 sendStatistics(false /* force */);
3509 }
3510
3511 if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) {
3512 // we're about to wait, flush the binder command buffer
3513 IPCThreadState::self()->flushCommands();
3514
3515 clearOutputTracks();
3516
3517 if (exitPending()) {
3518 break;
3519 }
3520
3521 releaseWakeLock_l();
3522 // wait until we have something to do...
3523 ALOGV("%s going to sleep", myName.string());
3524 mWaitWorkCV.wait(mLock);
3525 ALOGV("%s waking up", myName.string());
3526 acquireWakeLock_l();
3527
3528 mMixerStatus = MIXER_IDLE;
3529 mMixerStatusIgnoringFastTracks = MIXER_IDLE;
3530 mBytesWritten = 0;
3531 mBytesRemaining = 0;
3532 checkSilentMode_l();
3533
3534 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
3535 mSleepTimeUs = mIdleSleepTimeUs;
3536 if (mType == MIXER) {
3537 sleepTimeShift = 0;
3538 }
3539
3540 continue;
3541 }
3542 }
3543 // mMixerStatusIgnoringFastTracks is also updated internally
3544 mMixerStatus = prepareTracks_l(&tracksToRemove);
3545
3546 mActiveTracks.updatePowerState(this);
3547
3548 updateMetadata_l();
3549
3550 // prevent any changes in effect chain list and in each effect chain
3551 // during mixing and effect process as the audio buffers could be deleted
3552 // or modified if an effect is created or deleted
3553 lockEffectChains_l(effectChains);
3554
3555 // Determine which session to pick up haptic data.
3556 // This must be done under the same lock as prepareTracks_l().
3557 // TODO: Write haptic data directly to sink buffer when mixing.
3558 if (mHapticChannelCount > 0 && effectChains.size() > 0) {
3559 for (const auto& track : mActiveTracks) {
3560 if (track->getHapticPlaybackEnabled()) {
3561 activeHapticSessionId = track->sessionId();
3562 break;
3563 }
3564 }
3565 }
3566
3567 // Acquire a local copy of active tracks with lock (release w/o lock).
3568 //
3569 // Control methods on the track acquire the ThreadBase lock (e.g. start()
3570 // stop(), pause(), etc.), but the threadLoop is entitled to call audio
3571 // data / buffer methods on tracks from activeTracks without the ThreadBase lock.
3572 activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end());
3573 } // mLock scope ends
3574
3575 if (mBytesRemaining == 0) {
3576 mCurrentWriteLength = 0;
3577 if (mMixerStatus == MIXER_TRACKS_READY) {
3578 // threadLoop_mix() sets mCurrentWriteLength
3579 threadLoop_mix();
3580 } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
3581 && (mMixerStatus != MIXER_DRAIN_ALL)) {
3582 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data
3583 // must be written to HAL
3584 threadLoop_sleepTime();
3585 if (mSleepTimeUs == 0) {
3586 mCurrentWriteLength = mSinkBufferSize;
3587
3588 // Tally underrun frames as we are inserting 0s here.
3589 for (const auto& track : activeTracks) {
3590 if (track->mFillingUpStatus == Track::FS_ACTIVE) {
3591 track->mAudioTrackServerProxy->tallyUnderrunFrames(mNormalFrameCount);
3592 }
3593 }
3594 }
3595 }
3596 // Either threadLoop_mix() or threadLoop_sleepTime() should have set
3597 // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0.
3598 // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
3599 // or mSinkBuffer (if there are no effects).
3600 //
3601 // This is done pre-effects computation; if effects change to
3602 // support higher precision, this needs to move.
3603 //
3604 // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
3605 // TODO use mSleepTimeUs == 0 as an additional condition.
3606 if (mMixerBufferValid) {
3607 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
3608 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
3609
3610 // mono blend occurs for mixer threads only (not direct or offloaded)
3611 // and is handled here if we're going directly to the sink.
3612 if (requireMonoBlend() && !mEffectBufferValid) {
3613 mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount,
3614 true /*limit*/);
3615 }
3616
3617 if (!hasFastMixer()) {
3618 // Balance must take effect after mono conversion.
3619 // We do it here if there is no FastMixer.
3620 // mBalance detects zero balance within the class for speed (not needed here).
3621 mBalance.setBalance(mMasterBalance.load());
3622 mBalance.process((float *)mMixerBuffer, mNormalFrameCount);
3623 }
3624
3625 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
3626 mNormalFrameCount * (mChannelCount + mHapticChannelCount));
3627
3628 // If we're going directly to the sink and there are haptic channels,
3629 // we should adjust channels as the sample data is partially interleaved
3630 // in this case.
3631 if (!mEffectBufferValid && mHapticChannelCount > 0) {
3632 adjust_channels_non_destructive(buffer, mChannelCount, buffer,
3633 mChannelCount + mHapticChannelCount,
3634 audio_bytes_per_sample(format),
3635 audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount);
3636 }
3637 }
3638
3639 mBytesRemaining = mCurrentWriteLength;
3640 if (isSuspended()) {
3641 // Simulate write to HAL when suspended (e.g. BT SCO phone call).
3642 mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer.
3643 const size_t framesRemaining = mBytesRemaining / mFrameSize;
3644 mBytesWritten += mBytesRemaining;
3645 mFramesWritten += framesRemaining;
3646 mSuspendedFrames += framesRemaining; // to adjust kernel HAL position
3647 mBytesRemaining = 0;
3648 }
3649
3650 // only process effects if we're going to write
3651 if (mSleepTimeUs == 0 && mType != OFFLOAD) {
3652 for (size_t i = 0; i < effectChains.size(); i ++) {
3653 effectChains[i]->process_l();
3654 // TODO: Write haptic data directly to sink buffer when mixing.
3655 if (activeHapticSessionId != AUDIO_SESSION_NONE
3656 && activeHapticSessionId == effectChains[i]->sessionId()) {
3657 // Haptic data is active in this case, copy it directly from
3658 // in buffer to out buffer.
3659 const size_t audioBufferSize = mNormalFrameCount
3660 * audio_bytes_per_frame(mChannelCount, EFFECT_BUFFER_FORMAT);
3661 memcpy_by_audio_format(
3662 (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize,
3663 EFFECT_BUFFER_FORMAT,
3664 (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize,
3665 EFFECT_BUFFER_FORMAT, mNormalFrameCount * mHapticChannelCount);
3666 }
3667 }
3668 }
3669 }
3670 // Process effect chains for offloaded thread even if no audio
3671 // was read from audio track: process only updates effect state
3672 // and thus does have to be synchronized with audio writes but may have
3673 // to be called while waiting for async write callback
3674 if (mType == OFFLOAD) {
3675 for (size_t i = 0; i < effectChains.size(); i ++) {
3676 effectChains[i]->process_l();
3677 }
3678 }
3679
3680 // Only if the Effects buffer is enabled and there is data in the
3681 // Effects buffer (buffer valid), we need to
3682 // copy into the sink buffer.
3683 // TODO use mSleepTimeUs == 0 as an additional condition.
3684 if (mEffectBufferValid) {
3685 //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
3686
3687 if (requireMonoBlend()) {
3688 mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount,
3689 true /*limit*/);
3690 }
3691
3692 if (!hasFastMixer()) {
3693 // Balance must take effect after mono conversion.
3694 // We do it here if there is no FastMixer.
3695 // mBalance detects zero balance within the class for speed (not needed here).
3696 mBalance.setBalance(mMasterBalance.load());
3697 mBalance.process((float *)mEffectBuffer, mNormalFrameCount);
3698 }
3699
3700 memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat,
3701 mNormalFrameCount * (mChannelCount + mHapticChannelCount));
3702 // The sample data is partially interleaved when haptic channels exist,
3703 // we need to adjust channels here.
3704 if (mHapticChannelCount > 0) {
3705 adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer,
3706 mChannelCount + mHapticChannelCount,
3707 audio_bytes_per_sample(mFormat),
3708 audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount);
3709 }
3710 }
3711
3712 // enable changes in effect chain
3713 unlockEffectChains(effectChains);
3714
3715 if (!waitingAsyncCallback()) {
3716 // mSleepTimeUs == 0 means we must write to audio hardware
3717 if (mSleepTimeUs == 0) {
3718 ssize_t ret = 0;
3719 // writePeriodNs is updated >= 0 when ret > 0.
3720 int64_t writePeriodNs = -1;
3721 if (mBytesRemaining) {
3722 // FIXME rewrite to reduce number of system calls
3723 const int64_t lastIoBeginNs = systemTime();
3724 ret = threadLoop_write();
3725 const int64_t lastIoEndNs = systemTime();
3726 if (ret < 0) {
3727 mBytesRemaining = 0;
3728 } else if (ret > 0) {
3729 mBytesWritten += ret;
3730 mBytesRemaining -= ret;
3731 const int64_t frames = ret / mFrameSize;
3732 mFramesWritten += frames;
3733
3734 writePeriodNs = lastIoEndNs - mLastIoEndNs;
3735 // process information relating to write time.
3736 if (audio_has_proportional_frames(mFormat)) {
3737 // we are in a continuous mixing cycle
3738 if (mMixerStatus == MIXER_TRACKS_READY &&
3739 loopCount == lastLoopCountWritten + 1) {
3740
3741 const double jitterMs =
3742 TimestampVerifier<int64_t, int64_t>::computeJitterMs(
3743 {frames, writePeriodNs},
3744 {0, 0} /* lastTimestamp */, mSampleRate);
3745 const double processMs =
3746 (lastIoBeginNs - mLastIoEndNs) * 1e-6;
3747
3748 Mutex::Autolock _l(mLock);
3749 mIoJitterMs.add(jitterMs);
3750 mProcessTimeMs.add(processMs);
3751 }
3752
3753 // write blocked detection
3754 const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs;
3755 if (mType == MIXER && deltaWriteNs > maxPeriod) {
3756 mNumDelayedWrites++;
3757 if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) {
3758 ATRACE_NAME("underrun");
3759 ALOGW("write blocked for %lld msecs, "
3760 "%d delayed writes, thread %d",
3761 (long long)deltaWriteNs / NANOS_PER_MILLISECOND,
3762 mNumDelayedWrites, mId);
3763 lastWarning = lastIoEndNs;
3764 }
3765 }
3766 }
3767 // update timing info.
3768 mLastIoBeginNs = lastIoBeginNs;
3769 mLastIoEndNs = lastIoEndNs;
3770 lastLoopCountWritten = loopCount;
3771 }
3772 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
3773 (mMixerStatus == MIXER_DRAIN_ALL)) {
3774 threadLoop_drain();
3775 }
3776 if (mType == MIXER && !mStandby) {
3777
3778 if (mThreadThrottle
3779 && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks)
3780 && writePeriodNs > 0) { // we have write period info
3781 // Limit MixerThread data processing to no more than twice the
3782 // expected processing rate.
3783 //
3784 // This helps prevent underruns with NuPlayer and other applications
3785 // which may set up buffers that are close to the minimum size, or use
3786 // deep buffers, and rely on a double-buffering sleep strategy to fill.
3787 //
3788 // The throttle smooths out sudden large data drains from the device,
3789 // e.g. when it comes out of standby, which often causes problems with
3790 // (1) mixer threads without a fast mixer (which has its own warm-up)
3791 // (2) minimum buffer sized tracks (even if the track is full,
3792 // the app won't fill fast enough to handle the sudden draw).
3793 //
3794 // Total time spent in last processing cycle equals time spent in
3795 // 1. threadLoop_write, as well as time spent in
3796 // 2. threadLoop_mix (significant for heavy mixing, especially
3797 // on low tier processors)
3798
3799 // it's OK if deltaMs is an overestimate.
3800
3801 const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND;
3802
3803 const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs;
3804 if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) {
3805 usleep(throttleMs * 1000);
3806 // notify of throttle start on verbose log
3807 ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs,
3808 "mixer(%p) throttle begin:"
3809 " ret(%zd) deltaMs(%d) requires sleep %d ms",
3810 this, ret, deltaMs, throttleMs);
3811 mThreadThrottleTimeMs += throttleMs;
3812 // Throttle must be attributed to the previous mixer loop's write time
3813 // to allow back-to-back throttling.
3814 // This also ensures proper timing statistics.
3815 mLastIoEndNs = systemTime(); // we fetch the write end time again.
3816 } else {
3817 uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
3818 if (diff > 0) {
3819 // notify of throttle end on debug log
3820 // but prevent spamming for bluetooth
3821 ALOGD_IF(!audio_is_a2dp_out_device(outDevice()) &&
3822 !audio_is_hearing_aid_out_device(outDevice()),
3823 "mixer(%p) throttle end: throttle time(%u)", this, diff);
3824 mThreadThrottleEndMs = mThreadThrottleTimeMs;
3825 }
3826 }
3827 }
3828 }
3829
3830 } else {
3831 ATRACE_BEGIN("sleep");
3832 Mutex::Autolock _l(mLock);
3833 // suspended requires accurate metering of sleep time.
3834 if (isSuspended()) {
3835 // advance by expected sleepTime
3836 timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs);
3837 const nsecs_t nowNs = systemTime();
3838
3839 // compute expected next time vs current time.
3840 // (negative deltas are treated as delays).
3841 nsecs_t deltaNs = timeLoopNextNs - nowNs;
3842 if (deltaNs < -kMaxNextBufferDelayNs) {
3843 // Delays longer than the max allowed trigger a reset.
3844 ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs);
3845 deltaNs = microseconds((nsecs_t)mSleepTimeUs);
3846 timeLoopNextNs = nowNs + deltaNs;
3847 } else if (deltaNs < 0) {
3848 // Delays within the max delay allowed: zero the delta/sleepTime
3849 // to help the system catch up in the next iteration(s)
3850 ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs);
3851 deltaNs = 0;
3852 }
3853 // update sleep time (which is >= 0)
3854 mSleepTimeUs = deltaNs / 1000;
3855 }
3856 if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) {
3857 mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs));
3858 }
3859 ATRACE_END();
3860 }
3861 }
3862
3863 // Finally let go of removed track(s), without the lock held
3864 // since we can't guarantee the destructors won't acquire that
3865 // same lock. This will also mutate and push a new fast mixer state.
3866 threadLoop_removeTracks(tracksToRemove);
3867 tracksToRemove.clear();
3868
3869 // FIXME I don't understand the need for this here;
3870 // it was in the original code but maybe the
3871 // assignment in saveOutputTracks() makes this unnecessary?
3872 clearOutputTracks();
3873
3874 // Effect chains will be actually deleted here if they were removed from
3875 // mEffectChains list during mixing or effects processing
3876 effectChains.clear();
3877
3878 // FIXME Note that the above .clear() is no longer necessary since effectChains
3879 // is now local to this block, but will keep it for now (at least until merge done).
3880 }
3881
3882 threadLoop_exit();
3883
3884 if (!mStandby) {
3885 threadLoop_standby();
3886 mStandby = true;
3887 }
3888
3889 releaseWakeLock();
3890
3891 ALOGV("Thread %p type %d exiting", this, mType);
3892 return false;
3893 }
3. threadLoop
播放线程的主循环,当有数据传过来当时候会开启循环。
xref: /frameworks/av/services/audioflinger/Threads.cpp
2911 // shared by MIXER and DIRECT, overridden by DUPLICATING
2912 ssize_t AudioFlinger::PlaybackThread::threadLoop_write()
2913 {
2914 LOG_HIST_TS();
2915 mInWrite = true;
2916 ssize_t bytesWritten;
2917 const size_t offset = mCurrentWriteLength - mBytesRemaining;
2918
2919 // If an NBAIO sink is present, use it to write the normal mixer's submix
2920 if (mNormalSink != 0) {
2921
2922 const size_t count = mBytesRemaining / mFrameSize;
2923
2924 ATRACE_BEGIN("write");
2925 // update the setpoint when AudioFlinger::mScreenState changes
2926 uint32_t screenState = AudioFlinger::mScreenState;
2927 if (screenState != mScreenState) {
2928 mScreenState = screenState;
2929 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
2930 if (pipe != NULL) {
2931 pipe->setAvgFrames((mScreenState & 1) ?
2932 (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
2933 }
2934 }
2935 ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
2936 ATRACE_END();
2937 if (framesWritten > 0) {
2938 bytesWritten = framesWritten * mFrameSize;
2939 #ifdef TEE_SINK
2940 mTee.write((char *)mSinkBuffer + offset, framesWritten);
2941 #endif
2942 } else {
2943 bytesWritten = framesWritten;
2944 }
2945 // otherwise use the HAL / AudioStreamOut directly
2946 } else {
2947 // Direct output and offload threads
2948
2949 if (mUseAsyncWrite) {
2950 ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
2951 mWriteAckSequence += 2;
2952 mWriteAckSequence |= 1;
2953 ALOG_ASSERT(mCallbackThread != 0);
2954 mCallbackThread->setWriteBlocked(mWriteAckSequence);
2955 }
2956 // FIXME We should have an implementation of timestamps for direct output threads.
2957 // They are used e.g for multichannel PCM playback over HDMI.
2958 bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
2959
2960 if (mUseAsyncWrite &&
2961 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
2962 // do not wait for async callback in case of error of full write
2963 mWriteAckSequence &= ~1;
2964 ALOG_ASSERT(mCallbackThread != 0);
2965 mCallbackThread->setWriteBlocked(mWriteAckSequence);
2966 }
2967 }
2968
2969 mNumWrites++;
2970 mInWrite = false;
2971 mStandby = false;
2972 return bytesWritten;
2973 }
4. threadLoop_write
将混合后到数据写入硬件