AudioTrack.h revision e4756fe3a387615acb63c6a05788c8db9b5786cb
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2007 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See the License for the specific language governing permissions and
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef ANDROID_AUDIOTRACK_H
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ANDROID_AUDIOTRACK_H
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdint.h>
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <sys/types.h>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <media/IAudioFlinger.h>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <media/IAudioTrack.h>
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <media/AudioSystem.h>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/RefBase.h>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/Errors.h>
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <binder/IInterface.h>
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <binder/IMemory.h>
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cutils/sched_policy.h>
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/threads.h>
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace android {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------------
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class audio_track_cblk_t;
39
40// ----------------------------------------------------------------------------
41
42class AudioTrack : virtual public RefBase
43{
44public:
45    enum channel_index {
46        MONO   = 0,
47        LEFT   = 0,
48        RIGHT  = 1
49    };
50
51    /* Events used by AudioTrack callback function (audio_track_cblk_t).
52     * Keep in sync with frameworks/base/media/java/android/media/AudioTrack.java NATIVE_EVENT_*.
53     */
54    enum event_type {
55        EVENT_MORE_DATA = 0,        // Request to write more data to PCM buffer.
56        EVENT_UNDERRUN = 1,         // PCM buffer underrun occurred.
57        EVENT_LOOP_END = 2,         // Sample loop end was reached; playback restarted from
58                                    // loop start if loop count was not 0.
59        EVENT_MARKER = 3,           // Playback head is at the specified marker position
60                                    // (See setMarkerPosition()).
61        EVENT_NEW_POS = 4,          // Playback head is at a new position
62                                    // (See setPositionUpdatePeriod()).
63        EVENT_BUFFER_END = 5        // Playback head is at the end of the buffer.
64    };
65
66    /* Client should declare Buffer on the stack and pass address to obtainBuffer()
67     * and releaseBuffer().  See also callback_t for EVENT_MORE_DATA.
68     */
69
70    class Buffer
71    {
72    public:
73        size_t      frameCount;   // number of sample frames corresponding to size;
74                                  // on input it is the number of frames desired,
75                                  // on output is the number of frames actually filled
76
77        size_t      size;         // input/output in byte units
78        union {
79            void*       raw;
80            short*      i16;    // signed 16-bit
81            int8_t*     i8;     // unsigned 8-bit, offset by 0x80
82        };
83    };
84
85
86    /* As a convenience, if a callback is supplied, a handler thread
87     * is automatically created with the appropriate priority. This thread
88     * invokes the callback when a new buffer becomes available or various conditions occur.
89     * Parameters:
90     *
91     * event:   type of event notified (see enum AudioTrack::event_type).
92     * user:    Pointer to context for use by the callback receiver.
93     * info:    Pointer to optional parameter according to event type:
94     *          - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write
95     *            more bytes than indicated by 'size' field and update 'size' if fewer bytes are
96     *            written.
97     *          - EVENT_UNDERRUN: unused.
98     *          - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining.
99     *          - EVENT_MARKER: pointer to an uint32_t containing the marker position in frames.
100     *          - EVENT_NEW_POS: pointer to an uint32_t containing the new position in frames.
101     *          - EVENT_BUFFER_END: unused.
102     */
103
104    typedef void (*callback_t)(int event, void* user, void *info);
105
106    /* Returns the minimum frame count required for the successful creation of
107     * an AudioTrack object.
108     * Returned status (from utils/Errors.h) can be:
109     *  - NO_ERROR: successful operation
110     *  - NO_INIT: audio server or audio hardware not initialized
111     */
112
113     static status_t getMinFrameCount(size_t* frameCount,
114                                      audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
115                                      uint32_t sampleRate = 0);
116
117    /* Constructs an uninitialized AudioTrack. No connection with
118     * AudioFlinger takes place.
119     */
120                        AudioTrack();
121
122    /* Creates an AudioTrack object and registers it with AudioFlinger.
123     * Once created, the track needs to be started before it can be used.
124     * Unspecified values are set to the audio hardware's current
125     * values.
126     *
127     * Parameters:
128     *
129     * streamType:         Select the type of audio stream this track is attached to
130     *                     (e.g. AUDIO_STREAM_MUSIC).
131     * sampleRate:         Track sampling rate in Hz.
132     * format:             Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
133     *                     16 bits per sample).
134     * channelMask:        Channel mask.
135     * frameCount:         Minimum size of track PCM buffer in frames. This defines the
136     *                     application's contribution to the
137     *                     latency of the track. The actual size selected by the AudioTrack could be
138     *                     larger if the requested size is not compatible with current audio HAL
139     *                     latency.  Zero means to use a default value.
140     * flags:              See comments on audio_output_flags_t in <system/audio.h>.
141     * cbf:                Callback function. If not null, this function is called periodically
142     *                     to provide new PCM data.
143     * user:               Context for use by the callback receiver.
144     * notificationFrames: The callback function is called each time notificationFrames PCM
145     *                     frames have been consumed from track input buffer.
146     * sessionId:          Specific session ID, or zero to use default.
147     * threadCanCallJava:  Whether callbacks are made from an attached thread and thus can call JNI.
148     *                     If not present in parameter list, then fixed at false.
149     */
150
151                        AudioTrack( audio_stream_type_t streamType,
152                                    uint32_t sampleRate  = 0,
153                                    audio_format_t format = AUDIO_FORMAT_DEFAULT,
154                                    audio_channel_mask_t channelMask = 0,
155                                    int frameCount       = 0,
156                                    audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
157                                    callback_t cbf       = NULL,
158                                    void* user           = NULL,
159                                    int notificationFrames = 0,
160                                    int sessionId        = 0);
161
162    /* Creates an audio track and registers it with AudioFlinger. With this constructor,
163     * the PCM data to be rendered by AudioTrack is passed in a shared memory buffer
164     * identified by the argument sharedBuffer. This prototype is for static buffer playback.
165     * PCM data must be present in memory before the AudioTrack is started.
166     * The write() and flush() methods are not supported in this case.
167     * It is recommended to pass a callback function to be notified of playback end by an
168     * EVENT_UNDERRUN event.
169     */
170
171                        AudioTrack( audio_stream_type_t streamType,
172                                    uint32_t sampleRate = 0,
173                                    audio_format_t format = AUDIO_FORMAT_DEFAULT,
174                                    audio_channel_mask_t channelMask = 0,
175                                    const sp<IMemory>& sharedBuffer = 0,
176                                    audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
177                                    callback_t cbf      = NULL,
178                                    void* user          = NULL,
179                                    int notificationFrames = 0,
180                                    int sessionId       = 0);
181
182    /* Terminates the AudioTrack and unregisters it from AudioFlinger.
183     * Also destroys all resources associated with the AudioTrack.
184     */
185                        ~AudioTrack();
186
187
188    /* Initialize an uninitialized AudioTrack.
189     * Returned status (from utils/Errors.h) can be:
190     *  - NO_ERROR: successful initialization
191     *  - INVALID_OPERATION: AudioTrack is already initialized
192     *  - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
193     *  - NO_INIT: audio server or audio hardware not initialized
194     */
195            status_t    set(audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
196                            uint32_t sampleRate = 0,
197                            audio_format_t format = AUDIO_FORMAT_DEFAULT,
198                            audio_channel_mask_t channelMask = 0,
199                            int frameCount      = 0,
200                            audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
201                            callback_t cbf      = NULL,
202                            void* user          = NULL,
203                            int notificationFrames = 0,
204                            const sp<IMemory>& sharedBuffer = 0,
205                            bool threadCanCallJava = false,
206                            int sessionId       = 0);
207
208
209    /* Result of constructing the AudioTrack. This must be checked
210     * before using any AudioTrack API (except for set()), because using
211     * an uninitialized AudioTrack produces undefined results.
212     * See set() method above for possible return codes.
213     */
214            status_t    initCheck() const   { return mStatus; }
215
216    /* Returns this track's estimated latency in milliseconds.
217     * This includes the latency due to AudioTrack buffer size, AudioMixer (if any)
218     * and audio hardware driver.
219     */
220            uint32_t     latency() const    { return mLatency; }
221
222    /* getters, see constructors and set() */
223
224            audio_stream_type_t streamType() const { return mStreamType; }
225            audio_format_t format() const   { return mFormat; }
226
227    /* Return channelCount * (bit depth per channel / 8).
228     * channelCount is determined from channelMask, and bit depth comes from format.
229     */
230            uint32_t    channelCount() const { return mChannelCount; }
231
232            uint32_t    frameCount() const  { return mFrameCount; }
233            size_t      frameSize() const   { return mFrameSize; }
234
235            sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
236
237
238    /* After it's created the track is not active. Call start() to
239     * make it active. If set, the callback will start being called.
240     */
241            void        start();
242
243    /* Stop a track. If set, the callback will cease being called and
244     * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
245     * and will fill up buffers until the pool is exhausted.
246     */
247            void        stop();
248            bool        stopped() const;
249
250    /* Flush a stopped track. All pending buffers are discarded.
251     * This function has no effect if the track is not stopped.
252     */
253            void        flush();
254
255    /* Pause a track. If set, the callback will cease being called and
256     * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
257     * and will fill up buffers until the pool is exhausted.
258     */
259            void        pause();
260
261    /* Set volume for this track, mostly used for games' sound effects
262     * left and right volumes. Levels must be >= 0.0 and <= 1.0.
263     * This is the older API.  New applications should use setVolume(float) when possible.
264     */
265            status_t    setVolume(float left, float right);
266
267    /* Set volume for all channels.  This is the preferred API for new applications,
268     * especially for multi-channel content.
269     */
270            status_t    setVolume(float volume);
271
272    /* Set the send level for this track. An auxiliary effect should be attached
273     * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0.
274     */
275            status_t    setAuxEffectSendLevel(float level);
276            void        getAuxEffectSendLevel(float* level) const;
277
278    /* Set sample rate for this track in Hz, mostly used for games' sound effects
279     */
280            status_t    setSampleRate(uint32_t sampleRate);
281
282    /* Return current sample rate in Hz, or 0 if unknown */
283            uint32_t    getSampleRate() const;
284
285    /* Enables looping and sets the start and end points of looping.
286     *
287     * Parameters:
288     *
289     * loopStart:   loop start expressed as the number of PCM frames played since AudioTrack start.
290     * loopEnd:     loop end expressed as the number of PCM frames played since AudioTrack start.
291     * loopCount:   number of loops to execute. Calling setLoop() with loopCount == 0 cancels any
292     *              pending or active loop. loopCount = -1 means infinite looping.
293     *
294     * For proper operation the following condition must be respected:
295     *          (loopEnd-loopStart) <= framecount()
296     */
297            status_t    setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount);
298
299    /* Sets marker position. When playback reaches the number of frames specified, a callback with
300     * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker
301     * notification callback.
302     * If the AudioTrack has been opened with no callback function associated, the operation will
303     * fail.
304     *
305     * Parameters:
306     *
307     * marker:   marker position expressed in frames.
308     *
309     * Returned status (from utils/Errors.h) can be:
310     *  - NO_ERROR: successful operation
311     *  - INVALID_OPERATION: the AudioTrack has no callback installed.
312     */
313            status_t    setMarkerPosition(uint32_t marker);
314            status_t    getMarkerPosition(uint32_t *marker) const;
315
316
317    /* Sets position update period. Every time the number of frames specified has been played,
318     * a callback with event type EVENT_NEW_POS is called.
319     * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
320     * callback.
321     * If the AudioTrack has been opened with no callback function associated, the operation will
322     * fail.
323     *
324     * Parameters:
325     *
326     * updatePeriod:  position update notification period expressed in frames.
327     *
328     * Returned status (from utils/Errors.h) can be:
329     *  - NO_ERROR: successful operation
330     *  - INVALID_OPERATION: the AudioTrack has no callback installed.
331     */
332            status_t    setPositionUpdatePeriod(uint32_t updatePeriod);
333            status_t    getPositionUpdatePeriod(uint32_t *updatePeriod) const;
334
335    /* Sets playback head position within AudioTrack buffer. The new position is specified
336     * in number of frames.
337     * This method must be called with the AudioTrack in paused or stopped state.
338     * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames.
339     * Therefore using this method makes sense only when playing a "static" audio buffer
340     * as opposed to streaming.
341     * The getPosition() method on the other hand returns the total number of frames played since
342     * playback start.
343     *
344     * Parameters:
345     *
346     * position:  New playback head position within AudioTrack buffer.
347     *
348     * Returned status (from utils/Errors.h) can be:
349     *  - NO_ERROR: successful operation
350     *  - INVALID_OPERATION: the AudioTrack is not stopped.
351     *  - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack
352     *               buffer
353     */
354            status_t    setPosition(uint32_t position);
355            status_t    getPosition(uint32_t *position);
356
357    /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids
358     * rewriting the buffer before restarting playback after a stop.
359     * This method must be called with the AudioTrack in paused or stopped state.
360     *
361     * Returned status (from utils/Errors.h) can be:
362     *  - NO_ERROR: successful operation
363     *  - INVALID_OPERATION: the AudioTrack is not stopped.
364     */
365            status_t    reload();
366
367    /* Returns a handle on the audio output used by this AudioTrack.
368     *
369     * Parameters:
370     *  none.
371     *
372     * Returned value:
373     *  handle on audio hardware output
374     */
375            audio_io_handle_t    getOutput();
376
377    /* Returns the unique session ID associated with this track.
378     *
379     * Parameters:
380     *  none.
381     *
382     * Returned value:
383     *  AudioTrack session ID.
384     */
385            int    getSessionId() const { return mSessionId; }
386
387    /* Attach track auxiliary output to specified effect. Use effectId = 0
388     * to detach track from effect.
389     *
390     * Parameters:
391     *
392     * effectId:  effectId obtained from AudioEffect::id().
393     *
394     * Returned status (from utils/Errors.h) can be:
395     *  - NO_ERROR: successful operation
396     *  - INVALID_OPERATION: the effect is not an auxiliary effect.
397     *  - BAD_VALUE: The specified effect ID is invalid
398     */
399            status_t    attachAuxEffect(int effectId);
400
401    /* Obtains a buffer of "frameCount" frames. The buffer must be
402     * filled entirely, and then released with releaseBuffer().
403     * If the track is stopped, obtainBuffer() returns
404     * STOPPED instead of NO_ERROR as long as there are buffers available,
405     * at which point NO_MORE_BUFFERS is returned.
406     * Buffers will be returned until the pool
407     * is exhausted, at which point obtainBuffer() will either block
408     * or return WOULD_BLOCK depending on the value of the "blocking"
409     * parameter.
410     *
411     * Interpretation of waitCount:
412     *  +n  limits wait time to n * WAIT_PERIOD_MS,
413     *  -1  causes an (almost) infinite wait time,
414     *   0  non-blocking.
415     *
416     * Buffer fields
417     * On entry:
418     *  frameCount  number of frames requested
419     * After error return:
420     *  frameCount  0
421     *  size        0
422     *  raw         undefined
423     * After successful return:
424     *  frameCount  actual number of frames available, <= number requested
425     *  size        actual number of bytes available
426     *  raw         pointer to the buffer
427     */
428
429        enum {
430            NO_MORE_BUFFERS = 0x80000001,   // same name in AudioFlinger.h, ok to be different value
431            STOPPED = 1
432        };
433
434            status_t    obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
435
436    /* Release a filled buffer of "frameCount" frames for AudioFlinger to process. */
437            void        releaseBuffer(Buffer* audioBuffer);
438
439    /* As a convenience we provide a write() interface to the audio buffer.
440     * This is implemented on top of obtainBuffer/releaseBuffer. For best
441     * performance use callbacks. Returns actual number of bytes written >= 0,
442     * or one of the following negative status codes:
443     *      INVALID_OPERATION   AudioTrack is configured for shared buffer mode
444     *      BAD_VALUE           size is invalid
445     *      STOPPED             AudioTrack was stopped during the write
446     *      NO_MORE_BUFFERS     when obtainBuffer() returns same
447     *      or any other error code returned by IAudioTrack::start() or restoreTrack_l().
448     */
449            ssize_t     write(const void* buffer, size_t size);
450
451    /*
452     * Dumps the state of an audio track.
453     */
454            status_t dump(int fd, const Vector<String16>& args) const;
455
456protected:
457    /* copying audio tracks is not allowed */
458                        AudioTrack(const AudioTrack& other);
459            AudioTrack& operator = (const AudioTrack& other);
460
461    /* a small internal class to handle the callback */
462    class AudioTrackThread : public Thread
463    {
464    public:
465        AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false);
466
467        // Do not call Thread::requestExitAndWait() without first calling requestExit().
468        // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
469        virtual void        requestExit();
470
471                void        pause();    // suspend thread from execution at next loop boundary
472                void        resume();   // allow thread to execute, if not requested to exit
473
474    private:
475        friend class AudioTrack;
476        virtual bool        threadLoop();
477        AudioTrack& mReceiver;
478        ~AudioTrackThread();
479        Mutex               mMyLock;    // Thread::mLock is private
480        Condition           mMyCond;    // Thread::mThreadExitedCondition is private
481        bool                mPaused;    // whether thread is currently paused
482    };
483
484            // body of AudioTrackThread::threadLoop()
485            bool processAudioBuffer(const sp<AudioTrackThread>& thread);
486
487            // caller must hold lock on mLock for all _l methods
488            status_t createTrack_l(audio_stream_type_t streamType,
489                                 uint32_t sampleRate,
490                                 audio_format_t format,
491                                 size_t frameCount,
492                                 audio_output_flags_t flags,
493                                 const sp<IMemory>& sharedBuffer,
494                                 audio_io_handle_t output);
495            void flush_l();
496            status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount);
497            audio_io_handle_t getOutput_l();
498            status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart);
499            bool stopped_l() const { return !mActive; }
500
501    sp<IAudioTrack>         mAudioTrack;
502    sp<IMemory>             mCblkMemory;
503    sp<AudioTrackThread>    mAudioTrackThread;
504
505    float                   mVolume[2];
506    float                   mSendLevel;
507    size_t                  mFrameCount;            // corresponds to current IAudioTrack
508    size_t                  mReqFrameCount;         // frame count to request the next time a new
509                                                    // IAudioTrack is needed
510
511    audio_track_cblk_t*     mCblk;                  // re-load after mLock.unlock()
512
513            // Starting address of buffers in shared memory.  If there is a shared buffer, mBuffers
514            // is the value of pointer() for the shared buffer, otherwise mBuffers points
515            // immediately after the control block.  This address is for the mapping within client
516            // address space.  AudioFlinger::TrackBase::mBuffer is for the server address space.
517    void*                   mBuffers;
518
519    audio_format_t          mFormat;                // as requested by client, not forced to 16-bit
520    audio_stream_type_t     mStreamType;
521    uint32_t                mChannelCount;
522    audio_channel_mask_t    mChannelMask;
523
524                // mFrameSize is equal to mFrameSizeAF for non-PCM or 16-bit PCM data.
525                // For 8-bit PCM data, mFrameSizeAF is
526                // twice as large because data is expanded to 16-bit before being stored in buffer.
527    size_t                  mFrameSize;             // app-level frame size
528    size_t                  mFrameSizeAF;           // AudioFlinger frame size
529
530    status_t                mStatus;
531    uint32_t                mLatency;
532
533    bool                    mActive;                // protected by mLock
534
535    callback_t              mCbf;                   // callback handler for events, or NULL
536    void*                   mUserData;              // for client callback handler
537
538    // for notification APIs
539    uint32_t                mNotificationFramesReq; // requested number of frames between each
540                                                    // notification callback
541    uint32_t                mNotificationFramesAct; // actual number of frames between each
542                                                    // notification callback
543    sp<IMemory>             mSharedBuffer;
544    int                     mLoopCount;
545    uint32_t                mRemainingFrames;
546    uint32_t                mMarkerPosition;        // in frames
547    bool                    mMarkerReached;
548    uint32_t                mNewPosition;           // in frames
549    uint32_t                mUpdatePeriod;          // in frames
550
551    bool                    mFlushed; // FIXME will be made obsolete by making flush() synchronous
552    audio_output_flags_t    mFlags;
553    int                     mSessionId;
554    int                     mAuxEffectId;
555
556    // When locking both mLock and mCblk->lock, must lock in this order to avoid deadlock:
557    //      1. mLock
558    //      2. mCblk->lock
559    // It is OK to lock only mCblk->lock.
560    mutable Mutex           mLock;
561
562    bool                    mIsTimed;
563    int                     mPreviousPriority;          // before start()
564    SchedPolicy             mPreviousSchedulingGroup;
565};
566
567class TimedAudioTrack : public AudioTrack
568{
569public:
570    TimedAudioTrack();
571
572    /* allocate a shared memory buffer that can be passed to queueTimedBuffer */
573    status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer);
574
575    /* queue a buffer obtained via allocateTimedBuffer for playback at the
576       given timestamp.  PTS units are microseconds on the media time timeline.
577       The media time transform (set with setMediaTimeTransform) set by the
578       audio producer will handle converting from media time to local time
579       (perhaps going through the common time timeline in the case of
580       synchronized multiroom audio case) */
581    status_t queueTimedBuffer(const sp<IMemory>& buffer, int64_t pts);
582
583    /* define a transform between media time and either common time or
584       local time */
585    enum TargetTimeline {LOCAL_TIME, COMMON_TIME};
586    status_t setMediaTimeTransform(const LinearTransform& xform,
587                                   TargetTimeline target);
588};
589
590}; // namespace android
591
592#endif // ANDROID_AUDIOTRACK_H
593