• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
23 class Track : public TrackBase, public VolumeProvider {
24 public:
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                                 int uid,
35                                 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();
name()48             int         name() const { return mName; }
49 
50     virtual uint32_t    sampleRate() const;
51 
streamType()52             audio_stream_type_t streamType() const {
53                 return mStreamType;
54             }
isOffloaded()55             bool        isOffloaded() const { return (mFlags & IAudioFlinger::TRACK_OFFLOAD) != 0; }
56             status_t    setParameters(const String8& keyValuePairs);
57             status_t    attachAuxEffect(int EffectId);
58             void        setAuxBuffer(int EffectId, int32_t *buffer);
auxBuffer()59             int32_t     *auxBuffer() const { return mAuxBuffer; }
setMainBuffer(int16_t * buffer)60             void        setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
mainBuffer()61             int16_t     *mainBuffer() const { return mMainBuffer; }
auxEffectId()62             int         auxEffectId() const { return mAuxEffectId; }
63     virtual status_t    getTimestamp(AudioTimestamp& timestamp);
64             void        signal();
65 
66 // implement FastMixerState::VolumeProvider interface
67     virtual uint32_t    getVolumeLR();
68 
69     virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
70 
71 protected:
72     // for numerous
73     friend class PlaybackThread;
74     friend class MixerThread;
75     friend class DirectOutputThread;
76     friend class OffloadThread;
77 
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 
86     // ExtendedAudioBufferProvider interface
87     virtual size_t framesReady() const;
88     virtual size_t framesReleased() const;
89 
isPausing()90     bool isPausing() const { return mState == PAUSING; }
isPaused()91     bool isPaused() const { return mState == PAUSED; }
isResuming()92     bool isResuming() const { return mState == RESUMING; }
93     bool isReady() const;
setPaused()94     void setPaused() { mState = PAUSED; }
95     void reset();
96 
isOutputTrack()97     bool isOutputTrack() const {
98         return (mStreamType == AUDIO_STREAM_CNT);
99     }
100 
sharedBuffer()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 
108 public:
109     void triggerEvents(AudioSystem::sync_event_t type);
110     void invalidate();
isInvalid()111     bool isInvalid() const { return mIsInvalid; }
isTimedTrack()112     virtual bool isTimedTrack() const { return false; }
isFastTrack()113     bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
fastIndex()114     int fastIndex() const { return mFastIndex; }
115 
116 protected:
117 
118     // 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;
122 
123     // see comment at AudioFlinger::PlaybackThread::Track::~Track for why this can't be const
124     sp<IMemory>         mSharedBuffer;
125 
126     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
139 private:
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
151     volatile float      mCachedVolume;  // combined master volume and stream type volume;
152                                         // 'volatile' means accessed without lock or
153                                         // barrier, but is read/written atomically
154     bool                mIsInvalid; // non-resettable latch, set by invalidate()
155     AudioTrackServerProxy*  mAudioTrackServerProxy;
156     bool                mResumeToStopping; // track was paused in stopping state.
157 };  // end of Track
158 
159 class 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                                  int uid);
171     virtual ~TimedTrack();
172 
173     class TimedBuffer {
174       public:
175         TimedBuffer();
176         TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
buffer()177         const sp<IMemory>& buffer() const { return mBuffer; }
pts()178         int64_t pts() const { return mPTS; }
position()179         uint32_t position() const { return mPosition; }
setPosition(uint32_t pos)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.
isTimedTrack()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,
213                int sessionId,
214                int uid);
215 
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
248 class OutputTrack : public Track {
249 public:
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,
261                                 size_t frameCount,
262                                 int uid);
263     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);
bufferQueueEmpty()270             bool        bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
isActive()271             bool        isActive() const { return mActive; }
thread()272     const wp<ThreadBase>& thread() const { return mThread; }
273 
274 private:
275 
276     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()
287     AudioTrackClientProxy*      mClientProxy;
288 };  // end of OutputTrack
289