黑暗拍卖会第七卷:Android Camera Framework Stream(三)

来源:百度文库 编辑:九乡新闻网 时间:2024/04/26 20:39:41
7.     那么现在的关键就是 Client 类了·进一步跟进: CameraService::Client::Client(const sp& cameraService,         const sp& cameraClient, pid_t clientPid) {          …..          mCameraService = cameraService;     mCameraClient = cameraClient;     mClientPid = clientPid;     mHardware = openCameraHardware(); } 将 cameraService 和 cameraClient 的实例分别赋值给了 Client 的类成员变量。 另外 openCameraHardware() 是值得注意的地方,也就是连接上层应用和底层驱动的关键,通过调用 openCameraHardware() 得到了一个 CameraHardwareInterface 实例对象,并赋值给自己的类成员: `       sp mHardware;          对 hardware 的操作就是通过该对象完成的,所以说真正意义上的功能实现其实就是在这里,即 client 类的函数接口调用。          对于 hardware 的东东咱们暂时不去关注吧。          那么我们再次仔细研究下 Client 类的继承关系 ( 这些继承关系很容易混乱,涉及到较多的多态类型转换 ) ,这个其实往往都很关键:                                                                                                                                                         Client 继承于 BnCamera ,而 BnCamera 则继承于 ICamera ,也就是说 Client 继承了 ICamera, 实现了 ICamera 中的函数。          进而发现,原来绕一个大圈,把最开始的图简化下:                                                                                                        
 8.     除此之外还有两个步骤或许需要去研究下: 先从单一函数去跟进,看具体一些 callback 的实现流程: // callback from camera service void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) {     sp listener;     {         Mutex::Autolock _l(mLock);         listener = mListener;     }     if (listener != NULL) {         listener->notify(msgType, ext1, ext2);     } } 这是 Camera 类中一个 callback 函数实现,但其本质在哪?先看 camera 类的继承关系:  

  通过以上的继承关系,继续跟进其父类 ICameraClient : class ICameraClient: public IInterface { public:     DECLARE_META_INTERFACE(CameraClient);       virtual void   notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) = 0;     virtual void  dataCallback(int32_t msgType, const sp& data) = 0;     virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const      sp& data) = 0; }; 其中 notifyCallback() 又是纯虚函数 , 则同样说明实现在其子类 BpCameraClient 中:     // generic callback from camera service to app     void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)     {         LOGV("notifyCallback");         Parcel data, reply;         data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());         data.writeInt32(msgType);         data.writeInt32(ext1);         data.writeInt32(ext2);     remote()->transact(NOTIFY_CALLBACK,data, &reply, IBinder::FLAG_ONEWAY);     } 然后通过 Binder 通讯调用到 BnCameraClient 中实现: status_t BnCameraClient::onTransact(     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {     switch(code) {         case NOTIFY_CALLBACK: {             LOGV("NOTIFY_CALLBACK");             CHECK_INTERFACE(ICameraClient, data, reply);             int32_t msgType = data.readInt32();             int32_t ext1 = data.readInt32();             int32_t ext2 = data.readInt32();             notifyCallback(msgType, ext1, ext2);             return NO_ERROR;         } break;                    …. } 进而调用到了 Camera.cpp 中的函数实现了,但或许你有疑问,这些 callback 是涉及到一些驱动的 callback ,哪怎么跟驱动联系起来那?          结合之前对 hardware 接口调用的类 Client ,进一步可以发现 callback 的处理同样是在 Client 类实例化的时候: CameraService::Client::Client(const sp& cameraService,         const sp& cameraClient, pid_t clientPid) {          …..          mHardware->setCallbacks( notifyCallback ,                                dataCallback,                                dataCallbackTimestamp,                                mCameraService.get()); ….. }          调用了 mHardware 将 callback 传入,但此处的 notifyCallback 并不是 camera.cpp 中的函数,而是 client 类的 notifyCallback 函数。          再继续看 client 类中的 notifyCallback 函数实现: void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,int32_t ext2, void* user) {          …..         default:             sp c = client->mCameraClient;             if (c != NULL) {                 c->notifyCallback(msgType, ext1, ext2);             }             break;          …..     }          通过得到 ICameraClient 实例进而调用到了具体的对象 Camera 的 notifyCallback() 函数。这个地方估计会遇见跟 ICameraService 函数调用一样的问题, ICameraClient 函数调用所需要的函数实例在哪?          记得上述 ICameraService 讲到的 connect() 函数嘛?其中有一个参数不能被忽略掉的,就是 ICameraClient ,但它在真正传入的时候却是一个 ICameraClient 子类 camera 的实例对象。          CameraService:          sp CameraService::connect(const sp& cameraClient)            {                    …..          // create a new Client object              client = new Client(this, cameraClient , callingPid);                    ….. }                   Client: CameraService::Client::Client(const sp& cameraService,         const sp& cameraClient , pid_t clientPid) {      ….     mCameraService = cameraService;     mCameraClient = cameraClient ;     …. } 这样就清楚了,其实 Client 在调用设置 callback 的调用最终还是调用到了 camera.cpp 中的 callback 函数,进而将具体内容通过 callback 反馈给上层应用做出相应的处理。