AudioRecorder_to_android.cpp revision 75e22870e41386cdc597bd29c76744d74d4c22ad
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"
1975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi#include "android_prompts.h"
203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi// use this flag to dump all recorded audio into a file
223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//#define MONITOR_RECORDING
233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#define MONITOR_TARGET "/sdcard/monitor.raw"
253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#include <stdio.h>
263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic FILE* gMonitorFp = NULL;
273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
29b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
30b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_SOURCE_PARAMSIZE  sizeof(SLuint32)
31b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_PRESET_PARAMSIZE  sizeof(SLuint32)
32b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
33b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
34b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi// Internal utility functions
35b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//----------------------------
36b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
37b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_setPreset(CAudioRecorder* ar, SLuint32 recordPreset) {
38b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
39b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
4075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    int newRecordSource = android::AUDIO_SOURCE_DEFAULT;
41b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (recordPreset) {
42e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_GENERIC:
4375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        newRecordSource = android::AUDIO_SOURCE_DEFAULT;
44b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
45b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_CAMCORDER:
4675e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        newRecordSource = android::AUDIO_SOURCE_CAMCORDER;
47b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
48b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION:
4975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        newRecordSource = android::AUDIO_SOURCE_VOICE_RECOGNITION;
50b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
51b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_NONE:
52b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // it is an error to set preset "none"
53b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
5475e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDERPRESET_SET_UNKNOWN_PRESET);
55b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
56b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
57b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
5875e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    // recording preset needs to be set before the object is realized
5975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    // (ap->mAudioRecord is supposed to be NULL until then)
6075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    if (SL_OBJECT_STATE_UNREALIZED == ar->mObject.mState) {
6175e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDERPRESET_REALIZED);
6275e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        result = SL_RESULT_PRECONDITIONS_VIOLATED;
6375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    } else {
6475e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        ar->mRecordSource = newRecordSource;
6575e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    }
6675e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi
67b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
68b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
69b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
70b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
71b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_getPreset(CAudioRecorder* ar, SLuint32* pPreset) {
72b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
73b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
74b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (ar->mRecordSource) {
75b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_DEFAULT:
76b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_MIC:
77e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_GENERIC;
78b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
79b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_UPLINK:
80b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_DOWNLINK:
81b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_CALL:
82b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
83b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
84b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_VOICE_RECOGNITION:
85b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
86b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
87b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case android::AUDIO_SOURCE_CAMCORDER:
88b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_CAMCORDER;
89b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
90b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
91b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
92b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
93b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
94b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
95b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
96b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
97b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
98b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1003b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleNewPos_lockRecord(CAudioRecorder* ar) {
1013b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_NEW_POS from AudioRecord");
1023b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1033b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1043b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1053b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1063b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1073b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1083b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1093b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1103b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1113b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATNEWPOS was set in the event mask
1123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATNEWPOS);
1133b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1153b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1163b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1173b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleMarker_lockRecord(CAudioRecorder* ar) {
1183b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_MARKER from AudioRecord");
1193b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1203b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1213b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1223b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1233b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1243b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1253b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1263b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1273b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1283b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATMARKER was set in the event mask
1293b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATMARKER);
1303b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1313b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1323b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1333b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1343b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleOverrun_lockRecord(CAudioRecorder* ar) {
1353b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_OVERRUN from AudioRecord");
1363b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1373b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1383b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1393b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
140f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi    if (ar->mRecord.mCallbackEventsMask & SL_RECORDEVENT_HEADSTALLED) {
1413b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callback = ar->mRecord.mCallback;
1423b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callbackPContext = ar->mRecord.mContext;
1433b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1443b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1453b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1463b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
147f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADSTALLED);
1483b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1493b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1503b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) {
1533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
1553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSink   *pAudioSnk = &ar->mDataSink.u.mSink;
1563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Sink check:
1583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only buffer queue sinks are supported, regardless of the data source
1593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) {
16075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_BUFFERQUEUE);
1613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
162712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    } else {
163712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        // only PCM buffer queues are supported
164712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SLuint32 formatType = *(SLuint32 *)pAudioSnk->pFormat;
165712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        if (SL_DATAFORMAT_PCM == formatType) {
166712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)ar->mDataSink.u.mSink.pFormat;
167712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mSampleRateMilliHz = df_pcm->samplesPerSec;
168712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mNumChannels = df_pcm->numChannels;
169712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SL_LOGV("AudioRecorder requested sample rate = %lumHz, %u channel(s)",
170712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi                    ar->mSampleRateMilliHz, ar->mNumChannels);
171712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
172712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        else {
17375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_SINK_FORMAT_MUST_BE_PCM);
174712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
175712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
1763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Source check:
1793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only input device sources are supported
1803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // check it's an IO device
1813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) {
18275e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDER_SOURCE_MUST_BE_IODEVICE);
1833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
1843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    } else {
1853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's an input device
1873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SLDataLocator_IODevice *dl_iod =  (SLDataLocator_IODevice *) pAudioSrc->pLocator;
1883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) {
18975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_IODEVICE_MUST_BE_AUDIOINPUT);
1903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
1913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's the default input device, others aren't supported here
1943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) {
19575e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_INPUT_ID_MUST_BE_DEFAULT);
1963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
1973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
1983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return SL_RESULT_SUCCESS;
2013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) {
2043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info);
2053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    CAudioRecorder *ar = (CAudioRecorder *)user;
2073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    void * callbackPContext = NULL;
2083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch(event) {
2103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MORE_DATA: {
2113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        slBufferQueueCallback callback = NULL;
2123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info;
2133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // push data to the buffer queue
2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_lock_exclusive(&ar->mBufferQueue);
2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (ar->mBufferQueue.mState.count != 0) {
2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear);
2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *oldFront = ar->mBufferQueue.mFront;
2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *newFront = &oldFront[1];
2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // FIXME handle 8bit based on buffer format
2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed);
2253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // can't consume the whole or rest of the buffer in one shot
2273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed += pBuff->size;
2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // leave pBuff->size untouched
2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            } else {
2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // finish pushing the buffer or push the buffer in one shot
2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed;
2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed = 0;
2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (newFront ==  &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) {
2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    newFront = ar->mBufferQueue.mArray;
2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                }
2423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mFront = newFront;
2433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.count--;
2453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.playIndex++;
2463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // data has been copied to the buffer, and the buffer queue state has been updated
2533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // we will notify the client if applicable
2543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callback = ar->mBufferQueue.mCallback;
2553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // save callback data
2563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callbackPContext = ar->mBufferQueue.mContext;
2573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            }
2583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        } else {
2593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // no destination to push the data
2603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            pBuff->size = 0;
2613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_unlock_exclusive(&ar->mBufferQueue);
2643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // notify client
2653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (NULL != callback) {
2663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (*callback)(&ar->mBufferQueue.mItf, callbackPContext);
2673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2713b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    case android::AudioRecord::EVENT_OVERRUN:
2723b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleOverrun_lockRecord(ar);
2733b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        break;
2743b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
2753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MARKER:
2763b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleMarker_lockRecord(ar);
2773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_NEW_POS:
2803b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleNewPos_lockRecord(ar);
2813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) {
2893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_create(%p) entering", ar);
2903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
2923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = NULL;
294b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mRecordSource = android::AUDIO_SOURCE_DEFAULT;
295b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
296b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
297b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
298b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
299b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
300b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
301b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_setConfig(CAudioRecorder* ar, const SLchar *configKey,
302b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        const void *pConfigValue, SLuint32 valueSize) {
303b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
304b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
305b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
306b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (NULL == ar) {
307b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
308b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if (NULL == pConfigValue) {
30975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_NULL_PARAM);
310b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
311b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
312b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
313b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
314b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
315b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > valueSize) {
31675e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
317b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = SL_RESULT_PARAMETER_INVALID;
318b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
319b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_setPreset(ar, *(SLuint32*)pConfigValue);
320b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
321b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
322b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
32375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
324b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
325b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
326b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
327b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
328b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
329b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
330b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
331b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
332b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_getConfig(CAudioRecorder* ar, const SLchar *configKey,
333b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SLuint32* pValueSize, void *pConfigValue) {
334b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
335b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
336b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
337b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    if (NULL == ar) {
338b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        return SL_RESULT_INTERNAL_ERROR;
33975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    } else if (NULL == pValueSize) {
34075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_NULL_PARAM);
341b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
342b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
343b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
344b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
345b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
346b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > *pValueSize) {
34775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
348b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = SL_RESULT_PARAMETER_INVALID;
349b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
350b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            *pValueSize = KEY_RECORDING_PRESET_PARAMSIZE;
35175e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            if (NULL != pConfigValue) {
35275e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi                result = audioRecorder_getPreset(ar, (SLuint32*)pConfigValue);
35375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            }
354b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
355b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
356b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
35775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
358b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
359b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
3603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
3623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) {
3673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_realize(%p) entering", ar);
3683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
371712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-independent CAudioRecorder fields
372712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    if (SL_DATALOCATOR_BUFFERQUEUE != ar->mDataSink.mLocator.mLocatorType) {
37375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_BUFFERQUEUE);
374712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        return SL_RESULT_CONTENT_UNSUPPORTED;
375712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    }
376712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //  the following platform-independent field have been initialized in CreateAudioRecorder()
377712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mNumChannels
378712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mSampleRateMilliHz
379712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
38049e4076e940559bc204d0f0aa7ab412986445bfaGlenn Kasten    SL_LOGV("new AudioRecord %u channels, %lu mHz", ar->mNumChannels, ar->mSampleRateMilliHz);
381712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
382712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-specific CAudioRecorder fields
3833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = new android::AudioRecord();
384b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mAudioRecord->set(ar->mRecordSource, // source
385712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_sampleRate(ar->mSampleRateMilliHz), // sample rate in Hertz
3863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            android::AudioSystem::PCM_16_BIT,   //FIXME use format from buffer queue sink
387712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_channelMask(ar->mNumChannels, 0 /*no channel mask*/), // channel config
3883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     //frameCount min
3893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // flags
3903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            audioRecorder_callback,// callback_t
3913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (void*)ar,             // user, callback data, here the AudioRecorder
3923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // notificationFrames
3933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            false);                // threadCanCallJava, note: this will prevent direct Java
3943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                   //   callbacks, but we don't want them in the recording loop
3953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (android::NO_ERROR != ar->mAudioRecord->initCheck()) {
3973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar);
3983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
3993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
4023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = fopen(MONITOR_TARGET, "w");
4033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); }
4043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    else { SL_LOGE("recording to %s", MONITOR_TARGET); } // LOGE so it's always displayed
4053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
4083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) {
4133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_destroy(%p) entering", ar);
4143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != ar->mAudioRecord) {
4163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord->stop();
4173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        delete ar->mAudioRecord;
4183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord = NULL;
4193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
4223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != gMonitorFp) { fclose(gMonitorFp); }
4233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = NULL;
4243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) {
4303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_setRecordState(%p, %lu) entering", ar, state);
4313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
4333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return;
4343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch (state) {
4373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_STOPPED:
4383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_PAUSED:
4413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         // Note that pausing is treated like stop as this implementation only records to a buffer
4423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  queue, so there is no notion of destination being "opened" or "closed" (See description
4433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  of SL_RECORDSTATE in specification)
4443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_RECORDING:
4473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->start();
4483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     default:
4503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     }
4523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4543b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4553b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4563b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi//-----------------------------------------------------------------------------
4573b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid android_audioRecorder_useEventMask(CAudioRecorder *ar) {
4583b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    IRecord *pRecordItf = &ar->mRecord;
4593b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    SLuint32 eventFlags = pRecordItf->mCallbackEventsMask;
4603b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4613b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
4623b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        return;
4633b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4643b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4653b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if ((eventFlags & SL_RECORDEVENT_HEADATMARKER) && (pRecordItf->mMarkerPosition != 0)) {
4663b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition((uint32_t)((((int64_t)pRecordItf->mMarkerPosition
4673b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
4683b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
4693b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear marker
4703b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition(0);
4713b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4723b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4733b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATNEWPOS) {
4743b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi         ar->mAudioRecord->setPositionUpdatePeriod(
4753b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                (uint32_t)((((int64_t)pRecordItf->mPositionUpdatePeriod
4763b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
4773b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
4783b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear periodic update
4793b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setPositionUpdatePeriod(0);
4803b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4813b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4823b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATLIMIT) {
4833b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADATLIMIT
4843b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        SL_LOGE("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADATLIMIT) on an "
4853b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                    "SL_OBJECTID_AUDIORECORDER to be implemented ]");
4863b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4873b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4883b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADMOVING) {
4893b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADMOVING
4903b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        SL_LOGE("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADMOVING) on an "
4913b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                "SL_OBJECTID_AUDIORECORDER to be implemented ]");
4923b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4933b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4943b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_BUFFER_FULL) {
495f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // nothing to do for SL_RECORDEVENT_BUFFER_FULL since this will not be encountered on
496f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // recording to buffer queues
4973b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4983b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4993b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADSTALLED) {
500f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // nothing to do for SL_RECORDEVENT_HEADSTALLED, callback event will be checked against mask
501f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // when AudioRecord::EVENT_OVERRUN is encountered
502f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi
5033b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5043b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5053b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
506