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