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