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 | |
| 23 | #include <utils/threads.h> |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 25 | #include <utils/RefBase.h> |
| 26 | #include <media/nbaio/roundup.h> |
| 27 | #include <media/SingleStateQueue.h> |
| 28 | #include <private/media/StaticAudioTrackState.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 | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 34 | #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] | 35 | #define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger, |
Glenn Kasten | 9c5fdd8 | 2012-11-05 13:38:15 -0800 | [diff] [blame] | 36 | // clear: track is ready when buffer full |
Glenn Kasten | 864585d | 2012-11-06 16:15:41 -0800 | [diff] [blame] | 37 | #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] | 38 | #define CBLK_DISABLED 0x08 // output track disabled by AudioFlinger due to underrun, |
| 39 | // need to re-start. Unlike CBLK_UNDERRUN, this is not set |
| 40 | // immediately, but only after a long string of underruns. |
| 41 | // 0x10 unused |
| 42 | #define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes |
| 43 | #define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes |
| 44 | #define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping |
| 45 | #define CBLK_OVERRUN 0x100 // set by server immediately on input overrun, cleared by client |
| 46 | #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] | 47 | #define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client |
| 48 | |
| 49 | //EL_FIXME 20 seconds may not be enough and must be reconciled with new obtainBuffer implementation |
| 50 | #define MAX_RUN_OFFLOADED_TIMEOUT_MS 20000 //assuming upto a maximum of 20 seconds of offloaded |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 52 | struct AudioTrackSharedStreaming { |
| 53 | // similar to NBAIO MonoPipe |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 54 | // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2 |
| 55 | volatile int32_t mFront; // read by server |
| 56 | volatile int32_t mRear; // write by client |
| 57 | volatile int32_t mFlush; // incremented by client to indicate a request to flush; |
| 58 | // server notices and discards all data between mFront and mRear |
| 59 | volatile uint32_t mUnderrunFrames; // server increments for each unavailable but desired frame |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 62 | typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue; |
| 63 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 64 | struct AudioTrackSharedStatic { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 65 | StaticAudioTrackSingleStateQueue::Shared |
| 66 | mSingleStateQueue; |
| 67 | size_t mBufferPosition; // updated asynchronously by server, |
| 68 | // "for entertainment purposes only" |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | // ---------------------------------------------------------------------------- |
| 72 | |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 73 | // Important: do not add any virtual methods, including ~ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | struct audio_track_cblk_t |
| 75 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 76 | // Since the control block is always located in shared memory, this constructor |
| 77 | // is only used for placement new(). It is never used for regular new() or stack. |
| 78 | audio_track_cblk_t(); |
| 79 | /*virtual*/ ~audio_track_cblk_t() { } |
| 80 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 81 | friend class Proxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 82 | friend class ClientProxy; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 83 | friend class AudioTrackClientProxy; |
| 84 | friend class AudioRecordClientProxy; |
| 85 | friend class ServerProxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 86 | friend class AudioTrackServerProxy; |
| 87 | friend class AudioRecordServerProxy; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
| 89 | // The data members are grouped so that members accessed frequently and in the same context |
| 90 | // are in the same line of data cache. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 91 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 92 | volatile uint32_t server; // updated asynchronously by server, |
| 93 | // "for entertainment purposes only" |
Glenn Kasten | 22eb4e2 | 2012-11-07 14:03:00 -0800 | [diff] [blame] | 94 | |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 95 | size_t frameCount_; // used during creation to pass actual track buffer size |
| 96 | // from AudioFlinger to client, and not referenced again |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 97 | // FIXME remove here and replace by createTrack() in/out |
| 98 | // parameter |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 99 | // renamed to "_" to detect incorrect use |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 100 | |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 101 | volatile int32_t mFutex; // event flag: down (P) by client, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 102 | // up (V) by server or binderDied() or interrupt() |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 103 | #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] | 104 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 105 | private: |
| 106 | |
| 107 | size_t mMinimum; // server wakes up client if available >= mMinimum |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 108 | |
| 109 | // Channel volumes are fixed point U4.12, so 0x1000 means 1.0. |
| 110 | // Left channel is in [0:15], right channel is in [16:31]. |
| 111 | // Always read and write the combined pair atomically. |
| 112 | // For AudioTrack only, not used by AudioRecord. |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 113 | uint32_t mVolumeLR; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 114 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 115 | uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz |
| 116 | // or 0 == default. Write-only client, read-only server. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 117 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 118 | // client write-only, server read-only |
| 119 | uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0 |
| 120 | |
Glenn Kasten | 83a0382 | 2012-11-12 07:58:20 -0800 | [diff] [blame] | 121 | uint8_t mPad2; // unused |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 122 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 123 | public: |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 124 | // read-only for client, server writes once at initialization and is then read-only |
Glenn Kasten | 0c9d26d | 2012-05-31 14:35:01 -0700 | [diff] [blame] | 125 | uint8_t mName; // normal tracks: track name, fast tracks: track index |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 126 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 127 | volatile int32_t flags; |
| 128 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 129 | // Cache line boundary (32 bytes) |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 130 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 131 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 132 | union { |
| 133 | AudioTrackSharedStreaming mStreaming; |
| 134 | AudioTrackSharedStatic mStatic; |
| 135 | int mAlign[8]; |
| 136 | } u; |
| 137 | |
| 138 | // Cache line boundary (32 bytes) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 141 | // ---------------------------------------------------------------------------- |
| 142 | |
| 143 | // Proxy for shared memory control block, to isolate callers from needing to know the details. |
| 144 | // There is exactly one ClientProxy and one ServerProxy per shared memory control block. |
| 145 | // 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] | 146 | class Proxy : public RefBase { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 147 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 148 | Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut, |
| 149 | bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 150 | virtual ~Proxy() { } |
| 151 | |
| 152 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 153 | struct Buffer { |
| 154 | size_t mFrameCount; // number of frames available in this buffer |
| 155 | void* mRaw; // pointer to first frame |
| 156 | size_t mNonContig; // number of additional non-contiguous frames available |
| 157 | }; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 158 | |
| 159 | protected: |
| 160 | // These refer to shared memory, and are virtual addresses with respect to the current process. |
| 161 | // They may have different virtual addresses within the other process. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 162 | audio_track_cblk_t* const mCblk; // the control block |
| 163 | void* const mBuffers; // starting address of buffers |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 164 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 165 | const size_t mFrameCount; // not necessarily a power of 2 |
| 166 | const size_t mFrameSize; // in bytes |
| 167 | const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode |
| 168 | const bool mIsOut; // true for AudioTrack, false for AudioRecord |
| 169 | const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord |
| 170 | bool mIsShutdown; // latch set to true when shared memory corruption detected |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame^] | 171 | size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | // ---------------------------------------------------------------------------- |
| 175 | |
| 176 | // Proxy seen by AudioTrack client and AudioRecord client |
| 177 | class ClientProxy : public Proxy { |
| 178 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 179 | ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 180 | bool isOut, bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 181 | virtual ~ClientProxy() { } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 182 | |
| 183 | public: |
| 184 | static const struct timespec kForever; |
| 185 | static const struct timespec kNonBlocking; |
| 186 | |
| 187 | // Obtain a buffer with filled frames (reading) or empty frames (writing). |
| 188 | // It is permitted to call obtainBuffer() multiple times in succession, without any intervening |
| 189 | // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively |
| 190 | // sets or extends the unreleased frame count. |
| 191 | // On entry: |
| 192 | // buffer->mFrameCount should be initialized to maximum number of desired frames, |
| 193 | // which must be > 0. |
| 194 | // buffer->mNonContig is unused. |
| 195 | // buffer->mRaw is unused. |
| 196 | // requested is the requested timeout in local monotonic delta time units: |
| 197 | // NULL or &kNonBlocking means non-blocking (zero timeout). |
| 198 | // &kForever means block forever (infinite timeout). |
| 199 | // Other values mean a specific timeout in local monotonic delta time units. |
| 200 | // elapsed is a pointer to a location that will hold the total local monotonic time that |
| 201 | // elapsed while blocked, or NULL if not needed. |
| 202 | // On exit: |
| 203 | // buffer->mFrameCount has the actual number of contiguous available frames, |
| 204 | // which is always 0 when the return status != NO_ERROR. |
| 205 | // buffer->mNonContig is the number of additional non-contiguous available frames. |
| 206 | // buffer->mRaw is a pointer to the first available frame, |
| 207 | // or NULL when buffer->mFrameCount == 0. |
| 208 | // The return status is one of: |
| 209 | // NO_ERROR Success, buffer->mFrameCount > 0. |
| 210 | // WOULD_BLOCK Non-blocking mode and no frames are available. |
| 211 | // TIMED_OUT Timeout occurred before any frames became available. |
| 212 | // This can happen even for infinite timeout, due to a spurious wakeup. |
| 213 | // In this case, the caller should investigate and then re-try as appropriate. |
| 214 | // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create. |
| 215 | // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again. |
| 216 | // NO_INIT Shared memory is corrupt. |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame^] | 217 | // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 218 | status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL, |
| 219 | struct timespec *elapsed = NULL); |
| 220 | |
| 221 | // Release (some of) the frames last obtained. |
| 222 | // On entry, buffer->mFrameCount should have the number of frames to release, |
| 223 | // which must (cumulatively) be <= the number of frames last obtained but not yet released. |
| 224 | // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer(). |
| 225 | // It is permitted to call releaseBuffer() multiple times to release the frames in chunks. |
| 226 | // On exit: |
| 227 | // buffer->mFrameCount is zero. |
| 228 | // buffer->mRaw is NULL. |
| 229 | void releaseBuffer(Buffer* buffer); |
| 230 | |
| 231 | // Call after detecting server's death |
| 232 | void binderDied(); |
| 233 | |
| 234 | // Call to force an obtainBuffer() to return quickly with -EINTR |
| 235 | void interrupt(); |
| 236 | |
| 237 | size_t getPosition() { |
| 238 | return mEpoch + mCblk->server; |
| 239 | } |
| 240 | |
| 241 | void setEpoch(size_t epoch) { |
| 242 | mEpoch = epoch; |
| 243 | } |
| 244 | |
| 245 | void setMinimum(size_t minimum) { |
| 246 | mCblk->mMinimum = minimum; |
| 247 | } |
| 248 | |
| 249 | // Return the number of frames that would need to be obtained and released |
| 250 | // in order for the client to be aligned at start of buffer |
| 251 | virtual size_t getMisalignment(); |
| 252 | |
| 253 | size_t getEpoch() const { |
| 254 | return mEpoch; |
| 255 | } |
| 256 | |
| 257 | private: |
| 258 | size_t mEpoch; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | // ---------------------------------------------------------------------------- |
| 262 | |
| 263 | // Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack |
| 264 | class AudioTrackClientProxy : public ClientProxy { |
| 265 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 266 | AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 267 | size_t frameSize, bool clientInServer = false) |
| 268 | : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, |
| 269 | clientInServer) { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 270 | virtual ~AudioTrackClientProxy() { } |
| 271 | |
| 272 | // No barriers on the following operations, so the ordering of loads/stores |
| 273 | // with respect to other parameters is UNPREDICTABLE. That's considered safe. |
| 274 | |
| 275 | // caller must limit to 0.0 <= sendLevel <= 1.0 |
| 276 | void setSendLevel(float sendLevel) { |
| 277 | mCblk->mSendLevel = uint16_t(sendLevel * 0x1000); |
| 278 | } |
| 279 | |
| 280 | // caller must limit to 0 <= volumeLR <= 0x10001000 |
| 281 | void setVolumeLR(uint32_t volumeLR) { |
| 282 | mCblk->mVolumeLR = volumeLR; |
| 283 | } |
| 284 | |
| 285 | void setSampleRate(uint32_t sampleRate) { |
| 286 | mCblk->mSampleRate = sampleRate; |
| 287 | } |
| 288 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 289 | virtual void flush(); |
| 290 | |
| 291 | virtual uint32_t getUnderrunFrames() const { |
| 292 | return mCblk->u.mStreaming.mUnderrunFrames; |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | class StaticAudioTrackClientProxy : public AudioTrackClientProxy { |
| 297 | public: |
| 298 | StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 299 | size_t frameSize); |
| 300 | virtual ~StaticAudioTrackClientProxy() { } |
| 301 | |
| 302 | virtual void flush(); |
| 303 | |
| 304 | #define MIN_LOOP 16 // minimum length of each loop iteration in frames |
| 305 | void setLoop(size_t loopStart, size_t loopEnd, int loopCount); |
| 306 | size_t getBufferPosition(); |
| 307 | |
| 308 | virtual size_t getMisalignment() { |
| 309 | return 0; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 312 | virtual uint32_t getUnderrunFrames() const { |
| 313 | return 0; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 316 | private: |
| 317 | StaticAudioTrackSingleStateQueue::Mutator mMutator; |
| 318 | size_t mBufferPosition; // so that getBufferPosition() appears to be synchronous |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | // ---------------------------------------------------------------------------- |
| 322 | |
| 323 | // Proxy used by AudioRecord client |
| 324 | class AudioRecordClientProxy : public ClientProxy { |
| 325 | public: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 326 | AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 327 | size_t frameSize) |
| 328 | : ClientProxy(cblk, buffers, frameCount, frameSize, |
| 329 | false /*isOut*/, false /*clientInServer*/) { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 330 | ~AudioRecordClientProxy() { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | // ---------------------------------------------------------------------------- |
| 334 | |
| 335 | // Proxy used by AudioFlinger server |
| 336 | class ServerProxy : public Proxy { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 337 | protected: |
| 338 | ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 339 | bool isOut, bool clientInServer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 340 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 341 | virtual ~ServerProxy() { } |
| 342 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 343 | // Obtain a buffer with filled frames (writing) or empty frames (reading). |
| 344 | // It is permitted to call obtainBuffer() multiple times in succession, without any intervening |
| 345 | // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively |
| 346 | // sets or extends the unreleased frame count. |
| 347 | // Always non-blocking. |
| 348 | // On entry: |
| 349 | // buffer->mFrameCount should be initialized to maximum number of desired frames, |
| 350 | // which must be > 0. |
| 351 | // buffer->mNonContig is unused. |
| 352 | // buffer->mRaw is unused. |
| 353 | // On exit: |
| 354 | // buffer->mFrameCount has the actual number of contiguous available frames, |
| 355 | // which is always 0 when the return status != NO_ERROR. |
| 356 | // buffer->mNonContig is the number of additional non-contiguous available frames. |
| 357 | // buffer->mRaw is a pointer to the first available frame, |
| 358 | // or NULL when buffer->mFrameCount == 0. |
| 359 | // The return status is one of: |
| 360 | // NO_ERROR Success, buffer->mFrameCount > 0. |
| 361 | // WOULD_BLOCK No frames are available. |
| 362 | // NO_INIT Shared memory is corrupt. |
| 363 | virtual status_t obtainBuffer(Buffer* buffer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 364 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 365 | // Release (some of) the frames last obtained. |
| 366 | // On entry, buffer->mFrameCount should have the number of frames to release, |
| 367 | // which must (cumulatively) be <= the number of frames last obtained but not yet released. |
| 368 | // It is permitted to call releaseBuffer() multiple times to release the frames in chunks. |
| 369 | // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer(). |
| 370 | // On exit: |
| 371 | // buffer->mFrameCount is zero. |
| 372 | // buffer->mRaw is NULL. |
| 373 | virtual void releaseBuffer(Buffer* buffer); |
| 374 | |
| 375 | protected: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 376 | size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer() |
| 377 | private: |
| 378 | int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only |
| 379 | bool mDeferWake; // whether another releaseBuffer() is expected soon |
| 380 | }; |
| 381 | |
| 382 | // Proxy used by AudioFlinger for servicing AudioTrack |
| 383 | class AudioTrackServerProxy : public ServerProxy { |
| 384 | public: |
| 385 | AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 386 | size_t frameSize, bool clientInServer = false) |
| 387 | : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) { } |
| 388 | protected: |
| 389 | virtual ~AudioTrackServerProxy() { } |
| 390 | |
| 391 | public: |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 392 | // return value of these methods must be validated by the caller |
| 393 | uint32_t getSampleRate() const { return mCblk->mSampleRate; } |
| 394 | uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; } |
| 395 | uint32_t getVolumeLR() const { return mCblk->mVolumeLR; } |
| 396 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 397 | // estimated total number of filled frames available to server to read, |
| 398 | // which may include non-contiguous frames |
| 399 | virtual size_t framesReady(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 400 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 401 | // Currently AudioFlinger will call framesReady() for a fast track from two threads: |
| 402 | // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended |
| 403 | // to be called from at most one thread of server, and one thread of client. |
| 404 | // As a temporary workaround, this method informs the proxy implementation that it |
| 405 | // should avoid doing a state queue poll from within framesReady(). |
| 406 | // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread. |
| 407 | virtual void framesReadyIsCalledByMultipleThreads() { } |
| 408 | }; |
| 409 | |
| 410 | class StaticAudioTrackServerProxy : public AudioTrackServerProxy { |
| 411 | public: |
| 412 | StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 413 | size_t frameSize); |
| 414 | protected: |
| 415 | virtual ~StaticAudioTrackServerProxy() { } |
| 416 | |
| 417 | public: |
| 418 | virtual size_t framesReady(); |
| 419 | virtual void framesReadyIsCalledByMultipleThreads(); |
| 420 | virtual status_t obtainBuffer(Buffer* buffer); |
| 421 | virtual void releaseBuffer(Buffer* buffer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 422 | |
| 423 | private: |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 424 | ssize_t pollPosition(); // poll for state queue update, and return current position |
| 425 | StaticAudioTrackSingleStateQueue::Observer mObserver; |
| 426 | size_t mPosition; // server's current play position in frames, relative to 0 |
| 427 | size_t mEnd; // cached value computed from mState, safe for asynchronous read |
| 428 | bool mFramesReadyIsCalledByMultipleThreads; |
| 429 | StaticAudioTrackState mState; |
| 430 | }; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 431 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 432 | // Proxy used by AudioFlinger for servicing AudioRecord |
| 433 | class AudioRecordServerProxy : public ServerProxy { |
| 434 | public: |
| 435 | AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 436 | size_t frameSize) |
| 437 | : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/, |
| 438 | false /*clientInServer*/) { } |
| 439 | protected: |
| 440 | virtual ~AudioRecordServerProxy() { } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 441 | }; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | |
| 443 | // ---------------------------------------------------------------------------- |
| 444 | |
| 445 | }; // namespace android |
| 446 | |
| 447 | #endif // ANDROID_AUDIO_TRACK_SHARED_H |