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