1 /*
2 **
3 ** Copyright 2012, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 
19 #define LOG_TAG "AudioFlinger"
20 //#define LOG_NDEBUG 0
21 #define ATRACE_TAG ATRACE_TAG_AUDIO
22 
23 #include "Configuration.h"
24 #include <math.h>
25 #include <fcntl.h>
26 #include <memory>
27 #include <string>
28 #include <linux/futex.h>
29 #include <sys/stat.h>
30 #include <sys/syscall.h>
31 #include <cutils/bitops.h>
32 #include <cutils/properties.h>
33 #include <media/AudioContainers.h>
34 #include <media/AudioDeviceTypeAddr.h>
35 #include <media/AudioParameter.h>
36 #include <media/AudioResamplerPublic.h>
37 #include <media/RecordBufferConverter.h>
38 #include <media/TypeConverter.h>
39 #include <utils/Log.h>
40 #include <utils/Trace.h>
41 
42 #include <private/media/AudioTrackShared.h>
43 #include <private/android_filesystem_config.h>
44 #include <audio_utils/Balance.h>
45 #include <audio_utils/channels.h>
46 #include <audio_utils/mono_blend.h>
47 #include <audio_utils/primitives.h>
48 #include <audio_utils/format.h>
49 #include <audio_utils/minifloat.h>
50 #include <audio_utils/safe_math.h>
51 #include <system/audio_effects/effect_ns.h>
52 #include <system/audio_effects/effect_aec.h>
53 #include <system/audio.h>
54 
55 // NBAIO implementations
56 #include <media/nbaio/AudioStreamInSource.h>
57 #include <media/nbaio/AudioStreamOutSink.h>
58 #include <media/nbaio/MonoPipe.h>
59 #include <media/nbaio/MonoPipeReader.h>
60 #include <media/nbaio/Pipe.h>
61 #include <media/nbaio/PipeReader.h>
62 #include <media/nbaio/SourceAudioBufferProvider.h>
63 #include <mediautils/BatteryNotifier.h>
64 
65 #include <audiomanager/AudioManager.h>
66 #include <powermanager/PowerManager.h>
67 
68 #include <media/audiohal/EffectsFactoryHalInterface.h>
69 #include <media/audiohal/StreamHalInterface.h>
70 
71 #include "AudioFlinger.h"
72 #include "FastMixer.h"
73 #include "FastCapture.h"
74 #include <mediautils/SchedulingPolicyService.h>
75 #include <mediautils/ServiceUtilities.h>
76 
77 #ifdef ADD_BATTERY_DATA
78 #include <media/IMediaPlayerService.h>
79 #include <media/IMediaDeathNotifier.h>
80 #endif
81 
82 #ifdef DEBUG_CPU_USAGE
83 #include <audio_utils/Statistics.h>
84 #include <cpustats/ThreadCpuUsage.h>
85 #endif
86 
87 #include "AutoPark.h"
88 
89 #include <pthread.h>
90 #include "TypedLogger.h"
91 
92 // ----------------------------------------------------------------------------
93 
94 // Note: the following macro is used for extremely verbose logging message.  In
95 // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
96 // 0; but one side effect of this is to turn all LOGV's as well.  Some messages
97 // are so verbose that we want to suppress them even when we have ALOG_ASSERT
98 // turned on.  Do not uncomment the #def below unless you really know what you
99 // are doing and want to see all of the extremely verbose messages.
100 //#define VERY_VERY_VERBOSE_LOGGING
101 #ifdef VERY_VERY_VERBOSE_LOGGING
102 #define ALOGVV ALOGV
103 #else
104 #define ALOGVV(a...) do { } while(0)
105 #endif
106 
107 // TODO: Move these macro/inlines to a header file.
108 #define max(a, b) ((a) > (b) ? (a) : (b))
109 template <typename T>
min(const T & a,const T & b)110 static inline T min(const T& a, const T& b)
111 {
112     return a < b ? a : b;
113 }
114 
115 namespace android {
116 
117 // retry counts for buffer fill timeout
118 // 50 * ~20msecs = 1 second
119 static const int8_t kMaxTrackRetries = 50;
120 static const int8_t kMaxTrackStartupRetries = 50;
121 // allow less retry attempts on direct output thread.
122 // direct outputs can be a scarce resource in audio hardware and should
123 // be released as quickly as possible.
124 static const int8_t kMaxTrackRetriesDirect = 2;
125 
126 
127 
128 // don't warn about blocked writes or record buffer overflows more often than this
129 static const nsecs_t kWarningThrottleNs = seconds(5);
130 
131 // RecordThread loop sleep time upon application overrun or audio HAL read error
132 static const int kRecordThreadSleepUs = 5000;
133 
134 // maximum time to wait in sendConfigEvent_l() for a status to be received
135 static const nsecs_t kConfigEventTimeoutNs = seconds(2);
136 
137 // minimum sleep time for the mixer thread loop when tracks are active but in underrun
138 static const uint32_t kMinThreadSleepTimeUs = 5000;
139 // maximum divider applied to the active sleep time in the mixer thread loop
140 static const uint32_t kMaxThreadSleepTimeShift = 2;
141 
142 // minimum normal sink buffer size, expressed in milliseconds rather than frames
143 // FIXME This should be based on experimentally observed scheduling jitter
144 static const uint32_t kMinNormalSinkBufferSizeMs = 20;
145 // maximum normal sink buffer size
146 static const uint32_t kMaxNormalSinkBufferSizeMs = 24;
147 
148 // minimum capture buffer size in milliseconds to _not_ need a fast capture thread
149 // FIXME This should be based on experimentally observed scheduling jitter
150 static const uint32_t kMinNormalCaptureBufferSizeMs = 12;
151 
152 // Offloaded output thread standby delay: allows track transition without going to standby
153 static const nsecs_t kOffloadStandbyDelayNs = seconds(1);
154 
155 // Direct output thread minimum sleep time in idle or active(underrun) state
156 static const nsecs_t kDirectMinSleepTimeUs = 10000;
157 
158 // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good
159 // balance between power consumption and latency, and allows threads to be scheduled reliably
160 // by the CFS scheduler.
161 // FIXME Express other hardcoded references to 20ms with references to this constant and move
162 // it appropriately.
163 #define FMS_20 20
164 
165 // Whether to use fast mixer
166 static const enum {
167     FastMixer_Never,    // never initialize or use: for debugging only
168     FastMixer_Always,   // always initialize and use, even if not needed: for debugging only
169                         // normal mixer multiplier is 1
170     FastMixer_Static,   // initialize if needed, then use all the time if initialized,
171                         // multiplier is calculated based on min & max normal mixer buffer size
172     FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load,
173                         // multiplier is calculated based on min & max normal mixer buffer size
174     // FIXME for FastMixer_Dynamic:
175     //  Supporting this option will require fixing HALs that can't handle large writes.
176     //  For example, one HAL implementation returns an error from a large write,
177     //  and another HAL implementation corrupts memory, possibly in the sample rate converter.
178     //  We could either fix the HAL implementations, or provide a wrapper that breaks
179     //  up large writes into smaller ones, and the wrapper would need to deal with scheduler.
180 } kUseFastMixer = FastMixer_Static;
181 
182 // Whether to use fast capture
183 static const enum {
184     FastCapture_Never,  // never initialize or use: for debugging only
185     FastCapture_Always, // always initialize and use, even if not needed: for debugging only
186     FastCapture_Static, // initialize if needed, then use all the time if initialized
187 } kUseFastCapture = FastCapture_Static;
188 
189 // Priorities for requestPriority
190 static const int kPriorityAudioApp = 2;
191 static const int kPriorityFastMixer = 3;
192 static const int kPriorityFastCapture = 3;
193 
194 // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the
195 // track buffer in shared memory.  Zero on input means to use a default value.  For fast tracks,
196 // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'.
197 
198 // This is the default value, if not specified by property.
199 static const int kFastTrackMultiplier = 2;
200 
201 // The minimum and maximum allowed values
202 static const int kFastTrackMultiplierMin = 1;
203 static const int kFastTrackMultiplierMax = 2;
204 
205 // The actual value to use, which can be specified per-device via property af.fast_track_multiplier.
206 static int sFastTrackMultiplier = kFastTrackMultiplier;
207 
208 // See Thread::readOnlyHeap().
209 // Initially this heap is used to allocate client buffers for "fast" AudioRecord.
210 // Eventually it will be the single buffer that FastCapture writes into via HAL read(),
211 // and that all "fast" AudioRecord clients read from.  In either case, the size can be small.
212 static const size_t kRecordThreadReadOnlyHeapSize = 0xD000;
213 
214 // ----------------------------------------------------------------------------
215 
216 static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT;
217 
sFastTrackMultiplierInit()218 static void sFastTrackMultiplierInit()
219 {
220     char value[PROPERTY_VALUE_MAX];
221     if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
222         char *endptr;
223         unsigned long ul = strtoul(value, &endptr, 0);
224         if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
225             sFastTrackMultiplier = (int) ul;
226         }
227     }
228 }
229 
230 // ----------------------------------------------------------------------------
231 
232 #ifdef ADD_BATTERY_DATA
233 // To collect the amplifier usage
addBatteryData(uint32_t params)234 static void addBatteryData(uint32_t params) {
235     sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
236     if (service == NULL) {
237         // it already logged
238         return;
239     }
240 
241     service->addBatteryData(params);
242 }
243 #endif
244 
245 // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset
246 struct {
247     // call when you acquire a partial wakelock
acquireandroid::__anonf7c4eeac0308248     void acquire(const sp<IBinder> &wakeLockToken) {
249         pthread_mutex_lock(&mLock);
250         if (wakeLockToken.get() == nullptr) {
251             adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
252         } else {
253             if (mCount == 0) {
254                 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
255             }
256             ++mCount;
257         }
258         pthread_mutex_unlock(&mLock);
259     }
260 
261     // call when you release a partial wakelock.
releaseandroid::__anonf7c4eeac0308262     void release(const sp<IBinder> &wakeLockToken) {
263         if (wakeLockToken.get() == nullptr) {
264             return;
265         }
266         pthread_mutex_lock(&mLock);
267         if (--mCount < 0) {
268             ALOGE("negative wakelock count");
269             mCount = 0;
270         }
271         pthread_mutex_unlock(&mLock);
272     }
273 
274     // retrieves the boottime timebase offset from monotonic.
getBoottimeOffsetandroid::__anonf7c4eeac0308275     int64_t getBoottimeOffset() {
276         pthread_mutex_lock(&mLock);
277         int64_t boottimeOffset = mBoottimeOffset;
278         pthread_mutex_unlock(&mLock);
279         return boottimeOffset;
280     }
281 
282     // Adjusts the timebase offset between TIMEBASE_MONOTONIC
283     // and the selected timebase.
284     // Currently only TIMEBASE_BOOTTIME is allowed.
285     //
286     // This only needs to be called upon acquiring the first partial wakelock
287     // after all other partial wakelocks are released.
288     //
289     // We do an empirical measurement of the offset rather than parsing
290     // /proc/timer_list since the latter is not a formal kernel ABI.
adjustTimebaseOffsetandroid::__anonf7c4eeac0308291     static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) {
292         int clockbase;
293         switch (timebase) {
294         case ExtendedTimestamp::TIMEBASE_BOOTTIME:
295             clockbase = SYSTEM_TIME_BOOTTIME;
296             break;
297         default:
298             LOG_ALWAYS_FATAL("invalid timebase %d", timebase);
299             break;
300         }
301         // try three times to get the clock offset, choose the one
302         // with the minimum gap in measurements.
303         const int tries = 3;
304         nsecs_t bestGap, measured;
305         for (int i = 0; i < tries; ++i) {
306             const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
307             const nsecs_t tbase = systemTime(clockbase);
308             const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
309             const nsecs_t gap = tmono2 - tmono;
310             if (i == 0 || gap < bestGap) {
311                 bestGap = gap;
312                 measured = tbase - ((tmono + tmono2) >> 1);
313             }
314         }
315 
316         // to avoid micro-adjusting, we don't change the timebase
317         // unless it is significantly different.
318         //
319         // Assumption: It probably takes more than toleranceNs to
320         // suspend and resume the device.
321         static int64_t toleranceNs = 10000; // 10 us
322         if (llabs(*offset - measured) > toleranceNs) {
323             ALOGV("Adjusting timebase offset old: %lld  new: %lld",
324                     (long long)*offset, (long long)measured);
325             *offset = measured;
326         }
327     }
328 
329     pthread_mutex_t mLock;
330     int32_t mCount;
331     int64_t mBoottimeOffset;
332 } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization
333 
334 // ----------------------------------------------------------------------------
335 //      CPU Stats
336 // ----------------------------------------------------------------------------
337 
338 class CpuStats {
339 public:
340     CpuStats();
341     void sample(const String8 &title);
342 #ifdef DEBUG_CPU_USAGE
343 private:
344     ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns
345     audio_utils::Statistics<double> mWcStats; // statistics on thread CPU usage in wall clock ns
346 
347     audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles
348 
349     int mCpuNum;                        // thread's current CPU number
350     int mCpukHz;                        // frequency of thread's current CPU in kHz
351 #endif
352 };
353 
CpuStats()354 CpuStats::CpuStats()
355 #ifdef DEBUG_CPU_USAGE
356     : mCpuNum(-1), mCpukHz(-1)
357 #endif
358 {
359 }
360 
sample(const String8 & title __unused)361 void CpuStats::sample(const String8 &title
362 #ifndef DEBUG_CPU_USAGE
363                 __unused
364 #endif
365         ) {
366 #ifdef DEBUG_CPU_USAGE
367     // get current thread's delta CPU time in wall clock ns
368     double wcNs;
369     bool valid = mCpuUsage.sampleAndEnable(wcNs);
370 
371     // record sample for wall clock statistics
372     if (valid) {
373         mWcStats.add(wcNs);
374     }
375 
376     // get the current CPU number
377     int cpuNum = sched_getcpu();
378 
379     // get the current CPU frequency in kHz
380     int cpukHz = mCpuUsage.getCpukHz(cpuNum);
381 
382     // check if either CPU number or frequency changed
383     if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
384         mCpuNum = cpuNum;
385         mCpukHz = cpukHz;
386         // ignore sample for purposes of cycles
387         valid = false;
388     }
389 
390     // if no change in CPU number or frequency, then record sample for cycle statistics
391     if (valid && mCpukHz > 0) {
392         const double cycles = wcNs * cpukHz * 0.000001;
393         mHzStats.add(cycles);
394     }
395 
396     const unsigned n = mWcStats.getN();
397     // mCpuUsage.elapsed() is expensive, so don't call it every loop
398     if ((n & 127) == 1) {
399         const long long elapsed = mCpuUsage.elapsed();
400         if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
401             const double perLoop = elapsed / (double) n;
402             const double perLoop100 = perLoop * 0.01;
403             const double perLoop1k = perLoop * 0.001;
404             const double mean = mWcStats.getMean();
405             const double stddev = mWcStats.getStdDev();
406             const double minimum = mWcStats.getMin();
407             const double maximum = mWcStats.getMax();
408             const double meanCycles = mHzStats.getMean();
409             const double stddevCycles = mHzStats.getStdDev();
410             const double minCycles = mHzStats.getMin();
411             const double maxCycles = mHzStats.getMax();
412             mCpuUsage.resetElapsed();
413             mWcStats.reset();
414             mHzStats.reset();
415             ALOGD("CPU usage for %s over past %.1f secs\n"
416                 "  (%u mixer loops at %.1f mean ms per loop):\n"
417                 "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
418                 "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
419                 "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
420                     title.string(),
421                     elapsed * .000000001, n, perLoop * .000001,
422                     mean * .001,
423                     stddev * .001,
424                     minimum * .001,
425                     maximum * .001,
426                     mean / perLoop100,
427                     stddev / perLoop100,
428                     minimum / perLoop100,
429                     maximum / perLoop100,
430                     meanCycles / perLoop1k,
431                     stddevCycles / perLoop1k,
432                     minCycles / perLoop1k,
433                     maxCycles / perLoop1k);
434 
435         }
436     }
437 #endif
438 };
439 
440 // ----------------------------------------------------------------------------
441 //      ThreadBase
442 // ----------------------------------------------------------------------------
443 
444 // static
threadTypeToString(AudioFlinger::ThreadBase::type_t type)445 const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type)
446 {
447     switch (type) {
448     case MIXER:
449         return "MIXER";
450     case DIRECT:
451         return "DIRECT";
452     case DUPLICATING:
453         return "DUPLICATING";
454     case RECORD:
455         return "RECORD";
456     case OFFLOAD:
457         return "OFFLOAD";
458     case MMAP:
459         return "MMAP";
460     default:
461         return "unknown";
462     }
463 }
464 
ThreadBase(const sp<AudioFlinger> & audioFlinger,audio_io_handle_t id,type_t type,bool systemReady)465 AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
466         type_t type, bool systemReady)
467     :   Thread(false /*canCallJava*/),
468         mType(type),
469         mAudioFlinger(audioFlinger),
470         // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize
471         // are set by PlaybackThread::readOutputParameters_l() or
472         // RecordThread::readInputParameters_l()
473         //FIXME: mStandby should be true here. Is this some kind of hack?
474         mStandby(false),
475         mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id),
476         // mName will be set by concrete (non-virtual) subclass
477         mDeathRecipient(new PMDeathRecipient(this)),
478         mSystemReady(systemReady),
479         mSignalPending(false)
480 {
481     memset(&mPatch, 0, sizeof(struct audio_patch));
482 }
483 
~ThreadBase()484 AudioFlinger::ThreadBase::~ThreadBase()
485 {
486     // mConfigEvents should be empty, but just in case it isn't, free the memory it owns
487     mConfigEvents.clear();
488 
489     // do not lock the mutex in destructor
490     releaseWakeLock_l();
491     if (mPowerManager != 0) {
492         sp<IBinder> binder = IInterface::asBinder(mPowerManager);
493         binder->unlinkToDeath(mDeathRecipient);
494     }
495 
496     sendStatistics(true /* force */);
497 }
498 
readyToRun()499 status_t AudioFlinger::ThreadBase::readyToRun()
500 {
501     status_t status = initCheck();
502     if (status == NO_ERROR) {
503         ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid());
504     } else {
505         ALOGE("No working audio driver found.");
506     }
507     return status;
508 }
509 
exit()510 void AudioFlinger::ThreadBase::exit()
511 {
512     ALOGV("ThreadBase::exit");
513     // do any cleanup required for exit to succeed
514     preExit();
515     {
516         // This lock prevents the following race in thread (uniprocessor for illustration):
517         //  if (!exitPending()) {
518         //      // context switch from here to exit()
519         //      // exit() calls requestExit(), what exitPending() observes
520         //      // exit() calls signal(), which is dropped since no waiters
521         //      // context switch back from exit() to here
522         //      mWaitWorkCV.wait(...);
523         //      // now thread is hung
524         //  }
525         AutoMutex lock(mLock);
526         requestExit();
527         mWaitWorkCV.broadcast();
528     }
529     // When Thread::requestExitAndWait is made virtual and this method is renamed to
530     // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
531     requestExitAndWait();
532 }
533 
setParameters(const String8 & keyValuePairs)534 status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
535 {
536     ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
537     Mutex::Autolock _l(mLock);
538 
539     return sendSetParameterConfigEvent_l(keyValuePairs);
540 }
541 
542 // sendConfigEvent_l() must be called with ThreadBase::mLock held
543 // Can temporarily release the lock if waiting for a reply from processConfigEvents_l().
sendConfigEvent_l(sp<ConfigEvent> & event)544 status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event)
545 {
546     status_t status = NO_ERROR;
547 
548     if (event->mRequiresSystemReady && !mSystemReady) {
549         event->mWaitStatus = false;
550         mPendingConfigEvents.add(event);
551         return status;
552     }
553     mConfigEvents.add(event);
554     ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType);
555     mWaitWorkCV.signal();
556     mLock.unlock();
557     {
558         Mutex::Autolock _l(event->mLock);
559         while (event->mWaitStatus) {
560             if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) {
561                 event->mStatus = TIMED_OUT;
562                 event->mWaitStatus = false;
563             }
564         }
565         status = event->mStatus;
566     }
567     mLock.lock();
568     return status;
569 }
570 
sendIoConfigEvent(audio_io_config_event event,pid_t pid,audio_port_handle_t portId)571 void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid,
572                                                  audio_port_handle_t portId)
573 {
574     Mutex::Autolock _l(mLock);
575     sendIoConfigEvent_l(event, pid, portId);
576 }
577 
578 // sendIoConfigEvent_l() must be called with ThreadBase::mLock held
sendIoConfigEvent_l(audio_io_config_event event,pid_t pid,audio_port_handle_t portId)579 void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid,
580                                                    audio_port_handle_t portId)
581 {
582     // The audio statistics history is exponentially weighted to forget events
583     // about five or more seconds in the past.  In order to have
584     // crisper statistics for mediametrics, we reset the statistics on
585     // an IoConfigEvent, to reflect different properties for a new device.
586     mIoJitterMs.reset();
587     mLatencyMs.reset();
588     mProcessTimeMs.reset();
589     mTimestampVerifier.discontinuity();
590 
591     sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId);
592     sendConfigEvent_l(configEvent);
593 }
594 
sendPrioConfigEvent(pid_t pid,pid_t tid,int32_t prio,bool forApp)595 void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp)
596 {
597     Mutex::Autolock _l(mLock);
598     sendPrioConfigEvent_l(pid, tid, prio, forApp);
599 }
600 
601 // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held
sendPrioConfigEvent_l(pid_t pid,pid_t tid,int32_t prio,bool forApp)602 void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(
603         pid_t pid, pid_t tid, int32_t prio, bool forApp)
604 {
605     sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp);
606     sendConfigEvent_l(configEvent);
607 }
608 
609 // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held
sendSetParameterConfigEvent_l(const String8 & keyValuePair)610 status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair)
611 {
612     sp<ConfigEvent> configEvent;
613     AudioParameter param(keyValuePair);
614     int value;
615     if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) {
616         setMasterMono_l(value != 0);
617         if (param.size() == 1) {
618             return NO_ERROR; // should be a solo parameter - we don't pass down
619         }
620         param.remove(String8(AudioParameter::keyMonoOutput));
621         configEvent = new SetParameterConfigEvent(param.toString());
622     } else {
623         configEvent = new SetParameterConfigEvent(keyValuePair);
624     }
625     return sendConfigEvent_l(configEvent);
626 }
627 
sendCreateAudioPatchConfigEvent(const struct audio_patch * patch,audio_patch_handle_t * handle)628 status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent(
629                                                         const struct audio_patch *patch,
630                                                         audio_patch_handle_t *handle)
631 {
632     Mutex::Autolock _l(mLock);
633     sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle);
634     status_t status = sendConfigEvent_l(configEvent);
635     if (status == NO_ERROR) {
636         CreateAudioPatchConfigEventData *data =
637                                         (CreateAudioPatchConfigEventData *)configEvent->mData.get();
638         *handle = data->mHandle;
639     }
640     return status;
641 }
642 
sendReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle)643 status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent(
644                                                                 const audio_patch_handle_t handle)
645 {
646     Mutex::Autolock _l(mLock);
647     sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle);
648     return sendConfigEvent_l(configEvent);
649 }
650 
sendUpdateOutDeviceConfigEvent(const DeviceDescriptorBaseVector & outDevices)651 status_t AudioFlinger::ThreadBase::sendUpdateOutDeviceConfigEvent(
652         const DeviceDescriptorBaseVector& outDevices)
653 {
654     if (type() != RECORD) {
655         // The update out device operation is only for record thread.
656         return INVALID_OPERATION;
657     }
658     Mutex::Autolock _l(mLock);
659     sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices);
660     return sendConfigEvent_l(configEvent);
661 }
662 
663 
664 // post condition: mConfigEvents.isEmpty()
processConfigEvents_l()665 void AudioFlinger::ThreadBase::processConfigEvents_l()
666 {
667     bool configChanged = false;
668 
669     while (!mConfigEvents.isEmpty()) {
670         ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size());
671         sp<ConfigEvent> event = mConfigEvents[0];
672         mConfigEvents.removeAt(0);
673         switch (event->mType) {
674         case CFG_EVENT_PRIO: {
675             PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get();
676             // FIXME Need to understand why this has to be done asynchronously
677             int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp,
678                     true /*asynchronous*/);
679             if (err != 0) {
680                 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
681                       data->mPrio, data->mPid, data->mTid, err);
682             }
683         } break;
684         case CFG_EVENT_IO: {
685             IoConfigEventData *data = (IoConfigEventData *)event->mData.get();
686             ioConfigChanged(data->mEvent, data->mPid, data->mPortId);
687         } break;
688         case CFG_EVENT_SET_PARAMETER: {
689             SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get();
690             if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) {
691                 configChanged = true;
692                 mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed",
693                         data->mKeyValuePairs.string());
694             }
695         } break;
696         case CFG_EVENT_CREATE_AUDIO_PATCH: {
697             const DeviceTypeSet oldDevices = getDeviceTypes();
698             CreateAudioPatchConfigEventData *data =
699                                             (CreateAudioPatchConfigEventData *)event->mData.get();
700             event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle);
701             const DeviceTypeSet newDevices = getDeviceTypes();
702             mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)",
703                     dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(),
704                     dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str());
705         } break;
706         case CFG_EVENT_RELEASE_AUDIO_PATCH: {
707             const DeviceTypeSet oldDevices = getDeviceTypes();
708             ReleaseAudioPatchConfigEventData *data =
709                                             (ReleaseAudioPatchConfigEventData *)event->mData.get();
710             event->mStatus = releaseAudioPatch_l(data->mHandle);
711             const DeviceTypeSet newDevices = getDeviceTypes();
712             mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)",
713                     dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(),
714                     dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str());
715         } break;
716         case CFG_EVENT_UPDATE_OUT_DEVICE: {
717             UpdateOutDevicesConfigEventData *data =
718                     (UpdateOutDevicesConfigEventData *)event->mData.get();
719             updateOutDevices(data->mOutDevices);
720         } break;
721         default:
722             ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType);
723             break;
724         }
725         {
726             Mutex::Autolock _l(event->mLock);
727             if (event->mWaitStatus) {
728                 event->mWaitStatus = false;
729                 event->mCond.signal();
730             }
731         }
732         ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this);
733     }
734 
735     if (configChanged) {
736         cacheParameters_l();
737     }
738 }
739 
channelMaskToString(audio_channel_mask_t mask,bool output)740 String8 channelMaskToString(audio_channel_mask_t mask, bool output) {
741     String8 s;
742     const audio_channel_representation_t representation =
743             audio_channel_mask_get_representation(mask);
744 
745     switch (representation) {
746     // Travel all single bit channel mask to convert channel mask to string.
747     case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
748         if (output) {
749             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, ");
750             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, ");
751             if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, ");
752             if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, ");
753             if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, ");
754             if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, ");
755             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, ");
756             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, ");
757             if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, ");
758             if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, ");
759             if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, ");
760             if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,");
761             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, ");
762             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, ");
763             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, ");
764             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, ");
765             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " );
766             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " );
767             if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, " );
768             if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, " );
769             if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, " );
770             if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, " );
771             if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  ");
772         } else {
773             if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, ");
774             if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, ");
775             if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, ");
776             if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, ");
777             if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, ");
778             if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, ");
779             if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, ");
780             if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, ");
781             if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, ");
782             if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, ");
783             if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, ");
784             if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, ");
785             if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, ");
786             if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, ");
787             if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, ");
788             if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low freq, ");
789             if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, " );
790             if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, " );
791             if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, ");
792             if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, ");
793             if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  ");
794         }
795         const int len = s.length();
796         if (len > 2) {
797             (void) s.lockBuffer(len);      // needed?
798             s.unlockBuffer(len - 2);       // remove trailing ", "
799         }
800         return s;
801     }
802     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
803         s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask));
804         return s;
805     default:
806         s.appendFormat("unknown mask, representation:%d  bits:%#x",
807                 representation, audio_channel_mask_get_bits(mask));
808         return s;
809     }
810 }
811 
dump(int fd,const Vector<String16> & args)812 void AudioFlinger::ThreadBase::dump(int fd, const Vector<String16>& args)
813 {
814     dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input",
815             this, mThreadName, getTid(), type(), threadTypeToString(type()));
816 
817     bool locked = AudioFlinger::dumpTryLock(mLock);
818     if (!locked) {
819         dprintf(fd, "  Thread may be deadlocked\n");
820     }
821 
822     dumpBase_l(fd, args);
823     dumpInternals_l(fd, args);
824     dumpTracks_l(fd, args);
825     dumpEffectChains_l(fd, args);
826 
827     if (locked) {
828         mLock.unlock();
829     }
830 
831     dprintf(fd, "  Local log:\n");
832     mLocalLog.dump(fd, "   " /* prefix */, 40 /* lines */);
833 }
834 
dumpBase_l(int fd,const Vector<String16> & args __unused)835 void AudioFlinger::ThreadBase::dumpBase_l(int fd, const Vector<String16>& args __unused)
836 {
837     dprintf(fd, "  I/O handle: %d\n", mId);
838     dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no");
839     dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate);
840     dprintf(fd, "  HAL frame count: %zu\n", mFrameCount);
841     dprintf(fd, "  HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat).c_str());
842     dprintf(fd, "  HAL buffer size: %zu bytes\n", mBufferSize);
843     dprintf(fd, "  Channel count: %u\n", mChannelCount);
844     dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask,
845             channelMaskToString(mChannelMask, mType != RECORD).string());
846     dprintf(fd, "  Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat).c_str());
847     dprintf(fd, "  Processing frame size: %zu bytes\n", mFrameSize);
848     dprintf(fd, "  Pending config events:");
849     size_t numConfig = mConfigEvents.size();
850     if (numConfig) {
851         const size_t SIZE = 256;
852         char buffer[SIZE];
853         for (size_t i = 0; i < numConfig; i++) {
854             mConfigEvents[i]->dump(buffer, SIZE);
855             dprintf(fd, "\n    %s", buffer);
856         }
857         dprintf(fd, "\n");
858     } else {
859         dprintf(fd, " none\n");
860     }
861     // Note: output device may be used by capture threads for effects such as AEC.
862     dprintf(fd, "  Output devices: %s (%s)\n",
863             dumpDeviceTypes(outDeviceTypes()).c_str(), toString(outDeviceTypes()).c_str());
864     dprintf(fd, "  Input device: %#x (%s)\n",
865             inDeviceType(), toString(inDeviceType()).c_str());
866     dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str());
867 
868     // Dump timestamp statistics for the Thread types that support it.
869     if (mType == RECORD
870             || mType == MIXER
871             || mType == DUPLICATING
872             || mType == DIRECT
873             || mType == OFFLOAD) {
874         dprintf(fd, "  Timestamp stats: %s\n", mTimestampVerifier.toString().c_str());
875         dprintf(fd, "  Timestamp corrected: %s\n", isTimestampCorrectionEnabled() ? "yes" : "no");
876     }
877 
878     if (mLastIoBeginNs > 0) { // MMAP may not set this
879         dprintf(fd, "  Last %s occurred (msecs): %lld\n",
880                 isOutput() ? "write" : "read",
881                 (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND);
882     }
883 
884     if (mProcessTimeMs.getN() > 0) {
885         dprintf(fd, "  Process time ms stats: %s\n", mProcessTimeMs.toString().c_str());
886     }
887 
888     if (mIoJitterMs.getN() > 0) {
889         dprintf(fd, "  Hal %s jitter ms stats: %s\n",
890                 isOutput() ? "write" : "read",
891                 mIoJitterMs.toString().c_str());
892     }
893 
894     if (mLatencyMs.getN() > 0) {
895         dprintf(fd, "  Threadloop %s latency stats: %s\n",
896                 isOutput() ? "write" : "read",
897                 mLatencyMs.toString().c_str());
898     }
899 }
900 
dumpEffectChains_l(int fd,const Vector<String16> & args)901 void AudioFlinger::ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args)
902 {
903     const size_t SIZE = 256;
904     char buffer[SIZE];
905 
906     size_t numEffectChains = mEffectChains.size();
907     snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains);
908     write(fd, buffer, strlen(buffer));
909 
910     for (size_t i = 0; i < numEffectChains; ++i) {
911         sp<EffectChain> chain = mEffectChains[i];
912         if (chain != 0) {
913             chain->dump(fd, args);
914         }
915     }
916 }
917 
acquireWakeLock()918 void AudioFlinger::ThreadBase::acquireWakeLock()
919 {
920     Mutex::Autolock _l(mLock);
921     acquireWakeLock_l();
922 }
923 
getWakeLockTag()924 String16 AudioFlinger::ThreadBase::getWakeLockTag()
925 {
926     switch (mType) {
927     case MIXER:
928         return String16("AudioMix");
929     case DIRECT:
930         return String16("AudioDirectOut");
931     case DUPLICATING:
932         return String16("AudioDup");
933     case RECORD:
934         return String16("AudioIn");
935     case OFFLOAD:
936         return String16("AudioOffload");
937     case MMAP:
938         return String16("Mmap");
939     default:
940         ALOG_ASSERT(false);
941         return String16("AudioUnknown");
942     }
943 }
944 
acquireWakeLock_l()945 void AudioFlinger::ThreadBase::acquireWakeLock_l()
946 {
947     getPowerManager_l();
948     if (mPowerManager != 0) {
949         sp<IBinder> binder = new BBinder();
950         // Uses AID_AUDIOSERVER for wakelock.  updateWakeLockUids_l() updates with client uids.
951         status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
952                     binder,
953                     getWakeLockTag(),
954                     String16("audioserver"),
955                     true /* FIXME force oneway contrary to .aidl */);
956         if (status == NO_ERROR) {
957             mWakeLockToken = binder;
958         }
959         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status);
960     }
961 
962     gBoottime.acquire(mWakeLockToken);
963     mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
964             gBoottime.getBoottimeOffset();
965 }
966 
releaseWakeLock()967 void AudioFlinger::ThreadBase::releaseWakeLock()
968 {
969     Mutex::Autolock _l(mLock);
970     releaseWakeLock_l();
971 }
972 
releaseWakeLock_l()973 void AudioFlinger::ThreadBase::releaseWakeLock_l()
974 {
975     gBoottime.release(mWakeLockToken);
976     if (mWakeLockToken != 0) {
977         ALOGV("releaseWakeLock_l() %s", mThreadName);
978         if (mPowerManager != 0) {
979             mPowerManager->releaseWakeLock(mWakeLockToken, 0,
980                     true /* FIXME force oneway contrary to .aidl */);
981         }
982         mWakeLockToken.clear();
983     }
984 }
985 
getPowerManager_l()986 void AudioFlinger::ThreadBase::getPowerManager_l() {
987     if (mSystemReady && mPowerManager == 0) {
988         // use checkService() to avoid blocking if power service is not up yet
989         sp<IBinder> binder =
990             defaultServiceManager()->checkService(String16("power"));
991         if (binder == 0) {
992             ALOGW("Thread %s cannot connect to the power manager service", mThreadName);
993         } else {
994             mPowerManager = interface_cast<IPowerManager>(binder);
995             binder->linkToDeath(mDeathRecipient);
996         }
997     }
998 }
999 
updateWakeLockUids_l(const SortedVector<uid_t> & uids)1000 void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) {
1001     getPowerManager_l();
1002 
1003 #if !LOG_NDEBUG
1004     std::stringstream s;
1005     for (uid_t uid : uids) {
1006         s << uid << " ";
1007     }
1008     ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str());
1009 #endif
1010 
1011     if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called.
1012         if (mSystemReady) {
1013             ALOGE("no wake lock to update, but system ready!");
1014         } else {
1015             ALOGW("no wake lock to update, system not ready yet");
1016         }
1017         return;
1018     }
1019     if (mPowerManager != 0) {
1020         std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints
1021         status_t status = mPowerManager->updateWakeLockUids(
1022                 mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(),
1023                 true /* FIXME force oneway contrary to .aidl */);
1024         ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status);
1025     }
1026 }
1027 
clearPowerManager()1028 void AudioFlinger::ThreadBase::clearPowerManager()
1029 {
1030     Mutex::Autolock _l(mLock);
1031     releaseWakeLock_l();
1032     mPowerManager.clear();
1033 }
1034 
updateOutDevices(const DeviceDescriptorBaseVector & outDevices __unused)1035 void AudioFlinger::ThreadBase::updateOutDevices(
1036         const DeviceDescriptorBaseVector& outDevices __unused)
1037 {
1038     ALOGE("%s should only be called in RecordThread", __func__);
1039 }
1040 
binderDied(const wp<IBinder> & who __unused)1041 void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused)
1042 {
1043     sp<ThreadBase> thread = mThread.promote();
1044     if (thread != 0) {
1045         thread->clearPowerManager();
1046     }
1047     ALOGW("power manager service died !!!");
1048 }
1049 
setEffectSuspended_l(const effect_uuid_t * type,bool suspend,audio_session_t sessionId)1050 void AudioFlinger::ThreadBase::setEffectSuspended_l(
1051         const effect_uuid_t *type, bool suspend, audio_session_t sessionId)
1052 {
1053     sp<EffectChain> chain = getEffectChain_l(sessionId);
1054     if (chain != 0) {
1055         if (type != NULL) {
1056             chain->setEffectSuspended_l(type, suspend);
1057         } else {
1058             chain->setEffectSuspendedAll_l(suspend);
1059         }
1060     }
1061 
1062     updateSuspendedSessions_l(type, suspend, sessionId);
1063 }
1064 
checkSuspendOnAddEffectChain_l(const sp<EffectChain> & chain)1065 void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1066 {
1067     ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
1068     if (index < 0) {
1069         return;
1070     }
1071 
1072     const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
1073             mSuspendedSessions.valueAt(index);
1074 
1075     for (size_t i = 0; i < sessionEffects.size(); i++) {
1076         const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i);
1077         for (int j = 0; j < desc->mRefCount; j++) {
1078             if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1079                 chain->setEffectSuspendedAll_l(true);
1080             } else {
1081                 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
1082                     desc->mType.timeLow);
1083                 chain->setEffectSuspended_l(&desc->mType, true);
1084             }
1085         }
1086     }
1087 }
1088 
updateSuspendedSessions_l(const effect_uuid_t * type,bool suspend,audio_session_t sessionId)1089 void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1090                                                          bool suspend,
1091                                                          audio_session_t sessionId)
1092 {
1093     ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
1094 
1095     KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1096 
1097     if (suspend) {
1098         if (index >= 0) {
1099             sessionEffects = mSuspendedSessions.valueAt(index);
1100         } else {
1101             mSuspendedSessions.add(sessionId, sessionEffects);
1102         }
1103     } else {
1104         if (index < 0) {
1105             return;
1106         }
1107         sessionEffects = mSuspendedSessions.valueAt(index);
1108     }
1109 
1110 
1111     int key = EffectChain::kKeyForSuspendAll;
1112     if (type != NULL) {
1113         key = type->timeLow;
1114     }
1115     index = sessionEffects.indexOfKey(key);
1116 
1117     sp<SuspendedSessionDesc> desc;
1118     if (suspend) {
1119         if (index >= 0) {
1120             desc = sessionEffects.valueAt(index);
1121         } else {
1122             desc = new SuspendedSessionDesc();
1123             if (type != NULL) {
1124                 desc->mType = *type;
1125             }
1126             sessionEffects.add(key, desc);
1127             ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
1128         }
1129         desc->mRefCount++;
1130     } else {
1131         if (index < 0) {
1132             return;
1133         }
1134         desc = sessionEffects.valueAt(index);
1135         if (--desc->mRefCount == 0) {
1136             ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
1137             sessionEffects.removeItemsAt(index);
1138             if (sessionEffects.isEmpty()) {
1139                 ALOGV("updateSuspendedSessions_l() restore removing session %d",
1140                                  sessionId);
1141                 mSuspendedSessions.removeItem(sessionId);
1142             }
1143         }
1144     }
1145     if (!sessionEffects.isEmpty()) {
1146         mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1147     }
1148 }
1149 
checkSuspendOnEffectEnabled(bool enabled,audio_session_t sessionId,bool threadLocked)1150 void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(bool enabled,
1151                                                            audio_session_t sessionId,
1152                                                            bool threadLocked) {
1153     if (!threadLocked) {
1154         mLock.lock();
1155     }
1156 
1157     if (mType != RECORD) {
1158         // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1159         // another session. This gives the priority to well behaved effect control panels
1160         // and applications not using global effects.
1161         // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
1162         // global effects
1163         if (!audio_is_global_session(sessionId)) {
1164             setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1165         }
1166     }
1167 
1168     if (!threadLocked) {
1169         mLock.unlock();
1170     }
1171 }
1172 
1173 // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
checkEffectCompatibility_l(const effect_descriptor_t * desc,audio_session_t sessionId)1174 status_t AudioFlinger::RecordThread::checkEffectCompatibility_l(
1175         const effect_descriptor_t *desc, audio_session_t sessionId)
1176 {
1177     // No global output effect sessions on record threads
1178     if (sessionId == AUDIO_SESSION_OUTPUT_MIX
1179             || sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
1180         ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s",
1181                 desc->name, mThreadName);
1182         return BAD_VALUE;
1183     }
1184     // only pre processing effects on record thread
1185     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) {
1186         ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s",
1187                 desc->name, mThreadName);
1188         return BAD_VALUE;
1189     }
1190 
1191     // always allow effects without processing load or latency
1192     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
1193         return NO_ERROR;
1194     }
1195 
1196     audio_input_flags_t flags = mInput->flags;
1197     if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) {
1198         if (flags & AUDIO_INPUT_FLAG_RAW) {
1199             ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode",
1200                   desc->name, mThreadName);
1201             return BAD_VALUE;
1202         }
1203         if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1204             ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode",
1205                   desc->name, mThreadName);
1206             return BAD_VALUE;
1207         }
1208     }
1209     return NO_ERROR;
1210 }
1211 
1212 // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
checkEffectCompatibility_l(const effect_descriptor_t * desc,audio_session_t sessionId)1213 status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l(
1214         const effect_descriptor_t *desc, audio_session_t sessionId)
1215 {
1216     // no preprocessing on playback threads
1217     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
1218         ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback"
1219                 " thread %s", desc->name, mThreadName);
1220         return BAD_VALUE;
1221     }
1222 
1223     // always allow effects without processing load or latency
1224     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
1225         return NO_ERROR;
1226     }
1227 
1228     switch (mType) {
1229     case MIXER: {
1230 #ifndef MULTICHANNEL_EFFECT_CHAIN
1231         // Reject any effect on mixer multichannel sinks.
1232         // TODO: fix both format and multichannel issues with effects.
1233         if (mChannelCount != FCC_2) {
1234             ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER"
1235                     " thread %s", desc->name, mChannelCount, mThreadName);
1236             return BAD_VALUE;
1237         }
1238 #endif
1239         audio_output_flags_t flags = mOutput->flags;
1240         if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) {
1241             if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1242                 // global effects are applied only to non fast tracks if they are SW
1243                 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1244                     break;
1245                 }
1246             } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
1247                 // only post processing on output stage session
1248                 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1249                     ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed"
1250                             " on output stage session", desc->name);
1251                     return BAD_VALUE;
1252                 }
1253             } else if (sessionId == AUDIO_SESSION_DEVICE) {
1254                 // only post processing on output stage session
1255                 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1256                     ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed"
1257                             " on device session", desc->name);
1258                     return BAD_VALUE;
1259                 }
1260             } else {
1261                 // no restriction on effects applied on non fast tracks
1262                 if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) {
1263                     break;
1264                 }
1265             }
1266 
1267             if (flags & AUDIO_OUTPUT_FLAG_RAW) {
1268                 ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode",
1269                       desc->name);
1270                 return BAD_VALUE;
1271             }
1272             if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1273                 ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread"
1274                         " in fast mode", desc->name);
1275                 return BAD_VALUE;
1276             }
1277         }
1278     } break;
1279     case OFFLOAD:
1280         // nothing actionable on offload threads, if the effect:
1281         //   - is offloadable: the effect can be created
1282         //   - is NOT offloadable: the effect should still be created, but EffectHandle::enable()
1283         //     will take care of invalidating the tracks of the thread
1284         break;
1285     case DIRECT:
1286         // Reject any effect on Direct output threads for now, since the format of
1287         // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
1288         ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s",
1289                 desc->name, mThreadName);
1290         return BAD_VALUE;
1291     case DUPLICATING:
1292 #ifndef MULTICHANNEL_EFFECT_CHAIN
1293         // Reject any effect on mixer multichannel sinks.
1294         // TODO: fix both format and multichannel issues with effects.
1295         if (mChannelCount != FCC_2) {
1296             ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)"
1297                     " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName);
1298             return BAD_VALUE;
1299         }
1300 #endif
1301         if (audio_is_global_session(sessionId)) {
1302             ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING"
1303                     " thread %s", desc->name, mThreadName);
1304             return BAD_VALUE;
1305         }
1306         if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
1307             ALOGW("checkEffectCompatibility_l(): post processing effect %s on"
1308                     " DUPLICATING thread %s", desc->name, mThreadName);
1309             return BAD_VALUE;
1310         }
1311         if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) {
1312             ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on"
1313                     " DUPLICATING thread %s", desc->name, mThreadName);
1314             return BAD_VALUE;
1315         }
1316         break;
1317     default:
1318         LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType);
1319     }
1320 
1321     return NO_ERROR;
1322 }
1323 
1324 // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
createEffect_l(const sp<AudioFlinger::Client> & client,const sp<IEffectClient> & effectClient,int32_t priority,audio_session_t sessionId,effect_descriptor_t * desc,int * enabled,status_t * status,bool pinned)1325 sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
1326         const sp<AudioFlinger::Client>& client,
1327         const sp<IEffectClient>& effectClient,
1328         int32_t priority,
1329         audio_session_t sessionId,
1330         effect_descriptor_t *desc,
1331         int *enabled,
1332         status_t *status,
1333         bool pinned)
1334 {
1335     sp<EffectModule> effect;
1336     sp<EffectHandle> handle;
1337     status_t lStatus;
1338     sp<EffectChain> chain;
1339     bool chainCreated = false;
1340     bool effectCreated = false;
1341     audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED;
1342 
1343     lStatus = initCheck();
1344     if (lStatus != NO_ERROR) {
1345         ALOGW("createEffect_l() Audio driver not initialized.");
1346         goto Exit;
1347     }
1348 
1349     ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
1350 
1351     { // scope for mLock
1352         Mutex::Autolock _l(mLock);
1353 
1354         lStatus = checkEffectCompatibility_l(desc, sessionId);
1355         if (lStatus != NO_ERROR) {
1356             goto Exit;
1357         }
1358 
1359         // check for existing effect chain with the requested audio session
1360         chain = getEffectChain_l(sessionId);
1361         if (chain == 0) {
1362             // create a new chain for this session
1363             ALOGV("createEffect_l() new effect chain for session %d", sessionId);
1364             chain = new EffectChain(this, sessionId);
1365             addEffectChain_l(chain);
1366             chain->setStrategy(getStrategyForSession_l(sessionId));
1367             chainCreated = true;
1368         } else {
1369             effect = chain->getEffectFromDesc_l(desc);
1370         }
1371 
1372         ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
1373 
1374         if (effect == 0) {
1375             effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
1376             // create a new effect module if none present in the chain
1377             lStatus = chain->createEffect_l(effect, desc, effectId, sessionId, pinned);
1378             if (lStatus != NO_ERROR) {
1379                 goto Exit;
1380             }
1381             effectCreated = true;
1382 
1383             // FIXME: use vector of device and address when effect interface is ready.
1384             effect->setDevices(outDeviceTypeAddrs());
1385             effect->setInputDevice(inDeviceTypeAddr());
1386             effect->setMode(mAudioFlinger->getMode());
1387             effect->setAudioSource(mAudioSource);
1388         }
1389         // create effect handle and connect it to effect module
1390         handle = new EffectHandle(effect, client, effectClient, priority);
1391         lStatus = handle->initCheck();
1392         if (lStatus == OK) {
1393             lStatus = effect->addHandle(handle.get());
1394         }
1395         if (enabled != NULL) {
1396             *enabled = (int)effect->isEnabled();
1397         }
1398     }
1399 
1400 Exit:
1401     if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
1402         Mutex::Autolock _l(mLock);
1403         if (effectCreated) {
1404             chain->removeEffect_l(effect);
1405         }
1406         if (chainCreated) {
1407             removeEffectChain_l(chain);
1408         }
1409         // handle must be cleared by caller to avoid deadlock.
1410     }
1411 
1412     *status = lStatus;
1413     return handle;
1414 }
1415 
disconnectEffectHandle(EffectHandle * handle,bool unpinIfLast)1416 void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle,
1417                                                       bool unpinIfLast)
1418 {
1419     bool remove = false;
1420     sp<EffectModule> effect;
1421     {
1422         Mutex::Autolock _l(mLock);
1423         sp<EffectBase> effectBase = handle->effect().promote();
1424         if (effectBase == nullptr) {
1425             return;
1426         }
1427         effect = effectBase->asEffectModule();
1428         if (effect == nullptr) {
1429             return;
1430         }
1431         // restore suspended effects if the disconnected handle was enabled and the last one.
1432         remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
1433         if (remove) {
1434             removeEffect_l(effect, true);
1435         }
1436     }
1437     if (remove) {
1438         mAudioFlinger->updateOrphanEffectChains(effect);
1439         if (handle->enabled()) {
1440             effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
1441         }
1442     }
1443 }
1444 
onEffectEnable(const sp<EffectModule> & effect)1445 void AudioFlinger::ThreadBase::onEffectEnable(const sp<EffectModule>& effect) {
1446     if (mType == OFFLOAD || mType == MMAP) {
1447         Mutex::Autolock _l(mLock);
1448         broadcast_l();
1449     }
1450     if (!effect->isOffloadable()) {
1451         if (mType == ThreadBase::OFFLOAD) {
1452             PlaybackThread *t = (PlaybackThread *)this;
1453             t->invalidateTracks(AUDIO_STREAM_MUSIC);
1454         }
1455         if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1456             mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1457         }
1458     }
1459 }
1460 
onEffectDisable()1461 void AudioFlinger::ThreadBase::onEffectDisable() {
1462     if (mType == OFFLOAD || mType == MMAP) {
1463         Mutex::Autolock _l(mLock);
1464         broadcast_l();
1465     }
1466 }
1467 
getEffect(audio_session_t sessionId,int effectId)1468 sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId,
1469         int effectId)
1470 {
1471     Mutex::Autolock _l(mLock);
1472     return getEffect_l(sessionId, effectId);
1473 }
1474 
getEffect_l(audio_session_t sessionId,int effectId)1475 sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId,
1476         int effectId)
1477 {
1478     sp<EffectChain> chain = getEffectChain_l(sessionId);
1479     return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
1480 }
1481 
getEffectIds_l(audio_session_t sessionId)1482 std::vector<int> AudioFlinger::ThreadBase::getEffectIds_l(audio_session_t sessionId)
1483 {
1484     sp<EffectChain> chain = getEffectChain_l(sessionId);
1485     return chain != nullptr ? chain->getEffectIds() : std::vector<int>{};
1486 }
1487 
1488 // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
1489 // PlaybackThread::mLock held
addEffect_l(const sp<EffectModule> & effect)1490 status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
1491 {
1492     // check for existing effect chain with the requested audio session
1493     audio_session_t sessionId = effect->sessionId();
1494     sp<EffectChain> chain = getEffectChain_l(sessionId);
1495     bool chainCreated = false;
1496 
1497     ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(),
1498              "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %#x",
1499                     this, effect->desc().name, effect->desc().flags);
1500 
1501     if (chain == 0) {
1502         // create a new chain for this session
1503         ALOGV("addEffect_l() new effect chain for session %d", sessionId);
1504         chain = new EffectChain(this, sessionId);
1505         addEffectChain_l(chain);
1506         chain->setStrategy(getStrategyForSession_l(sessionId));
1507         chainCreated = true;
1508     }
1509     ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
1510 
1511     if (chain->getEffectFromId_l(effect->id()) != 0) {
1512         ALOGW("addEffect_l() %p effect %s already present in chain %p",
1513                 this, effect->desc().name, chain.get());
1514         return BAD_VALUE;
1515     }
1516 
1517     effect->setOffloaded(mType == OFFLOAD, mId);
1518 
1519     status_t status = chain->addEffect_l(effect);
1520     if (status != NO_ERROR) {
1521         if (chainCreated) {
1522             removeEffectChain_l(chain);
1523         }
1524         return status;
1525     }
1526 
1527     effect->setDevices(outDeviceTypeAddrs());
1528     effect->setInputDevice(inDeviceTypeAddr());
1529     effect->setMode(mAudioFlinger->getMode());
1530     effect->setAudioSource(mAudioSource);
1531 
1532     return NO_ERROR;
1533 }
1534 
removeEffect_l(const sp<EffectModule> & effect,bool release)1535 void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) {
1536 
1537     ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get());
1538     effect_descriptor_t desc = effect->desc();
1539     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1540         detachAuxEffect_l(effect->id());
1541     }
1542 
1543     sp<EffectChain> chain = effect->callback()->chain().promote();
1544     if (chain != 0) {
1545         // remove effect chain if removing last effect
1546         if (chain->removeEffect_l(effect, release) == 0) {
1547             removeEffectChain_l(chain);
1548         }
1549     } else {
1550         ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
1551     }
1552 }
1553 
lockEffectChains_l(Vector<sp<AudioFlinger::EffectChain>> & effectChains)1554 void AudioFlinger::ThreadBase::lockEffectChains_l(
1555         Vector< sp<AudioFlinger::EffectChain> >& effectChains)
1556 {
1557     effectChains = mEffectChains;
1558     for (size_t i = 0; i < mEffectChains.size(); i++) {
1559         mEffectChains[i]->lock();
1560     }
1561 }
1562 
unlockEffectChains(const Vector<sp<AudioFlinger::EffectChain>> & effectChains)1563 void AudioFlinger::ThreadBase::unlockEffectChains(
1564         const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
1565 {
1566     for (size_t i = 0; i < effectChains.size(); i++) {
1567         effectChains[i]->unlock();
1568     }
1569 }
1570 
getEffectChain(audio_session_t sessionId)1571 sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId)
1572 {
1573     Mutex::Autolock _l(mLock);
1574     return getEffectChain_l(sessionId);
1575 }
1576 
getEffectChain_l(audio_session_t sessionId) const1577 sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId)
1578         const
1579 {
1580     size_t size = mEffectChains.size();
1581     for (size_t i = 0; i < size; i++) {
1582         if (mEffectChains[i]->sessionId() == sessionId) {
1583             return mEffectChains[i];
1584         }
1585     }
1586     return 0;
1587 }
1588 
setMode(audio_mode_t mode)1589 void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
1590 {
1591     Mutex::Autolock _l(mLock);
1592     size_t size = mEffectChains.size();
1593     for (size_t i = 0; i < size; i++) {
1594         mEffectChains[i]->setMode_l(mode);
1595     }
1596 }
1597 
toAudioPortConfig(struct audio_port_config * config)1598 void AudioFlinger::ThreadBase::toAudioPortConfig(struct audio_port_config *config)
1599 {
1600     config->type = AUDIO_PORT_TYPE_MIX;
1601     config->ext.mix.handle = mId;
1602     config->sample_rate = mSampleRate;
1603     config->format = mFormat;
1604     config->channel_mask = mChannelMask;
1605     config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
1606                             AUDIO_PORT_CONFIG_FORMAT;
1607 }
1608 
systemReady()1609 void AudioFlinger::ThreadBase::systemReady()
1610 {
1611     Mutex::Autolock _l(mLock);
1612     if (mSystemReady) {
1613         return;
1614     }
1615     mSystemReady = true;
1616 
1617     for (size_t i = 0; i < mPendingConfigEvents.size(); i++) {
1618         sendConfigEvent_l(mPendingConfigEvents.editItemAt(i));
1619     }
1620     mPendingConfigEvents.clear();
1621 }
1622 
1623 template <typename T>
add(const sp<T> & track)1624 ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) {
1625     ssize_t index = mActiveTracks.indexOf(track);
1626     if (index >= 0) {
1627         ALOGW("ActiveTracks<T>::add track %p already there", track.get());
1628         return index;
1629     }
1630     logTrack("add", track);
1631     mActiveTracksGeneration++;
1632     mLatestActiveTrack = track;
1633     ++mBatteryCounter[track->uid()].second;
1634     mHasChanged = true;
1635     return mActiveTracks.add(track);
1636 }
1637 
1638 template <typename T>
remove(const sp<T> & track)1639 ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) {
1640     ssize_t index = mActiveTracks.remove(track);
1641     if (index < 0) {
1642         ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get());
1643         return index;
1644     }
1645     logTrack("remove", track);
1646     mActiveTracksGeneration++;
1647     --mBatteryCounter[track->uid()].second;
1648     // mLatestActiveTrack is not cleared even if is the same as track.
1649     mHasChanged = true;
1650 #ifdef TEE_SINK
1651     track->dumpTee(-1 /* fd */, "_REMOVE");
1652 #endif
1653     return index;
1654 }
1655 
1656 template <typename T>
clear()1657 void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() {
1658     for (const sp<T> &track : mActiveTracks) {
1659         BatteryNotifier::getInstance().noteStopAudio(track->uid());
1660         logTrack("clear", track);
1661     }
1662     mLastActiveTracksGeneration = mActiveTracksGeneration;
1663     if (!mActiveTracks.empty()) { mHasChanged = true; }
1664     mActiveTracks.clear();
1665     mLatestActiveTrack.clear();
1666     mBatteryCounter.clear();
1667 }
1668 
1669 template <typename T>
updatePowerState(sp<ThreadBase> thread,bool force)1670 void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState(
1671         sp<ThreadBase> thread, bool force) {
1672     // Updates ActiveTracks client uids to the thread wakelock.
1673     if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) {
1674         thread->updateWakeLockUids_l(getWakeLockUids());
1675         mLastActiveTracksGeneration = mActiveTracksGeneration;
1676     }
1677 
1678     // Updates BatteryNotifier uids
1679     for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) {
1680         const uid_t uid = it->first;
1681         ssize_t &previous = it->second.first;
1682         ssize_t &current = it->second.second;
1683         if (current > 0) {
1684             if (previous == 0) {
1685                 BatteryNotifier::getInstance().noteStartAudio(uid);
1686             }
1687             previous = current;
1688             ++it;
1689         } else if (current == 0) {
1690             if (previous > 0) {
1691                 BatteryNotifier::getInstance().noteStopAudio(uid);
1692             }
1693             it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase.
1694         } else /* (current < 0) */ {
1695             LOG_ALWAYS_FATAL("negative battery count %zd", current);
1696         }
1697     }
1698 }
1699 
1700 template <typename T>
readAndClearHasChanged()1701 bool AudioFlinger::ThreadBase::ActiveTracks<T>::readAndClearHasChanged() {
1702     const bool hasChanged = mHasChanged;
1703     mHasChanged = false;
1704     return hasChanged;
1705 }
1706 
1707 template <typename T>
logTrack(const char * funcName,const sp<T> & track) const1708 void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack(
1709         const char *funcName, const sp<T> &track) const {
1710     if (mLocalLog != nullptr) {
1711         String8 result;
1712         track->appendDump(result, false /* active */);
1713         mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string());
1714     }
1715 }
1716 
broadcast_l()1717 void AudioFlinger::ThreadBase::broadcast_l()
1718 {
1719     // Thread could be blocked waiting for async
1720     // so signal it to handle state changes immediately
1721     // If threadLoop is currently unlocked a signal of mWaitWorkCV will
1722     // be lost so we also flag to prevent it blocking on mWaitWorkCV
1723     mSignalPending = true;
1724     mWaitWorkCV.broadcast();
1725 }
1726 
1727 // Call only from threadLoop() or when it is idle.
1728 // Do not call from high performance code as this may do binder rpc to the MediaMetrics service.
sendStatistics(bool force)1729 void AudioFlinger::ThreadBase::sendStatistics(bool force)
1730 {
1731     // Do not log if we have no stats.
1732     // We choose the timestamp verifier because it is the most likely item to be present.
1733     const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN;
1734     if (nstats == 0) {
1735         return;
1736     }
1737 
1738     // Don't log more frequently than once per 12 hours.
1739     // We use BOOTTIME to include suspend time.
1740     const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME);
1741     const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0
1742     if (!force && sinceNs <= 12 * NANOS_PER_HOUR) {
1743         return;
1744     }
1745 
1746     mLastRecordedTimestampVerifierN = mTimestampVerifier.getN();
1747     mLastRecordedTimeNs = timeNs;
1748 
1749     std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create("audiothread"));
1750 
1751 #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors.
1752 
1753     // thread configuration
1754     item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle
1755     // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId);
1756     item->setCString(MM_PREFIX "type", threadTypeToString(mType));
1757     item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate);
1758     item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask);
1759     item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str());
1760     item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount);
1761     item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes()).c_str());
1762     item->setCString(MM_PREFIX "inDevice", toString(inDeviceType()).c_str());
1763 
1764     // thread statistics
1765     if (mIoJitterMs.getN() > 0) {
1766         item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean());
1767         item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev());
1768     }
1769     if (mProcessTimeMs.getN() > 0) {
1770         item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean());
1771         item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev());
1772     }
1773     const auto tsjitter = mTimestampVerifier.getJitterMs();
1774     if (tsjitter.getN() > 0) {
1775         item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean());
1776         item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev());
1777     }
1778     if (mLatencyMs.getN() > 0) {
1779         item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean());
1780         item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev());
1781     }
1782 
1783     item->selfrecord();
1784 }
1785 
1786 // ----------------------------------------------------------------------------
1787 //      Playback
1788 // ----------------------------------------------------------------------------
1789 
PlaybackThread(const sp<AudioFlinger> & audioFlinger,AudioStreamOut * output,audio_io_handle_t id,type_t type,bool systemReady)1790 AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1791                                              AudioStreamOut* output,
1792                                              audio_io_handle_t id,
1793                                              type_t type,
1794                                              bool systemReady)
1795     :   ThreadBase(audioFlinger, id, type, systemReady),
1796         mNormalFrameCount(0), mSinkBuffer(NULL),
1797         mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
1798         mMixerBuffer(NULL),
1799         mMixerBufferSize(0),
1800         mMixerBufferFormat(AUDIO_FORMAT_INVALID),
1801         mMixerBufferValid(false),
1802         mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
1803         mEffectBuffer(NULL),
1804         mEffectBufferSize(0),
1805         mEffectBufferFormat(AUDIO_FORMAT_INVALID),
1806         mEffectBufferValid(false),
1807         mSuspended(0), mBytesWritten(0),
1808         mFramesWritten(0),
1809         mSuspendedFrames(0),
1810         mActiveTracks(&this->mLocalLog),
1811         // mStreamTypes[] initialized in constructor body
1812         mTracks(type == MIXER),
1813         mOutput(output),
1814         mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
1815         mMixerStatus(MIXER_IDLE),
1816         mMixerStatusIgnoringFastTracks(MIXER_IDLE),
1817         mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs),
1818         mBytesRemaining(0),
1819         mCurrentWriteLength(0),
1820         mUseAsyncWrite(false),
1821         mWriteAckSequence(0),
1822         mDrainSequence(0),
1823         mScreenState(AudioFlinger::mScreenState),
1824         // index 0 is reserved for normal mixer's submix
1825         mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1),
1826         mHwSupportsPause(false), mHwPaused(false), mFlushPending(false),
1827         mLeftVolFloat(-1.0), mRightVolFloat(-1.0)
1828 {
1829     snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id);
1830     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
1831 
1832     // Assumes constructor is called by AudioFlinger with it's mLock held, but
1833     // it would be safer to explicitly pass initial masterVolume/masterMute as
1834     // parameter.
1835     //
1836     // If the HAL we are using has support for master volume or master mute,
1837     // then do not attenuate or mute during mixing (just leave the volume at 1.0
1838     // and the mute set to false).
1839     mMasterVolume = audioFlinger->masterVolume_l();
1840     mMasterMute = audioFlinger->masterMute_l();
1841     if (mOutput && mOutput->audioHwDev) {
1842         if (mOutput->audioHwDev->canSetMasterVolume()) {
1843             mMasterVolume = 1.0;
1844         }
1845 
1846         if (mOutput->audioHwDev->canSetMasterMute()) {
1847             mMasterMute = false;
1848         }
1849         mIsMsdDevice = strcmp(
1850                 mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
1851     }
1852 
1853     readOutputParameters_l();
1854 
1855     // TODO: We may also match on address as well as device type for
1856     // AUDIO_DEVICE_OUT_BUS, AUDIO_DEVICE_OUT_ALL_A2DP, AUDIO_DEVICE_OUT_REMOTE_SUBMIX
1857     if (type == MIXER || type == DIRECT || type == OFFLOAD) {
1858         // TODO: This property should be ensure that only contains one single device type.
1859         mTimestampCorrectedDevice = (audio_devices_t)property_get_int64(
1860                 "audio.timestamp.corrected_output_device",
1861                 (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD
1862                                        : AUDIO_DEVICE_NONE));
1863     }
1864 
1865     // ++ operator does not compile
1866     for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_FOR_POLICY_CNT;
1867             stream = (audio_stream_type_t) (stream + 1)) {
1868         mStreamTypes[stream].volume = 0.0f;
1869         mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
1870     }
1871     // Audio patch volume is always max
1872     mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f;
1873     mStreamTypes[AUDIO_STREAM_PATCH].mute = false;
1874 }
1875 
~PlaybackThread()1876 AudioFlinger::PlaybackThread::~PlaybackThread()
1877 {
1878     mAudioFlinger->unregisterWriter(mNBLogWriter);
1879     free(mSinkBuffer);
1880     free(mMixerBuffer);
1881     free(mEffectBuffer);
1882 }
1883 
1884 // Thread virtuals
1885 
onFirstRef()1886 void AudioFlinger::PlaybackThread::onFirstRef()
1887 {
1888     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
1889 }
1890 
1891 // ThreadBase virtuals
preExit()1892 void AudioFlinger::PlaybackThread::preExit()
1893 {
1894     ALOGV("  preExit()");
1895     // FIXME this is using hard-coded strings but in the future, this functionality will be
1896     //       converted to use audio HAL extensions required to support tunneling
1897     status_t result = mOutput->stream->setParameters(String8("exiting=1"));
1898     ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result);
1899 }
1900 
dumpTracks_l(int fd,const Vector<String16> & args __unused)1901 void AudioFlinger::PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
1902 {
1903     String8 result;
1904 
1905     result.appendFormat("  Stream volumes in dB: ");
1906     for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
1907         const stream_type_t *st = &mStreamTypes[i];
1908         if (i > 0) {
1909             result.appendFormat(", ");
1910         }
1911         result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
1912         if (st->mute) {
1913             result.append("M");
1914         }
1915     }
1916     result.append("\n");
1917     write(fd, result.string(), result.length());
1918     result.clear();
1919 
1920     // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way.
1921     FastTrackUnderruns underruns = getFastTrackUnderruns(0);
1922     dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n",
1923             underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
1924 
1925     size_t numtracks = mTracks.size();
1926     size_t numactive = mActiveTracks.size();
1927     dprintf(fd, "  %zu Tracks", numtracks);
1928     size_t numactiveseen = 0;
1929     const char *prefix = "    ";
1930     if (numtracks) {
1931         dprintf(fd, " of which %zu are active\n", numactive);
1932         result.append(prefix);
1933         mTracks[0]->appendDumpHeader(result);
1934         for (size_t i = 0; i < numtracks; ++i) {
1935             sp<Track> track = mTracks[i];
1936             if (track != 0) {
1937                 bool active = mActiveTracks.indexOf(track) >= 0;
1938                 if (active) {
1939                     numactiveseen++;
1940                 }
1941                 result.append(prefix);
1942                 track->appendDump(result, active);
1943             }
1944         }
1945     } else {
1946         result.append("\n");
1947     }
1948     if (numactiveseen != numactive) {
1949         // some tracks in the active list were not in the tracks list
1950         result.append("  The following tracks are in the active list but"
1951                 " not in the track list\n");
1952         result.append(prefix);
1953         mActiveTracks[0]->appendDumpHeader(result);
1954         for (size_t i = 0; i < numactive; ++i) {
1955             sp<Track> track = mActiveTracks[i];
1956             if (mTracks.indexOf(track) < 0) {
1957                 result.append(prefix);
1958                 track->appendDump(result, true /* active */);
1959             }
1960         }
1961     }
1962 
1963     write(fd, result.string(), result.size());
1964 }
1965 
dumpInternals_l(int fd,const Vector<String16> & args __unused)1966 void AudioFlinger::PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
1967 {
1968     dprintf(fd, "  Master mute: %s\n", mMasterMute ? "on" : "off");
1969     if (mHapticChannelMask != AUDIO_CHANNEL_NONE) {
1970         dprintf(fd, "  Haptic channel mask: %#x (%s)\n", mHapticChannelMask,
1971                 channelMaskToString(mHapticChannelMask, true /* output */).c_str());
1972     }
1973     dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount);
1974     dprintf(fd, "  Total writes: %d\n", mNumWrites);
1975     dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites);
1976     dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no");
1977     dprintf(fd, "  Suspend count: %d\n", mSuspended);
1978     dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer);
1979     dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer);
1980     dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer);
1981     dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask);
1982     dprintf(fd, "  Standby delay ns=%lld\n", (long long)mStandbyDelayNs);
1983     AudioStreamOut *output = mOutput;
1984     audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE;
1985     dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n",
1986             output, flags, toString(flags).c_str());
1987     dprintf(fd, "  Frames written: %lld\n", (long long)mFramesWritten);
1988     dprintf(fd, "  Suspended frames: %lld\n", (long long)mSuspendedFrames);
1989     if (mPipeSink.get() != nullptr) {
1990         dprintf(fd, "  PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten());
1991     }
1992     if (output != nullptr) {
1993         dprintf(fd, "  Hal stream dump:\n");
1994         (void)output->stream->dump(fd);
1995     }
1996 }
1997 
1998 // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
createTrack_l(const sp<AudioFlinger::Client> & client,audio_stream_type_t streamType,const audio_attributes_t & attr,uint32_t * pSampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * pFrameCount,size_t * pNotificationFrameCount,uint32_t notificationsPerBuffer,float speed,const sp<IMemory> & sharedBuffer,audio_session_t sessionId,audio_output_flags_t * flags,pid_t creatorPid,pid_t tid,uid_t uid,status_t * status,audio_port_handle_t portId)1999 sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
2000         const sp<AudioFlinger::Client>& client,
2001         audio_stream_type_t streamType,
2002         const audio_attributes_t& attr,
2003         uint32_t *pSampleRate,
2004         audio_format_t format,
2005         audio_channel_mask_t channelMask,
2006         size_t *pFrameCount,
2007         size_t *pNotificationFrameCount,
2008         uint32_t notificationsPerBuffer,
2009         float speed,
2010         const sp<IMemory>& sharedBuffer,
2011         audio_session_t sessionId,
2012         audio_output_flags_t *flags,
2013         pid_t creatorPid,
2014         pid_t tid,
2015         uid_t uid,
2016         status_t *status,
2017         audio_port_handle_t portId)
2018 {
2019     size_t frameCount = *pFrameCount;
2020     size_t notificationFrameCount = *pNotificationFrameCount;
2021     sp<Track> track;
2022     status_t lStatus;
2023     audio_output_flags_t outputFlags = mOutput->flags;
2024     audio_output_flags_t requestedFlags = *flags;
2025     uint32_t sampleRate;
2026 
2027     if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) {
2028         lStatus = BAD_VALUE;
2029         goto Exit;
2030     }
2031 
2032     if (*pSampleRate == 0) {
2033         *pSampleRate = mSampleRate;
2034     }
2035     sampleRate = *pSampleRate;
2036 
2037     // special case for FAST flag considered OK if fast mixer is present
2038     if (hasFastMixer()) {
2039         outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST);
2040     }
2041 
2042     // Check if requested flags are compatible with output stream flags
2043     if ((*flags & outputFlags) != *flags) {
2044         ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)",
2045               *flags, outputFlags);
2046         *flags = (audio_output_flags_t)(*flags & outputFlags);
2047     }
2048 
2049     // client expresses a preference for FAST, but we get the final say
2050     if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2051       if (
2052             // PCM data
2053             audio_is_linear_pcm(format) &&
2054             // TODO: extract as a data library function that checks that a computationally
2055             // expensive downmixer is not required: isFastOutputChannelConversion()
2056             (channelMask == (mChannelMask | mHapticChannelMask) ||
2057                     mChannelMask != AUDIO_CHANNEL_OUT_STEREO ||
2058                     (channelMask == AUDIO_CHANNEL_OUT_MONO
2059                             /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) &&
2060             // hardware sample rate
2061             (sampleRate == mSampleRate) &&
2062             // normal mixer has an associated fast mixer
2063             hasFastMixer() &&
2064             // there are sufficient fast track slots available
2065             (mFastTrackAvailMask != 0)
2066             // FIXME test that MixerThread for this fast track has a capable output HAL
2067             // FIXME add a permission test also?
2068         ) {
2069         // static tracks can have any nonzero framecount, streaming tracks check against minimum.
2070         if (sharedBuffer == 0) {
2071             // read the fast track multiplier property the first time it is needed
2072             int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit);
2073             if (ok != 0) {
2074                 ALOGE("%s pthread_once failed: %d", __func__, ok);
2075             }
2076             frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0
2077         }
2078 
2079         // check compatibility with audio effects.
2080         { // scope for mLock
2081             Mutex::Autolock _l(mLock);
2082             for (audio_session_t session : {
2083                     AUDIO_SESSION_DEVICE,
2084                     AUDIO_SESSION_OUTPUT_STAGE,
2085                     AUDIO_SESSION_OUTPUT_MIX,
2086                     sessionId,
2087                 }) {
2088                 sp<EffectChain> chain = getEffectChain_l(session);
2089                 if (chain.get() != nullptr) {
2090                     audio_output_flags_t old = *flags;
2091                     chain->checkOutputFlagCompatibility(flags);
2092                     if (old != *flags) {
2093                         ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x",
2094                                 (int)session, (int)old, (int)*flags);
2095                     }
2096                 }
2097             }
2098         }
2099         ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0,
2100                  "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
2101                  frameCount, mFrameCount);
2102       } else {
2103         ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu "
2104                 "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
2105                 "sampleRate=%u mSampleRate=%u "
2106                 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
2107                 sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
2108                 audio_is_linear_pcm(format),
2109                 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
2110         *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2111       }
2112     }
2113 
2114     if (!audio_has_proportional_frames(format)) {
2115         if (sharedBuffer != 0) {
2116             // Same comment as below about ignoring frameCount parameter for set()
2117             frameCount = sharedBuffer->size();
2118         } else if (frameCount == 0) {
2119             frameCount = mNormalFrameCount;
2120         }
2121         if (notificationFrameCount != frameCount) {
2122             notificationFrameCount = frameCount;
2123         }
2124     } else if (sharedBuffer != 0) {
2125         // FIXME: Ensure client side memory buffers need
2126         // not have additional alignment beyond sample
2127         // (e.g. 16 bit stereo accessed as 32 bit frame).
2128         size_t alignment = audio_bytes_per_sample(format);
2129         if (alignment & 1) {
2130             // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java).
2131             alignment = 1;
2132         }
2133         uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
2134         size_t frameSize = channelCount * audio_bytes_per_sample(format);
2135         if (channelCount > 1) {
2136             // More than 2 channels does not require stronger alignment than stereo
2137             alignment <<= 1;
2138         }
2139         if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
2140             ALOGE("Invalid buffer alignment: address %p, channel count %u",
2141                   sharedBuffer->pointer(), channelCount);
2142             lStatus = BAD_VALUE;
2143             goto Exit;
2144         }
2145 
2146         // When initializing a shared buffer AudioTrack via constructors,
2147         // there's no frameCount parameter.
2148         // But when initializing a shared buffer AudioTrack via set(),
2149         // there _is_ a frameCount parameter.  We silently ignore it.
2150         frameCount = sharedBuffer->size() / frameSize;
2151     } else {
2152         size_t minFrameCount = 0;
2153         // For fast tracks we try to respect the application's request for notifications per buffer.
2154         if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2155             if (notificationsPerBuffer > 0) {
2156                 // Avoid possible arithmetic overflow during multiplication.
2157                 if (notificationsPerBuffer > SIZE_MAX / mFrameCount) {
2158                     ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu",
2159                           notificationsPerBuffer, mFrameCount);
2160                 } else {
2161                     minFrameCount = mFrameCount * notificationsPerBuffer;
2162                 }
2163             }
2164         } else {
2165             // For normal PCM streaming tracks, update minimum frame count.
2166             // Buffer depth is forced to be at least 2 x the normal mixer frame count and
2167             // cover audio hardware latency.
2168             // This is probably too conservative, but legacy application code may depend on it.
2169             // If you change this calculation, also review the start threshold which is related.
2170             uint32_t latencyMs = latency_l();
2171             if (latencyMs == 0) {
2172                 ALOGE("Error when retrieving output stream latency");
2173                 lStatus = UNKNOWN_ERROR;
2174                 goto Exit;
2175             }
2176 
2177             minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount,
2178                                 mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/);
2179 
2180         }
2181         if (frameCount < minFrameCount) {
2182             frameCount = minFrameCount;
2183         }
2184     }
2185 
2186     // Make sure that application is notified with sufficient margin before underrun.
2187     // The client can divide the AudioTrack buffer into sub-buffers,
2188     // and expresses its desire to server as the notification frame count.
2189     if (sharedBuffer == 0 && audio_is_linear_pcm(format)) {
2190         size_t maxNotificationFrames;
2191         if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2192             // notify every HAL buffer, regardless of the size of the track buffer
2193             maxNotificationFrames = mFrameCount;
2194         } else {
2195             // Triple buffer the notification period for a triple buffered mixer period;
2196             // otherwise, double buffering for the notification period is fine.
2197             //
2198             // TODO: This should be moved to AudioTrack to modify the notification period
2199             // on AudioTrack::setBufferSizeInFrames() changes.
2200             const int nBuffering =
2201                     (uint64_t{frameCount} * mSampleRate)
2202                             / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2;
2203 
2204             maxNotificationFrames = frameCount / nBuffering;
2205             // If client requested a fast track but this was denied, then use the smaller maximum.
2206             if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) {
2207                 size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000;
2208                 if (maxNotificationFrames > maxNotificationFramesFastDenied) {
2209                     maxNotificationFrames = maxNotificationFramesFastDenied;
2210                 }
2211             }
2212         }
2213         if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
2214             if (notificationFrameCount == 0) {
2215                 ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu",
2216                     maxNotificationFrames, frameCount);
2217             } else {
2218                 ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu",
2219                       notificationFrameCount, maxNotificationFrames, frameCount);
2220             }
2221             notificationFrameCount = maxNotificationFrames;
2222         }
2223     }
2224 
2225     *pFrameCount = frameCount;
2226     *pNotificationFrameCount = notificationFrameCount;
2227 
2228     switch (mType) {
2229 
2230     case DIRECT:
2231         if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()?
2232             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
2233                 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x "
2234                         "for output %p with format %#x",
2235                         sampleRate, format, channelMask, mOutput, mFormat);
2236                 lStatus = BAD_VALUE;
2237                 goto Exit;
2238             }
2239         }
2240         break;
2241 
2242     case OFFLOAD:
2243         if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
2244             ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \""
2245                     "for output %p with format %#x",
2246                     sampleRate, format, channelMask, mOutput, mFormat);
2247             lStatus = BAD_VALUE;
2248             goto Exit;
2249         }
2250         break;
2251 
2252     default:
2253         if (!audio_is_linear_pcm(format)) {
2254                 ALOGE("createTrack_l() Bad parameter: format %#x \""
2255                         "for output %p with format %#x",
2256                         format, mOutput, mFormat);
2257                 lStatus = BAD_VALUE;
2258                 goto Exit;
2259         }
2260         if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
2261             ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
2262             lStatus = BAD_VALUE;
2263             goto Exit;
2264         }
2265         break;
2266 
2267     }
2268 
2269     lStatus = initCheck();
2270     if (lStatus != NO_ERROR) {
2271         ALOGE("createTrack_l() audio driver not initialized");
2272         goto Exit;
2273     }
2274 
2275     { // scope for mLock
2276         Mutex::Autolock _l(mLock);
2277 
2278         // all tracks in same audio session must share the same routing strategy otherwise
2279         // conflicts will happen when tracks are moved from one output to another by audio policy
2280         // manager
2281         uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
2282         for (size_t i = 0; i < mTracks.size(); ++i) {
2283             sp<Track> t = mTracks[i];
2284             if (t != 0 && t->isExternalTrack()) {
2285                 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
2286                 if (sessionId == t->sessionId() && strategy != actual) {
2287                     ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
2288                             strategy, actual);
2289                     lStatus = BAD_VALUE;
2290                     goto Exit;
2291                 }
2292             }
2293         }
2294 
2295         track = new Track(this, client, streamType, attr, sampleRate, format,
2296                           channelMask, frameCount,
2297                           nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer,
2298                           sessionId, creatorPid, uid, *flags, TrackBase::TYPE_DEFAULT, portId);
2299 
2300         lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
2301         if (lStatus != NO_ERROR) {
2302             ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus);
2303             // track must be cleared from the caller as the caller has the AF lock
2304             goto Exit;
2305         }
2306         mTracks.add(track);
2307 
2308         sp<EffectChain> chain = getEffectChain_l(sessionId);
2309         if (chain != 0) {
2310             ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
2311             track->setMainBuffer(chain->inBuffer());
2312             chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
2313             chain->incTrackCnt();
2314         }
2315 
2316         if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) {
2317             pid_t callingPid = IPCThreadState::self()->getCallingPid();
2318             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
2319             // so ask activity manager to do this on our behalf
2320             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
2321         }
2322     }
2323 
2324     lStatus = NO_ERROR;
2325 
2326 Exit:
2327     *status = lStatus;
2328     return track;
2329 }
2330 
2331 template<typename T>
remove(const sp<T> & track)2332 ssize_t AudioFlinger::PlaybackThread::Tracks<T>::remove(const sp<T> &track)
2333 {
2334     const int trackId = track->id();
2335     const ssize_t index = mTracks.remove(track);
2336     if (index >= 0) {
2337         if (mSaveDeletedTrackIds) {
2338             // We can't directly access mAudioMixer since the caller may be outside of threadLoop.
2339             // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update,
2340             // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer.
2341             mDeletedTrackIds.emplace(trackId);
2342         }
2343     }
2344     return index;
2345 }
2346 
correctLatency_l(uint32_t latency) const2347 uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const
2348 {
2349     return latency;
2350 }
2351 
latency() const2352 uint32_t AudioFlinger::PlaybackThread::latency() const
2353 {
2354     Mutex::Autolock _l(mLock);
2355     return latency_l();
2356 }
latency_l() const2357 uint32_t AudioFlinger::PlaybackThread::latency_l() const
2358 {
2359     uint32_t latency;
2360     if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) {
2361         return correctLatency_l(latency);
2362     }
2363     return 0;
2364 }
2365 
setMasterVolume(float value)2366 void AudioFlinger::PlaybackThread::setMasterVolume(float value)
2367 {
2368     Mutex::Autolock _l(mLock);
2369     // Don't apply master volume in SW if our HAL can do it for us.
2370     if (mOutput && mOutput->audioHwDev &&
2371         mOutput->audioHwDev->canSetMasterVolume()) {
2372         mMasterVolume = 1.0;
2373     } else {
2374         mMasterVolume = value;
2375     }
2376 }
2377 
setMasterBalance(float balance)2378 void AudioFlinger::PlaybackThread::setMasterBalance(float balance)
2379 {
2380     mMasterBalance.store(balance);
2381 }
2382 
setMasterMute(bool muted)2383 void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
2384 {
2385     if (isDuplicating()) {
2386         return;
2387     }
2388     Mutex::Autolock _l(mLock);
2389     // Don't apply master mute in SW if our HAL can do it for us.
2390     if (mOutput && mOutput->audioHwDev &&
2391         mOutput->audioHwDev->canSetMasterMute()) {
2392         mMasterMute = false;
2393     } else {
2394         mMasterMute = muted;
2395     }
2396 }
2397 
setStreamVolume(audio_stream_type_t stream,float value)2398 void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
2399 {
2400     Mutex::Autolock _l(mLock);
2401     mStreamTypes[stream].volume = value;
2402     broadcast_l();
2403 }
2404 
setStreamMute(audio_stream_type_t stream,bool muted)2405 void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
2406 {
2407     Mutex::Autolock _l(mLock);
2408     mStreamTypes[stream].mute = muted;
2409     broadcast_l();
2410 }
2411 
streamVolume(audio_stream_type_t stream) const2412 float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
2413 {
2414     Mutex::Autolock _l(mLock);
2415     return mStreamTypes[stream].volume;
2416 }
2417 
setVolumeForOutput_l(float left,float right) const2418 void AudioFlinger::PlaybackThread::setVolumeForOutput_l(float left, float right) const
2419 {
2420     mOutput->stream->setVolume(left, right);
2421 }
2422 
2423 // addTrack_l() must be called with ThreadBase::mLock held
addTrack_l(const sp<Track> & track)2424 status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
2425 {
2426     status_t status = ALREADY_EXISTS;
2427 
2428     if (mActiveTracks.indexOf(track) < 0) {
2429         // the track is newly added, make sure it fills up all its
2430         // buffers before playing. This is to ensure the client will
2431         // effectively get the latency it requested.
2432         if (track->isExternalTrack()) {
2433             TrackBase::track_state state = track->mState;
2434             mLock.unlock();
2435             status = AudioSystem::startOutput(track->portId());
2436             mLock.lock();
2437             // abort track was stopped/paused while we released the lock
2438             if (state != track->mState) {
2439                 if (status == NO_ERROR) {
2440                     mLock.unlock();
2441                     AudioSystem::stopOutput(track->portId());
2442                     mLock.lock();
2443                 }
2444                 return INVALID_OPERATION;
2445             }
2446             // abort if start is rejected by audio policy manager
2447             if (status != NO_ERROR) {
2448                 return PERMISSION_DENIED;
2449             }
2450 #ifdef ADD_BATTERY_DATA
2451             // to track the speaker usage
2452             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
2453 #endif
2454             sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId());
2455         }
2456 
2457         // set retry count for buffer fill
2458         if (track->isOffloaded()) {
2459             if (track->isStopping_1()) {
2460                 track->mRetryCount = kMaxTrackStopRetriesOffload;
2461             } else {
2462                 track->mRetryCount = kMaxTrackStartupRetriesOffload;
2463             }
2464             track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED;
2465         } else {
2466             track->mRetryCount = kMaxTrackStartupRetries;
2467             track->mFillingUpStatus =
2468                     track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING;
2469         }
2470 
2471         if ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
2472                 && mHapticChannelMask != AUDIO_CHANNEL_NONE) {
2473             // Unlock due to VibratorService will lock for this call and will
2474             // call Tracks.mute/unmute which also require thread's lock.
2475             mLock.unlock();
2476             const int intensity = AudioFlinger::onExternalVibrationStart(
2477                     track->getExternalVibration());
2478             mLock.lock();
2479             track->setHapticIntensity(static_cast<AudioMixer::haptic_intensity_t>(intensity));
2480             // Haptic playback should be enabled by vibrator service.
2481             if (track->getHapticPlaybackEnabled()) {
2482                 // Disable haptic playback of all active track to ensure only
2483                 // one track playing haptic if current track should play haptic.
2484                 for (const auto &t : mActiveTracks) {
2485                     t->setHapticPlaybackEnabled(false);
2486                 }
2487             }
2488         }
2489 
2490         track->mResetDone = false;
2491         track->mPresentationCompleteFrames = 0;
2492         mActiveTracks.add(track);
2493         sp<EffectChain> chain = getEffectChain_l(track->sessionId());
2494         if (chain != 0) {
2495             ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(),
2496                     track->sessionId());
2497             chain->incActiveTrackCnt();
2498         }
2499 
2500         status = NO_ERROR;
2501     }
2502 
2503     onAddNewTrack_l();
2504     return status;
2505 }
2506 
destroyTrack_l(const sp<Track> & track)2507 bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
2508 {
2509     track->terminate();
2510     // active tracks are removed by threadLoop()
2511     bool trackActive = (mActiveTracks.indexOf(track) >= 0);
2512     track->mState = TrackBase::STOPPED;
2513     if (!trackActive) {
2514         removeTrack_l(track);
2515     } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) {
2516         track->mState = TrackBase::STOPPING_1;
2517     }
2518 
2519     return trackActive;
2520 }
2521 
removeTrack_l(const sp<Track> & track)2522 void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
2523 {
2524     track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
2525 
2526     String8 result;
2527     track->appendDump(result, false /* active */);
2528     mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string());
2529 
2530     mTracks.remove(track);
2531     if (track->isFastTrack()) {
2532         int index = track->mFastIndex;
2533         ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks);
2534         ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
2535         mFastTrackAvailMask |= 1 << index;
2536         // redundant as track is about to be destroyed, for dumpsys only
2537         track->mFastIndex = -1;
2538     }
2539     sp<EffectChain> chain = getEffectChain_l(track->sessionId());
2540     if (chain != 0) {
2541         chain->decTrackCnt();
2542     }
2543 }
2544 
getParameters(const String8 & keys)2545 String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
2546 {
2547     Mutex::Autolock _l(mLock);
2548     String8 out_s8;
2549     if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) {
2550         return out_s8;
2551     }
2552     return String8();
2553 }
2554 
selectPresentation(int presentationId,int programId)2555 status_t AudioFlinger::DirectOutputThread::selectPresentation(int presentationId, int programId) {
2556     Mutex::Autolock _l(mLock);
2557     if (mOutput == nullptr || mOutput->stream == nullptr) {
2558         return NO_INIT;
2559     }
2560     return mOutput->stream->selectPresentation(presentationId, programId);
2561 }
2562 
ioConfigChanged(audio_io_config_event event,pid_t pid,audio_port_handle_t portId)2563 void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
2564                                                    audio_port_handle_t portId) {
2565     sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
2566     ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event);
2567 
2568     desc->mIoHandle = mId;
2569 
2570     switch (event) {
2571     case AUDIO_OUTPUT_OPENED:
2572     case AUDIO_OUTPUT_REGISTERED:
2573     case AUDIO_OUTPUT_CONFIG_CHANGED:
2574         desc->mPatch = mPatch;
2575         desc->mChannelMask = mChannelMask;
2576         desc->mSamplingRate = mSampleRate;
2577         desc->mFormat = mFormat;
2578         desc->mFrameCount = mNormalFrameCount; // FIXME see
2579                                              // AudioFlinger::frameCount(audio_io_handle_t)
2580         desc->mFrameCountHAL = mFrameCount;
2581         desc->mLatency = latency_l();
2582         break;
2583     case AUDIO_CLIENT_STARTED:
2584         desc->mPatch = mPatch;
2585         desc->mPortId = portId;
2586         break;
2587     case AUDIO_OUTPUT_CLOSED:
2588     default:
2589         break;
2590     }
2591     mAudioFlinger->ioConfigChanged(event, desc, pid);
2592 }
2593 
onWriteReady()2594 void AudioFlinger::PlaybackThread::onWriteReady()
2595 {
2596     mCallbackThread->resetWriteBlocked();
2597 }
2598 
onDrainReady()2599 void AudioFlinger::PlaybackThread::onDrainReady()
2600 {
2601     mCallbackThread->resetDraining();
2602 }
2603 
onError()2604 void AudioFlinger::PlaybackThread::onError()
2605 {
2606     mCallbackThread->setAsyncError();
2607 }
2608 
resetWriteBlocked(uint32_t sequence)2609 void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
2610 {
2611     Mutex::Autolock _l(mLock);
2612     // reject out of sequence requests
2613     if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) {
2614         mWriteAckSequence &= ~1;
2615         mWaitWorkCV.signal();
2616     }
2617 }
2618 
resetDraining(uint32_t sequence)2619 void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence)
2620 {
2621     Mutex::Autolock _l(mLock);
2622     // reject out of sequence requests
2623     if ((mDrainSequence & 1) && (sequence == mDrainSequence)) {
2624         // Register discontinuity when HW drain is completed because that can cause
2625         // the timestamp frame position to reset to 0 for direct and offload threads.
2626         // (Out of sequence requests are ignored, since the discontinuity would be handled
2627         // elsewhere, e.g. in flush).
2628         mTimestampVerifier.discontinuity();
2629         mDrainSequence &= ~1;
2630         mWaitWorkCV.signal();
2631     }
2632 }
2633 
readOutputParameters_l()2634 void AudioFlinger::PlaybackThread::readOutputParameters_l()
2635 {
2636     // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL
2637     mSampleRate = mOutput->getSampleRate();
2638     mChannelMask = mOutput->getChannelMask();
2639     if (!audio_is_output_channel(mChannelMask)) {
2640         LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask);
2641     }
2642     if ((mType == MIXER || mType == DUPLICATING)
2643             && !isValidPcmSinkChannelMask(mChannelMask)) {
2644         LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output",
2645                 mChannelMask);
2646     }
2647     mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
2648     mBalance.setChannelMask(mChannelMask);
2649 
2650     // Get actual HAL format.
2651     status_t result = mOutput->stream->getFormat(&mHALFormat);
2652     LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result);
2653     // Get format from the shim, which will be different than the HAL format
2654     // if playing compressed audio over HDMI passthrough.
2655     mFormat = mOutput->getFormat();
2656     if (!audio_is_valid_format(mFormat)) {
2657         LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat);
2658     }
2659     if ((mType == MIXER || mType == DUPLICATING)
2660             && !isValidPcmSinkFormat(mFormat)) {
2661         LOG_FATAL("HAL format %#x not supported for mixed output",
2662                 mFormat);
2663     }
2664     mFrameSize = mOutput->getFrameSize();
2665     result = mOutput->stream->getBufferSize(&mBufferSize);
2666     LOG_ALWAYS_FATAL_IF(result != OK,
2667             "Error when retrieving output stream buffer size: %d", result);
2668     mFrameCount = mBufferSize / mFrameSize;
2669     if (mFrameCount & 15) {
2670         ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames",
2671                 mFrameCount);
2672     }
2673 
2674     if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) {
2675         if (mOutput->stream->setCallback(this) == OK) {
2676             mUseAsyncWrite = true;
2677             mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
2678         }
2679     }
2680 
2681     mHwSupportsPause = false;
2682     if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
2683         bool supportsPause = false, supportsResume = false;
2684         if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) {
2685             if (supportsPause && supportsResume) {
2686                 mHwSupportsPause = true;
2687             } else if (supportsPause) {
2688                 ALOGW("direct output implements pause but not resume");
2689             } else if (supportsResume) {
2690                 ALOGW("direct output implements resume but not pause");
2691             }
2692         }
2693     }
2694     if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
2695         LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume");
2696     }
2697 
2698     if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) {
2699         // For best precision, we use float instead of the associated output
2700         // device format (typically PCM 16 bit).
2701 
2702         mFormat = AUDIO_FORMAT_PCM_FLOAT;
2703         mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
2704         mBufferSize = mFrameSize * mFrameCount;
2705 
2706         // TODO: We currently use the associated output device channel mask and sample rate.
2707         // (1) Perhaps use the ORed channel mask of all downstream MixerThreads
2708         // (if a valid mask) to avoid premature downmix.
2709         // (2) Perhaps use the maximum sample rate of all downstream MixerThreads
2710         // instead of the output device sample rate to avoid loss of high frequency information.
2711         // This may need to be updated as MixerThread/OutputTracks are added and not here.
2712     }
2713 
2714     // Calculate size of normal sink buffer relative to the HAL output buffer size
2715     double multiplier = 1.0;
2716     if (mType == MIXER && (kUseFastMixer == FastMixer_Static ||
2717             kUseFastMixer == FastMixer_Dynamic)) {
2718         size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
2719         size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
2720 
2721         // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
2722         minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
2723         maxNormalFrameCount = maxNormalFrameCount & ~15;
2724         if (maxNormalFrameCount < minNormalFrameCount) {
2725             maxNormalFrameCount = minNormalFrameCount;
2726         }
2727         multiplier = (double) minNormalFrameCount / (double) mFrameCount;
2728         if (multiplier <= 1.0) {
2729             multiplier = 1.0;
2730         } else if (multiplier <= 2.0) {
2731             if (2 * mFrameCount <= maxNormalFrameCount) {
2732                 multiplier = 2.0;
2733             } else {
2734                 multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
2735             }
2736         } else {
2737             multiplier = floor(multiplier);
2738         }
2739     }
2740     mNormalFrameCount = multiplier * mFrameCount;
2741     // round up to nearest 16 frames to satisfy AudioMixer
2742     if (mType == MIXER || mType == DUPLICATING) {
2743         mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
2744     }
2745     ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", mFrameCount,
2746             mNormalFrameCount);
2747 
2748     // Check if we want to throttle the processing to no more than 2x normal rate
2749     mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */);
2750     mThreadThrottleTimeMs = 0;
2751     mThreadThrottleEndMs = 0;
2752     mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate);
2753 
2754     // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames.
2755     // Originally this was int16_t[] array, need to remove legacy implications.
2756     free(mSinkBuffer);
2757     mSinkBuffer = NULL;
2758     // For sink buffer size, we use the frame size from the downstream sink to avoid problems
2759     // with non PCM formats for compressed music, e.g. AAC, and Offload threads.
2760     const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
2761     (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
2762 
2763     // We resize the mMixerBuffer according to the requirements of the sink buffer which
2764     // drives the output.
2765     free(mMixerBuffer);
2766     mMixerBuffer = NULL;
2767     if (mMixerBufferEnabled) {
2768         mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT.
2769         mMixerBufferSize = mNormalFrameCount * mChannelCount
2770                 * audio_bytes_per_sample(mMixerBufferFormat);
2771         (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
2772     }
2773     free(mEffectBuffer);
2774     mEffectBuffer = NULL;
2775     if (mEffectBufferEnabled) {
2776         mEffectBufferFormat = EFFECT_BUFFER_FORMAT;
2777         mEffectBufferSize = mNormalFrameCount * mChannelCount
2778                 * audio_bytes_per_sample(mEffectBufferFormat);
2779         (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize);
2780     }
2781 
2782     mHapticChannelMask = mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL;
2783     mChannelMask &= ~mHapticChannelMask;
2784     mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask);
2785     mChannelCount -= mHapticChannelCount;
2786 
2787     // force reconfiguration of effect chains and engines to take new buffer size and audio
2788     // parameters into account
2789     // Note that mLock is not held when readOutputParameters_l() is called from the constructor
2790     // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
2791     // matter.
2792     // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
2793     Vector< sp<EffectChain> > effectChains = mEffectChains;
2794     for (size_t i = 0; i < effectChains.size(); i ++) {
2795         mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(),
2796             this/* srcThread */, this/* dstThread */);
2797     }
2798 }
2799 
updateMetadata_l()2800 void AudioFlinger::PlaybackThread::updateMetadata_l()
2801 {
2802     if (mOutput == nullptr || mOutput->stream == nullptr ) {
2803         return; // That should not happen
2804     }
2805     bool hasChanged = mActiveTracks.readAndClearHasChanged();
2806     for (const sp<Track> &track : mActiveTracks) {
2807         // Do not short-circuit as all hasChanged states must be reset
2808         // as all the metadata are going to be sent
2809         hasChanged |= track->readAndClearHasChanged();
2810     }
2811     if (!hasChanged) {
2812         return; // nothing to do
2813     }
2814     StreamOutHalInterface::SourceMetadata metadata;
2815     auto backInserter = std::back_inserter(metadata.tracks);
2816     for (const sp<Track> &track : mActiveTracks) {
2817         // No track is invalid as this is called after prepareTrack_l in the same critical section
2818         track->copyMetadataTo(backInserter);
2819     }
2820     sendMetadataToBackend_l(metadata);
2821 }
2822 
sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata & metadata)2823 void AudioFlinger::PlaybackThread::sendMetadataToBackend_l(
2824         const StreamOutHalInterface::SourceMetadata& metadata)
2825 {
2826     mOutput->stream->updateSourceMetadata(metadata);
2827 };
2828 
getRenderPosition(uint32_t * halFrames,uint32_t * dspFrames)2829 status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
2830 {
2831     if (halFrames == NULL || dspFrames == NULL) {
2832         return BAD_VALUE;
2833     }
2834     Mutex::Autolock _l(mLock);
2835     if (initCheck() != NO_ERROR) {
2836         return INVALID_OPERATION;
2837     }
2838     int64_t framesWritten = mBytesWritten / mFrameSize;
2839     *halFrames = framesWritten;
2840 
2841     if (isSuspended()) {
2842         // return an estimation of rendered frames when the output is suspended
2843         size_t latencyFrames = (latency_l() * mSampleRate) / 1000;
2844         *dspFrames = (uint32_t)
2845                 (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0);
2846         return NO_ERROR;
2847     } else {
2848         status_t status;
2849         uint32_t frames;
2850         status = mOutput->getRenderPosition(&frames);
2851         *dspFrames = (size_t)frames;
2852         return status;
2853     }
2854 }
2855 
getStrategyForSession_l(audio_session_t sessionId)2856 uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId)
2857 {
2858     // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
2859     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
2860     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2861         return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
2862     }
2863     for (size_t i = 0; i < mTracks.size(); i++) {
2864         sp<Track> track = mTracks[i];
2865         if (sessionId == track->sessionId() && !track->isInvalid()) {
2866             return AudioSystem::getStrategyForStream(track->streamType());
2867         }
2868     }
2869     return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
2870 }
2871 
2872 
getOutput() const2873 AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
2874 {
2875     Mutex::Autolock _l(mLock);
2876     return mOutput;
2877 }
2878 
clearOutput()2879 AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
2880 {
2881     Mutex::Autolock _l(mLock);
2882     AudioStreamOut *output = mOutput;
2883     mOutput = NULL;
2884     // FIXME FastMixer might also have a raw ptr to mOutputSink;
2885     //       must push a NULL and wait for ack
2886     mOutputSink.clear();
2887     mPipeSink.clear();
2888     mNormalSink.clear();
2889     return output;
2890 }
2891 
2892 // this method must always be called either with ThreadBase mLock held or inside the thread loop
stream() const2893 sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const
2894 {
2895     if (mOutput == NULL) {
2896         return NULL;
2897     }
2898     return mOutput->stream;
2899 }
2900 
activeSleepTimeUs() const2901 uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
2902 {
2903     return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
2904 }
2905 
setSyncEvent(const sp<SyncEvent> & event)2906 status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
2907 {
2908     if (!isValidSyncEvent(event)) {
2909         return BAD_VALUE;
2910     }
2911 
2912     Mutex::Autolock _l(mLock);
2913 
2914     for (size_t i = 0; i < mTracks.size(); ++i) {
2915         sp<Track> track = mTracks[i];
2916         if (event->triggerSession() == track->sessionId()) {
2917             (void) track->setSyncEvent(event);
2918             return NO_ERROR;
2919         }
2920     }
2921 
2922     return NAME_NOT_FOUND;
2923 }
2924 
isValidSyncEvent(const sp<SyncEvent> & event) const2925 bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
2926 {
2927     return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
2928 }
2929 
threadLoop_removeTracks(const Vector<sp<Track>> & tracksToRemove)2930 void AudioFlinger::PlaybackThread::threadLoop_removeTracks(
2931         const Vector< sp<Track> >& tracksToRemove)
2932 {
2933     // Miscellaneous track cleanup when removed from the active list,
2934     // called without Thread lock but synchronized with threadLoop processing.
2935 #ifdef ADD_BATTERY_DATA
2936     for (const auto& track : tracksToRemove) {
2937         if (track->isExternalTrack()) {
2938             // to track the speaker usage
2939             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
2940         }
2941     }
2942 #else
2943     (void)tracksToRemove; // suppress unused warning
2944 #endif
2945 }
2946 
checkSilentMode_l()2947 void AudioFlinger::PlaybackThread::checkSilentMode_l()
2948 {
2949     if (!mMasterMute) {
2950         char value[PROPERTY_VALUE_MAX];
2951         if (isSingleDeviceType(outDeviceTypes(), AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) {
2952             ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX");
2953             return;
2954         }
2955         if (property_get("ro.audio.silent", value, "0") > 0) {
2956             char *endptr;
2957             unsigned long ul = strtoul(value, &endptr, 0);
2958             if (*endptr == '\0' && ul != 0) {
2959                 ALOGD("Silence is golden");
2960                 // The setprop command will not allow a property to be changed after
2961                 // the first time it is set, so we don't have to worry about un-muting.
2962                 setMasterMute_l(true);
2963             }
2964         }
2965     }
2966 }
2967 
2968 // shared by MIXER and DIRECT, overridden by DUPLICATING
threadLoop_write()2969 ssize_t AudioFlinger::PlaybackThread::threadLoop_write()
2970 {
2971     LOG_HIST_TS();
2972     mInWrite = true;
2973     ssize_t bytesWritten;
2974     const size_t offset = mCurrentWriteLength - mBytesRemaining;
2975 
2976     // If an NBAIO sink is present, use it to write the normal mixer's submix
2977     if (mNormalSink != 0) {
2978 
2979         const size_t count = mBytesRemaining / mFrameSize;
2980 
2981         ATRACE_BEGIN("write");
2982         // update the setpoint when AudioFlinger::mScreenState changes
2983         uint32_t screenState = AudioFlinger::mScreenState;
2984         if (screenState != mScreenState) {
2985             mScreenState = screenState;
2986             MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
2987             if (pipe != NULL) {
2988                 pipe->setAvgFrames((mScreenState & 1) ?
2989                         (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
2990             }
2991         }
2992         ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
2993         ATRACE_END();
2994         if (framesWritten > 0) {
2995             bytesWritten = framesWritten * mFrameSize;
2996 #ifdef TEE_SINK
2997             mTee.write((char *)mSinkBuffer + offset, framesWritten);
2998 #endif
2999         } else {
3000             bytesWritten = framesWritten;
3001         }
3002     // otherwise use the HAL / AudioStreamOut directly
3003     } else {
3004         // Direct output and offload threads
3005 
3006         if (mUseAsyncWrite) {
3007             ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
3008             mWriteAckSequence += 2;
3009             mWriteAckSequence |= 1;
3010             ALOG_ASSERT(mCallbackThread != 0);
3011             mCallbackThread->setWriteBlocked(mWriteAckSequence);
3012         }
3013         ATRACE_BEGIN("write");
3014         // FIXME We should have an implementation of timestamps for direct output threads.
3015         // They are used e.g for multichannel PCM playback over HDMI.
3016         bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
3017         ATRACE_END();
3018 
3019         if (mUseAsyncWrite &&
3020                 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
3021             // do not wait for async callback in case of error of full write
3022             mWriteAckSequence &= ~1;
3023             ALOG_ASSERT(mCallbackThread != 0);
3024             mCallbackThread->setWriteBlocked(mWriteAckSequence);
3025         }
3026     }
3027 
3028     mNumWrites++;
3029     mInWrite = false;
3030     mStandby = false;
3031     return bytesWritten;
3032 }
3033 
threadLoop_drain()3034 void AudioFlinger::PlaybackThread::threadLoop_drain()
3035 {
3036     bool supportsDrain = false;
3037     if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) {
3038         ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full");
3039         if (mUseAsyncWrite) {
3040             ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request");
3041             mDrainSequence |= 1;
3042             ALOG_ASSERT(mCallbackThread != 0);
3043             mCallbackThread->setDraining(mDrainSequence);
3044         }
3045         status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK);
3046         ALOGE_IF(result != OK, "Error when draining stream: %d", result);
3047     }
3048 }
3049 
threadLoop_exit()3050 void AudioFlinger::PlaybackThread::threadLoop_exit()
3051 {
3052     {
3053         Mutex::Autolock _l(mLock);
3054         for (size_t i = 0; i < mTracks.size(); i++) {
3055             sp<Track> track = mTracks[i];
3056             track->invalidate();
3057         }
3058         // Clear ActiveTracks to update BatteryNotifier in case active tracks remain.
3059         // After we exit there are no more track changes sent to BatteryNotifier
3060         // because that requires an active threadLoop.
3061         // TODO: should we decActiveTrackCnt() of the cleared track effect chain?
3062         mActiveTracks.clear();
3063     }
3064 }
3065 
3066 /*
3067 The derived values that are cached:
3068  - mSinkBufferSize from frame count * frame size
3069  - mActiveSleepTimeUs from activeSleepTimeUs()
3070  - mIdleSleepTimeUs from idleSleepTimeUs()
3071  - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least
3072    kDefaultStandbyTimeInNsecs when connected to an A2DP device.
3073  - maxPeriod from frame count and sample rate (MIXER only)
3074 
3075 The parameters that affect these derived values are:
3076  - frame count
3077  - frame size
3078  - sample rate
3079  - device type: A2DP or not
3080  - device latency
3081  - format: PCM or not
3082  - active sleep time
3083  - idle sleep time
3084 */
3085 
cacheParameters_l()3086 void AudioFlinger::PlaybackThread::cacheParameters_l()
3087 {
3088     mSinkBufferSize = mNormalFrameCount * mFrameSize;
3089     mActiveSleepTimeUs = activeSleepTimeUs();
3090     mIdleSleepTimeUs = idleSleepTimeUs();
3091 
3092     // make sure standby delay is not too short when connected to an A2DP sink to avoid
3093     // truncating audio when going to standby.
3094     mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs;
3095     if (!Intersection(outDeviceTypes(),  getAudioDeviceOutAllA2dpSet()).empty()) {
3096         if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) {
3097             mStandbyDelayNs = kDefaultStandbyTimeInNsecs;
3098         }
3099     }
3100 }
3101 
invalidateTracks_l(audio_stream_type_t streamType)3102 bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType)
3103 {
3104     ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu",
3105             this,  streamType, mTracks.size());
3106     bool trackMatch = false;
3107     size_t size = mTracks.size();
3108     for (size_t i = 0; i < size; i++) {
3109         sp<Track> t = mTracks[i];
3110         if (t->streamType() == streamType && t->isExternalTrack()) {
3111             t->invalidate();
3112             trackMatch = true;
3113         }
3114     }
3115     return trackMatch;
3116 }
3117 
invalidateTracks(audio_stream_type_t streamType)3118 void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
3119 {
3120     Mutex::Autolock _l(mLock);
3121     invalidateTracks_l(streamType);
3122 }
3123 
addEffectChain_l(const sp<EffectChain> & chain)3124 status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
3125 {
3126     audio_session_t session = chain->sessionId();
3127     sp<EffectBufferHalInterface> halInBuffer, halOutBuffer;
3128     status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer(
3129             mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer,
3130             mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize,
3131             &halInBuffer);
3132     if (result != OK) return result;
3133     halOutBuffer = halInBuffer;
3134     effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData());
3135     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
3136     if (!audio_is_global_session(session)) {
3137         // Only one effect chain can be present in direct output thread and it uses
3138         // the sink buffer as input
3139         if (mType != DIRECT) {
3140             size_t numSamples = mNormalFrameCount * (mChannelCount + mHapticChannelCount);
3141             status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
3142                     numSamples * sizeof(effect_buffer_t),
3143                     &halInBuffer);
3144             if (result != OK) return result;
3145 #ifdef FLOAT_EFFECT_CHAIN
3146             buffer = halInBuffer->audioBuffer()->f32;
3147 #else
3148             buffer = halInBuffer->audioBuffer()->s16;
3149 #endif
3150             ALOGV("addEffectChain_l() creating new input buffer %p session %d",
3151                     buffer, session);
3152         }
3153 
3154         // Attach all tracks with same session ID to this chain.
3155         for (size_t i = 0; i < mTracks.size(); ++i) {
3156             sp<Track> track = mTracks[i];
3157             if (session == track->sessionId()) {
3158                 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(),
3159                         buffer);
3160                 track->setMainBuffer(buffer);
3161                 chain->incTrackCnt();
3162             }
3163         }
3164 
3165         // indicate all active tracks in the chain
3166         for (const sp<Track> &track : mActiveTracks) {
3167             if (session == track->sessionId()) {
3168                 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
3169                 chain->incActiveTrackCnt();
3170             }
3171         }
3172     }
3173     chain->setThread(this);
3174     chain->setInBuffer(halInBuffer);
3175     chain->setOutBuffer(halOutBuffer);
3176     // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect
3177     // chains list in order to be processed last as it contains output device effects.
3178     // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post
3179     // processing effects specific to an output stream before effects applied to all streams
3180     // routed to a given device.
3181     // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
3182     // session AUDIO_SESSION_OUTPUT_STAGE to be processed
3183     // after track specific effects and before output stage.
3184     // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
3185     // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX.
3186     // Effect chain for other sessions are inserted at beginning of effect
3187     // chains list to be processed before output mix effects. Relative order between other
3188     // sessions is not important.
3189     static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 &&
3190             AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX &&
3191             AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE,
3192             "audio_session_t constants misdefined");
3193     size_t size = mEffectChains.size();
3194     size_t i = 0;
3195     for (i = 0; i < size; i++) {
3196         if (mEffectChains[i]->sessionId() < session) {
3197             break;
3198         }
3199     }
3200     mEffectChains.insertAt(chain, i);
3201     checkSuspendOnAddEffectChain_l(chain);
3202 
3203     return NO_ERROR;
3204 }
3205 
removeEffectChain_l(const sp<EffectChain> & chain)3206 size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
3207 {
3208     audio_session_t session = chain->sessionId();
3209 
3210     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
3211 
3212     for (size_t i = 0; i < mEffectChains.size(); i++) {
3213         if (chain == mEffectChains[i]) {
3214             mEffectChains.removeAt(i);
3215             // detach all active tracks from the chain
3216             for (const sp<Track> &track : mActiveTracks) {
3217                 if (session == track->sessionId()) {
3218                     ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
3219                             chain.get(), session);
3220                     chain->decActiveTrackCnt();
3221                 }
3222             }
3223 
3224             // detach all tracks with same session ID from this chain
3225             for (size_t i = 0; i < mTracks.size(); ++i) {
3226                 sp<Track> track = mTracks[i];
3227                 if (session == track->sessionId()) {
3228                     track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer));
3229                     chain->decTrackCnt();
3230                 }
3231             }
3232             break;
3233         }
3234     }
3235     return mEffectChains.size();
3236 }
3237 
attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> & track,int EffectId)3238 status_t AudioFlinger::PlaybackThread::attachAuxEffect(
3239         const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId)
3240 {
3241     Mutex::Autolock _l(mLock);
3242     return attachAuxEffect_l(track, EffectId);
3243 }
3244 
attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> & track,int EffectId)3245 status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
3246         const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId)
3247 {
3248     status_t status = NO_ERROR;
3249 
3250     if (EffectId == 0) {
3251         track->setAuxBuffer(0, NULL);
3252     } else {
3253         // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
3254         sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
3255         if (effect != 0) {
3256             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3257                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
3258             } else {
3259                 status = INVALID_OPERATION;
3260             }
3261         } else {
3262             status = BAD_VALUE;
3263         }
3264     }
3265     return status;
3266 }
3267 
detachAuxEffect_l(int effectId)3268 void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
3269 {
3270     for (size_t i = 0; i < mTracks.size(); ++i) {
3271         sp<Track> track = mTracks[i];
3272         if (track->auxEffectId() == effectId) {
3273             attachAuxEffect_l(track, 0);
3274         }
3275     }
3276 }
3277 
threadLoop()3278 bool AudioFlinger::PlaybackThread::threadLoop()
3279 {
3280     tlNBLogWriter = mNBLogWriter.get();
3281 
3282     Vector< sp<Track> > tracksToRemove;
3283 
3284     mStandbyTimeNs = systemTime();
3285     int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0.
3286     int64_t lastFramesWritten = -1;    // track changes in timestamp server frames written
3287 
3288     // MIXER
3289     nsecs_t lastWarning = 0;
3290 
3291     // DUPLICATING
3292     // FIXME could this be made local to while loop?
3293     writeFrames = 0;
3294 
3295     cacheParameters_l();
3296     mSleepTimeUs = mIdleSleepTimeUs;
3297 
3298     if (mType == MIXER) {
3299         sleepTimeShift = 0;
3300     }
3301 
3302     CpuStats cpuStats;
3303     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
3304 
3305     acquireWakeLock();
3306 
3307     // mNBLogWriter logging APIs can only be called by a single thread, typically the
3308     // thread associated with this PlaybackThread.
3309     // If you want to share the mNBLogWriter with other threads (for example, binder threads)
3310     // then all such threads must agree to hold a common mutex before logging.
3311     // So if you need to log when mutex is unlocked, set logString to a non-NULL string,
3312     // and then that string will be logged at the next convenient opportunity.
3313     // See reference to logString below.
3314     const char *logString = NULL;
3315 
3316     // Estimated time for next buffer to be written to hal. This is used only on
3317     // suspended mode (for now) to help schedule the wait time until next iteration.
3318     nsecs_t timeLoopNextNs = 0;
3319 
3320     checkSilentMode_l();
3321 
3322     // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
3323     // TODO: add confirmation checks:
3324     // 1) DIRECT threads and linear PCM format really resets to 0?
3325     // 2) Is frame count really valid if not linear pcm?
3326     // 3) Are all 64 bits of position returned, not just lowest 32 bits?
3327     if (mType == OFFLOAD || mType == DIRECT) {
3328         mTimestampVerifier.setDiscontinuityMode(mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
3329     }
3330     audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3331 
3332     // loopCount is used for statistics and diagnostics.
3333     for (int64_t loopCount = 0; !exitPending(); ++loopCount)
3334     {
3335         // Log merge requests are performed during AudioFlinger binder transactions, but
3336         // that does not cover audio playback. It's requested here for that reason.
3337         mAudioFlinger->requestLogMerge();
3338 
3339         cpuStats.sample(myName);
3340 
3341         Vector< sp<EffectChain> > effectChains;
3342         audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE;
3343         std::vector<sp<Track>> activeTracks;
3344 
3345         // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency.
3346         //
3347         // Note: we access outDeviceTypes() outside of mLock.
3348         if (isMsdDevice() && outDeviceTypes().count(AUDIO_DEVICE_OUT_BUS) != 0) {
3349             // Here, we try for the AF lock, but do not block on it as the latency
3350             // is more informational.
3351             if (mAudioFlinger->mLock.tryLock() == NO_ERROR) {
3352                 std::vector<PatchPanel::SoftwarePatch> swPatches;
3353                 double latencyMs;
3354                 status_t status = INVALID_OPERATION;
3355                 audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3356                 if (mAudioFlinger->mPatchPanel.getDownstreamSoftwarePatches(id(), &swPatches) == OK
3357                         && swPatches.size() > 0) {
3358                         status = swPatches[0].getLatencyMs_l(&latencyMs);
3359                         downstreamPatchHandle = swPatches[0].getPatchHandle();
3360                 }
3361                 if (downstreamPatchHandle != lastDownstreamPatchHandle) {
3362                     mDownstreamLatencyStatMs.reset();
3363                     lastDownstreamPatchHandle = downstreamPatchHandle;
3364                 }
3365                 if (status == OK) {
3366                     // verify downstream latency (we assume a max reasonable
3367                     // latency of 5 seconds).
3368                     const double minLatency = 0., maxLatency = 5000.;
3369                     if (latencyMs >= minLatency && latencyMs <= maxLatency) {
3370                         ALOGVV("new downstream latency %lf ms", latencyMs);
3371                     } else {
3372                         ALOGD("out of range downstream latency %lf ms", latencyMs);
3373                         if (latencyMs < minLatency) latencyMs = minLatency;
3374                         else if (latencyMs > maxLatency) latencyMs = maxLatency;
3375                     }
3376                     mDownstreamLatencyStatMs.add(latencyMs);
3377                 }
3378                 mAudioFlinger->mLock.unlock();
3379             }
3380         } else {
3381             if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
3382                 // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats.
3383                 mDownstreamLatencyStatMs.reset();
3384                 lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3385             }
3386         }
3387 
3388         { // scope for mLock
3389 
3390             Mutex::Autolock _l(mLock);
3391 
3392             processConfigEvents_l();
3393 
3394             // See comment at declaration of logString for why this is done under mLock
3395             if (logString != NULL) {
3396                 mNBLogWriter->logTimestamp();
3397                 mNBLogWriter->log(logString);
3398                 logString = NULL;
3399             }
3400 
3401             // Collect timestamp statistics for the Playback Thread types that support it.
3402             if (mType == MIXER
3403                     || mType == DUPLICATING
3404                     || mType == DIRECT
3405                     || mType == OFFLOAD) { // no indentation
3406             // Gather the framesReleased counters for all active tracks,
3407             // and associate with the sink frames written out.  We need
3408             // this to convert the sink timestamp to the track timestamp.
3409             bool kernelLocationUpdate = false;
3410             ExtendedTimestamp timestamp; // use private copy to fetch
3411             if (mStandby) {
3412                 mTimestampVerifier.discontinuity();
3413             } else if (threadloop_getHalTimestamp_l(&timestamp) == OK) {
3414                 mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
3415                         timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3416                         mSampleRate);
3417 
3418                 if (isTimestampCorrectionEnabled()) {
3419                     ALOGVV("TS_BEFORE: %d %lld %lld", id(),
3420                             (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3421                             (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
3422                     auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
3423                     timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3424                             = correctedTimestamp.mFrames;
3425                     timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
3426                             = correctedTimestamp.mTimeNs;
3427                     ALOGVV("TS_AFTER: %d %lld %lld", id(),
3428                             (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3429                             (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
3430 
3431                     // Note: Downstream latency only added if timestamp correction enabled.
3432                     if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info.
3433                         const int64_t newPosition =
3434                                 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3435                                 - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
3436                         // prevent retrograde
3437                         timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max(
3438                                 newPosition,
3439                                 (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3440                                         - mSuspendedFrames));
3441                     }
3442                 }
3443 
3444                 // We always fetch the timestamp here because often the downstream
3445                 // sink will block while writing.
3446 
3447                 // We keep track of the last valid kernel position in case we are in underrun
3448                 // and the normal mixer period is the same as the fast mixer period, or there
3449                 // is some error from the HAL.
3450                 if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
3451                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
3452                             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
3453                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
3454                             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
3455 
3456                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
3457                             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER];
3458                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
3459                             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER];
3460                 }
3461 
3462                 if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
3463                     kernelLocationUpdate = true;
3464                 } else {
3465                     ALOGVV("getTimestamp error - no valid kernel position");
3466                 }
3467 
3468                 // copy over kernel info
3469                 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
3470                         timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3471                         + mSuspendedFrames; // add frames discarded when suspended
3472                 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
3473                         timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
3474             } else {
3475                 mTimestampVerifier.error();
3476             }
3477 
3478             // mFramesWritten for non-offloaded tracks are contiguous
3479             // even after standby() is called. This is useful for the track frame
3480             // to sink frame mapping.
3481             bool serverLocationUpdate = false;
3482             if (mFramesWritten != lastFramesWritten) {
3483                 serverLocationUpdate = true;
3484                 lastFramesWritten = mFramesWritten;
3485             }
3486             // Only update timestamps if there is a meaningful change.
3487             // Either the kernel timestamp must be valid or we have written something.
3488             if (kernelLocationUpdate || serverLocationUpdate) {
3489                 if (serverLocationUpdate) {
3490                     // use the time before we called the HAL write - it is a bit more accurate
3491                     // to when the server last read data than the current time here.
3492                     //
3493                     // If we haven't written anything, mLastIoBeginNs will be -1
3494                     // and we use systemTime().
3495                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten;
3496                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1
3497                             ? systemTime() : mLastIoBeginNs;
3498                 }
3499 
3500                 for (const sp<Track> &t : mActiveTracks) {
3501                     if (!t->isFastTrack()) {
3502                         t->updateTrackFrameInfo(
3503                                 t->mAudioTrackServerProxy->framesReleased(),
3504                                 mFramesWritten,
3505                                 mSampleRate,
3506                                 mTimestamp);
3507                     }
3508                 }
3509             }
3510 
3511             if (audio_has_proportional_frames(mFormat)) {
3512                 const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate);
3513                 if (latencyMs != 0.) { // note 0. means timestamp is empty.
3514                     mLatencyMs.add(latencyMs);
3515                 }
3516             }
3517 
3518             } // if (mType ... ) { // no indentation
3519 #if 0
3520             // logFormat example
3521             if (z % 100 == 0) {
3522                 timespec ts;
3523                 clock_gettime(CLOCK_MONOTONIC, &ts);
3524                 LOGT("This is an integer %d, this is a float %f, this is my "
3525                     "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts);
3526                 LOGT("A deceptive null-terminated string %\0");
3527             }
3528             ++z;
3529 #endif
3530             saveOutputTracks();
3531             if (mSignalPending) {
3532                 // A signal was raised while we were unlocked
3533                 mSignalPending = false;
3534             } else if (waitingAsyncCallback_l()) {
3535                 if (exitPending()) {
3536                     break;
3537                 }
3538                 bool released = false;
3539                 if (!keepWakeLock()) {
3540                     releaseWakeLock_l();
3541                     released = true;
3542                 }
3543 
3544                 const int64_t waitNs = computeWaitTimeNs_l();
3545                 ALOGV("wait async completion (wait time: %lld)", (long long)waitNs);
3546                 status_t status = mWaitWorkCV.waitRelative(mLock, waitNs);
3547                 if (status == TIMED_OUT) {
3548                     mSignalPending = true; // if timeout recheck everything
3549                 }
3550                 ALOGV("async completion/wake");
3551                 if (released) {
3552                     acquireWakeLock_l();
3553                 }
3554                 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
3555                 mSleepTimeUs = 0;
3556 
3557                 continue;
3558             }
3559             if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) ||
3560                                    isSuspended()) {
3561                 // put audio hardware into standby after short delay
3562                 if (shouldStandby_l()) {
3563 
3564                     threadLoop_standby();
3565 
3566                     // This is where we go into standby
3567                     if (!mStandby) {
3568                         LOG_AUDIO_STATE();
3569                     }
3570                     mStandby = true;
3571                     sendStatistics(false /* force */);
3572                 }
3573 
3574                 if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) {
3575                     // we're about to wait, flush the binder command buffer
3576                     IPCThreadState::self()->flushCommands();
3577 
3578                     clearOutputTracks();
3579 
3580                     if (exitPending()) {
3581                         break;
3582                     }
3583 
3584                     releaseWakeLock_l();
3585                     // wait until we have something to do...
3586                     ALOGV("%s going to sleep", myName.string());
3587                     mWaitWorkCV.wait(mLock);
3588                     ALOGV("%s waking up", myName.string());
3589                     acquireWakeLock_l();
3590 
3591                     mMixerStatus = MIXER_IDLE;
3592                     mMixerStatusIgnoringFastTracks = MIXER_IDLE;
3593                     mBytesWritten = 0;
3594                     mBytesRemaining = 0;
3595                     checkSilentMode_l();
3596 
3597                     mStandbyTimeNs = systemTime() + mStandbyDelayNs;
3598                     mSleepTimeUs = mIdleSleepTimeUs;
3599                     if (mType == MIXER) {
3600                         sleepTimeShift = 0;
3601                     }
3602 
3603                     continue;
3604                 }
3605             }
3606             // mMixerStatusIgnoringFastTracks is also updated internally
3607             mMixerStatus = prepareTracks_l(&tracksToRemove);
3608 
3609             mActiveTracks.updatePowerState(this);
3610 
3611             updateMetadata_l();
3612 
3613             // prevent any changes in effect chain list and in each effect chain
3614             // during mixing and effect process as the audio buffers could be deleted
3615             // or modified if an effect is created or deleted
3616             lockEffectChains_l(effectChains);
3617 
3618             // Determine which session to pick up haptic data.
3619             // This must be done under the same lock as prepareTracks_l().
3620             // TODO: Write haptic data directly to sink buffer when mixing.
3621             if (mHapticChannelCount > 0 && effectChains.size() > 0) {
3622                 for (const auto& track : mActiveTracks) {
3623                     if (track->getHapticPlaybackEnabled()) {
3624                         activeHapticSessionId = track->sessionId();
3625                         break;
3626                     }
3627                 }
3628             }
3629 
3630             // Acquire a local copy of active tracks with lock (release w/o lock).
3631             //
3632             // Control methods on the track acquire the ThreadBase lock (e.g. start()
3633             // stop(), pause(), etc.), but the threadLoop is entitled to call audio
3634             // data / buffer methods on tracks from activeTracks without the ThreadBase lock.
3635             activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end());
3636         } // mLock scope ends
3637 
3638         if (mBytesRemaining == 0) {
3639             mCurrentWriteLength = 0;
3640             if (mMixerStatus == MIXER_TRACKS_READY) {
3641                 // threadLoop_mix() sets mCurrentWriteLength
3642                 threadLoop_mix();
3643             } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
3644                         && (mMixerStatus != MIXER_DRAIN_ALL)) {
3645                 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data
3646                 // must be written to HAL
3647                 threadLoop_sleepTime();
3648                 if (mSleepTimeUs == 0) {
3649                     mCurrentWriteLength = mSinkBufferSize;
3650 
3651                     // Tally underrun frames as we are inserting 0s here.
3652                     for (const auto& track : activeTracks) {
3653                         if (track->mFillingUpStatus == Track::FS_ACTIVE) {
3654                             track->mAudioTrackServerProxy->tallyUnderrunFrames(mNormalFrameCount);
3655                         }
3656                     }
3657                 }
3658             }
3659             // Either threadLoop_mix() or threadLoop_sleepTime() should have set
3660             // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0.
3661             // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
3662             // or mSinkBuffer (if there are no effects).
3663             //
3664             // This is done pre-effects computation; if effects change to
3665             // support higher precision, this needs to move.
3666             //
3667             // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
3668             // TODO use mSleepTimeUs == 0 as an additional condition.
3669             if (mMixerBufferValid) {
3670                 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
3671                 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
3672 
3673                 // mono blend occurs for mixer threads only (not direct or offloaded)
3674                 // and is handled here if we're going directly to the sink.
3675                 if (requireMonoBlend() && !mEffectBufferValid) {
3676                     mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount,
3677                                true /*limit*/);
3678                 }
3679 
3680                 if (!hasFastMixer()) {
3681                     // Balance must take effect after mono conversion.
3682                     // We do it here if there is no FastMixer.
3683                     // mBalance detects zero balance within the class for speed (not needed here).
3684                     mBalance.setBalance(mMasterBalance.load());
3685                     mBalance.process((float *)mMixerBuffer, mNormalFrameCount);
3686                 }
3687 
3688                 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
3689                         mNormalFrameCount * (mChannelCount + mHapticChannelCount));
3690 
3691                 // If we're going directly to the sink and there are haptic channels,
3692                 // we should adjust channels as the sample data is partially interleaved
3693                 // in this case.
3694                 if (!mEffectBufferValid && mHapticChannelCount > 0) {
3695                     adjust_channels_non_destructive(buffer, mChannelCount, buffer,
3696                             mChannelCount + mHapticChannelCount,
3697                             audio_bytes_per_sample(format),
3698                             audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount);
3699                 }
3700             }
3701 
3702             mBytesRemaining = mCurrentWriteLength;
3703             if (isSuspended()) {
3704                 // Simulate write to HAL when suspended (e.g. BT SCO phone call).
3705                 mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer.
3706                 const size_t framesRemaining = mBytesRemaining / mFrameSize;
3707                 mBytesWritten += mBytesRemaining;
3708                 mFramesWritten += framesRemaining;
3709                 mSuspendedFrames += framesRemaining; // to adjust kernel HAL position
3710                 mBytesRemaining = 0;
3711             }
3712 
3713             // only process effects if we're going to write
3714             if (mSleepTimeUs == 0 && mType != OFFLOAD) {
3715                 for (size_t i = 0; i < effectChains.size(); i ++) {
3716                     effectChains[i]->process_l();
3717                     // TODO: Write haptic data directly to sink buffer when mixing.
3718                     if (activeHapticSessionId != AUDIO_SESSION_NONE
3719                             && activeHapticSessionId == effectChains[i]->sessionId()) {
3720                         // Haptic data is active in this case, copy it directly from
3721                         // in buffer to out buffer.
3722                         const size_t audioBufferSize = mNormalFrameCount
3723                                 * audio_bytes_per_frame(mChannelCount, EFFECT_BUFFER_FORMAT);
3724                         memcpy_by_audio_format(
3725                                 (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize,
3726                                 EFFECT_BUFFER_FORMAT,
3727                                 (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize,
3728                                 EFFECT_BUFFER_FORMAT, mNormalFrameCount * mHapticChannelCount);
3729                     }
3730                 }
3731             }
3732         }
3733         // Process effect chains for offloaded thread even if no audio
3734         // was read from audio track: process only updates effect state
3735         // and thus does have to be synchronized with audio writes but may have
3736         // to be called while waiting for async write callback
3737         if (mType == OFFLOAD) {
3738             for (size_t i = 0; i < effectChains.size(); i ++) {
3739                 effectChains[i]->process_l();
3740             }
3741         }
3742 
3743         // Only if the Effects buffer is enabled and there is data in the
3744         // Effects buffer (buffer valid), we need to
3745         // copy into the sink buffer.
3746         // TODO use mSleepTimeUs == 0 as an additional condition.
3747         if (mEffectBufferValid) {
3748             //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
3749 
3750             if (requireMonoBlend()) {
3751                 mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount,
3752                            true /*limit*/);
3753             }
3754 
3755             if (!hasFastMixer()) {
3756                 // Balance must take effect after mono conversion.
3757                 // We do it here if there is no FastMixer.
3758                 // mBalance detects zero balance within the class for speed (not needed here).
3759                 mBalance.setBalance(mMasterBalance.load());
3760                 mBalance.process((float *)mEffectBuffer, mNormalFrameCount);
3761             }
3762 
3763             memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat,
3764                     mNormalFrameCount * (mChannelCount + mHapticChannelCount));
3765             // The sample data is partially interleaved when haptic channels exist,
3766             // we need to adjust channels here.
3767             if (mHapticChannelCount > 0) {
3768                 adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer,
3769                         mChannelCount + mHapticChannelCount,
3770                         audio_bytes_per_sample(mFormat),
3771                         audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount);
3772             }
3773         }
3774 
3775         // enable changes in effect chain
3776         unlockEffectChains(effectChains);
3777 
3778         if (!waitingAsyncCallback()) {
3779             // mSleepTimeUs == 0 means we must write to audio hardware
3780             if (mSleepTimeUs == 0) {
3781                 ssize_t ret = 0;
3782                 // writePeriodNs is updated >= 0 when ret > 0.
3783                 int64_t writePeriodNs = -1;
3784                 if (mBytesRemaining) {
3785                     // FIXME rewrite to reduce number of system calls
3786                     const int64_t lastIoBeginNs = systemTime();
3787                     ret = threadLoop_write();
3788                     const int64_t lastIoEndNs = systemTime();
3789                     if (ret < 0) {
3790                         mBytesRemaining = 0;
3791                     } else if (ret > 0) {
3792                         mBytesWritten += ret;
3793                         mBytesRemaining -= ret;
3794                         const int64_t frames = ret / mFrameSize;
3795                         mFramesWritten += frames;
3796 
3797                         writePeriodNs = lastIoEndNs - mLastIoEndNs;
3798                         // process information relating to write time.
3799                         if (audio_has_proportional_frames(mFormat)) {
3800                             // we are in a continuous mixing cycle
3801                             if (mMixerStatus == MIXER_TRACKS_READY &&
3802                                     loopCount == lastLoopCountWritten + 1) {
3803 
3804                                 const double jitterMs =
3805                                         TimestampVerifier<int64_t, int64_t>::computeJitterMs(
3806                                                 {frames, writePeriodNs},
3807                                                 {0, 0} /* lastTimestamp */, mSampleRate);
3808                                 const double processMs =
3809                                        (lastIoBeginNs - mLastIoEndNs) * 1e-6;
3810 
3811                                 Mutex::Autolock _l(mLock);
3812                                 mIoJitterMs.add(jitterMs);
3813                                 mProcessTimeMs.add(processMs);
3814                             }
3815 
3816                             // write blocked detection
3817                             const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs;
3818                             if (mType == MIXER && deltaWriteNs > maxPeriod) {
3819                                 mNumDelayedWrites++;
3820                                 if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) {
3821                                     ATRACE_NAME("underrun");
3822                                     ALOGW("write blocked for %lld msecs, "
3823                                             "%d delayed writes, thread %d",
3824                                             (long long)deltaWriteNs / NANOS_PER_MILLISECOND,
3825                                             mNumDelayedWrites, mId);
3826                                     lastWarning = lastIoEndNs;
3827                                 }
3828                             }
3829                         }
3830                         // update timing info.
3831                         mLastIoBeginNs = lastIoBeginNs;
3832                         mLastIoEndNs = lastIoEndNs;
3833                         lastLoopCountWritten = loopCount;
3834                     }
3835                 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
3836                         (mMixerStatus == MIXER_DRAIN_ALL)) {
3837                     threadLoop_drain();
3838                 }
3839                 if (mType == MIXER && !mStandby) {
3840 
3841                     if (mThreadThrottle
3842                             && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks)
3843                             && writePeriodNs > 0) {               // we have write period info
3844                         // Limit MixerThread data processing to no more than twice the
3845                         // expected processing rate.
3846                         //
3847                         // This helps prevent underruns with NuPlayer and other applications
3848                         // which may set up buffers that are close to the minimum size, or use
3849                         // deep buffers, and rely on a double-buffering sleep strategy to fill.
3850                         //
3851                         // The throttle smooths out sudden large data drains from the device,
3852                         // e.g. when it comes out of standby, which often causes problems with
3853                         // (1) mixer threads without a fast mixer (which has its own warm-up)
3854                         // (2) minimum buffer sized tracks (even if the track is full,
3855                         //     the app won't fill fast enough to handle the sudden draw).
3856                         //
3857                         // Total time spent in last processing cycle equals time spent in
3858                         // 1. threadLoop_write, as well as time spent in
3859                         // 2. threadLoop_mix (significant for heavy mixing, especially
3860                         //                    on low tier processors)
3861 
3862                         // it's OK if deltaMs is an overestimate.
3863 
3864                         const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND;
3865 
3866                         const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs;
3867                         if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) {
3868                             usleep(throttleMs * 1000);
3869                             // notify of throttle start on verbose log
3870                             ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs,
3871                                     "mixer(%p) throttle begin:"
3872                                     " ret(%zd) deltaMs(%d) requires sleep %d ms",
3873                                     this, ret, deltaMs, throttleMs);
3874                             mThreadThrottleTimeMs += throttleMs;
3875                             // Throttle must be attributed to the previous mixer loop's write time
3876                             // to allow back-to-back throttling.
3877                             // This also ensures proper timing statistics.
3878                             mLastIoEndNs = systemTime();  // we fetch the write end time again.
3879                         } else {
3880                             uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
3881                             if (diff > 0) {
3882                                 // notify of throttle end on debug log
3883                                 // but prevent spamming for bluetooth
3884                                 ALOGD_IF(!isSingleDeviceType(
3885                                                  outDeviceTypes(), audio_is_a2dp_out_device) &&
3886                                          !isSingleDeviceType(
3887                                                  outDeviceTypes(), audio_is_hearing_aid_out_device),
3888                                         "mixer(%p) throttle end: throttle time(%u)", this, diff);
3889                                 mThreadThrottleEndMs = mThreadThrottleTimeMs;
3890                             }
3891                         }
3892                     }
3893                 }
3894 
3895             } else {
3896                 ATRACE_BEGIN("sleep");
3897                 Mutex::Autolock _l(mLock);
3898                 // suspended requires accurate metering of sleep time.
3899                 if (isSuspended()) {
3900                     // advance by expected sleepTime
3901                     timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs);
3902                     const nsecs_t nowNs = systemTime();
3903 
3904                     // compute expected next time vs current time.
3905                     // (negative deltas are treated as delays).
3906                     nsecs_t deltaNs = timeLoopNextNs - nowNs;
3907                     if (deltaNs < -kMaxNextBufferDelayNs) {
3908                         // Delays longer than the max allowed trigger a reset.
3909                         ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs);
3910                         deltaNs = microseconds((nsecs_t)mSleepTimeUs);
3911                         timeLoopNextNs = nowNs + deltaNs;
3912                     } else if (deltaNs < 0) {
3913                         // Delays within the max delay allowed: zero the delta/sleepTime
3914                         // to help the system catch up in the next iteration(s)
3915                         ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs);
3916                         deltaNs = 0;
3917                     }
3918                     // update sleep time (which is >= 0)
3919                     mSleepTimeUs = deltaNs / 1000;
3920                 }
3921                 if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) {
3922                     mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs));
3923                 }
3924                 ATRACE_END();
3925             }
3926         }
3927 
3928         // Finally let go of removed track(s), without the lock held
3929         // since we can't guarantee the destructors won't acquire that
3930         // same lock.  This will also mutate and push a new fast mixer state.
3931         threadLoop_removeTracks(tracksToRemove);
3932         tracksToRemove.clear();
3933 
3934         // FIXME I don't understand the need for this here;
3935         //       it was in the original code but maybe the
3936         //       assignment in saveOutputTracks() makes this unnecessary?
3937         clearOutputTracks();
3938 
3939         // Effect chains will be actually deleted here if they were removed from
3940         // mEffectChains list during mixing or effects processing
3941         effectChains.clear();
3942 
3943         // FIXME Note that the above .clear() is no longer necessary since effectChains
3944         // is now local to this block, but will keep it for now (at least until merge done).
3945     }
3946 
3947     threadLoop_exit();
3948 
3949     if (!mStandby) {
3950         threadLoop_standby();
3951         mStandby = true;
3952     }
3953 
3954     releaseWakeLock();
3955 
3956     ALOGV("Thread %p type %d exiting", this, mType);
3957     return false;
3958 }
3959 
3960 // removeTracks_l() must be called with ThreadBase::mLock held
removeTracks_l(const Vector<sp<Track>> & tracksToRemove)3961 void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove)
3962 {
3963     for (const auto& track : tracksToRemove) {
3964         mActiveTracks.remove(track);
3965         ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId());
3966         sp<EffectChain> chain = getEffectChain_l(track->sessionId());
3967         if (chain != 0) {
3968             ALOGV("%s(%d): stopping track on chain %p for session Id: %d",
3969                     __func__, track->id(), chain.get(), track->sessionId());
3970             chain->decActiveTrackCnt();
3971         }
3972         // If an external client track, inform APM we're no longer active, and remove if needed.
3973         // We do this under lock so that the state is consistent if the Track is destroyed.
3974         if (track->isExternalTrack()) {
3975             AudioSystem::stopOutput(track->portId());
3976             if (track->isTerminated()) {
3977                 AudioSystem::releaseOutput(track->portId());
3978             }
3979         }
3980         if (track->isTerminated()) {
3981             // remove from our tracks vector
3982             removeTrack_l(track);
3983         }
3984         if ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
3985                 && mHapticChannelCount > 0) {
3986             mLock.unlock();
3987             // Unlock due to VibratorService will lock for this call and will
3988             // call Tracks.mute/unmute which also require thread's lock.
3989             AudioFlinger::onExternalVibrationStop(track->getExternalVibration());
3990             mLock.lock();
3991         }
3992     }
3993 }
3994 
getTimestamp_l(AudioTimestamp & timestamp)3995 status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp)
3996 {
3997     if (mNormalSink != 0) {
3998         ExtendedTimestamp ets;
3999         status_t status = mNormalSink->getTimestamp(ets);
4000         if (status == NO_ERROR) {
4001             status = ets.getBestTimestamp(&timestamp);
4002         }
4003         return status;
4004     }
4005     if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) {
4006         uint64_t position64;
4007         if (mOutput->getPresentationPosition(&position64, &timestamp.mTime) == OK) {
4008             timestamp.mPosition = (uint32_t)position64;
4009             if (mDownstreamLatencyStatMs.getN() > 0) {
4010                 const uint32_t positionOffset =
4011                     (uint32_t)(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
4012                 if (positionOffset > timestamp.mPosition) {
4013                     timestamp.mPosition = 0;
4014                 } else {
4015                     timestamp.mPosition -= positionOffset;
4016                 }
4017             }
4018             return NO_ERROR;
4019         }
4020     }
4021     return INVALID_OPERATION;
4022 }
4023 
4024 // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is
4025 // still applied by the mixer.
4026 // All tracks attached to a mixer with flag VOIP_RX are tied to the same
4027 // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even
4028 // if more than one track are active
handleVoipVolume_l(float * volume)4029 status_t AudioFlinger::PlaybackThread::handleVoipVolume_l(float *volume)
4030 {
4031     status_t result = NO_ERROR;
4032     if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) {
4033         if (*volume != mLeftVolFloat) {
4034             result = mOutput->stream->setVolume(*volume, *volume);
4035             ALOGE_IF(result != OK,
4036                      "Error when setting output stream volume: %d", result);
4037             if (result == NO_ERROR) {
4038                 mLeftVolFloat = *volume;
4039             }
4040         }
4041         // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we
4042         // remove stream volume contribution from software volume.
4043         if (mLeftVolFloat == *volume) {
4044             *volume = 1.0f;
4045         }
4046     }
4047     return result;
4048 }
4049 
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)4050 status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch,
4051                                                           audio_patch_handle_t *handle)
4052 {
4053     status_t status;
4054     if (property_get_bool("af.patch_park", false /* default_value */)) {
4055         // Park FastMixer to avoid potential DOS issues with writing to the HAL
4056         // or if HAL does not properly lock against access.
4057         AutoPark<FastMixer> park(mFastMixer);
4058         status = PlaybackThread::createAudioPatch_l(patch, handle);
4059     } else {
4060         status = PlaybackThread::createAudioPatch_l(patch, handle);
4061     }
4062     return status;
4063 }
4064 
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)4065 status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch,
4066                                                           audio_patch_handle_t *handle)
4067 {
4068     status_t status = NO_ERROR;
4069 
4070     // store new device and send to effects
4071     audio_devices_t type = AUDIO_DEVICE_NONE;
4072     AudioDeviceTypeAddrVector deviceTypeAddrs;
4073     for (unsigned int i = 0; i < patch->num_sinks; i++) {
4074         LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1
4075                             && !mOutput->audioHwDev->supportsAudioPatches(),
4076                             "Enumerated device type(%#x) must not be used "
4077                             "as it does not support audio patches",
4078                             patch->sinks[i].ext.device.type);
4079         type |= patch->sinks[i].ext.device.type;
4080         deviceTypeAddrs.push_back(AudioDeviceTypeAddr(patch->sinks[i].ext.device.type,
4081                 patch->sinks[i].ext.device.address));
4082     }
4083 
4084     audio_port_handle_t sinkPortId = patch->sinks[0].id;
4085 #ifdef ADD_BATTERY_DATA
4086     // when changing the audio output device, call addBatteryData to notify
4087     // the change
4088     if (outDeviceTypes() != deviceTypes) {
4089         uint32_t params = 0;
4090         // check whether speaker is on
4091         if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) {
4092             params |= IMediaPlayerService::kBatteryDataSpeakerOn;
4093         }
4094 
4095         // check if any other device (except speaker) is on
4096         if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) {
4097             params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
4098         }
4099 
4100         if (params != 0) {
4101             addBatteryData(params);
4102         }
4103     }
4104 #endif
4105 
4106     for (size_t i = 0; i < mEffectChains.size(); i++) {
4107         mEffectChains[i]->setDevices_l(deviceTypeAddrs);
4108     }
4109 
4110     // mPatch.num_sinks is not set when the thread is created so that
4111     // the first patch creation triggers an ioConfigChanged callback
4112     bool configChanged = (mPatch.num_sinks == 0) ||
4113                          (mPatch.sinks[0].id != sinkPortId);
4114     mPatch = *patch;
4115     mOutDeviceTypeAddrs = deviceTypeAddrs;
4116 
4117     if (mOutput->audioHwDev->supportsAudioPatches()) {
4118         sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
4119         status = hwDevice->createAudioPatch(patch->num_sources,
4120                                             patch->sources,
4121                                             patch->num_sinks,
4122                                             patch->sinks,
4123                                             handle);
4124     } else {
4125         char *address;
4126         if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
4127             //FIXME: we only support address on first sink with HAL version < 3.0
4128             address = audio_device_address_to_parameter(
4129                                                         patch->sinks[0].ext.device.type,
4130                                                         patch->sinks[0].ext.device.address);
4131         } else {
4132             address = (char *)calloc(1, 1);
4133         }
4134         AudioParameter param = AudioParameter(String8(address));
4135         free(address);
4136         param.addInt(String8(AudioParameter::keyRouting), (int)type);
4137         status = mOutput->stream->setParameters(param.toString());
4138         *handle = AUDIO_PATCH_HANDLE_NONE;
4139     }
4140     if (configChanged) {
4141         sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
4142     }
4143     return status;
4144 }
4145 
releaseAudioPatch_l(const audio_patch_handle_t handle)4146 status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
4147 {
4148     status_t status;
4149     if (property_get_bool("af.patch_park", false /* default_value */)) {
4150         // Park FastMixer to avoid potential DOS issues with writing to the HAL
4151         // or if HAL does not properly lock against access.
4152         AutoPark<FastMixer> park(mFastMixer);
4153         status = PlaybackThread::releaseAudioPatch_l(handle);
4154     } else {
4155         status = PlaybackThread::releaseAudioPatch_l(handle);
4156     }
4157     return status;
4158 }
4159 
releaseAudioPatch_l(const audio_patch_handle_t handle)4160 status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
4161 {
4162     status_t status = NO_ERROR;
4163 
4164     mPatch = audio_patch{};
4165     mOutDeviceTypeAddrs.clear();
4166 
4167     if (mOutput->audioHwDev->supportsAudioPatches()) {
4168         sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
4169         status = hwDevice->releaseAudioPatch(handle);
4170     } else {
4171         AudioParameter param;
4172         param.addInt(String8(AudioParameter::keyRouting), 0);
4173         status = mOutput->stream->setParameters(param.toString());
4174     }
4175     return status;
4176 }
4177 
addPatchTrack(const sp<PatchTrack> & track)4178 void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track)
4179 {
4180     Mutex::Autolock _l(mLock);
4181     mTracks.add(track);
4182 }
4183 
deletePatchTrack(const sp<PatchTrack> & track)4184 void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track)
4185 {
4186     Mutex::Autolock _l(mLock);
4187     destroyTrack_l(track);
4188 }
4189 
toAudioPortConfig(struct audio_port_config * config)4190 void AudioFlinger::PlaybackThread::toAudioPortConfig(struct audio_port_config *config)
4191 {
4192     ThreadBase::toAudioPortConfig(config);
4193     config->role = AUDIO_PORT_ROLE_SOURCE;
4194     config->ext.mix.hw_module = mOutput->audioHwDev->handle();
4195     config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
4196     if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
4197         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
4198         config->flags.output = mOutput->flags;
4199     }
4200 }
4201 
4202 // ----------------------------------------------------------------------------
4203 
MixerThread(const sp<AudioFlinger> & audioFlinger,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,type_t type)4204 AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
4205         audio_io_handle_t id, bool systemReady, type_t type)
4206     :   PlaybackThread(audioFlinger, output, id, type, systemReady),
4207         // mAudioMixer below
4208         // mFastMixer below
4209         mFastMixerFutex(0),
4210         mMasterMono(false)
4211         // mOutputSink below
4212         // mPipeSink below
4213         // mNormalSink below
4214 {
4215     setMasterBalance(audioFlinger->getMasterBalance_l());
4216     ALOGV("MixerThread() id=%d type=%d", id, type);
4217     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, "
4218             "mFrameCount=%zu, mNormalFrameCount=%zu",
4219             mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
4220             mNormalFrameCount);
4221     mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
4222 
4223     if (type == DUPLICATING) {
4224         // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks
4225         // (downstream MixerThreads) in DuplicatingThread::threadLoop_write().
4226         // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink.
4227         return;
4228     }
4229     // create an NBAIO sink for the HAL output stream, and negotiate
4230     mOutputSink = new AudioStreamOutSink(output->stream);
4231     size_t numCounterOffers = 0;
4232     const NBAIO_Format offers[1] = {Format_from_SR_C(
4233             mSampleRate, mChannelCount + mHapticChannelCount, mFormat)};
4234 #if !LOG_NDEBUG
4235     ssize_t index =
4236 #else
4237     (void)
4238 #endif
4239             mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
4240     ALOG_ASSERT(index == 0);
4241 
4242     // initialize fast mixer depending on configuration
4243     bool initFastMixer;
4244     switch (kUseFastMixer) {
4245     case FastMixer_Never:
4246         initFastMixer = false;
4247         break;
4248     case FastMixer_Always:
4249         initFastMixer = true;
4250         break;
4251     case FastMixer_Static:
4252     case FastMixer_Dynamic:
4253         // FastMixer was designed to operate with a HAL that pulls at a regular rate,
4254         // where the period is less than an experimentally determined threshold that can be
4255         // scheduled reliably with CFS. However, the BT A2DP HAL is
4256         // bursty (does not pull at a regular rate) and so cannot operate with FastMixer.
4257         initFastMixer = mFrameCount < mNormalFrameCount
4258                 && Intersection(outDeviceTypes(), getAudioDeviceOutAllA2dpSet()).empty();
4259         break;
4260     }
4261     ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount,
4262             "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu",
4263             mFrameCount, mNormalFrameCount);
4264     if (initFastMixer) {
4265         audio_format_t fastMixerFormat;
4266         if (mMixerBufferEnabled && mEffectBufferEnabled) {
4267             fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT;
4268         } else {
4269             fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
4270         }
4271         if (mFormat != fastMixerFormat) {
4272             // change our Sink format to accept our intermediate precision
4273             mFormat = fastMixerFormat;
4274             free(mSinkBuffer);
4275             mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat);
4276             const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
4277             (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
4278         }
4279 
4280         // create a MonoPipe to connect our submix to FastMixer
4281         NBAIO_Format format = mOutputSink->format();
4282 
4283         // adjust format to match that of the Fast Mixer
4284         ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat);
4285         format.mFormat = fastMixerFormat;
4286         format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount;
4287 
4288         // This pipe depth compensates for scheduling latency of the normal mixer thread.
4289         // When it wakes up after a maximum latency, it runs a few cycles quickly before
4290         // finally blocking.  Note the pipe implementation rounds up the request to a power of 2.
4291         MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
4292         const NBAIO_Format offers[1] = {format};
4293         size_t numCounterOffers = 0;
4294 #if !LOG_NDEBUG
4295         ssize_t index =
4296 #else
4297         (void)
4298 #endif
4299                 monoPipe->negotiate(offers, 1, NULL, numCounterOffers);
4300         ALOG_ASSERT(index == 0);
4301         monoPipe->setAvgFrames((mScreenState & 1) ?
4302                 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
4303         mPipeSink = monoPipe;
4304 
4305         // create fast mixer and configure it initially with just one fast track for our submix
4306         mFastMixer = new FastMixer(mId);
4307         FastMixerStateQueue *sq = mFastMixer->sq();
4308 #ifdef STATE_QUEUE_DUMP
4309         sq->setObserverDump(&mStateQueueObserverDump);
4310         sq->setMutatorDump(&mStateQueueMutatorDump);
4311 #endif
4312         FastMixerState *state = sq->begin();
4313         FastTrack *fastTrack = &state->mFastTracks[0];
4314         // wrap the source side of the MonoPipe to make it an AudioBufferProvider
4315         fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
4316         fastTrack->mVolumeProvider = NULL;
4317         fastTrack->mChannelMask = mChannelMask | mHapticChannelMask; // mPipeSink channel mask for
4318                                                                      // audio to FastMixer
4319         fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer
4320         fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE;
4321         fastTrack->mHapticIntensity = AudioMixer::HAPTIC_SCALE_NONE;
4322         fastTrack->mGeneration++;
4323         state->mFastTracksGen++;
4324         state->mTrackMask = 1;
4325         // fast mixer will use the HAL output sink
4326         state->mOutputSink = mOutputSink.get();
4327         state->mOutputSinkGen++;
4328         state->mFrameCount = mFrameCount;
4329         // specify sink channel mask when haptic channel mask present as it can not
4330         // be calculated directly from channel count
4331         state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE
4332                 ? AUDIO_CHANNEL_NONE : mChannelMask | mHapticChannelMask;
4333         state->mCommand = FastMixerState::COLD_IDLE;
4334         // already done in constructor initialization list
4335         //mFastMixerFutex = 0;
4336         state->mColdFutexAddr = &mFastMixerFutex;
4337         state->mColdGen++;
4338         state->mDumpState = &mFastMixerDumpState;
4339         mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer");
4340         state->mNBLogWriter = mFastMixerNBLogWriter.get();
4341         sq->end();
4342         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
4343 
4344         NBLog::thread_info_t info;
4345         info.id = mId;
4346         info.type = NBLog::FASTMIXER;
4347         mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info);
4348 
4349         // start the fast mixer
4350         mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
4351         pid_t tid = mFastMixer->getTid();
4352         sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
4353         stream()->setHalThreadPriority(kPriorityFastMixer);
4354 
4355 #ifdef AUDIO_WATCHDOG
4356         // create and start the watchdog
4357         mAudioWatchdog = new AudioWatchdog();
4358         mAudioWatchdog->setDump(&mAudioWatchdogDump);
4359         mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
4360         tid = mAudioWatchdog->getTid();
4361         sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
4362 #endif
4363     } else {
4364 #ifdef TEE_SINK
4365         // Only use the MixerThread tee if there is no FastMixer.
4366         mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
4367         mTee.setId(std::string("_") + std::to_string(mId) + "_M");
4368 #endif
4369     }
4370 
4371     switch (kUseFastMixer) {
4372     case FastMixer_Never:
4373     case FastMixer_Dynamic:
4374         mNormalSink = mOutputSink;
4375         break;
4376     case FastMixer_Always:
4377         mNormalSink = mPipeSink;
4378         break;
4379     case FastMixer_Static:
4380         mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
4381         break;
4382     }
4383 }
4384 
~MixerThread()4385 AudioFlinger::MixerThread::~MixerThread()
4386 {
4387     if (mFastMixer != 0) {
4388         FastMixerStateQueue *sq = mFastMixer->sq();
4389         FastMixerState *state = sq->begin();
4390         if (state->mCommand == FastMixerState::COLD_IDLE) {
4391             int32_t old = android_atomic_inc(&mFastMixerFutex);
4392             if (old == -1) {
4393                 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
4394             }
4395         }
4396         state->mCommand = FastMixerState::EXIT;
4397         sq->end();
4398         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
4399         mFastMixer->join();
4400         // Though the fast mixer thread has exited, it's state queue is still valid.
4401         // We'll use that extract the final state which contains one remaining fast track
4402         // corresponding to our sub-mix.
4403         state = sq->begin();
4404         ALOG_ASSERT(state->mTrackMask == 1);
4405         FastTrack *fastTrack = &state->mFastTracks[0];
4406         ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
4407         delete fastTrack->mBufferProvider;
4408         sq->end(false /*didModify*/);
4409         mFastMixer.clear();
4410 #ifdef AUDIO_WATCHDOG
4411         if (mAudioWatchdog != 0) {
4412             mAudioWatchdog->requestExit();
4413             mAudioWatchdog->requestExitAndWait();
4414             mAudioWatchdog.clear();
4415         }
4416 #endif
4417     }
4418     mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter);
4419     delete mAudioMixer;
4420 }
4421 
4422 
correctLatency_l(uint32_t latency) const4423 uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const
4424 {
4425     if (mFastMixer != 0) {
4426         MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
4427         latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
4428     }
4429     return latency;
4430 }
4431 
threadLoop_write()4432 ssize_t AudioFlinger::MixerThread::threadLoop_write()
4433 {
4434     // FIXME we should only do one push per cycle; confirm this is true
4435     // Start the fast mixer if it's not already running
4436     if (mFastMixer != 0) {
4437         FastMixerStateQueue *sq = mFastMixer->sq();
4438         FastMixerState *state = sq->begin();
4439         if (state->mCommand != FastMixerState::MIX_WRITE &&
4440                 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
4441             if (state->mCommand == FastMixerState::COLD_IDLE) {
4442 
4443                 // FIXME workaround for first HAL write being CPU bound on some devices
4444                 ATRACE_BEGIN("write");
4445                 mOutput->write((char *)mSinkBuffer, 0);
4446                 ATRACE_END();
4447 
4448                 int32_t old = android_atomic_inc(&mFastMixerFutex);
4449                 if (old == -1) {
4450                     (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
4451                 }
4452 #ifdef AUDIO_WATCHDOG
4453                 if (mAudioWatchdog != 0) {
4454                     mAudioWatchdog->resume();
4455                 }
4456 #endif
4457             }
4458             state->mCommand = FastMixerState::MIX_WRITE;
4459 #ifdef FAST_THREAD_STATISTICS
4460             mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
4461                 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN);
4462 #endif
4463             sq->end();
4464             sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
4465             if (kUseFastMixer == FastMixer_Dynamic) {
4466                 mNormalSink = mPipeSink;
4467             }
4468         } else {
4469             sq->end(false /*didModify*/);
4470         }
4471     }
4472     return PlaybackThread::threadLoop_write();
4473 }
4474 
threadLoop_standby()4475 void AudioFlinger::MixerThread::threadLoop_standby()
4476 {
4477     // Idle the fast mixer if it's currently running
4478     if (mFastMixer != 0) {
4479         FastMixerStateQueue *sq = mFastMixer->sq();
4480         FastMixerState *state = sq->begin();
4481         if (!(state->mCommand & FastMixerState::IDLE)) {
4482             // Report any frames trapped in the Monopipe
4483             MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get();
4484             const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite();
4485             mLocalLog.log("threadLoop_standby: framesWritten:%lld  suspendedFrames:%lld  "
4486                     "monoPipeWritten:%lld  monoPipeLeft:%lld",
4487                     (long long)mFramesWritten, (long long)mSuspendedFrames,
4488                     (long long)mPipeSink->framesWritten(), pipeFrames);
4489             mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str());
4490 
4491             state->mCommand = FastMixerState::COLD_IDLE;
4492             state->mColdFutexAddr = &mFastMixerFutex;
4493             state->mColdGen++;
4494             mFastMixerFutex = 0;
4495             sq->end();
4496             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
4497             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
4498             if (kUseFastMixer == FastMixer_Dynamic) {
4499                 mNormalSink = mOutputSink;
4500             }
4501 #ifdef AUDIO_WATCHDOG
4502             if (mAudioWatchdog != 0) {
4503                 mAudioWatchdog->pause();
4504             }
4505 #endif
4506         } else {
4507             sq->end(false /*didModify*/);
4508         }
4509     }
4510     PlaybackThread::threadLoop_standby();
4511 }
4512 
waitingAsyncCallback_l()4513 bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l()
4514 {
4515     return false;
4516 }
4517 
shouldStandby_l()4518 bool AudioFlinger::PlaybackThread::shouldStandby_l()
4519 {
4520     return !mStandby;
4521 }
4522 
waitingAsyncCallback()4523 bool AudioFlinger::PlaybackThread::waitingAsyncCallback()
4524 {
4525     Mutex::Autolock _l(mLock);
4526     return waitingAsyncCallback_l();
4527 }
4528 
4529 // shared by MIXER and DIRECT, overridden by DUPLICATING
threadLoop_standby()4530 void AudioFlinger::PlaybackThread::threadLoop_standby()
4531 {
4532     ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended);
4533     mOutput->standby();
4534     if (mUseAsyncWrite != 0) {
4535         // discard any pending drain or write ack by incrementing sequence
4536         mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
4537         mDrainSequence = (mDrainSequence + 2) & ~1;
4538         ALOG_ASSERT(mCallbackThread != 0);
4539         mCallbackThread->setWriteBlocked(mWriteAckSequence);
4540         mCallbackThread->setDraining(mDrainSequence);
4541     }
4542     mHwPaused = false;
4543 }
4544 
onAddNewTrack_l()4545 void AudioFlinger::PlaybackThread::onAddNewTrack_l()
4546 {
4547     ALOGV("signal playback thread");
4548     broadcast_l();
4549 }
4550 
onAsyncError()4551 void AudioFlinger::PlaybackThread::onAsyncError()
4552 {
4553     for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
4554         invalidateTracks((audio_stream_type_t)i);
4555     }
4556 }
4557 
threadLoop_mix()4558 void AudioFlinger::MixerThread::threadLoop_mix()
4559 {
4560     // mix buffers...
4561     mAudioMixer->process();
4562     mCurrentWriteLength = mSinkBufferSize;
4563     // increase sleep time progressively when application underrun condition clears.
4564     // Only increase sleep time if the mixer is ready for two consecutive times to avoid
4565     // that a steady state of alternating ready/not ready conditions keeps the sleep time
4566     // such that we would underrun the audio HAL.
4567     if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) {
4568         sleepTimeShift--;
4569     }
4570     mSleepTimeUs = 0;
4571     mStandbyTimeNs = systemTime() + mStandbyDelayNs;
4572     //TODO: delay standby when effects have a tail
4573 
4574 }
4575 
threadLoop_sleepTime()4576 void AudioFlinger::MixerThread::threadLoop_sleepTime()
4577 {
4578     // If no tracks are ready, sleep once for the duration of an output
4579     // buffer size, then write 0s to the output
4580     if (mSleepTimeUs == 0) {
4581         if (mMixerStatus == MIXER_TRACKS_ENABLED) {
4582             if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) {
4583                 // Using the Monopipe availableToWrite, we estimate the
4584                 // sleep time to retry for more data (before we underrun).
4585                 MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get());
4586                 const ssize_t availableToWrite = mPipeSink->availableToWrite();
4587                 const size_t pipeFrames = monoPipe->maxFrames();
4588                 const size_t framesLeft = pipeFrames - max(availableToWrite, 0);
4589                 // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount
4590                 const size_t framesDelay = std::min(
4591                         mNormalFrameCount, max(framesLeft / 2, mFrameCount));
4592                 ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu",
4593                         pipeFrames, framesLeft, framesDelay);
4594                 mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate;
4595             } else {
4596                 mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift;
4597                 if (mSleepTimeUs < kMinThreadSleepTimeUs) {
4598                     mSleepTimeUs = kMinThreadSleepTimeUs;
4599                 }
4600                 // reduce sleep time in case of consecutive application underruns to avoid
4601                 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
4602                 // duration we would end up writing less data than needed by the audio HAL if
4603                 // the condition persists.
4604                 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
4605                     sleepTimeShift++;
4606                 }
4607             }
4608         } else {
4609             mSleepTimeUs = mIdleSleepTimeUs;
4610         }
4611     } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
4612         // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared
4613         // before effects processing or output.
4614         if (mMixerBufferValid) {
4615             memset(mMixerBuffer, 0, mMixerBufferSize);
4616         } else {
4617             memset(mSinkBuffer, 0, mSinkBufferSize);
4618         }
4619         mSleepTimeUs = 0;
4620         ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED),
4621                 "anticipated start");
4622     }
4623     // TODO add standby time extension fct of effect tail
4624 }
4625 
4626 // prepareTracks_l() must be called with ThreadBase::mLock held
prepareTracks_l(Vector<sp<Track>> * tracksToRemove)4627 AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
4628         Vector< sp<Track> > *tracksToRemove)
4629 {
4630     // clean up deleted track ids in AudioMixer before allocating new tracks
4631     (void)mTracks.processDeletedTrackIds([this](int trackId) {
4632         // for each trackId, destroy it in the AudioMixer
4633         if (mAudioMixer->exists(trackId)) {
4634             mAudioMixer->destroy(trackId);
4635         }
4636     });
4637     mTracks.clearDeletedTrackIds();
4638 
4639     mixer_state mixerStatus = MIXER_IDLE;
4640     // find out which tracks need to be processed
4641     size_t count = mActiveTracks.size();
4642     size_t mixedTracks = 0;
4643     size_t tracksWithEffect = 0;
4644     // counts only _active_ fast tracks
4645     size_t fastTracks = 0;
4646     uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
4647 
4648     float masterVolume = mMasterVolume;
4649     bool masterMute = mMasterMute;
4650 
4651     if (masterMute) {
4652         masterVolume = 0;
4653     }
4654     // Delegate master volume control to effect in output mix effect chain if needed
4655     sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
4656     if (chain != 0) {
4657         uint32_t v = (uint32_t)(masterVolume * (1 << 24));
4658         chain->setVolume_l(&v, &v);
4659         masterVolume = (float)((v + (1 << 23)) >> 24);
4660         chain.clear();
4661     }
4662 
4663     // prepare a new state to push
4664     FastMixerStateQueue *sq = NULL;
4665     FastMixerState *state = NULL;
4666     bool didModify = false;
4667     FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
4668     bool coldIdle = false;
4669     if (mFastMixer != 0) {
4670         sq = mFastMixer->sq();
4671         state = sq->begin();
4672         coldIdle = state->mCommand == FastMixerState::COLD_IDLE;
4673     }
4674 
4675     mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found.
4676     mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found.
4677 
4678     // DeferredOperations handles statistics after setting mixerStatus.
4679     class DeferredOperations {
4680     public:
4681         DeferredOperations(mixer_state *mixerStatus)
4682             : mMixerStatus(mixerStatus) { }
4683 
4684         // when leaving scope, tally frames properly.
4685         ~DeferredOperations() {
4686             // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY)
4687             // because that is when the underrun occurs.
4688             // We do not distinguish between FastTracks and NormalTracks here.
4689             if (*mMixerStatus == MIXER_TRACKS_READY) {
4690                 for (const auto &underrun : mUnderrunFrames) {
4691                     underrun.first->mAudioTrackServerProxy->tallyUnderrunFrames(
4692                             underrun.second);
4693                 }
4694             }
4695         }
4696 
4697         // tallyUnderrunFrames() is called to update the track counters
4698         // with the number of underrun frames for a particular mixer period.
4699         // We defer tallying until we know the final mixer status.
4700         void tallyUnderrunFrames(sp<Track> track, size_t underrunFrames) {
4701             mUnderrunFrames.emplace_back(track, underrunFrames);
4702         }
4703 
4704     private:
4705         const mixer_state * const mMixerStatus;
4706         std::vector<std::pair<sp<Track>, size_t>> mUnderrunFrames;
4707     } deferredOperations(&mixerStatus); // implicit nested scope for variable capture
4708 
4709     bool noFastHapticTrack = true;
4710     for (size_t i=0 ; i<count ; i++) {
4711         const sp<Track> t = mActiveTracks[i];
4712 
4713         // this const just means the local variable doesn't change
4714         Track* const track = t.get();
4715 
4716         // process fast tracks
4717         if (track->isFastTrack()) {
4718             LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr,
4719                     "%s(%d): FastTrack(%d) present without FastMixer",
4720                      __func__, id(), track->id());
4721 
4722             if (track->getHapticPlaybackEnabled()) {
4723                 noFastHapticTrack = false;
4724             }
4725 
4726             // It's theoretically possible (though unlikely) for a fast track to be created
4727             // and then removed within the same normal mix cycle.  This is not a problem, as
4728             // the track never becomes active so it's fast mixer slot is never touched.
4729             // The converse, of removing an (active) track and then creating a new track
4730             // at the identical fast mixer slot within the same normal mix cycle,
4731             // is impossible because the slot isn't marked available until the end of each cycle.
4732             int j = track->mFastIndex;
4733             ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks);
4734             ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
4735             FastTrack *fastTrack = &state->mFastTracks[j];
4736 
4737             // Determine whether the track is currently in underrun condition,
4738             // and whether it had a recent underrun.
4739             FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
4740             FastTrackUnderruns underruns = ftDump->mUnderruns;
4741             uint32_t recentFull = (underruns.mBitFields.mFull -
4742                     track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK;
4743             uint32_t recentPartial = (underruns.mBitFields.mPartial -
4744                     track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK;
4745             uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
4746                     track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK;
4747             uint32_t recentUnderruns = recentPartial + recentEmpty;
4748             track->mObservedUnderruns = underruns;
4749             // don't count underruns that occur while stopping or pausing
4750             // or stopped which can occur when flush() is called while active
4751             size_t underrunFrames = 0;
4752             if (!(track->isStopping() || track->isPausing() || track->isStopped()) &&
4753                     recentUnderruns > 0) {
4754                 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun
4755                 underrunFrames = recentUnderruns * mFrameCount;
4756             }
4757             // Immediately account for FastTrack underruns.
4758             track->mAudioTrackServerProxy->tallyUnderrunFrames(underrunFrames);
4759 
4760             // This is similar to the state machine for normal tracks,
4761             // with a few modifications for fast tracks.
4762             bool isActive = true;
4763             switch (track->mState) {
4764             case TrackBase::STOPPING_1:
4765                 // track stays active in STOPPING_1 state until first underrun
4766                 if (recentUnderruns > 0 || track->isTerminated()) {
4767                     track->mState = TrackBase::STOPPING_2;
4768                 }
4769                 break;
4770             case TrackBase::PAUSING:
4771                 // ramp down is not yet implemented
4772                 track->setPaused();
4773                 break;
4774             case TrackBase::RESUMING:
4775                 // ramp up is not yet implemented
4776                 track->mState = TrackBase::ACTIVE;
4777                 break;
4778             case TrackBase::ACTIVE:
4779                 if (recentFull > 0 || recentPartial > 0) {
4780                     // track has provided at least some frames recently: reset retry count
4781                     track->mRetryCount = kMaxTrackRetries;
4782                 }
4783                 if (recentUnderruns == 0) {
4784                     // no recent underruns: stay active
4785                     break;
4786                 }
4787                 // there has recently been an underrun of some kind
4788                 if (track->sharedBuffer() == 0) {
4789                     // were any of the recent underruns "empty" (no frames available)?
4790                     if (recentEmpty == 0) {
4791                         // no, then ignore the partial underruns as they are allowed indefinitely
4792                         break;
4793                     }
4794                     // there has recently been an "empty" underrun: decrement the retry counter
4795                     if (--(track->mRetryCount) > 0) {
4796                         break;
4797                     }
4798                     // indicate to client process that the track was disabled because of underrun;
4799                     // it will then automatically call start() when data is available
4800                     track->disable();
4801                     // remove from active list, but state remains ACTIVE [confusing but true]
4802                     isActive = false;
4803                     break;
4804                 }
4805                 FALLTHROUGH_INTENDED;
4806             case TrackBase::STOPPING_2:
4807             case TrackBase::PAUSED:
4808             case TrackBase::STOPPED:
4809             case TrackBase::FLUSHED:   // flush() while active
4810                 // Check for presentation complete if track is inactive
4811                 // We have consumed all the buffers of this track.
4812                 // This would be incomplete if we auto-paused on underrun
4813                 {
4814                     uint32_t latency = 0;
4815                     status_t result = mOutput->stream->getLatency(&latency);
4816                     ALOGE_IF(result != OK,
4817                             "Error when retrieving output stream latency: %d", result);
4818                     size_t audioHALFrames = (latency * mSampleRate) / 1000;
4819                     int64_t framesWritten = mBytesWritten / mFrameSize;
4820                     if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) {
4821                         // track stays in active list until presentation is complete
4822                         break;
4823                     }
4824                 }
4825                 if (track->isStopping_2()) {
4826                     track->mState = TrackBase::STOPPED;
4827                 }
4828                 if (track->isStopped()) {
4829                     // Can't reset directly, as fast mixer is still polling this track
4830                     //   track->reset();
4831                     // So instead mark this track as needing to be reset after push with ack
4832                     resetMask |= 1 << i;
4833                 }
4834                 isActive = false;
4835                 break;
4836             case TrackBase::IDLE:
4837             default:
4838                 LOG_ALWAYS_FATAL("unexpected track state %d", track->mState);
4839             }
4840 
4841             if (isActive) {
4842                 // was it previously inactive?
4843                 if (!(state->mTrackMask & (1 << j))) {
4844                     ExtendedAudioBufferProvider *eabp = track;
4845                     VolumeProvider *vp = track;
4846                     fastTrack->mBufferProvider = eabp;
4847                     fastTrack->mVolumeProvider = vp;
4848                     fastTrack->mChannelMask = track->mChannelMask;
4849                     fastTrack->mFormat = track->mFormat;
4850                     fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
4851                     fastTrack->mHapticIntensity = track->getHapticIntensity();
4852                     fastTrack->mGeneration++;
4853                     state->mTrackMask |= 1 << j;
4854                     didModify = true;
4855                     // no acknowledgement required for newly active tracks
4856                 }
4857                 sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
4858                 float volume;
4859                 if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) {
4860                     volume = 0.f;
4861                 } else {
4862                     volume = masterVolume * mStreamTypes[track->streamType()].volume;
4863                 }
4864 
4865                 handleVoipVolume_l(&volume);
4866 
4867                 // cache the combined master volume and stream type volume for fast mixer; this
4868                 // lacks any synchronization or barrier so VolumeProvider may read a stale value
4869                 const float vh = track->getVolumeHandler()->getVolume(
4870                     proxy->framesReleased()).first;
4871                 volume *= vh;
4872                 track->mCachedVolume = volume;
4873                 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
4874                 float vlf = volume * float_from_gain(gain_minifloat_unpack_left(vlr));
4875                 float vrf = volume * float_from_gain(gain_minifloat_unpack_right(vlr));
4876 
4877                 track->setFinalVolume((vlf + vrf) / 2.f);
4878                 ++fastTracks;
4879             } else {
4880                 // was it previously active?
4881                 if (state->mTrackMask & (1 << j)) {
4882                     fastTrack->mBufferProvider = NULL;
4883                     fastTrack->mGeneration++;
4884                     state->mTrackMask &= ~(1 << j);
4885                     didModify = true;
4886                     // If any fast tracks were removed, we must wait for acknowledgement
4887                     // because we're about to decrement the last sp<> on those tracks.
4888                     block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
4889                 } else {
4890                     // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an
4891                     // AudioTrack may start (which may not be with a start() but with a write()
4892                     // after underrun) and immediately paused or released.  In that case the
4893                     // FastTrack state hasn't had time to update.
4894                     // TODO Remove the ALOGW when this theory is confirmed.
4895                     ALOGW("fast track %d should have been active; "
4896                             "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d",
4897                             j, track->mState, state->mTrackMask, recentUnderruns,
4898                             track->sharedBuffer() != 0);
4899                     // Since the FastMixer state already has the track inactive, do nothing here.
4900                 }
4901                 tracksToRemove->add(track);
4902                 // Avoids a misleading display in dumpsys
4903                 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL;
4904             }
4905             if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) {
4906                 fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
4907                 didModify = true;
4908             }
4909             continue;
4910         }
4911 
4912         {   // local variable scope to avoid goto warning
4913 
4914         audio_track_cblk_t* cblk = track->cblk();
4915 
4916         // The first time a track is added we wait
4917         // for all its buffers to be filled before processing it
4918         const int trackId = track->id();
4919 
4920         // if an active track doesn't exist in the AudioMixer, create it.
4921         // use the trackId as the AudioMixer name.
4922         if (!mAudioMixer->exists(trackId)) {
4923             status_t status = mAudioMixer->create(
4924                     trackId,
4925                     track->mChannelMask,
4926                     track->mFormat,
4927                     track->mSessionId);
4928             if (status != OK) {
4929                 ALOGW("%s(): AudioMixer cannot create track(%d)"
4930                         " mask %#x, format %#x, sessionId %d",
4931                         __func__, trackId,
4932                         track->mChannelMask, track->mFormat, track->mSessionId);
4933                 tracksToRemove->add(track);
4934                 track->invalidate(); // consider it dead.
4935                 continue;
4936             }
4937         }
4938 
4939         // make sure that we have enough frames to mix one full buffer.
4940         // enforce this condition only once to enable draining the buffer in case the client
4941         // app does not call stop() and relies on underrun to stop:
4942         // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
4943         // during last round
4944         size_t desiredFrames;
4945         const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate();
4946         AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate();
4947 
4948         desiredFrames = sourceFramesNeededWithTimestretch(
4949                 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed);
4950         // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed.
4951         // add frames already consumed but not yet released by the resampler
4952         // because mAudioTrackServerProxy->framesReady() will include these frames
4953         desiredFrames += mAudioMixer->getUnreleasedFrames(trackId);
4954 
4955         uint32_t minFrames = 1;
4956         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
4957                 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
4958             minFrames = desiredFrames;
4959         }
4960 
4961         size_t framesReady = track->framesReady();
4962         if (ATRACE_ENABLED()) {
4963             // I wish we had formatted trace names
4964             std::string traceName("nRdy");
4965             traceName += std::to_string(trackId);
4966             ATRACE_INT(traceName.c_str(), framesReady);
4967         }
4968         if ((framesReady >= minFrames) && track->isReady() &&
4969                 !track->isPaused() && !track->isTerminated())
4970         {
4971             ALOGVV("track(%d) s=%08x [OK] on thread %p", trackId, cblk->mServer, this);
4972 
4973             mixedTracks++;
4974 
4975             // track->mainBuffer() != mSinkBuffer or mMixerBuffer means
4976             // there is an effect chain connected to the track
4977             chain.clear();
4978             if (track->mainBuffer() != mSinkBuffer &&
4979                     track->mainBuffer() != mMixerBuffer) {
4980                 if (mEffectBufferEnabled) {
4981                     mEffectBufferValid = true; // Later can set directly.
4982                 }
4983                 chain = getEffectChain_l(track->sessionId());
4984                 // Delegate volume control to effect in track effect chain if needed
4985                 if (chain != 0) {
4986                     tracksWithEffect++;
4987                 } else {
4988                     ALOGW("prepareTracks_l(): track(%d) attached to effect but no chain found on "
4989                             "session %d",
4990                             trackId, track->sessionId());
4991                 }
4992             }
4993 
4994 
4995             int param = AudioMixer::VOLUME;
4996             if (track->mFillingUpStatus == Track::FS_FILLED) {
4997                 // no ramp for the first volume setting
4998                 track->mFillingUpStatus = Track::FS_ACTIVE;
4999                 if (track->mState == TrackBase::RESUMING) {
5000                     track->mState = TrackBase::ACTIVE;
5001                     // If a new track is paused immediately after start, do not ramp on resume.
5002                     if (cblk->mServer != 0) {
5003                         param = AudioMixer::RAMP_VOLUME;
5004                     }
5005                 }
5006                 mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
5007                 mLeftVolFloat = -1.0;
5008             // FIXME should not make a decision based on mServer
5009             } else if (cblk->mServer != 0) {
5010                 // If the track is stopped before the first frame was mixed,
5011                 // do not apply ramp
5012                 param = AudioMixer::RAMP_VOLUME;
5013             }
5014 
5015             // compute volume for this track
5016             uint32_t vl, vr;       // in U8.24 integer format
5017             float vlf, vrf, vaf;   // in [0.0, 1.0] float format
5018             // read original volumes with volume control
5019             float v = masterVolume * mStreamTypes[track->streamType()].volume;
5020             // Always fetch volumeshaper volume to ensure state is updated.
5021             const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
5022             const float vh = track->getVolumeHandler()->getVolume(
5023                     track->mAudioTrackServerProxy->framesReleased()).first;
5024 
5025             if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
5026                 v = 0;
5027             }
5028 
5029             handleVoipVolume_l(&v);
5030 
5031             if (track->isPausing()) {
5032                 vl = vr = 0;
5033                 vlf = vrf = vaf = 0.;
5034                 track->setPaused();
5035             } else {
5036                 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
5037                 vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
5038                 vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
5039                 // track volumes come from shared memory, so can't be trusted and must be clamped
5040                 if (vlf > GAIN_FLOAT_UNITY) {
5041                     ALOGV("Track left volume out of range: %.3g", vlf);
5042                     vlf = GAIN_FLOAT_UNITY;
5043                 }
5044                 if (vrf > GAIN_FLOAT_UNITY) {
5045                     ALOGV("Track right volume out of range: %.3g", vrf);
5046                     vrf = GAIN_FLOAT_UNITY;
5047                 }
5048                 // now apply the master volume and stream type volume and shaper volume
5049                 vlf *= v * vh;
5050                 vrf *= v * vh;
5051                 // assuming master volume and stream type volume each go up to 1.0,
5052                 // then derive vl and vr as U8.24 versions for the effect chain
5053                 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
5054                 vl = (uint32_t) (scaleto8_24 * vlf);
5055                 vr = (uint32_t) (scaleto8_24 * vrf);
5056                 // vl and vr are now in U8.24 format
5057                 uint16_t sendLevel = proxy->getSendLevel_U4_12();
5058                 // send level comes from shared memory and so may be corrupt
5059                 if (sendLevel > MAX_GAIN_INT) {
5060                     ALOGV("Track send level out of range: %04X", sendLevel);
5061                     sendLevel = MAX_GAIN_INT;
5062                 }
5063                 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
5064                 vaf = v * sendLevel * (1. / MAX_GAIN_INT);
5065             }
5066 
5067             track->setFinalVolume((vrf + vlf) / 2.f);
5068 
5069             // Delegate volume control to effect in track effect chain if needed
5070             if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
5071                 // Do not ramp volume if volume is controlled by effect
5072                 param = AudioMixer::VOLUME;
5073                 // Update remaining floating point volume levels
5074                 vlf = (float)vl / (1 << 24);
5075                 vrf = (float)vr / (1 << 24);
5076                 track->mHasVolumeController = true;
5077             } else {
5078                 // force no volume ramp when volume controller was just disabled or removed
5079                 // from effect chain to avoid volume spike
5080                 if (track->mHasVolumeController) {
5081                     param = AudioMixer::VOLUME;
5082                 }
5083                 track->mHasVolumeController = false;
5084             }
5085 
5086             // XXX: these things DON'T need to be done each time
5087             mAudioMixer->setBufferProvider(trackId, track);
5088             mAudioMixer->enable(trackId);
5089 
5090             mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf);
5091             mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf);
5092             mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf);
5093             mAudioMixer->setParameter(
5094                 trackId,
5095                 AudioMixer::TRACK,
5096                 AudioMixer::FORMAT, (void *)track->format());
5097             mAudioMixer->setParameter(
5098                 trackId,
5099                 AudioMixer::TRACK,
5100                 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
5101             mAudioMixer->setParameter(
5102                 trackId,
5103                 AudioMixer::TRACK,
5104                 AudioMixer::MIXER_CHANNEL_MASK,
5105                 (void *)(uintptr_t)(mChannelMask | mHapticChannelMask));
5106             // limit track sample rate to 2 x output sample rate, which changes at re-configuration
5107             uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX;
5108             uint32_t reqSampleRate = proxy->getSampleRate();
5109             if (reqSampleRate == 0) {
5110                 reqSampleRate = mSampleRate;
5111             } else if (reqSampleRate > maxSampleRate) {
5112                 reqSampleRate = maxSampleRate;
5113             }
5114             mAudioMixer->setParameter(
5115                 trackId,
5116                 AudioMixer::RESAMPLE,
5117                 AudioMixer::SAMPLE_RATE,
5118                 (void *)(uintptr_t)reqSampleRate);
5119 
5120             AudioPlaybackRate playbackRate = proxy->getPlaybackRate();
5121             mAudioMixer->setParameter(
5122                 trackId,
5123                 AudioMixer::TIMESTRETCH,
5124                 AudioMixer::PLAYBACK_RATE,
5125                 &playbackRate);
5126 
5127             /*
5128              * Select the appropriate output buffer for the track.
5129              *
5130              * Tracks with effects go into their own effects chain buffer
5131              * and from there into either mEffectBuffer or mSinkBuffer.
5132              *
5133              * Other tracks can use mMixerBuffer for higher precision
5134              * channel accumulation.  If this buffer is enabled
5135              * (mMixerBufferEnabled true), then selected tracks will accumulate
5136              * into it.
5137              *
5138              */
5139             if (mMixerBufferEnabled
5140                     && (track->mainBuffer() == mSinkBuffer
5141                             || track->mainBuffer() == mMixerBuffer)) {
5142                 mAudioMixer->setParameter(
5143                         trackId,
5144                         AudioMixer::TRACK,
5145                         AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
5146                 mAudioMixer->setParameter(
5147                         trackId,
5148                         AudioMixer::TRACK,
5149                         AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
5150                 // TODO: override track->mainBuffer()?
5151                 mMixerBufferValid = true;
5152             } else {
5153                 mAudioMixer->setParameter(
5154                         trackId,
5155                         AudioMixer::TRACK,
5156                         AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT);
5157                 mAudioMixer->setParameter(
5158                         trackId,
5159                         AudioMixer::TRACK,
5160                         AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
5161             }
5162             mAudioMixer->setParameter(
5163                 trackId,
5164                 AudioMixer::TRACK,
5165                 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
5166             mAudioMixer->setParameter(
5167                 trackId,
5168                 AudioMixer::TRACK,
5169                 AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled());
5170             mAudioMixer->setParameter(
5171                 trackId,
5172                 AudioMixer::TRACK,
5173                 AudioMixer::HAPTIC_INTENSITY, (void *)(uintptr_t)track->getHapticIntensity());
5174 
5175             // reset retry count
5176             track->mRetryCount = kMaxTrackRetries;
5177 
5178             // If one track is ready, set the mixer ready if:
5179             //  - the mixer was not ready during previous round OR
5180             //  - no other track is not ready
5181             if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
5182                     mixerStatus != MIXER_TRACKS_ENABLED) {
5183                 mixerStatus = MIXER_TRACKS_READY;
5184             }
5185         } else {
5186             size_t underrunFrames = 0;
5187             if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) {
5188                 ALOGV("track(%d) underrun,  framesReady(%zu) < framesDesired(%zd)",
5189                         trackId, framesReady, desiredFrames);
5190                 underrunFrames = desiredFrames;
5191             }
5192             deferredOperations.tallyUnderrunFrames(track, underrunFrames);
5193 
5194             // clear effect chain input buffer if an active track underruns to avoid sending
5195             // previous audio buffer again to effects
5196             chain = getEffectChain_l(track->sessionId());
5197             if (chain != 0) {
5198                 chain->clearInputBuffer();
5199             }
5200 
5201             ALOGVV("track(%d) s=%08x [NOT READY] on thread %p", trackId, cblk->mServer, this);
5202             if ((track->sharedBuffer() != 0) || track->isTerminated() ||
5203                     track->isStopped() || track->isPaused()) {
5204                 // We have consumed all the buffers of this track.
5205                 // Remove it from the list of active tracks.
5206                 // TODO: use actual buffer filling status instead of latency when available from
5207                 // audio HAL
5208                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000;
5209                 int64_t framesWritten = mBytesWritten / mFrameSize;
5210                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) {
5211                     if (track->isStopped()) {
5212                         track->reset();
5213                     }
5214                     tracksToRemove->add(track);
5215                 }
5216             } else {
5217                 // No buffers for this track. Give it a few chances to
5218                 // fill a buffer, then remove it from active list.
5219                 if (--(track->mRetryCount) <= 0) {
5220                     ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p",
5221                             trackId, this);
5222                     tracksToRemove->add(track);
5223                     // indicate to client process that the track was disabled because of underrun;
5224                     // it will then automatically call start() when data is available
5225                     track->disable();
5226                 // If one track is not ready, mark the mixer also not ready if:
5227                 //  - the mixer was ready during previous round OR
5228                 //  - no other track is ready
5229                 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
5230                                 mixerStatus != MIXER_TRACKS_READY) {
5231                     mixerStatus = MIXER_TRACKS_ENABLED;
5232                 }
5233             }
5234             mAudioMixer->disable(trackId);
5235         }
5236 
5237         }   // local variable scope to avoid goto warning
5238 
5239     }
5240 
5241     if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) {
5242         // When there is no fast track playing haptic and FastMixer exists,
5243         // enabling the first FastTrack, which provides mixed data from normal
5244         // tracks, to play haptic data.
5245         FastTrack *fastTrack = &state->mFastTracks[0];
5246         if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) {
5247             fastTrack->mHapticPlaybackEnabled = noFastHapticTrack;
5248             didModify = true;
5249         }
5250     }
5251 
5252     // Push the new FastMixer state if necessary
5253     bool pauseAudioWatchdog = false;
5254     if (didModify) {
5255         state->mFastTracksGen++;
5256         // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
5257         if (kUseFastMixer == FastMixer_Dynamic &&
5258                 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
5259             state->mCommand = FastMixerState::COLD_IDLE;
5260             state->mColdFutexAddr = &mFastMixerFutex;
5261             state->mColdGen++;
5262             mFastMixerFutex = 0;
5263             if (kUseFastMixer == FastMixer_Dynamic) {
5264                 mNormalSink = mOutputSink;
5265             }
5266             // If we go into cold idle, need to wait for acknowledgement
5267             // so that fast mixer stops doing I/O.
5268             block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
5269             pauseAudioWatchdog = true;
5270         }
5271     }
5272     if (sq != NULL) {
5273         sq->end(didModify);
5274         // No need to block if the FastMixer is in COLD_IDLE as the FastThread
5275         // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE
5276         // when bringing the output sink into standby.)
5277         //
5278         // We will get the latest FastMixer state when we come out of COLD_IDLE.
5279         //
5280         // This occurs with BT suspend when we idle the FastMixer with
5281         // active tracks, which may be added or removed.
5282         sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block);
5283     }
5284 #ifdef AUDIO_WATCHDOG
5285     if (pauseAudioWatchdog && mAudioWatchdog != 0) {
5286         mAudioWatchdog->pause();
5287     }
5288 #endif
5289 
5290     // Now perform the deferred reset on fast tracks that have stopped
5291     while (resetMask != 0) {
5292         size_t i = __builtin_ctz(resetMask);
5293         ALOG_ASSERT(i < count);
5294         resetMask &= ~(1 << i);
5295         sp<Track> track = mActiveTracks[i];
5296         ALOG_ASSERT(track->isFastTrack() && track->isStopped());
5297         track->reset();
5298     }
5299 
5300     // Track destruction may occur outside of threadLoop once it is removed from active tracks.
5301     // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if
5302     // it ceases to be active, to allow safe removal from the AudioMixer at the start
5303     // of prepareTracks_l(); this releases any outstanding buffer back to the track.
5304     // See also the implementation of destroyTrack_l().
5305     for (const auto &track : *tracksToRemove) {
5306         const int trackId = track->id();
5307         if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer.
5308             mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */);
5309         }
5310     }
5311 
5312     // remove all the tracks that need to be...
5313     removeTracks_l(*tracksToRemove);
5314 
5315     if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) {
5316         mEffectBufferValid = true;
5317     }
5318 
5319     if (mEffectBufferValid) {
5320         // as long as there are effects we should clear the effects buffer, to avoid
5321         // passing a non-clean buffer to the effect chain
5322         memset(mEffectBuffer, 0, mEffectBufferSize);
5323     }
5324     // sink or mix buffer must be cleared if all tracks are connected to an
5325     // effect chain as in this case the mixer will not write to the sink or mix buffer
5326     // and track effects will accumulate into it
5327     if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
5328             (mixedTracks == 0 && fastTracks > 0))) {
5329         // FIXME as a performance optimization, should remember previous zero status
5330         if (mMixerBufferValid) {
5331             memset(mMixerBuffer, 0, mMixerBufferSize);
5332             // TODO: In testing, mSinkBuffer below need not be cleared because
5333             // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer
5334             // after mixing.
5335             //
5336             // To enforce this guarantee:
5337             // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
5338             // (mixedTracks == 0 && fastTracks > 0))
5339             // must imply MIXER_TRACKS_READY.
5340             // Later, we may clear buffers regardless, and skip much of this logic.
5341         }
5342         // FIXME as a performance optimization, should remember previous zero status
5343         memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize);
5344     }
5345 
5346     // if any fast tracks, then status is ready
5347     mMixerStatusIgnoringFastTracks = mixerStatus;
5348     if (fastTracks > 0) {
5349         mixerStatus = MIXER_TRACKS_READY;
5350     }
5351     return mixerStatus;
5352 }
5353 
5354 // trackCountForUid_l() must be called with ThreadBase::mLock held
trackCountForUid_l(uid_t uid) const5355 uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) const
5356 {
5357     uint32_t trackCount = 0;
5358     for (size_t i = 0; i < mTracks.size() ; i++) {
5359         if (mTracks[i]->uid() == uid) {
5360             trackCount++;
5361         }
5362     }
5363     return trackCount;
5364 }
5365 
5366 // isTrackAllowed_l() must be called with ThreadBase::mLock held
isTrackAllowed_l(audio_channel_mask_t channelMask,audio_format_t format,audio_session_t sessionId,uid_t uid) const5367 bool AudioFlinger::MixerThread::isTrackAllowed_l(
5368         audio_channel_mask_t channelMask, audio_format_t format,
5369         audio_session_t sessionId, uid_t uid) const
5370 {
5371     if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) {
5372         return false;
5373     }
5374     // Check validity as we don't call AudioMixer::create() here.
5375     if (!mAudioMixer->isValidFormat(format)) {
5376         ALOGW("%s: invalid format: %#x", __func__, format);
5377         return false;
5378     }
5379     if (!mAudioMixer->isValidChannelMask(channelMask)) {
5380         ALOGW("%s: invalid channelMask: %#x", __func__, channelMask);
5381         return false;
5382     }
5383     return true;
5384 }
5385 
5386 // checkForNewParameter_l() must be called with ThreadBase::mLock held
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)5387 bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair,
5388                                                        status_t& status)
5389 {
5390     bool reconfig = false;
5391     bool a2dpDeviceChanged = false;
5392 
5393     status = NO_ERROR;
5394 
5395     AutoPark<FastMixer> park(mFastMixer);
5396 
5397     AudioParameter param = AudioParameter(keyValuePair);
5398     int value;
5399     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
5400         reconfig = true;
5401     }
5402     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
5403         if (!isValidPcmSinkFormat((audio_format_t) value)) {
5404             status = BAD_VALUE;
5405         } else {
5406             // no need to save value, since it's constant
5407             reconfig = true;
5408         }
5409     }
5410     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
5411         if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) {
5412             status = BAD_VALUE;
5413         } else {
5414             // no need to save value, since it's constant
5415             reconfig = true;
5416         }
5417     }
5418     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5419         // do not accept frame count changes if tracks are open as the track buffer
5420         // size depends on frame count and correct behavior would not be guaranteed
5421         // if frame count is changed after track creation
5422         if (!mTracks.isEmpty()) {
5423             status = INVALID_OPERATION;
5424         } else {
5425             reconfig = true;
5426         }
5427     }
5428     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
5429         LOG_FATAL("Should not set routing device in MixerThread");
5430     }
5431 
5432     if (status == NO_ERROR) {
5433         status = mOutput->stream->setParameters(keyValuePair);
5434         if (!mStandby && status == INVALID_OPERATION) {
5435             mOutput->standby();
5436             mStandby = true;
5437             mBytesWritten = 0;
5438             status = mOutput->stream->setParameters(keyValuePair);
5439         }
5440         if (status == NO_ERROR && reconfig) {
5441             readOutputParameters_l();
5442             delete mAudioMixer;
5443             mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
5444             for (const auto &track : mTracks) {
5445                 const int trackId = track->id();
5446                 status_t status = mAudioMixer->create(
5447                         trackId,
5448                         track->mChannelMask,
5449                         track->mFormat,
5450                         track->mSessionId);
5451                 ALOGW_IF(status != NO_ERROR,
5452                         "%s(): AudioMixer cannot create track(%d)"
5453                         " mask %#x, format %#x, sessionId %d",
5454                         __func__,
5455                         trackId, track->mChannelMask, track->mFormat, track->mSessionId);
5456             }
5457             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
5458         }
5459     }
5460 
5461     return reconfig || a2dpDeviceChanged;
5462 }
5463 
5464 
dumpInternals_l(int fd,const Vector<String16> & args)5465 void AudioFlinger::MixerThread::dumpInternals_l(int fd, const Vector<String16>& args)
5466 {
5467     PlaybackThread::dumpInternals_l(fd, args);
5468     dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs);
5469     dprintf(fd, "  AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str());
5470     dprintf(fd, "  Master mono: %s\n", mMasterMono ? "on" : "off");
5471     dprintf(fd, "  Master balance: %f (%s)\n", mMasterBalance.load(),
5472             (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance())
5473                             : mBalance.toString()).c_str());
5474     if (hasFastMixer()) {
5475         dprintf(fd, "  FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid());
5476 
5477         // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
5478         // while we are dumping it.  It may be inconsistent, but it won't mutate!
5479         // This is a large object so we place it on the heap.
5480         // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
5481         const std::unique_ptr<FastMixerDumpState> copy =
5482                 std::make_unique<FastMixerDumpState>(mFastMixerDumpState);
5483         copy->dump(fd);
5484 
5485 #ifdef STATE_QUEUE_DUMP
5486         // Similar for state queue
5487         StateQueueObserverDump observerCopy = mStateQueueObserverDump;
5488         observerCopy.dump(fd);
5489         StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
5490         mutatorCopy.dump(fd);
5491 #endif
5492 
5493 #ifdef AUDIO_WATCHDOG
5494         if (mAudioWatchdog != 0) {
5495             // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
5496             AudioWatchdogDump wdCopy = mAudioWatchdogDump;
5497             wdCopy.dump(fd);
5498         }
5499 #endif
5500 
5501     } else {
5502         dprintf(fd, "  No FastMixer\n");
5503     }
5504 }
5505 
idleSleepTimeUs() const5506 uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const
5507 {
5508     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
5509 }
5510 
suspendSleepTimeUs() const5511 uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const
5512 {
5513     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
5514 }
5515 
cacheParameters_l()5516 void AudioFlinger::MixerThread::cacheParameters_l()
5517 {
5518     PlaybackThread::cacheParameters_l();
5519 
5520     // FIXME: Relaxed timing because of a certain device that can't meet latency
5521     // Should be reduced to 2x after the vendor fixes the driver issue
5522     // increase threshold again due to low power audio mode. The way this warning
5523     // threshold is calculated and its usefulness should be reconsidered anyway.
5524     maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
5525 }
5526 
5527 // ----------------------------------------------------------------------------
5528 
DirectOutputThread(const sp<AudioFlinger> & audioFlinger,AudioStreamOut * output,audio_io_handle_t id,ThreadBase::type_t type,bool systemReady)5529 AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
5530         AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady)
5531     :   PlaybackThread(audioFlinger, output, id, type, systemReady)
5532 {
5533     setMasterBalance(audioFlinger->getMasterBalance_l());
5534 }
5535 
~DirectOutputThread()5536 AudioFlinger::DirectOutputThread::~DirectOutputThread()
5537 {
5538 }
5539 
dumpInternals_l(int fd,const Vector<String16> & args)5540 void AudioFlinger::DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args)
5541 {
5542     PlaybackThread::dumpInternals_l(fd, args);
5543     dprintf(fd, "  Master balance: %f  Left: %f  Right: %f\n",
5544             mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight);
5545 }
5546 
setMasterBalance(float balance)5547 void AudioFlinger::DirectOutputThread::setMasterBalance(float balance)
5548 {
5549     Mutex::Autolock _l(mLock);
5550     if (mMasterBalance != balance) {
5551         mMasterBalance.store(balance);
5552         mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight);
5553         broadcast_l();
5554     }
5555 }
5556 
processVolume_l(Track * track,bool lastTrack)5557 void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
5558 {
5559     float left, right;
5560 
5561     // Ensure volumeshaper state always advances even when muted.
5562     const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
5563     const auto [shaperVolume, shaperActive] = track->getVolumeHandler()->getVolume(
5564             proxy->framesReleased());
5565     mVolumeShaperActive = shaperActive;
5566 
5567     if (mMasterMute || mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
5568         left = right = 0;
5569     } else {
5570         float typeVolume = mStreamTypes[track->streamType()].volume;
5571         const float v = mMasterVolume * typeVolume * shaperVolume;
5572 
5573         gain_minifloat_packed_t vlr = proxy->getVolumeLR();
5574         left = float_from_gain(gain_minifloat_unpack_left(vlr));
5575         if (left > GAIN_FLOAT_UNITY) {
5576             left = GAIN_FLOAT_UNITY;
5577         }
5578         left *= v * mMasterBalanceLeft; // DirectOutputThread balance applied as track volume
5579         right = float_from_gain(gain_minifloat_unpack_right(vlr));
5580         if (right > GAIN_FLOAT_UNITY) {
5581             right = GAIN_FLOAT_UNITY;
5582         }
5583         right *= v * mMasterBalanceRight;
5584     }
5585 
5586     if (lastTrack) {
5587         track->setFinalVolume((left + right) / 2.f);
5588         if (left != mLeftVolFloat || right != mRightVolFloat) {
5589             mLeftVolFloat = left;
5590             mRightVolFloat = right;
5591 
5592             // Delegate volume control to effect in track effect chain if needed
5593             // only one effect chain can be present on DirectOutputThread, so if
5594             // there is one, the track is connected to it
5595             if (!mEffectChains.isEmpty()) {
5596                 // if effect chain exists, volume is handled by it.
5597                 // Convert volumes from float to 8.24
5598                 uint32_t vl = (uint32_t)(left * (1 << 24));
5599                 uint32_t vr = (uint32_t)(right * (1 << 24));
5600                 // Direct/Offload effect chains set output volume in setVolume_l().
5601                 (void)mEffectChains[0]->setVolume_l(&vl, &vr);
5602             } else {
5603                 // otherwise we directly set the volume.
5604                 setVolumeForOutput_l(left, right);
5605             }
5606         }
5607     }
5608 }
5609 
onAddNewTrack_l()5610 void AudioFlinger::DirectOutputThread::onAddNewTrack_l()
5611 {
5612     sp<Track> previousTrack = mPreviousTrack.promote();
5613     sp<Track> latestTrack = mActiveTracks.getLatest();
5614 
5615     if (previousTrack != 0 && latestTrack != 0) {
5616         if (mType == DIRECT) {
5617             if (previousTrack.get() != latestTrack.get()) {
5618                 mFlushPending = true;
5619             }
5620         } else /* mType == OFFLOAD */ {
5621             if (previousTrack->sessionId() != latestTrack->sessionId()) {
5622                 mFlushPending = true;
5623             }
5624         }
5625     } else if (previousTrack == 0) {
5626         // there could be an old track added back during track transition for direct
5627         // output, so always issues flush to flush data of the previous track if it
5628         // was already destroyed with HAL paused, then flush can resume the playback
5629         mFlushPending = true;
5630     }
5631     PlaybackThread::onAddNewTrack_l();
5632 }
5633 
prepareTracks_l(Vector<sp<Track>> * tracksToRemove)5634 AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
5635     Vector< sp<Track> > *tracksToRemove
5636 )
5637 {
5638     size_t count = mActiveTracks.size();
5639     mixer_state mixerStatus = MIXER_IDLE;
5640     bool doHwPause = false;
5641     bool doHwResume = false;
5642 
5643     // find out which tracks need to be processed
5644     for (const sp<Track> &t : mActiveTracks) {
5645         if (t->isInvalid()) {
5646             ALOGW("An invalidated track shouldn't be in active list");
5647             tracksToRemove->add(t);
5648             continue;
5649         }
5650 
5651         Track* const track = t.get();
5652 #ifdef VERY_VERY_VERBOSE_LOGGING
5653         audio_track_cblk_t* cblk = track->cblk();
5654 #endif
5655         // Only consider last track started for volume and mixer state control.
5656         // In theory an older track could underrun and restart after the new one starts
5657         // but as we only care about the transition phase between two tracks on a
5658         // direct output, it is not a problem to ignore the underrun case.
5659         sp<Track> l = mActiveTracks.getLatest();
5660         bool last = l.get() == track;
5661 
5662         if (track->isPausing()) {
5663             track->setPaused();
5664             if (mHwSupportsPause && last && !mHwPaused) {
5665                 doHwPause = true;
5666                 mHwPaused = true;
5667             }
5668         } else if (track->isFlushPending()) {
5669             track->flushAck();
5670             if (last) {
5671                 mFlushPending = true;
5672             }
5673         } else if (track->isResumePending()) {
5674             track->resumeAck();
5675             if (last) {
5676                 mLeftVolFloat = mRightVolFloat = -1.0;
5677                 if (mHwPaused) {
5678                     doHwResume = true;
5679                     mHwPaused = false;
5680                 }
5681             }
5682         }
5683 
5684         // The first time a track is added we wait
5685         // for all its buffers to be filled before processing it.
5686         // Allow draining the buffer in case the client
5687         // app does not call stop() and relies on underrun to stop:
5688         // hence the test on (track->mRetryCount > 1).
5689         // If retryCount<=1 then track is about to underrun and be removed.
5690         // Do not use a high threshold for compressed audio.
5691         uint32_t minFrames;
5692         if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()
5693             && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) {
5694             minFrames = mNormalFrameCount;
5695         } else {
5696             minFrames = 1;
5697         }
5698 
5699         const size_t framesReady = track->framesReady();
5700         const int trackId = track->id();
5701         if (ATRACE_ENABLED()) {
5702             std::string traceName("nRdy");
5703             traceName += std::to_string(trackId);
5704             ATRACE_INT(traceName.c_str(), framesReady);
5705         }
5706         if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() &&
5707                 !track->isStopping_2() && !track->isStopped())
5708         {
5709             ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer);
5710 
5711             if (track->mFillingUpStatus == Track::FS_FILLED) {
5712                 track->mFillingUpStatus = Track::FS_ACTIVE;
5713                 if (last) {
5714                     // make sure processVolume_l() will apply new volume even if 0
5715                     mLeftVolFloat = mRightVolFloat = -1.0;
5716                 }
5717                 if (!mHwSupportsPause) {
5718                     track->resumeAck();
5719                 }
5720             }
5721 
5722             // compute volume for this track
5723             processVolume_l(track, last);
5724             if (last) {
5725                 sp<Track> previousTrack = mPreviousTrack.promote();
5726                 if (previousTrack != 0) {
5727                     if (track != previousTrack.get()) {
5728                         // Flush any data still being written from last track
5729                         mBytesRemaining = 0;
5730                         // Invalidate previous track to force a seek when resuming.
5731                         previousTrack->invalidate();
5732                     }
5733                 }
5734                 mPreviousTrack = track;
5735 
5736                 // reset retry count
5737                 track->mRetryCount = kMaxTrackRetriesDirect;
5738                 mActiveTrack = t;
5739                 mixerStatus = MIXER_TRACKS_READY;
5740                 if (mHwPaused) {
5741                     doHwResume = true;
5742                     mHwPaused = false;
5743                 }
5744             }
5745         } else {
5746             // clear effect chain input buffer if the last active track started underruns
5747             // to avoid sending previous audio buffer again to effects
5748             if (!mEffectChains.isEmpty() && last) {
5749                 mEffectChains[0]->clearInputBuffer();
5750             }
5751             if (track->isStopping_1()) {
5752                 track->mState = TrackBase::STOPPING_2;
5753                 if (last && mHwPaused) {
5754                      doHwResume = true;
5755                      mHwPaused = false;
5756                  }
5757             }
5758             if ((track->sharedBuffer() != 0) || track->isStopped() ||
5759                     track->isStopping_2() || track->isPaused()) {
5760                 // We have consumed all the buffers of this track.
5761                 // Remove it from the list of active tracks.
5762                 size_t audioHALFrames;
5763                 if (audio_has_proportional_frames(mFormat)) {
5764                     audioHALFrames = (latency_l() * mSampleRate) / 1000;
5765                 } else {
5766                     audioHALFrames = 0;
5767                 }
5768 
5769                 int64_t framesWritten = mBytesWritten / mFrameSize;
5770                 if (mStandby || !last ||
5771                         track->presentationComplete(framesWritten, audioHALFrames) ||
5772                         track->isPaused() || mHwPaused) {
5773                     if (track->isStopping_2()) {
5774                         track->mState = TrackBase::STOPPED;
5775                     }
5776                     if (track->isStopped()) {
5777                         track->reset();
5778                     }
5779                     tracksToRemove->add(track);
5780                 }
5781             } else {
5782                 // No buffers for this track. Give it a few chances to
5783                 // fill a buffer, then remove it from active list.
5784                 // Only consider last track started for mixer state control
5785                 if (--(track->mRetryCount) <= 0) {
5786                     ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", trackId);
5787                     tracksToRemove->add(track);
5788                     // indicate to client process that the track was disabled because of underrun;
5789                     // it will then automatically call start() when data is available
5790                     track->disable();
5791                 } else if (last) {
5792                     ALOGW("pause because of UNDERRUN, framesReady = %zu,"
5793                             "minFrames = %u, mFormat = %#x",
5794                             framesReady, minFrames, mFormat);
5795                     mixerStatus = MIXER_TRACKS_ENABLED;
5796                     if (mHwSupportsPause && !mHwPaused && !mStandby) {
5797                         doHwPause = true;
5798                         mHwPaused = true;
5799                     }
5800                 }
5801             }
5802         }
5803     }
5804 
5805     // if an active track did not command a flush, check for pending flush on stopped tracks
5806     if (!mFlushPending) {
5807         for (size_t i = 0; i < mTracks.size(); i++) {
5808             if (mTracks[i]->isFlushPending()) {
5809                 mTracks[i]->flushAck();
5810                 mFlushPending = true;
5811             }
5812         }
5813     }
5814 
5815     // make sure the pause/flush/resume sequence is executed in the right order.
5816     // If a flush is pending and a track is active but the HW is not paused, force a HW pause
5817     // before flush and then resume HW. This can happen in case of pause/flush/resume
5818     // if resume is received before pause is executed.
5819     if (mHwSupportsPause && !mStandby &&
5820             (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
5821         status_t result = mOutput->stream->pause();
5822         ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
5823     }
5824     if (mFlushPending) {
5825         flushHw_l();
5826     }
5827     if (mHwSupportsPause && !mStandby && doHwResume) {
5828         status_t result = mOutput->stream->resume();
5829         ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
5830     }
5831     // remove all the tracks that need to be...
5832     removeTracks_l(*tracksToRemove);
5833 
5834     return mixerStatus;
5835 }
5836 
threadLoop_mix()5837 void AudioFlinger::DirectOutputThread::threadLoop_mix()
5838 {
5839     size_t frameCount = mFrameCount;
5840     int8_t *curBuf = (int8_t *)mSinkBuffer;
5841     // output audio to hardware
5842     while (frameCount) {
5843         AudioBufferProvider::Buffer buffer;
5844         buffer.frameCount = frameCount;
5845         status_t status = mActiveTrack->getNextBuffer(&buffer);
5846         if (status != NO_ERROR || buffer.raw == NULL) {
5847             // no need to pad with 0 for compressed audio
5848             if (audio_has_proportional_frames(mFormat)) {
5849                 memset(curBuf, 0, frameCount * mFrameSize);
5850             }
5851             break;
5852         }
5853         memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
5854         frameCount -= buffer.frameCount;
5855         curBuf += buffer.frameCount * mFrameSize;
5856         mActiveTrack->releaseBuffer(&buffer);
5857     }
5858     mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer;
5859     mSleepTimeUs = 0;
5860     mStandbyTimeNs = systemTime() + mStandbyDelayNs;
5861     mActiveTrack.clear();
5862 }
5863 
threadLoop_sleepTime()5864 void AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
5865 {
5866     // do not write to HAL when paused
5867     if (mHwPaused || (usesHwAvSync() && mStandby)) {
5868         mSleepTimeUs = mIdleSleepTimeUs;
5869         return;
5870     }
5871     if (mSleepTimeUs == 0) {
5872         if (mMixerStatus == MIXER_TRACKS_ENABLED) {
5873             mSleepTimeUs = mActiveSleepTimeUs;
5874         } else {
5875             mSleepTimeUs = mIdleSleepTimeUs;
5876         }
5877     } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) {
5878         memset(mSinkBuffer, 0, mFrameCount * mFrameSize);
5879         mSleepTimeUs = 0;
5880     }
5881 }
5882 
threadLoop_exit()5883 void AudioFlinger::DirectOutputThread::threadLoop_exit()
5884 {
5885     {
5886         Mutex::Autolock _l(mLock);
5887         for (size_t i = 0; i < mTracks.size(); i++) {
5888             if (mTracks[i]->isFlushPending()) {
5889                 mTracks[i]->flushAck();
5890                 mFlushPending = true;
5891             }
5892         }
5893         if (mFlushPending) {
5894             flushHw_l();
5895         }
5896     }
5897     PlaybackThread::threadLoop_exit();
5898 }
5899 
5900 // must be called with thread mutex locked
shouldStandby_l()5901 bool AudioFlinger::DirectOutputThread::shouldStandby_l()
5902 {
5903     bool trackPaused = false;
5904     bool trackStopped = false;
5905 
5906     if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) {
5907         return !mStandby;
5908     }
5909 
5910     // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack
5911     // after a timeout and we will enter standby then.
5912     if (mTracks.size() > 0) {
5913         trackPaused = mTracks[mTracks.size() - 1]->isPaused();
5914         trackStopped = mTracks[mTracks.size() - 1]->isStopped() ||
5915                            mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE;
5916     }
5917 
5918     return !mStandby && !(trackPaused || (mHwPaused && !trackStopped));
5919 }
5920 
5921 // checkForNewParameter_l() must be called with ThreadBase::mLock held
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)5922 bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair,
5923                                                               status_t& status)
5924 {
5925     bool reconfig = false;
5926     bool a2dpDeviceChanged = false;
5927 
5928     status = NO_ERROR;
5929 
5930     AudioParameter param = AudioParameter(keyValuePair);
5931     int value;
5932     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
5933         LOG_FATAL("Should not set routing device in DirectOutputThread");
5934     }
5935     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5936         // do not accept frame count changes if tracks are open as the track buffer
5937         // size depends on frame count and correct behavior would not be garantied
5938         // if frame count is changed after track creation
5939         if (!mTracks.isEmpty()) {
5940             status = INVALID_OPERATION;
5941         } else {
5942             reconfig = true;
5943         }
5944     }
5945     if (status == NO_ERROR) {
5946         status = mOutput->stream->setParameters(keyValuePair);
5947         if (!mStandby && status == INVALID_OPERATION) {
5948             mOutput->standby();
5949             mStandby = true;
5950             mBytesWritten = 0;
5951             status = mOutput->stream->setParameters(keyValuePair);
5952         }
5953         if (status == NO_ERROR && reconfig) {
5954             readOutputParameters_l();
5955             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
5956         }
5957     }
5958 
5959     return reconfig || a2dpDeviceChanged;
5960 }
5961 
activeSleepTimeUs() const5962 uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const
5963 {
5964     uint32_t time;
5965     if (audio_has_proportional_frames(mFormat)) {
5966         time = PlaybackThread::activeSleepTimeUs();
5967     } else {
5968         time = kDirectMinSleepTimeUs;
5969     }
5970     return time;
5971 }
5972 
idleSleepTimeUs() const5973 uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const
5974 {
5975     uint32_t time;
5976     if (audio_has_proportional_frames(mFormat)) {
5977         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
5978     } else {
5979         time = kDirectMinSleepTimeUs;
5980     }
5981     return time;
5982 }
5983 
suspendSleepTimeUs() const5984 uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const
5985 {
5986     uint32_t time;
5987     if (audio_has_proportional_frames(mFormat)) {
5988         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
5989     } else {
5990         time = kDirectMinSleepTimeUs;
5991     }
5992     return time;
5993 }
5994 
cacheParameters_l()5995 void AudioFlinger::DirectOutputThread::cacheParameters_l()
5996 {
5997     PlaybackThread::cacheParameters_l();
5998 
5999     // use shorter standby delay as on normal output to release
6000     // hardware resources as soon as possible
6001     // no delay on outputs with HW A/V sync
6002     if (usesHwAvSync()) {
6003         mStandbyDelayNs = 0;
6004     } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) {
6005         mStandbyDelayNs = kOffloadStandbyDelayNs;
6006     } else {
6007         mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2);
6008     }
6009 }
6010 
flushHw_l()6011 void AudioFlinger::DirectOutputThread::flushHw_l()
6012 {
6013     mOutput->flush();
6014     mHwPaused = false;
6015     mFlushPending = false;
6016     mTimestampVerifier.discontinuity(); // DIRECT and OFFLOADED flush resets frame count.
6017     mTimestamp.clear();
6018 }
6019 
computeWaitTimeNs_l() const6020 int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const {
6021     // If a VolumeShaper is active, we must wake up periodically to update volume.
6022     const int64_t NS_PER_MS = 1000000;
6023     return mVolumeShaperActive ?
6024             kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l();
6025 }
6026 
6027 // ----------------------------------------------------------------------------
6028 
AsyncCallbackThread(const wp<AudioFlinger::PlaybackThread> & playbackThread)6029 AudioFlinger::AsyncCallbackThread::AsyncCallbackThread(
6030         const wp<AudioFlinger::PlaybackThread>& playbackThread)
6031     :   Thread(false /*canCallJava*/),
6032         mPlaybackThread(playbackThread),
6033         mWriteAckSequence(0),
6034         mDrainSequence(0),
6035         mAsyncError(false)
6036 {
6037 }
6038 
~AsyncCallbackThread()6039 AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread()
6040 {
6041 }
6042 
onFirstRef()6043 void AudioFlinger::AsyncCallbackThread::onFirstRef()
6044 {
6045     run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO);
6046 }
6047 
threadLoop()6048 bool AudioFlinger::AsyncCallbackThread::threadLoop()
6049 {
6050     while (!exitPending()) {
6051         uint32_t writeAckSequence;
6052         uint32_t drainSequence;
6053         bool asyncError;
6054 
6055         {
6056             Mutex::Autolock _l(mLock);
6057             while (!((mWriteAckSequence & 1) ||
6058                      (mDrainSequence & 1) ||
6059                      mAsyncError ||
6060                      exitPending())) {
6061                 mWaitWorkCV.wait(mLock);
6062             }
6063 
6064             if (exitPending()) {
6065                 break;
6066             }
6067             ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d",
6068                   mWriteAckSequence, mDrainSequence);
6069             writeAckSequence = mWriteAckSequence;
6070             mWriteAckSequence &= ~1;
6071             drainSequence = mDrainSequence;
6072             mDrainSequence &= ~1;
6073             asyncError = mAsyncError;
6074             mAsyncError = false;
6075         }
6076         {
6077             sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote();
6078             if (playbackThread != 0) {
6079                 if (writeAckSequence & 1) {
6080                     playbackThread->resetWriteBlocked(writeAckSequence >> 1);
6081                 }
6082                 if (drainSequence & 1) {
6083                     playbackThread->resetDraining(drainSequence >> 1);
6084                 }
6085                 if (asyncError) {
6086                     playbackThread->onAsyncError();
6087                 }
6088             }
6089         }
6090     }
6091     return false;
6092 }
6093 
exit()6094 void AudioFlinger::AsyncCallbackThread::exit()
6095 {
6096     ALOGV("AsyncCallbackThread::exit");
6097     Mutex::Autolock _l(mLock);
6098     requestExit();
6099     mWaitWorkCV.broadcast();
6100 }
6101 
setWriteBlocked(uint32_t sequence)6102 void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence)
6103 {
6104     Mutex::Autolock _l(mLock);
6105     // bit 0 is cleared
6106     mWriteAckSequence = sequence << 1;
6107 }
6108 
resetWriteBlocked()6109 void AudioFlinger::AsyncCallbackThread::resetWriteBlocked()
6110 {
6111     Mutex::Autolock _l(mLock);
6112     // ignore unexpected callbacks
6113     if (mWriteAckSequence & 2) {
6114         mWriteAckSequence |= 1;
6115         mWaitWorkCV.signal();
6116     }
6117 }
6118 
setDraining(uint32_t sequence)6119 void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence)
6120 {
6121     Mutex::Autolock _l(mLock);
6122     // bit 0 is cleared
6123     mDrainSequence = sequence << 1;
6124 }
6125 
resetDraining()6126 void AudioFlinger::AsyncCallbackThread::resetDraining()
6127 {
6128     Mutex::Autolock _l(mLock);
6129     // ignore unexpected callbacks
6130     if (mDrainSequence & 2) {
6131         mDrainSequence |= 1;
6132         mWaitWorkCV.signal();
6133     }
6134 }
6135 
setAsyncError()6136 void AudioFlinger::AsyncCallbackThread::setAsyncError()
6137 {
6138     Mutex::Autolock _l(mLock);
6139     mAsyncError = true;
6140     mWaitWorkCV.signal();
6141 }
6142 
6143 
6144 // ----------------------------------------------------------------------------
OffloadThread(const sp<AudioFlinger> & audioFlinger,AudioStreamOut * output,audio_io_handle_t id,bool systemReady)6145 AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger,
6146         AudioStreamOut* output, audio_io_handle_t id, bool systemReady)
6147     :   DirectOutputThread(audioFlinger, output, id, OFFLOAD, systemReady),
6148         mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true),
6149         mOffloadUnderrunPosition(~0LL)
6150 {
6151     //FIXME: mStandby should be set to true by ThreadBase constructo
6152     mStandby = true;
6153     mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */);
6154 }
6155 
threadLoop_exit()6156 void AudioFlinger::OffloadThread::threadLoop_exit()
6157 {
6158     if (mFlushPending || mHwPaused) {
6159         // If a flush is pending or track was paused, just discard buffered data
6160         flushHw_l();
6161     } else {
6162         mMixerStatus = MIXER_DRAIN_ALL;
6163         threadLoop_drain();
6164     }
6165     if (mUseAsyncWrite) {
6166         ALOG_ASSERT(mCallbackThread != 0);
6167         mCallbackThread->exit();
6168     }
6169     PlaybackThread::threadLoop_exit();
6170 }
6171 
prepareTracks_l(Vector<sp<Track>> * tracksToRemove)6172 AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l(
6173     Vector< sp<Track> > *tracksToRemove
6174 )
6175 {
6176     size_t count = mActiveTracks.size();
6177 
6178     mixer_state mixerStatus = MIXER_IDLE;
6179     bool doHwPause = false;
6180     bool doHwResume = false;
6181 
6182     ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count);
6183 
6184     // find out which tracks need to be processed
6185     for (const sp<Track> &t : mActiveTracks) {
6186         Track* const track = t.get();
6187 #ifdef VERY_VERY_VERBOSE_LOGGING
6188         audio_track_cblk_t* cblk = track->cblk();
6189 #endif
6190         // Only consider last track started for volume and mixer state control.
6191         // In theory an older track could underrun and restart after the new one starts
6192         // but as we only care about the transition phase between two tracks on a
6193         // direct output, it is not a problem to ignore the underrun case.
6194         sp<Track> l = mActiveTracks.getLatest();
6195         bool last = l.get() == track;
6196 
6197         if (track->isInvalid()) {
6198             ALOGW("An invalidated track shouldn't be in active list");
6199             tracksToRemove->add(track);
6200             continue;
6201         }
6202 
6203         if (track->mState == TrackBase::IDLE) {
6204             ALOGW("An idle track shouldn't be in active list");
6205             continue;
6206         }
6207 
6208         if (track->isPausing()) {
6209             track->setPaused();
6210             if (last) {
6211                 if (mHwSupportsPause && !mHwPaused) {
6212                     doHwPause = true;
6213                     mHwPaused = true;
6214                 }
6215                 // If we were part way through writing the mixbuffer to
6216                 // the HAL we must save this until we resume
6217                 // BUG - this will be wrong if a different track is made active,
6218                 // in that case we want to discard the pending data in the
6219                 // mixbuffer and tell the client to present it again when the
6220                 // track is resumed
6221                 mPausedWriteLength = mCurrentWriteLength;
6222                 mPausedBytesRemaining = mBytesRemaining;
6223                 mBytesRemaining = 0;    // stop writing
6224             }
6225             tracksToRemove->add(track);
6226         } else if (track->isFlushPending()) {
6227             if (track->isStopping_1()) {
6228                 track->mRetryCount = kMaxTrackStopRetriesOffload;
6229             } else {
6230                 track->mRetryCount = kMaxTrackRetriesOffload;
6231             }
6232             track->flushAck();
6233             if (last) {
6234                 mFlushPending = true;
6235             }
6236         } else if (track->isResumePending()){
6237             track->resumeAck();
6238             if (last) {
6239                 if (mPausedBytesRemaining) {
6240                     // Need to continue write that was interrupted
6241                     mCurrentWriteLength = mPausedWriteLength;
6242                     mBytesRemaining = mPausedBytesRemaining;
6243                     mPausedBytesRemaining = 0;
6244                 }
6245                 if (mHwPaused) {
6246                     doHwResume = true;
6247                     mHwPaused = false;
6248                     // threadLoop_mix() will handle the case that we need to
6249                     // resume an interrupted write
6250                 }
6251                 // enable write to audio HAL
6252                 mSleepTimeUs = 0;
6253 
6254                 mLeftVolFloat = mRightVolFloat = -1.0;
6255 
6256                 // Do not handle new data in this iteration even if track->framesReady()
6257                 mixerStatus = MIXER_TRACKS_ENABLED;
6258             }
6259         }  else if (track->framesReady() && track->isReady() &&
6260                 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) {
6261             ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer);
6262             if (track->mFillingUpStatus == Track::FS_FILLED) {
6263                 track->mFillingUpStatus = Track::FS_ACTIVE;
6264                 if (last) {
6265                     // make sure processVolume_l() will apply new volume even if 0
6266                     mLeftVolFloat = mRightVolFloat = -1.0;
6267                 }
6268             }
6269 
6270             if (last) {
6271                 sp<Track> previousTrack = mPreviousTrack.promote();
6272                 if (previousTrack != 0) {
6273                     if (track != previousTrack.get()) {
6274                         // Flush any data still being written from last track
6275                         mBytesRemaining = 0;
6276                         if (mPausedBytesRemaining) {
6277                             // Last track was paused so we also need to flush saved
6278                             // mixbuffer state and invalidate track so that it will
6279                             // re-submit that unwritten data when it is next resumed
6280                             mPausedBytesRemaining = 0;
6281                             // Invalidate is a bit drastic - would be more efficient
6282                             // to have a flag to tell client that some of the
6283                             // previously written data was lost
6284                             previousTrack->invalidate();
6285                         }
6286                         // flush data already sent to the DSP if changing audio session as audio
6287                         // comes from a different source. Also invalidate previous track to force a
6288                         // seek when resuming.
6289                         if (previousTrack->sessionId() != track->sessionId()) {
6290                             previousTrack->invalidate();
6291                         }
6292                     }
6293                 }
6294                 mPreviousTrack = track;
6295                 // reset retry count
6296                 if (track->isStopping_1()) {
6297                     track->mRetryCount = kMaxTrackStopRetriesOffload;
6298                 } else {
6299                     track->mRetryCount = kMaxTrackRetriesOffload;
6300                 }
6301                 mActiveTrack = t;
6302                 mixerStatus = MIXER_TRACKS_READY;
6303             }
6304         } else {
6305             ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer);
6306             if (track->isStopping_1()) {
6307                 if (--(track->mRetryCount) <= 0) {
6308                     // Hardware buffer can hold a large amount of audio so we must
6309                     // wait for all current track's data to drain before we say
6310                     // that the track is stopped.
6311                     if (mBytesRemaining == 0) {
6312                         // Only start draining when all data in mixbuffer
6313                         // has been written
6314                         ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2");
6315                         track->mState = TrackBase::STOPPING_2; // so presentation completes after
6316                         // drain do not drain if no data was ever sent to HAL (mStandby == true)
6317                         if (last && !mStandby) {
6318                             // do not modify drain sequence if we are already draining. This happens
6319                             // when resuming from pause after drain.
6320                             if ((mDrainSequence & 1) == 0) {
6321                                 mSleepTimeUs = 0;
6322                                 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
6323                                 mixerStatus = MIXER_DRAIN_TRACK;
6324                                 mDrainSequence += 2;
6325                             }
6326                             if (mHwPaused) {
6327                                 // It is possible to move from PAUSED to STOPPING_1 without
6328                                 // a resume so we must ensure hardware is running
6329                                 doHwResume = true;
6330                                 mHwPaused = false;
6331                             }
6332                         }
6333                     }
6334                 } else if (last) {
6335                     ALOGV("stopping1 underrun retries left %d", track->mRetryCount);
6336                     mixerStatus = MIXER_TRACKS_ENABLED;
6337                 }
6338             } else if (track->isStopping_2()) {
6339                 // Drain has completed or we are in standby, signal presentation complete
6340                 if (!(mDrainSequence & 1) || !last || mStandby) {
6341                     track->mState = TrackBase::STOPPED;
6342                     uint32_t latency = 0;
6343                     status_t result = mOutput->stream->getLatency(&latency);
6344                     ALOGE_IF(result != OK,
6345                             "Error when retrieving output stream latency: %d", result);
6346                     size_t audioHALFrames = (latency * mSampleRate) / 1000;
6347                     int64_t framesWritten =
6348                             mBytesWritten / mOutput->getFrameSize();
6349                     track->presentationComplete(framesWritten, audioHALFrames);
6350                     track->reset();
6351                     tracksToRemove->add(track);
6352                     // DIRECT and OFFLOADED stop resets frame counts.
6353                     if (!mUseAsyncWrite) {
6354                         // If we don't get explicit drain notification we must
6355                         // register discontinuity regardless of whether this is
6356                         // the previous (!last) or the upcoming (last) track
6357                         // to avoid skipping the discontinuity.
6358                         mTimestampVerifier.discontinuity();
6359                     }
6360                 }
6361             } else {
6362                 // No buffers for this track. Give it a few chances to
6363                 // fill a buffer, then remove it from active list.
6364                 if (--(track->mRetryCount) <= 0) {
6365                     bool running = false;
6366                     uint64_t position = 0;
6367                     struct timespec unused;
6368                     // The running check restarts the retry counter at least once.
6369                     status_t ret = mOutput->stream->getPresentationPosition(&position, &unused);
6370                     if (ret == NO_ERROR && position != mOffloadUnderrunPosition) {
6371                         running = true;
6372                         mOffloadUnderrunPosition = position;
6373                     }
6374                     if (ret == NO_ERROR) {
6375                         ALOGVV("underrun counter, running(%d): %lld vs %lld", running,
6376                                 (long long)position, (long long)mOffloadUnderrunPosition);
6377                     }
6378                     if (running) { // still running, give us more time.
6379                         track->mRetryCount = kMaxTrackRetriesOffload;
6380                     } else {
6381                         ALOGV("OffloadThread: BUFFER TIMEOUT: remove track(%d) from active list",
6382                                 track->id());
6383                         tracksToRemove->add(track);
6384                         // tell client process that the track was disabled because of underrun;
6385                         // it will then automatically call start() when data is available
6386                         track->disable();
6387                     }
6388                 } else if (last){
6389                     mixerStatus = MIXER_TRACKS_ENABLED;
6390                 }
6391             }
6392         }
6393         // compute volume for this track
6394         if (track->isReady()) {  // check ready to prevent premature start.
6395             processVolume_l(track, last);
6396         }
6397     }
6398 
6399     // make sure the pause/flush/resume sequence is executed in the right order.
6400     // If a flush is pending and a track is active but the HW is not paused, force a HW pause
6401     // before flush and then resume HW. This can happen in case of pause/flush/resume
6402     // if resume is received before pause is executed.
6403     if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
6404         status_t result = mOutput->stream->pause();
6405         ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
6406     }
6407     if (mFlushPending) {
6408         flushHw_l();
6409     }
6410     if (!mStandby && doHwResume) {
6411         status_t result = mOutput->stream->resume();
6412         ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
6413     }
6414 
6415     // remove all the tracks that need to be...
6416     removeTracks_l(*tracksToRemove);
6417 
6418     return mixerStatus;
6419 }
6420 
6421 // must be called with thread mutex locked
waitingAsyncCallback_l()6422 bool AudioFlinger::OffloadThread::waitingAsyncCallback_l()
6423 {
6424     ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d",
6425           mWriteAckSequence, mDrainSequence);
6426     if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) {
6427         return true;
6428     }
6429     return false;
6430 }
6431 
waitingAsyncCallback()6432 bool AudioFlinger::OffloadThread::waitingAsyncCallback()
6433 {
6434     Mutex::Autolock _l(mLock);
6435     return waitingAsyncCallback_l();
6436 }
6437 
flushHw_l()6438 void AudioFlinger::OffloadThread::flushHw_l()
6439 {
6440     DirectOutputThread::flushHw_l();
6441     // Flush anything still waiting in the mixbuffer
6442     mCurrentWriteLength = 0;
6443     mBytesRemaining = 0;
6444     mPausedWriteLength = 0;
6445     mPausedBytesRemaining = 0;
6446     // reset bytes written count to reflect that DSP buffers are empty after flush.
6447     mBytesWritten = 0;
6448     mOffloadUnderrunPosition = ~0LL;
6449 
6450     if (mUseAsyncWrite) {
6451         // discard any pending drain or write ack by incrementing sequence
6452         mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
6453         mDrainSequence = (mDrainSequence + 2) & ~1;
6454         ALOG_ASSERT(mCallbackThread != 0);
6455         mCallbackThread->setWriteBlocked(mWriteAckSequence);
6456         mCallbackThread->setDraining(mDrainSequence);
6457     }
6458 }
6459 
invalidateTracks(audio_stream_type_t streamType)6460 void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType)
6461 {
6462     Mutex::Autolock _l(mLock);
6463     if (PlaybackThread::invalidateTracks_l(streamType)) {
6464         mFlushPending = true;
6465     }
6466 }
6467 
6468 // ----------------------------------------------------------------------------
6469 
DuplicatingThread(const sp<AudioFlinger> & audioFlinger,AudioFlinger::MixerThread * mainThread,audio_io_handle_t id,bool systemReady)6470 AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
6471         AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady)
6472     :   MixerThread(audioFlinger, mainThread->getOutput(), id,
6473                     systemReady, DUPLICATING),
6474         mWaitTimeMs(UINT_MAX)
6475 {
6476     addOutputTrack(mainThread);
6477 }
6478 
~DuplicatingThread()6479 AudioFlinger::DuplicatingThread::~DuplicatingThread()
6480 {
6481     for (size_t i = 0; i < mOutputTracks.size(); i++) {
6482         mOutputTracks[i]->destroy();
6483     }
6484 }
6485 
threadLoop_mix()6486 void AudioFlinger::DuplicatingThread::threadLoop_mix()
6487 {
6488     // mix buffers...
6489     if (outputsReady(outputTracks)) {
6490         mAudioMixer->process();
6491     } else {
6492         if (mMixerBufferValid) {
6493             memset(mMixerBuffer, 0, mMixerBufferSize);
6494         } else {
6495             memset(mSinkBuffer, 0, mSinkBufferSize);
6496         }
6497     }
6498     mSleepTimeUs = 0;
6499     writeFrames = mNormalFrameCount;
6500     mCurrentWriteLength = mSinkBufferSize;
6501     mStandbyTimeNs = systemTime() + mStandbyDelayNs;
6502 }
6503 
threadLoop_sleepTime()6504 void AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
6505 {
6506     if (mSleepTimeUs == 0) {
6507         if (mMixerStatus == MIXER_TRACKS_ENABLED) {
6508             mSleepTimeUs = mActiveSleepTimeUs;
6509         } else {
6510             mSleepTimeUs = mIdleSleepTimeUs;
6511         }
6512     } else if (mBytesWritten != 0) {
6513         if (mMixerStatus == MIXER_TRACKS_ENABLED) {
6514             writeFrames = mNormalFrameCount;
6515             memset(mSinkBuffer, 0, mSinkBufferSize);
6516         } else {
6517             // flush remaining overflow buffers in output tracks
6518             writeFrames = 0;
6519         }
6520         mSleepTimeUs = 0;
6521     }
6522 }
6523 
threadLoop_write()6524 ssize_t AudioFlinger::DuplicatingThread::threadLoop_write()
6525 {
6526     for (size_t i = 0; i < outputTracks.size(); i++) {
6527         const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames);
6528 
6529         // Consider the first OutputTrack for timestamp and frame counting.
6530 
6531         // The threadLoop() generally assumes writing a full sink buffer size at a time.
6532         // Here, we correct for writeFrames of 0 (a stop) or underruns because
6533         // we always claim success.
6534         if (i == 0) {
6535             const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten;
6536             ALOGD_IF(correction != 0 && writeFrames != 0,
6537                     "%s: writeFrames:%u  actualWritten:%zd  correction:%zd  mFramesWritten:%lld",
6538                     __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten);
6539             mFramesWritten -= correction;
6540         }
6541 
6542         // TODO: Report correction for the other output tracks and show in the dump.
6543     }
6544     mStandby = false;
6545     return (ssize_t)mSinkBufferSize;
6546 }
6547 
threadLoop_standby()6548 void AudioFlinger::DuplicatingThread::threadLoop_standby()
6549 {
6550     // DuplicatingThread implements standby by stopping all tracks
6551     for (size_t i = 0; i < outputTracks.size(); i++) {
6552         outputTracks[i]->stop();
6553     }
6554 }
6555 
dumpInternals_l(int fd,const Vector<String16> & args __unused)6556 void AudioFlinger::DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
6557 {
6558     MixerThread::dumpInternals_l(fd, args);
6559 
6560     std::stringstream ss;
6561     const size_t numTracks = mOutputTracks.size();
6562     ss << "  " << numTracks << " OutputTracks";
6563     if (numTracks > 0) {
6564         ss << ":";
6565         for (const auto &track : mOutputTracks) {
6566             const sp<ThreadBase> thread = track->thread().promote();
6567             ss << " (" << track->id() << " : ";
6568             if (thread.get() != nullptr) {
6569                 ss << thread.get() << ", " << thread->id();
6570             } else {
6571                 ss << "null";
6572             }
6573             ss << ")";
6574         }
6575     }
6576     ss << "\n";
6577     std::string result = ss.str();
6578     write(fd, result.c_str(), result.size());
6579 }
6580 
saveOutputTracks()6581 void AudioFlinger::DuplicatingThread::saveOutputTracks()
6582 {
6583     outputTracks = mOutputTracks;
6584 }
6585 
clearOutputTracks()6586 void AudioFlinger::DuplicatingThread::clearOutputTracks()
6587 {
6588     outputTracks.clear();
6589 }
6590 
addOutputTrack(MixerThread * thread)6591 void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
6592 {
6593     Mutex::Autolock _l(mLock);
6594     // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass.
6595     // Adjust for thread->sampleRate() to determine minimum buffer frame count.
6596     // Then triple buffer because Threads do not run synchronously and may not be clock locked.
6597     const size_t frameCount =
6598             3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate());
6599     // TODO: Consider asynchronous sample rate conversion to handle clock disparity
6600     // from different OutputTracks and their associated MixerThreads (e.g. one may
6601     // nearly empty and the other may be dropping data).
6602 
6603     sp<OutputTrack> outputTrack = new OutputTrack(thread,
6604                                             this,
6605                                             mSampleRate,
6606                                             mFormat,
6607                                             mChannelMask,
6608                                             frameCount,
6609                                             IPCThreadState::self()->getCallingUid());
6610     status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY;
6611     if (status != NO_ERROR) {
6612         ALOGE("addOutputTrack() initCheck failed %d", status);
6613         return;
6614     }
6615     thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
6616     mOutputTracks.add(outputTrack);
6617     ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
6618     updateWaitTime_l();
6619 }
6620 
removeOutputTrack(MixerThread * thread)6621 void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
6622 {
6623     Mutex::Autolock _l(mLock);
6624     for (size_t i = 0; i < mOutputTracks.size(); i++) {
6625         if (mOutputTracks[i]->thread() == thread) {
6626             mOutputTracks[i]->destroy();
6627             mOutputTracks.removeAt(i);
6628             updateWaitTime_l();
6629             if (thread->getOutput() == mOutput) {
6630                 mOutput = NULL;
6631             }
6632             return;
6633         }
6634     }
6635     ALOGV("removeOutputTrack(): unknown thread: %p", thread);
6636 }
6637 
6638 // caller must hold mLock
updateWaitTime_l()6639 void AudioFlinger::DuplicatingThread::updateWaitTime_l()
6640 {
6641     mWaitTimeMs = UINT_MAX;
6642     for (size_t i = 0; i < mOutputTracks.size(); i++) {
6643         sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
6644         if (strong != 0) {
6645             uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
6646             if (waitTimeMs < mWaitTimeMs) {
6647                 mWaitTimeMs = waitTimeMs;
6648             }
6649         }
6650     }
6651 }
6652 
6653 
outputsReady(const SortedVector<sp<OutputTrack>> & outputTracks)6654 bool AudioFlinger::DuplicatingThread::outputsReady(
6655         const SortedVector< sp<OutputTrack> > &outputTracks)
6656 {
6657     for (size_t i = 0; i < outputTracks.size(); i++) {
6658         sp<ThreadBase> thread = outputTracks[i]->thread().promote();
6659         if (thread == 0) {
6660             ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p",
6661                     outputTracks[i].get());
6662             return false;
6663         }
6664         PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
6665         // see note at standby() declaration
6666         if (playbackThread->standby() && !playbackThread->isSuspended()) {
6667             ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(),
6668                     thread.get());
6669             return false;
6670         }
6671     }
6672     return true;
6673 }
6674 
sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata & metadata)6675 void AudioFlinger::DuplicatingThread::sendMetadataToBackend_l(
6676         const StreamOutHalInterface::SourceMetadata& metadata)
6677 {
6678     for (auto& outputTrack : outputTracks) { // not mOutputTracks
6679         outputTrack->setMetadatas(metadata.tracks);
6680     }
6681 }
6682 
activeSleepTimeUs() const6683 uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const
6684 {
6685     return (mWaitTimeMs * 1000) / 2;
6686 }
6687 
cacheParameters_l()6688 void AudioFlinger::DuplicatingThread::cacheParameters_l()
6689 {
6690     // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
6691     updateWaitTime_l();
6692 
6693     MixerThread::cacheParameters_l();
6694 }
6695 
6696 
6697 // ----------------------------------------------------------------------------
6698 //      Record
6699 // ----------------------------------------------------------------------------
6700 
RecordThread(const sp<AudioFlinger> & audioFlinger,AudioStreamIn * input,audio_io_handle_t id,bool systemReady)6701 AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
6702                                          AudioStreamIn *input,
6703                                          audio_io_handle_t id,
6704                                          bool systemReady
6705                                          ) :
6706     ThreadBase(audioFlinger, id, RECORD, systemReady),
6707     mInput(input),
6708     mSource(mInput),
6709     mActiveTracks(&this->mLocalLog),
6710     mRsmpInBuffer(NULL),
6711     // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l()
6712     mRsmpInRear(0)
6713     , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize,
6714             "RecordThreadRO", MemoryHeapBase::READ_ONLY))
6715     // mFastCapture below
6716     , mFastCaptureFutex(0)
6717     // mInputSource
6718     // mPipeSink
6719     // mPipeSource
6720     , mPipeFramesP2(0)
6721     // mPipeMemory
6722     // mFastCaptureNBLogWriter
6723     , mFastTrackAvail(false)
6724     , mBtNrecSuspended(false)
6725 {
6726     snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id);
6727     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
6728 
6729     if (mInput->audioHwDev != nullptr) {
6730         mIsMsdDevice = strcmp(
6731                 mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
6732     }
6733 
6734     readInputParameters_l();
6735 
6736     // TODO: We may also match on address as well as device type for
6737     // AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_IN_REMOTE_SUBMIX
6738     // TODO: This property should be ensure that only contains one single device type.
6739     mTimestampCorrectedDevice = (audio_devices_t)property_get_int64(
6740             "audio.timestamp.corrected_input_device",
6741             (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD
6742                                    : AUDIO_DEVICE_NONE));
6743 
6744     // create an NBAIO source for the HAL input stream, and negotiate
6745     mInputSource = new AudioStreamInSource(input->stream);
6746     size_t numCounterOffers = 0;
6747     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
6748 #if !LOG_NDEBUG
6749     ssize_t index =
6750 #else
6751     (void)
6752 #endif
6753             mInputSource->negotiate(offers, 1, NULL, numCounterOffers);
6754     ALOG_ASSERT(index == 0);
6755 
6756     // initialize fast capture depending on configuration
6757     bool initFastCapture;
6758     switch (kUseFastCapture) {
6759     case FastCapture_Never:
6760         initFastCapture = false;
6761         ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this);
6762         break;
6763     case FastCapture_Always:
6764         initFastCapture = true;
6765         ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this);
6766         break;
6767     case FastCapture_Static:
6768         initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs;
6769         ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d",
6770                 this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs,
6771                 initFastCapture);
6772         break;
6773     // case FastCapture_Dynamic:
6774     }
6775 
6776     if (initFastCapture) {
6777         // create a Pipe for FastCapture to write to, and for us and fast tracks to read from
6778         NBAIO_Format format = mInputSource->format();
6779         // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread
6780         size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000);
6781         size_t pipeSize = pipeFramesP2 * Format_frameSize(format);
6782         void *pipeBuffer = nullptr;
6783         const sp<MemoryDealer> roHeap(readOnlyHeap());
6784         sp<IMemory> pipeMemory;
6785         if ((roHeap == 0) ||
6786                 (pipeMemory = roHeap->allocate(pipeSize)) == 0 ||
6787                 (pipeBuffer = pipeMemory->pointer()) == nullptr) {
6788             ALOGE("not enough memory for pipe buffer size=%zu; "
6789                     "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld",
6790                     pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer,
6791                     (long long)kRecordThreadReadOnlyHeapSize);
6792             goto failed;
6793         }
6794         // pipe will be shared directly with fast clients, so clear to avoid leaking old information
6795         memset(pipeBuffer, 0, pipeSize);
6796         Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer);
6797         const NBAIO_Format offers[1] = {format};
6798         size_t numCounterOffers = 0;
6799         ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
6800         ALOG_ASSERT(index == 0);
6801         mPipeSink = pipe;
6802         PipeReader *pipeReader = new PipeReader(*pipe);
6803         numCounterOffers = 0;
6804         index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
6805         ALOG_ASSERT(index == 0);
6806         mPipeSource = pipeReader;
6807         mPipeFramesP2 = pipeFramesP2;
6808         mPipeMemory = pipeMemory;
6809 
6810         // create fast capture
6811         mFastCapture = new FastCapture();
6812         FastCaptureStateQueue *sq = mFastCapture->sq();
6813 #ifdef STATE_QUEUE_DUMP
6814         // FIXME
6815 #endif
6816         FastCaptureState *state = sq->begin();
6817         state->mCblk = NULL;
6818         state->mInputSource = mInputSource.get();
6819         state->mInputSourceGen++;
6820         state->mPipeSink = pipe;
6821         state->mPipeSinkGen++;
6822         state->mFrameCount = mFrameCount;
6823         state->mCommand = FastCaptureState::COLD_IDLE;
6824         // already done in constructor initialization list
6825         //mFastCaptureFutex = 0;
6826         state->mColdFutexAddr = &mFastCaptureFutex;
6827         state->mColdGen++;
6828         state->mDumpState = &mFastCaptureDumpState;
6829 #ifdef TEE_SINK
6830         // FIXME
6831 #endif
6832         mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture");
6833         state->mNBLogWriter = mFastCaptureNBLogWriter.get();
6834         sq->end();
6835         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
6836 
6837         // start the fast capture
6838         mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO);
6839         pid_t tid = mFastCapture->getTid();
6840         sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/);
6841         stream()->setHalThreadPriority(kPriorityFastCapture);
6842 #ifdef AUDIO_WATCHDOG
6843         // FIXME
6844 #endif
6845 
6846         mFastTrackAvail = true;
6847     }
6848 #ifdef TEE_SINK
6849     mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD);
6850     mTee.setId(std::string("_") + std::to_string(mId) + "_C");
6851 #endif
6852 failed: ;
6853 
6854     // FIXME mNormalSource
6855 }
6856 
~RecordThread()6857 AudioFlinger::RecordThread::~RecordThread()
6858 {
6859     if (mFastCapture != 0) {
6860         FastCaptureStateQueue *sq = mFastCapture->sq();
6861         FastCaptureState *state = sq->begin();
6862         if (state->mCommand == FastCaptureState::COLD_IDLE) {
6863             int32_t old = android_atomic_inc(&mFastCaptureFutex);
6864             if (old == -1) {
6865                 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
6866             }
6867         }
6868         state->mCommand = FastCaptureState::EXIT;
6869         sq->end();
6870         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
6871         mFastCapture->join();
6872         mFastCapture.clear();
6873     }
6874     mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter);
6875     mAudioFlinger->unregisterWriter(mNBLogWriter);
6876     free(mRsmpInBuffer);
6877 }
6878 
onFirstRef()6879 void AudioFlinger::RecordThread::onFirstRef()
6880 {
6881     run(mThreadName, PRIORITY_URGENT_AUDIO);
6882 }
6883 
preExit()6884 void AudioFlinger::RecordThread::preExit()
6885 {
6886     ALOGV("  preExit()");
6887     Mutex::Autolock _l(mLock);
6888     for (size_t i = 0; i < mTracks.size(); i++) {
6889         sp<RecordTrack> track = mTracks[i];
6890         track->invalidate();
6891     }
6892     mActiveTracks.clear();
6893     mStartStopCond.broadcast();
6894 }
6895 
threadLoop()6896 bool AudioFlinger::RecordThread::threadLoop()
6897 {
6898     nsecs_t lastWarning = 0;
6899 
6900     inputStandBy();
6901 
6902 reacquire_wakelock:
6903     sp<RecordTrack> activeTrack;
6904     {
6905         Mutex::Autolock _l(mLock);
6906         acquireWakeLock_l();
6907     }
6908 
6909     // used to request a deferred sleep, to be executed later while mutex is unlocked
6910     uint32_t sleepUs = 0;
6911 
6912     int64_t lastLoopCountRead = -2;  // never matches "previous" loop, when loopCount = 0.
6913 
6914     // loop while there is work to do
6915     for (int64_t loopCount = 0;; ++loopCount) {  // loopCount used for statistics tracking
6916         Vector< sp<EffectChain> > effectChains;
6917 
6918         // activeTracks accumulates a copy of a subset of mActiveTracks
6919         Vector< sp<RecordTrack> > activeTracks;
6920 
6921         // reference to the (first and only) active fast track
6922         sp<RecordTrack> fastTrack;
6923 
6924         // reference to a fast track which is about to be removed
6925         sp<RecordTrack> fastTrackToRemove;
6926 
6927         { // scope for mLock
6928             Mutex::Autolock _l(mLock);
6929 
6930             processConfigEvents_l();
6931 
6932             // check exitPending here because checkForNewParameters_l() and
6933             // checkForNewParameters_l() can temporarily release mLock
6934             if (exitPending()) {
6935                 break;
6936             }
6937 
6938             // sleep with mutex unlocked
6939             if (sleepUs > 0) {
6940                 ATRACE_BEGIN("sleepC");
6941                 mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs));
6942                 ATRACE_END();
6943                 sleepUs = 0;
6944                 continue;
6945             }
6946 
6947             // if no active track(s), then standby and release wakelock
6948             size_t size = mActiveTracks.size();
6949             if (size == 0) {
6950                 standbyIfNotAlreadyInStandby();
6951                 // exitPending() can't become true here
6952                 releaseWakeLock_l();
6953                 ALOGV("RecordThread: loop stopping");
6954                 // go to sleep
6955                 mWaitWorkCV.wait(mLock);
6956                 ALOGV("RecordThread: loop starting");
6957                 goto reacquire_wakelock;
6958             }
6959 
6960             bool doBroadcast = false;
6961             bool allStopped = true;
6962             for (size_t i = 0; i < size; ) {
6963 
6964                 activeTrack = mActiveTracks[i];
6965                 if (activeTrack->isTerminated()) {
6966                     if (activeTrack->isFastTrack()) {
6967                         ALOG_ASSERT(fastTrackToRemove == 0);
6968                         fastTrackToRemove = activeTrack;
6969                     }
6970                     removeTrack_l(activeTrack);
6971                     mActiveTracks.remove(activeTrack);
6972                     size--;
6973                     continue;
6974                 }
6975 
6976                 TrackBase::track_state activeTrackState = activeTrack->mState;
6977                 switch (activeTrackState) {
6978 
6979                 case TrackBase::PAUSING:
6980                     mActiveTracks.remove(activeTrack);
6981                     activeTrack->mState = TrackBase::PAUSED;
6982                     doBroadcast = true;
6983                     size--;
6984                     continue;
6985 
6986                 case TrackBase::STARTING_1:
6987                     sleepUs = 10000;
6988                     i++;
6989                     allStopped = false;
6990                     continue;
6991 
6992                 case TrackBase::STARTING_2:
6993                     doBroadcast = true;
6994                     mStandby = false;
6995                     activeTrack->mState = TrackBase::ACTIVE;
6996                     allStopped = false;
6997                     break;
6998 
6999                 case TrackBase::ACTIVE:
7000                     allStopped = false;
7001                     break;
7002 
7003                 case TrackBase::IDLE:    // cannot be on ActiveTracks if idle
7004                 case TrackBase::PAUSED:  // cannot be on ActiveTracks if paused
7005                 case TrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated
7006                 default:
7007                     LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu",
7008                             __func__, activeTrackState, activeTrack->id(), size);
7009                 }
7010 
7011                 activeTracks.add(activeTrack);
7012                 i++;
7013 
7014                 if (activeTrack->isFastTrack()) {
7015                     ALOG_ASSERT(!mFastTrackAvail);
7016                     ALOG_ASSERT(fastTrack == 0);
7017                     fastTrack = activeTrack;
7018                 }
7019             }
7020 
7021             mActiveTracks.updatePowerState(this);
7022 
7023             updateMetadata_l();
7024 
7025             if (allStopped) {
7026                 standbyIfNotAlreadyInStandby();
7027             }
7028             if (doBroadcast) {
7029                 mStartStopCond.broadcast();
7030             }
7031 
7032             // sleep if there are no active tracks to process
7033             if (activeTracks.isEmpty()) {
7034                 if (sleepUs == 0) {
7035                     sleepUs = kRecordThreadSleepUs;
7036                 }
7037                 continue;
7038             }
7039             sleepUs = 0;
7040 
7041             lockEffectChains_l(effectChains);
7042         }
7043 
7044         // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0
7045 
7046         size_t size = effectChains.size();
7047         for (size_t i = 0; i < size; i++) {
7048             // thread mutex is not locked, but effect chain is locked
7049             effectChains[i]->process_l();
7050         }
7051 
7052         // Push a new fast capture state if fast capture is not already running, or cblk change
7053         if (mFastCapture != 0) {
7054             FastCaptureStateQueue *sq = mFastCapture->sq();
7055             FastCaptureState *state = sq->begin();
7056             bool didModify = false;
7057             FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED;
7058             if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME &&
7059                     (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) {
7060                 if (state->mCommand == FastCaptureState::COLD_IDLE) {
7061                     int32_t old = android_atomic_inc(&mFastCaptureFutex);
7062                     if (old == -1) {
7063                         (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
7064                     }
7065                 }
7066                 state->mCommand = FastCaptureState::READ_WRITE;
7067 #if 0   // FIXME
7068                 mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
7069                         FastThreadDumpState::kSamplingNforLowRamDevice :
7070                         FastThreadDumpState::kSamplingN);
7071 #endif
7072                 didModify = true;
7073             }
7074             audio_track_cblk_t *cblkOld = state->mCblk;
7075             audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL;
7076             if (cblkNew != cblkOld) {
7077                 state->mCblk = cblkNew;
7078                 // block until acked if removing a fast track
7079                 if (cblkOld != NULL) {
7080                     block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED;
7081                 }
7082                 didModify = true;
7083             }
7084             AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ?
7085                     reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr;
7086             if (state->mFastPatchRecordBufferProvider != abp) {
7087                 state->mFastPatchRecordBufferProvider = abp;
7088                 state->mFastPatchRecordFormat = fastTrack == 0 ?
7089                         AUDIO_FORMAT_INVALID : fastTrack->format();
7090                 didModify = true;
7091             }
7092             sq->end(didModify);
7093             if (didModify) {
7094                 sq->push(block);
7095 #if 0
7096                 if (kUseFastCapture == FastCapture_Dynamic) {
7097                     mNormalSource = mPipeSource;
7098                 }
7099 #endif
7100             }
7101         }
7102 
7103         // now run the fast track destructor with thread mutex unlocked
7104         fastTrackToRemove.clear();
7105 
7106         // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one.
7107         // Only the client(s) that are too slow will overrun. But if even the fastest client is too
7108         // slow, then this RecordThread will overrun by not calling HAL read often enough.
7109         // If destination is non-contiguous, first read past the nominal end of buffer, then
7110         // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated.
7111 
7112         int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1);
7113         ssize_t framesRead;
7114         const int64_t lastIoBeginNs = systemTime(); // start IO timing
7115 
7116         // If an NBAIO source is present, use it to read the normal capture's data
7117         if (mPipeSource != 0) {
7118             size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2);
7119 
7120             // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer
7121             // to the full buffer point (clearing the overflow condition).  Upon OVERRUN error,
7122             // we immediately retry the read() to get data and prevent another overflow.
7123             for (int retries = 0; retries <= 2; ++retries) {
7124                 ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries);
7125                 framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize,
7126                         framesToRead);
7127                 if (framesRead != OVERRUN) break;
7128             }
7129 
7130             const ssize_t availableToRead = mPipeSource->availableToRead();
7131             if (availableToRead >= 0) {
7132                 // PipeSource is the master clock.  It is up to the AudioRecord client to keep up.
7133                 LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2,
7134                         "more frames to read than fifo size, %zd > %zu",
7135                         availableToRead, mPipeFramesP2);
7136                 const size_t pipeFramesFree = mPipeFramesP2 - availableToRead;
7137                 const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2;
7138                 ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd",
7139                         mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead);
7140                 sleepUs = (sleepFrames * 1000000LL) / mSampleRate;
7141             }
7142             if (framesRead < 0) {
7143                 status_t status = (status_t) framesRead;
7144                 switch (status) {
7145                 case OVERRUN:
7146                     ALOGW("overrun on read from pipe");
7147                     framesRead = 0;
7148                     break;
7149                 case NEGOTIATE:
7150                     ALOGE("re-negotiation is needed");
7151                     framesRead = -1;  // Will cause an attempt to recover.
7152                     break;
7153                 default:
7154                     ALOGE("unknown error %d on read from pipe", status);
7155                     break;
7156                 }
7157             }
7158         // otherwise use the HAL / AudioStreamIn directly
7159         } else {
7160             ATRACE_BEGIN("read");
7161             size_t bytesRead;
7162             status_t result = mSource->read(
7163                     (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead);
7164             ATRACE_END();
7165             if (result < 0) {
7166                 framesRead = result;
7167             } else {
7168                 framesRead = bytesRead / mFrameSize;
7169             }
7170         }
7171 
7172         const int64_t lastIoEndNs = systemTime(); // end IO timing
7173 
7174         // Update server timestamp with server stats
7175         // systemTime() is optional if the hardware supports timestamps.
7176         mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead;
7177         mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs;
7178 
7179         // Update server timestamp with kernel stats
7180         if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) {
7181             int64_t position, time;
7182             if (mStandby) {
7183                 mTimestampVerifier.discontinuity();
7184             } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR
7185                     && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) {
7186 
7187                 mTimestampVerifier.add(position, time, mSampleRate);
7188 
7189                 // Correct timestamps
7190                 if (isTimestampCorrectionEnabled()) {
7191                     ALOGVV("TS_BEFORE: %d %lld %lld",
7192                             id(), (long long)time, (long long)position);
7193                     auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
7194                     position = correctedTimestamp.mFrames;
7195                     time = correctedTimestamp.mTimeNs;
7196                     ALOGVV("TS_AFTER: %d %lld %lld",
7197                             id(), (long long)time, (long long)position);
7198                 }
7199 
7200                 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position;
7201                 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time;
7202                 // Note: In general record buffers should tend to be empty in
7203                 // a properly running pipeline.
7204                 //
7205                 // Also, it is not advantageous to call get_presentation_position during the read
7206                 // as the read obtains a lock, preventing the timestamp call from executing.
7207             } else {
7208                 mTimestampVerifier.error();
7209             }
7210         }
7211 
7212         // From the timestamp, input read latency is negative output write latency.
7213         const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE;
7214         const double latencyMs = RecordTrack::checkServerLatencySupported(mFormat, flags)
7215                 ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.;
7216         if (latencyMs != 0.) { // note 0. means timestamp is empty.
7217             mLatencyMs.add(latencyMs);
7218         }
7219 
7220         // Use this to track timestamp information
7221         // ALOGD("%s", mTimestamp.toString().c_str());
7222 
7223         if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) {
7224             ALOGE("read failed: framesRead=%zd", framesRead);
7225             // Force input into standby so that it tries to recover at next read attempt
7226             inputStandBy();
7227             sleepUs = kRecordThreadSleepUs;
7228         }
7229         if (framesRead <= 0) {
7230             goto unlock;
7231         }
7232         ALOG_ASSERT(framesRead > 0);
7233         mFramesRead += framesRead;
7234 
7235 #ifdef TEE_SINK
7236         (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead);
7237 #endif
7238         // If destination is non-contiguous, we now correct for reading past end of buffer.
7239         {
7240             size_t part1 = mRsmpInFramesP2 - rear;
7241             if ((size_t) framesRead > part1) {
7242                 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize,
7243                         (framesRead - part1) * mFrameSize);
7244             }
7245         }
7246         rear = mRsmpInRear += framesRead;
7247 
7248         size = activeTracks.size();
7249 
7250         // loop over each active track
7251         for (size_t i = 0; i < size; i++) {
7252             activeTrack = activeTracks[i];
7253 
7254             // skip fast tracks, as those are handled directly by FastCapture
7255             if (activeTrack->isFastTrack()) {
7256                 continue;
7257             }
7258 
7259             // TODO: This code probably should be moved to RecordTrack.
7260             // TODO: Update the activeTrack buffer converter in case of reconfigure.
7261 
7262             enum {
7263                 OVERRUN_UNKNOWN,
7264                 OVERRUN_TRUE,
7265                 OVERRUN_FALSE
7266             } overrun = OVERRUN_UNKNOWN;
7267 
7268             // loop over getNextBuffer to handle circular sink
7269             for (;;) {
7270 
7271                 activeTrack->mSink.frameCount = ~0;
7272                 status_t status = activeTrack->getNextBuffer(&activeTrack->mSink);
7273                 size_t framesOut = activeTrack->mSink.frameCount;
7274                 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0));
7275 
7276                 // check available frames and handle overrun conditions
7277                 // if the record track isn't draining fast enough.
7278                 bool hasOverrun;
7279                 size_t framesIn;
7280                 activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun);
7281                 if (hasOverrun) {
7282                     overrun = OVERRUN_TRUE;
7283                 }
7284                 if (framesOut == 0 || framesIn == 0) {
7285                     break;
7286                 }
7287 
7288                 // Don't allow framesOut to be larger than what is possible with resampling
7289                 // from framesIn.
7290                 // This isn't strictly necessary but helps limit buffer resizing in
7291                 // RecordBufferConverter.  TODO: remove when no longer needed.
7292                 framesOut = min(framesOut,
7293                         destinationFramesPossible(
7294                                 framesIn, mSampleRate, activeTrack->mSampleRate));
7295 
7296                 if (activeTrack->isDirect()) {
7297                     // No RecordBufferConverter used for direct streams. Pass
7298                     // straight from RecordThread buffer to RecordTrack buffer.
7299                     AudioBufferProvider::Buffer buffer;
7300                     buffer.frameCount = framesOut;
7301                     status_t status = activeTrack->mResamplerBufferProvider->getNextBuffer(&buffer);
7302                     if (status == OK && buffer.frameCount != 0) {
7303                         ALOGV_IF(buffer.frameCount != framesOut,
7304                                 "%s() read less than expected (%zu vs %zu)",
7305                                 __func__, buffer.frameCount, framesOut);
7306                         framesOut = buffer.frameCount;
7307                         memcpy(activeTrack->mSink.raw, buffer.raw, buffer.frameCount * mFrameSize);
7308                         activeTrack->mResamplerBufferProvider->releaseBuffer(&buffer);
7309                     } else {
7310                         framesOut = 0;
7311                         ALOGE("%s() cannot fill request, status: %d, frameCount: %zu",
7312                             __func__, status, buffer.frameCount);
7313                     }
7314                 } else {
7315                     // process frames from the RecordThread buffer provider to the RecordTrack
7316                     // buffer
7317                     framesOut = activeTrack->mRecordBufferConverter->convert(
7318                             activeTrack->mSink.raw,
7319                             activeTrack->mResamplerBufferProvider,
7320                             framesOut);
7321                 }
7322 
7323                 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) {
7324                     overrun = OVERRUN_FALSE;
7325                 }
7326 
7327                 if (activeTrack->mFramesToDrop == 0) {
7328                     if (framesOut > 0) {
7329                         activeTrack->mSink.frameCount = framesOut;
7330                         // Sanitize before releasing if the track has no access to the source data
7331                         // An idle UID receives silence from non virtual devices until active
7332                         if (activeTrack->isSilenced()) {
7333                             memset(activeTrack->mSink.raw, 0, framesOut * activeTrack->frameSize());
7334                         }
7335                         activeTrack->releaseBuffer(&activeTrack->mSink);
7336                     }
7337                 } else {
7338                     // FIXME could do a partial drop of framesOut
7339                     if (activeTrack->mFramesToDrop > 0) {
7340                         activeTrack->mFramesToDrop -= (ssize_t)framesOut;
7341                         if (activeTrack->mFramesToDrop <= 0) {
7342                             activeTrack->clearSyncStartEvent();
7343                         }
7344                     } else {
7345                         activeTrack->mFramesToDrop += framesOut;
7346                         if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 ||
7347                                 activeTrack->mSyncStartEvent->isCancelled()) {
7348                             ALOGW("Synced record %s, session %d, trigger session %d",
7349                                   (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled",
7350                                   activeTrack->sessionId(),
7351                                   (activeTrack->mSyncStartEvent != 0) ?
7352                                           activeTrack->mSyncStartEvent->triggerSession() :
7353                                           AUDIO_SESSION_NONE);
7354                             activeTrack->clearSyncStartEvent();
7355                         }
7356                     }
7357                 }
7358 
7359                 if (framesOut == 0) {
7360                     break;
7361                 }
7362             }
7363 
7364             switch (overrun) {
7365             case OVERRUN_TRUE:
7366                 // client isn't retrieving buffers fast enough
7367                 if (!activeTrack->setOverflow()) {
7368                     nsecs_t now = systemTime();
7369                     // FIXME should lastWarning per track?
7370                     if ((now - lastWarning) > kWarningThrottleNs) {
7371                         ALOGW("RecordThread: buffer overflow");
7372                         lastWarning = now;
7373                     }
7374                 }
7375                 break;
7376             case OVERRUN_FALSE:
7377                 activeTrack->clearOverflow();
7378                 break;
7379             case OVERRUN_UNKNOWN:
7380                 break;
7381             }
7382 
7383             // update frame information and push timestamp out
7384             activeTrack->updateTrackFrameInfo(
7385                     activeTrack->mServerProxy->framesReleased(),
7386                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER],
7387                     mSampleRate, mTimestamp);
7388         }
7389 
7390 unlock:
7391         // enable changes in effect chain
7392         unlockEffectChains(effectChains);
7393         // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end
7394         if (audio_has_proportional_frames(mFormat)
7395             && loopCount == lastLoopCountRead + 1) {
7396             const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs;
7397             const double jitterMs =
7398                 TimestampVerifier<int64_t, int64_t>::computeJitterMs(
7399                     {framesRead, readPeriodNs},
7400                     {0, 0} /* lastTimestamp */, mSampleRate);
7401             const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6;
7402 
7403             Mutex::Autolock _l(mLock);
7404             mIoJitterMs.add(jitterMs);
7405             mProcessTimeMs.add(processMs);
7406         }
7407         // update timing info.
7408         mLastIoBeginNs = lastIoBeginNs;
7409         mLastIoEndNs = lastIoEndNs;
7410         lastLoopCountRead = loopCount;
7411     }
7412 
7413     standbyIfNotAlreadyInStandby();
7414 
7415     {
7416         Mutex::Autolock _l(mLock);
7417         for (size_t i = 0; i < mTracks.size(); i++) {
7418             sp<RecordTrack> track = mTracks[i];
7419             track->invalidate();
7420         }
7421         mActiveTracks.clear();
7422         mStartStopCond.broadcast();
7423     }
7424 
7425     releaseWakeLock();
7426 
7427     ALOGV("RecordThread %p exiting", this);
7428     return false;
7429 }
7430 
standbyIfNotAlreadyInStandby()7431 void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby()
7432 {
7433     if (!mStandby) {
7434         inputStandBy();
7435         mStandby = true;
7436     }
7437 }
7438 
inputStandBy()7439 void AudioFlinger::RecordThread::inputStandBy()
7440 {
7441     // Idle the fast capture if it's currently running
7442     if (mFastCapture != 0) {
7443         FastCaptureStateQueue *sq = mFastCapture->sq();
7444         FastCaptureState *state = sq->begin();
7445         if (!(state->mCommand & FastCaptureState::IDLE)) {
7446             state->mCommand = FastCaptureState::COLD_IDLE;
7447             state->mColdFutexAddr = &mFastCaptureFutex;
7448             state->mColdGen++;
7449             mFastCaptureFutex = 0;
7450             sq->end();
7451             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
7452             sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED);
7453 #if 0
7454             if (kUseFastCapture == FastCapture_Dynamic) {
7455                 // FIXME
7456             }
7457 #endif
7458 #ifdef AUDIO_WATCHDOG
7459             // FIXME
7460 #endif
7461         } else {
7462             sq->end(false /*didModify*/);
7463         }
7464     }
7465     status_t result = mSource->standby();
7466     ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result);
7467 
7468     // If going into standby, flush the pipe source.
7469     if (mPipeSource.get() != nullptr) {
7470         const ssize_t flushed = mPipeSource->flush();
7471         if (flushed > 0) {
7472             ALOGV("Input standby flushed PipeSource %zd frames", flushed);
7473             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed;
7474             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime();
7475         }
7476     }
7477 }
7478 
7479 // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held
createRecordTrack_l(const sp<AudioFlinger::Client> & client,const audio_attributes_t & attr,uint32_t * pSampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * pFrameCount,audio_session_t sessionId,size_t * pNotificationFrameCount,pid_t creatorPid,uid_t uid,audio_input_flags_t * flags,pid_t tid,status_t * status,audio_port_handle_t portId,const String16 & opPackageName)7480 sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
7481         const sp<AudioFlinger::Client>& client,
7482         const audio_attributes_t& attr,
7483         uint32_t *pSampleRate,
7484         audio_format_t format,
7485         audio_channel_mask_t channelMask,
7486         size_t *pFrameCount,
7487         audio_session_t sessionId,
7488         size_t *pNotificationFrameCount,
7489         pid_t creatorPid,
7490         uid_t uid,
7491         audio_input_flags_t *flags,
7492         pid_t tid,
7493         status_t *status,
7494         audio_port_handle_t portId,
7495         const String16& opPackageName)
7496 {
7497     size_t frameCount = *pFrameCount;
7498     size_t notificationFrameCount = *pNotificationFrameCount;
7499     sp<RecordTrack> track;
7500     status_t lStatus;
7501     audio_input_flags_t inputFlags = mInput->flags;
7502     audio_input_flags_t requestedFlags = *flags;
7503     uint32_t sampleRate;
7504 
7505     lStatus = initCheck();
7506     if (lStatus != NO_ERROR) {
7507         ALOGE("createRecordTrack_l() audio driver not initialized");
7508         goto Exit;
7509     }
7510 
7511     if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) {
7512         ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT");
7513         lStatus = BAD_VALUE;
7514         goto Exit;
7515     }
7516 
7517     if (*pSampleRate == 0) {
7518         *pSampleRate = mSampleRate;
7519     }
7520     sampleRate = *pSampleRate;
7521 
7522     // special case for FAST flag considered OK if fast capture is present
7523     if (hasFastCapture()) {
7524         inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST);
7525     }
7526 
7527     // Check if requested flags are compatible with input stream flags
7528     if ((*flags & inputFlags) != *flags) {
7529         ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and"
7530                 " input flags (%08x)",
7531               *flags, inputFlags);
7532         *flags = (audio_input_flags_t)(*flags & inputFlags);
7533     }
7534 
7535     // client expresses a preference for FAST, but we get the final say
7536     if (*flags & AUDIO_INPUT_FLAG_FAST) {
7537       if (
7538             // we formerly checked for a callback handler (non-0 tid),
7539             // but that is no longer required for TRANSFER_OBTAIN mode
7540             //
7541             // Frame count is not specified (0), or is less than or equal the pipe depth.
7542             // It is OK to provide a higher capacity than requested.
7543             // We will force it to mPipeFramesP2 below.
7544             (frameCount <= mPipeFramesP2) &&
7545             // PCM data
7546             audio_is_linear_pcm(format) &&
7547             // hardware format
7548             (format == mFormat) &&
7549             // hardware channel mask
7550             (channelMask == mChannelMask) &&
7551             // hardware sample rate
7552             (sampleRate == mSampleRate) &&
7553             // record thread has an associated fast capture
7554             hasFastCapture() &&
7555             // there are sufficient fast track slots available
7556             mFastTrackAvail
7557         ) {
7558           // check compatibility with audio effects.
7559           Mutex::Autolock _l(mLock);
7560           // Do not accept FAST flag if the session has software effects
7561           sp<EffectChain> chain = getEffectChain_l(sessionId);
7562           if (chain != 0) {
7563               audio_input_flags_t old = *flags;
7564               chain->checkInputFlagCompatibility(flags);
7565               if (old != *flags) {
7566                   ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x",
7567                           this, (int)old, (int)*flags);
7568               }
7569           }
7570           ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0,
7571                    "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
7572                    this, frameCount, mFrameCount);
7573       } else {
7574         ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu "
7575                 "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u "
7576                 "hasFastCapture=%d tid=%d mFastTrackAvail=%d",
7577                 this, frameCount, mFrameCount, mPipeFramesP2,
7578                 format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate,
7579                 hasFastCapture(), tid, mFastTrackAvail);
7580         *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
7581       }
7582     }
7583 
7584     // If FAST or RAW flags were corrected, ask caller to request new input from audio policy
7585     if ((*flags & AUDIO_INPUT_FLAG_FAST) !=
7586             (requestedFlags & AUDIO_INPUT_FLAG_FAST)) {
7587         *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW));
7588         lStatus = BAD_TYPE;
7589         goto Exit;
7590     }
7591 
7592     // compute track buffer size in frames, and suggest the notification frame count
7593     if (*flags & AUDIO_INPUT_FLAG_FAST) {
7594         // fast track: frame count is exactly the pipe depth
7595         frameCount = mPipeFramesP2;
7596         // ignore requested notificationFrames, and always notify exactly once every HAL buffer
7597         notificationFrameCount = mFrameCount;
7598     } else {
7599         // not fast track: max notification period is resampled equivalent of one HAL buffer time
7600         //                 or 20 ms if there is a fast capture
7601         // TODO This could be a roundupRatio inline, and const
7602         size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount)
7603                 * sampleRate + mSampleRate - 1) / mSampleRate;
7604         // minimum number of notification periods is at least kMinNotifications,
7605         // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs)
7606         static const size_t kMinNotifications = 3;
7607         static const uint32_t kMinMs = 30;
7608         // TODO This could be a roundupRatio inline
7609         const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000;
7610         // TODO This could be a roundupRatio inline
7611         const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) /
7612                 maxNotificationFrames;
7613         const size_t minFrameCount = maxNotificationFrames *
7614                 max(kMinNotifications, minNotificationsByMs);
7615         frameCount = max(frameCount, minFrameCount);
7616         if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
7617             notificationFrameCount = maxNotificationFrames;
7618         }
7619     }
7620     *pFrameCount = frameCount;
7621     *pNotificationFrameCount = notificationFrameCount;
7622 
7623     { // scope for mLock
7624         Mutex::Autolock _l(mLock);
7625 
7626         track = new RecordTrack(this, client, attr, sampleRate,
7627                       format, channelMask, frameCount,
7628                       nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, uid,
7629                       *flags, TrackBase::TYPE_DEFAULT, opPackageName, portId);
7630 
7631         lStatus = track->initCheck();
7632         if (lStatus != NO_ERROR) {
7633             ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus);
7634             // track must be cleared from the caller as the caller has the AF lock
7635             goto Exit;
7636         }
7637         mTracks.add(track);
7638 
7639         if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) {
7640             pid_t callingPid = IPCThreadState::self()->getCallingPid();
7641             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
7642             // so ask activity manager to do this on our behalf
7643             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
7644         }
7645     }
7646 
7647     lStatus = NO_ERROR;
7648 
7649 Exit:
7650     *status = lStatus;
7651     return track;
7652 }
7653 
start(RecordThread::RecordTrack * recordTrack,AudioSystem::sync_event_t event,audio_session_t triggerSession)7654 status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack,
7655                                            AudioSystem::sync_event_t event,
7656                                            audio_session_t triggerSession)
7657 {
7658     ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
7659     sp<ThreadBase> strongMe = this;
7660     status_t status = NO_ERROR;
7661 
7662     if (event == AudioSystem::SYNC_EVENT_NONE) {
7663         recordTrack->clearSyncStartEvent();
7664     } else if (event != AudioSystem::SYNC_EVENT_SAME) {
7665         recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event,
7666                                        triggerSession,
7667                                        recordTrack->sessionId(),
7668                                        syncStartEventCallback,
7669                                        recordTrack);
7670         // Sync event can be cancelled by the trigger session if the track is not in a
7671         // compatible state in which case we start record immediately
7672         if (recordTrack->mSyncStartEvent->isCancelled()) {
7673             recordTrack->clearSyncStartEvent();
7674         } else {
7675             // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs
7676             recordTrack->mFramesToDrop = -(ssize_t)
7677                     ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000);
7678         }
7679     }
7680 
7681     {
7682         // This section is a rendezvous between binder thread executing start() and RecordThread
7683         AutoMutex lock(mLock);
7684         if (recordTrack->isInvalid()) {
7685             recordTrack->clearSyncStartEvent();
7686             return INVALID_OPERATION;
7687         }
7688         if (mActiveTracks.indexOf(recordTrack) >= 0) {
7689             if (recordTrack->mState == TrackBase::PAUSING) {
7690                 // We haven't stopped yet (moved to PAUSED and not in mActiveTracks)
7691                 // so no need to startInput().
7692                 ALOGV("active record track PAUSING -> ACTIVE");
7693                 recordTrack->mState = TrackBase::ACTIVE;
7694             } else {
7695                 ALOGV("active record track state %d", recordTrack->mState);
7696             }
7697             return status;
7698         }
7699 
7700         // TODO consider other ways of handling this, such as changing the state to :STARTING and
7701         //      adding the track to mActiveTracks after returning from AudioSystem::startInput(),
7702         //      or using a separate command thread
7703         recordTrack->mState = TrackBase::STARTING_1;
7704         mActiveTracks.add(recordTrack);
7705         status_t status = NO_ERROR;
7706         if (recordTrack->isExternalTrack()) {
7707             mLock.unlock();
7708             status = AudioSystem::startInput(recordTrack->portId());
7709             mLock.lock();
7710             if (recordTrack->isInvalid()) {
7711                 recordTrack->clearSyncStartEvent();
7712                 if (status == NO_ERROR && recordTrack->mState == TrackBase::STARTING_1) {
7713                     recordTrack->mState = TrackBase::STARTING_2;
7714                     // STARTING_2 forces destroy to call stopInput.
7715                 }
7716                 return INVALID_OPERATION;
7717             }
7718             if (recordTrack->mState != TrackBase::STARTING_1) {
7719                 ALOGW("%s(%d): unsynchronized mState:%d change",
7720                     __func__, recordTrack->id(), recordTrack->mState);
7721                 // Someone else has changed state, let them take over,
7722                 // leave mState in the new state.
7723                 recordTrack->clearSyncStartEvent();
7724                 return INVALID_OPERATION;
7725             }
7726             // we're ok, but perhaps startInput has failed
7727             if (status != NO_ERROR) {
7728                 ALOGW("%s(%d): startInput failed, status %d",
7729                     __func__, recordTrack->id(), status);
7730                 // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks,
7731                 // leave in STARTING_1, so destroy() will not call stopInput.
7732                 mActiveTracks.remove(recordTrack);
7733                 recordTrack->clearSyncStartEvent();
7734                 return status;
7735             }
7736             sendIoConfigEvent_l(
7737                 AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId());
7738         }
7739         // Catch up with current buffer indices if thread is already running.
7740         // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront
7741         // was initialized to some value closer to the thread's mRsmpInFront, then the track could
7742         // see previously buffered data before it called start(), but with greater risk of overrun.
7743 
7744         recordTrack->mResamplerBufferProvider->reset();
7745         if (!recordTrack->isDirect()) {
7746             // clear any converter state as new data will be discontinuous
7747             recordTrack->mRecordBufferConverter->reset();
7748         }
7749         recordTrack->mState = TrackBase::STARTING_2;
7750         // signal thread to start
7751         mWaitWorkCV.broadcast();
7752         return status;
7753     }
7754 }
7755 
syncStartEventCallback(const wp<SyncEvent> & event)7756 void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
7757 {
7758     sp<SyncEvent> strongEvent = event.promote();
7759 
7760     if (strongEvent != 0) {
7761         sp<RefBase> ptr = strongEvent->cookie().promote();
7762         if (ptr != 0) {
7763             RecordTrack *recordTrack = (RecordTrack *)ptr.get();
7764             recordTrack->handleSyncStartEvent(strongEvent);
7765         }
7766     }
7767 }
7768 
stop(RecordThread::RecordTrack * recordTrack)7769 bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
7770     ALOGV("RecordThread::stop");
7771     AutoMutex _l(mLock);
7772     // if we're invalid, we can't be on the ActiveTracks.
7773     if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) {
7774         return false;
7775     }
7776     // note that threadLoop may still be processing the track at this point [without lock]
7777     recordTrack->mState = TrackBase::PAUSING;
7778 
7779     // NOTE: Waiting here is important to keep stop synchronous.
7780     // This is needed for proper patchRecord peer release.
7781     while (recordTrack->mState == TrackBase::PAUSING && !recordTrack->isInvalid()) {
7782         mWaitWorkCV.broadcast(); // signal thread to stop
7783         mStartStopCond.wait(mLock);
7784     }
7785 
7786     if (recordTrack->mState == TrackBase::PAUSED) { // successful stop
7787         ALOGV("Record stopped OK");
7788         return true;
7789     }
7790 
7791     // don't handle anything - we've been invalidated or restarted and in a different state
7792     ALOGW_IF("%s(%d): unsynchronized stop, state: %d",
7793             __func__, recordTrack->id(), recordTrack->mState);
7794     return false;
7795 }
7796 
isValidSyncEvent(const sp<SyncEvent> & event __unused) const7797 bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
7798 {
7799     return false;
7800 }
7801 
setSyncEvent(const sp<SyncEvent> & event __unused)7802 status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused)
7803 {
7804 #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future
7805     if (!isValidSyncEvent(event)) {
7806         return BAD_VALUE;
7807     }
7808 
7809     audio_session_t eventSession = event->triggerSession();
7810     status_t ret = NAME_NOT_FOUND;
7811 
7812     Mutex::Autolock _l(mLock);
7813 
7814     for (size_t i = 0; i < mTracks.size(); i++) {
7815         sp<RecordTrack> track = mTracks[i];
7816         if (eventSession == track->sessionId()) {
7817             (void) track->setSyncEvent(event);
7818             ret = NO_ERROR;
7819         }
7820     }
7821     return ret;
7822 #else
7823     return BAD_VALUE;
7824 #endif
7825 }
7826 
getActiveMicrophones(std::vector<media::MicrophoneInfo> * activeMicrophones)7827 status_t AudioFlinger::RecordThread::getActiveMicrophones(
7828         std::vector<media::MicrophoneInfo>* activeMicrophones)
7829 {
7830     ALOGV("RecordThread::getActiveMicrophones");
7831     AutoMutex _l(mLock);
7832     status_t status = mInput->stream->getActiveMicrophones(activeMicrophones);
7833     return status;
7834 }
7835 
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)7836 status_t AudioFlinger::RecordThread::setPreferredMicrophoneDirection(
7837             audio_microphone_direction_t direction)
7838 {
7839     ALOGV("setPreferredMicrophoneDirection(%d)", direction);
7840     AutoMutex _l(mLock);
7841     return mInput->stream->setPreferredMicrophoneDirection(direction);
7842 }
7843 
setPreferredMicrophoneFieldDimension(float zoom)7844 status_t AudioFlinger::RecordThread::setPreferredMicrophoneFieldDimension(float zoom)
7845 {
7846     ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
7847     AutoMutex _l(mLock);
7848     return mInput->stream->setPreferredMicrophoneFieldDimension(zoom);
7849 }
7850 
updateMetadata_l()7851 void AudioFlinger::RecordThread::updateMetadata_l()
7852 {
7853     if (mInput == nullptr || mInput->stream == nullptr ||
7854             !mActiveTracks.readAndClearHasChanged()) {
7855         return;
7856     }
7857     StreamInHalInterface::SinkMetadata metadata;
7858     for (const sp<RecordTrack> &track : mActiveTracks) {
7859         // No track is invalid as this is called after prepareTrack_l in the same critical section
7860         metadata.tracks.push_back({
7861                 .source = track->attributes().source,
7862                 .gain = 1, // capture tracks do not have volumes
7863         });
7864     }
7865     mInput->stream->updateSinkMetadata(metadata);
7866 }
7867 
7868 // destroyTrack_l() must be called with ThreadBase::mLock held
destroyTrack_l(const sp<RecordTrack> & track)7869 void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track)
7870 {
7871     track->terminate();
7872     track->mState = TrackBase::STOPPED;
7873     // active tracks are removed by threadLoop()
7874     if (mActiveTracks.indexOf(track) < 0) {
7875         removeTrack_l(track);
7876     }
7877 }
7878 
removeTrack_l(const sp<RecordTrack> & track)7879 void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track)
7880 {
7881     String8 result;
7882     track->appendDump(result, false /* active */);
7883     mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string());
7884 
7885     mTracks.remove(track);
7886     // need anything related to effects here?
7887     if (track->isFastTrack()) {
7888         ALOG_ASSERT(!mFastTrackAvail);
7889         mFastTrackAvail = true;
7890     }
7891 }
7892 
dumpInternals_l(int fd,const Vector<String16> & args __unused)7893 void AudioFlinger::RecordThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
7894 {
7895     AudioStreamIn *input = mInput;
7896     audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE;
7897     dprintf(fd, "  AudioStreamIn: %p flags %#x (%s)\n",
7898             input, flags, toString(flags).c_str());
7899     dprintf(fd, "  Frames read: %lld\n", (long long)mFramesRead);
7900     if (mActiveTracks.isEmpty()) {
7901         dprintf(fd, "  No active record clients\n");
7902     }
7903 
7904     if (input != nullptr) {
7905         dprintf(fd, "  Hal stream dump:\n");
7906         (void)input->stream->dump(fd);
7907     }
7908 
7909     dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no");
7910     dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no");
7911 
7912     // Make a non-atomic copy of fast capture dump state so it won't change underneath us
7913     // while we are dumping it.  It may be inconsistent, but it won't mutate!
7914     // This is a large object so we place it on the heap.
7915     // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
7916     const std::unique_ptr<FastCaptureDumpState> copy =
7917             std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState);
7918     copy->dump(fd);
7919 }
7920 
dumpTracks_l(int fd,const Vector<String16> & args __unused)7921 void AudioFlinger::RecordThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
7922 {
7923     String8 result;
7924     size_t numtracks = mTracks.size();
7925     size_t numactive = mActiveTracks.size();
7926     size_t numactiveseen = 0;
7927     dprintf(fd, "  %zu Tracks", numtracks);
7928     const char *prefix = "    ";
7929     if (numtracks) {
7930         dprintf(fd, " of which %zu are active\n", numactive);
7931         result.append(prefix);
7932         mTracks[0]->appendDumpHeader(result);
7933         for (size_t i = 0; i < numtracks ; ++i) {
7934             sp<RecordTrack> track = mTracks[i];
7935             if (track != 0) {
7936                 bool active = mActiveTracks.indexOf(track) >= 0;
7937                 if (active) {
7938                     numactiveseen++;
7939                 }
7940                 result.append(prefix);
7941                 track->appendDump(result, active);
7942             }
7943         }
7944     } else {
7945         dprintf(fd, "\n");
7946     }
7947 
7948     if (numactiveseen != numactive) {
7949         result.append("  The following tracks are in the active list but"
7950                 " not in the track list\n");
7951         result.append(prefix);
7952         mActiveTracks[0]->appendDumpHeader(result);
7953         for (size_t i = 0; i < numactive; ++i) {
7954             sp<RecordTrack> track = mActiveTracks[i];
7955             if (mTracks.indexOf(track) < 0) {
7956                 result.append(prefix);
7957                 track->appendDump(result, true /* active */);
7958             }
7959         }
7960 
7961     }
7962     write(fd, result.string(), result.size());
7963 }
7964 
setRecordSilenced(uid_t uid,bool silenced)7965 void AudioFlinger::RecordThread::setRecordSilenced(uid_t uid, bool silenced)
7966 {
7967     Mutex::Autolock _l(mLock);
7968     for (size_t i = 0; i < mTracks.size() ; i++) {
7969         sp<RecordTrack> track = mTracks[i];
7970         if (track != 0 && track->uid() == uid) {
7971             track->setSilenced(silenced);
7972         }
7973     }
7974 }
7975 
reset()7976 void AudioFlinger::RecordThread::ResamplerBufferProvider::reset()
7977 {
7978     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
7979     RecordThread *recordThread = (RecordThread *) threadBase.get();
7980     mRsmpInFront = recordThread->mRsmpInRear;
7981     mRsmpInUnrel = 0;
7982 }
7983 
sync(size_t * framesAvailable,bool * hasOverrun)7984 void AudioFlinger::RecordThread::ResamplerBufferProvider::sync(
7985         size_t *framesAvailable, bool *hasOverrun)
7986 {
7987     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
7988     RecordThread *recordThread = (RecordThread *) threadBase.get();
7989     const int32_t rear = recordThread->mRsmpInRear;
7990     const int32_t front = mRsmpInFront;
7991     const ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
7992 
7993     size_t framesIn;
7994     bool overrun = false;
7995     if (filled < 0) {
7996         // should not happen, but treat like a massive overrun and re-sync
7997         framesIn = 0;
7998         mRsmpInFront = rear;
7999         overrun = true;
8000     } else if ((size_t) filled <= recordThread->mRsmpInFrames) {
8001         framesIn = (size_t) filled;
8002     } else {
8003         // client is not keeping up with server, but give it latest data
8004         framesIn = recordThread->mRsmpInFrames;
8005         mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow(
8006                 rear, static_cast<int32_t>(framesIn));
8007         overrun = true;
8008     }
8009     if (framesAvailable != NULL) {
8010         *framesAvailable = framesIn;
8011     }
8012     if (hasOverrun != NULL) {
8013         *hasOverrun = overrun;
8014     }
8015 }
8016 
8017 // AudioBufferProvider interface
getNextBuffer(AudioBufferProvider::Buffer * buffer)8018 status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer(
8019         AudioBufferProvider::Buffer* buffer)
8020 {
8021     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
8022     if (threadBase == 0) {
8023         buffer->frameCount = 0;
8024         buffer->raw = NULL;
8025         return NOT_ENOUGH_DATA;
8026     }
8027     RecordThread *recordThread = (RecordThread *) threadBase.get();
8028     int32_t rear = recordThread->mRsmpInRear;
8029     int32_t front = mRsmpInFront;
8030     ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
8031     // FIXME should not be P2 (don't want to increase latency)
8032     // FIXME if client not keeping up, discard
8033     LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames));
8034     // 'filled' may be non-contiguous, so return only the first contiguous chunk
8035     front &= recordThread->mRsmpInFramesP2 - 1;
8036     size_t part1 = recordThread->mRsmpInFramesP2 - front;
8037     if (part1 > (size_t) filled) {
8038         part1 = filled;
8039     }
8040     size_t ask = buffer->frameCount;
8041     ALOG_ASSERT(ask > 0);
8042     if (part1 > ask) {
8043         part1 = ask;
8044     }
8045     if (part1 == 0) {
8046         // out of data is fine since the resampler will return a short-count.
8047         buffer->raw = NULL;
8048         buffer->frameCount = 0;
8049         mRsmpInUnrel = 0;
8050         return NOT_ENOUGH_DATA;
8051     }
8052 
8053     buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize;
8054     buffer->frameCount = part1;
8055     mRsmpInUnrel = part1;
8056     return NO_ERROR;
8057 }
8058 
8059 // AudioBufferProvider interface
releaseBuffer(AudioBufferProvider::Buffer * buffer)8060 void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer(
8061         AudioBufferProvider::Buffer* buffer)
8062 {
8063     int32_t stepCount = static_cast<int32_t>(buffer->frameCount);
8064     if (stepCount == 0) {
8065         return;
8066     }
8067     ALOG_ASSERT(stepCount <= mRsmpInUnrel);
8068     mRsmpInUnrel -= stepCount;
8069     mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount);
8070     buffer->raw = NULL;
8071     buffer->frameCount = 0;
8072 }
8073 
checkBtNrec()8074 void AudioFlinger::RecordThread::checkBtNrec()
8075 {
8076     Mutex::Autolock _l(mLock);
8077     checkBtNrec_l();
8078 }
8079 
checkBtNrec_l()8080 void AudioFlinger::RecordThread::checkBtNrec_l()
8081 {
8082     // disable AEC and NS if the device is a BT SCO headset supporting those
8083     // pre processings
8084     bool suspend = audio_is_bluetooth_sco_device(inDeviceType()) &&
8085                         mAudioFlinger->btNrecIsOff();
8086     if (mBtNrecSuspended.exchange(suspend) != suspend) {
8087         for (size_t i = 0; i < mEffectChains.size(); i++) {
8088             setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId());
8089             setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId());
8090         }
8091     }
8092 }
8093 
8094 
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)8095 bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair,
8096                                                         status_t& status)
8097 {
8098     bool reconfig = false;
8099 
8100     status = NO_ERROR;
8101 
8102     audio_format_t reqFormat = mFormat;
8103     uint32_t samplingRate = mSampleRate;
8104     // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs).
8105     audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount);
8106 
8107     AudioParameter param = AudioParameter(keyValuePair);
8108     int value;
8109 
8110     // scope for AutoPark extends to end of method
8111     AutoPark<FastCapture> park(mFastCapture);
8112 
8113     // TODO Investigate when this code runs. Check with audio policy when a sample rate and
8114     //      channel count change can be requested. Do we mandate the first client defines the
8115     //      HAL sampling rate and channel count or do we allow changes on the fly?
8116     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
8117         samplingRate = value;
8118         reconfig = true;
8119     }
8120     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
8121         if (!audio_is_linear_pcm((audio_format_t) value)) {
8122             status = BAD_VALUE;
8123         } else {
8124             reqFormat = (audio_format_t) value;
8125             reconfig = true;
8126         }
8127     }
8128     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
8129         audio_channel_mask_t mask = (audio_channel_mask_t) value;
8130         if (!audio_is_input_channel(mask) ||
8131                 audio_channel_count_from_in_mask(mask) > FCC_8) {
8132             status = BAD_VALUE;
8133         } else {
8134             channelMask = mask;
8135             reconfig = true;
8136         }
8137     }
8138     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
8139         // do not accept frame count changes if tracks are open as the track buffer
8140         // size depends on frame count and correct behavior would not be guaranteed
8141         // if frame count is changed after track creation
8142         if (mActiveTracks.size() > 0) {
8143             status = INVALID_OPERATION;
8144         } else {
8145             reconfig = true;
8146         }
8147     }
8148     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
8149         LOG_FATAL("Should not set routing device in RecordThread");
8150     }
8151     if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR &&
8152             mAudioSource != (audio_source_t)value) {
8153         LOG_FATAL("Should not set audio source in RecordThread");
8154     }
8155 
8156     if (status == NO_ERROR) {
8157         status = mInput->stream->setParameters(keyValuePair);
8158         if (status == INVALID_OPERATION) {
8159             inputStandBy();
8160             status = mInput->stream->setParameters(keyValuePair);
8161         }
8162         if (reconfig) {
8163             if (status == BAD_VALUE) {
8164                 uint32_t sRate;
8165                 audio_channel_mask_t channelMask;
8166                 audio_format_t format;
8167                 if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK &&
8168                         audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) &&
8169                         sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) &&
8170                         audio_channel_count_from_in_mask(channelMask) <= FCC_8) {
8171                     status = NO_ERROR;
8172                 }
8173             }
8174             if (status == NO_ERROR) {
8175                 readInputParameters_l();
8176                 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
8177             }
8178         }
8179     }
8180 
8181     return reconfig;
8182 }
8183 
getParameters(const String8 & keys)8184 String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
8185 {
8186     Mutex::Autolock _l(mLock);
8187     if (initCheck() == NO_ERROR) {
8188         String8 out_s8;
8189         if (mInput->stream->getParameters(keys, &out_s8) == OK) {
8190             return out_s8;
8191         }
8192     }
8193     return String8();
8194 }
8195 
ioConfigChanged(audio_io_config_event event,pid_t pid,audio_port_handle_t portId)8196 void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
8197                                                  audio_port_handle_t portId) {
8198     sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
8199 
8200     desc->mIoHandle = mId;
8201 
8202     switch (event) {
8203     case AUDIO_INPUT_OPENED:
8204     case AUDIO_INPUT_REGISTERED:
8205     case AUDIO_INPUT_CONFIG_CHANGED:
8206         desc->mPatch = mPatch;
8207         desc->mChannelMask = mChannelMask;
8208         desc->mSamplingRate = mSampleRate;
8209         desc->mFormat = mFormat;
8210         desc->mFrameCount = mFrameCount;
8211         desc->mFrameCountHAL = mFrameCount;
8212         desc->mLatency = 0;
8213         break;
8214     case AUDIO_CLIENT_STARTED:
8215         desc->mPatch = mPatch;
8216         desc->mPortId = portId;
8217         break;
8218     case AUDIO_INPUT_CLOSED:
8219     default:
8220         break;
8221     }
8222     mAudioFlinger->ioConfigChanged(event, desc, pid);
8223 }
8224 
readInputParameters_l()8225 void AudioFlinger::RecordThread::readInputParameters_l()
8226 {
8227     status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
8228     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
8229     mFormat = mHALFormat;
8230     mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
8231     if (audio_is_linear_pcm(mFormat)) {
8232         LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_8, "HAL channel count %d > %d",
8233                 mChannelCount, FCC_8);
8234     } else {
8235         // Can have more that FCC_8 channels in encoded streams.
8236         ALOGI("HAL format %#x is not linear pcm", mFormat);
8237     }
8238     result = mInput->stream->getFrameSize(&mFrameSize);
8239     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
8240     result = mInput->stream->getBufferSize(&mBufferSize);
8241     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
8242     mFrameCount = mBufferSize / mFrameSize;
8243     ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, "
8244             "mBufferSize=%lld, mFrameCount=%lld",
8245             this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize,
8246             (long long)mFrameCount);
8247     // This is the formula for calculating the temporary buffer size.
8248     // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to
8249     // 1 full output buffer, regardless of the alignment of the available input.
8250     // The value is somewhat arbitrary, and could probably be even larger.
8251     // A larger value should allow more old data to be read after a track calls start(),
8252     // without increasing latency.
8253     //
8254     // Note this is independent of the maximum downsampling ratio permitted for capture.
8255     mRsmpInFrames = mFrameCount * 7;
8256     mRsmpInFramesP2 = roundup(mRsmpInFrames);
8257     free(mRsmpInBuffer);
8258     mRsmpInBuffer = NULL;
8259 
8260     // TODO optimize audio capture buffer sizes ...
8261     // Here we calculate the size of the sliding buffer used as a source
8262     // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7).
8263     // For current HAL frame counts, this is usually 2048 = 40 ms.  It would
8264     // be better to have it derived from the pipe depth in the long term.
8265     // The current value is higher than necessary.  However it should not add to latency.
8266 
8267     // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer
8268     mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1;
8269     (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize);
8270     // if posix_memalign fails, will segv here.
8271     memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize);
8272 
8273     // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints.
8274     // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks?
8275 }
8276 
getInputFramesLost()8277 uint32_t AudioFlinger::RecordThread::getInputFramesLost()
8278 {
8279     Mutex::Autolock _l(mLock);
8280     uint32_t result;
8281     if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) {
8282         return result;
8283     }
8284     return 0;
8285 }
8286 
sessionIds() const8287 KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const
8288 {
8289     KeyedVector<audio_session_t, bool> ids;
8290     Mutex::Autolock _l(mLock);
8291     for (size_t j = 0; j < mTracks.size(); ++j) {
8292         sp<RecordThread::RecordTrack> track = mTracks[j];
8293         audio_session_t sessionId = track->sessionId();
8294         if (ids.indexOfKey(sessionId) < 0) {
8295             ids.add(sessionId, true);
8296         }
8297     }
8298     return ids;
8299 }
8300 
clearInput()8301 AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
8302 {
8303     Mutex::Autolock _l(mLock);
8304     AudioStreamIn *input = mInput;
8305     mInput = NULL;
8306     return input;
8307 }
8308 
8309 // this method must always be called either with ThreadBase mLock held or inside the thread loop
stream() const8310 sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const
8311 {
8312     if (mInput == NULL) {
8313         return NULL;
8314     }
8315     return mInput->stream;
8316 }
8317 
addEffectChain_l(const sp<EffectChain> & chain)8318 status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
8319 {
8320     ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
8321     chain->setThread(this);
8322     chain->setInBuffer(NULL);
8323     chain->setOutBuffer(NULL);
8324 
8325     checkSuspendOnAddEffectChain_l(chain);
8326 
8327     // make sure enabled pre processing effects state is communicated to the HAL as we
8328     // just moved them to a new input stream.
8329     chain->syncHalEffectsState();
8330 
8331     mEffectChains.add(chain);
8332 
8333     return NO_ERROR;
8334 }
8335 
removeEffectChain_l(const sp<EffectChain> & chain)8336 size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
8337 {
8338     ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
8339 
8340     for (size_t i = 0; i < mEffectChains.size(); i++) {
8341         if (chain == mEffectChains[i]) {
8342             mEffectChains.removeAt(i);
8343             break;
8344         }
8345     }
8346     return mEffectChains.size();
8347 }
8348 
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)8349 status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch,
8350                                                           audio_patch_handle_t *handle)
8351 {
8352     status_t status = NO_ERROR;
8353 
8354     // store new device and send to effects
8355     mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type;
8356     mInDeviceTypeAddr.mAddress = patch->sources[0].ext.device.address;
8357     audio_port_handle_t deviceId = patch->sources[0].id;
8358     for (size_t i = 0; i < mEffectChains.size(); i++) {
8359         mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr());
8360     }
8361 
8362     checkBtNrec_l();
8363 
8364     // store new source and send to effects
8365     if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
8366         mAudioSource = patch->sinks[0].ext.mix.usecase.source;
8367         for (size_t i = 0; i < mEffectChains.size(); i++) {
8368             mEffectChains[i]->setAudioSource_l(mAudioSource);
8369         }
8370     }
8371 
8372     if (mInput->audioHwDev->supportsAudioPatches()) {
8373         sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
8374         status = hwDevice->createAudioPatch(patch->num_sources,
8375                                             patch->sources,
8376                                             patch->num_sinks,
8377                                             patch->sinks,
8378                                             handle);
8379     } else {
8380         char *address;
8381         if (strcmp(patch->sources[0].ext.device.address, "") != 0) {
8382             address = audio_device_address_to_parameter(
8383                                                 patch->sources[0].ext.device.type,
8384                                                 patch->sources[0].ext.device.address);
8385         } else {
8386             address = (char *)calloc(1, 1);
8387         }
8388         AudioParameter param = AudioParameter(String8(address));
8389         free(address);
8390         param.addInt(String8(AudioParameter::keyRouting),
8391                      (int)patch->sources[0].ext.device.type);
8392         param.addInt(String8(AudioParameter::keyInputSource),
8393                                          (int)patch->sinks[0].ext.mix.usecase.source);
8394         status = mInput->stream->setParameters(param.toString());
8395         *handle = AUDIO_PATCH_HANDLE_NONE;
8396     }
8397 
8398     if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) {
8399         sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
8400         mPatch = *patch;
8401     }
8402 
8403     return status;
8404 }
8405 
releaseAudioPatch_l(const audio_patch_handle_t handle)8406 status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
8407 {
8408     status_t status = NO_ERROR;
8409 
8410     mPatch = audio_patch{};
8411     mInDeviceTypeAddr.reset();
8412 
8413     if (mInput->audioHwDev->supportsAudioPatches()) {
8414         sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
8415         status = hwDevice->releaseAudioPatch(handle);
8416     } else {
8417         AudioParameter param;
8418         param.addInt(String8(AudioParameter::keyRouting), 0);
8419         status = mInput->stream->setParameters(param.toString());
8420     }
8421     return status;
8422 }
8423 
updateOutDevices(const DeviceDescriptorBaseVector & outDevices)8424 void AudioFlinger::RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices)
8425 {
8426     mOutDevices = outDevices;
8427     mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices);
8428     for (size_t i = 0; i < mEffectChains.size(); i++) {
8429         mEffectChains[i]->setDevices_l(outDeviceTypeAddrs());
8430     }
8431 }
8432 
addPatchTrack(const sp<PatchRecord> & record)8433 void AudioFlinger::RecordThread::addPatchTrack(const sp<PatchRecord>& record)
8434 {
8435     Mutex::Autolock _l(mLock);
8436     mTracks.add(record);
8437     if (record->getSource()) {
8438         mSource = record->getSource();
8439     }
8440 }
8441 
deletePatchTrack(const sp<PatchRecord> & record)8442 void AudioFlinger::RecordThread::deletePatchTrack(const sp<PatchRecord>& record)
8443 {
8444     Mutex::Autolock _l(mLock);
8445     if (mSource == record->getSource()) {
8446         mSource = mInput;
8447     }
8448     destroyTrack_l(record);
8449 }
8450 
toAudioPortConfig(struct audio_port_config * config)8451 void AudioFlinger::RecordThread::toAudioPortConfig(struct audio_port_config *config)
8452 {
8453     ThreadBase::toAudioPortConfig(config);
8454     config->role = AUDIO_PORT_ROLE_SINK;
8455     config->ext.mix.hw_module = mInput->audioHwDev->handle();
8456     config->ext.mix.usecase.source = mAudioSource;
8457     if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
8458         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
8459         config->flags.input = mInput->flags;
8460     }
8461 }
8462 
8463 // ----------------------------------------------------------------------------
8464 //      Mmap
8465 // ----------------------------------------------------------------------------
8466 
MmapThreadHandle(const sp<MmapThread> & thread)8467 AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread)
8468     : mThread(thread)
8469 {
8470     assert(thread != 0); // thread must start non-null and stay non-null
8471 }
8472 
~MmapThreadHandle()8473 AudioFlinger::MmapThreadHandle::~MmapThreadHandle()
8474 {
8475     mThread->disconnect();
8476 }
8477 
createMmapBuffer(int32_t minSizeFrames,struct audio_mmap_buffer_info * info)8478 status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames,
8479                                   struct audio_mmap_buffer_info *info)
8480 {
8481     return mThread->createMmapBuffer(minSizeFrames, info);
8482 }
8483 
getMmapPosition(struct audio_mmap_position * position)8484 status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position)
8485 {
8486     return mThread->getMmapPosition(position);
8487 }
8488 
start(const AudioClient & client,audio_port_handle_t * handle)8489 status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client,
8490         audio_port_handle_t *handle)
8491 
8492 {
8493     return mThread->start(client, handle);
8494 }
8495 
stop(audio_port_handle_t handle)8496 status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle)
8497 {
8498     return mThread->stop(handle);
8499 }
8500 
standby()8501 status_t AudioFlinger::MmapThreadHandle::standby()
8502 {
8503     return mThread->standby();
8504 }
8505 
8506 
MmapThread(const sp<AudioFlinger> & audioFlinger,audio_io_handle_t id,AudioHwDevice * hwDev,sp<StreamHalInterface> stream,bool systemReady)8507 AudioFlinger::MmapThread::MmapThread(
8508         const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
8509         AudioHwDevice *hwDev, sp<StreamHalInterface> stream, bool systemReady)
8510     : ThreadBase(audioFlinger, id, MMAP, systemReady),
8511       mSessionId(AUDIO_SESSION_NONE),
8512       mPortId(AUDIO_PORT_HANDLE_NONE),
8513       mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev),
8514       mActiveTracks(&this->mLocalLog),
8515       mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later.
8516       mNoCallbackWarningCount(0)
8517 {
8518     mStandby = true;
8519     readHalParameters_l();
8520 }
8521 
~MmapThread()8522 AudioFlinger::MmapThread::~MmapThread()
8523 {
8524     releaseWakeLock_l();
8525 }
8526 
onFirstRef()8527 void AudioFlinger::MmapThread::onFirstRef()
8528 {
8529     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
8530 }
8531 
disconnect()8532 void AudioFlinger::MmapThread::disconnect()
8533 {
8534     ActiveTracks<MmapTrack> activeTracks;
8535     {
8536         Mutex::Autolock _l(mLock);
8537         for (const sp<MmapTrack> &t : mActiveTracks) {
8538             activeTracks.add(t);
8539         }
8540     }
8541     for (const sp<MmapTrack> &t : activeTracks) {
8542         stop(t->portId());
8543     }
8544     // This will decrement references and may cause the destruction of this thread.
8545     if (isOutput()) {
8546         AudioSystem::releaseOutput(mPortId);
8547     } else {
8548         AudioSystem::releaseInput(mPortId);
8549     }
8550 }
8551 
8552 
configure(const audio_attributes_t * attr,audio_stream_type_t streamType __unused,audio_session_t sessionId,const sp<MmapStreamCallback> & callback,audio_port_handle_t deviceId,audio_port_handle_t portId)8553 void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr,
8554                                                 audio_stream_type_t streamType __unused,
8555                                                 audio_session_t sessionId,
8556                                                 const sp<MmapStreamCallback>& callback,
8557                                                 audio_port_handle_t deviceId,
8558                                                 audio_port_handle_t portId)
8559 {
8560     mAttr = *attr;
8561     mSessionId = sessionId;
8562     mCallback = callback;
8563     mDeviceId = deviceId;
8564     mPortId = portId;
8565 }
8566 
createMmapBuffer(int32_t minSizeFrames,struct audio_mmap_buffer_info * info)8567 status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames,
8568                                   struct audio_mmap_buffer_info *info)
8569 {
8570     if (mHalStream == 0) {
8571         return NO_INIT;
8572     }
8573     mStandby = true;
8574     acquireWakeLock();
8575     return mHalStream->createMmapBuffer(minSizeFrames, info);
8576 }
8577 
getMmapPosition(struct audio_mmap_position * position)8578 status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position)
8579 {
8580     if (mHalStream == 0) {
8581         return NO_INIT;
8582     }
8583     return mHalStream->getMmapPosition(position);
8584 }
8585 
exitStandby()8586 status_t AudioFlinger::MmapThread::exitStandby()
8587 {
8588     status_t ret = mHalStream->start();
8589     if (ret != NO_ERROR) {
8590         ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret);
8591         return ret;
8592     }
8593     mStandby = false;
8594     return NO_ERROR;
8595 }
8596 
start(const AudioClient & client,audio_port_handle_t * handle)8597 status_t AudioFlinger::MmapThread::start(const AudioClient& client,
8598                                          audio_port_handle_t *handle)
8599 {
8600     ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__,
8601           client.clientUid, mStandby, mPortId, *handle);
8602     if (mHalStream == 0) {
8603         return NO_INIT;
8604     }
8605 
8606     status_t ret;
8607 
8608     if (*handle == mPortId) {
8609         // for the first track, reuse portId and session allocated when the stream was opened
8610         return exitStandby();
8611     }
8612 
8613     audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
8614 
8615     audio_io_handle_t io = mId;
8616     if (isOutput()) {
8617         audio_config_t config = AUDIO_CONFIG_INITIALIZER;
8618         config.sample_rate = mSampleRate;
8619         config.channel_mask = mChannelMask;
8620         config.format = mFormat;
8621         audio_stream_type_t stream = streamType();
8622         audio_output_flags_t flags =
8623                 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT);
8624         audio_port_handle_t deviceId = mDeviceId;
8625         std::vector<audio_io_handle_t> secondaryOutputs;
8626         ret = AudioSystem::getOutputForAttr(&mAttr, &io,
8627                                             mSessionId,
8628                                             &stream,
8629                                             client.clientPid,
8630                                             client.clientUid,
8631                                             &config,
8632                                             flags,
8633                                             &deviceId,
8634                                             &portId,
8635                                             &secondaryOutputs);
8636         ALOGD_IF(!secondaryOutputs.empty(),
8637                  "MmapThread::start does not support secondary outputs, ignoring them");
8638     } else {
8639         audio_config_base_t config;
8640         config.sample_rate = mSampleRate;
8641         config.channel_mask = mChannelMask;
8642         config.format = mFormat;
8643         audio_port_handle_t deviceId = mDeviceId;
8644         ret = AudioSystem::getInputForAttr(&mAttr, &io,
8645                                               RECORD_RIID_INVALID,
8646                                               mSessionId,
8647                                               client.clientPid,
8648                                               client.clientUid,
8649                                               client.packageName,
8650                                               &config,
8651                                               AUDIO_INPUT_FLAG_MMAP_NOIRQ,
8652                                               &deviceId,
8653                                               &portId);
8654     }
8655     // APM should not chose a different input or output stream for the same set of attributes
8656     // and audo configuration
8657     if (ret != NO_ERROR || io != mId) {
8658         ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)",
8659               __FUNCTION__, ret, io, mId);
8660         return BAD_VALUE;
8661     }
8662 
8663     if (isOutput()) {
8664         ret = AudioSystem::startOutput(portId);
8665     } else {
8666         ret = AudioSystem::startInput(portId);
8667     }
8668 
8669     Mutex::Autolock _l(mLock);
8670     // abort if start is rejected by audio policy manager
8671     if (ret != NO_ERROR) {
8672         ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret);
8673         if (!mActiveTracks.isEmpty()) {
8674             mLock.unlock();
8675             if (isOutput()) {
8676                 AudioSystem::releaseOutput(portId);
8677             } else {
8678                 AudioSystem::releaseInput(portId);
8679             }
8680             mLock.lock();
8681         } else {
8682             mHalStream->stop();
8683         }
8684         return PERMISSION_DENIED;
8685     }
8686 
8687     // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ?
8688     sp<MmapTrack> track = new MmapTrack(this, mAttr, mSampleRate, mFormat, mChannelMask, mSessionId,
8689                                         isOutput(), client.clientUid, client.clientPid,
8690                                         IPCThreadState::self()->getCallingPid(), portId);
8691 
8692     if (isOutput()) {
8693         // force volume update when a new track is added
8694         mHalVolFloat = -1.0f;
8695     } else if (!track->isSilenced_l()) {
8696         for (const sp<MmapTrack> &t : mActiveTracks) {
8697             if (t->isSilenced_l() && t->uid() != client.clientUid)
8698                 t->invalidate();
8699         }
8700     }
8701 
8702 
8703     mActiveTracks.add(track);
8704     sp<EffectChain> chain = getEffectChain_l(mSessionId);
8705     if (chain != 0) {
8706         chain->setStrategy(AudioSystem::getStrategyForStream(streamType()));
8707         chain->incTrackCnt();
8708         chain->incActiveTrackCnt();
8709     }
8710 
8711     *handle = portId;
8712     broadcast_l();
8713 
8714     ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get());
8715 
8716     return NO_ERROR;
8717 }
8718 
stop(audio_port_handle_t handle)8719 status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle)
8720 {
8721     ALOGV("%s handle %d", __FUNCTION__, handle);
8722 
8723     if (mHalStream == 0) {
8724         return NO_INIT;
8725     }
8726 
8727     if (handle == mPortId) {
8728         mHalStream->stop();
8729         return NO_ERROR;
8730     }
8731 
8732     Mutex::Autolock _l(mLock);
8733 
8734     sp<MmapTrack> track;
8735     for (const sp<MmapTrack> &t : mActiveTracks) {
8736         if (handle == t->portId()) {
8737             track = t;
8738             break;
8739         }
8740     }
8741     if (track == 0) {
8742         return BAD_VALUE;
8743     }
8744 
8745     mActiveTracks.remove(track);
8746 
8747     mLock.unlock();
8748     if (isOutput()) {
8749         AudioSystem::stopOutput(track->portId());
8750         AudioSystem::releaseOutput(track->portId());
8751     } else {
8752         AudioSystem::stopInput(track->portId());
8753         AudioSystem::releaseInput(track->portId());
8754     }
8755     mLock.lock();
8756 
8757     sp<EffectChain> chain = getEffectChain_l(track->sessionId());
8758     if (chain != 0) {
8759         chain->decActiveTrackCnt();
8760         chain->decTrackCnt();
8761     }
8762 
8763     broadcast_l();
8764 
8765     return NO_ERROR;
8766 }
8767 
standby()8768 status_t AudioFlinger::MmapThread::standby()
8769 {
8770     ALOGV("%s", __FUNCTION__);
8771 
8772     if (mHalStream == 0) {
8773         return NO_INIT;
8774     }
8775     if (!mActiveTracks.isEmpty()) {
8776         return INVALID_OPERATION;
8777     }
8778     mHalStream->standby();
8779     mStandby = true;
8780     releaseWakeLock();
8781     return NO_ERROR;
8782 }
8783 
8784 
readHalParameters_l()8785 void AudioFlinger::MmapThread::readHalParameters_l()
8786 {
8787     status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
8788     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
8789     mFormat = mHALFormat;
8790     LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat);
8791     result = mHalStream->getFrameSize(&mFrameSize);
8792     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
8793     result = mHalStream->getBufferSize(&mBufferSize);
8794     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
8795     mFrameCount = mBufferSize / mFrameSize;
8796 }
8797 
threadLoop()8798 bool AudioFlinger::MmapThread::threadLoop()
8799 {
8800     checkSilentMode_l();
8801 
8802     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
8803 
8804     while (!exitPending())
8805     {
8806         Vector< sp<EffectChain> > effectChains;
8807 
8808         { // under Thread lock
8809         Mutex::Autolock _l(mLock);
8810 
8811         if (mSignalPending) {
8812             // A signal was raised while we were unlocked
8813             mSignalPending = false;
8814         } else {
8815             if (mConfigEvents.isEmpty()) {
8816                 // we're about to wait, flush the binder command buffer
8817                 IPCThreadState::self()->flushCommands();
8818 
8819                 if (exitPending()) {
8820                     break;
8821                 }
8822 
8823                 // wait until we have something to do...
8824                 ALOGV("%s going to sleep", myName.string());
8825                 mWaitWorkCV.wait(mLock);
8826                 ALOGV("%s waking up", myName.string());
8827 
8828                 checkSilentMode_l();
8829 
8830                 continue;
8831             }
8832         }
8833 
8834         processConfigEvents_l();
8835 
8836         processVolume_l();
8837 
8838         checkInvalidTracks_l();
8839 
8840         mActiveTracks.updatePowerState(this);
8841 
8842         updateMetadata_l();
8843 
8844         lockEffectChains_l(effectChains);
8845         } // release Thread lock
8846 
8847         for (size_t i = 0; i < effectChains.size(); i ++) {
8848             effectChains[i]->process_l(); // Thread is not locked, but effect chain is locked
8849         }
8850 
8851         // enable changes in effect chain, including moving to another thread.
8852         unlockEffectChains(effectChains);
8853         // Effect chains will be actually deleted here if they were removed from
8854         // mEffectChains list during mixing or effects processing
8855     }
8856 
8857     threadLoop_exit();
8858 
8859     if (!mStandby) {
8860         threadLoop_standby();
8861         mStandby = true;
8862     }
8863 
8864     ALOGV("Thread %p type %d exiting", this, mType);
8865     return false;
8866 }
8867 
8868 // checkForNewParameter_l() must be called with ThreadBase::mLock held
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)8869 bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair,
8870                                                               status_t& status)
8871 {
8872     AudioParameter param = AudioParameter(keyValuePair);
8873     int value;
8874     bool sendToHal = true;
8875     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
8876         LOG_FATAL("Should not happen set routing device in MmapThread");
8877     }
8878     if (sendToHal) {
8879         status = mHalStream->setParameters(keyValuePair);
8880     } else {
8881         status = NO_ERROR;
8882     }
8883 
8884     return false;
8885 }
8886 
getParameters(const String8 & keys)8887 String8 AudioFlinger::MmapThread::getParameters(const String8& keys)
8888 {
8889     Mutex::Autolock _l(mLock);
8890     String8 out_s8;
8891     if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) {
8892         return out_s8;
8893     }
8894     return String8();
8895 }
8896 
ioConfigChanged(audio_io_config_event event,pid_t pid,audio_port_handle_t portId __unused)8897 void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
8898                                                audio_port_handle_t portId __unused) {
8899     sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
8900 
8901     desc->mIoHandle = mId;
8902 
8903     switch (event) {
8904     case AUDIO_INPUT_OPENED:
8905     case AUDIO_INPUT_REGISTERED:
8906     case AUDIO_INPUT_CONFIG_CHANGED:
8907     case AUDIO_OUTPUT_OPENED:
8908     case AUDIO_OUTPUT_REGISTERED:
8909     case AUDIO_OUTPUT_CONFIG_CHANGED:
8910         desc->mPatch = mPatch;
8911         desc->mChannelMask = mChannelMask;
8912         desc->mSamplingRate = mSampleRate;
8913         desc->mFormat = mFormat;
8914         desc->mFrameCount = mFrameCount;
8915         desc->mFrameCountHAL = mFrameCount;
8916         desc->mLatency = 0;
8917         break;
8918 
8919     case AUDIO_INPUT_CLOSED:
8920     case AUDIO_OUTPUT_CLOSED:
8921     default:
8922         break;
8923     }
8924     mAudioFlinger->ioConfigChanged(event, desc, pid);
8925 }
8926 
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)8927 status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch,
8928                                                           audio_patch_handle_t *handle)
8929 {
8930     status_t status = NO_ERROR;
8931 
8932     // store new device and send to effects
8933     audio_devices_t type = AUDIO_DEVICE_NONE;
8934     audio_port_handle_t deviceId;
8935     AudioDeviceTypeAddrVector sinkDeviceTypeAddrs;
8936     AudioDeviceTypeAddr sourceDeviceTypeAddr;
8937     uint32_t numDevices = 0;
8938     if (isOutput()) {
8939         for (unsigned int i = 0; i < patch->num_sinks; i++) {
8940             LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1
8941                                 && !mAudioHwDev->supportsAudioPatches(),
8942                                 "Enumerated device type(%#x) must not be used "
8943                                 "as it does not support audio patches",
8944                                 patch->sinks[i].ext.device.type);
8945             type |= patch->sinks[i].ext.device.type;
8946             sinkDeviceTypeAddrs.push_back(AudioDeviceTypeAddr(patch->sinks[i].ext.device.type,
8947                     patch->sinks[i].ext.device.address));
8948         }
8949         deviceId = patch->sinks[0].id;
8950         numDevices = mPatch.num_sinks;
8951     } else {
8952         type = patch->sources[0].ext.device.type;
8953         deviceId = patch->sources[0].id;
8954         numDevices = mPatch.num_sources;
8955         sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type;
8956         sourceDeviceTypeAddr.mAddress = patch->sources[0].ext.device.address;
8957     }
8958 
8959     for (size_t i = 0; i < mEffectChains.size(); i++) {
8960         if (isOutput()) {
8961             mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs);
8962         } else {
8963             mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr);
8964         }
8965     }
8966 
8967     if (!isOutput()) {
8968         // store new source and send to effects
8969         if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
8970             mAudioSource = patch->sinks[0].ext.mix.usecase.source;
8971             for (size_t i = 0; i < mEffectChains.size(); i++) {
8972                 mEffectChains[i]->setAudioSource_l(mAudioSource);
8973             }
8974         }
8975     }
8976 
8977     if (mAudioHwDev->supportsAudioPatches()) {
8978         status = mHalDevice->createAudioPatch(patch->num_sources,
8979                                             patch->sources,
8980                                             patch->num_sinks,
8981                                             patch->sinks,
8982                                             handle);
8983     } else {
8984         char *address;
8985         if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
8986             //FIXME: we only support address on first sink with HAL version < 3.0
8987             address = audio_device_address_to_parameter(
8988                                                         patch->sinks[0].ext.device.type,
8989                                                         patch->sinks[0].ext.device.address);
8990         } else {
8991             address = (char *)calloc(1, 1);
8992         }
8993         AudioParameter param = AudioParameter(String8(address));
8994         free(address);
8995         param.addInt(String8(AudioParameter::keyRouting), (int)type);
8996         if (!isOutput()) {
8997             param.addInt(String8(AudioParameter::keyInputSource),
8998                                          (int)patch->sinks[0].ext.mix.usecase.source);
8999         }
9000         status = mHalStream->setParameters(param.toString());
9001         *handle = AUDIO_PATCH_HANDLE_NONE;
9002     }
9003 
9004     if (numDevices == 0 || mDeviceId != deviceId) {
9005         if (isOutput()) {
9006             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
9007             mOutDeviceTypeAddrs = sinkDeviceTypeAddrs;
9008         } else {
9009             sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
9010             mInDeviceTypeAddr = sourceDeviceTypeAddr;
9011         }
9012         sp<MmapStreamCallback> callback = mCallback.promote();
9013         if (mDeviceId != deviceId && callback != 0) {
9014             mLock.unlock();
9015             callback->onRoutingChanged(deviceId);
9016             mLock.lock();
9017         }
9018         mPatch = *patch;
9019         mDeviceId = deviceId;
9020     }
9021     return status;
9022 }
9023 
releaseAudioPatch_l(const audio_patch_handle_t handle)9024 status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
9025 {
9026     status_t status = NO_ERROR;
9027 
9028     mPatch = audio_patch{};
9029     mOutDeviceTypeAddrs.clear();
9030     mInDeviceTypeAddr.reset();
9031 
9032     bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ?
9033                                         supportsAudioPatches : false;
9034 
9035     if (supportsAudioPatches) {
9036         status = mHalDevice->releaseAudioPatch(handle);
9037     } else {
9038         AudioParameter param;
9039         param.addInt(String8(AudioParameter::keyRouting), 0);
9040         status = mHalStream->setParameters(param.toString());
9041     }
9042     return status;
9043 }
9044 
toAudioPortConfig(struct audio_port_config * config)9045 void AudioFlinger::MmapThread::toAudioPortConfig(struct audio_port_config *config)
9046 {
9047     ThreadBase::toAudioPortConfig(config);
9048     if (isOutput()) {
9049         config->role = AUDIO_PORT_ROLE_SOURCE;
9050         config->ext.mix.hw_module = mAudioHwDev->handle();
9051         config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
9052     } else {
9053         config->role = AUDIO_PORT_ROLE_SINK;
9054         config->ext.mix.hw_module = mAudioHwDev->handle();
9055         config->ext.mix.usecase.source = mAudioSource;
9056     }
9057 }
9058 
addEffectChain_l(const sp<EffectChain> & chain)9059 status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain)
9060 {
9061     audio_session_t session = chain->sessionId();
9062 
9063     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
9064     // Attach all tracks with same session ID to this chain.
9065     // indicate all active tracks in the chain
9066     for (const sp<MmapTrack> &track : mActiveTracks) {
9067         if (session == track->sessionId()) {
9068             chain->incTrackCnt();
9069             chain->incActiveTrackCnt();
9070         }
9071     }
9072 
9073     chain->setThread(this);
9074     chain->setInBuffer(nullptr);
9075     chain->setOutBuffer(nullptr);
9076     chain->syncHalEffectsState();
9077 
9078     mEffectChains.add(chain);
9079     checkSuspendOnAddEffectChain_l(chain);
9080     return NO_ERROR;
9081 }
9082 
removeEffectChain_l(const sp<EffectChain> & chain)9083 size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain)
9084 {
9085     audio_session_t session = chain->sessionId();
9086 
9087     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
9088 
9089     for (size_t i = 0; i < mEffectChains.size(); i++) {
9090         if (chain == mEffectChains[i]) {
9091             mEffectChains.removeAt(i);
9092             // detach all active tracks from the chain
9093             // detach all tracks with same session ID from this chain
9094             for (const sp<MmapTrack> &track : mActiveTracks) {
9095                 if (session == track->sessionId()) {
9096                     chain->decActiveTrackCnt();
9097                     chain->decTrackCnt();
9098                 }
9099             }
9100             break;
9101         }
9102     }
9103     return mEffectChains.size();
9104 }
9105 
threadLoop_standby()9106 void AudioFlinger::MmapThread::threadLoop_standby()
9107 {
9108     mHalStream->standby();
9109 }
9110 
threadLoop_exit()9111 void AudioFlinger::MmapThread::threadLoop_exit()
9112 {
9113     // Do not call callback->onTearDown() because it is redundant for thread exit
9114     // and because it can cause a recursive mutex lock on stop().
9115 }
9116 
setSyncEvent(const sp<SyncEvent> & event __unused)9117 status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused)
9118 {
9119     return BAD_VALUE;
9120 }
9121 
isValidSyncEvent(const sp<SyncEvent> & event __unused) const9122 bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
9123 {
9124     return false;
9125 }
9126 
checkEffectCompatibility_l(const effect_descriptor_t * desc,audio_session_t sessionId)9127 status_t AudioFlinger::MmapThread::checkEffectCompatibility_l(
9128         const effect_descriptor_t *desc, audio_session_t sessionId)
9129 {
9130     // No global effect sessions on mmap threads
9131     if (audio_is_global_session(sessionId)) {
9132         ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s",
9133                 desc->name, mThreadName);
9134         return BAD_VALUE;
9135     }
9136 
9137     if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) {
9138         ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread",
9139                 desc->name);
9140         return BAD_VALUE;
9141     }
9142     if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
9143         ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap "
9144               "thread", desc->name);
9145         return BAD_VALUE;
9146     }
9147 
9148     // Only allow effects without processing load or latency
9149     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) {
9150         return BAD_VALUE;
9151     }
9152 
9153     return NO_ERROR;
9154 }
9155 
checkInvalidTracks_l()9156 void AudioFlinger::MmapThread::checkInvalidTracks_l()
9157 {
9158     for (const sp<MmapTrack> &track : mActiveTracks) {
9159         if (track->isInvalid()) {
9160             sp<MmapStreamCallback> callback = mCallback.promote();
9161             if (callback != 0) {
9162                 mLock.unlock();
9163                 callback->onTearDown(track->portId());
9164                 mLock.lock();
9165             } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
9166                 ALOGW("Could not notify MMAP stream tear down: no onTearDown callback!");
9167                 mNoCallbackWarningCount++;
9168             }
9169         }
9170     }
9171 }
9172 
dumpInternals_l(int fd,const Vector<String16> & args __unused)9173 void AudioFlinger::MmapThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
9174 {
9175     dprintf(fd, "  Attributes: content type %d usage %d source %d\n",
9176             mAttr.content_type, mAttr.usage, mAttr.source);
9177     dprintf(fd, "  Session: %d port Id: %d\n", mSessionId, mPortId);
9178     if (mActiveTracks.isEmpty()) {
9179         dprintf(fd, "  No active clients\n");
9180     }
9181 }
9182 
dumpTracks_l(int fd,const Vector<String16> & args __unused)9183 void AudioFlinger::MmapThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
9184 {
9185     String8 result;
9186     size_t numtracks = mActiveTracks.size();
9187     dprintf(fd, "  %zu Tracks\n", numtracks);
9188     const char *prefix = "    ";
9189     if (numtracks) {
9190         result.append(prefix);
9191         mActiveTracks[0]->appendDumpHeader(result);
9192         for (size_t i = 0; i < numtracks ; ++i) {
9193             sp<MmapTrack> track = mActiveTracks[i];
9194             result.append(prefix);
9195             track->appendDump(result, true /* active */);
9196         }
9197     } else {
9198         dprintf(fd, "\n");
9199     }
9200     write(fd, result.string(), result.size());
9201 }
9202 
MmapPlaybackThread(const sp<AudioFlinger> & audioFlinger,audio_io_handle_t id,AudioHwDevice * hwDev,AudioStreamOut * output,bool systemReady)9203 AudioFlinger::MmapPlaybackThread::MmapPlaybackThread(
9204         const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
9205         AudioHwDevice *hwDev,  AudioStreamOut *output, bool systemReady)
9206     : MmapThread(audioFlinger, id, hwDev, output->stream, systemReady),
9207       mStreamType(AUDIO_STREAM_MUSIC),
9208       mStreamVolume(1.0),
9209       mStreamMute(false),
9210       mOutput(output)
9211 {
9212     snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id);
9213     mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
9214     mMasterVolume = audioFlinger->masterVolume_l();
9215     mMasterMute = audioFlinger->masterMute_l();
9216     if (mAudioHwDev) {
9217         if (mAudioHwDev->canSetMasterVolume()) {
9218             mMasterVolume = 1.0;
9219         }
9220 
9221         if (mAudioHwDev->canSetMasterMute()) {
9222             mMasterMute = false;
9223         }
9224     }
9225 }
9226 
configure(const audio_attributes_t * attr,audio_stream_type_t streamType,audio_session_t sessionId,const sp<MmapStreamCallback> & callback,audio_port_handle_t deviceId,audio_port_handle_t portId)9227 void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr,
9228                                                 audio_stream_type_t streamType,
9229                                                 audio_session_t sessionId,
9230                                                 const sp<MmapStreamCallback>& callback,
9231                                                 audio_port_handle_t deviceId,
9232                                                 audio_port_handle_t portId)
9233 {
9234     MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId);
9235     mStreamType = streamType;
9236 }
9237 
clearOutput()9238 AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput()
9239 {
9240     Mutex::Autolock _l(mLock);
9241     AudioStreamOut *output = mOutput;
9242     mOutput = NULL;
9243     return output;
9244 }
9245 
setMasterVolume(float value)9246 void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value)
9247 {
9248     Mutex::Autolock _l(mLock);
9249     // Don't apply master volume in SW if our HAL can do it for us.
9250     if (mAudioHwDev &&
9251             mAudioHwDev->canSetMasterVolume()) {
9252         mMasterVolume = 1.0;
9253     } else {
9254         mMasterVolume = value;
9255     }
9256 }
9257 
setMasterMute(bool muted)9258 void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted)
9259 {
9260     Mutex::Autolock _l(mLock);
9261     // Don't apply master mute in SW if our HAL can do it for us.
9262     if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) {
9263         mMasterMute = false;
9264     } else {
9265         mMasterMute = muted;
9266     }
9267 }
9268 
setStreamVolume(audio_stream_type_t stream,float value)9269 void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
9270 {
9271     Mutex::Autolock _l(mLock);
9272     if (stream == mStreamType) {
9273         mStreamVolume = value;
9274         broadcast_l();
9275     }
9276 }
9277 
streamVolume(audio_stream_type_t stream) const9278 float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const
9279 {
9280     Mutex::Autolock _l(mLock);
9281     if (stream == mStreamType) {
9282         return mStreamVolume;
9283     }
9284     return 0.0f;
9285 }
9286 
setStreamMute(audio_stream_type_t stream,bool muted)9287 void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
9288 {
9289     Mutex::Autolock _l(mLock);
9290     if (stream == mStreamType) {
9291         mStreamMute= muted;
9292         broadcast_l();
9293     }
9294 }
9295 
invalidateTracks(audio_stream_type_t streamType)9296 void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType)
9297 {
9298     Mutex::Autolock _l(mLock);
9299     if (streamType == mStreamType) {
9300         for (const sp<MmapTrack> &track : mActiveTracks) {
9301             track->invalidate();
9302         }
9303         broadcast_l();
9304     }
9305 }
9306 
processVolume_l()9307 void AudioFlinger::MmapPlaybackThread::processVolume_l()
9308 {
9309     float volume;
9310 
9311     if (mMasterMute || mStreamMute) {
9312         volume = 0;
9313     } else {
9314         volume = mMasterVolume * mStreamVolume;
9315     }
9316 
9317     if (volume != mHalVolFloat) {
9318 
9319         // Convert volumes from float to 8.24
9320         uint32_t vol = (uint32_t)(volume * (1 << 24));
9321 
9322         // Delegate volume control to effect in track effect chain if needed
9323         // only one effect chain can be present on DirectOutputThread, so if
9324         // there is one, the track is connected to it
9325         if (!mEffectChains.isEmpty()) {
9326             mEffectChains[0]->setVolume_l(&vol, &vol);
9327             volume = (float)vol / (1 << 24);
9328         }
9329         // Try to use HW volume control and fall back to SW control if not implemented
9330         if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) {
9331             mHalVolFloat = volume; // HW volume control worked, so update value.
9332             mNoCallbackWarningCount = 0;
9333         } else {
9334             sp<MmapStreamCallback> callback = mCallback.promote();
9335             if (callback != 0) {
9336                 int channelCount;
9337                 if (isOutput()) {
9338                     channelCount = audio_channel_count_from_out_mask(mChannelMask);
9339                 } else {
9340                     channelCount = audio_channel_count_from_in_mask(mChannelMask);
9341                 }
9342                 Vector<float> values;
9343                 for (int i = 0; i < channelCount; i++) {
9344                     values.add(volume);
9345                 }
9346                 mHalVolFloat = volume; // SW volume control worked, so update value.
9347                 mNoCallbackWarningCount = 0;
9348                 mLock.unlock();
9349                 callback->onVolumeChanged(mChannelMask, values);
9350                 mLock.lock();
9351             } else {
9352                 if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
9353                     ALOGW("Could not set MMAP stream volume: no volume callback!");
9354                     mNoCallbackWarningCount++;
9355                 }
9356             }
9357         }
9358     }
9359 }
9360 
updateMetadata_l()9361 void AudioFlinger::MmapPlaybackThread::updateMetadata_l()
9362 {
9363     if (mOutput == nullptr || mOutput->stream == nullptr ||
9364             !mActiveTracks.readAndClearHasChanged()) {
9365         return;
9366     }
9367     StreamOutHalInterface::SourceMetadata metadata;
9368     for (const sp<MmapTrack> &track : mActiveTracks) {
9369         // No track is invalid as this is called after prepareTrack_l in the same critical section
9370         metadata.tracks.push_back({
9371                 .usage = track->attributes().usage,
9372                 .content_type = track->attributes().content_type,
9373                 .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume
9374         });
9375     }
9376     mOutput->stream->updateSourceMetadata(metadata);
9377 }
9378 
checkSilentMode_l()9379 void AudioFlinger::MmapPlaybackThread::checkSilentMode_l()
9380 {
9381     if (!mMasterMute) {
9382         char value[PROPERTY_VALUE_MAX];
9383         if (property_get("ro.audio.silent", value, "0") > 0) {
9384             char *endptr;
9385             unsigned long ul = strtoul(value, &endptr, 0);
9386             if (*endptr == '\0' && ul != 0) {
9387                 ALOGD("Silence is golden");
9388                 // The setprop command will not allow a property to be changed after
9389                 // the first time it is set, so we don't have to worry about un-muting.
9390                 setMasterMute_l(true);
9391             }
9392         }
9393     }
9394 }
9395 
toAudioPortConfig(struct audio_port_config * config)9396 void AudioFlinger::MmapPlaybackThread::toAudioPortConfig(struct audio_port_config *config)
9397 {
9398     MmapThread::toAudioPortConfig(config);
9399     if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
9400         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
9401         config->flags.output = mOutput->flags;
9402     }
9403 }
9404 
dumpInternals_l(int fd,const Vector<String16> & args)9405 void AudioFlinger::MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args)
9406 {
9407     MmapThread::dumpInternals_l(fd, args);
9408 
9409     dprintf(fd, "  Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n",
9410             mStreamType, mStreamVolume, mHalVolFloat, mStreamMute);
9411     dprintf(fd, "  Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute);
9412 }
9413 
MmapCaptureThread(const sp<AudioFlinger> & audioFlinger,audio_io_handle_t id,AudioHwDevice * hwDev,AudioStreamIn * input,bool systemReady)9414 AudioFlinger::MmapCaptureThread::MmapCaptureThread(
9415         const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
9416         AudioHwDevice *hwDev,  AudioStreamIn *input, bool systemReady)
9417     : MmapThread(audioFlinger, id, hwDev, input->stream, systemReady),
9418       mInput(input)
9419 {
9420     snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id);
9421     mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
9422 }
9423 
exitStandby()9424 status_t AudioFlinger::MmapCaptureThread::exitStandby()
9425 {
9426     {
9427         // mInput might have been cleared by clearInput()
9428         Mutex::Autolock _l(mLock);
9429         if (mInput != nullptr && mInput->stream != nullptr) {
9430             mInput->stream->setGain(1.0f);
9431         }
9432     }
9433     return MmapThread::exitStandby();
9434 }
9435 
clearInput()9436 AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput()
9437 {
9438     Mutex::Autolock _l(mLock);
9439     AudioStreamIn *input = mInput;
9440     mInput = NULL;
9441     return input;
9442 }
9443 
9444 
processVolume_l()9445 void AudioFlinger::MmapCaptureThread::processVolume_l()
9446 {
9447     bool changed = false;
9448     bool silenced = false;
9449 
9450     sp<MmapStreamCallback> callback = mCallback.promote();
9451     if (callback == 0) {
9452         if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
9453             ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!");
9454             mNoCallbackWarningCount++;
9455         }
9456     }
9457 
9458     // After a change occurred in track silenced state, mute capture in audio DSP if at least one
9459     // track is silenced and unmute otherwise
9460     for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) {
9461         if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) {
9462             changed = true;
9463             silenced = mActiveTracks[i]->isSilenced_l();
9464         }
9465     }
9466 
9467     if (changed) {
9468         mInput->stream->setGain(silenced ? 0.0f: 1.0f);
9469     }
9470 }
9471 
updateMetadata_l()9472 void AudioFlinger::MmapCaptureThread::updateMetadata_l()
9473 {
9474     if (mInput == nullptr || mInput->stream == nullptr ||
9475             !mActiveTracks.readAndClearHasChanged()) {
9476         return;
9477     }
9478     StreamInHalInterface::SinkMetadata metadata;
9479     for (const sp<MmapTrack> &track : mActiveTracks) {
9480         // No track is invalid as this is called after prepareTrack_l in the same critical section
9481         metadata.tracks.push_back({
9482                 .source = track->attributes().source,
9483                 .gain = 1, // capture tracks do not have volumes
9484         });
9485     }
9486     mInput->stream->updateSinkMetadata(metadata);
9487 }
9488 
setRecordSilenced(uid_t uid,bool silenced)9489 void AudioFlinger::MmapCaptureThread::setRecordSilenced(uid_t uid, bool silenced)
9490 {
9491     Mutex::Autolock _l(mLock);
9492     for (size_t i = 0; i < mActiveTracks.size() ; i++) {
9493         if (mActiveTracks[i]->uid() == uid) {
9494             mActiveTracks[i]->setSilenced_l(silenced);
9495             broadcast_l();
9496         }
9497     }
9498 }
9499 
toAudioPortConfig(struct audio_port_config * config)9500 void AudioFlinger::MmapCaptureThread::toAudioPortConfig(struct audio_port_config *config)
9501 {
9502     MmapThread::toAudioPortConfig(config);
9503     if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
9504         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
9505         config->flags.input = mInput->flags;
9506     }
9507 }
9508 
9509 } // namespace android
9510