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

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

Issue 12806009: Add OpenSL configurations (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: add opensles_outtput.h Created 7 years, 9 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
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 namespace media { 10 namespace media {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
85 // Start the recording by setting the state to |SL_RECORDSTATE_RECORDING|. 85 // Start the recording by setting the state to |SL_RECORDSTATE_RECORDING|.
86 err = (*recorder_)->SetRecordState(recorder_, SL_RECORDSTATE_RECORDING); 86 err = (*recorder_)->SetRecordState(recorder_, SL_RECORDSTATE_RECORDING);
87 DCHECK_EQ(SL_RESULT_SUCCESS, err); 87 if (SL_RESULT_SUCCESS != err) {
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 I hope you can make a pass (in a different CL) cle
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 What's the deal with the removal of the DCHECKs ev
leozwang1 2013/03/22 21:37:09 sure I will.
leozwang1 2013/03/22 21:37:09 Both Tommi and Qinmin suggested to remove DCHECK a
leozwang1 2013/03/23 07:00:39 Done.
leozwang1 2013/03/23 07:00:39 Done.
88 if (SL_RESULT_SUCCESS != err)
89 HandleError(err); 88 HandleError(err);
89 }
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 FYI braces are optional in the C++ style guide for
90 } 90 }
91 91
92 void OpenSLESInputStream::Stop() { 92 void OpenSLESInputStream::Stop() {
93 if (!started_) 93 if (!started_)
94 return; 94 return;
95 95
96 // Stop recording by setting the record state to |SL_RECORDSTATE_STOPPED|. 96 // Stop recording by setting the record state to |SL_RECORDSTATE_STOPPED|.
97 SLresult err = (*recorder_)->SetRecordState(recorder_, 97 SLresult err = (*recorder_)->SetRecordState(recorder_,
98 SL_RECORDSTATE_STOPPED); 98 SL_RECORDSTATE_STOPPED);
99 if (SL_RESULT_SUCCESS != err) { 99 if (SL_RESULT_SUCCESS != err) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 // object, we need to free the object and its resources. 153 // object, we need to free the object and its resources.
154 SLEngineOption option[] = { 154 SLEngineOption option[] = {
155 { SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE) } 155 { SL_ENGINEOPTION_THREADSAFE, static_cast<SLuint32>(SL_BOOLEAN_TRUE) }
156 }; 156 };
157 SLresult err = slCreateEngine(engine_object_.Receive(), 157 SLresult err = slCreateEngine(engine_object_.Receive(),
158 1, 158 1,
159 option, 159 option,
160 0, 160 0,
161 NULL, 161 NULL,
162 NULL); 162 NULL);
163 DCHECK_EQ(SL_RESULT_SUCCESS, err); 163 if (SL_RESULT_SUCCESS != err) {
164 if (SL_RESULT_SUCCESS != err) 164 DLOG(ERROR) << "CreateAudioRecorder slCreateEngine: " << err;
165 return false; 165 return false;
166 }
166 167
167 // Realize the SL engine object in synchronous mode. 168 // Realize the SL engine object in synchronous mode.
168 err = engine_object_->Realize(engine_object_.Get(), SL_BOOLEAN_FALSE); 169 err = engine_object_->Realize(engine_object_.Get(), SL_BOOLEAN_FALSE);
169 DCHECK_EQ(SL_RESULT_SUCCESS, err); 170 if (SL_RESULT_SUCCESS != err) {
170 if (SL_RESULT_SUCCESS != err) 171 DLOG(ERROR) << "CreateAudioRecorder Realize: " << err;
171 return false; 172 return false;
173 }
172 174
173 // Get the SL engine interface which is implicit. 175 // Get the SL engine interface which is implicit.
174 SLEngineItf engine; 176 SLEngineItf engine;
175 err = engine_object_->GetInterface( 177 err = engine_object_->GetInterface(
176 engine_object_.Get(), SL_IID_ENGINE, &engine); 178 engine_object_.Get(), SL_IID_ENGINE, &engine);
177 DCHECK_EQ(SL_RESULT_SUCCESS, err); 179 if (SL_RESULT_SUCCESS != err) {
178 if (SL_RESULT_SUCCESS != err) 180 DLOG(ERROR) << "GetInterface(SL_IID_ENGINE): " << err;
179 return false; 181 return false;
182 }
180 183
181 // Audio source configuration. 184 // Audio source configuration.
182 SLDataLocator_IODevice mic_locator = { 185 SLDataLocator_IODevice mic_locator = {
183 SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, 186 SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
184 SL_DEFAULTDEVICEID_AUDIOINPUT, NULL 187 SL_DEFAULTDEVICEID_AUDIOINPUT, NULL
185 }; 188 };
186 SLDataSource audio_source = { &mic_locator, NULL }; 189 SLDataSource audio_source = { &mic_locator, NULL };
187 190
188 // Audio sink configuration. 191 // Audio sink configuration.
189 SLDataLocator_AndroidSimpleBufferQueue buffer_queue = { 192 SLDataLocator_AndroidSimpleBufferQueue buffer_queue = {
190 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // Locator type. 193 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // Locator type.
191 static_cast<SLuint32>(kNumOfQueuesInBuffer) // Number of buffers. 194 static_cast<SLuint32>(kNumOfQueuesInBuffer) // Number of buffers.
192 }; 195 };
193 SLDataSink audio_sink = { &buffer_queue, &format_ }; 196 SLDataSink audio_sink = { &buffer_queue, &format_ };
194 197
195 // Create an audio recorder. 198 // Create an audio recorder.
196 const SLuint32 number_of_interfaces = 1; 199 const SLInterfaceID interface_id[] = {
197 const SLInterfaceID interface_id[number_of_interfaces] = { 200 SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
198 SL_IID_ANDROIDSIMPLEBUFFERQUEUE 201 SL_IID_ANDROIDCONFIGURATION
199 }; 202 };
203 const SLuint32 number_of_interfaces = arraysize(interface_id);
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 can inline into the CreateAudioRecorder call.
leozwang1 2013/03/22 21:37:09 Done.
200 const SLboolean interface_required[number_of_interfaces] = { 204 const SLboolean interface_required[number_of_interfaces] = {
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 number_of_interface is unnecessary.
leozwang1 2013/03/22 21:37:09 Done.
205 SL_BOOLEAN_TRUE,
201 SL_BOOLEAN_TRUE 206 SL_BOOLEAN_TRUE
202 }; 207 };
203 err = (*engine)->CreateAudioRecorder(engine, 208 err = (*engine)->CreateAudioRecorder(engine,
204 recorder_object_.Receive(), 209 recorder_object_.Receive(),
205 &audio_source, 210 &audio_source,
206 &audio_sink, 211 &audio_sink,
207 number_of_interfaces, 212 number_of_interfaces,
208 interface_id, 213 interface_id,
209 interface_required); 214 interface_required);
210 DCHECK_EQ(SL_RESULT_SUCCESS, err);
211 if (SL_RESULT_SUCCESS != err) { 215 if (SL_RESULT_SUCCESS != err) {
212 DLOG(ERROR) << "CreateAudioRecorder failed with error code " << err; 216 DLOG(ERROR) << "CreateAudioRecorder failed with error code " << err;
213 return false; 217 return false;
214 } 218 }
215 219
220 // Create AudioRecorder and specify SL_IID_ANDROIDCONFIGURATION.
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 "Create AudioRecorder" comment is a bit late here,
leozwang1 2013/03/22 21:37:09 Done.
221 SLAndroidConfigurationItf recorder_config;
222 err = recorder_object_->GetInterface(recorder_object_.Get(),
223 SL_IID_ANDROIDCONFIGURATION,
224 &recorder_config);
225 if (SL_RESULT_SUCCESS != err) {
226 DLOG(ERROR) << "GetInterface(SL_IID_ANDROIDCONFIGURATION): " << err;
227 return false;
228 }
229
230 SLint32 stream_type = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
Ami GONE FROM CHROMIUM 2013/03/22 16:24:41 this variable is obscuring what the call below act
231 err = (*recorder_config)->SetConfiguration(
232 recorder_config,
233 SL_ANDROID_KEY_RECORDING_PRESET,
234 &stream_type, sizeof(SLint32));
235 if (SL_RESULT_SUCCESS != err) {
236 DLOG(ERROR)
237 << "SetConfiguration(SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION): "
238 << err;
239 return false;
240 }
241
216 // Realize the recorder object in synchronous mode. 242 // Realize the recorder object in synchronous mode.
217 err = recorder_object_->Realize(recorder_object_.Get(), SL_BOOLEAN_FALSE); 243 err = recorder_object_->Realize(recorder_object_.Get(), SL_BOOLEAN_FALSE);
218 DCHECK_EQ(SL_RESULT_SUCCESS, err);
219 if (SL_RESULT_SUCCESS != err) { 244 if (SL_RESULT_SUCCESS != err) {
220 DLOG(ERROR) << "Recprder Realize() failed with error code " << err; 245 DLOG(ERROR) << "Recprder Realize() failed with error code " << err;
221 return false; 246 return false;
222 } 247 }
223 248
224 // Get an implicit recorder interface. 249 // Get an implicit recorder interface.
225 err = recorder_object_->GetInterface(recorder_object_.Get(), 250 err = recorder_object_->GetInterface(recorder_object_.Get(),
226 SL_IID_RECORD, 251 SL_IID_RECORD,
227 &recorder_); 252 &recorder_);
228 DCHECK_EQ(SL_RESULT_SUCCESS, err); 253 if (SL_RESULT_SUCCESS != err) {
229 if (SL_RESULT_SUCCESS != err) 254 DLOG(ERROR) << "GetInterface(SL_IID_RECORD): " << err;
230 return false; 255 return false;
256 }
231 257
232 // Get the simple buffer queue interface. 258 // Get the simple buffer queue interface.
233 err = recorder_object_->GetInterface(recorder_object_.Get(), 259 err = recorder_object_->GetInterface(recorder_object_.Get(),
234 SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 260 SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
235 &simple_buffer_queue_); 261 &simple_buffer_queue_);
236 DCHECK_EQ(SL_RESULT_SUCCESS, err); 262 if (SL_RESULT_SUCCESS != err) {
237 if (SL_RESULT_SUCCESS != err) 263 DLOG(ERROR) << "GetInterface(SL_IID_ANDROIDSIMPLEBUFFERQUEUE): " << err;
238 return false; 264 return false;
265 }
239 266
240 // Register the input callback for the simple buffer queue. 267 // Register the input callback for the simple buffer queue.
241 // This callback will be called when receiving new data from the device. 268 // This callback will be called when receiving new data from the device.
242 err = (*simple_buffer_queue_)->RegisterCallback(simple_buffer_queue_, 269 err = (*simple_buffer_queue_)->RegisterCallback(simple_buffer_queue_,
243 SimpleBufferQueueCallback, 270 SimpleBufferQueueCallback,
244 this); 271 this);
245 DCHECK_EQ(SL_RESULT_SUCCESS, err); 272 if (SL_RESULT_SUCCESS != err) {
273 DLOG(ERROR) << "AudioRecorder RegisterCallback: " << err;
274 return false;
275 }
246 276
247 return (SL_RESULT_SUCCESS == err); 277 return true;
248 } 278 }
249 279
250 void OpenSLESInputStream::SimpleBufferQueueCallback( 280 void OpenSLESInputStream::SimpleBufferQueueCallback(
251 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance) { 281 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance) {
252 OpenSLESInputStream* stream = 282 OpenSLESInputStream* stream =
253 reinterpret_cast<OpenSLESInputStream*>(instance); 283 reinterpret_cast<OpenSLESInputStream*>(instance);
254 stream->ReadBufferQueue(); 284 stream->ReadBufferQueue();
255 } 285 }
256 286
257 void OpenSLESInputStream::ReadBufferQueue() { 287 void OpenSLESInputStream::ReadBufferQueue() {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 } 322 }
293 } 323 }
294 324
295 void OpenSLESInputStream::HandleError(SLresult error) { 325 void OpenSLESInputStream::HandleError(SLresult error) {
296 DLOG(FATAL) << "OpenSLES error " << error; 326 DLOG(FATAL) << "OpenSLES error " << error;
297 if (callback_) 327 if (callback_)
298 callback_->OnError(this, error); 328 callback_->OnError(this, error);
299 } 329 }
300 330
301 } // namespace media 331 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698