slesTestRecBuffQueue.cpp revision c6853892c94800e72c0bd676d5d2136d48cea76e
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 4901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_ANDROIDCONFIGURATION 50b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi * on the AudioRecorder object */ 51b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define NUM_EXPLICIT_INTERFACES_FOR_RECORDER 2 52ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 53ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of the recording buffer queue */ 54ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define NB_BUFFERS_IN_QUEUE 1 55ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of each buffer in the queue */ 56ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define BUFFER_SIZE_IN_SAMPLES 1024 57a384948fc96e81947a9b689fc65ea0e7b93df25cJean-Michel Trivi#define BUFFER_SIZE_IN_BYTES (2*BUFFER_SIZE_IN_SAMPLES) 58ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 59ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Local storage for Audio data */ 60ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES]; 61ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 62ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* destination for recorded data */ 63ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivistatic FILE* gFp; 64ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 65ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 66ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Exits the application if an error is encountered */ 67ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 68ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 69ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line) 70ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 71ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (SL_RESULT_SUCCESS != result) { 7258432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten fprintf(stdout, "%u error code encountered at line %d, exiting\n", result, line); 73c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten exit(EXIT_FAILURE); 74ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 75ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 76ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 77ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 78ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Structure for passing information to callback function */ 79ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivitypedef struct CallbackCntxt_ { 80ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLPlayItf playItf; 81ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLuint32 size; 82ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLint8* pDataBase; // Base address of local audio data storage 83ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLint8* pData; // Current address of local audio data storage 84ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} CallbackCntxt; 85ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 869bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 879bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi//----------------------------------------------------------------- 889bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi/* Callback for recording buffer queue events */ 899bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivivoid RecCallback( 909bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLRecordItf caller, 919bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi void *pContext, 929bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLuint32 event) 939bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi{ 949bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi if (SL_RECORDEVENT_HEADATNEWPOS & event) { 959bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLmillisecond pMsec = 0; 969bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi (*caller)->GetPosition(caller, &pMsec); 9758432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten fprintf(stdout, "SL_RECORDEVENT_HEADATNEWPOS current position=%ums\n", pMsec); 989bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi } 999bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 1009bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi if (SL_RECORDEVENT_HEADATMARKER & event) { 1019bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SLmillisecond pMsec = 0; 1029bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi (*caller)->GetPosition(caller, &pMsec); 10358432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten fprintf(stdout, "SL_RECORDEVENT_HEADATMARKER current position=%ums\n", pMsec); 1049bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi } 1059bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi} 1069bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 107ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 108ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Callback for recording buffer queue events */ 109ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid RecBufferQueueCallback( 11001e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueItf queueItf, 111ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi void *pContext) 112ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 11304c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten //fprintf(stdout, "RecBufferQueueCallback called\n"); 114ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 115ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; 116ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 117ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Save the recorded data */ 118ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp); 119ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 120ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Increase data pointer by buffer size */ 121ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData += BUFFER_SIZE_IN_BYTES; 122ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 123ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) { 124ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pCntxt->pData = pCntxt->pDataBase; 125ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 126ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 127ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) ); 128ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 12901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueState recQueueState; 130ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) ); 131ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 13204c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten /*fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: " 13358432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten "count=%u playIndex=%u\n", 13404c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten pCntxt->pData, recQueueState.count, recQueueState.index);*/ 135ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 136ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 137ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 138ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 139ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path */ 140ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds) 141ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 142ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi gFp = fopen(path, "w"); 143ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (NULL == gFp) { 144ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(SL_RESULT_RESOURCE_ERROR); 145ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 146ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 147ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 148ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineItf EngineItf; 149ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 150ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Objects this application uses: one audio recorder */ 151ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf recorder; 152ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 153ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Interfaces for the audio recorder */ 15401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLAndroidSimpleBufferQueueItf recBuffQueueItf; 155b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLRecordItf recordItf; 156b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLAndroidConfigurationItf configItf; 157ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 158ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Source of audio data for the recording */ 159ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSource recSource; 160ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataLocator_IODevice ioDevice; 161ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 162ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Data sink for recorded audio */ 163ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataSink recDest; 16401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten SLDataLocator_AndroidSimpleBufferQueue recBuffQueue; 165ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLDataFormat_PCM pcm; 166ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 167ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 168ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER]; 169ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 170ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the SL Engine Interface which is implicit */ 171ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 172ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 173ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 174ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize arrays required[] and iidArray[] */ 175ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) { 176ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[i] = SL_BOOLEAN_FALSE; 177ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi iidArray[i] = SL_IID_NULL; 178ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 179ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 180ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 181ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 182ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Configuration of the recorder */ 183ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 18401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten /* Request the AndroidSimpleBufferQueue and AndroidConfiguration interfaces */ 185ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi required[0] = SL_BOOLEAN_TRUE; 18601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; 187b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi required[1] = SL_BOOLEAN_TRUE; 188b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 189ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 190ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data source */ 191ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.locatorType = SL_DATALOCATOR_IODEVICE; 192ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT; 193ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; 194ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ioDevice.device = NULL; 195ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recSource.pLocator = (void *) &ioDevice; 1962246c698482ab6860906672229f0ae6d886e6302Glenn Kasten recSource.pFormat = NULL; 197ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 198ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Setup the data sink */ 19901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; 200ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE; 201ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* set up the format of the data in the buffer queue */ 202ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.formatType = SL_DATAFORMAT_PCM; 203ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.numChannels = 1; 204ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.samplesPerSec = SL_SAMPLINGRATE_22_05; 205ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; 206ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.containerSize = 16; 207ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.channelMask = SL_SPEAKER_FRONT_LEFT; 208ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; 209ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 210ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pLocator = (void *) &recBuffQueue; 211ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi recDest.pFormat = (void * ) &pcm; 212ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 213ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Create the audio recorder */ 214b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest, 215b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required); 216ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 217ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder created\n"); 218ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 219b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Get the Android configuration interface which is explicit */ 220b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 221b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 222b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 223b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi /* Use the configuration interface to configure the recorder before it's realized */ 224b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_CAMCORDER; 225b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 226b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi &presetValue, sizeof(SLuint32)); 227b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 228b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi fprintf(stdout, "Recorder parametrized\n"); 229b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 230b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi presetValue = SL_ANDROID_RECORDING_PRESET_NONE; 231b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big 232b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET, 233b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi &presetSize, (void*)&presetValue); 234b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(result); 235b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi if (presetValue != SL_ANDROID_RECORDING_PRESET_CAMCORDER) { 236b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi fprintf(stderr, "Error retrieved recording preset\n"); 237b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi ExitOnError(SL_RESULT_INTERNAL_ERROR); 238b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi } 239b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi 240ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realize the recorder in synchronous mode. */ 241ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE); 242ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 243ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Recorder realized\n"); 244ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 245ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the record interface which is implicit */ 246ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf); 247ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 248ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 2499bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi /* Set up the recorder callback to get events during the recording */ 2509bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetMarkerPosition(recordItf, 2000); 2519bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2529bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetPositionUpdatePeriod(recordItf, 500); 2539bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2549bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->SetCallbackEventsMask(recordItf, 2559bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi SL_RECORDEVENT_HEADATMARKER | SL_RECORDEVENT_HEADATNEWPOS); 2569bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2579bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi result = (*recordItf)->RegisterCallback(recordItf, RecCallback, NULL); 2589bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi ExitOnError(result); 2599bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi fprintf(stdout, "Recorder callback registered\n"); 2609bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi 261ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Get the buffer queue interface which was explicitly requested */ 2625e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 2635e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten (void*)&recBuffQueueItf); 264ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 265ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 266ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 267ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Initialize the callback and its context for the recording buffer queue */ 268ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi CallbackCntxt cntxt; 269ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pDataBase = (int8_t*)&pcmData; 270ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 271ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.size = sizeof(pcmData); 272ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->RegisterCallback(recBuffQueueItf, RecBufferQueueCallback, &cntxt); 273ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 274ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 275ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Enqueue buffers to map the region of memory allocated to store the recorded data */ 276ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"Enqueueing buffer "); 277ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi for(int i = 0 ; i < NB_BUFFERS_IN_QUEUE ; i++) { 278ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"%d ", i); 279ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recBuffQueueItf)->Enqueue(recBuffQueueItf, cntxt.pData, BUFFER_SIZE_IN_BYTES); 280ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 281ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData += BUFFER_SIZE_IN_BYTES; 282ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 283ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout,"\n"); 284ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi cntxt.pData = cntxt.pDataBase; 285ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 286ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 287ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Start recording */ 288ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); 289ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 290ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Starting to record\n"); 291ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 292ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Record for at least a second */ 293ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (durationInSeconds < 1) { 294ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi durationInSeconds = 1; 295ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 296ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi usleep(durationInSeconds * 1000 * 1000); 297ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 298ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* ------------------------------------------------------ */ 299ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* End of recording */ 300ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 301ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Stop recording */ 302ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); 303ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 304ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Stopped recording\n"); 305ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 306ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Destroy the AudioRecorder object */ 307ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*recorder)->Destroy(recorder); 308ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 309ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fclose(gFp); 310ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 311ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 312ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//----------------------------------------------------------------- 313ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint main(int argc, char* const argv[]) 314ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{ 315ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLresult result; 316ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLObjectItf sl; 317ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 3185e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLAndroidSimpleBufferQueueItf ", 3195e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten argv[0]); 320ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "on an AudioRecorder object\n"); 321ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 322ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi if (argc < 2) { 323ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Usage: \t%s destination_file duration_in_seconds\n", argv[0]); 324ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", argv[0]); 325c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten exit(EXIT_FAILURE); 326ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi } 327ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 328ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi SLEngineOption EngineOption[] = { 329ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 330ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi }; 331ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 332ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 333ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 334ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 335ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Realizing the SL Engine in synchronous mode. */ 336ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 337ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi ExitOnError(result); 338ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 339ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi TestRecToBuffQueue(sl, argv[1], (SLAint64)atoi(argv[2])); 340ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 341ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi /* Shutdown OpenSL ES */ 342ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi (*sl)->Destroy(sl); 343ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi 344c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten return EXIT_SUCCESS; 345ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} 346