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