AudioRecord.h revision 2729ea9262ca60d93047e984739887cfc89e82eb
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 AUDIORECORD_H_
18#define AUDIORECORD_H_
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <media/IAudioFlinger.h>
24#include <media/IAudioRecord.h>
25#include <media/AudioTrack.h>
26
27#include <utils/RefBase.h>
28#include <utils/Errors.h>
29#include <utils/IInterface.h>
30#include <utils/IMemory.h>
31#include <utils/threads.h>
32
33
34namespace android {
35
36// ----------------------------------------------------------------------------
37
38class AudioRecord
39{
40public:
41
42    enum stream_type {
43        DEFAULT_INPUT   =-1,
44        MIC_INPUT       = 0,
45        NUM_STREAM_TYPES
46    };
47
48    static const int DEFAULT_SAMPLE_RATE = 8000;
49
50    /* Create Buffer on the stack and pass it to obtainBuffer()
51     * and releaseBuffer().
52     */
53
54    class Buffer
55    {
56    public:
57        enum {
58            MUTE    = 0x00000001
59        };
60        uint32_t    flags;
61        int         channelCount;
62        int         format;
63        size_t      frameCount;
64        size_t      size;
65        union {
66            void*       raw;
67            short*      i16;
68            int8_t*     i8;
69        };
70    };
71
72    /* These are static methods to control the system-wide AudioFlinger
73     * only privileged processes can have access to them
74     */
75
76//    static status_t setMasterMute(bool mute);
77
78    /* Returns AudioFlinger's frame count. AudioRecord's buffers will
79     * be created with this size.
80     */
81    static  size_t      frameCount();
82
83    /* As a convenience, if a callback is supplied, a handler thread
84     * is automatically created with the appropriate priority. This thread
85     * invokes the callback when a new buffer becomes availlable.
86     */
87    typedef bool (*callback_t)(void* user, const Buffer& info);
88
89    /* Constructs an uninitialized AudioRecord. No connection with
90     * AudioFlinger takes place.
91     */
92                        AudioRecord();
93
94    /* Creates an AudioRecord track and registers it with AudioFlinger.
95     * Once created, the track needs to be started before it can be used.
96     * Unspecified values are set to the audio hardware's current
97     * values.
98     */
99
100                        AudioRecord(int streamType      = 0,
101                                    uint32_t sampleRate = 0,
102                                    int format          = 0,
103                                    int channelCount    = 0,
104                                    int bufferCount     = 0,
105                                    uint32_t flags      = 0,
106                                    callback_t cbf = 0, void* user = 0);
107
108
109    /* Terminates the AudioRecord and unregisters it from AudioFlinger.
110     * Also destroys all resources assotiated with the AudioRecord.
111     */
112                        ~AudioRecord();
113
114
115    /* Initialize an uninitialized AudioRecord. */
116            status_t    set(int streamType      = 0,
117                            uint32_t sampleRate = 0,
118                            int format          = 0,
119                            int channelCount    = 0,
120                            int bufferCount     = 0,
121                            uint32_t flags      = 0,
122                            callback_t cbf = 0, void* user = 0);
123
124
125    /* Result of constructing the AudioRecord. This must be checked
126     * before using any AudioRecord API (except for set()), using
127     * an uninitialized AudioRecord prduces undefined results.
128     */
129            status_t    initCheck() const;
130
131    /* Returns this track's latency in nanoseconds or framecount.
132     * This only includes the latency due to the fill buffer size.
133     * In particular, the hardware or driver latencies are not accounted.
134     */
135            nsecs_t     latency() const;
136
137   /* getters, see constructor */
138
139            uint32_t    sampleRate() const;
140            int         format() const;
141            int         channelCount() const;
142            int         bufferCount() const;
143
144
145    /* After it's created the track is not active. Call start() to
146     * make it active. If set, the callback will start being called.
147     */
148            status_t    start();
149
150    /* Stop a track. If set, the callback will cease being called and
151     * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
152     * and will fill up buffers until the pool is exhausted.
153     */
154            status_t    stop();
155            bool        stopped() const;
156
157    /* get sample rate for this track
158     */
159            uint32_t    getSampleRate();
160
161    /* obtains a buffer of "frameCount" frames. The buffer must be
162     * filled entirely. If the track is stopped, obtainBuffer() returns
163     * STOPPED instead of NO_ERROR as long as there are buffers availlable,
164     * at which point NO_MORE_BUFFERS is returned.
165     * Buffers will be returned until the pool (buffercount())
166     * is exhausted, at which point obtainBuffer() will either block
167     * or return WOULD_BLOCK depending on the value of the "blocking"
168     * parameter.
169     */
170
171        enum {
172            NO_MORE_BUFFERS = 0x80000001,
173            STOPPED = 1
174        };
175
176            status_t    obtainBuffer(Buffer* audioBuffer, bool blocking);
177            void        releaseBuffer(Buffer* audioBuffer);
178
179
180    /* As a convenience we provide a read() interface to the audio buffer.
181     * This is implemented on top of lockBuffer/unlockBuffer.
182     */
183            ssize_t     read(void* buffer, size_t size);
184
185private:
186    /* copying audio tracks is not allowed */
187                        AudioRecord(const AudioRecord& other);
188            AudioRecord& operator = (const AudioRecord& other);
189
190    /* a small internal class to handle the callback */
191    class ClientRecordThread : public Thread
192    {
193    public:
194        ClientRecordThread(AudioRecord& receiver);
195    private:
196        friend class AudioRecord;
197        virtual bool        threadLoop();
198        virtual status_t    readyToRun() { return NO_ERROR; }
199        virtual void        onFirstRef() {}
200        AudioRecord& mReceiver;
201    };
202
203            bool processAudioBuffer(const sp<ClientRecordThread>& thread);
204
205    sp<IAudioFlinger>       mAudioFlinger;
206    sp<IAudioRecord>        mAudioRecord;
207    sp<IMemory>             mCblkMemory;
208    sp<ClientRecordThread>  mClientRecordThread;
209    Mutex                   mRecordThreadLock;
210
211    uint32_t                mSampleRate;
212    size_t                  mFrameCount;
213
214    audio_track_cblk_t*     mCblk;
215    uint8_t                 mFormat;
216    uint8_t                 mBufferCount;
217    uint8_t                 mChannelCount   : 4;
218    uint8_t                 mReserved       : 3;
219    status_t                mStatus;
220    nsecs_t                 mLatency;
221
222    volatile int32_t        mActive;
223
224    callback_t              mCbf;
225    void*                   mUserData;
226
227    AudioRecord::Buffer      mAudioBuffer;
228    size_t                  mPosition;
229
230    uint32_t                mReservedFBC[4];
231};
232
233}; // namespace android
234
235#endif /*AUDIORECORD_H_*/
236