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