slesTestRecBuffQueue.cpp revision b3e52a63baaea367cf411348b68ecd8fd429b029
1ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* 2ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project 3ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * 4ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); 5ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * you may not use this file except in compliance with the License. 6ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * You may obtain a copy of the License at 7ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * 8ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0 9ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * 10ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * Unless required by applicable law or agreed to in writing, software 11ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, 12ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * See the License for the specific language governing permissions and 14ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi * limitations under the License. 15ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi */ 16ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 17ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <getopt.h> 18ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <stdlib.h> 19ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <stdio.h> 20ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <string.h> 21ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <unistd.h> 22ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <sys/time.h> 23ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <fcntl.h> 24ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 25ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include "OpenSLES.h" 26ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include "OpenSLES_Android.h" 27b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#include "OpenSLES_AndroidConfiguration.h" 28ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 29b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi/* Explicitly requesting SL_IID_BUFFERQUEUE and SL_IID_ANDROIDCONFIGURATION 30b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi * on the AudioRecorder object */ 31b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define NUM_EXPLICIT_INTERFACES_FOR_RECORDER 2 32ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 33ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of the recording buffer queue */ 34ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define NB_BUFFERS_IN_QUEUE 1 35ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of each buffer in the queue */ 36ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define BUFFER_SIZE_IN_SAMPLES 1024 37a384948fc96e81947a9b689fc65ea0e7b93df25cJean-Michel Trivi#define BUFFER_SIZE_IN_BYTES (2*BUFFER_SIZE_IN_SAMPLES) 38ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 39ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Local storage for Audio data */ 40ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES]; 41ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 42ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* destination for recorded data */ 43ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivistatic FILE* gFp; 44ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 45ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 46ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Exits the application if an error is encountered */ 47ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 48ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 49ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line) 50ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 51ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (SL_RESULT_SUCCESS != result) { 52ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line); 53ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi exit(1); 54ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 55ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 56ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 57ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 58ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Structure for passing information to callback function */ 59ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivitypedef struct CallbackCntxt_ { 60ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLPlayItf playItf; 61ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLuint32 size; 62ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLint8* pDataBase; // Base address of local audio data storage 63ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLint8* pData; // Current address of local audio data storage 64ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} CallbackCntxt; 65ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 66ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 67ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Callback for recording buffer queue events */ 68ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid RecBufferQueueCallback( 69ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLBufferQueueItf queueItf, 70ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi void *pContext) 71ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 72ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "RecBufferQueueCallback called\n"); 73ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 74ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 75ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 76ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Save the recorded data */ 77ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp); 78ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 79ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Increase data pointer by buffer size */ 80ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData += BUFFER_SIZE_IN_BYTES; 81ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 82ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) { 83ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData = pCntxt->pDataBase; 84ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 85ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 86ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) ); 87ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 88ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLBufferQueueState recQueueState; 89ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) ); 90ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 91a384948fc96e81947a9b689fc65ea0e7b93df25cJean-Michel Trivi fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: " 92ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi "count=%lu playIndex=%lu\n", 93ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData, recQueueState.count, recQueueState.playIndex); 94ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 95ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 96ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 97ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 98ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path */ 99ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds) 100ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 101ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi gFp = fopen(path, "w"); 102ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (NULL == gFp) { 103ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(SL_RESULT_RESOURCE_ERROR); 104ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 105ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 106ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 107ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineItf EngineItf; 108ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 109ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Objects this application uses: one audio recorder */ 110ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf recorder; 111ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 112ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Interfaces for the audio recorder */ 113b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLBufferQueueItf recBuffQueueItf; 114b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLRecordItf recordItf; 115b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLAndroidConfigurationItf configItf; 116ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 117ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Source of audio data for the recording */ 118ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSource recSource; 119ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataLocator_IODevice ioDevice; 120ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 121ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Data sink for recorded audio */ 122ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSink recDest; 123ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataLocator_BufferQueue recBuffQueue; 124ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataFormat_PCM pcm; 125ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 126ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 127ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 128ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 129ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the SL Engine Interface which is implicit */ 130ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 131ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 132ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 133ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize arrays required[] and iidArray[] */ 134ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) { 135ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[i] = SL_BOOLEAN_FALSE; 136ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi iidArray[i] = SL_IID_NULL; 137ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 138ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 139ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 140ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 141ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Configuration of the recorder */ 142ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 143b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Request the BufferQueue and AndroidConfiguration interfaces */ 144ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[0] = SL_BOOLEAN_TRUE; 145ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi iidArray[0] = SL_IID_BUFFERQUEUE; 146b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi required[1] = SL_BOOLEAN_TRUE; 147b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 148ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 149ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data source */ 150ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.locatorType = SL_DATALOCATOR_IODEVICE; 151ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT; 152ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; 153ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.device = NULL; 154ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recSource.pLocator = (void *) &ioDevice; 155ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 156ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data sink */ 157ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recBuffQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE; 158ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE; 159ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* set up the format of the data in the buffer queue */ 160ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.formatType = SL_DATAFORMAT_PCM; 161ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.numChannels = 1; 162ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.samplesPerSec = SL_SAMPLINGRATE_22_05; 163ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; 164ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.containerSize = 16; 165ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.channelMask = SL_SPEAKER_FRONT_LEFT; 166ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; 167ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 168ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pLocator = (void *) &recBuffQueue; 169ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pFormat = (void * ) &pcm; 170ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 171ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Create the audio recorder */ 172b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest, 173b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required); 174ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 175ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder created\n"); 176ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 177b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Get the Android configuration interface which is explicit */ 178b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 179b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 180b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 181b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Use the configuration interface to configure the recorder before it's realized */ 182b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_CAMCORDER; 183b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 184b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi &presetValue, sizeof(SLuint32)); 185b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 186b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi fprintf(stdout, "Recorder parametrized\n"); 187b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 188b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi presetValue = SL_ANDROID_RECORDING_PRESET_NONE; 189b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big 190b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 191b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi &presetSize, (void*)&presetValue); 192b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 193b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi if (presetValue != SL_ANDROID_RECORDING_PRESET_CAMCORDER) { 194b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi fprintf(stderr, "Error retrieved recording preset\n"); 195b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(SL_RESULT_INTERNAL_ERROR); 196b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi } 197b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 198ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realize the recorder in synchronous mode. */ 199ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE); 200ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 201ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder realized\n"); 202ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 203ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the record interface which is implicit */ 204ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf); 205ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 206ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 207ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the buffer queue interface which was explicitly requested */ 208ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_BUFFERQUEUE, (void*)&recBuffQueueItf); 209ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 210ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 211ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 212ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize the callback and its context for the recording buffer queue */ 213ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt cntxt; 214ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pDataBase = (int8_t*)&pcmData; 215ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 216ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.size = sizeof(pcmData); 217ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->RegisterCallback(recBuffQueueItf, RecBufferQueueCallback, &cntxt); 218ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 219ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 220ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Enqueue buffers to map the region of memory allocated to store the recorded data */ 221ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"Enqueueing buffer "); 222ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) { 223ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"%d ", i); 224ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->Enqueue(recBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES); 225ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 226ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData += BUFFER_SIZE_IN_BYTES; 227ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 228ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"\n"); 229ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 230ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 231ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 232ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Start recording */ 233ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 234ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 235ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Starting to record\n"); 236ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 237ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Record for at least a second */ 238ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (durationInSeconds < 1) { 239ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi durationInSeconds = 1; 240ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 241ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi usleep(durationInSeconds * 1000 * 1000); 242ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 243ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 244ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* End of recording */ 245ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 246ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Stop recording */ 247ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 248ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 249ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Stopped recording\n"); 250ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 251ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Destroy the AudioRecorder object */ 252ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*recorder)->Destroy(recorder); 253ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 254ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fclose(gFp); 255ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 256ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 257ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 258ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint main(int argc, char* const argv[]) 259ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 260ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi //LOGV("Starting %s\n", argv[0]); 261ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 262ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 263ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf sl; 264ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 265ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLBufferQueueItf ", argv[0]); 266ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "on an AudioRecorder object\n"); 267ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 268ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (argc < 2) { 269ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Usage: \t%s destination_file duration_in_seconds\n", argv[0]); 270ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", argv[0]); 271ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi exit(1); 272ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 273ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 274ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineOption EngineOption[] = { 275ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 276ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi }; 277ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 278ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 279ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 280ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 281ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realizing the SL Engine in synchronous mode. */ 282ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 283ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 284ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 285ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi TestRecToBuffQueue(sl, argv[1], (SLAint64)atoi(argv[2])); 286ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 287ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Shutdown OpenSL ES */ 288ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*sl)->Destroy(sl); 289ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi exit(0); 290ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 291ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi return 0; 292ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 293