AudioRecorder_to_android.cpp revision 712b490060e4164fbe47986be1d2584d1610d8dd
13af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi/*
23af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project
33af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi *
43af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
53af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * you may not use this file except in compliance with the License.
63af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * You may obtain a copy of the License at
73af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi *
83af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
93af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi *
103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * See the License for the specific language governing permissions and
143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * limitations under the License.
153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi */
163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#include "sles_allinclusive.h"
193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi// use this flag to dump all recorded audio into a file
213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//#define MONITOR_RECORDING
223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#define MONITOR_TARGET "/sdcard/monitor.raw"
243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#include <stdio.h>
253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic FILE* gMonitorFp = NULL;
263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
28712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi#define ERROR_SINK_MUST_BE_BUFFERQUEUE \
29712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        "Cannot create AudioRecorder: data sink must be SL_DATALOCATOR_BUFFERQUEUE"
30712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi#define ERROR_SINK_FORMAT_MUST_BE_PCM \
31712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        "Cannot create AudioRecorder: data sink must be in PCM format"
32712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi#define ERROR_SOURCE_MUST_BE_IODEVICE \
33712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        "Cannot create AudioRecorder: data source must be SL_DATALOCATOR_IODEVICE"
34712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi#define ERROR_IODEVICE_MUST_BE_AUDIOINPUT \
35712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        "Cannot create AudioRecorder: data source device type must be SL_IODEVICE_AUDIOINPUT"
36712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi#define ERROR_INPUT_ID_MUST_BE_DEFAULT \
37712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        "Cannot create AudioRecorder: data source device ID must be SL_DEFAULTDEVICEID_AUDIOINPUT"
383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) {
413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSink   *pAudioSnk = &ar->mDataSink.u.mSink;
443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Sink check:
463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only buffer queue sinks are supported, regardless of the data source
473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) {
48712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SINK_MUST_BE_BUFFERQUEUE);
493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
50712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    } else {
51712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        // only PCM buffer queues are supported
52712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SLuint32 formatType = *(SLuint32 *)pAudioSnk->pFormat;
53712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        if (SL_DATAFORMAT_PCM == formatType) {
54712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)ar->mDataSink.u.mSink.pFormat;
55712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mSampleRateMilliHz = df_pcm->samplesPerSec;
56712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mNumChannels = df_pcm->numChannels;
57712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGV("AudioRecorder requested sample rate = %lumHz, %u channel(s)",
58712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi                    ar->mSampleRateMilliHz, ar->mNumChannels);
59712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
60712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        else {
61712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_SINK_FORMAT_MUST_BE_PCM);
62712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
63712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Source check:
673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only input device sources are supported
683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // check it's an IO device
693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) {
70712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SOURCE_MUST_BE_IODEVICE);
713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    } else {
733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's an input device
753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SLDataLocator_IODevice *dl_iod =  (SLDataLocator_IODevice *) pAudioSrc->pLocator;
763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) {
77712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_IODEVICE_MUST_BE_AUDIOINPUT);
783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's the default input device, others aren't supported here
823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) {
83712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_INPUT_ID_MUST_BE_DEFAULT);
843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return SL_RESULT_SUCCESS;
893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) {
923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info);
933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    CAudioRecorder *ar = (CAudioRecorder *)user;
953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    void * callbackPContext = NULL;
963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch(event) {
983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MORE_DATA: {
993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        slBufferQueueCallback callback = NULL;
1003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info;
1013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // push data to the buffer queue
1033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_lock_exclusive(&ar->mBufferQueue);
1043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (ar->mBufferQueue.mState.count != 0) {
1063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear);
1073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *oldFront = ar->mBufferQueue.mFront;
1093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *newFront = &oldFront[1];
1103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // FIXME handle 8bit based on buffer format
1123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed);
1133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
1143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // can't consume the whole or rest of the buffer in one shot
1153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed += pBuff->size;
1163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // leave pBuff->size untouched
1173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
1183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
1193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
1203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
1213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
1223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
1233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            } else {
1243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // finish pushing the buffer or push the buffer in one shot
1253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed;
1263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed = 0;
1273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (newFront ==  &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) {
1283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    newFront = ar->mBufferQueue.mArray;
1293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                }
1303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mFront = newFront;
1313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.count--;
1333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.playIndex++;
1343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
1353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
1363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
1373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
1383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
1393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
1403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // data has been copied to the buffer, and the buffer queue state has been updated
1413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // we will notify the client if applicable
1423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callback = ar->mBufferQueue.mCallback;
1433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // save callback data
1443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callbackPContext = ar->mBufferQueue.mContext;
1453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            }
1463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        } else {
1473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // no destination to push the data
1483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            pBuff->size = 0;
1493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_unlock_exclusive(&ar->mBufferQueue);
1523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // notify client
1533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (NULL != callback) {
1543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (*callback)(&ar->mBufferQueue.mItf, callbackPContext);
1553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
1583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MARKER:
1603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // FIXME implement
1613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("FIXME audioRecorder_callback(EVENT_MARKER, %p, %p) not supported", user, info);
1623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
1633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_NEW_POS:
1653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // FIXME implement
1663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("FIXME audioRecorder_callback(EVENT_NEW_POS, %p, %p) not supported", user, info);
1673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
1683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
1713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) {
1753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_create(%p) entering", ar);
1763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
1783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = NULL;
1803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
1823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
1833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) {
1873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_realize(%p) entering", ar);
1883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
1903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
191712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-independent CAudioRecorder fields
192712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != ar->mDataSink.mLocator.mLocatorType) {
193712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SINK_MUST_BE_BUFFERQUEUE);
194712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        return SL_RESULT_CONTENT_UNSUPPORTED;
195712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    }
196712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //  the following platform-independent field have been initialized in CreateAudioRecorder()
197712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mNumChannels
198712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mSampleRateMilliHz
199712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
200712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    SL_LOGV("new AudioRecord %u channels, %lu mHz\n", ar->mNumChannels, ar->mSampleRateMilliHz);
201712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
202712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-specific CAudioRecorder fields
2033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = new android::AudioRecord();
2043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord->set(android::AUDIO_SOURCE_DEFAULT, // source
205712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_sampleRate(ar->mSampleRateMilliHz), // sample rate in Hertz
2063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            android::AudioSystem::PCM_16_BIT,   //FIXME use format from buffer queue sink
207712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_channelMask(ar->mNumChannels, 0 /*no channel mask*/), // channel config
2083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     //frameCount min
2093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // flags
2103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            audioRecorder_callback,// callback_t
2113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (void*)ar,             // user, callback data, here the AudioRecorder
2123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // notificationFrames
2133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            false);                // threadCanCallJava, note: this will prevent direct Java
2143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                   //   callbacks, but we don't want them in the recording loop
2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (android::NO_ERROR != ar->mAudioRecord->initCheck()) {
2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar);
2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = fopen(MONITOR_TARGET, "w");
2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); }
2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    else { SL_LOGE("recording to %s", MONITOR_TARGET); } // LOGE so it's always displayed
2253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) {
2333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_destroy(%p) entering", ar);
2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != ar->mAudioRecord) {
2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord->stop();
2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        delete ar->mAudioRecord;
2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord = NULL;
2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != gMonitorFp) { fclose(gMonitorFp); }
2433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = NULL;
2443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) {
2503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_setRecordState(%p, %lu) entering", ar, state);
2513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
2533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return;
2543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch (state) {
2573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_STOPPED:
2583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
2593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
2603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_PAUSED:
2613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         // Note that pausing is treated like stop as this implementation only records to a buffer
2623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  queue, so there is no notion of destination being "opened" or "closed" (See description
2633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  of SL_RECORDSTATE in specification)
2643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
2653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
2663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_RECORDING:
2673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->start();
2683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
2693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     default:
2703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
2713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     }
2723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
274