USB摄像头

USB摄像头功能主要实现了手机无需root就支持USB Camera设备的检测、连接、预览和视频数据采集并推送到远端功能

1、使用开源项目UVCCameralibuvccameraso库,并实现设备监听接口OnDeviceConnectListener

public interface OnDeviceConnectListener {
        void onAttach(UsbDevice var1);

        void onDettach(UsbDevice var1);

        void onConnect(UsbDevice var1, USBMonitor.UsbControlBlock var2, boolean var3);

        void onDisconnect(UsbDevice var1, USBMonitor.UsbControlBlock var2);

        void onCancel(UsbDevice var1);
    }
2、实例化RongRTCAVOutputStream
mOutputStream = new RongRTCAVOutputStream(MediaType.VIDEO,"USB");
3、在设备监听接口的onConnect回调方法中进行初始化USB摄像头对象并开始预览和采集视频数据
@Override
    public void onConnect(final UsbDevice usbDevice, final USBMonitor.UsbControlBlock usbControlBlock, boolean b) {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                if (mState.get() == STATE_START){
                    onInitCamera(usbDevice,usbControlBlock);
                }
            }
        });
    }

 /**
     * 初始化摄像头
     * @param usbDevice
     * @param usbControlBlock
     */
    private void onInitCamera(UsbDevice usbDevice, USBMonitor.UsbControlBlock usbControlBlock) {
        releaseCamera();
        final UVCCamera camera = new UVCCamera();
        camera.open(usbControlBlock);
        camera.setStatusCallback(new IStatusCallback() {
            @Override
            public void onStatus(final int statusClass, final int event, final int selector,
                                 final int statusAttribute, final ByteBuffer data) {
                log("onStatus","statusClass=" + statusClass
                        + "; " +
                        "event=" + event + "; " +
                        "selector=" + selector + "; " +
                        "statusAttribute=" + statusAttribute + "; ");
            }
        });
        camera.setButtonCallback(new IButtonCallback() {
            @Override
            public void onButton(final int button, final int state) {
                log("onButton","(button=" + button + "; " +
                        "state=" + state + ")");
            }
        });
        Size size = getClosestSupportedSize(camera.getSupportedSizeList(), mReqWidth, mReqHeight);
        if (size != null) {
            mSelectWidth = size.width;
            mSelectHeight = size.height;
        }
        try {
            log("onInitCamera","select: "+mSelectWidth+"x"+mSelectHeight+" , req: "+mReqWidth+"x"+mReqHeight);
            camera.setPreviewSize(mSelectWidth, mSelectHeight,1, 60, UVCCamera.FRAME_FORMAT_YUYV, 1.0f);
        } catch (final IllegalArgumentException e) {
            // fallback to YUV mode
            try {
                camera.setPreviewSize(mSelectWidth, mSelectHeight, 1, 60, UVCCamera.FRAME_FORMAT_YUYV, 1.0f);
            } catch (final IllegalArgumentException e1) {
                camera.destroy();
                return;
            }
        }
        try {
            createTexture();
            camera.setPreviewTexture(mSurfaceTexture);
            mUVCCamera = camera;
            if (mState.get() == STATE_START)
                onStartPreview();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
4、将采集后的视频数据交给RongRTCAVOutputStream进行编码后推送到远端即可完成推送功能
if (mOutputStream != null)
            mOutputStream.writeTextureFrame(width, height,oesTextureId,transformMatrix,0,timestampNs);