AudioRecorder_to_android.cpp revision b05ea38e5131001884aa226f90fd50cf594a23f3
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18#include "sles_allinclusive.h" 19#include "android_prompts.h" 20 21// use this flag to dump all recorded audio into a file 22//#define MONITOR_RECORDING 23#ifdef MONITOR_RECORDING 24#define MONITOR_TARGET "/sdcard/monitor.raw" 25#include <stdio.h> 26static FILE* gMonitorFp = NULL; 27#endif 28 29 30#define KEY_RECORDING_SOURCE_PARAMSIZE sizeof(SLuint32) 31#define KEY_RECORDING_PRESET_PARAMSIZE sizeof(SLuint32) 32 33//----------------------------------------------------------------------------- 34// Internal utility functions 35//---------------------------- 36 37SLresult audioRecorder_setPreset(CAudioRecorder* ar, SLuint32 recordPreset) { 38 SLresult result = SL_RESULT_SUCCESS; 39 40 int newRecordSource = android::AUDIO_SOURCE_DEFAULT; 41 switch (recordPreset) { 42 case SL_ANDROID_RECORDING_PRESET_GENERIC: 43 newRecordSource = android::AUDIO_SOURCE_DEFAULT; 44 break; 45 case SL_ANDROID_RECORDING_PRESET_CAMCORDER: 46 newRecordSource = android::AUDIO_SOURCE_CAMCORDER; 47 break; 48 case SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION: 49 newRecordSource = android::AUDIO_SOURCE_VOICE_RECOGNITION; 50 break; 51 case SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION: 52 newRecordSource = android::AUDIO_SOURCE_VOICE_COMMUNICATION; 53 break; 54 case SL_ANDROID_RECORDING_PRESET_NONE: 55 // it is an error to set preset "none" 56 default: 57 SL_LOGE(ERROR_RECORDERPRESET_SET_UNKNOWN_PRESET); 58 result = SL_RESULT_PARAMETER_INVALID; 59 } 60 61 // recording preset needs to be set before the object is realized 62 // (ap->mAudioRecord is supposed to be NULL until then) 63 if (SL_OBJECT_STATE_UNREALIZED != ar->mObject.mState) { 64 SL_LOGE(ERROR_RECORDERPRESET_REALIZED); 65 result = SL_RESULT_PRECONDITIONS_VIOLATED; 66 } else { 67 ar->mRecordSource = newRecordSource; 68 } 69 70 return result; 71} 72 73 74SLresult audioRecorder_getPreset(CAudioRecorder* ar, SLuint32* pPreset) { 75 SLresult result = SL_RESULT_SUCCESS; 76 77 switch (ar->mRecordSource) { 78 case android::AUDIO_SOURCE_DEFAULT: 79 case android::AUDIO_SOURCE_MIC: 80 *pPreset = SL_ANDROID_RECORDING_PRESET_GENERIC; 81 break; 82 case android::AUDIO_SOURCE_VOICE_UPLINK: 83 case android::AUDIO_SOURCE_VOICE_DOWNLINK: 84 case android::AUDIO_SOURCE_VOICE_CALL: 85 *pPreset = SL_ANDROID_RECORDING_PRESET_NONE; 86 break; 87 case android::AUDIO_SOURCE_VOICE_RECOGNITION: 88 *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION; 89 break; 90 case android::AUDIO_SOURCE_CAMCORDER: 91 *pPreset = SL_ANDROID_RECORDING_PRESET_CAMCORDER; 92 break; 93 case android::AUDIO_SOURCE_VOICE_COMMUNICATION: 94 *pPreset = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION; 95 break; 96 default: 97 *pPreset = SL_ANDROID_RECORDING_PRESET_NONE; 98 result = SL_RESULT_INTERNAL_ERROR; 99 break; 100 } 101 102 return result; 103} 104 105 106void audioRecorder_handleNewPos_lockRecord(CAudioRecorder* ar) { 107 //SL_LOGV("received event EVENT_NEW_POS from AudioRecord"); 108 slRecordCallback callback = NULL; 109 void* callbackPContext = NULL; 110 111 interface_lock_shared(&ar->mRecord); 112 callback = ar->mRecord.mCallback; 113 callbackPContext = ar->mRecord.mContext; 114 interface_unlock_shared(&ar->mRecord); 115 116 if (NULL != callback) { 117 // getting this event implies SL_RECORDEVENT_HEADATNEWPOS was set in the event mask 118 (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATNEWPOS); 119 } 120} 121 122 123void audioRecorder_handleMarker_lockRecord(CAudioRecorder* ar) { 124 //SL_LOGV("received event EVENT_MARKER from AudioRecord"); 125 slRecordCallback callback = NULL; 126 void* callbackPContext = NULL; 127 128 interface_lock_shared(&ar->mRecord); 129 callback = ar->mRecord.mCallback; 130 callbackPContext = ar->mRecord.mContext; 131 interface_unlock_shared(&ar->mRecord); 132 133 if (NULL != callback) { 134 // getting this event implies SL_RECORDEVENT_HEADATMARKER was set in the event mask 135 (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADATMARKER); 136 } 137} 138 139 140void audioRecorder_handleOverrun_lockRecord(CAudioRecorder* ar) { 141 //SL_LOGV("received event EVENT_OVERRUN from AudioRecord"); 142 slRecordCallback callback = NULL; 143 void* callbackPContext = NULL; 144 145 interface_lock_shared(&ar->mRecord); 146 if (ar->mRecord.mCallbackEventsMask & SL_RECORDEVENT_HEADSTALLED) { 147 callback = ar->mRecord.mCallback; 148 callbackPContext = ar->mRecord.mContext; 149 } 150 interface_unlock_shared(&ar->mRecord); 151 152 if (NULL != callback) { 153 (*callback)(&ar->mRecord.mItf, callbackPContext, SL_RECORDEVENT_HEADSTALLED); 154 } 155} 156 157//----------------------------------------------------------------------------- 158SLresult android_audioRecorder_checkSourceSinkSupport(CAudioRecorder* ar) { 159 160 const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource; 161 const SLDataSink *pAudioSnk = &ar->mDataSink.u.mSink; 162 163 // Sink check: 164 // only buffer queue sinks are supported, regardless of the data source 165 if (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE != *(SLuint32 *)pAudioSnk->pLocator) { 166 SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_ANDROIDSIMPLEBUFFERQUEUE); 167 return SL_RESULT_PARAMETER_INVALID; 168 } else { 169 // only PCM buffer queues are supported 170 SLuint32 formatType = *(SLuint32 *)pAudioSnk->pFormat; 171 if (SL_DATAFORMAT_PCM == formatType) { 172 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)ar->mDataSink.u.mSink.pFormat; 173 ar->mSampleRateMilliHz = df_pcm->samplesPerSec; 174 ar->mNumChannels = df_pcm->numChannels; 175 SL_LOGV("AudioRecorder requested sample rate = %lumHz, %u channel(s)", 176 ar->mSampleRateMilliHz, ar->mNumChannels); 177 } 178 else { 179 SL_LOGE(ERROR_RECORDER_SINK_FORMAT_MUST_BE_PCM); 180 return SL_RESULT_PARAMETER_INVALID; 181 } 182 } 183 184 // Source check: 185 // only input device sources are supported 186 // check it's an IO device 187 if (SL_DATALOCATOR_IODEVICE != *(SLuint32 *)pAudioSrc->pLocator) { 188 SL_LOGE(ERROR_RECORDER_SOURCE_MUST_BE_IODEVICE); 189 return SL_RESULT_PARAMETER_INVALID; 190 } else { 191 192 // check it's an input device 193 SLDataLocator_IODevice *dl_iod = (SLDataLocator_IODevice *) pAudioSrc->pLocator; 194 if (SL_IODEVICE_AUDIOINPUT != dl_iod->deviceType) { 195 SL_LOGE(ERROR_RECORDER_IODEVICE_MUST_BE_AUDIOINPUT); 196 return SL_RESULT_PARAMETER_INVALID; 197 } 198 199 // check it's the default input device, others aren't supported here 200 if (SL_DEFAULTDEVICEID_AUDIOINPUT != dl_iod->deviceID) { 201 SL_LOGE(ERROR_RECORDER_INPUT_ID_MUST_BE_DEFAULT); 202 return SL_RESULT_PARAMETER_INVALID; 203 } 204 } 205 206 return SL_RESULT_SUCCESS; 207} 208//----------------------------------------------------------------------------- 209static void audioRecorder_callback(int event, void* user, void *info) { 210 //SL_LOGV("audioRecorder_callback(%d, %p, %p) entering", event, user, info); 211 212 CAudioRecorder *ar = (CAudioRecorder *)user; 213 void * callbackPContext = NULL; 214 215 switch(event) { 216 case android::AudioRecord::EVENT_MORE_DATA: { 217 slBufferQueueCallback callback = NULL; 218 android::AudioRecord::Buffer* pBuff = (android::AudioRecord::Buffer*)info; 219 220 // push data to the buffer queue 221 interface_lock_exclusive(&ar->mBufferQueue); 222 223 if (ar->mBufferQueue.mState.count != 0) { 224 assert(ar->mBufferQueue.mFront != ar->mBufferQueue.mRear); 225 226 BufferHeader *oldFront = ar->mBufferQueue.mFront; 227 BufferHeader *newFront = &oldFront[1]; 228 229 // FIXME handle 8bit based on buffer format 230 short *pDest = (short*)((char *)oldFront->mBuffer + ar->mBufferQueue.mSizeConsumed); 231 if (ar->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) { 232 // can't consume the whole or rest of the buffer in one shot 233 ar->mBufferQueue.mSizeConsumed += pBuff->size; 234 // leave pBuff->size untouched 235 // consume data 236 // FIXME can we avoid holding the lock during the copy? 237 memcpy (pDest, pBuff->i16, pBuff->size); 238#ifdef MONITOR_RECORDING 239 if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); } 240#endif 241 } else { 242 // finish pushing the buffer or push the buffer in one shot 243 pBuff->size = oldFront->mSize - ar->mBufferQueue.mSizeConsumed; 244 ar->mBufferQueue.mSizeConsumed = 0; 245 if (newFront == &ar->mBufferQueue.mArray[ar->mBufferQueue.mNumBuffers + 1]) { 246 newFront = ar->mBufferQueue.mArray; 247 } 248 ar->mBufferQueue.mFront = newFront; 249 250 ar->mBufferQueue.mState.count--; 251 ar->mBufferQueue.mState.playIndex++; 252 // consume data 253 // FIXME can we avoid holding the lock during the copy? 254 memcpy (pDest, pBuff->i16, pBuff->size); 255#ifdef MONITOR_RECORDING 256 if (NULL != gMonitorFp) { fwrite(pBuff->i16, pBuff->size, 1, gMonitorFp); } 257#endif 258 // data has been copied to the buffer, and the buffer queue state has been updated 259 // we will notify the client if applicable 260 callback = ar->mBufferQueue.mCallback; 261 // save callback data 262 callbackPContext = ar->mBufferQueue.mContext; 263 } 264 } else { 265 // no destination to push the data 266 pBuff->size = 0; 267 } 268 269 interface_unlock_exclusive(&ar->mBufferQueue); 270 // notify client 271 if (NULL != callback) { 272 (*callback)(&ar->mBufferQueue.mItf, callbackPContext); 273 } 274 } 275 break; 276 277 case android::AudioRecord::EVENT_OVERRUN: 278 audioRecorder_handleOverrun_lockRecord(ar); 279 break; 280 281 case android::AudioRecord::EVENT_MARKER: 282 audioRecorder_handleMarker_lockRecord(ar); 283 break; 284 285 case android::AudioRecord::EVENT_NEW_POS: 286 audioRecorder_handleNewPos_lockRecord(ar); 287 break; 288 289 } 290} 291 292 293//----------------------------------------------------------------------------- 294SLresult android_audioRecorder_create(CAudioRecorder* ar) { 295 SL_LOGV("android_audioRecorder_create(%p) entering", ar); 296 297 const SLDataSource *pAudioSrc = &ar->mDataSource.u.mSource; 298 const SLDataSink *pAudioSnk = &ar->mDataSink.u.mSink; 299 SLresult result = SL_RESULT_SUCCESS; 300 301 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator; 302 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 303 304 // the following platform-independent fields have been initialized in CreateAudioRecorder() 305 // ar->mNumChannels 306 // ar->mSampleRateMilliHz 307 308 if ((SL_DATALOCATOR_IODEVICE == sourceLocatorType) && 309 (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE == sinkLocatorType)) { 310 // microphone to simple buffer queue 311 ar->mAndroidObjType = AUDIORECORDER_FROM_MIC_TO_PCM_BUFFERQUEUE; 312 ar->mAudioRecord = NULL; 313 ar->mRecordSource = android::AUDIO_SOURCE_DEFAULT; 314 } else { 315 result = SL_RESULT_CONTENT_UNSUPPORTED; 316 } 317 318 return result; 319} 320 321 322//----------------------------------------------------------------------------- 323SLresult android_audioRecorder_setConfig(CAudioRecorder* ar, const SLchar *configKey, 324 const void *pConfigValue, SLuint32 valueSize) { 325 326 SLresult result = SL_RESULT_SUCCESS; 327 328 if (NULL == ar) { 329 result = SL_RESULT_INTERNAL_ERROR; 330 } else if (NULL == pConfigValue) { 331 SL_LOGE(ERROR_CONFIG_NULL_PARAM); 332 result = SL_RESULT_PARAMETER_INVALID; 333 334 } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) { 335 336 // recording preset 337 if (KEY_RECORDING_PRESET_PARAMSIZE > valueSize) { 338 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 339 result = SL_RESULT_PARAMETER_INVALID; 340 } else { 341 result = audioRecorder_setPreset(ar, *(SLuint32*)pConfigValue); 342 } 343 344 } else { 345 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 346 result = SL_RESULT_PARAMETER_INVALID; 347 } 348 349 return result; 350} 351 352 353//----------------------------------------------------------------------------- 354SLresult android_audioRecorder_getConfig(CAudioRecorder* ar, const SLchar *configKey, 355 SLuint32* pValueSize, void *pConfigValue) { 356 357 SLresult result = SL_RESULT_SUCCESS; 358 359 if (NULL == ar) { 360 return SL_RESULT_INTERNAL_ERROR; 361 } else if (NULL == pValueSize) { 362 SL_LOGE(ERROR_CONFIG_NULL_PARAM); 363 result = SL_RESULT_PARAMETER_INVALID; 364 365 } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_RECORDING_PRESET) == 0) { 366 367 // recording preset 368 if (KEY_RECORDING_PRESET_PARAMSIZE > *pValueSize) { 369 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 370 result = SL_RESULT_PARAMETER_INVALID; 371 } else { 372 *pValueSize = KEY_RECORDING_PRESET_PARAMSIZE; 373 if (NULL != pConfigValue) { 374 result = audioRecorder_getPreset(ar, (SLuint32*)pConfigValue); 375 } 376 } 377 378 } else { 379 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 380 result = SL_RESULT_PARAMETER_INVALID; 381 } 382 383 return result; 384} 385 386 387//----------------------------------------------------------------------------- 388SLresult android_audioRecorder_realize(CAudioRecorder* ar, SLboolean async) { 389 SL_LOGV("android_audioRecorder_realize(%p) entering", ar); 390 391 SLresult result = SL_RESULT_SUCCESS; 392 393 // initialize platform-independent CAudioRecorder fields 394 if (SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE != ar->mDataSink.mLocator.mLocatorType) { 395 SL_LOGE(ERROR_RECORDER_SINK_MUST_BE_ANDROIDSIMPLEBUFFERQUEUE); 396 return SL_RESULT_CONTENT_UNSUPPORTED; 397 } 398 // the following platform-independent fields have been initialized in CreateAudioRecorder() 399 // ar->mNumChannels 400 // ar->mSampleRateMilliHz 401 402 SL_LOGV("new AudioRecord %u channels, %lu mHz", ar->mNumChannels, ar->mSampleRateMilliHz); 403 404 // initialize platform-specific CAudioRecorder fields 405 ar->mAudioRecord = new android::AudioRecord(); 406 ar->mAudioRecord->set(ar->mRecordSource, // source 407 sles_to_android_sampleRate(ar->mSampleRateMilliHz), // sample rate in Hertz 408 android::AudioSystem::PCM_16_BIT, //FIXME use format from buffer queue sink 409 sles_to_android_channelMaskIn(ar->mNumChannels, 0 /*no channel mask*/), 410 // channel config 411 0, //frameCount min 412 0, // flags 413 audioRecorder_callback,// callback_t 414 (void*)ar, // user, callback data, here the AudioRecorder 415 0, // notificationFrames 416 false); // threadCanCallJava, note: this will prevent direct Java 417 // callbacks, but we don't want them in the recording loop 418 419 if (android::NO_ERROR != ar->mAudioRecord->initCheck()) { 420 SL_LOGE("android_audioRecorder_realize(%p) error creating AudioRecord object", ar); 421 result = SL_RESULT_CONTENT_UNSUPPORTED; 422 } 423 424#ifdef MONITOR_RECORDING 425 gMonitorFp = fopen(MONITOR_TARGET, "w"); 426 if (NULL == gMonitorFp) { SL_LOGE("error opening %s", MONITOR_TARGET); } 427 else { SL_LOGE("recording to %s", MONITOR_TARGET); } // SL_LOGE so it's always displayed 428#endif 429 430 return result; 431} 432 433 434//----------------------------------------------------------------------------- 435void android_audioRecorder_destroy(CAudioRecorder* ar) { 436 SL_LOGV("android_audioRecorder_destroy(%p) entering", ar); 437 438 if (NULL != ar->mAudioRecord) { 439 ar->mAudioRecord->stop(); 440 delete ar->mAudioRecord; 441 ar->mAudioRecord = NULL; 442 } 443 444#ifdef MONITOR_RECORDING 445 if (NULL != gMonitorFp) { 446 fclose(gMonitorFp); 447 gMonitorFp = NULL; 448 } 449#endif 450} 451 452 453//----------------------------------------------------------------------------- 454void android_audioRecorder_setRecordState(CAudioRecorder* ar, SLuint32 state) { 455 SL_LOGV("android_audioRecorder_setRecordState(%p, %lu) entering", ar, state); 456 457 if (NULL == ar->mAudioRecord) { 458 return; 459 } 460 461 switch (state) { 462 case SL_RECORDSTATE_STOPPED: 463 ar->mAudioRecord->stop(); 464 break; 465 case SL_RECORDSTATE_PAUSED: 466 // Note that pausing is treated like stop as this implementation only records to a buffer 467 // queue, so there is no notion of destination being "opened" or "closed" (See description 468 // of SL_RECORDSTATE in specification) 469 ar->mAudioRecord->stop(); 470 break; 471 case SL_RECORDSTATE_RECORDING: 472 ar->mAudioRecord->start(); 473 break; 474 default: 475 break; 476 } 477 478} 479 480 481//----------------------------------------------------------------------------- 482void android_audioRecorder_useEventMask(CAudioRecorder *ar) { 483 IRecord *pRecordItf = &ar->mRecord; 484 SLuint32 eventFlags = pRecordItf->mCallbackEventsMask; 485 486 if (NULL == ar->mAudioRecord) { 487 return; 488 } 489 490 if ((eventFlags & SL_RECORDEVENT_HEADATMARKER) && (pRecordItf->mMarkerPosition != 0)) { 491 ar->mAudioRecord->setMarkerPosition((uint32_t)((((int64_t)pRecordItf->mMarkerPosition 492 * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000)); 493 } else { 494 // clear marker 495 ar->mAudioRecord->setMarkerPosition(0); 496 } 497 498 if (eventFlags & SL_RECORDEVENT_HEADATNEWPOS) { 499 SL_LOGV("pos update period %ld", pRecordItf->mPositionUpdatePeriod); 500 ar->mAudioRecord->setPositionUpdatePeriod( 501 (uint32_t)((((int64_t)pRecordItf->mPositionUpdatePeriod 502 * sles_to_android_sampleRate(ar->mSampleRateMilliHz)))/1000)); 503 } else { 504 // clear periodic update 505 ar->mAudioRecord->setPositionUpdatePeriod(0); 506 } 507 508 if (eventFlags & SL_RECORDEVENT_HEADATLIMIT) { 509 // FIXME support SL_RECORDEVENT_HEADATLIMIT 510 SL_LOGD("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADATLIMIT) on an " 511 "SL_OBJECTID_AUDIORECORDER to be implemented ]"); 512 } 513 514 if (eventFlags & SL_RECORDEVENT_HEADMOVING) { 515 // FIXME support SL_RECORDEVENT_HEADMOVING 516 SL_LOGD("[ FIXME: IRecord_SetCallbackEventsMask(SL_RECORDEVENT_HEADMOVING) on an " 517 "SL_OBJECTID_AUDIORECORDER to be implemented ]"); 518 } 519 520 if (eventFlags & SL_RECORDEVENT_BUFFER_FULL) { 521 // nothing to do for SL_RECORDEVENT_BUFFER_FULL since this will not be encountered on 522 // recording to buffer queues 523 } 524 525 if (eventFlags & SL_RECORDEVENT_HEADSTALLED) { 526 // nothing to do for SL_RECORDEVENT_HEADSTALLED, callback event will be checked against mask 527 // when AudioRecord::EVENT_OVERRUN is encountered 528 529 } 530 531} 532 533 534//----------------------------------------------------------------------------- 535void android_audioRecorder_getPosition(CAudioRecorder *ar, SLmillisecond *pPosMsec) { 536 if ((NULL == ar) || (NULL == ar->mAudioRecord)) { 537 *pPosMsec = 0; 538 } else { 539 uint32_t positionInFrames; 540 ar->mAudioRecord->getPosition(&positionInFrames); 541 if (ar->mSampleRateMilliHz == 0) { 542 *pPosMsec = 0; 543 } else { 544 *pPosMsec = ((int64_t)positionInFrames * 1000) / 545 sles_to_android_sampleRate(ar->mSampleRateMilliHz); 546 } 547 } 548} 549