Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_device / android / audio_track_jni.h
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 WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
12 #define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
13
14 #include <jni.h>
15
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"
20
21 namespace webrtc {
22
23 class EventWrapper;
24 class ThreadWrapper;
25
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
28
29 class AudioTrackJni : public PlayoutDelayProvider {
30  public:
31   static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env,
32                                               void* context);
33   static void ClearAndroidAudioDeviceObjects();
34   explicit AudioTrackJni(const int32_t id);
35   virtual ~AudioTrackJni();
36
37   // Main initializaton and termination
38   int32_t Init();
39   int32_t Terminate();
40   bool Initialized() const { return _initialized; }
41
42   // Device enumeration
43   int16_t PlayoutDevices() { return 1; }  // There is one device only.
44
45   int32_t PlayoutDeviceName(uint16_t index,
46                             char name[kAdmMaxDeviceNameSize],
47                             char guid[kAdmMaxGuidSize]);
48
49   // Device selection
50   int32_t SetPlayoutDevice(uint16_t index);
51   int32_t SetPlayoutDevice(
52       AudioDeviceModule::WindowsDeviceType device);
53
54   // Audio transport initialization
55   int32_t PlayoutIsAvailable(bool& available);  // NOLINT
56   int32_t InitPlayout();
57   bool PlayoutIsInitialized() const { return _playIsInitialized; }
58
59   // Audio transport control
60   int32_t StartPlayout();
61   int32_t StopPlayout();
62   bool Playing() const { return _playing; }
63
64   // Audio mixer initialization
65   int32_t InitSpeaker();
66   bool SpeakerIsInitialized() const { return _speakerIsInitialized; }
67
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
75
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
80
81
82   // Stereo support
83   int32_t StereoPlayoutIsAvailable(bool& available);  // NOLINT
84   int32_t SetStereoPlayout(bool enable);
85   int32_t StereoPlayout(bool& enabled) const;  // NOLINT
86
87   // Delay information and control
88   int32_t SetPlayoutBuffer(const AudioDeviceModule::BufferType type,
89                            uint16_t sizeMS);
90   int32_t PlayoutBuffer(AudioDeviceModule::BufferType& type,  // NOLINT
91                         uint16_t& sizeMS) const;
92   int32_t PlayoutDelay(uint16_t& delayMS) const;  // NOLINT
93
94   // Attach audio buffer
95   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
96
97   int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec);
98
99   // Error and warning information
100   bool PlayoutWarning() const;
101   bool PlayoutError() const;
102   void ClearPlayoutWarning();
103   void ClearPlayoutError();
104
105   // Speaker audio routing
106   int32_t SetLoudspeakerStatus(bool enable);
107   int32_t GetLoudspeakerStatus(bool& enable) const;  // NOLINT
108
109  protected:
110   virtual int PlayoutDelayMs() { return 0; }
111
112  private:
113   void Lock() EXCLUSIVE_LOCK_FUNCTION(_critSect) {
114     _critSect.Enter();
115   }
116   void UnLock() UNLOCK_FUNCTION(_critSect) {
117     _critSect.Leave();
118   }
119
120   int32_t InitJavaResources();
121   int32_t InitSampleRate();
122
123   static bool PlayThreadFunc(void*);
124   bool PlayThreadProcess();
125
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;
133
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
141
142   AudioDeviceBuffer* _ptrAudioBuffer;
143   CriticalSectionWrapper& _critSect;
144   int32_t _id;
145   bool _initialized;
146
147   EventWrapper& _timeEventPlay;
148   EventWrapper& _playStartStopEvent;
149   ThreadWrapper* _ptrThreadPlay;
150   uint32_t _playThreadID;
151   bool _playThreadIsInitialized;
152   bool _shutdownPlayThread;
153   bool _playoutDeviceIsSpecified;
154
155   bool _playing;
156   bool _playIsInitialized;
157   bool _speakerIsInitialized;
158
159   bool _startPlay;
160
161   uint16_t _playWarning;
162   uint16_t _playError;
163
164   uint16_t _delayPlayout;
165
166   uint16_t _samplingFreqOut; // Sampling frequency for Speaker
167   uint32_t _maxSpeakerVolume; // The maximum speaker volume value
168   bool _loudSpeakerOn;
169
170 };
171
172 }  // namespace webrtc
173
174 #endif  // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_