slesTestRecBuffQueue.cpp revision 4dd1fab74463de4852b86af64481006f87d48b54
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/* Audio Record Test 18 19First run the program from shell: 20 # slesTest_recBuffQueue /sdcard/myrec.raw 4 21 22These use adb on host to retrive the file: 23 % adb pull /sdcard/myrec.raw myrec.raw 24 25How to examine the output with Audacity: 26 Project / Import raw data 27 Select myrec.raw file, then click Open button 28 Choose these options: 29 Signed 16-bit PCM 30 Little-endian 31 1 Channel (Mono) 32 Sample rate 22050 Hz 33 Click Import button 34 35*/ 36 37 38#include <stdlib.h> 39#include <stdio.h> 40#include <string.h> 41#include <unistd.h> 42#include <sys/time.h> 43#include <fcntl.h> 44 45#include <SLES/OpenSLES.h> 46#include <SLES/OpenSLES_Android.h> 47#include <SLES/OpenSLES_AndroidConfiguration.h> 48 49/* Preset number to use for recording */ 50SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_NONE; 51 52/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_ANDROIDCONFIGURATION 53 * on the AudioRecorder object */ 54#define NUM_EXPLICIT_INTERFACES_FOR_RECORDER 2 55 56/* Size of the recording buffer queue */ 57#define NB_BUFFERS_IN_QUEUE 1 58/* Size of each buffer in the queue */ 59#define BUFFER_SIZE_IN_SAMPLES 1024 60#define BUFFER_SIZE_IN_BYTES (2*BUFFER_SIZE_IN_SAMPLES) 61 62/* Local storage for Audio data */ 63int8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES]; 64 65/* destination for recorded data */ 66static FILE* gFp; 67 68//----------------------------------------------------------------- 69/* Exits the application if an error is encountered */ 70#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 71 72void ExitOnErrorFunc( SLresult result , int line) 73{ 74 if (SL_RESULT_SUCCESS != result) { 75 fprintf(stdout, "%u error code encountered at line %d, exiting\n", result, line); 76 exit(EXIT_FAILURE); 77 } 78} 79 80//----------------------------------------------------------------- 81/* Structure for passing information to callback function */ 82typedef struct CallbackCntxt_ { 83 SLPlayItf playItf; 84 SLuint32 size; 85 SLint8* pDataBase; // Base address of local audio data storage 86 SLint8* pData; // Current address of local audio data storage 87} CallbackCntxt; 88 89 90//----------------------------------------------------------------- 91/* Callback for recording buffer queue events */ 92void RecCallback( 93 SLRecordItf caller, 94 void *pContext, 95 SLuint32 event) 96{ 97 if (SL_RECORDEVENT_HEADATNEWPOS & event) { 98 SLmillisecond pMsec = 0; 99 (*caller)->GetPosition(caller, &pMsec); 100 fprintf(stdout, "SL_RECORDEVENT_HEADATNEWPOS current position=%ums\n", pMsec); 101 } 102 103 if (SL_RECORDEVENT_HEADATMARKER & event) { 104 SLmillisecond pMsec = 0; 105 (*caller)->GetPosition(caller, &pMsec); 106 fprintf(stdout, "SL_RECORDEVENT_HEADATMARKER current position=%ums\n", pMsec); 107 } 108} 109 110//----------------------------------------------------------------- 111/* Callback for recording buffer queue events */ 112void RecBufferQueueCallback( 113 SLAndroidSimpleBufferQueueItf queueItf, 114 void *pContext) 115{ 116 //fprintf(stdout, "RecBufferQueueCallback called\n"); 117 118 CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 119 120 /* Save the recorded data */ 121 fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp); 122 123 /* Increase data pointer by buffer size */ 124 pCntxt->pData += BUFFER_SIZE_IN_BYTES; 125 126 if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) { 127 pCntxt->pData = pCntxt->pDataBase; 128 } 129 130 ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) ); 131 132 SLAndroidSimpleBufferQueueState recQueueState; 133 ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) ); 134 135 /*fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: " 136 "count=%u playIndex=%u\n", 137 pCntxt->pData, recQueueState.count, recQueueState.index);*/ 138} 139 140//----------------------------------------------------------------- 141 142/* Play an audio path by opening a file descriptor on that path */ 143void TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds) 144{ 145 gFp = fopen(path, "w"); 146 if (NULL == gFp) { 147 ExitOnError(SL_RESULT_RESOURCE_ERROR); 148 } 149 150 SLresult result; 151 SLEngineItf EngineItf; 152 153 /* Objects this application uses: one audio recorder */ 154 SLObjectItf recorder; 155 156 /* Interfaces for the audio recorder */ 157 SLAndroidSimpleBufferQueueItf recBuffQueueItf; 158 SLRecordItf recordItf; 159 SLAndroidConfigurationItf configItf; 160 161 /* Source of audio data for the recording */ 162 SLDataSource recSource; 163 SLDataLocator_IODevice ioDevice; 164 165 /* Data sink for recorded audio */ 166 SLDataSink recDest; 167 SLDataLocator_AndroidSimpleBufferQueue recBuffQueue; 168 SLDataFormat_PCM pcm; 169 170 SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 171 SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 172 173 /* Get the SL Engine Interface which is implicit */ 174 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 175 ExitOnError(result); 176 177 /* Initialize arrays required[] and iidArray[] */ 178 for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) { 179 required[i] = SL_BOOLEAN_FALSE; 180 iidArray[i] = SL_IID_NULL; 181 } 182 183 184 /* ------------------------------------------------------ */ 185 /* Configuration of the recorder */ 186 187 /* Request the AndroidSimpleBufferQueue and AndroidConfiguration interfaces */ 188 required[0] = SL_BOOLEAN_TRUE; 189 iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; 190 required[1] = SL_BOOLEAN_TRUE; 191 iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 192 193 /* Setup the data source */ 194 ioDevice.locatorType = SL_DATALOCATOR_IODEVICE; 195 ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT; 196 ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; 197 ioDevice.device = NULL; 198 recSource.pLocator = (void *) &ioDevice; 199 recSource.pFormat = NULL; 200 201 /* Setup the data sink */ 202 recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; 203 recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE; 204 /* set up the format of the data in the buffer queue */ 205 pcm.formatType = SL_DATAFORMAT_PCM; 206 pcm.numChannels = 1; 207 pcm.samplesPerSec = SL_SAMPLINGRATE_22_05; 208 pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; 209 pcm.containerSize = 16; 210 pcm.channelMask = SL_SPEAKER_FRONT_LEFT; 211 pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; 212 213 recDest.pLocator = (void *) &recBuffQueue; 214 recDest.pFormat = (void * ) &pcm; 215 216 /* Create the audio recorder */ 217 result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest, 218 NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required); 219 ExitOnError(result); 220 fprintf(stdout, "Recorder created\n"); 221 222 /* Get the Android configuration interface which is explicit */ 223 result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 224 ExitOnError(result); 225 226 /* Use the configuration interface to configure the recorder before it's realized */ 227 if (presetValue != SL_ANDROID_RECORDING_PRESET_NONE) { 228 result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 229 &presetValue, sizeof(SLuint32)); 230 ExitOnError(result); 231 fprintf(stdout, "Recorder parameterized with preset %u\n", presetValue); 232 } else { 233 printf("Using default record preset\n"); 234 } 235 236 SLuint32 presetRetrieved = SL_ANDROID_RECORDING_PRESET_NONE; 237 SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big 238 result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 239 &presetSize, (void*)&presetRetrieved); 240 ExitOnError(result); 241 if (presetValue == SL_ANDROID_RECORDING_PRESET_NONE) { 242 printf("The default record preset appears to be %u\n", presetRetrieved); 243 } else if (presetValue != presetRetrieved) { 244 fprintf(stderr, "Error retrieving recording preset as %u instead of %u\n", presetRetrieved, presetValue); 245 ExitOnError(SL_RESULT_INTERNAL_ERROR); 246 } 247 248 /* Realize the recorder in synchronous mode. */ 249 result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE); 250 ExitOnError(result); 251 fprintf(stdout, "Recorder realized\n"); 252 253 /* Get the record interface which is implicit */ 254 result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf); 255 ExitOnError(result); 256 257 /* Set up the recorder callback to get events during the recording */ 258 result = (*recordItf)->SetMarkerPosition(recordItf, 2000); 259 ExitOnError(result); 260 result = (*recordItf)->SetPositionUpdatePeriod(recordItf, 500); 261 ExitOnError(result); 262 result = (*recordItf)->SetCallbackEventsMask(recordItf, 263 SL_RECORDEVENT_HEADATMARKER | SL_RECORDEVENT_HEADATNEWPOS); 264 ExitOnError(result); 265 result = (*recordItf)->RegisterCallback(recordItf, RecCallback, NULL); 266 ExitOnError(result); 267 fprintf(stdout, "Recorder callback registered\n"); 268 269 /* Get the buffer queue interface which was explicitly requested */ 270 result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 271 (void*)&recBuffQueueItf); 272 ExitOnError(result); 273 274 /* ------------------------------------------------------ */ 275 /* Initialize the callback and its context for the recording buffer queue */ 276 CallbackCntxt cntxt; 277 cntxt.pDataBase = (int8_t*)&pcmData; 278 cntxt.pData = cntxt.pDataBase; 279 cntxt.size = sizeof(pcmData); 280 result = (*recBuffQueueItf)->RegisterCallback(recBuffQueueItf, RecBufferQueueCallback, &cntxt); 281 ExitOnError(result); 282 283 /* Enqueue buffers to map the region of memory allocated to store the recorded data */ 284 fprintf(stdout,"Enqueueing buffer "); 285 for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) { 286 fprintf(stdout,"%d ", i); 287 result = (*recBuffQueueItf)->Enqueue(recBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES); 288 ExitOnError(result); 289 cntxt.pData += BUFFER_SIZE_IN_BYTES; 290 } 291 fprintf(stdout,"\n"); 292 cntxt.pData = cntxt.pDataBase; 293 294 /* ------------------------------------------------------ */ 295 /* Start recording */ 296 result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 297 ExitOnError(result); 298 fprintf(stdout, "Starting to record\n"); 299 300 /* Record for at least a second */ 301 if (durationInSeconds < 1) { 302 durationInSeconds = 1; 303 } 304 usleep(durationInSeconds * 1000 * 1000); 305 306 /* ------------------------------------------------------ */ 307 /* End of recording */ 308 309 /* Stop recording */ 310 result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 311 ExitOnError(result); 312 fprintf(stdout, "Stopped recording\n"); 313 314 /* Destroy the AudioRecorder object */ 315 (*recorder)->Destroy(recorder); 316 317 fclose(gFp); 318} 319 320//----------------------------------------------------------------- 321int main(int argc, char* const argv[]) 322{ 323 SLresult result; 324 SLObjectItf sl; 325 326 const char *prog = argv[0]; 327 fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLAndroidSimpleBufferQueueItf ", 328 prog); 329 fprintf(stdout, "on an AudioRecorder object\n"); 330 331 int i; 332 for (i = 1; i < argc; ++i) { 333 const char *arg = argv[i]; 334 if (arg[0] != '-') { 335 break; 336 } 337 switch (arg[1]) { 338 case 'p': // preset number 339 presetValue = atoi(&arg[2]); 340 break; 341 default: 342 fprintf(stderr, "%s: unknown option %s\n", prog, arg); 343 break; 344 } 345 } 346 347 if (argc-i < 2) { 348 fprintf(stdout, "Usage: \t%s [-p#] destination_file duration_in_seconds\n", prog); 349 fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", prog); 350 exit(EXIT_FAILURE); 351 } 352 353 SLEngineOption EngineOption[] = { 354 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 355 }; 356 357 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 358 ExitOnError(result); 359 360 /* Realizing the SL Engine in synchronous mode. */ 361 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 362 ExitOnError(result); 363 364 TestRecToBuffQueue(sl, argv[i], (SLAint64)atoi(argv[i+1])); 365 366 /* Shutdown OpenSL ES */ 367 (*sl)->Destroy(sl); 368 369 return EXIT_SUCCESS; 370} 371