标签:
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的服务列表中。
标签:
原文地址:http://www.cnblogs.com/kernel-style/p/4729940.html