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