OpenVidu-Android - Change cameras

Hello,

I am trying to understand the documentation of switching cameras. By understading, I mean trying to implement it in OpvenVidu-Andriod Demo.

For more than a week I have been stuck on this. I dont quite coperhand the documentation. What I have noticed, but correct me if I am wrong, the documentation is made for the whole OpenVidu, not exactly for Android version or whatever.

Can someone point me, more exactly, what do I need to do, so I can switch cameras but not disconnecting from the session.

What I have tried, maybe pointless and off topic (have a normal switch in activity_main.xml) - in SessionActivity, in function getTokenSuccess:

localParticipant.startCamera(false);
    Switch onOffSwitch = (Switch)  findViewById(R.id.cameraSwitch);
    onOffSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
        Log.v("Switch State= ", ""+isChecked);
        localParticipant.startCamera(isChecked);
    });

And then in localParticipant I have started to work with createCameraCapturer (passing the isChecked from the switch via startCamera)

for(String deviceName : deviceNames){
            if(!switchCamera){
                if (enumerator.isFrontFacing(deviceName)) {
                    videoCapturer = enumerator.createCapturer(deviceName, null);
                    if (videoCapturer != null) {
                        return videoCapturer;
                    }
                }
            }else{
                if (enumerator.isBackFacing(deviceName)) {
                    videoCapturer = enumerator.createCapturer(deviceName, null);
                    if (videoCapturer != null) {
                        return videoCapturer;
                    }
                }
            }
    }
    return null;

Also from what I read online, I need to release the camera somehow; I saw that in Camera2Enumerator, it’s calling some CameraManager functions from android api. But that file is readonly.

Either way, if someone has a good example or can point me in the right direction. I would be gratefull.

Also, I have read something about publishers too, but again, I cannot see how they are implemented in OpenVidu-Android.

Thank you a lot!

Hi

I recently figured this out, my post here: Switch Camera Track Count Is Wrong On iPhone - #23 by vipin_mishra

What I ended up doing was subscribing to the event when a subscriber has connected and then get the devices available, look for the front and back camera items in the array and store in variables the device Ids

When it came to switching I’d keep track of whether the front or back camera is currently being used and use the opposite id to switch the camera.

1 Like

Alright,
I understood the doc now and made it work it’s quite simple.

Small tip for the future if anyone wants to switch cameras in android using openvidu
at videosource in creating a new publisher
videoSource: isFrontCamera ?
videoDevices[0].deviceId :
videoDevices[videoDevices.length - 1].deviceId,

Since getDevices for some reasons after you put the filter of ‘videoinput’ sorts them like camera 1, back camera (labels)…camera 2, back camera… and in the end is camera 0, front camera;
This works for samsuns s10 plus and samsung A51 and 71.

Cheers!
if i didnt got explicit enough pls dm me and i will try to help you :slight_smile: