码迷,mamicode.com
首页 > 移动开发 > 详细

初窥Android AudioFlinger

时间:2015-08-14 15:35:51      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

         Android  AudioFlinger 是Android音频系统的两大服务之一,另一个服务是AudioPolicyService,这两大服务都在系统启动时有MediaSever加载,加载的代码位于:frameworks/base/media/mediaserver/main_mediaserver.cpp。

int main(int argc, char** argv)
{
    signal(SIGPIPE, SIG_IGN);
    char value[PROPERTY_VALUE_MAX];
    ..........
        sp<ProcessState> proc(ProcessState::self());
        sp<IServiceManager> sm = defaultServiceManager();
        ALOGI("ServiceManager: %p", sm.get());
        AudioFlinger::instantiate();
        MediaPlayerService::instantiate();
        CameraService::instantiate();
        AudioPolicyService::instantiate();
        registerExtensions();
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
    }
}

这里只说下 AudioFlinger::instantiate();的实现。

instantiate()函数调用位于:framework/base/native/include/binder.h

template<typename SERVICE>
class BinderService
{
public:
    static status_t publish(bool allowIsolated = false) {
        sp<IServiceManager> sm(defaultServiceManager());
        return sm->addService(
                String16(SERVICE::getServiceName()),
                new SERVICE(), allowIsolated);
    }

    ........
    static void instantiate() { publish(); }
    ........
}

刚看到此段源码时候有点疑惑,为什么是调用BinderService类的instantiate()方法的?后来找了AudioFlinger的类定义,发现是继承关系。

class AudioFlinger :
    public BinderService<AudioFlinger>,
    public BnAudioFlinger
{
    friend class BinderService<AudioFlinger>;   // for AudioFlinger()
public:
    static const char* getServiceName() ANDROID_API { return "media.audio_flinger"; }

    virtual     status_t    dump(int fd, const Vector<String16>& args);
    ........
}

step 1. BinderService 设计为模版类,并由AudioFlinger继承。回头看下BinderService 的static status_t publish(bool allowIsolated = false)方法。

sm->addService(
                String16(SERVICE::getServiceName()),
                new SERVICE(), allowIsolated);
BinderService<AudioFlinger> 兑换后
sm->addService(
                String16(AudioFlinger::getServiceName()),
                new AudioFlinger(), allowIsolated);
因此 AudioFlinger::instantiate(); 可以总结为:
setp 1 : 调用父类BinderService<AudioFlinger>的instantiate()函数。
setp 2 : 调用父类BinderService<AudioFlinger>的publish()函数,真正的创建了AudioFlinger实例,并添加到servicemanager的服务列表中。
 

初窥Android AudioFlinger

标签:

原文地址:http://www.cnblogs.com/kernel-style/p/4729940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!