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