blob: 63648dfa369a8c8aa875247f36fd864afbce39c4 [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef INCLUDING_FROM_AUDIOFLINGER_H
19 #error This header file should only be included from AudioFlinger.h
20#endif
21
Andy Hungbd72c542023-06-20 18:56:17 -070022public: // TODO(b/288339104) extract out of AudioFlinger class
Andy Hung4989d312023-06-29 21:19:25 -070023
24class AsyncCallbackThread;
25
26class ThreadBase : public virtual IAfThreadBase, public Thread {
Andy Hung3ff4b552023-06-26 19:20:57 -070027 // TODO(b/288339104) remove friends
28 friend class RecordTrack;
29 friend class Track;
30 friend class TrackBase;
Eric Laurent81784c32012-11-19 14:55:58 -080031public:
Glenn Kasten97b7b752014-09-28 13:04:24 -070032 static const char *threadTypeToString(type_t type);
33
Andy Hung44f27182023-07-06 20:56:16 -070034 AudioFlinger* audioFlinger() const final { return mAudioFlinger.get(); }
35
Eric Laurent81784c32012-11-19 14:55:58 -080036 ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
Andy Hungcf10d742020-04-28 15:38:24 -070037 type_t type, bool systemReady, bool isOut);
Andy Hung4989d312023-06-29 21:19:25 -070038 ~ThreadBase() override;
Eric Laurent81784c32012-11-19 14:55:58 -080039
Andy Hung4989d312023-06-29 21:19:25 -070040 status_t readyToRun() final;
41 void clearPowerManager() final;
Eric Laurent81784c32012-11-19 14:55:58 -080042
43 // base for record and playback
44 enum {
45 CFG_EVENT_IO,
Eric Laurent10351942014-05-08 18:49:52 -070046 CFG_EVENT_PRIO,
47 CFG_EVENT_SET_PARAMETER,
Eric Laurent1c333e22014-05-20 10:48:17 -070048 CFG_EVENT_CREATE_AUDIO_PATCH,
49 CFG_EVENT_RELEASE_AUDIO_PATCH,
jiabinc52b1ff2019-10-31 17:20:42 -070050 CFG_EVENT_UPDATE_OUT_DEVICE,
Eric Laurentb3f315a2021-07-13 15:09:05 +020051 CFG_EVENT_RESIZE_BUFFER,
Eric Laurent6f9534f2022-05-03 18:15:04 +020052 CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS,
53 CFG_EVENT_HAL_LATENCY_MODES_CHANGED,
Eric Laurent81784c32012-11-19 14:55:58 -080054 };
55
Eric Laurent10351942014-05-08 18:49:52 -070056 class ConfigEventData: public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080057 public:
Eric Laurent10351942014-05-08 18:49:52 -070058 virtual ~ConfigEventData() {}
Eric Laurent81784c32012-11-19 14:55:58 -080059
60 virtual void dump(char *buffer, size_t size) = 0;
Eric Laurent10351942014-05-08 18:49:52 -070061 protected:
62 ConfigEventData() {}
Eric Laurent81784c32012-11-19 14:55:58 -080063 };
64
Eric Laurent10351942014-05-08 18:49:52 -070065 // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
66 // 1. create SetParameterConfigEvent. This sets mWaitStatus in config event
67 // 2. Lock mLock
68 // 3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
69 // 4. sendConfigEvent_l() reads status from event->mStatus;
70 // 5. sendConfigEvent_l() returns status
71 // 6. Unlock
72 //
73 // Parameter sequence by server: threadLoop calling processConfigEvents_l():
74 // 1. Lock mLock
75 // 2. If there is an entry in mConfigEvents proceed ...
76 // 3. Read first entry in mConfigEvents
77 // 4. Remove first entry from mConfigEvents
78 // 5. Process
79 // 6. Set event->mStatus
80 // 7. event->mCond.signal
81 // 8. Unlock
Eric Laurent81784c32012-11-19 14:55:58 -080082
Eric Laurent10351942014-05-08 18:49:52 -070083 class ConfigEvent: public RefBase {
84 public:
Eric Laurentb3f315a2021-07-13 15:09:05 +020085 void dump(char *buffer, size_t size) {
86 snprintf(buffer, size, "Event type: %d\n", mType);
87 if (mData != nullptr) {
88 snprintf(buffer, size, "Data:\n");
89 mData->dump(buffer, size);
90 }
91 }
Eric Laurent10351942014-05-08 18:49:52 -070092
93 const int mType; // event type e.g. CFG_EVENT_IO
94 Mutex mLock; // mutex associated with mCond
95 Condition mCond; // condition for status return
96 status_t mStatus; // status communicated to sender
97 bool mWaitStatus; // true if sender is waiting for status
Eric Laurent72e3f392015-05-20 14:43:50 -070098 bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
Eric Laurent10351942014-05-08 18:49:52 -070099 sp<ConfigEventData> mData; // event specific parameter data
100
101 protected:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700102 explicit ConfigEvent(int type, bool requiresSystemReady = false) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700103 mType(type), mStatus(NO_ERROR), mWaitStatus(false),
104 mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
Eric Laurent10351942014-05-08 18:49:52 -0700105 };
106
107 class IoConfigEventData : public ConfigEventData {
108 public:
Mikhail Naganov88536df2021-07-26 17:30:29 -0700109 IoConfigEventData(audio_io_config_event_t event, pid_t pid,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700110 audio_port_handle_t portId) :
111 mEvent(event), mPid(pid), mPortId(portId) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800112
113 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200114 snprintf(buffer, size, "- IO event: event %d\n", mEvent);
Eric Laurent81784c32012-11-19 14:55:58 -0800115 }
116
Mikhail Naganov88536df2021-07-26 17:30:29 -0700117 const audio_io_config_event_t mEvent;
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700118 const pid_t mPid;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700119 const audio_port_handle_t mPortId;
Eric Laurent81784c32012-11-19 14:55:58 -0800120 };
121
Eric Laurent10351942014-05-08 18:49:52 -0700122 class IoConfigEvent : public ConfigEvent {
Eric Laurent81784c32012-11-19 14:55:58 -0800123 public:
Mikhail Naganov88536df2021-07-26 17:30:29 -0700124 IoConfigEvent(audio_io_config_event_t event, pid_t pid, audio_port_handle_t portId) :
Eric Laurent10351942014-05-08 18:49:52 -0700125 ConfigEvent(CFG_EVENT_IO) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700126 mData = new IoConfigEventData(event, pid, portId);
Eric Laurent10351942014-05-08 18:49:52 -0700127 }
Eric Laurent10351942014-05-08 18:49:52 -0700128 };
Eric Laurent81784c32012-11-19 14:55:58 -0800129
Eric Laurent10351942014-05-08 18:49:52 -0700130 class PrioConfigEventData : public ConfigEventData {
131 public:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800132 PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
133 mPid(pid), mTid(tid), mPrio(prio), mForApp(forApp) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800134
135 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200136 snprintf(buffer, size, "- Prio event: pid %d, tid %d, prio %d, for app? %d\n",
Mikhail Naganov83f04272017-02-07 10:45:09 -0800137 mPid, mTid, mPrio, mForApp);
Eric Laurent81784c32012-11-19 14:55:58 -0800138 }
139
Eric Laurent81784c32012-11-19 14:55:58 -0800140 const pid_t mPid;
141 const pid_t mTid;
142 const int32_t mPrio;
Mikhail Naganov83f04272017-02-07 10:45:09 -0800143 const bool mForApp;
Eric Laurent81784c32012-11-19 14:55:58 -0800144 };
145
Eric Laurent10351942014-05-08 18:49:52 -0700146 class PrioConfigEvent : public ConfigEvent {
147 public:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800148 PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700149 ConfigEvent(CFG_EVENT_PRIO, true) {
Mikhail Naganov83f04272017-02-07 10:45:09 -0800150 mData = new PrioConfigEventData(pid, tid, prio, forApp);
Eric Laurent10351942014-05-08 18:49:52 -0700151 }
Eric Laurent10351942014-05-08 18:49:52 -0700152 };
153
154 class SetParameterConfigEventData : public ConfigEventData {
155 public:
Andy Hung71ba4b32022-10-06 12:09:49 -0700156 explicit SetParameterConfigEventData(const String8& keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700157 mKeyValuePairs(keyValuePairs) {}
158
159 virtual void dump(char *buffer, size_t size) {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000160 snprintf(buffer, size, "- KeyValue: %s\n", mKeyValuePairs.c_str());
Eric Laurent10351942014-05-08 18:49:52 -0700161 }
162
163 const String8 mKeyValuePairs;
164 };
165
166 class SetParameterConfigEvent : public ConfigEvent {
167 public:
Andy Hung71ba4b32022-10-06 12:09:49 -0700168 explicit SetParameterConfigEvent(const String8& keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700169 ConfigEvent(CFG_EVENT_SET_PARAMETER) {
170 mData = new SetParameterConfigEventData(keyValuePairs);
171 mWaitStatus = true;
172 }
Eric Laurent10351942014-05-08 18:49:52 -0700173 };
174
Eric Laurent1c333e22014-05-20 10:48:17 -0700175 class CreateAudioPatchConfigEventData : public ConfigEventData {
176 public:
177 CreateAudioPatchConfigEventData(const struct audio_patch patch,
178 audio_patch_handle_t handle) :
179 mPatch(patch), mHandle(handle) {}
180
181 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200182 snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -0700183 }
184
185 const struct audio_patch mPatch;
186 audio_patch_handle_t mHandle;
187 };
188
189 class CreateAudioPatchConfigEvent : public ConfigEvent {
190 public:
191 CreateAudioPatchConfigEvent(const struct audio_patch patch,
192 audio_patch_handle_t handle) :
193 ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
194 mData = new CreateAudioPatchConfigEventData(patch, handle);
195 mWaitStatus = true;
196 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700197 };
198
199 class ReleaseAudioPatchConfigEventData : public ConfigEventData {
200 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700201 explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700202 mHandle(handle) {}
203
204 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200205 snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -0700206 }
207
208 audio_patch_handle_t mHandle;
209 };
210
211 class ReleaseAudioPatchConfigEvent : public ConfigEvent {
212 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700213 explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700214 ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
215 mData = new ReleaseAudioPatchConfigEventData(handle);
216 mWaitStatus = true;
217 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700218 };
Eric Laurent81784c32012-11-19 14:55:58 -0800219
jiabinc52b1ff2019-10-31 17:20:42 -0700220 class UpdateOutDevicesConfigEventData : public ConfigEventData {
221 public:
222 explicit UpdateOutDevicesConfigEventData(const DeviceDescriptorBaseVector& outDevices) :
223 mOutDevices(outDevices) {}
224
225 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200226 snprintf(buffer, size, "- Devices: %s", android::toString(mOutDevices).c_str());
jiabinc52b1ff2019-10-31 17:20:42 -0700227 }
228
229 DeviceDescriptorBaseVector mOutDevices;
230 };
231
232 class UpdateOutDevicesConfigEvent : public ConfigEvent {
233 public:
234 explicit UpdateOutDevicesConfigEvent(const DeviceDescriptorBaseVector& outDevices) :
235 ConfigEvent(CFG_EVENT_UPDATE_OUT_DEVICE) {
236 mData = new UpdateOutDevicesConfigEventData(outDevices);
237 }
jiabinc52b1ff2019-10-31 17:20:42 -0700238 };
239
Eric Laurentec376dc2021-04-08 20:41:22 +0200240 class ResizeBufferConfigEventData : public ConfigEventData {
241 public:
242 explicit ResizeBufferConfigEventData(int32_t maxSharedAudioHistoryMs) :
243 mMaxSharedAudioHistoryMs(maxSharedAudioHistoryMs) {}
244
245 virtual void dump(char *buffer, size_t size) {
Eric Laurentb3f315a2021-07-13 15:09:05 +0200246 snprintf(buffer, size, "- mMaxSharedAudioHistoryMs: %d", mMaxSharedAudioHistoryMs);
Eric Laurentec376dc2021-04-08 20:41:22 +0200247 }
248
249 int32_t mMaxSharedAudioHistoryMs;
250 };
251
252 class ResizeBufferConfigEvent : public ConfigEvent {
253 public:
254 explicit ResizeBufferConfigEvent(int32_t maxSharedAudioHistoryMs) :
255 ConfigEvent(CFG_EVENT_RESIZE_BUFFER) {
256 mData = new ResizeBufferConfigEventData(maxSharedAudioHistoryMs);
257 }
Eric Laurentec376dc2021-04-08 20:41:22 +0200258 };
259
Eric Laurentb3f315a2021-07-13 15:09:05 +0200260 class CheckOutputStageEffectsEvent : public ConfigEvent {
261 public:
262 CheckOutputStageEffectsEvent() :
263 ConfigEvent(CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS) {
264 }
Eric Laurentb3f315a2021-07-13 15:09:05 +0200265 };
266
Eric Laurent6f9534f2022-05-03 18:15:04 +0200267 class HalLatencyModesChangedEvent : public ConfigEvent {
268 public:
269 HalLatencyModesChangedEvent() :
270 ConfigEvent(CFG_EVENT_HAL_LATENCY_MODES_CHANGED) {
271 }
Eric Laurent6f9534f2022-05-03 18:15:04 +0200272 };
273
Eric Laurentb3f315a2021-07-13 15:09:05 +0200274
Eric Laurent81784c32012-11-19 14:55:58 -0800275 class PMDeathRecipient : public IBinder::DeathRecipient {
276 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700277 explicit PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800278 virtual ~PMDeathRecipient() {}
279
280 // IBinder::DeathRecipient
281 virtual void binderDied(const wp<IBinder>& who);
282
283 private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700284 DISALLOW_COPY_AND_ASSIGN(PMDeathRecipient);
Eric Laurent81784c32012-11-19 14:55:58 -0800285
286 wp<ThreadBase> mThread;
287 };
288
Andy Hung4989d312023-06-29 21:19:25 -0700289 type_t type() const final { return mType; }
290 bool isDuplicating() const final { return (mType == DUPLICATING); }
291 audio_io_handle_t id() const final { return mId;}
Eric Laurent81784c32012-11-19 14:55:58 -0800292
Andy Hung4989d312023-06-29 21:19:25 -0700293 uint32_t sampleRate() const final { return mSampleRate; }
294 audio_channel_mask_t channelMask() const final { return mChannelMask; }
295 audio_channel_mask_t mixerChannelMask() const override { return mChannelMask; }
296 audio_format_t format() const final { return mHALFormat; }
297 uint32_t channelCount() const final { return mChannelCount; }
298 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Andy Hung44f27182023-07-06 20:56:16 -0700299 uint32_t hapticChannelCount() const override { return 0; }
Andy Hung4989d312023-06-29 21:19:25 -0700300 uint32_t latency_l() const override { return 0; }
301 void setVolumeForOutput_l(float /* left */, float /* right */) const override {}
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700302
303 // Return's the HAL's frame count i.e. fast mixer buffer size.
Andy Hung4989d312023-06-29 21:19:25 -0700304 size_t frameCountHAL() const final { return mFrameCount; }
305 size_t frameSize() const final { return mFrameSize; }
Eric Laurent81784c32012-11-19 14:55:58 -0800306
307 // Should be "virtual status_t requestExitAndWait()" and override same
308 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
Andy Hung4989d312023-06-29 21:19:25 -0700309 void exit() final;
310 status_t setParameters(const String8& keyValuePairs) final;
311
Eric Laurent10351942014-05-08 18:49:52 -0700312 // sendConfigEvent_l() must be called with ThreadBase::mLock held
313 // Can temporarily release the lock if waiting for a reply from
314 // processConfigEvents_l().
Andy Hung4989d312023-06-29 21:19:25 -0700315 status_t sendConfigEvent_l(sp<ConfigEvent>& event);
316 void sendIoConfigEvent(audio_io_config_event_t event, pid_t pid = 0,
317 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
318 void sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid = 0,
319 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
320 void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) final;
321 void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp) final;
322 status_t sendSetParameterConfigEvent_l(const String8& keyValuePair) final;
323 status_t sendCreateAudioPatchConfigEvent(const struct audio_patch* patch,
324 audio_patch_handle_t* handle) final;
325 status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle) final;
326 status_t sendUpdateOutDeviceConfigEvent(
327 const DeviceDescriptorBaseVector& outDevices) final;
328 void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) final;
329 void sendCheckOutputStageEffectsEvent() final;
330 void sendCheckOutputStageEffectsEvent_l() final;
331 void sendHalLatencyModesChangedEvent_l() final;
Eric Laurentb3f315a2021-07-13 15:09:05 +0200332
Andy Hung4989d312023-06-29 21:19:25 -0700333 void processConfigEvents_l() final;
334 void setCheckOutputStageEffects() override {}
335 void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override;
336 void toAudioPortConfig(struct audio_port_config* config) override;
337 void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override;
Eric Laurent1c333e22014-05-20 10:48:17 -0700338
Andy Hung4989d312023-06-29 21:19:25 -0700339 // see note at declaration of mStandby, mOutDevice and mInDevice
340 bool inStandby() const override { return mStandby; }
341 const DeviceTypeSet outDeviceTypes() const final {
342 return getAudioDeviceTypes(mOutDeviceTypeAddrs);
343 }
344 audio_devices_t inDeviceType() const final { return mInDeviceTypeAddr.mType; }
345 DeviceTypeSet getDeviceTypes() const final {
346 return isOutput() ? outDeviceTypes() : DeviceTypeSet({inDeviceType()});
347 }
Eric Laurent81784c32012-11-19 14:55:58 -0800348
Andy Hung4989d312023-06-29 21:19:25 -0700349 const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const final {
350 return mOutDeviceTypeAddrs;
351 }
352 const AudioDeviceTypeAddr& inDeviceTypeAddr() const final {
353 return mInDeviceTypeAddr;
354 }
Andy Hung293558a2017-03-21 12:19:20 -0700355
Andy Hung4989d312023-06-29 21:19:25 -0700356 bool isOutput() const final { return mIsOut; }
jiabin8f278ee2019-11-11 12:16:27 -0800357
Andy Hung4989d312023-06-29 21:19:25 -0700358 bool isOffloadOrMmap() const final {
359 switch (mType) {
360 case OFFLOAD:
361 case MMAP_PLAYBACK:
362 case MMAP_CAPTURE:
363 return true;
364 default:
365 return false;
366 }
367 }
Eric Laurent81784c32012-11-19 14:55:58 -0800368
Andy Hung4989d312023-06-29 21:19:25 -0700369 sp<IAfEffectHandle> createEffect_l(
Andy Hungd65869f2023-06-27 17:05:02 -0700370 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700371 const sp<media::IEffectClient>& effectClient,
Eric Laurent81784c32012-11-19 14:55:58 -0800372 int32_t priority,
Glenn Kastend848eb42016-03-08 13:42:11 -0800373 audio_session_t sessionId,
Eric Laurent81784c32012-11-19 14:55:58 -0800374 effect_descriptor_t *desc,
375 int *enabled,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800376 status_t *status /*non-NULL*/,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700377 bool pinned,
Eric Laurentde8caf42021-08-11 17:19:25 +0200378 bool probe,
Andy Hung4989d312023-06-29 21:19:25 -0700379 bool notifyFramesProcessed) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800380
381 // return values for hasAudioSession (bit field)
382 enum effect_state {
383 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
384 // effect
Eric Laurent4c415062016-06-17 16:14:16 -0700385 TRACK_SESSION = 0x2, // the audio session corresponds to at least one
Eric Laurent81784c32012-11-19 14:55:58 -0800386 // track
Eric Laurentb62d0362021-10-26 17:40:18 +0200387 FAST_SESSION = 0x4, // the audio session corresponds to at least one
Eric Laurent4c415062016-06-17 16:14:16 -0700388 // fast track
jiabinc658e452022-10-21 20:52:21 +0000389 SPATIALIZED_SESSION = 0x8, // the audio session corresponds to at least one
390 // spatialized track
391 BIT_PERFECT_SESSION = 0x10 // the audio session corresponds to at least one
392 // bit-perfect track
Eric Laurent81784c32012-11-19 14:55:58 -0800393 };
394
Andy Hung4989d312023-06-29 21:19:25 -0700395 // get effect chain corresponding to session Id.
396 sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const final;
397 // same as getEffectChain() but must be called with ThreadBase mutex locked
398 sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const final;
399 std::vector<int> getEffectIds_l(audio_session_t sessionId) const final;
400
Eric Laurent81784c32012-11-19 14:55:58 -0800401 // lock all effect chains Mutexes. Must be called before releasing the
402 // ThreadBase mutex before processing the mixer and effects. This guarantees the
403 // integrity of the chains during the process.
404 // Also sets the parameter 'effectChains' to current value of mEffectChains.
Andy Hung4989d312023-06-29 21:19:25 -0700405 void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800406 // unlock effect chains after process
Andy Hung4989d312023-06-29 21:19:25 -0700407 void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) final;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800408 // get a copy of mEffectChains vector
Andy Hung4989d312023-06-29 21:19:25 -0700409 Vector<sp<IAfEffectChain>> getEffectChains_l() const final { return mEffectChains; };
Eric Laurent81784c32012-11-19 14:55:58 -0800410 // set audio mode to all effect chains
Andy Hung4989d312023-06-29 21:19:25 -0700411 void setMode(audio_mode_t mode) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800412 // get effect module with corresponding ID on specified audio session
Andy Hung4989d312023-06-29 21:19:25 -0700413 sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const final;
414 sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800415 // add and effect module. Also creates the effect chain is none exists for
Eric Laurent6c796322019-04-09 14:13:17 -0700416 // the effects audio session. Only called in a context of moving an effect
417 // from one thread to another
Andy Hung4989d312023-06-29 21:19:25 -0700418 status_t addEffect_l(const sp<IAfEffectModule>& effect) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800419 // remove and effect module. Also removes the effect chain is this was the last
420 // effect
Andy Hung4989d312023-06-29 21:19:25 -0700421 void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false) final;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800422 // disconnect an effect handle from module and destroy module if last handle
Andy Hung4989d312023-06-29 21:19:25 -0700423 void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800424 // detach all tracks connected to an auxiliary effect
Andy Hung4989d312023-06-29 21:19:25 -0700425 void detachAuxEffect_l(int /* effectId */) override {}
426 // TODO(b/288339104) - remove hasAudioSession_l below.
427 uint32_t hasAudioSession_l(audio_session_t sessionId) const override = 0;
428 uint32_t hasAudioSession(audio_session_t sessionId) const final {
Eric Laurent4c415062016-06-17 16:14:16 -0700429 Mutex::Autolock _l(mLock);
430 return hasAudioSession_l(sessionId);
431 }
432
Andy Hungc3d62f92019-03-14 13:38:51 -0700433 template <typename T>
434 uint32_t hasAudioSession_l(audio_session_t sessionId, const T& tracks) const {
435 uint32_t result = 0;
436 if (getEffectChain_l(sessionId) != 0) {
437 result = EFFECT_SESSION;
438 }
439 for (size_t i = 0; i < tracks.size(); ++i) {
Andy Hung3ff4b552023-06-26 19:20:57 -0700440 const sp<IAfTrackBase>& track = tracks[i];
Andy Hungc3d62f92019-03-14 13:38:51 -0700441 if (sessionId == track->sessionId()
442 && !track->isInvalid() // not yet removed from tracks.
443 && !track->isTerminated()) {
444 result |= TRACK_SESSION;
445 if (track->isFastTrack()) {
446 result |= FAST_SESSION; // caution, only represents first track.
447 }
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200448 if (track->isSpatialized()) {
Eric Laurentb62d0362021-10-26 17:40:18 +0200449 result |= SPATIALIZED_SESSION; // caution, only first track.
450 }
jiabinc658e452022-10-21 20:52:21 +0000451 if (track->isBitPerfect()) {
452 result |= BIT_PERFECT_SESSION;
453 }
Andy Hungc3d62f92019-03-14 13:38:51 -0700454 break;
455 }
456 }
457 return result;
458 }
459
Eric Laurent81784c32012-11-19 14:55:58 -0800460 // the value returned by default implementation is not important as the
461 // strategy is only meaningful for PlaybackThread which implements this method
Andy Hung4989d312023-06-29 21:19:25 -0700462 product_strategy_t getStrategyForSession_l(
463 audio_session_t /* sessionId */) const override {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800464 return static_cast<product_strategy_t>(0);
465 }
Eric Laurent81784c32012-11-19 14:55:58 -0800466
Eric Laurent81784c32012-11-19 14:55:58 -0800467 // check if some effects must be suspended/restored when an effect is enabled
468 // or disabled
Andy Hung4989d312023-06-29 21:19:25 -0700469 void checkSuspendOnEffectEnabled(bool enabled,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800470 audio_session_t sessionId,
Andy Hung4989d312023-06-29 21:19:25 -0700471 bool threadLocked) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800472
Eric Laurent81784c32012-11-19 14:55:58 -0800473
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700474 // Return a reference to a per-thread heap which can be used to allocate IMemory
475 // objects that will be read-only to client processes, read/write to mediaserver,
476 // and shared by all client processes of the thread.
477 // The heap is per-thread rather than common across all threads, because
478 // clients can't be trusted not to modify the offset of the IMemory they receive.
479 // If a thread does not have such a heap, this method returns 0.
Andy Hung4989d312023-06-29 21:19:25 -0700480 sp<MemoryDealer> readOnlyHeap() const override { return nullptr; }
Eric Laurent81784c32012-11-19 14:55:58 -0800481
Andy Hung4989d312023-06-29 21:19:25 -0700482 sp<IMemory> pipeMemory() const override { return nullptr; }
Glenn Kasten6181ffd2014-05-13 10:41:52 -0700483
Andy Hung4989d312023-06-29 21:19:25 -0700484 void systemReady() final;
Eric Laurent72e3f392015-05-20 14:43:50 -0700485
Andy Hung4989d312023-06-29 21:19:25 -0700486 void broadcast_l() final;
Eric Laurent4c415062016-06-17 16:14:16 -0700487
Andy Hung4989d312023-06-29 21:19:25 -0700488 bool isTimestampCorrectionEnabled() const override { return false; }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800489
Andy Hung4989d312023-06-29 21:19:25 -0700490 bool isMsdDevice() const final { return mIsMsdDevice; }
Andy Hungc8fddf32018-08-08 18:32:37 -0700491
Andy Hung4989d312023-06-29 21:19:25 -0700492 void dump(int fd, const Vector<String16>& args) override;
Andy Hungdc099c22018-09-18 13:46:39 -0700493
Andy Hungd0979812019-02-21 15:51:44 -0800494 // deliver stats to mediametrics.
Andy Hung4989d312023-06-29 21:19:25 -0700495 void sendStatistics(bool force) final;
Andy Hungd0979812019-02-21 15:51:44 -0800496
Andy Hung4989d312023-06-29 21:19:25 -0700497 Mutex& mutex() const final {
498 return mLock;
499 }
Eric Laurent81784c32012-11-19 14:55:58 -0800500 mutable Mutex mLock;
501
Andy Hung4989d312023-06-29 21:19:25 -0700502 void onEffectEnable(const sp<IAfEffectModule>& effect) final;
503 void onEffectDisable() final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800504
jiabineb3bda02020-06-30 14:07:03 -0700505 // invalidateTracksForAudioSession_l must be called with holding mLock.
Andy Hung4989d312023-06-29 21:19:25 -0700506 void invalidateTracksForAudioSession_l(audio_session_t /* sessionId */) const override {}
jiabineb3bda02020-06-30 14:07:03 -0700507 // Invalidate all the tracks with the given audio session.
Andy Hung4989d312023-06-29 21:19:25 -0700508 void invalidateTracksForAudioSession(audio_session_t sessionId) const final {
jiabineb3bda02020-06-30 14:07:03 -0700509 Mutex::Autolock _l(mLock);
510 invalidateTracksForAudioSession_l(sessionId);
511 }
512
513 template <typename T>
514 void invalidateTracksForAudioSession_l(audio_session_t sessionId,
515 const T& tracks) const {
516 for (size_t i = 0; i < tracks.size(); ++i) {
Andy Hung3ff4b552023-06-26 19:20:57 -0700517 const sp<IAfTrackBase>& track = tracks[i];
jiabineb3bda02020-06-30 14:07:03 -0700518 if (sessionId == track->sessionId()) {
519 track->invalidate();
520 }
521 }
522 }
523
Andy Hung4989d312023-06-29 21:19:25 -0700524 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override;
525 void stopMelComputation_l() override;
Vlad Popa6fbbfbf2023-02-22 15:05:43 +0100526
Eric Laurent81784c32012-11-19 14:55:58 -0800527protected:
528
529 // entry describing an effect being suspended in mSuspendedSessions keyed vector
530 class SuspendedSessionDesc : public RefBase {
531 public:
532 SuspendedSessionDesc() : mRefCount(0) {}
533
534 int mRefCount; // number of active suspend requests
535 effect_uuid_t mType; // effect type UUID
536 };
537
Andy Hungdae27702016-10-31 14:01:16 -0700538 void acquireWakeLock();
539 virtual void acquireWakeLock_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800540 void releaseWakeLock();
541 void releaseWakeLock_l();
Andy Hungd01b0f12016-11-07 16:10:30 -0800542 void updateWakeLockUids_l(const SortedVector<uid_t> &uids);
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800543 void getPowerManager_l();
Eric Laurentd8365c52017-07-16 15:27:05 -0700544 // suspend or restore effects of the specified type (or all if type is NULL)
545 // on a given session. The number of suspend requests is counted and restore
546 // occurs when all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800547 void setEffectSuspended_l(const effect_uuid_t *type,
548 bool suspend,
Andy Hung44f27182023-07-06 20:56:16 -0700549 audio_session_t sessionId) final;
Eric Laurentd8365c52017-07-16 15:27:05 -0700550 // updated mSuspendedSessions when an effect is suspended or restored
Eric Laurent81784c32012-11-19 14:55:58 -0800551 void updateSuspendedSessions_l(const effect_uuid_t *type,
552 bool suspend,
Glenn Kastend848eb42016-03-08 13:42:11 -0800553 audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800554 // check if some effects must be suspended when an effect chain is added
Andy Hungbd72c542023-06-20 18:56:17 -0700555 void checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain);
Eric Laurent81784c32012-11-19 14:55:58 -0800556
Kevin Rocard069c2712018-03-29 19:09:14 -0700557 // sends the metadata of the active tracks to the HAL
Vlad Popa7e81cea2023-01-19 16:34:16 +0100558 struct MetadataUpdate {
559 std::vector<playback_track_metadata_v7_t> playbackMetadataUpdate;
560 std::vector<record_track_metadata_v7_t> recordMetadataUpdate;
561 };
562 virtual MetadataUpdate updateMetadata_l() = 0;
Kevin Rocard069c2712018-03-29 19:09:14 -0700563
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100564 String16 getWakeLockTag();
565
Eric Laurent81784c32012-11-19 14:55:58 -0800566 virtual void preExit() { }
Andy Hung2ddee192015-12-18 17:34:44 -0800567 virtual void setMasterMono_l(bool mono __unused) { }
568 virtual bool requireMonoBlend() { return false; }
Eric Laurent81784c32012-11-19 14:55:58 -0800569
Andy Hung1c86ebe2018-05-29 20:29:08 -0700570 // called within the threadLoop to obtain timestamp from the HAL.
571 virtual status_t threadloop_getHalTimestamp_l(
572 ExtendedTimestamp *timestamp __unused) const {
573 return INVALID_OPERATION;
574 }
Andy Hungbd72c542023-06-20 18:56:17 -0700575public:
576// TODO(b/288339104) organize with publics
Eric Laurentd66d7a12021-07-13 13:35:32 +0200577 product_strategy_t getStrategyForStream(audio_stream_type_t stream) const;
Andy Hungbd72c542023-06-20 18:56:17 -0700578protected:
Eric Laurentd66d7a12021-07-13 13:35:32 +0200579
Eric Laurentb0463942022-12-20 16:31:10 +0100580 virtual void onHalLatencyModesChanged_l() {}
581
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700582 virtual void dumpInternals_l(int fd __unused, const Vector<String16>& args __unused)
583 { }
584 virtual void dumpTracks_l(int fd __unused, const Vector<String16>& args __unused) { }
585
586
Vlad Popae8d99472022-06-30 16:02:48 +0200587 friend class AudioFlinger; // for mEffectChains and mAudioManager
Eric Laurent81784c32012-11-19 14:55:58 -0800588
589 const type_t mType;
590
591 // Used by parameters, config events, addTrack_l, exit
592 Condition mWaitWorkCV;
593
594 const sp<AudioFlinger> mAudioFlinger;
Andy Hungcf10d742020-04-28 15:38:24 -0700595 ThreadMetrics mThreadMetrics;
596 const bool mIsOut;
Glenn Kasten9b58f632013-07-16 11:37:48 -0700597
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800598 // updated by PlaybackThread::readOutputParameters_l() or
599 // RecordThread::readInputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -0800600 uint32_t mSampleRate;
601 size_t mFrameCount; // output HAL, direct output, record
Eric Laurent81784c32012-11-19 14:55:58 -0800602 audio_channel_mask_t mChannelMask;
Glenn Kastenf6ed4232013-07-16 11:16:27 -0700603 uint32_t mChannelCount;
Eric Laurent81784c32012-11-19 14:55:58 -0800604 size_t mFrameSize;
Glenn Kasten97b7b752014-09-28 13:04:24 -0700605 // not HAL frame size, this is for output sink (to pipe to fast mixer)
Andy Hung463be252014-07-10 16:56:07 -0700606 audio_format_t mFormat; // Source format for Recording and
607 // Sink format for Playback.
608 // Sink format may be different than
609 // HAL format if Fastmixer is used.
610 audio_format_t mHALFormat;
Glenn Kasten70949c42013-08-06 07:40:12 -0700611 size_t mBufferSize; // HAL buffer size for read() or write()
jiabinc52b1ff2019-10-31 17:20:42 -0700612 AudioDeviceTypeAddrVector mOutDeviceTypeAddrs; // output device types and addresses
613 AudioDeviceTypeAddr mInDeviceTypeAddr; // input device type and address
Eric Laurent10351942014-05-08 18:49:52 -0700614 Vector< sp<ConfigEvent> > mConfigEvents;
Eric Laurent72e3f392015-05-20 14:43:50 -0700615 Vector< sp<ConfigEvent> > mPendingConfigEvents; // events awaiting system ready
Eric Laurent81784c32012-11-19 14:55:58 -0800616
617 // These fields are written and read by thread itself without lock or barrier,
jiabinc52b1ff2019-10-31 17:20:42 -0700618 // and read by other threads without lock or barrier via standby(), outDeviceTypes()
619 // and inDeviceType().
Eric Laurent81784c32012-11-19 14:55:58 -0800620 // Because of the absence of a lock or barrier, any other thread that reads
621 // these fields must use the information in isolation, or be prepared to deal
622 // with possibility that it might be inconsistent with other information.
Glenn Kasten4944acb2013-08-19 08:39:20 -0700623 bool mStandby; // Whether thread is currently in standby.
jiabinc52b1ff2019-10-31 17:20:42 -0700624
Eric Laurent296fb132015-05-01 11:38:42 -0700625 struct audio_patch mPatch;
jiabinc52b1ff2019-10-31 17:20:42 -0700626
Glenn Kastenf59497b2015-01-26 16:35:47 -0800627 audio_source_t mAudioSource;
Eric Laurent81784c32012-11-19 14:55:58 -0800628
629 const audio_io_handle_t mId;
Andy Hungbd72c542023-06-20 18:56:17 -0700630 Vector<sp<IAfEffectChain>> mEffectChains;
Eric Laurent81784c32012-11-19 14:55:58 -0800631
Glenn Kastend7dca052015-03-05 16:05:54 -0800632 static const int kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
633 char mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
Chris Ye6597d732020-02-28 22:38:25 -0800634 sp<os::IPowerManager> mPowerManager;
Eric Laurent81784c32012-11-19 14:55:58 -0800635 sp<IBinder> mWakeLockToken;
636 const sp<PMDeathRecipient> mDeathRecipient;
Glenn Kastend848eb42016-03-08 13:42:11 -0800637 // list of suspended effects per session and per type. The first (outer) vector is
638 // keyed by session ID, the second (inner) by type UUID timeLow field
Eric Laurentd8365c52017-07-16 15:27:05 -0700639 // Updated by updateSuspendedSessions_l() only.
Glenn Kastend848eb42016-03-08 13:42:11 -0800640 KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
Eric Laurent81784c32012-11-19 14:55:58 -0800641 mSuspendedSessions;
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700642 // TODO: add comment and adjust size as needed
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800643 static const size_t kLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800644 sp<NBLog::Writer> mNBLogWriter;
Eric Laurent72e3f392015-05-20 14:43:50 -0700645 bool mSystemReady;
Andy Hung818e7a32016-02-16 18:08:07 -0800646 ExtendedTimestamp mTimestamp;
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700647 TimestampVerifier< // For timestamp statistics.
648 int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier;
Dean Wheatley12473e92021-03-18 23:00:55 +1100649 // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
650 // TODO: add confirmation checks:
651 // 1) DIRECT threads and linear PCM format really resets to 0?
652 // 2) Is frame count really valid if not linear pcm?
653 // 3) Are all 64 bits of position returned, not just lowest 32 bits?
jiabinc52b1ff2019-10-31 17:20:42 -0700654 // Timestamp corrected device should be a single device.
655 audio_devices_t mTimestampCorrectedDevice = AUDIO_DEVICE_NONE;
Andy Hung446f4df2019-02-21 12:26:41 -0800656
657 // ThreadLoop statistics per iteration.
658 int64_t mLastIoBeginNs = -1;
659 int64_t mLastIoEndNs = -1;
660
Andy Hung44d648b2022-04-08 17:33:40 -0700661 // ThreadSnapshot is thread-safe (internally locked)
662 mediautils::ThreadSnapshot mThreadSnapshot;
663
Andy Hung446f4df2019-02-21 12:26:41 -0800664 // This should be read under ThreadBase lock (if not on the threadLoop thread).
665 audio_utils::Statistics<double> mIoJitterMs{0.995 /* alpha */};
666 audio_utils::Statistics<double> mProcessTimeMs{0.995 /* alpha */};
Andy Hunge6c37112019-02-26 17:38:10 -0800667 audio_utils::Statistics<double> mLatencyMs{0.995 /* alpha */};
Robert Wu06db0a32021-08-10 19:05:34 +0000668 audio_utils::Statistics<double> mMonopipePipeDepthStats{0.999 /* alpha */};
Andy Hung446f4df2019-02-21 12:26:41 -0800669
Andy Hungd0979812019-02-21 15:51:44 -0800670 // Save the last count when we delivered statistics to mediametrics.
671 int64_t mLastRecordedTimestampVerifierN = 0;
672 int64_t mLastRecordedTimeNs = 0; // BOOTTIME to include suspend.
673
Andy Hungc8fddf32018-08-08 18:32:37 -0700674 bool mIsMsdDevice = false;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800675 // A condition that must be evaluated by the thread loop has changed and
676 // we must not wait for async write callback in the thread loop before evaluating it
677 bool mSignalPending;
Andy Hungdae27702016-10-31 14:01:16 -0700678
Andy Hung8946a282018-04-19 20:04:56 -0700679#ifdef TEE_SINK
680 NBAIO_Tee mTee;
681#endif
Andy Hungdae27702016-10-31 14:01:16 -0700682 // ActiveTracks is a sorted vector of track type T representing the
683 // active tracks of threadLoop() to be considered by the locked prepare portion.
684 // ActiveTracks should be accessed with the ThreadBase lock held.
685 //
686 // During processing and I/O, the threadLoop does not hold the lock;
687 // hence it does not directly use ActiveTracks. Care should be taken
688 // to hold local strong references or defer removal of tracks
689 // if the threadLoop may still be accessing those tracks due to mix, etc.
690 //
691 // This class updates power information appropriately.
692 //
693
694 template <typename T>
695 class ActiveTracks {
696 public:
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700697 explicit ActiveTracks(SimpleLog *localLog = nullptr)
Andy Hungdae27702016-10-31 14:01:16 -0700698 : mActiveTracksGeneration(0)
699 , mLastActiveTracksGeneration(0)
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700700 , mLocalLog(localLog)
Andy Hungdae27702016-10-31 14:01:16 -0700701 { }
702
703 ~ActiveTracks() {
704 ALOGW_IF(!mActiveTracks.isEmpty(),
705 "ActiveTracks should be empty in destructor");
706 }
707 // returns the last track added (even though it may have been
708 // subsequently removed from ActiveTracks).
709 //
710 // Used for DirectOutputThread to ensure a flush is called when transitioning
711 // to a new track (even though it may be on the same session).
712 // Used for OffloadThread to ensure that volume and mixer state is
713 // taken from the latest track added.
714 //
715 // The latest track is saved with a weak pointer to prevent keeping an
716 // otherwise useless track alive. Thus the function will return nullptr
717 // if the latest track has subsequently been removed and destroyed.
718 sp<T> getLatest() {
719 return mLatestActiveTrack.promote();
720 }
721
722 // SortedVector methods
723 ssize_t add(const sp<T> &track);
724 ssize_t remove(const sp<T> &track);
725 size_t size() const {
726 return mActiveTracks.size();
727 }
Eric Tan39ec8d62018-07-24 09:49:29 -0700728 bool isEmpty() const {
729 return mActiveTracks.isEmpty();
730 }
Andy Hung44f27182023-07-06 20:56:16 -0700731 ssize_t indexOf(const sp<T>& item) const {
Andy Hungdae27702016-10-31 14:01:16 -0700732 return mActiveTracks.indexOf(item);
733 }
734 sp<T> operator[](size_t index) const {
735 return mActiveTracks[index];
736 }
737 typename SortedVector<sp<T>>::iterator begin() {
738 return mActiveTracks.begin();
739 }
740 typename SortedVector<sp<T>>::iterator end() {
741 return mActiveTracks.end();
742 }
743
744 // Due to Binder recursion optimization, clear() and updatePowerState()
745 // cannot be called from a Binder thread because they may call back into
746 // the original calling process (system server) for BatteryNotifier
747 // (which requires a Java environment that may not be present).
748 // Hence, call clear() and updatePowerState() only from the
749 // ThreadBase thread.
750 void clear();
751 // periodically called in the threadLoop() to update power state uids.
Andy Hung71ba4b32022-10-06 12:09:49 -0700752 void updatePowerState(const sp<ThreadBase>& thread, bool force = false);
Andy Hungdae27702016-10-31 14:01:16 -0700753
Kevin Rocardc86a7f72018-04-03 09:00:09 -0700754 /** @return true if one or move active tracks was added or removed since the
Jasmine Chaeaa10e42021-05-11 10:11:14 +0800755 * last time this function was called or the vector was created.
756 * true if volume of one of active tracks was changed.
757 */
Kevin Rocard069c2712018-03-29 19:09:14 -0700758 bool readAndClearHasChanged();
759
Eric Laurentdda206a2022-07-08 17:28:35 +0200760 /** Force updating track metadata to audio HAL stream next time
761 * readAndClearHasChanged() is called.
762 */
763 void setHasChanged() { mHasChanged = true; }
764
Andy Hungdae27702016-10-31 14:01:16 -0700765 private:
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700766 void logTrack(const char *funcName, const sp<T> &track) const;
767
Andy Hungd01b0f12016-11-07 16:10:30 -0800768 SortedVector<uid_t> getWakeLockUids() {
769 SortedVector<uid_t> wakeLockUids;
Andy Hungdae27702016-10-31 14:01:16 -0700770 for (const sp<T> &track : mActiveTracks) {
771 wakeLockUids.add(track->uid());
772 }
773 return wakeLockUids; // moved by underlying SharedBuffer
774 }
775
776 std::map<uid_t, std::pair<ssize_t /* previous */, ssize_t /* current */>>
777 mBatteryCounter;
778 SortedVector<sp<T>> mActiveTracks;
779 int mActiveTracksGeneration;
780 int mLastActiveTracksGeneration;
781 wp<T> mLatestActiveTrack; // latest track added to ActiveTracks
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700782 SimpleLog * const mLocalLog;
Kevin Rocardc86a7f72018-04-03 09:00:09 -0700783 // If the vector has changed since last call to readAndClearHasChanged
Kevin Rocard069c2712018-03-29 19:09:14 -0700784 bool mHasChanged = false;
Andy Hungdae27702016-10-31 14:01:16 -0700785 };
Andy Hung293558a2017-03-21 12:19:20 -0700786
787 SimpleLog mLocalLog;
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700788
789private:
790 void dumpBase_l(int fd, const Vector<String16>& args);
791 void dumpEffectChains_l(int fd, const Vector<String16>& args);
Eric Laurent81784c32012-11-19 14:55:58 -0800792};
793
794// --- PlaybackThread ---
Andy Hung4989d312023-06-29 21:19:25 -0700795class PlaybackThread : public ThreadBase, public virtual IAfPlaybackThread,
796 public StreamOutHalInterfaceCallback,
Andy Hung44f27182023-07-06 20:56:16 -0700797 public virtual VolumeInterface, public StreamOutHalInterfaceEventCallback {
Andy Hung3ff4b552023-06-26 19:20:57 -0700798 // TODO(b/288339104) remove friends
799 friend class OutputTrack;
800 friend class Track;
Eric Laurent81784c32012-11-19 14:55:58 -0800801public:
Andy Hung44f27182023-07-06 20:56:16 -0700802 sp<IAfPlaybackThread> asIAfPlaybackThread() final {
803 return sp<IAfPlaybackThread>::fromExisting(this);
804 }
Eric Laurent81784c32012-11-19 14:55:58 -0800805
Eric Laurente93cc032016-05-05 10:15:10 -0700806 // retry count before removing active track in case of underrun on offloaded thread:
807 // we need to make sure that AudioTrack client has enough time to send large buffers
808 //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
809 // handled for offloaded tracks
810 static const int8_t kMaxTrackRetriesOffload = 20;
811 static const int8_t kMaxTrackStartupRetriesOffload = 100;
Andy Hung8ed196a2018-01-05 13:21:11 -0800812 static constexpr uint32_t kMaxTracksPerUid = 40;
Andy Hung1bc088a2018-02-09 15:57:31 -0800813 static constexpr size_t kMaxTracks = 256;
Eric Laurente93cc032016-05-05 10:15:10 -0700814
rago1bb90822017-05-02 18:31:48 -0700815 // Maximum delay (in nanoseconds) for upcoming buffers in suspend mode, otherwise
816 // if delay is greater, the estimated time for timeLoopNextNs is reset.
817 // This allows for catch-up to be done for small delays, while resetting the estimate
818 // for initial conditions or large delays.
819 static const nsecs_t kMaxNextBufferDelayNs = 100000000;
820
Eric Laurent81784c32012-11-19 14:55:58 -0800821 PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200822 audio_io_handle_t id, type_t type, bool systemReady,
823 audio_config_base_t *mixerConfig = nullptr);
Andy Hung4989d312023-06-29 21:19:25 -0700824 ~PlaybackThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800825
Eric Laurent81784c32012-11-19 14:55:58 -0800826 // Thread virtuals
Andy Hung4989d312023-06-29 21:19:25 -0700827 bool threadLoop() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800828
829 // RefBase
Andy Hung4989d312023-06-29 21:19:25 -0700830 void onFirstRef() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800831
Andy Hung4989d312023-06-29 21:19:25 -0700832 status_t checkEffectCompatibility_l(
833 const effect_descriptor_t* desc, audio_session_t sessionId) final;
Eric Laurent4c415062016-06-17 16:14:16 -0700834
Andy Hung44f27182023-07-06 20:56:16 -0700835 void addOutputTrack_l(const sp<IAfTrack>& track) final {
836 mTracks.add(track);
837 }
838
Eric Laurent81784c32012-11-19 14:55:58 -0800839protected:
840 // Code snippets that were lifted up out of threadLoop()
841 virtual void threadLoop_mix() = 0;
842 virtual void threadLoop_sleepTime() = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800843 virtual ssize_t threadLoop_write();
844 virtual void threadLoop_drain();
Eric Laurent81784c32012-11-19 14:55:58 -0800845 virtual void threadLoop_standby();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800846 virtual void threadLoop_exit();
Andy Hung3ff4b552023-06-26 19:20:57 -0700847 virtual void threadLoop_removeTracks(const Vector<sp<IAfTrack>>& tracksToRemove);
Eric Laurent81784c32012-11-19 14:55:58 -0800848
849 // prepareTracks_l reads and writes mActiveTracks, and returns
850 // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller
851 // is responsible for clearing or destroying this Vector later on, when it
852 // is safe to do so. That will drop the final ref count and destroy the tracks.
Andy Hung3ff4b552023-06-26 19:20:57 -0700853 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) = 0;
854 void removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove);
Eric Laurenteab90452019-06-24 15:17:46 -0700855 status_t handleVoipVolume_l(float *volume);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800856
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700857 // StreamOutHalInterfaceCallback implementation
858 virtual void onWriteReady();
859 virtual void onDrainReady();
860 virtual void onError();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800861
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700862 void resetWriteBlocked(uint32_t sequence);
863 void resetDraining(uint32_t sequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800864
865 virtual bool waitingAsyncCallback();
866 virtual bool waitingAsyncCallback_l();
867 virtual bool shouldStandby_l();
Haynes Mathew George4c6a4332014-01-15 12:31:39 -0800868 virtual void onAddNewTrack_l();
Haynes Mathew George4527b9e2016-07-07 19:54:17 -0700869 void onAsyncError(); // error reported by AsyncCallbackThread
Eric Laurent81784c32012-11-19 14:55:58 -0800870
jiabinf6eb4c32020-02-25 14:06:25 -0800871 // StreamHalInterfaceCodecFormatCallback implementation
872 void onCodecFormatChanged(
Andy Hung4989d312023-06-29 21:19:25 -0700873 const std::basic_string<uint8_t>& metadataBs) final;
jiabinf6eb4c32020-02-25 14:06:25 -0800874
Eric Laurent81784c32012-11-19 14:55:58 -0800875 // ThreadBase virtuals
876 virtual void preExit();
877
Eric Laurent64667972016-03-30 18:19:46 -0700878 virtual bool keepWakeLock() const { return true; }
Andy Hungdae27702016-10-31 14:01:16 -0700879 virtual void acquireWakeLock_l() {
880 ThreadBase::acquireWakeLock_l();
881 mActiveTracks.updatePowerState(this, true /* force */);
882 }
Eric Laurent64667972016-03-30 18:19:46 -0700883
Eric Laurentb3f315a2021-07-13 15:09:05 +0200884 virtual void checkOutputStageEffects() {}
Eric Laurent6f9534f2022-05-03 18:15:04 +0200885 virtual void setHalLatencyMode_l() {}
886
Eric Laurentb3f315a2021-07-13 15:09:05 +0200887
Andy Hung4989d312023-06-29 21:19:25 -0700888 void dumpInternals_l(int fd, const Vector<String16>& args) override;
889 void dumpTracks_l(int fd, const Vector<String16>& args) final;
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -0700890
Eric Laurent81784c32012-11-19 14:55:58 -0800891public:
892
Andy Hung4989d312023-06-29 21:19:25 -0700893 status_t initCheck() const final { return mOutput == nullptr ? NO_INIT : NO_ERROR; }
Eric Laurent81784c32012-11-19 14:55:58 -0800894
895 // return estimated latency in milliseconds, as reported by HAL
Andy Hung4989d312023-06-29 21:19:25 -0700896 uint32_t latency() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800897 // same, but lock must already be held
Andy Hung4989d312023-06-29 21:19:25 -0700898 uint32_t latency_l() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800899
Eric Laurent6acd1d42017-01-04 14:23:29 -0800900 // VolumeInterface
Andy Hung4989d312023-06-29 21:19:25 -0700901 void setMasterVolume(float value) final;
902 void setMasterBalance(float balance) override;
903 void setMasterMute(bool muted) final;
904 void setStreamVolume(audio_stream_type_t stream, float value) final;
905 void setStreamMute(audio_stream_type_t stream, bool muted) final;
906 float streamVolume(audio_stream_type_t stream) const final;
907 void setVolumeForOutput_l(float left, float right) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800908
Andy Hung4989d312023-06-29 21:19:25 -0700909 sp<IAfTrack> createTrack_l(
Andy Hungd65869f2023-06-27 17:05:02 -0700910 const sp<Client>& client,
Eric Laurent81784c32012-11-19 14:55:58 -0800911 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700912 const audio_attributes_t& attr,
Eric Laurent21da6472017-11-09 16:29:26 -0800913 uint32_t *sampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -0800914 audio_format_t format,
915 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800916 size_t *pFrameCount,
Eric Laurent21da6472017-11-09 16:29:26 -0800917 size_t *pNotificationFrameCount,
918 uint32_t notificationsPerBuffer,
919 float speed,
Eric Laurent81784c32012-11-19 14:55:58 -0800920 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -0800921 audio_session_t sessionId,
Eric Laurent05067782016-06-01 18:27:28 -0700922 audio_output_flags_t *flags,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700923 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +0000924 const AttributionSourceState& attributionSource,
Eric Laurent81784c32012-11-19 14:55:58 -0800925 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800926 status_t *status /*non-NULL*/,
jiabinf6eb4c32020-02-25 14:06:25 -0800927 audio_port_handle_t portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200928 const sp<media::IAudioTrackCallback>& callback,
jiabinc658e452022-10-21 20:52:21 +0000929 bool isSpatialized,
Andy Hung4989d312023-06-29 21:19:25 -0700930 bool isBitPerfect) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800931
Andy Hung44f27182023-07-06 20:56:16 -0700932 bool isTrackActive(const sp<IAfTrack>& track) const final {
933 return mActiveTracks.indexOf(track) >= 0;
934 }
935
936 AudioStreamOut* getOutput_l() const final { return mOutput; }
Andy Hung4989d312023-06-29 21:19:25 -0700937 AudioStreamOut* getOutput() const final;
938 AudioStreamOut* clearOutput() final;
939 sp<StreamHalInterface> stream() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800940
941 // a very large number of suspend() will eventually wraparound, but unlikely
Andy Hung4989d312023-06-29 21:19:25 -0700942 void suspend() final { (void) android_atomic_inc(&mSuspended); }
943 void restore() final
Eric Laurent81784c32012-11-19 14:55:58 -0800944 {
945 // if restore() is done without suspend(), get back into
946 // range so that the next suspend() will operate correctly
947 if (android_atomic_dec(&mSuspended) <= 0) {
948 android_atomic_release_store(0, &mSuspended);
949 }
950 }
Andy Hung4989d312023-06-29 21:19:25 -0700951 bool isSuspended() const final
Eric Laurent81784c32012-11-19 14:55:58 -0800952 { return android_atomic_acquire_load(&mSuspended) > 0; }
953
Andy Hung4989d312023-06-29 21:19:25 -0700954 String8 getParameters(const String8& keys);
955 void ioConfigChanged(audio_io_config_event_t event, pid_t pid = 0,
956 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
957 status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const final;
Andy Hung010a1a12014-03-13 13:57:33 -0700958 // Consider also removing and passing an explicit mMainBuffer initialization
Andy Hung3ff4b552023-06-26 19:20:57 -0700959 // parameter to AF::IAfTrack::Track().
Andy Hung4989d312023-06-29 21:19:25 -0700960 float* sinkBuffer() const final {
Andy Hung319587b2023-05-23 14:01:03 -0700961 return reinterpret_cast<float *>(mSinkBuffer); };
Eric Laurent81784c32012-11-19 14:55:58 -0800962
Andy Hung4989d312023-06-29 21:19:25 -0700963 void detachAuxEffect_l(int effectId) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800964
Andy Hung4989d312023-06-29 21:19:25 -0700965 status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId) final;
966 status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId) final;
967
968 status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final;
969 size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final;
970 uint32_t hasAudioSession_l(audio_session_t sessionId) const final {
Andy Hungc3d62f92019-03-14 13:38:51 -0700971 return ThreadBase::hasAudioSession_l(sessionId, mTracks);
972 }
Andy Hung4989d312023-06-29 21:19:25 -0700973 product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800974
975
Andy Hung4989d312023-06-29 21:19:25 -0700976 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final;
977 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final;
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700978
979 // called with AudioFlinger lock held
Andy Hung4989d312023-06-29 21:19:25 -0700980 bool invalidateTracks_l(audio_stream_type_t streamType) final;
981 bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) final;
982 void invalidateTracks(audio_stream_type_t streamType) override;
jiabinc44b3462022-12-08 12:52:31 -0800983 // Invalidate tracks by a set of port ids. The port id will be removed from
984 // the given set if the corresponding track is found and invalidated.
Andy Hung4989d312023-06-29 21:19:25 -0700985 void invalidateTracks(std::set<audio_port_handle_t>& portIds) override;
Eric Laurent81784c32012-11-19 14:55:58 -0800986
Andy Hung44f27182023-07-06 20:56:16 -0700987 size_t frameCount() const final { return mNormalFrameCount; }
Glenn Kasten9b58f632013-07-16 11:37:48 -0700988
Andy Hung4989d312023-06-29 21:19:25 -0700989 audio_channel_mask_t mixerChannelMask() const final {
Eric Laurentf1f22e72021-07-13 14:04:14 +0200990 return mMixerChannelMask;
991 }
992
Andy Hung4989d312023-06-29 21:19:25 -0700993 status_t getTimestamp_l(AudioTimestamp& timestamp) final;
Eric Laurent83b88082014-06-20 18:31:16 -0700994
Andy Hung4989d312023-06-29 21:19:25 -0700995 void addPatchTrack(const sp<IAfPatchTrack>& track) final;
996 void deletePatchTrack(const sp<IAfPatchTrack>& track) final;
Eric Laurent83b88082014-06-20 18:31:16 -0700997
Andy Hung4989d312023-06-29 21:19:25 -0700998 void toAudioPortConfig(struct audio_port_config* config) final;
Eric Laurentaccc1472013-09-20 09:36:34 -0700999
Andy Hung10cbff12017-02-21 17:30:14 -08001000 // Return the asynchronous signal wait time.
Andy Hung4989d312023-06-29 21:19:25 -07001001 int64_t computeWaitTimeNs_l() const override { return INT64_MAX; }
Andy Hung1bc088a2018-02-09 15:57:31 -08001002 // returns true if the track is allowed to be added to the thread.
Andy Hung4989d312023-06-29 21:19:25 -07001003 bool isTrackAllowed_l(
Andy Hung1bc088a2018-02-09 15:57:31 -08001004 audio_channel_mask_t channelMask __unused,
1005 audio_format_t format __unused,
1006 audio_session_t sessionId __unused,
Andy Hung4989d312023-06-29 21:19:25 -07001007 uid_t uid) const override {
Andy Hung1bc088a2018-02-09 15:57:31 -08001008 return trackCountForUid_l(uid) < PlaybackThread::kMaxTracksPerUid
1009 && mTracks.size() < PlaybackThread::kMaxTracks;
1010 }
1011
Andy Hung4989d312023-06-29 21:19:25 -07001012 bool isTimestampCorrectionEnabled() const final {
jiabinc52b1ff2019-10-31 17:20:42 -07001013 return audio_is_output_devices(mTimestampCorrectedDevice)
1014 && outDeviceTypes().count(mTimestampCorrectedDevice) != 0;
Andy Hungc8fddf32018-08-08 18:32:37 -07001015 }
jiabinc52b1ff2019-10-31 17:20:42 -07001016
Andy Hung4989d312023-06-29 21:19:25 -07001017 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08001018 return !(mOutput == nullptr || mOutput->stream == nullptr);
1019 }
1020
Andy Hung4989d312023-06-29 21:19:25 -07001021 audio_channel_mask_t hapticChannelMask() const final {
jiabineb3bda02020-06-30 14:07:03 -07001022 return mHapticChannelMask;
1023 }
Andy Hung44f27182023-07-06 20:56:16 -07001024
1025 uint32_t hapticChannelCount() const final {
1026 return mHapticChannelCount;
1027 }
1028
Andy Hung4989d312023-06-29 21:19:25 -07001029 bool supportsHapticPlayback() const final {
jiabineb3bda02020-06-30 14:07:03 -07001030 return (mHapticChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE;
1031 }
1032
Andy Hung4989d312023-06-29 21:19:25 -07001033 void setDownStreamPatch(const struct audio_patch* patch) final {
Eric Laurent74c38dc2020-12-23 18:19:44 +01001034 Mutex::Autolock _l(mLock);
1035 mDownStreamPatch = *patch;
1036 }
1037
Andy Hung4989d312023-06-29 21:19:25 -07001038 IAfTrack* getTrackById_l(audio_port_handle_t trackId) final;
jiabinf042b9b2021-05-07 23:46:28 +00001039
Andy Hung4989d312023-06-29 21:19:25 -07001040 bool hasMixer() const final {
Eric Laurent1c5e2e32021-08-18 18:50:28 +02001041 return mType == MIXER || mType == DUPLICATING || mType == SPATIALIZER;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001042 }
Eric Laurent6f9534f2022-05-03 18:15:04 +02001043
Andy Hung4989d312023-06-29 21:19:25 -07001044 status_t setRequestedLatencyMode(
1045 audio_latency_mode_t /* mode */) override { return INVALID_OPERATION; }
Eric Laurent6f9534f2022-05-03 18:15:04 +02001046
Andy Hung4989d312023-06-29 21:19:25 -07001047 status_t getSupportedLatencyModes(
1048 std::vector<audio_latency_mode_t>* /* modes */) override {
Eric Laurent6f9534f2022-05-03 18:15:04 +02001049 return INVALID_OPERATION;
1050 }
1051
Andy Hung4989d312023-06-29 21:19:25 -07001052 status_t setBluetoothVariableLatencyEnabled(bool /* enabled */) override{
Eric Laurentb0463942022-12-20 16:31:10 +01001053 return INVALID_OPERATION;
1054 }
Eric Laurent01eb1642022-12-16 11:45:07 +01001055
Andy Hung4989d312023-06-29 21:19:25 -07001056 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override;
1057 void stopMelComputation_l() override;
Vlad Popab042ee62022-10-20 18:05:00 +02001058
Andy Hung4989d312023-06-29 21:19:25 -07001059 void setStandby() final {
Eric Laurent19952e12023-04-20 10:08:29 +02001060 Mutex::Autolock _l(mLock);
1061 setStandby_l();
1062 }
1063
Andy Hung4989d312023-06-29 21:19:25 -07001064 void setStandby_l() final {
Eric Laurent19952e12023-04-20 10:08:29 +02001065 mStandby = true;
1066 mHalStarted = false;
1067 mKernelPositionOnStandby =
1068 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1069 }
1070
Andy Hung4989d312023-06-29 21:19:25 -07001071 bool waitForHalStart() final {
Eric Laurent19952e12023-04-20 10:08:29 +02001072 Mutex::Autolock _l(mLock);
1073 static const nsecs_t kWaitHalTimeoutNs = seconds(2);
1074 nsecs_t endWaitTimetNs = systemTime() + kWaitHalTimeoutNs;
1075 while (!mHalStarted) {
1076 nsecs_t timeNs = systemTime();
1077 if (timeNs >= endWaitTimetNs) {
1078 break;
1079 }
1080 nsecs_t waitTimeLeftNs = endWaitTimetNs - timeNs;
1081 mWaitHalStartCV.waitRelative(mLock, waitTimeLeftNs);
1082 }
1083 return mHalStarted;
1084 }
Eric Laurent81784c32012-11-19 14:55:58 -08001085protected:
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001086 // updated by readOutputParameters_l()
Glenn Kasten9b58f632013-07-16 11:37:48 -07001087 size_t mNormalFrameCount; // normal mixer and effects
1088
Andy Hung08fb1742015-05-31 23:22:10 -07001089 bool mThreadThrottle; // throttle the thread processing
Andy Hung40eb1a12015-06-18 13:42:02 -07001090 uint32_t mThreadThrottleTimeMs; // throttle time for MIXER threads
1091 uint32_t mThreadThrottleEndMs; // notify once per throttling
Andy Hung08fb1742015-05-31 23:22:10 -07001092 uint32_t mHalfBufferMs; // half the buffer size in milliseconds
1093
Andy Hung010a1a12014-03-13 13:57:33 -07001094 void* mSinkBuffer; // frame size aligned sink buffer
Eric Laurent81784c32012-11-19 14:55:58 -08001095
Andy Hung98ef9782014-03-04 14:46:50 -08001096 // TODO:
1097 // Rearrange the buffer info into a struct/class with
1098 // clear, copy, construction, destruction methods.
1099 //
1100 // mSinkBuffer also has associated with it:
1101 //
1102 // mSinkBufferSize: Sink Buffer Size
1103 // mFormat: Sink Buffer Format
1104
Andy Hung69aed5f2014-02-25 17:24:40 -08001105 // Mixer Buffer (mMixerBuffer*)
1106 //
1107 // In the case of floating point or multichannel data, which is not in the
1108 // sink format, it is required to accumulate in a higher precision or greater channel count
1109 // buffer before downmixing or data conversion to the sink buffer.
1110
1111 // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
1112 bool mMixerBufferEnabled;
1113
1114 // Storage, 32 byte aligned (may make this alignment a requirement later).
1115 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
1116 void* mMixerBuffer;
1117
1118 // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
1119 size_t mMixerBufferSize;
1120
1121 // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
1122 audio_format_t mMixerBufferFormat;
1123
1124 // An internal flag set to true by MixerThread::prepareTracks_l()
1125 // when mMixerBuffer contains valid data after mixing.
1126 bool mMixerBufferValid;
1127
Andy Hung98ef9782014-03-04 14:46:50 -08001128 // Effects Buffer (mEffectsBuffer*)
1129 //
1130 // In the case of effects data, which is not in the sink format,
1131 // it is required to accumulate in a different buffer before data conversion
1132 // to the sink buffer.
1133
1134 // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
1135 bool mEffectBufferEnabled;
1136
1137 // Storage, 32 byte aligned (may make this alignment a requirement later).
1138 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
1139 void* mEffectBuffer;
1140
1141 // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
1142 size_t mEffectBufferSize;
1143
1144 // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
1145 audio_format_t mEffectBufferFormat;
1146
1147 // An internal flag set to true by MixerThread::prepareTracks_l()
1148 // when mEffectsBuffer contains valid data after mixing.
1149 //
1150 // When this is set, all mixer data is routed into the effects buffer
1151 // for any processing (including output processing).
1152 bool mEffectBufferValid;
1153
jiabinc658e452022-10-21 20:52:21 +00001154 // Set to "true" to enable when data has already copied to sink
1155 bool mHasDataCopiedToSinkBuffer = false;
1156
Eric Laurentb62d0362021-10-26 17:40:18 +02001157 // Frame size aligned buffer used as input and output to all post processing effects
1158 // except the Spatializer in a SPATIALIZER thread. Non spatialized tracks are mixed into
1159 // this buffer so that post processing effects can be applied.
1160 void* mPostSpatializerBuffer = nullptr;
1161
1162 // Size of mPostSpatializerBuffer in bytes
1163 size_t mPostSpatializerBufferSize;
Eric Laurent39095982021-08-24 18:29:27 +02001164
1165
Eric Laurent81784c32012-11-19 14:55:58 -08001166 // suspend count, > 0 means suspended. While suspended, the thread continues to pull from
1167 // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle
1168 // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
1169 // workaround that restriction.
1170 // 'volatile' means accessed via atomic operations and no lock.
1171 volatile int32_t mSuspended;
1172
Andy Hung818e7a32016-02-16 18:08:07 -08001173 int64_t mBytesWritten;
yucliu91503922022-07-20 17:40:39 -07001174 std::atomic<int64_t> mFramesWritten; // not reset on standby
Dean Wheatley12473e92021-03-18 23:00:55 +11001175 int64_t mLastFramesWritten = -1; // track changes in timestamp
1176 // server frames written.
Andy Hung238fa3d2016-07-28 10:53:22 -07001177 int64_t mSuspendedFrames; // not reset on standby
jiabin245cdd92018-12-07 17:55:15 -08001178
1179 // mHapticChannelMask and mHapticChannelCount will only be valid when the thread support
1180 // haptic playback.
1181 audio_channel_mask_t mHapticChannelMask = AUDIO_CHANNEL_NONE;
1182 uint32_t mHapticChannelCount = 0;
Eric Laurentf1f22e72021-07-13 14:04:14 +02001183
1184 audio_channel_mask_t mMixerChannelMask = AUDIO_CHANNEL_NONE;
1185
Eric Laurent81784c32012-11-19 14:55:58 -08001186private:
1187 // mMasterMute is in both PlaybackThread and in AudioFlinger. When a
1188 // PlaybackThread needs to find out if master-muted, it checks it's local
1189 // copy rather than the one in AudioFlinger. This optimization saves a lock.
1190 bool mMasterMute;
1191 void setMasterMute_l(bool muted) { mMasterMute = muted; }
Dean Wheatley12473e92021-03-18 23:00:55 +11001192
1193 auto discontinuityForStandbyOrFlush() const { // call on threadLoop or with lock.
1194 return ((mType == DIRECT && !audio_is_linear_pcm(mFormat))
1195 || mType == OFFLOAD)
1196 ? mTimestampVerifier.DISCONTINUITY_MODE_ZERO
1197 : mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS;
1198 }
1199
Eric Laurent81784c32012-11-19 14:55:58 -08001200protected:
Andy Hung3ff4b552023-06-26 19:20:57 -07001201 ActiveTracks<IAfTrack> mActiveTracks;
Eric Laurent81784c32012-11-19 14:55:58 -08001202
Eric Laurent81784c32012-11-19 14:55:58 -08001203 // Time to sleep between cycles when:
1204 virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED
1205 virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE
1206 virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us
1207 // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
1208 // No sleep in standby mode; waits on a condition
1209
1210 // Code snippets that are temporarily lifted up out of threadLoop() until the merge
Andy Hung4989d312023-06-29 21:19:25 -07001211 virtual void checkSilentMode_l() final; // consider unification with MMapThread
Eric Laurent81784c32012-11-19 14:55:58 -08001212
1213 // Non-trivial for DUPLICATING only
1214 virtual void saveOutputTracks() { }
1215 virtual void clearOutputTracks() { }
1216
1217 // Cache various calculated values, at threadLoop() entry and after a parameter change
1218 virtual void cacheParameters_l();
Eric Laurentb3f315a2021-07-13 15:09:05 +02001219 void setCheckOutputStageEffects() override {
1220 mCheckOutputStageEffects.store(true);
1221 }
Eric Laurent81784c32012-11-19 14:55:58 -08001222
1223 virtual uint32_t correctLatency_l(uint32_t latency) const;
1224
Eric Laurent1c333e22014-05-20 10:48:17 -07001225 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1226 audio_patch_handle_t *handle);
1227 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
1228
Andy Hung44f27182023-07-06 20:56:16 -07001229 bool usesHwAvSync() const final { return mType == DIRECT && mOutput != nullptr
Phil Burk6fc2a7c2015-04-30 16:08:10 -07001230 && mHwSupportsPause
1231 && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
Eric Laurent0f7b5f22014-12-19 10:43:21 -08001232
Andy Hung1bc088a2018-02-09 15:57:31 -08001233 uint32_t trackCountForUid_l(uid_t uid) const;
Eric Laurentad7dd962016-09-22 12:38:37 -07001234
jiabineb3bda02020-06-30 14:07:03 -07001235 void invalidateTracksForAudioSession_l(
Andy Hung4989d312023-06-29 21:19:25 -07001236 audio_session_t sessionId) const override {
jiabineb3bda02020-06-30 14:07:03 -07001237 ThreadBase::invalidateTracksForAudioSession_l(sessionId, mTracks);
1238 }
1239
Eric Laurent81784c32012-11-19 14:55:58 -08001240private:
1241
1242 friend class AudioFlinger; // for numerous
1243
Mikhail Naganovbf493082017-04-17 17:37:12 -07001244 DISALLOW_COPY_AND_ASSIGN(PlaybackThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001245
Andy Hung44f27182023-07-06 20:56:16 -07001246 status_t addTrack_l(const sp<IAfTrack>& track) final;
1247 bool destroyTrack_l(const sp<IAfTrack>& track) final;
1248
Andy Hung3ff4b552023-06-26 19:20:57 -07001249 void removeTrack_l(const sp<IAfTrack>& track);
Eric Laurent81784c32012-11-19 14:55:58 -08001250
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001251 void readOutputParameters_l();
Vlad Popa7e81cea2023-01-19 16:34:16 +01001252 MetadataUpdate updateMetadata_l() final;
Kevin Rocardc86a7f72018-04-03 09:00:09 -07001253 virtual void sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata& metadata);
Eric Laurent81784c32012-11-19 14:55:58 -08001254
Dean Wheatley12473e92021-03-18 23:00:55 +11001255 void collectTimestamps_l();
1256
Andy Hungc0691382018-09-12 18:01:57 -07001257 // The Tracks class manages tracks added and removed from the Thread.
Andy Hung1bc088a2018-02-09 15:57:31 -08001258 template <typename T>
1259 class Tracks {
1260 public:
Andy Hung71ba4b32022-10-06 12:09:49 -07001261 explicit Tracks(bool saveDeletedTrackIds) :
Andy Hungc0691382018-09-12 18:01:57 -07001262 mSaveDeletedTrackIds(saveDeletedTrackIds) { }
Andy Hung1bc088a2018-02-09 15:57:31 -08001263
1264 // SortedVector methods
Andy Hungc0691382018-09-12 18:01:57 -07001265 ssize_t add(const sp<T> &track) {
1266 const ssize_t index = mTracks.add(track);
1267 LOG_ALWAYS_FATAL_IF(index < 0, "cannot add track");
1268 return index;
1269 }
Andy Hung1bc088a2018-02-09 15:57:31 -08001270 ssize_t remove(const sp<T> &track);
1271 size_t size() const {
1272 return mTracks.size();
1273 }
1274 bool isEmpty() const {
1275 return mTracks.isEmpty();
1276 }
1277 ssize_t indexOf(const sp<T> &item) {
1278 return mTracks.indexOf(item);
1279 }
1280 sp<T> operator[](size_t index) const {
1281 return mTracks[index];
1282 }
1283 typename SortedVector<sp<T>>::iterator begin() {
1284 return mTracks.begin();
1285 }
1286 typename SortedVector<sp<T>>::iterator end() {
1287 return mTracks.end();
1288 }
1289
Andy Hung71ba4b32022-10-06 12:09:49 -07001290 size_t processDeletedTrackIds(const std::function<void(int)>& f) {
Andy Hungc0691382018-09-12 18:01:57 -07001291 for (const int trackId : mDeletedTrackIds) {
1292 f(trackId);
Andy Hung1bc088a2018-02-09 15:57:31 -08001293 }
Andy Hungc0691382018-09-12 18:01:57 -07001294 return mDeletedTrackIds.size();
Andy Hung1bc088a2018-02-09 15:57:31 -08001295 }
1296
Andy Hungc0691382018-09-12 18:01:57 -07001297 void clearDeletedTrackIds() { mDeletedTrackIds.clear(); }
Andy Hung1bc088a2018-02-09 15:57:31 -08001298
1299 private:
Andy Hungc0691382018-09-12 18:01:57 -07001300 // Tracks pending deletion for MIXER type threads
1301 const bool mSaveDeletedTrackIds; // true to enable tracking
1302 std::set<int> mDeletedTrackIds;
Andy Hung1bc088a2018-02-09 15:57:31 -08001303
1304 SortedVector<sp<T>> mTracks; // wrapped SortedVector.
1305 };
1306
Andy Hung3ff4b552023-06-26 19:20:57 -07001307 Tracks<IAfTrack> mTracks;
Andy Hung1bc088a2018-02-09 15:57:31 -08001308
Eric Laurent223fd5c2014-11-11 13:43:36 -08001309 stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurent81784c32012-11-19 14:55:58 -08001310 AudioStreamOut *mOutput;
1311
1312 float mMasterVolume;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001313 std::atomic<float> mMasterBalance{};
1314 audio_utils::Balance mBalance;
Eric Laurent81784c32012-11-19 14:55:58 -08001315 int mNumWrites;
1316 int mNumDelayedWrites;
1317 bool mInWrite;
1318
1319 // FIXME rename these former local variables of threadLoop to standard "m" names
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001320 nsecs_t mStandbyTimeNs;
Andy Hung25c2dac2014-02-27 14:56:00 -08001321 size_t mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -08001322
1323 // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001324 uint32_t mActiveSleepTimeUs;
1325 uint32_t mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08001326
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001327 uint32_t mSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -08001328
1329 // mixer status returned by prepareTracks_l()
1330 mixer_state mMixerStatus; // current cycle
1331 // previous cycle when in prepareTracks_l()
1332 mixer_state mMixerStatusIgnoringFastTracks;
1333 // FIXME or a separate ready state per track
1334
1335 // FIXME move these declarations into the specific sub-class that needs them
1336 // MIXER only
1337 uint32_t sleepTimeShift;
1338
1339 // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
Eric Laurentad9cb8b2015-05-26 16:38:19 -07001340 nsecs_t mStandbyDelayNs;
Eric Laurent81784c32012-11-19 14:55:58 -08001341
1342 // MIXER only
1343 nsecs_t maxPeriod;
1344
1345 // DUPLICATING only
1346 uint32_t writeFrames;
1347
Eric Laurentbfb1b832013-01-07 09:53:42 -08001348 size_t mBytesRemaining;
1349 size_t mCurrentWriteLength;
1350 bool mUseAsyncWrite;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001351 // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
1352 // incremented each time a write(), a flush() or a standby() occurs.
1353 // Bit 0 is set when a write blocks and indicates a callback is expected.
1354 // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
1355 // callbacks are ignored.
1356 uint32_t mWriteAckSequence;
1357 // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
1358 // incremented each time a drain is requested or a flush() or standby() occurs.
1359 // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
1360 // expected.
1361 // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
1362 // callbacks are ignored.
1363 uint32_t mDrainSequence;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001364 sp<AsyncCallbackThread> mCallbackThread;
1365
jiabinf6eb4c32020-02-25 14:06:25 -08001366 Mutex mAudioTrackCbLock;
1367 // Record of IAudioTrackCallback
Andy Hung3ff4b552023-06-26 19:20:57 -07001368 std::map<sp<IAfTrack>, sp<media::IAudioTrackCallback>> mAudioTrackCallbacks;
jiabinf6eb4c32020-02-25 14:06:25 -08001369
Eric Laurent81784c32012-11-19 14:55:58 -08001370private:
1371 // The HAL output sink is treated as non-blocking, but current implementation is blocking
1372 sp<NBAIO_Sink> mOutputSink;
1373 // If a fast mixer is present, the blocking pipe sink, otherwise clear
1374 sp<NBAIO_Sink> mPipeSink;
1375 // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
1376 sp<NBAIO_Sink> mNormalSink;
Eric Laurent81784c32012-11-19 14:55:58 -08001377 uint32_t mScreenState; // cached copy of gScreenState
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07001378 // TODO: add comment and adjust size as needed
Glenn Kasteneef598c2017-04-03 14:41:13 -07001379 static const size_t kFastMixerLogSize = 8 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -08001380 sp<NBLog::Writer> mFastMixerNBLogWriter;
Andy Hung2148bf02016-11-28 19:01:02 -08001381
Dean Wheatley30d28422018-11-06 10:27:40 +11001382 // Downstream patch latency, available if mDownstreamLatencyStatMs.getN() > 0.
1383 audio_utils::Statistics<double> mDownstreamLatencyStatMs{0.999};
Andy Hung2148bf02016-11-28 19:01:02 -08001384
Eric Laurent19952e12023-04-20 10:08:29 +02001385 // output stream start detection based on render position returned by the kernel
1386 // condition signalled when the output stream has started
1387 Condition mWaitHalStartCV;
1388 // true when the output stream render position has moved, reset to false in standby
1389 bool mHalStarted = false;
1390 // last kernel render position saved when entering standby
1391 int64_t mKernelPositionOnStandby = 0;
1392
Eric Laurent81784c32012-11-19 14:55:58 -08001393public:
Andy Hung4989d312023-06-29 21:19:25 -07001394 FastTrackUnderruns getFastTrackUnderruns(size_t /* fastIndex */) const override
1395 { return {}; }
1396 const std::atomic<int64_t>& framesWritten() const final { return mFramesWritten; }
Eric Laurent81784c32012-11-19 14:55:58 -08001397
1398protected:
1399 // accessed by both binder threads and within threadLoop(), lock on mutex needed
Andy Hung44f27182023-07-06 20:56:16 -07001400 uint32_t& fastTrackAvailMask_l() final { return mFastTrackAvailMask; }
1401 uint32_t mFastTrackAvailMask; // bit i set if fast track [i] is available
Eric Laurentd1f69b02014-12-15 14:33:13 -08001402 bool mHwSupportsPause;
1403 bool mHwPaused;
1404 bool mFlushPending;
Eric Laurent7c29ec92017-09-20 17:54:22 -07001405 // volumes last sent to audio HAL with stream->setVolume()
1406 float mLeftVolFloat;
1407 float mRightVolFloat;
Eric Laurent74c38dc2020-12-23 18:19:44 +01001408
1409 // audio patch used by the downstream software patch.
1410 // Only used if ThreadBase::mIsMsdDevice is true.
1411 struct audio_patch mDownStreamPatch;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001412
1413 std::atomic_bool mCheckOutputStageEffects{};
ziyangch8f194f12021-12-01 13:48:04 -08001414
ziyangch8f194f12021-12-01 13:48:04 -08001415
Brian Lindahl9e661ad2022-07-27 18:01:07 +02001416 // Provides periodic checking for timestamp advancement for underrun detection.
1417 class IsTimestampAdvancing {
1418 public:
1419 // The timestamp will not be checked any faster than the specified time.
Andy Hung71ba4b32022-10-06 12:09:49 -07001420 explicit IsTimestampAdvancing(nsecs_t minimumTimeBetweenChecksNs)
Brian Lindahl9e661ad2022-07-27 18:01:07 +02001421 : mMinimumTimeBetweenChecksNs(minimumTimeBetweenChecksNs)
1422 {
1423 clear();
1424 }
1425 // Check if the presentation position has advanced in the last periodic time.
1426 bool check(AudioStreamOut * output);
1427 // Clear the internal state when the playback state changes for the output
1428 // stream.
1429 void clear();
1430 private:
1431 // The minimum time between timestamp checks.
1432 const nsecs_t mMinimumTimeBetweenChecksNs;
1433 // Add differential check on the timestamps to see if there is a change in the
1434 // timestamp frame position between the last call to check.
1435 uint64_t mPreviousPosition;
1436 // The time at which the last check occurred, to ensure we don't check too
1437 // frequently, giving the Audio HAL enough time to update its timestamps.
1438 nsecs_t mPreviousNs;
1439 // The valued is latched so we don't check timestamps too frequently.
1440 bool mLatchedValue;
1441 };
1442 IsTimestampAdvancing mIsTimestampAdvancing;
ziyangch8f194f12021-12-01 13:48:04 -08001443
Brian Lindahl9e661ad2022-07-27 18:01:07 +02001444 virtual void flushHw_l() {
1445 mIsTimestampAdvancing.clear();
1446 }
Eric Laurent81784c32012-11-19 14:55:58 -08001447};
1448
Eric Laurentb0463942022-12-20 16:31:10 +01001449class MixerThread : public PlaybackThread,
1450 public StreamOutHalInterfaceLatencyModeCallback {
Eric Laurent81784c32012-11-19 14:55:58 -08001451public:
1452 MixerThread(const sp<AudioFlinger>& audioFlinger,
1453 AudioStreamOut* output,
1454 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001455 bool systemReady,
Eric Laurentf1f22e72021-07-13 14:04:14 +02001456 type_t type = MIXER,
1457 audio_config_base_t *mixerConfig = nullptr);
Andy Hung4989d312023-06-29 21:19:25 -07001458 ~MixerThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001459
Eric Laurentb0463942022-12-20 16:31:10 +01001460 // RefBase
Andy Hung4989d312023-06-29 21:19:25 -07001461 void onFirstRef() override;
Eric Laurentb0463942022-12-20 16:31:10 +01001462
1463 // StreamOutHalInterfaceLatencyModeCallback
1464 void onRecommendedLatencyModeChanged(
Andy Hung4989d312023-06-29 21:19:25 -07001465 std::vector<audio_latency_mode_t> modes) final;
Eric Laurentb0463942022-12-20 16:31:10 +01001466
Eric Laurent81784c32012-11-19 14:55:58 -08001467 // Thread virtuals
1468
Andy Hung4989d312023-06-29 21:19:25 -07001469 bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001470
Andy Hung4989d312023-06-29 21:19:25 -07001471 bool isTrackAllowed_l(
Andy Hung1bc088a2018-02-09 15:57:31 -08001472 audio_channel_mask_t channelMask, audio_format_t format,
Andy Hung4989d312023-06-29 21:19:25 -07001473 audio_session_t sessionId, uid_t uid) const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001474protected:
Andy Hung4989d312023-06-29 21:19:25 -07001475 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) override;
1476 uint32_t idleSleepTimeUs() const final;
1477 uint32_t suspendSleepTimeUs() const final;
1478 void cacheParameters_l() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001479
Andy Hung4989d312023-06-29 21:19:25 -07001480 void acquireWakeLock_l() final {
Andy Hungdae27702016-10-31 14:01:16 -07001481 PlaybackThread::acquireWakeLock_l();
Andy Hung818e7a32016-02-16 18:08:07 -08001482 if (hasFastMixer()) {
1483 mFastMixer->setBoottimeOffset(
1484 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1485 }
1486 }
1487
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001488 void dumpInternals_l(int fd, const Vector<String16>& args) override;
1489
Eric Laurent81784c32012-11-19 14:55:58 -08001490 // threadLoop snippets
Andy Hung4989d312023-06-29 21:19:25 -07001491 ssize_t threadLoop_write() override;
1492 void threadLoop_standby() override;
1493 void threadLoop_mix() override;
1494 void threadLoop_sleepTime() override;
1495 uint32_t correctLatency_l(uint32_t latency) const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001496
Andy Hung4989d312023-06-29 21:19:25 -07001497 status_t createAudioPatch_l(
1498 const struct audio_patch* patch, audio_patch_handle_t* handle) final;
1499 status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final;
Eric Laurent054d9d32015-04-24 08:48:48 -07001500
Eric Laurent81784c32012-11-19 14:55:58 -08001501 AudioMixer* mAudioMixer; // normal mixer
Eric Laurentb0463942022-12-20 16:31:10 +01001502
1503 // Support low latency mode by default as unless explicitly indicated by the audio HAL
1504 // we assume the audio path is compatible with the head tracking latency requirements
1505 std::vector<audio_latency_mode_t> mSupportedLatencyModes = {AUDIO_LATENCY_MODE_LOW};
1506 // default to invalid value to force first update to the audio HAL
1507 audio_latency_mode_t mSetLatencyMode =
1508 (audio_latency_mode_t)AUDIO_LATENCY_MODE_INVALID;
1509
1510 // Bluetooth Variable latency control logic is enabled or disabled for this thread
1511 std::atomic_bool mBluetoothLatencyModesEnabled;
1512
Eric Laurent81784c32012-11-19 14:55:58 -08001513private:
1514 // one-time initialization, no locks required
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001515 sp<FastMixer> mFastMixer; // non-0 if there is also a fast mixer
Eric Laurent81784c32012-11-19 14:55:58 -08001516 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
1517
1518 // contents are not guaranteed to be consistent, no locks required
1519 FastMixerDumpState mFastMixerDumpState;
1520#ifdef STATE_QUEUE_DUMP
1521 StateQueueObserverDump mStateQueueObserverDump;
1522 StateQueueMutatorDump mStateQueueMutatorDump;
1523#endif
1524 AudioWatchdogDump mAudioWatchdogDump;
1525
1526 // accessible only within the threadLoop(), no locks required
1527 // mFastMixer->sq() // for mutating and pushing state
1528 int32_t mFastMixerFutex; // for cold idle
1529
Andy Hung2ddee192015-12-18 17:34:44 -08001530 std::atomic_bool mMasterMono;
Eric Laurent81784c32012-11-19 14:55:58 -08001531public:
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001532 virtual bool hasFastMixer() const { return mFastMixer != 0; }
Eric Laurent81784c32012-11-19 14:55:58 -08001533 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
Glenn Kastendc2c50b2016-04-21 08:13:14 -07001534 ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
Eric Laurent81784c32012-11-19 14:55:58 -08001535 return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
1536 }
Eric Laurent83b88082014-06-20 18:31:16 -07001537
Andy Hung1c86ebe2018-05-29 20:29:08 -07001538 status_t threadloop_getHalTimestamp_l(
1539 ExtendedTimestamp *timestamp) const override {
1540 if (mNormalSink.get() != nullptr) {
1541 return mNormalSink->getTimestamp(*timestamp);
1542 }
1543 return INVALID_OPERATION;
1544 }
1545
Eric Laurentb0463942022-12-20 16:31:10 +01001546 status_t getSupportedLatencyModes(
1547 std::vector<audio_latency_mode_t>* modes) override;
1548
1549 status_t setBluetoothVariableLatencyEnabled(bool enabled) override;
1550
Andy Hung2ddee192015-12-18 17:34:44 -08001551protected:
1552 virtual void setMasterMono_l(bool mono) {
1553 mMasterMono.store(mono);
1554 if (mFastMixer != nullptr) { /* hasFastMixer() */
1555 mFastMixer->setMasterMono(mMasterMono);
1556 }
1557 }
1558 // the FastMixer performs mono blend if it exists.
Glenn Kasten03c48d52016-01-27 17:25:17 -08001559 // Blending with limiter is not idempotent,
1560 // and blending without limiter is idempotent but inefficient to do twice.
Andy Hung2ddee192015-12-18 17:34:44 -08001561 virtual bool requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001562
1563 void setMasterBalance(float balance) override {
1564 mMasterBalance.store(balance);
1565 if (hasFastMixer()) {
1566 mFastMixer->setMasterBalance(balance);
1567 }
1568 }
Eric Laurentb0463942022-12-20 16:31:10 +01001569
1570 void updateHalSupportedLatencyModes_l();
1571 void onHalLatencyModesChanged_l() override;
1572 void setHalLatencyMode_l() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001573};
1574
Andy Hung44f27182023-07-06 20:56:16 -07001575class DirectOutputThread : public PlaybackThread, public virtual IAfDirectOutputThread {
Eric Laurent81784c32012-11-19 14:55:58 -08001576public:
1577
Andy Hung44f27182023-07-06 20:56:16 -07001578 sp<IAfDirectOutputThread> asIAfDirectOutputThread() final {
1579 return sp<IAfDirectOutputThread>::fromExisting(this);
1580 }
1581
Eric Laurent81784c32012-11-19 14:55:58 -08001582 DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Gareth Fenn56576722022-10-05 13:42:36 -07001583 audio_io_handle_t id, bool systemReady,
1584 const audio_offload_info_t& offloadInfo)
1585 : DirectOutputThread(audioFlinger, output, id, DIRECT, systemReady, offloadInfo) { }
Andy Hung48f59ed2019-01-28 15:06:59 -08001586
Eric Laurent81784c32012-11-19 14:55:58 -08001587 virtual ~DirectOutputThread();
1588
Andy Hung44f27182023-07-06 20:56:16 -07001589 status_t selectPresentation(int presentationId, int programId) final;
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001590
Eric Laurent81784c32012-11-19 14:55:58 -08001591 // Thread virtuals
1592
Eric Laurent10351942014-05-08 18:49:52 -07001593 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1594 status_t& status);
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001595
ziyangch8f194f12021-12-01 13:48:04 -08001596 void flushHw_l() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001597
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001598 void setMasterBalance(float balance) override;
1599
Eric Laurent81784c32012-11-19 14:55:58 -08001600protected:
Eric Laurent81784c32012-11-19 14:55:58 -08001601 virtual uint32_t activeSleepTimeUs() const;
1602 virtual uint32_t idleSleepTimeUs() const;
1603 virtual uint32_t suspendSleepTimeUs() const;
1604 virtual void cacheParameters_l();
1605
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001606 void dumpInternals_l(int fd, const Vector<String16>& args) override;
1607
Eric Laurent81784c32012-11-19 14:55:58 -08001608 // threadLoop snippets
Andy Hung3ff4b552023-06-26 19:20:57 -07001609 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove);
Eric Laurent81784c32012-11-19 14:55:58 -08001610 virtual void threadLoop_mix();
1611 virtual void threadLoop_sleepTime();
Eric Laurentd1f69b02014-12-15 14:33:13 -08001612 virtual void threadLoop_exit();
1613 virtual bool shouldStandby_l();
Eric Laurent81784c32012-11-19 14:55:58 -08001614
Phil Burk43b4dcc2015-06-09 16:53:44 -07001615 virtual void onAddNewTrack_l();
1616
Gareth Fenn56576722022-10-05 13:42:36 -07001617 const audio_offload_info_t mOffloadInfo;
Andy Hungee86cee2022-12-13 19:19:53 -08001618
1619 audioflinger::MonotonicFrameCounter mMonotonicFrameCounter; // for VolumeShaper
Andy Hung48f59ed2019-01-28 15:06:59 -08001620 bool mVolumeShaperActive = false;
Eric Laurent81784c32012-11-19 14:55:58 -08001621
Eric Laurentbfb1b832013-01-07 09:53:42 -08001622 DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Gareth Fenn56576722022-10-05 13:42:36 -07001623 audio_io_handle_t id, ThreadBase::type_t type, bool systemReady,
1624 const audio_offload_info_t& offloadInfo);
Andy Hung3ff4b552023-06-26 19:20:57 -07001625 void processVolume_l(IAfTrack *track, bool lastTrack);
Gareth Fenn56576722022-10-05 13:42:36 -07001626 bool isTunerStream() const { return (mOffloadInfo.content_id > 0); }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001627
Eric Laurent81784c32012-11-19 14:55:58 -08001628 // prepareTracks_l() tells threadLoop_mix() the name of the single active track
Andy Hung3ff4b552023-06-26 19:20:57 -07001629 sp<IAfTrack> mActiveTrack;
Phil Burk43b4dcc2015-06-09 16:53:44 -07001630
Andy Hung3ff4b552023-06-26 19:20:57 -07001631 wp<IAfTrack> mPreviousTrack; // used to detect track switch
Phil Burk43b4dcc2015-06-09 16:53:44 -07001632
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001633 // This must be initialized for initial condition of mMasterBalance = 0 (disabled).
1634 float mMasterBalanceLeft = 1.f;
1635 float mMasterBalanceRight = 1.f;
1636
Eric Laurent81784c32012-11-19 14:55:58 -08001637public:
1638 virtual bool hasFastMixer() const { return false; }
Andy Hung10cbff12017-02-21 17:30:14 -08001639
1640 virtual int64_t computeWaitTimeNs_l() const override;
Andy Hungf3234512018-07-03 14:51:47 -07001641
1642 status_t threadloop_getHalTimestamp_l(ExtendedTimestamp *timestamp) const override {
1643 // For DIRECT and OFFLOAD threads, query the output sink directly.
1644 if (mOutput != nullptr) {
1645 uint64_t uposition64;
1646 struct timespec time;
1647 if (mOutput->getPresentationPosition(
1648 &uposition64, &time) == OK) {
1649 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL]
1650 = (int64_t)uposition64;
1651 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
1652 = audio_utils_ns_from_timespec(&time);
1653 return NO_ERROR;
1654 }
1655 }
1656 return INVALID_OPERATION;
1657 }
Eric Laurent81784c32012-11-19 14:55:58 -08001658};
1659
Eric Laurentbfb1b832013-01-07 09:53:42 -08001660class OffloadThread : public DirectOutputThread {
1661public:
1662
1663 OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Gareth Fenn56576722022-10-05 13:42:36 -07001664 audio_io_handle_t id, bool systemReady,
1665 const audio_offload_info_t& offloadInfo);
Eric Laurent6a51d7e2013-10-17 18:59:26 -07001666 virtual ~OffloadThread() {};
ziyangch8f194f12021-12-01 13:48:04 -08001667 void flushHw_l() override;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001668
1669protected:
1670 // threadLoop snippets
Andy Hung3ff4b552023-06-26 19:20:57 -07001671 virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001672 virtual void threadLoop_exit();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001673
1674 virtual bool waitingAsyncCallback();
1675 virtual bool waitingAsyncCallback_l();
Haynes Mathew George05317d22016-05-03 16:34:26 -07001676 virtual void invalidateTracks(audio_stream_type_t streamType);
jiabinc44b3462022-12-08 12:52:31 -08001677 void invalidateTracks(std::set<audio_port_handle_t>& portIds) override;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001678
Eric Laurentde0613d2016-07-22 18:19:11 -07001679 virtual bool keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
Eric Laurent64667972016-03-30 18:19:46 -07001680
Eric Laurentbfb1b832013-01-07 09:53:42 -08001681private:
Eric Laurentbfb1b832013-01-07 09:53:42 -08001682 size_t mPausedWriteLength; // length in bytes of write interrupted by pause
1683 size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume
Eric Laurent64667972016-03-30 18:19:46 -07001684 bool mKeepWakeLock; // keep wake lock while waiting for write callback
Eric Laurentbfb1b832013-01-07 09:53:42 -08001685};
1686
1687class AsyncCallbackThread : public Thread {
1688public:
1689
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001690 explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001691
1692 virtual ~AsyncCallbackThread();
1693
1694 // Thread virtuals
1695 virtual bool threadLoop();
1696
1697 // RefBase
1698 virtual void onFirstRef();
1699
1700 void exit();
Eric Laurent3b4529e2013-09-05 18:09:19 -07001701 void setWriteBlocked(uint32_t sequence);
1702 void resetWriteBlocked();
1703 void setDraining(uint32_t sequence);
1704 void resetDraining();
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07001705 void setAsyncError();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001706
1707private:
Eric Laurent4de95592013-09-26 15:28:21 -07001708 const wp<PlaybackThread> mPlaybackThread;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001709 // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
1710 // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
1711 // to indicate that the callback has been received via resetWriteBlocked()
Eric Laurent4de95592013-09-26 15:28:21 -07001712 uint32_t mWriteAckSequence;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001713 // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
1714 // setDraining(). The sequence is shifted one bit to the left and the lsb is used
1715 // to indicate that the callback has been received via resetDraining()
Eric Laurent4de95592013-09-26 15:28:21 -07001716 uint32_t mDrainSequence;
1717 Condition mWaitWorkCV;
1718 Mutex mLock;
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07001719 bool mAsyncError;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001720};
1721
Andy Hung44f27182023-07-06 20:56:16 -07001722class DuplicatingThread : public MixerThread, public IAfDuplicatingThread {
Eric Laurent81784c32012-11-19 14:55:58 -08001723public:
Andy Hung44f27182023-07-06 20:56:16 -07001724 DuplicatingThread(const sp<AudioFlinger>& audioFlinger, IAfPlaybackThread* mainThread,
Eric Laurent72e3f392015-05-20 14:43:50 -07001725 audio_io_handle_t id, bool systemReady);
Andy Hung44f27182023-07-06 20:56:16 -07001726 ~DuplicatingThread() override;
1727
1728 sp<IAfDuplicatingThread> asIAfDuplicatingThread() final {
1729 return sp<IAfDuplicatingThread>::fromExisting(this);
1730 }
Eric Laurent81784c32012-11-19 14:55:58 -08001731
1732 // Thread virtuals
Andy Hung44f27182023-07-06 20:56:16 -07001733 void addOutputTrack(IAfPlaybackThread* thread) final;
1734 void removeOutputTrack(IAfPlaybackThread* thread) final;
1735 uint32_t waitTimeMs() const final { return mWaitTimeMs; }
Kevin Rocard069c2712018-03-29 19:09:14 -07001736
Kevin Rocardc86a7f72018-04-03 09:00:09 -07001737 void sendMetadataToBackend_l(
1738 const StreamOutHalInterface::SourceMetadata& metadata) override;
Eric Laurent81784c32012-11-19 14:55:58 -08001739protected:
1740 virtual uint32_t activeSleepTimeUs() const;
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001741 void dumpInternals_l(int fd, const Vector<String16>& args) override;
Eric Laurent81784c32012-11-19 14:55:58 -08001742
1743private:
Andy Hung71ba4b32022-10-06 12:09:49 -07001744 bool outputsReady();
Eric Laurent81784c32012-11-19 14:55:58 -08001745protected:
1746 // threadLoop snippets
1747 virtual void threadLoop_mix();
1748 virtual void threadLoop_sleepTime();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001749 virtual ssize_t threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -08001750 virtual void threadLoop_standby();
1751 virtual void cacheParameters_l();
1752
1753private:
1754 // called from threadLoop, addOutputTrack, removeOutputTrack
1755 virtual void updateWaitTime_l();
1756protected:
1757 virtual void saveOutputTracks();
1758 virtual void clearOutputTracks();
1759private:
1760
1761 uint32_t mWaitTimeMs;
Andy Hung3ff4b552023-06-26 19:20:57 -07001762 SortedVector <sp<IAfOutputTrack>> outputTracks;
1763 SortedVector <sp<IAfOutputTrack>> mOutputTracks;
Eric Laurent81784c32012-11-19 14:55:58 -08001764public:
1765 virtual bool hasFastMixer() const { return false; }
Andy Hung1c86ebe2018-05-29 20:29:08 -07001766 status_t threadloop_getHalTimestamp_l(
1767 ExtendedTimestamp *timestamp) const override {
1768 if (mOutputTracks.size() > 0) {
1769 // forward the first OutputTrack's kernel information for timestamp.
1770 const ExtendedTimestamp trackTimestamp =
1771 mOutputTracks[0]->getClientProxyTimestamp();
1772 if (trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
1773 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
1774 trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
1775 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
1776 trackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1777 return OK; // discard server timestamp - that's ignored.
1778 }
1779 }
1780 return INVALID_OPERATION;
1781 }
Eric Laurent81784c32012-11-19 14:55:58 -08001782};
1783
Eric Laurentb0463942022-12-20 16:31:10 +01001784class SpatializerThread : public MixerThread {
Eric Laurentb3f315a2021-07-13 15:09:05 +02001785public:
Eric Laurentfa0f6742021-08-17 18:39:44 +02001786 SpatializerThread(const sp<AudioFlinger>& audioFlinger,
Eric Laurentb3f315a2021-07-13 15:09:05 +02001787 AudioStreamOut* output,
1788 audio_io_handle_t id,
1789 bool systemReady,
1790 audio_config_base_t *mixerConfig);
Eric Laurentb3f315a2021-07-13 15:09:05 +02001791
Andy Hung4989d312023-06-29 21:19:25 -07001792 bool hasFastMixer() const final { return false; }
Eric Laurentb3f315a2021-07-13 15:09:05 +02001793
Eric Laurent6f9534f2022-05-03 18:15:04 +02001794 // RefBase
Andy Hung4989d312023-06-29 21:19:25 -07001795 void onFirstRef() final;
Eric Laurent6f9534f2022-05-03 18:15:04 +02001796
Andy Hung4989d312023-06-29 21:19:25 -07001797 status_t setRequestedLatencyMode(audio_latency_mode_t mode) final;
Eric Laurent6f9534f2022-05-03 18:15:04 +02001798
Eric Laurentb3f315a2021-07-13 15:09:05 +02001799protected:
Andy Hung4989d312023-06-29 21:19:25 -07001800 void checkOutputStageEffects() final;
1801 void setHalLatencyMode_l() final;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001802
1803private:
Eric Laurent6f9534f2022-05-03 18:15:04 +02001804 // Do not request a specific mode by default
1805 audio_latency_mode_t mRequestedLatencyMode = AUDIO_LATENCY_MODE_FREE;
1806
Andy Hungbd72c542023-06-20 18:56:17 -07001807 sp<IAfEffectHandle> mFinalDownMixer;
Eric Laurentb3f315a2021-07-13 15:09:05 +02001808};
1809
Eric Laurent81784c32012-11-19 14:55:58 -08001810// record thread
Andy Hung44f27182023-07-06 20:56:16 -07001811class RecordThread : public IAfRecordThread, public ThreadBase
Eric Laurent81784c32012-11-19 14:55:58 -08001812{
Andy Hung3ff4b552023-06-26 19:20:57 -07001813 // TODO(b/288339104) remove friends
1814 friend class PassthruPatchRecord;
1815 friend class RecordTrack;
1816 friend class ResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08001817public:
Andy Hung44f27182023-07-06 20:56:16 -07001818 sp<IAfRecordThread> asIAfRecordThread() final {
1819 return sp<IAfRecordThread>::fromExisting(this);
1820 }
Eric Laurent81784c32012-11-19 14:55:58 -08001821
1822 RecordThread(const sp<AudioFlinger>& audioFlinger,
1823 AudioStreamIn *input,
Eric Laurent81784c32012-11-19 14:55:58 -08001824 audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -07001825 bool systemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08001826 );
Andy Hung4989d312023-06-29 21:19:25 -07001827 ~RecordThread() override;
Eric Laurent81784c32012-11-19 14:55:58 -08001828
1829 // no addTrack_l ?
Andy Hung44f27182023-07-06 20:56:16 -07001830 void destroyTrack_l(const sp<IAfRecordTrack>& track) final;
1831 void removeTrack_l(const sp<IAfRecordTrack>& track) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001832
Eric Laurent81784c32012-11-19 14:55:58 -08001833 // Thread virtuals
Andy Hung4989d312023-06-29 21:19:25 -07001834 bool threadLoop() final;
1835 void preExit() final;
Eric Laurent81784c32012-11-19 14:55:58 -08001836
1837 // RefBase
Andy Hung4989d312023-06-29 21:19:25 -07001838 void onFirstRef() final;
Eric Laurent81784c32012-11-19 14:55:58 -08001839
Andy Hung4989d312023-06-29 21:19:25 -07001840 status_t initCheck() const final { return mInput == nullptr ? NO_INIT : NO_ERROR; }
Glenn Kastene198c362013-08-13 09:13:36 -07001841
Andy Hung4989d312023-06-29 21:19:25 -07001842 sp<MemoryDealer> readOnlyHeap() const final { return mReadOnlyHeap; }
Glenn Kastenb880f5e2014-05-07 08:43:45 -07001843
Andy Hung4989d312023-06-29 21:19:25 -07001844 sp<IMemory> pipeMemory() const final { return mPipeMemory; }
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001845
Andy Hung44f27182023-07-06 20:56:16 -07001846 sp<IAfRecordTrack> createRecordTrack_l(
Andy Hungd65869f2023-06-27 17:05:02 -07001847 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001848 const audio_attributes_t& attr,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001849 uint32_t *pSampleRate,
Eric Laurent81784c32012-11-19 14:55:58 -08001850 audio_format_t format,
1851 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001852 size_t *pFrameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08001853 audio_session_t sessionId,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001854 size_t *pNotificationFrameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07001855 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00001856 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -07001857 audio_input_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -08001858 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001859 status_t *status /*non-NULL*/,
Eric Laurentec376dc2021-04-08 20:41:22 +02001860 audio_port_handle_t portId,
Andy Hung44f27182023-07-06 20:56:16 -07001861 int32_t maxSharedAudioHistoryMs) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001862
Andy Hung3ff4b552023-06-26 19:20:57 -07001863 status_t start(IAfRecordTrack* recordTrack,
Eric Laurent81784c32012-11-19 14:55:58 -08001864 AudioSystem::sync_event_t event,
Andy Hung44f27182023-07-06 20:56:16 -07001865 audio_session_t triggerSession) final;
Eric Laurent81784c32012-11-19 14:55:58 -08001866
1867 // ask the thread to stop the specified track, and
1868 // return true if the caller should then do it's part of the stopping process
Andy Hung44f27182023-07-06 20:56:16 -07001869 bool stop(IAfRecordTrack* recordTrack) final;
1870 AudioStreamIn* getInput() const final { return mInput; }
1871 AudioStreamIn* clearInput() final;
Eric Laurent81784c32012-11-19 14:55:58 -08001872
Andy Hung44f27182023-07-06 20:56:16 -07001873 // TODO(b/288339104) Unify with IAfThreadBase
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001874 virtual sp<StreamHalInterface> stream() const;
Eric Laurent81784c32012-11-19 14:55:58 -08001875
Eric Laurent81784c32012-11-19 14:55:58 -08001876
Eric Laurent10351942014-05-08 18:49:52 -07001877 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1878 status_t& status);
1879 virtual void cacheParameters_l() {}
Eric Laurent81784c32012-11-19 14:55:58 -08001880 virtual String8 getParameters(const String8& keys);
Andy Hung44f27182023-07-06 20:56:16 -07001881 void ioConfigChanged(audio_io_config_event_t event, pid_t pid = 0,
1882 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
Eric Laurent1c333e22014-05-20 10:48:17 -07001883 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1884 audio_patch_handle_t *handle);
1885 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
jiabinc52b1ff2019-10-31 17:20:42 -07001886 void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override;
Eric Laurent5f0fd7b2021-05-07 16:33:26 +02001887 void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override;
Eric Laurent83b88082014-06-20 18:31:16 -07001888
Andy Hung44f27182023-07-06 20:56:16 -07001889 void addPatchTrack(const sp<IAfPatchRecord>& record) final;
1890 void deletePatchTrack(const sp<IAfPatchRecord>& record) final;
Eric Laurent83b88082014-06-20 18:31:16 -07001891
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001892 void readInputParameters_l();
Andy Hung44f27182023-07-06 20:56:16 -07001893 uint32_t getInputFramesLost() const final;
Eric Laurent81784c32012-11-19 14:55:58 -08001894
Andy Hungbd72c542023-06-20 18:56:17 -07001895 virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain);
1896 virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain);
Andy Hungc3d62f92019-03-14 13:38:51 -07001897 uint32_t hasAudioSession_l(audio_session_t sessionId) const override {
1898 return ThreadBase::hasAudioSession_l(sessionId, mTracks);
1899 }
Eric Laurent81784c32012-11-19 14:55:58 -08001900
1901 // Return the set of unique session IDs across all tracks.
1902 // The keys are the session IDs, and the associated values are meaningless.
1903 // FIXME replace by Set [and implement Bag/Multiset for other uses].
Glenn Kastend848eb42016-03-08 13:42:11 -08001904 KeyedVector<audio_session_t, bool> sessionIds() const;
Eric Laurent81784c32012-11-19 14:55:58 -08001905
Andy Hung068e08e2023-05-15 19:02:55 -07001906 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) override;
1907 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const override;
Eric Laurent81784c32012-11-19 14:55:58 -08001908
Andy Hung068e08e2023-05-15 19:02:55 -07001909 static void syncStartEventCallback(const wp<audioflinger::SyncEvent>& event);
Eric Laurent81784c32012-11-19 14:55:58 -08001910
Glenn Kasten9b58f632013-07-16 11:37:48 -07001911 virtual size_t frameCount() const { return mFrameCount; }
Andy Hung44f27182023-07-06 20:56:16 -07001912 bool hasFastCapture() const final { return mFastCapture != 0; }
Mikhail Naganovdc769682018-05-04 15:34:08 -07001913 virtual void toAudioPortConfig(struct audio_port_config *config);
Glenn Kasten9b58f632013-07-16 11:37:48 -07001914
Eric Laurent4c415062016-06-17 16:14:16 -07001915 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
1916 audio_session_t sessionId);
1917
Andy Hungdae27702016-10-31 14:01:16 -07001918 virtual void acquireWakeLock_l() {
1919 ThreadBase::acquireWakeLock_l();
1920 mActiveTracks.updatePowerState(this, true /* force */);
1921 }
1922
Andy Hung44f27182023-07-06 20:56:16 -07001923 void checkBtNrec() final;
Eric Laurentd8365c52017-07-16 15:27:05 -07001924
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001925 // Sets the UID records silence
Andy Hung44f27182023-07-06 20:56:16 -07001926 void setRecordSilenced(audio_port_handle_t portId, bool silenced) final;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001927
Andy Hung44f27182023-07-06 20:56:16 -07001928 status_t getActiveMicrophones(
1929 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const final;
1930 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) final;
1931 status_t setPreferredMicrophoneFieldDimension(float zoom) final;
Paul McLean03a6e6a2018-12-04 10:54:13 -07001932
Vlad Popa7e81cea2023-01-19 16:34:16 +01001933 MetadataUpdate updateMetadata_l() override;
Kevin Rocard069c2712018-03-29 19:09:14 -07001934
Andy Hung44f27182023-07-06 20:56:16 -07001935 bool fastTrackAvailable() const final { return mFastTrackAvail; }
1936 void setFastTrackAvailable(bool available) final { mFastTrackAvail = available; }
jiabin01c8f562018-07-19 17:47:28 -07001937
Andy Hungc8fddf32018-08-08 18:32:37 -07001938 bool isTimestampCorrectionEnabled() const override {
1939 // checks popcount for exactly one device.
Atneya Nair497fff12022-01-18 16:23:04 -05001940 // Is currently disabled. Before enabling,
1941 // verify compressed record timestamps.
jiabinc52b1ff2019-10-31 17:20:42 -07001942 return audio_is_input_device(mTimestampCorrectedDevice)
1943 && inDeviceType() == mTimestampCorrectedDevice;
Andy Hungc8fddf32018-08-08 18:32:37 -07001944 }
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001945
Andy Hung44f27182023-07-06 20:56:16 -07001946 status_t shareAudioHistory(const std::string& sharedAudioPackageName,
Eric Laurentec376dc2021-04-08 20:41:22 +02001947 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
Andy Hung44f27182023-07-06 20:56:16 -07001948 int64_t sharedAudioStartMs = -1) final;
Eric Laurentec376dc2021-04-08 20:41:22 +02001949 status_t shareAudioHistory_l(const std::string& sharedAudioPackageName,
1950 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
1951 int64_t sharedAudioStartMs = -1);
Andy Hung44f27182023-07-06 20:56:16 -07001952 void resetAudioHistory_l() final;
Eric Laurentec376dc2021-04-08 20:41:22 +02001953
Andy Hung4989d312023-06-29 21:19:25 -07001954 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08001955 return !(mInput == nullptr || mInput->stream == nullptr);
1956 }
1957
Mikhail Naganov01dc5ca2019-03-29 10:12:12 -07001958protected:
1959 void dumpInternals_l(int fd, const Vector<String16>& args) override;
1960 void dumpTracks_l(int fd, const Vector<String16>& args) override;
1961
Eric Laurent81784c32012-11-19 14:55:58 -08001962private:
Eric Laurent81784c32012-11-19 14:55:58 -08001963 // Enter standby if not already in standby, and set mStandby flag
Glenn Kasten93e471f2013-08-19 08:40:07 -07001964 void standbyIfNotAlreadyInStandby();
Eric Laurent81784c32012-11-19 14:55:58 -08001965
1966 // Call the HAL standby method unconditionally, and don't change mStandby flag
Glenn Kastene198c362013-08-13 09:13:36 -07001967 void inputStandBy();
Eric Laurent81784c32012-11-19 14:55:58 -08001968
Eric Laurentd8365c52017-07-16 15:27:05 -07001969 void checkBtNrec_l();
1970
Eric Laurentec376dc2021-04-08 20:41:22 +02001971 int32_t getOldestFront_l();
1972 void updateFronts_l(int32_t offset);
1973
Eric Laurent81784c32012-11-19 14:55:58 -08001974 AudioStreamIn *mInput;
Mikhail Naganov2534b382019-09-25 13:05:02 -07001975 Source *mSource;
Andy Hung3ff4b552023-06-26 19:20:57 -07001976 SortedVector <sp<IAfRecordTrack>> mTracks;
Glenn Kasten2b806402013-11-20 16:37:38 -08001977 // mActiveTracks has dual roles: it indicates the current active track(s), and
Eric Laurent81784c32012-11-19 14:55:58 -08001978 // is used together with mStartStopCond to indicate start()/stop() progress
Andy Hung3ff4b552023-06-26 19:20:57 -07001979 ActiveTracks<IAfRecordTrack> mActiveTracks;
Andy Hungdae27702016-10-31 14:01:16 -07001980
Eric Laurent81784c32012-11-19 14:55:58 -08001981 Condition mStartStopCond;
Glenn Kasten9b58f632013-07-16 11:37:48 -07001982
Glenn Kasten85948432013-08-19 12:09:05 -07001983 // resampler converts input at HAL Hz to output at AudioRecord client Hz
Glenn Kasten1b291842016-07-18 14:55:21 -07001984 void *mRsmpInBuffer; // size = mRsmpInFramesOA
Glenn Kasten85948432013-08-19 12:09:05 -07001985 size_t mRsmpInFrames; // size of resampler input in frames
1986 size_t mRsmpInFramesP2;// size rounded up to a power-of-2
Glenn Kasten1b291842016-07-18 14:55:21 -07001987 size_t mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001988
1989 // rolling index that is never cleared
Glenn Kasten85948432013-08-19 12:09:05 -07001990 int32_t mRsmpInRear; // last filled frame + 1
Glenn Kasten85948432013-08-19 12:09:05 -07001991
Eric Laurent81784c32012-11-19 14:55:58 -08001992 // For dumpsys
Glenn Kastenb880f5e2014-05-07 08:43:45 -07001993 const sp<MemoryDealer> mReadOnlyHeap;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001994
1995 // one-time initialization, no locks required
Glenn Kastenb187de12014-12-30 08:18:15 -08001996 sp<FastCapture> mFastCapture; // non-0 if there is also
1997 // a fast capture
Eric Laurent72e3f392015-05-20 14:43:50 -07001998
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001999 // FIXME audio watchdog thread
2000
2001 // contents are not guaranteed to be consistent, no locks required
2002 FastCaptureDumpState mFastCaptureDumpState;
2003#ifdef STATE_QUEUE_DUMP
2004 // FIXME StateQueue observer and mutator dump fields
2005#endif
2006 // FIXME audio watchdog dump
2007
2008 // accessible only within the threadLoop(), no locks required
2009 // mFastCapture->sq() // for mutating and pushing state
2010 int32_t mFastCaptureFutex; // for cold idle
2011
2012 // The HAL input source is treated as non-blocking,
2013 // but current implementation is blocking
2014 sp<NBAIO_Source> mInputSource;
2015 // The source for the normal capture thread to read from: mInputSource or mPipeSource
2016 sp<NBAIO_Source> mNormalSource;
2017 // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
2018 // otherwise clear
2019 sp<NBAIO_Sink> mPipeSink;
2020 // If a fast capture is present, the non-blocking pipe source read by normal thread,
2021 // otherwise clear
2022 sp<NBAIO_Source> mPipeSource;
2023 // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
2024 size_t mPipeFramesP2;
2025 // If a fast capture is present, the Pipe as IMemory, otherwise clear
2026 sp<IMemory> mPipeMemory;
2027
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -07002028 // TODO: add comment and adjust size as needed
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07002029 static const size_t kFastCaptureLogSize = 4 * 1024;
2030 sp<NBLog::Writer> mFastCaptureNBLogWriter;
2031
2032 bool mFastTrackAvail; // true if fast track available
Eric Laurentd8365c52017-07-16 15:27:05 -07002033 // common state to all record threads
2034 std::atomic_bool mBtNrecSuspended;
Andy Hung6427e442018-08-09 12:51:02 -07002035
2036 int64_t mFramesRead = 0; // continuous running counter.
jiabinc52b1ff2019-10-31 17:20:42 -07002037
2038 DeviceDescriptorBaseVector mOutDevices;
Eric Laurentec376dc2021-04-08 20:41:22 +02002039
Eric Laurent5f0fd7b2021-05-07 16:33:26 +02002040 int32_t mMaxSharedAudioHistoryMs = 0;
Eric Laurentec376dc2021-04-08 20:41:22 +02002041 std::string mSharedAudioPackageName = {};
Eric Laurent2407ce32021-04-26 14:56:03 +02002042 int32_t mSharedAudioStartFrames = -1;
Eric Laurentec376dc2021-04-08 20:41:22 +02002043 audio_session_t mSharedAudioSessionId = AUDIO_SESSION_NONE;
Eric Laurent81784c32012-11-19 14:55:58 -08002044};
Eric Laurent6acd1d42017-01-04 14:23:29 -08002045
Andy Hung667dec42023-07-07 15:58:48 -07002046class MmapThread : public ThreadBase, public virtual IAfMmapThread
Eric Laurent6acd1d42017-01-04 14:23:29 -08002047{
2048 public:
Eric Laurent6acd1d42017-01-04 14:23:29 -08002049 MmapThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
Andy Hung71ba4b32022-10-06 12:09:49 -07002050 AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady,
Andy Hungcf10d742020-04-28 15:38:24 -07002051 bool isOut);
Andy Hung4989d312023-06-29 21:19:25 -07002052 ~MmapThread() override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002053
Andy Hung667dec42023-07-07 15:58:48 -07002054 void configure(const audio_attributes_t* attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08002055 audio_stream_type_t streamType,
2056 audio_session_t sessionId,
2057 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07002058 audio_port_handle_t deviceId,
Andy Hung667dec42023-07-07 15:58:48 -07002059 audio_port_handle_t portId) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002060
Andy Hung667dec42023-07-07 15:58:48 -07002061 void disconnect() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002062
Andy Hung4989d312023-06-29 21:19:25 -07002063 // MmapStreamInterface for adapter.
Andy Hung667dec42023-07-07 15:58:48 -07002064 status_t createMmapBuffer(int32_t minSizeFrames, struct audio_mmap_buffer_info* info) final;
2065 status_t getMmapPosition(struct audio_mmap_position* position) const override;
2066 status_t start(const AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -07002067 const audio_attributes_t *attr,
Andy Hung667dec42023-07-07 15:58:48 -07002068 audio_port_handle_t* handle) final;
2069 status_t stop(audio_port_handle_t handle) final;
2070 status_t standby() final;
2071 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const = 0;
2072 status_t reportData(const void* buffer, size_t frameCount) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002073
2074 // RefBase
Andy Hung4989d312023-06-29 21:19:25 -07002075 void onFirstRef() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002076
2077 // Thread virtuals
Andy Hung4989d312023-06-29 21:19:25 -07002078 bool threadLoop() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002079
Andy Hung4989d312023-06-29 21:19:25 -07002080 // Not in ThreadBase
2081 virtual void threadLoop_exit() final;
2082 virtual void threadLoop_standby() final;
2083 virtual bool shouldStandby_l() final { return false; }
2084 virtual status_t exitStandby_l() REQUIRES(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002085
Andy Hung4989d312023-06-29 21:19:25 -07002086 status_t initCheck() const final { return mHalStream == nullptr ? NO_INIT : NO_ERROR; }
2087 size_t frameCount() const final { return mFrameCount; }
2088 bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) final;
2089 String8 getParameters(const String8& keys) final;
2090 void ioConfigChanged(audio_io_config_event_t event, pid_t pid = 0,
2091 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002092 void readHalParameters_l();
Andy Hung4989d312023-06-29 21:19:25 -07002093 void cacheParameters_l() final {}
2094 status_t createAudioPatch_l(
2095 const struct audio_patch* patch, audio_patch_handle_t* handle) final;
2096 status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final;
2097 void toAudioPortConfig(struct audio_port_config* config) override;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002098
Andy Hung4989d312023-06-29 21:19:25 -07002099 sp<StreamHalInterface> stream() const final { return mHalStream; }
2100 status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final;
2101 size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final;
2102 status_t checkEffectCompatibility_l(
2103 const effect_descriptor_t *desc, audio_session_t sessionId) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002104
Andy Hung4989d312023-06-29 21:19:25 -07002105 uint32_t hasAudioSession_l(audio_session_t sessionId) const override {
Andy Hungc3d62f92019-03-14 13:38:51 -07002106 // Note: using mActiveTracks as no mTracks here.
2107 return ThreadBase::hasAudioSession_l(sessionId, mActiveTracks);
2108 }
Andy Hung4989d312023-06-29 21:19:25 -07002109 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final;
2110 bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002111
Andy Hung4989d312023-06-29 21:19:25 -07002112 virtual void checkSilentMode_l() {} // cannot be const (RecordThread)
2113 virtual void processVolume_l() {}
Eric Laurent6acd1d42017-01-04 14:23:29 -08002114 void checkInvalidTracks_l();
2115
Andy Hung4989d312023-06-29 21:19:25 -07002116 // Not in ThreadBase
2117 virtual audio_stream_type_t streamType() const { return AUDIO_STREAM_DEFAULT; }
2118 virtual void invalidateTracks(audio_stream_type_t /* streamType */) {}
Andy Hung667dec42023-07-07 15:58:48 -07002119 void invalidateTracks(std::set<audio_port_handle_t>& /* portIds */) override {}
Eric Laurent6acd1d42017-01-04 14:23:29 -08002120
Eric Laurent331679c2018-04-16 17:03:16 -07002121 // Sets the UID records silence
Andy Hung667dec42023-07-07 15:58:48 -07002122 void setRecordSilenced(
2123 audio_port_handle_t /* portId */, bool /* silenced */) override {}
Eric Laurent331679c2018-04-16 17:03:16 -07002124
Andy Hung4989d312023-06-29 21:19:25 -07002125 bool isStreamInitialized() const override { return false; }
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002126
jiabincfc10a42022-06-15 19:26:01 +00002127 void setClientSilencedState_l(audio_port_handle_t portId, bool silenced) {
2128 mClientSilencedStates[portId] = silenced;
2129 }
2130
2131 size_t eraseClientSilencedState_l(audio_port_handle_t portId) {
2132 return mClientSilencedStates.erase(portId);
2133 }
2134
2135 bool isClientSilenced_l(audio_port_handle_t portId) const {
2136 const auto it = mClientSilencedStates.find(portId);
2137 return it != mClientSilencedStates.end() ? it->second : false;
2138 }
2139
2140 void setClientSilencedIfExists_l(audio_port_handle_t portId, bool silenced) {
2141 const auto it = mClientSilencedStates.find(portId);
2142 if (it != mClientSilencedStates.end()) {
2143 it->second = silenced;
2144 }
2145 }
2146
Eric Laurent6acd1d42017-01-04 14:23:29 -08002147 protected:
Andy Hung4989d312023-06-29 21:19:25 -07002148 void dumpInternals_l(int fd, const Vector<String16>& args) override;
2149 void dumpTracks_l(int fd, const Vector<String16>& args) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002150
jiabinc52b1ff2019-10-31 17:20:42 -07002151 /**
2152 * @brief mDeviceId current device port unique identifier
2153 */
2154 audio_port_handle_t mDeviceId = AUDIO_PORT_HANDLE_NONE;
2155
Eric Laurent6acd1d42017-01-04 14:23:29 -08002156 audio_attributes_t mAttr;
2157 audio_session_t mSessionId;
2158 audio_port_handle_t mPortId;
2159
Phil Burk7f6b40d2017-02-09 13:18:38 -08002160 wp<MmapStreamCallback> mCallback;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002161 sp<StreamHalInterface> mHalStream;
2162 sp<DeviceHalInterface> mHalDevice;
2163 AudioHwDevice* const mAudioHwDev;
Andy Hung3ff4b552023-06-26 19:20:57 -07002164 ActiveTracks<IAfMmapTrack> mActiveTracks;
Eric Laurent67f97292018-04-20 18:05:41 -07002165 float mHalVolFloat;
jiabincfc10a42022-06-15 19:26:01 +00002166 std::map<audio_port_handle_t, bool> mClientSilencedStates;
Eric Laurent331679c2018-04-16 17:03:16 -07002167
2168 int32_t mNoCallbackWarningCount;
2169 static constexpr int32_t kMaxNoCallbackWarnings = 5;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002170};
2171
Andy Hung667dec42023-07-07 15:58:48 -07002172class MmapPlaybackThread : public MmapThread, public IAfMmapPlaybackThread,
2173 public virtual VolumeInterface {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002174public:
2175 MmapPlaybackThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
jiabinc52b1ff2019-10-31 17:20:42 -07002176 AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002177
Andy Hung667dec42023-07-07 15:58:48 -07002178 sp<IAfMmapPlaybackThread> asIAfMmapPlaybackThread() final {
2179 return sp<IAfMmapPlaybackThread>::fromExisting(this);
2180 }
2181
Andy Hung4989d312023-06-29 21:19:25 -07002182 void configure(const audio_attributes_t* attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08002183 audio_stream_type_t streamType,
2184 audio_session_t sessionId,
2185 const sp<MmapStreamCallback>& callback,
Eric Laurent7aa0ccb2017-08-28 11:12:52 -07002186 audio_port_handle_t deviceId,
Andy Hung4989d312023-06-29 21:19:25 -07002187 audio_port_handle_t portId) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002188
Andy Hung667dec42023-07-07 15:58:48 -07002189 AudioStreamOut* clearOutput() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002190
2191 // VolumeInterface
Andy Hung4989d312023-06-29 21:19:25 -07002192 void setMasterVolume(float value) final;
2193 void setMasterBalance(float /* value */) final {} // Needs implementation?
2194 void setMasterMute(bool muted) final;
2195 void setStreamVolume(audio_stream_type_t stream, float value) final;
2196 void setStreamMute(audio_stream_type_t stream, bool muted) final;
2197 float streamVolume(audio_stream_type_t stream) const final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002198
2199 void setMasterMute_l(bool muted) { mMasterMute = muted; }
2200
Andy Hung4989d312023-06-29 21:19:25 -07002201 void invalidateTracks(audio_stream_type_t streamType) final;
2202 void invalidateTracks(std::set<audio_port_handle_t>& portIds) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002203
Andy Hung4989d312023-06-29 21:19:25 -07002204 audio_stream_type_t streamType() const final { return mStreamType; }
2205 void checkSilentMode_l() final;
2206 void processVolume_l() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002207
Andy Hung4989d312023-06-29 21:19:25 -07002208 MetadataUpdate updateMetadata_l() final;
Kevin Rocard069c2712018-03-29 19:09:14 -07002209
Andy Hung4989d312023-06-29 21:19:25 -07002210 void toAudioPortConfig(struct audio_port_config* config) final;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07002211
Andy Hung4989d312023-06-29 21:19:25 -07002212 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final;
jiabinb7d8c5a2020-08-26 17:24:52 -07002213
Andy Hung4989d312023-06-29 21:19:25 -07002214 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002215 return !(mOutput == nullptr || mOutput->stream == nullptr);
2216 }
2217
Andy Hung4989d312023-06-29 21:19:25 -07002218 status_t reportData(const void* buffer, size_t frameCount) final;
jiabinfc791ee2023-02-15 19:43:40 +00002219
Andy Hung4989d312023-06-29 21:19:25 -07002220 void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) final;
2221 void stopMelComputation_l() final;
Vlad Popa6fbbfbf2023-02-22 15:05:43 +01002222
Eric Laurent6acd1d42017-01-04 14:23:29 -08002223protected:
Andy Hung4989d312023-06-29 21:19:25 -07002224 void dumpInternals_l(int fd, const Vector<String16>& args) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002225
2226 audio_stream_type_t mStreamType;
2227 float mMasterVolume;
2228 float mStreamVolume;
2229 bool mMasterMute;
2230 bool mStreamMute;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002231 AudioStreamOut* mOutput;
Vlad Popa6fbbfbf2023-02-22 15:05:43 +01002232
2233 mediautils::atomic_sp<audio_utils::MelProcessor> mMelProcessor;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002234};
2235
Andy Hung667dec42023-07-07 15:58:48 -07002236class MmapCaptureThread : public MmapThread, public IAfMmapCaptureThread
Eric Laurent6acd1d42017-01-04 14:23:29 -08002237{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002238public:
2239 MmapCaptureThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
jiabinc52b1ff2019-10-31 17:20:42 -07002240 AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002241
Andy Hung667dec42023-07-07 15:58:48 -07002242 sp<IAfMmapCaptureThread> asIAfMmapCaptureThread() final {
2243 return sp<IAfMmapCaptureThread>::fromExisting(this);
2244 }
2245
2246 AudioStreamIn* clearInput() final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002247
Andy Hung4989d312023-06-29 21:19:25 -07002248 status_t exitStandby_l() REQUIRES(mLock) final;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002249
Andy Hung4989d312023-06-29 21:19:25 -07002250 MetadataUpdate updateMetadata_l() final;
2251 void processVolume_l() final;
2252 void setRecordSilenced(audio_port_handle_t portId, bool silenced) final;
Kevin Rocard069c2712018-03-29 19:09:14 -07002253
Andy Hung4989d312023-06-29 21:19:25 -07002254 void toAudioPortConfig(struct audio_port_config* config) final;
Mikhail Naganov32abc2b2018-05-24 12:57:11 -07002255
Andy Hung4989d312023-06-29 21:19:25 -07002256 status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final;
jiabinb7d8c5a2020-08-26 17:24:52 -07002257
Andy Hung4989d312023-06-29 21:19:25 -07002258 bool isStreamInitialized() const final {
Jasmine Chaeaa10e42021-05-11 10:11:14 +08002259 return !(mInput == nullptr || mInput->stream == nullptr);
2260 }
2261
Eric Laurent6acd1d42017-01-04 14:23:29 -08002262protected:
2263
2264 AudioStreamIn* mInput;
2265};
jiabinc658e452022-10-21 20:52:21 +00002266
2267class BitPerfectThread : public MixerThread {
2268public:
2269 BitPerfectThread(const sp<AudioFlinger>& audioflinger, AudioStreamOut *output,
2270 audio_io_handle_t id, bool systemReady);
2271
2272protected:
Andy Hung4989d312023-06-29 21:19:25 -07002273 mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final;
2274 void threadLoop_mix() final;
jiabinc658e452022-10-21 20:52:21 +00002275
2276private:
2277 bool mIsBitPerfect;
jiabin76d94692022-12-15 21:51:21 +00002278 float mVolumeLeft = 0.f;
2279 float mVolumeRight = 0.f;
jiabinc658e452022-10-21 20:52:21 +00002280};
Andy Hungaaa18282023-06-23 19:27:19 -07002281
Andy Hung4989d312023-06-29 21:19:25 -07002282private: