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