blob: ee834d63519d7d81bccdc375e0234cbcd7d7dc70 [file] [log] [blame]
Andy Hungd29af632023-06-23 19:27:19 -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
Andy Hungc6f227f2023-07-18 18:31:50 -070019#include <android/media/BnAudioRecord.h>
20#include <android/media/BnAudioTrack.h>
Andy Hungf302e812024-01-26 11:55:15 -080021#include <audio_utils/mutex.h>
Andy Hungc6f227f2023-07-18 18:31:50 -070022#include <audiomanager/IAudioManager.h>
23#include <binder/IMemory.h>
Andy Hung6b137d12024-08-27 22:35:17 +000024#include <datapath/VolumePortInterface.h>
Andy Hungc6f227f2023-07-18 18:31:50 -070025#include <fastpath/FastMixerDumpState.h>
26#include <media/AudioSystem.h>
27#include <media/VolumeShaper.h>
28#include <private/media/AudioTrackShared.h>
29#include <timing/SyncEvent.h>
30#include <timing/SynchronizedRecordState.h>
31#include <utils/RefBase.h>
32#include <vibrator/ExternalVibration.h>
33
34#include <vector>
35
Andy Hungd29af632023-06-23 19:27:19 -070036namespace android {
37
Andy Hungc6f227f2023-07-18 18:31:50 -070038class Client;
39class ResamplerBufferProvider;
40struct Source;
41
Andy Hung87c693c2023-07-06 20:56:16 -070042class IAfDuplicatingThread;
Andy Hung16ed0da2023-07-14 11:45:38 -070043class IAfPatchRecord;
44class IAfPatchTrack;
Andy Hung87c693c2023-07-06 20:56:16 -070045class IAfPlaybackThread;
46class IAfRecordThread;
47class IAfThreadBase;
48
Andy Hung16ed0da2023-07-14 11:45:38 -070049struct TeePatch {
50 sp<IAfPatchRecord> patchRecord;
51 sp<IAfPatchTrack> patchTrack;
52};
53
54using TeePatches = std::vector<TeePatch>;
55
Andy Hungd29af632023-06-23 19:27:19 -070056// Common interface to all Playback and Record tracks.
57class IAfTrackBase : public virtual RefBase {
58public:
59 enum track_state : int32_t {
60 IDLE,
61 FLUSHED, // for PlaybackTracks only
62 STOPPED,
63 // next 2 states are currently used for fast tracks
64 // and offloaded tracks only
65 STOPPING_1, // waiting for first underrun
66 STOPPING_2, // waiting for presentation complete
67 RESUMING, // for PlaybackTracks only
68 ACTIVE,
69 PAUSING,
70 PAUSED,
71 STARTING_1, // for RecordTrack only
72 STARTING_2, // for RecordTrack only
73 };
74
75 // where to allocate the data buffer
76 enum alloc_type {
77 ALLOC_CBLK, // allocate immediately after control block
78 ALLOC_READONLY, // allocate from a separate read-only heap per thread
79 ALLOC_PIPE, // do not allocate; use the pipe buffer
80 ALLOC_LOCAL, // allocate a local buffer
81 ALLOC_NONE, // do not allocate:use the buffer passed to TrackBase constructor
82 };
83
84 enum track_type {
85 TYPE_DEFAULT,
86 TYPE_OUTPUT,
87 TYPE_PATCH,
88 };
89
90 virtual status_t initCheck() const = 0;
91 virtual status_t start(
92 AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
93 audio_session_t triggerSession = AUDIO_SESSION_NONE) = 0;
94 virtual void stop() = 0;
95 virtual sp<IMemory> getCblk() const = 0;
96 virtual audio_track_cblk_t* cblk() const = 0;
97 virtual audio_session_t sessionId() const = 0;
98 virtual uid_t uid() const = 0;
99 virtual pid_t creatorPid() const = 0;
100 virtual uint32_t sampleRate() const = 0;
101 virtual size_t frameSize() const = 0;
102 virtual audio_port_handle_t portId() const = 0;
103 virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) = 0;
104 virtual track_state state() const = 0;
105 virtual void setState(track_state state) = 0;
106 virtual sp<IMemory> getBuffers() const = 0;
107 virtual void* buffer() const = 0;
108 virtual size_t bufferSize() const = 0;
109 virtual bool isFastTrack() const = 0;
110 virtual bool isDirect() const = 0;
111 virtual bool isOutputTrack() const = 0;
112 virtual bool isPatchTrack() const = 0;
113 virtual bool isExternalTrack() const = 0;
114
115 virtual void invalidate() = 0;
116 virtual bool isInvalid() const = 0;
117
118 virtual void terminate() = 0;
119 virtual bool isTerminated() const = 0;
120
121 virtual audio_attributes_t attributes() const = 0;
122 virtual bool isSpatialized() const = 0;
123 virtual bool isBitPerfect() const = 0;
124
125 // not currently implemented in TrackBase, but overridden.
126 virtual void destroy() {}; // MmapTrack doesn't implement.
127 virtual void appendDumpHeader(String8& result) const = 0;
128 virtual void appendDump(String8& result, bool active) const = 0;
129
130 // Dup with AudioBufferProvider interface
131 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
132 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer) = 0;
133
134 // Added for RecordTrack and OutputTrack
Andy Hung87c693c2023-07-06 20:56:16 -0700135 virtual wp<IAfThreadBase> thread() const = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700136 virtual const sp<ServerProxy>& serverProxy() const = 0;
137
138 // TEE_SINK
139 virtual void dumpTee(int fd __unused, const std::string& reason __unused) const {};
140
141 /** returns the buffer contents size converted to time in milliseconds
142 * for PCM Playback or Record streaming tracks. The return value is zero for
143 * PCM static tracks and not defined for non-PCM tracks.
144 *
145 * This may be called without the thread lock.
146 */
147 virtual double bufferLatencyMs() const = 0;
148
149 /** returns whether the track supports server latency computation.
150 * This is set in the constructor and constant throughout the track lifetime.
151 */
152 virtual bool isServerLatencySupported() const = 0;
153
154 /** computes the server latency for PCM Playback or Record track
155 * to the device sink/source. This is the time for the next frame in the track buffer
156 * written or read from the server thread to the device source or sink.
157 *
158 * This may be called without the thread lock, but latencyMs and fromTrack
159 * may be not be synchronized. For example PatchPanel may not obtain the
160 * thread lock before calling.
161 *
162 * \param latencyMs on success is set to the latency in milliseconds of the
163 * next frame written/read by the server thread to/from the track buffer
164 * from the device source/sink.
165 * \param fromTrack on success is set to true if latency was computed directly
166 * from the track timestamp; otherwise set to false if latency was
167 * estimated from the server timestamp.
168 * fromTrack may be nullptr or omitted if not required.
169 *
170 * \returns OK or INVALID_OPERATION on failure.
171 */
172 virtual status_t getServerLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;
173
174 /** computes the total client latency for PCM Playback or Record tracks
175 * for the next client app access to the device sink/source; i.e. the
176 * server latency plus the buffer latency.
177 *
178 * This may be called without the thread lock, but latencyMs and fromTrack
179 * may be not be synchronized. For example PatchPanel may not obtain the
180 * thread lock before calling.
181 *
182 * \param latencyMs on success is set to the latency in milliseconds of the
183 * next frame written/read by the client app to/from the track buffer
184 * from the device sink/source.
185 * \param fromTrack on success is set to true if latency was computed directly
186 * from the track timestamp; otherwise set to false if latency was
187 * estimated from the server timestamp.
188 * fromTrack may be nullptr or omitted if not required.
189 *
190 * \returns OK or INVALID_OPERATION on failure.
191 */
192 virtual status_t getTrackLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;
193
194 // TODO: Consider making this external.
195 struct FrameTime {
196 int64_t frames;
197 int64_t timeNs;
198 };
199
200 // KernelFrameTime is updated per "mix" period even for non-pcm tracks.
201 virtual void getKernelFrameTime(FrameTime* ft) const = 0;
202
203 virtual audio_format_t format() const = 0;
204 virtual int id() const = 0;
205
206 virtual const char* getTrackStateAsString() const = 0;
207
208 // Called by the PlaybackThread to indicate that the track is becoming active
209 // and a new interval should start with a given device list.
210 virtual void logBeginInterval(const std::string& devices) = 0;
211
212 // Called by the PlaybackThread to indicate the track is no longer active.
213 virtual void logEndInterval() = 0;
214
215 // Called to tally underrun frames in playback.
216 virtual void tallyUnderrunFrames(size_t frames) = 0;
217
218 virtual audio_channel_mask_t channelMask() const = 0;
219
220 /** @return true if the track has changed (metadata or volume) since
221 * the last time this function was called,
222 * true if this function was never called since the track creation,
223 * false otherwise.
224 * Thread safe.
225 */
226 virtual bool readAndClearHasChanged() = 0;
227
228 /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */
229 virtual void setMetadataHasChanged() = 0;
230
231 /**
232 * Called when a track moves to active state to record its contribution to battery usage.
233 * Track state transitions should eventually be handled within the track class.
234 */
235 virtual void beginBatteryAttribution() = 0;
236
237 /**
238 * Called when a track moves out of the active state to record its contribution
239 * to battery usage.
240 */
241 virtual void endBatteryAttribution() = 0;
242
243 /**
244 * For RecordTrack
Andy Hung99b1ba62023-07-14 11:00:08 -0700245 * TODO(b/291317964) either use this or add asRecordTrack or asTrack etc.
Andy Hungd29af632023-06-23 19:27:19 -0700246 */
247 virtual void handleSyncStartEvent(const sp<audioflinger::SyncEvent>& event __unused){};
248
249 // For Thread use, fast tracks and offloaded tracks only
Andy Hung99b1ba62023-07-14 11:00:08 -0700250 // TODO(b/291317964) rearrange to IAfTrack.
Andy Hungd29af632023-06-23 19:27:19 -0700251 virtual bool isStopped() const = 0;
252 virtual bool isStopping() const = 0;
253 virtual bool isStopping_1() const = 0;
254 virtual bool isStopping_2() const = 0;
255};
256
257// Common interface for Playback tracks.
Andy Hung6b137d12024-08-27 22:35:17 +0000258class IAfTrack : public virtual IAfTrackBase, public virtual VolumePortInterface {
Andy Hungd29af632023-06-23 19:27:19 -0700259public:
Andy Hung8d31fd22023-06-26 19:20:57 -0700260 // FillingStatus is used for suppressing volume ramp at begin of playing
261 enum FillingStatus { FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE };
262
Andy Hungd29af632023-06-23 19:27:19 -0700263 // createIAudioTrackAdapter() is a static constructor which creates an
264 // IAudioTrack AIDL interface adapter from the Track object that
265 // may be passed back to the client (if needed).
266 //
267 // Only one AIDL IAudioTrack interface adapter should be created per Track.
268 static sp<media::IAudioTrack> createIAudioTrackAdapter(const sp<IAfTrack>& track);
269
Andy Hung87c693c2023-07-06 20:56:16 -0700270 static sp<IAfTrack> create(
271 IAfPlaybackThread* thread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700272 const sp<Client>& client,
273 audio_stream_type_t streamType,
274 const audio_attributes_t& attr,
275 uint32_t sampleRate,
276 audio_format_t format,
277 audio_channel_mask_t channelMask,
278 size_t frameCount,
279 void* buffer,
280 size_t bufferSize,
281 const sp<IMemory>& sharedBuffer,
282 audio_session_t sessionId,
283 pid_t creatorPid,
284 const AttributionSourceState& attributionSource,
285 audio_output_flags_t flags,
286 track_type type,
287 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
288 /** default behaviour is to start when there are as many frames
289 * ready as possible (aka. Buffer is full). */
290 size_t frameCountToBeReady = SIZE_MAX,
291 float speed = 1.0f,
292 bool isSpatialized = false,
Andy Hung6b137d12024-08-27 22:35:17 +0000293 bool isBitPerfect = false,
294 float volume = 0.0f);
Andy Hung8d31fd22023-06-26 19:20:57 -0700295
Andy Hungd29af632023-06-23 19:27:19 -0700296 virtual void pause() = 0;
297 virtual void flush() = 0;
298 virtual audio_stream_type_t streamType() const = 0;
299 virtual bool isOffloaded() const = 0;
300 virtual bool isOffloadedOrDirect() const = 0;
301 virtual bool isStatic() const = 0;
302 virtual status_t setParameters(const String8& keyValuePairs) = 0;
303 virtual status_t selectPresentation(int presentationId, int programId) = 0;
304 virtual status_t attachAuxEffect(int EffectId) = 0;
305 virtual void setAuxBuffer(int EffectId, int32_t* buffer) = 0;
306 virtual int32_t* auxBuffer() const = 0;
307 virtual void setMainBuffer(float* buffer) = 0;
308 virtual float* mainBuffer() const = 0;
309 virtual int auxEffectId() const = 0;
310 virtual status_t getTimestamp(AudioTimestamp& timestamp) = 0;
311 virtual void signal() = 0;
312 virtual status_t getDualMonoMode(audio_dual_mono_mode_t* mode) const = 0;
313 virtual status_t setDualMonoMode(audio_dual_mono_mode_t mode) = 0;
314 virtual status_t getAudioDescriptionMixLevel(float* leveldB) const = 0;
315 virtual status_t setAudioDescriptionMixLevel(float leveldB) = 0;
316 virtual status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) const = 0;
317 virtual status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) = 0;
318
319 // implement FastMixerState::VolumeProvider interface
320 virtual gain_minifloat_packed_t getVolumeLR() const = 0;
321
322 // implement volume handling.
323 virtual media::VolumeShaper::Status applyVolumeShaper(
324 const sp<media::VolumeShaper::Configuration>& configuration,
325 const sp<media::VolumeShaper::Operation>& operation) = 0;
326 virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) const = 0;
327 virtual sp<media::VolumeHandler> getVolumeHandler() const = 0;
328 /** Set the computed normalized final volume of the track.
329 * !masterMute * masterVolume * streamVolume * averageLRVolume */
330 virtual void setFinalVolume(float volumeLeft, float volumeRight) = 0;
331 virtual float getFinalVolume() const = 0;
332 virtual void getFinalVolume(float* left, float* right) const = 0;
333
334 using SourceMetadatas = std::vector<playback_track_metadata_v7_t>;
335 using MetadataInserter = std::back_insert_iterator<SourceMetadatas>;
336 /** Copy the track metadata in the provided iterator. Thread safe. */
337 virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0;
338
339 /** Return haptic playback of the track is enabled or not, used in mixer. */
340 virtual bool getHapticPlaybackEnabled() const = 0;
341 /** Set haptic playback of the track is enabled or not, should be
342 * set after query or get callback from vibrator service */
343 virtual void setHapticPlaybackEnabled(bool hapticPlaybackEnabled) = 0;
Ahmad Khalil229466a2024-02-05 12:15:30 +0000344 /** Return the haptics scale, used in mixer. */
345 virtual os::HapticScale getHapticScale() const = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700346 /** Return the maximum amplitude allowed for haptics data, used in mixer. */
347 virtual float getHapticMaxAmplitude() const = 0;
Ahmad Khalil229466a2024-02-05 12:15:30 +0000348 /** Set scale for haptic playback, should be set after querying vibrator service. */
349 virtual void setHapticScale(os::HapticScale hapticScale) = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700350 /** Set maximum amplitude allowed for haptic data, should be set after querying
351 * vibrator service.
352 */
353 virtual void setHapticMaxAmplitude(float maxAmplitude) = 0;
354 virtual sp<os::ExternalVibration> getExternalVibration() const = 0;
355
356 // This function should be called with holding thread lock.
Andy Hungf302e812024-01-26 11:55:15 -0800357 virtual void updateTeePatches_l() REQUIRES(audio_utils::ThreadBase_Mutex)
358 EXCLUDES_BELOW_ThreadBase_Mutex = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700359
Andy Hung16ed0da2023-07-14 11:45:38 -0700360 // Argument teePatchesToUpdate is by value, use std::move to optimize.
361 virtual void setTeePatchesToUpdate_l(TeePatches teePatchesToUpdate) = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700362
363 static bool checkServerLatencySupported(audio_format_t format, audio_output_flags_t flags) {
364 return audio_is_linear_pcm(format) && (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) == 0;
365 }
366
367 virtual audio_output_flags_t getOutputFlags() const = 0;
368 virtual float getSpeed() const = 0;
369
370 /**
371 * Updates the mute state and notifies the audio service. Call this only when holding player
372 * thread lock.
373 */
374 virtual void processMuteEvent_l(
375 const sp<IAudioManager>& audioManager, mute_state_t muteState) = 0;
376
377 virtual void triggerEvents(AudioSystem::sync_event_t type) = 0;
378
379 virtual void disable() = 0;
Eric Laurent022a5132024-04-12 17:02:51 +0000380 virtual bool isDisabled() const = 0;
381
Andy Hungd29af632023-06-23 19:27:19 -0700382 virtual int& fastIndex() = 0;
383 virtual bool isPlaybackRestricted() const = 0;
Andy Hung8d31fd22023-06-26 19:20:57 -0700384
385 // Used by thread only
386
387 virtual bool isPausing() const = 0;
388 virtual bool isPaused() const = 0;
389 virtual bool isResuming() const = 0;
390 virtual bool isReady() const = 0;
391 virtual void setPaused() = 0;
392 virtual void reset() = 0;
393 virtual bool isFlushPending() const = 0;
394 virtual void flushAck() = 0;
395 virtual bool isResumePending() const = 0;
396 virtual void resumeAck() = 0;
397 // For direct or offloaded tracks ensure that the pause state is acknowledged
398 // by the playback thread in case of an immediate flush.
399 virtual bool isPausePending() const = 0;
400 virtual void pauseAck() = 0;
401 virtual void updateTrackFrameInfo(
402 int64_t trackFramesReleased, int64_t sinkFramesWritten, uint32_t halSampleRate,
403 const ExtendedTimestamp& timeStamp) = 0;
404 virtual sp<IMemory> sharedBuffer() const = 0;
405
406 // Dup with ExtendedAudioBufferProvider
407 virtual size_t framesReady() const = 0;
408
409 // presentationComplete checked by frames. (Mixed Tracks).
410 // framesWritten is cumulative, never reset, and is shared all tracks
411 // audioHalFrames is derived from output latency
412 virtual bool presentationComplete(int64_t framesWritten, size_t audioHalFrames) = 0;
413
414 // presentationComplete checked by time. (Direct Tracks).
415 virtual bool presentationComplete(uint32_t latencyMs) = 0;
416
417 virtual void resetPresentationComplete() = 0;
418
419 virtual bool hasVolumeController() const = 0;
420 virtual void setHasVolumeController(bool hasVolumeController) = 0;
421 virtual const sp<AudioTrackServerProxy>& audioTrackServerProxy() const = 0;
422 virtual void setCachedVolume(float volume) = 0;
423 virtual void setResetDone(bool resetDone) = 0;
424
425 virtual ExtendedAudioBufferProvider* asExtendedAudioBufferProvider() = 0;
426 virtual VolumeProvider* asVolumeProvider() = 0;
427
Andy Hung99b1ba62023-07-14 11:00:08 -0700428 // TODO(b/291317964) split into getter/setter
Andy Hung8d31fd22023-06-26 19:20:57 -0700429 virtual FillingStatus& fillingStatus() = 0;
430 virtual int8_t& retryCount() = 0;
431 virtual FastTrackUnderruns& fastTrackUnderruns() = 0;
jiabin220eea12024-05-17 17:55:20 +0000432
433 // Internal mute, this is currently only used for bit-perfect playback
434 virtual bool getInternalMute() const = 0;
435 virtual void setInternalMute(bool muted) = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700436};
437
438// playback track, used by DuplicatingThread
439class IAfOutputTrack : public virtual IAfTrack {
440public:
Andy Hung8d31fd22023-06-26 19:20:57 -0700441 static sp<IAfOutputTrack> create(
Andy Hung87c693c2023-07-06 20:56:16 -0700442 IAfPlaybackThread* playbackThread,
443 IAfDuplicatingThread* sourceThread, uint32_t sampleRate,
Andy Hung8d31fd22023-06-26 19:20:57 -0700444 audio_format_t format, audio_channel_mask_t channelMask, size_t frameCount,
445 const AttributionSourceState& attributionSource);
446
Andy Hungd29af632023-06-23 19:27:19 -0700447 virtual ssize_t write(void* data, uint32_t frames) = 0;
448 virtual bool bufferQueueEmpty() const = 0;
449 virtual bool isActive() const = 0;
450
451 /** Set the metadatas of the upstream tracks. Thread safe. */
452 virtual void setMetadatas(const SourceMetadatas& metadatas) = 0;
453 /** returns client timestamp to the upstream duplicating thread. */
454 virtual ExtendedTimestamp getClientProxyTimestamp() const = 0;
455};
456
Andy Hung6b137d12024-08-27 22:35:17 +0000457class IAfMmapTrack : public virtual IAfTrackBase, public virtual VolumePortInterface {
Andy Hungd29af632023-06-23 19:27:19 -0700458public:
Andy Hung87c693c2023-07-06 20:56:16 -0700459 static sp<IAfMmapTrack> create(IAfThreadBase* thread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700460 const audio_attributes_t& attr,
461 uint32_t sampleRate,
462 audio_format_t format,
463 audio_channel_mask_t channelMask,
464 audio_session_t sessionId,
465 bool isOut,
466 const android::content::AttributionSourceState& attributionSource,
467 pid_t creatorPid,
Andy Hung6b137d12024-08-27 22:35:17 +0000468 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
469 float volume = 0.0f);
Andy Hung8d31fd22023-06-26 19:20:57 -0700470
Andy Hungd29af632023-06-23 19:27:19 -0700471 // protected by MMapThread::mLock
472 virtual void setSilenced_l(bool silenced) = 0;
473 // protected by MMapThread::mLock
474 virtual bool isSilenced_l() const = 0;
475 // protected by MMapThread::mLock
476 virtual bool getAndSetSilencedNotified_l() = 0;
477
478 /**
479 * Updates the mute state and notifies the audio service. Call this only when holding player
480 * thread lock.
481 */
482 virtual void processMuteEvent_l( // see IAfTrack
483 const sp<IAudioManager>& audioManager, mute_state_t muteState) = 0;
484};
485
Andy Hung8d31fd22023-06-26 19:20:57 -0700486class RecordBufferConverter;
487
Andy Hungd29af632023-06-23 19:27:19 -0700488class IAfRecordTrack : public virtual IAfTrackBase {
489public:
490 // createIAudioRecordAdapter() is a static constructor which creates an
491 // IAudioRecord AIDL interface adapter from the RecordTrack object that
492 // may be passed back to the client (if needed).
493 //
494 // Only one AIDL IAudioRecord interface adapter should be created per RecordTrack.
495 static sp<media::IAudioRecord> createIAudioRecordAdapter(const sp<IAfRecordTrack>& recordTrack);
496
Andy Hung87c693c2023-07-06 20:56:16 -0700497 static sp<IAfRecordTrack> create(IAfRecordThread* thread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700498 const sp<Client>& client,
499 const audio_attributes_t& attr,
500 uint32_t sampleRate,
501 audio_format_t format,
502 audio_channel_mask_t channelMask,
503 size_t frameCount,
504 void* buffer,
505 size_t bufferSize,
506 audio_session_t sessionId,
507 pid_t creatorPid,
508 const AttributionSourceState& attributionSource,
509 audio_input_flags_t flags,
510 track_type type,
511 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
512 int32_t startFrames = -1);
513
Andy Hungd29af632023-06-23 19:27:19 -0700514 // clear the buffer overflow flag
515 virtual void clearOverflow() = 0;
516 // set the buffer overflow flag and return previous value
517 virtual bool setOverflow() = 0;
518
Andy Hung99b1ba62023-07-14 11:00:08 -0700519 // TODO(b/291317964) handleSyncStartEvent in IAfTrackBase should move here.
Andy Hungd29af632023-06-23 19:27:19 -0700520 virtual void clearSyncStartEvent() = 0;
521 virtual void updateTrackFrameInfo(
522 int64_t trackFramesReleased, int64_t sourceFramesRead, uint32_t halSampleRate,
523 const ExtendedTimestamp& timestamp) = 0;
524
525 virtual void setSilenced(bool silenced) = 0;
526 virtual bool isSilenced() const = 0;
527 virtual status_t getActiveMicrophones(
528 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const = 0;
529
530 virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) = 0;
531 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
532 virtual status_t shareAudioHistory(
533 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) = 0;
534 virtual int32_t startFrames() const = 0;
535
536 static bool checkServerLatencySupported(audio_format_t format, audio_input_flags_t flags) {
537 return audio_is_linear_pcm(format) && (flags & AUDIO_INPUT_FLAG_HW_AV_SYNC) == 0;
538 }
539
540 using SinkMetadatas = std::vector<record_track_metadata_v7_t>;
541 using MetadataInserter = std::back_insert_iterator<SinkMetadatas>;
542 virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0; // see IAfTrack
Andy Hung8d31fd22023-06-26 19:20:57 -0700543
544 // private to Threads
545 virtual AudioBufferProvider::Buffer& sinkBuffer() = 0;
546 virtual audioflinger::SynchronizedRecordState& synchronizedRecordState() = 0;
547 virtual RecordBufferConverter* recordBufferConverter() const = 0;
548 virtual ResamplerBufferProvider* resamplerBufferProvider() const = 0;
Andy Hungd29af632023-06-23 19:27:19 -0700549};
550
Andy Hungca9be052023-06-26 19:20:57 -0700551// PatchProxyBufferProvider interface is implemented by PatchTrack and PatchRecord.
552// it provides buffer access methods that map those of a ClientProxy (see AudioTrackShared.h)
553class PatchProxyBufferProvider {
554public:
555 virtual ~PatchProxyBufferProvider() = default;
556 virtual bool producesBufferOnDemand() const = 0;
557 virtual status_t obtainBuffer(
558 Proxy::Buffer* buffer, const struct timespec* requested = nullptr) = 0;
559 virtual void releaseBuffer(Proxy::Buffer* buffer) = 0;
560};
561
562class IAfPatchTrackBase : public virtual RefBase {
563public:
Andy Hung8d31fd22023-06-26 19:20:57 -0700564 using Timeout = std::optional<std::chrono::nanoseconds>;
565
Andy Hungca9be052023-06-26 19:20:57 -0700566 virtual void setPeerTimeout(std::chrono::nanoseconds timeout) = 0;
567 virtual void setPeerProxy(const sp<IAfPatchTrackBase>& proxy, bool holdReference) = 0;
568 virtual void clearPeerProxy() = 0;
569 virtual PatchProxyBufferProvider* asPatchProxyBufferProvider() = 0;
570};
571
Andy Hung8d31fd22023-06-26 19:20:57 -0700572class IAfPatchTrack : public virtual IAfTrack, public virtual IAfPatchTrackBase {
573public:
574 static sp<IAfPatchTrack> create(
Andy Hung87c693c2023-07-06 20:56:16 -0700575 IAfPlaybackThread* playbackThread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700576 audio_stream_type_t streamType,
577 uint32_t sampleRate,
578 audio_channel_mask_t channelMask,
579 audio_format_t format,
580 size_t frameCount,
581 void *buffer,
582 size_t bufferSize,
583 audio_output_flags_t flags,
584 const Timeout& timeout = {},
guonaichao3acc9b12024-06-07 09:27:21 +0800585 size_t frameCountToBeReady = 1, /** Default behaviour is to start
Andy Hung8d31fd22023-06-26 19:20:57 -0700586 * as soon as possible to have
587 * the lowest possible latency
guonaichao3acc9b12024-06-07 09:27:21 +0800588 * even if it might glitch. */
Andy Hung6b137d12024-08-27 22:35:17 +0000589 float speed = 1.0f,
590 float volume = 1.0f);
Andy Hung8d31fd22023-06-26 19:20:57 -0700591};
Andy Hungca9be052023-06-26 19:20:57 -0700592
Andy Hungca9be052023-06-26 19:20:57 -0700593class IAfPatchRecord : public virtual IAfRecordTrack, public virtual IAfPatchTrackBase {
594public:
Andy Hung8d31fd22023-06-26 19:20:57 -0700595 static sp<IAfPatchRecord> create(
Andy Hung87c693c2023-07-06 20:56:16 -0700596 IAfRecordThread* recordThread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700597 uint32_t sampleRate,
598 audio_channel_mask_t channelMask,
599 audio_format_t format,
600 size_t frameCount,
601 void* buffer,
602 size_t bufferSize,
603 audio_input_flags_t flags,
604 const Timeout& timeout = {},
605 audio_source_t source = AUDIO_SOURCE_DEFAULT);
606
607 static sp<IAfPatchRecord> createPassThru(
Andy Hung87c693c2023-07-06 20:56:16 -0700608 IAfRecordThread* recordThread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700609 uint32_t sampleRate,
610 audio_channel_mask_t channelMask,
611 audio_format_t format,
612 size_t frameCount,
613 audio_input_flags_t flags,
614 audio_source_t source = AUDIO_SOURCE_DEFAULT);
615
Andy Hungca9be052023-06-26 19:20:57 -0700616 virtual Source* getSource() = 0;
617 virtual size_t writeFrames(const void* src, size_t frameCount, size_t frameSize) = 0;
618};
619
Andy Hungd29af632023-06-23 19:27:19 -0700620} // namespace android