xref: /aosp_15_r20/external/webrtc/sdk/android/src/jni/audio_device/aaudio_recorder.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
16 #include <memory>
17 
18 #include "api/sequence_checker.h"
19 #include "api/task_queue/task_queue_base.h"
20 #include "modules/audio_device/audio_device_buffer.h"
21 #include "modules/audio_device/include/audio_device_defines.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, public AAudioObserverInterface {
47  public:
48   explicit AAudioRecorder(const AudioParameters& audio_parameters);
49   ~AAudioRecorder() override;
50 
51   int Init() override;
52   int Terminate() override;
53 
54   int InitRecording() override;
55   bool RecordingIsInitialized() const override;
56 
57   int StartRecording() override;
58   int StopRecording() override;
59   bool Recording() const override;
60 
61   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;
62 
63   // TODO(henrika): add support using AAudio APIs when available.
64   bool IsAcousticEchoCancelerSupported() const override;
65   bool IsNoiseSuppressorSupported() const override;
66   int EnableBuiltInAEC(bool enable) override;
67   int EnableBuiltInNS(bool enable) override;
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  private:
83   // Closes the existing stream and starts a new stream.
84   void HandleStreamDisconnected();
85 
86   // Ensures that methods are called from the same thread as this object is
87   // created on.
88   SequenceChecker thread_checker_;
89 
90   // Stores thread ID in first call to AAudioPlayer::OnDataCallback from a
91   // real-time thread owned by AAudio. Detached during construction of this
92   // object.
93   SequenceChecker thread_checker_aaudio_;
94 
95   // The thread on which this object is created on.
96   TaskQueueBase* main_thread_;
97 
98   // Wraps all AAudio resources. Contains an input stream using the default
99   // input audio device.
100   AAudioWrapper aaudio_;
101 
102   // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
103   // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
104   AudioDeviceBuffer* audio_device_buffer_ = nullptr;
105 
106   bool initialized_ = false;
107   bool recording_ = false;
108 
109   // Consumes audio of native buffer size and feeds the WebRTC layer with 10ms
110   // chunks of audio.
111   std::unique_ptr<FineAudioBuffer> fine_audio_buffer_;
112 
113   // Counts number of detected overflow events reported by AAudio.
114   int32_t overflow_count_ = 0;
115 
116   // Estimated time between an audio frame was recorded by the input device and
117   // it can read on the input stream.
118   double latency_millis_ = 0;
119 
120   // True only for the first data callback in each audio session.
121   bool first_data_callback_ = true;
122 };
123 
124 }  // namespace jni
125 
126 }  // namespace webrtc
127 
128 #endif  // SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_AAUDIO_RECORDER_H_
129