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