var eventHandlers, options;

$(document).ready(function() {
	init()
})
function testStart() {
	//成功注册成功
	userAgent.on('registered', function(data) {
		if (data.response.status_code === 200) {
			loginTrue()
		}
	});
	//注册失败
	userAgent.on('registrationFailed', function(data) {
		console.log("registrationFailed, ", data); 
	});
	//为传入或传出会话/呼叫激发。data:
	userAgent.on('newRTCSession', function(data) {
		console.info('onNewRTCSession: ', data);
		if(data.session._direction == "outgoing") {
			sipCallRTCSession(data, 1)	//1是呼出
		}
		if(data.originator == "remote") {
			sipCallRTCSession(data, 2)	//2是呼入
			// sipIncomingRTCSession(incomingSession) confirmed
		}
		//接受呼叫时激发
		data.session.on('accepted', function(data) {
			console.info("3");
			if(data.originator == 'remote' && currentSession == null) {
				currentSession = incomingSession;
				incomingSession = null;
			}
		});
		//确认呼叫后激发
		data.session.on('confirmed', function(data) {
			console.info("4");
			$(".callStyle").text("通话中");

			if(data.originator == 'remote' && currentSession == null) {
				currentSession = incomingSession;
				incomingSession = null;
			}
		});
		//在将远程SDP传递到RTC引擎之前以及在发送本地SDP之前激发。此事件提供了修改传入和传出SDP的机制。
		data.session.on('sdp', function(data) {
			console.info("5");
		});
		//接收或生成对邀请请求的1XX SIP类响应（>100）时激发。该事件在SDP处理之前触发（如果存在），以便在需要时对其进行微调，甚至通过删除数据对象中响应参数的主体来删除它
		data.session.on('progress', function(data) {
			console.info("6");
			if(data.originator == 'remote') {}
		});
	});
}
function init() {
	sip_uri_ = "sip:" + selectExten + "@12345sp1.jbdu.cn"; // 12345sp.zwfw.anyang.gov.cn
	sip_password_ = "123456"; //zhumadian12345800100
	ws_uri_ = "wss://12345sp1.jbdu.cn:7443";
	console.info("get input info: sip_uri = ", sip_uri_, " sip_password = ", sip_password_, " ws_uri = ", ws_uri_);
	var socket = new JsSIP.WebSocketInterface(ws_uri_);
	var configuration = {
		sockets: [socket],
		outbound_proxy_set: ws_uri_,
		uri: sip_uri_, //与用户代理关联的SIP URI（字符串）。这是您的提供商提供给您的SIP地址
		password: sip_password_, //SIP身份验证密码
		contact_uri: 'sip:' + selectExten + '@12345sp1.jbdu.cn;transport=wss',
		register: true, //指示启动时JsSIP用户代理是否应自动注册
		session_timers: false, //启用会话计时器（根据RFC 4028）
	};
	userAgent = new JsSIP.UA(configuration);

	eventHandlers = {
		'progress': function(e) {
			console.log('call is in progress');
		},
		'failed': function(e) {
			console.log('call failed: ', e);
			callVideoFail()
		},
		'ended': function(e) {
			console.log('call ended : ', e);
			dropCall();
		},
		'confirmed': function(e) {
			console.log('call confirmed : ', e);
		}
	};
	options = {
		'eventHandlers': eventHandlers,
		'mediaConstraints': {
			'audio': true,
			'video': true
		},
		'mediaStream': localStream
	};
}