Index: media/audio/android/opensles_input.cc |
diff --git a/media/audio/android/opensles_input.cc b/media/audio/android/opensles_input.cc |
index 15c3eac3726389bb7f8d0a7f37ee4b8d35a82f91..8943167134ea58acee6219c4a97ab527c36729ca 100644 |
--- a/media/audio/android/opensles_input.cc |
+++ b/media/audio/android/opensles_input.cc |
@@ -27,6 +27,7 @@ OpenSLESInputStream::OpenSLESInputStream(AudioManagerAndroid* audio_manager, |
active_queue_(0), |
buffer_size_bytes_(0), |
started_(false) { |
+ DVLOG(2) << "OpenSLESInputStream::OpenSLESInputStream()"; |
format_.formatType = SL_DATAFORMAT_PCM; |
format_.numChannels = static_cast<SLuint32>(params.channels()); |
// Provides sampling rate in milliHertz to OpenSLES. |
@@ -47,6 +48,8 @@ OpenSLESInputStream::OpenSLESInputStream(AudioManagerAndroid* audio_manager, |
} |
OpenSLESInputStream::~OpenSLESInputStream() { |
+ DVLOG(2) << "OpenSLESInputStream::~OpenSLESInputStream()"; |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
DCHECK(!recorder_object_.Get()); |
DCHECK(!engine_object_.Get()); |
DCHECK(!recorder_); |
@@ -55,6 +58,8 @@ OpenSLESInputStream::~OpenSLESInputStream() { |
} |
bool OpenSLESInputStream::Open() { |
+ DVLOG(2) << "OpenSLESInputStream::Open()"; |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
if (engine_object_.Get()) |
return false; |
@@ -67,27 +72,33 @@ bool OpenSLESInputStream::Open() { |
} |
void OpenSLESInputStream::Start(AudioInputCallback* callback) { |
+ DVLOG(2) << "OpenSLESInputStream::Start()"; |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
DCHECK(callback); |
DCHECK(recorder_); |
DCHECK(simple_buffer_queue_); |
- if (started_) |
- return; |
- |
- // Enable the flags before streaming. |
- callback_ = callback; |
- active_queue_ = 0; |
- started_ = true; |
SLresult err = SL_RESULT_UNKNOWN_ERROR; |
- // Enqueues |kNumOfQueuesInBuffer| zero buffers to get the ball rolling. |
- for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
- err = (*simple_buffer_queue_)->Enqueue( |
- simple_buffer_queue_, |
- audio_data_[i], |
- buffer_size_bytes_); |
- if (SL_RESULT_SUCCESS != err) { |
- HandleError(err); |
+ { |
+ base::AutoLock lock(lock_); |
+ if (started_) |
wjia(left Chromium)
2013/08/29 14:21:55
No need to guard this check since this is the only
|
return; |
+ |
+ // Enable the flags before streaming. |
+ callback_ = callback; |
+ active_queue_ = 0; |
+ started_ = true; |
+ |
+ // Enqueues |kNumOfQueuesInBuffer| zero buffers to get the ball rolling. |
+ for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
+ err = (*simple_buffer_queue_)->Enqueue( |
+ simple_buffer_queue_, |
+ audio_data_[i], |
+ buffer_size_bytes_); |
+ if (SL_RESULT_SUCCESS != err) { |
+ HandleError(err); |
+ return; |
+ } |
} |
} |
@@ -98,33 +109,60 @@ void OpenSLESInputStream::Start(AudioInputCallback* callback) { |
} |
void OpenSLESInputStream::Stop() { |
- if (!started_) |
- return; |
+ DVLOG(2) << "OpenSLESInputStream::Stop()"; |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ { |
+ base::AutoLock lock(lock_); |
+ if (!started_) |
wjia(left Chromium)
2013/08/29 14:21:55
ditto.
|
+ return; |
+ } |
// Stop recording by setting the record state to |SL_RECORDSTATE_STOPPED|. |
LOG_ON_FAILURE_AND_RETURN( |
(*recorder_)->SetRecordState(recorder_, |
SL_RECORDSTATE_STOPPED)); |
+ { |
+ base::AutoLock lock(lock_); |
- // Clear the buffer queue to get rid of old data when resuming recording. |
- LOG_ON_FAILURE_AND_RETURN( |
- (*simple_buffer_queue_)->Clear(simple_buffer_queue_)); |
+ // Clear the buffer queue to get rid of old data when resuming recording. |
+ LOG_ON_FAILURE_AND_RETURN( |
+ (*simple_buffer_queue_)->Clear(simple_buffer_queue_)); |
- started_ = false; |
+ started_ = false; |
+ } |
} |
void OpenSLESInputStream::Close() { |
+ DVLOG(2) << "OpenSLESInputStream::Close()"; |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
// Stop the stream if it is still recording. |
Stop(); |
wjia(left Chromium)
2013/08/29 14:21:55
|lock_| is not needed after this point, since |sta
|
- // Explicitly free the player objects and invalidate their associated |
- // interfaces. They have to be done in the correct order. |
+ { |
+ base::AutoLock lock(lock_); |
+ if (callback_) { |
+ callback_->OnClose(this); |
+ callback_ = NULL; |
+ } |
+ } |
+ |
+ // Destroy the buffer queue recorder object and invalidate all associated |
+ // interfaces. |
recorder_object_.Reset(); |
- engine_object_.Reset(); |
- simple_buffer_queue_ = NULL; |
+ { |
+ base::AutoLock lock(lock_); |
+ simple_buffer_queue_ = NULL; |
+ } |
recorder_ = NULL; |
- ReleaseAudioBuffer(); |
+ // Destroy the engine object. We don't store any associated interface for |
+ // this object. |
+ engine_object_.Reset(); |
+ { |
+ base::AutoLock lock(lock_); |
+ ReleaseAudioBuffer(); |
+ } |
audio_manager_->ReleaseInputStream(this); |
} |
@@ -153,6 +191,12 @@ bool OpenSLESInputStream::GetAutomaticGainControl() { |
} |
bool OpenSLESInputStream::CreateRecorder() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(!engine_object_.Get()); |
+ DCHECK(!recorder_object_.Get()); |
+ DCHECK(!recorder_); |
+ DCHECK(!simple_buffer_queue_); |
+ |
// Initializes the engine object with specific option. After working with the |
// object, we need to free the object and its resources. |
SLEngineOption option[] = { |
@@ -201,6 +245,7 @@ bool OpenSLESInputStream::CreateRecorder() { |
SL_BOOLEAN_TRUE, |
SL_BOOLEAN_TRUE |
}; |
+ |
// Create AudioRecorder and specify SL_IID_ANDROIDCONFIGURATION. |
LOG_ON_FAILURE_AND_RETURN( |
(*engine)->CreateAudioRecorder(engine, |
@@ -265,6 +310,7 @@ void OpenSLESInputStream::SimpleBufferQueueCallback( |
} |
void OpenSLESInputStream::ReadBufferQueue() { |
+ base::AutoLock lock(lock_); |
if (!started_) |
return; |
@@ -287,6 +333,7 @@ void OpenSLESInputStream::ReadBufferQueue() { |
} |
void OpenSLESInputStream::SetupAudioBuffer() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
DCHECK(!audio_data_[0]); |
for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
audio_data_[i] = new uint8[buffer_size_bytes_]; |
@@ -294,6 +341,7 @@ void OpenSLESInputStream::SetupAudioBuffer() { |
} |
void OpenSLESInputStream::ReleaseAudioBuffer() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
if (audio_data_[0]) { |
for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
delete [] audio_data_[i]; |