api 調(diào)用
- 獲取CameraManager對(duì)象
CameraManager mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
- openCamera
mCameraManager.openCamera(cameraId, callback, handler);
調(diào)用的是 CameraManager 中的 openCameraDeviceUserAsync 方法
private CameraDevice openCameraDeviceUserAsync(String cameraId,
CameraDevice.StateCallback callback, Executor executor, final int uid)
throws CameraAccessException {
... ...
// Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices
ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
if (cameraService == null) {
throw new ServiceSpecificException(
ICameraService.ERROR_DISCONNECTED,
"Camera service is currently unavailable");
}
cameraUser = cameraService.connectDevice(callbacks, cameraId,
mContext.getOpPackageName(), uid);
... ...
return device;
}
ICameraService 是 aidl,來(lái)看一下它的對(duì)象實(shí)例化過(guò)程,它的實(shí)例化時(shí)在 CameraManager 的內(nèi)部類(lèi) CameraManagerGlobal 的 connectCameraServiceLocked 方法中
private void connectCameraServiceLocked() {
// Only reconnect if necessary
if (mCameraService != null || sCameraServiceDisabled) return;
Log.i(TAG, "Connecting to camera service");
IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
if (cameraServiceBinder == null) {
// Camera service is now down, leave mCameraService as null
return;
}
try {
cameraServiceBinder.linkToDeath(this, /*flags*/ 0);
} catch (RemoteException e) {
// Camera service is now down, leave mCameraService as null
return;
}
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
... ...
}
ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME) 也是通過(guò) aidl 獲取 CameraService 在 Native 中注冊(cè)的 binder 對(duì)象蜒简,然后我們通過(guò)這個(gè) binder 對(duì)象實(shí)例化 ICameraService 對(duì)象迎变,我們看一下 ICameraService.aidl 通過(guò)編譯生成的 ICameraService.java 中 asInterface 方法的實(shí)現(xiàn)(文件路徑: /android/out/soong/.intermediates/frameworks/base/framework/android_common/gen/aidl/frameworks/av/camera/aidl/android/hardware/ICameraService.java)
public static android.hardware.ICameraService asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof android.hardware.ICameraService))) {
return ((android.hardware.ICameraService) iin);
}
return new android.hardware.ICameraService.Stub.Proxy(obj);
}
首先會(huì)通過(guò)queryLocalInterface根據(jù)標(biāo)識(shí)DESCRIPTOR在本地搜索接口向楼,搜索到的是 ICameraService 內(nèi)部類(lèi) Stub 構(gòu)造函數(shù)內(nèi)調(diào)用 attachInterface 與 binder 綁定的接口
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
如果沒(méi)有實(shí)例化 Stub 對(duì)象的話則會(huì)返回代理類(lèi)的對(duì)象(ICameraService 就沒(méi)有 Stub 對(duì)象)兰吟,代理類(lèi)也是 Aidl 自動(dòng)生成的觅廓;所以調(diào)用的 connectDevice 方法其實(shí)是 Proxy 類(lèi)的
@Override
public android.hardware.camera2.ICameraDeviceUser connectDevice(android.hardware.camera2.ICameraDeviceCallbacks callbacks, java.lang.String cameraId, java.lang.String opPackageName, int clientUid) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
android.hardware.camera2.ICameraDeviceUser _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((((callbacks != null)) ? (callbacks.asBinder()) : (null)));
_data.writeString(cameraId);
_data.writeString(opPackageName);
_data.writeInt(clientUid);
mRemote.transact(Stub.TRANSACTION_connectDevice, _data, _reply, 0);
_reply.readException();
_result = android.hardware.camera2.ICameraDeviceUser.Stub.asInterface(_reply.readStrongBinder());
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
調(diào)用 transact 方法發(fā)起 RPC (遠(yuǎn)程過(guò)程調(diào)用)請(qǐng)求,同時(shí)當(dāng)前線程掛起服鹅,然后服務(wù)端的 onTransact 方法會(huì)被調(diào)用凳兵,直到 RPC 過(guò)程返回,當(dāng)前線程繼續(xù)執(zhí)行企软,從 _reply 中取出 RPC 過(guò)程中返回的結(jié)果
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
... ...
case TRANSACTION_connectDevice: {
data.enforceInterface(descriptor);
android.hardware.camera2.ICameraDeviceCallbacks _arg0;
_arg0 = android.hardware.camera2.ICameraDeviceCallbacks.Stub.asInterface(data.readStrongBinder());
java.lang.String _arg1;
_arg1 = data.readString();
java.lang.String _arg2;
_arg2 = data.readString();
int _arg3;
_arg3 = data.readInt();
android.hardware.camera2.ICameraDeviceUser _result = this.connectDevice(_arg0, _arg1, _arg2, _arg3);
reply.writeNoException();
reply.writeStrongBinder((((_result != null)) ? (_result.asBinder()) : (null)));
return true;
}
... ...
}
}
對(duì)應(yīng) ICameraService.java庐扫, aidl 同時(shí)也在 Native 生成了 ICameraService.cpp,內(nèi)有 BpCameraService (Native Proxy)仗哨,及 BnCameraService 的實(shí)現(xiàn)形庭;最終是在 BnCameraService 的 onTransact 方法內(nèi)調(diào)用其子類(lèi) CameraService 的 connectDevice 方法實(shí)現(xiàn) openCamera 的操作的
Status CameraService::connectDevice(
const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
const String16& cameraId,
const String16& clientPackageName,
int clientUid,
/*out*/
sp<hardware::camera2::ICameraDeviceUser>* device) {
ATRACE_CALL();
Status ret = Status::ok();
String8 id = String8(cameraId);
sp<CameraDeviceClient> client = nullptr;
ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
/*api1CameraId*/-1,
CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
clientUid, USE_CALLING_PID, API_2,
/*legacyMode*/ false, /*shimUpdateOnly*/ false,
/*out*/client);
if(!ret.isOk()) {
logRejected(id, getCallingPid(), String8(clientPackageName),
ret.toString8());
return ret;
}
*device = client;
return ret;
}