pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.53k stars 772 forks source link

How do I know which camera is currently in use ? #1543

Closed Moyout closed 1 month ago

Moyout commented 1 month ago

I want to choose a camera for a device before starting streaming. But I couldn't find a method or attribute. There is only one way to switch cameras.

public void switchCamera() {
        VideoSource source = genericStream.getVideoSource();
        if (source instanceof Camera1Source) {
            ((Camera1Source) source).switchCamera();
        } else if (source instanceof Camera2Source) {
            ((Camera2Source) source).switchCamera();
        }
     }

How can I choose between a front facing camera, a rear facing camera, or an external camera instead of switching.

pedroSG94 commented 1 month ago

Hello,

If you want start with front or rear camera you can do this (working with camera1 and camera2):

            //start with front camera
            if (CameraHelper.Facing.BACK == getCameraFacing()) {
                switchCamera()
            }
            //start with rear camera
            if (CameraHelper.Facing.FRONT == getCameraFacing()) {
                switchCamera()
            }

This way you can know which camera are you using and switch depend the camera that you want. About external camera, this is only available with camera2. You will need know the id of the external camera and open by id:

//open camera by id
openCameraId("2")
//get ids of all cameras
val ids = camerasAvailable()

Keep in mind that in most of devices, an usb camera is not detected using camera2 api and you will need use a library with uvc camera support.

Moyout commented 1 month ago

I got it!

哈罗,

如果您想从前置或后置摄像头开始,您可以这样做(使用相机1和相机2):

            //start with front camera
            if (CameraHelper.Facing.BACK == getCameraFacing()) {
                switchCamera()
            }
            //start with rear camera
            if (CameraHelper.Facing.FRONT == getCameraFacing()) {
                switchCamera()
            }

通过这种方式,您可以知道您正在使用哪台相机,并切换取决于您想要的相机。关于外部相机,这仅适用于相机2。您需要知道外部相机的ID,并通过ID打开:

//open camera by id
openCameraId("2")
//get ids of all cameras
val ids = camerasAvailable()

请记住,在大多数设备中,使用camera2 api无法检测到usb相机,您需要使用支持uvc相机的库。