xref: /aosp_15_r20/frameworks/av/services/audioflinger/IAfThread.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android/media/IAudioTrackCallback.h>
20 #include <android/media/IEffectClient.h>
21 #include <audiomanager/IAudioManager.h>
22 #include <audio_utils/DeferredExecutor.h>
23 #include <audio_utils/MelProcessor.h>
24 #include <audio_utils/mutex.h>
25 #include <binder/MemoryDealer.h>
26 #include <datapath/AudioStreamIn.h>
27 #include <datapath/AudioStreamOut.h>
28 #include <datapath/VolumeInterface.h>
29 #include <datapath/VolumePortInterface.h>
30 #include <fastpath/FastMixerDumpState.h>
31 #include <media/DeviceDescriptorBase.h>
32 #include <media/MmapStreamInterface.h>
33 #include <media/audiohal/StreamHalInterface.h>
34 #include <media/nblog/NBLog.h>
35 #include <timing/SyncEvent.h>
36 #include <utils/RefBase.h>
37 #include <vibrator/ExternalVibration.h>
38 
39 #include <optional>
40 
41 namespace com::android::media::permission {
42     class IPermissionProvider;
43 }
44 
45 namespace android {
46 
47 class IAfDirectOutputThread;
48 class IAfDuplicatingThread;
49 class IAfMmapCaptureThread;
50 class IAfMmapPlaybackThread;
51 class IAfPlaybackThread;
52 class IAfRecordThread;
53 
54 class IAfEffectChain;
55 class IAfEffectHandle;
56 class IAfEffectModule;
57 class IAfPatchPanel;
58 class IAfPatchRecord;
59 class IAfPatchTrack;
60 class IAfRecordTrack;
61 class IAfTrack;
62 class IAfTrackBase;
63 class Client;
64 class MelReporter;
65 
66 // Used internally for Threads.cpp and AudioFlinger.cpp
67 struct stream_type_t {
68     float volume = 1.f;
69     bool mute = false;
70 };
71 
72 // Note this is exposed through IAfThreadBase::afThreadCallback()
73 // and hence may be used by the Effect / Track framework.
74 class IAfThreadCallback : public virtual RefBase {
75 public:
76     virtual audio_utils::mutex& mutex() const
77             RETURN_CAPABILITY(audio_utils::AudioFlinger_Mutex) = 0;
78     virtual bool isNonOffloadableGlobalEffectEnabled_l() const
79             REQUIRES(mutex()) EXCLUDES_ThreadBase_Mutex = 0;  // Tracks
80     virtual audio_unique_id_t nextUniqueId(audio_unique_id_use_t use) = 0;
81     virtual bool btNrecIsOff() const = 0;
82     virtual float masterVolume_l() const
83             REQUIRES(mutex()) = 0;
84     virtual bool masterMute_l() const
85             REQUIRES(mutex()) = 0;
86     virtual float getMasterBalance_l() const
87             REQUIRES(mutex()) = 0;
88     virtual bool streamMute_l(audio_stream_type_t stream) const
89             REQUIRES(mutex()) = 0;
90     virtual audio_mode_t getMode() const = 0;
91     virtual bool isLowRamDevice() const = 0;
92     virtual bool isAudioPolicyReady() const = 0;  // Effects
93     virtual uint32_t getScreenState() const = 0;
94     virtual std::optional<media::AudioVibratorInfo> getDefaultVibratorInfo_l() const
95             REQUIRES(mutex()) = 0;
96     virtual const sp<IAfPatchPanel>& getPatchPanel() const = 0;
97     virtual const sp<MelReporter>& getMelReporter() const = 0;
98     virtual const sp<EffectsFactoryHalInterface>& getEffectsFactoryHal() const = 0;
99     virtual sp<IAudioManager> getOrCreateAudioManager() = 0;  // Tracks
100 
101     virtual bool updateOrphanEffectChains(const sp<IAfEffectModule>& effect)
102             EXCLUDES_AudioFlinger_Mutex = 0;
103     virtual status_t moveEffectChain_ll(audio_session_t sessionId,
104             IAfPlaybackThread* srcThread, IAfPlaybackThread* dstThread,
105             IAfEffectChain* srcChain = nullptr)
106             REQUIRES(mutex(), audio_utils::ThreadBase_Mutex) = 0;
107 
108     virtual void requestLogMerge() = 0;
109     virtual sp<NBLog::Writer> newWriter_l(size_t size, const char *name)
110             REQUIRES(mutex()) = 0;
111     virtual void unregisterWriter(const sp<NBLog::Writer>& writer) = 0;
112 
113     virtual sp<audioflinger::SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
114             audio_session_t triggerSession,
115             audio_session_t listenerSession,
116             const audioflinger::SyncEventCallback& callBack,
117             const wp<IAfTrackBase>& cookie)
118             EXCLUDES_AudioFlinger_Mutex = 0;
119 
120     // Hold either AudioFlinger::mutex or ThreadBase::mutex
121     virtual void ioConfigChanged_l(audio_io_config_event_t event,
122             const sp<AudioIoDescriptor>& ioDesc,
123             pid_t pid = 0) EXCLUDES_AudioFlinger_ClientMutex = 0;
124     virtual void onNonOffloadableGlobalEffectEnable() EXCLUDES_AudioFlinger_Mutex = 0;
125     virtual void onSupportedLatencyModesChanged(audio_io_handle_t output,
126                                                 const std::vector<audio_latency_mode_t>& modes)
127             EXCLUDES_AudioFlinger_ClientMutex = 0;
128 
129     virtual void onHardError(std::set<audio_port_handle_t>& trackPortIds) = 0;
130 
131     virtual const ::com::android::media::permission::IPermissionProvider&
132             getPermissionProvider() = 0;
133 };
134 
135 class IAfThreadBase : public virtual RefBase {
136 public:
137     enum type_t {
138         MIXER,          // Thread class is MixerThread
139         DIRECT,         // Thread class is DirectOutputThread
140         DUPLICATING,    // Thread class is DuplicatingThread
141         RECORD,         // Thread class is RecordThread
142         OFFLOAD,        // Thread class is OffloadThread
143         MMAP_PLAYBACK,  // Thread class for MMAP playback stream
144         MMAP_CAPTURE,   // Thread class for MMAP capture stream
145         SPATIALIZER,    //
146         BIT_PERFECT,    // Thread class for BitPerfectThread
147         // When adding a value, also update IAfThreadBase::threadTypeToString()
148     };
149 
150     static const char* threadTypeToString(type_t type);
151     static std::string formatToString(audio_format_t format);  // compliant for MediaMetrics
152     static bool isValidPcmSinkChannelMask(audio_channel_mask_t channelMask);
153     static bool isValidPcmSinkFormat(audio_format_t format);
154 
155     virtual status_t readyToRun() = 0;
156     virtual void clearPowerManager() EXCLUDES_ThreadBase_Mutex = 0;
157     virtual status_t initCheck() const = 0;
158     virtual type_t type() const = 0;
159     virtual bool isDuplicating() const = 0;
160     virtual audio_io_handle_t id() const = 0;
161     virtual uint32_t sampleRate() const = 0;
162     virtual audio_channel_mask_t channelMask() const = 0;
163     virtual audio_channel_mask_t mixerChannelMask() const = 0;
164     virtual audio_format_t format() const = 0;
165     virtual uint32_t channelCount() const = 0;
166     virtual std::string flagsAsString() const = 0;
167 
168     // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
169     // and returns the [normal mix] buffer's frame count.
170     virtual size_t frameCount() const = 0;
171     virtual audio_channel_mask_t hapticChannelMask() const = 0;
172     virtual uint32_t hapticChannelCount() const = 0;
173     virtual uint32_t latency_l() const = 0;  // NO_THREAD_SAFETY_ANALYSIS
174     virtual void setVolumeForOutput_l(float left, float right) const REQUIRES(mutex()) = 0;
175 
176     // Return's the HAL's frame count i.e. fast mixer buffer size.
177     virtual size_t frameCountHAL() const = 0;
178     virtual size_t frameSize() const = 0;
179     // Should be "virtual status_t requestExitAndWait()" and override same
180     // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
181     virtual void exit() EXCLUDES_ThreadBase_Mutex = 0;
182     virtual bool checkForNewParameter_l(const String8& keyValuePair, status_t& status)
183              REQUIRES(mutex()) = 0;
184     virtual status_t setParameters(const String8& keyValuePairs) EXCLUDES_ThreadBase_Mutex = 0;
185     virtual String8 getParameters(const String8& keys) EXCLUDES_ThreadBase_Mutex = 0;
186     virtual void ioConfigChanged_l(
187             audio_io_config_event_t event, pid_t pid = 0,
188             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE)
189             /* holds either AF::mutex or TB::mutex */ = 0;
190 
191     // sendConfigEvent_l() must be called with ThreadBase::mLock held
192     // Can temporarily release the lock if waiting for a reply from
193     // processConfigEvents_l().
194     // status_t sendConfigEvent_l(sp<ConfigEvent>& event);
195     virtual void sendIoConfigEvent(
196             audio_io_config_event_t event, pid_t pid = 0,
197             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) EXCLUDES_ThreadBase_Mutex = 0;
198     virtual void sendIoConfigEvent_l(
199             audio_io_config_event_t event, pid_t pid = 0,
200             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) REQUIRES(mutex()) = 0;
201     virtual void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp)
202             EXCLUDES_ThreadBase_Mutex = 0;
203     virtual void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp)
204             REQUIRES(mutex()) = 0;
205     virtual status_t sendSetParameterConfigEvent_l(const String8& keyValuePair)
206             REQUIRES(mutex()) = 0;
207     virtual status_t sendCreateAudioPatchConfigEvent(
208             const struct audio_patch* patch, audio_patch_handle_t* handle)
209             EXCLUDES_ThreadBase_Mutex = 0;
210     virtual status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle)
211             EXCLUDES_ThreadBase_Mutex = 0;
212     virtual status_t sendUpdateOutDeviceConfigEvent(
213             const DeviceDescriptorBaseVector& outDevices) EXCLUDES_ThreadBase_Mutex = 0;
214     virtual void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs)
215             REQUIRES(mutex()) = 0;
216     virtual void sendCheckOutputStageEffectsEvent() EXCLUDES_ThreadBase_Mutex = 0;
217     virtual void sendCheckOutputStageEffectsEvent_l()
218             REQUIRES(mutex()) = 0;
219     virtual void sendHalLatencyModesChangedEvent_l()
220             REQUIRES(mutex()) = 0;
221 
222     virtual void processConfigEvents_l()
223             REQUIRES(mutex()) = 0;
224     virtual void setCheckOutputStageEffects() = 0;  // no mutex needed
225     virtual void cacheParameters_l()
226             REQUIRES(mutex()) = 0;
227     virtual status_t createAudioPatch_l(
228             const struct audio_patch* patch, audio_patch_handle_t* handle)
229             REQUIRES(mutex()) = 0;
230     virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle)
231             REQUIRES(mutex()) = 0;
232     virtual void updateOutDevices(const DeviceDescriptorBaseVector& outDevices)
233             EXCLUDES_ThreadBase_Mutex = 0;
234     virtual void toAudioPortConfig(struct audio_port_config* config)
235             EXCLUDES_ThreadBase_Mutex = 0;
236     virtual void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs)
237             REQUIRES(mutex()) = 0;
238 
239     // see note at declaration of mStandby, mOutDevice and mInDevice
240     virtual bool inStandby() const = 0;
241     virtual const DeviceTypeSet outDeviceTypes_l() const REQUIRES(mutex()) = 0;
242     virtual audio_devices_t inDeviceType_l() const REQUIRES(mutex()) = 0;
243     virtual DeviceTypeSet getDeviceTypes_l() const REQUIRES(mutex()) = 0;
244     virtual const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const = 0;
245     virtual const AudioDeviceTypeAddr& inDeviceTypeAddr() const = 0;
246     virtual bool isOutput() const = 0;
247     virtual bool isOffloadOrMmap() const = 0;
248     virtual sp<StreamHalInterface> stream() const = 0;
249     virtual sp<IAfEffectHandle> createEffect_l(
250             const sp<Client>& client,
251             const sp<media::IEffectClient>& effectClient,
252             int32_t priority,
253             audio_session_t sessionId,
254             effect_descriptor_t* desc,
255             int* enabled,
256             status_t* status /*non-NULL*/,
257             bool pinned,
258             bool probe,
259             bool notifyFramesProcessed)
260             REQUIRES(audio_utils::AudioFlinger_Mutex) EXCLUDES_ThreadBase_Mutex = 0;
261 
262     // return values for hasAudioSession (bit field)
263     enum effect_state {
264         EFFECT_SESSION = 0x1,       // the audio session corresponds to at least one
265                                     // effect
266         TRACK_SESSION = 0x2,        // the audio session corresponds to at least one
267                                     // track
268         FAST_SESSION = 0x4,         // the audio session corresponds to at least one
269                                     // fast track
270         SPATIALIZED_SESSION = 0x8,  // the audio session corresponds to at least one
271                                     // spatialized track
272         BIT_PERFECT_SESSION = 0x10  // the audio session corresponds to at least one
273                                     // bit-perfect track
274     };
275 
276     // get effect chain corresponding to session Id.
277     virtual sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const
278             EXCLUDES_ThreadBase_Mutex = 0;
279     // same as getEffectChain() but must be called with ThreadBase mutex locked
280     virtual sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const
281             REQUIRES(mutex()) = 0;
282     virtual std::vector<int> getEffectIds_l(audio_session_t sessionId) const
283             REQUIRES(mutex()) = 0;
284     // add an effect chain to the chain list (mEffectChains)
285     virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain)
286             REQUIRES(mutex()) = 0;
287     // remove an effect chain from the chain list (mEffectChains)
288     virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain)
289             REQUIRES(mutex()) = 0;
290     // lock all effect chains Mutexes. Must be called before releasing the
291     // ThreadBase mutex before processing the mixer and effects. This guarantees the
292     // integrity of the chains during the process.
293     // Also sets the parameter 'effectChains' to current value of mEffectChains.
294     virtual void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains)
295             REQUIRES(mutex()) EXCLUDES_EffectChain_Mutex = 0;
296     // unlock effect chains after process
297     virtual void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains)
298             EXCLUDES_ThreadBase_Mutex = 0;
299     // get a copy of mEffectChains vector
300     virtual Vector<sp<IAfEffectChain>> getEffectChains_l() const
301             REQUIRES(mutex()) = 0;
302     // set audio mode to all effect chains
303     virtual void setMode(audio_mode_t mode)
304             EXCLUDES_ThreadBase_Mutex = 0;
305     // get effect module with corresponding ID on specified audio session
306     virtual sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const
307             EXCLUDES_ThreadBase_Mutex = 0;
308     virtual sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const
309             REQUIRES(mutex()) = 0;
310     // add and effect module. Also creates the effect chain is none exists for
311     // the effects audio session. Only called in a context of moving an effect
312     // from one thread to another
313     virtual status_t addEffect_ll(const sp<IAfEffectModule>& effect)
314             REQUIRES(audio_utils::AudioFlinger_Mutex, mutex()) = 0;
315     // remove and effect module. Also removes the effect chain is this was the last
316     // effect
317     virtual void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false)
318             REQUIRES(mutex()) = 0;
319     // disconnect an effect handle from module and destroy module if last handle
320     virtual void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast)
321             EXCLUDES_ThreadBase_Mutex = 0;
322     // detach all tracks connected to an auxiliary effect
323     virtual void detachAuxEffect_l(int effectId) REQUIRES(mutex()) = 0;
324     // returns a combination of:
325     // - EFFECT_SESSION if effects on this audio session exist in one chain
326     // - TRACK_SESSION if tracks on this audio session exist
327     // - FAST_SESSION if fast tracks on this audio session exist
328     // - SPATIALIZED_SESSION if spatialized tracks on this audio session exist
329     virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const REQUIRES(mutex()) = 0;
330     virtual uint32_t hasAudioSession(audio_session_t sessionId) const
331             EXCLUDES_ThreadBase_Mutex = 0;
332 
333     // the value returned by default implementation is not important as the
334     // strategy is only meaningful for PlaybackThread which implements this method
335     virtual product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const
336             REQUIRES(mutex()) = 0;
337 
338     // check if some effects must be suspended/restored when an effect is enabled
339     // or disabled
340     virtual void checkSuspendOnEffectEnabled(
341             bool enabled, audio_session_t sessionId, bool threadLocked)
342             EXCLUDES_ThreadBase_Mutex = 0;
343 
344     virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event)
345             EXCLUDES_ThreadBase_Mutex = 0;
346     // internally static, perhaps make static member.
347     virtual bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const = 0;
348 
349     // Return a reference to a per-thread heap which can be used to allocate IMemory
350     // objects that will be read-only to client processes, read/write to mediaserver,
351     // and shared by all client processes of the thread.
352     // The heap is per-thread rather than common across all threads, because
353     // clients can't be trusted not to modify the offset of the IMemory they receive.
354     // If a thread does not have such a heap, this method returns 0.
355     virtual sp<MemoryDealer> readOnlyHeap() const = 0;
356 
357     virtual sp<IMemory> pipeMemory() const = 0;
358 
359     virtual void systemReady() EXCLUDES_ThreadBase_Mutex = 0;
360 
361     // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
362     virtual status_t checkEffectCompatibility_l(
363             const effect_descriptor_t* desc, audio_session_t sessionId) REQUIRES(mutex()) = 0;
364 
365     virtual void broadcast_l() REQUIRES(mutex()) = 0;
366 
367     virtual bool isTimestampCorrectionEnabled_l() const REQUIRES(mutex()) = 0;
368 
369     virtual bool isMsdDevice() const = 0;
370 
371     virtual void dump(int fd, const Vector<String16>& args) EXCLUDES_ThreadBase_Mutex = 0;
372 
373     // deliver stats to mediametrics.
374     virtual void sendStatistics(bool force) EXCLUDES_ThreadBase_Mutex = 0;
375 
376     virtual audio_utils::mutex& mutex() const
377             RETURN_CAPABILITY(audio_utils::ThreadBase_Mutex) = 0;
378 
379     virtual void onEffectEnable(const sp<IAfEffectModule>& effect) EXCLUDES_ThreadBase_Mutex = 0;
380     virtual void onEffectDisable() EXCLUDES_ThreadBase_Mutex = 0;
381 
382     // invalidateTracksForAudioSession_l must be called with holding mLock.
383     virtual void invalidateTracksForAudioSession_l(audio_session_t sessionId) const
384             REQUIRES(mutex()) = 0;
385     // Invalidate all the tracks with the given audio session.
386     virtual void invalidateTracksForAudioSession(audio_session_t sessionId) const
387             EXCLUDES_ThreadBase_Mutex = 0;
388 
389     virtual bool isStreamInitialized() const = 0;
390     virtual void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor)
391             REQUIRES(audio_utils::AudioFlinger_Mutex) = 0;
392     virtual void stopMelComputation_l()
393             REQUIRES(audio_utils::AudioFlinger_Mutex) = 0;
394 
395     virtual product_strategy_t getStrategyForStream(audio_stream_type_t stream) const
396             EXCLUDES_AUDIO_ALL = 0;
397 
398     virtual void setEffectSuspended_l(
399             const effect_uuid_t* type, bool suspend, audio_session_t sessionId)
400             REQUIRES(mutex()) = 0;
401 
402     // Wait while the Thread is busy.  This is done to ensure that
403     // the Thread is not busy releasing the Tracks, during which the Thread mutex
404     // may be temporarily unlocked.  Some Track methods will use this method to
405     // avoid races.
406     virtual void waitWhileThreadBusy_l(audio_utils::unique_lock<audio_utils::mutex>& ul)
407             REQUIRES(mutex()) = 0;
408 
409     // The ThreadloopExecutor is used to defer functors or dtors
410     // to when the Threadloop does not hold any mutexes (at the end of the
411     // processing period cycle).
412     virtual audio_utils::DeferredExecutor& getThreadloopExecutor() = 0;
413 
414     // Dynamic cast to derived interface
asIAfDirectOutputThread()415     virtual sp<IAfDirectOutputThread> asIAfDirectOutputThread() { return nullptr; }
asIAfDuplicatingThread()416     virtual sp<IAfDuplicatingThread> asIAfDuplicatingThread() { return nullptr; }
asIAfPlaybackThread()417     virtual sp<IAfPlaybackThread> asIAfPlaybackThread() { return nullptr; }
asIAfRecordThread()418     virtual sp<IAfRecordThread> asIAfRecordThread() { return nullptr; }
419     virtual IAfThreadCallback* afThreadCallback() const = 0;
420 };
421 
422 class IAfPlaybackThread : public virtual IAfThreadBase, public virtual VolumeInterface {
423 public:
424     static sp<IAfPlaybackThread> createBitPerfectThread(
425             const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
426             audio_io_handle_t id, bool systemReady);
427 
428     static sp<IAfPlaybackThread> createDirectOutputThread(
429             const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
430             audio_io_handle_t id, bool systemReady, const audio_offload_info_t& offloadInfo);
431 
432     static sp<IAfPlaybackThread> createMixerThread(
433             const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
434             audio_io_handle_t id, bool systemReady, type_t type = MIXER,
435             audio_config_base_t* mixerConfig = nullptr);
436 
437     static sp<IAfPlaybackThread> createOffloadThread(
438             const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
439             audio_io_handle_t id, bool systemReady, const audio_offload_info_t& offloadInfo);
440 
441     static sp<IAfPlaybackThread> createSpatializerThread(
442             const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
443             audio_io_handle_t id, bool systemReady, audio_config_base_t* mixerConfig);
444 
445     static constexpr int8_t kMaxTrackStopRetriesOffload = 2;
446 
447     enum mixer_state {
448         MIXER_IDLE,            // no active tracks
449         MIXER_TRACKS_ENABLED,  // at least one active track, but no track has any data ready
450         MIXER_TRACKS_READY,    // at least one active track, and at least one track has data
451         MIXER_DRAIN_TRACK,     // drain currently playing track
452         MIXER_DRAIN_ALL,       // fully drain the hardware
453         // standby mode does not have an enum value
454         // suspend by audio policy manager is orthogonal to mixer state
455     };
456 
457     // return estimated latency in milliseconds, as reported by HAL
458     virtual uint32_t latency() const = 0;  // should be in IAfThreadBase?
459 
460     virtual uint32_t& fastTrackAvailMask_l() REQUIRES(mutex()) = 0;
461 
462     virtual sp<IAfTrack> createTrack_l(
463             const sp<Client>& client,
464             audio_stream_type_t streamType,
465             const audio_attributes_t& attr,
466             uint32_t* sampleRate,
467             audio_format_t format,
468             audio_channel_mask_t channelMask,
469             size_t* pFrameCount,
470             size_t* pNotificationFrameCount,
471             uint32_t notificationsPerBuffer,
472             float speed,
473             const sp<IMemory>& sharedBuffer,
474             audio_session_t sessionId,
475             audio_output_flags_t* flags,
476             pid_t creatorPid,
477             const AttributionSourceState& attributionSource,
478             pid_t tid,
479             status_t* status /*non-NULL*/,
480             audio_port_handle_t portId,
481             const sp<media::IAudioTrackCallback>& callback,
482             bool isSpatialized,
483             bool isBitPerfect,
484             audio_output_flags_t* afTrackFlags,
485             float volume,
486             bool muted)
487             REQUIRES(audio_utils::AudioFlinger_Mutex) = 0;
488 
489     virtual status_t addTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()) = 0;
490     virtual bool destroyTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()) = 0;
491     virtual bool isTrackActive(const sp<IAfTrack>& track) const REQUIRES(mutex()) = 0;
492     virtual void addOutputTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()) = 0;
493 
494     virtual AudioStreamOut* getOutput_l() const REQUIRES(mutex()) = 0;
495     virtual AudioStreamOut* getOutput() const EXCLUDES_ThreadBase_Mutex = 0;
496     virtual AudioStreamOut* clearOutput() EXCLUDES_ThreadBase_Mutex = 0;
497 
498     // a very large number of suspend() will eventually wraparound, but unlikely
499     virtual void suspend() = 0;
500     virtual void restore() = 0;
501     virtual bool isSuspended() const = 0;
502     virtual status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const
503             EXCLUDES_ThreadBase_Mutex = 0;
504     // Consider also removing and passing an explicit mMainBuffer initialization
505     // parameter to AF::IAfTrack::Track().
506     virtual float* sinkBuffer() const = 0;
507 
508     virtual status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId)
509             EXCLUDES_ThreadBase_Mutex = 0;
510     virtual status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId)
511             REQUIRES(mutex()) = 0;
512 
513     // called with AudioFlinger lock held
514     virtual bool invalidateTracks_l(audio_stream_type_t streamType) REQUIRES(mutex()) = 0;
515     virtual bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) REQUIRES(mutex()) = 0;
516     virtual void invalidateTracks(audio_stream_type_t streamType)
517             EXCLUDES_ThreadBase_Mutex = 0;
518     // Invalidate tracks by a set of port ids. The port id will be removed from
519     // the given set if the corresponding track is found and invalidated.
520     virtual void invalidateTracks(std::set<audio_port_handle_t>& portIds)
521             EXCLUDES_ThreadBase_Mutex = 0;
522 
523     virtual status_t getTimestamp_l(AudioTimestamp& timestamp) REQUIRES(mutex()) = 0;
524     virtual void addPatchTrack(const sp<IAfPatchTrack>& track) EXCLUDES_ThreadBase_Mutex = 0;
525     virtual void deletePatchTrack(const sp<IAfPatchTrack>& track) EXCLUDES_ThreadBase_Mutex = 0;
526 
527     // Return the asynchronous signal wait time.
528     virtual int64_t computeWaitTimeNs_l() const REQUIRES(mutex()) = 0;
529     // returns true if the track is allowed to be added to the thread.
530     virtual bool isTrackAllowed_l(
531             audio_channel_mask_t channelMask, audio_format_t format, audio_session_t sessionId,
532             uid_t uid) const REQUIRES(mutex()) = 0;
533 
534     virtual bool supportsHapticPlayback() const = 0;
535 
536     virtual void setDownStreamPatch(const struct audio_patch* patch)
537             EXCLUDES_ThreadBase_Mutex = 0;
538 
539     virtual IAfTrack* getTrackById_l(audio_port_handle_t trackId) REQUIRES(mutex()) = 0;
540 
541     virtual bool hasMixer() const = 0;
542 
543     virtual status_t setRequestedLatencyMode(audio_latency_mode_t mode) = 0;
544 
545     virtual status_t getSupportedLatencyModes(std::vector<audio_latency_mode_t>* modes)
546            EXCLUDES_ThreadBase_Mutex = 0;
547 
548     virtual status_t setBluetoothVariableLatencyEnabled(bool enabled) = 0;
549 
550     virtual void setStandby() EXCLUDES_ThreadBase_Mutex = 0;
551     virtual void setStandby_l() REQUIRES(mutex()) = 0;
552     virtual bool waitForHalStart() EXCLUDES_ThreadBase_Mutex = 0;
553 
554     virtual bool hasFastMixer() const = 0;
555     virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const = 0;
556     virtual const std::atomic<int64_t>& framesWritten() const = 0;
557 
558     virtual bool usesHwAvSync() const = 0;
559 
560     virtual void setTracksInternalMute(std::map<audio_port_handle_t, bool>* tracksInternalMute)
561             EXCLUDES_ThreadBase_Mutex = 0;
562 
563     virtual status_t setPortsVolume(const std::vector<audio_port_handle_t> &portIds, float volume,
564                                     bool muted) EXCLUDES_ThreadBase_Mutex = 0;
565 };
566 
567 class IAfDirectOutputThread : public virtual IAfPlaybackThread {
568 public:
569     virtual status_t selectPresentation(int presentationId, int programId) = 0;
570 };
571 
572 class IAfDuplicatingThread : public virtual IAfPlaybackThread {
573 public:
574     static sp<IAfDuplicatingThread> create(
575             const sp<IAfThreadCallback>& afThreadCallback, IAfPlaybackThread* mainThread,
576             audio_io_handle_t id, bool systemReady);
577 
578     virtual void addOutputTrack(IAfPlaybackThread* thread) EXCLUDES_ThreadBase_Mutex = 0;
579     virtual uint32_t waitTimeMs() const = 0;
580     virtual void removeOutputTrack(IAfPlaybackThread* thread) EXCLUDES_ThreadBase_Mutex = 0;
581 };
582 
583 class IAfRecordThread : public virtual IAfThreadBase {
584 public:
585     static sp<IAfRecordThread> create(
586             const sp<IAfThreadCallback>& afThreadCallback, AudioStreamIn* input,
587             audio_io_handle_t id, bool systemReady);
588 
589     virtual sp<IAfRecordTrack> createRecordTrack_l(
590             const sp<Client>& client,
591             const audio_attributes_t& attr,
592             uint32_t* pSampleRate,
593             audio_format_t format,
594             audio_channel_mask_t channelMask,
595             size_t* pFrameCount,
596             audio_session_t sessionId,
597             size_t* pNotificationFrameCount,
598             pid_t creatorPid,
599             const AttributionSourceState& attributionSource,
600             audio_input_flags_t* flags,
601             pid_t tid,
602             status_t* status /*non-NULL*/,
603             audio_port_handle_t portId,
604             int32_t maxSharedAudioHistoryMs)
605             REQUIRES(audio_utils::AudioFlinger_Mutex) EXCLUDES_ThreadBase_Mutex = 0;
606     virtual void destroyTrack_l(const sp<IAfRecordTrack>& track) REQUIRES(mutex()) = 0;
607     virtual void removeTrack_l(const sp<IAfRecordTrack>& track) REQUIRES(mutex()) = 0;
608 
609     virtual status_t start(
610             IAfRecordTrack* recordTrack, AudioSystem::sync_event_t event,
611             audio_session_t triggerSession) EXCLUDES_ThreadBase_Mutex = 0;
612 
613     // ask the thread to stop the specified track, and
614     // return true if the caller should then do it's part of the stopping process
615     virtual bool stop(IAfRecordTrack* recordTrack) EXCLUDES_ThreadBase_Mutex = 0;
616 
617     // NO_THREAD_SAFETY_ANALYSIS: consider atomics
618     virtual AudioStreamIn* getInput() const = 0;
619     virtual AudioStreamIn* clearInput() = 0;
620 
621     virtual status_t getActiveMicrophones(
622             std::vector<media::MicrophoneInfoFw>* activeMicrophones)
623             const EXCLUDES_ThreadBase_Mutex = 0;
624     virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction)
625             EXCLUDES_ThreadBase_Mutex = 0;
626     virtual status_t setPreferredMicrophoneFieldDimension(float zoom)
627             EXCLUDES_ThreadBase_Mutex = 0;
628 
629     virtual void addPatchTrack(const sp<IAfPatchRecord>& record)
630             EXCLUDES_ThreadBase_Mutex = 0;
631     virtual void deletePatchTrack(const sp<IAfPatchRecord>& record)
632             EXCLUDES_ThreadBase_Mutex = 0;
633     virtual bool fastTrackAvailable() const = 0;
634     virtual void setFastTrackAvailable(bool available) = 0;
635 
636     virtual void setRecordSilenced(audio_port_handle_t portId, bool silenced)
637             EXCLUDES_ThreadBase_Mutex = 0;
638     virtual bool hasFastCapture() const = 0;
639 
640     virtual void checkBtNrec() EXCLUDES_ThreadBase_Mutex = 0;
641     virtual uint32_t getInputFramesLost() const EXCLUDES_ThreadBase_Mutex = 0;
642 
643     virtual status_t shareAudioHistory(
644             const std::string& sharedAudioPackageName,
645             audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
646             int64_t sharedAudioStartMs = -1) EXCLUDES_ThreadBase_Mutex = 0;
647     virtual void resetAudioHistory_l() REQUIRES(mutex()) = 0;
648 };
649 
650 class IAfMmapThread : public virtual IAfThreadBase {
651 public:
652     // createIAudioTrackAdapter() is a static constructor which creates an
653     // MmapStreamInterface AIDL interface adapter from the MmapThread object that
654     // may be passed back to the client.
655     //
656     // Only one AIDL MmapStreamInterface interface adapter should be created per MmapThread.
657     static sp<MmapStreamInterface> createMmapStreamInterfaceAdapter(
658             const sp<IAfMmapThread>& mmapThread);
659 
660     virtual void configure(
661             const audio_attributes_t* attr,
662             audio_stream_type_t streamType,
663             audio_session_t sessionId,
664             const sp<MmapStreamCallback>& callback,
665             const DeviceIdVector& deviceIds,
666             audio_port_handle_t portId) EXCLUDES_ThreadBase_Mutex = 0;
667     virtual void disconnect() EXCLUDES_ThreadBase_Mutex = 0;
668 
669     // MmapStreamInterface handling (see adapter)
670     virtual status_t createMmapBuffer(
671             int32_t minSizeFrames, struct audio_mmap_buffer_info* info)
672             EXCLUDES_ThreadBase_Mutex = 0;
673     virtual status_t getMmapPosition(struct audio_mmap_position* position) const
674             EXCLUDES_ThreadBase_Mutex = 0;
675     virtual status_t start(
676             const AudioClient& client, const audio_attributes_t* attr,
677             audio_port_handle_t* handle) EXCLUDES_ThreadBase_Mutex = 0;
678     virtual status_t stop(audio_port_handle_t handle) EXCLUDES_ThreadBase_Mutex = 0;
679     virtual status_t standby() EXCLUDES_ThreadBase_Mutex = 0;
680     virtual status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const
681             EXCLUDES_ThreadBase_Mutex = 0;
682     virtual status_t reportData(const void* buffer, size_t frameCount)
683             EXCLUDES_ThreadBase_Mutex = 0;
684 
685     // TODO(b/291317898)  move to IAfThreadBase?
686     virtual void invalidateTracks(std::set<audio_port_handle_t>& portIds)
687             EXCLUDES_ThreadBase_Mutex = 0;
688 
689     // Sets the UID records silence - TODO(b/291317898)  move to IAfMmapCaptureThread
690     virtual void setRecordSilenced(audio_port_handle_t portId, bool silenced)
691             EXCLUDES_ThreadBase_Mutex = 0;
692 
asIAfMmapPlaybackThread()693     virtual sp<IAfMmapPlaybackThread> asIAfMmapPlaybackThread() { return nullptr; }
asIAfMmapCaptureThread()694     virtual sp<IAfMmapCaptureThread> asIAfMmapCaptureThread() { return nullptr; }
695 };
696 
697 class IAfMmapPlaybackThread : public virtual IAfMmapThread, public virtual VolumeInterface {
698 public:
699     static sp<IAfMmapPlaybackThread> create(
700             const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
701             AudioHwDevice* hwDev, AudioStreamOut* output, bool systemReady);
702 
703     virtual AudioStreamOut* clearOutput() EXCLUDES_ThreadBase_Mutex = 0;
704 
705     virtual status_t setPortsVolume(const std::vector<audio_port_handle_t>& portIds, float volume,
706                                     bool muted) EXCLUDES_ThreadBase_Mutex = 0;
707 };
708 
709 class IAfMmapCaptureThread : public virtual IAfMmapThread {
710 public:
711     static sp<IAfMmapCaptureThread> create(
712             const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
713             AudioHwDevice* hwDev, AudioStreamIn* input, bool systemReady);
714 
715     virtual AudioStreamIn* clearInput() EXCLUDES_ThreadBase_Mutex = 0;
716 };
717 
718 }  // namespace android
719