声明: 本博客内容均由https://blog.csdn.net/weixin_47702410原创,转载or引用请注明出处,谢谢!
创作不易,如果文章对你有帮助,麻烦点赞 收藏支持~感谢
/*****************************************************************************************************************/
1、init.rc启动到main_audioserver.cpp,这个文件会启动AudioPolicyService
2、AudioPolicyService创建AudioPolicyClient、AudioPolicyManager
int main(int argc __unused, char **argv)
{
...
AudioFlinger::instantiate(); -->AudioFlinger的启动
ALOGE("Mylog_AP:AudioPolicyService::instantiate!!");
AudioPolicyService::instantiate(); -->AudioPolicyService的启动
ALOGE("Mylog_AP:AudioPolicyService::instantiate done!!");
...
}
2、这个AudioPolicyService::instantiate(); 最终会通过BindService调用到AudioPolicyService,其构造函数便会起作用:
/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
AudioPolicyService::AudioPolicyService()
: BnAudioPolicyService(),
mAudioPolicyManager(NULL),
mAudioPolicyClient(NULL),
mPhoneState(AUDIO_MODE_INVALID),
mCaptureStateNotifier(false),
mCreateAudioPolicyManager(createAudioPolicyManager),
mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
Initialize_IC_manu_name_LogLevel("vendor.af.policy.debug");
ALOGE("%s Mylog_AP: AudioPolicyService::AudioPolicyService() done !", __func__);
}
3、注意上面函数是继承了AudioPolicyService::AudioPolicyService(): BnAudioPolicyService(),这个继承关系会导致AudioPolicyService::onFirstRef()的调用
(BnAudioPolicyService继承了BnInterface,而BnInterface又继承了IBinder,IBinder又继承了IInterface,IInterface最终继承了RefBase…)。
/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
void AudioPolicyService::onFirstRef()
{
...
ALOGE("%s Mylog_AP:new AudioPolicyClient !", __func__);
mAudioPolicyClient = new AudioPolicyClient(this);
ALOGE("%s Mylog_AP:new AudioPolicyClient Done!", __func__);
loadAudioPolicyManager();
ALOGE("%s Mylog_AP:mCreateAudioPolicyManager !", __func__);
mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
ALOGE("%s Mylog_AP:mCreateAudioPolicyManager Done!", __func__);
...
}
其先会new一个AudioPolicyClient,然后再调用mCreateAudioPolicyManager:
4、创建AudioPolicyManager:
/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
{
ALOGE("%s Mylog_AP: new AudioPolicyManager !!! !", __func__);
AudioPolicyManager *apm = new AudioPolicyManager(clientInterface, audiopolicymanager_IC_manu_name);
ALOGE("%s Mylog_AP: new AudioPolicyManager done !", __func__);
status_t status = apm->initialize();
ALOGE("%s Mylog_AP: apm->initialize() !", __func__);
...
}
这个CreateAudioPolicyManager函数其实也是new一个AudioPolicyManager(clientInterface, audiopolicymanager_IC_manu_name);
从这开始算是从AudioPolicyService创建出AudioPolicyManager了
下面再分析一下AudioPolicyManager.
5、从上面的代码可以直接看出构造函数又启动了IC_manu_name客制化的AudioPolicyManager,同时也将onFirstRef函数中new AudioPolicyClient作为参数传入,代码如下:
/frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface,
bool /*forTesting*/)
:
mUidCached(AID_AUDIOSERVER), // no need to call getuid(), there's only one of us running.
mpClientInterface(clientInterface),
mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
mA2dpSuspended(false),
mBLESuspended(false),
mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice),
mAudioPortGeneration(1),
mBeaconMuteRefCount(0),
mBeaconPlayingRefCount(0),
mBeaconMuted(false),
mTtsOutputAvailable(false),
mMasterMono(false),
mMusicEffectOutput(AUDIO_IO_HANDLE_NONE)
{
//...
}
AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, AudioPolicyManagerCustomInterface *customInterface)
: AudioPolicyManager(clientInterface, false /*forTesting*/)
{
//...
loadConfig();
}
6、然后就会走到loadConfig,加载策略文件(*.xml)
/frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
void AudioPolicyManager::loadConfig() {
ALOGE("Mylog_AP:AudioPolicyManager::loadConfig() !");
if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) {
ALOGE("could not load audio policy configuration file, setting defaults");
getConfig().setDefault();
}
}
这个函数主要就是将调用deserializeAudioPolicyXmlConfig加载并解析一些策略文件(例如"audio_policy_configuration.xml"),这个文件是如何加载,我们后面再分析。
代码加入上面的log, 编译烧录镜像,抓取开机log:
行 1996: 07-26 11:18:03.254630 707 707 E audioserver: Mylog_AP:AudioPolicyService::instantiate!!
→ /frameworks/av/media/audioserver/main_audioserver.cpp
开始进行AudioPolicyService的实例化
行 1997: 07-26 11:18:03.254764 707 707 E AudioPolicyService: AudioPolicyService Mylog_AP: AudioPolicyService::AudioPolicyService() done !
→ /frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
AudioPolicyService构造函数的执行
行 1998: 07-26 11:18:03.255591 707 707 E AudioPolicyService: onFirstRef Mylog_AP:new AudioPolicyClient !
→ /frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
AudioPolicyService 构造函数继承了BnAudioPolicyService,间接调用到AudioPolicyService: onFirstRef,此处new AudioPolicyClient
行 1999: 07-26 11:18:03.255644 707 707 E AudioPolicyService: onFirstRef Mylog_AP:new AudioPolicyClient Done!
→ /frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
new AudioPolicyClient done
行 2000: 07-26 11:18:03.256189 707 707 E AudioPolicyService: onFirstRef Mylog_AP:mCreateAudioPolicyManager !
→ /frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
AudioPolicyService: onFirstRef还会创建AudioPolicyManager
行 2001: 07-26 11:18:03.256387 707 707 E AudioPolicyService: createAudioPolicyManager Mylog_AP: new AudioPolicyManager !!! !
→ /frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
CreateAudioPolicyManager函数会new一个AudioPolicyManager
行 2002: 07-26 11:18:03.256507 707 707 E APM_AudioPolicyManager: Mylog_AP:AudioPolicyManager::loadConfig() !
→ /frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
AudioPolicyManager的构造函数会加载配置
行 2223: 07-26 11:18:03.378245 707 707 E AudioPolicyService: createAudioPolicyManager Mylog_AP: new AudioPolicyManager done !
→ /frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
创建完成AudioPolicyManager
行 3628: 07-26 11:18:06.125142 707 707 E AudioPolicyService: createAudioPolicyManager Mylog_AP: apm->initialize() !
→ /frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
AudioPolicyManager初始化
行 3629: 07-26 11:18:06.125199 707 707 E AudioPolicyService: onFirstRef Mylog_AP:mCreateAudioPolicyManager Done!
→ /frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
onFirstRef 函数创建AudioPolicyManager完成
行 3796: 07-26 11:18:06.616405 707 707 E audioserver: Mylog_AP:AudioPolicyService::instantiate done!!
→ /frameworks/av/media/audioserver/main_audioserver.cpp中
AudioPolicyService的实例化完成