blob: 13acd8a71302f2df31f679aa597d1dfeeac3b882 [file] [log] [blame]
henrike@webrtc.org82f014a2013-09-10 18:24:07 +00001/*
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_OPENSLES_OUTPUT_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_OUTPUT_H_
13
14#include <SLES/OpenSLES.h>
15#include <SLES/OpenSLES_Android.h>
16#include <SLES/OpenSLES_AndroidConfiguration.h>
17
18#include "webrtc/modules/audio_device/android/audio_manager_jni.h"
19#include "webrtc/modules/audio_device/android/low_latency_event.h"
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000020#include "webrtc/modules/audio_device/android/audio_common.h"
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000021#include "webrtc/modules/audio_device/include/audio_device_defines.h"
22#include "webrtc/modules/audio_device/include/audio_device.h"
23#include "webrtc/system_wrappers/interface/scoped_ptr.h"
24
25namespace webrtc {
26
27class AudioDeviceBuffer;
28class CriticalSectionWrapper;
29class FineAudioBuffer;
30class SingleRwFifo;
31class ThreadWrapper;
32
33// OpenSL implementation that facilitate playing PCM data to an android device.
34// This class is Thread-compatible. I.e. Given an instance of this class, calls
35// to non-const methods require exclusive access to the object.
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000036class OpenSlesOutput : public PlayoutDelayProvider {
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000037 public:
38 explicit OpenSlesOutput(const int32_t id);
39 virtual ~OpenSlesOutput();
40
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000041 static int32_t SetAndroidAudioDeviceObjects(void* javaVM,
42 void* env,
43 void* context);
henrike@webrtc.org573a1b42014-01-10 22:58:06 +000044 static void ClearAndroidAudioDeviceObjects();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000045
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000046 // Main initializaton and termination
47 int32_t Init();
48 int32_t Terminate();
49 bool Initialized() const { return initialized_; }
50
51 // Device enumeration
52 int16_t PlayoutDevices() { return 1; }
53
54 int32_t PlayoutDeviceName(uint16_t index,
55 char name[kAdmMaxDeviceNameSize],
56 char guid[kAdmMaxGuidSize]);
57
58 // Device selection
59 int32_t SetPlayoutDevice(uint16_t index);
60 int32_t SetPlayoutDevice(
61 AudioDeviceModule::WindowsDeviceType device) { return 0; }
62
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000063 // No-op
64 int32_t SetPlayoutSampleRate(uint32_t sample_rate_hz) { return 0; }
65
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000066 // Audio transport initialization
67 int32_t PlayoutIsAvailable(bool& available); // NOLINT
68 int32_t InitPlayout();
69 bool PlayoutIsInitialized() const { return play_initialized_; }
70
71 // Audio transport control
72 int32_t StartPlayout();
73 int32_t StopPlayout();
74 bool Playing() const { return playing_; }
75
76 // Audio mixer initialization
77 int32_t SpeakerIsAvailable(bool& available); // NOLINT
78 int32_t InitSpeaker();
79 bool SpeakerIsInitialized() const { return speaker_initialized_; }
80
81 // Speaker volume controls
82 int32_t SpeakerVolumeIsAvailable(bool& available); // NOLINT
83 int32_t SetSpeakerVolume(uint32_t volume);
84 int32_t SpeakerVolume(uint32_t& volume) const { return 0; } // NOLINT
85 int32_t MaxSpeakerVolume(uint32_t& maxVolume) const; // NOLINT
86 int32_t MinSpeakerVolume(uint32_t& minVolume) const; // NOLINT
87 int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const; // NOLINT
88
89 // Speaker mute control
90 int32_t SpeakerMuteIsAvailable(bool& available); // NOLINT
91 int32_t SetSpeakerMute(bool enable) { return -1; }
92 int32_t SpeakerMute(bool& enabled) const { return -1; } // NOLINT
93
94
95 // Stereo support
96 int32_t StereoPlayoutIsAvailable(bool& available); // NOLINT
97 int32_t SetStereoPlayout(bool enable);
98 int32_t StereoPlayout(bool& enabled) const; // NOLINT
99
100 // Delay information and control
101 int32_t SetPlayoutBuffer(const AudioDeviceModule::BufferType type,
102 uint16_t sizeMS) { return -1; }
103 int32_t PlayoutBuffer(AudioDeviceModule::BufferType& type, // NOLINT
104 uint16_t& sizeMS) const;
105 int32_t PlayoutDelay(uint16_t& delayMS) const; // NOLINT
106
107
108 // Error and warning information
109 bool PlayoutWarning() const { return false; }
110 bool PlayoutError() const { return false; }
111 void ClearPlayoutWarning() {}
112 void ClearPlayoutError() {}
113
114 // Attach audio buffer
115 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
116
117 // Speaker audio routing
118 int32_t SetLoudspeakerStatus(bool enable);
119 int32_t GetLoudspeakerStatus(bool& enable) const; // NOLINT
120
121 protected:
122 virtual int PlayoutDelayMs();
123
124 private:
125 enum {
126 kNumInterfaces = 3,
127 // TODO(xians): Reduce the numbers of buffers to improve the latency.
128 // Currently 30ms worth of buffers are needed due to audio
129 // pipeline processing jitter. Note: kNumOpenSlBuffers must
130 // not be changed.
131 // According to the opensles documentation in the ndk:
132 // The lower output latency path is used only if the application requests a
133 // buffer count of 2 or more. Use minimum number of buffers to keep delay
134 // as low as possible.
135 kNumOpenSlBuffers = 2,
136 // NetEq delivers frames on a 10ms basis. This means that every 10ms there
137 // will be a time consuming task. Keeping 10ms worth of buffers will ensure
138 // that there is 10ms to perform the time consuming task without running
139 // into underflow.
140 // In addition to the 10ms that needs to be stored for NetEq processing
141 // there will be jitter in audio pipe line due to the acquisition of locks.
142 // Note: The buffers in the OpenSL queue do not count towards the 10ms of
143 // frames needed since OpenSL needs to have them ready for playout.
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000144 kNum10MsToBuffer = 6,
henrike@webrtc.org82f014a2013-09-10 18:24:07 +0000145 };
146
147 bool InitSampleRate();
148 bool SetLowLatency();
149 void UpdatePlayoutDelay();
150 // It might be possible to dynamically add or remove buffers based on how
151 // close to depletion the fifo is. Few buffers means low delay. Too few
152 // buffers will cause underrun. Dynamically changing the number of buffer
153 // will greatly increase code complexity.
154 void CalculateNumFifoBuffersNeeded();
155 void AllocateBuffers();
156 int TotalBuffersUsed() const;
157 bool EnqueueAllBuffers();
158 // This function also configures the audio player, e.g. sample rate to use
159 // etc, so it should be called when starting playout.
160 bool CreateAudioPlayer();
161 void DestroyAudioPlayer();
162
163 // When underrun happens there won't be a new frame ready for playout that
164 // can be retrieved yet. Since the OpenSL thread must return ASAP there will
165 // be one less queue available to OpenSL. This function handles this case
166 // gracefully by restarting the audio, pushing silent frames to OpenSL for
167 // playout. This will sound like a click. Underruns are also logged to
168 // make it possible to identify these types of audio artifacts.
169 // This function returns true if there has been underrun. Further processing
170 // of audio data should be avoided until this function returns false again.
171 // The function needs to be protected by |crit_sect_|.
172 bool HandleUnderrun(int event_id, int event_msg);
173
174 static void PlayerSimpleBufferQueueCallback(
175 SLAndroidSimpleBufferQueueItf queueItf,
176 void* pContext);
177 // This function must not take any locks or do any heavy work. It is a
178 // requirement for the OpenSL implementation to work as intended. The reason
179 // for this is that taking locks exposes the OpenSL thread to the risk of
180 // priority inversion.
181 void PlayerSimpleBufferQueueCallbackHandler(
182 SLAndroidSimpleBufferQueueItf queueItf);
183
184 bool StartCbThreads();
185 void StopCbThreads();
186 static bool CbThread(void* context);
187 // This function must be protected against data race with threads calling this
188 // class' public functions. It is a requirement for this class to be
189 // Thread-compatible.
190 bool CbThreadImpl();
191
192 // Java API handle
193 AudioManagerJni audio_manager_;
194
195 int id_;
196 bool initialized_;
197 bool speaker_initialized_;
198 bool play_initialized_;
199
200 // Members that are read/write accessed concurrently by the process thread and
201 // threads calling public functions of this class.
202 scoped_ptr<ThreadWrapper> play_thread_; // Processing thread
203 scoped_ptr<CriticalSectionWrapper> crit_sect_;
204 // This member controls the starting and stopping of playing audio to the
205 // the device.
206 bool playing_;
207
208 // Only one thread, T1, may push and only one thread, T2, may pull. T1 may or
209 // may not be the same thread as T2. T1 is the process thread and T2 is the
210 // OpenSL thread.
211 scoped_ptr<SingleRwFifo> fifo_;
212 int num_fifo_buffers_needed_;
213 LowLatencyEvent event_;
214 int number_underruns_;
215
216 // OpenSL handles
217 SLObjectItf sles_engine_;
218 SLEngineItf sles_engine_itf_;
219 SLObjectItf sles_player_;
220 SLPlayItf sles_player_itf_;
221 SLAndroidSimpleBufferQueueItf sles_player_sbq_itf_;
222 SLObjectItf sles_output_mixer_;
223
224 // Audio buffers
225 AudioDeviceBuffer* audio_buffer_;
226 scoped_ptr<FineAudioBuffer> fine_buffer_;
227 scoped_array<scoped_array<int8_t> > play_buf_;
228 // Index in |rec_buf_| pointing to the audio buffer that will be ready the
229 // next time PlayerSimpleBufferQueueCallbackHandler is invoked.
230 // Ready means buffer is ready to be played out to device.
231 int active_queue_;
232
233 // Audio settings
234 uint32_t speaker_sampling_rate_;
235 int buffer_size_samples_;
236 int buffer_size_bytes_;
237
238 // Audio status
239 uint16_t playout_delay_;
240};
241
242} // namespace webrtc
243
244#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_OUTPUT_H_