AudioPolicyManager与AudioPolicyService

APM和APS,你中有我,我中有你的关系。

1.AudioPolicyService
在AudioServer的onFirstRef中产生,类中有一个指针指向AudioPolicyManager,AudioPolicyInterface *mAudioPolicyManager;
mAudioPolicyClient = new AudioPolicyClient(this);
mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);//创建APM实例
看代码:

void AudioPolicyService::onFirstRef()
{
    {
        Mutex::Autolock _l(mLock);

        // start tone playback thread
        mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
        // start audio commands thread
        mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);//上层通过mAudioCommandThread setParameter就是这个thread
        // start output activity command thread
        mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);

        mAudioPolicyClient = new AudioPolicyClient(this);
        mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);//创建APM实例
    }
    // load audio processing modules
    spaudioPolicyEffects = new AudioPolicyEffects();
    {
        Mutex::Autolock _l(mLock);
        mAudioPolicyEffects = audioPolicyEffects;
    }
}

1.创建了2个线程,2.创建AudioPolicyManager并将其被指向的指针保存于自身类中。

AudioPolicyService存在于3个文件中,一个是AudioPolicyService.cpp,另外一个是AudioPolicyClientImpl.cpp中,最后是AudioPolicyInterfaceImpl.cpp。

第一部分

主要以AudioCommandThread为最重要
bool AudioPolicyService::AudioCommandThread::threadLoop()
这个线程里面主要处理如下几个东西

AudioCommandThread::startToneCommand
AudioCommandThread::volumeCommand
AudioCommandThread::parametersCommand
AudioCommandThread::stopOutputCommand
AudioCommandThread::createAudioPatchCommand

这些是音量,setParameter和tone的控制部分。
举个栗子:

int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
                                        float volume,
                                        audio_io_handle_t output,
                                        int delayMs)
{
    return (int)mAudioCommandThread->volumeCommand(stream, volume,
                                                   output, delayMs);
}
status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
                                                               float volume,
                                                               audio_io_handle_t output,
                                                               int delayMs)
{
    sp command = new AudioCommand();
    command->mCommand = SET_VOLUME;
    sp data = new VolumeData();
    data->mStream = stream;
    data->mVolume = volume;
    data->mIO = output;
    command->mParam = data;
    command->mWaitStatus = true;
    ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
            stream, volume, output);
    return sendCommand(command, delayMs);
}
bool AudioPolicyService::AudioCommandThread::threadLoop()
{
                case SET_VOLUME: {
                    VolumeData *data = (VolumeData *)command->mParam.get();
                    ALOGV("AudioCommandThread() processing set volume stream %d, \
                            volume %f, output %d", data->mStream, data->mVolume, data->mIO);
                    command->mStatus = AudioSystem::setStreamVolume(data->mStream,
                                                                    data->mVolume,
                                                                    data->mIO);
                    }break;

}

第二部分

这部分是通过调用AudioFlinger来实现的
举个栗子

audio_module_handle_t AudioPolicyService::AudioPolicyClient::loadHwModule(const char *name)
{
    sp af = AudioSystem::get_audio_flinger();
    if (af == 0) {
        ALOGW("%s: could not get AudioFlinger", __func__);
        return AUDIO_MODULE_HANDLE_NONE;
    }

    return af->loadHwModule(name);
}

看到AudioPolicyClient有没有想起来什么很像的东西,对了,就是前面的mpClientInterface,那么看起来这个是APM调用过来的,经过APS到AudioFlinger

第三部分

AudioPolicyClientImpl.cpp,这里面的是从IAudioPolicyService.cpp继承过来的虚接口,是实现类,也是AudioPolicyService对外提供的接口,这部分通过APS直接调用到APM,AudioSystem通过获取APS的client端的接口可以直接调用到native的APS,然后调用下来,前面说的那些thread就没法调用到,因为没有接口提供给外面,都是内部使用的。

举个栗子

status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
                                               audio_policy_dev_state_t state,
                                               const char *device_address,
                                               const char *device_name)
{
    const sp& aps = AudioSystem::get_audio_policy_service();
    const char *address = "";
    const char *name = "";

    if (aps == 0) return PERMISSION_DENIED;

    if (device_address != NULL) {
        address = device_address;
    }
    if (device_name != NULL) {
        name = device_name;
    }
    return aps->setDeviceConnectionState(device, state, address, name);
}
AudioPolicyInterfaceImpl.cpp中
status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
                                                  audio_policy_dev_state_t state,
                                                  const char *device_address,
                                                  const char *device_name)
{
    Mutex::Autolock _l(mLock);
    return mAudioPolicyManager->setDeviceConnectionState(device, state,
                                                         device_address, device_name);
}
status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
                                                      audio_policy_dev_state_t state,
                                                      const char *device_address,
                                                      const char *device_name)
{![在这里插入图片描述](https://img-blog.csdnimg.cn/20190526230653713.JPG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuZHlZb3VuZzc3,size_16,color_FFFFFF,t_70)
    return setDeviceConnectionStateInt(device, state, device_address, device_name);
}

以上就是APS的总结,顺便把APM也给说了,画一张图看起来比较清楚。

你可能感兴趣的:(Android)