blob: 796596ac5f919927320a02b80b5cf407cd08f3a5 [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
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
Alex Ray371eb972012-11-30 11:11:54 -080021#define ATRACE_TAG ATRACE_TAG_AUDIO
Eric Laurent81784c32012-11-19 14:55:58 -080022
Glenn Kasten153b9fe2013-07-15 11:23:36 -070023#include "Configuration.h"
Eric Laurent81784c32012-11-19 14:55:58 -080024#include <math.h>
25#include <fcntl.h>
Eric Tan7b651152018-07-13 10:17:19 -070026#include <memory>
27#include <string>
Glenn Kastenad8510a2015-02-17 16:24:07 -080028#include <linux/futex.h>
Eric Laurent81784c32012-11-19 14:55:58 -080029#include <sys/stat.h>
Glenn Kastenad8510a2015-02-17 16:24:07 -080030#include <sys/syscall.h>
Glenn Kastenc9a23672020-06-30 12:12:42 -070031#include <cutils/bitops.h>
Eric Laurent81784c32012-11-19 14:55:58 -080032#include <cutils/properties.h>
jiabin10d86fd2019-10-31 17:20:42 -070033#include <media/AudioContainers.h>
34#include <media/AudioDeviceTypeAddr.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070035#include <media/AudioParameter.h>
Andy Hungcd044842014-08-07 11:04:34 -070036#include <media/AudioResamplerPublic.h>
Andy Hung89816052017-01-11 17:08:23 -080037#include <media/RecordBufferConverter.h>
Mikhail Naganov913d06c2016-11-01 12:49:22 -070038#include <media/TypeConverter.h>
Eric Laurent81784c32012-11-19 14:55:58 -080039#include <utils/Log.h>
Alex Ray371eb972012-11-30 11:11:54 -080040#include <utils/Trace.h>
Eric Laurent81784c32012-11-19 14:55:58 -080041
42#include <private/media/AudioTrackShared.h>
Wei Jiaf2ae3e12016-10-27 17:10:59 -070043#include <private/android_filesystem_config.h>
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010044#include <audio_utils/Balance.h>
jiabin245cdd92018-12-07 17:55:15 -080045#include <audio_utils/channels.h>
Glenn Kasten6bf707f2017-02-23 16:53:58 -080046#include <audio_utils/mono_blend.h>
Eric Laurent81784c32012-11-19 14:55:58 -080047#include <audio_utils/primitives.h>
Andy Hung98ef9782014-03-04 14:46:50 -080048#include <audio_utils/format.h>
Glenn Kastenc56f3422014-03-21 17:53:17 -070049#include <audio_utils/minifloat.h>
Hongwei Wang95e37682019-04-12 11:13:36 -070050#include <audio_utils/safe_math.h>
Mikhail Naganov9fe94012016-10-14 14:57:40 -070051#include <system/audio_effects/effect_ns.h>
52#include <system/audio_effects/effect_aec.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070053#include <system/audio.h>
Eric Laurent81784c32012-11-19 14:55:58 -080054
55// NBAIO implementations
Glenn Kasten6dbb5e32014-05-13 10:38:42 -070056#include <media/nbaio/AudioStreamInSource.h>
Eric Laurent81784c32012-11-19 14:55:58 -080057#include <media/nbaio/AudioStreamOutSink.h>
58#include <media/nbaio/MonoPipe.h>
59#include <media/nbaio/MonoPipeReader.h>
60#include <media/nbaio/Pipe.h>
61#include <media/nbaio/PipeReader.h>
62#include <media/nbaio/SourceAudioBufferProvider.h>
Wei Jia3f273d12015-11-24 09:06:49 -080063#include <mediautils/BatteryNotifier.h>
Eric Laurent81784c32012-11-19 14:55:58 -080064
Mikhail Naganov2996f672019-04-18 12:29:59 -070065#include <audiomanager/AudioManager.h>
Eric Laurent81784c32012-11-19 14:55:58 -080066#include <powermanager/PowerManager.h>
67
Kevin Rocard7588ff42018-01-08 11:11:30 -080068#include <media/audiohal/EffectsFactoryHalInterface.h>
Kevin Rocard069c2712018-03-29 19:09:14 -070069#include <media/audiohal/StreamHalInterface.h>
Kevin Rocard7588ff42018-01-08 11:11:30 -080070
Eric Laurent81784c32012-11-19 14:55:58 -080071#include "AudioFlinger.h"
Eric Laurent81784c32012-11-19 14:55:58 -080072#include "FastMixer.h"
Glenn Kasten6dbb5e32014-05-13 10:38:42 -070073#include "FastCapture.h"
Andy Hungab7ef302018-05-15 19:35:29 -070074#include <mediautils/SchedulingPolicyService.h>
75#include <mediautils/ServiceUtilities.h>
Eric Laurent81784c32012-11-19 14:55:58 -080076
Eric Laurent81784c32012-11-19 14:55:58 -080077#ifdef ADD_BATTERY_DATA
78#include <media/IMediaPlayerService.h>
79#include <media/IMediaDeathNotifier.h>
80#endif
81
Eric Laurent81784c32012-11-19 14:55:58 -080082#ifdef DEBUG_CPU_USAGE
Eric Tan5b13ff82018-07-27 11:20:17 -070083#include <audio_utils/Statistics.h>
Eric Laurent81784c32012-11-19 14:55:58 -080084#include <cpustats/ThreadCpuUsage.h>
85#endif
86
Glenn Kastenc05b8d72016-03-24 09:48:17 -070087#include "AutoPark.h"
88
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080089#include <pthread.h>
90#include "TypedLogger.h"
91
Eric Laurent81784c32012-11-19 14:55:58 -080092// ----------------------------------------------------------------------------
93
94// Note: the following macro is used for extremely verbose logging message. In
95// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
96// 0; but one side effect of this is to turn all LOGV's as well. Some messages
97// are so verbose that we want to suppress them even when we have ALOG_ASSERT
98// turned on. Do not uncomment the #def below unless you really know what you
99// are doing and want to see all of the extremely verbose messages.
100//#define VERY_VERY_VERBOSE_LOGGING
101#ifdef VERY_VERY_VERBOSE_LOGGING
102#define ALOGVV ALOGV
103#else
104#define ALOGVV(a...) do { } while(0)
105#endif
106
Andy Hung6770c6f2015-04-07 13:43:36 -0700107// TODO: Move these macro/inlines to a header file.
Glenn Kasten49d00ad2014-07-21 11:22:03 -0700108#define max(a, b) ((a) > (b) ? (a) : (b))
Andy Hung6770c6f2015-04-07 13:43:36 -0700109template <typename T>
110static inline T min(const T& a, const T& b)
111{
112 return a < b ? a : b;
113}
Glenn Kasten49d00ad2014-07-21 11:22:03 -0700114
Eric Laurent81784c32012-11-19 14:55:58 -0800115namespace android {
116
117// retry counts for buffer fill timeout
118// 50 * ~20msecs = 1 second
119static const int8_t kMaxTrackRetries = 50;
120static const int8_t kMaxTrackStartupRetries = 50;
121// allow less retry attempts on direct output thread.
122// direct outputs can be a scarce resource in audio hardware and should
123// be released as quickly as possible.
124static const int8_t kMaxTrackRetriesDirect = 2;
Eric Laurente93cc032016-05-05 10:15:10 -0700125
Eric Laurent51716182016-02-29 18:00:56 -0800126
Eric Laurent81784c32012-11-19 14:55:58 -0800127
128// don't warn about blocked writes or record buffer overflows more often than this
129static const nsecs_t kWarningThrottleNs = seconds(5);
130
131// RecordThread loop sleep time upon application overrun or audio HAL read error
132static const int kRecordThreadSleepUs = 5000;
133
Eric Laurent10351942014-05-08 18:49:52 -0700134// maximum time to wait in sendConfigEvent_l() for a status to be received
135static const nsecs_t kConfigEventTimeoutNs = seconds(2);
Eric Laurent81784c32012-11-19 14:55:58 -0800136
137// minimum sleep time for the mixer thread loop when tracks are active but in underrun
138static const uint32_t kMinThreadSleepTimeUs = 5000;
139// maximum divider applied to the active sleep time in the mixer thread loop
140static const uint32_t kMaxThreadSleepTimeShift = 2;
141
Andy Hung09a50072014-02-27 14:30:47 -0800142// minimum normal sink buffer size, expressed in milliseconds rather than frames
Glenn Kasteneb9487e2015-07-22 09:15:17 -0700143// FIXME This should be based on experimentally observed scheduling jitter
Andy Hung09a50072014-02-27 14:30:47 -0800144static const uint32_t kMinNormalSinkBufferSizeMs = 20;
145// maximum normal sink buffer size
146static const uint32_t kMaxNormalSinkBufferSizeMs = 24;
Eric Laurent81784c32012-11-19 14:55:58 -0800147
Glenn Kasteneb9487e2015-07-22 09:15:17 -0700148// minimum capture buffer size in milliseconds to _not_ need a fast capture thread
149// FIXME This should be based on experimentally observed scheduling jitter
150static const uint32_t kMinNormalCaptureBufferSizeMs = 12;
151
Eric Laurent972a1732013-09-04 09:42:59 -0700152// Offloaded output thread standby delay: allows track transition without going to standby
153static const nsecs_t kOffloadStandbyDelayNs = seconds(1);
154
Eric Laurent51716182016-02-29 18:00:56 -0800155// Direct output thread minimum sleep time in idle or active(underrun) state
156static const nsecs_t kDirectMinSleepTimeUs = 10000;
157
Glenn Kasten1b291842016-07-18 14:55:21 -0700158// The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good
159// balance between power consumption and latency, and allows threads to be scheduled reliably
160// by the CFS scheduler.
161// FIXME Express other hardcoded references to 20ms with references to this constant and move
162// it appropriately.
163#define FMS_20 20
Eric Laurent51716182016-02-29 18:00:56 -0800164
Eric Laurent81784c32012-11-19 14:55:58 -0800165// Whether to use fast mixer
166static const enum {
167 FastMixer_Never, // never initialize or use: for debugging only
168 FastMixer_Always, // always initialize and use, even if not needed: for debugging only
169 // normal mixer multiplier is 1
170 FastMixer_Static, // initialize if needed, then use all the time if initialized,
171 // multiplier is calculated based on min & max normal mixer buffer size
172 FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load,
173 // multiplier is calculated based on min & max normal mixer buffer size
174 // FIXME for FastMixer_Dynamic:
175 // Supporting this option will require fixing HALs that can't handle large writes.
176 // For example, one HAL implementation returns an error from a large write,
177 // and another HAL implementation corrupts memory, possibly in the sample rate converter.
178 // We could either fix the HAL implementations, or provide a wrapper that breaks
179 // up large writes into smaller ones, and the wrapper would need to deal with scheduler.
180} kUseFastMixer = FastMixer_Static;
181
Glenn Kasten6dbb5e32014-05-13 10:38:42 -0700182// Whether to use fast capture
183static const enum {
184 FastCapture_Never, // never initialize or use: for debugging only
185 FastCapture_Always, // always initialize and use, even if not needed: for debugging only
186 FastCapture_Static, // initialize if needed, then use all the time if initialized
187} kUseFastCapture = FastCapture_Static;
188
Eric Laurent81784c32012-11-19 14:55:58 -0800189// Priorities for requestPriority
190static const int kPriorityAudioApp = 2;
191static const int kPriorityFastMixer = 3;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -0700192static const int kPriorityFastCapture = 3;
Eric Laurent81784c32012-11-19 14:55:58 -0800193
Glenn Kastenea38ee72016-04-18 11:08:01 -0700194// IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the
195// track buffer in shared memory. Zero on input means to use a default value. For fast tracks,
196// AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'.
Glenn Kasten03490092014-05-27 12:30:54 -0700197
198// This is the default value, if not specified by property.
Glenn Kastenb5fed682013-12-03 09:06:43 -0800199static const int kFastTrackMultiplier = 2;
Eric Laurent81784c32012-11-19 14:55:58 -0800200
Glenn Kasten03490092014-05-27 12:30:54 -0700201// The minimum and maximum allowed values
202static const int kFastTrackMultiplierMin = 1;
203static const int kFastTrackMultiplierMax = 2;
204
205// The actual value to use, which can be specified per-device via property af.fast_track_multiplier.
206static int sFastTrackMultiplier = kFastTrackMultiplier;
207
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700208// See Thread::readOnlyHeap().
209// Initially this heap is used to allocate client buffers for "fast" AudioRecord.
210// Eventually it will be the single buffer that FastCapture writes into via HAL read(),
211// and that all "fast" AudioRecord clients read from. In either case, the size can be small.
Mikhail Naganov79a77822018-05-10 15:57:25 -0700212static const size_t kRecordThreadReadOnlyHeapSize = 0xD000;
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700213
Eric Laurent81784c32012-11-19 14:55:58 -0800214// ----------------------------------------------------------------------------
215
Glenn Kasten03490092014-05-27 12:30:54 -0700216static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT;
217
218static void sFastTrackMultiplierInit()
219{
220 char value[PROPERTY_VALUE_MAX];
221 if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
222 char *endptr;
223 unsigned long ul = strtoul(value, &endptr, 0);
224 if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
225 sFastTrackMultiplier = (int) ul;
226 }
227 }
228}
229
230// ----------------------------------------------------------------------------
231
Eric Laurent81784c32012-11-19 14:55:58 -0800232#ifdef ADD_BATTERY_DATA
233// To collect the amplifier usage
234static void addBatteryData(uint32_t params) {
235 sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
236 if (service == NULL) {
237 // it already logged
238 return;
239 }
240
241 service->addBatteryData(params);
242}
243#endif
244
Andy Hung3f0c9022016-01-15 17:49:46 -0800245// Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset
246struct {
247 // call when you acquire a partial wakelock
248 void acquire(const sp<IBinder> &wakeLockToken) {
249 pthread_mutex_lock(&mLock);
250 if (wakeLockToken.get() == nullptr) {
251 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
252 } else {
253 if (mCount == 0) {
254 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
255 }
256 ++mCount;
257 }
258 pthread_mutex_unlock(&mLock);
259 }
260
261 // call when you release a partial wakelock.
262 void release(const sp<IBinder> &wakeLockToken) {
263 if (wakeLockToken.get() == nullptr) {
264 return;
265 }
266 pthread_mutex_lock(&mLock);
267 if (--mCount < 0) {
268 ALOGE("negative wakelock count");
269 mCount = 0;
270 }
271 pthread_mutex_unlock(&mLock);
272 }
273
274 // retrieves the boottime timebase offset from monotonic.
275 int64_t getBoottimeOffset() {
276 pthread_mutex_lock(&mLock);
277 int64_t boottimeOffset = mBoottimeOffset;
278 pthread_mutex_unlock(&mLock);
279 return boottimeOffset;
280 }
281
282 // Adjusts the timebase offset between TIMEBASE_MONOTONIC
283 // and the selected timebase.
284 // Currently only TIMEBASE_BOOTTIME is allowed.
285 //
286 // This only needs to be called upon acquiring the first partial wakelock
287 // after all other partial wakelocks are released.
288 //
289 // We do an empirical measurement of the offset rather than parsing
290 // /proc/timer_list since the latter is not a formal kernel ABI.
291 static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) {
292 int clockbase;
293 switch (timebase) {
294 case ExtendedTimestamp::TIMEBASE_BOOTTIME:
295 clockbase = SYSTEM_TIME_BOOTTIME;
296 break;
297 default:
298 LOG_ALWAYS_FATAL("invalid timebase %d", timebase);
299 break;
300 }
301 // try three times to get the clock offset, choose the one
302 // with the minimum gap in measurements.
303 const int tries = 3;
304 nsecs_t bestGap, measured;
305 for (int i = 0; i < tries; ++i) {
306 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
307 const nsecs_t tbase = systemTime(clockbase);
308 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
309 const nsecs_t gap = tmono2 - tmono;
310 if (i == 0 || gap < bestGap) {
311 bestGap = gap;
312 measured = tbase - ((tmono + tmono2) >> 1);
313 }
314 }
315
316 // to avoid micro-adjusting, we don't change the timebase
317 // unless it is significantly different.
318 //
319 // Assumption: It probably takes more than toleranceNs to
320 // suspend and resume the device.
321 static int64_t toleranceNs = 10000; // 10 us
322 if (llabs(*offset - measured) > toleranceNs) {
323 ALOGV("Adjusting timebase offset old: %lld new: %lld",
324 (long long)*offset, (long long)measured);
325 *offset = measured;
326 }
327 }
328
329 pthread_mutex_t mLock;
330 int32_t mCount;
331 int64_t mBoottimeOffset;
332} gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization
Eric Laurent81784c32012-11-19 14:55:58 -0800333
334// ----------------------------------------------------------------------------
335// CPU Stats
336// ----------------------------------------------------------------------------
337
338class CpuStats {
339public:
340 CpuStats();
341 void sample(const String8 &title);
342#ifdef DEBUG_CPU_USAGE
343private:
344 ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns
Andy Hung16698b82018-08-01 10:48:38 -0700345 audio_utils::Statistics<double> mWcStats; // statistics on thread CPU usage in wall clock ns
Eric Laurent81784c32012-11-19 14:55:58 -0800346
Andy Hung16698b82018-08-01 10:48:38 -0700347 audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles
Eric Laurent81784c32012-11-19 14:55:58 -0800348
349 int mCpuNum; // thread's current CPU number
350 int mCpukHz; // frequency of thread's current CPU in kHz
351#endif
352};
353
354CpuStats::CpuStats()
355#ifdef DEBUG_CPU_USAGE
356 : mCpuNum(-1), mCpukHz(-1)
357#endif
358{
359}
360
Glenn Kasten0f11b512014-01-31 16:18:54 -0800361void CpuStats::sample(const String8 &title
362#ifndef DEBUG_CPU_USAGE
363 __unused
364#endif
365 ) {
Eric Laurent81784c32012-11-19 14:55:58 -0800366#ifdef DEBUG_CPU_USAGE
367 // get current thread's delta CPU time in wall clock ns
368 double wcNs;
369 bool valid = mCpuUsage.sampleAndEnable(wcNs);
370
371 // record sample for wall clock statistics
372 if (valid) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700373 mWcStats.add(wcNs);
Eric Laurent81784c32012-11-19 14:55:58 -0800374 }
375
376 // get the current CPU number
377 int cpuNum = sched_getcpu();
378
379 // get the current CPU frequency in kHz
380 int cpukHz = mCpuUsage.getCpukHz(cpuNum);
381
382 // check if either CPU number or frequency changed
383 if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
384 mCpuNum = cpuNum;
385 mCpukHz = cpukHz;
386 // ignore sample for purposes of cycles
387 valid = false;
388 }
389
390 // if no change in CPU number or frequency, then record sample for cycle statistics
391 if (valid && mCpukHz > 0) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700392 const double cycles = wcNs * cpukHz * 0.000001;
393 mHzStats.add(cycles);
Eric Laurent81784c32012-11-19 14:55:58 -0800394 }
395
Eric Tan5b13ff82018-07-27 11:20:17 -0700396 const unsigned n = mWcStats.getN();
Eric Laurent81784c32012-11-19 14:55:58 -0800397 // mCpuUsage.elapsed() is expensive, so don't call it every loop
398 if ((n & 127) == 1) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700399 const long long elapsed = mCpuUsage.elapsed();
Eric Laurent81784c32012-11-19 14:55:58 -0800400 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700401 const double perLoop = elapsed / (double) n;
402 const double perLoop100 = perLoop * 0.01;
403 const double perLoop1k = perLoop * 0.001;
404 const double mean = mWcStats.getMean();
405 const double stddev = mWcStats.getStdDev();
406 const double minimum = mWcStats.getMin();
407 const double maximum = mWcStats.getMax();
408 const double meanCycles = mHzStats.getMean();
409 const double stddevCycles = mHzStats.getStdDev();
410 const double minCycles = mHzStats.getMin();
411 const double maxCycles = mHzStats.getMax();
Eric Laurent81784c32012-11-19 14:55:58 -0800412 mCpuUsage.resetElapsed();
413 mWcStats.reset();
414 mHzStats.reset();
415 ALOGD("CPU usage for %s over past %.1f secs\n"
416 " (%u mixer loops at %.1f mean ms per loop):\n"
417 " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
418 " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
419 " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
420 title.string(),
421 elapsed * .000000001, n, perLoop * .000001,
422 mean * .001,
423 stddev * .001,
424 minimum * .001,
425 maximum * .001,
426 mean / perLoop100,
427 stddev / perLoop100,
428 minimum / perLoop100,
429 maximum / perLoop100,
430 meanCycles / perLoop1k,
431 stddevCycles / perLoop1k,
432 minCycles / perLoop1k,
433 maxCycles / perLoop1k);
434
435 }
436 }
437#endif
438};
439
440// ----------------------------------------------------------------------------
441// ThreadBase
442// ----------------------------------------------------------------------------
443
Glenn Kasten97b7b752014-09-28 13:04:24 -0700444// static
445const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type)
446{
447 switch (type) {
448 case MIXER:
449 return "MIXER";
450 case DIRECT:
451 return "DIRECT";
452 case DUPLICATING:
453 return "DUPLICATING";
454 case RECORD:
455 return "RECORD";
456 case OFFLOAD:
457 return "OFFLOAD";
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800458 case MMAP:
459 return "MMAP";
Glenn Kasten97b7b752014-09-28 13:04:24 -0700460 default:
461 return "unknown";
462 }
463}
464
Eric Laurent81784c32012-11-19 14:55:58 -0800465AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
jiabin10d86fd2019-10-31 17:20:42 -0700466 type_t type, bool systemReady)
Eric Laurent81784c32012-11-19 14:55:58 -0800467 : Thread(false /*canCallJava*/),
468 mType(type),
Glenn Kasten9b58f632013-07-16 11:37:48 -0700469 mAudioFlinger(audioFlinger),
Glenn Kasten70949c42013-08-06 07:40:12 -0700470 // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800471 // are set by PlaybackThread::readOutputParameters_l() or
472 // RecordThread::readInputParameters_l()
Eric Laurentfd477972013-10-25 18:10:40 -0700473 //FIXME: mStandby should be true here. Is this some kind of hack?
jiabin10d86fd2019-10-31 17:20:42 -0700474 mStandby(false),
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700475 mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id),
Eric Laurent81784c32012-11-19 14:55:58 -0800476 // mName will be set by concrete (non-virtual) subclass
Eric Laurent72e3f392015-05-20 14:43:50 -0700477 mDeathRecipient(new PMDeathRecipient(this)),
Eric Laurent6acd1d42017-01-04 14:23:29 -0800478 mSystemReady(systemReady),
479 mSignalPending(false)
Eric Laurent81784c32012-11-19 14:55:58 -0800480{
Eric Laurent296fb132015-05-01 11:38:42 -0700481 memset(&mPatch, 0, sizeof(struct audio_patch));
Eric Laurent81784c32012-11-19 14:55:58 -0800482}
483
484AudioFlinger::ThreadBase::~ThreadBase()
485{
Glenn Kastenc6ae3c82013-07-17 09:08:51 -0700486 // mConfigEvents should be empty, but just in case it isn't, free the memory it owns
Glenn Kastenc6ae3c82013-07-17 09:08:51 -0700487 mConfigEvents.clear();
488
Eric Laurent81784c32012-11-19 14:55:58 -0800489 // do not lock the mutex in destructor
490 releaseWakeLock_l();
491 if (mPowerManager != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800492 sp<IBinder> binder = IInterface::asBinder(mPowerManager);
Eric Laurent81784c32012-11-19 14:55:58 -0800493 binder->unlinkToDeath(mDeathRecipient);
494 }
Andy Hungd0979812019-02-21 15:51:44 -0800495
496 sendStatistics(true /* force */);
Eric Laurent81784c32012-11-19 14:55:58 -0800497}
498
Glenn Kastencf04c2c2013-08-06 07:41:16 -0700499status_t AudioFlinger::ThreadBase::readyToRun()
500{
501 status_t status = initCheck();
502 if (status == NO_ERROR) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800503 ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid());
Glenn Kastencf04c2c2013-08-06 07:41:16 -0700504 } else {
505 ALOGE("No working audio driver found.");
506 }
507 return status;
508}
509
Eric Laurent81784c32012-11-19 14:55:58 -0800510void AudioFlinger::ThreadBase::exit()
511{
512 ALOGV("ThreadBase::exit");
513 // do any cleanup required for exit to succeed
514 preExit();
515 {
516 // This lock prevents the following race in thread (uniprocessor for illustration):
517 // if (!exitPending()) {
518 // // context switch from here to exit()
519 // // exit() calls requestExit(), what exitPending() observes
520 // // exit() calls signal(), which is dropped since no waiters
521 // // context switch back from exit() to here
522 // mWaitWorkCV.wait(...);
523 // // now thread is hung
524 // }
525 AutoMutex lock(mLock);
526 requestExit();
527 mWaitWorkCV.broadcast();
528 }
529 // When Thread::requestExitAndWait is made virtual and this method is renamed to
530 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
531 requestExitAndWait();
532}
533
534status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
535{
Eric Laurent81784c32012-11-19 14:55:58 -0800536 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
537 Mutex::Autolock _l(mLock);
538
Eric Laurent10351942014-05-08 18:49:52 -0700539 return sendSetParameterConfigEvent_l(keyValuePairs);
540}
541
542// sendConfigEvent_l() must be called with ThreadBase::mLock held
543// Can temporarily release the lock if waiting for a reply from processConfigEvents_l().
544status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event)
545{
546 status_t status = NO_ERROR;
547
Eric Laurent72e3f392015-05-20 14:43:50 -0700548 if (event->mRequiresSystemReady && !mSystemReady) {
549 event->mWaitStatus = false;
550 mPendingConfigEvents.add(event);
551 return status;
552 }
Eric Laurent10351942014-05-08 18:49:52 -0700553 mConfigEvents.add(event);
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700554 ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType);
Eric Laurent81784c32012-11-19 14:55:58 -0800555 mWaitWorkCV.signal();
Eric Laurent10351942014-05-08 18:49:52 -0700556 mLock.unlock();
557 {
558 Mutex::Autolock _l(event->mLock);
559 while (event->mWaitStatus) {
560 if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) {
561 event->mStatus = TIMED_OUT;
562 event->mWaitStatus = false;
563 }
564 }
565 status = event->mStatus;
Eric Laurent81784c32012-11-19 14:55:58 -0800566 }
Eric Laurent10351942014-05-08 18:49:52 -0700567 mLock.lock();
Eric Laurent81784c32012-11-19 14:55:58 -0800568 return status;
569}
570
Eric Laurent09f1ed22019-04-24 17:45:17 -0700571void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid,
572 audio_port_handle_t portId)
Eric Laurent81784c32012-11-19 14:55:58 -0800573{
574 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700575 sendIoConfigEvent_l(event, pid, portId);
Eric Laurent81784c32012-11-19 14:55:58 -0800576}
577
578// sendIoConfigEvent_l() must be called with ThreadBase::mLock held
Eric Laurent09f1ed22019-04-24 17:45:17 -0700579void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid,
580 audio_port_handle_t portId)
Eric Laurent81784c32012-11-19 14:55:58 -0800581{
Andy Hungd0979812019-02-21 15:51:44 -0800582 // The audio statistics history is exponentially weighted to forget events
583 // about five or more seconds in the past. In order to have
584 // crisper statistics for mediametrics, we reset the statistics on
585 // an IoConfigEvent, to reflect different properties for a new device.
586 mIoJitterMs.reset();
587 mLatencyMs.reset();
588 mProcessTimeMs.reset();
589 mTimestampVerifier.discontinuity();
590
Eric Laurent09f1ed22019-04-24 17:45:17 -0700591 sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId);
Eric Laurent10351942014-05-08 18:49:52 -0700592 sendConfigEvent_l(configEvent);
Eric Laurent81784c32012-11-19 14:55:58 -0800593}
594
Mikhail Naganov83f04272017-02-07 10:45:09 -0800595void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp)
Eric Laurent72e3f392015-05-20 14:43:50 -0700596{
597 Mutex::Autolock _l(mLock);
Mikhail Naganov83f04272017-02-07 10:45:09 -0800598 sendPrioConfigEvent_l(pid, tid, prio, forApp);
Eric Laurent72e3f392015-05-20 14:43:50 -0700599}
600
Eric Laurent81784c32012-11-19 14:55:58 -0800601// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held
Mikhail Naganov83f04272017-02-07 10:45:09 -0800602void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(
603 pid_t pid, pid_t tid, int32_t prio, bool forApp)
Eric Laurent81784c32012-11-19 14:55:58 -0800604{
Mikhail Naganov83f04272017-02-07 10:45:09 -0800605 sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp);
Eric Laurent10351942014-05-08 18:49:52 -0700606 sendConfigEvent_l(configEvent);
Eric Laurent81784c32012-11-19 14:55:58 -0800607}
608
Eric Laurent10351942014-05-08 18:49:52 -0700609// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held
610status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair)
Eric Laurent81784c32012-11-19 14:55:58 -0800611{
Andy Hung2ddee192015-12-18 17:34:44 -0800612 sp<ConfigEvent> configEvent;
613 AudioParameter param(keyValuePair);
614 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -0700615 if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) {
Andy Hung2ddee192015-12-18 17:34:44 -0800616 setMasterMono_l(value != 0);
617 if (param.size() == 1) {
618 return NO_ERROR; // should be a solo parameter - we don't pass down
619 }
Mikhail Naganov00260b52016-10-13 12:54:24 -0700620 param.remove(String8(AudioParameter::keyMonoOutput));
Andy Hung2ddee192015-12-18 17:34:44 -0800621 configEvent = new SetParameterConfigEvent(param.toString());
622 } else {
623 configEvent = new SetParameterConfigEvent(keyValuePair);
624 }
Eric Laurent10351942014-05-08 18:49:52 -0700625 return sendConfigEvent_l(configEvent);
Glenn Kastenf7773312013-08-13 16:00:42 -0700626}
627
Eric Laurent1c333e22014-05-20 10:48:17 -0700628status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent(
629 const struct audio_patch *patch,
630 audio_patch_handle_t *handle)
631{
632 Mutex::Autolock _l(mLock);
633 sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle);
634 status_t status = sendConfigEvent_l(configEvent);
635 if (status == NO_ERROR) {
636 CreateAudioPatchConfigEventData *data =
637 (CreateAudioPatchConfigEventData *)configEvent->mData.get();
638 *handle = data->mHandle;
639 }
640 return status;
641}
642
643status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent(
644 const audio_patch_handle_t handle)
645{
646 Mutex::Autolock _l(mLock);
647 sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle);
648 return sendConfigEvent_l(configEvent);
649}
650
jiabin10d86fd2019-10-31 17:20:42 -0700651status_t AudioFlinger::ThreadBase::sendUpdateOutDeviceConfigEvent(
652 const DeviceDescriptorBaseVector& outDevices)
653{
654 if (type() != RECORD) {
655 // The update out device operation is only for record thread.
656 return INVALID_OPERATION;
657 }
658 Mutex::Autolock _l(mLock);
659 sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices);
660 return sendConfigEvent_l(configEvent);
661}
662
Eric Laurent1c333e22014-05-20 10:48:17 -0700663
Glenn Kasten2cfbf882013-08-14 13:12:11 -0700664// post condition: mConfigEvents.isEmpty()
Eric Laurent021cf962014-05-13 10:18:14 -0700665void AudioFlinger::ThreadBase::processConfigEvents_l()
Glenn Kastenf7773312013-08-13 16:00:42 -0700666{
Eric Laurent10351942014-05-08 18:49:52 -0700667 bool configChanged = false;
668
Eric Laurent81784c32012-11-19 14:55:58 -0800669 while (!mConfigEvents.isEmpty()) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700670 ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size());
Eric Laurent10351942014-05-08 18:49:52 -0700671 sp<ConfigEvent> event = mConfigEvents[0];
Eric Laurent81784c32012-11-19 14:55:58 -0800672 mConfigEvents.removeAt(0);
Eric Laurent10351942014-05-08 18:49:52 -0700673 switch (event->mType) {
Glenn Kasten3468e8a2013-08-13 16:01:22 -0700674 case CFG_EVENT_PRIO: {
Eric Laurent10351942014-05-08 18:49:52 -0700675 PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get();
676 // FIXME Need to understand why this has to be done asynchronously
Mikhail Naganov83f04272017-02-07 10:45:09 -0800677 int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp,
Glenn Kasten3468e8a2013-08-13 16:01:22 -0700678 true /*asynchronous*/);
679 if (err != 0) {
680 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
Eric Laurent10351942014-05-08 18:49:52 -0700681 data->mPrio, data->mPid, data->mTid, err);
Glenn Kasten3468e8a2013-08-13 16:01:22 -0700682 }
683 } break;
684 case CFG_EVENT_IO: {
Eric Laurent10351942014-05-08 18:49:52 -0700685 IoConfigEventData *data = (IoConfigEventData *)event->mData.get();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700686 ioConfigChanged(data->mEvent, data->mPid, data->mPortId);
Eric Laurent10351942014-05-08 18:49:52 -0700687 } break;
688 case CFG_EVENT_SET_PARAMETER: {
689 SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get();
690 if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) {
691 configChanged = true;
Andy Hung293558a2017-03-21 12:19:20 -0700692 mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed",
693 data->mKeyValuePairs.string());
Glenn Kastend5418eb2013-08-14 13:11:06 -0700694 }
Glenn Kasten3468e8a2013-08-13 16:01:22 -0700695 } break;
Eric Laurent1c333e22014-05-20 10:48:17 -0700696 case CFG_EVENT_CREATE_AUDIO_PATCH: {
jiabin10d86fd2019-10-31 17:20:42 -0700697 const DeviceTypeSet oldDevices = getDeviceTypes();
Eric Laurent1c333e22014-05-20 10:48:17 -0700698 CreateAudioPatchConfigEventData *data =
699 (CreateAudioPatchConfigEventData *)event->mData.get();
700 event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle);
jiabin10d86fd2019-10-31 17:20:42 -0700701 const DeviceTypeSet newDevices = getDeviceTypes();
702 mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)",
703 dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(),
704 dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str());
Eric Laurent1c333e22014-05-20 10:48:17 -0700705 } break;
706 case CFG_EVENT_RELEASE_AUDIO_PATCH: {
jiabin10d86fd2019-10-31 17:20:42 -0700707 const DeviceTypeSet oldDevices = getDeviceTypes();
Eric Laurent1c333e22014-05-20 10:48:17 -0700708 ReleaseAudioPatchConfigEventData *data =
709 (ReleaseAudioPatchConfigEventData *)event->mData.get();
710 event->mStatus = releaseAudioPatch_l(data->mHandle);
jiabin10d86fd2019-10-31 17:20:42 -0700711 const DeviceTypeSet newDevices = getDeviceTypes();
712 mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)",
713 dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(),
714 dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str());
715 } break;
716 case CFG_EVENT_UPDATE_OUT_DEVICE: {
717 UpdateOutDevicesConfigEventData *data =
718 (UpdateOutDevicesConfigEventData *)event->mData.get();
719 updateOutDevices(data->mOutDevices);
Eric Laurent1c333e22014-05-20 10:48:17 -0700720 } break;
Glenn Kasten3468e8a2013-08-13 16:01:22 -0700721 default:
Eric Laurent10351942014-05-08 18:49:52 -0700722 ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType);
Glenn Kasten3468e8a2013-08-13 16:01:22 -0700723 break;
Eric Laurent81784c32012-11-19 14:55:58 -0800724 }
Eric Laurent10351942014-05-08 18:49:52 -0700725 {
726 Mutex::Autolock _l(event->mLock);
727 if (event->mWaitStatus) {
728 event->mWaitStatus = false;
729 event->mCond.signal();
730 }
731 }
732 ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this);
733 }
734
735 if (configChanged) {
736 cacheParameters_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800737 }
Eric Laurent81784c32012-11-19 14:55:58 -0800738}
739
Marco Nelissenb2208842014-02-07 14:00:50 -0800740String8 channelMaskToString(audio_channel_mask_t mask, bool output) {
741 String8 s;
Glenn Kastene1635ec2015-06-08 15:46:49 -0700742 const audio_channel_representation_t representation =
743 audio_channel_mask_get_representation(mask);
Andy Hungf98ec8d2015-05-19 12:53:24 -0700744
745 switch (representation) {
jiabin245cdd92018-12-07 17:55:15 -0800746 // Travel all single bit channel mask to convert channel mask to string.
Andy Hungf98ec8d2015-05-19 12:53:24 -0700747 case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
748 if (output) {
749 if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, ");
750 if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, ");
751 if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, ");
752 if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, ");
753 if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, ");
754 if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, ");
755 if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, ");
756 if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, ");
757 if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, ");
758 if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, ");
759 if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, ");
760 if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,");
761 if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, ");
762 if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, ");
763 if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, ");
764 if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, ");
765 if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " );
766 if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " );
Mikhail Naganovc9948492018-05-10 17:25:28 -0700767 if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, " );
768 if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, " );
jiabin245cdd92018-12-07 17:55:15 -0800769 if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, " );
770 if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, " );
Andy Hungf98ec8d2015-05-19 12:53:24 -0700771 if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, ");
772 } else {
773 if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, ");
774 if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, ");
775 if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, ");
776 if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, ");
777 if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, ");
778 if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, ");
779 if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, ");
780 if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, ");
781 if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, ");
782 if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, ");
783 if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, ");
784 if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, ");
Mikhail Naganovc9948492018-05-10 17:25:28 -0700785 if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, ");
786 if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, ");
787 if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, ");
788 if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low freq, ");
789 if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, " );
790 if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, " );
Andy Hungf98ec8d2015-05-19 12:53:24 -0700791 if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, ");
792 if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, ");
793 if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, ");
794 }
795 const int len = s.length();
796 if (len > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700797 (void) s.lockBuffer(len); // needed?
Andy Hungf98ec8d2015-05-19 12:53:24 -0700798 s.unlockBuffer(len - 2); // remove trailing ", "
799 }
800 return s;
Marco Nelissenb2208842014-02-07 14:00:50 -0800801 }
Andy Hungf98ec8d2015-05-19 12:53:24 -0700802 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
803 s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask));
804 return s;
805 default:
806 s.appendFormat("unknown mask, representation:%d bits:%#x",
807 representation, audio_channel_mask_get_bits(mask));
808 return s;
Marco Nelissenb2208842014-02-07 14:00:50 -0800809 }
Marco Nelissenb2208842014-02-07 14:00:50 -0800810}
811
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700812void AudioFlinger::ThreadBase::dump(int fd, const Vector<String16>& args)
Eric Laurent81784c32012-11-19 14:55:58 -0800813{
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800814 dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input",
815 this, mThreadName, getTid(), type(), threadTypeToString(type()));
816
Eric Laurent81784c32012-11-19 14:55:58 -0800817 bool locked = AudioFlinger::dumpTryLock(mLock);
818 if (!locked) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800819 dprintf(fd, " Thread may be deadlocked\n");
Eric Laurent81784c32012-11-19 14:55:58 -0800820 }
821
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700822 dumpBase_l(fd, args);
823 dumpInternals_l(fd, args);
824 dumpTracks_l(fd, args);
825 dumpEffectChains_l(fd, args);
826
827 if (locked) {
828 mLock.unlock();
829 }
830
831 dprintf(fd, " Local log:\n");
832 mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */);
833}
834
835void AudioFlinger::ThreadBase::dumpBase_l(int fd, const Vector<String16>& args __unused)
836{
Elliott Hughes87cebad2014-05-22 10:14:43 -0700837 dprintf(fd, " I/O handle: %d\n", mId);
Elliott Hughes87cebad2014-05-22 10:14:43 -0700838 dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no");
Glenn Kasten97b7b752014-09-28 13:04:24 -0700839 dprintf(fd, " Sample rate: %u Hz\n", mSampleRate);
Elliott Hughes87cebad2014-05-22 10:14:43 -0700840 dprintf(fd, " HAL frame count: %zu\n", mFrameCount);
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700841 dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat).c_str());
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700842 dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize);
Glenn Kasten97b7b752014-09-28 13:04:24 -0700843 dprintf(fd, " Channel count: %u\n", mChannelCount);
844 dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask,
Marco Nelissenb2208842014-02-07 14:00:50 -0800845 channelMaskToString(mChannelMask, mType != RECORD).string());
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700846 dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat).c_str());
Glenn Kastenf87c2f52015-08-21 08:03:57 -0700847 dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize);
Elliott Hughes87cebad2014-05-22 10:14:43 -0700848 dprintf(fd, " Pending config events:");
Marco Nelissenb2208842014-02-07 14:00:50 -0800849 size_t numConfig = mConfigEvents.size();
850 if (numConfig) {
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700851 const size_t SIZE = 256;
852 char buffer[SIZE];
Marco Nelissenb2208842014-02-07 14:00:50 -0800853 for (size_t i = 0; i < numConfig; i++) {
854 mConfigEvents[i]->dump(buffer, SIZE);
Elliott Hughes87cebad2014-05-22 10:14:43 -0700855 dprintf(fd, "\n %s", buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800856 }
Elliott Hughes87cebad2014-05-22 10:14:43 -0700857 dprintf(fd, "\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800858 } else {
Elliott Hughes87cebad2014-05-22 10:14:43 -0700859 dprintf(fd, " none\n");
Eric Laurent81784c32012-11-19 14:55:58 -0800860 }
Andy Hung293558a2017-03-21 12:19:20 -0700861 // Note: output device may be used by capture threads for effects such as AEC.
jiabin10d86fd2019-10-31 17:20:42 -0700862 dprintf(fd, " Output devices: %s (%s)\n",
863 dumpDeviceTypes(outDeviceTypes()).c_str(), toString(outDeviceTypes()).c_str());
864 dprintf(fd, " Input device: %#x (%s)\n",
865 inDeviceType(), toString(inDeviceType()).c_str());
Andy Hung9b181952019-02-25 14:53:36 -0800866 dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str());
Eric Laurent81784c32012-11-19 14:55:58 -0800867
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700868 // Dump timestamp statistics for the Thread types that support it.
869 if (mType == RECORD
870 || mType == MIXER
871 || mType == DUPLICATING
Andy Hungf3234512018-07-03 14:51:47 -0700872 || mType == DIRECT
873 || mType == OFFLOAD) {
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700874 dprintf(fd, " Timestamp stats: %s\n", mTimestampVerifier.toString().c_str());
Andy Hungc8fddf32018-08-08 18:32:37 -0700875 dprintf(fd, " Timestamp corrected: %s\n", isTimestampCorrectionEnabled() ? "yes" : "no");
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700876 }
877
Andy Hung446f4df2019-02-21 12:26:41 -0800878 if (mLastIoBeginNs > 0) { // MMAP may not set this
879 dprintf(fd, " Last %s occurred (msecs): %lld\n",
880 isOutput() ? "write" : "read",
881 (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND);
882 }
883
884 if (mProcessTimeMs.getN() > 0) {
885 dprintf(fd, " Process time ms stats: %s\n", mProcessTimeMs.toString().c_str());
886 }
887
888 if (mIoJitterMs.getN() > 0) {
889 dprintf(fd, " Hal %s jitter ms stats: %s\n",
890 isOutput() ? "write" : "read",
891 mIoJitterMs.toString().c_str());
892 }
893
Andy Hunge6c37112019-02-26 17:38:10 -0800894 if (mLatencyMs.getN() > 0) {
895 dprintf(fd, " Threadloop %s latency stats: %s\n",
896 isOutput() ? "write" : "read",
897 mLatencyMs.toString().c_str());
898 }
Eric Laurent81784c32012-11-19 14:55:58 -0800899}
900
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700901void AudioFlinger::ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args)
Eric Laurent81784c32012-11-19 14:55:58 -0800902{
903 const size_t SIZE = 256;
904 char buffer[SIZE];
Eric Laurent81784c32012-11-19 14:55:58 -0800905
Marco Nelissenb2208842014-02-07 14:00:50 -0800906 size_t numEffectChains = mEffectChains.size();
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000907 snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains);
Eric Laurent81784c32012-11-19 14:55:58 -0800908 write(fd, buffer, strlen(buffer));
909
Marco Nelissenb2208842014-02-07 14:00:50 -0800910 for (size_t i = 0; i < numEffectChains; ++i) {
Eric Laurent81784c32012-11-19 14:55:58 -0800911 sp<EffectChain> chain = mEffectChains[i];
912 if (chain != 0) {
913 chain->dump(fd, args);
914 }
915 }
916}
917
Andy Hungdae27702016-10-31 14:01:16 -0700918void AudioFlinger::ThreadBase::acquireWakeLock()
Eric Laurent81784c32012-11-19 14:55:58 -0800919{
920 Mutex::Autolock _l(mLock);
Andy Hungdae27702016-10-31 14:01:16 -0700921 acquireWakeLock_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800922}
923
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100924String16 AudioFlinger::ThreadBase::getWakeLockTag()
925{
926 switch (mType) {
Glenn Kastenbcb14862015-03-05 17:11:21 -0800927 case MIXER:
928 return String16("AudioMix");
929 case DIRECT:
930 return String16("AudioDirectOut");
931 case DUPLICATING:
932 return String16("AudioDup");
933 case RECORD:
934 return String16("AudioIn");
935 case OFFLOAD:
936 return String16("AudioOffload");
Eric Laurent6acd1d42017-01-04 14:23:29 -0800937 case MMAP:
938 return String16("Mmap");
Glenn Kastenbcb14862015-03-05 17:11:21 -0800939 default:
940 ALOG_ASSERT(false);
941 return String16("AudioUnknown");
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100942 }
943}
944
Andy Hungdae27702016-10-31 14:01:16 -0700945void AudioFlinger::ThreadBase::acquireWakeLock_l()
Eric Laurent81784c32012-11-19 14:55:58 -0800946{
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800947 getPowerManager_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800948 if (mPowerManager != 0) {
949 sp<IBinder> binder = new BBinder();
Andy Hungdae27702016-10-31 14:01:16 -0700950 // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids.
951 status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
Marco Nelissene14a5d62013-10-03 08:51:24 -0700952 binder,
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100953 getWakeLockTag(),
Marco Nelissendcb346b2015-09-09 10:47:29 -0700954 String16("audioserver"),
Glenn Kasten3abc2de2014-09-05 16:45:52 -0700955 true /* FIXME force oneway contrary to .aidl */);
Eric Laurent81784c32012-11-19 14:55:58 -0800956 if (status == NO_ERROR) {
957 mWakeLockToken = binder;
958 }
Glenn Kastend7dca052015-03-05 16:05:54 -0800959 ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status);
Eric Laurent81784c32012-11-19 14:55:58 -0800960 }
Wei Jia3f273d12015-11-24 09:06:49 -0800961
Andy Hung3f0c9022016-01-15 17:49:46 -0800962 gBoottime.acquire(mWakeLockToken);
Andy Hung818e7a32016-02-16 18:08:07 -0800963 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
964 gBoottime.getBoottimeOffset();
Eric Laurent81784c32012-11-19 14:55:58 -0800965}
966
967void AudioFlinger::ThreadBase::releaseWakeLock()
968{
969 Mutex::Autolock _l(mLock);
970 releaseWakeLock_l();
971}
972
973void AudioFlinger::ThreadBase::releaseWakeLock_l()
974{
Andy Hung3f0c9022016-01-15 17:49:46 -0800975 gBoottime.release(mWakeLockToken);
Eric Laurent81784c32012-11-19 14:55:58 -0800976 if (mWakeLockToken != 0) {
Glenn Kastend7dca052015-03-05 16:05:54 -0800977 ALOGV("releaseWakeLock_l() %s", mThreadName);
Eric Laurent81784c32012-11-19 14:55:58 -0800978 if (mPowerManager != 0) {
Glenn Kasten3abc2de2014-09-05 16:45:52 -0700979 mPowerManager->releaseWakeLock(mWakeLockToken, 0,
980 true /* FIXME force oneway contrary to .aidl */);
Eric Laurent81784c32012-11-19 14:55:58 -0800981 }
982 mWakeLockToken.clear();
983 }
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800984}
985
986void AudioFlinger::ThreadBase::getPowerManager_l() {
Eric Laurent72e3f392015-05-20 14:43:50 -0700987 if (mSystemReady && mPowerManager == 0) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800988 // use checkService() to avoid blocking if power service is not up yet
989 sp<IBinder> binder =
990 defaultServiceManager()->checkService(String16("power"));
991 if (binder == 0) {
Glenn Kastend7dca052015-03-05 16:05:54 -0800992 ALOGW("Thread %s cannot connect to the power manager service", mThreadName);
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800993 } else {
994 mPowerManager = interface_cast<IPowerManager>(binder);
995 binder->linkToDeath(mDeathRecipient);
996 }
997 }
998}
999
Andy Hungd01b0f12016-11-07 16:10:30 -08001000void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001001 getPowerManager_l();
Andy Hungdae27702016-10-31 14:01:16 -07001002
1003#if !LOG_NDEBUG
1004 std::stringstream s;
Andy Hungd01b0f12016-11-07 16:10:30 -08001005 for (uid_t uid : uids) {
Andy Hungdae27702016-10-31 14:01:16 -07001006 s << uid << " ";
1007 }
1008 ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str());
1009#endif
1010
Andy Hung438e7572015-12-14 15:51:17 -08001011 if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called.
1012 if (mSystemReady) {
1013 ALOGE("no wake lock to update, but system ready!");
1014 } else {
1015 ALOGW("no wake lock to update, system not ready yet");
1016 }
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001017 return;
1018 }
1019 if (mPowerManager != 0) {
Andy Hung1f12a8a2016-11-07 16:10:30 -08001020 std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints
1021 status_t status = mPowerManager->updateWakeLockUids(
1022 mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(),
1023 true /* FIXME force oneway contrary to .aidl */);
Eric Laurent4d231dc2016-03-11 18:38:23 -08001024 ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status);
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001025 }
1026}
1027
Eric Laurent81784c32012-11-19 14:55:58 -08001028void AudioFlinger::ThreadBase::clearPowerManager()
1029{
1030 Mutex::Autolock _l(mLock);
1031 releaseWakeLock_l();
1032 mPowerManager.clear();
1033}
1034
jiabin10d86fd2019-10-31 17:20:42 -07001035void AudioFlinger::ThreadBase::updateOutDevices(
1036 const DeviceDescriptorBaseVector& outDevices __unused)
1037{
1038 ALOGE("%s should only be called in RecordThread", __func__);
1039}
1040
Glenn Kasten0f11b512014-01-31 16:18:54 -08001041void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08001042{
1043 sp<ThreadBase> thread = mThread.promote();
1044 if (thread != 0) {
1045 thread->clearPowerManager();
1046 }
1047 ALOGW("power manager service died !!!");
1048}
1049
Eric Laurent81784c32012-11-19 14:55:58 -08001050void AudioFlinger::ThreadBase::setEffectSuspended_l(
Glenn Kastend848eb42016-03-08 13:42:11 -08001051 const effect_uuid_t *type, bool suspend, audio_session_t sessionId)
Eric Laurent81784c32012-11-19 14:55:58 -08001052{
1053 sp<EffectChain> chain = getEffectChain_l(sessionId);
1054 if (chain != 0) {
1055 if (type != NULL) {
1056 chain->setEffectSuspended_l(type, suspend);
1057 } else {
1058 chain->setEffectSuspendedAll_l(suspend);
1059 }
1060 }
1061
1062 updateSuspendedSessions_l(type, suspend, sessionId);
1063}
1064
1065void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1066{
1067 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
1068 if (index < 0) {
1069 return;
1070 }
1071
1072 const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
1073 mSuspendedSessions.valueAt(index);
1074
1075 for (size_t i = 0; i < sessionEffects.size(); i++) {
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001076 const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i);
Eric Laurent81784c32012-11-19 14:55:58 -08001077 for (int j = 0; j < desc->mRefCount; j++) {
1078 if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1079 chain->setEffectSuspendedAll_l(true);
1080 } else {
1081 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
1082 desc->mType.timeLow);
1083 chain->setEffectSuspended_l(&desc->mType, true);
1084 }
1085 }
1086 }
1087}
1088
1089void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1090 bool suspend,
Glenn Kastend848eb42016-03-08 13:42:11 -08001091 audio_session_t sessionId)
Eric Laurent81784c32012-11-19 14:55:58 -08001092{
1093 ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
1094
1095 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1096
1097 if (suspend) {
1098 if (index >= 0) {
1099 sessionEffects = mSuspendedSessions.valueAt(index);
1100 } else {
1101 mSuspendedSessions.add(sessionId, sessionEffects);
1102 }
1103 } else {
1104 if (index < 0) {
1105 return;
1106 }
1107 sessionEffects = mSuspendedSessions.valueAt(index);
1108 }
1109
1110
1111 int key = EffectChain::kKeyForSuspendAll;
1112 if (type != NULL) {
1113 key = type->timeLow;
1114 }
1115 index = sessionEffects.indexOfKey(key);
1116
1117 sp<SuspendedSessionDesc> desc;
1118 if (suspend) {
1119 if (index >= 0) {
1120 desc = sessionEffects.valueAt(index);
1121 } else {
1122 desc = new SuspendedSessionDesc();
1123 if (type != NULL) {
1124 desc->mType = *type;
1125 }
1126 sessionEffects.add(key, desc);
1127 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
1128 }
1129 desc->mRefCount++;
1130 } else {
1131 if (index < 0) {
1132 return;
1133 }
1134 desc = sessionEffects.valueAt(index);
1135 if (--desc->mRefCount == 0) {
1136 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
1137 sessionEffects.removeItemsAt(index);
1138 if (sessionEffects.isEmpty()) {
1139 ALOGV("updateSuspendedSessions_l() restore removing session %d",
1140 sessionId);
1141 mSuspendedSessions.removeItem(sessionId);
1142 }
1143 }
1144 }
1145 if (!sessionEffects.isEmpty()) {
1146 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1147 }
1148}
1149
Eric Laurent5d885392019-12-13 10:56:31 -08001150void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(bool enabled,
1151 audio_session_t sessionId,
1152 bool threadLocked) {
1153 if (!threadLocked) {
1154 mLock.lock();
1155 }
Eric Laurent81784c32012-11-19 14:55:58 -08001156
Eric Laurent81784c32012-11-19 14:55:58 -08001157 if (mType != RECORD) {
1158 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1159 // another session. This gives the priority to well behaved effect control panels
1160 // and applications not using global effects.
1161 // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
1162 // global effects
Eric Laurenta20c4e92019-11-12 15:55:51 -08001163 if (!audio_is_global_session(sessionId)) {
Eric Laurent81784c32012-11-19 14:55:58 -08001164 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1165 }
1166 }
1167
Eric Laurent5d885392019-12-13 10:56:31 -08001168 if (!threadLocked) {
1169 mLock.unlock();
Eric Laurent81784c32012-11-19 14:55:58 -08001170 }
1171}
1172
Eric Laurent4c415062016-06-17 16:14:16 -07001173// checkEffectCompatibility_l() must be called with ThreadBase::mLock held
1174status_t AudioFlinger::RecordThread::checkEffectCompatibility_l(
1175 const effect_descriptor_t *desc, audio_session_t sessionId)
1176{
Eric Laurenta20c4e92019-11-12 15:55:51 -08001177 // No global output effect sessions on record threads
1178 if (sessionId == AUDIO_SESSION_OUTPUT_MIX
1179 || sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent4c415062016-06-17 16:14:16 -07001180 ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s",
1181 desc->name, mThreadName);
1182 return BAD_VALUE;
1183 }
1184 // only pre processing effects on record thread
1185 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) {
1186 ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s",
1187 desc->name, mThreadName);
1188 return BAD_VALUE;
1189 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -07001190
1191 // always allow effects without processing load or latency
1192 if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
1193 return NO_ERROR;
1194 }
1195
Eric Laurent4c415062016-06-17 16:14:16 -07001196 audio_input_flags_t flags = mInput->flags;
1197 if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) {
1198 if (flags & AUDIO_INPUT_FLAG_RAW) {
1199 ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode",
1200 desc->name, mThreadName);
1201 return BAD_VALUE;
1202 }
1203 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1204 ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode",
1205 desc->name, mThreadName);
1206 return BAD_VALUE;
1207 }
1208 }
1209 return NO_ERROR;
1210}
1211
1212// checkEffectCompatibility_l() must be called with ThreadBase::mLock held
1213status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l(
1214 const effect_descriptor_t *desc, audio_session_t sessionId)
1215{
1216 // no preprocessing on playback threads
1217 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
1218 ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback"
1219 " thread %s", desc->name, mThreadName);
1220 return BAD_VALUE;
1221 }
1222
Eric Laurent3e4de772017-07-16 16:55:08 -07001223 // always allow effects without processing load or latency
1224 if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
1225 return NO_ERROR;
1226 }
1227
Eric Laurent4c415062016-06-17 16:14:16 -07001228 switch (mType) {
1229 case MIXER: {
Andy Hung9aad48c2017-11-29 10:29:19 -08001230#ifndef MULTICHANNEL_EFFECT_CHAIN
Eric Laurent4c415062016-06-17 16:14:16 -07001231 // Reject any effect on mixer multichannel sinks.
1232 // TODO: fix both format and multichannel issues with effects.
1233 if (mChannelCount != FCC_2) {
1234 ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER"
1235 " thread %s", desc->name, mChannelCount, mThreadName);
1236 return BAD_VALUE;
1237 }
Andy Hung9aad48c2017-11-29 10:29:19 -08001238#endif
Eric Laurent4c415062016-06-17 16:14:16 -07001239 audio_output_flags_t flags = mOutput->flags;
1240 if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) {
1241 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1242 // global effects are applied only to non fast tracks if they are SW
1243 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1244 break;
1245 }
1246 } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
1247 // only post processing on output stage session
1248 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1249 ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed"
1250 " on output stage session", desc->name);
1251 return BAD_VALUE;
1252 }
Eric Laurenta20c4e92019-11-12 15:55:51 -08001253 } else if (sessionId == AUDIO_SESSION_DEVICE) {
1254 // only post processing on output stage session
1255 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
1256 ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed"
1257 " on device session", desc->name);
1258 return BAD_VALUE;
1259 }
Eric Laurent4c415062016-06-17 16:14:16 -07001260 } else {
1261 // no restriction on effects applied on non fast tracks
1262 if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) {
1263 break;
1264 }
1265 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -07001266
Eric Laurent4c415062016-06-17 16:14:16 -07001267 if (flags & AUDIO_OUTPUT_FLAG_RAW) {
1268 ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode",
1269 desc->name);
1270 return BAD_VALUE;
1271 }
1272 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
1273 ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread"
1274 " in fast mode", desc->name);
1275 return BAD_VALUE;
1276 }
1277 }
1278 } break;
1279 case OFFLOAD:
Jean-Michel Trivi773ee952016-07-11 16:53:18 -07001280 // nothing actionable on offload threads, if the effect:
1281 // - is offloadable: the effect can be created
1282 // - is NOT offloadable: the effect should still be created, but EffectHandle::enable()
1283 // will take care of invalidating the tracks of the thread
Eric Laurent4c415062016-06-17 16:14:16 -07001284 break;
1285 case DIRECT:
1286 // Reject any effect on Direct output threads for now, since the format of
1287 // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
1288 ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s",
1289 desc->name, mThreadName);
1290 return BAD_VALUE;
1291 case DUPLICATING:
Andy Hung9aad48c2017-11-29 10:29:19 -08001292#ifndef MULTICHANNEL_EFFECT_CHAIN
Eric Laurent4c415062016-06-17 16:14:16 -07001293 // Reject any effect on mixer multichannel sinks.
1294 // TODO: fix both format and multichannel issues with effects.
1295 if (mChannelCount != FCC_2) {
1296 ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)"
1297 " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName);
1298 return BAD_VALUE;
1299 }
Andy Hung9aad48c2017-11-29 10:29:19 -08001300#endif
Eric Laurenta20c4e92019-11-12 15:55:51 -08001301 if (audio_is_global_session(sessionId)) {
Eric Laurent4c415062016-06-17 16:14:16 -07001302 ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING"
1303 " thread %s", desc->name, mThreadName);
1304 return BAD_VALUE;
1305 }
1306 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
1307 ALOGW("checkEffectCompatibility_l(): post processing effect %s on"
1308 " DUPLICATING thread %s", desc->name, mThreadName);
1309 return BAD_VALUE;
1310 }
1311 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) {
1312 ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on"
1313 " DUPLICATING thread %s", desc->name, mThreadName);
1314 return BAD_VALUE;
1315 }
1316 break;
1317 default:
1318 LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType);
1319 }
1320
1321 return NO_ERROR;
1322}
1323
Eric Laurent81784c32012-11-19 14:55:58 -08001324// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
1325sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
1326 const sp<AudioFlinger::Client>& client,
1327 const sp<IEffectClient>& effectClient,
1328 int32_t priority,
Glenn Kastend848eb42016-03-08 13:42:11 -08001329 audio_session_t sessionId,
Eric Laurent81784c32012-11-19 14:55:58 -08001330 effect_descriptor_t *desc,
1331 int *enabled,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001332 status_t *status,
1333 bool pinned)
Eric Laurent81784c32012-11-19 14:55:58 -08001334{
1335 sp<EffectModule> effect;
1336 sp<EffectHandle> handle;
1337 status_t lStatus;
1338 sp<EffectChain> chain;
1339 bool chainCreated = false;
1340 bool effectCreated = false;
Mikhail Naganov2247f7b2017-01-13 11:52:54 -08001341 audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED;
Eric Laurent81784c32012-11-19 14:55:58 -08001342
1343 lStatus = initCheck();
1344 if (lStatus != NO_ERROR) {
1345 ALOGW("createEffect_l() Audio driver not initialized.");
1346 goto Exit;
1347 }
1348
Eric Laurent81784c32012-11-19 14:55:58 -08001349 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
1350
1351 { // scope for mLock
1352 Mutex::Autolock _l(mLock);
1353
Eric Laurent4c415062016-06-17 16:14:16 -07001354 lStatus = checkEffectCompatibility_l(desc, sessionId);
1355 if (lStatus != NO_ERROR) {
1356 goto Exit;
1357 }
1358
Eric Laurent81784c32012-11-19 14:55:58 -08001359 // check for existing effect chain with the requested audio session
1360 chain = getEffectChain_l(sessionId);
1361 if (chain == 0) {
1362 // create a new chain for this session
1363 ALOGV("createEffect_l() new effect chain for session %d", sessionId);
1364 chain = new EffectChain(this, sessionId);
1365 addEffectChain_l(chain);
1366 chain->setStrategy(getStrategyForSession_l(sessionId));
1367 chainCreated = true;
1368 } else {
1369 effect = chain->getEffectFromDesc_l(desc);
1370 }
1371
1372 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
1373
1374 if (effect == 0) {
Mikhail Naganov2247f7b2017-01-13 11:52:54 -08001375 effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
Eric Laurent81784c32012-11-19 14:55:58 -08001376 // create a new effect module if none present in the chain
Eric Laurent5d885392019-12-13 10:56:31 -08001377 lStatus = chain->createEffect_l(effect, desc, effectId, sessionId, pinned);
Eric Laurent81784c32012-11-19 14:55:58 -08001378 if (lStatus != NO_ERROR) {
1379 goto Exit;
1380 }
1381 effectCreated = true;
1382
jiabin10d86fd2019-10-31 17:20:42 -07001383 // FIXME: use vector of device and address when effect interface is ready.
jiabinb8269fd2019-11-11 12:16:27 -08001384 effect->setDevices(outDeviceTypeAddrs());
1385 effect->setInputDevice(inDeviceTypeAddr());
Eric Laurent81784c32012-11-19 14:55:58 -08001386 effect->setMode(mAudioFlinger->getMode());
1387 effect->setAudioSource(mAudioSource);
1388 }
1389 // create effect handle and connect it to effect module
1390 handle = new EffectHandle(effect, client, effectClient, priority);
Glenn Kastene75da402013-11-20 13:54:52 -08001391 lStatus = handle->initCheck();
1392 if (lStatus == OK) {
1393 lStatus = effect->addHandle(handle.get());
1394 }
Eric Laurent81784c32012-11-19 14:55:58 -08001395 if (enabled != NULL) {
1396 *enabled = (int)effect->isEnabled();
1397 }
1398 }
1399
1400Exit:
1401 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
1402 Mutex::Autolock _l(mLock);
1403 if (effectCreated) {
1404 chain->removeEffect_l(effect);
1405 }
Eric Laurent81784c32012-11-19 14:55:58 -08001406 if (chainCreated) {
1407 removeEffectChain_l(chain);
1408 }
haobo101735295ff7b0d2017-03-17 17:44:03 +08001409 // handle must be cleared by caller to avoid deadlock.
Eric Laurent81784c32012-11-19 14:55:58 -08001410 }
1411
Glenn Kasten9156ef32013-08-06 15:39:08 -07001412 *status = lStatus;
Eric Laurent81784c32012-11-19 14:55:58 -08001413 return handle;
1414}
1415
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001416void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle,
1417 bool unpinIfLast)
1418{
1419 bool remove = false;
1420 sp<EffectModule> effect;
1421 {
1422 Mutex::Autolock _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001423 sp<EffectBase> effectBase = handle->effect().promote();
1424 if (effectBase == nullptr) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001425 return;
1426 }
Eric Laurent9b2064c2019-11-22 17:25:04 -08001427 effect = effectBase->asEffectModule();
1428 if (effect == nullptr) {
1429 return;
1430 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001431 // restore suspended effects if the disconnected handle was enabled and the last one.
1432 remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
1433 if (remove) {
1434 removeEffect_l(effect, true);
1435 }
1436 }
1437 if (remove) {
1438 mAudioFlinger->updateOrphanEffectChains(effect);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001439 if (handle->enabled()) {
Eric Laurent5d885392019-12-13 10:56:31 -08001440 effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001441 }
1442 }
1443}
1444
Eric Laurent5d885392019-12-13 10:56:31 -08001445void AudioFlinger::ThreadBase::onEffectEnable(const sp<EffectModule>& effect) {
1446 if (mType == OFFLOAD || mType == MMAP) {
1447 Mutex::Autolock _l(mLock);
1448 broadcast_l();
1449 }
1450 if (!effect->isOffloadable()) {
1451 if (mType == ThreadBase::OFFLOAD) {
1452 PlaybackThread *t = (PlaybackThread *)this;
1453 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1454 }
1455 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1456 mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1457 }
1458 }
1459}
1460
1461void AudioFlinger::ThreadBase::onEffectDisable() {
1462 if (mType == OFFLOAD || mType == MMAP) {
1463 Mutex::Autolock _l(mLock);
1464 broadcast_l();
1465 }
1466}
1467
Glenn Kastend848eb42016-03-08 13:42:11 -08001468sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId,
1469 int effectId)
Eric Laurent81784c32012-11-19 14:55:58 -08001470{
1471 Mutex::Autolock _l(mLock);
1472 return getEffect_l(sessionId, effectId);
1473}
1474
Glenn Kastend848eb42016-03-08 13:42:11 -08001475sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId,
1476 int effectId)
Eric Laurent81784c32012-11-19 14:55:58 -08001477{
1478 sp<EffectChain> chain = getEffectChain_l(sessionId);
1479 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
1480}
1481
Eric Laurent6c796322019-04-09 14:13:17 -07001482std::vector<int> AudioFlinger::ThreadBase::getEffectIds_l(audio_session_t sessionId)
1483{
1484 sp<EffectChain> chain = getEffectChain_l(sessionId);
1485 return chain != nullptr ? chain->getEffectIds() : std::vector<int>{};
1486}
1487
Eric Laurent81784c32012-11-19 14:55:58 -08001488// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
1489// PlaybackThread::mLock held
1490status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
1491{
1492 // check for existing effect chain with the requested audio session
Glenn Kastend848eb42016-03-08 13:42:11 -08001493 audio_session_t sessionId = effect->sessionId();
Eric Laurent81784c32012-11-19 14:55:58 -08001494 sp<EffectChain> chain = getEffectChain_l(sessionId);
1495 bool chainCreated = false;
1496
Eric Laurent5baf2af2013-09-12 17:37:00 -07001497 ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(),
Glenn Kasten49f36ba2017-12-06 13:02:02 -08001498 "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %#x",
Eric Laurent5baf2af2013-09-12 17:37:00 -07001499 this, effect->desc().name, effect->desc().flags);
1500
Eric Laurent81784c32012-11-19 14:55:58 -08001501 if (chain == 0) {
1502 // create a new chain for this session
1503 ALOGV("addEffect_l() new effect chain for session %d", sessionId);
1504 chain = new EffectChain(this, sessionId);
1505 addEffectChain_l(chain);
1506 chain->setStrategy(getStrategyForSession_l(sessionId));
1507 chainCreated = true;
1508 }
1509 ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
1510
1511 if (chain->getEffectFromId_l(effect->id()) != 0) {
1512 ALOGW("addEffect_l() %p effect %s already present in chain %p",
1513 this, effect->desc().name, chain.get());
1514 return BAD_VALUE;
1515 }
1516
Eric Laurent5baf2af2013-09-12 17:37:00 -07001517 effect->setOffloaded(mType == OFFLOAD, mId);
1518
Eric Laurent81784c32012-11-19 14:55:58 -08001519 status_t status = chain->addEffect_l(effect);
1520 if (status != NO_ERROR) {
1521 if (chainCreated) {
1522 removeEffectChain_l(chain);
1523 }
1524 return status;
1525 }
1526
jiabinb8269fd2019-11-11 12:16:27 -08001527 effect->setDevices(outDeviceTypeAddrs());
1528 effect->setInputDevice(inDeviceTypeAddr());
Eric Laurent81784c32012-11-19 14:55:58 -08001529 effect->setMode(mAudioFlinger->getMode());
1530 effect->setAudioSource(mAudioSource);
Eric Laurentd8365c52017-07-16 15:27:05 -07001531
Eric Laurent81784c32012-11-19 14:55:58 -08001532 return NO_ERROR;
1533}
1534
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001535void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) {
Eric Laurent81784c32012-11-19 14:55:58 -08001536
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001537 ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get());
Eric Laurent81784c32012-11-19 14:55:58 -08001538 effect_descriptor_t desc = effect->desc();
1539 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1540 detachAuxEffect_l(effect->id());
1541 }
1542
Eric Laurent5d885392019-12-13 10:56:31 -08001543 sp<EffectChain> chain = effect->callback()->chain().promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001544 if (chain != 0) {
1545 // remove effect chain if removing last effect
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001546 if (chain->removeEffect_l(effect, release) == 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08001547 removeEffectChain_l(chain);
1548 }
1549 } else {
1550 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
1551 }
1552}
1553
1554void AudioFlinger::ThreadBase::lockEffectChains_l(
1555 Vector< sp<AudioFlinger::EffectChain> >& effectChains)
1556{
1557 effectChains = mEffectChains;
1558 for (size_t i = 0; i < mEffectChains.size(); i++) {
1559 mEffectChains[i]->lock();
1560 }
1561}
1562
1563void AudioFlinger::ThreadBase::unlockEffectChains(
1564 const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
1565{
1566 for (size_t i = 0; i < effectChains.size(); i++) {
1567 effectChains[i]->unlock();
1568 }
1569}
1570
Glenn Kastend848eb42016-03-08 13:42:11 -08001571sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId)
Eric Laurent81784c32012-11-19 14:55:58 -08001572{
1573 Mutex::Autolock _l(mLock);
1574 return getEffectChain_l(sessionId);
1575}
1576
Glenn Kastend848eb42016-03-08 13:42:11 -08001577sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId)
1578 const
Eric Laurent81784c32012-11-19 14:55:58 -08001579{
1580 size_t size = mEffectChains.size();
1581 for (size_t i = 0; i < size; i++) {
1582 if (mEffectChains[i]->sessionId() == sessionId) {
1583 return mEffectChains[i];
1584 }
1585 }
1586 return 0;
1587}
1588
1589void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
1590{
1591 Mutex::Autolock _l(mLock);
1592 size_t size = mEffectChains.size();
1593 for (size_t i = 0; i < size; i++) {
1594 mEffectChains[i]->setMode_l(mode);
1595 }
1596}
1597
Mikhail Naganovdc769682018-05-04 15:34:08 -07001598void AudioFlinger::ThreadBase::toAudioPortConfig(struct audio_port_config *config)
Eric Laurent83b88082014-06-20 18:31:16 -07001599{
1600 config->type = AUDIO_PORT_TYPE_MIX;
1601 config->ext.mix.handle = mId;
1602 config->sample_rate = mSampleRate;
1603 config->format = mFormat;
1604 config->channel_mask = mChannelMask;
1605 config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
1606 AUDIO_PORT_CONFIG_FORMAT;
1607}
1608
Eric Laurent72e3f392015-05-20 14:43:50 -07001609void AudioFlinger::ThreadBase::systemReady()
1610{
1611 Mutex::Autolock _l(mLock);
1612 if (mSystemReady) {
1613 return;
1614 }
1615 mSystemReady = true;
1616
1617 for (size_t i = 0; i < mPendingConfigEvents.size(); i++) {
1618 sendConfigEvent_l(mPendingConfigEvents.editItemAt(i));
1619 }
1620 mPendingConfigEvents.clear();
1621}
1622
Andy Hungdae27702016-10-31 14:01:16 -07001623template <typename T>
1624ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) {
1625 ssize_t index = mActiveTracks.indexOf(track);
1626 if (index >= 0) {
1627 ALOGW("ActiveTracks<T>::add track %p already there", track.get());
1628 return index;
1629 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001630 logTrack("add", track);
Andy Hungdae27702016-10-31 14:01:16 -07001631 mActiveTracksGeneration++;
1632 mLatestActiveTrack = track;
1633 ++mBatteryCounter[track->uid()].second;
Kevin Rocard069c2712018-03-29 19:09:14 -07001634 mHasChanged = true;
Andy Hungdae27702016-10-31 14:01:16 -07001635 return mActiveTracks.add(track);
1636}
1637
1638template <typename T>
1639ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) {
1640 ssize_t index = mActiveTracks.remove(track);
1641 if (index < 0) {
1642 ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get());
1643 return index;
1644 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001645 logTrack("remove", track);
Andy Hungdae27702016-10-31 14:01:16 -07001646 mActiveTracksGeneration++;
1647 --mBatteryCounter[track->uid()].second;
1648 // mLatestActiveTrack is not cleared even if is the same as track.
Kevin Rocard069c2712018-03-29 19:09:14 -07001649 mHasChanged = true;
Andy Hung8946a282018-04-19 20:04:56 -07001650#ifdef TEE_SINK
1651 track->dumpTee(-1 /* fd */, "_REMOVE");
1652#endif
Andy Hungdae27702016-10-31 14:01:16 -07001653 return index;
1654}
1655
1656template <typename T>
1657void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() {
1658 for (const sp<T> &track : mActiveTracks) {
1659 BatteryNotifier::getInstance().noteStopAudio(track->uid());
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001660 logTrack("clear", track);
Andy Hungdae27702016-10-31 14:01:16 -07001661 }
1662 mLastActiveTracksGeneration = mActiveTracksGeneration;
Kevin Rocard069c2712018-03-29 19:09:14 -07001663 if (!mActiveTracks.empty()) { mHasChanged = true; }
Andy Hungdae27702016-10-31 14:01:16 -07001664 mActiveTracks.clear();
1665 mLatestActiveTrack.clear();
1666 mBatteryCounter.clear();
1667}
1668
1669template <typename T>
1670void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState(
1671 sp<ThreadBase> thread, bool force) {
1672 // Updates ActiveTracks client uids to the thread wakelock.
1673 if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) {
1674 thread->updateWakeLockUids_l(getWakeLockUids());
1675 mLastActiveTracksGeneration = mActiveTracksGeneration;
1676 }
1677
1678 // Updates BatteryNotifier uids
1679 for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) {
1680 const uid_t uid = it->first;
1681 ssize_t &previous = it->second.first;
1682 ssize_t &current = it->second.second;
1683 if (current > 0) {
1684 if (previous == 0) {
1685 BatteryNotifier::getInstance().noteStartAudio(uid);
1686 }
1687 previous = current;
1688 ++it;
1689 } else if (current == 0) {
1690 if (previous > 0) {
1691 BatteryNotifier::getInstance().noteStopAudio(uid);
1692 }
1693 it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase.
1694 } else /* (current < 0) */ {
1695 LOG_ALWAYS_FATAL("negative battery count %zd", current);
1696 }
1697 }
1698}
Eric Laurent83b88082014-06-20 18:31:16 -07001699
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001700template <typename T>
Kevin Rocard069c2712018-03-29 19:09:14 -07001701bool AudioFlinger::ThreadBase::ActiveTracks<T>::readAndClearHasChanged() {
1702 const bool hasChanged = mHasChanged;
1703 mHasChanged = false;
1704 return hasChanged;
1705}
1706
1707template <typename T>
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001708void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack(
1709 const char *funcName, const sp<T> &track) const {
1710 if (mLocalLog != nullptr) {
1711 String8 result;
1712 track->appendDump(result, false /* active */);
1713 mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string());
1714 }
1715}
1716
Eric Laurent6acd1d42017-01-04 14:23:29 -08001717void AudioFlinger::ThreadBase::broadcast_l()
1718{
1719 // Thread could be blocked waiting for async
1720 // so signal it to handle state changes immediately
1721 // If threadLoop is currently unlocked a signal of mWaitWorkCV will
1722 // be lost so we also flag to prevent it blocking on mWaitWorkCV
1723 mSignalPending = true;
1724 mWaitWorkCV.broadcast();
1725}
1726
Andy Hungd0979812019-02-21 15:51:44 -08001727// Call only from threadLoop() or when it is idle.
1728// Do not call from high performance code as this may do binder rpc to the MediaMetrics service.
1729void AudioFlinger::ThreadBase::sendStatistics(bool force)
1730{
1731 // Do not log if we have no stats.
1732 // We choose the timestamp verifier because it is the most likely item to be present.
1733 const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN;
1734 if (nstats == 0) {
1735 return;
1736 }
1737
1738 // Don't log more frequently than once per 12 hours.
1739 // We use BOOTTIME to include suspend time.
1740 const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME);
1741 const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0
1742 if (!force && sinceNs <= 12 * NANOS_PER_HOUR) {
1743 return;
1744 }
1745
1746 mLastRecordedTimestampVerifierN = mTimestampVerifier.getN();
1747 mLastRecordedTimeNs = timeNs;
1748
1749 std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create("audiothread"));
1750
1751#define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors.
1752
1753 // thread configuration
1754 item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle
1755 // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId);
1756 item->setCString(MM_PREFIX "type", threadTypeToString(mType));
1757 item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate);
1758 item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask);
1759 item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str());
1760 item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount);
jiabin10d86fd2019-10-31 17:20:42 -07001761 item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes()).c_str());
1762 item->setCString(MM_PREFIX "inDevice", toString(inDeviceType()).c_str());
Andy Hungd0979812019-02-21 15:51:44 -08001763
1764 // thread statistics
1765 if (mIoJitterMs.getN() > 0) {
1766 item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean());
1767 item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev());
1768 }
1769 if (mProcessTimeMs.getN() > 0) {
1770 item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean());
1771 item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev());
1772 }
1773 const auto tsjitter = mTimestampVerifier.getJitterMs();
1774 if (tsjitter.getN() > 0) {
1775 item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean());
1776 item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev());
1777 }
1778 if (mLatencyMs.getN() > 0) {
1779 item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean());
1780 item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev());
1781 }
1782
1783 item->selfrecord();
1784}
1785
Eric Laurent81784c32012-11-19 14:55:58 -08001786// ----------------------------------------------------------------------------
1787// Playback
1788// ----------------------------------------------------------------------------
1789
1790AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1791 AudioStreamOut* output,
1792 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001793 type_t type,
Eric Laurente93cc032016-05-05 10:15:10 -07001794 bool systemReady)
jiabin10d86fd2019-10-31 17:20:42 -07001795 : ThreadBase(audioFlinger, id, type, systemReady),
Andy Hung2098f272014-02-27 14:00:06 -08001796 mNormalFrameCount(0), mSinkBuffer(NULL),
Andy Hung6146c082014-03-18 11:56:15 -07001797 mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
Andy Hung69aed5f2014-02-25 17:24:40 -08001798 mMixerBuffer(NULL),
1799 mMixerBufferSize(0),
1800 mMixerBufferFormat(AUDIO_FORMAT_INVALID),
1801 mMixerBufferValid(false),
Andy Hung6146c082014-03-18 11:56:15 -07001802 mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
Andy Hung98ef9782014-03-04 14:46:50 -08001803 mEffectBuffer(NULL),
1804 mEffectBufferSize(0),
1805 mEffectBufferFormat(AUDIO_FORMAT_INVALID),
1806 mEffectBufferValid(false),
Glenn Kastenc1fac192013-08-06 07:41:36 -07001807 mSuspended(0), mBytesWritten(0),
Andy Hungc54b1ff2016-02-23 14:07:07 -08001808 mFramesWritten(0),
Andy Hung238fa3d2016-07-28 10:53:22 -07001809 mSuspendedFrames(0),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001810 mActiveTracks(&this->mLocalLog),
Eric Laurent81784c32012-11-19 14:55:58 -08001811 // mStreamTypes[] initialized in constructor body
Andy Hung1bc088a2018-02-09 15:57:31 -08001812 mTracks(type == MIXER),
Eric Laurent81784c32012-11-19 14:55:58 -08001813 mOutput(output),
Andy Hung446f4df2019-02-21 12:26:41 -08001814 mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
Eric Laurent81784c32012-11-19 14:55:58 -08001815 mMixerStatus(MIXER_IDLE),
1816 mMixerStatusIgnoringFastTracks(MIXER_IDLE),
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001817 mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs),
Eric Laurentbfb1b832013-01-07 09:53:42 -08001818 mBytesRemaining(0),
1819 mCurrentWriteLength(0),
1820 mUseAsyncWrite(false),
Eric Laurent3b4529e2013-09-05 18:09:19 -07001821 mWriteAckSequence(0),
1822 mDrainSequence(0),
Eric Laurent81784c32012-11-19 14:55:58 -08001823 mScreenState(AudioFlinger::mScreenState),
1824 // index 0 is reserved for normal mixer's submix
Glenn Kastendc2c50b2016-04-21 08:13:14 -07001825 mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1),
Eric Laurent7c29ec92017-09-20 17:54:22 -07001826 mHwSupportsPause(false), mHwPaused(false), mFlushPending(false),
1827 mLeftVolFloat(-1.0), mRightVolFloat(-1.0)
Eric Laurent81784c32012-11-19 14:55:58 -08001828{
Glenn Kastend7dca052015-03-05 16:05:54 -08001829 snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id);
1830 mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
Eric Laurent81784c32012-11-19 14:55:58 -08001831
1832 // Assumes constructor is called by AudioFlinger with it's mLock held, but
1833 // it would be safer to explicitly pass initial masterVolume/masterMute as
1834 // parameter.
1835 //
1836 // If the HAL we are using has support for master volume or master mute,
1837 // then do not attenuate or mute during mixing (just leave the volume at 1.0
1838 // and the mute set to false).
1839 mMasterVolume = audioFlinger->masterVolume_l();
1840 mMasterMute = audioFlinger->masterMute_l();
1841 if (mOutput && mOutput->audioHwDev) {
1842 if (mOutput->audioHwDev->canSetMasterVolume()) {
1843 mMasterVolume = 1.0;
1844 }
1845
1846 if (mOutput->audioHwDev->canSetMasterMute()) {
1847 mMasterMute = false;
1848 }
Andy Hungc8fddf32018-08-08 18:32:37 -07001849 mIsMsdDevice = strcmp(
1850 mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
Eric Laurent81784c32012-11-19 14:55:58 -08001851 }
1852
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001853 readOutputParameters_l();
Eric Laurent81784c32012-11-19 14:55:58 -08001854
Andy Hungc8fddf32018-08-08 18:32:37 -07001855 // TODO: We may also match on address as well as device type for
1856 // AUDIO_DEVICE_OUT_BUS, AUDIO_DEVICE_OUT_ALL_A2DP, AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Dean Wheatleyf8726f02019-12-02 14:12:01 +11001857 if (type == MIXER || type == DIRECT || type == OFFLOAD) {
jiabin10d86fd2019-10-31 17:20:42 -07001858 // TODO: This property should be ensure that only contains one single device type.
1859 mTimestampCorrectedDevice = (audio_devices_t)property_get_int64(
1860 "audio.timestamp.corrected_output_device",
Andy Hungc8fddf32018-08-08 18:32:37 -07001861 (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD
1862 : AUDIO_DEVICE_NONE));
1863 }
1864
Eric Laurent223fd5c2014-11-11 13:43:36 -08001865 // ++ operator does not compile
Eric Laurent98e38192018-02-15 18:31:53 -08001866 for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_FOR_POLICY_CNT;
Eric Laurent81784c32012-11-19 14:55:58 -08001867 stream = (audio_stream_type_t) (stream + 1)) {
Eric Laurent98e38192018-02-15 18:31:53 -08001868 mStreamTypes[stream].volume = 0.0f;
Eric Laurent81784c32012-11-19 14:55:58 -08001869 mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
1870 }
Eric Laurent98e38192018-02-15 18:31:53 -08001871 // Audio patch volume is always max
1872 mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f;
1873 mStreamTypes[AUDIO_STREAM_PATCH].mute = false;
Eric Laurent81784c32012-11-19 14:55:58 -08001874}
1875
1876AudioFlinger::PlaybackThread::~PlaybackThread()
1877{
Glenn Kasten9e58b552013-01-18 15:09:48 -08001878 mAudioFlinger->unregisterWriter(mNBLogWriter);
Andy Hung010a1a12014-03-13 13:57:33 -07001879 free(mSinkBuffer);
Andy Hung69aed5f2014-02-25 17:24:40 -08001880 free(mMixerBuffer);
Andy Hung98ef9782014-03-04 14:46:50 -08001881 free(mEffectBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08001882}
1883
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001884// Thread virtuals
1885
1886void AudioFlinger::PlaybackThread::onFirstRef()
Eric Laurent81784c32012-11-19 14:55:58 -08001887{
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001888 run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
Eric Laurent81784c32012-11-19 14:55:58 -08001889}
1890
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001891// ThreadBase virtuals
1892void AudioFlinger::PlaybackThread::preExit()
1893{
1894 ALOGV(" preExit()");
1895 // FIXME this is using hard-coded strings but in the future, this functionality will be
1896 // converted to use audio HAL extensions required to support tunneling
1897 status_t result = mOutput->stream->setParameters(String8("exiting=1"));
1898 ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result);
1899}
1900
1901void AudioFlinger::PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08001902{
Eric Laurent81784c32012-11-19 14:55:58 -08001903 String8 result;
1904
Marco Nelissenb2208842014-02-07 14:00:50 -08001905 result.appendFormat(" Stream volumes in dB: ");
Eric Laurent81784c32012-11-19 14:55:58 -08001906 for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
1907 const stream_type_t *st = &mStreamTypes[i];
1908 if (i > 0) {
1909 result.appendFormat(", ");
1910 }
1911 result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
1912 if (st->mute) {
1913 result.append("M");
1914 }
1915 }
1916 result.append("\n");
1917 write(fd, result.string(), result.length());
1918 result.clear();
1919
Eric Laurent81784c32012-11-19 14:55:58 -08001920 // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way.
1921 FastTrackUnderruns underruns = getFastTrackUnderruns(0);
Elliott Hughes87cebad2014-05-22 10:14:43 -07001922 dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n",
Eric Laurent81784c32012-11-19 14:55:58 -08001923 underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
Marco Nelissenb2208842014-02-07 14:00:50 -08001924
1925 size_t numtracks = mTracks.size();
1926 size_t numactive = mActiveTracks.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001927 dprintf(fd, " %zu Tracks", numtracks);
Marco Nelissenb2208842014-02-07 14:00:50 -08001928 size_t numactiveseen = 0;
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001929 const char *prefix = " ";
Marco Nelissenb2208842014-02-07 14:00:50 -08001930 if (numtracks) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001931 dprintf(fd, " of which %zu are active\n", numactive);
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001932 result.append(prefix);
Andy Hungf6ab58d2018-05-25 12:50:39 -07001933 mTracks[0]->appendDumpHeader(result);
Marco Nelissenb2208842014-02-07 14:00:50 -08001934 for (size_t i = 0; i < numtracks; ++i) {
1935 sp<Track> track = mTracks[i];
1936 if (track != 0) {
1937 bool active = mActiveTracks.indexOf(track) >= 0;
1938 if (active) {
1939 numactiveseen++;
1940 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001941 result.append(prefix);
1942 track->appendDump(result, active);
Marco Nelissenb2208842014-02-07 14:00:50 -08001943 }
1944 }
1945 } else {
1946 result.append("\n");
1947 }
1948 if (numactiveseen != numactive) {
1949 // some tracks in the active list were not in the tracks list
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001950 result.append(" The following tracks are in the active list but"
Marco Nelissenb2208842014-02-07 14:00:50 -08001951 " not in the track list\n");
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001952 result.append(prefix);
Andy Hungf6ab58d2018-05-25 12:50:39 -07001953 mActiveTracks[0]->appendDumpHeader(result);
Marco Nelissenb2208842014-02-07 14:00:50 -08001954 for (size_t i = 0; i < numactive; ++i) {
Andy Hungdae27702016-10-31 14:01:16 -07001955 sp<Track> track = mActiveTracks[i];
1956 if (mTracks.indexOf(track) < 0) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001957 result.append(prefix);
1958 track->appendDump(result, true /* active */);
Marco Nelissenb2208842014-02-07 14:00:50 -08001959 }
1960 }
1961 }
1962
1963 write(fd, result.string(), result.size());
Eric Laurent81784c32012-11-19 14:55:58 -08001964}
1965
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001966void AudioFlinger::PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08001967{
Mikhail Naganovdfb411f2018-09-11 13:39:56 -07001968 dprintf(fd, " Master mute: %s\n", mMasterMute ? "on" : "off");
jiabin245cdd92018-12-07 17:55:15 -08001969 if (mHapticChannelMask != AUDIO_CHANNEL_NONE) {
1970 dprintf(fd, " Haptic channel mask: %#x (%s)\n", mHapticChannelMask,
1971 channelMaskToString(mHapticChannelMask, true /* output */).c_str());
1972 }
Elliott Hughes87cebad2014-05-22 10:14:43 -07001973 dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount);
Elliott Hughes87cebad2014-05-22 10:14:43 -07001974 dprintf(fd, " Total writes: %d\n", mNumWrites);
1975 dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites);
1976 dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no");
1977 dprintf(fd, " Suspend count: %d\n", mSuspended);
1978 dprintf(fd, " Sink buffer : %p\n", mSinkBuffer);
1979 dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer);
1980 dprintf(fd, " Effect buffer: %p\n", mEffectBuffer);
1981 dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask);
Eric Laurent42537be2016-01-08 17:16:42 -08001982 dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs);
Glenn Kasten97b7b752014-09-28 13:04:24 -07001983 AudioStreamOut *output = mOutput;
1984 audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001985 dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n",
Andy Hung9b181952019-02-25 14:53:36 -08001986 output, flags, toString(flags).c_str());
Andy Hungb54c8542016-09-21 12:55:15 -07001987 dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten);
1988 dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames);
1989 if (mPipeSink.get() != nullptr) {
1990 dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten());
1991 }
1992 if (output != nullptr) {
1993 dprintf(fd, " Hal stream dump:\n");
1994 (void)output->stream->dump(fd);
1995 }
Eric Laurent81784c32012-11-19 14:55:58 -08001996}
1997
Eric Laurent81784c32012-11-19 14:55:58 -08001998// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
1999sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
2000 const sp<AudioFlinger::Client>& client,
2001 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002002 const audio_attributes_t& attr,
Eric Laurent21da6472017-11-09 16:29:26 -08002003 uint32_t *pSampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -08002004 audio_format_t format,
2005 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08002006 size_t *pFrameCount,
Eric Laurent21da6472017-11-09 16:29:26 -08002007 size_t *pNotificationFrameCount,
2008 uint32_t notificationsPerBuffer,
2009 float speed,
Eric Laurent81784c32012-11-19 14:55:58 -08002010 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -08002011 audio_session_t sessionId,
Eric Laurent05067782016-06-01 18:27:28 -07002012 audio_output_flags_t *flags,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002013 pid_t creatorPid,
Eric Laurent81784c32012-11-19 14:55:58 -08002014 pid_t tid,
Andy Hung1f12a8a2016-11-07 16:10:30 -08002015 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08002016 status_t *status,
2017 audio_port_handle_t portId)
Eric Laurent81784c32012-11-19 14:55:58 -08002018{
Glenn Kasten74935e42013-12-19 08:56:45 -08002019 size_t frameCount = *pFrameCount;
Eric Laurent21da6472017-11-09 16:29:26 -08002020 size_t notificationFrameCount = *pNotificationFrameCount;
Eric Laurent81784c32012-11-19 14:55:58 -08002021 sp<Track> track;
2022 status_t lStatus;
Eric Laurent05067782016-06-01 18:27:28 -07002023 audio_output_flags_t outputFlags = mOutput->flags;
Eric Laurent21da6472017-11-09 16:29:26 -08002024 audio_output_flags_t requestedFlags = *flags;
Eric Laurent9b11c022018-06-06 19:19:22 -07002025 uint32_t sampleRate;
2026
2027 if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) {
2028 lStatus = BAD_VALUE;
2029 goto Exit;
2030 }
Eric Laurent21da6472017-11-09 16:29:26 -08002031
2032 if (*pSampleRate == 0) {
2033 *pSampleRate = mSampleRate;
2034 }
Eric Laurent9b11c022018-06-06 19:19:22 -07002035 sampleRate = *pSampleRate;
Eric Laurent05067782016-06-01 18:27:28 -07002036
2037 // special case for FAST flag considered OK if fast mixer is present
2038 if (hasFastMixer()) {
2039 outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST);
2040 }
2041
2042 // Check if requested flags are compatible with output stream flags
2043 if ((*flags & outputFlags) != *flags) {
2044 ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)",
2045 *flags, outputFlags);
2046 *flags = (audio_output_flags_t)(*flags & outputFlags);
2047 }
Eric Laurent81784c32012-11-19 14:55:58 -08002048
Eric Laurent81784c32012-11-19 14:55:58 -08002049 // client expresses a preference for FAST, but we get the final say
Eric Laurent05067782016-06-01 18:27:28 -07002050 if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
Eric Laurent81784c32012-11-19 14:55:58 -08002051 if (
Eric Laurent81784c32012-11-19 14:55:58 -08002052 // PCM data
2053 audio_is_linear_pcm(format) &&
Andy Hung1f439e12015-05-19 12:57:41 -07002054 // TODO: extract as a data library function that checks that a computationally
2055 // expensive downmixer is not required: isFastOutputChannelConversion()
jiabin245cdd92018-12-07 17:55:15 -08002056 (channelMask == (mChannelMask | mHapticChannelMask) ||
Andy Hung1f439e12015-05-19 12:57:41 -07002057 mChannelMask != AUDIO_CHANNEL_OUT_STEREO ||
2058 (channelMask == AUDIO_CHANNEL_OUT_MONO
2059 /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) &&
Eric Laurent81784c32012-11-19 14:55:58 -08002060 // hardware sample rate
2061 (sampleRate == mSampleRate) &&
Eric Laurent81784c32012-11-19 14:55:58 -08002062 // normal mixer has an associated fast mixer
2063 hasFastMixer() &&
2064 // there are sufficient fast track slots available
2065 (mFastTrackAvailMask != 0)
2066 // FIXME test that MixerThread for this fast track has a capable output HAL
2067 // FIXME add a permission test also?
2068 ) {
Andy Hunge0a269a2016-03-23 15:13:42 -07002069 // static tracks can have any nonzero framecount, streaming tracks check against minimum.
2070 if (sharedBuffer == 0) {
Glenn Kasten03490092014-05-27 12:30:54 -07002071 // read the fast track multiplier property the first time it is needed
2072 int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit);
2073 if (ok != 0) {
2074 ALOGE("%s pthread_once failed: %d", __func__, ok);
2075 }
Andy Hunge0a269a2016-03-23 15:13:42 -07002076 frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0
Eric Laurent81784c32012-11-19 14:55:58 -08002077 }
Eric Laurent4c415062016-06-17 16:14:16 -07002078
2079 // check compatibility with audio effects.
2080 { // scope for mLock
2081 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002082 for (audio_session_t session : {
Eric Laurenta20c4e92019-11-12 15:55:51 -08002083 AUDIO_SESSION_DEVICE,
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002084 AUDIO_SESSION_OUTPUT_STAGE,
2085 AUDIO_SESSION_OUTPUT_MIX,
2086 sessionId,
2087 }) {
2088 sp<EffectChain> chain = getEffectChain_l(session);
2089 if (chain.get() != nullptr) {
2090 audio_output_flags_t old = *flags;
2091 chain->checkOutputFlagCompatibility(flags);
2092 if (old != *flags) {
2093 ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x",
2094 (int)session, (int)old, (int)*flags);
2095 }
Eric Laurent4c415062016-06-17 16:14:16 -07002096 }
2097 }
2098 }
Eric Laurent122f7e72016-06-29 11:53:29 -07002099 ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0,
Eric Laurent4c415062016-06-17 16:14:16 -07002100 "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
2101 frameCount, mFrameCount);
Eric Laurent81784c32012-11-19 14:55:58 -08002102 } else {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002103 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu "
2104 "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
Andy Hung6146c082014-03-18 11:56:15 -07002105 "sampleRate=%u mSampleRate=%u "
Eric Laurent81784c32012-11-19 14:55:58 -08002106 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
Glenn Kastend79072e2016-01-06 08:41:20 -08002107 sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
Eric Laurent81784c32012-11-19 14:55:58 -08002108 audio_is_linear_pcm(format),
2109 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
Eric Laurent4c415062016-06-17 16:14:16 -07002110 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
Andy Hung0e48d252015-01-26 11:43:15 -08002111 }
2112 }
Eric Laurent21da6472017-11-09 16:29:26 -08002113
2114 if (!audio_has_proportional_frames(format)) {
2115 if (sharedBuffer != 0) {
2116 // Same comment as below about ignoring frameCount parameter for set()
2117 frameCount = sharedBuffer->size();
2118 } else if (frameCount == 0) {
2119 frameCount = mNormalFrameCount;
2120 }
2121 if (notificationFrameCount != frameCount) {
2122 notificationFrameCount = frameCount;
2123 }
2124 } else if (sharedBuffer != 0) {
2125 // FIXME: Ensure client side memory buffers need
2126 // not have additional alignment beyond sample
2127 // (e.g. 16 bit stereo accessed as 32 bit frame).
2128 size_t alignment = audio_bytes_per_sample(format);
2129 if (alignment & 1) {
2130 // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java).
2131 alignment = 1;
2132 }
2133 uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
2134 size_t frameSize = channelCount * audio_bytes_per_sample(format);
2135 if (channelCount > 1) {
2136 // More than 2 channels does not require stronger alignment than stereo
2137 alignment <<= 1;
2138 }
2139 if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
2140 ALOGE("Invalid buffer alignment: address %p, channel count %u",
2141 sharedBuffer->pointer(), channelCount);
2142 lStatus = BAD_VALUE;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002143 goto Exit;
2144 }
Eric Laurent21da6472017-11-09 16:29:26 -08002145
2146 // When initializing a shared buffer AudioTrack via constructors,
2147 // there's no frameCount parameter.
2148 // But when initializing a shared buffer AudioTrack via set(),
2149 // there _is_ a frameCount parameter. We silently ignore it.
2150 frameCount = sharedBuffer->size() / frameSize;
2151 } else {
2152 size_t minFrameCount = 0;
2153 // For fast tracks we try to respect the application's request for notifications per buffer.
2154 if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2155 if (notificationsPerBuffer > 0) {
2156 // Avoid possible arithmetic overflow during multiplication.
2157 if (notificationsPerBuffer > SIZE_MAX / mFrameCount) {
2158 ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu",
2159 notificationsPerBuffer, mFrameCount);
2160 } else {
2161 minFrameCount = mFrameCount * notificationsPerBuffer;
2162 }
2163 }
2164 } else {
2165 // For normal PCM streaming tracks, update minimum frame count.
2166 // Buffer depth is forced to be at least 2 x the normal mixer frame count and
2167 // cover audio hardware latency.
2168 // This is probably too conservative, but legacy application code may depend on it.
2169 // If you change this calculation, also review the start threshold which is related.
2170 uint32_t latencyMs = latency_l();
2171 if (latencyMs == 0) {
2172 ALOGE("Error when retrieving output stream latency");
2173 lStatus = UNKNOWN_ERROR;
2174 goto Exit;
2175 }
2176
2177 minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount,
2178 mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/);
2179
Eric Laurent81784c32012-11-19 14:55:58 -08002180 }
Eric Laurent21da6472017-11-09 16:29:26 -08002181 if (frameCount < minFrameCount) {
Eric Laurent81784c32012-11-19 14:55:58 -08002182 frameCount = minFrameCount;
2183 }
Eric Laurent81784c32012-11-19 14:55:58 -08002184 }
Eric Laurent21da6472017-11-09 16:29:26 -08002185
2186 // Make sure that application is notified with sufficient margin before underrun.
2187 // The client can divide the AudioTrack buffer into sub-buffers,
2188 // and expresses its desire to server as the notification frame count.
2189 if (sharedBuffer == 0 && audio_is_linear_pcm(format)) {
2190 size_t maxNotificationFrames;
2191 if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
2192 // notify every HAL buffer, regardless of the size of the track buffer
2193 maxNotificationFrames = mFrameCount;
2194 } else {
Andy Hungfa117802019-04-12 13:50:39 -07002195 // Triple buffer the notification period for a triple buffered mixer period;
2196 // otherwise, double buffering for the notification period is fine.
2197 //
2198 // TODO: This should be moved to AudioTrack to modify the notification period
2199 // on AudioTrack::setBufferSizeInFrames() changes.
2200 const int nBuffering =
2201 (uint64_t{frameCount} * mSampleRate)
2202 / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2;
2203
Eric Laurent21da6472017-11-09 16:29:26 -08002204 maxNotificationFrames = frameCount / nBuffering;
2205 // If client requested a fast track but this was denied, then use the smaller maximum.
2206 if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) {
2207 size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000;
2208 if (maxNotificationFrames > maxNotificationFramesFastDenied) {
2209 maxNotificationFrames = maxNotificationFramesFastDenied;
2210 }
2211 }
2212 }
2213 if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
2214 if (notificationFrameCount == 0) {
2215 ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu",
2216 maxNotificationFrames, frameCount);
2217 } else {
2218 ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu",
2219 notificationFrameCount, maxNotificationFrames, frameCount);
2220 }
2221 notificationFrameCount = maxNotificationFrames;
2222 }
2223 }
2224
Glenn Kasten74935e42013-12-19 08:56:45 -08002225 *pFrameCount = frameCount;
Eric Laurent21da6472017-11-09 16:29:26 -08002226 *pNotificationFrameCount = notificationFrameCount;
Eric Laurent81784c32012-11-19 14:55:58 -08002227
Glenn Kastenc3df8382014-03-13 15:05:25 -07002228 switch (mType) {
2229
2230 case DIRECT:
Phil Burkfdb3c072016-02-09 10:47:02 -08002231 if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()?
Eric Laurent81784c32012-11-19 14:55:58 -08002232 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08002233 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x "
2234 "for output %p with format %#x",
Eric Laurent81784c32012-11-19 14:55:58 -08002235 sampleRate, format, channelMask, mOutput, mFormat);
2236 lStatus = BAD_VALUE;
2237 goto Exit;
2238 }
2239 }
Glenn Kastenc3df8382014-03-13 15:05:25 -07002240 break;
2241
2242 case OFFLOAD:
Eric Laurentbfb1b832013-01-07 09:53:42 -08002243 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08002244 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \""
2245 "for output %p with format %#x",
Eric Laurentbfb1b832013-01-07 09:53:42 -08002246 sampleRate, format, channelMask, mOutput, mFormat);
2247 lStatus = BAD_VALUE;
2248 goto Exit;
2249 }
Glenn Kastenc3df8382014-03-13 15:05:25 -07002250 break;
2251
2252 default:
Glenn Kasten993fa062014-05-02 11:14:34 -07002253 if (!audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08002254 ALOGE("createTrack_l() Bad parameter: format %#x \""
2255 "for output %p with format %#x",
Eric Laurentbfb1b832013-01-07 09:53:42 -08002256 format, mOutput, mFormat);
2257 lStatus = BAD_VALUE;
2258 goto Exit;
2259 }
Andy Hungcd044842014-08-07 11:04:34 -07002260 if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
Eric Laurent81784c32012-11-19 14:55:58 -08002261 ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
2262 lStatus = BAD_VALUE;
2263 goto Exit;
2264 }
Glenn Kastenc3df8382014-03-13 15:05:25 -07002265 break;
2266
Eric Laurent81784c32012-11-19 14:55:58 -08002267 }
2268
2269 lStatus = initCheck();
2270 if (lStatus != NO_ERROR) {
Glenn Kasten15e57982013-09-24 11:52:37 -07002271 ALOGE("createTrack_l() audio driver not initialized");
Eric Laurent81784c32012-11-19 14:55:58 -08002272 goto Exit;
2273 }
2274
2275 { // scope for mLock
2276 Mutex::Autolock _l(mLock);
2277
2278 // all tracks in same audio session must share the same routing strategy otherwise
2279 // conflicts will happen when tracks are moved from one output to another by audio policy
2280 // manager
2281 uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
2282 for (size_t i = 0; i < mTracks.size(); ++i) {
2283 sp<Track> t = mTracks[i];
Eric Laurent83b88082014-06-20 18:31:16 -07002284 if (t != 0 && t->isExternalTrack()) {
Eric Laurent81784c32012-11-19 14:55:58 -08002285 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
2286 if (sessionId == t->sessionId() && strategy != actual) {
2287 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
2288 strategy, actual);
2289 lStatus = BAD_VALUE;
2290 goto Exit;
2291 }
2292 }
2293 }
2294
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002295 track = new Track(this, client, streamType, attr, sampleRate, format,
Andy Hung8fe68032017-06-05 16:17:51 -07002296 channelMask, frameCount,
2297 nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002298 sessionId, creatorPid, uid, *flags, TrackBase::TYPE_DEFAULT, portId);
Glenn Kasten03003332013-08-06 15:40:54 -07002299
Glenn Kasten03003332013-08-06 15:40:54 -07002300 lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
2301 if (lStatus != NO_ERROR) {
Glenn Kasten0cde0762014-01-16 15:06:36 -08002302 ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08002303 // track must be cleared from the caller as the caller has the AF lock
Eric Laurent81784c32012-11-19 14:55:58 -08002304 goto Exit;
2305 }
2306 mTracks.add(track);
2307
2308 sp<EffectChain> chain = getEffectChain_l(sessionId);
2309 if (chain != 0) {
2310 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
2311 track->setMainBuffer(chain->inBuffer());
2312 chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
2313 chain->incTrackCnt();
2314 }
2315
Eric Laurent05067782016-06-01 18:27:28 -07002316 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) {
Eric Laurent81784c32012-11-19 14:55:58 -08002317 pid_t callingPid = IPCThreadState::self()->getCallingPid();
2318 // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
2319 // so ask activity manager to do this on our behalf
Glenn Kastenaf9a7b52017-03-29 11:29:39 -07002320 sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
Eric Laurent81784c32012-11-19 14:55:58 -08002321 }
2322 }
2323
2324 lStatus = NO_ERROR;
2325
2326Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002327 *status = lStatus;
Eric Laurent81784c32012-11-19 14:55:58 -08002328 return track;
2329}
2330
Andy Hung1bc088a2018-02-09 15:57:31 -08002331template<typename T>
Andy Hung1bc088a2018-02-09 15:57:31 -08002332ssize_t AudioFlinger::PlaybackThread::Tracks<T>::remove(const sp<T> &track)
2333{
Andy Hungc0691382018-09-12 18:01:57 -07002334 const int trackId = track->id();
Andy Hung1bc088a2018-02-09 15:57:31 -08002335 const ssize_t index = mTracks.remove(track);
2336 if (index >= 0) {
Andy Hungc0691382018-09-12 18:01:57 -07002337 if (mSaveDeletedTrackIds) {
Andy Hung1bc088a2018-02-09 15:57:31 -08002338 // We can't directly access mAudioMixer since the caller may be outside of threadLoop.
Andy Hungc0691382018-09-12 18:01:57 -07002339 // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update,
Andy Hung1bc088a2018-02-09 15:57:31 -08002340 // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer.
Andy Hungc0691382018-09-12 18:01:57 -07002341 mDeletedTrackIds.emplace(trackId);
Andy Hung1bc088a2018-02-09 15:57:31 -08002342 }
Andy Hung1bc088a2018-02-09 15:57:31 -08002343 }
2344 return index;
2345}
2346
Eric Laurent81784c32012-11-19 14:55:58 -08002347uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const
2348{
2349 return latency;
2350}
2351
2352uint32_t AudioFlinger::PlaybackThread::latency() const
2353{
2354 Mutex::Autolock _l(mLock);
2355 return latency_l();
2356}
2357uint32_t AudioFlinger::PlaybackThread::latency_l() const
2358{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002359 uint32_t latency;
2360 if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) {
2361 return correctLatency_l(latency);
Eric Laurent81784c32012-11-19 14:55:58 -08002362 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002363 return 0;
Eric Laurent81784c32012-11-19 14:55:58 -08002364}
2365
2366void AudioFlinger::PlaybackThread::setMasterVolume(float value)
2367{
2368 Mutex::Autolock _l(mLock);
2369 // Don't apply master volume in SW if our HAL can do it for us.
2370 if (mOutput && mOutput->audioHwDev &&
2371 mOutput->audioHwDev->canSetMasterVolume()) {
2372 mMasterVolume = 1.0;
2373 } else {
2374 mMasterVolume = value;
2375 }
2376}
2377
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002378void AudioFlinger::PlaybackThread::setMasterBalance(float balance)
2379{
2380 mMasterBalance.store(balance);
2381}
2382
Eric Laurent81784c32012-11-19 14:55:58 -08002383void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
2384{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002385 if (isDuplicating()) {
2386 return;
2387 }
Eric Laurent81784c32012-11-19 14:55:58 -08002388 Mutex::Autolock _l(mLock);
2389 // Don't apply master mute in SW if our HAL can do it for us.
2390 if (mOutput && mOutput->audioHwDev &&
2391 mOutput->audioHwDev->canSetMasterMute()) {
2392 mMasterMute = false;
2393 } else {
2394 mMasterMute = muted;
2395 }
2396}
2397
2398void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
2399{
2400 Mutex::Autolock _l(mLock);
2401 mStreamTypes[stream].volume = value;
Eric Laurentede6c3b2013-09-19 14:37:46 -07002402 broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -08002403}
2404
2405void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
2406{
2407 Mutex::Autolock _l(mLock);
2408 mStreamTypes[stream].mute = muted;
Eric Laurentede6c3b2013-09-19 14:37:46 -07002409 broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -08002410}
2411
2412float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
2413{
2414 Mutex::Autolock _l(mLock);
2415 return mStreamTypes[stream].volume;
2416}
2417
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002418void AudioFlinger::PlaybackThread::setVolumeForOutput_l(float left, float right) const
2419{
2420 mOutput->stream->setVolume(left, right);
2421}
2422
Eric Laurent81784c32012-11-19 14:55:58 -08002423// addTrack_l() must be called with ThreadBase::mLock held
2424status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
2425{
2426 status_t status = ALREADY_EXISTS;
2427
Eric Laurent81784c32012-11-19 14:55:58 -08002428 if (mActiveTracks.indexOf(track) < 0) {
2429 // the track is newly added, make sure it fills up all its
2430 // buffers before playing. This is to ensure the client will
2431 // effectively get the latency it requested.
Eric Laurent83b88082014-06-20 18:31:16 -07002432 if (track->isExternalTrack()) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08002433 TrackBase::track_state state = track->mState;
2434 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002435 status = AudioSystem::startOutput(track->portId());
Eric Laurentbfb1b832013-01-07 09:53:42 -08002436 mLock.lock();
2437 // abort track was stopped/paused while we released the lock
2438 if (state != track->mState) {
2439 if (status == NO_ERROR) {
2440 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002441 AudioSystem::stopOutput(track->portId());
Eric Laurentbfb1b832013-01-07 09:53:42 -08002442 mLock.lock();
2443 }
2444 return INVALID_OPERATION;
2445 }
2446 // abort if start is rejected by audio policy manager
2447 if (status != NO_ERROR) {
2448 return PERMISSION_DENIED;
2449 }
2450#ifdef ADD_BATTERY_DATA
2451 // to track the speaker usage
2452 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
2453#endif
Eric Laurent09f1ed22019-04-24 17:45:17 -07002454 sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId());
Eric Laurentbfb1b832013-01-07 09:53:42 -08002455 }
2456
Eric Laurent51716182016-02-29 18:00:56 -08002457 // set retry count for buffer fill
2458 if (track->isOffloaded()) {
Eric Laurente93cc032016-05-05 10:15:10 -07002459 if (track->isStopping_1()) {
2460 track->mRetryCount = kMaxTrackStopRetriesOffload;
2461 } else {
2462 track->mRetryCount = kMaxTrackStartupRetriesOffload;
2463 }
2464 track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED;
Eric Laurent51716182016-02-29 18:00:56 -08002465 } else {
2466 track->mRetryCount = kMaxTrackStartupRetries;
Eric Laurente93cc032016-05-05 10:15:10 -07002467 track->mFillingUpStatus =
2468 track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING;
Eric Laurent51716182016-02-29 18:00:56 -08002469 }
2470
jiabin245cdd92018-12-07 17:55:15 -08002471 if ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
2472 && mHapticChannelMask != AUDIO_CHANNEL_NONE) {
jiabin57303cc2018-12-18 15:45:57 -08002473 // Unlock due to VibratorService will lock for this call and will
2474 // call Tracks.mute/unmute which also require thread's lock.
2475 mLock.unlock();
2476 const int intensity = AudioFlinger::onExternalVibrationStart(
2477 track->getExternalVibration());
2478 mLock.lock();
jiabinbf6b0ec2019-02-12 12:30:12 -08002479 track->setHapticIntensity(static_cast<AudioMixer::haptic_intensity_t>(intensity));
jiabin57303cc2018-12-18 15:45:57 -08002480 // Haptic playback should be enabled by vibrator service.
2481 if (track->getHapticPlaybackEnabled()) {
2482 // Disable haptic playback of all active track to ensure only
2483 // one track playing haptic if current track should play haptic.
2484 for (const auto &t : mActiveTracks) {
2485 t->setHapticPlaybackEnabled(false);
2486 }
jiabin245cdd92018-12-07 17:55:15 -08002487 }
jiabin245cdd92018-12-07 17:55:15 -08002488 }
2489
Eric Laurent81784c32012-11-19 14:55:58 -08002490 track->mResetDone = false;
2491 track->mPresentationCompleteFrames = 0;
2492 mActiveTracks.add(track);
Eric Laurentd0107bc2013-06-11 14:38:48 -07002493 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
2494 if (chain != 0) {
2495 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(),
2496 track->sessionId());
2497 chain->incActiveTrackCnt();
Eric Laurent81784c32012-11-19 14:55:58 -08002498 }
2499
2500 status = NO_ERROR;
2501 }
2502
Haynes Mathew George4c6a4332014-01-15 12:31:39 -08002503 onAddNewTrack_l();
Eric Laurent81784c32012-11-19 14:55:58 -08002504 return status;
2505}
2506
Eric Laurentbfb1b832013-01-07 09:53:42 -08002507bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
Eric Laurent81784c32012-11-19 14:55:58 -08002508{
Eric Laurentbfb1b832013-01-07 09:53:42 -08002509 track->terminate();
Eric Laurent81784c32012-11-19 14:55:58 -08002510 // active tracks are removed by threadLoop()
Eric Laurentbfb1b832013-01-07 09:53:42 -08002511 bool trackActive = (mActiveTracks.indexOf(track) >= 0);
2512 track->mState = TrackBase::STOPPED;
2513 if (!trackActive) {
Eric Laurent81784c32012-11-19 14:55:58 -08002514 removeTrack_l(track);
Eric Laurentab5cdba2014-06-09 17:22:27 -07002515 } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08002516 track->mState = TrackBase::STOPPING_1;
Eric Laurent81784c32012-11-19 14:55:58 -08002517 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002518
2519 return trackActive;
Eric Laurent81784c32012-11-19 14:55:58 -08002520}
2521
2522void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
2523{
2524 track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Andy Hung2148bf02016-11-28 19:01:02 -08002525
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002526 String8 result;
2527 track->appendDump(result, false /* active */);
2528 mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string());
Andy Hung2148bf02016-11-28 19:01:02 -08002529
Eric Laurent81784c32012-11-19 14:55:58 -08002530 mTracks.remove(track);
Eric Laurent81784c32012-11-19 14:55:58 -08002531 if (track->isFastTrack()) {
2532 int index = track->mFastIndex;
Glenn Kastendc2c50b2016-04-21 08:13:14 -07002533 ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks);
Eric Laurent81784c32012-11-19 14:55:58 -08002534 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
2535 mFastTrackAvailMask |= 1 << index;
2536 // redundant as track is about to be destroyed, for dumpsys only
2537 track->mFastIndex = -1;
2538 }
2539 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
2540 if (chain != 0) {
2541 chain->decTrackCnt();
2542 }
2543}
2544
2545String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
2546{
Eric Laurent81784c32012-11-19 14:55:58 -08002547 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002548 String8 out_s8;
2549 if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) {
2550 return out_s8;
Eric Laurent81784c32012-11-19 14:55:58 -08002551 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002552 return String8();
Eric Laurent81784c32012-11-19 14:55:58 -08002553}
2554
Mikhail Naganovac917ac2018-11-28 14:03:52 -08002555status_t AudioFlinger::DirectOutputThread::selectPresentation(int presentationId, int programId) {
2556 Mutex::Autolock _l(mLock);
2557 if (mOutput == nullptr || mOutput->stream == nullptr) {
2558 return NO_INIT;
2559 }
2560 return mOutput->stream->selectPresentation(presentationId, programId);
2561}
2562
Eric Laurent09f1ed22019-04-24 17:45:17 -07002563void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
2564 audio_port_handle_t portId) {
Eric Laurent73e26b62015-04-27 16:55:58 -07002565 sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
2566 ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event);
Eric Laurent81784c32012-11-19 14:55:58 -08002567
Eric Laurent73e26b62015-04-27 16:55:58 -07002568 desc->mIoHandle = mId;
Eric Laurent81784c32012-11-19 14:55:58 -08002569
2570 switch (event) {
Eric Laurent73e26b62015-04-27 16:55:58 -07002571 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -07002572 case AUDIO_OUTPUT_REGISTERED:
Eric Laurent73e26b62015-04-27 16:55:58 -07002573 case AUDIO_OUTPUT_CONFIG_CHANGED:
Eric Laurent296fb132015-05-01 11:38:42 -07002574 desc->mPatch = mPatch;
Eric Laurent73e26b62015-04-27 16:55:58 -07002575 desc->mChannelMask = mChannelMask;
2576 desc->mSamplingRate = mSampleRate;
2577 desc->mFormat = mFormat;
2578 desc->mFrameCount = mNormalFrameCount; // FIXME see
Eric Laurent81784c32012-11-19 14:55:58 -08002579 // AudioFlinger::frameCount(audio_io_handle_t)
Glenn Kasten4a8308b2016-04-18 14:10:01 -07002580 desc->mFrameCountHAL = mFrameCount;
Eric Laurent73e26b62015-04-27 16:55:58 -07002581 desc->mLatency = latency_l();
Eric Laurent81784c32012-11-19 14:55:58 -08002582 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -07002583 case AUDIO_CLIENT_STARTED:
2584 desc->mPatch = mPatch;
2585 desc->mPortId = portId;
2586 break;
Eric Laurent73e26b62015-04-27 16:55:58 -07002587 case AUDIO_OUTPUT_CLOSED:
Eric Laurent81784c32012-11-19 14:55:58 -08002588 default:
2589 break;
2590 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07002591 mAudioFlinger->ioConfigChanged(event, desc, pid);
Eric Laurent81784c32012-11-19 14:55:58 -08002592}
2593
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002594void AudioFlinger::PlaybackThread::onWriteReady()
Eric Laurentbfb1b832013-01-07 09:53:42 -08002595{
Eric Laurent3b4529e2013-09-05 18:09:19 -07002596 mCallbackThread->resetWriteBlocked();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002597}
2598
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002599void AudioFlinger::PlaybackThread::onDrainReady()
Eric Laurentbfb1b832013-01-07 09:53:42 -08002600{
Eric Laurent3b4529e2013-09-05 18:09:19 -07002601 mCallbackThread->resetDraining();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002602}
2603
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002604void AudioFlinger::PlaybackThread::onError()
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07002605{
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07002606 mCallbackThread->setAsyncError();
2607}
2608
Eric Laurent3b4529e2013-09-05 18:09:19 -07002609void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002610{
2611 Mutex::Autolock _l(mLock);
Eric Laurent3b4529e2013-09-05 18:09:19 -07002612 // reject out of sequence requests
2613 if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) {
2614 mWriteAckSequence &= ~1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002615 mWaitWorkCV.signal();
2616 }
2617}
2618
Eric Laurent3b4529e2013-09-05 18:09:19 -07002619void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002620{
2621 Mutex::Autolock _l(mLock);
Eric Laurent3b4529e2013-09-05 18:09:19 -07002622 // reject out of sequence requests
2623 if ((mDrainSequence & 1) && (sequence == mDrainSequence)) {
Andy Hungf3234512018-07-03 14:51:47 -07002624 // Register discontinuity when HW drain is completed because that can cause
2625 // the timestamp frame position to reset to 0 for direct and offload threads.
2626 // (Out of sequence requests are ignored, since the discontinuity would be handled
2627 // elsewhere, e.g. in flush).
2628 mTimestampVerifier.discontinuity();
Eric Laurent3b4529e2013-09-05 18:09:19 -07002629 mDrainSequence &= ~1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002630 mWaitWorkCV.signal();
2631 }
2632}
2633
Glenn Kastendeca2ae2014-02-07 10:25:56 -08002634void AudioFlinger::PlaybackThread::readOutputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -08002635{
Glenn Kastenadad3d72014-02-21 14:51:43 -08002636 // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL
Phil Burkca5e6142015-07-14 09:42:29 -07002637 mSampleRate = mOutput->getSampleRate();
2638 mChannelMask = mOutput->getChannelMask();
Glenn Kasten7fc97ba2013-07-16 17:18:58 -07002639 if (!audio_is_output_channel(mChannelMask)) {
Glenn Kastenadad3d72014-02-21 14:51:43 -08002640 LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask);
Glenn Kasten7fc97ba2013-07-16 17:18:58 -07002641 }
Andy Hung9a592762014-07-21 21:56:01 -07002642 if ((mType == MIXER || mType == DUPLICATING)
2643 && !isValidPcmSinkChannelMask(mChannelMask)) {
2644 LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output",
2645 mChannelMask);
Glenn Kasten7fc97ba2013-07-16 17:18:58 -07002646 }
Andy Hunge5412692014-05-16 11:25:07 -07002647 mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002648 mBalance.setChannelMask(mChannelMask);
Phil Burkca5e6142015-07-14 09:42:29 -07002649
2650 // Get actual HAL format.
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002651 status_t result = mOutput->stream->getFormat(&mHALFormat);
2652 LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result);
Phil Burkca5e6142015-07-14 09:42:29 -07002653 // Get format from the shim, which will be different than the HAL format
2654 // if playing compressed audio over HDMI passthrough.
2655 mFormat = mOutput->getFormat();
Glenn Kasten7fc97ba2013-07-16 17:18:58 -07002656 if (!audio_is_valid_format(mFormat)) {
Glenn Kastenadad3d72014-02-21 14:51:43 -08002657 LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat);
Glenn Kasten7fc97ba2013-07-16 17:18:58 -07002658 }
Andy Hung6146c082014-03-18 11:56:15 -07002659 if ((mType == MIXER || mType == DUPLICATING)
2660 && !isValidPcmSinkFormat(mFormat)) {
2661 LOG_FATAL("HAL format %#x not supported for mixed output",
2662 mFormat);
Glenn Kasten7fc97ba2013-07-16 17:18:58 -07002663 }
Phil Burk062e67a2015-02-11 13:40:50 -08002664 mFrameSize = mOutput->getFrameSize();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002665 result = mOutput->stream->getBufferSize(&mBufferSize);
2666 LOG_ALWAYS_FATAL_IF(result != OK,
2667 "Error when retrieving output stream buffer size: %d", result);
Glenn Kasten70949c42013-08-06 07:40:12 -07002668 mFrameCount = mBufferSize / mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08002669 if (mFrameCount & 15) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002670 ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames",
Eric Laurent81784c32012-11-19 14:55:58 -08002671 mFrameCount);
2672 }
2673
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002674 if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) {
2675 if (mOutput->stream->setCallback(this) == OK) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08002676 mUseAsyncWrite = true;
Eric Laurent4de95592013-09-26 15:28:21 -07002677 mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002678 }
2679 }
2680
Eric Laurentd1f69b02014-12-15 14:33:13 -08002681 mHwSupportsPause = false;
2682 if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002683 bool supportsPause = false, supportsResume = false;
2684 if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) {
2685 if (supportsPause && supportsResume) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08002686 mHwSupportsPause = true;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002687 } else if (supportsPause) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08002688 ALOGW("direct output implements pause but not resume");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002689 } else if (supportsResume) {
2690 ALOGW("direct output implements resume but not pause");
Eric Laurentd1f69b02014-12-15 14:33:13 -08002691 }
Eric Laurentd1f69b02014-12-15 14:33:13 -08002692 }
2693 }
Phil Burk6fc2a7c2015-04-30 16:08:10 -07002694 if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
2695 LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume");
2696 }
Eric Laurentd1f69b02014-12-15 14:33:13 -08002697
Andy Hungfbfc3952015-01-15 13:33:51 -08002698 if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) {
2699 // For best precision, we use float instead of the associated output
2700 // device format (typically PCM 16 bit).
2701
2702 mFormat = AUDIO_FORMAT_PCM_FLOAT;
2703 mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
2704 mBufferSize = mFrameSize * mFrameCount;
2705
2706 // TODO: We currently use the associated output device channel mask and sample rate.
2707 // (1) Perhaps use the ORed channel mask of all downstream MixerThreads
2708 // (if a valid mask) to avoid premature downmix.
2709 // (2) Perhaps use the maximum sample rate of all downstream MixerThreads
2710 // instead of the output device sample rate to avoid loss of high frequency information.
2711 // This may need to be updated as MixerThread/OutputTracks are added and not here.
2712 }
2713
Andy Hung09a50072014-02-27 14:30:47 -08002714 // Calculate size of normal sink buffer relative to the HAL output buffer size
Eric Laurent81784c32012-11-19 14:55:58 -08002715 double multiplier = 1.0;
2716 if (mType == MIXER && (kUseFastMixer == FastMixer_Static ||
2717 kUseFastMixer == FastMixer_Dynamic)) {
Andy Hung09a50072014-02-27 14:30:47 -08002718 size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
2719 size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
Haynes Mathew George227a14b2016-05-09 12:45:48 -07002720
Eric Laurent81784c32012-11-19 14:55:58 -08002721 // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
2722 minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
2723 maxNormalFrameCount = maxNormalFrameCount & ~15;
2724 if (maxNormalFrameCount < minNormalFrameCount) {
2725 maxNormalFrameCount = minNormalFrameCount;
2726 }
2727 multiplier = (double) minNormalFrameCount / (double) mFrameCount;
2728 if (multiplier <= 1.0) {
2729 multiplier = 1.0;
2730 } else if (multiplier <= 2.0) {
2731 if (2 * mFrameCount <= maxNormalFrameCount) {
2732 multiplier = 2.0;
2733 } else {
2734 multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
2735 }
2736 } else {
Haynes Mathew George227a14b2016-05-09 12:45:48 -07002737 multiplier = floor(multiplier);
Eric Laurent81784c32012-11-19 14:55:58 -08002738 }
2739 }
2740 mNormalFrameCount = multiplier * mFrameCount;
2741 // round up to nearest 16 frames to satisfy AudioMixer
Eric Laurentab5cdba2014-06-09 17:22:27 -07002742 if (mType == MIXER || mType == DUPLICATING) {
2743 mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
2744 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002745 ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", mFrameCount,
Eric Laurent81784c32012-11-19 14:55:58 -08002746 mNormalFrameCount);
2747
Andy Hung08fb1742015-05-31 23:22:10 -07002748 // Check if we want to throttle the processing to no more than 2x normal rate
2749 mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */);
Andy Hung40eb1a12015-06-18 13:42:02 -07002750 mThreadThrottleTimeMs = 0;
2751 mThreadThrottleEndMs = 0;
Andy Hung08fb1742015-05-31 23:22:10 -07002752 mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate);
2753
Andy Hung010a1a12014-03-13 13:57:33 -07002754 // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames.
2755 // Originally this was int16_t[] array, need to remove legacy implications.
2756 free(mSinkBuffer);
2757 mSinkBuffer = NULL;
Andy Hung5b10a202014-03-13 13:59:29 -07002758 // For sink buffer size, we use the frame size from the downstream sink to avoid problems
2759 // with non PCM formats for compressed music, e.g. AAC, and Offload threads.
2760 const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
Andy Hung010a1a12014-03-13 13:57:33 -07002761 (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002762
Andy Hung69aed5f2014-02-25 17:24:40 -08002763 // We resize the mMixerBuffer according to the requirements of the sink buffer which
2764 // drives the output.
2765 free(mMixerBuffer);
2766 mMixerBuffer = NULL;
2767 if (mMixerBufferEnabled) {
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002768 mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT.
Andy Hung69aed5f2014-02-25 17:24:40 -08002769 mMixerBufferSize = mNormalFrameCount * mChannelCount
2770 * audio_bytes_per_sample(mMixerBufferFormat);
2771 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
2772 }
Andy Hung98ef9782014-03-04 14:46:50 -08002773 free(mEffectBuffer);
2774 mEffectBuffer = NULL;
2775 if (mEffectBufferEnabled) {
rago94a1ee82017-07-21 15:11:02 -07002776 mEffectBufferFormat = EFFECT_BUFFER_FORMAT;
Andy Hung98ef9782014-03-04 14:46:50 -08002777 mEffectBufferSize = mNormalFrameCount * mChannelCount
2778 * audio_bytes_per_sample(mEffectBufferFormat);
2779 (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize);
2780 }
Andy Hung69aed5f2014-02-25 17:24:40 -08002781
jiabin245cdd92018-12-07 17:55:15 -08002782 mHapticChannelMask = mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL;
2783 mChannelMask &= ~mHapticChannelMask;
2784 mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask);
2785 mChannelCount -= mHapticChannelCount;
2786
Eric Laurent81784c32012-11-19 14:55:58 -08002787 // force reconfiguration of effect chains and engines to take new buffer size and audio
2788 // parameters into account
Glenn Kastendeca2ae2014-02-07 10:25:56 -08002789 // Note that mLock is not held when readOutputParameters_l() is called from the constructor
Eric Laurent81784c32012-11-19 14:55:58 -08002790 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
2791 // matter.
2792 // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
2793 Vector< sp<EffectChain> > effectChains = mEffectChains;
2794 for (size_t i = 0; i < effectChains.size(); i ++) {
Eric Laurent6c796322019-04-09 14:13:17 -07002795 mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(),
2796 this/* srcThread */, this/* dstThread */);
Eric Laurent81784c32012-11-19 14:55:58 -08002797 }
2798}
2799
Kevin Rocard069c2712018-03-29 19:09:14 -07002800void AudioFlinger::PlaybackThread::updateMetadata_l()
2801{
Kevin Rocard12381092018-04-11 09:19:59 -07002802 if (mOutput == nullptr || mOutput->stream == nullptr ) {
2803 return; // That should not happen
2804 }
2805 bool hasChanged = mActiveTracks.readAndClearHasChanged();
2806 for (const sp<Track> &track : mActiveTracks) {
2807 // Do not short-circuit as all hasChanged states must be reset
2808 // as all the metadata are going to be sent
2809 hasChanged |= track->readAndClearHasChanged();
2810 }
2811 if (!hasChanged) {
2812 return; // nothing to do
Kevin Rocard069c2712018-03-29 19:09:14 -07002813 }
2814 StreamOutHalInterface::SourceMetadata metadata;
Kevin Rocard12381092018-04-11 09:19:59 -07002815 auto backInserter = std::back_inserter(metadata.tracks);
Kevin Rocard069c2712018-03-29 19:09:14 -07002816 for (const sp<Track> &track : mActiveTracks) {
2817 // No track is invalid as this is called after prepareTrack_l in the same critical section
Kevin Rocard12381092018-04-11 09:19:59 -07002818 track->copyMetadataTo(backInserter);
Kevin Rocard069c2712018-03-29 19:09:14 -07002819 }
Kevin Rocard12381092018-04-11 09:19:59 -07002820 sendMetadataToBackend_l(metadata);
Kevin Rocard80ee2722018-04-11 15:53:48 +00002821}
Kevin Rocardc86a7f72018-04-03 09:00:09 -07002822
Kevin Rocard12381092018-04-11 09:19:59 -07002823void AudioFlinger::PlaybackThread::sendMetadataToBackend_l(
2824 const StreamOutHalInterface::SourceMetadata& metadata)
2825{
2826 mOutput->stream->updateSourceMetadata(metadata);
2827};
2828
Kévin PETIT377b2ec2014-02-03 12:35:36 +00002829status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
Eric Laurent81784c32012-11-19 14:55:58 -08002830{
2831 if (halFrames == NULL || dspFrames == NULL) {
2832 return BAD_VALUE;
2833 }
2834 Mutex::Autolock _l(mLock);
2835 if (initCheck() != NO_ERROR) {
2836 return INVALID_OPERATION;
2837 }
Andy Hung818e7a32016-02-16 18:08:07 -08002838 int64_t framesWritten = mBytesWritten / mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08002839 *halFrames = framesWritten;
2840
2841 if (isSuspended()) {
2842 // return an estimation of rendered frames when the output is suspended
2843 size_t latencyFrames = (latency_l() * mSampleRate) / 1000;
Andy Hung818e7a32016-02-16 18:08:07 -08002844 *dspFrames = (uint32_t)
2845 (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0);
Eric Laurent81784c32012-11-19 14:55:58 -08002846 return NO_ERROR;
2847 } else {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00002848 status_t status;
2849 uint32_t frames;
Phil Burk062e67a2015-02-11 13:40:50 -08002850 status = mOutput->getRenderPosition(&frames);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00002851 *dspFrames = (size_t)frames;
2852 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08002853 }
2854}
2855
Glenn Kastend848eb42016-03-08 13:42:11 -08002856uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId)
Eric Laurent81784c32012-11-19 14:55:58 -08002857{
2858 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
2859 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
2860 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2861 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
2862 }
2863 for (size_t i = 0; i < mTracks.size(); i++) {
2864 sp<Track> track = mTracks[i];
Glenn Kasten5736c352012-12-04 12:12:34 -08002865 if (sessionId == track->sessionId() && !track->isInvalid()) {
Eric Laurent81784c32012-11-19 14:55:58 -08002866 return AudioSystem::getStrategyForStream(track->streamType());
2867 }
2868 }
2869 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
2870}
2871
2872
Phil Burk062e67a2015-02-11 13:40:50 -08002873AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
Eric Laurent81784c32012-11-19 14:55:58 -08002874{
2875 Mutex::Autolock _l(mLock);
2876 return mOutput;
2877}
2878
Phil Burk062e67a2015-02-11 13:40:50 -08002879AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
Eric Laurent81784c32012-11-19 14:55:58 -08002880{
2881 Mutex::Autolock _l(mLock);
2882 AudioStreamOut *output = mOutput;
2883 mOutput = NULL;
2884 // FIXME FastMixer might also have a raw ptr to mOutputSink;
2885 // must push a NULL and wait for ack
2886 mOutputSink.clear();
2887 mPipeSink.clear();
2888 mNormalSink.clear();
2889 return output;
2890}
2891
2892// this method must always be called either with ThreadBase mLock held or inside the thread loop
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002893sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const
Eric Laurent81784c32012-11-19 14:55:58 -08002894{
2895 if (mOutput == NULL) {
2896 return NULL;
2897 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002898 return mOutput->stream;
Eric Laurent81784c32012-11-19 14:55:58 -08002899}
2900
2901uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
2902{
2903 return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
2904}
2905
2906status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
2907{
2908 if (!isValidSyncEvent(event)) {
2909 return BAD_VALUE;
2910 }
2911
2912 Mutex::Autolock _l(mLock);
2913
2914 for (size_t i = 0; i < mTracks.size(); ++i) {
2915 sp<Track> track = mTracks[i];
2916 if (event->triggerSession() == track->sessionId()) {
2917 (void) track->setSyncEvent(event);
2918 return NO_ERROR;
2919 }
2920 }
2921
2922 return NAME_NOT_FOUND;
2923}
2924
2925bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
2926{
2927 return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
2928}
2929
2930void AudioFlinger::PlaybackThread::threadLoop_removeTracks(
2931 const Vector< sp<Track> >& tracksToRemove)
2932{
Andy Hungfe726a62018-09-27 15:17:25 -07002933 // Miscellaneous track cleanup when removed from the active list,
2934 // called without Thread lock but synchronized with threadLoop processing.
Eric Laurentbfb1b832013-01-07 09:53:42 -08002935#ifdef ADD_BATTERY_DATA
Andy Hungfe726a62018-09-27 15:17:25 -07002936 for (const auto& track : tracksToRemove) {
2937 if (track->isExternalTrack()) {
2938 // to track the speaker usage
2939 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Eric Laurent81784c32012-11-19 14:55:58 -08002940 }
2941 }
Andy Hungfe726a62018-09-27 15:17:25 -07002942#else
2943 (void)tracksToRemove; // suppress unused warning
2944#endif
Eric Laurent81784c32012-11-19 14:55:58 -08002945}
2946
2947void AudioFlinger::PlaybackThread::checkSilentMode_l()
2948{
2949 if (!mMasterMute) {
2950 char value[PROPERTY_VALUE_MAX];
jiabin10d86fd2019-10-31 17:20:42 -07002951 if (isSingleDeviceType(outDeviceTypes(), AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) {
Jean-Michel Trivi32f37c22016-03-31 16:00:32 -07002952 ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX");
2953 return;
2954 }
Eric Laurent81784c32012-11-19 14:55:58 -08002955 if (property_get("ro.audio.silent", value, "0") > 0) {
2956 char *endptr;
2957 unsigned long ul = strtoul(value, &endptr, 0);
2958 if (*endptr == '\0' && ul != 0) {
2959 ALOGD("Silence is golden");
2960 // The setprop command will not allow a property to be changed after
2961 // the first time it is set, so we don't have to worry about un-muting.
2962 setMasterMute_l(true);
2963 }
2964 }
2965 }
2966}
2967
2968// shared by MIXER and DIRECT, overridden by DUPLICATING
Eric Laurentbfb1b832013-01-07 09:53:42 -08002969ssize_t AudioFlinger::PlaybackThread::threadLoop_write()
Eric Laurent81784c32012-11-19 14:55:58 -08002970{
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07002971 LOG_HIST_TS();
Eric Laurent81784c32012-11-19 14:55:58 -08002972 mInWrite = true;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002973 ssize_t bytesWritten;
Andy Hung010a1a12014-03-13 13:57:33 -07002974 const size_t offset = mCurrentWriteLength - mBytesRemaining;
Eric Laurent81784c32012-11-19 14:55:58 -08002975
2976 // If an NBAIO sink is present, use it to write the normal mixer's submix
2977 if (mNormalSink != 0) {
Glenn Kasten4c053ea2014-09-28 14:41:07 -07002978
Andy Hung010a1a12014-03-13 13:57:33 -07002979 const size_t count = mBytesRemaining / mFrameSize;
2980
Simon Wilson2d590962012-11-29 15:18:50 -08002981 ATRACE_BEGIN("write");
Eric Laurent81784c32012-11-19 14:55:58 -08002982 // update the setpoint when AudioFlinger::mScreenState changes
2983 uint32_t screenState = AudioFlinger::mScreenState;
2984 if (screenState != mScreenState) {
2985 mScreenState = screenState;
2986 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
2987 if (pipe != NULL) {
2988 pipe->setAvgFrames((mScreenState & 1) ?
2989 (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
2990 }
2991 }
Andy Hung010a1a12014-03-13 13:57:33 -07002992 ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
Simon Wilson2d590962012-11-29 15:18:50 -08002993 ATRACE_END();
Eric Laurent81784c32012-11-19 14:55:58 -08002994 if (framesWritten > 0) {
Andy Hung010a1a12014-03-13 13:57:33 -07002995 bytesWritten = framesWritten * mFrameSize;
Andy Hung8946a282018-04-19 20:04:56 -07002996#ifdef TEE_SINK
2997 mTee.write((char *)mSinkBuffer + offset, framesWritten);
2998#endif
Eric Laurent81784c32012-11-19 14:55:58 -08002999 } else {
3000 bytesWritten = framesWritten;
3001 }
3002 // otherwise use the HAL / AudioStreamOut directly
3003 } else {
Eric Laurentbfb1b832013-01-07 09:53:42 -08003004 // Direct output and offload threads
Andy Hung010a1a12014-03-13 13:57:33 -07003005
Eric Laurentbfb1b832013-01-07 09:53:42 -08003006 if (mUseAsyncWrite) {
Eric Laurent3b4529e2013-09-05 18:09:19 -07003007 ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
3008 mWriteAckSequence += 2;
3009 mWriteAckSequence |= 1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003010 ALOG_ASSERT(mCallbackThread != 0);
Eric Laurent3b4529e2013-09-05 18:09:19 -07003011 mCallbackThread->setWriteBlocked(mWriteAckSequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -08003012 }
Mikhail Naganov76e89c32019-08-15 20:18:47 -07003013 ATRACE_BEGIN("write");
Glenn Kasten767094d2013-08-23 13:51:43 -07003014 // FIXME We should have an implementation of timestamps for direct output threads.
3015 // They are used e.g for multichannel PCM playback over HDMI.
Phil Burk062e67a2015-02-11 13:40:50 -08003016 bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
Mikhail Naganov76e89c32019-08-15 20:18:47 -07003017 ATRACE_END();
Eric Laurent51716182016-02-29 18:00:56 -08003018
Eric Laurentbfb1b832013-01-07 09:53:42 -08003019 if (mUseAsyncWrite &&
3020 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
3021 // do not wait for async callback in case of error of full write
Eric Laurent3b4529e2013-09-05 18:09:19 -07003022 mWriteAckSequence &= ~1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003023 ALOG_ASSERT(mCallbackThread != 0);
Eric Laurent3b4529e2013-09-05 18:09:19 -07003024 mCallbackThread->setWriteBlocked(mWriteAckSequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -08003025 }
Eric Laurent81784c32012-11-19 14:55:58 -08003026 }
3027
Eric Laurent81784c32012-11-19 14:55:58 -08003028 mNumWrites++;
3029 mInWrite = false;
Eric Laurentfd477972013-10-25 18:10:40 -07003030 mStandby = false;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003031 return bytesWritten;
3032}
3033
3034void AudioFlinger::PlaybackThread::threadLoop_drain()
3035{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07003036 bool supportsDrain = false;
3037 if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08003038 ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full");
3039 if (mUseAsyncWrite) {
Eric Laurent3b4529e2013-09-05 18:09:19 -07003040 ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request");
3041 mDrainSequence |= 1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003042 ALOG_ASSERT(mCallbackThread != 0);
Eric Laurent3b4529e2013-09-05 18:09:19 -07003043 mCallbackThread->setDraining(mDrainSequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -08003044 }
Mikhail Naganovcbc8f612016-10-11 18:05:13 -07003045 status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07003046 ALOGE_IF(result != OK, "Error when draining stream: %d", result);
Eric Laurentbfb1b832013-01-07 09:53:42 -08003047 }
3048}
3049
3050void AudioFlinger::PlaybackThread::threadLoop_exit()
3051{
Eric Laurent275e8e92014-11-30 15:14:47 -08003052 {
3053 Mutex::Autolock _l(mLock);
3054 for (size_t i = 0; i < mTracks.size(); i++) {
3055 sp<Track> track = mTracks[i];
3056 track->invalidate();
3057 }
Andy Hungdae27702016-10-31 14:01:16 -07003058 // Clear ActiveTracks to update BatteryNotifier in case active tracks remain.
3059 // After we exit there are no more track changes sent to BatteryNotifier
3060 // because that requires an active threadLoop.
3061 // TODO: should we decActiveTrackCnt() of the cleared track effect chain?
3062 mActiveTracks.clear();
Eric Laurent275e8e92014-11-30 15:14:47 -08003063 }
Eric Laurent81784c32012-11-19 14:55:58 -08003064}
3065
3066/*
3067The derived values that are cached:
Andy Hung25c2dac2014-02-27 14:56:00 -08003068 - mSinkBufferSize from frame count * frame size
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003069 - mActiveSleepTimeUs from activeSleepTimeUs()
3070 - mIdleSleepTimeUs from idleSleepTimeUs()
Eric Laurent42537be2016-01-08 17:16:42 -08003071 - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least
3072 kDefaultStandbyTimeInNsecs when connected to an A2DP device.
Eric Laurent81784c32012-11-19 14:55:58 -08003073 - maxPeriod from frame count and sample rate (MIXER only)
3074
3075The parameters that affect these derived values are:
3076 - frame count
3077 - frame size
3078 - sample rate
3079 - device type: A2DP or not
3080 - device latency
3081 - format: PCM or not
3082 - active sleep time
3083 - idle sleep time
3084*/
3085
3086void AudioFlinger::PlaybackThread::cacheParameters_l()
3087{
Andy Hung25c2dac2014-02-27 14:56:00 -08003088 mSinkBufferSize = mNormalFrameCount * mFrameSize;
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003089 mActiveSleepTimeUs = activeSleepTimeUs();
3090 mIdleSleepTimeUs = idleSleepTimeUs();
Eric Laurent42537be2016-01-08 17:16:42 -08003091
3092 // make sure standby delay is not too short when connected to an A2DP sink to avoid
3093 // truncating audio when going to standby.
3094 mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs;
jiabin10d86fd2019-10-31 17:20:42 -07003095 if (!Intersection(outDeviceTypes(), getAudioDeviceOutAllA2dpSet()).empty()) {
Eric Laurent42537be2016-01-08 17:16:42 -08003096 if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) {
3097 mStandbyDelayNs = kDefaultStandbyTimeInNsecs;
3098 }
3099 }
Eric Laurent81784c32012-11-19 14:55:58 -08003100}
3101
Eric Laurent13084622016-05-17 10:51:49 -07003102bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType)
Eric Laurent81784c32012-11-19 14:55:58 -08003103{
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003104 ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu",
Eric Laurent81784c32012-11-19 14:55:58 -08003105 this, streamType, mTracks.size());
Eric Laurent13084622016-05-17 10:51:49 -07003106 bool trackMatch = false;
Eric Laurent81784c32012-11-19 14:55:58 -08003107 size_t size = mTracks.size();
3108 for (size_t i = 0; i < size; i++) {
3109 sp<Track> t = mTracks[i];
Eric Laurentd60560a2015-04-10 11:31:20 -07003110 if (t->streamType() == streamType && t->isExternalTrack()) {
Glenn Kasten5736c352012-12-04 12:12:34 -08003111 t->invalidate();
Eric Laurent13084622016-05-17 10:51:49 -07003112 trackMatch = true;
Eric Laurent81784c32012-11-19 14:55:58 -08003113 }
3114 }
Eric Laurent13084622016-05-17 10:51:49 -07003115 return trackMatch;
Eric Laurent81784c32012-11-19 14:55:58 -08003116}
3117
Haynes Mathew George05317d22016-05-03 16:34:26 -07003118void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
3119{
3120 Mutex::Autolock _l(mLock);
3121 invalidateTracks_l(streamType);
3122}
3123
Eric Laurent81784c32012-11-19 14:55:58 -08003124status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
3125{
Glenn Kastend848eb42016-03-08 13:42:11 -08003126 audio_session_t session = chain->sessionId();
Mikhail Naganov022b9952017-01-04 16:36:51 -08003127 sp<EffectBufferHalInterface> halInBuffer, halOutBuffer;
Kevin Rocard7588ff42018-01-08 11:11:30 -08003128 status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08003129 mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer,
3130 mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize,
3131 &halInBuffer);
3132 if (result != OK) return result;
3133 halOutBuffer = halInBuffer;
rago94a1ee82017-07-21 15:11:02 -07003134 effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData());
Eric Laurent81784c32012-11-19 14:55:58 -08003135 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
Eric Laurenta20c4e92019-11-12 15:55:51 -08003136 if (!audio_is_global_session(session)) {
Eric Laurent81784c32012-11-19 14:55:58 -08003137 // Only one effect chain can be present in direct output thread and it uses
Andy Hung2098f272014-02-27 14:00:06 -08003138 // the sink buffer as input
Eric Laurent81784c32012-11-19 14:55:58 -08003139 if (mType != DIRECT) {
jiabin245cdd92018-12-07 17:55:15 -08003140 size_t numSamples = mNormalFrameCount * (mChannelCount + mHapticChannelCount);
Kevin Rocard7588ff42018-01-08 11:11:30 -08003141 status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07003142 numSamples * sizeof(effect_buffer_t),
Mikhail Naganov022b9952017-01-04 16:36:51 -08003143 &halInBuffer);
3144 if (result != OK) return result;
rago94a1ee82017-07-21 15:11:02 -07003145#ifdef FLOAT_EFFECT_CHAIN
3146 buffer = halInBuffer->audioBuffer()->f32;
3147#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08003148 buffer = halInBuffer->audioBuffer()->s16;
rago94a1ee82017-07-21 15:11:02 -07003149#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08003150 ALOGV("addEffectChain_l() creating new input buffer %p session %d",
3151 buffer, session);
Eric Laurent81784c32012-11-19 14:55:58 -08003152 }
3153
3154 // Attach all tracks with same session ID to this chain.
3155 for (size_t i = 0; i < mTracks.size(); ++i) {
3156 sp<Track> track = mTracks[i];
3157 if (session == track->sessionId()) {
3158 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(),
3159 buffer);
3160 track->setMainBuffer(buffer);
3161 chain->incTrackCnt();
3162 }
3163 }
3164
3165 // indicate all active tracks in the chain
Andy Hungdae27702016-10-31 14:01:16 -07003166 for (const sp<Track> &track : mActiveTracks) {
Eric Laurent81784c32012-11-19 14:55:58 -08003167 if (session == track->sessionId()) {
3168 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
3169 chain->incActiveTrackCnt();
3170 }
3171 }
3172 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003173 chain->setThread(this);
Mikhail Naganov022b9952017-01-04 16:36:51 -08003174 chain->setInBuffer(halInBuffer);
3175 chain->setOutBuffer(halOutBuffer);
Eric Laurenta20c4e92019-11-12 15:55:51 -08003176 // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect
3177 // chains list in order to be processed last as it contains output device effects.
3178 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post
3179 // processing effects specific to an output stream before effects applied to all streams
3180 // routed to a given device.
Eric Laurent81784c32012-11-19 14:55:58 -08003181 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
3182 // session AUDIO_SESSION_OUTPUT_STAGE to be processed
Glenn Kastend848eb42016-03-08 13:42:11 -08003183 // after track specific effects and before output stage.
Eric Laurent81784c32012-11-19 14:55:58 -08003184 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
Glenn Kastend848eb42016-03-08 13:42:11 -08003185 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX.
Eric Laurent81784c32012-11-19 14:55:58 -08003186 // Effect chain for other sessions are inserted at beginning of effect
3187 // chains list to be processed before output mix effects. Relative order between other
Glenn Kastend848eb42016-03-08 13:42:11 -08003188 // sessions is not important.
3189 static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 &&
Eric Laurenta20c4e92019-11-12 15:55:51 -08003190 AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX &&
3191 AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE,
Glenn Kastend848eb42016-03-08 13:42:11 -08003192 "audio_session_t constants misdefined");
Eric Laurent81784c32012-11-19 14:55:58 -08003193 size_t size = mEffectChains.size();
3194 size_t i = 0;
3195 for (i = 0; i < size; i++) {
3196 if (mEffectChains[i]->sessionId() < session) {
3197 break;
3198 }
3199 }
3200 mEffectChains.insertAt(chain, i);
3201 checkSuspendOnAddEffectChain_l(chain);
3202
3203 return NO_ERROR;
3204}
3205
3206size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
3207{
Glenn Kastend848eb42016-03-08 13:42:11 -08003208 audio_session_t session = chain->sessionId();
Eric Laurent81784c32012-11-19 14:55:58 -08003209
3210 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
3211
3212 for (size_t i = 0; i < mEffectChains.size(); i++) {
3213 if (chain == mEffectChains[i]) {
3214 mEffectChains.removeAt(i);
3215 // detach all active tracks from the chain
Andy Hungdae27702016-10-31 14:01:16 -07003216 for (const sp<Track> &track : mActiveTracks) {
Eric Laurent81784c32012-11-19 14:55:58 -08003217 if (session == track->sessionId()) {
3218 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
3219 chain.get(), session);
3220 chain->decActiveTrackCnt();
3221 }
3222 }
3223
3224 // detach all tracks with same session ID from this chain
3225 for (size_t i = 0; i < mTracks.size(); ++i) {
3226 sp<Track> track = mTracks[i];
3227 if (session == track->sessionId()) {
rago94a1ee82017-07-21 15:11:02 -07003228 track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer));
Eric Laurent81784c32012-11-19 14:55:58 -08003229 chain->decTrackCnt();
3230 }
3231 }
3232 break;
3233 }
3234 }
3235 return mEffectChains.size();
3236}
3237
3238status_t AudioFlinger::PlaybackThread::attachAuxEffect(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003239 const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId)
Eric Laurent81784c32012-11-19 14:55:58 -08003240{
3241 Mutex::Autolock _l(mLock);
3242 return attachAuxEffect_l(track, EffectId);
3243}
3244
3245status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003246 const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId)
Eric Laurent81784c32012-11-19 14:55:58 -08003247{
3248 status_t status = NO_ERROR;
3249
3250 if (EffectId == 0) {
3251 track->setAuxBuffer(0, NULL);
3252 } else {
3253 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
3254 sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
3255 if (effect != 0) {
3256 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3257 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
3258 } else {
3259 status = INVALID_OPERATION;
3260 }
3261 } else {
3262 status = BAD_VALUE;
3263 }
3264 }
3265 return status;
3266}
3267
3268void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
3269{
3270 for (size_t i = 0; i < mTracks.size(); ++i) {
3271 sp<Track> track = mTracks[i];
3272 if (track->auxEffectId() == effectId) {
3273 attachAuxEffect_l(track, 0);
3274 }
3275 }
3276}
3277
3278bool AudioFlinger::PlaybackThread::threadLoop()
3279{
Glenn Kasten388d5712017-04-07 14:38:41 -07003280 tlNBLogWriter = mNBLogWriter.get();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08003281
Eric Laurent81784c32012-11-19 14:55:58 -08003282 Vector< sp<Track> > tracksToRemove;
3283
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003284 mStandbyTimeNs = systemTime();
Andy Hung446f4df2019-02-21 12:26:41 -08003285 int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0.
3286 int64_t lastFramesWritten = -1; // track changes in timestamp server frames written
Eric Laurent81784c32012-11-19 14:55:58 -08003287
3288 // MIXER
3289 nsecs_t lastWarning = 0;
3290
3291 // DUPLICATING
3292 // FIXME could this be made local to while loop?
3293 writeFrames = 0;
3294
3295 cacheParameters_l();
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003296 mSleepTimeUs = mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08003297
3298 if (mType == MIXER) {
3299 sleepTimeShift = 0;
3300 }
3301
3302 CpuStats cpuStats;
3303 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
3304
3305 acquireWakeLock();
3306
Glenn Kasteneef598c2017-04-03 14:41:13 -07003307 // mNBLogWriter logging APIs can only be called by a single thread, typically the
3308 // thread associated with this PlaybackThread.
3309 // If you want to share the mNBLogWriter with other threads (for example, binder threads)
3310 // then all such threads must agree to hold a common mutex before logging.
Glenn Kasten9e58b552013-01-18 15:09:48 -08003311 // So if you need to log when mutex is unlocked, set logString to a non-NULL string,
3312 // and then that string will be logged at the next convenient opportunity.
Glenn Kasteneef598c2017-04-03 14:41:13 -07003313 // See reference to logString below.
Glenn Kasten9e58b552013-01-18 15:09:48 -08003314 const char *logString = NULL;
3315
rago1bb90822017-05-02 18:31:48 -07003316 // Estimated time for next buffer to be written to hal. This is used only on
3317 // suspended mode (for now) to help schedule the wait time until next iteration.
3318 nsecs_t timeLoopNextNs = 0;
3319
Eric Laurent664539d2013-09-23 18:24:31 -07003320 checkSilentMode_l();
Glenn Kasteneef598c2017-04-03 14:41:13 -07003321
Andy Hungf3234512018-07-03 14:51:47 -07003322 // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
3323 // TODO: add confirmation checks:
3324 // 1) DIRECT threads and linear PCM format really resets to 0?
3325 // 2) Is frame count really valid if not linear pcm?
3326 // 3) Are all 64 bits of position returned, not just lowest 32 bits?
3327 if (mType == OFFLOAD || mType == DIRECT) {
3328 mTimestampVerifier.setDiscontinuityMode(mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
3329 }
Andy Hung2dbffc22018-08-08 18:50:41 -07003330 audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Andy Hungf3234512018-07-03 14:51:47 -07003331
Andy Hung446f4df2019-02-21 12:26:41 -08003332 // loopCount is used for statistics and diagnostics.
3333 for (int64_t loopCount = 0; !exitPending(); ++loopCount)
Eric Laurent81784c32012-11-19 14:55:58 -08003334 {
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08003335 // Log merge requests are performed during AudioFlinger binder transactions, but
3336 // that does not cover audio playback. It's requested here for that reason.
3337 mAudioFlinger->requestLogMerge();
3338
Eric Laurent81784c32012-11-19 14:55:58 -08003339 cpuStats.sample(myName);
3340
3341 Vector< sp<EffectChain> > effectChains;
Andy Hung6e6a2e62019-04-30 16:38:41 -07003342 audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE;
Andy Hungc1646382019-04-30 16:12:10 -07003343 std::vector<sp<Track>> activeTracks;
Eric Laurent81784c32012-11-19 14:55:58 -08003344
Andy Hung2dbffc22018-08-08 18:50:41 -07003345 // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency.
3346 //
jiabin10d86fd2019-10-31 17:20:42 -07003347 // Note: we access outDeviceTypes() outside of mLock.
3348 if (isMsdDevice() && outDeviceTypes().count(AUDIO_DEVICE_OUT_BUS) != 0) {
Andy Hung2dbffc22018-08-08 18:50:41 -07003349 // Here, we try for the AF lock, but do not block on it as the latency
3350 // is more informational.
3351 if (mAudioFlinger->mLock.tryLock() == NO_ERROR) {
3352 std::vector<PatchPanel::SoftwarePatch> swPatches;
3353 double latencyMs;
3354 status_t status = INVALID_OPERATION;
3355 audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3356 if (mAudioFlinger->mPatchPanel.getDownstreamSoftwarePatches(id(), &swPatches) == OK
3357 && swPatches.size() > 0) {
3358 status = swPatches[0].getLatencyMs_l(&latencyMs);
3359 downstreamPatchHandle = swPatches[0].getPatchHandle();
3360 }
3361 if (downstreamPatchHandle != lastDownstreamPatchHandle) {
Dean Wheatley30d28422018-11-06 10:27:40 +11003362 mDownstreamLatencyStatMs.reset();
Andy Hung2dbffc22018-08-08 18:50:41 -07003363 lastDownstreamPatchHandle = downstreamPatchHandle;
3364 }
3365 if (status == OK) {
3366 // verify downstream latency (we assume a max reasonable
Mikhail Naganov6aa0a312018-11-09 11:00:01 -08003367 // latency of 5 seconds).
3368 const double minLatency = 0., maxLatency = 5000.;
3369 if (latencyMs >= minLatency && latencyMs <= maxLatency) {
Dean Wheatley56a583e2020-05-08 15:12:17 +10003370 ALOGVV("new downstream latency %lf ms", latencyMs);
Andy Hung2dbffc22018-08-08 18:50:41 -07003371 } else {
3372 ALOGD("out of range downstream latency %lf ms", latencyMs);
Mikhail Naganov6aa0a312018-11-09 11:00:01 -08003373 if (latencyMs < minLatency) latencyMs = minLatency;
3374 else if (latencyMs > maxLatency) latencyMs = maxLatency;
Andy Hung2dbffc22018-08-08 18:50:41 -07003375 }
Dean Wheatley30d28422018-11-06 10:27:40 +11003376 mDownstreamLatencyStatMs.add(latencyMs);
Andy Hung2dbffc22018-08-08 18:50:41 -07003377 }
3378 mAudioFlinger->mLock.unlock();
3379 }
3380 } else {
3381 if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
3382 // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats.
Dean Wheatley30d28422018-11-06 10:27:40 +11003383 mDownstreamLatencyStatMs.reset();
Andy Hung2dbffc22018-08-08 18:50:41 -07003384 lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3385 }
3386 }
3387
Eric Laurent81784c32012-11-19 14:55:58 -08003388 { // scope for mLock
3389
3390 Mutex::Autolock _l(mLock);
3391
Eric Laurent021cf962014-05-13 10:18:14 -07003392 processConfigEvents_l();
Eric Laurent10351942014-05-08 18:49:52 -07003393
Glenn Kasteneef598c2017-04-03 14:41:13 -07003394 // See comment at declaration of logString for why this is done under mLock
Glenn Kasten9e58b552013-01-18 15:09:48 -08003395 if (logString != NULL) {
3396 mNBLogWriter->logTimestamp();
3397 mNBLogWriter->log(logString);
3398 logString = NULL;
3399 }
3400
Andy Hung2e2c0bb2018-06-11 19:13:11 -07003401 // Collect timestamp statistics for the Playback Thread types that support it.
3402 if (mType == MIXER
3403 || mType == DUPLICATING
Andy Hungf3234512018-07-03 14:51:47 -07003404 || mType == DIRECT
3405 || mType == OFFLOAD) { // no indentation
Glenn Kasten4c053ea2014-09-28 14:41:07 -07003406 // Gather the framesReleased counters for all active tracks,
Andy Hunge10393e2015-06-12 13:59:33 -07003407 // and associate with the sink frames written out. We need
3408 // this to convert the sink timestamp to the track timestamp.
Andy Hung69488c42016-05-16 18:43:33 -07003409 bool kernelLocationUpdate = false;
Andy Hung1c86ebe2018-05-29 20:29:08 -07003410 ExtendedTimestamp timestamp; // use private copy to fetch
Andy Hung2e2c0bb2018-06-11 19:13:11 -07003411 if (mStandby) {
3412 mTimestampVerifier.discontinuity();
3413 } else if (threadloop_getHalTimestamp_l(&timestamp) == OK) {
3414 mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
3415 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3416 mSampleRate);
Andy Hungc8fddf32018-08-08 18:32:37 -07003417
3418 if (isTimestampCorrectionEnabled()) {
Dean Wheatley56a583e2020-05-08 15:12:17 +10003419 ALOGVV("TS_BEFORE: %d %lld %lld", id(),
Andy Hungc8fddf32018-08-08 18:32:37 -07003420 (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3421 (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
3422 auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
3423 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3424 = correctedTimestamp.mFrames;
3425 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
3426 = correctedTimestamp.mTimeNs;
Dean Wheatley56a583e2020-05-08 15:12:17 +10003427 ALOGVV("TS_AFTER: %d %lld %lld", id(),
Andy Hungc8fddf32018-08-08 18:32:37 -07003428 (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
3429 (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
Andy Hung2dbffc22018-08-08 18:50:41 -07003430
3431 // Note: Downstream latency only added if timestamp correction enabled.
Dean Wheatley30d28422018-11-06 10:27:40 +11003432 if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info.
Andy Hung2dbffc22018-08-08 18:50:41 -07003433 const int64_t newPosition =
3434 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
Dean Wheatley30d28422018-11-06 10:27:40 +11003435 - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
Andy Hung2dbffc22018-08-08 18:50:41 -07003436 // prevent retrograde
3437 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max(
3438 newPosition,
3439 (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3440 - mSuspendedFrames));
3441 }
Andy Hungc8fddf32018-08-08 18:32:37 -07003442 }
3443
Andy Hung818e7a32016-02-16 18:08:07 -08003444 // We always fetch the timestamp here because often the downstream
Andy Hung69488c42016-05-16 18:43:33 -07003445 // sink will block while writing.
Andy Hung6d7b1192016-05-07 22:59:48 -07003446
3447 // We keep track of the last valid kernel position in case we are in underrun
3448 // and the normal mixer period is the same as the fast mixer period, or there
3449 // is some error from the HAL.
3450 if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
3451 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
3452 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
3453 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
3454 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
3455
3456 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
3457 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER];
3458 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
3459 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER];
Andy Hung69488c42016-05-16 18:43:33 -07003460 }
3461
3462 if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
3463 kernelLocationUpdate = true;
Andy Hung6d7b1192016-05-07 22:59:48 -07003464 } else {
Eric Laurent122f7e72016-06-29 11:53:29 -07003465 ALOGVV("getTimestamp error - no valid kernel position");
Andy Hung6d7b1192016-05-07 22:59:48 -07003466 }
3467
Andy Hung818e7a32016-02-16 18:08:07 -08003468 // copy over kernel info
3469 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
Andy Hung238fa3d2016-07-28 10:53:22 -07003470 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
3471 + mSuspendedFrames; // add frames discarded when suspended
Andy Hung818e7a32016-02-16 18:08:07 -08003472 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
3473 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
Andy Hung2e2c0bb2018-06-11 19:13:11 -07003474 } else {
3475 mTimestampVerifier.error();
Andy Hungc54b1ff2016-02-23 14:07:07 -08003476 }
Andy Hung2e2c0bb2018-06-11 19:13:11 -07003477
Andy Hungc54b1ff2016-02-23 14:07:07 -08003478 // mFramesWritten for non-offloaded tracks are contiguous
3479 // even after standby() is called. This is useful for the track frame
3480 // to sink frame mapping.
Andy Hung69488c42016-05-16 18:43:33 -07003481 bool serverLocationUpdate = false;
3482 if (mFramesWritten != lastFramesWritten) {
3483 serverLocationUpdate = true;
3484 lastFramesWritten = mFramesWritten;
3485 }
3486 // Only update timestamps if there is a meaningful change.
3487 // Either the kernel timestamp must be valid or we have written something.
3488 if (kernelLocationUpdate || serverLocationUpdate) {
3489 if (serverLocationUpdate) {
3490 // use the time before we called the HAL write - it is a bit more accurate
3491 // to when the server last read data than the current time here.
3492 //
Andy Hung446f4df2019-02-21 12:26:41 -08003493 // If we haven't written anything, mLastIoBeginNs will be -1
Andy Hung69488c42016-05-16 18:43:33 -07003494 // and we use systemTime().
3495 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten;
Andy Hung446f4df2019-02-21 12:26:41 -08003496 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1
3497 ? systemTime() : mLastIoBeginNs;
Andy Hung69488c42016-05-16 18:43:33 -07003498 }
Andy Hungdae27702016-10-31 14:01:16 -07003499
3500 for (const sp<Track> &t : mActiveTracks) {
3501 if (!t->isFastTrack()) {
Andy Hung69488c42016-05-16 18:43:33 -07003502 t->updateTrackFrameInfo(
3503 t->mAudioTrackServerProxy->framesReleased(),
3504 mFramesWritten,
Andy Hungcef2daa2018-06-01 15:31:49 -07003505 mSampleRate,
Andy Hung69488c42016-05-16 18:43:33 -07003506 mTimestamp);
3507 }
Andy Hunge10393e2015-06-12 13:59:33 -07003508 }
Glenn Kastenbd096fd2013-08-23 13:53:56 -07003509 }
Andy Hunge6c37112019-02-26 17:38:10 -08003510
3511 if (audio_has_proportional_frames(mFormat)) {
3512 const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate);
3513 if (latencyMs != 0.) { // note 0. means timestamp is empty.
3514 mLatencyMs.add(latencyMs);
3515 }
3516 }
3517
Andy Hung2e2c0bb2018-06-11 19:13:11 -07003518 } // if (mType ... ) { // no indentation
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08003519#if 0
3520 // logFormat example
Nicolas Rouletc20cb502017-02-01 12:35:24 -08003521 if (z % 100 == 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08003522 timespec ts;
3523 clock_gettime(CLOCK_MONOTONIC, &ts);
Nicolas Roulet4da78202017-02-03 12:53:39 -08003524 LOGT("This is an integer %d, this is a float %f, this is my "
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08003525 "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts);
Nicolas Roulet4da78202017-02-03 12:53:39 -08003526 LOGT("A deceptive null-terminated string %\0");
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08003527 }
3528 ++z;
3529#endif
Eric Laurent81784c32012-11-19 14:55:58 -08003530 saveOutputTracks();
Eric Laurentbfb1b832013-01-07 09:53:42 -08003531 if (mSignalPending) {
3532 // A signal was raised while we were unlocked
3533 mSignalPending = false;
3534 } else if (waitingAsyncCallback_l()) {
3535 if (exitPending()) {
3536 break;
3537 }
Marco Nelissen078538c2015-05-12 09:17:57 -07003538 bool released = false;
Eric Laurent64667972016-03-30 18:19:46 -07003539 if (!keepWakeLock()) {
Marco Nelissen078538c2015-05-12 09:17:57 -07003540 releaseWakeLock_l();
3541 released = true;
3542 }
Andy Hung10cbff12017-02-21 17:30:14 -08003543
3544 const int64_t waitNs = computeWaitTimeNs_l();
3545 ALOGV("wait async completion (wait time: %lld)", (long long)waitNs);
3546 status_t status = mWaitWorkCV.waitRelative(mLock, waitNs);
3547 if (status == TIMED_OUT) {
3548 mSignalPending = true; // if timeout recheck everything
3549 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08003550 ALOGV("async completion/wake");
Marco Nelissen078538c2015-05-12 09:17:57 -07003551 if (released) {
3552 acquireWakeLock_l();
3553 }
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003554 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
3555 mSleepTimeUs = 0;
Eric Laurentede6c3b2013-09-19 14:37:46 -07003556
3557 continue;
3558 }
Eric Tan39ec8d62018-07-24 09:49:29 -07003559 if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) ||
Eric Laurentbfb1b832013-01-07 09:53:42 -08003560 isSuspended()) {
3561 // put audio hardware into standby after short delay
3562 if (shouldStandby_l()) {
Eric Laurent81784c32012-11-19 14:55:58 -08003563
3564 threadLoop_standby();
3565
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07003566 // This is where we go into standby
3567 if (!mStandby) {
3568 LOG_AUDIO_STATE();
3569 }
Eric Laurent81784c32012-11-19 14:55:58 -08003570 mStandby = true;
Andy Hungd0979812019-02-21 15:51:44 -08003571 sendStatistics(false /* force */);
Eric Laurent81784c32012-11-19 14:55:58 -08003572 }
3573
Eric Tan39ec8d62018-07-24 09:49:29 -07003574 if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) {
Eric Laurent81784c32012-11-19 14:55:58 -08003575 // we're about to wait, flush the binder command buffer
3576 IPCThreadState::self()->flushCommands();
3577
3578 clearOutputTracks();
3579
3580 if (exitPending()) {
3581 break;
3582 }
3583
3584 releaseWakeLock_l();
3585 // wait until we have something to do...
3586 ALOGV("%s going to sleep", myName.string());
3587 mWaitWorkCV.wait(mLock);
3588 ALOGV("%s waking up", myName.string());
3589 acquireWakeLock_l();
3590
3591 mMixerStatus = MIXER_IDLE;
3592 mMixerStatusIgnoringFastTracks = MIXER_IDLE;
3593 mBytesWritten = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003594 mBytesRemaining = 0;
Eric Laurent81784c32012-11-19 14:55:58 -08003595 checkSilentMode_l();
3596
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003597 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
3598 mSleepTimeUs = mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08003599 if (mType == MIXER) {
3600 sleepTimeShift = 0;
3601 }
3602
3603 continue;
3604 }
3605 }
Eric Laurent81784c32012-11-19 14:55:58 -08003606 // mMixerStatusIgnoringFastTracks is also updated internally
3607 mMixerStatus = prepareTracks_l(&tracksToRemove);
3608
Andy Hungdae27702016-10-31 14:01:16 -07003609 mActiveTracks.updatePowerState(this);
Marco Nelissen462fd2f2013-01-14 14:12:05 -08003610
Kevin Rocard069c2712018-03-29 19:09:14 -07003611 updateMetadata_l();
3612
Eric Laurent81784c32012-11-19 14:55:58 -08003613 // prevent any changes in effect chain list and in each effect chain
3614 // during mixing and effect process as the audio buffers could be deleted
3615 // or modified if an effect is created or deleted
3616 lockEffectChains_l(effectChains);
Andy Hung6e6a2e62019-04-30 16:38:41 -07003617
3618 // Determine which session to pick up haptic data.
3619 // This must be done under the same lock as prepareTracks_l().
3620 // TODO: Write haptic data directly to sink buffer when mixing.
3621 if (mHapticChannelCount > 0 && effectChains.size() > 0) {
3622 for (const auto& track : mActiveTracks) {
3623 if (track->getHapticPlaybackEnabled()) {
3624 activeHapticSessionId = track->sessionId();
3625 break;
3626 }
3627 }
3628 }
3629
Andy Hungc1646382019-04-30 16:12:10 -07003630 // Acquire a local copy of active tracks with lock (release w/o lock).
3631 //
3632 // Control methods on the track acquire the ThreadBase lock (e.g. start()
3633 // stop(), pause(), etc.), but the threadLoop is entitled to call audio
3634 // data / buffer methods on tracks from activeTracks without the ThreadBase lock.
3635 activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end());
Marco Nelissen462fd2f2013-01-14 14:12:05 -08003636 } // mLock scope ends
Eric Laurent81784c32012-11-19 14:55:58 -08003637
Eric Laurentbfb1b832013-01-07 09:53:42 -08003638 if (mBytesRemaining == 0) {
3639 mCurrentWriteLength = 0;
3640 if (mMixerStatus == MIXER_TRACKS_READY) {
3641 // threadLoop_mix() sets mCurrentWriteLength
3642 threadLoop_mix();
3643 } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
3644 && (mMixerStatus != MIXER_DRAIN_ALL)) {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003645 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data
Eric Laurentbfb1b832013-01-07 09:53:42 -08003646 // must be written to HAL
3647 threadLoop_sleepTime();
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003648 if (mSleepTimeUs == 0) {
Andy Hung25c2dac2014-02-27 14:56:00 -08003649 mCurrentWriteLength = mSinkBufferSize;
Andy Hungc1646382019-04-30 16:12:10 -07003650
3651 // Tally underrun frames as we are inserting 0s here.
3652 for (const auto& track : activeTracks) {
3653 if (track->mFillingUpStatus == Track::FS_ACTIVE) {
3654 track->mAudioTrackServerProxy->tallyUnderrunFrames(mNormalFrameCount);
3655 }
3656 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08003657 }
3658 }
Andy Hung98ef9782014-03-04 14:46:50 -08003659 // Either threadLoop_mix() or threadLoop_sleepTime() should have set
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003660 // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0.
Andy Hung98ef9782014-03-04 14:46:50 -08003661 // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
3662 // or mSinkBuffer (if there are no effects).
3663 //
3664 // This is done pre-effects computation; if effects change to
3665 // support higher precision, this needs to move.
3666 //
3667 // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003668 // TODO use mSleepTimeUs == 0 as an additional condition.
Andy Hung98ef9782014-03-04 14:46:50 -08003669 if (mMixerBufferValid) {
3670 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
3671 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
3672
Andy Hung2ddee192015-12-18 17:34:44 -08003673 // mono blend occurs for mixer threads only (not direct or offloaded)
3674 // and is handled here if we're going directly to the sink.
3675 if (requireMonoBlend() && !mEffectBufferValid) {
Glenn Kasten03c48d52016-01-27 17:25:17 -08003676 mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount,
3677 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -08003678 }
3679
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01003680 if (!hasFastMixer()) {
3681 // Balance must take effect after mono conversion.
3682 // We do it here if there is no FastMixer.
3683 // mBalance detects zero balance within the class for speed (not needed here).
3684 mBalance.setBalance(mMasterBalance.load());
3685 mBalance.process((float *)mMixerBuffer, mNormalFrameCount);
3686 }
3687
Andy Hung98ef9782014-03-04 14:46:50 -08003688 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
jiabin245cdd92018-12-07 17:55:15 -08003689 mNormalFrameCount * (mChannelCount + mHapticChannelCount));
3690
3691 // If we're going directly to the sink and there are haptic channels,
3692 // we should adjust channels as the sample data is partially interleaved
3693 // in this case.
3694 if (!mEffectBufferValid && mHapticChannelCount > 0) {
3695 adjust_channels_non_destructive(buffer, mChannelCount, buffer,
3696 mChannelCount + mHapticChannelCount,
3697 audio_bytes_per_sample(format),
3698 audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount);
3699 }
Andy Hung98ef9782014-03-04 14:46:50 -08003700 }
3701
Eric Laurentbfb1b832013-01-07 09:53:42 -08003702 mBytesRemaining = mCurrentWriteLength;
3703 if (isSuspended()) {
Andy Hung238fa3d2016-07-28 10:53:22 -07003704 // Simulate write to HAL when suspended (e.g. BT SCO phone call).
3705 mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer.
3706 const size_t framesRemaining = mBytesRemaining / mFrameSize;
3707 mBytesWritten += mBytesRemaining;
3708 mFramesWritten += framesRemaining;
3709 mSuspendedFrames += framesRemaining; // to adjust kernel HAL position
Eric Laurentbfb1b832013-01-07 09:53:42 -08003710 mBytesRemaining = 0;
3711 }
Eric Laurent81784c32012-11-19 14:55:58 -08003712
Eric Laurentbfb1b832013-01-07 09:53:42 -08003713 // only process effects if we're going to write
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003714 if (mSleepTimeUs == 0 && mType != OFFLOAD) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08003715 for (size_t i = 0; i < effectChains.size(); i ++) {
3716 effectChains[i]->process_l();
jiabin47affe52019-04-04 18:02:07 -07003717 // TODO: Write haptic data directly to sink buffer when mixing.
Andy Hung6e6a2e62019-04-30 16:38:41 -07003718 if (activeHapticSessionId != AUDIO_SESSION_NONE
3719 && activeHapticSessionId == effectChains[i]->sessionId()) {
jiabin47affe52019-04-04 18:02:07 -07003720 // Haptic data is active in this case, copy it directly from
3721 // in buffer to out buffer.
3722 const size_t audioBufferSize = mNormalFrameCount
3723 * audio_bytes_per_frame(mChannelCount, EFFECT_BUFFER_FORMAT);
3724 memcpy_by_audio_format(
3725 (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize,
3726 EFFECT_BUFFER_FORMAT,
3727 (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize,
3728 EFFECT_BUFFER_FORMAT, mNormalFrameCount * mHapticChannelCount);
3729 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08003730 }
Eric Laurent81784c32012-11-19 14:55:58 -08003731 }
3732 }
Eric Laurent59fe0102013-09-27 18:48:26 -07003733 // Process effect chains for offloaded thread even if no audio
3734 // was read from audio track: process only updates effect state
3735 // and thus does have to be synchronized with audio writes but may have
3736 // to be called while waiting for async write callback
3737 if (mType == OFFLOAD) {
3738 for (size_t i = 0; i < effectChains.size(); i ++) {
3739 effectChains[i]->process_l();
3740 }
3741 }
Eric Laurent81784c32012-11-19 14:55:58 -08003742
Andy Hung98ef9782014-03-04 14:46:50 -08003743 // Only if the Effects buffer is enabled and there is data in the
3744 // Effects buffer (buffer valid), we need to
3745 // copy into the sink buffer.
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003746 // TODO use mSleepTimeUs == 0 as an additional condition.
Andy Hung98ef9782014-03-04 14:46:50 -08003747 if (mEffectBufferValid) {
3748 //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
Andy Hung2ddee192015-12-18 17:34:44 -08003749
3750 if (requireMonoBlend()) {
Glenn Kasten03c48d52016-01-27 17:25:17 -08003751 mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount,
3752 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -08003753 }
3754
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01003755 if (!hasFastMixer()) {
3756 // Balance must take effect after mono conversion.
3757 // We do it here if there is no FastMixer.
3758 // mBalance detects zero balance within the class for speed (not needed here).
3759 mBalance.setBalance(mMasterBalance.load());
3760 mBalance.process((float *)mEffectBuffer, mNormalFrameCount);
3761 }
3762
Andy Hung98ef9782014-03-04 14:46:50 -08003763 memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat,
jiabin245cdd92018-12-07 17:55:15 -08003764 mNormalFrameCount * (mChannelCount + mHapticChannelCount));
3765 // The sample data is partially interleaved when haptic channels exist,
3766 // we need to adjust channels here.
3767 if (mHapticChannelCount > 0) {
3768 adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer,
3769 mChannelCount + mHapticChannelCount,
3770 audio_bytes_per_sample(mFormat),
3771 audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount);
3772 }
Andy Hung98ef9782014-03-04 14:46:50 -08003773 }
3774
Eric Laurent81784c32012-11-19 14:55:58 -08003775 // enable changes in effect chain
3776 unlockEffectChains(effectChains);
3777
Eric Laurentbfb1b832013-01-07 09:53:42 -08003778 if (!waitingAsyncCallback()) {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07003779 // mSleepTimeUs == 0 means we must write to audio hardware
3780 if (mSleepTimeUs == 0) {
Andy Hung08fb1742015-05-31 23:22:10 -07003781 ssize_t ret = 0;
Andy Hung446f4df2019-02-21 12:26:41 -08003782 // writePeriodNs is updated >= 0 when ret > 0.
3783 int64_t writePeriodNs = -1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003784 if (mBytesRemaining) {
Andy Hung69488c42016-05-16 18:43:33 -07003785 // FIXME rewrite to reduce number of system calls
Andy Hung446f4df2019-02-21 12:26:41 -08003786 const int64_t lastIoBeginNs = systemTime();
Andy Hung08fb1742015-05-31 23:22:10 -07003787 ret = threadLoop_write();
Andy Hung446f4df2019-02-21 12:26:41 -08003788 const int64_t lastIoEndNs = systemTime();
Eric Laurentbfb1b832013-01-07 09:53:42 -08003789 if (ret < 0) {
3790 mBytesRemaining = 0;
Andy Hung446f4df2019-02-21 12:26:41 -08003791 } else if (ret > 0) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08003792 mBytesWritten += ret;
3793 mBytesRemaining -= ret;
Andy Hung446f4df2019-02-21 12:26:41 -08003794 const int64_t frames = ret / mFrameSize;
3795 mFramesWritten += frames;
3796
3797 writePeriodNs = lastIoEndNs - mLastIoEndNs;
3798 // process information relating to write time.
3799 if (audio_has_proportional_frames(mFormat)) {
3800 // we are in a continuous mixing cycle
3801 if (mMixerStatus == MIXER_TRACKS_READY &&
3802 loopCount == lastLoopCountWritten + 1) {
3803
3804 const double jitterMs =
3805 TimestampVerifier<int64_t, int64_t>::computeJitterMs(
3806 {frames, writePeriodNs},
3807 {0, 0} /* lastTimestamp */, mSampleRate);
3808 const double processMs =
3809 (lastIoBeginNs - mLastIoEndNs) * 1e-6;
3810
3811 Mutex::Autolock _l(mLock);
3812 mIoJitterMs.add(jitterMs);
3813 mProcessTimeMs.add(processMs);
3814 }
3815
3816 // write blocked detection
3817 const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs;
3818 if (mType == MIXER && deltaWriteNs > maxPeriod) {
3819 mNumDelayedWrites++;
3820 if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) {
3821 ATRACE_NAME("underrun");
3822 ALOGW("write blocked for %lld msecs, "
3823 "%d delayed writes, thread %d",
3824 (long long)deltaWriteNs / NANOS_PER_MILLISECOND,
3825 mNumDelayedWrites, mId);
3826 lastWarning = lastIoEndNs;
3827 }
3828 }
3829 }
3830 // update timing info.
3831 mLastIoBeginNs = lastIoBeginNs;
3832 mLastIoEndNs = lastIoEndNs;
3833 lastLoopCountWritten = loopCount;
Eric Laurentbfb1b832013-01-07 09:53:42 -08003834 }
3835 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
3836 (mMixerStatus == MIXER_DRAIN_ALL)) {
3837 threadLoop_drain();
Eric Laurent81784c32012-11-19 14:55:58 -08003838 }
Andy Hung08fb1742015-05-31 23:22:10 -07003839 if (mType == MIXER && !mStandby) {
Andy Hung08fb1742015-05-31 23:22:10 -07003840
3841 if (mThreadThrottle
3842 && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks)
Andy Hung446f4df2019-02-21 12:26:41 -08003843 && writePeriodNs > 0) { // we have write period info
Andy Hung08fb1742015-05-31 23:22:10 -07003844 // Limit MixerThread data processing to no more than twice the
3845 // expected processing rate.
3846 //
3847 // This helps prevent underruns with NuPlayer and other applications
3848 // which may set up buffers that are close to the minimum size, or use
3849 // deep buffers, and rely on a double-buffering sleep strategy to fill.
3850 //
3851 // The throttle smooths out sudden large data drains from the device,
3852 // e.g. when it comes out of standby, which often causes problems with
3853 // (1) mixer threads without a fast mixer (which has its own warm-up)
3854 // (2) minimum buffer sized tracks (even if the track is full,
3855 // the app won't fill fast enough to handle the sudden draw).
Haynes Mathew Georgef92b2172016-05-09 11:34:15 -07003856 //
3857 // Total time spent in last processing cycle equals time spent in
3858 // 1. threadLoop_write, as well as time spent in
3859 // 2. threadLoop_mix (significant for heavy mixing, especially
3860 // on low tier processors)
Andy Hung08fb1742015-05-31 23:22:10 -07003861
Andy Hung446f4df2019-02-21 12:26:41 -08003862 // it's OK if deltaMs is an overestimate.
3863
3864 const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND;
Ivan Lozanoe02bc542017-10-26 09:51:54 -07003865
Ivan Lozanoea04d392017-11-07 14:37:07 -08003866 const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs;
Andy Hung08fb1742015-05-31 23:22:10 -07003867 if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) {
3868 usleep(throttleMs * 1000);
Andy Hung40eb1a12015-06-18 13:42:02 -07003869 // notify of throttle start on verbose log
3870 ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs,
3871 "mixer(%p) throttle begin:"
3872 " ret(%zd) deltaMs(%d) requires sleep %d ms",
Andy Hung08fb1742015-05-31 23:22:10 -07003873 this, ret, deltaMs, throttleMs);
Andy Hung40eb1a12015-06-18 13:42:02 -07003874 mThreadThrottleTimeMs += throttleMs;
Andy Hung0a31ddd2016-07-06 19:10:29 -07003875 // Throttle must be attributed to the previous mixer loop's write time
3876 // to allow back-to-back throttling.
Andy Hung446f4df2019-02-21 12:26:41 -08003877 // This also ensures proper timing statistics.
3878 mLastIoEndNs = systemTime(); // we fetch the write end time again.
Andy Hung40eb1a12015-06-18 13:42:02 -07003879 } else {
3880 uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
3881 if (diff > 0) {
3882 // notify of throttle end on debug log
Andy Hung3ea004d2016-05-05 16:48:37 -07003883 // but prevent spamming for bluetooth
jiabin10d86fd2019-10-31 17:20:42 -07003884 ALOGD_IF(!isSingleDeviceType(
3885 outDeviceTypes(), audio_is_a2dp_out_device) &&
3886 !isSingleDeviceType(
3887 outDeviceTypes(), audio_is_hearing_aid_out_device),
Andy Hung3ea004d2016-05-05 16:48:37 -07003888 "mixer(%p) throttle end: throttle time(%u)", this, diff);
Andy Hung40eb1a12015-06-18 13:42:02 -07003889 mThreadThrottleEndMs = mThreadThrottleTimeMs;
3890 }
Andy Hung08fb1742015-05-31 23:22:10 -07003891 }
3892 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08003893 }
Eric Laurent81784c32012-11-19 14:55:58 -08003894
Eric Laurentbfb1b832013-01-07 09:53:42 -08003895 } else {
Glenn Kastene7754022014-10-31 12:11:26 -07003896 ATRACE_BEGIN("sleep");
Eric Laurente93cc032016-05-05 10:15:10 -07003897 Mutex::Autolock _l(mLock);
rago1bb90822017-05-02 18:31:48 -07003898 // suspended requires accurate metering of sleep time.
3899 if (isSuspended()) {
3900 // advance by expected sleepTime
3901 timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs);
3902 const nsecs_t nowNs = systemTime();
3903
3904 // compute expected next time vs current time.
3905 // (negative deltas are treated as delays).
3906 nsecs_t deltaNs = timeLoopNextNs - nowNs;
3907 if (deltaNs < -kMaxNextBufferDelayNs) {
3908 // Delays longer than the max allowed trigger a reset.
3909 ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs);
3910 deltaNs = microseconds((nsecs_t)mSleepTimeUs);
3911 timeLoopNextNs = nowNs + deltaNs;
3912 } else if (deltaNs < 0) {
3913 // Delays within the max delay allowed: zero the delta/sleepTime
3914 // to help the system catch up in the next iteration(s)
3915 ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs);
3916 deltaNs = 0;
3917 }
3918 // update sleep time (which is >= 0)
3919 mSleepTimeUs = deltaNs / 1000;
3920 }
Eric Laurente93cc032016-05-05 10:15:10 -07003921 if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) {
3922 mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs));
Eric Laurent51716182016-02-29 18:00:56 -08003923 }
Glenn Kastene7754022014-10-31 12:11:26 -07003924 ATRACE_END();
Eric Laurentbfb1b832013-01-07 09:53:42 -08003925 }
Eric Laurent81784c32012-11-19 14:55:58 -08003926 }
3927
3928 // Finally let go of removed track(s), without the lock held
3929 // since we can't guarantee the destructors won't acquire that
3930 // same lock. This will also mutate and push a new fast mixer state.
3931 threadLoop_removeTracks(tracksToRemove);
3932 tracksToRemove.clear();
3933
3934 // FIXME I don't understand the need for this here;
3935 // it was in the original code but maybe the
3936 // assignment in saveOutputTracks() makes this unnecessary?
3937 clearOutputTracks();
3938
3939 // Effect chains will be actually deleted here if they were removed from
3940 // mEffectChains list during mixing or effects processing
3941 effectChains.clear();
3942
3943 // FIXME Note that the above .clear() is no longer necessary since effectChains
3944 // is now local to this block, but will keep it for now (at least until merge done).
3945 }
3946
Eric Laurentbfb1b832013-01-07 09:53:42 -08003947 threadLoop_exit();
3948
Eric Laurentcf817a22014-08-04 20:36:31 -07003949 if (!mStandby) {
3950 threadLoop_standby();
3951 mStandby = true;
Eric Laurent81784c32012-11-19 14:55:58 -08003952 }
3953
3954 releaseWakeLock();
3955
3956 ALOGV("Thread %p type %d exiting", this, mType);
3957 return false;
3958}
3959
Eric Laurentbfb1b832013-01-07 09:53:42 -08003960// removeTracks_l() must be called with ThreadBase::mLock held
3961void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove)
3962{
Andy Hungfe726a62018-09-27 15:17:25 -07003963 for (const auto& track : tracksToRemove) {
3964 mActiveTracks.remove(track);
3965 ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId());
3966 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
3967 if (chain != 0) {
3968 ALOGV("%s(%d): stopping track on chain %p for session Id: %d",
3969 __func__, track->id(), chain.get(), track->sessionId());
3970 chain->decActiveTrackCnt();
3971 }
3972 // If an external client track, inform APM we're no longer active, and remove if needed.
3973 // We do this under lock so that the state is consistent if the Track is destroyed.
3974 if (track->isExternalTrack()) {
3975 AudioSystem::stopOutput(track->portId());
Eric Laurentbfb1b832013-01-07 09:53:42 -08003976 if (track->isTerminated()) {
Andy Hungfe726a62018-09-27 15:17:25 -07003977 AudioSystem::releaseOutput(track->portId());
Eric Laurentbfb1b832013-01-07 09:53:42 -08003978 }
3979 }
Andy Hungfe726a62018-09-27 15:17:25 -07003980 if (track->isTerminated()) {
3981 // remove from our tracks vector
3982 removeTrack_l(track);
3983 }
jiabin57303cc2018-12-18 15:45:57 -08003984 if ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
3985 && mHapticChannelCount > 0) {
3986 mLock.unlock();
3987 // Unlock due to VibratorService will lock for this call and will
3988 // call Tracks.mute/unmute which also require thread's lock.
3989 AudioFlinger::onExternalVibrationStop(track->getExternalVibration());
3990 mLock.lock();
jiabin245cdd92018-12-07 17:55:15 -08003991 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08003992 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08003993}
Eric Laurent81784c32012-11-19 14:55:58 -08003994
Eric Laurentaccc1472013-09-20 09:36:34 -07003995status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp)
3996{
3997 if (mNormalSink != 0) {
Andy Hung818e7a32016-02-16 18:08:07 -08003998 ExtendedTimestamp ets;
3999 status_t status = mNormalSink->getTimestamp(ets);
4000 if (status == NO_ERROR) {
4001 status = ets.getBestTimestamp(&timestamp);
4002 }
4003 return status;
Eric Laurentaccc1472013-09-20 09:36:34 -07004004 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07004005 if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) {
Eric Laurentaccc1472013-09-20 09:36:34 -07004006 uint64_t position64;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07004007 if (mOutput->getPresentationPosition(&position64, &timestamp.mTime) == OK) {
Eric Laurentaccc1472013-09-20 09:36:34 -07004008 timestamp.mPosition = (uint32_t)position64;
Dean Wheatley30d28422018-11-06 10:27:40 +11004009 if (mDownstreamLatencyStatMs.getN() > 0) {
4010 const uint32_t positionOffset =
4011 (uint32_t)(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
4012 if (positionOffset > timestamp.mPosition) {
4013 timestamp.mPosition = 0;
4014 } else {
4015 timestamp.mPosition -= positionOffset;
4016 }
4017 }
Eric Laurentaccc1472013-09-20 09:36:34 -07004018 return NO_ERROR;
4019 }
4020 }
4021 return INVALID_OPERATION;
4022}
Eric Laurent1c333e22014-05-20 10:48:17 -07004023
Eric Laurenteab90452019-06-24 15:17:46 -07004024// For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is
4025// still applied by the mixer.
4026// All tracks attached to a mixer with flag VOIP_RX are tied to the same
4027// stream type STREAM_VOICE_CALL so this will only change the HAL volume once even
4028// if more than one track are active
4029status_t AudioFlinger::PlaybackThread::handleVoipVolume_l(float *volume)
4030{
4031 status_t result = NO_ERROR;
4032 if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) {
4033 if (*volume != mLeftVolFloat) {
4034 result = mOutput->stream->setVolume(*volume, *volume);
4035 ALOGE_IF(result != OK,
4036 "Error when setting output stream volume: %d", result);
4037 if (result == NO_ERROR) {
4038 mLeftVolFloat = *volume;
4039 }
4040 }
4041 // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we
4042 // remove stream volume contribution from software volume.
4043 if (mLeftVolFloat == *volume) {
4044 *volume = 1.0f;
4045 }
4046 }
4047 return result;
4048}
4049
Eric Laurent054d9d32015-04-24 08:48:48 -07004050status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch,
4051 audio_patch_handle_t *handle)
4052{
Andy Hungf60abce2016-08-26 11:37:54 -07004053 status_t status;
4054 if (property_get_bool("af.patch_park", false /* default_value */)) {
4055 // Park FastMixer to avoid potential DOS issues with writing to the HAL
4056 // or if HAL does not properly lock against access.
4057 AutoPark<FastMixer> park(mFastMixer);
4058 status = PlaybackThread::createAudioPatch_l(patch, handle);
4059 } else {
4060 status = PlaybackThread::createAudioPatch_l(patch, handle);
4061 }
Eric Laurent054d9d32015-04-24 08:48:48 -07004062 return status;
4063}
4064
Eric Laurent1c333e22014-05-20 10:48:17 -07004065status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch,
4066 audio_patch_handle_t *handle)
4067{
4068 status_t status = NO_ERROR;
Eric Laurent054d9d32015-04-24 08:48:48 -07004069
4070 // store new device and send to effects
4071 audio_devices_t type = AUDIO_DEVICE_NONE;
jiabin10d86fd2019-10-31 17:20:42 -07004072 AudioDeviceTypeAddrVector deviceTypeAddrs;
Eric Laurent054d9d32015-04-24 08:48:48 -07004073 for (unsigned int i = 0; i < patch->num_sinks; i++) {
jiabin10d86fd2019-10-31 17:20:42 -07004074 LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1
4075 && !mOutput->audioHwDev->supportsAudioPatches(),
4076 "Enumerated device type(%#x) must not be used "
4077 "as it does not support audio patches",
4078 patch->sinks[i].ext.device.type);
Eric Laurent054d9d32015-04-24 08:48:48 -07004079 type |= patch->sinks[i].ext.device.type;
jiabin10d86fd2019-10-31 17:20:42 -07004080 deviceTypeAddrs.push_back(AudioDeviceTypeAddr(patch->sinks[i].ext.device.type,
4081 patch->sinks[i].ext.device.address));
Eric Laurent054d9d32015-04-24 08:48:48 -07004082 }
4083
François Gaffie0c280aa2018-07-25 10:02:15 +02004084 audio_port_handle_t sinkPortId = patch->sinks[0].id;
Eric Laurent054d9d32015-04-24 08:48:48 -07004085#ifdef ADD_BATTERY_DATA
4086 // when changing the audio output device, call addBatteryData to notify
4087 // the change
jiabin10d86fd2019-10-31 17:20:42 -07004088 if (outDeviceTypes() != deviceTypes) {
Eric Laurent054d9d32015-04-24 08:48:48 -07004089 uint32_t params = 0;
4090 // check whether speaker is on
jiabin10d86fd2019-10-31 17:20:42 -07004091 if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) {
Eric Laurent054d9d32015-04-24 08:48:48 -07004092 params |= IMediaPlayerService::kBatteryDataSpeakerOn;
Eric Laurent1c333e22014-05-20 10:48:17 -07004093 }
4094
Eric Laurent054d9d32015-04-24 08:48:48 -07004095 // check if any other device (except speaker) is on
jiabin10d86fd2019-10-31 17:20:42 -07004096 if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07004097 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
4098 }
4099
4100 if (params != 0) {
4101 addBatteryData(params);
4102 }
4103 }
4104#endif
4105
4106 for (size_t i = 0; i < mEffectChains.size(); i++) {
jiabinb8269fd2019-11-11 12:16:27 -08004107 mEffectChains[i]->setDevices_l(deviceTypeAddrs);
Eric Laurent054d9d32015-04-24 08:48:48 -07004108 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07004109
jiabin10d86fd2019-10-31 17:20:42 -07004110 // mPatch.num_sinks is not set when the thread is created so that
4111 // the first patch creation triggers an ioConfigChanged callback
4112 bool configChanged = (mPatch.num_sinks == 0) ||
4113 (mPatch.sinks[0].id != sinkPortId);
Eric Laurent296fb132015-05-01 11:38:42 -07004114 mPatch = *patch;
jiabin10d86fd2019-10-31 17:20:42 -07004115 mOutDeviceTypeAddrs = deviceTypeAddrs;
Eric Laurent054d9d32015-04-24 08:48:48 -07004116
Mikhail Naganov9ee05402016-10-13 15:58:17 -07004117 if (mOutput->audioHwDev->supportsAudioPatches()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07004118 sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
4119 status = hwDevice->createAudioPatch(patch->num_sources,
4120 patch->sources,
4121 patch->num_sinks,
4122 patch->sinks,
4123 handle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004124 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -07004125 char *address;
4126 if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
4127 //FIXME: we only support address on first sink with HAL version < 3.0
4128 address = audio_device_address_to_parameter(
4129 patch->sinks[0].ext.device.type,
4130 patch->sinks[0].ext.device.address);
4131 } else {
4132 address = (char *)calloc(1, 1);
4133 }
4134 AudioParameter param = AudioParameter(String8(address));
4135 free(address);
Mikhail Naganov00260b52016-10-13 12:54:24 -07004136 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07004137 status = mOutput->stream->setParameters(param.toString());
Eric Laurent054d9d32015-04-24 08:48:48 -07004138 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent1c333e22014-05-20 10:48:17 -07004139 }
Eric Laurente8726fe2015-06-26 09:39:24 -07004140 if (configChanged) {
4141 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
4142 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004143 return status;
4144}
4145
Eric Laurent054d9d32015-04-24 08:48:48 -07004146status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
4147{
Andy Hungf60abce2016-08-26 11:37:54 -07004148 status_t status;
4149 if (property_get_bool("af.patch_park", false /* default_value */)) {
4150 // Park FastMixer to avoid potential DOS issues with writing to the HAL
4151 // or if HAL does not properly lock against access.
4152 AutoPark<FastMixer> park(mFastMixer);
4153 status = PlaybackThread::releaseAudioPatch_l(handle);
4154 } else {
4155 status = PlaybackThread::releaseAudioPatch_l(handle);
4156 }
Eric Laurent054d9d32015-04-24 08:48:48 -07004157 return status;
4158}
4159
Eric Laurent1c333e22014-05-20 10:48:17 -07004160status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
4161{
4162 status_t status = NO_ERROR;
Eric Laurent054d9d32015-04-24 08:48:48 -07004163
jiabin10d86fd2019-10-31 17:20:42 -07004164 mPatch = audio_patch{};
4165 mOutDeviceTypeAddrs.clear();
Eric Laurent054d9d32015-04-24 08:48:48 -07004166
Mikhail Naganov9ee05402016-10-13 15:58:17 -07004167 if (mOutput->audioHwDev->supportsAudioPatches()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07004168 sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
4169 status = hwDevice->releaseAudioPatch(handle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004170 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -07004171 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07004172 param.addInt(String8(AudioParameter::keyRouting), 0);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07004173 status = mOutput->stream->setParameters(param.toString());
Eric Laurent1c333e22014-05-20 10:48:17 -07004174 }
4175 return status;
4176}
4177
Eric Laurent83b88082014-06-20 18:31:16 -07004178void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track)
4179{
4180 Mutex::Autolock _l(mLock);
4181 mTracks.add(track);
4182}
4183
4184void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track)
4185{
4186 Mutex::Autolock _l(mLock);
4187 destroyTrack_l(track);
4188}
4189
Mikhail Naganovdc769682018-05-04 15:34:08 -07004190void AudioFlinger::PlaybackThread::toAudioPortConfig(struct audio_port_config *config)
Eric Laurent83b88082014-06-20 18:31:16 -07004191{
Mikhail Naganovdc769682018-05-04 15:34:08 -07004192 ThreadBase::toAudioPortConfig(config);
Eric Laurent83b88082014-06-20 18:31:16 -07004193 config->role = AUDIO_PORT_ROLE_SOURCE;
4194 config->ext.mix.hw_module = mOutput->audioHwDev->handle();
4195 config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07004196 if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
4197 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
4198 config->flags.output = mOutput->flags;
4199 }
Eric Laurent83b88082014-06-20 18:31:16 -07004200}
4201
Eric Laurent81784c32012-11-19 14:55:58 -08004202// ----------------------------------------------------------------------------
4203
4204AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
jiabin10d86fd2019-10-31 17:20:42 -07004205 audio_io_handle_t id, bool systemReady, type_t type)
4206 : PlaybackThread(audioFlinger, output, id, type, systemReady),
Eric Laurent81784c32012-11-19 14:55:58 -08004207 // mAudioMixer below
4208 // mFastMixer below
Andy Hung2ddee192015-12-18 17:34:44 -08004209 mFastMixerFutex(0),
4210 mMasterMono(false)
Eric Laurent81784c32012-11-19 14:55:58 -08004211 // mOutputSink below
4212 // mPipeSink below
4213 // mNormalSink below
4214{
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01004215 setMasterBalance(audioFlinger->getMasterBalance_l());
jiabin10d86fd2019-10-31 17:20:42 -07004216 ALOGV("MixerThread() id=%d type=%d", id, type);
Glenn Kasten49f36ba2017-12-06 13:02:02 -08004217 ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, "
Glenn Kastenc42e9b42016-03-21 11:35:03 -07004218 "mFrameCount=%zu, mNormalFrameCount=%zu",
Eric Laurent81784c32012-11-19 14:55:58 -08004219 mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
4220 mNormalFrameCount);
4221 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
4222
Andy Hungfbfc3952015-01-15 13:33:51 -08004223 if (type == DUPLICATING) {
4224 // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks
4225 // (downstream MixerThreads) in DuplicatingThread::threadLoop_write().
4226 // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink.
4227 return;
4228 }
Eric Laurent81784c32012-11-19 14:55:58 -08004229 // create an NBAIO sink for the HAL output stream, and negotiate
Mikhail Naganova0c91332016-09-19 10:01:12 -07004230 mOutputSink = new AudioStreamOutSink(output->stream);
Eric Laurent81784c32012-11-19 14:55:58 -08004231 size_t numCounterOffers = 0;
jiabin245cdd92018-12-07 17:55:15 -08004232 const NBAIO_Format offers[1] = {Format_from_SR_C(
4233 mSampleRate, mChannelCount + mHapticChannelCount, mFormat)};
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07004234#if !LOG_NDEBUG
4235 ssize_t index =
4236#else
4237 (void)
4238#endif
4239 mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
Eric Laurent81784c32012-11-19 14:55:58 -08004240 ALOG_ASSERT(index == 0);
4241
4242 // initialize fast mixer depending on configuration
4243 bool initFastMixer;
4244 switch (kUseFastMixer) {
4245 case FastMixer_Never:
4246 initFastMixer = false;
4247 break;
4248 case FastMixer_Always:
4249 initFastMixer = true;
4250 break;
4251 case FastMixer_Static:
4252 case FastMixer_Dynamic:
Andy Hungfda69402017-02-15 14:33:12 -08004253 // FastMixer was designed to operate with a HAL that pulls at a regular rate,
4254 // where the period is less than an experimentally determined threshold that can be
4255 // scheduled reliably with CFS. However, the BT A2DP HAL is
4256 // bursty (does not pull at a regular rate) and so cannot operate with FastMixer.
4257 initFastMixer = mFrameCount < mNormalFrameCount
jiabin10d86fd2019-10-31 17:20:42 -07004258 && Intersection(outDeviceTypes(), getAudioDeviceOutAllA2dpSet()).empty();
Eric Laurent81784c32012-11-19 14:55:58 -08004259 break;
4260 }
Andy Hungfda69402017-02-15 14:33:12 -08004261 ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount,
4262 "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu",
4263 mFrameCount, mNormalFrameCount);
Eric Laurent81784c32012-11-19 14:55:58 -08004264 if (initFastMixer) {
Andy Hung1258c1a2014-05-23 21:22:17 -07004265 audio_format_t fastMixerFormat;
4266 if (mMixerBufferEnabled && mEffectBufferEnabled) {
4267 fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT;
4268 } else {
4269 fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
4270 }
4271 if (mFormat != fastMixerFormat) {
4272 // change our Sink format to accept our intermediate precision
4273 mFormat = fastMixerFormat;
4274 free(mSinkBuffer);
jiabin245cdd92018-12-07 17:55:15 -08004275 mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -07004276 const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
4277 (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
4278 }
Eric Laurent81784c32012-11-19 14:55:58 -08004279
4280 // create a MonoPipe to connect our submix to FastMixer
4281 NBAIO_Format format = mOutputSink->format();
Andy Hung8946a282018-04-19 20:04:56 -07004282
Andy Hung1258c1a2014-05-23 21:22:17 -07004283 // adjust format to match that of the Fast Mixer
Glenn Kasten49f36ba2017-12-06 13:02:02 -08004284 ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -07004285 format.mFormat = fastMixerFormat;
4286 format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount;
4287
Eric Laurent81784c32012-11-19 14:55:58 -08004288 // This pipe depth compensates for scheduling latency of the normal mixer thread.
4289 // When it wakes up after a maximum latency, it runs a few cycles quickly before
4290 // finally blocking. Note the pipe implementation rounds up the request to a power of 2.
4291 MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
4292 const NBAIO_Format offers[1] = {format};
4293 size_t numCounterOffers = 0;
Andy Hung8946a282018-04-19 20:04:56 -07004294#if !LOG_NDEBUG
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07004295 ssize_t index =
4296#else
4297 (void)
4298#endif
4299 monoPipe->negotiate(offers, 1, NULL, numCounterOffers);
Eric Laurent81784c32012-11-19 14:55:58 -08004300 ALOG_ASSERT(index == 0);
4301 monoPipe->setAvgFrames((mScreenState & 1) ?
4302 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
4303 mPipeSink = monoPipe;
4304
Eric Laurent81784c32012-11-19 14:55:58 -08004305 // create fast mixer and configure it initially with just one fast track for our submix
Andy Hung8946a282018-04-19 20:04:56 -07004306 mFastMixer = new FastMixer(mId);
Eric Laurent81784c32012-11-19 14:55:58 -08004307 FastMixerStateQueue *sq = mFastMixer->sq();
4308#ifdef STATE_QUEUE_DUMP
4309 sq->setObserverDump(&mStateQueueObserverDump);
4310 sq->setMutatorDump(&mStateQueueMutatorDump);
4311#endif
4312 FastMixerState *state = sq->begin();
4313 FastTrack *fastTrack = &state->mFastTracks[0];
4314 // wrap the source side of the MonoPipe to make it an AudioBufferProvider
4315 fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
4316 fastTrack->mVolumeProvider = NULL;
jiabin245cdd92018-12-07 17:55:15 -08004317 fastTrack->mChannelMask = mChannelMask | mHapticChannelMask; // mPipeSink channel mask for
4318 // audio to FastMixer
Andy Hunge8a1ced2014-05-09 15:02:21 -07004319 fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer
jiabin245cdd92018-12-07 17:55:15 -08004320 fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE;
jiabin84114c32019-04-10 16:38:07 -07004321 fastTrack->mHapticIntensity = AudioMixer::HAPTIC_SCALE_NONE;
Eric Laurent81784c32012-11-19 14:55:58 -08004322 fastTrack->mGeneration++;
4323 state->mFastTracksGen++;
4324 state->mTrackMask = 1;
4325 // fast mixer will use the HAL output sink
4326 state->mOutputSink = mOutputSink.get();
4327 state->mOutputSinkGen++;
4328 state->mFrameCount = mFrameCount;
jiabin245cdd92018-12-07 17:55:15 -08004329 // specify sink channel mask when haptic channel mask present as it can not
4330 // be calculated directly from channel count
4331 state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE
4332 ? AUDIO_CHANNEL_NONE : mChannelMask | mHapticChannelMask;
Eric Laurent81784c32012-11-19 14:55:58 -08004333 state->mCommand = FastMixerState::COLD_IDLE;
4334 // already done in constructor initialization list
4335 //mFastMixerFutex = 0;
4336 state->mColdFutexAddr = &mFastMixerFutex;
4337 state->mColdGen++;
4338 state->mDumpState = &mFastMixerDumpState;
Glenn Kasten9e58b552013-01-18 15:09:48 -08004339 mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer");
4340 state->mNBLogWriter = mFastMixerNBLogWriter.get();
Eric Laurent81784c32012-11-19 14:55:58 -08004341 sq->end();
4342 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
4343
Eric Tan0513b5d2018-09-17 10:32:48 -07004344 NBLog::thread_info_t info;
4345 info.id = mId;
4346 info.type = NBLog::FASTMIXER;
4347 mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info);
4348
Eric Laurent81784c32012-11-19 14:55:58 -08004349 // start the fast mixer
4350 mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
4351 pid_t tid = mFastMixer->getTid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07004352 sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -08004353 stream()->setHalThreadPriority(kPriorityFastMixer);
Eric Laurent81784c32012-11-19 14:55:58 -08004354
4355#ifdef AUDIO_WATCHDOG
4356 // create and start the watchdog
4357 mAudioWatchdog = new AudioWatchdog();
4358 mAudioWatchdog->setDump(&mAudioWatchdogDump);
4359 mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
4360 tid = mAudioWatchdog->getTid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07004361 sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
Eric Laurent81784c32012-11-19 14:55:58 -08004362#endif
Andy Hung8946a282018-04-19 20:04:56 -07004363 } else {
4364#ifdef TEE_SINK
4365 // Only use the MixerThread tee if there is no FastMixer.
4366 mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
4367 mTee.setId(std::string("_") + std::to_string(mId) + "_M");
4368#endif
Eric Laurent81784c32012-11-19 14:55:58 -08004369 }
4370
4371 switch (kUseFastMixer) {
4372 case FastMixer_Never:
4373 case FastMixer_Dynamic:
4374 mNormalSink = mOutputSink;
4375 break;
4376 case FastMixer_Always:
4377 mNormalSink = mPipeSink;
4378 break;
4379 case FastMixer_Static:
4380 mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
4381 break;
4382 }
4383}
4384
4385AudioFlinger::MixerThread::~MixerThread()
4386{
Glenn Kasten4d23ca32014-05-13 10:39:51 -07004387 if (mFastMixer != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08004388 FastMixerStateQueue *sq = mFastMixer->sq();
4389 FastMixerState *state = sq->begin();
4390 if (state->mCommand == FastMixerState::COLD_IDLE) {
4391 int32_t old = android_atomic_inc(&mFastMixerFutex);
4392 if (old == -1) {
Elliott Hughesee499292014-05-21 17:55:51 -07004393 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
Eric Laurent81784c32012-11-19 14:55:58 -08004394 }
4395 }
4396 state->mCommand = FastMixerState::EXIT;
4397 sq->end();
4398 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
4399 mFastMixer->join();
4400 // Though the fast mixer thread has exited, it's state queue is still valid.
4401 // We'll use that extract the final state which contains one remaining fast track
4402 // corresponding to our sub-mix.
4403 state = sq->begin();
4404 ALOG_ASSERT(state->mTrackMask == 1);
4405 FastTrack *fastTrack = &state->mFastTracks[0];
4406 ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
4407 delete fastTrack->mBufferProvider;
4408 sq->end(false /*didModify*/);
Glenn Kasten4d23ca32014-05-13 10:39:51 -07004409 mFastMixer.clear();
Eric Laurent81784c32012-11-19 14:55:58 -08004410#ifdef AUDIO_WATCHDOG
4411 if (mAudioWatchdog != 0) {
4412 mAudioWatchdog->requestExit();
4413 mAudioWatchdog->requestExitAndWait();
4414 mAudioWatchdog.clear();
4415 }
4416#endif
4417 }
Glenn Kasten9e58b552013-01-18 15:09:48 -08004418 mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter);
Eric Laurent81784c32012-11-19 14:55:58 -08004419 delete mAudioMixer;
4420}
4421
4422
4423uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const
4424{
Glenn Kasten4d23ca32014-05-13 10:39:51 -07004425 if (mFastMixer != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08004426 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
4427 latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
4428 }
4429 return latency;
4430}
4431
Eric Laurentbfb1b832013-01-07 09:53:42 -08004432ssize_t AudioFlinger::MixerThread::threadLoop_write()
Eric Laurent81784c32012-11-19 14:55:58 -08004433{
4434 // FIXME we should only do one push per cycle; confirm this is true
4435 // Start the fast mixer if it's not already running
Glenn Kasten4d23ca32014-05-13 10:39:51 -07004436 if (mFastMixer != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08004437 FastMixerStateQueue *sq = mFastMixer->sq();
4438 FastMixerState *state = sq->begin();
4439 if (state->mCommand != FastMixerState::MIX_WRITE &&
4440 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
4441 if (state->mCommand == FastMixerState::COLD_IDLE) {
Eric Laurenta2ab4502015-09-09 12:25:51 -07004442
4443 // FIXME workaround for first HAL write being CPU bound on some devices
4444 ATRACE_BEGIN("write");
4445 mOutput->write((char *)mSinkBuffer, 0);
4446 ATRACE_END();
4447
Eric Laurent81784c32012-11-19 14:55:58 -08004448 int32_t old = android_atomic_inc(&mFastMixerFutex);
4449 if (old == -1) {
Elliott Hughesee499292014-05-21 17:55:51 -07004450 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
Eric Laurent81784c32012-11-19 14:55:58 -08004451 }
4452#ifdef AUDIO_WATCHDOG
4453 if (mAudioWatchdog != 0) {
4454 mAudioWatchdog->resume();
4455 }
4456#endif
4457 }
4458 state->mCommand = FastMixerState::MIX_WRITE;
Glenn Kastend797a9d2015-03-02 14:19:25 -08004459#ifdef FAST_THREAD_STATISTICS
Glenn Kasten4182c4e2013-07-15 14:45:07 -07004460 mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
Glenn Kastenfbdb2ac2015-03-02 14:47:19 -08004461 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN);
Glenn Kastend797a9d2015-03-02 14:19:25 -08004462#endif
Eric Laurent81784c32012-11-19 14:55:58 -08004463 sq->end();
4464 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
4465 if (kUseFastMixer == FastMixer_Dynamic) {
4466 mNormalSink = mPipeSink;
4467 }
4468 } else {
4469 sq->end(false /*didModify*/);
4470 }
4471 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08004472 return PlaybackThread::threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -08004473}
4474
4475void AudioFlinger::MixerThread::threadLoop_standby()
4476{
4477 // Idle the fast mixer if it's currently running
Glenn Kasten4d23ca32014-05-13 10:39:51 -07004478 if (mFastMixer != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08004479 FastMixerStateQueue *sq = mFastMixer->sq();
4480 FastMixerState *state = sq->begin();
4481 if (!(state->mCommand & FastMixerState::IDLE)) {
Andy Hung2148bf02016-11-28 19:01:02 -08004482 // Report any frames trapped in the Monopipe
4483 MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get();
4484 const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite();
4485 mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld "
4486 "monoPipeWritten:%lld monoPipeLeft:%lld",
4487 (long long)mFramesWritten, (long long)mSuspendedFrames,
4488 (long long)mPipeSink->framesWritten(), pipeFrames);
4489 mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str());
4490
Eric Laurent81784c32012-11-19 14:55:58 -08004491 state->mCommand = FastMixerState::COLD_IDLE;
4492 state->mColdFutexAddr = &mFastMixerFutex;
4493 state->mColdGen++;
4494 mFastMixerFutex = 0;
4495 sq->end();
4496 // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
4497 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
4498 if (kUseFastMixer == FastMixer_Dynamic) {
4499 mNormalSink = mOutputSink;
4500 }
4501#ifdef AUDIO_WATCHDOG
4502 if (mAudioWatchdog != 0) {
4503 mAudioWatchdog->pause();
4504 }
4505#endif
4506 } else {
4507 sq->end(false /*didModify*/);
4508 }
4509 }
4510 PlaybackThread::threadLoop_standby();
4511}
4512
Eric Laurentbfb1b832013-01-07 09:53:42 -08004513bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l()
4514{
4515 return false;
4516}
4517
4518bool AudioFlinger::PlaybackThread::shouldStandby_l()
4519{
4520 return !mStandby;
4521}
4522
4523bool AudioFlinger::PlaybackThread::waitingAsyncCallback()
4524{
4525 Mutex::Autolock _l(mLock);
4526 return waitingAsyncCallback_l();
4527}
4528
Eric Laurent81784c32012-11-19 14:55:58 -08004529// shared by MIXER and DIRECT, overridden by DUPLICATING
4530void AudioFlinger::PlaybackThread::threadLoop_standby()
4531{
4532 ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended);
Phil Burk062e67a2015-02-11 13:40:50 -08004533 mOutput->standby();
Eric Laurentbfb1b832013-01-07 09:53:42 -08004534 if (mUseAsyncWrite != 0) {
Eric Laurent3b4529e2013-09-05 18:09:19 -07004535 // discard any pending drain or write ack by incrementing sequence
4536 mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
4537 mDrainSequence = (mDrainSequence + 2) & ~1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08004538 ALOG_ASSERT(mCallbackThread != 0);
Eric Laurent3b4529e2013-09-05 18:09:19 -07004539 mCallbackThread->setWriteBlocked(mWriteAckSequence);
4540 mCallbackThread->setDraining(mDrainSequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -08004541 }
Eric Laurentd1f69b02014-12-15 14:33:13 -08004542 mHwPaused = false;
Eric Laurent81784c32012-11-19 14:55:58 -08004543}
4544
Haynes Mathew George4c6a4332014-01-15 12:31:39 -08004545void AudioFlinger::PlaybackThread::onAddNewTrack_l()
4546{
4547 ALOGV("signal playback thread");
4548 broadcast_l();
4549}
4550
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07004551void AudioFlinger::PlaybackThread::onAsyncError()
4552{
4553 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
4554 invalidateTracks((audio_stream_type_t)i);
4555 }
4556}
4557
Eric Laurent81784c32012-11-19 14:55:58 -08004558void AudioFlinger::MixerThread::threadLoop_mix()
4559{
Eric Laurent81784c32012-11-19 14:55:58 -08004560 // mix buffers...
Glenn Kastend79072e2016-01-06 08:41:20 -08004561 mAudioMixer->process();
Andy Hung25c2dac2014-02-27 14:56:00 -08004562 mCurrentWriteLength = mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -08004563 // increase sleep time progressively when application underrun condition clears.
4564 // Only increase sleep time if the mixer is ready for two consecutive times to avoid
4565 // that a steady state of alternating ready/not ready conditions keeps the sleep time
4566 // such that we would underrun the audio HAL.
Eric Laurentad9cb8b2015-05-26 16:38:19 -07004567 if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) {
Eric Laurent81784c32012-11-19 14:55:58 -08004568 sleepTimeShift--;
4569 }
Eric Laurentad9cb8b2015-05-26 16:38:19 -07004570 mSleepTimeUs = 0;
4571 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
Eric Laurent81784c32012-11-19 14:55:58 -08004572 //TODO: delay standby when effects have a tail
Glenn Kasten4c053ea2014-09-28 14:41:07 -07004573
Eric Laurent81784c32012-11-19 14:55:58 -08004574}
4575
4576void AudioFlinger::MixerThread::threadLoop_sleepTime()
4577{
4578 // If no tracks are ready, sleep once for the duration of an output
4579 // buffer size, then write 0s to the output
Eric Laurentad9cb8b2015-05-26 16:38:19 -07004580 if (mSleepTimeUs == 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08004581 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Andy Hung06d13222018-05-03 20:13:31 -07004582 if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) {
4583 // Using the Monopipe availableToWrite, we estimate the
4584 // sleep time to retry for more data (before we underrun).
4585 MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get());
4586 const ssize_t availableToWrite = mPipeSink->availableToWrite();
4587 const size_t pipeFrames = monoPipe->maxFrames();
4588 const size_t framesLeft = pipeFrames - max(availableToWrite, 0);
4589 // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount
4590 const size_t framesDelay = std::min(
4591 mNormalFrameCount, max(framesLeft / 2, mFrameCount));
4592 ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu",
4593 pipeFrames, framesLeft, framesDelay);
4594 mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate;
4595 } else {
4596 mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift;
4597 if (mSleepTimeUs < kMinThreadSleepTimeUs) {
4598 mSleepTimeUs = kMinThreadSleepTimeUs;
4599 }
4600 // reduce sleep time in case of consecutive application underruns to avoid
4601 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
4602 // duration we would end up writing less data than needed by the audio HAL if
4603 // the condition persists.
4604 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
4605 sleepTimeShift++;
4606 }
Eric Laurent81784c32012-11-19 14:55:58 -08004607 }
4608 } else {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07004609 mSleepTimeUs = mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08004610 }
4611 } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
Andy Hung98ef9782014-03-04 14:46:50 -08004612 // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared
4613 // before effects processing or output.
4614 if (mMixerBufferValid) {
4615 memset(mMixerBuffer, 0, mMixerBufferSize);
4616 } else {
4617 memset(mSinkBuffer, 0, mSinkBufferSize);
4618 }
Eric Laurentad9cb8b2015-05-26 16:38:19 -07004619 mSleepTimeUs = 0;
Eric Laurent81784c32012-11-19 14:55:58 -08004620 ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED),
4621 "anticipated start");
4622 }
4623 // TODO add standby time extension fct of effect tail
4624}
4625
4626// prepareTracks_l() must be called with ThreadBase::mLock held
4627AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
4628 Vector< sp<Track> > *tracksToRemove)
4629{
Andy Hungc0691382018-09-12 18:01:57 -07004630 // clean up deleted track ids in AudioMixer before allocating new tracks
4631 (void)mTracks.processDeletedTrackIds([this](int trackId) {
4632 // for each trackId, destroy it in the AudioMixer
4633 if (mAudioMixer->exists(trackId)) {
4634 mAudioMixer->destroy(trackId);
Andy Hung1bc088a2018-02-09 15:57:31 -08004635 }
4636 });
Andy Hungc0691382018-09-12 18:01:57 -07004637 mTracks.clearDeletedTrackIds();
Eric Laurent81784c32012-11-19 14:55:58 -08004638
4639 mixer_state mixerStatus = MIXER_IDLE;
4640 // find out which tracks need to be processed
4641 size_t count = mActiveTracks.size();
4642 size_t mixedTracks = 0;
4643 size_t tracksWithEffect = 0;
4644 // counts only _active_ fast tracks
4645 size_t fastTracks = 0;
4646 uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
4647
4648 float masterVolume = mMasterVolume;
4649 bool masterMute = mMasterMute;
4650
4651 if (masterMute) {
4652 masterVolume = 0;
4653 }
4654 // Delegate master volume control to effect in output mix effect chain if needed
4655 sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
4656 if (chain != 0) {
4657 uint32_t v = (uint32_t)(masterVolume * (1 << 24));
4658 chain->setVolume_l(&v, &v);
4659 masterVolume = (float)((v + (1 << 23)) >> 24);
4660 chain.clear();
4661 }
4662
4663 // prepare a new state to push
4664 FastMixerStateQueue *sq = NULL;
4665 FastMixerState *state = NULL;
4666 bool didModify = false;
4667 FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
Andy Hungf2285312017-02-14 18:31:59 -08004668 bool coldIdle = false;
Glenn Kasten4d23ca32014-05-13 10:39:51 -07004669 if (mFastMixer != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08004670 sq = mFastMixer->sq();
4671 state = sq->begin();
Andy Hungf2285312017-02-14 18:31:59 -08004672 coldIdle = state->mCommand == FastMixerState::COLD_IDLE;
Eric Laurent81784c32012-11-19 14:55:58 -08004673 }
4674
Andy Hung69aed5f2014-02-25 17:24:40 -08004675 mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found.
Andy Hung98ef9782014-03-04 14:46:50 -08004676 mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found.
Andy Hung69aed5f2014-02-25 17:24:40 -08004677
Andy Hungbd3b2b02018-05-21 10:53:11 -07004678 // DeferredOperations handles statistics after setting mixerStatus.
4679 class DeferredOperations {
4680 public:
4681 DeferredOperations(mixer_state *mixerStatus)
4682 : mMixerStatus(mixerStatus) { }
4683
4684 // when leaving scope, tally frames properly.
4685 ~DeferredOperations() {
4686 // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY)
4687 // because that is when the underrun occurs.
4688 // We do not distinguish between FastTracks and NormalTracks here.
4689 if (*mMixerStatus == MIXER_TRACKS_READY) {
4690 for (const auto &underrun : mUnderrunFrames) {
4691 underrun.first->mAudioTrackServerProxy->tallyUnderrunFrames(
4692 underrun.second);
4693 }
4694 }
4695 }
4696
4697 // tallyUnderrunFrames() is called to update the track counters
4698 // with the number of underrun frames for a particular mixer period.
4699 // We defer tallying until we know the final mixer status.
4700 void tallyUnderrunFrames(sp<Track> track, size_t underrunFrames) {
4701 mUnderrunFrames.emplace_back(track, underrunFrames);
4702 }
4703
4704 private:
4705 const mixer_state * const mMixerStatus;
4706 std::vector<std::pair<sp<Track>, size_t>> mUnderrunFrames;
4707 } deferredOperations(&mixerStatus); // implicit nested scope for variable capture
4708
jiabin245cdd92018-12-07 17:55:15 -08004709 bool noFastHapticTrack = true;
Eric Laurent81784c32012-11-19 14:55:58 -08004710 for (size_t i=0 ; i<count ; i++) {
Andy Hungdae27702016-10-31 14:01:16 -07004711 const sp<Track> t = mActiveTracks[i];
Eric Laurent81784c32012-11-19 14:55:58 -08004712
4713 // this const just means the local variable doesn't change
4714 Track* const track = t.get();
4715
4716 // process fast tracks
4717 if (track->isFastTrack()) {
Andy Hungae22b482019-05-09 15:38:55 -07004718 LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr,
4719 "%s(%d): FastTrack(%d) present without FastMixer",
4720 __func__, id(), track->id());
4721
jiabin245cdd92018-12-07 17:55:15 -08004722 if (track->getHapticPlaybackEnabled()) {
4723 noFastHapticTrack = false;
4724 }
Eric Laurent81784c32012-11-19 14:55:58 -08004725
4726 // It's theoretically possible (though unlikely) for a fast track to be created
4727 // and then removed within the same normal mix cycle. This is not a problem, as
4728 // the track never becomes active so it's fast mixer slot is never touched.
4729 // The converse, of removing an (active) track and then creating a new track
4730 // at the identical fast mixer slot within the same normal mix cycle,
4731 // is impossible because the slot isn't marked available until the end of each cycle.
4732 int j = track->mFastIndex;
Glenn Kastendc2c50b2016-04-21 08:13:14 -07004733 ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks);
Eric Laurent81784c32012-11-19 14:55:58 -08004734 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
4735 FastTrack *fastTrack = &state->mFastTracks[j];
4736
4737 // Determine whether the track is currently in underrun condition,
4738 // and whether it had a recent underrun.
4739 FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
4740 FastTrackUnderruns underruns = ftDump->mUnderruns;
4741 uint32_t recentFull = (underruns.mBitFields.mFull -
4742 track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK;
4743 uint32_t recentPartial = (underruns.mBitFields.mPartial -
4744 track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK;
4745 uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
4746 track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK;
4747 uint32_t recentUnderruns = recentPartial + recentEmpty;
4748 track->mObservedUnderruns = underruns;
4749 // don't count underruns that occur while stopping or pausing
4750 // or stopped which can occur when flush() is called while active
Andy Hungbd3b2b02018-05-21 10:53:11 -07004751 size_t underrunFrames = 0;
Glenn Kasten82aaf942013-07-17 16:05:07 -07004752 if (!(track->isStopping() || track->isPausing() || track->isStopped()) &&
4753 recentUnderruns > 0) {
4754 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun
Andy Hungbd3b2b02018-05-21 10:53:11 -07004755 underrunFrames = recentUnderruns * mFrameCount;
Eric Laurent81784c32012-11-19 14:55:58 -08004756 }
Andy Hungbd3b2b02018-05-21 10:53:11 -07004757 // Immediately account for FastTrack underruns.
4758 track->mAudioTrackServerProxy->tallyUnderrunFrames(underrunFrames);
Eric Laurent81784c32012-11-19 14:55:58 -08004759
4760 // This is similar to the state machine for normal tracks,
4761 // with a few modifications for fast tracks.
4762 bool isActive = true;
4763 switch (track->mState) {
4764 case TrackBase::STOPPING_1:
4765 // track stays active in STOPPING_1 state until first underrun
Eric Laurentbfb1b832013-01-07 09:53:42 -08004766 if (recentUnderruns > 0 || track->isTerminated()) {
Eric Laurent81784c32012-11-19 14:55:58 -08004767 track->mState = TrackBase::STOPPING_2;
4768 }
4769 break;
4770 case TrackBase::PAUSING:
4771 // ramp down is not yet implemented
4772 track->setPaused();
4773 break;
4774 case TrackBase::RESUMING:
4775 // ramp up is not yet implemented
4776 track->mState = TrackBase::ACTIVE;
4777 break;
4778 case TrackBase::ACTIVE:
4779 if (recentFull > 0 || recentPartial > 0) {
4780 // track has provided at least some frames recently: reset retry count
4781 track->mRetryCount = kMaxTrackRetries;
4782 }
4783 if (recentUnderruns == 0) {
4784 // no recent underruns: stay active
4785 break;
4786 }
4787 // there has recently been an underrun of some kind
4788 if (track->sharedBuffer() == 0) {
4789 // were any of the recent underruns "empty" (no frames available)?
4790 if (recentEmpty == 0) {
4791 // no, then ignore the partial underruns as they are allowed indefinitely
4792 break;
4793 }
4794 // there has recently been an "empty" underrun: decrement the retry counter
4795 if (--(track->mRetryCount) > 0) {
4796 break;
4797 }
4798 // indicate to client process that the track was disabled because of underrun;
4799 // it will then automatically call start() when data is available
Eric Laurent4d231dc2016-03-11 18:38:23 -08004800 track->disable();
Eric Laurent81784c32012-11-19 14:55:58 -08004801 // remove from active list, but state remains ACTIVE [confusing but true]
4802 isActive = false;
4803 break;
4804 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -07004805 FALLTHROUGH_INTENDED;
Eric Laurent81784c32012-11-19 14:55:58 -08004806 case TrackBase::STOPPING_2:
4807 case TrackBase::PAUSED:
Eric Laurent81784c32012-11-19 14:55:58 -08004808 case TrackBase::STOPPED:
4809 case TrackBase::FLUSHED: // flush() while active
4810 // Check for presentation complete if track is inactive
4811 // We have consumed all the buffers of this track.
4812 // This would be incomplete if we auto-paused on underrun
4813 {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07004814 uint32_t latency = 0;
4815 status_t result = mOutput->stream->getLatency(&latency);
4816 ALOGE_IF(result != OK,
4817 "Error when retrieving output stream latency: %d", result);
4818 size_t audioHALFrames = (latency * mSampleRate) / 1000;
Andy Hung818e7a32016-02-16 18:08:07 -08004819 int64_t framesWritten = mBytesWritten / mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08004820 if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) {
4821 // track stays in active list until presentation is complete
4822 break;
4823 }
4824 }
4825 if (track->isStopping_2()) {
4826 track->mState = TrackBase::STOPPED;
4827 }
4828 if (track->isStopped()) {
4829 // Can't reset directly, as fast mixer is still polling this track
4830 // track->reset();
4831 // So instead mark this track as needing to be reset after push with ack
4832 resetMask |= 1 << i;
4833 }
4834 isActive = false;
4835 break;
4836 case TrackBase::IDLE:
4837 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -08004838 LOG_ALWAYS_FATAL("unexpected track state %d", track->mState);
Eric Laurent81784c32012-11-19 14:55:58 -08004839 }
4840
4841 if (isActive) {
4842 // was it previously inactive?
4843 if (!(state->mTrackMask & (1 << j))) {
4844 ExtendedAudioBufferProvider *eabp = track;
4845 VolumeProvider *vp = track;
4846 fastTrack->mBufferProvider = eabp;
4847 fastTrack->mVolumeProvider = vp;
Eric Laurent81784c32012-11-19 14:55:58 -08004848 fastTrack->mChannelMask = track->mChannelMask;
Andy Hunge8a1ced2014-05-09 15:02:21 -07004849 fastTrack->mFormat = track->mFormat;
jiabin245cdd92018-12-07 17:55:15 -08004850 fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
jiabin77270b82018-12-18 15:41:29 -08004851 fastTrack->mHapticIntensity = track->getHapticIntensity();
Eric Laurent81784c32012-11-19 14:55:58 -08004852 fastTrack->mGeneration++;
4853 state->mTrackMask |= 1 << j;
4854 didModify = true;
4855 // no acknowledgement required for newly active tracks
4856 }
Kevin Rocard12381092018-04-11 09:19:59 -07004857 sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
Eric Laurenteab90452019-06-24 15:17:46 -07004858 float volume;
4859 if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) {
4860 volume = 0.f;
4861 } else {
4862 volume = masterVolume * mStreamTypes[track->streamType()].volume;
4863 }
4864
4865 handleVoipVolume_l(&volume);
4866
Eric Laurent81784c32012-11-19 14:55:58 -08004867 // cache the combined master volume and stream type volume for fast mixer; this
4868 // lacks any synchronization or barrier so VolumeProvider may read a stale value
Andy Hung9fc8b5c2017-01-24 13:36:48 -08004869 const float vh = track->getVolumeHandler()->getVolume(
Eric Laurenteab90452019-06-24 15:17:46 -07004870 proxy->framesReleased()).first;
4871 volume *= vh;
Kevin Rocardb0eeaf122018-04-11 08:30:16 -07004872 track->mCachedVolume = volume;
Kevin Rocard12381092018-04-11 09:19:59 -07004873 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
4874 float vlf = volume * float_from_gain(gain_minifloat_unpack_left(vlr));
4875 float vrf = volume * float_from_gain(gain_minifloat_unpack_right(vlr));
Eric Laurenteab90452019-06-24 15:17:46 -07004876
Kevin Rocard12381092018-04-11 09:19:59 -07004877 track->setFinalVolume((vlf + vrf) / 2.f);
Eric Laurent81784c32012-11-19 14:55:58 -08004878 ++fastTracks;
4879 } else {
4880 // was it previously active?
4881 if (state->mTrackMask & (1 << j)) {
4882 fastTrack->mBufferProvider = NULL;
4883 fastTrack->mGeneration++;
4884 state->mTrackMask &= ~(1 << j);
4885 didModify = true;
4886 // If any fast tracks were removed, we must wait for acknowledgement
4887 // because we're about to decrement the last sp<> on those tracks.
4888 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
4889 } else {
Glenn Kastenbf848af2017-12-22 11:55:01 -08004890 // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an
4891 // AudioTrack may start (which may not be with a start() but with a write()
4892 // after underrun) and immediately paused or released. In that case the
4893 // FastTrack state hasn't had time to update.
4894 // TODO Remove the ALOGW when this theory is confirmed.
4895 ALOGW("fast track %d should have been active; "
Glenn Kastenf7d65ee2015-12-02 13:45:01 -08004896 "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d",
4897 j, track->mState, state->mTrackMask, recentUnderruns,
4898 track->sharedBuffer() != 0);
Glenn Kastenbf848af2017-12-22 11:55:01 -08004899 // Since the FastMixer state already has the track inactive, do nothing here.
Eric Laurent81784c32012-11-19 14:55:58 -08004900 }
4901 tracksToRemove->add(track);
4902 // Avoids a misleading display in dumpsys
4903 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL;
4904 }
jiabin245cdd92018-12-07 17:55:15 -08004905 if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) {
4906 fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
4907 didModify = true;
4908 }
Eric Laurent81784c32012-11-19 14:55:58 -08004909 continue;
4910 }
4911
4912 { // local variable scope to avoid goto warning
4913
4914 audio_track_cblk_t* cblk = track->cblk();
4915
4916 // The first time a track is added we wait
4917 // for all its buffers to be filled before processing it
Andy Hungc0691382018-09-12 18:01:57 -07004918 const int trackId = track->id();
Andy Hung1bc088a2018-02-09 15:57:31 -08004919
4920 // if an active track doesn't exist in the AudioMixer, create it.
Andy Hungc0691382018-09-12 18:01:57 -07004921 // use the trackId as the AudioMixer name.
4922 if (!mAudioMixer->exists(trackId)) {
Andy Hung1bc088a2018-02-09 15:57:31 -08004923 status_t status = mAudioMixer->create(
Andy Hungc0691382018-09-12 18:01:57 -07004924 trackId,
Andy Hung1bc088a2018-02-09 15:57:31 -08004925 track->mChannelMask,
4926 track->mFormat,
4927 track->mSessionId);
4928 if (status != OK) {
Andy Hungc0691382018-09-12 18:01:57 -07004929 ALOGW("%s(): AudioMixer cannot create track(%d)"
4930 " mask %#x, format %#x, sessionId %d",
4931 __func__, trackId,
4932 track->mChannelMask, track->mFormat, track->mSessionId);
Andy Hung1bc088a2018-02-09 15:57:31 -08004933 tracksToRemove->add(track);
4934 track->invalidate(); // consider it dead.
4935 continue;
4936 }
4937 }
4938
Eric Laurent81784c32012-11-19 14:55:58 -08004939 // make sure that we have enough frames to mix one full buffer.
4940 // enforce this condition only once to enable draining the buffer in case the client
4941 // app does not call stop() and relies on underrun to stop:
4942 // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
4943 // during last round
Glenn Kasten9f80dd22012-12-18 15:57:32 -08004944 size_t desiredFrames;
Andy Hung8edb8dc2015-03-26 19:13:55 -07004945 const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate();
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -07004946 AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate();
Andy Hung8edb8dc2015-03-26 19:13:55 -07004947
4948 desiredFrames = sourceFramesNeededWithTimestretch(
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -07004949 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed);
Andy Hung8edb8dc2015-03-26 19:13:55 -07004950 // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed.
4951 // add frames already consumed but not yet released by the resampler
4952 // because mAudioTrackServerProxy->framesReady() will include these frames
Andy Hungc0691382018-09-12 18:01:57 -07004953 desiredFrames += mAudioMixer->getUnreleasedFrames(trackId);
Andy Hung8edb8dc2015-03-26 19:13:55 -07004954
Eric Laurent81784c32012-11-19 14:55:58 -08004955 uint32_t minFrames = 1;
4956 if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
4957 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08004958 minFrames = desiredFrames;
Eric Laurent81784c32012-11-19 14:55:58 -08004959 }
Eric Laurent13e4c962013-12-20 17:36:01 -08004960
4961 size_t framesReady = track->framesReady();
Glenn Kastene7754022014-10-31 12:11:26 -07004962 if (ATRACE_ENABLED()) {
4963 // I wish we had formatted trace names
Andy Hung1bc088a2018-02-09 15:57:31 -08004964 std::string traceName("nRdy");
Andy Hungc0691382018-09-12 18:01:57 -07004965 traceName += std::to_string(trackId);
Andy Hung1bc088a2018-02-09 15:57:31 -08004966 ATRACE_INT(traceName.c_str(), framesReady);
Glenn Kastene7754022014-10-31 12:11:26 -07004967 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08004968 if ((framesReady >= minFrames) && track->isReady() &&
Eric Laurent81784c32012-11-19 14:55:58 -08004969 !track->isPaused() && !track->isTerminated())
4970 {
Andy Hungc0691382018-09-12 18:01:57 -07004971 ALOGVV("track(%d) s=%08x [OK] on thread %p", trackId, cblk->mServer, this);
Eric Laurent81784c32012-11-19 14:55:58 -08004972
4973 mixedTracks++;
4974
Andy Hung69aed5f2014-02-25 17:24:40 -08004975 // track->mainBuffer() != mSinkBuffer or mMixerBuffer means
4976 // there is an effect chain connected to the track
Eric Laurent81784c32012-11-19 14:55:58 -08004977 chain.clear();
Andy Hung69aed5f2014-02-25 17:24:40 -08004978 if (track->mainBuffer() != mSinkBuffer &&
4979 track->mainBuffer() != mMixerBuffer) {
Andy Hung98ef9782014-03-04 14:46:50 -08004980 if (mEffectBufferEnabled) {
4981 mEffectBufferValid = true; // Later can set directly.
4982 }
Eric Laurent81784c32012-11-19 14:55:58 -08004983 chain = getEffectChain_l(track->sessionId());
4984 // Delegate volume control to effect in track effect chain if needed
4985 if (chain != 0) {
4986 tracksWithEffect++;
4987 } else {
Andy Hungc0691382018-09-12 18:01:57 -07004988 ALOGW("prepareTracks_l(): track(%d) attached to effect but no chain found on "
Eric Laurent81784c32012-11-19 14:55:58 -08004989 "session %d",
Andy Hungc0691382018-09-12 18:01:57 -07004990 trackId, track->sessionId());
Eric Laurent81784c32012-11-19 14:55:58 -08004991 }
4992 }
4993
4994
4995 int param = AudioMixer::VOLUME;
4996 if (track->mFillingUpStatus == Track::FS_FILLED) {
4997 // no ramp for the first volume setting
4998 track->mFillingUpStatus = Track::FS_ACTIVE;
4999 if (track->mState == TrackBase::RESUMING) {
5000 track->mState = TrackBase::ACTIVE;
Revathi Uddaraju453bcb52017-08-14 14:19:40 +08005001 // If a new track is paused immediately after start, do not ramp on resume.
5002 if (cblk->mServer != 0) {
5003 param = AudioMixer::RAMP_VOLUME;
5004 }
Eric Laurent81784c32012-11-19 14:55:58 -08005005 }
Andy Hungc0691382018-09-12 18:01:57 -07005006 mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
Eric Laurent7c29ec92017-09-20 17:54:22 -07005007 mLeftVolFloat = -1.0;
Glenn Kastenf20e1d82013-07-12 09:45:18 -07005008 // FIXME should not make a decision based on mServer
5009 } else if (cblk->mServer != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08005010 // If the track is stopped before the first frame was mixed,
5011 // do not apply ramp
5012 param = AudioMixer::RAMP_VOLUME;
5013 }
5014
5015 // compute volume for this track
Andy Hung6be49402014-05-30 10:42:03 -07005016 uint32_t vl, vr; // in U8.24 integer format
5017 float vlf, vrf, vaf; // in [0.0, 1.0] float format
Eric Laurent7c29ec92017-09-20 17:54:22 -07005018 // read original volumes with volume control
Eric Laurenteab90452019-06-24 15:17:46 -07005019 float v = masterVolume * mStreamTypes[track->streamType()].volume;
Andy Hung333ab962019-05-28 20:23:35 -07005020 // Always fetch volumeshaper volume to ensure state is updated.
5021 const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
5022 const float vh = track->getVolumeHandler()->getVolume(
5023 track->mAudioTrackServerProxy->framesReleased()).first;
Eric Laurent7c29ec92017-09-20 17:54:22 -07005024
Eric Laurenteab90452019-06-24 15:17:46 -07005025 if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
5026 v = 0;
5027 }
5028
5029 handleVoipVolume_l(&v);
5030
5031 if (track->isPausing()) {
Andy Hung6be49402014-05-30 10:42:03 -07005032 vl = vr = 0;
5033 vlf = vrf = vaf = 0.;
Eric Laurenteab90452019-06-24 15:17:46 -07005034 track->setPaused();
Eric Laurent81784c32012-11-19 14:55:58 -08005035 } else {
Glenn Kastenc56f3422014-03-21 17:53:17 -07005036 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -07005037 vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
5038 vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
Eric Laurent81784c32012-11-19 14:55:58 -08005039 // track volumes come from shared memory, so can't be trusted and must be clamped
Glenn Kastenc56f3422014-03-21 17:53:17 -07005040 if (vlf > GAIN_FLOAT_UNITY) {
5041 ALOGV("Track left volume out of range: %.3g", vlf);
5042 vlf = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08005043 }
Glenn Kastenc56f3422014-03-21 17:53:17 -07005044 if (vrf > GAIN_FLOAT_UNITY) {
5045 ALOGV("Track right volume out of range: %.3g", vrf);
5046 vrf = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08005047 }
Andy Hung9fc8b5c2017-01-24 13:36:48 -08005048 // now apply the master volume and stream type volume and shaper volume
5049 vlf *= v * vh;
5050 vrf *= v * vh;
Eric Laurent81784c32012-11-19 14:55:58 -08005051 // assuming master volume and stream type volume each go up to 1.0,
Andy Hung6be49402014-05-30 10:42:03 -07005052 // then derive vl and vr as U8.24 versions for the effect chain
5053 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
5054 vl = (uint32_t) (scaleto8_24 * vlf);
5055 vr = (uint32_t) (scaleto8_24 * vrf);
5056 // vl and vr are now in U8.24 format
Glenn Kastene3aa6592012-12-04 12:22:46 -08005057 uint16_t sendLevel = proxy->getSendLevel_U4_12();
Eric Laurent81784c32012-11-19 14:55:58 -08005058 // send level comes from shared memory and so may be corrupt
5059 if (sendLevel > MAX_GAIN_INT) {
5060 ALOGV("Track send level out of range: %04X", sendLevel);
5061 sendLevel = MAX_GAIN_INT;
5062 }
Andy Hung6be49402014-05-30 10:42:03 -07005063 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
5064 vaf = v * sendLevel * (1. / MAX_GAIN_INT);
Eric Laurent81784c32012-11-19 14:55:58 -08005065 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08005066
Kevin Rocard12381092018-04-11 09:19:59 -07005067 track->setFinalVolume((vrf + vlf) / 2.f);
5068
Eric Laurent81784c32012-11-19 14:55:58 -08005069 // Delegate volume control to effect in track effect chain if needed
5070 if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
5071 // Do not ramp volume if volume is controlled by effect
5072 param = AudioMixer::VOLUME;
Bryant Liub6be7f22014-06-12 22:02:41 +08005073 // Update remaining floating point volume levels
5074 vlf = (float)vl / (1 << 24);
5075 vrf = (float)vr / (1 << 24);
Eric Laurent81784c32012-11-19 14:55:58 -08005076 track->mHasVolumeController = true;
5077 } else {
5078 // force no volume ramp when volume controller was just disabled or removed
5079 // from effect chain to avoid volume spike
5080 if (track->mHasVolumeController) {
5081 param = AudioMixer::VOLUME;
5082 }
5083 track->mHasVolumeController = false;
5084 }
5085
Eric Laurent81784c32012-11-19 14:55:58 -08005086 // XXX: these things DON'T need to be done each time
Andy Hungc0691382018-09-12 18:01:57 -07005087 mAudioMixer->setBufferProvider(trackId, track);
5088 mAudioMixer->enable(trackId);
Eric Laurent81784c32012-11-19 14:55:58 -08005089
Andy Hungc0691382018-09-12 18:01:57 -07005090 mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf);
5091 mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf);
5092 mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf);
Eric Laurent81784c32012-11-19 14:55:58 -08005093 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005094 trackId,
Eric Laurent81784c32012-11-19 14:55:58 -08005095 AudioMixer::TRACK,
5096 AudioMixer::FORMAT, (void *)track->format());
5097 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005098 trackId,
Eric Laurent81784c32012-11-19 14:55:58 -08005099 AudioMixer::TRACK,
Kévin PETIT377b2ec2014-02-03 12:35:36 +00005100 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
Andy Hung9a592762014-07-21 21:56:01 -07005101 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005102 trackId,
Andy Hung9a592762014-07-21 21:56:01 -07005103 AudioMixer::TRACK,
jiabin245cdd92018-12-07 17:55:15 -08005104 AudioMixer::MIXER_CHANNEL_MASK,
5105 (void *)(uintptr_t)(mChannelMask | mHapticChannelMask));
Glenn Kastene3aa6592012-12-04 12:22:46 -08005106 // limit track sample rate to 2 x output sample rate, which changes at re-configuration
Andy Hungcd044842014-08-07 11:04:34 -07005107 uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX;
Andy Hung333ab962019-05-28 20:23:35 -07005108 uint32_t reqSampleRate = proxy->getSampleRate();
Glenn Kastene3aa6592012-12-04 12:22:46 -08005109 if (reqSampleRate == 0) {
5110 reqSampleRate = mSampleRate;
5111 } else if (reqSampleRate > maxSampleRate) {
5112 reqSampleRate = maxSampleRate;
5113 }
Eric Laurent81784c32012-11-19 14:55:58 -08005114 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005115 trackId,
Eric Laurent81784c32012-11-19 14:55:58 -08005116 AudioMixer::RESAMPLE,
5117 AudioMixer::SAMPLE_RATE,
Kévin PETIT377b2ec2014-02-03 12:35:36 +00005118 (void *)(uintptr_t)reqSampleRate);
Andy Hung8edb8dc2015-03-26 19:13:55 -07005119
Andy Hung333ab962019-05-28 20:23:35 -07005120 AudioPlaybackRate playbackRate = proxy->getPlaybackRate();
Andy Hung8edb8dc2015-03-26 19:13:55 -07005121 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005122 trackId,
Andy Hung8edb8dc2015-03-26 19:13:55 -07005123 AudioMixer::TIMESTRETCH,
5124 AudioMixer::PLAYBACK_RATE,
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -07005125 &playbackRate);
Andy Hung8edb8dc2015-03-26 19:13:55 -07005126
Andy Hung69aed5f2014-02-25 17:24:40 -08005127 /*
5128 * Select the appropriate output buffer for the track.
5129 *
Andy Hung98ef9782014-03-04 14:46:50 -08005130 * Tracks with effects go into their own effects chain buffer
5131 * and from there into either mEffectBuffer or mSinkBuffer.
Andy Hung69aed5f2014-02-25 17:24:40 -08005132 *
5133 * Other tracks can use mMixerBuffer for higher precision
5134 * channel accumulation. If this buffer is enabled
5135 * (mMixerBufferEnabled true), then selected tracks will accumulate
5136 * into it.
5137 *
5138 */
5139 if (mMixerBufferEnabled
5140 && (track->mainBuffer() == mSinkBuffer
5141 || track->mainBuffer() == mMixerBuffer)) {
5142 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005143 trackId,
Andy Hung69aed5f2014-02-25 17:24:40 -08005144 AudioMixer::TRACK,
Andy Hung78820702014-02-28 16:23:02 -08005145 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Andy Hung69aed5f2014-02-25 17:24:40 -08005146 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005147 trackId,
Andy Hung69aed5f2014-02-25 17:24:40 -08005148 AudioMixer::TRACK,
5149 AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
5150 // TODO: override track->mainBuffer()?
5151 mMixerBufferValid = true;
5152 } else {
5153 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005154 trackId,
Andy Hung69aed5f2014-02-25 17:24:40 -08005155 AudioMixer::TRACK,
rago94a1ee82017-07-21 15:11:02 -07005156 AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT);
Andy Hung69aed5f2014-02-25 17:24:40 -08005157 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005158 trackId,
Andy Hung69aed5f2014-02-25 17:24:40 -08005159 AudioMixer::TRACK,
5160 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
5161 }
Eric Laurent81784c32012-11-19 14:55:58 -08005162 mAudioMixer->setParameter(
Andy Hungc0691382018-09-12 18:01:57 -07005163 trackId,
Eric Laurent81784c32012-11-19 14:55:58 -08005164 AudioMixer::TRACK,
5165 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
jiabin245cdd92018-12-07 17:55:15 -08005166 mAudioMixer->setParameter(
5167 trackId,
5168 AudioMixer::TRACK,
5169 AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled());
jiabin77270b82018-12-18 15:41:29 -08005170 mAudioMixer->setParameter(
5171 trackId,
5172 AudioMixer::TRACK,
5173 AudioMixer::HAPTIC_INTENSITY, (void *)(uintptr_t)track->getHapticIntensity());
Eric Laurent81784c32012-11-19 14:55:58 -08005174
5175 // reset retry count
5176 track->mRetryCount = kMaxTrackRetries;
5177
5178 // If one track is ready, set the mixer ready if:
5179 // - the mixer was not ready during previous round OR
5180 // - no other track is not ready
5181 if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
5182 mixerStatus != MIXER_TRACKS_ENABLED) {
5183 mixerStatus = MIXER_TRACKS_READY;
5184 }
5185 } else {
Andy Hungbd3b2b02018-05-21 10:53:11 -07005186 size_t underrunFrames = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08005187 if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) {
Andy Hungc0691382018-09-12 18:01:57 -07005188 ALOGV("track(%d) underrun, framesReady(%zu) < framesDesired(%zd)",
5189 trackId, framesReady, desiredFrames);
Andy Hungbd3b2b02018-05-21 10:53:11 -07005190 underrunFrames = desiredFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08005191 }
Andy Hungbd3b2b02018-05-21 10:53:11 -07005192 deferredOperations.tallyUnderrunFrames(track, underrunFrames);
Phil Burk2812d9e2016-01-04 10:34:30 -08005193
Eric Laurent81784c32012-11-19 14:55:58 -08005194 // clear effect chain input buffer if an active track underruns to avoid sending
5195 // previous audio buffer again to effects
5196 chain = getEffectChain_l(track->sessionId());
5197 if (chain != 0) {
5198 chain->clearInputBuffer();
5199 }
5200
Andy Hungc0691382018-09-12 18:01:57 -07005201 ALOGVV("track(%d) s=%08x [NOT READY] on thread %p", trackId, cblk->mServer, this);
Eric Laurent81784c32012-11-19 14:55:58 -08005202 if ((track->sharedBuffer() != 0) || track->isTerminated() ||
5203 track->isStopped() || track->isPaused()) {
5204 // We have consumed all the buffers of this track.
5205 // Remove it from the list of active tracks.
5206 // TODO: use actual buffer filling status instead of latency when available from
5207 // audio HAL
5208 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000;
Andy Hung818e7a32016-02-16 18:08:07 -08005209 int64_t framesWritten = mBytesWritten / mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08005210 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) {
5211 if (track->isStopped()) {
5212 track->reset();
5213 }
5214 tracksToRemove->add(track);
5215 }
5216 } else {
Eric Laurent81784c32012-11-19 14:55:58 -08005217 // No buffers for this track. Give it a few chances to
5218 // fill a buffer, then remove it from active list.
5219 if (--(track->mRetryCount) <= 0) {
Andy Hungc0691382018-09-12 18:01:57 -07005220 ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p",
5221 trackId, this);
Eric Laurent81784c32012-11-19 14:55:58 -08005222 tracksToRemove->add(track);
5223 // indicate to client process that the track was disabled because of underrun;
5224 // it will then automatically call start() when data is available
Eric Laurent4d231dc2016-03-11 18:38:23 -08005225 track->disable();
Eric Laurent81784c32012-11-19 14:55:58 -08005226 // If one track is not ready, mark the mixer also not ready if:
5227 // - the mixer was ready during previous round OR
5228 // - no other track is ready
5229 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
5230 mixerStatus != MIXER_TRACKS_READY) {
5231 mixerStatus = MIXER_TRACKS_ENABLED;
5232 }
5233 }
Andy Hungc0691382018-09-12 18:01:57 -07005234 mAudioMixer->disable(trackId);
Eric Laurent81784c32012-11-19 14:55:58 -08005235 }
5236
5237 } // local variable scope to avoid goto warning
Eric Laurent81784c32012-11-19 14:55:58 -08005238
5239 }
5240
jiabin245cdd92018-12-07 17:55:15 -08005241 if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) {
5242 // When there is no fast track playing haptic and FastMixer exists,
5243 // enabling the first FastTrack, which provides mixed data from normal
5244 // tracks, to play haptic data.
5245 FastTrack *fastTrack = &state->mFastTracks[0];
5246 if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) {
5247 fastTrack->mHapticPlaybackEnabled = noFastHapticTrack;
5248 didModify = true;
5249 }
5250 }
5251
Eric Laurent81784c32012-11-19 14:55:58 -08005252 // Push the new FastMixer state if necessary
5253 bool pauseAudioWatchdog = false;
5254 if (didModify) {
5255 state->mFastTracksGen++;
5256 // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
5257 if (kUseFastMixer == FastMixer_Dynamic &&
5258 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
5259 state->mCommand = FastMixerState::COLD_IDLE;
5260 state->mColdFutexAddr = &mFastMixerFutex;
5261 state->mColdGen++;
5262 mFastMixerFutex = 0;
5263 if (kUseFastMixer == FastMixer_Dynamic) {
5264 mNormalSink = mOutputSink;
5265 }
5266 // If we go into cold idle, need to wait for acknowledgement
5267 // so that fast mixer stops doing I/O.
5268 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
5269 pauseAudioWatchdog = true;
5270 }
Eric Laurent81784c32012-11-19 14:55:58 -08005271 }
5272 if (sq != NULL) {
5273 sq->end(didModify);
Andy Hungf2285312017-02-14 18:31:59 -08005274 // No need to block if the FastMixer is in COLD_IDLE as the FastThread
5275 // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE
5276 // when bringing the output sink into standby.)
5277 //
5278 // We will get the latest FastMixer state when we come out of COLD_IDLE.
5279 //
5280 // This occurs with BT suspend when we idle the FastMixer with
5281 // active tracks, which may be added or removed.
5282 sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block);
Eric Laurent81784c32012-11-19 14:55:58 -08005283 }
5284#ifdef AUDIO_WATCHDOG
5285 if (pauseAudioWatchdog && mAudioWatchdog != 0) {
5286 mAudioWatchdog->pause();
5287 }
5288#endif
5289
5290 // Now perform the deferred reset on fast tracks that have stopped
5291 while (resetMask != 0) {
5292 size_t i = __builtin_ctz(resetMask);
5293 ALOG_ASSERT(i < count);
5294 resetMask &= ~(1 << i);
Andy Hungdae27702016-10-31 14:01:16 -07005295 sp<Track> track = mActiveTracks[i];
Eric Laurent81784c32012-11-19 14:55:58 -08005296 ALOG_ASSERT(track->isFastTrack() && track->isStopped());
5297 track->reset();
5298 }
5299
Andy Hung80d03d22018-04-10 10:32:11 -07005300 // Track destruction may occur outside of threadLoop once it is removed from active tracks.
5301 // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if
5302 // it ceases to be active, to allow safe removal from the AudioMixer at the start
5303 // of prepareTracks_l(); this releases any outstanding buffer back to the track.
5304 // See also the implementation of destroyTrack_l().
5305 for (const auto &track : *tracksToRemove) {
Andy Hungc0691382018-09-12 18:01:57 -07005306 const int trackId = track->id();
5307 if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer.
5308 mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */);
Andy Hung80d03d22018-04-10 10:32:11 -07005309 }
5310 }
5311
Eric Laurent81784c32012-11-19 14:55:58 -08005312 // remove all the tracks that need to be...
Eric Laurentbfb1b832013-01-07 09:53:42 -08005313 removeTracks_l(*tracksToRemove);
Eric Laurent81784c32012-11-19 14:55:58 -08005314
Eric Laurent97d547d2014-09-02 14:45:53 -07005315 if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) {
5316 mEffectBufferValid = true;
Marco Nelissenac302142014-10-20 13:15:38 -07005317 }
5318
5319 if (mEffectBufferValid) {
Marco Nelissen57088b52014-10-17 16:39:39 -07005320 // as long as there are effects we should clear the effects buffer, to avoid
5321 // passing a non-clean buffer to the effect chain
5322 memset(mEffectBuffer, 0, mEffectBufferSize);
Eric Laurent97d547d2014-09-02 14:45:53 -07005323 }
Andy Hung69aed5f2014-02-25 17:24:40 -08005324 // sink or mix buffer must be cleared if all tracks are connected to an
5325 // effect chain as in this case the mixer will not write to the sink or mix buffer
5326 // and track effects will accumulate into it
Eric Laurentbfb1b832013-01-07 09:53:42 -08005327 if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
5328 (mixedTracks == 0 && fastTracks > 0))) {
Eric Laurent81784c32012-11-19 14:55:58 -08005329 // FIXME as a performance optimization, should remember previous zero status
Andy Hung69aed5f2014-02-25 17:24:40 -08005330 if (mMixerBufferValid) {
5331 memset(mMixerBuffer, 0, mMixerBufferSize);
5332 // TODO: In testing, mSinkBuffer below need not be cleared because
5333 // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer
5334 // after mixing.
5335 //
5336 // To enforce this guarantee:
5337 // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
5338 // (mixedTracks == 0 && fastTracks > 0))
5339 // must imply MIXER_TRACKS_READY.
5340 // Later, we may clear buffers regardless, and skip much of this logic.
5341 }
Andy Hung98ef9782014-03-04 14:46:50 -08005342 // FIXME as a performance optimization, should remember previous zero status
Andy Hung5567aaf2014-07-17 14:00:07 -07005343 memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08005344 }
5345
5346 // if any fast tracks, then status is ready
5347 mMixerStatusIgnoringFastTracks = mixerStatus;
5348 if (fastTracks > 0) {
5349 mixerStatus = MIXER_TRACKS_READY;
5350 }
5351 return mixerStatus;
5352}
5353
Eric Laurentad7dd962016-09-22 12:38:37 -07005354// trackCountForUid_l() must be called with ThreadBase::mLock held
Andy Hung1bc088a2018-02-09 15:57:31 -08005355uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) const
Eric Laurentad7dd962016-09-22 12:38:37 -07005356{
5357 uint32_t trackCount = 0;
5358 for (size_t i = 0; i < mTracks.size() ; i++) {
Andy Hung1f12a8a2016-11-07 16:10:30 -08005359 if (mTracks[i]->uid() == uid) {
Eric Laurentad7dd962016-09-22 12:38:37 -07005360 trackCount++;
5361 }
5362 }
5363 return trackCount;
5364}
5365
Andy Hung1bc088a2018-02-09 15:57:31 -08005366// isTrackAllowed_l() must be called with ThreadBase::mLock held
5367bool AudioFlinger::MixerThread::isTrackAllowed_l(
5368 audio_channel_mask_t channelMask, audio_format_t format,
5369 audio_session_t sessionId, uid_t uid) const
Eric Laurent81784c32012-11-19 14:55:58 -08005370{
Andy Hung1bc088a2018-02-09 15:57:31 -08005371 if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) {
5372 return false;
Eric Laurentad7dd962016-09-22 12:38:37 -07005373 }
Andy Hung1bc088a2018-02-09 15:57:31 -08005374 // Check validity as we don't call AudioMixer::create() here.
Mikhail Naganov32f0d162019-07-30 14:42:32 -07005375 if (!mAudioMixer->isValidFormat(format)) {
Andy Hung1bc088a2018-02-09 15:57:31 -08005376 ALOGW("%s: invalid format: %#x", __func__, format);
5377 return false;
5378 }
Mikhail Naganov32f0d162019-07-30 14:42:32 -07005379 if (!mAudioMixer->isValidChannelMask(channelMask)) {
Andy Hung1bc088a2018-02-09 15:57:31 -08005380 ALOGW("%s: invalid channelMask: %#x", __func__, channelMask);
5381 return false;
5382 }
5383 return true;
Eric Laurent81784c32012-11-19 14:55:58 -08005384}
5385
Eric Laurent10351942014-05-08 18:49:52 -07005386// checkForNewParameter_l() must be called with ThreadBase::mLock held
5387bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair,
5388 status_t& status)
Eric Laurent81784c32012-11-19 14:55:58 -08005389{
Eric Laurent81784c32012-11-19 14:55:58 -08005390 bool reconfig = false;
Eric Laurent42537be2016-01-08 17:16:42 -08005391 bool a2dpDeviceChanged = false;
Eric Laurent81784c32012-11-19 14:55:58 -08005392
Eric Laurent10351942014-05-08 18:49:52 -07005393 status = NO_ERROR;
Eric Laurent81784c32012-11-19 14:55:58 -08005394
Glenn Kastenc05b8d72016-03-24 09:48:17 -07005395 AutoPark<FastMixer> park(mFastMixer);
Eric Laurent81784c32012-11-19 14:55:58 -08005396
Eric Laurent10351942014-05-08 18:49:52 -07005397 AudioParameter param = AudioParameter(keyValuePair);
5398 int value;
5399 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
5400 reconfig = true;
5401 }
5402 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Andy Hung9a592762014-07-21 21:56:01 -07005403 if (!isValidPcmSinkFormat((audio_format_t) value)) {
Eric Laurent10351942014-05-08 18:49:52 -07005404 status = BAD_VALUE;
5405 } else {
5406 // no need to save value, since it's constant
Eric Laurent81784c32012-11-19 14:55:58 -08005407 reconfig = true;
5408 }
Eric Laurent10351942014-05-08 18:49:52 -07005409 }
5410 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Andy Hung9a592762014-07-21 21:56:01 -07005411 if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) {
Eric Laurent10351942014-05-08 18:49:52 -07005412 status = BAD_VALUE;
5413 } else {
5414 // no need to save value, since it's constant
5415 reconfig = true;
Eric Laurent81784c32012-11-19 14:55:58 -08005416 }
Eric Laurent10351942014-05-08 18:49:52 -07005417 }
5418 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5419 // do not accept frame count changes if tracks are open as the track buffer
5420 // size depends on frame count and correct behavior would not be guaranteed
5421 // if frame count is changed after track creation
5422 if (!mTracks.isEmpty()) {
5423 status = INVALID_OPERATION;
5424 } else {
5425 reconfig = true;
Eric Laurent81784c32012-11-19 14:55:58 -08005426 }
Eric Laurent10351942014-05-08 18:49:52 -07005427 }
5428 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
jiabin10d86fd2019-10-31 17:20:42 -07005429 LOG_FATAL("Should not set routing device in MixerThread");
Eric Laurent10351942014-05-08 18:49:52 -07005430 }
Eric Laurent81784c32012-11-19 14:55:58 -08005431
Eric Laurent10351942014-05-08 18:49:52 -07005432 if (status == NO_ERROR) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07005433 status = mOutput->stream->setParameters(keyValuePair);
Eric Laurent10351942014-05-08 18:49:52 -07005434 if (!mStandby && status == INVALID_OPERATION) {
Phil Burk062e67a2015-02-11 13:40:50 -08005435 mOutput->standby();
Eric Laurent10351942014-05-08 18:49:52 -07005436 mStandby = true;
5437 mBytesWritten = 0;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07005438 status = mOutput->stream->setParameters(keyValuePair);
Eric Laurent81784c32012-11-19 14:55:58 -08005439 }
Eric Laurent10351942014-05-08 18:49:52 -07005440 if (status == NO_ERROR && reconfig) {
5441 readOutputParameters_l();
5442 delete mAudioMixer;
5443 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
Andy Hung1bc088a2018-02-09 15:57:31 -08005444 for (const auto &track : mTracks) {
Andy Hungc0691382018-09-12 18:01:57 -07005445 const int trackId = track->id();
Andy Hung1bc088a2018-02-09 15:57:31 -08005446 status_t status = mAudioMixer->create(
Andy Hungc0691382018-09-12 18:01:57 -07005447 trackId,
Andy Hung1bc088a2018-02-09 15:57:31 -08005448 track->mChannelMask,
5449 track->mFormat,
5450 track->mSessionId);
5451 ALOGW_IF(status != NO_ERROR,
Andy Hungc0691382018-09-12 18:01:57 -07005452 "%s(): AudioMixer cannot create track(%d)"
5453 " mask %#x, format %#x, sessionId %d",
Andy Hung1bc088a2018-02-09 15:57:31 -08005454 __func__,
Andy Hungc0691382018-09-12 18:01:57 -07005455 trackId, track->mChannelMask, track->mFormat, track->mSessionId);
Eric Laurent10351942014-05-08 18:49:52 -07005456 }
Eric Laurent73e26b62015-04-27 16:55:58 -07005457 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
Eric Laurent10351942014-05-08 18:49:52 -07005458 }
Eric Laurent81784c32012-11-19 14:55:58 -08005459 }
5460
Eric Laurent42537be2016-01-08 17:16:42 -08005461 return reconfig || a2dpDeviceChanged;
Eric Laurent81784c32012-11-19 14:55:58 -08005462}
5463
5464
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07005465void AudioFlinger::MixerThread::dumpInternals_l(int fd, const Vector<String16>& args)
Eric Laurent81784c32012-11-19 14:55:58 -08005466{
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07005467 PlaybackThread::dumpInternals_l(fd, args);
Andy Hung40eb1a12015-06-18 13:42:02 -07005468 dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs);
Andy Hung8ed196a2018-01-05 13:21:11 -08005469 dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str());
Andy Hung2ddee192015-12-18 17:34:44 -08005470 dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off");
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01005471 dprintf(fd, " Master balance: %f (%s)\n", mMasterBalance.load(),
5472 (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance())
5473 : mBalance.toString()).c_str());
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08005474 if (hasFastMixer()) {
5475 dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid());
5476
5477 // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
5478 // while we are dumping it. It may be inconsistent, but it won't mutate!
5479 // This is a large object so we place it on the heap.
5480 // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
Eric Tan2942a4e2018-09-12 17:41:27 -07005481 const std::unique_ptr<FastMixerDumpState> copy =
5482 std::make_unique<FastMixerDumpState>(mFastMixerDumpState);
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08005483 copy->dump(fd);
Eric Laurent81784c32012-11-19 14:55:58 -08005484
5485#ifdef STATE_QUEUE_DUMP
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08005486 // Similar for state queue
5487 StateQueueObserverDump observerCopy = mStateQueueObserverDump;
5488 observerCopy.dump(fd);
5489 StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
5490 mutatorCopy.dump(fd);
Eric Laurent81784c32012-11-19 14:55:58 -08005491#endif
5492
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08005493#ifdef AUDIO_WATCHDOG
5494 if (mAudioWatchdog != 0) {
5495 // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
5496 AudioWatchdogDump wdCopy = mAudioWatchdogDump;
5497 wdCopy.dump(fd);
5498 }
5499#endif
5500
5501 } else {
5502 dprintf(fd, " No FastMixer\n");
5503 }
Eric Laurent81784c32012-11-19 14:55:58 -08005504}
5505
5506uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const
5507{
5508 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
5509}
5510
5511uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const
5512{
5513 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
5514}
5515
5516void AudioFlinger::MixerThread::cacheParameters_l()
5517{
5518 PlaybackThread::cacheParameters_l();
5519
5520 // FIXME: Relaxed timing because of a certain device that can't meet latency
5521 // Should be reduced to 2x after the vendor fixes the driver issue
5522 // increase threshold again due to low power audio mode. The way this warning
5523 // threshold is calculated and its usefulness should be reconsidered anyway.
5524 maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
5525}
5526
5527// ----------------------------------------------------------------------------
5528
5529AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
jiabin10d86fd2019-10-31 17:20:42 -07005530 AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady)
5531 : PlaybackThread(audioFlinger, output, id, type, systemReady)
Eric Laurentbfb1b832013-01-07 09:53:42 -08005532{
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01005533 setMasterBalance(audioFlinger->getMasterBalance_l());
Eric Laurentbfb1b832013-01-07 09:53:42 -08005534}
5535
Eric Laurent81784c32012-11-19 14:55:58 -08005536AudioFlinger::DirectOutputThread::~DirectOutputThread()
5537{
5538}
5539
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07005540void AudioFlinger::DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args)
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01005541{
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07005542 PlaybackThread::dumpInternals_l(fd, args);
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01005543 dprintf(fd, " Master balance: %f Left: %f Right: %f\n",
5544 mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight);
5545}
5546
5547void AudioFlinger::DirectOutputThread::setMasterBalance(float balance)
5548{
5549 Mutex::Autolock _l(mLock);
5550 if (mMasterBalance != balance) {
5551 mMasterBalance.store(balance);
5552 mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight);
5553 broadcast_l();
5554 }
5555}
5556
Eric Laurent5850c4c2016-11-10 13:04:31 -08005557void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
Eric Laurentbfb1b832013-01-07 09:53:42 -08005558{
Eric Laurentbfb1b832013-01-07 09:53:42 -08005559 float left, right;
5560
Andy Hung333ab962019-05-28 20:23:35 -07005561 // Ensure volumeshaper state always advances even when muted.
5562 const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
5563 const auto [shaperVolume, shaperActive] = track->getVolumeHandler()->getVolume(
5564 proxy->framesReleased());
5565 mVolumeShaperActive = shaperActive;
5566
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -08005567 if (mMasterMute || mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08005568 left = right = 0;
5569 } else {
5570 float typeVolume = mStreamTypes[track->streamType()].volume;
Andy Hung333ab962019-05-28 20:23:35 -07005571 const float v = mMasterVolume * typeVolume * shaperVolume;
Andy Hung9fc8b5c2017-01-24 13:36:48 -08005572
Glenn Kastenc56f3422014-03-21 17:53:17 -07005573 gain_minifloat_packed_t vlr = proxy->getVolumeLR();
5574 left = float_from_gain(gain_minifloat_unpack_left(vlr));
5575 if (left > GAIN_FLOAT_UNITY) {
5576 left = GAIN_FLOAT_UNITY;
5577 }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01005578 left *= v * mMasterBalanceLeft; // DirectOutputThread balance applied as track volume
Glenn Kastenc56f3422014-03-21 17:53:17 -07005579 right = float_from_gain(gain_minifloat_unpack_right(vlr));
5580 if (right > GAIN_FLOAT_UNITY) {
5581 right = GAIN_FLOAT_UNITY;
5582 }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01005583 right *= v * mMasterBalanceRight;
Eric Laurentbfb1b832013-01-07 09:53:42 -08005584 }
5585
5586 if (lastTrack) {
Kevin Rocard12381092018-04-11 09:19:59 -07005587 track->setFinalVolume((left + right) / 2.f);
Eric Laurentbfb1b832013-01-07 09:53:42 -08005588 if (left != mLeftVolFloat || right != mRightVolFloat) {
5589 mLeftVolFloat = left;
5590 mRightVolFloat = right;
5591
Eric Laurentbfb1b832013-01-07 09:53:42 -08005592 // Delegate volume control to effect in track effect chain if needed
5593 // only one effect chain can be present on DirectOutputThread, so if
5594 // there is one, the track is connected to it
5595 if (!mEffectChains.isEmpty()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09005596 // if effect chain exists, volume is handled by it.
5597 // Convert volumes from float to 8.24
5598 uint32_t vl = (uint32_t)(left * (1 << 24));
5599 uint32_t vr = (uint32_t)(right * (1 << 24));
5600 // Direct/Offload effect chains set output volume in setVolume_l().
5601 (void)mEffectChains[0]->setVolume_l(&vl, &vr);
5602 } else {
5603 // otherwise we directly set the volume.
5604 setVolumeForOutput_l(left, right);
Eric Laurentbfb1b832013-01-07 09:53:42 -08005605 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08005606 }
5607 }
5608}
5609
Phil Burk43b4dcc2015-06-09 16:53:44 -07005610void AudioFlinger::DirectOutputThread::onAddNewTrack_l()
5611{
5612 sp<Track> previousTrack = mPreviousTrack.promote();
Andy Hungdae27702016-10-31 14:01:16 -07005613 sp<Track> latestTrack = mActiveTracks.getLatest();
Phil Burk43b4dcc2015-06-09 16:53:44 -07005614
Eric Laurent0f0631e2015-07-06 18:01:25 -07005615 if (previousTrack != 0 && latestTrack != 0) {
5616 if (mType == DIRECT) {
5617 if (previousTrack.get() != latestTrack.get()) {
5618 mFlushPending = true;
5619 }
5620 } else /* mType == OFFLOAD */ {
5621 if (previousTrack->sessionId() != latestTrack->sessionId()) {
5622 mFlushPending = true;
5623 }
5624 }
Revathi Uddaraju20413a92016-10-13 17:16:14 +08005625 } else if (previousTrack == 0) {
5626 // there could be an old track added back during track transition for direct
5627 // output, so always issues flush to flush data of the previous track if it
5628 // was already destroyed with HAL paused, then flush can resume the playback
5629 mFlushPending = true;
Phil Burk43b4dcc2015-06-09 16:53:44 -07005630 }
5631 PlaybackThread::onAddNewTrack_l();
5632}
Eric Laurentbfb1b832013-01-07 09:53:42 -08005633
Eric Laurent81784c32012-11-19 14:55:58 -08005634AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
5635 Vector< sp<Track> > *tracksToRemove
5636)
5637{
Eric Laurentd595b7c2013-04-03 17:27:56 -07005638 size_t count = mActiveTracks.size();
Eric Laurent81784c32012-11-19 14:55:58 -08005639 mixer_state mixerStatus = MIXER_IDLE;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005640 bool doHwPause = false;
5641 bool doHwResume = false;
Eric Laurent81784c32012-11-19 14:55:58 -08005642
5643 // find out which tracks need to be processed
Andy Hungdae27702016-10-31 14:01:16 -07005644 for (const sp<Track> &t : mActiveTracks) {
Eric Laurent5850c4c2016-11-10 13:04:31 -08005645 if (t->isInvalid()) {
Phil Burk43b4dcc2015-06-09 16:53:44 -07005646 ALOGW("An invalidated track shouldn't be in active list");
Eric Laurent5850c4c2016-11-10 13:04:31 -08005647 tracksToRemove->add(t);
Phil Burk43b4dcc2015-06-09 16:53:44 -07005648 continue;
5649 }
5650
Eric Laurent5850c4c2016-11-10 13:04:31 -08005651 Track* const track = t.get();
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07005652#ifdef VERY_VERY_VERBOSE_LOGGING
Eric Laurent81784c32012-11-19 14:55:58 -08005653 audio_track_cblk_t* cblk = track->cblk();
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07005654#endif
Eric Laurentfd477972013-10-25 18:10:40 -07005655 // Only consider last track started for volume and mixer state control.
5656 // In theory an older track could underrun and restart after the new one starts
5657 // but as we only care about the transition phase between two tracks on a
5658 // direct output, it is not a problem to ignore the underrun case.
Andy Hungdae27702016-10-31 14:01:16 -07005659 sp<Track> l = mActiveTracks.getLatest();
Eric Laurent5850c4c2016-11-10 13:04:31 -08005660 bool last = l.get() == track;
Eric Laurent81784c32012-11-19 14:55:58 -08005661
Phil Burk6fc2a7c2015-04-30 16:08:10 -07005662 if (track->isPausing()) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08005663 track->setPaused();
Phil Burk6fc2a7c2015-04-30 16:08:10 -07005664 if (mHwSupportsPause && last && !mHwPaused) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08005665 doHwPause = true;
5666 mHwPaused = true;
5667 }
Eric Laurentd1f69b02014-12-15 14:33:13 -08005668 } else if (track->isFlushPending()) {
5669 track->flushAck();
5670 if (last) {
Phil Burk43b4dcc2015-06-09 16:53:44 -07005671 mFlushPending = true;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005672 }
Phil Burk6fc2a7c2015-04-30 16:08:10 -07005673 } else if (track->isResumePending()) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08005674 track->resumeAck();
Eric Laurent3df841a2016-07-15 15:15:40 -07005675 if (last) {
5676 mLeftVolFloat = mRightVolFloat = -1.0;
5677 if (mHwPaused) {
5678 doHwResume = true;
5679 mHwPaused = false;
5680 }
Eric Laurentd1f69b02014-12-15 14:33:13 -08005681 }
5682 }
5683
Eric Laurent81784c32012-11-19 14:55:58 -08005684 // The first time a track is added we wait
Phil Burk99adee32014-12-10 16:46:30 -08005685 // for all its buffers to be filled before processing it.
5686 // Allow draining the buffer in case the client
5687 // app does not call stop() and relies on underrun to stop:
5688 // hence the test on (track->mRetryCount > 1).
5689 // If retryCount<=1 then track is about to underrun and be removed.
Phil Burkca5e6142015-07-14 09:42:29 -07005690 // Do not use a high threshold for compressed audio.
Eric Laurent81784c32012-11-19 14:55:58 -08005691 uint32_t minFrames;
Phil Burk99adee32014-12-10 16:46:30 -08005692 if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()
Phil Burkfdb3c072016-02-09 10:47:02 -08005693 && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) {
Eric Laurent81784c32012-11-19 14:55:58 -08005694 minFrames = mNormalFrameCount;
5695 } else {
5696 minFrames = 1;
5697 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08005698
Mikhail Naganov76e89c32019-08-15 20:18:47 -07005699 const size_t framesReady = track->framesReady();
5700 const int trackId = track->id();
5701 if (ATRACE_ENABLED()) {
5702 std::string traceName("nRdy");
5703 traceName += std::to_string(trackId);
5704 ATRACE_INT(traceName.c_str(), framesReady);
5705 }
5706 if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() &&
Eric Laurentab5cdba2014-06-09 17:22:27 -07005707 !track->isStopping_2() && !track->isStopped())
Eric Laurent81784c32012-11-19 14:55:58 -08005708 {
Mikhail Naganov76e89c32019-08-15 20:18:47 -07005709 ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer);
Eric Laurent81784c32012-11-19 14:55:58 -08005710
5711 if (track->mFillingUpStatus == Track::FS_FILLED) {
5712 track->mFillingUpStatus = Track::FS_ACTIVE;
Eric Laurent3df841a2016-07-15 15:15:40 -07005713 if (last) {
5714 // make sure processVolume_l() will apply new volume even if 0
5715 mLeftVolFloat = mRightVolFloat = -1.0;
5716 }
Eric Laurentd1f69b02014-12-15 14:33:13 -08005717 if (!mHwSupportsPause) {
5718 track->resumeAck();
Eric Laurent81784c32012-11-19 14:55:58 -08005719 }
5720 }
5721
5722 // compute volume for this track
Eric Laurentbfb1b832013-01-07 09:53:42 -08005723 processVolume_l(track, last);
5724 if (last) {
Phil Burk43b4dcc2015-06-09 16:53:44 -07005725 sp<Track> previousTrack = mPreviousTrack.promote();
5726 if (previousTrack != 0) {
5727 if (track != previousTrack.get()) {
5728 // Flush any data still being written from last track
5729 mBytesRemaining = 0;
Eric Laurent0f0631e2015-07-06 18:01:25 -07005730 // Invalidate previous track to force a seek when resuming.
5731 previousTrack->invalidate();
Phil Burk43b4dcc2015-06-09 16:53:44 -07005732 }
5733 }
5734 mPreviousTrack = track;
5735
Eric Laurentd595b7c2013-04-03 17:27:56 -07005736 // reset retry count
5737 track->mRetryCount = kMaxTrackRetriesDirect;
Eric Laurent5850c4c2016-11-10 13:04:31 -08005738 mActiveTrack = t;
Eric Laurentd595b7c2013-04-03 17:27:56 -07005739 mixerStatus = MIXER_TRACKS_READY;
Eric Laurent5cff4032015-05-26 13:49:58 -07005740 if (mHwPaused) {
Eric Laurent0f7b5f22014-12-19 10:43:21 -08005741 doHwResume = true;
5742 mHwPaused = false;
5743 }
Eric Laurentd595b7c2013-04-03 17:27:56 -07005744 }
Eric Laurent81784c32012-11-19 14:55:58 -08005745 } else {
Eric Laurentd595b7c2013-04-03 17:27:56 -07005746 // clear effect chain input buffer if the last active track started underruns
5747 // to avoid sending previous audio buffer again to effects
Eric Laurentfd477972013-10-25 18:10:40 -07005748 if (!mEffectChains.isEmpty() && last) {
Eric Laurent81784c32012-11-19 14:55:58 -08005749 mEffectChains[0]->clearInputBuffer();
5750 }
Eric Laurentab5cdba2014-06-09 17:22:27 -07005751 if (track->isStopping_1()) {
5752 track->mState = TrackBase::STOPPING_2;
Eric Laurentb369caf2015-03-30 20:51:47 -07005753 if (last && mHwPaused) {
5754 doHwResume = true;
5755 mHwPaused = false;
5756 }
Eric Laurentab5cdba2014-06-09 17:22:27 -07005757 }
5758 if ((track->sharedBuffer() != 0) || track->isStopped() ||
5759 track->isStopping_2() || track->isPaused()) {
Eric Laurent81784c32012-11-19 14:55:58 -08005760 // We have consumed all the buffers of this track.
5761 // Remove it from the list of active tracks.
Eric Laurentab5cdba2014-06-09 17:22:27 -07005762 size_t audioHALFrames;
Phil Burkfdb3c072016-02-09 10:47:02 -08005763 if (audio_has_proportional_frames(mFormat)) {
Eric Laurentab5cdba2014-06-09 17:22:27 -07005764 audioHALFrames = (latency_l() * mSampleRate) / 1000;
5765 } else {
5766 audioHALFrames = 0;
5767 }
5768
Andy Hung818e7a32016-02-16 18:08:07 -08005769 int64_t framesWritten = mBytesWritten / mFrameSize;
Eric Laurentfd477972013-10-25 18:10:40 -07005770 if (mStandby || !last ||
Aniket Kumar Lata179a0742019-01-30 14:16:16 -08005771 track->presentationComplete(framesWritten, audioHALFrames) ||
Jindong32dc26e2019-11-11 18:10:01 +08005772 track->isPaused() || mHwPaused) {
Eric Laurentab5cdba2014-06-09 17:22:27 -07005773 if (track->isStopping_2()) {
5774 track->mState = TrackBase::STOPPED;
5775 }
Eric Laurent81784c32012-11-19 14:55:58 -08005776 if (track->isStopped()) {
5777 track->reset();
5778 }
Eric Laurentd595b7c2013-04-03 17:27:56 -07005779 tracksToRemove->add(track);
Eric Laurent81784c32012-11-19 14:55:58 -08005780 }
5781 } else {
5782 // No buffers for this track. Give it a few chances to
5783 // fill a buffer, then remove it from active list.
Eric Laurentd595b7c2013-04-03 17:27:56 -07005784 // Only consider last track started for mixer state control
Eric Laurent81784c32012-11-19 14:55:58 -08005785 if (--(track->mRetryCount) <= 0) {
Mikhail Naganov76e89c32019-08-15 20:18:47 -07005786 ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", trackId);
Eric Laurentd595b7c2013-04-03 17:27:56 -07005787 tracksToRemove->add(track);
Eric Laurenta23f17a2013-11-05 18:22:08 -08005788 // indicate to client process that the track was disabled because of underrun;
5789 // it will then automatically call start() when data is available
Eric Laurent4d231dc2016-03-11 18:38:23 -08005790 track->disable();
Eric Laurentbfb1b832013-01-07 09:53:42 -08005791 } else if (last) {
Phil Burkca5e6142015-07-14 09:42:29 -07005792 ALOGW("pause because of UNDERRUN, framesReady = %zu,"
5793 "minFrames = %u, mFormat = %#x",
Mikhail Naganov76e89c32019-08-15 20:18:47 -07005794 framesReady, minFrames, mFormat);
Eric Laurent81784c32012-11-19 14:55:58 -08005795 mixerStatus = MIXER_TRACKS_ENABLED;
Eric Laurent5cff4032015-05-26 13:49:58 -07005796 if (mHwSupportsPause && !mHwPaused && !mStandby) {
Eric Laurent0f7b5f22014-12-19 10:43:21 -08005797 doHwPause = true;
5798 mHwPaused = true;
5799 }
Eric Laurent81784c32012-11-19 14:55:58 -08005800 }
5801 }
5802 }
5803 }
5804
Eric Laurentd1f69b02014-12-15 14:33:13 -08005805 // if an active track did not command a flush, check for pending flush on stopped tracks
Phil Burk43b4dcc2015-06-09 16:53:44 -07005806 if (!mFlushPending) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08005807 for (size_t i = 0; i < mTracks.size(); i++) {
5808 if (mTracks[i]->isFlushPending()) {
5809 mTracks[i]->flushAck();
Phil Burk43b4dcc2015-06-09 16:53:44 -07005810 mFlushPending = true;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005811 }
5812 }
5813 }
5814
5815 // make sure the pause/flush/resume sequence is executed in the right order.
5816 // If a flush is pending and a track is active but the HW is not paused, force a HW pause
5817 // before flush and then resume HW. This can happen in case of pause/flush/resume
5818 // if resume is received before pause is executed.
5819 if (mHwSupportsPause && !mStandby &&
Phil Burk43b4dcc2015-06-09 16:53:44 -07005820 (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07005821 status_t result = mOutput->stream->pause();
5822 ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
Eric Laurentd1f69b02014-12-15 14:33:13 -08005823 }
Phil Burk43b4dcc2015-06-09 16:53:44 -07005824 if (mFlushPending) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08005825 flushHw_l();
5826 }
5827 if (mHwSupportsPause && !mStandby && doHwResume) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07005828 status_t result = mOutput->stream->resume();
5829 ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
Eric Laurentd1f69b02014-12-15 14:33:13 -08005830 }
Eric Laurent81784c32012-11-19 14:55:58 -08005831 // remove all the tracks that need to be...
Eric Laurentbfb1b832013-01-07 09:53:42 -08005832 removeTracks_l(*tracksToRemove);
Eric Laurent81784c32012-11-19 14:55:58 -08005833
5834 return mixerStatus;
5835}
5836
5837void AudioFlinger::DirectOutputThread::threadLoop_mix()
5838{
Eric Laurent81784c32012-11-19 14:55:58 -08005839 size_t frameCount = mFrameCount;
Andy Hung2098f272014-02-27 14:00:06 -08005840 int8_t *curBuf = (int8_t *)mSinkBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -08005841 // output audio to hardware
5842 while (frameCount) {
Glenn Kasten34542ac2013-06-26 11:29:02 -07005843 AudioBufferProvider::Buffer buffer;
Eric Laurent81784c32012-11-19 14:55:58 -08005844 buffer.frameCount = frameCount;
Phil Burk062e67a2015-02-11 13:40:50 -08005845 status_t status = mActiveTrack->getNextBuffer(&buffer);
5846 if (status != NO_ERROR || buffer.raw == NULL) {
Eric Laurent51716182016-02-29 18:00:56 -08005847 // no need to pad with 0 for compressed audio
5848 if (audio_has_proportional_frames(mFormat)) {
5849 memset(curBuf, 0, frameCount * mFrameSize);
5850 }
Eric Laurent81784c32012-11-19 14:55:58 -08005851 break;
5852 }
5853 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
5854 frameCount -= buffer.frameCount;
5855 curBuf += buffer.frameCount * mFrameSize;
5856 mActiveTrack->releaseBuffer(&buffer);
5857 }
Andy Hung2098f272014-02-27 14:00:06 -08005858 mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer;
Eric Laurentad9cb8b2015-05-26 16:38:19 -07005859 mSleepTimeUs = 0;
5860 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
Eric Laurent81784c32012-11-19 14:55:58 -08005861 mActiveTrack.clear();
Eric Laurent81784c32012-11-19 14:55:58 -08005862}
5863
5864void AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
5865{
Eric Laurentd1f69b02014-12-15 14:33:13 -08005866 // do not write to HAL when paused
Eric Laurent0f7b5f22014-12-19 10:43:21 -08005867 if (mHwPaused || (usesHwAvSync() && mStandby)) {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07005868 mSleepTimeUs = mIdleSleepTimeUs;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005869 return;
5870 }
Eric Laurentad9cb8b2015-05-26 16:38:19 -07005871 if (mSleepTimeUs == 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08005872 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Eric Laurente93cc032016-05-05 10:15:10 -07005873 mSleepTimeUs = mActiveSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08005874 } else {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07005875 mSleepTimeUs = mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08005876 }
Phil Burkfdb3c072016-02-09 10:47:02 -08005877 } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) {
Andy Hung2098f272014-02-27 14:00:06 -08005878 memset(mSinkBuffer, 0, mFrameCount * mFrameSize);
Eric Laurentad9cb8b2015-05-26 16:38:19 -07005879 mSleepTimeUs = 0;
Eric Laurent81784c32012-11-19 14:55:58 -08005880 }
5881}
5882
Eric Laurentd1f69b02014-12-15 14:33:13 -08005883void AudioFlinger::DirectOutputThread::threadLoop_exit()
5884{
5885 {
5886 Mutex::Autolock _l(mLock);
Eric Laurentd1f69b02014-12-15 14:33:13 -08005887 for (size_t i = 0; i < mTracks.size(); i++) {
5888 if (mTracks[i]->isFlushPending()) {
5889 mTracks[i]->flushAck();
Phil Burk43b4dcc2015-06-09 16:53:44 -07005890 mFlushPending = true;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005891 }
5892 }
Phil Burk43b4dcc2015-06-09 16:53:44 -07005893 if (mFlushPending) {
Eric Laurentd1f69b02014-12-15 14:33:13 -08005894 flushHw_l();
5895 }
5896 }
5897 PlaybackThread::threadLoop_exit();
5898}
5899
5900// must be called with thread mutex locked
5901bool AudioFlinger::DirectOutputThread::shouldStandby_l()
5902{
5903 bool trackPaused = false;
Eric Laurentb369caf2015-03-30 20:51:47 -07005904 bool trackStopped = false;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005905
vivek mehta9cd7ad12016-03-17 00:18:29 -07005906 if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) {
5907 return !mStandby;
5908 }
5909
Eric Laurentd1f69b02014-12-15 14:33:13 -08005910 // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack
5911 // after a timeout and we will enter standby then.
5912 if (mTracks.size() > 0) {
5913 trackPaused = mTracks[mTracks.size() - 1]->isPaused();
Eric Laurentb369caf2015-03-30 20:51:47 -07005914 trackStopped = mTracks[mTracks.size() - 1]->isStopped() ||
5915 mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE;
Eric Laurentd1f69b02014-12-15 14:33:13 -08005916 }
5917
Eric Laurent5cff4032015-05-26 13:49:58 -07005918 return !mStandby && !(trackPaused || (mHwPaused && !trackStopped));
Eric Laurentd1f69b02014-12-15 14:33:13 -08005919}
5920
Eric Laurent10351942014-05-08 18:49:52 -07005921// checkForNewParameter_l() must be called with ThreadBase::mLock held
5922bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair,
5923 status_t& status)
Eric Laurent81784c32012-11-19 14:55:58 -08005924{
5925 bool reconfig = false;
Eric Laurent42537be2016-01-08 17:16:42 -08005926 bool a2dpDeviceChanged = false;
Eric Laurent81784c32012-11-19 14:55:58 -08005927
Eric Laurent10351942014-05-08 18:49:52 -07005928 status = NO_ERROR;
Eric Laurent81784c32012-11-19 14:55:58 -08005929
Eric Laurent10351942014-05-08 18:49:52 -07005930 AudioParameter param = AudioParameter(keyValuePair);
5931 int value;
5932 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
jiabin10d86fd2019-10-31 17:20:42 -07005933 LOG_FATAL("Should not set routing device in DirectOutputThread");
Eric Laurent81784c32012-11-19 14:55:58 -08005934 }
Eric Laurent10351942014-05-08 18:49:52 -07005935 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5936 // do not accept frame count changes if tracks are open as the track buffer
5937 // size depends on frame count and correct behavior would not be garantied
5938 // if frame count is changed after track creation
5939 if (!mTracks.isEmpty()) {
5940 status = INVALID_OPERATION;
5941 } else {
5942 reconfig = true;
5943 }
5944 }
5945 if (status == NO_ERROR) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07005946 status = mOutput->stream->setParameters(keyValuePair);
Eric Laurent10351942014-05-08 18:49:52 -07005947 if (!mStandby && status == INVALID_OPERATION) {
Phil Burk062e67a2015-02-11 13:40:50 -08005948 mOutput->standby();
Eric Laurent10351942014-05-08 18:49:52 -07005949 mStandby = true;
5950 mBytesWritten = 0;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07005951 status = mOutput->stream->setParameters(keyValuePair);
Eric Laurent10351942014-05-08 18:49:52 -07005952 }
5953 if (status == NO_ERROR && reconfig) {
5954 readOutputParameters_l();
Eric Laurent73e26b62015-04-27 16:55:58 -07005955 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
Eric Laurent10351942014-05-08 18:49:52 -07005956 }
5957 }
5958
Eric Laurent42537be2016-01-08 17:16:42 -08005959 return reconfig || a2dpDeviceChanged;
Eric Laurent81784c32012-11-19 14:55:58 -08005960}
5961
5962uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const
5963{
5964 uint32_t time;
Phil Burkfdb3c072016-02-09 10:47:02 -08005965 if (audio_has_proportional_frames(mFormat)) {
Eric Laurent81784c32012-11-19 14:55:58 -08005966 time = PlaybackThread::activeSleepTimeUs();
5967 } else {
Eric Laurent51716182016-02-29 18:00:56 -08005968 time = kDirectMinSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08005969 }
5970 return time;
5971}
5972
5973uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const
5974{
5975 uint32_t time;
Phil Burkfdb3c072016-02-09 10:47:02 -08005976 if (audio_has_proportional_frames(mFormat)) {
Eric Laurent81784c32012-11-19 14:55:58 -08005977 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
5978 } else {
Eric Laurent51716182016-02-29 18:00:56 -08005979 time = kDirectMinSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08005980 }
5981 return time;
5982}
5983
5984uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const
5985{
5986 uint32_t time;
Phil Burkfdb3c072016-02-09 10:47:02 -08005987 if (audio_has_proportional_frames(mFormat)) {
Eric Laurent81784c32012-11-19 14:55:58 -08005988 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
5989 } else {
Eric Laurent51716182016-02-29 18:00:56 -08005990 time = kDirectMinSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08005991 }
5992 return time;
5993}
5994
5995void AudioFlinger::DirectOutputThread::cacheParameters_l()
5996{
5997 PlaybackThread::cacheParameters_l();
5998
5999 // use shorter standby delay as on normal output to release
6000 // hardware resources as soon as possible
Eric Laurentb369caf2015-03-30 20:51:47 -07006001 // no delay on outputs with HW A/V sync
6002 if (usesHwAvSync()) {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006003 mStandbyDelayNs = 0;
Phil Burkfdb3c072016-02-09 10:47:02 -08006004 } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006005 mStandbyDelayNs = kOffloadStandbyDelayNs;
Eric Laurent5cff4032015-05-26 13:49:58 -07006006 } else {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006007 mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2);
Eric Laurent972a1732013-09-04 09:42:59 -07006008 }
Eric Laurent81784c32012-11-19 14:55:58 -08006009}
6010
Eric Laurente659ef42014-09-29 13:06:46 -07006011void AudioFlinger::DirectOutputThread::flushHw_l()
6012{
Phil Burk062e67a2015-02-11 13:40:50 -08006013 mOutput->flush();
Eric Laurentd1f69b02014-12-15 14:33:13 -08006014 mHwPaused = false;
Phil Burk43b4dcc2015-06-09 16:53:44 -07006015 mFlushPending = false;
Andy Hungf3234512018-07-03 14:51:47 -07006016 mTimestampVerifier.discontinuity(); // DIRECT and OFFLOADED flush resets frame count.
Sampath Shetty999f0e82020-01-15 10:19:06 +11006017 mTimestamp.clear();
Eric Laurente659ef42014-09-29 13:06:46 -07006018}
6019
Andy Hung10cbff12017-02-21 17:30:14 -08006020int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const {
6021 // If a VolumeShaper is active, we must wake up periodically to update volume.
6022 const int64_t NS_PER_MS = 1000000;
6023 return mVolumeShaperActive ?
6024 kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l();
6025}
6026
Eric Laurent81784c32012-11-19 14:55:58 -08006027// ----------------------------------------------------------------------------
6028
Eric Laurentbfb1b832013-01-07 09:53:42 -08006029AudioFlinger::AsyncCallbackThread::AsyncCallbackThread(
Eric Laurent4de95592013-09-26 15:28:21 -07006030 const wp<AudioFlinger::PlaybackThread>& playbackThread)
Eric Laurentbfb1b832013-01-07 09:53:42 -08006031 : Thread(false /*canCallJava*/),
Eric Laurent4de95592013-09-26 15:28:21 -07006032 mPlaybackThread(playbackThread),
Eric Laurent3b4529e2013-09-05 18:09:19 -07006033 mWriteAckSequence(0),
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07006034 mDrainSequence(0),
6035 mAsyncError(false)
Eric Laurentbfb1b832013-01-07 09:53:42 -08006036{
6037}
6038
6039AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread()
6040{
6041}
6042
6043void AudioFlinger::AsyncCallbackThread::onFirstRef()
6044{
6045 run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO);
6046}
6047
6048bool AudioFlinger::AsyncCallbackThread::threadLoop()
6049{
6050 while (!exitPending()) {
Eric Laurent3b4529e2013-09-05 18:09:19 -07006051 uint32_t writeAckSequence;
6052 uint32_t drainSequence;
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07006053 bool asyncError;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006054
6055 {
6056 Mutex::Autolock _l(mLock);
Haynes Mathew George24a325d2013-12-03 21:26:02 -08006057 while (!((mWriteAckSequence & 1) ||
6058 (mDrainSequence & 1) ||
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07006059 mAsyncError ||
Haynes Mathew George24a325d2013-12-03 21:26:02 -08006060 exitPending())) {
6061 mWaitWorkCV.wait(mLock);
6062 }
6063
Eric Laurentbfb1b832013-01-07 09:53:42 -08006064 if (exitPending()) {
6065 break;
6066 }
Eric Laurent3b4529e2013-09-05 18:09:19 -07006067 ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d",
6068 mWriteAckSequence, mDrainSequence);
6069 writeAckSequence = mWriteAckSequence;
6070 mWriteAckSequence &= ~1;
6071 drainSequence = mDrainSequence;
6072 mDrainSequence &= ~1;
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07006073 asyncError = mAsyncError;
6074 mAsyncError = false;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006075 }
6076 {
Eric Laurent4de95592013-09-26 15:28:21 -07006077 sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote();
6078 if (playbackThread != 0) {
Eric Laurent3b4529e2013-09-05 18:09:19 -07006079 if (writeAckSequence & 1) {
Eric Laurent4de95592013-09-26 15:28:21 -07006080 playbackThread->resetWriteBlocked(writeAckSequence >> 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -08006081 }
Eric Laurent3b4529e2013-09-05 18:09:19 -07006082 if (drainSequence & 1) {
Eric Laurent4de95592013-09-26 15:28:21 -07006083 playbackThread->resetDraining(drainSequence >> 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -08006084 }
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07006085 if (asyncError) {
6086 playbackThread->onAsyncError();
6087 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006088 }
6089 }
6090 }
6091 return false;
6092}
6093
6094void AudioFlinger::AsyncCallbackThread::exit()
6095{
6096 ALOGV("AsyncCallbackThread::exit");
6097 Mutex::Autolock _l(mLock);
6098 requestExit();
6099 mWaitWorkCV.broadcast();
6100}
6101
Eric Laurent3b4529e2013-09-05 18:09:19 -07006102void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence)
Eric Laurentbfb1b832013-01-07 09:53:42 -08006103{
6104 Mutex::Autolock _l(mLock);
Eric Laurent3b4529e2013-09-05 18:09:19 -07006105 // bit 0 is cleared
6106 mWriteAckSequence = sequence << 1;
6107}
6108
6109void AudioFlinger::AsyncCallbackThread::resetWriteBlocked()
6110{
6111 Mutex::Autolock _l(mLock);
6112 // ignore unexpected callbacks
6113 if (mWriteAckSequence & 2) {
6114 mWriteAckSequence |= 1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006115 mWaitWorkCV.signal();
6116 }
6117}
6118
Eric Laurent3b4529e2013-09-05 18:09:19 -07006119void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence)
Eric Laurentbfb1b832013-01-07 09:53:42 -08006120{
6121 Mutex::Autolock _l(mLock);
Eric Laurent3b4529e2013-09-05 18:09:19 -07006122 // bit 0 is cleared
6123 mDrainSequence = sequence << 1;
6124}
6125
6126void AudioFlinger::AsyncCallbackThread::resetDraining()
6127{
6128 Mutex::Autolock _l(mLock);
6129 // ignore unexpected callbacks
6130 if (mDrainSequence & 2) {
6131 mDrainSequence |= 1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006132 mWaitWorkCV.signal();
6133 }
6134}
6135
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07006136void AudioFlinger::AsyncCallbackThread::setAsyncError()
6137{
6138 Mutex::Autolock _l(mLock);
6139 mAsyncError = true;
6140 mWaitWorkCV.signal();
6141}
6142
Eric Laurentbfb1b832013-01-07 09:53:42 -08006143
6144// ----------------------------------------------------------------------------
6145AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger,
jiabin10d86fd2019-10-31 17:20:42 -07006146 AudioStreamOut* output, audio_io_handle_t id, bool systemReady)
6147 : DirectOutputThread(audioFlinger, output, id, OFFLOAD, systemReady),
Andy Hungf8044752016-07-27 14:58:11 -07006148 mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true),
6149 mOffloadUnderrunPosition(~0LL)
Eric Laurentbfb1b832013-01-07 09:53:42 -08006150{
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07006151 //FIXME: mStandby should be set to true by ThreadBase constructo
Eric Laurentfd477972013-10-25 18:10:40 -07006152 mStandby = true;
Eric Laurent64667972016-03-30 18:19:46 -07006153 mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */);
Eric Laurentbfb1b832013-01-07 09:53:42 -08006154}
6155
Eric Laurentbfb1b832013-01-07 09:53:42 -08006156void AudioFlinger::OffloadThread::threadLoop_exit()
6157{
6158 if (mFlushPending || mHwPaused) {
6159 // If a flush is pending or track was paused, just discard buffered data
6160 flushHw_l();
6161 } else {
6162 mMixerStatus = MIXER_DRAIN_ALL;
6163 threadLoop_drain();
6164 }
Uday Gupta56604aa2014-05-13 11:19:17 -07006165 if (mUseAsyncWrite) {
6166 ALOG_ASSERT(mCallbackThread != 0);
6167 mCallbackThread->exit();
6168 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006169 PlaybackThread::threadLoop_exit();
6170}
6171
6172AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l(
6173 Vector< sp<Track> > *tracksToRemove
6174)
6175{
Eric Laurentbfb1b832013-01-07 09:53:42 -08006176 size_t count = mActiveTracks.size();
6177
6178 mixer_state mixerStatus = MIXER_IDLE;
Eric Laurent972a1732013-09-04 09:42:59 -07006179 bool doHwPause = false;
6180 bool doHwResume = false;
6181
Glenn Kastenc42e9b42016-03-21 11:35:03 -07006182 ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count);
Eric Laurentede6c3b2013-09-19 14:37:46 -07006183
Eric Laurentbfb1b832013-01-07 09:53:42 -08006184 // find out which tracks need to be processed
Andy Hungdae27702016-10-31 14:01:16 -07006185 for (const sp<Track> &t : mActiveTracks) {
Eric Laurent5850c4c2016-11-10 13:04:31 -08006186 Track* const track = t.get();
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07006187#ifdef VERY_VERY_VERBOSE_LOGGING
Eric Laurentbfb1b832013-01-07 09:53:42 -08006188 audio_track_cblk_t* cblk = track->cblk();
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07006189#endif
Eric Laurentfd477972013-10-25 18:10:40 -07006190 // Only consider last track started for volume and mixer state control.
6191 // In theory an older track could underrun and restart after the new one starts
6192 // but as we only care about the transition phase between two tracks on a
6193 // direct output, it is not a problem to ignore the underrun case.
Andy Hungdae27702016-10-31 14:01:16 -07006194 sp<Track> l = mActiveTracks.getLatest();
Eric Laurent5850c4c2016-11-10 13:04:31 -08006195 bool last = l.get() == track;
Eric Laurentfd477972013-10-25 18:10:40 -07006196
Haynes Mathew George7844f672014-01-15 12:32:55 -08006197 if (track->isInvalid()) {
6198 ALOGW("An invalidated track shouldn't be in active list");
6199 tracksToRemove->add(track);
6200 continue;
6201 }
6202
6203 if (track->mState == TrackBase::IDLE) {
6204 ALOGW("An idle track shouldn't be in active list");
6205 continue;
6206 }
6207
Eric Laurentbfb1b832013-01-07 09:53:42 -08006208 if (track->isPausing()) {
6209 track->setPaused();
6210 if (last) {
Eric Laurent5cff4032015-05-26 13:49:58 -07006211 if (mHwSupportsPause && !mHwPaused) {
Eric Laurent972a1732013-09-04 09:42:59 -07006212 doHwPause = true;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006213 mHwPaused = true;
6214 }
6215 // If we were part way through writing the mixbuffer to
6216 // the HAL we must save this until we resume
6217 // BUG - this will be wrong if a different track is made active,
6218 // in that case we want to discard the pending data in the
6219 // mixbuffer and tell the client to present it again when the
6220 // track is resumed
6221 mPausedWriteLength = mCurrentWriteLength;
6222 mPausedBytesRemaining = mBytesRemaining;
6223 mBytesRemaining = 0; // stop writing
6224 }
6225 tracksToRemove->add(track);
Haynes Mathew George7844f672014-01-15 12:32:55 -08006226 } else if (track->isFlushPending()) {
Eric Laurente93cc032016-05-05 10:15:10 -07006227 if (track->isStopping_1()) {
6228 track->mRetryCount = kMaxTrackStopRetriesOffload;
6229 } else {
6230 track->mRetryCount = kMaxTrackRetriesOffload;
6231 }
Haynes Mathew George7844f672014-01-15 12:32:55 -08006232 track->flushAck();
6233 if (last) {
6234 mFlushPending = true;
6235 }
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08006236 } else if (track->isResumePending()){
6237 track->resumeAck();
6238 if (last) {
6239 if (mPausedBytesRemaining) {
6240 // Need to continue write that was interrupted
6241 mCurrentWriteLength = mPausedWriteLength;
6242 mBytesRemaining = mPausedBytesRemaining;
6243 mPausedBytesRemaining = 0;
6244 }
6245 if (mHwPaused) {
6246 doHwResume = true;
6247 mHwPaused = false;
6248 // threadLoop_mix() will handle the case that we need to
6249 // resume an interrupted write
6250 }
6251 // enable write to audio HAL
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006252 mSleepTimeUs = 0;
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08006253
Eric Laurent3df841a2016-07-15 15:15:40 -07006254 mLeftVolFloat = mRightVolFloat = -1.0;
6255
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08006256 // Do not handle new data in this iteration even if track->framesReady()
6257 mixerStatus = MIXER_TRACKS_ENABLED;
6258 }
6259 } else if (track->framesReady() && track->isReady() &&
Eric Laurent3b4529e2013-09-05 18:09:19 -07006260 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) {
Andy Hungc0691382018-09-12 18:01:57 -07006261 ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer);
Eric Laurentbfb1b832013-01-07 09:53:42 -08006262 if (track->mFillingUpStatus == Track::FS_FILLED) {
6263 track->mFillingUpStatus = Track::FS_ACTIVE;
Eric Laurent3df841a2016-07-15 15:15:40 -07006264 if (last) {
6265 // make sure processVolume_l() will apply new volume even if 0
6266 mLeftVolFloat = mRightVolFloat = -1.0;
6267 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006268 }
6269
6270 if (last) {
Eric Laurentd7e59222013-11-15 12:02:28 -08006271 sp<Track> previousTrack = mPreviousTrack.promote();
6272 if (previousTrack != 0) {
6273 if (track != previousTrack.get()) {
Eric Laurent9da3d952013-11-12 19:25:43 -08006274 // Flush any data still being written from last track
6275 mBytesRemaining = 0;
6276 if (mPausedBytesRemaining) {
6277 // Last track was paused so we also need to flush saved
6278 // mixbuffer state and invalidate track so that it will
6279 // re-submit that unwritten data when it is next resumed
6280 mPausedBytesRemaining = 0;
6281 // Invalidate is a bit drastic - would be more efficient
6282 // to have a flag to tell client that some of the
6283 // previously written data was lost
Eric Laurentd7e59222013-11-15 12:02:28 -08006284 previousTrack->invalidate();
Eric Laurent9da3d952013-11-12 19:25:43 -08006285 }
6286 // flush data already sent to the DSP if changing audio session as audio
6287 // comes from a different source. Also invalidate previous track to force a
6288 // seek when resuming.
Eric Laurentd7e59222013-11-15 12:02:28 -08006289 if (previousTrack->sessionId() != track->sessionId()) {
6290 previousTrack->invalidate();
Eric Laurent9da3d952013-11-12 19:25:43 -08006291 }
6292 }
6293 }
6294 mPreviousTrack = track;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006295 // reset retry count
Eric Laurente93cc032016-05-05 10:15:10 -07006296 if (track->isStopping_1()) {
6297 track->mRetryCount = kMaxTrackStopRetriesOffload;
6298 } else {
6299 track->mRetryCount = kMaxTrackRetriesOffload;
6300 }
Eric Laurent5850c4c2016-11-10 13:04:31 -08006301 mActiveTrack = t;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006302 mixerStatus = MIXER_TRACKS_READY;
6303 }
6304 } else {
Andy Hungc0691382018-09-12 18:01:57 -07006305 ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer);
Eric Laurentbfb1b832013-01-07 09:53:42 -08006306 if (track->isStopping_1()) {
Eric Laurente93cc032016-05-05 10:15:10 -07006307 if (--(track->mRetryCount) <= 0) {
6308 // Hardware buffer can hold a large amount of audio so we must
6309 // wait for all current track's data to drain before we say
6310 // that the track is stopped.
6311 if (mBytesRemaining == 0) {
6312 // Only start draining when all data in mixbuffer
6313 // has been written
6314 ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2");
6315 track->mState = TrackBase::STOPPING_2; // so presentation completes after
6316 // drain do not drain if no data was ever sent to HAL (mStandby == true)
6317 if (last && !mStandby) {
6318 // do not modify drain sequence if we are already draining. This happens
6319 // when resuming from pause after drain.
6320 if ((mDrainSequence & 1) == 0) {
6321 mSleepTimeUs = 0;
6322 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
6323 mixerStatus = MIXER_DRAIN_TRACK;
6324 mDrainSequence += 2;
6325 }
6326 if (mHwPaused) {
6327 // It is possible to move from PAUSED to STOPPING_1 without
6328 // a resume so we must ensure hardware is running
6329 doHwResume = true;
6330 mHwPaused = false;
6331 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006332 }
6333 }
Eric Laurente93cc032016-05-05 10:15:10 -07006334 } else if (last) {
6335 ALOGV("stopping1 underrun retries left %d", track->mRetryCount);
6336 mixerStatus = MIXER_TRACKS_ENABLED;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006337 }
6338 } else if (track->isStopping_2()) {
Eric Laurent6a51d7e2013-10-17 18:59:26 -07006339 // Drain has completed or we are in standby, signal presentation complete
6340 if (!(mDrainSequence & 1) || !last || mStandby) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08006341 track->mState = TrackBase::STOPPED;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07006342 uint32_t latency = 0;
6343 status_t result = mOutput->stream->getLatency(&latency);
6344 ALOGE_IF(result != OK,
6345 "Error when retrieving output stream latency: %d", result);
6346 size_t audioHALFrames = (latency * mSampleRate) / 1000;
Andy Hung818e7a32016-02-16 18:08:07 -08006347 int64_t framesWritten =
Phil Burk062e67a2015-02-11 13:40:50 -08006348 mBytesWritten / mOutput->getFrameSize();
Eric Laurentbfb1b832013-01-07 09:53:42 -08006349 track->presentationComplete(framesWritten, audioHALFrames);
6350 track->reset();
6351 tracksToRemove->add(track);
Andy Hungf3234512018-07-03 14:51:47 -07006352 // DIRECT and OFFLOADED stop resets frame counts.
6353 if (!mUseAsyncWrite) {
6354 // If we don't get explicit drain notification we must
6355 // register discontinuity regardless of whether this is
6356 // the previous (!last) or the upcoming (last) track
6357 // to avoid skipping the discontinuity.
6358 mTimestampVerifier.discontinuity();
6359 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006360 }
6361 } else {
6362 // No buffers for this track. Give it a few chances to
6363 // fill a buffer, then remove it from active list.
6364 if (--(track->mRetryCount) <= 0) {
Andy Hungf8044752016-07-27 14:58:11 -07006365 bool running = false;
Mikhail Naganov1dc98672016-08-18 17:50:29 -07006366 uint64_t position = 0;
6367 struct timespec unused;
6368 // The running check restarts the retry counter at least once.
6369 status_t ret = mOutput->stream->getPresentationPosition(&position, &unused);
6370 if (ret == NO_ERROR && position != mOffloadUnderrunPosition) {
6371 running = true;
6372 mOffloadUnderrunPosition = position;
6373 }
6374 if (ret == NO_ERROR) {
Andy Hungf8044752016-07-27 14:58:11 -07006375 ALOGVV("underrun counter, running(%d): %lld vs %lld", running,
6376 (long long)position, (long long)mOffloadUnderrunPosition);
6377 }
6378 if (running) { // still running, give us more time.
6379 track->mRetryCount = kMaxTrackRetriesOffload;
6380 } else {
Andy Hungc0691382018-09-12 18:01:57 -07006381 ALOGV("OffloadThread: BUFFER TIMEOUT: remove track(%d) from active list",
6382 track->id());
Andy Hungf8044752016-07-27 14:58:11 -07006383 tracksToRemove->add(track);
Glenn Kastend3bb6452016-12-05 18:14:37 -08006384 // tell client process that the track was disabled because of underrun;
Andy Hungf8044752016-07-27 14:58:11 -07006385 // it will then automatically call start() when data is available
6386 track->disable();
6387 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006388 } else if (last){
6389 mixerStatus = MIXER_TRACKS_ENABLED;
6390 }
6391 }
6392 }
6393 // compute volume for this track
Wenfeng Sun79458332019-05-29 20:12:43 +08006394 if (track->isReady()) { // check ready to prevent premature start.
6395 processVolume_l(track, last);
6396 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08006397 }
Eric Laurent6bf9ae22013-08-30 15:12:37 -07006398
Eric Laurentea0fade2013-10-04 16:23:48 -07006399 // make sure the pause/flush/resume sequence is executed in the right order.
6400 // If a flush is pending and a track is active but the HW is not paused, force a HW pause
6401 // before flush and then resume HW. This can happen in case of pause/flush/resume
6402 // if resume is received before pause is executed.
Eric Laurentfd477972013-10-25 18:10:40 -07006403 if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07006404 status_t result = mOutput->stream->pause();
6405 ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
Eric Laurent972a1732013-09-04 09:42:59 -07006406 }
Eric Laurent6bf9ae22013-08-30 15:12:37 -07006407 if (mFlushPending) {
6408 flushHw_l();
Eric Laurent6bf9ae22013-08-30 15:12:37 -07006409 }
Eric Laurentfd477972013-10-25 18:10:40 -07006410 if (!mStandby && doHwResume) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07006411 status_t result = mOutput->stream->resume();
6412 ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
Eric Laurent972a1732013-09-04 09:42:59 -07006413 }
Eric Laurent6bf9ae22013-08-30 15:12:37 -07006414
Eric Laurentbfb1b832013-01-07 09:53:42 -08006415 // remove all the tracks that need to be...
6416 removeTracks_l(*tracksToRemove);
6417
6418 return mixerStatus;
6419}
6420
Eric Laurentbfb1b832013-01-07 09:53:42 -08006421// must be called with thread mutex locked
6422bool AudioFlinger::OffloadThread::waitingAsyncCallback_l()
6423{
Eric Laurent3b4529e2013-09-05 18:09:19 -07006424 ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d",
6425 mWriteAckSequence, mDrainSequence);
6426 if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08006427 return true;
6428 }
6429 return false;
6430}
6431
Eric Laurentbfb1b832013-01-07 09:53:42 -08006432bool AudioFlinger::OffloadThread::waitingAsyncCallback()
6433{
6434 Mutex::Autolock _l(mLock);
6435 return waitingAsyncCallback_l();
6436}
6437
6438void AudioFlinger::OffloadThread::flushHw_l()
6439{
Eric Laurente659ef42014-09-29 13:06:46 -07006440 DirectOutputThread::flushHw_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08006441 // Flush anything still waiting in the mixbuffer
6442 mCurrentWriteLength = 0;
6443 mBytesRemaining = 0;
6444 mPausedWriteLength = 0;
6445 mPausedBytesRemaining = 0;
Eric Laurent3eaf66b2016-04-01 14:44:17 -07006446 // reset bytes written count to reflect that DSP buffers are empty after flush.
6447 mBytesWritten = 0;
Andy Hungf8044752016-07-27 14:58:11 -07006448 mOffloadUnderrunPosition = ~0LL;
Haynes Mathew George0f02f262014-01-11 13:03:57 -08006449
Eric Laurentbfb1b832013-01-07 09:53:42 -08006450 if (mUseAsyncWrite) {
Eric Laurent3b4529e2013-09-05 18:09:19 -07006451 // discard any pending drain or write ack by incrementing sequence
6452 mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
6453 mDrainSequence = (mDrainSequence + 2) & ~1;
Eric Laurentbfb1b832013-01-07 09:53:42 -08006454 ALOG_ASSERT(mCallbackThread != 0);
Eric Laurent3b4529e2013-09-05 18:09:19 -07006455 mCallbackThread->setWriteBlocked(mWriteAckSequence);
6456 mCallbackThread->setDraining(mDrainSequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -08006457 }
6458}
6459
Haynes Mathew George05317d22016-05-03 16:34:26 -07006460void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType)
6461{
6462 Mutex::Autolock _l(mLock);
Eric Laurent13084622016-05-17 10:51:49 -07006463 if (PlaybackThread::invalidateTracks_l(streamType)) {
6464 mFlushPending = true;
6465 }
Haynes Mathew George05317d22016-05-03 16:34:26 -07006466}
6467
Eric Laurentbfb1b832013-01-07 09:53:42 -08006468// ----------------------------------------------------------------------------
6469
Eric Laurent81784c32012-11-19 14:55:58 -08006470AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
Eric Laurent72e3f392015-05-20 14:43:50 -07006471 AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady)
jiabin10d86fd2019-10-31 17:20:42 -07006472 : MixerThread(audioFlinger, mainThread->getOutput(), id,
Eric Laurent72e3f392015-05-20 14:43:50 -07006473 systemReady, DUPLICATING),
Eric Laurent81784c32012-11-19 14:55:58 -08006474 mWaitTimeMs(UINT_MAX)
6475{
6476 addOutputTrack(mainThread);
6477}
6478
6479AudioFlinger::DuplicatingThread::~DuplicatingThread()
6480{
6481 for (size_t i = 0; i < mOutputTracks.size(); i++) {
6482 mOutputTracks[i]->destroy();
6483 }
6484}
6485
6486void AudioFlinger::DuplicatingThread::threadLoop_mix()
6487{
6488 // mix buffers...
6489 if (outputsReady(outputTracks)) {
Glenn Kastend79072e2016-01-06 08:41:20 -08006490 mAudioMixer->process();
Eric Laurent81784c32012-11-19 14:55:58 -08006491 } else {
Eric Laurent02b57082014-11-07 17:28:28 -08006492 if (mMixerBufferValid) {
6493 memset(mMixerBuffer, 0, mMixerBufferSize);
6494 } else {
6495 memset(mSinkBuffer, 0, mSinkBufferSize);
6496 }
Eric Laurent81784c32012-11-19 14:55:58 -08006497 }
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006498 mSleepTimeUs = 0;
Eric Laurent81784c32012-11-19 14:55:58 -08006499 writeFrames = mNormalFrameCount;
Andy Hung25c2dac2014-02-27 14:56:00 -08006500 mCurrentWriteLength = mSinkBufferSize;
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006501 mStandbyTimeNs = systemTime() + mStandbyDelayNs;
Eric Laurent81784c32012-11-19 14:55:58 -08006502}
6503
6504void AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
6505{
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006506 if (mSleepTimeUs == 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08006507 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006508 mSleepTimeUs = mActiveSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08006509 } else {
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006510 mSleepTimeUs = mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08006511 }
6512 } else if (mBytesWritten != 0) {
6513 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
6514 writeFrames = mNormalFrameCount;
Andy Hung25c2dac2014-02-27 14:56:00 -08006515 memset(mSinkBuffer, 0, mSinkBufferSize);
Eric Laurent81784c32012-11-19 14:55:58 -08006516 } else {
6517 // flush remaining overflow buffers in output tracks
6518 writeFrames = 0;
6519 }
Eric Laurentad9cb8b2015-05-26 16:38:19 -07006520 mSleepTimeUs = 0;
Eric Laurent81784c32012-11-19 14:55:58 -08006521 }
6522}
6523
Eric Laurentbfb1b832013-01-07 09:53:42 -08006524ssize_t AudioFlinger::DuplicatingThread::threadLoop_write()
Eric Laurent81784c32012-11-19 14:55:58 -08006525{
6526 for (size_t i = 0; i < outputTracks.size(); i++) {
Andy Hung1c86ebe2018-05-29 20:29:08 -07006527 const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames);
6528
6529 // Consider the first OutputTrack for timestamp and frame counting.
6530
6531 // The threadLoop() generally assumes writing a full sink buffer size at a time.
6532 // Here, we correct for writeFrames of 0 (a stop) or underruns because
6533 // we always claim success.
6534 if (i == 0) {
6535 const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten;
6536 ALOGD_IF(correction != 0 && writeFrames != 0,
6537 "%s: writeFrames:%u actualWritten:%zd correction:%zd mFramesWritten:%lld",
6538 __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten);
6539 mFramesWritten -= correction;
6540 }
6541
6542 // TODO: Report correction for the other output tracks and show in the dump.
Eric Laurent81784c32012-11-19 14:55:58 -08006543 }
Eric Laurent2c3740f2013-10-30 16:57:06 -07006544 mStandby = false;
Andy Hung25c2dac2014-02-27 14:56:00 -08006545 return (ssize_t)mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -08006546}
6547
6548void AudioFlinger::DuplicatingThread::threadLoop_standby()
6549{
6550 // DuplicatingThread implements standby by stopping all tracks
6551 for (size_t i = 0; i < outputTracks.size(); i++) {
6552 outputTracks[i]->stop();
6553 }
6554}
6555
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07006556void AudioFlinger::DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
Andy Hung1bc088a2018-02-09 15:57:31 -08006557{
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07006558 MixerThread::dumpInternals_l(fd, args);
Andy Hung1bc088a2018-02-09 15:57:31 -08006559
6560 std::stringstream ss;
6561 const size_t numTracks = mOutputTracks.size();
6562 ss << " " << numTracks << " OutputTracks";
6563 if (numTracks > 0) {
6564 ss << ":";
6565 for (const auto &track : mOutputTracks) {
6566 const sp<ThreadBase> thread = track->thread().promote();
Andy Hungc0691382018-09-12 18:01:57 -07006567 ss << " (" << track->id() << " : ";
Andy Hung1bc088a2018-02-09 15:57:31 -08006568 if (thread.get() != nullptr) {
6569 ss << thread.get() << ", " << thread->id();
6570 } else {
6571 ss << "null";
6572 }
6573 ss << ")";
6574 }
6575 }
6576 ss << "\n";
6577 std::string result = ss.str();
6578 write(fd, result.c_str(), result.size());
6579}
6580
Eric Laurent81784c32012-11-19 14:55:58 -08006581void AudioFlinger::DuplicatingThread::saveOutputTracks()
6582{
6583 outputTracks = mOutputTracks;
6584}
6585
6586void AudioFlinger::DuplicatingThread::clearOutputTracks()
6587{
6588 outputTracks.clear();
6589}
6590
6591void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
6592{
6593 Mutex::Autolock _l(mLock);
Andy Hungc25b84a2015-01-14 19:04:10 -08006594 // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass.
6595 // Adjust for thread->sampleRate() to determine minimum buffer frame count.
6596 // Then triple buffer because Threads do not run synchronously and may not be clock locked.
6597 const size_t frameCount =
6598 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate());
6599 // TODO: Consider asynchronous sample rate conversion to handle clock disparity
6600 // from different OutputTracks and their associated MixerThreads (e.g. one may
6601 // nearly empty and the other may be dropping data).
6602
6603 sp<OutputTrack> outputTrack = new OutputTrack(thread,
Eric Laurent81784c32012-11-19 14:55:58 -08006604 this,
6605 mSampleRate,
Andy Hungc25b84a2015-01-14 19:04:10 -08006606 mFormat,
Eric Laurent81784c32012-11-19 14:55:58 -08006607 mChannelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08006608 frameCount,
6609 IPCThreadState::self()->getCallingUid());
Eric Laurentaf3ec7c2016-08-01 11:25:19 -07006610 status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY;
6611 if (status != NO_ERROR) {
6612 ALOGE("addOutputTrack() initCheck failed %d", status);
6613 return;
Eric Laurent81784c32012-11-19 14:55:58 -08006614 }
Eric Laurentaf3ec7c2016-08-01 11:25:19 -07006615 thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
6616 mOutputTracks.add(outputTrack);
6617 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
6618 updateWaitTime_l();
Eric Laurent81784c32012-11-19 14:55:58 -08006619}
6620
6621void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
6622{
6623 Mutex::Autolock _l(mLock);
6624 for (size_t i = 0; i < mOutputTracks.size(); i++) {
6625 if (mOutputTracks[i]->thread() == thread) {
6626 mOutputTracks[i]->destroy();
6627 mOutputTracks.removeAt(i);
6628 updateWaitTime_l();
Eric Laurentf6870ae2015-05-08 10:50:03 -07006629 if (thread->getOutput() == mOutput) {
6630 mOutput = NULL;
6631 }
Eric Laurent81784c32012-11-19 14:55:58 -08006632 return;
6633 }
6634 }
Eric Laurentf6870ae2015-05-08 10:50:03 -07006635 ALOGV("removeOutputTrack(): unknown thread: %p", thread);
Eric Laurent81784c32012-11-19 14:55:58 -08006636}
6637
6638// caller must hold mLock
6639void AudioFlinger::DuplicatingThread::updateWaitTime_l()
6640{
6641 mWaitTimeMs = UINT_MAX;
6642 for (size_t i = 0; i < mOutputTracks.size(); i++) {
6643 sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
6644 if (strong != 0) {
6645 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
6646 if (waitTimeMs < mWaitTimeMs) {
6647 mWaitTimeMs = waitTimeMs;
6648 }
6649 }
6650 }
6651}
6652
6653
6654bool AudioFlinger::DuplicatingThread::outputsReady(
6655 const SortedVector< sp<OutputTrack> > &outputTracks)
6656{
6657 for (size_t i = 0; i < outputTracks.size(); i++) {
6658 sp<ThreadBase> thread = outputTracks[i]->thread().promote();
6659 if (thread == 0) {
6660 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p",
6661 outputTracks[i].get());
6662 return false;
6663 }
6664 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
6665 // see note at standby() declaration
6666 if (playbackThread->standby() && !playbackThread->isSuspended()) {
6667 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(),
6668 thread.get());
6669 return false;
6670 }
6671 }
6672 return true;
6673}
6674
Kevin Rocard12381092018-04-11 09:19:59 -07006675void AudioFlinger::DuplicatingThread::sendMetadataToBackend_l(
6676 const StreamOutHalInterface::SourceMetadata& metadata)
Kevin Rocard069c2712018-03-29 19:09:14 -07006677{
Kevin Rocard12381092018-04-11 09:19:59 -07006678 for (auto& outputTrack : outputTracks) { // not mOutputTracks
6679 outputTrack->setMetadatas(metadata.tracks);
6680 }
Kevin Rocard069c2712018-03-29 19:09:14 -07006681}
6682
Eric Laurent81784c32012-11-19 14:55:58 -08006683uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const
6684{
6685 return (mWaitTimeMs * 1000) / 2;
6686}
6687
6688void AudioFlinger::DuplicatingThread::cacheParameters_l()
6689{
6690 // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
6691 updateWaitTime_l();
6692
6693 MixerThread::cacheParameters_l();
6694}
6695
Eric Laurent6acd1d42017-01-04 14:23:29 -08006696
Eric Laurent81784c32012-11-19 14:55:58 -08006697// ----------------------------------------------------------------------------
6698// Record
6699// ----------------------------------------------------------------------------
6700
6701AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
6702 AudioStreamIn *input,
Eric Laurent81784c32012-11-19 14:55:58 -08006703 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07006704 bool systemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08006705 ) :
jiabin10d86fd2019-10-31 17:20:42 -07006706 ThreadBase(audioFlinger, id, RECORD, systemReady),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07006707 mInput(input),
Mikhail Naganovaf288872019-09-25 13:05:02 -07006708 mSource(mInput),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07006709 mActiveTracks(&this->mLocalLog),
6710 mRsmpInBuffer(NULL),
Glenn Kasten1b291842016-07-18 14:55:21 -07006711 // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l()
Glenn Kasten4cc0a6a2014-02-17 14:31:46 -08006712 mRsmpInRear(0)
Glenn Kastenb880f5e2014-05-07 08:43:45 -07006713 , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize,
6714 "RecordThreadRO", MemoryHeapBase::READ_ONLY))
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006715 // mFastCapture below
6716 , mFastCaptureFutex(0)
6717 // mInputSource
6718 // mPipeSink
6719 // mPipeSource
6720 , mPipeFramesP2(0)
6721 // mPipeMemory
6722 // mFastCaptureNBLogWriter
Glenn Kasten6e6704c2014-07-03 10:20:00 -07006723 , mFastTrackAvail(false)
Eric Laurentd8365c52017-07-16 15:27:05 -07006724 , mBtNrecSuspended(false)
Eric Laurent81784c32012-11-19 14:55:58 -08006725{
Glenn Kastend7dca052015-03-05 16:05:54 -08006726 snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id);
6727 mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
Eric Laurent81784c32012-11-19 14:55:58 -08006728
George Burgess IVa8f90c12020-05-14 11:27:19 -07006729 if (mInput->audioHwDev != nullptr) {
Andy Hungc8fddf32018-08-08 18:32:37 -07006730 mIsMsdDevice = strcmp(
6731 mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
6732 }
6733
Glenn Kastendeca2ae2014-02-07 10:25:56 -08006734 readInputParameters_l();
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006735
Andy Hungc8fddf32018-08-08 18:32:37 -07006736 // TODO: We may also match on address as well as device type for
6737 // AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_IN_REMOTE_SUBMIX
jiabin10d86fd2019-10-31 17:20:42 -07006738 // TODO: This property should be ensure that only contains one single device type.
6739 mTimestampCorrectedDevice = (audio_devices_t)property_get_int64(
6740 "audio.timestamp.corrected_input_device",
Andy Hungc8fddf32018-08-08 18:32:37 -07006741 (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD
6742 : AUDIO_DEVICE_NONE));
6743
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006744 // create an NBAIO source for the HAL input stream, and negotiate
Mikhail Naganova0c91332016-09-19 10:01:12 -07006745 mInputSource = new AudioStreamInSource(input->stream);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006746 size_t numCounterOffers = 0;
6747 const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07006748#if !LOG_NDEBUG
6749 ssize_t index =
6750#else
6751 (void)
6752#endif
6753 mInputSource->negotiate(offers, 1, NULL, numCounterOffers);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006754 ALOG_ASSERT(index == 0);
6755
6756 // initialize fast capture depending on configuration
6757 bool initFastCapture;
6758 switch (kUseFastCapture) {
6759 case FastCapture_Never:
6760 initFastCapture = false;
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07006761 ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006762 break;
6763 case FastCapture_Always:
6764 initFastCapture = true;
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07006765 ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006766 break;
6767 case FastCapture_Static:
Glenn Kasteneb9487e2015-07-22 09:15:17 -07006768 initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs;
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07006769 ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d",
6770 this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs,
6771 initFastCapture);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006772 break;
6773 // case FastCapture_Dynamic:
6774 }
6775
6776 if (initFastCapture) {
Glenn Kastend198b852015-03-16 14:55:53 -07006777 // create a Pipe for FastCapture to write to, and for us and fast tracks to read from
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006778 NBAIO_Format format = mInputSource->format();
Glenn Kasten1b291842016-07-18 14:55:21 -07006779 // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread
6780 size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006781 size_t pipeSize = pipeFramesP2 * Format_frameSize(format);
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07006782 void *pipeBuffer = nullptr;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006783 const sp<MemoryDealer> roHeap(readOnlyHeap());
6784 sp<IMemory> pipeMemory;
6785 if ((roHeap == 0) ||
6786 (pipeMemory = roHeap->allocate(pipeSize)) == 0 ||
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07006787 (pipeBuffer = pipeMemory->pointer()) == nullptr) {
6788 ALOGE("not enough memory for pipe buffer size=%zu; "
6789 "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld",
6790 pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer,
6791 (long long)kRecordThreadReadOnlyHeapSize);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006792 goto failed;
6793 }
6794 // pipe will be shared directly with fast clients, so clear to avoid leaking old information
6795 memset(pipeBuffer, 0, pipeSize);
6796 Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer);
6797 const NBAIO_Format offers[1] = {format};
6798 size_t numCounterOffers = 0;
6799 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
6800 ALOG_ASSERT(index == 0);
6801 mPipeSink = pipe;
6802 PipeReader *pipeReader = new PipeReader(*pipe);
6803 numCounterOffers = 0;
6804 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
6805 ALOG_ASSERT(index == 0);
6806 mPipeSource = pipeReader;
6807 mPipeFramesP2 = pipeFramesP2;
6808 mPipeMemory = pipeMemory;
6809
6810 // create fast capture
6811 mFastCapture = new FastCapture();
6812 FastCaptureStateQueue *sq = mFastCapture->sq();
6813#ifdef STATE_QUEUE_DUMP
6814 // FIXME
6815#endif
6816 FastCaptureState *state = sq->begin();
6817 state->mCblk = NULL;
6818 state->mInputSource = mInputSource.get();
6819 state->mInputSourceGen++;
6820 state->mPipeSink = pipe;
6821 state->mPipeSinkGen++;
6822 state->mFrameCount = mFrameCount;
6823 state->mCommand = FastCaptureState::COLD_IDLE;
6824 // already done in constructor initialization list
6825 //mFastCaptureFutex = 0;
6826 state->mColdFutexAddr = &mFastCaptureFutex;
6827 state->mColdGen++;
6828 state->mDumpState = &mFastCaptureDumpState;
6829#ifdef TEE_SINK
6830 // FIXME
6831#endif
6832 mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture");
6833 state->mNBLogWriter = mFastCaptureNBLogWriter.get();
6834 sq->end();
6835 sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
6836
6837 // start the fast capture
6838 mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO);
6839 pid_t tid = mFastCapture->getTid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07006840 sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -08006841 stream()->setHalThreadPriority(kPriorityFastCapture);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006842#ifdef AUDIO_WATCHDOG
6843 // FIXME
6844#endif
6845
Glenn Kasten6e6704c2014-07-03 10:20:00 -07006846 mFastTrackAvail = true;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006847 }
Andy Hung8946a282018-04-19 20:04:56 -07006848#ifdef TEE_SINK
6849 mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD);
6850 mTee.setId(std::string("_") + std::to_string(mId) + "_C");
6851#endif
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006852failed: ;
6853
6854 // FIXME mNormalSource
Eric Laurent81784c32012-11-19 14:55:58 -08006855}
6856
Eric Laurent81784c32012-11-19 14:55:58 -08006857AudioFlinger::RecordThread::~RecordThread()
6858{
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006859 if (mFastCapture != 0) {
6860 FastCaptureStateQueue *sq = mFastCapture->sq();
6861 FastCaptureState *state = sq->begin();
6862 if (state->mCommand == FastCaptureState::COLD_IDLE) {
6863 int32_t old = android_atomic_inc(&mFastCaptureFutex);
6864 if (old == -1) {
6865 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
6866 }
6867 }
6868 state->mCommand = FastCaptureState::EXIT;
6869 sq->end();
6870 sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
6871 mFastCapture->join();
6872 mFastCapture.clear();
6873 }
6874 mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter);
Glenn Kasten481fb672013-09-30 14:39:28 -07006875 mAudioFlinger->unregisterWriter(mNBLogWriter);
Andy Hung57446612015-04-19 23:56:46 -07006876 free(mRsmpInBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08006877}
6878
6879void AudioFlinger::RecordThread::onFirstRef()
6880{
Glenn Kastend7dca052015-03-05 16:05:54 -08006881 run(mThreadName, PRIORITY_URGENT_AUDIO);
Eric Laurent81784c32012-11-19 14:55:58 -08006882}
6883
Eric Laurent555530a2017-02-07 18:17:24 -08006884void AudioFlinger::RecordThread::preExit()
6885{
6886 ALOGV(" preExit()");
6887 Mutex::Autolock _l(mLock);
6888 for (size_t i = 0; i < mTracks.size(); i++) {
6889 sp<RecordTrack> track = mTracks[i];
6890 track->invalidate();
6891 }
6892 mActiveTracks.clear();
6893 mStartStopCond.broadcast();
6894}
6895
Eric Laurent81784c32012-11-19 14:55:58 -08006896bool AudioFlinger::RecordThread::threadLoop()
6897{
Eric Laurent81784c32012-11-19 14:55:58 -08006898 nsecs_t lastWarning = 0;
6899
6900 inputStandBy();
Eric Laurent81784c32012-11-19 14:55:58 -08006901
Glenn Kastenf10ffec2013-11-20 16:40:08 -08006902reacquire_wakelock:
6903 sp<RecordTrack> activeTrack;
6904 {
6905 Mutex::Autolock _l(mLock);
Andy Hungdae27702016-10-31 14:01:16 -07006906 acquireWakeLock_l();
Glenn Kastenf10ffec2013-11-20 16:40:08 -08006907 }
6908
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006909 // used to request a deferred sleep, to be executed later while mutex is unlocked
6910 uint32_t sleepUs = 0;
6911
Andy Hung446f4df2019-02-21 12:26:41 -08006912 int64_t lastLoopCountRead = -2; // never matches "previous" loop, when loopCount = 0.
6913
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006914 // loop while there is work to do
Andy Hung446f4df2019-02-21 12:26:41 -08006915 for (int64_t loopCount = 0;; ++loopCount) { // loopCount used for statistics tracking
Glenn Kastenc527a7c2013-08-13 15:43:49 -07006916 Vector< sp<EffectChain> > effectChains;
Glenn Kasten2cfbf882013-08-14 13:12:11 -07006917
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006918 // activeTracks accumulates a copy of a subset of mActiveTracks
6919 Vector< sp<RecordTrack> > activeTracks;
6920
Glenn Kasten735f45f2014-08-18 15:51:59 -07006921 // reference to the (first and only) active fast track
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07006922 sp<RecordTrack> fastTrack;
Eric Laurent10351942014-05-08 18:49:52 -07006923
Glenn Kasten735f45f2014-08-18 15:51:59 -07006924 // reference to a fast track which is about to be removed
6925 sp<RecordTrack> fastTrackToRemove;
6926
Eric Laurent81784c32012-11-19 14:55:58 -08006927 { // scope for mLock
6928 Mutex::Autolock _l(mLock);
Eric Laurent000a4192014-01-29 15:17:32 -08006929
Eric Laurent021cf962014-05-13 10:18:14 -07006930 processConfigEvents_l();
Glenn Kastenf10ffec2013-11-20 16:40:08 -08006931
Eric Laurent000a4192014-01-29 15:17:32 -08006932 // check exitPending here because checkForNewParameters_l() and
6933 // checkForNewParameters_l() can temporarily release mLock
6934 if (exitPending()) {
6935 break;
6936 }
6937
Eric Laurent5c25d562016-07-13 17:17:45 -07006938 // sleep with mutex unlocked
6939 if (sleepUs > 0) {
Glenn Kastenf9715e42016-07-13 14:02:03 -07006940 ATRACE_BEGIN("sleepC");
Eric Laurent5c25d562016-07-13 17:17:45 -07006941 mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs));
6942 ATRACE_END();
6943 sleepUs = 0;
6944 continue;
6945 }
6946
Glenn Kasten2b806402013-11-20 16:37:38 -08006947 // if no active track(s), then standby and release wakelock
6948 size_t size = mActiveTracks.size();
6949 if (size == 0) {
Glenn Kasten93e471f2013-08-19 08:40:07 -07006950 standbyIfNotAlreadyInStandby();
Glenn Kasten4ef0b462013-08-14 13:52:27 -07006951 // exitPending() can't become true here
Eric Laurent81784c32012-11-19 14:55:58 -08006952 releaseWakeLock_l();
6953 ALOGV("RecordThread: loop stopping");
6954 // go to sleep
6955 mWaitWorkCV.wait(mLock);
6956 ALOGV("RecordThread: loop starting");
Glenn Kastenf10ffec2013-11-20 16:40:08 -08006957 goto reacquire_wakelock;
6958 }
6959
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006960 bool doBroadcast = false;
Eric Laurent5c25d562016-07-13 17:17:45 -07006961 bool allStopped = true;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006962 for (size_t i = 0; i < size; ) {
Glenn Kasten9e982352013-08-14 14:39:50 -07006963
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006964 activeTrack = mActiveTracks[i];
6965 if (activeTrack->isTerminated()) {
Glenn Kasten735f45f2014-08-18 15:51:59 -07006966 if (activeTrack->isFastTrack()) {
6967 ALOG_ASSERT(fastTrackToRemove == 0);
6968 fastTrackToRemove = activeTrack;
6969 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006970 removeTrack_l(activeTrack);
Glenn Kasten2b806402013-11-20 16:37:38 -08006971 mActiveTracks.remove(activeTrack);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006972 size--;
Glenn Kasten9e982352013-08-14 14:39:50 -07006973 continue;
6974 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006975
6976 TrackBase::track_state activeTrackState = activeTrack->mState;
6977 switch (activeTrackState) {
6978
6979 case TrackBase::PAUSING:
6980 mActiveTracks.remove(activeTrack);
Andy Hungce685402018-10-05 17:23:27 -07006981 activeTrack->mState = TrackBase::PAUSED;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006982 doBroadcast = true;
6983 size--;
6984 continue;
6985
6986 case TrackBase::STARTING_1:
6987 sleepUs = 10000;
6988 i++;
Eric Laurent5c25d562016-07-13 17:17:45 -07006989 allStopped = false;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006990 continue;
6991
6992 case TrackBase::STARTING_2:
6993 doBroadcast = true;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006994 mStandby = false;
Glenn Kasten9e982352013-08-14 14:39:50 -07006995 activeTrack->mState = TrackBase::ACTIVE;
Eric Laurent5c25d562016-07-13 17:17:45 -07006996 allStopped = false;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08006997 break;
6998
6999 case TrackBase::ACTIVE:
Eric Laurent5c25d562016-07-13 17:17:45 -07007000 allStopped = false;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007001 break;
7002
Andy Hungce685402018-10-05 17:23:27 -07007003 case TrackBase::IDLE: // cannot be on ActiveTracks if idle
7004 case TrackBase::PAUSED: // cannot be on ActiveTracks if paused
7005 case TrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007006 default:
Andy Hungce685402018-10-05 17:23:27 -07007007 LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu",
7008 __func__, activeTrackState, activeTrack->id(), size);
Glenn Kasten9e982352013-08-14 14:39:50 -07007009 }
Glenn Kasten9e982352013-08-14 14:39:50 -07007010
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007011 activeTracks.add(activeTrack);
7012 i++;
Glenn Kasten9e982352013-08-14 14:39:50 -07007013
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007014 if (activeTrack->isFastTrack()) {
7015 ALOG_ASSERT(!mFastTrackAvail);
7016 ALOG_ASSERT(fastTrack == 0);
7017 fastTrack = activeTrack;
7018 }
Glenn Kasten9e982352013-08-14 14:39:50 -07007019 }
Eric Laurent5c25d562016-07-13 17:17:45 -07007020
Andy Hungdae27702016-10-31 14:01:16 -07007021 mActiveTracks.updatePowerState(this);
7022
Kevin Rocard069c2712018-03-29 19:09:14 -07007023 updateMetadata_l();
7024
Eric Laurent5c25d562016-07-13 17:17:45 -07007025 if (allStopped) {
7026 standbyIfNotAlreadyInStandby();
7027 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007028 if (doBroadcast) {
7029 mStartStopCond.broadcast();
7030 }
7031
7032 // sleep if there are no active tracks to process
Eric Tan39ec8d62018-07-24 09:49:29 -07007033 if (activeTracks.isEmpty()) {
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007034 if (sleepUs == 0) {
7035 sleepUs = kRecordThreadSleepUs;
7036 }
7037 continue;
7038 }
7039 sleepUs = 0;
Glenn Kasten9e982352013-08-14 14:39:50 -07007040
Eric Laurent81784c32012-11-19 14:55:58 -08007041 lockEffectChains_l(effectChains);
7042 }
7043
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007044 // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0
Glenn Kasten71652682013-08-14 15:17:55 -07007045
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007046 size_t size = effectChains.size();
7047 for (size_t i = 0; i < size; i++) {
Glenn Kasten1ba19cd2013-08-14 14:02:21 -07007048 // thread mutex is not locked, but effect chain is locked
7049 effectChains[i]->process_l();
7050 }
7051
Glenn Kasten735f45f2014-08-18 15:51:59 -07007052 // Push a new fast capture state if fast capture is not already running, or cblk change
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007053 if (mFastCapture != 0) {
7054 FastCaptureStateQueue *sq = mFastCapture->sq();
7055 FastCaptureState *state = sq->begin();
Glenn Kasten735f45f2014-08-18 15:51:59 -07007056 bool didModify = false;
7057 FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007058 if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME &&
7059 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) {
7060 if (state->mCommand == FastCaptureState::COLD_IDLE) {
7061 int32_t old = android_atomic_inc(&mFastCaptureFutex);
7062 if (old == -1) {
7063 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
7064 }
7065 }
7066 state->mCommand = FastCaptureState::READ_WRITE;
7067#if 0 // FIXME
7068 mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
Glenn Kastenfbdb2ac2015-03-02 14:47:19 -08007069 FastThreadDumpState::kSamplingNforLowRamDevice :
7070 FastThreadDumpState::kSamplingN);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007071#endif
Glenn Kasten735f45f2014-08-18 15:51:59 -07007072 didModify = true;
7073 }
7074 audio_track_cblk_t *cblkOld = state->mCblk;
7075 audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL;
7076 if (cblkNew != cblkOld) {
7077 state->mCblk = cblkNew;
7078 // block until acked if removing a fast track
7079 if (cblkOld != NULL) {
7080 block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED;
7081 }
7082 didModify = true;
7083 }
jiabin01c8f562018-07-19 17:47:28 -07007084 AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ?
7085 reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr;
7086 if (state->mFastPatchRecordBufferProvider != abp) {
7087 state->mFastPatchRecordBufferProvider = abp;
7088 state->mFastPatchRecordFormat = fastTrack == 0 ?
7089 AUDIO_FORMAT_INVALID : fastTrack->format();
7090 didModify = true;
7091 }
Glenn Kasten735f45f2014-08-18 15:51:59 -07007092 sq->end(didModify);
7093 if (didModify) {
7094 sq->push(block);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007095#if 0
7096 if (kUseFastCapture == FastCapture_Dynamic) {
7097 mNormalSource = mPipeSource;
7098 }
7099#endif
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007100 }
7101 }
7102
Glenn Kasten735f45f2014-08-18 15:51:59 -07007103 // now run the fast track destructor with thread mutex unlocked
7104 fastTrackToRemove.clear();
7105
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007106 // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one.
7107 // Only the client(s) that are too slow will overrun. But if even the fastest client is too
7108 // slow, then this RecordThread will overrun by not calling HAL read often enough.
7109 // If destination is non-contiguous, first read past the nominal end of buffer, then
7110 // copy to the right place. Permitted because mRsmpInBuffer was over-allocated.
Glenn Kasten1ba19cd2013-08-14 14:02:21 -07007111
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007112 int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1);
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007113 ssize_t framesRead;
Andy Hung446f4df2019-02-21 12:26:41 -08007114 const int64_t lastIoBeginNs = systemTime(); // start IO timing
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007115
7116 // If an NBAIO source is present, use it to read the normal capture's data
7117 if (mPipeSource != 0) {
Andy Hung156317a2018-08-02 11:49:29 -07007118 size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2);
Andy Hungd5b638f2018-04-30 13:56:10 -07007119
7120 // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer
7121 // to the full buffer point (clearing the overflow condition). Upon OVERRUN error,
7122 // we immediately retry the read() to get data and prevent another overflow.
7123 for (int retries = 0; retries <= 2; ++retries) {
7124 ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries);
7125 framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize,
7126 framesToRead);
7127 if (framesRead != OVERRUN) break;
7128 }
7129
Andy Hung7a3dc6b2018-05-01 16:39:51 -07007130 const ssize_t availableToRead = mPipeSource->availableToRead();
7131 if (availableToRead >= 0) {
7132 // PipeSource is the master clock. It is up to the AudioRecord client to keep up.
7133 LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2,
7134 "more frames to read than fifo size, %zd > %zu",
7135 availableToRead, mPipeFramesP2);
7136 const size_t pipeFramesFree = mPipeFramesP2 - availableToRead;
7137 const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2;
7138 ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd",
7139 mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead);
Glenn Kasten1b291842016-07-18 14:55:21 -07007140 sleepUs = (sleepFrames * 1000000LL) / mSampleRate;
7141 }
7142 if (framesRead < 0) {
7143 status_t status = (status_t) framesRead;
7144 switch (status) {
7145 case OVERRUN:
7146 ALOGW("overrun on read from pipe");
7147 framesRead = 0;
7148 break;
7149 case NEGOTIATE:
7150 ALOGE("re-negotiation is needed");
7151 framesRead = -1; // Will cause an attempt to recover.
7152 break;
7153 default:
7154 ALOGE("unknown error %d on read from pipe", status);
7155 break;
7156 }
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007157 }
7158 // otherwise use the HAL / AudioStreamIn directly
7159 } else {
Glenn Kastenec6a7032016-03-14 07:40:23 -07007160 ATRACE_BEGIN("read");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07007161 size_t bytesRead;
Mikhail Naganovaf288872019-09-25 13:05:02 -07007162 status_t result = mSource->read(
Mikhail Naganov1dc98672016-08-18 17:50:29 -07007163 (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead);
Glenn Kastenec6a7032016-03-14 07:40:23 -07007164 ATRACE_END();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07007165 if (result < 0) {
7166 framesRead = result;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007167 } else {
7168 framesRead = bytesRead / mFrameSize;
7169 }
7170 }
7171
Andy Hung446f4df2019-02-21 12:26:41 -08007172 const int64_t lastIoEndNs = systemTime(); // end IO timing
7173
Andy Hung3f0c9022016-01-15 17:49:46 -08007174 // Update server timestamp with server stats
7175 // systemTime() is optional if the hardware supports timestamps.
7176 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead;
Andy Hung446f4df2019-02-21 12:26:41 -08007177 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs;
Andy Hung3f0c9022016-01-15 17:49:46 -08007178
7179 // Update server timestamp with kernel stats
Mikhail Naganov1dc98672016-08-18 17:50:29 -07007180 if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) {
Andy Hung3f0c9022016-01-15 17:49:46 -08007181 int64_t position, time;
Andy Hung2e2c0bb2018-06-11 19:13:11 -07007182 if (mStandby) {
7183 mTimestampVerifier.discontinuity();
Mikhail Naganovaf288872019-09-25 13:05:02 -07007184 } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR
Andy Hungc8fddf32018-08-08 18:32:37 -07007185 && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) {
7186
7187 mTimestampVerifier.add(position, time, mSampleRate);
7188
7189 // Correct timestamps
7190 if (isTimestampCorrectionEnabled()) {
Dean Wheatley56a583e2020-05-08 15:12:17 +10007191 ALOGVV("TS_BEFORE: %d %lld %lld",
Andy Hungc8fddf32018-08-08 18:32:37 -07007192 id(), (long long)time, (long long)position);
7193 auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
7194 position = correctedTimestamp.mFrames;
7195 time = correctedTimestamp.mTimeNs;
Dean Wheatley56a583e2020-05-08 15:12:17 +10007196 ALOGVV("TS_AFTER: %d %lld %lld",
Andy Hungc8fddf32018-08-08 18:32:37 -07007197 id(), (long long)time, (long long)position);
7198 }
7199
Andy Hung3f0c9022016-01-15 17:49:46 -08007200 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position;
7201 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time;
7202 // Note: In general record buffers should tend to be empty in
7203 // a properly running pipeline.
7204 //
7205 // Also, it is not advantageous to call get_presentation_position during the read
7206 // as the read obtains a lock, preventing the timestamp call from executing.
Andy Hung2e2c0bb2018-06-11 19:13:11 -07007207 } else {
7208 mTimestampVerifier.error();
Andy Hung3f0c9022016-01-15 17:49:46 -08007209 }
7210 }
Andy Hunge6c37112019-02-26 17:38:10 -08007211
7212 // From the timestamp, input read latency is negative output write latency.
7213 const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE;
7214 const double latencyMs = RecordTrack::checkServerLatencySupported(mFormat, flags)
7215 ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.;
7216 if (latencyMs != 0.) { // note 0. means timestamp is empty.
7217 mLatencyMs.add(latencyMs);
7218 }
7219
Andy Hung3f0c9022016-01-15 17:49:46 -08007220 // Use this to track timestamp information
7221 // ALOGD("%s", mTimestamp.toString().c_str());
7222
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007223 if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07007224 ALOGE("read failed: framesRead=%zd", framesRead);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007225 // Force input into standby so that it tries to recover at next read attempt
7226 inputStandBy();
7227 sleepUs = kRecordThreadSleepUs;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007228 }
7229 if (framesRead <= 0) {
Glenn Kasten3d61bc12014-06-16 10:25:20 -07007230 goto unlock;
Glenn Kasten1ba19cd2013-08-14 14:02:21 -07007231 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007232 ALOG_ASSERT(framesRead > 0);
Andy Hung6427e442018-08-09 12:51:02 -07007233 mFramesRead += framesRead;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007234
Andy Hung8946a282018-04-19 20:04:56 -07007235#ifdef TEE_SINK
7236 (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead);
7237#endif
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007238 // If destination is non-contiguous, we now correct for reading past end of buffer.
Glenn Kasten3d61bc12014-06-16 10:25:20 -07007239 {
7240 size_t part1 = mRsmpInFramesP2 - rear;
7241 if ((size_t) framesRead > part1) {
Andy Hung57446612015-04-19 23:56:46 -07007242 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize,
Glenn Kasten3d61bc12014-06-16 10:25:20 -07007243 (framesRead - part1) * mFrameSize);
7244 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007245 }
7246 rear = mRsmpInRear += framesRead;
7247
7248 size = activeTracks.size();
Svet Ganovf4ddfef2018-01-16 07:37:58 -08007249
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007250 // loop over each active track
7251 for (size_t i = 0; i < size; i++) {
7252 activeTrack = activeTracks[i];
7253
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007254 // skip fast tracks, as those are handled directly by FastCapture
7255 if (activeTrack->isFastTrack()) {
7256 continue;
7257 }
7258
Andy Hung73c02e42015-03-29 01:13:58 -07007259 // TODO: This code probably should be moved to RecordTrack.
Andy Hung97a893e2015-03-29 01:03:07 -07007260 // TODO: Update the activeTrack buffer converter in case of reconfigure.
7261
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007262 enum {
7263 OVERRUN_UNKNOWN,
7264 OVERRUN_TRUE,
7265 OVERRUN_FALSE
7266 } overrun = OVERRUN_UNKNOWN;
7267
7268 // loop over getNextBuffer to handle circular sink
7269 for (;;) {
7270
7271 activeTrack->mSink.frameCount = ~0;
7272 status_t status = activeTrack->getNextBuffer(&activeTrack->mSink);
7273 size_t framesOut = activeTrack->mSink.frameCount;
7274 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0));
7275
Andy Hung73c02e42015-03-29 01:13:58 -07007276 // check available frames and handle overrun conditions
7277 // if the record track isn't draining fast enough.
7278 bool hasOverrun;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007279 size_t framesIn;
Andy Hung73c02e42015-03-29 01:13:58 -07007280 activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun);
7281 if (hasOverrun) {
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007282 overrun = OVERRUN_TRUE;
7283 }
Glenn Kasten4cc0a6a2014-02-17 14:31:46 -08007284 if (framesOut == 0 || framesIn == 0) {
7285 break;
7286 }
7287
Andy Hung6770c6f2015-04-07 13:43:36 -07007288 // Don't allow framesOut to be larger than what is possible with resampling
7289 // from framesIn.
7290 // This isn't strictly necessary but helps limit buffer resizing in
7291 // RecordBufferConverter. TODO: remove when no longer needed.
7292 framesOut = min(framesOut,
7293 destinationFramesPossible(
7294 framesIn, mSampleRate, activeTrack->mSampleRate));
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07007295
7296 if (activeTrack->isDirect()) {
Daniel Van Veen9e2376e2018-09-27 14:37:58 +10007297 // No RecordBufferConverter used for direct streams. Pass
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07007298 // straight from RecordThread buffer to RecordTrack buffer.
7299 AudioBufferProvider::Buffer buffer;
7300 buffer.frameCount = framesOut;
7301 status_t status = activeTrack->mResamplerBufferProvider->getNextBuffer(&buffer);
7302 if (status == OK && buffer.frameCount != 0) {
7303 ALOGV_IF(buffer.frameCount != framesOut,
7304 "%s() read less than expected (%zu vs %zu)",
7305 __func__, buffer.frameCount, framesOut);
7306 framesOut = buffer.frameCount;
Daniel Van Veen9e2376e2018-09-27 14:37:58 +10007307 memcpy(activeTrack->mSink.raw, buffer.raw, buffer.frameCount * mFrameSize);
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07007308 activeTrack->mResamplerBufferProvider->releaseBuffer(&buffer);
7309 } else {
7310 framesOut = 0;
7311 ALOGE("%s() cannot fill request, status: %d, frameCount: %zu",
7312 __func__, status, buffer.frameCount);
7313 }
7314 } else {
7315 // process frames from the RecordThread buffer provider to the RecordTrack
7316 // buffer
7317 framesOut = activeTrack->mRecordBufferConverter->convert(
7318 activeTrack->mSink.raw,
7319 activeTrack->mResamplerBufferProvider,
7320 framesOut);
7321 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007322
7323 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) {
7324 overrun = OVERRUN_FALSE;
7325 }
7326
7327 if (activeTrack->mFramesToDrop == 0) {
7328 if (framesOut > 0) {
7329 activeTrack->mSink.frameCount = framesOut;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08007330 // Sanitize before releasing if the track has no access to the source data
7331 // An idle UID receives silence from non virtual devices until active
7332 if (activeTrack->isSilenced()) {
Jean-Michel Trivib0de5692019-08-20 15:42:04 -07007333 memset(activeTrack->mSink.raw, 0, framesOut * activeTrack->frameSize());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08007334 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007335 activeTrack->releaseBuffer(&activeTrack->mSink);
7336 }
7337 } else {
7338 // FIXME could do a partial drop of framesOut
7339 if (activeTrack->mFramesToDrop > 0) {
Mikhail Naganov6d91ba52019-03-26 14:35:28 -07007340 activeTrack->mFramesToDrop -= (ssize_t)framesOut;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007341 if (activeTrack->mFramesToDrop <= 0) {
Glenn Kasten25f4aa82014-02-07 10:50:43 -08007342 activeTrack->clearSyncStartEvent();
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007343 }
7344 } else {
7345 activeTrack->mFramesToDrop += framesOut;
7346 if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 ||
7347 activeTrack->mSyncStartEvent->isCancelled()) {
7348 ALOGW("Synced record %s, session %d, trigger session %d",
7349 (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled",
7350 activeTrack->sessionId(),
7351 (activeTrack->mSyncStartEvent != 0) ?
Glenn Kastend848eb42016-03-08 13:42:11 -08007352 activeTrack->mSyncStartEvent->triggerSession() :
7353 AUDIO_SESSION_NONE);
Glenn Kasten25f4aa82014-02-07 10:50:43 -08007354 activeTrack->clearSyncStartEvent();
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007355 }
7356 }
7357 }
7358
7359 if (framesOut == 0) {
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007360 break;
Glenn Kasten1ba19cd2013-08-14 14:02:21 -07007361 }
7362 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007363
7364 switch (overrun) {
7365 case OVERRUN_TRUE:
7366 // client isn't retrieving buffers fast enough
7367 if (!activeTrack->setOverflow()) {
7368 nsecs_t now = systemTime();
7369 // FIXME should lastWarning per track?
7370 if ((now - lastWarning) > kWarningThrottleNs) {
7371 ALOGW("RecordThread: buffer overflow");
7372 lastWarning = now;
7373 }
7374 }
7375 break;
7376 case OVERRUN_FALSE:
7377 activeTrack->clearOverflow();
7378 break;
7379 case OVERRUN_UNKNOWN:
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007380 break;
7381 }
7382
Andy Hung3f0c9022016-01-15 17:49:46 -08007383 // update frame information and push timestamp out
7384 activeTrack->updateTrackFrameInfo(
Andy Hung6ae58432016-02-16 18:32:24 -08007385 activeTrack->mServerProxy->framesReleased(),
Andy Hung3f0c9022016-01-15 17:49:46 -08007386 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER],
7387 mSampleRate, mTimestamp);
Glenn Kasten1ba19cd2013-08-14 14:02:21 -07007388 }
7389
Glenn Kasten3d61bc12014-06-16 10:25:20 -07007390unlock:
Eric Laurent81784c32012-11-19 14:55:58 -08007391 // enable changes in effect chain
7392 unlockEffectChains(effectChains);
Glenn Kastenc527a7c2013-08-13 15:43:49 -07007393 // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end
Eric Laurentcccbc762019-04-05 14:20:05 -07007394 if (audio_has_proportional_frames(mFormat)
7395 && loopCount == lastLoopCountRead + 1) {
7396 const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs;
7397 const double jitterMs =
7398 TimestampVerifier<int64_t, int64_t>::computeJitterMs(
7399 {framesRead, readPeriodNs},
7400 {0, 0} /* lastTimestamp */, mSampleRate);
7401 const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6;
7402
7403 Mutex::Autolock _l(mLock);
7404 mIoJitterMs.add(jitterMs);
7405 mProcessTimeMs.add(processMs);
7406 }
7407 // update timing info.
7408 mLastIoBeginNs = lastIoBeginNs;
7409 mLastIoEndNs = lastIoEndNs;
7410 lastLoopCountRead = loopCount;
Eric Laurent81784c32012-11-19 14:55:58 -08007411 }
7412
Glenn Kasten93e471f2013-08-19 08:40:07 -07007413 standbyIfNotAlreadyInStandby();
Eric Laurent81784c32012-11-19 14:55:58 -08007414
7415 {
7416 Mutex::Autolock _l(mLock);
Eric Laurent9a54bc22013-09-09 09:08:44 -07007417 for (size_t i = 0; i < mTracks.size(); i++) {
7418 sp<RecordTrack> track = mTracks[i];
7419 track->invalidate();
7420 }
Glenn Kasten2b806402013-11-20 16:37:38 -08007421 mActiveTracks.clear();
Eric Laurent81784c32012-11-19 14:55:58 -08007422 mStartStopCond.broadcast();
7423 }
7424
7425 releaseWakeLock();
7426
7427 ALOGV("RecordThread %p exiting", this);
7428 return false;
7429}
7430
Glenn Kasten93e471f2013-08-19 08:40:07 -07007431void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby()
Eric Laurent81784c32012-11-19 14:55:58 -08007432{
7433 if (!mStandby) {
7434 inputStandBy();
7435 mStandby = true;
7436 }
7437}
7438
7439void AudioFlinger::RecordThread::inputStandBy()
7440{
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007441 // Idle the fast capture if it's currently running
7442 if (mFastCapture != 0) {
7443 FastCaptureStateQueue *sq = mFastCapture->sq();
7444 FastCaptureState *state = sq->begin();
7445 if (!(state->mCommand & FastCaptureState::IDLE)) {
7446 state->mCommand = FastCaptureState::COLD_IDLE;
7447 state->mColdFutexAddr = &mFastCaptureFutex;
7448 state->mColdGen++;
7449 mFastCaptureFutex = 0;
7450 sq->end();
7451 // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
7452 sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED);
7453#if 0
7454 if (kUseFastCapture == FastCapture_Dynamic) {
7455 // FIXME
7456 }
7457#endif
7458#ifdef AUDIO_WATCHDOG
7459 // FIXME
7460#endif
7461 } else {
7462 sq->end(false /*didModify*/);
7463 }
7464 }
Mikhail Naganovaf288872019-09-25 13:05:02 -07007465 status_t result = mSource->standby();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07007466 ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result);
Andy Hungad6d52d2016-07-18 13:42:03 -07007467
7468 // If going into standby, flush the pipe source.
7469 if (mPipeSource.get() != nullptr) {
7470 const ssize_t flushed = mPipeSource->flush();
7471 if (flushed > 0) {
7472 ALOGV("Input standby flushed PipeSource %zd frames", flushed);
7473 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed;
7474 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime();
7475 }
7476 }
Eric Laurent81784c32012-11-19 14:55:58 -08007477}
7478
Glenn Kasten05997e22014-03-13 15:08:33 -07007479// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held
Glenn Kastene198c362013-08-13 09:13:36 -07007480sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
Eric Laurent81784c32012-11-19 14:55:58 -08007481 const sp<AudioFlinger::Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07007482 const audio_attributes_t& attr,
Eric Laurentf14db3c2017-12-08 14:20:36 -08007483 uint32_t *pSampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -08007484 audio_format_t format,
7485 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08007486 size_t *pFrameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08007487 audio_session_t sessionId,
Eric Laurentf14db3c2017-12-08 14:20:36 -08007488 size_t *pNotificationFrameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07007489 pid_t creatorPid,
Andy Hung1f12a8a2016-11-07 16:10:30 -08007490 uid_t uid,
Eric Laurent05067782016-06-01 18:27:28 -07007491 audio_input_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -08007492 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08007493 status_t *status,
Jean-Michel Trivib0de5692019-08-20 15:42:04 -07007494 audio_port_handle_t portId,
7495 const String16& opPackageName)
Eric Laurent81784c32012-11-19 14:55:58 -08007496{
Glenn Kasten74935e42013-12-19 08:56:45 -08007497 size_t frameCount = *pFrameCount;
Eric Laurentf14db3c2017-12-08 14:20:36 -08007498 size_t notificationFrameCount = *pNotificationFrameCount;
Eric Laurent81784c32012-11-19 14:55:58 -08007499 sp<RecordTrack> track;
7500 status_t lStatus;
Eric Laurent05067782016-06-01 18:27:28 -07007501 audio_input_flags_t inputFlags = mInput->flags;
Eric Laurentf14db3c2017-12-08 14:20:36 -08007502 audio_input_flags_t requestedFlags = *flags;
7503 uint32_t sampleRate;
7504
7505 lStatus = initCheck();
7506 if (lStatus != NO_ERROR) {
7507 ALOGE("createRecordTrack_l() audio driver not initialized");
7508 goto Exit;
7509 }
7510
Mikhail Naganovce9f2652018-07-16 11:09:24 -07007511 if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) {
7512 ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT");
7513 lStatus = BAD_VALUE;
7514 goto Exit;
7515 }
7516
Eric Laurentf14db3c2017-12-08 14:20:36 -08007517 if (*pSampleRate == 0) {
7518 *pSampleRate = mSampleRate;
7519 }
7520 sampleRate = *pSampleRate;
Eric Laurent05067782016-06-01 18:27:28 -07007521
7522 // special case for FAST flag considered OK if fast capture is present
7523 if (hasFastCapture()) {
7524 inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST);
7525 }
7526
Eric Laurentf14db3c2017-12-08 14:20:36 -08007527 // Check if requested flags are compatible with input stream flags
Eric Laurent05067782016-06-01 18:27:28 -07007528 if ((*flags & inputFlags) != *flags) {
7529 ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and"
7530 " input flags (%08x)",
7531 *flags, inputFlags);
7532 *flags = (audio_input_flags_t)(*flags & inputFlags);
7533 }
Eric Laurent81784c32012-11-19 14:55:58 -08007534
Glenn Kasten90e58b12013-07-31 16:16:02 -07007535 // client expresses a preference for FAST, but we get the final say
Eric Laurent05067782016-06-01 18:27:28 -07007536 if (*flags & AUDIO_INPUT_FLAG_FAST) {
Glenn Kasten90e58b12013-07-31 16:16:02 -07007537 if (
Glenn Kastenb7fbf7e2015-03-18 12:57:28 -07007538 // we formerly checked for a callback handler (non-0 tid),
7539 // but that is no longer required for TRANSFER_OBTAIN mode
7540 //
Phil Burk7ed66a12019-04-18 13:20:30 -07007541 // Frame count is not specified (0), or is less than or equal the pipe depth.
7542 // It is OK to provide a higher capacity than requested.
7543 // We will force it to mPipeFramesP2 below.
7544 (frameCount <= mPipeFramesP2) &&
Glenn Kasten3a6c90a2014-03-13 15:07:51 -07007545 // PCM data
7546 audio_is_linear_pcm(format) &&
Glenn Kasten7fd04222016-02-02 12:38:16 -08007547 // hardware format
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007548 (format == mFormat) &&
Glenn Kasten7fd04222016-02-02 12:38:16 -08007549 // hardware channel mask
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007550 (channelMask == mChannelMask) &&
Glenn Kasten7fd04222016-02-02 12:38:16 -08007551 // hardware sample rate
Glenn Kasten90e58b12013-07-31 16:16:02 -07007552 (sampleRate == mSampleRate) &&
Glenn Kasten3a6c90a2014-03-13 15:07:51 -07007553 // record thread has an associated fast capture
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007554 hasFastCapture() &&
7555 // there are sufficient fast track slots available
7556 mFastTrackAvail
Glenn Kasten90e58b12013-07-31 16:16:02 -07007557 ) {
Eric Laurent4c415062016-06-17 16:14:16 -07007558 // check compatibility with audio effects.
7559 Mutex::Autolock _l(mLock);
7560 // Do not accept FAST flag if the session has software effects
7561 sp<EffectChain> chain = getEffectChain_l(sessionId);
7562 if (chain != 0) {
Andy Hungd3bb0ad2016-10-11 17:16:43 -07007563 audio_input_flags_t old = *flags;
7564 chain->checkInputFlagCompatibility(flags);
7565 if (old != *flags) {
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07007566 ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x",
7567 this, (int)old, (int)*flags);
Eric Laurent4c415062016-06-17 16:14:16 -07007568 }
7569 }
Eric Laurent122f7e72016-06-29 11:53:29 -07007570 ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0,
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07007571 "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
7572 this, frameCount, mFrameCount);
Glenn Kasten90e58b12013-07-31 16:16:02 -07007573 } else {
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07007574 ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu "
7575 "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u "
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007576 "hasFastCapture=%d tid=%d mFastTrackAvail=%d",
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07007577 this, frameCount, mFrameCount, mPipeFramesP2,
7578 format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate,
Glenn Kasten74105912014-07-03 12:28:53 -07007579 hasFastCapture(), tid, mFastTrackAvail);
Eric Laurent05067782016-06-01 18:27:28 -07007580 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
Glenn Kasten74105912014-07-03 12:28:53 -07007581 }
7582 }
7583
Eric Laurentf14db3c2017-12-08 14:20:36 -08007584 // If FAST or RAW flags were corrected, ask caller to request new input from audio policy
7585 if ((*flags & AUDIO_INPUT_FLAG_FAST) !=
7586 (requestedFlags & AUDIO_INPUT_FLAG_FAST)) {
7587 *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW));
7588 lStatus = BAD_TYPE;
7589 goto Exit;
7590 }
7591
Glenn Kasten74105912014-07-03 12:28:53 -07007592 // compute track buffer size in frames, and suggest the notification frame count
Eric Laurent05067782016-06-01 18:27:28 -07007593 if (*flags & AUDIO_INPUT_FLAG_FAST) {
Glenn Kasten74105912014-07-03 12:28:53 -07007594 // fast track: frame count is exactly the pipe depth
7595 frameCount = mPipeFramesP2;
7596 // ignore requested notificationFrames, and always notify exactly once every HAL buffer
Eric Laurentf14db3c2017-12-08 14:20:36 -08007597 notificationFrameCount = mFrameCount;
Glenn Kasten74105912014-07-03 12:28:53 -07007598 } else {
Glenn Kasten49d00ad2014-07-21 11:22:03 -07007599 // not fast track: max notification period is resampled equivalent of one HAL buffer time
7600 // or 20 ms if there is a fast capture
7601 // TODO This could be a roundupRatio inline, and const
7602 size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount)
7603 * sampleRate + mSampleRate - 1) / mSampleRate;
7604 // minimum number of notification periods is at least kMinNotifications,
7605 // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs)
7606 static const size_t kMinNotifications = 3;
7607 static const uint32_t kMinMs = 30;
7608 // TODO This could be a roundupRatio inline
7609 const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000;
7610 // TODO This could be a roundupRatio inline
7611 const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) /
7612 maxNotificationFrames;
7613 const size_t minFrameCount = maxNotificationFrames *
7614 max(kMinNotifications, minNotificationsByMs);
7615 frameCount = max(frameCount, minFrameCount);
Eric Laurentf14db3c2017-12-08 14:20:36 -08007616 if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
7617 notificationFrameCount = maxNotificationFrames;
Glenn Kasten74105912014-07-03 12:28:53 -07007618 }
Glenn Kasten90e58b12013-07-31 16:16:02 -07007619 }
Glenn Kasten74935e42013-12-19 08:56:45 -08007620 *pFrameCount = frameCount;
Eric Laurentf14db3c2017-12-08 14:20:36 -08007621 *pNotificationFrameCount = notificationFrameCount;
Eric Laurent81784c32012-11-19 14:55:58 -08007622
7623 { // scope for mLock
7624 Mutex::Autolock _l(mLock);
7625
Kevin Rocard1f564ac2018-03-29 13:53:10 -07007626 track = new RecordTrack(this, client, attr, sampleRate,
Andy Hung8fe68032017-06-05 16:17:51 -07007627 format, channelMask, frameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07007628 nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, uid,
Jean-Michel Trivib0de5692019-08-20 15:42:04 -07007629 *flags, TrackBase::TYPE_DEFAULT, opPackageName, portId);
Eric Laurent81784c32012-11-19 14:55:58 -08007630
Glenn Kasten03003332013-08-06 15:40:54 -07007631 lStatus = track->initCheck();
7632 if (lStatus != NO_ERROR) {
Glenn Kasten35295072013-10-07 09:27:06 -07007633 ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08007634 // track must be cleared from the caller as the caller has the AF lock
Eric Laurent81784c32012-11-19 14:55:58 -08007635 goto Exit;
7636 }
7637 mTracks.add(track);
7638
Eric Laurent05067782016-06-01 18:27:28 -07007639 if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) {
Glenn Kasten90e58b12013-07-31 16:16:02 -07007640 pid_t callingPid = IPCThreadState::self()->getCallingPid();
7641 // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
7642 // so ask activity manager to do this on our behalf
Glenn Kastenaf9a7b52017-03-29 11:29:39 -07007643 sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
Glenn Kasten90e58b12013-07-31 16:16:02 -07007644 }
Eric Laurent81784c32012-11-19 14:55:58 -08007645 }
Glenn Kasten05997e22014-03-13 15:08:33 -07007646
Eric Laurent81784c32012-11-19 14:55:58 -08007647 lStatus = NO_ERROR;
7648
7649Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07007650 *status = lStatus;
Eric Laurent81784c32012-11-19 14:55:58 -08007651 return track;
7652}
7653
7654status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack,
7655 AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08007656 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08007657{
7658 ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
7659 sp<ThreadBase> strongMe = this;
7660 status_t status = NO_ERROR;
7661
7662 if (event == AudioSystem::SYNC_EVENT_NONE) {
Glenn Kasten25f4aa82014-02-07 10:50:43 -08007663 recordTrack->clearSyncStartEvent();
Eric Laurent81784c32012-11-19 14:55:58 -08007664 } else if (event != AudioSystem::SYNC_EVENT_SAME) {
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007665 recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event,
Eric Laurent81784c32012-11-19 14:55:58 -08007666 triggerSession,
7667 recordTrack->sessionId(),
7668 syncStartEventCallback,
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007669 recordTrack);
Eric Laurent81784c32012-11-19 14:55:58 -08007670 // Sync event can be cancelled by the trigger session if the track is not in a
7671 // compatible state in which case we start record immediately
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007672 if (recordTrack->mSyncStartEvent->isCancelled()) {
Glenn Kasten25f4aa82014-02-07 10:50:43 -08007673 recordTrack->clearSyncStartEvent();
Eric Laurent81784c32012-11-19 14:55:58 -08007674 } else {
7675 // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs
Ivan Lozano1b35dd62017-12-06 16:55:10 -08007676 recordTrack->mFramesToDrop = -(ssize_t)
Glenn Kasten4cc0a6a2014-02-17 14:31:46 -08007677 ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000);
Eric Laurent81784c32012-11-19 14:55:58 -08007678 }
7679 }
7680
7681 {
Glenn Kasten47c20702013-08-13 15:37:35 -07007682 // This section is a rendezvous between binder thread executing start() and RecordThread
Eric Laurent81784c32012-11-19 14:55:58 -08007683 AutoMutex lock(mLock);
Andy Hungce685402018-10-05 17:23:27 -07007684 if (recordTrack->isInvalid()) {
7685 recordTrack->clearSyncStartEvent();
7686 return INVALID_OPERATION;
7687 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007688 if (mActiveTracks.indexOf(recordTrack) >= 0) {
7689 if (recordTrack->mState == TrackBase::PAUSING) {
Andy Hungce685402018-10-05 17:23:27 -07007690 // We haven't stopped yet (moved to PAUSED and not in mActiveTracks)
7691 // so no need to startInput().
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007692 ALOGV("active record track PAUSING -> ACTIVE");
Glenn Kastenf10ffec2013-11-20 16:40:08 -08007693 recordTrack->mState = TrackBase::ACTIVE;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007694 } else {
7695 ALOGV("active record track state %d", recordTrack->mState);
Eric Laurent81784c32012-11-19 14:55:58 -08007696 }
7697 return status;
7698 }
7699
Glenn Kasten4cc0a6a2014-02-17 14:31:46 -08007700 // TODO consider other ways of handling this, such as changing the state to :STARTING and
7701 // adding the track to mActiveTracks after returning from AudioSystem::startInput(),
7702 // or using a separate command thread
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007703 recordTrack->mState = TrackBase::STARTING_1;
Glenn Kasten2b806402013-11-20 16:37:38 -08007704 mActiveTracks.add(recordTrack);
Eric Laurent83b88082014-06-20 18:31:16 -07007705 status_t status = NO_ERROR;
7706 if (recordTrack->isExternalTrack()) {
7707 mLock.unlock();
Eric Laurent4eb58f12018-12-07 16:41:02 -08007708 status = AudioSystem::startInput(recordTrack->portId());
Eric Laurent83b88082014-06-20 18:31:16 -07007709 mLock.lock();
Andy Hungce685402018-10-05 17:23:27 -07007710 if (recordTrack->isInvalid()) {
7711 recordTrack->clearSyncStartEvent();
7712 if (status == NO_ERROR && recordTrack->mState == TrackBase::STARTING_1) {
7713 recordTrack->mState = TrackBase::STARTING_2;
7714 // STARTING_2 forces destroy to call stopInput.
7715 }
7716 return INVALID_OPERATION;
7717 }
7718 if (recordTrack->mState != TrackBase::STARTING_1) {
7719 ALOGW("%s(%d): unsynchronized mState:%d change",
7720 __func__, recordTrack->id(), recordTrack->mState);
7721 // Someone else has changed state, let them take over,
7722 // leave mState in the new state.
7723 recordTrack->clearSyncStartEvent();
7724 return INVALID_OPERATION;
7725 }
7726 // we're ok, but perhaps startInput has failed
Eric Laurent83b88082014-06-20 18:31:16 -07007727 if (status != NO_ERROR) {
Andy Hungce685402018-10-05 17:23:27 -07007728 ALOGW("%s(%d): startInput failed, status %d",
7729 __func__, recordTrack->id(), status);
7730 // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks,
7731 // leave in STARTING_1, so destroy() will not call stopInput.
Eric Laurent83b88082014-06-20 18:31:16 -07007732 mActiveTracks.remove(recordTrack);
Eric Laurent83b88082014-06-20 18:31:16 -07007733 recordTrack->clearSyncStartEvent();
Eric Laurent83b88082014-06-20 18:31:16 -07007734 return status;
7735 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07007736 sendIoConfigEvent_l(
7737 AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId());
Eric Laurent81784c32012-11-19 14:55:58 -08007738 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007739 // Catch up with current buffer indices if thread is already running.
7740 // This is what makes a new client discard all buffered data. If the track's mRsmpInFront
7741 // was initialized to some value closer to the thread's mRsmpInFront, then the track could
7742 // see previously buffered data before it called start(), but with greater risk of overrun.
7743
Andy Hung73c02e42015-03-29 01:13:58 -07007744 recordTrack->mResamplerBufferProvider->reset();
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07007745 if (!recordTrack->isDirect()) {
7746 // clear any converter state as new data will be discontinuous
7747 recordTrack->mRecordBufferConverter->reset();
7748 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08007749 recordTrack->mState = TrackBase::STARTING_2;
Eric Laurent81784c32012-11-19 14:55:58 -08007750 // signal thread to start
Eric Laurent81784c32012-11-19 14:55:58 -08007751 mWaitWorkCV.broadcast();
Eric Laurent81784c32012-11-19 14:55:58 -08007752 return status;
7753 }
Eric Laurent81784c32012-11-19 14:55:58 -08007754}
7755
Eric Laurent81784c32012-11-19 14:55:58 -08007756void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
7757{
7758 sp<SyncEvent> strongEvent = event.promote();
7759
7760 if (strongEvent != 0) {
Eric Laurent8ea16e42014-02-20 16:26:11 -08007761 sp<RefBase> ptr = strongEvent->cookie().promote();
7762 if (ptr != 0) {
7763 RecordTrack *recordTrack = (RecordTrack *)ptr.get();
7764 recordTrack->handleSyncStartEvent(strongEvent);
7765 }
Eric Laurent81784c32012-11-19 14:55:58 -08007766 }
7767}
7768
Glenn Kastena8356f62013-07-25 14:37:52 -07007769bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
Eric Laurent81784c32012-11-19 14:55:58 -08007770 ALOGV("RecordThread::stop");
Glenn Kastena8356f62013-07-25 14:37:52 -07007771 AutoMutex _l(mLock);
Andy Hungce685402018-10-05 17:23:27 -07007772 // if we're invalid, we can't be on the ActiveTracks.
Jean-Michel Trivi38f86712017-04-11 14:10:17 -07007773 if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) {
Eric Laurent81784c32012-11-19 14:55:58 -08007774 return false;
7775 }
Glenn Kasten47c20702013-08-13 15:37:35 -07007776 // note that threadLoop may still be processing the track at this point [without lock]
Eric Laurent81784c32012-11-19 14:55:58 -08007777 recordTrack->mState = TrackBase::PAUSING;
Andy Hungce685402018-10-05 17:23:27 -07007778
Andy Hungabfab202019-03-07 19:45:54 -08007779 // NOTE: Waiting here is important to keep stop synchronous.
7780 // This is needed for proper patchRecord peer release.
Andy Hungce685402018-10-05 17:23:27 -07007781 while (recordTrack->mState == TrackBase::PAUSING && !recordTrack->isInvalid()) {
7782 mWaitWorkCV.broadcast(); // signal thread to stop
7783 mStartStopCond.wait(mLock);
Eric Laurent81784c32012-11-19 14:55:58 -08007784 }
Andy Hungce685402018-10-05 17:23:27 -07007785
7786 if (recordTrack->mState == TrackBase::PAUSED) { // successful stop
Eric Laurent81784c32012-11-19 14:55:58 -08007787 ALOGV("Record stopped OK");
7788 return true;
7789 }
Andy Hungce685402018-10-05 17:23:27 -07007790
7791 // don't handle anything - we've been invalidated or restarted and in a different state
7792 ALOGW_IF("%s(%d): unsynchronized stop, state: %d",
7793 __func__, recordTrack->id(), recordTrack->mState);
Eric Laurent81784c32012-11-19 14:55:58 -08007794 return false;
7795}
7796
Glenn Kasten0f11b512014-01-31 16:18:54 -08007797bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
Eric Laurent81784c32012-11-19 14:55:58 -08007798{
7799 return false;
7800}
7801
Glenn Kasten0f11b512014-01-31 16:18:54 -08007802status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08007803{
7804#if 0 // This branch is currently dead code, but is preserved in case it will be needed in future
7805 if (!isValidSyncEvent(event)) {
7806 return BAD_VALUE;
7807 }
7808
Glenn Kastend848eb42016-03-08 13:42:11 -08007809 audio_session_t eventSession = event->triggerSession();
Eric Laurent81784c32012-11-19 14:55:58 -08007810 status_t ret = NAME_NOT_FOUND;
7811
7812 Mutex::Autolock _l(mLock);
7813
7814 for (size_t i = 0; i < mTracks.size(); i++) {
7815 sp<RecordTrack> track = mTracks[i];
7816 if (eventSession == track->sessionId()) {
7817 (void) track->setSyncEvent(event);
7818 ret = NO_ERROR;
7819 }
7820 }
7821 return ret;
7822#else
7823 return BAD_VALUE;
7824#endif
7825}
7826
jiabin653cc0a2018-01-17 17:54:10 -08007827status_t AudioFlinger::RecordThread::getActiveMicrophones(
7828 std::vector<media::MicrophoneInfo>* activeMicrophones)
7829{
7830 ALOGV("RecordThread::getActiveMicrophones");
7831 AutoMutex _l(mLock);
jiabin9ff780e2018-03-19 18:19:52 -07007832 status_t status = mInput->stream->getActiveMicrophones(activeMicrophones);
7833 return status;
jiabin653cc0a2018-01-17 17:54:10 -08007834}
7835
Paul McLean12340082019-03-19 09:35:05 -06007836status_t AudioFlinger::RecordThread::setPreferredMicrophoneDirection(
7837 audio_microphone_direction_t direction)
Paul McLean03a6e6a2018-12-04 10:54:13 -07007838{
Paul McLean12340082019-03-19 09:35:05 -06007839 ALOGV("setPreferredMicrophoneDirection(%d)", direction);
Paul McLean03a6e6a2018-12-04 10:54:13 -07007840 AutoMutex _l(mLock);
Paul McLean12340082019-03-19 09:35:05 -06007841 return mInput->stream->setPreferredMicrophoneDirection(direction);
Paul McLean03a6e6a2018-12-04 10:54:13 -07007842}
7843
Paul McLean12340082019-03-19 09:35:05 -06007844status_t AudioFlinger::RecordThread::setPreferredMicrophoneFieldDimension(float zoom)
Paul McLean03a6e6a2018-12-04 10:54:13 -07007845{
Paul McLean12340082019-03-19 09:35:05 -06007846 ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
Paul McLean03a6e6a2018-12-04 10:54:13 -07007847 AutoMutex _l(mLock);
Paul McLean12340082019-03-19 09:35:05 -06007848 return mInput->stream->setPreferredMicrophoneFieldDimension(zoom);
Paul McLean03a6e6a2018-12-04 10:54:13 -07007849}
7850
Kevin Rocard069c2712018-03-29 19:09:14 -07007851void AudioFlinger::RecordThread::updateMetadata_l()
7852{
7853 if (mInput == nullptr || mInput->stream == nullptr ||
7854 !mActiveTracks.readAndClearHasChanged()) {
7855 return;
7856 }
7857 StreamInHalInterface::SinkMetadata metadata;
7858 for (const sp<RecordTrack> &track : mActiveTracks) {
7859 // No track is invalid as this is called after prepareTrack_l in the same critical section
7860 metadata.tracks.push_back({
7861 .source = track->attributes().source,
7862 .gain = 1, // capture tracks do not have volumes
7863 });
7864 }
7865 mInput->stream->updateSinkMetadata(metadata);
7866}
7867
Eric Laurent81784c32012-11-19 14:55:58 -08007868// destroyTrack_l() must be called with ThreadBase::mLock held
7869void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track)
7870{
Eric Laurentbfb1b832013-01-07 09:53:42 -08007871 track->terminate();
7872 track->mState = TrackBase::STOPPED;
Eric Laurent81784c32012-11-19 14:55:58 -08007873 // active tracks are removed by threadLoop()
Glenn Kasten2b806402013-11-20 16:37:38 -08007874 if (mActiveTracks.indexOf(track) < 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08007875 removeTrack_l(track);
7876 }
7877}
7878
7879void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track)
7880{
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007881 String8 result;
7882 track->appendDump(result, false /* active */);
7883 mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string());
7884
Eric Laurent81784c32012-11-19 14:55:58 -08007885 mTracks.remove(track);
7886 // need anything related to effects here?
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007887 if (track->isFastTrack()) {
7888 ALOG_ASSERT(!mFastTrackAvail);
7889 mFastTrackAvail = true;
7890 }
Eric Laurent81784c32012-11-19 14:55:58 -08007891}
7892
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07007893void AudioFlinger::RecordThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08007894{
Mikhail Naganov913d06c2016-11-01 12:49:22 -07007895 AudioStreamIn *input = mInput;
7896 audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE;
7897 dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n",
Andy Hung9b181952019-02-25 14:53:36 -08007898 input, flags, toString(flags).c_str());
Andy Hung6427e442018-08-09 12:51:02 -07007899 dprintf(fd, " Frames read: %lld\n", (long long)mFramesRead);
Eric Tan39ec8d62018-07-24 09:49:29 -07007900 if (mActiveTracks.isEmpty()) {
Elliott Hughes87cebad2014-05-22 10:14:43 -07007901 dprintf(fd, " No active record clients\n");
Eric Laurent81784c32012-11-19 14:55:58 -08007902 }
Andy Hungbfa64962017-06-12 14:43:19 -07007903
7904 if (input != nullptr) {
7905 dprintf(fd, " Hal stream dump:\n");
7906 (void)input->stream->dump(fd);
7907 }
7908
Glenn Kasten6e6704c2014-07-03 10:20:00 -07007909 dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no");
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07007910 dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no");
Glenn Kasten17c9c992015-03-02 15:53:01 -08007911
Glenn Kasten2f90c512015-12-02 11:40:09 -08007912 // Make a non-atomic copy of fast capture dump state so it won't change underneath us
7913 // while we are dumping it. It may be inconsistent, but it won't mutate!
7914 // This is a large object so we place it on the heap.
7915 // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
Eric Tan2942a4e2018-09-12 17:41:27 -07007916 const std::unique_ptr<FastCaptureDumpState> copy =
7917 std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState);
Glenn Kasten2f90c512015-12-02 11:40:09 -08007918 copy->dump(fd);
Eric Laurent81784c32012-11-19 14:55:58 -08007919}
7920
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07007921void AudioFlinger::RecordThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08007922{
Eric Laurent81784c32012-11-19 14:55:58 -08007923 String8 result;
Marco Nelissenb2208842014-02-07 14:00:50 -08007924 size_t numtracks = mTracks.size();
7925 size_t numactive = mActiveTracks.size();
7926 size_t numactiveseen = 0;
Glenn Kastenc42e9b42016-03-21 11:35:03 -07007927 dprintf(fd, " %zu Tracks", numtracks);
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007928 const char *prefix = " ";
Marco Nelissenb2208842014-02-07 14:00:50 -08007929 if (numtracks) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07007930 dprintf(fd, " of which %zu are active\n", numactive);
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007931 result.append(prefix);
Andy Hung000adb52018-06-01 15:43:26 -07007932 mTracks[0]->appendDumpHeader(result);
Marco Nelissenb2208842014-02-07 14:00:50 -08007933 for (size_t i = 0; i < numtracks ; ++i) {
7934 sp<RecordTrack> track = mTracks[i];
7935 if (track != 0) {
7936 bool active = mActiveTracks.indexOf(track) >= 0;
7937 if (active) {
7938 numactiveseen++;
7939 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007940 result.append(prefix);
7941 track->appendDump(result, active);
Marco Nelissenb2208842014-02-07 14:00:50 -08007942 }
Eric Laurent81784c32012-11-19 14:55:58 -08007943 }
Marco Nelissenb2208842014-02-07 14:00:50 -08007944 } else {
Elliott Hughes87cebad2014-05-22 10:14:43 -07007945 dprintf(fd, "\n");
Eric Laurent81784c32012-11-19 14:55:58 -08007946 }
7947
Marco Nelissenb2208842014-02-07 14:00:50 -08007948 if (numactiveseen != numactive) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007949 result.append(" The following tracks are in the active list but"
Marco Nelissenb2208842014-02-07 14:00:50 -08007950 " not in the track list\n");
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007951 result.append(prefix);
Andy Hung000adb52018-06-01 15:43:26 -07007952 mActiveTracks[0]->appendDumpHeader(result);
Marco Nelissenb2208842014-02-07 14:00:50 -08007953 for (size_t i = 0; i < numactive; ++i) {
Glenn Kasten2b806402013-11-20 16:37:38 -08007954 sp<RecordTrack> track = mActiveTracks[i];
Marco Nelissenb2208842014-02-07 14:00:50 -08007955 if (mTracks.indexOf(track) < 0) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -07007956 result.append(prefix);
7957 track->appendDump(result, true /* active */);
Marco Nelissenb2208842014-02-07 14:00:50 -08007958 }
Glenn Kasten2b806402013-11-20 16:37:38 -08007959 }
Eric Laurent81784c32012-11-19 14:55:58 -08007960
7961 }
7962 write(fd, result.string(), result.size());
7963}
7964
Svet Ganovf4ddfef2018-01-16 07:37:58 -08007965void AudioFlinger::RecordThread::setRecordSilenced(uid_t uid, bool silenced)
7966{
7967 Mutex::Autolock _l(mLock);
7968 for (size_t i = 0; i < mTracks.size() ; i++) {
7969 sp<RecordTrack> track = mTracks[i];
7970 if (track != 0 && track->uid() == uid) {
7971 track->setSilenced(silenced);
7972 }
7973 }
7974}
Andy Hung73c02e42015-03-29 01:13:58 -07007975
7976void AudioFlinger::RecordThread::ResamplerBufferProvider::reset()
7977{
7978 sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
7979 RecordThread *recordThread = (RecordThread *) threadBase.get();
7980 mRsmpInFront = recordThread->mRsmpInRear;
7981 mRsmpInUnrel = 0;
7982}
7983
7984void AudioFlinger::RecordThread::ResamplerBufferProvider::sync(
7985 size_t *framesAvailable, bool *hasOverrun)
7986{
7987 sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
7988 RecordThread *recordThread = (RecordThread *) threadBase.get();
7989 const int32_t rear = recordThread->mRsmpInRear;
7990 const int32_t front = mRsmpInFront;
Hongwei Wang95e37682019-04-12 11:13:36 -07007991 const ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
Andy Hung73c02e42015-03-29 01:13:58 -07007992
7993 size_t framesIn;
7994 bool overrun = false;
7995 if (filled < 0) {
7996 // should not happen, but treat like a massive overrun and re-sync
7997 framesIn = 0;
7998 mRsmpInFront = rear;
7999 overrun = true;
8000 } else if ((size_t) filled <= recordThread->mRsmpInFrames) {
8001 framesIn = (size_t) filled;
8002 } else {
8003 // client is not keeping up with server, but give it latest data
8004 framesIn = recordThread->mRsmpInFrames;
Hongwei Wang95e37682019-04-12 11:13:36 -07008005 mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow(
8006 rear, static_cast<int32_t>(framesIn));
Andy Hung73c02e42015-03-29 01:13:58 -07008007 overrun = true;
8008 }
8009 if (framesAvailable != NULL) {
8010 *framesAvailable = framesIn;
8011 }
8012 if (hasOverrun != NULL) {
8013 *hasOverrun = overrun;
8014 }
8015}
8016
Eric Laurent81784c32012-11-19 14:55:58 -08008017// AudioBufferProvider interface
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008018status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08008019 AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08008020{
Andy Hung73c02e42015-03-29 01:13:58 -07008021 sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008022 if (threadBase == 0) {
8023 buffer->frameCount = 0;
Glenn Kasten607fa3e2014-02-21 14:24:58 -08008024 buffer->raw = NULL;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008025 return NOT_ENOUGH_DATA;
8026 }
8027 RecordThread *recordThread = (RecordThread *) threadBase.get();
8028 int32_t rear = recordThread->mRsmpInRear;
Andy Hung73c02e42015-03-29 01:13:58 -07008029 int32_t front = mRsmpInFront;
Hongwei Wang95e37682019-04-12 11:13:36 -07008030 ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008031 // FIXME should not be P2 (don't want to increase latency)
8032 // FIXME if client not keeping up, discard
Glenn Kasten607fa3e2014-02-21 14:24:58 -08008033 LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames));
Glenn Kasten85948432013-08-19 12:09:05 -07008034 // 'filled' may be non-contiguous, so return only the first contiguous chunk
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008035 front &= recordThread->mRsmpInFramesP2 - 1;
8036 size_t part1 = recordThread->mRsmpInFramesP2 - front;
Glenn Kasten85948432013-08-19 12:09:05 -07008037 if (part1 > (size_t) filled) {
8038 part1 = filled;
8039 }
8040 size_t ask = buffer->frameCount;
8041 ALOG_ASSERT(ask > 0);
8042 if (part1 > ask) {
8043 part1 = ask;
8044 }
8045 if (part1 == 0) {
Andy Hung73c02e42015-03-29 01:13:58 -07008046 // out of data is fine since the resampler will return a short-count.
Glenn Kasten85948432013-08-19 12:09:05 -07008047 buffer->raw = NULL;
8048 buffer->frameCount = 0;
Andy Hung73c02e42015-03-29 01:13:58 -07008049 mRsmpInUnrel = 0;
Glenn Kasten85948432013-08-19 12:09:05 -07008050 return NOT_ENOUGH_DATA;
Eric Laurent81784c32012-11-19 14:55:58 -08008051 }
8052
Andy Hung57446612015-04-19 23:56:46 -07008053 buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize;
Glenn Kasten85948432013-08-19 12:09:05 -07008054 buffer->frameCount = part1;
Andy Hung73c02e42015-03-29 01:13:58 -07008055 mRsmpInUnrel = part1;
Eric Laurent81784c32012-11-19 14:55:58 -08008056 return NO_ERROR;
8057}
8058
8059// AudioBufferProvider interface
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008060void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer(
8061 AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08008062{
Hongwei Wang95e37682019-04-12 11:13:36 -07008063 int32_t stepCount = static_cast<int32_t>(buffer->frameCount);
Glenn Kasten85948432013-08-19 12:09:05 -07008064 if (stepCount == 0) {
8065 return;
8066 }
Andy Hung73c02e42015-03-29 01:13:58 -07008067 ALOG_ASSERT(stepCount <= mRsmpInUnrel);
8068 mRsmpInUnrel -= stepCount;
Hongwei Wang95e37682019-04-12 11:13:36 -07008069 mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount);
Glenn Kasten85948432013-08-19 12:09:05 -07008070 buffer->raw = NULL;
Eric Laurent81784c32012-11-19 14:55:58 -08008071 buffer->frameCount = 0;
8072}
8073
Eric Laurentd8365c52017-07-16 15:27:05 -07008074void AudioFlinger::RecordThread::checkBtNrec()
8075{
8076 Mutex::Autolock _l(mLock);
8077 checkBtNrec_l();
8078}
8079
8080void AudioFlinger::RecordThread::checkBtNrec_l()
8081{
8082 // disable AEC and NS if the device is a BT SCO headset supporting those
8083 // pre processings
jiabin10d86fd2019-10-31 17:20:42 -07008084 bool suspend = audio_is_bluetooth_sco_device(inDeviceType()) &&
Eric Laurentd8365c52017-07-16 15:27:05 -07008085 mAudioFlinger->btNrecIsOff();
8086 if (mBtNrecSuspended.exchange(suspend) != suspend) {
8087 for (size_t i = 0; i < mEffectChains.size(); i++) {
8088 setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId());
8089 setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId());
8090 }
8091 }
8092}
8093
Andy Hung97a893e2015-03-29 01:03:07 -07008094
Eric Laurent10351942014-05-08 18:49:52 -07008095bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair,
8096 status_t& status)
Eric Laurent81784c32012-11-19 14:55:58 -08008097{
8098 bool reconfig = false;
8099
Eric Laurent10351942014-05-08 18:49:52 -07008100 status = NO_ERROR;
Eric Laurent81784c32012-11-19 14:55:58 -08008101
Eric Laurent10351942014-05-08 18:49:52 -07008102 audio_format_t reqFormat = mFormat;
8103 uint32_t samplingRate = mSampleRate;
Glenn Kastene1635ec2015-06-08 15:46:49 -07008104 // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs).
Eric Laurent10351942014-05-08 18:49:52 -07008105 audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount);
8106
8107 AudioParameter param = AudioParameter(keyValuePair);
8108 int value;
Haynes Mathew George9ce67b52015-09-30 11:40:47 -07008109
8110 // scope for AutoPark extends to end of method
8111 AutoPark<FastCapture> park(mFastCapture);
8112
Eric Laurent10351942014-05-08 18:49:52 -07008113 // TODO Investigate when this code runs. Check with audio policy when a sample rate and
8114 // channel count change can be requested. Do we mandate the first client defines the
8115 // HAL sampling rate and channel count or do we allow changes on the fly?
8116 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
8117 samplingRate = value;
8118 reconfig = true;
8119 }
8120 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Andy Hung97a893e2015-03-29 01:03:07 -07008121 if (!audio_is_linear_pcm((audio_format_t) value)) {
Eric Laurent10351942014-05-08 18:49:52 -07008122 status = BAD_VALUE;
8123 } else {
8124 reqFormat = (audio_format_t) value;
Eric Laurent81784c32012-11-19 14:55:58 -08008125 reconfig = true;
8126 }
Eric Laurent10351942014-05-08 18:49:52 -07008127 }
8128 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
8129 audio_channel_mask_t mask = (audio_channel_mask_t) value;
Andy Hungd330ee42015-04-20 13:23:41 -07008130 if (!audio_is_input_channel(mask) ||
8131 audio_channel_count_from_in_mask(mask) > FCC_8) {
Eric Laurent10351942014-05-08 18:49:52 -07008132 status = BAD_VALUE;
8133 } else {
8134 channelMask = mask;
8135 reconfig = true;
Eric Laurent81784c32012-11-19 14:55:58 -08008136 }
Eric Laurent10351942014-05-08 18:49:52 -07008137 }
8138 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
8139 // do not accept frame count changes if tracks are open as the track buffer
8140 // size depends on frame count and correct behavior would not be guaranteed
8141 // if frame count is changed after track creation
8142 if (mActiveTracks.size() > 0) {
8143 status = INVALID_OPERATION;
8144 } else {
8145 reconfig = true;
Eric Laurent81784c32012-11-19 14:55:58 -08008146 }
Eric Laurent10351942014-05-08 18:49:52 -07008147 }
8148 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
jiabin10d86fd2019-10-31 17:20:42 -07008149 LOG_FATAL("Should not set routing device in RecordThread");
Eric Laurent10351942014-05-08 18:49:52 -07008150 }
8151 if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR &&
8152 mAudioSource != (audio_source_t)value) {
jiabin10d86fd2019-10-31 17:20:42 -07008153 LOG_FATAL("Should not set audio source in RecordThread");
Eric Laurent10351942014-05-08 18:49:52 -07008154 }
Glenn Kastene198c362013-08-13 09:13:36 -07008155
Eric Laurent10351942014-05-08 18:49:52 -07008156 if (status == NO_ERROR) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008157 status = mInput->stream->setParameters(keyValuePair);
Eric Laurent10351942014-05-08 18:49:52 -07008158 if (status == INVALID_OPERATION) {
8159 inputStandBy();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008160 status = mInput->stream->setParameters(keyValuePair);
Eric Laurent10351942014-05-08 18:49:52 -07008161 }
8162 if (reconfig) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008163 if (status == BAD_VALUE) {
8164 uint32_t sRate;
8165 audio_channel_mask_t channelMask;
8166 audio_format_t format;
8167 if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK &&
8168 audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) &&
8169 sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) &&
8170 audio_channel_count_from_in_mask(channelMask) <= FCC_8) {
8171 status = NO_ERROR;
8172 }
Eric Laurent81784c32012-11-19 14:55:58 -08008173 }
Eric Laurent10351942014-05-08 18:49:52 -07008174 if (status == NO_ERROR) {
8175 readInputParameters_l();
Eric Laurent73e26b62015-04-27 16:55:58 -07008176 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
Eric Laurent81784c32012-11-19 14:55:58 -08008177 }
8178 }
Eric Laurent81784c32012-11-19 14:55:58 -08008179 }
Eric Laurent10351942014-05-08 18:49:52 -07008180
Eric Laurent81784c32012-11-19 14:55:58 -08008181 return reconfig;
8182}
8183
8184String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
8185{
Eric Laurent81784c32012-11-19 14:55:58 -08008186 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008187 if (initCheck() == NO_ERROR) {
8188 String8 out_s8;
8189 if (mInput->stream->getParameters(keys, &out_s8) == OK) {
8190 return out_s8;
8191 }
Eric Laurent81784c32012-11-19 14:55:58 -08008192 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008193 return String8();
Eric Laurent81784c32012-11-19 14:55:58 -08008194}
8195
Eric Laurent09f1ed22019-04-24 17:45:17 -07008196void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
8197 audio_port_handle_t portId) {
Eric Laurent73e26b62015-04-27 16:55:58 -07008198 sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
8199
8200 desc->mIoHandle = mId;
Eric Laurent81784c32012-11-19 14:55:58 -08008201
8202 switch (event) {
Eric Laurent73e26b62015-04-27 16:55:58 -07008203 case AUDIO_INPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -07008204 case AUDIO_INPUT_REGISTERED:
Eric Laurent73e26b62015-04-27 16:55:58 -07008205 case AUDIO_INPUT_CONFIG_CHANGED:
Eric Laurent296fb132015-05-01 11:38:42 -07008206 desc->mPatch = mPatch;
Eric Laurent73e26b62015-04-27 16:55:58 -07008207 desc->mChannelMask = mChannelMask;
8208 desc->mSamplingRate = mSampleRate;
8209 desc->mFormat = mFormat;
8210 desc->mFrameCount = mFrameCount;
Glenn Kasten4a8308b2016-04-18 14:10:01 -07008211 desc->mFrameCountHAL = mFrameCount;
Eric Laurent73e26b62015-04-27 16:55:58 -07008212 desc->mLatency = 0;
Eric Laurent81784c32012-11-19 14:55:58 -08008213 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -07008214 case AUDIO_CLIENT_STARTED:
8215 desc->mPatch = mPatch;
8216 desc->mPortId = portId;
8217 break;
Eric Laurent73e26b62015-04-27 16:55:58 -07008218 case AUDIO_INPUT_CLOSED:
Eric Laurent81784c32012-11-19 14:55:58 -08008219 default:
8220 break;
8221 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07008222 mAudioFlinger->ioConfigChanged(event, desc, pid);
Eric Laurent81784c32012-11-19 14:55:58 -08008223}
8224
Glenn Kastendeca2ae2014-02-07 10:25:56 -08008225void AudioFlinger::RecordThread::readInputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -08008226{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008227 status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
8228 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
Andy Hung463be252014-07-10 16:56:07 -07008229 mFormat = mHALFormat;
Mikhail Naganovce9f2652018-07-16 11:09:24 -07008230 mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
8231 if (audio_is_linear_pcm(mFormat)) {
8232 LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_8, "HAL channel count %d > %d",
8233 mChannelCount, FCC_8);
8234 } else {
8235 // Can have more that FCC_8 channels in encoded streams.
8236 ALOGI("HAL format %#x is not linear pcm", mFormat);
8237 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008238 result = mInput->stream->getFrameSize(&mFrameSize);
8239 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
8240 result = mInput->stream->getBufferSize(&mBufferSize);
8241 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
Glenn Kasten548efc92012-11-29 08:48:51 -08008242 mFrameCount = mBufferSize / mFrameSize;
Mikhail Naganovb3cf7e62017-06-02 10:24:24 -07008243 ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, "
8244 "mBufferSize=%lld, mFrameCount=%lld",
8245 this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize,
8246 (long long)mFrameCount);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008247 // This is the formula for calculating the temporary buffer size.
Glenn Kastene8426142014-02-28 16:45:03 -08008248 // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to
Glenn Kasten85948432013-08-19 12:09:05 -07008249 // 1 full output buffer, regardless of the alignment of the available input.
Glenn Kastene8426142014-02-28 16:45:03 -08008250 // The value is somewhat arbitrary, and could probably be even larger.
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08008251 // A larger value should allow more old data to be read after a track calls start(),
8252 // without increasing latency.
Andy Hung97a893e2015-03-29 01:03:07 -07008253 //
8254 // Note this is independent of the maximum downsampling ratio permitted for capture.
Glenn Kastene8426142014-02-28 16:45:03 -08008255 mRsmpInFrames = mFrameCount * 7;
Glenn Kasten85948432013-08-19 12:09:05 -07008256 mRsmpInFramesP2 = roundup(mRsmpInFrames);
Andy Hung57446612015-04-19 23:56:46 -07008257 free(mRsmpInBuffer);
Andy Hung0a01c2f2015-09-21 12:44:54 -07008258 mRsmpInBuffer = NULL;
Glenn Kasten49d00ad2014-07-21 11:22:03 -07008259
8260 // TODO optimize audio capture buffer sizes ...
8261 // Here we calculate the size of the sliding buffer used as a source
8262 // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7).
8263 // For current HAL frame counts, this is usually 2048 = 40 ms. It would
8264 // be better to have it derived from the pipe depth in the long term.
8265 // The current value is higher than necessary. However it should not add to latency.
8266
Glenn Kasten85948432013-08-19 12:09:05 -07008267 // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer
Glenn Kasten1b291842016-07-18 14:55:21 -07008268 mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1;
8269 (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize);
Glenn Kastend3bb6452016-12-05 18:14:37 -08008270 // if posix_memalign fails, will segv here.
8271 memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08008272
Glenn Kasten4cc0a6a2014-02-17 14:31:46 -08008273 // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints.
8274 // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks?
Eric Laurent81784c32012-11-19 14:55:58 -08008275}
8276
Glenn Kasten5f972c02014-01-13 09:59:31 -08008277uint32_t AudioFlinger::RecordThread::getInputFramesLost()
Eric Laurent81784c32012-11-19 14:55:58 -08008278{
8279 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008280 uint32_t result;
8281 if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) {
8282 return result;
Eric Laurent81784c32012-11-19 14:55:58 -08008283 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008284 return 0;
Eric Laurent81784c32012-11-19 14:55:58 -08008285}
8286
Glenn Kastend848eb42016-03-08 13:42:11 -08008287KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const
Eric Laurent81784c32012-11-19 14:55:58 -08008288{
Glenn Kastend848eb42016-03-08 13:42:11 -08008289 KeyedVector<audio_session_t, bool> ids;
Eric Laurent81784c32012-11-19 14:55:58 -08008290 Mutex::Autolock _l(mLock);
8291 for (size_t j = 0; j < mTracks.size(); ++j) {
8292 sp<RecordThread::RecordTrack> track = mTracks[j];
Glenn Kastend848eb42016-03-08 13:42:11 -08008293 audio_session_t sessionId = track->sessionId();
Eric Laurent81784c32012-11-19 14:55:58 -08008294 if (ids.indexOfKey(sessionId) < 0) {
8295 ids.add(sessionId, true);
8296 }
8297 }
8298 return ids;
8299}
8300
8301AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
8302{
8303 Mutex::Autolock _l(mLock);
8304 AudioStreamIn *input = mInput;
8305 mInput = NULL;
8306 return input;
8307}
8308
8309// this method must always be called either with ThreadBase mLock held or inside the thread loop
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008310sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const
Eric Laurent81784c32012-11-19 14:55:58 -08008311{
8312 if (mInput == NULL) {
8313 return NULL;
8314 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008315 return mInput->stream;
Eric Laurent81784c32012-11-19 14:55:58 -08008316}
8317
8318status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
8319{
Eric Laurent81784c32012-11-19 14:55:58 -08008320 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
Eric Laurentaaa44472014-09-12 17:41:50 -07008321 chain->setThread(this);
Eric Laurent81784c32012-11-19 14:55:58 -08008322 chain->setInBuffer(NULL);
8323 chain->setOutBuffer(NULL);
8324
8325 checkSuspendOnAddEffectChain_l(chain);
8326
Eric Laurent1b928682014-10-02 19:41:47 -07008327 // make sure enabled pre processing effects state is communicated to the HAL as we
8328 // just moved them to a new input stream.
8329 chain->syncHalEffectsState();
8330
Eric Laurent81784c32012-11-19 14:55:58 -08008331 mEffectChains.add(chain);
8332
8333 return NO_ERROR;
8334}
8335
8336size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
8337{
8338 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
Eric Laurentb20cf7d2019-04-05 19:37:34 -07008339
8340 for (size_t i = 0; i < mEffectChains.size(); i++) {
8341 if (chain == mEffectChains[i]) {
8342 mEffectChains.removeAt(i);
8343 break;
8344 }
Eric Laurent81784c32012-11-19 14:55:58 -08008345 }
Eric Laurentb20cf7d2019-04-05 19:37:34 -07008346 return mEffectChains.size();
Eric Laurent81784c32012-11-19 14:55:58 -08008347}
8348
Eric Laurent1c333e22014-05-20 10:48:17 -07008349status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch,
8350 audio_patch_handle_t *handle)
8351{
8352 status_t status = NO_ERROR;
Eric Laurent054d9d32015-04-24 08:48:48 -07008353
8354 // store new device and send to effects
jiabin10d86fd2019-10-31 17:20:42 -07008355 mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type;
8356 mInDeviceTypeAddr.mAddress = patch->sources[0].ext.device.address;
François Gaffie0c280aa2018-07-25 10:02:15 +02008357 audio_port_handle_t deviceId = patch->sources[0].id;
Eric Laurent054d9d32015-04-24 08:48:48 -07008358 for (size_t i = 0; i < mEffectChains.size(); i++) {
jiabinb8269fd2019-11-11 12:16:27 -08008359 mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr());
Eric Laurent054d9d32015-04-24 08:48:48 -07008360 }
8361
Eric Laurentd8365c52017-07-16 15:27:05 -07008362 checkBtNrec_l();
Eric Laurent054d9d32015-04-24 08:48:48 -07008363
8364 // store new source and send to effects
8365 if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
8366 mAudioSource = patch->sinks[0].ext.mix.usecase.source;
Eric Laurent1c333e22014-05-20 10:48:17 -07008367 for (size_t i = 0; i < mEffectChains.size(); i++) {
Eric Laurent054d9d32015-04-24 08:48:48 -07008368 mEffectChains[i]->setAudioSource_l(mAudioSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07008369 }
Eric Laurent054d9d32015-04-24 08:48:48 -07008370 }
Eric Laurent1c333e22014-05-20 10:48:17 -07008371
Mikhail Naganov9ee05402016-10-13 15:58:17 -07008372 if (mInput->audioHwDev->supportsAudioPatches()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07008373 sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
8374 status = hwDevice->createAudioPatch(patch->num_sources,
8375 patch->sources,
8376 patch->num_sinks,
8377 patch->sinks,
8378 handle);
Eric Laurent1c333e22014-05-20 10:48:17 -07008379 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -07008380 char *address;
8381 if (strcmp(patch->sources[0].ext.device.address, "") != 0) {
8382 address = audio_device_address_to_parameter(
8383 patch->sources[0].ext.device.type,
8384 patch->sources[0].ext.device.address);
8385 } else {
8386 address = (char *)calloc(1, 1);
8387 }
8388 AudioParameter param = AudioParameter(String8(address));
8389 free(address);
Mikhail Naganov00260b52016-10-13 12:54:24 -07008390 param.addInt(String8(AudioParameter::keyRouting),
Eric Laurent054d9d32015-04-24 08:48:48 -07008391 (int)patch->sources[0].ext.device.type);
Mikhail Naganov00260b52016-10-13 12:54:24 -07008392 param.addInt(String8(AudioParameter::keyInputSource),
Eric Laurent054d9d32015-04-24 08:48:48 -07008393 (int)patch->sinks[0].ext.mix.usecase.source);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008394 status = mInput->stream->setParameters(param.toString());
Eric Laurent054d9d32015-04-24 08:48:48 -07008395 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent1c333e22014-05-20 10:48:17 -07008396 }
Eric Laurent054d9d32015-04-24 08:48:48 -07008397
jiabin10d86fd2019-10-31 17:20:42 -07008398 if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) {
Eric Laurente8726fe2015-06-26 09:39:24 -07008399 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
jiabin10d86fd2019-10-31 17:20:42 -07008400 mPatch = *patch;
Eric Laurente8726fe2015-06-26 09:39:24 -07008401 }
Eric Laurent296fb132015-05-01 11:38:42 -07008402
Eric Laurent1c333e22014-05-20 10:48:17 -07008403 return status;
8404}
8405
8406status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
8407{
8408 status_t status = NO_ERROR;
Eric Laurent054d9d32015-04-24 08:48:48 -07008409
jiabin10d86fd2019-10-31 17:20:42 -07008410 mPatch = audio_patch{};
8411 mInDeviceTypeAddr.reset();
Eric Laurent054d9d32015-04-24 08:48:48 -07008412
Mikhail Naganov9ee05402016-10-13 15:58:17 -07008413 if (mInput->audioHwDev->supportsAudioPatches()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07008414 sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
8415 status = hwDevice->releaseAudioPatch(handle);
Eric Laurent1c333e22014-05-20 10:48:17 -07008416 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -07008417 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07008418 param.addInt(String8(AudioParameter::keyRouting), 0);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07008419 status = mInput->stream->setParameters(param.toString());
Eric Laurent1c333e22014-05-20 10:48:17 -07008420 }
8421 return status;
8422}
8423
jiabin10d86fd2019-10-31 17:20:42 -07008424void AudioFlinger::RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices)
8425{
8426 mOutDevices = outDevices;
8427 mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices);
8428 for (size_t i = 0; i < mEffectChains.size(); i++) {
jiabinb8269fd2019-11-11 12:16:27 -08008429 mEffectChains[i]->setDevices_l(outDeviceTypeAddrs());
jiabin10d86fd2019-10-31 17:20:42 -07008430 }
8431}
8432
Mikhail Naganov444ecc32018-05-01 17:40:05 -07008433void AudioFlinger::RecordThread::addPatchTrack(const sp<PatchRecord>& record)
Eric Laurent83b88082014-06-20 18:31:16 -07008434{
8435 Mutex::Autolock _l(mLock);
8436 mTracks.add(record);
Mikhail Naganovaf288872019-09-25 13:05:02 -07008437 if (record->getSource()) {
8438 mSource = record->getSource();
8439 }
Eric Laurent83b88082014-06-20 18:31:16 -07008440}
8441
Mikhail Naganov444ecc32018-05-01 17:40:05 -07008442void AudioFlinger::RecordThread::deletePatchTrack(const sp<PatchRecord>& record)
Eric Laurent83b88082014-06-20 18:31:16 -07008443{
8444 Mutex::Autolock _l(mLock);
Mikhail Naganovaf288872019-09-25 13:05:02 -07008445 if (mSource == record->getSource()) {
8446 mSource = mInput;
8447 }
Eric Laurent83b88082014-06-20 18:31:16 -07008448 destroyTrack_l(record);
8449}
8450
Mikhail Naganovdc769682018-05-04 15:34:08 -07008451void AudioFlinger::RecordThread::toAudioPortConfig(struct audio_port_config *config)
Eric Laurent83b88082014-06-20 18:31:16 -07008452{
Mikhail Naganovdc769682018-05-04 15:34:08 -07008453 ThreadBase::toAudioPortConfig(config);
Eric Laurent83b88082014-06-20 18:31:16 -07008454 config->role = AUDIO_PORT_ROLE_SINK;
8455 config->ext.mix.hw_module = mInput->audioHwDev->handle();
8456 config->ext.mix.usecase.source = mAudioSource;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07008457 if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
8458 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
8459 config->flags.input = mInput->flags;
8460 }
Eric Laurent83b88082014-06-20 18:31:16 -07008461}
Eric Laurent1c333e22014-05-20 10:48:17 -07008462
Eric Laurent6acd1d42017-01-04 14:23:29 -08008463// ----------------------------------------------------------------------------
8464// Mmap
8465// ----------------------------------------------------------------------------
8466
8467AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread)
8468 : mThread(thread)
8469{
Phil Burk9fabbf82017-08-03 12:02:00 -07008470 assert(thread != 0); // thread must start non-null and stay non-null
Eric Laurent6acd1d42017-01-04 14:23:29 -08008471}
8472
8473AudioFlinger::MmapThreadHandle::~MmapThreadHandle()
8474{
Phil Burk9fabbf82017-08-03 12:02:00 -07008475 mThread->disconnect();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008476}
8477
8478status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames,
8479 struct audio_mmap_buffer_info *info)
8480{
Eric Laurent6acd1d42017-01-04 14:23:29 -08008481 return mThread->createMmapBuffer(minSizeFrames, info);
8482}
8483
8484status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position)
8485{
Eric Laurent6acd1d42017-01-04 14:23:29 -08008486 return mThread->getMmapPosition(position);
8487}
8488
Eric Laurenta54f1282017-07-01 19:39:32 -07008489status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client,
Glenn Kastend3bb6452016-12-05 18:14:37 -08008490 audio_port_handle_t *handle)
Eric Laurent6acd1d42017-01-04 14:23:29 -08008491
8492{
Eric Laurent6acd1d42017-01-04 14:23:29 -08008493 return mThread->start(client, handle);
8494}
8495
8496status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle)
8497{
Eric Laurent6acd1d42017-01-04 14:23:29 -08008498 return mThread->stop(handle);
8499}
8500
Eric Laurent18b57012017-02-13 16:23:52 -08008501status_t AudioFlinger::MmapThreadHandle::standby()
8502{
Eric Laurent18b57012017-02-13 16:23:52 -08008503 return mThread->standby();
8504}
8505
Eric Laurent6acd1d42017-01-04 14:23:29 -08008506
8507AudioFlinger::MmapThread::MmapThread(
8508 const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
jiabin10d86fd2019-10-31 17:20:42 -07008509 AudioHwDevice *hwDev, sp<StreamHalInterface> stream, bool systemReady)
8510 : ThreadBase(audioFlinger, id, MMAP, systemReady),
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07008511 mSessionId(AUDIO_SESSION_NONE),
François Gaffie0c280aa2018-07-25 10:02:15 +02008512 mPortId(AUDIO_PORT_HANDLE_NONE),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07008513 mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev),
Eric Laurent67f97292018-04-20 18:05:41 -07008514 mActiveTracks(&this->mLocalLog),
8515 mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later.
8516 mNoCallbackWarningCount(0)
Eric Laurent6acd1d42017-01-04 14:23:29 -08008517{
Eric Laurent18b57012017-02-13 16:23:52 -08008518 mStandby = true;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008519 readHalParameters_l();
8520}
8521
8522AudioFlinger::MmapThread::~MmapThread()
8523{
Eric Laurent18b57012017-02-13 16:23:52 -08008524 releaseWakeLock_l();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008525}
8526
8527void AudioFlinger::MmapThread::onFirstRef()
8528{
8529 run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
8530}
8531
8532void AudioFlinger::MmapThread::disconnect()
8533{
Eric Laurent331679c2018-04-16 17:03:16 -07008534 ActiveTracks<MmapTrack> activeTracks;
8535 {
8536 Mutex::Autolock _l(mLock);
8537 for (const sp<MmapTrack> &t : mActiveTracks) {
8538 activeTracks.add(t);
8539 }
8540 }
8541 for (const sp<MmapTrack> &t : activeTracks) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08008542 stop(t->portId());
8543 }
Phil Burk9fabbf82017-08-03 12:02:00 -07008544 // This will decrement references and may cause the destruction of this thread.
Eric Laurent6acd1d42017-01-04 14:23:29 -08008545 if (isOutput()) {
Eric Laurentd7fe0862018-07-14 16:48:01 -07008546 AudioSystem::releaseOutput(mPortId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008547 } else {
Eric Laurentfee19762018-01-29 18:44:13 -08008548 AudioSystem::releaseInput(mPortId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008549 }
8550}
8551
8552
8553void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr,
8554 audio_stream_type_t streamType __unused,
8555 audio_session_t sessionId,
8556 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07008557 audio_port_handle_t deviceId,
Eric Laurent6acd1d42017-01-04 14:23:29 -08008558 audio_port_handle_t portId)
8559{
8560 mAttr = *attr;
8561 mSessionId = sessionId;
8562 mCallback = callback;
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07008563 mDeviceId = deviceId;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008564 mPortId = portId;
8565}
8566
8567status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames,
8568 struct audio_mmap_buffer_info *info)
8569{
8570 if (mHalStream == 0) {
8571 return NO_INIT;
8572 }
Eric Laurent18b57012017-02-13 16:23:52 -08008573 mStandby = true;
8574 acquireWakeLock();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008575 return mHalStream->createMmapBuffer(minSizeFrames, info);
8576}
8577
8578status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position)
8579{
8580 if (mHalStream == 0) {
8581 return NO_INIT;
8582 }
8583 return mHalStream->getMmapPosition(position);
8584}
8585
Eric Laurent331679c2018-04-16 17:03:16 -07008586status_t AudioFlinger::MmapThread::exitStandby()
8587{
8588 status_t ret = mHalStream->start();
8589 if (ret != NO_ERROR) {
8590 ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret);
8591 return ret;
8592 }
8593 mStandby = false;
8594 return NO_ERROR;
8595}
8596
Eric Laurenta54f1282017-07-01 19:39:32 -07008597status_t AudioFlinger::MmapThread::start(const AudioClient& client,
Eric Laurent6acd1d42017-01-04 14:23:29 -08008598 audio_port_handle_t *handle)
8599{
Eric Laurenta54f1282017-07-01 19:39:32 -07008600 ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__,
8601 client.clientUid, mStandby, mPortId, *handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008602 if (mHalStream == 0) {
8603 return NO_INIT;
8604 }
8605
8606 status_t ret;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008607
Eric Laurenta54f1282017-07-01 19:39:32 -07008608 if (*handle == mPortId) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08008609 // for the first track, reuse portId and session allocated when the stream was opened
Eric Laurent331679c2018-04-16 17:03:16 -07008610 return exitStandby();
Eric Laurenta54f1282017-07-01 19:39:32 -07008611 }
8612
8613 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
8614
8615 audio_io_handle_t io = mId;
8616 if (isOutput()) {
8617 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
8618 config.sample_rate = mSampleRate;
8619 config.channel_mask = mChannelMask;
8620 config.format = mFormat;
8621 audio_stream_type_t stream = streamType();
8622 audio_output_flags_t flags =
8623 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07008624 audio_port_handle_t deviceId = mDeviceId;
Kevin Rocard153f92d2018-12-18 18:33:28 -08008625 std::vector<audio_io_handle_t> secondaryOutputs;
Eric Laurenta54f1282017-07-01 19:39:32 -07008626 ret = AudioSystem::getOutputForAttr(&mAttr, &io,
8627 mSessionId,
8628 &stream,
Nadav Bar766fb022018-01-07 12:18:03 +02008629 client.clientPid,
Eric Laurenta54f1282017-07-01 19:39:32 -07008630 client.clientUid,
8631 &config,
8632 flags,
8633 &deviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -08008634 &portId,
8635 &secondaryOutputs);
8636 ALOGD_IF(!secondaryOutputs.empty(),
8637 "MmapThread::start does not support secondary outputs, ignoring them");
Eric Laurent6acd1d42017-01-04 14:23:29 -08008638 } else {
Eric Laurenta54f1282017-07-01 19:39:32 -07008639 audio_config_base_t config;
8640 config.sample_rate = mSampleRate;
8641 config.channel_mask = mChannelMask;
8642 config.format = mFormat;
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07008643 audio_port_handle_t deviceId = mDeviceId;
Eric Laurenta54f1282017-07-01 19:39:32 -07008644 ret = AudioSystem::getInputForAttr(&mAttr, &io,
Mikhail Naganov2996f672019-04-18 12:29:59 -07008645 RECORD_RIID_INVALID,
Eric Laurenta54f1282017-07-01 19:39:32 -07008646 mSessionId,
8647 client.clientPid,
8648 client.clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -08008649 client.packageName,
Eric Laurenta54f1282017-07-01 19:39:32 -07008650 &config,
8651 AUDIO_INPUT_FLAG_MMAP_NOIRQ,
8652 &deviceId,
8653 &portId);
8654 }
8655 // APM should not chose a different input or output stream for the same set of attributes
8656 // and audo configuration
8657 if (ret != NO_ERROR || io != mId) {
8658 ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)",
8659 __FUNCTION__, ret, io, mId);
8660 return BAD_VALUE;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008661 }
8662
8663 if (isOutput()) {
Eric Laurentd7fe0862018-07-14 16:48:01 -07008664 ret = AudioSystem::startOutput(portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008665 } else {
Eric Laurent4eb58f12018-12-07 16:41:02 -08008666 ret = AudioSystem::startInput(portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008667 }
8668
Eric Laurent331679c2018-04-16 17:03:16 -07008669 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008670 // abort if start is rejected by audio policy manager
8671 if (ret != NO_ERROR) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08008672 ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret);
Eric Tan39ec8d62018-07-24 09:49:29 -07008673 if (!mActiveTracks.isEmpty()) {
Eric Laurent331679c2018-04-16 17:03:16 -07008674 mLock.unlock();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008675 if (isOutput()) {
Eric Laurentd7fe0862018-07-14 16:48:01 -07008676 AudioSystem::releaseOutput(portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008677 } else {
Eric Laurentfee19762018-01-29 18:44:13 -08008678 AudioSystem::releaseInput(portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008679 }
Eric Laurent331679c2018-04-16 17:03:16 -07008680 mLock.lock();
Eric Laurent18b57012017-02-13 16:23:52 -08008681 } else {
8682 mHalStream->stop();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008683 }
8684 return PERMISSION_DENIED;
8685 }
8686
Kevin Rocard1f564ac2018-03-29 13:53:10 -07008687 // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ?
8688 sp<MmapTrack> track = new MmapTrack(this, mAttr, mSampleRate, mFormat, mChannelMask, mSessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -07008689 isOutput(), client.clientUid, client.clientPid,
8690 IPCThreadState::self()->getCallingPid(), portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008691
Eric Laurent4eb58f12018-12-07 16:41:02 -08008692 if (isOutput()) {
8693 // force volume update when a new track is added
8694 mHalVolFloat = -1.0f;
8695 } else if (!track->isSilenced_l()) {
8696 for (const sp<MmapTrack> &t : mActiveTracks) {
8697 if (t->isSilenced_l() && t->uid() != client.clientUid)
8698 t->invalidate();
8699 }
8700 }
8701
8702
Eric Laurent6acd1d42017-01-04 14:23:29 -08008703 mActiveTracks.add(track);
Eric Laurenta54f1282017-07-01 19:39:32 -07008704 sp<EffectChain> chain = getEffectChain_l(mSessionId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008705 if (chain != 0) {
8706 chain->setStrategy(AudioSystem::getStrategyForStream(streamType()));
8707 chain->incTrackCnt();
8708 chain->incActiveTrackCnt();
8709 }
8710
8711 *handle = portId;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008712 broadcast_l();
8713
Eric Laurenta54f1282017-07-01 19:39:32 -07008714 ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08008715
8716 return NO_ERROR;
8717}
8718
8719status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle)
8720{
Eric Laurent6acd1d42017-01-04 14:23:29 -08008721 ALOGV("%s handle %d", __FUNCTION__, handle);
8722
8723 if (mHalStream == 0) {
8724 return NO_INIT;
8725 }
8726
Eric Laurenta54f1282017-07-01 19:39:32 -07008727 if (handle == mPortId) {
8728 mHalStream->stop();
8729 return NO_ERROR;
8730 }
8731
Eric Laurent331679c2018-04-16 17:03:16 -07008732 Mutex::Autolock _l(mLock);
8733
Eric Laurent6acd1d42017-01-04 14:23:29 -08008734 sp<MmapTrack> track;
8735 for (const sp<MmapTrack> &t : mActiveTracks) {
8736 if (handle == t->portId()) {
8737 track = t;
8738 break;
8739 }
8740 }
8741 if (track == 0) {
8742 return BAD_VALUE;
8743 }
8744
8745 mActiveTracks.remove(track);
8746
Eric Laurent331679c2018-04-16 17:03:16 -07008747 mLock.unlock();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008748 if (isOutput()) {
Eric Laurentd7fe0862018-07-14 16:48:01 -07008749 AudioSystem::stopOutput(track->portId());
8750 AudioSystem::releaseOutput(track->portId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08008751 } else {
Eric Laurentfee19762018-01-29 18:44:13 -08008752 AudioSystem::stopInput(track->portId());
8753 AudioSystem::releaseInput(track->portId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08008754 }
Eric Laurent331679c2018-04-16 17:03:16 -07008755 mLock.lock();
Eric Laurent6acd1d42017-01-04 14:23:29 -08008756
8757 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
8758 if (chain != 0) {
8759 chain->decActiveTrackCnt();
8760 chain->decTrackCnt();
8761 }
8762
8763 broadcast_l();
8764
Eric Laurent6acd1d42017-01-04 14:23:29 -08008765 return NO_ERROR;
8766}
8767
Eric Laurent18b57012017-02-13 16:23:52 -08008768status_t AudioFlinger::MmapThread::standby()
8769{
8770 ALOGV("%s", __FUNCTION__);
8771
8772 if (mHalStream == 0) {
8773 return NO_INIT;
8774 }
Eric Tan39ec8d62018-07-24 09:49:29 -07008775 if (!mActiveTracks.isEmpty()) {
Eric Laurent18b57012017-02-13 16:23:52 -08008776 return INVALID_OPERATION;
8777 }
8778 mHalStream->standby();
8779 mStandby = true;
8780 releaseWakeLock();
8781 return NO_ERROR;
8782}
8783
Eric Laurent6acd1d42017-01-04 14:23:29 -08008784
8785void AudioFlinger::MmapThread::readHalParameters_l()
8786{
8787 status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
8788 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
8789 mFormat = mHALFormat;
8790 LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat);
8791 result = mHalStream->getFrameSize(&mFrameSize);
8792 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
8793 result = mHalStream->getBufferSize(&mBufferSize);
8794 LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
8795 mFrameCount = mBufferSize / mFrameSize;
8796}
8797
8798bool AudioFlinger::MmapThread::threadLoop()
8799{
Eric Laurent6acd1d42017-01-04 14:23:29 -08008800 checkSilentMode_l();
8801
8802 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
8803
8804 while (!exitPending())
8805 {
Eric Laurent6acd1d42017-01-04 14:23:29 -08008806 Vector< sp<EffectChain> > effectChains;
8807
Andy Hung13850be2019-03-14 11:33:09 -07008808 { // under Thread lock
8809 Mutex::Autolock _l(mLock);
8810
Eric Laurent6acd1d42017-01-04 14:23:29 -08008811 if (mSignalPending) {
8812 // A signal was raised while we were unlocked
8813 mSignalPending = false;
8814 } else {
8815 if (mConfigEvents.isEmpty()) {
8816 // we're about to wait, flush the binder command buffer
8817 IPCThreadState::self()->flushCommands();
8818
8819 if (exitPending()) {
8820 break;
8821 }
8822
Eric Laurent6acd1d42017-01-04 14:23:29 -08008823 // wait until we have something to do...
8824 ALOGV("%s going to sleep", myName.string());
8825 mWaitWorkCV.wait(mLock);
8826 ALOGV("%s waking up", myName.string());
Eric Laurent6acd1d42017-01-04 14:23:29 -08008827
8828 checkSilentMode_l();
8829
8830 continue;
8831 }
8832 }
8833
8834 processConfigEvents_l();
8835
8836 processVolume_l();
8837
8838 checkInvalidTracks_l();
8839
8840 mActiveTracks.updatePowerState(this);
8841
Kevin Rocard069c2712018-03-29 19:09:14 -07008842 updateMetadata_l();
8843
Eric Laurent6acd1d42017-01-04 14:23:29 -08008844 lockEffectChains_l(effectChains);
Andy Hung13850be2019-03-14 11:33:09 -07008845 } // release Thread lock
8846
Eric Laurent6acd1d42017-01-04 14:23:29 -08008847 for (size_t i = 0; i < effectChains.size(); i ++) {
Andy Hung13850be2019-03-14 11:33:09 -07008848 effectChains[i]->process_l(); // Thread is not locked, but effect chain is locked
Eric Laurent6acd1d42017-01-04 14:23:29 -08008849 }
Andy Hung13850be2019-03-14 11:33:09 -07008850
8851 // enable changes in effect chain, including moving to another thread.
Eric Laurent6acd1d42017-01-04 14:23:29 -08008852 unlockEffectChains(effectChains);
8853 // Effect chains will be actually deleted here if they were removed from
8854 // mEffectChains list during mixing or effects processing
8855 }
8856
8857 threadLoop_exit();
8858
8859 if (!mStandby) {
8860 threadLoop_standby();
8861 mStandby = true;
8862 }
8863
Eric Laurent6acd1d42017-01-04 14:23:29 -08008864 ALOGV("Thread %p type %d exiting", this, mType);
8865 return false;
8866}
8867
8868// checkForNewParameter_l() must be called with ThreadBase::mLock held
8869bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair,
8870 status_t& status)
8871{
8872 AudioParameter param = AudioParameter(keyValuePair);
8873 int value;
Eric Laurente6e9a482017-07-25 19:26:02 -07008874 bool sendToHal = true;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008875 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
jiabin10d86fd2019-10-31 17:20:42 -07008876 LOG_FATAL("Should not happen set routing device in MmapThread");
Eric Laurent6acd1d42017-01-04 14:23:29 -08008877 }
Eric Laurente6e9a482017-07-25 19:26:02 -07008878 if (sendToHal) {
8879 status = mHalStream->setParameters(keyValuePair);
8880 } else {
8881 status = NO_ERROR;
8882 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08008883
8884 return false;
8885}
8886
8887String8 AudioFlinger::MmapThread::getParameters(const String8& keys)
8888{
8889 Mutex::Autolock _l(mLock);
8890 String8 out_s8;
8891 if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) {
8892 return out_s8;
8893 }
8894 return String8();
8895}
8896
Eric Laurent09f1ed22019-04-24 17:45:17 -07008897void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
8898 audio_port_handle_t portId __unused) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08008899 sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
8900
8901 desc->mIoHandle = mId;
8902
8903 switch (event) {
8904 case AUDIO_INPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -07008905 case AUDIO_INPUT_REGISTERED:
Eric Laurent6acd1d42017-01-04 14:23:29 -08008906 case AUDIO_INPUT_CONFIG_CHANGED:
8907 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -07008908 case AUDIO_OUTPUT_REGISTERED:
Eric Laurent6acd1d42017-01-04 14:23:29 -08008909 case AUDIO_OUTPUT_CONFIG_CHANGED:
8910 desc->mPatch = mPatch;
8911 desc->mChannelMask = mChannelMask;
8912 desc->mSamplingRate = mSampleRate;
8913 desc->mFormat = mFormat;
8914 desc->mFrameCount = mFrameCount;
8915 desc->mFrameCountHAL = mFrameCount;
8916 desc->mLatency = 0;
8917 break;
8918
8919 case AUDIO_INPUT_CLOSED:
8920 case AUDIO_OUTPUT_CLOSED:
8921 default:
8922 break;
8923 }
8924 mAudioFlinger->ioConfigChanged(event, desc, pid);
8925}
8926
8927status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch,
8928 audio_patch_handle_t *handle)
8929{
8930 status_t status = NO_ERROR;
8931
8932 // store new device and send to effects
8933 audio_devices_t type = AUDIO_DEVICE_NONE;
8934 audio_port_handle_t deviceId;
jiabin10d86fd2019-10-31 17:20:42 -07008935 AudioDeviceTypeAddrVector sinkDeviceTypeAddrs;
8936 AudioDeviceTypeAddr sourceDeviceTypeAddr;
8937 uint32_t numDevices = 0;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008938 if (isOutput()) {
8939 for (unsigned int i = 0; i < patch->num_sinks; i++) {
jiabin10d86fd2019-10-31 17:20:42 -07008940 LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1
8941 && !mAudioHwDev->supportsAudioPatches(),
8942 "Enumerated device type(%#x) must not be used "
8943 "as it does not support audio patches",
8944 patch->sinks[i].ext.device.type);
Eric Laurent6acd1d42017-01-04 14:23:29 -08008945 type |= patch->sinks[i].ext.device.type;
jiabin10d86fd2019-10-31 17:20:42 -07008946 sinkDeviceTypeAddrs.push_back(AudioDeviceTypeAddr(patch->sinks[i].ext.device.type,
8947 patch->sinks[i].ext.device.address));
Eric Laurent6acd1d42017-01-04 14:23:29 -08008948 }
8949 deviceId = patch->sinks[0].id;
jiabin10d86fd2019-10-31 17:20:42 -07008950 numDevices = mPatch.num_sinks;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008951 } else {
8952 type = patch->sources[0].ext.device.type;
8953 deviceId = patch->sources[0].id;
jiabin10d86fd2019-10-31 17:20:42 -07008954 numDevices = mPatch.num_sources;
8955 sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type;
8956 sourceDeviceTypeAddr.mAddress = patch->sources[0].ext.device.address;
Eric Laurent6acd1d42017-01-04 14:23:29 -08008957 }
8958
8959 for (size_t i = 0; i < mEffectChains.size(); i++) {
jiabinb8269fd2019-11-11 12:16:27 -08008960 if (isOutput()) {
8961 mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs);
8962 } else {
8963 mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr);
8964 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08008965 }
8966
jiabin10d86fd2019-10-31 17:20:42 -07008967 if (!isOutput()) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08008968 // store new source and send to effects
8969 if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
8970 mAudioSource = patch->sinks[0].ext.mix.usecase.source;
8971 for (size_t i = 0; i < mEffectChains.size(); i++) {
8972 mEffectChains[i]->setAudioSource_l(mAudioSource);
8973 }
8974 }
8975 }
8976
8977 if (mAudioHwDev->supportsAudioPatches()) {
8978 status = mHalDevice->createAudioPatch(patch->num_sources,
8979 patch->sources,
8980 patch->num_sinks,
8981 patch->sinks,
8982 handle);
8983 } else {
8984 char *address;
8985 if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
8986 //FIXME: we only support address on first sink with HAL version < 3.0
8987 address = audio_device_address_to_parameter(
8988 patch->sinks[0].ext.device.type,
8989 patch->sinks[0].ext.device.address);
8990 } else {
8991 address = (char *)calloc(1, 1);
8992 }
8993 AudioParameter param = AudioParameter(String8(address));
8994 free(address);
8995 param.addInt(String8(AudioParameter::keyRouting), (int)type);
8996 if (!isOutput()) {
8997 param.addInt(String8(AudioParameter::keyInputSource),
8998 (int)patch->sinks[0].ext.mix.usecase.source);
8999 }
9000 status = mHalStream->setParameters(param.toString());
9001 *handle = AUDIO_PATCH_HANDLE_NONE;
9002 }
9003
jiabin10d86fd2019-10-31 17:20:42 -07009004 if (numDevices == 0 || mDeviceId != deviceId) {
9005 if (isOutput()) {
9006 sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
9007 mOutDeviceTypeAddrs = sinkDeviceTypeAddrs;
9008 } else {
9009 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
9010 mInDeviceTypeAddr = sourceDeviceTypeAddr;
9011 }
Phil Burk7f6b40d2017-02-09 13:18:38 -08009012 sp<MmapStreamCallback> callback = mCallback.promote();
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07009013 if (mDeviceId != deviceId && callback != 0) {
Eric Laurent734046e2018-04-16 14:50:52 -07009014 mLock.unlock();
Phil Burk7f6b40d2017-02-09 13:18:38 -08009015 callback->onRoutingChanged(deviceId);
Eric Laurent734046e2018-04-16 14:50:52 -07009016 mLock.lock();
Eric Laurent6acd1d42017-01-04 14:23:29 -08009017 }
jiabin10d86fd2019-10-31 17:20:42 -07009018 mPatch = *patch;
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07009019 mDeviceId = deviceId;
Eric Laurent6acd1d42017-01-04 14:23:29 -08009020 }
9021 return status;
9022}
9023
9024status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
9025{
9026 status_t status = NO_ERROR;
9027
jiabin10d86fd2019-10-31 17:20:42 -07009028 mPatch = audio_patch{};
9029 mOutDeviceTypeAddrs.clear();
9030 mInDeviceTypeAddr.reset();
Eric Laurent6acd1d42017-01-04 14:23:29 -08009031
9032 bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ?
9033 supportsAudioPatches : false;
9034
9035 if (supportsAudioPatches) {
9036 status = mHalDevice->releaseAudioPatch(handle);
9037 } else {
9038 AudioParameter param;
9039 param.addInt(String8(AudioParameter::keyRouting), 0);
9040 status = mHalStream->setParameters(param.toString());
9041 }
9042 return status;
9043}
9044
Mikhail Naganovdc769682018-05-04 15:34:08 -07009045void AudioFlinger::MmapThread::toAudioPortConfig(struct audio_port_config *config)
Eric Laurent6acd1d42017-01-04 14:23:29 -08009046{
Mikhail Naganovdc769682018-05-04 15:34:08 -07009047 ThreadBase::toAudioPortConfig(config);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009048 if (isOutput()) {
9049 config->role = AUDIO_PORT_ROLE_SOURCE;
9050 config->ext.mix.hw_module = mAudioHwDev->handle();
9051 config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
9052 } else {
9053 config->role = AUDIO_PORT_ROLE_SINK;
9054 config->ext.mix.hw_module = mAudioHwDev->handle();
9055 config->ext.mix.usecase.source = mAudioSource;
9056 }
9057}
9058
9059status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain)
9060{
9061 audio_session_t session = chain->sessionId();
9062
9063 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
9064 // Attach all tracks with same session ID to this chain.
9065 // indicate all active tracks in the chain
9066 for (const sp<MmapTrack> &track : mActiveTracks) {
9067 if (session == track->sessionId()) {
9068 chain->incTrackCnt();
9069 chain->incActiveTrackCnt();
9070 }
9071 }
9072
9073 chain->setThread(this);
9074 chain->setInBuffer(nullptr);
9075 chain->setOutBuffer(nullptr);
9076 chain->syncHalEffectsState();
9077
9078 mEffectChains.add(chain);
9079 checkSuspendOnAddEffectChain_l(chain);
9080 return NO_ERROR;
9081}
9082
9083size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain)
9084{
9085 audio_session_t session = chain->sessionId();
9086
9087 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
9088
9089 for (size_t i = 0; i < mEffectChains.size(); i++) {
9090 if (chain == mEffectChains[i]) {
9091 mEffectChains.removeAt(i);
9092 // detach all active tracks from the chain
9093 // detach all tracks with same session ID from this chain
9094 for (const sp<MmapTrack> &track : mActiveTracks) {
9095 if (session == track->sessionId()) {
9096 chain->decActiveTrackCnt();
9097 chain->decTrackCnt();
9098 }
9099 }
9100 break;
9101 }
9102 }
9103 return mEffectChains.size();
9104}
9105
Eric Laurent6acd1d42017-01-04 14:23:29 -08009106void AudioFlinger::MmapThread::threadLoop_standby()
9107{
9108 mHalStream->standby();
9109}
9110
9111void AudioFlinger::MmapThread::threadLoop_exit()
9112{
Phil Burk7dce7282017-09-27 13:51:41 -07009113 // Do not call callback->onTearDown() because it is redundant for thread exit
9114 // and because it can cause a recursive mutex lock on stop().
Eric Laurent6acd1d42017-01-04 14:23:29 -08009115}
9116
9117status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused)
9118{
9119 return BAD_VALUE;
9120}
9121
9122bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
9123{
9124 return false;
9125}
9126
9127status_t AudioFlinger::MmapThread::checkEffectCompatibility_l(
9128 const effect_descriptor_t *desc, audio_session_t sessionId)
9129{
9130 // No global effect sessions on mmap threads
Eric Laurenta20c4e92019-11-12 15:55:51 -08009131 if (audio_is_global_session(sessionId)) {
9132 ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s",
Eric Laurent6acd1d42017-01-04 14:23:29 -08009133 desc->name, mThreadName);
9134 return BAD_VALUE;
9135 }
9136
9137 if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) {
9138 ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread",
9139 desc->name);
9140 return BAD_VALUE;
9141 }
9142 if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08009143 ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap "
9144 "thread", desc->name);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009145 return BAD_VALUE;
9146 }
9147
9148 // Only allow effects without processing load or latency
9149 if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) {
9150 return BAD_VALUE;
9151 }
9152
9153 return NO_ERROR;
Eric Laurent6acd1d42017-01-04 14:23:29 -08009154}
9155
9156void AudioFlinger::MmapThread::checkInvalidTracks_l()
9157{
9158 for (const sp<MmapTrack> &track : mActiveTracks) {
9159 if (track->isInvalid()) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08009160 sp<MmapStreamCallback> callback = mCallback.promote();
9161 if (callback != 0) {
Eric Laurent734046e2018-04-16 14:50:52 -07009162 mLock.unlock();
Eric Laurent331679c2018-04-16 17:03:16 -07009163 callback->onTearDown(track->portId());
Eric Laurent734046e2018-04-16 14:50:52 -07009164 mLock.lock();
Eric Laurent331679c2018-04-16 17:03:16 -07009165 } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
9166 ALOGW("Could not notify MMAP stream tear down: no onTearDown callback!");
9167 mNoCallbackWarningCount++;
Eric Laurent6acd1d42017-01-04 14:23:29 -08009168 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08009169 }
9170 }
9171}
9172
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07009173void AudioFlinger::MmapThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
Eric Laurent6acd1d42017-01-04 14:23:29 -08009174{
Eric Laurent6acd1d42017-01-04 14:23:29 -08009175 dprintf(fd, " Attributes: content type %d usage %d source %d\n",
9176 mAttr.content_type, mAttr.usage, mAttr.source);
9177 dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId);
Eric Tan39ec8d62018-07-24 09:49:29 -07009178 if (mActiveTracks.isEmpty()) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08009179 dprintf(fd, " No active clients\n");
9180 }
9181}
9182
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07009183void AudioFlinger::MmapThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
Eric Laurent6acd1d42017-01-04 14:23:29 -08009184{
Eric Laurent6acd1d42017-01-04 14:23:29 -08009185 String8 result;
Eric Laurent6acd1d42017-01-04 14:23:29 -08009186 size_t numtracks = mActiveTracks.size();
Andy Hung2c6c3bb2017-06-16 14:01:45 -07009187 dprintf(fd, " %zu Tracks\n", numtracks);
9188 const char *prefix = " ";
Eric Laurent6acd1d42017-01-04 14:23:29 -08009189 if (numtracks) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -07009190 result.append(prefix);
Kevin Rocard5f2136e2018-05-11 22:03:00 -07009191 mActiveTracks[0]->appendDumpHeader(result);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009192 for (size_t i = 0; i < numtracks ; ++i) {
9193 sp<MmapTrack> track = mActiveTracks[i];
Andy Hung2c6c3bb2017-06-16 14:01:45 -07009194 result.append(prefix);
9195 track->appendDump(result, true /* active */);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009196 }
9197 } else {
9198 dprintf(fd, "\n");
9199 }
9200 write(fd, result.string(), result.size());
9201}
9202
9203AudioFlinger::MmapPlaybackThread::MmapPlaybackThread(
9204 const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
jiabin10d86fd2019-10-31 17:20:42 -07009205 AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady)
9206 : MmapThread(audioFlinger, id, hwDev, output->stream, systemReady),
Eric Laurent6acd1d42017-01-04 14:23:29 -08009207 mStreamType(AUDIO_STREAM_MUSIC),
Phil Burk56ecf3e2018-03-12 15:38:17 -07009208 mStreamVolume(1.0),
9209 mStreamMute(false),
Phil Burk56ecf3e2018-03-12 15:38:17 -07009210 mOutput(output)
Eric Laurent6acd1d42017-01-04 14:23:29 -08009211{
9212 snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id);
9213 mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
9214 mMasterVolume = audioFlinger->masterVolume_l();
9215 mMasterMute = audioFlinger->masterMute_l();
9216 if (mAudioHwDev) {
9217 if (mAudioHwDev->canSetMasterVolume()) {
9218 mMasterVolume = 1.0;
9219 }
9220
9221 if (mAudioHwDev->canSetMasterMute()) {
9222 mMasterMute = false;
9223 }
9224 }
9225}
9226
9227void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr,
9228 audio_stream_type_t streamType,
9229 audio_session_t sessionId,
9230 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07009231 audio_port_handle_t deviceId,
Eric Laurent6acd1d42017-01-04 14:23:29 -08009232 audio_port_handle_t portId)
9233{
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07009234 MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009235 mStreamType = streamType;
9236}
9237
9238AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput()
9239{
9240 Mutex::Autolock _l(mLock);
9241 AudioStreamOut *output = mOutput;
9242 mOutput = NULL;
9243 return output;
9244}
9245
9246void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value)
9247{
9248 Mutex::Autolock _l(mLock);
9249 // Don't apply master volume in SW if our HAL can do it for us.
9250 if (mAudioHwDev &&
9251 mAudioHwDev->canSetMasterVolume()) {
9252 mMasterVolume = 1.0;
9253 } else {
9254 mMasterVolume = value;
9255 }
9256}
9257
9258void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted)
9259{
9260 Mutex::Autolock _l(mLock);
9261 // Don't apply master mute in SW if our HAL can do it for us.
9262 if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) {
9263 mMasterMute = false;
9264 } else {
9265 mMasterMute = muted;
9266 }
9267}
9268
9269void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
9270{
9271 Mutex::Autolock _l(mLock);
9272 if (stream == mStreamType) {
9273 mStreamVolume = value;
9274 broadcast_l();
9275 }
9276}
9277
9278float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const
9279{
9280 Mutex::Autolock _l(mLock);
9281 if (stream == mStreamType) {
9282 return mStreamVolume;
9283 }
9284 return 0.0f;
9285}
9286
9287void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
9288{
9289 Mutex::Autolock _l(mLock);
9290 if (stream == mStreamType) {
9291 mStreamMute= muted;
9292 broadcast_l();
9293 }
9294}
9295
9296void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType)
9297{
9298 Mutex::Autolock _l(mLock);
9299 if (streamType == mStreamType) {
9300 for (const sp<MmapTrack> &track : mActiveTracks) {
9301 track->invalidate();
9302 }
9303 broadcast_l();
9304 }
9305}
9306
9307void AudioFlinger::MmapPlaybackThread::processVolume_l()
9308{
9309 float volume;
9310
9311 if (mMasterMute || mStreamMute) {
9312 volume = 0;
9313 } else {
9314 volume = mMasterVolume * mStreamVolume;
9315 }
9316
9317 if (volume != mHalVolFloat) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08009318
9319 // Convert volumes from float to 8.24
9320 uint32_t vol = (uint32_t)(volume * (1 << 24));
9321
9322 // Delegate volume control to effect in track effect chain if needed
9323 // only one effect chain can be present on DirectOutputThread, so if
9324 // there is one, the track is connected to it
9325 if (!mEffectChains.isEmpty()) {
9326 mEffectChains[0]->setVolume_l(&vol, &vol);
9327 volume = (float)vol / (1 << 24);
9328 }
Eric Laurentdff774a2017-04-21 15:29:38 -07009329 // Try to use HW volume control and fall back to SW control if not implemented
Phil Burk56ecf3e2018-03-12 15:38:17 -07009330 if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) {
9331 mHalVolFloat = volume; // HW volume control worked, so update value.
9332 mNoCallbackWarningCount = 0;
9333 } else {
Eric Laurentdff774a2017-04-21 15:29:38 -07009334 sp<MmapStreamCallback> callback = mCallback.promote();
9335 if (callback != 0) {
9336 int channelCount;
9337 if (isOutput()) {
9338 channelCount = audio_channel_count_from_out_mask(mChannelMask);
9339 } else {
9340 channelCount = audio_channel_count_from_in_mask(mChannelMask);
9341 }
9342 Vector<float> values;
9343 for (int i = 0; i < channelCount; i++) {
9344 values.add(volume);
9345 }
Phil Burk56ecf3e2018-03-12 15:38:17 -07009346 mHalVolFloat = volume; // SW volume control worked, so update value.
9347 mNoCallbackWarningCount = 0;
Eric Laurent734046e2018-04-16 14:50:52 -07009348 mLock.unlock();
9349 callback->onVolumeChanged(mChannelMask, values);
9350 mLock.lock();
Eric Laurent6acd1d42017-01-04 14:23:29 -08009351 } else {
Phil Burk56ecf3e2018-03-12 15:38:17 -07009352 if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
9353 ALOGW("Could not set MMAP stream volume: no volume callback!");
9354 mNoCallbackWarningCount++;
9355 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08009356 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08009357 }
9358 }
9359}
9360
Kevin Rocard069c2712018-03-29 19:09:14 -07009361void AudioFlinger::MmapPlaybackThread::updateMetadata_l()
9362{
9363 if (mOutput == nullptr || mOutput->stream == nullptr ||
9364 !mActiveTracks.readAndClearHasChanged()) {
9365 return;
9366 }
9367 StreamOutHalInterface::SourceMetadata metadata;
9368 for (const sp<MmapTrack> &track : mActiveTracks) {
9369 // No track is invalid as this is called after prepareTrack_l in the same critical section
9370 metadata.tracks.push_back({
9371 .usage = track->attributes().usage,
9372 .content_type = track->attributes().content_type,
9373 .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume
9374 });
9375 }
9376 mOutput->stream->updateSourceMetadata(metadata);
9377}
9378
Eric Laurent6acd1d42017-01-04 14:23:29 -08009379void AudioFlinger::MmapPlaybackThread::checkSilentMode_l()
9380{
9381 if (!mMasterMute) {
9382 char value[PROPERTY_VALUE_MAX];
9383 if (property_get("ro.audio.silent", value, "0") > 0) {
9384 char *endptr;
9385 unsigned long ul = strtoul(value, &endptr, 0);
9386 if (*endptr == '\0' && ul != 0) {
9387 ALOGD("Silence is golden");
9388 // The setprop command will not allow a property to be changed after
9389 // the first time it is set, so we don't have to worry about un-muting.
9390 setMasterMute_l(true);
9391 }
9392 }
9393 }
9394}
9395
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07009396void AudioFlinger::MmapPlaybackThread::toAudioPortConfig(struct audio_port_config *config)
9397{
9398 MmapThread::toAudioPortConfig(config);
9399 if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
9400 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
9401 config->flags.output = mOutput->flags;
9402 }
9403}
9404
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07009405void AudioFlinger::MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args)
Eric Laurent6acd1d42017-01-04 14:23:29 -08009406{
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07009407 MmapThread::dumpInternals_l(fd, args);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009408
Glenn Kastend3bb6452016-12-05 18:14:37 -08009409 dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n",
9410 mStreamType, mStreamVolume, mHalVolFloat, mStreamMute);
Eric Laurent6acd1d42017-01-04 14:23:29 -08009411 dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute);
9412}
9413
9414AudioFlinger::MmapCaptureThread::MmapCaptureThread(
9415 const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
jiabin10d86fd2019-10-31 17:20:42 -07009416 AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady)
9417 : MmapThread(audioFlinger, id, hwDev, input->stream, systemReady),
Eric Laurent6acd1d42017-01-04 14:23:29 -08009418 mInput(input)
9419{
9420 snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id);
9421 mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
9422}
9423
Eric Laurent331679c2018-04-16 17:03:16 -07009424status_t AudioFlinger::MmapCaptureThread::exitStandby()
9425{
Phil Burkf054fc32018-12-06 09:45:59 -08009426 {
9427 // mInput might have been cleared by clearInput()
9428 Mutex::Autolock _l(mLock);
9429 if (mInput != nullptr && mInput->stream != nullptr) {
9430 mInput->stream->setGain(1.0f);
9431 }
9432 }
Eric Laurent331679c2018-04-16 17:03:16 -07009433 return MmapThread::exitStandby();
9434}
9435
Eric Laurent6acd1d42017-01-04 14:23:29 -08009436AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput()
9437{
9438 Mutex::Autolock _l(mLock);
9439 AudioStreamIn *input = mInput;
9440 mInput = NULL;
9441 return input;
9442}
Kevin Rocard069c2712018-03-29 19:09:14 -07009443
Eric Laurent331679c2018-04-16 17:03:16 -07009444
9445void AudioFlinger::MmapCaptureThread::processVolume_l()
9446{
9447 bool changed = false;
9448 bool silenced = false;
9449
9450 sp<MmapStreamCallback> callback = mCallback.promote();
9451 if (callback == 0) {
9452 if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
9453 ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!");
9454 mNoCallbackWarningCount++;
9455 }
9456 }
9457
9458 // After a change occurred in track silenced state, mute capture in audio DSP if at least one
9459 // track is silenced and unmute otherwise
9460 for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) {
9461 if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) {
9462 changed = true;
9463 silenced = mActiveTracks[i]->isSilenced_l();
9464 }
9465 }
9466
9467 if (changed) {
9468 mInput->stream->setGain(silenced ? 0.0f: 1.0f);
9469 }
9470}
9471
Kevin Rocard069c2712018-03-29 19:09:14 -07009472void AudioFlinger::MmapCaptureThread::updateMetadata_l()
9473{
9474 if (mInput == nullptr || mInput->stream == nullptr ||
9475 !mActiveTracks.readAndClearHasChanged()) {
9476 return;
9477 }
9478 StreamInHalInterface::SinkMetadata metadata;
9479 for (const sp<MmapTrack> &track : mActiveTracks) {
9480 // No track is invalid as this is called after prepareTrack_l in the same critical section
9481 metadata.tracks.push_back({
9482 .source = track->attributes().source,
9483 .gain = 1, // capture tracks do not have volumes
9484 });
9485 }
9486 mInput->stream->updateSinkMetadata(metadata);
9487}
9488
Eric Laurent331679c2018-04-16 17:03:16 -07009489void AudioFlinger::MmapCaptureThread::setRecordSilenced(uid_t uid, bool silenced)
9490{
9491 Mutex::Autolock _l(mLock);
9492 for (size_t i = 0; i < mActiveTracks.size() ; i++) {
9493 if (mActiveTracks[i]->uid() == uid) {
9494 mActiveTracks[i]->setSilenced_l(silenced);
9495 broadcast_l();
9496 }
9497 }
9498}
9499
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07009500void AudioFlinger::MmapCaptureThread::toAudioPortConfig(struct audio_port_config *config)
9501{
9502 MmapThread::toAudioPortConfig(config);
9503 if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
9504 config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
9505 config->flags.input = mInput->flags;
9506 }
9507}
9508
Glenn Kasten63238ef2015-03-02 15:50:29 -08009509} // namespace android