AudioTrack
音轨创建:
AudioTrack::createTrack
1)AudioSystem::get_audio_flinger
2)AudioSystem::getOutputSamplingRate
3)AudioSystem::getOutputFrameCount
4)AudioSystem::getOutputLatency
5)audioFlinger->createTrack调用audioFlinger的方法
6)sp<IMemory> cblk = track->getCblk();获得buffer,实际是在audioFlinger中申请的
7)mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());获得buffer指针
8)mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
获取缓冲:
AudioTrack::obtainBuffer
1)uint32_t framesAvail = cblk->framesAvailable();buffer中有多少帧数据可用
2)while (framesAvail == 0){}只要没获取成功,就循环着
3)cblk->userBase cblk->frameCount环形buffer起止位置调整
写音频数据
AudioTrack::write
1)obtainBuffer(&audioBuffer, -1)获取一个buffer,不用等待
2)memcpy(audioBuffer.i8, src, toWrite)拷贝数据
3)releaseBuffer(&audioBuffer)释放buffer
获取输出设备
AudioTrack::getOutput
1)AudioSystem::getOutput
总之,audioTrack会调用audioFlinger创建音轨,获得缓冲buffer,会调用audioSystem获取参数
AudioSystem
获取输出设备
AudioSystem::getOutput
1) AudioSystem::gStreamOutputMap.valueFor获取iohandle
2) Aps = AudioSystem::get_audio_policy_service获取不到iohandle,请求aps帮助
3) output = aps->getOutput通过aps帮助获得输出设备
4) AudioSystem::gStreamOutputMap.add保存结果,避免下次还要调用aps
其他的方法类似,都是调用audioPolicyService的方法
audioPolicyService
创建服务
AudioPolicyService::AudioPolicyService
1)mpPolicyManager = createAudioPolicyManager(this)
获取输出设备
AudioPolicyService::getOutput
1)mpPolicyManager->getOutput
其他的方法类似,都是调用AudioPolicyManager的方法
但是AudioPolicyManager没有实现,而是在它父类中实现方法,继承关系:
class AudioPolicyManager: public AudioPolicyManagerBase
AudioPolicyManagerBase
创建服务
AudioPolicyManagerBase::getOutput
1) AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor()
2) mHardwareOutput = mpClientInterface->openOutput需要输入参数是client,不知道是哪里创建的client
3) addOutput
4) setOutputDevice
获取设备
AudioPolicyManagerBase::getOutput
1)output = mHardwareOutput最终得到了output
audioFlinger
audioFlinger是整个音频架构的中间层,起着承上启下的重要作用,audioFlinger.cpp中因此也定义了很多的类以及各自的方法实现
AudioFlinger::openOutput
1)AudioStreamOut *output = mAudioHardware->openOutputStream
2)new DirectOutputThread 或者 new MixerThread
3)thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
AudioFlinger::closeOutput
1)dupThread->removeOutputTrack
2)audioConfigChanged_l
3)thread->exit();
4)mAudioHardware->closeOutputStream
audioFlinger的方法可分成playback和record两部分,流程和函数相似;音效部分的类的构造和函数实现暂时不分析