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