xref: /aosp_15_r20/external/webrtc/sdk/android/src/jni/audio_device/audio_record_jni.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 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_AUDIO_RECORD_JNI_H_
12 #define SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_AUDIO_RECORD_JNI_H_
13 
14 #include <jni.h>
15 
16 #include <memory>
17 
18 #include "api/sequence_checker.h"
19 #include "modules/audio_device/audio_device_buffer.h"
20 #include "modules/audio_device/include/audio_device_defines.h"
21 #include "sdk/android/src/jni/audio_device/audio_device_module.h"
22 
23 namespace webrtc {
24 
25 namespace jni {
26 
27 // Implements 16-bit mono PCM audio input support for Android using the Java
28 // AudioRecord interface. Most of the work is done by its Java counterpart in
29 // WebRtcAudioRecord.java. This class is created and lives on a thread in
30 // C++-land, but recorded audio buffers are delivered on a high-priority
31 // thread managed by the Java class.
32 //
33 // The Java class makes use of AudioEffect features (mainly AEC) which are
34 // first available in Jelly Bean. If it is instantiated running against earlier
35 // SDKs, the AEC provided by the APM in WebRTC must be used and enabled
36 // separately instead.
37 //
38 // An instance can be created on any thread, but must then be used on one and
39 // the same thread. All public methods must also be called on the same thread. A
40 // thread checker will RTC_DCHECK if any method is called on an invalid thread.
41 //
42 // This class uses AttachCurrentThreadIfNeeded to attach to a Java VM if needed.
43 // Additional thread checking guarantees that no other (possibly non attached)
44 // thread is used.
45 class AudioRecordJni : public AudioInput {
46  public:
47   static ScopedJavaLocalRef<jobject> CreateJavaWebRtcAudioRecord(
48       JNIEnv* env,
49       const JavaRef<jobject>& j_context,
50       const JavaRef<jobject>& j_audio_manager);
51 
52   AudioRecordJni(JNIEnv* env,
53                  const AudioParameters& audio_parameters,
54                  int total_delay_ms,
55                  const JavaRef<jobject>& j_webrtc_audio_record);
56   ~AudioRecordJni() override;
57 
58   int32_t Init() override;
59   int32_t Terminate() override;
60 
61   int32_t InitRecording() override;
62   bool RecordingIsInitialized() const override;
63 
64   int32_t StartRecording() override;
65   int32_t StopRecording() override;
66   bool Recording() const override;
67 
68   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;
69 
70   bool IsAcousticEchoCancelerSupported() const override;
71   bool IsNoiseSuppressorSupported() const override;
72 
73   int32_t EnableBuiltInAEC(bool enable) override;
74   int32_t EnableBuiltInNS(bool enable) override;
75 
76   // Called from Java side so we can cache the address of the Java-manged
77   // `byte_buffer` in `direct_buffer_address_`. The size of the buffer
78   // is also stored in `direct_buffer_capacity_in_bytes_`.
79   // This method will be called by the WebRtcAudioRecord constructor, i.e.,
80   // on the same thread that this object is created on.
81   void CacheDirectBufferAddress(JNIEnv* env,
82                                 const JavaParamRef<jobject>& j_caller,
83                                 const JavaParamRef<jobject>& byte_buffer);
84 
85   // Called periodically by the Java based WebRtcAudioRecord object when
86   // recording has started. Each call indicates that there are `length` new
87   // bytes recorded in the memory area `direct_buffer_address_` and it is
88   // now time to send these to the consumer.
89   // This method is called on a high-priority thread from Java. The name of
90   // the thread is 'AudioRecordThread'.
91   void DataIsRecorded(JNIEnv* env,
92                       const JavaParamRef<jobject>& j_caller,
93                       int length,
94                       int64_t capture_timestamp_ns);
95 
96  private:
97   // Stores thread ID in constructor.
98   SequenceChecker thread_checker_;
99 
100   // Stores thread ID in first call to OnDataIsRecorded() from high-priority
101   // thread in Java. Detached during construction of this object.
102   SequenceChecker thread_checker_java_;
103 
104   // Wraps the Java specific parts of the AudioRecordJni class.
105   JNIEnv* env_ = nullptr;
106   ScopedJavaGlobalRef<jobject> j_audio_record_;
107 
108   const AudioParameters audio_parameters_;
109 
110   // Delay estimate of the total round-trip delay (input + output).
111   // Fixed value set once in AttachAudioBuffer() and it can take one out of two
112   // possible values. See audio_common.h for details.
113   const int total_delay_ms_;
114 
115   // Cached copy of address to direct audio buffer owned by `j_audio_record_`.
116   void* direct_buffer_address_;
117 
118   // Number of bytes in the direct audio buffer owned by `j_audio_record_`.
119   size_t direct_buffer_capacity_in_bytes_;
120 
121   // Number audio frames per audio buffer. Each audio frame corresponds to
122   // one sample of PCM mono data at 16 bits per sample. Hence, each audio
123   // frame contains 2 bytes (given that the Java layer only supports mono).
124   // Example: 480 for 48000 Hz or 441 for 44100 Hz.
125   size_t frames_per_buffer_;
126 
127   bool initialized_;
128 
129   bool recording_;
130 
131   // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
132   // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
133   AudioDeviceBuffer* audio_device_buffer_;
134 };
135 
136 }  // namespace jni
137 
138 }  // namespace webrtc
139 
140 #endif  // SDK_ANDROID_SRC_JNI_AUDIO_DEVICE_AUDIO_RECORD_JNI_H_
141