OLD | NEW |
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 namespace media { | 10 namespace media { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 if (started_) | 64 if (started_) |
65 return; | 65 return; |
66 | 66 |
67 // Enable the flags before streaming. | 67 // Enable the flags before streaming. |
68 callback_ = callback; | 68 callback_ = callback; |
69 active_queue_ = 0; | 69 active_queue_ = 0; |
70 started_ = true; | 70 started_ = true; |
71 | 71 |
72 SLresult err = SL_RESULT_UNKNOWN_ERROR; | 72 SLresult err = SL_RESULT_UNKNOWN_ERROR; |
73 // Enqueues |kNumOfQueuesInBuffer| zero buffers to get the ball rolling. | 73 // Enqueues |kNumOfQueuesInBuffer| zero buffers to get the ball rolling. |
74 for (int i = 0; i < kNumOfQueuesInBuffer - 1; ++i) { | 74 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
75 err = (*simple_buffer_queue_)->Enqueue( | 75 err = (*simple_buffer_queue_)->Enqueue( |
76 simple_buffer_queue_, | 76 simple_buffer_queue_, |
77 audio_data_[i], | 77 audio_data_[i], |
78 buffer_size_bytes_); | 78 buffer_size_bytes_); |
79 if (SL_RESULT_SUCCESS != err) { | 79 if (SL_RESULT_SUCCESS != err) { |
80 HandleError(err); | 80 HandleError(err); |
81 return; | 81 return; |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 180 |
181 // Audio source configuration. | 181 // Audio source configuration. |
182 SLDataLocator_IODevice mic_locator = { | 182 SLDataLocator_IODevice mic_locator = { |
183 SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, | 183 SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, |
184 SL_DEFAULTDEVICEID_AUDIOINPUT, NULL | 184 SL_DEFAULTDEVICEID_AUDIOINPUT, NULL |
185 }; | 185 }; |
186 SLDataSource audio_source = { &mic_locator, NULL }; | 186 SLDataSource audio_source = { &mic_locator, NULL }; |
187 | 187 |
188 // Audio sink configuration. | 188 // Audio sink configuration. |
189 SLDataLocator_AndroidSimpleBufferQueue buffer_queue = { | 189 SLDataLocator_AndroidSimpleBufferQueue buffer_queue = { |
190 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // Locator type. | 190 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // Locator type. |
191 static_cast<SLuint32>(kNumOfQueuesInBuffer) // Number of buffers. | 191 static_cast<SLuint32>(kNumOfQueuesInBuffer) // Number of buffers. |
192 }; | 192 }; |
193 SLDataSink audio_sink = { &buffer_queue, &format_ }; | 193 SLDataSink audio_sink = { &buffer_queue, &format_ }; |
194 | 194 |
195 // Create an audio recorder. | 195 // Create an audio recorder. |
196 const SLuint32 number_of_interfaces = 1; | 196 const SLuint32 number_of_interfaces = 1; |
197 const SLInterfaceID interface_id[number_of_interfaces] = { | 197 const SLInterfaceID interface_id[number_of_interfaces] = { |
198 SL_IID_ANDROIDSIMPLEBUFFERQUEUE | 198 SL_IID_ANDROIDSIMPLEBUFFERQUEUE |
199 }; | 199 }; |
200 const SLboolean interface_required[number_of_interfaces] = { | 200 const SLboolean interface_required[number_of_interfaces] = { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance) { | 251 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance) { |
252 OpenSLESInputStream* stream = | 252 OpenSLESInputStream* stream = |
253 reinterpret_cast<OpenSLESInputStream*>(instance); | 253 reinterpret_cast<OpenSLESInputStream*>(instance); |
254 stream->ReadBufferQueue(); | 254 stream->ReadBufferQueue(); |
255 } | 255 } |
256 | 256 |
257 void OpenSLESInputStream::ReadBufferQueue() { | 257 void OpenSLESInputStream::ReadBufferQueue() { |
258 if (!started_) | 258 if (!started_) |
259 return; | 259 return; |
260 | 260 |
261 // Get the enqueued buffer from the soundcard. | 261 // TODO(xians): Get an accurate delay estimation. |
| 262 callback_->OnData(this, |
| 263 audio_data_[active_queue_], |
| 264 buffer_size_bytes_, |
| 265 buffer_size_bytes_, |
| 266 0.0); |
| 267 |
| 268 // Done with this buffer. Send it to device for recording. |
262 SLresult err = (*simple_buffer_queue_)->Enqueue( | 269 SLresult err = (*simple_buffer_queue_)->Enqueue( |
263 simple_buffer_queue_, | 270 simple_buffer_queue_, |
264 audio_data_[active_queue_], | 271 audio_data_[active_queue_], |
265 buffer_size_bytes_); | 272 buffer_size_bytes_); |
266 if (SL_RESULT_SUCCESS != err) | 273 if (SL_RESULT_SUCCESS != err) |
267 HandleError(err); | 274 HandleError(err); |
268 | 275 |
269 // TODO(xians): Get an accurate delay estimation. | |
270 callback_->OnData(this, | |
271 audio_data_[active_queue_], | |
272 buffer_size_bytes_, | |
273 buffer_size_bytes_, | |
274 0.0); | |
275 | |
276 active_queue_ = (active_queue_ + 1) % kNumOfQueuesInBuffer; | 276 active_queue_ = (active_queue_ + 1) % kNumOfQueuesInBuffer; |
277 } | 277 } |
278 | 278 |
279 void OpenSLESInputStream::SetupAudioBuffer() { | 279 void OpenSLESInputStream::SetupAudioBuffer() { |
280 DCHECK(!audio_data_[0]); | 280 DCHECK(!audio_data_[0]); |
281 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { | 281 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
282 audio_data_[i] = new uint8[buffer_size_bytes_]; | 282 audio_data_[i] = new uint8[buffer_size_bytes_]; |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 void OpenSLESInputStream::ReleaseAudioBuffer() { | 286 void OpenSLESInputStream::ReleaseAudioBuffer() { |
287 if (audio_data_[0]) { | 287 if (audio_data_[0]) { |
288 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { | 288 for (int i = 0; i < kNumOfQueuesInBuffer; ++i) { |
289 delete [] audio_data_[i]; | 289 delete [] audio_data_[i]; |
290 audio_data_[i] = NULL; | 290 audio_data_[i] = NULL; |
291 } | 291 } |
292 } | 292 } |
293 } | 293 } |
294 | 294 |
295 void OpenSLESInputStream::HandleError(SLresult error) { | 295 void OpenSLESInputStream::HandleError(SLresult error) { |
296 DLOG(FATAL) << "OpenSLES error " << error; | 296 DLOG(FATAL) << "OpenSLES error " << error; |
297 if (callback_) | 297 if (callback_) |
298 callback_->OnError(this); | 298 callback_->OnError(this); |
299 } | 299 } |
300 | 300 |
301 } // namespace media | 301 } // namespace media |
OLD | NEW |