AudioRecorder_to_android.cpp revision adf8bd5d4360542b6b6c48c4471fdd8d855e05cc
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
2110a3840407ac3ed61e7873ee7b86d664ccc6149fDima Zavin#include <system/audio.h>
22ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin
233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi// use this flag to dump all recorded audio into a file
243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//#define MONITOR_RECORDING
253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#define MONITOR_TARGET "/sdcard/monitor.raw"
273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#include <stdio.h>
283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic FILE* gMonitorFp = NULL;
293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
31b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
32b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_SOURCE_PARAMSIZE  sizeof(SLuint32)
33b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_PRESET_PARAMSIZE  sizeof(SLuint32)
34b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
35b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
36b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi// Internal utility functions
37b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//----------------------------
38b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
39b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_setPreset(CAudioRecorder* ar, SLuint32 recordPreset) {
40b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
41b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
42adf8bd5d4360542b6b6c48c4471fdd8d855e05ccGlenn Kasten    audio_source_t newRecordSource = AUDIO_SOURCE_DEFAULT;
43b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (recordPreset) {
44e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_GENERIC:
45ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_DEFAULT;
46b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
47b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_CAMCORDER:
48ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_CAMCORDER;
49b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
50b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION:
51ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_VOICE_RECOGNITION;
52b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
53a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION:
54ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
55a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi        break;
56b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_NONE:
57b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // it is an error to set preset "none"
58b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
5975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDERPRESET_SET_UNKNOWN_PRESET);
60b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
61b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
62b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
6375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    // recording preset needs to be set before the object is realized
6475e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    // (ap->mAudioRecord is supposed to be NULL until then)
655f71e35da153d194d805e030ab38935599e065d2Jean-Michel Trivi    if (SL_OBJECT_STATE_UNREALIZED != ar->mObject.mState) {
6675e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDERPRESET_REALIZED);
6775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        result = SL_RESULT_PRECONDITIONS_VIOLATED;
6875e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    } else {
6975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        ar->mRecordSource = newRecordSource;
7075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    }
7175e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi
72b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
73b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
74b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
75b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
76b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_getPreset(CAudioRecorder* ar, SLuint32* pPreset) {
77b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
78b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
79b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (ar->mRecordSource) {
80ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_DEFAULT:
81ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_MIC:
82e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_GENERIC;
83b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
84ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
85ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
86ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
87b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
88b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
89ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
90b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
91b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
92ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
93b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_CAMCORDER;
94b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
95ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
96a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
97a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi        break;
98b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
99b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
100b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
101b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
102b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
103b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
104b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
105b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
106b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
1073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1083b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleNewPos_lockRecord(CAudioRecorder* ar) {
1093b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_NEW_POS from AudioRecord");
1103b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1113b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1133b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1153b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1163b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1173b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1183b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1193b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATNEWPOS was set in the event mask
1203b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATNEWPOS);
1213b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1223b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1233b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1243b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1253b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleMarker_lockRecord(CAudioRecorder* ar) {
1263b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_MARKER from AudioRecord");
1273b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1283b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1293b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1303b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1313b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1323b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1333b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1343b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1353b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1363b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATMARKER was set in the event mask
1373b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATMARKER);
1383b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1393b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1403b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1413b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1423b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleOverrun_lockRecord(CAudioRecorder* ar) {
1433b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_OVERRUN from AudioRecord");
1443b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1453b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1463b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1473b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
148f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi    if (ar->mRecord.mCallbackEventsMask & SL_RECORDEVENT_HEADSTALLED) {
1493b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callback = ar->mRecord.mCallback;
1503b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callbackPContext = ar->mRecord.mContext;
1513b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1523b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1533b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1543b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
155f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADSTALLED);
1563b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1573b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1583b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
1603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) {
1613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
1633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSink   *pAudioSnk = &ar->mDataSink.u.mSink;
1643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Sink check:
1663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only buffer queue sinks are supported, regardless of the data source
16701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    if (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) {
16801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten        SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_ANDROIDSIMPLEBUFFERQUEUE);
1693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
170712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    } else {
171712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        // only PCM buffer queues are supported
172712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        SLuint32 formatType = *(SLuint32 *)pAudioSnk->pFormat;
173712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        if (SL_DATAFORMAT_PCM == formatType) {
174712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)ar->mDataSink.u.mSink.pFormat;
175712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mSampleRateMilliHz = df_pcm->samplesPerSec;
176712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mNumChannels = df_pcm->numChannels;
177a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten            SL_LOGV("AudioRecorder requested sample rate = %u mHz, %u channel(s)",
178712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi                    ar->mSampleRateMilliHz, ar->mNumChannels);
179712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
180712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        else {
18175e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_SINK_FORMAT_MUST_BE_PCM);
182712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
183712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        }
1843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
1853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Source check:
1873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only input device sources are supported
1883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // check it's an IO device
1893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) {
19075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDER_SOURCE_MUST_BE_IODEVICE);
1913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
1923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    } else {
1933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's an input device
1953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SLDataLocator_IODevice *dl_iod =  (SLDataLocator_IODevice *) pAudioSrc->pLocator;
1963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) {
19775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_IODEVICE_MUST_BE_AUDIOINPUT);
1983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
1993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's the default input device, others aren't supported here
2023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) {
20375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_INPUT_ID_MUST_BE_DEFAULT);
2043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
2053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return SL_RESULT_SUCCESS;
2093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) {
2123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info);
2133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    CAudioRecorder *ar = (CAudioRecorder *)user;
2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    void * callbackPContext = NULL;
2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch(event) {
2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MORE_DATA: {
2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        slBufferQueueCallback callback = NULL;
2203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info;
2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // push data to the buffer queue
2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_lock_exclusive(&ar->mBufferQueue);
2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (ar->mBufferQueue.mState.count != 0) {
2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear);
2273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *oldFront = ar->mBufferQueue.mFront;
2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *newFront = &oldFront[1];
2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // FIXME handle 8bit based on buffer format
2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed);
2333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // can't consume the whole or rest of the buffer in one shot
2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed += pBuff->size;
2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // leave pBuff->size untouched
2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            } else {
2443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // finish pushing the buffer or push the buffer in one shot
2453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed;
2463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed = 0;
2473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (newFront ==  &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) {
2483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    newFront = ar->mBufferQueue.mArray;
2493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                }
2503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mFront = newFront;
2513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.count--;
2533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.playIndex++;
2543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // consume data
2553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // FIXME can we avoid holding the lock during the copy?
2563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                memcpy (pDest, pBuff->i16, pBuff->size);
2573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
2583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); }
2593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
2603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // data has been copied to the buffer, and the buffer queue state has been updated
2613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // we will notify the client if applicable
2623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callback = ar->mBufferQueue.mCallback;
2633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // save callback data
2643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callbackPContext = ar->mBufferQueue.mContext;
2653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            }
2663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        } else {
2673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // no destination to push the data
2683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            pBuff->size = 0;
2693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_unlock_exclusive(&ar->mBufferQueue);
2723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // notify client
2733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (NULL != callback) {
2743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (*callback)(&ar->mBufferQueue.mItf, callbackPContext);
2753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2793b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    case android::AudioRecord::EVENT_OVERRUN:
2803b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleOverrun_lockRecord(ar);
2813b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        break;
2823b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
2833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MARKER:
2843b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleMarker_lockRecord(ar);
2853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_NEW_POS:
2883b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleNewPos_lockRecord(ar);
2893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
2903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) {
2973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_create(%p) entering", ar);
2983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
299b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
300b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLDataSink *pAudioSnk = &ar->mDataSink.u.mSink;
3013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
303b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator;
304b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
305b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi
306b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //  the following platform-independent fields have been initialized in CreateAudioRecorder()
307b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //    ar->mNumChannels
308b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //    ar->mSampleRateMilliHz
309b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi
310b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    if ((SL_DATALOCATOR_IODEVICE == sourceLocatorType) &&
311b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi            (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE == sinkLocatorType)) {
312b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi        // microphone to simple buffer queue
313b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi        ar->mAndroidObjType = AUDIORECORDER_FROM_MIC_TO_PCM_BUFFERQUEUE;
314b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi        ar->mAudioRecord = NULL;
315ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        ar->mRecordSource = AUDIO_SOURCE_DEFAULT;
316b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    } else {
317b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
318b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    }
319b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
320b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
321b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
322b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
323b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
324b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
325b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_setConfig(CAudioRecorder* ar, const SLchar *configKey,
326b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        const void *pConfigValue, SLuint32 valueSize) {
327b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
328c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    SLresult result;
329b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
330c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    assert(NULL != ar && NULL != configKey && NULL != pConfigValue);
331ca426f63e9c900ecbd28f8e3037aaf47ef739dd4Glenn Kasten    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
332b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
333b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
334b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > valueSize) {
33575e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
336c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = SL_RESULT_BUFFER_INSUFFICIENT;
337b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
338b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_setPreset(ar, *(SLuint32*)pConfigValue);
339b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
340b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
341b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
34275e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
343b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
344b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
345b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
346b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
347b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
348b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
349b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
350b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
351b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_getConfig(CAudioRecorder* ar, const SLchar *configKey,
352b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SLuint32* pValueSize, void *pConfigValue) {
353b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
354c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    SLresult result;
355b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
356c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    assert(NULL != ar && NULL != configKey && NULL != pValueSize);
357ca426f63e9c900ecbd28f8e3037aaf47ef739dd4Glenn Kasten    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
358b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
359b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
360c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten        if (NULL == pConfigValue) {
361c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = SL_RESULT_SUCCESS;
362c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten        } else if (KEY_RECORDING_PRESET_PARAMSIZE > *pValueSize) {
36375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
364c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = SL_RESULT_BUFFER_INSUFFICIENT;
365b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
366c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = audioRecorder_getPreset(ar, (SLuint32*)pConfigValue);
367b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
368c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten        *pValueSize = KEY_RECORDING_PRESET_PARAMSIZE;
369b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
370b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
37175e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
372b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
373b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
3743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
3763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) {
3813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_realize(%p) entering", ar);
3823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
385712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-independent CAudioRecorder fields
38601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    if (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE != ar->mDataSink.mLocator.mLocatorType) {
38701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten        SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_ANDROIDSIMPLEBUFFERQUEUE);
388712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi        return SL_RESULT_CONTENT_UNSUPPORTED;
389712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    }
390b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //  the following platform-independent fields have been initialized in CreateAudioRecorder()
391712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mNumChannels
392712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mSampleRateMilliHz
393712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
394a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten    SL_LOGV("new AudioRecord %u channels, %u mHz", ar->mNumChannels, ar->mSampleRateMilliHz);
395712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
396712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-specific CAudioRecorder fields
3973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    ar->mAudioRecord = new android::AudioRecord();
398b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    ar->mAudioRecord->set(ar->mRecordSource, // source
399712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            sles_to_android_sampleRate(ar->mSampleRateMilliHz), // sample rate in Hertz
400ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin            AUDIO_FORMAT_PCM_16_BIT,   //FIXME use format from buffer queue sink
4011d081e49a10543018e1ae33792bd3d30504719baGlenn Kasten            sles_to_android_channelMaskIn(ar->mNumChannels, 0 /*no channel mask*/),
4021d081e49a10543018e1ae33792bd3d30504719baGlenn Kasten                                   // channel config
4033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     //frameCount min
4043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // flags
4053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            audioRecorder_callback,// callback_t
4063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (void*)ar,             // user, callback data, here the AudioRecorder
4073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // notificationFrames
4083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            false);                // threadCanCallJava, note: this will prevent direct Java
4093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                   //   callbacks, but we don't want them in the recording loop
4103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (android::NO_ERROR != ar->mAudioRecord->initCheck()) {
4123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar);
4133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
4143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
4173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    gMonitorFp = fopen(MONITOR_TARGET, "w");
4183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); }
419a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten    else { SL_LOGE("recording to %s", MONITOR_TARGET); } // SL_LOGE so it's always displayed
4203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
4233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) {
4283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_destroy(%p) entering", ar);
4293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL != ar->mAudioRecord) {
4313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord->stop();
4323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        delete ar->mAudioRecord;
4333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord = NULL;
4343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING
437a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten    if (NULL != gMonitorFp) {
438a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten        fclose(gMonitorFp);
439a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten        gMonitorFp = NULL;
440a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten    }
4413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) {
447a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten    SL_LOGV("android_audioRecorder_setRecordState(%p, %u) entering", ar, state);
4483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
4503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return;
4513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch (state) {
4543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_STOPPED:
4553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_PAUSED:
4583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         // Note that pausing is treated like stop as this implementation only records to a buffer
4593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  queue, so there is no notion of destination being "opened" or "closed" (See description
4603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  of SL_RECORDSTATE in specification)
4613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
4623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_RECORDING:
4643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->start();
4653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     default:
4673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
4683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     }
4693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4713b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4723b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4733b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi//-----------------------------------------------------------------------------
474bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kastenvoid android_audioRecorder_useRecordEventMask(CAudioRecorder *ar) {
4753b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    IRecord *pRecordItf = &ar->mRecord;
4763b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    SLuint32 eventFlags = pRecordItf->mCallbackEventsMask;
4773b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4783b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL == ar->mAudioRecord) {
4793b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        return;
4803b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4813b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4823b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if ((eventFlags & SL_RECORDEVENT_HEADATMARKER) && (pRecordItf->mMarkerPosition != 0)) {
4833b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition((uint32_t)((((int64_t)pRecordItf->mMarkerPosition
4843b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
4853b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
4863b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear marker
4873b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition(0);
4883b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4893b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
4903b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATNEWPOS) {
491a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten        SL_LOGV("pos update period %d", pRecordItf->mPositionUpdatePeriod);
4923b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi         ar->mAudioRecord->setPositionUpdatePeriod(
4933b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                (uint32_t)((((int64_t)pRecordItf->mPositionUpdatePeriod
4943b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
4953b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
4963b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear periodic update
4973b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setPositionUpdatePeriod(0);
4983b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
4993b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5003b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATLIMIT) {
5013b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADATLIMIT
502a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten        SL_LOGD("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADATLIMIT) on an "
5033b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                    "SL_OBJECTID_AUDIORECORDER to be implemented ]");
5043b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5053b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5063b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADMOVING) {
5073b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADMOVING
508a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten        SL_LOGD("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADMOVING) on an "
5093b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                "SL_OBJECTID_AUDIORECORDER to be implemented ]");
5103b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5113b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_BUFFER_FULL) {
513f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // nothing to do for SL_RECORDEVENT_BUFFER_FULL since this will not be encountered on
514f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // recording to buffer queues
5153b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5163b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5173b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADSTALLED) {
518f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // nothing to do for SL_RECORDEVENT_HEADSTALLED, callback event will be checked against mask
519f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // when AudioRecord::EVENT_OVERRUN is encountered
520f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi
5213b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5223b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5233b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
524b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi
525b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi
526b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi//-----------------------------------------------------------------------------
527b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivivoid android_audioRecorder_getPosition(CAudioRecorder *ar, SLmillisecond *pPosMsec) {
528b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi    if ((NULL == ar) || (NULL == ar->mAudioRecord)) {
529b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        *pPosMsec = 0;
530b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi    } else {
531b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        uint32_t positionInFrames;
532b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        ar->mAudioRecord->getPosition(&positionInFrames);
5331a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten        if (ar->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) {
534b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi            *pPosMsec = 0;
535b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        } else {
536b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi            *pPosMsec = ((int64_t)positionInFrames * 1000) /
537b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi                    sles_to_android_sampleRate(ar->mSampleRateMilliHz);
538b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        }
539b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi    }
540b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi}
541