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