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