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