The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | #ifndef ANDROID_AUDIO_TRACK_SHARED_H |
| 18 | #define ANDROID_AUDIO_TRACK_SHARED_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 23 | #include <audio_utils/minifloat.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/threads.h> |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 26 | #include <utils/RefBase.h> |
Glenn Kasten | 53dbe77 | 2015-01-06 10:46:38 -0800 | [diff] [blame] | 27 | #include <audio_utils/roundup.h> |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 28 | #include <media/AudioResamplerPublic.h> |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 29 | #include <media/AudioTimestamp.h> |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 30 | #include <media/Modulo.h> |
Mikhail Naganov | 9f3c02d | 2019-08-12 11:36:05 -0700 | [diff] [blame] | 31 | #include <media/nbaio/SingleStateQueue.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 37 | // for audio_track_cblk_t::mFlags |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 38 | #define CBLK_UNDERRUN 0x01 // set by server immediately on output underrun, cleared by client |
Glenn Kasten | 864585d | 2012-11-06 16:15:41 -0800 | [diff] [blame] | 39 | #define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger, |
Glenn Kasten | 9c5fdd8 | 2012-11-05 13:38:15 -0800 | [diff] [blame] | 40 | // clear: track is ready when buffer full |
Glenn Kasten | 864585d | 2012-11-06 16:15:41 -0800 | [diff] [blame] | 41 | #define CBLK_INVALID 0x04 // track buffer invalidated by AudioFlinger, need to re-create |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 42 | #define CBLK_DISABLED 0x08 // output track disabled by AudioFlinger due to underrun, |
| 43 | // need to re-start. Unlike CBLK_UNDERRUN, this is not set |
| 44 | // immediately, but only after a long string of underruns. |
| 45 | // 0x10 unused |
| 46 | #define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes |
| 47 | #define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes |
| 48 | #define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping |
| 49 | #define CBLK_OVERRUN 0x100 // set by server immediately on input overrun, cleared by client |
| 50 | #define CBLK_INTERRUPT 0x200 // set by client on interrupt(), cleared by client in obtainBuffer() |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 51 | #define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client |
| 52 | |
| 53 | //EL_FIXME 20 seconds may not be enough and must be reconciled with new obtainBuffer implementation |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 54 | #define MAX_RUN_OFFLOADED_TIMEOUT_MS 20000 // assuming up to a maximum of 20 seconds of offloaded |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame^] | 56 | // for audio_track_cblk_t::mState, to match TrackBase.h |
| 57 | static inline constexpr int CBLK_STATE_IDLE = 0; |
| 58 | static inline constexpr int CBLK_STATE_PAUSING = 7; |
| 59 | |
| 60 | /** |
| 61 | * MirroredVariable is a local variable which simultaneously updates |
| 62 | * a mirrored storage location. This is useful for server side variables |
| 63 | * where a local copy is kept, but a client visible copy is offered through shared memory. |
| 64 | * |
| 65 | * We use std::atomic as the default container class to access this memory. |
| 66 | */ |
| 67 | template <typename T, template <typename> class Container = std::atomic> |
| 68 | class MirroredVariable { |
| 69 | template <typename C> |
| 70 | struct Constraints { |
| 71 | // If setMirror is used with a different type U != T passed in, |
| 72 | // as a general rule, the Container must issue a memcpy to read or write |
| 73 | // (or its equivalent) to avoid possible strict aliasing issues. |
| 74 | // The memcpy also avoids gaps in structs and alignment issues with different types. |
| 75 | static constexpr bool ok_ = false; // Containers must specify constraints. |
| 76 | }; |
| 77 | template <typename X> |
| 78 | struct Constraints<std::atomic<X>> { |
| 79 | // Atomics force read and write to memory. |
| 80 | static constexpr bool ok = std::is_same_v<X, T> || |
| 81 | (std::atomic<X>::is_always_lock_free // no additional locking |
| 82 | && sizeof(std::atomic<X>) == sizeof(X) // layout identical to X. |
| 83 | && (std::is_arithmetic_v<X> || std::is_enum_v<X>)); // No gaps in the layout. |
| 84 | }; |
| 85 | |
| 86 | static_assert(Constraints<Container<T>>::ok); |
| 87 | public: |
| 88 | explicit MirroredVariable(const T& t) : t_{t} {} |
| 89 | |
| 90 | // implicit conversion operator |
| 91 | operator T() const { |
| 92 | return t_; |
| 93 | } |
| 94 | |
| 95 | MirroredVariable& operator=(const T& t) { |
| 96 | t_ = t; |
| 97 | if (mirror_ != nullptr) { |
| 98 | *mirror_ = t; |
| 99 | } |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | template <typename U> |
| 104 | void setMirror(Container<U> *other_mirror) { |
| 105 | // Much of the concern is with T != U, however there are additional concerns |
| 106 | // when storage uses shared memory between processes. For atomics, it must be |
| 107 | // lock free. |
| 108 | static_assert(sizeof(U) == sizeof(T)); |
| 109 | static_assert(alignof(U) == alignof(T)); |
| 110 | static_assert(Constraints<Container<U>>::ok); |
| 111 | static_assert(sizeof(Container<U>) == sizeof(Container<T>)); |
| 112 | static_assert(alignof(Container<U>) == alignof(Container<T>)); |
| 113 | auto mirror = reinterpret_cast<Container<T>*>(other_mirror); |
| 114 | if (mirror_ != mirror) { |
| 115 | mirror_ = mirror; |
| 116 | if (mirror != nullptr) { |
| 117 | *mirror = t_; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void clear() { |
| 123 | mirror_ = nullptr; |
| 124 | } |
| 125 | |
| 126 | MirroredVariable& operator&() const = delete; |
| 127 | |
| 128 | protected: |
| 129 | T t_{}; |
| 130 | Container<T>* mirror_ = nullptr; |
| 131 | }; |
| 132 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 133 | struct AudioTrackSharedStreaming { |
| 134 | // similar to NBAIO MonoPipe |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 135 | // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2 |
Glenn Kasten | f59497b | 2015-01-26 16:35:47 -0800 | [diff] [blame] | 136 | volatile int32_t mFront; // read by consumer (output: server, input: client) |
| 137 | volatile int32_t mRear; // written by producer (output: client, input: server) |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 138 | volatile int32_t mFlush; // incremented by client to indicate a request to flush; |
| 139 | // server notices and discards all data between mFront and mRear |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 140 | volatile int32_t mStop; // set by client to indicate a stop frame position; server |
| 141 | // will not read beyond this position until start is called. |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 142 | volatile uint32_t mUnderrunFrames; // server increments for each unavailable but desired frame |
| 143 | volatile uint32_t mUnderrunCount; // server increments for each underrun occurrence |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 146 | // Represents a single state of an AudioTrack that was created in static mode (shared memory buffer |
| 147 | // supplied by the client). This state needs to be communicated from the client to server. As this |
| 148 | // state is too large to be updated atomically without a mutex, and mutexes aren't allowed here, the |
| 149 | // state is wrapped by a SingleStateQueue. |
| 150 | struct StaticAudioTrackState { |
| 151 | // Do not define constructors, destructors, or virtual methods as this is part of a |
| 152 | // union in shared memory and they will not get called properly. |
| 153 | |
| 154 | // These fields should both be size_t, but since they are located in shared memory we |
| 155 | // force to 32-bit. The client and server may have different typedefs for size_t. |
| 156 | |
| 157 | // The state has a sequence counter to indicate whether changes are made to loop or position. |
| 158 | // The sequence counter also currently indicates whether loop or position is first depending |
| 159 | // on which is greater; it jumps by max(mLoopSequence, mPositionSequence) + 1. |
| 160 | |
| 161 | uint32_t mLoopStart; |
| 162 | uint32_t mLoopEnd; |
| 163 | int32_t mLoopCount; |
| 164 | uint32_t mLoopSequence; // a sequence counter to indicate changes to loop |
| 165 | uint32_t mPosition; |
| 166 | uint32_t mPositionSequence; // a sequence counter to indicate changes to position |
| 167 | }; |
| 168 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 169 | typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue; |
| 170 | |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 171 | struct StaticAudioTrackPosLoop { |
| 172 | // Do not define constructors, destructors, or virtual methods as this is part of a |
| 173 | // union in shared memory and will not get called properly. |
| 174 | |
| 175 | // These fields should both be size_t, but since they are located in shared memory we |
| 176 | // force to 32-bit. The client and server may have different typedefs for size_t. |
| 177 | |
| 178 | // This struct information is stored in a single state queue to communicate the |
| 179 | // static AudioTrack server state to the client while data is consumed. |
| 180 | // It is smaller than StaticAudioTrackState to prevent unnecessary information from |
| 181 | // being sent. |
| 182 | |
| 183 | uint32_t mBufferPosition; |
| 184 | int32_t mLoopCount; |
| 185 | }; |
| 186 | |
| 187 | typedef SingleStateQueue<StaticAudioTrackPosLoop> StaticAudioTrackPosLoopQueue; |
| 188 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 189 | struct AudioTrackSharedStatic { |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 190 | // client requests to the server for loop or position changes. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 191 | StaticAudioTrackSingleStateQueue::Shared |
| 192 | mSingleStateQueue; |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 193 | // position info updated asynchronously by server and read by client, |
| 194 | // "for entertainment purposes only" |
| 195 | StaticAudioTrackPosLoopQueue::Shared |
| 196 | mPosLoopQueue; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 199 | typedef SingleStateQueue<AudioPlaybackRate> PlaybackRateQueue; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 200 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 201 | typedef SingleStateQueue<ExtendedTimestamp> ExtendedTimestampQueue; |
| 202 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 203 | // ---------------------------------------------------------------------------- |
| 204 | |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 205 | // Important: do not add any virtual methods, including ~ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | struct audio_track_cblk_t |
| 207 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 208 | // Since the control block is always located in shared memory, this constructor |
| 209 | // is only used for placement new(). It is never used for regular new() or stack. |
| 210 | audio_track_cblk_t(); |
| 211 | /*virtual*/ ~audio_track_cblk_t() { } |
| 212 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 213 | friend class Proxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 214 | friend class ClientProxy; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 215 | friend class AudioTrackClientProxy; |
| 216 | friend class AudioRecordClientProxy; |
| 217 | friend class ServerProxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 218 | friend class AudioTrackServerProxy; |
| 219 | friend class AudioRecordServerProxy; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | |
| 221 | // The data members are grouped so that members accessed frequently and in the same context |
| 222 | // are in the same line of data cache. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 223 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 224 | uint32_t mServer; // Number of filled frames consumed by server (mIsOut), |
| 225 | // or filled frames provided by server (!mIsOut). |
| 226 | // It is updated asynchronously by server without a barrier. |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 227 | // The value should be used |
| 228 | // "for entertainment purposes only", |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 229 | // which means don't make important decisions based on it. |
Glenn Kasten | 22eb4e2 | 2012-11-07 14:03:00 -0800 | [diff] [blame] | 230 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 231 | uint32_t mPad1; // unused |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 232 | |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 233 | volatile int32_t mFutex; // event flag: down (P) by client, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 234 | // up (V) by server or binderDied() or interrupt() |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 235 | #define CBLK_FUTEX_WAKE 1 // if event flag bit is set, then a deferred wake is pending |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 236 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 237 | private: |
| 238 | |
Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 239 | // This field should be a size_t, but since it is located in shared memory we |
| 240 | // force to 32-bit. The client and server may have different typedefs for size_t. |
| 241 | uint32_t mMinimum; // server wakes up client if available >= mMinimum |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 242 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 243 | // Stereo gains for AudioTrack only, not used by AudioRecord. |
| 244 | gain_minifloat_packed_t mVolumeLR; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 245 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 246 | uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz |
| 247 | // or 0 == default. Write-only client, read-only server. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 248 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 249 | PlaybackRateQueue::Shared mPlaybackRateQueue; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 250 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 251 | // client write-only, server read-only |
| 252 | uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0 |
| 253 | |
Colin Cross | b8c35f9 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 254 | uint16_t mPad2 __attribute__((__unused__)); // unused |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 255 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 256 | // server write-only, client read |
Eric Laurent | 8ce8e26 | 2016-02-16 11:59:23 -0800 | [diff] [blame] | 257 | ExtendedTimestampQueue::Shared mExtendedTimestampQueue; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 258 | |
Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 259 | // This is set by AudioTrack.setBufferSizeInFrames(). |
| 260 | // A write will not fill the buffer above this limit. |
| 261 | volatile uint32_t mBufferSizeInFrames; // effective size of the buffer |
Andy Hung | 3c7f47a | 2021-03-16 17:30:09 -0700 | [diff] [blame] | 262 | volatile uint32_t mStartThresholdInFrames; // min frames in buffer to start streaming |
Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 263 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 264 | public: |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 265 | |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 266 | volatile int32_t mFlags; // combinations of CBLK_* |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 267 | |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame^] | 268 | std::atomic<int32_t> mState; // current TrackBase state. |
| 269 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 270 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 271 | union { |
| 272 | AudioTrackSharedStreaming mStreaming; |
| 273 | AudioTrackSharedStatic mStatic; |
| 274 | int mAlign[8]; |
| 275 | } u; |
| 276 | |
| 277 | // Cache line boundary (32 bytes) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame^] | 280 | // TODO: ensure standard layout. |
| 281 | // static_assert(std::is_standard_layout_v<audio_track_cblk_t>); |
| 282 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 283 | // ---------------------------------------------------------------------------- |
| 284 | |
| 285 | // Proxy for shared memory control block, to isolate callers from needing to know the details. |
| 286 | // There is exactly one ClientProxy and one ServerProxy per shared memory control block. |
| 287 | // The proxies are located in normal memory, and are not multi-thread safe within a given side. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 288 | class Proxy : public RefBase { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 289 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 290 | Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut, |
| 291 | bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 292 | virtual ~Proxy() { } |
| 293 | |
| 294 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 295 | struct Buffer { |
| 296 | size_t mFrameCount; // number of frames available in this buffer |
| 297 | void* mRaw; // pointer to first frame |
| 298 | size_t mNonContig; // number of additional non-contiguous frames available |
| 299 | }; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 300 | |
Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 301 | size_t frameCount() const { return mFrameCount; } |
Andy Hung | 3c7f47a | 2021-03-16 17:30:09 -0700 | [diff] [blame] | 302 | uint32_t getStartThresholdInFrames() const; |
| 303 | uint32_t setStartThresholdInFrames(uint32_t startThresholdInFrames); |
Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 304 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 305 | protected: |
| 306 | // These refer to shared memory, and are virtual addresses with respect to the current process. |
| 307 | // They may have different virtual addresses within the other process. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 308 | audio_track_cblk_t* const mCblk; // the control block |
| 309 | void* const mBuffers; // starting address of buffers |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 310 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 311 | const size_t mFrameCount; // not necessarily a power of 2 |
| 312 | const size_t mFrameSize; // in bytes |
| 313 | const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode |
| 314 | const bool mIsOut; // true for AudioTrack, false for AudioRecord |
| 315 | const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord |
| 316 | bool mIsShutdown; // latch set to true when shared memory corruption detected |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 317 | size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | // ---------------------------------------------------------------------------- |
| 321 | |
| 322 | // Proxy seen by AudioTrack client and AudioRecord client |
| 323 | class ClientProxy : public Proxy { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 324 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 325 | ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 326 | bool isOut, bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 327 | virtual ~ClientProxy() { } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 328 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 329 | static const struct timespec kForever; |
| 330 | static const struct timespec kNonBlocking; |
| 331 | |
| 332 | // Obtain a buffer with filled frames (reading) or empty frames (writing). |
| 333 | // It is permitted to call obtainBuffer() multiple times in succession, without any intervening |
| 334 | // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively |
| 335 | // sets or extends the unreleased frame count. |
| 336 | // On entry: |
| 337 | // buffer->mFrameCount should be initialized to maximum number of desired frames, |
| 338 | // which must be > 0. |
| 339 | // buffer->mNonContig is unused. |
| 340 | // buffer->mRaw is unused. |
| 341 | // requested is the requested timeout in local monotonic delta time units: |
| 342 | // NULL or &kNonBlocking means non-blocking (zero timeout). |
| 343 | // &kForever means block forever (infinite timeout). |
| 344 | // Other values mean a specific timeout in local monotonic delta time units. |
| 345 | // elapsed is a pointer to a location that will hold the total local monotonic time that |
| 346 | // elapsed while blocked, or NULL if not needed. |
| 347 | // On exit: |
| 348 | // buffer->mFrameCount has the actual number of contiguous available frames, |
| 349 | // which is always 0 when the return status != NO_ERROR. |
| 350 | // buffer->mNonContig is the number of additional non-contiguous available frames. |
| 351 | // buffer->mRaw is a pointer to the first available frame, |
| 352 | // or NULL when buffer->mFrameCount == 0. |
| 353 | // The return status is one of: |
| 354 | // NO_ERROR Success, buffer->mFrameCount > 0. |
| 355 | // WOULD_BLOCK Non-blocking mode and no frames are available. |
| 356 | // TIMED_OUT Timeout occurred before any frames became available. |
| 357 | // This can happen even for infinite timeout, due to a spurious wakeup. |
| 358 | // In this case, the caller should investigate and then re-try as appropriate. |
| 359 | // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create. |
| 360 | // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again. |
| 361 | // NO_INIT Shared memory is corrupt. |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 362 | // NOT_ENOUGH_DATA Server has disabled the track because of underrun: restart the track |
| 363 | // if still in active state. |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 364 | // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 365 | status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL, |
| 366 | struct timespec *elapsed = NULL); |
| 367 | |
| 368 | // Release (some of) the frames last obtained. |
| 369 | // On entry, buffer->mFrameCount should have the number of frames to release, |
| 370 | // which must (cumulatively) be <= the number of frames last obtained but not yet released. |
| 371 | // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer(). |
| 372 | // It is permitted to call releaseBuffer() multiple times to release the frames in chunks. |
| 373 | // On exit: |
| 374 | // buffer->mFrameCount is zero. |
| 375 | // buffer->mRaw is NULL. |
| 376 | void releaseBuffer(Buffer* buffer); |
| 377 | |
| 378 | // Call after detecting server's death |
| 379 | void binderDied(); |
| 380 | |
| 381 | // Call to force an obtainBuffer() to return quickly with -EINTR |
| 382 | void interrupt(); |
| 383 | |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 384 | Modulo<uint32_t> getPosition() { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 385 | return mEpoch + mCblk->mServer; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 388 | void setEpoch(const Modulo<uint32_t> &epoch) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 389 | mEpoch = epoch; |
| 390 | } |
| 391 | |
| 392 | void setMinimum(size_t minimum) { |
Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 393 | // This can only happen on a 64-bit client |
| 394 | if (minimum > UINT32_MAX) { |
| 395 | minimum = UINT32_MAX; |
| 396 | } |
| 397 | mCblk->mMinimum = (uint32_t) minimum; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Return the number of frames that would need to be obtained and released |
| 401 | // in order for the client to be aligned at start of buffer |
| 402 | virtual size_t getMisalignment(); |
| 403 | |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 404 | Modulo<uint32_t> getEpoch() const { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 405 | return mEpoch; |
| 406 | } |
| 407 | |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame^] | 408 | int32_t getState() const { return mCblk->mState; } |
Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 409 | uint32_t getBufferSizeInFrames() const { return mBufferSizeInFrames; } |
| 410 | // See documentation for AudioTrack::setBufferSizeInFrames() |
| 411 | uint32_t setBufferSizeInFrames(uint32_t requestedSize); |
Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 412 | |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 413 | status_t getTimestamp(ExtendedTimestamp *timestamp) { |
| 414 | if (timestamp == nullptr) { |
| 415 | return BAD_VALUE; |
| 416 | } |
| 417 | (void) mTimestampObserver.poll(mTimestamp); |
| 418 | *timestamp = mTimestamp; |
| 419 | return OK; |
| 420 | } |
| 421 | |
| 422 | void clearTimestamp() { |
| 423 | mTimestamp.clear(); |
| 424 | } |
| 425 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 426 | virtual void stop() { }; // called by client in AudioTrack::stop() |
| 427 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 428 | private: |
Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 429 | // This is a copy of mCblk->mBufferSizeInFrames |
| 430 | uint32_t mBufferSizeInFrames; // effective size of the buffer |
| 431 | |
Andy Hung | 90e8a97 | 2015-11-09 16:42:40 -0800 | [diff] [blame] | 432 | Modulo<uint32_t> mEpoch; |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 433 | |
| 434 | // The shared buffer contents referred to by the timestamp observer |
| 435 | // is initialized when the server proxy created. A local zero timestamp |
| 436 | // is initialized by the client constructor. |
| 437 | ExtendedTimestampQueue::Observer mTimestampObserver; |
| 438 | ExtendedTimestamp mTimestamp; // initialized by constructor |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | // ---------------------------------------------------------------------------- |
| 442 | |
| 443 | // Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack |
| 444 | class AudioTrackClientProxy : public ClientProxy { |
| 445 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 446 | AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 447 | size_t frameSize, bool clientInServer = false) |
| 448 | : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 449 | clientInServer), |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 450 | mPlaybackRateMutator(&cblk->mPlaybackRateQueue) { |
| 451 | } |
| 452 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 453 | virtual ~AudioTrackClientProxy() { } |
| 454 | |
| 455 | // No barriers on the following operations, so the ordering of loads/stores |
| 456 | // with respect to other parameters is UNPREDICTABLE. That's considered safe. |
| 457 | |
| 458 | // caller must limit to 0.0 <= sendLevel <= 1.0 |
| 459 | void setSendLevel(float sendLevel) { |
| 460 | mCblk->mSendLevel = uint16_t(sendLevel * 0x1000); |
| 461 | } |
| 462 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 463 | // set stereo gains |
| 464 | void setVolumeLR(gain_minifloat_packed_t volumeLR) { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 465 | mCblk->mVolumeLR = volumeLR; |
| 466 | } |
| 467 | |
| 468 | void setSampleRate(uint32_t sampleRate) { |
| 469 | mCblk->mSampleRate = sampleRate; |
| 470 | } |
| 471 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 472 | void setPlaybackRate(const AudioPlaybackRate& playbackRate) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 473 | mPlaybackRateMutator.push(playbackRate); |
| 474 | } |
| 475 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 476 | // Sends flush and stop position information from the client to the server, |
| 477 | // used by streaming AudioTrack flush() or stop(). |
| 478 | void sendStreamingFlushStop(bool flush); |
| 479 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 480 | virtual void flush(); |
| 481 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 482 | void stop() override; |
| 483 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 484 | virtual uint32_t getUnderrunFrames() const { |
| 485 | return mCblk->u.mStreaming.mUnderrunFrames; |
| 486 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 487 | virtual uint32_t getUnderrunCount() const { |
| 488 | return mCblk->u.mStreaming.mUnderrunCount; |
| 489 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 490 | |
| 491 | bool clearStreamEndDone(); // and return previous value |
| 492 | |
| 493 | bool getStreamEndDone() const; |
| 494 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 495 | status_t waitStreamEndDone(const struct timespec *requested); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 496 | |
| 497 | private: |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 498 | PlaybackRateQueue::Mutator mPlaybackRateMutator; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | class StaticAudioTrackClientProxy : public AudioTrackClientProxy { |
| 502 | public: |
| 503 | StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 504 | size_t frameSize); |
| 505 | virtual ~StaticAudioTrackClientProxy() { } |
| 506 | |
| 507 | virtual void flush(); |
| 508 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 509 | void stop() override; |
| 510 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 511 | #define MIN_LOOP 16 // minimum length of each loop iteration in frames |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 512 | |
| 513 | // setLoop(), setBufferPosition(), and setBufferPositionAndLoop() set the |
| 514 | // static buffer position and looping parameters. These commands are not |
| 515 | // synchronous (they do not wait or block); instead they take effect at the |
| 516 | // next buffer data read from the server side. However, the client side |
| 517 | // getters will read a cached version of the position and loop variables |
| 518 | // until the setting takes effect. |
| 519 | // |
| 520 | // setBufferPositionAndLoop() is equivalent to calling, in order, setLoop() and |
| 521 | // setBufferPosition(). |
| 522 | // |
| 523 | // The functions should not be relied upon to do parameter or state checking. |
| 524 | // That is done at the AudioTrack level. |
| 525 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 526 | void setLoop(size_t loopStart, size_t loopEnd, int loopCount); |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 527 | void setBufferPosition(size_t position); |
| 528 | void setBufferPositionAndLoop(size_t position, size_t loopStart, size_t loopEnd, |
| 529 | int loopCount); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 530 | size_t getBufferPosition(); |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 531 | // getBufferPositionAndLoopCount() provides the proper snapshot of |
| 532 | // position and loopCount together. |
| 533 | void getBufferPositionAndLoopCount(size_t *position, int *loopCount); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 534 | |
| 535 | virtual size_t getMisalignment() { |
| 536 | return 0; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Andy Hung | 03e1e2c | 2018-02-20 12:49:08 -0800 | [diff] [blame] | 539 | virtual uint32_t getUnderrunFrames() const override { |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | virtual uint32_t getUnderrunCount() const override { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 544 | return 0; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 547 | private: |
| 548 | StaticAudioTrackSingleStateQueue::Mutator mMutator; |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 549 | StaticAudioTrackPosLoopQueue::Observer mPosLoopObserver; |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 550 | StaticAudioTrackState mState; // last communicated state to server |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 551 | StaticAudioTrackPosLoop mPosLoop; // snapshot of position and loop. |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | // ---------------------------------------------------------------------------- |
| 555 | |
| 556 | // Proxy used by AudioRecord client |
| 557 | class AudioRecordClientProxy : public ClientProxy { |
| 558 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 559 | AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 560 | size_t frameSize) |
| 561 | : ClientProxy(cblk, buffers, frameCount, frameSize, |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 562 | false /*isOut*/, false /*clientInServer*/) { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 563 | ~AudioRecordClientProxy() { } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 564 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 565 | // Advances the client read pointer to the server write head pointer |
| 566 | // effectively flushing the client read buffer. The effect is |
| 567 | // instantaneous. Returns the number of frames flushed. |
| 568 | uint32_t flush() { |
| 569 | int32_t rear = android_atomic_acquire_load(&mCblk->u.mStreaming.mRear); |
| 570 | int32_t front = mCblk->u.mStreaming.mFront; |
| 571 | android_atomic_release_store(rear, &mCblk->u.mStreaming.mFront); |
| 572 | return (Modulo<int32_t>(rear) - front).unsignedValue(); |
| 573 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | // ---------------------------------------------------------------------------- |
| 577 | |
| 578 | // Proxy used by AudioFlinger server |
| 579 | class ServerProxy : public Proxy { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 580 | protected: |
| 581 | ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 582 | bool isOut, bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 583 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 584 | virtual ~ServerProxy() { } |
| 585 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 586 | // Obtain a buffer with filled frames (writing) or empty frames (reading). |
| 587 | // It is permitted to call obtainBuffer() multiple times in succession, without any intervening |
| 588 | // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively |
| 589 | // sets or extends the unreleased frame count. |
| 590 | // Always non-blocking. |
| 591 | // On entry: |
| 592 | // buffer->mFrameCount should be initialized to maximum number of desired frames, |
| 593 | // which must be > 0. |
| 594 | // buffer->mNonContig is unused. |
| 595 | // buffer->mRaw is unused. |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 596 | // ackFlush is true iff being called from Track::start to acknowledge a pending flush. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 597 | // On exit: |
| 598 | // buffer->mFrameCount has the actual number of contiguous available frames, |
| 599 | // which is always 0 when the return status != NO_ERROR. |
| 600 | // buffer->mNonContig is the number of additional non-contiguous available frames. |
| 601 | // buffer->mRaw is a pointer to the first available frame, |
| 602 | // or NULL when buffer->mFrameCount == 0. |
| 603 | // The return status is one of: |
| 604 | // NO_ERROR Success, buffer->mFrameCount > 0. |
| 605 | // WOULD_BLOCK No frames are available. |
| 606 | // NO_INIT Shared memory is corrupt. |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 607 | virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 608 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 609 | // Release (some of) the frames last obtained. |
| 610 | // On entry, buffer->mFrameCount should have the number of frames to release, |
| 611 | // which must (cumulatively) be <= the number of frames last obtained but not yet released. |
| 612 | // It is permitted to call releaseBuffer() multiple times to release the frames in chunks. |
| 613 | // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer(). |
| 614 | // On exit: |
| 615 | // buffer->mFrameCount is zero. |
| 616 | // buffer->mRaw is NULL. |
| 617 | virtual void releaseBuffer(Buffer* buffer); |
| 618 | |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 619 | // Return the total number of frames that AudioFlinger has obtained and released |
| 620 | virtual int64_t framesReleased() const { return mReleased; } |
| 621 | |
| 622 | // Expose timestamp to client proxy. Should only be called by a single thread. |
| 623 | virtual void setTimestamp(const ExtendedTimestamp ×tamp) { |
| 624 | mTimestampMutator.push(timestamp); |
| 625 | } |
| 626 | |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 627 | virtual ExtendedTimestamp getTimestamp() const { |
| 628 | return mTimestampMutator.last(); |
| 629 | } |
| 630 | |
Phil Burk | 4bb650b | 2016-09-09 12:11:17 -0700 | [diff] [blame] | 631 | // Flushes the shared ring buffer if the client had requested it using mStreaming.mFlush. |
| 632 | // If flush occurs then: |
| 633 | // cblk->u.mStreaming.mFront, ServerProxy::mFlush and ServerProxy::mFlushed will be modified |
| 634 | // client will be notified via Futex |
| 635 | virtual void flushBufferIfNeeded(); |
| 636 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 637 | // Returns the rear position of the AudioTrack shared ring buffer, limited by |
| 638 | // the stop frame position level. |
| 639 | virtual int32_t getRear() const = 0; |
| 640 | |
Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 641 | // Total count of the number of flushed frames since creation (never reset). |
| 642 | virtual int64_t framesFlushed() const { return mFlushed; } |
| 643 | |
Andy Hung | 2a4e161 | 2018-06-01 15:06:09 -0700 | [diff] [blame] | 644 | // Safe frames ready query with no side effects. |
| 645 | virtual size_t framesReadySafe() const = 0; |
| 646 | |
Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 647 | // Get dynamic buffer size from the shared control block. |
| 648 | uint32_t getBufferSizeInFrames() const { |
| 649 | return android_atomic_acquire_load((int32_t *)&mCblk->mBufferSizeInFrames); |
| 650 | } |
| 651 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 652 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 653 | size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer() |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 654 | int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 655 | int64_t mReleased; // our copy of cblk->mServer, at 64 bit resolution |
Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 656 | int64_t mFlushed; // flushed frames to account for client-server discrepancy |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 657 | ExtendedTimestampQueue::Mutator mTimestampMutator; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 658 | }; |
| 659 | |
| 660 | // Proxy used by AudioFlinger for servicing AudioTrack |
| 661 | class AudioTrackServerProxy : public ServerProxy { |
| 662 | public: |
| 663 | AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
Kevin Rocard | 3686203 | 2019-10-10 10:52:19 +0100 | [diff] [blame] | 664 | size_t frameSize, bool clientInServer, uint32_t sampleRate) |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 665 | : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer), |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 666 | mPlaybackRateObserver(&cblk->mPlaybackRateQueue), |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 667 | mUnderrunCount(0), mUnderrunning(false), mDrained(true) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 668 | mCblk->mSampleRate = sampleRate; |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 669 | mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 670 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 671 | protected: |
| 672 | virtual ~AudioTrackServerProxy() { } |
| 673 | |
| 674 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 675 | // return value of these methods must be validated by the caller |
| 676 | uint32_t getSampleRate() const { return mCblk->mSampleRate; } |
| 677 | uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 678 | gain_minifloat_packed_t getVolumeLR() const { return mCblk->mVolumeLR; } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 679 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 680 | // estimated total number of filled frames available to server to read, |
| 681 | // which may include non-contiguous frames |
| 682 | virtual size_t framesReady(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 683 | |
Andy Hung | 2a4e161 | 2018-06-01 15:06:09 -0700 | [diff] [blame] | 684 | size_t framesReadySafe() const override; // frames available to read by server. |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 685 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 686 | // Currently AudioFlinger will call framesReady() for a fast track from two threads: |
| 687 | // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended |
| 688 | // to be called from at most one thread of server, and one thread of client. |
| 689 | // As a temporary workaround, this method informs the proxy implementation that it |
| 690 | // should avoid doing a state queue poll from within framesReady(). |
| 691 | // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread. |
| 692 | virtual void framesReadyIsCalledByMultipleThreads() { } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 693 | |
| 694 | bool setStreamEndDone(); // and return previous value |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 695 | |
| 696 | // Add to the tally of underrun frames, and inform client of underrun |
| 697 | virtual void tallyUnderrunFrames(uint32_t frameCount); |
| 698 | |
| 699 | // Return the total number of frames which AudioFlinger desired but were unavailable, |
| 700 | // and thus which resulted in an underrun. |
| 701 | virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 702 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 703 | virtual uint32_t getUnderrunCount() const { return mCblk->u.mStreaming.mUnderrunCount; } |
| 704 | |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 705 | // Return the playback speed and pitch read atomically. Not multi-thread safe on server side. |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 706 | AudioPlaybackRate getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 707 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 708 | // Set the internal drain state of the track buffer from the timestamp received. |
| 709 | virtual void setDrained(bool drained) { |
| 710 | mDrained.store(drained); |
| 711 | } |
| 712 | |
| 713 | // Check if the internal drain state of the track buffer. |
| 714 | // This is not a guarantee, but advisory for determining whether the track is |
| 715 | // fully played out. |
| 716 | virtual bool isDrained() const { |
| 717 | return mDrained.load(); |
| 718 | } |
| 719 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 720 | int32_t getRear() const override; |
| 721 | |
| 722 | // Called on server side track start(). |
| 723 | virtual void start(); |
| 724 | |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 725 | private: |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 726 | AudioPlaybackRate mPlaybackRate; // last observed playback rate |
| 727 | PlaybackRateQueue::Observer mPlaybackRateObserver; |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 728 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 729 | // Last client stop-at position when start() was called. Used for streaming AudioTracks. |
| 730 | std::atomic<int32_t> mStopLast{0}; |
| 731 | |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 732 | // The server keeps a copy here where it is safe from the client. |
| 733 | uint32_t mUnderrunCount; // echoed to mCblk |
| 734 | bool mUnderrunning; // used to detect edge of underrun |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 735 | |
| 736 | std::atomic<bool> mDrained; // is the track buffer drained |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | class StaticAudioTrackServerProxy : public AudioTrackServerProxy { |
| 740 | public: |
| 741 | StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
Kevin Rocard | 3686203 | 2019-10-10 10:52:19 +0100 | [diff] [blame] | 742 | size_t frameSize, uint32_t sampleRate); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 743 | protected: |
| 744 | virtual ~StaticAudioTrackServerProxy() { } |
| 745 | |
| 746 | public: |
| 747 | virtual size_t framesReady(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 748 | virtual size_t framesReadySafe() const override; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 749 | virtual void framesReadyIsCalledByMultipleThreads(); |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 750 | virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 751 | virtual void releaseBuffer(Buffer* buffer); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 752 | virtual void tallyUnderrunFrames(uint32_t frameCount); |
| 753 | virtual uint32_t getUnderrunFrames() const { return 0; } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 754 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 755 | int32_t getRear() const override; |
| 756 | |
| 757 | void start() override { } // ignore for static tracks |
| 758 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 759 | private: |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 760 | status_t updateStateWithLoop(StaticAudioTrackState *localState, |
| 761 | const StaticAudioTrackState &update) const; |
| 762 | status_t updateStateWithPosition(StaticAudioTrackState *localState, |
| 763 | const StaticAudioTrackState &update) const; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 764 | ssize_t pollPosition(); // poll for state queue update, and return current position |
| 765 | StaticAudioTrackSingleStateQueue::Observer mObserver; |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 766 | StaticAudioTrackPosLoopQueue::Mutator mPosLoopMutator; |
Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 767 | size_t mFramesReadySafe; // Assuming size_t read/writes are atomic on 32 / 64 bit |
| 768 | // processors, this is a thread-safe version of |
| 769 | // mFramesReady. |
| 770 | int64_t mFramesReady; // The number of frames ready in the static buffer |
| 771 | // including loops. This is 64 bits since loop mode |
| 772 | // can cause a track to appear to have a large number |
| 773 | // of frames. INT64_MAX means an infinite loop. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 774 | bool mFramesReadyIsCalledByMultipleThreads; |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 775 | StaticAudioTrackState mState; // Server side state. Any updates from client must be |
| 776 | // passed by the mObserver SingleStateQueue. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 777 | }; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 778 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 779 | // Proxy used by AudioFlinger for servicing AudioRecord |
| 780 | class AudioRecordServerProxy : public ServerProxy { |
| 781 | public: |
| 782 | AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 783 | size_t frameSize, bool clientInServer) |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 784 | : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/, clientInServer) { } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 785 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 786 | int32_t getRear() const override { |
| 787 | return mCblk->u.mStreaming.mRear; // For completeness only; mRear written by server. |
| 788 | } |
| 789 | |
Andy Hung | 2a4e161 | 2018-06-01 15:06:09 -0700 | [diff] [blame] | 790 | size_t framesReadySafe() const override; // frames available to read by client. |
| 791 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 792 | protected: |
| 793 | virtual ~AudioRecordServerProxy() { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 794 | }; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | |
| 796 | // ---------------------------------------------------------------------------- |
| 797 | |
| 798 | }; // namespace android |
| 799 | |
| 800 | #endif // ANDROID_AUDIO_TRACK_SHARED_H |