Search code examples
iphonesippjsip

Switching camera from front to back in pjsip iPhone


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.

pjsua_call_vid_strm_op_param param;
pjsua_call_vid_strm_op_param_default(&param);
param.cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
pj_status_t status = pjsua_call_set_vid_strm(_current_call, 
PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, &param);
if (status == PJ_SUCCESS)
{
   NSLog(@"Toggle");
}

I am not able to switch camera using above code.

I have also added below code before adding account in pjsip

acc_cfg.vid_in_auto_show = PJ_TRUE;
acc_cfg.vid_out_auto_transmit = PJ_TRUE;
acc_cfg.vid_cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
acc_cfg.vid_rend_dev = PJMEDIA_VID_DEFAULT_RENDER_DEV;

pjmedia_orient orient;
orient = PJMEDIA_ORIENT_ROTATE_90DEG;
pjsua_vid_dev_set_setting(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, 
PJMEDIA_VID_DEV_CAP_ORIENTATION, &orient, PJ_TRUE);

If there is any other method please guide me.


Solution

  • pjsua_call_set_vid_strm() is the correct method already, just try to change the capture device ID (i.e: param.cap_dev). Note that PJMEDIA_VID_DEFAULT_CAPTURE_DEV means the default capture device, which on iPhone normally is the front facing camera.

    You can use pjsua_vid_dev_count() and pjsua_vid_dev_get_info() to enumerate available video devices. Capture devices should have direction info (pjmedia_vid_dev_info.dir) set to PJMEDIA_DIR_CAPTURE. And device name info (pjmedia_vid_dev_info.name) should tell which one is the back camera.