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