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> |
| 27 | #include <media/nbaio/roundup.h> |
| 28 | #include <media/SingleStateQueue.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 34 | // for audio_track_cblk_t::mFlags |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 35 | #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] | 36 | #define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger, |
Glenn Kasten | 9c5fdd8 | 2012-11-05 13:38:15 -0800 | [diff] [blame] | 37 | // clear: track is ready when buffer full |
Glenn Kasten | 864585d | 2012-11-06 16:15:41 -0800 | [diff] [blame] | 38 | #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] | 39 | #define CBLK_DISABLED 0x08 // output track disabled by AudioFlinger due to underrun, |
| 40 | // need to re-start. Unlike CBLK_UNDERRUN, this is not set |
| 41 | // immediately, but only after a long string of underruns. |
| 42 | // 0x10 unused |
| 43 | #define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes |
| 44 | #define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes |
| 45 | #define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping |
| 46 | #define CBLK_OVERRUN 0x100 // set by server immediately on input overrun, cleared by client |
| 47 | #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] | 48 | #define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client |
| 49 | |
| 50 | //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] | 51 | #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] | 52 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 53 | struct AudioTrackSharedStreaming { |
| 54 | // similar to NBAIO MonoPipe |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 55 | // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2 |
| 56 | volatile int32_t mFront; // read by server |
| 57 | volatile int32_t mRear; // write by client |
| 58 | volatile int32_t mFlush; // incremented by client to indicate a request to flush; |
| 59 | // server notices and discards all data between mFront and mRear |
| 60 | volatile uint32_t mUnderrunFrames; // server increments for each unavailable but desired frame |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 63 | // Represents a single state of an AudioTrack that was created in static mode (shared memory buffer |
| 64 | // supplied by the client). This state needs to be communicated from the client to server. As this |
| 65 | // state is too large to be updated atomically without a mutex, and mutexes aren't allowed here, the |
| 66 | // state is wrapped by a SingleStateQueue. |
| 67 | struct StaticAudioTrackState { |
| 68 | // Do not define constructors, destructors, or virtual methods as this is part of a |
| 69 | // union in shared memory and they will not get called properly. |
| 70 | |
| 71 | // These fields should both be size_t, but since they are located in shared memory we |
| 72 | // force to 32-bit. The client and server may have different typedefs for size_t. |
| 73 | |
| 74 | // The state has a sequence counter to indicate whether changes are made to loop or position. |
| 75 | // The sequence counter also currently indicates whether loop or position is first depending |
| 76 | // on which is greater; it jumps by max(mLoopSequence, mPositionSequence) + 1. |
| 77 | |
| 78 | uint32_t mLoopStart; |
| 79 | uint32_t mLoopEnd; |
| 80 | int32_t mLoopCount; |
| 81 | uint32_t mLoopSequence; // a sequence counter to indicate changes to loop |
| 82 | uint32_t mPosition; |
| 83 | uint32_t mPositionSequence; // a sequence counter to indicate changes to position |
| 84 | }; |
| 85 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 86 | typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue; |
| 87 | |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 88 | struct StaticAudioTrackPosLoop { |
| 89 | // Do not define constructors, destructors, or virtual methods as this is part of a |
| 90 | // union in shared memory and will not get called properly. |
| 91 | |
| 92 | // These fields should both be size_t, but since they are located in shared memory we |
| 93 | // force to 32-bit. The client and server may have different typedefs for size_t. |
| 94 | |
| 95 | // This struct information is stored in a single state queue to communicate the |
| 96 | // static AudioTrack server state to the client while data is consumed. |
| 97 | // It is smaller than StaticAudioTrackState to prevent unnecessary information from |
| 98 | // being sent. |
| 99 | |
| 100 | uint32_t mBufferPosition; |
| 101 | int32_t mLoopCount; |
| 102 | }; |
| 103 | |
| 104 | typedef SingleStateQueue<StaticAudioTrackPosLoop> StaticAudioTrackPosLoopQueue; |
| 105 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 106 | struct AudioTrackSharedStatic { |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 107 | // client requests to the server for loop or position changes. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 108 | StaticAudioTrackSingleStateQueue::Shared |
| 109 | mSingleStateQueue; |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 110 | // position info updated asynchronously by server and read by client, |
| 111 | // "for entertainment purposes only" |
| 112 | StaticAudioTrackPosLoopQueue::Shared |
| 113 | mPosLoopQueue; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // ---------------------------------------------------------------------------- |
| 117 | |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 118 | // Important: do not add any virtual methods, including ~ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | struct audio_track_cblk_t |
| 120 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 121 | // Since the control block is always located in shared memory, this constructor |
| 122 | // is only used for placement new(). It is never used for regular new() or stack. |
| 123 | audio_track_cblk_t(); |
| 124 | /*virtual*/ ~audio_track_cblk_t() { } |
| 125 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 126 | friend class Proxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 127 | friend class ClientProxy; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 128 | friend class AudioTrackClientProxy; |
| 129 | friend class AudioRecordClientProxy; |
| 130 | friend class ServerProxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 131 | friend class AudioTrackServerProxy; |
| 132 | friend class AudioRecordServerProxy; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | |
| 134 | // The data members are grouped so that members accessed frequently and in the same context |
| 135 | // are in the same line of data cache. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 136 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 137 | uint32_t mServer; // Number of filled frames consumed by server (mIsOut), |
| 138 | // or filled frames provided by server (!mIsOut). |
| 139 | // It is updated asynchronously by server without a barrier. |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 140 | // The value should be used |
| 141 | // "for entertainment purposes only", |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 142 | // which means don't make important decisions based on it. |
Glenn Kasten | 22eb4e2 | 2012-11-07 14:03:00 -0800 | [diff] [blame] | 143 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 144 | uint32_t mPad1; // unused |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 145 | |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 146 | volatile int32_t mFutex; // event flag: down (P) by client, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 147 | // up (V) by server or binderDied() or interrupt() |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 148 | #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] | 149 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 150 | private: |
| 151 | |
Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 152 | // This field should be a size_t, but since it is located in shared memory we |
| 153 | // force to 32-bit. The client and server may have different typedefs for size_t. |
| 154 | uint32_t mMinimum; // server wakes up client if available >= mMinimum |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 155 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 156 | // Stereo gains for AudioTrack only, not used by AudioRecord. |
| 157 | gain_minifloat_packed_t mVolumeLR; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 158 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 159 | uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz |
| 160 | // or 0 == default. Write-only client, read-only server. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 161 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 162 | // client write-only, server read-only |
| 163 | uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0 |
| 164 | |
Glenn Kasten | d054c32 | 2013-07-12 12:59:20 -0700 | [diff] [blame] | 165 | uint16_t mPad2; // unused |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 166 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 167 | public: |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 168 | |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 169 | volatile int32_t mFlags; // combinations of CBLK_* |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 170 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 171 | // Cache line boundary (32 bytes) |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 172 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 173 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 174 | union { |
| 175 | AudioTrackSharedStreaming mStreaming; |
| 176 | AudioTrackSharedStatic mStatic; |
| 177 | int mAlign[8]; |
| 178 | } u; |
| 179 | |
| 180 | // Cache line boundary (32 bytes) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 183 | // ---------------------------------------------------------------------------- |
| 184 | |
| 185 | // Proxy for shared memory control block, to isolate callers from needing to know the details. |
| 186 | // There is exactly one ClientProxy and one ServerProxy per shared memory control block. |
| 187 | // 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] | 188 | class Proxy : public RefBase { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 189 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 190 | Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut, |
| 191 | bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 192 | virtual ~Proxy() { } |
| 193 | |
| 194 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 195 | struct Buffer { |
| 196 | size_t mFrameCount; // number of frames available in this buffer |
| 197 | void* mRaw; // pointer to first frame |
| 198 | size_t mNonContig; // number of additional non-contiguous frames available |
| 199 | }; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 200 | |
| 201 | protected: |
| 202 | // These refer to shared memory, and are virtual addresses with respect to the current process. |
| 203 | // They may have different virtual addresses within the other process. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 204 | audio_track_cblk_t* const mCblk; // the control block |
| 205 | void* const mBuffers; // starting address of buffers |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 206 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 207 | const size_t mFrameCount; // not necessarily a power of 2 |
| 208 | const size_t mFrameSize; // in bytes |
| 209 | const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode |
| 210 | const bool mIsOut; // true for AudioTrack, false for AudioRecord |
| 211 | const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord |
| 212 | bool mIsShutdown; // latch set to true when shared memory corruption detected |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 213 | size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | // ---------------------------------------------------------------------------- |
| 217 | |
| 218 | // Proxy seen by AudioTrack client and AudioRecord client |
| 219 | class ClientProxy : public Proxy { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 220 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 221 | ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 222 | bool isOut, bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 223 | virtual ~ClientProxy() { } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 224 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 225 | static const struct timespec kForever; |
| 226 | static const struct timespec kNonBlocking; |
| 227 | |
| 228 | // Obtain a buffer with filled frames (reading) or empty frames (writing). |
| 229 | // It is permitted to call obtainBuffer() multiple times in succession, without any intervening |
| 230 | // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively |
| 231 | // sets or extends the unreleased frame count. |
| 232 | // On entry: |
| 233 | // buffer->mFrameCount should be initialized to maximum number of desired frames, |
| 234 | // which must be > 0. |
| 235 | // buffer->mNonContig is unused. |
| 236 | // buffer->mRaw is unused. |
| 237 | // requested is the requested timeout in local monotonic delta time units: |
| 238 | // NULL or &kNonBlocking means non-blocking (zero timeout). |
| 239 | // &kForever means block forever (infinite timeout). |
| 240 | // Other values mean a specific timeout in local monotonic delta time units. |
| 241 | // elapsed is a pointer to a location that will hold the total local monotonic time that |
| 242 | // elapsed while blocked, or NULL if not needed. |
| 243 | // On exit: |
| 244 | // buffer->mFrameCount has the actual number of contiguous available frames, |
| 245 | // which is always 0 when the return status != NO_ERROR. |
| 246 | // buffer->mNonContig is the number of additional non-contiguous available frames. |
| 247 | // buffer->mRaw is a pointer to the first available frame, |
| 248 | // or NULL when buffer->mFrameCount == 0. |
| 249 | // The return status is one of: |
| 250 | // NO_ERROR Success, buffer->mFrameCount > 0. |
| 251 | // WOULD_BLOCK Non-blocking mode and no frames are available. |
| 252 | // TIMED_OUT Timeout occurred before any frames became available. |
| 253 | // This can happen even for infinite timeout, due to a spurious wakeup. |
| 254 | // In this case, the caller should investigate and then re-try as appropriate. |
| 255 | // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create. |
| 256 | // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again. |
| 257 | // NO_INIT Shared memory is corrupt. |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 258 | // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 259 | status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL, |
| 260 | struct timespec *elapsed = NULL); |
| 261 | |
| 262 | // Release (some of) the frames last obtained. |
| 263 | // On entry, buffer->mFrameCount should have the number of frames to release, |
| 264 | // which must (cumulatively) be <= the number of frames last obtained but not yet released. |
| 265 | // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer(). |
| 266 | // It is permitted to call releaseBuffer() multiple times to release the frames in chunks. |
| 267 | // On exit: |
| 268 | // buffer->mFrameCount is zero. |
| 269 | // buffer->mRaw is NULL. |
| 270 | void releaseBuffer(Buffer* buffer); |
| 271 | |
| 272 | // Call after detecting server's death |
| 273 | void binderDied(); |
| 274 | |
| 275 | // Call to force an obtainBuffer() to return quickly with -EINTR |
| 276 | void interrupt(); |
| 277 | |
| 278 | size_t getPosition() { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 279 | return mEpoch + mCblk->mServer; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void setEpoch(size_t epoch) { |
| 283 | mEpoch = epoch; |
| 284 | } |
| 285 | |
| 286 | void setMinimum(size_t minimum) { |
Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 287 | // This can only happen on a 64-bit client |
| 288 | if (minimum > UINT32_MAX) { |
| 289 | minimum = UINT32_MAX; |
| 290 | } |
| 291 | mCblk->mMinimum = (uint32_t) minimum; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Return the number of frames that would need to be obtained and released |
| 295 | // in order for the client to be aligned at start of buffer |
| 296 | virtual size_t getMisalignment(); |
| 297 | |
| 298 | size_t getEpoch() const { |
| 299 | return mEpoch; |
| 300 | } |
| 301 | |
Eric Laurent | cc21e4f | 2013-10-16 15:12:32 -0700 | [diff] [blame] | 302 | size_t getFramesFilled(); |
| 303 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 304 | private: |
| 305 | size_t mEpoch; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | // ---------------------------------------------------------------------------- |
| 309 | |
| 310 | // Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack |
| 311 | class AudioTrackClientProxy : public ClientProxy { |
| 312 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 313 | AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 314 | size_t frameSize, bool clientInServer = false) |
| 315 | : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, |
| 316 | clientInServer) { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 317 | virtual ~AudioTrackClientProxy() { } |
| 318 | |
| 319 | // No barriers on the following operations, so the ordering of loads/stores |
| 320 | // with respect to other parameters is UNPREDICTABLE. That's considered safe. |
| 321 | |
| 322 | // caller must limit to 0.0 <= sendLevel <= 1.0 |
| 323 | void setSendLevel(float sendLevel) { |
| 324 | mCblk->mSendLevel = uint16_t(sendLevel * 0x1000); |
| 325 | } |
| 326 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 327 | // set stereo gains |
| 328 | void setVolumeLR(gain_minifloat_packed_t volumeLR) { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 329 | mCblk->mVolumeLR = volumeLR; |
| 330 | } |
| 331 | |
| 332 | void setSampleRate(uint32_t sampleRate) { |
| 333 | mCblk->mSampleRate = sampleRate; |
| 334 | } |
| 335 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 336 | virtual void flush(); |
| 337 | |
| 338 | virtual uint32_t getUnderrunFrames() const { |
| 339 | return mCblk->u.mStreaming.mUnderrunFrames; |
| 340 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 341 | |
| 342 | bool clearStreamEndDone(); // and return previous value |
| 343 | |
| 344 | bool getStreamEndDone() const; |
| 345 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 346 | status_t waitStreamEndDone(const struct timespec *requested); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | class StaticAudioTrackClientProxy : public AudioTrackClientProxy { |
| 350 | public: |
| 351 | StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 352 | size_t frameSize); |
| 353 | virtual ~StaticAudioTrackClientProxy() { } |
| 354 | |
| 355 | virtual void flush(); |
| 356 | |
| 357 | #define MIN_LOOP 16 // minimum length of each loop iteration in frames |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 358 | |
| 359 | // setLoop(), setBufferPosition(), and setBufferPositionAndLoop() set the |
| 360 | // static buffer position and looping parameters. These commands are not |
| 361 | // synchronous (they do not wait or block); instead they take effect at the |
| 362 | // next buffer data read from the server side. However, the client side |
| 363 | // getters will read a cached version of the position and loop variables |
| 364 | // until the setting takes effect. |
| 365 | // |
| 366 | // setBufferPositionAndLoop() is equivalent to calling, in order, setLoop() and |
| 367 | // setBufferPosition(). |
| 368 | // |
| 369 | // The functions should not be relied upon to do parameter or state checking. |
| 370 | // That is done at the AudioTrack level. |
| 371 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 372 | void setLoop(size_t loopStart, size_t loopEnd, int loopCount); |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 373 | void setBufferPosition(size_t position); |
| 374 | void setBufferPositionAndLoop(size_t position, size_t loopStart, size_t loopEnd, |
| 375 | int loopCount); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 376 | size_t getBufferPosition(); |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 377 | // getBufferPositionAndLoopCount() provides the proper snapshot of |
| 378 | // position and loopCount together. |
| 379 | void getBufferPositionAndLoopCount(size_t *position, int *loopCount); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 380 | |
| 381 | virtual size_t getMisalignment() { |
| 382 | return 0; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 385 | virtual uint32_t getUnderrunFrames() const { |
| 386 | return 0; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 389 | private: |
| 390 | StaticAudioTrackSingleStateQueue::Mutator mMutator; |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 391 | StaticAudioTrackPosLoopQueue::Observer mPosLoopObserver; |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 392 | StaticAudioTrackState mState; // last communicated state to server |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 393 | StaticAudioTrackPosLoop mPosLoop; // snapshot of position and loop. |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | // ---------------------------------------------------------------------------- |
| 397 | |
| 398 | // Proxy used by AudioRecord client |
| 399 | class AudioRecordClientProxy : public ClientProxy { |
| 400 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 401 | AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 402 | size_t frameSize) |
| 403 | : ClientProxy(cblk, buffers, frameCount, frameSize, |
| 404 | false /*isOut*/, false /*clientInServer*/) { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 405 | ~AudioRecordClientProxy() { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | // ---------------------------------------------------------------------------- |
| 409 | |
| 410 | // Proxy used by AudioFlinger server |
| 411 | class ServerProxy : public Proxy { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 412 | protected: |
| 413 | ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 414 | bool isOut, bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 415 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 416 | virtual ~ServerProxy() { } |
| 417 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 418 | // Obtain a buffer with filled frames (writing) or empty frames (reading). |
| 419 | // It is permitted to call obtainBuffer() multiple times in succession, without any intervening |
| 420 | // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively |
| 421 | // sets or extends the unreleased frame count. |
| 422 | // Always non-blocking. |
| 423 | // On entry: |
| 424 | // buffer->mFrameCount should be initialized to maximum number of desired frames, |
| 425 | // which must be > 0. |
| 426 | // buffer->mNonContig is unused. |
| 427 | // buffer->mRaw is unused. |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 428 | // 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] | 429 | // On exit: |
| 430 | // buffer->mFrameCount has the actual number of contiguous available frames, |
| 431 | // which is always 0 when the return status != NO_ERROR. |
| 432 | // buffer->mNonContig is the number of additional non-contiguous available frames. |
| 433 | // buffer->mRaw is a pointer to the first available frame, |
| 434 | // or NULL when buffer->mFrameCount == 0. |
| 435 | // The return status is one of: |
| 436 | // NO_ERROR Success, buffer->mFrameCount > 0. |
| 437 | // WOULD_BLOCK No frames are available. |
| 438 | // NO_INIT Shared memory is corrupt. |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 439 | virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 440 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 441 | // Release (some of) the frames last obtained. |
| 442 | // On entry, buffer->mFrameCount should have the number of frames to release, |
| 443 | // which must (cumulatively) be <= the number of frames last obtained but not yet released. |
| 444 | // It is permitted to call releaseBuffer() multiple times to release the frames in chunks. |
| 445 | // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer(). |
| 446 | // On exit: |
| 447 | // buffer->mFrameCount is zero. |
| 448 | // buffer->mRaw is NULL. |
| 449 | virtual void releaseBuffer(Buffer* buffer); |
| 450 | |
| 451 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 452 | size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer() |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 453 | int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | // Proxy used by AudioFlinger for servicing AudioTrack |
| 457 | class AudioTrackServerProxy : public ServerProxy { |
| 458 | public: |
| 459 | AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 460 | size_t frameSize, bool clientInServer = false, uint32_t sampleRate = 0) |
| 461 | : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) { |
| 462 | mCblk->mSampleRate = sampleRate; |
| 463 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 464 | protected: |
| 465 | virtual ~AudioTrackServerProxy() { } |
| 466 | |
| 467 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 468 | // return value of these methods must be validated by the caller |
| 469 | uint32_t getSampleRate() const { return mCblk->mSampleRate; } |
| 470 | uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 471 | gain_minifloat_packed_t getVolumeLR() const { return mCblk->mVolumeLR; } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 472 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 473 | // estimated total number of filled frames available to server to read, |
| 474 | // which may include non-contiguous frames |
| 475 | virtual size_t framesReady(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 476 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 477 | // Currently AudioFlinger will call framesReady() for a fast track from two threads: |
| 478 | // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended |
| 479 | // to be called from at most one thread of server, and one thread of client. |
| 480 | // As a temporary workaround, this method informs the proxy implementation that it |
| 481 | // should avoid doing a state queue poll from within framesReady(). |
| 482 | // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread. |
| 483 | virtual void framesReadyIsCalledByMultipleThreads() { } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 484 | |
| 485 | bool setStreamEndDone(); // and return previous value |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 486 | |
| 487 | // Add to the tally of underrun frames, and inform client of underrun |
| 488 | virtual void tallyUnderrunFrames(uint32_t frameCount); |
| 489 | |
| 490 | // Return the total number of frames which AudioFlinger desired but were unavailable, |
| 491 | // and thus which resulted in an underrun. |
| 492 | virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 493 | |
| 494 | // Return the total number of frames that AudioFlinger has obtained and released |
| 495 | virtual size_t framesReleased() const { return mCblk->mServer; } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | class StaticAudioTrackServerProxy : public AudioTrackServerProxy { |
| 499 | public: |
| 500 | StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 501 | size_t frameSize); |
| 502 | protected: |
| 503 | virtual ~StaticAudioTrackServerProxy() { } |
| 504 | |
| 505 | public: |
| 506 | virtual size_t framesReady(); |
| 507 | virtual void framesReadyIsCalledByMultipleThreads(); |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 508 | virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 509 | virtual void releaseBuffer(Buffer* buffer); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 510 | virtual void tallyUnderrunFrames(uint32_t frameCount); |
| 511 | virtual uint32_t getUnderrunFrames() const { return 0; } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 512 | |
| 513 | private: |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 514 | status_t updateStateWithLoop(StaticAudioTrackState *localState, |
| 515 | const StaticAudioTrackState &update) const; |
| 516 | status_t updateStateWithPosition(StaticAudioTrackState *localState, |
| 517 | const StaticAudioTrackState &update) const; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 518 | ssize_t pollPosition(); // poll for state queue update, and return current position |
| 519 | StaticAudioTrackSingleStateQueue::Observer mObserver; |
Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame^] | 520 | StaticAudioTrackPosLoopQueue::Mutator mPosLoopMutator; |
Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 521 | size_t mFramesReadySafe; // Assuming size_t read/writes are atomic on 32 / 64 bit |
| 522 | // processors, this is a thread-safe version of |
| 523 | // mFramesReady. |
| 524 | int64_t mFramesReady; // The number of frames ready in the static buffer |
| 525 | // including loops. This is 64 bits since loop mode |
| 526 | // can cause a track to appear to have a large number |
| 527 | // of frames. INT64_MAX means an infinite loop. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 528 | bool mFramesReadyIsCalledByMultipleThreads; |
Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 529 | StaticAudioTrackState mState; // Server side state. Any updates from client must be |
| 530 | // passed by the mObserver SingleStateQueue. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 531 | }; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 532 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 533 | // Proxy used by AudioFlinger for servicing AudioRecord |
| 534 | class AudioRecordServerProxy : public ServerProxy { |
| 535 | public: |
| 536 | AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 537 | size_t frameSize, bool clientInServer) |
| 538 | : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/, clientInServer) { } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 539 | protected: |
| 540 | virtual ~AudioRecordServerProxy() { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 541 | }; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | |
| 543 | // ---------------------------------------------------------------------------- |
| 544 | |
| 545 | }; // namespace android |
| 546 | |
| 547 | #endif // ANDROID_AUDIO_TRACK_SHARED_H |