android audio 架构分析(2) AudioPolicyService 的结构

AudioPolicyService简介

AudioPolicyService是android audio system中两大服务之一,另外一个是AudioFlinger。

主要的功能:

1. 检测和设置输入输出设备的连接状态

2. 音频系统的切换策略抉择

3. 音量和音频系统参数的设置

4. 音效的设置

AudioPolicyService的相关代码

frameworks/av/services/audioflinger/AudioPolicyService.cpp

hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp

hardware/libhardware_legacy/audio/audio_policy_hal.cpp

hardware/libhardware_legacy/audio/AudioPolicyCompatClient.cpp

AudioPolicyService启动

AudioPolicyService是个Service,其启动是在mediaserver中启动的

frameworks/av/media/mediaserver/main_mediaserver.cpp

AudioPolicyService::instantiate();

AudioPolicyService和AudioPolicyManager结构关系

AudioPolicyService中的具体实现实际都是AudioPolicyManager完成的

AudioPolicyService连接AudioPolicyManager

AudioPolicyService中:

struct audio_policy *mpAudioPolicy;   对应的是struct audio_policy policy;(audio_policy_hal.cpp)


audio_policy_hal.cpp中:

audio_policy中所有的Methods最后调用AudioPolicyManager中的方法


AudioPolicyManager连接AudioPolicyService

AudioPolicyManager中:

其并没有直接与AudioPolicyService连接,而是通过AudioPolicyCompatClient连接的

AudioPolicyClientInterface *mpClientInterface;    对应的就是AudioPolicyCompatClient


AudioPolicyCompatClient中:

struct audio_policy_service_ops* mServiceOps;    对应的是AudioPolicyService的Methods
void*                            mService;                            对应的是AudioPolicyService


因此AudioPolicyManager通过AudioPolicyCompatClient调用AudioPolicyService开放的接口

audio_policy_service_ops的结构定义在hardware/libhardware/include/hardware/audio_policy.h

AudioPolicyService的架构实现(如何连上AudioPolicyManager)

AudioPolicyService.cpp

AudioPolicyService::AudioPolicyService()
    : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)

{

    //命令接收线程:

    // START_TONE STOP_TONE SET_VOLUME SET_PARAMETERS SET_VOICE_VOLUME SET_FM_VOLUME

    mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));

    //得到AudioPolicyHardwareModule

    rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);

    rc = audio_policy_dev_open(module, &mpAudioPolicyDev);

    //创建对应的AudioPolicyManager

    rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
                                               &mpAudioPolicy);

}

//创建mpAudioPolicyDev

static inline int audio_policy_dev_open(const hw_module_t* module,
                                    struct audio_policy_device** device)
{
    return module->methods->open(module, AUDIO_POLICY_INTERFACE,
                                 (hw_device_t**)device);
}

audio_policy_hal.cpp

//注册AUDIO_POLICY_HARDWARE_MODULE_ID module

static struct hw_module_methods_t qcom_ap_module_methods = {
        open: qcom_ap_dev_open
};

struct legacy_ap_module HAL_MODULE_INFO_SYM = {
    module: {
        common: {
            tag: HARDWARE_MODULE_TAG,
            version_major: 1,
            version_minor: 0,
            id: AUDIO_POLICY_HARDWARE_MODULE_ID,
            name: "LEGACY Audio Policy HAL",
            author: "The Android Open Source Project",
            methods: &legacy_ap_module_methods,
            dso : NULL,
            reserved : {0},
        },
    },
};

//注册  struct audio_policy_device *mpAudioPolicyDev;

static int qcom_ap_dev_open(const hw_module_t* module, const char* name,
                                    hw_device_t** device)

{

    dev->device.common.tag = HARDWARE_DEVICE_TAG;
    dev->device.common.version = 0;
    dev->device.common.module = const_cast<hw_module_t*>(module);
    dev->device.common.close = qcom_ap_dev_close;
    dev->device.create_audio_policy = create_qcom_ap;
    dev->device.destroy_audio_policy = destroy_qcom_ap;
    *device = &dev->device.common;

}

//创建AudioPolicyManager

static int create_qcom_ap(const struct audio_policy_device *device,
                            struct audio_policy_service_ops *aps_ops,
                            void *service,
                            struct audio_policy **ap)

{

    qap = (struct qcom_audio_policy *)calloc(1, sizeof(*qap));

//注册函数

    qap->policy.set_device_connection_state = ap_set_device_connection_state;
    qap->policy.get_device_connection_state = ap_get_device_connection_state;

.......

    qap->service = service;
    qap->aps_ops = aps_ops;
//创建AudioPolicyCompatClient,并且注册service和aps_ops

    qap->service_client =
        new AudioPolicyCompatClient(aps_ops, service);

//创建AudioPolicyManager

    qap->apm = createAudioPolicyManager(qap->service_client);

//将qap->policy返回给AudioPolicyService

    *ap = &qap->policy;

}

 AudioPolicyManager的架构实现(如何连上AudioPolicyService)

AudioPolicyManagerAlsa.cpp

//创建AudioPolicyManager,并传入参数clientInterface

extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
{
    return new AudioPolicyManager(clientInterface);
}

AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)

{

    mpClientInterface = clientInterface;

}

从上面的分析,mpClientInterface是与AudioPolicyService的接口。

而且其是AudioPolicyCompatClient

    qap->service_client =
        new AudioPolicyCompatClient(aps_ops, service);

AudioPolicyCompatClient.cpp

例如AudioPolicyManager调用AudioPolicyService的setParameters

void AudioPolicyCompatClient::setParameters(audio_io_handle_t ioHandle,
                                            const String8& keyValuePairs,
                                            int delayMs)
{
    mServiceOps->set_parameters(mService, ioHandle, keyValuePairs.string(),
                           delayMs);
}


你可能感兴趣的:(android audio 架构分析(2) AudioPolicyService 的结构)