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