var ws_uri_ = 'wss://12345sp1.jbdu.cn:7443',

	incomingSession = null,
	password = "123456" //密码  //zhumadian12345800100


$("#initBtn").click(function(){
	init()
})
// 初始化
function testStart() {
	
	coolPhone.start();
	coolPhone.on('registered', function(data) {
		if (data.response.status_code === 200) {
			loginTrue()
		}
	});
	coolPhone.on('newRTCSession', function(e) {

		console.log(e.originator)
		console.log(e.session._direction)
		if (e.session._direction == "outgoing") {
			// sipCallRTCSession(e)
			sipCallRTCSession(e, 1)
		}
		if (e.originator == "remote") {
			incomingSession = e.session
			incomingTrue()
			sipCallRTCSession(e, 2)
			// sipIncomingRTCSession(incomingSession) confirmed
		}
		e.session.on("accepted", function() {
			console.log("accepted")
		})
		e.session.on("confirmed", function() {
			console.log("confirmed")
		})
		e.session.on("progress", function() {
			console.log("progress")
		})
		e.session.on("ended", function() {
			hangupCallTrue()
			console.log("ended")
		})

	})
}
// 视频呼叫
function videoCall() {
	// $("#callPhone").val()
	var callNumber = "sip:" + $("#callPhone").val() + "@example.com"
	// var callNumber = $("#callPhone").val().toString()
	coolPhone.call(callNumber, options)
}
// 视频挂断
function hangupCall() {
	coolPhone.terminateSessions();
}
// 注销
function unregisterSip() {
	coolPhone.unregister(true);
	logoutTrue()
}

function init() {
	extNum = $("#registerPhone").val()
	alert(extNum)
	sip_uri_ = "sip:" + extNum + "@12345sp1.jbdu.cn" //  
	var socket = new JsSIP.WebSocketInterface(ws_uri_);
	JsSIP.debug.disable('JsSIP:*')
	configuration = {
		sockets: [socket],
		outbound_proxy_set: ws_uri_,
		contact_uri: 'sip:' + extNum + '@12345sp1.jbdu.cn;transport=wss',
		uri: sip_uri_,
		password: '123456',
		// contact_uri: 'sip:'+extNum+'@'+returnCitySN["cip"]+';transport=wss', 
		register: true, //指示启动时JsSIP用户代理是否应自动注册
		session_timers: false, //启用会话计时器（根据RFC 4028）
		register_expires: 60,
	};
	coolPhone = new JsSIP.UA(configuration);
	// Register callbacks to desired call events

	eventHandlers = {
		'progress': function(e) {
			console.log('call is in progress');
		},
		'failed': function(e) {
			// hangupCallTrue()
			console.log('call failed with cause: ' + e);
		},
		// 'ended': function(e) {
		// 	hangupCallTrue()
		// 	console.log('call ended with cause: ' + e.data);
		// },
		// 'confirmed': function(e) {
		// 	console.log('call confirmed');
		// }
	};
	options = {
		'eventHandlers': eventHandlers,
		'mediaConstraints': {
			'audio': true,
			'video': true
		}
	};
}


// var session = ua.call('sip:bob@example.com', options);
