Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 18 | #pragma once |
| 19 | |
Andy Hung | 472f127 | 2023-07-19 11:40:07 -0700 | [diff] [blame] | 20 | #include "Configuration.h" // TEE_SINK |
| 21 | #include "IAfTrack.h" |
| 22 | |
| 23 | #include <afutils/NBAIO_Tee.h> |
| 24 | #include <android-base/macros.h> // DISALLOW_COPY_AND_ASSIGN |
| 25 | #include <datapath/TrackMetrics.h> |
| 26 | #include <mediautils/BatteryNotifier.h> |
| 27 | |
| 28 | #include <atomic> // avoid transitive dependency |
| 29 | #include <list> // avoid transitive dependency |
| 30 | #include <optional> // avoid transitive dependency |
| 31 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 32 | namespace android { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 33 | |
| 34 | // base for record and playback |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 35 | class TrackBase : public ExtendedAudioBufferProvider, public virtual IAfTrackBase { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 36 | public: |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 37 | TrackBase(IAfThreadBase* thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 38 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 39 | const audio_attributes_t& mAttr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | uint32_t sampleRate, |
| 41 | audio_format_t format, |
| 42 | audio_channel_mask_t channelMask, |
| 43 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 44 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 45 | size_t bufferSize, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 46 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 47 | pid_t creatorPid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 48 | uid_t uid, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 49 | bool isOut, |
Dmitry Sidorenkov | a41c273 | 2023-05-15 13:47:07 -0700 | [diff] [blame] | 50 | const alloc_type alloc = ALLOC_CBLK, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 51 | track_type type = TYPE_DEFAULT, |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 52 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE, |
| 53 | std::string metricsId = {}); |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 54 | ~TrackBase() override; |
| 55 | status_t initCheck() const override; |
| 56 | sp<IMemory> getCblk() const final { return mCblkMemory; } |
| 57 | audio_track_cblk_t* cblk() const final { return mCblk; } |
| 58 | audio_session_t sessionId() const final { return mSessionId; } |
| 59 | uid_t uid() const final { return mUid; } |
| 60 | pid_t creatorPid() const final { return mCreatorPid; } |
| 61 | audio_port_handle_t portId() const final { return mPortId; } |
| 62 | status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) override; |
| 63 | track_state state() const final { return mState; } |
| 64 | void setState(track_state state) final { mState = state; } |
| 65 | sp<IMemory> getBuffers() const final { return mBufferMemory; } |
| 66 | void* buffer() const final { return mBuffer; } |
| 67 | size_t bufferSize() const final { return mBufferSize; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 68 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 69 | bool isOutputTrack() const final { return (mType == TYPE_OUTPUT); } |
| 70 | bool isPatchTrack() const final { return (mType == TYPE_PATCH); } |
| 71 | bool isExternalTrack() const final { return !isOutputTrack() && !isPatchTrack(); } |
| 72 | void invalidate() override { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 73 | if (mIsInvalid) return; |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 74 | mTrackMetrics.logInvalidate(); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 75 | mIsInvalid = true; |
| 76 | } |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 77 | bool isInvalid() const final { return mIsInvalid; } |
| 78 | void terminate() final { mTerminated = true; } |
| 79 | bool isTerminated() const final { return mTerminated; } |
| 80 | audio_attributes_t attributes() const final { return mAttr; } |
| 81 | bool isSpatialized() const override { return false; } |
| 82 | bool isBitPerfect() const override { return false; } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 83 | |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 84 | wp<IAfThreadBase> thread() const final { return mThread; } |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 85 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 86 | const sp<ServerProxy>& serverProxy() const final { return mServerProxy; } |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 87 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 88 | #ifdef TEE_SINK |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 89 | void dumpTee(int fd, const std::string &reason) const final { |
| 90 | mTee.dump(fd, reason); |
| 91 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 92 | #endif |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 93 | /** returns the buffer contents size converted to time in milliseconds |
| 94 | * for PCM Playback or Record streaming tracks. The return value is zero for |
| 95 | * PCM static tracks and not defined for non-PCM tracks. |
| 96 | * |
| 97 | * This may be called without the thread lock. |
| 98 | */ |
| 99 | double bufferLatencyMs() const override { |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 100 | return mServerProxy->framesReadySafe() * 1000. / sampleRate(); |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 103 | /** returns whether the track supports server latency computation. |
| 104 | * This is set in the constructor and constant throughout the track lifetime. |
| 105 | */ |
| 106 | bool isServerLatencySupported() const final { return mServerLatencySupported; } |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 107 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 108 | /** computes the server latency for PCM Playback or Record track |
| 109 | * to the device sink/source. This is the time for the next frame in the track buffer |
| 110 | * written or read from the server thread to the device source or sink. |
| 111 | * |
| 112 | * This may be called without the thread lock, but latencyMs and fromTrack |
| 113 | * may be not be synchronized. For example PatchPanel may not obtain the |
| 114 | * thread lock before calling. |
| 115 | * |
| 116 | * \param latencyMs on success is set to the latency in milliseconds of the |
| 117 | * next frame written/read by the server thread to/from the track buffer |
| 118 | * from the device source/sink. |
| 119 | * \param fromTrack on success is set to true if latency was computed directly |
| 120 | * from the track timestamp; otherwise set to false if latency was |
| 121 | * estimated from the server timestamp. |
| 122 | * fromTrack may be nullptr or omitted if not required. |
| 123 | * |
| 124 | * \returns OK or INVALID_OPERATION on failure. |
| 125 | */ |
| 126 | status_t getServerLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const final { |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 127 | if (!isServerLatencySupported()) { |
| 128 | return INVALID_OPERATION; |
| 129 | } |
| 130 | |
| 131 | // if no thread lock is acquired, these atomics are not |
| 132 | // synchronized with each other, considered a benign race. |
| 133 | |
| 134 | const double serverLatencyMs = mServerLatencyMs.load(); |
| 135 | if (serverLatencyMs == 0.) { |
| 136 | return INVALID_OPERATION; |
| 137 | } |
| 138 | if (fromTrack != nullptr) { |
| 139 | *fromTrack = mServerLatencyFromTrack.load(); |
| 140 | } |
| 141 | *latencyMs = serverLatencyMs; |
| 142 | return OK; |
| 143 | } |
| 144 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 145 | /** computes the total client latency for PCM Playback or Record tracks |
| 146 | * for the next client app access to the device sink/source; i.e. the |
| 147 | * server latency plus the buffer latency. |
| 148 | * |
| 149 | * This may be called without the thread lock, but latencyMs and fromTrack |
| 150 | * may be not be synchronized. For example PatchPanel may not obtain the |
| 151 | * thread lock before calling. |
| 152 | * |
| 153 | * \param latencyMs on success is set to the latency in milliseconds of the |
| 154 | * next frame written/read by the client app to/from the track buffer |
| 155 | * from the device sink/source. |
| 156 | * \param fromTrack on success is set to true if latency was computed directly |
| 157 | * from the track timestamp; otherwise set to false if latency was |
| 158 | * estimated from the server timestamp. |
| 159 | * fromTrack may be nullptr or omitted if not required. |
| 160 | * |
| 161 | * \returns OK or INVALID_OPERATION on failure. |
| 162 | */ |
| 163 | status_t getTrackLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const { |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 164 | double serverLatencyMs; |
| 165 | status_t status = getServerLatencyMs(&serverLatencyMs, fromTrack); |
| 166 | if (status == OK) { |
| 167 | *latencyMs = serverLatencyMs + bufferLatencyMs(); |
| 168 | } |
| 169 | return status; |
| 170 | } |
| 171 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 172 | // KernelFrameTime is updated per "mix" period even for non-pcm tracks. |
| 173 | void getKernelFrameTime(FrameTime* ft) const final { |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 174 | *ft = mKernelFrameTime.load(); |
| 175 | } |
| 176 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 177 | audio_format_t format() const final { return mFormat; } |
| 178 | int id() const final { return mId; } |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 179 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 180 | const char* getTrackStateAsString() const final { |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 181 | if (isTerminated()) { |
| 182 | return "TERMINATED"; |
| 183 | } |
| 184 | switch (mState) { |
| 185 | case IDLE: |
| 186 | return "IDLE"; |
| 187 | case STOPPING_1: // for Fast and Offload |
| 188 | return "STOPPING_1"; |
| 189 | case STOPPING_2: // for Fast and Offload |
| 190 | return "STOPPING_2"; |
| 191 | case STOPPED: |
| 192 | return "STOPPED"; |
| 193 | case RESUMING: |
| 194 | return "RESUMING"; |
| 195 | case ACTIVE: |
| 196 | return "ACTIVE"; |
| 197 | case PAUSING: |
| 198 | return "PAUSING"; |
| 199 | case PAUSED: |
| 200 | return "PAUSED"; |
| 201 | case FLUSHED: |
| 202 | return "FLUSHED"; |
| 203 | case STARTING_1: // for RecordTrack |
| 204 | return "STARTING_1"; |
| 205 | case STARTING_2: // for RecordTrack |
| 206 | return "STARTING_2"; |
| 207 | default: |
| 208 | return "UNKNOWN"; |
| 209 | } |
| 210 | } |
| 211 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 212 | // Called by the PlaybackThread to indicate that the track is becoming active |
| 213 | // and a new interval should start with a given device list. |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 214 | void logBeginInterval(const std::string& devices) final { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 215 | mTrackMetrics.logBeginInterval(devices); |
| 216 | } |
| 217 | |
| 218 | // Called by the PlaybackThread to indicate the track is no longer active. |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 219 | void logEndInterval() final { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 220 | mTrackMetrics.logEndInterval(); |
| 221 | } |
| 222 | |
| 223 | // Called to tally underrun frames in playback. |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 224 | void tallyUnderrunFrames(size_t /* frames */) override {} |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 225 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 226 | audio_channel_mask_t channelMask() const final { return mChannelMask; } |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 227 | |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 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 | */ |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 234 | bool readAndClearHasChanged() final { return !mChangeNotified.test_and_set(); } |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 235 | |
| 236 | /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */ |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 237 | void setMetadataHasChanged() final { mChangeNotified.clear(); } |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 238 | |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 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 | */ |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 243 | void beginBatteryAttribution() final { |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 244 | mBatteryStatsHolder.emplace(uid()); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Called when a track moves out of the active state to record its contribution |
| 249 | * to battery usage. |
| 250 | */ |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 251 | void endBatteryAttribution() final { |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 252 | mBatteryStatsHolder.reset(); |
| 253 | } |
| 254 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 255 | protected: |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 256 | DISALLOW_COPY_AND_ASSIGN(TrackBase); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 257 | |
Andy Hung | 689e82c | 2019-08-21 17:53:17 -0700 | [diff] [blame] | 258 | void releaseCblk() { |
| 259 | if (mCblk != nullptr) { |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame] | 260 | mState.clear(); |
Andy Hung | 689e82c | 2019-08-21 17:53:17 -0700 | [diff] [blame] | 261 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 262 | if (mClient == 0) { |
| 263 | free(mCblk); |
| 264 | } |
| 265 | mCblk = nullptr; |
| 266 | } |
| 267 | } |
| 268 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 269 | // AudioBufferProvider interface |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 270 | // status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override; |
| 271 | void releaseBuffer(AudioBufferProvider::Buffer* buffer) override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 272 | |
| 273 | // ExtendedAudioBufferProvider interface is only needed for Track, |
| 274 | // but putting it in TrackBase avoids the complexity of virtual inheritance |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 275 | size_t framesReady() const override { return SIZE_MAX; } // MmapTrack doesn't implement. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 276 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 277 | uint32_t channelCount() const { return mChannelCount; } |
| 278 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 279 | size_t frameSize() const final { return mFrameSize; } |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 280 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 281 | uint32_t sampleRate() const override { return mSampleRate; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 282 | |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 283 | bool isStopped() const final { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 284 | return (mState == STOPPED || mState == FLUSHED); |
| 285 | } |
| 286 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 287 | // for fast tracks and offloaded tracks only |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 288 | bool isStopping() const final { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 289 | return mState == STOPPING_1 || mState == STOPPING_2; |
| 290 | } |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 291 | bool isStopping_1() const final { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 292 | return mState == STOPPING_1; |
| 293 | } |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 294 | bool isStopping_2() const final { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 295 | return mState == STOPPING_2; |
| 296 | } |
| 297 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 298 | // Upper case characters are final states. |
| 299 | // Lower case characters are transitory. |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 300 | const char *getTrackStateAsCodedString() const { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 301 | if (isTerminated()) { |
| 302 | return "T "; |
| 303 | } |
| 304 | switch (mState) { |
| 305 | case IDLE: |
| 306 | return "I "; |
| 307 | case STOPPING_1: // for Fast and Offload |
| 308 | return "s1"; |
| 309 | case STOPPING_2: // for Fast and Offload |
| 310 | return "s2"; |
| 311 | case STOPPED: |
| 312 | return "S "; |
| 313 | case RESUMING: |
| 314 | return "r "; |
| 315 | case ACTIVE: |
| 316 | return "A "; |
| 317 | case PAUSING: |
| 318 | return "p "; |
| 319 | case PAUSED: |
| 320 | return "P "; |
| 321 | case FLUSHED: |
| 322 | return "F "; |
| 323 | case STARTING_1: // for RecordTrack |
| 324 | return "r1"; |
| 325 | case STARTING_2: // for RecordTrack |
| 326 | return "r2"; |
| 327 | default: |
| 328 | return "? "; |
| 329 | } |
| 330 | } |
| 331 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 332 | bool isOut() const { return mIsOut; } |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 333 | // true for Track, false for RecordTrack, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 334 | // this could be a track type if needed later |
| 335 | |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 336 | void deferRestartIfDisabled(); |
| 337 | virtual void restartIfDisabled() {} |
| 338 | |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 339 | const wp<IAfThreadBase> mThread; |
Dmitry Sidorenkov | a41c273 | 2023-05-15 13:47:07 -0700 | [diff] [blame] | 340 | const alloc_type mAllocType; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 341 | /*const*/ sp<Client> mClient; // see explanation at ~TrackBase() why not const |
| 342 | sp<IMemory> mCblkMemory; |
| 343 | audio_track_cblk_t* mCblk; |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 344 | sp<IMemory> mBufferMemory; // currently non-0 for fast RecordTrack only |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 345 | void* mBuffer; // start of track buffer, typically in shared memory |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 346 | // except for OutputTrack when it is in local memory |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 347 | size_t mBufferSize; // size of mBuffer in bytes |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 348 | // we don't really need a lock for these |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame] | 349 | MirroredVariable<track_state> mState; |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 350 | audio_attributes_t mAttr; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 351 | const uint32_t mSampleRate; // initial sample rate only; for tracks which |
| 352 | // support dynamic rates, the current value is in control block |
| 353 | const audio_format_t mFormat; |
| 354 | const audio_channel_mask_t mChannelMask; |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 355 | const uint32_t mChannelCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 356 | const size_t mFrameSize; // AudioFlinger's view of frame size in shared memory, |
| 357 | // where for AudioTrack (but not AudioRecord), |
| 358 | // 8-bit PCM samples are stored as 16-bit |
| 359 | const size_t mFrameCount;// size of track buffer given at createTrack() or |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 360 | // createRecord(), and then adjusted as needed |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 361 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 362 | const audio_session_t mSessionId; |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 363 | uid_t mUid; |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 364 | std::list<sp<audioflinger::SyncEvent>> mSyncEvents; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 365 | const bool mIsOut; |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 366 | sp<ServerProxy> mServerProxy; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 367 | const int mId; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 368 | #ifdef TEE_SINK |
| 369 | NBAIO_Tee mTee; |
| 370 | #endif |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 371 | bool mTerminated; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 372 | track_type mType; // must be one of TYPE_DEFAULT, TYPE_OUTPUT, TYPE_PATCH ... |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 373 | audio_io_handle_t mThreadIoHandle; // I/O handle of the thread the track is attached to |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 374 | audio_port_handle_t mPortId; // unique ID for this track used by audio policy |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 375 | bool mIsInvalid; // non-resettable latch, set by invalidate() |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 376 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 377 | // It typically takes 5 threadloop mix iterations for latency to stabilize. |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 378 | // However, this can be 12+ iterations for BT. |
| 379 | // To be sure, we wait for latency to dip (it usually increases at the start) |
| 380 | // to assess stability and then log to MediaMetrics. |
| 381 | // Rapid start / pause calls may cause inaccurate numbers. |
| 382 | static inline constexpr int32_t LOG_START_COUNTDOWN = 12; |
| 383 | int32_t mLogStartCountdown = 0; // Mixer period countdown |
| 384 | int64_t mLogStartTimeNs = 0; // Monotonic time at start() |
| 385 | int64_t mLogStartFrames = 0; // Timestamp frames at start() |
| 386 | double mLogLatencyMs = 0.; // Track the last log latency |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 387 | |
Andy Hung | a81a4b4 | 2022-05-19 19:24:51 -0700 | [diff] [blame] | 388 | bool mLogForceVolumeUpdate = true; // force volume update to TrackMetrics. |
| 389 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 390 | TrackMetrics mTrackMetrics; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 391 | |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 392 | bool mServerLatencySupported = false; |
| 393 | std::atomic<bool> mServerLatencyFromTrack{}; // latency from track or server timestamp. |
| 394 | std::atomic<double> mServerLatencyMs{}; // last latency pushed from server thread. |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 395 | std::atomic<FrameTime> mKernelFrameTime{}; // last frame time on kernel side. |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 396 | const pid_t mCreatorPid; // can be different from mclient->pid() for instance |
| 397 | // when created by NuPlayer on behalf of a client |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 398 | |
| 399 | // If the last track change was notified to the client with readAndClearHasChanged |
| 400 | std::atomic_flag mChangeNotified = ATOMIC_FLAG_INIT; |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 401 | // RAII object for battery stats book-keeping |
| 402 | std::optional<mediautils::BatteryStatsAudioHandle> mBatteryStatsHolder; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 403 | }; |
| 404 | |
Andy Hung | 062a7a3 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 405 | class PatchTrackBase : public PatchProxyBufferProvider, public virtual IAfPatchTrackBase |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 406 | { |
| 407 | public: |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 408 | PatchTrackBase(const sp<ClientProxy>& proxy, |
Andy Hung | 7e888b3 | 2023-07-14 16:57:01 -0700 | [diff] [blame] | 409 | IAfThreadBase* thread, |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 410 | const Timeout& timeout); |
Andy Hung | 062a7a3 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 411 | void setPeerTimeout(std::chrono::nanoseconds timeout) final; |
| 412 | void setPeerProxy(const sp<IAfPatchTrackBase>& proxy, bool holdReference) final { |
| 413 | if (proxy) { |
| 414 | mPeerReferenceHold = holdReference ? proxy : nullptr; |
| 415 | mPeerProxy = proxy->asPatchProxyBufferProvider(); |
| 416 | } else { |
| 417 | clearPeerProxy(); |
| 418 | } |
| 419 | } |
| 420 | void clearPeerProxy() final { |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 421 | mPeerReferenceHold.clear(); |
| 422 | mPeerProxy = nullptr; |
| 423 | } |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 424 | |
Andy Hung | 062a7a3 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 425 | PatchProxyBufferProvider* asPatchProxyBufferProvider() final { return this; } |
| 426 | |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 427 | bool producesBufferOnDemand() const override { return false; } |
| 428 | |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 429 | protected: |
| 430 | const sp<ClientProxy> mProxy; |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 431 | sp<RefBase> mPeerReferenceHold; // keeps mPeerProxy alive during access. |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 432 | PatchProxyBufferProvider* mPeerProxy = nullptr; |
| 433 | struct timespec mPeerTimeout{}; |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 434 | }; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 435 | |
| 436 | } // namespace android |