3

I have used libjingle library for webrtc android application. I have successfully implemented audio video streaming for two way communication.

Till now i was using front camera for video streaming but now i want to put option for users to select front or back camera for video streaming.

How can i archive it? i have no idea about this.

I have tried VideocaptureAndroid switch camera method but not working.

If anyone one knows then help me out for this functionality?

Thanks in advance.

adit microsys
  • 319
  • 3
  • 13

5 Answers5

5

You need to use the same videoCapturer object, which is created while initial MediaStream creation.

CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
cameraVideoCapturer.switchCamera(null);

AppRTC Reference

Ajay
  • 2,483
  • 2
  • 16
  • 27
  • Thank you for answer. Right now this class totally changed in new webrtc libjingle library. So this solution not working. – adit microsys Apr 20 '17 at 09:32
  • Its working for me and it should work with latest code base. Try building AppRTC demo app and check switch camera. Either your jingle library is old or your doing something wrong. Try with my framework https://github.com/AjayChoudary/WebRTCFramework – Ajay Apr 22 '17 at 16:41
  • Tried this on the native WebRtc for android and it works! – Jaswant Singh Jun 04 '20 at 08:09
  • it is cycling through cameras, tested on samsung a30 and it is switching 2 times in front facing – Rahul Gaur Jan 07 '22 at 09:35
0

Using this version: org.webrtc:google-webrtc:1.0.22672

Create the VideoCapturer by this method:

VideoCapturer videoCaptor = createCameraCaptor(new Camera1Enumerator(false));

The trick is on isBackFacing(...)/ isFrontFacing(...)

private VideoCapturer createCameraCaptor(CameraEnumerator enumerator) {
    final String[] deviceNames = enumerator.getDeviceNames();

    // First, try to find back facing camera
    Logging.d(TAG, "Looking for back facing cameras.");
    for (String deviceName : deviceNames) {
        if (enumerator.isBackFacing(deviceName)) {
            Logging.d(TAG, "Creating back facing camera captor.");
            VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);

            if (videoCapturer != null) {
                return videoCapturer;
            }
        }
    }

    // back facing camera not found, try something else
    Logging.d(TAG, "Looking for other cameras.");
    for (String deviceName : deviceNames) {
        if (!enumerator.isBackFacing(deviceName)) {
            Logging.d(TAG, "Creating other camera captor.");
            VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);

            if (videoCapturer != null) {
                return videoCapturer;
            }
        }
    }

    return null;
}
Amos
  • 2,222
  • 1
  • 26
  • 42
0

Here is an example using libjingle.
If you want to switch between the front and rear camera you will need to get the name of the device you want to use. This can be done using VideoCapturerAndroid.getNameOfFrontFacingDevice() or VideoCapturerAndroid.getNameOfRearFacingDevice() depending on whether you want to use the front or rear camera.

Here's a simple example of how to get the correct VideoCapturer using io.pristine.libjingle:9127

private VideoCapturer getCameraCapturer(boolean useFrontCamera) {
    String deviceName = useFrontCamera ? VideoCapturerAndroid.getNameOfFrontFacingDevice() : VideoCapturerAndroid.getNameOfBackFacingDevice();
    return VideoCapturerAndroid.create(deviceName);
}

If you're using a different version of LibJingle or for any reason this doesn't work let me know and I'll be happy to help!

Cheers,

A. Watson
  • 83
  • 2
  • 9
0

Create a new video capturer and start it. Dont forget to stop the old one.

fun switchCamera() {
    cameraFacingFront = !cameraFacingFront
    try {
        videoCapturer!!.stopCapture()
    } catch (e: InterruptedException) {
    }
    videoCapturer = createVideoCapturer(cameraFacingFront)
    videoCapturer!!.initialize(
        surfaceTextureHelper,
        activity,
        videoSource!!.getCapturerObserver()
    )
    videoCapturer!!.startCapture(
        VIDEO_SIZE_WIDTH,
        VIDEO_SIZE_HEIGHT,
        VIDEO_FPS
    )
}
user1942887
  • 94
  • 1
  • 5
0

The fact is that if the device has several front and/or several back cameras, the switchCamera(null) method will switch between all of them.

For correct operation, you must specify the name of the selected camera.

val nextDevice = if (isFrontCameraSelected) frontCameraName else backCameraName
videoCapturer?.switchCamera(null, nextDevice)

To determine the camera names:

Camera1Enumerator().run {
    deviceNames.forEach {
        if (frontCameraName == null && isFrontFacing(it)) {
            frontCameraName = it
        }
        if (backCameraName == null && isBackFacing(it)) {
            backCameraName = it
        }
    }
}
Denys Makhov
  • 362
  • 5
  • 7