blob: 0308b99be97cf79bef465eb4156a2c03b5b8bace [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 Laurent81784c32012-11-19 14:55:58 -080063
64// implement FastMixerState::VolumeProvider interface
65 virtual uint32_t getVolumeLR();
66
67 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
68
69protected:
70 // for numerous
71 friend class PlaybackThread;
72 friend class MixerThread;
73 friend class DirectOutputThread;
Eric Laurentbfb1b832013-01-07 09:53:42 -080074 friend class OffloadThread;
Eric Laurent81784c32012-11-19 14:55:58 -080075
76 Track(const Track&);
77 Track& operator = (const Track&);
78
79 // AudioBufferProvider interface
80 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
81 int64_t pts = kInvalidPTS);
82 // releaseBuffer() not overridden
83
Glenn Kasten6466c9e2013-08-23 10:54:07 -070084 // ExtendedAudioBufferProvider interface
Eric Laurent81784c32012-11-19 14:55:58 -080085 virtual size_t framesReady() const;
Glenn Kasten6466c9e2013-08-23 10:54:07 -070086 virtual size_t framesReleased() const;
Eric Laurent81784c32012-11-19 14:55:58 -080087
Glenn Kastenc9b2e202013-02-26 11:32:32 -080088 bool isPausing() const { return mState == PAUSING; }
89 bool isPaused() const { return mState == PAUSED; }
90 bool isResuming() const { return mState == RESUMING; }
Eric Laurent81784c32012-11-19 14:55:58 -080091 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);
Glenn Kasten5736c352012-12-04 12:12:34 -0800108 void invalidate();
109 bool isInvalid() const { return mIsInvalid; }
Eric Laurent81784c32012-11-19 14:55:58 -0800110 virtual bool isTimedTrack() const { return false; }
111 bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
Glenn Kastend054c322013-07-12 12:59:20 -0700112 int fastIndex() const { return mFastIndex; }
Eric Laurent81784c32012-11-19 14:55:58 -0800113
114protected:
115
Eric Laurent81784c32012-11-19 14:55:58 -0800116 // FILLED state is used for suppressing volume ramp at begin of playing
117 enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE};
118 mutable uint8_t mFillingUpStatus;
119 int8_t mRetryCount;
120 const sp<IMemory> mSharedBuffer;
121 bool mResetDone;
122 const audio_stream_type_t mStreamType;
123 int mName; // track name on the normal mixer,
124 // allocated statically at track creation time,
125 // and is even allocated (though unused) for fast tracks
126 // FIXME don't allocate track name for fast tracks
127 int16_t *mMainBuffer;
128 int32_t *mAuxBuffer;
129 int mAuxEffectId;
130 bool mHasVolumeController;
131 size_t mPresentationCompleteFrames; // number of frames written to the
132 // audio HAL when this track will be fully rendered
133 // zero means not monitoring
134private:
135 IAudioFlinger::track_flags_t mFlags;
136
137 // The following fields are only for fast tracks, and should be in a subclass
138 int mFastIndex; // index within FastMixerState::mFastTracks[];
139 // either mFastIndex == -1 if not isFastTrack()
140 // or 0 < mFastIndex < FastMixerState::kMaxFast because
141 // index 0 is reserved for normal mixer's submix;
142 // index is allocated statically at track creation time
143 // but the slot is only used if track is active
144 FastTrackUnderruns mObservedUnderruns; // Most recently observed value of
145 // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns
Eric Laurent81784c32012-11-19 14:55:58 -0800146 volatile float mCachedVolume; // combined master volume and stream type volume;
147 // 'volatile' means accessed without lock or
148 // barrier, but is read/written atomically
Glenn Kasten5736c352012-12-04 12:12:34 -0800149 bool mIsInvalid; // non-resettable latch, set by invalidate()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800150 AudioTrackServerProxy* mAudioTrackServerProxy;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800151 bool mResumeToStopping; // track was paused in stopping state.
Eric Laurent81784c32012-11-19 14:55:58 -0800152}; // end of Track
153
154class TimedTrack : public Track {
155 public:
156 static sp<TimedTrack> create(PlaybackThread *thread,
157 const sp<Client>& client,
158 audio_stream_type_t streamType,
159 uint32_t sampleRate,
160 audio_format_t format,
161 audio_channel_mask_t channelMask,
162 size_t frameCount,
163 const sp<IMemory>& sharedBuffer,
164 int sessionId);
165 virtual ~TimedTrack();
166
167 class TimedBuffer {
168 public:
169 TimedBuffer();
170 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
171 const sp<IMemory>& buffer() const { return mBuffer; }
172 int64_t pts() const { return mPTS; }
173 uint32_t position() const { return mPosition; }
174 void setPosition(uint32_t pos) { mPosition = pos; }
175 private:
176 sp<IMemory> mBuffer;
177 int64_t mPTS;
178 uint32_t mPosition;
179 };
180
181 // Mixer facing methods.
182 virtual bool isTimedTrack() const { return true; }
183 virtual size_t framesReady() const;
184
185 // AudioBufferProvider interface
186 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
187 int64_t pts);
188 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
189
190 // Client/App facing methods.
191 status_t allocateTimedBuffer(size_t size,
192 sp<IMemory>* buffer);
193 status_t queueTimedBuffer(const sp<IMemory>& buffer,
194 int64_t pts);
195 status_t setMediaTimeTransform(const LinearTransform& xform,
196 TimedAudioTrack::TargetTimeline target);
197
198 private:
199 TimedTrack(PlaybackThread *thread,
200 const sp<Client>& client,
201 audio_stream_type_t streamType,
202 uint32_t sampleRate,
203 audio_format_t format,
204 audio_channel_mask_t channelMask,
205 size_t frameCount,
206 const sp<IMemory>& sharedBuffer,
207 int sessionId);
208
209 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
210 void timedYieldSilence_l(uint32_t numFrames,
211 AudioBufferProvider::Buffer* buffer);
212 void trimTimedBufferQueue_l();
213 void trimTimedBufferQueueHead_l(const char* logTag);
214 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
215 const char* logTag);
216
217 uint64_t mLocalTimeFreq;
218 LinearTransform mLocalTimeToSampleTransform;
219 LinearTransform mMediaTimeToSampleTransform;
220 sp<MemoryDealer> mTimedMemoryDealer;
221
222 Vector<TimedBuffer> mTimedBufferQueue;
223 bool mQueueHeadInFlight;
224 bool mTrimQueueHeadOnRelease;
225 uint32_t mFramesPendingInQueue;
226
227 uint8_t* mTimedSilenceBuffer;
228 uint32_t mTimedSilenceBufferSize;
229 mutable Mutex mTimedBufferQueueLock;
230 bool mTimedAudioOutputOnTime;
231 CCHelper mCCHelper;
232
233 Mutex mMediaTimeTransformLock;
234 LinearTransform mMediaTimeTransform;
235 bool mMediaTimeTransformValid;
236 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
237};
238
239
240// playback track, used by DuplicatingThread
241class OutputTrack : public Track {
242public:
243
244 class Buffer : public AudioBufferProvider::Buffer {
245 public:
246 int16_t *mBuffer;
247 };
248
249 OutputTrack(PlaybackThread *thread,
250 DuplicatingThread *sourceThread,
251 uint32_t sampleRate,
252 audio_format_t format,
253 audio_channel_mask_t channelMask,
254 size_t frameCount);
255 virtual ~OutputTrack();
256
257 virtual status_t start(AudioSystem::sync_event_t event =
258 AudioSystem::SYNC_EVENT_NONE,
259 int triggerSession = 0);
260 virtual void stop();
261 bool write(int16_t* data, uint32_t frames);
262 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
263 bool isActive() const { return mActive; }
264 const wp<ThreadBase>& thread() const { return mThread; }
265
266private:
267
Eric Laurent81784c32012-11-19 14:55:58 -0800268 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