双camera同时打开

平台:android5.1
场景:双屏异显设备需求在两个屏上同时打开两个摄像头,确认可行性。
时间:2016.9.28

双camera示例代码:

        this.mSurfaceHolder = this.sfv1.getHolder();
        this.mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
                Camera mcamera = Camera.open(0);
                mcamera.setDisplayOrientation(90);
                try {
                    mcamera.setPreviewDisplay(mSurfaceHolder);
                    mcamera.startPreview();
                    return;
                } catch (IOException localIOException) {
                    localIOException.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
        });
        this.mSurfaceHolder.setType(3);

        this.mSurfaceHolder1 = this.sfv2.getHolder();
        this.mSurfaceHolder1.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
                Camera mcamera = Camera.open(1);
                mcamera.setDisplayOrientation(90);
                try {
                    mcamera.setPreviewDisplay(mSurfaceHolder1);
                    mcamera.startPreview();
                    return;
                } catch (IOException localIOException) {
                    localIOException.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
        });
        this.mSurfaceHolder1.setType(3);

双Camera同时工作需要硬件设计支持:两路ISP,以及MCLK,I2C是否共用等。

当硬件支持后,如果软件没有支持,则基本上会在Camera.open(1)报错,从此处开始给出调用链:

Camera.java::open()
native_setup()
android_hardware_Camera.cpp::android_hardware_Camera_native_setup()          //Camera::connect()
CameraBase.cpp::connect()               //(cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,/*out*/ c->mCamera),此处fnConnectServiceconnect,即CameraServiceconnect函数。
CameraService.cpp::connect()                        
//CameraService::onFirstRef()通过hw_get_module(CAMERA_HARDWARE_MODULE_ID, (const hw_module_t **)&mModule)的调用,将module获取。而后在connect()中通过CAMERA_DEVICE_API_VERSION_来创建对应的clinet,KK版本使用的1_0版本,即CameraClient对象。
connectFinishUnsafe()
//此函数调用client->initialize(mModule)。CameraClient::initialize(),建立CameraHardwareInterface对象,并调用其initialize(),继而 module->methods->open(),获取到hw_device_t对象。

以上对open()调用,已经切入到HAL中。双camera同时工作的判断基本都放在此open()中:
Intel 4.4平台示例:

static int ATOM_OpenCameraHardware(const hw_module_t* module, const char* name,
                hw_device_t** device)
{
...
    if ((!PlatformData::supportDualVideo() && atom_instances == 1) || atom_instances > MAX_HAL_INSTANCES-1) {
        LOGE("error:only support maximum  %d instances for front/primary sensor", atom_instances);
        return -EINVAL;
    }
...
}

MTK5.1平台示例:

status_t
CamDeviceManagerBase::
openDeviceLocked(
    hw_device_t** device,
    hw_module_t const* module,
    int32_t const i4OpenId,
    uint32_t device_version
)
{
...
    if  ( OK != (status = validateOpenLocked(i4OpenId, device_version)) )
    {
        return  status;
    }
...
}

status_t
CamDeviceManagerImp::
validateOpenLocked(int32_t i4OpenId, uint32_t device_version) const
{
...
#if !defined(MTKCAM_HAVE_NATIVE_PIP)          //MTK平台的ProjectConfig.mk中MTK_CAM_NATIVE_PIP_SUPPORT影响这里宏定义
    if  ( 0 != mOpenMap.size() )
    {
        MY_LOGE("Not support PIP");
        MY_LOGE("[Now] fail to open (deviceId:%d version:0x%x) => failure", i4OpenId, device_version);
        MY_LOGE("[Previous] (deviceId:%d version:0x%x) mOpenMap.size:%zu", mOpenMap.keyAt(0), mOpenMap.valueAt(0)->uDeviceVersion, mOpenMap.size());
        return -EUSERS;
    }
#endif
...
}

PS:
关于Camera HAL的结构体和API支持宏定义都申明在hardware/libhardware/include/hardware/camera_common.h和camera.h文件中.

你可能感兴趣的:(双camera同时打开)