henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_device/android/opensles_player.h" |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 12 | |
| 13 | #include <android/log.h> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/array_view.h" |
| 16 | #include "modules/audio_device/android/audio_common.h" |
| 17 | #include "modules/audio_device/android/audio_manager.h" |
| 18 | #include "modules/audio_device/fine_audio_buffer.h" |
| 19 | #include "rtc_base/arraysize.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/format_macros.h" |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 22 | #include "rtc_base/platform_thread.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/timeutils.h" |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 24 | |
| 25 | #define TAG "OpenSLESPlayer" |
| 26 | #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__) |
| 27 | #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) |
| 28 | #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) |
| 29 | #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__) |
| 30 | #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) |
| 31 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 32 | #define RETURN_ON_ERROR(op, ...) \ |
| 33 | do { \ |
| 34 | SLresult err = (op); \ |
| 35 | if (err != SL_RESULT_SUCCESS) { \ |
| 36 | ALOGE("%s failed: %s", #op, GetSLErrorString(err)); \ |
| 37 | return __VA_ARGS__; \ |
| 38 | } \ |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 39 | } while (0) |
| 40 | |
| 41 | namespace webrtc { |
| 42 | |
| 43 | OpenSLESPlayer::OpenSLESPlayer(AudioManager* audio_manager) |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 44 | : audio_manager_(audio_manager), |
| 45 | audio_parameters_(audio_manager->GetPlayoutAudioParameters()), |
| 46 | audio_device_buffer_(nullptr), |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 47 | initialized_(false), |
| 48 | playing_(false), |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 49 | buffer_index_(0), |
| 50 | engine_(nullptr), |
| 51 | player_(nullptr), |
| 52 | simple_buffer_queue_(nullptr), |
henrika | e71b24e | 2015-11-12 01:48:32 -0800 | [diff] [blame] | 53 | volume_(nullptr), |
| 54 | last_play_time_(0) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 55 | ALOGD("ctor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 56 | // Use native audio output parameters provided by the audio manager and |
| 57 | // define the PCM format structure. |
| 58 | pcm_format_ = CreatePCMConfiguration(audio_parameters_.channels(), |
| 59 | audio_parameters_.sample_rate(), |
| 60 | audio_parameters_.bits_per_sample()); |
| 61 | // Detach from this thread since we want to use the checker to verify calls |
| 62 | // from the internal audio thread. |
| 63 | thread_checker_opensles_.DetachFromThread(); |
| 64 | } |
| 65 | |
| 66 | OpenSLESPlayer::~OpenSLESPlayer() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 67 | ALOGD("dtor[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 68 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 69 | Terminate(); |
| 70 | DestroyAudioPlayer(); |
| 71 | DestroyMix(); |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 72 | engine_ = nullptr; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 73 | RTC_DCHECK(!engine_); |
| 74 | RTC_DCHECK(!output_mix_.Get()); |
| 75 | RTC_DCHECK(!player_); |
| 76 | RTC_DCHECK(!simple_buffer_queue_); |
| 77 | RTC_DCHECK(!volume_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int OpenSLESPlayer::Init() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 81 | ALOGD("Init[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 82 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 76535de | 2017-09-11 01:25:55 -0700 | [diff] [blame] | 83 | if (audio_parameters_.channels() == 2) { |
| 84 | // TODO(henrika): FineAudioBuffer needs more work to support stereo. |
| 85 | ALOGE("OpenSLESPlayer does not support stereo"); |
| 86 | return -1; |
| 87 | } |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int OpenSLESPlayer::Terminate() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 92 | ALOGD("Terminate[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 93 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 94 | StopPlayout(); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int OpenSLESPlayer::InitPlayout() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 99 | ALOGD("InitPlayout[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 100 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 | RTC_DCHECK(!initialized_); |
| 102 | RTC_DCHECK(!playing_); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 103 | if (!ObtainEngineInterface()) { |
| 104 | ALOGE("Failed to obtain SL Engine interface"); |
| 105 | return -1; |
| 106 | } |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 107 | CreateMix(); |
| 108 | initialized_ = true; |
| 109 | buffer_index_ = 0; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int OpenSLESPlayer::StartPlayout() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 114 | ALOGD("StartPlayout[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 115 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 | RTC_DCHECK(initialized_); |
| 117 | RTC_DCHECK(!playing_); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 118 | if (fine_audio_buffer_) { |
| 119 | fine_audio_buffer_->ResetPlayout(); |
| 120 | } |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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. |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 128 | last_play_time_ = rtc::Time(); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 129 | for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) { |
henrika | 14acf65 | 2016-10-11 06:15:41 -0700 | [diff] [blame] | 130 | EnqueuePlayoutData(true); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 137 | RTC_DCHECK(playing_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int OpenSLESPlayer::StopPlayout() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame^] | 142 | ALOGD("StopPlayout[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 143 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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); |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 151 | #if RTC_DCHECK_IS_ON |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 155 | RTC_DCHECK_EQ(0, buffer_queue_state.count); |
| 156 | RTC_DCHECK_EQ(0, buffer_queue_state.index); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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_.DetachFromThread(); |
| 162 | initialized_ = false; |
| 163 | playing_ = false; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int OpenSLESPlayer::SpeakerVolumeIsAvailable(bool& available) { |
| 168 | available = false; |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | int OpenSLESPlayer::MaxSpeakerVolume(uint32_t& maxVolume) const { |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | int OpenSLESPlayer::MinSpeakerVolume(uint32_t& minVolume) const { |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | int OpenSLESPlayer::SetSpeakerVolume(uint32_t volume) { |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | int OpenSLESPlayer::SpeakerVolume(uint32_t& volume) const { |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | void OpenSLESPlayer::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) { |
| 189 | ALOGD("AttachAudioBuffer"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 190 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 195 | const size_t channels = audio_parameters_.channels(); |
| 196 | ALOGD("SetPlayoutChannels(%" PRIuS ")", channels); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 197 | audio_device_buffer_->SetPlayoutChannels(channels); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 198 | RTC_CHECK(audio_device_buffer_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 199 | AllocateDataBuffers(); |
| 200 | } |
| 201 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 202 | void OpenSLESPlayer::AllocateDataBuffers() { |
| 203 | ALOGD("AllocateDataBuffers"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 204 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 205 | RTC_DCHECK(!simple_buffer_queue_); |
| 206 | RTC_CHECK(audio_device_buffer_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 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 |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 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. |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 215 | const size_t buffer_size_in_bytes = audio_parameters_.GetBytesPerBuffer(); |
| 216 | ALOGD("native buffer size: %" PRIuS, buffer_size_in_bytes); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 217 | ALOGD("native buffer size in ms: %.2f", |
| 218 | audio_parameters_.GetBufferSizeInMilliseconds()); |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 219 | fine_audio_buffer_.reset(new FineAudioBuffer(audio_device_buffer_, |
| 220 | audio_parameters_.sample_rate(), |
| 221 | 2 * buffer_size_in_bytes)); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 222 | // Allocated memory for audio buffers. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 223 | for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 224 | audio_buffers_[i].reset(new SLint8[buffer_size_in_bytes]); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 228 | bool OpenSLESPlayer::ObtainEngineInterface() { |
| 229 | ALOGD("ObtainEngineInterface"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 230 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 231 | if (engine_) |
| 232 | return true; |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 233 | // Get access to (or create if not already existing) the global OpenSL Engine |
| 234 | // object. |
| 235 | SLObjectItf engine_object = audio_manager_->GetOpenSLEngine(); |
| 236 | if (engine_object == nullptr) { |
| 237 | ALOGE("Failed to access the global OpenSL engine"); |
| 238 | return false; |
| 239 | } |
| 240 | // Get the SL Engine Interface which is implicit. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 241 | RETURN_ON_ERROR( |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 242 | (*engine_object)->GetInterface(engine_object, SL_IID_ENGINE, &engine_), |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 243 | false); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 244 | return true; |
| 245 | } |
| 246 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 247 | bool OpenSLESPlayer::CreateMix() { |
| 248 | ALOGD("CreateMix"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 249 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 250 | RTC_DCHECK(engine_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 251 | if (output_mix_.Get()) |
| 252 | return true; |
| 253 | |
| 254 | // Create the ouput mix on the engine object. No interfaces will be used. |
| 255 | RETURN_ON_ERROR((*engine_)->CreateOutputMix(engine_, output_mix_.Receive(), 0, |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 256 | nullptr, nullptr), |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 257 | false); |
| 258 | RETURN_ON_ERROR(output_mix_->Realize(output_mix_.Get(), SL_BOOLEAN_FALSE), |
| 259 | false); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | void OpenSLESPlayer::DestroyMix() { |
| 264 | ALOGD("DestroyMix"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 265 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 266 | if (!output_mix_.Get()) |
| 267 | return; |
| 268 | output_mix_.Reset(); |
| 269 | } |
| 270 | |
| 271 | bool OpenSLESPlayer::CreateAudioPlayer() { |
| 272 | ALOGD("CreateAudioPlayer"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 273 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 274 | RTC_DCHECK(output_mix_.Get()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 275 | if (player_object_.Get()) |
| 276 | return true; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 277 | RTC_DCHECK(!player_); |
| 278 | RTC_DCHECK(!simple_buffer_queue_); |
| 279 | RTC_DCHECK(!volume_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 280 | |
| 281 | // source: Android Simple Buffer Queue Data Locator is source. |
| 282 | SLDataLocator_AndroidSimpleBufferQueue simple_buffer_queue = { |
| 283 | SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, |
| 284 | static_cast<SLuint32>(kNumOfOpenSLESBuffers)}; |
| 285 | SLDataSource audio_source = {&simple_buffer_queue, &pcm_format_}; |
| 286 | |
| 287 | // sink: OutputMix-based data is sink. |
| 288 | SLDataLocator_OutputMix locator_output_mix = {SL_DATALOCATOR_OUTPUTMIX, |
| 289 | output_mix_.Get()}; |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 290 | SLDataSink audio_sink = {&locator_output_mix, nullptr}; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 291 | |
| 292 | // Define interfaces that we indend to use and realize. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 293 | const SLInterfaceID interface_ids[] = {SL_IID_ANDROIDCONFIGURATION, |
| 294 | SL_IID_BUFFERQUEUE, SL_IID_VOLUME}; |
| 295 | const SLboolean interface_required[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, |
| 296 | SL_BOOLEAN_TRUE}; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 297 | |
| 298 | // Create the audio player on the engine interface. |
| 299 | RETURN_ON_ERROR( |
| 300 | (*engine_)->CreateAudioPlayer( |
| 301 | engine_, player_object_.Receive(), &audio_source, &audio_sink, |
| 302 | arraysize(interface_ids), interface_ids, interface_required), |
| 303 | false); |
| 304 | |
| 305 | // Use the Android configuration interface to set platform-specific |
| 306 | // parameters. Should be done before player is realized. |
| 307 | SLAndroidConfigurationItf player_config; |
| 308 | RETURN_ON_ERROR( |
| 309 | player_object_->GetInterface(player_object_.Get(), |
| 310 | SL_IID_ANDROIDCONFIGURATION, &player_config), |
| 311 | false); |
| 312 | // Set audio player configuration to SL_ANDROID_STREAM_VOICE which |
| 313 | // corresponds to android.media.AudioManager.STREAM_VOICE_CALL. |
henrika | 1ba936a | 2015-11-03 04:27:58 -0800 | [diff] [blame] | 314 | SLint32 stream_type = SL_ANDROID_STREAM_VOICE; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 315 | RETURN_ON_ERROR( |
| 316 | (*player_config) |
| 317 | ->SetConfiguration(player_config, SL_ANDROID_KEY_STREAM_TYPE, |
| 318 | &stream_type, sizeof(SLint32)), |
| 319 | false); |
| 320 | |
| 321 | // Realize the audio player object after configuration has been set. |
| 322 | RETURN_ON_ERROR( |
| 323 | player_object_->Realize(player_object_.Get(), SL_BOOLEAN_FALSE), false); |
| 324 | |
| 325 | // Get the SLPlayItf interface on the audio player. |
| 326 | RETURN_ON_ERROR( |
| 327 | player_object_->GetInterface(player_object_.Get(), SL_IID_PLAY, &player_), |
| 328 | false); |
| 329 | |
| 330 | // Get the SLAndroidSimpleBufferQueueItf interface on the audio player. |
| 331 | RETURN_ON_ERROR( |
| 332 | player_object_->GetInterface(player_object_.Get(), SL_IID_BUFFERQUEUE, |
| 333 | &simple_buffer_queue_), |
| 334 | false); |
| 335 | |
| 336 | // Register callback method for the Android Simple Buffer Queue interface. |
| 337 | // This method will be called when the native audio layer needs audio data. |
| 338 | RETURN_ON_ERROR((*simple_buffer_queue_) |
| 339 | ->RegisterCallback(simple_buffer_queue_, |
| 340 | SimpleBufferQueueCallback, this), |
| 341 | false); |
| 342 | |
| 343 | // Get the SLVolumeItf interface on the audio player. |
| 344 | RETURN_ON_ERROR(player_object_->GetInterface(player_object_.Get(), |
| 345 | SL_IID_VOLUME, &volume_), |
| 346 | false); |
| 347 | |
| 348 | // TODO(henrika): might not be required to set volume to max here since it |
| 349 | // seems to be default on most devices. Might be required for unit tests. |
| 350 | // RETURN_ON_ERROR((*volume_)->SetVolumeLevel(volume_, 0), false); |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | void OpenSLESPlayer::DestroyAudioPlayer() { |
| 356 | ALOGD("DestroyAudioPlayer"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 357 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 358 | if (!player_object_.Get()) |
| 359 | return; |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 360 | (*simple_buffer_queue_) |
| 361 | ->RegisterCallback(simple_buffer_queue_, nullptr, nullptr); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 362 | player_object_.Reset(); |
| 363 | player_ = nullptr; |
| 364 | simple_buffer_queue_ = nullptr; |
| 365 | volume_ = nullptr; |
| 366 | } |
| 367 | |
| 368 | // static |
| 369 | void OpenSLESPlayer::SimpleBufferQueueCallback( |
| 370 | SLAndroidSimpleBufferQueueItf caller, |
| 371 | void* context) { |
| 372 | OpenSLESPlayer* stream = reinterpret_cast<OpenSLESPlayer*>(context); |
| 373 | stream->FillBufferQueue(); |
| 374 | } |
| 375 | |
| 376 | void OpenSLESPlayer::FillBufferQueue() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 377 | RTC_DCHECK(thread_checker_opensles_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 378 | SLuint32 state = GetPlayState(); |
| 379 | if (state != SL_PLAYSTATE_PLAYING) { |
| 380 | ALOGW("Buffer callback in non-playing state!"); |
| 381 | return; |
| 382 | } |
henrika | 14acf65 | 2016-10-11 06:15:41 -0700 | [diff] [blame] | 383 | EnqueuePlayoutData(false); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 384 | } |
| 385 | |
henrika | 14acf65 | 2016-10-11 06:15:41 -0700 | [diff] [blame] | 386 | void OpenSLESPlayer::EnqueuePlayoutData(bool silence) { |
henrika | e71b24e | 2015-11-12 01:48:32 -0800 | [diff] [blame] | 387 | // Check delta time between two successive callbacks and provide a warning |
| 388 | // if it becomes very large. |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 389 | // TODO(henrika): using 150ms as upper limit but this value is rather random. |
henrika | e71b24e | 2015-11-12 01:48:32 -0800 | [diff] [blame] | 390 | const uint32_t current_time = rtc::Time(); |
| 391 | const uint32_t diff = current_time - last_play_time_; |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 392 | if (diff > 150) { |
henrika | e71b24e | 2015-11-12 01:48:32 -0800 | [diff] [blame] | 393 | ALOGW("Bad OpenSL ES playout timing, dT=%u [ms]", diff); |
| 394 | } |
| 395 | last_play_time_ = current_time; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 396 | SLint8* audio_ptr = audio_buffers_[buffer_index_].get(); |
henrika | 14acf65 | 2016-10-11 06:15:41 -0700 | [diff] [blame] | 397 | if (silence) { |
| 398 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 399 | // Avoid aquiring 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_ptr, 0, audio_parameters_.GetBytesPerBuffer()); |
| 403 | } else { |
| 404 | RTC_DCHECK(thread_checker_opensles_.CalledOnValidThread()); |
| 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. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 408 | fine_audio_buffer_->GetPlayoutData(rtc::ArrayView<SLint8>( |
| 409 | audio_ptr, audio_parameters_.GetBytesPerBuffer())); |
henrika | 14acf65 | 2016-10-11 06:15:41 -0700 | [diff] [blame] | 410 | } |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 411 | // Enqueue the decoded audio buffer for playback. |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 412 | SLresult err = (*simple_buffer_queue_) |
| 413 | ->Enqueue(simple_buffer_queue_, audio_ptr, |
| 414 | audio_parameters_.GetBytesPerBuffer()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 415 | if (SL_RESULT_SUCCESS != err) { |
| 416 | ALOGE("Enqueue failed: %d", err); |
| 417 | } |
| 418 | buffer_index_ = (buffer_index_ + 1) % kNumOfOpenSLESBuffers; |
| 419 | } |
| 420 | |
| 421 | SLuint32 OpenSLESPlayer::GetPlayState() const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 422 | RTC_DCHECK(player_); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 423 | SLuint32 state; |
| 424 | SLresult err = (*player_)->GetPlayState(player_, &state); |
| 425 | if (SL_RESULT_SUCCESS != err) { |
| 426 | ALOGE("GetPlayState failed: %d", err); |
| 427 | } |
| 428 | return state; |
| 429 | } |
| 430 | |
| 431 | } // namespace webrtc |