Camera服務分為Client和Server兩部分,本文主要分析Client部分。
1. Camera Client介紹
主要由以下幾個文件組成:
Camera.h/ Camera.cpp
ICameraClient.h/ IcameraClient.h
如圖中所示,Camera.class繼承自IcameraClient.class。Camera.class主要由libandroid_runtime.so中的android_hardware_Camera.cpp調用(frameworks/base/core/jni/android_hardware_Camera.cpp),也就是說Camera.h是Camera服務框架,對上層的接口。
2. ICameraClient.h分析
class ICameraClient: public IInterface
{
public:
DECLARE_META_INTERFACE(CameraClient);
virtual void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
virtual void dataCallback(int32_t msgType, const sp<IMemory>& data) = 0;
virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& data) = 0;
};
// ----------------------------------------------------------------------------
class BnCameraClient: public BnInterface<ICameraClient>
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
根據BnInterface類模版的定義BnInterface<ICameraClient>類相當于雙繼承了BnInterface和ICameraClient。
大家如果熟悉Binder結構的話,看到這個文件可能會迷惑了,這不是一個Client嗎?怎么還要繼承自IInterface并且要實現(xiàn)BnCameraClient呢?這在Binder結構中是一個service應該做的。因為camera服務和一般的android服務不太一樣,一般的android服務,只需要client通過binder調用service的一些功能,并得到返回結果。再說的直接一點兒,就是client只需要單向調用service即可,所以一般的client只需要實現(xiàn)BpXXX,service只需要實現(xiàn)BnXXX。但是Camera服務中,service需要回調Client,所以就是說Camera服務中的client需要雙向調用,所以client和service都要實現(xiàn)BnXXX和BpXXX。
service需要回調client的接口就是:
virtual void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
= 0;
virtual void dataCallback(int32_t msgType, const sp<IMemory>& data) = 0;
virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& data) = 0;
這幾個接口與Camera.h中定義的CameraListener接口中的函數(shù)是對應的,從它們的函數(shù)名稱和參數(shù)列表就可以看出。
3.Camera.h
class CameraListener: virtual public RefBase
{
public:
virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
};
class Camera : public BnCameraClient, public IBinder::DeathRecipient
{
public:
//對上層的接口
static sp<Camera> create(const sp<ICamera>& camera);
static int32_t getNumberOfCameras();
static status_t getCameraInfo(int cameraId,
struct CameraInfo* cameraInfo);
static sp<Camera> connect(int cameraId);
~Camera();
void init();
status_t reconnect();
void disconnect();
status_t startPreview();
void stopPreview();
//對上層的接口
// ICameraClient interface
virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
private:
Camera();
Camera(const Camera&);
Camera& operator=(const Camera);
virtual void binderDied(const wp<IBinder>& who);
class DeathNotifier: public IBinder::DeathRecipient
{
public:
DeathNotifier() {
}
virtual void binderDied(const wp<IBinder>& who);
};
static sp<DeathNotifier> mDeathNotifier;
// helper function to obtain camera service handle
static const sp<ICameraService>& getCameraService();
sp<ICamera> mCamera;
status_t mStatus;
sp<CameraListener> mListener;
friend class DeathNotifier;
static Mutex mLock;
static sp<ICameraService> mCameraService;
};
從接口中可以看出Camera類剛好實現(xiàn)了一個Camera的基本操作,例如播放(startPreview)、停止(stopPreview)、暫停(takePicture)等。在Camera類中connect()是一個靜態(tài)函數(shù),它用于得到一個Camera的實例。
BnCameraClient繼承了BnInterface<ICameraClient>,這是為基于Android的基礎類Binder機制實現(xiàn)在進程通訊而構建的。
4.ICameraClient.cpp分析
class BpCameraClient: public BpInterface<ICameraClient>
{
public:
BpCameraClient(const sp<IBinder>& impl)
: BpInterface<ICameraClient>(impl)
{
}
// generic callback from camera service to app
void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
{
remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
}
// generic data callback from camera service to app with image data
void dataCallback(int32_t msgType, const sp<IMemory>& imageData)
{
remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
}
// generic data callback from camera service to app with image data
void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
{
remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
}
};
IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");
// ----------------------------------------------------------------------
status_t BnCameraClient::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case NOTIFY_CALLBACK: {
notifyCallback(msgType, ext1, ext2);
} break;
case DATA_CALLBACK: {
dataCallback(msgType, imageData);
} break;
case DATA_CALLBACK_TIMESTAMP: {
dataCallbackTimestamp(timestamp, msgType, imageData);
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
ICameraClient.cpp實現(xiàn)了BnCameraClient和BpCameraClient。BnCameraClient中調用的notifyCallback,dataCallback和dataCallbackTimestamp(ICameraClient.h中定義)是在Camera.cpp中實現(xiàn)的。關于BnCameraClient和BpCameraClient,可以看一下Binder的結構。
5.Camera.cpp分析
首先獲取Camera的service:
// establish binder interface to camera service
const sp<ICameraService>& Camera::getCameraService()
{
Mutex::Autolock _l(mLock);
if (mCameraService.get() == 0) {
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder;
do {
binder = sm->getService(String16("media.camera"));
if (binder != 0)
break;
LOGW("CameraService not published, waiting...");
usleep(500000); // 0.5 s
} while(true);
if (mDeathNotifier == NULL) {
mDeathNotifier = new DeathNotifier();
}
binder->linkToDeath(mDeathNotifier);
mCameraService = interface_cast<ICameraService>(binder);
}
LOGE_IF(mCameraService==0, "no CameraService!?");
return mCameraService;
}
sp<Camera> Camera::connect(int cameraId)
{
LOGV("connect");
sp<Camera> c = new Camera();
const sp<ICameraService>& cs = getCameraService();
if (cs != 0) {
c->mCamera = cs->connect(c, cameraId);
}
if (c->mCamera != 0) {
c->mCamera->asBinder()->linkToDeath(c);
c->mStatus = NO_ERROR;
} else {
c.clear();
}
return c;
}
在getCameraService()中首先通過ServiceManager獲取到ICameraService,然后在connect方法中通過調用ICameraService的connect方法,獲取到一個ICamera,并賦值給Camera的成員變量mCamera??梢宰约喝タ匆幌翪amera.cpp的剩余代碼,幾乎所有的函數(shù)中,都是對ICameraService或者mCamera的調用。
實現(xiàn)ICameraClient接口:
// callback from camera service
void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
{
sp<CameraListener> listener;
{
Mutex::Autolock _l(mLock);
listener = mListener;
}
if (listener != NULL) {
listener->notify(msgType, ext1, ext2);
}
}
可以看出,其實就是對CameraListener的調用。
mListener由
void Camera::setListener(const sp<CameraListener>& listener)
{
Mutex::Autolock _l(mLock);
mListener = listener;
}
方法設定。
|