2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_device/android/opensles_input.h"
15 #include "webrtc/modules/audio_device/android/audio_common.h"
16 #include "webrtc/modules/audio_device/android/opensles_common.h"
17 #include "webrtc/modules/audio_device/android/single_rw_fifo.h"
18 #include "webrtc/modules/audio_device/audio_device_buffer.h"
19 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
21 #include "webrtc/system_wrappers/interface/trace.h"
24 #define OPENSL_RETURN_ON_FAILURE(op, ret_val) \
26 SLresult err = (op); \
27 if (err != SL_RESULT_SUCCESS) { \
28 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, id_, \
29 "OpenSL error: %d", err); \
35 static const SLEngineOption kOption[] = {
36 { SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE) },
46 OpenSlesInput::OpenSlesInput(
47 const int32_t id, PlayoutDelayProvider* delay_provider)
49 delay_provider_(delay_provider),
51 mic_initialized_(false),
52 rec_initialized_(false),
53 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
55 num_fifo_buffers_needed_(0),
58 sles_engine_itf_(NULL),
60 sles_recorder_itf_(NULL),
61 sles_recorder_sbq_itf_(NULL),
64 rec_sampling_rate_(0),
69 OpenSlesInput::~OpenSlesInput() {
72 int32_t OpenSlesInput::SetAndroidAudioDeviceObjects(void* javaVM,
78 void OpenSlesInput::ClearAndroidAudioDeviceObjects() {
81 int32_t OpenSlesInput::Init() {
82 assert(!initialized_);
84 // Set up OpenSL engine.
85 OPENSL_RETURN_ON_FAILURE(slCreateEngine(&sles_engine_, 1, kOption, 0,
88 OPENSL_RETURN_ON_FAILURE((*sles_engine_)->Realize(sles_engine_,
91 OPENSL_RETURN_ON_FAILURE((*sles_engine_)->GetInterface(sles_engine_,
96 if (InitSampleRate() != 0) {
104 int32_t OpenSlesInput::Terminate() {
105 // It is assumed that the caller has stopped recording before terminating.
107 (*sles_engine_)->Destroy(sles_engine_);
108 initialized_ = false;
109 mic_initialized_ = false;
110 rec_initialized_ = false;
114 int32_t OpenSlesInput::RecordingDeviceName(uint16_t index,
115 char name[kAdmMaxDeviceNameSize],
116 char guid[kAdmMaxGuidSize]) {
124 int32_t OpenSlesInput::SetRecordingDevice(uint16_t index) {
129 int32_t OpenSlesInput::RecordingIsAvailable(bool& available) { // NOLINT
134 int32_t OpenSlesInput::InitRecording() {
135 assert(initialized_);
136 rec_initialized_ = true;
140 int32_t OpenSlesInput::StartRecording() {
141 assert(rec_initialized_);
143 if (!CreateAudioRecorder()) {
146 // Setup to receive buffer queue event callbacks.
147 OPENSL_RETURN_ON_FAILURE(
148 (*sles_recorder_sbq_itf_)->RegisterCallback(
149 sles_recorder_sbq_itf_,
150 RecorderSimpleBufferQueueCallback,
154 if (!EnqueueAllBuffers()) {
159 // To prevent the compiler from e.g. optimizing the code to
160 // recording_ = StartCbThreads() which wouldn't have been thread safe.
161 CriticalSectionScoped lock(crit_sect_.get());
164 if (!StartCbThreads()) {
171 int32_t OpenSlesInput::StopRecording() {
173 DestroyAudioRecorder();
178 int32_t OpenSlesInput::SetAGC(bool enable) {
179 agc_enabled_ = enable;
183 int32_t OpenSlesInput::InitMicrophone() {
184 assert(initialized_);
186 mic_initialized_ = true;
190 int32_t OpenSlesInput::MicrophoneVolumeIsAvailable(bool& available) { // NOLINT
195 int32_t OpenSlesInput::MinMicrophoneVolume(
196 uint32_t& minVolume) const { // NOLINT
201 int32_t OpenSlesInput::MicrophoneVolumeStepSize(
202 uint16_t& stepSize) const {
207 int32_t OpenSlesInput::MicrophoneMuteIsAvailable(bool& available) { // NOLINT
208 available = false; // Mic mute not supported on Android
212 int32_t OpenSlesInput::MicrophoneBoostIsAvailable(bool& available) { // NOLINT
213 available = false; // Mic boost not supported on Android.
217 int32_t OpenSlesInput::SetMicrophoneBoost(bool enable) {
219 return -1; // Not supported
222 int32_t OpenSlesInput::MicrophoneBoost(bool& enabled) const { // NOLINT
224 return -1; // Not supported
227 int32_t OpenSlesInput::StereoRecordingIsAvailable(bool& available) { // NOLINT
228 available = false; // Stereo recording not supported on Android.
232 int32_t OpenSlesInput::StereoRecording(bool& enabled) const { // NOLINT
237 int32_t OpenSlesInput::RecordingDelay(uint16_t& delayMS) const { // NOLINT
238 delayMS = recording_delay_;
242 void OpenSlesInput::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
243 audio_buffer_ = audioBuffer;
246 int OpenSlesInput::InitSampleRate() {
248 audio_buffer_->SetRecordingSampleRate(rec_sampling_rate_);
249 audio_buffer_->SetRecordingChannels(kNumChannels);
250 UpdateRecordingDelay();
254 int OpenSlesInput::buffer_size_samples() const {
255 // Since there is no low latency recording, use buffer size corresponding to
256 // 10ms of data since that's the framesize WebRTC uses. Getting any other
257 // size would require patching together buffers somewhere before passing them
259 return rec_sampling_rate_ * 10 / 1000;
262 int OpenSlesInput::buffer_size_bytes() const {
263 return buffer_size_samples() * kNumChannels * sizeof(int16_t);
266 void OpenSlesInput::UpdateRecordingDelay() {
267 // TODO(hellner): Add accurate delay estimate.
268 // On average half the current buffer will have been filled with audio.
269 int outstanding_samples =
270 (TotalBuffersUsed() - 0.5) * buffer_size_samples();
271 recording_delay_ = outstanding_samples / (rec_sampling_rate_ / 1000);
274 void OpenSlesInput::UpdateSampleRate() {
275 rec_sampling_rate_ = audio_manager_.low_latency_supported() ?
276 audio_manager_.native_output_sample_rate() : kDefaultSampleRate;
279 void OpenSlesInput::CalculateNumFifoBuffersNeeded() {
280 // Buffer size is 10ms of data.
281 num_fifo_buffers_needed_ = kNum10MsToBuffer;
284 void OpenSlesInput::AllocateBuffers() {
285 // Allocate FIFO to handle passing buffers between processing and OpenSL
287 CalculateNumFifoBuffersNeeded();
288 assert(num_fifo_buffers_needed_ > 0);
289 fifo_.reset(new SingleRwFifo(num_fifo_buffers_needed_));
291 // Allocate the memory area to be used.
292 rec_buf_.reset(new scoped_ptr<int8_t[]>[TotalBuffersUsed()]);
293 for (int i = 0; i < TotalBuffersUsed(); ++i) {
294 rec_buf_[i].reset(new int8_t[buffer_size_bytes()]);
298 int OpenSlesInput::TotalBuffersUsed() const {
299 return num_fifo_buffers_needed_ + kNumOpenSlBuffers;
302 bool OpenSlesInput::EnqueueAllBuffers() {
304 number_overruns_ = 0;
305 for (int i = 0; i < kNumOpenSlBuffers; ++i) {
306 memset(rec_buf_[i].get(), 0, buffer_size_bytes());
307 OPENSL_RETURN_ON_FAILURE(
308 (*sles_recorder_sbq_itf_)->Enqueue(
309 sles_recorder_sbq_itf_,
310 reinterpret_cast<void*>(rec_buf_[i].get()),
311 buffer_size_bytes()),
314 // In case of underrun the fifo will be at capacity. In case of first enqueue
315 // no audio can have been returned yet meaning fifo must be empty. Any other
316 // values are unexpected.
317 assert(fifo_->size() == fifo_->capacity() ||
319 // OpenSL recording has been stopped. I.e. only this thread is touching
321 while (fifo_->size() != 0) {
328 bool OpenSlesInput::CreateAudioRecorder() {
329 if (!event_.Start()) {
333 SLDataLocator_IODevice micLocator = {
334 SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
335 SL_DEFAULTDEVICEID_AUDIOINPUT, NULL };
336 SLDataSource audio_source = { &micLocator, NULL };
338 SLDataLocator_AndroidSimpleBufferQueue simple_buf_queue = {
339 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
340 static_cast<SLuint32>(TotalBuffersUsed())
342 SLDataFormat_PCM configuration =
343 webrtc_opensl::CreatePcmConfiguration(rec_sampling_rate_);
344 SLDataSink audio_sink = { &simple_buf_queue, &configuration };
346 // Interfaces for recording android audio data and Android are needed.
347 // Note the interfaces still need to be initialized. This only tells OpenSl
348 // that the interfaces will be needed at some point.
349 const SLInterfaceID id[kNumInterfaces] = {
350 SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
351 const SLboolean req[kNumInterfaces] = {
352 SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
353 OPENSL_RETURN_ON_FAILURE(
354 (*sles_engine_itf_)->CreateAudioRecorder(sles_engine_itf_,
363 SLAndroidConfigurationItf recorder_config;
364 OPENSL_RETURN_ON_FAILURE(
365 (*sles_recorder_)->GetInterface(sles_recorder_,
366 SL_IID_ANDROIDCONFIGURATION,
370 // Set audio recorder configuration to
371 // SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION which ensures that we
372 // use the main microphone tuned for audio communications.
373 SLint32 stream_type = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
374 OPENSL_RETURN_ON_FAILURE(
375 (*recorder_config)->SetConfiguration(recorder_config,
376 SL_ANDROID_KEY_RECORDING_PRESET,
381 // Realize the recorder in synchronous mode.
382 OPENSL_RETURN_ON_FAILURE((*sles_recorder_)->Realize(sles_recorder_,
385 OPENSL_RETURN_ON_FAILURE(
386 (*sles_recorder_)->GetInterface(sles_recorder_, SL_IID_RECORD,
387 static_cast<void*>(&sles_recorder_itf_)),
389 OPENSL_RETURN_ON_FAILURE(
390 (*sles_recorder_)->GetInterface(
392 SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
393 static_cast<void*>(&sles_recorder_sbq_itf_)),
398 void OpenSlesInput::DestroyAudioRecorder() {
400 if (sles_recorder_sbq_itf_) {
401 // Release all buffers currently queued up.
402 OPENSL_RETURN_ON_FAILURE(
403 (*sles_recorder_sbq_itf_)->Clear(sles_recorder_sbq_itf_),
405 sles_recorder_sbq_itf_ = NULL;
407 sles_recorder_itf_ = NULL;
409 if (sles_recorder_) {
410 (*sles_recorder_)->Destroy(sles_recorder_);
411 sles_recorder_ = NULL;
415 bool OpenSlesInput::HandleOverrun(int event_id, int event_msg) {
419 if (event_id == kNoOverrun) {
422 WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, id_, "Audio overrun");
423 assert(event_id == kOverrun);
424 assert(event_msg > 0);
425 // Wait for all enqueued buffers be flushed.
426 if (event_msg != kNumOpenSlBuffers) {
429 // All buffers passed to OpenSL have been flushed. Restart the audio from
431 // No need to check sles_recorder_itf_ as recording_ would be false before it
433 OPENSL_RETURN_ON_FAILURE(
434 (*sles_recorder_itf_)->SetRecordState(sles_recorder_itf_,
435 SL_RECORDSTATE_STOPPED),
438 OPENSL_RETURN_ON_FAILURE(
439 (*sles_recorder_itf_)->SetRecordState(sles_recorder_itf_,
440 SL_RECORDSTATE_RECORDING),
445 void OpenSlesInput::RecorderSimpleBufferQueueCallback(
446 SLAndroidSimpleBufferQueueItf queue_itf,
448 OpenSlesInput* audio_device = reinterpret_cast<OpenSlesInput*>(context);
449 audio_device->RecorderSimpleBufferQueueCallbackHandler(queue_itf);
452 void OpenSlesInput::RecorderSimpleBufferQueueCallbackHandler(
453 SLAndroidSimpleBufferQueueItf queue_itf) {
454 if (fifo_->size() >= fifo_->capacity() || number_overruns_ > 0) {
456 event_.SignalEvent(kOverrun, number_overruns_);
459 int8_t* audio = rec_buf_[active_queue_].get();
460 // There is at least one spot available in the fifo.
462 active_queue_ = (active_queue_ + 1) % TotalBuffersUsed();
463 event_.SignalEvent(kNoOverrun, 0);
464 // active_queue_ is indexing the next buffer to record to. Since the current
465 // buffer has been recorded it means that the buffer index
466 // kNumOpenSlBuffers - 1 past |active_queue_| contains the next free buffer.
467 // Since |fifo_| wasn't at capacity, at least one buffer is free to be used.
468 int next_free_buffer =
469 (active_queue_ + kNumOpenSlBuffers - 1) % TotalBuffersUsed();
470 OPENSL_RETURN_ON_FAILURE(
471 (*sles_recorder_sbq_itf_)->Enqueue(
472 sles_recorder_sbq_itf_,
473 reinterpret_cast<void*>(rec_buf_[next_free_buffer].get()),
474 buffer_size_bytes()),
478 bool OpenSlesInput::StartCbThreads() {
479 rec_thread_.reset(ThreadWrapper::CreateThread(CbThread,
482 "opensl_rec_thread"));
483 assert(rec_thread_.get());
484 unsigned int thread_id = 0;
485 if (!rec_thread_->Start(thread_id)) {
489 OPENSL_RETURN_ON_FAILURE(
490 (*sles_recorder_itf_)->SetRecordState(sles_recorder_itf_,
491 SL_RECORDSTATE_RECORDING),
496 void OpenSlesInput::StopCbThreads() {
498 CriticalSectionScoped lock(crit_sect_.get());
501 if (sles_recorder_itf_) {
502 OPENSL_RETURN_ON_FAILURE(
503 (*sles_recorder_itf_)->SetRecordState(sles_recorder_itf_,
504 SL_RECORDSTATE_STOPPED),
507 if (rec_thread_.get() == NULL) {
511 if (rec_thread_->Stop()) {
518 bool OpenSlesInput::CbThread(void* context) {
519 return reinterpret_cast<OpenSlesInput*>(context)->CbThreadImpl();
522 bool OpenSlesInput::CbThreadImpl() {
525 // event_ must not be waited on while a lock has been taken.
526 event_.WaitOnEvent(&event_id, &event_msg);
528 CriticalSectionScoped lock(crit_sect_.get());
529 if (HandleOverrun(event_id, event_msg)) {
532 // If the fifo_ has audio data process it.
533 while (fifo_->size() > 0 && recording_) {
534 int8_t* audio = fifo_->Pop();
535 audio_buffer_->SetRecordedBuffer(audio, buffer_size_samples());
536 audio_buffer_->SetVQEData(delay_provider_->PlayoutDelayMs(),
537 recording_delay_, 0);
538 audio_buffer_->DeliverRecordedData();
543 } // namespace webrtc