一.AudioRecord录音的初始化设置
AudioRecord::set
->
AudioSystem::getInput
->
调用IAudioPolicyService.cpp文件中的
BpAudioPolicyService的getInput函数
remote()->transact(GET_INPUT, data, &reply);
BnAudioPolicyService::onTransact(
case GET_INPUT:
audio_io_handle_t input = getInput(inputSource,
samplingRate,
format,
channels,
acoustics); // 调用的是AudioPolicyService::getInput函数
->
AudioPolicyService::getInput
->
AudioPolicyManagerBase::getInput
mpClientInterface->openInput
->
AudioPolicyService::openInput
->
BpAudioFlinger的openInput函数
->
AudioFlinger::openInput
mAudioHardware->openInputStream
->
AudioHardwareALSA::openInputStream
err = mALSADevice->open(&(*it), devices, mode(), 0);
in = new AudioStreamInALSA(this, &(*it), acoustics);
err = in->set(format, channels, sampleRate);
->
alsa_default.cpp
调用s_open函数
ALSAStreamOps::set
if (rate && *rate > 0) {
if (mHandle->sampleRate != *rate) {
LOGE("%s L%d mHandle->sampleRate = %d, return BAD_VALUE %d ", __FUNCTION__, __LINE__, mHandle->sampleRate, BAD_VALUE);
return BAD_VALUE;
}
}
E/AudioHardwareALSA( 1181): set L123 mHandle->sampleRate = 8000, return BAD_VALUE -22 // 如果采样率不是8000,则返回BAD_VALUE
E/AudioHardwareALSA( 1181): openInputStream L314 in->set err = -22
// BAD_VALUE的定义是
BAD_VALUE = -EINVAL,
#define EINVAL 3
按照以上定义,BAD_VALUE的值应该是-3,但log输出的值是-22,很奇怪。
----------------------------------------------------------------------------------
二.录音start函数的处理流程
1.如果是录像,则录像中的音频录制的调用顺序是:
MPEG4Writer::start
->
MPEG4Writer::startWriterThread()
->
MPEG4Writer::startTracks
->
MPEG4Writer::Track::start
->
录像时,video track是CameraSource,即调用CameraSource::start函数
audio track是AMRNBEncoder,即调用AMRNBEncoder::start函数
->
2.如果是单纯的录音操作,则直接走下面的路程:
AudioSource::start
->
AudioRecord::start()
->
IAudioRecord::start()
如果返回值为DEAD_OBJECT,则调用
->
AudioRecord::openRecord
->
AudioFlinger::openRecord
->
返回RecordHandle对象
->
AudioFlinger::RecordHandle::start()
->
AudioFlinger::RecordThread::start
->
AudioSystem::startInput(mId);
->
AudioPolicyService::startInput
->
AudioPolicyManagerBase::startInput
如果AudioRecord::openRecord返回NO_ERROR,则继续调用IAudioRecord::start()
如果IAudioRecord::start()返回NO_ERROR,则启动ClientRecordThread线程。
----------------------------------------------------------------------------------
三.AudioRecord开始录音的处理流程
1.frameworks\base\media\java\android\media\AudioRecord.java文件中的
startRecording()函数
// start recording
synchronized(mRecordingStateLock) { // 一直处于同步状态,同一时间只能有一个录音
if (native_start() == SUCCESS) { // 调用native方法
mRecordingState = RECORDSTATE_RECORDING;
}
}
->
2.native_start对应frameworks\base\core\jni\android_media_AudioRecord.cpp文件中的
android_media_AudioRecord_start 函数
取得AudioRecord对象lpRecorder,然后调用lpRecorder->start()的函数,开始录音
->
3.frameworks\base\media\libmedia\AudioRecord.cpp文件的
AudioRecord::start()函数
3.1 取得ClientRecordThread对象t
3.2 调用IAudioRecord对象mAudioRecord->start()
(1)如果返回DEAD_OBJECT,则执行AudioRecord::openRecord函数,调用IAudioFlinger的openRecord函数打开录音设备,然后再执行mAudioRecord->start()开始录音