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