1b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika/*
2b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *
4b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *  Use of this source code is governed by a BSD-style license
5b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *  that can be found in the LICENSE file in the root of the source
6b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *  tree. An additional intellectual property rights grant can be found
7b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *  in the file PATENTS.  All contributing project authors may
8b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika *  be found in the AUTHORS file in the root of the source tree.
9b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika */
10b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
11b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#include "webrtc/modules/audio_device/android/opensles_player.h"
12b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
13b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#include <android/log.h>
14b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
15b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#include "webrtc/base/arraysize.h"
16b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#include "webrtc/base/checks.h"
171380e266ff48be9718ce0867cfd65058cb09c5fcPeter Kasting#include "webrtc/base/format_macros.h"
18e71b24e42163afb48f24832ed079a92ee83f01b8henrika#include "webrtc/base/timeutils.h"
19b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#include "webrtc/modules/audio_device/android/audio_manager.h"
2086d907cffda803ee34ee68f9833c1980d1b9f7a6henrika#include "webrtc/modules/audio_device/fine_audio_buffer.h"
21b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
22b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define TAG "OpenSLESPlayer"
23b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
24b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
25b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
26b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
27b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
28b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
29b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#define RETURN_ON_ERROR(op, ...)        \
30b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  do {                                  \
31b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    SLresult err = (op);                \
32b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    if (err != SL_RESULT_SUCCESS) {     \
33b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      ALOGE("%s failed: %d", #op, err); \
34b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      return __VA_ARGS__;               \
35b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    }                                   \
36b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  } while (0)
37b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
38b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikanamespace webrtc {
39b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
40b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaOpenSLESPlayer::OpenSLESPlayer(AudioManager* audio_manager)
41b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    : audio_parameters_(audio_manager->GetPlayoutAudioParameters()),
42b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      audio_device_buffer_(NULL),
43b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      initialized_(false),
44b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      playing_(false),
45b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      bytes_per_buffer_(0),
46b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      buffer_index_(0),
47b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      engine_(nullptr),
48b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      player_(nullptr),
49b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      simple_buffer_queue_(nullptr),
50e71b24e42163afb48f24832ed079a92ee83f01b8henrika      volume_(nullptr),
51e71b24e42163afb48f24832ed079a92ee83f01b8henrika      last_play_time_(0) {
52b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("ctor%s", GetThreadInfo().c_str());
53b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Use native audio output parameters provided by the audio manager and
54b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // define the PCM format structure.
55b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  pcm_format_ = CreatePCMConfiguration(audio_parameters_.channels(),
56b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                       audio_parameters_.sample_rate(),
57b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                       audio_parameters_.bits_per_sample());
58b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Detach from this thread since we want to use the checker to verify calls
59b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // from the internal  audio thread.
60b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  thread_checker_opensles_.DetachFromThread();
61b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
62b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
63b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaOpenSLESPlayer::~OpenSLESPlayer() {
64b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("dtor%s", GetThreadInfo().c_str());
6591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
66b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  Terminate();
67b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  DestroyAudioPlayer();
68b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  DestroyMix();
69b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  DestroyEngine();
7091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!engine_object_.Get());
7191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!engine_);
7291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!output_mix_.Get());
7391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!player_);
7491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!simple_buffer_queue_);
7591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!volume_);
76b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
77b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
78b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::Init() {
79b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("Init%s", GetThreadInfo().c_str());
8091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
81b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return 0;
82b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
83b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
84b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::Terminate() {
85b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("Terminate%s", GetThreadInfo().c_str());
8691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
87b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  StopPlayout();
88b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return 0;
89b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
90b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
91b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::InitPlayout() {
92b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("InitPlayout%s", GetThreadInfo().c_str());
9391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
9491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!initialized_);
9591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!playing_);
96b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  CreateEngine();
97b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  CreateMix();
98b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  initialized_ = true;
99b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  buffer_index_ = 0;
100e71b24e42163afb48f24832ed079a92ee83f01b8henrika  last_play_time_ = rtc::Time();
101b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return 0;
102b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
103b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
104b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::StartPlayout() {
105b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("StartPlayout%s", GetThreadInfo().c_str());
10691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
10791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(initialized_);
10891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!playing_);
109b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // The number of lower latency audio players is limited, hence we create the
110b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // audio player in Start() and destroy it in Stop().
111b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  CreateAudioPlayer();
112b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Fill up audio buffers to avoid initial glitch and to ensure that playback
113b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // starts when mode is later changed to SL_PLAYSTATE_PLAYING.
114b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // TODO(henrika): we can save some delay by only making one call to
115b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // EnqueuePlayoutData. Most likely not worth the risk of adding a glitch.
116b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) {
117b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    EnqueuePlayoutData();
118b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
119b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Start streaming data by setting the play state to SL_PLAYSTATE_PLAYING.
120b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // For a player object, when the object is in the SL_PLAYSTATE_PLAYING
121b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // state, adding buffers will implicitly start playback.
122b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR((*player_)->SetPlayState(player_, SL_PLAYSTATE_PLAYING), -1);
123b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  playing_ = (GetPlayState() == SL_PLAYSTATE_PLAYING);
12491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(playing_);
125b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return 0;
126b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
127b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
128b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::StopPlayout() {
129b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("StopPlayout%s", GetThreadInfo().c_str());
13091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
131b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (!initialized_ || !playing_) {
132b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return 0;
133b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
134b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Stop playing by setting the play state to SL_PLAYSTATE_STOPPED.
135b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR((*player_)->SetPlayState(player_, SL_PLAYSTATE_STOPPED), -1);
136b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Clear the buffer queue to flush out any remaining data.
137b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR((*simple_buffer_queue_)->Clear(simple_buffer_queue_), -1);
138b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#ifndef NDEBUG
139b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Verify that the buffer queue is in fact cleared as it should.
140b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLAndroidSimpleBufferQueueState buffer_queue_state;
141b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  (*simple_buffer_queue_)->GetState(simple_buffer_queue_, &buffer_queue_state);
14291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK_EQ(0u, buffer_queue_state.count);
14391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK_EQ(0u, buffer_queue_state.index);
144b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika#endif
145b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // The number of lower latency audio players is limited, hence we create the
146b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // audio player in Start() and destroy it in Stop().
147b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  DestroyAudioPlayer();
148b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  thread_checker_opensles_.DetachFromThread();
149b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  initialized_ = false;
150b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  playing_ = false;
151b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return 0;
152b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
153b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
154b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::SpeakerVolumeIsAvailable(bool& available) {
155b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  available = false;
156b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return 0;
157b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
158b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
159b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::MaxSpeakerVolume(uint32_t& maxVolume) const {
160b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return -1;
161b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
162b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
163b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::MinSpeakerVolume(uint32_t& minVolume) const {
164b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return -1;
165b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
166b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
167b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::SetSpeakerVolume(uint32_t volume) {
168b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return -1;
169b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
170b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
171b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaint OpenSLESPlayer::SpeakerVolume(uint32_t& volume) const {
172b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return -1;
173b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
174b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
175b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
176b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("AttachAudioBuffer");
17791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
178b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  audio_device_buffer_ = audioBuffer;
179b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  const int sample_rate_hz = audio_parameters_.sample_rate();
180b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("SetPlayoutSampleRate(%d)", sample_rate_hz);
181b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  audio_device_buffer_->SetPlayoutSampleRate(sample_rate_hz);
1826955870806624479723addfae6dcf5d13968796cPeter Kasting  const size_t channels = audio_parameters_.channels();
1836955870806624479723addfae6dcf5d13968796cPeter Kasting  ALOGD("SetPlayoutChannels(%" PRIuS ")", channels);
184b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  audio_device_buffer_->SetPlayoutChannels(channels);
18591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_CHECK(audio_device_buffer_);
186b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  AllocateDataBuffers();
187b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
188b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
1891380e266ff48be9718ce0867cfd65058cb09c5fcPeter KastingSLDataFormat_PCM OpenSLESPlayer::CreatePCMConfiguration(
1906955870806624479723addfae6dcf5d13968796cPeter Kasting    size_t channels,
1911380e266ff48be9718ce0867cfd65058cb09c5fcPeter Kasting    int sample_rate,
1921380e266ff48be9718ce0867cfd65058cb09c5fcPeter Kasting    size_t bits_per_sample) {
193b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("CreatePCMConfiguration");
19491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_CHECK_EQ(bits_per_sample, SL_PCMSAMPLEFORMAT_FIXED_16);
195b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLDataFormat_PCM format;
196b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  format.formatType = SL_DATAFORMAT_PCM;
197b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  format.numChannels = static_cast<SLuint32>(channels);
198b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Note that, the unit of sample rate is actually in milliHertz and not Hertz.
199b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  switch (sample_rate) {
200b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    case 8000:
201b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      format.samplesPerSec = SL_SAMPLINGRATE_8;
202b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      break;
203b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    case 16000:
204b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      format.samplesPerSec = SL_SAMPLINGRATE_16;
205b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      break;
206b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    case 22050:
207b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      format.samplesPerSec = SL_SAMPLINGRATE_22_05;
208b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      break;
209b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    case 32000:
210b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      format.samplesPerSec = SL_SAMPLINGRATE_32;
211b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      break;
212b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    case 44100:
213b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      format.samplesPerSec = SL_SAMPLINGRATE_44_1;
214b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      break;
215b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    case 48000:
216b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      format.samplesPerSec = SL_SAMPLINGRATE_48;
217b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      break;
218b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    default:
21991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      RTC_CHECK(false) << "Unsupported sample rate: " << sample_rate;
220b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
221b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  format.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
222b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  format.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
223b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  format.endianness = SL_BYTEORDER_LITTLEENDIAN;
224b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (format.numChannels == 1)
225b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    format.channelMask = SL_SPEAKER_FRONT_CENTER;
226b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  else if (format.numChannels == 2)
227b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    format.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
228b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  else
22991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    RTC_CHECK(false) << "Unsupported number of channels: "
23091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg                     << format.numChannels;
231b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return format;
232b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
233b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
234b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::AllocateDataBuffers() {
235b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("AllocateDataBuffers");
23691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
23791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!simple_buffer_queue_);
23891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_CHECK(audio_device_buffer_);
239e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // Don't use the lowest possible size as native buffer size. Instead,
240e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // use 10ms to better match the frame size that WebRTC uses. It will result
241e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // in a reduced risk for audio glitches and also in a more "clean" sequence
242e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // of callbacks from the OpenSL ES thread in to WebRTC when asking for audio
243e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // to render.
244e71b24e42163afb48f24832ed079a92ee83f01b8henrika  ALOGD("lowest possible buffer size: %" PRIuS,
245e71b24e42163afb48f24832ed079a92ee83f01b8henrika      audio_parameters_.GetBytesPerBuffer());
246e71b24e42163afb48f24832ed079a92ee83f01b8henrika  bytes_per_buffer_ = audio_parameters_.GetBytesPerFrame() *
247e71b24e42163afb48f24832ed079a92ee83f01b8henrika      audio_parameters_.frames_per_10ms_buffer();
24876a31ca3d4ef10cd19cfac557b783e438907ef01henrika  RTC_DCHECK_GE(bytes_per_buffer_, audio_parameters_.GetBytesPerBuffer());
2491380e266ff48be9718ce0867cfd65058cb09c5fcPeter Kasting  ALOGD("native buffer size: %" PRIuS, bytes_per_buffer_);
250b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Create a modified audio buffer class which allows us to ask for any number
251b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // of samples (and not only multiple of 10ms) to match the native OpenSL ES
252b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // buffer size.
253b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  fine_buffer_.reset(new FineAudioBuffer(audio_device_buffer_,
254b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                         bytes_per_buffer_,
255b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                         audio_parameters_.sample_rate()));
256b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Each buffer must be of this size to avoid unnecessary memcpy while caching
257b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // data between successive callbacks.
25886d907cffda803ee34ee68f9833c1980d1b9f7a6henrika  const size_t required_buffer_size =
25986d907cffda803ee34ee68f9833c1980d1b9f7a6henrika      fine_buffer_->RequiredPlayoutBufferSizeBytes();
2601380e266ff48be9718ce0867cfd65058cb09c5fcPeter Kasting  ALOGD("required buffer size: %" PRIuS, required_buffer_size);
261b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) {
262b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    audio_buffers_[i].reset(new SLint8[required_buffer_size]);
263b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
264b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
265b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
266b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikabool OpenSLESPlayer::CreateEngine() {
267b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("CreateEngine");
26891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
269b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (engine_object_.Get())
270b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return true;
27191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!engine_);
272b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  const SLEngineOption option[] = {
273b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    {SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE)}};
274b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
275b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      slCreateEngine(engine_object_.Receive(), 1, option, 0, NULL, NULL),
276b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      false);
277b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
278b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      engine_object_->Realize(engine_object_.Get(), SL_BOOLEAN_FALSE), false);
279b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(engine_object_->GetInterface(engine_object_.Get(),
280b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                               SL_IID_ENGINE, &engine_),
281b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                  false);
282b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return true;
283b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
284b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
285b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::DestroyEngine() {
286b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("DestroyEngine");
28791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
288b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (!engine_object_.Get())
289b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return;
290b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  engine_ = nullptr;
291b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  engine_object_.Reset();
292b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
293b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
294b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikabool OpenSLESPlayer::CreateMix() {
295b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("CreateMix");
29691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
29791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(engine_);
298b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (output_mix_.Get())
299b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return true;
300b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
301b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Create the ouput mix on the engine object. No interfaces will be used.
302b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR((*engine_)->CreateOutputMix(engine_, output_mix_.Receive(), 0,
303b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                              NULL, NULL),
304b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                  false);
305b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(output_mix_->Realize(output_mix_.Get(), SL_BOOLEAN_FALSE),
306b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                  false);
307b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return true;
308b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
309b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
310b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::DestroyMix() {
311b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("DestroyMix");
31291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
313b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (!output_mix_.Get())
314b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return;
315b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  output_mix_.Reset();
316b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
317b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
318b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikabool OpenSLESPlayer::CreateAudioPlayer() {
319b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("CreateAudioPlayer");
32091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
32191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(engine_object_.Get());
32291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(output_mix_.Get());
323b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (player_object_.Get())
324b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return true;
32591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!player_);
32691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!simple_buffer_queue_);
32791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(!volume_);
328b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
329b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // source: Android Simple Buffer Queue Data Locator is source.
330b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLDataLocator_AndroidSimpleBufferQueue simple_buffer_queue = {
331b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
332b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      static_cast<SLuint32>(kNumOfOpenSLESBuffers)};
333b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLDataSource audio_source = {&simple_buffer_queue, &pcm_format_};
334b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
335b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // sink: OutputMix-based data is sink.
336b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLDataLocator_OutputMix locator_output_mix = {SL_DATALOCATOR_OUTPUTMIX,
337b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                                output_mix_.Get()};
338b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLDataSink audio_sink = {&locator_output_mix, NULL};
339b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
340b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Define interfaces that we indend to use and realize.
341b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  const SLInterfaceID interface_ids[] = {
342b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      SL_IID_ANDROIDCONFIGURATION, SL_IID_BUFFERQUEUE, SL_IID_VOLUME};
343b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  const SLboolean interface_required[] = {
344b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
345b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
346b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Create the audio player on the engine interface.
347b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
348b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      (*engine_)->CreateAudioPlayer(
349b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika          engine_, player_object_.Receive(), &audio_source, &audio_sink,
350b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika          arraysize(interface_ids), interface_ids, interface_required),
351b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      false);
352b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
353b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Use the Android configuration interface to set platform-specific
354b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // parameters. Should be done before player is realized.
355b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLAndroidConfigurationItf player_config;
356b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
357b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      player_object_->GetInterface(player_object_.Get(),
358b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                   SL_IID_ANDROIDCONFIGURATION, &player_config),
359b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      false);
360b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Set audio player configuration to SL_ANDROID_STREAM_VOICE which
361b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // corresponds to android.media.AudioManager.STREAM_VOICE_CALL.
3621ba936a807b12d887b7985317e4205b80f1120b2henrika  SLint32 stream_type = SL_ANDROID_STREAM_VOICE;
363b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
364b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      (*player_config)
365b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika          ->SetConfiguration(player_config, SL_ANDROID_KEY_STREAM_TYPE,
366b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                             &stream_type, sizeof(SLint32)),
367b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      false);
368b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
369b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Realize the audio player object after configuration has been set.
370b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
371b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      player_object_->Realize(player_object_.Get(), SL_BOOLEAN_FALSE), false);
372b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
373b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Get the SLPlayItf interface on the audio player.
374b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
375b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      player_object_->GetInterface(player_object_.Get(), SL_IID_PLAY, &player_),
376b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      false);
377b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
378b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Get the SLAndroidSimpleBufferQueueItf interface on the audio player.
379b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(
380b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      player_object_->GetInterface(player_object_.Get(), SL_IID_BUFFERQUEUE,
381b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                   &simple_buffer_queue_),
382b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      false);
383b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
384b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Register callback method for the Android Simple Buffer Queue interface.
385b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // This method will be called when the native audio layer needs audio data.
386b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR((*simple_buffer_queue_)
387b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                      ->RegisterCallback(simple_buffer_queue_,
388b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                         SimpleBufferQueueCallback, this),
389b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                  false);
390b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
391b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Get the SLVolumeItf interface on the audio player.
392b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  RETURN_ON_ERROR(player_object_->GetInterface(player_object_.Get(),
393b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                                               SL_IID_VOLUME, &volume_),
394b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika                  false);
395b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
396b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // TODO(henrika): might not be required to set volume to max here since it
397b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // seems to be default on most devices. Might be required for unit tests.
398b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // RETURN_ON_ERROR((*volume_)->SetVolumeLevel(volume_, 0), false);
399b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
400b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return true;
401b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
402b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
403b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::DestroyAudioPlayer() {
404b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  ALOGD("DestroyAudioPlayer");
40591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_.CalledOnValidThread());
406b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (!player_object_.Get())
407b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return;
408b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  player_object_.Reset();
409b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  player_ = nullptr;
410b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  simple_buffer_queue_ = nullptr;
411b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  volume_ = nullptr;
412b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
413b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
414b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika// static
415b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::SimpleBufferQueueCallback(
416b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    SLAndroidSimpleBufferQueueItf caller,
417b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    void* context) {
418b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  OpenSLESPlayer* stream = reinterpret_cast<OpenSLESPlayer*>(context);
419b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  stream->FillBufferQueue();
420b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
421b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
422b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::FillBufferQueue() {
42391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(thread_checker_opensles_.CalledOnValidThread());
424b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLuint32 state = GetPlayState();
425b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (state != SL_PLAYSTATE_PLAYING) {
426b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    ALOGW("Buffer callback in non-playing state!");
427b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    return;
428b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
429b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  EnqueuePlayoutData();
430b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
431b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
432b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikavoid OpenSLESPlayer::EnqueuePlayoutData() {
433e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // Check delta time between two successive callbacks and provide a warning
434e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // if it becomes very large.
435e71b24e42163afb48f24832ed079a92ee83f01b8henrika  // TODO(henrika): using 100ms as upper limit but this value is rather random.
436e71b24e42163afb48f24832ed079a92ee83f01b8henrika  const uint32_t current_time = rtc::Time();
437e71b24e42163afb48f24832ed079a92ee83f01b8henrika  const uint32_t diff = current_time - last_play_time_;
438e71b24e42163afb48f24832ed079a92ee83f01b8henrika  if (diff > 100) {
439e71b24e42163afb48f24832ed079a92ee83f01b8henrika    ALOGW("Bad OpenSL ES playout timing, dT=%u [ms]", diff);
440e71b24e42163afb48f24832ed079a92ee83f01b8henrika  }
441e71b24e42163afb48f24832ed079a92ee83f01b8henrika  last_play_time_ = current_time;
442b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Read audio data from the WebRTC source using the FineAudioBuffer object
443b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // to adjust for differences in buffer size between WebRTC (10ms) and native
444b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // OpenSL ES.
445b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLint8* audio_ptr = audio_buffers_[buffer_index_].get();
44686d907cffda803ee34ee68f9833c1980d1b9f7a6henrika  fine_buffer_->GetPlayoutData(audio_ptr);
447b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  // Enqueue the decoded audio buffer for playback.
448b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLresult err =
449b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika      (*simple_buffer_queue_)
450b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika          ->Enqueue(simple_buffer_queue_, audio_ptr, bytes_per_buffer_);
451b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (SL_RESULT_SUCCESS != err) {
452b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    ALOGE("Enqueue failed: %d", err);
453b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
454b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  buffer_index_ = (buffer_index_ + 1) % kNumOfOpenSLESBuffers;
455b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
456b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
457b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrikaSLuint32 OpenSLESPlayer::GetPlayState() const {
45891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_DCHECK(player_);
459b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLuint32 state;
460b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  SLresult err = (*player_)->GetPlayState(player_, &state);
461b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  if (SL_RESULT_SUCCESS != err) {
462b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika    ALOGE("GetPlayState failed: %d", err);
463b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  }
464b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika  return state;
465b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}
466b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika
467b26198972c1fcb4aa7abaf3895b007e301e7d5dchenrika}  // namespace webrtc
468