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#include "sles_allinclusive.h"
1875e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi#include "android_prompts.h"
194e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean#include "channels.h"
203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
212523b697aa1fda376c2ccd72680394d359acbd3fSvet Ganov#include <utils/String16.h>
222523b697aa1fda376c2ccd72680394d359acbd3fSvet Ganov
2310a3840407ac3ed61e7873ee7b86d664ccc6149fDima Zavin#include <system/audio.h>
244e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean#include <SLES/OpenSLES_Android.h>
25ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin
2615f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean#include <android_runtime/AndroidRuntime.h>
2715f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean
28b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_SOURCE_PARAMSIZE  sizeof(SLuint32)
29b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi#define KEY_RECORDING_PRESET_PARAMSIZE  sizeof(SLuint32)
30b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
31b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
32b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi// Internal utility functions
33b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//----------------------------
34b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
35b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_setPreset(CAudioRecorder* ar, SLuint32 recordPreset) {
36b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
37b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
38adf8bd5d4360542b6b6c48c4471fdd8d855e05ccGlenn Kasten    audio_source_t newRecordSource = AUDIO_SOURCE_DEFAULT;
39b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (recordPreset) {
40e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_GENERIC:
41ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_DEFAULT;
42b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
43b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_CAMCORDER:
44ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_CAMCORDER;
45b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
46b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION:
47ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_VOICE_RECOGNITION;
48b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
49a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION:
50ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        newRecordSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
51a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi        break;
52e9b57cefb954e7c1bffc5d4b59f89aca5e050797rago    case SL_ANDROID_RECORDING_PRESET_UNPROCESSED:
53e9b57cefb954e7c1bffc5d4b59f89aca5e050797rago            newRecordSource = AUDIO_SOURCE_UNPROCESSED;
54e9b57cefb954e7c1bffc5d4b59f89aca5e050797rago            break;
55b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    case SL_ANDROID_RECORDING_PRESET_NONE:
56b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // it is an error to set preset "none"
57b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
5875e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDERPRESET_SET_UNKNOWN_PRESET);
59b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
60b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
61b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
6275e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    // recording preset needs to be set before the object is realized
63e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    // (ap->mAudioRecord is supposed to be 0 until then)
645f71e35da153d194d805e030ab38935599e065d2Jean-Michel Trivi    if (SL_OBJECT_STATE_UNREALIZED != ar->mObject.mState) {
6575e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDERPRESET_REALIZED);
6675e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        result = SL_RESULT_PRECONDITIONS_VIOLATED;
6775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    } else {
6875e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        ar->mRecordSource = newRecordSource;
6975e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi    }
7075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi
71b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
72b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
73b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
74b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
75b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult audioRecorder_getPreset(CAudioRecorder* ar, SLuint32* pPreset) {
76b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
77b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
78b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    switch (ar->mRecordSource) {
79ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_DEFAULT:
80ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_MIC:
81e214a8c49938e7356943b59db53474e5fc3ae07cJean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_GENERIC;
82b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
83ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
84ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
85ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
86b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
87b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
88ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
89b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
90b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
91ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
92b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_CAMCORDER;
93b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
94ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
95a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
96a4b4d478171631eaa97e933eb46c1ff01bd04daaJean-Michel Trivi        break;
97e9b57cefb954e7c1bffc5d4b59f89aca5e050797rago    case AUDIO_SOURCE_UNPROCESSED:
98e9b57cefb954e7c1bffc5d4b59f89aca5e050797rago        *pPreset = SL_ANDROID_RECORDING_PRESET_UNPROCESSED;
99e9b57cefb954e7c1bffc5d4b59f89aca5e050797rago        break;
100b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    default:
101b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        *pPreset = SL_ANDROID_RECORDING_PRESET_NONE;
102b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_INTERNAL_ERROR;
103b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        break;
104b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
105b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
106b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
107b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
108b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
1093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1103b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleNewPos_lockRecord(CAudioRecorder* ar) {
1113b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_NEW_POS from AudioRecord");
1123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1133b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1153b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1163b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1173b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1183b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1193b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1203b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1213b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATNEWPOS was set in the event mask
1223b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATNEWPOS);
1233b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1243b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1253b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1263b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1273b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleMarker_lockRecord(CAudioRecorder* ar) {
1283b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_MARKER from AudioRecord");
1293b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1303b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1313b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1323b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
1333b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callback = ar->mRecord.mCallback;
1343b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    callbackPContext = ar->mRecord.mContext;
1353b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1363b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1373b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
1383b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // getting this event implies SL_RECORDEVENT_HEADATMARKER was set in the event mask
1393b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATMARKER);
1403b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1413b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1423b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1433b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1443b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivivoid audioRecorder_handleOverrun_lockRecord(CAudioRecorder* ar) {
1453b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    //SL_LOGV("received event EVENT_OVERRUN from AudioRecord");
1463b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    slRecordCallback callback = NULL;
1473b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    void* callbackPContext = NULL;
1483b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1493b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_lock_shared(&ar->mRecord);
150f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi    if (ar->mRecord.mCallbackEventsMask & SL_RECORDEVENT_HEADSTALLED) {
1513b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callback = ar->mRecord.mCallback;
1523b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        callbackPContext = ar->mRecord.mContext;
1533b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1543b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    interface_unlock_shared(&ar->mRecord);
1553b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1563b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (NULL != callback) {
157f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADSTALLED);
1583b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
1593b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
1603b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
1613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
16200a66a027199238a93b2aa9056af126efc09fe38Glenn KastenSLresult android_audioRecorder_checkSourceSink(CAudioRecorder* ar) {
1633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
1643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
1653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    const SLDataSink   *pAudioSnk = &ar->mDataSink.u.mSink;
1663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
167274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
168274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat;
169274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
170274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    const SLuint32 *df_representation = NULL; // pointer to representation field, if it exists
171274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
172274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    // sink must be an Android simple buffer queue with PCM data format
173274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    switch (sinkLocatorType) {
174274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: {
175274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        switch (sinkFormatType) {
176274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        case SL_ANDROID_DATAFORMAT_PCM_EX: {
177274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            const SLAndroidDataFormat_PCM_EX *df_pcm =
178274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten                    (SLAndroidDataFormat_PCM_EX *) pAudioSnk->pFormat;
179df9b397f73d8f063ff66e0fbf86ced075fe6d5aaGlenn Kasten            // checkDataFormat() already checked representation
180df9b397f73d8f063ff66e0fbf86ced075fe6d5aaGlenn Kasten            df_representation = &df_pcm->representation;
181274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        } // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
182274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        case SL_DATAFORMAT_PCM: {
183274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            const SLDataFormat_PCM *df_pcm = (const SLDataFormat_PCM *) pAudioSnk->pFormat;
1844e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean            // checkDataFormat already checked sample rate, channels, and mask
185712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            ar->mNumChannels = df_pcm->numChannels;
186a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten
187a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten            if (df_pcm->endianness != ar->mObject.mEngine->mEngine.mNativeEndianness) {
18871065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten                SL_LOGE("Cannot create audio recorder: unsupported byte order %u",
18971065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten                        df_pcm->endianness);
190a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten                return SL_RESULT_CONTENT_UNSUPPORTED;
191a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten            }
192a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten
193274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            ar->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
194a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten            SL_LOGV("AudioRecorder requested sample rate = %u mHz, %u channel(s)",
195712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi                    ar->mSampleRateMilliHz, ar->mNumChannels);
19671065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten
19771065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten            // we don't support container size != sample depth
19871065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten            if (df_pcm->containerSize != df_pcm->bitsPerSample) {
19971065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten                SL_LOGE("Cannot create audio recorder: unsupported container size %u bits for "
20071065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten                        "sample depth %u bits",
20171065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten                        df_pcm->containerSize, (SLuint32)df_pcm->bitsPerSample);
20271065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten                return SL_RESULT_CONTENT_UNSUPPORTED;
20371065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten            }
20471065fbf12abafd4c2a0dc85c81f13b564ff69fbGlenn Kasten
205274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            } break;
206274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        default:
20775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_SINK_FORMAT_MUST_BE_PCM);
208712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
209274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        }   // switch (sourceFormatType)
210274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        } break;    // case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
211274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    default:
212274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_ANDROIDSIMPLEBUFFERQUEUE);
213274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        return SL_RESULT_PARAMETER_INVALID;
214274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    }   // switch (sourceLocatorType)
2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // Source check:
2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // only input device sources are supported
2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    // check it's an IO device
2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) {
22075e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_RECORDER_SOURCE_MUST_BE_IODEVICE);
2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    } else {
2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's an input device
22522ced1dc023dc000118e3a26517b14e9babd7c5aGlenn Kasten        SLDataLocator_IODevice *dl_iod = (SLDataLocator_IODevice *) pAudioSrc->pLocator;
2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) {
22775e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_IODEVICE_MUST_BE_AUDIOINPUT);
2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // check it's the default input device, others aren't supported here
2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) {
23375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_RECORDER_INPUT_ID_MUST_BE_DEFAULT);
2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            return SL_RESULT_PARAMETER_INVALID;
2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return SL_RESULT_SUCCESS;
2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) {
2423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info);
2433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    CAudioRecorder *ar = (CAudioRecorder *)user;
245460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten
246460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    if (!android::CallbackProtector::enterCbIfOk(ar->mCallbackProtector)) {
247460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten        // it is not safe to enter the callback (the track is about to go away)
248460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten        return;
249460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    }
250460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten
2513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    void * callbackPContext = NULL;
2523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
253ac28eca1df49f581d952ffbda5d3019f7e3b7be6Glenn Kasten    switch (event) {
2543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MORE_DATA: {
2553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        slBufferQueueCallback callback = NULL;
2563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info;
2573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // push data to the buffer queue
2593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_lock_exclusive(&ar->mBufferQueue);
2603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (ar->mBufferQueue.mState.count != 0) {
2623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear);
2633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *oldFront = ar->mBufferQueue.mFront;
2653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            BufferHeader *newFront = &oldFront[1];
2663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
267274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            size_t availSink = oldFront->mSize - ar->mBufferQueue.mSizeConsumed;
268274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            size_t availSource = pBuff->size;
269274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            size_t bytesToCopy = availSink < availSource ? availSink : availSource;
270274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            void *pDest = (char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed;
271274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            memcpy(pDest, pBuff->raw, bytesToCopy);
272274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
273274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            if (bytesToCopy < availSink) {
2743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // can't consume the whole or rest of the buffer in one shot
275274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten                ar->mBufferQueue.mSizeConsumed += availSource;
276274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten                // pBuff->size is already equal to bytesToCopy in this case
2773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            } else {
2783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // finish pushing the buffer or push the buffer in one shot
279274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten                pBuff->size = bytesToCopy;
2803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mSizeConsumed = 0;
28122ced1dc023dc000118e3a26517b14e9babd7c5aGlenn Kasten                if (newFront == &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) {
2823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    newFront = ar->mBufferQueue.mArray;
2833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                }
2843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mFront = newFront;
2853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
2863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.count--;
2873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                ar->mBufferQueue.mState.playIndex++;
288274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
2893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // data has been copied to the buffer, and the buffer queue state has been updated
2903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // we will notify the client if applicable
2913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callback = ar->mBufferQueue.mCallback;
2923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                // save callback data
2933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                callbackPContext = ar->mBufferQueue.mContext;
2943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            }
295274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten        } else { // empty queue
2963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            // no destination to push the data
2973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            pBuff->size = 0;
2983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
2993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        interface_unlock_exclusive(&ar->mBufferQueue);
301274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
3023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        // notify client
3033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        if (NULL != callback) {
3043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (*callback)(&ar->mBufferQueue.mItf, callbackPContext);
3053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
3063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        }
3073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
3083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3093b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    case android::AudioRecord::EVENT_OVERRUN:
3103b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleOverrun_lockRecord(ar);
3113b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        break;
3123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
3133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_MARKER:
3143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleMarker_lockRecord(ar);
3153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
3163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    case android::AudioRecord::EVENT_NEW_POS:
3183b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        audioRecorder_handleNewPos_lockRecord(ar);
3193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        break;
3203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3219f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten    case android::AudioRecord::EVENT_NEW_IAUDIORECORD:
3229f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten        // ignore for now
3239f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten        break;
3249f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten
3259f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten    default:
3269f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten        SL_LOGE("Encountered unknown AudioRecord event %d for CAudioRecord %p", event, ar);
3279f3ac83aa036a780ca901f9ff75e47a5a7cbba1fGlenn Kasten        break;
3283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
329460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten
330460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    ar->mCallbackProtector->exitCb();
3313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
3323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
3343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
3353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) {
3363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_create(%p) entering", ar);
3373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
338b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource;
339b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLDataSink *pAudioSnk = &ar->mDataSink.u.mSink;
3403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
3413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
342b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator;
343b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
344b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi
345b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //  the following platform-independent fields have been initialized in CreateAudioRecorder()
346b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //    ar->mNumChannels
347b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //    ar->mSampleRateMilliHz
348b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi
349b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    if ((SL_DATALOCATOR_IODEVICE == sourceLocatorType) &&
350b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi            (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE == sinkLocatorType)) {
351b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi        // microphone to simple buffer queue
352b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi        ar->mAndroidObjType = AUDIORECORDER_FROM_MIC_TO_PCM_BUFFERQUEUE;
353e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten        ar->mAudioRecord.clear();
354460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten        ar->mCallbackProtector = new android::CallbackProtector();
355ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin        ar->mRecordSource = AUDIO_SOURCE_DEFAULT;
356b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    } else {
357b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
358b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi    }
359b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
360b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
361b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
362b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
363b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
364b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
365b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_setConfig(CAudioRecorder* ar, const SLchar *configKey,
366b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        const void *pConfigValue, SLuint32 valueSize) {
367b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
368c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    SLresult result;
369b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
370c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    assert(NULL != ar && NULL != configKey && NULL != pConfigValue);
371ca426f63e9c900ecbd28f8e3037aaf47ef739dd4Glenn Kasten    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
372b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
373b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
374b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        if (KEY_RECORDING_PRESET_PARAMSIZE > valueSize) {
37575e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
376c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = SL_RESULT_BUFFER_INSUFFICIENT;
377b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
378b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi            result = audioRecorder_setPreset(ar, *(SLuint32*)pConfigValue);
379b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
380b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
381b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
38275e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
383b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
384b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
385b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
386b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    return result;
387b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi}
388b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
389b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
390b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi//-----------------------------------------------------------------------------
391b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel TriviSLresult android_audioRecorder_getConfig(CAudioRecorder* ar, const SLchar *configKey,
392b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        SLuint32* pValueSize, void *pConfigValue) {
393b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
394c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    SLresult result;
395b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
396c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten    assert(NULL != ar && NULL != configKey && NULL != pValueSize);
397ca426f63e9c900ecbd28f8e3037aaf47ef739dd4Glenn Kasten    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) {
398b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
399b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        // recording preset
400c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten        if (NULL == pConfigValue) {
401c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = SL_RESULT_SUCCESS;
402c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten        } else if (KEY_RECORDING_PRESET_PARAMSIZE > *pValueSize) {
40375e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
404c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = SL_RESULT_BUFFER_INSUFFICIENT;
405b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        } else {
406c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten            result = audioRecorder_getPreset(ar, (SLuint32*)pConfigValue);
407b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        }
408c2a325746469c4c7625ec78a169b65a11dbe1e30Glenn Kasten        *pValueSize = KEY_RECORDING_PRESET_PARAMSIZE;
409b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi
410b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    } else {
41175e22870e41386cdc597bd29c76744d74d4c22adJean-Michel Trivi        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
412b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi        result = SL_RESULT_PARAMETER_INVALID;
413b3e52a63baaea367cf411348b68ecd8fd429b029Jean-Michel Trivi    }
4143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
4163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
4173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
4203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) {
4213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_realize(%p) entering", ar);
4223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SLresult result = SL_RESULT_SUCCESS;
4243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
425274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    // already checked in created and checkSourceSink
426274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    assert(ar->mDataSink.mLocator.mLocatorType == SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE);
427274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
428274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    const SLDataFormat_PCM *df_pcm = &ar->mDataSink.mFormat.mPCM;
429274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten
430b0cf731f4d81a3d6f8cac31de3cdddbbf555c305Jean-Michel Trivi    //  the following platform-independent fields have been initialized in CreateAudioRecorder()
431712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mNumChannels
432712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    //    ar->mSampleRateMilliHz
433712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
434274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec);
435712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
4365e3bcbffacc309fb2d9e002b7bc1ed131fddc6fcGlenn Kasten    // currently nothing analogous to canUseFastTrack() for recording
4375e3bcbffacc309fb2d9e002b7bc1ed131fddc6fcGlenn Kasten    audio_input_flags_t policy = AUDIO_INPUT_FLAG_FAST;
4385e3bcbffacc309fb2d9e002b7bc1ed131fddc6fcGlenn Kasten
4394e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean    SL_LOGV("Audio Record format: %dch(0x%x), %dbit, %dKHz",
4404e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean            df_pcm->numChannels,
4414e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean            df_pcm->channelMask,
4424e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean            df_pcm->bitsPerSample,
4434e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean            df_pcm->samplesPerSec / 1000000);
44433f46b8cd9ece650736a4aa5294318c6b51b2ffdilewis
4454e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean    // note that df_pcm->channelMask has already been validated during object creation.
4464e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean    audio_channel_mask_t channelMask = sles_to_audio_input_channel_mask(df_pcm->channelMask);
44733f46b8cd9ece650736a4aa5294318c6b51b2ffdilewis
44828340d1d9199b789f0db015680b008c98084f0b7ilewis    // To maintain backward compatibility with previous releases, ignore
44928340d1d9199b789f0db015680b008c98084f0b7ilewis    // channel masks that are not indexed.
45028340d1d9199b789f0db015680b008c98084f0b7ilewis    if (channelMask == AUDIO_CHANNEL_INVALID
45128340d1d9199b789f0db015680b008c98084f0b7ilewis            || audio_channel_mask_get_representation(channelMask)
45228340d1d9199b789f0db015680b008c98084f0b7ilewis                == AUDIO_CHANNEL_REPRESENTATION_POSITION) {
45333f46b8cd9ece650736a4aa5294318c6b51b2ffdilewis        channelMask = audio_channel_in_mask_from_count(df_pcm->numChannels);
45428340d1d9199b789f0db015680b008c98084f0b7ilewis        SL_LOGI("Emulating old channel mask behavior "
45528340d1d9199b789f0db015680b008c98084f0b7ilewis                "(ignoring positional mask %#x, using default mask %#x based on "
45628340d1d9199b789f0db015680b008c98084f0b7ilewis                "channel count of %d)", df_pcm->channelMask, channelMask,
45728340d1d9199b789f0db015680b008c98084f0b7ilewis                df_pcm->numChannels);
45833f46b8cd9ece650736a4aa5294318c6b51b2ffdilewis    }
45928340d1d9199b789f0db015680b008c98084f0b7ilewis    SL_LOGV("SLES channel mask %#x converted to Android mask %#x", df_pcm->channelMask, channelMask);
4604e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean
461712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi    // initialize platform-specific CAudioRecorder fields
462274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    ar->mAudioRecord = new android::AudioRecord(
4632dc0674aba6242c677365b675795773738397ab9Glenn Kasten            ar->mRecordSource,     // source
464274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            sampleRate,            // sample rate in Hertz
465274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            sles_to_android_sampleFormat(df_pcm),               // format
4664e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean            channelMask,           // channel mask
467274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            android::String16(),   // app ops
468274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten            0,                     // frameCount
4693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            audioRecorder_callback,// callback_t
4703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            (void*)ar,             // user, callback data, here the AudioRecorder
4713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi            0,                     // notificationFrames
472213c31153b9bda7b5a091f2996da7b655c8ed451Glenn Kasten            AUDIO_SESSION_ALLOCATE,
4735e3bcbffacc309fb2d9e002b7bc1ed131fddc6fcGlenn Kasten            android::AudioRecord::TRANSFER_CALLBACK,
4745e3bcbffacc309fb2d9e002b7bc1ed131fddc6fcGlenn Kasten                                   // transfer type
4755e3bcbffacc309fb2d9e002b7bc1ed131fddc6fcGlenn Kasten            policy);               // audio_input_flags_t
4763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
477274c079467f6ade516771b4c721c67041243cf4bGlenn Kasten    android::status_t status = ar->mAudioRecord->initCheck();
4782dc0674aba6242c677365b675795773738397ab9Glenn Kasten    if (android::NO_ERROR != status) {
4792dc0674aba6242c677365b675795773738397ab9Glenn Kasten        SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object; status %d",
4802dc0674aba6242c677365b675795773738397ab9Glenn Kasten                ar, status);
4812dc0674aba6242c677365b675795773738397ab9Glenn Kasten        // FIXME should return a more specific result depending on status
4823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        result = SL_RESULT_CONTENT_UNSUPPORTED;
4832dc0674aba6242c677365b675795773738397ab9Glenn Kasten        ar->mAudioRecord.clear();
4843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
4853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
48615f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean    // If there is a JavaAudioRoutingProxy associated with this recorder, hook it up...
48715f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean    JNIEnv* j_env = NULL;
48815f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean    jclass clsAudioRecord = NULL;
48915f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean    jmethodID midRoutingProxy_connect = NULL;
49015f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean    if (ar->mAndroidConfiguration.mRoutingProxy != NULL &&
49115f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean            (j_env = android::AndroidRuntime::getJNIEnv()) != NULL &&
49215f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean            (clsAudioRecord = j_env->FindClass("android/media/AudioRecord")) != NULL &&
49315f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean            (midRoutingProxy_connect =
49415f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean                j_env->GetMethodID(clsAudioRecord, "deferred_connect", "(J)V")) != NULL) {
49515f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean        j_env->ExceptionClear();
49615f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean        j_env->CallVoidMethod(ar->mAndroidConfiguration.mRoutingProxy,
49715f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean                              midRoutingProxy_connect,
49815f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean                              ar->mAudioRecord.get());
49915f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean        if (j_env->ExceptionCheck()) {
50015f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean            SL_LOGE("Java exception releasing recorder routing object.");
50115f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean            result = SL_RESULT_INTERNAL_ERROR;
50215f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean        }
50315f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean   }
50415f1e492e8fe7e2b6665009b5facd2b42913ab0fPaul McLean
5053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    return result;
5063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
5073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
510460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten/**
511460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten * Called with a lock on AudioRecorder, and blocks until safe to destroy
512460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten */
513460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kastenvoid android_audioRecorder_preDestroy(CAudioRecorder* ar) {
514460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    object_unlock_exclusive(&ar->mObject);
515460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    if (ar->mCallbackProtector != 0) {
516460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten        ar->mCallbackProtector->requestCbExitAndWait();
517460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    }
518460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    object_lock_exclusive(&ar->mObject);
519460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten}
520460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten
521460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten
522460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten//-----------------------------------------------------------------------------
5233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) {
5243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    SL_LOGV("android_audioRecorder_destroy(%p) entering", ar);
5253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
526e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    if (ar->mAudioRecord != 0) {
5273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        ar->mAudioRecord->stop();
528460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten        ar->mAudioRecord.clear();
5293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
530e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    // explicit destructor
531e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    ar->mAudioRecord.~sp();
532460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten    ar->mCallbackProtector.~sp();
5333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
5343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//-----------------------------------------------------------------------------
5373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) {
538a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten    SL_LOGV("android_audioRecorder_setRecordState(%p, %u) entering", ar, state);
5393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
540e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    if (ar->mAudioRecord == 0) {
5413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi        return;
5423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    }
5433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi    switch (state) {
5453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_STOPPED:
5463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
5473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
5483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_PAUSED:
5493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         // Note that pausing is treated like stop as this implementation only records to a buffer
5503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  queue, so there is no notion of destination being "opened" or "closed" (See description
5513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         //  of SL_RECORDSTATE in specification)
5523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->stop();
5533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
5543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     case SL_RECORDSTATE_RECORDING:
5553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         ar->mAudioRecord->start();
5563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
5573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     default:
5583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi         break;
5593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi     }
5603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi}
5623b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5633b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5643b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi//-----------------------------------------------------------------------------
565bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kastenvoid android_audioRecorder_useRecordEventMask(CAudioRecorder *ar) {
5663b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    IRecord *pRecordItf = &ar->mRecord;
5673b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    SLuint32 eventFlags = pRecordItf->mCallbackEventsMask;
5683b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
569e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    if (ar->mAudioRecord == 0) {
5703b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        return;
5713b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5723b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5733b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if ((eventFlags & SL_RECORDEVENT_HEADATMARKER) && (pRecordItf->mMarkerPosition != 0)) {
5743b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition((uint32_t)((((int64_t)pRecordItf->mMarkerPosition
5753b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
5763b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
5773b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear marker
5783b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setMarkerPosition(0);
5793b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5803b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5813b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATNEWPOS) {
582a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten        SL_LOGV("pos update period %d", pRecordItf->mPositionUpdatePeriod);
5833b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi         ar->mAudioRecord->setPositionUpdatePeriod(
5843b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                (uint32_t)((((int64_t)pRecordItf->mPositionUpdatePeriod
5853b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000));
5863b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    } else {
5873b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // clear periodic update
5883b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        ar->mAudioRecord->setPositionUpdatePeriod(0);
5893b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5903b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5913b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADATLIMIT) {
5923b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADATLIMIT
593a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten        SL_LOGD("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADATLIMIT) on an "
5943b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                    "SL_OBJECTID_AUDIORECORDER to be implemented ]");
5953b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
5963b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
5973b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADMOVING) {
5983b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi        // FIXME support SL_RECORDEVENT_HEADMOVING
599a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten        SL_LOGD("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADMOVING) on an "
6003b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi                "SL_OBJECTID_AUDIORECORDER to be implemented ]");
6013b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
6023b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
6033b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_BUFFER_FULL) {
604f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // nothing to do for SL_RECORDEVENT_BUFFER_FULL since this will not be encountered on
605f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // recording to buffer queues
6063b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
6073b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
6083b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    if (eventFlags & SL_RECORDEVENT_HEADSTALLED) {
609f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // nothing to do for SL_RECORDEVENT_HEADSTALLED, callback event will be checked against mask
610f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi        // when AudioRecord::EVENT_OVERRUN is encountered
611f6cca2f731329d101651348d1b7e51ead7b1290aJean-Michel Trivi
6123b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi    }
6133b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi
6143b142e50f4ae068f50f8e3d277e0f19910c67001Jean-Michel Trivi}
615b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi
616b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi
617b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi//-----------------------------------------------------------------------------
618b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivivoid android_audioRecorder_getPosition(CAudioRecorder *ar, SLmillisecond *pPosMsec) {
619e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten    if ((NULL == ar) || (ar->mAudioRecord == 0)) {
620b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        *pPosMsec = 0;
621b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi    } else {
622b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        uint32_t positionInFrames;
623b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        ar->mAudioRecord->getPosition(&positionInFrames);
6241a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten        if (ar->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) {
625b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi            *pPosMsec = 0;
626b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        } else {
627b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi            *pPosMsec = ((int64_t)positionInFrames * 1000) /
628b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi                    sles_to_android_sampleRate(ar->mSampleRateMilliHz);
629b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi        }
630b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi    }
631b5ef0195d7b8fa1fceae5ef8a256ed19eb03cbedJean-Michel Trivi}
632