AudioRecord.h revision e2ffd5b583da9d30d96710b0e8879e90b2b51d30
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 ANDROID_AUDIORECORD_H 18#define ANDROID_AUDIORECORD_H 19 20#include <cutils/sched_policy.h> 21#include <media/AudioSystem.h> 22#include <media/IAudioRecord.h> 23#include <utils/threads.h> 24 25namespace android { 26 27// ---------------------------------------------------------------------------- 28 29class audio_track_cblk_t; 30class AudioRecordClientProxy; 31 32// ---------------------------------------------------------------------------- 33 34class AudioRecord : public RefBase 35{ 36public: 37 38 static const int DEFAULT_SAMPLE_RATE = 8000; 39 40 /* Events used by AudioRecord callback function (callback_t). 41 * Keep in sync with frameworks/base/media/java/android/media/AudioRecord.java NATIVE_EVENT_*. 42 */ 43 enum event_type { 44 EVENT_MORE_DATA = 0, // Request to read more data from PCM buffer. 45 EVENT_OVERRUN = 1, // PCM buffer overrun occurred. 46 EVENT_MARKER = 2, // Record head is at the specified marker position 47 // (See setMarkerPosition()). 48 EVENT_NEW_POS = 3, // Record head is at a new position 49 // (See setPositionUpdatePeriod()). 50 EVENT_NEW_IAUDIORECORD = 4, // IAudioRecord was re-created, either due to re-routing and 51 // voluntary invalidation by mediaserver, or mediaserver crash. 52 }; 53 54 /* Client should declare Buffer on the stack and pass address to obtainBuffer() 55 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA. 56 */ 57 58 class Buffer 59 { 60 public: 61 // FIXME use m prefix 62 size_t frameCount; // number of sample frames corresponding to size; 63 // on input it is the number of frames available, 64 // on output is the number of frames actually drained 65 66 size_t size; // input/output in bytes == frameCount * frameSize 67 // FIXME this is redundant with respect to frameCount, 68 // and TRANSFER_OBTAIN mode is broken for 8-bit data 69 // since we don't define the frame format 70 71 union { 72 void* raw; 73 short* i16; // signed 16-bit 74 int8_t* i8; // unsigned 8-bit, offset by 0x80 75 }; 76 }; 77 78 /* As a convenience, if a callback is supplied, a handler thread 79 * is automatically created with the appropriate priority. This thread 80 * invokes the callback when a new buffer becomes ready or various conditions occur. 81 * Parameters: 82 * 83 * event: type of event notified (see enum AudioRecord::event_type). 84 * user: Pointer to context for use by the callback receiver. 85 * info: Pointer to optional parameter according to event type: 86 * - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read 87 * more bytes than indicated by 'size' field and update 'size' if fewer bytes are 88 * consumed. 89 * - EVENT_OVERRUN: unused. 90 * - EVENT_MARKER: pointer to const uint32_t containing the marker position in frames. 91 * - EVENT_NEW_POS: pointer to const uint32_t containing the new position in frames. 92 * - EVENT_NEW_IAUDIORECORD: unused. 93 */ 94 95 typedef void (*callback_t)(int event, void* user, void *info); 96 97 /* Returns the minimum frame count required for the successful creation of 98 * an AudioRecord object. 99 * Returned status (from utils/Errors.h) can be: 100 * - NO_ERROR: successful operation 101 * - NO_INIT: audio server or audio hardware not initialized 102 * - BAD_VALUE: unsupported configuration 103 */ 104 105 static status_t getMinFrameCount(size_t* frameCount, 106 uint32_t sampleRate, 107 audio_format_t format, 108 audio_channel_mask_t channelMask); 109 110 /* How data is transferred from AudioRecord 111 */ 112 enum transfer_type { 113 TRANSFER_DEFAULT, // not specified explicitly; determine from other parameters 114 TRANSFER_CALLBACK, // callback EVENT_MORE_DATA 115 TRANSFER_OBTAIN, // FIXME deprecated: call obtainBuffer() and releaseBuffer() 116 TRANSFER_SYNC, // synchronous read() 117 }; 118 119 /* Constructs an uninitialized AudioRecord. No connection with 120 * AudioFlinger takes place. Use set() after this. 121 */ 122 AudioRecord(); 123 124 /* Creates an AudioRecord object and registers it with AudioFlinger. 125 * Once created, the track needs to be started before it can be used. 126 * Unspecified values are set to appropriate default values. 127 * 128 * Parameters: 129 * 130 * inputSource: Select the audio input to record from (e.g. AUDIO_SOURCE_DEFAULT). 131 * sampleRate: Data sink 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. 135 * frameCount: Minimum size of track PCM buffer in frames. This defines the 136 * application's contribution to the 137 * latency of the track. The actual size selected by the AudioRecord could 138 * be larger if the requested size is not compatible with current audio HAL 139 * latency. Zero means to use a default value. 140 * cbf: Callback function. If not null, this function is called periodically 141 * to consume new PCM data and inform of marker, position updates, etc. 142 * user: Context for use by the callback receiver. 143 * notificationFrames: The callback function is called each time notificationFrames PCM 144 * frames are ready in record track output buffer. 145 * sessionId: Not yet supported. 146 * transferType: How data is transferred from AudioRecord. 147 * threadCanCallJava: Not present in parameter list, and so is fixed at false. 148 */ 149 150 AudioRecord(audio_source_t inputSource, 151 uint32_t sampleRate = 0, 152 audio_format_t format = AUDIO_FORMAT_DEFAULT, 153 audio_channel_mask_t channelMask = AUDIO_CHANNEL_IN_MONO, 154 int frameCount = 0, 155 callback_t cbf = NULL, 156 void* user = NULL, 157 int notificationFrames = 0, 158 int sessionId = 0, 159 transfer_type transferType = TRANSFER_DEFAULT); 160 161 /* Terminates the AudioRecord and unregisters it from AudioFlinger. 162 * Also destroys all resources associated with the AudioRecord. 163 */ 164protected: 165 virtual ~AudioRecord(); 166public: 167 168 /* Initialize an AudioRecord that was created using the AudioRecord() constructor. 169 * Don't call set() more than once, or after an AudioRecord() constructor that takes parameters. 170 * Returned status (from utils/Errors.h) can be: 171 * - NO_ERROR: successful intialization 172 * - INVALID_OPERATION: AudioRecord is already initialized or record device is already in use 173 * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) 174 * - NO_INIT: audio server or audio hardware not initialized 175 * - PERMISSION_DENIED: recording is not allowed for the requesting process 176 * 177 * Parameters not listed in the AudioRecord constructors above: 178 * 179 * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI. 180 */ 181 status_t set(audio_source_t inputSource = AUDIO_SOURCE_DEFAULT, 182 uint32_t sampleRate = 0, 183 audio_format_t format = AUDIO_FORMAT_DEFAULT, 184 audio_channel_mask_t channelMask = AUDIO_CHANNEL_IN_MONO, 185 int frameCount = 0, 186 callback_t cbf = NULL, 187 void* user = NULL, 188 int notificationFrames = 0, 189 bool threadCanCallJava = false, 190 int sessionId = 0, 191 transfer_type transferType = TRANSFER_DEFAULT); 192 193 /* Result of constructing the AudioRecord. This must be checked 194 * before using any AudioRecord API (except for set()), because using 195 * an uninitialized AudioRecord produces undefined results. 196 * See set() method above for possible return codes. 197 */ 198 status_t initCheck() const { return mStatus; } 199 200 /* Returns this track's estimated latency in milliseconds. 201 * This includes the latency due to AudioRecord buffer size, 202 * and audio hardware driver. 203 */ 204 uint32_t latency() const { return mLatency; } 205 206 /* getters, see constructor and set() */ 207 208 audio_format_t format() const { return mFormat; } 209 uint32_t channelCount() const { return mChannelCount; } 210 size_t frameCount() const { return mFrameCount; } 211 size_t frameSize() const { return mFrameSize; } 212 audio_source_t inputSource() const { return mInputSource; } 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 * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until 217 * the specified event occurs on the specified trigger session. 218 */ 219 status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE, 220 int triggerSession = 0); 221 222 /* Stop a track. If set, the callback will cease being called. Note that obtainBuffer() still 223 * works and will drain buffers until the pool is exhausted, and then will return WOULD_BLOCK. 224 */ 225 void stop(); 226 bool stopped() const; 227 228 /* Return the sink sample rate for this record track in Hz. 229 * Unlike AudioTrack, the sample rate is const after initialization, so doesn't need a lock. 230 */ 231 uint32_t getSampleRate() const { return mSampleRate; } 232 233 /* Sets marker position. When record reaches the number of frames specified, 234 * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition 235 * with marker == 0 cancels marker notification callback. 236 * To set a marker at a position which would compute as 0, 237 * a workaround is to the set the marker at a nearby position such as ~0 or 1. 238 * If the AudioRecord has been opened with no callback function associated, 239 * the operation will fail. 240 * 241 * Parameters: 242 * 243 * marker: marker position expressed in wrapping (overflow) frame units, 244 * like the return value of getPosition(). 245 * 246 * Returned status (from utils/Errors.h) can be: 247 * - NO_ERROR: successful operation 248 * - INVALID_OPERATION: the AudioRecord has no callback installed. 249 */ 250 status_t setMarkerPosition(uint32_t marker); 251 status_t getMarkerPosition(uint32_t *marker) const; 252 253 /* Sets position update period. Every time the number of frames specified has been recorded, 254 * a callback with event type EVENT_NEW_POS is called. 255 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification 256 * callback. 257 * If the AudioRecord has been opened with no callback function associated, 258 * the operation will fail. 259 * Extremely small values may be rounded up to a value the implementation can support. 260 * 261 * Parameters: 262 * 263 * updatePeriod: position update notification period expressed in frames. 264 * 265 * Returned status (from utils/Errors.h) can be: 266 * - NO_ERROR: successful operation 267 * - INVALID_OPERATION: the AudioRecord has no callback installed. 268 */ 269 status_t setPositionUpdatePeriod(uint32_t updatePeriod); 270 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; 271 272 /* Return the total number of frames recorded since recording started. 273 * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz. 274 * It is reset to zero by stop(). 275 * 276 * Parameters: 277 * 278 * position: Address where to return record head position. 279 * 280 * Returned status (from utils/Errors.h) can be: 281 * - NO_ERROR: successful operation 282 * - BAD_VALUE: position is NULL 283 */ 284 status_t getPosition(uint32_t *position) const; 285 286 /* Returns a handle on the audio input used by this AudioRecord. 287 * 288 * Parameters: 289 * none. 290 * 291 * Returned value: 292 * handle on audio hardware input 293 */ 294 audio_io_handle_t getInput() const; 295 296 /* Returns the audio session ID associated with this AudioRecord. 297 * 298 * Parameters: 299 * none. 300 * 301 * Returned value: 302 * AudioRecord session ID. 303 * 304 * No lock needed because session ID doesn't change after first set(). 305 */ 306 int getSessionId() const { return mSessionId; } 307 308 /* Obtains a buffer of up to "audioBuffer->frameCount" full frames. 309 * After draining these frames of data, the caller should release them with releaseBuffer(). 310 * If the track buffer is not empty, obtainBuffer() returns as many contiguous 311 * full frames as are available immediately. 312 * If the track buffer is empty and track is stopped, obtainBuffer() returns WOULD_BLOCK 313 * regardless of the value of waitCount. 314 * If the track buffer is empty and track is not stopped, obtainBuffer() blocks with a 315 * maximum timeout based on waitCount; see chart below. 316 * Buffers will be returned until the pool 317 * is exhausted, at which point obtainBuffer() will either block 318 * or return WOULD_BLOCK depending on the value of the "waitCount" 319 * parameter. 320 * 321 * obtainBuffer() and releaseBuffer() are deprecated for direct use by applications, 322 * which should use read() or callback EVENT_MORE_DATA instead. 323 * 324 * Interpretation of waitCount: 325 * +n limits wait time to n * WAIT_PERIOD_MS, 326 * -1 causes an (almost) infinite wait time, 327 * 0 non-blocking. 328 * 329 * Buffer fields 330 * On entry: 331 * frameCount number of frames requested 332 * After error return: 333 * frameCount 0 334 * size 0 335 * raw undefined 336 * After successful return: 337 * frameCount actual number of frames available, <= number requested 338 * size actual number of bytes available 339 * raw pointer to the buffer 340 */ 341 342 /* FIXME Deprecated public API for TRANSFER_OBTAIN mode */ 343 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount) 344 __attribute__((__deprecated__)); 345 346private: 347 /* New internal API. 348 * If nonContig is non-NULL, it is an output parameter that will be set to the number of 349 * additional non-contiguous frames that are available immediately. 350 * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(), 351 * in case the requested amount of frames is in two or more non-contiguous regions. 352 * FIXME requested and elapsed are both relative times. Consider changing to absolute time. 353 */ 354 status_t obtainBuffer(Buffer* audioBuffer, const struct timespec *requested, 355 struct timespec *elapsed = NULL, size_t *nonContig = NULL); 356public: 357 358 /* Release an emptied buffer of "audioBuffer->frameCount" frames for AudioFlinger to re-fill. */ 359 // FIXME make private when obtainBuffer() for TRANSFER_OBTAIN is removed 360 void releaseBuffer(Buffer* audioBuffer); 361 362 /* As a convenience we provide a read() interface to the audio buffer. 363 * Input parameter 'size' is in byte units. 364 * This is implemented on top of obtainBuffer/releaseBuffer. For best 365 * performance use callbacks. Returns actual number of bytes read >= 0, 366 * or a negative status code. 367 */ 368 ssize_t read(void* buffer, size_t size); 369 370 /* Return the number of input frames lost in the audio driver since the last call of this 371 * function. Audio driver is expected to reset the value to 0 and restart counting upon 372 * returning the current value by this function call. Such loss typically occurs when the 373 * user space process is blocked longer than the capacity of audio driver buffers. 374 * Units: the number of input audio frames. 375 */ 376 unsigned int getInputFramesLost() const; 377 378private: 379 /* copying audio record objects is not allowed */ 380 AudioRecord(const AudioRecord& other); 381 AudioRecord& operator = (const AudioRecord& other); 382 383 /* a small internal class to handle the callback */ 384 class AudioRecordThread : public Thread 385 { 386 public: 387 AudioRecordThread(AudioRecord& receiver, bool bCanCallJava = false); 388 389 // Do not call Thread::requestExitAndWait() without first calling requestExit(). 390 // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough. 391 virtual void requestExit(); 392 393 void pause(); // suspend thread from execution at next loop boundary 394 void resume(); // allow thread to execute, if not requested to exit 395 void pauseConditional(); 396 // like pause(), but only if prior resume() wasn't latched 397 398 private: 399 friend class AudioRecord; 400 virtual bool threadLoop(); 401 AudioRecord& mReceiver; 402 virtual ~AudioRecordThread(); 403 Mutex mMyLock; // Thread::mLock is private 404 Condition mMyCond; // Thread::mThreadExitedCondition is private 405 bool mPaused; // whether thread is currently paused 406 bool mResumeLatch; // whether next pauseConditional() will be a nop 407 }; 408 409 // body of AudioRecordThread::threadLoop() 410 // returns the maximum amount of time before we would like to run again, where: 411 // 0 immediately 412 // > 0 no later than this many nanoseconds from now 413 // NS_WHENEVER still active but no particular deadline 414 // NS_INACTIVE inactive so don't run again until re-started 415 // NS_NEVER never again 416 static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3; 417 nsecs_t processAudioBuffer(const sp<AudioRecordThread>& thread); 418 419 // caller must hold lock on mLock for all _l methods 420 status_t openRecord_l(uint32_t sampleRate, 421 audio_format_t format, 422 size_t frameCount, 423 audio_io_handle_t input, 424 size_t epoch); 425 426 audio_io_handle_t getInput_l(); 427 428 // FIXME enum is faster than strcmp() for parameter 'from' 429 status_t restoreRecord_l(const char *from); 430 431 sp<AudioRecordThread> mAudioRecordThread; 432 mutable Mutex mLock; 433 434 // Current client state: false = stopped, true = active. Protected by mLock. If more states 435 // are added, consider changing this to enum State { ... } mState as in AudioTrack. 436 bool mActive; 437 438 // for client callback handler 439 callback_t mCbf; // callback handler for events, or NULL 440 void* mUserData; // for client callback handler 441 442 // for notification APIs 443 uint32_t mNotificationFrames; // frames between each notification callback 444 bool mRefreshRemaining; // processAudioBuffer() should refresh next 2 445 446 // These are private to processAudioBuffer(), and are not protected by a lock 447 uint32_t mRemainingFrames; // number of frames to request in obtainBuffer() 448 bool mRetryOnPartialBuffer; // sleep and retry after partial obtainBuffer() 449 int mObservedSequence; // last observed value of mSequence 450 451 uint32_t mMarkerPosition; // in wrapping (overflow) frame units 452 bool mMarkerReached; 453 uint32_t mNewPosition; // in frames 454 uint32_t mUpdatePeriod; // in frames, zero means no EVENT_NEW_POS 455 456 status_t mStatus; 457 458 // constant after constructor or set() 459 uint32_t mSampleRate; 460 size_t mFrameCount; 461 audio_format_t mFormat; 462 uint32_t mChannelCount; 463 size_t mFrameSize; // app-level frame size == AudioFlinger frame size 464 audio_source_t mInputSource; 465 uint32_t mLatency; // in ms 466 audio_channel_mask_t mChannelMask; 467 int mSessionId; 468 transfer_type mTransfer; 469 470 audio_io_handle_t mInput; // returned by AudioSystem::getInput() 471 472 // may be changed if IAudioRecord object is re-created 473 sp<IAudioRecord> mAudioRecord; 474 sp<IMemory> mCblkMemory; 475 audio_track_cblk_t* mCblk; // re-load after mLock.unlock() 476 477 int mPreviousPriority; // before start() 478 SchedPolicy mPreviousSchedulingGroup; 479 480 // The proxy should only be referenced while a lock is held because the proxy isn't 481 // multi-thread safe. 482 // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock, 483 // provided that the caller also holds an extra reference to the proxy and shared memory to keep 484 sp<AudioRecordClientProxy> mProxy; 485 486 bool mInOverrun; // whether recorder is currently in overrun state 487 488private: 489 class DeathNotifier : public IBinder::DeathRecipient { 490 public: 491 DeathNotifier(AudioRecord* audioRecord) : mAudioRecord(audioRecord) { } 492 protected: 493 virtual void binderDied(const wp<IBinder>& who); 494 private: 495 const wp<AudioRecord> mAudioRecord; 496 }; 497 498 sp<DeathNotifier> mDeathNotifier; 499 uint32_t mSequence; // incremented for each new IAudioRecord attempt 500}; 501 502}; // namespace android 503 504#endif // ANDROID_AUDIORECORD_H 505