AudioRecorder_to_android.cpp revision b3e52a63baaea367cf411348b68ecd8fd429b029
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"
38b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_UNKNOWN_KEY \
39b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Cannot set recording configuration: unknown key"
40b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_SET_UNKNOWN_PRESET \
41b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Cannot set recording preset: unknown preset"
42b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_VALUESIZE_TOO_LOW \
43b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Configuration error: value size too low to store valid value"
44b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_INVALID_SOURCE_FOR_VOICE_RECO \
45b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Cannot set recording preset to voice recognition: incompatible recording source"
46b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_AEC_UNSUPPORTED \
47b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Cannot change AEC setting: unsupported feature on this platform"
48b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_AGC_UNSUPPORTED \
49b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Cannot change AGC setting: unsupported feature on this platform"
50b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define ERROR_NULL_PARAM \
51b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        "Configuration error: invalid NULL parameter"
52b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
53b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_SOURCE_PARAMSIZE  sizeof(SLuint32)
54b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_PRESET_PARAMSIZE  sizeof(SLuint32)
55b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
56b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
57b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi// Internal utility functions
58b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//----------------------------
59b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
60b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_setPreset(CAudioRecorder* ar, SLuint32 recordPreset) {
61b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
62b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
63b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (recordPreset) {
64b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_DEFAULT:
65b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        ar->mRecordSource = android::AUDIO_SOURCE_DEFAULT;
66b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
67b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_CAMCORDER:
68b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        ar->mRecordSource = android::AUDIO_SOURCE_CAMCORDER;
69b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
70b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION:
71b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        ar->mRecordSource = android::AUDIO_SOURCE_VOICE_RECOGNITION;
72b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
73b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_NONE:
74b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // it is an error to set preset "none"
75b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
76b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_SET_UNKNOWN_PRESET);
77b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
78b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
79b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
80b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
81b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
82b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
83b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
84b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_getPreset(CAudioRecorder* ar, SLuint32* pPreset) {
85b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
86b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
87b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (ar->mRecordSource) {
88b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_DEFAULT:
89b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_MIC:
90b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_DEFAULT;
91b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
92b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_UPLINK:
93b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_DOWNLINK:
94b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_CALL:
95b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
96b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
97b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_RECOGNITION:
98b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
99b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
100b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_CAMCORDER:
101b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_CAMCORDER;
102b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
103b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
104b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
105b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
106b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
107b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
108b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
109b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
110b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
111b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
1123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) {
1153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
1173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSink   *pAudioSnk = &ar->mDataSink.u.mSink;
1183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Sink check:
1203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only buffer queue sinks are supported, regardless of the data source
1213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) {
122712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SINK_MUST_BE_BUFFERQUEUE);
1233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
124712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    } else {
125712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        // only PCM buffer queues are supported
126712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SLuint32 formatType = *(SLuint32 *)pAudioSnk->pFormat;
127712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        if (SL_DATAFORMAT_PCM == formatType) {
128712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)ar->mDataSink.u.mSink.pFormat;
129712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mSampleRateMilliHz = df_pcm->samplesPerSec;
130712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mNumChannels = df_pcm->numChannels;
131712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGV("AudioRecorder requested sample rate = %lumHz, %u channel(s)",
132712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi                    ar->mSampleRateMilliHz, ar->mNumChannels);
133712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
134712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        else {
135712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_SINK_FORMAT_MUST_BE_PCM);
136712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
137712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
1383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Source check:
1413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only input device sources are supported
1423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // check it's an IO device
1433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) {
144712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SOURCE_MUST_BE_IODEVICE);
1453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
1463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    } else {
1473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's an input device
1493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SLDataLocator_IODevice *dl_iod =  (SLDataLocator_IODevice *) pAudioSrc->pLocator;
1503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) {
151712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_IODEVICE_MUST_BE_AUDIOINPUT);
1523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
1533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's the default input device, others aren't supported here
1563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) {
157712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_INPUT_ID_MUST_BE_DEFAULT);
1583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
1593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return SL_RESULT_SUCCESS;
1633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
1643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) {
1663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info);
1673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    CAudioRecorder *ar = (CAudioRecorder *)user;
1693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    void * callbackPContext = NULL;
1703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch(event) {
1723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MORE_DATA: {
1733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        slBufferQueueCallback callback = NULL;
1743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info;
1753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // push data to the buffer queue
1773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_lock_exclusive(&ar->mBufferQueue);
1783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (ar->mBufferQueue.mState.count != 0) {
1803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear);
1813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *oldFront = ar->mBufferQueue.mFront;
1833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *newFront = &oldFront[1];
1843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // FIXME handle 8bit based on buffer format
1863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed);
1873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
1883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // can't consume the whole or rest of the buffer in one shot
1893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed += pBuff->size;
1903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // leave pBuff->size untouched
1913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
1923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
1933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
1943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
1953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
1963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
1973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            } else {
1983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // finish pushing the buffer or push the buffer in one shot
1993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed;
2003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed = 0;
2013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (newFront ==  &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) {
2023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    newFront = ar->mBufferQueue.mArray;
2033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                }
2043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mFront = newFront;
2053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.count--;
2073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.playIndex++;
2083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // data has been copied to the buffer, and the buffer queue state has been updated
2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // we will notify the client if applicable
2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callback = ar->mBufferQueue.mCallback;
2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // save callback data
2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callbackPContext = ar->mBufferQueue.mContext;
2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            }
2203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        } else {
2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // no destination to push the data
2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            pBuff->size = 0;
2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_unlock_exclusive(&ar->mBufferQueue);
2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // notify client
2273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (NULL != callback) {
2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (*callback)(&ar->mBufferQueue.mItf, callbackPContext);
2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MARKER:
2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // FIXME implement
2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("FIXME audioRecorder_callback(EVENT_MARKER, %p, %p) not supported", user, info);
2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_NEW_POS:
2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // FIXME implement
2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("FIXME audioRecorder_callback(EVENT_NEW_POS, %p, %p) not supported", user, info);
2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) {
2493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_create(%p) entering", ar);
2503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
2523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = NULL;
254b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mRecordSource = android::AUDIO_SOURCE_DEFAULT;
255b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
256b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
257b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
258b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
259b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
260b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
261b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_setConfig(CAudioRecorder* ar, const SLchar *configKey,
262b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        const void *pConfigValue, SLuint32 valueSize) {
263b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
264b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
265b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
266b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (NULL == ar) {
267b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
268b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if (NULL == pConfigValue) {
269b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_NULL_PARAM);
270b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
271b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
272b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
273b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
274b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
275b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > valueSize) {
276b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            SL_LOGE(ERROR_VALUESIZE_TOO_LOW);
277b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = SL_RESULT_PARAMETER_INVALID;
278b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
279b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_setPreset(ar, *(SLuint32*)pConfigValue);
280b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
281b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
282b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
283b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_UNKNOWN_KEY);
284b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
285b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
286b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
287b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
288b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
289b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
290b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
291b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
292b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_getConfig(CAudioRecorder* ar, const SLchar *configKey,
293b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SLuint32* pValueSize, void *pConfigValue) {
294b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
295b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
296b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
297b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (NULL == ar) {
298b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        return SL_RESULT_INTERNAL_ERROR;
299b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if ((NULL == pValueSize) || (NULL == pConfigValue)) {
300b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_NULL_PARAM);
301b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
302b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
303b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
304b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
305b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
306b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > *pValueSize) {
307b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            SL_LOGE(ERROR_VALUESIZE_TOO_LOW);
308b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = SL_RESULT_PARAMETER_INVALID;
309b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
310b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            *pValueSize = KEY_RECORDING_PRESET_PARAMSIZE;
311b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_getPreset(ar, (SLuint32*)pConfigValue);
312b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
313b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
314b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
315b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_UNKNOWN_KEY);
316b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
317b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
3183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
3203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) {
3253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_realize(%p) entering", ar);
3263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
329712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-independent CAudioRecorder fields
330712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != ar->mDataSink.mLocator.mLocatorType) {
331712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SINK_MUST_BE_BUFFERQUEUE);
332712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        return SL_RESULT_CONTENT_UNSUPPORTED;
333712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    }
334712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //  the following platform-independent field have been initialized in CreateAudioRecorder()
335712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mNumChannels
336712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mSampleRateMilliHz
337712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
33849e4076e940559bc204d0f0aa7ab412986445bfaGlenn Kasten    SL_LOGV("new AudioRecord %u channels, %lu mHz", ar->mNumChannels, ar->mSampleRateMilliHz);
339712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
340712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-specific CAudioRecorder fields
3413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = new android::AudioRecord();
342b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mAudioRecord->set(ar->mRecordSource, // source
343712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_sampleRate(ar->mSampleRateMilliHz), // sample rate in Hertz
3443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            android::AudioSystem::PCM_16_BIT,   //FIXME use format from buffer queue sink
345712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_channelMask(ar->mNumChannels, 0 /*no channel mask*/), // channel config
3463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     //frameCount min
3473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // flags
3483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            audioRecorder_callback,// callback_t
3493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (void*)ar,             // user, callback data, here the AudioRecorder
3503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // notificationFrames
3513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            false);                // threadCanCallJava, note: this will prevent direct Java
3523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                   //   callbacks, but we don't want them in the recording loop
3533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (android::NO_ERROR != ar->mAudioRecord->initCheck()) {
3553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar);
3563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
3573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
3583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
3603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = fopen(MONITOR_TARGET, "w");
3613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); }
3623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    else { SL_LOGE("recording to %s", MONITOR_TARGET); } // LOGE so it's always displayed
3633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
3643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
3663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) {
3713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_destroy(%p) entering", ar);
3723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != ar->mAudioRecord) {
3743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord->stop();
3753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        delete ar->mAudioRecord;
3763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord = NULL;
3773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
3783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
3803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != gMonitorFp) { fclose(gMonitorFp); }
3813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = NULL;
3823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
3833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) {
3883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_setRecordState(%p, %lu) entering", ar, state);
3893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
3913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return;
3923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
3933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch (state) {
3953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_STOPPED:
3963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
3973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
3983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_PAUSED:
3993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         // Note that pausing is treated like stop as this implementation only records to a buffer
4003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  queue, so there is no notion of destination being "opened" or "closed" (See description
4013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  of SL_RECORDSTATE in specification)
4023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_RECORDING:
4053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->start();
4063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     default:
4083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     }
4103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
412