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