1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_device/android/opensles_recorder.h"
12 
13 #include <android/log.h>
14 
15 #include <memory>
16 
17 #include "api/array_view.h"
18 #include "modules/audio_device/android/audio_common.h"
19 #include "modules/audio_device/android/audio_manager.h"
20 #include "modules/audio_device/fine_audio_buffer.h"
21 #include "rtc_base/arraysize.h"
22 #include "rtc_base/checks.h"
23 #include "rtc_base/format_macros.h"
24 #include "rtc_base/platform_thread.h"
25 #include "rtc_base/time_utils.h"
26 
27 #define TAG "OpenSLESRecorder"
28 #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
29 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
30 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
31 #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
32 #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
33 
34 #define LOG_ON_ERROR(op)                                    \
35   [](SLresult err) {                                        \
36     if (err != SL_RESULT_SUCCESS) {                         \
37       ALOGE("%s:%d %s failed: %s", __FILE__, __LINE__, #op, \
38             GetSLErrorString(err));                         \
39       return true;                                          \
40     }                                                       \
41     return false;                                           \
42   }(op)
43 
44 namespace webrtc {
45 
OpenSLESRecorder(AudioManager * audio_manager)46 OpenSLESRecorder::OpenSLESRecorder(AudioManager* audio_manager)
47     : audio_manager_(audio_manager),
48       audio_parameters_(audio_manager->GetRecordAudioParameters()),
49       audio_device_buffer_(nullptr),
50       initialized_(false),
51       recording_(false),
52       engine_(nullptr),
53       recorder_(nullptr),
54       simple_buffer_queue_(nullptr),
55       buffer_index_(0),
56       last_rec_time_(0) {
57   ALOGD("ctor[tid=%d]", rtc::CurrentThreadId());
58   // Detach from this thread since we want to use the checker to verify calls
59   // from the internal  audio thread.
60   thread_checker_opensles_.Detach();
61   // Use native audio output parameters provided by the audio manager and
62   // define the PCM format structure.
63   pcm_format_ = CreatePCMConfiguration(audio_parameters_.channels(),
64                                        audio_parameters_.sample_rate(),
65                                        audio_parameters_.bits_per_sample());
66 }
67 
~OpenSLESRecorder()68 OpenSLESRecorder::~OpenSLESRecorder() {
69   ALOGD("dtor[tid=%d]", rtc::CurrentThreadId());
70   RTC_DCHECK(thread_checker_.IsCurrent());
71   Terminate();
72   DestroyAudioRecorder();
73   engine_ = nullptr;
74   RTC_DCHECK(!engine_);
75   RTC_DCHECK(!recorder_);
76   RTC_DCHECK(!simple_buffer_queue_);
77 }
78 
Init()79 int OpenSLESRecorder::Init() {
80   ALOGD("Init[tid=%d]", rtc::CurrentThreadId());
81   RTC_DCHECK(thread_checker_.IsCurrent());
82   if (audio_parameters_.channels() == 2) {
83     ALOGD("Stereo mode is enabled");
84   }
85   return 0;
86 }
87 
Terminate()88 int OpenSLESRecorder::Terminate() {
89   ALOGD("Terminate[tid=%d]", rtc::CurrentThreadId());
90   RTC_DCHECK(thread_checker_.IsCurrent());
91   StopRecording();
92   return 0;
93 }
94 
InitRecording()95 int OpenSLESRecorder::InitRecording() {
96   ALOGD("InitRecording[tid=%d]", rtc::CurrentThreadId());
97   RTC_DCHECK(thread_checker_.IsCurrent());
98   RTC_DCHECK(!initialized_);
99   RTC_DCHECK(!recording_);
100   if (!ObtainEngineInterface()) {
101     ALOGE("Failed to obtain SL Engine interface");
102     return -1;
103   }
104   CreateAudioRecorder();
105   initialized_ = true;
106   buffer_index_ = 0;
107   return 0;
108 }
109 
StartRecording()110 int OpenSLESRecorder::StartRecording() {
111   ALOGD("StartRecording[tid=%d]", rtc::CurrentThreadId());
112   RTC_DCHECK(thread_checker_.IsCurrent());
113   RTC_DCHECK(initialized_);
114   RTC_DCHECK(!recording_);
115   if (fine_audio_buffer_) {
116     fine_audio_buffer_->ResetRecord();
117   }
118   // Add buffers to the queue before changing state to SL_RECORDSTATE_RECORDING
119   // to ensure that recording starts as soon as the state is modified. On some
120   // devices, SLAndroidSimpleBufferQueue::Clear() used in Stop() does not flush
121   // the buffers as intended and we therefore check the number of buffers
122   // already queued first. Enqueue() can return SL_RESULT_BUFFER_INSUFFICIENT
123   // otherwise.
124   int num_buffers_in_queue = GetBufferCount();
125   for (int i = 0; i < kNumOfOpenSLESBuffers - num_buffers_in_queue; ++i) {
126     if (!EnqueueAudioBuffer()) {
127       recording_ = false;
128       return -1;
129     }
130   }
131   num_buffers_in_queue = GetBufferCount();
132   RTC_DCHECK_EQ(num_buffers_in_queue, kNumOfOpenSLESBuffers);
133   LogBufferState();
134   // Start audio recording by changing the state to SL_RECORDSTATE_RECORDING.
135   // Given that buffers are already enqueued, recording should start at once.
136   // The macro returns -1 if recording fails to start.
137   last_rec_time_ = rtc::Time();
138   if (LOG_ON_ERROR(
139           (*recorder_)->SetRecordState(recorder_, SL_RECORDSTATE_RECORDING))) {
140     return -1;
141   }
142   recording_ = (GetRecordState() == SL_RECORDSTATE_RECORDING);
143   RTC_DCHECK(recording_);
144   return 0;
145 }
146 
StopRecording()147 int OpenSLESRecorder::StopRecording() {
148   ALOGD("StopRecording[tid=%d]", rtc::CurrentThreadId());
149   RTC_DCHECK(thread_checker_.IsCurrent());
150   if (!initialized_ || !recording_) {
151     return 0;
152   }
153   // Stop recording by setting the record state to SL_RECORDSTATE_STOPPED.
154   if (LOG_ON_ERROR(
155           (*recorder_)->SetRecordState(recorder_, SL_RECORDSTATE_STOPPED))) {
156     return -1;
157   }
158   // Clear the buffer queue to get rid of old data when resuming recording.
159   if (LOG_ON_ERROR((*simple_buffer_queue_)->Clear(simple_buffer_queue_))) {
160     return -1;
161   }
162   thread_checker_opensles_.Detach();
163   initialized_ = false;
164   recording_ = false;
165   return 0;
166 }
167 
AttachAudioBuffer(AudioDeviceBuffer * audio_buffer)168 void OpenSLESRecorder::AttachAudioBuffer(AudioDeviceBuffer* audio_buffer) {
169   ALOGD("AttachAudioBuffer");
170   RTC_DCHECK(thread_checker_.IsCurrent());
171   RTC_CHECK(audio_buffer);
172   audio_device_buffer_ = audio_buffer;
173   // Ensure that the audio device buffer is informed about the native sample
174   // rate used on the recording side.
175   const int sample_rate_hz = audio_parameters_.sample_rate();
176   ALOGD("SetRecordingSampleRate(%d)", sample_rate_hz);
177   audio_device_buffer_->SetRecordingSampleRate(sample_rate_hz);
178   // Ensure that the audio device buffer is informed about the number of
179   // channels preferred by the OS on the recording side.
180   const size_t channels = audio_parameters_.channels();
181   ALOGD("SetRecordingChannels(%" RTC_PRIuS ")", channels);
182   audio_device_buffer_->SetRecordingChannels(channels);
183   // Allocated memory for internal data buffers given existing audio parameters.
184   AllocateDataBuffers();
185 }
186 
EnableBuiltInAEC(bool enable)187 int OpenSLESRecorder::EnableBuiltInAEC(bool enable) {
188   ALOGD("EnableBuiltInAEC(%d)", enable);
189   RTC_DCHECK(thread_checker_.IsCurrent());
190   ALOGE("Not implemented");
191   return 0;
192 }
193 
EnableBuiltInAGC(bool enable)194 int OpenSLESRecorder::EnableBuiltInAGC(bool enable) {
195   ALOGD("EnableBuiltInAGC(%d)", enable);
196   RTC_DCHECK(thread_checker_.IsCurrent());
197   ALOGE("Not implemented");
198   return 0;
199 }
200 
EnableBuiltInNS(bool enable)201 int OpenSLESRecorder::EnableBuiltInNS(bool enable) {
202   ALOGD("EnableBuiltInNS(%d)", enable);
203   RTC_DCHECK(thread_checker_.IsCurrent());
204   ALOGE("Not implemented");
205   return 0;
206 }
207 
ObtainEngineInterface()208 bool OpenSLESRecorder::ObtainEngineInterface() {
209   ALOGD("ObtainEngineInterface");
210   RTC_DCHECK(thread_checker_.IsCurrent());
211   if (engine_)
212     return true;
213   // Get access to (or create if not already existing) the global OpenSL Engine
214   // object.
215   SLObjectItf engine_object = audio_manager_->GetOpenSLEngine();
216   if (engine_object == nullptr) {
217     ALOGE("Failed to access the global OpenSL engine");
218     return false;
219   }
220   // Get the SL Engine Interface which is implicit.
221   if (LOG_ON_ERROR(
222           (*engine_object)
223               ->GetInterface(engine_object, SL_IID_ENGINE, &engine_))) {
224     return false;
225   }
226   return true;
227 }
228 
CreateAudioRecorder()229 bool OpenSLESRecorder::CreateAudioRecorder() {
230   ALOGD("CreateAudioRecorder");
231   RTC_DCHECK(thread_checker_.IsCurrent());
232   if (recorder_object_.Get())
233     return true;
234   RTC_DCHECK(!recorder_);
235   RTC_DCHECK(!simple_buffer_queue_);
236 
237   // Audio source configuration.
238   SLDataLocator_IODevice mic_locator = {SL_DATALOCATOR_IODEVICE,
239                                         SL_IODEVICE_AUDIOINPUT,
240                                         SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
241   SLDataSource audio_source = {&mic_locator, NULL};
242 
243   // Audio sink configuration.
244   SLDataLocator_AndroidSimpleBufferQueue buffer_queue = {
245       SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
246       static_cast<SLuint32>(kNumOfOpenSLESBuffers)};
247   SLDataSink audio_sink = {&buffer_queue, &pcm_format_};
248 
249   // Create the audio recorder object (requires the RECORD_AUDIO permission).
250   // Do not realize the recorder yet. Set the configuration first.
251   const SLInterfaceID interface_id[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
252                                         SL_IID_ANDROIDCONFIGURATION};
253   const SLboolean interface_required[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
254   if (LOG_ON_ERROR((*engine_)->CreateAudioRecorder(
255           engine_, recorder_object_.Receive(), &audio_source, &audio_sink,
256           arraysize(interface_id), interface_id, interface_required))) {
257     return false;
258   }
259 
260   // Configure the audio recorder (before it is realized).
261   SLAndroidConfigurationItf recorder_config;
262   if (LOG_ON_ERROR((recorder_object_->GetInterface(recorder_object_.Get(),
263                                                    SL_IID_ANDROIDCONFIGURATION,
264                                                    &recorder_config)))) {
265     return false;
266   }
267 
268   // Uses the default microphone tuned for audio communication.
269   // Note that, SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION leads to a fast
270   // track but also excludes usage of required effects like AEC, AGC and NS.
271   // SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION
272   SLint32 stream_type = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
273   if (LOG_ON_ERROR(((*recorder_config)
274                         ->SetConfiguration(recorder_config,
275                                            SL_ANDROID_KEY_RECORDING_PRESET,
276                                            &stream_type, sizeof(SLint32))))) {
277     return false;
278   }
279 
280   // The audio recorder can now be realized (in synchronous mode).
281   if (LOG_ON_ERROR((recorder_object_->Realize(recorder_object_.Get(),
282                                               SL_BOOLEAN_FALSE)))) {
283     return false;
284   }
285 
286   // Get the implicit recorder interface (SL_IID_RECORD).
287   if (LOG_ON_ERROR((recorder_object_->GetInterface(
288           recorder_object_.Get(), SL_IID_RECORD, &recorder_)))) {
289     return false;
290   }
291 
292   // Get the simple buffer queue interface (SL_IID_ANDROIDSIMPLEBUFFERQUEUE).
293   // It was explicitly requested.
294   if (LOG_ON_ERROR((recorder_object_->GetInterface(
295           recorder_object_.Get(), SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
296           &simple_buffer_queue_)))) {
297     return false;
298   }
299 
300   // Register the input callback for the simple buffer queue.
301   // This callback will be called when receiving new data from the device.
302   if (LOG_ON_ERROR(((*simple_buffer_queue_)
303                         ->RegisterCallback(simple_buffer_queue_,
304                                            SimpleBufferQueueCallback, this)))) {
305     return false;
306   }
307   return true;
308 }
309 
DestroyAudioRecorder()310 void OpenSLESRecorder::DestroyAudioRecorder() {
311   ALOGD("DestroyAudioRecorder");
312   RTC_DCHECK(thread_checker_.IsCurrent());
313   if (!recorder_object_.Get())
314     return;
315   (*simple_buffer_queue_)
316       ->RegisterCallback(simple_buffer_queue_, nullptr, nullptr);
317   recorder_object_.Reset();
318   recorder_ = nullptr;
319   simple_buffer_queue_ = nullptr;
320 }
321 
SimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf buffer_queue,void * context)322 void OpenSLESRecorder::SimpleBufferQueueCallback(
323     SLAndroidSimpleBufferQueueItf buffer_queue,
324     void* context) {
325   OpenSLESRecorder* stream = static_cast<OpenSLESRecorder*>(context);
326   stream->ReadBufferQueue();
327 }
328 
AllocateDataBuffers()329 void OpenSLESRecorder::AllocateDataBuffers() {
330   ALOGD("AllocateDataBuffers");
331   RTC_DCHECK(thread_checker_.IsCurrent());
332   RTC_DCHECK(!simple_buffer_queue_);
333   RTC_CHECK(audio_device_buffer_);
334   // Create a modified audio buffer class which allows us to deliver any number
335   // of samples (and not only multiple of 10ms) to match the native audio unit
336   // buffer size.
337   ALOGD("frames per native buffer: %" RTC_PRIuS,
338         audio_parameters_.frames_per_buffer());
339   ALOGD("frames per 10ms buffer: %" RTC_PRIuS,
340         audio_parameters_.frames_per_10ms_buffer());
341   ALOGD("bytes per native buffer: %" RTC_PRIuS,
342         audio_parameters_.GetBytesPerBuffer());
343   ALOGD("native sample rate: %d", audio_parameters_.sample_rate());
344   RTC_DCHECK(audio_device_buffer_);
345   fine_audio_buffer_ = std::make_unique<FineAudioBuffer>(audio_device_buffer_);
346   // Allocate queue of audio buffers that stores recorded audio samples.
347   const int buffer_size_samples =
348       audio_parameters_.frames_per_buffer() * audio_parameters_.channels();
349   audio_buffers_.reset(new std::unique_ptr<SLint16[]>[kNumOfOpenSLESBuffers]);
350   for (int i = 0; i < kNumOfOpenSLESBuffers; ++i) {
351     audio_buffers_[i].reset(new SLint16[buffer_size_samples]);
352   }
353 }
354 
ReadBufferQueue()355 void OpenSLESRecorder::ReadBufferQueue() {
356   RTC_DCHECK(thread_checker_opensles_.IsCurrent());
357   SLuint32 state = GetRecordState();
358   if (state != SL_RECORDSTATE_RECORDING) {
359     ALOGW("Buffer callback in non-recording state!");
360     return;
361   }
362   // Check delta time between two successive callbacks and provide a warning
363   // if it becomes very large.
364   // TODO(henrika): using 150ms as upper limit but this value is rather random.
365   const uint32_t current_time = rtc::Time();
366   const uint32_t diff = current_time - last_rec_time_;
367   if (diff > 150) {
368     ALOGW("Bad OpenSL ES record timing, dT=%u [ms]", diff);
369   }
370   last_rec_time_ = current_time;
371   // Send recorded audio data to the WebRTC sink.
372   // TODO(henrika): fix delay estimates. It is OK to use fixed values for now
373   // since there is no support to turn off built-in EC in combination with
374   // OpenSL ES anyhow. Hence, as is, the WebRTC based AEC (which would use
375   // these estimates) will never be active.
376   fine_audio_buffer_->DeliverRecordedData(
377       rtc::ArrayView<const int16_t>(
378           audio_buffers_[buffer_index_].get(),
379           audio_parameters_.frames_per_buffer() * audio_parameters_.channels()),
380       25);
381   // Enqueue the utilized audio buffer and use if for recording again.
382   EnqueueAudioBuffer();
383 }
384 
EnqueueAudioBuffer()385 bool OpenSLESRecorder::EnqueueAudioBuffer() {
386   SLresult err =
387       (*simple_buffer_queue_)
388           ->Enqueue(
389               simple_buffer_queue_,
390               reinterpret_cast<SLint8*>(audio_buffers_[buffer_index_].get()),
391               audio_parameters_.GetBytesPerBuffer());
392   if (SL_RESULT_SUCCESS != err) {
393     ALOGE("Enqueue failed: %s", GetSLErrorString(err));
394     return false;
395   }
396   buffer_index_ = (buffer_index_ + 1) % kNumOfOpenSLESBuffers;
397   return true;
398 }
399 
GetRecordState() const400 SLuint32 OpenSLESRecorder::GetRecordState() const {
401   RTC_DCHECK(recorder_);
402   SLuint32 state;
403   SLresult err = (*recorder_)->GetRecordState(recorder_, &state);
404   if (SL_RESULT_SUCCESS != err) {
405     ALOGE("GetRecordState failed: %s", GetSLErrorString(err));
406   }
407   return state;
408 }
409 
GetBufferQueueState() const410 SLAndroidSimpleBufferQueueState OpenSLESRecorder::GetBufferQueueState() const {
411   RTC_DCHECK(simple_buffer_queue_);
412   // state.count: Number of buffers currently in the queue.
413   // state.index: Index of the currently filling buffer. This is a linear index
414   // that keeps a cumulative count of the number of buffers recorded.
415   SLAndroidSimpleBufferQueueState state;
416   SLresult err =
417       (*simple_buffer_queue_)->GetState(simple_buffer_queue_, &state);
418   if (SL_RESULT_SUCCESS != err) {
419     ALOGE("GetState failed: %s", GetSLErrorString(err));
420   }
421   return state;
422 }
423 
LogBufferState() const424 void OpenSLESRecorder::LogBufferState() const {
425   SLAndroidSimpleBufferQueueState state = GetBufferQueueState();
426   ALOGD("state.count:%d state.index:%d", state.count, state.index);
427 }
428 
GetBufferCount()429 SLuint32 OpenSLESRecorder::GetBufferCount() {
430   SLAndroidSimpleBufferQueueState state = GetBufferQueueState();
431   return state.count;
432 }
433 
434 }  // namespace webrtc
435