Search code examples
androidandroid-cameraandroid-source

Underlying logic of camera's native code


I'm trying to better understand the underlying logic of camera's native code, but I appear to be hitting a dead-end when looking for the method definition for Camera::connect() and other functions declared from Camera.h.

The steps I've followed are these:

  1. In the master branch I located Camera.java, which contains the logic for resolving cameras, selecting CameraInfo.CAMERA_FACING_BACK when it is encountered:
    • Camera.open() calls Camera.getCameraInfo(int, CameraInfo) for each int in Camera.getNumberOfCameras().
    • Camera.getCameraInfo(int, CameraInfo) in turn calls the native function Camera._getCameraInfo(int, CameraInfo).
  2. Delving into the JNI:

    • android_hardware_Camera_getCameraInfo(JNIEnv*, jobject, jint, jobject) then invokes the static method Camera::getCameraInfo(jint, CameraInfo*), which appears to be declared through:

      #include <camera/Camera.h>
      
  3. Browsing and searching the master branch doesn't seem to give any hits for camera/Camera.h. The only result I could find was in older tags, for instance in the gingerbread branch. Here there's only a method declaration:

    static  status_t    getCameraInfo(int cameraId,
                                  struct CameraInfo* cameraInfo);
    
  4. However, the method body doesn't appear to be defined anywhere.

The last two steps are where I am confused. Where is camera/Camera.h defined for more recent versions of Android? And finally, where are the method bodies of Camera actually defined?


Solution

  • In ICS, the Camera::getCameraInfo(jint, CameraInfo*) is defined in frameworks/av/camera/Camera.cpp as

    status_t Camera::getCameraInfo(int cameraId,
                                   struct CameraInfo* cameraInfo) {
        const sp<ICameraService>& cs = getCameraService();
        if (cs == 0) return UNKNOWN_ERROR;
        return cs->getCameraInfo(cameraId, cameraInfo);
    }
    

    Then it grabs a binder object of CameraService and call the getCameraInfo on CameraService.

    status_t CameraService::getCameraInfo(int cameraId,
                                          struct CameraInfo* cameraInfo) {
        ...
        struct camera_info info;
        status_t rc = mModule->get_camera_info(cameraId, &info);
        ...
        return rc;
    }
    

    The mModule contains the actual implementation of camera on your device. Different devices may have different implementations. For example you can find QualcommCamera under hardware/qcom/camera/QualcommCamera.cpp.

    Then take a look at connect. The connect does a similar work and finally calls CameraService::connect. In that method, there is a CameraClient initialized by the mModule. So when you do something with the Client, you are actually working with the device-specific implementation.

    One more word, the mModule is initialized by hw_get_module in CameraService::onFirstRef.