1 /*
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 
22 class ThreadBase : public Thread {
23 public:
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
31         RECORD,             // Thread class is RecordThread
32         OFFLOAD,            // Thread class is OffloadThread
33         MMAP_PLAYBACK,      // Thread class for MMAP playback stream
34         MMAP_CAPTURE,       // Thread class for MMAP capture stream
35         SPATIALIZER,  //
36         // If you add any values here, also update ThreadBase::threadTypeToString()
37     };
38 
39     static const char *threadTypeToString(type_t type);
40 
41     ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
42                type_t type, bool systemReady, bool isOut);
43     virtual             ~ThreadBase();
44 
45     virtual status_t    readyToRun();
46 
47     void clearPowerManager();
48 
49     // base for record and playback
50     enum {
51         CFG_EVENT_IO,
52         CFG_EVENT_PRIO,
53         CFG_EVENT_SET_PARAMETER,
54         CFG_EVENT_CREATE_AUDIO_PATCH,
55         CFG_EVENT_RELEASE_AUDIO_PATCH,
56         CFG_EVENT_UPDATE_OUT_DEVICE,
57         CFG_EVENT_RESIZE_BUFFER,
58         CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS
59     };
60 
61     class ConfigEventData: public RefBase {
62     public:
~ConfigEventData()63         virtual ~ConfigEventData() {}
64 
65         virtual  void dump(char *buffer, size_t size) = 0;
66     protected:
ConfigEventData()67         ConfigEventData() {}
68     };
69 
70     // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
71     //  1. create SetParameterConfigEvent. This sets mWaitStatus in config event
72     //  2. Lock mLock
73     //  3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
74     //  4. sendConfigEvent_l() reads status from event->mStatus;
75     //  5. sendConfigEvent_l() returns status
76     //  6. Unlock
77     //
78     // Parameter sequence by server: threadLoop calling processConfigEvents_l():
79     // 1. Lock mLock
80     // 2. If there is an entry in mConfigEvents proceed ...
81     // 3. Read first entry in mConfigEvents
82     // 4. Remove first entry from mConfigEvents
83     // 5. Process
84     // 6. Set event->mStatus
85     // 7. event->mCond.signal
86     // 8. Unlock
87 
88     class ConfigEvent: public RefBase {
89     public:
~ConfigEvent()90         virtual ~ConfigEvent() {}
91 
dump(char * buffer,size_t size)92         void dump(char *buffer, size_t size) {
93             snprintf(buffer, size, "Event type: %d\n", mType);
94             if (mData != nullptr) {
95                 snprintf(buffer, size, "Data:\n");
96                 mData->dump(buffer, size);
97             }
98         }
99 
100         const int mType; // event type e.g. CFG_EVENT_IO
101         Mutex mLock;     // mutex associated with mCond
102         Condition mCond; // condition for status return
103         status_t mStatus; // status communicated to sender
104         bool mWaitStatus; // true if sender is waiting for status
105         bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
106         sp<ConfigEventData> mData;     // event specific parameter data
107 
108     protected:
109         explicit ConfigEvent(int type, bool requiresSystemReady = false) :
110             mType(type), mStatus(NO_ERROR), mWaitStatus(false),
111             mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
112     };
113 
114     class IoConfigEventData : public ConfigEventData {
115     public:
IoConfigEventData(audio_io_config_event event,pid_t pid,audio_port_handle_t portId)116         IoConfigEventData(audio_io_config_event event, pid_t pid,
117                           audio_port_handle_t portId) :
118             mEvent(event), mPid(pid), mPortId(portId) {}
119 
dump(char * buffer,size_t size)120         virtual  void dump(char *buffer, size_t size) {
121             snprintf(buffer, size, "- IO event: event %d\n", mEvent);
122         }
123 
124         const audio_io_config_event mEvent;
125         const pid_t                 mPid;
126         const audio_port_handle_t   mPortId;
127     };
128 
129     class IoConfigEvent : public ConfigEvent {
130     public:
IoConfigEvent(audio_io_config_event event,pid_t pid,audio_port_handle_t portId)131         IoConfigEvent(audio_io_config_event event, pid_t pid, audio_port_handle_t portId) :
132             ConfigEvent(CFG_EVENT_IO) {
133             mData = new IoConfigEventData(event, pid, portId);
134         }
~IoConfigEvent()135         virtual ~IoConfigEvent() {}
136     };
137 
138     class PrioConfigEventData : public ConfigEventData {
139     public:
PrioConfigEventData(pid_t pid,pid_t tid,int32_t prio,bool forApp)140         PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
141             mPid(pid), mTid(tid), mPrio(prio), mForApp(forApp) {}
142 
dump(char * buffer,size_t size)143         virtual  void dump(char *buffer, size_t size) {
144             snprintf(buffer, size, "- Prio event: pid %d, tid %d, prio %d, for app? %d\n",
145                     mPid, mTid, mPrio, mForApp);
146         }
147 
148         const pid_t mPid;
149         const pid_t mTid;
150         const int32_t mPrio;
151         const bool mForApp;
152     };
153 
154     class PrioConfigEvent : public ConfigEvent {
155     public:
PrioConfigEvent(pid_t pid,pid_t tid,int32_t prio,bool forApp)156         PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
157             ConfigEvent(CFG_EVENT_PRIO, true) {
158             mData = new PrioConfigEventData(pid, tid, prio, forApp);
159         }
~PrioConfigEvent()160         virtual ~PrioConfigEvent() {}
161     };
162 
163     class SetParameterConfigEventData : public ConfigEventData {
164     public:
SetParameterConfigEventData(String8 keyValuePairs)165         explicit SetParameterConfigEventData(String8 keyValuePairs) :
166             mKeyValuePairs(keyValuePairs) {}
167 
dump(char * buffer,size_t size)168         virtual  void dump(char *buffer, size_t size) {
169             snprintf(buffer, size, "- KeyValue: %s\n", mKeyValuePairs.string());
170         }
171 
172         const String8 mKeyValuePairs;
173     };
174 
175     class SetParameterConfigEvent : public ConfigEvent {
176     public:
SetParameterConfigEvent(String8 keyValuePairs)177         explicit SetParameterConfigEvent(String8 keyValuePairs) :
178             ConfigEvent(CFG_EVENT_SET_PARAMETER) {
179             mData = new SetParameterConfigEventData(keyValuePairs);
180             mWaitStatus = true;
181         }
~SetParameterConfigEvent()182         virtual ~SetParameterConfigEvent() {}
183     };
184 
185     class CreateAudioPatchConfigEventData : public ConfigEventData {
186     public:
CreateAudioPatchConfigEventData(const struct audio_patch patch,audio_patch_handle_t handle)187         CreateAudioPatchConfigEventData(const struct audio_patch patch,
188                                         audio_patch_handle_t handle) :
189             mPatch(patch), mHandle(handle) {}
190 
dump(char * buffer,size_t size)191         virtual  void dump(char *buffer, size_t size) {
192             snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
193         }
194 
195         const struct audio_patch mPatch;
196         audio_patch_handle_t mHandle;
197     };
198 
199     class CreateAudioPatchConfigEvent : public ConfigEvent {
200     public:
CreateAudioPatchConfigEvent(const struct audio_patch patch,audio_patch_handle_t handle)201         CreateAudioPatchConfigEvent(const struct audio_patch patch,
202                                     audio_patch_handle_t handle) :
203             ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
204             mData = new CreateAudioPatchConfigEventData(patch, handle);
205             mWaitStatus = true;
206         }
~CreateAudioPatchConfigEvent()207         virtual ~CreateAudioPatchConfigEvent() {}
208     };
209 
210     class ReleaseAudioPatchConfigEventData : public ConfigEventData {
211     public:
ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle)212         explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
213             mHandle(handle) {}
214 
dump(char * buffer,size_t size)215         virtual  void dump(char *buffer, size_t size) {
216             snprintf(buffer, size, "- Patch handle: %u\n", mHandle);
217         }
218 
219         audio_patch_handle_t mHandle;
220     };
221 
222     class ReleaseAudioPatchConfigEvent : public ConfigEvent {
223     public:
ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle)224         explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
225             ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
226             mData = new ReleaseAudioPatchConfigEventData(handle);
227             mWaitStatus = true;
228         }
~ReleaseAudioPatchConfigEvent()229         virtual ~ReleaseAudioPatchConfigEvent() {}
230     };
231 
232     class UpdateOutDevicesConfigEventData : public ConfigEventData {
233     public:
UpdateOutDevicesConfigEventData(const DeviceDescriptorBaseVector & outDevices)234         explicit UpdateOutDevicesConfigEventData(const DeviceDescriptorBaseVector& outDevices) :
235             mOutDevices(outDevices) {}
236 
dump(char * buffer,size_t size)237         virtual void dump(char *buffer, size_t size) {
238             snprintf(buffer, size, "- Devices: %s", android::toString(mOutDevices).c_str());
239         }
240 
241         DeviceDescriptorBaseVector mOutDevices;
242     };
243 
244     class UpdateOutDevicesConfigEvent : public ConfigEvent {
245     public:
UpdateOutDevicesConfigEvent(const DeviceDescriptorBaseVector & outDevices)246         explicit UpdateOutDevicesConfigEvent(const DeviceDescriptorBaseVector& outDevices) :
247             ConfigEvent(CFG_EVENT_UPDATE_OUT_DEVICE) {
248             mData = new UpdateOutDevicesConfigEventData(outDevices);
249         }
250 
251         virtual ~UpdateOutDevicesConfigEvent();
252     };
253 
254     class ResizeBufferConfigEventData : public ConfigEventData {
255     public:
ResizeBufferConfigEventData(int32_t maxSharedAudioHistoryMs)256         explicit ResizeBufferConfigEventData(int32_t maxSharedAudioHistoryMs) :
257             mMaxSharedAudioHistoryMs(maxSharedAudioHistoryMs) {}
258 
dump(char * buffer,size_t size)259         virtual void dump(char *buffer, size_t size) {
260             snprintf(buffer, size, "- mMaxSharedAudioHistoryMs: %d", mMaxSharedAudioHistoryMs);
261         }
262 
263         int32_t mMaxSharedAudioHistoryMs;
264     };
265 
266     class ResizeBufferConfigEvent : public ConfigEvent {
267     public:
ResizeBufferConfigEvent(int32_t maxSharedAudioHistoryMs)268         explicit ResizeBufferConfigEvent(int32_t maxSharedAudioHistoryMs) :
269             ConfigEvent(CFG_EVENT_RESIZE_BUFFER) {
270             mData = new ResizeBufferConfigEventData(maxSharedAudioHistoryMs);
271         }
272 
~ResizeBufferConfigEvent()273         virtual ~ResizeBufferConfigEvent() {}
274     };
275 
276     class CheckOutputStageEffectsEvent : public ConfigEvent {
277     public:
CheckOutputStageEffectsEvent()278         CheckOutputStageEffectsEvent() :
279             ConfigEvent(CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS) {
280         }
281 
~CheckOutputStageEffectsEvent()282         virtual ~CheckOutputStageEffectsEvent() {}
283     };
284 
285 
286     class PMDeathRecipient : public IBinder::DeathRecipient {
287     public:
PMDeathRecipient(const wp<ThreadBase> & thread)288         explicit    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
~PMDeathRecipient()289         virtual     ~PMDeathRecipient() {}
290 
291         // IBinder::DeathRecipient
292         virtual     void        binderDied(const wp<IBinder>& who);
293 
294     private:
295         DISALLOW_COPY_AND_ASSIGN(PMDeathRecipient);
296 
297         wp<ThreadBase> mThread;
298     };
299 
300     virtual     status_t    initCheck() const = 0;
301 
302                 // static externally-visible
type()303                 type_t      type() const { return mType; }
isDuplicating()304                 bool isDuplicating() const { return (mType == DUPLICATING); }
305 
id()306                 audio_io_handle_t id() const { return mId;}
307 
308                 // dynamic externally-visible
sampleRate()309                 uint32_t    sampleRate() const { return mSampleRate; }
channelMask()310                 audio_channel_mask_t channelMask() const { return mChannelMask; }
mixerChannelMask()311     virtual     audio_channel_mask_t mixerChannelMask() const { return mChannelMask; }
312 
format()313                 audio_format_t format() const { return mHALFormat; }
channelCount()314                 uint32_t channelCount() const { return mChannelCount; }
315 
316                 // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
317                 // and returns the [normal mix] buffer's frame count.
318     virtual     size_t      frameCount() const = 0;
hapticChannelMask()319     virtual     audio_channel_mask_t hapticChannelMask() const { return AUDIO_CHANNEL_NONE; }
latency_l()320     virtual     uint32_t    latency_l() const { return 0; }
setVolumeForOutput_l(float left __unused,float right __unused)321     virtual     void        setVolumeForOutput_l(float left __unused, float right __unused) const {}
322 
323                 // Return's the HAL's frame count i.e. fast mixer buffer size.
frameCountHAL()324                 size_t      frameCountHAL() const { return mFrameCount; }
325 
frameSize()326                 size_t      frameSize() const { return mFrameSize; }
327 
328     // Should be "virtual status_t requestExitAndWait()" and override same
329     // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
330                 void        exit();
331     virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
332                                                     status_t& status) = 0;
333     virtual     status_t    setParameters(const String8& keyValuePairs);
334     virtual     String8     getParameters(const String8& keys) = 0;
335     virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0,
336                                         audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
337                 // sendConfigEvent_l() must be called with ThreadBase::mLock held
338                 // Can temporarily release the lock if waiting for a reply from
339                 // processConfigEvents_l().
340                 status_t    sendConfigEvent_l(sp<ConfigEvent>& event);
341                 void        sendIoConfigEvent(audio_io_config_event event, pid_t pid = 0,
342                                               audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
343                 void        sendIoConfigEvent_l(audio_io_config_event event, pid_t pid = 0,
344                                             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
345                 void        sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp);
346                 void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp);
347                 status_t    sendSetParameterConfigEvent_l(const String8& keyValuePair);
348                 status_t    sendCreateAudioPatchConfigEvent(const struct audio_patch *patch,
349                                                             audio_patch_handle_t *handle);
350                 status_t    sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle);
351                 status_t    sendUpdateOutDeviceConfigEvent(
352                                     const DeviceDescriptorBaseVector& outDevices);
353                 void        sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs);
354                 void        sendCheckOutputStageEffectsEvent();
355                 void        sendCheckOutputStageEffectsEvent_l();
356 
357                 void        processConfigEvents_l();
setCheckOutputStageEffects()358     virtual     void        setCheckOutputStageEffects() {}
359     virtual     void        cacheParameters_l() = 0;
360     virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
361                                                audio_patch_handle_t *handle) = 0;
362     virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
363     virtual     void        updateOutDevices(const DeviceDescriptorBaseVector& outDevices);
364     virtual     void        toAudioPortConfig(struct audio_port_config *config) = 0;
365 
366     virtual     void        resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs);
367 
368 
369 
370                 // see note at declaration of mStandby, mOutDevice and mInDevice
standby()371                 bool        standby() const { return mStandby; }
outDeviceTypes()372                 const DeviceTypeSet outDeviceTypes() const {
373                     return getAudioDeviceTypes(mOutDeviceTypeAddrs);
374                 }
inDeviceType()375                 audio_devices_t inDeviceType() const { return mInDeviceTypeAddr.mType; }
getDeviceTypes()376                 DeviceTypeSet getDeviceTypes() const {
377                     return isOutput() ? outDeviceTypes() : DeviceTypeSet({inDeviceType()});
378                 }
379 
outDeviceTypeAddrs()380                 const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const {
381                     return mOutDeviceTypeAddrs;
382                 }
inDeviceTypeAddr()383                 const AudioDeviceTypeAddr& inDeviceTypeAddr() const {
384                     return mInDeviceTypeAddr;
385                 }
386 
isOutput()387                 bool        isOutput() const { return mIsOut; }
388 
isOffloadOrMmap()389                 bool        isOffloadOrMmap() const {
390                     switch (mType) {
391                     case OFFLOAD:
392                     case MMAP_PLAYBACK:
393                     case MMAP_CAPTURE:
394                         return true;
395                     default:
396                         return false;
397                     }
398                 }
399 
400     virtual     sp<StreamHalInterface> stream() const = 0;
401 
402                 sp<EffectHandle> createEffect_l(
403                                     const sp<AudioFlinger::Client>& client,
404                                     const sp<media::IEffectClient>& effectClient,
405                                     int32_t priority,
406                                     audio_session_t sessionId,
407                                     effect_descriptor_t *desc,
408                                     int *enabled,
409                                     status_t *status /*non-NULL*/,
410                                     bool pinned,
411                                     bool probe,
412                                     bool notifyFramesProcessed);
413 
414                 // return values for hasAudioSession (bit field)
415                 enum effect_state {
416                     EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
417                                             // effect
418                     TRACK_SESSION = 0x2,    // the audio session corresponds to at least one
419                                             // track
420                     FAST_SESSION = 0x4,     // the audio session corresponds to at least one
421                                             // fast track
422                     SPATIALIZED_SESSION = 0x8 // the audio session corresponds to at least one
423                                               // spatialized track
424                 };
425 
426                 // get effect chain corresponding to session Id.
427                 sp<EffectChain> getEffectChain(audio_session_t sessionId);
428                 // same as getEffectChain() but must be called with ThreadBase mutex locked
429                 sp<EffectChain> getEffectChain_l(audio_session_t sessionId) const;
430                 std::vector<int> getEffectIds_l(audio_session_t sessionId);
431                 // add an effect chain to the chain list (mEffectChains)
432     virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
433                 // remove an effect chain from the chain list (mEffectChains)
434     virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
435                 // lock all effect chains Mutexes. Must be called before releasing the
436                 // ThreadBase mutex before processing the mixer and effects. This guarantees the
437                 // integrity of the chains during the process.
438                 // Also sets the parameter 'effectChains' to current value of mEffectChains.
439                 void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
440                 // unlock effect chains after process
441                 void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
442                 // get a copy of mEffectChains vector
getEffectChains_l()443                 Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
444                 // set audio mode to all effect chains
445                 void setMode(audio_mode_t mode);
446                 // get effect module with corresponding ID on specified audio session
447                 sp<AudioFlinger::EffectModule> getEffect(audio_session_t sessionId, int effectId);
448                 sp<AudioFlinger::EffectModule> getEffect_l(audio_session_t sessionId, int effectId);
449                 // add and effect module. Also creates the effect chain is none exists for
450                 // the effects audio session. Only called in a context of moving an effect
451                 // from one thread to another
452                 status_t addEffect_l(const sp< EffectModule>& effect);
453                 // remove and effect module. Also removes the effect chain is this was the last
454                 // effect
455                 void removeEffect_l(const sp< EffectModule>& effect, bool release = false);
456                 // disconnect an effect handle from module and destroy module if last handle
457                 void disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast);
458                 // detach all tracks connected to an auxiliary effect
detachAuxEffect_l(int effectId __unused)459     virtual     void detachAuxEffect_l(int effectId __unused) {}
460                 // returns a combination of:
461                 // - EFFECT_SESSION if effects on this audio session exist in one chain
462                 // - TRACK_SESSION if tracks on this audio session exist
463                 // - FAST_SESSION if fast tracks on this audio session exist
464                 // - SPATIALIZED_SESSION if spatialized tracks on this audio session exist
465     virtual     uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
hasAudioSession(audio_session_t sessionId)466                 uint32_t hasAudioSession(audio_session_t sessionId) const {
467                     Mutex::Autolock _l(mLock);
468                     return hasAudioSession_l(sessionId);
469                 }
470 
471                 template <typename T>
hasAudioSession_l(audio_session_t sessionId,const T & tracks)472                 uint32_t hasAudioSession_l(audio_session_t sessionId, const T& tracks) const {
473                     uint32_t result = 0;
474                     if (getEffectChain_l(sessionId) != 0) {
475                         result = EFFECT_SESSION;
476                     }
477                     for (size_t i = 0; i < tracks.size(); ++i) {
478                         const sp<TrackBase>& track = tracks[i];
479                         if (sessionId == track->sessionId()
480                                 && !track->isInvalid()       // not yet removed from tracks.
481                                 && !track->isTerminated()) {
482                             result |= TRACK_SESSION;
483                             if (track->isFastTrack()) {
484                                 result |= FAST_SESSION;  // caution, only represents first track.
485                             }
486                             if (track->canBeSpatialized()) {
487                                 result |= SPATIALIZED_SESSION;  // caution, only first track.
488                             }
489                             break;
490                         }
491                     }
492                     return result;
493                 }
494 
495                 // the value returned by default implementation is not important as the
496                 // strategy is only meaningful for PlaybackThread which implements this method
getStrategyForSession_l(audio_session_t sessionId __unused)497                 virtual product_strategy_t getStrategyForSession_l(
498                         audio_session_t sessionId __unused) {
499                     return static_cast<product_strategy_t>(0);
500                 }
501 
502                 // check if some effects must be suspended/restored when an effect is enabled
503                 // or disabled
504                 void checkSuspendOnEffectEnabled(bool enabled,
505                                                  audio_session_t sessionId,
506                                                  bool threadLocked);
507 
508                 virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
509                 virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
510 
511                 // Return a reference to a per-thread heap which can be used to allocate IMemory
512                 // objects that will be read-only to client processes, read/write to mediaserver,
513                 // and shared by all client processes of the thread.
514                 // The heap is per-thread rather than common across all threads, because
515                 // clients can't be trusted not to modify the offset of the IMemory they receive.
516                 // If a thread does not have such a heap, this method returns 0.
readOnlyHeap()517                 virtual sp<MemoryDealer>    readOnlyHeap() const { return 0; }
518 
pipeMemory()519                 virtual sp<IMemory> pipeMemory() const { return 0; }
520 
521                         void systemReady();
522 
523                 // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
524                 virtual status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
525                                                                audio_session_t sessionId) = 0;
526 
527                         void        broadcast_l();
528 
isTimestampCorrectionEnabled()529                 virtual bool        isTimestampCorrectionEnabled() const { return false; }
530 
isMsdDevice()531                 bool                isMsdDevice() const { return mIsMsdDevice; }
532 
533                 void                dump(int fd, const Vector<String16>& args);
534 
535                 // deliver stats to mediametrics.
536                 void                sendStatistics(bool force);
537 
538     mutable     Mutex                   mLock;
539 
540                 void onEffectEnable(const sp<EffectModule>& effect);
541                 void onEffectDisable();
542 
543                 // invalidateTracksForAudioSession_l must be called with holding mLock.
invalidateTracksForAudioSession_l(audio_session_t sessionId __unused)544     virtual     void invalidateTracksForAudioSession_l(audio_session_t sessionId __unused) const { }
545                 // Invalidate all the tracks with the given audio session.
invalidateTracksForAudioSession(audio_session_t sessionId)546                 void invalidateTracksForAudioSession(audio_session_t sessionId) const {
547                     Mutex::Autolock _l(mLock);
548                     invalidateTracksForAudioSession_l(sessionId);
549                 }
550 
551                 template <typename T>
invalidateTracksForAudioSession_l(audio_session_t sessionId,const T & tracks)552                 void invalidateTracksForAudioSession_l(audio_session_t sessionId,
553                                                        const T& tracks) const {
554                     for (size_t i = 0; i < tracks.size(); ++i) {
555                         const sp<TrackBase>& track = tracks[i];
556                         if (sessionId == track->sessionId()) {
557                             track->invalidate();
558                         }
559                     }
560                 }
561 
562     virtual     bool isStreamInitialized() = 0;
563 
564 protected:
565 
566                 // entry describing an effect being suspended in mSuspendedSessions keyed vector
567                 class SuspendedSessionDesc : public RefBase {
568                 public:
SuspendedSessionDesc()569                     SuspendedSessionDesc() : mRefCount(0) {}
570 
571                     int mRefCount;          // number of active suspend requests
572                     effect_uuid_t mType;    // effect type UUID
573                 };
574 
575                 void        acquireWakeLock();
576                 virtual void acquireWakeLock_l();
577                 void        releaseWakeLock();
578                 void        releaseWakeLock_l();
579                 void        updateWakeLockUids_l(const SortedVector<uid_t> &uids);
580                 void        getPowerManager_l();
581                 // suspend or restore effects of the specified type (or all if type is NULL)
582                 // on a given session. The number of suspend requests is counted and restore
583                 // occurs when all suspend requests are cancelled.
584                 void setEffectSuspended_l(const effect_uuid_t *type,
585                                           bool suspend,
586                                           audio_session_t sessionId);
587                 // updated mSuspendedSessions when an effect is suspended or restored
588                 void        updateSuspendedSessions_l(const effect_uuid_t *type,
589                                                       bool suspend,
590                                                       audio_session_t sessionId);
591                 // check if some effects must be suspended when an effect chain is added
592                 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
593 
594                 // sends the metadata of the active tracks to the HAL
595     virtual     void        updateMetadata_l() = 0;
596 
597                 String16 getWakeLockTag();
598 
preExit()599     virtual     void        preExit() { }
setMasterMono_l(bool mono __unused)600     virtual     void        setMasterMono_l(bool mono __unused) { }
requireMonoBlend()601     virtual     bool        requireMonoBlend() { return false; }
602 
603                             // called within the threadLoop to obtain timestamp from the HAL.
threadloop_getHalTimestamp_l(ExtendedTimestamp * timestamp __unused)604     virtual     status_t    threadloop_getHalTimestamp_l(
605                                     ExtendedTimestamp *timestamp __unused) const {
606                                 return INVALID_OPERATION;
607                             }
608 
609                 product_strategy_t getStrategyForStream(audio_stream_type_t stream) const;
610 
dumpInternals_l(int fd __unused,const Vector<String16> & args __unused)611     virtual     void        dumpInternals_l(int fd __unused, const Vector<String16>& args __unused)
612                             { }
dumpTracks_l(int fd __unused,const Vector<String16> & args __unused)613     virtual     void        dumpTracks_l(int fd __unused, const Vector<String16>& args __unused) { }
614 
615 
616     friend class AudioFlinger;      // for mEffectChains
617 
618                 const type_t            mType;
619 
620                 // Used by parameters, config events, addTrack_l, exit
621                 Condition               mWaitWorkCV;
622 
623                 const sp<AudioFlinger>  mAudioFlinger;
624                 ThreadMetrics           mThreadMetrics;
625                 const bool              mIsOut;
626 
627                 // updated by PlaybackThread::readOutputParameters_l() or
628                 // RecordThread::readInputParameters_l()
629                 uint32_t                mSampleRate;
630                 size_t                  mFrameCount;       // output HAL, direct output, record
631                 audio_channel_mask_t    mChannelMask;
632                 uint32_t                mChannelCount;
633                 size_t                  mFrameSize;
634                 // not HAL frame size, this is for output sink (to pipe to fast mixer)
635                 audio_format_t          mFormat;           // Source format for Recording and
636                                                            // Sink format for Playback.
637                                                            // Sink format may be different than
638                                                            // HAL format if Fastmixer is used.
639                 audio_format_t          mHALFormat;
640                 size_t                  mBufferSize;       // HAL buffer size for read() or write()
641                 AudioDeviceTypeAddrVector mOutDeviceTypeAddrs; // output device types and addresses
642                 AudioDeviceTypeAddr       mInDeviceTypeAddr;   // input device type and address
643                 Vector< sp<ConfigEvent> >     mConfigEvents;
644                 Vector< sp<ConfigEvent> >     mPendingConfigEvents; // events awaiting system ready
645 
646                 // These fields are written and read by thread itself without lock or barrier,
647                 // and read by other threads without lock or barrier via standby(), outDeviceTypes()
648                 // and inDeviceType().
649                 // Because of the absence of a lock or barrier, any other thread that reads
650                 // these fields must use the information in isolation, or be prepared to deal
651                 // with possibility that it might be inconsistent with other information.
652                 bool                    mStandby;     // Whether thread is currently in standby.
653 
654                 struct audio_patch      mPatch;
655 
656                 audio_source_t          mAudioSource;
657 
658                 const audio_io_handle_t mId;
659                 Vector< sp<EffectChain> > mEffectChains;
660 
661                 static const int        kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
662                 char                    mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
663                 sp<os::IPowerManager>   mPowerManager;
664                 sp<IBinder>             mWakeLockToken;
665                 const sp<PMDeathRecipient> mDeathRecipient;
666                 // list of suspended effects per session and per type. The first (outer) vector is
667                 // keyed by session ID, the second (inner) by type UUID timeLow field
668                 // Updated by updateSuspendedSessions_l() only.
669                 KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
670                                         mSuspendedSessions;
671                 // TODO: add comment and adjust size as needed
672                 static const size_t     kLogSize = 4 * 1024;
673                 sp<NBLog::Writer>       mNBLogWriter;
674                 bool                    mSystemReady;
675                 ExtendedTimestamp       mTimestamp;
676                 TimestampVerifier< // For timestamp statistics.
677                         int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier;
678                 // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
679                 // TODO: add confirmation checks:
680                 // 1) DIRECT threads and linear PCM format really resets to 0?
681                 // 2) Is frame count really valid if not linear pcm?
682                 // 3) Are all 64 bits of position returned, not just lowest 32 bits?
683                 // Timestamp corrected device should be a single device.
684                 audio_devices_t         mTimestampCorrectedDevice = AUDIO_DEVICE_NONE;
685 
686                 // ThreadLoop statistics per iteration.
687                 int64_t                 mLastIoBeginNs = -1;
688                 int64_t                 mLastIoEndNs = -1;
689 
690                 // This should be read under ThreadBase lock (if not on the threadLoop thread).
691                 audio_utils::Statistics<double> mIoJitterMs{0.995 /* alpha */};
692                 audio_utils::Statistics<double> mProcessTimeMs{0.995 /* alpha */};
693                 audio_utils::Statistics<double> mLatencyMs{0.995 /* alpha */};
694 
695                 // Save the last count when we delivered statistics to mediametrics.
696                 int64_t                 mLastRecordedTimestampVerifierN = 0;
697                 int64_t                 mLastRecordedTimeNs = 0;  // BOOTTIME to include suspend.
698 
699                 bool                    mIsMsdDevice = false;
700                 // A condition that must be evaluated by the thread loop has changed and
701                 // we must not wait for async write callback in the thread loop before evaluating it
702                 bool                    mSignalPending;
703 
704 #ifdef TEE_SINK
705                 NBAIO_Tee               mTee;
706 #endif
707                 // ActiveTracks is a sorted vector of track type T representing the
708                 // active tracks of threadLoop() to be considered by the locked prepare portion.
709                 // ActiveTracks should be accessed with the ThreadBase lock held.
710                 //
711                 // During processing and I/O, the threadLoop does not hold the lock;
712                 // hence it does not directly use ActiveTracks.  Care should be taken
713                 // to hold local strong references or defer removal of tracks
714                 // if the threadLoop may still be accessing those tracks due to mix, etc.
715                 //
716                 // This class updates power information appropriately.
717                 //
718 
719                 template <typename T>
720                 class ActiveTracks {
721                 public:
722                     explicit ActiveTracks(SimpleLog *localLog = nullptr)
723                         : mActiveTracksGeneration(0)
724                         , mLastActiveTracksGeneration(0)
725                         , mLocalLog(localLog)
726                     { }
727 
~ActiveTracks()728                     ~ActiveTracks() {
729                         ALOGW_IF(!mActiveTracks.isEmpty(),
730                                 "ActiveTracks should be empty in destructor");
731                     }
732                     // returns the last track added (even though it may have been
733                     // subsequently removed from ActiveTracks).
734                     //
735                     // Used for DirectOutputThread to ensure a flush is called when transitioning
736                     // to a new track (even though it may be on the same session).
737                     // Used for OffloadThread to ensure that volume and mixer state is
738                     // taken from the latest track added.
739                     //
740                     // The latest track is saved with a weak pointer to prevent keeping an
741                     // otherwise useless track alive. Thus the function will return nullptr
742                     // if the latest track has subsequently been removed and destroyed.
getLatest()743                     sp<T> getLatest() {
744                         return mLatestActiveTrack.promote();
745                     }
746 
747                     // SortedVector methods
748                     ssize_t         add(const sp<T> &track);
749                     ssize_t         remove(const sp<T> &track);
size()750                     size_t          size() const {
751                         return mActiveTracks.size();
752                     }
isEmpty()753                     bool            isEmpty() const {
754                         return mActiveTracks.isEmpty();
755                     }
indexOf(const sp<T> & item)756                     ssize_t         indexOf(const sp<T>& item) {
757                         return mActiveTracks.indexOf(item);
758                     }
759                     sp<T>           operator[](size_t index) const {
760                         return mActiveTracks[index];
761                     }
begin()762                     typename SortedVector<sp<T>>::iterator begin() {
763                         return mActiveTracks.begin();
764                     }
end()765                     typename SortedVector<sp<T>>::iterator end() {
766                         return mActiveTracks.end();
767                     }
768 
769                     // Due to Binder recursion optimization, clear() and updatePowerState()
770                     // cannot be called from a Binder thread because they may call back into
771                     // the original calling process (system server) for BatteryNotifier
772                     // (which requires a Java environment that may not be present).
773                     // Hence, call clear() and updatePowerState() only from the
774                     // ThreadBase thread.
775                     void            clear();
776                     // periodically called in the threadLoop() to update power state uids.
777                     void            updatePowerState(sp<ThreadBase> thread, bool force = false);
778 
779                     /** @return true if one or move active tracks was added or removed since the
780                      *          last time this function was called or the vector was created.
781                      *          true if volume of one of active tracks was changed.
782                      */
783                     bool            readAndClearHasChanged();
784 
785                 private:
786                     void            logTrack(const char *funcName, const sp<T> &track) const;
787 
getWakeLockUids()788                     SortedVector<uid_t> getWakeLockUids() {
789                         SortedVector<uid_t> wakeLockUids;
790                         for (const sp<T> &track : mActiveTracks) {
791                             wakeLockUids.add(track->uid());
792                         }
793                         return wakeLockUids; // moved by underlying SharedBuffer
794                     }
795 
796                     std::map<uid_t, std::pair<ssize_t /* previous */, ssize_t /* current */>>
797                                         mBatteryCounter;
798                     SortedVector<sp<T>> mActiveTracks;
799                     int                 mActiveTracksGeneration;
800                     int                 mLastActiveTracksGeneration;
801                     wp<T>               mLatestActiveTrack; // latest track added to ActiveTracks
802                     SimpleLog * const   mLocalLog;
803                     // If the vector has changed since last call to readAndClearHasChanged
804                     bool                mHasChanged = false;
805                 };
806 
807                 SimpleLog mLocalLog;
808 
809 private:
810                 void dumpBase_l(int fd, const Vector<String16>& args);
811                 void dumpEffectChains_l(int fd, const Vector<String16>& args);
812 };
813 
814 class VolumeInterface {
815  public:
816 
~VolumeInterface()817     virtual ~VolumeInterface() {}
818 
819     virtual void        setMasterVolume(float value) = 0;
820     virtual void        setMasterMute(bool muted) = 0;
821     virtual void        setStreamVolume(audio_stream_type_t stream, float value) = 0;
822     virtual void        setStreamMute(audio_stream_type_t stream, bool muted) = 0;
823     virtual float       streamVolume(audio_stream_type_t stream) const = 0;
824 
825 };
826 
827 // --- PlaybackThread ---
828 class PlaybackThread : public ThreadBase, public StreamOutHalInterfaceCallback,
829                        public VolumeInterface, public StreamOutHalInterfaceEventCallback {
830 public:
831 
832 #include "PlaybackTracks.h"
833 
834     enum mixer_state {
835         MIXER_IDLE,             // no active tracks
836         MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
837         MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
838         MIXER_DRAIN_TRACK,      // drain currently playing track
839         MIXER_DRAIN_ALL,        // fully drain the hardware
840         // standby mode does not have an enum value
841         // suspend by audio policy manager is orthogonal to mixer state
842     };
843 
844     // retry count before removing active track in case of underrun on offloaded thread:
845     // we need to make sure that AudioTrack client has enough time to send large buffers
846     //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
847     // handled for offloaded tracks
848     static const int8_t kMaxTrackRetriesOffload = 20;
849     static const int8_t kMaxTrackStartupRetriesOffload = 100;
850     static const int8_t kMaxTrackStopRetriesOffload = 2;
851     static constexpr uint32_t kMaxTracksPerUid = 40;
852     static constexpr size_t kMaxTracks = 256;
853 
854     // Maximum delay (in nanoseconds) for upcoming buffers in suspend mode, otherwise
855     // if delay is greater, the estimated time for timeLoopNextNs is reset.
856     // This allows for catch-up to be done for small delays, while resetting the estimate
857     // for initial conditions or large delays.
858     static const nsecs_t kMaxNextBufferDelayNs = 100000000;
859 
860     PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
861                    audio_io_handle_t id, type_t type, bool systemReady,
862                    audio_config_base_t *mixerConfig = nullptr);
863     virtual             ~PlaybackThread();
864 
865     // Thread virtuals
866     virtual     bool        threadLoop();
867 
868     // RefBase
869     virtual     void        onFirstRef();
870 
871     virtual     status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
872                                                        audio_session_t sessionId);
873 
874 protected:
875     // Code snippets that were lifted up out of threadLoop()
876     virtual     void        threadLoop_mix() = 0;
877     virtual     void        threadLoop_sleepTime() = 0;
878     virtual     ssize_t     threadLoop_write();
879     virtual     void        threadLoop_drain();
880     virtual     void        threadLoop_standby();
881     virtual     void        threadLoop_exit();
882     virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
883 
884                 // prepareTracks_l reads and writes mActiveTracks, and returns
885                 // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
886                 // is responsible for clearing or destroying this Vector later on, when it
887                 // is safe to do so. That will drop the final ref count and destroy the tracks.
888     virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
889                 void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
890                 status_t    handleVoipVolume_l(float *volume);
891 
892     // StreamOutHalInterfaceCallback implementation
893     virtual     void        onWriteReady();
894     virtual     void        onDrainReady();
895     virtual     void        onError();
896 
897                 void        resetWriteBlocked(uint32_t sequence);
898                 void        resetDraining(uint32_t sequence);
899 
900     virtual     bool        waitingAsyncCallback();
901     virtual     bool        waitingAsyncCallback_l();
902     virtual     bool        shouldStandby_l();
903     virtual     void        onAddNewTrack_l();
904                 void        onAsyncError(); // error reported by AsyncCallbackThread
905 
906     // StreamHalInterfaceCodecFormatCallback implementation
907                 void        onCodecFormatChanged(
908                                 const std::basic_string<uint8_t>& metadataBs) override;
909 
910     // ThreadBase virtuals
911     virtual     void        preExit();
912 
keepWakeLock()913     virtual     bool        keepWakeLock() const { return true; }
acquireWakeLock_l()914     virtual     void        acquireWakeLock_l() {
915                                 ThreadBase::acquireWakeLock_l();
916                                 mActiveTracks.updatePowerState(this, true /* force */);
917                             }
918 
checkOutputStageEffects()919     virtual     void        checkOutputStageEffects() {}
920 
921                 void        dumpInternals_l(int fd, const Vector<String16>& args) override;
922                 void        dumpTracks_l(int fd, const Vector<String16>& args) override;
923 
924 public:
925 
initCheck()926     virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
927 
928                 // return estimated latency in milliseconds, as reported by HAL
929                 uint32_t    latency() const;
930                 // same, but lock must already be held
931                 uint32_t    latency_l() const override;
932 
933                 // VolumeInterface
934     virtual     void        setMasterVolume(float value);
935     virtual     void        setMasterBalance(float balance);
936     virtual     void        setMasterMute(bool muted);
937     virtual     void        setStreamVolume(audio_stream_type_t stream, float value);
938     virtual     void        setStreamMute(audio_stream_type_t stream, bool muted);
939     virtual     float       streamVolume(audio_stream_type_t stream) const;
940 
941                 void        setVolumeForOutput_l(float left, float right) const override;
942 
943                 sp<Track>   createTrack_l(
944                                 const sp<AudioFlinger::Client>& client,
945                                 audio_stream_type_t streamType,
946                                 const audio_attributes_t& attr,
947                                 uint32_t *sampleRate,
948                                 audio_format_t format,
949                                 audio_channel_mask_t channelMask,
950                                 size_t *pFrameCount,
951                                 size_t *pNotificationFrameCount,
952                                 uint32_t notificationsPerBuffer,
953                                 float speed,
954                                 const sp<IMemory>& sharedBuffer,
955                                 audio_session_t sessionId,
956                                 audio_output_flags_t *flags,
957                                 pid_t creatorPid,
958                                 const AttributionSourceState& attributionSource,
959                                 pid_t tid,
960                                 status_t *status /*non-NULL*/,
961                                 audio_port_handle_t portId,
962                                 const sp<media::IAudioTrackCallback>& callback);
963 
964                 AudioStreamOut* getOutput() const;
965                 AudioStreamOut* clearOutput();
966                 virtual sp<StreamHalInterface> stream() const;
967 
968                 // a very large number of suspend() will eventually wraparound, but unlikely
suspend()969                 void        suspend() { (void) android_atomic_inc(&mSuspended); }
restore()970                 void        restore()
971                                 {
972                                     // if restore() is done without suspend(), get back into
973                                     // range so that the next suspend() will operate correctly
974                                     if (android_atomic_dec(&mSuspended) <= 0) {
975                                         android_atomic_release_store(0, &mSuspended);
976                                     }
977                                 }
isSuspended()978                 bool        isSuspended() const
979                                 { return android_atomic_acquire_load(&mSuspended) > 0; }
980 
981     virtual     String8     getParameters(const String8& keys);
982     virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0,
983                                             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
984                 status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
985                 // Consider also removing and passing an explicit mMainBuffer initialization
986                 // parameter to AF::PlaybackThread::Track::Track().
sinkBuffer()987                 effect_buffer_t *sinkBuffer() const {
988                     return reinterpret_cast<effect_buffer_t *>(mSinkBuffer); };
989 
990     virtual     void detachAuxEffect_l(int effectId);
991                 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track>& track,
992                         int EffectId);
993                 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track>& track,
994                         int EffectId);
995 
996                 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
997                 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
hasAudioSession_l(audio_session_t sessionId)998                         uint32_t hasAudioSession_l(audio_session_t sessionId) const override {
999                             return ThreadBase::hasAudioSession_l(sessionId, mTracks);
1000                         }
1001                 virtual product_strategy_t getStrategyForSession_l(audio_session_t sessionId);
1002 
1003 
1004                 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
1005                 virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
1006 
1007                 // called with AudioFlinger lock held
1008                         bool     invalidateTracks_l(audio_stream_type_t streamType);
1009                 virtual void     invalidateTracks(audio_stream_type_t streamType);
1010 
frameCount()1011     virtual     size_t      frameCount() const { return mNormalFrameCount; }
1012 
mixerChannelMask()1013                 audio_channel_mask_t mixerChannelMask() const override {
1014                     return mMixerChannelMask;
1015                 }
1016 
1017                 status_t    getTimestamp_l(AudioTimestamp& timestamp);
1018 
1019                 void        addPatchTrack(const sp<PatchTrack>& track);
1020                 void        deletePatchTrack(const sp<PatchTrack>& track);
1021 
1022     virtual     void        toAudioPortConfig(struct audio_port_config *config);
1023 
1024                 // Return the asynchronous signal wait time.
computeWaitTimeNs_l()1025     virtual     int64_t     computeWaitTimeNs_l() const { return INT64_MAX; }
1026                 // returns true if the track is allowed to be added to the thread.
isTrackAllowed_l(audio_channel_mask_t channelMask __unused,audio_format_t format __unused,audio_session_t sessionId __unused,uid_t uid)1027     virtual     bool        isTrackAllowed_l(
1028                                     audio_channel_mask_t channelMask __unused,
1029                                     audio_format_t format __unused,
1030                                     audio_session_t sessionId __unused,
1031                                     uid_t uid) const {
1032                                 return trackCountForUid_l(uid) < PlaybackThread::kMaxTracksPerUid
1033                                        && mTracks.size() < PlaybackThread::kMaxTracks;
1034                             }
1035 
isTimestampCorrectionEnabled()1036                 bool        isTimestampCorrectionEnabled() const override {
1037                                 return audio_is_output_devices(mTimestampCorrectedDevice)
1038                                         && outDeviceTypes().count(mTimestampCorrectedDevice) != 0;
1039                             }
1040 
isStreamInitialized()1041     virtual     bool        isStreamInitialized() {
1042                                 return !(mOutput == nullptr || mOutput->stream == nullptr);
1043                             }
1044 
hapticChannelMask()1045                 audio_channel_mask_t hapticChannelMask() const override {
1046                                          return mHapticChannelMask;
1047                                      }
supportsHapticPlayback()1048                 bool supportsHapticPlayback() const {
1049                     return (mHapticChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE;
1050                 }
1051 
setDownStreamPatch(const struct audio_patch * patch)1052                 void setDownStreamPatch(const struct audio_patch *patch) {
1053                     Mutex::Autolock _l(mLock);
1054                     mDownStreamPatch = *patch;
1055                 }
1056 
1057                 PlaybackThread::Track* getTrackById_l(audio_port_handle_t trackId);
1058 
hasMixer()1059                 bool hasMixer() const {
1060                     return mType == MIXER || mType == DUPLICATING || mType == SPATIALIZER;
1061                 }
1062 protected:
1063     // updated by readOutputParameters_l()
1064     size_t                          mNormalFrameCount;  // normal mixer and effects
1065 
1066     bool                            mThreadThrottle;     // throttle the thread processing
1067     uint32_t                        mThreadThrottleTimeMs; // throttle time for MIXER threads
1068     uint32_t                        mThreadThrottleEndMs;  // notify once per throttling
1069     uint32_t                        mHalfBufferMs;       // half the buffer size in milliseconds
1070 
1071     void*                           mSinkBuffer;         // frame size aligned sink buffer
1072 
1073     // TODO:
1074     // Rearrange the buffer info into a struct/class with
1075     // clear, copy, construction, destruction methods.
1076     //
1077     // mSinkBuffer also has associated with it:
1078     //
1079     // mSinkBufferSize: Sink Buffer Size
1080     // mFormat: Sink Buffer Format
1081 
1082     // Mixer Buffer (mMixerBuffer*)
1083     //
1084     // In the case of floating point or multichannel data, which is not in the
1085     // sink format, it is required to accumulate in a higher precision or greater channel count
1086     // buffer before downmixing or data conversion to the sink buffer.
1087 
1088     // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
1089     bool                            mMixerBufferEnabled;
1090 
1091     // Storage, 32 byte aligned (may make this alignment a requirement later).
1092     // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
1093     void*                           mMixerBuffer;
1094 
1095     // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
1096     size_t                          mMixerBufferSize;
1097 
1098     // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
1099     audio_format_t                  mMixerBufferFormat;
1100 
1101     // An internal flag set to true by MixerThread::prepareTracks_l()
1102     // when mMixerBuffer contains valid data after mixing.
1103     bool                            mMixerBufferValid;
1104 
1105     // Effects Buffer (mEffectsBuffer*)
1106     //
1107     // In the case of effects data, which is not in the sink format,
1108     // it is required to accumulate in a different buffer before data conversion
1109     // to the sink buffer.
1110 
1111     // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
1112     bool                            mEffectBufferEnabled;
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*                           mEffectBuffer;
1117 
1118     // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
1119     size_t                          mEffectBufferSize;
1120 
1121     // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
1122     audio_format_t                  mEffectBufferFormat;
1123 
1124     // An internal flag set to true by MixerThread::prepareTracks_l()
1125     // when mEffectsBuffer contains valid data after mixing.
1126     //
1127     // When this is set, all mixer data is routed into the effects buffer
1128     // for any processing (including output processing).
1129     bool                            mEffectBufferValid;
1130 
1131     // Frame size aligned buffer used as input and output to all post processing effects
1132     // except the Spatializer in a SPATIALIZER thread. Non spatialized tracks are mixed into
1133     // this buffer so that post processing effects can be applied.
1134     void*                           mPostSpatializerBuffer = nullptr;
1135 
1136     // Size of mPostSpatializerBuffer in bytes
1137     size_t                          mPostSpatializerBufferSize;
1138 
1139 
1140     // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
1141     // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
1142     // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
1143     // workaround that restriction.
1144     // 'volatile' means accessed via atomic operations and no lock.
1145     volatile int32_t                mSuspended;
1146 
1147     int64_t                         mBytesWritten;
1148     int64_t                         mFramesWritten; // not reset on standby
1149     int64_t                         mLastFramesWritten = -1; // track changes in timestamp
1150                                                              // server frames written.
1151     int64_t                         mSuspendedFrames; // not reset on standby
1152 
1153     // mHapticChannelMask and mHapticChannelCount will only be valid when the thread support
1154     // haptic playback.
1155     audio_channel_mask_t            mHapticChannelMask = AUDIO_CHANNEL_NONE;
1156     uint32_t                        mHapticChannelCount = 0;
1157 
1158     audio_channel_mask_t            mMixerChannelMask = AUDIO_CHANNEL_NONE;
1159 
1160 private:
1161     // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
1162     // PlaybackThread needs to find out if master-muted, it checks it's local
1163     // copy rather than the one in AudioFlinger.  This optimization saves a lock.
1164     bool                            mMasterMute;
setMasterMute_l(bool muted)1165                 void        setMasterMute_l(bool muted) { mMasterMute = muted; }
1166 
discontinuityForStandbyOrFlush()1167                 auto discontinuityForStandbyOrFlush() const { // call on threadLoop or with lock.
1168                     return ((mType == DIRECT && !audio_is_linear_pcm(mFormat))
1169                                     || mType == OFFLOAD)
1170                             ? mTimestampVerifier.DISCONTINUITY_MODE_ZERO
1171                             : mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS;
1172                 }
1173 
1174 protected:
1175     ActiveTracks<Track>     mActiveTracks;
1176 
1177     // Time to sleep between cycles when:
1178     virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
1179     virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
1180     virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
1181     // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
1182     // No sleep in standby mode; waits on a condition
1183 
1184     // Code snippets that are temporarily lifted up out of threadLoop() until the merge
1185                 void        checkSilentMode_l();
1186 
1187     // Non-trivial for DUPLICATING only
saveOutputTracks()1188     virtual     void        saveOutputTracks() { }
clearOutputTracks()1189     virtual     void        clearOutputTracks() { }
1190 
1191     // Cache various calculated values, at threadLoop() entry and after a parameter change
1192     virtual     void        cacheParameters_l();
setCheckOutputStageEffects()1193                 void        setCheckOutputStageEffects() override {
1194                                 mCheckOutputStageEffects.store(true);
1195                             }
1196 
1197     virtual     uint32_t    correctLatency_l(uint32_t latency) const;
1198 
1199     virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
1200                                    audio_patch_handle_t *handle);
1201     virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
1202 
usesHwAvSync()1203                 bool        usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL)
1204                                     && mHwSupportsPause
1205                                     && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
1206 
1207                 uint32_t    trackCountForUid_l(uid_t uid) const;
1208 
invalidateTracksForAudioSession_l(audio_session_t sessionId)1209                 void        invalidateTracksForAudioSession_l(
1210                                     audio_session_t sessionId) const override {
1211                                 ThreadBase::invalidateTracksForAudioSession_l(sessionId, mTracks);
1212                             }
1213 
1214 private:
1215 
1216     friend class AudioFlinger;      // for numerous
1217 
1218     DISALLOW_COPY_AND_ASSIGN(PlaybackThread);
1219 
1220     status_t    addTrack_l(const sp<Track>& track);
1221     bool        destroyTrack_l(const sp<Track>& track);
1222     void        removeTrack_l(const sp<Track>& track);
1223 
1224     void        readOutputParameters_l();
1225     void        updateMetadata_l() final;
1226     virtual void sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata& metadata);
1227 
1228     void        collectTimestamps_l();
1229 
1230     // The Tracks class manages tracks added and removed from the Thread.
1231     template <typename T>
1232     class Tracks {
1233     public:
Tracks(bool saveDeletedTrackIds)1234         Tracks(bool saveDeletedTrackIds) :
1235             mSaveDeletedTrackIds(saveDeletedTrackIds) { }
1236 
1237         // SortedVector methods
add(const sp<T> & track)1238         ssize_t         add(const sp<T> &track) {
1239             const ssize_t index = mTracks.add(track);
1240             LOG_ALWAYS_FATAL_IF(index < 0, "cannot add track");
1241             return index;
1242         }
1243         ssize_t         remove(const sp<T> &track);
size()1244         size_t          size() const {
1245             return mTracks.size();
1246         }
isEmpty()1247         bool            isEmpty() const {
1248             return mTracks.isEmpty();
1249         }
indexOf(const sp<T> & item)1250         ssize_t         indexOf(const sp<T> &item) {
1251             return mTracks.indexOf(item);
1252         }
1253         sp<T>           operator[](size_t index) const {
1254             return mTracks[index];
1255         }
begin()1256         typename SortedVector<sp<T>>::iterator begin() {
1257             return mTracks.begin();
1258         }
end()1259         typename SortedVector<sp<T>>::iterator end() {
1260             return mTracks.end();
1261         }
1262 
processDeletedTrackIds(std::function<void (int)> f)1263         size_t          processDeletedTrackIds(std::function<void(int)> f) {
1264             for (const int trackId : mDeletedTrackIds) {
1265                 f(trackId);
1266             }
1267             return mDeletedTrackIds.size();
1268         }
1269 
clearDeletedTrackIds()1270         void            clearDeletedTrackIds() { mDeletedTrackIds.clear(); }
1271 
1272     private:
1273         // Tracks pending deletion for MIXER type threads
1274         const bool mSaveDeletedTrackIds; // true to enable tracking
1275         std::set<int> mDeletedTrackIds;
1276 
1277         SortedVector<sp<T>> mTracks; // wrapped SortedVector.
1278     };
1279 
1280     Tracks<Track>                   mTracks;
1281 
1282     stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT];
1283     AudioStreamOut                  *mOutput;
1284 
1285     float                           mMasterVolume;
1286     std::atomic<float>              mMasterBalance{};
1287     audio_utils::Balance            mBalance;
1288     int                             mNumWrites;
1289     int                             mNumDelayedWrites;
1290     bool                            mInWrite;
1291 
1292     // FIXME rename these former local variables of threadLoop to standard "m" names
1293     nsecs_t                         mStandbyTimeNs;
1294     size_t                          mSinkBufferSize;
1295 
1296     // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
1297     uint32_t                        mActiveSleepTimeUs;
1298     uint32_t                        mIdleSleepTimeUs;
1299 
1300     uint32_t                        mSleepTimeUs;
1301 
1302     // mixer status returned by prepareTracks_l()
1303     mixer_state                     mMixerStatus; // current cycle
1304                                                   // previous cycle when in prepareTracks_l()
1305     mixer_state                     mMixerStatusIgnoringFastTracks;
1306                                                   // FIXME or a separate ready state per track
1307 
1308     // FIXME move these declarations into the specific sub-class that needs them
1309     // MIXER only
1310     uint32_t                        sleepTimeShift;
1311 
1312     // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
1313     nsecs_t                         mStandbyDelayNs;
1314 
1315     // MIXER only
1316     nsecs_t                         maxPeriod;
1317 
1318     // DUPLICATING only
1319     uint32_t                        writeFrames;
1320 
1321     size_t                          mBytesRemaining;
1322     size_t                          mCurrentWriteLength;
1323     bool                            mUseAsyncWrite;
1324     // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
1325     // incremented each time a write(), a flush() or a standby() occurs.
1326     // Bit 0 is set when a write blocks and indicates a callback is expected.
1327     // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
1328     // callbacks are ignored.
1329     uint32_t                        mWriteAckSequence;
1330     // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
1331     // incremented each time a drain is requested or a flush() or standby() occurs.
1332     // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
1333     // expected.
1334     // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
1335     // callbacks are ignored.
1336     uint32_t                        mDrainSequence;
1337     sp<AsyncCallbackThread>         mCallbackThread;
1338 
1339     Mutex                                    mAudioTrackCbLock;
1340     // Record of IAudioTrackCallback
1341     std::map<sp<Track>, sp<media::IAudioTrackCallback>> mAudioTrackCallbacks;
1342 
1343 private:
1344     // The HAL output sink is treated as non-blocking, but current implementation is blocking
1345     sp<NBAIO_Sink>          mOutputSink;
1346     // If a fast mixer is present, the blocking pipe sink, otherwise clear
1347     sp<NBAIO_Sink>          mPipeSink;
1348     // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
1349     sp<NBAIO_Sink>          mNormalSink;
1350     uint32_t                mScreenState;   // cached copy of gScreenState
1351     // TODO: add comment and adjust size as needed
1352     static const size_t     kFastMixerLogSize = 8 * 1024;
1353     sp<NBLog::Writer>       mFastMixerNBLogWriter;
1354 
1355     // Downstream patch latency, available if mDownstreamLatencyStatMs.getN() > 0.
1356     audio_utils::Statistics<double> mDownstreamLatencyStatMs{0.999};
1357 
1358 public:
1359     virtual     bool        hasFastMixer() const = 0;
getFastTrackUnderruns(size_t fastIndex __unused)1360     virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
1361                                 { FastTrackUnderruns dummy; return dummy; }
1362 
1363 protected:
1364                 // accessed by both binder threads and within threadLoop(), lock on mutex needed
1365                 unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
1366                 bool        mHwSupportsPause;
1367                 bool        mHwPaused;
1368                 bool        mFlushPending;
1369                 // volumes last sent to audio HAL with stream->setVolume()
1370                 float mLeftVolFloat;
1371                 float mRightVolFloat;
1372 
1373                 // audio patch used by the downstream software patch.
1374                 // Only used if ThreadBase::mIsMsdDevice is true.
1375                 struct audio_patch mDownStreamPatch;
1376 
1377                 std::atomic_bool mCheckOutputStageEffects{};
1378 };
1379 
1380 class MixerThread : public PlaybackThread {
1381 public:
1382     MixerThread(const sp<AudioFlinger>& audioFlinger,
1383                 AudioStreamOut* output,
1384                 audio_io_handle_t id,
1385                 bool systemReady,
1386                 type_t type = MIXER,
1387                 audio_config_base_t *mixerConfig = nullptr);
1388     virtual             ~MixerThread();
1389 
1390     // Thread virtuals
1391 
1392     virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
1393                                                    status_t& status);
1394 
1395     virtual     bool        isTrackAllowed_l(
1396                                     audio_channel_mask_t channelMask, audio_format_t format,
1397                                     audio_session_t sessionId, uid_t uid) const override;
1398 protected:
1399     virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1400     virtual     uint32_t    idleSleepTimeUs() const;
1401     virtual     uint32_t    suspendSleepTimeUs() const;
1402     virtual     void        cacheParameters_l();
1403 
acquireWakeLock_l()1404     virtual void acquireWakeLock_l() {
1405         PlaybackThread::acquireWakeLock_l();
1406         if (hasFastMixer()) {
1407             mFastMixer->setBoottimeOffset(
1408                     mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1409         }
1410     }
1411 
1412                 void        dumpInternals_l(int fd, const Vector<String16>& args) override;
1413 
1414     // threadLoop snippets
1415     virtual     ssize_t     threadLoop_write();
1416     virtual     void        threadLoop_standby();
1417     virtual     void        threadLoop_mix();
1418     virtual     void        threadLoop_sleepTime();
1419     virtual     uint32_t    correctLatency_l(uint32_t latency) const;
1420 
1421     virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
1422                                    audio_patch_handle_t *handle);
1423     virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
1424 
1425                 AudioMixer* mAudioMixer;    // normal mixer
1426 private:
1427                 // one-time initialization, no locks required
1428                 sp<FastMixer>     mFastMixer;     // non-0 if there is also a fast mixer
1429                 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
1430 
1431                 // contents are not guaranteed to be consistent, no locks required
1432                 FastMixerDumpState mFastMixerDumpState;
1433 #ifdef STATE_QUEUE_DUMP
1434                 StateQueueObserverDump mStateQueueObserverDump;
1435                 StateQueueMutatorDump  mStateQueueMutatorDump;
1436 #endif
1437                 AudioWatchdogDump mAudioWatchdogDump;
1438 
1439                 // accessible only within the threadLoop(), no locks required
1440                 //          mFastMixer->sq()    // for mutating and pushing state
1441                 int32_t     mFastMixerFutex;    // for cold idle
1442 
1443                 std::atomic_bool mMasterMono;
1444 public:
hasFastMixer()1445     virtual     bool        hasFastMixer() const { return mFastMixer != 0; }
getFastTrackUnderruns(size_t fastIndex)1446     virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
1447                               ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
1448                               return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
1449                             }
1450 
threadloop_getHalTimestamp_l(ExtendedTimestamp * timestamp)1451                 status_t    threadloop_getHalTimestamp_l(
1452                                     ExtendedTimestamp *timestamp) const override {
1453                                 if (mNormalSink.get() != nullptr) {
1454                                     return mNormalSink->getTimestamp(*timestamp);
1455                                 }
1456                                 return INVALID_OPERATION;
1457                             }
1458 
1459 protected:
setMasterMono_l(bool mono)1460     virtual     void       setMasterMono_l(bool mono) {
1461                                mMasterMono.store(mono);
1462                                if (mFastMixer != nullptr) { /* hasFastMixer() */
1463                                    mFastMixer->setMasterMono(mMasterMono);
1464                                }
1465                            }
1466                 // the FastMixer performs mono blend if it exists.
1467                 // Blending with limiter is not idempotent,
1468                 // and blending without limiter is idempotent but inefficient to do twice.
requireMonoBlend()1469     virtual     bool       requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
1470 
setMasterBalance(float balance)1471                 void       setMasterBalance(float balance) override {
1472                                mMasterBalance.store(balance);
1473                                if (hasFastMixer()) {
1474                                    mFastMixer->setMasterBalance(balance);
1475                                }
1476                            }
1477 };
1478 
1479 class DirectOutputThread : public PlaybackThread {
1480 public:
1481 
DirectOutputThread(const sp<AudioFlinger> & audioFlinger,AudioStreamOut * output,audio_io_handle_t id,bool systemReady)1482     DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1483                        audio_io_handle_t id, bool systemReady)
1484         : DirectOutputThread(audioFlinger, output, id, DIRECT, systemReady) { }
1485 
1486     virtual                 ~DirectOutputThread();
1487 
1488                 status_t    selectPresentation(int presentationId, int programId);
1489 
1490     // Thread virtuals
1491 
1492     virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
1493                                                    status_t& status);
1494 
1495     virtual     void        flushHw_l();
1496 
1497                 void        setMasterBalance(float balance) override;
1498 
1499 protected:
1500     virtual     uint32_t    activeSleepTimeUs() const;
1501     virtual     uint32_t    idleSleepTimeUs() const;
1502     virtual     uint32_t    suspendSleepTimeUs() const;
1503     virtual     void        cacheParameters_l();
1504 
1505                 void        dumpInternals_l(int fd, const Vector<String16>& args) override;
1506 
1507     // threadLoop snippets
1508     virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1509     virtual     void        threadLoop_mix();
1510     virtual     void        threadLoop_sleepTime();
1511     virtual     void        threadLoop_exit();
1512     virtual     bool        shouldStandby_l();
1513 
1514     virtual     void        onAddNewTrack_l();
1515 
1516     bool mVolumeShaperActive = false;
1517 
1518     DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1519                        audio_io_handle_t id, ThreadBase::type_t type, bool systemReady);
1520     void processVolume_l(Track *track, bool lastTrack);
1521 
1522     // prepareTracks_l() tells threadLoop_mix() the name of the single active track
1523     sp<Track>               mActiveTrack;
1524 
1525     wp<Track>               mPreviousTrack;         // used to detect track switch
1526 
1527     // This must be initialized for initial condition of mMasterBalance = 0 (disabled).
1528     float                   mMasterBalanceLeft = 1.f;
1529     float                   mMasterBalanceRight = 1.f;
1530 
1531 public:
hasFastMixer()1532     virtual     bool        hasFastMixer() const { return false; }
1533 
1534     virtual     int64_t     computeWaitTimeNs_l() const override;
1535 
threadloop_getHalTimestamp_l(ExtendedTimestamp * timestamp)1536     status_t    threadloop_getHalTimestamp_l(ExtendedTimestamp *timestamp) const override {
1537                     // For DIRECT and OFFLOAD threads, query the output sink directly.
1538                     if (mOutput != nullptr) {
1539                         uint64_t uposition64;
1540                         struct timespec time;
1541                         if (mOutput->getPresentationPosition(
1542                                 &uposition64, &time) == OK) {
1543                             timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL]
1544                                     = (int64_t)uposition64;
1545                             timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
1546                                     = audio_utils_ns_from_timespec(&time);
1547                             return NO_ERROR;
1548                         }
1549                     }
1550                     return INVALID_OPERATION;
1551                 }
1552 };
1553 
1554 class OffloadThread : public DirectOutputThread {
1555 public:
1556 
1557     OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1558                   audio_io_handle_t id, bool systemReady);
~OffloadThread()1559     virtual                 ~OffloadThread() {};
1560     virtual     void        flushHw_l();
1561 
1562 protected:
1563     // threadLoop snippets
1564     virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1565     virtual     void        threadLoop_exit();
1566 
1567     virtual     bool        waitingAsyncCallback();
1568     virtual     bool        waitingAsyncCallback_l();
1569     virtual     void        invalidateTracks(audio_stream_type_t streamType);
1570 
keepWakeLock()1571     virtual     bool        keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
1572 
1573 private:
1574     size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
1575     size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
1576     bool        mKeepWakeLock;          // keep wake lock while waiting for write callback
1577     uint64_t    mOffloadUnderrunPosition; // Current frame position for offloaded playback
1578                                           // used and valid only during underrun.  ~0 if
1579                                           // no underrun has occurred during playback and
1580                                           // is not reset on standby.
1581 };
1582 
1583 class AsyncCallbackThread : public Thread {
1584 public:
1585 
1586     explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
1587 
1588     virtual             ~AsyncCallbackThread();
1589 
1590     // Thread virtuals
1591     virtual bool        threadLoop();
1592 
1593     // RefBase
1594     virtual void        onFirstRef();
1595 
1596             void        exit();
1597             void        setWriteBlocked(uint32_t sequence);
1598             void        resetWriteBlocked();
1599             void        setDraining(uint32_t sequence);
1600             void        resetDraining();
1601             void        setAsyncError();
1602 
1603 private:
1604     const wp<PlaybackThread>   mPlaybackThread;
1605     // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
1606     // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
1607     // to indicate that the callback has been received via resetWriteBlocked()
1608     uint32_t                   mWriteAckSequence;
1609     // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
1610     // setDraining(). The sequence is shifted one bit to the left and the lsb is used
1611     // to indicate that the callback has been received via resetDraining()
1612     uint32_t                   mDrainSequence;
1613     Condition                  mWaitWorkCV;
1614     Mutex                      mLock;
1615     bool                       mAsyncError;
1616 };
1617 
1618 class DuplicatingThread : public MixerThread {
1619 public:
1620     DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
1621                       audio_io_handle_t id, bool systemReady);
1622     virtual                 ~DuplicatingThread();
1623 
1624     // Thread virtuals
1625                 void        addOutputTrack(MixerThread* thread);
1626                 void        removeOutputTrack(MixerThread* thread);
waitTimeMs()1627                 uint32_t    waitTimeMs() const { return mWaitTimeMs; }
1628 
1629                 void        sendMetadataToBackend_l(
1630                         const StreamOutHalInterface::SourceMetadata& metadata) override;
1631 protected:
1632     virtual     uint32_t    activeSleepTimeUs() const;
1633                 void        dumpInternals_l(int fd, const Vector<String16>& args) override;
1634 
1635 private:
1636                 bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
1637 protected:
1638     // threadLoop snippets
1639     virtual     void        threadLoop_mix();
1640     virtual     void        threadLoop_sleepTime();
1641     virtual     ssize_t     threadLoop_write();
1642     virtual     void        threadLoop_standby();
1643     virtual     void        cacheParameters_l();
1644 
1645 private:
1646     // called from threadLoop, addOutputTrack, removeOutputTrack
1647     virtual     void        updateWaitTime_l();
1648 protected:
1649     virtual     void        saveOutputTracks();
1650     virtual     void        clearOutputTracks();
1651 private:
1652 
1653                 uint32_t    mWaitTimeMs;
1654     SortedVector < sp<OutputTrack> >  outputTracks;
1655     SortedVector < sp<OutputTrack> >  mOutputTracks;
1656 public:
hasFastMixer()1657     virtual     bool        hasFastMixer() const { return false; }
threadloop_getHalTimestamp_l(ExtendedTimestamp * timestamp)1658                 status_t    threadloop_getHalTimestamp_l(
1659                                     ExtendedTimestamp *timestamp) const override {
1660         if (mOutputTracks.size() > 0) {
1661             // forward the first OutputTrack's kernel information for timestamp.
1662             const ExtendedTimestamp trackTimestamp =
1663                     mOutputTracks[0]->getClientProxyTimestamp();
1664             if (trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
1665                 timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
1666                         trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
1667                 timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
1668                         trackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1669                 return OK;  // discard server timestamp - that's ignored.
1670             }
1671         }
1672         return INVALID_OPERATION;
1673     }
1674 };
1675 
1676 class SpatializerThread : public MixerThread {
1677 public:
1678     SpatializerThread(const sp<AudioFlinger>& audioFlinger,
1679                            AudioStreamOut* output,
1680                            audio_io_handle_t id,
1681                            bool systemReady,
1682                            audio_config_base_t *mixerConfig);
~SpatializerThread()1683             ~SpatializerThread() override {}
1684 
hasFastMixer()1685             bool hasFastMixer() const override { return false; }
1686 
1687 protected:
1688             void checkOutputStageEffects() override;
1689 
1690 private:
1691             sp<EffectHandle> mFinalDownMixer;
1692 };
1693 
1694 // record thread
1695 class RecordThread : public ThreadBase
1696 {
1697 public:
1698 
1699     class RecordTrack;
1700 
1701     /* The ResamplerBufferProvider is used to retrieve recorded input data from the
1702      * RecordThread.  It maintains local state on the relative position of the read
1703      * position of the RecordTrack compared with the RecordThread.
1704      */
1705     class ResamplerBufferProvider : public AudioBufferProvider
1706     {
1707     public:
ResamplerBufferProvider(RecordTrack * recordTrack)1708         explicit ResamplerBufferProvider(RecordTrack* recordTrack) :
1709             mRecordTrack(recordTrack),
1710             mRsmpInUnrel(0), mRsmpInFront(0) { }
~ResamplerBufferProvider()1711         virtual ~ResamplerBufferProvider() { }
1712 
1713         // called to set the ResamplerBufferProvider to head of the RecordThread data buffer,
1714         // skipping any previous data read from the hal.
1715         virtual void reset();
1716 
1717         /* Synchronizes RecordTrack position with the RecordThread.
1718          * Calculates available frames and handle overruns if the RecordThread
1719          * has advanced faster than the ResamplerBufferProvider has retrieved data.
1720          * TODO: why not do this for every getNextBuffer?
1721          *
1722          * Parameters
1723          * framesAvailable:  pointer to optional output size_t to store record track
1724          *                   frames available.
1725          *      hasOverrun:  pointer to optional boolean, returns true if track has overrun.
1726          */
1727 
1728         virtual void sync(size_t *framesAvailable = NULL, bool *hasOverrun = NULL);
1729 
1730         // AudioBufferProvider interface
1731         virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer);
1732         virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
1733 
getFront()1734                 int32_t     getFront() const { return mRsmpInFront; }
setFront(int32_t front)1735                 void        setFront(int32_t front) { mRsmpInFront = front; }
1736     private:
1737         RecordTrack * const mRecordTrack;
1738         size_t              mRsmpInUnrel;   // unreleased frames remaining from
1739                                             // most recent getNextBuffer
1740                                             // for debug only
1741         int32_t             mRsmpInFront;   // next available frame
1742                                             // rolling counter that is never cleared
1743     };
1744 
1745 #include "RecordTracks.h"
1746 
1747             RecordThread(const sp<AudioFlinger>& audioFlinger,
1748                     AudioStreamIn *input,
1749                     audio_io_handle_t id,
1750                     bool systemReady
1751                     );
1752             virtual     ~RecordThread();
1753 
1754     // no addTrack_l ?
1755     void        destroyTrack_l(const sp<RecordTrack>& track);
1756     void        removeTrack_l(const sp<RecordTrack>& track);
1757 
1758     // Thread virtuals
1759     virtual bool        threadLoop();
1760     virtual void        preExit();
1761 
1762     // RefBase
1763     virtual void        onFirstRef();
1764 
initCheck()1765     virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1766 
readOnlyHeap()1767     virtual sp<MemoryDealer>    readOnlyHeap() const { return mReadOnlyHeap; }
1768 
pipeMemory()1769     virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
1770 
1771             sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
1772                     const sp<AudioFlinger::Client>& client,
1773                     const audio_attributes_t& attr,
1774                     uint32_t *pSampleRate,
1775                     audio_format_t format,
1776                     audio_channel_mask_t channelMask,
1777                     size_t *pFrameCount,
1778                     audio_session_t sessionId,
1779                     size_t *pNotificationFrameCount,
1780                     pid_t creatorPid,
1781                     const AttributionSourceState& attributionSource,
1782                     audio_input_flags_t *flags,
1783                     pid_t tid,
1784                     status_t *status /*non-NULL*/,
1785                     audio_port_handle_t portId,
1786                     int32_t maxSharedAudioHistoryMs);
1787 
1788             status_t    start(RecordTrack* recordTrack,
1789                               AudioSystem::sync_event_t event,
1790                               audio_session_t triggerSession);
1791 
1792             // ask the thread to stop the specified track, and
1793             // return true if the caller should then do it's part of the stopping process
1794             bool        stop(RecordTrack* recordTrack);
1795 
1796             AudioStreamIn* clearInput();
1797             virtual sp<StreamHalInterface> stream() const;
1798 
1799 
1800     virtual bool        checkForNewParameter_l(const String8& keyValuePair,
1801                                                status_t& status);
cacheParameters_l()1802     virtual void        cacheParameters_l() {}
1803     virtual String8     getParameters(const String8& keys);
1804     virtual void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0,
1805                                         audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
1806     virtual status_t    createAudioPatch_l(const struct audio_patch *patch,
1807                                            audio_patch_handle_t *handle);
1808     virtual status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
1809             void        updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override;
1810             void        resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override;
1811 
1812             void        addPatchTrack(const sp<PatchRecord>& record);
1813             void        deletePatchTrack(const sp<PatchRecord>& record);
1814 
1815             void        readInputParameters_l();
1816     virtual uint32_t    getInputFramesLost();
1817 
1818     virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1819     virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
hasAudioSession_l(audio_session_t sessionId)1820             uint32_t hasAudioSession_l(audio_session_t sessionId) const override {
1821                          return ThreadBase::hasAudioSession_l(sessionId, mTracks);
1822                      }
1823 
1824             // Return the set of unique session IDs across all tracks.
1825             // The keys are the session IDs, and the associated values are meaningless.
1826             // FIXME replace by Set [and implement Bag/Multiset for other uses].
1827             KeyedVector<audio_session_t, bool> sessionIds() const;
1828 
1829     virtual status_t setSyncEvent(const sp<SyncEvent>& event);
1830     virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
1831 
1832     static void syncStartEventCallback(const wp<SyncEvent>& event);
1833 
frameCount()1834     virtual size_t      frameCount() const { return mFrameCount; }
hasFastCapture()1835             bool        hasFastCapture() const { return mFastCapture != 0; }
1836     virtual void        toAudioPortConfig(struct audio_port_config *config);
1837 
1838     virtual status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
1839                                                    audio_session_t sessionId);
1840 
acquireWakeLock_l()1841     virtual void        acquireWakeLock_l() {
1842                             ThreadBase::acquireWakeLock_l();
1843                             mActiveTracks.updatePowerState(this, true /* force */);
1844                         }
1845 
1846             void        checkBtNrec();
1847 
1848             // Sets the UID records silence
1849             void        setRecordSilenced(audio_port_handle_t portId, bool silenced);
1850 
1851             status_t    getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones);
1852 
1853             status_t    setPreferredMicrophoneDirection(audio_microphone_direction_t direction);
1854             status_t    setPreferredMicrophoneFieldDimension(float zoom);
1855 
1856             void        updateMetadata_l() override;
1857 
fastTrackAvailable()1858             bool        fastTrackAvailable() const { return mFastTrackAvail; }
1859 
isTimestampCorrectionEnabled()1860             bool        isTimestampCorrectionEnabled() const override {
1861                             // checks popcount for exactly one device.
1862                             return audio_is_input_device(mTimestampCorrectedDevice)
1863                                     && inDeviceType() == mTimestampCorrectedDevice;
1864                         }
1865 
1866             status_t    shareAudioHistory(const std::string& sharedAudioPackageName,
1867                                           audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
1868                                           int64_t sharedAudioStartMs = -1);
1869             status_t    shareAudioHistory_l(const std::string& sharedAudioPackageName,
1870                                           audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
1871                                           int64_t sharedAudioStartMs = -1);
1872             void        resetAudioHistory_l();
1873 
isStreamInitialized()1874     virtual bool        isStreamInitialized() {
1875                             return !(mInput == nullptr || mInput->stream == nullptr);
1876                         }
1877 
1878 protected:
1879             void        dumpInternals_l(int fd, const Vector<String16>& args) override;
1880             void        dumpTracks_l(int fd, const Vector<String16>& args) override;
1881 
1882 private:
1883             // Enter standby if not already in standby, and set mStandby flag
1884             void    standbyIfNotAlreadyInStandby();
1885 
1886             // Call the HAL standby method unconditionally, and don't change mStandby flag
1887             void    inputStandBy();
1888 
1889             void    checkBtNrec_l();
1890 
1891             int32_t getOldestFront_l();
1892             void    updateFronts_l(int32_t offset);
1893 
1894             AudioStreamIn                       *mInput;
1895             Source                              *mSource;
1896             SortedVector < sp<RecordTrack> >    mTracks;
1897             // mActiveTracks has dual roles:  it indicates the current active track(s), and
1898             // is used together with mStartStopCond to indicate start()/stop() progress
1899             ActiveTracks<RecordTrack>           mActiveTracks;
1900 
1901             Condition                           mStartStopCond;
1902 
1903             // resampler converts input at HAL Hz to output at AudioRecord client Hz
1904             void                               *mRsmpInBuffer;  // size = mRsmpInFramesOA
1905             size_t                              mRsmpInFrames;  // size of resampler input in frames
1906             size_t                              mRsmpInFramesP2;// size rounded up to a power-of-2
1907             size_t                              mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
1908 
1909             // rolling index that is never cleared
1910             int32_t                             mRsmpInRear;    // last filled frame + 1
1911 
1912             // For dumpsys
1913             const sp<MemoryDealer>              mReadOnlyHeap;
1914 
1915             // one-time initialization, no locks required
1916             sp<FastCapture>                     mFastCapture;   // non-0 if there is also
1917                                                                 // a fast capture
1918 
1919             // FIXME audio watchdog thread
1920 
1921             // contents are not guaranteed to be consistent, no locks required
1922             FastCaptureDumpState                mFastCaptureDumpState;
1923 #ifdef STATE_QUEUE_DUMP
1924             // FIXME StateQueue observer and mutator dump fields
1925 #endif
1926             // FIXME audio watchdog dump
1927 
1928             // accessible only within the threadLoop(), no locks required
1929             //          mFastCapture->sq()      // for mutating and pushing state
1930             int32_t     mFastCaptureFutex;      // for cold idle
1931 
1932             // The HAL input source is treated as non-blocking,
1933             // but current implementation is blocking
1934             sp<NBAIO_Source>                    mInputSource;
1935             // The source for the normal capture thread to read from: mInputSource or mPipeSource
1936             sp<NBAIO_Source>                    mNormalSource;
1937             // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
1938             // otherwise clear
1939             sp<NBAIO_Sink>                      mPipeSink;
1940             // If a fast capture is present, the non-blocking pipe source read by normal thread,
1941             // otherwise clear
1942             sp<NBAIO_Source>                    mPipeSource;
1943             // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
1944             size_t                              mPipeFramesP2;
1945             // If a fast capture is present, the Pipe as IMemory, otherwise clear
1946             sp<IMemory>                         mPipeMemory;
1947 
1948             // TODO: add comment and adjust size as needed
1949             static const size_t                 kFastCaptureLogSize = 4 * 1024;
1950             sp<NBLog::Writer>                   mFastCaptureNBLogWriter;
1951 
1952             bool                                mFastTrackAvail;    // true if fast track available
1953             // common state to all record threads
1954             std::atomic_bool                    mBtNrecSuspended;
1955 
1956             int64_t                             mFramesRead = 0;    // continuous running counter.
1957 
1958             DeviceDescriptorBaseVector          mOutDevices;
1959 
1960             int32_t                             mMaxSharedAudioHistoryMs = 0;
1961             std::string                         mSharedAudioPackageName = {};
1962             int32_t                             mSharedAudioStartFrames = -1;
1963             audio_session_t                     mSharedAudioSessionId = AUDIO_SESSION_NONE;
1964 };
1965 
1966 class MmapThread : public ThreadBase
1967 {
1968  public:
1969 
1970 #include "MmapTracks.h"
1971 
1972     MmapThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1973                AudioHwDevice *hwDev, sp<StreamHalInterface> stream, bool systemReady,
1974                bool isOut);
1975     virtual     ~MmapThread();
1976 
1977     virtual     void        configure(const audio_attributes_t *attr,
1978                                       audio_stream_type_t streamType,
1979                                       audio_session_t sessionId,
1980                                       const sp<MmapStreamCallback>& callback,
1981                                       audio_port_handle_t deviceId,
1982                                       audio_port_handle_t portId);
1983 
1984                 void        disconnect();
1985 
1986     // MmapStreamInterface
1987     status_t createMmapBuffer(int32_t minSizeFrames,
1988                                       struct audio_mmap_buffer_info *info);
1989     status_t getMmapPosition(struct audio_mmap_position *position);
1990     status_t start(const AudioClient& client,
1991                    const audio_attributes_t *attr,
1992                    audio_port_handle_t *handle);
1993     status_t stop(audio_port_handle_t handle);
1994     status_t standby();
1995     virtual status_t getExternalPosition(uint64_t *position, int64_t *timeNaos) = 0;
1996 
1997     // RefBase
1998     virtual     void        onFirstRef();
1999 
2000     // Thread virtuals
2001     virtual     bool        threadLoop();
2002 
2003     virtual     void        threadLoop_exit();
2004     virtual     void        threadLoop_standby();
shouldStandby_l()2005     virtual     bool        shouldStandby_l() { return false; }
2006     virtual     status_t    exitStandby();
2007 
initCheck()2008     virtual     status_t    initCheck() const { return (mHalStream == 0) ? NO_INIT : NO_ERROR; }
frameCount()2009     virtual     size_t      frameCount() const { return mFrameCount; }
2010     virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
2011                                                     status_t& status);
2012     virtual     String8     getParameters(const String8& keys);
2013     virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0,
2014                                             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
2015                 void        readHalParameters_l();
cacheParameters_l()2016     virtual     void        cacheParameters_l() {}
2017     virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
2018                                                audio_patch_handle_t *handle);
2019     virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
2020     virtual     void        toAudioPortConfig(struct audio_port_config *config);
2021 
stream()2022     virtual     sp<StreamHalInterface> stream() const { return mHalStream; }
2023     virtual     status_t    addEffectChain_l(const sp<EffectChain>& chain);
2024     virtual     size_t      removeEffectChain_l(const sp<EffectChain>& chain);
2025     virtual     status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
2026                                                                audio_session_t sessionId);
2027 
hasAudioSession_l(audio_session_t sessionId)2028                 uint32_t    hasAudioSession_l(audio_session_t sessionId) const override {
2029                                 // Note: using mActiveTracks as no mTracks here.
2030                                 return ThreadBase::hasAudioSession_l(sessionId, mActiveTracks);
2031                             }
2032     virtual     status_t    setSyncEvent(const sp<SyncEvent>& event);
2033     virtual     bool        isValidSyncEvent(const sp<SyncEvent>& event) const;
2034 
checkSilentMode_l()2035     virtual     void        checkSilentMode_l() {}
processVolume_l()2036     virtual     void        processVolume_l() {}
2037                 void        checkInvalidTracks_l();
2038 
streamType()2039     virtual     audio_stream_type_t streamType() { return AUDIO_STREAM_DEFAULT; }
2040 
invalidateTracks(audio_stream_type_t streamType __unused)2041     virtual     void        invalidateTracks(audio_stream_type_t streamType __unused) {}
2042 
2043                 // Sets the UID records silence
setRecordSilenced(audio_port_handle_t portId __unused,bool silenced __unused)2044     virtual     void        setRecordSilenced(audio_port_handle_t portId __unused,
2045                                               bool silenced __unused) {}
2046 
isStreamInitialized()2047     virtual     bool        isStreamInitialized() { return false; }
2048 
2049  protected:
2050                 void        dumpInternals_l(int fd, const Vector<String16>& args) override;
2051                 void        dumpTracks_l(int fd, const Vector<String16>& args) override;
2052 
2053                 /**
2054                  * @brief mDeviceId  current device port unique identifier
2055                  */
2056                 audio_port_handle_t     mDeviceId = AUDIO_PORT_HANDLE_NONE;
2057 
2058                 audio_attributes_t      mAttr;
2059                 audio_session_t         mSessionId;
2060                 audio_port_handle_t     mPortId;
2061 
2062                 wp<MmapStreamCallback>  mCallback;
2063                 sp<StreamHalInterface>  mHalStream;
2064                 sp<DeviceHalInterface>  mHalDevice;
2065                 AudioHwDevice* const    mAudioHwDev;
2066                 ActiveTracks<MmapTrack> mActiveTracks;
2067                 float                   mHalVolFloat;
2068 
2069                 int32_t                 mNoCallbackWarningCount;
2070      static     constexpr int32_t       kMaxNoCallbackWarnings = 5;
2071 };
2072 
2073 class MmapPlaybackThread : public MmapThread, public VolumeInterface
2074 {
2075 
2076 public:
2077     MmapPlaybackThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
2078                        AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady);
~MmapPlaybackThread()2079     virtual     ~MmapPlaybackThread() {}
2080 
2081     virtual     void        configure(const audio_attributes_t *attr,
2082                                       audio_stream_type_t streamType,
2083                                       audio_session_t sessionId,
2084                                       const sp<MmapStreamCallback>& callback,
2085                                       audio_port_handle_t deviceId,
2086                                       audio_port_handle_t portId);
2087 
2088                 AudioStreamOut* clearOutput();
2089 
2090                 // VolumeInterface
2091     virtual     void        setMasterVolume(float value);
2092     virtual     void        setMasterMute(bool muted);
2093     virtual     void        setStreamVolume(audio_stream_type_t stream, float value);
2094     virtual     void        setStreamMute(audio_stream_type_t stream, bool muted);
2095     virtual     float       streamVolume(audio_stream_type_t stream) const;
2096 
setMasterMute_l(bool muted)2097                 void        setMasterMute_l(bool muted) { mMasterMute = muted; }
2098 
2099     virtual     void        invalidateTracks(audio_stream_type_t streamType);
2100 
streamType()2101     virtual     audio_stream_type_t streamType() { return mStreamType; }
2102     virtual     void        checkSilentMode_l();
2103                 void        processVolume_l() override;
2104 
2105                 void        updateMetadata_l() override;
2106 
2107     virtual     void        toAudioPortConfig(struct audio_port_config *config);
2108 
2109                 status_t    getExternalPosition(uint64_t *position, int64_t *timeNanos) override;
2110 
isStreamInitialized()2111     virtual     bool        isStreamInitialized() {
2112                                 return !(mOutput == nullptr || mOutput->stream == nullptr);
2113                             }
2114 
2115 protected:
2116                 void        dumpInternals_l(int fd, const Vector<String16>& args) override;
2117 
2118                 audio_stream_type_t         mStreamType;
2119                 float                       mMasterVolume;
2120                 float                       mStreamVolume;
2121                 bool                        mMasterMute;
2122                 bool                        mStreamMute;
2123                 AudioStreamOut*             mOutput;
2124 };
2125 
2126 class MmapCaptureThread : public MmapThread
2127 {
2128 
2129 public:
2130     MmapCaptureThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
2131                       AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady);
~MmapCaptureThread()2132     virtual     ~MmapCaptureThread() {}
2133 
2134                 AudioStreamIn* clearInput();
2135 
2136                 status_t       exitStandby() override;
2137 
2138                 void           updateMetadata_l() override;
2139                 void           processVolume_l() override;
2140                 void           setRecordSilenced(audio_port_handle_t portId,
2141                                                  bool silenced) override;
2142 
2143     virtual     void           toAudioPortConfig(struct audio_port_config *config);
2144 
2145                 status_t       getExternalPosition(uint64_t *position, int64_t *timeNanos) override;
2146 
isStreamInitialized()2147     virtual     bool           isStreamInitialized() {
2148                                    return !(mInput == nullptr || mInput->stream == nullptr);
2149                                }
2150 
2151 protected:
2152 
2153                 AudioStreamIn*  mInput;
2154 };
2155