TrackBase.h revision bfb1b832079bbb9426f72f3863199a54aefd02da
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project
481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License.
781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at
881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software
1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and
1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License.
1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/
1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifndef INCLUDING_FROM_AUDIOFLINGER_H
1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    #error This header file should only be included from AudioFlinger.h
2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
2181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// base for record and playback
2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass TrackBase : public ExtendedAudioBufferProvider, public RefBase {
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum track_state {
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        IDLE,
2881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FLUSHED,
2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        STOPPED,
30bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // next 2 states are currently used for fast tracks
31bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // and offloaded tracks only
3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        STOPPING_1,     // waiting for first underrun
3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        STOPPING_2,     // waiting for presentation complete
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        RESUMING,
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ACTIVE,
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PAUSING,
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PAUSED
3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        TrackBase(ThreadBase *thread,
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<Client>& client,
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                size_t frameCount,
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
47e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                                int sessionId,
48e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                                bool isOut);
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~TrackBase();
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    start(AudioSystem::sync_event_t event,
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             int triggerSession) = 0;
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        stop() = 0;
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<IMemory> getCblk() const { return mCblkMemory; }
5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            audio_track_cblk_t* cblk() const { return mCblk; }
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int         sessionId() const { return mSessionId; }
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        TrackBase(const TrackBase&);
6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        TrackBase& operator = (const TrackBase&);
6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // AudioBufferProvider interface
6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) = 0;
6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ExtendedAudioBufferProvider interface is only needed for Track,
6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but putting it in TrackBase avoids the complexity of virtual inheritance
6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t  framesReady() const { return SIZE_MAX; }
7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten    audio_format_t format() const { return mFormat; }
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t channelCount() const { return mChannelCount; }
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_channel_mask_t channelMask() const { return mChannelMask; }
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    virtual uint32_t sampleRate() const { return mSampleRate; }
7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Return a pointer to the start of a contiguous slice of the track buffer.
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Parameter 'offset' is the requested start position, expressed in
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // monotonically increasing frame units relative to the track epoch.
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Parameter 'frames' is the requested length, also in frame units.
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Always returns non-NULL.  It is the caller's responsibility to
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // verify that this will be successful; the result of calling this
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // function with invalid 'offset' or 'frames' is undefined.
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void* getBuffer(uint32_t offset, uint32_t frames) const;
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isStopped() const {
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return (mState == STOPPED || mState == FLUSHED);
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // for fast tracks and offloaded tracks only
9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isStopping() const {
9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState == STOPPING_1 || mState == STOPPING_2;
9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isStopping_1() const {
9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState == STOPPING_1;
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isStopping_2() const {
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState == STOPPING_2;
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isTerminated() const {
104bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        return mTerminated;
105bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
106bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
107bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    void terminate() {
108bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mTerminated = true;
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool step();    // mStepCount is an implicit input
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void reset();
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    bool isOut() const { return mIsOut; }
115e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                                    // true for Track and TimedTrack, false for RecordTrack,
11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // this could be a track type if needed later
11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const wp<ThreadBase> mThread;
11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    /*const*/ sp<Client> mClient;   // see explanation at ~TrackBase() why not const
12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IMemory>         mCblkMemory;
12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_track_cblk_t* mCblk;
12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void*               mBuffer;    // start of track buffer, typically in shared memory
123e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                                    // except for OutputTrack when it is in local memory
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void*               mBufferEnd; // &mBuffer[mFrameCount * frameSize], where frameSize
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    //   is based on mChannelCount and 16-bit samples
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t            mStepCount; // saves AudioBufferProvider::Buffer::frameCount as of
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // time of releaseBuffer() for later use by step()
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // we don't really need a lock for these
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track_state         mState;
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const uint32_t      mSampleRate;    // initial sample rate only; for tracks which
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // support dynamic rates, the current value is in control block
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const audio_format_t mFormat;
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const audio_channel_mask_t mChannelMask;
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const uint8_t       mChannelCount;
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t        mFrameSize; // AudioFlinger's view of frame size in shared memory,
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // where for AudioTrack (but not AudioRecord),
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // 8-bit PCM samples are stored as 16-bit
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t        mFrameCount;// size of track buffer given at createTrack() or
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // openRecord(), and then adjusted as needed
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mStepServerFailed;
14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int           mSessionId;
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector < sp<SyncEvent> >mSyncEvents;
144e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    const bool          mIsOut;
145e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    ServerProxy*        mServerProxy;
146da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    const int           mId;
147da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    sp<NBAIO_Sink>      mTeeSink;
148da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    sp<NBAIO_Source>    mTeeSource;
149bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                mTerminated;
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
151