Android原始碼筆記——Camera系統架構
Camera的架構與Android系統的整體架構保持一致,如下圖所示,本文主要從以下四個方面對其進行說明。
- Framework:Camera.java
- Android Runtime:android_hardware_Camera.cpp
- Library:Camera Client和Camera Service
- HAL:CameraHardwareInterface
一、Framework:Camera.java
Camera是應用層軟體直接使用的類,涵蓋了啟動、預覽、拍攝及關閉等操作攝像頭的全部介面。Camera.Java在Android原始碼中的路徑為:framework/base/core/java/android/hardware。為了說明整個Camera系統的架構,這裡暫不橫向分析Camera.java的功能,下面從open()方法著手:
public static Camera open() { int numberOfCameras = getNumberOfCameras(); CameraInfo cameraInfo = new CameraInfo(); for (int i = 0; i < numberOfCameras; i++) { getCameraInfo(i, cameraInfo); if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) { return newCamera(i); } } return null; }
open()方法需要注意以下幾點:
- getNumberOfCameras為native方法,實現在android_hardware_Camera.cpp中;
- CameraInfo是Camera定義的靜態內部類,包含facing、orientation、canDisableShutterSound;
- getCameraInfo內部呼叫native方法_getCameraInfo獲取攝像頭資訊;
- open()預設啟動的是後置攝像頭(CAMERA_FACING_BACK)。
/** used by Camera#open, Camera#open(int)*/ Camera(int cameraId) { int err = cameraInitNormal(cameraId); if (checkInitErrors(err)) { switch(err) { case EACCESS: throw new RuntimeException("Fail to connect to camera service"); case ENODEV: throw new RuntimeException("Camera initialization failed"); default: // Should never hit this. throw new RuntimeException("Unknown camera error"); } } }
Camera構造器的核心實現在cameraInitNormal中,cameraInitNormal呼叫cameraInitVersion,並傳入引數cameraId和CAMERA_HAL_API_VERSION_NORMAL_CONNECT,後者代表HAL的版本。
private int cameraInitVersion(int cameraId, int halVersion) {
……
String packageName = ActivityThread.currentPackageName(); return native_setup(new WeakReference<Camera>(this), cameraId, halVersion, packageName); }
cameraInitNormal呼叫本地方法native_setup(),由此進入到android_hardware_Camera.cpp中,native_setup()的簽名如下:
private native final int native_setup(Object camera_this, int cameraId, int halVersion, String packageName);
二、Android Runtime:android_hardware_Camera.cpp
native_setup()被動態註冊到JNI,通過JNI呼叫android_hardware_Camera_native_setup()方法。
static JNINativeMethod camMethods[] = { …… { "native_setup", "(Ljava/lang/Object;ILjava/lang/String;)V", (void*)android_hardware_Camera_native_setup } …… };
JNI的重點是android_hardware_Camera_native_setup()方法的實現:
// connect to camera service static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, jint cameraId, jint halVersion, jstring clientPackageName) { // Convert jstring to String16 const char16_t *rawClientName = env->GetStringChars(clientPackageName, NULL); jsize rawClientNameLen = env->GetStringLength(clientPackageName); String16 clientName(rawClientName, rawClientNameLen); env->ReleaseStringChars(clientPackageName, rawClientName); sp<Camera> camera; if (halVersion == CAMERA_HAL_API_VERSION_NORMAL_CONNECT) { // Default path: hal version is don't care, do normal camera connect. camera = Camera::connect(cameraId, clientName, Camera::USE_CALLING_UID); } else { jint status = Camera::connectLegacy(cameraId, halVersion, clientName, Camera::USE_CALLING_UID, camera); if (status != NO_ERROR) { return status; } } if (camera == NULL) { return -EACCES; } // make sure camera hardware is alive if (camera->getStatus() != NO_ERROR) { return NO_INIT; } jclass clazz = env->GetObjectClass(thiz); if (clazz == NULL) { // This should never happen jniThrowRuntimeException(env, "Can't find android/hardware/Camera"); return INVALID_OPERATION; } // We use a weak reference so the Camera object can be garbage collected. // The reference is only used as a proxy for callbacks. sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera); context->incStrong((void*)android_hardware_Camera_native_setup); camera->setListener(context); // save context in opaque field env->SetLongField(thiz, fields.context, (jlong)context.get()); return NO_ERROR; }
android_hardware_Camera_native_setup()方法通過呼叫Camera::connect()方法請求連線CameraService服務。入參中:
- clientName是通過將clientPackageName從jstring轉換為String16格式得到;
- Camera::USE_CALLING_UID是定義在Camera.h中的列舉型別,其值為ICameraService::USE_CALLING_UID(同樣為列舉型別,值為-1)。
Camera::connect()位於Camera.cpp中,由此進入到Library層。
三、Library:Camera Client和Camera Service
如上述架構圖中所示,ICameraService.h、ICameraClient.h和ICamera.h三個類定義了Camera的介面和架構,ICameraService.cpp和Camera.cpp兩個檔案用於Camera架構的實現,Camera的具體功能在下層呼叫硬體相關的介面來實現。Camera.h是Camera系統對上層的介面。
具體的,Camera類繼承模板類CameraBase,Camera::connect()呼叫了CameraBase.cpp中的connect()方法。
sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName, int clientUid) { return CameraBaseT::connect(cameraId, clientPackageName, clientUid); }
CameraBase實際上又繼承了IBinder的DeathRecipient內部類,DeathRecipient虛擬繼承自RefBase。RefBase是Android中的引用計數基礎類,其中定義了incStrong、decStrong、incWeak和decWeak等涉及sp/wp的指標操作函式,當然這扯遠了。
template <typename TCam> struct CameraTraits { }; template <typename TCam, typename TCamTraits = CameraTraits<TCam> > class CameraBase : public IBinder::DeathRecipient { public: static sp<TCam> connect(int cameraId, const String16& clientPackageName, int clientUid); …… }
class DeathRecipient : public virtual RefBase { public: virtual void binderDied(const wp<IBinder>& who) = 0; };
回到Camera::connect()的實現上,其中,new TCam(cameraId)生成BnCameraClient物件,BnCameraClient定義在ICameraClient.h檔案中,繼承自模板類BnInterface。getCameraService()方法返回CameraService的服務代理BpCameraService,BpCameraService同樣繼承自模板類BnInterface。然後通過Binder通訊傳送CONNECT命令,當BnCameraService收到CONNECT命令後呼叫CameraService的connect()成員函式來做相應的處理。
template <typename TCam, typename TCamTraits> sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId, const String16& clientPackageName, int clientUid) { ALOGV("%s: connect", __FUNCTION__); sp<TCam> c = new TCam(cameraId); // BnCameraClient sp<TCamCallbacks> cl = c; status_t status = NO_ERROR; const sp<ICameraService>& cs = getCameraService(); // BpCameraService if (cs != 0) { TCamConnectService fnConnectService = TCamTraits::fnConnectService; status = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid, /*out*/ c->mCamera); } if (status == OK && c->mCamera != 0) { c->mCamera->asBinder()->linkToDeath(c); c->mStatus = NO_ERROR; } else { ALOGW("An error occurred while connecting to camera: %d", cameraId); c.clear(); } return c; }
class BnCameraClient: public BnInterface<ICameraClient> { public: virtual status_t onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0); };
class BpCameraService: public BpInterface<ICameraService> { public: BpCameraService(const sp<IBinder>& impl) : BpInterface<ICameraService>(impl) { } …… }
注:connect()函式在BpCameraService和BnCameraService的父類ICameraService中宣告為純虛擬函式,在BpCameraService和CameraService中分別給出了實現,BpCameraService作為代理類,提供介面給客戶端,真正實現在BnCameraService的子類CameraService中。
在BpCameraService中,connect()函式實現如下:
// connect to camera service (android.hardware.Camera) virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId, const String16 &clientPackageName, int clientUid, /*out*/ sp<ICamera>& device) { Parcel data, reply; data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); data.writeStrongBinder(cameraClient->asBinder()); data.writeInt32(cameraId); data.writeString16(clientPackageName); data.writeInt32(clientUid); remote()->transact(BnCameraService::CONNECT, data, &reply); // BpBinder的transact()函式向IPCThreadState例項傳送訊息,通知其有訊息要傳送給binder driver
if (readExceptionCode(reply)) return -EPROTO; status_t status = reply.readInt32(); if (reply.readInt32() != 0) { device = interface_cast<ICamera>(reply.readStrongBinder()); // client端讀出server返回的bind } return status; }
首先將傳遞過來的Camera物件cameraClient轉換成IBinder型別,將呼叫的引數寫到Parcel(可理解為Binder通訊的管道)中,通過BpBinder的transact()函式傳送訊息,然後由BnCameraService去響應該連線,最後就是等待服務端返回,如果成功則生成一個BpCamera例項。
真正的服務端響應實現在BnCameraService的onTransact()函式中,其負責解包收到的Parcel並執行client端的請求的方法。
status_t BnCameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) {
…… case CONNECT: { CHECK_INTERFACE(ICameraService, data, reply); sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder()); // 使用Camera的Binder物件生成Camera客戶代理BpCameraClient例項 int32_t cameraId = data.readInt32(); const String16 clientName = data.readString16(); int32_t clientUid = data.readInt32(); sp<ICamera> camera; status_t status = connect(cameraClient, cameraId, clientName, clientUid, /*out*/camera); // 將生成的BpCameraClient物件作為引數傳遞到CameraService的connect()函式中 reply->writeNoException(); reply->writeInt32(status); // 將BpCamera物件以IBinder的形式打包到Parcel中返回 if (camera != NULL) { reply->writeInt32(1); reply->writeStrongBinder(camera->asBinder()); } else { reply->writeInt32(0); } return NO_ERROR; } break; …… } }
主要的處理包括:
- 通過data中Camera的Binder物件生成Camera客戶代理BpCameraClient例項;
- 將生成的BpCameraClient物件作為引數傳遞到CameraService(/frameworks/av/services/camera /libcameraservice/CameraService.cpp)的connect()函式中,該函式會返回一個BpCamera例項;
- 將在上述例項物件以IBinder的形式打包到Parcel中返回。
最後,BpCamera例項是通過CameraService::connect()函式返回的。CameraService::connect()實現的核心是呼叫connectHelperLocked()函式根據HAL不同API的版本建立不同的client例項(早期版本中好像沒有connectHelperLocked()這個函式,但功能基本相似)。
status_t CameraService::connectHelperLocked( /*out*/ sp<Client>& client, /*in*/ const sp<ICameraClient>& cameraClient, int cameraId, const String16& clientPackageName, int clientUid, int callingPid, int halVersion, bool legacyMode) { int facing = -1; int deviceVersion = getDeviceVersion(cameraId, &facing); if (halVersion < 0 || halVersion == deviceVersion) { // Default path: HAL version is unspecified by caller, create CameraClient // based on device version reported by the HAL. switch(deviceVersion) { case CAMERA_DEVICE_API_VERSION_1_0: client = new CameraClient(this, cameraClient, clientPackageName, cameraId, facing, callingPid, clientUid, getpid(), legacyMode); break; case CAMERA_DEVICE_API_VERSION_2_0: case CAMERA_DEVICE_API_VERSION_2_1: case CAMERA_DEVICE_API_VERSION_3_0: case CAMERA_DEVICE_API_VERSION_3_1: case CAMERA_DEVICE_API_VERSION_3_2: client = new Camera2Client(this, cameraClient, clientPackageName, cameraId, facing, callingPid, clientUid, getpid(), legacyMode); break; case -1: ALOGE("Invalid camera id %d", cameraId); return BAD_VALUE; default: ALOGE("Unknown camera device HAL version: %d", deviceVersion); return INVALID_OPERATION; } } else { // A particular HAL version is requested by caller. Create CameraClient // based on the requested HAL version. if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && halVersion == CAMERA_DEVICE_API_VERSION_1_0) { // Only support higher HAL version device opened as HAL1.0 device. client = new CameraClient(this, cameraClient, clientPackageName, cameraId, facing, callingPid, clientUid, getpid(), legacyMode); } else { // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet. ALOGE("Invalid camera HAL version %x: HAL %x device can only be" " opened as HAL %x device", halVersion, deviceVersion, CAMERA_DEVICE_API_VERSION_1_0); return INVALID_OPERATION; } } status_t status = connectFinishUnsafe(client, client->getRemote()); if (status != OK) { // this is probably not recoverable.. maybe the client can try again return status; } mClient[cameraId] = client; LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid()); return OK; }
可見,在CAMERA_DEVICE_API_VERSION_2_0之前使用CameraClient進行例項化,之後則採用Camera2Client進行例項化。以CameraClient為例,其initialize()函式如下:
status_t CameraClient::initialize(camera_module_t *module) { int callingPid = getCallingPid(); status_t res; LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId); // Verify ops permissions res = startCameraOps(); if (res != OK) { return res; } char camera_device_name[10]; snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId); mHardware = new CameraHardwareInterface(camera_device_name); res = mHardware->initialize(&module->common); if (res != OK) { ALOGE("%s: Camera %d: unable to initialize device: %s (%d)", __FUNCTION__, mCameraId, strerror(-res), res); mHardware.clear(); return res; } mHardware->setCallbacks(notifyCallback, dataCallback, dataCallbackTimestamp, (void *)(uintptr_t)mCameraId); // Enable zoom, error, focus, and metadata messages by default enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS | CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE); LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId); return OK; }
上述函式中,主要注意以下流程:
- 加粗的程式碼CameraHardwareInterface新建了了一個Camera硬體介面,當然,camera_device_name為攝像頭裝置名;
- mHardware->initialize(&module->common)呼叫底層硬體的初始化方法;
- mHardware->setCallbacks將CamerService處的回撥函式註冊到HAL處。
CameraHardwareInterface定義了Camera的硬體抽象特徵,由此進入到HAL。
四、HAL:CameraHardwareInterface
CameraHardwareInterface的作用在於連結Camera Server和V4L2,通過實現CameraHardwareInterface可以遮蔽不同的driver對Camera Server的影響。CameraHardwareInterface同樣虛擬繼承自RefBase。
class CameraHardwareInterface : public virtual RefBase { public: CameraHardwareInterface(const char *name) { mDevice = 0; mName = name; } …… }
CameraHardwareInterface中包含了控制通道和資料通道,控制通道用於處理預覽和視訊獲取的開始/停止、拍攝照片、自動對焦等功能,資料通道通過回撥函式來獲得預覽、視訊錄製、自動對焦等資料。當需要支援新的硬體時就需要繼承於CameraHardwareInterface ,來實現對應的功能。CameraHardwareInterface提供的public方法如下:
在前一節中,initialize()函式呼叫了mHardware->initialize和mHardware->se