PlaybackTracks.h revision 81784c37c61b09289654b979567a42bf73cd2b12
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// playback track
2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass Track : public TrackBase, public VolumeProvider {
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        Track(  PlaybackThread *thread,
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<Client>& client,
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
2881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
3081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
3181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                size_t frameCount,
3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                int sessionId,
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                IAudioFlinger::track_flags_t flags);
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~Track();
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static  void        appendDumpHeader(String8& result);
3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(char* buffer, size_t size);
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    start(AudioSystem::sync_event_t event =
4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    AudioSystem::SYNC_EVENT_NONE,
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             int triggerSession = 0);
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        stop();
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        pause();
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        flush();
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        destroy();
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        mute(bool);
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int         name() const { return mName; }
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            audio_stream_type_t streamType() const {
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                return mStreamType;
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    attachAuxEffect(int EffectId);
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        setAuxBuffer(int EffectId, int32_t *buffer);
5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int32_t     *auxBuffer() const { return mAuxBuffer; }
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int16_t     *mainBuffer() const { return mMainBuffer; }
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int         auxEffectId() const { return mAuxEffectId; }
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// implement FastMixerState::VolumeProvider interface
6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t    getVolumeLR();
6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // for numerous
6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class PlaybackThread;
6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class MixerThread;
6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class DirectOutputThread;
7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        Track(const Track&);
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        Track& operator = (const Track&);
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // AudioBufferProvider interface
7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   int64_t pts = kInvalidPTS);
7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // releaseBuffer() not overridden
7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t framesReady() const;
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isMuted() const { return mMute; }
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isPausing() const {
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState == PAUSING;
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isPaused() const {
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState == PAUSED;
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isResuming() const {
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState == RESUMING;
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isReady() const;
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setPaused() { mState = PAUSED; }
9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void reset();
9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isOutputTrack() const {
9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return (mStreamType == AUDIO_STREAM_CNT);
9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // framesWritten is cumulative, never reset, and is shared all tracks
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // audioHalFrames is derived from output latency
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME parameters not needed, could get them from the thread
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool presentationComplete(size_t framesWritten, size_t audioHalFrames);
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void triggerEvents(AudioSystem::sync_event_t type);
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool isTimedTrack() const { return false; }
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool isOut() const;
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // written by Track::mute() called by binder thread(s), without a mutex or barrier.
11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // read by Track::isMuted() called by playback thread, also without a mutex or barrier.
11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The lack of mutex or barrier is safe because the mute status is only used by itself.
11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mMute;
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FILLED state is used for suppressing volume ramp at begin of playing
12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE};
12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable uint8_t     mFillingUpStatus;
12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int8_t              mRetryCount;
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const sp<IMemory>   mSharedBuffer;
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mResetDone;
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const audio_stream_type_t mStreamType;
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                 mName;      // track name on the normal mixer,
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // allocated statically at track creation time,
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // and is even allocated (though unused) for fast tracks
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // FIXME don't allocate track name for fast tracks
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t             *mMainBuffer;
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t             *mAuxBuffer;
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                 mAuxEffectId;
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mHasVolumeController;
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t              mPresentationCompleteFrames; // number of frames written to the
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // audio HAL when this track will be fully rendered
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // zero means not monitoring
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    IAudioFlinger::track_flags_t mFlags;
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The following fields are only for fast tracks, and should be in a subclass
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                 mFastIndex; // index within FastMixerState::mFastTracks[];
14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // either mFastIndex == -1 if not isFastTrack()
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // or 0 < mFastIndex < FastMixerState::kMaxFast because
14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // index 0 is reserved for normal mixer's submix;
14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // index is allocated statically at track creation time
14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // but the slot is only used if track is active
14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastTrackUnderruns  mObservedUnderruns; // Most recently observed value of
14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns
14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t            mUnderrunCount; // Counter of total number of underruns, never reset
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile float      mCachedVolume;  // combined master volume and stream type volume;
15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // 'volatile' means accessed without lock or
15281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // barrier, but is read/written atomically
15381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};  // end of Track
15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass TimedTrack : public Track {
15681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent  public:
15781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static sp<TimedTrack> create(PlaybackThread *thread,
15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 const sp<Client>& client,
15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 audio_stream_type_t streamType,
16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 uint32_t sampleRate,
16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 audio_format_t format,
16281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 audio_channel_mask_t channelMask,
16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 size_t frameCount,
16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 const sp<IMemory>& sharedBuffer,
16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 int sessionId);
16681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual ~TimedTrack();
16781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class TimedBuffer {
16981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      public:
17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        TimedBuffer();
17181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
17281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IMemory>& buffer() const { return mBuffer; }
17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int64_t pts() const { return mPTS; }
17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t position() const { return mPosition; }
17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        void setPosition(uint32_t pos) { mPosition = pos; }
17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      private:
17781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<IMemory> mBuffer;
17881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int64_t     mPTS;
17981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t    mPosition;
18081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
18181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Mixer facing methods.
18381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool isTimedTrack() const { return true; }
18481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t framesReady() const;
18581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
18681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // AudioBufferProvider interface
18781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   int64_t pts);
18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Client/App facing methods.
19281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    allocateTimedBuffer(size_t size,
19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sp<IMemory>* buffer);
19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    queueTimedBuffer(const sp<IMemory>& buffer,
19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 int64_t pts);
19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    setMediaTimeTransform(const LinearTransform& xform,
19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                      TimedAudioTrack::TargetTimeline target);
19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent  private:
20081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    TimedTrack(PlaybackThread *thread,
20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               const sp<Client>& client,
20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               audio_stream_type_t streamType,
20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               uint32_t sampleRate,
20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               audio_format_t format,
20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               audio_channel_mask_t channelMask,
20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               size_t frameCount,
20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               const sp<IMemory>& sharedBuffer,
20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent               int sessionId);
20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void timedYieldSilence_l(uint32_t numFrames,
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             AudioBufferProvider::Buffer* buffer);
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void trimTimedBufferQueue_l();
21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void trimTimedBufferQueueHead_l(const char* logTag);
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        const char* logTag);
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint64_t            mLocalTimeFreq;
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    LinearTransform     mLocalTimeToSampleTransform;
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    LinearTransform     mMediaTimeToSampleTransform;
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<MemoryDealer>    mTimedMemoryDealer;
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector<TimedBuffer> mTimedBufferQueue;
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mQueueHeadInFlight;
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mTrimQueueHeadOnRelease;
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t            mFramesPendingInQueue;
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint8_t*            mTimedSilenceBuffer;
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t            mTimedSilenceBufferSize;
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable Mutex       mTimedBufferQueueLock;
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mTimedAudioOutputOnTime;
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CCHelper            mCCHelper;
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex               mMediaTimeTransformLock;
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    LinearTransform     mMediaTimeTransform;
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mMediaTimeTransformValid;
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// playback track, used by DuplicatingThread
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass OutputTrack : public Track {
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class Buffer : public AudioBufferProvider::Buffer {
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int16_t *mBuffer;
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        OutputTrack(PlaybackThread *thread,
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                DuplicatingThread *sourceThread,
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                size_t frameCount);
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~OutputTrack();
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    start(AudioSystem::sync_event_t event =
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    AudioSystem::SYNC_EVENT_NONE,
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             int triggerSession = 0);
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        stop();
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool        write(int16_t* data, uint32_t frames);
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool        bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool        isActive() const { return mActive; }
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const wp<ThreadBase>& thread() const { return mThread; }
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        NO_MORE_BUFFERS = 0x80000001,   // same in AudioTrack.h, ok to be different value
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t            obtainBuffer(AudioBufferProvider::Buffer* buffer,
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                     uint32_t waitTimeMs);
27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void                clearBufferQueue();
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Maximum number of pending buffers allocated by OutputTrack::write()
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const uint8_t kMaxOverFlowBuffers = 10;
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector < Buffer* >          mBufferQueue;
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioBufferProvider::Buffer mOutBuffer;
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                        mActive;
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void*                       mBuffers;   // starting address of buffers in plain memory
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};  // end of OutputTrack
286