AudioRecord.h revision a636433cbd09c0708b85f337ef45c0cdef3bcb4d
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 <cutils/sched_policy.h> 31#include <utils/threads.h> 32 33#include <system/audio.h> 34#include <media/AudioSystem.h> 35 36namespace android { 37 38class audio_track_cblk_t; 39 40// ---------------------------------------------------------------------------- 41 42class AudioRecord : virtual public RefBase 43{ 44public: 45 46 static const int DEFAULT_SAMPLE_RATE = 8000; 47 48 /* Events used by AudioRecord callback function (callback_t). 49 * 50 * to keep in sync with frameworks/base/media/java/android/media/AudioRecord.java 51 */ 52 enum event_type { 53 EVENT_MORE_DATA = 0, // Request to reqd more data from PCM buffer. 54 EVENT_OVERRUN = 1, // PCM buffer overrun occured. 55 EVENT_MARKER = 2, // Record head is at the specified marker position 56 // (See setMarkerPosition()). 57 EVENT_NEW_POS = 3, // Record head is at a new position 58 // (See setPositionUpdatePeriod()). 59 }; 60 61 /* Create Buffer on the stack and pass it to obtainBuffer() 62 * and releaseBuffer(). 63 */ 64 65 class Buffer 66 { 67 public: 68 enum { 69 MUTE = 0x00000001 70 }; 71 uint32_t flags; 72 int channelCount; 73 audio_format_t format; 74 size_t frameCount; 75 size_t size; 76 union { 77 void* raw; 78 short* i16; 79 int8_t* i8; 80 }; 81 }; 82 83 /* These are static methods to control the system-wide AudioFlinger 84 * only privileged processes can have access to them 85 */ 86 87// static status_t setMasterMute(bool mute); 88 89 /* As a convenience, if a callback is supplied, a handler thread 90 * is automatically created with the appropriate priority. This thread 91 * invokes the callback when a new buffer becomes ready or an overrun condition occurs. 92 * Parameters: 93 * 94 * event: type of event notified (see enum AudioRecord::event_type). 95 * user: Pointer to context for use by the callback receiver. 96 * info: Pointer to optional parameter according to event type: 97 * - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read 98 * more bytes than indicated by 'size' field and update 'size' if less bytes are 99 * read. 100 * - EVENT_OVERRUN: unused. 101 * - EVENT_MARKER: pointer to an uin32_t containing the marker position in frames. 102 * - EVENT_NEW_POS: pointer to an uin32_t containing the new position in frames. 103 */ 104 105 typedef void (*callback_t)(int event, void* user, void *info); 106 107 /* Returns the minimum frame count required for the successful creation of 108 * an AudioRecord object. 109 * Returned status (from utils/Errors.h) can be: 110 * - NO_ERROR: successful operation 111 * - NO_INIT: audio server or audio hardware not initialized 112 * - BAD_VALUE: unsupported configuration 113 */ 114 115 static status_t getMinFrameCount(int* frameCount, 116 uint32_t sampleRate, 117 audio_format_t format, 118 int channelCount); 119 120 /* Constructs an uninitialized AudioRecord. No connection with 121 * AudioFlinger takes place. 122 */ 123 AudioRecord(); 124 125 /* Creates an AudioRecord track and registers it with AudioFlinger. 126 * Once created, the track needs to be started before it can be used. 127 * Unspecified values are set to the audio hardware's current 128 * values. 129 * 130 * Parameters: 131 * 132 * inputSource: Select the audio input to record to (e.g. AUDIO_SOURCE_DEFAULT). 133 * sampleRate: Track sampling rate in Hz. 134 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed 135 * 16 bits per sample). 136 * channelMask: Channel mask: see audio_channels_t. 137 * frameCount: Total size of track PCM buffer in frames. This defines the 138 * latency of the track. 139 * flags: A bitmask of acoustic values from enum record_flags. It enables 140 * AGC, NS, and IIR. 141 * cbf: Callback function. If not null, this function is called periodically 142 * to provide new PCM data. 143 * notificationFrames: The callback function is called each time notificationFrames PCM 144 * frames are ready in record track output buffer. 145 * user Context for use by the callback receiver. 146 */ 147 148 // FIXME consider removing this alias and replacing it by audio_in_acoustics_t 149 // or removing the parameter entirely if it is unused 150 enum record_flags { 151 RECORD_AGC_ENABLE = AUDIO_IN_ACOUSTICS_AGC_ENABLE, 152 RECORD_NS_ENABLE = AUDIO_IN_ACOUSTICS_NS_ENABLE, 153 RECORD_IIR_ENABLE = AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE, 154 }; 155 156 AudioRecord(audio_source_t inputSource, 157 uint32_t sampleRate = 0, 158 audio_format_t format = AUDIO_FORMAT_DEFAULT, 159 uint32_t channelMask = AUDIO_CHANNEL_IN_MONO, 160 int frameCount = 0, 161 record_flags flags = (record_flags) 0, 162 callback_t cbf = NULL, 163 void* user = NULL, 164 int notificationFrames = 0, 165 int sessionId = 0); 166 167 168 /* Terminates the AudioRecord and unregisters it from AudioFlinger. 169 * Also destroys all resources assotiated with the AudioRecord. 170 */ 171 ~AudioRecord(); 172 173 174 /* Initialize an uninitialized AudioRecord. 175 * Returned status (from utils/Errors.h) can be: 176 * - NO_ERROR: successful intialization 177 * - INVALID_OPERATION: AudioRecord is already intitialized or record device is already in use 178 * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) 179 * - NO_INIT: audio server or audio hardware not initialized 180 * - PERMISSION_DENIED: recording is not allowed for the requesting process 181 * */ 182 status_t set(audio_source_t inputSource = AUDIO_SOURCE_DEFAULT, 183 uint32_t sampleRate = 0, 184 audio_format_t format = AUDIO_FORMAT_DEFAULT, 185 uint32_t channelMask = AUDIO_CHANNEL_IN_MONO, 186 int frameCount = 0, 187 record_flags flags = (record_flags) 0, 188 callback_t cbf = NULL, 189 void* user = NULL, 190 int notificationFrames = 0, 191 bool threadCanCallJava = false, 192 int sessionId = 0); 193 194 195 /* Result of constructing the AudioRecord. This must be checked 196 * before using any AudioRecord API (except for set()), using 197 * an uninitialized AudioRecord produces undefined results. 198 * See set() method above for possible return codes. 199 */ 200 status_t initCheck() const; 201 202 /* Returns this track's latency in milliseconds. 203 * This includes the latency due to AudioRecord buffer size 204 * and audio hardware driver. 205 */ 206 uint32_t latency() const; 207 208 /* getters, see constructor */ 209 210 audio_format_t format() const; 211 int channelCount() const; 212 int channels() const; 213 uint32_t frameCount() const; 214 size_t frameSize() const; 215 audio_source_t inputSource() const; 216 217 218 /* After it's created the track is not active. Call start() to 219 * make it active. If set, the callback will start being called. 220 * if event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until 221 * the specified event occurs on the specified trigger session. 222 */ 223 status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE, 224 int triggerSession = 0); 225 226 /* Stop a track. If set, the callback will cease being called and 227 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works 228 * and will fill up buffers until the pool is exhausted. 229 */ 230 status_t stop(); 231 bool stopped() const; 232 233 /* get sample rate for this record track 234 */ 235 uint32_t getSampleRate() const; 236 237 /* Sets marker position. When record reaches the number of frames specified, 238 * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition 239 * with marker == 0 cancels marker notification callback. 240 * If the AudioRecord has been opened with no callback function associated, 241 * the operation will fail. 242 * 243 * Parameters: 244 * 245 * marker: marker position expressed in frames. 246 * 247 * Returned status (from utils/Errors.h) can be: 248 * - NO_ERROR: successful operation 249 * - INVALID_OPERATION: the AudioRecord has no callback installed. 250 */ 251 status_t setMarkerPosition(uint32_t marker); 252 status_t getMarkerPosition(uint32_t *marker) const; 253 254 255 /* Sets position update period. Every time the number of frames specified has been recorded, 256 * a callback with event type EVENT_NEW_POS is called. 257 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification 258 * callback. 259 * If the AudioRecord has been opened with no callback function associated, 260 * the operation will fail. 261 * 262 * Parameters: 263 * 264 * updatePeriod: position update notification period expressed in frames. 265 * 266 * Returned status (from utils/Errors.h) can be: 267 * - NO_ERROR: successful operation 268 * - INVALID_OPERATION: the AudioRecord has no callback installed. 269 */ 270 status_t setPositionUpdatePeriod(uint32_t updatePeriod); 271 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; 272 273 274 /* Gets record head position. The position is the total number of frames 275 * recorded since record start. 276 * 277 * Parameters: 278 * 279 * position: Address where to return record head position within AudioRecord buffer. 280 * 281 * Returned status (from utils/Errors.h) can be: 282 * - NO_ERROR: successful operation 283 * - BAD_VALUE: position is NULL 284 */ 285 status_t getPosition(uint32_t *position) const; 286 287 /* returns a handle on the audio input used by this AudioRecord. 288 * 289 * Parameters: 290 * none. 291 * 292 * Returned value: 293 * handle on audio hardware input 294 */ 295 audio_io_handle_t getInput() const; 296 297 /* returns the audio session ID associated to this AudioRecord. 298 * 299 * Parameters: 300 * none. 301 * 302 * Returned value: 303 * AudioRecord session ID. 304 */ 305 int getSessionId() const; 306 307 /* obtains a buffer of "frameCount" frames. The buffer must be 308 * filled entirely. If the track is stopped, obtainBuffer() returns 309 * STOPPED instead of NO_ERROR as long as there are buffers available, 310 * at which point NO_MORE_BUFFERS is returned. 311 * Buffers will be returned until the pool (buffercount()) 312 * is exhausted, at which point obtainBuffer() will either block 313 * or return WOULD_BLOCK depending on the value of the "blocking" 314 * parameter. 315 */ 316 317 enum { 318 NO_MORE_BUFFERS = 0x80000001, 319 STOPPED = 1 320 }; 321 322 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); 323 void releaseBuffer(Buffer* audioBuffer); 324 325 326 /* As a convenience we provide a read() interface to the audio buffer. 327 * This is implemented on top of obtainBuffer/releaseBuffer. 328 */ 329 ssize_t read(void* buffer, size_t size); 330 331 /* Return the amount of input frames lost in the audio driver since the last call of this 332 * function. Audio driver is expected to reset the value to 0 and restart counting upon 333 * returning the current value by this function call. Such loss typically occurs when the 334 * user space process is blocked longer than the capacity of audio driver buffers. 335 * Unit: the number of input audio frames 336 */ 337 unsigned int getInputFramesLost() const; 338 339private: 340 /* copying audio tracks is not allowed */ 341 AudioRecord(const AudioRecord& other); 342 AudioRecord& operator = (const AudioRecord& other); 343 344 /* a small internal class to handle the callback */ 345 class ClientRecordThread : public Thread 346 { 347 public: 348 ClientRecordThread(AudioRecord& receiver, bool bCanCallJava = false); 349 private: 350 friend class AudioRecord; 351 virtual bool threadLoop(); 352 virtual status_t readyToRun(); 353 virtual void onFirstRef() {} 354 AudioRecord& mReceiver; 355 }; 356 357 bool processAudioBuffer(const sp<ClientRecordThread>& thread); 358 status_t openRecord_l(uint32_t sampleRate, 359 audio_format_t format, 360 uint32_t channelMask, 361 int frameCount, 362 audio_io_handle_t input); 363 audio_io_handle_t getInput_l(); 364 status_t restoreRecord_l(audio_track_cblk_t*& cblk); 365 366 sp<IAudioRecord> mAudioRecord; 367 sp<IMemory> mCblkMemory; 368 sp<ClientRecordThread> mClientRecordThread; 369 status_t mReadyToRun; 370 mutable Mutex mLock; 371 Condition mCondition; 372 373 uint32_t mFrameCount; 374 375 audio_track_cblk_t* mCblk; 376 audio_format_t mFormat; 377 uint8_t mChannelCount; 378 audio_source_t mInputSource; 379 status_t mStatus; 380 uint32_t mLatency; 381 382 volatile int32_t mActive; 383 384 callback_t mCbf; 385 void* mUserData; 386 uint32_t mNotificationFrames; 387 uint32_t mRemainingFrames; 388 uint32_t mMarkerPosition; 389 bool mMarkerReached; 390 uint32_t mNewPosition; 391 uint32_t mUpdatePeriod; 392 record_flags mFlags; 393 uint32_t mChannelMask; 394 audio_io_handle_t mInput; 395 int mSessionId; 396 int mPreviousPriority; // before start() 397 SchedPolicy mPreviousSchedulingGroup; 398}; 399 400}; // namespace android 401 402#endif /*AUDIORECORD_H_*/ 403