blob: adec93821faecf417b8ceeddb5b0cafc2a1d472d [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
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();
Eric Laurent81784c32012-11-19 14:55:58 -080047 int name() const { return mName; }
48
49 audio_stream_type_t streamType() const {
50 return mStreamType;
51 }
52 status_t attachAuxEffect(int EffectId);
53 void setAuxBuffer(int EffectId, int32_t *buffer);
54 int32_t *auxBuffer() const { return mAuxBuffer; }
55 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
56 int16_t *mainBuffer() const { return mMainBuffer; }
57 int auxEffectId() const { return mAuxEffectId; }
58
59// implement FastMixerState::VolumeProvider interface
60 virtual uint32_t getVolumeLR();
61
62 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
63
64protected:
65 // for numerous
66 friend class PlaybackThread;
67 friend class MixerThread;
68 friend class DirectOutputThread;
69
70 Track(const Track&);
71 Track& operator = (const Track&);
72
73 // AudioBufferProvider interface
74 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
75 int64_t pts = kInvalidPTS);
76 // releaseBuffer() not overridden
77
78 virtual size_t framesReady() const;
79
Eric Laurent81784c32012-11-19 14:55:58 -080080 bool isPausing() const {
81 return mState == PAUSING;
82 }
83 bool isPaused() const {
84 return mState == PAUSED;
85 }
86 bool isResuming() const {
87 return mState == RESUMING;
88 }
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);
Glenn Kasten5736c352012-12-04 12:12:34 -0800106 void invalidate();
107 bool isInvalid() const { return mIsInvalid; }
Eric Laurent81784c32012-11-19 14:55:58 -0800108 virtual bool isTimedTrack() const { return false; }
109 bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
Eric Laurent81784c32012-11-19 14:55:58 -0800110
111protected:
112
Eric Laurent81784c32012-11-19 14:55:58 -0800113 // FILLED state is used for suppressing volume ramp at begin of playing
114 enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE};
115 mutable uint8_t mFillingUpStatus;
116 int8_t mRetryCount;
117 const sp<IMemory> mSharedBuffer;
118 bool mResetDone;
119 const audio_stream_type_t mStreamType;
120 int mName; // track name on the normal mixer,
121 // allocated statically at track creation time,
122 // and is even allocated (though unused) for fast tracks
123 // FIXME don't allocate track name for fast tracks
124 int16_t *mMainBuffer;
125 int32_t *mAuxBuffer;
126 int mAuxEffectId;
127 bool mHasVolumeController;
128 size_t mPresentationCompleteFrames; // number of frames written to the
129 // audio HAL when this track will be fully rendered
130 // zero means not monitoring
131private:
132 IAudioFlinger::track_flags_t mFlags;
133
134 // The following fields are only for fast tracks, and should be in a subclass
135 int mFastIndex; // index within FastMixerState::mFastTracks[];
136 // either mFastIndex == -1 if not isFastTrack()
137 // or 0 < mFastIndex < FastMixerState::kMaxFast because
138 // index 0 is reserved for normal mixer's submix;
139 // index is allocated statically at track creation time
140 // but the slot is only used if track is active
141 FastTrackUnderruns mObservedUnderruns; // Most recently observed value of
142 // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns
143 uint32_t mUnderrunCount; // Counter of total number of underruns, never reset
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
Glenn Kasten5736c352012-12-04 12:12:34 -0800147 bool mIsInvalid; // non-resettable latch, set by invalidate()
Eric Laurent81784c32012-11-19 14:55:58 -0800148}; // end of Track
149
150class TimedTrack : public Track {
151 public:
152 static sp<TimedTrack> create(PlaybackThread *thread,
153 const sp<Client>& client,
154 audio_stream_type_t streamType,
155 uint32_t sampleRate,
156 audio_format_t format,
157 audio_channel_mask_t channelMask,
158 size_t frameCount,
159 const sp<IMemory>& sharedBuffer,
160 int sessionId);
161 virtual ~TimedTrack();
162
163 class TimedBuffer {
164 public:
165 TimedBuffer();
166 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
167 const sp<IMemory>& buffer() const { return mBuffer; }
168 int64_t pts() const { return mPTS; }
169 uint32_t position() const { return mPosition; }
170 void setPosition(uint32_t pos) { mPosition = pos; }
171 private:
172 sp<IMemory> mBuffer;
173 int64_t mPTS;
174 uint32_t mPosition;
175 };
176
177 // Mixer facing methods.
178 virtual bool isTimedTrack() const { return true; }
179 virtual size_t framesReady() const;
180
181 // AudioBufferProvider interface
182 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
183 int64_t pts);
184 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
185
186 // Client/App facing methods.
187 status_t allocateTimedBuffer(size_t size,
188 sp<IMemory>* buffer);
189 status_t queueTimedBuffer(const sp<IMemory>& buffer,
190 int64_t pts);
191 status_t setMediaTimeTransform(const LinearTransform& xform,
192 TimedAudioTrack::TargetTimeline target);
193
194 private:
195 TimedTrack(PlaybackThread *thread,
196 const sp<Client>& client,
197 audio_stream_type_t streamType,
198 uint32_t sampleRate,
199 audio_format_t format,
200 audio_channel_mask_t channelMask,
201 size_t frameCount,
202 const sp<IMemory>& sharedBuffer,
203 int sessionId);
204
205 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
206 void timedYieldSilence_l(uint32_t numFrames,
207 AudioBufferProvider::Buffer* buffer);
208 void trimTimedBufferQueue_l();
209 void trimTimedBufferQueueHead_l(const char* logTag);
210 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
211 const char* logTag);
212
213 uint64_t mLocalTimeFreq;
214 LinearTransform mLocalTimeToSampleTransform;
215 LinearTransform mMediaTimeToSampleTransform;
216 sp<MemoryDealer> mTimedMemoryDealer;
217
218 Vector<TimedBuffer> mTimedBufferQueue;
219 bool mQueueHeadInFlight;
220 bool mTrimQueueHeadOnRelease;
221 uint32_t mFramesPendingInQueue;
222
223 uint8_t* mTimedSilenceBuffer;
224 uint32_t mTimedSilenceBufferSize;
225 mutable Mutex mTimedBufferQueueLock;
226 bool mTimedAudioOutputOnTime;
227 CCHelper mCCHelper;
228
229 Mutex mMediaTimeTransformLock;
230 LinearTransform mMediaTimeTransform;
231 bool mMediaTimeTransformValid;
232 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
233};
234
235
236// playback track, used by DuplicatingThread
237class OutputTrack : public Track {
238public:
239
240 class Buffer : public AudioBufferProvider::Buffer {
241 public:
242 int16_t *mBuffer;
243 };
244
245 OutputTrack(PlaybackThread *thread,
246 DuplicatingThread *sourceThread,
247 uint32_t sampleRate,
248 audio_format_t format,
249 audio_channel_mask_t channelMask,
250 size_t frameCount);
251 virtual ~OutputTrack();
252
253 virtual status_t start(AudioSystem::sync_event_t event =
254 AudioSystem::SYNC_EVENT_NONE,
255 int triggerSession = 0);
256 virtual void stop();
257 bool write(int16_t* data, uint32_t frames);
258 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
259 bool isActive() const { return mActive; }
260 const wp<ThreadBase>& thread() const { return mThread; }
261
262private:
263
264 enum {
265 NO_MORE_BUFFERS = 0x80000001, // same in AudioTrack.h, ok to be different value
266 };
267
268 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer,
269 uint32_t waitTimeMs);
270 void clearBufferQueue();
271
272 // Maximum number of pending buffers allocated by OutputTrack::write()
273 static const uint8_t kMaxOverFlowBuffers = 10;
274
275 Vector < Buffer* > mBufferQueue;
276 AudioBufferProvider::Buffer mOutBuffer;
277 bool mActive;
278 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
Glenn Kastene3aa6592012-12-04 12:22:46 -0800279 AudioTrackClientProxy* mClientProxy;
Eric Laurent81784c32012-11-19 14:55:58 -0800280}; // end of OutputTrack