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 "Threads.h"
24
25 #include "Client.h"
26 #include "IAfEffect.h"
27 #include "MelReporter.h"
28 #include "ResamplerBufferProvider.h"
29
30 #include <afutils/FallibleLockGuard.h>
31 #include <afutils/Permission.h>
32 #include <afutils/TypedLogger.h>
33 #include <afutils/Vibrator.h>
34 #include <audio_utils/MelProcessor.h>
35 #include <audio_utils/Metadata.h>
36 #include <audio_utils/Trace.h>
37 #ifdef DEBUG_CPU_USAGE
38 #include <audio_utils/Statistics.h>
39 #include <cpustats/ThreadCpuUsage.h>
40 #endif
41 #include <audio_utils/channels.h>
42 #include <audio_utils/format.h>
43 #include <audio_utils/minifloat.h>
44 #include <audio_utils/mono_blend.h>
45 #include <audio_utils/primitives.h>
46 #include <audio_utils/safe_math.h>
47 #include <audiomanager/AudioManager.h>
48 #include <binder/IPCThreadState.h>
49 #include <binder/IServiceManager.h>
50 #include <binder/PersistableBundle.h>
51 #include <com_android_media_audio.h>
52 #include <com_android_media_audioserver.h>
53 #include <cutils/bitops.h>
54 #include <cutils/properties.h>
55 #include <fastpath/AutoPark.h>
56 #include <media/AudioContainers.h>
57 #include <media/AudioDeviceTypeAddr.h>
58 #include <media/AudioParameter.h>
59 #include <media/AudioResamplerPublic.h>
60 #ifdef ADD_BATTERY_DATA
61 #include <media/IMediaPlayerService.h>
62 #include <media/IMediaDeathNotifier.h>
63 #endif
64 #include <media/MmapStreamCallback.h>
65 #include <media/RecordBufferConverter.h>
66 #include <media/TypeConverter.h>
67 #include <media/audiohal/EffectsFactoryHalInterface.h>
68 #include <media/audiohal/StreamHalInterface.h>
69 #include <media/nbaio/AudioStreamInSource.h>
70 #include <media/nbaio/AudioStreamOutSink.h>
71 #include <media/nbaio/MonoPipe.h>
72 #include <media/nbaio/MonoPipeReader.h>
73 #include <media/nbaio/Pipe.h>
74 #include <media/nbaio/PipeReader.h>
75 #include <media/nbaio/SourceAudioBufferProvider.h>
76 #include <media/ValidatedAttributionSourceState.h>
77 #include <mediautils/BatteryNotifier.h>
78 #include <mediautils/Process.h>
79 #include <mediautils/SchedulingPolicyService.h>
80 #include <mediautils/ServiceUtilities.h>
81 #include <powermanager/PowerManager.h>
82 #include <private/android_filesystem_config.h>
83 #include <private/media/AudioTrackShared.h>
84 #include <psh_utils/AudioPowerManager.h>
85 #include <system/audio_effects/effect_aec.h>
86 #include <system/audio_effects/effect_downmix.h>
87 #include <system/audio_effects/effect_ns.h>
88 #include <system/audio_effects/effect_spatializer.h>
89 #include <utils/Log.h>
90 #include <utils/Trace.h>
91
92 #include <fcntl.h>
93 #include <linux/futex.h>
94 #include <math.h>
95 #include <memory>
96 #include <pthread.h>
97 #include <sstream>
98 #include <string>
99 #include <sys/stat.h>
100 #include <sys/syscall.h>
101
102 // ----------------------------------------------------------------------------
103
104 // Note: the following macro is used for extremely verbose logging message. In
105 // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
106 // 0; but one side effect of this is to turn all LOGV's as well. Some messages
107 // are so verbose that we want to suppress them even when we have ALOG_ASSERT
108 // turned on. Do not uncomment the #def below unless you really know what you
109 // are doing and want to see all of the extremely verbose messages.
110 //#define VERY_VERY_VERBOSE_LOGGING
111 #ifdef VERY_VERY_VERBOSE_LOGGING
112 #define ALOGVV ALOGV
113 #else
114 #define ALOGVV(a...) do { } while(0)
115 #endif
116
117 // TODO: Move these macro/inlines to a header file.
118 #define max(a, b) ((a) > (b) ? (a) : (b))
119
120 template <typename T>
min(const T & a,const T & b)121 static inline T min(const T& a, const T& b)
122 {
123 return a < b ? a : b;
124 }
125
126 using com::android::media::audio::audioserver_permissions;
127 using com::android::media::permission::PermissionEnum::CAPTURE_AUDIO_HOTWORD;
128 using com::android::media::permission::ValidatedAttributionSourceState;
129 namespace audioserver_flags = com::android::media::audioserver;
130
131 namespace android {
132
133 using audioflinger::SyncEvent;
134 using media::IEffectClient;
135 using content::AttributionSourceState;
136
137 // Keep in sync with java definition in media/java/android/media/AudioRecord.java
138 static constexpr int32_t kMaxSharedAudioHistoryMs = 5000;
139
140 // retry counts for buffer fill timeout
141 // 50 * ~20msecs = 1 second
142 static const int8_t kMaxTrackRetries = 50;
143 static const int8_t kMaxTrackStartupRetries = 50;
144
145 // allow less retry attempts on direct output thread.
146 // direct outputs can be a scarce resource in audio hardware and should
147 // be released as quickly as possible.
148 // Notes:
149 // 1) The retry duration kMaxTrackRetriesDirectMs may be increased
150 // in case the data write is bursty for the AudioTrack. The application
151 // should endeavor to write at least once every kMaxTrackRetriesDirectMs
152 // to prevent an underrun situation. If the data is bursty, then
153 // the application can also throttle the data sent to be even.
154 // 2) For compressed audio data, any data present in the AudioTrack buffer
155 // will be sent and reset the retry count. This delivers data as
156 // it arrives, with approximately kDirectMinSleepTimeUs = 10ms checking interval.
157 // 3) For linear PCM or proportional PCM, we wait one period for a period's worth
158 // of data to be available, then any remaining data is delivered.
159 // This is required to ensure the last bit of data is delivered before underrun.
160 //
161 // Sleep time per cycle is kDirectMinSleepTimeUs for compressed tracks
162 // or the size of the HAL period for proportional / linear PCM tracks.
163 static const int32_t kMaxTrackRetriesDirectMs = 200;
164
165 // don't warn about blocked writes or record buffer overflows more often than this
166 static const nsecs_t kWarningThrottleNs = seconds(5);
167
168 // RecordThread loop sleep time upon application overrun or audio HAL read error
169 static const int kRecordThreadSleepUs = 5000;
170
171 // maximum time to wait in sendConfigEvent_l() for a status to be received
172 static const nsecs_t kConfigEventTimeoutNs = seconds(2);
173 // longer timeout for create audio patch to account for specific scenarii
174 // with Bluetooth devices
175 static const nsecs_t kCreatePatchEventTimeoutNs = seconds(4);
176
177 // minimum sleep time for the mixer thread loop when tracks are active but in underrun
178 static const uint32_t kMinThreadSleepTimeUs = 5000;
179 // maximum divider applied to the active sleep time in the mixer thread loop
180 static const uint32_t kMaxThreadSleepTimeShift = 2;
181
182 // minimum normal sink buffer size, expressed in milliseconds rather than frames
183 // FIXME This should be based on experimentally observed scheduling jitter
184 static const uint32_t kMinNormalSinkBufferSizeMs = 20;
185 // maximum normal sink buffer size
186 static const uint32_t kMaxNormalSinkBufferSizeMs = 24;
187
188 // minimum capture buffer size in milliseconds to _not_ need a fast capture thread
189 // FIXME This should be based on experimentally observed scheduling jitter
190 static const uint32_t kMinNormalCaptureBufferSizeMs = 12;
191
192 // Offloaded output thread standby delay: allows track transition without going to standby
193 static const nsecs_t kOffloadStandbyDelayNs = seconds(1);
194
195 // Direct output thread minimum sleep time in idle or active(underrun) state
196 static const nsecs_t kDirectMinSleepTimeUs = 10000;
197
198 // Minimum amount of time between checking to see if the timestamp is advancing
199 // for underrun detection. If we check too frequently, we may not detect a
200 // timestamp update and will falsely detect underrun.
201 static constexpr nsecs_t kMinimumTimeBetweenTimestampChecksNs = 150 /* ms */ * 1'000'000;
202
203 // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good
204 // balance between power consumption and latency, and allows threads to be scheduled reliably
205 // by the CFS scheduler.
206 // FIXME Express other hardcoded references to 20ms with references to this constant and move
207 // it appropriately.
208 #define FMS_20 20
209
210 // Whether to use fast mixer
211 static const enum {
212 FastMixer_Never, // never initialize or use: for debugging only
213 FastMixer_Always, // always initialize and use, even if not needed: for debugging only
214 // normal mixer multiplier is 1
215 FastMixer_Static, // initialize if needed, then use all the time if initialized,
216 // multiplier is calculated based on min & max normal mixer buffer size
217 FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load,
218 // multiplier is calculated based on min & max normal mixer buffer size
219 // FIXME for FastMixer_Dynamic:
220 // Supporting this option will require fixing HALs that can't handle large writes.
221 // For example, one HAL implementation returns an error from a large write,
222 // and another HAL implementation corrupts memory, possibly in the sample rate converter.
223 // We could either fix the HAL implementations, or provide a wrapper that breaks
224 // up large writes into smaller ones, and the wrapper would need to deal with scheduler.
225 } kUseFastMixer = FastMixer_Static;
226
227 // Whether to use fast capture
228 static const enum {
229 FastCapture_Never, // never initialize or use: for debugging only
230 FastCapture_Always, // always initialize and use, even if not needed: for debugging only
231 FastCapture_Static, // initialize if needed, then use all the time if initialized
232 } kUseFastCapture = FastCapture_Static;
233
234 // Priorities for requestPriority
235 static const int kPriorityAudioApp = 2;
236 static const int kPriorityFastMixer = 3;
237 static const int kPriorityFastCapture = 3;
238 // Request real-time priority for PlaybackThread in ARC
239 static const int kPriorityPlaybackThreadArc = 1;
240
241 // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the
242 // track buffer in shared memory. Zero on input means to use a default value. For fast tracks,
243 // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'.
244
245 // This is the default value, if not specified by property.
246 static const int kFastTrackMultiplier = 2;
247
248 // The minimum and maximum allowed values
249 static const int kFastTrackMultiplierMin = 1;
250 static const int kFastTrackMultiplierMax = 2;
251
252 // The actual value to use, which can be specified per-device via property af.fast_track_multiplier.
253 static int sFastTrackMultiplier = kFastTrackMultiplier;
254
255 // See Thread::readOnlyHeap().
256 // Initially this heap is used to allocate client buffers for "fast" AudioRecord.
257 // Eventually it will be the single buffer that FastCapture writes into via HAL read(),
258 // and that all "fast" AudioRecord clients read from. In either case, the size can be small.
259 static const size_t kRecordThreadReadOnlyHeapSize = 0xD000;
260
261 static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3);
262
getStandbyTimeInNanos()263 static nsecs_t getStandbyTimeInNanos() {
264 static nsecs_t standbyTimeInNanos = []() {
265 const int ms = property_get_int32("ro.audio.flinger_standbytime_ms",
266 kDefaultStandbyTimeInNsecs / NANOS_PER_MILLISECOND);
267 ALOGI("%s: Using %d ms as standby time", __func__, ms);
268 return milliseconds(ms);
269 }();
270 return standbyTimeInNanos;
271 }
272
273 // Set kEnableExtendedChannels to true to enable greater than stereo output
274 // for the MixerThread and device sink. Number of channels allowed is
275 // FCC_2 <= channels <= FCC_LIMIT.
276 constexpr bool kEnableExtendedChannels = true;
277
278 // Returns true if channel mask is permitted for the PCM sink in the MixerThread
279 /* static */
isValidPcmSinkChannelMask(audio_channel_mask_t channelMask)280 bool IAfThreadBase::isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) {
281 switch (audio_channel_mask_get_representation(channelMask)) {
282 case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
283 // Haptic channel mask is only applicable for channel position mask.
284 const uint32_t channelCount = audio_channel_count_from_out_mask(
285 static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL));
286 const uint32_t maxChannelCount = kEnableExtendedChannels
287 ? FCC_LIMIT : FCC_2;
288 if (channelCount < FCC_2 // mono is not supported at this time
289 || channelCount > maxChannelCount) {
290 return false;
291 }
292 // check that channelMask is the "canonical" one we expect for the channelCount.
293 return audio_channel_position_mask_is_out_canonical(channelMask);
294 }
295 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
296 if (kEnableExtendedChannels) {
297 const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
298 if (channelCount >= FCC_2 // mono is not supported at this time
299 && channelCount <= FCC_LIMIT) {
300 return true;
301 }
302 }
303 return false;
304 default:
305 return false;
306 }
307 }
308
309 // Set kEnableExtendedPrecision to true to use extended precision in MixerThread
310 constexpr bool kEnableExtendedPrecision = true;
311
312 // Returns true if format is permitted for the PCM sink in the MixerThread
313 /* static */
isValidPcmSinkFormat(audio_format_t format)314 bool IAfThreadBase::isValidPcmSinkFormat(audio_format_t format) {
315 switch (format) {
316 case AUDIO_FORMAT_PCM_16_BIT:
317 return true;
318 case AUDIO_FORMAT_PCM_FLOAT:
319 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
320 case AUDIO_FORMAT_PCM_32_BIT:
321 case AUDIO_FORMAT_PCM_8_24_BIT:
322 return kEnableExtendedPrecision;
323 default:
324 return false;
325 }
326 }
327
328 // ----------------------------------------------------------------------------
329
330 // formatToString() needs to be exact for MediaMetrics purposes.
331 // Do not use media/TypeConverter.h toString().
332 /* static */
formatToString(audio_format_t format)333 std::string IAfThreadBase::formatToString(audio_format_t format) {
334 std::string result;
335 FormatConverter::toString(format, result);
336 return result;
337 }
338
339 // TODO: move all toString helpers to audio.h
340 // under #ifdef __cplusplus #endif
patchSinksToString(const struct audio_patch * patch)341 static std::string patchSinksToString(const struct audio_patch *patch)
342 {
343 std::string s;
344 for (size_t i = 0; i < patch->num_sinks; ++i) {
345 if (i > 0) s.append("|");
346 if (patch->sinks[i].ext.device.address[0]) {
347 s.append("(").append(toString(patch->sinks[i].ext.device.type))
348 .append(", ").append(patch->sinks[i].ext.device.address).append(")");
349 } else {
350 s.append(toString(patch->sinks[i].ext.device.type));
351 }
352 }
353 return s;
354 }
355
patchSourcesToString(const struct audio_patch * patch)356 static std::string patchSourcesToString(const struct audio_patch *patch)
357 {
358 std::string s;
359 for (size_t i = 0; i < patch->num_sources; ++i) {
360 if (i > 0) s.append("|");
361 if (patch->sources[i].ext.device.address[0]) {
362 s.append("(").append(toString(patch->sources[i].ext.device.type))
363 .append(", ").append(patch->sources[i].ext.device.address).append(")");
364 } else {
365 s.append(toString(patch->sources[i].ext.device.type));
366 }
367 }
368 return s;
369 }
370
toString(audio_latency_mode_t mode)371 static std::string toString(audio_latency_mode_t mode) {
372 // We convert to the AIDL type to print (eventually the legacy type will be removed).
373 const auto result = legacy2aidl_audio_latency_mode_t_AudioLatencyMode(mode);
374 return result.has_value() ? media::audio::common::toString(*result) : "UNKNOWN";
375 }
376
377 // Could be made a template, but other toString overloads for std::vector are confused.
toString(const std::vector<audio_latency_mode_t> & elements)378 static std::string toString(const std::vector<audio_latency_mode_t>& elements) {
379 std::string s("{ ");
380 for (const auto& e : elements) {
381 s.append(toString(e));
382 s.append(" ");
383 }
384 s.append("}");
385 return s;
386 }
387
388 static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT;
389
sFastTrackMultiplierInit()390 static void sFastTrackMultiplierInit()
391 {
392 char value[PROPERTY_VALUE_MAX];
393 if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
394 char *endptr;
395 unsigned long ul = strtoul(value, &endptr, 0);
396 if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
397 sFastTrackMultiplier = (int) ul;
398 }
399 }
400 }
401
402 // ----------------------------------------------------------------------------
403
404 #ifdef ADD_BATTERY_DATA
405 // To collect the amplifier usage
addBatteryData(uint32_t params)406 static void addBatteryData(uint32_t params) {
407 sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
408 if (service == NULL) {
409 // it already logged
410 return;
411 }
412
413 service->addBatteryData(params);
414 }
415 #endif
416
417 // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset
418 struct {
419 // call when you acquire a partial wakelock
acquireandroid::__anona8ab89a20408420 void acquire(const sp<IBinder> &wakeLockToken) {
421 pthread_mutex_lock(&mLock);
422 if (wakeLockToken.get() == nullptr) {
423 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
424 } else {
425 if (mCount == 0) {
426 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
427 }
428 ++mCount;
429 }
430 pthread_mutex_unlock(&mLock);
431 }
432
433 // call when you release a partial wakelock.
releaseandroid::__anona8ab89a20408434 void release(const sp<IBinder> &wakeLockToken) {
435 if (wakeLockToken.get() == nullptr) {
436 return;
437 }
438 pthread_mutex_lock(&mLock);
439 if (--mCount < 0) {
440 ALOGE("negative wakelock count");
441 mCount = 0;
442 }
443 pthread_mutex_unlock(&mLock);
444 }
445
446 // retrieves the boottime timebase offset from monotonic.
getBoottimeOffsetandroid::__anona8ab89a20408447 int64_t getBoottimeOffset() {
448 pthread_mutex_lock(&mLock);
449 int64_t boottimeOffset = mBoottimeOffset;
450 pthread_mutex_unlock(&mLock);
451 return boottimeOffset;
452 }
453
454 // Adjusts the timebase offset between TIMEBASE_MONOTONIC
455 // and the selected timebase.
456 // Currently only TIMEBASE_BOOTTIME is allowed.
457 //
458 // This only needs to be called upon acquiring the first partial wakelock
459 // after all other partial wakelocks are released.
460 //
461 // We do an empirical measurement of the offset rather than parsing
462 // /proc/timer_list since the latter is not a formal kernel ABI.
adjustTimebaseOffsetandroid::__anona8ab89a20408463 static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) {
464 int clockbase;
465 switch (timebase) {
466 case ExtendedTimestamp::TIMEBASE_BOOTTIME:
467 clockbase = SYSTEM_TIME_BOOTTIME;
468 break;
469 default:
470 LOG_ALWAYS_FATAL("invalid timebase %d", timebase);
471 break;
472 }
473 // try three times to get the clock offset, choose the one
474 // with the minimum gap in measurements.
475 const int tries = 3;
476 nsecs_t bestGap = 0, measured = 0; // not required, initialized for clang-tidy
477 for (int i = 0; i < tries; ++i) {
478 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
479 const nsecs_t tbase = systemTime(clockbase);
480 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
481 const nsecs_t gap = tmono2 - tmono;
482 if (i == 0 || gap < bestGap) {
483 bestGap = gap;
484 measured = tbase - ((tmono + tmono2) >> 1);
485 }
486 }
487
488 // to avoid micro-adjusting, we don't change the timebase
489 // unless it is significantly different.
490 //
491 // Assumption: It probably takes more than toleranceNs to
492 // suspend and resume the device.
493 static int64_t toleranceNs = 10000; // 10 us
494 if (llabs(*offset - measured) > toleranceNs) {
495 ALOGV("Adjusting timebase offset old: %lld new: %lld",
496 (long long)*offset, (long long)measured);
497 *offset = measured;
498 }
499 }
500
501 pthread_mutex_t mLock;
502 int32_t mCount;
503 int64_t mBoottimeOffset;
504 } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization
505
506 // ----------------------------------------------------------------------------
507 // CPU Stats
508 // ----------------------------------------------------------------------------
509
510 class CpuStats {
511 public:
512 CpuStats();
513 void sample(const String8 &title);
514 #ifdef DEBUG_CPU_USAGE
515 private:
516 ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns
517 audio_utils::Statistics<double> mWcStats; // statistics on thread CPU usage in wall clock ns
518
519 audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles
520
521 int mCpuNum; // thread's current CPU number
522 int mCpukHz; // frequency of thread's current CPU in kHz
523 #endif
524 };
525
CpuStats()526 CpuStats::CpuStats()
527 #ifdef DEBUG_CPU_USAGE
528 : mCpuNum(-1), mCpukHz(-1)
529 #endif
530 {
531 }
532
sample(const String8 & title __unused)533 void CpuStats::sample(const String8 &title
534 #ifndef DEBUG_CPU_USAGE
535 __unused
536 #endif
537 ) {
538 #ifdef DEBUG_CPU_USAGE
539 // get current thread's delta CPU time in wall clock ns
540 double wcNs;
541 bool valid = mCpuUsage.sampleAndEnable(wcNs);
542
543 // record sample for wall clock statistics
544 if (valid) {
545 mWcStats.add(wcNs);
546 }
547
548 // get the current CPU number
549 int cpuNum = sched_getcpu();
550
551 // get the current CPU frequency in kHz
552 int cpukHz = mCpuUsage.getCpukHz(cpuNum);
553
554 // check if either CPU number or frequency changed
555 if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
556 mCpuNum = cpuNum;
557 mCpukHz = cpukHz;
558 // ignore sample for purposes of cycles
559 valid = false;
560 }
561
562 // if no change in CPU number or frequency, then record sample for cycle statistics
563 if (valid && mCpukHz > 0) {
564 const double cycles = wcNs * cpukHz * 0.000001;
565 mHzStats.add(cycles);
566 }
567
568 const unsigned n = mWcStats.getN();
569 // mCpuUsage.elapsed() is expensive, so don't call it every loop
570 if ((n & 127) == 1) {
571 const long long elapsed = mCpuUsage.elapsed();
572 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
573 const double perLoop = elapsed / (double) n;
574 const double perLoop100 = perLoop * 0.01;
575 const double perLoop1k = perLoop * 0.001;
576 const double mean = mWcStats.getMean();
577 const double stddev = mWcStats.getStdDev();
578 const double minimum = mWcStats.getMin();
579 const double maximum = mWcStats.getMax();
580 const double meanCycles = mHzStats.getMean();
581 const double stddevCycles = mHzStats.getStdDev();
582 const double minCycles = mHzStats.getMin();
583 const double maxCycles = mHzStats.getMax();
584 mCpuUsage.resetElapsed();
585 mWcStats.reset();
586 mHzStats.reset();
587 ALOGD("CPU usage for %s over past %.1f secs\n"
588 " (%u mixer loops at %.1f mean ms per loop):\n"
589 " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
590 " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
591 " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
592 title.c_str(),
593 elapsed * .000000001, n, perLoop * .000001,
594 mean * .001,
595 stddev * .001,
596 minimum * .001,
597 maximum * .001,
598 mean / perLoop100,
599 stddev / perLoop100,
600 minimum / perLoop100,
601 maximum / perLoop100,
602 meanCycles / perLoop1k,
603 stddevCycles / perLoop1k,
604 minCycles / perLoop1k,
605 maxCycles / perLoop1k);
606
607 }
608 }
609 #endif
610 };
611
612 // ----------------------------------------------------------------------------
613 // ThreadBase
614 // ----------------------------------------------------------------------------
615
616 // static
threadTypeToString(ThreadBase::type_t type)617 const char* IAfThreadBase::threadTypeToString(ThreadBase::type_t type)
618 {
619 switch (type) {
620 case MIXER:
621 return "MIXER";
622 case DIRECT:
623 return "DIRECT";
624 case DUPLICATING:
625 return "DUPLICATING";
626 case RECORD:
627 return "RECORD";
628 case OFFLOAD:
629 return "OFFLOAD";
630 case MMAP_PLAYBACK:
631 return "MMAP_PLAYBACK";
632 case MMAP_CAPTURE:
633 return "MMAP_CAPTURE";
634 case SPATIALIZER:
635 return "SPATIALIZER";
636 case BIT_PERFECT:
637 return "BIT_PERFECT";
638 case DIRECT_RECORD:
639 return "DIRECT_RECORD";
640 default:
641 return "unknown";
642 }
643 }
644
ThreadBase(const sp<IAfThreadCallback> & afThreadCallback,audio_io_handle_t id,type_t type,bool systemReady,bool isOut)645 ThreadBase::ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
646 type_t type, bool systemReady, bool isOut)
647 : Thread(false /*canCallJava*/),
648 mType(type),
649 mAfThreadCallback(afThreadCallback),
650 mThreadMetrics(std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) + std::to_string(id),
651 isOut),
652 mIsOut(isOut),
653 // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize
654 // are set by PlaybackThread::readOutputParameters_l() or
655 // RecordThread::readInputParameters_l()
656 //FIXME: mStandby should be true here. Is this some kind of hack?
657 mStandby(false),
658 mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id),
659 // mName will be set by concrete (non-virtual) subclass
660 mDeathRecipient(new PMDeathRecipient(this)),
661 mSystemReady(systemReady),
662 mSignalPending(false)
663 {
664 mThreadMetrics.logConstructor(getpid(), threadTypeToString(type), id);
665 memset(&mPatch, 0, sizeof(struct audio_patch));
666 }
667
~ThreadBase()668 ThreadBase::~ThreadBase()
669 {
670 // mConfigEvents should be empty, but just in case it isn't, free the memory it owns
671 mConfigEvents.clear();
672
673 // do not lock the mutex in destructor
674 releaseWakeLock_l();
675 if (mPowerManager != 0) {
676 sp<IBinder> binder = IInterface::asBinder(mPowerManager);
677 binder->unlinkToDeath(mDeathRecipient);
678 }
679
680 sendStatistics(true /* force */);
681 }
682
readyToRun()683 status_t ThreadBase::readyToRun()
684 {
685 status_t status = initCheck();
686 if (status == NO_ERROR) {
687 ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid());
688 } else {
689 ALOGE("No working audio driver found.");
690 }
691 return status;
692 }
693
exit()694 void ThreadBase::exit()
695 {
696 ALOGV("ThreadBase::exit");
697 // do any cleanup required for exit to succeed
698 preExit();
699 {
700 // This lock prevents the following race in thread (uniprocessor for illustration):
701 // if (!exitPending()) {
702 // // context switch from here to exit()
703 // // exit() calls requestExit(), what exitPending() observes
704 // // exit() calls signal(), which is dropped since no waiters
705 // // context switch back from exit() to here
706 // mWaitWorkCV.wait(...);
707 // // now thread is hung
708 // }
709 audio_utils::lock_guard lock(mutex());
710 requestExit();
711 mWaitWorkCV.notify_all();
712 }
713 // When Thread::requestExitAndWait is made virtual and this method is renamed to
714 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
715
716 // For TimeCheck: track waiting on the thread join of getTid().
717 audio_utils::mutex::scoped_join_wait_check sjw(getTid());
718
719 requestExitAndWait();
720 }
721
setParameters(const String8 & keyValuePairs)722 status_t ThreadBase::setParameters(const String8& keyValuePairs)
723 {
724 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.c_str());
725 audio_utils::lock_guard _l(mutex());
726
727 return sendSetParameterConfigEvent_l(keyValuePairs);
728 }
729
730 // sendConfigEvent_l() must be called with ThreadBase::mLock held
731 // Can temporarily release the lock if waiting for a reply from processConfigEvents_l().
sendConfigEvent_l(sp<ConfigEvent> & event)732 status_t ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event)
733 NO_THREAD_SAFETY_ANALYSIS // condition variable
734 {
735 status_t status = NO_ERROR;
736
737 if (event->mRequiresSystemReady && !mSystemReady) {
738 event->mWaitStatus = false;
739 mPendingConfigEvents.add(event);
740 return status;
741 }
742 mConfigEvents.add(event);
743 ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType);
744 mWaitWorkCV.notify_one();
745 mutex().unlock();
746 {
747 audio_utils::unique_lock _l(event->mutex());
748 nsecs_t timeoutNs = event->mType == CFG_EVENT_CREATE_AUDIO_PATCH ?
749 kCreatePatchEventTimeoutNs : kConfigEventTimeoutNs;
750 while (event->mWaitStatus) {
751 if (event->mCondition.wait_for(
752 _l, std::chrono::nanoseconds(timeoutNs), getTid())
753 == std::cv_status::timeout) {
754 event->mStatus = TIMED_OUT;
755 event->mWaitStatus = false;
756 }
757 }
758 status = event->mStatus;
759 }
760 mutex().lock();
761 return status;
762 }
763
sendIoConfigEvent(audio_io_config_event_t event,pid_t pid,audio_port_handle_t portId)764 void ThreadBase::sendIoConfigEvent(audio_io_config_event_t event, pid_t pid,
765 audio_port_handle_t portId)
766 {
767 audio_utils::lock_guard _l(mutex());
768 sendIoConfigEvent_l(event, pid, portId);
769 }
770
771 // sendIoConfigEvent_l() must be called with ThreadBase::mutex() held
sendIoConfigEvent_l(audio_io_config_event_t event,pid_t pid,audio_port_handle_t portId)772 void ThreadBase::sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid,
773 audio_port_handle_t portId)
774 {
775 // The audio statistics history is exponentially weighted to forget events
776 // about five or more seconds in the past. In order to have
777 // crisper statistics for mediametrics, we reset the statistics on
778 // an IoConfigEvent, to reflect different properties for a new device.
779 mIoJitterMs.reset();
780 mLatencyMs.reset();
781 mProcessTimeMs.reset();
782 mMonopipePipeDepthStats.reset();
783 mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS);
784
785 sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId);
786 sendConfigEvent_l(configEvent);
787 }
788
sendPrioConfigEvent(pid_t pid,pid_t tid,int32_t prio,bool forApp)789 void ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp)
790 {
791 audio_utils::lock_guard _l(mutex());
792 sendPrioConfigEvent_l(pid, tid, prio, forApp);
793 }
794
795 // sendPrioConfigEvent_l() must be called with ThreadBase::mutex() held
sendPrioConfigEvent_l(pid_t pid,pid_t tid,int32_t prio,bool forApp)796 void ThreadBase::sendPrioConfigEvent_l(
797 pid_t pid, pid_t tid, int32_t prio, bool forApp)
798 {
799 sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp);
800 sendConfigEvent_l(configEvent);
801 }
802
803 // sendSetParameterConfigEvent_l() must be called with ThreadBase::mutex() held
sendSetParameterConfigEvent_l(const String8 & keyValuePair)804 status_t ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair)
805 {
806 sp<ConfigEvent> configEvent;
807 AudioParameter param(keyValuePair);
808 int value;
809 if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) {
810 setMasterMono_l(value != 0);
811 if (param.size() == 1) {
812 return NO_ERROR; // should be a solo parameter - we don't pass down
813 }
814 param.remove(String8(AudioParameter::keyMonoOutput));
815 configEvent = new SetParameterConfigEvent(param.toString());
816 } else {
817 configEvent = new SetParameterConfigEvent(keyValuePair);
818 }
819 return sendConfigEvent_l(configEvent);
820 }
821
sendCreateAudioPatchConfigEvent(const struct audio_patch * patch,audio_patch_handle_t * handle)822 status_t ThreadBase::sendCreateAudioPatchConfigEvent(
823 const struct audio_patch *patch,
824 audio_patch_handle_t *handle)
825 {
826 audio_utils::lock_guard _l(mutex());
827 sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle);
828 status_t status = sendConfigEvent_l(configEvent);
829 if (status == NO_ERROR) {
830 CreateAudioPatchConfigEventData *data =
831 (CreateAudioPatchConfigEventData *)configEvent->mData.get();
832 *handle = data->mHandle;
833 }
834 return status;
835 }
836
sendReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle)837 status_t ThreadBase::sendReleaseAudioPatchConfigEvent(
838 const audio_patch_handle_t handle)
839 {
840 audio_utils::lock_guard _l(mutex());
841 sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle);
842 return sendConfigEvent_l(configEvent);
843 }
844
sendUpdateOutDeviceConfigEvent(const DeviceDescriptorBaseVector & outDevices)845 status_t ThreadBase::sendUpdateOutDeviceConfigEvent(
846 const DeviceDescriptorBaseVector& outDevices)
847 {
848 if (type() != RECORD) {
849 // The update out device operation is only for record thread.
850 return INVALID_OPERATION;
851 }
852 audio_utils::lock_guard _l(mutex());
853 sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices);
854 return sendConfigEvent_l(configEvent);
855 }
856
sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs)857 void ThreadBase::sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs)
858 {
859 ALOG_ASSERT(type() == RECORD, "sendResizeBufferConfigEvent_l() called on non record thread");
860 sp<ConfigEvent> configEvent =
861 (ConfigEvent *)new ResizeBufferConfigEvent(maxSharedAudioHistoryMs);
862 sendConfigEvent_l(configEvent);
863 }
864
sendCheckOutputStageEffectsEvent()865 void ThreadBase::sendCheckOutputStageEffectsEvent()
866 {
867 audio_utils::lock_guard _l(mutex());
868 sendCheckOutputStageEffectsEvent_l();
869 }
870
sendCheckOutputStageEffectsEvent_l()871 void ThreadBase::sendCheckOutputStageEffectsEvent_l()
872 {
873 sp<ConfigEvent> configEvent =
874 (ConfigEvent *)new CheckOutputStageEffectsEvent();
875 sendConfigEvent_l(configEvent);
876 }
877
sendHalLatencyModesChangedEvent_l()878 void ThreadBase::sendHalLatencyModesChangedEvent_l()
879 {
880 sp<ConfigEvent> configEvent = sp<HalLatencyModesChangedEvent>::make();
881 sendConfigEvent_l(configEvent);
882 }
883
884 // post condition: mConfigEvents.isEmpty()
processConfigEvents_l()885 void ThreadBase::processConfigEvents_l()
886 {
887 bool configChanged = false;
888
889 while (!mConfigEvents.isEmpty()) {
890 ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size());
891 sp<ConfigEvent> event = mConfigEvents[0];
892 mConfigEvents.removeAt(0);
893 switch (event->mType) {
894 case CFG_EVENT_PRIO: {
895 PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get();
896 // FIXME Need to understand why this has to be done asynchronously
897 int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp,
898 true /*asynchronous*/);
899 if (err != 0) {
900 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
901 data->mPrio, data->mPid, data->mTid, err);
902 }
903 } break;
904 case CFG_EVENT_IO: {
905 IoConfigEventData *data = (IoConfigEventData *)event->mData.get();
906 ioConfigChanged_l(data->mEvent, data->mPid, data->mPortId);
907 } break;
908 case CFG_EVENT_SET_PARAMETER: {
909 SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get();
910 if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) {
911 configChanged = true;
912 mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed",
913 data->mKeyValuePairs.c_str());
914 }
915 } break;
916 case CFG_EVENT_CREATE_AUDIO_PATCH: {
917 const DeviceTypeSet oldDevices = getDeviceTypes_l();
918 CreateAudioPatchConfigEventData *data =
919 (CreateAudioPatchConfigEventData *)event->mData.get();
920 event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle);
921 const DeviceTypeSet newDevices = getDeviceTypes_l();
922 configChanged = oldDevices != newDevices;
923 mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)",
924 dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(),
925 dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str());
926 } break;
927 case CFG_EVENT_RELEASE_AUDIO_PATCH: {
928 const DeviceTypeSet oldDevices = getDeviceTypes_l();
929 ReleaseAudioPatchConfigEventData *data =
930 (ReleaseAudioPatchConfigEventData *)event->mData.get();
931 event->mStatus = releaseAudioPatch_l(data->mHandle);
932 const DeviceTypeSet newDevices = getDeviceTypes_l();
933 configChanged = oldDevices != newDevices;
934 mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)",
935 dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(),
936 dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str());
937 } break;
938 case CFG_EVENT_UPDATE_OUT_DEVICE: {
939 UpdateOutDevicesConfigEventData *data =
940 (UpdateOutDevicesConfigEventData *)event->mData.get();
941 updateOutDevices(data->mOutDevices);
942 } break;
943 case CFG_EVENT_RESIZE_BUFFER: {
944 ResizeBufferConfigEventData *data =
945 (ResizeBufferConfigEventData *)event->mData.get();
946 resizeInputBuffer_l(data->mMaxSharedAudioHistoryMs);
947 } break;
948
949 case CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS: {
950 setCheckOutputStageEffects();
951 } break;
952
953 case CFG_EVENT_HAL_LATENCY_MODES_CHANGED: {
954 onHalLatencyModesChanged_l();
955 } break;
956
957 default:
958 ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType);
959 break;
960 }
961 {
962 audio_utils::lock_guard _l(event->mutex());
963 if (event->mWaitStatus) {
964 event->mWaitStatus = false;
965 event->mCondition.notify_one();
966 }
967 }
968 ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this);
969 }
970
971 if (configChanged) {
972 cacheParameters_l();
973 }
974 }
975
channelMaskToString(audio_channel_mask_t mask,bool output)976 String8 channelMaskToString(audio_channel_mask_t mask, bool output) {
977 String8 s;
978 const audio_channel_representation_t representation =
979 audio_channel_mask_get_representation(mask);
980
981 switch (representation) {
982 // Travel all single bit channel mask to convert channel mask to string.
983 case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
984 if (output) {
985 if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, ");
986 if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, ");
987 if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, ");
988 if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low-frequency, ");
989 if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, ");
990 if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, ");
991 if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, ");
992 if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, ");
993 if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, ");
994 if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, ");
995 if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, ");
996 if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,");
997 if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, ");
998 if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, ");
999 if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, ");
1000 if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, ");
1001 if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, ");
1002 if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, ");
1003 if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, ");
1004 if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, ");
1005 if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_LEFT) s.append("bottom-front-left, ");
1006 if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_CENTER) s.append("bottom-front-center, ");
1007 if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_RIGHT) s.append("bottom-front-right, ");
1008 if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2) s.append("low-frequency-2, ");
1009 if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, ");
1010 if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, ");
1011 if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, ");
1012 } else {
1013 if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, ");
1014 if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, ");
1015 if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, ");
1016 if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, ");
1017 if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, ");
1018 if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, ");
1019 if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, ");
1020 if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, ");
1021 if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, ");
1022 if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, ");
1023 if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, ");
1024 if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, ");
1025 if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, ");
1026 if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, ");
1027 if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, ");
1028 if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low-frequency, ");
1029 if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, ");
1030 if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, ");
1031 if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, ");
1032 if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, ");
1033 if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, ");
1034 }
1035 const int len = s.length();
1036 if (len > 2) {
1037 (void) s.lockBuffer(len); // needed?
1038 s.unlockBuffer(len - 2); // remove trailing ", "
1039 }
1040 return s;
1041 }
1042 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1043 s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask));
1044 return s;
1045 default:
1046 s.appendFormat("unknown mask, representation:%d bits:%#x",
1047 representation, audio_channel_mask_get_bits(mask));
1048 return s;
1049 }
1050 }
1051
dump(int fd,const Vector<String16> & args)1052 void ThreadBase::dump(int fd, const Vector<String16>& args)
1053 {
1054 dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input",
1055 this, mThreadName, getTid(), type(), threadTypeToString(type()));
1056
1057 {
1058 afutils::FallibleLockGuard l{mutex()};
1059 if (!l) {
1060 dprintf(fd, " Thread may be deadlocked\n");
1061 }
1062 dumpBase_l(fd, args);
1063 dumpInternals_l(fd, args);
1064 dumpTracks_l(fd, args);
1065 dumpEffectChains_l(fd, args);
1066 }
1067
1068 dprintf(fd, " Local log:\n");
1069 const auto logHeader = this->getLocalLogHeader();
1070 write(fd, logHeader.data(), logHeader.length());
1071 mLocalLog.dump(fd, " " /* prefix */);
1072
1073 // --all does the statistics
1074 bool dumpAll = false;
1075 for (const auto &arg : args) {
1076 if (arg == String16("--all")) {
1077 dumpAll = true;
1078 }
1079 }
1080 if (dumpAll || type() == SPATIALIZER) {
1081 const std::string sched = mThreadSnapshot.toString();
1082 if (!sched.empty()) {
1083 (void)write(fd, sched.c_str(), sched.size());
1084 }
1085 }
1086 }
1087
dumpBase_l(int fd,const Vector<String16> &)1088 void ThreadBase::dumpBase_l(int fd, const Vector<String16>& /* args */)
1089 {
1090 dprintf(fd, " I/O handle: %d\n", mId);
1091 dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no");
1092 dprintf(fd, " Sample rate: %u Hz\n", mSampleRate);
1093 dprintf(fd, " HAL frame count: %zu\n", mFrameCount);
1094 dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat,
1095 IAfThreadBase::formatToString(mHALFormat).c_str());
1096 dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize);
1097 dprintf(fd, " Channel count: %u\n", mChannelCount);
1098 dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask,
1099 channelMaskToString(mChannelMask, mType != RECORD).c_str());
1100 dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat,
1101 IAfThreadBase::formatToString(mFormat).c_str());
1102 dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize);
1103 dprintf(fd, " Pending config events:");
1104 size_t numConfig = mConfigEvents.size();
1105 if (numConfig) {
1106 const size_t SIZE = 256;
1107 char buffer[SIZE];
1108 for (size_t i = 0; i < numConfig; i++) {
1109 mConfigEvents[i]->dump(buffer, SIZE);
1110 dprintf(fd, "\n %s", buffer);
1111 }
1112 dprintf(fd, "\n");
1113 } else {
1114 dprintf(fd, " none\n");
1115 }
1116 // Note: output device may be used by capture threads for effects such as AEC.
1117 dprintf(fd, " Output devices: %s (%s)\n",
1118 dumpDeviceTypes(outDeviceTypes_l()).c_str(), toString(outDeviceTypes_l()).c_str());
1119 dprintf(fd, " Input device: %#x (%s)\n",
1120 inDeviceType_l(), toString(inDeviceType_l()).c_str());
1121 dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str());
1122
1123 // Dump timestamp statistics for the Thread types that support it.
1124 if (mType == RECORD
1125 || mType == MIXER
1126 || mType == DUPLICATING
1127 || mType == DIRECT
1128 || mType == OFFLOAD
1129 || mType == SPATIALIZER) {
1130 dprintf(fd, " Timestamp stats: %s\n", mTimestampVerifier.toString().c_str());
1131 dprintf(fd, " Timestamp corrected: %s\n",
1132 isTimestampCorrectionEnabled_l() ? "yes" : "no");
1133 }
1134
1135 if (mLastIoBeginNs > 0) { // MMAP may not set this
1136 dprintf(fd, " Last %s occurred (msecs): %lld\n",
1137 isOutput() ? "write" : "read",
1138 (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND);
1139 }
1140
1141 if (mProcessTimeMs.getN() > 0) {
1142 dprintf(fd, " Process time ms stats: %s\n", mProcessTimeMs.toString().c_str());
1143 }
1144
1145 if (mIoJitterMs.getN() > 0) {
1146 dprintf(fd, " Hal %s jitter ms stats: %s\n",
1147 isOutput() ? "write" : "read",
1148 mIoJitterMs.toString().c_str());
1149 }
1150
1151 if (mLatencyMs.getN() > 0) {
1152 dprintf(fd, " Threadloop %s latency stats: %s\n",
1153 isOutput() ? "write" : "read",
1154 mLatencyMs.toString().c_str());
1155 }
1156
1157 if (mMonopipePipeDepthStats.getN() > 0) {
1158 dprintf(fd, " Monopipe %s pipe depth stats: %s\n",
1159 isOutput() ? "write" : "read",
1160 mMonopipePipeDepthStats.toString().c_str());
1161 }
1162 }
1163
dumpEffectChains_l(int fd,const Vector<String16> & args)1164 void ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args)
1165 {
1166 const size_t SIZE = 256;
1167 char buffer[SIZE];
1168
1169 size_t numEffectChains = mEffectChains.size();
1170 snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains);
1171 write(fd, buffer, strlen(buffer));
1172
1173 for (size_t i = 0; i < numEffectChains; ++i) {
1174 sp<IAfEffectChain> chain = mEffectChains[i];
1175 if (chain != 0) {
1176 chain->dump(fd, args);
1177 }
1178 }
1179 }
1180
acquireWakeLock()1181 void ThreadBase::acquireWakeLock()
1182 {
1183 audio_utils::lock_guard _l(mutex());
1184 acquireWakeLock_l();
1185 }
1186
getWakeLockTag()1187 String16 ThreadBase::getWakeLockTag()
1188 {
1189 switch (mType) {
1190 case MIXER:
1191 return String16("AudioMix");
1192 case DIRECT:
1193 return String16("AudioDirectOut");
1194 case DUPLICATING:
1195 return String16("AudioDup");
1196 case RECORD:
1197 return String16("AudioIn");
1198 case OFFLOAD:
1199 return String16("AudioOffload");
1200 case MMAP_PLAYBACK:
1201 return String16("MmapPlayback");
1202 case MMAP_CAPTURE:
1203 return String16("MmapCapture");
1204 case SPATIALIZER:
1205 return String16("AudioSpatial");
1206 case BIT_PERFECT:
1207 return String16("AudioBitPerfect");
1208 default:
1209 ALOG_ASSERT(false);
1210 return String16("AudioUnknown");
1211 }
1212 }
1213
acquireWakeLock_l()1214 void ThreadBase::acquireWakeLock_l()
1215 {
1216 getPowerManager_l();
1217 if (mPowerManager != 0) {
1218 sp<IBinder> binder = new BBinder();
1219 // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids.
1220 binder::Status status = mPowerManager->acquireWakeLockAsync(binder,
1221 POWERMANAGER_PARTIAL_WAKE_LOCK,
1222 getWakeLockTag(),
1223 String16("audioserver"),
1224 {} /* workSource */,
1225 {} /* historyTag */);
1226 if (status.isOk()) {
1227 mWakeLockToken = binder;
1228 if (media::psh_utils::AudioPowerManager::enabled()) {
1229 mThreadToken = media::psh_utils::createAudioThreadToken(
1230 getTid(), String8(getWakeLockTag()).c_str());
1231 }
1232 }
1233 ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status.exceptionCode());
1234 }
1235
1236 gBoottime.acquire(mWakeLockToken);
1237 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
1238 gBoottime.getBoottimeOffset();
1239 }
1240
releaseWakeLock()1241 void ThreadBase::releaseWakeLock()
1242 {
1243 audio_utils::lock_guard _l(mutex());
1244 releaseWakeLock_l();
1245 }
1246
releaseWakeLock_l()1247 void ThreadBase::releaseWakeLock_l()
1248 {
1249 gBoottime.release(mWakeLockToken);
1250 if (mWakeLockToken != 0) {
1251 ALOGV("releaseWakeLock_l() %s", mThreadName);
1252 if (mPowerManager != 0) {
1253 mPowerManager->releaseWakeLockAsync(mWakeLockToken, 0);
1254 }
1255 mWakeLockToken.clear();
1256 }
1257 mThreadToken.reset();
1258 }
1259
getPowerManager_l()1260 void ThreadBase::getPowerManager_l() {
1261 if (mSystemReady && mPowerManager == 0) {
1262 // use checkService() to avoid blocking if power service is not up yet
1263 sp<IBinder> binder =
1264 defaultServiceManager()->checkService(String16("power"));
1265 if (binder == 0) {
1266 ALOGW("Thread %s cannot connect to the power manager service", mThreadName);
1267 } else {
1268 mPowerManager = interface_cast<os::IPowerManager>(binder);
1269 binder->linkToDeath(mDeathRecipient);
1270 }
1271 }
1272 }
1273
updateWakeLockUids_l(const SortedVector<uid_t> & uids)1274 void ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t>& uids) {
1275 getPowerManager_l();
1276
1277 #if !LOG_NDEBUG
1278 std::stringstream s;
1279 for (uid_t uid : uids) {
1280 s << uid << " ";
1281 }
1282 ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str());
1283 #endif
1284
1285 if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called.
1286 if (mSystemReady) {
1287 ALOGE("no wake lock to update, but system ready!");
1288 } else {
1289 ALOGW("no wake lock to update, system not ready yet");
1290 }
1291 return;
1292 }
1293 if (mPowerManager != 0) {
1294 std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints
1295 binder::Status status = mPowerManager->updateWakeLockUidsAsync(
1296 mWakeLockToken, uidsAsInt);
1297 ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status.exceptionCode());
1298 }
1299 }
1300
clearPowerManager()1301 void ThreadBase::clearPowerManager()
1302 {
1303 audio_utils::lock_guard _l(mutex());
1304 releaseWakeLock_l();
1305 mPowerManager.clear();
1306 }
1307
updateOutDevices(const DeviceDescriptorBaseVector & outDevices __unused)1308 void ThreadBase::updateOutDevices(
1309 const DeviceDescriptorBaseVector& outDevices __unused)
1310 {
1311 ALOGE("%s should only be called in RecordThread", __func__);
1312 }
1313
resizeInputBuffer_l(int32_t)1314 void ThreadBase::resizeInputBuffer_l(int32_t /* maxSharedAudioHistoryMs */)
1315 {
1316 ALOGE("%s should only be called in RecordThread", __func__);
1317 }
1318
binderDied(const wp<IBinder> &)1319 void ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& /* who */)
1320 {
1321 sp<ThreadBase> thread = mThread.promote();
1322 if (thread != 0) {
1323 thread->clearPowerManager();
1324 }
1325 ALOGW("power manager service died !!!");
1326 }
1327
setEffectSuspended_l(const effect_uuid_t * type,bool suspend,audio_session_t sessionId)1328 void ThreadBase::setEffectSuspended_l(
1329 const effect_uuid_t *type, bool suspend, audio_session_t sessionId)
1330 {
1331 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
1332 if (chain != 0) {
1333 if (type != NULL) {
1334 chain->setEffectSuspended_l(type, suspend);
1335 } else {
1336 chain->setEffectSuspendedAll_l(suspend);
1337 }
1338 }
1339
1340 updateSuspendedSessions_l(type, suspend, sessionId);
1341 }
1342
checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain> & chain)1343 void ThreadBase::checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain)
1344 {
1345 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
1346 if (index < 0) {
1347 return;
1348 }
1349
1350 const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
1351 mSuspendedSessions.valueAt(index);
1352
1353 for (size_t i = 0; i < sessionEffects.size(); i++) {
1354 const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i);
1355 for (int j = 0; j < desc->mRefCount; j++) {
1356 if (sessionEffects.keyAt(i) == IAfEffectChain::kKeyForSuspendAll) {
1357 chain->setEffectSuspendedAll_l(true);
1358 } else {
1359 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
1360 desc->mType.timeLow);
1361 chain->setEffectSuspended_l(&desc->mType, true);
1362 }
1363 }
1364 }
1365 }
1366
updateSuspendedSessions_l(const effect_uuid_t * type,bool suspend,audio_session_t sessionId)1367 void ThreadBase::updateSuspendedSessions_l(const effect_uuid_t* type,
1368 bool suspend,
1369 audio_session_t sessionId)
1370 {
1371 ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
1372
1373 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1374
1375 if (suspend) {
1376 if (index >= 0) {
1377 sessionEffects = mSuspendedSessions.valueAt(index);
1378 } else {
1379 mSuspendedSessions.add(sessionId, sessionEffects);
1380 }
1381 } else {
1382 if (index < 0) {
1383 return;
1384 }
1385 sessionEffects = mSuspendedSessions.valueAt(index);
1386 }
1387
1388
1389 int key = IAfEffectChain::kKeyForSuspendAll;
1390 if (type != NULL) {
1391 key = type->timeLow;
1392 }
1393 index = sessionEffects.indexOfKey(key);
1394
1395 sp<SuspendedSessionDesc> desc;
1396 if (suspend) {
1397 if (index >= 0) {
1398 desc = sessionEffects.valueAt(index);
1399 } else {
1400 desc = new SuspendedSessionDesc();
1401 if (type != NULL) {
1402 desc->mType = *type;
1403 }
1404 sessionEffects.add(key, desc);
1405 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
1406 }
1407 desc->mRefCount++;
1408 } else {
1409 if (index < 0) {
1410 return;
1411 }
1412 desc = sessionEffects.valueAt(index);
1413 if (--desc->mRefCount == 0) {
1414 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
1415 sessionEffects.removeItemsAt(index);
1416 if (sessionEffects.isEmpty()) {
1417 ALOGV("updateSuspendedSessions_l() restore removing session %d",
1418 sessionId);
1419 mSuspendedSessions.removeItem(sessionId);
1420 }
1421 }
1422 }
1423 if (!sessionEffects.isEmpty()) {
1424 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1425 }
1426 }
1427
checkSuspendOnEffectEnabled(bool enabled,audio_session_t sessionId,bool threadLocked)1428 void ThreadBase::checkSuspendOnEffectEnabled(bool enabled,
1429 audio_session_t sessionId,
1430 bool threadLocked)
1431 NO_THREAD_SAFETY_ANALYSIS // manual locking
1432 {
1433 if (!threadLocked) {
1434 mutex().lock();
1435 }
1436
1437 if (mType != RECORD) {
1438 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1439 // another session. This gives the priority to well behaved effect control panels
1440 // and applications not using global effects.
1441 // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
1442 // global effects
1443 if (!audio_is_global_session(sessionId)) {
1444 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1445 }
1446 }
1447
1448 if (!threadLocked) {
1449 mutex().unlock();
1450 }
1451 }
1452
1453 // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held
checkEffectCompatibility_l(const effect_descriptor_t * desc,audio_session_t sessionId)1454 status_t RecordThread::checkEffectCompatibility_l(
1455 const effect_descriptor_t *desc, audio_session_t sessionId)
1456 {
1457 // No global output effect sessions on record threads
1458 if (sessionId == AUDIO_SESSION_OUTPUT_MIX
1459 || sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
1460 ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s",
1461 desc->name, mThreadName);
1462 return BAD_VALUE;
1463 }
1464 // only pre processing effects on record thread
1465 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) {
1466 ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s",
1467 desc->name, mThreadName);
1468 return BAD_VALUE;
1469 }
1470
1471 // always allow effects without processing load or latency
1472 if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
1473 return NO_ERROR;
1474 }
1475
1476 audio_input_flags_t flags = mInput->flags;
1477 if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) {
1478 if (flags & AUDIO_INPUT_FLAG_RAW) {
1479 ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode",
1480 desc->name, mThreadName);
1481 return BAD_VALUE;
1482 }
1483 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1484 ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode",
1485 desc->name, mThreadName);
1486 return BAD_VALUE;
1487 }
1488 }
1489
1490 if (IAfEffectModule::isHapticGenerator(&desc->type)) {
1491 ALOGE("%s(): HapticGenerator is not supported in RecordThread", __func__);
1492 return BAD_VALUE;
1493 }
1494 return NO_ERROR;
1495 }
1496
1497 // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held
checkEffectCompatibility_l(const effect_descriptor_t * desc,audio_session_t sessionId)1498 status_t PlaybackThread::checkEffectCompatibility_l(
1499 const effect_descriptor_t *desc, audio_session_t sessionId)
1500 {
1501 // no preprocessing on playback threads
1502 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
1503 ALOGW("%s: pre processing effect %s created on playback"
1504 " thread %s", __func__, desc->name, mThreadName);
1505 return BAD_VALUE;
1506 }
1507
1508 // always allow effects without processing load or latency
1509 if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
1510 return NO_ERROR;
1511 }
1512
1513 if (IAfEffectModule::isHapticGenerator(&desc->type) && mHapticChannelCount == 0) {
1514 ALOGW("%s: thread (%s) doesn't support haptic playback while the effect is HapticGenerator",
1515 __func__, threadTypeToString(mType));
1516 return BAD_VALUE;
1517 }
1518
1519 if (IAfEffectModule::isSpatializer(&desc->type)
1520 && mType != SPATIALIZER) {
1521 ALOGW("%s: attempt to create a spatializer effect on a thread of type %d",
1522 __func__, mType);
1523 return BAD_VALUE;
1524 }
1525
1526 switch (mType) {
1527 case MIXER: {
1528 audio_output_flags_t flags = mOutput->flags;
1529 if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) {
1530 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1531 // global effects are applied only to non fast tracks if they are SW
1532 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1533 break;
1534 }
1535 } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
1536 // only post processing on output stage session
1537 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1538 ALOGW("%s: non post processing effect %s not allowed on output stage session",
1539 __func__, desc->name);
1540 return BAD_VALUE;
1541 }
1542 } else if (sessionId == AUDIO_SESSION_DEVICE) {
1543 // only post processing on output stage session
1544 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1545 ALOGW("%s: non post processing effect %s not allowed on device session",
1546 __func__, desc->name);
1547 return BAD_VALUE;
1548 }
1549 } else {
1550 // no restriction on effects applied on non fast tracks
1551 if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) {
1552 break;
1553 }
1554 }
1555
1556 if (flags & AUDIO_OUTPUT_FLAG_RAW) {
1557 ALOGW("%s: effect %s on playback thread in raw mode", __func__, desc->name);
1558 return BAD_VALUE;
1559 }
1560 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1561 ALOGW("%s: non HW effect %s on playback thread in fast mode",
1562 __func__, desc->name);
1563 return BAD_VALUE;
1564 }
1565 }
1566 } break;
1567 case OFFLOAD:
1568 // nothing actionable on offload threads, if the effect:
1569 // - is offloadable: the effect can be created
1570 // - is NOT offloadable: the effect should still be created, but EffectHandle::enable()
1571 // will take care of invalidating the tracks of the thread
1572 break;
1573 case DIRECT:
1574 // Reject any effect on Direct output threads for now, since the format of
1575 // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
1576 ALOGW("%s: effect %s on DIRECT output thread %s",
1577 __func__, desc->name, mThreadName);
1578 return BAD_VALUE;
1579 case DUPLICATING:
1580 if (audio_is_global_session(sessionId)) {
1581 ALOGW("%s: global effect %s on DUPLICATING thread %s",
1582 __func__, desc->name, mThreadName);
1583 return BAD_VALUE;
1584 }
1585 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
1586 ALOGW("%s: post processing effect %s on DUPLICATING thread %s",
1587 __func__, desc->name, mThreadName);
1588 return BAD_VALUE;
1589 }
1590 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) {
1591 ALOGW("%s: HW tunneled effect %s on DUPLICATING thread %s",
1592 __func__, desc->name, mThreadName);
1593 return BAD_VALUE;
1594 }
1595 break;
1596 case SPATIALIZER:
1597 // Global effects (AUDIO_SESSION_OUTPUT_MIX) are supported on spatializer mixer, but only
1598 // the spatialized track have global effects applied for now.
1599 // Post processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE)
1600 // are supported and added after the spatializer.
1601 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1602 ALOGD("%s: global effect %s on spatializer thread %s", __func__, desc->name,
1603 mThreadName);
1604 } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
1605 // only post processing , downmixer or spatializer effects on output stage session
1606 if (IAfEffectModule::isSpatializer(&desc->type)
1607 || memcmp(&desc->type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
1608 break;
1609 }
1610 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1611 ALOGW("%s: non post processing effect %s not allowed on output stage session",
1612 __func__, desc->name);
1613 return BAD_VALUE;
1614 }
1615 } else if (sessionId == AUDIO_SESSION_DEVICE) {
1616 // only post processing on output stage session
1617 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1618 ALOGW("%s: non post processing effect %s not allowed on device session",
1619 __func__, desc->name);
1620 return BAD_VALUE;
1621 }
1622 }
1623 break;
1624 case BIT_PERFECT:
1625 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) {
1626 // Allow HW accelerated effects of tunnel type
1627 break;
1628 }
1629 // As bit-perfect tracks will not be allowed to apply audio effect that will touch the audio
1630 // data, effects will not be allowed on 1) global effects (AUDIO_SESSION_OUTPUT_MIX),
1631 // 2) post-processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) and
1632 // 3) there is any bit-perfect track with the given session id.
1633 if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE ||
1634 sessionId == AUDIO_SESSION_DEVICE) {
1635 ALOGW("%s: effect %s not supported on bit-perfect thread %s",
1636 __func__, desc->name, mThreadName);
1637 return BAD_VALUE;
1638 } else if ((hasAudioSession_l(sessionId) & ThreadBase::BIT_PERFECT_SESSION) != 0) {
1639 ALOGW("%s: effect %s not supported as there is a bit-perfect track with session as %d",
1640 __func__, desc->name, sessionId);
1641 return BAD_VALUE;
1642 }
1643 break;
1644 default:
1645 LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType);
1646 }
1647
1648 return NO_ERROR;
1649 }
1650
1651 // ThreadBase::createEffect_l() must be called with AudioFlinger::mutex() held
createEffect_l(const sp<Client> & client,const sp<IEffectClient> & effectClient,int32_t priority,audio_session_t sessionId,effect_descriptor_t * desc,int * enabled,status_t * status,bool pinned,bool probe,bool notifyFramesProcessed)1652 sp<IAfEffectHandle> ThreadBase::createEffect_l(
1653 const sp<Client>& client,
1654 const sp<IEffectClient>& effectClient,
1655 int32_t priority,
1656 audio_session_t sessionId,
1657 effect_descriptor_t *desc,
1658 int *enabled,
1659 status_t *status,
1660 bool pinned,
1661 bool probe,
1662 bool notifyFramesProcessed)
1663 {
1664 sp<IAfEffectModule> effect;
1665 sp<IAfEffectHandle> handle;
1666 status_t lStatus;
1667 sp<IAfEffectChain> chain;
1668 bool chainCreated = false;
1669 bool effectCreated = false;
1670 audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED;
1671
1672 lStatus = initCheck();
1673 if (lStatus != NO_ERROR) {
1674 ALOGW("createEffect_l() Audio driver not initialized.");
1675 goto Exit;
1676 }
1677
1678 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
1679
1680 { // scope for mutex()
1681 audio_utils::lock_guard _l(mutex());
1682
1683 lStatus = checkEffectCompatibility_l(desc, sessionId);
1684 if (probe || lStatus != NO_ERROR) {
1685 goto Exit;
1686 }
1687
1688 // check for existing effect chain with the requested audio session
1689 chain = getEffectChain_l(sessionId);
1690 if (chain == 0) {
1691 // create a new chain for this session
1692 ALOGV("createEffect_l() new effect chain for session %d", sessionId);
1693 chain = IAfEffectChain::create(this, sessionId, mAfThreadCallback);
1694 addEffectChain_l(chain);
1695 chain->setStrategy(getStrategyForSession_l(sessionId));
1696 chainCreated = true;
1697 } else {
1698 effect = chain->getEffectFromDesc(desc);
1699 }
1700
1701 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
1702
1703 if (effect == 0) {
1704 effectId = mAfThreadCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
1705 // create a new effect module if none present in the chain
1706 lStatus = chain->createEffect(effect, desc, effectId, sessionId, pinned);
1707 if (lStatus != NO_ERROR) {
1708 goto Exit;
1709 }
1710 effectCreated = true;
1711
1712 // FIXME: use vector of device and address when effect interface is ready.
1713 effect->setDevices(outDeviceTypeAddrs());
1714 effect->setInputDevice(inDeviceTypeAddr());
1715 effect->setMode(mAfThreadCallback->getMode());
1716 effect->setAudioSource(mAudioSource);
1717 }
1718 if (effect->isHapticGenerator()) {
1719 // TODO(b/184194057): Use the vibrator information from the vibrator that will be used
1720 // for the HapticGenerator.
1721 const std::optional<media::AudioVibratorInfo> defaultVibratorInfo =
1722 mAfThreadCallback->getDefaultVibratorInfo_l();
1723 if (defaultVibratorInfo) {
1724 audio_utils::lock_guard _cl(chain->mutex());
1725 // Only set the vibrator info when it is a valid one.
1726 effect->setVibratorInfo_l(*defaultVibratorInfo);
1727 }
1728 }
1729 // create effect handle and connect it to effect module
1730 handle = IAfEffectHandle::create(
1731 effect, client, effectClient, priority, notifyFramesProcessed);
1732 lStatus = handle->initCheck();
1733 if (lStatus == OK) {
1734 lStatus = effect->addHandle(handle.get());
1735 sendCheckOutputStageEffectsEvent_l();
1736 }
1737 if (enabled != NULL) {
1738 *enabled = (int)effect->isEnabled();
1739 }
1740 }
1741
1742 Exit:
1743 if (!probe && lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
1744 audio_utils::lock_guard _l(mutex());
1745 if (effectCreated) {
1746 chain->removeEffect(effect);
1747 }
1748 if (chainCreated) {
1749 removeEffectChain_l(chain);
1750 }
1751 // handle must be cleared by caller to avoid deadlock.
1752 }
1753
1754 *status = lStatus;
1755 return handle;
1756 }
1757
disconnectEffectHandle(IAfEffectHandle * handle,bool unpinIfLast)1758 void ThreadBase::disconnectEffectHandle(IAfEffectHandle* handle,
1759 bool unpinIfLast)
1760 {
1761 bool remove = false;
1762 sp<IAfEffectModule> effect;
1763 {
1764 audio_utils::lock_guard _l(mutex());
1765 sp<IAfEffectBase> effectBase = handle->effect().promote();
1766 if (effectBase == nullptr) {
1767 return;
1768 }
1769 effect = effectBase->asEffectModule();
1770 if (effect == nullptr) {
1771 return;
1772 }
1773 // restore suspended effects if the disconnected handle was enabled and the last one.
1774 remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
1775 if (remove) {
1776 removeEffect_l(effect, true);
1777 }
1778 sendCheckOutputStageEffectsEvent_l();
1779 }
1780 if (remove) {
1781 mAfThreadCallback->updateOrphanEffectChains(effect);
1782 if (handle->enabled()) {
1783 effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
1784 }
1785 }
1786 }
1787
onEffectEnable(const sp<IAfEffectModule> & effect)1788 void ThreadBase::onEffectEnable(const sp<IAfEffectModule>& effect) {
1789 if (isOffloadOrMmap()) {
1790 audio_utils::lock_guard _l(mutex());
1791 broadcast_l();
1792 }
1793 if (!effect->isOffloadable()) {
1794 if (mType == ThreadBase::OFFLOAD) {
1795 PlaybackThread *t = (PlaybackThread *)this;
1796 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1797 }
1798 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1799 mAfThreadCallback->onNonOffloadableGlobalEffectEnable();
1800 }
1801 }
1802 }
1803
onEffectDisable()1804 void ThreadBase::onEffectDisable() {
1805 if (isOffloadOrMmap()) {
1806 audio_utils::lock_guard _l(mutex());
1807 broadcast_l();
1808 }
1809 }
1810
getEffect(audio_session_t sessionId,int effectId) const1811 sp<IAfEffectModule> ThreadBase::getEffect(audio_session_t sessionId,
1812 int effectId) const
1813 {
1814 audio_utils::lock_guard _l(mutex());
1815 return getEffect_l(sessionId, effectId);
1816 }
1817
getEffect_l(audio_session_t sessionId,int effectId) const1818 sp<IAfEffectModule> ThreadBase::getEffect_l(audio_session_t sessionId,
1819 int effectId) const
1820 {
1821 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
1822 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
1823 }
1824
getEffectIds_l(audio_session_t sessionId) const1825 std::vector<int> ThreadBase::getEffectIds_l(audio_session_t sessionId) const
1826 {
1827 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
1828 return chain != nullptr ? chain->getEffectIds_l() : std::vector<int>{};
1829 }
1830
1831 // PlaybackThread::addEffect_ll() must be called with AudioFlinger::mutex() and
1832 // ThreadBase::mutex() held
addEffect_ll(const sp<IAfEffectModule> & effect)1833 status_t ThreadBase::addEffect_ll(const sp<IAfEffectModule>& effect)
1834 {
1835 // check for existing effect chain with the requested audio session
1836 audio_session_t sessionId = effect->sessionId();
1837 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
1838 bool chainCreated = false;
1839
1840 ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(),
1841 "%s: on offloaded thread %p: effect %s does not support offload flags %#x",
1842 __func__, this, effect->desc().name, effect->desc().flags);
1843
1844 if (chain == 0) {
1845 // create a new chain for this session
1846 ALOGV("%s: new effect chain for session %d", __func__, sessionId);
1847 chain = IAfEffectChain::create(this, sessionId, mAfThreadCallback);
1848 addEffectChain_l(chain);
1849 chain->setStrategy(getStrategyForSession_l(sessionId));
1850 chainCreated = true;
1851 }
1852 ALOGV("%s: %p chain %p effect %p", __func__, this, chain.get(), effect.get());
1853
1854 if (chain->getEffectFromId_l(effect->id()) != 0) {
1855 ALOGW("%s: %p effect %s already present in chain %p",
1856 __func__, this, effect->desc().name, chain.get());
1857 return BAD_VALUE;
1858 }
1859
1860 effect->setOffloaded_l(mType == OFFLOAD, mId);
1861
1862 status_t status = chain->addEffect(effect);
1863 if (status != NO_ERROR) {
1864 if (chainCreated) {
1865 removeEffectChain_l(chain);
1866 }
1867 return status;
1868 }
1869
1870 effect->setDevices(outDeviceTypeAddrs());
1871 effect->setInputDevice(inDeviceTypeAddr());
1872 effect->setMode(mAfThreadCallback->getMode());
1873 effect->setAudioSource(mAudioSource);
1874
1875 return NO_ERROR;
1876 }
1877
removeEffect_l(const sp<IAfEffectModule> & effect,bool release)1878 void ThreadBase::removeEffect_l(const sp<IAfEffectModule>& effect, bool release) {
1879
1880 ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get());
1881 effect_descriptor_t desc = effect->desc();
1882 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1883 detachAuxEffect_l(effect->id());
1884 }
1885
1886 sp<IAfEffectChain> chain = effect->getCallback()->chain().promote();
1887 if (chain != 0) {
1888 // remove effect chain if removing last effect
1889 if (chain->removeEffect(effect, release) == 0) {
1890 removeEffectChain_l(chain);
1891 }
1892 } else {
1893 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
1894 }
1895 }
1896
lockEffectChains_l(Vector<sp<IAfEffectChain>> & effectChains)1897 void ThreadBase::lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains)
1898 NO_THREAD_SAFETY_ANALYSIS // calls EffectChain::lock()
1899 {
1900 effectChains = mEffectChains;
1901 for (const auto& effectChain : effectChains) {
1902 effectChain->mutex().lock();
1903 }
1904 }
1905
unlockEffectChains(const Vector<sp<IAfEffectChain>> & effectChains)1906 void ThreadBase::unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains)
1907 NO_THREAD_SAFETY_ANALYSIS // calls EffectChain::unlock()
1908 {
1909 for (const auto& effectChain : effectChains) {
1910 effectChain->mutex().unlock();
1911 }
1912 }
1913
getEffectChain(audio_session_t sessionId) const1914 sp<IAfEffectChain> ThreadBase::getEffectChain(audio_session_t sessionId) const
1915 {
1916 audio_utils::lock_guard _l(mutex());
1917 return getEffectChain_l(sessionId);
1918 }
1919
getEffectChain_l(audio_session_t sessionId) const1920 sp<IAfEffectChain> ThreadBase::getEffectChain_l(audio_session_t sessionId)
1921 const
1922 {
1923 size_t size = mEffectChains.size();
1924 for (size_t i = 0; i < size; i++) {
1925 if (mEffectChains[i]->sessionId() == sessionId) {
1926 return mEffectChains[i];
1927 }
1928 }
1929 return 0;
1930 }
1931
setMode(audio_mode_t mode)1932 void ThreadBase::setMode(audio_mode_t mode)
1933 {
1934 audio_utils::lock_guard _l(mutex());
1935 size_t size = mEffectChains.size();
1936 for (size_t i = 0; i < size; i++) {
1937 mEffectChains[i]->setMode_l(mode);
1938 }
1939 }
1940
toAudioPortConfig(struct audio_port_config * config)1941 void ThreadBase::toAudioPortConfig(struct audio_port_config* config)
1942 {
1943 config->type = AUDIO_PORT_TYPE_MIX;
1944 config->ext.mix.handle = mId;
1945 config->sample_rate = mSampleRate;
1946 config->format = mHALFormat;
1947 config->channel_mask = mChannelMask;
1948 config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
1949 AUDIO_PORT_CONFIG_FORMAT;
1950 }
1951
systemReady()1952 void ThreadBase::systemReady()
1953 {
1954 audio_utils::lock_guard _l(mutex());
1955 if (mSystemReady) {
1956 return;
1957 }
1958 mSystemReady = true;
1959
1960 for (size_t i = 0; i < mPendingConfigEvents.size(); i++) {
1961 sendConfigEvent_l(mPendingConfigEvents.editItemAt(i));
1962 }
1963 mPendingConfigEvents.clear();
1964 }
1965
1966 template <typename T>
add(const sp<T> & track)1967 ssize_t ThreadBase::ActiveTracks<T>::add(const sp<T>& track) {
1968 ssize_t index = mActiveTracks.indexOf(track);
1969 if (index >= 0) {
1970 ALOGW("ActiveTracks<T>::add track %p already there", track.get());
1971 return index;
1972 }
1973 logTrack("add", track);
1974 mActiveTracksGeneration++;
1975 mLatestActiveTrack = track;
1976 track->beginBatteryAttribution();
1977 mHasChanged = true;
1978 return mActiveTracks.add(track);
1979 }
1980
1981 template <typename T>
remove(const sp<T> & track)1982 ssize_t ThreadBase::ActiveTracks<T>::remove(const sp<T>& track) {
1983 ssize_t index = mActiveTracks.remove(track);
1984 if (index < 0) {
1985 ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get());
1986 return index;
1987 }
1988 logTrack("remove", track);
1989 mActiveTracksGeneration++;
1990 track->endBatteryAttribution();
1991 // mLatestActiveTrack is not cleared even if is the same as track.
1992 mHasChanged = true;
1993 #ifdef TEE_SINK
1994 track->dumpTee(-1 /* fd */, "_REMOVE");
1995 #endif
1996 track->logEndInterval(); // log to MediaMetrics
1997 return index;
1998 }
1999
2000 template <typename T>
clear()2001 void ThreadBase::ActiveTracks<T>::clear() {
2002 for (const sp<T> &track : mActiveTracks) {
2003 track->endBatteryAttribution();
2004 logTrack("clear", track);
2005 }
2006 mLastActiveTracksGeneration = mActiveTracksGeneration;
2007 if (!mActiveTracks.empty()) { mHasChanged = true; }
2008 mActiveTracks.clear();
2009 mLatestActiveTrack.clear();
2010 }
2011
2012 template <typename T>
updatePowerState_l(const sp<ThreadBase> & thread,bool force)2013 void ThreadBase::ActiveTracks<T>::updatePowerState_l(
2014 const sp<ThreadBase>& thread, bool force) {
2015 // Updates ActiveTracks client uids to the thread wakelock.
2016 if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) {
2017 thread->updateWakeLockUids_l(getWakeLockUids());
2018 mLastActiveTracksGeneration = mActiveTracksGeneration;
2019 }
2020 }
2021
2022 template <typename T>
readAndClearHasChanged()2023 bool ThreadBase::ActiveTracks<T>::readAndClearHasChanged() {
2024 bool hasChanged = mHasChanged;
2025 mHasChanged = false;
2026
2027 for (const sp<T> &track : mActiveTracks) {
2028 // Do not short-circuit as all hasChanged states must be reset
2029 // as all the metadata are going to be sent
2030 hasChanged |= track->readAndClearHasChanged();
2031 }
2032 return hasChanged;
2033 }
2034
2035 template <typename T>
logTrack(const char * funcName,const sp<T> & track) const2036 void ThreadBase::ActiveTracks<T>::logTrack(
2037 const char *funcName, const sp<T> &track) const {
2038 if (mLocalLog != nullptr) {
2039 String8 result;
2040 track->appendDump(result, false /* active */);
2041 mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.c_str());
2042 }
2043 }
2044
broadcast_l()2045 void ThreadBase::broadcast_l()
2046 {
2047 // Thread could be blocked waiting for async
2048 // so signal it to handle state changes immediately
2049 // If threadLoop is currently unlocked a signal of mWaitWorkCV will
2050 // be lost so we also flag to prevent it blocking on mWaitWorkCV
2051 mSignalPending = true;
2052 mWaitWorkCV.notify_all();
2053 }
2054
2055 // Call only from threadLoop() or when it is idle.
2056 // Do not call from high performance code as this may do binder rpc to the MediaMetrics service.
sendStatistics(bool force)2057 void ThreadBase::sendStatistics(bool force)
2058 NO_THREAD_SAFETY_ANALYSIS
2059 {
2060 // Do not log if we have no stats.
2061 // We choose the timestamp verifier because it is the most likely item to be present.
2062 const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN;
2063 if (nstats == 0) {
2064 return;
2065 }
2066
2067 // Don't log more frequently than once per 12 hours.
2068 // We use BOOTTIME to include suspend time.
2069 const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME);
2070 const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0
2071 if (!force && sinceNs <= 12 * NANOS_PER_HOUR) {
2072 return;
2073 }
2074
2075 mLastRecordedTimestampVerifierN = mTimestampVerifier.getN();
2076 mLastRecordedTimeNs = timeNs;
2077
2078 std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("audiothread"));
2079
2080 #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors.
2081
2082 // thread configuration
2083 item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle
2084 // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId);
2085 item->setCString(MM_PREFIX "type", threadTypeToString(mType));
2086 item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate);
2087 item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask);
2088 item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str());
2089 item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount);
2090 item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes_l()).c_str());
2091 item->setCString(MM_PREFIX "inDevice", toString(inDeviceType_l()).c_str());
2092
2093 // thread statistics
2094 if (mIoJitterMs.getN() > 0) {
2095 item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean());
2096 item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev());
2097 }
2098 if (mProcessTimeMs.getN() > 0) {
2099 item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean());
2100 item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev());
2101 }
2102 const auto tsjitter = mTimestampVerifier.getJitterMs();
2103 if (tsjitter.getN() > 0) {
2104 item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean());
2105 item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev());
2106 }
2107 if (mLatencyMs.getN() > 0) {
2108 item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean());
2109 item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev());
2110 }
2111 if (mMonopipePipeDepthStats.getN() > 0) {
2112 item->setDouble(MM_PREFIX "monopipePipeDepthStats.mean",
2113 mMonopipePipeDepthStats.getMean());
2114 item->setDouble(MM_PREFIX "monopipePipeDepthStats.std",
2115 mMonopipePipeDepthStats.getStdDev());
2116 }
2117
2118 item->selfrecord();
2119 }
2120
getStrategyForStream(audio_stream_type_t stream) const2121 product_strategy_t ThreadBase::getStrategyForStream(audio_stream_type_t stream) const
2122 {
2123 if (!mAfThreadCallback->isAudioPolicyReady()) {
2124 return PRODUCT_STRATEGY_NONE;
2125 }
2126 return AudioSystem::getStrategyForStream(stream);
2127 }
2128
2129 // startMelComputation_l() must be called with AudioFlinger::mutex() held
startMelComputation_l(const sp<audio_utils::MelProcessor> &)2130 void ThreadBase::startMelComputation_l(
2131 const sp<audio_utils::MelProcessor>& /*processor*/)
2132 {
2133 // Do nothing
2134 ALOGW("%s: ThreadBase does not support CSD", __func__);
2135 }
2136
2137 // stopMelComputation_l() must be called with AudioFlinger::mutex() held
stopMelComputation_l()2138 void ThreadBase::stopMelComputation_l()
2139 {
2140 // Do nothing
2141 ALOGW("%s: ThreadBase does not support CSD", __func__);
2142 }
2143
2144 // ----------------------------------------------------------------------------
2145 // Playback
2146 // ----------------------------------------------------------------------------
2147
PlaybackThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,type_t type,bool systemReady,audio_config_base_t * mixerConfig)2148 PlaybackThread::PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback,
2149 AudioStreamOut* output,
2150 audio_io_handle_t id,
2151 type_t type,
2152 bool systemReady,
2153 audio_config_base_t *mixerConfig)
2154 : ThreadBase(afThreadCallback, id, type, systemReady, true /* isOut */),
2155 mNormalFrameCount(0), mSinkBuffer(NULL),
2156 mMixerBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER),
2157 mMixerBuffer(NULL),
2158 mMixerBufferSize(0),
2159 mMixerBufferFormat(AUDIO_FORMAT_INVALID),
2160 mMixerBufferValid(false),
2161 mEffectBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER),
2162 mEffectBuffer(NULL),
2163 mEffectBufferSize(0),
2164 mEffectBufferFormat(AUDIO_FORMAT_INVALID),
2165 mEffectBufferValid(false),
2166 mSuspended(0), mBytesWritten(0),
2167 mFramesWritten(0),
2168 mSuspendedFrames(0),
2169 mActiveTracks(&this->mLocalLog),
2170 // mStreamTypes[] initialized in constructor body
2171 mTracks(type == MIXER),
2172 mOutput(output),
2173 mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
2174 mMixerStatus(MIXER_IDLE),
2175 mMixerStatusIgnoringFastTracks(MIXER_IDLE),
2176 mStandbyDelayNs(getStandbyTimeInNanos()),
2177 mBytesRemaining(0),
2178 mCurrentWriteLength(0),
2179 mUseAsyncWrite(false),
2180 mWriteAckSequence(0),
2181 mDrainSequence(0),
2182 mScreenState(mAfThreadCallback->getScreenState()),
2183 // index 0 is reserved for normal mixer's submix
2184 mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1),
2185 mHwSupportsPause(false), mHwPaused(false), mFlushPending(false),
2186 mLeftVolFloat(-1.0), mRightVolFloat(-1.0),
2187 mDownStreamPatch{},
2188 mIsTimestampAdvancing(kMinimumTimeBetweenTimestampChecksNs)
2189 {
2190 snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id);
2191 mFlagsAsString = toString(output->flags);
2192
2193 // Assumes constructor is called by AudioFlinger with its mutex() held, but
2194 // it would be safer to explicitly pass initial masterVolume/masterMute as
2195 // parameter.
2196 //
2197 // If the HAL we are using has support for master volume or master mute,
2198 // then do not attenuate or mute during mixing (just leave the volume at 1.0
2199 // and the mute set to false).
2200 mMasterVolume = afThreadCallback->masterVolume_l();
2201 mMasterMute = afThreadCallback->masterMute_l();
2202 if (mOutput->audioHwDev) {
2203 if (mOutput->audioHwDev->canSetMasterVolume()) {
2204 mMasterVolume = 1.0;
2205 }
2206
2207 if (mOutput->audioHwDev->canSetMasterMute()) {
2208 mMasterMute = false;
2209 }
2210 mIsMsdDevice = strcmp(
2211 mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
2212 }
2213
2214 if (mixerConfig != nullptr && mixerConfig->channel_mask != AUDIO_CHANNEL_NONE) {
2215 mMixerChannelMask = mixerConfig->channel_mask;
2216 }
2217
2218 readOutputParameters_l();
2219
2220 if (mType != SPATIALIZER
2221 && mMixerChannelMask != mChannelMask) {
2222 LOG_ALWAYS_FATAL("HAL channel mask %#x does not match mixer channel mask %#x",
2223 mChannelMask, mMixerChannelMask);
2224 }
2225
2226 // TODO: We may also match on address as well as device type for
2227 // AUDIO_DEVICE_OUT_BUS, AUDIO_DEVICE_OUT_ALL_A2DP, AUDIO_DEVICE_OUT_REMOTE_SUBMIX
2228 if (type == MIXER || type == DIRECT || type == OFFLOAD) {
2229 // TODO: This property should be ensure that only contains one single device type.
2230 mTimestampCorrectedDevice = (audio_devices_t)property_get_int64(
2231 "audio.timestamp.corrected_output_device",
2232 (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD
2233 : AUDIO_DEVICE_NONE));
2234 }
2235 if (!audioserver_flags::portid_volume_management()) {
2236 for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) {
2237 const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)};
2238 mStreamTypes[stream].volume = 0.0f;
2239 mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream);
2240 }
2241 // Audio patch and call assistant volume are always max
2242 mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f;
2243 mStreamTypes[AUDIO_STREAM_PATCH].mute = false;
2244 mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f;
2245 mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false;
2246 }
2247 }
2248
~PlaybackThread()2249 PlaybackThread::~PlaybackThread()
2250 {
2251 free(mSinkBuffer);
2252 free(mMixerBuffer);
2253 free(mEffectBuffer);
2254 free(mPostSpatializerBuffer);
2255 }
2256
2257 // Thread virtuals
2258
onFirstRef()2259 void PlaybackThread::onFirstRef()
2260 {
2261 if (!isStreamInitialized()) {
2262 ALOGE("The stream is not open yet"); // This should not happen.
2263 } else {
2264 // Callbacks take strong or weak pointers as a parameter.
2265 // Since PlaybackThread passes itself as a callback handler, it can only
2266 // be done outside of the constructor. Creating weak and especially strong
2267 // pointers to a refcounted object in its own constructor is strongly
2268 // discouraged, see comments in system/core/libutils/include/utils/RefBase.h.
2269 // Even if a function takes a weak pointer, it is possible that it will
2270 // need to convert it to a strong pointer down the line.
2271 if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING &&
2272 mOutput->stream->setCallback(this) == OK) {
2273 mUseAsyncWrite = true;
2274 mCallbackThread = sp<AsyncCallbackThread>::make(this);
2275 }
2276
2277 if (mOutput->stream->setEventCallback(this) != OK) {
2278 ALOGD("Failed to add event callback");
2279 }
2280 }
2281 run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
2282 mThreadSnapshot.setTid(getTid());
2283 }
2284
2285 // ThreadBase virtuals
preExit()2286 void PlaybackThread::preExit()
2287 {
2288 ALOGV(" preExit()");
2289 status_t result = mOutput->stream->exit();
2290 ALOGE_IF(result != OK, "Error when calling exit(): %d", result);
2291 }
2292
dumpTracks_l(int fd,const Vector<String16> &)2293 void PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& /* args */)
2294 {
2295 String8 result;
2296 if (!audioserver_flags::portid_volume_management()) {
2297 result.appendFormat(" Stream volumes in dB: ");
2298 for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
2299 const stream_type_t *st = &mStreamTypes[i];
2300 if (i > 0) {
2301 result.appendFormat(", ");
2302 }
2303 result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
2304 if (st->mute) {
2305 result.append("M");
2306 }
2307 }
2308 }
2309 result.append("\n");
2310 write(fd, result.c_str(), result.length());
2311 result.clear();
2312
2313 // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way.
2314 FastTrackUnderruns underruns = getFastTrackUnderruns(0);
2315 dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n",
2316 underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
2317
2318 size_t numtracks = mTracks.size();
2319 size_t numactive = mActiveTracks.size();
2320 dprintf(fd, " %zu Tracks", numtracks);
2321 size_t numactiveseen = 0;
2322 const char *prefix = " ";
2323 if (numtracks) {
2324 dprintf(fd, " of which %zu are active\n", numactive);
2325 result.append(prefix);
2326 mTracks[0]->appendDumpHeader(result);
2327 for (size_t i = 0; i < numtracks; ++i) {
2328 sp<IAfTrack> track = mTracks[i];
2329 if (track != 0) {
2330 bool active = mActiveTracks.indexOf(track) >= 0;
2331 if (active) {
2332 numactiveseen++;
2333 }
2334 result.append(prefix);
2335 track->appendDump(result, active);
2336 }
2337 }
2338 } else {
2339 result.append("\n");
2340 }
2341 if (numactiveseen != numactive) {
2342 // some tracks in the active list were not in the tracks list
2343 result.append(" The following tracks are in the active list but"
2344 " not in the track list\n");
2345 result.append(prefix);
2346 mActiveTracks[0]->appendDumpHeader(result);
2347 for (size_t i = 0; i < numactive; ++i) {
2348 sp<IAfTrack> track = mActiveTracks[i];
2349 if (mTracks.indexOf(track) < 0) {
2350 result.append(prefix);
2351 track->appendDump(result, true /* active */);
2352 }
2353 }
2354 }
2355
2356 write(fd, result.c_str(), result.size());
2357 }
2358
dumpInternals_l(int fd,const Vector<String16> & args)2359 void PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args)
2360 {
2361 dprintf(fd, " Master volume: %f\n", mMasterVolume);
2362 dprintf(fd, " Master mute: %s\n", mMasterMute ? "on" : "off");
2363 dprintf(fd, " Mixer channel Mask: %#x (%s)\n",
2364 mMixerChannelMask, channelMaskToString(mMixerChannelMask, true /* output */).c_str());
2365 if (mHapticChannelMask != AUDIO_CHANNEL_NONE) {
2366 dprintf(fd, " Haptic channel mask: %#x (%s)\n", mHapticChannelMask,
2367 channelMaskToString(mHapticChannelMask, true /* output */).c_str());
2368 }
2369 dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount);
2370 dprintf(fd, " Total writes: %d\n", mNumWrites);
2371 dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites);
2372 dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no");
2373 dprintf(fd, " Suspend count: %d\n", (int32_t)mSuspended);
2374 dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask);
2375 dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs);
2376 AudioStreamOut *output = mOutput;
2377 audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE;
2378 dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n",
2379 output, flags, toString(flags).c_str());
2380 dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten);
2381 dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames);
2382 if (mPipeSink.get() != nullptr) {
2383 dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten());
2384 }
2385 if (output != nullptr) {
2386 dprintf(fd, " Hal stream dump:\n");
2387 (void)output->stream->dump(fd, args);
2388 }
2389 }
2390
2391 // PlaybackThread::createTrack_l() must be called with AudioFlinger::mutex() held
createTrack_l(const sp<Client> & client,audio_stream_type_t streamType,const audio_attributes_t & attr,uint32_t * pSampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * pFrameCount,size_t * pNotificationFrameCount,uint32_t notificationsPerBuffer,float speed,const sp<IMemory> & sharedBuffer,audio_session_t sessionId,audio_output_flags_t * flags,pid_t creatorPid,const AttributionSourceState & attributionSource,pid_t tid,status_t * status,audio_port_handle_t portId,const sp<media::IAudioTrackCallback> & callback,bool isSpatialized,bool isBitPerfect,audio_output_flags_t * afTrackFlags,float volume,bool muted)2392 sp<IAfTrack> PlaybackThread::createTrack_l(
2393 const sp<Client>& client,
2394 audio_stream_type_t streamType,
2395 const audio_attributes_t& attr,
2396 uint32_t *pSampleRate,
2397 audio_format_t format,
2398 audio_channel_mask_t channelMask,
2399 size_t *pFrameCount,
2400 size_t *pNotificationFrameCount,
2401 uint32_t notificationsPerBuffer,
2402 float speed,
2403 const sp<IMemory>& sharedBuffer,
2404 audio_session_t sessionId,
2405 audio_output_flags_t *flags,
2406 pid_t creatorPid,
2407 const AttributionSourceState& attributionSource,
2408 pid_t tid,
2409 status_t *status,
2410 audio_port_handle_t portId,
2411 const sp<media::IAudioTrackCallback>& callback,
2412 bool isSpatialized,
2413 bool isBitPerfect,
2414 audio_output_flags_t *afTrackFlags,
2415 float volume,
2416 bool muted)
2417 {
2418 size_t frameCount = *pFrameCount;
2419 size_t notificationFrameCount = *pNotificationFrameCount;
2420 sp<IAfTrack> track;
2421 status_t lStatus;
2422 audio_output_flags_t outputFlags = mOutput->flags;
2423 audio_output_flags_t requestedFlags = *flags;
2424 uint32_t sampleRate;
2425
2426 if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) {
2427 lStatus = BAD_VALUE;
2428 goto Exit;
2429 }
2430
2431 if (*pSampleRate == 0) {
2432 *pSampleRate = mSampleRate;
2433 }
2434 sampleRate = *pSampleRate;
2435
2436 // special case for FAST flag considered OK if fast mixer is present
2437 if (hasFastMixer()) {
2438 outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST);
2439 }
2440
2441 // Check if requested flags are compatible with output stream flags
2442 if ((*flags & outputFlags) != *flags) {
2443 ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)",
2444 *flags, outputFlags);
2445 *flags = (audio_output_flags_t)(*flags & outputFlags);
2446 }
2447
2448 if (isBitPerfect) {
2449 audio_utils::lock_guard _l(mutex());
2450 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
2451 if (chain.get() != nullptr) {
2452 // Bit-perfect is required according to the configuration and preferred mixer
2453 // attributes, but it is not in the output flag from the client's request. Explicitly
2454 // adding bit-perfect flag to check the compatibility
2455 audio_output_flags_t flagsToCheck =
2456 (audio_output_flags_t)(*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2457 chain->checkOutputFlagCompatibility(&flagsToCheck);
2458 if ((flagsToCheck & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE) {
2459 ALOGE("%s cannot create track as there is data-processing effect attached to "
2460 "given session id(%d)", __func__, sessionId);
2461 lStatus = BAD_VALUE;
2462 goto Exit;
2463 }
2464 *flags = flagsToCheck;
2465 }
2466 }
2467
2468 // client expresses a preference for FAST, but we get the final say
2469 if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2470 if (
2471 // PCM data
2472 audio_is_linear_pcm(format) &&
2473 // TODO: extract as a data library function that checks that a computationally
2474 // expensive downmixer is not required: isFastOutputChannelConversion()
2475 (channelMask == (mChannelMask | mHapticChannelMask) ||
2476 mChannelMask != AUDIO_CHANNEL_OUT_STEREO ||
2477 (channelMask == AUDIO_CHANNEL_OUT_MONO
2478 /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) &&
2479 // hardware sample rate
2480 (sampleRate == mSampleRate) &&
2481 // normal mixer has an associated fast mixer
2482 hasFastMixer() &&
2483 // there are sufficient fast track slots available
2484 (mFastTrackAvailMask != 0)
2485 // FIXME test that MixerThread for this fast track has a capable output HAL
2486 // FIXME add a permission test also?
2487 ) {
2488 // static tracks can have any nonzero framecount, streaming tracks check against minimum.
2489 if (sharedBuffer == 0) {
2490 // read the fast track multiplier property the first time it is needed
2491 int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit);
2492 if (ok != 0) {
2493 ALOGE("%s pthread_once failed: %d", __func__, ok);
2494 }
2495 frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0
2496 }
2497
2498 // check compatibility with audio effects.
2499 { // scope for mutex()
2500 audio_utils::lock_guard _l(mutex());
2501 for (audio_session_t session : {
2502 AUDIO_SESSION_DEVICE,
2503 AUDIO_SESSION_OUTPUT_STAGE,
2504 AUDIO_SESSION_OUTPUT_MIX,
2505 sessionId,
2506 }) {
2507 sp<IAfEffectChain> chain = getEffectChain_l(session);
2508 if (chain.get() != nullptr) {
2509 audio_output_flags_t old = *flags;
2510 chain->checkOutputFlagCompatibility(flags);
2511 if (old != *flags) {
2512 ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x",
2513 (int)session, (int)old, (int)*flags);
2514 }
2515 }
2516 }
2517 }
2518 ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0,
2519 "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
2520 frameCount, mFrameCount);
2521 } else {
2522 ALOGD("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu "
2523 "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
2524 "sampleRate=%u mSampleRate=%u "
2525 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
2526 sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
2527 audio_is_linear_pcm(format), channelMask, sampleRate,
2528 mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
2529 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2530 }
2531 }
2532
2533 if (!audio_has_proportional_frames(format)) {
2534 if (sharedBuffer != 0) {
2535 // Same comment as below about ignoring frameCount parameter for set()
2536 frameCount = sharedBuffer->size();
2537 } else if (frameCount == 0) {
2538 frameCount = mNormalFrameCount;
2539 }
2540 if (notificationFrameCount != frameCount) {
2541 notificationFrameCount = frameCount;
2542 }
2543 } else if (sharedBuffer != 0) {
2544 // FIXME: Ensure client side memory buffers need
2545 // not have additional alignment beyond sample
2546 // (e.g. 16 bit stereo accessed as 32 bit frame).
2547 size_t alignment = audio_bytes_per_sample(format);
2548 if (alignment & 1) {
2549 // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java).
2550 alignment = 1;
2551 }
2552 uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
2553 size_t frameSize = channelCount * audio_bytes_per_sample(format);
2554 if (channelCount > 1) {
2555 // More than 2 channels does not require stronger alignment than stereo
2556 alignment <<= 1;
2557 }
2558 if (((uintptr_t)sharedBuffer->unsecurePointer() & (alignment - 1)) != 0) {
2559 ALOGE("Invalid buffer alignment: address %p, channel count %u",
2560 sharedBuffer->unsecurePointer(), channelCount);
2561 lStatus = BAD_VALUE;
2562 goto Exit;
2563 }
2564
2565 // When initializing a shared buffer AudioTrack via constructors,
2566 // there's no frameCount parameter.
2567 // But when initializing a shared buffer AudioTrack via set(),
2568 // there _is_ a frameCount parameter. We silently ignore it.
2569 frameCount = sharedBuffer->size() / frameSize;
2570 } else {
2571 size_t minFrameCount = 0;
2572 // For fast tracks we try to respect the application's request for notifications per buffer.
2573 if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2574 if (notificationsPerBuffer > 0) {
2575 // Avoid possible arithmetic overflow during multiplication.
2576 if (notificationsPerBuffer > SIZE_MAX / mFrameCount) {
2577 ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu",
2578 notificationsPerBuffer, mFrameCount);
2579 } else {
2580 minFrameCount = mFrameCount * notificationsPerBuffer;
2581 }
2582 }
2583 } else {
2584 // For normal PCM streaming tracks, update minimum frame count.
2585 // Buffer depth is forced to be at least 2 x the normal mixer frame count and
2586 // cover audio hardware latency.
2587 // This is probably too conservative, but legacy application code may depend on it.
2588 // If you change this calculation, also review the start threshold which is related.
2589 uint32_t latencyMs = latency_l();
2590 if (latencyMs == 0) {
2591 ALOGE("Error when retrieving output stream latency");
2592 lStatus = UNKNOWN_ERROR;
2593 goto Exit;
2594 }
2595
2596 minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount,
2597 mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/);
2598
2599 }
2600 if (frameCount < minFrameCount) {
2601 frameCount = minFrameCount;
2602 }
2603 }
2604
2605 // Make sure that application is notified with sufficient margin before underrun.
2606 // The client can divide the AudioTrack buffer into sub-buffers,
2607 // and expresses its desire to server as the notification frame count.
2608 if (sharedBuffer == 0 && audio_is_linear_pcm(format)) {
2609 size_t maxNotificationFrames;
2610 if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2611 // notify every HAL buffer, regardless of the size of the track buffer
2612 maxNotificationFrames = mFrameCount;
2613 } else {
2614 // Triple buffer the notification period for a triple buffered mixer period;
2615 // otherwise, double buffering for the notification period is fine.
2616 //
2617 // TODO: This should be moved to AudioTrack to modify the notification period
2618 // on AudioTrack::setBufferSizeInFrames() changes.
2619 const int nBuffering =
2620 (uint64_t{frameCount} * mSampleRate)
2621 / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2;
2622
2623 maxNotificationFrames = frameCount / nBuffering;
2624 // If client requested a fast track but this was denied, then use the smaller maximum.
2625 if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) {
2626 size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000;
2627 if (maxNotificationFrames > maxNotificationFramesFastDenied) {
2628 maxNotificationFrames = maxNotificationFramesFastDenied;
2629 }
2630 }
2631 }
2632 if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
2633 if (notificationFrameCount == 0) {
2634 ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu",
2635 maxNotificationFrames, frameCount);
2636 } else {
2637 ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu",
2638 notificationFrameCount, maxNotificationFrames, frameCount);
2639 }
2640 notificationFrameCount = maxNotificationFrames;
2641 }
2642 }
2643
2644 *pFrameCount = frameCount;
2645 *pNotificationFrameCount = notificationFrameCount;
2646
2647 switch (mType) {
2648 case BIT_PERFECT:
2649 if (isBitPerfect) {
2650 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
2651 ALOGE("%s, bad parameter when request streaming bit-perfect, sampleRate=%u, "
2652 "format=%#x, channelMask=%#x, mSampleRate=%u, mFormat=%#x, mChannelMask=%#x",
2653 __func__, sampleRate, format, channelMask, mSampleRate, mFormat,
2654 mChannelMask);
2655 lStatus = BAD_VALUE;
2656 goto Exit;
2657 }
2658 }
2659 break;
2660
2661 case DIRECT:
2662 if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()?
2663 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
2664 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x "
2665 "for output %p with format %#x",
2666 sampleRate, format, channelMask, mOutput, mFormat);
2667 lStatus = BAD_VALUE;
2668 goto Exit;
2669 }
2670 }
2671 break;
2672
2673 case OFFLOAD:
2674 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
2675 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \""
2676 "for output %p with format %#x",
2677 sampleRate, format, channelMask, mOutput, mFormat);
2678 lStatus = BAD_VALUE;
2679 goto Exit;
2680 }
2681 break;
2682
2683 default:
2684 if (!audio_is_linear_pcm(format)) {
2685 ALOGE("createTrack_l() Bad parameter: format %#x \""
2686 "for output %p with format %#x",
2687 format, mOutput, mFormat);
2688 lStatus = BAD_VALUE;
2689 goto Exit;
2690 }
2691 if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
2692 ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
2693 lStatus = BAD_VALUE;
2694 goto Exit;
2695 }
2696 break;
2697
2698 }
2699
2700 lStatus = initCheck();
2701 if (lStatus != NO_ERROR) {
2702 ALOGE("createTrack_l() audio driver not initialized");
2703 goto Exit;
2704 }
2705
2706 { // scope for mutex()
2707 audio_utils::lock_guard _l(mutex());
2708
2709 // all tracks in same audio session must share the same routing strategy otherwise
2710 // conflicts will happen when tracks are moved from one output to another by audio policy
2711 // manager
2712 product_strategy_t strategy = getStrategyForStream(streamType);
2713 for (size_t i = 0; i < mTracks.size(); ++i) {
2714 sp<IAfTrack> t = mTracks[i];
2715 if (t != 0 && t->isExternalTrack()) {
2716 product_strategy_t actual = getStrategyForStream(t->streamType());
2717 if (sessionId == t->sessionId() && strategy != actual) {
2718 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
2719 strategy, actual);
2720 lStatus = BAD_VALUE;
2721 goto Exit;
2722 }
2723 }
2724 }
2725
2726 // Set DIRECT/OFFLOAD flag if current thread is DirectOutputThread/OffloadThread.
2727 // This can happen when the playback is rerouted to direct output/offload thread by
2728 // dynamic audio policy.
2729 // Do NOT report the flag changes back to client, since the client
2730 // doesn't explicitly request a direct/offload flag.
2731 audio_output_flags_t trackFlags = *flags;
2732 if (mType == DIRECT) {
2733 trackFlags = static_cast<audio_output_flags_t>(trackFlags | AUDIO_OUTPUT_FLAG_DIRECT);
2734 } else if (mType == OFFLOAD) {
2735 trackFlags = static_cast<audio_output_flags_t>(trackFlags |
2736 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT);
2737 }
2738 *afTrackFlags = trackFlags;
2739
2740 track = IAfTrack::create(this, client, streamType, attr, sampleRate, format,
2741 channelMask, frameCount,
2742 nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer,
2743 sessionId, creatorPid, attributionSource, trackFlags,
2744 IAfTrackBase::TYPE_DEFAULT, portId, SIZE_MAX /*frameCountToBeReady*/,
2745 speed, isSpatialized, isBitPerfect, volume, muted);
2746
2747 lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
2748 if (lStatus != NO_ERROR) {
2749 ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus);
2750 // track must be cleared from the caller as the caller has the AF lock
2751 goto Exit;
2752 }
2753 mTracks.add(track);
2754 {
2755 audio_utils::lock_guard _atCbL(audioTrackCbMutex());
2756 if (callback.get() != nullptr) {
2757 mAudioTrackCallbacks.emplace(track, callback);
2758 }
2759 }
2760
2761 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
2762 if (chain != 0) {
2763 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
2764 track->setMainBuffer(chain->inBuffer());
2765 chain->setStrategy(getStrategyForStream(track->streamType()));
2766 chain->incTrackCnt();
2767 }
2768
2769 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) {
2770 pid_t callingPid = IPCThreadState::self()->getCallingPid();
2771 // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
2772 // so ask activity manager to do this on our behalf
2773 sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
2774 }
2775 }
2776
2777 lStatus = NO_ERROR;
2778
2779 Exit:
2780 *status = lStatus;
2781 return track;
2782 }
2783
2784 template<typename T>
remove(const sp<T> & track)2785 ssize_t PlaybackThread::Tracks<T>::remove(const sp<T>& track)
2786 {
2787 const int trackId = track->id();
2788 const ssize_t index = mTracks.remove(track);
2789 if (index >= 0) {
2790 if (mSaveDeletedTrackIds) {
2791 // We can't directly access mAudioMixer since the caller may be outside of threadLoop.
2792 // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update,
2793 // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer.
2794 mDeletedTrackIds.emplace(trackId);
2795 }
2796 }
2797 return index;
2798 }
2799
correctLatency_l(uint32_t latency) const2800 uint32_t PlaybackThread::correctLatency_l(uint32_t latency) const
2801 {
2802 return latency;
2803 }
2804
latency() const2805 uint32_t PlaybackThread::latency() const
2806 {
2807 audio_utils::lock_guard _l(mutex());
2808 return latency_l();
2809 }
latency_l() const2810 uint32_t PlaybackThread::latency_l() const
2811 NO_THREAD_SAFETY_ANALYSIS
2812 // Fix later.
2813 {
2814 uint32_t latency;
2815 if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) {
2816 return correctLatency_l(latency);
2817 }
2818 return 0;
2819 }
2820
setMasterVolume(float value)2821 void PlaybackThread::setMasterVolume(float value)
2822 {
2823 audio_utils::lock_guard _l(mutex());
2824 // Don't apply master volume in SW if our HAL can do it for us.
2825 if (mOutput && mOutput->audioHwDev &&
2826 mOutput->audioHwDev->canSetMasterVolume()) {
2827 mMasterVolume = 1.0;
2828 } else {
2829 mMasterVolume = value;
2830 }
2831 }
2832
setMasterBalance(float balance)2833 void PlaybackThread::setMasterBalance(float balance)
2834 {
2835 mMasterBalance.store(balance);
2836 }
2837
setMasterMute(bool muted)2838 void PlaybackThread::setMasterMute(bool muted)
2839 {
2840 if (isDuplicating()) {
2841 return;
2842 }
2843 audio_utils::lock_guard _l(mutex());
2844 // Don't apply master mute in SW if our HAL can do it for us.
2845 if (mOutput && mOutput->audioHwDev &&
2846 mOutput->audioHwDev->canSetMasterMute()) {
2847 mMasterMute = false;
2848 } else {
2849 mMasterMute = muted;
2850 }
2851 }
2852
setStreamVolume(audio_stream_type_t stream,float value,bool muted)2853 void PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value, bool muted)
2854 {
2855 ALOGV("%s: stream %d value %f muted %d", __func__, stream, value, muted);
2856 audio_utils::lock_guard _l(mutex());
2857 mStreamTypes[stream].volume = value;
2858 if (com_android_media_audio_ring_my_car()) {
2859 mStreamTypes[stream].mute = muted;
2860 }
2861 broadcast_l();
2862 }
2863
setStreamMute(audio_stream_type_t stream,bool muted)2864 void PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
2865 {
2866 audio_utils::lock_guard _l(mutex());
2867 mStreamTypes[stream].mute = muted;
2868 broadcast_l();
2869 }
2870
streamVolume(audio_stream_type_t stream) const2871 float PlaybackThread::streamVolume(audio_stream_type_t stream) const
2872 {
2873 audio_utils::lock_guard _l(mutex());
2874 return mStreamTypes[stream].volume;
2875 }
2876
setPortsVolume(const std::vector<audio_port_handle_t> & portIds,float volume,bool muted)2877 status_t PlaybackThread::setPortsVolume(
2878 const std::vector<audio_port_handle_t>& portIds, float volume, bool muted) {
2879 audio_utils::lock_guard _l(mutex());
2880 for (const auto& portId : portIds) {
2881 for (size_t i = 0; i < mTracks.size(); i++) {
2882 sp<IAfTrack> track = mTracks[i].get();
2883 if (portId == track->portId()) {
2884 track->setPortVolume(volume);
2885 track->setPortMute(muted);
2886 break;
2887 }
2888 }
2889 }
2890 broadcast_l();
2891 return NO_ERROR;
2892 }
2893
setVolumeForOutput_l(float left,float right) const2894 void PlaybackThread::setVolumeForOutput_l(float left, float right) const
2895 {
2896 mOutput->stream->setVolume(left, right);
2897 }
2898
checkUpdateTrackMetadataForUid(uid_t uid)2899 void PlaybackThread::checkUpdateTrackMetadataForUid(uid_t uid) {
2900 audio_utils::lock_guard _l(mutex());
2901 for (const sp<IAfTrack>& track : mActiveTracks) {
2902 if (track->uid() == uid) {
2903 track->setMetadataHasChanged();
2904 }
2905 }
2906 }
2907
2908 // addTrack_l() must be called with ThreadBase::mutex() held
addTrack_l(const sp<IAfTrack> & track)2909 status_t PlaybackThread::addTrack_l(const sp<IAfTrack>& track)
2910 {
2911 status_t status = ALREADY_EXISTS;
2912
2913 if (mActiveTracks.indexOf(track) < 0) {
2914 // the track is newly added, make sure it fills up all its
2915 // buffers before playing. This is to ensure the client will
2916 // effectively get the latency it requested.
2917 if (track->isExternalTrack()) {
2918 IAfTrackBase::track_state state = track->state();
2919 // Because the track is not on the ActiveTracks,
2920 // at this point, only the TrackHandle will be adding the track.
2921 mutex().unlock();
2922 status = AudioSystem::startOutput(track->portId());
2923 mutex().lock();
2924 // abort track was stopped/paused while we released the lock
2925 if (state != track->state()) {
2926 if (status == NO_ERROR) {
2927 mutex().unlock();
2928 AudioSystem::stopOutput(track->portId());
2929 mutex().lock();
2930 }
2931 return INVALID_OPERATION;
2932 }
2933 // abort if start is rejected by audio policy manager
2934 if (status != NO_ERROR) {
2935 // Do not replace the error if it is DEAD_OBJECT. When this happens, it indicates
2936 // current playback thread is reopened, which may happen when clients set preferred
2937 // mixer configuration. Returning DEAD_OBJECT will make the client restore track
2938 // immediately.
2939 return status == DEAD_OBJECT ? status : PERMISSION_DENIED;
2940 }
2941 #ifdef ADD_BATTERY_DATA
2942 // to track the speaker usage
2943 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
2944 #endif
2945 sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId());
2946 }
2947
2948 // set retry count for buffer fill
2949 if (track->isOffloaded()) {
2950 if (track->isStopping_1()) {
2951 track->retryCount() = kMaxTrackStopRetriesOffload;
2952 } else {
2953 track->retryCount() = kMaxTrackStartupRetriesOffload;
2954 }
2955 track->fillingStatus() = mStandby ? IAfTrack::FS_FILLING : IAfTrack::FS_FILLED;
2956 } else {
2957 track->retryCount() = kMaxTrackStartupRetries;
2958 track->fillingStatus() =
2959 track->sharedBuffer() != 0 ? IAfTrack::FS_FILLED : IAfTrack::FS_FILLING;
2960 }
2961
2962 sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId());
2963 if (mHapticChannelMask != AUDIO_CHANNEL_NONE
2964 && ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
2965 || (chain != nullptr && chain->containsHapticGeneratingEffect()))) {
2966 // Unlock due to VibratorService will lock for this call and will
2967 // call Tracks.mute/unmute which also require thread's lock.
2968 mutex().unlock();
2969 const os::HapticScale hapticScale = afutils::onExternalVibrationStart(
2970 track->getExternalVibration());
2971 std::optional<media::AudioVibratorInfo> vibratorInfo;
2972 {
2973 // TODO(b/184194780): Use the vibrator information from the vibrator that will be
2974 // used to play this track.
2975 audio_utils::lock_guard _l(mAfThreadCallback->mutex());
2976 vibratorInfo = mAfThreadCallback->getDefaultVibratorInfo_l();
2977 }
2978 mutex().lock();
2979 track->setHapticScale(hapticScale);
2980 if (vibratorInfo) {
2981 track->setHapticMaxAmplitude(vibratorInfo->maxAmplitude);
2982 }
2983
2984 // Haptic playback should be enabled by vibrator service.
2985 if (track->getHapticPlaybackEnabled()) {
2986 // Disable haptic playback of all active track to ensure only
2987 // one track playing haptic if current track should play haptic.
2988 for (const auto &t : mActiveTracks) {
2989 t->setHapticPlaybackEnabled(false);
2990 }
2991 }
2992
2993 // Set haptic intensity for effect
2994 if (chain != nullptr) {
2995 chain->setHapticScale_l(track->id(), hapticScale);
2996 }
2997 }
2998
2999 track->setResetDone(false);
3000 track->resetPresentationComplete();
3001
3002 // Do not release the ThreadBase mutex after the track is added to mActiveTracks unless
3003 // all key changes are complete. It is possible that the threadLoop will begin
3004 // processing the added track immediately after the ThreadBase mutex is released.
3005 mActiveTracks.add(track);
3006
3007 if (chain != 0) {
3008 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(),
3009 track->sessionId());
3010 chain->incActiveTrackCnt();
3011 }
3012
3013 track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics
3014 status = NO_ERROR;
3015 }
3016
3017 onAddNewTrack_l();
3018 return status;
3019 }
3020
destroyTrack_l(const sp<IAfTrack> & track)3021 bool PlaybackThread::destroyTrack_l(const sp<IAfTrack>& track)
3022 {
3023 track->terminate();
3024 // active tracks are removed by threadLoop()
3025 bool trackActive = (mActiveTracks.indexOf(track) >= 0);
3026 track->setState(IAfTrackBase::STOPPED);
3027 if (!trackActive) {
3028 removeTrack_l(track);
3029 } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) {
3030 if (track->isPausePending()) {
3031 track->pauseAck();
3032 }
3033 track->setState(IAfTrackBase::STOPPING_1);
3034 }
3035
3036 return trackActive;
3037 }
3038
removeTrack_l(const sp<IAfTrack> & track)3039 void PlaybackThread::removeTrack_l(const sp<IAfTrack>& track)
3040 {
3041 track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
3042
3043 String8 result;
3044 track->appendDump(result, false /* active */);
3045 mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str());
3046
3047 mTracks.remove(track);
3048 {
3049 audio_utils::lock_guard _atCbL(audioTrackCbMutex());
3050 mAudioTrackCallbacks.erase(track);
3051 }
3052 if (track->isFastTrack()) {
3053 int index = track->fastIndex();
3054 ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks);
3055 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
3056 mFastTrackAvailMask |= 1 << index;
3057 // redundant as track is about to be destroyed, for dumpsys only
3058 track->fastIndex() = -1;
3059 }
3060 sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId());
3061 if (chain != 0) {
3062 chain->decTrackCnt();
3063 }
3064 }
3065
getTrackPortIds_l()3066 std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds_l()
3067 {
3068 std::set<int32_t> result;
3069 for (const auto& t : mTracks) {
3070 if (t->isExternalTrack()) {
3071 result.insert(t->portId());
3072 }
3073 }
3074 return result;
3075 }
3076
getTrackPortIds()3077 std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds()
3078 {
3079 audio_utils::lock_guard _l(mutex());
3080 return getTrackPortIds_l();
3081 }
3082
getParameters(const String8 & keys)3083 String8 PlaybackThread::getParameters(const String8& keys)
3084 {
3085 audio_utils::lock_guard _l(mutex());
3086 String8 out_s8;
3087 if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) {
3088 return out_s8;
3089 }
3090 return {};
3091 }
3092
selectPresentation(int presentationId,int programId)3093 status_t DirectOutputThread::selectPresentation(int presentationId, int programId) {
3094 audio_utils::lock_guard _l(mutex());
3095 if (!isStreamInitialized()) {
3096 return NO_INIT;
3097 }
3098 return mOutput->stream->selectPresentation(presentationId, programId);
3099 }
3100
ioConfigChanged_l(audio_io_config_event_t event,pid_t pid,audio_port_handle_t portId)3101 void PlaybackThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid,
3102 audio_port_handle_t portId) {
3103 ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event);
3104 sp<AudioIoDescriptor> desc;
3105 const struct audio_patch patch = isMsdDevice() ? mDownStreamPatch : mPatch;
3106 switch (event) {
3107 case AUDIO_OUTPUT_OPENED:
3108 case AUDIO_OUTPUT_REGISTERED:
3109 case AUDIO_OUTPUT_CONFIG_CHANGED:
3110 desc = sp<AudioIoDescriptor>::make(mId, patch, false /*isInput*/,
3111 mSampleRate, mFormat, mChannelMask,
3112 // FIXME AudioFlinger::frameCount(audio_io_handle_t) instead of mNormalFrameCount?
3113 mNormalFrameCount, mFrameCount, latency_l());
3114 break;
3115 case AUDIO_CLIENT_STARTED:
3116 desc = sp<AudioIoDescriptor>::make(mId, patch, portId);
3117 break;
3118 case AUDIO_OUTPUT_CLOSED:
3119 default:
3120 desc = sp<AudioIoDescriptor>::make(mId);
3121 break;
3122 }
3123 mAfThreadCallback->ioConfigChanged_l(event, desc, pid);
3124 }
3125
onWriteReady()3126 void PlaybackThread::onWriteReady()
3127 {
3128 mCallbackThread->resetWriteBlocked();
3129 }
3130
onDrainReady()3131 void PlaybackThread::onDrainReady()
3132 {
3133 mCallbackThread->resetDraining();
3134 }
3135
onError(bool isHardError)3136 void PlaybackThread::onError(bool isHardError)
3137 {
3138 mCallbackThread->setAsyncError(isHardError);
3139 }
3140
onCodecFormatChanged(const std::vector<uint8_t> & metadataBs)3141 void PlaybackThread::onCodecFormatChanged(
3142 const std::vector<uint8_t>& metadataBs)
3143 {
3144 const auto weakPointerThis = wp<PlaybackThread>::fromExisting(this);
3145 std::thread([this, metadataBs, weakPointerThis]() {
3146 const sp<PlaybackThread> playbackThread = weakPointerThis.promote();
3147 if (playbackThread == nullptr) {
3148 ALOGW("PlaybackThread was destroyed, skip codec format change event");
3149 return;
3150 }
3151
3152 audio_utils::metadata::Data metadata =
3153 audio_utils::metadata::dataFromByteString(metadataBs);
3154 if (metadata.empty()) {
3155 ALOGW("Can not transform the buffer to audio metadata, %s, %d",
3156 reinterpret_cast<char*>(const_cast<uint8_t*>(metadataBs.data())),
3157 (int)metadataBs.size());
3158 return;
3159 }
3160
3161 audio_utils::metadata::ByteString metaDataStr =
3162 audio_utils::metadata::byteStringFromData(metadata);
3163 std::vector metadataVec(metaDataStr.begin(), metaDataStr.end());
3164 audio_utils::lock_guard _l(audioTrackCbMutex());
3165 for (const auto& callbackPair : mAudioTrackCallbacks) {
3166 callbackPair.second->onCodecFormatChanged(metadataVec);
3167 }
3168 }).detach();
3169 }
3170
resetWriteBlocked(uint32_t sequence)3171 void PlaybackThread::resetWriteBlocked(uint32_t sequence)
3172 {
3173 audio_utils::lock_guard _l(mutex());
3174 // reject out of sequence requests
3175 if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) {
3176 mWriteAckSequence &= ~1;
3177 mWaitWorkCV.notify_one();
3178 }
3179 }
3180
resetDraining(uint32_t sequence)3181 void PlaybackThread::resetDraining(uint32_t sequence)
3182 {
3183 audio_utils::lock_guard _l(mutex());
3184 // reject out of sequence requests
3185 if ((mDrainSequence & 1) && (sequence == mDrainSequence)) {
3186 // Register discontinuity when HW drain is completed because that can cause
3187 // the timestamp frame position to reset to 0 for direct and offload threads.
3188 // (Out of sequence requests are ignored, since the discontinuity would be handled
3189 // elsewhere, e.g. in flush).
3190 mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
3191 mDrainSequence &= ~1;
3192 mWaitWorkCV.notify_one();
3193 }
3194 }
3195
readOutputParameters_l()3196 void PlaybackThread::readOutputParameters_l()
3197 NO_THREAD_SAFETY_ANALYSIS
3198 // 'moveEffectChain_ll' requires holding mutex 'AudioFlinger_Mutex' exclusively
3199 {
3200 // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL
3201 const audio_config_base_t audioConfig = mOutput->getAudioProperties();
3202 mSampleRate = audioConfig.sample_rate;
3203 mChannelMask = audioConfig.channel_mask;
3204 if (!audio_is_output_channel(mChannelMask)) {
3205 LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask);
3206 }
3207 if (hasMixer() && !isValidPcmSinkChannelMask(mChannelMask)) {
3208 LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output",
3209 mChannelMask);
3210 }
3211
3212 if (mMixerChannelMask == AUDIO_CHANNEL_NONE) {
3213 mMixerChannelMask = mChannelMask;
3214 }
3215
3216 mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
3217 mBalance.setChannelMask(mChannelMask);
3218
3219 uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mMixerChannelMask);
3220
3221 // Get actual HAL format.
3222 status_t result = mOutput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat);
3223 LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result);
3224 // Get format from the shim, which will be different than the HAL format
3225 // if playing compressed audio over HDMI passthrough.
3226 mFormat = audioConfig.format;
3227 if (!audio_is_valid_format(mFormat)) {
3228 LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat);
3229 }
3230 if (hasMixer() && !isValidPcmSinkFormat(mFormat)) {
3231 LOG_FATAL("HAL format %#x not supported for mixed output",
3232 mFormat);
3233 }
3234 mFrameSize = mOutput->getFrameSize();
3235 result = mOutput->stream->getBufferSize(&mBufferSize);
3236 LOG_ALWAYS_FATAL_IF(result != OK,
3237 "Error when retrieving output stream buffer size: %d", result);
3238 mFrameCount = mBufferSize / mFrameSize;
3239 if (hasMixer() && (mFrameCount & 15)) {
3240 ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames",
3241 mFrameCount);
3242 }
3243
3244 mHwSupportsPause = false;
3245 if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
3246 bool supportsPause = false, supportsResume = false;
3247 if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) {
3248 if (supportsPause && supportsResume) {
3249 mHwSupportsPause = true;
3250 } else if (supportsPause) {
3251 ALOGW("direct output implements pause but not resume");
3252 } else if (supportsResume) {
3253 ALOGW("direct output implements resume but not pause");
3254 }
3255 }
3256 }
3257 if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
3258 LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume");
3259 }
3260
3261 if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) {
3262 // For best precision, we use float instead of the associated output
3263 // device format (typically PCM 16 bit).
3264
3265 mFormat = AUDIO_FORMAT_PCM_FLOAT;
3266 mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
3267 mBufferSize = mFrameSize * mFrameCount;
3268
3269 // TODO: We currently use the associated output device channel mask and sample rate.
3270 // (1) Perhaps use the ORed channel mask of all downstream MixerThreads
3271 // (if a valid mask) to avoid premature downmix.
3272 // (2) Perhaps use the maximum sample rate of all downstream MixerThreads
3273 // instead of the output device sample rate to avoid loss of high frequency information.
3274 // This may need to be updated as MixerThread/OutputTracks are added and not here.
3275 }
3276
3277 // Calculate size of normal sink buffer relative to the HAL output buffer size
3278 double multiplier = 1.0;
3279 // Note: mType == SPATIALIZER does not support FastMixer and DEEP is by definition not "fast"
3280 if ((mType == MIXER && !(mOutput->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) &&
3281 (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) {
3282 size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
3283 size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
3284
3285 // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
3286 minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
3287 maxNormalFrameCount = maxNormalFrameCount & ~15;
3288 if (maxNormalFrameCount < minNormalFrameCount) {
3289 maxNormalFrameCount = minNormalFrameCount;
3290 }
3291 multiplier = (double) minNormalFrameCount / (double) mFrameCount;
3292 if (multiplier <= 1.0) {
3293 multiplier = 1.0;
3294 } else if (multiplier <= 2.0) {
3295 if (2 * mFrameCount <= maxNormalFrameCount) {
3296 multiplier = 2.0;
3297 } else {
3298 multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
3299 }
3300 } else {
3301 multiplier = floor(multiplier);
3302 }
3303 }
3304 mNormalFrameCount = multiplier * mFrameCount;
3305 // round up to nearest 16 frames to satisfy AudioMixer
3306 if (hasMixer()) {
3307 mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
3308 }
3309 ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames",
3310 (size_t)mFrameCount, mNormalFrameCount);
3311
3312 // Check if we want to throttle the processing to no more than 2x normal rate
3313 mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */);
3314 mThreadThrottleTimeMs = 0;
3315 mThreadThrottleEndMs = 0;
3316 mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate);
3317
3318 // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames.
3319 // Originally this was int16_t[] array, need to remove legacy implications.
3320 free(mSinkBuffer);
3321 mSinkBuffer = NULL;
3322
3323 // For sink buffer size, we use the frame size from the downstream sink to avoid problems
3324 // with non PCM formats for compressed music, e.g. AAC, and Offload threads.
3325 const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
3326 (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
3327
3328 // We resize the mMixerBuffer according to the requirements of the sink buffer which
3329 // drives the output.
3330 free(mMixerBuffer);
3331 mMixerBuffer = NULL;
3332 if (mMixerBufferEnabled) {
3333 mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT.
3334 mMixerBufferSize = mNormalFrameCount * mixerChannelCount
3335 * audio_bytes_per_sample(mMixerBufferFormat);
3336 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
3337 }
3338 free(mEffectBuffer);
3339 mEffectBuffer = NULL;
3340 if (mEffectBufferEnabled) {
3341 mEffectBufferFormat = AUDIO_FORMAT_PCM_FLOAT;
3342 mEffectBufferSize = mNormalFrameCount * mixerChannelCount
3343 * audio_bytes_per_sample(mEffectBufferFormat);
3344 (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize);
3345 }
3346
3347 if (mType == SPATIALIZER) {
3348 free(mPostSpatializerBuffer);
3349 mPostSpatializerBuffer = nullptr;
3350 mPostSpatializerBufferSize = mNormalFrameCount * mChannelCount
3351 * audio_bytes_per_sample(mEffectBufferFormat);
3352 (void)posix_memalign(&mPostSpatializerBuffer, 32, mPostSpatializerBufferSize);
3353 }
3354
3355 mHapticChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
3356 mChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & ~mHapticChannelMask);
3357 mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask);
3358 mChannelCount -= mHapticChannelCount;
3359 mMixerChannelMask = static_cast<audio_channel_mask_t>(mMixerChannelMask & ~mHapticChannelMask);
3360
3361 // force reconfiguration of effect chains and engines to take new buffer size and audio
3362 // parameters into account
3363 // Note that mutex() is not held when readOutputParameters_l() is called from the constructor
3364 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
3365 // matter.
3366 // create a copy of mEffectChains as calling moveEffectChain_ll()
3367 // can reorder some effect chains
3368 Vector<sp<IAfEffectChain>> effectChains = mEffectChains;
3369 for (size_t i = 0; i < effectChains.size(); i ++) {
3370 mAfThreadCallback->moveEffectChain_ll(effectChains[i]->sessionId(),
3371 this/* srcThread */, this/* dstThread */);
3372 }
3373
3374 audio_output_flags_t flags = mOutput->flags;
3375 mediametrics::LogItem item(mThreadMetrics.getMetricsId()); // TODO: method in ThreadMetrics?
3376 item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS)
3377 .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str())
3378 .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate)
3379 .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask)
3380 .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount)
3381 .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mNormalFrameCount)
3382 .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str())
3383 .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK,
3384 (int32_t)mHapticChannelMask)
3385 .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT,
3386 (int32_t)mHapticChannelCount)
3387 .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING,
3388 IAfThreadBase::formatToString(mHALFormat).c_str())
3389 .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT,
3390 (int32_t)mFrameCount) // sic - added HAL
3391 ;
3392 uint32_t latencyMs;
3393 if (mOutput->stream->getLatency(&latencyMs) == NO_ERROR) {
3394 item.set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_LATENCYMS, (double)latencyMs);
3395 }
3396 item.record();
3397 }
3398
updateMetadata_l()3399 ThreadBase::MetadataUpdate PlaybackThread::updateMetadata_l()
3400 {
3401 if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) {
3402 return {}; // nothing to do
3403 }
3404 StreamOutHalInterface::SourceMetadata metadata;
3405 std::map<audio_session_t, std::vector<playback_track_metadata_v7_t> >allSessionsMetadata;
3406 for (const sp<IAfTrack>& track : mActiveTracks) {
3407 std::vector<playback_track_metadata_v7_t>& sessionMetadata =
3408 allSessionsMetadata[track->sessionId()];
3409 auto backInserter = std::back_inserter(sessionMetadata);
3410 // No track is invalid as this is called after prepareTrack_l in the same
3411 // critical section
3412 track->copyMetadataTo(backInserter);
3413 }
3414 std::vector<playback_track_metadata_v7_t> spatializedTracksMetaData;
3415 for (const auto& [session, sessionTrackMetadata] : allSessionsMetadata) {
3416 metadata.tracks.insert(metadata.tracks.end(),
3417 sessionTrackMetadata.begin(), sessionTrackMetadata.end());
3418 if (auto chain = getEffectChain_l(session) ; chain != nullptr) {
3419 chain->sendMetadata_l(sessionTrackMetadata, {});
3420 }
3421 if ((hasAudioSession_l(session) & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
3422 spatializedTracksMetaData.insert(spatializedTracksMetaData.end(),
3423 sessionTrackMetadata.begin(), sessionTrackMetadata.end());
3424 }
3425 }
3426 if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); chain != nullptr) {
3427 chain->sendMetadata_l(metadata.tracks, {});
3428 }
3429 if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); chain != nullptr) {
3430 chain->sendMetadata_l(metadata.tracks, spatializedTracksMetaData);
3431 }
3432 if (auto chain = getEffectChain_l(AUDIO_SESSION_DEVICE); chain != nullptr) {
3433 chain->sendMetadata_l(metadata.tracks, {});
3434 }
3435
3436 sendMetadataToBackend_l(metadata);
3437 MetadataUpdate change;
3438 change.playbackMetadataUpdate = metadata.tracks;
3439 return change;
3440 }
3441
sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata & metadata)3442 void PlaybackThread::sendMetadataToBackend_l(
3443 const StreamOutHalInterface::SourceMetadata& metadata)
3444 {
3445 mOutput->stream->updateSourceMetadata(metadata);
3446 };
3447
getRenderPosition(uint32_t * halFrames,uint32_t * dspFrames) const3448 status_t PlaybackThread::getRenderPosition(
3449 uint32_t* halFrames, uint32_t* dspFrames) const
3450 {
3451 if (halFrames == NULL || dspFrames == NULL) {
3452 return BAD_VALUE;
3453 }
3454 audio_utils::lock_guard _l(mutex());
3455 if (initCheck() != NO_ERROR) {
3456 return INVALID_OPERATION;
3457 }
3458 int64_t framesWritten = mBytesWritten / mFrameSize;
3459 *halFrames = framesWritten;
3460
3461 if (isSuspended()) {
3462 // return an estimation of rendered frames when the output is suspended
3463 size_t latencyFrames = (latency_l() * mSampleRate) / 1000;
3464 *dspFrames = (uint32_t)
3465 (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0);
3466 return NO_ERROR;
3467 } else {
3468 status_t status;
3469 uint64_t frames = 0;
3470 status = mOutput->getRenderPosition(&frames);
3471 *dspFrames = (uint32_t)frames;
3472 return status;
3473 }
3474 }
3475
getStrategyForSession_l(audio_session_t sessionId) const3476 product_strategy_t PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) const
3477 {
3478 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
3479 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
3480 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
3481 return getStrategyForStream(AUDIO_STREAM_MUSIC);
3482 }
3483 for (size_t i = 0; i < mTracks.size(); i++) {
3484 sp<IAfTrack> track = mTracks[i];
3485 if (sessionId == track->sessionId() && !track->isInvalid()) {
3486 return getStrategyForStream(track->streamType());
3487 }
3488 }
3489 return getStrategyForStream(AUDIO_STREAM_MUSIC);
3490 }
3491
3492
getOutput() const3493 AudioStreamOut* PlaybackThread::getOutput() const
3494 {
3495 audio_utils::lock_guard _l(mutex());
3496 return mOutput;
3497 }
3498
clearOutput()3499 AudioStreamOut* PlaybackThread::clearOutput()
3500 {
3501 audio_utils::lock_guard _l(mutex());
3502 AudioStreamOut *output = mOutput;
3503 mOutput = NULL;
3504 // FIXME FastMixer might also have a raw ptr to mOutputSink;
3505 // must push a NULL and wait for ack
3506 mOutputSink.clear();
3507 mPipeSink.clear();
3508 mNormalSink.clear();
3509 return output;
3510 }
3511
3512 // this method must always be called either with ThreadBase mutex() held or inside the thread loop
stream() const3513 sp<StreamHalInterface> PlaybackThread::stream() const
3514 {
3515 if (mOutput == NULL) {
3516 return NULL;
3517 }
3518 return mOutput->stream;
3519 }
3520
activeSleepTimeUs() const3521 uint32_t PlaybackThread::activeSleepTimeUs() const
3522 {
3523 return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
3524 }
3525
setSyncEvent(const sp<SyncEvent> & event)3526 status_t PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
3527 {
3528 if (!isValidSyncEvent(event)) {
3529 return BAD_VALUE;
3530 }
3531
3532 audio_utils::lock_guard _l(mutex());
3533
3534 for (size_t i = 0; i < mTracks.size(); ++i) {
3535 sp<IAfTrack> track = mTracks[i];
3536 if (event->triggerSession() == track->sessionId()) {
3537 (void) track->setSyncEvent(event);
3538 return NO_ERROR;
3539 }
3540 }
3541
3542 return NAME_NOT_FOUND;
3543 }
3544
isValidSyncEvent(const sp<SyncEvent> & event) const3545 bool PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
3546 {
3547 return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
3548 }
3549
threadLoop_removeTracks(const Vector<sp<IAfTrack>> & tracksToRemove)3550 void PlaybackThread::threadLoop_removeTracks(
3551 [[maybe_unused]] const Vector<sp<IAfTrack>>& tracksToRemove)
3552 {
3553 // Miscellaneous track cleanup when removed from the active list,
3554 // called without Thread lock but synchronized with threadLoop processing.
3555 #ifdef ADD_BATTERY_DATA
3556 for (const auto& track : tracksToRemove) {
3557 if (track->isExternalTrack()) {
3558 // to track the speaker usage
3559 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
3560 }
3561 }
3562 #endif
3563 }
3564
checkSilentMode_l()3565 void PlaybackThread::checkSilentMode_l()
3566 {
3567 if (property_get_bool("ro.audio.silent", false)) {
3568 ALOGW("ro.audio.silent is now ignored");
3569 }
3570 }
3571
3572 // shared by MIXER and DIRECT, overridden by DUPLICATING
threadLoop_write()3573 ssize_t PlaybackThread::threadLoop_write()
3574 {
3575 LOG_HIST_TS();
3576 mInWrite = true;
3577 ssize_t bytesWritten;
3578 const size_t offset = mCurrentWriteLength - mBytesRemaining;
3579
3580 // If an NBAIO sink is present, use it to write the normal mixer's submix
3581 if (mNormalSink != 0) {
3582
3583 const size_t count = mBytesRemaining / mFrameSize;
3584
3585 ATRACE_BEGIN("write");
3586 // update the setpoint when AudioFlinger::mScreenState changes
3587 const uint32_t screenState = mAfThreadCallback->getScreenState();
3588 if (screenState != mScreenState) {
3589 mScreenState = screenState;
3590 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
3591 if (pipe != NULL) {
3592 pipe->setAvgFrames((mScreenState & 1) ?
3593 (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
3594 }
3595 }
3596 ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
3597 ATRACE_END();
3598
3599 if (framesWritten > 0) {
3600 bytesWritten = framesWritten * mFrameSize;
3601
3602 #ifdef TEE_SINK
3603 mTee.write((char *)mSinkBuffer + offset, framesWritten);
3604 #endif
3605 } else {
3606 bytesWritten = framesWritten;
3607 }
3608 // otherwise use the HAL / AudioStreamOut directly
3609 } else {
3610 // Direct output and offload threads
3611
3612 if (mUseAsyncWrite) {
3613 ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
3614 mWriteAckSequence += 2;
3615 mWriteAckSequence |= 1;
3616 ALOG_ASSERT(mCallbackThread != 0);
3617 mCallbackThread->setWriteBlocked(mWriteAckSequence);
3618 }
3619 ATRACE_BEGIN("write");
3620 // FIXME We should have an implementation of timestamps for direct output threads.
3621 // They are used e.g for multichannel PCM playback over HDMI.
3622 bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
3623 ATRACE_END();
3624
3625 if (mUseAsyncWrite &&
3626 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
3627 // do not wait for async callback in case of error of full write
3628 mWriteAckSequence &= ~1;
3629 ALOG_ASSERT(mCallbackThread != 0);
3630 mCallbackThread->setWriteBlocked(mWriteAckSequence);
3631 }
3632 }
3633
3634 mNumWrites++;
3635 mInWrite = false;
3636 if (mStandby) {
3637 mThreadMetrics.logBeginInterval();
3638 mThreadSnapshot.onBegin();
3639 mStandby = false;
3640 }
3641 return bytesWritten;
3642 }
3643
3644 // startMelComputation_l() must be called with AudioFlinger::mutex() held
startMelComputation_l(const sp<audio_utils::MelProcessor> & processor)3645 void PlaybackThread::startMelComputation_l(
3646 const sp<audio_utils::MelProcessor>& processor)
3647 {
3648 auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get());
3649 if (outputSink != nullptr) {
3650 outputSink->startMelComputation(processor);
3651 }
3652 }
3653
3654 // stopMelComputation_l() must be called with AudioFlinger::mutex() held
stopMelComputation_l()3655 void PlaybackThread::stopMelComputation_l()
3656 {
3657 auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get());
3658 if (outputSink != nullptr) {
3659 outputSink->stopMelComputation();
3660 }
3661 }
3662
threadLoop_drain()3663 void PlaybackThread::threadLoop_drain()
3664 {
3665 bool supportsDrain = false;
3666 if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) {
3667 ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full");
3668 if (mUseAsyncWrite) {
3669 ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request");
3670 mDrainSequence |= 1;
3671 ALOG_ASSERT(mCallbackThread != 0);
3672 mCallbackThread->setDraining(mDrainSequence);
3673 }
3674 status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK);
3675 ALOGE_IF(result != OK, "Error when draining stream: %d", result);
3676 }
3677 }
3678
threadLoop_exit()3679 void PlaybackThread::threadLoop_exit()
3680 {
3681 {
3682 audio_utils::lock_guard _l(mutex());
3683 for (size_t i = 0; i < mTracks.size(); i++) {
3684 sp<IAfTrack> track = mTracks[i];
3685 track->invalidate();
3686 }
3687 // Clear ActiveTracks to update BatteryNotifier in case active tracks remain.
3688 // After we exit there are no more track changes sent to BatteryNotifier
3689 // because that requires an active threadLoop.
3690 // TODO: should we decActiveTrackCnt() of the cleared track effect chain?
3691 mActiveTracks.clear();
3692 }
3693 }
3694
3695 /*
3696 The derived values that are cached:
3697 - mSinkBufferSize from frame count * frame size
3698 - mActiveSleepTimeUs from activeSleepTimeUs()
3699 - mIdleSleepTimeUs from idleSleepTimeUs()
3700 - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least
3701 kDefaultStandbyTimeInNsecs when connected to an A2DP device.
3702 - maxPeriod from frame count and sample rate (MIXER only)
3703
3704 The parameters that affect these derived values are:
3705 - frame count
3706 - frame size
3707 - sample rate
3708 - device type: A2DP or not
3709 - device latency
3710 - format: PCM or not
3711 - active sleep time
3712 - idle sleep time
3713 */
3714
cacheParameters_l()3715 void PlaybackThread::cacheParameters_l()
3716 {
3717 mSinkBufferSize = mNormalFrameCount * mFrameSize;
3718 mActiveSleepTimeUs = activeSleepTimeUs();
3719 mIdleSleepTimeUs = idleSleepTimeUs();
3720
3721 mStandbyDelayNs = getStandbyTimeInNanos();
3722
3723 // make sure standby delay is not too short when connected to an A2DP sink to avoid
3724 // truncating audio when going to standby.
3725 if (!Intersection(outDeviceTypes_l(), getAudioDeviceOutAllA2dpSet()).empty()) {
3726 if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) {
3727 mStandbyDelayNs = kDefaultStandbyTimeInNsecs;
3728 }
3729 }
3730 }
3731
invalidateTracks_l(audio_stream_type_t streamType)3732 bool PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType)
3733 {
3734 ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu",
3735 this, streamType, mTracks.size());
3736 bool trackMatch = false;
3737 size_t size = mTracks.size();
3738 for (size_t i = 0; i < size; i++) {
3739 sp<IAfTrack> t = mTracks[i];
3740 if (t->streamType() == streamType && t->isExternalTrack()) {
3741 t->invalidate();
3742 trackMatch = true;
3743 }
3744 }
3745 return trackMatch;
3746 }
3747
invalidateTracks(audio_stream_type_t streamType)3748 void PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
3749 {
3750 audio_utils::lock_guard _l(mutex());
3751 invalidateTracks_l(streamType);
3752 }
3753
invalidateTracks(std::set<audio_port_handle_t> & portIds)3754 void PlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) {
3755 audio_utils::lock_guard _l(mutex());
3756 invalidateTracks_l(portIds);
3757 }
3758
invalidateTracks_l(std::set<audio_port_handle_t> & portIds)3759 bool PlaybackThread::invalidateTracks_l(std::set<audio_port_handle_t>& portIds) {
3760 bool trackMatch = false;
3761 const size_t size = mTracks.size();
3762 for (size_t i = 0; i < size; i++) {
3763 sp<IAfTrack> t = mTracks[i];
3764 if (t->isExternalTrack() && portIds.find(t->portId()) != portIds.end()) {
3765 t->invalidate();
3766 portIds.erase(t->portId());
3767 trackMatch = true;
3768 }
3769 if (portIds.empty()) {
3770 break;
3771 }
3772 }
3773 return trackMatch;
3774 }
3775
3776 // getTrackById_l must be called with holding thread lock
getTrackById_l(audio_port_handle_t trackPortId)3777 IAfTrack* PlaybackThread::getTrackById_l(
3778 audio_port_handle_t trackPortId) {
3779 for (size_t i = 0; i < mTracks.size(); i++) {
3780 if (mTracks[i]->portId() == trackPortId) {
3781 return mTracks[i].get();
3782 }
3783 }
3784 return nullptr;
3785 }
3786
3787 // getTracks_l must be called with holding thread lock
getTracks_l()3788 std::vector<sp<IAfTrack>> PlaybackThread::getTracks_l() {
3789 return std::vector(mTracks.begin(), mTracks.end());
3790 }
3791
addEffectChain_l(const sp<IAfEffectChain> & chain)3792 status_t PlaybackThread::addEffectChain_l(const sp<IAfEffectChain>& chain)
3793 {
3794 audio_session_t session = chain->sessionId();
3795 sp<EffectBufferHalInterface> halInBuffer, halOutBuffer;
3796 float *buffer = nullptr; // only used for non global sessions
3797
3798 if (mType == SPATIALIZER) {
3799 if (!audio_is_global_session(session)) {
3800 // player sessions on a spatializer output will use a dedicated input buffer and
3801 // will either output multi channel to mEffectBuffer if the track is spatilaized
3802 // or stereo to mPostSpatializerBuffer if not spatialized.
3803 uint32_t channelMask;
3804 bool isSessionSpatialized =
3805 (hasAudioSession_l(session) & ThreadBase::SPATIALIZED_SESSION) != 0;
3806 if (isSessionSpatialized) {
3807 channelMask = mMixerChannelMask;
3808 } else {
3809 channelMask = mChannelMask;
3810 }
3811 size_t numSamples = mNormalFrameCount
3812 * (audio_channel_count_from_out_mask(channelMask) + mHapticChannelCount);
3813 status_t result = mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer(
3814 numSamples * sizeof(float),
3815 &halInBuffer);
3816 if (result != OK) return result;
3817
3818 result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer(
3819 isSessionSpatialized ? mEffectBuffer : mPostSpatializerBuffer,
3820 isSessionSpatialized ? mEffectBufferSize : mPostSpatializerBufferSize,
3821 &halOutBuffer);
3822 if (result != OK) return result;
3823
3824 buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer;
3825
3826 ALOGV("addEffectChain_l() creating new input buffer %p session %d",
3827 buffer, session);
3828 } else {
3829 status_t result = INVALID_OPERATION;
3830 // Buffer configuration for global sessions on a SPATIALIZER thread:
3831 // - AUDIO_SESSION_OUTPUT_MIX session uses the mEffectBuffer as input and output buffer
3832 // - AUDIO_SESSION_OUTPUT_STAGE session uses the mEffectBuffer as input buffer and
3833 // mPostSpatializerBuffer as output buffer
3834 // - AUDIO_SESSION_DEVICE session uses the mPostSpatializerBuffer as input and output
3835 // buffer
3836 if (session == AUDIO_SESSION_OUTPUT_MIX || session == AUDIO_SESSION_OUTPUT_STAGE) {
3837 result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer(
3838 mEffectBuffer, mEffectBufferSize, &halInBuffer);
3839 if (result != OK) return result;
3840
3841 if (session == AUDIO_SESSION_OUTPUT_MIX) {
3842 halOutBuffer = halInBuffer;
3843 }
3844 }
3845
3846 if (session == AUDIO_SESSION_OUTPUT_STAGE || session == AUDIO_SESSION_DEVICE) {
3847 result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer(
3848 mPostSpatializerBuffer, mPostSpatializerBufferSize, &halOutBuffer);
3849 if (result != OK) return result;
3850
3851 if (session == AUDIO_SESSION_DEVICE) {
3852 halInBuffer = halOutBuffer;
3853 }
3854 }
3855 }
3856 } else {
3857 status_t result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer(
3858 mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer,
3859 mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize,
3860 &halInBuffer);
3861 if (result != OK) return result;
3862 halOutBuffer = halInBuffer;
3863 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
3864 if (!audio_is_global_session(session)) {
3865 buffer = halInBuffer ? reinterpret_cast<float*>(halInBuffer->externalData())
3866 : buffer;
3867 // Only one effect chain can be present in direct output thread and it uses
3868 // the sink buffer as input
3869 if (mType != DIRECT) {
3870 size_t numSamples = mNormalFrameCount
3871 * (audio_channel_count_from_out_mask(mMixerChannelMask)
3872 + mHapticChannelCount);
3873 const status_t allocateStatus =
3874 mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer(
3875 numSamples * sizeof(float),
3876 &halInBuffer);
3877 if (allocateStatus != OK) return allocateStatus;
3878
3879 buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer;
3880 ALOGV("addEffectChain_l() creating new input buffer %p session %d",
3881 buffer, session);
3882 }
3883 }
3884 }
3885
3886 if (!audio_is_global_session(session)) {
3887 // Attach all tracks with same session ID to this chain.
3888 for (size_t i = 0; i < mTracks.size(); ++i) {
3889 sp<IAfTrack> track = mTracks[i];
3890 if (session == track->sessionId()) {
3891 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p",
3892 track.get(), buffer);
3893 track->setMainBuffer(buffer);
3894 chain->incTrackCnt();
3895 }
3896 }
3897
3898 // indicate all active tracks in the chain
3899 for (const sp<IAfTrack>& track : mActiveTracks) {
3900 if (session == track->sessionId()) {
3901 ALOGV("addEffectChain_l() activating track %p on session %d",
3902 track.get(), session);
3903 chain->incActiveTrackCnt();
3904 }
3905 }
3906 }
3907
3908 chain->setThread(this);
3909 chain->setInBuffer(halInBuffer);
3910 chain->setOutBuffer(halOutBuffer);
3911 // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect
3912 // chains list in order to be processed last as it contains output device effects.
3913 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post
3914 // processing effects specific to an output stream before effects applied to all streams
3915 // routed to a given device.
3916 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
3917 // session AUDIO_SESSION_OUTPUT_STAGE to be processed
3918 // after track specific effects and before output stage.
3919 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
3920 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX.
3921 // Effect chain for other sessions are inserted at beginning of effect
3922 // chains list to be processed before output mix effects. Relative order between other
3923 // sessions is not important.
3924 static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 &&
3925 AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX &&
3926 AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE,
3927 "audio_session_t constants misdefined");
3928 size_t size = mEffectChains.size();
3929 size_t i = 0;
3930 for (i = 0; i < size; i++) {
3931 if (mEffectChains[i]->sessionId() < session) {
3932 break;
3933 }
3934 }
3935 mEffectChains.insertAt(chain, i);
3936 checkSuspendOnAddEffectChain_l(chain);
3937
3938 return NO_ERROR;
3939 }
3940
removeEffectChain_l(const sp<IAfEffectChain> & chain)3941 size_t PlaybackThread::removeEffectChain_l(const sp<IAfEffectChain>& chain)
3942 {
3943 audio_session_t session = chain->sessionId();
3944
3945 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
3946
3947 for (size_t i = 0; i < mEffectChains.size(); i++) {
3948 if (chain == mEffectChains[i]) {
3949 mEffectChains.removeAt(i);
3950 // detach all active tracks from the chain
3951 for (const sp<IAfTrack>& track : mActiveTracks) {
3952 if (session == track->sessionId()) {
3953 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
3954 chain.get(), session);
3955 chain->decActiveTrackCnt();
3956 }
3957 }
3958
3959 // detach all tracks with same session ID from this chain
3960 for (size_t j = 0; j < mTracks.size(); ++j) {
3961 sp<IAfTrack> track = mTracks[j];
3962 if (session == track->sessionId()) {
3963 track->setMainBuffer(reinterpret_cast<float*>(mSinkBuffer));
3964 chain->decTrackCnt();
3965 }
3966 }
3967 break;
3968 }
3969 }
3970 return mEffectChains.size();
3971 }
3972
attachAuxEffect(const sp<IAfTrack> & track,int EffectId)3973 status_t PlaybackThread::attachAuxEffect(
3974 const sp<IAfTrack>& track, int EffectId)
3975 {
3976 audio_utils::lock_guard _l(mutex());
3977 return attachAuxEffect_l(track, EffectId);
3978 }
3979
attachAuxEffect_l(const sp<IAfTrack> & track,int EffectId)3980 status_t PlaybackThread::attachAuxEffect_l(
3981 const sp<IAfTrack>& track, int EffectId)
3982 {
3983 status_t status = NO_ERROR;
3984
3985 if (EffectId == 0) {
3986 track->setAuxBuffer(0, NULL);
3987 } else {
3988 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
3989 sp<IAfEffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
3990 if (effect != 0) {
3991 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3992 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
3993 } else {
3994 status = INVALID_OPERATION;
3995 }
3996 } else {
3997 status = BAD_VALUE;
3998 }
3999 }
4000 return status;
4001 }
4002
detachAuxEffect_l(int effectId)4003 void PlaybackThread::detachAuxEffect_l(int effectId)
4004 {
4005 for (size_t i = 0; i < mTracks.size(); ++i) {
4006 sp<IAfTrack> track = mTracks[i];
4007 if (track->auxEffectId() == effectId) {
4008 attachAuxEffect_l(track, 0);
4009 }
4010 }
4011 }
4012
threadLoop()4013 bool PlaybackThread::threadLoop()
4014 NO_THREAD_SAFETY_ANALYSIS // manual locking of AudioFlinger
4015 {
4016 if (mType == SPATIALIZER) {
4017 const pid_t tid = getTid();
4018 if (tid == -1) { // odd: we are here, we must be a running thread.
4019 ALOGW("%s: Cannot update Spatializer mixer thread priority, no tid", __func__);
4020 } else {
4021 const int priorityBoost = requestSpatializerPriority(getpid(), tid);
4022 if (priorityBoost > 0) {
4023 stream()->setHalThreadPriority(priorityBoost);
4024 }
4025 }
4026 } else if (property_get_bool("ro.boot.container", false /* default_value */)) {
4027 // In ARC experiments (b/73091832), the latency under using CFS scheduler with any priority
4028 // is not enough for PlaybackThread to process audio data in time. We request the lowest
4029 // real-time priority, SCHED_FIFO=1, for PlaybackThread in ARC. ro.boot.container is true
4030 // only on ARC.
4031 const pid_t tid = getTid();
4032 if (tid == -1) {
4033 ALOGW("%s: Cannot update PlaybackThread priority for ARC, no tid", __func__);
4034 } else {
4035 const status_t status = requestPriority(getpid(),
4036 tid,
4037 kPriorityPlaybackThreadArc,
4038 false /* isForApp */,
4039 true /* asynchronous */);
4040 if (status != OK) {
4041 ALOGW("%s: Cannot update PlaybackThread priority for ARC, status %d", __func__,
4042 status);
4043 } else {
4044 stream()->setHalThreadPriority(kPriorityPlaybackThreadArc);
4045 }
4046 }
4047 }
4048
4049 Vector<sp<IAfTrack>> tracksToRemove;
4050
4051 mStandbyTimeNs = systemTime();
4052 int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0.
4053
4054 // MIXER
4055 nsecs_t lastWarning = 0;
4056
4057 // DUPLICATING
4058 // FIXME could this be made local to while loop?
4059 writeFrames = 0;
4060
4061 {
4062 audio_utils::lock_guard l(mutex());
4063
4064 cacheParameters_l();
4065 checkSilentMode_l();
4066 }
4067
4068 mSleepTimeUs = mIdleSleepTimeUs;
4069
4070 if (mType == MIXER || mType == SPATIALIZER) {
4071 sleepTimeShift = 0;
4072 }
4073
4074 CpuStats cpuStats;
4075 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
4076
4077 acquireWakeLock();
4078
4079 // Estimated time for next buffer to be written to hal. This is used only on
4080 // suspended mode (for now) to help schedule the wait time until next iteration.
4081 nsecs_t timeLoopNextNs = 0;
4082
4083 audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4084
4085 sendCheckOutputStageEffectsEvent();
4086
4087 // loopCount is used for statistics and diagnostics.
4088 for (int64_t loopCount = 0; !exitPending(); ++loopCount)
4089 {
4090 cpuStats.sample(myName);
4091
4092 Vector<sp<IAfEffectChain>> effectChains;
4093 audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE;
4094 bool isHapticSessionSpatialized = false;
4095 std::vector<sp<IAfTrack>> activeTracks;
4096
4097 // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency.
4098 //
4099 // Note: we access outDeviceTypes() outside of mutex().
4100 if (isMsdDevice() && outDeviceTypes_l().count(AUDIO_DEVICE_OUT_BUS) != 0) {
4101 // Here, we try for the AF lock, but do not block on it as the latency
4102 // is more informational.
4103 if (mAfThreadCallback->mutex().try_lock()) {
4104 std::vector<SoftwarePatch> swPatches;
4105 double latencyMs = 0.; // not required; initialized for clang-tidy
4106 status_t status = INVALID_OPERATION;
4107 audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4108 if (mAfThreadCallback->getPatchPanel()->getDownstreamSoftwarePatches(
4109 id(), &swPatches) == OK
4110 && swPatches.size() > 0) {
4111 status = swPatches[0].getLatencyMs_l(&latencyMs);
4112 downstreamPatchHandle = swPatches[0].getPatchHandle();
4113 }
4114 if (downstreamPatchHandle != lastDownstreamPatchHandle) {
4115 mDownstreamLatencyStatMs.reset();
4116 lastDownstreamPatchHandle = downstreamPatchHandle;
4117 }
4118 if (status == OK) {
4119 // verify downstream latency (we assume a max reasonable
4120 // latency of 5 seconds).
4121 const double minLatency = 0., maxLatency = 5000.;
4122 if (latencyMs >= minLatency && latencyMs <= maxLatency) {
4123 ALOGVV("new downstream latency %lf ms", latencyMs);
4124 } else {
4125 ALOGD("out of range downstream latency %lf ms", latencyMs);
4126 latencyMs = std::clamp(latencyMs, minLatency, maxLatency);
4127 }
4128 mDownstreamLatencyStatMs.add(latencyMs);
4129 }
4130 mAfThreadCallback->mutex().unlock();
4131 }
4132 } else {
4133 if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
4134 // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats.
4135 mDownstreamLatencyStatMs.reset();
4136 lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4137 }
4138 }
4139
4140 if (mCheckOutputStageEffects.exchange(false)) {
4141 checkOutputStageEffects();
4142 }
4143
4144 MetadataUpdate metadataUpdate;
4145 { // scope for mutex()
4146
4147 audio_utils::unique_lock _l(mutex());
4148
4149 processConfigEvents_l();
4150 if (mCheckOutputStageEffects.load()) {
4151 continue;
4152 }
4153
4154 collectTimestamps_l();
4155
4156 saveOutputTracks();
4157 if (mSignalPending) {
4158 // A signal was raised while we were unlocked
4159 mSignalPending = false;
4160 } else if (waitingAsyncCallback_l()) {
4161 if (exitPending()) {
4162 break;
4163 }
4164 bool released = false;
4165 if (!keepWakeLock()) {
4166 releaseWakeLock_l();
4167 released = true;
4168 }
4169
4170 const int64_t waitNs = computeWaitTimeNs_l();
4171 ALOGV("wait async completion (wait time: %lld)", (long long)waitNs);
4172 std::cv_status cvstatus =
4173 mWaitWorkCV.wait_for(_l, std::chrono::nanoseconds(waitNs));
4174 if (cvstatus == std::cv_status::timeout) {
4175 mSignalPending = true; // if timeout recheck everything
4176 }
4177 ALOGV("async completion/wake");
4178 if (released) {
4179 acquireWakeLock_l();
4180 }
4181 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
4182 mSleepTimeUs = 0;
4183
4184 continue;
4185 }
4186 if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) ||
4187 isSuspended()) {
4188 // put audio hardware into standby after short delay
4189 if (shouldStandby_l()) {
4190
4191 threadLoop_standby();
4192
4193 // This is where we go into standby
4194 if (!mStandby) {
4195 LOG_AUDIO_STATE();
4196 mThreadMetrics.logEndInterval();
4197 mThreadSnapshot.onEnd();
4198 setStandby_l();
4199 }
4200 sendStatistics(false /* force */);
4201 }
4202
4203 if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) {
4204 // we're about to wait, flush the binder command buffer
4205 IPCThreadState::self()->flushCommands();
4206
4207 clearOutputTracks();
4208
4209 if (exitPending()) {
4210 break;
4211 }
4212
4213 releaseWakeLock_l();
4214 // wait until we have something to do...
4215 ALOGV("%s going to sleep", myName.c_str());
4216 mWaitWorkCV.wait(_l);
4217 ALOGV("%s waking up", myName.c_str());
4218 acquireWakeLock_l();
4219
4220 mMixerStatus = MIXER_IDLE;
4221 mMixerStatusIgnoringFastTracks = MIXER_IDLE;
4222 mBytesWritten = 0;
4223 mBytesRemaining = 0;
4224 checkSilentMode_l();
4225
4226 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
4227 mSleepTimeUs = mIdleSleepTimeUs;
4228 if (mType == MIXER || mType == SPATIALIZER) {
4229 sleepTimeShift = 0;
4230 }
4231
4232 continue;
4233 }
4234 }
4235 // mMixerStatusIgnoringFastTracks is also updated internally
4236 mMixerStatus = prepareTracks_l(&tracksToRemove);
4237
4238 mActiveTracks.updatePowerState_l(this);
4239
4240 metadataUpdate = updateMetadata_l();
4241
4242 // Acquire a local copy of active tracks with lock (release w/o lock).
4243 //
4244 // Control methods on the track acquire the ThreadBase lock (e.g. start()
4245 // stop(), pause(), etc.), but the threadLoop is entitled to call audio
4246 // data / buffer methods on tracks from activeTracks without the ThreadBase lock.
4247 activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end());
4248
4249 setHalLatencyMode_l();
4250
4251 // updateTeePatches_l will acquire the ThreadBase_Mutex of other threads,
4252 // so this is done before we lock our effect chains.
4253 for (const auto& track : mActiveTracks) {
4254 track->updateTeePatches_l();
4255 }
4256
4257 // check if traces have been enabled.
4258 bool atraceEnabled = ATRACE_ENABLED();
4259 if (atraceEnabled != mAtraceEnabled) [[unlikely]] {
4260 mAtraceEnabled = atraceEnabled;
4261 if (atraceEnabled) {
4262 const auto devices = patchSinksToString(&mPatch);
4263 for (const auto& track : activeTracks) {
4264 track->logRefreshInterval(devices);
4265 }
4266 }
4267 }
4268 // signal actual start of output stream when the render position reported by
4269 // the kernel starts moving.
4270 if (!mHalStarted && ((isSuspended() && (mBytesWritten != 0)) || (!mStandby
4271 && (mKernelPositionOnStandby
4272 != mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL])))) {
4273 mHalStarted = true;
4274 mWaitHalStartCV.notify_all();
4275 }
4276
4277 // prevent any changes in effect chain list and in each effect chain
4278 // during mixing and effect process as the audio buffers could be deleted
4279 // or modified if an effect is created or deleted
4280 lockEffectChains_l(effectChains);
4281
4282 // Determine which session to pick up haptic data.
4283 // This must be done under the same lock as prepareTracks_l().
4284 // The haptic data from the effect is at a higher priority than the one from track.
4285 // TODO: Write haptic data directly to sink buffer when mixing.
4286 if (mHapticChannelCount > 0) {
4287 for (const auto& track : mActiveTracks) {
4288 sp<IAfEffectChain> effectChain = getEffectChain_l(track->sessionId());
4289 if (effectChain != nullptr
4290 && effectChain->containsHapticGeneratingEffect_l()) {
4291 activeHapticSessionId = track->sessionId();
4292 isHapticSessionSpatialized =
4293 mType == SPATIALIZER && track->isSpatialized();
4294 break;
4295 }
4296 if (activeHapticSessionId == AUDIO_SESSION_NONE
4297 && track->getHapticPlaybackEnabled()) {
4298 activeHapticSessionId = track->sessionId();
4299 isHapticSessionSpatialized =
4300 mType == SPATIALIZER && track->isSpatialized();
4301 }
4302 }
4303 }
4304 } // mutex() scope ends
4305
4306 if (mBytesRemaining == 0) {
4307 mCurrentWriteLength = 0;
4308 if (mMixerStatus == MIXER_TRACKS_READY) {
4309 // threadLoop_mix() sets mCurrentWriteLength
4310 threadLoop_mix();
4311 } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
4312 && (mMixerStatus != MIXER_DRAIN_ALL)) {
4313 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data
4314 // must be written to HAL
4315 threadLoop_sleepTime();
4316 if (mSleepTimeUs == 0) {
4317 mCurrentWriteLength = mSinkBufferSize;
4318
4319 // Tally underrun frames as we are inserting 0s here.
4320 for (const auto& track : activeTracks) {
4321 if (track->fillingStatus() == IAfTrack::FS_ACTIVE
4322 && !track->isStopped()
4323 && !track->isPaused()
4324 && !track->isTerminated()) {
4325 ALOGV("%s: track(%d) %s underrun due to thread sleep of %zu frames",
4326 __func__, track->id(), track->getTrackStateAsString(),
4327 mNormalFrameCount);
4328 track->audioTrackServerProxy()->tallyUnderrunFrames(
4329 mNormalFrameCount);
4330 }
4331 }
4332 }
4333 }
4334 // Either threadLoop_mix() or threadLoop_sleepTime() should have set
4335 // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0.
4336 // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
4337 // or mSinkBuffer (if there are no effects and there is no data already copied to
4338 // mSinkBuffer).
4339 //
4340 // This is done pre-effects computation; if effects change to
4341 // support higher precision, this needs to move.
4342 //
4343 // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
4344 // TODO use mSleepTimeUs == 0 as an additional condition.
4345 uint32_t mixerChannelCount = mEffectBufferValid ?
4346 audio_channel_count_from_out_mask(mMixerChannelMask) : mChannelCount;
4347 if (mMixerBufferValid && (mEffectBufferValid || !mHasDataCopiedToSinkBuffer)) {
4348 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
4349 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
4350
4351 // Apply mono blending and balancing if the effect buffer is not valid. Otherwise,
4352 // do these processes after effects are applied.
4353 if (!mEffectBufferValid) {
4354 // mono blend occurs for mixer threads only (not direct or offloaded)
4355 // and is handled here if we're going directly to the sink.
4356 if (requireMonoBlend()) {
4357 mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount,
4358 mNormalFrameCount, true /*limit*/);
4359 }
4360
4361 if (!hasFastMixer()) {
4362 // Balance must take effect after mono conversion.
4363 // We do it here if there is no FastMixer.
4364 // mBalance detects zero balance within the class for speed
4365 // (not needed here).
4366 mBalance.setBalance(mMasterBalance.load());
4367 mBalance.process((float *)mMixerBuffer, mNormalFrameCount);
4368 }
4369 }
4370
4371 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
4372 mNormalFrameCount * (mixerChannelCount + mHapticChannelCount));
4373
4374 // If we're going directly to the sink and there are haptic channels,
4375 // we should adjust channels as the sample data is partially interleaved
4376 // in this case.
4377 if (!mEffectBufferValid && mHapticChannelCount > 0) {
4378 adjust_channels_non_destructive(buffer, mChannelCount, buffer,
4379 mChannelCount + mHapticChannelCount,
4380 audio_bytes_per_sample(format),
4381 audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount);
4382 }
4383 }
4384
4385 mBytesRemaining = mCurrentWriteLength;
4386 if (isSuspended()) {
4387 // Simulate write to HAL when suspended (e.g. BT SCO phone call).
4388 mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer.
4389 const size_t framesRemaining = mBytesRemaining / mFrameSize;
4390 mBytesWritten += mBytesRemaining;
4391 mFramesWritten += framesRemaining;
4392 mSuspendedFrames += framesRemaining; // to adjust kernel HAL position
4393 mBytesRemaining = 0;
4394 }
4395
4396 // only process effects if we're going to write
4397 if (mSleepTimeUs == 0 && mType != OFFLOAD) {
4398 for (size_t i = 0; i < effectChains.size(); i ++) {
4399 effectChains[i]->process_l();
4400 // TODO: Write haptic data directly to sink buffer when mixing.
4401 if (activeHapticSessionId != AUDIO_SESSION_NONE
4402 && activeHapticSessionId == effectChains[i]->sessionId()) {
4403 // Haptic data is active in this case, copy it directly from
4404 // in buffer to out buffer.
4405 uint32_t hapticSessionChannelCount = mEffectBufferValid ?
4406 audio_channel_count_from_out_mask(mMixerChannelMask) :
4407 mChannelCount;
4408 if (mType == SPATIALIZER && !isHapticSessionSpatialized) {
4409 hapticSessionChannelCount = mChannelCount;
4410 }
4411
4412 const size_t audioBufferSize = mNormalFrameCount
4413 * audio_bytes_per_frame(hapticSessionChannelCount,
4414 AUDIO_FORMAT_PCM_FLOAT);
4415 memcpy_by_audio_format(
4416 (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize,
4417 AUDIO_FORMAT_PCM_FLOAT,
4418 (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize,
4419 AUDIO_FORMAT_PCM_FLOAT, mNormalFrameCount * mHapticChannelCount);
4420 }
4421 }
4422 }
4423 }
4424 // Process effect chains for offloaded thread even if no audio
4425 // was read from audio track: process only updates effect state
4426 // and thus does have to be synchronized with audio writes but may have
4427 // to be called while waiting for async write callback
4428 if (mType == OFFLOAD) {
4429 for (size_t i = 0; i < effectChains.size(); i ++) {
4430 effectChains[i]->process_l();
4431 }
4432 }
4433
4434 // Only if the Effects buffer is enabled and there is data in the
4435 // Effects buffer (buffer valid), we need to
4436 // copy into the sink buffer.
4437 // TODO use mSleepTimeUs == 0 as an additional condition.
4438 if (mEffectBufferValid && !mHasDataCopiedToSinkBuffer) {
4439 //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
4440 void *effectBuffer = (mType == SPATIALIZER) ? mPostSpatializerBuffer : mEffectBuffer;
4441 if (requireMonoBlend()) {
4442 mono_blend(effectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount,
4443 true /*limit*/);
4444 }
4445
4446 if (!hasFastMixer()) {
4447 // Balance must take effect after mono conversion.
4448 // We do it here if there is no FastMixer.
4449 // mBalance detects zero balance within the class for speed (not needed here).
4450 mBalance.setBalance(mMasterBalance.load());
4451 mBalance.process((float *)effectBuffer, mNormalFrameCount);
4452 }
4453
4454 // for SPATIALIZER thread, Move haptics channels from mEffectBuffer to
4455 // mPostSpatializerBuffer if the haptics track is spatialized.
4456 // Otherwise, the haptics channels are already in mPostSpatializerBuffer.
4457 // For other thread types, the haptics channels are already in mEffectBuffer.
4458 if (mType == SPATIALIZER && isHapticSessionSpatialized) {
4459 const size_t srcBufferSize = mNormalFrameCount *
4460 audio_bytes_per_frame(audio_channel_count_from_out_mask(mMixerChannelMask),
4461 mEffectBufferFormat);
4462 const size_t dstBufferSize = mNormalFrameCount
4463 * audio_bytes_per_frame(mChannelCount, mEffectBufferFormat);
4464
4465 memcpy_by_audio_format((uint8_t*)mPostSpatializerBuffer + dstBufferSize,
4466 mEffectBufferFormat,
4467 (uint8_t*)mEffectBuffer + srcBufferSize,
4468 mEffectBufferFormat,
4469 mNormalFrameCount * mHapticChannelCount);
4470 }
4471 const size_t framesToCopy = mNormalFrameCount * (mChannelCount + mHapticChannelCount);
4472 if (mFormat == AUDIO_FORMAT_PCM_FLOAT &&
4473 mEffectBufferFormat == AUDIO_FORMAT_PCM_FLOAT) {
4474 // Clamp PCM float values more than this distance from 0 to insulate
4475 // a HAL which doesn't handle NaN correctly.
4476 static constexpr float HAL_FLOAT_SAMPLE_LIMIT = 2.0f;
4477 memcpy_to_float_from_float_with_clamping(static_cast<float*>(mSinkBuffer),
4478 static_cast<const float*>(effectBuffer),
4479 framesToCopy, HAL_FLOAT_SAMPLE_LIMIT /* absMax */);
4480 } else {
4481 memcpy_by_audio_format(mSinkBuffer, mFormat,
4482 effectBuffer, mEffectBufferFormat, framesToCopy);
4483 }
4484 // The sample data is partially interleaved when haptic channels exist,
4485 // we need to adjust channels here.
4486 if (mHapticChannelCount > 0) {
4487 adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer,
4488 mChannelCount + mHapticChannelCount,
4489 audio_bytes_per_sample(mFormat),
4490 audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount);
4491 }
4492 }
4493
4494 // enable changes in effect chain
4495 unlockEffectChains(effectChains);
4496
4497 if (!metadataUpdate.playbackMetadataUpdate.empty()) {
4498 mAfThreadCallback->getMelReporter()->updateMetadataForCsd(id(),
4499 metadataUpdate.playbackMetadataUpdate);
4500 }
4501
4502 if (!waitingAsyncCallback()) {
4503 // mSleepTimeUs == 0 means we must write to audio hardware
4504 if (mSleepTimeUs == 0) {
4505 ssize_t ret = 0;
4506 // writePeriodNs is updated >= 0 when ret > 0.
4507 int64_t writePeriodNs = -1;
4508 if (mBytesRemaining) {
4509 // FIXME rewrite to reduce number of system calls
4510 const int64_t lastIoBeginNs = systemTime();
4511 ret = threadLoop_write();
4512 const int64_t lastIoEndNs = systemTime();
4513 if (ret < 0) {
4514 mBytesRemaining = 0;
4515 } else if (ret > 0) {
4516 mBytesWritten += ret;
4517 mBytesRemaining -= ret;
4518 const int64_t frames = ret / mFrameSize;
4519 mFramesWritten += frames;
4520
4521 writePeriodNs = lastIoEndNs - mLastIoEndNs;
4522 // process information relating to write time.
4523 if (audio_has_proportional_frames(mFormat)) {
4524 // we are in a continuous mixing cycle
4525 if (mMixerStatus == MIXER_TRACKS_READY &&
4526 loopCount == lastLoopCountWritten + 1) {
4527
4528 const double jitterMs =
4529 TimestampVerifier<int64_t, int64_t>::computeJitterMs(
4530 {frames, writePeriodNs},
4531 {0, 0} /* lastTimestamp */, mSampleRate);
4532 const double processMs =
4533 (lastIoBeginNs - mLastIoEndNs) * 1e-6;
4534
4535 audio_utils::lock_guard _l(mutex());
4536 mIoJitterMs.add(jitterMs);
4537 mProcessTimeMs.add(processMs);
4538
4539 if (mPipeSink.get() != nullptr) {
4540 // Using the Monopipe availableToWrite, we estimate the current
4541 // buffer size.
4542 MonoPipe* monoPipe = static_cast<MonoPipe*>(mPipeSink.get());
4543 const ssize_t
4544 availableToWrite = mPipeSink->availableToWrite();
4545 const size_t pipeFrames = monoPipe->maxFrames();
4546 const size_t
4547 remainingFrames = pipeFrames - max(availableToWrite, 0);
4548 mMonopipePipeDepthStats.add(remainingFrames);
4549 }
4550 }
4551
4552 // write blocked detection
4553 const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs;
4554 if ((mType == MIXER || mType == SPATIALIZER)
4555 && deltaWriteNs > maxPeriod) {
4556 mNumDelayedWrites++;
4557 if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) {
4558 ATRACE_NAME("underrun");
4559 ALOGW("write blocked for %lld msecs, "
4560 "%d delayed writes, thread %d",
4561 (long long)deltaWriteNs / NANOS_PER_MILLISECOND,
4562 mNumDelayedWrites, mId);
4563 lastWarning = lastIoEndNs;
4564 }
4565 }
4566 }
4567 // update timing info.
4568 mLastIoBeginNs = lastIoBeginNs;
4569 mLastIoEndNs = lastIoEndNs;
4570 lastLoopCountWritten = loopCount;
4571 }
4572 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
4573 (mMixerStatus == MIXER_DRAIN_ALL)) {
4574 threadLoop_drain();
4575 }
4576 if ((mType == MIXER || mType == SPATIALIZER) && !mStandby) {
4577
4578 if (mThreadThrottle
4579 && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks)
4580 && writePeriodNs > 0) { // we have write period info
4581 // Limit MixerThread data processing to no more than twice the
4582 // expected processing rate.
4583 //
4584 // This helps prevent underruns with NuPlayer and other applications
4585 // which may set up buffers that are close to the minimum size, or use
4586 // deep buffers, and rely on a double-buffering sleep strategy to fill.
4587 //
4588 // The throttle smooths out sudden large data drains from the device,
4589 // e.g. when it comes out of standby, which often causes problems with
4590 // (1) mixer threads without a fast mixer (which has its own warm-up)
4591 // (2) minimum buffer sized tracks (even if the track is full,
4592 // the app won't fill fast enough to handle the sudden draw).
4593 //
4594 // Total time spent in last processing cycle equals time spent in
4595 // 1. threadLoop_write, as well as time spent in
4596 // 2. threadLoop_mix (significant for heavy mixing, especially
4597 // on low tier processors)
4598
4599 // it's OK if deltaMs is an overestimate.
4600
4601 const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND;
4602
4603 const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs;
4604 if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) {
4605 mThreadMetrics.logThrottleMs((double)throttleMs);
4606
4607 usleep(throttleMs * 1000);
4608 // notify of throttle start on verbose log
4609 ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs,
4610 "mixer(%p) throttle begin:"
4611 " ret(%zd) deltaMs(%d) requires sleep %d ms",
4612 this, ret, deltaMs, throttleMs);
4613 mThreadThrottleTimeMs += throttleMs;
4614 // Throttle must be attributed to the previous mixer loop's write time
4615 // to allow back-to-back throttling.
4616 // This also ensures proper timing statistics.
4617 mLastIoEndNs = systemTime(); // we fetch the write end time again.
4618 } else {
4619 uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
4620 if (diff > 0) {
4621 // notify of throttle end on debug log
4622 // but prevent spamming for bluetooth
4623 ALOGD_IF(!isSingleDeviceType(
4624 outDeviceTypes_l(), audio_is_a2dp_out_device) &&
4625 !isSingleDeviceType(
4626 outDeviceTypes_l(),
4627 audio_is_hearing_aid_out_device),
4628 "mixer(%p) throttle end: throttle time(%u)", this, diff);
4629 mThreadThrottleEndMs = mThreadThrottleTimeMs;
4630 }
4631 }
4632 }
4633 }
4634
4635 } else {
4636 ATRACE_BEGIN("sleep");
4637 audio_utils::unique_lock _l(mutex());
4638 // suspended requires accurate metering of sleep time.
4639 if (isSuspended()) {
4640 // advance by expected sleepTime
4641 timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs);
4642 const nsecs_t nowNs = systemTime();
4643
4644 // compute expected next time vs current time.
4645 // (negative deltas are treated as delays).
4646 nsecs_t deltaNs = timeLoopNextNs - nowNs;
4647 if (deltaNs < -kMaxNextBufferDelayNs) {
4648 // Delays longer than the max allowed trigger a reset.
4649 ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs);
4650 deltaNs = microseconds((nsecs_t)mSleepTimeUs);
4651 timeLoopNextNs = nowNs + deltaNs;
4652 } else if (deltaNs < 0) {
4653 // Delays within the max delay allowed: zero the delta/sleepTime
4654 // to help the system catch up in the next iteration(s)
4655 ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs);
4656 deltaNs = 0;
4657 }
4658 // update sleep time (which is >= 0)
4659 mSleepTimeUs = deltaNs / 1000;
4660 }
4661 if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) {
4662 mWaitWorkCV.wait_for(_l, std::chrono::microseconds(mSleepTimeUs));
4663 }
4664 ATRACE_END();
4665 }
4666 }
4667
4668 // Finally let go of removed track(s), without the lock held
4669 // since we can't guarantee the destructors won't acquire that
4670 // same lock. This will also mutate and push a new fast mixer state.
4671 threadLoop_removeTracks(tracksToRemove);
4672 tracksToRemove.clear();
4673
4674 // FIXME I don't understand the need for this here;
4675 // it was in the original code but maybe the
4676 // assignment in saveOutputTracks() makes this unnecessary?
4677 clearOutputTracks();
4678
4679 // Effect chains will be actually deleted here if they were removed from
4680 // mEffectChains list during mixing or effects processing
4681 effectChains.clear();
4682
4683 // FIXME Note that the above .clear() is no longer necessary since effectChains
4684 // is now local to this block, but will keep it for now (at least until merge done).
4685
4686 mThreadloopExecutor.process();
4687 }
4688 mThreadloopExecutor.process(); // process any remaining deferred actions.
4689 // deferred actions after this point are ignored.
4690
4691 threadLoop_exit();
4692
4693 if (!mStandby) {
4694 threadLoop_standby();
4695 setStandby();
4696 }
4697
4698 releaseWakeLock();
4699
4700 ALOGV("Thread %p type %d exiting", this, mType);
4701 return false;
4702 }
4703
collectTimestamps_l()4704 void PlaybackThread::collectTimestamps_l()
4705 {
4706 if (mStandby) {
4707 mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush());
4708 return;
4709 } else if (mHwPaused) {
4710 mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS);
4711 return;
4712 }
4713
4714 // Gather the framesReleased counters for all active tracks,
4715 // and associate with the sink frames written out. We need
4716 // this to convert the sink timestamp to the track timestamp.
4717 bool kernelLocationUpdate = false;
4718 ExtendedTimestamp timestamp; // use private copy to fetch
4719
4720 // Always query HAL timestamp and update timestamp verifier. In standby or pause,
4721 // HAL may be draining some small duration buffered data for fade out.
4722 if (threadloop_getHalTimestamp_l(×tamp) == OK) {
4723 mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
4724 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
4725 mSampleRate);
4726
4727 if (isTimestampCorrectionEnabled_l()) {
4728 ALOGVV("TS_BEFORE: %d %lld %lld", id(),
4729 (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
4730 (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
4731 auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
4732 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
4733 = correctedTimestamp.mFrames;
4734 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
4735 = correctedTimestamp.mTimeNs;
4736 ALOGVV("TS_AFTER: %d %lld %lld", id(),
4737 (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
4738 (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
4739
4740 // Note: Downstream latency only added if timestamp correction enabled.
4741 if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info.
4742 const int64_t newPosition =
4743 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
4744 - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
4745 // prevent retrograde
4746 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max(
4747 newPosition,
4748 (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
4749 - mSuspendedFrames));
4750 }
4751 }
4752
4753 // We always fetch the timestamp here because often the downstream
4754 // sink will block while writing.
4755
4756 // We keep track of the last valid kernel position in case we are in underrun
4757 // and the normal mixer period is the same as the fast mixer period, or there
4758 // is some error from the HAL.
4759 if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
4760 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
4761 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
4762 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
4763 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
4764
4765 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
4766 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER];
4767 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
4768 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER];
4769 }
4770
4771 if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
4772 kernelLocationUpdate = true;
4773 } else {
4774 ALOGVV("getTimestamp error - no valid kernel position");
4775 }
4776
4777 // copy over kernel info
4778 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
4779 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
4780 + mSuspendedFrames; // add frames discarded when suspended
4781 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
4782 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
4783 } else {
4784 mTimestampVerifier.error();
4785 }
4786
4787 // mFramesWritten for non-offloaded tracks are contiguous
4788 // even after standby() is called. This is useful for the track frame
4789 // to sink frame mapping.
4790 bool serverLocationUpdate = false;
4791 if (mFramesWritten != mLastFramesWritten) {
4792 serverLocationUpdate = true;
4793 mLastFramesWritten = mFramesWritten;
4794 }
4795 // Only update timestamps if there is a meaningful change.
4796 // Either the kernel timestamp must be valid or we have written something.
4797 if (kernelLocationUpdate || serverLocationUpdate) {
4798 if (serverLocationUpdate) {
4799 // use the time before we called the HAL write - it is a bit more accurate
4800 // to when the server last read data than the current time here.
4801 //
4802 // If we haven't written anything, mLastIoBeginNs will be -1
4803 // and we use systemTime().
4804 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten;
4805 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1
4806 ? systemTime() : (int64_t)mLastIoBeginNs;
4807 }
4808
4809 for (const sp<IAfTrack>& t : mActiveTracks) {
4810 if (!t->isFastTrack()) {
4811 t->updateTrackFrameInfo(
4812 t->audioTrackServerProxy()->framesReleased(),
4813 mFramesWritten,
4814 mSampleRate,
4815 mTimestamp);
4816 }
4817 }
4818 }
4819
4820 if (audio_has_proportional_frames(mFormat)) {
4821 const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate);
4822 if (latencyMs != 0.) { // note 0. means timestamp is empty.
4823 mLatencyMs.add(latencyMs);
4824 }
4825 }
4826 #if 0
4827 // logFormat example
4828 if (z % 100 == 0) {
4829 timespec ts;
4830 clock_gettime(CLOCK_MONOTONIC, &ts);
4831 LOGT("This is an integer %d, this is a float %f, this is my "
4832 "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts);
4833 LOGT("A deceptive null-terminated string %\0");
4834 }
4835 ++z;
4836 #endif
4837 }
4838
4839 // removeTracks_l() must be called with ThreadBase::mutex() held
removeTracks_l(const Vector<sp<IAfTrack>> & tracksToRemove)4840 void PlaybackThread::removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove)
4841 NO_THREAD_SAFETY_ANALYSIS // release and re-acquire mutex()
4842 {
4843 if (tracksToRemove.empty()) return;
4844
4845 // Block all incoming TrackHandle requests until we are finished with the release.
4846 setThreadBusy_l(true);
4847
4848 for (const auto& track : tracksToRemove) {
4849 ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId());
4850 sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId());
4851 if (chain != 0) {
4852 ALOGV("%s(%d): stopping track on chain %p for session Id: %d",
4853 __func__, track->id(), chain.get(), track->sessionId());
4854 chain->decActiveTrackCnt();
4855 }
4856
4857 // If an external client track, inform APM we're no longer active, and remove if needed.
4858 // Since the track is active, we do it here instead of TrackBase::destroy().
4859 if (track->isExternalTrack()) {
4860 mutex().unlock();
4861 AudioSystem::stopOutput(track->portId());
4862 if (track->isTerminated()) {
4863 AudioSystem::releaseOutput(track->portId());
4864 }
4865 mutex().lock();
4866 }
4867 if (mHapticChannelCount > 0 &&
4868 ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
4869 || (chain != nullptr && chain->containsHapticGeneratingEffect()))) {
4870 mutex().unlock();
4871 // Unlock due to VibratorService will lock for this call and will
4872 // call Tracks.mute/unmute which also require thread's lock.
4873 afutils::onExternalVibrationStop(track->getExternalVibration());
4874 mutex().lock();
4875
4876 // When the track is stop, set the haptic intensity as MUTE
4877 // for the HapticGenerator effect.
4878 if (chain != nullptr) {
4879 chain->setHapticScale_l(track->id(), os::HapticScale::mute());
4880 }
4881 }
4882
4883 // Under lock, the track is removed from the active tracks list.
4884 //
4885 // Once the track is no longer active, the TrackHandle may directly
4886 // modify it as the threadLoop() is no longer responsible for its maintenance.
4887 // Do not modify the track from threadLoop after the mutex is unlocked
4888 // if it is not active.
4889 mActiveTracks.remove(track);
4890
4891 if (track->isTerminated()) {
4892 // remove from our tracks vector
4893 removeTrack_l(track);
4894 }
4895 }
4896
4897 // Allow incoming TrackHandle requests. We still hold the mutex,
4898 // so pending TrackHandle requests will occur after we unlock it.
4899 setThreadBusy_l(false);
4900 }
4901
getTimestamp_l(AudioTimestamp & timestamp)4902 status_t PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp)
4903 {
4904 if (mNormalSink != 0) {
4905 ExtendedTimestamp ets;
4906 status_t status = mNormalSink->getTimestamp(ets);
4907 if (status == NO_ERROR) {
4908 status = ets.getBestTimestamp(×tamp);
4909 }
4910 return status;
4911 }
4912 if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) {
4913 collectTimestamps_l();
4914 if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] <= 0) {
4915 return INVALID_OPERATION;
4916 }
4917 timestamp.mPosition = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
4918 const int64_t timeNs = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
4919 timestamp.mTime.tv_sec = timeNs / NANOS_PER_SECOND;
4920 timestamp.mTime.tv_nsec = timeNs - (timestamp.mTime.tv_sec * NANOS_PER_SECOND);
4921 return NO_ERROR;
4922 }
4923 return INVALID_OPERATION;
4924 }
4925
4926 // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is
4927 // still applied by the mixer.
4928 // All tracks attached to a mixer with flag VOIP_RX are tied to the same
4929 // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even
4930 // if more than one track are active
handleVoipVolume_l(float * volume)4931 status_t PlaybackThread::handleVoipVolume_l(float* volume)
4932 {
4933 status_t result = NO_ERROR;
4934 if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) {
4935 if (*volume != mLeftVolFloat) {
4936 result = mOutput->stream->setVolume(*volume, *volume);
4937 // HAL can return INVALID_OPERATION if operation is not supported.
4938 ALOGE_IF(result != OK && result != INVALID_OPERATION,
4939 "Error when setting output stream volume: %d", result);
4940 if (result == NO_ERROR) {
4941 mLeftVolFloat = *volume;
4942 }
4943 }
4944 // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we
4945 // remove stream volume contribution from software volume.
4946 if (mLeftVolFloat == *volume) {
4947 *volume = 1.0f;
4948 }
4949 }
4950 return result;
4951 }
4952
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)4953 status_t MixerThread::createAudioPatch_l(const struct audio_patch* patch,
4954 audio_patch_handle_t *handle)
4955 {
4956 status_t status;
4957 if (property_get_bool("af.patch_park", false /* default_value */)) {
4958 // Park FastMixer to avoid potential DOS issues with writing to the HAL
4959 // or if HAL does not properly lock against access.
4960 AutoPark<FastMixer> park(mFastMixer);
4961 status = PlaybackThread::createAudioPatch_l(patch, handle);
4962 } else {
4963 status = PlaybackThread::createAudioPatch_l(patch, handle);
4964 }
4965
4966 updateHalSupportedLatencyModes_l();
4967 return status;
4968 }
4969
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)4970 status_t PlaybackThread::createAudioPatch_l(const struct audio_patch *patch,
4971 audio_patch_handle_t *handle)
4972 {
4973 status_t status = NO_ERROR;
4974
4975 // store new device and send to effects
4976 audio_devices_t type = AUDIO_DEVICE_NONE;
4977 AudioDeviceTypeAddrVector deviceTypeAddrs;
4978 for (unsigned int i = 0; i < patch->num_sinks; i++) {
4979 LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1
4980 && !mOutput->audioHwDev->supportsAudioPatches(),
4981 "Enumerated device type(%#x) must not be used "
4982 "as it does not support audio patches",
4983 patch->sinks[i].ext.device.type);
4984 type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type);
4985 deviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type,
4986 patch->sinks[i].ext.device.address);
4987 }
4988
4989 audio_port_handle_t sinkPortId = patch->sinks[0].id;
4990 #ifdef ADD_BATTERY_DATA
4991 // when changing the audio output device, call addBatteryData to notify
4992 // the change
4993 if (outDeviceTypes() != deviceTypes) {
4994 uint32_t params = 0;
4995 // check whether speaker is on
4996 if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) {
4997 params |= IMediaPlayerService::kBatteryDataSpeakerOn;
4998 }
4999
5000 // check if any other device (except speaker) is on
5001 if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) {
5002 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
5003 }
5004
5005 if (params != 0) {
5006 addBatteryData(params);
5007 }
5008 }
5009 #endif
5010
5011 for (size_t i = 0; i < mEffectChains.size(); i++) {
5012 mEffectChains[i]->setDevices_l(deviceTypeAddrs);
5013 }
5014
5015 // mPatch.num_sinks is not set when the thread is created so that
5016 // the first patch creation triggers an ioConfigChanged callback
5017 bool configChanged = (mPatch.num_sinks == 0) ||
5018 (mPatch.sinks[0].id != sinkPortId);
5019 mPatch = *patch;
5020 mOutDeviceTypeAddrs = deviceTypeAddrs;
5021 checkSilentMode_l();
5022
5023 if (mOutput->audioHwDev->supportsAudioPatches()) {
5024 sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
5025 status = hwDevice->createAudioPatch(patch->num_sources,
5026 patch->sources,
5027 patch->num_sinks,
5028 patch->sinks,
5029 handle);
5030 } else {
5031 status = mOutput->stream->legacyCreateAudioPatch(patch->sinks[0], std::nullopt, type);
5032 *handle = AUDIO_PATCH_HANDLE_NONE;
5033 }
5034 const std::string patchSinksAsString = patchSinksToString(patch);
5035
5036 mThreadMetrics.logEndInterval();
5037 mThreadMetrics.logCreatePatch(/* inDevices */ {}, patchSinksAsString);
5038 mThreadMetrics.logBeginInterval();
5039 // also dispatch to active AudioTracks for MediaMetrics
5040 for (const auto &track : mActiveTracks) {
5041 track->logEndInterval();
5042 track->logBeginInterval(patchSinksAsString);
5043 }
5044
5045 if (configChanged) {
5046 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
5047 }
5048 // Force metadata update after a route change
5049 mActiveTracks.setHasChanged();
5050
5051 return status;
5052 }
5053
releaseAudioPatch_l(const audio_patch_handle_t handle)5054 status_t MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
5055 {
5056 status_t status;
5057 if (property_get_bool("af.patch_park", false /* default_value */)) {
5058 // Park FastMixer to avoid potential DOS issues with writing to the HAL
5059 // or if HAL does not properly lock against access.
5060 AutoPark<FastMixer> park(mFastMixer);
5061 status = PlaybackThread::releaseAudioPatch_l(handle);
5062 } else {
5063 status = PlaybackThread::releaseAudioPatch_l(handle);
5064 }
5065 return status;
5066 }
5067
releaseAudioPatch_l(const audio_patch_handle_t handle)5068 status_t PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
5069 {
5070 status_t status = NO_ERROR;
5071
5072 mPatch = audio_patch{};
5073 mOutDeviceTypeAddrs.clear();
5074
5075 if (mOutput->audioHwDev->supportsAudioPatches()) {
5076 sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
5077 status = hwDevice->releaseAudioPatch(handle);
5078 } else {
5079 status = mOutput->stream->legacyReleaseAudioPatch();
5080 }
5081 // Force meteadata update after a route change
5082 mActiveTracks.setHasChanged();
5083
5084 return status;
5085 }
5086
addPatchTrack(const sp<IAfPatchTrack> & track)5087 void PlaybackThread::addPatchTrack(const sp<IAfPatchTrack>& track)
5088 {
5089 audio_utils::lock_guard _l(mutex());
5090 mTracks.add(track);
5091 }
5092
deletePatchTrack(const sp<IAfPatchTrack> & track)5093 void PlaybackThread::deletePatchTrack(const sp<IAfPatchTrack>& track)
5094 {
5095 audio_utils::lock_guard _l(mutex());
5096 destroyTrack_l(track);
5097 }
5098
toAudioPortConfig(struct audio_port_config * config)5099 void PlaybackThread::toAudioPortConfig(struct audio_port_config* config)
5100 {
5101 ThreadBase::toAudioPortConfig(config);
5102 config->role = AUDIO_PORT_ROLE_SOURCE;
5103 config->ext.mix.hw_module = mOutput->audioHwDev->handle();
5104 config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
5105 if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
5106 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
5107 config->flags.output = mOutput->flags;
5108 }
5109 }
5110
getLocalLogHeader() const5111 std::string PlaybackThread::getLocalLogHeader() const {
5112 using namespace std::literals;
5113 static constexpr auto indent = " "
5114 " "sv;
5115 return std::string{indent}.append(IAfTrack::getLogHeader());
5116 }
5117 // ----------------------------------------------------------------------------
5118
5119 /* static */
createMixerThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,type_t type,audio_config_base_t * mixerConfig)5120 sp<IAfPlaybackThread> IAfPlaybackThread::createMixerThread(
5121 const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
5122 audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t* mixerConfig) {
5123 return sp<MixerThread>::make(afThreadCallback, output, id, systemReady, type, mixerConfig);
5124 }
5125
MixerThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,type_t type,audio_config_base_t * mixerConfig)5126 MixerThread::MixerThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
5127 audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t *mixerConfig)
5128 : PlaybackThread(afThreadCallback, output, id, type, systemReady, mixerConfig),
5129 // mAudioMixer below
5130 // mFastMixer below
5131 mBluetoothLatencyModesEnabled(false),
5132 mFastMixerFutex(0),
5133 mMasterMono(false)
5134 // mOutputSink below
5135 // mPipeSink below
5136 // mNormalSink below
5137 {
5138 ALOGV("MixerThread() id=%d type=%d", id, type);
5139 ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, "
5140 "mFrameCount=%zu, mNormalFrameCount=%zu",
5141 mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
5142 mNormalFrameCount);
5143 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
5144
5145 if (type == DUPLICATING) {
5146 // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks
5147 // (downstream MixerThreads) in DuplicatingThread::threadLoop_write().
5148 // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink.
5149 // Balance is *not* set in the DuplicatingThread here (or from AudioFlinger),
5150 // as the downstream MixerThreads implement it.
5151 return;
5152 }
5153 // create an NBAIO sink for the HAL output stream, and negotiate
5154 mOutputSink = new AudioStreamOutSink(output->stream);
5155 size_t numCounterOffers = 0;
5156 const NBAIO_Format offers[1] = {Format_from_SR_C(
5157 mSampleRate, mChannelCount + mHapticChannelCount, mFormat)};
5158 #if !LOG_NDEBUG
5159 ssize_t index =
5160 #else
5161 (void)
5162 #endif
5163 mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
5164 ALOG_ASSERT(index == 0);
5165
5166 // initialize fast mixer depending on configuration
5167 bool initFastMixer;
5168 if (mType == SPATIALIZER || mType == BIT_PERFECT) {
5169 initFastMixer = false;
5170 } else {
5171 switch (kUseFastMixer) {
5172 case FastMixer_Never:
5173 initFastMixer = false;
5174 break;
5175 case FastMixer_Always:
5176 initFastMixer = true;
5177 break;
5178 case FastMixer_Static:
5179 case FastMixer_Dynamic:
5180 if (mType == MIXER && (output->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) {
5181 /* Do not init fast mixer on deep buffer, warn if buffers are confed too small */
5182 initFastMixer = false;
5183 ALOGW_IF(mFrameCount * 1000 / mSampleRate < kMinNormalSinkBufferSizeMs,
5184 "HAL DEEP BUFFER Buffer (%zu ms) is smaller than set minimal buffer "
5185 "(%u ms), seems like a configuration error",
5186 mFrameCount * 1000 / mSampleRate, kMinNormalSinkBufferSizeMs);
5187 } else {
5188 initFastMixer = mFrameCount < mNormalFrameCount;
5189 }
5190 break;
5191 }
5192 ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount,
5193 "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu",
5194 mFrameCount, mNormalFrameCount);
5195 }
5196 if (initFastMixer) {
5197 audio_format_t fastMixerFormat;
5198 if (mMixerBufferEnabled && mEffectBufferEnabled) {
5199 fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT;
5200 } else {
5201 fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
5202 }
5203 if (mFormat != fastMixerFormat) {
5204 // change our Sink format to accept our intermediate precision
5205 mFormat = fastMixerFormat;
5206 free(mSinkBuffer);
5207 mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat);
5208 const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
5209 (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
5210 }
5211
5212 // create a MonoPipe to connect our submix to FastMixer
5213 NBAIO_Format format = mOutputSink->format();
5214
5215 // adjust format to match that of the Fast Mixer
5216 ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat);
5217 format.mFormat = fastMixerFormat;
5218 format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount;
5219
5220 // This pipe depth compensates for scheduling latency of the normal mixer thread.
5221 // When it wakes up after a maximum latency, it runs a few cycles quickly before
5222 // finally blocking. Note the pipe implementation rounds up the request to a power of 2.
5223 MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
5224 const NBAIO_Format offersFast[1] = {format};
5225 size_t numCounterOffersFast = 0;
5226 #if !LOG_NDEBUG
5227 index =
5228 #else
5229 (void)
5230 #endif
5231 monoPipe->negotiate(offersFast, std::size(offersFast),
5232 nullptr /* counterOffers */, numCounterOffersFast);
5233 ALOG_ASSERT(index == 0);
5234 monoPipe->setAvgFrames((mScreenState & 1) ?
5235 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
5236 mPipeSink = monoPipe;
5237
5238 // create fast mixer and configure it initially with just one fast track for our submix
5239 mFastMixer = new FastMixer(mId);
5240 FastMixerStateQueue *sq = mFastMixer->sq();
5241 #ifdef STATE_QUEUE_DUMP
5242 sq->setObserverDump(&mStateQueueObserverDump);
5243 sq->setMutatorDump(&mStateQueueMutatorDump);
5244 #endif
5245 FastMixerState *state = sq->begin();
5246 FastTrack *fastTrack = &state->mFastTracks[0];
5247 // wrap the source side of the MonoPipe to make it an AudioBufferProvider
5248 fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
5249 fastTrack->mVolumeProvider = NULL;
5250 fastTrack->mChannelMask = static_cast<audio_channel_mask_t>(
5251 mChannelMask | mHapticChannelMask); // mPipeSink channel mask for
5252 // audio to FastMixer
5253 fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer
5254 fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE;
5255 fastTrack->mHapticScale = os::HapticScale::none();
5256 fastTrack->mHapticMaxAmplitude = NAN;
5257 fastTrack->mGeneration++;
5258 snprintf(fastTrack->mTraceName, sizeof(fastTrack->mTraceName),
5259 "%s.0.0.%d", AUDIO_TRACE_PREFIX_AUDIO_TRACK_FRDY, mId);
5260 state->mFastTracksGen++;
5261 state->mTrackMask = 1;
5262 // fast mixer will use the HAL output sink
5263 state->mOutputSink = mOutputSink.get();
5264 state->mOutputSinkGen++;
5265 state->mFrameCount = mFrameCount;
5266 // specify sink channel mask when haptic channel mask present as it can not
5267 // be calculated directly from channel count
5268 state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE
5269 ? AUDIO_CHANNEL_NONE
5270 : static_cast<audio_channel_mask_t>(mChannelMask | mHapticChannelMask);
5271 state->mCommand = FastMixerState::COLD_IDLE;
5272 // already done in constructor initialization list
5273 //mFastMixerFutex = 0;
5274 state->mColdFutexAddr = &mFastMixerFutex;
5275 state->mColdGen++;
5276 state->mDumpState = &mFastMixerDumpState;
5277 sq->end();
5278 {
5279 audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid());
5280 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
5281 }
5282
5283 // start the fast mixer
5284 mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
5285 pid_t tid = mFastMixer->getTid();
5286 sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
5287 stream()->setHalThreadPriority(kPriorityFastMixer);
5288
5289 #ifdef AUDIO_WATCHDOG
5290 // create and start the watchdog
5291 mAudioWatchdog = new AudioWatchdog();
5292 mAudioWatchdog->setDump(&mAudioWatchdogDump);
5293 mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
5294 tid = mAudioWatchdog->getTid();
5295 sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
5296 #endif
5297 } else {
5298 #ifdef TEE_SINK
5299 // Only use the MixerThread tee if there is no FastMixer.
5300 mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
5301 mTee.setId(std::string("_") + std::to_string(mId) + "_M");
5302 #endif
5303 }
5304
5305 switch (kUseFastMixer) {
5306 case FastMixer_Never:
5307 case FastMixer_Dynamic:
5308 mNormalSink = mOutputSink;
5309 break;
5310 case FastMixer_Always:
5311 mNormalSink = mPipeSink;
5312 break;
5313 case FastMixer_Static:
5314 mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
5315 break;
5316 }
5317 // setMasterBalance needs to be called after the FastMixer
5318 // (if any) is set up, in order to deliver the balance settings to it.
5319 setMasterBalance(afThreadCallback->getMasterBalance_l());
5320 }
5321
~MixerThread()5322 MixerThread::~MixerThread()
5323 {
5324 if (mFastMixer != 0) {
5325 FastMixerStateQueue *sq = mFastMixer->sq();
5326 FastMixerState *state = sq->begin();
5327 if (state->mCommand == FastMixerState::COLD_IDLE) {
5328 int32_t old = android_atomic_inc(&mFastMixerFutex);
5329 if (old == -1) {
5330 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
5331 }
5332 }
5333 state->mCommand = FastMixerState::EXIT;
5334 sq->end();
5335 {
5336 audio_utils::mutex::scoped_join_wait_check queueWaitCheck(mFastMixer->getTid());
5337 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
5338 mFastMixer->join();
5339 }
5340 // Though the fast mixer thread has exited, it's state queue is still valid.
5341 // We'll use that extract the final state which contains one remaining fast track
5342 // corresponding to our sub-mix.
5343 state = sq->begin();
5344 ALOG_ASSERT(state->mTrackMask == 1);
5345 FastTrack *fastTrack = &state->mFastTracks[0];
5346 ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
5347 delete fastTrack->mBufferProvider;
5348 sq->end(false /*didModify*/);
5349 mFastMixer.clear();
5350 #ifdef AUDIO_WATCHDOG
5351 if (mAudioWatchdog != 0) {
5352 mAudioWatchdog->requestExit();
5353 mAudioWatchdog->requestExitAndWait();
5354 mAudioWatchdog.clear();
5355 }
5356 #endif
5357 }
5358 delete mAudioMixer;
5359 }
5360
onFirstRef()5361 void MixerThread::onFirstRef() {
5362 PlaybackThread::onFirstRef();
5363
5364 audio_utils::lock_guard _l(mutex());
5365 if (mOutput != nullptr && mOutput->stream != nullptr) {
5366 status_t status = mOutput->stream->setLatencyModeCallback(this);
5367 if (status != INVALID_OPERATION) {
5368 updateHalSupportedLatencyModes_l();
5369 }
5370 // Default to enabled if the HAL supports it. This can be changed by Audioflinger after
5371 // the thread construction according to AudioFlinger::mBluetoothLatencyModesEnabled
5372 mBluetoothLatencyModesEnabled.store(
5373 mOutput->audioHwDev->supportsBluetoothVariableLatency());
5374 }
5375 }
5376
correctLatency_l(uint32_t latency) const5377 uint32_t MixerThread::correctLatency_l(uint32_t latency) const
5378 {
5379 if (mFastMixer != 0) {
5380 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
5381 latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
5382 }
5383 return latency;
5384 }
5385
threadLoop_write()5386 ssize_t MixerThread::threadLoop_write()
5387 {
5388 // FIXME we should only do one push per cycle; confirm this is true
5389 // Start the fast mixer if it's not already running
5390 if (mFastMixer != 0) {
5391 FastMixerStateQueue *sq = mFastMixer->sq();
5392 FastMixerState *state = sq->begin();
5393 if (state->mCommand != FastMixerState::MIX_WRITE &&
5394 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
5395 if (state->mCommand == FastMixerState::COLD_IDLE) {
5396
5397 // FIXME workaround for first HAL write being CPU bound on some devices
5398 ATRACE_BEGIN("write");
5399 mOutput->write((char *)mSinkBuffer, 0);
5400 ATRACE_END();
5401
5402 int32_t old = android_atomic_inc(&mFastMixerFutex);
5403 if (old == -1) {
5404 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
5405 }
5406 #ifdef AUDIO_WATCHDOG
5407 if (mAudioWatchdog != 0) {
5408 mAudioWatchdog->resume();
5409 }
5410 #endif
5411 }
5412 state->mCommand = FastMixerState::MIX_WRITE;
5413 #ifdef FAST_THREAD_STATISTICS
5414 mFastMixerDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ?
5415 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN);
5416 #endif
5417 sq->end();
5418 {
5419 audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid());
5420 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
5421 }
5422 if (kUseFastMixer == FastMixer_Dynamic) {
5423 mNormalSink = mPipeSink;
5424 }
5425 } else {
5426 sq->end(false /*didModify*/);
5427 }
5428 }
5429 return PlaybackThread::threadLoop_write();
5430 }
5431
threadLoop_standby()5432 void MixerThread::threadLoop_standby()
5433 {
5434 // Idle the fast mixer if it's currently running
5435 if (mFastMixer != 0) {
5436 FastMixerStateQueue *sq = mFastMixer->sq();
5437 FastMixerState *state = sq->begin();
5438 if (!(state->mCommand & FastMixerState::IDLE)) {
5439 // Report any frames trapped in the Monopipe
5440 MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get();
5441 const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite();
5442 mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld "
5443 "monoPipeWritten:%lld monoPipeLeft:%lld",
5444 (long long)mFramesWritten, (long long)mSuspendedFrames,
5445 (long long)mPipeSink->framesWritten(), pipeFrames);
5446 mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str());
5447
5448 state->mCommand = FastMixerState::COLD_IDLE;
5449 state->mColdFutexAddr = &mFastMixerFutex;
5450 state->mColdGen++;
5451 mFastMixerFutex = 0;
5452 sq->end();
5453 // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
5454 {
5455 audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid());
5456 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
5457 }
5458 if (kUseFastMixer == FastMixer_Dynamic) {
5459 mNormalSink = mOutputSink;
5460 }
5461 #ifdef AUDIO_WATCHDOG
5462 if (mAudioWatchdog != 0) {
5463 mAudioWatchdog->pause();
5464 }
5465 #endif
5466 } else {
5467 sq->end(false /*didModify*/);
5468 }
5469 }
5470 PlaybackThread::threadLoop_standby();
5471 }
5472
waitingAsyncCallback_l()5473 bool PlaybackThread::waitingAsyncCallback_l()
5474 {
5475 return false;
5476 }
5477
shouldStandby_l()5478 bool PlaybackThread::shouldStandby_l()
5479 {
5480 return !mStandby;
5481 }
5482
waitingAsyncCallback()5483 bool PlaybackThread::waitingAsyncCallback()
5484 {
5485 audio_utils::lock_guard _l(mutex());
5486 return waitingAsyncCallback_l();
5487 }
5488
5489 // shared by MIXER and DIRECT, overridden by DUPLICATING
threadLoop_standby()5490 void PlaybackThread::threadLoop_standby()
5491 {
5492 ALOGV("%s: audio hardware entering standby, mixer %p, suspend count %d",
5493 __func__, this, (int32_t)mSuspended);
5494 mOutput->standby();
5495 if (mUseAsyncWrite != 0) {
5496 // discard any pending drain or write ack by incrementing sequence
5497 mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
5498 mDrainSequence = (mDrainSequence + 2) & ~1;
5499 ALOG_ASSERT(mCallbackThread != 0);
5500 mCallbackThread->setWriteBlocked(mWriteAckSequence);
5501 mCallbackThread->setDraining(mDrainSequence);
5502 }
5503 mHwPaused = false;
5504 setHalLatencyMode_l();
5505 }
5506
onAddNewTrack_l()5507 void PlaybackThread::onAddNewTrack_l()
5508 {
5509 ALOGV("signal playback thread");
5510 broadcast_l();
5511 }
5512
onAsyncError(bool isHardError)5513 void PlaybackThread::onAsyncError(bool isHardError)
5514 {
5515 auto allTrackPortIds = getTrackPortIds();
5516 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
5517 invalidateTracks((audio_stream_type_t)i);
5518 }
5519 if (isHardError) {
5520 mAfThreadCallback->onHardError(allTrackPortIds);
5521 }
5522 }
5523
threadLoop_mix()5524 void MixerThread::threadLoop_mix()
5525 {
5526 // mix buffers...
5527 mAudioMixer->process();
5528 mCurrentWriteLength = mSinkBufferSize;
5529 // increase sleep time progressively when application underrun condition clears.
5530 // Only increase sleep time if the mixer is ready for two consecutive times to avoid
5531 // that a steady state of alternating ready/not ready conditions keeps the sleep time
5532 // such that we would underrun the audio HAL.
5533 if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) {
5534 sleepTimeShift--;
5535 }
5536 mSleepTimeUs = 0;
5537 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
5538 //TODO: delay standby when effects have a tail
5539
5540 }
5541
threadLoop_sleepTime()5542 void MixerThread::threadLoop_sleepTime()
5543 {
5544 // If no tracks are ready, sleep once for the duration of an output
5545 // buffer size, then write 0s to the output
5546 if (mSleepTimeUs == 0) {
5547 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
5548 if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) {
5549 // Using the Monopipe availableToWrite, we estimate the
5550 // sleep time to retry for more data (before we underrun).
5551 MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get());
5552 const ssize_t availableToWrite = mPipeSink->availableToWrite();
5553 const size_t pipeFrames = monoPipe->maxFrames();
5554 const size_t framesLeft = pipeFrames - max(availableToWrite, 0);
5555 // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount
5556 const size_t framesDelay = std::min(
5557 mNormalFrameCount, max(framesLeft / 2, mFrameCount));
5558 ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu",
5559 pipeFrames, framesLeft, framesDelay);
5560 mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate;
5561 } else {
5562 mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift;
5563 if (mSleepTimeUs < kMinThreadSleepTimeUs) {
5564 mSleepTimeUs = kMinThreadSleepTimeUs;
5565 }
5566 // reduce sleep time in case of consecutive application underruns to avoid
5567 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
5568 // duration we would end up writing less data than needed by the audio HAL if
5569 // the condition persists.
5570 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
5571 sleepTimeShift++;
5572 }
5573 }
5574 } else {
5575 mSleepTimeUs = mIdleSleepTimeUs;
5576 }
5577 } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
5578 // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared
5579 // before effects processing or output.
5580 if (mMixerBufferValid) {
5581 memset(mMixerBuffer, 0, mMixerBufferSize);
5582 if (mType == SPATIALIZER) {
5583 memset(mSinkBuffer, 0, mSinkBufferSize);
5584 }
5585 } else {
5586 memset(mSinkBuffer, 0, mSinkBufferSize);
5587 }
5588 mSleepTimeUs = 0;
5589 ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED),
5590 "anticipated start");
5591 }
5592 // TODO add standby time extension fct of effect tail
5593 }
5594
5595 // prepareTracks_l() must be called with ThreadBase::mutex() held
prepareTracks_l(Vector<sp<IAfTrack>> * tracksToRemove)5596 PlaybackThread::mixer_state MixerThread::prepareTracks_l(
5597 Vector<sp<IAfTrack>>* tracksToRemove)
5598 {
5599 // clean up deleted track ids in AudioMixer before allocating new tracks
5600 (void)mTracks.processDeletedTrackIds([this](int trackId) {
5601 // for each trackId, destroy it in the AudioMixer
5602 if (mAudioMixer->exists(trackId)) {
5603 mAudioMixer->destroy(trackId);
5604 }
5605 });
5606 mTracks.clearDeletedTrackIds();
5607
5608 mixer_state mixerStatus = MIXER_IDLE;
5609 // find out which tracks need to be processed
5610 size_t count = mActiveTracks.size();
5611 size_t mixedTracks = 0;
5612 size_t tracksWithEffect = 0;
5613 // counts only _active_ fast tracks
5614 size_t fastTracks = 0;
5615 uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
5616
5617 float masterVolume = mMasterVolume;
5618 bool masterMute = mMasterMute;
5619
5620 if (masterMute) {
5621 masterVolume = 0;
5622 }
5623 // Delegate master volume control to effect in output mix effect chain if needed
5624 sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
5625 if (chain != 0) {
5626 uint32_t v = (uint32_t)(masterVolume * (1 << 24));
5627 chain->setVolume(&v, &v);
5628 masterVolume = (float)((v + (1 << 23)) >> 24);
5629 chain.clear();
5630 }
5631
5632 // prepare a new state to push
5633 FastMixerStateQueue *sq = NULL;
5634 FastMixerState *state = NULL;
5635 bool didModify = false;
5636 FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
5637 bool coldIdle = false;
5638 if (mFastMixer != 0) {
5639 sq = mFastMixer->sq();
5640 state = sq->begin();
5641 coldIdle = state->mCommand == FastMixerState::COLD_IDLE;
5642 }
5643
5644 mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found.
5645 mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found.
5646
5647 // DeferredOperations handles statistics after setting mixerStatus.
5648 class DeferredOperations {
5649 public:
5650 DeferredOperations(mixer_state *mixerStatus, ThreadMetrics *threadMetrics)
5651 : mMixerStatus(mixerStatus)
5652 , mThreadMetrics(threadMetrics) {}
5653
5654 // when leaving scope, tally frames properly.
5655 ~DeferredOperations() {
5656 // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY)
5657 // because that is when the underrun occurs.
5658 // We do not distinguish between FastTracks and NormalTracks here.
5659 size_t maxUnderrunFrames = 0;
5660 if (*mMixerStatus == MIXER_TRACKS_READY && mUnderrunFrames.size() > 0) {
5661 for (const auto &underrun : mUnderrunFrames) {
5662 underrun.first->tallyUnderrunFrames(underrun.second);
5663 maxUnderrunFrames = max(underrun.second, maxUnderrunFrames);
5664 }
5665 }
5666 // send the max underrun frames for this mixer period
5667 mThreadMetrics->logUnderrunFrames(maxUnderrunFrames);
5668 }
5669
5670 // tallyUnderrunFrames() is called to update the track counters
5671 // with the number of underrun frames for a particular mixer period.
5672 // We defer tallying until we know the final mixer status.
5673 void tallyUnderrunFrames(const sp<IAfTrack>& track, size_t underrunFrames) {
5674 mUnderrunFrames.emplace_back(track, underrunFrames);
5675 }
5676
5677 private:
5678 const mixer_state * const mMixerStatus;
5679 ThreadMetrics * const mThreadMetrics;
5680 std::vector<std::pair<sp<IAfTrack>, size_t>> mUnderrunFrames;
5681 } deferredOperations(&mixerStatus, &mThreadMetrics);
5682 // implicit nested scope for variable capture
5683
5684 bool noFastHapticTrack = true;
5685 for (size_t i=0 ; i<count ; i++) {
5686 const sp<IAfTrack> t = mActiveTracks[i];
5687
5688 // this const just means the local variable doesn't change
5689 IAfTrack* const track = t.get();
5690
5691 // process fast tracks
5692 if (track->isFastTrack()) {
5693 LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr,
5694 "%s(%d): FastTrack(%d) present without FastMixer",
5695 __func__, id(), track->id());
5696
5697 if (track->getHapticPlaybackEnabled()) {
5698 noFastHapticTrack = false;
5699 }
5700
5701 // It's theoretically possible (though unlikely) for a fast track to be created
5702 // and then removed within the same normal mix cycle. This is not a problem, as
5703 // the track never becomes active so it's fast mixer slot is never touched.
5704 // The converse, of removing an (active) track and then creating a new track
5705 // at the identical fast mixer slot within the same normal mix cycle,
5706 // is impossible because the slot isn't marked available until the end of each cycle.
5707 int j = track->fastIndex();
5708 ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks);
5709 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
5710 FastTrack *fastTrack = &state->mFastTracks[j];
5711
5712 // Determine whether the track is currently in underrun condition,
5713 // and whether it had a recent underrun.
5714 FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
5715 FastTrackUnderruns underruns = ftDump->mUnderruns;
5716 uint32_t recentFull = (underruns.mBitFields.mFull -
5717 track->fastTrackUnderruns().mBitFields.mFull) & UNDERRUN_MASK;
5718 uint32_t recentPartial = (underruns.mBitFields.mPartial -
5719 track->fastTrackUnderruns().mBitFields.mPartial) & UNDERRUN_MASK;
5720 uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
5721 track->fastTrackUnderruns().mBitFields.mEmpty) & UNDERRUN_MASK;
5722 uint32_t recentUnderruns = recentPartial + recentEmpty;
5723 track->fastTrackUnderruns() = underruns;
5724 // don't count underruns that occur while stopping or pausing
5725 // or stopped which can occur when flush() is called while active
5726 size_t underrunFrames = 0;
5727 if (!(track->isStopping() || track->isPausing()
5728 || track->isStopped() || track->isPaused())
5729 && recentUnderruns > 0) {
5730 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun
5731 underrunFrames = recentUnderruns * mFrameCount;
5732 }
5733 // Immediately account for FastTrack underruns.
5734 track->audioTrackServerProxy()->tallyUnderrunFrames(underrunFrames);
5735
5736 // This is similar to the state machine for normal tracks,
5737 // with a few modifications for fast tracks.
5738 bool isActive = true;
5739 switch (track->state()) {
5740 case IAfTrackBase::STOPPING_1:
5741 // track stays active in STOPPING_1 state until first underrun
5742 if (recentUnderruns > 0 || track->isTerminated()) {
5743 track->setState(IAfTrackBase::STOPPING_2);
5744 }
5745 break;
5746 case IAfTrackBase::PAUSING:
5747 // ramp down is not yet implemented
5748 track->setPaused();
5749 break;
5750 case IAfTrackBase::RESUMING:
5751 // ramp up is not yet implemented
5752 track->setState(IAfTrackBase::ACTIVE);
5753 break;
5754 case IAfTrackBase::ACTIVE:
5755 if (recentFull > 0 || recentPartial > 0) {
5756 // track has provided at least some frames recently: reset retry count
5757 track->retryCount() = kMaxTrackRetries;
5758 }
5759 if (recentUnderruns == 0) {
5760 // no recent underruns: stay active
5761 break;
5762 }
5763 // there has recently been an underrun of some kind
5764 if (track->sharedBuffer() == 0) {
5765 // were any of the recent underruns "empty" (no frames available)?
5766 if (recentEmpty == 0) {
5767 // no, then ignore the partial underruns as they are allowed indefinitely
5768 break;
5769 }
5770 // there has recently been an "empty" underrun: decrement the retry counter
5771 if (--(track->retryCount()) > 0) {
5772 break;
5773 }
5774 // indicate to client process that the track was disabled because of underrun;
5775 // it will then automatically call start() when data is available
5776 track->disable();
5777 // remove from active list, but state remains ACTIVE [confusing but true]
5778 isActive = false;
5779 break;
5780 }
5781 FALLTHROUGH_INTENDED;
5782 case IAfTrackBase::STOPPING_2:
5783 case IAfTrackBase::PAUSED:
5784 case IAfTrackBase::STOPPED:
5785 case IAfTrackBase::FLUSHED: // flush() while active
5786 // Check for presentation complete if track is inactive
5787 // We have consumed all the buffers of this track.
5788 // This would be incomplete if we auto-paused on underrun
5789 {
5790 uint32_t latency = 0;
5791 status_t result = mOutput->stream->getLatency(&latency);
5792 ALOGE_IF(result != OK,
5793 "Error when retrieving output stream latency: %d", result);
5794 size_t audioHALFrames = (latency * mSampleRate) / 1000;
5795 int64_t framesWritten = mBytesWritten / mFrameSize;
5796 if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) {
5797 // track stays in active list until presentation is complete
5798 break;
5799 }
5800 }
5801 if (track->isStopping_2()) {
5802 track->setState(IAfTrackBase::STOPPED);
5803 }
5804 if (track->isStopped()) {
5805 // Can't reset directly, as fast mixer is still polling this track
5806 // track->reset();
5807 // So instead mark this track as needing to be reset after push with ack
5808 resetMask |= 1 << i;
5809 }
5810 isActive = false;
5811 break;
5812 case IAfTrackBase::IDLE:
5813 default:
5814 LOG_ALWAYS_FATAL("unexpected track state %d", (int)track->state());
5815 }
5816
5817 if (isActive) {
5818 // was it previously inactive?
5819 if (!(state->mTrackMask & (1 << j))) {
5820 ExtendedAudioBufferProvider *eabp = track->asExtendedAudioBufferProvider();
5821 VolumeProvider *vp = track->asVolumeProvider();
5822 fastTrack->mBufferProvider = eabp;
5823 fastTrack->mVolumeProvider = vp;
5824 fastTrack->mChannelMask = track->channelMask();
5825 fastTrack->mFormat = track->format();
5826 fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
5827 fastTrack->mHapticScale = track->getHapticScale();
5828 fastTrack->mHapticMaxAmplitude = track->getHapticMaxAmplitude();
5829 fastTrack->mGeneration++;
5830 snprintf(fastTrack->mTraceName, sizeof(fastTrack->mTraceName),
5831 "%s%s", AUDIO_TRACE_PREFIX_AUDIO_TRACK_FRDY,
5832 track->getTraceSuffix().c_str());
5833 state->mTrackMask |= 1 << j;
5834 didModify = true;
5835 // no acknowledgement required for newly active tracks
5836 }
5837 sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy();
5838 float volume;
5839 if (!audioserver_flags::portid_volume_management()) {
5840 if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) {
5841 volume = 0.f;
5842 } else {
5843 volume = masterVolume * mStreamTypes[track->streamType()].volume;
5844 }
5845 } else {
5846 if (track->isPlaybackRestricted() || track->getPortMute()) {
5847 volume = 0.f;
5848 } else {
5849 volume = masterVolume * track->getPortVolume();
5850 }
5851 }
5852 const auto amn = mAfThreadCallback->getAudioManagerNative();
5853 if (amn) {
5854 track->maybeLogPlaybackHardening(*amn);
5855 }
5856 handleVoipVolume_l(&volume);
5857
5858 // cache the combined master volume and stream type volume for fast mixer; this
5859 // lacks any synchronization or barrier so VolumeProvider may read a stale value
5860 const float vh = track->getVolumeHandler()->getVolume(
5861 proxy->framesReleased()).first;
5862 volume *= vh;
5863 track->setCachedVolume(volume);
5864 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
5865 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
5866 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
5867 if (amn) {
5868 if (!audioserver_flags::portid_volume_management()) {
5869 track->processMuteEvent(*amn,
5870 /*muteState=*/{masterVolume == 0.f,
5871 mStreamTypes[track->streamType()].volume == 0.f,
5872 mStreamTypes[track->streamType()].mute,
5873 track->isPlaybackRestrictedOp(),
5874 vlf == 0.f && vrf == 0.f,
5875 vh == 0.f,
5876 /*muteFromPortVolume=*/false,
5877 track->isPlaybackRestrictedControl()});
5878 } else {
5879 track->processMuteEvent(*amn,
5880 /*muteState=*/{masterVolume == 0.f,
5881 track->getPortVolume() == 0.f,
5882 /* muteFromStreamMuted= */ false,
5883 track->isPlaybackRestrictedOp(),
5884 vlf == 0.f && vrf == 0.f,
5885 vh == 0.f,
5886 track->getPortMute(),
5887 track->isPlaybackRestrictedControl()});
5888 }
5889 }
5890 vlf *= volume;
5891 vrf *= volume;
5892
5893 if (track->getInternalMute()) {
5894 vlf = 0.f;
5895 vrf = 0.f;
5896 }
5897
5898 track->setFinalVolume(vlf, vrf);
5899 ++fastTracks;
5900 } else {
5901 // was it previously active?
5902 if (state->mTrackMask & (1 << j)) {
5903 fastTrack->mBufferProvider = NULL;
5904 fastTrack->mGeneration++;
5905 state->mTrackMask &= ~(1 << j);
5906 didModify = true;
5907 // If any fast tracks were removed, we must wait for acknowledgement
5908 // because we're about to decrement the last sp<> on those tracks.
5909 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
5910 } else {
5911 // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an
5912 // AudioTrack may start (which may not be with a start() but with a write()
5913 // after underrun) and immediately paused or released. In that case the
5914 // FastTrack state hasn't had time to update.
5915 // TODO Remove the ALOGW when this theory is confirmed.
5916 ALOGW("fast track %d should have been active; "
5917 "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d",
5918 j, (int)track->state(), state->mTrackMask, recentUnderruns,
5919 track->sharedBuffer() != 0);
5920 // Since the FastMixer state already has the track inactive, do nothing here.
5921 }
5922 tracksToRemove->add(track);
5923 // Avoids a misleading display in dumpsys
5924 track->fastTrackUnderruns().mBitFields.mMostRecent = UNDERRUN_FULL;
5925 }
5926 if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) {
5927 fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
5928 didModify = true;
5929 }
5930 continue;
5931 }
5932
5933 { // local variable scope to avoid goto warning
5934
5935 audio_track_cblk_t* cblk = track->cblk();
5936
5937 // The first time a track is added we wait
5938 // for all its buffers to be filled before processing it
5939 const int trackId = track->id();
5940
5941 // if an active track doesn't exist in the AudioMixer, create it.
5942 // use the trackId as the AudioMixer name.
5943 if (!mAudioMixer->exists(trackId)) {
5944 status_t status = mAudioMixer->create(
5945 trackId,
5946 track->channelMask(),
5947 track->format(),
5948 track->sessionId());
5949 if (status != OK) {
5950 ALOGW("%s(): AudioMixer cannot create track(%d)"
5951 " mask %#x, format %#x, sessionId %d",
5952 __func__, trackId,
5953 track->channelMask(), track->format(), track->sessionId());
5954 tracksToRemove->add(track);
5955 track->invalidate(); // consider it dead.
5956 continue;
5957 }
5958 }
5959
5960 // make sure that we have enough frames to mix one full buffer.
5961 // enforce this condition only once to enable draining the buffer in case the client
5962 // app does not call stop() and relies on underrun to stop:
5963 // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
5964 // during last round
5965 size_t desiredFrames;
5966 const uint32_t sampleRate = track->audioTrackServerProxy()->getSampleRate();
5967 const AudioPlaybackRate playbackRate = track->audioTrackServerProxy()->getPlaybackRate();
5968
5969 desiredFrames = sourceFramesNeededWithTimestretch(
5970 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed);
5971 // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed.
5972 // add frames already consumed but not yet released by the resampler
5973 // because mAudioTrackServerProxy->framesReady() will include these frames
5974 desiredFrames += mAudioMixer->getUnreleasedFrames(trackId);
5975
5976 uint32_t minFrames = 1;
5977 if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
5978 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
5979 minFrames = desiredFrames;
5980 }
5981
5982 size_t framesReady = track->framesReady();
5983 if (ATRACE_ENABLED()) [[unlikely]] {
5984 ATRACE_INT(std::string(AUDIO_TRACE_PREFIX_AUDIO_TRACK_NRDY)
5985 .append(track->getTraceSuffix()).c_str(), framesReady);
5986 }
5987 if ((framesReady >= minFrames) && track->isReady() &&
5988 !track->isPaused() && !track->isTerminated())
5989 {
5990 ALOGVV("track(%d) s=%08x [OK] on thread %p", trackId, cblk->mServer, this);
5991
5992 mixedTracks++;
5993
5994 // track->mainBuffer() != mSinkBuffer and mMixerBuffer means
5995 // there is an effect chain connected to the track
5996 chain.clear();
5997 if (track->mainBuffer() != mSinkBuffer &&
5998 track->mainBuffer() != mMixerBuffer) {
5999 if (mEffectBufferEnabled) {
6000 mEffectBufferValid = true; // Later can set directly.
6001 }
6002 chain = getEffectChain_l(track->sessionId());
6003 // Delegate volume control to effect in track effect chain if needed
6004 if (chain != 0) {
6005 tracksWithEffect++;
6006 } else {
6007 ALOGW("prepareTracks_l(): track(%d) attached to effect but no chain found on "
6008 "session %d",
6009 trackId, track->sessionId());
6010 }
6011 }
6012
6013
6014 int param = AudioMixer::VOLUME;
6015 if (track->fillingStatus() == IAfTrack::FS_FILLED) {
6016 // no ramp for the first volume setting
6017 track->fillingStatus() = IAfTrack::FS_ACTIVE;
6018 if (track->state() == IAfTrackBase::RESUMING) {
6019 track->setState(IAfTrackBase::ACTIVE);
6020 // If a new track is paused immediately after start, do not ramp on resume.
6021 if (cblk->mServer != 0) {
6022 param = AudioMixer::RAMP_VOLUME;
6023 }
6024 }
6025 mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
6026 mLeftVolFloat = -1.0;
6027 // FIXME should not make a decision based on mServer
6028 } else if (cblk->mServer != 0) {
6029 // If the track is stopped before the first frame was mixed,
6030 // do not apply ramp
6031 param = AudioMixer::RAMP_VOLUME;
6032 }
6033
6034 // compute volume for this track
6035 uint32_t vl, vr; // in U8.24 integer format
6036 float vlf, vrf, vaf; // in [0.0, 1.0] float format
6037 // read original volumes with volume control
6038 // Always fetch volumeshaper volume to ensure state is updated.
6039 const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy();
6040 const float vh = track->getVolumeHandler()->getVolume(
6041 track->audioTrackServerProxy()->framesReleased()).first;
6042 float v;
6043 if (!audioserver_flags::portid_volume_management()) {
6044 v = masterVolume * mStreamTypes[track->streamType()].volume;
6045 if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
6046 v = 0;
6047 }
6048 } else {
6049 v = masterVolume * track->getPortVolume();
6050 if (track->isPlaybackRestricted() || track->getPortMute()) {
6051 v = 0;
6052 }
6053 }
6054
6055 handleVoipVolume_l(&v);
6056 const auto amn = mAfThreadCallback->getAudioManagerNative();
6057 if (amn) {
6058 track->maybeLogPlaybackHardening(*amn);
6059 }
6060
6061 if (track->isPausing()) {
6062 vl = vr = 0;
6063 vlf = vrf = vaf = 0.;
6064 track->setPaused();
6065 } else {
6066 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
6067 vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
6068 vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
6069 // track volumes come from shared memory, so can't be trusted and must be clamped
6070 if (vlf > GAIN_FLOAT_UNITY) {
6071 ALOGV("Track left volume out of range: %.3g", vlf);
6072 vlf = GAIN_FLOAT_UNITY;
6073 }
6074 if (vrf > GAIN_FLOAT_UNITY) {
6075 ALOGV("Track right volume out of range: %.3g", vrf);
6076 vrf = GAIN_FLOAT_UNITY;
6077 }
6078 if (amn) {
6079 if (!audioserver_flags::portid_volume_management()) {
6080 track->processMuteEvent(*amn,
6081 /*muteState=*/{masterVolume == 0.f,
6082 mStreamTypes[track->streamType()].volume == 0.f,
6083 mStreamTypes[track->streamType()].mute,
6084 track->isPlaybackRestrictedOp(),
6085 vlf == 0.f && vrf == 0.f,
6086 vh == 0.f,
6087 /*muteFromPortVolume=*/false,
6088 track->isPlaybackRestrictedControl()});
6089 } else {
6090 track->processMuteEvent(*amn,
6091 /*muteState=*/{masterVolume == 0.f,
6092 track->getPortVolume() == 0.f,
6093 /* muteFromStreamMuted= */ false,
6094 track->isPlaybackRestrictedOp(),
6095 vlf == 0.f && vrf == 0.f,
6096 vh == 0.f,
6097 track->getPortMute(),
6098 track->isPlaybackRestrictedControl()});
6099 }
6100 }
6101 // now apply the master volume and stream type volume and shaper volume
6102 vlf *= v * vh;
6103 vrf *= v * vh;
6104 // assuming master volume and stream type volume each go up to 1.0,
6105 // then derive vl and vr as U8.24 versions for the effect chain
6106 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
6107 vl = (uint32_t) (scaleto8_24 * vlf);
6108 vr = (uint32_t) (scaleto8_24 * vrf);
6109 // vl and vr are now in U8.24 format
6110 uint16_t sendLevel = proxy->getSendLevel_U4_12();
6111 // send level comes from shared memory and so may be corrupt
6112 if (sendLevel > MAX_GAIN_INT) {
6113 ALOGV("Track send level out of range: %04X", sendLevel);
6114 sendLevel = MAX_GAIN_INT;
6115 }
6116 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
6117 vaf = v * sendLevel * (1. / MAX_GAIN_INT);
6118 }
6119
6120 if (track->getInternalMute()) {
6121 vrf = 0.f;
6122 vlf = 0.f;
6123 }
6124
6125 track->setFinalVolume(vlf, vrf);
6126
6127 // Delegate volume control to effect in track effect chain if needed
6128 if (chain != 0 && chain->setVolume(&vl, &vr)) {
6129 // Do not ramp volume if volume is controlled by effect
6130 param = AudioMixer::VOLUME;
6131 // Update remaining floating point volume levels
6132 vlf = (float)vl / (1 << 24);
6133 vrf = (float)vr / (1 << 24);
6134 track->setHasVolumeController(true);
6135 } else {
6136 // force no volume ramp when volume controller was just disabled or removed
6137 // from effect chain to avoid volume spike
6138 if (track->hasVolumeController()) {
6139 param = AudioMixer::VOLUME;
6140 }
6141 track->setHasVolumeController(false);
6142 }
6143
6144 // XXX: these things DON'T need to be done each time
6145 mAudioMixer->setBufferProvider(trackId, track->asExtendedAudioBufferProvider());
6146 mAudioMixer->enable(trackId);
6147
6148 mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf);
6149 mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf);
6150 mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf);
6151 mAudioMixer->setParameter(
6152 trackId,
6153 AudioMixer::TRACK,
6154 AudioMixer::FORMAT, (void *)track->format());
6155 mAudioMixer->setParameter(
6156 trackId,
6157 AudioMixer::TRACK,
6158 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
6159
6160 if (mType == SPATIALIZER && !track->isSpatialized()) {
6161 mAudioMixer->setParameter(
6162 trackId,
6163 AudioMixer::TRACK,
6164 AudioMixer::MIXER_CHANNEL_MASK,
6165 (void *)(uintptr_t)(mChannelMask | mHapticChannelMask));
6166 } else {
6167 mAudioMixer->setParameter(
6168 trackId,
6169 AudioMixer::TRACK,
6170 AudioMixer::MIXER_CHANNEL_MASK,
6171 (void *)(uintptr_t)(mMixerChannelMask | mHapticChannelMask));
6172 }
6173
6174 // limit track sample rate to 2 x output sample rate, which changes at re-configuration
6175 uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX;
6176 uint32_t reqSampleRate = proxy->getSampleRate();
6177 if (reqSampleRate == 0) {
6178 reqSampleRate = mSampleRate;
6179 } else if (reqSampleRate > maxSampleRate) {
6180 reqSampleRate = maxSampleRate;
6181 }
6182 mAudioMixer->setParameter(
6183 trackId,
6184 AudioMixer::RESAMPLE,
6185 AudioMixer::SAMPLE_RATE,
6186 (void *)(uintptr_t)reqSampleRate);
6187
6188 mAudioMixer->setParameter(
6189 trackId,
6190 AudioMixer::TIMESTRETCH,
6191 AudioMixer::PLAYBACK_RATE,
6192 // cast away constness for this generic API.
6193 const_cast<void *>(reinterpret_cast<const void *>(&playbackRate)));
6194
6195 /*
6196 * Select the appropriate output buffer for the track.
6197 *
6198 * Tracks with effects go into their own effects chain buffer
6199 * and from there into either mEffectBuffer or mSinkBuffer.
6200 *
6201 * Other tracks can use mMixerBuffer for higher precision
6202 * channel accumulation. If this buffer is enabled
6203 * (mMixerBufferEnabled true), then selected tracks will accumulate
6204 * into it.
6205 *
6206 */
6207 if (mMixerBufferEnabled
6208 && (track->mainBuffer() == mSinkBuffer
6209 || track->mainBuffer() == mMixerBuffer)) {
6210 if (mType == SPATIALIZER && !track->isSpatialized()) {
6211 mAudioMixer->setParameter(
6212 trackId,
6213 AudioMixer::TRACK,
6214 AudioMixer::MIXER_FORMAT, (void *)mEffectBufferFormat);
6215 mAudioMixer->setParameter(
6216 trackId,
6217 AudioMixer::TRACK,
6218 AudioMixer::MAIN_BUFFER, (void *)mPostSpatializerBuffer);
6219 } else {
6220 mAudioMixer->setParameter(
6221 trackId,
6222 AudioMixer::TRACK,
6223 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
6224 mAudioMixer->setParameter(
6225 trackId,
6226 AudioMixer::TRACK,
6227 AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
6228 // TODO: override track->mainBuffer()?
6229 mMixerBufferValid = true;
6230 }
6231 } else {
6232 mAudioMixer->setParameter(
6233 trackId,
6234 AudioMixer::TRACK,
6235 AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_FLOAT);
6236 mAudioMixer->setParameter(
6237 trackId,
6238 AudioMixer::TRACK,
6239 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
6240 }
6241 mAudioMixer->setParameter(
6242 trackId,
6243 AudioMixer::TRACK,
6244 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
6245 mAudioMixer->setParameter(
6246 trackId,
6247 AudioMixer::TRACK,
6248 AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled());
6249 const os::HapticScale hapticScale = track->getHapticScale();
6250 mAudioMixer->setParameter(
6251 trackId,
6252 AudioMixer::TRACK,
6253 AudioMixer::HAPTIC_SCALE, (void *)&hapticScale);
6254 const float hapticMaxAmplitude = track->getHapticMaxAmplitude();
6255 mAudioMixer->setParameter(
6256 trackId,
6257 AudioMixer::TRACK,
6258 AudioMixer::HAPTIC_MAX_AMPLITUDE, (void *)&hapticMaxAmplitude);
6259
6260 // reset retry count
6261 track->retryCount() = kMaxTrackRetries;
6262
6263 // If one track is ready, set the mixer ready if:
6264 // - the mixer was not ready during previous round OR
6265 // - no other track is not ready
6266 if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
6267 mixerStatus != MIXER_TRACKS_ENABLED) {
6268 mixerStatus = MIXER_TRACKS_READY;
6269 }
6270
6271 // Enable the next few lines to instrument a test for underrun log handling.
6272 // TODO: Remove when we have a better way of testing the underrun log.
6273 #if 0
6274 static int i;
6275 if ((++i & 0xf) == 0) {
6276 deferredOperations.tallyUnderrunFrames(track, 10 /* underrunFrames */);
6277 }
6278 #endif
6279 } else {
6280 size_t underrunFrames = 0;
6281 if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) {
6282 ALOGV("track(%d) underrun, track state %s framesReady(%zu) < framesDesired(%zd)",
6283 trackId, track->getTrackStateAsString(), framesReady, desiredFrames);
6284 underrunFrames = desiredFrames;
6285 }
6286 deferredOperations.tallyUnderrunFrames(track, underrunFrames);
6287
6288 // clear effect chain input buffer if an active track underruns to avoid sending
6289 // previous audio buffer again to effects
6290 chain = getEffectChain_l(track->sessionId());
6291 if (chain != 0) {
6292 chain->clearInputBuffer();
6293 }
6294
6295 ALOGVV("track(%d) s=%08x [NOT READY] on thread %p", trackId, cblk->mServer, this);
6296 if ((track->sharedBuffer() != 0) || track->isTerminated() ||
6297 track->isStopped() || track->isPaused()) {
6298 // We have consumed all the buffers of this track.
6299 // Remove it from the list of active tracks.
6300 // TODO: use actual buffer filling status instead of latency when available from
6301 // audio HAL
6302 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000;
6303 int64_t framesWritten = mBytesWritten / mFrameSize;
6304 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) {
6305 if (track->isStopped()) {
6306 track->reset();
6307 }
6308 tracksToRemove->add(track);
6309 }
6310 } else {
6311 // No buffers for this track. Give it a few chances to
6312 // fill a buffer, then remove it from active list.
6313 if (--(track->retryCount()) <= 0) {
6314 ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to underrun"
6315 " on thread %d", __func__, trackId, mId);
6316 tracksToRemove->add(track);
6317 // indicate to client process that the track was disabled because of underrun;
6318 // it will then automatically call start() when data is available
6319 track->disable();
6320 // If one track is not ready, mark the mixer also not ready if:
6321 // - the mixer was ready during previous round OR
6322 // - no other track is ready
6323 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
6324 mixerStatus != MIXER_TRACKS_READY) {
6325 mixerStatus = MIXER_TRACKS_ENABLED;
6326 }
6327 }
6328 mAudioMixer->disable(trackId);
6329 }
6330
6331 } // local variable scope to avoid goto warning
6332
6333 }
6334
6335 if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) {
6336 // When there is no fast track playing haptic and FastMixer exists,
6337 // enabling the first FastTrack, which provides mixed data from normal
6338 // tracks, to play haptic data.
6339 FastTrack *fastTrack = &state->mFastTracks[0];
6340 if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) {
6341 fastTrack->mHapticPlaybackEnabled = noFastHapticTrack;
6342 didModify = true;
6343 }
6344 }
6345
6346 // Push the new FastMixer state if necessary
6347 [[maybe_unused]] bool pauseAudioWatchdog = false;
6348 if (didModify) {
6349 state->mFastTracksGen++;
6350 // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
6351 if (kUseFastMixer == FastMixer_Dynamic &&
6352 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
6353 state->mCommand = FastMixerState::COLD_IDLE;
6354 state->mColdFutexAddr = &mFastMixerFutex;
6355 state->mColdGen++;
6356 mFastMixerFutex = 0;
6357 if (kUseFastMixer == FastMixer_Dynamic) {
6358 mNormalSink = mOutputSink;
6359 }
6360 // If we go into cold idle, need to wait for acknowledgement
6361 // so that fast mixer stops doing I/O.
6362 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
6363 pauseAudioWatchdog = true;
6364 }
6365 }
6366 if (sq != NULL) {
6367 sq->end(didModify);
6368 // No need to block if the FastMixer is in COLD_IDLE as the FastThread
6369 // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE
6370 // when bringing the output sink into standby.)
6371 //
6372 // We will get the latest FastMixer state when we come out of COLD_IDLE.
6373 //
6374 // This occurs with BT suspend when we idle the FastMixer with
6375 // active tracks, which may be added or removed.
6376 {
6377 audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid());
6378 sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block);
6379 }
6380 }
6381 #ifdef AUDIO_WATCHDOG
6382 if (pauseAudioWatchdog && mAudioWatchdog != 0) {
6383 mAudioWatchdog->pause();
6384 }
6385 #endif
6386
6387 // Now perform the deferred reset on fast tracks that have stopped
6388 while (resetMask != 0) {
6389 size_t i = __builtin_ctz(resetMask);
6390 ALOG_ASSERT(i < count);
6391 resetMask &= ~(1 << i);
6392 sp<IAfTrack> track = mActiveTracks[i];
6393 ALOG_ASSERT(track->isFastTrack() && track->isStopped());
6394 track->reset();
6395 }
6396
6397 // Track destruction may occur outside of threadLoop once it is removed from active tracks.
6398 // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if
6399 // it ceases to be active, to allow safe removal from the AudioMixer at the start
6400 // of prepareTracks_l(); this releases any outstanding buffer back to the track.
6401 // See also the implementation of destroyTrack_l().
6402 for (const auto &track : *tracksToRemove) {
6403 const int trackId = track->id();
6404 if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer.
6405 mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */);
6406 }
6407 }
6408
6409 // remove all the tracks that need to be...
6410 removeTracks_l(*tracksToRemove);
6411
6412 if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0 ||
6413 getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE) != 0) {
6414 mEffectBufferValid = true;
6415 }
6416
6417 if (mEffectBufferValid) {
6418 // as long as there are effects we should clear the effects buffer, to avoid
6419 // passing a non-clean buffer to the effect chain
6420 memset(mEffectBuffer, 0, mEffectBufferSize);
6421 if (mType == SPATIALIZER) {
6422 memset(mPostSpatializerBuffer, 0, mPostSpatializerBufferSize);
6423 }
6424 }
6425 // sink or mix buffer must be cleared if all tracks are connected to an
6426 // effect chain as in this case the mixer will not write to the sink or mix buffer
6427 // and track effects will accumulate into it
6428 // always clear sink buffer for spatializer output as the output of the spatializer
6429 // effect will be accumulated into it
6430 if ((mBytesRemaining == 0) && (((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
6431 (mixedTracks == 0 && fastTracks > 0)) || (mType == SPATIALIZER))) {
6432 // FIXME as a performance optimization, should remember previous zero status
6433 if (mMixerBufferValid) {
6434 memset(mMixerBuffer, 0, mMixerBufferSize);
6435 // TODO: In testing, mSinkBuffer below need not be cleared because
6436 // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer
6437 // after mixing.
6438 //
6439 // To enforce this guarantee:
6440 // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
6441 // (mixedTracks == 0 && fastTracks > 0))
6442 // must imply MIXER_TRACKS_READY.
6443 // Later, we may clear buffers regardless, and skip much of this logic.
6444 }
6445 // FIXME as a performance optimization, should remember previous zero status
6446 memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize);
6447 }
6448
6449 // if any fast tracks, then status is ready
6450 mMixerStatusIgnoringFastTracks = mixerStatus;
6451 if (fastTracks > 0) {
6452 mixerStatus = MIXER_TRACKS_READY;
6453 }
6454 return mixerStatus;
6455 }
6456
6457 // trackCountForUid_l() must be called with ThreadBase::mutex() held
trackCountForUid_l(uid_t uid) const6458 uint32_t PlaybackThread::trackCountForUid_l(uid_t uid) const
6459 {
6460 uint32_t trackCount = 0;
6461 for (size_t i = 0; i < mTracks.size() ; i++) {
6462 if (mTracks[i]->uid() == uid) {
6463 trackCount++;
6464 }
6465 }
6466 return trackCount;
6467 }
6468
check(AudioStreamOut * output)6469 bool PlaybackThread::IsTimestampAdvancing::check(AudioStreamOut* output)
6470 {
6471 // Check the timestamp to see if it's advancing once every 150ms. If we check too frequently, we
6472 // could falsely detect that the frame position has stalled due to underrun because we haven't
6473 // given the Audio HAL enough time to update.
6474 const nsecs_t nowNs = systemTime();
6475 if (nowNs - mPreviousNs < mMinimumTimeBetweenChecksNs) {
6476 return mLatchedValue;
6477 }
6478 mPreviousNs = nowNs;
6479 mLatchedValue = false;
6480 // Determine if the presentation position is still advancing.
6481 uint64_t position = 0;
6482 struct timespec unused;
6483 const status_t ret = output->getPresentationPosition(&position, &unused);
6484 if (ret == NO_ERROR) {
6485 if (position != mPreviousPosition) {
6486 mPreviousPosition = position;
6487 mLatchedValue = true;
6488 }
6489 }
6490 return mLatchedValue;
6491 }
6492
clear()6493 void PlaybackThread::IsTimestampAdvancing::clear()
6494 {
6495 mLatchedValue = true;
6496 mPreviousPosition = 0;
6497 mPreviousNs = 0;
6498 }
6499
6500 // isTrackAllowed_l() must be called with ThreadBase::mutex() held
isTrackAllowed_l(audio_channel_mask_t channelMask,audio_format_t format,audio_session_t sessionId,uid_t uid) const6501 bool MixerThread::isTrackAllowed_l(
6502 audio_channel_mask_t channelMask, audio_format_t format,
6503 audio_session_t sessionId, uid_t uid) const
6504 {
6505 if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) {
6506 return false;
6507 }
6508 // Check validity as we don't call AudioMixer::create() here.
6509 if (!mAudioMixer->isValidFormat(format)) {
6510 ALOGW("%s: invalid format: %#x", __func__, format);
6511 return false;
6512 }
6513 if (!mAudioMixer->isValidChannelMask(channelMask)) {
6514 ALOGW("%s: invalid channelMask: %#x", __func__, channelMask);
6515 return false;
6516 }
6517 return true;
6518 }
6519
6520 // checkForNewParameter_l() must be called with ThreadBase::mutex() held
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)6521 bool MixerThread::checkForNewParameter_l(const String8& keyValuePair,
6522 status_t& status)
6523 {
6524 bool reconfig = false;
6525 status = NO_ERROR;
6526
6527 AutoPark<FastMixer> park(mFastMixer);
6528
6529 AudioParameter param = AudioParameter(keyValuePair);
6530 int value;
6531 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
6532 reconfig = true;
6533 }
6534 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
6535 if (!isValidPcmSinkFormat(static_cast<audio_format_t>(value))) {
6536 status = BAD_VALUE;
6537 } else {
6538 // no need to save value, since it's constant
6539 reconfig = true;
6540 }
6541 }
6542 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
6543 if (!isValidPcmSinkChannelMask(static_cast<audio_channel_mask_t>(value))) {
6544 status = BAD_VALUE;
6545 } else {
6546 // no need to save value, since it's constant
6547 reconfig = true;
6548 }
6549 }
6550 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
6551 // do not accept frame count changes if tracks are open as the track buffer
6552 // size depends on frame count and correct behavior would not be guaranteed
6553 // if frame count is changed after track creation
6554 if (!mTracks.isEmpty()) {
6555 status = INVALID_OPERATION;
6556 } else {
6557 reconfig = true;
6558 }
6559 }
6560 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
6561 LOG_FATAL("Should not set routing device in MixerThread");
6562 }
6563
6564 if (status == NO_ERROR) {
6565 status = mOutput->stream->setParameters(keyValuePair);
6566 if (!mStandby && status == INVALID_OPERATION) {
6567 ALOGW("%s: setParameters failed with keyValuePair %s, entering standby",
6568 __func__, keyValuePair.c_str());
6569 mOutput->standby();
6570 mThreadMetrics.logEndInterval();
6571 mThreadSnapshot.onEnd();
6572 setStandby_l();
6573 mBytesWritten = 0;
6574 status = mOutput->stream->setParameters(keyValuePair);
6575 }
6576 if (status == NO_ERROR && reconfig) {
6577 readOutputParameters_l();
6578 delete mAudioMixer;
6579 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
6580 for (const auto &track : mTracks) {
6581 const int trackId = track->id();
6582 const status_t createStatus = mAudioMixer->create(
6583 trackId,
6584 track->channelMask(),
6585 track->format(),
6586 track->sessionId());
6587 ALOGW_IF(createStatus != NO_ERROR,
6588 "%s(): AudioMixer cannot create track(%d)"
6589 " mask %#x, format %#x, sessionId %d",
6590 __func__,
6591 trackId, track->channelMask(), track->format(), track->sessionId());
6592 }
6593 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
6594 }
6595 }
6596
6597 return reconfig;
6598 }
6599
6600
dumpInternals_l(int fd,const Vector<String16> & args)6601 void MixerThread::dumpInternals_l(int fd, const Vector<String16>& args)
6602 {
6603 PlaybackThread::dumpInternals_l(fd, args);
6604 dprintf(fd, " Thread throttle time (msecs): %u\n", (uint32_t)mThreadThrottleTimeMs);
6605 dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str());
6606 dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off");
6607 dprintf(fd, " Master balance: %f (%s)\n", mMasterBalance.load(),
6608 (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance())
6609 : mBalance.toString()).c_str());
6610 if (hasFastMixer()) {
6611 dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid());
6612
6613 // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
6614 // while we are dumping it. It may be inconsistent, but it won't mutate!
6615 // This is a large object so we place it on the heap.
6616 // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
6617 const std::unique_ptr<FastMixerDumpState> copy =
6618 std::make_unique<FastMixerDumpState>(mFastMixerDumpState);
6619 copy->dump(fd);
6620
6621 #ifdef STATE_QUEUE_DUMP
6622 // Similar for state queue
6623 StateQueueObserverDump observerCopy = mStateQueueObserverDump;
6624 observerCopy.dump(fd);
6625 StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
6626 mutatorCopy.dump(fd);
6627 #endif
6628
6629 #ifdef AUDIO_WATCHDOG
6630 if (mAudioWatchdog != 0) {
6631 // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
6632 AudioWatchdogDump wdCopy = mAudioWatchdogDump;
6633 wdCopy.dump(fd);
6634 }
6635 #endif
6636
6637 } else {
6638 dprintf(fd, " No FastMixer\n");
6639 }
6640
6641 dprintf(fd, "Bluetooth latency modes are %senabled\n",
6642 mBluetoothLatencyModesEnabled ? "" : "not ");
6643 dprintf(fd, "HAL does %ssupport Bluetooth latency modes\n", mOutput != nullptr &&
6644 mOutput->audioHwDev->supportsBluetoothVariableLatency() ? "" : "not ");
6645 dprintf(fd, "Supported latency modes: %s\n", toString(mSupportedLatencyModes).c_str());
6646 }
6647
idleSleepTimeUs() const6648 uint32_t MixerThread::idleSleepTimeUs() const
6649 {
6650 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
6651 }
6652
suspendSleepTimeUs() const6653 uint32_t MixerThread::suspendSleepTimeUs() const
6654 {
6655 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
6656 }
6657
cacheParameters_l()6658 void MixerThread::cacheParameters_l()
6659 {
6660 PlaybackThread::cacheParameters_l();
6661
6662 // FIXME: Relaxed timing because of a certain device that can't meet latency
6663 // Should be reduced to 2x after the vendor fixes the driver issue
6664 // increase threshold again due to low power audio mode. The way this warning
6665 // threshold is calculated and its usefulness should be reconsidered anyway.
6666 maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
6667 }
6668
onHalLatencyModesChanged_l()6669 void MixerThread::onHalLatencyModesChanged_l() {
6670 mAfThreadCallback->onSupportedLatencyModesChanged(mId, mSupportedLatencyModes);
6671 }
6672
setHalLatencyMode_l()6673 void MixerThread::setHalLatencyMode_l() {
6674 // Only handle latency mode if:
6675 // - mBluetoothLatencyModesEnabled is true
6676 // - the HAL supports latency modes
6677 // - the selected device is Bluetooth LE or A2DP
6678 if (!mBluetoothLatencyModesEnabled.load() || mSupportedLatencyModes.empty()) {
6679 return;
6680 }
6681 if (mOutDeviceTypeAddrs.size() != 1
6682 || !(audio_is_a2dp_out_device(mOutDeviceTypeAddrs[0].mType)
6683 || audio_is_ble_out_device(mOutDeviceTypeAddrs[0].mType))) {
6684 return;
6685 }
6686
6687 audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE;
6688 if (mSupportedLatencyModes.size() == 1) {
6689 // If the HAL only support one latency mode currently, confirm the choice
6690 latencyMode = mSupportedLatencyModes[0];
6691 } else if (mSupportedLatencyModes.size() > 1) {
6692 // Request low latency if:
6693 // - At least one active track is either:
6694 // - a fast track with gaming usage or
6695 // - a track with acessibility usage
6696 for (const auto& track : mActiveTracks) {
6697 if ((track->isFastTrack() && track->attributes().usage == AUDIO_USAGE_GAME)
6698 || track->attributes().usage == AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) {
6699 latencyMode = AUDIO_LATENCY_MODE_LOW;
6700 break;
6701 }
6702 }
6703 }
6704
6705 if (latencyMode != mSetLatencyMode) {
6706 status_t status = mOutput->stream->setLatencyMode(latencyMode);
6707 ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d",
6708 __func__, mId, toString(latencyMode).c_str(), status);
6709 if (status == NO_ERROR) {
6710 mSetLatencyMode = latencyMode;
6711 }
6712 }
6713 }
6714
updateHalSupportedLatencyModes_l()6715 void MixerThread::updateHalSupportedLatencyModes_l() {
6716
6717 if (mOutput == nullptr || mOutput->stream == nullptr) {
6718 return;
6719 }
6720 std::vector<audio_latency_mode_t> latencyModes;
6721 const status_t status = mOutput->stream->getRecommendedLatencyModes(&latencyModes);
6722 if (status != NO_ERROR) {
6723 latencyModes.clear();
6724 }
6725 if (latencyModes != mSupportedLatencyModes) {
6726 ALOGD("%s: thread(%d) status %d supported latency modes: %s",
6727 __func__, mId, status, toString(latencyModes).c_str());
6728 mSupportedLatencyModes.swap(latencyModes);
6729 sendHalLatencyModesChangedEvent_l();
6730 }
6731 }
6732
getSupportedLatencyModes(std::vector<audio_latency_mode_t> * modes)6733 status_t MixerThread::getSupportedLatencyModes(
6734 std::vector<audio_latency_mode_t>* modes) {
6735 if (modes == nullptr) {
6736 return BAD_VALUE;
6737 }
6738 audio_utils::lock_guard _l(mutex());
6739 *modes = mSupportedLatencyModes;
6740 return NO_ERROR;
6741 }
6742
onRecommendedLatencyModeChanged(std::vector<audio_latency_mode_t> modes)6743 void MixerThread::onRecommendedLatencyModeChanged(
6744 std::vector<audio_latency_mode_t> modes) {
6745 audio_utils::lock_guard _l(mutex());
6746 if (modes != mSupportedLatencyModes) {
6747 ALOGD("%s: thread(%d) supported latency modes: %s",
6748 __func__, mId, toString(modes).c_str());
6749 mSupportedLatencyModes.swap(modes);
6750 sendHalLatencyModesChangedEvent_l();
6751 }
6752 }
6753
setBluetoothVariableLatencyEnabled(bool enabled)6754 status_t MixerThread::setBluetoothVariableLatencyEnabled(bool enabled) {
6755 if (mOutput == nullptr || mOutput->audioHwDev == nullptr
6756 || !mOutput->audioHwDev->supportsBluetoothVariableLatency()) {
6757 return INVALID_OPERATION;
6758 }
6759 mBluetoothLatencyModesEnabled.store(enabled);
6760 return NO_ERROR;
6761 }
6762
6763 // ----------------------------------------------------------------------------
6764
6765 /* static */
createDirectOutputThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,const audio_offload_info_t & offloadInfo)6766 sp<IAfPlaybackThread> IAfPlaybackThread::createDirectOutputThread(
6767 const sp<IAfThreadCallback>& afThreadCallback,
6768 AudioStreamOut* output, audio_io_handle_t id, bool systemReady,
6769 const audio_offload_info_t& offloadInfo) {
6770 return sp<DirectOutputThread>::make(
6771 afThreadCallback, output, id, systemReady, offloadInfo);
6772 }
6773
DirectOutputThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,ThreadBase::type_t type,bool systemReady,const audio_offload_info_t & offloadInfo)6774 DirectOutputThread::DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback,
6775 AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady,
6776 const audio_offload_info_t& offloadInfo)
6777 : PlaybackThread(afThreadCallback, output, id, type, systemReady)
6778 , mOffloadInfo(offloadInfo)
6779 {
6780 setMasterBalance(afThreadCallback->getMasterBalance_l());
6781 }
6782
~DirectOutputThread()6783 DirectOutputThread::~DirectOutputThread()
6784 {
6785 }
6786
dumpInternals_l(int fd,const Vector<String16> & args)6787 void DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args)
6788 {
6789 PlaybackThread::dumpInternals_l(fd, args);
6790 dprintf(fd, " Master balance: %f Left: %f Right: %f\n",
6791 mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight);
6792 }
6793
setMasterBalance(float balance)6794 void DirectOutputThread::setMasterBalance(float balance)
6795 {
6796 audio_utils::lock_guard _l(mutex());
6797 if (mMasterBalance != balance) {
6798 mMasterBalance.store(balance);
6799 mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight);
6800 broadcast_l();
6801 }
6802 }
6803
processVolume_l(IAfTrack * track,bool lastTrack)6804 void DirectOutputThread::processVolume_l(IAfTrack* track, bool lastTrack)
6805 {
6806 float left, right;
6807
6808 // Ensure volumeshaper state always advances even when muted.
6809 const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy();
6810
6811 const int64_t frames = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
6812 const int64_t time = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
6813
6814 ALOGVV("%s: Direct/Offload bufferConsumed:%zu timestamp frames:%lld time:%lld",
6815 __func__, proxy->framesReleased(), (long long)frames, (long long)time);
6816
6817 const int64_t volumeShaperFrames =
6818 mMonotonicFrameCounter.updateAndGetMonotonicFrameCount(frames, time);
6819 const auto [shaperVolume, shaperActive] =
6820 track->getVolumeHandler()->getVolume(volumeShaperFrames);
6821 mVolumeShaperActive = shaperActive;
6822
6823 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
6824 left = float_from_gain(gain_minifloat_unpack_left(vlr));
6825 right = float_from_gain(gain_minifloat_unpack_right(vlr));
6826
6827 const bool clientVolumeMute = (left == 0.f && right == 0.f);
6828
6829 const auto amn = mAfThreadCallback->getAudioManagerNative();
6830 if (!audioserver_flags::portid_volume_management()) {
6831 if (mMasterMute || mStreamTypes[track->streamType()].mute ||
6832 track->isPlaybackRestricted()) {
6833 left = right = 0;
6834 } else {
6835 float typeVolume = mStreamTypes[track->streamType()].volume;
6836 const float v = mMasterVolume * typeVolume * shaperVolume;
6837
6838 if (left > GAIN_FLOAT_UNITY) {
6839 left = GAIN_FLOAT_UNITY;
6840 }
6841 if (right > GAIN_FLOAT_UNITY) {
6842 right = GAIN_FLOAT_UNITY;
6843 }
6844 left *= v;
6845 right *= v;
6846 if (mAfThreadCallback->getMode() != AUDIO_MODE_IN_COMMUNICATION
6847 || audio_channel_count_from_out_mask(mChannelMask) > 1) {
6848 left *= mMasterBalanceLeft; // DirectOutputThread balance applied as track volume
6849 right *= mMasterBalanceRight;
6850 }
6851 }
6852 if (amn) {
6853 track->processMuteEvent(*amn,
6854 /*muteState=*/{mMasterMute,
6855 mStreamTypes[track->streamType()].volume == 0.f,
6856 mStreamTypes[track->streamType()].mute,
6857 track->isPlaybackRestrictedOp(),
6858 clientVolumeMute,
6859 shaperVolume == 0.f,
6860 /*muteFromPortVolume=*/false,
6861 track->isPlaybackRestrictedControl()});
6862 }
6863 } else {
6864 if (mMasterMute || track->isPlaybackRestricted()) {
6865 left = right = 0;
6866 } else {
6867 float typeVolume = track->getPortVolume();
6868 const float v = mMasterVolume * typeVolume * shaperVolume;
6869
6870 if (left > GAIN_FLOAT_UNITY) {
6871 left = GAIN_FLOAT_UNITY;
6872 }
6873 if (right > GAIN_FLOAT_UNITY) {
6874 right = GAIN_FLOAT_UNITY;
6875 }
6876 left *= v;
6877 right *= v;
6878 if (mAfThreadCallback->getMode() != AUDIO_MODE_IN_COMMUNICATION
6879 || audio_channel_count_from_out_mask(mChannelMask) > 1) {
6880 left *= mMasterBalanceLeft; // DirectOutputThread balance applied as track volume
6881 right *= mMasterBalanceRight;
6882 }
6883 }
6884 if (amn) {
6885 track->processMuteEvent(*amn,
6886 /*muteState=*/{mMasterMute,
6887 track->getPortVolume() == 0.f,
6888 /* muteFromStreamMuted= */ false,
6889 track->isPlaybackRestrictedOp(),
6890 clientVolumeMute,
6891 shaperVolume == 0.f,
6892 track->getPortMute(),
6893 track->isPlaybackRestrictedControl()});
6894 }
6895 }
6896 if (amn) {
6897 track->maybeLogPlaybackHardening(*amn);
6898 }
6899 if (lastTrack) {
6900 track->setFinalVolume(left, right);
6901 if (left != mLeftVolFloat || right != mRightVolFloat) {
6902 mLeftVolFloat = left;
6903 mRightVolFloat = right;
6904
6905 // Delegate volume control to effect in track effect chain if needed
6906 // only one effect chain can be present on DirectOutputThread, so if
6907 // there is one, the track is connected to it
6908 if (!mEffectChains.isEmpty()) {
6909 // if effect chain exists, volume is handled by it.
6910 // Convert volumes from float to 8.24
6911 uint32_t vl = (uint32_t)(left * (1 << 24));
6912 uint32_t vr = (uint32_t)(right * (1 << 24));
6913 // Direct/Offload effect chains set output volume in setVolume().
6914 (void)mEffectChains[0]->setVolume(&vl, &vr);
6915 } else {
6916 // otherwise we directly set the volume.
6917 setVolumeForOutput_l(left, right);
6918 }
6919 }
6920 }
6921 }
6922
onAddNewTrack_l()6923 void DirectOutputThread::onAddNewTrack_l()
6924 {
6925 sp<IAfTrack> previousTrack = mPreviousTrack.promote();
6926 sp<IAfTrack> latestTrack = mActiveTracks.getLatest();
6927
6928 if (previousTrack != 0 && latestTrack != 0) {
6929 if (mType == DIRECT) {
6930 if (previousTrack.get() != latestTrack.get()) {
6931 mFlushPending = true;
6932 }
6933 } else /* mType == OFFLOAD */ {
6934 if (previousTrack->sessionId() != latestTrack->sessionId() ||
6935 previousTrack->isFlushPending()) {
6936 mFlushPending = true;
6937 }
6938 }
6939 } else if (previousTrack == 0) {
6940 // there could be an old track added back during track transition for direct
6941 // output, so always issues flush to flush data of the previous track if it
6942 // was already destroyed with HAL paused, then flush can resume the playback
6943 mFlushPending = true;
6944 }
6945 PlaybackThread::onAddNewTrack_l();
6946 }
6947
prepareTracks_l(Vector<sp<IAfTrack>> * tracksToRemove)6948 PlaybackThread::mixer_state DirectOutputThread::prepareTracks_l(
6949 Vector<sp<IAfTrack>>* tracksToRemove
6950 )
6951 {
6952 size_t count = mActiveTracks.size();
6953 mixer_state mixerStatus = MIXER_IDLE;
6954 bool doHwPause = false;
6955 bool doHwResume = false;
6956
6957 // find out which tracks need to be processed
6958 for (const sp<IAfTrack>& t : mActiveTracks) {
6959 if (t->isInvalid()) {
6960 ALOGW("An invalidated track shouldn't be in active list");
6961 tracksToRemove->add(t);
6962 continue;
6963 }
6964
6965 IAfTrack* const track = t.get();
6966 #ifdef VERY_VERY_VERBOSE_LOGGING
6967 audio_track_cblk_t* cblk = track->cblk();
6968 #endif
6969 // Only consider last track started for volume and mixer state control.
6970 // In theory an older track could underrun and restart after the new one starts
6971 // but as we only care about the transition phase between two tracks on a
6972 // direct output, it is not a problem to ignore the underrun case.
6973 sp<IAfTrack> l = mActiveTracks.getLatest();
6974 bool last = l.get() == track;
6975
6976 if (track->isPausePending()) {
6977 track->pauseAck();
6978 // It is possible a track might have been flushed or stopped.
6979 // Other operations such as flush pending might occur on the next prepare.
6980 if (track->isPausing()) {
6981 track->setPaused();
6982 }
6983 // Always perform pause, as an immediate flush will change
6984 // the pause state to be no longer isPausing().
6985 if (mHwSupportsPause && last && !mHwPaused) {
6986 doHwPause = true;
6987 mHwPaused = true;
6988 }
6989 } else if (track->isFlushPending()) {
6990 track->flushAck();
6991 if (last) {
6992 mFlushPending = true;
6993 }
6994 } else if (track->isResumePending()) {
6995 track->resumeAck();
6996 if (last) {
6997 mLeftVolFloat = mRightVolFloat = -1.0;
6998 if (mHwPaused) {
6999 doHwResume = true;
7000 mHwPaused = false;
7001 }
7002 }
7003 }
7004
7005 // The first time a track is added we wait
7006 // for all its buffers to be filled before processing it.
7007 // Allow draining the buffer in case the client
7008 // app does not call stop() and relies on underrun to stop:
7009 // hence the test on (track->retryCount() > 1).
7010 // If track->retryCount() <= 1 then track is about to be disabled, paused, removed,
7011 // so we accept any nonzero amount of data delivered by the AudioTrack (which will
7012 // reset the retry counter).
7013 // Do not use a high threshold for compressed audio.
7014
7015 // target retry count that we will use is based on the time we wait for retries.
7016 const int32_t targetRetryCount = kMaxTrackRetriesDirectMs * 1000 / mActiveSleepTimeUs;
7017 // the retry threshold is when we accept any size for PCM data. This is slightly
7018 // smaller than the retry count so we can push small bits of data without a glitch.
7019 const int32_t retryThreshold = targetRetryCount > 2 ? targetRetryCount - 1 : 1;
7020 uint32_t minFrames;
7021 if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()
7022 && (track->retryCount() > retryThreshold) && audio_has_proportional_frames(mFormat)) {
7023 minFrames = mNormalFrameCount;
7024 } else {
7025 minFrames = 1;
7026 }
7027
7028 const size_t framesReady = track->framesReady();
7029 const int trackId = track->id();
7030 if (ATRACE_ENABLED()) [[unlikely]] {
7031 ATRACE_INT(std::string(AUDIO_TRACE_PREFIX_AUDIO_TRACK_NRDY)
7032 .append(track->getTraceSuffix()).c_str(), framesReady);
7033 }
7034 if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() &&
7035 !track->isStopping_2() && !track->isStopped())
7036 {
7037 ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer);
7038
7039 if (track->fillingStatus() == IAfTrack::FS_FILLED) {
7040 track->fillingStatus() = IAfTrack::FS_ACTIVE;
7041 if (last) {
7042 // make sure processVolume_l() will apply new volume even if 0
7043 mLeftVolFloat = mRightVolFloat = -1.0;
7044 }
7045 if (!mHwSupportsPause) {
7046 track->resumeAck();
7047 }
7048 }
7049
7050 // compute volume for this track
7051 processVolume_l(track, last);
7052 if (last) {
7053 sp<IAfTrack> previousTrack = mPreviousTrack.promote();
7054 if (previousTrack != 0) {
7055 if (track != previousTrack.get()) {
7056 // Flush any data still being written from last track
7057 mBytesRemaining = 0;
7058 // Invalidate previous track to force a seek when resuming.
7059 previousTrack->invalidate();
7060 }
7061 }
7062 mPreviousTrack = track;
7063
7064 // reset retry count
7065 track->retryCount() = targetRetryCount;
7066 mActiveTrack = t;
7067 mixerStatus = MIXER_TRACKS_READY;
7068 if (mHwPaused) {
7069 doHwResume = true;
7070 mHwPaused = false;
7071 }
7072 }
7073 } else {
7074 // clear effect chain input buffer if the last active track started underruns
7075 // to avoid sending previous audio buffer again to effects
7076 if (!mEffectChains.isEmpty() && last) {
7077 mEffectChains[0]->clearInputBuffer();
7078 }
7079 if (track->isStopping_1()) {
7080 track->setState(IAfTrackBase::STOPPING_2);
7081 if (last && mHwPaused) {
7082 doHwResume = true;
7083 mHwPaused = false;
7084 }
7085 }
7086 if ((track->sharedBuffer() != 0) || track->isStopped() ||
7087 track->isStopping_2() || track->isPaused()) {
7088 // We have consumed all the buffers of this track.
7089 // Remove it from the list of active tracks.
7090 bool presComplete = false;
7091 if (mStandby || !last ||
7092 (presComplete = track->presentationComplete(latency_l())) ||
7093 track->isPaused() || mHwPaused) {
7094 if (presComplete) {
7095 mOutput->presentationComplete();
7096 }
7097 if (track->isStopping_2()) {
7098 track->setState(IAfTrackBase::STOPPED);
7099 }
7100 if (track->isStopped()) {
7101 track->reset();
7102 }
7103 tracksToRemove->add(track);
7104 }
7105 } else {
7106 // No buffers for this track. Give it a few chances to
7107 // fill a buffer, then remove it from active list.
7108 // Only consider last track started for mixer state control
7109 bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput);
7110 if (!isTunerStream() // tuner streams remain active in underrun
7111 && --(track->retryCount()) <= 0) {
7112 if (isTimestampAdvancing) { // HAL is still playing audio, give us more time.
7113 track->retryCount() = kMaxTrackRetriesOffload;
7114 } else {
7115 ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to"
7116 " underrun on thread %d", __func__, trackId, mId);
7117 tracksToRemove->add(track);
7118 // indicate to client process that the track was disabled because of
7119 // underrun; it will then automatically call start() when data is available
7120 track->disable();
7121 // only do hw pause when track is going to be removed due to BUFFER TIMEOUT.
7122 // unlike mixerthread, HAL can be paused for direct output
7123 ALOGW("pause because of UNDERRUN, framesReady = %zu,"
7124 "minFrames = %u, mFormat = %#x",
7125 framesReady, minFrames, mFormat);
7126 if (last && mHwSupportsPause && !mHwPaused && !mStandby) {
7127 doHwPause = true;
7128 mHwPaused = true;
7129 }
7130 }
7131 } else if (last) {
7132 mixerStatus = MIXER_TRACKS_ENABLED;
7133 }
7134 }
7135 }
7136 }
7137
7138 // if an active track did not command a flush, check for pending flush on stopped tracks
7139 if (!mFlushPending) {
7140 for (size_t i = 0; i < mTracks.size(); i++) {
7141 if (mTracks[i]->isFlushPending()) {
7142 mTracks[i]->flushAck();
7143 mFlushPending = true;
7144 }
7145 }
7146 }
7147
7148 // make sure the pause/flush/resume sequence is executed in the right order.
7149 // If a flush is pending and a track is active but the HW is not paused, force a HW pause
7150 // before flush and then resume HW. This can happen in case of pause/flush/resume
7151 // if resume is received before pause is executed.
7152 if (mHwSupportsPause && !mStandby &&
7153 (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
7154 status_t result = mOutput->stream->pause();
7155 ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
7156 doHwResume = !doHwPause; // resume if pause is due to flush.
7157 }
7158 if (mFlushPending) {
7159 flushHw_l();
7160 }
7161 if (mHwSupportsPause && !mStandby && doHwResume) {
7162 status_t result = mOutput->stream->resume();
7163 ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
7164 }
7165 // remove all the tracks that need to be...
7166 removeTracks_l(*tracksToRemove);
7167
7168 return mixerStatus;
7169 }
7170
threadLoop_mix()7171 void DirectOutputThread::threadLoop_mix()
7172 {
7173 size_t frameCount = mFrameCount;
7174 int8_t *curBuf = (int8_t *)mSinkBuffer;
7175 // output audio to hardware
7176 while (frameCount) {
7177 AudioBufferProvider::Buffer buffer;
7178 buffer.frameCount = frameCount;
7179 status_t status = mActiveTrack->getNextBuffer(&buffer);
7180 if (status != NO_ERROR || buffer.raw == NULL) {
7181 // no need to pad with 0 for compressed audio
7182 if (audio_has_proportional_frames(mFormat)) {
7183 memset(curBuf, 0, frameCount * mFrameSize);
7184 }
7185 break;
7186 }
7187 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
7188 frameCount -= buffer.frameCount;
7189 curBuf += buffer.frameCount * mFrameSize;
7190 mActiveTrack->releaseBuffer(&buffer);
7191 }
7192 mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer;
7193 mSleepTimeUs = 0;
7194 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
7195 mActiveTrack.clear();
7196 }
7197
threadLoop_sleepTime()7198 void DirectOutputThread::threadLoop_sleepTime()
7199 {
7200 // do not write to HAL when paused
7201 if (mHwPaused || (usesHwAvSync() && mStandby)) {
7202 mSleepTimeUs = mIdleSleepTimeUs;
7203 return;
7204 }
7205 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
7206 mSleepTimeUs = mActiveSleepTimeUs;
7207 } else {
7208 mSleepTimeUs = mIdleSleepTimeUs;
7209 }
7210 // Note: In S or later, we do not write zeroes for
7211 // linear or proportional PCM direct tracks in underrun.
7212 }
7213
threadLoop_exit()7214 void DirectOutputThread::threadLoop_exit()
7215 {
7216 {
7217 audio_utils::lock_guard _l(mutex());
7218 for (size_t i = 0; i < mTracks.size(); i++) {
7219 if (mTracks[i]->isFlushPending()) {
7220 mTracks[i]->flushAck();
7221 mFlushPending = true;
7222 }
7223 }
7224 if (mFlushPending) {
7225 flushHw_l();
7226 }
7227 }
7228 PlaybackThread::threadLoop_exit();
7229 }
7230
7231 // must be called with thread mutex locked
shouldStandby_l()7232 bool DirectOutputThread::shouldStandby_l()
7233 {
7234 bool trackPaused = false;
7235 bool trackStopped = false;
7236 bool trackDisabled = false;
7237
7238 // do not put the HAL in standby when paused. NuPlayer clear the offloaded AudioTrack
7239 // after a timeout and we will enter standby then.
7240 // On offload threads, do not enter standby if the main track is still underrunning.
7241 if (mTracks.size() > 0) {
7242 const auto& mainTrack = mTracks[mTracks.size() - 1];
7243
7244 trackPaused = mainTrack->isPaused();
7245 trackStopped = mainTrack->isStopped() || mainTrack->state() == IAfTrackBase::IDLE;
7246 trackDisabled = (mType == OFFLOAD) && mainTrack->isDisabled();
7247 }
7248
7249 return !mStandby && !(trackPaused || (mHwPaused && !trackStopped) || trackDisabled);
7250 }
7251
7252 // checkForNewParameter_l() must be called with ThreadBase::mutex() held
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)7253 bool DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair,
7254 status_t& status)
7255 {
7256 bool reconfig = false;
7257 status = NO_ERROR;
7258
7259 AudioParameter param = AudioParameter(keyValuePair);
7260 int value;
7261 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
7262 LOG_FATAL("Should not set routing device in DirectOutputThread");
7263 }
7264 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
7265 // do not accept frame count changes if tracks are open as the track buffer
7266 // size depends on frame count and correct behavior would not be garantied
7267 // if frame count is changed after track creation
7268 if (!mTracks.isEmpty()) {
7269 status = INVALID_OPERATION;
7270 } else {
7271 reconfig = true;
7272 }
7273 }
7274 if (status == NO_ERROR) {
7275 status = mOutput->stream->setParameters(keyValuePair);
7276 if (!mStandby && status == INVALID_OPERATION) {
7277 mOutput->standby();
7278 if (!mStandby) {
7279 mThreadMetrics.logEndInterval();
7280 mThreadSnapshot.onEnd();
7281 setStandby_l();
7282 }
7283 mBytesWritten = 0;
7284 status = mOutput->stream->setParameters(keyValuePair);
7285 }
7286 if (status == NO_ERROR && reconfig) {
7287 readOutputParameters_l();
7288 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
7289 }
7290 }
7291
7292 return reconfig;
7293 }
7294
activeSleepTimeUs() const7295 uint32_t DirectOutputThread::activeSleepTimeUs() const
7296 {
7297 uint32_t time;
7298 if (audio_has_proportional_frames(mFormat) && mType != OFFLOAD) {
7299 time = PlaybackThread::activeSleepTimeUs();
7300 } else {
7301 time = kDirectMinSleepTimeUs;
7302 }
7303 return time;
7304 }
7305
idleSleepTimeUs() const7306 uint32_t DirectOutputThread::idleSleepTimeUs() const
7307 {
7308 uint32_t time;
7309 if (audio_has_proportional_frames(mFormat) && mType != OFFLOAD) {
7310 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
7311 } else {
7312 time = kDirectMinSleepTimeUs;
7313 }
7314 return time;
7315 }
7316
suspendSleepTimeUs() const7317 uint32_t DirectOutputThread::suspendSleepTimeUs() const
7318 {
7319 uint32_t time;
7320 if (audio_has_proportional_frames(mFormat) && mType != OFFLOAD) {
7321 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
7322 } else {
7323 time = kDirectMinSleepTimeUs;
7324 }
7325 return time;
7326 }
7327
cacheParameters_l()7328 void DirectOutputThread::cacheParameters_l()
7329 {
7330 PlaybackThread::cacheParameters_l();
7331
7332 // use shorter standby delay as on normal output to release
7333 // hardware resources as soon as possible
7334 // no delay on outputs with HW A/V sync
7335 if (usesHwAvSync()) {
7336 mStandbyDelayNs = 0;
7337 } else if (mType == OFFLOAD) {
7338 mStandbyDelayNs = kOffloadStandbyDelayNs;
7339 } else {
7340 mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2);
7341 }
7342 }
7343
flushHw_l()7344 void DirectOutputThread::flushHw_l()
7345 {
7346 PlaybackThread::flushHw_l();
7347 mOutput->flush();
7348 mFlushPending = false;
7349 mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush());
7350 mTimestamp.clear();
7351 mMonotonicFrameCounter.onFlush();
7352 // We do not reset mHwPaused which is hidden from the Track client.
7353 // Note: the client track in Tracks.cpp and AudioTrack.cpp
7354 // has a FLUSHED state but the DirectOutputThread does not;
7355 // those tracks will continue to show isStopped().
7356 }
7357
computeWaitTimeNs_l() const7358 int64_t DirectOutputThread::computeWaitTimeNs_l() const {
7359 // If a VolumeShaper is active, we must wake up periodically to update volume.
7360 const int64_t NS_PER_MS = 1000000;
7361 return mVolumeShaperActive ?
7362 kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l();
7363 }
7364
7365 // ----------------------------------------------------------------------------
7366
AsyncCallbackThread(const wp<PlaybackThread> & playbackThread)7367 AsyncCallbackThread::AsyncCallbackThread(
7368 const wp<PlaybackThread>& playbackThread)
7369 : Thread(false /*canCallJava*/),
7370 mPlaybackThread(playbackThread),
7371 mWriteAckSequence(0),
7372 mDrainSequence(0),
7373 mAsyncError(ASYNC_ERROR_NONE)
7374 {
7375 }
7376
onFirstRef()7377 void AsyncCallbackThread::onFirstRef()
7378 {
7379 run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO);
7380 }
7381
threadLoop()7382 bool AsyncCallbackThread::threadLoop()
7383 {
7384 while (!exitPending()) {
7385 uint32_t writeAckSequence;
7386 uint32_t drainSequence;
7387 AsyncError asyncError;
7388
7389 {
7390 audio_utils::unique_lock _l(mutex());
7391 while (!((mWriteAckSequence & 1) ||
7392 (mDrainSequence & 1) ||
7393 mAsyncError ||
7394 exitPending())) {
7395 mWaitWorkCV.wait(_l);
7396 }
7397
7398 if (exitPending()) {
7399 break;
7400 }
7401 ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d",
7402 mWriteAckSequence, mDrainSequence);
7403 writeAckSequence = mWriteAckSequence;
7404 mWriteAckSequence &= ~1;
7405 drainSequence = mDrainSequence;
7406 mDrainSequence &= ~1;
7407 asyncError = mAsyncError;
7408 mAsyncError = ASYNC_ERROR_NONE;
7409 }
7410 {
7411 const sp<PlaybackThread> playbackThread = mPlaybackThread.promote();
7412 if (playbackThread != 0) {
7413 if (writeAckSequence & 1) {
7414 playbackThread->resetWriteBlocked(writeAckSequence >> 1);
7415 }
7416 if (drainSequence & 1) {
7417 playbackThread->resetDraining(drainSequence >> 1);
7418 }
7419 if (asyncError != ASYNC_ERROR_NONE) {
7420 playbackThread->onAsyncError(asyncError == ASYNC_ERROR_HARD);
7421 }
7422 }
7423 }
7424 }
7425 return false;
7426 }
7427
exit()7428 void AsyncCallbackThread::exit()
7429 {
7430 ALOGV("AsyncCallbackThread::exit");
7431 audio_utils::lock_guard _l(mutex());
7432 requestExit();
7433 mWaitWorkCV.notify_all();
7434 }
7435
setWriteBlocked(uint32_t sequence)7436 void AsyncCallbackThread::setWriteBlocked(uint32_t sequence)
7437 {
7438 audio_utils::lock_guard _l(mutex());
7439 // bit 0 is cleared
7440 mWriteAckSequence = sequence << 1;
7441 }
7442
resetWriteBlocked()7443 void AsyncCallbackThread::resetWriteBlocked()
7444 {
7445 audio_utils::lock_guard _l(mutex());
7446 // ignore unexpected callbacks
7447 if (mWriteAckSequence & 2) {
7448 mWriteAckSequence |= 1;
7449 mWaitWorkCV.notify_one();
7450 }
7451 }
7452
setDraining(uint32_t sequence)7453 void AsyncCallbackThread::setDraining(uint32_t sequence)
7454 {
7455 audio_utils::lock_guard _l(mutex());
7456 // bit 0 is cleared
7457 mDrainSequence = sequence << 1;
7458 }
7459
resetDraining()7460 void AsyncCallbackThread::resetDraining()
7461 {
7462 audio_utils::lock_guard _l(mutex());
7463 // ignore unexpected callbacks
7464 if (mDrainSequence & 2) {
7465 mDrainSequence |= 1;
7466 mWaitWorkCV.notify_one();
7467 }
7468 }
7469
setAsyncError(bool isHardError)7470 void AsyncCallbackThread::setAsyncError(bool isHardError)
7471 {
7472 audio_utils::lock_guard _l(mutex());
7473 mAsyncError = isHardError ? ASYNC_ERROR_HARD : ASYNC_ERROR_SOFT;
7474 mWaitWorkCV.notify_one();
7475 }
7476
7477
7478 // ----------------------------------------------------------------------------
7479
7480 /* static */
createOffloadThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,const audio_offload_info_t & offloadInfo)7481 sp<IAfPlaybackThread> IAfPlaybackThread::createOffloadThread(
7482 const sp<IAfThreadCallback>& afThreadCallback,
7483 AudioStreamOut* output, audio_io_handle_t id, bool systemReady,
7484 const audio_offload_info_t& offloadInfo) {
7485 return sp<OffloadThread>::make(afThreadCallback, output, id, systemReady, offloadInfo);
7486 }
7487
OffloadThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,const audio_offload_info_t & offloadInfo)7488 OffloadThread::OffloadThread(const sp<IAfThreadCallback>& afThreadCallback,
7489 AudioStreamOut* output, audio_io_handle_t id, bool systemReady,
7490 const audio_offload_info_t& offloadInfo)
7491 : DirectOutputThread(afThreadCallback, output, id, OFFLOAD, systemReady, offloadInfo),
7492 mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true)
7493 {
7494 //FIXME: mStandby should be set to true by ThreadBase constructo
7495 mStandby = true;
7496 mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */);
7497 }
7498
threadLoop_exit()7499 void OffloadThread::threadLoop_exit()
7500 {
7501 if (mFlushPending || mHwPaused) {
7502 // If a flush is pending or track was paused, just discard buffered data
7503 audio_utils::lock_guard l(mutex());
7504 flushHw_l();
7505 } else {
7506 mMixerStatus = MIXER_DRAIN_ALL;
7507 threadLoop_drain();
7508 }
7509 if (mUseAsyncWrite) {
7510 ALOG_ASSERT(mCallbackThread != 0);
7511 mCallbackThread->exit();
7512 }
7513 PlaybackThread::threadLoop_exit();
7514 }
7515
prepareTracks_l(Vector<sp<IAfTrack>> * tracksToRemove)7516 PlaybackThread::mixer_state OffloadThread::prepareTracks_l(
7517 Vector<sp<IAfTrack>>* tracksToRemove
7518 )
7519 {
7520 size_t count = mActiveTracks.size();
7521
7522 mixer_state mixerStatus = MIXER_IDLE;
7523 bool doHwPause = false;
7524 bool doHwResume = false;
7525
7526 ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count);
7527
7528 // find out which tracks need to be processed
7529 for (const sp<IAfTrack>& t : mActiveTracks) {
7530 IAfTrack* const track = t.get();
7531 #ifdef VERY_VERY_VERBOSE_LOGGING
7532 audio_track_cblk_t* cblk = track->cblk();
7533 #endif
7534 // Only consider last track started for volume and mixer state control.
7535 // In theory an older track could underrun and restart after the new one starts
7536 // but as we only care about the transition phase between two tracks on a
7537 // direct output, it is not a problem to ignore the underrun case.
7538 sp<IAfTrack> l = mActiveTracks.getLatest();
7539 bool last = l.get() == track;
7540
7541 if (track->isInvalid()) {
7542 ALOGW("An invalidated track shouldn't be in active list");
7543 tracksToRemove->add(track);
7544 continue;
7545 }
7546
7547 if (track->state() == IAfTrackBase::IDLE) {
7548 ALOGW("An idle track shouldn't be in active list");
7549 continue;
7550 }
7551
7552 const size_t framesReady = track->framesReady();
7553 if (ATRACE_ENABLED()) [[unlikely]] {
7554 ATRACE_INT(std::string(AUDIO_TRACE_PREFIX_AUDIO_TRACK_NRDY)
7555 .append(track->getTraceSuffix()).c_str(), framesReady);
7556 }
7557 if (track->isPausePending()) {
7558 track->pauseAck();
7559 // It is possible a track might have been flushed or stopped.
7560 // Other operations such as flush pending might occur on the next prepare.
7561 if (track->isPausing()) {
7562 track->setPaused();
7563 }
7564 // Always perform pause if last, as an immediate flush will change
7565 // the pause state to be no longer isPausing().
7566 if (last) {
7567 if (mHwSupportsPause && !mHwPaused) {
7568 doHwPause = true;
7569 mHwPaused = true;
7570 }
7571 // If we were part way through writing the mixbuffer to
7572 // the HAL we must save this until we resume
7573 // BUG - this will be wrong if a different track is made active,
7574 // in that case we want to discard the pending data in the
7575 // mixbuffer and tell the client to present it again when the
7576 // track is resumed
7577 mPausedWriteLength = mCurrentWriteLength;
7578 mPausedBytesRemaining = mBytesRemaining;
7579 mBytesRemaining = 0; // stop writing
7580 }
7581 tracksToRemove->add(track);
7582 } else if (track->isFlushPending()) {
7583 if (track->isStopping_1()) {
7584 track->retryCount() = kMaxTrackStopRetriesOffload;
7585 } else {
7586 track->retryCount() = kMaxTrackRetriesOffload;
7587 }
7588 track->flushAck();
7589 if (last) {
7590 mFlushPending = true;
7591 }
7592 } else if (track->isResumePending()){
7593 track->resumeAck();
7594 if (last) {
7595 if (mPausedBytesRemaining) {
7596 // Need to continue write that was interrupted
7597 mCurrentWriteLength = mPausedWriteLength;
7598 mBytesRemaining = mPausedBytesRemaining;
7599 mPausedBytesRemaining = 0;
7600 }
7601 if (mHwPaused) {
7602 doHwResume = true;
7603 mHwPaused = false;
7604 // threadLoop_mix() will handle the case that we need to
7605 // resume an interrupted write
7606 }
7607 // enable write to audio HAL
7608 mSleepTimeUs = 0;
7609
7610 mLeftVolFloat = mRightVolFloat = -1.0;
7611
7612 // Do not handle new data in this iteration even if track->framesReady()
7613 mixerStatus = MIXER_TRACKS_ENABLED;
7614 }
7615 } else if (framesReady && track->isReady() &&
7616 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) {
7617 ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer);
7618 if (track->fillingStatus() == IAfTrack::FS_FILLED) {
7619 track->fillingStatus() = IAfTrack::FS_ACTIVE;
7620 if (last) {
7621 // make sure processVolume_l() will apply new volume even if 0
7622 mLeftVolFloat = mRightVolFloat = -1.0;
7623 }
7624 }
7625
7626 if (last) {
7627 sp<IAfTrack> previousTrack = mPreviousTrack.promote();
7628 if (previousTrack != 0) {
7629 if (track != previousTrack.get()) {
7630 // Flush any data still being written from last track
7631 mBytesRemaining = 0;
7632 if (mPausedBytesRemaining) {
7633 // Last track was paused so we also need to flush saved
7634 // mixbuffer state and invalidate track so that it will
7635 // re-submit that unwritten data when it is next resumed
7636 mPausedBytesRemaining = 0;
7637 // Invalidate is a bit drastic - would be more efficient
7638 // to have a flag to tell client that some of the
7639 // previously written data was lost
7640 previousTrack->invalidate();
7641 }
7642 // flush data already sent to the DSP if changing audio session as audio
7643 // comes from a different source. Also invalidate previous track to force a
7644 // seek when resuming.
7645 if (previousTrack->sessionId() != track->sessionId()) {
7646 previousTrack->invalidate();
7647 }
7648 }
7649 }
7650 mPreviousTrack = track;
7651 // reset retry count
7652 if (track->isStopping_1()) {
7653 track->retryCount() = kMaxTrackStopRetriesOffload;
7654 } else {
7655 track->retryCount() = kMaxTrackRetriesOffload;
7656 }
7657 mActiveTrack = t;
7658 mixerStatus = MIXER_TRACKS_READY;
7659 }
7660 } else {
7661 ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer);
7662 if (track->isStopping_1()) {
7663 if (--(track->retryCount()) <= 0) {
7664 // Hardware buffer can hold a large amount of audio so we must
7665 // wait for all current track's data to drain before we say
7666 // that the track is stopped.
7667 if (mBytesRemaining == 0) {
7668 // Only start draining when all data in mixbuffer
7669 // has been written
7670 ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2");
7671 track->setState(IAfTrackBase::STOPPING_2);
7672 // so presentation completes after
7673 // drain do not drain if no data was ever sent to HAL (mStandby == true)
7674 if (last && !mStandby) {
7675 // do not modify drain sequence if we are already draining. This happens
7676 // when resuming from pause after drain.
7677 if ((mDrainSequence & 1) == 0) {
7678 mSleepTimeUs = 0;
7679 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
7680 mixerStatus = MIXER_DRAIN_TRACK;
7681 mDrainSequence += 2;
7682 }
7683 if (mHwPaused) {
7684 // It is possible to move from PAUSED to STOPPING_1 without
7685 // a resume so we must ensure hardware is running
7686 doHwResume = true;
7687 mHwPaused = false;
7688 }
7689 }
7690 }
7691 } else if (last) {
7692 ALOGV("stopping1 underrun retries left %d", track->retryCount());
7693 mixerStatus = MIXER_TRACKS_ENABLED;
7694 }
7695 } else if (track->isStopping_2()) {
7696 // Drain has completed or we are in standby, signal presentation complete
7697 if (!(mDrainSequence & 1) || !last || mStandby) {
7698 track->setState(IAfTrackBase::STOPPED);
7699 mOutput->presentationComplete();
7700 track->presentationComplete(latency_l()); // always returns true
7701 track->reset();
7702 tracksToRemove->add(track);
7703 // OFFLOADED stop resets frame counts.
7704 if (!mUseAsyncWrite) {
7705 // If we don't get explicit drain notification we must
7706 // register discontinuity regardless of whether this is
7707 // the previous (!last) or the upcoming (last) track
7708 // to avoid skipping the discontinuity.
7709 mTimestampVerifier.discontinuity(
7710 mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
7711 }
7712 }
7713 } else {
7714 // No buffers for this track. Give it a few chances to
7715 // fill a buffer, then remove it from active list.
7716 bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput);
7717 if (!isTunerStream() // tuner streams remain active in underrun
7718 && --(track->retryCount()) <= 0) {
7719 if (isTimestampAdvancing) { // HAL is still playing audio, give us more time.
7720 track->retryCount() = kMaxTrackRetriesOffload;
7721 } else {
7722 ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to"
7723 " underrun on thread %d", __func__, track->id(), mId);
7724 tracksToRemove->add(track);
7725 // tell client process that the track was disabled because of underrun;
7726 // it will then automatically call start() when data is available
7727 track->disable();
7728 }
7729 } else if (last){
7730 mixerStatus = MIXER_TRACKS_ENABLED;
7731 }
7732 }
7733 }
7734 // compute volume for this track
7735 if (track->isReady()) { // check ready to prevent premature start.
7736 processVolume_l(track, last);
7737 }
7738 }
7739
7740 // make sure the pause/flush/resume sequence is executed in the right order.
7741 // If a flush is pending and a track is active but the HW is not paused, force a HW pause
7742 // before flush and then resume HW. This can happen in case of pause/flush/resume
7743 // if resume is received before pause is executed.
7744 if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
7745 status_t result = mOutput->stream->pause();
7746 ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
7747 doHwResume = !doHwPause; // resume if pause is due to flush.
7748 }
7749 if (mFlushPending) {
7750 flushHw_l();
7751 }
7752 if (!mStandby && doHwResume) {
7753 status_t result = mOutput->stream->resume();
7754 ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
7755 }
7756
7757 // remove all the tracks that need to be...
7758 removeTracks_l(*tracksToRemove);
7759
7760 return mixerStatus;
7761 }
7762
7763 // must be called with thread mutex locked
waitingAsyncCallback_l()7764 bool OffloadThread::waitingAsyncCallback_l()
7765 {
7766 ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d",
7767 mWriteAckSequence, mDrainSequence);
7768 if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) {
7769 return true;
7770 }
7771 return false;
7772 }
7773
waitingAsyncCallback()7774 bool OffloadThread::waitingAsyncCallback()
7775 {
7776 audio_utils::lock_guard _l(mutex());
7777 return waitingAsyncCallback_l();
7778 }
7779
flushHw_l()7780 void OffloadThread::flushHw_l()
7781 {
7782 DirectOutputThread::flushHw_l();
7783 // Flush anything still waiting in the mixbuffer
7784 mCurrentWriteLength = 0;
7785 mBytesRemaining = 0;
7786 mPausedWriteLength = 0;
7787 mPausedBytesRemaining = 0;
7788 // reset bytes written count to reflect that DSP buffers are empty after flush.
7789 mBytesWritten = 0;
7790
7791 if (mUseAsyncWrite) {
7792 // discard any pending drain or write ack by incrementing sequence
7793 mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
7794 mDrainSequence = (mDrainSequence + 2) & ~1;
7795 ALOG_ASSERT(mCallbackThread != 0);
7796 mCallbackThread->setWriteBlocked(mWriteAckSequence);
7797 mCallbackThread->setDraining(mDrainSequence);
7798 }
7799 }
7800
invalidateTracks(audio_stream_type_t streamType)7801 void OffloadThread::invalidateTracks(audio_stream_type_t streamType)
7802 {
7803 audio_utils::lock_guard _l(mutex());
7804 if (PlaybackThread::invalidateTracks_l(streamType)) {
7805 mFlushPending = true;
7806 }
7807 }
7808
invalidateTracks(std::set<audio_port_handle_t> & portIds)7809 void OffloadThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) {
7810 audio_utils::lock_guard _l(mutex());
7811 if (PlaybackThread::invalidateTracks_l(portIds)) {
7812 mFlushPending = true;
7813 }
7814 }
7815
7816 // ----------------------------------------------------------------------------
7817
7818 /* static */
create(const sp<IAfThreadCallback> & afThreadCallback,IAfPlaybackThread * mainThread,audio_io_handle_t id,bool systemReady)7819 sp<IAfDuplicatingThread> IAfDuplicatingThread::create(
7820 const sp<IAfThreadCallback>& afThreadCallback,
7821 IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) {
7822 return sp<DuplicatingThread>::make(afThreadCallback, mainThread, id, systemReady);
7823 }
7824
DuplicatingThread(const sp<IAfThreadCallback> & afThreadCallback,IAfPlaybackThread * mainThread,audio_io_handle_t id,bool systemReady)7825 DuplicatingThread::DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback,
7826 IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady)
7827 : MixerThread(afThreadCallback, mainThread->getOutput(), id,
7828 systemReady, DUPLICATING),
7829 mWaitTimeMs(UINT_MAX)
7830 {
7831 addOutputTrack(mainThread);
7832 }
7833
~DuplicatingThread()7834 DuplicatingThread::~DuplicatingThread()
7835 {
7836 for (size_t i = 0; i < mOutputTracks.size(); i++) {
7837 mOutputTracks[i]->destroy();
7838 }
7839 }
7840
threadLoop_mix()7841 void DuplicatingThread::threadLoop_mix()
7842 {
7843 // mix buffers...
7844 if (outputsReady()) {
7845 mAudioMixer->process();
7846 } else {
7847 if (mMixerBufferValid) {
7848 memset(mMixerBuffer, 0, mMixerBufferSize);
7849 } else {
7850 memset(mSinkBuffer, 0, mSinkBufferSize);
7851 }
7852 }
7853 mSleepTimeUs = 0;
7854 writeFrames = mNormalFrameCount;
7855 mCurrentWriteLength = mSinkBufferSize;
7856 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
7857 }
7858
threadLoop_sleepTime()7859 void DuplicatingThread::threadLoop_sleepTime()
7860 {
7861 if (mSleepTimeUs == 0) {
7862 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
7863 mSleepTimeUs = mActiveSleepTimeUs;
7864 } else {
7865 mSleepTimeUs = mIdleSleepTimeUs;
7866 }
7867 } else if (mBytesWritten != 0) {
7868 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
7869 writeFrames = mNormalFrameCount;
7870 memset(mSinkBuffer, 0, mSinkBufferSize);
7871 } else {
7872 // flush remaining overflow buffers in output tracks
7873 writeFrames = 0;
7874 }
7875 mSleepTimeUs = 0;
7876 }
7877 }
7878
threadLoop_write()7879 ssize_t DuplicatingThread::threadLoop_write()
7880 {
7881 ATRACE_BEGIN("write");
7882 for (size_t i = 0; i < outputTracks.size(); i++) {
7883 const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames);
7884
7885 // Consider the first OutputTrack for timestamp and frame counting.
7886
7887 // The threadLoop() generally assumes writing a full sink buffer size at a time.
7888 // Here, we correct for writeFrames of 0 (a stop) or underruns because
7889 // we always claim success.
7890 if (i == 0) {
7891 const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten;
7892 ALOGD_IF(correction != 0 && writeFrames != 0,
7893 "%s: writeFrames:%u actualWritten:%zd correction:%zd mFramesWritten:%lld",
7894 __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten);
7895 mFramesWritten -= correction;
7896 }
7897
7898 // TODO: Report correction for the other output tracks and show in the dump.
7899 }
7900 ATRACE_END();
7901 if (mStandby) {
7902 mThreadMetrics.logBeginInterval();
7903 mThreadSnapshot.onBegin();
7904 mStandby = false;
7905 }
7906 return (ssize_t)mSinkBufferSize;
7907 }
7908
threadLoop_standby()7909 void DuplicatingThread::threadLoop_standby()
7910 {
7911 // DuplicatingThread implements standby by stopping all tracks
7912 for (size_t i = 0; i < outputTracks.size(); i++) {
7913 outputTracks[i]->stop();
7914 }
7915 }
7916
threadLoop_exit()7917 void DuplicatingThread::threadLoop_exit()
7918 {
7919 // Prevent calling the OutputTrack dtor in the DuplicatingThread dtor
7920 // where other mutexes (i.e. AudioPolicyService_Mutex) may be held.
7921 // Do so here in the threadLoop_exit().
7922
7923 SortedVector <sp<IAfOutputTrack>> localTracks;
7924 {
7925 audio_utils::lock_guard l(mutex());
7926 localTracks = std::move(mOutputTracks);
7927 mOutputTracks.clear();
7928 for (size_t i = 0; i < localTracks.size(); ++i) {
7929 localTracks[i]->destroy();
7930 }
7931 }
7932 localTracks.clear();
7933 outputTracks.clear();
7934 PlaybackThread::threadLoop_exit();
7935 }
7936
dumpInternals_l(int fd,const Vector<String16> & args)7937 void DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args)
7938 {
7939 MixerThread::dumpInternals_l(fd, args);
7940
7941 std::stringstream ss;
7942 const size_t numTracks = mOutputTracks.size();
7943 ss << " " << numTracks << " OutputTracks";
7944 if (numTracks > 0) {
7945 ss << ":";
7946 for (const auto &track : mOutputTracks) {
7947 const auto thread = track->thread().promote();
7948 ss << " (" << track->id() << " : ";
7949 if (thread.get() != nullptr) {
7950 ss << thread.get() << ", " << thread->id();
7951 } else {
7952 ss << "null";
7953 }
7954 ss << ")";
7955 }
7956 }
7957 ss << "\n";
7958 std::string result = ss.str();
7959 write(fd, result.c_str(), result.size());
7960 }
7961
saveOutputTracks()7962 void DuplicatingThread::saveOutputTracks()
7963 {
7964 outputTracks = mOutputTracks;
7965 }
7966
clearOutputTracks()7967 void DuplicatingThread::clearOutputTracks()
7968 {
7969 outputTracks.clear();
7970 }
7971
addOutputTrack(IAfPlaybackThread * thread)7972 void DuplicatingThread::addOutputTrack(IAfPlaybackThread* thread)
7973 {
7974 audio_utils::lock_guard _l(mutex());
7975 // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass.
7976 // Adjust for thread->sampleRate() to determine minimum buffer frame count.
7977 // Then triple buffer because Threads do not run synchronously and may not be clock locked.
7978 const size_t frameCount =
7979 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate());
7980 // TODO: Consider asynchronous sample rate conversion to handle clock disparity
7981 // from different OutputTracks and their associated MixerThreads (e.g. one may
7982 // nearly empty and the other may be dropping data).
7983
7984 // TODO b/182392769: use attribution source util, move to server edge
7985 AttributionSourceState attributionSource = AttributionSourceState();
7986 attributionSource.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(
7987 IPCThreadState::self()->getCallingUid()));
7988 attributionSource.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t(
7989 IPCThreadState::self()->getCallingPid()));
7990 attributionSource.token = sp<BBinder>::make();
7991 sp<IAfOutputTrack> outputTrack = IAfOutputTrack::create(thread,
7992 this,
7993 mSampleRate,
7994 mFormat,
7995 mChannelMask,
7996 frameCount,
7997 attributionSource);
7998 status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY;
7999 if (status != NO_ERROR) {
8000 ALOGE("addOutputTrack() initCheck failed %d", status);
8001 return;
8002 }
8003 if (!audioserver_flags::portid_volume_management()) {
8004 thread->setStreamVolume(AUDIO_STREAM_PATCH, /*volume=*/1.0f, /*muted=*/false);
8005 }
8006
8007 mOutputTracks.add(outputTrack);
8008 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
8009 updateWaitTime_l();
8010 }
8011
removeOutputTrack(IAfPlaybackThread * thread)8012 void DuplicatingThread::removeOutputTrack(IAfPlaybackThread* thread)
8013 {
8014 audio_utils::lock_guard _l(mutex());
8015 for (size_t i = 0; i < mOutputTracks.size(); i++) {
8016 if (mOutputTracks[i]->thread() == thread) {
8017 mOutputTracks[i]->destroy();
8018 mOutputTracks.removeAt(i);
8019 updateWaitTime_l();
8020 // NO_THREAD_SAFETY_ANALYSIS
8021 // Lambda workaround: as thread != this
8022 // we can safely call the remote thread getOutput.
8023 const bool equalOutput =
8024 [&](){ return thread->getOutput() == mOutput; }();
8025 if (equalOutput) {
8026 mOutput = nullptr;
8027 }
8028 return;
8029 }
8030 }
8031 ALOGV("removeOutputTrack(): unknown thread: %p", thread);
8032 }
8033
8034 // caller must hold mutex()
updateWaitTime_l()8035 void DuplicatingThread::updateWaitTime_l()
8036 {
8037 // Initialize mWaitTimeMs according to the mixer buffer size.
8038 mWaitTimeMs = mNormalFrameCount * 2 * 1000 / mSampleRate;
8039 for (size_t i = 0; i < mOutputTracks.size(); i++) {
8040 const auto strong = mOutputTracks[i]->thread().promote();
8041 if (strong != 0) {
8042 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
8043 if (waitTimeMs < mWaitTimeMs) {
8044 mWaitTimeMs = waitTimeMs;
8045 }
8046 }
8047 }
8048 }
8049
outputsReady()8050 bool DuplicatingThread::outputsReady()
8051 {
8052 for (size_t i = 0; i < outputTracks.size(); i++) {
8053 const auto thread = outputTracks[i]->thread().promote();
8054 if (thread == 0) {
8055 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p",
8056 outputTracks[i].get());
8057 return false;
8058 }
8059 IAfPlaybackThread* const playbackThread = thread->asIAfPlaybackThread().get();
8060 // see note at standby() declaration
8061 if (playbackThread->inStandby() && !playbackThread->isSuspended()) {
8062 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(),
8063 thread.get());
8064 return false;
8065 }
8066 }
8067 return true;
8068 }
8069
sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata & metadata)8070 void DuplicatingThread::sendMetadataToBackend_l(
8071 const StreamOutHalInterface::SourceMetadata& metadata)
8072 {
8073 for (auto& outputTrack : outputTracks) { // not mOutputTracks
8074 outputTrack->setMetadatas(metadata.tracks);
8075 }
8076 }
8077
activeSleepTimeUs() const8078 uint32_t DuplicatingThread::activeSleepTimeUs() const
8079 {
8080 // return half the wait time in microseconds.
8081 return std::min(mWaitTimeMs * 500ULL, (unsigned long long)UINT32_MAX); // prevent overflow.
8082 }
8083
cacheParameters_l()8084 void DuplicatingThread::cacheParameters_l()
8085 {
8086 // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
8087 updateWaitTime_l();
8088
8089 MixerThread::cacheParameters_l();
8090 }
8091
8092 // ----------------------------------------------------------------------------
8093
8094 /* static */
createSpatializerThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,audio_config_base_t * mixerConfig)8095 sp<IAfPlaybackThread> IAfPlaybackThread::createSpatializerThread(
8096 const sp<IAfThreadCallback>& afThreadCallback,
8097 AudioStreamOut* output,
8098 audio_io_handle_t id,
8099 bool systemReady,
8100 audio_config_base_t* mixerConfig) {
8101 return sp<SpatializerThread>::make(afThreadCallback, output, id, systemReady, mixerConfig);
8102 }
8103
SpatializerThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady,audio_config_base_t * mixerConfig)8104 SpatializerThread::SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback,
8105 AudioStreamOut* output,
8106 audio_io_handle_t id,
8107 bool systemReady,
8108 audio_config_base_t *mixerConfig)
8109 : MixerThread(afThreadCallback, output, id, systemReady, SPATIALIZER, mixerConfig)
8110 {
8111 }
8112
setHalLatencyMode_l()8113 void SpatializerThread::setHalLatencyMode_l() {
8114 // if mSupportedLatencyModes is empty, the HAL stream does not support
8115 // latency mode control and we can exit.
8116 if (mSupportedLatencyModes.empty()) {
8117 return;
8118 }
8119 // Do not update the HAL latency mode if no track is active
8120 if (mActiveTracks.isEmpty()) {
8121 return;
8122 }
8123
8124 audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE;
8125 if (mSupportedLatencyModes.size() == 1) {
8126 // If the HAL only support one latency mode currently, confirm the choice
8127 latencyMode = mSupportedLatencyModes[0];
8128 } else if (mSupportedLatencyModes.size() > 1) {
8129 // Request low latency if:
8130 // - The low latency mode is requested by the spatializer controller
8131 // (mRequestedLatencyMode = AUDIO_LATENCY_MODE_LOW)
8132 // AND
8133 // - At least one active track is spatialized
8134 for (const auto& track : mActiveTracks) {
8135 if (track->isSpatialized()) {
8136 latencyMode = mRequestedLatencyMode;
8137 break;
8138 }
8139 }
8140 }
8141
8142 if (latencyMode != mSetLatencyMode) {
8143 status_t status = mOutput->stream->setLatencyMode(latencyMode);
8144 ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d",
8145 __func__, mId, toString(latencyMode).c_str(), status);
8146 if (status == NO_ERROR) {
8147 mSetLatencyMode = latencyMode;
8148 }
8149 }
8150 }
8151
setRequestedLatencyMode(audio_latency_mode_t mode)8152 status_t SpatializerThread::setRequestedLatencyMode(audio_latency_mode_t mode) {
8153 if (mode < 0 || mode >= AUDIO_LATENCY_MODE_CNT) {
8154 return BAD_VALUE;
8155 }
8156 audio_utils::lock_guard _l(mutex());
8157 mRequestedLatencyMode = mode;
8158 return NO_ERROR;
8159 }
8160
checkOutputStageEffects()8161 void SpatializerThread::checkOutputStageEffects()
8162 NO_THREAD_SAFETY_ANALYSIS
8163 // 'createEffect_l' requires holding mutex 'AudioFlinger_Mutex' exclusively
8164 {
8165 bool hasVirtualizer = false;
8166 bool hasDownMixer = false;
8167 sp<IAfEffectHandle> finalDownMixer;
8168 {
8169 audio_utils::lock_guard _l(mutex());
8170 sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE);
8171 if (chain != 0) {
8172 hasVirtualizer = chain->getEffectFromType_l(FX_IID_SPATIALIZER) != nullptr;
8173 hasDownMixer = chain->getEffectFromType_l(EFFECT_UIID_DOWNMIX) != nullptr;
8174 }
8175
8176 finalDownMixer = mFinalDownMixer;
8177 mFinalDownMixer.clear();
8178 }
8179
8180 if (hasVirtualizer) {
8181 if (finalDownMixer != nullptr) {
8182 int32_t ret;
8183 finalDownMixer->asIEffect()->disable(&ret);
8184 }
8185 finalDownMixer.clear();
8186 } else if (!hasDownMixer) {
8187 std::vector<effect_descriptor_t> descriptors;
8188 status_t status = mAfThreadCallback->getEffectsFactoryHal()->getDescriptors(
8189 EFFECT_UIID_DOWNMIX, &descriptors);
8190 if (status != NO_ERROR) {
8191 return;
8192 }
8193 ALOG_ASSERT(!descriptors.empty(),
8194 "%s getDescriptors() returned no error but empty list", __func__);
8195
8196 finalDownMixer = createEffect_l(nullptr /*client*/, nullptr /*effectClient*/,
8197 0 /*priority*/, AUDIO_SESSION_OUTPUT_STAGE, &descriptors[0], nullptr /*enabled*/,
8198 &status, false /*pinned*/, false /*probe*/, false /*notifyFramesProcessed*/);
8199
8200 if (finalDownMixer == nullptr || (status != NO_ERROR && status != ALREADY_EXISTS)) {
8201 ALOGW("%s error creating downmixer %d", __func__, status);
8202 finalDownMixer.clear();
8203 } else {
8204 int32_t ret;
8205 finalDownMixer->asIEffect()->enable(&ret);
8206 }
8207 }
8208
8209 {
8210 audio_utils::lock_guard _l(mutex());
8211 mFinalDownMixer = finalDownMixer;
8212 }
8213 }
8214
threadLoop_exit()8215 void SpatializerThread::threadLoop_exit()
8216 {
8217 // The Spatializer EffectHandle must be released on the PlaybackThread
8218 // threadLoop() to prevent lock inversion in the SpatializerThread dtor.
8219 mFinalDownMixer.clear();
8220
8221 PlaybackThread::threadLoop_exit();
8222 }
8223
8224 // ----------------------------------------------------------------------------
8225 // Record
8226 // ----------------------------------------------------------------------------
8227
create(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamIn * input,audio_io_handle_t id,bool systemReady)8228 sp<IAfRecordThread> IAfRecordThread::create(const sp<IAfThreadCallback>& afThreadCallback,
8229 AudioStreamIn* input,
8230 audio_io_handle_t id,
8231 bool systemReady) {
8232 if (input->flags & AUDIO_INPUT_FLAG_DIRECT) {
8233 return sp<DirectRecordThread>::make(afThreadCallback, input, id, systemReady);
8234 }
8235 return sp<RecordThread>::make(afThreadCallback, RECORD, input, id, systemReady);
8236 }
8237
RecordThread(const sp<IAfThreadCallback> & afThreadCallback,ThreadBase::type_t type,AudioStreamIn * input,audio_io_handle_t id,bool systemReady)8238 RecordThread::RecordThread(const sp<IAfThreadCallback>& afThreadCallback,
8239 ThreadBase::type_t type,
8240 AudioStreamIn *input,
8241 audio_io_handle_t id,
8242 bool systemReady
8243 ) :
8244 ThreadBase(afThreadCallback, id, type, systemReady, false /* isOut */),
8245 mInput(input),
8246 mSource(mInput),
8247 mActiveTracks(&this->mLocalLog),
8248 mRsmpInBuffer(NULL),
8249 // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l()
8250 mRsmpInRear(0)
8251 , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize,
8252 "RecordThreadRO", MemoryHeapBase::READ_ONLY))
8253 // mFastCapture below
8254 , mFastCaptureFutex(0)
8255 // mInputSource
8256 // mPipeSink
8257 // mPipeSource
8258 , mPipeFramesP2(0)
8259 // mPipeMemory
8260 // mFastCaptureNBLogWriter
8261 , mFastTrackAvail(false)
8262 , mBtNrecSuspended(false)
8263 {
8264 snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id);
8265 mFlagsAsString = toString(input->flags);
8266
8267 if (mInput->audioHwDev != nullptr) {
8268 mIsMsdDevice = strcmp(
8269 mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
8270 }
8271
8272 readInputParameters_l();
8273
8274 // TODO: We may also match on address as well as device type for
8275 // AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_IN_REMOTE_SUBMIX
8276 // TODO: This property should be ensure that only contains one single device type.
8277 mTimestampCorrectedDevice = (audio_devices_t)property_get_int64(
8278 "audio.timestamp.corrected_input_device",
8279 (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD
8280 : AUDIO_DEVICE_NONE));
8281
8282 // create an NBAIO source for the HAL input stream, and negotiate
8283 mInputSource = new AudioStreamInSource(input->stream);
8284 size_t numCounterOffers = 0;
8285 const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
8286 #if !LOG_NDEBUG
8287 [[maybe_unused]] ssize_t index =
8288 #else
8289 (void)
8290 #endif
8291 mInputSource->negotiate(offers, 1, NULL, numCounterOffers);
8292 ALOG_ASSERT(index == 0);
8293
8294 // initialize fast capture depending on configuration
8295 bool initFastCapture;
8296 switch (kUseFastCapture) {
8297 case FastCapture_Never:
8298 initFastCapture = false;
8299 ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this);
8300 break;
8301 case FastCapture_Always:
8302 initFastCapture = true;
8303 ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this);
8304 break;
8305 case FastCapture_Static:
8306 initFastCapture = !mIsMsdDevice // Disable fast capture for MSD BUS devices.
8307 && audio_is_linear_pcm(mFormat)
8308 && (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs;
8309 ALOGV("%p kUseFastCapture = Static, format = 0x%x, (%lld * 1000) / %u vs %u, "
8310 "initFastCapture = %d, mIsMsdDevice = %d", this, mFormat, (long long)mFrameCount,
8311 mSampleRate, kMinNormalCaptureBufferSizeMs, initFastCapture, mIsMsdDevice);
8312 break;
8313 // case FastCapture_Dynamic:
8314 }
8315
8316 if (initFastCapture) {
8317 // create a Pipe for FastCapture to write to, and for us and fast tracks to read from
8318 NBAIO_Format format = mInputSource->format();
8319 // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread
8320 size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000);
8321 size_t pipeSize = pipeFramesP2 * Format_frameSize(format);
8322 void *pipeBuffer = nullptr;
8323 const sp<MemoryDealer> roHeap(readOnlyHeap());
8324 sp<IMemory> pipeMemory;
8325 if ((roHeap == 0) ||
8326 (pipeMemory = roHeap->allocate(pipeSize)) == 0 ||
8327 (pipeBuffer = pipeMemory->unsecurePointer()) == nullptr) {
8328 ALOGE("not enough memory for pipe buffer size=%zu; "
8329 "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld",
8330 pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer,
8331 (long long)kRecordThreadReadOnlyHeapSize);
8332 goto failed;
8333 }
8334 // pipe will be shared directly with fast clients, so clear to avoid leaking old information
8335 memset(pipeBuffer, 0, pipeSize);
8336 Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer);
8337 const NBAIO_Format offersFast[1] = {format};
8338 size_t numCounterOffersFast = 0;
8339 [[maybe_unused]] ssize_t index2 = pipe->negotiate(offersFast, std::size(offersFast),
8340 nullptr /* counterOffers */, numCounterOffersFast);
8341 ALOG_ASSERT(index2 == 0);
8342 mPipeSink = pipe;
8343 PipeReader *pipeReader = new PipeReader(*pipe);
8344 numCounterOffersFast = 0;
8345 index2 = pipeReader->negotiate(offersFast, std::size(offersFast),
8346 nullptr /* counterOffers */, numCounterOffersFast);
8347 ALOG_ASSERT(index2 == 0);
8348 mPipeSource = pipeReader;
8349 mPipeFramesP2 = pipeFramesP2;
8350 mPipeMemory = pipeMemory;
8351
8352 // create fast capture
8353 mFastCapture = new FastCapture();
8354 FastCaptureStateQueue *sq = mFastCapture->sq();
8355 #ifdef STATE_QUEUE_DUMP
8356 // FIXME
8357 #endif
8358 FastCaptureState *state = sq->begin();
8359 state->mCblk = NULL;
8360 state->mInputSource = mInputSource.get();
8361 state->mInputSourceGen++;
8362 state->mPipeSink = pipe;
8363 state->mPipeSinkGen++;
8364 state->mFrameCount = mFrameCount;
8365 state->mCommand = FastCaptureState::COLD_IDLE;
8366 // already done in constructor initialization list
8367 //mFastCaptureFutex = 0;
8368 state->mColdFutexAddr = &mFastCaptureFutex;
8369 state->mColdGen++;
8370 state->mDumpState = &mFastCaptureDumpState;
8371 #ifdef TEE_SINK
8372 // FIXME
8373 #endif
8374 sq->end();
8375 {
8376 audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastCapture->getTid());
8377 sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
8378 }
8379 // start the fast capture
8380 mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO);
8381 pid_t tid = mFastCapture->getTid();
8382 sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/);
8383 stream()->setHalThreadPriority(kPriorityFastCapture);
8384 #ifdef AUDIO_WATCHDOG
8385 // FIXME
8386 #endif
8387
8388 mFastTrackAvail = true;
8389 }
8390 #ifdef TEE_SINK
8391 mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD);
8392 mTee.setId(std::string("_") + std::to_string(mId) + "_C");
8393 #endif
8394 failed: ;
8395
8396 // FIXME mNormalSource
8397 }
8398
~RecordThread()8399 RecordThread::~RecordThread()
8400 {
8401 if (mFastCapture != 0) {
8402 FastCaptureStateQueue *sq = mFastCapture->sq();
8403 FastCaptureState *state = sq->begin();
8404 if (state->mCommand == FastCaptureState::COLD_IDLE) {
8405 int32_t old = android_atomic_inc(&mFastCaptureFutex);
8406 if (old == -1) {
8407 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
8408 }
8409 }
8410 state->mCommand = FastCaptureState::EXIT;
8411 sq->end();
8412 {
8413 audio_utils::mutex::scoped_join_wait_check queueWaitCheck(mFastCapture->getTid());
8414 sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
8415 mFastCapture->join();
8416 }
8417 mFastCapture.clear();
8418 }
8419 free(mRsmpInBuffer);
8420 }
8421
onFirstRef()8422 void RecordThread::onFirstRef()
8423 {
8424 run(mThreadName, PRIORITY_URGENT_AUDIO);
8425 }
8426
preExit()8427 void RecordThread::preExit()
8428 {
8429 ALOGV(" preExit()");
8430 audio_utils::lock_guard _l(mutex());
8431 for (size_t i = 0; i < mTracks.size(); i++) {
8432 sp<IAfRecordTrack> track = mTracks[i];
8433 track->invalidate();
8434 }
8435 mActiveTracks.clear();
8436 mStartStopCV.notify_all();
8437 }
8438
threadLoop()8439 bool RecordThread::threadLoop()
8440 {
8441 nsecs_t lastWarning = 0;
8442
8443 inputStandBy();
8444
8445 reacquire_wakelock:
8446 {
8447 audio_utils::lock_guard _l(mutex());
8448 acquireWakeLock_l();
8449 }
8450
8451 // used to request a deferred sleep, to be executed later while mutex is unlocked
8452 uint32_t sleepUs = 0;
8453
8454 // timestamp correction enable is determined under lock, used in processing step.
8455 bool timestampCorrectionEnabled = false;
8456
8457 int64_t lastLoopCountRead = -2; // never matches "previous" loop, when loopCount = 0.
8458
8459 // loop while there is work to do
8460 for (int64_t loopCount = 0;; ++loopCount) { // loopCount used for statistics tracking
8461 // Note: these sp<> are released at the end of the for loop outside of the mutex() lock.
8462 sp<IAfRecordTrack> activeTrack;
8463 std::vector<sp<IAfRecordTrack>> oldActiveTracks;
8464 Vector<sp<IAfEffectChain>> effectChains;
8465
8466 // activeTracks accumulates a copy of a subset of mActiveTracks
8467 Vector<sp<IAfRecordTrack>> activeTracks;
8468
8469 // reference to the (first and only) active fast track
8470 sp<IAfRecordTrack> fastTrack;
8471
8472 // reference to a fast track which is about to be removed
8473 sp<IAfRecordTrack> fastTrackToRemove;
8474
8475 bool silenceFastCapture = false;
8476
8477 { // scope for mutex()
8478 audio_utils::unique_lock _l(mutex());
8479
8480 processConfigEvents_l();
8481
8482 // check exitPending here because checkForNewParameters_l() and
8483 // checkForNewParameters_l() can temporarily release mutex()
8484 if (exitPending()) {
8485 break;
8486 }
8487
8488 // sleep with mutex unlocked
8489 if (sleepUs > 0) {
8490 ATRACE_BEGIN("sleepC");
8491 (void)mWaitWorkCV.wait_for(_l, std::chrono::microseconds(sleepUs));
8492 ATRACE_END();
8493 sleepUs = 0;
8494 continue;
8495 }
8496
8497 // if no active track(s), then standby and release wakelock
8498 size_t size = mActiveTracks.size();
8499 if (size == 0) {
8500 standbyIfNotAlreadyInStandby();
8501 // exitPending() can't become true here
8502 releaseWakeLock_l();
8503 ALOGV("RecordThread: loop stopping");
8504 // go to sleep
8505 mWaitWorkCV.wait(_l);
8506 ALOGV("RecordThread: loop starting");
8507 goto reacquire_wakelock;
8508 }
8509
8510 bool doBroadcast = false;
8511 bool allStopped = true;
8512 for (size_t i = 0; i < size; ) {
8513 if (activeTrack) { // ensure track release is outside lock.
8514 oldActiveTracks.emplace_back(std::move(activeTrack));
8515 }
8516 activeTrack = mActiveTracks[i];
8517 if (activeTrack->isTerminated()) {
8518 if (activeTrack->isFastTrack()) {
8519 ALOG_ASSERT(fastTrackToRemove == 0);
8520 fastTrackToRemove = activeTrack;
8521 }
8522 removeTrack_l(activeTrack);
8523 mActiveTracks.remove(activeTrack);
8524 size--;
8525 continue;
8526 }
8527
8528 IAfTrackBase::track_state activeTrackState = activeTrack->state();
8529 switch (activeTrackState) {
8530
8531 case IAfTrackBase::PAUSING:
8532 mActiveTracks.remove(activeTrack);
8533 activeTrack->setState(IAfTrackBase::PAUSED);
8534 if (activeTrack->isFastTrack()) {
8535 ALOGV("%s fast track is paused, thus removed from active list", __func__);
8536 // Keep a ref on fast track to wait for FastCapture thread to get updated
8537 // state before potential track removal
8538 fastTrackToRemove = activeTrack;
8539 }
8540 doBroadcast = true;
8541 size--;
8542 continue;
8543
8544 case IAfTrackBase::STARTING_1:
8545 sleepUs = 10000;
8546 i++;
8547 allStopped = false;
8548 continue;
8549
8550 case IAfTrackBase::STARTING_2:
8551 doBroadcast = true;
8552 if (mStandby) {
8553 mThreadMetrics.logBeginInterval();
8554 mThreadSnapshot.onBegin();
8555 mStandby = false;
8556 }
8557 activeTrack->setState(IAfTrackBase::ACTIVE);
8558 allStopped = false;
8559 break;
8560
8561 case IAfTrackBase::ACTIVE:
8562 allStopped = false;
8563 break;
8564
8565 case IAfTrackBase::IDLE: // cannot be on ActiveTracks if idle
8566 case IAfTrackBase::PAUSED: // cannot be on ActiveTracks if paused
8567 case IAfTrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated
8568 default:
8569 LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu",
8570 __func__, activeTrackState, activeTrack->id(), size);
8571 }
8572
8573 if (activeTrack->isFastTrack()) {
8574 ALOG_ASSERT(!mFastTrackAvail);
8575 ALOG_ASSERT(fastTrack == 0);
8576 // if the active fast track is silenced either:
8577 // 1) silence the whole capture from fast capture buffer if this is
8578 // the only active track
8579 // 2) invalidate this track: this will cause the client to reconnect and possibly
8580 // be invalidated again until unsilenced
8581 bool invalidate = false;
8582 if (activeTrack->isSilenced()) {
8583 if (size > 1) {
8584 invalidate = true;
8585 } else {
8586 silenceFastCapture = true;
8587 }
8588 }
8589 // Invalidate fast tracks if access to audio history is required as this is not
8590 // possible with fast tracks. Once the fast track has been invalidated, no new
8591 // fast track will be created until mMaxSharedAudioHistoryMs is cleared.
8592 if (mMaxSharedAudioHistoryMs != 0) {
8593 invalidate = true;
8594 }
8595 if (invalidate) {
8596 activeTrack->invalidate();
8597 fastTrackToRemove = activeTrack;
8598 removeTrack_l(activeTrack);
8599 mActiveTracks.remove(activeTrack);
8600 size--;
8601 continue;
8602 }
8603 fastTrack = activeTrack;
8604 }
8605
8606 activeTracks.add(activeTrack);
8607 i++;
8608
8609 }
8610
8611 mActiveTracks.updatePowerState_l(this);
8612
8613 // check if traces have been enabled.
8614 bool atraceEnabled = ATRACE_ENABLED();
8615 if (atraceEnabled != mAtraceEnabled) [[unlikely]] {
8616 mAtraceEnabled = atraceEnabled;
8617 if (atraceEnabled) {
8618 const auto devices = patchSourcesToString(&mPatch);
8619 for (const auto& track : activeTracks) {
8620 track->logRefreshInterval(devices);
8621 }
8622 }
8623 }
8624
8625 updateMetadata_l();
8626
8627 if (allStopped) {
8628 standbyIfNotAlreadyInStandby();
8629 }
8630 if (doBroadcast) {
8631 mStartStopCV.notify_all();
8632 }
8633
8634 // sleep if there are no active tracks to process
8635 if (activeTracks.isEmpty()) {
8636 if (sleepUs == 0) {
8637 sleepUs = kRecordThreadSleepUs;
8638 }
8639 continue;
8640 }
8641 sleepUs = 0;
8642
8643 timestampCorrectionEnabled = isTimestampCorrectionEnabled_l();
8644 lockEffectChains_l(effectChains);
8645 // We're exiting locked scope with non empty activeTracks, make sure
8646 // that we're not in standby mode which we could have entered if some
8647 // tracks were muted/unmuted.
8648 mStandby = false;
8649 }
8650
8651 // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0
8652
8653 size_t size = effectChains.size();
8654 for (size_t i = 0; i < size; i++) {
8655 // thread mutex is not locked, but effect chain is locked
8656 effectChains[i]->process_l();
8657 }
8658
8659 // Push a new fast capture state if fast capture is not already running, or cblk change
8660 if (mFastCapture != 0) {
8661 FastCaptureStateQueue *sq = mFastCapture->sq();
8662 FastCaptureState *state = sq->begin();
8663 bool didModify = false;
8664 FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED;
8665 if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME &&
8666 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) {
8667 if (state->mCommand == FastCaptureState::COLD_IDLE) {
8668 int32_t old = android_atomic_inc(&mFastCaptureFutex);
8669 if (old == -1) {
8670 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
8671 }
8672 }
8673 state->mCommand = FastCaptureState::READ_WRITE;
8674 #if 0 // FIXME
8675 mFastCaptureDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ?
8676 FastThreadDumpState::kSamplingNforLowRamDevice :
8677 FastThreadDumpState::kSamplingN);
8678 #endif
8679 didModify = true;
8680 }
8681 audio_track_cblk_t *cblkOld = state->mCblk;
8682 audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL;
8683 if (cblkNew != cblkOld) {
8684 state->mCblk = cblkNew;
8685 // block until acked if removing a fast track
8686 if (cblkOld != NULL) {
8687 block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED;
8688 }
8689 didModify = true;
8690 }
8691 AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ?
8692 reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr;
8693 if (state->mFastPatchRecordBufferProvider != abp) {
8694 state->mFastPatchRecordBufferProvider = abp;
8695 state->mFastPatchRecordFormat = fastTrack == 0 ?
8696 AUDIO_FORMAT_INVALID : fastTrack->format();
8697 didModify = true;
8698 }
8699 if (state->mSilenceCapture != silenceFastCapture) {
8700 state->mSilenceCapture = silenceFastCapture;
8701 didModify = true;
8702 }
8703 sq->end(didModify);
8704 if (didModify) {
8705 sq->push(block);
8706 #if 0
8707 if (kUseFastCapture == FastCapture_Dynamic) {
8708 mNormalSource = mPipeSource;
8709 }
8710 #endif
8711 }
8712 }
8713
8714 // now run the fast track destructor with thread mutex unlocked
8715 fastTrackToRemove.clear();
8716
8717 // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one.
8718 // Only the client(s) that are too slow will overrun. But if even the fastest client is too
8719 // slow, then this RecordThread will overrun by not calling HAL read often enough.
8720 // If destination is non-contiguous, first read past the nominal end of buffer, then
8721 // copy to the right place. Permitted because mRsmpInBuffer was over-allocated.
8722
8723 int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1);
8724 ssize_t framesRead = 0; // not needed, remove clang-tidy warning.
8725 const int64_t lastIoBeginNs = systemTime(); // start IO timing
8726
8727 // If an NBAIO source is present, use it to read the normal capture's data
8728 if (mPipeSource != 0) {
8729 size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2);
8730
8731 // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer
8732 // to the full buffer point (clearing the overflow condition). Upon OVERRUN error,
8733 // we immediately retry the read() to get data and prevent another overflow.
8734 for (int retries = 0; retries <= 2; ++retries) {
8735 ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries);
8736 framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize,
8737 framesToRead);
8738 if (framesRead != OVERRUN) break;
8739 }
8740
8741 const ssize_t availableToRead = mPipeSource->availableToRead();
8742 if (availableToRead >= 0) {
8743 mMonopipePipeDepthStats.add(availableToRead);
8744 // PipeSource is the primary clock. It is up to the AudioRecord client to keep up.
8745 LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2,
8746 "more frames to read than fifo size, %zd > %zu",
8747 availableToRead, mPipeFramesP2);
8748 const size_t pipeFramesFree = mPipeFramesP2 - availableToRead;
8749 const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2;
8750 ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd",
8751 mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead);
8752 sleepUs = (sleepFrames * 1000000LL) / mSampleRate;
8753 }
8754 if (framesRead < 0) {
8755 status_t status = (status_t) framesRead;
8756 switch (status) {
8757 case OVERRUN:
8758 ALOGW("overrun on read from pipe");
8759 framesRead = 0;
8760 break;
8761 case NEGOTIATE:
8762 ALOGE("re-negotiation is needed");
8763 framesRead = -1; // Will cause an attempt to recover.
8764 break;
8765 default:
8766 ALOGE("unknown error %d on read from pipe", status);
8767 break;
8768 }
8769 }
8770 // otherwise use the HAL / AudioStreamIn directly
8771 } else {
8772 ATRACE_BEGIN("read");
8773 size_t bytesRead;
8774 status_t result = mSource->read(
8775 (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead);
8776 ATRACE_END();
8777 if (result < 0) {
8778 framesRead = result;
8779 } else {
8780 framesRead = bytesRead / mFrameSize;
8781 }
8782 }
8783
8784 const int64_t lastIoEndNs = systemTime(); // end IO timing
8785
8786 // Update server timestamp with server stats
8787 // systemTime() is optional if the hardware supports timestamps.
8788 if (framesRead >= 0) {
8789 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead;
8790 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs;
8791 }
8792
8793 // Update server timestamp with kernel stats
8794 if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) {
8795 int64_t position, time;
8796 if (mStandby) {
8797 mTimestampVerifier.discontinuity(audio_is_linear_pcm(mFormat) ?
8798 mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS :
8799 mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
8800 } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR
8801 && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) {
8802
8803 mTimestampVerifier.add(position, time, mSampleRate);
8804 if (timestampCorrectionEnabled) {
8805 ALOGVV("TS_BEFORE: %d %lld %lld",
8806 id(), (long long)time, (long long)position);
8807 auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
8808 position = correctedTimestamp.mFrames;
8809 time = correctedTimestamp.mTimeNs;
8810 ALOGVV("TS_AFTER: %d %lld %lld",
8811 id(), (long long)time, (long long)position);
8812 }
8813
8814 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position;
8815 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time;
8816 // Note: In general record buffers should tend to be empty in
8817 // a properly running pipeline.
8818 //
8819 // Also, it is not advantageous to call get_presentation_position during the read
8820 // as the read obtains a lock, preventing the timestamp call from executing.
8821 } else {
8822 mTimestampVerifier.error();
8823 }
8824 }
8825
8826 // From the timestamp, input read latency is negative output write latency.
8827 const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE;
8828 const double latencyMs = IAfRecordTrack::checkServerLatencySupported(mFormat, flags)
8829 ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.;
8830 if (latencyMs != 0.) { // note 0. means timestamp is empty.
8831 mLatencyMs.add(latencyMs);
8832 }
8833
8834 // Use this to track timestamp information
8835 // ALOGD("%s", mTimestamp.toString().c_str());
8836
8837 if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) {
8838 ALOGE("read failed: framesRead=%zd", framesRead);
8839 // Force input into standby so that it tries to recover at next read attempt
8840 inputStandBy();
8841 sleepUs = kRecordThreadSleepUs;
8842 }
8843 if (framesRead <= 0) {
8844 goto unlock;
8845 }
8846 ALOG_ASSERT(framesRead > 0);
8847 mFramesRead += framesRead;
8848
8849 #ifdef TEE_SINK
8850 (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead);
8851 #endif
8852 // If destination is non-contiguous, we now correct for reading past end of buffer.
8853 {
8854 size_t part1 = mRsmpInFramesP2 - rear;
8855 if ((size_t) framesRead > part1) {
8856 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize,
8857 (framesRead - part1) * mFrameSize);
8858 }
8859 }
8860 mRsmpInRear = audio_utils::safe_add_overflow(mRsmpInRear, (int32_t)framesRead);
8861
8862 size = activeTracks.size();
8863
8864 // loop over each active track
8865 for (size_t i = 0; i < size; i++) {
8866 if (activeTrack) { // ensure track release is outside lock.
8867 oldActiveTracks.emplace_back(std::move(activeTrack));
8868 }
8869 activeTrack = activeTracks[i];
8870
8871 // skip fast tracks, as those are handled directly by FastCapture
8872 if (activeTrack->isFastTrack()) {
8873 continue;
8874 }
8875
8876 // TODO: This code probably should be moved to RecordTrack.
8877 // TODO: Update the activeTrack buffer converter in case of reconfigure.
8878
8879 enum {
8880 OVERRUN_UNKNOWN,
8881 OVERRUN_TRUE,
8882 OVERRUN_FALSE
8883 } overrun = OVERRUN_UNKNOWN;
8884
8885 // loop over getNextBuffer to handle circular sink
8886 for (;;) {
8887
8888 activeTrack->sinkBuffer().frameCount = ~0;
8889 status_t status = activeTrack->getNextBuffer(&activeTrack->sinkBuffer());
8890 size_t framesOut = activeTrack->sinkBuffer().frameCount;
8891 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0));
8892
8893 // check available frames and handle overrun conditions
8894 // if the record track isn't draining fast enough.
8895 bool hasOverrun;
8896 size_t framesIn;
8897 activeTrack->resamplerBufferProvider()->sync(&framesIn, &hasOverrun);
8898 if (hasOverrun) {
8899 overrun = OVERRUN_TRUE;
8900 }
8901 if (framesOut == 0 || framesIn == 0) {
8902 break;
8903 }
8904
8905 // Don't allow framesOut to be larger than what is possible with resampling
8906 // from framesIn.
8907 // This isn't strictly necessary but helps limit buffer resizing in
8908 // RecordBufferConverter. TODO: remove when no longer needed.
8909 if (audio_is_linear_pcm(activeTrack->format())) {
8910 framesOut = min(framesOut,
8911 destinationFramesPossible(
8912 framesIn, mSampleRate, activeTrack->sampleRate()));
8913 }
8914
8915 if (activeTrack->isDirect()) {
8916 // No RecordBufferConverter used for direct streams. Pass
8917 // straight from RecordThread buffer to RecordTrack buffer.
8918 AudioBufferProvider::Buffer buffer;
8919 buffer.frameCount = framesOut;
8920 const status_t getNextBufferStatus =
8921 activeTrack->resamplerBufferProvider()->getNextBuffer(&buffer);
8922 if (getNextBufferStatus == OK && buffer.frameCount != 0) {
8923 ALOGV_IF(buffer.frameCount != framesOut,
8924 "%s() read less than expected (%zu vs %zu)",
8925 __func__, buffer.frameCount, framesOut);
8926 framesOut = buffer.frameCount;
8927 memcpy(activeTrack->sinkBuffer().raw,
8928 buffer.raw, buffer.frameCount * mFrameSize);
8929 activeTrack->resamplerBufferProvider()->releaseBuffer(&buffer);
8930 } else {
8931 framesOut = 0;
8932 ALOGE("%s() cannot fill request, status: %d, frameCount: %zu",
8933 __func__, getNextBufferStatus, buffer.frameCount);
8934 }
8935 } else {
8936 // process frames from the RecordThread buffer provider to the RecordTrack
8937 // buffer
8938 framesOut = activeTrack->recordBufferConverter()->convert(
8939 activeTrack->sinkBuffer().raw,
8940 activeTrack->resamplerBufferProvider(),
8941 framesOut);
8942 }
8943
8944 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) {
8945 overrun = OVERRUN_FALSE;
8946 }
8947
8948 // MediaSyncEvent handling: Synchronize AudioRecord to AudioTrack completion.
8949 const ssize_t framesToDrop =
8950 activeTrack->synchronizedRecordState().updateRecordFrames(framesOut);
8951 if (framesToDrop == 0) {
8952 // no sync event, process normally, otherwise ignore.
8953 if (framesOut > 0) {
8954 activeTrack->sinkBuffer().frameCount = framesOut;
8955 // Sanitize before releasing if the track has no access to the source data
8956 // An idle UID receives silence from non virtual devices until active
8957 if (activeTrack->isSilenced()) {
8958 memset(activeTrack->sinkBuffer().raw,
8959 0, framesOut * activeTrack->frameSize());
8960 }
8961 activeTrack->releaseBuffer(&activeTrack->sinkBuffer());
8962 }
8963 }
8964 if (framesOut == 0) {
8965 break;
8966 }
8967 }
8968
8969 switch (overrun) {
8970 case OVERRUN_TRUE:
8971 // client isn't retrieving buffers fast enough
8972 if (!activeTrack->setOverflow()) {
8973 nsecs_t now = systemTime();
8974 // FIXME should lastWarning per track?
8975 if ((now - lastWarning) > kWarningThrottleNs) {
8976 ALOGW("RecordThread: buffer overflow");
8977 lastWarning = now;
8978 }
8979 }
8980 break;
8981 case OVERRUN_FALSE:
8982 activeTrack->clearOverflow();
8983 break;
8984 case OVERRUN_UNKNOWN:
8985 break;
8986 }
8987
8988 // update frame information and push timestamp out
8989 activeTrack->updateTrackFrameInfo(
8990 activeTrack->serverProxy()->framesReleased(),
8991 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER],
8992 mSampleRate, mTimestamp);
8993 }
8994
8995 unlock:
8996 // enable changes in effect chain
8997 unlockEffectChains(effectChains);
8998 // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end
8999 if (audio_has_proportional_frames(mFormat)
9000 && loopCount == lastLoopCountRead + 1) {
9001 const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs;
9002 const double jitterMs =
9003 TimestampVerifier<int64_t, int64_t>::computeJitterMs(
9004 {framesRead, readPeriodNs},
9005 {0, 0} /* lastTimestamp */, mSampleRate);
9006 const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6;
9007
9008 audio_utils::lock_guard _l(mutex());
9009 mIoJitterMs.add(jitterMs);
9010 mProcessTimeMs.add(processMs);
9011 }
9012 mThreadloopExecutor.process();
9013 // update timing info.
9014 mLastIoBeginNs = lastIoBeginNs;
9015 mLastIoEndNs = lastIoEndNs;
9016 lastLoopCountRead = loopCount;
9017 }
9018 mThreadloopExecutor.process(); // process any remaining deferred actions.
9019 // deferred actions after this point are ignored.
9020
9021 standbyIfNotAlreadyInStandby();
9022
9023 {
9024 audio_utils::lock_guard _l(mutex());
9025 for (size_t i = 0; i < mTracks.size(); i++) {
9026 sp<IAfRecordTrack> track = mTracks[i];
9027 track->invalidate();
9028 }
9029 mActiveTracks.clear();
9030 mStartStopCV.notify_all();
9031 }
9032
9033 releaseWakeLock();
9034
9035 ALOGV("RecordThread %p exiting", this);
9036 return false;
9037 }
9038
standbyIfNotAlreadyInStandby()9039 void RecordThread::standbyIfNotAlreadyInStandby()
9040 {
9041 if (!mStandby) {
9042 inputStandBy();
9043 mThreadMetrics.logEndInterval();
9044 mThreadSnapshot.onEnd();
9045 mStandby = true;
9046 }
9047 }
9048
inputStandBy()9049 void RecordThread::inputStandBy()
9050 {
9051 // Idle the fast capture if it's currently running
9052 if (mFastCapture != 0) {
9053 FastCaptureStateQueue *sq = mFastCapture->sq();
9054 FastCaptureState *state = sq->begin();
9055 if (!(state->mCommand & FastCaptureState::IDLE)) {
9056 state->mCommand = FastCaptureState::COLD_IDLE;
9057 state->mColdFutexAddr = &mFastCaptureFutex;
9058 state->mColdGen++;
9059 mFastCaptureFutex = 0;
9060 sq->end();
9061 // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
9062 {
9063 audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastCapture->getTid());
9064 sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED);
9065 }
9066
9067 #if 0
9068 if (kUseFastCapture == FastCapture_Dynamic) {
9069 // FIXME
9070 }
9071 #endif
9072 #ifdef AUDIO_WATCHDOG
9073 // FIXME
9074 #endif
9075 } else {
9076 sq->end(false /*didModify*/);
9077 }
9078 }
9079 status_t result = mSource->standby();
9080 ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result);
9081
9082 // If going into standby, flush the pipe source.
9083 if (mPipeSource.get() != nullptr) {
9084 const ssize_t flushed = mPipeSource->flush();
9085 if (flushed > 0) {
9086 ALOGV("Input standby flushed PipeSource %zd frames", flushed);
9087 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed;
9088 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime();
9089 }
9090 }
9091 }
9092
9093 // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mutex() held
createRecordTrack_l(const sp<Client> & client,const audio_attributes_t & attr,uint32_t * pSampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * pFrameCount,audio_session_t sessionId,size_t * pNotificationFrameCount,pid_t creatorPid,const AttributionSourceState & attributionSource,audio_input_flags_t * flags,pid_t tid,status_t * status,audio_port_handle_t portId,int32_t maxSharedAudioHistoryMs)9094 sp<IAfRecordTrack> RecordThread::createRecordTrack_l(
9095 const sp<Client>& client,
9096 const audio_attributes_t& attr,
9097 uint32_t *pSampleRate,
9098 audio_format_t format,
9099 audio_channel_mask_t channelMask,
9100 size_t *pFrameCount,
9101 audio_session_t sessionId,
9102 size_t *pNotificationFrameCount,
9103 pid_t creatorPid,
9104 const AttributionSourceState& attributionSource,
9105 audio_input_flags_t *flags,
9106 pid_t tid,
9107 status_t *status,
9108 audio_port_handle_t portId,
9109 int32_t maxSharedAudioHistoryMs)
9110 {
9111 size_t frameCount = *pFrameCount;
9112 size_t notificationFrameCount = *pNotificationFrameCount;
9113 sp<IAfRecordTrack> track;
9114 status_t lStatus;
9115 audio_input_flags_t inputFlags = mInput->flags;
9116 audio_input_flags_t requestedFlags = *flags;
9117 uint32_t sampleRate;
9118
9119 lStatus = initCheck();
9120 if (lStatus != NO_ERROR) {
9121 ALOGE("createRecordTrack_l() audio driver not initialized");
9122 goto Exit;
9123 }
9124
9125 if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) {
9126 ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT");
9127 lStatus = BAD_VALUE;
9128 goto Exit;
9129 }
9130
9131 if (maxSharedAudioHistoryMs != 0) {
9132 if (audioserver_permissions()) {
9133 const auto res = mAfThreadCallback->getPermissionProvider().checkPermission(
9134 CAPTURE_AUDIO_HOTWORD,
9135 attributionSource.uid);
9136 if (!res.ok()) {
9137 lStatus = aidl_utils::statusTFromBinderStatus(res.error());
9138 }
9139 if (!res.value()) {
9140 lStatus = PERMISSION_DENIED;
9141 goto Exit;
9142 }
9143 } else {
9144 if (!captureHotwordAllowed(attributionSource)) {
9145 lStatus = PERMISSION_DENIED;
9146 goto Exit;
9147 }
9148 }
9149 if (maxSharedAudioHistoryMs < 0
9150 || maxSharedAudioHistoryMs > kMaxSharedAudioHistoryMs) {
9151 lStatus = BAD_VALUE;
9152 goto Exit;
9153 }
9154 }
9155 if (*pSampleRate == 0) {
9156 *pSampleRate = mSampleRate;
9157 }
9158 sampleRate = *pSampleRate;
9159
9160 // special case for FAST flag considered OK if fast capture is present and access to
9161 // audio history is not required
9162 if (hasFastCapture() && mMaxSharedAudioHistoryMs == 0) {
9163 inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST);
9164 }
9165
9166 // Check if requested flags are compatible with input stream flags
9167 if ((*flags & inputFlags) != *flags) {
9168 ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and"
9169 " input flags (%08x)",
9170 *flags, inputFlags);
9171 *flags = (audio_input_flags_t)(*flags & inputFlags);
9172 }
9173
9174 // client expresses a preference for FAST and no access to audio history,
9175 // but we get the final say
9176 if (*flags & AUDIO_INPUT_FLAG_FAST && maxSharedAudioHistoryMs == 0) {
9177 if (
9178 // we formerly checked for a callback handler (non-0 tid),
9179 // but that is no longer required for TRANSFER_OBTAIN mode
9180 // No need to match hardware format, format conversion will be done in client side.
9181 //
9182 // Frame count is not specified (0), or is less than or equal the pipe depth.
9183 // It is OK to provide a higher capacity than requested.
9184 // We will force it to mPipeFramesP2 below.
9185 (frameCount <= mPipeFramesP2) &&
9186 // PCM data
9187 audio_is_linear_pcm(format) &&
9188 // hardware channel mask
9189 (channelMask == mChannelMask) &&
9190 // hardware sample rate
9191 (sampleRate == mSampleRate) &&
9192 // record thread has an associated fast capture
9193 hasFastCapture() &&
9194 // there are sufficient fast track slots available
9195 mFastTrackAvail
9196 ) {
9197 // check compatibility with audio effects.
9198 audio_utils::lock_guard _l(mutex());
9199 // Do not accept FAST flag if the session has software effects
9200 sp<IAfEffectChain> chain = getEffectChain_l(sessionId);
9201 if (chain != 0) {
9202 audio_input_flags_t old = *flags;
9203 chain->checkInputFlagCompatibility(flags);
9204 if (old != *flags) {
9205 ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x",
9206 this, (int)old, (int)*flags);
9207 }
9208 }
9209 ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0,
9210 "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
9211 this, frameCount, mFrameCount);
9212 } else {
9213 ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu "
9214 "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u "
9215 "hasFastCapture=%d tid=%d mFastTrackAvail=%d",
9216 this, frameCount, mFrameCount, mPipeFramesP2,
9217 format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate,
9218 hasFastCapture(), tid, mFastTrackAvail);
9219 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
9220 }
9221 }
9222
9223 // If FAST or RAW flags were corrected, ask caller to request new input from audio policy
9224 if ((*flags & AUDIO_INPUT_FLAG_FAST) !=
9225 (requestedFlags & AUDIO_INPUT_FLAG_FAST)) {
9226 *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW));
9227 lStatus = BAD_TYPE;
9228 goto Exit;
9229 }
9230
9231 // compute track buffer size in frames, and suggest the notification frame count
9232 if (*flags & AUDIO_INPUT_FLAG_FAST) {
9233 // fast track: frame count is exactly the pipe depth
9234 frameCount = mPipeFramesP2;
9235 // ignore requested notificationFrames, and always notify exactly once every HAL buffer
9236 notificationFrameCount = mFrameCount;
9237 } else {
9238 // not fast track: max notification period is resampled equivalent of one HAL buffer time
9239 // or 20 ms if there is a fast capture
9240 // TODO This could be a roundupRatio inline, and const
9241 size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount)
9242 * sampleRate + mSampleRate - 1) / mSampleRate;
9243 // minimum number of notification periods is at least kMinNotifications,
9244 // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs)
9245 static const size_t kMinNotifications = 3;
9246 static const uint32_t kMinMs = 30;
9247 // TODO This could be a roundupRatio inline
9248 const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000;
9249 // TODO This could be a roundupRatio inline
9250 const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) /
9251 maxNotificationFrames;
9252 const size_t minFrameCount = maxNotificationFrames *
9253 max(kMinNotifications, minNotificationsByMs);
9254 frameCount = max(frameCount, minFrameCount);
9255 if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
9256 notificationFrameCount = maxNotificationFrames;
9257 }
9258 }
9259 *pFrameCount = frameCount;
9260 *pNotificationFrameCount = notificationFrameCount;
9261
9262 { // scope for mutex()
9263 audio_utils::lock_guard _l(mutex());
9264 int32_t startFrames = -1;
9265 if (!mSharedAudioPackageName.empty()
9266 && mSharedAudioPackageName == attributionSource.packageName
9267 && mSharedAudioSessionId == sessionId
9268 && (audioserver_permissions() ?
9269 mAfThreadCallback->getPermissionProvider().checkPermission(
9270 CAPTURE_AUDIO_HOTWORD,
9271 attributionSource.uid).value_or(false)
9272 : captureHotwordAllowed(attributionSource))) {
9273 startFrames = mSharedAudioStartFrames;
9274 }
9275
9276 track = IAfRecordTrack::create(this, client, attr, sampleRate,
9277 format, channelMask, frameCount,
9278 nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid,
9279 attributionSource, *flags, IAfTrackBase::TYPE_DEFAULT, portId,
9280 startFrames);
9281
9282 lStatus = track->initCheck();
9283 if (lStatus != NO_ERROR) {
9284 ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus);
9285 // track must be cleared from the caller as the caller has the AF lock
9286 goto Exit;
9287 }
9288 mTracks.add(track);
9289
9290 if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) {
9291 pid_t callingPid = IPCThreadState::self()->getCallingPid();
9292 // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
9293 // so ask activity manager to do this on our behalf
9294 sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
9295 }
9296
9297 if (maxSharedAudioHistoryMs != 0) {
9298 sendResizeBufferConfigEvent_l(maxSharedAudioHistoryMs);
9299 }
9300 }
9301
9302 lStatus = NO_ERROR;
9303
9304 Exit:
9305 *status = lStatus;
9306 return track;
9307 }
9308
start(IAfRecordTrack * recordTrack,AudioSystem::sync_event_t event,audio_session_t triggerSession)9309 status_t RecordThread::start(IAfRecordTrack* recordTrack,
9310 AudioSystem::sync_event_t event,
9311 audio_session_t triggerSession)
9312 {
9313 ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
9314 sp<ThreadBase> strongMe = this;
9315 status_t status = NO_ERROR;
9316
9317 if (event == AudioSystem::SYNC_EVENT_NONE) {
9318 recordTrack->clearSyncStartEvent();
9319 } else if (event != AudioSystem::SYNC_EVENT_SAME) {
9320 recordTrack->synchronizedRecordState().startRecording(
9321 mAfThreadCallback->createSyncEvent(
9322 event, triggerSession,
9323 recordTrack->sessionId(), syncStartEventCallback, recordTrack));
9324 }
9325
9326 {
9327 // This section is a rendezvous between binder thread executing start() and RecordThread
9328 audio_utils::lock_guard lock(mutex());
9329 if (recordTrack->isInvalid()) {
9330 recordTrack->clearSyncStartEvent();
9331 ALOGW("%s track %d: invalidated before startInput", __func__, recordTrack->portId());
9332 return DEAD_OBJECT;
9333 }
9334 if (mActiveTracks.indexOf(recordTrack) >= 0) {
9335 if (recordTrack->state() == IAfTrackBase::PAUSING) {
9336 // We haven't stopped yet (moved to PAUSED and not in mActiveTracks)
9337 // so no need to startInput().
9338 ALOGV("active record track PAUSING -> ACTIVE");
9339 recordTrack->setState(IAfTrackBase::ACTIVE);
9340 } else {
9341 ALOGV("active record track state %d", (int)recordTrack->state());
9342 }
9343 return status;
9344 }
9345
9346 // TODO consider other ways of handling this, such as changing the state to :STARTING and
9347 // adding the track to mActiveTracks after returning from AudioSystem::startInput(),
9348 // or using a separate command thread
9349 recordTrack->setState(IAfTrackBase::STARTING_1);
9350 mActiveTracks.add(recordTrack);
9351 if (recordTrack->isExternalTrack()) {
9352 mutex().unlock();
9353 status = AudioSystem::startInput(recordTrack->portId());
9354 mutex().lock();
9355 if (recordTrack->isInvalid()) {
9356 recordTrack->clearSyncStartEvent();
9357 if (status == NO_ERROR && recordTrack->state() == IAfTrackBase::STARTING_1) {
9358 recordTrack->setState(IAfTrackBase::STARTING_2);
9359 // STARTING_2 forces destroy to call stopInput.
9360 }
9361 ALOGW("%s track %d: invalidated after startInput", __func__, recordTrack->portId());
9362 return DEAD_OBJECT;
9363 }
9364 if (recordTrack->state() != IAfTrackBase::STARTING_1) {
9365 ALOGW("%s(%d): unsynchronized mState:%d change",
9366 __func__, recordTrack->id(), (int)recordTrack->state());
9367 // Someone else has changed state, let them take over,
9368 // leave mState in the new state.
9369 recordTrack->clearSyncStartEvent();
9370 return INVALID_OPERATION;
9371 }
9372 // we're ok, but perhaps startInput has failed
9373 if (status != NO_ERROR) {
9374 ALOGW("%s(%d): startInput failed, status %d",
9375 __func__, recordTrack->id(), status);
9376 // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks,
9377 // leave in STARTING_1, so destroy() will not call stopInput.
9378 mActiveTracks.remove(recordTrack);
9379 recordTrack->clearSyncStartEvent();
9380 return status;
9381 }
9382 sendIoConfigEvent_l(
9383 AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId());
9384 }
9385
9386 recordTrack->logBeginInterval(patchSourcesToString(&mPatch)); // log to MediaMetrics
9387
9388 // Catch up with current buffer indices if thread is already running.
9389 // This is what makes a new client discard all buffered data. If the track's mRsmpInFront
9390 // was initialized to some value closer to the thread's mRsmpInFront, then the track could
9391 // see previously buffered data before it called start(), but with greater risk of overrun.
9392
9393 recordTrack->resamplerBufferProvider()->reset();
9394 if (!recordTrack->isDirect()) {
9395 // clear any converter state as new data will be discontinuous
9396 recordTrack->recordBufferConverter()->reset();
9397 }
9398 recordTrack->setState(IAfTrackBase::STARTING_2);
9399 // signal thread to start
9400 mWaitWorkCV.notify_all();
9401 return status;
9402 }
9403 }
9404
syncStartEventCallback(const wp<SyncEvent> & event)9405 void RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
9406 {
9407 const sp<SyncEvent> strongEvent = event.promote();
9408
9409 if (strongEvent != 0) {
9410 sp<IAfTrackBase> ptr =
9411 std::any_cast<const wp<IAfTrackBase>>(strongEvent->cookie()).promote();
9412 if (ptr != nullptr) {
9413 // TODO(b/291317898) handleSyncStartEvent is in IAfTrackBase not IAfRecordTrack.
9414 ptr->handleSyncStartEvent(strongEvent);
9415 }
9416 }
9417 }
9418
stop(IAfRecordTrack * recordTrack)9419 bool RecordThread::stop(IAfRecordTrack* recordTrack) {
9420 ALOGV("RecordThread::stop");
9421 audio_utils::unique_lock _l(mutex());
9422 // if we're invalid, we can't be on the ActiveTracks.
9423 if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->state() == IAfTrackBase::PAUSING) {
9424 return false;
9425 }
9426 // note that threadLoop may still be processing the track at this point [without lock]
9427 recordTrack->setState(IAfTrackBase::PAUSING);
9428
9429 // NOTE: Waiting here is important to keep stop synchronous.
9430 // This is needed for proper patchRecord peer release.
9431 while (recordTrack->state() == IAfTrackBase::PAUSING && !recordTrack->isInvalid()) {
9432 mWaitWorkCV.notify_all(); // signal thread to stop
9433 mStartStopCV.wait(_l, getTid());
9434 }
9435
9436 if (recordTrack->state() == IAfTrackBase::PAUSED) { // successful stop
9437 ALOGV("Record stopped OK");
9438 return true;
9439 }
9440
9441 // don't handle anything - we've been invalidated or restarted and in a different state
9442 ALOGW_IF("%s(%d): unsynchronized stop, state: %d",
9443 __func__, recordTrack->id(), recordTrack->state());
9444 return false;
9445 }
9446
isValidSyncEvent(const sp<SyncEvent> &) const9447 bool RecordThread::isValidSyncEvent(const sp<SyncEvent>& /* event */) const
9448 {
9449 return false;
9450 }
9451
setSyncEvent(const sp<SyncEvent> &)9452 status_t RecordThread::setSyncEvent(const sp<SyncEvent>& /* event */)
9453 {
9454 #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future
9455 if (!isValidSyncEvent(event)) {
9456 return BAD_VALUE;
9457 }
9458
9459 audio_session_t eventSession = event->triggerSession();
9460 status_t ret = NAME_NOT_FOUND;
9461
9462 audio_utils::lock_guard _l(mutex());
9463
9464 for (size_t i = 0; i < mTracks.size(); i++) {
9465 sp<IAfRecordTrack> track = mTracks[i];
9466 if (eventSession == track->sessionId()) {
9467 (void) track->setSyncEvent(event);
9468 ret = NO_ERROR;
9469 }
9470 }
9471 return ret;
9472 #else
9473 return BAD_VALUE;
9474 #endif
9475 }
9476
getActiveMicrophones(std::vector<media::MicrophoneInfoFw> * activeMicrophones) const9477 status_t RecordThread::getActiveMicrophones(
9478 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const
9479 {
9480 ALOGV("RecordThread::getActiveMicrophones");
9481 audio_utils::lock_guard _l(mutex());
9482 if (!isStreamInitialized()) {
9483 return NO_INIT;
9484 }
9485 status_t status = mInput->stream->getActiveMicrophones(activeMicrophones);
9486 return status;
9487 }
9488
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)9489 status_t RecordThread::setPreferredMicrophoneDirection(
9490 audio_microphone_direction_t direction)
9491 {
9492 ALOGV("setPreferredMicrophoneDirection(%d)", direction);
9493 audio_utils::lock_guard _l(mutex());
9494 if (!isStreamInitialized()) {
9495 return NO_INIT;
9496 }
9497 return mInput->stream->setPreferredMicrophoneDirection(direction);
9498 }
9499
setPreferredMicrophoneFieldDimension(float zoom)9500 status_t RecordThread::setPreferredMicrophoneFieldDimension(float zoom)
9501 {
9502 ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
9503 audio_utils::lock_guard _l(mutex());
9504 if (!isStreamInitialized()) {
9505 return NO_INIT;
9506 }
9507 return mInput->stream->setPreferredMicrophoneFieldDimension(zoom);
9508 }
9509
shareAudioHistory(const std::string & sharedAudioPackageName,audio_session_t sharedSessionId,int64_t sharedAudioStartMs)9510 status_t RecordThread::shareAudioHistory(
9511 const std::string& sharedAudioPackageName, audio_session_t sharedSessionId,
9512 int64_t sharedAudioStartMs) {
9513 audio_utils::lock_guard _l(mutex());
9514 return shareAudioHistory_l(sharedAudioPackageName, sharedSessionId, sharedAudioStartMs);
9515 }
9516
shareAudioHistory_l(const std::string & sharedAudioPackageName,audio_session_t sharedSessionId,int64_t sharedAudioStartMs)9517 status_t RecordThread::shareAudioHistory_l(
9518 const std::string& sharedAudioPackageName, audio_session_t sharedSessionId,
9519 int64_t sharedAudioStartMs) {
9520
9521 if ((hasAudioSession_l(sharedSessionId) & ThreadBase::TRACK_SESSION) == 0) {
9522 return BAD_VALUE;
9523 }
9524
9525 if (sharedAudioStartMs < 0
9526 || sharedAudioStartMs > INT64_MAX / mSampleRate) {
9527 return BAD_VALUE;
9528 }
9529
9530 // Current implementation of the input resampling buffer wraps around indexes at 32 bit.
9531 // As we cannot detect more than one wraparound, only accept values up current write position
9532 // after one wraparound
9533 // We assume recent wraparounds on mRsmpInRear only given it is unlikely that the requesting
9534 // app waits several hours after the start time was computed.
9535 int64_t sharedAudioStartFrames = sharedAudioStartMs * mSampleRate / 1000;
9536 const int32_t sharedOffset = audio_utils::safe_sub_overflow(mRsmpInRear,
9537 (int32_t)sharedAudioStartFrames);
9538 // Bring the start frame position within the input buffer to match the documented
9539 // "best effort" behavior of the API.
9540 if (sharedOffset < 0) {
9541 sharedAudioStartFrames = mRsmpInRear;
9542 } else if (sharedOffset > static_cast<signed>(mRsmpInFrames)) {
9543 sharedAudioStartFrames =
9544 audio_utils::safe_sub_overflow(mRsmpInRear, (int32_t)mRsmpInFrames);
9545 }
9546
9547 mSharedAudioPackageName = sharedAudioPackageName;
9548 if (mSharedAudioPackageName.empty()) {
9549 resetAudioHistory_l();
9550 } else {
9551 mSharedAudioSessionId = sharedSessionId;
9552 mSharedAudioStartFrames = (int32_t)sharedAudioStartFrames;
9553 }
9554 return NO_ERROR;
9555 }
9556
resetAudioHistory_l()9557 void RecordThread::resetAudioHistory_l() {
9558 mSharedAudioSessionId = AUDIO_SESSION_NONE;
9559 mSharedAudioStartFrames = -1;
9560 mSharedAudioPackageName = "";
9561 }
9562
updateMetadata_l()9563 ThreadBase::MetadataUpdate RecordThread::updateMetadata_l()
9564 {
9565 if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) {
9566 return {}; // nothing to do
9567 }
9568 StreamInHalInterface::SinkMetadata metadata;
9569 auto backInserter = std::back_inserter(metadata.tracks);
9570 for (const sp<IAfRecordTrack>& track : mActiveTracks) {
9571 track->copyMetadataTo(backInserter);
9572 }
9573 mInput->stream->updateSinkMetadata(metadata);
9574 MetadataUpdate change;
9575 change.recordMetadataUpdate = metadata.tracks;
9576 return change;
9577 }
9578
9579 // destroyTrack_l() must be called with ThreadBase::mutex() held
destroyTrack_l(const sp<IAfRecordTrack> & track)9580 void RecordThread::destroyTrack_l(const sp<IAfRecordTrack>& track)
9581 {
9582 track->terminate();
9583 track->setState(IAfTrackBase::STOPPED);
9584
9585 // active tracks are removed by threadLoop()
9586 if (mActiveTracks.indexOf(track) < 0) {
9587 removeTrack_l(track);
9588 }
9589 }
9590
removeTrack_l(const sp<IAfRecordTrack> & track)9591 void RecordThread::removeTrack_l(const sp<IAfRecordTrack>& track)
9592 {
9593 String8 result;
9594 track->appendDump(result, false /* active */);
9595 mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str());
9596
9597 mTracks.remove(track);
9598 // need anything related to effects here?
9599 if (track->isFastTrack()) {
9600 ALOG_ASSERT(!mFastTrackAvail);
9601 mFastTrackAvail = true;
9602 }
9603 }
9604
dumpInternals_l(int fd,const Vector<String16> &)9605 void RecordThread::dumpInternals_l(int fd, const Vector<String16>& /* args */)
9606 {
9607 AudioStreamIn *input = mInput;
9608 audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE;
9609 dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n",
9610 input, flags, toString(flags).c_str());
9611 dprintf(fd, " Frames read: %lld\n", (long long)mFramesRead);
9612 if (mActiveTracks.isEmpty()) {
9613 dprintf(fd, " No active record clients\n");
9614 }
9615
9616 if (input != nullptr) {
9617 dprintf(fd, " Hal stream dump:\n");
9618 (void)input->stream->dump(fd);
9619 }
9620
9621 dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no");
9622 dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no");
9623
9624 // Make a non-atomic copy of fast capture dump state so it won't change underneath us
9625 // while we are dumping it. It may be inconsistent, but it won't mutate!
9626 // This is a large object so we place it on the heap.
9627 // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
9628 const std::unique_ptr<FastCaptureDumpState> copy =
9629 std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState);
9630 copy->dump(fd);
9631 }
9632
dumpTracks_l(int fd,const Vector<String16> &)9633 void RecordThread::dumpTracks_l(int fd, const Vector<String16>& /* args */)
9634 {
9635 String8 result;
9636 size_t numtracks = mTracks.size();
9637 size_t numactive = mActiveTracks.size();
9638 size_t numactiveseen = 0;
9639 dprintf(fd, " %zu Tracks", numtracks);
9640 const char *prefix = " ";
9641 if (numtracks) {
9642 dprintf(fd, " of which %zu are active\n", numactive);
9643 result.append(prefix);
9644 mTracks[0]->appendDumpHeader(result);
9645 for (size_t i = 0; i < numtracks ; ++i) {
9646 sp<IAfRecordTrack> track = mTracks[i];
9647 if (track != 0) {
9648 bool active = mActiveTracks.indexOf(track) >= 0;
9649 if (active) {
9650 numactiveseen++;
9651 }
9652 result.append(prefix);
9653 track->appendDump(result, active);
9654 }
9655 }
9656 } else {
9657 dprintf(fd, "\n");
9658 }
9659
9660 if (numactiveseen != numactive) {
9661 result.append(" The following tracks are in the active list but"
9662 " not in the track list\n");
9663 result.append(prefix);
9664 mActiveTracks[0]->appendDumpHeader(result);
9665 for (size_t i = 0; i < numactive; ++i) {
9666 sp<IAfRecordTrack> track = mActiveTracks[i];
9667 if (mTracks.indexOf(track) < 0) {
9668 result.append(prefix);
9669 track->appendDump(result, true /* active */);
9670 }
9671 }
9672
9673 }
9674 write(fd, result.c_str(), result.size());
9675 }
9676
setRecordSilenced(audio_port_handle_t portId,bool silenced)9677 void RecordThread::setRecordSilenced(audio_port_handle_t portId, bool silenced)
9678 {
9679 audio_utils::lock_guard _l(mutex());
9680 for (size_t i = 0; i < mTracks.size() ; i++) {
9681 sp<IAfRecordTrack> track = mTracks[i];
9682 if (track != 0 && track->portId() == portId) {
9683 track->setSilenced(silenced);
9684 }
9685 }
9686 }
9687
9688 // --------------------------------------------------------------------------------------
9689 // DirectRecordThread
9690 // --------------------------------------------------------------------------------------
9691
DirectRecordThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamIn * input,audio_io_handle_t id,bool systemReady)9692 DirectRecordThread::DirectRecordThread(const sp<IAfThreadCallback>& afThreadCallback,
9693 AudioStreamIn* input, audio_io_handle_t id, bool systemReady)
9694 : RecordThread(afThreadCallback, DIRECT_RECORD, input, id, systemReady) {
9695 ALOGD("%s:", __func__);
9696 }
9697
~DirectRecordThread()9698 DirectRecordThread::~DirectRecordThread() {}
9699
reset()9700 void ResamplerBufferProvider::reset()
9701 {
9702 const auto threadBase = mRecordTrack->thread().promote();
9703 auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get());
9704 mRsmpInUnrel = 0;
9705 const int32_t rear = recordThread->mRsmpInRear;
9706 ssize_t deltaFrames = 0;
9707 if (mRecordTrack->startFrames() >= 0) {
9708 int32_t startFrames = mRecordTrack->startFrames();
9709 // Accept a recent wraparound of mRsmpInRear
9710 if (startFrames <= rear) {
9711 deltaFrames = rear - startFrames;
9712 } else {
9713 deltaFrames = (int32_t)((int64_t)rear + UINT32_MAX + 1 - startFrames);
9714 }
9715 // start frame cannot be further in the past than start of resampling buffer
9716 if ((size_t) deltaFrames > recordThread->mRsmpInFrames) {
9717 deltaFrames = recordThread->mRsmpInFrames;
9718 }
9719 }
9720 mRsmpInFront = audio_utils::safe_sub_overflow(rear, static_cast<int32_t>(deltaFrames));
9721 }
9722
sync(size_t * framesAvailable,bool * hasOverrun)9723 void ResamplerBufferProvider::sync(
9724 size_t *framesAvailable, bool *hasOverrun)
9725 {
9726 const auto threadBase = mRecordTrack->thread().promote();
9727 auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get());
9728 const int32_t rear = recordThread->mRsmpInRear;
9729 const int32_t front = mRsmpInFront;
9730 const ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
9731
9732 size_t framesIn;
9733 bool overrun = false;
9734 if (filled < 0) {
9735 // should not happen, but treat like a massive overrun and re-sync
9736 framesIn = 0;
9737 mRsmpInFront = rear;
9738 overrun = true;
9739 } else if ((size_t) filled <= recordThread->mRsmpInFrames) {
9740 framesIn = (size_t) filled;
9741 } else {
9742 // client is not keeping up with server, but give it latest data
9743 framesIn = recordThread->mRsmpInFrames;
9744 mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow(
9745 rear, static_cast<int32_t>(framesIn));
9746 overrun = true;
9747 }
9748 if (framesAvailable != NULL) {
9749 *framesAvailable = framesIn;
9750 }
9751 if (hasOverrun != NULL) {
9752 *hasOverrun = overrun;
9753 }
9754 }
9755
9756 // AudioBufferProvider interface
getNextBuffer(AudioBufferProvider::Buffer * buffer)9757 status_t ResamplerBufferProvider::getNextBuffer(
9758 AudioBufferProvider::Buffer* buffer)
9759 {
9760 const auto threadBase = mRecordTrack->thread().promote();
9761 if (threadBase == 0) {
9762 buffer->frameCount = 0;
9763 buffer->raw = NULL;
9764 return NOT_ENOUGH_DATA;
9765 }
9766 auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get());
9767 int32_t rear = recordThread->mRsmpInRear;
9768 int32_t front = mRsmpInFront;
9769 ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
9770 // FIXME should not be P2 (don't want to increase latency)
9771 // FIXME if client not keeping up, discard
9772 LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames));
9773 // 'filled' may be non-contiguous, so return only the first contiguous chunk
9774
9775 front &= recordThread->mRsmpInFramesP2 - 1;
9776 size_t part1 = recordThread->mRsmpInFramesP2 - front;
9777 if (part1 > (size_t) filled) {
9778 part1 = filled;
9779 }
9780 size_t ask = buffer->frameCount;
9781 ALOG_ASSERT(ask > 0);
9782 if (part1 > ask) {
9783 part1 = ask;
9784 }
9785 if (part1 == 0) {
9786 // out of data is fine since the resampler will return a short-count.
9787 buffer->raw = NULL;
9788 buffer->frameCount = 0;
9789 mRsmpInUnrel = 0;
9790 return NOT_ENOUGH_DATA;
9791 }
9792
9793 buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize;
9794 buffer->frameCount = part1;
9795 mRsmpInUnrel = part1;
9796 return NO_ERROR;
9797 }
9798
9799 // AudioBufferProvider interface
releaseBuffer(AudioBufferProvider::Buffer * buffer)9800 void ResamplerBufferProvider::releaseBuffer(
9801 AudioBufferProvider::Buffer* buffer)
9802 {
9803 int32_t stepCount = static_cast<int32_t>(buffer->frameCount);
9804 if (stepCount == 0) {
9805 return;
9806 }
9807 ALOG_ASSERT(stepCount <= (int32_t)mRsmpInUnrel);
9808 mRsmpInUnrel -= stepCount;
9809 mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount);
9810 buffer->raw = NULL;
9811 buffer->frameCount = 0;
9812 }
9813
checkBtNrec()9814 void RecordThread::checkBtNrec()
9815 {
9816 audio_utils::lock_guard _l(mutex());
9817 checkBtNrec_l();
9818 }
9819
checkBtNrec_l()9820 void RecordThread::checkBtNrec_l()
9821 {
9822 // disable AEC and NS if the device is a BT SCO headset supporting those
9823 // pre processings
9824 bool suspend = audio_is_bluetooth_sco_device(inDeviceType_l()) &&
9825 mAfThreadCallback->btNrecIsOff();
9826 if (mBtNrecSuspended.exchange(suspend) != suspend) {
9827 for (size_t i = 0; i < mEffectChains.size(); i++) {
9828 setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId());
9829 setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId());
9830 }
9831 }
9832 }
9833
9834
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)9835 bool RecordThread::checkForNewParameter_l(const String8& keyValuePair,
9836 status_t& status)
9837 {
9838 bool reconfig = false;
9839
9840 status = NO_ERROR;
9841
9842 audio_format_t reqFormat = mFormat;
9843 uint32_t samplingRate = mSampleRate;
9844 // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs).
9845 [[maybe_unused]] audio_channel_mask_t channelMask =
9846 audio_channel_in_mask_from_count(mChannelCount);
9847
9848 AudioParameter param = AudioParameter(keyValuePair);
9849 int value;
9850
9851 // scope for AutoPark extends to end of method
9852 AutoPark<FastCapture> park(mFastCapture);
9853
9854 // TODO Investigate when this code runs. Check with audio policy when a sample rate and
9855 // channel count change can be requested. Do we mandate the first client defines the
9856 // HAL sampling rate and channel count or do we allow changes on the fly?
9857 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
9858 samplingRate = value;
9859 reconfig = true;
9860 }
9861 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
9862 if (!audio_is_linear_pcm((audio_format_t) value)) {
9863 status = BAD_VALUE;
9864 } else {
9865 reqFormat = (audio_format_t) value;
9866 reconfig = true;
9867 }
9868 }
9869 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
9870 audio_channel_mask_t mask = (audio_channel_mask_t) value;
9871 if (!audio_is_input_channel(mask) ||
9872 audio_channel_count_from_in_mask(mask) > FCC_LIMIT) {
9873 status = BAD_VALUE;
9874 } else {
9875 channelMask = mask;
9876 reconfig = true;
9877 }
9878 }
9879 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
9880 // do not accept frame count changes if tracks are open as the track buffer
9881 // size depends on frame count and correct behavior would not be guaranteed
9882 // if frame count is changed after track creation
9883 if (mActiveTracks.size() > 0) {
9884 status = INVALID_OPERATION;
9885 } else {
9886 reconfig = true;
9887 }
9888 }
9889 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
9890 LOG_FATAL("Should not set routing device in RecordThread");
9891 }
9892 if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR &&
9893 mAudioSource != (audio_source_t)value) {
9894 LOG_FATAL("Should not set audio source in RecordThread");
9895 }
9896
9897 if (status == NO_ERROR) {
9898 status = mInput->stream->setParameters(keyValuePair);
9899 if (status == INVALID_OPERATION) {
9900 inputStandBy();
9901 status = mInput->stream->setParameters(keyValuePair);
9902 }
9903 if (reconfig) {
9904 if (status == BAD_VALUE) {
9905 audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
9906 if (mInput->stream->getAudioProperties(&config) == OK &&
9907 audio_is_linear_pcm(config.format) && audio_is_linear_pcm(reqFormat) &&
9908 config.sample_rate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) &&
9909 audio_channel_count_from_in_mask(config.channel_mask) <= FCC_LIMIT) {
9910 status = NO_ERROR;
9911 }
9912 }
9913 if (status == NO_ERROR) {
9914 readInputParameters_l();
9915 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
9916 }
9917 }
9918 }
9919
9920 return reconfig;
9921 }
9922
getParameters(const String8 & keys)9923 String8 RecordThread::getParameters(const String8& keys)
9924 {
9925 audio_utils::lock_guard _l(mutex());
9926 if (initCheck() == NO_ERROR) {
9927 String8 out_s8;
9928 if (mInput->stream->getParameters(keys, &out_s8) == OK) {
9929 return out_s8;
9930 }
9931 }
9932 return {};
9933 }
9934
ioConfigChanged_l(audio_io_config_event_t event,pid_t pid,audio_port_handle_t portId)9935 void RecordThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid,
9936 audio_port_handle_t portId) {
9937 sp<AudioIoDescriptor> desc;
9938 switch (event) {
9939 case AUDIO_INPUT_OPENED:
9940 case AUDIO_INPUT_REGISTERED:
9941 case AUDIO_INPUT_CONFIG_CHANGED:
9942 desc = sp<AudioIoDescriptor>::make(mId, mPatch, true /*isInput*/,
9943 mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount);
9944 break;
9945 case AUDIO_CLIENT_STARTED:
9946 desc = sp<AudioIoDescriptor>::make(mId, mPatch, portId);
9947 break;
9948 case AUDIO_INPUT_CLOSED:
9949 default:
9950 desc = sp<AudioIoDescriptor>::make(mId);
9951 break;
9952 }
9953 mAfThreadCallback->ioConfigChanged_l(event, desc, pid);
9954 }
9955
readInputParameters_l()9956 void RecordThread::readInputParameters_l()
9957 {
9958 const audio_config_base_t audioConfig = mInput->getAudioProperties();
9959 mSampleRate = audioConfig.sample_rate;
9960 mChannelMask = audioConfig.channel_mask;
9961 if (!audio_is_input_channel(mChannelMask)) {
9962 LOG_ALWAYS_FATAL("Channel mask %#x not valid for input", mChannelMask);
9963 }
9964
9965 mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
9966
9967 // Get actual HAL format.
9968 status_t result = mInput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat);
9969 LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving input stream format: %d", result);
9970 // Get format from the shim, which will be different than the HAL format
9971 // if recording compressed audio from IEC61937 wrapped sources.
9972 mFormat = audioConfig.format;
9973 if (!audio_is_valid_format(mFormat)) {
9974 LOG_ALWAYS_FATAL("Format %#x not valid for input", mFormat);
9975 }
9976 if (audio_is_linear_pcm(mFormat)) {
9977 LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_LIMIT, "HAL channel count %d > %d",
9978 mChannelCount, FCC_LIMIT);
9979 } else {
9980 // Can have more that FCC_LIMIT channels in encoded streams.
9981 ALOGI("HAL format %#x is not linear pcm", mFormat);
9982 }
9983 mFrameSize = mInput->getFrameSize();
9984 LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero",
9985 mFrameSize);
9986 result = mInput->stream->getBufferSize(&mBufferSize);
9987 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
9988 mFrameCount = mBufferSize / mFrameSize;
9989 ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, "
9990 "mBufferSize=%zu, mFrameCount=%zu",
9991 this, mChannelCount, mFormat, mFrameSize, mBufferSize, mFrameCount);
9992
9993 // mRsmpInFrames must be 0 before calling resizeInputBuffer_l for the first time
9994 mRsmpInFrames = 0;
9995 resizeInputBuffer_l(0 /*maxSharedAudioHistoryMs*/);
9996
9997 // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints.
9998 // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks?
9999
10000 audio_input_flags_t flags = mInput->flags;
10001 mediametrics::LogItem item(mThreadMetrics.getMetricsId());
10002 item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS)
10003 .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str())
10004 .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str())
10005 .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate)
10006 .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask)
10007 .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount)
10008 .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount)
10009 .record();
10010 }
10011
getInputFramesLost() const10012 uint32_t RecordThread::getInputFramesLost() const
10013 {
10014 audio_utils::lock_guard _l(mutex());
10015 uint32_t result;
10016 if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) {
10017 return result;
10018 }
10019 return 0;
10020 }
10021
sessionIds() const10022 KeyedVector<audio_session_t, bool> RecordThread::sessionIds() const
10023 {
10024 KeyedVector<audio_session_t, bool> ids;
10025 audio_utils::lock_guard _l(mutex());
10026 for (size_t j = 0; j < mTracks.size(); ++j) {
10027 sp<IAfRecordTrack> track = mTracks[j];
10028 audio_session_t sessionId = track->sessionId();
10029 if (ids.indexOfKey(sessionId) < 0) {
10030 ids.add(sessionId, true);
10031 }
10032 }
10033 return ids;
10034 }
10035
clearInput()10036 AudioStreamIn* RecordThread::clearInput()
10037 {
10038 audio_utils::lock_guard _l(mutex());
10039 AudioStreamIn *input = mInput;
10040 mInput = NULL;
10041 mInputSource.clear();
10042 return input;
10043 }
10044
10045 // this method must always be called either with ThreadBase mutex() held or inside the thread loop
stream() const10046 sp<StreamHalInterface> RecordThread::stream() const
10047 {
10048 if (mInput == NULL) {
10049 return NULL;
10050 }
10051 return mInput->stream;
10052 }
10053
addEffectChain_l(const sp<IAfEffectChain> & chain)10054 status_t RecordThread::addEffectChain_l(const sp<IAfEffectChain>& chain)
10055 {
10056 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
10057 chain->setThread(this);
10058 chain->setInBuffer(NULL);
10059 chain->setOutBuffer(NULL);
10060
10061 checkSuspendOnAddEffectChain_l(chain);
10062
10063 // make sure enabled pre processing effects state is communicated to the HAL as we
10064 // just moved them to a new input stream.
10065 chain->syncHalEffectsState_l();
10066
10067 mEffectChains.add(chain);
10068
10069 return NO_ERROR;
10070 }
10071
removeEffectChain_l(const sp<IAfEffectChain> & chain)10072 size_t RecordThread::removeEffectChain_l(const sp<IAfEffectChain>& chain)
10073 {
10074 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
10075
10076 for (size_t i = 0; i < mEffectChains.size(); i++) {
10077 if (chain == mEffectChains[i]) {
10078 mEffectChains.removeAt(i);
10079 break;
10080 }
10081 }
10082 return mEffectChains.size();
10083 }
10084
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)10085 status_t RecordThread::createAudioPatch_l(const struct audio_patch* patch,
10086 audio_patch_handle_t *handle)
10087 {
10088 status_t status = NO_ERROR;
10089
10090 // store new device and send to effects
10091 mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type;
10092 mInDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address);
10093 audio_port_handle_t deviceId = patch->sources[0].id;
10094 for (size_t i = 0; i < mEffectChains.size(); i++) {
10095 mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr());
10096 }
10097
10098 checkBtNrec_l();
10099
10100 // store new source and send to effects
10101 if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
10102 mAudioSource = patch->sinks[0].ext.mix.usecase.source;
10103 for (size_t i = 0; i < mEffectChains.size(); i++) {
10104 mEffectChains[i]->setAudioSource_l(mAudioSource);
10105 }
10106 }
10107
10108 if (mInput->audioHwDev->supportsAudioPatches()) {
10109 sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
10110 status = hwDevice->createAudioPatch(patch->num_sources,
10111 patch->sources,
10112 patch->num_sinks,
10113 patch->sinks,
10114 handle);
10115 } else {
10116 status = mInput->stream->legacyCreateAudioPatch(patch->sources[0],
10117 patch->sinks[0].ext.mix.usecase.source,
10118 patch->sources[0].ext.device.type);
10119 *handle = AUDIO_PATCH_HANDLE_NONE;
10120 }
10121
10122 if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) {
10123 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
10124 mPatch = *patch;
10125 }
10126
10127 const std::string pathSourcesAsString = patchSourcesToString(patch);
10128 mThreadMetrics.logEndInterval();
10129 mThreadMetrics.logCreatePatch(pathSourcesAsString, /* outDevices */ {});
10130 mThreadMetrics.logBeginInterval();
10131 // also dispatch to active AudioRecords
10132 for (const auto &track : mActiveTracks) {
10133 track->logEndInterval();
10134 track->logBeginInterval(pathSourcesAsString);
10135 }
10136 // Force meteadata update after a route change
10137 mActiveTracks.setHasChanged();
10138
10139 return status;
10140 }
10141
releaseAudioPatch_l(const audio_patch_handle_t handle)10142 status_t RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
10143 {
10144 status_t status = NO_ERROR;
10145
10146 mPatch = audio_patch{};
10147 mInDeviceTypeAddr.reset();
10148
10149 if (mInput->audioHwDev->supportsAudioPatches()) {
10150 sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
10151 status = hwDevice->releaseAudioPatch(handle);
10152 } else {
10153 status = mInput->stream->legacyReleaseAudioPatch();
10154 }
10155 // Force meteadata update after a route change
10156 mActiveTracks.setHasChanged();
10157
10158 return status;
10159 }
10160
updateOutDevices(const DeviceDescriptorBaseVector & outDevices)10161 void RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices)
10162 {
10163 audio_utils::lock_guard _l(mutex());
10164 mOutDevices = outDevices;
10165 mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices);
10166 for (size_t i = 0; i < mEffectChains.size(); i++) {
10167 mEffectChains[i]->setDevices_l(outDeviceTypeAddrs());
10168 }
10169 }
10170
getOldestFront_l()10171 int32_t RecordThread::getOldestFront_l()
10172 {
10173 if (mTracks.size() == 0) {
10174 return mRsmpInRear;
10175 }
10176 int32_t oldestFront = mRsmpInRear;
10177 int32_t maxFilled = 0;
10178 for (size_t i = 0; i < mTracks.size(); i++) {
10179 int32_t front = mTracks[i]->resamplerBufferProvider()->getFront();
10180 int32_t filled;
10181 (void)__builtin_sub_overflow(mRsmpInRear, front, &filled);
10182 if (filled > maxFilled) {
10183 oldestFront = front;
10184 maxFilled = filled;
10185 }
10186 }
10187 if (maxFilled > static_cast<signed>(mRsmpInFrames)) {
10188 (void)__builtin_sub_overflow(mRsmpInRear, mRsmpInFrames, &oldestFront);
10189 }
10190 return oldestFront;
10191 }
10192
updateFronts_l(int32_t offset)10193 void RecordThread::updateFronts_l(int32_t offset)
10194 {
10195 if (offset == 0) {
10196 return;
10197 }
10198 for (size_t i = 0; i < mTracks.size(); i++) {
10199 int32_t front = mTracks[i]->resamplerBufferProvider()->getFront();
10200 front = audio_utils::safe_sub_overflow(front, offset);
10201 mTracks[i]->resamplerBufferProvider()->setFront(front);
10202 }
10203 }
10204
resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs)10205 void RecordThread::resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs)
10206 {
10207 // This is the formula for calculating the temporary buffer size.
10208 // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to
10209 // 1 full output buffer, regardless of the alignment of the available input.
10210 // The value is somewhat arbitrary, and could probably be even larger.
10211 // A larger value should allow more old data to be read after a track calls start(),
10212 // without increasing latency.
10213 //
10214 // Note this is independent of the maximum downsampling ratio permitted for capture.
10215 size_t minRsmpInFrames = mFrameCount * 7;
10216
10217 // maxSharedAudioHistoryMs != 0 indicates a request to possibly make some part of the audio
10218 // capture history available to another client using the same session ID:
10219 // dimension the resampler input buffer accordingly.
10220
10221 // Get oldest client read position: getOldestFront_l() must be called before altering
10222 // mRsmpInRear, or mRsmpInFrames
10223 int32_t previousFront = getOldestFront_l();
10224 size_t previousRsmpInFramesP2 = mRsmpInFramesP2;
10225 int32_t previousRear = mRsmpInRear;
10226 mRsmpInRear = 0;
10227
10228 ALOG_ASSERT(maxSharedAudioHistoryMs >= 0
10229 && maxSharedAudioHistoryMs <= kMaxSharedAudioHistoryMs,
10230 "resizeInputBuffer_l() called with invalid max shared history %d",
10231 maxSharedAudioHistoryMs);
10232 if (maxSharedAudioHistoryMs != 0) {
10233 // resizeInputBuffer_l should never be called with a non zero shared history if the
10234 // buffer was not already allocated
10235 ALOG_ASSERT(mRsmpInBuffer != nullptr && mRsmpInFrames != 0,
10236 "resizeInputBuffer_l() called with shared history and unallocated buffer");
10237 size_t rsmpInFrames = (size_t)maxSharedAudioHistoryMs * mSampleRate / 1000;
10238 // never reduce resampler input buffer size
10239 if (rsmpInFrames <= mRsmpInFrames) {
10240 return;
10241 }
10242 mRsmpInFrames = rsmpInFrames;
10243 }
10244 mMaxSharedAudioHistoryMs = maxSharedAudioHistoryMs;
10245 // Note: mRsmpInFrames is 0 when called with maxSharedAudioHistoryMs equals to 0 so it is always
10246 // initialized
10247 if (mRsmpInFrames < minRsmpInFrames) {
10248 mRsmpInFrames = minRsmpInFrames;
10249 }
10250 mRsmpInFramesP2 = roundup(mRsmpInFrames);
10251
10252 // TODO optimize audio capture buffer sizes ...
10253 // Here we calculate the size of the sliding buffer used as a source
10254 // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7).
10255 // For current HAL frame counts, this is usually 2048 = 40 ms. It would
10256 // be better to have it derived from the pipe depth in the long term.
10257 // The current value is higher than necessary. However it should not add to latency.
10258
10259 // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer
10260 mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1;
10261
10262 void *rsmpInBuffer;
10263 (void)posix_memalign(&rsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize);
10264 // if posix_memalign fails, will segv here.
10265 memset(rsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize);
10266
10267 // Copy audio history if any from old buffer before freeing it
10268 if (previousRear != 0) {
10269 ALOG_ASSERT(mRsmpInBuffer != nullptr,
10270 "resizeInputBuffer_l() called with null buffer but frames already read from HAL");
10271
10272 ssize_t unread = audio_utils::safe_sub_overflow(previousRear, previousFront);
10273 previousFront &= previousRsmpInFramesP2 - 1;
10274 size_t part1 = previousRsmpInFramesP2 - previousFront;
10275 if (part1 > (size_t) unread) {
10276 part1 = unread;
10277 }
10278 if (part1 != 0) {
10279 memcpy(rsmpInBuffer, (const uint8_t*)mRsmpInBuffer + previousFront * mFrameSize,
10280 part1 * mFrameSize);
10281 mRsmpInRear = part1;
10282 part1 = unread - part1;
10283 if (part1 != 0) {
10284 memcpy((uint8_t*)rsmpInBuffer + mRsmpInRear * mFrameSize,
10285 (const uint8_t*)mRsmpInBuffer, part1 * mFrameSize);
10286 mRsmpInRear += part1;
10287 }
10288 }
10289 // Update front for all clients according to new rear
10290 updateFronts_l(audio_utils::safe_sub_overflow(previousRear, mRsmpInRear));
10291 } else {
10292 mRsmpInRear = 0;
10293 }
10294 free(mRsmpInBuffer);
10295 mRsmpInBuffer = rsmpInBuffer;
10296 }
10297
addPatchTrack(const sp<IAfPatchRecord> & record)10298 void RecordThread::addPatchTrack(const sp<IAfPatchRecord>& record)
10299 {
10300 audio_utils::lock_guard _l(mutex());
10301 mTracks.add(record);
10302 if (record->getSource()) {
10303 mSource = record->getSource();
10304 }
10305 }
10306
deletePatchTrack(const sp<IAfPatchRecord> & record)10307 void RecordThread::deletePatchTrack(const sp<IAfPatchRecord>& record)
10308 {
10309 audio_utils::lock_guard _l(mutex());
10310 if (mSource == record->getSource()) {
10311 mSource = mInput;
10312 }
10313 destroyTrack_l(record);
10314 }
10315
toAudioPortConfig(struct audio_port_config * config)10316 void RecordThread::toAudioPortConfig(struct audio_port_config* config)
10317 {
10318 ThreadBase::toAudioPortConfig(config);
10319 config->role = AUDIO_PORT_ROLE_SINK;
10320 config->ext.mix.hw_module = mInput->audioHwDev->handle();
10321 config->ext.mix.usecase.source = mAudioSource;
10322 if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
10323 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
10324 config->flags.input = mInput->flags;
10325 }
10326 }
10327
getLocalLogHeader() const10328 std::string RecordThread::getLocalLogHeader() const {
10329 using namespace std::literals;
10330 static constexpr auto indent = " "
10331 " "sv;
10332 return std::string{indent}.append(IAfRecordTrack::getLogHeader());
10333 }
10334
10335 // ----------------------------------------------------------------------------
10336 // Mmap
10337 // ----------------------------------------------------------------------------
10338
10339 // Mmap stream control interface implementation. Each MmapThreadHandle controls one
10340 // MmapPlaybackThread or MmapCaptureThread instance.
10341 class MmapThreadHandle : public MmapStreamInterface {
10342 public:
10343 explicit MmapThreadHandle(const sp<IAfMmapThread>& thread);
10344 ~MmapThreadHandle() override;
10345
10346 // MmapStreamInterface virtuals
10347 status_t createMmapBuffer(int32_t minSizeFrames,
10348 struct audio_mmap_buffer_info* info) final;
10349 status_t getMmapPosition(struct audio_mmap_position* position) final;
10350 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) final;
10351 status_t start(const AudioClient& client,
10352 const audio_attributes_t* attr, audio_port_handle_t* handle) final;
10353 status_t stop(audio_port_handle_t handle) final;
10354 status_t standby() final;
10355 status_t reportData(const void* buffer, size_t frameCount) final;
10356 private:
10357 const sp<IAfMmapThread> mThread;
10358 };
10359
10360 /* static */
createMmapStreamInterfaceAdapter(const sp<IAfMmapThread> & mmapThread)10361 sp<MmapStreamInterface> IAfMmapThread::createMmapStreamInterfaceAdapter(
10362 const sp<IAfMmapThread>& mmapThread) {
10363 return sp<MmapThreadHandle>::make(mmapThread);
10364 }
10365
MmapThreadHandle(const sp<IAfMmapThread> & thread)10366 MmapThreadHandle::MmapThreadHandle(const sp<IAfMmapThread>& thread)
10367 : mThread(thread)
10368 {
10369 assert(thread != 0); // thread must start non-null and stay non-null
10370 }
10371
10372 // MmapStreamInterface could be directly implemented by MmapThread excepting this
10373 // special handling on adapter dtor.
~MmapThreadHandle()10374 MmapThreadHandle::~MmapThreadHandle()
10375 {
10376 mThread->disconnect();
10377 }
10378
createMmapBuffer(int32_t minSizeFrames,struct audio_mmap_buffer_info * info)10379 status_t MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames,
10380 struct audio_mmap_buffer_info *info)
10381 {
10382 return mThread->createMmapBuffer(minSizeFrames, info);
10383 }
10384
getMmapPosition(struct audio_mmap_position * position)10385 status_t MmapThreadHandle::getMmapPosition(struct audio_mmap_position* position)
10386 {
10387 return mThread->getMmapPosition(position);
10388 }
10389
getExternalPosition(uint64_t * position,int64_t * timeNanos)10390 status_t MmapThreadHandle::getExternalPosition(uint64_t* position,
10391 int64_t *timeNanos) {
10392 return mThread->getExternalPosition(position, timeNanos);
10393 }
10394
start(const AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * handle)10395 status_t MmapThreadHandle::start(const AudioClient& client,
10396 const audio_attributes_t *attr, audio_port_handle_t *handle)
10397 {
10398 return mThread->start(client, attr, handle);
10399 }
10400
stop(audio_port_handle_t handle)10401 status_t MmapThreadHandle::stop(audio_port_handle_t handle)
10402 {
10403 return mThread->stop(handle);
10404 }
10405
standby()10406 status_t MmapThreadHandle::standby()
10407 {
10408 return mThread->standby();
10409 }
10410
reportData(const void * buffer,size_t frameCount)10411 status_t MmapThreadHandle::reportData(const void* buffer, size_t frameCount)
10412 {
10413 return mThread->reportData(buffer, frameCount);
10414 }
10415
10416
MmapThread(const sp<IAfThreadCallback> & afThreadCallback,audio_io_handle_t id,AudioHwDevice * hwDev,const sp<StreamHalInterface> & stream,bool systemReady,bool isOut)10417 MmapThread::MmapThread(
10418 const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
10419 AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady, bool isOut)
10420 : ThreadBase(afThreadCallback, id, (isOut ? MMAP_PLAYBACK : MMAP_CAPTURE), systemReady, isOut),
10421 mSessionId(AUDIO_SESSION_NONE),
10422 mPortId(AUDIO_PORT_HANDLE_NONE),
10423 mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev),
10424 mActiveTracks(&this->mLocalLog),
10425 mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later.
10426 mNoCallbackWarningCount(0)
10427 {
10428 mStandby = true;
10429 readHalParameters_l();
10430 }
10431
onFirstRef()10432 void MmapThread::onFirstRef()
10433 {
10434 run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
10435 }
10436
disconnect()10437 void MmapThread::disconnect()
10438 {
10439 ActiveTracks<IAfMmapTrack> activeTracks;
10440 audio_port_handle_t localPortId;
10441 {
10442 audio_utils::lock_guard _l(mutex());
10443 for (const sp<IAfMmapTrack>& t : mActiveTracks) {
10444 activeTracks.add(t);
10445 }
10446 localPortId = mPortId;
10447 ALOGD("%s: localPortId = %d", __func__, localPortId);
10448 mPortId = AUDIO_PORT_HANDLE_NONE;
10449 }
10450 for (const sp<IAfMmapTrack>& t : activeTracks) {
10451 ALOGD("%s: t->portId() = %d", __func__, t->portId());
10452 stop(t->portId());
10453 }
10454 // This will decrement references and may cause the destruction of this thread.
10455 if (isOutput()) {
10456 AudioSystem::releaseOutput(localPortId);
10457 } else {
10458 AudioSystem::releaseInput(localPortId);
10459 }
10460 }
10461
10462
configure_l(const audio_attributes_t * attr,audio_stream_type_t streamType __unused,audio_session_t sessionId,const sp<MmapStreamCallback> & callback,const DeviceIdVector & deviceIds,audio_port_handle_t portId)10463 void MmapThread::configure_l(const audio_attributes_t* attr,
10464 audio_stream_type_t streamType __unused,
10465 audio_session_t sessionId,
10466 const sp<MmapStreamCallback>& callback,
10467 const DeviceIdVector& deviceIds,
10468 audio_port_handle_t portId)
10469 {
10470 mAttr = *attr;
10471 mSessionId = sessionId;
10472 mCallback = callback;
10473 mDeviceIds = deviceIds;
10474 mPortId = portId;
10475 }
10476
createMmapBuffer(int32_t minSizeFrames,struct audio_mmap_buffer_info * info)10477 status_t MmapThread::createMmapBuffer(int32_t minSizeFrames,
10478 struct audio_mmap_buffer_info *info)
10479 {
10480 audio_utils::lock_guard l(mutex());
10481 if (mHalStream == 0) {
10482 return NO_INIT;
10483 }
10484 mStandby = true;
10485 return mHalStream->createMmapBuffer(minSizeFrames, info);
10486 }
10487
getMmapPosition(struct audio_mmap_position * position) const10488 status_t MmapThread::getMmapPosition(struct audio_mmap_position* position) const
10489 {
10490 audio_utils::lock_guard l(mutex());
10491 if (mHalStream == 0) {
10492 return NO_INIT;
10493 }
10494 return mHalStream->getMmapPosition(position);
10495 }
10496
exitStandby_l()10497 status_t MmapThread::exitStandby_l()
10498 {
10499 // The HAL must receive track metadata before starting the stream
10500 updateMetadata_l();
10501 status_t ret = mHalStream->start();
10502 if (ret != NO_ERROR) {
10503 ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret);
10504 return ret;
10505 }
10506 if (mStandby) {
10507 mThreadMetrics.logBeginInterval();
10508 mThreadSnapshot.onBegin();
10509 mStandby = false;
10510 }
10511 return NO_ERROR;
10512 }
10513
start(const AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * handle)10514 status_t MmapThread::start(const AudioClient& client,
10515 const audio_attributes_t *attr,
10516 audio_port_handle_t *handle)
10517 {
10518 audio_utils::lock_guard l(mutex());
10519 ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__,
10520 client.attributionSource.uid, mStandby, mPortId, *handle);
10521 if (mHalStream == 0) {
10522 return NO_INIT;
10523 }
10524
10525 status_t ret;
10526
10527 // For the first track, reuse portId and session allocated when the stream was opened.
10528 if (*handle == mPortId) {
10529 acquireWakeLock_l();
10530 return NO_ERROR;
10531 }
10532
10533 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
10534
10535 audio_io_handle_t io = mId;
10536 AttributionSourceState adjAttributionSource;
10537 if (!com::android::media::audio::audioserver_permissions()) {
10538 adjAttributionSource = afutils::checkAttributionSourcePackage(
10539 client.attributionSource);
10540 } else {
10541 // TODO(b/342475009) validate in oboeservice, and plumb downwards
10542 auto validatedRes = ValidatedAttributionSourceState::createFromTrustedUidNoPackage(
10543 client.attributionSource,
10544 mAfThreadCallback->getPermissionProvider()
10545 );
10546 if (!validatedRes.has_value()) {
10547 ALOGE("MMAP client package validation fail: %s",
10548 validatedRes.error().toString8().c_str());
10549 return aidl_utils::statusTFromBinderStatus(validatedRes.error());
10550 }
10551 adjAttributionSource = std::move(validatedRes.value()).unwrapInto();
10552 }
10553
10554 const auto localSessionId = mSessionId;
10555 auto localAttr = mAttr;
10556 float volume = 0.0f;
10557 bool muted = false;
10558 if (isOutput()) {
10559 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
10560 config.sample_rate = mSampleRate;
10561 config.channel_mask = mChannelMask;
10562 config.format = mFormat;
10563 audio_stream_type_t stream = streamType_l();
10564 audio_output_flags_t flags =
10565 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT);
10566 DeviceIdVector deviceIds = mDeviceIds;
10567 std::vector<audio_io_handle_t> secondaryOutputs;
10568 bool isSpatialized;
10569 bool isBitPerfect;
10570 mutex().unlock();
10571 ret = AudioSystem::getOutputForAttr(&localAttr, &io,
10572 localSessionId,
10573 &stream,
10574 adjAttributionSource,
10575 &config,
10576 flags,
10577 &deviceIds,
10578 &portId,
10579 &secondaryOutputs,
10580 &isSpatialized,
10581 &isBitPerfect,
10582 &volume,
10583 &muted);
10584 mutex().lock();
10585 mAttr = localAttr;
10586 ALOGD_IF(!secondaryOutputs.empty(),
10587 "MmapThread::start does not support secondary outputs, ignoring them");
10588 } else {
10589 audio_config_base_t config;
10590 config.sample_rate = mSampleRate;
10591 config.channel_mask = mChannelMask;
10592 config.format = mFormat;
10593 audio_port_handle_t deviceId = getFirstDeviceId(mDeviceIds);
10594 audio_source_t source = AUDIO_SOURCE_DEFAULT;
10595 mutex().unlock();
10596 ret = AudioSystem::getInputForAttr(&localAttr, &io,
10597 RECORD_RIID_INVALID,
10598 localSessionId,
10599 adjAttributionSource,
10600 &config,
10601 AUDIO_INPUT_FLAG_MMAP_NOIRQ,
10602 &deviceId,
10603 &portId,
10604 &source);
10605 mutex().lock();
10606 // localAttr is const for getInputForAttr.
10607 localAttr.source = source;
10608 }
10609 // APM should not chose a different input or output stream for the same set of attributes
10610 // and audo configuration
10611 if (ret != NO_ERROR || io != mId) {
10612 ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)",
10613 __FUNCTION__, ret, io, mId);
10614 return BAD_VALUE;
10615 }
10616
10617 if (isOutput()) {
10618 mutex().unlock();
10619 ret = AudioSystem::startOutput(portId);
10620 mutex().lock();
10621 } else {
10622 {
10623 // Add the track record before starting input so that the silent status for the
10624 // client can be cached.
10625 setClientSilencedState_l(portId, false /*silenced*/);
10626 }
10627 mutex().unlock();
10628 ret = AudioSystem::startInput(portId);
10629 mutex().lock();
10630 }
10631
10632 // abort if start is rejected by audio policy manager
10633 if (ret != NO_ERROR) {
10634 ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret);
10635 if (!mActiveTracks.isEmpty()) {
10636 mutex().unlock();
10637 if (isOutput()) {
10638 AudioSystem::releaseOutput(portId);
10639 } else {
10640 AudioSystem::releaseInput(portId);
10641 }
10642 mutex().lock();
10643 } else {
10644 mHalStream->stop();
10645 }
10646 eraseClientSilencedState_l(portId);
10647 return PERMISSION_DENIED;
10648 }
10649
10650 // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ?
10651 sp<IAfMmapTrack> track = IAfMmapTrack::create(
10652 this, attr == nullptr ? mAttr : *attr, mSampleRate, mFormat,
10653 mChannelMask, mSessionId, isOutput(),
10654 adjAttributionSource,
10655 IPCThreadState::self()->getCallingPid(), portId,
10656 volume, muted);
10657
10658 // MMAP tracks are only created when they are started, so mark them as Start for the purposes
10659 // of the IAfTrackBase interface
10660 track->start();
10661 if (!isOutput()) {
10662 track->setSilenced_l(isClientSilenced_l(portId));
10663 }
10664
10665 if (isOutput()) {
10666 // force volume update when a new track is added
10667 mHalVolFloat = -1.0f;
10668 } else if (!track->isSilenced_l()) {
10669 for (const sp<IAfMmapTrack>& t : mActiveTracks) {
10670 if (t->isSilenced_l()
10671 && t->uid() != static_cast<uid_t>(adjAttributionSource.uid)) {
10672 t->invalidate();
10673 }
10674 }
10675 }
10676
10677 mActiveTracks.add(track);
10678 sp<IAfEffectChain> chain = getEffectChain_l(mSessionId);
10679 if (chain != 0) {
10680 chain->setStrategy(getStrategyForStream(streamType_l()));
10681 chain->incTrackCnt();
10682 chain->incActiveTrackCnt();
10683 }
10684
10685 // log to MediaMetrics
10686 track->logBeginInterval(
10687 isOutput() ? patchSinksToString(&mPatch) : patchSourcesToString(&mPatch));
10688 *handle = portId;
10689
10690 if (mActiveTracks.size() == 1) {
10691 ret = exitStandby_l();
10692 }
10693
10694 broadcast_l();
10695
10696 ALOGV("%s DONE status %d handle %d stream %p", __FUNCTION__, ret, *handle, mHalStream.get());
10697
10698 return ret;
10699 }
10700
stop(audio_port_handle_t handle)10701 status_t MmapThread::stop(audio_port_handle_t handle)
10702 {
10703 ALOGV("%s handle %d", __FUNCTION__, handle);
10704 audio_utils::lock_guard l(mutex());
10705
10706 if (mHalStream == 0) {
10707 return NO_INIT;
10708 }
10709
10710 if (handle == mPortId) {
10711 releaseWakeLock_l();
10712 return NO_ERROR;
10713 }
10714
10715 sp<IAfMmapTrack> track;
10716 for (const sp<IAfMmapTrack>& t : mActiveTracks) {
10717 if (handle == t->portId()) {
10718 track = t;
10719 break;
10720 }
10721 }
10722 if (track == 0) {
10723 return BAD_VALUE;
10724 }
10725
10726 mActiveTracks.remove(track);
10727 eraseClientSilencedState_l(track->portId());
10728 track->stop();
10729
10730 mutex().unlock();
10731 if (isOutput()) {
10732 AudioSystem::stopOutput(track->portId());
10733 AudioSystem::releaseOutput(track->portId());
10734 } else {
10735 AudioSystem::stopInput(track->portId());
10736 AudioSystem::releaseInput(track->portId());
10737 }
10738 mutex().lock();
10739
10740 sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId());
10741 if (chain != 0) {
10742 chain->decActiveTrackCnt();
10743 chain->decTrackCnt();
10744 }
10745
10746 if (mActiveTracks.isEmpty()) {
10747 mHalStream->stop();
10748 }
10749
10750 broadcast_l();
10751
10752 return NO_ERROR;
10753 }
10754
standby()10755 status_t MmapThread::standby()
10756 NO_THREAD_SAFETY_ANALYSIS // clang bug
10757 {
10758 ALOGV("%s", __FUNCTION__);
10759 audio_utils::lock_guard l_{mutex()};
10760
10761 if (mHalStream == 0) {
10762 return NO_INIT;
10763 }
10764 if (!mActiveTracks.isEmpty()) {
10765 return INVALID_OPERATION;
10766 }
10767 mHalStream->standby();
10768 if (!mStandby) {
10769 mThreadMetrics.logEndInterval();
10770 mThreadSnapshot.onEnd();
10771 mStandby = true;
10772 }
10773 releaseWakeLock_l();
10774 return NO_ERROR;
10775 }
10776
reportData(const void *,size_t)10777 status_t MmapThread::reportData(const void* /*buffer*/, size_t /*frameCount*/) {
10778 // This is a stub implementation. The MmapPlaybackThread overrides this function.
10779 return INVALID_OPERATION;
10780 }
10781
readHalParameters_l()10782 void MmapThread::readHalParameters_l()
10783 {
10784 status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
10785 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
10786 mFormat = mHALFormat;
10787 LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat);
10788 result = mHalStream->getFrameSize(&mFrameSize);
10789 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
10790 LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero",
10791 mFrameSize);
10792 result = mHalStream->getBufferSize(&mBufferSize);
10793 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
10794 mFrameCount = mBufferSize / mFrameSize;
10795
10796 // TODO: make a readHalParameters call?
10797 mediametrics::LogItem item(mThreadMetrics.getMetricsId());
10798 item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS)
10799 .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str())
10800 .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate)
10801 .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask)
10802 .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount)
10803 .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount)
10804 /*
10805 .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str())
10806 .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK,
10807 (int32_t)mHapticChannelMask)
10808 .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT,
10809 (int32_t)mHapticChannelCount)
10810 */
10811 .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING,
10812 IAfThreadBase::formatToString(mHALFormat).c_str())
10813 .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT,
10814 (int32_t)mFrameCount) // sic - added HAL
10815 .record();
10816 }
10817
threadLoop()10818 bool MmapThread::threadLoop()
10819 {
10820 {
10821 audio_utils::unique_lock _l(mutex());
10822 checkSilentMode_l();
10823 }
10824
10825 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
10826
10827 while (!exitPending())
10828 {
10829 Vector<sp<IAfEffectChain>> effectChains;
10830
10831 { // under Thread lock
10832 audio_utils::unique_lock _l(mutex());
10833
10834 if (mSignalPending) {
10835 // A signal was raised while we were unlocked
10836 mSignalPending = false;
10837 } else {
10838 if (mConfigEvents.isEmpty()) {
10839 // we're about to wait, flush the binder command buffer
10840 IPCThreadState::self()->flushCommands();
10841
10842 if (exitPending()) {
10843 break;
10844 }
10845
10846 // wait until we have something to do...
10847 ALOGV("%s going to sleep", myName.c_str());
10848 mWaitWorkCV.wait(_l);
10849 ALOGV("%s waking up", myName.c_str());
10850
10851 checkSilentMode_l();
10852
10853 continue;
10854 }
10855 }
10856
10857 processConfigEvents_l();
10858
10859 processVolume_l();
10860
10861 checkInvalidTracks_l();
10862
10863 mActiveTracks.updatePowerState_l(this);
10864
10865 updateMetadata_l();
10866
10867 lockEffectChains_l(effectChains);
10868 } // release Thread lock
10869
10870 for (size_t i = 0; i < effectChains.size(); i ++) {
10871 effectChains[i]->process_l(); // Thread is not locked, but effect chain is locked
10872 }
10873
10874 // enable changes in effect chain, including moving to another thread.
10875 unlockEffectChains(effectChains);
10876 // Effect chains will be actually deleted here if they were removed from
10877 // mEffectChains list during mixing or effects processing
10878 mThreadloopExecutor.process();
10879 }
10880 mThreadloopExecutor.process(); // process any remaining deferred actions.
10881 // deferred actions after this point are ignored.
10882
10883 threadLoop_exit();
10884
10885 if (!mStandby) {
10886 threadLoop_standby();
10887 mStandby = true;
10888 }
10889
10890 ALOGV("Thread %p type %d exiting", this, mType);
10891 return false;
10892 }
10893
10894 // checkForNewParameter_l() must be called with ThreadBase::mutex() held
checkForNewParameter_l(const String8 & keyValuePair,status_t & status)10895 bool MmapThread::checkForNewParameter_l(const String8& keyValuePair,
10896 status_t& status)
10897 {
10898 AudioParameter param = AudioParameter(keyValuePair);
10899 int value;
10900 bool sendToHal = true;
10901 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
10902 LOG_FATAL("Should not happen set routing device in MmapThread");
10903 }
10904 if (sendToHal) {
10905 status = mHalStream->setParameters(keyValuePair);
10906 } else {
10907 status = NO_ERROR;
10908 }
10909
10910 return false;
10911 }
10912
getParameters(const String8 & keys)10913 String8 MmapThread::getParameters(const String8& keys)
10914 {
10915 audio_utils::lock_guard _l(mutex());
10916 String8 out_s8;
10917 if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) {
10918 return out_s8;
10919 }
10920 return {};
10921 }
10922
ioConfigChanged_l(audio_io_config_event_t event,pid_t pid,audio_port_handle_t portId __unused)10923 void MmapThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid,
10924 audio_port_handle_t portId __unused) {
10925 sp<AudioIoDescriptor> desc;
10926 bool isInput = false;
10927 switch (event) {
10928 case AUDIO_INPUT_OPENED:
10929 case AUDIO_INPUT_REGISTERED:
10930 case AUDIO_INPUT_CONFIG_CHANGED:
10931 isInput = true;
10932 FALLTHROUGH_INTENDED;
10933 case AUDIO_OUTPUT_OPENED:
10934 case AUDIO_OUTPUT_REGISTERED:
10935 case AUDIO_OUTPUT_CONFIG_CHANGED:
10936 desc = sp<AudioIoDescriptor>::make(mId, mPatch, isInput,
10937 mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount);
10938 break;
10939 case AUDIO_INPUT_CLOSED:
10940 case AUDIO_OUTPUT_CLOSED:
10941 default:
10942 desc = sp<AudioIoDescriptor>::make(mId);
10943 break;
10944 }
10945 mAfThreadCallback->ioConfigChanged_l(event, desc, pid);
10946 }
10947
createAudioPatch_l(const struct audio_patch * patch,audio_patch_handle_t * handle)10948 status_t MmapThread::createAudioPatch_l(const struct audio_patch* patch,
10949 audio_patch_handle_t *handle)
10950 NO_THREAD_SAFETY_ANALYSIS // elease and re-acquire mutex()
10951 {
10952 status_t status = NO_ERROR;
10953
10954 // store new device and send to effects
10955 audio_devices_t type = AUDIO_DEVICE_NONE;
10956 DeviceIdVector deviceIds;
10957 AudioDeviceTypeAddrVector sinkDeviceTypeAddrs;
10958 AudioDeviceTypeAddr sourceDeviceTypeAddr;
10959 uint32_t numDevices = 0;
10960 if (isOutput()) {
10961 for (unsigned int i = 0; i < patch->num_sinks; i++) {
10962 LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1
10963 && !mAudioHwDev->supportsAudioPatches(),
10964 "Enumerated device type(%#x) must not be used "
10965 "as it does not support audio patches",
10966 patch->sinks[i].ext.device.type);
10967 type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type);
10968 sinkDeviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type,
10969 patch->sinks[i].ext.device.address);
10970 deviceIds.push_back(patch->sinks[i].id);
10971 }
10972 numDevices = mPatch.num_sinks;
10973 } else {
10974 type = patch->sources[0].ext.device.type;
10975 deviceIds.push_back(patch->sources[0].id);
10976 numDevices = mPatch.num_sources;
10977 sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type;
10978 sourceDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address);
10979 }
10980
10981 for (size_t i = 0; i < mEffectChains.size(); i++) {
10982 if (isOutput()) {
10983 mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs);
10984 } else {
10985 mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr);
10986 }
10987 }
10988
10989 if (!isOutput()) {
10990 // store new source and send to effects
10991 if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
10992 mAudioSource = patch->sinks[0].ext.mix.usecase.source;
10993 for (size_t i = 0; i < mEffectChains.size(); i++) {
10994 mEffectChains[i]->setAudioSource_l(mAudioSource);
10995 }
10996 }
10997 }
10998
10999 // For mmap streams, once the routing has changed, they will be disconnected. It should be
11000 // okay to notify the client earlier before the new patch creation.
11001 if (!areDeviceIdsEqual(deviceIds, mDeviceIds)) {
11002 if (const sp<MmapStreamCallback> callback = mCallback.promote()) {
11003 // The aaudioservice handle the routing changed event asynchronously. In that case,
11004 // it is safe to hold the lock here.
11005 callback->onRoutingChanged(deviceIds);
11006 }
11007 }
11008
11009 if (mAudioHwDev->supportsAudioPatches()) {
11010 status = mHalDevice->createAudioPatch(patch->num_sources, patch->sources, patch->num_sinks,
11011 patch->sinks, handle);
11012 } else {
11013 audio_port_config port;
11014 std::optional<audio_source_t> source;
11015 if (isOutput()) {
11016 port = patch->sinks[0];
11017 } else {
11018 port = patch->sources[0];
11019 source = patch->sinks[0].ext.mix.usecase.source;
11020 }
11021 status = mHalStream->legacyCreateAudioPatch(port, source, type);
11022 *handle = AUDIO_PATCH_HANDLE_NONE;
11023 }
11024
11025 if (numDevices == 0 || (!areDeviceIdsEqual(deviceIds, mDeviceIds))) {
11026 if (isOutput()) {
11027 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
11028 mOutDeviceTypeAddrs = sinkDeviceTypeAddrs;
11029 checkSilentMode_l();
11030 } else {
11031 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
11032 mInDeviceTypeAddr = sourceDeviceTypeAddr;
11033 }
11034 mPatch = *patch;
11035 mDeviceIds = deviceIds;
11036 }
11037 // Force meteadata update after a route change
11038 mActiveTracks.setHasChanged();
11039
11040 const std::string patchSourcesAsString = isOutput() ? "" : patchSourcesToString(patch);
11041 const std::string patchSinksAsString = isOutput() ? patchSinksToString(patch) : "";
11042 mThreadMetrics.logEndInterval();
11043 mThreadMetrics.logCreatePatch(patchSourcesAsString, patchSinksAsString);
11044 mThreadMetrics.logBeginInterval();
11045 for (const auto &track : mActiveTracks) {
11046 track->logEndInterval();
11047 track->logBeginInterval(isOutput() ? patchSinksAsString : patchSourcesAsString);
11048 }
11049
11050 return status;
11051 }
11052
releaseAudioPatch_l(const audio_patch_handle_t handle)11053 status_t MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
11054 {
11055 status_t status = NO_ERROR;
11056
11057 mPatch = audio_patch{};
11058 mOutDeviceTypeAddrs.clear();
11059 mInDeviceTypeAddr.reset();
11060
11061 bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ?
11062 supportsAudioPatches : false;
11063
11064 if (supportsAudioPatches) {
11065 status = mHalDevice->releaseAudioPatch(handle);
11066 } else {
11067 status = mHalStream->legacyReleaseAudioPatch();
11068 }
11069 // Force meteadata update after a route change
11070 mActiveTracks.setHasChanged();
11071
11072 return status;
11073 }
11074
toAudioPortConfig(struct audio_port_config * config)11075 void MmapThread::toAudioPortConfig(struct audio_port_config* config)
11076 NO_THREAD_SAFETY_ANALYSIS // mAudioHwDev handle access
11077 {
11078 ThreadBase::toAudioPortConfig(config);
11079 if (isOutput()) {
11080 config->role = AUDIO_PORT_ROLE_SOURCE;
11081 config->ext.mix.hw_module = mAudioHwDev->handle();
11082 config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
11083 } else {
11084 config->role = AUDIO_PORT_ROLE_SINK;
11085 config->ext.mix.hw_module = mAudioHwDev->handle();
11086 config->ext.mix.usecase.source = mAudioSource;
11087 }
11088 }
11089
addEffectChain_l(const sp<IAfEffectChain> & chain)11090 status_t MmapThread::addEffectChain_l(const sp<IAfEffectChain>& chain)
11091 {
11092 audio_session_t session = chain->sessionId();
11093
11094 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
11095 // Attach all tracks with same session ID to this chain.
11096 // indicate all active tracks in the chain
11097 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11098 if (session == track->sessionId()) {
11099 chain->incTrackCnt();
11100 chain->incActiveTrackCnt();
11101 }
11102 }
11103
11104 chain->setThread(this);
11105 chain->setInBuffer(nullptr);
11106 chain->setOutBuffer(nullptr);
11107 chain->syncHalEffectsState_l();
11108
11109 mEffectChains.add(chain);
11110 checkSuspendOnAddEffectChain_l(chain);
11111 return NO_ERROR;
11112 }
11113
removeEffectChain_l(const sp<IAfEffectChain> & chain)11114 size_t MmapThread::removeEffectChain_l(const sp<IAfEffectChain>& chain)
11115 {
11116 audio_session_t session = chain->sessionId();
11117
11118 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
11119
11120 for (size_t i = 0; i < mEffectChains.size(); i++) {
11121 if (chain == mEffectChains[i]) {
11122 mEffectChains.removeAt(i);
11123 // detach all active tracks from the chain
11124 // detach all tracks with same session ID from this chain
11125 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11126 if (session == track->sessionId()) {
11127 chain->decActiveTrackCnt();
11128 chain->decTrackCnt();
11129 }
11130 }
11131 break;
11132 }
11133 }
11134 return mEffectChains.size();
11135 }
11136
threadLoop_standby()11137 void MmapThread::threadLoop_standby()
11138 {
11139 mHalStream->standby();
11140 }
11141
threadLoop_exit()11142 void MmapThread::threadLoop_exit()
11143 {
11144 // Do not call callback->onTearDown() because it is redundant for thread exit
11145 // and because it can cause a recursive mutex lock on stop().
11146 }
11147
setSyncEvent(const sp<SyncEvent> &)11148 status_t MmapThread::setSyncEvent(const sp<SyncEvent>& /* event */)
11149 {
11150 return BAD_VALUE;
11151 }
11152
isValidSyncEvent(const sp<SyncEvent> &) const11153 bool MmapThread::isValidSyncEvent(
11154 const sp<SyncEvent>& /* event */) const
11155 {
11156 return false;
11157 }
11158
checkEffectCompatibility_l(const effect_descriptor_t * desc,audio_session_t sessionId)11159 status_t MmapThread::checkEffectCompatibility_l(
11160 const effect_descriptor_t *desc, audio_session_t sessionId)
11161 {
11162 // No global effect sessions on mmap threads
11163 if (audio_is_global_session(sessionId)) {
11164 ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s",
11165 desc->name, mThreadName);
11166 return BAD_VALUE;
11167 }
11168
11169 if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) {
11170 ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread",
11171 desc->name);
11172 return BAD_VALUE;
11173 }
11174 if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
11175 ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap "
11176 "thread", desc->name);
11177 return BAD_VALUE;
11178 }
11179
11180 // Only allow effects without processing load or latency
11181 if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) {
11182 return BAD_VALUE;
11183 }
11184
11185 if (IAfEffectModule::isHapticGenerator(&desc->type)) {
11186 ALOGE("%s(): HapticGenerator is not supported for MmapThread", __func__);
11187 return BAD_VALUE;
11188 }
11189
11190 return NO_ERROR;
11191 }
11192
checkInvalidTracks_l()11193 void MmapThread::checkInvalidTracks_l()
11194 {
11195 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11196 if (track->isInvalid()) {
11197 if (const sp<MmapStreamCallback> callback = mCallback.promote()) {
11198 // The aaudioservice handle the routing changed event asynchronously. In that case,
11199 // it is safe to hold the lock here.
11200 callback->onRoutingChanged({});
11201 } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
11202 ALOGW("Could not notify MMAP stream tear down: no onRoutingChanged callback!");
11203 mNoCallbackWarningCount++;
11204 }
11205 break;
11206 }
11207 }
11208 }
11209
dumpInternals_l(int fd,const Vector<String16> &)11210 void MmapThread::dumpInternals_l(int fd, const Vector<String16>& /* args */)
11211 {
11212 dprintf(fd, " Attributes: content type %d usage %d source %d\n",
11213 mAttr.content_type, mAttr.usage, mAttr.source);
11214 dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId);
11215 if (mActiveTracks.isEmpty()) {
11216 dprintf(fd, " No active clients\n");
11217 }
11218 }
11219
dumpTracks_l(int fd,const Vector<String16> &)11220 void MmapThread::dumpTracks_l(int fd, const Vector<String16>& /* args */)
11221 {
11222 String8 result;
11223 size_t numtracks = mActiveTracks.size();
11224 dprintf(fd, " %zu Tracks\n", numtracks);
11225 const char *prefix = " ";
11226 if (numtracks) {
11227 result.append(prefix);
11228 mActiveTracks[0]->appendDumpHeader(result);
11229 for (size_t i = 0; i < numtracks ; ++i) {
11230 sp<IAfMmapTrack> track = mActiveTracks[i];
11231 result.append(prefix);
11232 track->appendDump(result, true /* active */);
11233 }
11234 } else {
11235 dprintf(fd, "\n");
11236 }
11237 write(fd, result.c_str(), result.size());
11238 }
11239
getLocalLogHeader() const11240 std::string MmapThread::getLocalLogHeader() const {
11241 using namespace std::literals;
11242 static constexpr auto indent = " "
11243 " "sv;
11244 return std::string{indent}.append(IAfMmapTrack::getLogHeader());
11245 }
11246
11247 /* static */
create(const sp<IAfThreadCallback> & afThreadCallback,audio_io_handle_t id,AudioHwDevice * hwDev,AudioStreamOut * output,bool systemReady)11248 sp<IAfMmapPlaybackThread> IAfMmapPlaybackThread::create(
11249 const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
11250 AudioHwDevice* hwDev, AudioStreamOut* output, bool systemReady) {
11251 return sp<MmapPlaybackThread>::make(afThreadCallback, id, hwDev, output, systemReady);
11252 }
11253
MmapPlaybackThread(const sp<IAfThreadCallback> & afThreadCallback,audio_io_handle_t id,AudioHwDevice * hwDev,AudioStreamOut * output,bool systemReady)11254 MmapPlaybackThread::MmapPlaybackThread(
11255 const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
11256 AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady)
11257 : MmapThread(afThreadCallback, id, hwDev, output->stream, systemReady, true /* isOut */),
11258 mStreamType(AUDIO_STREAM_MUSIC),
11259 mOutput(output)
11260 {
11261 snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id);
11262 mFlagsAsString = toString(output->flags);
11263 mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
11264 mMasterVolume = afThreadCallback->masterVolume_l();
11265 mMasterMute = afThreadCallback->masterMute_l();
11266 if (!audioserver_flags::portid_volume_management()) {
11267 for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) {
11268 const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)};
11269 mStreamTypes[stream].volume = 0.0f;
11270 mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream);
11271 }
11272 // Audio patch and call assistant volume are always max
11273 mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f;
11274 mStreamTypes[AUDIO_STREAM_PATCH].mute = false;
11275 mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f;
11276 mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false;
11277 }
11278 if (mAudioHwDev) {
11279 if (mAudioHwDev->canSetMasterVolume()) {
11280 mMasterVolume = 1.0;
11281 }
11282
11283 if (mAudioHwDev->canSetMasterMute()) {
11284 mMasterMute = false;
11285 }
11286 }
11287 }
11288
configure(const audio_attributes_t * attr,audio_stream_type_t streamType,audio_session_t sessionId,const sp<MmapStreamCallback> & callback,const DeviceIdVector & deviceIds,audio_port_handle_t portId)11289 void MmapPlaybackThread::configure(const audio_attributes_t* attr,
11290 audio_stream_type_t streamType,
11291 audio_session_t sessionId,
11292 const sp<MmapStreamCallback>& callback,
11293 const DeviceIdVector& deviceIds,
11294 audio_port_handle_t portId)
11295 {
11296 audio_utils::lock_guard l(mutex());
11297 MmapThread::configure_l(attr, streamType, sessionId, callback, deviceIds, portId);
11298 mStreamType = streamType;
11299 }
11300
clearOutput()11301 AudioStreamOut* MmapPlaybackThread::clearOutput()
11302 {
11303 audio_utils::lock_guard _l(mutex());
11304 AudioStreamOut *output = mOutput;
11305 mOutput = NULL;
11306 return output;
11307 }
11308
setMasterVolume(float value)11309 void MmapPlaybackThread::setMasterVolume(float value)
11310 {
11311 audio_utils::lock_guard _l(mutex());
11312 // Don't apply master volume in SW if our HAL can do it for us.
11313 if (mAudioHwDev &&
11314 mAudioHwDev->canSetMasterVolume()) {
11315 mMasterVolume = 1.0;
11316 } else {
11317 mMasterVolume = value;
11318 }
11319 }
11320
setMasterMute(bool muted)11321 void MmapPlaybackThread::setMasterMute(bool muted)
11322 {
11323 audio_utils::lock_guard _l(mutex());
11324 // Don't apply master mute in SW if our HAL can do it for us.
11325 if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) {
11326 mMasterMute = false;
11327 } else {
11328 mMasterMute = muted;
11329 }
11330 }
11331
setStreamVolume(audio_stream_type_t stream,float value,bool muted)11332 void MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value, bool muted)
11333 {
11334 ALOGV("%s: stream %d value %f muted %d", __func__, stream, value, muted);
11335 audio_utils::lock_guard _l(mutex());
11336 mStreamTypes[stream].volume = value;
11337 if (com_android_media_audio_ring_my_car()) {
11338 mStreamTypes[stream].mute = muted;
11339 }
11340 if (stream == mStreamType) {
11341 broadcast_l();
11342 }
11343 }
11344
streamVolume(audio_stream_type_t stream) const11345 float MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const
11346 {
11347 audio_utils::lock_guard _l(mutex());
11348 return mStreamTypes[stream].volume;
11349 }
11350
setStreamMute(audio_stream_type_t stream,bool muted)11351 void MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
11352 {
11353 audio_utils::lock_guard _l(mutex());
11354 mStreamTypes[stream].mute = muted;
11355 if (stream == mStreamType) {
11356 broadcast_l();
11357 }
11358 }
11359
setPortsVolume(const std::vector<audio_port_handle_t> & portIds,float volume,bool muted)11360 status_t MmapPlaybackThread::setPortsVolume(
11361 const std::vector<audio_port_handle_t>& portIds, float volume, bool muted) {
11362 audio_utils::lock_guard _l(mutex());
11363 for (const auto& portId : portIds) {
11364 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11365 if (portId == track->portId()) {
11366 track->setPortVolume(volume);
11367 track->setPortMute(muted);
11368 break;
11369 }
11370 }
11371 }
11372 broadcast_l();
11373 return NO_ERROR;
11374 }
11375
checkUpdateTrackMetadataForUid(uid_t uid)11376 void MmapPlaybackThread::checkUpdateTrackMetadataForUid(uid_t uid) {
11377 audio_utils::lock_guard _l(mutex());
11378 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11379 if (track->uid() == uid) {
11380 track->setMetadataHasChanged();
11381 }
11382 }
11383 }
11384
invalidateTracks(audio_stream_type_t streamType)11385 void MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType)
11386 {
11387 audio_utils::lock_guard _l(mutex());
11388 if (streamType == mStreamType) {
11389 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11390 track->invalidate();
11391 }
11392 broadcast_l();
11393 }
11394 }
11395
invalidateTracks(std::set<audio_port_handle_t> & portIds)11396 void MmapPlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds)
11397 {
11398 audio_utils::lock_guard _l(mutex());
11399 bool trackMatch = false;
11400 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11401 if (portIds.find(track->portId()) != portIds.end()) {
11402 track->invalidate();
11403 trackMatch = true;
11404 portIds.erase(track->portId());
11405 }
11406 if (portIds.empty()) {
11407 break;
11408 }
11409 }
11410 if (trackMatch) {
11411 broadcast_l();
11412 }
11413 }
11414
processVolume_l()11415 void MmapPlaybackThread::processVolume_l()
11416 NO_THREAD_SAFETY_ANALYSIS // access of track->processMuteEvent
11417 {
11418 float volume = 0;
11419 if (!audioserver_flags::portid_volume_management()) {
11420 if (mMasterMute || streamMuted_l()) {
11421 volume = 0;
11422 } else {
11423 volume = mMasterVolume * streamVolume_l();
11424 }
11425 } else {
11426 if (mMasterMute) {
11427 volume = 0;
11428 } else {
11429 // All mmap tracks are declared with the same audio attributes to the audio policy
11430 // manager. Hence, they follow the same routing / volume group. Any change of volume
11431 // will be broadcasted to all tracks. Thus, take arbitrarily first track volume.
11432 size_t numtracks = mActiveTracks.size();
11433 if (numtracks) {
11434 if (mActiveTracks[0]->getPortMute()) {
11435 volume = 0;
11436 } else {
11437 volume = mMasterVolume * mActiveTracks[0]->getPortVolume();
11438 }
11439 }
11440 }
11441 }
11442
11443 bool shouldMutePlaybackHardening = std::all_of(mActiveTracks.begin(), mActiveTracks.end(),
11444 [](const auto& x) { return x->isPlaybackRestrictedControl(); });
11445 if (shouldMutePlaybackHardening) {
11446 volume = 0;
11447 }
11448
11449 if (volume != mHalVolFloat) {
11450 // Convert volumes from float to 8.24
11451 uint32_t vol = (uint32_t)(volume * (1 << 24));
11452
11453 // Delegate volume control to effect in track effect chain if needed
11454 // only one effect chain can be present on DirectOutputThread, so if
11455 // there is one, the track is connected to it
11456 if (!mEffectChains.isEmpty()) {
11457 mEffectChains[0]->setVolume(&vol, &vol);
11458 volume = (float)vol / (1 << 24);
11459 }
11460 // Try to use HW volume control and fall back to SW control if not implemented
11461 if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) {
11462 mHalVolFloat = volume; // HW volume control worked, so update value.
11463 mNoCallbackWarningCount = 0;
11464 } else {
11465 sp<MmapStreamCallback> callback = mCallback.promote();
11466 if (callback != 0) {
11467 mHalVolFloat = volume; // SW volume control worked, so update value.
11468 mNoCallbackWarningCount = 0;
11469 mutex().unlock();
11470 callback->onVolumeChanged(volume);
11471 mutex().lock();
11472 } else {
11473 if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
11474 ALOGW("Could not set MMAP stream volume: no volume callback!");
11475 mNoCallbackWarningCount++;
11476 }
11477 }
11478 }
11479 const auto amn = mAfThreadCallback->getAudioManagerNative();
11480 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11481 track->setMetadataHasChanged();
11482 if (amn) {
11483 if (!audioserver_flags::portid_volume_management()) {
11484 track->processMuteEvent(*amn,
11485 /*muteState=*/{mMasterMute,
11486 streamVolume_l() == 0.f,
11487 streamMuted_l(),
11488 // TODO(b/241533526): adjust logic to include mute from AppOps
11489 false /*muteFromPlaybackRestricted*/,
11490 false /*muteFromClientVolume*/,
11491 false /*muteFromVolumeShaper*/,
11492 false /*muteFromPortVolume*/,
11493 shouldMutePlaybackHardening});
11494 } else {
11495 track->processMuteEvent(*amn,
11496 /*muteState=*/{mMasterMute,
11497 track->getPortVolume() == 0.f,
11498 /* muteFromStreamMuted= */ false,
11499 // TODO(b/241533526): adjust logic to include mute from AppOp
11500 false /*muteFromPlaybackRestricted*/,
11501 false /*muteFromClientVolume*/,
11502 false /*muteFromVolumeShaper*/,
11503 track->getPortMute(),
11504 shouldMutePlaybackHardening});
11505 }
11506 track->maybeLogPlaybackHardening(*amn);
11507 }
11508 }
11509 }
11510 }
11511
updateMetadata_l()11512 ThreadBase::MetadataUpdate MmapPlaybackThread::updateMetadata_l()
11513 {
11514 if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) {
11515 return {}; // nothing to do
11516 }
11517 StreamOutHalInterface::SourceMetadata metadata;
11518 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11519 // No track is invalid as this is called after prepareTrack_l in the same critical section
11520 playback_track_metadata_v7_t trackMetadata;
11521 trackMetadata.base = {
11522 .usage = track->attributes().usage,
11523 .content_type = track->attributes().content_type,
11524 .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume
11525 };
11526 trackMetadata.channel_mask = track->channelMask();
11527 std::string tagStr(track->attributes().tags);
11528 if (audioserver_flags::enable_gmap_mode() && track->attributes().usage == AUDIO_USAGE_GAME
11529 && afThreadCallback()->hasAlreadyCaptured(track->uid())
11530 && (tagStr.size() + strlen(AUDIO_ATTRIBUTES_TAG_GMAP_BIDIRECTIONAL)
11531 + (tagStr.size() ? 1 : 0))
11532 < AUDIO_ATTRIBUTES_TAGS_MAX_SIZE) {
11533
11534 if (tagStr.size() != 0) {
11535 tagStr.append(1, AUDIO_ATTRIBUTES_TAGS_SEPARATOR);
11536 }
11537 tagStr.append(AUDIO_ATTRIBUTES_TAG_GMAP_BIDIRECTIONAL);
11538 }
11539 strncpy(trackMetadata.tags, tagStr.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
11540 trackMetadata.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1] = '\0';
11541 metadata.tracks.push_back(trackMetadata);
11542 }
11543 mOutput->stream->updateSourceMetadata(metadata);
11544
11545 MetadataUpdate change;
11546 change.playbackMetadataUpdate = metadata.tracks;
11547 return change;
11548 };
11549
checkSilentMode_l()11550 void MmapPlaybackThread::checkSilentMode_l()
11551 {
11552 if (property_get_bool("ro.audio.silent", false)) {
11553 ALOGW("ro.audio.silent is now ignored");
11554 }
11555 }
11556
toAudioPortConfig(struct audio_port_config * config)11557 void MmapPlaybackThread::toAudioPortConfig(struct audio_port_config* config)
11558 {
11559 MmapThread::toAudioPortConfig(config);
11560 if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
11561 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
11562 config->flags.output = mOutput->flags;
11563 }
11564 }
11565
getExternalPosition(uint64_t * position,int64_t * timeNanos) const11566 status_t MmapPlaybackThread::getExternalPosition(uint64_t* position,
11567 int64_t* timeNanos) const
11568 {
11569 if (mOutput == nullptr) {
11570 return NO_INIT;
11571 }
11572 struct timespec timestamp;
11573 status_t status = mOutput->getPresentationPosition(position, ×tamp);
11574 if (status == NO_ERROR) {
11575 *timeNanos = timestamp.tv_sec * NANOS_PER_SECOND + timestamp.tv_nsec;
11576 }
11577 return status;
11578 }
11579
reportData(const void * buffer,size_t frameCount)11580 status_t MmapPlaybackThread::reportData(const void* buffer, size_t frameCount) {
11581 // Send to MelProcessor for sound dose measurement.
11582 auto processor = mMelProcessor.load();
11583 if (processor) {
11584 processor->process(buffer, frameCount * mFrameSize);
11585 }
11586
11587 return NO_ERROR;
11588 }
11589
11590 // startMelComputation_l() must be called with AudioFlinger::mutex() held
startMelComputation_l(const sp<audio_utils::MelProcessor> & processor)11591 void MmapPlaybackThread::startMelComputation_l(
11592 const sp<audio_utils::MelProcessor>& processor)
11593 {
11594 ALOGV("%s: starting mel processor for thread %d", __func__, id());
11595 mMelProcessor.store(processor);
11596 if (processor) {
11597 processor->resume();
11598 }
11599
11600 // no need to update output format for MMapPlaybackThread since it is
11601 // assigned constant for each thread
11602 }
11603
11604 // stopMelComputation_l() must be called with AudioFlinger::mutex() held
stopMelComputation_l()11605 void MmapPlaybackThread::stopMelComputation_l()
11606 {
11607 ALOGV("%s: pausing mel processor for thread %d", __func__, id());
11608 auto melProcessor = mMelProcessor.load();
11609 if (melProcessor != nullptr) {
11610 melProcessor->pause();
11611 }
11612 }
11613
dumpInternals_l(int fd,const Vector<String16> & args)11614 void MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args)
11615 {
11616 MmapThread::dumpInternals_l(fd, args);
11617 if (!audioserver_flags::portid_volume_management()) {
11618 dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d",
11619 mStreamType, streamVolume_l(), mHalVolFloat, streamMuted_l());
11620 } else {
11621 dprintf(fd, " HAL volume: %f", mHalVolFloat);
11622 }
11623 dprintf(fd, "\n");
11624 dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute);
11625 }
11626
11627 /* static */
create(const sp<IAfThreadCallback> & afThreadCallback,audio_io_handle_t id,AudioHwDevice * hwDev,AudioStreamIn * input,bool systemReady)11628 sp<IAfMmapCaptureThread> IAfMmapCaptureThread::create(
11629 const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
11630 AudioHwDevice* hwDev, AudioStreamIn* input, bool systemReady) {
11631 return sp<MmapCaptureThread>::make(afThreadCallback, id, hwDev, input, systemReady);
11632 }
11633
MmapCaptureThread(const sp<IAfThreadCallback> & afThreadCallback,audio_io_handle_t id,AudioHwDevice * hwDev,AudioStreamIn * input,bool systemReady)11634 MmapCaptureThread::MmapCaptureThread(
11635 const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
11636 AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady)
11637 : MmapThread(afThreadCallback, id, hwDev, input->stream, systemReady, false /* isOut */),
11638 mInput(input)
11639 {
11640 snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id);
11641 mFlagsAsString = toString(input->flags);
11642 mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
11643 }
11644
exitStandby_l()11645 status_t MmapCaptureThread::exitStandby_l()
11646 {
11647 {
11648 // mInput might have been cleared by clearInput()
11649 if (mInput != nullptr && mInput->stream != nullptr) {
11650 mInput->stream->setGain(1.0f);
11651 }
11652 }
11653 return MmapThread::exitStandby_l();
11654 }
11655
clearInput()11656 AudioStreamIn* MmapCaptureThread::clearInput()
11657 {
11658 audio_utils::lock_guard _l(mutex());
11659 AudioStreamIn *input = mInput;
11660 mInput = NULL;
11661 return input;
11662 }
11663
processVolume_l()11664 void MmapCaptureThread::processVolume_l()
11665 {
11666 bool changed = false;
11667 bool silenced = false;
11668
11669 sp<MmapStreamCallback> callback = mCallback.promote();
11670 if (callback == 0) {
11671 if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
11672 ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!");
11673 mNoCallbackWarningCount++;
11674 }
11675 }
11676
11677 // After a change occurred in track silenced state, mute capture in audio DSP if at least one
11678 // track is silenced and unmute otherwise
11679 for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) {
11680 if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) {
11681 changed = true;
11682 silenced = mActiveTracks[i]->isSilenced_l();
11683 }
11684 }
11685
11686 if (changed) {
11687 mInput->stream->setGain(silenced ? 0.0f: 1.0f);
11688 }
11689 }
11690
updateMetadata_l()11691 ThreadBase::MetadataUpdate MmapCaptureThread::updateMetadata_l()
11692 {
11693 if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) {
11694 return {}; // nothing to do
11695 }
11696 StreamInHalInterface::SinkMetadata metadata;
11697 for (const sp<IAfMmapTrack>& track : mActiveTracks) {
11698 // No track is invalid as this is called after prepareTrack_l in the same critical section
11699 record_track_metadata_v7_t trackMetadata;
11700 trackMetadata.base = {
11701 .source = track->attributes().source,
11702 .gain = 1, // capture tracks do not have volumes
11703 };
11704 trackMetadata.channel_mask = track->channelMask(),
11705 strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
11706 metadata.tracks.push_back(trackMetadata);
11707 }
11708 mInput->stream->updateSinkMetadata(metadata);
11709 MetadataUpdate change;
11710 change.recordMetadataUpdate = metadata.tracks;
11711 return change;
11712 }
11713
setRecordSilenced(audio_port_handle_t portId,bool silenced)11714 void MmapCaptureThread::setRecordSilenced(audio_port_handle_t portId, bool silenced)
11715 {
11716 audio_utils::lock_guard _l(mutex());
11717 for (size_t i = 0; i < mActiveTracks.size() ; i++) {
11718 if (mActiveTracks[i]->portId() == portId) {
11719 mActiveTracks[i]->setSilenced_l(silenced);
11720 broadcast_l();
11721 }
11722 }
11723 setClientSilencedIfExists_l(portId, silenced);
11724 }
11725
toAudioPortConfig(struct audio_port_config * config)11726 void MmapCaptureThread::toAudioPortConfig(struct audio_port_config* config)
11727 {
11728 MmapThread::toAudioPortConfig(config);
11729 if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
11730 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
11731 config->flags.input = mInput->flags;
11732 }
11733 }
11734
getExternalPosition(uint64_t * position,int64_t * timeNanos) const11735 status_t MmapCaptureThread::getExternalPosition(
11736 uint64_t* position, int64_t* timeNanos) const
11737 {
11738 if (mInput == nullptr) {
11739 return NO_INIT;
11740 }
11741 return mInput->getCapturePosition((int64_t*)position, timeNanos);
11742 }
11743
11744 // ----------------------------------------------------------------------------
11745
11746 /* static */
createBitPerfectThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady)11747 sp<IAfPlaybackThread> IAfPlaybackThread::createBitPerfectThread(
11748 const sp<IAfThreadCallback>& afThreadCallback,
11749 AudioStreamOut* output, audio_io_handle_t id, bool systemReady) {
11750 return sp<BitPerfectThread>::make(afThreadCallback, output, id, systemReady);
11751 }
11752
BitPerfectThread(const sp<IAfThreadCallback> & afThreadCallback,AudioStreamOut * output,audio_io_handle_t id,bool systemReady)11753 BitPerfectThread::BitPerfectThread(const sp<IAfThreadCallback> &afThreadCallback,
11754 AudioStreamOut *output, audio_io_handle_t id, bool systemReady)
11755 : MixerThread(afThreadCallback, output, id, systemReady, BIT_PERFECT) {}
11756
prepareTracks_l(Vector<sp<IAfTrack>> * tracksToRemove)11757 PlaybackThread::mixer_state BitPerfectThread::prepareTracks_l(
11758 Vector<sp<IAfTrack>>* tracksToRemove) {
11759 mixer_state result = MixerThread::prepareTracks_l(tracksToRemove);
11760 // If there is only one active track and it is bit-perfect, enable tee buffer.
11761 float volumeLeft = 1.0f;
11762 float volumeRight = 1.0f;
11763 if (sp<IAfTrack> bitPerfectTrack = getTrackToStreamBitPerfectly_l();
11764 bitPerfectTrack != nullptr) {
11765 const int trackId = bitPerfectTrack->id();
11766 mAudioMixer->setParameter(
11767 trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, (void *)mSinkBuffer);
11768 mAudioMixer->setParameter(
11769 trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER_FRAME_COUNT,
11770 (void *)(uintptr_t)mNormalFrameCount);
11771 bitPerfectTrack->getFinalVolume(&volumeLeft, &volumeRight);
11772 mIsBitPerfect = true;
11773 } else {
11774 mIsBitPerfect = false;
11775 // No need to copy bit-perfect data directly to sink buffer given there are multiple tracks
11776 // active.
11777 for (const auto& track : mActiveTracks) {
11778 const int trackId = track->id();
11779 mAudioMixer->setParameter(
11780 trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, nullptr);
11781 }
11782 }
11783 if (mVolumeLeft != volumeLeft || mVolumeRight != volumeRight) {
11784 mVolumeLeft = volumeLeft;
11785 mVolumeRight = volumeRight;
11786 setVolumeForOutput_l(volumeLeft, volumeRight);
11787 }
11788 return result;
11789 }
11790
threadLoop_mix()11791 void BitPerfectThread::threadLoop_mix() {
11792 MixerThread::threadLoop_mix();
11793 mHasDataCopiedToSinkBuffer = mIsBitPerfect;
11794 }
11795
setTracksInternalMute(std::map<audio_port_handle_t,bool> * tracksInternalMute)11796 void BitPerfectThread::setTracksInternalMute(
11797 std::map<audio_port_handle_t, bool>* tracksInternalMute) {
11798 audio_utils::lock_guard _l(mutex());
11799 for (auto& track : mTracks) {
11800 if (auto it = tracksInternalMute->find(track->portId()); it != tracksInternalMute->end()) {
11801 track->setInternalMute(it->second);
11802 tracksInternalMute->erase(it);
11803 }
11804 }
11805 }
11806
getTrackToStreamBitPerfectly_l()11807 sp<IAfTrack> BitPerfectThread::getTrackToStreamBitPerfectly_l() {
11808 if (com::android::media::audioserver::
11809 fix_concurrent_playback_behavior_with_bit_perfect_client()) {
11810 sp<IAfTrack> bitPerfectTrack = nullptr;
11811 bool allOtherTracksMuted = true;
11812 // Return the bit perfect track if all other tracks are muted
11813 for (const auto& track : mActiveTracks) {
11814 if (track->isBitPerfect()) {
11815 if (track->getInternalMute()) {
11816 // There can only be one bit-perfect client active. If it is mute internally,
11817 // there is no need to stream bit-perfectly.
11818 break;
11819 }
11820 bitPerfectTrack = track;
11821 } else if (track->getFinalVolume() != 0.f) {
11822 allOtherTracksMuted = false;
11823 if (bitPerfectTrack != nullptr) {
11824 break;
11825 }
11826 }
11827 }
11828 return allOtherTracksMuted ? bitPerfectTrack : nullptr;
11829 } else {
11830 if (mActiveTracks.size() == 1 && mActiveTracks[0]->isBitPerfect()) {
11831 return mActiveTracks[0];
11832 }
11833 }
11834 return nullptr;
11835 }
11836
11837 } // namespace android
11838