2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
12 #define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17 #include "webrtc/modules/audio_device/android/audio_common.h"
18 #include "webrtc/modules/audio_device/include/audio_device_defines.h"
19 #include "webrtc/modules/audio_device/audio_device_generic.h"
26 const uint32_t N_PLAY_SAMPLES_PER_SEC = 16000; // Default is 16 kHz
27 const uint32_t N_PLAY_CHANNELS = 1; // default is mono playout
29 class AudioTrackJni : public PlayoutDelayProvider {
31 static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env,
33 static void ClearAndroidAudioDeviceObjects();
34 explicit AudioTrackJni(const int32_t id);
35 virtual ~AudioTrackJni();
37 // Main initializaton and termination
40 bool Initialized() const { return _initialized; }
43 int16_t PlayoutDevices() { return 1; } // There is one device only.
45 int32_t PlayoutDeviceName(uint16_t index,
46 char name[kAdmMaxDeviceNameSize],
47 char guid[kAdmMaxGuidSize]);
50 int32_t SetPlayoutDevice(uint16_t index);
51 int32_t SetPlayoutDevice(
52 AudioDeviceModule::WindowsDeviceType device);
54 // Audio transport initialization
55 int32_t PlayoutIsAvailable(bool& available); // NOLINT
56 int32_t InitPlayout();
57 bool PlayoutIsInitialized() const { return _playIsInitialized; }
59 // Audio transport control
60 int32_t StartPlayout();
61 int32_t StopPlayout();
62 bool Playing() const { return _playing; }
64 // Audio mixer initialization
65 int32_t InitSpeaker();
66 bool SpeakerIsInitialized() const { return _speakerIsInitialized; }
68 // Speaker volume controls
69 int32_t SpeakerVolumeIsAvailable(bool& available); // NOLINT
70 int32_t SetSpeakerVolume(uint32_t volume);
71 int32_t SpeakerVolume(uint32_t& volume) const; // NOLINT
72 int32_t MaxSpeakerVolume(uint32_t& maxVolume) const; // NOLINT
73 int32_t MinSpeakerVolume(uint32_t& minVolume) const; // NOLINT
74 int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const; // NOLINT
76 // Speaker mute control
77 int32_t SpeakerMuteIsAvailable(bool& available); // NOLINT
78 int32_t SetSpeakerMute(bool enable);
79 int32_t SpeakerMute(bool& enabled) const; // NOLINT
83 int32_t StereoPlayoutIsAvailable(bool& available); // NOLINT
84 int32_t SetStereoPlayout(bool enable);
85 int32_t StereoPlayout(bool& enabled) const; // NOLINT
87 // Delay information and control
88 int32_t SetPlayoutBuffer(const AudioDeviceModule::BufferType type,
90 int32_t PlayoutBuffer(AudioDeviceModule::BufferType& type, // NOLINT
91 uint16_t& sizeMS) const;
92 int32_t PlayoutDelay(uint16_t& delayMS) const; // NOLINT
94 // Attach audio buffer
95 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
97 int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec);
99 // Error and warning information
100 bool PlayoutWarning() const;
101 bool PlayoutError() const;
102 void ClearPlayoutWarning();
103 void ClearPlayoutError();
105 // Speaker audio routing
106 int32_t SetLoudspeakerStatus(bool enable);
107 int32_t GetLoudspeakerStatus(bool& enable) const; // NOLINT
110 virtual int PlayoutDelayMs() { return 0; }
113 void Lock() EXCLUSIVE_LOCK_FUNCTION(_critSect) {
116 void UnLock() UNLOCK_FUNCTION(_critSect) {
120 int32_t InitJavaResources();
121 int32_t InitSampleRate();
123 static bool PlayThreadFunc(void*);
124 bool PlayThreadProcess();
126 // TODO(leozwang): Android holds only one JVM, all these jni handling
127 // will be consolidated into a single place to make it consistant and
128 // reliable. Chromium has a good example at base/android.
129 static JavaVM* globalJvm;
130 static JNIEnv* globalJNIEnv;
131 static jobject globalContext;
132 static jclass globalScClass;
134 JavaVM* _javaVM; // denotes a Java VM
135 JNIEnv* _jniEnvPlay; // The JNI env for playout thread
136 jclass _javaScClass; // AudioDeviceAndroid class
137 jobject _javaScObj; // AudioDeviceAndroid object
138 jobject _javaPlayBuffer;
139 void* _javaDirectPlayBuffer; // Direct buffer pointer to play buffer
140 jmethodID _javaMidPlayAudio; // Method ID of play in AudioDeviceAndroid
142 AudioDeviceBuffer* _ptrAudioBuffer;
143 CriticalSectionWrapper& _critSect;
147 EventWrapper& _timeEventPlay;
148 EventWrapper& _playStartStopEvent;
149 ThreadWrapper* _ptrThreadPlay;
150 uint32_t _playThreadID;
151 bool _playThreadIsInitialized;
152 bool _shutdownPlayThread;
153 bool _playoutDeviceIsSpecified;
156 bool _playIsInitialized;
157 bool _speakerIsInitialized;
161 uint16_t _playWarning;
164 uint16_t _delayPlayout;
166 uint16_t _samplingFreqOut; // Sampling frequency for Speaker
167 uint32_t _maxSpeakerVolume; // The maximum speaker volume value
172 } // namespace webrtc
174 #endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_