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