Refactor OpenSL audio driver

Message:
I want to start this review, the basica framework is almost done.

Description:
This implementation is similar to current one, but
1. followed the design doc at
https://docs.google.com/a/google.com/document/d/1g5q2SVtkFPl2OSjvSF3eeLb_S7sCnbINxiBUES6XLCM/edit
which uses two threads, playout thread and recording thread, uses large
audio buffer, etc.
2. google code style.

What are missing in this cl,
1. a better way to control schedule/thread priority.
2. java/jni interface to better support what cannot be done in OpenSL.

Please take a review, thanks.
Review URL: https://webrtc-codereview.appspot.com/902004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3040 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
leozwang@webrtc.org 2012-11-02 16:48:03 +00:00
parent 737ed3bfa5
commit 5f9970fb0b
2 changed files with 1316 additions and 1549 deletions

View File

@ -8,14 +8,10 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_OPENSLES_ANDROID_H
#define WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_OPENSLES_ANDROID_H
#ifndef SRC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_OPENSLES_ANDROID_H_
#define SRC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_OPENSLES_ANDROID_H_
#include "audio_device_generic.h"
#include "critical_section_wrapper.h"
#include <jni.h> // For accessing AudioDeviceAndroid.java
#include <queue>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
@ -23,41 +19,55 @@
#include <SLES/OpenSLES_Android.h>
#include <SLES/OpenSLES_AndroidConfiguration.h>
namespace webrtc
{
#include <queue>
#include "modules/audio_device/audio_device_generic.h"
#include "system_wrappers/interface/critical_section_wrapper.h"
namespace webrtc {
class EventWrapper;
const WebRtc_UWord32 N_MAX_INTERFACES = 3;
const WebRtc_UWord32 N_MAX_OUTPUT_DEVICES = 6;
const WebRtc_UWord32 N_MAX_INPUT_DEVICES = 3;
const WebRtc_UWord32 N_REC_SAMPLES_PER_SEC = 16000;//44000; // Default fs
const WebRtc_UWord32 N_PLAY_SAMPLES_PER_SEC = 16000;//44000; // Default fs
const WebRtc_UWord32 N_REC_SAMPLES_PER_SEC = 16000; // Default fs
const WebRtc_UWord32 N_PLAY_SAMPLES_PER_SEC = 16000; // Default fs
const WebRtc_UWord32 N_REC_CHANNELS = 1; // default is mono recording
const WebRtc_UWord32 N_PLAY_CHANNELS = 1; // default is mono playout
const WebRtc_UWord32 N_REC_CHANNELS = 1;
const WebRtc_UWord32 N_PLAY_CHANNELS = 1;
const WebRtc_UWord32 REC_BUF_SIZE_IN_SAMPLES = 480; // Handle max 10 ms @ 48 kHz
const WebRtc_UWord32 REC_BUF_SIZE_IN_SAMPLES = 480;
const WebRtc_UWord32 PLAY_BUF_SIZE_IN_SAMPLES = 480;
const WebRtc_UWord32 REC_MAX_TEMP_BUF_SIZE_PER_10ms =
N_REC_CHANNELS * REC_BUF_SIZE_IN_SAMPLES * sizeof(int16_t);
const WebRtc_UWord32 PLAY_MAX_TEMP_BUF_SIZE_PER_10ms =
N_PLAY_CHANNELS * PLAY_BUF_SIZE_IN_SAMPLES * sizeof(int16_t);
// Number of the buffers in playout queue
const WebRtc_UWord16 N_PLAY_QUEUE_BUFFERS = 2;
const WebRtc_UWord16 N_PLAY_QUEUE_BUFFERS = 8;
// Number of buffers in recording queue
const WebRtc_UWord16 N_REC_QUEUE_BUFFERS = 2;
// Number of 10 ms recording blocks in rec buffer
const WebRtc_UWord16 N_REC_BUFFERS = 20;
// TODO(xian): Reduce the numbers of buffers to improve the latency.
const WebRtc_UWord16 N_REC_QUEUE_BUFFERS = 8;
// Some values returned from getMinBufferSize
// (Nexus S playout 72ms, recording 64ms)
// (Galaxy, 167ms, 44ms)
// (Nexus 7, 72ms, 48ms)
// (Xoom 92ms, 40ms)
class ThreadWrapper;
class AudioDeviceAndroidOpenSLES: public AudioDeviceGeneric
{
public:
AudioDeviceAndroidOpenSLES(const WebRtc_Word32 id);
class AudioDeviceAndroidOpenSLES: public AudioDeviceGeneric {
public:
explicit AudioDeviceAndroidOpenSLES(const WebRtc_Word32 id);
~AudioDeviceAndroidOpenSLES();
// Retrieve the currently utilized audio layer
virtual WebRtc_Word32
ActiveAudioLayer(AudioDeviceModule::AudioLayer& audioLayer) const;
ActiveAudioLayer(AudioDeviceModule::AudioLayer& audioLayer) const; // NOLINT
// Main initializaton and termination
virtual WebRtc_Word32 Init();
@ -85,10 +95,10 @@ public:
SetRecordingDevice(AudioDeviceModule::WindowsDeviceType device);
// Audio transport initialization
virtual WebRtc_Word32 PlayoutIsAvailable(bool& available);
virtual WebRtc_Word32 PlayoutIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 InitPlayout();
virtual bool PlayoutIsInitialized() const;
virtual WebRtc_Word32 RecordingIsAvailable(bool& available);
virtual WebRtc_Word32 RecordingIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 InitRecording();
virtual bool RecordingIsInitialized() const;
@ -107,69 +117,82 @@ public:
// Volume control based on the Windows Wave API (Windows only)
virtual WebRtc_Word32 SetWaveOutVolume(WebRtc_UWord16 volumeLeft,
WebRtc_UWord16 volumeRight);
virtual WebRtc_Word32 WaveOutVolume(WebRtc_UWord16& volumeLeft,
WebRtc_UWord16& volumeRight) const;
virtual WebRtc_Word32 WaveOutVolume(
WebRtc_UWord16& volumeLeft, // NOLINT
WebRtc_UWord16& volumeRight) const; // NOLINT
// Audio mixer initialization
virtual WebRtc_Word32 SpeakerIsAvailable(bool& available);
virtual WebRtc_Word32 SpeakerIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 InitSpeaker();
virtual bool SpeakerIsInitialized() const;
SLPlayItf playItf;
virtual WebRtc_Word32 MicrophoneIsAvailable(bool& available);
virtual WebRtc_Word32 MicrophoneIsAvailable(
bool& available);
virtual WebRtc_Word32 InitMicrophone();
virtual bool MicrophoneIsInitialized() const;
// Speaker volume controls
virtual WebRtc_Word32 SpeakerVolumeIsAvailable(bool& available);
virtual WebRtc_Word32 SpeakerVolumeIsAvailable(
bool& available); // NOLINT
virtual WebRtc_Word32 SetSpeakerVolume(WebRtc_UWord32 volume);
virtual WebRtc_Word32 SpeakerVolume(WebRtc_UWord32& volume) const;
virtual WebRtc_Word32 MaxSpeakerVolume(WebRtc_UWord32& maxVolume) const;
virtual WebRtc_Word32 MinSpeakerVolume(WebRtc_UWord32& minVolume) const;
virtual WebRtc_Word32 SpeakerVolumeStepSize(WebRtc_UWord16& stepSize) const;
virtual WebRtc_Word32 SpeakerVolume(
WebRtc_UWord32& volume) const; // NOLINT
virtual WebRtc_Word32 MaxSpeakerVolume(
WebRtc_UWord32& maxVolume) const; // NOLINT
virtual WebRtc_Word32 MinSpeakerVolume(
WebRtc_UWord32& minVolume) const; // NOLINT
virtual WebRtc_Word32 SpeakerVolumeStepSize(
WebRtc_UWord16& stepSize) const; // NOLINT
// Microphone volume controls
virtual WebRtc_Word32 MicrophoneVolumeIsAvailable(bool& available);
virtual WebRtc_Word32 MicrophoneVolumeIsAvailable(
bool& available); // NOLINT
virtual WebRtc_Word32 SetMicrophoneVolume(WebRtc_UWord32 volume);
virtual WebRtc_Word32 MicrophoneVolume(WebRtc_UWord32& volume) const;
virtual WebRtc_Word32 MaxMicrophoneVolume(WebRtc_UWord32& maxVolume) const;
virtual WebRtc_Word32 MinMicrophoneVolume(WebRtc_UWord32& minVolume) const;
virtual WebRtc_Word32 MicrophoneVolume(
WebRtc_UWord32& volume) const; // NOLINT
virtual WebRtc_Word32 MaxMicrophoneVolume(
WebRtc_UWord32& maxVolume) const; // NOLINT
virtual WebRtc_Word32 MinMicrophoneVolume(
WebRtc_UWord32& minVolume) const; // NOLINT
virtual WebRtc_Word32
MicrophoneVolumeStepSize(WebRtc_UWord16& stepSize) const;
MicrophoneVolumeStepSize(WebRtc_UWord16& stepSize) const; // NOLINT
// Speaker mute control
virtual WebRtc_Word32 SpeakerMuteIsAvailable(bool& available);
virtual WebRtc_Word32 SpeakerMuteIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 SetSpeakerMute(bool enable);
virtual WebRtc_Word32 SpeakerMute(bool& enabled) const;
virtual WebRtc_Word32 SpeakerMute(bool& enabled) const; // NOLINT
// Microphone mute control
virtual WebRtc_Word32 MicrophoneMuteIsAvailable(bool& available);
virtual WebRtc_Word32 MicrophoneMuteIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 SetMicrophoneMute(bool enable);
virtual WebRtc_Word32 MicrophoneMute(bool& enabled) const;
virtual WebRtc_Word32 MicrophoneMute(bool& enabled) const; // NOLINT
// Microphone boost control
virtual WebRtc_Word32 MicrophoneBoostIsAvailable(bool& available);
virtual WebRtc_Word32 MicrophoneBoostIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 SetMicrophoneBoost(bool enable);
virtual WebRtc_Word32 MicrophoneBoost(bool& enabled) const;
virtual WebRtc_Word32 MicrophoneBoost(bool& enabled) const; // NOLINT
// Stereo support
virtual WebRtc_Word32 StereoPlayoutIsAvailable(bool& available);
virtual WebRtc_Word32 StereoPlayoutIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 SetStereoPlayout(bool enable);
virtual WebRtc_Word32 StereoPlayout(bool& enabled) const;
virtual WebRtc_Word32 StereoRecordingIsAvailable(bool& available);
virtual WebRtc_Word32 StereoPlayout(bool& enabled) const; // NOLINT
virtual WebRtc_Word32 StereoRecordingIsAvailable(bool& available); // NOLINT
virtual WebRtc_Word32 SetStereoRecording(bool enable);
virtual WebRtc_Word32 StereoRecording(bool& enabled) const;
virtual WebRtc_Word32 StereoRecording(bool& enabled) const; // NOLINT
// Delay information and control
virtual WebRtc_Word32
SetPlayoutBuffer(const AudioDeviceModule::BufferType type,
WebRtc_UWord16 sizeMS);
virtual WebRtc_Word32 PlayoutBuffer(AudioDeviceModule::BufferType& type,
virtual WebRtc_Word32 PlayoutBuffer(
AudioDeviceModule::BufferType& type, // NOLINT
WebRtc_UWord16& sizeMS) const;
virtual WebRtc_Word32 PlayoutDelay(WebRtc_UWord16& delayMS) const;
virtual WebRtc_Word32 RecordingDelay(WebRtc_UWord16& delayMS) const;
virtual WebRtc_Word32 PlayoutDelay(
WebRtc_UWord16& delayMS) const; // NOLINT
virtual WebRtc_Word32 RecordingDelay(
WebRtc_UWord16& delayMS) const; // NOLINT
// CPU load
virtual WebRtc_Word32 CPULoad(WebRtc_UWord16& load) const;
virtual WebRtc_Word32 CPULoad(WebRtc_UWord16& load) const; // NOLINT
// Error and warning information
virtual bool PlayoutWarning() const;
@ -186,27 +209,25 @@ public:
// Speaker audio routing
virtual WebRtc_Word32 SetLoudspeakerStatus(bool enable);
virtual WebRtc_Word32 GetLoudspeakerStatus(bool& enable) const;
virtual WebRtc_Word32 GetLoudspeakerStatus(bool& enable) const; // NOLINT
private:
private:
// Lock
void Lock()
{
_critSect.Enter();
void Lock() {
crit_sect_.Enter();
};
void UnLock()
{
_critSect.Leave();
void UnLock() {
crit_sect_.Leave();
};
static void PlayerSimpleBufferQueueCallback(
SLAndroidSimpleBufferQueueItf queueItf,
void *pContext);
void PlayerSimpleBufferQueueCallbackHandler(
SLAndroidSimpleBufferQueueItf queueItf);
static void RecorderSimpleBufferQueueCallback(
SLAndroidSimpleBufferQueueItf queueItf,
void *pContext);
void PlayerSimpleBufferQueueCallbackHandler(
SLAndroidSimpleBufferQueueItf queueItf);
void RecorderSimpleBufferQueueCallbackHandler(
SLAndroidSimpleBufferQueueItf queueItf);
void CheckErr(SLresult res);
@ -218,99 +239,79 @@ private:
// Init
WebRtc_Word32 InitSampleRate();
// Threads
static bool RecThreadFunc(void*);
static bool PlayThreadFunc(void*);
bool RecThreadProcess();
bool PlayThreadProcess();
// Misc
AudioDeviceBuffer* _ptrAudioBuffer;
CriticalSectionWrapper& _critSect;
WebRtc_Word32 _id;
AudioDeviceBuffer* voe_audio_buffer_;
CriticalSectionWrapper& crit_sect_;
WebRtc_Word32 id_;
// audio unit
SLObjectItf _slEngineObject;
SLObjectItf sles_engine_;
// playout device
SLObjectItf _slPlayer;
SLEngineItf _slEngine;
SLPlayItf _slPlayerPlay;
SLAndroidSimpleBufferQueueItf _slPlayerSimpleBufferQueue;
SLObjectItf _slOutputMixObject;
SLVolumeItf _slSpeakerVolume;
SLObjectItf sles_player_;
SLEngineItf sles_engine_itf_;
SLPlayItf sles_player_itf_;
SLAndroidSimpleBufferQueueItf sles_player_sbq_itf_;
SLObjectItf sles_output_mixer_;
SLVolumeItf sles_speaker_volume_;
// recording device
SLObjectItf _slRecorder;
SLRecordItf _slRecorderRecord;
SLAudioIODeviceCapabilitiesItf _slAudioIODeviceCapabilities;
SLAndroidSimpleBufferQueueItf _slRecorderSimpleBufferQueue;
SLDeviceVolumeItf _slMicVolume;
SLObjectItf sles_recorder_;
SLRecordItf sles_recorder_itf_;
SLAndroidSimpleBufferQueueItf sles_recorder_sbq_itf_;
SLDeviceVolumeItf sles_mic_volume_;
WebRtc_UWord32 mic_dev_id_;
WebRtc_UWord32 _micDeviceId;
WebRtc_UWord32 _recQueueSeq;
// Events
EventWrapper& _timeEventRec;
// Threads
ThreadWrapper* _ptrThreadRec;
WebRtc_UWord32 _recThreadID;
// TODO(xians), remove the following flag
bool _recThreadIsInitialized;
// Playout buffer
WebRtc_Word8 _playQueueBuffer[N_PLAY_QUEUE_BUFFERS][2
* PLAY_BUF_SIZE_IN_SAMPLES];
WebRtc_UWord32 _playQueueSeq;
WebRtc_UWord32 play_warning_, play_error_;
WebRtc_UWord32 rec_warning_, rec_error_;
// States
bool _recordingDeviceIsSpecified;
bool _playoutDeviceIsSpecified;
bool _initialized;
bool _recording;
bool _playing;
bool _recIsInitialized;
bool _playIsInitialized;
bool _micIsInitialized;
bool _speakerIsInitialized;
// Warnings and errors
WebRtc_UWord16 _playWarning;
WebRtc_UWord16 _playError;
WebRtc_UWord16 _recWarning;
WebRtc_UWord16 _recError;
bool is_recording_dev_specified_;
bool is_playout_dev_specified_;
bool is_initialized_;
bool is_recording_;
bool is_playing_;
bool is_rec_initialized_;
bool is_play_initialized_;
bool is_mic_initialized_;
bool is_speaker_initialized_;
// Delay
WebRtc_UWord16 _playoutDelay;
WebRtc_UWord16 _recordingDelay;
WebRtc_UWord16 playout_delay_;
WebRtc_UWord16 recording_delay_;
// AGC state
bool _AGC;
bool agc_enabled_;
// The sampling rate to use with Audio Device Buffer
WebRtc_UWord32 _adbSampleRate;
// Stored device properties
WebRtc_UWord32 _samplingRateIn; // Sampling frequency for Mic
WebRtc_UWord32 _samplingRateOut; // Sampling frequency for Speaker
WebRtc_UWord32 _maxSpeakerVolume; // The maximum speaker volume value
WebRtc_UWord32 _minSpeakerVolume; // The minimum speaker volume value
bool _loudSpeakerOn;
// Threads
ThreadWrapper* rec_thread_;
WebRtc_UWord32 rec_thread_id_;
static bool RecThreadFunc(void* context);
bool RecThreadFuncImpl();
EventWrapper& rec_timer_;
// Recording buffer used by the queues.
int8_t rec_buffer_[N_REC_BUFFERS][2 * REC_BUF_SIZE_IN_SAMPLES];
WebRtc_UWord32 mic_sampling_rate_;
WebRtc_UWord32 speaker_sampling_rate_;
WebRtc_UWord32 max_speaker_vol_;
WebRtc_UWord32 min_speaker_vol_;
bool loundspeaker_on_;
// Queues accessed by both callback thread and recording thread after
// recording has been started.
std::queue<int8_t*> rec_worker_queue_;
std::queue<int8_t*> rec_available_queue_;
SLDataFormat_PCM player_pcm_;
SLDataFormat_PCM record_pcm_;
// Queue accssed by only callback thread after recording has been started.
std::queue<int8_t*> rec_callback_queue_;
std::queue<WebRtc_Word8*> rec_queue_;
std::queue<WebRtc_Word8*> rec_voe_audio_queue_;
std::queue<WebRtc_Word8*> rec_voe_ready_queue_;
WebRtc_Word8 rec_buf_[N_REC_QUEUE_BUFFERS][
N_REC_CHANNELS * sizeof(int16_t) * REC_BUF_SIZE_IN_SAMPLES];
WebRtc_Word8 rec_voe_buf_[N_REC_QUEUE_BUFFERS][
N_REC_CHANNELS * sizeof(int16_t) * REC_BUF_SIZE_IN_SAMPLES];
// Flag to protect setting the recording thread priority multiple times.
bool is_thread_priority_set_;
std::queue<WebRtc_Word8*> play_queue_;
WebRtc_Word8 play_buf_[N_PLAY_QUEUE_BUFFERS][
N_PLAY_CHANNELS * sizeof(int16_t) * PLAY_BUF_SIZE_IN_SAMPLES];
};
} // namespace webrtc
#endif // WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_OPENSLES_ANDROID_H
#endif // SRC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_OPENSLES_ANDROID_H_