blob: 43b77f3d121cbe3440024058eeb432c2b8f81cbf [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,
Marco Nelissen9cae2172013-01-14 14:12:05 -080034 int uid,
Eric Laurent81784c32012-11-19 14:55:58 -080035 IAudioFlinger::track_flags_t flags);
36 virtual ~Track();
37
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,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800169 int sessionId,
170 int uid);
Eric Laurent81784c32012-11-19 14:55:58 -0800171 virtual ~TimedTrack();
172
173 class TimedBuffer {
174 public:
175 TimedBuffer();
176 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
177 const sp<IMemory>& buffer() const { return mBuffer; }
178 int64_t pts() const { return mPTS; }
179 uint32_t position() const { return mPosition; }
180 void setPosition(uint32_t pos) { mPosition = pos; }
181 private:
182 sp<IMemory> mBuffer;
183 int64_t mPTS;
184 uint32_t mPosition;
185 };
186
187 // Mixer facing methods.
188 virtual bool isTimedTrack() const { return true; }
189 virtual size_t framesReady() const;
190
191 // AudioBufferProvider interface
192 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
193 int64_t pts);
194 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
195
196 // Client/App facing methods.
197 status_t allocateTimedBuffer(size_t size,
198 sp<IMemory>* buffer);
199 status_t queueTimedBuffer(const sp<IMemory>& buffer,
200 int64_t pts);
201 status_t setMediaTimeTransform(const LinearTransform& xform,
202 TimedAudioTrack::TargetTimeline target);
203
204 private:
205 TimedTrack(PlaybackThread *thread,
206 const sp<Client>& client,
207 audio_stream_type_t streamType,
208 uint32_t sampleRate,
209 audio_format_t format,
210 audio_channel_mask_t channelMask,
211 size_t frameCount,
212 const sp<IMemory>& sharedBuffer,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800213 int sessionId,
214 int uid);
Eric Laurent81784c32012-11-19 14:55:58 -0800215
216 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
217 void timedYieldSilence_l(uint32_t numFrames,
218 AudioBufferProvider::Buffer* buffer);
219 void trimTimedBufferQueue_l();
220 void trimTimedBufferQueueHead_l(const char* logTag);
221 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
222 const char* logTag);
223
224 uint64_t mLocalTimeFreq;
225 LinearTransform mLocalTimeToSampleTransform;
226 LinearTransform mMediaTimeToSampleTransform;
227 sp<MemoryDealer> mTimedMemoryDealer;
228
229 Vector<TimedBuffer> mTimedBufferQueue;
230 bool mQueueHeadInFlight;
231 bool mTrimQueueHeadOnRelease;
232 uint32_t mFramesPendingInQueue;
233
234 uint8_t* mTimedSilenceBuffer;
235 uint32_t mTimedSilenceBufferSize;
236 mutable Mutex mTimedBufferQueueLock;
237 bool mTimedAudioOutputOnTime;
238 CCHelper mCCHelper;
239
240 Mutex mMediaTimeTransformLock;
241 LinearTransform mMediaTimeTransform;
242 bool mMediaTimeTransformValid;
243 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
244};
245
246
247// playback track, used by DuplicatingThread
248class OutputTrack : public Track {
249public:
250
251 class Buffer : public AudioBufferProvider::Buffer {
252 public:
253 int16_t *mBuffer;
254 };
255
256 OutputTrack(PlaybackThread *thread,
257 DuplicatingThread *sourceThread,
258 uint32_t sampleRate,
259 audio_format_t format,
260 audio_channel_mask_t channelMask,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800261 size_t frameCount,
262 int uid);
Eric Laurent81784c32012-11-19 14:55:58 -0800263 virtual ~OutputTrack();
264
265 virtual status_t start(AudioSystem::sync_event_t event =
266 AudioSystem::SYNC_EVENT_NONE,
267 int triggerSession = 0);
268 virtual void stop();
269 bool write(int16_t* data, uint32_t frames);
270 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
271 bool isActive() const { return mActive; }
272 const wp<ThreadBase>& thread() const { return mThread; }
273
274private:
275
Eric Laurent81784c32012-11-19 14:55:58 -0800276 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer,
277 uint32_t waitTimeMs);
278 void clearBufferQueue();
279
280 // Maximum number of pending buffers allocated by OutputTrack::write()
281 static const uint8_t kMaxOverFlowBuffers = 10;
282
283 Vector < Buffer* > mBufferQueue;
284 AudioBufferProvider::Buffer mOutBuffer;
285 bool mActive;
286 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
Glenn Kastene3aa6592012-12-04 12:22:46 -0800287 AudioTrackClientProxy* mClientProxy;
Eric Laurent81784c32012-11-19 14:55:58 -0800288}; // end of OutputTrack