Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116707
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2014-05-13 14:37:19

<深入理解android>卷1 邓凡平著 第六章笔记
mediaserver的目录
/work/ct/android42/frameworks/av/media/mediaserver/main_mediaserver.cpp
  1. int main(int argc, char** argv)
  2. {
  3.     signal(SIGPIPE, SIG_IGN);
  4.     sp<ProcessState> proc(ProcessState::self());         //1.打开设备结点
  5.     //2.创建了两个对象,BpBinder与BpServiceManager,并让BpServiceManager的remote指向BpBinder
  6.     sp<IServiceManager> sm = defaultServiceManager();    
  7.     AudioFlinger::instantiate();
  8.     MediaPlayerService::instantiate();                  //3.
  9.     CameraService::instantiate();
  10.     AudioPolicyService::instantiate();
  11.     ProcessState::self()->startThreadPool();
  12.     IPCThreadState::self()->joinThreadPool();
  13. }
1. 打开设备结点
/work/ct/android42/frameworks/native/libs/binder/ProcessState.cpp
  1. sp<ProcessState> ProcessState::self()
  2. {
  3.     Mutex::Autolock _l(gProcessMutex);
  4.     if (gProcess != NULL) {
  5.         return gProcess;
  6.     }
  7.     gProcess = new ProcessState;     //第一次就new一个
  8.     return gProcess;
  9. }
new一个ProcessState,并保存/dev/binder结点的fd到mDriverFD中
  1. ProcessState::ProcessState()
  2.     : mDriverFD(open_driver())   //打开设备结点,并设置最大连接数为15
  3.     , mVMStart(MAP_FAILED)
  4.     , mManagesContexts(false)
  5.     , mBinderContextCheckFunc(NULL)
  6.     , mBinderContextUserData(NULL)
  7.     , mThreadPoolStarted(false)
  8.     , mThreadPoolSeq(1)
  9. {
  10.     mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
  11. }

打开设备结点,并设转置最大连接数为15
  1. static int open_driver()
  2. {
  3.     int fd = open("/dev/binder", O_RDWR);         //打开设备文件/dev/binder
  4.     fcntl(fd, F_SETFD, FD_CLOEXEC);               //子进程关闭无用的fd,禁止exec 
  5.     result = ioctl(fd, BINDER_VERSION, &vers);    //获取binder的version,并检查
  6.     size_t maxThreads = 15;
  7.     ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); //设置线程池中最多线程数为15
  8.     return fd;
  9. }
2.创建了两个对象,BpBinder与BpServiceManager
/work/ct/android42/frameworks/native/libs/binder/IServiceManager.cpp
  1. sp<IServiceManager> defaultServiceManager()
  2. {
  3.     //不是第一次返回全局的IServiceManager
  4.     if (gDefaultServiceManager != NULL)
  5.         return gDefaultServiceManager;
  6.     //第一次就new一个
  7.     AutoMutex _l(gDefaultServiceManagerLock);
  8.     if (gDefaultServiceManager == NULL) {
  9.         //下面这一句创建了两个对象,BpBinder与BpServiceManager,
  10.         //并让BpServiceManager的remote指向BpBinder
  11.         gDefaultServiceManager = interface_cast<IServiceManager>(ProcessState::self()->getContextObject(NULL));   //2.1 - 2.2
  12.     return gDefaultServiceManager;
  13. }
2.1 创建BpBinder对象
./frameworks/native/libs/binder/ProcessState.cpp
  1. sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& caller)
  2. {
  3.     return getStrongProxyForHandle(0);
  4. }
./frameworks/native/libs/binder/ProcessState.cpp
  1. sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
  2. {
  3.     sp<IBinder> result;

  4.     AutoMutex _l(mLock);

  5.     handle_entry* e = lookupHandleLocked(handle);

  6.     if (e != NULL) {
  7.         IBinder* b = e->binder;
  8.         if (b == NULL || !e->refs->attemptIncWeak(this)) {
  9.             b = new BpBinder(handle);
  10.             e->binder = b;
  11.             if (b) e->refs = b->getWeakRefs();
  12.             result = b;
  13.         } else {
  14.             result.force_set(b);
  15.             e->refs->decWeak(this);
  16.         }
  17.     }

  18.     return result;
  19. }
./frameworks/native/libs/binder/BpBinder.cpp
  1. BpBinder::BpBinder(int32_t handle)
  2.     : mHandle(handle)
  3.     , mAlive(1)
  4.     , mObitsSent(0)
  5.     , mObituaries(NULL)
  6. {
  7.     extendObjectLifetime(OBJECT_LIFETIME_WEAK);
  8.     IPCThreadState::self()->incWeakHandle(handle);
  9. }
2.2 创建BpServiceManager并用remote指向BpBinder
知道ProcessState::self()->getContextObject(NULL)的作用之后,下述就可以重新定义为:
gDefaultServiceManager = interface_cast(ProcessState::self()->getContextObject(NULL));
即: gDefaultServiceManager = interface_cast(new BpBinder(0));

再看一下 interface_cast
  1. template<typename INTERFACE>
  2. inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
  3. {
  4.     return INTERFACE::asInterface(obj);
  5. }
对应到IServiceManager就是
  1. inline sp<IServiceManager> interface_cast(const sp<IBinder>& obj)
  2. {
  3.     return IServiceManager::asInterface(obj);
  4. }
其中 IServiceManager的asInterface函数是通过DECLARE_META_INTERFACE宏定义的,
其实现是./frameworks/native/libs/binder/IServiceManager.cpp中的宏IMPLEMENT_META_INTERFACE
  1. android::sp<IServiceManager> IServiceManager::asInterface(const android::sp<android::IBinder>&obj)
  2.     {
  3.         android::sp<IServiceManager> intr;
  4.         if (obj != NULL) {
  5.             intr = static_cast<IServiceManager*>(obj->queryLocalInterface(IServiceManager::descriptor).get());
  6.             if (intr == NULL) { 
  7.                 //重新创建了一个BpServiceManager对象,并且remote指向了上述2.1中new的BpBiner对象
  8.                 intr = new BpServiceManager(obj);
  9.             }
  10.         }
  11.         return intr;
  12.     }
3. addServer
在./frameworks/av/media/libmediaplayerservice/MediaPlayerService.cpp
  1. void MediaPlayerService::instantiate() {
  2.     defaultServiceManager()->addService(String16("media.player"), new MediaPlayerService());
  3. }
defaultServiceManager()是一个BpServiceManager,所以调用BpServiceManager->addService

MediaPlayerService
::initantiate
    --> BpServiceManager::addService
在./frameworks/native/libs/binder/IServiceManager.cpp中
  1. virtual status_t addService(const String16& name, const sp<IBinder>& service,bool allowIsolated)
  2. {
  3.     //构造数据包data,结果的状态放在reply中
  4.     Parcel data, reply;
  5.     data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
  6.     data.writeString16(name);
  7.     data.writeStrongBinder(service);
  8.     data.writeInt32(allowIsolated ? 1 : 0);
  9.     //remote指向BpBinder,所以就是调用BpBinder的transact
  10.     status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
  11.     return err == NO_ERROR ? reply.readExceptionCode() : err;
  12. }
因为BpServiceManager的remote指向了BpBinder,所以remote()->transact就是BpBinder->transact();
MediaPlayerService::initantiate
    --> BpServiceManager::addService
        --> BpBinder::transact
在./frameworks/native/libs/binder/BpBinder.cpp中
  1. status_t BpBinder::transact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  2. {
  3.     if (mAlive) {
  4.         //BpBinder的transact没有干活,只是简单的把事情交给了IPCThreadState
  5.         status_t status = IPCThreadState::self()->transact(mHandle, code, data, reply, flags);
  6.         if (status == DEAD_OBJECT) mAlive = 0;
  7.         return status;
  8.     }

  9.     return DEAD_OBJECT;
  10. }
MediaPlayerService::initantiate
    --> BpServiceManager::addService
        --> BpBinder::transact
            --> IPCThreadState::transact
        --> BpBinder::transact
在./frameworks/native/libs/binder/IPCThreadState.cpp中
  1. status_t IPCThreadState::transact(int32_t handle,uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags)
  2. {
  3.    writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);    //将数据写到mOut中去,在下一步waitForResponse中正式发送
  4.    waitForResponse(reply);     //真正的发送数据到/dev/binder并接收从/dev/binder中反馈的数据
  5.    return err;
  6. }
MediaPlayerService::initantiate
    --> BpServiceManager::addService
        --> BpBinder::transact
            --> IPCThreadState::transact
                --> waitForResponse
在./frameworks/native/libs/binder/IPCThreadState.cpp中
  1. status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
  2. {
  3.     while (1) {
  4.         talkWithDriver();          //与驱动进行通信并发送数据,利用ioctl
  5.         cmd = mIn.readInt32();     //读取从驱动中反馈的信息,并作处理
  6.         switch (cmd) {
  7.         case BR_TRANSACTION_COMPLETE:
  8.         case BR_DEAD_REPLY:
  9.         case BR_FAILED_REPLY:
  10.         case BR_ACQUIRE_RESULT:
  11.         case BR_REPLY:
  12.         default:
  13.             executeCommand(cmd);
  14. }



阅读(801) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~