喷泉之所以漂亮,是因为她有压力;瀑布之所以壮观,是因为她没退路。
全部博文(149)
分类: Android平台
2015-11-09 14:27:41
camera service端的结构很容易让人迷惑的,下面是对其进行的分析:
service这部分包括以下几个头文件:
ICamera.h, 路径:frameworks/av/include/camera
ICameraService.h, 路径:frameworks/av/include/camera/ICameraService.h
CameraService.h,路径:frameworks/av/services/camera/libcameraservice
对应的实现:
ICamera.cpp, 路径:frameworks/av/camera
ICameraService.cpp, 路径:frameworks/av/camera
CameraService.cpp。路径:frameworks/av/services/camera/libcameraservice
CameraService中包含了一个内部类CameraService::Client,这个CameraService::Client是对 ICamera的实现,camera client得到的ICamera就是这个CameraService::Client。
也就是说与camera client真正通信的,是这个CameraService::Client。下面我们来逐步分析service这块的实现。
1. ICameraService
(1)ICameraService.h
其中只定义了3个方法:
virtual int32_t getNumberOfCameras() = 0;
virtual status_t getCameraInfo(int cameraId, struct CameraInfo* cameraInfo) = 0;
virtual sp connect(const sp& cameraClient, int cameraId) = 0;
(2) ICameraService.cpp
看代码:
status_t BnCameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)这3个函数的真正实现在CameraService.cpp中。
sp cameraClient = interface_cast(data.readStrongBinder());中得到此次连接的 client,并且传递到connect函数中。保存client信息的目的是保存在CameraService::Client中,在适当的时机回调 client。
2.CameraService
(1)CameraService.h
class CameraService : public BinderService, public BnCameraService
这个 BinderService是什么东西呢?看看它的定义,在frameworks/base/include/binder下BinderService.h。
template
class BinderService
{
public:
static status_t publish() {
sp sm(defaultServiceManager());
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
}
static void publishAndJoinThreadPool() {
sp proc(ProcessState::self());
sp sm(defaultServiceManager());
sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
static void instantiate() { publish(); }
static status_t shutdown() {
return NO_ERROR;
}
};
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());从这句代码,可以看出,这是native service在向SM注册。
其实这个模板类,就是为native service提供了一向service注册的统一方式。
publish和publishAndJoinThreadPool的区别就是在于,注册之后是否开启线程池来监听。
因为之前看过media server的代码,在media server的main函数里,调用的就是CameraService.instantiate来注册的,因为camera service是跑在media server进程里的,所以camera service不需要自己来开启线程来循环监听。
所以,我觉得调用publish和publishAndJoinThreadPool的不同场景是:如果这个service是跑在别的进程里的,就调用publish,如果是自己单独一个进程,就调用publishAndJoinThreadPool。
(2)CameraService.cpp
实现在ICameraService定义的3个函数。
getNumberOfCameras()和getCameraInfo()的实现很简单。
int32_t CameraService::getNumberOfCameras() {
return mNumberOfCameras; //mNumberOfCameras是在构造函数中初始化的,mNumberOfCameras = HAL_getNumberOfCameras();
}
status_t CameraService::getCameraInfo(int cameraId,
struct CameraInfo* cameraInfo) {
if (cameraId < 0 || cameraId >= mNumberOfCameras) {
return BAD_VALUE;
}
HAL_getCameraInfo(cameraId, cameraInfo);
return OK;
}
主要看connect函数:
sp CameraService::connect( const sp& cameraClient, int cameraId) {
sp client; //CameraService::Client
…………
if (mClient[cameraId] != 0) {
client = mClient[cameraId].promote();
if (client != 0) {
if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
LOG1("CameraService::connect X (pid %d) (the same client)",
callingPid);
return client;
}
}
}
…………
sp hardware =
HAL_openCameraHardware(cameraId);
//获取CameraHardwareInterface接口,在下边的代码构造Client时传递进入
…………
CameraInfo info;
HAL_getCameraInfo(cameraId, &info);
client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid); //这里就是之前说过的,在onTransact保存的client
mClient[cameraId] = client;
return client;
}
3.CameraService::Client
class Client : public BnCamera, Client是对ICamera的实现。
构造函数:
CameraService::Client::Client(const sp& cameraService,
const sp& cameraClient,
const sp& hardware,
int cameraId, int cameraFacing, int clientPid) {
int callingPid = getCallingPid();
LOG1("Client::Client E (pid %d)", callingPid);
mCameraService = cameraService;
mCameraClient = cameraClient;
mHardware = hardware;
mCameraId = cameraId;
mCameraFacing = cameraFacing;
mClientPid = clientPid;
mUseOverlay = mHardware->useOverlay();
mMsgEnabled = 0;
mHardware->setCallbacks(notifyCallback,
dataCallback,
dataCallbackTimestamp,
(void *)cameraId);
// Enable zoom, error, and focus messages by default
enableMsgType(CAMERA_MSG_ERROR |
CAMERA_MSG_ZOOM |
CAMERA_MSG_FOCUS);
mOverlayW = 0;
mOverlayH = 0;
// Callback is disabled by default
mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
mOrientationChanged = false;
cameraService->setCameraBusy(cameraId);
cameraService->loadSound();
LOG1("Client::Client X (pid %d)", callingPid);
}
主要进行一些成员变量的初始化,主要有mCameraClient, mHardware等。都是在CameraService::connect时进行的。
Client的各个函数可以自己看一下,主要就是对mHardware的调用,这是一个CameraHardwareInterface接口,也就是对HAL层的一个封装。比如stopRecording函数:
// stop recording mode
void CameraService::Client::stopRecording() {
……
mCameraService->playSound(SOUND_RECORDING);
disableMsgType(CAMERA_MSG_VIDEO_FRAME);
mHardware->stopRecording();
…………
}
4.ICamera
最后再来说一下ICamera。
这个ICamera定义的其实就是camera client与camera service的接口。
当camera client连接camera service时,也就是调用CameraService::connect时,返回一个ICamera接口给camera client调用,这个ICamera接口的真正实现是CameraService::Client。