TrackBase.h revision 83b8808faad1e91690c64d7007348be8d9ebde73
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, 376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten PAUSED, 386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten STARTING_1, // for RecordTrack only 396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten STARTING_2, // for RecordTrack only 4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 426181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten // where to allocate the data buffer 436181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten enum alloc_type { 446181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten ALLOC_CBLK, // allocate immediately after control block 456181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten ALLOC_READONLY, // allocate from a separate read-only heap per thread 466181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten ALLOC_PIPE, // do not allocate; use the pipe buffer 4783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ALLOC_LOCAL, // allocate a local buffer 4883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent ALLOC_NONE, // do not allocate:use the buffer passed to TrackBase constructor 4983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent }; 5083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 5183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent enum track_type { 5283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent TYPE_DEFAULT, 5383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent TYPE_TIMED, 5483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent TYPE_OUTPUT, 5583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent TYPE_PATCH, 566181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten }; 576181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten 5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TrackBase(ThreadBase *thread, 5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<Client>& client, 6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount, 6483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent void *buffer, 65e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten int sessionId, 66462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid, 67755b0a611f539dfa49e88aac592a938427c7e1b8Glenn Kasten IAudioFlinger::track_flags_t flags, 68d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten bool isOut, 6983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent alloc_type alloc = ALLOC_CBLK, 7083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent track_type type = TYPE_DEFAULT); 7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~TrackBase(); 7283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual status_t initCheck() const; 7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t start(AudioSystem::sync_event_t event, 7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession) = 0; 7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void stop() = 0; 7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IMemory> getCblk() const { return mCblkMemory; } 7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* cblk() const { return mCblk; } 7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId() const { return mSessionId; } 80462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int uid() const { return mUid; } 8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t setSyncEvent(const sp<SyncEvent>& event); 8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 83d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten sp<IMemory> getBuffers() const { return mBufferMemory; } 8483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent void* buffer() const { return mBuffer; } 85755b0a611f539dfa49e88aac592a938427c7e1b8Glenn Kasten bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; } 8683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent bool isTimedTrack() const { return (mType == TYPE_TIMED); } 8783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent bool isOutputTrack() const { return (mType == TYPE_OUTPUT); } 8883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent bool isPatchTrack() const { return (mType == TYPE_PATCH); } 8983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent bool isExternalTrack() const { return !isOutputTrack() && !isPatchTrack(); } 90d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten 9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TrackBase(const TrackBase&); 9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TrackBase& operator = (const TrackBase&); 9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // AudioBufferProvider interface 9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) = 0; 9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); 9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ExtendedAudioBufferProvider interface is only needed for Track, 10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but putting it in TrackBase avoids the complexity of virtual inheritance 10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual size_t framesReady() const { return SIZE_MAX; } 10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 103c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten audio_format_t format() const { return mFormat; } 10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t channelCount() const { return mChannelCount; } 10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask() const { return mChannelMask; } 10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten virtual uint32_t sampleRate() const { return mSampleRate; } 11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isStopped() const { 11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return (mState == STOPPED || mState == FLUSHED); 11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 115bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // for fast tracks and offloaded tracks only 11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isStopping() const { 11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mState == STOPPING_1 || mState == STOPPING_2; 11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isStopping_1() const { 12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mState == STOPPING_1; 12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isStopping_2() const { 12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mState == STOPPING_2; 12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isTerminated() const { 127bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return mTerminated; 128bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 129bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 130bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void terminate() { 131bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent mTerminated = true; 13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten bool isOut() const { return mIsOut; } 135e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten // true for Track and TimedTrack, false for RecordTrack, 13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // this could be a track type if needed later 13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const wp<ThreadBase> mThread; 13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent /*const*/ sp<Client> mClient; // see explanation at ~TrackBase() why not const 14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IMemory> mCblkMemory; 14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_track_cblk_t* mCblk; 142d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten sp<IMemory> mBufferMemory; // currently non-0 for fast RecordTrack only 14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void* mBuffer; // start of track buffer, typically in shared memory 144e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten // except for OutputTrack when it is in local memory 14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // we don't really need a lock for these 14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent track_state mState; 14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const uint32_t mSampleRate; // initial sample rate only; for tracks which 14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // support dynamic rates, the current value is in control block 14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const audio_format_t mFormat; 15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const audio_channel_mask_t mChannelMask; 151f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten const uint32_t mChannelCount; 15281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t mFrameSize; // AudioFlinger's view of frame size in shared memory, 15381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // where for AudioTrack (but not AudioRecord), 15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 8-bit PCM samples are stored as 16-bit 15581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const size_t mFrameCount;// size of track buffer given at createTrack() or 15681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // openRecord(), and then adjusted as needed 15781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const int mSessionId; 159462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int mUid; 16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector < sp<SyncEvent> >mSyncEvents; 161755b0a611f539dfa49e88aac592a938427c7e1b8Glenn Kasten const IAudioFlinger::track_flags_t mFlags; 162e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten const bool mIsOut; 163e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten ServerProxy* mServerProxy; 164da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten const int mId; 165da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten sp<NBAIO_Sink> mTeeSink; 166da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten sp<NBAIO_Source> mTeeSource; 167bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool mTerminated; 16883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent track_type mType; // must be one of TYPE_DEFAULT, TYPE_OUTPUT, TYPE_PATCH ... 16983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}; 17083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 17183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent// PatchProxyBufferProvider interface is implemented by PatchTrack and PatchRecord. 17283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent// it provides buffer access methods that map those of a ClientProxy (see AudioTrackShared.h) 17383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentclass PatchProxyBufferProvider 17483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{ 17583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentpublic: 17683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 17783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual ~PatchProxyBufferProvider() {} 17883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 17983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual status_t obtainBuffer(Proxy::Buffer* buffer, 18083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent const struct timespec *requested = NULL) = 0; 18183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual void releaseBuffer(Proxy::Buffer* buffer) = 0; 18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 183