Android WebRTC: switch camera back & front issue with NullPointerException in Camera2Enumerator.convertSizes

Viewed 734

Based on the example project, I have implemented webrtc for video call in my application. Everything works fine except for switching camera.

On a video call, the default camera is front. If I switch camera front --> back --> front for the 1st time, it works.

But if I switch again camera front to back, I got the following exception in Camera2Enumerator:

java.lang.NullPointerException: Attempt to get length of null array
    at org.webrtc.Camera2Enumerator.convertSizes(Camera2Enumerator.java:234)
    at org.webrtc.Camera2Enumerator.getSupportedSizes(Camera2Enumerator.java:147)
    at org.webrtc.Camera2Session.findCaptureFormat(Camera2Session.java:325)
    at org.webrtc.Camera2Session.start(Camera2Session.java:313)
    at org.webrtc.Camera2Session.<init>(Camera2Session.java:296)
    at org.webrtc.Camera2Session.create(Camera2Session.java:274)
    at org.webrtc.Camera2Capturer.createCameraSession(Camera2Capturer.java:35)
    at org.webrtc.CameraCapturer$5.run(CameraCapturer.java:270)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.os.HandlerThread.run(HandlerThread.java:67)

To switch the camera, I use the code in the example project as follow:

private void switchCameraInternal() {
    if (videoCapturer instanceof CameraVideoCapturer) {
        if (!isVideoCallEnabled() || isError) {
            Log.e(TAG,
                    "Failed to switch camera. Video: " + isVideoCallEnabled() + ". Error : " + isError);
            return; // No video is sent or only one camera is available or error happened.
        }
        Log.d(TAG, "Switch camera");
        CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
        cameraVideoCapturer.switchCamera(null);
    } else {
        Log.d(TAG, "Will not switch camera, video caputurer is not a camera");
    }
}

Also, I have notice that in "CameraVideoCapture" interface, there is another switchCamera for specific camera Id. However, I cannot call this function, this function marked as not defined.

void switchCamera(CameraSwitchHandler switchEventsHandler, String cameraName);

So I look at the CameraCapturer class who implements the above interface, but it does not implement this one. So I think there is only 1 choice to use the default switchCamera as the above code in the example project.

Is there anything wrong with this code? Thanks a lot for any help.

1 Answers

This is an issue with Camera2 api .I resolved it by using the camera1 apis even if the device supports Camera2

Related