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