1 /*
2 * Copyright (c) 2015 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 #include "modules/audio_device/android/opensles_player.h"
12
13 #include <android/log.h>
14
15 #include <memory>
16
17 #include "api/array_view.h"
18 #include "modules/audio_device/android/audio_common.h"
19 #include "modules/audio_device/android/audio_manager.h"
20 #include "modules/audio_device/fine_audio_buffer.h"
21 #include "rtc_base/arraysize.h"
22 #include "rtc_base/checks.h"
23 #include "rtc_base/format_macros.h"
24 #include "rtc_base/platform_thread.h"
25 #include "rtc_base/time_utils.h"
26
27 #define TAG "OpenSLESPlayer"
28 #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
29 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
30 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
31 #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
32 #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
33
34 #define RETURN_ON_ERROR(op, ...) \
35 do { \
36 SLresult err = (op); \
37 if (err != SL_RESULT_SUCCESS) { \
38 ALOGE("%s failed: %s", #op, GetSLErrorString(err)); \
39 return __VA_ARGS__; \
40 } \
41 } while (0)
42
43 namespace webrtc {
44
OpenSLESPlayer(AudioManager * audio_manager)45 OpenSLESPlayer::OpenSLESPlayer(AudioManager* audio_manager)
46 : audio_manager_(audio_manager),
47 audio_parameters_(audio_manager->GetPlayoutAudioParameters()),
48 audio_device_buffer_(nullptr),
49 initialized_(false),
50 playing_(false),
51 buffer_index_(0),
52 engine_(nullptr),
53 player_(nullptr),
54 simple_buffer_queue_(nullptr),
55 volume_(nullptr),
56 last_play_time_(0) {
57 ALOGD("ctor[tid=%d]", rtc::CurrentThreadId());
58 // Use native audio output parameters provided by the audio manager and
59 // define the PCM format structure.
60 pcm_format_ = CreatePCMConfiguration(audio_parameters_.channels(),
61 audio_parameters_.sample_rate(),
62 audio_parameters_.bits_per_sample());
63 // Detach from this thread since we want to use the checker to verify calls
64 // from the internal audio thread.
65 thread_checker_opensles_.Detach();
66 }
67
~OpenSLESPlayer()68 OpenSLESPlayer::~OpenSLESPlayer() {
69 ALOGD("dtor[tid=%d]", rtc::CurrentThreadId());
70 RTC_DCHECK(thread_checker_.IsCurrent());
71 Terminate();
72 DestroyAudioPlayer();
73 DestroyMix();
74 engine_ = nullptr;
75 RTC_DCHECK(!engine_);
76 RTC_DCHECK(!output_mix_.Get());
77 RTC_DCHECK(!player_);
78 RTC_DCHECK(!simple_buffer_queue_);
79 RTC_DCHECK(!volume_);
80 }
81
Init()82 int OpenSLESPlayer::Init() {
83 ALOGD("Init[tid=%d]", rtc::CurrentThreadId());
84 RTC_DCHECK(thread_checker_.IsCurrent());
85 if (audio_parameters_.channels() == 2) {
86 ALOGW("Stereo mode is enabled");
87 }
88 return 0;
89 }
90
Terminate()91 int OpenSLESPlayer::Terminate() {
92 ALOGD("Terminate[tid=%d]", rtc::CurrentThreadId());
93 RTC_DCHECK(thread_checker_.IsCurrent());
94 StopPlayout();
95 return 0;
96 }
97
InitPlayout()98 int OpenSLESPlayer::InitPlayout() {
99 ALOGD("InitPlayout[tid=%d]", rtc::CurrentThreadId());
100 RTC_DCHECK(thread_checker_.IsCurrent());
101 RTC_DCHECK(!initialized_);
102 RTC_DCHECK(!playing_);
103 if (!ObtainEngineInterface()) {
104 ALOGE("Failed to obtain SL Engine interface");
105 return -1;
106 }
107 CreateMix();
108 initialized_ = true;
109 buffer_index_ = 0;
110 return 0;
111 }
112
StartPlayout()113 int OpenSLESPlayer::StartPlayout() {
114 ALOGD("StartPlayout[tid=%d]", rtc::CurrentThreadId());
115 RTC_DCHECK(thread_checker_.IsCurrent());
116 RTC_DCHECK(initialized_);
117 RTC_DCHECK(!playing_);
118 if (fine_audio_buffer_) {
119 fine_audio_buffer_->ResetPlayout();
120 }
121 // The number of lower latency audio players is limited, hence we create the
122 // audio player in Start() and destroy it in Stop().
123 CreateAudioPlayer();
124 // Fill up audio buffers to avoid initial glitch and to ensure that playback
125 // starts when mode is later changed to SL_PLAYSTATE_PLAYING.
126 // TODO(henrika): we can save some delay by only making one call to
127 // EnqueuePlayoutData. Most likely not worth the risk of adding a glitch.
128 last_play_time_ = rtc::Time();
129 for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) {
130 EnqueuePlayoutData(true);
131 }
132 // Start streaming data by setting the play state to SL_PLAYSTATE_PLAYING.
133 // For a player object, when the object is in the SL_PLAYSTATE_PLAYING
134 // state, adding buffers will implicitly start playback.
135 RETURN_ON_ERROR((*player_)->SetPlayState(player_, SL_PLAYSTATE_PLAYING), -1);
136 playing_ = (GetPlayState() == SL_PLAYSTATE_PLAYING);
137 RTC_DCHECK(playing_);
138 return 0;
139 }
140
StopPlayout()141 int OpenSLESPlayer::StopPlayout() {
142 ALOGD("StopPlayout[tid=%d]", rtc::CurrentThreadId());
143 RTC_DCHECK(thread_checker_.IsCurrent());
144 if (!initialized_ || !playing_) {
145 return 0;
146 }
147 // Stop playing by setting the play state to SL_PLAYSTATE_STOPPED.
148 RETURN_ON_ERROR((*player_)->SetPlayState(player_, SL_PLAYSTATE_STOPPED), -1);
149 // Clear the buffer queue to flush out any remaining data.
150 RETURN_ON_ERROR((*simple_buffer_queue_)->Clear(simple_buffer_queue_), -1);
151 #if RTC_DCHECK_IS_ON
152 // Verify that the buffer queue is in fact cleared as it should.
153 SLAndroidSimpleBufferQueueState buffer_queue_state;
154 (*simple_buffer_queue_)->GetState(simple_buffer_queue_, &buffer_queue_state);
155 RTC_DCHECK_EQ(0, buffer_queue_state.count);
156 RTC_DCHECK_EQ(0, buffer_queue_state.index);
157 #endif
158 // The number of lower latency audio players is limited, hence we create the
159 // audio player in Start() and destroy it in Stop().
160 DestroyAudioPlayer();
161 thread_checker_opensles_.Detach();
162 initialized_ = false;
163 playing_ = false;
164 return 0;
165 }
166
SpeakerVolumeIsAvailable(bool & available)167 int OpenSLESPlayer::SpeakerVolumeIsAvailable(bool& available) {
168 available = false;
169 return 0;
170 }
171
MaxSpeakerVolume(uint32_t & maxVolume) const172 int OpenSLESPlayer::MaxSpeakerVolume(uint32_t& maxVolume) const {
173 return -1;
174 }
175
MinSpeakerVolume(uint32_t & minVolume) const176 int OpenSLESPlayer::MinSpeakerVolume(uint32_t& minVolume) const {
177 return -1;
178 }
179
SetSpeakerVolume(uint32_t volume)180 int OpenSLESPlayer::SetSpeakerVolume(uint32_t volume) {
181 return -1;
182 }
183
SpeakerVolume(uint32_t & volume) const184 int OpenSLESPlayer::SpeakerVolume(uint32_t& volume) const {
185 return -1;
186 }
187
AttachAudioBuffer(AudioDeviceBuffer * audioBuffer)188 void OpenSLESPlayer::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
189 ALOGD("AttachAudioBuffer");
190 RTC_DCHECK(thread_checker_.IsCurrent());
191 audio_device_buffer_ = audioBuffer;
192 const int sample_rate_hz = audio_parameters_.sample_rate();
193 ALOGD("SetPlayoutSampleRate(%d)", sample_rate_hz);
194 audio_device_buffer_->SetPlayoutSampleRate(sample_rate_hz);
195 const size_t channels = audio_parameters_.channels();
196 ALOGD("SetPlayoutChannels(%" RTC_PRIuS ")", channels);
197 audio_device_buffer_->SetPlayoutChannels(channels);
198 RTC_CHECK(audio_device_buffer_);
199 AllocateDataBuffers();
200 }
201
AllocateDataBuffers()202 void OpenSLESPlayer::AllocateDataBuffers() {
203 ALOGD("AllocateDataBuffers");
204 RTC_DCHECK(thread_checker_.IsCurrent());
205 RTC_DCHECK(!simple_buffer_queue_);
206 RTC_CHECK(audio_device_buffer_);
207 // Create a modified audio buffer class which allows us to ask for any number
208 // of samples (and not only multiple of 10ms) to match the native OpenSL ES
209 // buffer size. The native buffer size corresponds to the
210 // PROPERTY_OUTPUT_FRAMES_PER_BUFFER property which is the number of audio
211 // frames that the HAL (Hardware Abstraction Layer) buffer can hold. It is
212 // recommended to construct audio buffers so that they contain an exact
213 // multiple of this number. If so, callbacks will occur at regular intervals,
214 // which reduces jitter.
215 const size_t buffer_size_in_samples =
216 audio_parameters_.frames_per_buffer() * audio_parameters_.channels();
217 ALOGD("native buffer size: %" RTC_PRIuS, buffer_size_in_samples);
218 ALOGD("native buffer size in ms: %.2f",
219 audio_parameters_.GetBufferSizeInMilliseconds());
220 fine_audio_buffer_ = std::make_unique<FineAudioBuffer>(audio_device_buffer_);
221 // Allocated memory for audio buffers.
222 for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) {
223 audio_buffers_[i].reset(new SLint16[buffer_size_in_samples]);
224 }
225 }
226
ObtainEngineInterface()227 bool OpenSLESPlayer::ObtainEngineInterface() {
228 ALOGD("ObtainEngineInterface");
229 RTC_DCHECK(thread_checker_.IsCurrent());
230 if (engine_)
231 return true;
232 // Get access to (or create if not already existing) the global OpenSL Engine
233 // object.
234 SLObjectItf engine_object = audio_manager_->GetOpenSLEngine();
235 if (engine_object == nullptr) {
236 ALOGE("Failed to access the global OpenSL engine");
237 return false;
238 }
239 // Get the SL Engine Interface which is implicit.
240 RETURN_ON_ERROR(
241 (*engine_object)->GetInterface(engine_object, SL_IID_ENGINE, &engine_),
242 false);
243 return true;
244 }
245
CreateMix()246 bool OpenSLESPlayer::CreateMix() {
247 ALOGD("CreateMix");
248 RTC_DCHECK(thread_checker_.IsCurrent());
249 RTC_DCHECK(engine_);
250 if (output_mix_.Get())
251 return true;
252
253 // Create the ouput mix on the engine object. No interfaces will be used.
254 RETURN_ON_ERROR((*engine_)->CreateOutputMix(engine_, output_mix_.Receive(), 0,
255 nullptr, nullptr),
256 false);
257 RETURN_ON_ERROR(output_mix_->Realize(output_mix_.Get(), SL_BOOLEAN_FALSE),
258 false);
259 return true;
260 }
261
DestroyMix()262 void OpenSLESPlayer::DestroyMix() {
263 ALOGD("DestroyMix");
264 RTC_DCHECK(thread_checker_.IsCurrent());
265 if (!output_mix_.Get())
266 return;
267 output_mix_.Reset();
268 }
269
CreateAudioPlayer()270 bool OpenSLESPlayer::CreateAudioPlayer() {
271 ALOGD("CreateAudioPlayer");
272 RTC_DCHECK(thread_checker_.IsCurrent());
273 RTC_DCHECK(output_mix_.Get());
274 if (player_object_.Get())
275 return true;
276 RTC_DCHECK(!player_);
277 RTC_DCHECK(!simple_buffer_queue_);
278 RTC_DCHECK(!volume_);
279
280 // source: Android Simple Buffer Queue Data Locator is source.
281 SLDataLocator_AndroidSimpleBufferQueue simple_buffer_queue = {
282 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
283 static_cast<SLuint32>(kNumOfOpenSLESBuffers)};
284 SLDataSource audio_source = {&simple_buffer_queue, &pcm_format_};
285
286 // sink: OutputMix-based data is sink.
287 SLDataLocator_OutputMix locator_output_mix = {SL_DATALOCATOR_OUTPUTMIX,
288 output_mix_.Get()};
289 SLDataSink audio_sink = {&locator_output_mix, nullptr};
290
291 // Define interfaces that we indend to use and realize.
292 const SLInterfaceID interface_ids[] = {SL_IID_ANDROIDCONFIGURATION,
293 SL_IID_BUFFERQUEUE, SL_IID_VOLUME};
294 const SLboolean interface_required[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE,
295 SL_BOOLEAN_TRUE};
296
297 // Create the audio player on the engine interface.
298 RETURN_ON_ERROR(
299 (*engine_)->CreateAudioPlayer(
300 engine_, player_object_.Receive(), &audio_source, &audio_sink,
301 arraysize(interface_ids), interface_ids, interface_required),
302 false);
303
304 // Use the Android configuration interface to set platform-specific
305 // parameters. Should be done before player is realized.
306 SLAndroidConfigurationItf player_config;
307 RETURN_ON_ERROR(
308 player_object_->GetInterface(player_object_.Get(),
309 SL_IID_ANDROIDCONFIGURATION, &player_config),
310 false);
311 // Set audio player configuration to SL_ANDROID_STREAM_VOICE which
312 // corresponds to android.media.AudioManager.STREAM_VOICE_CALL.
313 SLint32 stream_type = SL_ANDROID_STREAM_VOICE;
314 RETURN_ON_ERROR(
315 (*player_config)
316 ->SetConfiguration(player_config, SL_ANDROID_KEY_STREAM_TYPE,
317 &stream_type, sizeof(SLint32)),
318 false);
319
320 // Realize the audio player object after configuration has been set.
321 RETURN_ON_ERROR(
322 player_object_->Realize(player_object_.Get(), SL_BOOLEAN_FALSE), false);
323
324 // Get the SLPlayItf interface on the audio player.
325 RETURN_ON_ERROR(
326 player_object_->GetInterface(player_object_.Get(), SL_IID_PLAY, &player_),
327 false);
328
329 // Get the SLAndroidSimpleBufferQueueItf interface on the audio player.
330 RETURN_ON_ERROR(
331 player_object_->GetInterface(player_object_.Get(), SL_IID_BUFFERQUEUE,
332 &simple_buffer_queue_),
333 false);
334
335 // Register callback method for the Android Simple Buffer Queue interface.
336 // This method will be called when the native audio layer needs audio data.
337 RETURN_ON_ERROR((*simple_buffer_queue_)
338 ->RegisterCallback(simple_buffer_queue_,
339 SimpleBufferQueueCallback, this),
340 false);
341
342 // Get the SLVolumeItf interface on the audio player.
343 RETURN_ON_ERROR(player_object_->GetInterface(player_object_.Get(),
344 SL_IID_VOLUME, &volume_),
345 false);
346
347 // TODO(henrika): might not be required to set volume to max here since it
348 // seems to be default on most devices. Might be required for unit tests.
349 // RETURN_ON_ERROR((*volume_)->SetVolumeLevel(volume_, 0), false);
350
351 return true;
352 }
353
DestroyAudioPlayer()354 void OpenSLESPlayer::DestroyAudioPlayer() {
355 ALOGD("DestroyAudioPlayer");
356 RTC_DCHECK(thread_checker_.IsCurrent());
357 if (!player_object_.Get())
358 return;
359 (*simple_buffer_queue_)
360 ->RegisterCallback(simple_buffer_queue_, nullptr, nullptr);
361 player_object_.Reset();
362 player_ = nullptr;
363 simple_buffer_queue_ = nullptr;
364 volume_ = nullptr;
365 }
366
367 // static
SimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf caller,void * context)368 void OpenSLESPlayer::SimpleBufferQueueCallback(
369 SLAndroidSimpleBufferQueueItf caller,
370 void* context) {
371 OpenSLESPlayer* stream = reinterpret_cast<OpenSLESPlayer*>(context);
372 stream->FillBufferQueue();
373 }
374
FillBufferQueue()375 void OpenSLESPlayer::FillBufferQueue() {
376 RTC_DCHECK(thread_checker_opensles_.IsCurrent());
377 SLuint32 state = GetPlayState();
378 if (state != SL_PLAYSTATE_PLAYING) {
379 ALOGW("Buffer callback in non-playing state!");
380 return;
381 }
382 EnqueuePlayoutData(false);
383 }
384
EnqueuePlayoutData(bool silence)385 void OpenSLESPlayer::EnqueuePlayoutData(bool silence) {
386 // Check delta time between two successive callbacks and provide a warning
387 // if it becomes very large.
388 // TODO(henrika): using 150ms as upper limit but this value is rather random.
389 const uint32_t current_time = rtc::Time();
390 const uint32_t diff = current_time - last_play_time_;
391 if (diff > 150) {
392 ALOGW("Bad OpenSL ES playout timing, dT=%u [ms]", diff);
393 }
394 last_play_time_ = current_time;
395 SLint8* audio_ptr8 =
396 reinterpret_cast<SLint8*>(audio_buffers_[buffer_index_].get());
397 if (silence) {
398 RTC_DCHECK(thread_checker_.IsCurrent());
399 // Avoid acquiring real audio data from WebRTC and fill the buffer with
400 // zeros instead. Used to prime the buffer with silence and to avoid asking
401 // for audio data from two different threads.
402 memset(audio_ptr8, 0, audio_parameters_.GetBytesPerBuffer());
403 } else {
404 RTC_DCHECK(thread_checker_opensles_.IsCurrent());
405 // Read audio data from the WebRTC source using the FineAudioBuffer object
406 // to adjust for differences in buffer size between WebRTC (10ms) and native
407 // OpenSL ES. Use hardcoded delay estimate since OpenSL ES does not support
408 // delay estimation.
409 fine_audio_buffer_->GetPlayoutData(
410 rtc::ArrayView<int16_t>(audio_buffers_[buffer_index_].get(),
411 audio_parameters_.frames_per_buffer() *
412 audio_parameters_.channels()),
413 25);
414 }
415 // Enqueue the decoded audio buffer for playback.
416 SLresult err = (*simple_buffer_queue_)
417 ->Enqueue(simple_buffer_queue_, audio_ptr8,
418 audio_parameters_.GetBytesPerBuffer());
419 if (SL_RESULT_SUCCESS != err) {
420 ALOGE("Enqueue failed: %d", err);
421 }
422 buffer_index_ = (buffer_index_ + 1) % kNumOfOpenSLESBuffers;
423 }
424
GetPlayState() const425 SLuint32 OpenSLESPlayer::GetPlayState() const {
426 RTC_DCHECK(player_);
427 SLuint32 state;
428 SLresult err = (*player_)->GetPlayState(player_, &state);
429 if (SL_RESULT_SUCCESS != err) {
430 ALOGE("GetPlayState failed: %d", err);
431 }
432 return state;
433 }
434
435 } // namespace webrtc
436