Android camera open 流程

open 过程涉及到架构中的三个层次:App 层,层,层 。
其中,App 层直接调用层所封装的方法,而层需要通过远程调用中的函数 。

Android camera open 流程

文章插图
()
从连接到APP
mCameraManager.openCamera(currentCameraId, stateCallback, backgroundHandler);
//base/core/java////.java
最初的入口就是的方法,但它也是仅仅是调用了方法,最终主要调用了Async方法
459/**460* Helper for opening a connection to a camera with the given ID.461*462* @param cameraId The unique identifier of the camera device to open463* @param callback The callback for the camera. Must not be null.464* @param executor The executor to invoke the callback with. Must not be null.465* @param uidThe UID of the application actually opening the camera.466*Must be USE_CALLING_UID unless the caller is a service467*that is trusted to open the device on behalf of an468*application and to forward the real UID.469*470* @throws CameraAccessException if the camera is disabled by device policy,471* too many camera devices are already open, or the cameraId does not match472* any currently available camera device.473*474* @throws SecurityException if the application does not have permission to475* access the camera476* @throws IllegalArgumentException if callback or handler is null.477* @return A handle to the newly-created camera device.478*479* @see #getCameraIdList480* @see android.app.admin.DevicePolicyManager#setCameraDisabled481*/482private CameraDevice openCameraDeviceUserAsync(String cameraId,483CameraDevice.StateCallback callback, Executor executor, final int uid)
private CameraDevice openCameraDeviceUserAsync(String cameraId,483CameraDevice.StateCallback callback, Executor executor, final int uid)484throws CameraAccessException {//获取到目标camera的特性参数485CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);486CameraDevice device = null;487488synchronized (mLock) {489490ICameraDeviceUser cameraUser = null;//实例化一个 CameraDeviceImpl,构造时传入了 CameraDevice.StateCallback 以及 Handler492android.hardware.camera2.impl.CameraDeviceImpl deviceImpl =493new android.hardware.camera2.impl.CameraDeviceImpl(494cameraId,495callback,496executor,497characteristics,498mContext.getApplicationInfo().targetSdkVersion);//获取 CameraDeviceCallback 实例,这是提供给远端连接到 CameraDeviceImpl 的接口500ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks();//从 CameraManagerGlobal 中获取 CameraService 的本地接口,通过它远端调用(采用 Binder 机制)//connectDevice 方法连接到相机设备 。注意返回的 cameraUser 实际上指向的是远端//CameraDeviceClient 的本地接口 。502try {503if (supportsCamera2ApiLocked(cameraId)) {504// Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices505ICameraService cameraService = CameraManagerGlobal.get().getCameraService();506if (cameraService == null) {507throw new ServiceSpecificException(508ICameraService.ERROR_DISCONNECTED,509"Camera service is currently unavailable");510}511cameraUser = cameraService.connectDevice(callbacks, cameraId,512mContext.getOpPackageName(), mContext.getAttributionTag(), uid);513} else {514// Use legacy camera implementation for HAL1 devices515int id;516try {517id = Integer.parseInt(cameraId);518} catch (NumberFormatException e) {519throw new IllegalArgumentException("Expected cameraId to be numeric, but it was: "520+ cameraId);521}522523Log.i(TAG, "Using legacy camera HAL.");524cameraUser = CameraDeviceUserShim.connectBinderShim(callbacks, id,525getDisplaySize());526}527} catch (ServiceSpecificException e) {............557}//将 CameraDeviceClient 设置到 CameraDeviceImpl 中进行管理559// TODO: factor out callback to be non-nested, then move setter to constructor560// For now, calling setRemoteDevice will fire initial561// onOpened/onUnconfigured callbacks.562// This function call may post onDisconnected and throw CAMERA_DISCONNECTED if563// cameraUser dies during setup.564deviceImpl.setRemoteDevice(cameraUser);565device = deviceImpl;566}567568return device;569}