blob: e8f23490bead1aee3d283a8419c438fc61ee990d [file] [log] [blame]
Andy Hung4989d312023-06-29 21:19:25 -07001/*
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 "IAfTrack.h"
20
21namespace android {
22
Andy Hung44f27182023-07-06 20:56:16 -070023class IAfDirectOutputThread;
24class IAfDuplicatingThread;
25class IAfPlaybackThread;
26class IAfRecordThread;
27
Andy Hung4989d312023-06-29 21:19:25 -070028class IAfThreadBase : public virtual RefBase {
29public:
30 enum type_t {
31 MIXER, // Thread class is MixerThread
32 DIRECT, // Thread class is DirectOutputThread
33 DUPLICATING, // Thread class is DuplicatingThread
34 RECORD, // Thread class is RecordThread
35 OFFLOAD, // Thread class is OffloadThread
36 MMAP_PLAYBACK, // Thread class for MMAP playback stream
37 MMAP_CAPTURE, // Thread class for MMAP capture stream
38 SPATIALIZER, //
39 BIT_PERFECT, // Thread class for BitPerfectThread
40 // When adding a value, also update IAfThreadBase::threadTypeToString()
41 };
42
43 static const char* threadTypeToString(type_t type);
44 virtual status_t readyToRun() = 0;
45 virtual void clearPowerManager() = 0;
46 virtual status_t initCheck() const = 0;
47 virtual type_t type() const = 0;
48 virtual bool isDuplicating() const = 0;
49 virtual audio_io_handle_t id() const = 0;
50 virtual uint32_t sampleRate() const = 0;
51 virtual audio_channel_mask_t channelMask() const = 0;
52 virtual audio_channel_mask_t mixerChannelMask() const = 0;
53 virtual audio_format_t format() const = 0;
54 virtual uint32_t channelCount() const = 0;
55
56 // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
57 // and returns the [normal mix] buffer's frame count.
58 virtual size_t frameCount() const = 0;
59 virtual audio_channel_mask_t hapticChannelMask() const = 0;
Andy Hung44f27182023-07-06 20:56:16 -070060 virtual uint32_t hapticChannelCount() const = 0;
Andy Hung4989d312023-06-29 21:19:25 -070061 virtual uint32_t latency_l() const = 0;
62 virtual void setVolumeForOutput_l(float left, float right) const = 0;
63
64 // Return's the HAL's frame count i.e. fast mixer buffer size.
65 virtual size_t frameCountHAL() const = 0;
66 virtual size_t frameSize() const = 0;
67 // Should be "virtual status_t requestExitAndWait()" and override same
68 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
69 virtual void exit() = 0;
70 virtual bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) = 0;
71 virtual status_t setParameters(const String8& keyValuePairs) = 0;
72 virtual String8 getParameters(const String8& keys) = 0;
73 virtual void ioConfigChanged(
74 audio_io_config_event_t event, pid_t pid = 0,
75 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
76
77 // sendConfigEvent_l() must be called with ThreadBase::mLock held
78 // Can temporarily release the lock if waiting for a reply from
79 // processConfigEvents_l().
80 // status_t sendConfigEvent_l(sp<ConfigEvent>& event);
81 virtual void sendIoConfigEvent(
82 audio_io_config_event_t event, pid_t pid = 0,
83 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
84 virtual void sendIoConfigEvent_l(
85 audio_io_config_event_t event, pid_t pid = 0,
86 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
87 virtual void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) = 0;
88 virtual void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp) = 0;
89 virtual status_t sendSetParameterConfigEvent_l(const String8& keyValuePair) = 0;
90 virtual status_t sendCreateAudioPatchConfigEvent(
91 const struct audio_patch* patch, audio_patch_handle_t* handle) = 0;
92 virtual status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle) = 0;
93 virtual status_t sendUpdateOutDeviceConfigEvent(
94 const DeviceDescriptorBaseVector& outDevices) = 0;
95 virtual void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) = 0;
96 virtual void sendCheckOutputStageEffectsEvent() = 0;
97 virtual void sendCheckOutputStageEffectsEvent_l() = 0;
98 virtual void sendHalLatencyModesChangedEvent_l() = 0;
99
100 virtual void processConfigEvents_l() = 0;
101 virtual void setCheckOutputStageEffects() = 0;
102 virtual void cacheParameters_l() = 0;
103 virtual status_t createAudioPatch_l(
104 const struct audio_patch* patch, audio_patch_handle_t* handle) = 0;
105 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
106 virtual void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) = 0;
107 virtual void toAudioPortConfig(struct audio_port_config* config) = 0;
108 virtual void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) = 0;
109
110 // see note at declaration of mStandby, mOutDevice and mInDevice
111 virtual bool inStandby() const = 0;
112 virtual const DeviceTypeSet outDeviceTypes() const = 0;
113 virtual audio_devices_t inDeviceType() const = 0;
114 virtual DeviceTypeSet getDeviceTypes() const = 0;
115 virtual const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const = 0;
116 virtual const AudioDeviceTypeAddr& inDeviceTypeAddr() const = 0;
117 virtual bool isOutput() const = 0;
118 virtual bool isOffloadOrMmap() const = 0;
119 virtual sp<StreamHalInterface> stream() const = 0;
120 virtual sp<IAfEffectHandle> createEffect_l(
121 const sp<Client>& client,
122 const sp<media::IEffectClient>& effectClient,
123 int32_t priority,
124 audio_session_t sessionId,
125 effect_descriptor_t* desc,
126 int* enabled,
127 status_t* status /*non-NULL*/,
128 bool pinned,
129 bool probe,
130 bool notifyFramesProcessed) = 0;
131
132 // return values for hasAudioSession (bit field)
133 enum effect_state {
134 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
135 // effect
136 TRACK_SESSION = 0x2, // the audio session corresponds to at least one
137 // track
138 FAST_SESSION = 0x4, // the audio session corresponds to at least one
139 // fast track
140 SPATIALIZED_SESSION = 0x8, // the audio session corresponds to at least one
141 // spatialized track
142 BIT_PERFECT_SESSION = 0x10 // the audio session corresponds to at least one
143 // bit-perfect track
144 };
145
146 // get effect chain corresponding to session Id.
147 virtual sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const = 0;
148 // same as getEffectChain() but must be called with ThreadBase mutex locked
149 virtual sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const = 0;
150 virtual std::vector<int> getEffectIds_l(audio_session_t sessionId) const = 0;
151 // add an effect chain to the chain list (mEffectChains)
152 virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain) = 0;
153 // remove an effect chain from the chain list (mEffectChains)
154 virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) = 0;
155 // lock all effect chains Mutexes. Must be called before releasing the
156 // ThreadBase mutex before processing the mixer and effects. This guarantees the
157 // integrity of the chains during the process.
158 // Also sets the parameter 'effectChains' to current value of mEffectChains.
159 virtual void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) = 0;
160 // unlock effect chains after process
161 virtual void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) = 0;
162 // get a copy of mEffectChains vector
163 virtual Vector<sp<IAfEffectChain>> getEffectChains_l() const = 0;
164 // set audio mode to all effect chains
165 virtual void setMode(audio_mode_t mode) = 0;
166 // get effect module with corresponding ID on specified audio session
167 virtual sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const = 0;
168 virtual sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const = 0;
169 // add and effect module. Also creates the effect chain is none exists for
170 // the effects audio session. Only called in a context of moving an effect
171 // from one thread to another
172 virtual status_t addEffect_l(const sp<IAfEffectModule>& effect) = 0;
173 // remove and effect module. Also removes the effect chain is this was the last
174 // effect
175 virtual void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false) = 0;
176 // disconnect an effect handle from module and destroy module if last handle
177 virtual void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast) = 0;
178 // detach all tracks connected to an auxiliary effect
179 virtual void detachAuxEffect_l(int effectId) = 0;
180 // returns a combination of:
181 // - EFFECT_SESSION if effects on this audio session exist in one chain
182 // - TRACK_SESSION if tracks on this audio session exist
183 // - FAST_SESSION if fast tracks on this audio session exist
184 // - SPATIALIZED_SESSION if spatialized tracks on this audio session exist
185 virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
186 virtual uint32_t hasAudioSession(audio_session_t sessionId) const = 0;
187
188 // the value returned by default implementation is not important as the
189 // strategy is only meaningful for PlaybackThread which implements this method
190 virtual product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const = 0;
191
192 // check if some effects must be suspended/restored when an effect is enabled
193 // or disabled
194 virtual void checkSuspendOnEffectEnabled(
195 bool enabled, audio_session_t sessionId, bool threadLocked) = 0;
196
197 virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) = 0;
198 virtual bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const = 0;
199
200 // Return a reference to a per-thread heap which can be used to allocate IMemory
201 // objects that will be read-only to client processes, read/write to mediaserver,
202 // and shared by all client processes of the thread.
203 // The heap is per-thread rather than common across all threads, because
204 // clients can't be trusted not to modify the offset of the IMemory they receive.
205 // If a thread does not have such a heap, this method returns 0.
206 virtual sp<MemoryDealer> readOnlyHeap() const = 0;
207
208 virtual sp<IMemory> pipeMemory() const = 0;
209
210 virtual void systemReady() = 0;
211
212 // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
213 virtual status_t checkEffectCompatibility_l(
214 const effect_descriptor_t* desc, audio_session_t sessionId) = 0;
215
216 virtual void broadcast_l() = 0;
217
218 virtual bool isTimestampCorrectionEnabled() const = 0;
219
220 virtual bool isMsdDevice() const = 0;
221
222 virtual void dump(int fd, const Vector<String16>& args) = 0;
223
224 // deliver stats to mediametrics.
225 virtual void sendStatistics(bool force) = 0;
226
227 virtual Mutex& mutex() const = 0;
228
229 virtual void onEffectEnable(const sp<IAfEffectModule>& effect) = 0;
230 virtual void onEffectDisable() = 0;
231
232 // invalidateTracksForAudioSession_l must be called with holding mLock.
233 virtual void invalidateTracksForAudioSession_l(audio_session_t sessionId) const = 0;
234 // Invalidate all the tracks with the given audio session.
235 virtual void invalidateTracksForAudioSession(audio_session_t sessionId) const = 0;
236
237 virtual bool isStreamInitialized() const = 0;
238 virtual void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) = 0;
239 virtual void stopMelComputation_l() = 0;
240
241 virtual product_strategy_t getStrategyForStream(audio_stream_type_t stream) const = 0;
Andy Hung44f27182023-07-06 20:56:16 -0700242
243 virtual void setEffectSuspended_l(
244 const effect_uuid_t* type, bool suspend, audio_session_t sessionId) = 0;
245
246 // Dynamic cast to derived interface
247 virtual sp<IAfDirectOutputThread> asIAfDirectOutputThread() { return nullptr; }
248 virtual sp<IAfDuplicatingThread> asIAfDuplicatingThread() { return nullptr; }
249 virtual sp<IAfPlaybackThread> asIAfPlaybackThread() { return nullptr; }
250 virtual sp<IAfRecordThread> asIAfRecordThread() { return nullptr; }
251 virtual AudioFlinger* audioFlinger() const = 0;
Andy Hung4989d312023-06-29 21:19:25 -0700252};
253
Andy Hung44f27182023-07-06 20:56:16 -0700254class IAfPlaybackThread : public virtual IAfThreadBase, public virtual VolumeInterface {
Andy Hung4989d312023-06-29 21:19:25 -0700255public:
Andy Hung44f27182023-07-06 20:56:16 -0700256 static constexpr int8_t kMaxTrackStopRetriesOffload = 2;
257
Andy Hung4989d312023-06-29 21:19:25 -0700258 enum mixer_state {
259 MIXER_IDLE, // no active tracks
260 MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready
261 MIXER_TRACKS_READY, // at least one active track, and at least one track has data
262 MIXER_DRAIN_TRACK, // drain currently playing track
263 MIXER_DRAIN_ALL, // fully drain the hardware
264 // standby mode does not have an enum value
265 // suspend by audio policy manager is orthogonal to mixer state
266 };
267
268 // return estimated latency in milliseconds, as reported by HAL
269 virtual uint32_t latency() const = 0; // should be in IAfThreadBase?
270
Andy Hung44f27182023-07-06 20:56:16 -0700271 virtual uint32_t& fastTrackAvailMask_l() = 0;
272
Andy Hung4989d312023-06-29 21:19:25 -0700273 virtual sp<IAfTrack> createTrack_l(
274 const sp<Client>& client,
275 audio_stream_type_t streamType,
276 const audio_attributes_t& attr,
277 uint32_t* sampleRate,
278 audio_format_t format,
279 audio_channel_mask_t channelMask,
280 size_t* pFrameCount,
281 size_t* pNotificationFrameCount,
282 uint32_t notificationsPerBuffer,
283 float speed,
284 const sp<IMemory>& sharedBuffer,
285 audio_session_t sessionId,
286 audio_output_flags_t* flags,
287 pid_t creatorPid,
288 const AttributionSourceState& attributionSource,
289 pid_t tid,
290 status_t* status /*non-NULL*/,
291 audio_port_handle_t portId,
292 const sp<media::IAudioTrackCallback>& callback,
293 bool isSpatialized,
294 bool isBitPerfect) = 0;
295
Andy Hung44f27182023-07-06 20:56:16 -0700296 virtual status_t addTrack_l(const sp<IAfTrack>& track) = 0;
297 virtual bool destroyTrack_l(const sp<IAfTrack>& track) = 0;
298 virtual bool isTrackActive(const sp<IAfTrack>& track) const = 0;
299 virtual void addOutputTrack_l(const sp<IAfTrack>& track) = 0;
300
301 virtual AudioStreamOut* getOutput_l() const = 0;
Andy Hung4989d312023-06-29 21:19:25 -0700302 virtual AudioStreamOut* getOutput() const = 0;
303 virtual AudioStreamOut* clearOutput() = 0;
Andy Hung4989d312023-06-29 21:19:25 -0700304
305 // a very large number of suspend() will eventually wraparound, but unlikely
306 virtual void suspend() = 0;
307 virtual void restore() = 0;
308 virtual bool isSuspended() const = 0;
309 virtual status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const = 0;
310 // Consider also removing and passing an explicit mMainBuffer initialization
311 // parameter to AF::IAfTrack::Track().
312 virtual float* sinkBuffer() const = 0;
313
314 virtual status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId) = 0;
315 virtual status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId) = 0;
316
317 // called with AudioFlinger lock held
318 virtual bool invalidateTracks_l(audio_stream_type_t streamType) = 0;
319 virtual bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) = 0;
320 virtual void invalidateTracks(audio_stream_type_t streamType) = 0;
321 // Invalidate tracks by a set of port ids. The port id will be removed from
322 // the given set if the corresponding track is found and invalidated.
323 virtual void invalidateTracks(std::set<audio_port_handle_t>& portIds) = 0;
324
325 virtual status_t getTimestamp_l(AudioTimestamp& timestamp) = 0;
326 virtual void addPatchTrack(const sp<IAfPatchTrack>& track) = 0;
327 virtual void deletePatchTrack(const sp<IAfPatchTrack>& track) = 0;
328
329 // Return the asynchronous signal wait time.
330 virtual int64_t computeWaitTimeNs_l() const = 0;
331 // returns true if the track is allowed to be added to the thread.
332 virtual bool isTrackAllowed_l(
333 audio_channel_mask_t channelMask, audio_format_t format, audio_session_t sessionId,
334 uid_t uid) const = 0;
335
336 virtual bool supportsHapticPlayback() const = 0;
337
338 virtual void setDownStreamPatch(const struct audio_patch* patch) = 0;
339
340 virtual IAfTrack* getTrackById_l(audio_port_handle_t trackId) = 0;
341
342 virtual bool hasMixer() const = 0;
343
344 virtual status_t setRequestedLatencyMode(audio_latency_mode_t mode) = 0;
345
346 virtual status_t getSupportedLatencyModes(std::vector<audio_latency_mode_t>* modes) = 0;
347
348 virtual status_t setBluetoothVariableLatencyEnabled(bool enabled) = 0;
349
350 virtual void setStandby() = 0;
351 virtual void setStandby_l() = 0;
352 virtual bool waitForHalStart() = 0;
353
354 virtual bool hasFastMixer() const = 0;
355 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const = 0;
356 virtual const std::atomic<int64_t>& framesWritten() const = 0;
Andy Hung44f27182023-07-06 20:56:16 -0700357
358 virtual bool usesHwAvSync() const = 0;
359};
360
361class IAfDirectOutputThread : public virtual IAfPlaybackThread {
362public:
363 virtual status_t selectPresentation(int presentationId, int programId) = 0;
364};
365
366class IAfDuplicatingThread : public virtual IAfPlaybackThread {
367public:
368 virtual void addOutputTrack(IAfPlaybackThread* thread) = 0;
369 virtual uint32_t waitTimeMs() const = 0;
370 virtual void removeOutputTrack(IAfPlaybackThread* thread) = 0;
371};
372
373class IAfRecordThread : public virtual IAfThreadBase {
374public:
375 static sp<IAfRecordThread> create(
376 const sp<AudioFlinger>& audioFlinger, AudioStreamIn* input, audio_io_handle_t id,
377 bool systemReady);
378
379 virtual sp<IAfRecordTrack> createRecordTrack_l(
380 const sp<Client>& client,
381 const audio_attributes_t& attr,
382 uint32_t* pSampleRate,
383 audio_format_t format,
384 audio_channel_mask_t channelMask,
385 size_t* pFrameCount,
386 audio_session_t sessionId,
387 size_t* pNotificationFrameCount,
388 pid_t creatorPid,
389 const AttributionSourceState& attributionSource,
390 audio_input_flags_t* flags,
391 pid_t tid,
392 status_t* status /*non-NULL*/,
393 audio_port_handle_t portId,
394 int32_t maxSharedAudioHistoryMs) = 0;
395 virtual void destroyTrack_l(const sp<IAfRecordTrack>& track) = 0;
396 virtual void removeTrack_l(const sp<IAfRecordTrack>& track) = 0;
397
398 virtual status_t start(
399 IAfRecordTrack* recordTrack, AudioSystem::sync_event_t event,
400 audio_session_t triggerSession) = 0;
401
402 // ask the thread to stop the specified track, and
403 // return true if the caller should then do it's part of the stopping process
404 virtual bool stop(IAfRecordTrack* recordTrack) = 0;
405
406 virtual AudioStreamIn* getInput() const = 0;
407 virtual AudioStreamIn* clearInput() = 0;
408
409 virtual status_t getActiveMicrophones(
410 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const = 0;
411 virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) = 0;
412 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
413
414 virtual void addPatchTrack(const sp<IAfPatchRecord>& record) = 0;
415 virtual void deletePatchTrack(const sp<IAfPatchRecord>& record) = 0;
416 virtual bool fastTrackAvailable() const = 0;
417 virtual void setFastTrackAvailable(bool available) = 0;
418
419 virtual void setRecordSilenced(audio_port_handle_t portId, bool silenced) = 0;
420 virtual bool hasFastCapture() const = 0;
421
422 virtual void checkBtNrec() = 0;
423 virtual uint32_t getInputFramesLost() const = 0;
424
425 virtual status_t shareAudioHistory(
426 const std::string& sharedAudioPackageName,
427 audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
428 int64_t sharedAudioStartMs = -1) = 0;
429 virtual void resetAudioHistory_l() = 0;
Andy Hung4989d312023-06-29 21:19:25 -0700430};
431
432} // namespace android