blob: aaa5333d45b1fccb5c33baeaaae24de48ee1a792 [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; }
110 virtual bool isOut() const;
111
112protected:
113
Eric Laurent81784c32012-11-19 14:55:58 -0800114 // 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 uint32_t mUnderrunCount; // Counter of total number of underruns, never reset
145 volatile float mCachedVolume; // combined master volume and stream type volume;
146 // 'volatile' means accessed without lock or
147 // barrier, but is read/written atomically
Glenn Kasten5736c352012-12-04 12:12:34 -0800148 bool mIsInvalid; // non-resettable latch, set by invalidate()
Eric Laurent81784c32012-11-19 14:55:58 -0800149}; // end of Track
150
151class TimedTrack : public Track {
152 public:
153 static sp<TimedTrack> create(PlaybackThread *thread,
154 const sp<Client>& client,
155 audio_stream_type_t streamType,
156 uint32_t sampleRate,
157 audio_format_t format,
158 audio_channel_mask_t channelMask,
159 size_t frameCount,
160 const sp<IMemory>& sharedBuffer,
161 int sessionId);
162 virtual ~TimedTrack();
163
164 class TimedBuffer {
165 public:
166 TimedBuffer();
167 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
168 const sp<IMemory>& buffer() const { return mBuffer; }
169 int64_t pts() const { return mPTS; }
170 uint32_t position() const { return mPosition; }
171 void setPosition(uint32_t pos) { mPosition = pos; }
172 private:
173 sp<IMemory> mBuffer;
174 int64_t mPTS;
175 uint32_t mPosition;
176 };
177
178 // Mixer facing methods.
179 virtual bool isTimedTrack() const { return true; }
180 virtual size_t framesReady() const;
181
182 // AudioBufferProvider interface
183 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
184 int64_t pts);
185 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
186
187 // Client/App facing methods.
188 status_t allocateTimedBuffer(size_t size,
189 sp<IMemory>* buffer);
190 status_t queueTimedBuffer(const sp<IMemory>& buffer,
191 int64_t pts);
192 status_t setMediaTimeTransform(const LinearTransform& xform,
193 TimedAudioTrack::TargetTimeline target);
194
195 private:
196 TimedTrack(PlaybackThread *thread,
197 const sp<Client>& client,
198 audio_stream_type_t streamType,
199 uint32_t sampleRate,
200 audio_format_t format,
201 audio_channel_mask_t channelMask,
202 size_t frameCount,
203 const sp<IMemory>& sharedBuffer,
204 int sessionId);
205
206 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
207 void timedYieldSilence_l(uint32_t numFrames,
208 AudioBufferProvider::Buffer* buffer);
209 void trimTimedBufferQueue_l();
210 void trimTimedBufferQueueHead_l(const char* logTag);
211 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
212 const char* logTag);
213
214 uint64_t mLocalTimeFreq;
215 LinearTransform mLocalTimeToSampleTransform;
216 LinearTransform mMediaTimeToSampleTransform;
217 sp<MemoryDealer> mTimedMemoryDealer;
218
219 Vector<TimedBuffer> mTimedBufferQueue;
220 bool mQueueHeadInFlight;
221 bool mTrimQueueHeadOnRelease;
222 uint32_t mFramesPendingInQueue;
223
224 uint8_t* mTimedSilenceBuffer;
225 uint32_t mTimedSilenceBufferSize;
226 mutable Mutex mTimedBufferQueueLock;
227 bool mTimedAudioOutputOnTime;
228 CCHelper mCCHelper;
229
230 Mutex mMediaTimeTransformLock;
231 LinearTransform mMediaTimeTransform;
232 bool mMediaTimeTransformValid;
233 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
234};
235
236
237// playback track, used by DuplicatingThread
238class OutputTrack : public Track {
239public:
240
241 class Buffer : public AudioBufferProvider::Buffer {
242 public:
243 int16_t *mBuffer;
244 };
245
246 OutputTrack(PlaybackThread *thread,
247 DuplicatingThread *sourceThread,
248 uint32_t sampleRate,
249 audio_format_t format,
250 audio_channel_mask_t channelMask,
251 size_t frameCount);
252 virtual ~OutputTrack();
253
254 virtual status_t start(AudioSystem::sync_event_t event =
255 AudioSystem::SYNC_EVENT_NONE,
256 int triggerSession = 0);
257 virtual void stop();
258 bool write(int16_t* data, uint32_t frames);
259 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
260 bool isActive() const { return mActive; }
261 const wp<ThreadBase>& thread() const { return mThread; }
262
263private:
264
265 enum {
266 NO_MORE_BUFFERS = 0x80000001, // same in AudioTrack.h, ok to be different value
267 };
268
269 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer,
270 uint32_t waitTimeMs);
271 void clearBufferQueue();
272
273 // Maximum number of pending buffers allocated by OutputTrack::write()
274 static const uint8_t kMaxOverFlowBuffers = 10;
275
276 Vector < Buffer* > mBufferQueue;
277 AudioBufferProvider::Buffer mOutBuffer;
278 bool mActive;
279 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
280 void* mBuffers; // starting address of buffers in plain memory
281}; // end of OutputTrack