slesTestRecBuffQueue.cpp revision c2303eb5497c488db786dcb2b8514db229452536
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 <stdlib.h>
18ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <stdio.h>
19ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <string.h>
20ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <unistd.h>
21ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <sys/time.h>
22ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include <fcntl.h>
23ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
24ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include "OpenSLES.h"
25ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#include "OpenSLES_Android.h"
26b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#include "OpenSLES_AndroidConfiguration.h"
27ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
2801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_ANDROIDCONFIGURATION
29b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi * on the AudioRecorder object */
30b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define NUM_EXPLICIT_INTERFACES_FOR_RECORDER 2
31ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
32ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of the recording buffer queue */
33ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define NB_BUFFERS_IN_QUEUE 1
34ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Size of each buffer in the queue */
35ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define BUFFER_SIZE_IN_SAMPLES 1024
36a384948fc96e81947a9b689fc65ea0e7b93df25cJean-Michel Trivi#define BUFFER_SIZE_IN_BYTES   (2*BUFFER_SIZE_IN_SAMPLES)
37ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
38ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Local storage for Audio data */
39ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Triviint8_t pcmData[NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES];
40ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
41ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* destination for recorded data */
42ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivistatic FILE* gFp;
43ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
44ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//-----------------------------------------------------------------
45ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Exits the application if an error is encountered */
46ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
47ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
48ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
49ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{
50ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
51ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line);
52c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
53ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    }
54ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi}
55ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
56ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//-----------------------------------------------------------------
57ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Structure for passing information to callback function */
58ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivitypedef struct CallbackCntxt_ {
59ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLPlayItf  playItf;
60ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLuint32   size;
61ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLint8*   pDataBase;    // Base address of local audio data storage
62ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLint8*   pData;        // Current address of local audio data storage
63ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi} CallbackCntxt;
64ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
659bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi
669bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi//-----------------------------------------------------------------
679bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi/* Callback for recording buffer queue events */
689bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivivoid RecCallback(
699bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        SLRecordItf caller,
709bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        void *pContext,
719bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        SLuint32 event)
729bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi{
739bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    if (SL_RECORDEVENT_HEADATNEWPOS & event) {
749bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        SLmillisecond pMsec = 0;
759bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        (*caller)->GetPosition(caller, &pMsec);
769bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        fprintf(stdout, "SL_RECORDEVENT_HEADATNEWPOS current position=%lums\n", pMsec);
779bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    }
789bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi
799bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    if (SL_RECORDEVENT_HEADATMARKER & event) {
809bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        SLmillisecond pMsec = 0;
819bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        (*caller)->GetPosition(caller, &pMsec);
829bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi        fprintf(stdout, "SL_RECORDEVENT_HEADATMARKER current position=%lums\n", pMsec);
839bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    }
849bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi}
859bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi
86ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//-----------------------------------------------------------------
87ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Callback for recording buffer queue events */
88ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid RecBufferQueueCallback(
8901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten        SLAndroidSimpleBufferQueueItf queueItf,
90ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        void *pContext)
91ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{
9204c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten    //fprintf(stdout, "RecBufferQueueCallback called\n");
93ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
94ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    CallbackCntxt *pCntxt = (CallbackCntxt*)pContext;
95ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
96ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Save the recorded data  */
97ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    fwrite(pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES, 1, gFp);
98ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
99ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Increase data pointer by buffer size */
100ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pCntxt->pData += BUFFER_SIZE_IN_BYTES;
101ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
102ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    if (pCntxt->pData >= pCntxt->pDataBase + (NB_BUFFERS_IN_QUEUE * BUFFER_SIZE_IN_BYTES)) {
103ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        pCntxt->pData = pCntxt->pDataBase;
104ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    }
105ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
106ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError( (*queueItf)->Enqueue(queueItf, pCntxt->pDataBase, BUFFER_SIZE_IN_BYTES) );
107ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
10801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    SLAndroidSimpleBufferQueueState recQueueState;
109ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError( (*queueItf)->GetState(queueItf, &recQueueState) );
110ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
11104c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten    /*fprintf(stderr, "\tRecBufferQueueCallback now has pCntxt->pData=%p queue: "
112ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi            "count=%lu playIndex=%lu\n",
11304c7354b8a9afcf2151c00c1dfbc64d0ba5d33ccGlenn Kasten            pCntxt->pData, recQueueState.count, recQueueState.index);*/
114ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi}
115ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
116ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi//-----------------------------------------------------------------
117ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
118ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi/* Play an audio path by opening a file descriptor on that path  */
119ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivivoid TestRecToBuffQueue( SLObjectItf sl, const char* path, SLAint64 durationInSeconds)
120ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi{
121ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    gFp = fopen(path, "w");
122ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    if (NULL == gFp) {
123ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
124ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    }
125ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
126ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLresult  result;
127ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLEngineItf EngineItf;
128ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
129ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Objects this application uses: one audio recorder */
130ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLObjectItf  recorder;
131ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
132ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Interfaces for the audio recorder */
13301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    SLAndroidSimpleBufferQueueItf recBuffQueueItf;
134b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLRecordItf               recordItf;
135b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLAndroidConfigurationItf configItf;
136ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
137ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Source of audio data for the recording */
138ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLDataSource           recSource;
139ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLDataLocator_IODevice ioDevice;
140ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
141ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Data sink for recorded audio */
142ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLDataSink                recDest;
14301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    SLDataLocator_AndroidSimpleBufferQueue recBuffQueue;
144ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLDataFormat_PCM          pcm;
145ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
146ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLboolean required[NUM_EXPLICIT_INTERFACES_FOR_RECORDER];
147ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLInterfaceID iidArray[NUM_EXPLICIT_INTERFACES_FOR_RECORDER];
148ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
149ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
150ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
151ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError(result);
152ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
153ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
154ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    for (int i=0 ; i < NUM_EXPLICIT_INTERFACES_FOR_RECORDER ; i++) {
155ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
156ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        iidArray[i] = SL_IID_NULL;
157ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    }
158ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
159ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
160ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* ------------------------------------------------------ */
161ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Configuration of the recorder  */
162ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
16301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    /* Request the AndroidSimpleBufferQueue and AndroidConfiguration interfaces */
164ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
16501e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
166b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
167b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    iidArray[1] = SL_IID_ANDROIDCONFIGURATION;
168ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
169ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Setup the data source */
170ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ioDevice.locatorType = SL_DATALOCATOR_IODEVICE;
171ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT;
172ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT;
173ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ioDevice.device = NULL;
174ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    recSource.pLocator = (void *) &ioDevice;
175ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
176ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Setup the data sink */
17701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
178ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    recBuffQueue.numBuffers = NB_BUFFERS_IN_QUEUE;
179ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /*    set up the format of the data in the buffer queue */
180ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.formatType = SL_DATAFORMAT_PCM;
181ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.numChannels = 1;
182ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.samplesPerSec = SL_SAMPLINGRATE_22_05;
183ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
184ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.containerSize = 16;
185ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.channelMask = SL_SPEAKER_FRONT_LEFT;
186ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
187ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
188ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    recDest.pLocator = (void *) &recBuffQueue;
189ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    recDest.pFormat = (void * ) &pcm;
190ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
191ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Create the audio recorder */
192b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    result = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recDest,
193b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            NUM_EXPLICIT_INTERFACES_FOR_RECORDER, iidArray, required);
194ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError(result);
195ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    fprintf(stdout, "Recorder created\n");
196ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
197b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    /* Get the Android configuration interface which is explicit */
198b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf);
199b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ExitOnError(result);
200b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
201b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    /* Use the configuration interface to configure the recorder before it's realized */
202b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_CAMCORDER;
203b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    result = (*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET,
204b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            &presetValue, sizeof(SLuint32));
205b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ExitOnError(result);
206b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    fprintf(stdout, "Recorder parametrized\n");
207b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
208b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    presetValue = SL_ANDROID_RECORDING_PRESET_NONE;
209b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big
210b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    result = (*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET,
211b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            &presetSize, (void*)&presetValue);
212b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ExitOnError(result);
213b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (presetValue != SL_ANDROID_RECORDING_PRESET_CAMCORDER) {
214b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        fprintf(stderr, "Error retrieved recording preset\n");
215b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        ExitOnError(SL_RESULT_INTERNAL_ERROR);
216b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
217b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
218ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Realize the recorder in synchronous mode. */
219ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    result = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE);
220ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError(result);
221ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    fprintf(stdout, "Recorder realized\n");
222ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
223ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Get the record interface which is implicit */
224ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    result = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void*)&recordItf);
225ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError(result);
226ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
2279bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    /* Set up the recorder callback to get events during the recording */
2289bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    result = (*recordItf)->SetMarkerPosition(recordItf, 2000);
2299bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    ExitOnError(result);
2309bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    result = (*recordItf)->SetPositionUpdatePeriod(recordItf, 500);
2319bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    ExitOnError(result);
2329bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    result = (*recordItf)->SetCallbackEventsMask(recordItf,
2339bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi            SL_RECORDEVENT_HEADATMARKER | SL_RECORDEVENT_HEADATNEWPOS);
2349bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    ExitOnError(result);
2359bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    result = (*recordItf)->RegisterCallback(recordItf, RecCallback, NULL);
2369bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    ExitOnError(result);
2379bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi    fprintf(stdout, "Recorder callback registered\n");
2389bc234ed758273259e334144cc6e1643b2494175Jean-Michel Trivi
239ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Get the buffer queue interface which was explicitly requested */
2405e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten    result = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
2415e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten            (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    SLresult    result;
294ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLObjectItf sl;
295ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
2965e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten    fprintf(stdout, "OpenSL ES test %s: exercises SLRecordItf and SLAndroidSimpleBufferQueueItf ",
2975e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten            argv[0]);
298ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    fprintf(stdout, "on an AudioRecorder object\n");
299ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
300ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    if (argc < 2) {
301ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        fprintf(stdout, "Usage: \t%s destination_file duration_in_seconds\n", argv[0]);
302ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi        fprintf(stdout, "Example: \"%s /sdcard/myrec.raw 4\" \n", argv[0]);
303c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten        exit(EXIT_FAILURE);
304ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    }
305ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
306ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    SLEngineOption EngineOption[] = {
307ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
308ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    };
309ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
310ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
311ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError(result);
312ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
313ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
314ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
315ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    ExitOnError(result);
316ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
317ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    TestRecToBuffQueue(sl, argv[1], (SLAint64)atoi(argv[2]));
318ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
319ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    /* Shutdown OpenSL ES */
320ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi    (*sl)->Destroy(sl);
321ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi
322c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten    return EXIT_SUCCESS;
323ece0014725c0f3b8ff6c369aeabd27a71f03510bJean-Michel Trivi}
324