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 MODULES_AUDIO_DEVICE_ANDROID_AUDIO_RECORD_JNI_H_
12 #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_RECORD_JNI_H_
13 
14 #include <memory>
15 
16 #include <jni.h>
17 
18 #include "modules/audio_device/android/audio_manager.h"
19 #include "modules/audio_device/audio_device_generic.h"
20 #include "modules/audio_device/include/audio_device_defines.h"
21 #include "modules/utility/include/helpers_android.h"
22 #include "modules/utility/include/jvm_android.h"
23 #include "rtc_base/thread_checker.h"
24 
25 namespace webrtc {
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 must be created and destroyed on one and the same thread.
39 // All public methods must also be called on the same thread. A thread checker
40 // 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 // and detach when the object goes out of scope. Additional thread checking
44 // guarantees that no other (possibly non attached) thread is used.
45 class AudioRecordJni {
46  public:
47   // Wraps the Java specific parts of the AudioRecordJni into one helper class.
48   class JavaAudioRecord {
49    public:
50     JavaAudioRecord(NativeRegistration* native_registration,
51                    std::unique_ptr<GlobalRef> audio_track);
52     ~JavaAudioRecord();
53 
54     int InitRecording(int sample_rate, size_t channels);
55     bool StartRecording();
56     bool StopRecording();
57     bool EnableBuiltInAEC(bool enable);
58     bool EnableBuiltInNS(bool enable);
59 
60    private:
61     std::unique_ptr<GlobalRef> audio_record_;
62     jmethodID init_recording_;
63     jmethodID start_recording_;
64     jmethodID stop_recording_;
65     jmethodID enable_built_in_aec_;
66     jmethodID enable_built_in_ns_;
67   };
68 
69   explicit AudioRecordJni(AudioManager* audio_manager);
70   ~AudioRecordJni();
71 
72   int32_t Init();
73   int32_t Terminate();
74 
75   int32_t InitRecording();
RecordingIsInitialized()76   bool RecordingIsInitialized() const { return initialized_; }
77 
78   int32_t StartRecording();
79   int32_t StopRecording();
Recording()80   bool Recording() const { return recording_; }
81 
82   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
83 
84   int32_t EnableBuiltInAEC(bool enable);
85   int32_t EnableBuiltInAGC(bool enable);
86   int32_t EnableBuiltInNS(bool enable);
87 
88  private:
89   // Called from Java side so we can cache the address of the Java-manged
90   // |byte_buffer| in |direct_buffer_address_|. The size of the buffer
91   // is also stored in |direct_buffer_capacity_in_bytes_|.
92   // This method will be called by the WebRtcAudioRecord constructor, i.e.,
93   // on the same thread that this object is created on.
94   static void JNICALL CacheDirectBufferAddress(
95     JNIEnv* env, jobject obj, jobject byte_buffer, jlong nativeAudioRecord);
96   void OnCacheDirectBufferAddress(JNIEnv* env, jobject byte_buffer);
97 
98   // Called periodically by the Java based WebRtcAudioRecord object when
99   // recording has started. Each call indicates that there are |length| new
100   // bytes recorded in the memory area |direct_buffer_address_| and it is
101   // now time to send these to the consumer.
102   // This method is called on a high-priority thread from Java. The name of
103   // the thread is 'AudioRecordThread'.
104   static void JNICALL DataIsRecorded(
105     JNIEnv* env, jobject obj, jint length, jlong nativeAudioRecord);
106   void OnDataIsRecorded(int length);
107 
108   // Stores thread ID in constructor.
109   rtc::ThreadChecker thread_checker_;
110 
111   // Stores thread ID in first call to OnDataIsRecorded() from high-priority
112   // thread in Java. Detached during construction of this object.
113   rtc::ThreadChecker thread_checker_java_;
114 
115   // Calls AttachCurrentThread() if this thread is not attached at construction.
116   // Also ensures that DetachCurrentThread() is called at destruction.
117   AttachCurrentThreadIfNeeded attach_thread_if_needed_;
118 
119   // Wraps the JNI interface pointer and methods associated with it.
120   std::unique_ptr<JNIEnvironment> j_environment_;
121 
122   // Contains factory method for creating the Java object.
123   std::unique_ptr<NativeRegistration> j_native_registration_;
124 
125   // Wraps the Java specific parts of the AudioRecordJni class.
126   std::unique_ptr<AudioRecordJni::JavaAudioRecord> j_audio_record_;
127 
128   // Raw pointer to the audio manger.
129   const AudioManager* audio_manager_;
130 
131   // Contains audio parameters provided to this class at construction by the
132   // AudioManager.
133   const AudioParameters audio_parameters_;
134 
135   // Delay estimate of the total round-trip delay (input + output).
136   // Fixed value set once in AttachAudioBuffer() and it can take one out of two
137   // possible values. See audio_common.h for details.
138   int total_delay_in_milliseconds_;
139 
140   // Cached copy of address to direct audio buffer owned by |j_audio_record_|.
141   void* direct_buffer_address_;
142 
143   // Number of bytes in the direct audio buffer owned by |j_audio_record_|.
144   size_t direct_buffer_capacity_in_bytes_;
145 
146   // Number audio frames per audio buffer. Each audio frame corresponds to
147   // one sample of PCM mono data at 16 bits per sample. Hence, each audio
148   // frame contains 2 bytes (given that the Java layer only supports mono).
149   // Example: 480 for 48000 Hz or 441 for 44100 Hz.
150   size_t frames_per_buffer_;
151 
152   bool initialized_;
153 
154   bool recording_;
155 
156   // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
157   // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
158   AudioDeviceBuffer* audio_device_buffer_;
159 };
160 
161 }  // namespace webrtc
162 
163 #endif  // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_RECORD_JNI_H_
164