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