AudioRecorder_to_android.cpp revision 3af2a8dd03f3113d5da1000dd79c143a9f0c4f36
13af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi/* 23af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project 33af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * 43af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); 53af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * you may not use this file except in compliance with the License. 63af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * You may obtain a copy of the License at 73af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * 83af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0 93af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * 103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software 113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, 123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * See the License for the specific language governing permissions and 143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi * limitations under the License. 153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi */ 163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#include "sles_allinclusive.h" 193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi// use this flag to dump all recorded audio into a file 213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//#define MONITOR_RECORDING 223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING 233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#define MONITOR_TARGET "/sdcard/monitor.raw" 243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#include <stdio.h> 253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic FILE* gMonitorFp = NULL; 263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif 273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//----------------------------------------------------------------------------- 303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) { 313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource; 333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi const SLDataSink *pAudioSnk = &ar->mDataSink.u.mSink; 343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // Sink check: 363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // only buffer queue sinks are supported, regardless of the data source 373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (SL_DATALOCATOR_BUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) { 383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("Android: Cannot create AudioRecorder: data sink must be \ 393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSL_DATALOCATOR_BUFFERQUEUE\n"); 403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return SL_RESULT_PARAMETER_INVALID; 413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // Source check: 443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // only input device sources are supported 453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // check it's an IO device 463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) { 473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("Android: Cannot create AudioRecorder: data source must be \ 483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSL_DATALOCATOR_IODEVICE\n"); 493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return SL_RESULT_PARAMETER_INVALID; 503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } else { 513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // check it's an input device 533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SLDataLocator_IODevice *dl_iod = (SLDataLocator_IODevice *) pAudioSrc->pLocator; 543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) { 553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("Android: Cannot create AudioRecorder: data source device type must be \ 563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSL_IODEVICE_AUDIOINPUT\n"); 573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return SL_RESULT_PARAMETER_INVALID; 583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // check it's the default input device, others aren't supported here 613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) { 623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("Android: Cannot create AudioRecorder: data source device ID must be \ 633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSL_DEFAULTDEVICEID_AUDIOINPUT\n"); 643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return SL_RESULT_PARAMETER_INVALID; 653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return SL_RESULT_SUCCESS; 693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi} 703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//----------------------------------------------------------------------------- 713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivistatic void audioRecorder_callback(int event, void* user, void *info) { 723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info); 733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi CAudioRecorder *ar = (CAudioRecorder *)user; 753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi void * callbackPContext = NULL; 763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi switch(event) { 783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi case android::AudioRecord::EVENT_MORE_DATA: { 793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi slBufferQueueCallback callback = NULL; 803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info; 813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // push data to the buffer queue 833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi interface_lock_exclusive(&ar->mBufferQueue); 843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (ar->mBufferQueue.mState.count != 0) { 863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear); 873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi BufferHeader *oldFront = ar->mBufferQueue.mFront; 893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi BufferHeader *newFront = &oldFront[1]; 903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // FIXME handle 8bit based on buffer format 923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed); 933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) { 943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // can't consume the whole or rest of the buffer in one shot 953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mBufferQueue.mSizeConsumed += pBuff->size; 963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // leave pBuff->size untouched 973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // consume data 983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // FIXME can we avoid holding the lock during the copy? 993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi memcpy (pDest, pBuff->i16, pBuff->size); 1003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING 1013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); } 1023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif 1033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } else { 1043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // finish pushing the buffer or push the buffer in one shot 1053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed; 1063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mBufferQueue.mSizeConsumed = 0; 1073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (newFront == &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) { 1083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi newFront = ar->mBufferQueue.mArray; 1093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mBufferQueue.mFront = newFront; 1113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mBufferQueue.mState.count--; 1133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mBufferQueue.mState.playIndex++; 1143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // consume data 1153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // FIXME can we avoid holding the lock during the copy? 1163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi memcpy (pDest, pBuff->i16, pBuff->size); 1173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING 1183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); } 1193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif 1203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // data has been copied to the buffer, and the buffer queue state has been updated 1213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // we will notify the client if applicable 1223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi callback = ar->mBufferQueue.mCallback; 1233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // save callback data 1243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi callbackPContext = ar->mBufferQueue.mContext; 1253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } else { 1273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // no destination to push the data 1283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi pBuff->size = 0; 1293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi interface_unlock_exclusive(&ar->mBufferQueue); 1323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // notify client 1333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL != callback) { 1343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi (*callback)(&ar->mBufferQueue.mItf, callbackPContext); 1353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 1383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi case android::AudioRecord::EVENT_MARKER: 1403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // FIXME implement 1413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("FIXME audioRecorder_callback(EVENT_MARKER, %p, %p) not supported", user, info); 1423af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 1433af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1443af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi case android::AudioRecord::EVENT_NEW_POS: 1453af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // FIXME implement 1463af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("FIXME audioRecorder_callback(EVENT_NEW_POS, %p, %p) not supported", user, info); 1473af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 1483af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1493af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1503af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi} 1513af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1523af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1533af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//----------------------------------------------------------------------------- 1543af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_create(CAudioRecorder* ar) { 1553af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGV("android_audioRecorder_create(%p) entering", ar); 1563af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1573af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SLresult result = SL_RESULT_SUCCESS; 1583af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1593af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord = NULL; 1603af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1613af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return result; 1623af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi} 1633af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1643af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1653af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//----------------------------------------------------------------------------- 1663af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel TriviSLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) { 1673af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGV("android_audioRecorder_realize(%p) entering", ar); 1683af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1693af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SLresult result = SL_RESULT_SUCCESS; 1703af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1713af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord = new android::AudioRecord(); 1723af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord->set(android::AUDIO_SOURCE_DEFAULT, // source 1733af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 22050, // sample rate in Hertz //FIXME use rate from buffer queue sink 1743af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi android::AudioSystem::PCM_16_BIT, //FIXME use format from buffer queue sink 1753af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi android::AudioSystem::CHANNEL_IN_MONO, // FIXME use channel config from buffer queue sink 1763af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 0, //frameCount min 1773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 0, // flags 1783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi audioRecorder_callback,// callback_t 1793af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi (void*)ar, // user, callback data, here the AudioRecorder 1803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 0, // notificationFrames 1813af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi false); // threadCanCallJava, note: this will prevent direct Java 1823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // callbacks, but we don't want them in the recording loop 1833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (android::NO_ERROR != ar->mAudioRecord->initCheck()) { 1853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar); 1863af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi result = SL_RESULT_CONTENT_UNSUPPORTED; 1873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 1883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1893af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING 1903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi gMonitorFp = fopen(MONITOR_TARGET, "w"); 1913af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); } 1923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi else { SL_LOGE("recording to %s", MONITOR_TARGET); } // LOGE so it's always displayed 1933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif 1943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return result; 1963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi} 1973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 1993af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//----------------------------------------------------------------------------- 2003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_destroy(CAudioRecorder* ar) { 2013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGV("android_audioRecorder_destroy(%p) entering", ar); 2023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2033af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL != ar->mAudioRecord) { 2043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord->stop(); 2053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi delete ar->mAudioRecord; 2063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord = NULL; 2073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 2083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef MONITOR_RECORDING 2103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL != gMonitorFp) { fclose(gMonitorFp); } 2113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi gMonitorFp = NULL; 2123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif 2133af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi} 2143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi//----------------------------------------------------------------------------- 2173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivivoid android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) { 2183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi SL_LOGV("android_audioRecorder_setRecordState(%p, %lu) entering", ar, state); 2193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2203af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi if (NULL == ar->mAudioRecord) { 2213af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi return; 2223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 2233af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi switch (state) { 2253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi case SL_RECORDSTATE_STOPPED: 2263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord->stop(); 2273af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 2283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi case SL_RECORDSTATE_PAUSED: 2293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // Note that pausing is treated like stop as this implementation only records to a buffer 2303af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // queue, so there is no notion of destination being "opened" or "closed" (See description 2313af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi // of SL_RECORDSTATE in specification) 2323af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord->stop(); 2333af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 2343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi case SL_RECORDSTATE_RECORDING: 2353af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi ar->mAudioRecord->start(); 2363af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 2373af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi default: 2383af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi break; 2393af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi } 2403af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi 2413af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi} 242