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