Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: media/audio/android/opensles_input.cc

Issue 23296008: Adding audio unit tests for Android (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Now supports Start,Stop,Start Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/android/opensles_input.h ('k') | media/audio/android/opensles_output.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/audio/android/opensles_input.h" 5 #include "media/audio/android/opensles_input.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "media/audio/android/audio_manager_android.h" 8 #include "media/audio/android/audio_manager_android.h"
9 9
10 #define LOG_ON_FAILURE_AND_RETURN(op, ...) \ 10 #define LOG_ON_FAILURE_AND_RETURN(op, ...) \
11 do { \ 11 do { \
12 SLresult err = (op); \ 12 SLresult err = (op); \
13 if (err != SL_RESULT_SUCCESS) { \ 13 if (err != SL_RESULT_SUCCESS) { \
14 DLOG(ERROR) << #op << " failed: " << err; \ 14 DLOG(ERROR) << #op << " failed: " << err; \
15 return __VA_ARGS__; \ 15 return __VA_ARGS__; \
16 } \ 16 } \
17 } while (0) 17 } while (0)
18 18
19 namespace media { 19 namespace media {
20 20
21 OpenSLESInputStream::OpenSLESInputStream(AudioManagerAndroid* audio_manager, 21 OpenSLESInputStream::OpenSLESInputStream(AudioManagerAndroid* audio_manager,
22 const AudioParameters& params) 22 const AudioParameters& params)
23 : audio_manager_(audio_manager), 23 : audio_manager_(audio_manager),
24 callback_(NULL), 24 callback_(NULL),
25 recorder_(NULL), 25 recorder_(NULL),
26 simple_buffer_queue_(NULL), 26 simple_buffer_queue_(NULL),
27 active_queue_(0), 27 active_queue_(0),
28 buffer_size_bytes_(0), 28 buffer_size_bytes_(0),
29 started_(false) { 29 started_(false) {
30 DVLOG(2) << "OpenSLESInputStream::OpenSLESInputStream()";
30 format_.formatType = SL_DATAFORMAT_PCM; 31 format_.formatType = SL_DATAFORMAT_PCM;
31 format_.numChannels = static_cast<SLuint32>(params.channels()); 32 format_.numChannels = static_cast<SLuint32>(params.channels());
32 // Provides sampling rate in milliHertz to OpenSLES. 33 // Provides sampling rate in milliHertz to OpenSLES.
33 format_.samplesPerSec = static_cast<SLuint32>(params.sample_rate() * 1000); 34 format_.samplesPerSec = static_cast<SLuint32>(params.sample_rate() * 1000);
34 format_.bitsPerSample = params.bits_per_sample(); 35 format_.bitsPerSample = params.bits_per_sample();
35 format_.containerSize = params.bits_per_sample(); 36 format_.containerSize = params.bits_per_sample();
36 format_.endianness = SL_BYTEORDER_LITTLEENDIAN; 37 format_.endianness = SL_BYTEORDER_LITTLEENDIAN;
37 if (format_.numChannels == 1) 38 if (format_.numChannels == 1)
38 format_.channelMask = SL_SPEAKER_FRONT_CENTER; 39 format_.channelMask = SL_SPEAKER_FRONT_CENTER;
39 else if (format_.numChannels == 2) 40 else if (format_.numChannels == 2)
40 format_.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; 41 format_.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
41 else 42 else
42 NOTREACHED() << "Unsupported number of channels: " << format_.numChannels; 43 NOTREACHED() << "Unsupported number of channels: " << format_.numChannels;
43 44
44 buffer_size_bytes_ = params.GetBytesPerBuffer(); 45 buffer_size_bytes_ = params.GetBytesPerBuffer();
45 46
46 memset(&audio_data_, 0, sizeof(audio_data_)); 47 memset(&audio_data_, 0, sizeof(audio_data_));
47 } 48 }
48 49
49 OpenSLESInputStream::~OpenSLESInputStream() { 50 OpenSLESInputStream::~OpenSLESInputStream() {
51 DVLOG(2) << "OpenSLESInputStream::~OpenSLESInputStream()";
52 DCHECK(thread_checker_.CalledOnValidThread());
50 DCHECK(!recorder_object_.Get()); 53 DCHECK(!recorder_object_.Get());
51 DCHECK(!engine_object_.Get()); 54 DCHECK(!engine_object_.Get());
52 DCHECK(!recorder_); 55 DCHECK(!recorder_);
53 DCHECK(!simple_buffer_queue_); 56 DCHECK(!simple_buffer_queue_);
54 DCHECK(!audio_data_[0]); 57 DCHECK(!audio_data_[0]);
55 } 58 }
56 59
57 bool OpenSLESInputStream::Open() { 60 bool OpenSLESInputStream::Open() {
61 DVLOG(2) << "OpenSLESInputStream::Open()";
62 DCHECK(thread_checker_.CalledOnValidThread());
58 if (engine_object_.Get()) 63 if (engine_object_.Get())
59 return false; 64 return false;
60 65
61 if (!CreateRecorder()) 66 if (!CreateRecorder())
62 return false; 67 return false;
63 68
64 SetupAudioBuffer(); 69 SetupAudioBuffer();
65 70
66 return true; 71 return true;
67 } 72 }
68 73
69 void OpenSLESInputStream::Start(AudioInputCallback* callback) { 74 void OpenSLESInputStream::Start(AudioInputCallback* callback) {
75 DVLOG(2) << "OpenSLESInputStream::Start()";
76 DCHECK(thread_checker_.CalledOnValidThread());
70 DCHECK(callback); 77 DCHECK(callback);
71 DCHECK(recorder_); 78 DCHECK(recorder_);
72 DCHECK(simple_buffer_queue_); 79 DCHECK(simple_buffer_queue_);
80
81 base::AutoLock lock(lock_);
73 if (started_) 82 if (started_)
tommi (sloooow) - chröme 2013/08/30 12:43:34 now that you don't touch started_ in the callback,
henrika (OOO until Aug 14) 2013/08/30 14:20:37 oops, thanks
74 return; 83 return;
75 84
76 // Enable the flags before streaming. 85 // Enable the flags before streaming.
77 callback_ = callback; 86 callback_ = callback;
tommi (sloooow) - chröme 2013/08/30 12:43:34 should we dcheck that callback_ is NULL before ass
henrika (OOO until Aug 14) 2013/08/30 14:20:37 Done.
78 active_queue_ = 0; 87 active_queue_ = 0;
79 started_ = true; 88 started_ = true;
80 89
81 SLresult err = SL_RESULT_UNKNOWN_ERROR; 90
82 // Enqueues |kNumOfQueuesInBuffer| zero buffers to get the ball rolling. 91 SLAndroidSimpleBufferQueueState buffer_queue_state;
83 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { 92 SLresult err = (*simple_buffer_queue_)->GetState(simple_buffer_queue_,
84 err = (*simple_buffer_queue_)->Enqueue( 93 &buffer_queue_state);
85 simple_buffer_queue_, 94 if (SL_RESULT_SUCCESS != err) {
86 audio_data_[i], 95 HandleError(err);
tommi (sloooow) - chröme 2013/08/30 12:43:34 set started_ to false? alternatively don't set st
henrika (OOO until Aug 14) 2013/08/30 14:20:37 Moved it to the end since we don't check it in the
87 buffer_size_bytes_); 96 return;
88 if (SL_RESULT_SUCCESS != err) { 97 }
89 HandleError(err); 98
90 return; 99 // Enqueues |kNumOfQueuesInBuffer| zero buffers to get the ball rolling
100 // but only if the queue is empty. It can be non-empty if Stop has been
101 // called followed by a new call to Start. We do clear the queue in Stop,
102 // and the buffer state should then be cleared, but for some reason it is
103 // not. Using this approach enables call sequences like: Start, Stop, Start,
104 // even if there might be some old data remaining at the second call to
105 // Start.
106 if (buffer_queue_state.index == 0) {
107 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) {
108 err = (*simple_buffer_queue_)->Enqueue(
109 simple_buffer_queue_,
110 audio_data_[i],
111 buffer_size_bytes_);
112 if (SL_RESULT_SUCCESS != err) {
113 HandleError(err);
114 return;
115 }
91 } 116 }
92 } 117 }
93 118
94 // Start the recording by setting the state to |SL_RECORDSTATE_RECORDING|. 119 // Start the recording by setting the state to |SL_RECORDSTATE_RECORDING|.
95 err = (*recorder_)->SetRecordState(recorder_, SL_RECORDSTATE_RECORDING); 120 err = (*recorder_)->SetRecordState(recorder_, SL_RECORDSTATE_RECORDING);
96 if (SL_RESULT_SUCCESS != err) 121 if (SL_RESULT_SUCCESS != err) {
97 HandleError(err); 122 HandleError(err);
123 }
98 } 124 }
99 125
100 void OpenSLESInputStream::Stop() { 126 void OpenSLESInputStream::Stop() {
127 DVLOG(2) << "OpenSLESInputStream::Stop()";
128 DCHECK(thread_checker_.CalledOnValidThread());
129
130 base::AutoLock lock(lock_);
tommi (sloooow) - chröme 2013/08/30 12:43:34 same here
henrika (OOO until Aug 14) 2013/08/30 14:20:37 Done.
101 if (!started_) 131 if (!started_)
102 return; 132 return;
103 133
104 // Stop recording by setting the record state to |SL_RECORDSTATE_STOPPED|. 134 // Stop recording by setting the record state to |SL_RECORDSTATE_STOPPED|.
105 LOG_ON_FAILURE_AND_RETURN( 135 LOG_ON_FAILURE_AND_RETURN(
106 (*recorder_)->SetRecordState(recorder_, 136 (*recorder_)->SetRecordState(recorder_,
107 SL_RECORDSTATE_STOPPED)); 137 SL_RECORDSTATE_STOPPED));
108 138
109 // Clear the buffer queue to get rid of old data when resuming recording. 139 // Clear the buffer queue to get rid of old data when resuming recording.
110 LOG_ON_FAILURE_AND_RETURN( 140 LOG_ON_FAILURE_AND_RETURN(
111 (*simple_buffer_queue_)->Clear(simple_buffer_queue_)); 141 (*simple_buffer_queue_)->Clear(simple_buffer_queue_));
112 142
113 started_ = false; 143 started_ = false;
114 } 144 }
115 145
116 void OpenSLESInputStream::Close() { 146 void OpenSLESInputStream::Close() {
147 DVLOG(2) << "OpenSLESInputStream::Close()";
148 DCHECK(thread_checker_.CalledOnValidThread());
149
117 // Stop the stream if it is still recording. 150 // Stop the stream if it is still recording.
118 Stop(); 151 Stop();
119 152
120 // Explicitly free the player objects and invalidate their associated 153 {
121 // interfaces. They have to be done in the correct order. 154 base::AutoLock lock(lock_);
122 recorder_object_.Reset();
123 engine_object_.Reset();
124 simple_buffer_queue_ = NULL;
125 recorder_ = NULL;
126 155
127 ReleaseAudioBuffer(); 156 if (callback_) {
157 callback_->OnClose(this);
158 callback_ = NULL;
159 }
160
161 // Destroy the buffer queue recorder object and invalidate all associated
162 // interfaces.
163 recorder_object_.Reset();
164 simple_buffer_queue_ = NULL;
165 recorder_ = NULL;
166
167 // Destroy the engine object. We don't store any associated interface for
168 // this object.
169 engine_object_.Reset();
170 ReleaseAudioBuffer();
171 }
128 172
129 audio_manager_->ReleaseInputStream(this); 173 audio_manager_->ReleaseInputStream(this);
130 } 174 }
131 175
132 double OpenSLESInputStream::GetMaxVolume() { 176 double OpenSLESInputStream::GetMaxVolume() {
133 NOTIMPLEMENTED(); 177 NOTIMPLEMENTED();
134 return 0.0; 178 return 0.0;
135 } 179 }
136 180
137 void OpenSLESInputStream::SetVolume(double volume) { 181 void OpenSLESInputStream::SetVolume(double volume) {
138 NOTIMPLEMENTED(); 182 NOTIMPLEMENTED();
139 } 183 }
140 184
141 double OpenSLESInputStream::GetVolume() { 185 double OpenSLESInputStream::GetVolume() {
142 NOTIMPLEMENTED(); 186 NOTIMPLEMENTED();
143 return 0.0; 187 return 0.0;
144 } 188 }
145 189
146 void OpenSLESInputStream::SetAutomaticGainControl(bool enabled) { 190 void OpenSLESInputStream::SetAutomaticGainControl(bool enabled) {
147 NOTIMPLEMENTED(); 191 NOTIMPLEMENTED();
148 } 192 }
149 193
150 bool OpenSLESInputStream::GetAutomaticGainControl() { 194 bool OpenSLESInputStream::GetAutomaticGainControl() {
151 NOTIMPLEMENTED(); 195 NOTIMPLEMENTED();
152 return false; 196 return false;
153 } 197 }
154 198
155 bool OpenSLESInputStream::CreateRecorder() { 199 bool OpenSLESInputStream::CreateRecorder() {
200 DCHECK(thread_checker_.CalledOnValidThread());
201 DCHECK(!engine_object_.Get());
202 DCHECK(!recorder_object_.Get());
203 DCHECK(!recorder_);
204 DCHECK(!simple_buffer_queue_);
205
156 // Initializes the engine object with specific option. After working with the 206 // Initializes the engine object with specific option. After working with the
157 // object, we need to free the object and its resources. 207 // object, we need to free the object and its resources.
158 SLEngineOption option[] = { 208 SLEngineOption option[] = {
159 { SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE) } 209 { SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE) }
160 }; 210 };
161 LOG_ON_FAILURE_AND_RETURN(slCreateEngine(engine_object_.Receive(), 211 LOG_ON_FAILURE_AND_RETURN(slCreateEngine(engine_object_.Receive(),
162 1, 212 1,
163 option, 213 option,
164 0, 214 0,
165 NULL, 215 NULL,
(...skipping 28 matching lines...) Expand all
194 244
195 // Create an audio recorder. 245 // Create an audio recorder.
196 const SLInterfaceID interface_id[] = { 246 const SLInterfaceID interface_id[] = {
197 SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 247 SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
198 SL_IID_ANDROIDCONFIGURATION 248 SL_IID_ANDROIDCONFIGURATION
199 }; 249 };
200 const SLboolean interface_required[] = { 250 const SLboolean interface_required[] = {
201 SL_BOOLEAN_TRUE, 251 SL_BOOLEAN_TRUE,
202 SL_BOOLEAN_TRUE 252 SL_BOOLEAN_TRUE
203 }; 253 };
254
204 // Create AudioRecorder and specify SL_IID_ANDROIDCONFIGURATION. 255 // Create AudioRecorder and specify SL_IID_ANDROIDCONFIGURATION.
205 LOG_ON_FAILURE_AND_RETURN( 256 LOG_ON_FAILURE_AND_RETURN(
206 (*engine)->CreateAudioRecorder(engine, 257 (*engine)->CreateAudioRecorder(engine,
207 recorder_object_.Receive(), 258 recorder_object_.Receive(),
208 &audio_source, 259 &audio_source,
209 &audio_sink, 260 &audio_sink,
210 arraysize(interface_id), 261 arraysize(interface_id),
211 interface_id, 262 interface_id,
212 interface_required), 263 interface_required),
213 false); 264 false);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 } 309 }
259 310
260 void OpenSLESInputStream::SimpleBufferQueueCallback( 311 void OpenSLESInputStream::SimpleBufferQueueCallback(
261 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance) { 312 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance) {
262 OpenSLESInputStream* stream = 313 OpenSLESInputStream* stream =
263 reinterpret_cast<OpenSLESInputStream*>(instance); 314 reinterpret_cast<OpenSLESInputStream*>(instance);
264 stream->ReadBufferQueue(); 315 stream->ReadBufferQueue();
265 } 316 }
266 317
267 void OpenSLESInputStream::ReadBufferQueue() { 318 void OpenSLESInputStream::ReadBufferQueue() {
268 if (!started_) 319 base::AutoLock lock(lock_);
320
321 // Verify that we are in a recording state.
322 SLuint32 state;
323 SLresult err = (*recorder_)->GetRecordState(recorder_, &state);
324 if (SL_RESULT_SUCCESS != err)
325 HandleError(err);
tommi (sloooow) - chröme 2013/08/30 12:43:34 missing return?
henrika (OOO until Aug 14) 2013/08/30 14:20:37 Done.
326 if (state != SL_RECORDSTATE_RECORDING) {
327 DLOG(WARNING) << "Invalid callback in non-recording state";
tommi (sloooow) - chröme 2013/08/30 12:43:34 nit: s/Invalid/Received
henrika (OOO until Aug 14) 2013/08/30 14:20:37 Done.
269 return; 328 return;
329 }
270 330
271 // TODO(xians): Get an accurate delay estimation. 331 // TODO(xians): Get an accurate delay estimation.
272 callback_->OnData(this, 332 callback_->OnData(this,
273 audio_data_[active_queue_], 333 audio_data_[active_queue_],
274 buffer_size_bytes_, 334 buffer_size_bytes_,
275 buffer_size_bytes_, 335 buffer_size_bytes_,
276 0.0); 336 0.0);
277 337
278 // Done with this buffer. Send it to device for recording. 338 // Done with this buffer. Send it to device for recording.
279 SLresult err = (*simple_buffer_queue_)->Enqueue( 339 err = (*simple_buffer_queue_)->Enqueue(
280 simple_buffer_queue_, 340 simple_buffer_queue_,
281 audio_data_[active_queue_], 341 audio_data_[active_queue_],
282 buffer_size_bytes_); 342 buffer_size_bytes_);
283 if (SL_RESULT_SUCCESS != err) 343 if (SL_RESULT_SUCCESS != err)
284 HandleError(err); 344 HandleError(err);
285 345
286 active_queue_ = (active_queue_ + 1) % kNumOfQueuesInBuffer; 346 active_queue_ = (active_queue_ + 1) % kNumOfQueuesInBuffer;
tommi (sloooow) - chröme 2013/08/30 12:43:34 if Enqueue failed, is this still the right thing t
henrika (OOO until Aug 14) 2013/08/30 14:20:37 Really good comment. I rewrote Start to ensure th
287 } 347 }
288 348
289 void OpenSLESInputStream::SetupAudioBuffer() { 349 void OpenSLESInputStream::SetupAudioBuffer() {
350 DCHECK(thread_checker_.CalledOnValidThread());
290 DCHECK(!audio_data_[0]); 351 DCHECK(!audio_data_[0]);
291 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { 352 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) {
292 audio_data_[i] = new uint8[buffer_size_bytes_]; 353 audio_data_[i] = new uint8[buffer_size_bytes_];
293 } 354 }
294 } 355 }
295 356
296 void OpenSLESInputStream::ReleaseAudioBuffer() { 357 void OpenSLESInputStream::ReleaseAudioBuffer() {
358 DCHECK(thread_checker_.CalledOnValidThread());
297 if (audio_data_[0]) { 359 if (audio_data_[0]) {
298 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { 360 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) {
299 delete [] audio_data_[i]; 361 delete [] audio_data_[i];
300 audio_data_[i] = NULL; 362 audio_data_[i] = NULL;
301 } 363 }
302 } 364 }
303 } 365 }
304 366
305 void OpenSLESInputStream::HandleError(SLresult error) { 367 void OpenSLESInputStream::HandleError(SLresult error) {
306 DLOG(FATAL) << "OpenSLES Input error " << error; 368 DLOG(ERROR) << "OpenSLES Input error " << error;
307 if (callback_) 369 if (callback_)
308 callback_->OnError(this); 370 callback_->OnError(this);
309 } 371 }
310 372
311 } // namespace media 373 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/android/opensles_input.h ('k') | media/audio/android/opensles_output.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698