| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> C++知识库 -> CameraBase基类和Camera类介绍 -> 正文阅读 |
|
[C++知识库]CameraBase基类和Camera类介绍 |
这个类的定义如下: template <typename TCam, typename TCamTraits = CameraTraits<TCam> > class CameraBase : public IBinder::DeathRecipient { public: ??? typedef typename TCamTraits::TCamListener?????? TCamListener; ??? typedef typename TCamTraits::TCamUser?????????? TCamUser; ??? typedef typename TCamTraits::TCamCallbacks????? TCamCallbacks; ??? typedef typename TCamTraits::TCamConnectService TCamConnectService; ??? static sp<TCam>????? connect(int cameraId, ???????????????????????????????? const String16& clientPackageName, ???????????????????????????????? int clientUid, int clientPid); ??? virtual void???????? disconnect(); ??? void???????????????? setListener(const sp<TCamListener>& listener); ??? static int?????????? getNumberOfCameras(); ??? static status_t????? getCameraInfo(int cameraId, ?????????????????????????????????????? /*out*/ ??????????????????????????????? ???????struct hardware::CameraInfo* cameraInfo); ??? sp<TCamUser>???????? remote(); ??? // Status is set to 'UNKNOWN_ERROR' after successful (re)connection ??? status_t???????????? getStatus(); protected: ??? CameraBase(int cameraId); ??? virtual?????? ???????~CameraBase(); ??? ??? // TCamCallbacks implementation ??? ??? virtual void???????? notifyCallback(int32_t msgType, int32_t ext, ???? ???????????????????????????????????int32_t ext2); ??? ??? // Common instance variables ??? ??? Mutex??????????????????????????? mLock; ??? virtual void???????????????????? binderDied(const wp<IBinder>& who); ??? // helper function to obtain camera service handle ??? static const sp<::android::hardware::ICameraService> getCameraService(); ??? sp<TCamUser>???????????????????? mCamera; ??? status_t???????????????????????? mStatus; ??? sp<TCamListener>???????????????? mListener; ??? const int??????????????????????? mCameraId; ??? typedef CameraBase<TCam>???????? CameraBaseT; }; 从定义来看,这运用了模板,并使用了我们前一章定义的萃取器并设置TCamTraits = CameraTraits<TCam>,这里来获取camera特性。同时为了避免编译错误我们通知?? ?typedef typename TCamTraits::TCamListener?????? TCamListener; ??? typedef typename TCamTraits::TCamUser?????????? TCamUser; ??? typedef typename TCamTraits::TCamCallbacks????? TCamCallbacks; ??? typedef typename TCamTraits::TCamConnectService TCamConnectService; 这些传过来的类型,而不是变量,便于后续的使用。后续当我们使用这个基类的时候由于未指定Tcam,后续需要指定Tcam,类似于CameraBase<TCam>. 这个类继承public IBinder::DeathRecipient通过virtual void? binderDied(const wp<IBinder>& who);用来监听binder绑定的进程是否退出。 ??? class DeathRecipient : public virtual RefBase ??? { ??? public: ??????? virtual void binderDied(const wp<IBinder>& who) = 0; }; 这里虚函数的实现是在Z:\SA800U_Android9.0_R01_r220\frameworks\av\camera\CameraBase.cpp 内容如下: template <typename TCam, typename TCamTraits> void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) { ??? ALOGW("mediaserver's remote binder Camera object died"); ??? notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0); }
先上定义代码如下: Z:\SA800U_Android9.0_R01_r220\frameworks\av\include\camera\Camera.h class Camera : ??? public CameraBase<Camera>, ??? public ::android::hardware::BnCameraClient { public: ??? enum { ??????? USE_CALLING_UID = ::android::hardware::ICameraService::USE_CALLING_UID ??? }; ??? enum { ??????? USE_CALLING_PID = ::android::hardware::ICameraService::USE_CALLING_PID ??? }; ??????????? // construct a camera client from an existing remote ??? static? sp<Camera>? create(const sp<::android::hardware::ICamera>& camera); ??? static? sp<Camera>? connect(int cameraId, ??????????????????????????????? const String16& clientPackageName, ??????????????????????????????? int clientUid, int clientPid); ??? static? status_t? connectLegacy(int cameraId, int halVersion, ???????????????????????????????????? const String16& clientPackageName, ???????????????????????????????????? int clientUid, sp<Camera>& camera); ??????????? virtual???? ~Camera(); ??????????? status_t??? reconnect(); ??????????? status_t??? lock(); ??? ????????status_t??? unlock(); ??????????? // pass the buffered IGraphicBufferProducer to the camera service ??????????? status_t??? setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer); ??????????? // start preview mode, must call setPreviewTarget first ??????????? status_t??? startPreview(); ??????????? // stop preview mode ??????????? void??????? stopPreview(); ??????????? // get preview state ??????????? bool??????? previewEnabled(); ??????????? // start recording mode, must call setPreviewTarget first ??????????? status_t??? startRecording(); ??????????? // stop recording mode ??????????? void??????? stopRecording(); ??????????? // get recording state ??????????? bool??????? recordingEnabled(); ??????????? // release a recording frame ??????????? void??????? releaseRecordingFrame(const sp<IMemory>& mem); ??????????? // release a recording frame handle ??????????? void??????? releaseRecordingFrameHandle(native_handle_t *handle); ??????????? // release a batch of recording frame handles ??????????? void??????? releaseRecordingFrameHandleBatch( ??????????????????? const std::vector<native_handle_t*> handles); ??????????? // autoFocus - status returned from callback ??????????? status_t??? autoFocus(); ??????????? // cancel auto focus ??????????? status_t??? cancelAutoFocus(); ??????????? // take a picture - picture returned from callback ??????????? status_t??? takePicture(int msgType); ??????????? // set preview/capture parameters - key/value pairs ??????????? status_t??? setParameters(const String8& params); ??????????? // get preview/capture parameters - key/value pairs ??????????? String8???? getParameters() const; ??????????? // send command to camera driver ??????????? status_t??? sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); ??????????? // Tell camera how to pass video buffers. videoBufferMode is one of VIDEO_BUFFER_MODE_*. ??????????? // Returns OK if the specified video buffer mode is supported. If videoBufferMode is ??????????? // VIDEO_BUFFER_MODE_BUFFER_QUEUE, setVideoTarget() must be called before starting ??????????? // video recording. ??????????? status_t??? setVideoBufferMode(int32_t videoBufferMode); ??????????? // Set the video buffer producer for camera to use in VIDEO_BUFFER_MODE_BUFFER_QUEUE ????????? ??// mode. ??????????? status_t??? setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer); ??????????? void??????? setListener(const sp<CameraListener>& listener); ??????????? void??????? setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener); ??????????? // Configure preview callbacks to app. Only one of the older ??????????? // callbacks or the callback surface can be active at the same time; ??????????? // enabling one will disable the other if active. Flags can be ???? ???????// disabled by calling it with CAMERA_FRAME_CALLBACK_FLAG_NOOP, and ??????????? // Target by calling it with a NULL interface. ??????????? void??????? setPreviewCallbackFlags(int preview_callback_flag); ??????????? status_t??? setPreviewCallbackTarget( ??????????????????? const sp<IGraphicBufferProducer>& callbackProducer); ??????????? sp<ICameraRecordingProxy> getRecordingProxy(); ??? // ICameraClient interface ??? virtual void??????? notifyCallback(int32_t msgType, int32_t ext, int32_t ext2); ?? ?virtual void??????? dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, ???????????????????????????????????? camera_frame_metadata_t *metadata); ??? virtual void??????? dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); ??? virtual void??????? recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle); ??? virtual void??????? recordingFrameHandleCallbackTimestampBatch( ??????????????????????????????? const std::vector<nsecs_t>& timestamps, ? ??????????????????????????????const std::vector<native_handle_t*>& handles); ??? class RecordingProxy : public BnCameraRecordingProxy ??? { ??? public: ??????? explicit RecordingProxy(const sp<Camera>& camera); ??????? // ICameraRecordingProxy interface ??????? virtual status_t startRecording(const sp<ICameraRecordingProxyListener>& listener); ??????? virtual void stopRecording(); ??????? virtual void releaseRecordingFrame(const sp<IMemory>& mem); ??????? virtual void releaseRecordingFrameHandle(native_handle_t* handle); ??????? virtual void releaseRecordingFrameHandleBatch( ??????????????? const std::vector<native_handle_t*>& handles); ??? private: ??????? sp<Camera>???????? mCamera; ??? }; protected: ??? explicit??????????? Camera(int cameraId); ???? ???????????????????Camera(const Camera&); ??????????????????????? Camera& operator=(const Camera); ??? sp<ICameraRecordingProxyListener>? mRecordingProxyListener; ??? friend class??????? CameraBase; }; 可以看到这个类继承两个父类,其中CameraBase已经讲过了不必多述, 接下来讲一下另一个父类BnCameraClient。
这个类是用来进行binder通信的,最终我们Camera是作为客户端需要调用binder服务端CameraService执行我们需要的动作,这里牵扯到跨进程通信,Camera Service在安卓启动过程中就会被系统拉起来等待客户端的调用。 首先看下这个类的定义: Z:\SA800U_Android9.0_R01_r220\frameworks\av\include\camera\android\hardware\ICameraClient.h class BnCameraClient: public android::BnInterface<ICameraClient> { public: ??? virtual status_t??? onTransact( uint32_t code, ??????????????????????????????????? const Parcel& data, ??????????????????????? ????????????Parcel* reply, ??????????????????????????????????? uint32_t flags = 0); }; 可以看到这个类又继承自BnInterface并传入了IcameraClient作为其模板类型,我们先来看看这个BnInterface的定义 template<typename INTERFACE> class BnInterface : public INTERFACE, public Bbinder { public: ??? virtual sp<IInterface>????? queryLocalInterface(const String16& _descriptor); ??? virtual const String16&???? getInterfaceDescriptor() const; protected: ??? virtual IBinder*??????????? onAsBinder(); }; 可以看到这个类继承自INTERFACE模板参数以及继承自Bbinder,那么返回去也会就是BnCameraClient同样会继承IcameraClient,BnInterface为一个通用模板类,安卓framework native中很多服务都用到这个类,这个主要是用来进行binder通信。其中BBinder启动重要作用。 class BBinder : public Ibinder 这个类继承自Ibinder. 这部分涉及到安卓native的binder跨进程通信。 我们回到IcameraClient它的定义跟BnCameraClient在同一个文件中,定义一些CameraClient的一些功能,BnCameraClient作为跨进程服务者存在,所以它只定义一个onTransact函数。 这里使用虚函数来定义,给子类实现多态性。 这样我们大体的类关系可以梳理一下如下图: ? ? |
|
C++知识库 最新文章 |
【C++】友元、嵌套类、异常、RTTI、类型转换 |
通讯录的思路与实现(C语言) |
C++PrimerPlus 第七章 函数-C++的编程模块( |
Problem C: 算法9-9~9-12:平衡二叉树的基本 |
MSVC C++ UTF-8编程 |
C++进阶 多态原理 |
简单string类c++实现 |
我的年度总结 |
【C语言】以深厚地基筑伟岸高楼-基础篇(六 |
c语言常见错误合集 |
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/24 4:40:22- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |