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