【AudioPolicy To AudioHAL笔记(一)】AudioPolicy启动过程

AudioPolicy启动过程

/*****************************************************************************************************************/

声明: 本博客内容均由https://blog.csdn.net/weixin_47702410原创,转载or引用请注明出处,谢谢!

创作不易,如果文章对你有帮助,麻烦点赞 收藏支持~感谢

/*****************************************************************************************************************/

一、总体启动过程

1、init.rc启动到main_audioserver.cpp,这个文件会启动AudioPolicyService

2、AudioPolicyService创建AudioPolicyClient、AudioPolicyManager

二、代码分析

  • 1、在/frameworks/av/media/audioserver/main_audioserver.cpp中会进行启动AudioServer的进程。
c 复制代码
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,其构造函数便会起作用:

c 复制代码
/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...)。

c 复制代码
/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:

c 复制代码
/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作为参数传入,代码如下:

c 复制代码
/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)

c 复制代码
/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的实例化完成
相关推荐
oushaojun211 分钟前
win10中使用ffmpeg和MediaMTX 推流rtsp视频
ffmpeg·rtsp·mediamtx
萧大侠jdeps11 分钟前
视频 wmv 转MP4
音视频
gomogomono13 分钟前
HDR视频技术之三:色度学与颜色空间
音视频·hdr
萧大侠jdeps13 分钟前
图片生成视频-右进
前端·javascript·音视频
daily_233315 分钟前
数据结构——小小二叉树第三幕(链式结构的小拓展,二叉树的创建,深入理解二叉树的遍历)超详细!!!
数据结构·c++·算法
gomogomono15 分钟前
HDR视频技术之四:HDR 主要标准
音视频·hdr
laimaxgg39 分钟前
C++特殊类设计(不能被拷贝的类、只能在堆上创建对象的类、不能被继承的类、单例模式)
c++·单例模式
SUN_Gyq1 小时前
什么是 C++ 中的模板特化和偏特化? 如何进行模板特化和偏特化?
开发语言·c++·算法
愿天垂怜1 小时前
【C++】C++11引入的新特性(1)
java·c语言·数据结构·c++·算法·rust·哈希算法
垂杨有暮鸦⊙_⊙1 小时前
阅读《先进引信技术的发展与展望》识别和控制部分_笔记
笔记·学习