var yourVideoView = document.getElementById('yourVideo'); //对方视频信息
var myVideoView = document.getElementById('myVideo'); //对方视频信息
var localStream,videoInputDevices=[],currentVideoIndex = 0
$("#startBtn").attr("disabled",false) //注册
$("#logoutBtn").attr("disabled",true) //注销
$("#videoCall").attr("disabled",true)	//视频呼叫
$("#answerCall").attr("disabled",true)	//接听
$("#hangupCall").attr("disabled",true)  //挂断

$(function() {
	mediumInfo()
})
// 初始化
$("#startBtn").click(function(){
	testStart()
	localMediaStream()
	//loginTrue()
})
// 注销
$("#logoutBtn").click(function(){
	// logoutTrue()
	unregisterSip()
})
// 视频呼叫
$("#videoCall").click(function(){
	videoCall()
	// videoCallTrue()
})
// 接听
$("#answerCall").click(function(){
	console.log(localStream)
	sipIncomingRTCSession(incomingSession)
	// answerCallTrue()
})
// 挂断
$("#hangupCall").click(function(){
	hangupCall()
	// userAgent.terminateSessions();
	// hangupCallTrue()
})

function logout(){
	//注销
}

// 打电话
function sipCallRTCSession(e,state){
	console.log(e.session)
	console.log(state)  // 1是呼出，2是呼入
	e.session.on("confirmed", function(data){
		console.log("confirmed")
		console.log(data)
		if(e.session.connection.getReceivers){
			console.log(e.session.connection.getReceivers())
			remoteStream =  new MediaStream();
			e.session.connection.getReceivers().forEach(element => {
			 // track可能一个音轨或者视频轨迹
			 remoteStream.addTrack(element.track)
			})
			// console.log(remoteStream)  confirmed
			yourVideoView.srcObject = remoteStream  
			yourVideoView.onloadedmetadata = function() {
			  yourVideoView.play();
				yourVideoView.muted = true;
				inCallTrue()
				state===1?console.log("呼叫成功"):console.log("接听成功")
			}
		}
	})
}
// 来电
function sipIncomingRTCSession(incomingSession){
	incomingSession.answer({
		'mediaConstraints': {
			'audio': true,
			'video': true
		},
		'mediaStream': localStream
	});
	incomingSession = null
}
//获取摄像头，音频设备信息
function mediumInfo() {
  
  navigator.mediaDevices.enumerateDevices()
    .then(gotDevices).catch(handleError);

  function gotDevices(deviceInfos) {
    deviceInfos.forEach(function(n) {
      if (n.kind === 'videoinput') {
        videoInputDevices.push(n);
      }
    })
  }
  function handleError(error) {
    //alert(JSON.stringify(error));
  }
}
//获取本地媒体流
function localMediaStream() {
	
	console.log(videoInputDevices)
	// if(!videoInputDevices[0].deviceId){
	// 	alert('获取摄像头失败，请重试');
	// 	return
	// }
	navigator.mediaDevices.getUserMedia({
    video: {
      deviceId: videoInputDevices[currentVideoIndex].deviceId
    },
    audio: true
  }).then((stream)=>{
		localStream = stream;
		console.log(localStream)
		myVideoView.srcObject = localStream
		myVideoView.onloadedmetadata = function() {
		  myVideoView.play();
			myVideoView.muted = true
		}
	}).catch((res)=>{
		alert('getUserMedia() error: ' + res.name);
	})
}
// 注册成功
function loginTrue(){
	console.log("初始化成功");
	$("#startBtn").attr("disabled",true) //注册
	$("#logoutBtn").attr("disabled",false) //注销
	$("#videoCall").attr("disabled",false)	//视频呼叫
	$("#answerCall").attr("disabled",true)	//接听
	$("#hangupCall").attr("disabled",true)  //挂断
}
//	注销成功
function logoutTrue(){
	console.log("注销成功");
	$("#startBtn").attr("disabled",false) //注册
	$("#logoutBtn").attr("disabled",true) //注销
	$("#videoCall").attr("disabled",true)	//视频呼叫
	$("#answerCall").attr("disabled",true)	//接听
	$("#hangupCall").attr("disabled",true)  //挂断
}
// 来电振铃
function incomingTrue(){
	console.log("接听成功");
	$("#startBtn").attr("disabled",true) //注册
	$("#logoutBtn").attr("disabled",true) //注销
	$("#videoCall").attr("disabled",true)	//视频呼叫
	$("#answerCall").attr("disabled",false)	//接听
	$("#hangupCall").attr("disabled",false)  //挂断
}
// 通话中
function inCallTrue(){
	$("#startBtn").attr("disabled",true) //注册
	$("#logoutBtn").attr("disabled",true) //注销
	$("#videoCall").attr("disabled",true)	//视频呼叫
	$("#answerCall").attr("disabled",true)	//接听
	$("#hangupCall").attr("disabled",false)  //挂断
}
// 挂断成功
function hangupCallTrue(){
	console.log("挂断成功");
	$("#startBtn").attr("disabled",true) //注册
	$("#logoutBtn").attr("disabled",false) //注销
	$("#videoCall").attr("disabled",false)	//视频呼叫
	$("#answerCall").attr("disabled",true)	//接听
	$("#hangupCall").attr("disabled",true)  //挂断
}
