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