AudioRecord.h revision 7cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebd
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     * flags:              See comments on audio_input_flags_t in <system/audio.h>
148     * threadCanCallJava:  Not present in parameter list, and so is fixed at false.
149     */
150
151                        AudioRecord(audio_source_t inputSource,
152                                    uint32_t sampleRate,
153                                    audio_format_t format,
154                                    audio_channel_mask_t channelMask,
155                                    int frameCount      = 0,
156                                    callback_t cbf = NULL,
157                                    void* user = NULL,
158                                    int notificationFrames = 0,
159                                    int sessionId = 0,
160                                    transfer_type transferType = TRANSFER_DEFAULT,
161                                    audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE);
162
163    /* Terminates the AudioRecord and unregisters it from AudioFlinger.
164     * Also destroys all resources associated with the AudioRecord.
165     */
166protected:
167                        virtual ~AudioRecord();
168public:
169
170    /* Initialize an AudioRecord that was created using the AudioRecord() constructor.
171     * Don't call set() more than once, or after an AudioRecord() constructor that takes parameters.
172     * Returned status (from utils/Errors.h) can be:
173     *  - NO_ERROR: successful intialization
174     *  - INVALID_OPERATION: AudioRecord is already initialized or record device is already in use
175     *  - BAD_VALUE: invalid parameter (channels, format, sampleRate...)
176     *  - NO_INIT: audio server or audio hardware not initialized
177     *  - PERMISSION_DENIED: recording is not allowed for the requesting process
178     *
179     * Parameters not listed in the AudioRecord constructors above:
180     *
181     * threadCanCallJava:  Whether callbacks are made from an attached thread and thus can call JNI.
182     */
183            status_t    set(audio_source_t inputSource,
184                            uint32_t sampleRate,
185                            audio_format_t format,
186                            audio_channel_mask_t channelMask,
187                            int frameCount      = 0,
188                            callback_t cbf = NULL,
189                            void* user = NULL,
190                            int notificationFrames = 0,
191                            bool threadCanCallJava = false,
192                            int sessionId = 0,
193                            transfer_type transferType = TRANSFER_DEFAULT,
194                            audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE);
195
196    /* Result of constructing the AudioRecord. This must be checked
197     * before using any AudioRecord API (except for set()), because using
198     * an uninitialized AudioRecord produces undefined results.
199     * See set() method above for possible return codes.
200     */
201            status_t    initCheck() const   { return mStatus; }
202
203    /* Returns this track's estimated latency in milliseconds.
204     * This includes the latency due to AudioRecord buffer size,
205     * and audio hardware driver.
206     */
207            uint32_t    latency() const     { return mLatency; }
208
209   /* getters, see constructor and set() */
210
211            audio_format_t format() const   { return mFormat; }
212            uint32_t    channelCount() const    { return mChannelCount; }
213            size_t      frameCount() const  { return mFrameCount; }
214            size_t      frameSize() const   { return mFrameSize; }
215            audio_source_t inputSource() const  { return mInputSource; }
216
217    /* After it's created the track is not active. Call start() to
218     * make it active. If set, the callback will start being called.
219     * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until
220     * the specified event occurs on the specified trigger session.
221     */
222            status_t    start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
223                              int triggerSession = 0);
224
225    /* Stop a track. If set, the callback will cease being called.  Note that obtainBuffer() still
226     * works and will drain buffers until the pool is exhausted, and then will return WOULD_BLOCK.
227     */
228            void        stop();
229            bool        stopped() const;
230
231    /* Return the sink sample rate for this record track in Hz.
232     * Unlike AudioTrack, the sample rate is const after initialization, so doesn't need a lock.
233     */
234            uint32_t    getSampleRate() const   { return mSampleRate; }
235
236    /* Sets marker position. When record reaches the number of frames specified,
237     * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition
238     * with marker == 0 cancels marker notification callback.
239     * To set a marker at a position which would compute as 0,
240     * a workaround is to the set the marker at a nearby position such as ~0 or 1.
241     * If the AudioRecord has been opened with no callback function associated,
242     * the operation will fail.
243     *
244     * Parameters:
245     *
246     * marker:   marker position expressed in wrapping (overflow) frame units,
247     *           like the return value of getPosition().
248     *
249     * Returned status (from utils/Errors.h) can be:
250     *  - NO_ERROR: successful operation
251     *  - INVALID_OPERATION: the AudioRecord has no callback installed.
252     */
253            status_t    setMarkerPosition(uint32_t marker);
254            status_t    getMarkerPosition(uint32_t *marker) const;
255
256    /* Sets position update period. Every time the number of frames specified has been recorded,
257     * a callback with event type EVENT_NEW_POS is called.
258     * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
259     * callback.
260     * If the AudioRecord has been opened with no callback function associated,
261     * the operation will fail.
262     * Extremely small values may be rounded up to a value the implementation can support.
263     *
264     * Parameters:
265     *
266     * updatePeriod:  position update notification period expressed in frames.
267     *
268     * Returned status (from utils/Errors.h) can be:
269     *  - NO_ERROR: successful operation
270     *  - INVALID_OPERATION: the AudioRecord has no callback installed.
271     */
272            status_t    setPositionUpdatePeriod(uint32_t updatePeriod);
273            status_t    getPositionUpdatePeriod(uint32_t *updatePeriod) const;
274
275    /* Return the total number of frames recorded since recording started.
276     * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz.
277     * It is reset to zero by stop().
278     *
279     * Parameters:
280     *
281     *  position:  Address where to return record head position.
282     *
283     * Returned status (from utils/Errors.h) can be:
284     *  - NO_ERROR: successful operation
285     *  - BAD_VALUE:  position is NULL
286     */
287            status_t    getPosition(uint32_t *position) const;
288
289    /* Returns a handle on the audio input used by this AudioRecord.
290     *
291     * Parameters:
292     *  none.
293     *
294     * Returned value:
295     *  handle on audio hardware input
296     */
297            audio_io_handle_t    getInput() const;
298
299    /* Returns the audio session ID associated with this AudioRecord.
300     *
301     * Parameters:
302     *  none.
303     *
304     * Returned value:
305     *  AudioRecord session ID.
306     *
307     * No lock needed because session ID doesn't change after first set().
308     */
309            int    getSessionId() const { return mSessionId; }
310
311    /* Obtains a buffer of up to "audioBuffer->frameCount" full frames.
312     * After draining these frames of data, the caller should release them with releaseBuffer().
313     * If the track buffer is not empty, obtainBuffer() returns as many contiguous
314     * full frames as are available immediately.
315     * If the track buffer is empty and track is stopped, obtainBuffer() returns WOULD_BLOCK
316     * regardless of the value of waitCount.
317     * If the track buffer is empty and track is not stopped, obtainBuffer() blocks with a
318     * maximum timeout based on waitCount; see chart below.
319     * Buffers will be returned until the pool
320     * is exhausted, at which point obtainBuffer() will either block
321     * or return WOULD_BLOCK depending on the value of the "waitCount"
322     * parameter.
323     *
324     * obtainBuffer() and releaseBuffer() are deprecated for direct use by applications,
325     * which should use read() or callback EVENT_MORE_DATA instead.
326     *
327     * Interpretation of waitCount:
328     *  +n  limits wait time to n * WAIT_PERIOD_MS,
329     *  -1  causes an (almost) infinite wait time,
330     *   0  non-blocking.
331     *
332     * Buffer fields
333     * On entry:
334     *  frameCount  number of frames requested
335     * After error return:
336     *  frameCount  0
337     *  size        0
338     *  raw         undefined
339     * After successful return:
340     *  frameCount  actual number of frames available, <= number requested
341     *  size        actual number of bytes available
342     *  raw         pointer to the buffer
343     */
344
345    /* FIXME Deprecated public API for TRANSFER_OBTAIN mode */
346            status_t    obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
347                                __attribute__((__deprecated__));
348
349private:
350    /* New internal API.
351     * If nonContig is non-NULL, it is an output parameter that will be set to the number of
352     * additional non-contiguous frames that are available immediately.
353     * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(),
354     * in case the requested amount of frames is in two or more non-contiguous regions.
355     * FIXME requested and elapsed are both relative times.  Consider changing to absolute time.
356     */
357            status_t    obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
358                                     struct timespec *elapsed = NULL, size_t *nonContig = NULL);
359public:
360
361    /* Release an emptied buffer of "audioBuffer->frameCount" frames for AudioFlinger to re-fill. */
362    // FIXME make private when obtainBuffer() for TRANSFER_OBTAIN is removed
363            void        releaseBuffer(Buffer* audioBuffer);
364
365    /* As a convenience we provide a read() interface to the audio buffer.
366     * Input parameter 'size' is in byte units.
367     * This is implemented on top of obtainBuffer/releaseBuffer. For best
368     * performance use callbacks. Returns actual number of bytes read >= 0,
369     * or a negative status code.
370     */
371            ssize_t     read(void* buffer, size_t size);
372
373    /* Return the number of input frames lost in the audio driver since the last call of this
374     * function.  Audio driver is expected to reset the value to 0 and restart counting upon
375     * returning the current value by this function call.  Such loss typically occurs when the
376     * user space process is blocked longer than the capacity of audio driver buffers.
377     * Units: the number of input audio frames.
378     */
379            unsigned int  getInputFramesLost() const;
380
381private:
382    /* copying audio record objects is not allowed */
383                        AudioRecord(const AudioRecord& other);
384            AudioRecord& operator = (const AudioRecord& other);
385
386    /* a small internal class to handle the callback */
387    class AudioRecordThread : public Thread
388    {
389    public:
390        AudioRecordThread(AudioRecord& receiver, bool bCanCallJava = false);
391
392        // Do not call Thread::requestExitAndWait() without first calling requestExit().
393        // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
394        virtual void        requestExit();
395
396                void        pause();    // suspend thread from execution at next loop boundary
397                void        resume();   // allow thread to execute, if not requested to exit
398                void        pauseConditional();
399                                        // like pause(), but only if prior resume() wasn't latched
400
401    private:
402        friend class AudioRecord;
403        virtual bool        threadLoop();
404        AudioRecord&        mReceiver;
405        virtual ~AudioRecordThread();
406        Mutex               mMyLock;    // Thread::mLock is private
407        Condition           mMyCond;    // Thread::mThreadExitedCondition is private
408        bool                mPaused;    // whether thread is currently paused
409        bool                mResumeLatch;   // whether next pauseConditional() will be a nop
410    };
411
412            // body of AudioRecordThread::threadLoop()
413            // returns the maximum amount of time before we would like to run again, where:
414            //      0           immediately
415            //      > 0         no later than this many nanoseconds from now
416            //      NS_WHENEVER still active but no particular deadline
417            //      NS_INACTIVE inactive so don't run again until re-started
418            //      NS_NEVER    never again
419            static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3;
420            nsecs_t processAudioBuffer(const sp<AudioRecordThread>& thread);
421
422            // caller must hold lock on mLock for all _l methods
423            status_t openRecord_l(uint32_t sampleRate,
424                                audio_format_t format,
425                                size_t frameCount,
426                                audio_input_flags_t flags,
427                                audio_io_handle_t input,
428                                size_t epoch);
429
430            audio_io_handle_t getInput_l();
431
432            // FIXME enum is faster than strcmp() for parameter 'from'
433            status_t restoreRecord_l(const char *from);
434
435    sp<AudioRecordThread>   mAudioRecordThread;
436    mutable Mutex           mLock;
437
438    // Current client state:  false = stopped, true = active.  Protected by mLock.  If more states
439    // are added, consider changing this to enum State { ... } mState as in AudioTrack.
440    bool                    mActive;
441
442    // for client callback handler
443    callback_t              mCbf;               // callback handler for events, or NULL
444    void*                   mUserData;          // for client callback handler
445
446    // for notification APIs
447    uint32_t                mNotificationFramesReq; // requested number of frames between each
448                                                    // notification callback
449    uint32_t                mNotificationFramesAct; // actual number of frames between each
450                                                    // notification callback
451    bool                    mRefreshRemaining;  // processAudioBuffer() should refresh next 2
452
453    // These are private to processAudioBuffer(), and are not protected by a lock
454    uint32_t                mRemainingFrames;       // number of frames to request in obtainBuffer()
455    bool                    mRetryOnPartialBuffer;  // sleep and retry after partial obtainBuffer()
456    int                     mObservedSequence;      // last observed value of mSequence
457
458    uint32_t                mMarkerPosition;    // in wrapping (overflow) frame units
459    bool                    mMarkerReached;
460    uint32_t                mNewPosition;       // in frames
461    uint32_t                mUpdatePeriod;      // in frames, zero means no EVENT_NEW_POS
462
463    status_t                mStatus;
464
465    // constant after constructor or set()
466    uint32_t                mSampleRate;
467    size_t                  mFrameCount;
468    audio_format_t          mFormat;
469    uint32_t                mChannelCount;
470    size_t                  mFrameSize;         // app-level frame size == AudioFlinger frame size
471    audio_source_t          mInputSource;
472    uint32_t                mLatency;           // in ms
473    audio_channel_mask_t    mChannelMask;
474    audio_input_flags_t     mFlags;
475    int                     mSessionId;
476    transfer_type           mTransfer;
477
478    audio_io_handle_t       mInput;             // returned by AudioSystem::getInput()
479
480    // may be changed if IAudioRecord object is re-created
481    sp<IAudioRecord>        mAudioRecord;
482    sp<IMemory>             mCblkMemory;
483    audio_track_cblk_t*     mCblk;              // re-load after mLock.unlock()
484
485    int                     mPreviousPriority;  // before start()
486    SchedPolicy             mPreviousSchedulingGroup;
487    bool                    mAwaitBoost;    // thread should wait for priority boost before running
488
489    // The proxy should only be referenced while a lock is held because the proxy isn't
490    // multi-thread safe.
491    // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock,
492    // provided that the caller also holds an extra reference to the proxy and shared memory to keep
493    sp<AudioRecordClientProxy> mProxy;
494
495    bool                    mInOverrun;         // whether recorder is currently in overrun state
496
497private:
498    class DeathNotifier : public IBinder::DeathRecipient {
499    public:
500        DeathNotifier(AudioRecord* audioRecord) : mAudioRecord(audioRecord) { }
501    protected:
502        virtual void        binderDied(const wp<IBinder>& who);
503    private:
504        const wp<AudioRecord> mAudioRecord;
505    };
506
507    sp<DeathNotifier>       mDeathNotifier;
508    uint32_t                mSequence;              // incremented for each new IAudioRecord attempt
509};
510
511}; // namespace android
512
513#endif // ANDROID_AUDIORECORD_H
514