7. 那么現(xiàn)在的關鍵就是 Client 類了·進一步跟進:
CameraService::Client::Client(const sp<CameraService>& cameraService,
const sp<ICameraClient>& cameraClient, pid_t clientPid)
{
…..
mCameraService = cameraService;
mCameraClient = cameraClient;
mClientPid = clientPid;
mHardware = openCameraHardware();
}
將 cameraService 和 cameraClient 的實例分別賦值給了 Client 的類成員變量。
另外 openCameraHardware() 是值得注意的地方,也就是連接上層應用和底層驅(qū)動的關鍵,通過調(diào)用 openCameraHardware() 得到了一個 CameraHardwareInterface 實例對象,并賦值給自己的類成員: `
sp<CameraHardwareInterface> mHardware;
對 hardware 的操作就是通過該對象完成的,所以說真正意義上的功能實現(xiàn)其實就是在這里,即 client 類的函數(shù)接口調(diào)用。
對于 hardware 的東東咱們暫時不去關注吧。
那么我們再次仔細研究下 Client 類的繼承關系 ( 這些繼承關系很容易混亂,涉及到較多的多態(tài)類型轉(zhuǎn)換 ) ,這個其實往往都很關鍵:
Client 繼承于 BnCamera ,而 BnCamera 則繼承于 ICamera ,也就是說 Client 繼承了 ICamera, 實現(xiàn)了 ICamera 中的函數(shù)。
進而發(fā)現(xiàn),原來繞一個大圈,把最開始的圖簡化下:
8. 除此之外還有兩個步驟或許需要去研究下:
先從單一函數(shù)去跟進,看具體一些 callback 的實現(xiàn)流程:
// 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);
}
}
這是 Camera 類中一個 callback 函數(shù)實現(xiàn),但其本質(zhì)在哪?先看 camera 類的繼承關系:
通過以上的繼承關系,繼續(xù)跟進其父類 ICameraClient :
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;
};
其中 notifyCallback() 又是純虛函數(shù) , 則同樣說明實現(xiàn)在其子類 BpCameraClient 中:
// generic callback from camera service to app
void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
{
LOGV("notifyCallback");
Parcel data, reply;
data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
data.writeInt32(msgType);
data.writeInt32(ext1);
data.writeInt32(ext2);
remote()->transact(NOTIFY_CALLBACK,data, &reply, IBinder::FLAG_ONEWAY);
}
然后通過 Binder 通訊調(diào)用到 BnCameraClient 中實現(xiàn):
status_t BnCameraClient::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case NOTIFY_CALLBACK: {
LOGV("NOTIFY_CALLBACK");
CHECK_INTERFACE(ICameraClient, data, reply);
int32_t msgType = data.readInt32();
int32_t ext1 = data.readInt32();
int32_t ext2 = data.readInt32();
notifyCallback(msgType, ext1, ext2);
return NO_ERROR;
} break;
….
}
進而調(diào)用到了 Camera.cpp 中的函數(shù)實現(xiàn)了,但或許你有疑問,這些 callback 是涉及到一些驅(qū)動的 callback ,哪怎么跟驅(qū)動聯(lián)系起來那?
結合之前對 hardware 接口調(diào)用的類 Client ,進一步可以發(fā)現(xiàn) callback 的處理同樣是在 Client 類實例化的時候:
CameraService::Client::Client(const sp<CameraService>& cameraService,
const sp<ICameraClient>& cameraClient, pid_t clientPid)
{
…..
mHardware->setCallbacks( notifyCallback ,
dataCallback,
dataCallbackTimestamp,
mCameraService.get());
…..
}
調(diào)用了 mHardware 將 callback 傳入,但此處的 notifyCallback 并不是 camera.cpp 中的函數(shù),而是 client 類的 notifyCallback 函數(shù)。
再繼續(xù)看 client 類中的 notifyCallback 函數(shù)實現(xiàn):
void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,int32_t ext2, void* user)
{
…..
default:
sp<ICameraClient> c = client->mCameraClient;
if (c != NULL) {
c->notifyCallback(msgType, ext1, ext2);
}
break;
…..
}
通過得到 ICameraClient 實例進而調(diào)用到了具體的對象 Camera 的 notifyCallback() 函數(shù)。這個地方估計會遇見跟 ICameraService 函數(shù)調(diào)用一樣的問題, ICameraClient 函數(shù)調(diào)用所需要的函數(shù)實例在哪?
記得上述 ICameraService 講到的 connect() 函數(shù)嘛?其中有一個參數(shù)不能被忽略掉的,就是 ICameraClient ,但它在真正傳入的時候卻是一個 ICameraClient 子類 camera 的實例對象。
CameraService:
sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
{
…..
// create a new Client object
client = new Client(this, cameraClient , callingPid);
…..
}
Client:
CameraService::Client::Client(const sp<CameraService>& cameraService,
const sp<ICameraClient>& cameraClient , pid_t clientPid)
{
….
mCameraService = cameraService;
mCameraClient = cameraClient ;
….
}
這樣就清楚了,其實 Client 在調(diào)用設置 callback 的調(diào)用最終還是調(diào)用到了 camera.cpp 中的 callback 函數(shù),進而將具體內(nèi)容通過 callback 反饋給上層應用做出相應的處理。