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
virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp
};
// ----------------------------------------------------------------------------
class BnCameraClient: public BnInterface
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
根据BnInterface类模版的定义BnInterface
大家如果熟悉Binder结构的话,看到这个文件可能会迷惑了,这不是一个Client吗?怎么还要继承自IInterface并且要实现BnCameraClient呢?这在Binder结构中是一个service应该做的。因为camera服务和一般的android服务不太一样,一般的android服务,只需要client通过binder调用service的一些功能,并得到返回结果。再说的直接一点儿,就是client只需要单向调用service即可,所以一般的client只需要实现BpXXX,service只需要实现BnXXX。但是Camera服务中,service需要回调Client,所以就是说Camera服务中的client需要双向调用,所以client和service都要实现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
virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp
这几个接口与Camera.h中定义的CameraListener接口中的函数是对应的,从它们的函数名称和参数列表就可以看出。
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
virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp
};
class Camera : public BnCameraClient, public IBinder::DeathRecipient
{
public:
//对上层的接口
static sp
static int32_t getNumberOfCameras();
static status_t getCameraInfo(int cameraId,
struct CameraInfo* cameraInfo);
static sp
~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
virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp
private:
Camera();
Camera(const Camera&);
Camera& operator=(const Camera);
virtual void binderDied(const wp
class DeathNotifier: public IBinder::DeathRecipient
{
public:
DeathNotifier() {
}
virtual void binderDied(const wp
};
static sp
// helper function to obtain camera service handle
static const sp
sp
status_t mStatus;
sp
friend class DeathNotifier;
static Mutex mLock;
static sp
};
从接口中可以看出Camera类刚好实现了一个Camera的基本操作,例如播放(startPreview)、停止(stopPreview)、暂停(takePicture)等。在Camera类中connect()是一个静态函数,它用于得到一个Camera的实例。
BnCameraClient继承了BnInterface
4.ICameraClient.cpp分析
class BpCameraClient: public BpInterface
{
public:
BpCameraClient(const sp
: BpInterface
{
}
// 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
{
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
{
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实现了BnCameraClient和BpCameraClient。BnCameraClient中调用的notifyCallback,dataCallback和dataCallbackTimestamp(ICameraClient.h中定义)是在Camera.cpp中实现的。关于BnCameraClient和BpCameraClient,可以看一下Binder的结构。
5.Camera.cpp分析
首先获取Camera的service:
// establish binder interface to camera service
const sp
{
Mutex::Autolock _l(mLock);
if (mCameraService.get() == 0) {
sp
sp
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
}
LOGE_IF(mCameraService==0, "no CameraService!?");
return mCameraService;
}
sp
{
LOGV("connect");
sp
const sp
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。可以自己去看一下Camera.cpp的剩余代码,几乎所有的函数中,都是对ICameraService或者mCamera的调用。
实现ICameraClient接口:
// callback from camera service
void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
{
sp
{
Mutex::Autolock _l(mLock);
listener = mListener;
}
if (listener != NULL) {
listener->notify(msgType, ext1, ext2);
}
}
可以看出,其实就是对CameraListener的调用。
mListener由
void Camera::setListener(const sp
{
Mutex::Autolock _l(mLock);
mListener = listener;
}
方法设定。