1 /* 2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_RECORDER_H_ 12 #define MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_RECORDER_H_ 13 14 #include <aaudio/AAudio.h> 15 16 #include <memory> 17 18 #include "api/sequence_checker.h" 19 #include "api/task_queue/task_queue_base.h" 20 #include "modules/audio_device/android/aaudio_wrapper.h" 21 #include "modules/audio_device/include/audio_device_defines.h" 22 23 namespace webrtc { 24 25 class AudioDeviceBuffer; 26 class FineAudioBuffer; 27 class AudioManager; 28 29 // Implements low-latency 16-bit mono PCM audio input support for Android 30 // using the C based AAudio API. 31 // 32 // An instance must be created and destroyed on one and the same thread. 33 // All public methods must also be called on the same thread. A thread checker 34 // will RTC_DCHECK if any method is called on an invalid thread. Audio buffers 35 // are delivered on a dedicated high-priority thread owned by AAudio. 36 // 37 // The existing design forces the user to call InitRecording() after 38 // StopRecording() to be able to call StartRecording() again. This is in line 39 // with how the Java- based implementation works. 40 // 41 // TODO(henrika): add comments about device changes and adaptive buffer 42 // management. 43 class AAudioRecorder : public AAudioObserverInterface { 44 public: 45 explicit AAudioRecorder(AudioManager* audio_manager); 46 ~AAudioRecorder(); 47 48 int Init(); 49 int Terminate(); 50 51 int InitRecording(); RecordingIsInitialized()52 bool RecordingIsInitialized() const { return initialized_; } 53 54 int StartRecording(); 55 int StopRecording(); Recording()56 bool Recording() const { return recording_; } 57 58 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer); 59 latency_millis()60 double latency_millis() const { return latency_millis_; } 61 62 // TODO(henrika): add support using AAudio APIs when available. 63 int EnableBuiltInAEC(bool enable); 64 int EnableBuiltInAGC(bool enable); 65 int EnableBuiltInNS(bool enable); 66 67 protected: 68 // AAudioObserverInterface implementation. 69 70 // For an input stream, this function should read `num_frames` of recorded 71 // data, in the stream's current data format, from the `audio_data` buffer. 72 // Called on a real-time thread owned by AAudio. 73 aaudio_data_callback_result_t OnDataCallback(void* audio_data, 74 int32_t num_frames) override; 75 76 // AAudio calls this function if any error occurs on a callback thread. 77 // Called on a real-time thread owned by AAudio. 78 void OnErrorCallback(aaudio_result_t error) override; 79 80 private: 81 // Closes the existing stream and starts a new stream. 82 void HandleStreamDisconnected(); 83 84 // Ensures that methods are called from the same thread as this object is 85 // created on. 86 SequenceChecker thread_checker_; 87 88 // Stores thread ID in first call to AAudioPlayer::OnDataCallback from a 89 // real-time thread owned by AAudio. Detached during construction of this 90 // object. 91 SequenceChecker thread_checker_aaudio_; 92 93 // The thread on which this object is created on. 94 TaskQueueBase* main_thread_; 95 96 // Wraps all AAudio resources. Contains an input stream using the default 97 // input audio device. 98 AAudioWrapper aaudio_; 99 100 // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the 101 // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create(). 102 AudioDeviceBuffer* audio_device_buffer_ = nullptr; 103 104 bool initialized_ = false; 105 bool recording_ = false; 106 107 // Consumes audio of native buffer size and feeds the WebRTC layer with 10ms 108 // chunks of audio. 109 std::unique_ptr<FineAudioBuffer> fine_audio_buffer_; 110 111 // Counts number of detected overflow events reported by AAudio. 112 int32_t overflow_count_ = 0; 113 114 // Estimated time between an audio frame was recorded by the input device and 115 // it can read on the input stream. 116 double latency_millis_ = 0; 117 118 // True only for the first data callback in each audio session. 119 bool first_data_callback_ = true; 120 }; 121 122 } // namespace webrtc 123 124 #endif // MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_RECORDER_H_ 125