AudioTrackShared.h revision bfb1b832079bbb9426f72f3863199a54aefd02da
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2007 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) * See the License for the specific language governing permissions and
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef ANDROID_AUDIO_TRACK_SHARED_H
18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#define ANDROID_AUDIO_TRACK_SHARED_H
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <stdint.h>
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <sys/types.h>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/threads.h>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/Log.h>
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/RefBase.h>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <media/nbaio/roundup.h>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <media/SingleStateQueue.h>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <private/media/StaticAudioTrackState.h>
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace android {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------------
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_UNDERRUN   0x01 // set by server immediately on output underrun, cleared by client
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // clear: track is ready when buffer full
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_INVALID    0x04 // track buffer invalidated by AudioFlinger, need to re-create
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_DISABLED   0x08 // output track disabled by AudioFlinger due to underrun,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // need to re-start.  Unlike CBLK_UNDERRUN, this is not set
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // immediately, but only after a long string of underruns.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 0x10 unused
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_OVERRUN   0x100 // set by server immediately on input overrun, cleared by client
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_INTERRUPT 0x200 // set by client on interrupt(), cleared by client in obtainBuffer()
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//EL_FIXME 20 seconds may not be enough and must be reconciled with new obtainBuffer implementation
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MAX_RUN_OFFLOADED_TIMEOUT_MS 20000 //assuming upto a maximum of 20 seconds of offloaded
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct AudioTrackSharedStreaming {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // similar to NBAIO MonoPipe
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile int32_t mFront;    // read by server
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile int32_t mRear;     // write by client
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile int32_t mFlush;    // incremented by client to indicate a request to flush;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                // server notices and discards all data between mFront and mRear
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile uint32_t mUnderrunFrames;  // server increments for each unavailable but desired frame
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct AudioTrackSharedStatic {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StaticAudioTrackSingleStateQueue::Shared
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    mSingleStateQueue;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t          mBufferPosition;    // updated asynchronously by server,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        // "for entertainment purposes only"
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------------
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Important: do not add any virtual methods, including ~
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct audio_track_cblk_t
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // Since the control block is always located in shared memory, this constructor
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // is only used for placement new().  It is never used for regular new() or stack.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            audio_track_cblk_t();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                /*virtual*/ ~audio_track_cblk_t() { }
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class Proxy;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class ClientProxy;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class AudioTrackClientProxy;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class AudioRecordClientProxy;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class ServerProxy;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class AudioTrackServerProxy;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                friend class AudioRecordServerProxy;
88010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The data members are grouped so that members accessed frequently and in the same context
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // are in the same line of data cache.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile    uint32_t    server;     // updated asynchronously by server,
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        // "for entertainment purposes only"
94010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                size_t      frameCount_;    // used during creation to pass actual track buffer size
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            // from AudioFlinger to client, and not referenced again
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            // FIXME remove here and replace by createTrack() in/out
98010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                            // parameter
99010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                            // renamed to "_" to detect incorrect use
100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile    int32_t     mFutex;     // event flag: down (P) by client,
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        // up (V) by server or binderDied() or interrupt()
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CBLK_FUTEX_WAKE 1               // if event flag bit is set, then a deferred wake is pending
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)private:
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                size_t      mMinimum;       // server wakes up client if available >= mMinimum
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                // Channel volumes are fixed point U4.12, so 0x1000 means 1.0.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // Left channel is in [0:15], right channel is in [16:31].
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // Always read and write the combined pair atomically.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // For AudioTrack only, not used by AudioRecord.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                uint32_t    mVolumeLR;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                uint32_t    mSampleRate;    // AudioTrack only: client's requested sample rate in Hz
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            // or 0 == default. Write-only client, read-only server.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                // client write-only, server read-only
119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                uint16_t    mSendLevel;      // Fixed point U4.12 so 0x1000 means 1.0
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                uint8_t     mPad2;           // unused
122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)public:
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // read-only for client, server writes once at initialization and is then read-only
125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                uint8_t     mName;           // normal tracks: track name, fast tracks: track index
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    volatile    int32_t     flags;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // Cache line boundary (32 bytes)
130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
131010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)public:
132010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                union {
133010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                    AudioTrackSharedStreaming   mStreaming;
134010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                    AudioTrackSharedStatic      mStatic;
135010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                    int                         mAlign[8];
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                } u;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // Cache line boundary (32 bytes)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
140010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// ----------------------------------------------------------------------------
142010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Proxy for shared memory control block, to isolate callers from needing to know the details.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There is exactly one ClientProxy and one ServerProxy per shared memory control block.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The proxies are located in normal memory, and are not multi-thread safe within a given side.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Proxy : public RefBase {
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)protected:
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut,
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            bool clientInServer);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~Proxy() { }
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    struct Buffer {
154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        size_t  mFrameCount;            // number of frames available in this buffer
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        void*   mRaw;                   // pointer to first frame
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        size_t  mNonContig;             // number of additional non-contiguous frames available
1574ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    };
1584ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch
1594ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochprotected:
1604ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    // These refer to shared memory, and are virtual addresses with respect to the current process.
1614ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    // They may have different virtual addresses within the other process.
162010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    audio_track_cblk_t* const   mCblk;  // the control block
1634ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    void* const     mBuffers;           // starting address of buffers
1644ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch
1654ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    const size_t    mFrameCount;        // not necessarily a power of 2
1664ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    const size_t    mFrameSize;         // in bytes
167010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    const size_t    mFrameCountP2;      // mFrameCount rounded to power of 2, streaming mode
1684ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    const bool      mIsOut;             // true for AudioTrack, false for AudioRecord
1694ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    const bool      mClientInServer;    // true for OutputTrack, false for AudioTrack & AudioRecord
1704ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    bool            mIsShutdown;        // latch set to true when shared memory corruption detected
1714ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch    size_t          mUnreleased;        // unreleased frames remaining from most recent obtainBuffer
1724ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch};
1734ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch
1744ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// ----------------------------------------------------------------------------
1754ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Proxy seen by AudioTrack client and AudioRecord client
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ClientProxy : public Proxy {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)protected:
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            bool isOut, bool clientInServer);
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~ClientProxy() { }
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const struct timespec kForever;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const struct timespec kNonBlocking;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Obtain a buffer with filled frames (reading) or empty frames (writing).
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // calls to releaseBuffer().  In that case, the final obtainBuffer() is the one that effectively
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // sets or extends the unreleased frame count.
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // On entry:
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //  buffer->mFrameCount should be initialized to maximum number of desired frames,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //      which must be > 0.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //  buffer->mNonContig is unused.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //  buffer->mRaw is unused.
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //  requested is the requested timeout in local monotonic delta time units:
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //      NULL or &kNonBlocking means non-blocking (zero timeout).
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //      &kForever means block forever (infinite timeout).
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //      Other values mean a specific timeout in local monotonic delta time units.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //  elapsed is a pointer to a location that will hold the total local monotonic time that
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //      elapsed while blocked, or NULL if not needed.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // On exit:
2034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //  buffer->mFrameCount has the actual number of contiguous available frames,
2044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //      which is always 0 when the return status != NO_ERROR.
2054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //  buffer->mNonContig is the number of additional non-contiguous available frames.
2064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //  buffer->mRaw is a pointer to the first available frame,
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //      or NULL when buffer->mFrameCount == 0.
2084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // The return status is one of:
2094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //  NO_ERROR    Success, buffer->mFrameCount > 0.
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //  WOULD_BLOCK Non-blocking mode and no frames are available.
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //  TIMED_OUT   Timeout occurred before any frames became available.
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //              This can happen even for infinite timeout, due to a spurious wakeup.
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //              In this case, the caller should investigate and then re-try as appropriate.
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //  DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create.
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //  -EINTR      Call has been interrupted.  Look around to see why, and then perhaps try again.
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //  NO_INIT     Shared memory is corrupt.
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0.
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    status_t    obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL,
219f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            struct timespec *elapsed = NULL);
220f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
221f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Release (some of) the frames last obtained.
222f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // On entry, buffer->mFrameCount should have the number of frames to release,
223f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // which must (cumulatively) be <= the number of frames last obtained but not yet released.
224f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
225f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
226f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // On exit:
227f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    //  buffer->mFrameCount is zero.
228f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    //  buffer->mRaw is NULL.
229f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    void        releaseBuffer(Buffer* buffer);
230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Call after detecting server's death
232f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    void        binderDied();
233f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
234f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Call to force an obtainBuffer() to return quickly with -EINTR
235f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    void        interrupt();
236f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
237f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t      getPosition() {
238f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        return mEpoch + mCblk->server;
239f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
241f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    void        setEpoch(size_t epoch) {
242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        mEpoch = epoch;
243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
244f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
245f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    void        setMinimum(size_t minimum) {
246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        mCblk->mMinimum = minimum;
247f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
248f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Return the number of frames that would need to be obtained and released
250f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // in order for the client to be aligned at start of buffer
251f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    virtual size_t  getMisalignment();
252f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t      getEpoch() const {
254f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        return mEpoch;
255f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
256f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
257f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)private:
258f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t      mEpoch;
259f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)};
260f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
261f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// ----------------------------------------------------------------------------
262f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
263f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack
264f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class AudioTrackClientProxy : public ClientProxy {
265f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)public:
266f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
267f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            size_t frameSize, bool clientInServer = false)
268f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/,
269f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          clientInServer) { }
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~AudioTrackClientProxy() { }
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // No barriers on the following operations, so the ordering of loads/stores
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // with respect to other parameters is UNPREDICTABLE. That's considered safe.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // caller must limit to 0.0 <= sendLevel <= 1.0
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    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
289    virtual void flush();
290
291    virtual uint32_t    getUnderrunFrames() const {
292        return mCblk->u.mStreaming.mUnderrunFrames;
293    }
294
295    bool        clearStreamEndDone();   // and return previous value
296
297    bool        getStreamEndDone() const;
298
299};
300
301class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
302public:
303    StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
304            size_t frameSize);
305    virtual ~StaticAudioTrackClientProxy() { }
306
307    virtual void    flush();
308
309#define MIN_LOOP    16  // minimum length of each loop iteration in frames
310            void    setLoop(size_t loopStart, size_t loopEnd, int loopCount);
311            size_t  getBufferPosition();
312
313    virtual size_t  getMisalignment() {
314        return 0;
315    }
316
317    virtual uint32_t    getUnderrunFrames() const {
318        return 0;
319    }
320
321private:
322    StaticAudioTrackSingleStateQueue::Mutator   mMutator;
323    size_t          mBufferPosition;    // so that getBufferPosition() appears to be synchronous
324};
325
326// ----------------------------------------------------------------------------
327
328// Proxy used by AudioRecord client
329class AudioRecordClientProxy : public ClientProxy {
330public:
331    AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
332            size_t frameSize)
333        : ClientProxy(cblk, buffers, frameCount, frameSize,
334            false /*isOut*/, false /*clientInServer*/) { }
335    ~AudioRecordClientProxy() { }
336};
337
338// ----------------------------------------------------------------------------
339
340// Proxy used by AudioFlinger server
341class ServerProxy : public Proxy {
342protected:
343    ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
344            bool isOut, bool clientInServer);
345public:
346    virtual ~ServerProxy() { }
347
348    // Obtain a buffer with filled frames (writing) or empty frames (reading).
349    // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
350    // calls to releaseBuffer().  In that case, the final obtainBuffer() is the one that effectively
351    // sets or extends the unreleased frame count.
352    // Always non-blocking.
353    // On entry:
354    //  buffer->mFrameCount should be initialized to maximum number of desired frames,
355    //      which must be > 0.
356    //  buffer->mNonContig is unused.
357    //  buffer->mRaw is unused.
358    // On exit:
359    //  buffer->mFrameCount has the actual number of contiguous available frames,
360    //      which is always 0 when the return status != NO_ERROR.
361    //  buffer->mNonContig is the number of additional non-contiguous available frames.
362    //  buffer->mRaw is a pointer to the first available frame,
363    //      or NULL when buffer->mFrameCount == 0.
364    // The return status is one of:
365    //  NO_ERROR    Success, buffer->mFrameCount > 0.
366    //  WOULD_BLOCK No frames are available.
367    //  NO_INIT     Shared memory is corrupt.
368    virtual status_t    obtainBuffer(Buffer* buffer);
369
370    // Release (some of) the frames last obtained.
371    // On entry, buffer->mFrameCount should have the number of frames to release,
372    // which must (cumulatively) be <= the number of frames last obtained but not yet released.
373    // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
374    // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
375    // On exit:
376    //  buffer->mFrameCount is zero.
377    //  buffer->mRaw is NULL.
378    virtual void        releaseBuffer(Buffer* buffer);
379
380protected:
381    size_t      mAvailToClient; // estimated frames available to client prior to releaseBuffer()
382private:
383    int32_t     mFlush;         // our copy of cblk->u.mStreaming.mFlush, for streaming output only
384    bool        mDeferWake;     // whether another releaseBuffer() is expected soon
385};
386
387// Proxy used by AudioFlinger for servicing AudioTrack
388class AudioTrackServerProxy : public ServerProxy {
389public:
390    AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
391            size_t frameSize, bool clientInServer = false)
392        : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) { }
393protected:
394    virtual ~AudioTrackServerProxy() { }
395
396public:
397    // return value of these methods must be validated by the caller
398    uint32_t    getSampleRate() const { return mCblk->mSampleRate; }
399    uint16_t    getSendLevel_U4_12() const { return mCblk->mSendLevel; }
400    uint32_t    getVolumeLR() const { return mCblk->mVolumeLR; }
401
402    // estimated total number of filled frames available to server to read,
403    // which may include non-contiguous frames
404    virtual size_t      framesReady();
405
406    // Currently AudioFlinger will call framesReady() for a fast track from two threads:
407    // FastMixer thread, and normal mixer thread.  This is dangerous, as the proxy is intended
408    // to be called from at most one thread of server, and one thread of client.
409    // As a temporary workaround, this method informs the proxy implementation that it
410    // should avoid doing a state queue poll from within framesReady().
411    // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread.
412    virtual void        framesReadyIsCalledByMultipleThreads() { }
413
414    bool     setStreamEndDone();    // and return previous value
415};
416
417class StaticAudioTrackServerProxy : public AudioTrackServerProxy {
418public:
419    StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
420            size_t frameSize);
421protected:
422    virtual ~StaticAudioTrackServerProxy() { }
423
424public:
425    virtual size_t      framesReady();
426    virtual void        framesReadyIsCalledByMultipleThreads();
427    virtual status_t    obtainBuffer(Buffer* buffer);
428    virtual void        releaseBuffer(Buffer* buffer);
429
430private:
431    ssize_t             pollPosition(); // poll for state queue update, and return current position
432    StaticAudioTrackSingleStateQueue::Observer  mObserver;
433    size_t              mPosition;  // server's current play position in frames, relative to 0
434    size_t              mEnd;       // cached value computed from mState, safe for asynchronous read
435    bool                mFramesReadyIsCalledByMultipleThreads;
436    StaticAudioTrackState   mState;
437};
438
439// Proxy used by AudioFlinger for servicing AudioRecord
440class AudioRecordServerProxy : public ServerProxy {
441public:
442    AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
443            size_t frameSize)
444        : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/,
445            false /*clientInServer*/) { }
446protected:
447    virtual ~AudioRecordServerProxy() { }
448};
449
450// ----------------------------------------------------------------------------
451
452}; // namespace android
453
454#endif // ANDROID_AUDIO_TRACK_SHARED_H
455