您好,登錄后才能下訂單哦!
本篇內容介紹了“Camera Service的結構是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
在camera service這端的結構還是很容易讓人迷惑的,我就是看了好久,才縷清楚關系。
service這部分包括以下幾個頭文件:ICamera.h, ICameraService.h, CameraService.h,對應的實現ICamera.cpp, ICameraService.cpp, CameraService.cpp。
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<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId) = 0;
(2) ICameraService.cpp
看代碼:
status_t BnCameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case GET_NUMBER_OF_CAMERAS: {
CHECK_INTERFACE(ICameraService, data, reply);
reply->writeInt32(getNumberOfCameras());
return NO_ERROR;
} break;
case GET_CAMERA_INFO: {
CHECK_INTERFACE(ICameraService, data, reply);
CameraInfo cameraInfo;
memset(&cameraInfo, 0, sizeof(cameraInfo));
status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
reply->writeInt32(cameraInfo.facing);
reply->writeInt32(cameraInfo.orientation);
reply->writeInt32(result);
return NO_ERROR;
} break;
case CONNECT: {
CHECK_INTERFACE(ICameraService, data, reply);
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
sp<ICamera> camera = connect(cameraClient, data.readInt32());
reply->writeStrongBinder(camera->asBinder());
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
這3個函數的真正實現在CameraService.cpp中。
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());中得到此次連接的client,并且傳遞到connect函數中。保存client信息的目的是保存在CameraService::Client中,在適當的時機回調client。
2.CameraService
(1)CameraService.h
class CameraService : public BinderService<CameraService>, public BnCameraService
這個 BinderService<CameraService>是什么東西呢?看看它的定義,在frameworks/base/include/binder下BinderService.h。
template<typename SERVICE>
class BinderService
{
public:
static status_t publish() {
sp<IServiceManager> sm(defaultServiceManager());
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
}
static void publishAndJoinThreadPool() {
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> 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<ICamera> CameraService::connect( const sp<ICameraClient>& cameraClient, int cameraId) {
sp<Client> 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<CameraHardwareInterface> 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>& cameraService,
const sp<ICameraClient>& cameraClient,
const sp<CameraHardwareInterface>& 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。
“Camera Service的結構是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。