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