slesTestRecBuffQueue.cpp revision 01e9f5fa4698856f92bcfd88188ee4c8397b22db
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 2901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE 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 669bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 679bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi//----------------------------------------------------------------- 689bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi/* Callback for recording buffer queue events */ 699bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivivoid RecCallback( 709bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLRecordItf caller, 719bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi void *pContext, 729bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLuint32 event) 739bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi{ 749bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi if (SL_RECORDEVENT_HEADATNEWPOS & event) { 759bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLmillisecond pMsec = 0; 769bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi (*caller)->GetPosition(caller, &pMsec); 779bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi fprintf(stdout, "SL_RECORDEVENT_HEADATNEWPOS current position=%lums\n", pMsec); 789bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi } 799bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 809bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi if (SL_RECORDEVENT_HEADATMARKER & event) { 819bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLmillisecond pMsec = 0; 829bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi (*caller)->GetPosition(caller, &pMsec); 839bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi fprintf(stdout, "SL_RECORDEVENT_HEADATMARKER current position=%lums\n", pMsec); 849bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi } 859bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi} 869bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 87ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 88ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Callback for recording buffer queue events */ 89ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid RecBufferQueueCallback( 9001e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueItf queueItf, 91ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi void *pContext) 92ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 93ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "RecBufferQueueCallback called\n"); 94ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 95ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 96ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 97ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Save the recorded data */ 98ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp); 99ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 100ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Increase data pointer by buffer size */ 101ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData += BUFFER_SIZE_IN_BYTES; 102ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 103ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) { 104ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData = pCntxt->pDataBase; 105ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 106ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 107ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) ); 108ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 10901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueState recQueueState; 110ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) ); 111ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 112a384948fc96e81947a9b689fc65ea0e7b93df25cJean-Michel Trivi fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: " 113ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi "count=%lu playIndex=%lu\n", 11401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten pCntxt->pData, recQueueState.count, recQueueState.index); 115ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 116ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 117ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 118ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 119ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path */ 120ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds) 121ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 122ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi gFp = fopen(path, "w"); 123ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (NULL == gFp) { 124ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(SL_RESULT_RESOURCE_ERROR); 125ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 126ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 127ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 128ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineItf EngineItf; 129ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 130ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Objects this application uses: one audio recorder */ 131ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf recorder; 132ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 133ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Interfaces for the audio recorder */ 13401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueItf recBuffQueueItf; 135b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLRecordItf recordItf; 136b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLAndroidConfigurationItf configItf; 137ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 138ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Source of audio data for the recording */ 139ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSource recSource; 140ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataLocator_IODevice ioDevice; 141ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 142ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Data sink for recorded audio */ 143ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSink recDest; 14401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLDataLocator_AndroidSimpleBufferQueue recBuffQueue; 145ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataFormat_PCM pcm; 146ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 147ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 148ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 149ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 150ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the SL Engine Interface which is implicit */ 151ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 152ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 153ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 154ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize arrays required[] and iidArray[] */ 155ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) { 156ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[i] = SL_BOOLEAN_FALSE; 157ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi iidArray[i] = SL_IID_NULL; 158ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 159ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 160ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 161ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 162ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Configuration of the recorder */ 163ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 16401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten /* Request the AndroidSimpleBufferQueue and AndroidConfiguration interfaces */ 165ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[0] = SL_BOOLEAN_TRUE; 16601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; 167b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi required[1] = SL_BOOLEAN_TRUE; 168b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 169ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 170ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data source */ 171ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.locatorType = SL_DATALOCATOR_IODEVICE; 172ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT; 173ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; 174ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.device = NULL; 175ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recSource.pLocator = (void *) &ioDevice; 176ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 177ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data sink */ 17801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; 179ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE; 180ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* set up the format of the data in the buffer queue */ 181ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.formatType = SL_DATAFORMAT_PCM; 182ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.numChannels = 1; 183ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.samplesPerSec = SL_SAMPLINGRATE_22_05; 184ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; 185ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.containerSize = 16; 186ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.channelMask = SL_SPEAKER_FRONT_LEFT; 187ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; 188ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 189ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pLocator = (void *) &recBuffQueue; 190ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pFormat = (void * ) &pcm; 191ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 192ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Create the audio recorder */ 193b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest, 194b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required); 195ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 196ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder created\n"); 197ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 198b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Get the Android configuration interface which is explicit */ 199b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 200b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 201b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 202b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Use the configuration interface to configure the recorder before it's realized */ 203b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_CAMCORDER; 204b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 205b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi &presetValue, sizeof(SLuint32)); 206b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 207b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi fprintf(stdout, "Recorder parametrized\n"); 208b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 209b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi presetValue = SL_ANDROID_RECORDING_PRESET_NONE; 210b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big 211b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 212b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi &presetSize, (void*)&presetValue); 213b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 214b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi if (presetValue != SL_ANDROID_RECORDING_PRESET_CAMCORDER) { 215b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi fprintf(stderr, "Error retrieved recording preset\n"); 216b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(SL_RESULT_INTERNAL_ERROR); 217b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi } 218b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 219ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realize the recorder in synchronous mode. */ 220ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE); 221ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 222ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder realized\n"); 223ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 224ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the record interface which is implicit */ 225ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf); 226ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 227ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 2289bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi /* Set up the recorder callback to get events during the recording */ 2299bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetMarkerPosition(recordItf, 2000); 2309bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2319bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetPositionUpdatePeriod(recordItf, 500); 2329bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2339bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetCallbackEventsMask(recordItf, 2349bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SL_RECORDEVENT_HEADATMARKER | SL_RECORDEVENT_HEADATNEWPOS); 2359bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2369bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->RegisterCallback(recordItf, RecCallback, NULL); 2379bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2389bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi fprintf(stdout, "Recorder callback registered\n"); 2399bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 240ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the buffer queue interface which was explicitly requested */ 24101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void*)&recBuffQueueItf); 242ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 243ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 244ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 245ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize the callback and its context for the recording buffer queue */ 246ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt cntxt; 247ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pDataBase = (int8_t*)&pcmData; 248ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 249ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.size = sizeof(pcmData); 250ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->RegisterCallback(recBuffQueueItf, RecBufferQueueCallback, &cntxt); 251ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 252ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 253ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Enqueue buffers to map the region of memory allocated to store the recorded data */ 254ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"Enqueueing buffer "); 255ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) { 256ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"%d ", i); 257ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->Enqueue(recBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES); 258ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 259ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData += BUFFER_SIZE_IN_BYTES; 260ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 261ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"\n"); 262ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 263ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 264ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 265ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Start recording */ 266ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 267ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 268ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Starting to record\n"); 269ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 270ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Record for at least a second */ 271ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (durationInSeconds < 1) { 272ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi durationInSeconds = 1; 273ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 274ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi usleep(durationInSeconds * 1000 * 1000); 275ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 276ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 277ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* End of recording */ 278ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 279ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Stop recording */ 280ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 281ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 282ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Stopped recording\n"); 283ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 284ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Destroy the AudioRecorder object */ 285ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*recorder)->Destroy(recorder); 286ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 287ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fclose(gFp); 288ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 289ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 290ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 291ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint main(int argc, char* const argv[]) 292ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 293ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi //LOGV("Starting %s\n", argv[0]); 294ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 295ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 296ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf sl; 297ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 29801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLAndroidSimpleBufferQueueItf ", argv[0]); 299ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "on an AudioRecorder object\n"); 300ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 301ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (argc < 2) { 302ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Usage: \t%s destination_file duration_in_seconds\n", argv[0]); 303ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", argv[0]); 304ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi exit(1); 305ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 306ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 307ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineOption EngineOption[] = { 308ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 309ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi }; 310ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 311ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 312ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 313ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 314ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realizing the SL Engine in synchronous mode. */ 315ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 316ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 317ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 318ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi TestRecToBuffQueue(sl, argv[1], (SLAint64)atoi(argv[2])); 319ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 320ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Shutdown OpenSL ES */ 321ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*sl)->Destroy(sl); 322ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi exit(0); 323ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 324ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi return 0; 325ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 326