blob: f7ad6b1690c53420d28ad05dd5a6b6d4c5cce691 [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;
Glenn Kasten0c72b242013-09-11 09:14:16 -0700120
121 // see comment at AudioFlinger::PlaybackThread::Track::~Track for why this can't be const
122 sp<IMemory> mSharedBuffer;
123
Eric Laurent81784c32012-11-19 14:55:58 -0800124 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
Eric Laurent81784c32012-11-19 14:55:58 -0800149 volatile float mCachedVolume; // combined master volume and stream type volume;
150 // 'volatile' means accessed without lock or
151 // barrier, but is read/written atomically
Glenn Kasten5736c352012-12-04 12:12:34 -0800152 bool mIsInvalid; // non-resettable latch, set by invalidate()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800153 AudioTrackServerProxy* mAudioTrackServerProxy;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800154 bool mResumeToStopping; // track was paused in stopping state.
Eric Laurent81784c32012-11-19 14:55:58 -0800155}; // end of Track
156
157class TimedTrack : public Track {
158 public:
159 static sp<TimedTrack> create(PlaybackThread *thread,
160 const sp<Client>& client,
161 audio_stream_type_t streamType,
162 uint32_t sampleRate,
163 audio_format_t format,
164 audio_channel_mask_t channelMask,
165 size_t frameCount,
166 const sp<IMemory>& sharedBuffer,
167 int sessionId);
168 virtual ~TimedTrack();
169
170 class TimedBuffer {
171 public:
172 TimedBuffer();
173 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
174 const sp<IMemory>& buffer() const { return mBuffer; }
175 int64_t pts() const { return mPTS; }
176 uint32_t position() const { return mPosition; }
177 void setPosition(uint32_t pos) { mPosition = pos; }
178 private:
179 sp<IMemory> mBuffer;
180 int64_t mPTS;
181 uint32_t mPosition;
182 };
183
184 // Mixer facing methods.
185 virtual bool isTimedTrack() const { return true; }
186 virtual size_t framesReady() const;
187
188 // AudioBufferProvider interface
189 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
190 int64_t pts);
191 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
192
193 // Client/App facing methods.
194 status_t allocateTimedBuffer(size_t size,
195 sp<IMemory>* buffer);
196 status_t queueTimedBuffer(const sp<IMemory>& buffer,
197 int64_t pts);
198 status_t setMediaTimeTransform(const LinearTransform& xform,
199 TimedAudioTrack::TargetTimeline target);
200
201 private:
202 TimedTrack(PlaybackThread *thread,
203 const sp<Client>& client,
204 audio_stream_type_t streamType,
205 uint32_t sampleRate,
206 audio_format_t format,
207 audio_channel_mask_t channelMask,
208 size_t frameCount,
209 const sp<IMemory>& sharedBuffer,
210 int sessionId);
211
212 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
213 void timedYieldSilence_l(uint32_t numFrames,
214 AudioBufferProvider::Buffer* buffer);
215 void trimTimedBufferQueue_l();
216 void trimTimedBufferQueueHead_l(const char* logTag);
217 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
218 const char* logTag);
219
220 uint64_t mLocalTimeFreq;
221 LinearTransform mLocalTimeToSampleTransform;
222 LinearTransform mMediaTimeToSampleTransform;
223 sp<MemoryDealer> mTimedMemoryDealer;
224
225 Vector<TimedBuffer> mTimedBufferQueue;
226 bool mQueueHeadInFlight;
227 bool mTrimQueueHeadOnRelease;
228 uint32_t mFramesPendingInQueue;
229
230 uint8_t* mTimedSilenceBuffer;
231 uint32_t mTimedSilenceBufferSize;
232 mutable Mutex mTimedBufferQueueLock;
233 bool mTimedAudioOutputOnTime;
234 CCHelper mCCHelper;
235
236 Mutex mMediaTimeTransformLock;
237 LinearTransform mMediaTimeTransform;
238 bool mMediaTimeTransformValid;
239 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
240};
241
242
243// playback track, used by DuplicatingThread
244class OutputTrack : public Track {
245public:
246
247 class Buffer : public AudioBufferProvider::Buffer {
248 public:
249 int16_t *mBuffer;
250 };
251
252 OutputTrack(PlaybackThread *thread,
253 DuplicatingThread *sourceThread,
254 uint32_t sampleRate,
255 audio_format_t format,
256 audio_channel_mask_t channelMask,
257 size_t frameCount);
258 virtual ~OutputTrack();
259
260 virtual status_t start(AudioSystem::sync_event_t event =
261 AudioSystem::SYNC_EVENT_NONE,
262 int triggerSession = 0);
263 virtual void stop();
264 bool write(int16_t* data, uint32_t frames);
265 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
266 bool isActive() const { return mActive; }
267 const wp<ThreadBase>& thread() const { return mThread; }
268
269private:
270
Eric Laurent81784c32012-11-19 14:55:58 -0800271 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer,
272 uint32_t waitTimeMs);
273 void clearBufferQueue();
274
275 // Maximum number of pending buffers allocated by OutputTrack::write()
276 static const uint8_t kMaxOverFlowBuffers = 10;
277
278 Vector < Buffer* > mBufferQueue;
279 AudioBufferProvider::Buffer mOutBuffer;
280 bool mActive;
281 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
Glenn Kastene3aa6592012-12-04 12:22:46 -0800282 AudioTrackClientProxy* mClientProxy;
Eric Laurent81784c32012-11-19 14:55:58 -0800283}; // end of OutputTrack