PlaybackTracks.h revision 573d80a8f463f648a515fc0975bf83951b272993
1/* 2** 3** Copyright 2012, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18#ifndef INCLUDING_FROM_AUDIOFLINGER_H 19 #error This header file should only be included from AudioFlinger.h 20#endif 21 22// playback track 23class Track : public TrackBase, public VolumeProvider { 24public: 25 Track( PlaybackThread *thread, 26 const sp<Client>& client, 27 audio_stream_type_t streamType, 28 uint32_t sampleRate, 29 audio_format_t format, 30 audio_channel_mask_t channelMask, 31 size_t frameCount, 32 const sp<IMemory>& sharedBuffer, 33 int sessionId, 34 IAudioFlinger::track_flags_t flags); 35 virtual ~Track(); 36 37 static void appendDumpHeader(String8& result); 38 void dump(char* buffer, size_t size); 39 virtual status_t start(AudioSystem::sync_event_t event = 40 AudioSystem::SYNC_EVENT_NONE, 41 int triggerSession = 0); 42 virtual void stop(); 43 void pause(); 44 45 void flush(); 46 void destroy(); 47 int name() const { return mName; } 48 49 virtual uint32_t sampleRate() const; 50 51 audio_stream_type_t streamType() const { 52 return mStreamType; 53 } 54 bool isOffloaded() const { return (mFlags & IAudioFlinger::TRACK_OFFLOAD) != 0; } 55 status_t setParameters(const String8& keyValuePairs); 56 status_t attachAuxEffect(int EffectId); 57 void setAuxBuffer(int EffectId, int32_t *buffer); 58 int32_t *auxBuffer() const { return mAuxBuffer; } 59 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; } 60 int16_t *mainBuffer() const { return mMainBuffer; } 61 int auxEffectId() const { return mAuxEffectId; } 62 virtual status_t getTimestamp(AudioTimestamp& timestamp); 63 64// implement FastMixerState::VolumeProvider interface 65 virtual uint32_t getVolumeLR(); 66 67 virtual status_t setSyncEvent(const sp<SyncEvent>& event); 68 69protected: 70 // for numerous 71 friend class PlaybackThread; 72 friend class MixerThread; 73 friend class DirectOutputThread; 74 friend class OffloadThread; 75 76 Track(const Track&); 77 Track& operator = (const Track&); 78 79 // AudioBufferProvider interface 80 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, 81 int64_t pts = kInvalidPTS); 82 // releaseBuffer() not overridden 83 84 virtual size_t framesReady() const; 85 86 bool isPausing() const { return mState == PAUSING; } 87 bool isPaused() const { return mState == PAUSED; } 88 bool isResuming() const { return mState == RESUMING; } 89 bool isReady() const; 90 void setPaused() { mState = PAUSED; } 91 void reset(); 92 93 bool isOutputTrack() const { 94 return (mStreamType == AUDIO_STREAM_CNT); 95 } 96 97 sp<IMemory> sharedBuffer() const { return mSharedBuffer; } 98 99 // framesWritten is cumulative, never reset, and is shared all tracks 100 // audioHalFrames is derived from output latency 101 // FIXME parameters not needed, could get them from the thread 102 bool presentationComplete(size_t framesWritten, size_t audioHalFrames); 103 104public: 105 void triggerEvents(AudioSystem::sync_event_t type); 106 void invalidate(); 107 bool isInvalid() const { return mIsInvalid; } 108 virtual bool isTimedTrack() const { return false; } 109 bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; } 110 int fastIndex() const { return mFastIndex; } 111 112protected: 113 114 // FILLED state is used for suppressing volume ramp at begin of playing 115 enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE}; 116 mutable uint8_t mFillingUpStatus; 117 int8_t mRetryCount; 118 const sp<IMemory> mSharedBuffer; 119 bool mResetDone; 120 const audio_stream_type_t mStreamType; 121 int mName; // track name on the normal mixer, 122 // allocated statically at track creation time, 123 // and is even allocated (though unused) for fast tracks 124 // FIXME don't allocate track name for fast tracks 125 int16_t *mMainBuffer; 126 int32_t *mAuxBuffer; 127 int mAuxEffectId; 128 bool mHasVolumeController; 129 size_t mPresentationCompleteFrames; // number of frames written to the 130 // audio HAL when this track will be fully rendered 131 // zero means not monitoring 132private: 133 IAudioFlinger::track_flags_t mFlags; 134 135 // The following fields are only for fast tracks, and should be in a subclass 136 int mFastIndex; // index within FastMixerState::mFastTracks[]; 137 // either mFastIndex == -1 if not isFastTrack() 138 // or 0 < mFastIndex < FastMixerState::kMaxFast because 139 // index 0 is reserved for normal mixer's submix; 140 // index is allocated statically at track creation time 141 // but the slot is only used if track is active 142 FastTrackUnderruns mObservedUnderruns; // Most recently observed value of 143 // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns 144 volatile float mCachedVolume; // combined master volume and stream type volume; 145 // 'volatile' means accessed without lock or 146 // barrier, but is read/written atomically 147 bool mIsInvalid; // non-resettable latch, set by invalidate() 148 AudioTrackServerProxy* mAudioTrackServerProxy; 149 bool mResumeToStopping; // track was paused in stopping state. 150}; // end of Track 151 152class TimedTrack : public Track { 153 public: 154 static sp<TimedTrack> create(PlaybackThread *thread, 155 const sp<Client>& client, 156 audio_stream_type_t streamType, 157 uint32_t sampleRate, 158 audio_format_t format, 159 audio_channel_mask_t channelMask, 160 size_t frameCount, 161 const sp<IMemory>& sharedBuffer, 162 int sessionId); 163 virtual ~TimedTrack(); 164 165 class TimedBuffer { 166 public: 167 TimedBuffer(); 168 TimedBuffer(const sp<IMemory>& buffer, int64_t pts); 169 const sp<IMemory>& buffer() const { return mBuffer; } 170 int64_t pts() const { return mPTS; } 171 uint32_t position() const { return mPosition; } 172 void setPosition(uint32_t pos) { mPosition = pos; } 173 private: 174 sp<IMemory> mBuffer; 175 int64_t mPTS; 176 uint32_t mPosition; 177 }; 178 179 // Mixer facing methods. 180 virtual bool isTimedTrack() const { return true; } 181 virtual size_t framesReady() const; 182 183 // AudioBufferProvider interface 184 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, 185 int64_t pts); 186 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); 187 188 // Client/App facing methods. 189 status_t allocateTimedBuffer(size_t size, 190 sp<IMemory>* buffer); 191 status_t queueTimedBuffer(const sp<IMemory>& buffer, 192 int64_t pts); 193 status_t setMediaTimeTransform(const LinearTransform& xform, 194 TimedAudioTrack::TargetTimeline target); 195 196 private: 197 TimedTrack(PlaybackThread *thread, 198 const sp<Client>& client, 199 audio_stream_type_t streamType, 200 uint32_t sampleRate, 201 audio_format_t format, 202 audio_channel_mask_t channelMask, 203 size_t frameCount, 204 const sp<IMemory>& sharedBuffer, 205 int sessionId); 206 207 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer); 208 void timedYieldSilence_l(uint32_t numFrames, 209 AudioBufferProvider::Buffer* buffer); 210 void trimTimedBufferQueue_l(); 211 void trimTimedBufferQueueHead_l(const char* logTag); 212 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf, 213 const char* logTag); 214 215 uint64_t mLocalTimeFreq; 216 LinearTransform mLocalTimeToSampleTransform; 217 LinearTransform mMediaTimeToSampleTransform; 218 sp<MemoryDealer> mTimedMemoryDealer; 219 220 Vector<TimedBuffer> mTimedBufferQueue; 221 bool mQueueHeadInFlight; 222 bool mTrimQueueHeadOnRelease; 223 uint32_t mFramesPendingInQueue; 224 225 uint8_t* mTimedSilenceBuffer; 226 uint32_t mTimedSilenceBufferSize; 227 mutable Mutex mTimedBufferQueueLock; 228 bool mTimedAudioOutputOnTime; 229 CCHelper mCCHelper; 230 231 Mutex mMediaTimeTransformLock; 232 LinearTransform mMediaTimeTransform; 233 bool mMediaTimeTransformValid; 234 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget; 235}; 236 237 238// playback track, used by DuplicatingThread 239class OutputTrack : public Track { 240public: 241 242 class Buffer : public AudioBufferProvider::Buffer { 243 public: 244 int16_t *mBuffer; 245 }; 246 247 OutputTrack(PlaybackThread *thread, 248 DuplicatingThread *sourceThread, 249 uint32_t sampleRate, 250 audio_format_t format, 251 audio_channel_mask_t channelMask, 252 size_t frameCount); 253 virtual ~OutputTrack(); 254 255 virtual status_t start(AudioSystem::sync_event_t event = 256 AudioSystem::SYNC_EVENT_NONE, 257 int triggerSession = 0); 258 virtual void stop(); 259 bool write(int16_t* data, uint32_t frames); 260 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; } 261 bool isActive() const { return mActive; } 262 const wp<ThreadBase>& thread() const { return mThread; } 263 264private: 265 266 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, 267 uint32_t waitTimeMs); 268 void clearBufferQueue(); 269 270 // Maximum number of pending buffers allocated by OutputTrack::write() 271 static const uint8_t kMaxOverFlowBuffers = 10; 272 273 Vector < Buffer* > mBufferQueue; 274 AudioBufferProvider::Buffer mOutBuffer; 275 bool mActive; 276 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write() 277 AudioTrackClientProxy* mClientProxy; 278}; // end of OutputTrack 279