function WebRtcRespone(){}

WebRtcRespone.handleResponse = function(message) {
//	console.log("webRtcResponse==============：","公用的===");
//	console.log("webRtcResponse==============：",message);
    var signal = JSON.parse(message.data);
    
    //console.log("WebRtcRespone-handleResponse:childframe:"+WebRtcServer.childframe);
    //console.log(signal);
   //console.log("*************videortc signal************************");
	
    var uuid = signal.uuid;
    if(signal.login){
        isLoginOn =  signal.login === 'ok' ;
        //console.log("signal.login:"+signal.login);
        WebRtcServer.serverHeartbeat();
    }
    
    if(signal.getchannel) {}
    
    if(signal.queryrecord) {
        //console.log('got queryrecord resp');
        //console.log(signal.queryrecord);
        WebRtcServer.childframe.BlockUtil.renderRecordResult(signal.queryrecord,signal.uuid);
    }
    
    if(signal.startplayback) {
    	//console.log("got.startplayback callback.........................................send.......");
        var block  = WebRtcServer.childframe.BlockUtil.getBlockByUuid(uuid) ;
 //       console.log(signal.startplayback);
        monid = signal.startplayback.monid;
        block.setMonId(signal.startplayback.monid);
    	//console.log("got.startplayback callback.........................................back........");
    }

    if(signal.controlplayback) {
        //console.log('got controlplayback resp');
        //console.log(signal.controlplayback);
    }
    
    if(signal.stopmonitor) {
        //console.log('got signal.stopmonitor resp');
       // console.log(signal.stopplayback);
        if(signal.stopmonitor.result == 'ok') {
            monid = null;
            peerConnection = null;
            var block  = WebRtcServer.childframe.BlockUtil.getBlockByUuid(signal.uuid) ;
//            var block  = WebRtcServer.childframe.BlockCacher.getBlockByUuid(signal.uuid) ;
           
            block.setMonId(null);
            block.setPeerConnection(null);
            //console.log(block);
        }
    }
    
    if(signal.stopplayback) {
        //console.log('got stopplayback resp');
        //console.log(signal.stopplayback);
       
        if(signal.stopplayback.result == 'ok') {
            monid = null;
            peerConnection = null;
        }
    }
    if(signal.startmonitor) {
    	//console.log("signal.startmonitor callback.........................................startmonitor--send.......");
    	var block  = WebRtcServer.childframe.BlockUtil.getBlockByUuid(uuid) ;
//    	var block  = WebRtcServer.childframe.BlockutilBase.getBlockByUuid(uuid) ;
//    	var block  = WebRtcServer.childframe.BlockCacher.getBlockByUuid(signal.uuid) ; 	
    	//console.log("startmonitor uuid:"+uuid);
        block.setMonId(signal.startmonitor.monid);
        if(WebRtcPanel.queueArray.length){
        	setTimeout(WebRtcPanel.videoPlayDeQueue,300);
        }
        if(signal.startmonitor.stunServer){}
    	//console.log("signal.startmonitor callback.........................................startmonitor--back........")
    }
    
    if(signal.queryprepointlist) {
        //console.log('got queryprepointlist resp');
       // console.log(signal.queryprepointlist);
    }
    
    //一键通话
    if(signal.makecall){
    	console.log('++++++++makecall++++++++++++++++++');
    }
    
    if(signal.sdp) {
 //   	console.log("signal.sdp callback.......................start...........................")
//    	console.log(signal);
    	peerConnection = new RTCPeerConnection();
//    	console.log("WebRtcServer：",WebRtcServer);
//  	  	var block  = WebRtcServer.childframe.BlockUtil.getBlockByMonId(signal.monid);
  		var block  = WebRtcServer.childframe.BlockUtil.getBlockByUuid(signal.uuid);
//    	var block  = WebRtcServer.childframe.BlockutilBase.getBlockByUuid(signal.uuid);
//    	var block  = WebRtcServer.childframe.BlockCacher.getBlockByUuid(signal.uuid) ;

  	  	monid  = signal.monid;
 // 	  	console.log("block：",block)
  	  	//console.log("signal.uuid:"+signal.uuid)
  	  	//console.log("block.uuid:"+block.uuid)
  	  	block.setRunning(true).setPeerConnection(peerConnection);
  	  	
  		peerConnection.onicecandidate =  function gotIceCandidate(event) {
  		   // console.log('got ice');
  		   // console.log(event);
  		   // console.log(event.candidate);
  		    var uuid = event.uuid;
  		    if(event.candidate != null) {
  		        serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid, 'monid': signal.monid}));
  		    }
  		}

  	  	peerConnection.onaddstream =  function gotRemoteStream(event) {
	  	  	//console.log('got remote stream' + event.stream);
	  		//console.log('--------------------------------------------------------------------------------------------------');
	  		//console.log('------------------------------------------*******************--------------------------------------');
	  	  	try {
		  			block.getVideoComp().srcObject = event.stream;
			} catch (error) {
				block.getVideoComp().src = window.URL.createObjectURL(event.stream);
			}
//	  		console.log(block);
//	  		console.log(block.uuid);
//	  		console.log('------------------------------------------*******************--------------------------------------');
//	  		console.log('--------------------------------------------------------------------------------------------------');
	  	}

  	  	peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
  	  		if(signal.sdp.type == 'offer') {
  	  			peerConnection.createAnswer().then(
  	  				function(description) {
	  	  			    //console.log('got description');
	  	  			    peerConnection.setLocalDescription(description).then(function() {
	  	  			        serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid, 'monid': signal.monid}));
	  	  			    }).catch(WebRtcRespone.errorHandler);
  	  				}
  	  			
  	  			).catch(WebRtcRespone.errorHandler);
  	  		}
  	  	}).catch(WebRtcRespone.errorHandler);
  	  	
  	  //console.log("signal.sdp callback.......................start2...........................")
     }
}

//WebRtcRespone.gotIceCandidate = function(event) {
//    console.log('got ice');
//    console.log(event);
//    console.log(event.candidate);
//    var uuid = event.uuid;
//    if(event.candidate != null) {
//        serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid, 'monid': monid}));
//    }
//}

//WebRtcRespone.createdDescription = function(description) {
//    console.log('got description');
//
//    peerConnection.setLocalDescription(description).then(function() {
//        serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid, 'monid': monid}));
//    }).catch(WebRtcRespone.errorHandler);
//}

//WebRtcRespone.gotRemoteStream = function(event) {
//	console.log('got remote stream' + event.stream);
//	console.log('--------------------------------------------------------------------------------------------------');
//	console.log('------------------------------------------*******************--------------------------------------');
//    var videoblock = WebRtcServer.childframe.BlockUtil.getBlockByPeerConnection(event.currentTarget);
//	videoblock.getVideoComp().src = window.URL.createObjectURL(event.stream);
//	console.log(videoblock);
//	console.log("got remove stream video tag: "+ $(videoblock.getVideoComp()).attr("id"))
//	console.log('------------------------------------------*******************--------------------------------------');
//	console.log('--------------------------------------------------------------------------------------------------');
//}

WebRtcRespone.errorHandler = function(error) {
    console.log(error);
}