blob: 10a77efbcb70480ac6b161528269920ef516400d [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
Andy Hungee58e4a2023-07-07 13:47:37 -070018#pragma once
Eric Laurent81784c32012-11-19 14:55:58 -080019
Andy Hung25a80ac2023-07-19 12:47:35 -070020// ADD_BATTERY_DATA AUDIO_WATCHDOG FAST_THREAD_STATISTICS STATE_QUEUE_DUMP TEE_SINK
21#include "Configuration.h"
22#include "IAfThread.h"
23#include "IAfTrack.h"
24
25#include <android-base/macros.h> // DISALLOW_COPY_AND_ASSIGN
26#include <android/os/IPowerManager.h>
27#include <afutils/AudioWatchdog.h>
28#include <afutils/NBAIO_Tee.h>
29#include <audio_utils/Balance.h>
30#include <audio_utils/SimpleLog.h>
31#include <datapath/ThreadMetrics.h>
32#include <fastpath/FastCapture.h>
33#include <fastpath/FastMixer.h>
34#include <mediautils/Synchronization.h>
35#include <mediautils/ThreadSnapshot.h>
36#include <timing/MonotonicFrameCounter.h>
37#include <utils/Log.h>
38
Andy Hungee58e4a2023-07-07 13:47:37 -070039namespace android {
Andy Hung440901d2023-06-29 21:19:25 -070040
41class AsyncCallbackThread;
42
43class ThreadBase : public virtual IAfThreadBase, public Thread {
Eric Laurent81784c32012-11-19 14:55:58 -080044public:
Glenn Kasten97b7b752014-09-28 13:04:24 -070045 static const char *threadTypeToString(type_t type);
46
Andy Hung8d672e02023-09-15 18:19:28 -070047 // ThreadBase_ThreadLoop is a virtual mutex (always nullptr) that
48 // guards methods and variables that ONLY run and are accessed
49 // on the single threaded threadLoop().
50 //
51 // As access is by a single thread, the variables are thread safe.
52 static audio_utils::mutex* ThreadBase_ThreadLoop;
53
Andy Hung583043b2023-07-17 17:05:00 -070054 IAfThreadCallback* afThreadCallback() const final { return mAfThreadCallback.get(); }
Andy Hung87c693c2023-07-06 20:56:16 -070055
Andy Hung583043b2023-07-17 17:05:00 -070056 ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
Andy Hungcf10d742020-04-28 15:38:24 -070057 type_t type, bool systemReady, bool isOut);
Andy Hung440901d2023-06-29 21:19:25 -070058 ~ThreadBase() override;
Eric Laurent81784c32012-11-19 14:55:58 -080059
Andy Hung440901d2023-06-29 21:19:25 -070060 status_t readyToRun() final;
Andy Hungab65b182023-09-06 19:41:47 -070061 void clearPowerManager() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -080062
63 // base for record and playback
64 enum {
65 CFG_EVENT_IO,
Eric Laurent10351942014-05-08 18:49:52 -070066 CFG_EVENT_PRIO,
67 CFG_EVENT_SET_PARAMETER,
Eric Laurent1c333e22014-05-20 10:48:17 -070068 CFG_EVENT_CREATE_AUDIO_PATCH,
69 CFG_EVENT_RELEASE_AUDIO_PATCH,
jiabinc52b1ff2019-10-31 17:20:42 -070070 CFG_EVENT_UPDATE_OUT_DEVICE,
Eric Laurentb3f315a2021-07-13 15:09:05 +020071 CFG_EVENT_RESIZE_BUFFER,
Eric Laurent68a40a82022-05-03 18:15:04 +020072 CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS,
73 CFG_EVENT_HAL_LATENCY_MODES_CHANGED,
Eric Laurent81784c32012-11-19 14:55:58 -080074 };
75
Eric Laurent10351942014-05-08 18:49:52 -070076 class ConfigEventData: public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080077 public:
Eric Laurent81784c32012-11-19 14:55:58 -080078 virtual void dump(char *buffer, size_t size) = 0;
Eric Laurent10351942014-05-08 18:49:52 -070079 protected:
Andy Hungab65b182023-09-06 19:41:47 -070080 ConfigEventData() = default;
Eric Laurent81784c32012-11-19 14:55:58 -080081 };
82
Eric Laurent10351942014-05-08 18:49:52 -070083 // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
84 // 1. create SetParameterConfigEvent. This sets mWaitStatus in config event
Andy Hungc5007f82023-08-29 14:26:09 -070085 // 2. Lock mutex()
Eric Laurent10351942014-05-08 18:49:52 -070086 // 3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
87 // 4. sendConfigEvent_l() reads status from event->mStatus;
88 // 5. sendConfigEvent_l() returns status
89 // 6. Unlock
90 //
91 // Parameter sequence by server: threadLoop calling processConfigEvents_l():
Andy Hungc5007f82023-08-29 14:26:09 -070092 // 1. Lock mutex()
Eric Laurent10351942014-05-08 18:49:52 -070093 // 2. If there is an entry in mConfigEvents proceed ...
94 // 3. Read first entry in mConfigEvents
95 // 4. Remove first entry from mConfigEvents
96 // 5. Process
97 // 6. Set event->mStatus
Andy Hungc5007f82023-08-29 14:26:09 -070098 // 7. event->mCondition.notify_one()
Eric Laurent10351942014-05-08 18:49:52 -070099 // 8. Unlock
Eric Laurent81784c32012-11-19 14:55:58 -0800100
Eric Laurent10351942014-05-08 18:49:52 -0700101 class ConfigEvent: public RefBase {
102 public:
Eric Laurentb3f315a2021-07-13 15:09:05 +0200103 void dump(char *buffer, size_t size) {
104 snprintf(buffer, size, "Event type: %d\n", mType);
105 if (mData != nullptr) {
106 snprintf(buffer, size, "Data:\n");
107 mData->dump(buffer, size);
108 }
109 }
Eric Laurent10351942014-05-08 18:49:52 -0700110
Andy Hungab65b182023-09-06 19:41:47 -0700111 audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::ConfigEvent_Mutex) {
112 return mMutex;
113 }
Eric Laurent10351942014-05-08 18:49:52 -0700114 const int mType; // event type e.g. CFG_EVENT_IO
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700115 // mutex associated with mCondition
116 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kConfigEvent_Mutex};
Andy Hungc5007f82023-08-29 14:26:09 -0700117 audio_utils::condition_variable mCondition; // condition for status return
Andy Hungab65b182023-09-06 19:41:47 -0700118
119 // NO_THREAD_SAFETY_ANALYSIS Can we add GUARDED_BY?
Eric Laurent10351942014-05-08 18:49:52 -0700120 status_t mStatus; // status communicated to sender
Andy Hungab65b182023-09-06 19:41:47 -0700121
122 bool mWaitStatus GUARDED_BY(mutex()); // true if sender is waiting for status
123 // true if must wait for system ready to enter event queue
124 bool mRequiresSystemReady GUARDED_BY(mutex());
125
126 // NO_THREAD_SAFETY_ANALYSIS Can we add GUARDED_BY?
127 sp<ConfigEventData> mData; // event specific parameter data
Eric Laurent10351942014-05-08 18:49:52 -0700128
129 protected:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700130 explicit ConfigEvent(int type, bool requiresSystemReady = false) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700131 mType(type), mStatus(NO_ERROR), mWaitStatus(false),
132 mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
Eric Laurent10351942014-05-08 18:49:52 -0700133 };
134
135 class IoConfigEventData : public ConfigEventData {
136 public:
Mikhail Naganov88536df2021-07-26 17:30:29 -0700137 IoConfigEventData(audio_io_config_event_t event, pid_t pid,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700138 audio_port_handle_t portId) :
139 mEvent(event), mPid(pid), mPortId(portId) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800140
141 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200142 snprintf(buffer, size, "- IO event: event %d\n", mEvent);
Eric Laurent81784c32012-11-19 14:55:58 -0800143 }
144
Mikhail Naganov88536df2021-07-26 17:30:29 -0700145 const audio_io_config_event_t mEvent;
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700146 const pid_t mPid;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700147 const audio_port_handle_t mPortId;
Eric Laurent81784c32012-11-19 14:55:58 -0800148 };
149
Eric Laurent10351942014-05-08 18:49:52 -0700150 class IoConfigEvent : public ConfigEvent {
Eric Laurent81784c32012-11-19 14:55:58 -0800151 public:
Mikhail Naganov88536df2021-07-26 17:30:29 -0700152 IoConfigEvent(audio_io_config_event_t event, pid_t pid, audio_port_handle_t portId) :
Eric Laurent10351942014-05-08 18:49:52 -0700153 ConfigEvent(CFG_EVENT_IO) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700154 mData = new IoConfigEventData(event, pid, portId);
Eric Laurent10351942014-05-08 18:49:52 -0700155 }
Eric Laurent10351942014-05-08 18:49:52 -0700156 };
Eric Laurent81784c32012-11-19 14:55:58 -0800157
Eric Laurent10351942014-05-08 18:49:52 -0700158 class PrioConfigEventData : public ConfigEventData {
159 public:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800160 PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
161 mPid(pid), mTid(tid), mPrio(prio), mForApp(forApp) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800162
163 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200164 snprintf(buffer, size, "- Prio event: pid %d, tid %d, prio %d, for app? %d\n",
Mikhail Naganov83f04272017-02-07 10:45:09 -0800165 mPid, mTid, mPrio, mForApp);
Eric Laurent81784c32012-11-19 14:55:58 -0800166 }
167
Eric Laurent81784c32012-11-19 14:55:58 -0800168 const pid_t mPid;
169 const pid_t mTid;
170 const int32_t mPrio;
Mikhail Naganov83f04272017-02-07 10:45:09 -0800171 const bool mForApp;
Eric Laurent81784c32012-11-19 14:55:58 -0800172 };
173
Eric Laurent10351942014-05-08 18:49:52 -0700174 class PrioConfigEvent : public ConfigEvent {
175 public:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800176 PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700177 ConfigEvent(CFG_EVENT_PRIO, true) {
Mikhail Naganov83f04272017-02-07 10:45:09 -0800178 mData = new PrioConfigEventData(pid, tid, prio, forApp);
Eric Laurent10351942014-05-08 18:49:52 -0700179 }
Eric Laurent10351942014-05-08 18:49:52 -0700180 };
181
182 class SetParameterConfigEventData : public ConfigEventData {
183 public:
Andy Hung920f6572022-10-06 12:09:49 -0700184 explicit SetParameterConfigEventData(const String8& keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700185 mKeyValuePairs(keyValuePairs) {}
186
187 virtual void dump(char *buffer, size_t size) {
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000188 snprintf(buffer, size, "- KeyValue: %s\n", mKeyValuePairs.c_str());
Eric Laurent10351942014-05-08 18:49:52 -0700189 }
190
191 const String8 mKeyValuePairs;
192 };
193
194 class SetParameterConfigEvent : public ConfigEvent {
195 public:
Andy Hung920f6572022-10-06 12:09:49 -0700196 explicit SetParameterConfigEvent(const String8& keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700197 ConfigEvent(CFG_EVENT_SET_PARAMETER) {
198 mData = new SetParameterConfigEventData(keyValuePairs);
199 mWaitStatus = true;
200 }
Eric Laurent10351942014-05-08 18:49:52 -0700201 };
202
Eric Laurent1c333e22014-05-20 10:48:17 -0700203 class CreateAudioPatchConfigEventData : public ConfigEventData {
204 public:
205 CreateAudioPatchConfigEventData(const struct audio_patch patch,
206 audio_patch_handle_t handle) :
207 mPatch(patch), mHandle(handle) {}
208
209 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200210 snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -0700211 }
212
213 const struct audio_patch mPatch;
Andy Hungab65b182023-09-06 19:41:47 -0700214 audio_patch_handle_t mHandle; // cannot be const
Eric Laurent1c333e22014-05-20 10:48:17 -0700215 };
216
217 class CreateAudioPatchConfigEvent : public ConfigEvent {
218 public:
219 CreateAudioPatchConfigEvent(const struct audio_patch patch,
220 audio_patch_handle_t handle) :
221 ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
222 mData = new CreateAudioPatchConfigEventData(patch, handle);
223 mWaitStatus = true;
224 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700225 };
226
227 class ReleaseAudioPatchConfigEventData : public ConfigEventData {
228 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700229 explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700230 mHandle(handle) {}
231
232 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200233 snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -0700234 }
235
Andy Hungab65b182023-09-06 19:41:47 -0700236 const audio_patch_handle_t mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -0700237 };
238
239 class ReleaseAudioPatchConfigEvent : public ConfigEvent {
240 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700241 explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700242 ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
243 mData = new ReleaseAudioPatchConfigEventData(handle);
244 mWaitStatus = true;
245 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700246 };
Eric Laurent81784c32012-11-19 14:55:58 -0800247
jiabinc52b1ff2019-10-31 17:20:42 -0700248 class UpdateOutDevicesConfigEventData : public ConfigEventData {
249 public:
250 explicit UpdateOutDevicesConfigEventData(const DeviceDescriptorBaseVector& outDevices) :
251 mOutDevices(outDevices) {}
252
253 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200254 snprintf(buffer, size, "- Devices: %s", android::toString(mOutDevices).c_str());
jiabinc52b1ff2019-10-31 17:20:42 -0700255 }
256
Andy Hungab65b182023-09-06 19:41:47 -0700257 const DeviceDescriptorBaseVector mOutDevices;
jiabinc52b1ff2019-10-31 17:20:42 -0700258 };
259
260 class UpdateOutDevicesConfigEvent : public ConfigEvent {
261 public:
262 explicit UpdateOutDevicesConfigEvent(const DeviceDescriptorBaseVector& outDevices) :
263 ConfigEvent(CFG_EVENT_UPDATE_OUT_DEVICE) {
264 mData = new UpdateOutDevicesConfigEventData(outDevices);
265 }
jiabinc52b1ff2019-10-31 17:20:42 -0700266 };
267
Eric Laurentec376dc2021-04-08 20:41:22 +0200268 class ResizeBufferConfigEventData : public ConfigEventData {
269 public:
270 explicit ResizeBufferConfigEventData(int32_t maxSharedAudioHistoryMs) :
271 mMaxSharedAudioHistoryMs(maxSharedAudioHistoryMs) {}
272
273 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200274 snprintf(buffer, size, "- mMaxSharedAudioHistoryMs: %d", mMaxSharedAudioHistoryMs);
Eric Laurentec376dc2021-04-08 20:41:22 +0200275 }
276
Andy Hungab65b182023-09-06 19:41:47 -0700277 const int32_t mMaxSharedAudioHistoryMs;
Eric Laurentec376dc2021-04-08 20:41:22 +0200278 };
279
280 class ResizeBufferConfigEvent : public ConfigEvent {
281 public:
282 explicit ResizeBufferConfigEvent(int32_t maxSharedAudioHistoryMs) :
283 ConfigEvent(CFG_EVENT_RESIZE_BUFFER) {
284 mData = new ResizeBufferConfigEventData(maxSharedAudioHistoryMs);
285 }
Eric Laurentec376dc2021-04-08 20:41:22 +0200286 };
287
Eric Laurentb3f315a2021-07-13 15:09:05 +0200288 class CheckOutputStageEffectsEvent : public ConfigEvent {
289 public:
290 CheckOutputStageEffectsEvent() :
291 ConfigEvent(CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS) {
292 }
Eric Laurentb3f315a2021-07-13 15:09:05 +0200293 };
294
Eric Laurent68a40a82022-05-03 18:15:04 +0200295 class HalLatencyModesChangedEvent : public ConfigEvent {
296 public:
297 HalLatencyModesChangedEvent() :
298 ConfigEvent(CFG_EVENT_HAL_LATENCY_MODES_CHANGED) {
299 }
Eric Laurent68a40a82022-05-03 18:15:04 +0200300 };
301
Eric Laurentb3f315a2021-07-13 15:09:05 +0200302
Eric Laurent81784c32012-11-19 14:55:58 -0800303 class PMDeathRecipient : public IBinder::DeathRecipient {
304 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700305 explicit PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800306
307 // IBinder::DeathRecipient
Andy Hungab65b182023-09-06 19:41:47 -0700308 void binderDied(const wp<IBinder>& who) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800309
310 private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700311 DISALLOW_COPY_AND_ASSIGN(PMDeathRecipient);
Eric Laurent81784c32012-11-19 14:55:58 -0800312
Andy Hungab65b182023-09-06 19:41:47 -0700313 const wp<ThreadBase> mThread;
Eric Laurent81784c32012-11-19 14:55:58 -0800314 };
315
Andy Hung440901d2023-06-29 21:19:25 -0700316 type_t type() const final { return mType; }
317 bool isDuplicating() const final { return (mType == DUPLICATING); }
318 audio_io_handle_t id() const final { return mId;}
Eric Laurent81784c32012-11-19 14:55:58 -0800319
Andy Hung440901d2023-06-29 21:19:25 -0700320 uint32_t sampleRate() const final { return mSampleRate; }
321 audio_channel_mask_t channelMask() const final { return mChannelMask; }
322 audio_channel_mask_t mixerChannelMask() const override { return mChannelMask; }
323 audio_format_t format() const final { return mHALFormat; }
324 uint32_t channelCount() const final { return mChannelCount; }
325 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Andy Hung87c693c2023-07-06 20:56:16 -0700326 uint32_t hapticChannelCount() const override { return 0; }
Andy Hungab65b182023-09-06 19:41:47 -0700327 uint32_t latency_l() const override { return 0; } // NO_THREAD_SAFETY_ANALYSIS
328 void setVolumeForOutput_l(float /* left */, float /* right */) const override
329 REQUIRES(mutex()) {}
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700330
331 // Return's the HAL's frame count i.e. fast mixer buffer size.
Andy Hung440901d2023-06-29 21:19:25 -0700332 size_t frameCountHAL() const final { return mFrameCount; }
333 size_t frameSize() const final { return mFrameSize; }
Eric Laurent81784c32012-11-19 14:55:58 -0800334
335 // Should be "virtual status_t requestExitAndWait()" and override same
336 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
Andy Hungab65b182023-09-06 19:41:47 -0700337 void exit() final EXCLUDES_ThreadBase_Mutex;
338 status_t setParameters(const String8& keyValuePairs) final EXCLUDES_ThreadBase_Mutex;
Andy Hung440901d2023-06-29 21:19:25 -0700339
Andy Hungc5007f82023-08-29 14:26:09 -0700340 // sendConfigEvent_l() must be called with ThreadBase::mutex() held
Eric Laurent10351942014-05-08 18:49:52 -0700341 // Can temporarily release the lock if waiting for a reply from
342 // processConfigEvents_l().
Andy Hungab65b182023-09-06 19:41:47 -0700343 status_t sendConfigEvent_l(sp<ConfigEvent>& event) REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -0700344 void sendIoConfigEvent(audio_io_config_event_t event, pid_t pid = 0,
Andy Hungab65b182023-09-06 19:41:47 -0700345 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final EXCLUDES_ThreadBase_Mutex;
Andy Hung440901d2023-06-29 21:19:25 -0700346 void sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid = 0,
Andy Hungab65b182023-09-06 19:41:47 -0700347 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final REQUIRES(mutex());
348 void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) final
349 EXCLUDES_ThreadBase_Mutex;
350 void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp) final
351 REQUIRES(mutex());
352 status_t sendSetParameterConfigEvent_l(const String8& keyValuePair) final REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -0700353 status_t sendCreateAudioPatchConfigEvent(const struct audio_patch* patch,
Andy Hungab65b182023-09-06 19:41:47 -0700354 audio_patch_handle_t* handle) final EXCLUDES_ThreadBase_Mutex;
355 status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle) final
356 EXCLUDES_ThreadBase_Mutex;
Andy Hung440901d2023-06-29 21:19:25 -0700357 status_t sendUpdateOutDeviceConfigEvent(
Andy Hungab65b182023-09-06 19:41:47 -0700358 const DeviceDescriptorBaseVector& outDevices) final EXCLUDES_ThreadBase_Mutex;
359 void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) final REQUIRES(mutex());
360 void sendCheckOutputStageEffectsEvent() final EXCLUDES_ThreadBase_Mutex;
361 void sendCheckOutputStageEffectsEvent_l() final REQUIRES(mutex());
362 void sendHalLatencyModesChangedEvent_l() final REQUIRES(mutex());
Eric Laurentb3f315a2021-07-13 15:09:05 +0200363
Andy Hungab65b182023-09-06 19:41:47 -0700364 void processConfigEvents_l() final REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -0700365 void setCheckOutputStageEffects() override {}
366 void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override;
367 void toAudioPortConfig(struct audio_port_config* config) override;
Andy Hungab65b182023-09-06 19:41:47 -0700368 void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override REQUIRES(mutex());
Eric Laurent1c333e22014-05-20 10:48:17 -0700369
Andy Hung440901d2023-06-29 21:19:25 -0700370 // see note at declaration of mStandby, mOutDevice and mInDevice
371 bool inStandby() const override { return mStandby; }
Andy Hungab65b182023-09-06 19:41:47 -0700372 const DeviceTypeSet outDeviceTypes_l() const final REQUIRES(mutex()) {
Andy Hung440901d2023-06-29 21:19:25 -0700373 return getAudioDeviceTypes(mOutDeviceTypeAddrs);
374 }
Andy Hungab65b182023-09-06 19:41:47 -0700375 audio_devices_t inDeviceType_l() const final REQUIRES(mutex()) {
376 return mInDeviceTypeAddr.mType;
377 }
378 DeviceTypeSet getDeviceTypes_l() const final REQUIRES(mutex()) {
379 return isOutput() ? outDeviceTypes_l() : DeviceTypeSet({inDeviceType_l()});
Andy Hung440901d2023-06-29 21:19:25 -0700380 }
Eric Laurent81784c32012-11-19 14:55:58 -0800381
Liz Pruckaac078262024-07-29 15:39:16 +0000382 const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const final REQUIRES(mutex()) {
Andy Hung440901d2023-06-29 21:19:25 -0700383 return mOutDeviceTypeAddrs;
384 }
Liz Pruckaac078262024-07-29 15:39:16 +0000385 const AudioDeviceTypeAddr& inDeviceTypeAddr() const final REQUIRES(mutex()) {
Andy Hung440901d2023-06-29 21:19:25 -0700386 return mInDeviceTypeAddr;
387 }
Andy Hung293558a2017-03-21 12:19:20 -0700388
Andy Hung440901d2023-06-29 21:19:25 -0700389 bool isOutput() const final { return mIsOut; }
jiabin8f278ee2019-11-11 12:16:27 -0800390
Andy Hung440901d2023-06-29 21:19:25 -0700391 bool isOffloadOrMmap() const final {
392 switch (mType) {
393 case OFFLOAD:
394 case MMAP_PLAYBACK:
395 case MMAP_CAPTURE:
396 return true;
397 default:
398 return false;
399 }
400 }
Eric Laurent81784c32012-11-19 14:55:58 -0800401
Andy Hung440901d2023-06-29 21:19:25 -0700402 sp<IAfEffectHandle> createEffect_l(
Andy Hung88035ac2023-06-27 17:05:02 -0700403 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700404 const sp<media::IEffectClient>& effectClient,
Eric Laurent81784c32012-11-19 14:55:58 -0800405 int32_t priority,
Glenn Kastend848eb42016-03-08 13:42:11 -0800406 audio_session_t sessionId,
Eric Laurent81784c32012-11-19 14:55:58 -0800407 effect_descriptor_t *desc,
408 int *enabled,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800409 status_t *status /*non-NULL*/,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700410 bool pinned,
Eric Laurentde8caf42021-08-11 17:19:25 +0200411 bool probe,
Andy Hung972bec12023-08-31 16:13:39 -0700412 bool notifyFramesProcessed) final
413 REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800414
415 // return values for hasAudioSession (bit field)
416 enum effect_state {
417 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
418 // effect
Eric Laurent4c415062016-06-17 16:14:16 -0700419 TRACK_SESSION = 0x2, // the audio session corresponds to at least one
Eric Laurent81784c32012-11-19 14:55:58 -0800420 // track
Eric Laurentb62d0362021-10-26 17:40:18 +0200421 FAST_SESSION = 0x4, // the audio session corresponds to at least one
Eric Laurent4c415062016-06-17 16:14:16 -0700422 // fast track
jiabinc658e452022-10-21 20:52:21 +0000423 SPATIALIZED_SESSION = 0x8, // the audio session corresponds to at least one
424 // spatialized track
425 BIT_PERFECT_SESSION = 0x10 // the audio session corresponds to at least one
426 // bit-perfect track
Eric Laurent81784c32012-11-19 14:55:58 -0800427 };
428
Andy Hung440901d2023-06-29 21:19:25 -0700429 // get effect chain corresponding to session Id.
430 sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const final;
431 // same as getEffectChain() but must be called with ThreadBase mutex locked
Andy Hung8d672e02023-09-15 18:19:28 -0700432 sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const final REQUIRES(mutex());
433 std::vector<int> getEffectIds_l(audio_session_t sessionId) const final REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -0700434
Eric Laurent81784c32012-11-19 14:55:58 -0800435 // lock all effect chains Mutexes. Must be called before releasing the
436 // ThreadBase mutex before processing the mixer and effects. This guarantees the
437 // integrity of the chains during the process.
438 // Also sets the parameter 'effectChains' to current value of mEffectChains.
Shunkai Yaof4847652024-01-12 00:25:20 +0000439 void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) final
Shunkai Yaod125e402024-01-20 03:19:06 +0000440 REQUIRES(audio_utils::ThreadBase_Mutex) ACQUIRE(audio_utils::EffectChain_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800441 // unlock effect chains after process
Shunkai Yaod125e402024-01-20 03:19:06 +0000442 void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) final
443 RELEASE(audio_utils::EffectChain_Mutex);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800444 // get a copy of mEffectChains vector
Andy Hungab65b182023-09-06 19:41:47 -0700445 Vector<sp<IAfEffectChain>> getEffectChains_l() const final REQUIRES(mutex()) {
446 return mEffectChains;
447 }
Eric Laurent81784c32012-11-19 14:55:58 -0800448 // set audio mode to all effect chains
Andy Hung440901d2023-06-29 21:19:25 -0700449 void setMode(audio_mode_t mode) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800450 // get effect module with corresponding ID on specified audio session
Andy Hung440901d2023-06-29 21:19:25 -0700451 sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const final;
Andy Hungab65b182023-09-06 19:41:47 -0700452 sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const final
453 REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800454 // add and effect module. Also creates the effect chain is none exists for
Eric Laurent6c796322019-04-09 14:13:17 -0700455 // the effects audio session. Only called in a context of moving an effect
456 // from one thread to another
Andy Hung972bec12023-08-31 16:13:39 -0700457 status_t addEffect_ll(const sp<IAfEffectModule>& effect) final
458 REQUIRES(audio_utils::AudioFlinger_Mutex, mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800459 // remove and effect module. Also removes the effect chain is this was the last
460 // effect
Andy Hungab65b182023-09-06 19:41:47 -0700461 void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false) final
462 REQUIRES(mutex());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800463 // disconnect an effect handle from module and destroy module if last handle
Andy Hung440901d2023-06-29 21:19:25 -0700464 void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800465 // detach all tracks connected to an auxiliary effect
Andy Hungab65b182023-09-06 19:41:47 -0700466 void detachAuxEffect_l(int /* effectId */) override REQUIRES(mutex()) {}
Andy Hung99b1ba62023-07-14 11:00:08 -0700467 // TODO(b/291317898) - remove hasAudioSession_l below.
Andy Hungab65b182023-09-06 19:41:47 -0700468 uint32_t hasAudioSession_l(audio_session_t sessionId) const override REQUIRES(mutex()) = 0;
469 uint32_t hasAudioSession(audio_session_t sessionId) const final EXCLUDES_ThreadBase_Mutex {
Andy Hung8d672e02023-09-15 18:19:28 -0700470 audio_utils::lock_guard _l(mutex());
Andy Hungab65b182023-09-06 19:41:47 -0700471 return hasAudioSession_l(sessionId);
472 }
Eric Laurent4c415062016-06-17 16:14:16 -0700473
Andy Hungc3d62f92019-03-14 13:38:51 -0700474 template <typename T>
Andy Hung8d672e02023-09-15 18:19:28 -0700475 uint32_t hasAudioSession_l(audio_session_t sessionId, const T& tracks) const
476 REQUIRES(mutex()) {
Andy Hungc3d62f92019-03-14 13:38:51 -0700477 uint32_t result = 0;
478 if (getEffectChain_l(sessionId) != 0) {
479 result = EFFECT_SESSION;
480 }
481 for (size_t i = 0; i < tracks.size(); ++i) {
Andy Hung8d31fd22023-06-26 19:20:57 -0700482 const sp<IAfTrackBase>& track = tracks[i];
Andy Hungc3d62f92019-03-14 13:38:51 -0700483 if (sessionId == track->sessionId()
484 && !track->isInvalid() // not yet removed from tracks.
485 && !track->isTerminated()) {
486 result |= TRACK_SESSION;
487 if (track->isFastTrack()) {
488 result |= FAST_SESSION; // caution, only represents first track.
489 }
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200490 if (track->isSpatialized()) {
Eric Laurentb62d0362021-10-26 17:40:18 +0200491 result |= SPATIALIZED_SESSION; // caution, only first track.
492 }
jiabinc658e452022-10-21 20:52:21 +0000493 if (track->isBitPerfect()) {
494 result |= BIT_PERFECT_SESSION;
495 }
Andy Hungc3d62f92019-03-14 13:38:51 -0700496 break;
497 }
498 }
499 return result;
500 }
501
Eric Laurent81784c32012-11-19 14:55:58 -0800502 // the value returned by default implementation is not important as the
503 // strategy is only meaningful for PlaybackThread which implements this method
Andy Hung440901d2023-06-29 21:19:25 -0700504 product_strategy_t getStrategyForSession_l(
Andy Hungab65b182023-09-06 19:41:47 -0700505 audio_session_t /* sessionId */) const override REQUIRES(mutex()){
506 return static_cast<product_strategy_t>(0);
507 }
Eric Laurent81784c32012-11-19 14:55:58 -0800508
Eric Laurent81784c32012-11-19 14:55:58 -0800509 // check if some effects must be suspended/restored when an effect is enabled
510 // or disabled
Andy Hung440901d2023-06-29 21:19:25 -0700511 void checkSuspendOnEffectEnabled(bool enabled,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800512 audio_session_t sessionId,
Andy Hung440901d2023-06-29 21:19:25 -0700513 bool threadLocked) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800514
Eric Laurent81784c32012-11-19 14:55:58 -0800515
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700516 // Return a reference to a per-thread heap which can be used to allocate IMemory
517 // objects that will be read-only to client processes, read/write to mediaserver,
518 // and shared by all client processes of the thread.
519 // The heap is per-thread rather than common across all threads, because
520 // clients can't be trusted not to modify the offset of the IMemory they receive.
521 // If a thread does not have such a heap, this method returns 0.
Andy Hung440901d2023-06-29 21:19:25 -0700522 sp<MemoryDealer> readOnlyHeap() const override { return nullptr; }
Eric Laurent81784c32012-11-19 14:55:58 -0800523
Andy Hung440901d2023-06-29 21:19:25 -0700524 sp<IMemory> pipeMemory() const override { return nullptr; }
Glenn Kasten6181ffd2014-05-13 10:41:52 -0700525
Andy Hungab65b182023-09-06 19:41:47 -0700526 void systemReady() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent72e3f392015-05-20 14:43:50 -0700527
Andy Hungab65b182023-09-06 19:41:47 -0700528 void broadcast_l() final REQUIRES(mutex());
Eric Laurent4c415062016-06-17 16:14:16 -0700529
Andy Hungab65b182023-09-06 19:41:47 -0700530 bool isTimestampCorrectionEnabled_l() const override REQUIRES(mutex()) { return false; }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800531
Andy Hung440901d2023-06-29 21:19:25 -0700532 bool isMsdDevice() const final { return mIsMsdDevice; }
Andy Hungc8fddf32018-08-08 18:32:37 -0700533
Andy Hung440901d2023-06-29 21:19:25 -0700534 void dump(int fd, const Vector<String16>& args) override;
Andy Hungdc099c22018-09-18 13:46:39 -0700535
Andy Hungd0979812019-02-21 15:51:44 -0800536 // deliver stats to mediametrics.
Andy Hung8d672e02023-09-15 18:19:28 -0700537 void sendStatistics(bool force) final
538 REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex;
Andy Hungd0979812019-02-21 15:51:44 -0800539
Andy Hung972bec12023-08-31 16:13:39 -0700540 audio_utils::mutex& mutex() const final RETURN_CAPABILITY(audio_utils::ThreadBase_Mutex) {
Andy Hungc5007f82023-08-29 14:26:09 -0700541 return mMutex;
Andy Hung440901d2023-06-29 21:19:25 -0700542 }
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700543 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kThreadBase_Mutex};
Eric Laurent81784c32012-11-19 14:55:58 -0800544
Andy Hungab65b182023-09-06 19:41:47 -0700545 void onEffectEnable(const sp<IAfEffectModule>& effect) final EXCLUDES_ThreadBase_Mutex;
546 void onEffectDisable() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800547
Andy Hungc5007f82023-08-29 14:26:09 -0700548 // invalidateTracksForAudioSession_l must be called with holding mutex().
Andy Hungab65b182023-09-06 19:41:47 -0700549 void invalidateTracksForAudioSession_l(audio_session_t /* sessionId */) const override
550 REQUIRES(mutex()) {}
jiabineb3bda02020-06-30 14:07:03 -0700551 // Invalidate all the tracks with the given audio session.
Andy Hungab65b182023-09-06 19:41:47 -0700552 void invalidateTracksForAudioSession(audio_session_t sessionId) const final
553 EXCLUDES_ThreadBase_Mutex {
Andy Hung8d672e02023-09-15 18:19:28 -0700554 audio_utils::lock_guard _l(mutex());
jiabineb3bda02020-06-30 14:07:03 -0700555 invalidateTracksForAudioSession_l(sessionId);
556 }
557
558 template <typename T>
Andy Hungab65b182023-09-06 19:41:47 -0700559 void invalidateTracksForAudioSession_l(audio_session_t sessionId,
560 const T& tracks) const REQUIRES(mutex()) {
jiabineb3bda02020-06-30 14:07:03 -0700561 for (size_t i = 0; i < tracks.size(); ++i) {
Andy Hung8d31fd22023-06-26 19:20:57 -0700562 const sp<IAfTrackBase>& track = tracks[i];
jiabineb3bda02020-06-30 14:07:03 -0700563 if (sessionId == track->sessionId()) {
564 track->invalidate();
565 }
566 }
567 }
568
Andy Hung972bec12023-08-31 16:13:39 -0700569 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override
570 REQUIRES(audio_utils::AudioFlinger_Mutex);
571 void stopMelComputation_l() override
572 REQUIRES(audio_utils::AudioFlinger_Mutex);
Vlad Popa6fbbfbf2023-02-22 15:05:43 +0100573
Andy Hung56ce2ed2024-06-12 16:03:16 -0700574 audio_utils::DeferredExecutor& getThreadloopExecutor() override {
575 return mThreadloopExecutor;
576 }
577
Eric Laurent81784c32012-11-19 14:55:58 -0800578protected:
579
580 // entry describing an effect being suspended in mSuspendedSessions keyed vector
581 class SuspendedSessionDesc : public RefBase {
582 public:
583 SuspendedSessionDesc() : mRefCount(0) {}
584
585 int mRefCount; // number of active suspend requests
586 effect_uuid_t mType; // effect type UUID
587 };
588
Andy Hungab65b182023-09-06 19:41:47 -0700589 void acquireWakeLock() EXCLUDES_ThreadBase_Mutex;
590 virtual void acquireWakeLock_l() REQUIRES(mutex());
591 void releaseWakeLock() EXCLUDES_ThreadBase_Mutex;
592 void releaseWakeLock_l() REQUIRES(mutex());
593 void updateWakeLockUids_l(const SortedVector<uid_t> &uids) REQUIRES(mutex());
594 void getPowerManager_l() REQUIRES(mutex());
Eric Laurentd8365c52017-07-16 15:27:05 -0700595 // suspend or restore effects of the specified type (or all if type is NULL)
596 // on a given session. The number of suspend requests is counted and restore
597 // occurs when all suspend requests are cancelled.
Andy Hungab65b182023-09-06 19:41:47 -0700598 void setEffectSuspended_l(const effect_uuid_t *type,
Eric Laurent81784c32012-11-19 14:55:58 -0800599 bool suspend,
Andy Hungab65b182023-09-06 19:41:47 -0700600 audio_session_t sessionId) final REQUIRES(mutex());
Eric Laurentd8365c52017-07-16 15:27:05 -0700601 // updated mSuspendedSessions when an effect is suspended or restored
Andy Hungab65b182023-09-06 19:41:47 -0700602 void updateSuspendedSessions_l(const effect_uuid_t *type,
Eric Laurent81784c32012-11-19 14:55:58 -0800603 bool suspend,
Andy Hungab65b182023-09-06 19:41:47 -0700604 audio_session_t sessionId) REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800605 // check if some effects must be suspended when an effect chain is added
Andy Hungab65b182023-09-06 19:41:47 -0700606 void checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain) REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800607
Andy Hung6c498e92023-12-05 17:28:17 -0800608 /**
609 * waitWhileThreadBusy_l() serves as a mutex gate, which does not allow
610 * progress beyond the method while the PlaybackThread is busy (see setThreadBusy_l()).
611 * During the wait, the ThreadBase_Mutex is temporarily unlocked.
612 *
613 * This implementation uses a condition variable. Alternative methods to gate
614 * the thread may use a second mutex (i.e. entry based on scoped_lock(mutex, gating_mutex)),
615 * but those have less flexibility and more lock order issues.
616 *
617 * Current usage by Track::destroy(), Track::start(), Track::stop(), Track::pause(),
618 * and Track::flush() block this way, and the primary caller is through TrackHandle
619 * with no other mutexes held.
620 *
621 * Special tracks like PatchTrack and OutputTrack may also hold the another thread's
622 * ThreadBase_Mutex during this time. No other mutex is held.
623 */
624
625 void waitWhileThreadBusy_l(audio_utils::unique_lock& ul) final REQUIRES(mutex()) {
626 // the wait returns immediately if the predicate is satisfied.
627 mThreadBusyCv.wait(ul, [&]{ return mThreadBusy == false;});
628 }
629
630 void setThreadBusy_l(bool busy) REQUIRES(mutex()) {
631 if (busy == mThreadBusy) return;
632 mThreadBusy = busy;
633 if (busy == true) return; // no need to wake threads if we become busy.
634 mThreadBusyCv.notify_all();
635 }
636
Kevin Rocard069c2712018-03-29 19:09:14 -0700637 // sends the metadata of the active tracks to the HAL
Vlad Popa7e81cea2023-01-19 16:34:16 +0100638 struct MetadataUpdate {
639 std::vector<playback_track_metadata_v7_t> playbackMetadataUpdate;
640 std::vector<record_track_metadata_v7_t> recordMetadataUpdate;
641 };
Andy Hung8d672e02023-09-15 18:19:28 -0700642 // NO_THREAD_SAFETY_ANALYSIS, updateMetadata_l() should include ThreadBase_ThreadLoop
643 // but MmapThread::start() -> exitStandby_l() -> updateMetadata_l() prevents this.
Andy Hungab65b182023-09-06 19:41:47 -0700644 virtual MetadataUpdate updateMetadata_l() REQUIRES(mutex()) = 0;
Kevin Rocard069c2712018-03-29 19:09:14 -0700645
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100646 String16 getWakeLockTag();
647
Andy Hungab65b182023-09-06 19:41:47 -0700648 virtual void preExit() EXCLUDES_ThreadBase_Mutex {}
649 virtual void setMasterMono_l(bool mono __unused) REQUIRES(mutex()) {}
Andy Hung2ddee192015-12-18 17:34:44 -0800650 virtual bool requireMonoBlend() { return false; }
Eric Laurent81784c32012-11-19 14:55:58 -0800651
Andy Hung1c86ebe2018-05-29 20:29:08 -0700652 // called within the threadLoop to obtain timestamp from the HAL.
Andy Hungab65b182023-09-06 19:41:47 -0700653 virtual status_t threadloop_getHalTimestamp_l(
Andy Hung8d672e02023-09-15 18:19:28 -0700654 ExtendedTimestamp *timestamp __unused) const
655 REQUIRES(mutex(), ThreadBase_ThreadLoop) {
Andy Hung1c86ebe2018-05-29 20:29:08 -0700656 return INVALID_OPERATION;
657 }
Andy Hung116bc262023-06-20 18:56:17 -0700658public:
Andy Hung99b1ba62023-07-14 11:00:08 -0700659// TODO(b/291317898) organize with publics
Eric Laurentd66d7a12021-07-13 13:35:32 +0200660 product_strategy_t getStrategyForStream(audio_stream_type_t stream) const;
Andy Hung116bc262023-06-20 18:56:17 -0700661protected:
Eric Laurentd66d7a12021-07-13 13:35:32 +0200662
Andy Hungab65b182023-09-06 19:41:47 -0700663 virtual void onHalLatencyModesChanged_l() REQUIRES(mutex()) {}
Eric Laurentb0463942022-12-20 16:31:10 +0100664
Andy Hungab65b182023-09-06 19:41:47 -0700665 virtual void dumpInternals_l(int fd __unused, const Vector<String16>& args __unused)
666 REQUIRES(mutex()) {}
667 virtual void dumpTracks_l(int fd __unused, const Vector<String16>& args __unused)
668 REQUIRES(mutex()) {}
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700669
Eric Laurent81784c32012-11-19 14:55:58 -0800670 const type_t mType;
671
672 // Used by parameters, config events, addTrack_l, exit
Andy Hungc5007f82023-08-29 14:26:09 -0700673 audio_utils::condition_variable mWaitWorkCV;
Eric Laurent81784c32012-11-19 14:55:58 -0800674
Andy Hung583043b2023-07-17 17:05:00 -0700675 const sp<IAfThreadCallback> mAfThreadCallback;
Andy Hungcf10d742020-04-28 15:38:24 -0700676 ThreadMetrics mThreadMetrics;
677 const bool mIsOut;
Glenn Kasten9b58f632013-07-16 11:37:48 -0700678
Andy Hung6c498e92023-12-05 17:28:17 -0800679 // mThreadBusy is checked under the ThreadBase_Mutex to ensure that
680 // TrackHandle operations do not proceed while the ThreadBase is busy
681 // with the track. mThreadBusy is only true if the track is active.
682 //
683 bool mThreadBusy = false; // GUARDED_BY(ThreadBase_Mutex) but read in lambda.
684 audio_utils::condition_variable mThreadBusyCv;
685
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800686 // updated by PlaybackThread::readOutputParameters_l() or
687 // RecordThread::readInputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -0800688 uint32_t mSampleRate;
689 size_t mFrameCount; // output HAL, direct output, record
Eric Laurent81784c32012-11-19 14:55:58 -0800690 audio_channel_mask_t mChannelMask;
Glenn Kastenf6ed4232013-07-16 11:16:27 -0700691 uint32_t mChannelCount;
Eric Laurent81784c32012-11-19 14:55:58 -0800692 size_t mFrameSize;
Glenn Kasten97b7b752014-09-28 13:04:24 -0700693 // not HAL frame size, this is for output sink (to pipe to fast mixer)
Andy Hung463be252014-07-10 16:56:07 -0700694 audio_format_t mFormat; // Source format for Recording and
695 // Sink format for Playback.
696 // Sink format may be different than
697 // HAL format if Fastmixer is used.
698 audio_format_t mHALFormat;
Glenn Kasten70949c42013-08-06 07:40:12 -0700699 size_t mBufferSize; // HAL buffer size for read() or write()
Andy Hungab65b182023-09-06 19:41:47 -0700700
701 // output device types and addresses
702 AudioDeviceTypeAddrVector mOutDeviceTypeAddrs GUARDED_BY(mutex());
703 AudioDeviceTypeAddr mInDeviceTypeAddr GUARDED_BY(mutex()); // input device type and address
Andy Hung8d672e02023-09-15 18:19:28 -0700704 Vector<sp<ConfigEvent>> mConfigEvents GUARDED_BY(mutex());
705
706 // events awaiting system ready
707 Vector<sp<ConfigEvent>> mPendingConfigEvents GUARDED_BY(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800708
709 // These fields are written and read by thread itself without lock or barrier,
jiabinc52b1ff2019-10-31 17:20:42 -0700710 // and read by other threads without lock or barrier via standby(), outDeviceTypes()
711 // and inDeviceType().
Eric Laurent81784c32012-11-19 14:55:58 -0800712 // Because of the absence of a lock or barrier, any other thread that reads
713 // these fields must use the information in isolation, or be prepared to deal
714 // with possibility that it might be inconsistent with other information.
Glenn Kasten4944acb2013-08-19 08:39:20 -0700715 bool mStandby; // Whether thread is currently in standby.
jiabinc52b1ff2019-10-31 17:20:42 -0700716
Andy Hung8d672e02023-09-15 18:19:28 -0700717 // NO_THREAD_SAFETY_ANALYSIS - mPatch and mAudioSource should be guarded by mutex().
Eric Laurent296fb132015-05-01 11:38:42 -0700718 struct audio_patch mPatch;
Glenn Kastenf59497b2015-01-26 16:35:47 -0800719 audio_source_t mAudioSource;
Eric Laurent81784c32012-11-19 14:55:58 -0800720
721 const audio_io_handle_t mId;
Andy Hung8d672e02023-09-15 18:19:28 -0700722 Vector<sp<IAfEffectChain>> mEffectChains GUARDED_BY(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800723
Glenn Kastend7dca052015-03-05 16:05:54 -0800724 static const int kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
725 char mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
Andy Hung8d672e02023-09-15 18:19:28 -0700726 sp<os::IPowerManager> mPowerManager GUARDED_BY(mutex());
727 sp<IBinder> mWakeLockToken GUARDED_BY(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800728 const sp<PMDeathRecipient> mDeathRecipient;
Glenn Kastend848eb42016-03-08 13:42:11 -0800729 // list of suspended effects per session and per type. The first (outer) vector is
730 // keyed by session ID, the second (inner) by type UUID timeLow field
Eric Laurentd8365c52017-07-16 15:27:05 -0700731 // Updated by updateSuspendedSessions_l() only.
Glenn Kastend848eb42016-03-08 13:42:11 -0800732 KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
Eric Laurent81784c32012-11-19 14:55:58 -0800733 mSuspendedSessions;
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700734 // TODO: add comment and adjust size as needed
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800735 static const size_t kLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800736 sp<NBLog::Writer> mNBLogWriter;
Eric Laurent72e3f392015-05-20 14:43:50 -0700737 bool mSystemReady;
Andy Hung8d672e02023-09-15 18:19:28 -0700738
739 // NO_THREAD_SAFETY_ANALYSIS - mTimestamp and mTimestampVerifier should be
740 // accessed under mutex for the RecordThread.
741 ExtendedTimestamp mTimestamp;
742 TimestampVerifier<int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier;
Dean Wheatley12473e92021-03-18 23:00:55 +1100743 // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
744 // TODO: add confirmation checks:
745 // 1) DIRECT threads and linear PCM format really resets to 0?
746 // 2) Is frame count really valid if not linear pcm?
747 // 3) Are all 64 bits of position returned, not just lowest 32 bits?
jiabinc52b1ff2019-10-31 17:20:42 -0700748 // Timestamp corrected device should be a single device.
Andy Hung8d672e02023-09-15 18:19:28 -0700749
750 audio_devices_t mTimestampCorrectedDevice = AUDIO_DEVICE_NONE; // CONST set in ctor
Andy Hung446f4df2019-02-21 12:26:41 -0800751
752 // ThreadLoop statistics per iteration.
Andy Hung8d672e02023-09-15 18:19:28 -0700753 std::atomic<int64_t> mLastIoBeginNs = -1; // set in threadLoop, read by dump()
754 int64_t mLastIoEndNs GUARDED_BY(ThreadBase_ThreadLoop) = -1;
Andy Hung446f4df2019-02-21 12:26:41 -0800755
Andy Hung44d648b2022-04-08 17:33:40 -0700756 // ThreadSnapshot is thread-safe (internally locked)
757 mediautils::ThreadSnapshot mThreadSnapshot;
758
Andy Hung8d672e02023-09-15 18:19:28 -0700759 audio_utils::Statistics<double> mIoJitterMs GUARDED_BY(mutex()) {0.995 /* alpha */};
760 audio_utils::Statistics<double> mProcessTimeMs GUARDED_BY(mutex()) {0.995 /* alpha */};
761
762 // NO_THREAD_SAFETY_ANALYSIS GUARDED_BY(mutex())
Andy Hunge6c37112019-02-26 17:38:10 -0800763 audio_utils::Statistics<double> mLatencyMs{0.995 /* alpha */};
Robert Wu06db0a32021-08-10 19:05:34 +0000764 audio_utils::Statistics<double> mMonopipePipeDepthStats{0.999 /* alpha */};
Andy Hung446f4df2019-02-21 12:26:41 -0800765
Andy Hungd0979812019-02-21 15:51:44 -0800766 // Save the last count when we delivered statistics to mediametrics.
767 int64_t mLastRecordedTimestampVerifierN = 0;
768 int64_t mLastRecordedTimeNs = 0; // BOOTTIME to include suspend.
769
Andy Hungc8fddf32018-08-08 18:32:37 -0700770 bool mIsMsdDevice = false;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800771 // A condition that must be evaluated by the thread loop has changed and
772 // we must not wait for async write callback in the thread loop before evaluating it
773 bool mSignalPending;
Andy Hungdae27702016-10-31 14:01:16 -0700774
Andy Hung8946a282018-04-19 20:04:56 -0700775#ifdef TEE_SINK
776 NBAIO_Tee mTee;
777#endif
Andy Hungdae27702016-10-31 14:01:16 -0700778 // ActiveTracks is a sorted vector of track type T representing the
779 // active tracks of threadLoop() to be considered by the locked prepare portion.
780 // ActiveTracks should be accessed with the ThreadBase lock held.
781 //
782 // During processing and I/O, the threadLoop does not hold the lock;
783 // hence it does not directly use ActiveTracks. Care should be taken
784 // to hold local strong references or defer removal of tracks
785 // if the threadLoop may still be accessing those tracks due to mix, etc.
786 //
787 // This class updates power information appropriately.
788 //
789
790 template <typename T>
791 class ActiveTracks {
792 public:
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700793 explicit ActiveTracks(SimpleLog *localLog = nullptr)
Andy Hungdae27702016-10-31 14:01:16 -0700794 : mActiveTracksGeneration(0)
795 , mLastActiveTracksGeneration(0)
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700796 , mLocalLog(localLog)
Andy Hungdae27702016-10-31 14:01:16 -0700797 { }
798
799 ~ActiveTracks() {
800 ALOGW_IF(!mActiveTracks.isEmpty(),
801 "ActiveTracks should be empty in destructor");
802 }
803 // returns the last track added (even though it may have been
804 // subsequently removed from ActiveTracks).
805 //
806 // Used for DirectOutputThread to ensure a flush is called when transitioning
807 // to a new track (even though it may be on the same session).
808 // Used for OffloadThread to ensure that volume and mixer state is
809 // taken from the latest track added.
810 //
811 // The latest track is saved with a weak pointer to prevent keeping an
812 // otherwise useless track alive. Thus the function will return nullptr
813 // if the latest track has subsequently been removed and destroyed.
814 sp<T> getLatest() {
815 return mLatestActiveTrack.promote();
816 }
817
818 // SortedVector methods
819 ssize_t add(const sp<T> &track);
820 ssize_t remove(const sp<T> &track);
821 size_t size() const {
822 return mActiveTracks.size();
823 }
Eric Tan39ec8d62018-07-24 09:49:29 -0700824 bool isEmpty() const {
825 return mActiveTracks.isEmpty();
826 }
Andy Hung87c693c2023-07-06 20:56:16 -0700827 ssize_t indexOf(const sp<T>& item) const {
Andy Hungdae27702016-10-31 14:01:16 -0700828 return mActiveTracks.indexOf(item);
829 }
830 sp<T> operator[](size_t index) const {
831 return mActiveTracks[index];
832 }
833 typename SortedVector<sp<T>>::iterator begin() {
834 return mActiveTracks.begin();
835 }
836 typename SortedVector<sp<T>>::iterator end() {
837 return mActiveTracks.end();
838 }
839
840 // Due to Binder recursion optimization, clear() and updatePowerState()
841 // cannot be called from a Binder thread because they may call back into
842 // the original calling process (system server) for BatteryNotifier
843 // (which requires a Java environment that may not be present).
844 // Hence, call clear() and updatePowerState() only from the
845 // ThreadBase thread.
846 void clear();
847 // periodically called in the threadLoop() to update power state uids.
Andy Hungab65b182023-09-06 19:41:47 -0700848 void updatePowerState_l(const sp<ThreadBase>& thread, bool force = false)
849 REQUIRES(audio_utils::ThreadBase_Mutex);
Andy Hungdae27702016-10-31 14:01:16 -0700850
Kevin Rocardc86a7f72018-04-03 09:00:09 -0700851 /** @return true if one or move active tracks was added or removed since the
Jasmine Chaeaa10e42021-05-11 10:11:14 +0800852 * last time this function was called or the vector was created.
853 * true if volume of one of active tracks was changed.
854 */
Kevin Rocard069c2712018-03-29 19:09:14 -0700855 bool readAndClearHasChanged();
856
Eric Laurentdda206a2022-07-08 17:28:35 +0200857 /** Force updating track metadata to audio HAL stream next time
858 * readAndClearHasChanged() is called.
859 */
860 void setHasChanged() { mHasChanged = true; }
861
Andy Hungdae27702016-10-31 14:01:16 -0700862 private:
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700863 void logTrack(const char *funcName, const sp<T> &track) const;
864
Andy Hungd01b0f12016-11-07 16:10:30 -0800865 SortedVector<uid_t> getWakeLockUids() {
866 SortedVector<uid_t> wakeLockUids;
Andy Hungdae27702016-10-31 14:01:16 -0700867 for (const sp<T> &track : mActiveTracks) {
868 wakeLockUids.add(track->uid());
869 }
870 return wakeLockUids; // moved by underlying SharedBuffer
871 }
872
Andy Hungdae27702016-10-31 14:01:16 -0700873 SortedVector<sp<T>> mActiveTracks;
874 int mActiveTracksGeneration;
875 int mLastActiveTracksGeneration;
876 wp<T> mLatestActiveTrack; // latest track added to ActiveTracks
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700877 SimpleLog * const mLocalLog;
Kevin Rocardc86a7f72018-04-03 09:00:09 -0700878 // If the vector has changed since last call to readAndClearHasChanged
Kevin Rocard069c2712018-03-29 19:09:14 -0700879 bool mHasChanged = false;
Andy Hungdae27702016-10-31 14:01:16 -0700880 };
Andy Hung293558a2017-03-21 12:19:20 -0700881
Andy Hungab65b182023-09-06 19:41:47 -0700882 SimpleLog mLocalLog; // locked internally
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700883
Andy Hung56ce2ed2024-06-12 16:03:16 -0700884 // mThreadloopExecutor contains deferred functors and object (dtors) to
885 // be executed at the end of the processing period, without any
886 // mutexes held.
887 //
888 // mThreadloopExecutor is locked internally, so its methods are thread-safe
889 // for access.
890 audio_utils::DeferredExecutor mThreadloopExecutor;
891
Andy Hung6c498e92023-12-05 17:28:17 -0800892 private:
Andy Hungab65b182023-09-06 19:41:47 -0700893 void dumpBase_l(int fd, const Vector<String16>& args) REQUIRES(mutex());
894 void dumpEffectChains_l(int fd, const Vector<String16>& args) REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800895};
896
897// --- PlaybackThread ---
Andy Hung440901d2023-06-29 21:19:25 -0700898class PlaybackThread : public ThreadBase, public virtual IAfPlaybackThread,
899 public StreamOutHalInterfaceCallback,
Andy Hung87c693c2023-07-06 20:56:16 -0700900 public virtual VolumeInterface, public StreamOutHalInterfaceEventCallback {
Eric Laurent81784c32012-11-19 14:55:58 -0800901public:
Andy Hung87c693c2023-07-06 20:56:16 -0700902 sp<IAfPlaybackThread> asIAfPlaybackThread() final {
903 return sp<IAfPlaybackThread>::fromExisting(this);
904 }
Eric Laurent81784c32012-11-19 14:55:58 -0800905
Eric Laurente93cc032016-05-05 10:15:10 -0700906 // retry count before removing active track in case of underrun on offloaded thread:
907 // we need to make sure that AudioTrack client has enough time to send large buffers
908 //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
909 // handled for offloaded tracks
910 static const int8_t kMaxTrackRetriesOffload = 20;
911 static const int8_t kMaxTrackStartupRetriesOffload = 100;
Andy Hung8ed196a2018-01-05 13:21:11 -0800912 static constexpr uint32_t kMaxTracksPerUid = 40;
Andy Hung1bc088a2018-02-09 15:57:31 -0800913 static constexpr size_t kMaxTracks = 256;
Eric Laurente93cc032016-05-05 10:15:10 -0700914
rago1bb90822017-05-02 18:31:48 -0700915 // Maximum delay (in nanoseconds) for upcoming buffers in suspend mode, otherwise
916 // if delay is greater, the estimated time for timeLoopNextNs is reset.
917 // This allows for catch-up to be done for small delays, while resetting the estimate
918 // for initial conditions or large delays.
919 static const nsecs_t kMaxNextBufferDelayNs = 100000000;
920
Andy Hung583043b2023-07-17 17:05:00 -0700921 PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200922 audio_io_handle_t id, type_t type, bool systemReady,
923 audio_config_base_t *mixerConfig = nullptr);
Andy Hung440901d2023-06-29 21:19:25 -0700924 ~PlaybackThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800925
Eric Laurent81784c32012-11-19 14:55:58 -0800926 // Thread virtuals
Andy Hung8d672e02023-09-15 18:19:28 -0700927 bool threadLoop() final REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800928
929 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -0700930 void onFirstRef() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800931
Andy Hung440901d2023-06-29 21:19:25 -0700932 status_t checkEffectCompatibility_l(
Andy Hungab65b182023-09-06 19:41:47 -0700933 const effect_descriptor_t* desc, audio_session_t sessionId) final REQUIRES(mutex());
Eric Laurent4c415062016-06-17 16:14:16 -0700934
Andy Hungab65b182023-09-06 19:41:47 -0700935 void addOutputTrack_l(const sp<IAfTrack>& track) final REQUIRES(mutex()) {
Andy Hung87c693c2023-07-06 20:56:16 -0700936 mTracks.add(track);
937 }
938
Eric Laurent81784c32012-11-19 14:55:58 -0800939protected:
940 // Code snippets that were lifted up out of threadLoop()
Andy Hung8d672e02023-09-15 18:19:28 -0700941 virtual void threadLoop_mix() REQUIRES(ThreadBase_ThreadLoop) = 0;
942 virtual void threadLoop_sleepTime() REQUIRES(ThreadBase_ThreadLoop) = 0;
943 virtual ssize_t threadLoop_write() REQUIRES(ThreadBase_ThreadLoop);
944 virtual void threadLoop_drain() REQUIRES(ThreadBase_ThreadLoop);
945 virtual void threadLoop_standby() REQUIRES(ThreadBase_ThreadLoop);
946 virtual void threadLoop_exit() REQUIRES(ThreadBase_ThreadLoop);
947 virtual void threadLoop_removeTracks(const Vector<sp<IAfTrack>>& tracksToRemove)
948 REQUIRES(ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -0800949
950 // prepareTracks_l reads and writes mActiveTracks, and returns
951 // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller
952 // is responsible for clearing or destroying this Vector later on, when it
953 // is safe to do so. That will drop the final ref count and destroy the tracks.
Andy Hung8d672e02023-09-15 18:19:28 -0700954 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove)
955 REQUIRES(mutex(), ThreadBase_ThreadLoop) = 0;
956
957 void removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove) REQUIRES(mutex());
958 status_t handleVoipVolume_l(float *volume) REQUIRES(mutex());
Eric Laurentbfb1b832013-01-07 09:53:42 -0800959
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700960 // StreamOutHalInterfaceCallback implementation
961 virtual void onWriteReady();
962 virtual void onDrainReady();
Mikhail Naganovbf203ce2024-05-23 16:27:59 -0700963 virtual void onError(bool /*isHardError*/);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800964
Andy Hungee58e4a2023-07-07 13:47:37 -0700965public: // AsyncCallbackThread
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700966 void resetWriteBlocked(uint32_t sequence);
967 void resetDraining(uint32_t sequence);
Andy Hungee58e4a2023-07-07 13:47:37 -0700968protected:
Eric Laurentbfb1b832013-01-07 09:53:42 -0800969
970 virtual bool waitingAsyncCallback();
Andy Hungab65b182023-09-06 19:41:47 -0700971 virtual bool waitingAsyncCallback_l() REQUIRES(mutex());
Andy Hung8d672e02023-09-15 18:19:28 -0700972 virtual bool shouldStandby_l() REQUIRES(mutex(), ThreadBase_ThreadLoop);
Andy Hungab65b182023-09-06 19:41:47 -0700973 virtual void onAddNewTrack_l() REQUIRES(mutex());
Andy Hungee58e4a2023-07-07 13:47:37 -0700974public: // AsyncCallbackThread
Mikhail Naganovbf203ce2024-05-23 16:27:59 -0700975 void onAsyncError(bool isHardError); // error reported by AsyncCallbackThread
Andy Hungee58e4a2023-07-07 13:47:37 -0700976protected:
jiabinf6eb4c32020-02-25 14:06:25 -0800977 // StreamHalInterfaceCodecFormatCallback implementation
978 void onCodecFormatChanged(
Ryan Prichard78c5e452024-02-08 16:16:57 -0800979 const std::vector<uint8_t>& metadataBs) final;
jiabinf6eb4c32020-02-25 14:06:25 -0800980
Eric Laurent81784c32012-11-19 14:55:58 -0800981 // ThreadBase virtuals
Andy Hungab65b182023-09-06 19:41:47 -0700982 void preExit() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800983
Eric Laurent64667972016-03-30 18:19:46 -0700984 virtual bool keepWakeLock() const { return true; }
Andy Hungab65b182023-09-06 19:41:47 -0700985 virtual void acquireWakeLock_l() REQUIRES(mutex()) {
Andy Hungdae27702016-10-31 14:01:16 -0700986 ThreadBase::acquireWakeLock_l();
Andy Hungab65b182023-09-06 19:41:47 -0700987 mActiveTracks.updatePowerState_l(this, true /* force */);
Andy Hungdae27702016-10-31 14:01:16 -0700988 }
Eric Laurent64667972016-03-30 18:19:46 -0700989
Andy Hung8d672e02023-09-15 18:19:28 -0700990 virtual void checkOutputStageEffects()
991 REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex {}
Eric Laurent68a40a82022-05-03 18:15:04 +0200992 virtual void setHalLatencyMode_l() {}
993
Eric Laurentb3f315a2021-07-13 15:09:05 +0200994
Andy Hungab65b182023-09-06 19:41:47 -0700995 void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex());
996 void dumpTracks_l(int fd, const Vector<String16>& args) final REQUIRES(mutex());
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700997
Eric Laurent81784c32012-11-19 14:55:58 -0800998public:
999
Andy Hung440901d2023-06-29 21:19:25 -07001000 status_t initCheck() const final { return mOutput == nullptr ? NO_INIT : NO_ERROR; }
Eric Laurent81784c32012-11-19 14:55:58 -08001001
1002 // return estimated latency in milliseconds, as reported by HAL
Andy Hung440901d2023-06-29 21:19:25 -07001003 uint32_t latency() const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001004 // same, but lock must already be held
Andy Hungab65b182023-09-06 19:41:47 -07001005 uint32_t latency_l() const final /* REQUIRES(mutex()) */; // NO_THREAD_SAFETY_ANALYSIS
Eric Laurent81784c32012-11-19 14:55:58 -08001006
Eric Laurent6acd1d42017-01-04 14:23:29 -08001007 // VolumeInterface
Andy Hung440901d2023-06-29 21:19:25 -07001008 void setMasterVolume(float value) final;
Andy Hungab65b182023-09-06 19:41:47 -07001009 void setMasterBalance(float balance) override EXCLUDES_ThreadBase_Mutex;
Andy Hung440901d2023-06-29 21:19:25 -07001010 void setMasterMute(bool muted) final;
Andy Hungab65b182023-09-06 19:41:47 -07001011 void setStreamVolume(audio_stream_type_t stream, float value) final EXCLUDES_ThreadBase_Mutex;
1012 void setStreamMute(audio_stream_type_t stream, bool muted) final EXCLUDES_ThreadBase_Mutex;
1013 float streamVolume(audio_stream_type_t stream) const final EXCLUDES_ThreadBase_Mutex;
Andy Hung440901d2023-06-29 21:19:25 -07001014 void setVolumeForOutput_l(float left, float right) const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001015
Andy Hung440901d2023-06-29 21:19:25 -07001016 sp<IAfTrack> createTrack_l(
Andy Hung88035ac2023-06-27 17:05:02 -07001017 const sp<Client>& client,
Eric Laurent81784c32012-11-19 14:55:58 -08001018 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001019 const audio_attributes_t& attr,
Eric Laurent21da6472017-11-09 16:29:26 -08001020 uint32_t *sampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -08001021 audio_format_t format,
1022 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001023 size_t *pFrameCount,
Eric Laurent21da6472017-11-09 16:29:26 -08001024 size_t *pNotificationFrameCount,
1025 uint32_t notificationsPerBuffer,
1026 float speed,
Eric Laurent81784c32012-11-19 14:55:58 -08001027 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -08001028 audio_session_t sessionId,
Eric Laurent05067782016-06-01 18:27:28 -07001029 audio_output_flags_t *flags,
Eric Laurent09f1ed22019-04-24 17:45:17 -07001030 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00001031 const AttributionSourceState& attributionSource,
Eric Laurent81784c32012-11-19 14:55:58 -08001032 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001033 status_t *status /*non-NULL*/,
jiabinf6eb4c32020-02-25 14:06:25 -08001034 audio_port_handle_t portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001035 const sp<media::IAudioTrackCallback>& callback,
jiabinc658e452022-10-21 20:52:21 +00001036 bool isSpatialized,
jiabin94ed47c2023-07-27 23:34:20 +00001037 bool isBitPerfect,
Pechetty Sravani (xWF)2e077f02024-08-27 01:46:20 +00001038 audio_output_flags_t* afTrackFlags) final
Andy Hung972bec12023-08-31 16:13:39 -07001039 REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -08001040
Andy Hung87c693c2023-07-06 20:56:16 -07001041 bool isTrackActive(const sp<IAfTrack>& track) const final {
1042 return mActiveTracks.indexOf(track) >= 0;
1043 }
1044
Andy Hungab65b182023-09-06 19:41:47 -07001045 AudioStreamOut* getOutput_l() const final REQUIRES(mutex()) { return mOutput; }
Andy Hung8d672e02023-09-15 18:19:28 -07001046 AudioStreamOut* getOutput() const final EXCLUDES_ThreadBase_Mutex;
1047 AudioStreamOut* clearOutput() final EXCLUDES_ThreadBase_Mutex;
1048
1049 // NO_THREAD_SAFETY_ANALYSIS -- probably needs a lock.
Andy Hung440901d2023-06-29 21:19:25 -07001050 sp<StreamHalInterface> stream() const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001051
Andy Hung8d672e02023-09-15 18:19:28 -07001052 // suspend(), restore(), and isSuspended() are implemented atomically.
1053 void suspend() final { ++mSuspended; }
1054 void restore() final {
1055 // if restore() is done without suspend(), get back into
1056 // range so that the next suspend() will operate correctly
1057 while (true) {
1058 int32_t suspended = mSuspended;
1059 if (suspended <= 0) {
1060 ALOGW("%s: invalid mSuspended %d <= 0", __func__, suspended);
1061 return;
1062 }
1063 const int32_t desired = suspended - 1;
1064 if (mSuspended.compare_exchange_weak(suspended, desired)) return;
1065 }
1066 }
1067 bool isSuspended() const final { return mSuspended > 0; }
Eric Laurent81784c32012-11-19 14:55:58 -08001068
Andy Hungab65b182023-09-06 19:41:47 -07001069 String8 getParameters(const String8& keys) EXCLUDES_ThreadBase_Mutex;
1070
1071 // Hold either the AudioFlinger::mutex or the ThreadBase::mutex
1072 void ioConfigChanged_l(audio_io_config_event_t event, pid_t pid = 0,
Andy Hung440901d2023-06-29 21:19:25 -07001073 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
Andy Hungab65b182023-09-06 19:41:47 -07001074 status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const final
1075 EXCLUDES_ThreadBase_Mutex;
Andy Hung010a1a12014-03-13 13:57:33 -07001076 // Consider also removing and passing an explicit mMainBuffer initialization
Andy Hung8d31fd22023-06-26 19:20:57 -07001077 // parameter to AF::IAfTrack::Track().
Andy Hung440901d2023-06-29 21:19:25 -07001078 float* sinkBuffer() const final {
Andy Hung319587b2023-05-23 14:01:03 -07001079 return reinterpret_cast<float *>(mSinkBuffer); };
Eric Laurent81784c32012-11-19 14:55:58 -08001080
Andy Hungab65b182023-09-06 19:41:47 -07001081 void detachAuxEffect_l(int effectId) final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001082
Andy Hungab65b182023-09-06 19:41:47 -07001083 status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId) final
1084 EXCLUDES_ThreadBase_Mutex;
1085 status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId) final REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -07001086
Andy Hungab65b182023-09-06 19:41:47 -07001087 status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex());
1088 size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex());
1089 uint32_t hasAudioSession_l(audio_session_t sessionId) const final REQUIRES(mutex()) {
Andy Hungc3d62f92019-03-14 13:38:51 -07001090 return ThreadBase::hasAudioSession_l(sessionId, mTracks);
1091 }
Andy Hungab65b182023-09-06 19:41:47 -07001092 product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const final
1093 REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001094
1095
Andy Hungab65b182023-09-06 19:41:47 -07001096 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final
1097 EXCLUDES_ThreadBase_Mutex;
1098 // could be static.
Andy Hung440901d2023-06-29 21:19:25 -07001099 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final;
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001100
Andy Hungab65b182023-09-06 19:41:47 -07001101 // Does this require the AudioFlinger mutex as well?
1102 bool invalidateTracks_l(audio_stream_type_t streamType) final
1103 REQUIRES(mutex());
1104 bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) final
1105 REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -07001106 void invalidateTracks(audio_stream_type_t streamType) override;
jiabinc44b3462022-12-08 12:52:31 -08001107 // Invalidate tracks by a set of port ids. The port id will be removed from
1108 // the given set if the corresponding track is found and invalidated.
Andy Hungab65b182023-09-06 19:41:47 -07001109 void invalidateTracks(std::set<audio_port_handle_t>& portIds) override
1110 EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -08001111
Andy Hung87c693c2023-07-06 20:56:16 -07001112 size_t frameCount() const final { return mNormalFrameCount; }
Glenn Kasten9b58f632013-07-16 11:37:48 -07001113
Andy Hung440901d2023-06-29 21:19:25 -07001114 audio_channel_mask_t mixerChannelMask() const final {
Eric Laurentf1f22e72021-07-13 14:04:14 +02001115 return mMixerChannelMask;
1116 }
1117
Andy Hung8d672e02023-09-15 18:19:28 -07001118 status_t getTimestamp_l(AudioTimestamp& timestamp) final
1119 REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurent83b88082014-06-20 18:31:16 -07001120
Andy Hungab65b182023-09-06 19:41:47 -07001121 void addPatchTrack(const sp<IAfPatchTrack>& track) final EXCLUDES_ThreadBase_Mutex;
1122 void deletePatchTrack(const sp<IAfPatchTrack>& track) final EXCLUDES_ThreadBase_Mutex;
Eric Laurent83b88082014-06-20 18:31:16 -07001123
Andy Hungab65b182023-09-06 19:41:47 -07001124 // NO_THREAD_SAFETY_ANALYSIS - fix this to use atomics.
Andy Hung440901d2023-06-29 21:19:25 -07001125 void toAudioPortConfig(struct audio_port_config* config) final;
Eric Laurentaccc1472013-09-20 09:36:34 -07001126
Andy Hung10cbff12017-02-21 17:30:14 -08001127 // Return the asynchronous signal wait time.
Andy Hungab65b182023-09-06 19:41:47 -07001128 int64_t computeWaitTimeNs_l() const override REQUIRES(mutex()) { return INT64_MAX; }
Andy Hung1bc088a2018-02-09 15:57:31 -08001129 // returns true if the track is allowed to be added to the thread.
Andy Hung440901d2023-06-29 21:19:25 -07001130 bool isTrackAllowed_l(
Andy Hung1bc088a2018-02-09 15:57:31 -08001131 audio_channel_mask_t channelMask __unused,
1132 audio_format_t format __unused,
1133 audio_session_t sessionId __unused,
Andy Hungab65b182023-09-06 19:41:47 -07001134 uid_t uid) const override REQUIRES(mutex()) {
Andy Hung1bc088a2018-02-09 15:57:31 -08001135 return trackCountForUid_l(uid) < PlaybackThread::kMaxTracksPerUid
1136 && mTracks.size() < PlaybackThread::kMaxTracks;
1137 }
1138
Andy Hungab65b182023-09-06 19:41:47 -07001139 bool isTimestampCorrectionEnabled_l() const final REQUIRES(mutex()) {
1140 return audio_is_output_devices(mTimestampCorrectedDevice)
1141 && outDeviceTypes_l().count(mTimestampCorrectedDevice) != 0;
Andy Hungc8fddf32018-08-08 18:32:37 -07001142 }
jiabinc52b1ff2019-10-31 17:20:42 -07001143
Andy Hungab65b182023-09-06 19:41:47 -07001144 // NO_THREAD_SAFETY_ANALYSIS - fix this to be atomic.
Andy Hung440901d2023-06-29 21:19:25 -07001145 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08001146 return !(mOutput == nullptr || mOutput->stream == nullptr);
1147 }
1148
Andy Hung440901d2023-06-29 21:19:25 -07001149 audio_channel_mask_t hapticChannelMask() const final {
jiabineb3bda02020-06-30 14:07:03 -07001150 return mHapticChannelMask;
1151 }
Andy Hung87c693c2023-07-06 20:56:16 -07001152
1153 uint32_t hapticChannelCount() const final {
1154 return mHapticChannelCount;
1155 }
1156
Andy Hung440901d2023-06-29 21:19:25 -07001157 bool supportsHapticPlayback() const final {
jiabineb3bda02020-06-30 14:07:03 -07001158 return (mHapticChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE;
1159 }
1160
Andy Hungab65b182023-09-06 19:41:47 -07001161 void setDownStreamPatch(const struct audio_patch* patch) final EXCLUDES_ThreadBase_Mutex {
Andy Hung8d672e02023-09-15 18:19:28 -07001162 audio_utils::lock_guard _l(mutex());
Eric Laurent74c38dc2020-12-23 18:19:44 +01001163 mDownStreamPatch = *patch;
1164 }
1165
Andy Hungab65b182023-09-06 19:41:47 -07001166 IAfTrack* getTrackById_l(audio_port_handle_t trackId) final REQUIRES(mutex());
jiabinf042b9b2021-05-07 23:46:28 +00001167
Andy Hung440901d2023-06-29 21:19:25 -07001168 bool hasMixer() const final {
Eric Laurent1c5e2e32021-08-18 18:50:28 +02001169 return mType == MIXER || mType == DUPLICATING || mType == SPATIALIZER;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001170 }
Eric Laurent68a40a82022-05-03 18:15:04 +02001171
Andy Hung440901d2023-06-29 21:19:25 -07001172 status_t setRequestedLatencyMode(
1173 audio_latency_mode_t /* mode */) override { return INVALID_OPERATION; }
Eric Laurent68a40a82022-05-03 18:15:04 +02001174
Andy Hung440901d2023-06-29 21:19:25 -07001175 status_t getSupportedLatencyModes(
1176 std::vector<audio_latency_mode_t>* /* modes */) override {
Eric Laurent68a40a82022-05-03 18:15:04 +02001177 return INVALID_OPERATION;
1178 }
1179
Andy Hung440901d2023-06-29 21:19:25 -07001180 status_t setBluetoothVariableLatencyEnabled(bool /* enabled */) override{
Eric Laurentb0463942022-12-20 16:31:10 +01001181 return INVALID_OPERATION;
1182 }
Eric Laurent52057642022-12-16 11:45:07 +01001183
Andy Hung972bec12023-08-31 16:13:39 -07001184 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override
1185 REQUIRES(audio_utils::AudioFlinger_Mutex);
1186 void stopMelComputation_l() override
1187 REQUIRES(audio_utils::AudioFlinger_Mutex);
Vlad Popab042ee62022-10-20 18:05:00 +02001188
Andy Hung8d672e02023-09-15 18:19:28 -07001189 void setStandby() final EXCLUDES_ThreadBase_Mutex {
1190 audio_utils::lock_guard _l(mutex());
Eric Laurent19952e12023-04-20 10:08:29 +02001191 setStandby_l();
1192 }
1193
Andy Hungab65b182023-09-06 19:41:47 -07001194 void setStandby_l() final REQUIRES(mutex()) {
Eric Laurent19952e12023-04-20 10:08:29 +02001195 mStandby = true;
1196 mHalStarted = false;
1197 mKernelPositionOnStandby =
1198 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1199 }
1200
Andy Hungab65b182023-09-06 19:41:47 -07001201 bool waitForHalStart() final EXCLUDES_ThreadBase_Mutex {
Andy Hungc5007f82023-08-29 14:26:09 -07001202 audio_utils::unique_lock _l(mutex());
Eric Laurent19952e12023-04-20 10:08:29 +02001203 static const nsecs_t kWaitHalTimeoutNs = seconds(2);
1204 nsecs_t endWaitTimetNs = systemTime() + kWaitHalTimeoutNs;
1205 while (!mHalStarted) {
1206 nsecs_t timeNs = systemTime();
1207 if (timeNs >= endWaitTimetNs) {
1208 break;
1209 }
1210 nsecs_t waitTimeLeftNs = endWaitTimetNs - timeNs;
Andy Hungc5007f82023-08-29 14:26:09 -07001211 mWaitHalStartCV.wait_for(_l, std::chrono::nanoseconds(waitTimeLeftNs));
Eric Laurent19952e12023-04-20 10:08:29 +02001212 }
1213 return mHalStarted;
1214 }
jiabin220eea12024-05-17 17:55:20 +00001215
1216 void setTracksInternalMute(std::map<audio_port_handle_t, bool>* /* tracksInternalMute */)
1217 override EXCLUDES_ThreadBase_Mutex {
1218 // Do nothing. It is only used for bit perfect thread
1219 }
Eric Laurent81784c32012-11-19 14:55:58 -08001220protected:
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001221 // updated by readOutputParameters_l()
Glenn Kasten9b58f632013-07-16 11:37:48 -07001222 size_t mNormalFrameCount; // normal mixer and effects
1223
Andy Hung8d672e02023-09-15 18:19:28 -07001224 // throttle the thread processing
1225 bool mThreadThrottle GUARDED_BY(ThreadBase_ThreadLoop);
1226
1227 // throttle time for MIXER threads - atomic as read by dump()
1228 std::atomic<uint32_t> mThreadThrottleTimeMs;
1229
1230 // notify once per throttling
1231 uint32_t mThreadThrottleEndMs GUARDED_BY(ThreadBase_ThreadLoop);
1232
1233 // half the buffer size in milliseconds
1234 uint32_t mHalfBufferMs GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung08fb1742015-05-31 23:22:10 -07001235
Andy Hung010a1a12014-03-13 13:57:33 -07001236 void* mSinkBuffer; // frame size aligned sink buffer
Eric Laurent81784c32012-11-19 14:55:58 -08001237
Andy Hung98ef9782014-03-04 14:46:50 -08001238 // TODO:
1239 // Rearrange the buffer info into a struct/class with
1240 // clear, copy, construction, destruction methods.
1241 //
1242 // mSinkBuffer also has associated with it:
1243 //
1244 // mSinkBufferSize: Sink Buffer Size
1245 // mFormat: Sink Buffer Format
1246
Andy Hung69aed5f2014-02-25 17:24:40 -08001247 // Mixer Buffer (mMixerBuffer*)
1248 //
1249 // In the case of floating point or multichannel data, which is not in the
1250 // sink format, it is required to accumulate in a higher precision or greater channel count
1251 // buffer before downmixing or data conversion to the sink buffer.
1252
1253 // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
Andy Hung8d672e02023-09-15 18:19:28 -07001254 bool mMixerBufferEnabled GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung69aed5f2014-02-25 17:24:40 -08001255
1256 // Storage, 32 byte aligned (may make this alignment a requirement later).
1257 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
Andy Hung8d672e02023-09-15 18:19:28 -07001258 void* mMixerBuffer GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung69aed5f2014-02-25 17:24:40 -08001259
1260 // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
Andy Hung8d672e02023-09-15 18:19:28 -07001261 size_t mMixerBufferSize GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung69aed5f2014-02-25 17:24:40 -08001262
1263 // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
Andy Hung8d672e02023-09-15 18:19:28 -07001264 audio_format_t mMixerBufferFormat GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung69aed5f2014-02-25 17:24:40 -08001265
1266 // An internal flag set to true by MixerThread::prepareTracks_l()
1267 // when mMixerBuffer contains valid data after mixing.
Andy Hung8d672e02023-09-15 18:19:28 -07001268 bool mMixerBufferValid GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung69aed5f2014-02-25 17:24:40 -08001269
Andy Hung98ef9782014-03-04 14:46:50 -08001270 // Effects Buffer (mEffectsBuffer*)
1271 //
1272 // In the case of effects data, which is not in the sink format,
1273 // it is required to accumulate in a different buffer before data conversion
1274 // to the sink buffer.
1275
1276 // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
Andy Hung8d672e02023-09-15 18:19:28 -07001277 bool mEffectBufferEnabled;
1278 // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l()
Andy Hung98ef9782014-03-04 14:46:50 -08001279
1280 // Storage, 32 byte aligned (may make this alignment a requirement later).
1281 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
Andy Hung8d672e02023-09-15 18:19:28 -07001282 void* mEffectBuffer;
1283 // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l()
Andy Hung98ef9782014-03-04 14:46:50 -08001284
1285 // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
Andy Hung8d672e02023-09-15 18:19:28 -07001286 size_t mEffectBufferSize;
1287 // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l()
Andy Hung98ef9782014-03-04 14:46:50 -08001288
1289 // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
Andy Hung8d672e02023-09-15 18:19:28 -07001290 // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l()
1291 audio_format_t mEffectBufferFormat;
Andy Hung98ef9782014-03-04 14:46:50 -08001292
1293 // An internal flag set to true by MixerThread::prepareTracks_l()
1294 // when mEffectsBuffer contains valid data after mixing.
1295 //
1296 // When this is set, all mixer data is routed into the effects buffer
1297 // for any processing (including output processing).
Andy Hung8d672e02023-09-15 18:19:28 -07001298 bool mEffectBufferValid GUARDED_BY(ThreadBase_ThreadLoop);
Andy Hung98ef9782014-03-04 14:46:50 -08001299
jiabinc658e452022-10-21 20:52:21 +00001300 // Set to "true" to enable when data has already copied to sink
Andy Hung8d672e02023-09-15 18:19:28 -07001301 bool mHasDataCopiedToSinkBuffer GUARDED_BY(ThreadBase_ThreadLoop) = false;
jiabinc658e452022-10-21 20:52:21 +00001302
Eric Laurentb62d0362021-10-26 17:40:18 +02001303 // Frame size aligned buffer used as input and output to all post processing effects
1304 // except the Spatializer in a SPATIALIZER thread. Non spatialized tracks are mixed into
1305 // this buffer so that post processing effects can be applied.
Andy Hung8d672e02023-09-15 18:19:28 -07001306 void* mPostSpatializerBuffer GUARDED_BY(mutex()) = nullptr;
Eric Laurentb62d0362021-10-26 17:40:18 +02001307
1308 // Size of mPostSpatializerBuffer in bytes
Andy Hung8d672e02023-09-15 18:19:28 -07001309 size_t mPostSpatializerBufferSize GUARDED_BY(mutex());
Eric Laurent39095982021-08-24 18:29:27 +02001310
Eric Laurent81784c32012-11-19 14:55:58 -08001311 // suspend count, > 0 means suspended. While suspended, the thread continues to pull from
1312 // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle
1313 // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
1314 // workaround that restriction.
1315 // 'volatile' means accessed via atomic operations and no lock.
Andy Hung8d672e02023-09-15 18:19:28 -07001316 std::atomic<int32_t> mSuspended;
Eric Laurent81784c32012-11-19 14:55:58 -08001317
Andy Hung818e7a32016-02-16 18:08:07 -08001318 int64_t mBytesWritten;
Andy Hung8d672e02023-09-15 18:19:28 -07001319 std::atomic<int64_t> mFramesWritten; // not reset on standby
Dean Wheatley12473e92021-03-18 23:00:55 +11001320 int64_t mLastFramesWritten = -1; // track changes in timestamp
1321 // server frames written.
Andy Hung238fa3d2016-07-28 10:53:22 -07001322 int64_t mSuspendedFrames; // not reset on standby
jiabin245cdd92018-12-07 17:55:15 -08001323
1324 // mHapticChannelMask and mHapticChannelCount will only be valid when the thread support
1325 // haptic playback.
1326 audio_channel_mask_t mHapticChannelMask = AUDIO_CHANNEL_NONE;
1327 uint32_t mHapticChannelCount = 0;
Eric Laurentf1f22e72021-07-13 14:04:14 +02001328
1329 audio_channel_mask_t mMixerChannelMask = AUDIO_CHANNEL_NONE;
1330
Eric Laurent81784c32012-11-19 14:55:58 -08001331 // mMasterMute is in both PlaybackThread and in AudioFlinger. When a
1332 // PlaybackThread needs to find out if master-muted, it checks it's local
1333 // copy rather than the one in AudioFlinger. This optimization saves a lock.
Andy Hung8d672e02023-09-15 18:19:28 -07001334 bool mMasterMute GUARDED_BY(mutex());
1335 void setMasterMute_l(bool muted) REQUIRES(mutex()) { mMasterMute = muted; }
Dean Wheatley12473e92021-03-18 23:00:55 +11001336
1337 auto discontinuityForStandbyOrFlush() const { // call on threadLoop or with lock.
1338 return ((mType == DIRECT && !audio_is_linear_pcm(mFormat))
1339 || mType == OFFLOAD)
1340 ? mTimestampVerifier.DISCONTINUITY_MODE_ZERO
1341 : mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS;
1342 }
1343
Andy Hung8d31fd22023-06-26 19:20:57 -07001344 ActiveTracks<IAfTrack> mActiveTracks;
Eric Laurent81784c32012-11-19 14:55:58 -08001345
Eric Laurent81784c32012-11-19 14:55:58 -08001346 // Time to sleep between cycles when:
1347 virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED
1348 virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE
1349 virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us
1350 // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
1351 // No sleep in standby mode; waits on a condition
1352
1353 // Code snippets that are temporarily lifted up out of threadLoop() until the merge
Andy Hungab65b182023-09-06 19:41:47 -07001354
1355 // consider unification with MMapThread
1356 virtual void checkSilentMode_l() final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001357
1358 // Non-trivial for DUPLICATING only
Andy Hung8d672e02023-09-15 18:19:28 -07001359 virtual void saveOutputTracks() REQUIRES(ThreadBase_ThreadLoop) {}
1360 virtual void clearOutputTracks() REQUIRES(ThreadBase_ThreadLoop) {}
Eric Laurent81784c32012-11-19 14:55:58 -08001361
1362 // Cache various calculated values, at threadLoop() entry and after a parameter change
Andy Hung8d672e02023-09-15 18:19:28 -07001363 virtual void cacheParameters_l() REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurentb3f315a2021-07-13 15:09:05 +02001364 void setCheckOutputStageEffects() override {
1365 mCheckOutputStageEffects.store(true);
1366 }
Eric Laurent81784c32012-11-19 14:55:58 -08001367
Andy Hungab65b182023-09-06 19:41:47 -07001368 virtual uint32_t correctLatency_l(uint32_t latency) const REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001369
Eric Laurent1c333e22014-05-20 10:48:17 -07001370 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
Andy Hungab65b182023-09-06 19:41:47 -07001371 audio_patch_handle_t *handle) REQUIRES(mutex());
1372 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle)
1373 REQUIRES(mutex());
Eric Laurent1c333e22014-05-20 10:48:17 -07001374
Andy Hungab65b182023-09-06 19:41:47 -07001375 // NO_THREAD_SAFETY_ANALYSIS - fix this to use atomics
Andy Hung87c693c2023-07-06 20:56:16 -07001376 bool usesHwAvSync() const final { return mType == DIRECT && mOutput != nullptr
Phil Burk6fc2a7c2015-04-30 16:08:10 -07001377 && mHwSupportsPause
1378 && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
Eric Laurent0f7b5f22014-12-19 10:43:21 -08001379
Andy Hung1bc088a2018-02-09 15:57:31 -08001380 uint32_t trackCountForUid_l(uid_t uid) const;
Eric Laurentad7dd962016-09-22 12:38:37 -07001381
jiabineb3bda02020-06-30 14:07:03 -07001382 void invalidateTracksForAudioSession_l(
Andy Hungab65b182023-09-06 19:41:47 -07001383 audio_session_t sessionId) const override REQUIRES(mutex()) {
jiabineb3bda02020-06-30 14:07:03 -07001384 ThreadBase::invalidateTracksForAudioSession_l(sessionId, mTracks);
1385 }
1386
Mikhail Naganovbf493082017-04-17 17:37:12 -07001387 DISALLOW_COPY_AND_ASSIGN(PlaybackThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001388
Andy Hungab65b182023-09-06 19:41:47 -07001389 status_t addTrack_l(const sp<IAfTrack>& track) final REQUIRES(mutex());
1390 bool destroyTrack_l(const sp<IAfTrack>& track) final REQUIRES(mutex());
Andy Hung87c693c2023-07-06 20:56:16 -07001391
Andy Hungab65b182023-09-06 19:41:47 -07001392 void removeTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex());
Mikhail Naganovbf203ce2024-05-23 16:27:59 -07001393 std::set<audio_port_handle_t> getTrackPortIds_l() REQUIRES(mutex());
1394 std::set<audio_port_handle_t> getTrackPortIds();
Eric Laurent81784c32012-11-19 14:55:58 -08001395
Andy Hungab65b182023-09-06 19:41:47 -07001396 void readOutputParameters_l() REQUIRES(mutex());
1397 MetadataUpdate updateMetadata_l() final REQUIRES(mutex());
1398 virtual void sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata& metadata)
1399 REQUIRES(mutex()) ;
Eric Laurent81784c32012-11-19 14:55:58 -08001400
Andy Hung8d672e02023-09-15 18:19:28 -07001401 void collectTimestamps_l() REQUIRES(mutex(), ThreadBase_ThreadLoop);
Dean Wheatley12473e92021-03-18 23:00:55 +11001402
Andy Hungc0691382018-09-12 18:01:57 -07001403 // The Tracks class manages tracks added and removed from the Thread.
Andy Hung1bc088a2018-02-09 15:57:31 -08001404 template <typename T>
1405 class Tracks {
1406 public:
Andy Hung920f6572022-10-06 12:09:49 -07001407 explicit Tracks(bool saveDeletedTrackIds) :
Andy Hungc0691382018-09-12 18:01:57 -07001408 mSaveDeletedTrackIds(saveDeletedTrackIds) { }
Andy Hung1bc088a2018-02-09 15:57:31 -08001409
1410 // SortedVector methods
Andy Hungc0691382018-09-12 18:01:57 -07001411 ssize_t add(const sp<T> &track) {
1412 const ssize_t index = mTracks.add(track);
1413 LOG_ALWAYS_FATAL_IF(index < 0, "cannot add track");
1414 return index;
1415 }
Andy Hung1bc088a2018-02-09 15:57:31 -08001416 ssize_t remove(const sp<T> &track);
1417 size_t size() const {
1418 return mTracks.size();
1419 }
1420 bool isEmpty() const {
1421 return mTracks.isEmpty();
1422 }
1423 ssize_t indexOf(const sp<T> &item) {
1424 return mTracks.indexOf(item);
1425 }
1426 sp<T> operator[](size_t index) const {
1427 return mTracks[index];
1428 }
1429 typename SortedVector<sp<T>>::iterator begin() {
1430 return mTracks.begin();
1431 }
1432 typename SortedVector<sp<T>>::iterator end() {
1433 return mTracks.end();
1434 }
1435
Andy Hung920f6572022-10-06 12:09:49 -07001436 size_t processDeletedTrackIds(const std::function<void(int)>& f) {
Andy Hungc0691382018-09-12 18:01:57 -07001437 for (const int trackId : mDeletedTrackIds) {
1438 f(trackId);
Andy Hung1bc088a2018-02-09 15:57:31 -08001439 }
Andy Hungc0691382018-09-12 18:01:57 -07001440 return mDeletedTrackIds.size();
Andy Hung1bc088a2018-02-09 15:57:31 -08001441 }
1442
Andy Hungc0691382018-09-12 18:01:57 -07001443 void clearDeletedTrackIds() { mDeletedTrackIds.clear(); }
Andy Hung1bc088a2018-02-09 15:57:31 -08001444
1445 private:
Andy Hungc0691382018-09-12 18:01:57 -07001446 // Tracks pending deletion for MIXER type threads
1447 const bool mSaveDeletedTrackIds; // true to enable tracking
1448 std::set<int> mDeletedTrackIds;
Andy Hung1bc088a2018-02-09 15:57:31 -08001449
1450 SortedVector<sp<T>> mTracks; // wrapped SortedVector.
1451 };
1452
Andy Hung8d31fd22023-06-26 19:20:57 -07001453 Tracks<IAfTrack> mTracks;
Andy Hung1bc088a2018-02-09 15:57:31 -08001454
Eric Laurent223fd5c2014-11-11 13:43:36 -08001455 stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Andy Hungee58e4a2023-07-07 13:47:37 -07001456
Eric Laurent81784c32012-11-19 14:55:58 -08001457 AudioStreamOut *mOutput;
1458
1459 float mMasterVolume;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001460 std::atomic<float> mMasterBalance{};
1461 audio_utils::Balance mBalance;
Eric Laurent81784c32012-11-19 14:55:58 -08001462 int mNumWrites;
1463 int mNumDelayedWrites;
1464 bool mInWrite;
1465
1466 // FIXME rename these former local variables of threadLoop to standard "m" names
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001467 nsecs_t mStandbyTimeNs;
Andy Hung25c2dac2014-02-27 14:56:00 -08001468 size_t mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -08001469
1470 // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001471 uint32_t mActiveSleepTimeUs;
1472 uint32_t mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08001473
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001474 uint32_t mSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08001475
1476 // mixer status returned by prepareTracks_l()
Andy Hung8d672e02023-09-15 18:19:28 -07001477 mixer_state mMixerStatus GUARDED_BY(ThreadBase_ThreadLoop); // current cycle
Eric Laurent81784c32012-11-19 14:55:58 -08001478 // previous cycle when in prepareTracks_l()
Andy Hung8d672e02023-09-15 18:19:28 -07001479 mixer_state mMixerStatusIgnoringFastTracks GUARDED_BY(ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001480 // FIXME or a separate ready state per track
1481
1482 // FIXME move these declarations into the specific sub-class that needs them
1483 // MIXER only
Andy Hung8d672e02023-09-15 18:19:28 -07001484 uint32_t sleepTimeShift GUARDED_BY(ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001485
1486 // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
Andy Hung8d672e02023-09-15 18:19:28 -07001487 nsecs_t mStandbyDelayNs; // GUARDED_BY(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001488
1489 // MIXER only
1490 nsecs_t maxPeriod;
1491
1492 // DUPLICATING only
1493 uint32_t writeFrames;
1494
Andy Hung8d672e02023-09-15 18:19:28 -07001495 size_t mBytesRemaining GUARDED_BY(ThreadBase_ThreadLoop);
1496 size_t mCurrentWriteLength GUARDED_BY(ThreadBase_ThreadLoop);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001497 bool mUseAsyncWrite;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001498 // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
1499 // incremented each time a write(), a flush() or a standby() occurs.
1500 // Bit 0 is set when a write blocks and indicates a callback is expected.
1501 // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
1502 // callbacks are ignored.
1503 uint32_t mWriteAckSequence;
1504 // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
1505 // incremented each time a drain is requested or a flush() or standby() occurs.
1506 // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
1507 // expected.
1508 // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
1509 // callbacks are ignored.
1510 uint32_t mDrainSequence;
Andy Hungee58e4a2023-07-07 13:47:37 -07001511
Eric Laurentbfb1b832013-01-07 09:53:42 -08001512 sp<AsyncCallbackThread> mCallbackThread;
1513
Andy Hungc5007f82023-08-29 14:26:09 -07001514 audio_utils::mutex& audioTrackCbMutex() const { return mAudioTrackCbMutex; }
Andy Hunga6f1cbb2023-10-12 18:18:13 -07001515 mutable audio_utils::mutex mAudioTrackCbMutex{
1516 audio_utils::MutexOrder::kPlaybackThread_AudioTrackCbMutex};
jiabinf6eb4c32020-02-25 14:06:25 -08001517 // Record of IAudioTrackCallback
Andy Hung8d31fd22023-06-26 19:20:57 -07001518 std::map<sp<IAfTrack>, sp<media::IAudioTrackCallback>> mAudioTrackCallbacks;
jiabinf6eb4c32020-02-25 14:06:25 -08001519
Eric Laurent81784c32012-11-19 14:55:58 -08001520 // The HAL output sink is treated as non-blocking, but current implementation is blocking
1521 sp<NBAIO_Sink> mOutputSink;
1522 // If a fast mixer is present, the blocking pipe sink, otherwise clear
1523 sp<NBAIO_Sink> mPipeSink;
1524 // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
1525 sp<NBAIO_Sink> mNormalSink;
Andy Hungee58e4a2023-07-07 13:47:37 -07001526
Eric Laurent81784c32012-11-19 14:55:58 -08001527 uint32_t mScreenState; // cached copy of gScreenState
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07001528 // TODO: add comment and adjust size as needed
Glenn Kasteneef598c2017-04-03 14:41:13 -07001529 static const size_t kFastMixerLogSize = 8 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -08001530 sp<NBLog::Writer> mFastMixerNBLogWriter;
Andy Hung2148bf02016-11-28 19:01:02 -08001531
Dean Wheatley30d28422018-11-06 10:27:40 +11001532 // Downstream patch latency, available if mDownstreamLatencyStatMs.getN() > 0.
1533 audio_utils::Statistics<double> mDownstreamLatencyStatMs{0.999};
Andy Hung2148bf02016-11-28 19:01:02 -08001534
Eric Laurent19952e12023-04-20 10:08:29 +02001535 // output stream start detection based on render position returned by the kernel
1536 // condition signalled when the output stream has started
Andy Hungc5007f82023-08-29 14:26:09 -07001537 audio_utils::condition_variable mWaitHalStartCV;
Eric Laurent19952e12023-04-20 10:08:29 +02001538 // true when the output stream render position has moved, reset to false in standby
1539 bool mHalStarted = false;
1540 // last kernel render position saved when entering standby
1541 int64_t mKernelPositionOnStandby = 0;
1542
Eric Laurent81784c32012-11-19 14:55:58 -08001543public:
Andy Hung440901d2023-06-29 21:19:25 -07001544 FastTrackUnderruns getFastTrackUnderruns(size_t /* fastIndex */) const override
1545 { return {}; }
1546 const std::atomic<int64_t>& framesWritten() const final { return mFramesWritten; }
Eric Laurent81784c32012-11-19 14:55:58 -08001547
1548protected:
1549 // accessed by both binder threads and within threadLoop(), lock on mutex needed
Andy Hungab65b182023-09-06 19:41:47 -07001550 uint32_t& fastTrackAvailMask_l() final REQUIRES(mutex()) { return mFastTrackAvailMask; }
Andy Hung87c693c2023-07-06 20:56:16 -07001551 uint32_t mFastTrackAvailMask; // bit i set if fast track [i] is available
Eric Laurentd1f69b02014-12-15 14:33:13 -08001552 bool mHwSupportsPause;
1553 bool mHwPaused;
1554 bool mFlushPending;
Eric Laurent7c29ec92017-09-20 17:54:22 -07001555 // volumes last sent to audio HAL with stream->setVolume()
1556 float mLeftVolFloat;
1557 float mRightVolFloat;
Eric Laurent74c38dc2020-12-23 18:19:44 +01001558
1559 // audio patch used by the downstream software patch.
1560 // Only used if ThreadBase::mIsMsdDevice is true.
1561 struct audio_patch mDownStreamPatch;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001562
1563 std::atomic_bool mCheckOutputStageEffects{};
ziyangch8f194f12021-12-01 13:48:04 -08001564
ziyangch8f194f12021-12-01 13:48:04 -08001565
Brian Lindahl65e90012022-07-27 18:01:07 +02001566 // Provides periodic checking for timestamp advancement for underrun detection.
1567 class IsTimestampAdvancing {
1568 public:
1569 // The timestamp will not be checked any faster than the specified time.
Andy Hung920f6572022-10-06 12:09:49 -07001570 explicit IsTimestampAdvancing(nsecs_t minimumTimeBetweenChecksNs)
Brian Lindahl65e90012022-07-27 18:01:07 +02001571 : mMinimumTimeBetweenChecksNs(minimumTimeBetweenChecksNs)
1572 {
1573 clear();
1574 }
1575 // Check if the presentation position has advanced in the last periodic time.
1576 bool check(AudioStreamOut * output);
1577 // Clear the internal state when the playback state changes for the output
1578 // stream.
1579 void clear();
1580 private:
1581 // The minimum time between timestamp checks.
1582 const nsecs_t mMinimumTimeBetweenChecksNs;
1583 // Add differential check on the timestamps to see if there is a change in the
1584 // timestamp frame position between the last call to check.
1585 uint64_t mPreviousPosition;
1586 // The time at which the last check occurred, to ensure we don't check too
1587 // frequently, giving the Audio HAL enough time to update its timestamps.
1588 nsecs_t mPreviousNs;
1589 // The valued is latched so we don't check timestamps too frequently.
1590 bool mLatchedValue;
1591 };
1592 IsTimestampAdvancing mIsTimestampAdvancing;
ziyangch8f194f12021-12-01 13:48:04 -08001593
Brian Lindahl65e90012022-07-27 18:01:07 +02001594 virtual void flushHw_l() {
1595 mIsTimestampAdvancing.clear();
1596 }
Eric Laurent81784c32012-11-19 14:55:58 -08001597};
1598
Eric Laurentb0463942022-12-20 16:31:10 +01001599class MixerThread : public PlaybackThread,
1600 public StreamOutHalInterfaceLatencyModeCallback {
Eric Laurent81784c32012-11-19 14:55:58 -08001601public:
Andy Hung583043b2023-07-17 17:05:00 -07001602 MixerThread(const sp<IAfThreadCallback>& afThreadCallback,
Eric Laurent81784c32012-11-19 14:55:58 -08001603 AudioStreamOut* output,
1604 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001605 bool systemReady,
Eric Laurentf1f22e72021-07-13 14:04:14 +02001606 type_t type = MIXER,
1607 audio_config_base_t *mixerConfig = nullptr);
Andy Hung440901d2023-06-29 21:19:25 -07001608 ~MixerThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001609
Eric Laurentb0463942022-12-20 16:31:10 +01001610 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -07001611 void onFirstRef() override;
Eric Laurentb0463942022-12-20 16:31:10 +01001612
1613 // StreamOutHalInterfaceLatencyModeCallback
1614 void onRecommendedLatencyModeChanged(
Andy Hung440901d2023-06-29 21:19:25 -07001615 std::vector<audio_latency_mode_t> modes) final;
Eric Laurentb0463942022-12-20 16:31:10 +01001616
Eric Laurent81784c32012-11-19 14:55:58 -08001617 // Thread virtuals
1618
Andy Hungab65b182023-09-06 19:41:47 -07001619 bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) final
1620 REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001621
Andy Hung440901d2023-06-29 21:19:25 -07001622 bool isTrackAllowed_l(
Andy Hung1bc088a2018-02-09 15:57:31 -08001623 audio_channel_mask_t channelMask, audio_format_t format,
Andy Hungab65b182023-09-06 19:41:47 -07001624 audio_session_t sessionId, uid_t uid) const final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001625protected:
Andy Hung8d672e02023-09-15 18:19:28 -07001626 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) override
1627 REQUIRES(mutex(), ThreadBase_ThreadLoop);
Andy Hung440901d2023-06-29 21:19:25 -07001628 uint32_t idleSleepTimeUs() const final;
1629 uint32_t suspendSleepTimeUs() const final;
Andy Hung8d672e02023-09-15 18:19:28 -07001630 void cacheParameters_l() override REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001631
Andy Hungab65b182023-09-06 19:41:47 -07001632 void acquireWakeLock_l() final REQUIRES(mutex()) {
Andy Hungdae27702016-10-31 14:01:16 -07001633 PlaybackThread::acquireWakeLock_l();
Andy Hung818e7a32016-02-16 18:08:07 -08001634 if (hasFastMixer()) {
1635 mFastMixer->setBoottimeOffset(
1636 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1637 }
1638 }
1639
Andy Hungab65b182023-09-06 19:41:47 -07001640 void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex());
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001641
Eric Laurent81784c32012-11-19 14:55:58 -08001642 // threadLoop snippets
Andy Hung8d672e02023-09-15 18:19:28 -07001643 ssize_t threadLoop_write() override REQUIRES(ThreadBase_ThreadLoop);
1644 void threadLoop_standby() override REQUIRES(ThreadBase_ThreadLoop);
1645 void threadLoop_mix() override REQUIRES(ThreadBase_ThreadLoop);
1646 void threadLoop_sleepTime() override REQUIRES(ThreadBase_ThreadLoop);
Andy Hungab65b182023-09-06 19:41:47 -07001647 uint32_t correctLatency_l(uint32_t latency) const final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001648
Andy Hung440901d2023-06-29 21:19:25 -07001649 status_t createAudioPatch_l(
Andy Hungab65b182023-09-06 19:41:47 -07001650 const struct audio_patch* patch, audio_patch_handle_t* handle)
1651 final REQUIRES(mutex());
1652 status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final REQUIRES(mutex());
Eric Laurent054d9d32015-04-24 08:48:48 -07001653
Eric Laurent81784c32012-11-19 14:55:58 -08001654 AudioMixer* mAudioMixer; // normal mixer
Eric Laurentb0463942022-12-20 16:31:10 +01001655
1656 // Support low latency mode by default as unless explicitly indicated by the audio HAL
1657 // we assume the audio path is compatible with the head tracking latency requirements
1658 std::vector<audio_latency_mode_t> mSupportedLatencyModes = {AUDIO_LATENCY_MODE_LOW};
1659 // default to invalid value to force first update to the audio HAL
1660 audio_latency_mode_t mSetLatencyMode =
1661 (audio_latency_mode_t)AUDIO_LATENCY_MODE_INVALID;
1662
1663 // Bluetooth Variable latency control logic is enabled or disabled for this thread
1664 std::atomic_bool mBluetoothLatencyModesEnabled;
1665
Eric Laurent81784c32012-11-19 14:55:58 -08001666private:
1667 // one-time initialization, no locks required
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001668 sp<FastMixer> mFastMixer; // non-0 if there is also a fast mixer
Eric Laurent81784c32012-11-19 14:55:58 -08001669 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
1670
1671 // contents are not guaranteed to be consistent, no locks required
1672 FastMixerDumpState mFastMixerDumpState;
1673#ifdef STATE_QUEUE_DUMP
1674 StateQueueObserverDump mStateQueueObserverDump;
1675 StateQueueMutatorDump mStateQueueMutatorDump;
1676#endif
1677 AudioWatchdogDump mAudioWatchdogDump;
1678
1679 // accessible only within the threadLoop(), no locks required
1680 // mFastMixer->sq() // for mutating and pushing state
Andy Hung8d672e02023-09-15 18:19:28 -07001681 int32_t mFastMixerFutex GUARDED_BY(ThreadBase_ThreadLoop); // for cold idle
Eric Laurent81784c32012-11-19 14:55:58 -08001682
Andy Hung2ddee192015-12-18 17:34:44 -08001683 std::atomic_bool mMasterMono;
Eric Laurent81784c32012-11-19 14:55:58 -08001684public:
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001685 virtual bool hasFastMixer() const { return mFastMixer != 0; }
Eric Laurent81784c32012-11-19 14:55:58 -08001686 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
Glenn Kastendc2c50b2016-04-21 08:13:14 -07001687 ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
Eric Laurent81784c32012-11-19 14:55:58 -08001688 return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
1689 }
Eric Laurent83b88082014-06-20 18:31:16 -07001690
Andy Hungab65b182023-09-06 19:41:47 -07001691 status_t threadloop_getHalTimestamp_l(
Andy Hung8d672e02023-09-15 18:19:28 -07001692 ExtendedTimestamp *timestamp) const override
1693 REQUIRES(mutex(), ThreadBase_ThreadLoop) {
Andy Hung1c86ebe2018-05-29 20:29:08 -07001694 if (mNormalSink.get() != nullptr) {
1695 return mNormalSink->getTimestamp(*timestamp);
1696 }
1697 return INVALID_OPERATION;
1698 }
1699
Eric Laurentb0463942022-12-20 16:31:10 +01001700 status_t getSupportedLatencyModes(
1701 std::vector<audio_latency_mode_t>* modes) override;
1702
1703 status_t setBluetoothVariableLatencyEnabled(bool enabled) override;
1704
Andy Hung2ddee192015-12-18 17:34:44 -08001705protected:
1706 virtual void setMasterMono_l(bool mono) {
1707 mMasterMono.store(mono);
1708 if (mFastMixer != nullptr) { /* hasFastMixer() */
1709 mFastMixer->setMasterMono(mMasterMono);
1710 }
1711 }
1712 // the FastMixer performs mono blend if it exists.
Glenn Kasten03c48d52016-01-27 17:25:17 -08001713 // Blending with limiter is not idempotent,
1714 // and blending without limiter is idempotent but inefficient to do twice.
Andy Hung2ddee192015-12-18 17:34:44 -08001715 virtual bool requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001716
Andy Hungab65b182023-09-06 19:41:47 -07001717 void setMasterBalance(float balance) override EXCLUDES_ThreadBase_Mutex {
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001718 mMasterBalance.store(balance);
1719 if (hasFastMixer()) {
1720 mFastMixer->setMasterBalance(balance);
1721 }
1722 }
Eric Laurentb0463942022-12-20 16:31:10 +01001723
Andy Hungab65b182023-09-06 19:41:47 -07001724 void updateHalSupportedLatencyModes_l() REQUIRES(mutex());
1725 void onHalLatencyModesChanged_l() override REQUIRES(mutex());
1726 void setHalLatencyMode_l() override REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001727};
1728
Andy Hung87c693c2023-07-06 20:56:16 -07001729class DirectOutputThread : public PlaybackThread, public virtual IAfDirectOutputThread {
Eric Laurent81784c32012-11-19 14:55:58 -08001730public:
1731
Andy Hung87c693c2023-07-06 20:56:16 -07001732 sp<IAfDirectOutputThread> asIAfDirectOutputThread() final {
1733 return sp<IAfDirectOutputThread>::fromExisting(this);
1734 }
1735
Andy Hung583043b2023-07-17 17:05:00 -07001736 DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Gareth Fennb18c1a32022-10-05 13:42:36 -07001737 audio_io_handle_t id, bool systemReady,
1738 const audio_offload_info_t& offloadInfo)
Andy Hung583043b2023-07-17 17:05:00 -07001739 : DirectOutputThread(afThreadCallback, output, id, DIRECT, systemReady, offloadInfo) { }
Andy Hung48f59ed2019-01-28 15:06:59 -08001740
Andy Hungab65b182023-09-06 19:41:47 -07001741 ~DirectOutputThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001742
Andy Hung87c693c2023-07-06 20:56:16 -07001743 status_t selectPresentation(int presentationId, int programId) final;
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001744
Eric Laurent81784c32012-11-19 14:55:58 -08001745 // Thread virtuals
1746
Eric Laurent10351942014-05-08 18:49:52 -07001747 virtual bool checkForNewParameter_l(const String8& keyValuePair,
Andy Hungab65b182023-09-06 19:41:47 -07001748 status_t& status) REQUIRES(mutex());
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001749
Andy Hung8d672e02023-09-15 18:19:28 -07001750 void flushHw_l() override REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001751
Andy Hungab65b182023-09-06 19:41:47 -07001752 void setMasterBalance(float balance) override EXCLUDES_ThreadBase_Mutex;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001753
Eric Laurent81784c32012-11-19 14:55:58 -08001754protected:
Eric Laurent81784c32012-11-19 14:55:58 -08001755 virtual uint32_t activeSleepTimeUs() const;
1756 virtual uint32_t idleSleepTimeUs() const;
1757 virtual uint32_t suspendSleepTimeUs() const;
Andy Hung8d672e02023-09-15 18:19:28 -07001758 virtual void cacheParameters_l() REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001759
Andy Hungab65b182023-09-06 19:41:47 -07001760 void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex());
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001761
Eric Laurent81784c32012-11-19 14:55:58 -08001762 // threadLoop snippets
Andy Hung8d672e02023-09-15 18:19:28 -07001763 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove)
1764 REQUIRES(mutex(), ThreadBase_ThreadLoop);
1765 virtual void threadLoop_mix() REQUIRES(ThreadBase_ThreadLoop);
1766 virtual void threadLoop_sleepTime() REQUIRES(ThreadBase_ThreadLoop);
1767 virtual void threadLoop_exit() REQUIRES(ThreadBase_ThreadLoop);
Andy Hungab65b182023-09-06 19:41:47 -07001768 virtual bool shouldStandby_l() REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001769
Andy Hungab65b182023-09-06 19:41:47 -07001770 virtual void onAddNewTrack_l() REQUIRES(mutex());
Phil Burk43b4dcc2015-06-09 16:53:44 -07001771
Gareth Fennb18c1a32022-10-05 13:42:36 -07001772 const audio_offload_info_t mOffloadInfo;
Andy Hung398ffa22022-12-13 19:19:53 -08001773
1774 audioflinger::MonotonicFrameCounter mMonotonicFrameCounter; // for VolumeShaper
Andy Hung48f59ed2019-01-28 15:06:59 -08001775 bool mVolumeShaperActive = false;
Eric Laurent81784c32012-11-19 14:55:58 -08001776
Andy Hung583043b2023-07-17 17:05:00 -07001777 DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Gareth Fennb18c1a32022-10-05 13:42:36 -07001778 audio_io_handle_t id, ThreadBase::type_t type, bool systemReady,
1779 const audio_offload_info_t& offloadInfo);
Andy Hungab65b182023-09-06 19:41:47 -07001780 void processVolume_l(IAfTrack *track, bool lastTrack) REQUIRES(mutex());
Gareth Fennb18c1a32022-10-05 13:42:36 -07001781 bool isTunerStream() const { return (mOffloadInfo.content_id > 0); }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001782
Eric Laurent81784c32012-11-19 14:55:58 -08001783 // prepareTracks_l() tells threadLoop_mix() the name of the single active track
Andy Hung8d31fd22023-06-26 19:20:57 -07001784 sp<IAfTrack> mActiveTrack;
Phil Burk43b4dcc2015-06-09 16:53:44 -07001785
Andy Hung8d31fd22023-06-26 19:20:57 -07001786 wp<IAfTrack> mPreviousTrack; // used to detect track switch
Phil Burk43b4dcc2015-06-09 16:53:44 -07001787
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001788 // This must be initialized for initial condition of mMasterBalance = 0 (disabled).
1789 float mMasterBalanceLeft = 1.f;
1790 float mMasterBalanceRight = 1.f;
1791
Eric Laurent81784c32012-11-19 14:55:58 -08001792public:
1793 virtual bool hasFastMixer() const { return false; }
Andy Hung10cbff12017-02-21 17:30:14 -08001794
Andy Hungab65b182023-09-06 19:41:47 -07001795 virtual int64_t computeWaitTimeNs_l() const override REQUIRES(mutex());
Andy Hungf3234512018-07-03 14:51:47 -07001796
1797 status_t threadloop_getHalTimestamp_l(ExtendedTimestamp *timestamp) const override {
1798 // For DIRECT and OFFLOAD threads, query the output sink directly.
1799 if (mOutput != nullptr) {
1800 uint64_t uposition64;
1801 struct timespec time;
1802 if (mOutput->getPresentationPosition(
1803 &uposition64, &time) == OK) {
1804 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL]
1805 = (int64_t)uposition64;
1806 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
1807 = audio_utils_ns_from_timespec(&time);
1808 return NO_ERROR;
1809 }
1810 }
1811 return INVALID_OPERATION;
1812 }
Eric Laurent81784c32012-11-19 14:55:58 -08001813};
1814
Eric Laurentbfb1b832013-01-07 09:53:42 -08001815class OffloadThread : public DirectOutputThread {
1816public:
1817
Andy Hung583043b2023-07-17 17:05:00 -07001818 OffloadThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Gareth Fennb18c1a32022-10-05 13:42:36 -07001819 audio_io_handle_t id, bool systemReady,
1820 const audio_offload_info_t& offloadInfo);
Eric Laurent6a51d7e2013-10-17 18:59:26 -07001821 virtual ~OffloadThread() {};
Andy Hung8d672e02023-09-15 18:19:28 -07001822 void flushHw_l() final REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001823
1824protected:
1825 // threadLoop snippets
Andy Hungab65b182023-09-06 19:41:47 -07001826 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final
Andy Hung8d672e02023-09-15 18:19:28 -07001827 REQUIRES(mutex(), ThreadBase_ThreadLoop);
1828 void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001829
Andy Hungab65b182023-09-06 19:41:47 -07001830 bool waitingAsyncCallback() final;
1831 bool waitingAsyncCallback_l() final REQUIRES(mutex());
1832 void invalidateTracks(audio_stream_type_t streamType) final EXCLUDES_ThreadBase_Mutex;
1833 void invalidateTracks(std::set<audio_port_handle_t>& portIds) final EXCLUDES_ThreadBase_Mutex;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001834
Andy Hungab65b182023-09-06 19:41:47 -07001835 bool keepWakeLock() const final { return (mKeepWakeLock || (mDrainSequence & 1)); }
Eric Laurent64667972016-03-30 18:19:46 -07001836
Eric Laurentbfb1b832013-01-07 09:53:42 -08001837private:
Eric Laurentbfb1b832013-01-07 09:53:42 -08001838 size_t mPausedWriteLength; // length in bytes of write interrupted by pause
1839 size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume
Eric Laurent64667972016-03-30 18:19:46 -07001840 bool mKeepWakeLock; // keep wake lock while waiting for write callback
Eric Laurentbfb1b832013-01-07 09:53:42 -08001841};
1842
1843class AsyncCallbackThread : public Thread {
1844public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001845 explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001846
Eric Laurentbfb1b832013-01-07 09:53:42 -08001847 // Thread virtuals
Andy Hung8d672e02023-09-15 18:19:28 -07001848 bool threadLoop() final;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001849
1850 // RefBase
Andy Hung8d672e02023-09-15 18:19:28 -07001851 void onFirstRef() final;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001852
1853 void exit();
Eric Laurent3b4529e2013-09-05 18:09:19 -07001854 void setWriteBlocked(uint32_t sequence);
1855 void resetWriteBlocked();
1856 void setDraining(uint32_t sequence);
1857 void resetDraining();
Mikhail Naganovbf203ce2024-05-23 16:27:59 -07001858 void setAsyncError(bool isHardError);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001859
1860private:
Eric Laurent4de95592013-09-26 15:28:21 -07001861 const wp<PlaybackThread> mPlaybackThread;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001862 // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
1863 // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
1864 // to indicate that the callback has been received via resetWriteBlocked()
Eric Laurent4de95592013-09-26 15:28:21 -07001865 uint32_t mWriteAckSequence;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001866 // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
1867 // setDraining(). The sequence is shifted one bit to the left and the lsb is used
1868 // to indicate that the callback has been received via resetDraining()
Eric Laurent4de95592013-09-26 15:28:21 -07001869 uint32_t mDrainSequence;
Andy Hungc5007f82023-08-29 14:26:09 -07001870 audio_utils::condition_variable mWaitWorkCV;
Andy Hunga6f1cbb2023-10-12 18:18:13 -07001871 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kAsyncCallbackThread_Mutex};
Mikhail Naganovbf203ce2024-05-23 16:27:59 -07001872 enum AsyncError { ASYNC_ERROR_NONE, ASYNC_ERROR_SOFT, ASYNC_ERROR_HARD };
1873 AsyncError mAsyncError;
Andy Hungc5007f82023-08-29 14:26:09 -07001874
Andy Hungab65b182023-09-06 19:41:47 -07001875 audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::AsyncCallbackThread_Mutex) {
1876 return mMutex;
1877 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001878};
1879
Andy Hung87c693c2023-07-06 20:56:16 -07001880class DuplicatingThread : public MixerThread, public IAfDuplicatingThread {
Eric Laurent81784c32012-11-19 14:55:58 -08001881public:
Andy Hung583043b2023-07-17 17:05:00 -07001882 DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback,
1883 IAfPlaybackThread* mainThread,
Eric Laurent72e3f392015-05-20 14:43:50 -07001884 audio_io_handle_t id, bool systemReady);
Andy Hung87c693c2023-07-06 20:56:16 -07001885 ~DuplicatingThread() override;
1886
1887 sp<IAfDuplicatingThread> asIAfDuplicatingThread() final {
1888 return sp<IAfDuplicatingThread>::fromExisting(this);
1889 }
Eric Laurent81784c32012-11-19 14:55:58 -08001890
1891 // Thread virtuals
Andy Hungab65b182023-09-06 19:41:47 -07001892 void addOutputTrack(IAfPlaybackThread* thread) final EXCLUDES_ThreadBase_Mutex;
1893 void removeOutputTrack(IAfPlaybackThread* thread) final EXCLUDES_ThreadBase_Mutex;
Andy Hung87c693c2023-07-06 20:56:16 -07001894 uint32_t waitTimeMs() const final { return mWaitTimeMs; }
Kevin Rocard069c2712018-03-29 19:09:14 -07001895
Kevin Rocardc86a7f72018-04-03 09:00:09 -07001896 void sendMetadataToBackend_l(
Andy Hungab65b182023-09-06 19:41:47 -07001897 const StreamOutHalInterface::SourceMetadata& metadata) final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001898protected:
1899 virtual uint32_t activeSleepTimeUs() const;
Andy Hungab65b182023-09-06 19:41:47 -07001900 void dumpInternals_l(int fd, const Vector<String16>& args) final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001901
1902private:
Andy Hung8d672e02023-09-15 18:19:28 -07001903 bool outputsReady() REQUIRES(ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001904protected:
1905 // threadLoop snippets
Andy Hung8d672e02023-09-15 18:19:28 -07001906 void threadLoop_mix() final REQUIRES(ThreadBase_ThreadLoop);
1907 void threadLoop_sleepTime() final REQUIRES(ThreadBase_ThreadLoop);
1908 ssize_t threadLoop_write() final REQUIRES(ThreadBase_ThreadLoop);
1909 void threadLoop_standby() final REQUIRES(ThreadBase_ThreadLoop);
Andy Hung8a5abfd2023-12-07 19:35:12 -08001910 void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop);
Andy Hung8d672e02023-09-15 18:19:28 -07001911 void cacheParameters_l() final REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001912
1913private:
1914 // called from threadLoop, addOutputTrack, removeOutputTrack
Andy Hungab65b182023-09-06 19:41:47 -07001915 void updateWaitTime_l() REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001916protected:
Andy Hung8d672e02023-09-15 18:19:28 -07001917 void saveOutputTracks() final REQUIRES(mutex(), ThreadBase_ThreadLoop);
1918 void clearOutputTracks() final REQUIRES(mutex(), ThreadBase_ThreadLoop);
Eric Laurent81784c32012-11-19 14:55:58 -08001919private:
1920
1921 uint32_t mWaitTimeMs;
Andy Hung8d672e02023-09-15 18:19:28 -07001922 // NO_THREAD_SAFETY_ANALYSIS GUARDED_BY(ThreadBase_ThreadLoop)
1923 SortedVector <sp<IAfOutputTrack>> outputTracks;
1924 SortedVector <sp<IAfOutputTrack>> mOutputTracks GUARDED_BY(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001925public:
1926 virtual bool hasFastMixer() const { return false; }
Andy Hung1c86ebe2018-05-29 20:29:08 -07001927 status_t threadloop_getHalTimestamp_l(
Andy Hungab65b182023-09-06 19:41:47 -07001928 ExtendedTimestamp *timestamp) const override REQUIRES(mutex()) {
Andy Hung1c86ebe2018-05-29 20:29:08 -07001929 if (mOutputTracks.size() > 0) {
1930 // forward the first OutputTrack's kernel information for timestamp.
1931 const ExtendedTimestamp trackTimestamp =
1932 mOutputTracks[0]->getClientProxyTimestamp();
1933 if (trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
1934 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
1935 trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
1936 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
1937 trackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1938 return OK; // discard server timestamp - that's ignored.
1939 }
1940 }
1941 return INVALID_OPERATION;
1942 }
Eric Laurent81784c32012-11-19 14:55:58 -08001943};
1944
Eric Laurentb0463942022-12-20 16:31:10 +01001945class SpatializerThread : public MixerThread {
Eric Laurentb3f315a2021-07-13 15:09:05 +02001946public:
Andy Hung583043b2023-07-17 17:05:00 -07001947 SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback,
Eric Laurentb3f315a2021-07-13 15:09:05 +02001948 AudioStreamOut* output,
1949 audio_io_handle_t id,
1950 bool systemReady,
1951 audio_config_base_t *mixerConfig);
Eric Laurentb3f315a2021-07-13 15:09:05 +02001952
Andy Hung440901d2023-06-29 21:19:25 -07001953 bool hasFastMixer() const final { return false; }
Eric Laurentb3f315a2021-07-13 15:09:05 +02001954
Andy Hungab65b182023-09-06 19:41:47 -07001955 status_t setRequestedLatencyMode(audio_latency_mode_t mode) final EXCLUDES_ThreadBase_Mutex;
Eric Laurent68a40a82022-05-03 18:15:04 +02001956
Eric Laurentb3f315a2021-07-13 15:09:05 +02001957protected:
Andy Hung8d672e02023-09-15 18:19:28 -07001958 void checkOutputStageEffects() final
1959 REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex;
Andy Hungab65b182023-09-06 19:41:47 -07001960 void setHalLatencyMode_l() final REQUIRES(mutex());
Eric Laurentb3f315a2021-07-13 15:09:05 +02001961
Andy Hunge2514462023-12-06 14:59:24 -08001962 void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop);
1963
Eric Laurentb3f315a2021-07-13 15:09:05 +02001964private:
Eric Laurent68a40a82022-05-03 18:15:04 +02001965 // Do not request a specific mode by default
1966 audio_latency_mode_t mRequestedLatencyMode = AUDIO_LATENCY_MODE_FREE;
1967
Andy Hung116bc262023-06-20 18:56:17 -07001968 sp<IAfEffectHandle> mFinalDownMixer;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001969};
1970
Eric Laurent81784c32012-11-19 14:55:58 -08001971// record thread
Andy Hung87c693c2023-07-06 20:56:16 -07001972class RecordThread : public IAfRecordThread, public ThreadBase
Eric Laurent81784c32012-11-19 14:55:58 -08001973{
Andy Hung8d31fd22023-06-26 19:20:57 -07001974 friend class ResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08001975public:
Andy Hung87c693c2023-07-06 20:56:16 -07001976 sp<IAfRecordThread> asIAfRecordThread() final {
1977 return sp<IAfRecordThread>::fromExisting(this);
1978 }
Eric Laurent81784c32012-11-19 14:55:58 -08001979
Andy Hung583043b2023-07-17 17:05:00 -07001980 RecordThread(const sp<IAfThreadCallback>& afThreadCallback,
Eric Laurent81784c32012-11-19 14:55:58 -08001981 AudioStreamIn *input,
Eric Laurent81784c32012-11-19 14:55:58 -08001982 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001983 bool systemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08001984 );
Andy Hung440901d2023-06-29 21:19:25 -07001985 ~RecordThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001986
1987 // no addTrack_l ?
Andy Hungab65b182023-09-06 19:41:47 -07001988 void destroyTrack_l(const sp<IAfRecordTrack>& track) final REQUIRES(mutex());
1989 void removeTrack_l(const sp<IAfRecordTrack>& track) final REQUIRES(mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001990
Eric Laurent81784c32012-11-19 14:55:58 -08001991 // Thread virtuals
Andy Hung8d672e02023-09-15 18:19:28 -07001992 bool threadLoop() final REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex;
Andy Hungab65b182023-09-06 19:41:47 -07001993 void preExit() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -08001994
1995 // RefBase
Andy Hungab65b182023-09-06 19:41:47 -07001996 void onFirstRef() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -08001997
Andy Hung440901d2023-06-29 21:19:25 -07001998 status_t initCheck() const final { return mInput == nullptr ? NO_INIT : NO_ERROR; }
Glenn Kastene198c362013-08-13 09:13:36 -07001999
Andy Hung440901d2023-06-29 21:19:25 -07002000 sp<MemoryDealer> readOnlyHeap() const final { return mReadOnlyHeap; }
Glenn Kastenb880f5e2014-05-07 08:43:45 -07002001
Andy Hung440901d2023-06-29 21:19:25 -07002002 sp<IMemory> pipeMemory() const final { return mPipeMemory; }
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002003
Andy Hung87c693c2023-07-06 20:56:16 -07002004 sp<IAfRecordTrack> createRecordTrack_l(
Andy Hung88035ac2023-06-27 17:05:02 -07002005 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002006 const audio_attributes_t& attr,
Eric Laurentf14db3c2017-12-08 14:20:36 -08002007 uint32_t *pSampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -08002008 audio_format_t format,
2009 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08002010 size_t *pFrameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08002011 audio_session_t sessionId,
Eric Laurentf14db3c2017-12-08 14:20:36 -08002012 size_t *pNotificationFrameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002013 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00002014 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -07002015 audio_input_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -08002016 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08002017 status_t *status /*non-NULL*/,
Eric Laurentec376dc2021-04-08 20:41:22 +02002018 audio_port_handle_t portId,
Andy Hung972bec12023-08-31 16:13:39 -07002019 int32_t maxSharedAudioHistoryMs) final
Andy Hungab65b182023-09-06 19:41:47 -07002020 REQUIRES(audio_utils::AudioFlinger_Mutex) EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -08002021
Andy Hung8d31fd22023-06-26 19:20:57 -07002022 status_t start(IAfRecordTrack* recordTrack,
Eric Laurent81784c32012-11-19 14:55:58 -08002023 AudioSystem::sync_event_t event,
Andy Hungab65b182023-09-06 19:41:47 -07002024 audio_session_t triggerSession) final EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -08002025
2026 // ask the thread to stop the specified track, and
2027 // return true if the caller should then do it's part of the stopping process
Andy Hungab65b182023-09-06 19:41:47 -07002028 bool stop(IAfRecordTrack* recordTrack) final EXCLUDES_ThreadBase_Mutex;
Andy Hung87c693c2023-07-06 20:56:16 -07002029 AudioStreamIn* getInput() const final { return mInput; }
2030 AudioStreamIn* clearInput() final;
Eric Laurent81784c32012-11-19 14:55:58 -08002031
Andy Hung99b1ba62023-07-14 11:00:08 -07002032 // TODO(b/291317898) Unify with IAfThreadBase
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002033 virtual sp<StreamHalInterface> stream() const;
Eric Laurent81784c32012-11-19 14:55:58 -08002034
Eric Laurent81784c32012-11-19 14:55:58 -08002035
Andy Hungab65b182023-09-06 19:41:47 -07002036 virtual bool checkForNewParameter_l(const String8& keyValuePair,
2037 status_t& status) REQUIRES(mutex());
Andy Hung8d672e02023-09-15 18:19:28 -07002038 virtual void cacheParameters_l() REQUIRES(mutex(), ThreadBase_ThreadLoop) {}
Andy Hungab65b182023-09-06 19:41:47 -07002039 virtual String8 getParameters(const String8& keys) EXCLUDES_ThreadBase_Mutex;
2040
2041 // Hold either the AudioFlinger::mutex or the ThreadBase::mutex
2042 void ioConfigChanged_l(audio_io_config_event_t event, pid_t pid = 0,
Andy Hung87c693c2023-07-06 20:56:16 -07002043 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
Eric Laurent1c333e22014-05-20 10:48:17 -07002044 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
Andy Hungab65b182023-09-06 19:41:47 -07002045 audio_patch_handle_t *handle) REQUIRES(mutex());
2046 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) REQUIRES(mutex());
2047 void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override
2048 EXCLUDES_ThreadBase_Mutex;
2049 void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override REQUIRES(mutex());
Eric Laurent83b88082014-06-20 18:31:16 -07002050
Andy Hungab65b182023-09-06 19:41:47 -07002051 void addPatchTrack(const sp<IAfPatchRecord>& record) final EXCLUDES_ThreadBase_Mutex;
2052 void deletePatchTrack(const sp<IAfPatchRecord>& record) final EXCLUDES_ThreadBase_Mutex;
Eric Laurent83b88082014-06-20 18:31:16 -07002053
Andy Hungab65b182023-09-06 19:41:47 -07002054 void readInputParameters_l() REQUIRES(mutex());
2055 uint32_t getInputFramesLost() const final EXCLUDES_ThreadBase_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -08002056
Andy Hungab65b182023-09-06 19:41:47 -07002057 virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain) REQUIRES(mutex());
2058 virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) REQUIRES(mutex());
2059 uint32_t hasAudioSession_l(audio_session_t sessionId) const override REQUIRES(mutex()) {
Andy Hungc3d62f92019-03-14 13:38:51 -07002060 return ThreadBase::hasAudioSession_l(sessionId, mTracks);
2061 }
Eric Laurent81784c32012-11-19 14:55:58 -08002062
2063 // Return the set of unique session IDs across all tracks.
2064 // The keys are the session IDs, and the associated values are meaningless.
2065 // FIXME replace by Set [and implement Bag/Multiset for other uses].
Glenn Kastend848eb42016-03-08 13:42:11 -08002066 KeyedVector<audio_session_t, bool> sessionIds() const;
Eric Laurent81784c32012-11-19 14:55:58 -08002067
Andy Hungab65b182023-09-06 19:41:47 -07002068 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) override
2069 EXCLUDES_ThreadBase_Mutex;
Andy Hung068e08e2023-05-15 19:02:55 -07002070 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const override;
Eric Laurent81784c32012-11-19 14:55:58 -08002071
Andy Hung068e08e2023-05-15 19:02:55 -07002072 static void syncStartEventCallback(const wp<audioflinger::SyncEvent>& event);
Eric Laurent81784c32012-11-19 14:55:58 -08002073
Glenn Kasten9b58f632013-07-16 11:37:48 -07002074 virtual size_t frameCount() const { return mFrameCount; }
Andy Hung87c693c2023-07-06 20:56:16 -07002075 bool hasFastCapture() const final { return mFastCapture != 0; }
Mikhail Naganovdc769682018-05-04 15:34:08 -07002076 virtual void toAudioPortConfig(struct audio_port_config *config);
Glenn Kasten9b58f632013-07-16 11:37:48 -07002077
Andy Hungab65b182023-09-06 19:41:47 -07002078 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
2079 audio_session_t sessionId) REQUIRES(mutex());
Eric Laurent4c415062016-06-17 16:14:16 -07002080
Andy Hungab65b182023-09-06 19:41:47 -07002081 virtual void acquireWakeLock_l() REQUIRES(mutex()) {
Andy Hungdae27702016-10-31 14:01:16 -07002082 ThreadBase::acquireWakeLock_l();
Andy Hungab65b182023-09-06 19:41:47 -07002083 mActiveTracks.updatePowerState_l(this, true /* force */);
Andy Hungdae27702016-10-31 14:01:16 -07002084 }
2085
Andy Hungab65b182023-09-06 19:41:47 -07002086 void checkBtNrec() final EXCLUDES_ThreadBase_Mutex;
Eric Laurentd8365c52017-07-16 15:27:05 -07002087
Svet Ganovf4ddfef2018-01-16 07:37:58 -08002088 // Sets the UID records silence
Andy Hungab65b182023-09-06 19:41:47 -07002089 void setRecordSilenced(audio_port_handle_t portId, bool silenced) final
2090 EXCLUDES_ThreadBase_Mutex;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08002091
Andy Hung87c693c2023-07-06 20:56:16 -07002092 status_t getActiveMicrophones(
Andy Hungab65b182023-09-06 19:41:47 -07002093 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const final
2094 EXCLUDES_ThreadBase_Mutex;
2095 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) final
2096 EXCLUDES_ThreadBase_Mutex;
2097 status_t setPreferredMicrophoneFieldDimension(float zoom) final EXCLUDES_ThreadBase_Mutex;
Paul McLean03a6e6a2018-12-04 10:54:13 -07002098
Andy Hungab65b182023-09-06 19:41:47 -07002099 MetadataUpdate updateMetadata_l() override REQUIRES(mutex());
Kevin Rocard069c2712018-03-29 19:09:14 -07002100
Andy Hung87c693c2023-07-06 20:56:16 -07002101 bool fastTrackAvailable() const final { return mFastTrackAvail; }
2102 void setFastTrackAvailable(bool available) final { mFastTrackAvail = available; }
jiabin01c8f562018-07-19 17:47:28 -07002103
Andy Hungab65b182023-09-06 19:41:47 -07002104 bool isTimestampCorrectionEnabled_l() const override REQUIRES(mutex()) {
Andy Hungc8fddf32018-08-08 18:32:37 -07002105 // checks popcount for exactly one device.
Atneya Nair497fff12022-01-18 16:23:04 -05002106 // Is currently disabled. Before enabling,
2107 // verify compressed record timestamps.
jiabinc52b1ff2019-10-31 17:20:42 -07002108 return audio_is_input_device(mTimestampCorrectedDevice)
Andy Hungab65b182023-09-06 19:41:47 -07002109 && inDeviceType_l() == mTimestampCorrectedDevice;
Andy Hungc8fddf32018-08-08 18:32:37 -07002110 }
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07002111
Andy Hung87c693c2023-07-06 20:56:16 -07002112 status_t shareAudioHistory(const std::string& sharedAudioPackageName,
Eric Laurentec376dc2021-04-08 20:41:22 +02002113 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
Andy Hungab65b182023-09-06 19:41:47 -07002114 int64_t sharedAudioStartMs = -1) final EXCLUDES_ThreadBase_Mutex;
Eric Laurentec376dc2021-04-08 20:41:22 +02002115 status_t shareAudioHistory_l(const std::string& sharedAudioPackageName,
2116 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
Andy Hungab65b182023-09-06 19:41:47 -07002117 int64_t sharedAudioStartMs = -1) REQUIRES(mutex());
2118 void resetAudioHistory_l() final REQUIRES(mutex());
Eric Laurentec376dc2021-04-08 20:41:22 +02002119
Andy Hung440901d2023-06-29 21:19:25 -07002120 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002121 return !(mInput == nullptr || mInput->stream == nullptr);
2122 }
2123
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07002124protected:
Andy Hungab65b182023-09-06 19:41:47 -07002125 void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex());
2126 void dumpTracks_l(int fd, const Vector<String16>& args) override REQUIRES(mutex());
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07002127
Eric Laurent81784c32012-11-19 14:55:58 -08002128private:
Eric Laurent81784c32012-11-19 14:55:58 -08002129 // Enter standby if not already in standby, and set mStandby flag
Glenn Kasten93e471f2013-08-19 08:40:07 -07002130 void standbyIfNotAlreadyInStandby();
Eric Laurent81784c32012-11-19 14:55:58 -08002131
2132 // Call the HAL standby method unconditionally, and don't change mStandby flag
Glenn Kastene198c362013-08-13 09:13:36 -07002133 void inputStandBy();
Eric Laurent81784c32012-11-19 14:55:58 -08002134
Andy Hungab65b182023-09-06 19:41:47 -07002135 void checkBtNrec_l() REQUIRES(mutex());
Eric Laurentd8365c52017-07-16 15:27:05 -07002136
Andy Hungab65b182023-09-06 19:41:47 -07002137 int32_t getOldestFront_l() REQUIRES(mutex());
2138 void updateFronts_l(int32_t offset) REQUIRES(mutex());
Eric Laurentec376dc2021-04-08 20:41:22 +02002139
Eric Laurent81784c32012-11-19 14:55:58 -08002140 AudioStreamIn *mInput;
Mikhail Naganov2534b382019-09-25 13:05:02 -07002141 Source *mSource;
Andy Hung8d31fd22023-06-26 19:20:57 -07002142 SortedVector <sp<IAfRecordTrack>> mTracks;
Glenn Kasten2b806402013-11-20 16:37:38 -08002143 // mActiveTracks has dual roles: it indicates the current active track(s), and
Andy Hungc5007f82023-08-29 14:26:09 -07002144 // is used together with mStartStopCV to indicate start()/stop() progress
Andy Hung8d31fd22023-06-26 19:20:57 -07002145 ActiveTracks<IAfRecordTrack> mActiveTracks;
Andy Hungdae27702016-10-31 14:01:16 -07002146
Andy Hungc5007f82023-08-29 14:26:09 -07002147 audio_utils::condition_variable mStartStopCV;
Glenn Kasten9b58f632013-07-16 11:37:48 -07002148
Glenn Kasten85948432013-08-19 12:09:05 -07002149 // resampler converts input at HAL Hz to output at AudioRecord client Hz
Glenn Kasten1b291842016-07-18 14:55:21 -07002150 void *mRsmpInBuffer; // size = mRsmpInFramesOA
Glenn Kasten85948432013-08-19 12:09:05 -07002151 size_t mRsmpInFrames; // size of resampler input in frames
2152 size_t mRsmpInFramesP2;// size rounded up to a power-of-2
Glenn Kasten1b291842016-07-18 14:55:21 -07002153 size_t mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002154
2155 // rolling index that is never cleared
Glenn Kasten85948432013-08-19 12:09:05 -07002156 int32_t mRsmpInRear; // last filled frame + 1
Glenn Kasten85948432013-08-19 12:09:05 -07002157
Eric Laurent81784c32012-11-19 14:55:58 -08002158 // For dumpsys
Glenn Kastenb880f5e2014-05-07 08:43:45 -07002159 const sp<MemoryDealer> mReadOnlyHeap;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002160
2161 // one-time initialization, no locks required
Glenn Kastenb187de12014-12-30 08:18:15 -08002162 sp<FastCapture> mFastCapture; // non-0 if there is also
2163 // a fast capture
Eric Laurent72e3f392015-05-20 14:43:50 -07002164
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002165 // FIXME audio watchdog thread
2166
2167 // contents are not guaranteed to be consistent, no locks required
2168 FastCaptureDumpState mFastCaptureDumpState;
2169#ifdef STATE_QUEUE_DUMP
2170 // FIXME StateQueue observer and mutator dump fields
2171#endif
2172 // FIXME audio watchdog dump
2173
2174 // accessible only within the threadLoop(), no locks required
2175 // mFastCapture->sq() // for mutating and pushing state
2176 int32_t mFastCaptureFutex; // for cold idle
2177
2178 // The HAL input source is treated as non-blocking,
2179 // but current implementation is blocking
2180 sp<NBAIO_Source> mInputSource;
2181 // The source for the normal capture thread to read from: mInputSource or mPipeSource
2182 sp<NBAIO_Source> mNormalSource;
2183 // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
2184 // otherwise clear
2185 sp<NBAIO_Sink> mPipeSink;
2186 // If a fast capture is present, the non-blocking pipe source read by normal thread,
2187 // otherwise clear
2188 sp<NBAIO_Source> mPipeSource;
2189 // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
2190 size_t mPipeFramesP2;
2191 // If a fast capture is present, the Pipe as IMemory, otherwise clear
2192 sp<IMemory> mPipeMemory;
2193
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07002194 // TODO: add comment and adjust size as needed
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002195 static const size_t kFastCaptureLogSize = 4 * 1024;
2196 sp<NBLog::Writer> mFastCaptureNBLogWriter;
2197
2198 bool mFastTrackAvail; // true if fast track available
Eric Laurentd8365c52017-07-16 15:27:05 -07002199 // common state to all record threads
2200 std::atomic_bool mBtNrecSuspended;
Andy Hung6427e442018-08-09 12:51:02 -07002201
2202 int64_t mFramesRead = 0; // continuous running counter.
jiabinc52b1ff2019-10-31 17:20:42 -07002203
2204 DeviceDescriptorBaseVector mOutDevices;
Eric Laurentec376dc2021-04-08 20:41:22 +02002205
Eric Laurent5f0fd7b2021-05-07 16:33:26 +02002206 int32_t mMaxSharedAudioHistoryMs = 0;
Eric Laurentec376dc2021-04-08 20:41:22 +02002207 std::string mSharedAudioPackageName = {};
Eric Laurent2407ce32021-04-26 14:56:03 +02002208 int32_t mSharedAudioStartFrames = -1;
Eric Laurentec376dc2021-04-08 20:41:22 +02002209 audio_session_t mSharedAudioSessionId = AUDIO_SESSION_NONE;
Eric Laurent81784c32012-11-19 14:55:58 -08002210};
Eric Laurent6acd1d42017-01-04 14:23:29 -08002211
Andy Hung7aa7d102023-07-07 15:58:48 -07002212class MmapThread : public ThreadBase, public virtual IAfMmapThread
Eric Laurent6acd1d42017-01-04 14:23:29 -08002213{
2214 public:
Andy Hung583043b2023-07-17 17:05:00 -07002215 MmapThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
Andy Hung920f6572022-10-06 12:09:49 -07002216 AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady,
Andy Hungcf10d742020-04-28 15:38:24 -07002217 bool isOut);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002218
Andy Hung7aa7d102023-07-07 15:58:48 -07002219 void configure(const audio_attributes_t* attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08002220 audio_stream_type_t streamType,
2221 audio_session_t sessionId,
2222 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07002223 audio_port_handle_t deviceId,
Andy Hung8d672e02023-09-15 18:19:28 -07002224 audio_port_handle_t portId) override EXCLUDES_ThreadBase_Mutex {
2225 audio_utils::lock_guard l(mutex());
2226 configure_l(attr, streamType, sessionId, callback, deviceId, portId);
2227 }
2228
2229 void configure_l(const audio_attributes_t* attr,
2230 audio_stream_type_t streamType,
2231 audio_session_t sessionId,
2232 const sp<MmapStreamCallback>& callback,
2233 audio_port_handle_t deviceId,
2234 audio_port_handle_t portId) REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002235
Andy Hungab65b182023-09-06 19:41:47 -07002236 void disconnect() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002237
Andy Hung440901d2023-06-29 21:19:25 -07002238 // MmapStreamInterface for adapter.
Andy Hung3f49ebb2023-09-19 14:48:41 -07002239 status_t createMmapBuffer(int32_t minSizeFrames, struct audio_mmap_buffer_info* info) final
2240 EXCLUDES_ThreadBase_Mutex;
2241 status_t getMmapPosition(struct audio_mmap_position* position) const override
2242 EXCLUDES_ThreadBase_Mutex;
Andy Hung7aa7d102023-07-07 15:58:48 -07002243 status_t start(const AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -07002244 const audio_attributes_t *attr,
Andy Hungab65b182023-09-06 19:41:47 -07002245 audio_port_handle_t* handle) final EXCLUDES_ThreadBase_Mutex;
2246 status_t stop(audio_port_handle_t handle) final EXCLUDES_ThreadBase_Mutex;
2247 status_t standby() final EXCLUDES_ThreadBase_Mutex;
Andy Hung3f49ebb2023-09-19 14:48:41 -07002248 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const
2249 EXCLUDES_ThreadBase_Mutex = 0;
2250 status_t reportData(const void* buffer, size_t frameCount) override EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002251
2252 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -07002253 void onFirstRef() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002254
2255 // Thread virtuals
Andy Hung8d672e02023-09-15 18:19:28 -07002256 bool threadLoop() final REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002257
Andy Hung440901d2023-06-29 21:19:25 -07002258 // Not in ThreadBase
Andy Hung8d672e02023-09-15 18:19:28 -07002259 virtual void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop);
2260 virtual void threadLoop_standby() final REQUIRES(ThreadBase_ThreadLoop);
Andy Hungab65b182023-09-06 19:41:47 -07002261 virtual bool shouldStandby_l() final REQUIRES(mutex()){ return false; }
Andy Hungc5007f82023-08-29 14:26:09 -07002262 virtual status_t exitStandby_l() REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002263
Andy Hung440901d2023-06-29 21:19:25 -07002264 status_t initCheck() const final { return mHalStream == nullptr ? NO_INIT : NO_ERROR; }
2265 size_t frameCount() const final { return mFrameCount; }
Andy Hungab65b182023-09-06 19:41:47 -07002266 bool checkForNewParameter_l(const String8& keyValuePair, status_t& status)
2267 final REQUIRES(mutex());
2268 String8 getParameters(const String8& keys) final EXCLUDES_ThreadBase_Mutex;
2269 void ioConfigChanged_l(audio_io_config_event_t event, pid_t pid = 0,
2270 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final
2271 /* holds either AF::mutex or TB::mutex */;
2272 void readHalParameters_l() REQUIRES(mutex());
Andy Hung8d672e02023-09-15 18:19:28 -07002273 void cacheParameters_l() final REQUIRES(mutex(), ThreadBase_ThreadLoop) {}
Andy Hung440901d2023-06-29 21:19:25 -07002274 status_t createAudioPatch_l(
Andy Hungab65b182023-09-06 19:41:47 -07002275 const struct audio_patch* patch, audio_patch_handle_t* handle) final
2276 REQUIRES(mutex());
2277 status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final
2278 REQUIRES(mutex());
Andy Hung3f49ebb2023-09-19 14:48:41 -07002279 // NO_THREAD_SAFETY_ANALYSIS
Andy Hung440901d2023-06-29 21:19:25 -07002280 void toAudioPortConfig(struct audio_port_config* config) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002281
Andy Hung440901d2023-06-29 21:19:25 -07002282 sp<StreamHalInterface> stream() const final { return mHalStream; }
Andy Hungab65b182023-09-06 19:41:47 -07002283 status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex());
2284 size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex());
Andy Hung440901d2023-06-29 21:19:25 -07002285 status_t checkEffectCompatibility_l(
Andy Hungab65b182023-09-06 19:41:47 -07002286 const effect_descriptor_t *desc, audio_session_t sessionId) final REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002287
Andy Hungab65b182023-09-06 19:41:47 -07002288 uint32_t hasAudioSession_l(audio_session_t sessionId) const override REQUIRES(mutex()) {
Andy Hungc3d62f92019-03-14 13:38:51 -07002289 // Note: using mActiveTracks as no mTracks here.
2290 return ThreadBase::hasAudioSession_l(sessionId, mActiveTracks);
2291 }
Andy Hung440901d2023-06-29 21:19:25 -07002292 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final;
2293 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002294
Andy Hungab65b182023-09-06 19:41:47 -07002295 virtual void checkSilentMode_l() REQUIRES(mutex()) {} // cannot be const (RecordThread)
2296 virtual void processVolume_l() REQUIRES(mutex()) {}
Andy Hung8d672e02023-09-15 18:19:28 -07002297 void checkInvalidTracks_l() REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002298
Andy Hung440901d2023-06-29 21:19:25 -07002299 // Not in ThreadBase
Andy Hung3f49ebb2023-09-19 14:48:41 -07002300 virtual audio_stream_type_t streamType_l() const REQUIRES(mutex()) {
2301 return AUDIO_STREAM_DEFAULT;
2302 }
Andy Hungab65b182023-09-06 19:41:47 -07002303 virtual void invalidateTracks(audio_stream_type_t /* streamType */)
2304 EXCLUDES_ThreadBase_Mutex {}
2305 void invalidateTracks(std::set<audio_port_handle_t>& /* portIds */) override
2306 EXCLUDES_ThreadBase_Mutex {}
Eric Laurent6acd1d42017-01-04 14:23:29 -08002307
Eric Laurent331679c2018-04-16 17:03:16 -07002308 // Sets the UID records silence
Andy Hung7aa7d102023-07-07 15:58:48 -07002309 void setRecordSilenced(
Andy Hungab65b182023-09-06 19:41:47 -07002310 audio_port_handle_t /* portId */, bool /* silenced */) override
2311 EXCLUDES_ThreadBase_Mutex {}
Eric Laurent331679c2018-04-16 17:03:16 -07002312
Andy Hung440901d2023-06-29 21:19:25 -07002313 bool isStreamInitialized() const override { return false; }
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002314
Andy Hungab65b182023-09-06 19:41:47 -07002315 void setClientSilencedState_l(audio_port_handle_t portId, bool silenced) REQUIRES(mutex()) {
jiabin09609032022-06-15 19:26:01 +00002316 mClientSilencedStates[portId] = silenced;
2317 }
2318
Andy Hungab65b182023-09-06 19:41:47 -07002319 size_t eraseClientSilencedState_l(audio_port_handle_t portId) REQUIRES(mutex()) {
jiabin09609032022-06-15 19:26:01 +00002320 return mClientSilencedStates.erase(portId);
2321 }
2322
Andy Hungab65b182023-09-06 19:41:47 -07002323 bool isClientSilenced_l(audio_port_handle_t portId) const REQUIRES(mutex()) {
jiabin09609032022-06-15 19:26:01 +00002324 const auto it = mClientSilencedStates.find(portId);
2325 return it != mClientSilencedStates.end() ? it->second : false;
2326 }
2327
Andy Hungab65b182023-09-06 19:41:47 -07002328 void setClientSilencedIfExists_l(audio_port_handle_t portId, bool silenced)
2329 REQUIRES(mutex()) {
jiabin09609032022-06-15 19:26:01 +00002330 const auto it = mClientSilencedStates.find(portId);
2331 if (it != mClientSilencedStates.end()) {
2332 it->second = silenced;
2333 }
2334 }
2335
Eric Laurent6acd1d42017-01-04 14:23:29 -08002336 protected:
Andy Hungab65b182023-09-06 19:41:47 -07002337 void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex());
2338 void dumpTracks_l(int fd, const Vector<String16>& args) final REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002339
jiabinc52b1ff2019-10-31 17:20:42 -07002340 /**
2341 * @brief mDeviceId current device port unique identifier
2342 */
Andy Hung3f49ebb2023-09-19 14:48:41 -07002343 audio_port_handle_t mDeviceId GUARDED_BY(mutex()) = AUDIO_PORT_HANDLE_NONE;
jiabinc52b1ff2019-10-31 17:20:42 -07002344
Andy Hung3f49ebb2023-09-19 14:48:41 -07002345 audio_attributes_t mAttr GUARDED_BY(mutex());
2346 audio_session_t mSessionId GUARDED_BY(mutex());
2347 audio_port_handle_t mPortId GUARDED_BY(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002348
Andy Hung3f49ebb2023-09-19 14:48:41 -07002349 wp<MmapStreamCallback> mCallback GUARDED_BY(mutex());
2350 sp<StreamHalInterface> mHalStream; // NO_THREAD_SAFETY_ANALYSIS
2351 sp<DeviceHalInterface> mHalDevice GUARDED_BY(mutex());
2352 AudioHwDevice* const mAudioHwDev GUARDED_BY(mutex());
2353 ActiveTracks<IAfMmapTrack> mActiveTracks GUARDED_BY(mutex());
2354 float mHalVolFloat GUARDED_BY(mutex());
2355 std::map<audio_port_handle_t, bool> mClientSilencedStates GUARDED_BY(mutex());
Eric Laurent331679c2018-04-16 17:03:16 -07002356
Andy Hung3f49ebb2023-09-19 14:48:41 -07002357 int32_t mNoCallbackWarningCount GUARDED_BY(mutex());
2358 static constexpr int32_t kMaxNoCallbackWarnings = 5;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002359};
2360
Andy Hung7aa7d102023-07-07 15:58:48 -07002361class MmapPlaybackThread : public MmapThread, public IAfMmapPlaybackThread,
2362 public virtual VolumeInterface {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002363public:
Andy Hung583043b2023-07-17 17:05:00 -07002364 MmapPlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
jiabinc52b1ff2019-10-31 17:20:42 -07002365 AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002366
Andy Hung7aa7d102023-07-07 15:58:48 -07002367 sp<IAfMmapPlaybackThread> asIAfMmapPlaybackThread() final {
2368 return sp<IAfMmapPlaybackThread>::fromExisting(this);
2369 }
2370
Andy Hung440901d2023-06-29 21:19:25 -07002371 void configure(const audio_attributes_t* attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08002372 audio_stream_type_t streamType,
2373 audio_session_t sessionId,
2374 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07002375 audio_port_handle_t deviceId,
Andy Hung8d672e02023-09-15 18:19:28 -07002376 audio_port_handle_t portId) final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002377
Andy Hungab65b182023-09-06 19:41:47 -07002378 AudioStreamOut* clearOutput() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002379
2380 // VolumeInterface
Andy Hung440901d2023-06-29 21:19:25 -07002381 void setMasterVolume(float value) final;
Andy Hungab65b182023-09-06 19:41:47 -07002382 // Needs implementation?
2383 void setMasterBalance(float /* value */) final EXCLUDES_ThreadBase_Mutex {}
Andy Hung8d672e02023-09-15 18:19:28 -07002384 void setMasterMute(bool muted) final EXCLUDES_ThreadBase_Mutex;
Andy Hungab65b182023-09-06 19:41:47 -07002385 void setStreamVolume(audio_stream_type_t stream, float value) final EXCLUDES_ThreadBase_Mutex;
2386 void setStreamMute(audio_stream_type_t stream, bool muted) final EXCLUDES_ThreadBase_Mutex;
2387 float streamVolume(audio_stream_type_t stream) const final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002388
Andy Hung8d672e02023-09-15 18:19:28 -07002389 void setMasterMute_l(bool muted) REQUIRES(mutex()) { mMasterMute = muted; }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002390
Andy Hungab65b182023-09-06 19:41:47 -07002391 void invalidateTracks(audio_stream_type_t streamType) final EXCLUDES_ThreadBase_Mutex;
2392 void invalidateTracks(std::set<audio_port_handle_t>& portIds) final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002393
Andy Hung3f49ebb2023-09-19 14:48:41 -07002394 audio_stream_type_t streamType_l() const final REQUIRES(mutex()) {
Andy Hung8d672e02023-09-15 18:19:28 -07002395 return mStreamType;
2396 }
Andy Hungab65b182023-09-06 19:41:47 -07002397 void checkSilentMode_l() final REQUIRES(mutex());
2398 void processVolume_l() final REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002399
Andy Hungab65b182023-09-06 19:41:47 -07002400 MetadataUpdate updateMetadata_l() final REQUIRES(mutex());
Kevin Rocard069c2712018-03-29 19:09:14 -07002401
Andy Hung440901d2023-06-29 21:19:25 -07002402 void toAudioPortConfig(struct audio_port_config* config) final;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07002403
Andy Hung440901d2023-06-29 21:19:25 -07002404 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final;
jiabinb7d8c5a2020-08-26 17:24:52 -07002405
Andy Hung440901d2023-06-29 21:19:25 -07002406 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002407 return !(mOutput == nullptr || mOutput->stream == nullptr);
2408 }
2409
Andy Hung440901d2023-06-29 21:19:25 -07002410 status_t reportData(const void* buffer, size_t frameCount) final;
jiabinfc791ee2023-02-15 19:43:40 +00002411
Andy Hung972bec12023-08-31 16:13:39 -07002412 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) final
2413 REQUIRES(audio_utils::AudioFlinger_Mutex);
2414 void stopMelComputation_l() final
2415 REQUIRES(audio_utils::AudioFlinger_Mutex);
Vlad Popa6fbbfbf2023-02-22 15:05:43 +01002416
Eric Laurent6acd1d42017-01-04 14:23:29 -08002417protected:
Andy Hungab65b182023-09-06 19:41:47 -07002418 void dumpInternals_l(int fd, const Vector<String16>& args) final REQUIRES(mutex());
Andy Hung8d672e02023-09-15 18:19:28 -07002419 float streamVolume_l() const REQUIRES(mutex()) {
Eric Laurent1f9b5e62023-07-03 18:14:07 +02002420 return mStreamTypes[mStreamType].volume;
2421 }
Andy Hung8d672e02023-09-15 18:19:28 -07002422 bool streamMuted_l() const REQUIRES(mutex()) {
Eric Laurent1f9b5e62023-07-03 18:14:07 +02002423 return mStreamTypes[mStreamType].mute;
2424 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002425
Andy Hung8d672e02023-09-15 18:19:28 -07002426 stream_type_t mStreamTypes[AUDIO_STREAM_CNT] GUARDED_BY(mutex());
2427 audio_stream_type_t mStreamType GUARDED_BY(mutex());
2428 float mMasterVolume GUARDED_BY(mutex());
2429 bool mMasterMute GUARDED_BY(mutex());
2430 AudioStreamOut* mOutput; // NO_THREAD_SAFETY_ANALYSIS
Vlad Popa6fbbfbf2023-02-22 15:05:43 +01002431
Andy Hung8d672e02023-09-15 18:19:28 -07002432 mediautils::atomic_sp<audio_utils::MelProcessor> mMelProcessor; // locked internally
Eric Laurent6acd1d42017-01-04 14:23:29 -08002433};
2434
Andy Hung7aa7d102023-07-07 15:58:48 -07002435class MmapCaptureThread : public MmapThread, public IAfMmapCaptureThread
Eric Laurent6acd1d42017-01-04 14:23:29 -08002436{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002437public:
Andy Hung583043b2023-07-17 17:05:00 -07002438 MmapCaptureThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
jiabinc52b1ff2019-10-31 17:20:42 -07002439 AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002440
Andy Hung7aa7d102023-07-07 15:58:48 -07002441 sp<IAfMmapCaptureThread> asIAfMmapCaptureThread() final {
2442 return sp<IAfMmapCaptureThread>::fromExisting(this);
2443 }
2444
Andy Hungab65b182023-09-06 19:41:47 -07002445 AudioStreamIn* clearInput() final EXCLUDES_ThreadBase_Mutex;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002446
Andy Hungc5007f82023-08-29 14:26:09 -07002447 status_t exitStandby_l() REQUIRES(mutex()) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002448
Andy Hungab65b182023-09-06 19:41:47 -07002449 MetadataUpdate updateMetadata_l() final REQUIRES(mutex());
2450 void processVolume_l() final REQUIRES(mutex());
2451 void setRecordSilenced(audio_port_handle_t portId, bool silenced) final
2452 EXCLUDES_ThreadBase_Mutex;
Kevin Rocard069c2712018-03-29 19:09:14 -07002453
Andy Hung440901d2023-06-29 21:19:25 -07002454 void toAudioPortConfig(struct audio_port_config* config) final;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07002455
Andy Hung440901d2023-06-29 21:19:25 -07002456 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final;
jiabinb7d8c5a2020-08-26 17:24:52 -07002457
Andy Hung440901d2023-06-29 21:19:25 -07002458 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002459 return !(mInput == nullptr || mInput->stream == nullptr);
2460 }
2461
Eric Laurent6acd1d42017-01-04 14:23:29 -08002462protected:
2463
Andy Hung8d672e02023-09-15 18:19:28 -07002464 AudioStreamIn* mInput; // NO_THREAD_SAFETY_ANALYSIS
Eric Laurent6acd1d42017-01-04 14:23:29 -08002465};
jiabinc658e452022-10-21 20:52:21 +00002466
2467class BitPerfectThread : public MixerThread {
2468public:
Andy Hung583043b2023-07-17 17:05:00 -07002469 BitPerfectThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut *output,
jiabinc658e452022-10-21 20:52:21 +00002470 audio_io_handle_t id, bool systemReady);
2471
jiabin220eea12024-05-17 17:55:20 +00002472 void setTracksInternalMute(std::map<audio_port_handle_t, bool>* tracksInternalMuted)
2473 final EXCLUDES_ThreadBase_Mutex;
2474
jiabinc658e452022-10-21 20:52:21 +00002475protected:
Andy Hung8d672e02023-09-15 18:19:28 -07002476 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final
2477 REQUIRES(mutex(), ThreadBase_ThreadLoop);
2478 void threadLoop_mix() final REQUIRES(ThreadBase_ThreadLoop);
jiabinc658e452022-10-21 20:52:21 +00002479
2480private:
jiabin220eea12024-05-17 17:55:20 +00002481 sp<IAfTrack> getTrackToStreamBitPerfectly_l() REQUIRES(mutex());
2482
Andy Hung8d672e02023-09-15 18:19:28 -07002483 // These variables are only accessed on the threadLoop; hence need no mutex.
2484 bool mIsBitPerfect GUARDED_BY(ThreadBase_ThreadLoop) = false;
2485 float mVolumeLeft GUARDED_BY(ThreadBase_ThreadLoop) = 0.f;
2486 float mVolumeRight GUARDED_BY(ThreadBase_ThreadLoop) = 0.f;
jiabinc658e452022-10-21 20:52:21 +00002487};
Andy Hunga5a7fc92023-06-23 19:27:19 -07002488
Andy Hungee58e4a2023-07-07 13:47:37 -07002489} // namespace android