Android Camera进程间通信类总结

《Android Camera架构》
《Android Camera进程间通信类总结》
《Android Camera模块解析之拍照》
《Android Camera模块解析之视频录制》
《Android Camera原理之CameraDeviceCallbacks回调模块》
《Android Camera原理之openCamera模块(一)》
《Android Camera原理之openCamera模块(二)》
《Android Camera原理之createCaptureSession模块》
《Android Camera原理之setRepeatingRequest与capture模块》
《Android Camera原理之编译》
《Android Camera原理之camera provider启动》
《Android Camera原理之cameraserver与cameraprovider是怎样联系的》
《Android Camera原理之camera service与camera provider session会话与capture request轮转》
《Android Camera原理之camera HAL底层数据结构与类总结》
《Android Camera原理之camera service类与接口关系》

我们直达camera模块的代码根据层次可以分为下面几层,开发者直接接触的是Camera api的部分,Camera api会通过Binder IPC方式调用到Camera服务部分,但是这部分代码实在繁杂,需要将这部分IPC联系的方式总结一些,方便后来人方便的学习这一块的内容。



  • Camera api部分:
    frameworks/base/core/java/android/hardware/camera2
  • Camera JNI部分:
    frameworks/base/core/jni/android_hardware_Camera.cpp
    编译选项在目录下的Android.bp
    make libandroid_runtime -j1
  • Camera UI库部分:
    frameworks/av/camera/
    编译选项在目录下的Android.bp
    make libcamera_client -j1
  • Camera服务部分:
    frameworks/av/services/camera/libcameraservice/
    编译选项在目录下的Android.mk
    make libcameraservice -j1
  • Camera HAL部分:
    hardware/qcom/camera/


首先大家关注一下这个代码路径:frameworks/av/camera/aidl/ 这里是和cameraservice端交互的桥梁。

image.png

cameraservice端的代码在frameworks/av/services/camera/libcameraservice/下面。
image.png

1.ICameraDeviceUser.aidl

这个aidl文件会自动生成:
./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/camera2/ICameraDeviceUser.h

#ifndef AIDL_GENERATED_ANDROID_HARDWARE_CAMERA2_I_CAMERA_DEVICE_USER_H_
#define AIDL_GENERATED_ANDROID_HARDWARE_CAMERA2_I_CAMERA_DEVICE_USER_H_

#include <binder/IBinder.h>
#include <binder/IInterface.h>
#include <binder/Status.h>
#include <camera/CameraMetadata.h>
#include <camera/camera2/CaptureRequest.h>
#include <camera/camera2/OutputConfiguration.h>
#include <camera/camera2/SubmitInfo.h>
#include <cstdint>
#include <gui/view/Surface.h>
#include <utils/StrongPointer.h>
#include <vector>

namespace android {

namespace hardware {

namespace camera2 {

class ICameraDeviceUser : public ::android::IInterface {
public:
  DECLARE_META_INTERFACE(CameraDeviceUser)
  enum  : int32_t {
    NO_IN_FLIGHT_REPEATING_FRAMES = -1,
    NORMAL_MODE = 0,
    CONSTRAINED_HIGH_SPEED_MODE = 1,
    VENDOR_MODE_START = 32768,
    TEMPLATE_PREVIEW = 1,
    TEMPLATE_STILL_CAPTURE = 2,
    TEMPLATE_RECORD = 3,
    TEMPLATE_VIDEO_SNAPSHOT = 4,
    TEMPLATE_ZERO_SHUTTER_LAG = 5,
    TEMPLATE_MANUAL = 6,
  };
  virtual ::android::binder::Status disconnect() = 0;
  virtual ::android::binder::Status submitRequest(const ::android::hardware::camera2::CaptureRequest& request, bool streaming, ::android::hardware::camera2::utils::SubmitInfo* _aidl_return) = 0;
  virtual ::android::binder::Status submitRequestList(const ::std::vector<::android::hardware::camera2::CaptureRequest>& requestList, bool streaming, ::android::hardware::camera2::utils::SubmitInfo* _aidl_return) = 0;
  virtual ::android::binder::Status cancelRequest(int32_t requestId, int64_t* _aidl_return) = 0;
  virtual ::android::binder::Status beginConfigure() = 0;
  virtual ::android::binder::Status endConfigure(int32_t operatingMode, const ::android::hardware::camera2::impl::CameraMetadataNative& sessionParams) = 0;
  virtual ::android::binder::Status deleteStream(int32_t streamId) = 0;
  virtual ::android::binder::Status createStream(const ::android::hardware::camera2::params::OutputConfiguration& outputConfiguration, int32_t* _aidl_return) = 0;
  virtual ::android::binder::Status createInputStream(int32_t width, int32_t height, int32_t format, int32_t* _aidl_return) = 0;
  virtual ::android::binder::Status getInputSurface(::android::view::Surface* _aidl_return) = 0;
  virtual ::android::binder::Status createDefaultRequest(int32_t templateId, ::android::hardware::camera2::impl::CameraMetadataNative* _aidl_return) = 0;
  virtual ::android::binder::Status getCameraInfo(::android::hardware::camera2::impl::CameraMetadataNative* _aidl_return) = 0;
  virtual ::android::binder::Status waitUntilIdle() = 0;
  virtual ::android::binder::Status flush(int64_t* _aidl_return) = 0;
  virtual ::android::binder::Status prepare(int32_t streamId) = 0;
  virtual ::android::binder::Status tearDown(int32_t streamId) = 0;
  virtual ::android::binder::Status prepare2(int32_t maxCount, int32_t streamId) = 0;
  virtual ::android::binder::Status updateOutputConfiguration(int32_t streamId, const ::android::hardware::camera2::params::OutputConfiguration& outputConfiguration) = 0;
  virtual ::android::binder::Status finalizeOutputConfigurations(int32_t streamId, const ::android::hardware::camera2::params::OutputConfiguration& outputConfiguration) = 0;
  enum Call {
    DISCONNECT = ::android::IBinder::FIRST_CALL_TRANSACTION + 0,
    SUBMITREQUEST = ::android::IBinder::FIRST_CALL_TRANSACTION + 1,
    SUBMITREQUESTLIST = ::android::IBinder::FIRST_CALL_TRANSACTION + 2,
    CANCELREQUEST = ::android::IBinder::FIRST_CALL_TRANSACTION + 3,
    BEGINCONFIGURE = ::android::IBinder::FIRST_CALL_TRANSACTION + 4,
    ENDCONFIGURE = ::android::IBinder::FIRST_CALL_TRANSACTION + 5,
    DELETESTREAM = ::android::IBinder::FIRST_CALL_TRANSACTION + 6,
    CREATESTREAM = ::android::IBinder::FIRST_CALL_TRANSACTION + 7,
    CREATEINPUTSTREAM = ::android::IBinder::FIRST_CALL_TRANSACTION + 8,
    GETINPUTSURFACE = ::android::IBinder::FIRST_CALL_TRANSACTION + 9,
    CREATEDEFAULTREQUEST = ::android::IBinder::FIRST_CALL_TRANSACTION + 10,
    GETCAMERAINFO = ::android::IBinder::FIRST_CALL_TRANSACTION + 11,
    WAITUNTILIDLE = ::android::IBinder::FIRST_CALL_TRANSACTION + 12,
    FLUSH = ::android::IBinder::FIRST_CALL_TRANSACTION + 13,
    PREPARE = ::android::IBinder::FIRST_CALL_TRANSACTION + 14,
    TEARDOWN = ::android::IBinder::FIRST_CALL_TRANSACTION + 15,
    PREPARE2 = ::android::IBinder::FIRST_CALL_TRANSACTION + 16,
    UPDATEOUTPUTCONFIGURATION = ::android::IBinder::FIRST_CALL_TRANSACTION + 17,
    FINALIZEOUTPUTCONFIGURATIONS = ::android::IBinder::FIRST_CALL_TRANSACTION + 18,
  };
};  // class ICameraDeviceUser

class ICameraDeviceUserDefault : public ICameraDeviceUser {
public:
  ::android::IBinder* onAsBinder() override;
  ::android::binder::Status disconnect() override;
  ::android::binder::Status submitRequest(const ::android::hardware::camera2::CaptureRequest& request, bool streaming, ::android::hardware::camera2::utils::SubmitInfo* _aidl_return) override;
  ::android::binder::Status submitRequestList(const ::std::vector<::android::hardware::camera2::CaptureRequest>& requestList, bool streaming, ::android::hardware::camera2::utils::SubmitInfo* _aidl_return) override;
  ::android::binder::Status cancelRequest(int32_t requestId, int64_t* _aidl_return) override;
  ::android::binder::Status beginConfigure() override;
  ::android::binder::Status endConfigure(int32_t operatingMode, const ::android::hardware::camera2::impl::CameraMetadataNative& sessionParams) override;
  ::android::binder::Status deleteStream(int32_t streamId) override;
  ::android::binder::Status createStream(const ::android::hardware::camera2::params::OutputConfiguration& outputConfiguration, int32_t* _aidl_return) override;
  ::android::binder::Status createInputStream(int32_t width, int32_t height, int32_t format, int32_t* _aidl_return) override;
  ::android::binder::Status getInputSurface(::android::view::Surface* _aidl_return) override;
  ::android::binder::Status createDefaultRequest(int32_t templateId, ::android::hardware::camera2::impl::CameraMetadataNative* _aidl_return) override;
  ::android::binder::Status getCameraInfo(::android::hardware::camera2::impl::CameraMetadataNative* _aidl_return) override;
  ::android::binder::Status waitUntilIdle() override;
  ::android::binder::Status flush(int64_t* _aidl_return) override;
  ::android::binder::Status prepare(int32_t streamId) override;
  ::android::binder::Status tearDown(int32_t streamId) override;
  ::android::binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
  ::android::binder::Status updateOutputConfiguration(int32_t streamId, const ::android::hardware::camera2::params::OutputConfiguration& outputConfiguration) override;
  ::android::binder::Status finalizeOutputConfigurations(int32_t streamId, const ::android::hardware::camera2::params::OutputConfiguration& outputConfiguration) override;

};

}  // namespace camera2

}  // namespace hardware

}  // namespace android

#endif  // AIDL_GENERATED_ANDROID_HARDWARE_CAMERA2_I_CAMERA_DEVICE_USER_H_

./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/camera2/BnCameraDeviceUser.h继承了ICameraDeviceUser.h

#ifndef AIDL_GENERATED_ANDROID_HARDWARE_CAMERA2_BN_CAMERA_DEVICE_USER_H_
#define AIDL_GENERATED_ANDROID_HARDWARE_CAMERA2_BN_CAMERA_DEVICE_USER_H_

#include <binder/IInterface.h>
#include <android/hardware/camera2/ICameraDeviceUser.h>

namespace android {

namespace hardware {

namespace camera2 {

class BnCameraDeviceUser : public ::android::BnInterface<ICameraDeviceUser> {
public:
  ::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags = 0) override;
};  // class BnCameraDeviceUser

}  // namespace camera2

}  // namespace hardware

}  // namespace android

#endif  // AIDL_GENERATED_ANDROID_HARDWARE_CAMERA2_BN_CAMERA_DEVICE_USER_H_

./frameworks/av/services/camera/libcameraservice/api2/CameraDeviceClient.h中又发现了继承BnCameraDeviceUser.h的代码。

struct CameraDeviceClientBase :
         public CameraService::BasicClient,
         public hardware::camera2::BnCameraDeviceUser
{
    typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;

    const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
        return mRemoteCallback;
    }

protected:
    CameraDeviceClientBase(const sp<CameraService>& cameraService,
            const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
            const String16& clientPackageName,
            const String8& cameraId,
            int api1CameraId,
            int cameraFacing,
            int clientPid,
            uid_t clientUid,
            int servicePid);

    sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
};

class CameraDeviceClient :
        public Camera2ClientBase<CameraDeviceClientBase>,
        public camera2::FrameProcessorBase::FilteredListener
{
//......
}

这个IPC调用时为了操作底层的Camera Device,获取camera device重要信息,并操作Camera Device,提供了int createStream(in OutputConfiguration outputConfiguration);来创建captute session机制,Surface getInputSurface();来返回相应的输入流。

2.ICameraDeviceCallbacks.aidl

./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/camera2/ICameraDeviceCallbacks.h
| |
| |
./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/camera2/BnCameraDeviceCallbacks.h
| |
| |
frameworks/av/camera/ndk/impl/ACameraDevice.h

    // Callbacks from camera service
    class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks {
      public:
        explicit ServiceCallback(CameraDevice* device) : mDevice(device) {}
        binder::Status onDeviceError(int32_t errorCode,
                           const CaptureResultExtras& resultExtras) override;
        binder::Status onDeviceIdle() override;
        binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
                              int64_t timestamp) override;
        binder::Status onResultReceived(const CameraMetadata& metadata,
                              const CaptureResultExtras& resultExtras,
                              const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) override;
        binder::Status onPrepared(int streamId) override;
        binder::Status onRequestQueueEmpty() override;
        binder::Status onRepeatingRequestError(int64_t lastFrameNumber,
                int32_t stoppedSequenceId) override;
      private:
        const wp<CameraDevice> mDevice;
    };

这是获取当前Camera Device状态的回调,在操作camera device之前需要判断当前的camera是否可用。

3.ICameraService.aidl

./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/ICameraService.h
| |
| |
./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/BnCameraService.h
| |
| |
frameworks/av/services/camera/libcameraservice/CameraService.h

class CameraService :
    public BinderService<CameraService>,
    public virtual ::android::hardware::BnCameraService,
    public virtual IBinder::DeathRecipient,
    public virtual CameraProviderManager::StatusListener
{
//......
}

这个Camera Service的主类,用户进程中操作camera核心功能:connectDevice、设置闪光灯等等操作都是通过IPC调用camera service实现的。

4.ICameraServiceProxy.aidl

./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/ICameraServiceProxy.h
| |
| |
./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/BnCameraServiceProxy.h
| |
| |
frameworks/base/services/core/java/com/android/server/camera/CameraServiceProxy.java

    private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() {
        @Override
        public void pingForUserUpdate() {
            if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
                Slog.e(TAG, "Calling UID: " + Binder.getCallingUid() + " doesn't match expected " +
                        " camera service UID!");
                return;
            }
            notifySwitchWithRetries(30);
        }

        @Override
        public void notifyCameraState(String cameraId, int newCameraState, int facing,
                String clientName, int apiLevel) {
            if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
                Slog.e(TAG, "Calling UID: " + Binder.getCallingUid() + " doesn't match expected " +
                        " camera service UID!");
                return;
            }
            String state = cameraStateToString(newCameraState);
            String facingStr = cameraFacingToString(facing);
            if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " +
                    state + " for client " + clientName + " API Level " + apiLevel);

            updateActivityCount(cameraId, newCameraState, facing, clientName, apiLevel);
        }
    };

5.ICameraServiceListener.aidl

./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/ICameraServiceListener.h
| |
| |
./out/soong/.intermediates/frameworks/av/camera/libcamera_client/android_arm_armv7-a-neon_core_shared_platform/gen/aidl/android/hardware/BnCameraServiceListener.h
| |
| |
frameworks/base/core/java/android/hardware/camera2/CameraManager.java

    private static final class CameraManagerGlobal extends ICameraServiceListener.Stub
            implements IBinder.DeathRecipient {
    }

6.其他aidl文件

其他的aidl文件都是IPC间传输的Parcelable类型的数据,例如CaptureRequest.aidl就是IPC中相机捕获请求的数据,CameraInfo.aidl就是IPC中 camera数据的集合。

推荐阅读更多精彩内容

  • 跟叔叔聊天,工作不顺心,一直返工返工,我说感受到了这个世界满满的恶意。他说,谁欺负你了,明明这个世界还是很美好的嘛...
    MissJean阅读 54评论 0 1
  • 这周完成的 1 完善古诗模块后台部分数据接口实现机制,完善古诗活动数据统计异步任务实现机制。 2 导入古诗模块相关...
    heartshapebox阅读 64评论 0 0
  • 什么样的灵魂可以被救赎?
    三古月甯阅读 49评论 0 1
  • 文/心雨 任性,从来与金钱无关 我总是以为,这辈子只是路过人间 和风耳语,白云作伴 在山川草木间游荡 在深夜聆听谁...
    心雨挥洒阅读 40评论 0 2
  • 陈珊珊回家进门,正赶上珊妈蒸好了年糕招呼她来吃。奇怪的是平时一直把门的奶牛猫薄荷不见了踪影。一问才知道跑没了!陈珊...
    安泊璨阅读 128评论 10 6