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