slesTestRecBuffQueue.cpp revision 2246c698482ab6860906672229f0ae6d886e6302
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 "OpenSLES.h" 46#include "OpenSLES_Android.h" 47#include "OpenSLES_AndroidConfiguration.h" 48 49/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_ANDROIDCONFIGURATION 50 * on the AudioRecorder object */ 51#define NUM_EXPLICIT_INTERFACES_FOR_RECORDER 2 52 53/* Size of the recording buffer queue */ 54#define NB_BUFFERS_IN_QUEUE 1 55/* Size of each buffer in the queue */ 56#define BUFFER_SIZE_IN_SAMPLES 1024 57#define BUFFER_SIZE_IN_BYTES (2*BUFFER_SIZE_IN_SAMPLES) 58 59/* Local storage for Audio data */ 60int8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES]; 61 62/* destination for recorded data */ 63static FILE* gFp; 64 65//----------------------------------------------------------------- 66/* Exits the application if an error is encountered */ 67#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 68 69void ExitOnErrorFunc( SLresult result , int line) 70{ 71 if (SL_RESULT_SUCCESS != result) { 72 fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line); 73 exit(EXIT_FAILURE); 74 } 75} 76 77//----------------------------------------------------------------- 78/* Structure for passing information to callback function */ 79typedef struct CallbackCntxt_ { 80 SLPlayItf playItf; 81 SLuint32 size; 82 SLint8* pDataBase; // Base address of local audio data storage 83 SLint8* pData; // Current address of local audio data storage 84} CallbackCntxt; 85 86 87//----------------------------------------------------------------- 88/* Callback for recording buffer queue events */ 89void RecCallback( 90 SLRecordItf caller, 91 void *pContext, 92 SLuint32 event) 93{ 94 if (SL_RECORDEVENT_HEADATNEWPOS & event) { 95 SLmillisecond pMsec = 0; 96 (*caller)->GetPosition(caller, &pMsec); 97 fprintf(stdout, "SL_RECORDEVENT_HEADATNEWPOS current position=%lums\n", pMsec); 98 } 99 100 if (SL_RECORDEVENT_HEADATMARKER & event) { 101 SLmillisecond pMsec = 0; 102 (*caller)->GetPosition(caller, &pMsec); 103 fprintf(stdout, "SL_RECORDEVENT_HEADATMARKER current position=%lums\n", pMsec); 104 } 105} 106 107//----------------------------------------------------------------- 108/* Callback for recording buffer queue events */ 109void RecBufferQueueCallback( 110 SLAndroidSimpleBufferQueueItf queueItf, 111 void *pContext) 112{ 113 //fprintf(stdout, "RecBufferQueueCallback called\n"); 114 115 CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 116 117 /* Save the recorded data */ 118 fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp); 119 120 /* Increase data pointer by buffer size */ 121 pCntxt->pData += BUFFER_SIZE_IN_BYTES; 122 123 if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) { 124 pCntxt->pData = pCntxt->pDataBase; 125 } 126 127 ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) ); 128 129 SLAndroidSimpleBufferQueueState recQueueState; 130 ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) ); 131 132 /*fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: " 133 "count=%lu playIndex=%lu\n", 134 pCntxt->pData, recQueueState.count, recQueueState.index);*/ 135} 136 137//----------------------------------------------------------------- 138 139/* Play an audio path by opening a file descriptor on that path */ 140void TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds) 141{ 142 gFp = fopen(path, "w"); 143 if (NULL == gFp) { 144 ExitOnError(SL_RESULT_RESOURCE_ERROR); 145 } 146 147 SLresult result; 148 SLEngineItf EngineItf; 149 150 /* Objects this application uses: one audio recorder */ 151 SLObjectItf recorder; 152 153 /* Interfaces for the audio recorder */ 154 SLAndroidSimpleBufferQueueItf recBuffQueueItf; 155 SLRecordItf recordItf; 156 SLAndroidConfigurationItf configItf; 157 158 /* Source of audio data for the recording */ 159 SLDataSource recSource; 160 SLDataLocator_IODevice ioDevice; 161 162 /* Data sink for recorded audio */ 163 SLDataSink recDest; 164 SLDataLocator_AndroidSimpleBufferQueue recBuffQueue; 165 SLDataFormat_PCM pcm; 166 167 SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 168 SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 169 170 /* Get the SL Engine Interface which is implicit */ 171 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 172 ExitOnError(result); 173 174 /* Initialize arrays required[] and iidArray[] */ 175 for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) { 176 required[i] = SL_BOOLEAN_FALSE; 177 iidArray[i] = SL_IID_NULL; 178 } 179 180 181 /* ------------------------------------------------------ */ 182 /* Configuration of the recorder */ 183 184 /* Request the AndroidSimpleBufferQueue and AndroidConfiguration interfaces */ 185 required[0] = SL_BOOLEAN_TRUE; 186 iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; 187 required[1] = SL_BOOLEAN_TRUE; 188 iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 189 190 /* Setup the data source */ 191 ioDevice.locatorType = SL_DATALOCATOR_IODEVICE; 192 ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT; 193 ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; 194 ioDevice.device = NULL; 195 recSource.pLocator = (void *) &ioDevice; 196 recSource.pFormat = NULL; 197 198 /* Setup the data sink */ 199 recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; 200 recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE; 201 /* set up the format of the data in the buffer queue */ 202 pcm.formatType = SL_DATAFORMAT_PCM; 203 pcm.numChannels = 1; 204 pcm.samplesPerSec = SL_SAMPLINGRATE_22_05; 205 pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; 206 pcm.containerSize = 16; 207 pcm.channelMask = SL_SPEAKER_FRONT_LEFT; 208 pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; 209 210 recDest.pLocator = (void *) &recBuffQueue; 211 recDest.pFormat = (void * ) &pcm; 212 213 /* Create the audio recorder */ 214 result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest, 215 NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required); 216 ExitOnError(result); 217 fprintf(stdout, "Recorder created\n"); 218 219 /* Get the Android configuration interface which is explicit */ 220 result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 221 ExitOnError(result); 222 223 /* Use the configuration interface to configure the recorder before it's realized */ 224 SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_CAMCORDER; 225 result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 226 &presetValue, sizeof(SLuint32)); 227 ExitOnError(result); 228 fprintf(stdout, "Recorder parametrized\n"); 229 230 presetValue = SL_ANDROID_RECORDING_PRESET_NONE; 231 SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big 232 result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 233 &presetSize, (void*)&presetValue); 234 ExitOnError(result); 235 if (presetValue != SL_ANDROID_RECORDING_PRESET_CAMCORDER) { 236 fprintf(stderr, "Error retrieved recording preset\n"); 237 ExitOnError(SL_RESULT_INTERNAL_ERROR); 238 } 239 240 /* Realize the recorder in synchronous mode. */ 241 result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE); 242 ExitOnError(result); 243 fprintf(stdout, "Recorder realized\n"); 244 245 /* Get the record interface which is implicit */ 246 result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf); 247 ExitOnError(result); 248 249 /* Set up the recorder callback to get events during the recording */ 250 result = (*recordItf)->SetMarkerPosition(recordItf, 2000); 251 ExitOnError(result); 252 result = (*recordItf)->SetPositionUpdatePeriod(recordItf, 500); 253 ExitOnError(result); 254 result = (*recordItf)->SetCallbackEventsMask(recordItf, 255 SL_RECORDEVENT_HEADATMARKER | SL_RECORDEVENT_HEADATNEWPOS); 256 ExitOnError(result); 257 result = (*recordItf)->RegisterCallback(recordItf, RecCallback, NULL); 258 ExitOnError(result); 259 fprintf(stdout, "Recorder callback registered\n"); 260 261 /* Get the buffer queue interface which was explicitly requested */ 262 result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 263 (void*)&recBuffQueueItf); 264 ExitOnError(result); 265 266 /* ------------------------------------------------------ */ 267 /* Initialize the callback and its context for the recording buffer queue */ 268 CallbackCntxt cntxt; 269 cntxt.pDataBase = (int8_t*)&pcmData; 270 cntxt.pData = cntxt.pDataBase; 271 cntxt.size = sizeof(pcmData); 272 result = (*recBuffQueueItf)->RegisterCallback(recBuffQueueItf, RecBufferQueueCallback, &cntxt); 273 ExitOnError(result); 274 275 /* Enqueue buffers to map the region of memory allocated to store the recorded data */ 276 fprintf(stdout,"Enqueueing buffer "); 277 for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) { 278 fprintf(stdout,"%d ", i); 279 result = (*recBuffQueueItf)->Enqueue(recBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES); 280 ExitOnError(result); 281 cntxt.pData += BUFFER_SIZE_IN_BYTES; 282 } 283 fprintf(stdout,"\n"); 284 cntxt.pData = cntxt.pDataBase; 285 286 /* ------------------------------------------------------ */ 287 /* Start recording */ 288 result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 289 ExitOnError(result); 290 fprintf(stdout, "Starting to record\n"); 291 292 /* Record for at least a second */ 293 if (durationInSeconds < 1) { 294 durationInSeconds = 1; 295 } 296 usleep(durationInSeconds * 1000 * 1000); 297 298 /* ------------------------------------------------------ */ 299 /* End of recording */ 300 301 /* Stop recording */ 302 result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 303 ExitOnError(result); 304 fprintf(stdout, "Stopped recording\n"); 305 306 /* Destroy the AudioRecorder object */ 307 (*recorder)->Destroy(recorder); 308 309 fclose(gFp); 310} 311 312//----------------------------------------------------------------- 313int main(int argc, char* const argv[]) 314{ 315 SLresult result; 316 SLObjectItf sl; 317 318 fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLAndroidSimpleBufferQueueItf ", 319 argv[0]); 320 fprintf(stdout, "on an AudioRecorder object\n"); 321 322 if (argc < 2) { 323 fprintf(stdout, "Usage: \t%s destination_file duration_in_seconds\n", argv[0]); 324 fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", argv[0]); 325 exit(EXIT_FAILURE); 326 } 327 328 SLEngineOption EngineOption[] = { 329 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 330 }; 331 332 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 333 ExitOnError(result); 334 335 /* Realizing the SL Engine in synchronous mode. */ 336 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 337 ExitOnError(result); 338 339 TestRecToBuffQueue(sl, argv[1], (SLAint64)atoi(argv[2])); 340 341 /* Shutdown OpenSL ES */ 342 (*sl)->Destroy(sl); 343 344 return EXIT_SUCCESS; 345} 346