本文主要是介绍android camera: android.hardware.camera2向下和camera service,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
android.hardware.camera2向下和camera service的通信是通过mCameraService和mRemoteDevice
frameworks/base/core/java/android/hardware/camera2//**
* A per-process global camera manager instance, to retain a connection to the camera service,
* and to distribute camera availability notices to API-registered callbacks
*/
private static final class CameraManagerGlobal extends ICameraServiceListener.Stub
/**
* Connect to the camera service if it's available, and set up listeners.
*/
private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera";
private void connectCameraServiceLocked() {
IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
CameraMetadataNative.setupGlobalVendorTagDescriptor();
try {
CameraStatus[] cameraStatuses = cameraService.addListener(this);
for (CameraStatus c : cameraStatuses) {
onStatusChangedLocked(c.status, c.cameraId);
}
mCameraService = cameraService;
}
}
}
通过JNI调用setupGlobalVendorTagDescriptor,natice层也是调用CameraService做具体的实现。
CameraManager通过CameraMangerGlobal工作:
public void openCamera(@NonNull String cameraId,
@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler) ->
public void openCameraForUid(@NonNull String cameraId,
@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler,
int clientUid) ->
private CameraDevice openCameraDeviceUserAsync(String cameraId,
CameraDevice.StateCallback callback, Handler handler, final int uid){
CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
CameraDevice device = null;
ICameraDeviceUser cameraUser = null;
android.hardware.camera2.impl.CameraDeviceImpl deviceImpl =
new android.hardware.camera2.impl.CameraDeviceImpl(
cameraId,
callback,
handler,
characteristics,
mContext.getApplicationInfo().targetSdkVersion);
ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks();
// Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices
ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
cameraUser = cameraService.connectDevice(callbacks, cameraId,
mContext.getOpPackageName(), uid);
deviceImpl.setRemoteDevice(cameraUser);
device = deviceImpl;
return device;
}
CameraDevice, CameraDeviceImpl, ICameraDeviceUser三者之间的关系:
public class CameraDeviceImpl extends CameraDevice
public void setRemoteDevice(ICameraDeviceUser remoteDevice) throws CameraAccessException {
mRemoteDevice = new ICameraDeviceUserWrapper(remoteDevice);
mDeviceHandler.post(mCallOnOpened);
mDeviceHandler.post(mCallOnUnconfigured);
}
1. 通过ICameraService接口访问ICameraService.aidl
ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
ICameraDeviceUser cameraUser=cameraService . connectDevice ( callbacks , cameraId , mContext . getOpPackageName (), uid );
mRemoteDevice = new ICameraDeviceUserWrapper(cameraUser);
2. 通过ICameraDeviceUserWrapper mRemoteDevice访问ICameraDeviceUser.aidl
这里Wrapper的意思只是多了异常捕捉而已
public void prepare(int streamId) throws CameraAccessException {try {mRemoteDevice.prepare(streamId);} catch (Throwable t) {CameraManager.throwAsPublicException(t);throw new UnsupportedOperationException("Unexpected exception", t);}}
这篇关于android camera: android.hardware.camera2向下和camera service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!