blob: 7df66686f21f4dae3ac2aa6c998c1a23ed38fb8b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26#include <utils/RefBase.h>
27#include <utils/Errors.h>
Mathias Agopian07952722009-05-19 19:08:10 -070028#include <binder/IInterface.h>
29#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <utils/threads.h>
31
Dima Zavin34bb4192011-05-11 14:15:23 -070032#include <system/audio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34namespace android {
35
Glenn Kastena6dafea2012-01-18 14:54:46 -080036class audio_track_cblk_t;
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038// ----------------------------------------------------------------------------
39
40class AudioRecord
41{
42public:
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 static const int DEFAULT_SAMPLE_RATE = 8000;
45
46 /* Events used by AudioRecord callback function (callback_t).
Eric Laurenta553c252009-07-17 12:17:14 -070047 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 * to keep in sync with frameworks/base/media/java/android/media/AudioRecord.java
49 */
50 enum event_type {
51 EVENT_MORE_DATA = 0, // Request to reqd more data from PCM buffer.
52 EVENT_OVERRUN = 1, // PCM buffer overrun occured.
53 EVENT_MARKER = 2, // Record head is at the specified marker position
54 // (See setMarkerPosition()).
Eric Laurenta553c252009-07-17 12:17:14 -070055 EVENT_NEW_POS = 3, // Record head is at a new position
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 // (See setPositionUpdatePeriod()).
57 };
58
59 /* Create Buffer on the stack and pass it to obtainBuffer()
60 * and releaseBuffer().
61 */
62
63 class Buffer
64 {
65 public:
66 enum {
67 MUTE = 0x00000001
68 };
69 uint32_t flags;
70 int channelCount;
Glenn Kasten0a204ed2012-01-12 12:27:51 -080071 audio_format_t format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 size_t frameCount;
73 size_t size;
74 union {
75 void* raw;
76 short* i16;
77 int8_t* i8;
78 };
79 };
80
81 /* These are static methods to control the system-wide AudioFlinger
82 * only privileged processes can have access to them
83 */
84
85// static status_t setMasterMute(bool mute);
86
87 /* As a convenience, if a callback is supplied, a handler thread
88 * is automatically created with the appropriate priority. This thread
89 * invokes the callback when a new buffer becomes ready or an overrun condition occurs.
90 * Parameters:
91 *
92 * event: type of event notified (see enum AudioRecord::event_type).
93 * user: Pointer to context for use by the callback receiver.
94 * info: Pointer to optional parameter according to event type:
95 * - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read
96 * more bytes than indicated by 'size' field and update 'size' if less bytes are
97 * read.
98 * - EVENT_OVERRUN: unused.
99 * - EVENT_MARKER: pointer to an uin32_t containing the marker position in frames.
100 * - EVENT_NEW_POS: pointer to an uin32_t containing the new position in frames.
101 */
102
103 typedef void (*callback_t)(int event, void* user, void *info);
104
Chia-chi Yeh97d61f72010-06-22 08:01:13 +0800105 /* 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 */
112
113 static status_t getMinFrameCount(int* frameCount,
114 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800115 audio_format_t format,
Chia-chi Yeh97d61f72010-06-22 08:01:13 +0800116 int channelCount);
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /* Constructs an uninitialized AudioRecord. No connection with
119 * AudioFlinger takes place.
120 */
121 AudioRecord();
122
123 /* Creates an AudioRecord track and registers it with AudioFlinger.
124 * Once created, the track needs to be started before it can be used.
125 * Unspecified values are set to the audio hardware's current
126 * values.
127 *
128 * Parameters:
129 *
Eric Laurenta553c252009-07-17 12:17:14 -0700130 * inputSource: Select the audio input to record to (e.g. AUDIO_SOURCE_DEFAULT).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * sampleRate: Track sampling rate in Hz.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700132 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * 16 bits per sample).
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700134 * channelMask: Channel mask: see audio_channels_t.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * frameCount: Total size of track PCM buffer in frames. This defines the
136 * latency of the track.
137 * flags: A bitmask of acoustic values from enum record_flags. It enables
138 * AGC, NS, and IIR.
139 * cbf: Callback function. If not null, this function is called periodically
140 * to provide new PCM data.
141 * notificationFrames: The callback function is called each time notificationFrames PCM
142 * frames are ready in record track output buffer.
143 * user Context for use by the callback receiver.
144 */
145
Glenn Kastenf60a5d72012-03-07 09:04:02 -0800146 // FIXME consider removing this alias and replacing it by audio_in_acoustics_t
147 // or removing the parameter entirely if it is unused
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 enum record_flags {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700149 RECORD_AGC_ENABLE = AUDIO_IN_ACOUSTICS_AGC_ENABLE,
150 RECORD_NS_ENABLE = AUDIO_IN_ACOUSTICS_NS_ENABLE,
151 RECORD_IIR_ENABLE = AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 };
153
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800154 AudioRecord(audio_source_t inputSource,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 uint32_t sampleRate = 0,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800156 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700157 uint32_t channelMask = AUDIO_CHANNEL_IN_MONO,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 int frameCount = 0,
Glenn Kastenf60a5d72012-03-07 09:04:02 -0800159 record_flags flags = (record_flags) 0,
Glenn Kasten3694ec12012-01-27 16:47:15 -0800160 callback_t cbf = NULL,
161 void* user = NULL,
Eric Laurent65b65452010-06-01 23:49:17 -0700162 int notificationFrames = 0,
163 int sessionId = 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165
166 /* Terminates the AudioRecord and unregisters it from AudioFlinger.
167 * Also destroys all resources assotiated with the AudioRecord.
168 */
169 ~AudioRecord();
170
171
172 /* Initialize an uninitialized AudioRecord.
173 * Returned status (from utils/Errors.h) can be:
174 * - NO_ERROR: successful intialization
175 * - INVALID_OPERATION: AudioRecord is already intitialized or record device is already in use
Eric Laurenta553c252009-07-17 12:17:14 -0700176 * - BAD_VALUE: invalid parameter (channels, format, sampleRate...)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * - NO_INIT: audio server or audio hardware not initialized
178 * - PERMISSION_DENIED: recording is not allowed for the requesting process
179 * */
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800180 status_t set(audio_source_t inputSource = AUDIO_SOURCE_DEFAULT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 uint32_t sampleRate = 0,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800182 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700183 uint32_t channelMask = AUDIO_CHANNEL_IN_MONO,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 int frameCount = 0,
Glenn Kastenf60a5d72012-03-07 09:04:02 -0800185 record_flags flags = (record_flags) 0,
Glenn Kasten3694ec12012-01-27 16:47:15 -0800186 callback_t cbf = NULL,
187 void* user = NULL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 int notificationFrames = 0,
Eric Laurent65b65452010-06-01 23:49:17 -0700189 bool threadCanCallJava = false,
190 int sessionId = 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
192
193 /* Result of constructing the AudioRecord. This must be checked
194 * before using any AudioRecord API (except for set()), using
195 * an uninitialized AudioRecord produces undefined results.
196 * See set() method above for possible return codes.
197 */
198 status_t initCheck() const;
199
200 /* Returns this track's latency in milliseconds.
201 * This includes the latency due to AudioRecord buffer size
202 * and audio hardware driver.
203 */
204 uint32_t latency() const;
205
206 /* getters, see constructor */
207
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800208 audio_format_t format() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 int channelCount() const;
Eric Laurenta553c252009-07-17 12:17:14 -0700210 int channels() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 uint32_t frameCount() const;
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800212 size_t frameSize() const;
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800213 audio_source_t inputSource() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
215
216 /* After it's created the track is not active. Call start() to
217 * make it active. If set, the callback will start being called.
218 */
219 status_t start();
220
221 /* Stop a track. If set, the callback will cease being called and
222 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
223 * and will fill up buffers until the pool is exhausted.
224 */
225 status_t stop();
226 bool stopped() const;
227
Eric Laurent88e209d2009-07-07 07:10:45 -0700228 /* get sample rate for this record track
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Glenn Kastenbd714b62012-02-24 16:33:14 -0800230 uint32_t getSampleRate() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
232 /* Sets marker position. When record reaches the number of frames specified,
233 * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition
Eric Laurenta553c252009-07-17 12:17:14 -0700234 * with marker == 0 cancels marker notification callback.
235 * If the AudioRecord has been opened with no callback function associated,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * the operation will fail.
237 *
238 * Parameters:
239 *
240 * marker: marker position expressed in frames.
241 *
242 * Returned status (from utils/Errors.h) can be:
243 * - NO_ERROR: successful operation
244 * - INVALID_OPERATION: the AudioRecord has no callback installed.
245 */
246 status_t setMarkerPosition(uint32_t marker);
Glenn Kastenbd714b62012-02-24 16:33:14 -0800247 status_t getMarkerPosition(uint32_t *marker) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249
Eric Laurenta553c252009-07-17 12:17:14 -0700250 /* Sets position update period. Every time the number of frames specified has been recorded,
251 * a callback with event type EVENT_NEW_POS is called.
252 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
253 * callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * If the AudioRecord has been opened with no callback function associated,
255 * the operation will fail.
256 *
257 * Parameters:
258 *
259 * updatePeriod: position update notification period expressed in frames.
260 *
261 * Returned status (from utils/Errors.h) can be:
262 * - NO_ERROR: successful operation
263 * - INVALID_OPERATION: the AudioRecord has no callback installed.
264 */
265 status_t setPositionUpdatePeriod(uint32_t updatePeriod);
Glenn Kastenbd714b62012-02-24 16:33:14 -0800266 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
268
Eric Laurenta553c252009-07-17 12:17:14 -0700269 /* Gets record head position. The position is the total number of frames
270 * recorded since record start.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 *
272 * Parameters:
273 *
274 * position: Address where to return record head position within AudioRecord buffer.
275 *
276 * Returned status (from utils/Errors.h) can be:
277 * - NO_ERROR: successful operation
278 * - BAD_VALUE: position is NULL
279 */
Glenn Kastenbd714b62012-02-24 16:33:14 -0800280 status_t getPosition(uint32_t *position) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
Eric Laurenta553c252009-07-17 12:17:14 -0700282 /* returns a handle on the audio input used by this AudioRecord.
283 *
284 * Parameters:
285 * none.
286 *
287 * Returned value:
288 * handle on audio hardware input
289 */
Glenn Kastenbd714b62012-02-24 16:33:14 -0800290 audio_io_handle_t getInput() const;
Eric Laurenta553c252009-07-17 12:17:14 -0700291
Eric Laurent65b65452010-06-01 23:49:17 -0700292 /* returns the audio session ID associated to this AudioRecord.
293 *
294 * Parameters:
295 * none.
296 *
297 * Returned value:
298 * AudioRecord session ID.
299 */
Glenn Kastenbd714b62012-02-24 16:33:14 -0800300 int getSessionId() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /* obtains a buffer of "frameCount" frames. The buffer must be
303 * filled entirely. If the track is stopped, obtainBuffer() returns
Glenn Kastenbd3db2f2012-02-24 13:00:45 -0800304 * STOPPED instead of NO_ERROR as long as there are buffers available,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * at which point NO_MORE_BUFFERS is returned.
306 * Buffers will be returned until the pool (buffercount())
307 * is exhausted, at which point obtainBuffer() will either block
308 * or return WOULD_BLOCK depending on the value of the "blocking"
309 * parameter.
310 */
311
312 enum {
313 NO_MORE_BUFFERS = 0x80000001,
314 STOPPED = 1
315 };
316
317 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
318 void releaseBuffer(Buffer* audioBuffer);
319
320
321 /* As a convenience we provide a read() interface to the audio buffer.
Glenn Kastenbd3db2f2012-02-24 13:00:45 -0800322 * This is implemented on top of obtainBuffer/releaseBuffer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
324 ssize_t read(void* buffer, size_t size);
325
Glenn Kastenbd3db2f2012-02-24 13:00:45 -0800326 /* Return the amount of input frames lost in the audio driver since the last call of this
327 * function. Audio driver is expected to reset the value to 0 and restart counting upon
328 * returning the current value by this function call. Such loss typically occurs when the
329 * user space process is blocked longer than the capacity of audio driver buffers.
Eric Laurent47d0a922010-02-26 02:47:27 -0800330 * Unit: the number of input audio frames
331 */
Glenn Kastenbd714b62012-02-24 16:33:14 -0800332 unsigned int getInputFramesLost() const;
Eric Laurent47d0a922010-02-26 02:47:27 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334private:
335 /* copying audio tracks is not allowed */
336 AudioRecord(const AudioRecord& other);
337 AudioRecord& operator = (const AudioRecord& other);
338
339 /* a small internal class to handle the callback */
340 class ClientRecordThread : public Thread
341 {
342 public:
343 ClientRecordThread(AudioRecord& receiver, bool bCanCallJava = false);
344 private:
345 friend class AudioRecord;
346 virtual bool threadLoop();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800347 virtual status_t readyToRun();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 virtual void onFirstRef() {}
349 AudioRecord& mReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 };
351
352 bool processAudioBuffer(const sp<ClientRecordThread>& thread);
Eric Laurent421ddc02011-03-07 14:52:59 -0800353 status_t openRecord_l(uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800354 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700355 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800356 int frameCount,
Eric Laurent49f02be2009-11-19 09:00:56 -0800357 uint32_t flags,
358 audio_io_handle_t input);
Eric Laurent421ddc02011-03-07 14:52:59 -0800359 audio_io_handle_t getInput_l();
360 status_t restoreRecord_l(audio_track_cblk_t*& cblk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 sp<IAudioRecord> mAudioRecord;
363 sp<IMemory> mCblkMemory;
364 sp<ClientRecordThread> mClientRecordThread;
Glenn Kasten6a20b262012-02-02 10:56:47 -0800365 status_t mReadyToRun;
Glenn Kastenbd714b62012-02-24 16:33:14 -0800366 mutable Mutex mLock;
Glenn Kasten6a20b262012-02-02 10:56:47 -0800367 Condition mCondition;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 uint32_t mFrameCount;
370
371 audio_track_cblk_t* mCblk;
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800372 audio_format_t mFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 uint8_t mChannelCount;
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800374 audio_source_t mInputSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 status_t mStatus;
376 uint32_t mLatency;
377
378 volatile int32_t mActive;
379
380 callback_t mCbf;
381 void* mUserData;
382 uint32_t mNotificationFrames;
383 uint32_t mRemainingFrames;
384 uint32_t mMarkerPosition;
Jean-Michel Trivi78c13142009-03-24 19:48:58 -0700385 bool mMarkerReached;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 uint32_t mNewPosition;
387 uint32_t mUpdatePeriod;
Glenn Kastenf60a5d72012-03-07 09:04:02 -0800388 record_flags mFlags;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700389 uint32_t mChannelMask;
Eric Laurent47d0a922010-02-26 02:47:27 -0800390 audio_io_handle_t mInput;
Eric Laurent65b65452010-06-01 23:49:17 -0700391 int mSessionId;
Glenn Kasten99d54432011-06-22 16:15:25 -0700392 int mPreviousPriority; // before start()
393 int mPreviousSchedulingGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394};
395
396}; // namespace android
397
398#endif /*AUDIORECORD_H_*/