IBinder的三个主要部分(暂不分析底层如何实现)

来源:互联网 发布:手机本地端口1080 编辑:程序博客网 时间:2024/06/05 00:19
1:定义服务类,如
AudioFlinger.cpp 
MediaPlayerService.cpp
CameraService.cpp
AudioPolicyService.cpp
  实现的过程参考前面的某一篇博客,差不多就是定义一个IXXXX,然后定义两个BnXXX和BpXXX等等

2:在开机流程中,将这些服务注册到servicemanager中去,这个servicemanager有什么用涉及到IBinder的实现机制,暂不管,只要知道它是一个管理IBinder服务的地方,上面三个服务的注册在开机流程mediaserver这个最重要的继承创建后(Main_MediaServer.cpp)
int main(int argc, char** argv)
{
   sp<ProcessState>proc(ProcessState::self());
   sp<IServiceManager> sm =defaultServiceManager();
    LOGI("ServiceManager:%p", sm.get());
    AudioFlinger::instantiate();
   MediaPlayerService::instantiate();
   CameraService::instantiate();
   AudioPolicyService::instantiate();
   ProcessState::self()->startThreadPool();
   IPCThreadState::self()->joinThreadPool();
}
具体每个服务类的instantiate(),大致一致,MediaPlayerService.cpp为例
void MediaPlayerService::instantiate() {
    defaultServiceManager()->addService(
          String16("media.player"), new MediaPlayerService());
}
其他几个只要换个名字就可以了:media.audio_flinger,media.audio_policy,media.camera

3:最后就是客户端需要使用这些服务的时候需要做的事情了:从servicemanager中取出相应的服务类的代理
代码基本一致,和上面一样只要换个名字就可以了
if (gAudioPolicyService.get() == 0) {
       sp<IServiceManager> sm =defaultServiceManager();
       sp<IBinder>binder;
       do {
          binder =sm->getService(String16("media.audio_policy"));
           if (binder!= 0)
              break;
          LOGW("AudioPolicyService not published, waiting...");
          usleep(500000); // 0.5 s
       } while(true);
       if (gAudioPolicyServiceClient == NULL) {
          gAudioPolicyServiceClient = new AudioPolicyServiceClient();
       }
      binder->linkToDeath(gAudioPolicyServiceClient);
       gAudioPolicyService =interface_cast<IAudioPolicyService>(binder);
       gLock.unlock();
    }

if (gAudioFlinger.get() == 0) {
       sp<IServiceManager> sm =defaultServiceManager();
       sp<IBinder>binder;
       do {
          binder =sm->getService(String16("media.audio_flinger"));
           if (binder!= 0)
              break;
          LOGW("AudioFlinger not published, waiting...");
          usleep(500000); // 0.5 s
       } while(true);
       if (gAudioFlingerClient == NULL) {
          gAudioFlingerClient = new AudioFlingerClient();
       } else {
           if(gAudioErrorCallback) {
             gAudioErrorCallback(NO_ERROR);
           }
        }
      binder->linkToDeath(gAudioFlingerClient);
       gAudioFlinger =interface_cast<IAudioFlinger>(binder);
      gAudioFlinger->registerClient(gAudioFlingerClient);
    }

if (sMediaPlayerService.get() == 0) {
       sp<IServiceManager> sm =defaultServiceManager();
       sp<IBinder>binder;
       do {
          binder =sm->getService(String16("media.player"));
           if (binder!= 0) {
              break;
           }
           LOGW("Media player service not published,waiting...");
           usleep(500000); // 0.5 s
       } while(true);

       if (sDeathNotifier == NULL) {
       sDeathNotifier = new DeathNotifier();
    }
   binder->linkToDeath(sDeathNotifier);
    sMediaPlayerService =interface_cast<IMediaPlayerService>(binder);
0 0
原创粉丝点击