blob: 4d1b129a0809d79838049af795c8242c0d524a45 [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
22class ThreadBase : public Thread {
23public:
24
25#include "TrackBase.h"
26
27 enum type_t {
28 MIXER, // Thread class is MixerThread
29 DIRECT, // Thread class is DirectOutputThread
30 DUPLICATING, // Thread class is DuplicatingThread
Eric Laurentbfb1b832013-01-07 09:53:42 -080031 RECORD, // Thread class is RecordThread
Eric Laurent6acd1d42017-01-04 14:23:29 -080032 OFFLOAD, // Thread class is OffloadThread
33 MMAP // control thread for MMAP stream
Eric Laurent81784c32012-11-19 14:55:58 -080034 };
35
Glenn Kasten97b7b752014-09-28 13:04:24 -070036 static const char *threadTypeToString(type_t type);
37
Eric Laurent81784c32012-11-19 14:55:58 -080038 ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
Eric Laurent72e3f392015-05-20 14:43:50 -070039 audio_devices_t outDevice, audio_devices_t inDevice, type_t type,
40 bool systemReady);
Eric Laurent81784c32012-11-19 14:55:58 -080041 virtual ~ThreadBase();
42
Glenn Kastencf04c2c2013-08-06 07:41:16 -070043 virtual status_t readyToRun();
44
Eric Laurent81784c32012-11-19 14:55:58 -080045 void dumpBase(int fd, const Vector<String16>& args);
46 void dumpEffectChains(int fd, const Vector<String16>& args);
47
48 void clearPowerManager();
49
50 // base for record and playback
51 enum {
52 CFG_EVENT_IO,
Eric Laurent10351942014-05-08 18:49:52 -070053 CFG_EVENT_PRIO,
54 CFG_EVENT_SET_PARAMETER,
Eric Laurent1c333e22014-05-20 10:48:17 -070055 CFG_EVENT_CREATE_AUDIO_PATCH,
56 CFG_EVENT_RELEASE_AUDIO_PATCH,
Eric Laurent81784c32012-11-19 14:55:58 -080057 };
58
Eric Laurent10351942014-05-08 18:49:52 -070059 class ConfigEventData: public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080060 public:
Eric Laurent10351942014-05-08 18:49:52 -070061 virtual ~ConfigEventData() {}
Eric Laurent81784c32012-11-19 14:55:58 -080062
63 virtual void dump(char *buffer, size_t size) = 0;
Eric Laurent10351942014-05-08 18:49:52 -070064 protected:
65 ConfigEventData() {}
Eric Laurent81784c32012-11-19 14:55:58 -080066 };
67
Eric Laurent10351942014-05-08 18:49:52 -070068 // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
69 // 1. create SetParameterConfigEvent. This sets mWaitStatus in config event
70 // 2. Lock mLock
71 // 3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
72 // 4. sendConfigEvent_l() reads status from event->mStatus;
73 // 5. sendConfigEvent_l() returns status
74 // 6. Unlock
75 //
76 // Parameter sequence by server: threadLoop calling processConfigEvents_l():
77 // 1. Lock mLock
78 // 2. If there is an entry in mConfigEvents proceed ...
79 // 3. Read first entry in mConfigEvents
80 // 4. Remove first entry from mConfigEvents
81 // 5. Process
82 // 6. Set event->mStatus
83 // 7. event->mCond.signal
84 // 8. Unlock
Eric Laurent81784c32012-11-19 14:55:58 -080085
Eric Laurent10351942014-05-08 18:49:52 -070086 class ConfigEvent: public RefBase {
87 public:
88 virtual ~ConfigEvent() {}
89
90 void dump(char *buffer, size_t size) { mData->dump(buffer, size); }
91
92 const int mType; // event type e.g. CFG_EVENT_IO
93 Mutex mLock; // mutex associated with mCond
94 Condition mCond; // condition for status return
95 status_t mStatus; // status communicated to sender
96 bool mWaitStatus; // true if sender is waiting for status
Eric Laurent72e3f392015-05-20 14:43:50 -070097 bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
Eric Laurent10351942014-05-08 18:49:52 -070098 sp<ConfigEventData> mData; // event specific parameter data
99
100 protected:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700101 explicit ConfigEvent(int type, bool requiresSystemReady = false) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700102 mType(type), mStatus(NO_ERROR), mWaitStatus(false),
103 mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
Eric Laurent10351942014-05-08 18:49:52 -0700104 };
105
106 class IoConfigEventData : public ConfigEventData {
107 public:
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700108 IoConfigEventData(audio_io_config_event event, pid_t pid) :
109 mEvent(event), mPid(pid) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800110
111 virtual void dump(char *buffer, size_t size) {
Eric Laurent73e26b62015-04-27 16:55:58 -0700112 snprintf(buffer, size, "IO event: event %d\n", mEvent);
Eric Laurent81784c32012-11-19 14:55:58 -0800113 }
114
Eric Laurent73e26b62015-04-27 16:55:58 -0700115 const audio_io_config_event mEvent;
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700116 const pid_t mPid;
Eric Laurent81784c32012-11-19 14:55:58 -0800117 };
118
Eric Laurent10351942014-05-08 18:49:52 -0700119 class IoConfigEvent : public ConfigEvent {
Eric Laurent81784c32012-11-19 14:55:58 -0800120 public:
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700121 IoConfigEvent(audio_io_config_event event, pid_t pid) :
Eric Laurent10351942014-05-08 18:49:52 -0700122 ConfigEvent(CFG_EVENT_IO) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700123 mData = new IoConfigEventData(event, pid);
Eric Laurent10351942014-05-08 18:49:52 -0700124 }
125 virtual ~IoConfigEvent() {}
126 };
Eric Laurent81784c32012-11-19 14:55:58 -0800127
Eric Laurent10351942014-05-08 18:49:52 -0700128 class PrioConfigEventData : public ConfigEventData {
129 public:
130 PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio) :
131 mPid(pid), mTid(tid), mPrio(prio) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800132
133 virtual void dump(char *buffer, size_t size) {
134 snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
135 }
136
Eric Laurent81784c32012-11-19 14:55:58 -0800137 const pid_t mPid;
138 const pid_t mTid;
139 const int32_t mPrio;
140 };
141
Eric Laurent10351942014-05-08 18:49:52 -0700142 class PrioConfigEvent : public ConfigEvent {
143 public:
144 PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
Eric Laurent72e3f392015-05-20 14:43:50 -0700145 ConfigEvent(CFG_EVENT_PRIO, true) {
Eric Laurent10351942014-05-08 18:49:52 -0700146 mData = new PrioConfigEventData(pid, tid, prio);
147 }
148 virtual ~PrioConfigEvent() {}
149 };
150
151 class SetParameterConfigEventData : public ConfigEventData {
152 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700153 explicit SetParameterConfigEventData(String8 keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700154 mKeyValuePairs(keyValuePairs) {}
155
156 virtual void dump(char *buffer, size_t size) {
157 snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string());
158 }
159
160 const String8 mKeyValuePairs;
161 };
162
163 class SetParameterConfigEvent : public ConfigEvent {
164 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700165 explicit SetParameterConfigEvent(String8 keyValuePairs) :
Eric Laurent10351942014-05-08 18:49:52 -0700166 ConfigEvent(CFG_EVENT_SET_PARAMETER) {
167 mData = new SetParameterConfigEventData(keyValuePairs);
168 mWaitStatus = true;
169 }
170 virtual ~SetParameterConfigEvent() {}
171 };
172
Eric Laurent1c333e22014-05-20 10:48:17 -0700173 class CreateAudioPatchConfigEventData : public ConfigEventData {
174 public:
175 CreateAudioPatchConfigEventData(const struct audio_patch patch,
176 audio_patch_handle_t handle) :
177 mPatch(patch), mHandle(handle) {}
178
179 virtual void dump(char *buffer, size_t size) {
180 snprintf(buffer, size, "Patch handle: %u\n", mHandle);
181 }
182
183 const struct audio_patch mPatch;
184 audio_patch_handle_t mHandle;
185 };
186
187 class CreateAudioPatchConfigEvent : public ConfigEvent {
188 public:
189 CreateAudioPatchConfigEvent(const struct audio_patch patch,
190 audio_patch_handle_t handle) :
191 ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
192 mData = new CreateAudioPatchConfigEventData(patch, handle);
193 mWaitStatus = true;
194 }
195 virtual ~CreateAudioPatchConfigEvent() {}
196 };
197
198 class ReleaseAudioPatchConfigEventData : public ConfigEventData {
199 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700200 explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700201 mHandle(handle) {}
202
203 virtual void dump(char *buffer, size_t size) {
204 snprintf(buffer, size, "Patch handle: %u\n", mHandle);
205 }
206
207 audio_patch_handle_t mHandle;
208 };
209
210 class ReleaseAudioPatchConfigEvent : public ConfigEvent {
211 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700212 explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
Eric Laurent1c333e22014-05-20 10:48:17 -0700213 ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
214 mData = new ReleaseAudioPatchConfigEventData(handle);
215 mWaitStatus = true;
216 }
217 virtual ~ReleaseAudioPatchConfigEvent() {}
218 };
Eric Laurent81784c32012-11-19 14:55:58 -0800219
220 class PMDeathRecipient : public IBinder::DeathRecipient {
221 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700222 explicit PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800223 virtual ~PMDeathRecipient() {}
224
225 // IBinder::DeathRecipient
226 virtual void binderDied(const wp<IBinder>& who);
227
228 private:
229 PMDeathRecipient(const PMDeathRecipient&);
230 PMDeathRecipient& operator = (const PMDeathRecipient&);
231
232 wp<ThreadBase> mThread;
233 };
234
235 virtual status_t initCheck() const = 0;
236
237 // static externally-visible
238 type_t type() const { return mType; }
Eric Laurentf6870ae2015-05-08 10:50:03 -0700239 bool isDuplicating() const { return (mType == DUPLICATING); }
240
Eric Laurent81784c32012-11-19 14:55:58 -0800241 audio_io_handle_t id() const { return mId;}
242
243 // dynamic externally-visible
244 uint32_t sampleRate() const { return mSampleRate; }
Eric Laurent81784c32012-11-19 14:55:58 -0800245 audio_channel_mask_t channelMask() const { return mChannelMask; }
Andy Hung463be252014-07-10 16:56:07 -0700246 audio_format_t format() const { return mHALFormat; }
Eric Laurent83b88082014-06-20 18:31:16 -0700247 uint32_t channelCount() const { return mChannelCount; }
Eric Laurent81784c32012-11-19 14:55:58 -0800248 // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
Glenn Kasten9b58f632013-07-16 11:37:48 -0700249 // and returns the [normal mix] buffer's frame count.
250 virtual size_t frameCount() const = 0;
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700251
252 // Return's the HAL's frame count i.e. fast mixer buffer size.
253 size_t frameCountHAL() const { return mFrameCount; }
254
Eric Laurentbfb1b832013-01-07 09:53:42 -0800255 size_t frameSize() const { return mFrameSize; }
Eric Laurent81784c32012-11-19 14:55:58 -0800256
257 // Should be "virtual status_t requestExitAndWait()" and override same
258 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
259 void exit();
Eric Laurent10351942014-05-08 18:49:52 -0700260 virtual bool checkForNewParameter_l(const String8& keyValuePair,
261 status_t& status) = 0;
Eric Laurent81784c32012-11-19 14:55:58 -0800262 virtual status_t setParameters(const String8& keyValuePairs);
263 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700264 virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0) = 0;
Eric Laurent10351942014-05-08 18:49:52 -0700265 // sendConfigEvent_l() must be called with ThreadBase::mLock held
266 // Can temporarily release the lock if waiting for a reply from
267 // processConfigEvents_l().
268 status_t sendConfigEvent_l(sp<ConfigEvent>& event);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700269 void sendIoConfigEvent(audio_io_config_event event, pid_t pid = 0);
270 void sendIoConfigEvent_l(audio_io_config_event event, pid_t pid = 0);
Eric Laurent72e3f392015-05-20 14:43:50 -0700271 void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio);
Eric Laurent81784c32012-11-19 14:55:58 -0800272 void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
Eric Laurent10351942014-05-08 18:49:52 -0700273 status_t sendSetParameterConfigEvent_l(const String8& keyValuePair);
Eric Laurent1c333e22014-05-20 10:48:17 -0700274 status_t sendCreateAudioPatchConfigEvent(const struct audio_patch *patch,
275 audio_patch_handle_t *handle);
276 status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle);
Eric Laurent021cf962014-05-13 10:18:14 -0700277 void processConfigEvents_l();
Eric Laurent10351942014-05-08 18:49:52 -0700278 virtual void cacheParameters_l() = 0;
Eric Laurent1c333e22014-05-20 10:48:17 -0700279 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
280 audio_patch_handle_t *handle) = 0;
281 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
Eric Laurent83b88082014-06-20 18:31:16 -0700282 virtual void getAudioPortConfig(struct audio_port_config *config) = 0;
Eric Laurent1c333e22014-05-20 10:48:17 -0700283
Eric Laurent81784c32012-11-19 14:55:58 -0800284
285 // see note at declaration of mStandby, mOutDevice and mInDevice
286 bool standby() const { return mStandby; }
287 audio_devices_t outDevice() const { return mOutDevice; }
288 audio_devices_t inDevice() const { return mInDevice; }
289
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700290 virtual sp<StreamHalInterface> stream() const = 0;
Eric Laurent81784c32012-11-19 14:55:58 -0800291
292 sp<EffectHandle> createEffect_l(
293 const sp<AudioFlinger::Client>& client,
294 const sp<IEffectClient>& effectClient,
295 int32_t priority,
Glenn Kastend848eb42016-03-08 13:42:11 -0800296 audio_session_t sessionId,
Eric Laurent81784c32012-11-19 14:55:58 -0800297 effect_descriptor_t *desc,
298 int *enabled,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800299 status_t *status /*non-NULL*/,
300 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800301
302 // return values for hasAudioSession (bit field)
303 enum effect_state {
304 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
305 // effect
Eric Laurent4c415062016-06-17 16:14:16 -0700306 TRACK_SESSION = 0x2, // the audio session corresponds to at least one
Eric Laurent81784c32012-11-19 14:55:58 -0800307 // track
Eric Laurent4c415062016-06-17 16:14:16 -0700308 FAST_SESSION = 0x4 // the audio session corresponds to at least one
309 // fast track
Eric Laurent81784c32012-11-19 14:55:58 -0800310 };
311
312 // get effect chain corresponding to session Id.
Glenn Kastend848eb42016-03-08 13:42:11 -0800313 sp<EffectChain> getEffectChain(audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800314 // same as getEffectChain() but must be called with ThreadBase mutex locked
Glenn Kastend848eb42016-03-08 13:42:11 -0800315 sp<EffectChain> getEffectChain_l(audio_session_t sessionId) const;
Eric Laurent81784c32012-11-19 14:55:58 -0800316 // add an effect chain to the chain list (mEffectChains)
317 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
318 // remove an effect chain from the chain list (mEffectChains)
319 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
320 // lock all effect chains Mutexes. Must be called before releasing the
321 // ThreadBase mutex before processing the mixer and effects. This guarantees the
322 // integrity of the chains during the process.
323 // Also sets the parameter 'effectChains' to current value of mEffectChains.
324 void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
325 // unlock effect chains after process
326 void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800327 // get a copy of mEffectChains vector
328 Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
Eric Laurent81784c32012-11-19 14:55:58 -0800329 // set audio mode to all effect chains
330 void setMode(audio_mode_t mode);
331 // get effect module with corresponding ID on specified audio session
Glenn Kastend848eb42016-03-08 13:42:11 -0800332 sp<AudioFlinger::EffectModule> getEffect(audio_session_t sessionId, int effectId);
333 sp<AudioFlinger::EffectModule> getEffect_l(audio_session_t sessionId, int effectId);
Eric Laurent81784c32012-11-19 14:55:58 -0800334 // add and effect module. Also creates the effect chain is none exists for
335 // the effects audio session
336 status_t addEffect_l(const sp< EffectModule>& effect);
337 // remove and effect module. Also removes the effect chain is this was the last
338 // effect
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800339 void removeEffect_l(const sp< EffectModule>& effect, bool release = false);
340 // disconnect an effect handle from module and destroy module if last handle
341 void disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800342 // detach all tracks connected to an auxiliary effect
Glenn Kasten0f11b512014-01-31 16:18:54 -0800343 virtual void detachAuxEffect_l(int effectId __unused) {}
Eric Laurent4c415062016-06-17 16:14:16 -0700344 // returns a combination of:
345 // - EFFECT_SESSION if effects on this audio session exist in one chain
346 // - TRACK_SESSION if tracks on this audio session exist
347 // - FAST_SESSION if fast tracks on this audio session exist
348 virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
349 uint32_t hasAudioSession(audio_session_t sessionId) const {
350 Mutex::Autolock _l(mLock);
351 return hasAudioSession_l(sessionId);
352 }
353
Eric Laurent81784c32012-11-19 14:55:58 -0800354 // the value returned by default implementation is not important as the
355 // strategy is only meaningful for PlaybackThread which implements this method
Glenn Kastend848eb42016-03-08 13:42:11 -0800356 virtual uint32_t getStrategyForSession_l(audio_session_t sessionId __unused)
357 { return 0; }
Eric Laurent81784c32012-11-19 14:55:58 -0800358
359 // suspend or restore effect according to the type of effect passed. a NULL
360 // type pointer means suspend all effects in the session
361 void setEffectSuspended(const effect_uuid_t *type,
362 bool suspend,
Glenn Kastend848eb42016-03-08 13:42:11 -0800363 audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent81784c32012-11-19 14:55:58 -0800364 // check if some effects must be suspended/restored when an effect is enabled
365 // or disabled
366 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
367 bool enabled,
Glenn Kastend848eb42016-03-08 13:42:11 -0800368 audio_session_t sessionId =
369 AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent81784c32012-11-19 14:55:58 -0800370 void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
371 bool enabled,
Glenn Kastend848eb42016-03-08 13:42:11 -0800372 audio_session_t sessionId =
373 AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent81784c32012-11-19 14:55:58 -0800374
375 virtual status_t setSyncEvent(const sp<SyncEvent>& event) = 0;
376 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
377
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700378 // Return a reference to a per-thread heap which can be used to allocate IMemory
379 // objects that will be read-only to client processes, read/write to mediaserver,
380 // and shared by all client processes of the thread.
381 // The heap is per-thread rather than common across all threads, because
382 // clients can't be trusted not to modify the offset of the IMemory they receive.
383 // If a thread does not have such a heap, this method returns 0.
384 virtual sp<MemoryDealer> readOnlyHeap() const { return 0; }
Eric Laurent81784c32012-11-19 14:55:58 -0800385
Glenn Kasten6181ffd2014-05-13 10:41:52 -0700386 virtual sp<IMemory> pipeMemory() const { return 0; }
387
Eric Laurent72e3f392015-05-20 14:43:50 -0700388 void systemReady();
389
Eric Laurent4c415062016-06-17 16:14:16 -0700390 // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
391 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
392 audio_session_t sessionId) = 0;
393
Eric Laurent6acd1d42017-01-04 14:23:29 -0800394 void broadcast_l();
395
Eric Laurent81784c32012-11-19 14:55:58 -0800396 mutable Mutex mLock;
397
398protected:
399
400 // entry describing an effect being suspended in mSuspendedSessions keyed vector
401 class SuspendedSessionDesc : public RefBase {
402 public:
403 SuspendedSessionDesc() : mRefCount(0) {}
404
405 int mRefCount; // number of active suspend requests
406 effect_uuid_t mType; // effect type UUID
407 };
408
Andy Hungdae27702016-10-31 14:01:16 -0700409 void acquireWakeLock();
410 virtual void acquireWakeLock_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800411 void releaseWakeLock();
412 void releaseWakeLock_l();
Andy Hungd01b0f12016-11-07 16:10:30 -0800413 void updateWakeLockUids_l(const SortedVector<uid_t> &uids);
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800414 void getPowerManager_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800415 void setEffectSuspended_l(const effect_uuid_t *type,
416 bool suspend,
Glenn Kastend848eb42016-03-08 13:42:11 -0800417 audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800418 // updated mSuspendedSessions when an effect suspended or restored
419 void updateSuspendedSessions_l(const effect_uuid_t *type,
420 bool suspend,
Glenn Kastend848eb42016-03-08 13:42:11 -0800421 audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800422 // check if some effects must be suspended when an effect chain is added
423 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
424
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100425 String16 getWakeLockTag();
426
Eric Laurent81784c32012-11-19 14:55:58 -0800427 virtual void preExit() { }
Andy Hung2ddee192015-12-18 17:34:44 -0800428 virtual void setMasterMono_l(bool mono __unused) { }
429 virtual bool requireMonoBlend() { return false; }
Eric Laurent81784c32012-11-19 14:55:58 -0800430
431 friend class AudioFlinger; // for mEffectChains
432
433 const type_t mType;
434
435 // Used by parameters, config events, addTrack_l, exit
436 Condition mWaitWorkCV;
437
438 const sp<AudioFlinger> mAudioFlinger;
Glenn Kasten9b58f632013-07-16 11:37:48 -0700439
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800440 // updated by PlaybackThread::readOutputParameters_l() or
441 // RecordThread::readInputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -0800442 uint32_t mSampleRate;
443 size_t mFrameCount; // output HAL, direct output, record
Eric Laurent81784c32012-11-19 14:55:58 -0800444 audio_channel_mask_t mChannelMask;
Glenn Kastenf6ed4232013-07-16 11:16:27 -0700445 uint32_t mChannelCount;
Eric Laurent81784c32012-11-19 14:55:58 -0800446 size_t mFrameSize;
Glenn Kasten97b7b752014-09-28 13:04:24 -0700447 // not HAL frame size, this is for output sink (to pipe to fast mixer)
Andy Hung463be252014-07-10 16:56:07 -0700448 audio_format_t mFormat; // Source format for Recording and
449 // Sink format for Playback.
450 // Sink format may be different than
451 // HAL format if Fastmixer is used.
452 audio_format_t mHALFormat;
Glenn Kasten70949c42013-08-06 07:40:12 -0700453 size_t mBufferSize; // HAL buffer size for read() or write()
Eric Laurent81784c32012-11-19 14:55:58 -0800454
Eric Laurent10351942014-05-08 18:49:52 -0700455 Vector< sp<ConfigEvent> > mConfigEvents;
Eric Laurent72e3f392015-05-20 14:43:50 -0700456 Vector< sp<ConfigEvent> > mPendingConfigEvents; // events awaiting system ready
Eric Laurent81784c32012-11-19 14:55:58 -0800457
458 // These fields are written and read by thread itself without lock or barrier,
Glenn Kasten4944acb2013-08-19 08:39:20 -0700459 // and read by other threads without lock or barrier via standby(), outDevice()
Eric Laurent81784c32012-11-19 14:55:58 -0800460 // and inDevice().
461 // Because of the absence of a lock or barrier, any other thread that reads
462 // these fields must use the information in isolation, or be prepared to deal
463 // with possibility that it might be inconsistent with other information.
Glenn Kasten4944acb2013-08-19 08:39:20 -0700464 bool mStandby; // Whether thread is currently in standby.
Eric Laurent81784c32012-11-19 14:55:58 -0800465 audio_devices_t mOutDevice; // output device
466 audio_devices_t mInDevice; // input device
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700467 audio_devices_t mPrevOutDevice; // previous output device
Eric Laurente8726fe2015-06-26 09:39:24 -0700468 audio_devices_t mPrevInDevice; // previous input device
Eric Laurent296fb132015-05-01 11:38:42 -0700469 struct audio_patch mPatch;
Glenn Kastenf59497b2015-01-26 16:35:47 -0800470 audio_source_t mAudioSource;
Eric Laurent81784c32012-11-19 14:55:58 -0800471
472 const audio_io_handle_t mId;
473 Vector< sp<EffectChain> > mEffectChains;
474
Glenn Kastend7dca052015-03-05 16:05:54 -0800475 static const int kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
476 char mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
Eric Laurent81784c32012-11-19 14:55:58 -0800477 sp<IPowerManager> mPowerManager;
478 sp<IBinder> mWakeLockToken;
479 const sp<PMDeathRecipient> mDeathRecipient;
Glenn Kastend848eb42016-03-08 13:42:11 -0800480 // list of suspended effects per session and per type. The first (outer) vector is
481 // keyed by session ID, the second (inner) by type UUID timeLow field
482 KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
Eric Laurent81784c32012-11-19 14:55:58 -0800483 mSuspendedSessions;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800484 static const size_t kLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800485 sp<NBLog::Writer> mNBLogWriter;
Eric Laurent72e3f392015-05-20 14:43:50 -0700486 bool mSystemReady;
Andy Hung818e7a32016-02-16 18:08:07 -0800487 ExtendedTimestamp mTimestamp;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800488 // A condition that must be evaluated by the thread loop has changed and
489 // we must not wait for async write callback in the thread loop before evaluating it
490 bool mSignalPending;
Andy Hungdae27702016-10-31 14:01:16 -0700491
492 // ActiveTracks is a sorted vector of track type T representing the
493 // active tracks of threadLoop() to be considered by the locked prepare portion.
494 // ActiveTracks should be accessed with the ThreadBase lock held.
495 //
496 // During processing and I/O, the threadLoop does not hold the lock;
497 // hence it does not directly use ActiveTracks. Care should be taken
498 // to hold local strong references or defer removal of tracks
499 // if the threadLoop may still be accessing those tracks due to mix, etc.
500 //
501 // This class updates power information appropriately.
502 //
503
504 template <typename T>
505 class ActiveTracks {
506 public:
507 ActiveTracks()
508 : mActiveTracksGeneration(0)
509 , mLastActiveTracksGeneration(0)
510 { }
511
512 ~ActiveTracks() {
513 ALOGW_IF(!mActiveTracks.isEmpty(),
514 "ActiveTracks should be empty in destructor");
515 }
516 // returns the last track added (even though it may have been
517 // subsequently removed from ActiveTracks).
518 //
519 // Used for DirectOutputThread to ensure a flush is called when transitioning
520 // to a new track (even though it may be on the same session).
521 // Used for OffloadThread to ensure that volume and mixer state is
522 // taken from the latest track added.
523 //
524 // The latest track is saved with a weak pointer to prevent keeping an
525 // otherwise useless track alive. Thus the function will return nullptr
526 // if the latest track has subsequently been removed and destroyed.
527 sp<T> getLatest() {
528 return mLatestActiveTrack.promote();
529 }
530
531 // SortedVector methods
532 ssize_t add(const sp<T> &track);
533 ssize_t remove(const sp<T> &track);
534 size_t size() const {
535 return mActiveTracks.size();
536 }
537 ssize_t indexOf(const sp<T>& item) {
538 return mActiveTracks.indexOf(item);
539 }
540 sp<T> operator[](size_t index) const {
541 return mActiveTracks[index];
542 }
543 typename SortedVector<sp<T>>::iterator begin() {
544 return mActiveTracks.begin();
545 }
546 typename SortedVector<sp<T>>::iterator end() {
547 return mActiveTracks.end();
548 }
549
550 // Due to Binder recursion optimization, clear() and updatePowerState()
551 // cannot be called from a Binder thread because they may call back into
552 // the original calling process (system server) for BatteryNotifier
553 // (which requires a Java environment that may not be present).
554 // Hence, call clear() and updatePowerState() only from the
555 // ThreadBase thread.
556 void clear();
557 // periodically called in the threadLoop() to update power state uids.
558 void updatePowerState(sp<ThreadBase> thread, bool force = false);
559
560 private:
Andy Hungd01b0f12016-11-07 16:10:30 -0800561 SortedVector<uid_t> getWakeLockUids() {
562 SortedVector<uid_t> wakeLockUids;
Andy Hungdae27702016-10-31 14:01:16 -0700563 for (const sp<T> &track : mActiveTracks) {
564 wakeLockUids.add(track->uid());
565 }
566 return wakeLockUids; // moved by underlying SharedBuffer
567 }
568
569 std::map<uid_t, std::pair<ssize_t /* previous */, ssize_t /* current */>>
570 mBatteryCounter;
571 SortedVector<sp<T>> mActiveTracks;
572 int mActiveTracksGeneration;
573 int mLastActiveTracksGeneration;
574 wp<T> mLatestActiveTrack; // latest track added to ActiveTracks
575 };
Eric Laurent81784c32012-11-19 14:55:58 -0800576};
577
Eric Laurent6acd1d42017-01-04 14:23:29 -0800578class VolumeInterface {
579 public:
580
581 virtual ~VolumeInterface() {}
582
583 virtual void setMasterVolume(float value) = 0;
584 virtual void setMasterMute(bool muted) = 0;
585 virtual void setStreamVolume(audio_stream_type_t stream, float value) = 0;
586 virtual void setStreamMute(audio_stream_type_t stream, bool muted) = 0;
587 virtual float streamVolume(audio_stream_type_t stream) const = 0;
588
589};
590
Eric Laurent81784c32012-11-19 14:55:58 -0800591// --- PlaybackThread ---
Eric Laurent6acd1d42017-01-04 14:23:29 -0800592class PlaybackThread : public ThreadBase, public StreamOutHalInterfaceCallback,
593 public VolumeInterface {
Eric Laurent81784c32012-11-19 14:55:58 -0800594public:
595
596#include "PlaybackTracks.h"
597
598 enum mixer_state {
599 MIXER_IDLE, // no active tracks
600 MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready
Eric Laurentbfb1b832013-01-07 09:53:42 -0800601 MIXER_TRACKS_READY, // at least one active track, and at least one track has data
602 MIXER_DRAIN_TRACK, // drain currently playing track
603 MIXER_DRAIN_ALL, // fully drain the hardware
Eric Laurent81784c32012-11-19 14:55:58 -0800604 // standby mode does not have an enum value
605 // suspend by audio policy manager is orthogonal to mixer state
606 };
607
Eric Laurente93cc032016-05-05 10:15:10 -0700608 // retry count before removing active track in case of underrun on offloaded thread:
609 // we need to make sure that AudioTrack client has enough time to send large buffers
610 //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
611 // handled for offloaded tracks
612 static const int8_t kMaxTrackRetriesOffload = 20;
613 static const int8_t kMaxTrackStartupRetriesOffload = 100;
614 static const int8_t kMaxTrackStopRetriesOffload = 2;
Eric Laurentad7dd962016-09-22 12:38:37 -0700615 // 14 tracks max per client allows for 2 misbehaving application leaving 4 available tracks.
616 static const uint32_t kMaxTracksPerUid = 14;
Eric Laurente93cc032016-05-05 10:15:10 -0700617
Eric Laurent81784c32012-11-19 14:55:58 -0800618 PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Eric Laurente93cc032016-05-05 10:15:10 -0700619 audio_io_handle_t id, audio_devices_t device, type_t type, bool systemReady);
Eric Laurent81784c32012-11-19 14:55:58 -0800620 virtual ~PlaybackThread();
621
622 void dump(int fd, const Vector<String16>& args);
623
624 // Thread virtuals
Eric Laurent81784c32012-11-19 14:55:58 -0800625 virtual bool threadLoop();
626
627 // RefBase
628 virtual void onFirstRef();
629
Eric Laurent4c415062016-06-17 16:14:16 -0700630 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
631 audio_session_t sessionId);
632
Eric Laurent81784c32012-11-19 14:55:58 -0800633protected:
634 // Code snippets that were lifted up out of threadLoop()
635 virtual void threadLoop_mix() = 0;
636 virtual void threadLoop_sleepTime() = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800637 virtual ssize_t threadLoop_write();
638 virtual void threadLoop_drain();
Eric Laurent81784c32012-11-19 14:55:58 -0800639 virtual void threadLoop_standby();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800640 virtual void threadLoop_exit();
Eric Laurent81784c32012-11-19 14:55:58 -0800641 virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
642
643 // prepareTracks_l reads and writes mActiveTracks, and returns
644 // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller
645 // is responsible for clearing or destroying this Vector later on, when it
646 // is safe to do so. That will drop the final ref count and destroy the tracks.
647 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648 void removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
649
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700650 // StreamOutHalInterfaceCallback implementation
651 virtual void onWriteReady();
652 virtual void onDrainReady();
653 virtual void onError();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800654
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700655 void resetWriteBlocked(uint32_t sequence);
656 void resetDraining(uint32_t sequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800657
658 virtual bool waitingAsyncCallback();
659 virtual bool waitingAsyncCallback_l();
660 virtual bool shouldStandby_l();
Haynes Mathew George4c6a4332014-01-15 12:31:39 -0800661 virtual void onAddNewTrack_l();
Haynes Mathew George4527b9e2016-07-07 19:54:17 -0700662 void onAsyncError(); // error reported by AsyncCallbackThread
Eric Laurent81784c32012-11-19 14:55:58 -0800663
664 // ThreadBase virtuals
665 virtual void preExit();
666
Eric Laurent64667972016-03-30 18:19:46 -0700667 virtual bool keepWakeLock() const { return true; }
Andy Hungdae27702016-10-31 14:01:16 -0700668 virtual void acquireWakeLock_l() {
669 ThreadBase::acquireWakeLock_l();
670 mActiveTracks.updatePowerState(this, true /* force */);
671 }
Eric Laurent64667972016-03-30 18:19:46 -0700672
Eric Laurent81784c32012-11-19 14:55:58 -0800673public:
674
675 virtual status_t initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
676
677 // return estimated latency in milliseconds, as reported by HAL
678 uint32_t latency() const;
679 // same, but lock must already be held
680 uint32_t latency_l() const;
681
Eric Laurent6acd1d42017-01-04 14:23:29 -0800682 // VolumeInterface
683 virtual void setMasterVolume(float value);
684 virtual void setMasterMute(bool muted);
685 virtual void setStreamVolume(audio_stream_type_t stream, float value);
686 virtual void setStreamMute(audio_stream_type_t stream, bool muted);
687 virtual float streamVolume(audio_stream_type_t stream) const;
Eric Laurent81784c32012-11-19 14:55:58 -0800688
689 sp<Track> createTrack_l(
690 const sp<AudioFlinger::Client>& client,
691 audio_stream_type_t streamType,
692 uint32_t sampleRate,
693 audio_format_t format,
694 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800695 size_t *pFrameCount,
Eric Laurent81784c32012-11-19 14:55:58 -0800696 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -0800697 audio_session_t sessionId,
Eric Laurent05067782016-06-01 18:27:28 -0700698 audio_output_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -0800699 pid_t tid,
Andy Hung1f12a8a2016-11-07 16:10:30 -0800700 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800701 status_t *status /*non-NULL*/,
702 audio_port_handle_t portId);
Eric Laurent81784c32012-11-19 14:55:58 -0800703
704 AudioStreamOut* getOutput() const;
705 AudioStreamOut* clearOutput();
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700706 virtual sp<StreamHalInterface> stream() const;
Eric Laurent81784c32012-11-19 14:55:58 -0800707
708 // a very large number of suspend() will eventually wraparound, but unlikely
709 void suspend() { (void) android_atomic_inc(&mSuspended); }
710 void restore()
711 {
712 // if restore() is done without suspend(), get back into
713 // range so that the next suspend() will operate correctly
714 if (android_atomic_dec(&mSuspended) <= 0) {
715 android_atomic_release_store(0, &mSuspended);
716 }
717 }
718 bool isSuspended() const
719 { return android_atomic_acquire_load(&mSuspended) > 0; }
720
721 virtual String8 getParameters(const String8& keys);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700722 virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000723 status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Andy Hung010a1a12014-03-13 13:57:33 -0700724 // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency.
725 // Consider also removing and passing an explicit mMainBuffer initialization
726 // parameter to AF::PlaybackThread::Track::Track().
727 int16_t *mixBuffer() const {
728 return reinterpret_cast<int16_t *>(mSinkBuffer); };
Eric Laurent81784c32012-11-19 14:55:58 -0800729
730 virtual void detachAuxEffect_l(int effectId);
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700731 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track>& track,
Eric Laurent81784c32012-11-19 14:55:58 -0800732 int EffectId);
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700733 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track>& track,
Eric Laurent81784c32012-11-19 14:55:58 -0800734 int EffectId);
735
736 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
737 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent4c415062016-06-17 16:14:16 -0700738 virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
Glenn Kastend848eb42016-03-08 13:42:11 -0800739 virtual uint32_t getStrategyForSession_l(audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800740
741
742 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
743 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700744
745 // called with AudioFlinger lock held
Eric Laurent13084622016-05-17 10:51:49 -0700746 bool invalidateTracks_l(audio_stream_type_t streamType);
Haynes Mathew George05317d22016-05-03 16:34:26 -0700747 virtual void invalidateTracks(audio_stream_type_t streamType);
Eric Laurent81784c32012-11-19 14:55:58 -0800748
Glenn Kasten9b58f632013-07-16 11:37:48 -0700749 virtual size_t frameCount() const { return mNormalFrameCount; }
750
Eric Laurent83b88082014-06-20 18:31:16 -0700751 status_t getTimestamp_l(AudioTimestamp& timestamp);
752
753 void addPatchTrack(const sp<PatchTrack>& track);
754 void deletePatchTrack(const sp<PatchTrack>& track);
755
756 virtual void getAudioPortConfig(struct audio_port_config *config);
Eric Laurentaccc1472013-09-20 09:36:34 -0700757
Eric Laurent81784c32012-11-19 14:55:58 -0800758protected:
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800759 // updated by readOutputParameters_l()
Glenn Kasten9b58f632013-07-16 11:37:48 -0700760 size_t mNormalFrameCount; // normal mixer and effects
761
Andy Hung08fb1742015-05-31 23:22:10 -0700762 bool mThreadThrottle; // throttle the thread processing
Andy Hung40eb1a12015-06-18 13:42:02 -0700763 uint32_t mThreadThrottleTimeMs; // throttle time for MIXER threads
764 uint32_t mThreadThrottleEndMs; // notify once per throttling
Andy Hung08fb1742015-05-31 23:22:10 -0700765 uint32_t mHalfBufferMs; // half the buffer size in milliseconds
766
Andy Hung010a1a12014-03-13 13:57:33 -0700767 void* mSinkBuffer; // frame size aligned sink buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800768
Andy Hung98ef9782014-03-04 14:46:50 -0800769 // TODO:
770 // Rearrange the buffer info into a struct/class with
771 // clear, copy, construction, destruction methods.
772 //
773 // mSinkBuffer also has associated with it:
774 //
775 // mSinkBufferSize: Sink Buffer Size
776 // mFormat: Sink Buffer Format
777
Andy Hung69aed5f2014-02-25 17:24:40 -0800778 // Mixer Buffer (mMixerBuffer*)
779 //
780 // In the case of floating point or multichannel data, which is not in the
781 // sink format, it is required to accumulate in a higher precision or greater channel count
782 // buffer before downmixing or data conversion to the sink buffer.
783
784 // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
785 bool mMixerBufferEnabled;
786
787 // Storage, 32 byte aligned (may make this alignment a requirement later).
788 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
789 void* mMixerBuffer;
790
791 // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
792 size_t mMixerBufferSize;
793
794 // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
795 audio_format_t mMixerBufferFormat;
796
797 // An internal flag set to true by MixerThread::prepareTracks_l()
798 // when mMixerBuffer contains valid data after mixing.
799 bool mMixerBufferValid;
800
Andy Hung98ef9782014-03-04 14:46:50 -0800801 // Effects Buffer (mEffectsBuffer*)
802 //
803 // In the case of effects data, which is not in the sink format,
804 // it is required to accumulate in a different buffer before data conversion
805 // to the sink buffer.
806
807 // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
808 bool mEffectBufferEnabled;
809
810 // Storage, 32 byte aligned (may make this alignment a requirement later).
811 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
812 void* mEffectBuffer;
813
814 // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
815 size_t mEffectBufferSize;
816
817 // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
818 audio_format_t mEffectBufferFormat;
819
820 // An internal flag set to true by MixerThread::prepareTracks_l()
821 // when mEffectsBuffer contains valid data after mixing.
822 //
823 // When this is set, all mixer data is routed into the effects buffer
824 // for any processing (including output processing).
825 bool mEffectBufferValid;
826
Eric Laurent81784c32012-11-19 14:55:58 -0800827 // suspend count, > 0 means suspended. While suspended, the thread continues to pull from
828 // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle
829 // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
830 // workaround that restriction.
831 // 'volatile' means accessed via atomic operations and no lock.
832 volatile int32_t mSuspended;
833
Andy Hung818e7a32016-02-16 18:08:07 -0800834 int64_t mBytesWritten;
Andy Hungc54b1ff2016-02-23 14:07:07 -0800835 int64_t mFramesWritten; // not reset on standby
Andy Hung238fa3d2016-07-28 10:53:22 -0700836 int64_t mSuspendedFrames; // not reset on standby
Eric Laurent81784c32012-11-19 14:55:58 -0800837private:
838 // mMasterMute is in both PlaybackThread and in AudioFlinger. When a
839 // PlaybackThread needs to find out if master-muted, it checks it's local
840 // copy rather than the one in AudioFlinger. This optimization saves a lock.
841 bool mMasterMute;
842 void setMasterMute_l(bool muted) { mMasterMute = muted; }
843protected:
Andy Hungdae27702016-10-31 14:01:16 -0700844 ActiveTracks<Track> mActiveTracks;
Eric Laurent81784c32012-11-19 14:55:58 -0800845
846 // Allocate a track name for a given channel mask.
847 // Returns name >= 0 if successful, -1 on failure.
Eric Laurentad7dd962016-09-22 12:38:37 -0700848 virtual int getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format,
849 audio_session_t sessionId, uid_t uid) = 0;
Eric Laurent81784c32012-11-19 14:55:58 -0800850 virtual void deleteTrackName_l(int name) = 0;
851
852 // Time to sleep between cycles when:
853 virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED
854 virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE
855 virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us
856 // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
857 // No sleep in standby mode; waits on a condition
858
859 // Code snippets that are temporarily lifted up out of threadLoop() until the merge
860 void checkSilentMode_l();
861
862 // Non-trivial for DUPLICATING only
863 virtual void saveOutputTracks() { }
864 virtual void clearOutputTracks() { }
865
866 // Cache various calculated values, at threadLoop() entry and after a parameter change
867 virtual void cacheParameters_l();
868
869 virtual uint32_t correctLatency_l(uint32_t latency) const;
870
Eric Laurent1c333e22014-05-20 10:48:17 -0700871 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
872 audio_patch_handle_t *handle);
873 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
874
Phil Burk6fc2a7c2015-04-30 16:08:10 -0700875 bool usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL)
876 && mHwSupportsPause
877 && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
Eric Laurent0f7b5f22014-12-19 10:43:21 -0800878
Eric Laurentad7dd962016-09-22 12:38:37 -0700879 uint32_t trackCountForUid_l(uid_t uid);
880
Eric Laurent81784c32012-11-19 14:55:58 -0800881private:
882
883 friend class AudioFlinger; // for numerous
884
Eric Laurent81784c32012-11-19 14:55:58 -0800885 PlaybackThread& operator = (const PlaybackThread&);
886
887 status_t addTrack_l(const sp<Track>& track);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800888 bool destroyTrack_l(const sp<Track>& track);
Eric Laurent81784c32012-11-19 14:55:58 -0800889 void removeTrack_l(const sp<Track>& track);
890
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800891 void readOutputParameters_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800892
893 virtual void dumpInternals(int fd, const Vector<String16>& args);
894 void dumpTracks(int fd, const Vector<String16>& args);
895
896 SortedVector< sp<Track> > mTracks;
Eric Laurent223fd5c2014-11-11 13:43:36 -0800897 stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurent81784c32012-11-19 14:55:58 -0800898 AudioStreamOut *mOutput;
899
900 float mMasterVolume;
901 nsecs_t mLastWriteTime;
902 int mNumWrites;
903 int mNumDelayedWrites;
904 bool mInWrite;
905
906 // FIXME rename these former local variables of threadLoop to standard "m" names
Eric Laurentad9cb8b2015-05-26 16:38:19 -0700907 nsecs_t mStandbyTimeNs;
Andy Hung25c2dac2014-02-27 14:56:00 -0800908 size_t mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -0800909
910 // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
Eric Laurentad9cb8b2015-05-26 16:38:19 -0700911 uint32_t mActiveSleepTimeUs;
912 uint32_t mIdleSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -0800913
Eric Laurentad9cb8b2015-05-26 16:38:19 -0700914 uint32_t mSleepTimeUs;
Eric Laurent81784c32012-11-19 14:55:58 -0800915
916 // mixer status returned by prepareTracks_l()
917 mixer_state mMixerStatus; // current cycle
918 // previous cycle when in prepareTracks_l()
919 mixer_state mMixerStatusIgnoringFastTracks;
920 // FIXME or a separate ready state per track
921
922 // FIXME move these declarations into the specific sub-class that needs them
923 // MIXER only
924 uint32_t sleepTimeShift;
925
926 // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
Eric Laurentad9cb8b2015-05-26 16:38:19 -0700927 nsecs_t mStandbyDelayNs;
Eric Laurent81784c32012-11-19 14:55:58 -0800928
929 // MIXER only
930 nsecs_t maxPeriod;
931
932 // DUPLICATING only
933 uint32_t writeFrames;
934
Eric Laurentbfb1b832013-01-07 09:53:42 -0800935 size_t mBytesRemaining;
936 size_t mCurrentWriteLength;
937 bool mUseAsyncWrite;
Eric Laurent3b4529e2013-09-05 18:09:19 -0700938 // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
939 // incremented each time a write(), a flush() or a standby() occurs.
940 // Bit 0 is set when a write blocks and indicates a callback is expected.
941 // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
942 // callbacks are ignored.
943 uint32_t mWriteAckSequence;
944 // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
945 // incremented each time a drain is requested or a flush() or standby() occurs.
946 // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
947 // expected.
948 // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
949 // callbacks are ignored.
950 uint32_t mDrainSequence;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800951 sp<AsyncCallbackThread> mCallbackThread;
952
Eric Laurent81784c32012-11-19 14:55:58 -0800953private:
954 // The HAL output sink is treated as non-blocking, but current implementation is blocking
955 sp<NBAIO_Sink> mOutputSink;
956 // If a fast mixer is present, the blocking pipe sink, otherwise clear
957 sp<NBAIO_Sink> mPipeSink;
958 // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
959 sp<NBAIO_Sink> mNormalSink;
Glenn Kasten46909e72013-02-26 09:20:22 -0800960#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -0800961 // For dumpsys
962 sp<NBAIO_Sink> mTeeSink;
963 sp<NBAIO_Source> mTeeSource;
Glenn Kasten46909e72013-02-26 09:20:22 -0800964#endif
Eric Laurent81784c32012-11-19 14:55:58 -0800965 uint32_t mScreenState; // cached copy of gScreenState
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800966 static const size_t kFastMixerLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800967 sp<NBLog::Writer> mFastMixerNBLogWriter;
Andy Hung2148bf02016-11-28 19:01:02 -0800968
969 // Do not call from a sched_fifo thread as it uses a system time call
970 // and obtains a local mutex.
971 class LocalLog {
972 public:
973 void log(const char *fmt, ...) {
974 va_list val;
975 va_start(val, fmt);
976
977 // format to buffer
978 char buffer[512];
979 int length = vsnprintf(buffer, sizeof(buffer), fmt, val);
980 if (length >= (signed)sizeof(buffer)) {
981 length = sizeof(buffer) - 1;
982 }
983
984 // strip out trailing newline
985 while (length > 0 && buffer[length - 1] == '\n') {
986 buffer[--length] = 0;
987 }
988
989 // store in circular array
990 AutoMutex _l(mLock);
991 mLog.emplace_back(
992 std::make_pair(systemTime(SYSTEM_TIME_REALTIME), std::string(buffer)));
993 if (mLog.size() > kLogSize) {
994 mLog.pop_front();
995 }
996
997 va_end(val);
998 }
999
1000 void dump(int fd, const Vector<String16>& args, const char *prefix = "") {
1001 if (!AudioFlinger::dumpTryLock(mLock)) return; // a local lock, shouldn't happen
1002 if (mLog.size() > 0) {
1003 bool dumpAll = false;
1004 for (const auto &arg : args) {
1005 if (arg == String16("--locallog")) {
1006 dumpAll = true;
1007 }
1008 }
1009
1010 dprintf(fd, "Local Log:\n");
1011 auto it = mLog.begin();
Andy Hungf28bcf52016-12-20 12:52:39 -08001012 if (!dumpAll) {
1013 const size_t lines =
1014 (size_t)property_get_int32("audio.locallog.lines", kLogPrint);
1015 if (mLog.size() > lines) {
1016 it += (mLog.size() - lines);
1017 }
Andy Hung2148bf02016-11-28 19:01:02 -08001018 }
1019 for (; it != mLog.end(); ++it) {
1020 const int64_t ns = it->first;
1021 const int ns_per_sec = 1000000000;
1022 const time_t sec = ns / ns_per_sec;
1023 struct tm tm;
1024 localtime_r(&sec, &tm);
1025
1026 dprintf(fd, "%s%02d-%02d %02d:%02d:%02d.%03d %s\n",
1027 prefix,
1028 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
1029 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1030 (int)(ns % ns_per_sec / 1000000),
1031 it->second.c_str());
1032 }
1033 }
1034 mLock.unlock();
1035 }
1036
1037 private:
1038 Mutex mLock;
1039 static const size_t kLogSize = 256; // full history
1040 static const size_t kLogPrint = 32; // default print history
1041 std::deque<std::pair<int64_t, std::string>> mLog;
1042 } mLocalLog;
1043
Eric Laurent81784c32012-11-19 14:55:58 -08001044public:
1045 virtual bool hasFastMixer() const = 0;
Glenn Kasten0f11b512014-01-31 16:18:54 -08001046 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
Eric Laurent81784c32012-11-19 14:55:58 -08001047 { FastTrackUnderruns dummy; return dummy; }
1048
1049protected:
1050 // accessed by both binder threads and within threadLoop(), lock on mutex needed
1051 unsigned mFastTrackAvailMask; // bit i set if fast track [i] is available
Eric Laurentd1f69b02014-12-15 14:33:13 -08001052 bool mHwSupportsPause;
1053 bool mHwPaused;
1054 bool mFlushPending;
Eric Laurent81784c32012-11-19 14:55:58 -08001055};
1056
1057class MixerThread : public PlaybackThread {
1058public:
1059 MixerThread(const sp<AudioFlinger>& audioFlinger,
1060 AudioStreamOut* output,
1061 audio_io_handle_t id,
1062 audio_devices_t device,
Eric Laurent72e3f392015-05-20 14:43:50 -07001063 bool systemReady,
Eric Laurent81784c32012-11-19 14:55:58 -08001064 type_t type = MIXER);
1065 virtual ~MixerThread();
1066
1067 // Thread virtuals
1068
Eric Laurent10351942014-05-08 18:49:52 -07001069 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1070 status_t& status);
Eric Laurent81784c32012-11-19 14:55:58 -08001071 virtual void dumpInternals(int fd, const Vector<String16>& args);
1072
1073protected:
1074 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
Eric Laurentad7dd962016-09-22 12:38:37 -07001075 virtual int getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format,
1076 audio_session_t sessionId, uid_t uid);
Eric Laurent81784c32012-11-19 14:55:58 -08001077 virtual void deleteTrackName_l(int name);
1078 virtual uint32_t idleSleepTimeUs() const;
1079 virtual uint32_t suspendSleepTimeUs() const;
1080 virtual void cacheParameters_l();
1081
Andy Hungdae27702016-10-31 14:01:16 -07001082 virtual void acquireWakeLock_l() {
1083 PlaybackThread::acquireWakeLock_l();
Andy Hung818e7a32016-02-16 18:08:07 -08001084 if (hasFastMixer()) {
1085 mFastMixer->setBoottimeOffset(
1086 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1087 }
1088 }
1089
Eric Laurent81784c32012-11-19 14:55:58 -08001090 // threadLoop snippets
Eric Laurentbfb1b832013-01-07 09:53:42 -08001091 virtual ssize_t threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -08001092 virtual void threadLoop_standby();
1093 virtual void threadLoop_mix();
1094 virtual void threadLoop_sleepTime();
1095 virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
1096 virtual uint32_t correctLatency_l(uint32_t latency) const;
1097
Eric Laurent054d9d32015-04-24 08:48:48 -07001098 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1099 audio_patch_handle_t *handle);
1100 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
1101
Eric Laurent81784c32012-11-19 14:55:58 -08001102 AudioMixer* mAudioMixer; // normal mixer
1103private:
1104 // one-time initialization, no locks required
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001105 sp<FastMixer> mFastMixer; // non-0 if there is also a fast mixer
Eric Laurent81784c32012-11-19 14:55:58 -08001106 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
1107
1108 // contents are not guaranteed to be consistent, no locks required
1109 FastMixerDumpState mFastMixerDumpState;
1110#ifdef STATE_QUEUE_DUMP
1111 StateQueueObserverDump mStateQueueObserverDump;
1112 StateQueueMutatorDump mStateQueueMutatorDump;
1113#endif
1114 AudioWatchdogDump mAudioWatchdogDump;
1115
1116 // accessible only within the threadLoop(), no locks required
1117 // mFastMixer->sq() // for mutating and pushing state
1118 int32_t mFastMixerFutex; // for cold idle
1119
Andy Hung2ddee192015-12-18 17:34:44 -08001120 std::atomic_bool mMasterMono;
Eric Laurent81784c32012-11-19 14:55:58 -08001121public:
Glenn Kasten4d23ca32014-05-13 10:39:51 -07001122 virtual bool hasFastMixer() const { return mFastMixer != 0; }
Eric Laurent81784c32012-11-19 14:55:58 -08001123 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
Glenn Kastendc2c50b2016-04-21 08:13:14 -07001124 ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
Eric Laurent81784c32012-11-19 14:55:58 -08001125 return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
1126 }
Eric Laurent83b88082014-06-20 18:31:16 -07001127
Andy Hung2ddee192015-12-18 17:34:44 -08001128protected:
1129 virtual void setMasterMono_l(bool mono) {
1130 mMasterMono.store(mono);
1131 if (mFastMixer != nullptr) { /* hasFastMixer() */
1132 mFastMixer->setMasterMono(mMasterMono);
1133 }
1134 }
1135 // the FastMixer performs mono blend if it exists.
Glenn Kasten03c48d52016-01-27 17:25:17 -08001136 // Blending with limiter is not idempotent,
1137 // and blending without limiter is idempotent but inefficient to do twice.
Andy Hung2ddee192015-12-18 17:34:44 -08001138 virtual bool requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
Eric Laurent81784c32012-11-19 14:55:58 -08001139};
1140
1141class DirectOutputThread : public PlaybackThread {
1142public:
1143
1144 DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Eric Laurente93cc032016-05-05 10:15:10 -07001145 audio_io_handle_t id, audio_devices_t device, bool systemReady);
Eric Laurent81784c32012-11-19 14:55:58 -08001146 virtual ~DirectOutputThread();
1147
1148 // Thread virtuals
1149
Eric Laurent10351942014-05-08 18:49:52 -07001150 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1151 status_t& status);
Eric Laurente659ef42014-09-29 13:06:46 -07001152 virtual void flushHw_l();
Eric Laurent81784c32012-11-19 14:55:58 -08001153
1154protected:
Eric Laurentad7dd962016-09-22 12:38:37 -07001155 virtual int getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format,
1156 audio_session_t sessionId, uid_t uid);
Eric Laurent81784c32012-11-19 14:55:58 -08001157 virtual void deleteTrackName_l(int name);
1158 virtual uint32_t activeSleepTimeUs() const;
1159 virtual uint32_t idleSleepTimeUs() const;
1160 virtual uint32_t suspendSleepTimeUs() const;
1161 virtual void cacheParameters_l();
1162
1163 // threadLoop snippets
1164 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1165 virtual void threadLoop_mix();
1166 virtual void threadLoop_sleepTime();
Eric Laurentd1f69b02014-12-15 14:33:13 -08001167 virtual void threadLoop_exit();
1168 virtual bool shouldStandby_l();
Eric Laurent81784c32012-11-19 14:55:58 -08001169
Phil Burk43b4dcc2015-06-09 16:53:44 -07001170 virtual void onAddNewTrack_l();
1171
Eric Laurent81784c32012-11-19 14:55:58 -08001172 // volumes last sent to audio HAL with stream->set_volume()
1173 float mLeftVolFloat;
1174 float mRightVolFloat;
1175
Eric Laurentbfb1b832013-01-07 09:53:42 -08001176 DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Eric Laurent72e3f392015-05-20 14:43:50 -07001177 audio_io_handle_t id, uint32_t device, ThreadBase::type_t type,
Eric Laurente93cc032016-05-05 10:15:10 -07001178 bool systemReady);
Eric Laurent5850c4c2016-11-10 13:04:31 -08001179 void processVolume_l(Track *track, bool lastTrack);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001180
Eric Laurent81784c32012-11-19 14:55:58 -08001181 // prepareTracks_l() tells threadLoop_mix() the name of the single active track
1182 sp<Track> mActiveTrack;
Phil Burk43b4dcc2015-06-09 16:53:44 -07001183
1184 wp<Track> mPreviousTrack; // used to detect track switch
1185
Eric Laurent81784c32012-11-19 14:55:58 -08001186public:
1187 virtual bool hasFastMixer() const { return false; }
1188};
1189
Eric Laurentbfb1b832013-01-07 09:53:42 -08001190class OffloadThread : public DirectOutputThread {
1191public:
1192
1193 OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Eric Laurente93cc032016-05-05 10:15:10 -07001194 audio_io_handle_t id, uint32_t device, bool systemReady);
Eric Laurent6a51d7e2013-10-17 18:59:26 -07001195 virtual ~OffloadThread() {};
Eric Laurente659ef42014-09-29 13:06:46 -07001196 virtual void flushHw_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001197
1198protected:
1199 // threadLoop snippets
1200 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1201 virtual void threadLoop_exit();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001202
1203 virtual bool waitingAsyncCallback();
1204 virtual bool waitingAsyncCallback_l();
Haynes Mathew George05317d22016-05-03 16:34:26 -07001205 virtual void invalidateTracks(audio_stream_type_t streamType);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001206
Eric Laurentde0613d2016-07-22 18:19:11 -07001207 virtual bool keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
Eric Laurent64667972016-03-30 18:19:46 -07001208
Eric Laurentbfb1b832013-01-07 09:53:42 -08001209private:
Eric Laurentbfb1b832013-01-07 09:53:42 -08001210 size_t mPausedWriteLength; // length in bytes of write interrupted by pause
1211 size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume
Eric Laurent64667972016-03-30 18:19:46 -07001212 bool mKeepWakeLock; // keep wake lock while waiting for write callback
Andy Hungf8044752016-07-27 14:58:11 -07001213 uint64_t mOffloadUnderrunPosition; // Current frame position for offloaded playback
1214 // used and valid only during underrun. ~0 if
1215 // no underrun has occurred during playback and
1216 // is not reset on standby.
Eric Laurentbfb1b832013-01-07 09:53:42 -08001217};
1218
1219class AsyncCallbackThread : public Thread {
1220public:
1221
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001222 explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001223
1224 virtual ~AsyncCallbackThread();
1225
1226 // Thread virtuals
1227 virtual bool threadLoop();
1228
1229 // RefBase
1230 virtual void onFirstRef();
1231
1232 void exit();
Eric Laurent3b4529e2013-09-05 18:09:19 -07001233 void setWriteBlocked(uint32_t sequence);
1234 void resetWriteBlocked();
1235 void setDraining(uint32_t sequence);
1236 void resetDraining();
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07001237 void setAsyncError();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001238
1239private:
Eric Laurent4de95592013-09-26 15:28:21 -07001240 const wp<PlaybackThread> mPlaybackThread;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001241 // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
1242 // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
1243 // to indicate that the callback has been received via resetWriteBlocked()
Eric Laurent4de95592013-09-26 15:28:21 -07001244 uint32_t mWriteAckSequence;
Eric Laurent3b4529e2013-09-05 18:09:19 -07001245 // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
1246 // setDraining(). The sequence is shifted one bit to the left and the lsb is used
1247 // to indicate that the callback has been received via resetDraining()
Eric Laurent4de95592013-09-26 15:28:21 -07001248 uint32_t mDrainSequence;
1249 Condition mWaitWorkCV;
1250 Mutex mLock;
Haynes Mathew George4527b9e2016-07-07 19:54:17 -07001251 bool mAsyncError;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001252};
1253
Eric Laurent81784c32012-11-19 14:55:58 -08001254class DuplicatingThread : public MixerThread {
1255public:
1256 DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
Eric Laurent72e3f392015-05-20 14:43:50 -07001257 audio_io_handle_t id, bool systemReady);
Eric Laurent81784c32012-11-19 14:55:58 -08001258 virtual ~DuplicatingThread();
1259
1260 // Thread virtuals
1261 void addOutputTrack(MixerThread* thread);
1262 void removeOutputTrack(MixerThread* thread);
1263 uint32_t waitTimeMs() const { return mWaitTimeMs; }
1264protected:
1265 virtual uint32_t activeSleepTimeUs() const;
1266
1267private:
1268 bool outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
1269protected:
1270 // threadLoop snippets
1271 virtual void threadLoop_mix();
1272 virtual void threadLoop_sleepTime();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001273 virtual ssize_t threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -08001274 virtual void threadLoop_standby();
1275 virtual void cacheParameters_l();
1276
1277private:
1278 // called from threadLoop, addOutputTrack, removeOutputTrack
1279 virtual void updateWaitTime_l();
1280protected:
1281 virtual void saveOutputTracks();
1282 virtual void clearOutputTracks();
1283private:
1284
1285 uint32_t mWaitTimeMs;
1286 SortedVector < sp<OutputTrack> > outputTracks;
1287 SortedVector < sp<OutputTrack> > mOutputTracks;
1288public:
1289 virtual bool hasFastMixer() const { return false; }
1290};
1291
Eric Laurent81784c32012-11-19 14:55:58 -08001292// record thread
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001293class RecordThread : public ThreadBase
Eric Laurent81784c32012-11-19 14:55:58 -08001294{
1295public:
1296
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001297 class RecordTrack;
Andy Hung73c02e42015-03-29 01:13:58 -07001298
1299 /* The ResamplerBufferProvider is used to retrieve recorded input data from the
1300 * RecordThread. It maintains local state on the relative position of the read
1301 * position of the RecordTrack compared with the RecordThread.
1302 */
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001303 class ResamplerBufferProvider : public AudioBufferProvider
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001304 {
1305 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001306 explicit ResamplerBufferProvider(RecordTrack* recordTrack) :
Andy Hung73c02e42015-03-29 01:13:58 -07001307 mRecordTrack(recordTrack),
1308 mRsmpInUnrel(0), mRsmpInFront(0) { }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001309 virtual ~ResamplerBufferProvider() { }
Andy Hung73c02e42015-03-29 01:13:58 -07001310
1311 // called to set the ResamplerBufferProvider to head of the RecordThread data buffer,
1312 // skipping any previous data read from the hal.
1313 virtual void reset();
1314
1315 /* Synchronizes RecordTrack position with the RecordThread.
1316 * Calculates available frames and handle overruns if the RecordThread
1317 * has advanced faster than the ResamplerBufferProvider has retrieved data.
1318 * TODO: why not do this for every getNextBuffer?
1319 *
1320 * Parameters
1321 * framesAvailable: pointer to optional output size_t to store record track
1322 * frames available.
1323 * hasOverrun: pointer to optional boolean, returns true if track has overrun.
1324 */
1325
1326 virtual void sync(size_t *framesAvailable = NULL, bool *hasOverrun = NULL);
1327
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001328 // AudioBufferProvider interface
Glenn Kastend79072e2016-01-06 08:41:20 -08001329 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001330 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
1331 private:
1332 RecordTrack * const mRecordTrack;
Andy Hung73c02e42015-03-29 01:13:58 -07001333 size_t mRsmpInUnrel; // unreleased frames remaining from
1334 // most recent getNextBuffer
1335 // for debug only
1336 int32_t mRsmpInFront; // next available frame
1337 // rolling counter that is never cleared
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001338 };
1339
Andy Hung97a893e2015-03-29 01:03:07 -07001340 /* The RecordBufferConverter is used for format, channel, and sample rate
1341 * conversion for a RecordTrack.
1342 *
1343 * TODO: Self contained, so move to a separate file later.
1344 *
1345 * RecordBufferConverter uses the convert() method rather than exposing a
1346 * buffer provider interface; this is to save a memory copy.
1347 */
1348 class RecordBufferConverter
1349 {
1350 public:
1351 RecordBufferConverter(
1352 audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
1353 uint32_t srcSampleRate,
1354 audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
1355 uint32_t dstSampleRate);
1356
1357 ~RecordBufferConverter();
1358
1359 /* Converts input data from an AudioBufferProvider by format, channelMask,
1360 * and sampleRate to a destination buffer.
1361 *
1362 * Parameters
1363 * dst: buffer to place the converted data.
1364 * provider: buffer provider to obtain source data.
1365 * frames: number of frames to convert
1366 *
1367 * Returns the number of frames converted.
1368 */
1369 size_t convert(void *dst, AudioBufferProvider *provider, size_t frames);
1370
1371 // returns NO_ERROR if constructor was successful
1372 status_t initCheck() const {
1373 // mSrcChannelMask set on successful updateParameters
1374 return mSrcChannelMask != AUDIO_CHANNEL_INVALID ? NO_ERROR : NO_INIT;
1375 }
1376
1377 // allows dynamic reconfigure of all parameters
1378 status_t updateParameters(
1379 audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
1380 uint32_t srcSampleRate,
1381 audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
1382 uint32_t dstSampleRate);
1383
1384 // called to reset resampler buffers on record track discontinuity
1385 void reset() {
1386 if (mResampler != NULL) {
1387 mResampler->reset();
1388 }
1389 }
1390
1391 private:
Andy Hungd330ee42015-04-20 13:23:41 -07001392 // format conversion when not using resampler
1393 void convertNoResampler(void *dst, const void *src, size_t frames);
1394
1395 // format conversion when using resampler; modifies src in-place
1396 void convertResampler(void *dst, /*not-a-const*/ void *src, size_t frames);
Andy Hung97a893e2015-03-29 01:03:07 -07001397
1398 // user provided information
1399 audio_channel_mask_t mSrcChannelMask;
1400 audio_format_t mSrcFormat;
1401 uint32_t mSrcSampleRate;
1402 audio_channel_mask_t mDstChannelMask;
1403 audio_format_t mDstFormat;
1404 uint32_t mDstSampleRate;
1405
1406 // derived information
1407 uint32_t mSrcChannelCount;
1408 uint32_t mDstChannelCount;
1409 size_t mDstFrameSize;
1410
1411 // format conversion buffer
1412 void *mBuf;
1413 size_t mBufFrames;
1414 size_t mBufFrameSize;
1415
1416 // resampler info
1417 AudioResampler *mResampler;
Andy Hungd330ee42015-04-20 13:23:41 -07001418
1419 bool mIsLegacyDownmix; // legacy stereo to mono conversion needed
1420 bool mIsLegacyUpmix; // legacy mono to stereo conversion needed
1421 bool mRequiresFloat; // data processing requires float (e.g. resampler)
1422 PassthruBufferProvider *mInputConverterProvider; // converts input to float
1423 int8_t mIdxAry[sizeof(uint32_t) * 8]; // used for channel mask conversion
Andy Hung97a893e2015-03-29 01:03:07 -07001424 };
1425
Eric Laurent81784c32012-11-19 14:55:58 -08001426#include "RecordTracks.h"
1427
1428 RecordThread(const sp<AudioFlinger>& audioFlinger,
1429 AudioStreamIn *input,
Eric Laurent81784c32012-11-19 14:55:58 -08001430 audio_io_handle_t id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001431 audio_devices_t outDevice,
Eric Laurent72e3f392015-05-20 14:43:50 -07001432 audio_devices_t inDevice,
1433 bool systemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08001434#ifdef TEE_SINK
1435 , const sp<NBAIO_Sink>& teeSink
1436#endif
1437 );
Eric Laurent81784c32012-11-19 14:55:58 -08001438 virtual ~RecordThread();
1439
1440 // no addTrack_l ?
1441 void destroyTrack_l(const sp<RecordTrack>& track);
1442 void removeTrack_l(const sp<RecordTrack>& track);
1443
1444 void dumpInternals(int fd, const Vector<String16>& args);
1445 void dumpTracks(int fd, const Vector<String16>& args);
1446
1447 // Thread virtuals
1448 virtual bool threadLoop();
Eric Laurent81784c32012-11-19 14:55:58 -08001449
1450 // RefBase
1451 virtual void onFirstRef();
1452
1453 virtual status_t initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
Glenn Kastene198c362013-08-13 09:13:36 -07001454
Glenn Kastenb880f5e2014-05-07 08:43:45 -07001455 virtual sp<MemoryDealer> readOnlyHeap() const { return mReadOnlyHeap; }
1456
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001457 virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
1458
Eric Laurent81784c32012-11-19 14:55:58 -08001459 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
1460 const sp<AudioFlinger::Client>& client,
1461 uint32_t sampleRate,
1462 audio_format_t format,
1463 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001464 size_t *pFrameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08001465 audio_session_t sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001466 size_t *notificationFrames,
Andy Hung1f12a8a2016-11-07 16:10:30 -08001467 uid_t uid,
Eric Laurent05067782016-06-01 18:27:28 -07001468 audio_input_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -08001469 pid_t tid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001470 status_t *status /*non-NULL*/,
1471 audio_port_handle_t portId);
Eric Laurent81784c32012-11-19 14:55:58 -08001472
1473 status_t start(RecordTrack* recordTrack,
1474 AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08001475 audio_session_t triggerSession);
Eric Laurent81784c32012-11-19 14:55:58 -08001476
1477 // ask the thread to stop the specified track, and
1478 // return true if the caller should then do it's part of the stopping process
Glenn Kastena8356f62013-07-25 14:37:52 -07001479 bool stop(RecordTrack* recordTrack);
Eric Laurent81784c32012-11-19 14:55:58 -08001480
1481 void dump(int fd, const Vector<String16>& args);
1482 AudioStreamIn* clearInput();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001483 virtual sp<StreamHalInterface> stream() const;
Eric Laurent81784c32012-11-19 14:55:58 -08001484
Eric Laurent81784c32012-11-19 14:55:58 -08001485
Eric Laurent10351942014-05-08 18:49:52 -07001486 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1487 status_t& status);
1488 virtual void cacheParameters_l() {}
Eric Laurent81784c32012-11-19 14:55:58 -08001489 virtual String8 getParameters(const String8& keys);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001490 virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07001491 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1492 audio_patch_handle_t *handle);
1493 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
Eric Laurent83b88082014-06-20 18:31:16 -07001494
1495 void addPatchRecord(const sp<PatchRecord>& record);
1496 void deletePatchRecord(const sp<PatchRecord>& record);
1497
Glenn Kastendeca2ae2014-02-07 10:25:56 -08001498 void readInputParameters_l();
Glenn Kasten5f972c02014-01-13 09:59:31 -08001499 virtual uint32_t getInputFramesLost();
Eric Laurent81784c32012-11-19 14:55:58 -08001500
1501 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1502 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent4c415062016-06-17 16:14:16 -07001503 virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
Eric Laurent81784c32012-11-19 14:55:58 -08001504
1505 // Return the set of unique session IDs across all tracks.
1506 // The keys are the session IDs, and the associated values are meaningless.
1507 // FIXME replace by Set [and implement Bag/Multiset for other uses].
Glenn Kastend848eb42016-03-08 13:42:11 -08001508 KeyedVector<audio_session_t, bool> sessionIds() const;
Eric Laurent81784c32012-11-19 14:55:58 -08001509
1510 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
1511 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
1512
1513 static void syncStartEventCallback(const wp<SyncEvent>& event);
Eric Laurent81784c32012-11-19 14:55:58 -08001514
Glenn Kasten9b58f632013-07-16 11:37:48 -07001515 virtual size_t frameCount() const { return mFrameCount; }
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001516 bool hasFastCapture() const { return mFastCapture != 0; }
Eric Laurent83b88082014-06-20 18:31:16 -07001517 virtual void getAudioPortConfig(struct audio_port_config *config);
Glenn Kasten9b58f632013-07-16 11:37:48 -07001518
Eric Laurent4c415062016-06-17 16:14:16 -07001519 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
1520 audio_session_t sessionId);
1521
Andy Hungdae27702016-10-31 14:01:16 -07001522 virtual void acquireWakeLock_l() {
1523 ThreadBase::acquireWakeLock_l();
1524 mActiveTracks.updatePowerState(this, true /* force */);
1525 }
1526
Eric Laurent81784c32012-11-19 14:55:58 -08001527private:
Eric Laurent81784c32012-11-19 14:55:58 -08001528 // Enter standby if not already in standby, and set mStandby flag
Glenn Kasten93e471f2013-08-19 08:40:07 -07001529 void standbyIfNotAlreadyInStandby();
Eric Laurent81784c32012-11-19 14:55:58 -08001530
1531 // Call the HAL standby method unconditionally, and don't change mStandby flag
Glenn Kastene198c362013-08-13 09:13:36 -07001532 void inputStandBy();
Eric Laurent81784c32012-11-19 14:55:58 -08001533
1534 AudioStreamIn *mInput;
1535 SortedVector < sp<RecordTrack> > mTracks;
Glenn Kasten2b806402013-11-20 16:37:38 -08001536 // mActiveTracks has dual roles: it indicates the current active track(s), and
Eric Laurent81784c32012-11-19 14:55:58 -08001537 // is used together with mStartStopCond to indicate start()/stop() progress
Andy Hungdae27702016-10-31 14:01:16 -07001538 ActiveTracks<RecordTrack> mActiveTracks;
1539
Eric Laurent81784c32012-11-19 14:55:58 -08001540 Condition mStartStopCond;
Glenn Kasten9b58f632013-07-16 11:37:48 -07001541
Glenn Kasten85948432013-08-19 12:09:05 -07001542 // resampler converts input at HAL Hz to output at AudioRecord client Hz
Glenn Kasten1b291842016-07-18 14:55:21 -07001543 void *mRsmpInBuffer; // size = mRsmpInFramesOA
Glenn Kasten85948432013-08-19 12:09:05 -07001544 size_t mRsmpInFrames; // size of resampler input in frames
1545 size_t mRsmpInFramesP2;// size rounded up to a power-of-2
Glenn Kasten1b291842016-07-18 14:55:21 -07001546 size_t mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001547
1548 // rolling index that is never cleared
Glenn Kasten85948432013-08-19 12:09:05 -07001549 int32_t mRsmpInRear; // last filled frame + 1
Glenn Kasten85948432013-08-19 12:09:05 -07001550
Eric Laurent81784c32012-11-19 14:55:58 -08001551 // For dumpsys
1552 const sp<NBAIO_Sink> mTeeSink;
Glenn Kastenb880f5e2014-05-07 08:43:45 -07001553
1554 const sp<MemoryDealer> mReadOnlyHeap;
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001555
1556 // one-time initialization, no locks required
Glenn Kastenb187de12014-12-30 08:18:15 -08001557 sp<FastCapture> mFastCapture; // non-0 if there is also
1558 // a fast capture
Eric Laurent72e3f392015-05-20 14:43:50 -07001559
Glenn Kasten6dbb5e32014-05-13 10:38:42 -07001560 // FIXME audio watchdog thread
1561
1562 // contents are not guaranteed to be consistent, no locks required
1563 FastCaptureDumpState mFastCaptureDumpState;
1564#ifdef STATE_QUEUE_DUMP
1565 // FIXME StateQueue observer and mutator dump fields
1566#endif
1567 // FIXME audio watchdog dump
1568
1569 // accessible only within the threadLoop(), no locks required
1570 // mFastCapture->sq() // for mutating and pushing state
1571 int32_t mFastCaptureFutex; // for cold idle
1572
1573 // The HAL input source is treated as non-blocking,
1574 // but current implementation is blocking
1575 sp<NBAIO_Source> mInputSource;
1576 // The source for the normal capture thread to read from: mInputSource or mPipeSource
1577 sp<NBAIO_Source> mNormalSource;
1578 // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
1579 // otherwise clear
1580 sp<NBAIO_Sink> mPipeSink;
1581 // If a fast capture is present, the non-blocking pipe source read by normal thread,
1582 // otherwise clear
1583 sp<NBAIO_Source> mPipeSource;
1584 // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
1585 size_t mPipeFramesP2;
1586 // If a fast capture is present, the Pipe as IMemory, otherwise clear
1587 sp<IMemory> mPipeMemory;
1588
1589 static const size_t kFastCaptureLogSize = 4 * 1024;
1590 sp<NBLog::Writer> mFastCaptureNBLogWriter;
1591
1592 bool mFastTrackAvail; // true if fast track available
Eric Laurent81784c32012-11-19 14:55:58 -08001593};
Eric Laurent6acd1d42017-01-04 14:23:29 -08001594
1595class MmapThread : public ThreadBase
1596{
1597 public:
1598
1599#include "MmapTracks.h"
1600
1601 MmapThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1602 AudioHwDevice *hwDev, sp<StreamHalInterface> stream,
1603 audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
1604 virtual ~MmapThread();
1605
1606 virtual void configure(const audio_attributes_t *attr,
1607 audio_stream_type_t streamType,
1608 audio_session_t sessionId,
1609 const sp<MmapStreamCallback>& callback,
1610 audio_port_handle_t portId);
1611
1612 void disconnect();
1613
1614 // MmapStreamInterface
1615 status_t createMmapBuffer(int32_t minSizeFrames,
1616 struct audio_mmap_buffer_info *info);
1617 status_t getMmapPosition(struct audio_mmap_position *position);
1618 status_t start(const MmapStreamInterface::Client& client, audio_port_handle_t *handle);
1619 status_t stop(audio_port_handle_t handle);
1620
1621 // RefBase
1622 virtual void onFirstRef();
1623
1624 // Thread virtuals
1625 virtual bool threadLoop();
1626
1627 virtual void threadLoop_exit();
1628 virtual void threadLoop_standby();
1629
1630 virtual status_t initCheck() const { return (mHalStream == 0) ? NO_INIT : NO_ERROR; }
1631 virtual size_t frameCount() const { return mFrameCount; }
1632 virtual bool checkForNewParameter_l(const String8& keyValuePair,
1633 status_t& status);
1634 virtual String8 getParameters(const String8& keys);
1635 virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
1636 void readHalParameters_l();
1637 virtual void cacheParameters_l() {}
1638 virtual status_t createAudioPatch_l(const struct audio_patch *patch,
1639 audio_patch_handle_t *handle);
1640 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle);
1641 virtual void getAudioPortConfig(struct audio_port_config *config);
1642
1643 virtual sp<StreamHalInterface> stream() const { return mHalStream; }
1644 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1645 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
1646 virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
1647 audio_session_t sessionId);
1648
1649 virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
1650 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
1651 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
1652
1653 virtual void checkSilentMode_l() {}
1654 virtual void processVolume_l() {}
1655 void checkInvalidTracks_l();
1656
1657 virtual audio_stream_type_t streamType() { return AUDIO_STREAM_DEFAULT; }
1658
1659 virtual void invalidateTracks(audio_stream_type_t streamType __unused) {}
1660
1661 void dump(int fd, const Vector<String16>& args);
1662 virtual void dumpInternals(int fd, const Vector<String16>& args);
1663 void dumpTracks(int fd, const Vector<String16>& args);
1664
1665 virtual bool isOutput() const = 0;
1666
1667 protected:
1668
1669 audio_attributes_t mAttr;
1670 audio_session_t mSessionId;
1671 audio_port_handle_t mPortId;
1672
1673 sp<MmapStreamCallback> mCallback;
1674 sp<StreamHalInterface> mHalStream;
1675 sp<DeviceHalInterface> mHalDevice;
1676 AudioHwDevice* const mAudioHwDev;
1677 ActiveTracks<MmapTrack> mActiveTracks;
1678};
1679
1680class MmapPlaybackThread : public MmapThread, public VolumeInterface
1681{
1682
1683public:
1684 MmapPlaybackThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1685 AudioHwDevice *hwDev, AudioStreamOut *output,
1686 audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
1687 virtual ~MmapPlaybackThread() {}
1688
1689 virtual void configure(const audio_attributes_t *attr,
1690 audio_stream_type_t streamType,
1691 audio_session_t sessionId,
1692 const sp<MmapStreamCallback>& callback,
1693 audio_port_handle_t portId);
1694
1695 AudioStreamOut* clearOutput();
1696
1697 // VolumeInterface
1698 virtual void setMasterVolume(float value);
1699 virtual void setMasterMute(bool muted);
1700 virtual void setStreamVolume(audio_stream_type_t stream, float value);
1701 virtual void setStreamMute(audio_stream_type_t stream, bool muted);
1702 virtual float streamVolume(audio_stream_type_t stream) const;
1703
1704 void setMasterMute_l(bool muted) { mMasterMute = muted; }
1705
1706 virtual void invalidateTracks(audio_stream_type_t streamType);
1707
1708 virtual audio_stream_type_t streamType() { return mStreamType; }
1709 virtual void checkSilentMode_l();
1710 virtual void processVolume_l();
1711
1712 virtual void dumpInternals(int fd, const Vector<String16>& args);
1713
1714 virtual bool isOutput() const { return true; }
1715
1716protected:
1717
1718 audio_stream_type_t mStreamType;
1719 float mMasterVolume;
1720 float mStreamVolume;
1721 bool mMasterMute;
1722 bool mStreamMute;
1723 float mHalVolFloat;
1724 AudioStreamOut* mOutput;
1725};
1726
1727class MmapCaptureThread : public MmapThread
1728{
1729
1730public:
1731 MmapCaptureThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1732 AudioHwDevice *hwDev, AudioStreamIn *input,
1733 audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
1734 virtual ~MmapCaptureThread() {}
1735
1736 AudioStreamIn* clearInput();
1737
1738 virtual bool isOutput() const { return false; }
1739
1740protected:
1741
1742 AudioStreamIn* mInput;
1743};