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 <memory> 21 #include <vector> 22 23 #include <binder/IMemory.h> 24 #include <cutils/sched_policy.h> 25 #include <media/AudioSystem.h> 26 #include <media/AudioTimestamp.h> 27 #include <media/MediaAnalyticsItem.h> 28 #include <media/Modulo.h> 29 #include <media/MicrophoneInfo.h> 30 #include <media/RecordingActivityTracker.h> 31 #include <utils/RefBase.h> 32 #include <utils/threads.h> 33 34 #include "android/media/IAudioRecord.h" 35 36 namespace android { 37 38 // ---------------------------------------------------------------------------- 39 40 struct audio_track_cblk_t; 41 class AudioRecordClientProxy; 42 43 // ---------------------------------------------------------------------------- 44 45 class AudioRecord : public AudioSystem::AudioDeviceCallback 46 { 47 public: 48 49 /* Events used by AudioRecord callback function (callback_t). 50 * Keep in sync with frameworks/base/media/java/android/media/AudioRecord.java NATIVE_EVENT_*. 51 */ 52 enum event_type { 53 EVENT_MORE_DATA = 0, // Request to read available data from buffer. 54 // If this event is delivered but the callback handler 55 // does not want to read the available data, the handler must 56 // explicitly ignore the event by setting frameCount to zero. 57 EVENT_OVERRUN = 1, // Buffer overrun occurred. 58 EVENT_MARKER = 2, // Record head is at the specified marker position 59 // (See setMarkerPosition()). 60 EVENT_NEW_POS = 3, // Record head is at a new position 61 // (See setPositionUpdatePeriod()). 62 EVENT_NEW_IAUDIORECORD = 4, // IAudioRecord was re-created, either due to re-routing and 63 // voluntary invalidation by mediaserver, or mediaserver crash. 64 }; 65 66 /* Client should declare a Buffer and pass address to obtainBuffer() 67 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA. 68 */ 69 70 class Buffer 71 { 72 public: 73 // FIXME use m prefix 74 size_t frameCount; // number of sample frames corresponding to size; 75 // on input to obtainBuffer() it is the number of frames desired 76 // on output from obtainBuffer() it is the number of available 77 // frames to be read 78 // on input to releaseBuffer() it is currently ignored 79 80 size_t size; // input/output in bytes == frameCount * frameSize 81 // on input to obtainBuffer() it is ignored 82 // on output from obtainBuffer() it is the number of available 83 // bytes to be read, which is frameCount * frameSize 84 // on input to releaseBuffer() it is the number of bytes to 85 // release 86 // FIXME This is redundant with respect to frameCount. Consider 87 // removing size and making frameCount the primary field. 88 89 union { 90 void* raw; 91 int16_t* i16; // signed 16-bit 92 int8_t* i8; // unsigned 8-bit, offset by 0x80 93 // input to obtainBuffer(): unused, output: pointer to buffer 94 }; 95 96 uint32_t sequence; // IAudioRecord instance sequence number, as of obtainBuffer(). 97 // It is set by obtainBuffer() and confirmed by releaseBuffer(). 98 // Not "user-serviceable". 99 // TODO Consider sp<IMemory> instead, or in addition to this. 100 }; 101 102 /* As a convenience, if a callback is supplied, a handler thread 103 * is automatically created with the appropriate priority. This thread 104 * invokes the callback when a new buffer becomes available or various conditions occur. 105 * Parameters: 106 * 107 * event: type of event notified (see enum AudioRecord::event_type). 108 * user: Pointer to context for use by the callback receiver. 109 * info: Pointer to optional parameter according to event type: 110 * - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read 111 * more bytes than indicated by 'size' field and update 'size' if 112 * fewer bytes are consumed. 113 * - EVENT_OVERRUN: unused. 114 * - EVENT_MARKER: pointer to const uint32_t containing the marker position in frames. 115 * - EVENT_NEW_POS: pointer to const uint32_t containing the new position in frames. 116 * - EVENT_NEW_IAUDIORECORD: unused. 117 */ 118 119 typedef void (*callback_t)(int event, void* user, void *info); 120 121 /* Returns the minimum frame count required for the successful creation of 122 * an AudioRecord object. 123 * Returned status (from utils/Errors.h) can be: 124 * - NO_ERROR: successful operation 125 * - NO_INIT: audio server or audio hardware not initialized 126 * - BAD_VALUE: unsupported configuration 127 * frameCount is guaranteed to be non-zero if status is NO_ERROR, 128 * and is undefined otherwise. 129 * FIXME This API assumes a route, and so should be deprecated. 130 */ 131 132 static status_t getMinFrameCount(size_t* frameCount, 133 uint32_t sampleRate, 134 audio_format_t format, 135 audio_channel_mask_t channelMask); 136 137 /* How data is transferred from AudioRecord 138 */ 139 enum transfer_type { 140 TRANSFER_DEFAULT, // not specified explicitly; determine from the other parameters 141 TRANSFER_CALLBACK, // callback EVENT_MORE_DATA 142 TRANSFER_OBTAIN, // call obtainBuffer() and releaseBuffer() 143 TRANSFER_SYNC, // synchronous read() 144 }; 145 146 /* Constructs an uninitialized AudioRecord. No connection with 147 * AudioFlinger takes place. Use set() after this. 148 * 149 * Parameters: 150 * 151 * opPackageName: The package name used for app ops. 152 */ 153 AudioRecord(const String16& opPackageName); 154 155 /* Creates an AudioRecord object and registers it with AudioFlinger. 156 * Once created, the track needs to be started before it can be used. 157 * Unspecified values are set to appropriate default values. 158 * 159 * Parameters: 160 * 161 * inputSource: Select the audio input to record from (e.g. AUDIO_SOURCE_DEFAULT). 162 * sampleRate: Data sink sampling rate in Hz. Zero means to use the source sample rate. 163 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed 164 * 16 bits per sample). 165 * channelMask: Channel mask, such that audio_is_input_channel(channelMask) is true. 166 * opPackageName: The package name used for app ops. 167 * frameCount: Minimum size of track PCM buffer in frames. This defines the 168 * application's contribution to the 169 * latency of the track. The actual size selected by the AudioRecord could 170 * be larger if the requested size is not compatible with current audio HAL 171 * latency. Zero means to use a default value. 172 * cbf: Callback function. If not null, this function is called periodically 173 * to consume new data in TRANSFER_CALLBACK mode 174 * and inform of marker, position updates, etc. 175 * user: Context for use by the callback receiver. 176 * notificationFrames: The callback function is called each time notificationFrames PCM 177 * frames are ready in record track output buffer. 178 * sessionId: Not yet supported. 179 * transferType: How data is transferred from AudioRecord. 180 * flags: See comments on audio_input_flags_t in <system/audio.h> 181 * pAttributes: If not NULL, supersedes inputSource for use case selection. 182 * threadCanCallJava: Not present in parameter list, and so is fixed at false. 183 */ 184 185 AudioRecord(audio_source_t inputSource, 186 uint32_t sampleRate, 187 audio_format_t format, 188 audio_channel_mask_t channelMask, 189 const String16& opPackageName, 190 size_t frameCount = 0, 191 callback_t cbf = NULL, 192 void* user = NULL, 193 uint32_t notificationFrames = 0, 194 audio_session_t sessionId = AUDIO_SESSION_ALLOCATE, 195 transfer_type transferType = TRANSFER_DEFAULT, 196 audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, 197 uid_t uid = AUDIO_UID_INVALID, 198 pid_t pid = -1, 199 const audio_attributes_t* pAttributes = NULL, 200 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE, 201 audio_microphone_direction_t 202 selectedMicDirection = MIC_DIRECTION_UNSPECIFIED, 203 float selectedMicFieldDimension = MIC_FIELD_DIMENSION_DEFAULT); 204 205 /* Terminates the AudioRecord and unregisters it from AudioFlinger. 206 * Also destroys all resources associated with the AudioRecord. 207 */ 208 protected: 209 virtual ~AudioRecord(); 210 public: 211 212 /* Initialize an AudioRecord that was created using the AudioRecord() constructor. 213 * Don't call set() more than once, or after an AudioRecord() constructor that takes parameters. 214 * set() is not multi-thread safe. 215 * Returned status (from utils/Errors.h) can be: 216 * - NO_ERROR: successful intialization 217 * - INVALID_OPERATION: AudioRecord is already initialized or record device is already in use 218 * - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...) 219 * - NO_INIT: audio server or audio hardware not initialized 220 * - PERMISSION_DENIED: recording is not allowed for the requesting process 221 * If status is not equal to NO_ERROR, don't call any other APIs on this AudioRecord. 222 * 223 * Parameters not listed in the AudioRecord constructors above: 224 * 225 * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI. 226 */ 227 status_t set(audio_source_t inputSource, 228 uint32_t sampleRate, 229 audio_format_t format, 230 audio_channel_mask_t channelMask, 231 size_t frameCount = 0, 232 callback_t cbf = NULL, 233 void* user = NULL, 234 uint32_t notificationFrames = 0, 235 bool threadCanCallJava = false, 236 audio_session_t sessionId = AUDIO_SESSION_ALLOCATE, 237 transfer_type transferType = TRANSFER_DEFAULT, 238 audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, 239 uid_t uid = AUDIO_UID_INVALID, 240 pid_t pid = -1, 241 const audio_attributes_t* pAttributes = NULL, 242 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE, 243 audio_microphone_direction_t 244 selectedMicDirection = MIC_DIRECTION_UNSPECIFIED, 245 float selectedMicFieldDimension = MIC_FIELD_DIMENSION_DEFAULT); 246 247 /* Result of constructing the AudioRecord. This must be checked for successful initialization 248 * before using any AudioRecord API (except for set()), because using 249 * an uninitialized AudioRecord produces undefined results. 250 * See set() method above for possible return codes. 251 */ initCheck()252 status_t initCheck() const { return mStatus; } 253 254 /* Returns this track's estimated latency in milliseconds. 255 * This includes the latency due to AudioRecord buffer size, resampling if applicable, 256 * and audio hardware driver. 257 */ latency()258 uint32_t latency() const { return mLatency; } 259 260 /* getters, see constructor and set() */ 261 format()262 audio_format_t format() const { return mFormat; } channelCount()263 uint32_t channelCount() const { return mChannelCount; } frameCount()264 size_t frameCount() const { return mFrameCount; } frameSize()265 size_t frameSize() const { return mFrameSize; } inputSource()266 audio_source_t inputSource() const { return mAttributes.source; } 267 268 /* 269 * Return the period of the notification callback in frames. 270 * This value is set when the AudioRecord is constructed. 271 * It can be modified if the AudioRecord is rerouted. 272 */ getNotificationPeriodInFrames()273 uint32_t getNotificationPeriodInFrames() const { return mNotificationFramesAct; } 274 275 /* 276 * return metrics information for the current instance. 277 */ 278 status_t getMetrics(MediaAnalyticsItem * &item); 279 280 /* After it's created the track is not active. Call start() to 281 * make it active. If set, the callback will start being called. 282 * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until 283 * the specified event occurs on the specified trigger session. 284 */ 285 status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE, 286 audio_session_t triggerSession = AUDIO_SESSION_NONE); 287 288 /* Stop a track. The callback will cease being called. Note that obtainBuffer() still 289 * works and will drain buffers until the pool is exhausted, and then will return WOULD_BLOCK. 290 */ 291 void stop(); 292 bool stopped() const; 293 294 /* Return the sink sample rate for this record track in Hz. 295 * If specified as zero in constructor or set(), this will be the source sample rate. 296 * Unlike AudioTrack, the sample rate is const after initialization, so doesn't need a lock. 297 */ getSampleRate()298 uint32_t getSampleRate() const { return mSampleRate; } 299 300 /* Sets marker position. When record reaches the number of frames specified, 301 * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition 302 * with marker == 0 cancels marker notification callback. 303 * To set a marker at a position which would compute as 0, 304 * a workaround is to set the marker at a nearby position such as ~0 or 1. 305 * If the AudioRecord has been opened with no callback function associated, 306 * the operation will fail. 307 * 308 * Parameters: 309 * 310 * marker: marker position expressed in wrapping (overflow) frame units, 311 * like the return value of getPosition(). 312 * 313 * Returned status (from utils/Errors.h) can be: 314 * - NO_ERROR: successful operation 315 * - INVALID_OPERATION: the AudioRecord has no callback installed. 316 */ 317 status_t setMarkerPosition(uint32_t marker); 318 status_t getMarkerPosition(uint32_t *marker) const; 319 320 /* Sets position update period. Every time the number of frames specified has been recorded, 321 * a callback with event type EVENT_NEW_POS is called. 322 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification 323 * callback. 324 * If the AudioRecord has been opened with no callback function associated, 325 * the operation will fail. 326 * Extremely small values may be rounded up to a value the implementation can support. 327 * 328 * Parameters: 329 * 330 * updatePeriod: position update notification period expressed in frames. 331 * 332 * Returned status (from utils/Errors.h) can be: 333 * - NO_ERROR: successful operation 334 * - INVALID_OPERATION: the AudioRecord has no callback installed. 335 */ 336 status_t setPositionUpdatePeriod(uint32_t updatePeriod); 337 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; 338 339 /* Return the total number of frames recorded since recording started. 340 * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz. 341 * It is reset to zero by stop(). 342 * 343 * Parameters: 344 * 345 * position: Address where to return record head position. 346 * 347 * Returned status (from utils/Errors.h) can be: 348 * - NO_ERROR: successful operation 349 * - BAD_VALUE: position is NULL 350 */ 351 status_t getPosition(uint32_t *position) const; 352 353 /* Return the record timestamp. 354 * 355 * Parameters: 356 * timestamp: A pointer to the timestamp to be filled. 357 * 358 * Returned status (from utils/Errors.h) can be: 359 * - NO_ERROR: successful operation 360 * - BAD_VALUE: timestamp is NULL 361 */ 362 status_t getTimestamp(ExtendedTimestamp *timestamp); 363 364 /** 365 * @param transferType 366 * @return text string that matches the enum name 367 */ 368 static const char * convertTransferToText(transfer_type transferType); 369 370 /* Returns a handle on the audio input used by this AudioRecord. 371 * 372 * Parameters: 373 * none. 374 * 375 * Returned value: 376 * handle on audio hardware input 377 */ 378 // FIXME The only known public caller is frameworks/opt/net/voip/src/jni/rtp/AudioGroup.cpp getInput()379 audio_io_handle_t getInput() const __attribute__((__deprecated__)) 380 { return getInputPrivate(); } 381 private: 382 audio_io_handle_t getInputPrivate() const; 383 public: 384 385 /* Returns the audio session ID associated with this AudioRecord. 386 * 387 * Parameters: 388 * none. 389 * 390 * Returned value: 391 * AudioRecord session ID. 392 * 393 * No lock needed because session ID doesn't change after first set(). 394 */ getSessionId()395 audio_session_t getSessionId() const { return mSessionId; } 396 397 /* Public API for TRANSFER_OBTAIN mode. 398 * Obtains a buffer of up to "audioBuffer->frameCount" full frames. 399 * After draining these frames of data, the caller should release them with releaseBuffer(). 400 * If the track buffer is not empty, obtainBuffer() returns as many contiguous 401 * full frames as are available immediately. 402 * 403 * If nonContig is non-NULL, it is an output parameter that will be set to the number of 404 * additional non-contiguous frames that are predicted to be available immediately, 405 * if the client were to release the first frames and then call obtainBuffer() again. 406 * This value is only a prediction, and needs to be confirmed. 407 * It will be set to zero for an error return. 408 * 409 * If the track buffer is empty and track is stopped, obtainBuffer() returns WOULD_BLOCK 410 * regardless of the value of waitCount. 411 * If the track buffer is empty and track is not stopped, obtainBuffer() blocks with a 412 * maximum timeout based on waitCount; see chart below. 413 * Buffers will be returned until the pool 414 * is exhausted, at which point obtainBuffer() will either block 415 * or return WOULD_BLOCK depending on the value of the "waitCount" 416 * parameter. 417 * 418 * Interpretation of waitCount: 419 * +n limits wait time to n * WAIT_PERIOD_MS, 420 * -1 causes an (almost) infinite wait time, 421 * 0 non-blocking. 422 * 423 * Buffer fields 424 * On entry: 425 * frameCount number of frames requested 426 * size ignored 427 * raw ignored 428 * sequence ignored 429 * After error return: 430 * frameCount 0 431 * size 0 432 * raw undefined 433 * sequence undefined 434 * After successful return: 435 * frameCount actual number of frames available, <= number requested 436 * size actual number of bytes available 437 * raw pointer to the buffer 438 * sequence IAudioRecord instance sequence number, as of obtainBuffer() 439 */ 440 441 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount, 442 size_t *nonContig = NULL); 443 444 // Explicit Routing 445 /** 446 * TODO Document this method. 447 */ 448 status_t setInputDevice(audio_port_handle_t deviceId); 449 450 /** 451 * TODO Document this method. 452 */ 453 audio_port_handle_t getInputDevice(); 454 455 /* Returns the ID of the audio device actually used by the input to which this AudioRecord 456 * is attached. 457 * The device ID is relevant only if the AudioRecord is active. 458 * When the AudioRecord is inactive, the device ID returned can be either: 459 * - AUDIO_PORT_HANDLE_NONE if the AudioRecord is not attached to any output. 460 * - The device ID used before paused or stopped. 461 * - The device ID selected by audio policy manager of setOutputDevice() if the AudioRecord 462 * has not been started yet. 463 * 464 * Parameters: 465 * none. 466 */ 467 audio_port_handle_t getRoutedDeviceId(); 468 469 /* Add an AudioDeviceCallback. The caller will be notified when the audio device 470 * to which this AudioRecord is routed is updated. 471 * Replaces any previously installed callback. 472 * Parameters: 473 * callback: The callback interface 474 * Returns NO_ERROR if successful. 475 * INVALID_OPERATION if the same callback is already installed. 476 * NO_INIT or PREMISSION_DENIED if AudioFlinger service is not reachable 477 * BAD_VALUE if the callback is NULL 478 */ 479 status_t addAudioDeviceCallback( 480 const sp<AudioSystem::AudioDeviceCallback>& callback); 481 482 /* remove an AudioDeviceCallback. 483 * Parameters: 484 * callback: The callback interface 485 * Returns NO_ERROR if successful. 486 * INVALID_OPERATION if the callback is not installed 487 * BAD_VALUE if the callback is NULL 488 */ 489 status_t removeAudioDeviceCallback( 490 const sp<AudioSystem::AudioDeviceCallback>& callback); 491 492 // AudioSystem::AudioDeviceCallback> virtuals 493 virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo, 494 audio_port_handle_t deviceId); 495 496 private: 497 /* If nonContig is non-NULL, it is an output parameter that will be set to the number of 498 * additional non-contiguous frames that are predicted to be available immediately, 499 * if the client were to release the first frames and then call obtainBuffer() again. 500 * This value is only a prediction, and needs to be confirmed. 501 * It will be set to zero for an error return. 502 * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(), 503 * in case the requested amount of frames is in two or more non-contiguous regions. 504 * FIXME requested and elapsed are both relative times. Consider changing to absolute time. 505 */ 506 status_t obtainBuffer(Buffer* audioBuffer, const struct timespec *requested, 507 struct timespec *elapsed = NULL, size_t *nonContig = NULL); 508 public: 509 510 /* Public API for TRANSFER_OBTAIN mode. 511 * Release an emptied buffer of "audioBuffer->frameCount" frames for AudioFlinger to re-fill. 512 * 513 * Buffer fields: 514 * frameCount currently ignored but recommend to set to actual number of frames consumed 515 * size actual number of bytes consumed, must be multiple of frameSize 516 * raw ignored 517 */ 518 void releaseBuffer(const Buffer* audioBuffer); 519 520 /* As a convenience we provide a read() interface to the audio buffer. 521 * Input parameter 'size' is in byte units. 522 * This is implemented on top of obtainBuffer/releaseBuffer. For best 523 * performance use callbacks. Returns actual number of bytes read >= 0, 524 * or one of the following negative status codes: 525 * INVALID_OPERATION AudioRecord is configured for streaming mode 526 * BAD_VALUE size is invalid 527 * WOULD_BLOCK when obtainBuffer() returns same, or 528 * AudioRecord was stopped during the read 529 * or any other error code returned by IAudioRecord::start() or restoreRecord_l(). 530 * Default behavior is to only return when all data has been transferred. Set 'blocking' to 531 * false for the method to return immediately without waiting to try multiple times to read 532 * the full content of the buffer. 533 */ 534 ssize_t read(void* buffer, size_t size, bool blocking = true); 535 536 /* Return the number of input frames lost in the audio driver since the last call of this 537 * function. Audio driver is expected to reset the value to 0 and restart counting upon 538 * returning the current value by this function call. Such loss typically occurs when the 539 * user space process is blocked longer than the capacity of audio driver buffers. 540 * Units: the number of input audio frames. 541 * FIXME The side-effect of resetting the counter may be incompatible with multi-client. 542 * Consider making it more like AudioTrack::getUnderrunFrames which doesn't have side effects. 543 */ 544 uint32_t getInputFramesLost() const; 545 546 /* Get the flags */ getFlags()547 audio_input_flags_t getFlags() const { AutoMutex _l(mLock); return mFlags; } 548 549 /* Get active microphones. A empty vector of MicrophoneInfo will be passed as a parameter, 550 * the data will be filled when querying the hal. 551 */ 552 status_t getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones); 553 554 /* Set the Microphone direction (for processing purposes). 555 */ 556 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction); 557 558 /* Set the Microphone zoom factor (for processing purposes). 559 */ 560 status_t setPreferredMicrophoneFieldDimension(float zoom); 561 562 /* Get the unique port ID assigned to this AudioRecord instance by audio policy manager. 563 * The ID is unique across all audioserver clients and can change during the life cycle 564 * of a given AudioRecord instance if the connection to audioserver is restored. 565 */ getPortId()566 audio_port_handle_t getPortId() const { return mPortId; }; 567 568 /* 569 * Dumps the state of an audio record. 570 */ 571 status_t dump(int fd, const Vector<String16>& args) const; 572 573 private: 574 /* copying audio record objects is not allowed */ 575 AudioRecord(const AudioRecord& other); 576 AudioRecord& operator = (const AudioRecord& other); 577 578 /* a small internal class to handle the callback */ 579 class AudioRecordThread : public Thread 580 { 581 public: 582 AudioRecordThread(AudioRecord& receiver); 583 584 // Do not call Thread::requestExitAndWait() without first calling requestExit(). 585 // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough. 586 virtual void requestExit(); 587 588 void pause(); // suspend thread from execution at next loop boundary 589 void resume(); // allow thread to execute, if not requested to exit 590 void wake(); // wake to handle changed notification conditions. 591 592 private: 593 void pauseInternal(nsecs_t ns = 0LL); 594 // like pause(), but only used internally within thread 595 596 friend class AudioRecord; 597 virtual bool threadLoop(); 598 AudioRecord& mReceiver; 599 virtual ~AudioRecordThread(); 600 Mutex mMyLock; // Thread::mLock is private 601 Condition mMyCond; // Thread::mThreadExitedCondition is private 602 bool mPaused; // whether thread is requested to pause at next loop entry 603 bool mPausedInt; // whether thread internally requests pause 604 nsecs_t mPausedNs; // if mPausedInt then associated timeout, otherwise ignored 605 bool mIgnoreNextPausedInt; // skip any internal pause and go immediately 606 // to processAudioBuffer() as state may have changed 607 // since pause time calculated. 608 }; 609 610 // body of AudioRecordThread::threadLoop() 611 // returns the maximum amount of time before we would like to run again, where: 612 // 0 immediately 613 // > 0 no later than this many nanoseconds from now 614 // NS_WHENEVER still active but no particular deadline 615 // NS_INACTIVE inactive so don't run again until re-started 616 // NS_NEVER never again 617 static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3; 618 nsecs_t processAudioBuffer(); 619 620 // caller must hold lock on mLock for all _l methods 621 622 status_t createRecord_l(const Modulo<uint32_t> &epoch, const String16& opPackageName); 623 624 // FIXME enum is faster than strcmp() for parameter 'from' 625 status_t restoreRecord_l(const char *from); 626 627 void updateRoutedDeviceId_l(); 628 629 sp<AudioRecordThread> mAudioRecordThread; 630 mutable Mutex mLock; 631 632 std::unique_ptr<RecordingActivityTracker> mTracker; 633 634 // Current client state: false = stopped, true = active. Protected by mLock. If more states 635 // are added, consider changing this to enum State { ... } mState as in AudioTrack. 636 bool mActive; 637 638 // for client callback handler 639 callback_t mCbf; // callback handler for events, or NULL 640 void* mUserData; 641 642 // for notification APIs 643 uint32_t mNotificationFramesReq; // requested number of frames between each 644 // notification callback 645 // as specified in constructor or set() 646 uint32_t mNotificationFramesAct; // actual number of frames between each 647 // notification callback 648 bool mRefreshRemaining; // processAudioBuffer() should refresh 649 // mRemainingFrames and mRetryOnPartialBuffer 650 651 // These are private to processAudioBuffer(), and are not protected by a lock 652 uint32_t mRemainingFrames; // number of frames to request in obtainBuffer() 653 bool mRetryOnPartialBuffer; // sleep and retry after partial obtainBuffer() 654 uint32_t mObservedSequence; // last observed value of mSequence 655 656 Modulo<uint32_t> mMarkerPosition; // in wrapping (overflow) frame units 657 bool mMarkerReached; 658 Modulo<uint32_t> mNewPosition; // in frames 659 uint32_t mUpdatePeriod; // in frames, zero means no EVENT_NEW_POS 660 661 status_t mStatus; 662 663 String16 mOpPackageName; // The package name used for app ops. 664 665 size_t mFrameCount; // corresponds to current IAudioRecord, value is 666 // reported back by AudioFlinger to the client 667 size_t mReqFrameCount; // frame count to request the first or next time 668 // a new IAudioRecord is needed, non-decreasing 669 670 int64_t mFramesRead; // total frames read. reset to zero after 671 // the start() following stop(). It is not 672 // changed after restoring the track. 673 int64_t mFramesReadServerOffset; // An offset to server frames read due to 674 // restoring AudioRecord, or stop/start. 675 // constant after constructor or set() 676 uint32_t mSampleRate; 677 audio_format_t mFormat; 678 uint32_t mChannelCount; 679 size_t mFrameSize; // app-level frame size == AudioFlinger frame size 680 uint32_t mLatency; // in ms 681 audio_channel_mask_t mChannelMask; 682 683 audio_input_flags_t mFlags; // same as mOrigFlags, except for bits that may 684 // be denied by client or server, such as 685 // AUDIO_INPUT_FLAG_FAST. mLock must be 686 // held to read or write those bits reliably. 687 audio_input_flags_t mOrigFlags; // as specified in constructor or set(), const 688 689 audio_session_t mSessionId; 690 audio_port_handle_t mPortId; // Id from Audio Policy Manager 691 transfer_type mTransfer; 692 693 // Next 5 fields may be changed if IAudioRecord is re-created, but always != 0 694 // provided the initial set() was successful 695 sp<media::IAudioRecord> mAudioRecord; 696 sp<IMemory> mCblkMemory; 697 audio_track_cblk_t* mCblk; // re-load after mLock.unlock() 698 sp<IMemory> mBufferMemory; 699 audio_io_handle_t mInput = AUDIO_IO_HANDLE_NONE; // from AudioSystem::getInputforAttr() 700 701 int mPreviousPriority; // before start() 702 SchedPolicy mPreviousSchedulingGroup; 703 bool mAwaitBoost; // thread should wait for priority boost before running 704 705 // The proxy should only be referenced while a lock is held because the proxy isn't 706 // multi-thread safe. 707 // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock, 708 // provided that the caller also holds an extra reference to the proxy and shared memory to keep 709 // them around in case they are replaced during the obtainBuffer(). 710 sp<AudioRecordClientProxy> mProxy; 711 712 bool mInOverrun; // whether recorder is currently in overrun state 713 714 ExtendedTimestamp mPreviousTimestamp{}; // used to detect retrograde motion 715 bool mTimestampRetrogradePositionReported = false; // reduce log spam 716 bool mTimestampRetrogradeTimeReported = false; // reduce log spam 717 718 private: 719 class DeathNotifier : public IBinder::DeathRecipient { 720 public: DeathNotifier(AudioRecord * audioRecord)721 DeathNotifier(AudioRecord* audioRecord) : mAudioRecord(audioRecord) { } 722 protected: 723 virtual void binderDied(const wp<IBinder>& who); 724 private: 725 const wp<AudioRecord> mAudioRecord; 726 }; 727 728 sp<DeathNotifier> mDeathNotifier; 729 uint32_t mSequence; // incremented for each new IAudioRecord attempt 730 uid_t mClientUid; 731 pid_t mClientPid; 732 audio_attributes_t mAttributes; 733 734 // For Device Selection API 735 // a value of AUDIO_PORT_HANDLE_NONE indicated default (AudioPolicyManager) routing. 736 audio_port_handle_t mSelectedDeviceId; // Device requested by the application. 737 audio_port_handle_t mRoutedDeviceId; // Device actually selected by audio policy manager: 738 // May not match the app selection depending on other 739 // activity and connected devices 740 wp<AudioSystem::AudioDeviceCallback> mDeviceCallback; 741 742 audio_microphone_direction_t mSelectedMicDirection; 743 float mSelectedMicFieldDimension; 744 745 private: 746 class MediaMetrics { 747 public: MediaMetrics()748 MediaMetrics() : mAnalyticsItem(MediaAnalyticsItem::create("audiorecord")), 749 mCreatedNs(systemTime(SYSTEM_TIME_REALTIME)), 750 mStartedNs(0), mDurationNs(0), mCount(0), 751 mLastError(NO_ERROR) { 752 } ~MediaMetrics()753 ~MediaMetrics() { 754 // mAnalyticsItem alloc failure will be flagged in the constructor 755 // don't log empty records 756 if (mAnalyticsItem->count() > 0) { 757 mAnalyticsItem->selfrecord(); 758 } 759 } 760 void gather(const AudioRecord *record); dup()761 MediaAnalyticsItem *dup() { return mAnalyticsItem->dup(); } 762 logStart(nsecs_t when)763 void logStart(nsecs_t when) { mStartedNs = when; mCount++; } logStop(nsecs_t when)764 void logStop(nsecs_t when) { mDurationNs += (when-mStartedNs); mStartedNs = 0;} markError(status_t errcode,const char * func)765 void markError(status_t errcode, const char *func) 766 { mLastError = errcode; mLastErrorFunc = func;} 767 private: 768 std::unique_ptr<MediaAnalyticsItem> mAnalyticsItem; 769 nsecs_t mCreatedNs; // XXX: perhaps not worth it in production 770 nsecs_t mStartedNs; 771 nsecs_t mDurationNs; 772 int32_t mCount; 773 774 status_t mLastError; 775 std::string mLastErrorFunc; 776 }; 777 MediaMetrics mMediaMetrics; 778 }; 779 780 }; // namespace android 781 782 #endif // ANDROID_AUDIORECORD_H 783