slesTestRecBuffQueue.cpp revision 4dd1fab74463de4852b86af64481006f87d48b54
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 177126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten/* Audio Record Test 187126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 197126c25d7c037e5086216cf540ecf40779c3585aGlenn KastenFirst run the program from shell: 207126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten # slesTest_recBuffQueue /sdcard/myrec.raw 4 217126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 227126c25d7c037e5086216cf540ecf40779c3585aGlenn KastenThese use adb on host to retrive the file: 237126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten % adb pull /sdcard/myrec.raw myrec.raw 247126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 257126c25d7c037e5086216cf540ecf40779c3585aGlenn KastenHow to examine the output with Audacity: 267126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Project / Import raw data 277126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Select myrec.raw file, then click Open button 287126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Choose these options: 297126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Signed 16-bit PCM 307126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Little-endian 317126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 1 Channel (Mono) 327126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Sample rate 22050 Hz 337126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten Click Import button 347126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 357126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten*/ 367126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 377126c25d7c037e5086216cf540ecf40779c3585aGlenn Kasten 38ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <stdlib.h> 39ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <stdio.h> 40ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <string.h> 41ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <unistd.h> 42ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <sys/time.h> 43ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <fcntl.h> 44ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 45c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES.h> 46c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES_Android.h> 47c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES_AndroidConfiguration.h> 48ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 494dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten/* Preset number to use for recording */ 504dd1fab74463de4852b86af64481006f87d48b54Glenn KastenSLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_NONE; 514dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten 5201e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_ANDROIDCONFIGURATION 53b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi * on the AudioRecorder object */ 54b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define NUM_EXPLICIT_INTERFACES_FOR_RECORDER 2 55ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 56ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of the recording buffer queue */ 57ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define NB_BUFFERS_IN_QUEUE 1 58ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of each buffer in the queue */ 59ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define BUFFER_SIZE_IN_SAMPLES 1024 60a384948fc96e81947a9b689fc65ea0e7b93df25cJean-Michel Trivi#define BUFFER_SIZE_IN_BYTES (2*BUFFER_SIZE_IN_SAMPLES) 61ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 62ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Local storage for Audio data */ 63ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES]; 64ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 65ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* destination for recorded data */ 66ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivistatic FILE* gFp; 67ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 68ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 69ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Exits the application if an error is encountered */ 70ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 71ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 72ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line) 73ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 74ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (SL_RESULT_SUCCESS != result) { 7558432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten fprintf(stdout, "%u error code encountered at line %d, exiting\n", result, line); 76c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten exit(EXIT_FAILURE); 77ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 78ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 79ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 80ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 81ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Structure for passing information to callback function */ 82ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivitypedef struct CallbackCntxt_ { 83ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLPlayItf playItf; 84ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLuint32 size; 85ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLint8* pDataBase; // Base address of local audio data storage 86ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLint8* pData; // Current address of local audio data storage 87ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} CallbackCntxt; 88ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 899bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 909bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi//----------------------------------------------------------------- 919bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi/* Callback for recording buffer queue events */ 929bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivivoid RecCallback( 939bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLRecordItf caller, 949bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi void *pContext, 959bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLuint32 event) 969bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi{ 979bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi if (SL_RECORDEVENT_HEADATNEWPOS & event) { 989bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLmillisecond pMsec = 0; 999bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi (*caller)->GetPosition(caller, &pMsec); 10058432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten fprintf(stdout, "SL_RECORDEVENT_HEADATNEWPOS current position=%ums\n", pMsec); 1019bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi } 1029bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 1039bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi if (SL_RECORDEVENT_HEADATMARKER & event) { 1049bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLmillisecond pMsec = 0; 1059bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi (*caller)->GetPosition(caller, &pMsec); 10658432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten fprintf(stdout, "SL_RECORDEVENT_HEADATMARKER current position=%ums\n", pMsec); 1079bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi } 1089bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi} 1099bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 110ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 111ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Callback for recording buffer queue events */ 112ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid RecBufferQueueCallback( 11301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueItf queueItf, 114ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi void *pContext) 115ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 11604c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten //fprintf(stdout, "RecBufferQueueCallback called\n"); 117ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 118ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 119ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 120ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Save the recorded data */ 121ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp); 122ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 123ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Increase data pointer by buffer size */ 124ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData += BUFFER_SIZE_IN_BYTES; 125ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 126ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) { 127ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData = pCntxt->pDataBase; 128ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 129ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 130ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) ); 131ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 13201e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueState recQueueState; 133ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) ); 134ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 13504c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten /*fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: " 13658432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten "count=%u playIndex=%u\n", 13704c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten pCntxt->pData, recQueueState.count, recQueueState.index);*/ 138ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 139ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 140ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 141ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 142ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path */ 143ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds) 144ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 145ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi gFp = fopen(path, "w"); 146ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (NULL == gFp) { 147ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(SL_RESULT_RESOURCE_ERROR); 148ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 149ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 150ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 151ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineItf EngineItf; 152ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 153ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Objects this application uses: one audio recorder */ 154ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf recorder; 155ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 156ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Interfaces for the audio recorder */ 15701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueItf recBuffQueueItf; 158b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLRecordItf recordItf; 159b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLAndroidConfigurationItf configItf; 160ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 161ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Source of audio data for the recording */ 162ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSource recSource; 163ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataLocator_IODevice ioDevice; 164ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 165ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Data sink for recorded audio */ 166ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSink recDest; 16701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLDataLocator_AndroidSimpleBufferQueue recBuffQueue; 168ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataFormat_PCM pcm; 169ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 170ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 171ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 172ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 173ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the SL Engine Interface which is implicit */ 174ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 175ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 176ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 177ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize arrays required[] and iidArray[] */ 178ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) { 179ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[i] = SL_BOOLEAN_FALSE; 180ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi iidArray[i] = SL_IID_NULL; 181ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 182ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 183ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 184ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 185ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Configuration of the recorder */ 186ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 18701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten /* Request the AndroidSimpleBufferQueue and AndroidConfiguration interfaces */ 188ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[0] = SL_BOOLEAN_TRUE; 18901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; 190b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi required[1] = SL_BOOLEAN_TRUE; 191b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 192ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 193ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data source */ 194ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.locatorType = SL_DATALOCATOR_IODEVICE; 195ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT; 196ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; 197ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.device = NULL; 198ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recSource.pLocator = (void *) &ioDevice; 1992246c698482ab6860906672229f0ae6d886e6302Glenn Kasten recSource.pFormat = NULL; 200ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 201ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data sink */ 20201e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; 203ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE; 204ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* set up the format of the data in the buffer queue */ 205ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.formatType = SL_DATAFORMAT_PCM; 206ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.numChannels = 1; 207ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.samplesPerSec = SL_SAMPLINGRATE_22_05; 208ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; 209ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.containerSize = 16; 210ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.channelMask = SL_SPEAKER_FRONT_LEFT; 211ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; 212ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 213ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pLocator = (void *) &recBuffQueue; 214ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pFormat = (void * ) &pcm; 215ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 216ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Create the audio recorder */ 217b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest, 218b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required); 219ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 220ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder created\n"); 221ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 222b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Get the Android configuration interface which is explicit */ 223b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 224b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 225b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 226b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Use the configuration interface to configure the recorder before it's realized */ 2274dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten if (presetValue != SL_ANDROID_RECORDING_PRESET_NONE) { 2284dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 2294dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten &presetValue, sizeof(SLuint32)); 2304dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten ExitOnError(result); 2314dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten fprintf(stdout, "Recorder parameterized with preset %u\n", presetValue); 2324dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten } else { 2334dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten printf("Using default record preset\n"); 2344dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten } 235b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 2364dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten SLuint32 presetRetrieved = SL_ANDROID_RECORDING_PRESET_NONE; 237b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big 238b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 2394dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten &presetSize, (void*)&presetRetrieved); 240b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 2414dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten if (presetValue == SL_ANDROID_RECORDING_PRESET_NONE) { 2424dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten printf("The default record preset appears to be %u\n", presetRetrieved); 2434dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten } else if (presetValue != presetRetrieved) { 2444dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten fprintf(stderr, "Error retrieving recording preset as %u instead of %u\n", presetRetrieved, presetValue); 245b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(SL_RESULT_INTERNAL_ERROR); 246b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi } 247b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 248ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realize the recorder in synchronous mode. */ 249ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE); 250ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 251ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder realized\n"); 252ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 253ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the record interface which is implicit */ 254ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf); 255ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 256ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 2579bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi /* Set up the recorder callback to get events during the recording */ 2589bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetMarkerPosition(recordItf, 2000); 2599bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2609bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetPositionUpdatePeriod(recordItf, 500); 2619bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2629bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetCallbackEventsMask(recordItf, 2639bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SL_RECORDEVENT_HEADATMARKER | SL_RECORDEVENT_HEADATNEWPOS); 2649bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2659bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->RegisterCallback(recordItf, RecCallback, NULL); 2669bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2679bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi fprintf(stdout, "Recorder callback registered\n"); 2689bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 269ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the buffer queue interface which was explicitly requested */ 2705e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 2715e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten (void*)&recBuffQueueItf); 272ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 273ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 274ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 275ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize the callback and its context for the recording buffer queue */ 276ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt cntxt; 277ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pDataBase = (int8_t*)&pcmData; 278ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 279ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.size = sizeof(pcmData); 280ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->RegisterCallback(recBuffQueueItf, RecBufferQueueCallback, &cntxt); 281ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 282ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 283ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Enqueue buffers to map the region of memory allocated to store the recorded data */ 284ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"Enqueueing buffer "); 285ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) { 286ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"%d ", i); 287ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->Enqueue(recBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES); 288ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 289ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData += BUFFER_SIZE_IN_BYTES; 290ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 291ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"\n"); 292ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 293ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 294ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 295ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Start recording */ 296ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 297ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 298ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Starting to record\n"); 299ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 300ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Record for at least a second */ 301ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (durationInSeconds < 1) { 302ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi durationInSeconds = 1; 303ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 304ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi usleep(durationInSeconds * 1000 * 1000); 305ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 306ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 307ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* End of recording */ 308ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 309ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Stop recording */ 310ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 311ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 312ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Stopped recording\n"); 313ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 314ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Destroy the AudioRecorder object */ 315ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*recorder)->Destroy(recorder); 316ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 317ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fclose(gFp); 318ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 319ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 320ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 321ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint main(int argc, char* const argv[]) 322ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 323ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 324ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf sl; 325ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 3264dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten const char *prog = argv[0]; 3275e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLAndroidSimpleBufferQueueItf ", 3284dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten prog); 329ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "on an AudioRecorder object\n"); 330ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 3314dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten int i; 3324dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten for (i = 1; i < argc; ++i) { 3334dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten const char *arg = argv[i]; 3344dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten if (arg[0] != '-') { 3354dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten break; 3364dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten } 3374dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten switch (arg[1]) { 3384dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten case 'p': // preset number 3394dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten presetValue = atoi(&arg[2]); 3404dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten break; 3414dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten default: 3424dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten fprintf(stderr, "%s: unknown option %s\n", prog, arg); 3434dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten break; 3444dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten } 3454dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten } 3464dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten 3474dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten if (argc-i < 2) { 3484dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten fprintf(stdout, "Usage: \t%s [-p#] destination_file duration_in_seconds\n", prog); 3494dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", prog); 350c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten exit(EXIT_FAILURE); 351ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 352ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 353ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineOption EngineOption[] = { 354ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 355ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi }; 356ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 357ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 358ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 359ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 360ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realizing the SL Engine in synchronous mode. */ 361ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 362ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 363ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 3644dd1fab74463de4852b86af64481006f87d48b54Glenn Kasten TestRecToBuffQueue(sl, argv[i], (SLAint64)atoi(argv[i+1])); 365ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 366ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Shutdown OpenSL ES */ 367ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*sl)->Destroy(sl); 368ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 369c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten return EXIT_SUCCESS; 370ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 371