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