blob: 449ed909de86e731547f32e32eb3aa885e93a6e1 [file] [log] [blame]
Andy Hung440901d2023-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
23class IAfThreadBase : public virtual RefBase {
24public:
25 enum type_t {
26 MIXER, // Thread class is MixerThread
27 DIRECT, // Thread class is DirectOutputThread
28 DUPLICATING, // Thread class is DuplicatingThread
29 RECORD, // Thread class is RecordThread
30 OFFLOAD, // Thread class is OffloadThread
31 MMAP_PLAYBACK, // Thread class for MMAP playback stream
32 MMAP_CAPTURE, // Thread class for MMAP capture stream
33 SPATIALIZER, //
34 BIT_PERFECT, // Thread class for BitPerfectThread
35 // When adding a value, also update IAfThreadBase::threadTypeToString()
36 };
37
38 static const char* threadTypeToString(type_t type);
39 virtual status_t readyToRun() = 0;
40 virtual void clearPowerManager() = 0;
41 virtual status_t initCheck() const = 0;
42 virtual type_t type() const = 0;
43 virtual bool isDuplicating() const = 0;
44 virtual audio_io_handle_t id() const = 0;
45 virtual uint32_t sampleRate() const = 0;
46 virtual audio_channel_mask_t channelMask() const = 0;
47 virtual audio_channel_mask_t mixerChannelMask() const = 0;
48 virtual audio_format_t format() const = 0;
49 virtual uint32_t channelCount() const = 0;
50
51 // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
52 // and returns the [normal mix] buffer's frame count.
53 virtual size_t frameCount() const = 0;
54 virtual audio_channel_mask_t hapticChannelMask() const = 0;
55 virtual uint32_t latency_l() const = 0;
56 virtual void setVolumeForOutput_l(float left, float right) const = 0;
57
58 // Return's the HAL's frame count i.e. fast mixer buffer size.
59 virtual size_t frameCountHAL() const = 0;
60 virtual size_t frameSize() const = 0;
61 // Should be "virtual status_t requestExitAndWait()" and override same
62 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
63 virtual void exit() = 0;
64 virtual bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) = 0;
65 virtual status_t setParameters(const String8& keyValuePairs) = 0;
66 virtual String8 getParameters(const String8& keys) = 0;
67 virtual void ioConfigChanged(
68 audio_io_config_event_t event, pid_t pid = 0,
69 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
70
71 // sendConfigEvent_l() must be called with ThreadBase::mLock held
72 // Can temporarily release the lock if waiting for a reply from
73 // processConfigEvents_l().
74 // status_t sendConfigEvent_l(sp<ConfigEvent>& event);
75 virtual void sendIoConfigEvent(
76 audio_io_config_event_t event, pid_t pid = 0,
77 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
78 virtual void sendIoConfigEvent_l(
79 audio_io_config_event_t event, pid_t pid = 0,
80 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) = 0;
81 virtual void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) = 0;
82 virtual void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp) = 0;
83 virtual status_t sendSetParameterConfigEvent_l(const String8& keyValuePair) = 0;
84 virtual status_t sendCreateAudioPatchConfigEvent(
85 const struct audio_patch* patch, audio_patch_handle_t* handle) = 0;
86 virtual status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle) = 0;
87 virtual status_t sendUpdateOutDeviceConfigEvent(
88 const DeviceDescriptorBaseVector& outDevices) = 0;
89 virtual void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) = 0;
90 virtual void sendCheckOutputStageEffectsEvent() = 0;
91 virtual void sendCheckOutputStageEffectsEvent_l() = 0;
92 virtual void sendHalLatencyModesChangedEvent_l() = 0;
93
94 virtual void processConfigEvents_l() = 0;
95 virtual void setCheckOutputStageEffects() = 0;
96 virtual void cacheParameters_l() = 0;
97 virtual status_t createAudioPatch_l(
98 const struct audio_patch* patch, audio_patch_handle_t* handle) = 0;
99 virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
100 virtual void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) = 0;
101 virtual void toAudioPortConfig(struct audio_port_config* config) = 0;
102 virtual void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) = 0;
103
104 // see note at declaration of mStandby, mOutDevice and mInDevice
105 virtual bool inStandby() const = 0;
106 virtual const DeviceTypeSet outDeviceTypes() const = 0;
107 virtual audio_devices_t inDeviceType() const = 0;
108 virtual DeviceTypeSet getDeviceTypes() const = 0;
109 virtual const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const = 0;
110 virtual const AudioDeviceTypeAddr& inDeviceTypeAddr() const = 0;
111 virtual bool isOutput() const = 0;
112 virtual bool isOffloadOrMmap() const = 0;
113 virtual sp<StreamHalInterface> stream() const = 0;
114 virtual sp<IAfEffectHandle> createEffect_l(
115 const sp<Client>& client,
116 const sp<media::IEffectClient>& effectClient,
117 int32_t priority,
118 audio_session_t sessionId,
119 effect_descriptor_t* desc,
120 int* enabled,
121 status_t* status /*non-NULL*/,
122 bool pinned,
123 bool probe,
124 bool notifyFramesProcessed) = 0;
125
126 // return values for hasAudioSession (bit field)
127 enum effect_state {
128 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
129 // effect
130 TRACK_SESSION = 0x2, // the audio session corresponds to at least one
131 // track
132 FAST_SESSION = 0x4, // the audio session corresponds to at least one
133 // fast track
134 SPATIALIZED_SESSION = 0x8, // the audio session corresponds to at least one
135 // spatialized track
136 BIT_PERFECT_SESSION = 0x10 // the audio session corresponds to at least one
137 // bit-perfect track
138 };
139
140 // get effect chain corresponding to session Id.
141 virtual sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const = 0;
142 // same as getEffectChain() but must be called with ThreadBase mutex locked
143 virtual sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const = 0;
144 virtual std::vector<int> getEffectIds_l(audio_session_t sessionId) const = 0;
145 // add an effect chain to the chain list (mEffectChains)
146 virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain) = 0;
147 // remove an effect chain from the chain list (mEffectChains)
148 virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) = 0;
149 // lock all effect chains Mutexes. Must be called before releasing the
150 // ThreadBase mutex before processing the mixer and effects. This guarantees the
151 // integrity of the chains during the process.
152 // Also sets the parameter 'effectChains' to current value of mEffectChains.
153 virtual void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) = 0;
154 // unlock effect chains after process
155 virtual void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) = 0;
156 // get a copy of mEffectChains vector
157 virtual Vector<sp<IAfEffectChain>> getEffectChains_l() const = 0;
158 // set audio mode to all effect chains
159 virtual void setMode(audio_mode_t mode) = 0;
160 // get effect module with corresponding ID on specified audio session
161 virtual sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const = 0;
162 virtual sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const = 0;
163 // add and effect module. Also creates the effect chain is none exists for
164 // the effects audio session. Only called in a context of moving an effect
165 // from one thread to another
166 virtual status_t addEffect_l(const sp<IAfEffectModule>& effect) = 0;
167 // remove and effect module. Also removes the effect chain is this was the last
168 // effect
169 virtual void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false) = 0;
170 // disconnect an effect handle from module and destroy module if last handle
171 virtual void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast) = 0;
172 // detach all tracks connected to an auxiliary effect
173 virtual void detachAuxEffect_l(int effectId) = 0;
174 // returns a combination of:
175 // - EFFECT_SESSION if effects on this audio session exist in one chain
176 // - TRACK_SESSION if tracks on this audio session exist
177 // - FAST_SESSION if fast tracks on this audio session exist
178 // - SPATIALIZED_SESSION if spatialized tracks on this audio session exist
179 virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
180 virtual uint32_t hasAudioSession(audio_session_t sessionId) const = 0;
181
182 // the value returned by default implementation is not important as the
183 // strategy is only meaningful for PlaybackThread which implements this method
184 virtual product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const = 0;
185
186 // check if some effects must be suspended/restored when an effect is enabled
187 // or disabled
188 virtual void checkSuspendOnEffectEnabled(
189 bool enabled, audio_session_t sessionId, bool threadLocked) = 0;
190
191 virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) = 0;
192 virtual bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const = 0;
193
194 // Return a reference to a per-thread heap which can be used to allocate IMemory
195 // objects that will be read-only to client processes, read/write to mediaserver,
196 // and shared by all client processes of the thread.
197 // The heap is per-thread rather than common across all threads, because
198 // clients can't be trusted not to modify the offset of the IMemory they receive.
199 // If a thread does not have such a heap, this method returns 0.
200 virtual sp<MemoryDealer> readOnlyHeap() const = 0;
201
202 virtual sp<IMemory> pipeMemory() const = 0;
203
204 virtual void systemReady() = 0;
205
206 // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
207 virtual status_t checkEffectCompatibility_l(
208 const effect_descriptor_t* desc, audio_session_t sessionId) = 0;
209
210 virtual void broadcast_l() = 0;
211
212 virtual bool isTimestampCorrectionEnabled() const = 0;
213
214 virtual bool isMsdDevice() const = 0;
215
216 virtual void dump(int fd, const Vector<String16>& args) = 0;
217
218 // deliver stats to mediametrics.
219 virtual void sendStatistics(bool force) = 0;
220
221 virtual Mutex& mutex() const = 0;
222
223 virtual void onEffectEnable(const sp<IAfEffectModule>& effect) = 0;
224 virtual void onEffectDisable() = 0;
225
226 // invalidateTracksForAudioSession_l must be called with holding mLock.
227 virtual void invalidateTracksForAudioSession_l(audio_session_t sessionId) const = 0;
228 // Invalidate all the tracks with the given audio session.
229 virtual void invalidateTracksForAudioSession(audio_session_t sessionId) const = 0;
230
231 virtual bool isStreamInitialized() const = 0;
232 virtual void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) = 0;
233 virtual void stopMelComputation_l() = 0;
234
235 virtual product_strategy_t getStrategyForStream(audio_stream_type_t stream) const = 0;
236};
237
238class IAfPlaybackThread : public virtual IAfThreadBase {
239public:
240 enum mixer_state {
241 MIXER_IDLE, // no active tracks
242 MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready
243 MIXER_TRACKS_READY, // at least one active track, and at least one track has data
244 MIXER_DRAIN_TRACK, // drain currently playing track
245 MIXER_DRAIN_ALL, // fully drain the hardware
246 // standby mode does not have an enum value
247 // suspend by audio policy manager is orthogonal to mixer state
248 };
249
250 // return estimated latency in milliseconds, as reported by HAL
251 virtual uint32_t latency() const = 0; // should be in IAfThreadBase?
252
253 virtual sp<IAfTrack> createTrack_l(
254 const sp<Client>& client,
255 audio_stream_type_t streamType,
256 const audio_attributes_t& attr,
257 uint32_t* sampleRate,
258 audio_format_t format,
259 audio_channel_mask_t channelMask,
260 size_t* pFrameCount,
261 size_t* pNotificationFrameCount,
262 uint32_t notificationsPerBuffer,
263 float speed,
264 const sp<IMemory>& sharedBuffer,
265 audio_session_t sessionId,
266 audio_output_flags_t* flags,
267 pid_t creatorPid,
268 const AttributionSourceState& attributionSource,
269 pid_t tid,
270 status_t* status /*non-NULL*/,
271 audio_port_handle_t portId,
272 const sp<media::IAudioTrackCallback>& callback,
273 bool isSpatialized,
274 bool isBitPerfect) = 0;
275
276 virtual AudioStreamOut* getOutput() const = 0;
277 virtual AudioStreamOut* clearOutput() = 0;
278 virtual sp<StreamHalInterface> stream() const = 0;
279
280 // a very large number of suspend() will eventually wraparound, but unlikely
281 virtual void suspend() = 0;
282 virtual void restore() = 0;
283 virtual bool isSuspended() const = 0;
284 virtual status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const = 0;
285 // Consider also removing and passing an explicit mMainBuffer initialization
286 // parameter to AF::IAfTrack::Track().
287 virtual float* sinkBuffer() const = 0;
288
289 virtual status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId) = 0;
290 virtual status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId) = 0;
291
292 // called with AudioFlinger lock held
293 virtual bool invalidateTracks_l(audio_stream_type_t streamType) = 0;
294 virtual bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) = 0;
295 virtual void invalidateTracks(audio_stream_type_t streamType) = 0;
296 // Invalidate tracks by a set of port ids. The port id will be removed from
297 // the given set if the corresponding track is found and invalidated.
298 virtual void invalidateTracks(std::set<audio_port_handle_t>& portIds) = 0;
299
300 virtual status_t getTimestamp_l(AudioTimestamp& timestamp) = 0;
301 virtual void addPatchTrack(const sp<IAfPatchTrack>& track) = 0;
302 virtual void deletePatchTrack(const sp<IAfPatchTrack>& track) = 0;
303
304 // Return the asynchronous signal wait time.
305 virtual int64_t computeWaitTimeNs_l() const = 0;
306 // returns true if the track is allowed to be added to the thread.
307 virtual bool isTrackAllowed_l(
308 audio_channel_mask_t channelMask, audio_format_t format, audio_session_t sessionId,
309 uid_t uid) const = 0;
310
311 virtual bool supportsHapticPlayback() const = 0;
312
313 virtual void setDownStreamPatch(const struct audio_patch* patch) = 0;
314
315 virtual IAfTrack* getTrackById_l(audio_port_handle_t trackId) = 0;
316
317 virtual bool hasMixer() const = 0;
318
319 virtual status_t setRequestedLatencyMode(audio_latency_mode_t mode) = 0;
320
321 virtual status_t getSupportedLatencyModes(std::vector<audio_latency_mode_t>* modes) = 0;
322
323 virtual status_t setBluetoothVariableLatencyEnabled(bool enabled) = 0;
324
325 virtual void setStandby() = 0;
326 virtual void setStandby_l() = 0;
327 virtual bool waitForHalStart() = 0;
328
329 virtual bool hasFastMixer() const = 0;
330 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const = 0;
331 virtual const std::atomic<int64_t>& framesWritten() const = 0;
332};
333
334} // namespace android