5

I am working on pjsip video calling app. I want to switch preview camera in an ongoing call.

Here is the code that I am trying.

private void switchCamera(boolean isFront) {
        try {
            int w = (int) SipCallService.currentCall.vidPrev.getVideoWindow()
                    .getInfo().getSize().getW();
            int h = (int) SipCallService.currentCall.vidPrev.getVideoWindow()
                    .getInfo().getSize().getH();
            if (isFront) {
                PjCamera camera = new PjCamera(0, w, h, 0, 0, 0,
                        mSurfaceCapture);
                camera.SwitchDevice(0);
            } else {
                PjCamera camera = new PjCamera(0, w, h, 0, 0, 0,
                        mSurfaceCapture);
                camera.SwitchDevice(1);
            }

        } catch (Exception e) {
            e.printStackTrace();
            showToast("Error while switching camera");
        }
    }

PjCamera is the class provided by pjsip.

I am not able to switch camera using above code.

If there is any other method please guide me to it.

Manoj
  • 2,799
  • 5
  • 30
  • 49
  • I believe that you should use one instance of PjCamera and only call SwitchDevice(0) or SwitchDevice(1). But in your code there is another potential problem: either front or back camera may not support the size **(w,h)**. – Alex Cohn Jan 04 '16 at 22:50
  • @AlexCohn can you please help me with geting instance of PjCamera, I am kind of having hard time to get it. It would be really nice if you can paste a code snippet here – Manoj Jan 05 '16 at 06:03
  • Hi, I'm also looking for the same solution, any luck so far ? – Ajinkya S Feb 19 '16 at 11:25

3 Answers3

1

I used this code, to switch between the front / back cameras.

    int cameraId = isFront? 1 :2;

    CallVidSetStreamParam callVidSetStreamParam = new CallVidSetStreamParam();
    callVidSetStreamParam.setCapDev(cameraId);
    try {
        sipCall.vidSetStream(pjsua_call_vid_strm_op.PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, callVidSetStreamParam);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
user3001001
  • 111
  • 1
  • 3
  • When i using pjsua_call_set_vid_strm to change cap_dev. It take a lot of time and block all connection during switch camera. – Son Pham Sep 10 '19 at 09:02
0

I have never used the pjsip library, but from looking at their source code, this is how your method could be rewritten:

public class PjsipActivity extends Activity {

    PjCamera pjCamera;
    …
    void switchCamera(boolean isFront) {
        if (pjCamera == null) {
            int w = (int) SipCallService.currentCall.vidPrev.getVideoWindow()
                .getInfo().getSize().getW();
            int h = (int) SipCallService.currentCall.vidPrev.getVideoWindow()
                .getInfo().getSize().getH();
            pjCamera = new PjCamera(0, w, h, 0, 0, 0, mSurfaceCapture);
        }

        CameraInfo ci = new CameraInfo();
        for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
            Camera.getCameraInfo(i, ci);
            if (isFront && ci.facing == CameraInfo.CAMERA_FACING_FRONT ||
                !isFront && ci.facing == CameraInfo.CAMERA_FACING_BACK) {
                if (pjCamera.SwitchDevice(i) == 0) {
                    return;
                }
            }
        }
        showToast("Error while switching camera");
    }
}

Note that this snippet does not address the preview size issue. As far as I can judge, the PjCamera does not support changing width and height on the fly. The size of the video window should probably be negotiated between the two peers separately before the session is established. If the front-facing camera or the back-facing camera does not support this preview size, SwitchDevice() will most likely fail with return code -30 but it could also crash or fail silently.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

This is the best method to Switch Camera in PJSIP Android front camera ID=1; back camera ID=2 (In this case only it support these IDs).

 try {

    CallVidSetStreamParam callVidSetStreamParam = new CallVidSetStreamParam();
    callVidSetStreamParam.setCapDev(cameraId);
    currentCall.vidSetStream(pjsua_call_vid_strm_op.PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, callVidSetStreamParam);

    } catch (Exception e) {
        e.printStackTrace();
    }
cosmoonot
  • 2,161
  • 3
  • 32
  • 38