// open the android audio device for input OPENSL_STREAM *android_OpenRecDevice(char *name, int sr, int inchannels, int bufferframes, int bits_per_sample) { OPENSL_STREAM *p; p = (OPENSL_STREAM *) calloc(sizeof(OPENSL_STREAM),1); memset(p, 0, sizeof(OPENSL_STREAM)); p->inchannels = inchannels; p->sr = sr; p->queue = Queue_New(TRUE, -1, -1); p->buffersize = bufferframes; p->bits_per_sample = bits_per_sample; if ((p->bits_per_sample != 8) && (p->bits_per_sample != 16)) { android_CloseRecDevice(p); return NULL; } if(openSLCreateEngine(p) != SL_RESULT_SUCCESS) { android_CloseRecDevice(p); return NULL; } if(openSLRecOpen(p) != SL_RESULT_SUCCESS) { android_CloseRecDevice(p); return NULL; } return p; }
/* Start thread for capture audio data */ void * startThreadInAudio(void * arg){ LOGI("Enter: startThreadInAudio\n"); mainData_t* mainData = (mainData_t*)arg; SLresult res; SLObjectItf sl; /* Create OpenSL ES engine in thread-safe mode */ SLEngineOption EngineOption[] = {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}; res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); if(res != SL_RESULT_SUCCESS){ LOGE("Exit: startThreadInAudio: Can't create OpenSL engine\n"); return 0; } /* Realizing the SL Engine in synchronous mode. */ res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); if(res != SL_RESULT_SUCCESS){ LOGE("Exit: startThreadInAudio: Can't Realize OpenSL engine\n"); /* Shutdown OpenSL ES */ (*sl)->Destroy(sl); return 0; } openSLRecOpen(mainData,sl); /* Shutdown OpenSL ES */ (*sl)->Destroy(sl); LOGI("Exit: startThreadInAudio: OK\n"); return 0; }
// open the android audio device for input and/or output OPENSL_STREAM *android_OpenAudioDevice(int sr, int inchannels, int outchannels, int bufferframes) { OPENSL_STREAM *p; p = (OPENSL_STREAM *) calloc(sizeof(OPENSL_STREAM), 1); p->inchannels = inchannels; p->outchannels = outchannels; p->sr = sr; p->inlock = createThreadLock(); p->outlock = createThreadLock(); if ((p->outBufSamples = bufferframes * outchannels) != 0) { if ((p->outputBuffer[0] = (short *) calloc(p->outBufSamples, sizeof(short))) == NULL || (p->outputBuffer[1] = (short *) calloc(p->outBufSamples, sizeof(short))) == NULL) { android_CloseAudioDevice(p); return NULL; } } if ((p->inBufSamples = bufferframes * inchannels) != 0) { if ((p->inputBuffer[0] = (short *) calloc(p->inBufSamples, sizeof(short))) == NULL || (p->inputBuffer[1] = (short *) calloc(p->inBufSamples, sizeof(short))) == NULL) { android_CloseAudioDevice(p); return NULL; } } p->currentInputIndex = 0; p->currentOutputBuffer = 0; p->currentInputIndex = p->inBufSamples; p->currentInputBuffer = 0; if (openSLCreateEngine(p) != SL_RESULT_SUCCESS) { android_CloseAudioDevice(p); return NULL; } if (openSLRecOpen(p) != SL_RESULT_SUCCESS) { android_CloseAudioDevice(p); return NULL; } if (openSLPlayOpen(p) != SL_RESULT_SUCCESS) { android_CloseAudioDevice(p); return NULL; } notifyThreadLock(p->outlock); notifyThreadLock(p->inlock); p->time = 0.; return p; }