blob: 2763157b1a0ef67a369ee7aca5538937241fbf4b [file] [log] [blame]
Andy Hungfafbebc2023-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
19namespace android {
20
Andy Hung0c1e11e2023-07-06 20:56:16 -070021class IAfDuplicatingThread;
Andy Hung6bad18a2023-07-14 11:45:38 -070022class IAfPatchRecord;
23class IAfPatchTrack;
Andy Hung0c1e11e2023-07-06 20:56:16 -070024class IAfPlaybackThread;
25class IAfRecordThread;
26class IAfThreadBase;
27
Andy Hung6bad18a2023-07-14 11:45:38 -070028struct TeePatch {
29 sp<IAfPatchRecord> patchRecord;
30 sp<IAfPatchTrack> patchTrack;
31};
32
33using TeePatches = std::vector<TeePatch>;
34
Andy Hungfafbebc2023-06-23 19:27:19 -070035// Common interface to all Playback and Record tracks.
36class IAfTrackBase : public virtual RefBase {
37public:
38 enum track_state : int32_t {
39 IDLE,
40 FLUSHED, // for PlaybackTracks only
41 STOPPED,
42 // next 2 states are currently used for fast tracks
43 // and offloaded tracks only
44 STOPPING_1, // waiting for first underrun
45 STOPPING_2, // waiting for presentation complete
46 RESUMING, // for PlaybackTracks only
47 ACTIVE,
48 PAUSING,
49 PAUSED,
50 STARTING_1, // for RecordTrack only
51 STARTING_2, // for RecordTrack only
52 };
53
54 // where to allocate the data buffer
55 enum alloc_type {
56 ALLOC_CBLK, // allocate immediately after control block
57 ALLOC_READONLY, // allocate from a separate read-only heap per thread
58 ALLOC_PIPE, // do not allocate; use the pipe buffer
59 ALLOC_LOCAL, // allocate a local buffer
60 ALLOC_NONE, // do not allocate:use the buffer passed to TrackBase constructor
61 };
62
63 enum track_type {
64 TYPE_DEFAULT,
65 TYPE_OUTPUT,
66 TYPE_PATCH,
67 };
68
69 virtual status_t initCheck() const = 0;
70 virtual status_t start(
71 AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
72 audio_session_t triggerSession = AUDIO_SESSION_NONE) = 0;
73 virtual void stop() = 0;
74 virtual sp<IMemory> getCblk() const = 0;
75 virtual audio_track_cblk_t* cblk() const = 0;
76 virtual audio_session_t sessionId() const = 0;
77 virtual uid_t uid() const = 0;
78 virtual pid_t creatorPid() const = 0;
79 virtual uint32_t sampleRate() const = 0;
80 virtual size_t frameSize() const = 0;
81 virtual audio_port_handle_t portId() const = 0;
82 virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) = 0;
83 virtual track_state state() const = 0;
84 virtual void setState(track_state state) = 0;
85 virtual sp<IMemory> getBuffers() const = 0;
86 virtual void* buffer() const = 0;
87 virtual size_t bufferSize() const = 0;
88 virtual bool isFastTrack() const = 0;
89 virtual bool isDirect() const = 0;
90 virtual bool isOutputTrack() const = 0;
91 virtual bool isPatchTrack() const = 0;
92 virtual bool isExternalTrack() const = 0;
93
94 virtual void invalidate() = 0;
95 virtual bool isInvalid() const = 0;
96
97 virtual void terminate() = 0;
98 virtual bool isTerminated() const = 0;
99
100 virtual audio_attributes_t attributes() const = 0;
101 virtual bool isSpatialized() const = 0;
102 virtual bool isBitPerfect() const = 0;
103
104 // not currently implemented in TrackBase, but overridden.
105 virtual void destroy() {}; // MmapTrack doesn't implement.
106 virtual void appendDumpHeader(String8& result) const = 0;
107 virtual void appendDump(String8& result, bool active) const = 0;
108
109 // Dup with AudioBufferProvider interface
110 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
111 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer) = 0;
112
113 // Added for RecordTrack and OutputTrack
Andy Hung0c1e11e2023-07-06 20:56:16 -0700114 virtual wp<IAfThreadBase> thread() const = 0;
Andy Hungfafbebc2023-06-23 19:27:19 -0700115 virtual const sp<ServerProxy>& serverProxy() const = 0;
116
117 // TEE_SINK
118 virtual void dumpTee(int fd __unused, const std::string& reason __unused) const {};
119
120 /** returns the buffer contents size converted to time in milliseconds
121 * for PCM Playback or Record streaming tracks. The return value is zero for
122 * PCM static tracks and not defined for non-PCM tracks.
123 *
124 * This may be called without the thread lock.
125 */
126 virtual double bufferLatencyMs() const = 0;
127
128 /** returns whether the track supports server latency computation.
129 * This is set in the constructor and constant throughout the track lifetime.
130 */
131 virtual bool isServerLatencySupported() const = 0;
132
133 /** computes the server latency for PCM Playback or Record track
134 * to the device sink/source. This is the time for the next frame in the track buffer
135 * written or read from the server thread to the device source or sink.
136 *
137 * This may be called without the thread lock, but latencyMs and fromTrack
138 * may be not be synchronized. For example PatchPanel may not obtain the
139 * thread lock before calling.
140 *
141 * \param latencyMs on success is set to the latency in milliseconds of the
142 * next frame written/read by the server thread to/from the track buffer
143 * from the device source/sink.
144 * \param fromTrack on success is set to true if latency was computed directly
145 * from the track timestamp; otherwise set to false if latency was
146 * estimated from the server timestamp.
147 * fromTrack may be nullptr or omitted if not required.
148 *
149 * \returns OK or INVALID_OPERATION on failure.
150 */
151 virtual status_t getServerLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;
152
153 /** computes the total client latency for PCM Playback or Record tracks
154 * for the next client app access to the device sink/source; i.e. the
155 * server latency plus the buffer latency.
156 *
157 * This may be called without the thread lock, but latencyMs and fromTrack
158 * may be not be synchronized. For example PatchPanel may not obtain the
159 * thread lock before calling.
160 *
161 * \param latencyMs on success is set to the latency in milliseconds of the
162 * next frame written/read by the client app to/from the track buffer
163 * from the device sink/source.
164 * \param fromTrack on success is set to true if latency was computed directly
165 * from the track timestamp; otherwise set to false if latency was
166 * estimated from the server timestamp.
167 * fromTrack may be nullptr or omitted if not required.
168 *
169 * \returns OK or INVALID_OPERATION on failure.
170 */
171 virtual status_t getTrackLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;
172
173 // TODO: Consider making this external.
174 struct FrameTime {
175 int64_t frames;
176 int64_t timeNs;
177 };
178
179 // KernelFrameTime is updated per "mix" period even for non-pcm tracks.
180 virtual void getKernelFrameTime(FrameTime* ft) const = 0;
181
182 virtual audio_format_t format() const = 0;
183 virtual int id() const = 0;
184
185 virtual const char* getTrackStateAsString() const = 0;
186
187 // Called by the PlaybackThread to indicate that the track is becoming active
188 // and a new interval should start with a given device list.
189 virtual void logBeginInterval(const std::string& devices) = 0;
190
191 // Called by the PlaybackThread to indicate the track is no longer active.
192 virtual void logEndInterval() = 0;
193
194 // Called to tally underrun frames in playback.
195 virtual void tallyUnderrunFrames(size_t frames) = 0;
196
197 virtual audio_channel_mask_t channelMask() const = 0;
198
199 /** @return true if the track has changed (metadata or volume) since
200 * the last time this function was called,
201 * true if this function was never called since the track creation,
202 * false otherwise.
203 * Thread safe.
204 */
205 virtual bool readAndClearHasChanged() = 0;
206
207 /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */
208 virtual void setMetadataHasChanged() = 0;
209
210 /**
211 * Called when a track moves to active state to record its contribution to battery usage.
212 * Track state transitions should eventually be handled within the track class.
213 */
214 virtual void beginBatteryAttribution() = 0;
215
216 /**
217 * Called when a track moves out of the active state to record its contribution
218 * to battery usage.
219 */
220 virtual void endBatteryAttribution() = 0;
221
222 /**
223 * For RecordTrack
Andy Hungeb6b5f82023-07-14 11:00:08 -0700224 * TODO(b/291317964) either use this or add asRecordTrack or asTrack etc.
Andy Hungfafbebc2023-06-23 19:27:19 -0700225 */
226 virtual void handleSyncStartEvent(const sp<audioflinger::SyncEvent>& event __unused){};
227
228 // For Thread use, fast tracks and offloaded tracks only
Andy Hungeb6b5f82023-07-14 11:00:08 -0700229 // TODO(b/291317964) rearrange to IAfTrack.
Andy Hungfafbebc2023-06-23 19:27:19 -0700230 virtual bool isStopped() const = 0;
231 virtual bool isStopping() const = 0;
232 virtual bool isStopping_1() const = 0;
233 virtual bool isStopping_2() const = 0;
234};
235
236// Common interface for Playback tracks.
237class IAfTrack : public virtual IAfTrackBase {
238public:
Andy Hung11e74242023-06-26 19:20:57 -0700239 // FillingStatus is used for suppressing volume ramp at begin of playing
240 enum FillingStatus { FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE };
241
Andy Hungfafbebc2023-06-23 19:27:19 -0700242 // createIAudioTrackAdapter() is a static constructor which creates an
243 // IAudioTrack AIDL interface adapter from the Track object that
244 // may be passed back to the client (if needed).
245 //
246 // Only one AIDL IAudioTrack interface adapter should be created per Track.
247 static sp<media::IAudioTrack> createIAudioTrackAdapter(const sp<IAfTrack>& track);
248
Andy Hung0c1e11e2023-07-06 20:56:16 -0700249 static sp<IAfTrack> create(
250 IAfPlaybackThread* thread,
Andy Hung11e74242023-06-26 19:20:57 -0700251 const sp<Client>& client,
252 audio_stream_type_t streamType,
253 const audio_attributes_t& attr,
254 uint32_t sampleRate,
255 audio_format_t format,
256 audio_channel_mask_t channelMask,
257 size_t frameCount,
258 void* buffer,
259 size_t bufferSize,
260 const sp<IMemory>& sharedBuffer,
261 audio_session_t sessionId,
262 pid_t creatorPid,
263 const AttributionSourceState& attributionSource,
264 audio_output_flags_t flags,
265 track_type type,
266 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
267 /** default behaviour is to start when there are as many frames
268 * ready as possible (aka. Buffer is full). */
269 size_t frameCountToBeReady = SIZE_MAX,
270 float speed = 1.0f,
271 bool isSpatialized = false,
272 bool isBitPerfect = false);
273
Andy Hungfafbebc2023-06-23 19:27:19 -0700274 virtual void pause() = 0;
275 virtual void flush() = 0;
276 virtual audio_stream_type_t streamType() const = 0;
277 virtual bool isOffloaded() const = 0;
278 virtual bool isOffloadedOrDirect() const = 0;
279 virtual bool isStatic() const = 0;
280 virtual status_t setParameters(const String8& keyValuePairs) = 0;
281 virtual status_t selectPresentation(int presentationId, int programId) = 0;
282 virtual status_t attachAuxEffect(int EffectId) = 0;
283 virtual void setAuxBuffer(int EffectId, int32_t* buffer) = 0;
284 virtual int32_t* auxBuffer() const = 0;
285 virtual void setMainBuffer(float* buffer) = 0;
286 virtual float* mainBuffer() const = 0;
287 virtual int auxEffectId() const = 0;
288 virtual status_t getTimestamp(AudioTimestamp& timestamp) = 0;
289 virtual void signal() = 0;
290 virtual status_t getDualMonoMode(audio_dual_mono_mode_t* mode) const = 0;
291 virtual status_t setDualMonoMode(audio_dual_mono_mode_t mode) = 0;
292 virtual status_t getAudioDescriptionMixLevel(float* leveldB) const = 0;
293 virtual status_t setAudioDescriptionMixLevel(float leveldB) = 0;
294 virtual status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) const = 0;
295 virtual status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) = 0;
296
297 // implement FastMixerState::VolumeProvider interface
298 virtual gain_minifloat_packed_t getVolumeLR() const = 0;
299
300 // implement volume handling.
301 virtual media::VolumeShaper::Status applyVolumeShaper(
302 const sp<media::VolumeShaper::Configuration>& configuration,
303 const sp<media::VolumeShaper::Operation>& operation) = 0;
304 virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) const = 0;
305 virtual sp<media::VolumeHandler> getVolumeHandler() const = 0;
306 /** Set the computed normalized final volume of the track.
307 * !masterMute * masterVolume * streamVolume * averageLRVolume */
308 virtual void setFinalVolume(float volumeLeft, float volumeRight) = 0;
309 virtual float getFinalVolume() const = 0;
310 virtual void getFinalVolume(float* left, float* right) const = 0;
311
312 using SourceMetadatas = std::vector<playback_track_metadata_v7_t>;
313 using MetadataInserter = std::back_insert_iterator<SourceMetadatas>;
314 /** Copy the track metadata in the provided iterator. Thread safe. */
315 virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0;
316
317 /** Return haptic playback of the track is enabled or not, used in mixer. */
318 virtual bool getHapticPlaybackEnabled() const = 0;
319 /** Set haptic playback of the track is enabled or not, should be
320 * set after query or get callback from vibrator service */
321 virtual void setHapticPlaybackEnabled(bool hapticPlaybackEnabled) = 0;
322 /** Return at what intensity to play haptics, used in mixer. */
323 virtual os::HapticScale getHapticIntensity() const = 0;
324 /** Return the maximum amplitude allowed for haptics data, used in mixer. */
325 virtual float getHapticMaxAmplitude() const = 0;
326 /** Set intensity of haptic playback, should be set after querying vibrator service. */
327 virtual void setHapticIntensity(os::HapticScale hapticIntensity) = 0;
328 /** Set maximum amplitude allowed for haptic data, should be set after querying
329 * vibrator service.
330 */
331 virtual void setHapticMaxAmplitude(float maxAmplitude) = 0;
332 virtual sp<os::ExternalVibration> getExternalVibration() const = 0;
333
334 // This function should be called with holding thread lock.
335 virtual void updateTeePatches_l() = 0;
336
Andy Hung6bad18a2023-07-14 11:45:38 -0700337 // Argument teePatchesToUpdate is by value, use std::move to optimize.
338 virtual void setTeePatchesToUpdate_l(TeePatches teePatchesToUpdate) = 0;
Andy Hungfafbebc2023-06-23 19:27:19 -0700339
340 static bool checkServerLatencySupported(audio_format_t format, audio_output_flags_t flags) {
341 return audio_is_linear_pcm(format) && (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) == 0;
342 }
343
344 virtual audio_output_flags_t getOutputFlags() const = 0;
345 virtual float getSpeed() const = 0;
346
347 /**
348 * Updates the mute state and notifies the audio service. Call this only when holding player
349 * thread lock.
350 */
351 virtual void processMuteEvent_l(
352 const sp<IAudioManager>& audioManager, mute_state_t muteState) = 0;
353
354 virtual void triggerEvents(AudioSystem::sync_event_t type) = 0;
355
356 virtual void disable() = 0;
357 virtual int& fastIndex() = 0;
358 virtual bool isPlaybackRestricted() const = 0;
Andy Hung11e74242023-06-26 19:20:57 -0700359
360 // Used by thread only
361
362 virtual bool isPausing() const = 0;
363 virtual bool isPaused() const = 0;
364 virtual bool isResuming() const = 0;
365 virtual bool isReady() const = 0;
366 virtual void setPaused() = 0;
367 virtual void reset() = 0;
368 virtual bool isFlushPending() const = 0;
369 virtual void flushAck() = 0;
370 virtual bool isResumePending() const = 0;
371 virtual void resumeAck() = 0;
372 // For direct or offloaded tracks ensure that the pause state is acknowledged
373 // by the playback thread in case of an immediate flush.
374 virtual bool isPausePending() const = 0;
375 virtual void pauseAck() = 0;
376 virtual void updateTrackFrameInfo(
377 int64_t trackFramesReleased, int64_t sinkFramesWritten, uint32_t halSampleRate,
378 const ExtendedTimestamp& timeStamp) = 0;
379 virtual sp<IMemory> sharedBuffer() const = 0;
380
381 // Dup with ExtendedAudioBufferProvider
382 virtual size_t framesReady() const = 0;
383
384 // presentationComplete checked by frames. (Mixed Tracks).
385 // framesWritten is cumulative, never reset, and is shared all tracks
386 // audioHalFrames is derived from output latency
387 virtual bool presentationComplete(int64_t framesWritten, size_t audioHalFrames) = 0;
388
389 // presentationComplete checked by time. (Direct Tracks).
390 virtual bool presentationComplete(uint32_t latencyMs) = 0;
391
392 virtual void resetPresentationComplete() = 0;
393
394 virtual bool hasVolumeController() const = 0;
395 virtual void setHasVolumeController(bool hasVolumeController) = 0;
396 virtual const sp<AudioTrackServerProxy>& audioTrackServerProxy() const = 0;
397 virtual void setCachedVolume(float volume) = 0;
398 virtual void setResetDone(bool resetDone) = 0;
399
400 virtual ExtendedAudioBufferProvider* asExtendedAudioBufferProvider() = 0;
401 virtual VolumeProvider* asVolumeProvider() = 0;
402
Andy Hungeb6b5f82023-07-14 11:00:08 -0700403 // TODO(b/291317964) split into getter/setter
Andy Hung11e74242023-06-26 19:20:57 -0700404 virtual FillingStatus& fillingStatus() = 0;
405 virtual int8_t& retryCount() = 0;
406 virtual FastTrackUnderruns& fastTrackUnderruns() = 0;
Andy Hungfafbebc2023-06-23 19:27:19 -0700407};
408
409// playback track, used by DuplicatingThread
410class IAfOutputTrack : public virtual IAfTrack {
411public:
Andy Hung11e74242023-06-26 19:20:57 -0700412 static sp<IAfOutputTrack> create(
Andy Hung0c1e11e2023-07-06 20:56:16 -0700413 IAfPlaybackThread* playbackThread,
414 IAfDuplicatingThread* sourceThread, uint32_t sampleRate,
Andy Hung11e74242023-06-26 19:20:57 -0700415 audio_format_t format, audio_channel_mask_t channelMask, size_t frameCount,
416 const AttributionSourceState& attributionSource);
417
Andy Hungfafbebc2023-06-23 19:27:19 -0700418 virtual ssize_t write(void* data, uint32_t frames) = 0;
419 virtual bool bufferQueueEmpty() const = 0;
420 virtual bool isActive() const = 0;
421
422 /** Set the metadatas of the upstream tracks. Thread safe. */
423 virtual void setMetadatas(const SourceMetadatas& metadatas) = 0;
424 /** returns client timestamp to the upstream duplicating thread. */
425 virtual ExtendedTimestamp getClientProxyTimestamp() const = 0;
426};
427
428class IAfMmapTrack : public virtual IAfTrackBase {
429public:
Andy Hung0c1e11e2023-07-06 20:56:16 -0700430 static sp<IAfMmapTrack> create(IAfThreadBase* thread,
Andy Hung11e74242023-06-26 19:20:57 -0700431 const audio_attributes_t& attr,
432 uint32_t sampleRate,
433 audio_format_t format,
434 audio_channel_mask_t channelMask,
435 audio_session_t sessionId,
436 bool isOut,
437 const android::content::AttributionSourceState& attributionSource,
438 pid_t creatorPid,
439 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
440
Andy Hungfafbebc2023-06-23 19:27:19 -0700441 // protected by MMapThread::mLock
442 virtual void setSilenced_l(bool silenced) = 0;
443 // protected by MMapThread::mLock
444 virtual bool isSilenced_l() const = 0;
445 // protected by MMapThread::mLock
446 virtual bool getAndSetSilencedNotified_l() = 0;
447
448 /**
449 * Updates the mute state and notifies the audio service. Call this only when holding player
450 * thread lock.
451 */
452 virtual void processMuteEvent_l( // see IAfTrack
453 const sp<IAudioManager>& audioManager, mute_state_t muteState) = 0;
454};
455
Andy Hung11e74242023-06-26 19:20:57 -0700456class RecordBufferConverter;
457
Andy Hungfafbebc2023-06-23 19:27:19 -0700458class IAfRecordTrack : public virtual IAfTrackBase {
459public:
460 // createIAudioRecordAdapter() is a static constructor which creates an
461 // IAudioRecord AIDL interface adapter from the RecordTrack object that
462 // may be passed back to the client (if needed).
463 //
464 // Only one AIDL IAudioRecord interface adapter should be created per RecordTrack.
465 static sp<media::IAudioRecord> createIAudioRecordAdapter(const sp<IAfRecordTrack>& recordTrack);
466
Andy Hung0c1e11e2023-07-06 20:56:16 -0700467 static sp<IAfRecordTrack> create(IAfRecordThread* thread,
Andy Hung11e74242023-06-26 19:20:57 -0700468 const sp<Client>& client,
469 const audio_attributes_t& attr,
470 uint32_t sampleRate,
471 audio_format_t format,
472 audio_channel_mask_t channelMask,
473 size_t frameCount,
474 void* buffer,
475 size_t bufferSize,
476 audio_session_t sessionId,
477 pid_t creatorPid,
478 const AttributionSourceState& attributionSource,
479 audio_input_flags_t flags,
480 track_type type,
481 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
482 int32_t startFrames = -1);
483
Andy Hungfafbebc2023-06-23 19:27:19 -0700484 // clear the buffer overflow flag
485 virtual void clearOverflow() = 0;
486 // set the buffer overflow flag and return previous value
487 virtual bool setOverflow() = 0;
488
Andy Hungeb6b5f82023-07-14 11:00:08 -0700489 // TODO(b/291317964) handleSyncStartEvent in IAfTrackBase should move here.
Andy Hungfafbebc2023-06-23 19:27:19 -0700490 virtual void clearSyncStartEvent() = 0;
491 virtual void updateTrackFrameInfo(
492 int64_t trackFramesReleased, int64_t sourceFramesRead, uint32_t halSampleRate,
493 const ExtendedTimestamp& timestamp) = 0;
494
495 virtual void setSilenced(bool silenced) = 0;
496 virtual bool isSilenced() const = 0;
497 virtual status_t getActiveMicrophones(
498 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const = 0;
499
500 virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) = 0;
501 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
502 virtual status_t shareAudioHistory(
503 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) = 0;
504 virtual int32_t startFrames() const = 0;
505
506 static bool checkServerLatencySupported(audio_format_t format, audio_input_flags_t flags) {
507 return audio_is_linear_pcm(format) && (flags & AUDIO_INPUT_FLAG_HW_AV_SYNC) == 0;
508 }
509
510 using SinkMetadatas = std::vector<record_track_metadata_v7_t>;
511 using MetadataInserter = std::back_insert_iterator<SinkMetadatas>;
512 virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0; // see IAfTrack
Andy Hung11e74242023-06-26 19:20:57 -0700513
514 // private to Threads
515 virtual AudioBufferProvider::Buffer& sinkBuffer() = 0;
516 virtual audioflinger::SynchronizedRecordState& synchronizedRecordState() = 0;
517 virtual RecordBufferConverter* recordBufferConverter() const = 0;
518 virtual ResamplerBufferProvider* resamplerBufferProvider() const = 0;
Andy Hungfafbebc2023-06-23 19:27:19 -0700519};
520
Andy Hung062a7a32023-06-26 19:20:57 -0700521// PatchProxyBufferProvider interface is implemented by PatchTrack and PatchRecord.
522// it provides buffer access methods that map those of a ClientProxy (see AudioTrackShared.h)
523class PatchProxyBufferProvider {
524public:
525 virtual ~PatchProxyBufferProvider() = default;
526 virtual bool producesBufferOnDemand() const = 0;
527 virtual status_t obtainBuffer(
528 Proxy::Buffer* buffer, const struct timespec* requested = nullptr) = 0;
529 virtual void releaseBuffer(Proxy::Buffer* buffer) = 0;
530};
531
532class IAfPatchTrackBase : public virtual RefBase {
533public:
Andy Hung11e74242023-06-26 19:20:57 -0700534 using Timeout = std::optional<std::chrono::nanoseconds>;
535
Andy Hung062a7a32023-06-26 19:20:57 -0700536 virtual void setPeerTimeout(std::chrono::nanoseconds timeout) = 0;
537 virtual void setPeerProxy(const sp<IAfPatchTrackBase>& proxy, bool holdReference) = 0;
538 virtual void clearPeerProxy() = 0;
539 virtual PatchProxyBufferProvider* asPatchProxyBufferProvider() = 0;
540};
541
Andy Hung11e74242023-06-26 19:20:57 -0700542class IAfPatchTrack : public virtual IAfTrack, public virtual IAfPatchTrackBase {
543public:
544 static sp<IAfPatchTrack> create(
Andy Hung0c1e11e2023-07-06 20:56:16 -0700545 IAfPlaybackThread* playbackThread,
Andy Hung11e74242023-06-26 19:20:57 -0700546 audio_stream_type_t streamType,
547 uint32_t sampleRate,
548 audio_channel_mask_t channelMask,
549 audio_format_t format,
550 size_t frameCount,
551 void *buffer,
552 size_t bufferSize,
553 audio_output_flags_t flags,
554 const Timeout& timeout = {},
555 size_t frameCountToBeReady = 1 /** Default behaviour is to start
556 * as soon as possible to have
557 * the lowest possible latency
558 * even if it might glitch. */);
559};
Andy Hung062a7a32023-06-26 19:20:57 -0700560
Andy Hung062a7a32023-06-26 19:20:57 -0700561class IAfPatchRecord : public virtual IAfRecordTrack, public virtual IAfPatchTrackBase {
562public:
Andy Hung11e74242023-06-26 19:20:57 -0700563 static sp<IAfPatchRecord> create(
Andy Hung0c1e11e2023-07-06 20:56:16 -0700564 IAfRecordThread* recordThread,
Andy Hung11e74242023-06-26 19:20:57 -0700565 uint32_t sampleRate,
566 audio_channel_mask_t channelMask,
567 audio_format_t format,
568 size_t frameCount,
569 void* buffer,
570 size_t bufferSize,
571 audio_input_flags_t flags,
572 const Timeout& timeout = {},
573 audio_source_t source = AUDIO_SOURCE_DEFAULT);
574
575 static sp<IAfPatchRecord> createPassThru(
Andy Hung0c1e11e2023-07-06 20:56:16 -0700576 IAfRecordThread* recordThread,
Andy Hung11e74242023-06-26 19:20:57 -0700577 uint32_t sampleRate,
578 audio_channel_mask_t channelMask,
579 audio_format_t format,
580 size_t frameCount,
581 audio_input_flags_t flags,
582 audio_source_t source = AUDIO_SOURCE_DEFAULT);
583
Andy Hung062a7a32023-06-26 19:20:57 -0700584 virtual Source* getSource() = 0;
585 virtual size_t writeFrames(const void* src, size_t frameCount, size_t frameSize) = 0;
586};
587
Andy Hungfafbebc2023-06-23 19:27:19 -0700588} // namespace android