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