AudioRecord.h revision d198b85a163330b03e7507c9e8bfeb5f4d958a6c
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIORECORD_H
18#define ANDROID_AUDIORECORD_H
19
20#include <cutils/sched_policy.h>
21#include <media/AudioSystem.h>
22#include <media/IAudioRecord.h>
23#include <utils/threads.h>
24
25namespace android {
26
27// ----------------------------------------------------------------------------
28
29struct audio_track_cblk_t;
30class AudioRecordClientProxy;
31
32// ----------------------------------------------------------------------------
33
34class AudioRecord : public RefBase
35{
36public:
37
38    /* Events used by AudioRecord callback function (callback_t).
39     * Keep in sync with frameworks/base/media/java/android/media/AudioRecord.java NATIVE_EVENT_*.
40     */
41    enum event_type {
42        EVENT_MORE_DATA = 0,        // Request to read available data from buffer.
43                                    // If this event is delivered but the callback handler
44                                    // does not want to read the available data, the handler must
45                                    // explicitly
46                                    // ignore the event by setting frameCount to zero.
47        EVENT_OVERRUN = 1,          // Buffer overrun occurred.
48        EVENT_MARKER = 2,           // Record head is at the specified marker position
49                                    // (See setMarkerPosition()).
50        EVENT_NEW_POS = 3,          // Record head is at a new position
51                                    // (See setPositionUpdatePeriod()).
52        EVENT_NEW_IAUDIORECORD = 4, // IAudioRecord was re-created, either due to re-routing and
53                                    // voluntary invalidation by mediaserver, or mediaserver crash.
54    };
55
56    /* Client should declare Buffer on the stack and pass address to obtainBuffer()
57     * and releaseBuffer().  See also callback_t for EVENT_MORE_DATA.
58     */
59
60    class Buffer
61    {
62    public:
63        // FIXME use m prefix
64        size_t      frameCount;     // number of sample frames corresponding to size;
65                                    // on input it is the number of frames available,
66                                    // on output is the number of frames actually drained
67                                    // (currently ignored but will make the primary field in future)
68
69        size_t      size;           // input/output in bytes == frameCount * frameSize
70                                    // on output is the number of bytes actually drained
71                                    // FIXME this is redundant with respect to frameCount,
72                                    // and TRANSFER_OBTAIN mode is broken for 8-bit data
73                                    // since we don't define the frame format
74
75        union {
76            void*       raw;
77            short*      i16;        // signed 16-bit
78            int8_t*     i8;         // unsigned 8-bit, offset by 0x80
79        };
80    };
81
82    /* As a convenience, if a callback is supplied, a handler thread
83     * is automatically created with the appropriate priority. This thread
84     * invokes the callback when a new buffer becomes available or various conditions occur.
85     * Parameters:
86     *
87     * event:   type of event notified (see enum AudioRecord::event_type).
88     * user:    Pointer to context for use by the callback receiver.
89     * info:    Pointer to optional parameter according to event type:
90     *          - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read
91     *                             more bytes than indicated by 'size' field and update 'size' if
92     *                             fewer bytes are consumed.
93     *          - EVENT_OVERRUN: unused.
94     *          - EVENT_MARKER: pointer to const uint32_t containing the marker position in frames.
95     *          - EVENT_NEW_POS: pointer to const uint32_t containing the new position in frames.
96     *          - EVENT_NEW_IAUDIORECORD: unused.
97     */
98
99    typedef void (*callback_t)(int event, void* user, void *info);
100
101    /* Returns the minimum frame count required for the successful creation of
102     * an AudioRecord object.
103     * Returned status (from utils/Errors.h) can be:
104     *  - NO_ERROR: successful operation
105     *  - NO_INIT: audio server or audio hardware not initialized
106     *  - BAD_VALUE: unsupported configuration
107     * frameCount is guaranteed to be non-zero if status is NO_ERROR,
108     * and is undefined otherwise.
109     * FIXME This API assumes a route, and so should be deprecated.
110     */
111
112     static status_t getMinFrameCount(size_t* frameCount,
113                                      uint32_t sampleRate,
114                                      audio_format_t format,
115                                      audio_channel_mask_t channelMask);
116
117    /* How data is transferred from AudioRecord
118     */
119    enum transfer_type {
120        TRANSFER_DEFAULT,   // not specified explicitly; determine from the other parameters
121        TRANSFER_CALLBACK,  // callback EVENT_MORE_DATA
122        TRANSFER_OBTAIN,    // FIXME deprecated: call obtainBuffer() and releaseBuffer()
123        TRANSFER_SYNC,      // synchronous read()
124    };
125
126    /* Constructs an uninitialized AudioRecord. No connection with
127     * AudioFlinger takes place.  Use set() after this.
128     */
129                        AudioRecord();
130
131    /* Creates an AudioRecord object and registers it with AudioFlinger.
132     * Once created, the track needs to be started before it can be used.
133     * Unspecified values are set to appropriate default values.
134     *
135     * Parameters:
136     *
137     * inputSource:        Select the audio input to record from (e.g. AUDIO_SOURCE_DEFAULT).
138     * sampleRate:         Data sink sampling rate in Hz.
139     * format:             Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
140     *                     16 bits per sample).
141     * channelMask:        Channel mask, such that audio_is_input_channel(channelMask) is true.
142     * frameCount:         Minimum size of track PCM buffer in frames. This defines the
143     *                     application's contribution to the
144     *                     latency of the track.  The actual size selected by the AudioRecord could
145     *                     be larger if the requested size is not compatible with current audio HAL
146     *                     latency.  Zero means to use a default value.
147     * cbf:                Callback function. If not null, this function is called periodically
148     *                     to consume new data and inform of marker, position updates, etc.
149     * user:               Context for use by the callback receiver.
150     * notificationFrames: The callback function is called each time notificationFrames PCM
151     *                     frames are ready in record track output buffer.
152     * sessionId:          Not yet supported.
153     * transferType:       How data is transferred from AudioRecord.
154     * flags:              See comments on audio_input_flags_t in <system/audio.h>
155     * pAttributes:        If not NULL, supersedes inputSource for use case selection.
156     * threadCanCallJava:  Not present in parameter list, and so is fixed at false.
157     */
158
159                        AudioRecord(audio_source_t inputSource,
160                                    uint32_t sampleRate,
161                                    audio_format_t format,
162                                    audio_channel_mask_t channelMask,
163                                    size_t frameCount = 0,
164                                    callback_t cbf = NULL,
165                                    void* user = NULL,
166                                    uint32_t notificationFrames = 0,
167                                    int sessionId = AUDIO_SESSION_ALLOCATE,
168                                    transfer_type transferType = TRANSFER_DEFAULT,
169                                    audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
170                                    const audio_attributes_t* pAttributes = NULL);
171
172    /* Terminates the AudioRecord and unregisters it from AudioFlinger.
173     * Also destroys all resources associated with the AudioRecord.
174     */
175protected:
176                        virtual ~AudioRecord();
177public:
178
179    /* Initialize an AudioRecord that was created using the AudioRecord() constructor.
180     * Don't call set() more than once, or after an AudioRecord() constructor that takes parameters.
181     * Returned status (from utils/Errors.h) can be:
182     *  - NO_ERROR: successful intialization
183     *  - INVALID_OPERATION: AudioRecord is already initialized or record device is already in use
184     *  - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
185     *  - NO_INIT: audio server or audio hardware not initialized
186     *  - PERMISSION_DENIED: recording is not allowed for the requesting process
187     * If status is not equal to NO_ERROR, don't call any other APIs on this AudioRecord.
188     *
189     * Parameters not listed in the AudioRecord constructors above:
190     *
191     * threadCanCallJava:  Whether callbacks are made from an attached thread and thus can call JNI.
192     */
193            status_t    set(audio_source_t inputSource,
194                            uint32_t sampleRate,
195                            audio_format_t format,
196                            audio_channel_mask_t channelMask,
197                            size_t frameCount = 0,
198                            callback_t cbf = NULL,
199                            void* user = NULL,
200                            uint32_t notificationFrames = 0,
201                            bool threadCanCallJava = false,
202                            int sessionId = AUDIO_SESSION_ALLOCATE,
203                            transfer_type transferType = TRANSFER_DEFAULT,
204                            audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
205                            const audio_attributes_t* pAttributes = NULL);
206
207    /* Result of constructing the AudioRecord. This must be checked for successful initialization
208     * before using any AudioRecord API (except for set()), because using
209     * an uninitialized AudioRecord produces undefined results.
210     * See set() method above for possible return codes.
211     */
212            status_t    initCheck() const   { return mStatus; }
213
214    /* Returns this track's estimated latency in milliseconds.
215     * This includes the latency due to AudioRecord buffer size,
216     * and audio hardware driver.
217     */
218            uint32_t    latency() const     { return mLatency; }
219
220   /* getters, see constructor and set() */
221
222            audio_format_t format() const   { return mFormat; }
223            uint32_t    channelCount() const    { return mChannelCount; }
224            size_t      frameCount() const  { return mFrameCount; }
225            size_t      frameSize() const   { return mFrameSize; }
226            audio_source_t inputSource() const  { return mAttributes.source; }
227
228    /* After it's created the track is not active. Call start() to
229     * make it active. If set, the callback will start being called.
230     * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until
231     * the specified event occurs on the specified trigger session.
232     */
233            status_t    start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
234                              int triggerSession = 0);
235
236    /* Stop a track.  The callback will cease being called.  Note that obtainBuffer() still
237     * works and will drain buffers until the pool is exhausted, and then will return WOULD_BLOCK.
238     */
239            void        stop();
240            bool        stopped() const;
241
242    /* Return the sink sample rate for this record track in Hz.
243     * Unlike AudioTrack, the sample rate is const after initialization, so doesn't need a lock.
244     */
245            uint32_t    getSampleRate() const   { return mSampleRate; }
246
247    /* Return the notification frame count.
248     * This is approximately how often the callback is invoked, for transfer type TRANSFER_CALLBACK.
249     */
250            size_t      notificationFrames() const  { return mNotificationFramesAct; }
251
252    /* Sets marker position. When record reaches the number of frames specified,
253     * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition
254     * with marker == 0 cancels marker notification callback.
255     * To set a marker at a position which would compute as 0,
256     * a workaround is to set the marker at a nearby position such as ~0 or 1.
257     * If the AudioRecord has been opened with no callback function associated,
258     * the operation will fail.
259     *
260     * Parameters:
261     *
262     * marker:   marker position expressed in wrapping (overflow) frame units,
263     *           like the return value of getPosition().
264     *
265     * Returned status (from utils/Errors.h) can be:
266     *  - NO_ERROR: successful operation
267     *  - INVALID_OPERATION: the AudioRecord has no callback installed.
268     */
269            status_t    setMarkerPosition(uint32_t marker);
270            status_t    getMarkerPosition(uint32_t *marker) const;
271
272    /* Sets position update period. Every time the number of frames specified has been recorded,
273     * a callback with event type EVENT_NEW_POS is called.
274     * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
275     * callback.
276     * If the AudioRecord has been opened with no callback function associated,
277     * the operation will fail.
278     * Extremely small values may be rounded up to a value the implementation can support.
279     *
280     * Parameters:
281     *
282     * updatePeriod:  position update notification period expressed in frames.
283     *
284     * Returned status (from utils/Errors.h) can be:
285     *  - NO_ERROR: successful operation
286     *  - INVALID_OPERATION: the AudioRecord has no callback installed.
287     */
288            status_t    setPositionUpdatePeriod(uint32_t updatePeriod);
289            status_t    getPositionUpdatePeriod(uint32_t *updatePeriod) const;
290
291    /* Return the total number of frames recorded since recording started.
292     * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz.
293     * It is reset to zero by stop().
294     *
295     * Parameters:
296     *
297     *  position:  Address where to return record head position.
298     *
299     * Returned status (from utils/Errors.h) can be:
300     *  - NO_ERROR: successful operation
301     *  - BAD_VALUE:  position is NULL
302     */
303            status_t    getPosition(uint32_t *position) const;
304
305    /* Returns a handle on the audio input used by this AudioRecord.
306     *
307     * Parameters:
308     *  none.
309     *
310     * Returned value:
311     *  handle on audio hardware input
312     */
313            audio_io_handle_t    getInput() const;
314
315    /* Returns the audio session ID associated with this AudioRecord.
316     *
317     * Parameters:
318     *  none.
319     *
320     * Returned value:
321     *  AudioRecord session ID.
322     *
323     * No lock needed because session ID doesn't change after first set().
324     */
325            int    getSessionId() const { return mSessionId; }
326
327    /* Obtains a buffer of up to "audioBuffer->frameCount" full frames.
328     * After draining these frames of data, the caller should release them with releaseBuffer().
329     * If the track buffer is not empty, obtainBuffer() returns as many contiguous
330     * full frames as are available immediately.
331     * If the track buffer is empty and track is stopped, obtainBuffer() returns WOULD_BLOCK
332     * regardless of the value of waitCount.
333     * If the track buffer is empty and track is not stopped, obtainBuffer() blocks with a
334     * maximum timeout based on waitCount; see chart below.
335     * Buffers will be returned until the pool
336     * is exhausted, at which point obtainBuffer() will either block
337     * or return WOULD_BLOCK depending on the value of the "waitCount"
338     * parameter.
339     *
340     * obtainBuffer() and releaseBuffer() are deprecated for direct use by applications,
341     * which should use read() or callback EVENT_MORE_DATA instead.
342     *
343     * Interpretation of waitCount:
344     *  +n  limits wait time to n * WAIT_PERIOD_MS,
345     *  -1  causes an (almost) infinite wait time,
346     *   0  non-blocking.
347     *
348     * Buffer fields
349     * On entry:
350     *  frameCount  number of frames requested
351     * After error return:
352     *  frameCount  0
353     *  size        0
354     *  raw         undefined
355     * After successful return:
356     *  frameCount  actual number of frames available, <= number requested
357     *  size        actual number of bytes available
358     *  raw         pointer to the buffer
359     */
360
361    /* FIXME Deprecated public API for TRANSFER_OBTAIN mode */
362            status_t    obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
363                                __attribute__((__deprecated__));
364
365private:
366    /* If nonContig is non-NULL, it is an output parameter that will be set to the number of
367     * additional non-contiguous frames that are available immediately.
368     * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(),
369     * in case the requested amount of frames is in two or more non-contiguous regions.
370     * FIXME requested and elapsed are both relative times.  Consider changing to absolute time.
371     */
372            status_t    obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
373                                     struct timespec *elapsed = NULL, size_t *nonContig = NULL);
374public:
375
376    /* Release an emptied buffer of "audioBuffer->frameCount" frames for AudioFlinger to re-fill. */
377    // FIXME make private when obtainBuffer() for TRANSFER_OBTAIN is removed
378            void        releaseBuffer(Buffer* audioBuffer);
379
380    /* As a convenience we provide a read() interface to the audio buffer.
381     * Input parameter 'size' is in byte units.
382     * This is implemented on top of obtainBuffer/releaseBuffer. For best
383     * performance use callbacks. Returns actual number of bytes read >= 0,
384     * or one of the following negative status codes:
385     *      INVALID_OPERATION   AudioRecord is configured for streaming mode
386     *      BAD_VALUE           size is invalid
387     *      WOULD_BLOCK         when obtainBuffer() returns same, or
388     *                          AudioRecord was stopped during the read
389     *      or any other error code returned by IAudioRecord::start() or restoreRecord_l().
390     */
391            ssize_t     read(void* buffer, size_t size);
392
393    /* Return the number of input frames lost in the audio driver since the last call of this
394     * function.  Audio driver is expected to reset the value to 0 and restart counting upon
395     * returning the current value by this function call.  Such loss typically occurs when the
396     * user space process is blocked longer than the capacity of audio driver buffers.
397     * Units: the number of input audio frames.
398     * FIXME The side-effect of resetting the counter may be incompatible with multi-client.
399     * Consider making it more like AudioTrack::getUnderrunFrames which doesn't have side effects.
400     */
401            uint32_t    getInputFramesLost() const;
402
403private:
404    /* copying audio record objects is not allowed */
405                        AudioRecord(const AudioRecord& other);
406            AudioRecord& operator = (const AudioRecord& other);
407
408    /* a small internal class to handle the callback */
409    class AudioRecordThread : public Thread
410    {
411    public:
412        AudioRecordThread(AudioRecord& receiver, bool bCanCallJava = false);
413
414        // Do not call Thread::requestExitAndWait() without first calling requestExit().
415        // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
416        virtual void        requestExit();
417
418                void        pause();    // suspend thread from execution at next loop boundary
419                void        resume();   // allow thread to execute, if not requested to exit
420                void        wake();     // wake to handle changed notification conditions.
421
422    private:
423                void        pauseInternal(nsecs_t ns = 0LL);
424                                        // like pause(), but only used internally within thread
425
426        friend class AudioRecord;
427        virtual bool        threadLoop();
428        AudioRecord&        mReceiver;
429        virtual ~AudioRecordThread();
430        Mutex               mMyLock;    // Thread::mLock is private
431        Condition           mMyCond;    // Thread::mThreadExitedCondition is private
432        bool                mPaused;    // whether thread is requested to pause at next loop entry
433        bool                mPausedInt; // whether thread internally requests pause
434        nsecs_t             mPausedNs;  // if mPausedInt then associated timeout, otherwise ignored
435        bool                mIgnoreNextPausedInt;   // skip any internal pause and go immediately
436                                        // to processAudioBuffer() as state may have changed
437                                        // since pause time calculated.
438    };
439
440            // body of AudioRecordThread::threadLoop()
441            // returns the maximum amount of time before we would like to run again, where:
442            //      0           immediately
443            //      > 0         no later than this many nanoseconds from now
444            //      NS_WHENEVER still active but no particular deadline
445            //      NS_INACTIVE inactive so don't run again until re-started
446            //      NS_NEVER    never again
447            static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3;
448            nsecs_t processAudioBuffer();
449
450            // caller must hold lock on mLock for all _l methods
451
452            status_t openRecord_l(size_t epoch);
453
454            // FIXME enum is faster than strcmp() for parameter 'from'
455            status_t restoreRecord_l(const char *from);
456
457    sp<AudioRecordThread>   mAudioRecordThread;
458    mutable Mutex           mLock;
459
460    // Current client state:  false = stopped, true = active.  Protected by mLock.  If more states
461    // are added, consider changing this to enum State { ... } mState as in AudioTrack.
462    bool                    mActive;
463
464    // for client callback handler
465    callback_t              mCbf;               // callback handler for events, or NULL
466    void*                   mUserData;
467
468    // for notification APIs
469    uint32_t                mNotificationFramesReq; // requested number of frames between each
470                                                    // notification callback
471                                                    // as specified in constructor or set()
472    uint32_t                mNotificationFramesAct; // actual number of frames between each
473                                                    // notification callback
474    bool                    mRefreshRemaining;      // processAudioBuffer() should refresh
475                                                    // mRemainingFrames and mRetryOnPartialBuffer
476
477    // These are private to processAudioBuffer(), and are not protected by a lock
478    uint32_t                mRemainingFrames;       // number of frames to request in obtainBuffer()
479    bool                    mRetryOnPartialBuffer;  // sleep and retry after partial obtainBuffer()
480    uint32_t                mObservedSequence;      // last observed value of mSequence
481
482    uint32_t                mMarkerPosition;    // in wrapping (overflow) frame units
483    bool                    mMarkerReached;
484    uint32_t                mNewPosition;       // in frames
485    uint32_t                mUpdatePeriod;      // in frames, zero means no EVENT_NEW_POS
486
487    status_t                mStatus;
488
489    size_t                  mFrameCount;            // corresponds to current IAudioRecord, value is
490                                                    // reported back by AudioFlinger to the client
491    size_t                  mReqFrameCount;         // frame count to request the first or next time
492                                                    // a new IAudioRecord is needed, non-decreasing
493
494    // constant after constructor or set()
495    uint32_t                mSampleRate;
496    audio_format_t          mFormat;
497    uint32_t                mChannelCount;
498    size_t                  mFrameSize;         // app-level frame size == AudioFlinger frame size
499    uint32_t                mLatency;           // in ms
500    audio_channel_mask_t    mChannelMask;
501    audio_input_flags_t     mFlags;
502    int                     mSessionId;
503    transfer_type           mTransfer;
504
505    // Next 5 fields may be changed if IAudioRecord is re-created, but always != 0
506    // provided the initial set() was successful
507    sp<IAudioRecord>        mAudioRecord;
508    sp<IMemory>             mCblkMemory;
509    audio_track_cblk_t*     mCblk;              // re-load after mLock.unlock()
510    sp<IMemory>             mBufferMemory;
511    audio_io_handle_t       mInput;             // returned by AudioSystem::getInput()
512
513    int                     mPreviousPriority;  // before start()
514    SchedPolicy             mPreviousSchedulingGroup;
515    bool                    mAwaitBoost;    // thread should wait for priority boost before running
516
517    // The proxy should only be referenced while a lock is held because the proxy isn't
518    // multi-thread safe.
519    // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock,
520    // provided that the caller also holds an extra reference to the proxy and shared memory to keep
521    // them around in case they are replaced during the obtainBuffer().
522    sp<AudioRecordClientProxy> mProxy;
523
524    bool                    mInOverrun;         // whether recorder is currently in overrun state
525
526private:
527    class DeathNotifier : public IBinder::DeathRecipient {
528    public:
529        DeathNotifier(AudioRecord* audioRecord) : mAudioRecord(audioRecord) { }
530    protected:
531        virtual void        binderDied(const wp<IBinder>& who);
532    private:
533        const wp<AudioRecord> mAudioRecord;
534    };
535
536    sp<DeathNotifier>       mDeathNotifier;
537    uint32_t                mSequence;              // incremented for each new IAudioRecord attempt
538    audio_attributes_t      mAttributes;
539};
540
541}; // namespace android
542
543#endif // ANDROID_AUDIORECORD_H
544