blob: 89884d4158b9d4ea01d8cff972e03e4039aa2d76 [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 Hung583043b2023-07-17 17:05:00 -070047 IAfThreadCallback* afThreadCallback() const final { return mAfThreadCallback.get(); }
Andy Hung87c693c2023-07-06 20:56:16 -070048
Andy Hung583043b2023-07-17 17:05:00 -070049 ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
Andy Hungcf10d742020-04-28 15:38:24 -070050 type_t type, bool systemReady, bool isOut);
Andy Hung440901d2023-06-29 21:19:25 -070051 ~ThreadBase() override;
Eric Laurent81784c32012-11-19 14:55:58 -080052
Andy Hung440901d2023-06-29 21:19:25 -070053 status_t readyToRun() final;
54 void clearPowerManager() final;
Eric Laurent81784c32012-11-19 14:55:58 -080055
56 // base for record and playback
57 enum {
58 CFG_EVENT_IO,
Eric Laurent10351942014-05-08 18:49:52 -070059 CFG_EVENT_PRIO,
60 CFG_EVENT_SET_PARAMETER,
Eric Laurent1c333e22014-05-20 10:48:17 -070061 CFG_EVENT_CREATE_AUDIO_PATCH,
62 CFG_EVENT_RELEASE_AUDIO_PATCH,
jiabinc52b1ff2019-10-31 17:20:42 -070063 CFG_EVENT_UPDATE_OUT_DEVICE,
Eric Laurentb3f315a2021-07-13 15:09:05 +020064 CFG_EVENT_RESIZE_BUFFER,
Eric Laurent68a40a82022-05-03 18:15:04 +020065 CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS,
66 CFG_EVENT_HAL_LATENCY_MODES_CHANGED,
Eric Laurent81784c32012-11-19 14:55:58 -080067 };
68
Eric Laurent10351942014-05-08 18:49:52 -070069 class ConfigEventData: public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080070 public:
Eric Laurent10351942014-05-08 18:49:52 -070071 virtual ~ConfigEventData() {}
Eric Laurent81784c32012-11-19 14:55:58 -080072
73 virtual void dump(char *buffer, size_t size) = 0;
Eric Laurent10351942014-05-08 18:49:52 -070074 protected:
75 ConfigEventData() {}
Eric Laurent81784c32012-11-19 14:55:58 -080076 };
77
Eric Laurent10351942014-05-08 18:49:52 -070078 // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
79 // 1. create SetParameterConfigEvent. This sets mWaitStatus in config event
Andy Hungc5007f82023-08-29 14:26:09 -070080 // 2. Lock mutex()
Eric Laurent10351942014-05-08 18:49:52 -070081 // 3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
82 // 4. sendConfigEvent_l() reads status from event->mStatus;
83 // 5. sendConfigEvent_l() returns status
84 // 6. Unlock
85 //
86 // Parameter sequence by server: threadLoop calling processConfigEvents_l():
Andy Hungc5007f82023-08-29 14:26:09 -070087 // 1. Lock mutex()
Eric Laurent10351942014-05-08 18:49:52 -070088 // 2. If there is an entry in mConfigEvents proceed ...
89 // 3. Read first entry in mConfigEvents
90 // 4. Remove first entry from mConfigEvents
91 // 5. Process
92 // 6. Set event->mStatus
Andy Hungc5007f82023-08-29 14:26:09 -070093 // 7. event->mCondition.notify_one()
Eric Laurent10351942014-05-08 18:49:52 -070094 // 8. Unlock
Eric Laurent81784c32012-11-19 14:55:58 -080095
Eric Laurent10351942014-05-08 18:49:52 -070096 class ConfigEvent: public RefBase {
97 public:
Eric Laurentb3f315a2021-07-13 15:09:05 +020098 void dump(char *buffer, size_t size) {
99 snprintf(buffer, size, "Event type: %d\n", mType);
100 if (mData != nullptr) {
101 snprintf(buffer, size, "Data:\n");
102 mData->dump(buffer, size);
103 }
104 }
Eric Laurent10351942014-05-08 18:49:52 -0700105
Andy Hungc5007f82023-08-29 14:26:09 -0700106 audio_utils::mutex& mutex() const { return mMutex; }
Eric Laurent10351942014-05-08 18:49:52 -0700107 const int mType; // event type e.g. CFG_EVENT_IO
Andy Hungc5007f82023-08-29 14:26:09 -0700108 mutable audio_utils::mutex mMutex; // mutex associated with mCondition
109 audio_utils::condition_variable mCondition; // condition for status return
Eric Laurent10351942014-05-08 18:49:52 -0700110 status_t mStatus; // status communicated to sender
111 bool mWaitStatus; // true if sender is waiting for status
Eric Laurent72e3f392015-05-20 14:43:50 -0700112 bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
Eric Laurent10351942014-05-08 18:49:52 -0700113 sp<ConfigEventData> mData; // event specific parameter data
114
115 protected:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700116 explicit ConfigEvent(int type, bool requiresSystemReady = false) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700117 mType(type), mStatus(NO_ERROR), mWaitStatus(false),
118 mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
Eric Laurent10351942014-05-08 18:49:52 -0700119 };
120
121 class IoConfigEventData : public ConfigEventData {
122 public:
Mikhail Naganov88536df2021-07-26 17:30:29 -0700123 IoConfigEventData(audio_io_config_event_t event, pid_t pid,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700124 audio_port_handle_t portId) :
125 mEvent(event), mPid(pid), mPortId(portId) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800126
127 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200128 snprintf(buffer, size, "- IO event: event %d\n", mEvent);
Eric Laurent81784c32012-11-19 14:55:58 -0800129 }
130
Mikhail Naganov88536df2021-07-26 17:30:29 -0700131 const audio_io_config_event_t mEvent;
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700132 const pid_t mPid;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700133 const audio_port_handle_t mPortId;
Eric Laurent81784c32012-11-19 14:55:58 -0800134 };
135
Eric Laurent10351942014-05-08 18:49:52 -0700136 class IoConfigEvent : public ConfigEvent {
Eric Laurent81784c32012-11-19 14:55:58 -0800137 public:
Mikhail Naganov88536df2021-07-26 17:30:29 -0700138 IoConfigEvent(audio_io_config_event_t event, pid_t pid, audio_port_handle_t portId) :
Eric Laurent10351942014-05-08 18:49:52 -0700139 ConfigEvent(CFG_EVENT_IO) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700140 mData = new IoConfigEventData(event, pid, portId);
Eric Laurent10351942014-05-08 18:49:52 -0700141 }
Eric Laurent10351942014-05-08 18:49:52 -0700142 };
Eric Laurent81784c32012-11-19 14:55:58 -0800143
Eric Laurent10351942014-05-08 18:49:52 -0700144 class PrioConfigEventData : public ConfigEventData {
145 public:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800146 PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
147 mPid(pid), mTid(tid), mPrio(prio), mForApp(forApp) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800148
149 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200150 snprintf(buffer, size, "- Prio event: pid %d, tid %d, prio %d, for app? %d\n",
Mikhail Naganov83f04272017-02-07 10:45:09 -0800151 mPid, mTid, mPrio, mForApp);
Eric Laurent81784c32012-11-19 14:55:58 -0800152 }
153
Eric Laurent81784c32012-11-19 14:55:58 -0800154 const pid_t mPid;
155 const pid_t mTid;
156 const int32_t mPrio;
Mikhail Naganov83f04272017-02-07 10:45:09 -0800157 const bool mForApp;
Eric Laurent81784c32012-11-19 14:55:58 -0800158 };
159
Eric Laurent10351942014-05-08 18:49:52 -0700160 class PrioConfigEvent : public ConfigEvent {
161 public:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800162 PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700163 ConfigEvent(CFG_EVENT_PRIO, true) {
Mikhail Naganov83f04272017-02-07 10:45:09 -0800164 mData = new PrioConfigEventData(pid, tid, prio, forApp);
Eric Laurent10351942014-05-08 18:49:52 -0700165 }
Eric Laurent10351942014-05-08 18:49:52 -0700166 };
167
168 class SetParameterConfigEventData : public ConfigEventData {
169 public:
Andy Hung920f6572022-10-06 12:09:49 -0700170 explicit SetParameterConfigEventData(const String8& keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700171 mKeyValuePairs(keyValuePairs) {}
172
173 virtual void dump(char *buffer, size_t size) {
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000174 snprintf(buffer, size, "- KeyValue: %s\n", mKeyValuePairs.c_str());
Eric Laurent10351942014-05-08 18:49:52 -0700175 }
176
177 const String8 mKeyValuePairs;
178 };
179
180 class SetParameterConfigEvent : public ConfigEvent {
181 public:
Andy Hung920f6572022-10-06 12:09:49 -0700182 explicit SetParameterConfigEvent(const String8& keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700183 ConfigEvent(CFG_EVENT_SET_PARAMETER) {
184 mData = new SetParameterConfigEventData(keyValuePairs);
185 mWaitStatus = true;
186 }
Eric Laurent10351942014-05-08 18:49:52 -0700187 };
188
Eric Laurent1c333e22014-05-20 10:48:17 -0700189 class CreateAudioPatchConfigEventData : public ConfigEventData {
190 public:
191 CreateAudioPatchConfigEventData(const struct audio_patch patch,
192 audio_patch_handle_t handle) :
193 mPatch(patch), mHandle(handle) {}
194
195 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200196 snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -0700197 }
198
199 const struct audio_patch mPatch;
200 audio_patch_handle_t mHandle;
201 };
202
203 class CreateAudioPatchConfigEvent : public ConfigEvent {
204 public:
205 CreateAudioPatchConfigEvent(const struct audio_patch patch,
206 audio_patch_handle_t handle) :
207 ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
208 mData = new CreateAudioPatchConfigEventData(patch, handle);
209 mWaitStatus = true;
210 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700211 };
212
213 class ReleaseAudioPatchConfigEventData : public ConfigEventData {
214 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700215 explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700216 mHandle(handle) {}
217
218 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200219 snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -0700220 }
221
222 audio_patch_handle_t mHandle;
223 };
224
225 class ReleaseAudioPatchConfigEvent : public ConfigEvent {
226 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700227 explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700228 ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
229 mData = new ReleaseAudioPatchConfigEventData(handle);
230 mWaitStatus = true;
231 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700232 };
Eric Laurent81784c32012-11-19 14:55:58 -0800233
jiabinc52b1ff2019-10-31 17:20:42 -0700234 class UpdateOutDevicesConfigEventData : public ConfigEventData {
235 public:
236 explicit UpdateOutDevicesConfigEventData(const DeviceDescriptorBaseVector& outDevices) :
237 mOutDevices(outDevices) {}
238
239 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200240 snprintf(buffer, size, "- Devices: %s", android::toString(mOutDevices).c_str());
jiabinc52b1ff2019-10-31 17:20:42 -0700241 }
242
243 DeviceDescriptorBaseVector mOutDevices;
244 };
245
246 class UpdateOutDevicesConfigEvent : public ConfigEvent {
247 public:
248 explicit UpdateOutDevicesConfigEvent(const DeviceDescriptorBaseVector& outDevices) :
249 ConfigEvent(CFG_EVENT_UPDATE_OUT_DEVICE) {
250 mData = new UpdateOutDevicesConfigEventData(outDevices);
251 }
jiabinc52b1ff2019-10-31 17:20:42 -0700252 };
253
Eric Laurentec376dc2021-04-08 20:41:22 +0200254 class ResizeBufferConfigEventData : public ConfigEventData {
255 public:
256 explicit ResizeBufferConfigEventData(int32_t maxSharedAudioHistoryMs) :
257 mMaxSharedAudioHistoryMs(maxSharedAudioHistoryMs) {}
258
259 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200260 snprintf(buffer, size, "- mMaxSharedAudioHistoryMs: %d", mMaxSharedAudioHistoryMs);
Eric Laurentec376dc2021-04-08 20:41:22 +0200261 }
262
263 int32_t mMaxSharedAudioHistoryMs;
264 };
265
266 class ResizeBufferConfigEvent : public ConfigEvent {
267 public:
268 explicit ResizeBufferConfigEvent(int32_t maxSharedAudioHistoryMs) :
269 ConfigEvent(CFG_EVENT_RESIZE_BUFFER) {
270 mData = new ResizeBufferConfigEventData(maxSharedAudioHistoryMs);
271 }
Eric Laurentec376dc2021-04-08 20:41:22 +0200272 };
273
Eric Laurentb3f315a2021-07-13 15:09:05 +0200274 class CheckOutputStageEffectsEvent : public ConfigEvent {
275 public:
276 CheckOutputStageEffectsEvent() :
277 ConfigEvent(CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS) {
278 }
Eric Laurentb3f315a2021-07-13 15:09:05 +0200279 };
280
Eric Laurent68a40a82022-05-03 18:15:04 +0200281 class HalLatencyModesChangedEvent : public ConfigEvent {
282 public:
283 HalLatencyModesChangedEvent() :
284 ConfigEvent(CFG_EVENT_HAL_LATENCY_MODES_CHANGED) {
285 }
Eric Laurent68a40a82022-05-03 18:15:04 +0200286 };
287
Eric Laurentb3f315a2021-07-13 15:09:05 +0200288
Eric Laurent81784c32012-11-19 14:55:58 -0800289 class PMDeathRecipient : public IBinder::DeathRecipient {
290 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700291 explicit PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800292 virtual ~PMDeathRecipient() {}
293
294 // IBinder::DeathRecipient
295 virtual void binderDied(const wp<IBinder>& who);
296
297 private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700298 DISALLOW_COPY_AND_ASSIGN(PMDeathRecipient);
Eric Laurent81784c32012-11-19 14:55:58 -0800299
300 wp<ThreadBase> mThread;
301 };
302
Andy Hung440901d2023-06-29 21:19:25 -0700303 type_t type() const final { return mType; }
304 bool isDuplicating() const final { return (mType == DUPLICATING); }
305 audio_io_handle_t id() const final { return mId;}
Eric Laurent81784c32012-11-19 14:55:58 -0800306
Andy Hung440901d2023-06-29 21:19:25 -0700307 uint32_t sampleRate() const final { return mSampleRate; }
308 audio_channel_mask_t channelMask() const final { return mChannelMask; }
309 audio_channel_mask_t mixerChannelMask() const override { return mChannelMask; }
310 audio_format_t format() const final { return mHALFormat; }
311 uint32_t channelCount() const final { return mChannelCount; }
312 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Andy Hung87c693c2023-07-06 20:56:16 -0700313 uint32_t hapticChannelCount() const override { return 0; }
Andy Hung440901d2023-06-29 21:19:25 -0700314 uint32_t latency_l() const override { return 0; }
315 void setVolumeForOutput_l(float /* left */, float /* right */) const override {}
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700316
317 // Return's the HAL's frame count i.e. fast mixer buffer size.
Andy Hung440901d2023-06-29 21:19:25 -0700318 size_t frameCountHAL() const final { return mFrameCount; }
319 size_t frameSize() const final { return mFrameSize; }
Eric Laurent81784c32012-11-19 14:55:58 -0800320
321 // Should be "virtual status_t requestExitAndWait()" and override same
322 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
Andy Hung440901d2023-06-29 21:19:25 -0700323 void exit() final;
324 status_t setParameters(const String8& keyValuePairs) final;
325
Andy Hungc5007f82023-08-29 14:26:09 -0700326 // sendConfigEvent_l() must be called with ThreadBase::mutex() held
Eric Laurent10351942014-05-08 18:49:52 -0700327 // Can temporarily release the lock if waiting for a reply from
328 // processConfigEvents_l().
Andy Hung440901d2023-06-29 21:19:25 -0700329 status_t sendConfigEvent_l(sp<ConfigEvent>& event);
330 void sendIoConfigEvent(audio_io_config_event_t event, pid_t pid = 0,
331 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
332 void sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid = 0,
333 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
334 void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) final;
335 void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp) final;
336 status_t sendSetParameterConfigEvent_l(const String8& keyValuePair) final;
337 status_t sendCreateAudioPatchConfigEvent(const struct audio_patch* patch,
338 audio_patch_handle_t* handle) final;
339 status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle) final;
340 status_t sendUpdateOutDeviceConfigEvent(
341 const DeviceDescriptorBaseVector& outDevices) final;
342 void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) final;
343 void sendCheckOutputStageEffectsEvent() final;
344 void sendCheckOutputStageEffectsEvent_l() final;
345 void sendHalLatencyModesChangedEvent_l() final;
Eric Laurentb3f315a2021-07-13 15:09:05 +0200346
Andy Hung440901d2023-06-29 21:19:25 -0700347 void processConfigEvents_l() final;
348 void setCheckOutputStageEffects() override {}
349 void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override;
350 void toAudioPortConfig(struct audio_port_config* config) override;
351 void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override;
Eric Laurent1c333e22014-05-20 10:48:17 -0700352
Andy Hung440901d2023-06-29 21:19:25 -0700353 // see note at declaration of mStandby, mOutDevice and mInDevice
354 bool inStandby() const override { return mStandby; }
355 const DeviceTypeSet outDeviceTypes() const final {
356 return getAudioDeviceTypes(mOutDeviceTypeAddrs);
357 }
358 audio_devices_t inDeviceType() const final { return mInDeviceTypeAddr.mType; }
359 DeviceTypeSet getDeviceTypes() const final {
360 return isOutput() ? outDeviceTypes() : DeviceTypeSet({inDeviceType()});
361 }
Eric Laurent81784c32012-11-19 14:55:58 -0800362
Andy Hung440901d2023-06-29 21:19:25 -0700363 const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const final {
364 return mOutDeviceTypeAddrs;
365 }
366 const AudioDeviceTypeAddr& inDeviceTypeAddr() const final {
367 return mInDeviceTypeAddr;
368 }
Andy Hung293558a2017-03-21 12:19:20 -0700369
Andy Hung440901d2023-06-29 21:19:25 -0700370 bool isOutput() const final { return mIsOut; }
jiabin8f278ee2019-11-11 12:16:27 -0800371
Andy Hung440901d2023-06-29 21:19:25 -0700372 bool isOffloadOrMmap() const final {
373 switch (mType) {
374 case OFFLOAD:
375 case MMAP_PLAYBACK:
376 case MMAP_CAPTURE:
377 return true;
378 default:
379 return false;
380 }
381 }
Eric Laurent81784c32012-11-19 14:55:58 -0800382
Andy Hung440901d2023-06-29 21:19:25 -0700383 sp<IAfEffectHandle> createEffect_l(
Andy Hung88035ac2023-06-27 17:05:02 -0700384 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700385 const sp<media::IEffectClient>& effectClient,
Eric Laurent81784c32012-11-19 14:55:58 -0800386 int32_t priority,
Glenn Kastend848eb42016-03-08 13:42:11 -0800387 audio_session_t sessionId,
Eric Laurent81784c32012-11-19 14:55:58 -0800388 effect_descriptor_t *desc,
389 int *enabled,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800390 status_t *status /*non-NULL*/,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700391 bool pinned,
Eric Laurentde8caf42021-08-11 17:19:25 +0200392 bool probe,
Andy Hung972bec12023-08-31 16:13:39 -0700393 bool notifyFramesProcessed) final
394 REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800395
396 // return values for hasAudioSession (bit field)
397 enum effect_state {
398 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
399 // effect
Eric Laurent4c415062016-06-17 16:14:16 -0700400 TRACK_SESSION = 0x2, // the audio session corresponds to at least one
Eric Laurent81784c32012-11-19 14:55:58 -0800401 // track
Eric Laurentb62d0362021-10-26 17:40:18 +0200402 FAST_SESSION = 0x4, // the audio session corresponds to at least one
Eric Laurent4c415062016-06-17 16:14:16 -0700403 // fast track
jiabinc658e452022-10-21 20:52:21 +0000404 SPATIALIZED_SESSION = 0x8, // the audio session corresponds to at least one
405 // spatialized track
406 BIT_PERFECT_SESSION = 0x10 // the audio session corresponds to at least one
407 // bit-perfect track
Eric Laurent81784c32012-11-19 14:55:58 -0800408 };
409
Andy Hung440901d2023-06-29 21:19:25 -0700410 // get effect chain corresponding to session Id.
411 sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const final;
412 // same as getEffectChain() but must be called with ThreadBase mutex locked
413 sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const final;
414 std::vector<int> getEffectIds_l(audio_session_t sessionId) const final;
415
Eric Laurent81784c32012-11-19 14:55:58 -0800416 // lock all effect chains Mutexes. Must be called before releasing the
417 // ThreadBase mutex before processing the mixer and effects. This guarantees the
418 // integrity of the chains during the process.
419 // Also sets the parameter 'effectChains' to current value of mEffectChains.
Andy Hung440901d2023-06-29 21:19:25 -0700420 void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800421 // unlock effect chains after process
Andy Hung440901d2023-06-29 21:19:25 -0700422 void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) final;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800423 // get a copy of mEffectChains vector
Andy Hung440901d2023-06-29 21:19:25 -0700424 Vector<sp<IAfEffectChain>> getEffectChains_l() const final { return mEffectChains; };
Eric Laurent81784c32012-11-19 14:55:58 -0800425 // set audio mode to all effect chains
Andy Hung440901d2023-06-29 21:19:25 -0700426 void setMode(audio_mode_t mode) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800427 // get effect module with corresponding ID on specified audio session
Andy Hung440901d2023-06-29 21:19:25 -0700428 sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const final;
429 sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800430 // add and effect module. Also creates the effect chain is none exists for
Eric Laurent6c796322019-04-09 14:13:17 -0700431 // the effects audio session. Only called in a context of moving an effect
432 // from one thread to another
Andy Hung972bec12023-08-31 16:13:39 -0700433 status_t addEffect_ll(const sp<IAfEffectModule>& effect) final
434 REQUIRES(audio_utils::AudioFlinger_Mutex, mutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800435 // remove and effect module. Also removes the effect chain is this was the last
436 // effect
Andy Hung440901d2023-06-29 21:19:25 -0700437 void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false) final;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800438 // disconnect an effect handle from module and destroy module if last handle
Andy Hung440901d2023-06-29 21:19:25 -0700439 void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800440 // detach all tracks connected to an auxiliary effect
Andy Hung440901d2023-06-29 21:19:25 -0700441 void detachAuxEffect_l(int /* effectId */) override {}
Andy Hung99b1ba62023-07-14 11:00:08 -0700442 // TODO(b/291317898) - remove hasAudioSession_l below.
Andy Hung440901d2023-06-29 21:19:25 -0700443 uint32_t hasAudioSession_l(audio_session_t sessionId) const override = 0;
444 uint32_t hasAudioSession(audio_session_t sessionId) const final {
Andy Hungc5007f82023-08-29 14:26:09 -0700445 std::lock_guard _l(mutex());
Eric Laurent4c415062016-06-17 16:14:16 -0700446 return hasAudioSession_l(sessionId);
447 }
448
Andy Hungc3d62f92019-03-14 13:38:51 -0700449 template <typename T>
450 uint32_t hasAudioSession_l(audio_session_t sessionId, const T& tracks) const {
451 uint32_t result = 0;
452 if (getEffectChain_l(sessionId) != 0) {
453 result = EFFECT_SESSION;
454 }
455 for (size_t i = 0; i < tracks.size(); ++i) {
Andy Hung8d31fd22023-06-26 19:20:57 -0700456 const sp<IAfTrackBase>& track = tracks[i];
Andy Hungc3d62f92019-03-14 13:38:51 -0700457 if (sessionId == track->sessionId()
458 && !track->isInvalid() // not yet removed from tracks.
459 && !track->isTerminated()) {
460 result |= TRACK_SESSION;
461 if (track->isFastTrack()) {
462 result |= FAST_SESSION; // caution, only represents first track.
463 }
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200464 if (track->isSpatialized()) {
Eric Laurentb62d0362021-10-26 17:40:18 +0200465 result |= SPATIALIZED_SESSION; // caution, only first track.
466 }
jiabinc658e452022-10-21 20:52:21 +0000467 if (track->isBitPerfect()) {
468 result |= BIT_PERFECT_SESSION;
469 }
Andy Hungc3d62f92019-03-14 13:38:51 -0700470 break;
471 }
472 }
473 return result;
474 }
475
Eric Laurent81784c32012-11-19 14:55:58 -0800476 // the value returned by default implementation is not important as the
477 // strategy is only meaningful for PlaybackThread which implements this method
Andy Hung440901d2023-06-29 21:19:25 -0700478 product_strategy_t getStrategyForSession_l(
479 audio_session_t /* sessionId */) const override {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800480 return static_cast<product_strategy_t>(0);
481 }
Eric Laurent81784c32012-11-19 14:55:58 -0800482
Eric Laurent81784c32012-11-19 14:55:58 -0800483 // check if some effects must be suspended/restored when an effect is enabled
484 // or disabled
Andy Hung440901d2023-06-29 21:19:25 -0700485 void checkSuspendOnEffectEnabled(bool enabled,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800486 audio_session_t sessionId,
Andy Hung440901d2023-06-29 21:19:25 -0700487 bool threadLocked) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800488
Eric Laurent81784c32012-11-19 14:55:58 -0800489
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700490 // Return a reference to a per-thread heap which can be used to allocate IMemory
491 // objects that will be read-only to client processes, read/write to mediaserver,
492 // and shared by all client processes of the thread.
493 // The heap is per-thread rather than common across all threads, because
494 // clients can't be trusted not to modify the offset of the IMemory they receive.
495 // If a thread does not have such a heap, this method returns 0.
Andy Hung440901d2023-06-29 21:19:25 -0700496 sp<MemoryDealer> readOnlyHeap() const override { return nullptr; }
Eric Laurent81784c32012-11-19 14:55:58 -0800497
Andy Hung440901d2023-06-29 21:19:25 -0700498 sp<IMemory> pipeMemory() const override { return nullptr; }
Glenn Kasten6181ffd2014-05-13 10:41:52 -0700499
Andy Hung440901d2023-06-29 21:19:25 -0700500 void systemReady() final;
Eric Laurent72e3f392015-05-20 14:43:50 -0700501
Andy Hung440901d2023-06-29 21:19:25 -0700502 void broadcast_l() final;
Eric Laurent4c415062016-06-17 16:14:16 -0700503
Andy Hung440901d2023-06-29 21:19:25 -0700504 bool isTimestampCorrectionEnabled() const override { return false; }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800505
Andy Hung440901d2023-06-29 21:19:25 -0700506 bool isMsdDevice() const final { return mIsMsdDevice; }
Andy Hungc8fddf32018-08-08 18:32:37 -0700507
Andy Hung440901d2023-06-29 21:19:25 -0700508 void dump(int fd, const Vector<String16>& args) override;
Andy Hungdc099c22018-09-18 13:46:39 -0700509
Andy Hungd0979812019-02-21 15:51:44 -0800510 // deliver stats to mediametrics.
Andy Hung440901d2023-06-29 21:19:25 -0700511 void sendStatistics(bool force) final;
Andy Hungd0979812019-02-21 15:51:44 -0800512
Andy Hung972bec12023-08-31 16:13:39 -0700513 audio_utils::mutex& mutex() const final RETURN_CAPABILITY(audio_utils::ThreadBase_Mutex) {
Andy Hungc5007f82023-08-29 14:26:09 -0700514 return mMutex;
Andy Hung440901d2023-06-29 21:19:25 -0700515 }
Andy Hungc5007f82023-08-29 14:26:09 -0700516 mutable audio_utils::mutex mMutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800517
Andy Hung440901d2023-06-29 21:19:25 -0700518 void onEffectEnable(const sp<IAfEffectModule>& effect) final;
519 void onEffectDisable() final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800520
Andy Hungc5007f82023-08-29 14:26:09 -0700521 // invalidateTracksForAudioSession_l must be called with holding mutex().
Andy Hung440901d2023-06-29 21:19:25 -0700522 void invalidateTracksForAudioSession_l(audio_session_t /* sessionId */) const override {}
jiabineb3bda02020-06-30 14:07:03 -0700523 // Invalidate all the tracks with the given audio session.
Andy Hung440901d2023-06-29 21:19:25 -0700524 void invalidateTracksForAudioSession(audio_session_t sessionId) const final {
Andy Hungc5007f82023-08-29 14:26:09 -0700525 std::lock_guard _l(mutex());
jiabineb3bda02020-06-30 14:07:03 -0700526 invalidateTracksForAudioSession_l(sessionId);
527 }
528
529 template <typename T>
530 void invalidateTracksForAudioSession_l(audio_session_t sessionId,
531 const T& tracks) const {
532 for (size_t i = 0; i < tracks.size(); ++i) {
Andy Hung8d31fd22023-06-26 19:20:57 -0700533 const sp<IAfTrackBase>& track = tracks[i];
jiabineb3bda02020-06-30 14:07:03 -0700534 if (sessionId == track->sessionId()) {
535 track->invalidate();
536 }
537 }
538 }
539
Andy Hung972bec12023-08-31 16:13:39 -0700540 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override
541 REQUIRES(audio_utils::AudioFlinger_Mutex);
542 void stopMelComputation_l() override
543 REQUIRES(audio_utils::AudioFlinger_Mutex);
Vlad Popa6fbbfbf2023-02-22 15:05:43 +0100544
Eric Laurent81784c32012-11-19 14:55:58 -0800545protected:
546
547 // entry describing an effect being suspended in mSuspendedSessions keyed vector
548 class SuspendedSessionDesc : public RefBase {
549 public:
550 SuspendedSessionDesc() : mRefCount(0) {}
551
552 int mRefCount; // number of active suspend requests
553 effect_uuid_t mType; // effect type UUID
554 };
555
Andy Hungdae27702016-10-31 14:01:16 -0700556 void acquireWakeLock();
557 virtual void acquireWakeLock_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800558 void releaseWakeLock();
559 void releaseWakeLock_l();
Andy Hungd01b0f12016-11-07 16:10:30 -0800560 void updateWakeLockUids_l(const SortedVector<uid_t> &uids);
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800561 void getPowerManager_l();
Eric Laurentd8365c52017-07-16 15:27:05 -0700562 // suspend or restore effects of the specified type (or all if type is NULL)
563 // on a given session. The number of suspend requests is counted and restore
564 // occurs when all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800565 void setEffectSuspended_l(const effect_uuid_t *type,
566 bool suspend,
Andy Hung87c693c2023-07-06 20:56:16 -0700567 audio_session_t sessionId) final;
Eric Laurentd8365c52017-07-16 15:27:05 -0700568 // updated mSuspendedSessions when an effect is suspended or restored
Eric Laurent81784c32012-11-19 14:55:58 -0800569 void updateSuspendedSessions_l(const effect_uuid_t *type,
570 bool suspend,
Glenn Kastend848eb42016-03-08 13:42:11 -0800571 audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800572 // check if some effects must be suspended when an effect chain is added
Andy Hung116bc262023-06-20 18:56:17 -0700573 void checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain);
Eric Laurent81784c32012-11-19 14:55:58 -0800574
Kevin Rocard069c2712018-03-29 19:09:14 -0700575 // sends the metadata of the active tracks to the HAL
Vlad Popa7e81cea2023-01-19 16:34:16 +0100576 struct MetadataUpdate {
577 std::vector<playback_track_metadata_v7_t> playbackMetadataUpdate;
578 std::vector<record_track_metadata_v7_t> recordMetadataUpdate;
579 };
580 virtual MetadataUpdate updateMetadata_l() = 0;
Kevin Rocard069c2712018-03-29 19:09:14 -0700581
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100582 String16 getWakeLockTag();
583
Eric Laurent81784c32012-11-19 14:55:58 -0800584 virtual void preExit() { }
Andy Hung2ddee192015-12-18 17:34:44 -0800585 virtual void setMasterMono_l(bool mono __unused) { }
586 virtual bool requireMonoBlend() { return false; }
Eric Laurent81784c32012-11-19 14:55:58 -0800587
Andy Hung1c86ebe2018-05-29 20:29:08 -0700588 // called within the threadLoop to obtain timestamp from the HAL.
589 virtual status_t threadloop_getHalTimestamp_l(
590 ExtendedTimestamp *timestamp __unused) const {
591 return INVALID_OPERATION;
592 }
Andy Hung116bc262023-06-20 18:56:17 -0700593public:
Andy Hung99b1ba62023-07-14 11:00:08 -0700594// TODO(b/291317898) organize with publics
Eric Laurentd66d7a12021-07-13 13:35:32 +0200595 product_strategy_t getStrategyForStream(audio_stream_type_t stream) const;
Andy Hung116bc262023-06-20 18:56:17 -0700596protected:
Eric Laurentd66d7a12021-07-13 13:35:32 +0200597
Eric Laurentb0463942022-12-20 16:31:10 +0100598 virtual void onHalLatencyModesChanged_l() {}
599
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700600 virtual void dumpInternals_l(int fd __unused, const Vector<String16>& args __unused)
601 { }
602 virtual void dumpTracks_l(int fd __unused, const Vector<String16>& args __unused) { }
603
Eric Laurent81784c32012-11-19 14:55:58 -0800604 const type_t mType;
605
606 // Used by parameters, config events, addTrack_l, exit
Andy Hungc5007f82023-08-29 14:26:09 -0700607 audio_utils::condition_variable mWaitWorkCV;
Eric Laurent81784c32012-11-19 14:55:58 -0800608
Andy Hung583043b2023-07-17 17:05:00 -0700609 const sp<IAfThreadCallback> mAfThreadCallback;
Andy Hungcf10d742020-04-28 15:38:24 -0700610 ThreadMetrics mThreadMetrics;
611 const bool mIsOut;
Glenn Kasten9b58f632013-07-16 11:37:48 -0700612
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800613 // updated by PlaybackThread::readOutputParameters_l() or
614 // RecordThread::readInputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -0800615 uint32_t mSampleRate;
616 size_t mFrameCount; // output HAL, direct output, record
Eric Laurent81784c32012-11-19 14:55:58 -0800617 audio_channel_mask_t mChannelMask;
Glenn Kastenf6ed4232013-07-16 11:16:27 -0700618 uint32_t mChannelCount;
Eric Laurent81784c32012-11-19 14:55:58 -0800619 size_t mFrameSize;
Glenn Kasten97b7b752014-09-28 13:04:24 -0700620 // not HAL frame size, this is for output sink (to pipe to fast mixer)
Andy Hung463be252014-07-10 16:56:07 -0700621 audio_format_t mFormat; // Source format for Recording and
622 // Sink format for Playback.
623 // Sink format may be different than
624 // HAL format if Fastmixer is used.
625 audio_format_t mHALFormat;
Glenn Kasten70949c42013-08-06 07:40:12 -0700626 size_t mBufferSize; // HAL buffer size for read() or write()
jiabinc52b1ff2019-10-31 17:20:42 -0700627 AudioDeviceTypeAddrVector mOutDeviceTypeAddrs; // output device types and addresses
628 AudioDeviceTypeAddr mInDeviceTypeAddr; // input device type and address
Eric Laurent10351942014-05-08 18:49:52 -0700629 Vector< sp<ConfigEvent> > mConfigEvents;
Eric Laurent72e3f392015-05-20 14:43:50 -0700630 Vector< sp<ConfigEvent> > mPendingConfigEvents; // events awaiting system ready
Eric Laurent81784c32012-11-19 14:55:58 -0800631
632 // These fields are written and read by thread itself without lock or barrier,
jiabinc52b1ff2019-10-31 17:20:42 -0700633 // and read by other threads without lock or barrier via standby(), outDeviceTypes()
634 // and inDeviceType().
Eric Laurent81784c32012-11-19 14:55:58 -0800635 // Because of the absence of a lock or barrier, any other thread that reads
636 // these fields must use the information in isolation, or be prepared to deal
637 // with possibility that it might be inconsistent with other information.
Glenn Kasten4944acb2013-08-19 08:39:20 -0700638 bool mStandby; // Whether thread is currently in standby.
jiabinc52b1ff2019-10-31 17:20:42 -0700639
Eric Laurent296fb132015-05-01 11:38:42 -0700640 struct audio_patch mPatch;
jiabinc52b1ff2019-10-31 17:20:42 -0700641
Glenn Kastenf59497b2015-01-26 16:35:47 -0800642 audio_source_t mAudioSource;
Eric Laurent81784c32012-11-19 14:55:58 -0800643
644 const audio_io_handle_t mId;
Andy Hung116bc262023-06-20 18:56:17 -0700645 Vector<sp<IAfEffectChain>> mEffectChains;
Eric Laurent81784c32012-11-19 14:55:58 -0800646
Glenn Kastend7dca052015-03-05 16:05:54 -0800647 static const int kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
648 char mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
Chris Ye6597d732020-02-28 22:38:25 -0800649 sp<os::IPowerManager> mPowerManager;
Eric Laurent81784c32012-11-19 14:55:58 -0800650 sp<IBinder> mWakeLockToken;
651 const sp<PMDeathRecipient> mDeathRecipient;
Glenn Kastend848eb42016-03-08 13:42:11 -0800652 // list of suspended effects per session and per type. The first (outer) vector is
653 // keyed by session ID, the second (inner) by type UUID timeLow field
Eric Laurentd8365c52017-07-16 15:27:05 -0700654 // Updated by updateSuspendedSessions_l() only.
Glenn Kastend848eb42016-03-08 13:42:11 -0800655 KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
Eric Laurent81784c32012-11-19 14:55:58 -0800656 mSuspendedSessions;
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700657 // TODO: add comment and adjust size as needed
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800658 static const size_t kLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800659 sp<NBLog::Writer> mNBLogWriter;
Eric Laurent72e3f392015-05-20 14:43:50 -0700660 bool mSystemReady;
Andy Hung818e7a32016-02-16 18:08:07 -0800661 ExtendedTimestamp mTimestamp;
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700662 TimestampVerifier< // For timestamp statistics.
663 int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier;
Dean Wheatley12473e92021-03-18 23:00:55 +1100664 // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
665 // TODO: add confirmation checks:
666 // 1) DIRECT threads and linear PCM format really resets to 0?
667 // 2) Is frame count really valid if not linear pcm?
668 // 3) Are all 64 bits of position returned, not just lowest 32 bits?
jiabinc52b1ff2019-10-31 17:20:42 -0700669 // Timestamp corrected device should be a single device.
670 audio_devices_t mTimestampCorrectedDevice = AUDIO_DEVICE_NONE;
Andy Hung446f4df2019-02-21 12:26:41 -0800671
672 // ThreadLoop statistics per iteration.
673 int64_t mLastIoBeginNs = -1;
674 int64_t mLastIoEndNs = -1;
675
Andy Hung44d648b2022-04-08 17:33:40 -0700676 // ThreadSnapshot is thread-safe (internally locked)
677 mediautils::ThreadSnapshot mThreadSnapshot;
678
Andy Hung446f4df2019-02-21 12:26:41 -0800679 // This should be read under ThreadBase lock (if not on the threadLoop thread).
680 audio_utils::Statistics<double> mIoJitterMs{0.995 /* alpha */};
681 audio_utils::Statistics<double> mProcessTimeMs{0.995 /* alpha */};
Andy Hunge6c37112019-02-26 17:38:10 -0800682 audio_utils::Statistics<double> mLatencyMs{0.995 /* alpha */};
Robert Wu06db0a32021-08-10 19:05:34 +0000683 audio_utils::Statistics<double> mMonopipePipeDepthStats{0.999 /* alpha */};
Andy Hung446f4df2019-02-21 12:26:41 -0800684
Andy Hungd0979812019-02-21 15:51:44 -0800685 // Save the last count when we delivered statistics to mediametrics.
686 int64_t mLastRecordedTimestampVerifierN = 0;
687 int64_t mLastRecordedTimeNs = 0; // BOOTTIME to include suspend.
688
Andy Hungc8fddf32018-08-08 18:32:37 -0700689 bool mIsMsdDevice = false;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800690 // A condition that must be evaluated by the thread loop has changed and
691 // we must not wait for async write callback in the thread loop before evaluating it
692 bool mSignalPending;
Andy Hungdae27702016-10-31 14:01:16 -0700693
Andy Hung8946a282018-04-19 20:04:56 -0700694#ifdef TEE_SINK
695 NBAIO_Tee mTee;
696#endif
Andy Hungdae27702016-10-31 14:01:16 -0700697 // ActiveTracks is a sorted vector of track type T representing the
698 // active tracks of threadLoop() to be considered by the locked prepare portion.
699 // ActiveTracks should be accessed with the ThreadBase lock held.
700 //
701 // During processing and I/O, the threadLoop does not hold the lock;
702 // hence it does not directly use ActiveTracks. Care should be taken
703 // to hold local strong references or defer removal of tracks
704 // if the threadLoop may still be accessing those tracks due to mix, etc.
705 //
706 // This class updates power information appropriately.
707 //
708
709 template <typename T>
710 class ActiveTracks {
711 public:
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700712 explicit ActiveTracks(SimpleLog *localLog = nullptr)
Andy Hungdae27702016-10-31 14:01:16 -0700713 : mActiveTracksGeneration(0)
714 , mLastActiveTracksGeneration(0)
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700715 , mLocalLog(localLog)
Andy Hungdae27702016-10-31 14:01:16 -0700716 { }
717
718 ~ActiveTracks() {
719 ALOGW_IF(!mActiveTracks.isEmpty(),
720 "ActiveTracks should be empty in destructor");
721 }
722 // returns the last track added (even though it may have been
723 // subsequently removed from ActiveTracks).
724 //
725 // Used for DirectOutputThread to ensure a flush is called when transitioning
726 // to a new track (even though it may be on the same session).
727 // Used for OffloadThread to ensure that volume and mixer state is
728 // taken from the latest track added.
729 //
730 // The latest track is saved with a weak pointer to prevent keeping an
731 // otherwise useless track alive. Thus the function will return nullptr
732 // if the latest track has subsequently been removed and destroyed.
733 sp<T> getLatest() {
734 return mLatestActiveTrack.promote();
735 }
736
737 // SortedVector methods
738 ssize_t add(const sp<T> &track);
739 ssize_t remove(const sp<T> &track);
740 size_t size() const {
741 return mActiveTracks.size();
742 }
Eric Tan39ec8d62018-07-24 09:49:29 -0700743 bool isEmpty() const {
744 return mActiveTracks.isEmpty();
745 }
Andy Hung87c693c2023-07-06 20:56:16 -0700746 ssize_t indexOf(const sp<T>& item) const {
Andy Hungdae27702016-10-31 14:01:16 -0700747 return mActiveTracks.indexOf(item);
748 }
749 sp<T> operator[](size_t index) const {
750 return mActiveTracks[index];
751 }
752 typename SortedVector<sp<T>>::iterator begin() {
753 return mActiveTracks.begin();
754 }
755 typename SortedVector<sp<T>>::iterator end() {
756 return mActiveTracks.end();
757 }
758
759 // Due to Binder recursion optimization, clear() and updatePowerState()
760 // cannot be called from a Binder thread because they may call back into
761 // the original calling process (system server) for BatteryNotifier
762 // (which requires a Java environment that may not be present).
763 // Hence, call clear() and updatePowerState() only from the
764 // ThreadBase thread.
765 void clear();
766 // periodically called in the threadLoop() to update power state uids.
Andy Hung920f6572022-10-06 12:09:49 -0700767 void updatePowerState(const sp<ThreadBase>& thread, bool force = false);
Andy Hungdae27702016-10-31 14:01:16 -0700768
Kevin Rocardc86a7f72018-04-03 09:00:09 -0700769 /** @return true if one or move active tracks was added or removed since the
Jasmine Chaeaa10e42021-05-11 10:11:14 +0800770 * last time this function was called or the vector was created.
771 * true if volume of one of active tracks was changed.
772 */
Kevin Rocard069c2712018-03-29 19:09:14 -0700773 bool readAndClearHasChanged();
774
Eric Laurentdda206a2022-07-08 17:28:35 +0200775 /** Force updating track metadata to audio HAL stream next time
776 * readAndClearHasChanged() is called.
777 */
778 void setHasChanged() { mHasChanged = true; }
779
Andy Hungdae27702016-10-31 14:01:16 -0700780 private:
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700781 void logTrack(const char *funcName, const sp<T> &track) const;
782
Andy Hungd01b0f12016-11-07 16:10:30 -0800783 SortedVector<uid_t> getWakeLockUids() {
784 SortedVector<uid_t> wakeLockUids;
Andy Hungdae27702016-10-31 14:01:16 -0700785 for (const sp<T> &track : mActiveTracks) {
786 wakeLockUids.add(track->uid());
787 }
788 return wakeLockUids; // moved by underlying SharedBuffer
789 }
790
Andy Hungdae27702016-10-31 14:01:16 -0700791 SortedVector<sp<T>> mActiveTracks;
792 int mActiveTracksGeneration;
793 int mLastActiveTracksGeneration;
794 wp<T> mLatestActiveTrack; // latest track added to ActiveTracks
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700795 SimpleLog * const mLocalLog;
Kevin Rocardc86a7f72018-04-03 09:00:09 -0700796 // If the vector has changed since last call to readAndClearHasChanged
Kevin Rocard069c2712018-03-29 19:09:14 -0700797 bool mHasChanged = false;
Andy Hungdae27702016-10-31 14:01:16 -0700798 };
Andy Hung293558a2017-03-21 12:19:20 -0700799
800 SimpleLog mLocalLog;
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700801
802private:
803 void dumpBase_l(int fd, const Vector<String16>& args);
804 void dumpEffectChains_l(int fd, const Vector<String16>& args);
Eric Laurent81784c32012-11-19 14:55:58 -0800805};
806
807// --- PlaybackThread ---
Andy Hung440901d2023-06-29 21:19:25 -0700808class PlaybackThread : public ThreadBase, public virtual IAfPlaybackThread,
809 public StreamOutHalInterfaceCallback,
Andy Hung87c693c2023-07-06 20:56:16 -0700810 public virtual VolumeInterface, public StreamOutHalInterfaceEventCallback {
Eric Laurent81784c32012-11-19 14:55:58 -0800811public:
Andy Hung87c693c2023-07-06 20:56:16 -0700812 sp<IAfPlaybackThread> asIAfPlaybackThread() final {
813 return sp<IAfPlaybackThread>::fromExisting(this);
814 }
Eric Laurent81784c32012-11-19 14:55:58 -0800815
Eric Laurente93cc032016-05-05 10:15:10 -0700816 // retry count before removing active track in case of underrun on offloaded thread:
817 // we need to make sure that AudioTrack client has enough time to send large buffers
818 //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
819 // handled for offloaded tracks
820 static const int8_t kMaxTrackRetriesOffload = 20;
821 static const int8_t kMaxTrackStartupRetriesOffload = 100;
Andy Hung8ed196a2018-01-05 13:21:11 -0800822 static constexpr uint32_t kMaxTracksPerUid = 40;
Andy Hung1bc088a2018-02-09 15:57:31 -0800823 static constexpr size_t kMaxTracks = 256;
Eric Laurente93cc032016-05-05 10:15:10 -0700824
rago1bb90822017-05-02 18:31:48 -0700825 // Maximum delay (in nanoseconds) for upcoming buffers in suspend mode, otherwise
826 // if delay is greater, the estimated time for timeLoopNextNs is reset.
827 // This allows for catch-up to be done for small delays, while resetting the estimate
828 // for initial conditions or large delays.
829 static const nsecs_t kMaxNextBufferDelayNs = 100000000;
830
Andy Hung583043b2023-07-17 17:05:00 -0700831 PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200832 audio_io_handle_t id, type_t type, bool systemReady,
833 audio_config_base_t *mixerConfig = nullptr);
Andy Hung440901d2023-06-29 21:19:25 -0700834 ~PlaybackThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800835
Eric Laurent81784c32012-11-19 14:55:58 -0800836 // Thread virtuals
Andy Hung440901d2023-06-29 21:19:25 -0700837 bool threadLoop() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800838
839 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -0700840 void onFirstRef() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800841
Andy Hung440901d2023-06-29 21:19:25 -0700842 status_t checkEffectCompatibility_l(
843 const effect_descriptor_t* desc, audio_session_t sessionId) final;
Eric Laurent4c415062016-06-17 16:14:16 -0700844
Andy Hung87c693c2023-07-06 20:56:16 -0700845 void addOutputTrack_l(const sp<IAfTrack>& track) final {
846 mTracks.add(track);
847 }
848
Eric Laurent81784c32012-11-19 14:55:58 -0800849protected:
850 // Code snippets that were lifted up out of threadLoop()
851 virtual void threadLoop_mix() = 0;
852 virtual void threadLoop_sleepTime() = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800853 virtual ssize_t threadLoop_write();
854 virtual void threadLoop_drain();
Eric Laurent81784c32012-11-19 14:55:58 -0800855 virtual void threadLoop_standby();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800856 virtual void threadLoop_exit();
Andy Hung8d31fd22023-06-26 19:20:57 -0700857 virtual void threadLoop_removeTracks(const Vector<sp<IAfTrack>>& tracksToRemove);
Eric Laurent81784c32012-11-19 14:55:58 -0800858
859 // prepareTracks_l reads and writes mActiveTracks, and returns
860 // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller
861 // is responsible for clearing or destroying this Vector later on, when it
862 // is safe to do so. That will drop the final ref count and destroy the tracks.
Andy Hung8d31fd22023-06-26 19:20:57 -0700863 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) = 0;
864 void removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove);
Eric Laurenteab90452019-06-24 15:17:46 -0700865 status_t handleVoipVolume_l(float *volume);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800866
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700867 // StreamOutHalInterfaceCallback implementation
868 virtual void onWriteReady();
869 virtual void onDrainReady();
870 virtual void onError();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800871
Andy Hungee58e4a2023-07-07 13:47:37 -0700872public: // AsyncCallbackThread
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700873 void resetWriteBlocked(uint32_t sequence);
874 void resetDraining(uint32_t sequence);
Andy Hungee58e4a2023-07-07 13:47:37 -0700875protected:
Eric Laurentbfb1b832013-01-07 09:53:42 -0800876
877 virtual bool waitingAsyncCallback();
878 virtual bool waitingAsyncCallback_l();
879 virtual bool shouldStandby_l();
Haynes Mathew George4c6a4332014-01-15 12:31:39 -0800880 virtual void onAddNewTrack_l();
Andy Hungee58e4a2023-07-07 13:47:37 -0700881public: // AsyncCallbackThread
Haynes Mathew George4527b9e2016-07-07 19:54:17 -0700882 void onAsyncError(); // error reported by AsyncCallbackThread
Andy Hungee58e4a2023-07-07 13:47:37 -0700883protected:
jiabinf6eb4c32020-02-25 14:06:25 -0800884 // StreamHalInterfaceCodecFormatCallback implementation
885 void onCodecFormatChanged(
Andy Hung440901d2023-06-29 21:19:25 -0700886 const std::basic_string<uint8_t>& metadataBs) final;
jiabinf6eb4c32020-02-25 14:06:25 -0800887
Eric Laurent81784c32012-11-19 14:55:58 -0800888 // ThreadBase virtuals
889 virtual void preExit();
890
Eric Laurent64667972016-03-30 18:19:46 -0700891 virtual bool keepWakeLock() const { return true; }
Andy Hungdae27702016-10-31 14:01:16 -0700892 virtual void acquireWakeLock_l() {
893 ThreadBase::acquireWakeLock_l();
894 mActiveTracks.updatePowerState(this, true /* force */);
895 }
Eric Laurent64667972016-03-30 18:19:46 -0700896
Eric Laurentb3f315a2021-07-13 15:09:05 +0200897 virtual void checkOutputStageEffects() {}
Eric Laurent68a40a82022-05-03 18:15:04 +0200898 virtual void setHalLatencyMode_l() {}
899
Eric Laurentb3f315a2021-07-13 15:09:05 +0200900
Andy Hung440901d2023-06-29 21:19:25 -0700901 void dumpInternals_l(int fd, const Vector<String16>& args) override;
902 void dumpTracks_l(int fd, const Vector<String16>& args) final;
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700903
Eric Laurent81784c32012-11-19 14:55:58 -0800904public:
905
Andy Hung440901d2023-06-29 21:19:25 -0700906 status_t initCheck() const final { return mOutput == nullptr ? NO_INIT : NO_ERROR; }
Eric Laurent81784c32012-11-19 14:55:58 -0800907
908 // return estimated latency in milliseconds, as reported by HAL
Andy Hung440901d2023-06-29 21:19:25 -0700909 uint32_t latency() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800910 // same, but lock must already be held
Andy Hung440901d2023-06-29 21:19:25 -0700911 uint32_t latency_l() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800912
Eric Laurent6acd1d42017-01-04 14:23:29 -0800913 // VolumeInterface
Andy Hung440901d2023-06-29 21:19:25 -0700914 void setMasterVolume(float value) final;
915 void setMasterBalance(float balance) override;
916 void setMasterMute(bool muted) final;
917 void setStreamVolume(audio_stream_type_t stream, float value) final;
918 void setStreamMute(audio_stream_type_t stream, bool muted) final;
919 float streamVolume(audio_stream_type_t stream) const final;
920 void setVolumeForOutput_l(float left, float right) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800921
Andy Hung440901d2023-06-29 21:19:25 -0700922 sp<IAfTrack> createTrack_l(
Andy Hung88035ac2023-06-27 17:05:02 -0700923 const sp<Client>& client,
Eric Laurent81784c32012-11-19 14:55:58 -0800924 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700925 const audio_attributes_t& attr,
Eric Laurent21da6472017-11-09 16:29:26 -0800926 uint32_t *sampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -0800927 audio_format_t format,
928 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800929 size_t *pFrameCount,
Eric Laurent21da6472017-11-09 16:29:26 -0800930 size_t *pNotificationFrameCount,
931 uint32_t notificationsPerBuffer,
932 float speed,
Eric Laurent81784c32012-11-19 14:55:58 -0800933 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -0800934 audio_session_t sessionId,
Eric Laurent05067782016-06-01 18:27:28 -0700935 audio_output_flags_t *flags,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700936 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +0000937 const AttributionSourceState& attributionSource,
Eric Laurent81784c32012-11-19 14:55:58 -0800938 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800939 status_t *status /*non-NULL*/,
jiabinf6eb4c32020-02-25 14:06:25 -0800940 audio_port_handle_t portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200941 const sp<media::IAudioTrackCallback>& callback,
jiabinc658e452022-10-21 20:52:21 +0000942 bool isSpatialized,
jiabin94ed47c2023-07-27 23:34:20 +0000943 bool isBitPerfect,
Andy Hung972bec12023-08-31 16:13:39 -0700944 audio_output_flags_t* afTrackFlags) final
945 REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800946
Andy Hung87c693c2023-07-06 20:56:16 -0700947 bool isTrackActive(const sp<IAfTrack>& track) const final {
948 return mActiveTracks.indexOf(track) >= 0;
949 }
950
951 AudioStreamOut* getOutput_l() const final { return mOutput; }
Andy Hung440901d2023-06-29 21:19:25 -0700952 AudioStreamOut* getOutput() const final;
953 AudioStreamOut* clearOutput() final;
954 sp<StreamHalInterface> stream() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800955
956 // a very large number of suspend() will eventually wraparound, but unlikely
Andy Hung440901d2023-06-29 21:19:25 -0700957 void suspend() final { (void) android_atomic_inc(&mSuspended); }
958 void restore() final
Eric Laurent81784c32012-11-19 14:55:58 -0800959 {
960 // if restore() is done without suspend(), get back into
961 // range so that the next suspend() will operate correctly
962 if (android_atomic_dec(&mSuspended) <= 0) {
963 android_atomic_release_store(0, &mSuspended);
964 }
965 }
Andy Hung440901d2023-06-29 21:19:25 -0700966 bool isSuspended() const final
Eric Laurent81784c32012-11-19 14:55:58 -0800967 { return android_atomic_acquire_load(&mSuspended) > 0; }
968
Andy Hung440901d2023-06-29 21:19:25 -0700969 String8 getParameters(const String8& keys);
970 void ioConfigChanged(audio_io_config_event_t event, pid_t pid = 0,
971 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
972 status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const final;
Andy Hung010a1a12014-03-13 13:57:33 -0700973 // Consider also removing and passing an explicit mMainBuffer initialization
Andy Hung8d31fd22023-06-26 19:20:57 -0700974 // parameter to AF::IAfTrack::Track().
Andy Hung440901d2023-06-29 21:19:25 -0700975 float* sinkBuffer() const final {
Andy Hung319587b2023-05-23 14:01:03 -0700976 return reinterpret_cast<float *>(mSinkBuffer); };
Eric Laurent81784c32012-11-19 14:55:58 -0800977
Andy Hung440901d2023-06-29 21:19:25 -0700978 void detachAuxEffect_l(int effectId) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800979
Andy Hung440901d2023-06-29 21:19:25 -0700980 status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId) final;
981 status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId) final;
982
983 status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final;
984 size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final;
985 uint32_t hasAudioSession_l(audio_session_t sessionId) const final {
Andy Hungc3d62f92019-03-14 13:38:51 -0700986 return ThreadBase::hasAudioSession_l(sessionId, mTracks);
987 }
Andy Hung440901d2023-06-29 21:19:25 -0700988 product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800989
990
Andy Hung440901d2023-06-29 21:19:25 -0700991 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final;
992 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final;
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700993
994 // called with AudioFlinger lock held
Andy Hung440901d2023-06-29 21:19:25 -0700995 bool invalidateTracks_l(audio_stream_type_t streamType) final;
996 bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) final;
997 void invalidateTracks(audio_stream_type_t streamType) override;
jiabinc44b3462022-12-08 12:52:31 -0800998 // Invalidate tracks by a set of port ids. The port id will be removed from
999 // the given set if the corresponding track is found and invalidated.
Andy Hung440901d2023-06-29 21:19:25 -07001000 void invalidateTracks(std::set<audio_port_handle_t>& portIds) override;
Eric Laurent81784c32012-11-19 14:55:58 -08001001
Andy Hung87c693c2023-07-06 20:56:16 -07001002 size_t frameCount() const final { return mNormalFrameCount; }
Glenn Kasten9b58f632013-07-16 11:37:48 -07001003
Andy Hung440901d2023-06-29 21:19:25 -07001004 audio_channel_mask_t mixerChannelMask() const final {
Eric Laurentf1f22e72021-07-13 14:04:14 +02001005 return mMixerChannelMask;
1006 }
1007
Andy Hung440901d2023-06-29 21:19:25 -07001008 status_t getTimestamp_l(AudioTimestamp& timestamp) final;
Eric Laurent83b88082014-06-20 18:31:16 -07001009
Andy Hung440901d2023-06-29 21:19:25 -07001010 void addPatchTrack(const sp<IAfPatchTrack>& track) final;
1011 void deletePatchTrack(const sp<IAfPatchTrack>& track) final;
Eric Laurent83b88082014-06-20 18:31:16 -07001012
Andy Hung440901d2023-06-29 21:19:25 -07001013 void toAudioPortConfig(struct audio_port_config* config) final;
Eric Laurentaccc1472013-09-20 09:36:34 -07001014
Andy Hung10cbff12017-02-21 17:30:14 -08001015 // Return the asynchronous signal wait time.
Andy Hung440901d2023-06-29 21:19:25 -07001016 int64_t computeWaitTimeNs_l() const override { return INT64_MAX; }
Andy Hung1bc088a2018-02-09 15:57:31 -08001017 // returns true if the track is allowed to be added to the thread.
Andy Hung440901d2023-06-29 21:19:25 -07001018 bool isTrackAllowed_l(
Andy Hung1bc088a2018-02-09 15:57:31 -08001019 audio_channel_mask_t channelMask __unused,
1020 audio_format_t format __unused,
1021 audio_session_t sessionId __unused,
Andy Hung440901d2023-06-29 21:19:25 -07001022 uid_t uid) const override {
Andy Hung1bc088a2018-02-09 15:57:31 -08001023 return trackCountForUid_l(uid) < PlaybackThread::kMaxTracksPerUid
1024 && mTracks.size() < PlaybackThread::kMaxTracks;
1025 }
1026
Andy Hung440901d2023-06-29 21:19:25 -07001027 bool isTimestampCorrectionEnabled() const final {
jiabinc52b1ff2019-10-31 17:20:42 -07001028 return audio_is_output_devices(mTimestampCorrectedDevice)
1029 && outDeviceTypes().count(mTimestampCorrectedDevice) != 0;
Andy Hungc8fddf32018-08-08 18:32:37 -07001030 }
jiabinc52b1ff2019-10-31 17:20:42 -07001031
Andy Hung440901d2023-06-29 21:19:25 -07001032 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08001033 return !(mOutput == nullptr || mOutput->stream == nullptr);
1034 }
1035
Andy Hung440901d2023-06-29 21:19:25 -07001036 audio_channel_mask_t hapticChannelMask() const final {
jiabineb3bda02020-06-30 14:07:03 -07001037 return mHapticChannelMask;
1038 }
Andy Hung87c693c2023-07-06 20:56:16 -07001039
1040 uint32_t hapticChannelCount() const final {
1041 return mHapticChannelCount;
1042 }
1043
Andy Hung440901d2023-06-29 21:19:25 -07001044 bool supportsHapticPlayback() const final {
jiabineb3bda02020-06-30 14:07:03 -07001045 return (mHapticChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE;
1046 }
1047
Andy Hung440901d2023-06-29 21:19:25 -07001048 void setDownStreamPatch(const struct audio_patch* patch) final {
Andy Hungc5007f82023-08-29 14:26:09 -07001049 std::lock_guard _l(mutex());
Eric Laurent74c38dc2020-12-23 18:19:44 +01001050 mDownStreamPatch = *patch;
1051 }
1052
Andy Hung440901d2023-06-29 21:19:25 -07001053 IAfTrack* getTrackById_l(audio_port_handle_t trackId) final;
jiabinf042b9b2021-05-07 23:46:28 +00001054
Andy Hung440901d2023-06-29 21:19:25 -07001055 bool hasMixer() const final {
Eric Laurent1c5e2e32021-08-18 18:50:28 +02001056 return mType == MIXER || mType == DUPLICATING || mType == SPATIALIZER;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001057 }
Eric Laurent68a40a82022-05-03 18:15:04 +02001058
Andy Hung440901d2023-06-29 21:19:25 -07001059 status_t setRequestedLatencyMode(
1060 audio_latency_mode_t /* mode */) override { return INVALID_OPERATION; }
Eric Laurent68a40a82022-05-03 18:15:04 +02001061
Andy Hung440901d2023-06-29 21:19:25 -07001062 status_t getSupportedLatencyModes(
1063 std::vector<audio_latency_mode_t>* /* modes */) override {
Eric Laurent68a40a82022-05-03 18:15:04 +02001064 return INVALID_OPERATION;
1065 }
1066
Andy Hung440901d2023-06-29 21:19:25 -07001067 status_t setBluetoothVariableLatencyEnabled(bool /* enabled */) override{
Eric Laurentb0463942022-12-20 16:31:10 +01001068 return INVALID_OPERATION;
1069 }
Eric Laurent52057642022-12-16 11:45:07 +01001070
Andy Hung972bec12023-08-31 16:13:39 -07001071 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override
1072 REQUIRES(audio_utils::AudioFlinger_Mutex);
1073 void stopMelComputation_l() override
1074 REQUIRES(audio_utils::AudioFlinger_Mutex);
Vlad Popab042ee62022-10-20 18:05:00 +02001075
Andy Hung440901d2023-06-29 21:19:25 -07001076 void setStandby() final {
Andy Hungc5007f82023-08-29 14:26:09 -07001077 std::lock_guard _l(mutex());
Eric Laurent19952e12023-04-20 10:08:29 +02001078 setStandby_l();
1079 }
1080
Andy Hung440901d2023-06-29 21:19:25 -07001081 void setStandby_l() final {
Eric Laurent19952e12023-04-20 10:08:29 +02001082 mStandby = true;
1083 mHalStarted = false;
1084 mKernelPositionOnStandby =
1085 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1086 }
1087
Andy Hung440901d2023-06-29 21:19:25 -07001088 bool waitForHalStart() final {
Andy Hungc5007f82023-08-29 14:26:09 -07001089 audio_utils::unique_lock _l(mutex());
Eric Laurent19952e12023-04-20 10:08:29 +02001090 static const nsecs_t kWaitHalTimeoutNs = seconds(2);
1091 nsecs_t endWaitTimetNs = systemTime() + kWaitHalTimeoutNs;
1092 while (!mHalStarted) {
1093 nsecs_t timeNs = systemTime();
1094 if (timeNs >= endWaitTimetNs) {
1095 break;
1096 }
1097 nsecs_t waitTimeLeftNs = endWaitTimetNs - timeNs;
Andy Hungc5007f82023-08-29 14:26:09 -07001098 mWaitHalStartCV.wait_for(_l, std::chrono::nanoseconds(waitTimeLeftNs));
Eric Laurent19952e12023-04-20 10:08:29 +02001099 }
1100 return mHalStarted;
1101 }
Eric Laurent81784c32012-11-19 14:55:58 -08001102protected:
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001103 // updated by readOutputParameters_l()
Glenn Kasten9b58f632013-07-16 11:37:48 -07001104 size_t mNormalFrameCount; // normal mixer and effects
1105
Andy Hung08fb1742015-05-31 23:22:10 -07001106 bool mThreadThrottle; // throttle the thread processing
Andy Hung40eb1a12015-06-18 13:42:02 -07001107 uint32_t mThreadThrottleTimeMs; // throttle time for MIXER threads
1108 uint32_t mThreadThrottleEndMs; // notify once per throttling
Andy Hung08fb1742015-05-31 23:22:10 -07001109 uint32_t mHalfBufferMs; // half the buffer size in milliseconds
1110
Andy Hung010a1a12014-03-13 13:57:33 -07001111 void* mSinkBuffer; // frame size aligned sink buffer
Eric Laurent81784c32012-11-19 14:55:58 -08001112
Andy Hung98ef9782014-03-04 14:46:50 -08001113 // TODO:
1114 // Rearrange the buffer info into a struct/class with
1115 // clear, copy, construction, destruction methods.
1116 //
1117 // mSinkBuffer also has associated with it:
1118 //
1119 // mSinkBufferSize: Sink Buffer Size
1120 // mFormat: Sink Buffer Format
1121
Andy Hung69aed5f2014-02-25 17:24:40 -08001122 // Mixer Buffer (mMixerBuffer*)
1123 //
1124 // In the case of floating point or multichannel data, which is not in the
1125 // sink format, it is required to accumulate in a higher precision or greater channel count
1126 // buffer before downmixing or data conversion to the sink buffer.
1127
1128 // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
1129 bool mMixerBufferEnabled;
1130
1131 // Storage, 32 byte aligned (may make this alignment a requirement later).
1132 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
1133 void* mMixerBuffer;
1134
1135 // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
1136 size_t mMixerBufferSize;
1137
1138 // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
1139 audio_format_t mMixerBufferFormat;
1140
1141 // An internal flag set to true by MixerThread::prepareTracks_l()
1142 // when mMixerBuffer contains valid data after mixing.
1143 bool mMixerBufferValid;
1144
Andy Hung98ef9782014-03-04 14:46:50 -08001145 // Effects Buffer (mEffectsBuffer*)
1146 //
1147 // In the case of effects data, which is not in the sink format,
1148 // it is required to accumulate in a different buffer before data conversion
1149 // to the sink buffer.
1150
1151 // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
1152 bool mEffectBufferEnabled;
1153
1154 // Storage, 32 byte aligned (may make this alignment a requirement later).
1155 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
1156 void* mEffectBuffer;
1157
1158 // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
1159 size_t mEffectBufferSize;
1160
1161 // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
1162 audio_format_t mEffectBufferFormat;
1163
1164 // An internal flag set to true by MixerThread::prepareTracks_l()
1165 // when mEffectsBuffer contains valid data after mixing.
1166 //
1167 // When this is set, all mixer data is routed into the effects buffer
1168 // for any processing (including output processing).
1169 bool mEffectBufferValid;
1170
jiabinc658e452022-10-21 20:52:21 +00001171 // Set to "true" to enable when data has already copied to sink
1172 bool mHasDataCopiedToSinkBuffer = false;
1173
Eric Laurentb62d0362021-10-26 17:40:18 +02001174 // Frame size aligned buffer used as input and output to all post processing effects
1175 // except the Spatializer in a SPATIALIZER thread. Non spatialized tracks are mixed into
1176 // this buffer so that post processing effects can be applied.
1177 void* mPostSpatializerBuffer = nullptr;
1178
1179 // Size of mPostSpatializerBuffer in bytes
1180 size_t mPostSpatializerBufferSize;
Eric Laurent39095982021-08-24 18:29:27 +02001181
1182
Eric Laurent81784c32012-11-19 14:55:58 -08001183 // suspend count, > 0 means suspended. While suspended, the thread continues to pull from
1184 // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle
1185 // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
1186 // workaround that restriction.
1187 // 'volatile' means accessed via atomic operations and no lock.
1188 volatile int32_t mSuspended;
1189
Andy Hung818e7a32016-02-16 18:08:07 -08001190 int64_t mBytesWritten;
yucliu6cfb5932022-07-20 17:40:39 -07001191 std::atomic<int64_t> mFramesWritten; // not reset on standby
Dean Wheatley12473e92021-03-18 23:00:55 +11001192 int64_t mLastFramesWritten = -1; // track changes in timestamp
1193 // server frames written.
Andy Hung238fa3d2016-07-28 10:53:22 -07001194 int64_t mSuspendedFrames; // not reset on standby
jiabin245cdd92018-12-07 17:55:15 -08001195
1196 // mHapticChannelMask and mHapticChannelCount will only be valid when the thread support
1197 // haptic playback.
1198 audio_channel_mask_t mHapticChannelMask = AUDIO_CHANNEL_NONE;
1199 uint32_t mHapticChannelCount = 0;
Eric Laurentf1f22e72021-07-13 14:04:14 +02001200
1201 audio_channel_mask_t mMixerChannelMask = AUDIO_CHANNEL_NONE;
1202
Eric Laurent81784c32012-11-19 14:55:58 -08001203 // mMasterMute is in both PlaybackThread and in AudioFlinger. When a
1204 // PlaybackThread needs to find out if master-muted, it checks it's local
1205 // copy rather than the one in AudioFlinger. This optimization saves a lock.
1206 bool mMasterMute;
1207 void setMasterMute_l(bool muted) { mMasterMute = muted; }
Dean Wheatley12473e92021-03-18 23:00:55 +11001208
1209 auto discontinuityForStandbyOrFlush() const { // call on threadLoop or with lock.
1210 return ((mType == DIRECT && !audio_is_linear_pcm(mFormat))
1211 || mType == OFFLOAD)
1212 ? mTimestampVerifier.DISCONTINUITY_MODE_ZERO
1213 : mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS;
1214 }
1215
Andy Hung8d31fd22023-06-26 19:20:57 -07001216 ActiveTracks<IAfTrack> mActiveTracks;
Eric Laurent81784c32012-11-19 14:55:58 -08001217
Eric Laurent81784c32012-11-19 14:55:58 -08001218 // Time to sleep between cycles when:
1219 virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED
1220 virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE
1221 virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us
1222 // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
1223 // No sleep in standby mode; waits on a condition
1224
1225 // Code snippets that are temporarily lifted up out of threadLoop() until the merge
Andy Hung440901d2023-06-29 21:19:25 -07001226 virtual void checkSilentMode_l() final; // consider unification with MMapThread
Eric Laurent81784c32012-11-19 14:55:58 -08001227
1228 // Non-trivial for DUPLICATING only
1229 virtual void saveOutputTracks() { }
1230 virtual void clearOutputTracks() { }
1231
1232 // Cache various calculated values, at threadLoop() entry and after a parameter change
1233 virtual void cacheParameters_l();
Eric Laurentb3f315a2021-07-13 15:09:05 +02001234 void setCheckOutputStageEffects() override {
1235 mCheckOutputStageEffects.store(true);
1236 }
Eric Laurent81784c32012-11-19 14:55:58 -08001237
1238 virtual uint32_t correctLatency_l(uint32_t latency) const;
1239
Eric Laurent1c333e22014-05-20 10:48:17 -07001240 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1241 audio_patch_handle_t *handle);
1242 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
1243
Andy Hung87c693c2023-07-06 20:56:16 -07001244 bool usesHwAvSync() const final { return mType == DIRECT && mOutput != nullptr
Phil Burk6fc2a7c2015-04-30 16:08:10 -07001245 && mHwSupportsPause
1246 && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
Eric Laurent0f7b5f22014-12-19 10:43:21 -08001247
Andy Hung1bc088a2018-02-09 15:57:31 -08001248 uint32_t trackCountForUid_l(uid_t uid) const;
Eric Laurentad7dd962016-09-22 12:38:37 -07001249
jiabineb3bda02020-06-30 14:07:03 -07001250 void invalidateTracksForAudioSession_l(
Andy Hung440901d2023-06-29 21:19:25 -07001251 audio_session_t sessionId) const override {
jiabineb3bda02020-06-30 14:07:03 -07001252 ThreadBase::invalidateTracksForAudioSession_l(sessionId, mTracks);
1253 }
1254
Mikhail Naganovbf493082017-04-17 17:37:12 -07001255 DISALLOW_COPY_AND_ASSIGN(PlaybackThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001256
Andy Hung87c693c2023-07-06 20:56:16 -07001257 status_t addTrack_l(const sp<IAfTrack>& track) final;
1258 bool destroyTrack_l(const sp<IAfTrack>& track) final;
1259
Andy Hung8d31fd22023-06-26 19:20:57 -07001260 void removeTrack_l(const sp<IAfTrack>& track);
Eric Laurent81784c32012-11-19 14:55:58 -08001261
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001262 void readOutputParameters_l();
Vlad Popa7e81cea2023-01-19 16:34:16 +01001263 MetadataUpdate updateMetadata_l() final;
Kevin Rocardc86a7f72018-04-03 09:00:09 -07001264 virtual void sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata& metadata);
Eric Laurent81784c32012-11-19 14:55:58 -08001265
Dean Wheatley12473e92021-03-18 23:00:55 +11001266 void collectTimestamps_l();
1267
Andy Hungc0691382018-09-12 18:01:57 -07001268 // The Tracks class manages tracks added and removed from the Thread.
Andy Hung1bc088a2018-02-09 15:57:31 -08001269 template <typename T>
1270 class Tracks {
1271 public:
Andy Hung920f6572022-10-06 12:09:49 -07001272 explicit Tracks(bool saveDeletedTrackIds) :
Andy Hungc0691382018-09-12 18:01:57 -07001273 mSaveDeletedTrackIds(saveDeletedTrackIds) { }
Andy Hung1bc088a2018-02-09 15:57:31 -08001274
1275 // SortedVector methods
Andy Hungc0691382018-09-12 18:01:57 -07001276 ssize_t add(const sp<T> &track) {
1277 const ssize_t index = mTracks.add(track);
1278 LOG_ALWAYS_FATAL_IF(index < 0, "cannot add track");
1279 return index;
1280 }
Andy Hung1bc088a2018-02-09 15:57:31 -08001281 ssize_t remove(const sp<T> &track);
1282 size_t size() const {
1283 return mTracks.size();
1284 }
1285 bool isEmpty() const {
1286 return mTracks.isEmpty();
1287 }
1288 ssize_t indexOf(const sp<T> &item) {
1289 return mTracks.indexOf(item);
1290 }
1291 sp<T> operator[](size_t index) const {
1292 return mTracks[index];
1293 }
1294 typename SortedVector<sp<T>>::iterator begin() {
1295 return mTracks.begin();
1296 }
1297 typename SortedVector<sp<T>>::iterator end() {
1298 return mTracks.end();
1299 }
1300
Andy Hung920f6572022-10-06 12:09:49 -07001301 size_t processDeletedTrackIds(const std::function<void(int)>& f) {
Andy Hungc0691382018-09-12 18:01:57 -07001302 for (const int trackId : mDeletedTrackIds) {
1303 f(trackId);
Andy Hung1bc088a2018-02-09 15:57:31 -08001304 }
Andy Hungc0691382018-09-12 18:01:57 -07001305 return mDeletedTrackIds.size();
Andy Hung1bc088a2018-02-09 15:57:31 -08001306 }
1307
Andy Hungc0691382018-09-12 18:01:57 -07001308 void clearDeletedTrackIds() { mDeletedTrackIds.clear(); }
Andy Hung1bc088a2018-02-09 15:57:31 -08001309
1310 private:
Andy Hungc0691382018-09-12 18:01:57 -07001311 // Tracks pending deletion for MIXER type threads
1312 const bool mSaveDeletedTrackIds; // true to enable tracking
1313 std::set<int> mDeletedTrackIds;
Andy Hung1bc088a2018-02-09 15:57:31 -08001314
1315 SortedVector<sp<T>> mTracks; // wrapped SortedVector.
1316 };
1317
Andy Hung8d31fd22023-06-26 19:20:57 -07001318 Tracks<IAfTrack> mTracks;
Andy Hung1bc088a2018-02-09 15:57:31 -08001319
Eric Laurent223fd5c2014-11-11 13:43:36 -08001320 stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Andy Hungee58e4a2023-07-07 13:47:37 -07001321
Eric Laurent81784c32012-11-19 14:55:58 -08001322 AudioStreamOut *mOutput;
1323
1324 float mMasterVolume;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001325 std::atomic<float> mMasterBalance{};
1326 audio_utils::Balance mBalance;
Eric Laurent81784c32012-11-19 14:55:58 -08001327 int mNumWrites;
1328 int mNumDelayedWrites;
1329 bool mInWrite;
1330
1331 // FIXME rename these former local variables of threadLoop to standard "m" names
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001332 nsecs_t mStandbyTimeNs;
Andy Hung25c2dac2014-02-27 14:56:00 -08001333 size_t mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -08001334
1335 // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001336 uint32_t mActiveSleepTimeUs;
1337 uint32_t mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08001338
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001339 uint32_t mSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08001340
1341 // mixer status returned by prepareTracks_l()
1342 mixer_state mMixerStatus; // current cycle
1343 // previous cycle when in prepareTracks_l()
1344 mixer_state mMixerStatusIgnoringFastTracks;
1345 // FIXME or a separate ready state per track
1346
1347 // FIXME move these declarations into the specific sub-class that needs them
1348 // MIXER only
1349 uint32_t sleepTimeShift;
1350
1351 // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001352 nsecs_t mStandbyDelayNs;
Eric Laurent81784c32012-11-19 14:55:58 -08001353
1354 // MIXER only
1355 nsecs_t maxPeriod;
1356
1357 // DUPLICATING only
1358 uint32_t writeFrames;
1359
Eric Laurentbfb1b832013-01-07 09:53:42 -08001360 size_t mBytesRemaining;
1361 size_t mCurrentWriteLength;
1362 bool mUseAsyncWrite;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001363 // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
1364 // incremented each time a write(), a flush() or a standby() occurs.
1365 // Bit 0 is set when a write blocks and indicates a callback is expected.
1366 // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
1367 // callbacks are ignored.
1368 uint32_t mWriteAckSequence;
1369 // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
1370 // incremented each time a drain is requested or a flush() or standby() occurs.
1371 // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
1372 // expected.
1373 // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
1374 // callbacks are ignored.
1375 uint32_t mDrainSequence;
Andy Hungee58e4a2023-07-07 13:47:37 -07001376
Eric Laurentbfb1b832013-01-07 09:53:42 -08001377 sp<AsyncCallbackThread> mCallbackThread;
1378
Andy Hungc5007f82023-08-29 14:26:09 -07001379 audio_utils::mutex& audioTrackCbMutex() const { return mAudioTrackCbMutex; }
1380 mutable audio_utils::mutex mAudioTrackCbMutex;
jiabinf6eb4c32020-02-25 14:06:25 -08001381 // Record of IAudioTrackCallback
Andy Hung8d31fd22023-06-26 19:20:57 -07001382 std::map<sp<IAfTrack>, sp<media::IAudioTrackCallback>> mAudioTrackCallbacks;
jiabinf6eb4c32020-02-25 14:06:25 -08001383
Eric Laurent81784c32012-11-19 14:55:58 -08001384 // The HAL output sink is treated as non-blocking, but current implementation is blocking
1385 sp<NBAIO_Sink> mOutputSink;
1386 // If a fast mixer is present, the blocking pipe sink, otherwise clear
1387 sp<NBAIO_Sink> mPipeSink;
1388 // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
1389 sp<NBAIO_Sink> mNormalSink;
Andy Hungee58e4a2023-07-07 13:47:37 -07001390
Eric Laurent81784c32012-11-19 14:55:58 -08001391 uint32_t mScreenState; // cached copy of gScreenState
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07001392 // TODO: add comment and adjust size as needed
Glenn Kasteneef598c2017-04-03 14:41:13 -07001393 static const size_t kFastMixerLogSize = 8 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -08001394 sp<NBLog::Writer> mFastMixerNBLogWriter;
Andy Hung2148bf02016-11-28 19:01:02 -08001395
Dean Wheatley30d28422018-11-06 10:27:40 +11001396 // Downstream patch latency, available if mDownstreamLatencyStatMs.getN() > 0.
1397 audio_utils::Statistics<double> mDownstreamLatencyStatMs{0.999};
Andy Hung2148bf02016-11-28 19:01:02 -08001398
Eric Laurent19952e12023-04-20 10:08:29 +02001399 // output stream start detection based on render position returned by the kernel
1400 // condition signalled when the output stream has started
Andy Hungc5007f82023-08-29 14:26:09 -07001401 audio_utils::condition_variable mWaitHalStartCV;
Eric Laurent19952e12023-04-20 10:08:29 +02001402 // true when the output stream render position has moved, reset to false in standby
1403 bool mHalStarted = false;
1404 // last kernel render position saved when entering standby
1405 int64_t mKernelPositionOnStandby = 0;
1406
Eric Laurent81784c32012-11-19 14:55:58 -08001407public:
Andy Hung440901d2023-06-29 21:19:25 -07001408 FastTrackUnderruns getFastTrackUnderruns(size_t /* fastIndex */) const override
1409 { return {}; }
1410 const std::atomic<int64_t>& framesWritten() const final { return mFramesWritten; }
Eric Laurent81784c32012-11-19 14:55:58 -08001411
1412protected:
1413 // accessed by both binder threads and within threadLoop(), lock on mutex needed
Andy Hung87c693c2023-07-06 20:56:16 -07001414 uint32_t& fastTrackAvailMask_l() final { return mFastTrackAvailMask; }
1415 uint32_t mFastTrackAvailMask; // bit i set if fast track [i] is available
Eric Laurentd1f69b02014-12-15 14:33:13 -08001416 bool mHwSupportsPause;
1417 bool mHwPaused;
1418 bool mFlushPending;
Eric Laurent7c29ec92017-09-20 17:54:22 -07001419 // volumes last sent to audio HAL with stream->setVolume()
1420 float mLeftVolFloat;
1421 float mRightVolFloat;
Eric Laurent74c38dc2020-12-23 18:19:44 +01001422
1423 // audio patch used by the downstream software patch.
1424 // Only used if ThreadBase::mIsMsdDevice is true.
1425 struct audio_patch mDownStreamPatch;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001426
1427 std::atomic_bool mCheckOutputStageEffects{};
ziyangch8f194f12021-12-01 13:48:04 -08001428
ziyangch8f194f12021-12-01 13:48:04 -08001429
Brian Lindahl65e90012022-07-27 18:01:07 +02001430 // Provides periodic checking for timestamp advancement for underrun detection.
1431 class IsTimestampAdvancing {
1432 public:
1433 // The timestamp will not be checked any faster than the specified time.
Andy Hung920f6572022-10-06 12:09:49 -07001434 explicit IsTimestampAdvancing(nsecs_t minimumTimeBetweenChecksNs)
Brian Lindahl65e90012022-07-27 18:01:07 +02001435 : mMinimumTimeBetweenChecksNs(minimumTimeBetweenChecksNs)
1436 {
1437 clear();
1438 }
1439 // Check if the presentation position has advanced in the last periodic time.
1440 bool check(AudioStreamOut * output);
1441 // Clear the internal state when the playback state changes for the output
1442 // stream.
1443 void clear();
1444 private:
1445 // The minimum time between timestamp checks.
1446 const nsecs_t mMinimumTimeBetweenChecksNs;
1447 // Add differential check on the timestamps to see if there is a change in the
1448 // timestamp frame position between the last call to check.
1449 uint64_t mPreviousPosition;
1450 // The time at which the last check occurred, to ensure we don't check too
1451 // frequently, giving the Audio HAL enough time to update its timestamps.
1452 nsecs_t mPreviousNs;
1453 // The valued is latched so we don't check timestamps too frequently.
1454 bool mLatchedValue;
1455 };
1456 IsTimestampAdvancing mIsTimestampAdvancing;
ziyangch8f194f12021-12-01 13:48:04 -08001457
Brian Lindahl65e90012022-07-27 18:01:07 +02001458 virtual void flushHw_l() {
1459 mIsTimestampAdvancing.clear();
1460 }
Eric Laurent81784c32012-11-19 14:55:58 -08001461};
1462
Eric Laurentb0463942022-12-20 16:31:10 +01001463class MixerThread : public PlaybackThread,
1464 public StreamOutHalInterfaceLatencyModeCallback {
Eric Laurent81784c32012-11-19 14:55:58 -08001465public:
Andy Hung583043b2023-07-17 17:05:00 -07001466 MixerThread(const sp<IAfThreadCallback>& afThreadCallback,
Eric Laurent81784c32012-11-19 14:55:58 -08001467 AudioStreamOut* output,
1468 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001469 bool systemReady,
Eric Laurentf1f22e72021-07-13 14:04:14 +02001470 type_t type = MIXER,
1471 audio_config_base_t *mixerConfig = nullptr);
Andy Hung440901d2023-06-29 21:19:25 -07001472 ~MixerThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001473
Eric Laurentb0463942022-12-20 16:31:10 +01001474 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -07001475 void onFirstRef() override;
Eric Laurentb0463942022-12-20 16:31:10 +01001476
1477 // StreamOutHalInterfaceLatencyModeCallback
1478 void onRecommendedLatencyModeChanged(
Andy Hung440901d2023-06-29 21:19:25 -07001479 std::vector<audio_latency_mode_t> modes) final;
Eric Laurentb0463942022-12-20 16:31:10 +01001480
Eric Laurent81784c32012-11-19 14:55:58 -08001481 // Thread virtuals
1482
Andy Hung440901d2023-06-29 21:19:25 -07001483 bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001484
Andy Hung440901d2023-06-29 21:19:25 -07001485 bool isTrackAllowed_l(
Andy Hung1bc088a2018-02-09 15:57:31 -08001486 audio_channel_mask_t channelMask, audio_format_t format,
Andy Hung440901d2023-06-29 21:19:25 -07001487 audio_session_t sessionId, uid_t uid) const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001488protected:
Andy Hung440901d2023-06-29 21:19:25 -07001489 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) override;
1490 uint32_t idleSleepTimeUs() const final;
1491 uint32_t suspendSleepTimeUs() const final;
1492 void cacheParameters_l() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001493
Andy Hung440901d2023-06-29 21:19:25 -07001494 void acquireWakeLock_l() final {
Andy Hungdae27702016-10-31 14:01:16 -07001495 PlaybackThread::acquireWakeLock_l();
Andy Hung818e7a32016-02-16 18:08:07 -08001496 if (hasFastMixer()) {
1497 mFastMixer->setBoottimeOffset(
1498 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1499 }
1500 }
1501
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001502 void dumpInternals_l(int fd, const Vector<String16>& args) override;
1503
Eric Laurent81784c32012-11-19 14:55:58 -08001504 // threadLoop snippets
Andy Hung440901d2023-06-29 21:19:25 -07001505 ssize_t threadLoop_write() override;
1506 void threadLoop_standby() override;
1507 void threadLoop_mix() override;
1508 void threadLoop_sleepTime() override;
1509 uint32_t correctLatency_l(uint32_t latency) const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001510
Andy Hung440901d2023-06-29 21:19:25 -07001511 status_t createAudioPatch_l(
1512 const struct audio_patch* patch, audio_patch_handle_t* handle) final;
1513 status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final;
Eric Laurent054d9d32015-04-24 08:48:48 -07001514
Eric Laurent81784c32012-11-19 14:55:58 -08001515 AudioMixer* mAudioMixer; // normal mixer
Eric Laurentb0463942022-12-20 16:31:10 +01001516
1517 // Support low latency mode by default as unless explicitly indicated by the audio HAL
1518 // we assume the audio path is compatible with the head tracking latency requirements
1519 std::vector<audio_latency_mode_t> mSupportedLatencyModes = {AUDIO_LATENCY_MODE_LOW};
1520 // default to invalid value to force first update to the audio HAL
1521 audio_latency_mode_t mSetLatencyMode =
1522 (audio_latency_mode_t)AUDIO_LATENCY_MODE_INVALID;
1523
1524 // Bluetooth Variable latency control logic is enabled or disabled for this thread
1525 std::atomic_bool mBluetoothLatencyModesEnabled;
1526
Eric Laurent81784c32012-11-19 14:55:58 -08001527private:
1528 // one-time initialization, no locks required
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001529 sp<FastMixer> mFastMixer; // non-0 if there is also a fast mixer
Eric Laurent81784c32012-11-19 14:55:58 -08001530 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
1531
1532 // contents are not guaranteed to be consistent, no locks required
1533 FastMixerDumpState mFastMixerDumpState;
1534#ifdef STATE_QUEUE_DUMP
1535 StateQueueObserverDump mStateQueueObserverDump;
1536 StateQueueMutatorDump mStateQueueMutatorDump;
1537#endif
1538 AudioWatchdogDump mAudioWatchdogDump;
1539
1540 // accessible only within the threadLoop(), no locks required
1541 // mFastMixer->sq() // for mutating and pushing state
1542 int32_t mFastMixerFutex; // for cold idle
1543
Andy Hung2ddee192015-12-18 17:34:44 -08001544 std::atomic_bool mMasterMono;
Eric Laurent81784c32012-11-19 14:55:58 -08001545public:
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001546 virtual bool hasFastMixer() const { return mFastMixer != 0; }
Eric Laurent81784c32012-11-19 14:55:58 -08001547 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
Glenn Kastendc2c50b2016-04-21 08:13:14 -07001548 ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
Eric Laurent81784c32012-11-19 14:55:58 -08001549 return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
1550 }
Eric Laurent83b88082014-06-20 18:31:16 -07001551
Andy Hung1c86ebe2018-05-29 20:29:08 -07001552 status_t threadloop_getHalTimestamp_l(
1553 ExtendedTimestamp *timestamp) const override {
1554 if (mNormalSink.get() != nullptr) {
1555 return mNormalSink->getTimestamp(*timestamp);
1556 }
1557 return INVALID_OPERATION;
1558 }
1559
Eric Laurentb0463942022-12-20 16:31:10 +01001560 status_t getSupportedLatencyModes(
1561 std::vector<audio_latency_mode_t>* modes) override;
1562
1563 status_t setBluetoothVariableLatencyEnabled(bool enabled) override;
1564
Andy Hung2ddee192015-12-18 17:34:44 -08001565protected:
1566 virtual void setMasterMono_l(bool mono) {
1567 mMasterMono.store(mono);
1568 if (mFastMixer != nullptr) { /* hasFastMixer() */
1569 mFastMixer->setMasterMono(mMasterMono);
1570 }
1571 }
1572 // the FastMixer performs mono blend if it exists.
Glenn Kasten03c48d52016-01-27 17:25:17 -08001573 // Blending with limiter is not idempotent,
1574 // and blending without limiter is idempotent but inefficient to do twice.
Andy Hung2ddee192015-12-18 17:34:44 -08001575 virtual bool requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001576
1577 void setMasterBalance(float balance) override {
1578 mMasterBalance.store(balance);
1579 if (hasFastMixer()) {
1580 mFastMixer->setMasterBalance(balance);
1581 }
1582 }
Eric Laurentb0463942022-12-20 16:31:10 +01001583
1584 void updateHalSupportedLatencyModes_l();
1585 void onHalLatencyModesChanged_l() override;
1586 void setHalLatencyMode_l() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001587};
1588
Andy Hung87c693c2023-07-06 20:56:16 -07001589class DirectOutputThread : public PlaybackThread, public virtual IAfDirectOutputThread {
Eric Laurent81784c32012-11-19 14:55:58 -08001590public:
1591
Andy Hung87c693c2023-07-06 20:56:16 -07001592 sp<IAfDirectOutputThread> asIAfDirectOutputThread() final {
1593 return sp<IAfDirectOutputThread>::fromExisting(this);
1594 }
1595
Andy Hung583043b2023-07-17 17:05:00 -07001596 DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Gareth Fennb18c1a32022-10-05 13:42:36 -07001597 audio_io_handle_t id, bool systemReady,
1598 const audio_offload_info_t& offloadInfo)
Andy Hung583043b2023-07-17 17:05:00 -07001599 : DirectOutputThread(afThreadCallback, output, id, DIRECT, systemReady, offloadInfo) { }
Andy Hung48f59ed2019-01-28 15:06:59 -08001600
Eric Laurent81784c32012-11-19 14:55:58 -08001601 virtual ~DirectOutputThread();
1602
Andy Hung87c693c2023-07-06 20:56:16 -07001603 status_t selectPresentation(int presentationId, int programId) final;
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001604
Eric Laurent81784c32012-11-19 14:55:58 -08001605 // Thread virtuals
1606
Eric Laurent10351942014-05-08 18:49:52 -07001607 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1608 status_t& status);
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001609
ziyangch8f194f12021-12-01 13:48:04 -08001610 void flushHw_l() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001611
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001612 void setMasterBalance(float balance) override;
1613
Eric Laurent81784c32012-11-19 14:55:58 -08001614protected:
Eric Laurent81784c32012-11-19 14:55:58 -08001615 virtual uint32_t activeSleepTimeUs() const;
1616 virtual uint32_t idleSleepTimeUs() const;
1617 virtual uint32_t suspendSleepTimeUs() const;
1618 virtual void cacheParameters_l();
1619
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001620 void dumpInternals_l(int fd, const Vector<String16>& args) override;
1621
Eric Laurent81784c32012-11-19 14:55:58 -08001622 // threadLoop snippets
Andy Hung8d31fd22023-06-26 19:20:57 -07001623 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove);
Eric Laurent81784c32012-11-19 14:55:58 -08001624 virtual void threadLoop_mix();
1625 virtual void threadLoop_sleepTime();
Eric Laurentd1f69b02014-12-15 14:33:13 -08001626 virtual void threadLoop_exit();
1627 virtual bool shouldStandby_l();
Eric Laurent81784c32012-11-19 14:55:58 -08001628
Phil Burk43b4dcc2015-06-09 16:53:44 -07001629 virtual void onAddNewTrack_l();
1630
Gareth Fennb18c1a32022-10-05 13:42:36 -07001631 const audio_offload_info_t mOffloadInfo;
Andy Hung398ffa22022-12-13 19:19:53 -08001632
1633 audioflinger::MonotonicFrameCounter mMonotonicFrameCounter; // for VolumeShaper
Andy Hung48f59ed2019-01-28 15:06:59 -08001634 bool mVolumeShaperActive = false;
Eric Laurent81784c32012-11-19 14:55:58 -08001635
Andy Hung583043b2023-07-17 17:05:00 -07001636 DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Gareth Fennb18c1a32022-10-05 13:42:36 -07001637 audio_io_handle_t id, ThreadBase::type_t type, bool systemReady,
1638 const audio_offload_info_t& offloadInfo);
Andy Hung8d31fd22023-06-26 19:20:57 -07001639 void processVolume_l(IAfTrack *track, bool lastTrack);
Gareth Fennb18c1a32022-10-05 13:42:36 -07001640 bool isTunerStream() const { return (mOffloadInfo.content_id > 0); }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001641
Eric Laurent81784c32012-11-19 14:55:58 -08001642 // prepareTracks_l() tells threadLoop_mix() the name of the single active track
Andy Hung8d31fd22023-06-26 19:20:57 -07001643 sp<IAfTrack> mActiveTrack;
Phil Burk43b4dcc2015-06-09 16:53:44 -07001644
Andy Hung8d31fd22023-06-26 19:20:57 -07001645 wp<IAfTrack> mPreviousTrack; // used to detect track switch
Phil Burk43b4dcc2015-06-09 16:53:44 -07001646
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001647 // This must be initialized for initial condition of mMasterBalance = 0 (disabled).
1648 float mMasterBalanceLeft = 1.f;
1649 float mMasterBalanceRight = 1.f;
1650
Eric Laurent81784c32012-11-19 14:55:58 -08001651public:
1652 virtual bool hasFastMixer() const { return false; }
Andy Hung10cbff12017-02-21 17:30:14 -08001653
1654 virtual int64_t computeWaitTimeNs_l() const override;
Andy Hungf3234512018-07-03 14:51:47 -07001655
1656 status_t threadloop_getHalTimestamp_l(ExtendedTimestamp *timestamp) const override {
1657 // For DIRECT and OFFLOAD threads, query the output sink directly.
1658 if (mOutput != nullptr) {
1659 uint64_t uposition64;
1660 struct timespec time;
1661 if (mOutput->getPresentationPosition(
1662 &uposition64, &time) == OK) {
1663 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL]
1664 = (int64_t)uposition64;
1665 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
1666 = audio_utils_ns_from_timespec(&time);
1667 return NO_ERROR;
1668 }
1669 }
1670 return INVALID_OPERATION;
1671 }
Eric Laurent81784c32012-11-19 14:55:58 -08001672};
1673
Eric Laurentbfb1b832013-01-07 09:53:42 -08001674class OffloadThread : public DirectOutputThread {
1675public:
1676
Andy Hung583043b2023-07-17 17:05:00 -07001677 OffloadThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
Gareth Fennb18c1a32022-10-05 13:42:36 -07001678 audio_io_handle_t id, bool systemReady,
1679 const audio_offload_info_t& offloadInfo);
Eric Laurent6a51d7e2013-10-17 18:59:26 -07001680 virtual ~OffloadThread() {};
ziyangch8f194f12021-12-01 13:48:04 -08001681 void flushHw_l() override;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001682
1683protected:
1684 // threadLoop snippets
Andy Hung8d31fd22023-06-26 19:20:57 -07001685 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001686 virtual void threadLoop_exit();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001687
1688 virtual bool waitingAsyncCallback();
1689 virtual bool waitingAsyncCallback_l();
Haynes Mathew George05317d22016-05-03 16:34:26 -07001690 virtual void invalidateTracks(audio_stream_type_t streamType);
jiabinc44b3462022-12-08 12:52:31 -08001691 void invalidateTracks(std::set<audio_port_handle_t>& portIds) override;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001692
Eric Laurentde0613d2016-07-22 18:19:11 -07001693 virtual bool keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
Eric Laurent64667972016-03-30 18:19:46 -07001694
Eric Laurentbfb1b832013-01-07 09:53:42 -08001695private:
Eric Laurentbfb1b832013-01-07 09:53:42 -08001696 size_t mPausedWriteLength; // length in bytes of write interrupted by pause
1697 size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume
Eric Laurent64667972016-03-30 18:19:46 -07001698 bool mKeepWakeLock; // keep wake lock while waiting for write callback
Eric Laurentbfb1b832013-01-07 09:53:42 -08001699};
1700
1701class AsyncCallbackThread : public Thread {
1702public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001703 explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001704
Eric Laurentbfb1b832013-01-07 09:53:42 -08001705 // Thread virtuals
1706 virtual bool threadLoop();
1707
1708 // RefBase
1709 virtual void onFirstRef();
1710
1711 void exit();
Eric Laurent3b4529e2013-09-05 18:09:19 -07001712 void setWriteBlocked(uint32_t sequence);
1713 void resetWriteBlocked();
1714 void setDraining(uint32_t sequence);
1715 void resetDraining();
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07001716 void setAsyncError();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001717
1718private:
Eric Laurent4de95592013-09-26 15:28:21 -07001719 const wp<PlaybackThread> mPlaybackThread;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001720 // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
1721 // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
1722 // to indicate that the callback has been received via resetWriteBlocked()
Eric Laurent4de95592013-09-26 15:28:21 -07001723 uint32_t mWriteAckSequence;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001724 // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
1725 // setDraining(). The sequence is shifted one bit to the left and the lsb is used
1726 // to indicate that the callback has been received via resetDraining()
Eric Laurent4de95592013-09-26 15:28:21 -07001727 uint32_t mDrainSequence;
Andy Hungc5007f82023-08-29 14:26:09 -07001728 audio_utils::condition_variable mWaitWorkCV;
1729 mutable audio_utils::mutex mMutex;
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07001730 bool mAsyncError;
Andy Hungc5007f82023-08-29 14:26:09 -07001731
1732 audio_utils::mutex& mutex() const { return mMutex; }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001733};
1734
Andy Hung87c693c2023-07-06 20:56:16 -07001735class DuplicatingThread : public MixerThread, public IAfDuplicatingThread {
Eric Laurent81784c32012-11-19 14:55:58 -08001736public:
Andy Hung583043b2023-07-17 17:05:00 -07001737 DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback,
1738 IAfPlaybackThread* mainThread,
Eric Laurent72e3f392015-05-20 14:43:50 -07001739 audio_io_handle_t id, bool systemReady);
Andy Hung87c693c2023-07-06 20:56:16 -07001740 ~DuplicatingThread() override;
1741
1742 sp<IAfDuplicatingThread> asIAfDuplicatingThread() final {
1743 return sp<IAfDuplicatingThread>::fromExisting(this);
1744 }
Eric Laurent81784c32012-11-19 14:55:58 -08001745
1746 // Thread virtuals
Andy Hung87c693c2023-07-06 20:56:16 -07001747 void addOutputTrack(IAfPlaybackThread* thread) final;
1748 void removeOutputTrack(IAfPlaybackThread* thread) final;
1749 uint32_t waitTimeMs() const final { return mWaitTimeMs; }
Kevin Rocard069c2712018-03-29 19:09:14 -07001750
Kevin Rocardc86a7f72018-04-03 09:00:09 -07001751 void sendMetadataToBackend_l(
1752 const StreamOutHalInterface::SourceMetadata& metadata) override;
Eric Laurent81784c32012-11-19 14:55:58 -08001753protected:
1754 virtual uint32_t activeSleepTimeUs() const;
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001755 void dumpInternals_l(int fd, const Vector<String16>& args) override;
Eric Laurent81784c32012-11-19 14:55:58 -08001756
1757private:
Andy Hung920f6572022-10-06 12:09:49 -07001758 bool outputsReady();
Eric Laurent81784c32012-11-19 14:55:58 -08001759protected:
1760 // threadLoop snippets
1761 virtual void threadLoop_mix();
1762 virtual void threadLoop_sleepTime();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001763 virtual ssize_t threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -08001764 virtual void threadLoop_standby();
1765 virtual void cacheParameters_l();
1766
1767private:
1768 // called from threadLoop, addOutputTrack, removeOutputTrack
1769 virtual void updateWaitTime_l();
1770protected:
1771 virtual void saveOutputTracks();
1772 virtual void clearOutputTracks();
1773private:
1774
1775 uint32_t mWaitTimeMs;
Andy Hung8d31fd22023-06-26 19:20:57 -07001776 SortedVector <sp<IAfOutputTrack>> outputTracks;
1777 SortedVector <sp<IAfOutputTrack>> mOutputTracks;
Eric Laurent81784c32012-11-19 14:55:58 -08001778public:
1779 virtual bool hasFastMixer() const { return false; }
Andy Hung1c86ebe2018-05-29 20:29:08 -07001780 status_t threadloop_getHalTimestamp_l(
1781 ExtendedTimestamp *timestamp) const override {
1782 if (mOutputTracks.size() > 0) {
1783 // forward the first OutputTrack's kernel information for timestamp.
1784 const ExtendedTimestamp trackTimestamp =
1785 mOutputTracks[0]->getClientProxyTimestamp();
1786 if (trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
1787 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
1788 trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
1789 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
1790 trackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1791 return OK; // discard server timestamp - that's ignored.
1792 }
1793 }
1794 return INVALID_OPERATION;
1795 }
Eric Laurent81784c32012-11-19 14:55:58 -08001796};
1797
Eric Laurentb0463942022-12-20 16:31:10 +01001798class SpatializerThread : public MixerThread {
Eric Laurentb3f315a2021-07-13 15:09:05 +02001799public:
Andy Hung583043b2023-07-17 17:05:00 -07001800 SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback,
Eric Laurentb3f315a2021-07-13 15:09:05 +02001801 AudioStreamOut* output,
1802 audio_io_handle_t id,
1803 bool systemReady,
1804 audio_config_base_t *mixerConfig);
Eric Laurentb3f315a2021-07-13 15:09:05 +02001805
Andy Hung440901d2023-06-29 21:19:25 -07001806 bool hasFastMixer() const final { return false; }
Eric Laurentb3f315a2021-07-13 15:09:05 +02001807
Eric Laurent68a40a82022-05-03 18:15:04 +02001808 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -07001809 void onFirstRef() final;
Eric Laurent68a40a82022-05-03 18:15:04 +02001810
Andy Hung440901d2023-06-29 21:19:25 -07001811 status_t setRequestedLatencyMode(audio_latency_mode_t mode) final;
Eric Laurent68a40a82022-05-03 18:15:04 +02001812
Eric Laurentb3f315a2021-07-13 15:09:05 +02001813protected:
Andy Hung440901d2023-06-29 21:19:25 -07001814 void checkOutputStageEffects() final;
1815 void setHalLatencyMode_l() final;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001816
1817private:
Eric Laurent68a40a82022-05-03 18:15:04 +02001818 // Do not request a specific mode by default
1819 audio_latency_mode_t mRequestedLatencyMode = AUDIO_LATENCY_MODE_FREE;
1820
Andy Hung116bc262023-06-20 18:56:17 -07001821 sp<IAfEffectHandle> mFinalDownMixer;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001822};
1823
Eric Laurent81784c32012-11-19 14:55:58 -08001824// record thread
Andy Hung87c693c2023-07-06 20:56:16 -07001825class RecordThread : public IAfRecordThread, public ThreadBase
Eric Laurent81784c32012-11-19 14:55:58 -08001826{
Andy Hung8d31fd22023-06-26 19:20:57 -07001827 friend class ResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08001828public:
Andy Hung87c693c2023-07-06 20:56:16 -07001829 sp<IAfRecordThread> asIAfRecordThread() final {
1830 return sp<IAfRecordThread>::fromExisting(this);
1831 }
Eric Laurent81784c32012-11-19 14:55:58 -08001832
Andy Hung583043b2023-07-17 17:05:00 -07001833 RecordThread(const sp<IAfThreadCallback>& afThreadCallback,
Eric Laurent81784c32012-11-19 14:55:58 -08001834 AudioStreamIn *input,
Eric Laurent81784c32012-11-19 14:55:58 -08001835 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001836 bool systemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08001837 );
Andy Hung440901d2023-06-29 21:19:25 -07001838 ~RecordThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001839
1840 // no addTrack_l ?
Andy Hung87c693c2023-07-06 20:56:16 -07001841 void destroyTrack_l(const sp<IAfRecordTrack>& track) final;
1842 void removeTrack_l(const sp<IAfRecordTrack>& track) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001843
Eric Laurent81784c32012-11-19 14:55:58 -08001844 // Thread virtuals
Andy Hung440901d2023-06-29 21:19:25 -07001845 bool threadLoop() final;
1846 void preExit() final;
Eric Laurent81784c32012-11-19 14:55:58 -08001847
1848 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -07001849 void onFirstRef() final;
Eric Laurent81784c32012-11-19 14:55:58 -08001850
Andy Hung440901d2023-06-29 21:19:25 -07001851 status_t initCheck() const final { return mInput == nullptr ? NO_INIT : NO_ERROR; }
Glenn Kastene198c362013-08-13 09:13:36 -07001852
Andy Hung440901d2023-06-29 21:19:25 -07001853 sp<MemoryDealer> readOnlyHeap() const final { return mReadOnlyHeap; }
Glenn Kastenb880f5e2014-05-07 08:43:45 -07001854
Andy Hung440901d2023-06-29 21:19:25 -07001855 sp<IMemory> pipeMemory() const final { return mPipeMemory; }
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001856
Andy Hung87c693c2023-07-06 20:56:16 -07001857 sp<IAfRecordTrack> createRecordTrack_l(
Andy Hung88035ac2023-06-27 17:05:02 -07001858 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001859 const audio_attributes_t& attr,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001860 uint32_t *pSampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -08001861 audio_format_t format,
1862 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001863 size_t *pFrameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08001864 audio_session_t sessionId,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001865 size_t *pNotificationFrameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07001866 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00001867 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -07001868 audio_input_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -08001869 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001870 status_t *status /*non-NULL*/,
Eric Laurentec376dc2021-04-08 20:41:22 +02001871 audio_port_handle_t portId,
Andy Hung972bec12023-08-31 16:13:39 -07001872 int32_t maxSharedAudioHistoryMs) final
1873 REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -08001874
Andy Hung8d31fd22023-06-26 19:20:57 -07001875 status_t start(IAfRecordTrack* recordTrack,
Eric Laurent81784c32012-11-19 14:55:58 -08001876 AudioSystem::sync_event_t event,
Andy Hung87c693c2023-07-06 20:56:16 -07001877 audio_session_t triggerSession) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001878
1879 // ask the thread to stop the specified track, and
1880 // return true if the caller should then do it's part of the stopping process
Andy Hung87c693c2023-07-06 20:56:16 -07001881 bool stop(IAfRecordTrack* recordTrack) final;
1882 AudioStreamIn* getInput() const final { return mInput; }
1883 AudioStreamIn* clearInput() final;
Eric Laurent81784c32012-11-19 14:55:58 -08001884
Andy Hung99b1ba62023-07-14 11:00:08 -07001885 // TODO(b/291317898) Unify with IAfThreadBase
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001886 virtual sp<StreamHalInterface> stream() const;
Eric Laurent81784c32012-11-19 14:55:58 -08001887
Eric Laurent81784c32012-11-19 14:55:58 -08001888
Eric Laurent10351942014-05-08 18:49:52 -07001889 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1890 status_t& status);
1891 virtual void cacheParameters_l() {}
Eric Laurent81784c32012-11-19 14:55:58 -08001892 virtual String8 getParameters(const String8& keys);
Andy Hung87c693c2023-07-06 20:56:16 -07001893 void ioConfigChanged(audio_io_config_event_t event, pid_t pid = 0,
1894 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
Eric Laurent1c333e22014-05-20 10:48:17 -07001895 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1896 audio_patch_handle_t *handle);
1897 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
jiabinc52b1ff2019-10-31 17:20:42 -07001898 void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override;
Eric Laurent5f0fd7b2021-05-07 16:33:26 +02001899 void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override;
Eric Laurent83b88082014-06-20 18:31:16 -07001900
Andy Hung87c693c2023-07-06 20:56:16 -07001901 void addPatchTrack(const sp<IAfPatchRecord>& record) final;
1902 void deletePatchTrack(const sp<IAfPatchRecord>& record) final;
Eric Laurent83b88082014-06-20 18:31:16 -07001903
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001904 void readInputParameters_l();
Andy Hung87c693c2023-07-06 20:56:16 -07001905 uint32_t getInputFramesLost() const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001906
Andy Hung116bc262023-06-20 18:56:17 -07001907 virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain);
1908 virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain);
Andy Hungc3d62f92019-03-14 13:38:51 -07001909 uint32_t hasAudioSession_l(audio_session_t sessionId) const override {
1910 return ThreadBase::hasAudioSession_l(sessionId, mTracks);
1911 }
Eric Laurent81784c32012-11-19 14:55:58 -08001912
1913 // Return the set of unique session IDs across all tracks.
1914 // The keys are the session IDs, and the associated values are meaningless.
1915 // FIXME replace by Set [and implement Bag/Multiset for other uses].
Glenn Kastend848eb42016-03-08 13:42:11 -08001916 KeyedVector<audio_session_t, bool> sessionIds() const;
Eric Laurent81784c32012-11-19 14:55:58 -08001917
Andy Hung068e08e2023-05-15 19:02:55 -07001918 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) override;
1919 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const override;
Eric Laurent81784c32012-11-19 14:55:58 -08001920
Andy Hung068e08e2023-05-15 19:02:55 -07001921 static void syncStartEventCallback(const wp<audioflinger::SyncEvent>& event);
Eric Laurent81784c32012-11-19 14:55:58 -08001922
Glenn Kasten9b58f632013-07-16 11:37:48 -07001923 virtual size_t frameCount() const { return mFrameCount; }
Andy Hung87c693c2023-07-06 20:56:16 -07001924 bool hasFastCapture() const final { return mFastCapture != 0; }
Mikhail Naganovdc769682018-05-04 15:34:08 -07001925 virtual void toAudioPortConfig(struct audio_port_config *config);
Glenn Kasten9b58f632013-07-16 11:37:48 -07001926
Eric Laurent4c415062016-06-17 16:14:16 -07001927 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
1928 audio_session_t sessionId);
1929
Andy Hungdae27702016-10-31 14:01:16 -07001930 virtual void acquireWakeLock_l() {
1931 ThreadBase::acquireWakeLock_l();
1932 mActiveTracks.updatePowerState(this, true /* force */);
1933 }
1934
Andy Hung87c693c2023-07-06 20:56:16 -07001935 void checkBtNrec() final;
Eric Laurentd8365c52017-07-16 15:27:05 -07001936
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001937 // Sets the UID records silence
Andy Hung87c693c2023-07-06 20:56:16 -07001938 void setRecordSilenced(audio_port_handle_t portId, bool silenced) final;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001939
Andy Hung87c693c2023-07-06 20:56:16 -07001940 status_t getActiveMicrophones(
1941 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const final;
1942 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) final;
1943 status_t setPreferredMicrophoneFieldDimension(float zoom) final;
Paul McLean03a6e6a2018-12-04 10:54:13 -07001944
Vlad Popa7e81cea2023-01-19 16:34:16 +01001945 MetadataUpdate updateMetadata_l() override;
Kevin Rocard069c2712018-03-29 19:09:14 -07001946
Andy Hung87c693c2023-07-06 20:56:16 -07001947 bool fastTrackAvailable() const final { return mFastTrackAvail; }
1948 void setFastTrackAvailable(bool available) final { mFastTrackAvail = available; }
jiabin01c8f562018-07-19 17:47:28 -07001949
Andy Hungc8fddf32018-08-08 18:32:37 -07001950 bool isTimestampCorrectionEnabled() const override {
1951 // checks popcount for exactly one device.
Atneya Nair497fff12022-01-18 16:23:04 -05001952 // Is currently disabled. Before enabling,
1953 // verify compressed record timestamps.
jiabinc52b1ff2019-10-31 17:20:42 -07001954 return audio_is_input_device(mTimestampCorrectedDevice)
1955 && inDeviceType() == mTimestampCorrectedDevice;
Andy Hungc8fddf32018-08-08 18:32:37 -07001956 }
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001957
Andy Hung87c693c2023-07-06 20:56:16 -07001958 status_t shareAudioHistory(const std::string& sharedAudioPackageName,
Eric Laurentec376dc2021-04-08 20:41:22 +02001959 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
Andy Hung87c693c2023-07-06 20:56:16 -07001960 int64_t sharedAudioStartMs = -1) final;
Eric Laurentec376dc2021-04-08 20:41:22 +02001961 status_t shareAudioHistory_l(const std::string& sharedAudioPackageName,
1962 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
1963 int64_t sharedAudioStartMs = -1);
Andy Hung87c693c2023-07-06 20:56:16 -07001964 void resetAudioHistory_l() final;
Eric Laurentec376dc2021-04-08 20:41:22 +02001965
Andy Hung440901d2023-06-29 21:19:25 -07001966 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08001967 return !(mInput == nullptr || mInput->stream == nullptr);
1968 }
1969
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001970protected:
1971 void dumpInternals_l(int fd, const Vector<String16>& args) override;
1972 void dumpTracks_l(int fd, const Vector<String16>& args) override;
1973
Eric Laurent81784c32012-11-19 14:55:58 -08001974private:
Eric Laurent81784c32012-11-19 14:55:58 -08001975 // Enter standby if not already in standby, and set mStandby flag
Glenn Kasten93e471f2013-08-19 08:40:07 -07001976 void standbyIfNotAlreadyInStandby();
Eric Laurent81784c32012-11-19 14:55:58 -08001977
1978 // Call the HAL standby method unconditionally, and don't change mStandby flag
Glenn Kastene198c362013-08-13 09:13:36 -07001979 void inputStandBy();
Eric Laurent81784c32012-11-19 14:55:58 -08001980
Eric Laurentd8365c52017-07-16 15:27:05 -07001981 void checkBtNrec_l();
1982
Eric Laurentec376dc2021-04-08 20:41:22 +02001983 int32_t getOldestFront_l();
1984 void updateFronts_l(int32_t offset);
1985
Eric Laurent81784c32012-11-19 14:55:58 -08001986 AudioStreamIn *mInput;
Mikhail Naganov2534b382019-09-25 13:05:02 -07001987 Source *mSource;
Andy Hung8d31fd22023-06-26 19:20:57 -07001988 SortedVector <sp<IAfRecordTrack>> mTracks;
Glenn Kasten2b806402013-11-20 16:37:38 -08001989 // mActiveTracks has dual roles: it indicates the current active track(s), and
Andy Hungc5007f82023-08-29 14:26:09 -07001990 // is used together with mStartStopCV to indicate start()/stop() progress
Andy Hung8d31fd22023-06-26 19:20:57 -07001991 ActiveTracks<IAfRecordTrack> mActiveTracks;
Andy Hungdae27702016-10-31 14:01:16 -07001992
Andy Hungc5007f82023-08-29 14:26:09 -07001993 audio_utils::condition_variable mStartStopCV;
Glenn Kasten9b58f632013-07-16 11:37:48 -07001994
Glenn Kasten85948432013-08-19 12:09:05 -07001995 // resampler converts input at HAL Hz to output at AudioRecord client Hz
Glenn Kasten1b291842016-07-18 14:55:21 -07001996 void *mRsmpInBuffer; // size = mRsmpInFramesOA
Glenn Kasten85948432013-08-19 12:09:05 -07001997 size_t mRsmpInFrames; // size of resampler input in frames
1998 size_t mRsmpInFramesP2;// size rounded up to a power-of-2
Glenn Kasten1b291842016-07-18 14:55:21 -07001999 size_t mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002000
2001 // rolling index that is never cleared
Glenn Kasten85948432013-08-19 12:09:05 -07002002 int32_t mRsmpInRear; // last filled frame + 1
Glenn Kasten85948432013-08-19 12:09:05 -07002003
Eric Laurent81784c32012-11-19 14:55:58 -08002004 // For dumpsys
Glenn Kastenb880f5e2014-05-07 08:43:45 -07002005 const sp<MemoryDealer> mReadOnlyHeap;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002006
2007 // one-time initialization, no locks required
Glenn Kastenb187de12014-12-30 08:18:15 -08002008 sp<FastCapture> mFastCapture; // non-0 if there is also
2009 // a fast capture
Eric Laurent72e3f392015-05-20 14:43:50 -07002010
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002011 // FIXME audio watchdog thread
2012
2013 // contents are not guaranteed to be consistent, no locks required
2014 FastCaptureDumpState mFastCaptureDumpState;
2015#ifdef STATE_QUEUE_DUMP
2016 // FIXME StateQueue observer and mutator dump fields
2017#endif
2018 // FIXME audio watchdog dump
2019
2020 // accessible only within the threadLoop(), no locks required
2021 // mFastCapture->sq() // for mutating and pushing state
2022 int32_t mFastCaptureFutex; // for cold idle
2023
2024 // The HAL input source is treated as non-blocking,
2025 // but current implementation is blocking
2026 sp<NBAIO_Source> mInputSource;
2027 // The source for the normal capture thread to read from: mInputSource or mPipeSource
2028 sp<NBAIO_Source> mNormalSource;
2029 // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
2030 // otherwise clear
2031 sp<NBAIO_Sink> mPipeSink;
2032 // If a fast capture is present, the non-blocking pipe source read by normal thread,
2033 // otherwise clear
2034 sp<NBAIO_Source> mPipeSource;
2035 // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
2036 size_t mPipeFramesP2;
2037 // If a fast capture is present, the Pipe as IMemory, otherwise clear
2038 sp<IMemory> mPipeMemory;
2039
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07002040 // TODO: add comment and adjust size as needed
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002041 static const size_t kFastCaptureLogSize = 4 * 1024;
2042 sp<NBLog::Writer> mFastCaptureNBLogWriter;
2043
2044 bool mFastTrackAvail; // true if fast track available
Eric Laurentd8365c52017-07-16 15:27:05 -07002045 // common state to all record threads
2046 std::atomic_bool mBtNrecSuspended;
Andy Hung6427e442018-08-09 12:51:02 -07002047
2048 int64_t mFramesRead = 0; // continuous running counter.
jiabinc52b1ff2019-10-31 17:20:42 -07002049
2050 DeviceDescriptorBaseVector mOutDevices;
Eric Laurentec376dc2021-04-08 20:41:22 +02002051
Eric Laurent5f0fd7b2021-05-07 16:33:26 +02002052 int32_t mMaxSharedAudioHistoryMs = 0;
Eric Laurentec376dc2021-04-08 20:41:22 +02002053 std::string mSharedAudioPackageName = {};
Eric Laurent2407ce32021-04-26 14:56:03 +02002054 int32_t mSharedAudioStartFrames = -1;
Eric Laurentec376dc2021-04-08 20:41:22 +02002055 audio_session_t mSharedAudioSessionId = AUDIO_SESSION_NONE;
Eric Laurent81784c32012-11-19 14:55:58 -08002056};
Eric Laurent6acd1d42017-01-04 14:23:29 -08002057
Andy Hung7aa7d102023-07-07 15:58:48 -07002058class MmapThread : public ThreadBase, public virtual IAfMmapThread
Eric Laurent6acd1d42017-01-04 14:23:29 -08002059{
2060 public:
Andy Hung583043b2023-07-17 17:05:00 -07002061 MmapThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
Andy Hung920f6572022-10-06 12:09:49 -07002062 AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady,
Andy Hungcf10d742020-04-28 15:38:24 -07002063 bool isOut);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002064
Andy Hung7aa7d102023-07-07 15:58:48 -07002065 void configure(const audio_attributes_t* attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08002066 audio_stream_type_t streamType,
2067 audio_session_t sessionId,
2068 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07002069 audio_port_handle_t deviceId,
Andy Hung7aa7d102023-07-07 15:58:48 -07002070 audio_port_handle_t portId) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002071
Andy Hung7aa7d102023-07-07 15:58:48 -07002072 void disconnect() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002073
Andy Hung440901d2023-06-29 21:19:25 -07002074 // MmapStreamInterface for adapter.
Andy Hung7aa7d102023-07-07 15:58:48 -07002075 status_t createMmapBuffer(int32_t minSizeFrames, struct audio_mmap_buffer_info* info) final;
2076 status_t getMmapPosition(struct audio_mmap_position* position) const override;
2077 status_t start(const AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -07002078 const audio_attributes_t *attr,
Andy Hung7aa7d102023-07-07 15:58:48 -07002079 audio_port_handle_t* handle) final;
2080 status_t stop(audio_port_handle_t handle) final;
2081 status_t standby() final;
2082 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const = 0;
2083 status_t reportData(const void* buffer, size_t frameCount) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002084
2085 // RefBase
Andy Hung440901d2023-06-29 21:19:25 -07002086 void onFirstRef() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002087
2088 // Thread virtuals
Andy Hung440901d2023-06-29 21:19:25 -07002089 bool threadLoop() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002090
Andy Hung440901d2023-06-29 21:19:25 -07002091 // Not in ThreadBase
2092 virtual void threadLoop_exit() final;
2093 virtual void threadLoop_standby() final;
2094 virtual bool shouldStandby_l() final { return false; }
Andy Hungc5007f82023-08-29 14:26:09 -07002095 virtual status_t exitStandby_l() REQUIRES(mutex());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002096
Andy Hung440901d2023-06-29 21:19:25 -07002097 status_t initCheck() const final { return mHalStream == nullptr ? NO_INIT : NO_ERROR; }
2098 size_t frameCount() const final { return mFrameCount; }
2099 bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) final;
2100 String8 getParameters(const String8& keys) final;
2101 void ioConfigChanged(audio_io_config_event_t event, pid_t pid = 0,
2102 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002103 void readHalParameters_l();
Andy Hung440901d2023-06-29 21:19:25 -07002104 void cacheParameters_l() final {}
2105 status_t createAudioPatch_l(
2106 const struct audio_patch* patch, audio_patch_handle_t* handle) final;
2107 status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final;
2108 void toAudioPortConfig(struct audio_port_config* config) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002109
Andy Hung440901d2023-06-29 21:19:25 -07002110 sp<StreamHalInterface> stream() const final { return mHalStream; }
2111 status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final;
2112 size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final;
2113 status_t checkEffectCompatibility_l(
2114 const effect_descriptor_t *desc, audio_session_t sessionId) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002115
Andy Hung440901d2023-06-29 21:19:25 -07002116 uint32_t hasAudioSession_l(audio_session_t sessionId) const override {
Andy Hungc3d62f92019-03-14 13:38:51 -07002117 // Note: using mActiveTracks as no mTracks here.
2118 return ThreadBase::hasAudioSession_l(sessionId, mActiveTracks);
2119 }
Andy Hung440901d2023-06-29 21:19:25 -07002120 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final;
2121 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002122
Andy Hung440901d2023-06-29 21:19:25 -07002123 virtual void checkSilentMode_l() {} // cannot be const (RecordThread)
2124 virtual void processVolume_l() {}
Eric Laurent6acd1d42017-01-04 14:23:29 -08002125 void checkInvalidTracks_l();
2126
Andy Hung440901d2023-06-29 21:19:25 -07002127 // Not in ThreadBase
2128 virtual audio_stream_type_t streamType() const { return AUDIO_STREAM_DEFAULT; }
2129 virtual void invalidateTracks(audio_stream_type_t /* streamType */) {}
Andy Hung7aa7d102023-07-07 15:58:48 -07002130 void invalidateTracks(std::set<audio_port_handle_t>& /* portIds */) override {}
Eric Laurent6acd1d42017-01-04 14:23:29 -08002131
Eric Laurent331679c2018-04-16 17:03:16 -07002132 // Sets the UID records silence
Andy Hung7aa7d102023-07-07 15:58:48 -07002133 void setRecordSilenced(
2134 audio_port_handle_t /* portId */, bool /* silenced */) override {}
Eric Laurent331679c2018-04-16 17:03:16 -07002135
Andy Hung440901d2023-06-29 21:19:25 -07002136 bool isStreamInitialized() const override { return false; }
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002137
jiabin09609032022-06-15 19:26:01 +00002138 void setClientSilencedState_l(audio_port_handle_t portId, bool silenced) {
2139 mClientSilencedStates[portId] = silenced;
2140 }
2141
2142 size_t eraseClientSilencedState_l(audio_port_handle_t portId) {
2143 return mClientSilencedStates.erase(portId);
2144 }
2145
2146 bool isClientSilenced_l(audio_port_handle_t portId) const {
2147 const auto it = mClientSilencedStates.find(portId);
2148 return it != mClientSilencedStates.end() ? it->second : false;
2149 }
2150
2151 void setClientSilencedIfExists_l(audio_port_handle_t portId, bool silenced) {
2152 const auto it = mClientSilencedStates.find(portId);
2153 if (it != mClientSilencedStates.end()) {
2154 it->second = silenced;
2155 }
2156 }
2157
Eric Laurent6acd1d42017-01-04 14:23:29 -08002158 protected:
Andy Hung440901d2023-06-29 21:19:25 -07002159 void dumpInternals_l(int fd, const Vector<String16>& args) override;
2160 void dumpTracks_l(int fd, const Vector<String16>& args) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002161
jiabinc52b1ff2019-10-31 17:20:42 -07002162 /**
2163 * @brief mDeviceId current device port unique identifier
2164 */
2165 audio_port_handle_t mDeviceId = AUDIO_PORT_HANDLE_NONE;
2166
Eric Laurent6acd1d42017-01-04 14:23:29 -08002167 audio_attributes_t mAttr;
2168 audio_session_t mSessionId;
2169 audio_port_handle_t mPortId;
2170
Phil Burk7f6b40d2017-02-09 13:18:38 -08002171 wp<MmapStreamCallback> mCallback;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002172 sp<StreamHalInterface> mHalStream;
2173 sp<DeviceHalInterface> mHalDevice;
2174 AudioHwDevice* const mAudioHwDev;
Andy Hung8d31fd22023-06-26 19:20:57 -07002175 ActiveTracks<IAfMmapTrack> mActiveTracks;
Eric Laurent67f97292018-04-20 18:05:41 -07002176 float mHalVolFloat;
jiabin09609032022-06-15 19:26:01 +00002177 std::map<audio_port_handle_t, bool> mClientSilencedStates;
Eric Laurent331679c2018-04-16 17:03:16 -07002178
2179 int32_t mNoCallbackWarningCount;
2180 static constexpr int32_t kMaxNoCallbackWarnings = 5;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002181};
2182
Andy Hung7aa7d102023-07-07 15:58:48 -07002183class MmapPlaybackThread : public MmapThread, public IAfMmapPlaybackThread,
2184 public virtual VolumeInterface {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002185public:
Andy Hung583043b2023-07-17 17:05:00 -07002186 MmapPlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
jiabinc52b1ff2019-10-31 17:20:42 -07002187 AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002188
Andy Hung7aa7d102023-07-07 15:58:48 -07002189 sp<IAfMmapPlaybackThread> asIAfMmapPlaybackThread() final {
2190 return sp<IAfMmapPlaybackThread>::fromExisting(this);
2191 }
2192
Andy Hung440901d2023-06-29 21:19:25 -07002193 void configure(const audio_attributes_t* attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08002194 audio_stream_type_t streamType,
2195 audio_session_t sessionId,
2196 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07002197 audio_port_handle_t deviceId,
Andy Hung440901d2023-06-29 21:19:25 -07002198 audio_port_handle_t portId) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002199
Andy Hung7aa7d102023-07-07 15:58:48 -07002200 AudioStreamOut* clearOutput() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002201
2202 // VolumeInterface
Andy Hung440901d2023-06-29 21:19:25 -07002203 void setMasterVolume(float value) final;
2204 void setMasterBalance(float /* value */) final {} // Needs implementation?
2205 void setMasterMute(bool muted) final;
2206 void setStreamVolume(audio_stream_type_t stream, float value) final;
2207 void setStreamMute(audio_stream_type_t stream, bool muted) final;
2208 float streamVolume(audio_stream_type_t stream) const final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002209
2210 void setMasterMute_l(bool muted) { mMasterMute = muted; }
2211
Andy Hung440901d2023-06-29 21:19:25 -07002212 void invalidateTracks(audio_stream_type_t streamType) final;
2213 void invalidateTracks(std::set<audio_port_handle_t>& portIds) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002214
Andy Hung440901d2023-06-29 21:19:25 -07002215 audio_stream_type_t streamType() const final { return mStreamType; }
2216 void checkSilentMode_l() final;
2217 void processVolume_l() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002218
Andy Hung440901d2023-06-29 21:19:25 -07002219 MetadataUpdate updateMetadata_l() final;
Kevin Rocard069c2712018-03-29 19:09:14 -07002220
Andy Hung440901d2023-06-29 21:19:25 -07002221 void toAudioPortConfig(struct audio_port_config* config) final;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07002222
Andy Hung440901d2023-06-29 21:19:25 -07002223 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final;
jiabinb7d8c5a2020-08-26 17:24:52 -07002224
Andy Hung440901d2023-06-29 21:19:25 -07002225 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002226 return !(mOutput == nullptr || mOutput->stream == nullptr);
2227 }
2228
Andy Hung440901d2023-06-29 21:19:25 -07002229 status_t reportData(const void* buffer, size_t frameCount) final;
jiabinfc791ee2023-02-15 19:43:40 +00002230
Andy Hung972bec12023-08-31 16:13:39 -07002231 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) final
2232 REQUIRES(audio_utils::AudioFlinger_Mutex);
2233 void stopMelComputation_l() final
2234 REQUIRES(audio_utils::AudioFlinger_Mutex);
Vlad Popa6fbbfbf2023-02-22 15:05:43 +01002235
Eric Laurent6acd1d42017-01-04 14:23:29 -08002236protected:
Andy Hung440901d2023-06-29 21:19:25 -07002237 void dumpInternals_l(int fd, const Vector<String16>& args) final;
Eric Laurent1f9b5e62023-07-03 18:14:07 +02002238 float streamVolume_l() const {
2239 return mStreamTypes[mStreamType].volume;
2240 }
2241 bool streamMuted_l() const {
2242 return mStreamTypes[mStreamType].mute;
2243 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002244
Eric Laurent1f9b5e62023-07-03 18:14:07 +02002245 stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurent6acd1d42017-01-04 14:23:29 -08002246 audio_stream_type_t mStreamType;
2247 float mMasterVolume;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002248 bool mMasterMute;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002249 AudioStreamOut* mOutput;
Vlad Popa6fbbfbf2023-02-22 15:05:43 +01002250
2251 mediautils::atomic_sp<audio_utils::MelProcessor> mMelProcessor;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002252};
2253
Andy Hung7aa7d102023-07-07 15:58:48 -07002254class MmapCaptureThread : public MmapThread, public IAfMmapCaptureThread
Eric Laurent6acd1d42017-01-04 14:23:29 -08002255{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002256public:
Andy Hung583043b2023-07-17 17:05:00 -07002257 MmapCaptureThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
jiabinc52b1ff2019-10-31 17:20:42 -07002258 AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002259
Andy Hung7aa7d102023-07-07 15:58:48 -07002260 sp<IAfMmapCaptureThread> asIAfMmapCaptureThread() final {
2261 return sp<IAfMmapCaptureThread>::fromExisting(this);
2262 }
2263
2264 AudioStreamIn* clearInput() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002265
Andy Hungc5007f82023-08-29 14:26:09 -07002266 status_t exitStandby_l() REQUIRES(mutex()) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002267
Andy Hung440901d2023-06-29 21:19:25 -07002268 MetadataUpdate updateMetadata_l() final;
2269 void processVolume_l() final;
2270 void setRecordSilenced(audio_port_handle_t portId, bool silenced) final;
Kevin Rocard069c2712018-03-29 19:09:14 -07002271
Andy Hung440901d2023-06-29 21:19:25 -07002272 void toAudioPortConfig(struct audio_port_config* config) final;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07002273
Andy Hung440901d2023-06-29 21:19:25 -07002274 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final;
jiabinb7d8c5a2020-08-26 17:24:52 -07002275
Andy Hung440901d2023-06-29 21:19:25 -07002276 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002277 return !(mInput == nullptr || mInput->stream == nullptr);
2278 }
2279
Eric Laurent6acd1d42017-01-04 14:23:29 -08002280protected:
2281
2282 AudioStreamIn* mInput;
2283};
jiabinc658e452022-10-21 20:52:21 +00002284
2285class BitPerfectThread : public MixerThread {
2286public:
Andy Hung583043b2023-07-17 17:05:00 -07002287 BitPerfectThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut *output,
jiabinc658e452022-10-21 20:52:21 +00002288 audio_io_handle_t id, bool systemReady);
2289
2290protected:
Andy Hung440901d2023-06-29 21:19:25 -07002291 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final;
2292 void threadLoop_mix() final;
jiabinc658e452022-10-21 20:52:21 +00002293
2294private:
2295 bool mIsBitPerfect;
jiabin76d94692022-12-15 21:51:21 +00002296 float mVolumeLeft = 0.f;
2297 float mVolumeRight = 0.f;
jiabinc658e452022-10-21 20:52:21 +00002298};
Andy Hunga5a7fc92023-06-23 19:27:19 -07002299
Andy Hungee58e4a2023-07-07 13:47:37 -07002300} // namespace android