AudioRecorder_to_android.cpp revision 3b142e50f4ae068f50f8e3d277e0f19910c67001
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 \
41e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi        "Cannot set recording preset: unknown or invalid 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) {
64e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_GENERIC:
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:
90e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_GENERIC;
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
1133b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleNewPos_lockRecord(CAudioRecorder* ar) {
1143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_NEW_POS from AudioRecord");
1153b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1163b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1173b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1183b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1193b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1203b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1213b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1223b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1233b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1243b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATNEWPOS was set in the event mask
1253b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATNEWPOS);
1263b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1273b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1283b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1293b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1303b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleMarker_lockRecord(CAudioRecorder* ar) {
1313b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_MARKER from AudioRecord");
1323b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1333b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1343b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1353b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1363b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1373b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1383b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1393b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1403b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1413b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATMARKER was set in the event mask
1423b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATMARKER);
1433b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1443b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1453b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1463b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1473b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleOverrun_lockRecord(CAudioRecorder* ar) {
1483b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_OVERRUN from AudioRecord");
1493b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1503b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1513b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1523b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1533b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (ar->mRecord.mCallbackEventsMask & SL_RECORDEVENT_BUFFER_FULL) {
1543b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callback = ar->mRecord.mCallback;
1553b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callbackPContext = ar->mRecord.mContext;
1563b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1573b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1583b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1593b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1603b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_BUFFER_FULL);
1613b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1623b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1633b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) {
1663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
1683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSink   *pAudioSnk = &ar->mDataSink.u.mSink;
1693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Sink check:
1713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only buffer queue sinks are supported, regardless of the data source
1723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) {
173712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SINK_MUST_BE_BUFFERQUEUE);
1743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
175712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    } else {
176712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        // only PCM buffer queues are supported
177712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SLuint32 formatType = *(SLuint32 *)pAudioSnk->pFormat;
178712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        if (SL_DATAFORMAT_PCM == formatType) {
179712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)ar->mDataSink.u.mSink.pFormat;
180712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mSampleRateMilliHz = df_pcm->samplesPerSec;
181712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mNumChannels = df_pcm->numChannels;
182712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGV("AudioRecorder requested sample rate = %lumHz, %u channel(s)",
183712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi                    ar->mSampleRateMilliHz, ar->mNumChannels);
184712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
185712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        else {
186712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_SINK_FORMAT_MUST_BE_PCM);
187712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
188712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
1893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Source check:
1923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only input device sources are supported
1933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // check it's an IO device
1943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) {
195712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SOURCE_MUST_BE_IODEVICE);
1963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
1973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    } else {
1983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's an input device
2003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SLDataLocator_IODevice *dl_iod =  (SLDataLocator_IODevice *) pAudioSrc->pLocator;
2013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) {
202712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_IODEVICE_MUST_BE_AUDIOINPUT);
2033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
2043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's the default input device, others aren't supported here
2073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) {
208712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGE(ERROR_INPUT_ID_MUST_BE_DEFAULT);
2093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
2103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return SL_RESULT_SUCCESS;
2143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) {
2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info);
2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    CAudioRecorder *ar = (CAudioRecorder *)user;
2203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    void * callbackPContext = NULL;
2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch(event) {
2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MORE_DATA: {
2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        slBufferQueueCallback callback = NULL;
2253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info;
2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // push data to the buffer queue
2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_lock_exclusive(&ar->mBufferQueue);
2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (ar->mBufferQueue.mState.count != 0) {
2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear);
2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *oldFront = ar->mBufferQueue.mFront;
2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *newFront = &oldFront[1];
2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // FIXME handle 8bit based on buffer format
2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed);
2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // can't consume the whole or rest of the buffer in one shot
2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed += pBuff->size;
2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // leave pBuff->size untouched
2423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            } else {
2493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // finish pushing the buffer or push the buffer in one shot
2503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed;
2513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed = 0;
2523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (newFront ==  &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) {
2533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    newFront = ar->mBufferQueue.mArray;
2543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                }
2553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mFront = newFront;
2563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.count--;
2583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.playIndex++;
2593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // data has been copied to the buffer, and the buffer queue state has been updated
2663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // we will notify the client if applicable
2673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callback = ar->mBufferQueue.mCallback;
2683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // save callback data
2693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callbackPContext = ar->mBufferQueue.mContext;
2703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            }
2713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        } else {
2723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // no destination to push the data
2733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            pBuff->size = 0;
2743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_unlock_exclusive(&ar->mBufferQueue);
2773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // notify client
2783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (NULL != callback) {
2793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (*callback)(&ar->mBufferQueue.mItf, callbackPContext);
2803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2843b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    case android::AudioRecord::EVENT_OVERRUN:
2853b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleOverrun_lockRecord(ar);
2863b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        break;
2873b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
2883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MARKER:
2893b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleMarker_lockRecord(ar);
2903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_NEW_POS:
2933b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleNewPos_lockRecord(ar);
2943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) {
3023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_create(%p) entering", ar);
3033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = NULL;
307b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mRecordSource = android::AUDIO_SOURCE_DEFAULT;
308b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
309b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
310b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
311b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
312b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
313b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
314b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_setConfig(CAudioRecorder* ar, const SLchar *configKey,
315b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        const void *pConfigValue, SLuint32 valueSize) {
316b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
317b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
318b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
319b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (NULL == ar) {
320b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
321b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if (NULL == pConfigValue) {
322b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_NULL_PARAM);
323b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
324b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
325b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
326b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
327b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
328b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > valueSize) {
329b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            SL_LOGE(ERROR_VALUESIZE_TOO_LOW);
330b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = SL_RESULT_PARAMETER_INVALID;
331b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
332b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_setPreset(ar, *(SLuint32*)pConfigValue);
333b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
334b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
335b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
336b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_UNKNOWN_KEY);
337b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
338b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
339b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
340b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
341b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
342b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
343b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
344b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
345b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_getConfig(CAudioRecorder* ar, const SLchar *configKey,
346b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SLuint32* pValueSize, void *pConfigValue) {
347b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
348b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
349b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
350b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (NULL == ar) {
351b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        return SL_RESULT_INTERNAL_ERROR;
352b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if ((NULL == pValueSize) || (NULL == pConfigValue)) {
353b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_NULL_PARAM);
354b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
355b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
356b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
357b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
358b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
359b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > *pValueSize) {
360b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            SL_LOGE(ERROR_VALUESIZE_TOO_LOW);
361b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = SL_RESULT_PARAMETER_INVALID;
362b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
363b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            *pValueSize = KEY_RECORDING_PRESET_PARAMSIZE;
364b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_getPreset(ar, (SLuint32*)pConfigValue);
365b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
366b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
367b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
368b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SL_LOGE(ERROR_UNKNOWN_KEY);
369b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
370b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
3713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
3733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) {
3783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_realize(%p) entering", ar);
3793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
382712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-independent CAudioRecorder fields
383712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != ar->mDataSink.mLocator.mLocatorType) {
384712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SL_LOGE(ERROR_SINK_MUST_BE_BUFFERQUEUE);
385712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        return SL_RESULT_CONTENT_UNSUPPORTED;
386712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    }
387712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //  the following platform-independent field have been initialized in CreateAudioRecorder()
388712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mNumChannels
389712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mSampleRateMilliHz
390712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
39149e4076e940559bc204d0f0aa7ab412986445bfaGlenn Kasten    SL_LOGV("new AudioRecord %u channels, %lu mHz", ar->mNumChannels, ar->mSampleRateMilliHz);
392712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
393712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-specific CAudioRecorder fields
3943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = new android::AudioRecord();
395b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mAudioRecord->set(ar->mRecordSource, // source
396712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_sampleRate(ar->mSampleRateMilliHz), // sample rate in Hertz
3973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            android::AudioSystem::PCM_16_BIT,   //FIXME use format from buffer queue sink
398712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_channelMask(ar->mNumChannels, 0 /*no channel mask*/), // channel config
3993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     //frameCount min
4003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // flags
4013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            audioRecorder_callback,// callback_t
4023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (void*)ar,             // user, callback data, here the AudioRecorder
4033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // notificationFrames
4043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            false);                // threadCanCallJava, note: this will prevent direct Java
4053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                   //   callbacks, but we don't want them in the recording loop
4063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (android::NO_ERROR != ar->mAudioRecord->initCheck()) {
4083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar);
4093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
4103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
4133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = fopen(MONITOR_TARGET, "w");
4143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); }
4153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    else { SL_LOGE("recording to %s", MONITOR_TARGET); } // LOGE so it's always displayed
4163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
4193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) {
4243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_destroy(%p) entering", ar);
4253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != ar->mAudioRecord) {
4273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord->stop();
4283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        delete ar->mAudioRecord;
4293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord = NULL;
4303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
4333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != gMonitorFp) { fclose(gMonitorFp); }
4343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = NULL;
4353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) {
4413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_setRecordState(%p, %lu) entering", ar, state);
4423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
4443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return;
4453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch (state) {
4483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_STOPPED:
4493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_PAUSED:
4523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         // Note that pausing is treated like stop as this implementation only records to a buffer
4533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  queue, so there is no notion of destination being "opened" or "closed" (See description
4543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  of SL_RECORDSTATE in specification)
4553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_RECORDING:
4583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->start();
4593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     default:
4613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     }
4633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4653b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4663b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4673b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi//-----------------------------------------------------------------------------
4683b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid android_audioRecorder_useEventMask(CAudioRecorder *ar) {
4693b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    IRecord *pRecordItf = &ar->mRecord;
4703b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    SLuint32 eventFlags = pRecordItf->mCallbackEventsMask;
4713b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4723b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
4733b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        return;
4743b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4753b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4763b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if ((eventFlags & SL_RECORDEVENT_HEADATMARKER) && (pRecordItf->mMarkerPosition != 0)) {
4773b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition((uint32_t)((((int64_t)pRecordItf->mMarkerPosition
4783b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
4793b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
4803b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear marker
4813b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition(0);
4823b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4833b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4843b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATNEWPOS) {
4853b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi         ar->mAudioRecord->setPositionUpdatePeriod(
4863b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                (uint32_t)((((int64_t)pRecordItf->mPositionUpdatePeriod
4873b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
4883b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
4893b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear periodic update
4903b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setPositionUpdatePeriod(0);
4913b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4923b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4933b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATLIMIT) {
4943b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADATLIMIT
4953b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        SL_LOGE("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADATLIMIT) on an "
4963b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                    "SL_OBJECTID_AUDIORECORDER to be implemented ]");
4973b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4983b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4993b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADMOVING) {
5003b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADMOVING
5013b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        SL_LOGE("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADMOVING) on an "
5023b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                "SL_OBJECTID_AUDIORECORDER to be implemented ]");
5033b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5043b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5053b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_BUFFER_FULL) {
5063b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // nothing to do for SL_RECORDEVENT_BUFFER_FULL, callback event will be checked against mask
5073b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // when AudioRecord::EVENT_OVERRUN is encountered
5083b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5093b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5103b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADSTALLED) {
5113b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADSTALLED
5123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        SL_LOGE("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADSTALLED) on an "
5133b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                "SL_OBJECTID_AUDIORECORDER to be implemented ]");
5143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5153b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5163b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
517