• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <android/media/BnAudioRecord.h>
20 #include <android/media/BnAudioTrack.h>
21 #include <audio_utils/mutex.h>
22 #include <audiomanager/IAudioManager.h>
23 #include <binder/IMemory.h>
24 #include <media/AppOpsSession.h>
25 #include <mediautils/SingleThreadExecutor.h>
26 #include <datapath/VolumePortInterface.h>
27 #include <fastpath/FastMixerDumpState.h>
28 #include <media/AudioSystem.h>
29 #include <media/VolumeShaper.h>
30 #include <private/media/AudioTrackShared.h>
31 #include <timing/SyncEvent.h>
32 #include <timing/SynchronizedRecordState.h>
33 #include <utils/RefBase.h>
34 #include <vibrator/ExternalVibration.h>
35 
36 #include <vector>
37 
38 namespace android {
39 
40 class Client;
41 class ResamplerBufferProvider;
42 struct Source;
43 
44 class IAfDuplicatingThread;
45 class IAfPatchRecord;
46 class IAfPatchTrack;
47 class IAfPlaybackThread;
48 class IAfRecordThread;
49 class IAfThreadBase;
50 class IAfThreadCallback;
51 
52 struct TeePatch {
53     sp<IAfPatchRecord> patchRecord;
54     sp<IAfPatchTrack> patchTrack;
55 };
56 
57 using TeePatches = std::vector<TeePatch>;
58 
59 // Common interface to all Playback and Record tracks.
60 class IAfTrackBase : public virtual RefBase {
61 public:
62     enum track_state : int32_t {
63         IDLE,
64         FLUSHED,  // for PlaybackTracks only
65         STOPPED,
66         // next 2 states are currently used for fast tracks
67         // and offloaded tracks only
68         STOPPING_1,  // waiting for first underrun
69         STOPPING_2,  // waiting for presentation complete
70         RESUMING,    // for PlaybackTracks only
71         ACTIVE,
72         PAUSING,
73         PAUSED,
74         STARTING_1,  // for RecordTrack only
75         STARTING_2,  // for RecordTrack only
76     };
77 
78     // where to allocate the data buffer
79     enum alloc_type {
80         ALLOC_CBLK,      // allocate immediately after control block
81         ALLOC_READONLY,  // allocate from a separate read-only heap per thread
82         ALLOC_PIPE,      // do not allocate; use the pipe buffer
83         ALLOC_LOCAL,     // allocate a local buffer
84         ALLOC_NONE,      // do not allocate:use the buffer passed to TrackBase constructor
85     };
86 
87     enum track_type {
88         TYPE_DEFAULT,
89         TYPE_OUTPUT,
90         TYPE_PATCH,
91     };
92 
93     virtual status_t initCheck() const = 0;
94     virtual status_t start(
95             AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
96             audio_session_t triggerSession = AUDIO_SESSION_NONE) = 0;
97     virtual void stop() = 0;
98     virtual sp<IMemory> getCblk() const = 0;
99     virtual audio_track_cblk_t* cblk() const = 0;
100     virtual audio_session_t sessionId() const = 0;
101     virtual uid_t uid() const = 0;
102     virtual pid_t creatorPid() const = 0;
103     virtual uint32_t sampleRate() const = 0;
104     virtual size_t frameSize() const = 0;
105     virtual audio_port_handle_t portId() const = 0;
106     virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) = 0;
107     virtual track_state state() const = 0;
108     virtual void setState(track_state state) = 0;
109     virtual sp<IMemory> getBuffers() const = 0;
110     virtual void* buffer() const = 0;
111     virtual size_t bufferSize() const = 0;
112     virtual bool isFastTrack() const = 0;
113     virtual bool isDirect() const = 0;
114     virtual bool isOutputTrack() const = 0;
115     virtual bool isPatchTrack() const = 0;
116     virtual bool isExternalTrack() const = 0;
117 
118     virtual void invalidate() = 0;
119     virtual bool isInvalid() const = 0;
120 
121     virtual void terminate() = 0;
122     virtual bool isTerminated() const = 0;
123 
124     virtual audio_attributes_t attributes() const = 0;
125     virtual bool isSpatialized() const = 0;
126     virtual bool isBitPerfect() const = 0;
127 
128     // not currently implemented in TrackBase, but overridden.
destroy()129     virtual void destroy() {};  // MmapTrack doesn't implement.
130     virtual void appendDumpHeader(String8& result) const = 0;
131     virtual void appendDump(String8& result, bool active) const = 0;
132 
133     // Dup with AudioBufferProvider interface
134     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
135     virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer) = 0;
136 
137     virtual void signal() = 0;
138     // Added for RecordTrack and OutputTrack
139     virtual wp<IAfThreadBase> thread() const = 0;
140     virtual const sp<ServerProxy>& serverProxy() const = 0;
141 
142     // TEE_SINK
dumpTee(int fd __unused,const std::string & reason __unused)143     virtual void dumpTee(int fd __unused, const std::string& reason __unused) const {};
144 
145     /** returns the buffer contents size converted to time in milliseconds
146      * for PCM Playback or Record streaming tracks. The return value is zero for
147      * PCM static tracks and not defined for non-PCM tracks.
148      *
149      * This may be called without the thread lock.
150      */
151     virtual double bufferLatencyMs() const = 0;
152 
153     /** returns whether the track supports server latency computation.
154      * This is set in the constructor and constant throughout the track lifetime.
155      */
156     virtual bool isServerLatencySupported() const = 0;
157 
158     /** computes the server latency for PCM Playback or Record track
159      * to the device sink/source.  This is the time for the next frame in the track buffer
160      * written or read from the server thread to the device source or sink.
161      *
162      * This may be called without the thread lock, but latencyMs and fromTrack
163      * may be not be synchronized. For example PatchPanel may not obtain the
164      * thread lock before calling.
165      *
166      * \param latencyMs on success is set to the latency in milliseconds of the
167      *        next frame written/read by the server thread to/from the track buffer
168      *        from the device source/sink.
169      * \param fromTrack on success is set to true if latency was computed directly
170      *        from the track timestamp; otherwise set to false if latency was
171      *        estimated from the server timestamp.
172      *        fromTrack may be nullptr or omitted if not required.
173      *
174      * \returns OK or INVALID_OPERATION on failure.
175      */
176     virtual status_t getServerLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;
177 
178     /** computes the total client latency for PCM Playback or Record tracks
179      * for the next client app access to the device sink/source; i.e. the
180      * server latency plus the buffer latency.
181      *
182      * This may be called without the thread lock, but latencyMs and fromTrack
183      * may be not be synchronized. For example PatchPanel may not obtain the
184      * thread lock before calling.
185      *
186      * \param latencyMs on success is set to the latency in milliseconds of the
187      *        next frame written/read by the client app to/from the track buffer
188      *        from the device sink/source.
189      * \param fromTrack on success is set to true if latency was computed directly
190      *        from the track timestamp; otherwise set to false if latency was
191      *        estimated from the server timestamp.
192      *        fromTrack may be nullptr or omitted if not required.
193      *
194      * \returns OK or INVALID_OPERATION on failure.
195      */
196     virtual status_t getTrackLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;
197 
198     // TODO: Consider making this external.
199     struct FrameTime {
200         int64_t frames;
201         int64_t timeNs;
202     };
203 
204     // KernelFrameTime is updated per "mix" period even for non-pcm tracks.
205     virtual void getKernelFrameTime(FrameTime* ft) const = 0;
206 
207     virtual audio_format_t format() const = 0;
208     virtual int id() const = 0;
209 
210     virtual const char* getTrackStateAsString() const = 0;
211 
212     virtual const std::string& getTraceSuffix() const = 0;
213     // Called by the PlaybackThread to indicate that the track is becoming active
214     // and a new interval should start with a given device list.
215     virtual void logBeginInterval(const std::string& devices) = 0;
216 
217     // Called by the PlaybackThread to indicate the track is no longer active.
218     virtual void logEndInterval() = 0;
219 
220     // Called by the PlaybackThread when ATRACE is enabled.
221     virtual void logRefreshInterval(const std::string& devices) = 0;
222 
223     // Called to tally underrun frames in playback.
224     virtual void tallyUnderrunFrames(size_t frames) = 0;
225 
226     virtual audio_channel_mask_t channelMask() const = 0;
227 
228     /** @return true if the track has changed (metadata or volume) since
229      *          the last time this function was called,
230      *          true if this function was never called since the track creation,
231      *          false otherwise.
232      *  Thread safe.
233      */
234     virtual bool readAndClearHasChanged() = 0;
235 
236     /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */
237     virtual void setMetadataHasChanged() = 0;
238 
239     /**
240      * Called when a track moves to active state to record its contribution to battery usage.
241      * Track state transitions should eventually be handled within the track class.
242      */
243     virtual void beginBatteryAttribution() = 0;
244 
245     /**
246      * Called when a track moves out of the active state to record its contribution
247      * to battery usage.
248      */
249     virtual void endBatteryAttribution() = 0;
250 
251     /**
252      * For RecordTrack
253      * TODO(b/291317964) either use this or add asRecordTrack or asTrack etc.
254      */
handleSyncStartEvent(const sp<audioflinger::SyncEvent> & event __unused)255     virtual void handleSyncStartEvent(const sp<audioflinger::SyncEvent>& event __unused){};
256 
257     // For Thread use, fast tracks and offloaded tracks only
258     // TODO(b/291317964) rearrange to IAfTrack.
259     virtual bool isStopped() const = 0;
260     virtual bool isStopping() const = 0;
261     virtual bool isStopping_1() const = 0;
262     virtual bool isStopping_2() const = 0;
263 };
264 
265 // Functionality shared between MMAP and audioflinger datapath playback tracks. Note that MMAP
266 // tracks don't implement the IAfTrack, just IAfTrackBase
267 // Not a pure interface since no forward declaration necessary.
268 class AfPlaybackCommon : public virtual VolumePortInterface {
269     using AppOpsSession = media::permission::AppOpsSession<media::permission::DefaultAppOpsFacade>;
270 
271   public:
272     enum class EnforcementLevel {
273         NONE, // no enforcement
274         PARTIAL, // enforcement for CONTROL_PARTIAL
275         FULL, // enforcement for CONTROL
276     };
277 
278     AfPlaybackCommon(IAfTrackBase& self, IAfThreadBase& thread, float volume, bool muted,
279                      const audio_attributes_t& attr,
280                      const AttributionSourceState& attributionSource,
281                      bool isOffloadOrMmap,
282                      bool shouldPlaybackHarden = true);
283 
284     /**
285      * Updates the mute state and notifies the audio service.
286      */
287     void processMuteEvent(media::IAudioManagerNative& am, mute_state_t muteState);
288 
289     void maybeLogPlaybackHardening(media::IAudioManagerNative& am) const;
290 
291     // Restricted due to OP_AUDIO_CONTROL_PARTIAL
hasOpControlPartial()292     bool hasOpControlPartial() const {
293         return mOpControlPartialSession ? mHasOpControlPartial.load(std::memory_order_acquire)
294                                         : true;
295     }
296 
297     // Restricted due to OP_AUDIO_CONTROL
hasOpControlFull()298     bool hasOpControlFull() const {
299         return mOpControlFullSession ? mHasOpControlFull.load(std::memory_order_acquire) : true;
300     }
301 
isPlaybackRestrictedControl()302     bool isPlaybackRestrictedControl() const {
303         using enum EnforcementLevel;
304         switch (mEnforcementLevel) {
305             case NONE:
306                 return false;
307             case PARTIAL:
308                 return !hasOpControlPartial();
309             case FULL:
310                 return !hasOpControlFull();
311         }
312     }
313 
314     // VolumePortInterface implementation
315     // for now the secondary patch tracks will always be not muted
316     // TODO(b/388241142): use volume capture rules to forward the vol/mute to patch tracks
317 
setPortVolume(float volume)318     void setPortVolume(float volume) final { mVolume = volume; }
319 
setPortMute(bool muted)320     void setPortMute(bool muted) final {
321         mMutedFromPort = muted;
322     }
323 
getPortVolume()324     float getPortVolume() const final { return mVolume; }
325 
getPortMute()326     bool getPortMute() const final { return mMutedFromPort; }
327 
328   protected:
329     // The following methods are for notifying that sonifying playback intends to begin/end
330     // for playback hardening purposes.
331     // TODO(b/385417236) once mute logic is centralized, the delivery request session should be
332     // tied to sonifying playback instead of track start->pause
333     void startPlaybackDelivery();
334     void endPlaybackDelivery();
335 
336   private:
337     const IAfTrackBase& mSelf;
338 
339     std::optional<mediautils::SingleThreadExecutor> mExecutor;
340     // TODO: atomic necessary if underneath thread lock?
341     std::atomic<mute_state_t> mMuteState;
342     std::atomic<bool> mMutedFromPort;
343     // associated with port
344     std::atomic<float> mVolume = 0.0f;
345 
346     const EnforcementLevel mEnforcementLevel;
347 
348     std::atomic<bool> mHasOpControlPartial {true};
349     std::atomic<bool> mHasOpControlFull {true};
350     mutable std::atomic<bool> mPlaybackHardeningLogged {false};
351     // the ref behind the optional is const
352     // these members are last in decl order to ensure it is destroyed first
353     std::optional<AppOpsSession> mOpControlPartialSession;
354     std::optional<AppOpsSession> mOpControlFullSession;
355 };
356 
357 // Common interface for audioflinger Playback tracks.
358 class IAfTrack : public virtual IAfTrackBase, public virtual AfPlaybackCommon {
359 public:
360     // FillingStatus is used for suppressing volume ramp at begin of playing
361     enum FillingStatus { FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE };
362 
363     // createIAudioTrackAdapter() is a static constructor which creates an
364     // IAudioTrack AIDL interface adapter from the Track object that
365     // may be passed back to the client (if needed).
366     //
367     // Only one AIDL IAudioTrack interface adapter should be created per Track.
368     static sp<media::IAudioTrack> createIAudioTrackAdapter(const sp<IAfTrack>& track);
369 
370     static sp<IAfTrack> create(
371             IAfPlaybackThread* thread,
372             const sp<Client>& client,
373             audio_stream_type_t streamType,
374             const audio_attributes_t& attr,
375             uint32_t sampleRate,
376             audio_format_t format,
377             audio_channel_mask_t channelMask,
378             size_t frameCount,
379             void* buffer,
380             size_t bufferSize,
381             const sp<IMemory>& sharedBuffer,
382             audio_session_t sessionId,
383             pid_t creatorPid,
384             const AttributionSourceState& attributionSource,
385             audio_output_flags_t flags,
386             track_type type,
387             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
388             /** default behaviour is to start when there are as many frames
389               * ready as possible (aka. Buffer is full). */
390             size_t frameCountToBeReady = SIZE_MAX,
391             float speed = 1.0f,
392             bool isSpatialized = false,
393             bool isBitPerfect = false,
394             float volume = 0.0f,
395             bool muted = false);
396 
getLogHeader()397     static constexpr std::string_view getLogHeader() {
398         using namespace std::literals;
399         return "Type     Id Active Client(pid/uid) Session Port Id S  Flags "
400                         "  Format Chn mask  SRate "
401                         "ST Usg CT "
402                         " G db  L dB  R dB  VS dB  PortVol dB  PortMuted "
403                         "  Server FrmCnt  FrmRdy F Underruns  Flushed BitPerfect InternalMute"
404                         "   Latency\n"sv;
405     }
406 
407     virtual void pause() = 0;
408     virtual void flush() = 0;
409     virtual audio_stream_type_t streamType() const = 0;
410     virtual bool isOffloaded() const = 0;
411     virtual bool isOffloadedOrDirect() const = 0;
412     virtual bool isStatic() const = 0;
413     virtual status_t setParameters(const String8& keyValuePairs) = 0;
414     virtual status_t selectPresentation(int presentationId, int programId) = 0;
415     virtual status_t attachAuxEffect(int EffectId) = 0;
416     virtual void setAuxBuffer(int EffectId, int32_t* buffer) = 0;
417     virtual int32_t* auxBuffer() const = 0;
418     virtual void setMainBuffer(float* buffer) = 0;
419     virtual float* mainBuffer() const = 0;
420     virtual int auxEffectId() const = 0;
421     virtual status_t getTimestamp(AudioTimestamp& timestamp) = 0;
422     virtual status_t getDualMonoMode(audio_dual_mono_mode_t* mode) const = 0;
423     virtual status_t setDualMonoMode(audio_dual_mono_mode_t mode) = 0;
424     virtual status_t getAudioDescriptionMixLevel(float* leveldB) const = 0;
425     virtual status_t setAudioDescriptionMixLevel(float leveldB) = 0;
426     virtual status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) const = 0;
427     virtual status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) = 0;
428 
429     // implement FastMixerState::VolumeProvider interface
430     virtual gain_minifloat_packed_t getVolumeLR() const = 0;
431 
432     // implement volume handling.
433     virtual media::VolumeShaper::Status applyVolumeShaper(
434             const sp<media::VolumeShaper::Configuration>& configuration,
435             const sp<media::VolumeShaper::Operation>& operation) = 0;
436     virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) const = 0;
437     virtual sp<media::VolumeHandler> getVolumeHandler() const = 0;
438     /** Set the computed normalized final volume of the track.
439      * !masterMute * masterVolume * streamVolume * averageLRVolume */
440     virtual void setFinalVolume(float volumeLeft, float volumeRight) = 0;
441     virtual float getFinalVolume() const = 0;
442     virtual void getFinalVolume(float* left, float* right) const = 0;
443 
444     using SourceMetadatas = std::vector<playback_track_metadata_v7_t>;
445     using MetadataInserter = std::back_insert_iterator<SourceMetadatas>;
446     /** Copy the track metadata in the provided iterator. Thread safe. */
447     virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0;
448 
449     /** Return haptic playback of the track is enabled or not, used in mixer. */
450     virtual bool getHapticPlaybackEnabled() const = 0;
451     /** Set haptic playback of the track is enabled or not, should be
452      * set after query or get callback from vibrator service */
453     virtual void setHapticPlaybackEnabled(bool hapticPlaybackEnabled) = 0;
454     /** Return the haptics scale, used in mixer. */
455     virtual os::HapticScale getHapticScale() const = 0;
456     /** Return the maximum amplitude allowed for haptics data, used in mixer. */
457     virtual float getHapticMaxAmplitude() const = 0;
458     /** Set scale for haptic playback, should be set after querying vibrator service. */
459     virtual void setHapticScale(os::HapticScale hapticScale) = 0;
460     /** Set maximum amplitude allowed for haptic data, should be set after querying
461      *  vibrator service.
462      */
463     virtual void setHapticMaxAmplitude(float maxAmplitude) = 0;
464     virtual sp<os::ExternalVibration> getExternalVibration() const = 0;
465 
466     // This function should be called with holding thread lock.
467     virtual void updateTeePatches_l() REQUIRES(audio_utils::ThreadBase_Mutex)
468             EXCLUDES_BELOW_ThreadBase_Mutex = 0;
469 
470     // Argument teePatchesToUpdate is by value, use std::move to optimize.
471     virtual void setTeePatchesToUpdate_l(TeePatches teePatchesToUpdate) = 0;
472 
checkServerLatencySupported(audio_format_t format,audio_output_flags_t flags)473     static bool checkServerLatencySupported(audio_format_t format, audio_output_flags_t flags) {
474         return audio_is_linear_pcm(format) && (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) == 0;
475     }
476 
477     virtual audio_output_flags_t getOutputFlags() const = 0;
478     virtual float getSpeed() const = 0;
479 
480     virtual void triggerEvents(AudioSystem::sync_event_t type) = 0;
481 
482     virtual void disable() = 0;
483     virtual bool isDisabled() const = 0;
484 
485     virtual int& fastIndex() = 0;
486 
487     // Restricted due to OP_PLAY_AUDIO
488     virtual bool isPlaybackRestrictedOp() const = 0;
489 
490     virtual bool isPlaybackRestricted() const = 0;
491 
492     // Used by thread only
493 
494     virtual bool isPausing() const = 0;
495     virtual bool isPaused() const = 0;
496     virtual bool isResuming() const = 0;
497     virtual bool isReady() const = 0;
498     virtual void setPaused() = 0;
499     virtual void reset() = 0;
500     virtual bool isFlushPending() const = 0;
501     virtual void flushAck() = 0;
502     virtual bool isResumePending() const = 0;
503     virtual void resumeAck() = 0;
504     // For direct or offloaded tracks ensure that the pause state is acknowledged
505     // by the playback thread in case of an immediate flush.
506     virtual bool isPausePending() const = 0;
507     virtual void pauseAck() = 0;
508     virtual void updateTrackFrameInfo(
509             int64_t trackFramesReleased, int64_t sinkFramesWritten, uint32_t halSampleRate,
510             const ExtendedTimestamp& timeStamp) = 0;
511     virtual sp<IMemory> sharedBuffer() const = 0;
512 
513     // Dup with ExtendedAudioBufferProvider
514     virtual size_t framesReady() const = 0;
515 
516     // presentationComplete checked by frames. (Mixed Tracks).
517     // framesWritten is cumulative, never reset, and is shared all tracks
518     // audioHalFrames is derived from output latency
519     virtual bool presentationComplete(int64_t framesWritten, size_t audioHalFrames) = 0;
520 
521     // presentationComplete checked by time. (Direct Tracks).
522     virtual bool presentationComplete(uint32_t latencyMs) = 0;
523 
524     virtual void resetPresentationComplete() = 0;
525 
526     virtual bool hasVolumeController() const = 0;
527     virtual void setHasVolumeController(bool hasVolumeController) = 0;
528     virtual const sp<AudioTrackServerProxy>& audioTrackServerProxy() const = 0;
529     virtual void setCachedVolume(float volume) = 0;
530     virtual void setResetDone(bool resetDone) = 0;
531 
532     virtual ExtendedAudioBufferProvider* asExtendedAudioBufferProvider() = 0;
533     virtual VolumeProvider* asVolumeProvider() = 0;
534 
535     // TODO(b/291317964) split into getter/setter
536     virtual FillingStatus& fillingStatus() = 0;
537     virtual int8_t& retryCount() = 0;
538     virtual FastTrackUnderruns& fastTrackUnderruns() = 0;
539 
540     // Internal mute, this is currently only used for bit-perfect playback
541     virtual bool getInternalMute() const = 0;
542     virtual void setInternalMute(bool muted) = 0;
543 };
544 
545 // playback track, used by DuplicatingThread
546 class IAfOutputTrack : public virtual IAfTrack {
547 public:
548     static sp<IAfOutputTrack> create(
549             IAfPlaybackThread* playbackThread,
550             IAfDuplicatingThread* sourceThread, uint32_t sampleRate,
551             audio_format_t format, audio_channel_mask_t channelMask, size_t frameCount,
552             const AttributionSourceState& attributionSource);
553 
554     virtual ssize_t write(void* data, uint32_t frames) = 0;
555     virtual bool bufferQueueEmpty() const = 0;
556     virtual bool isActive() const = 0;
557 
558     /** Set the metadatas of the upstream tracks. Thread safe. */
559     virtual void setMetadatas(const SourceMetadatas& metadatas) = 0;
560     /** returns client timestamp to the upstream duplicating thread. */
561     virtual ExtendedTimestamp getClientProxyTimestamp() const = 0;
562 };
563 
564 class IAfMmapTrack : public virtual IAfTrackBase, public virtual AfPlaybackCommon {
565 public:
566     static sp<IAfMmapTrack> create(IAfThreadBase* thread,
567             const audio_attributes_t& attr,
568             uint32_t sampleRate,
569             audio_format_t format,
570             audio_channel_mask_t channelMask,
571             audio_session_t sessionId,
572             bool isOut,
573             const android::content::AttributionSourceState& attributionSource,
574             pid_t creatorPid,
575             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
576             float volume = 0.0f,
577             bool muted = false);
578 
getLogHeader()579     static constexpr std::string_view getLogHeader() {
580         using namespace std::literals;
581         return "Client(pid/uid) Session Port Id"
582                 "   Format Chn mask  SRate Flags Usg/Src PortVol dB PortMuted\n"sv;
583     };
584 
585     // protected by MMapThread::mLock
586     virtual void setSilenced_l(bool silenced) = 0;
587     // protected by MMapThread::mLock
588     virtual bool isSilenced_l() const = 0;
589     // protected by MMapThread::mLock
590     virtual bool getAndSetSilencedNotified_l() = 0;
591 };
592 
593 class RecordBufferConverter;
594 
595 class IAfRecordTrack : public virtual IAfTrackBase {
596 public:
597     // createIAudioRecordAdapter() is a static constructor which creates an
598     // IAudioRecord AIDL interface adapter from the RecordTrack object that
599     // may be passed back to the client (if needed).
600     //
601     // Only one AIDL IAudioRecord interface adapter should be created per RecordTrack.
602     static sp<media::IAudioRecord> createIAudioRecordAdapter(const sp<IAfRecordTrack>& recordTrack);
603 
604     static sp<IAfRecordTrack> create(IAfRecordThread* thread,
605             const sp<Client>& client,
606             const audio_attributes_t& attr,
607             uint32_t sampleRate,
608             audio_format_t format,
609             audio_channel_mask_t channelMask,
610             size_t frameCount,
611             void* buffer,
612             size_t bufferSize,
613             audio_session_t sessionId,
614             pid_t creatorPid,
615             const AttributionSourceState& attributionSource,
616             audio_input_flags_t flags,
617             track_type type,
618             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
619             int32_t startFrames = -1);
620 
getLogHeader()621     static constexpr std::string_view getLogHeader() {
622         using namespace std::literals;
623         return "Active     Id Client(pid/uid) Session Port Id  S  Flags  "
624                         " Format Chn mask  SRate Source  "
625                         " Server FrmCnt FrmRdy Sil   Latency\n"sv;
626     }
627 
628     // clear the buffer overflow flag
629     virtual void clearOverflow() = 0;
630     // set the buffer overflow flag and return previous value
631     virtual bool setOverflow() = 0;
632 
633     // TODO(b/291317964) handleSyncStartEvent in IAfTrackBase should move here.
634     virtual void clearSyncStartEvent() = 0;
635     virtual void updateTrackFrameInfo(
636             int64_t trackFramesReleased, int64_t sourceFramesRead, uint32_t halSampleRate,
637             const ExtendedTimestamp& timestamp) = 0;
638 
639     virtual void setSilenced(bool silenced) = 0;
640     virtual bool isSilenced() const = 0;
641     virtual status_t getActiveMicrophones(
642             std::vector<media::MicrophoneInfoFw>* activeMicrophones) const = 0;
643 
644     virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) = 0;
645     virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
646     virtual status_t shareAudioHistory(
647             const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) = 0;
648     virtual status_t setParameters(const String8& keyValuePairs) = 0;
649     virtual int32_t startFrames() const = 0;
650 
checkServerLatencySupported(audio_format_t format,audio_input_flags_t flags)651     static bool checkServerLatencySupported(audio_format_t format, audio_input_flags_t flags) {
652         return audio_is_linear_pcm(format) && (flags & AUDIO_INPUT_FLAG_HW_AV_SYNC) == 0;
653     }
654 
655     using SinkMetadatas = std::vector<record_track_metadata_v7_t>;
656     using MetadataInserter = std::back_insert_iterator<SinkMetadatas>;
657     virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0; // see IAfTrack
658 
659     // private to Threads
660     virtual AudioBufferProvider::Buffer& sinkBuffer() = 0;
661     virtual audioflinger::SynchronizedRecordState& synchronizedRecordState() = 0;
662     virtual RecordBufferConverter* recordBufferConverter() const = 0;
663     virtual ResamplerBufferProvider* resamplerBufferProvider() const = 0;
664 };
665 
666 // PatchProxyBufferProvider interface is implemented by PatchTrack and PatchRecord.
667 // it provides buffer access methods that map those of a ClientProxy (see AudioTrackShared.h)
668 class PatchProxyBufferProvider {
669 public:
670     virtual ~PatchProxyBufferProvider() = default;
671     virtual bool producesBufferOnDemand() const = 0;
672     virtual status_t obtainBuffer(
673             Proxy::Buffer* buffer, const struct timespec* requested = nullptr) = 0;
674     virtual void releaseBuffer(Proxy::Buffer* buffer) = 0;
675 };
676 
677 class IAfPatchTrackBase : public virtual RefBase {
678 public:
679     using Timeout = std::optional<std::chrono::nanoseconds>;
680 
681     virtual void setPeerTimeout(std::chrono::nanoseconds timeout) = 0;
682     virtual void setPeerProxy(const sp<IAfPatchTrackBase>& proxy, bool holdReference) = 0;
683     virtual void clearPeerProxy() = 0;
684     virtual PatchProxyBufferProvider* asPatchProxyBufferProvider() = 0;
685 };
686 
687 class IAfPatchTrack : public virtual IAfTrack, public virtual IAfPatchTrackBase {
688 public:
689     static sp<IAfPatchTrack> create(
690             IAfPlaybackThread* playbackThread,
691             audio_stream_type_t streamType,
692             uint32_t sampleRate,
693             audio_channel_mask_t channelMask,
694             audio_format_t format,
695             size_t frameCount,
696             void *buffer,
697             size_t bufferSize,
698             audio_output_flags_t flags,
699             const Timeout& timeout = {},
700             size_t frameCountToBeReady = 1, /** Default behaviour is to start
701                                              *  as soon as possible to have
702                                              *  the lowest possible latency
703                                              *  even if it might glitch. */
704             float speed = 1.0f,
705             float volume = 1.0f,
706             bool muted = false);
707 };
708 
709 class IAfPatchRecord : public virtual IAfRecordTrack, public virtual IAfPatchTrackBase {
710 public:
711     static sp<IAfPatchRecord> create(
712             IAfRecordThread* recordThread,
713             uint32_t sampleRate,
714             audio_channel_mask_t channelMask,
715             audio_format_t format,
716             size_t frameCount,
717             void* buffer,
718             size_t bufferSize,
719             audio_input_flags_t flags,
720             const Timeout& timeout = {},
721             audio_source_t source = AUDIO_SOURCE_DEFAULT);
722 
723     static sp<IAfPatchRecord> createPassThru(
724             IAfRecordThread* recordThread,
725             uint32_t sampleRate,
726             audio_channel_mask_t channelMask,
727             audio_format_t format,
728             size_t frameCount,
729             audio_input_flags_t flags,
730             audio_source_t source = AUDIO_SOURCE_DEFAULT);
731 
732     virtual Source* getSource() = 0;
733     virtual size_t writeFrames(const void* src, size_t frameCount, size_t frameSize) = 0;
734 };
735 
736 }  // namespace android
737