Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/modules/audio_device/android/audio_device_jni_android.h @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 8.7 KB
Line 
1/*
2 *  Copyright (c) 2012 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/*
12 *  Android audio device interface (JNI/AudioTrack/AudioRecord usage)
13 */
14
15#ifndef WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_JNI_ANDROID_H
16#define WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_JNI_ANDROID_H
17
18#include "webrtc/modules/audio_device/audio_device_generic.h"
19#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20
21#include <jni.h> // For accessing AudioDeviceAndroid java class
22
23namespace webrtc
24{
25class EventWrapper;
26
27const uint32_t N_REC_SAMPLES_PER_SEC = 16000; // Default is 16 kHz
28const uint32_t N_PLAY_SAMPLES_PER_SEC = 16000; // Default is 16 kHz
29
30const uint32_t N_REC_CHANNELS = 1; // default is mono recording
31const uint32_t N_PLAY_CHANNELS = 1; // default is mono playout
32
33const uint32_t REC_BUF_SIZE_IN_SAMPLES = 480; // Handle max 10 ms @ 48 kHz
34
35
36class ThreadWrapper;
37
38class AudioDeviceAndroidJni : public AudioDeviceGeneric {
39 public:
40  AudioDeviceAndroidJni(const int32_t id);
41  ~AudioDeviceAndroidJni();
42
43  static int32_t SetAndroidAudioDeviceObjects(void* javaVM,
44                                              void* env,
45                                              void* context);
46
47  virtual int32_t ActiveAudioLayer(
48      AudioDeviceModule::AudioLayer& audioLayer) const;
49
50  virtual int32_t Init();
51  virtual int32_t Terminate();
52  virtual bool Initialized() const;
53
54  virtual int16_t PlayoutDevices();
55  virtual int16_t RecordingDevices();
56  virtual int32_t PlayoutDeviceName(uint16_t index,
57                                    char name[kAdmMaxDeviceNameSize],
58                                    char guid[kAdmMaxGuidSize]);
59  virtual int32_t RecordingDeviceName(
60      uint16_t index,
61      char name[kAdmMaxDeviceNameSize],
62      char guid[kAdmMaxGuidSize]);
63
64  virtual int32_t SetPlayoutDevice(uint16_t index);
65  virtual int32_t SetPlayoutDevice(
66      AudioDeviceModule::WindowsDeviceType device);
67  virtual int32_t SetRecordingDevice(uint16_t index);
68  virtual int32_t SetRecordingDevice(
69      AudioDeviceModule::WindowsDeviceType device);
70
71  virtual int32_t PlayoutIsAvailable(bool& available);
72  virtual int32_t InitPlayout();
73  virtual bool PlayoutIsInitialized() const;
74  virtual int32_t RecordingIsAvailable(bool& available);
75  virtual int32_t InitRecording();
76  virtual bool RecordingIsInitialized() const;
77
78  virtual int32_t StartPlayout();
79  virtual int32_t StopPlayout();
80  virtual bool Playing() const;
81  virtual int32_t StartRecording();
82  virtual int32_t StopRecording();
83  virtual bool Recording() const;
84
85  virtual int32_t SetAGC(bool enable);
86  virtual bool AGC() const;
87
88  virtual int32_t SetWaveOutVolume(uint16_t volumeLeft, uint16_t volumeRight);
89  virtual int32_t WaveOutVolume(uint16_t& volumeLeft,
90                                uint16_t& volumeRight) const;
91
92  virtual int32_t SpeakerIsAvailable(bool& available);
93  virtual int32_t InitSpeaker();
94  virtual bool SpeakerIsInitialized() const;
95  virtual int32_t MicrophoneIsAvailable(bool& available);
96  virtual int32_t InitMicrophone();
97  virtual bool MicrophoneIsInitialized() const;
98
99  virtual int32_t SpeakerVolumeIsAvailable(bool& available);
100  virtual int32_t SetSpeakerVolume(uint32_t volume);
101  virtual int32_t SpeakerVolume(uint32_t& volume) const;
102  virtual int32_t MaxSpeakerVolume(uint32_t& maxVolume) const;
103  virtual int32_t MinSpeakerVolume(uint32_t& minVolume) const;
104  virtual int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const;
105
106  virtual int32_t MicrophoneVolumeIsAvailable(bool& available);
107  virtual int32_t SetMicrophoneVolume(uint32_t volume);
108  virtual int32_t MicrophoneVolume(uint32_t& volume) const;
109  virtual int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const;
110  virtual int32_t MinMicrophoneVolume(uint32_t& minVolume) const;
111  virtual int32_t MicrophoneVolumeStepSize(
112      uint16_t& stepSize) const;
113
114  virtual int32_t SpeakerMuteIsAvailable(bool& available);
115  virtual int32_t SetSpeakerMute(bool enable);
116  virtual int32_t SpeakerMute(bool& enabled) const;
117
118  virtual int32_t MicrophoneMuteIsAvailable(bool& available);
119  virtual int32_t SetMicrophoneMute(bool enable);
120  virtual int32_t MicrophoneMute(bool& enabled) const;
121
122  virtual int32_t MicrophoneBoostIsAvailable(bool& available);
123  virtual int32_t SetMicrophoneBoost(bool enable);
124  virtual int32_t MicrophoneBoost(bool& enabled) const;
125
126  virtual int32_t StereoPlayoutIsAvailable(bool& available);
127  virtual int32_t SetStereoPlayout(bool enable);
128  virtual int32_t StereoPlayout(bool& enabled) const;
129  virtual int32_t StereoRecordingIsAvailable(bool& available);
130  virtual int32_t SetStereoRecording(bool enable);
131  virtual int32_t StereoRecording(bool& enabled) const;
132
133  virtual int32_t SetPlayoutBuffer(
134      const AudioDeviceModule::BufferType type, uint16_t sizeMS);
135  virtual int32_t PlayoutBuffer(
136      AudioDeviceModule::BufferType& type, uint16_t& sizeMS) const;
137  virtual int32_t PlayoutDelay(uint16_t& delayMS) const;
138  virtual int32_t RecordingDelay(uint16_t& delayMS) const;
139
140  virtual int32_t CPULoad(uint16_t& load) const;
141
142  virtual bool PlayoutWarning() const;
143  virtual bool PlayoutError() const;
144  virtual bool RecordingWarning() const;
145  virtual bool RecordingError() const;
146  virtual void ClearPlayoutWarning();
147  virtual void ClearPlayoutError();
148  virtual void ClearRecordingWarning();
149  virtual void ClearRecordingError();
150
151  virtual void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
152
153  virtual int32_t SetRecordingSampleRate(
154      const uint32_t samplesPerSec);
155  virtual int32_t SetPlayoutSampleRate(
156      const uint32_t samplesPerSec);
157
158  virtual int32_t SetLoudspeakerStatus(bool enable);
159  virtual int32_t GetLoudspeakerStatus(bool& enable) const;
160
161 private:
162  // Lock
163  void Lock() {
164    _critSect.Enter();
165  };
166  void UnLock() {
167    _critSect.Leave();
168  };
169
170  // Init
171  int32_t InitJavaResources();
172  int32_t InitSampleRate();
173
174  // Threads
175  static bool RecThreadFunc(void*);
176  static bool PlayThreadFunc(void*);
177  bool RecThreadProcess();
178  bool PlayThreadProcess();
179
180  // Misc
181  AudioDeviceBuffer* _ptrAudioBuffer;
182  CriticalSectionWrapper& _critSect;
183  int32_t _id;
184
185  // Events
186  EventWrapper& _timeEventRec;
187  EventWrapper& _timeEventPlay;
188  EventWrapper& _recStartStopEvent;
189  EventWrapper& _playStartStopEvent;
190
191  // Threads
192  ThreadWrapper* _ptrThreadPlay;
193  ThreadWrapper* _ptrThreadRec;
194  uint32_t _recThreadID;
195  uint32_t _playThreadID;
196  bool _playThreadIsInitialized;
197  bool _recThreadIsInitialized;
198  bool _shutdownPlayThread;
199  bool _shutdownRecThread;
200
201  // Rec buffer
202  int8_t _recBuffer[2 * REC_BUF_SIZE_IN_SAMPLES];
203
204  // States
205  bool _recordingDeviceIsSpecified;
206  bool _playoutDeviceIsSpecified;
207  bool _initialized;
208  bool _recording;
209  bool _playing;
210  bool _recIsInitialized;
211  bool _playIsInitialized;
212  bool _micIsInitialized;
213  bool _speakerIsInitialized;
214
215  // Signal flags to threads
216  bool _startRec;
217  bool _startPlay;
218
219  // Warnings and errors
220  uint16_t _playWarning;
221  uint16_t _playError;
222  uint16_t _recWarning;
223  uint16_t _recError;
224
225  // Delay
226  uint16_t _delayPlayout;
227  uint16_t _delayRecording;
228
229  // AGC state
230  bool _AGC;
231
232  // Stored device properties
233  uint16_t _samplingFreqIn; // Sampling frequency for Mic
234  uint16_t _samplingFreqOut; // Sampling frequency for Speaker
235  uint32_t _maxSpeakerVolume; // The maximum speaker volume value
236  bool _loudSpeakerOn;
237  // Stores the desired audio source to use, set in SetRecordingDevice
238  int _recAudioSource;
239
240  // JNI and Java
241  JavaVM* _javaVM; // denotes a Java VM
242
243  JNIEnv* _jniEnvPlay; // The JNI env for playout thread
244  JNIEnv* _jniEnvRec; // The JNI env for recording thread
245
246  jclass _javaScClass; // AudioDeviceAndroid class
247  jobject _javaScObj; // AudioDeviceAndroid object
248
249  // The play buffer field in AudioDeviceAndroid object (global ref)
250  jobject _javaPlayBuffer;
251  // The rec buffer field in AudioDeviceAndroid object (global ref)
252  jobject _javaRecBuffer;
253  void* _javaDirectPlayBuffer; // Direct buffer pointer to play buffer
254  void* _javaDirectRecBuffer; // Direct buffer pointer to rec buffer
255  jmethodID _javaMidPlayAudio; // Method ID of play in AudioDeviceAndroid
256  jmethodID _javaMidRecAudio; // Method ID of rec in AudioDeviceAndroid
257
258  // TODO(leozwang): Android holds only one JVM, all these jni handling
259  // will be consolidated into a single place to make it consistant and
260  // reliable. Chromium has a good example at base/android.
261  static JavaVM* globalJvm;
262  static JNIEnv* globalJNIEnv;
263  static jobject globalContext;
264  static jclass globalScClass;
265};
266
267}  // namespace webrtc
268
269#endif  // WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_JNI_ANDROID_H
Note: See TracBrowser for help on using the repository browser.