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