Switch camera front to back in pjsip android

I am working on a pjsip video application. I want to switch the preview camera during the current call.

Here is the code I'm trying to do.

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 a class provided by pjsip.

I cannot switch the camera using the code above.

If there is any other method, please help me.

+4
source share
2 answers

I never used the pjsip library , but looking at their source code , here is how your method can 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");
    }
}

, . , PjCamera . , , . , SwitchDevice() -30, .

0

/ .

    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();
    }
0

Source: https://habr.com/ru/post/1622753/


All Articles