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