blob: 374f36199fc3b2c77581d57034649bb7cff19b24 [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef INCLUDING_FROM_AUDIOFLINGER_H
19 #error This header file should only be included from AudioFlinger.h
20#endif
21
Lais Andradebc3f37a2021-07-02 00:13:19 +010022#include <math.h>
Vlad Popa103be862023-07-10 20:27:41 -070023#include <sys/types.h>
Lais Andradebc3f37a2021-07-02 00:13:19 +010024
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080025// Checks and monitors OP_PLAY_AUDIO
26class OpPlayAudioMonitor : public RefBase {
Vlad Popa103be862023-07-10 20:27:41 -070027 friend class sp<OpPlayAudioMonitor>;
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080028public:
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080029 ~OpPlayAudioMonitor() override;
30 bool hasOpPlayAudio() const;
31
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -070032 static sp<OpPlayAudioMonitor> createIfNeeded(
Vlad Popa103be862023-07-10 20:27:41 -070033 AudioFlinger::ThreadBase* thread,
Svet Ganov33761132021-05-13 22:51:08 +000034 const AttributionSourceState& attributionSource,
Philip P. Moltmannbda45752020-07-17 16:41:18 -070035 const audio_attributes_t& attr, int id,
36 audio_stream_type_t streamType);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -070037
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080038private:
Vlad Popa103be862023-07-10 20:27:41 -070039 OpPlayAudioMonitor(AudioFlinger::ThreadBase* thread,
40 const AttributionSourceState& attributionSource,
41 audio_usage_t usage, int id, uid_t uid);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -070042 void onFirstRef() override;
Eric Laurent9066ad32019-05-20 14:40:10 -070043 static void getPackagesForUid(uid_t uid, Vector<String16>& packages);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -070044
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080045 AppOpsManager mAppOpsManager;
46
47 class PlayAudioOpCallback : public BnAppOpsCallback {
48 public:
49 explicit PlayAudioOpCallback(const wp<OpPlayAudioMonitor>& monitor);
50 void opChanged(int32_t op, const String16& packageName) override;
51
52 private:
53 const wp<OpPlayAudioMonitor> mMonitor;
54 };
55
56 sp<PlayAudioOpCallback> mOpCallback;
57 // called by PlayAudioOpCallback when OP_PLAY_AUDIO is updated in AppOp callback
Vlad Popad2152122023-08-02 18:36:04 -070058 void checkPlayAudioForUsage(bool doBroadcast);
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080059
Vlad Popa103be862023-07-10 20:27:41 -070060 wp<AudioFlinger::ThreadBase> mThread;
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080061 std::atomic_bool mHasOpPlayAudio;
Svet Ganov33761132021-05-13 22:51:08 +000062 const AttributionSourceState mAttributionSource;
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080063 const int32_t mUsage; // on purpose not audio_usage_t because always checked in appOps as int32_t
64 const int mId; // for logging purposes only
Vlad Popa103be862023-07-10 20:27:41 -070065 const uid_t mUid;
66 const String16 mPackageName;
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -080067};
68
Eric Laurent81784c32012-11-19 14:55:58 -080069// playback track
Andy Hung02a6c4e2023-06-23 19:27:19 -070070class Track : public TrackBase, public virtual IAfTrack, public VolumeProvider {
Eric Laurent81784c32012-11-19 14:55:58 -080071public:
72 Track( PlaybackThread *thread,
73 const sp<Client>& client,
74 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -070075 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -080076 uint32_t sampleRate,
77 audio_format_t format,
78 audio_channel_mask_t channelMask,
79 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -070080 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -070081 size_t bufferSize,
Eric Laurent81784c32012-11-19 14:55:58 -080082 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -080083 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -070084 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +000085 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -070086 audio_output_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -080087 track_type type,
Kevin Rocard01c7d9e2019-09-18 11:24:52 +010088 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
89 /** default behaviour is to start when there are as many frames
90 * ready as possible (aka. Buffer is full). */
jiabinf042b9b2021-05-07 23:46:28 +000091 size_t frameCountToBeReady = SIZE_MAX,
Eric Laurentb0a7bc92022-04-05 15:06:08 +020092 float speed = 1.0f,
jiabinc658e452022-10-21 20:52:21 +000093 bool isSpatialized = false,
94 bool isBitPerfect = false);
Andy Hung02a6c4e2023-06-23 19:27:19 -070095 ~Track() override;
96 status_t initCheck() const final;
97 void appendDumpHeader(String8& result) const final;
98 void appendDump(String8& result, bool active) const final;
99 status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
100 audio_session_t triggerSession = AUDIO_SESSION_NONE) override;
101 void stop() override;
102 void pause() final;
103 void flush() final;
104 void destroy() final;
105 uint32_t sampleRate() const final;
106 audio_stream_type_t streamType() const final {
Eric Laurent81784c32012-11-19 14:55:58 -0800107 return mStreamType;
108 }
Andy Hung02a6c4e2023-06-23 19:27:19 -0700109 bool isOffloaded() const final
Eric Laurent05067782016-06-01 18:27:28 -0700110 { return (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; }
Andy Hung02a6c4e2023-06-23 19:27:19 -0700111 bool isDirect() const final
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700112 { return (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0; }
Andy Hung02a6c4e2023-06-23 19:27:19 -0700113 bool isOffloadedOrDirect() const final { return (mFlags
Andy Hung10cbff12017-02-21 17:30:14 -0800114 & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
115 | AUDIO_OUTPUT_FLAG_DIRECT)) != 0; }
Andy Hung02a6c4e2023-06-23 19:27:19 -0700116 bool isStatic() const final { return mSharedBuffer.get() != nullptr; }
Andy Hung10cbff12017-02-21 17:30:14 -0800117
Andy Hung02a6c4e2023-06-23 19:27:19 -0700118 status_t setParameters(const String8& keyValuePairs) final;
119 status_t selectPresentation(int presentationId, int programId) final;
120 status_t attachAuxEffect(int EffectId) final;
121 void setAuxBuffer(int EffectId, int32_t* buffer) final;
122 int32_t* auxBuffer() const final { return mAuxBuffer; }
123 void setMainBuffer(float* buffer) final { mMainBuffer = buffer; }
124 float* mainBuffer() const final { return mMainBuffer; }
125 int auxEffectId() const final { return mAuxEffectId; }
126 status_t getTimestamp(AudioTimestamp& timestamp) final;
127 void signal() final;
128 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) const final;
129 status_t setDualMonoMode(audio_dual_mono_mode_t mode) final;
130 status_t getAudioDescriptionMixLevel(float* leveldB) const final;
131 status_t setAudioDescriptionMixLevel(float leveldB) final;
132 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) const final;
133 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800134
Andy Hung02a6c4e2023-06-23 19:27:19 -0700135 // implement FastMixerState::VolumeProvider interface
136 gain_minifloat_packed_t getVolumeLR() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800137
Andy Hung02a6c4e2023-06-23 19:27:19 -0700138 status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final;
139 bool isFastTrack() const final { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; }
140 double bufferLatencyMs() const final {
Andy Hungcef2daa2018-06-01 15:31:49 -0700141 return isStatic() ? 0. : TrackBase::bufferLatencyMs();
142 }
Andy Hungf6ab58d2018-05-25 12:50:39 -0700143
Andy Hung02a6c4e2023-06-23 19:27:19 -0700144 // implement volume handling.
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700145 media::VolumeShaper::Status applyVolumeShaper(
146 const sp<media::VolumeShaper::Configuration>& configuration,
147 const sp<media::VolumeShaper::Operation>& operation);
Andy Hung02a6c4e2023-06-23 19:27:19 -0700148 sp<media::VolumeShaper::State> getVolumeShaperState(int id) const final;
149 sp<media::VolumeHandler> getVolumeHandler() const final{ return mVolumeHandler; }
Kevin Rocard12381092018-04-11 09:19:59 -0700150 /** Set the computed normalized final volume of the track.
151 * !masterMute * masterVolume * streamVolume * averageLRVolume */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700152 void setFinalVolume(float volumeLeft, float volumeRight) final;
153 float getFinalVolume() const final { return mFinalVolume; }
154 void getFinalVolume(float* left, float* right) const final {
jiabin76d94692022-12-15 21:51:21 +0000155 *left = mFinalVolumeLeft;
156 *right = mFinalVolumeRight;
157 }
Kevin Rocard12381092018-04-11 09:19:59 -0700158
Eric Laurent94579172020-11-20 18:41:04 +0100159 using SourceMetadatas = std::vector<playback_track_metadata_v7_t>;
Kevin Rocard12381092018-04-11 09:19:59 -0700160 using MetadataInserter = std::back_insert_iterator<SourceMetadatas>;
161 /** Copy the track metadata in the provided iterator. Thread safe. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700162 void copyMetadataTo(MetadataInserter& backInserter) const override;
163
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800164
jiabin245cdd92018-12-07 17:55:15 -0800165 /** Return haptic playback of the track is enabled or not, used in mixer. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700166 bool getHapticPlaybackEnabled() const final { return mHapticPlaybackEnabled; }
jiabin245cdd92018-12-07 17:55:15 -0800167 /** Set haptic playback of the track is enabled or not, should be
168 * set after query or get callback from vibrator service */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700169 void setHapticPlaybackEnabled(bool hapticPlaybackEnabled) final {
jiabin245cdd92018-12-07 17:55:15 -0800170 mHapticPlaybackEnabled = hapticPlaybackEnabled;
171 }
jiabin77270b82018-12-18 15:41:29 -0800172 /** Return at what intensity to play haptics, used in mixer. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700173 os::HapticScale getHapticIntensity() const final { return mHapticIntensity; }
Lais Andradebc3f37a2021-07-02 00:13:19 +0100174 /** Return the maximum amplitude allowed for haptics data, used in mixer. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700175 float getHapticMaxAmplitude() const final { return mHapticMaxAmplitude; }
jiabin77270b82018-12-18 15:41:29 -0800176 /** Set intensity of haptic playback, should be set after querying vibrator service. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700177 void setHapticIntensity(os::HapticScale hapticIntensity) final {
jiabine70bc7f2020-06-30 22:07:55 -0700178 if (os::isValidHapticScale(hapticIntensity)) {
jiabin77270b82018-12-18 15:41:29 -0800179 mHapticIntensity = hapticIntensity;
jiabine70bc7f2020-06-30 22:07:55 -0700180 setHapticPlaybackEnabled(mHapticIntensity != os::HapticScale::MUTE);
jiabin77270b82018-12-18 15:41:29 -0800181 }
182 }
Lais Andradebc3f37a2021-07-02 00:13:19 +0100183 /** Set maximum amplitude allowed for haptic data, should be set after querying
184 * vibrator service.
185 */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700186 void setHapticMaxAmplitude(float maxAmplitude) final {
Lais Andradebc3f37a2021-07-02 00:13:19 +0100187 mHapticMaxAmplitude = maxAmplitude;
188 }
Andy Hung02a6c4e2023-06-23 19:27:19 -0700189 sp<os::ExternalVibration> getExternalVibration() const final { return mExternalVibration; }
jiabin245cdd92018-12-07 17:55:15 -0800190
Jiabin Huangfb476842022-12-06 03:18:10 +0000191 // This function should be called with holding thread lock.
Andy Hung02a6c4e2023-06-23 19:27:19 -0700192 void updateTeePatches() final;
193 void setTeePatchesToUpdate(const void* teePatchesToUpdate) final {
194 setTeePatchesToUpdate(*reinterpret_cast<const TeePatches*>(teePatchesToUpdate));
195 }
196 void setTeePatchesToUpdate(TeePatches teePatchesToUpdate);
Kevin Rocard153f92d2018-12-18 18:33:28 -0800197
Andy Hung02a6c4e2023-06-23 19:27:19 -0700198 void tallyUnderrunFrames(size_t frames) final {
Andy Hungc2b11cb2020-04-22 09:04:01 -0700199 if (isOut()) { // we expect this from output tracks only
200 mAudioTrackServerProxy->tallyUnderrunFrames(frames);
201 // Fetch absolute numbers from AudioTrackShared as it counts
202 // contiguous underruns as a one -- we want a consistent number.
203 // TODO: isolate this counting into a class.
204 mTrackMetrics.logUnderruns(mAudioTrackServerProxy->getUnderrunCount(),
205 mAudioTrackServerProxy->getUnderrunFrames());
206 }
207 }
jiabinf042b9b2021-05-07 23:46:28 +0000208
Andy Hung02a6c4e2023-06-23 19:27:19 -0700209 audio_output_flags_t getOutputFlags() const final { return mFlags; }
210 float getSpeed() const final { return mSpeed; }
211 bool isSpatialized() const final { return mIsSpatialized; }
212 bool isBitPerfect() const final { return mIsBitPerfect; }
Eric Laurent39095982021-08-24 18:29:27 +0200213
Vlad Popae8d99472022-06-30 16:02:48 +0200214 /**
215 * Updates the mute state and notifies the audio service. Call this only when holding player
216 * thread lock.
217 */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700218 void processMuteEvent_l(const sp<IAudioManager>& audioManager, mute_state_t muteState) final;
Eric Laurent39095982021-08-24 18:29:27 +0200219
Eric Laurent81784c32012-11-19 14:55:58 -0800220protected:
221 // for numerous
222 friend class PlaybackThread;
223 friend class MixerThread;
224 friend class DirectOutputThread;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800225 friend class OffloadThread;
Eric Laurent81784c32012-11-19 14:55:58 -0800226
Mikhail Naganovbf493082017-04-17 17:37:12 -0700227 DISALLOW_COPY_AND_ASSIGN(Track);
Eric Laurent81784c32012-11-19 14:55:58 -0800228
229 // AudioBufferProvider interface
Kevin Rocard153f92d2018-12-18 18:33:28 -0800230 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override;
231 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override;
Eric Laurent81784c32012-11-19 14:55:58 -0800232
Glenn Kasten6466c9e2013-08-23 10:54:07 -0700233 // ExtendedAudioBufferProvider interface
Andy Hung02a6c4e2023-06-23 19:27:19 -0700234 size_t framesReady() const override;
235 int64_t framesReleased() const override;
236 void onTimestamp(const ExtendedTimestamp &timestamp) override;
Eric Laurent81784c32012-11-19 14:55:58 -0800237
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800238 bool isPausing() const { return mState == PAUSING; }
239 bool isPaused() const { return mState == PAUSED; }
240 bool isResuming() const { return mState == RESUMING; }
Eric Laurent81784c32012-11-19 14:55:58 -0800241 bool isReady() const;
242 void setPaused() { mState = PAUSED; }
243 void reset();
Haynes Mathew George7844f672014-01-15 12:32:55 -0800244 bool isFlushPending() const { return mFlushHwPending; }
245 void flushAck();
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -0800246 bool isResumePending();
247 void resumeAck();
Kuowei Li23666472021-01-20 10:23:25 +0800248 // For direct or offloaded tracks ensure that the pause state is acknowledged
249 // by the playback thread in case of an immediate flush.
250 bool isPausePending() const { return mPauseHwPending; }
251 void pauseAck();
Andy Hung818e7a32016-02-16 18:08:07 -0800252 void updateTrackFrameInfo(int64_t trackFramesReleased, int64_t sinkFramesWritten,
Andy Hungcef2daa2018-06-01 15:31:49 -0700253 uint32_t halSampleRate, const ExtendedTimestamp &timeStamp);
Eric Laurent81784c32012-11-19 14:55:58 -0800254
Eric Laurent81784c32012-11-19 14:55:58 -0800255 sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
256
Andy Hung59de4262021-06-14 10:53:54 -0700257 // presentationComplete checked by frames. (Mixed Tracks).
Eric Laurent81784c32012-11-19 14:55:58 -0800258 // framesWritten is cumulative, never reset, and is shared all tracks
259 // audioHalFrames is derived from output latency
Andy Hung818e7a32016-02-16 18:08:07 -0800260 bool presentationComplete(int64_t framesWritten, size_t audioHalFrames);
Andy Hung59de4262021-06-14 10:53:54 -0700261
262 // presentationComplete checked by time. (Direct Tracks).
263 bool presentationComplete(uint32_t latencyMs);
264
265 void resetPresentationComplete() {
266 mPresentationCompleteFrames = 0;
267 mPresentationCompleteTimeNs = 0;
268 }
269
270 // notifyPresentationComplete is called when presentationComplete() detects
271 // that the track is finished stopping.
272 void notifyPresentationComplete();
273
Eric Laurent4d231dc2016-03-11 18:38:23 -0800274 void signalClientFlag(int32_t flag);
Eric Laurent81784c32012-11-19 14:55:58 -0800275
276public:
Andy Hung02a6c4e2023-06-23 19:27:19 -0700277 void triggerEvents(AudioSystem::sync_event_t type) final;
278 void invalidate() final;
279 void disable() final;
280 int& fastIndex() final { return mFastIndex; }
281 bool isPlaybackRestricted() const final {
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700282 // The monitor is only created for tracks that can be silenced.
283 return mOpPlayAudioMonitor ? !mOpPlayAudioMonitor->hasOpPlayAudio() : false; }
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800284
Eric Laurent81784c32012-11-19 14:55:58 -0800285protected:
286
Eric Laurent81784c32012-11-19 14:55:58 -0800287 // FILLED state is used for suppressing volume ramp at begin of playing
288 enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE};
289 mutable uint8_t mFillingUpStatus;
290 int8_t mRetryCount;
Glenn Kasten0c72b242013-09-11 09:14:16 -0700291
292 // see comment at AudioFlinger::PlaybackThread::Track::~Track for why this can't be const
293 sp<IMemory> mSharedBuffer;
294
Eric Laurent81784c32012-11-19 14:55:58 -0800295 bool mResetDone;
296 const audio_stream_type_t mStreamType;
Andy Hung319587b2023-05-23 14:01:03 -0700297 float *mMainBuffer;
rago94a1ee82017-07-21 15:11:02 -0700298
Eric Laurent81784c32012-11-19 14:55:58 -0800299 int32_t *mAuxBuffer;
300 int mAuxEffectId;
301 bool mHasVolumeController;
Andy Hunge10393e2015-06-12 13:59:33 -0700302
303 // access these three variables only when holding thread lock.
Andy Hung818e7a32016-02-16 18:08:07 -0800304 LinearMap<int64_t> mFrameMap; // track frame to server frame mapping
305
306 ExtendedTimestamp mSinkTimestamp;
Andy Hunge10393e2015-06-12 13:59:33 -0700307
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700308 sp<media::VolumeHandler> mVolumeHandler; // handles multiple VolumeShaper configs and operations
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800309
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800310 sp<OpPlayAudioMonitor> mOpPlayAudioMonitor;
311
jiabin245cdd92018-12-07 17:55:15 -0800312 bool mHapticPlaybackEnabled = false; // indicates haptic playback enabled or not
jiabin77270b82018-12-18 15:41:29 -0800313 // intensity to play haptic data
jiabine70bc7f2020-06-30 22:07:55 -0700314 os::HapticScale mHapticIntensity = os::HapticScale::MUTE;
Lais Andradebc3f37a2021-07-02 00:13:19 +0100315 // max amplitude allowed for haptic data
316 float mHapticMaxAmplitude = NAN;
jiabin57303cc2018-12-18 15:45:57 -0800317 class AudioVibrationController : public os::BnExternalVibrationController {
318 public:
319 explicit AudioVibrationController(Track* track) : mTrack(track) {}
320 binder::Status mute(/*out*/ bool *ret) override;
321 binder::Status unmute(/*out*/ bool *ret) override;
322 private:
323 Track* const mTrack;
SPeak Shen0db56b32022-11-11 00:28:50 +0800324 bool setMute(bool muted);
jiabin57303cc2018-12-18 15:45:57 -0800325 };
326 sp<AudioVibrationController> mAudioVibrationController;
327 sp<os::ExternalVibration> mExternalVibration;
jiabin245cdd92018-12-07 17:55:15 -0800328
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800329 audio_dual_mono_mode_t mDualMonoMode = AUDIO_DUAL_MONO_MODE_OFF;
330 float mAudioDescriptionMixLevel = -std::numeric_limits<float>::infinity();
331 audio_playback_rate_t mPlaybackRateParameters = AUDIO_PLAYBACK_RATE_INITIALIZER;
332
Eric Laurent81784c32012-11-19 14:55:58 -0800333private:
Kevin Rocard153f92d2018-12-18 18:33:28 -0800334 void interceptBuffer(const AudioBufferProvider::Buffer& buffer);
Kevin Rocardc43ea142019-01-31 18:17:37 -0800335 template <class F>
336 void forEachTeePatchTrack(F f) {
337 for (auto& tp : mTeePatches) { f(tp.patchTrack); }
338 };
Kevin Rocard153f92d2018-12-18 18:33:28 -0800339
Andy Hung59de4262021-06-14 10:53:54 -0700340 size_t mPresentationCompleteFrames = 0; // (Used for Mixed tracks)
341 // The number of frames written to the
342 // audio HAL when this track is considered fully rendered.
343 // Zero means not monitoring.
344 int64_t mPresentationCompleteTimeNs = 0; // (Used for Direct tracks)
345 // The time when this track is considered fully rendered.
346 // Zero means not monitoring.
347
Eric Laurent81784c32012-11-19 14:55:58 -0800348 // The following fields are only for fast tracks, and should be in a subclass
349 int mFastIndex; // index within FastMixerState::mFastTracks[];
350 // either mFastIndex == -1 if not isFastTrack()
351 // or 0 < mFastIndex < FastMixerState::kMaxFast because
352 // index 0 is reserved for normal mixer's submix;
353 // index is allocated statically at track creation time
354 // but the slot is only used if track is active
355 FastTrackUnderruns mObservedUnderruns; // Most recently observed value of
356 // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns
Eric Laurent81784c32012-11-19 14:55:58 -0800357 volatile float mCachedVolume; // combined master volume and stream type volume;
358 // 'volatile' means accessed without lock or
359 // barrier, but is read/written atomically
Kevin Rocard12381092018-04-11 09:19:59 -0700360 float mFinalVolume; // combine master volume, stream type volume and track volume
jiabin76d94692022-12-15 21:51:21 +0000361 float mFinalVolumeLeft; // combine master volume, stream type volume and track
362 // volume
363 float mFinalVolumeRight; // combine master volume, stream type volume and track
364 // volume
Eric Laurent5bba2f62016-03-18 11:14:14 -0700365 sp<AudioTrackServerProxy> mAudioTrackServerProxy;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800366 bool mResumeToStopping; // track was paused in stopping state.
Haynes Mathew George7844f672014-01-15 12:32:55 -0800367 bool mFlushHwPending; // track requests for thread flush
Kuowei Li23666472021-01-20 10:23:25 +0800368 bool mPauseHwPending = false; // direct/offload track request for thread pause
Eric Laurent05067782016-06-01 18:27:28 -0700369 audio_output_flags_t mFlags;
Kevin Rocard153f92d2018-12-18 18:33:28 -0800370 TeePatches mTeePatches;
Jiabin Huangfb476842022-12-06 03:18:10 +0000371 std::optional<TeePatches> mTeePatchesToUpdate;
jiabinf042b9b2021-05-07 23:46:28 +0000372 const float mSpeed;
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200373 const bool mIsSpatialized;
jiabinc658e452022-10-21 20:52:21 +0000374 const bool mIsBitPerfect;
Vlad Popae8d99472022-06-30 16:02:48 +0200375
376 // TODO: replace PersistableBundle with own struct
377 // access these two variables only when holding player thread lock.
378 std::unique_ptr<os::PersistableBundle> mMuteEventExtras;
379 mute_state_t mMuteState;
Eric Laurent81784c32012-11-19 14:55:58 -0800380}; // end of Track
381
Eric Laurent81784c32012-11-19 14:55:58 -0800382
383// playback track, used by DuplicatingThread
Andy Hung02a6c4e2023-06-23 19:27:19 -0700384class OutputTrack : public Track, public IAfOutputTrack {
Eric Laurent81784c32012-11-19 14:55:58 -0800385public:
386
387 class Buffer : public AudioBufferProvider::Buffer {
388 public:
Andy Hungc25b84a2015-01-14 19:04:10 -0800389 void *mBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800390 };
391
392 OutputTrack(PlaybackThread *thread,
393 DuplicatingThread *sourceThread,
394 uint32_t sampleRate,
395 audio_format_t format,
396 audio_channel_mask_t channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800397 size_t frameCount,
Svet Ganov33761132021-05-13 22:51:08 +0000398 const AttributionSourceState& attributionSource);
Andy Hung02a6c4e2023-06-23 19:27:19 -0700399 ~OutputTrack() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800400
Andy Hung02a6c4e2023-06-23 19:27:19 -0700401 status_t start(AudioSystem::sync_event_t event =
Eric Laurent81784c32012-11-19 14:55:58 -0800402 AudioSystem::SYNC_EVENT_NONE,
Andy Hung02a6c4e2023-06-23 19:27:19 -0700403 audio_session_t triggerSession = AUDIO_SESSION_NONE) final;
404 void stop() final;
405 ssize_t write(void* data, uint32_t frames) final;
406 bool bufferQueueEmpty() const final { return mBufferQueue.size() == 0; }
407 bool isActive() const final { return mActive; }
Eric Laurent81784c32012-11-19 14:55:58 -0800408
Andy Hung02a6c4e2023-06-23 19:27:19 -0700409 void copyMetadataTo(MetadataInserter& backInserter) const final;
Kevin Rocard12381092018-04-11 09:19:59 -0700410 /** Set the metadatas of the upstream tracks. Thread safe. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700411 void setMetadatas(const SourceMetadatas& metadatas) final;
Andy Hung1c86ebe2018-05-29 20:29:08 -0700412 /** returns client timestamp to the upstream duplicating thread. */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700413 ExtendedTimestamp getClientProxyTimestamp() const final {
Andy Hung1c86ebe2018-05-29 20:29:08 -0700414 // server - kernel difference is not true latency when drained
415 // i.e. mServerProxy->isDrained().
416 ExtendedTimestamp timestamp;
417 (void) mClientProxy->getTimestamp(&timestamp);
418 // On success, the timestamp LOCATION_SERVER and LOCATION_KERNEL
419 // entries will be properly filled. If getTimestamp()
420 // is unsuccessful, then a default initialized timestamp
421 // (with mTimeNs[] filled with -1's) is returned.
422 return timestamp;
423 }
Kevin Rocard12381092018-04-11 09:19:59 -0700424private:
Eric Laurent81784c32012-11-19 14:55:58 -0800425 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer,
426 uint32_t waitTimeMs);
Eric Laurent19952e12023-04-20 10:08:29 +0200427 void queueBuffer(Buffer& inBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -0800428 void clearBufferQueue();
429
Eric Laurent4d231dc2016-03-11 18:38:23 -0800430 void restartIfDisabled();
431
Eric Laurent81784c32012-11-19 14:55:58 -0800432 // Maximum number of pending buffers allocated by OutputTrack::write()
433 static const uint8_t kMaxOverFlowBuffers = 10;
434
435 Vector < Buffer* > mBufferQueue;
436 AudioBufferProvider::Buffer mOutBuffer;
437 bool mActive;
Eric Laurent5bba2f62016-03-18 11:14:14 -0700438 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
439 sp<AudioTrackClientProxy> mClientProxy;
Andy Hung1c86ebe2018-05-29 20:29:08 -0700440
Kevin Rocard12381092018-04-11 09:19:59 -0700441 /** Attributes of the source tracks.
442 *
443 * This member must be accessed with mTrackMetadatasMutex taken.
444 * There is one writer (duplicating thread) and one reader (downstream mixer).
445 *
446 * That means that the duplicating thread can block the downstream mixer
447 * thread and vice versa for the time of the copy.
448 * If this becomes an issue, the metadata could be stored in an atomic raw pointer,
449 * and a exchange with nullptr and delete can be used.
450 * Alternatively a read-copy-update might be implemented.
451 */
452 SourceMetadatas mTrackMetadatas;
453 /** Protects mTrackMetadatas against concurrent access. */
454 mutable std::mutex mTrackMetadatasMutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800455}; // end of OutputTrack
Eric Laurent83b88082014-06-20 18:31:16 -0700456
457// playback track, used by PatchPanel
Andy Hung4d85ad02023-06-26 19:20:57 -0700458class PatchTrack : public Track, public PatchTrackBase, public IAfPatchTrack {
Eric Laurent83b88082014-06-20 18:31:16 -0700459public:
460
461 PatchTrack(PlaybackThread *playbackThread,
Eric Laurent3bcf8592015-04-03 12:13:24 -0700462 audio_stream_type_t streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700463 uint32_t sampleRate,
464 audio_channel_mask_t channelMask,
465 audio_format_t format,
466 size_t frameCount,
467 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -0700468 size_t bufferSize,
Kevin Rocard45986c72018-12-18 18:22:59 -0800469 audio_output_flags_t flags,
Kevin Rocard01c7d9e2019-09-18 11:24:52 +0100470 const Timeout& timeout = {},
471 size_t frameCountToBeReady = 1 /** Default behaviour is to start
472 * as soon as possible to have
473 * the lowest possible latency
474 * even if it might glitch. */);
Andy Hung02a6c4e2023-06-23 19:27:19 -0700475 ~PatchTrack() override;
Eric Laurent83b88082014-06-20 18:31:16 -0700476
Andy Hung02a6c4e2023-06-23 19:27:19 -0700477 size_t framesReady() const final;
Mikhail Naganovcaf59942019-09-25 14:05:29 -0700478
Andy Hung02a6c4e2023-06-23 19:27:19 -0700479 status_t start(AudioSystem::sync_event_t event =
Eric Laurent4d231dc2016-03-11 18:38:23 -0800480 AudioSystem::SYNC_EVENT_NONE,
Andy Hung02a6c4e2023-06-23 19:27:19 -0700481 audio_session_t triggerSession = AUDIO_SESSION_NONE) final;
Eric Laurent4d231dc2016-03-11 18:38:23 -0800482
Eric Laurent83b88082014-06-20 18:31:16 -0700483 // AudioBufferProvider interface
Andy Hung02a6c4e2023-06-23 19:27:19 -0700484 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) final;
485 void releaseBuffer(AudioBufferProvider::Buffer* buffer) final;
Eric Laurent83b88082014-06-20 18:31:16 -0700486
487 // PatchProxyBufferProvider interface
Andy Hung02a6c4e2023-06-23 19:27:19 -0700488 status_t obtainBuffer(Proxy::Buffer* buffer, const struct timespec* timeOut = nullptr) final;
489 void releaseBuffer(Proxy::Buffer* buffer) final;
Eric Laurent83b88082014-06-20 18:31:16 -0700490
Eric Laurent83b88082014-06-20 18:31:16 -0700491private:
Eric Laurent4d231dc2016-03-11 18:38:23 -0800492 void restartIfDisabled();
Eric Laurent83b88082014-06-20 18:31:16 -0700493}; // end of PatchTrack