AudioRecord.h revision 58f30210ea540b6ce5aa6a46330cd3499483cb97
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 26#include <utils/RefBase.h> 27#include <utils/Errors.h> 28#include <binder/IInterface.h> 29#include <binder/IMemory.h> 30#include <utils/threads.h> 31 32#include <system/audio.h> 33 34namespace android { 35 36class audio_track_cblk_t; 37 38// ---------------------------------------------------------------------------- 39 40class AudioRecord 41{ 42public: 43 44 static const int DEFAULT_SAMPLE_RATE = 8000; 45 46 /* Events used by AudioRecord callback function (callback_t). 47 * 48 * 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()). 55 EVENT_NEW_POS = 3, // Record head is at a new position 56 // (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; 71 audio_format_t format; 72 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 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 */ 112 113 static status_t getMinFrameCount(int* frameCount, 114 uint32_t sampleRate, 115 audio_format_t format, 116 int channelCount); 117 118 /* 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 * 130 * inputSource: Select the audio input to record to (e.g. AUDIO_SOURCE_DEFAULT). 131 * sampleRate: Track 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: see audio_channels_t. 135 * 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 146 enum record_flags { 147 RECORD_AGC_ENABLE = AUDIO_IN_ACOUSTICS_AGC_ENABLE, 148 RECORD_NS_ENABLE = AUDIO_IN_ACOUSTICS_NS_ENABLE, 149 RECORD_IIR_ENABLE = AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE, 150 }; 151 152 AudioRecord(int inputSource, 153 uint32_t sampleRate = 0, 154 audio_format_t format = AUDIO_FORMAT_DEFAULT, 155 uint32_t channelMask = AUDIO_CHANNEL_IN_MONO, 156 int frameCount = 0, 157 uint32_t flags = 0, 158 callback_t cbf = 0, 159 void* user = 0, 160 int notificationFrames = 0, 161 int sessionId = 0); 162 163 164 /* Terminates the AudioRecord and unregisters it from AudioFlinger. 165 * Also destroys all resources assotiated with the AudioRecord. 166 */ 167 ~AudioRecord(); 168 169 170 /* Initialize an uninitialized AudioRecord. 171 * Returned status (from utils/Errors.h) can be: 172 * - NO_ERROR: successful intialization 173 * - INVALID_OPERATION: AudioRecord is already intitialized or record device is already in use 174 * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) 175 * - NO_INIT: audio server or audio hardware not initialized 176 * - PERMISSION_DENIED: recording is not allowed for the requesting process 177 * */ 178 status_t set(int inputSource = 0, 179 uint32_t sampleRate = 0, 180 audio_format_t format = AUDIO_FORMAT_DEFAULT, 181 uint32_t channelMask = AUDIO_CHANNEL_IN_MONO, 182 int frameCount = 0, 183 uint32_t flags = 0, 184 callback_t cbf = 0, 185 void* user = 0, 186 int notificationFrames = 0, 187 bool threadCanCallJava = false, 188 int sessionId = 0); 189 190 191 /* Result of constructing the AudioRecord. This must be checked 192 * before using any AudioRecord API (except for set()), using 193 * an uninitialized AudioRecord produces undefined results. 194 * See set() method above for possible return codes. 195 */ 196 status_t initCheck() const; 197 198 /* Returns this track's latency in milliseconds. 199 * This includes the latency due to AudioRecord buffer size 200 * and audio hardware driver. 201 */ 202 uint32_t latency() const; 203 204 /* getters, see constructor */ 205 206 audio_format_t format() const; 207 int channelCount() const; 208 int channels() const; 209 uint32_t frameCount() const; 210 size_t frameSize() const; 211 int inputSource() const; 212 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 */ 217 status_t start(); 218 219 /* Stop a track. If set, the callback will cease being called and 220 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works 221 * and will fill up buffers until the pool is exhausted. 222 */ 223 status_t stop(); 224 bool stopped() const; 225 226 /* get sample rate for this record track 227 */ 228 uint32_t getSampleRate(); 229 230 /* Sets marker position. When record reaches the number of frames specified, 231 * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition 232 * with marker == 0 cancels marker notification callback. 233 * If the AudioRecord has been opened with no callback function associated, 234 * the operation will fail. 235 * 236 * Parameters: 237 * 238 * marker: marker position expressed in frames. 239 * 240 * Returned status (from utils/Errors.h) can be: 241 * - NO_ERROR: successful operation 242 * - INVALID_OPERATION: the AudioRecord has no callback installed. 243 */ 244 status_t setMarkerPosition(uint32_t marker); 245 status_t getMarkerPosition(uint32_t *marker); 246 247 248 /* Sets position update period. Every time the number of frames specified has been recorded, 249 * a callback with event type EVENT_NEW_POS is called. 250 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification 251 * callback. 252 * If the AudioRecord has been opened with no callback function associated, 253 * the operation will fail. 254 * 255 * Parameters: 256 * 257 * updatePeriod: position update notification period expressed in frames. 258 * 259 * Returned status (from utils/Errors.h) can be: 260 * - NO_ERROR: successful operation 261 * - INVALID_OPERATION: the AudioRecord has no callback installed. 262 */ 263 status_t setPositionUpdatePeriod(uint32_t updatePeriod); 264 status_t getPositionUpdatePeriod(uint32_t *updatePeriod); 265 266 267 /* Gets record head position. The position is the total number of frames 268 * recorded since record start. 269 * 270 * Parameters: 271 * 272 * position: Address where to return record head position within AudioRecord buffer. 273 * 274 * Returned status (from utils/Errors.h) can be: 275 * - NO_ERROR: successful operation 276 * - BAD_VALUE: position is NULL 277 */ 278 status_t getPosition(uint32_t *position); 279 280 /* returns a handle on the audio input used by this AudioRecord. 281 * 282 * Parameters: 283 * none. 284 * 285 * Returned value: 286 * handle on audio hardware input 287 */ 288 audio_io_handle_t getInput(); 289 290 /* returns the audio session ID associated to this AudioRecord. 291 * 292 * Parameters: 293 * none. 294 * 295 * Returned value: 296 * AudioRecord session ID. 297 */ 298 int getSessionId(); 299 300 /* obtains a buffer of "frameCount" frames. The buffer must be 301 * filled entirely. If the track is stopped, obtainBuffer() returns 302 * STOPPED instead of NO_ERROR as long as there are buffers availlable, 303 * at which point NO_MORE_BUFFERS is returned. 304 * Buffers will be returned until the pool (buffercount()) 305 * is exhausted, at which point obtainBuffer() will either block 306 * or return WOULD_BLOCK depending on the value of the "blocking" 307 * parameter. 308 */ 309 310 enum { 311 NO_MORE_BUFFERS = 0x80000001, 312 STOPPED = 1 313 }; 314 315 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); 316 void releaseBuffer(Buffer* audioBuffer); 317 318 319 /* As a convenience we provide a read() interface to the audio buffer. 320 * This is implemented on top of lockBuffer/unlockBuffer. 321 */ 322 ssize_t read(void* buffer, size_t size); 323 324 /* Return the amount of input frames lost in the audio driver since the last call of this function. 325 * Audio driver is expected to reset the value to 0 and restart counting upon returning the current value by this function call. 326 * Such loss typically occurs when the user space process is blocked longer than the capacity of audio driver buffers. 327 * Unit: the number of input audio frames 328 */ 329 unsigned int getInputFramesLost(); 330 331private: 332 /* copying audio tracks is not allowed */ 333 AudioRecord(const AudioRecord& other); 334 AudioRecord& operator = (const AudioRecord& other); 335 336 /* a small internal class to handle the callback */ 337 class ClientRecordThread : public Thread 338 { 339 public: 340 ClientRecordThread(AudioRecord& receiver, bool bCanCallJava = false); 341 private: 342 friend class AudioRecord; 343 virtual bool threadLoop(); 344 virtual status_t readyToRun() { return NO_ERROR; } 345 virtual void onFirstRef() {} 346 AudioRecord& mReceiver; 347 Mutex mLock; 348 }; 349 350 bool processAudioBuffer(const sp<ClientRecordThread>& thread); 351 status_t openRecord_l(uint32_t sampleRate, 352 audio_format_t format, 353 uint32_t channelMask, 354 int frameCount, 355 uint32_t flags, 356 audio_io_handle_t input); 357 audio_io_handle_t getInput_l(); 358 status_t restoreRecord_l(audio_track_cblk_t*& cblk); 359 360 sp<IAudioRecord> mAudioRecord; 361 sp<IMemory> mCblkMemory; 362 sp<ClientRecordThread> mClientRecordThread; 363 Mutex mLock; 364 365 uint32_t mFrameCount; 366 367 audio_track_cblk_t* mCblk; 368 audio_format_t mFormat; 369 uint8_t mChannelCount; 370 uint8_t mInputSource; 371 uint8_t mReserved[2]; 372 status_t mStatus; 373 uint32_t mLatency; 374 375 volatile int32_t mActive; 376 377 callback_t mCbf; 378 void* mUserData; 379 uint32_t mNotificationFrames; 380 uint32_t mRemainingFrames; 381 uint32_t mMarkerPosition; 382 bool mMarkerReached; 383 uint32_t mNewPosition; 384 uint32_t mUpdatePeriod; 385 uint32_t mFlags; 386 uint32_t mChannelMask; 387 audio_io_handle_t mInput; 388 int mSessionId; 389 int mPreviousPriority; // before start() 390 int mPreviousSchedulingGroup; 391}; 392 393}; // namespace android 394 395#endif /*AUDIORECORD_H_*/ 396