<html>
<head>
    <base href="./">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" type="text/css" href="./js/css/dss/edit-or-info-dialog.css">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>视频播放</title>

    <script type="text/javascript" src="./js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="./js/jsw.js"></script>
    <style>
body {
    margin: 0;
}
.form-items{
    /* width: 100%; */
    padding-left: 20px;
    height: calc(100% - 59px);
    overflow-y: scroll;
    background-color: #f9f9f9;
}

.button-submit2 {
    margin-top: 10px;
    width: 100px;
    background-color: #3F51B5;
    color: white;
    height: 36px;
}

.button {
    background-color: #3F51B5;
    color: white;
    height: 36px;
}

.buttons {
    padding-left: 10px;
    width: calc(100% - 10px);
    height: 36px;
}

.form-items {
    padding-left: 0;
    overflow: hidden;
}
    </style>
<body>
    <div class="form-items">
		<div id='id_video' style="width:100%;height:100%;"></div>
    </div>
    <div class="buttons">
        <button class="button-submit2" type="submit" mat-raised-button color="primary" onclick="openVideo('httpflv')">打开视频</button>&nbsp;&nbsp;&nbsp;
        <button class="button-submit2" mat-raised-button color="primary" onclick="closeVideo()">关闭视频</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <button class="button-submit2" mat-raised-button color="primary" onclick="connectCall()">打开对讲</button>&nbsp;&nbsp;&nbsp;
        <button class="button-submit2" mat-raised-button color="primary" onclick="disconnectCall()">关闭对讲</button>&nbsp;&nbsp;&nbsp;
        <input id="id_open_audiorec" type="checkbox" checked="checked" />对讲音频接收&nbsp;&nbsp;&nbsp;
        <input id="id_open_audiosend" type="checkbox" style="margin-left: 20px" checked="checked" />音频发送
    </div>



<script type="text/javascript">
    var hndSession;
    let defaultPUID = 'PU_2098B3A8'
    let x= JSON.parse('<%-JSON.stringify(device_id)%>');
    
    console.log(x);
    var getParams = {
        device_id:x
    }
	$(function(){
		var initParams = {
				url: "http://117.186.28.121:8081", // bv_nginx.exe服务器地址，用于指示库websocket 服务器地址
				calltype: jSW.CallProtoType.HTTP, // AUTO: IE优先使用OCX, 如果希望IE仍然使用HTTP通信, 请使用
				oninit: oninitcall
			}
			jSW.swInit(initParams);
	});

    function oninitcall(rc) {
        if (rc == 0) {
            //库初始化成功
            // 在这之后才能调用其他接口，否则会报错，proto找不到等
            hndLogin();
        } else {
            return console.log("ocx on init fald：" + rc)
        }
    }
		   
	window.onbeforeunload = function(){
		//销毁插件
		jSW.swDeInit();
		return ''; 
	}

    //登录
	function hndLogin() {
        if (hndSession) {
            delete hndSession;
        }
        hndSession = new jSW.SWSession({
            server:"117.186.28.121", // 如果是jSW.CallProtoType.OCX方式，这里需要填写具体的IP
            port: "9701",
            onopen: function (sess) {
                sess.swLogin({
                    user: "admin",
                    password: "123456"
                })
            },
            onclose: function(){
               console.log("与服务器的命令传输 websocket 通道 关闭");
            }
        });

        // 注册事件的回调函数
        hndSession.swAddCallBack('login', sessionCallback);
    }
	    
   function sessionCallback(sender, event, json) {
		if (json.code == jSW.RcCode.RC_CODE_S_OK) {
		   //登录成功延时2秒调用视频
            	// $("#openVideo").removeAttr("disabled");
	
		}else{
		 console.log("登录失败");
		}
	}
	function openVideo(type){
			/*
			* 初始化画面数
            */
            console.log(type, getParams.device_id);
			var szPuid = getParams.device_id;
            var chanel = hndSession.swGetPuChanel(szPuid, 0);
            
            var mediaDir = jSW.MEDIADIR.VIDEORECV; 

            // if(document.getElementById("id_close_audiorec").checked){
            //     mediaDir = mediaDir | jSW.MEDIADIR.AUDIORECV
            // }
			if (chanel) {
				var result = chanel.swOpenEx({
					div:"id_video",
					prototype: type, //rtmp > hls
                    media: 10, //打开视频httpflv,目前只支持音视频同传，之后会改
                    // mediaDir: mediaDir,
					callback: function (options, response) {
					   
					}
				});
			}	
	}
	function closeVideo(){
	  var szPuid = getParams.device_id;
	  var chanel = hndSession.swGetPuChanel(szPuid, 0);
		 //关闭视频
		 chanel.swClose();
    }

    function getSwChanel() {
        var szpuid = getParams.device_id;
        var channelindex = 0;
        var chanel = null;
        if (hndSession) {
            chanel = hndSession.swGetPuChanel(szpuid, channelindex);
        }
        return chanel;
    }

    // function connectCall() {
    //     var chanel = getSwChanel();
    //     var result = chanel.swOpenCommand({
    //         callback: function(opt, response){
    //             if (response.emms.code == jSW.RcCode.RC_CODE_S_OK) {
    //                 console.log('打开喊话成功: ' + result);
    //             }
    //         },
    //         tag: chanel
    //     })

    //     if (result != jSW.RcCode.RC_CODE_S_OK) {
    //         console.log('打开喊话失败: ' + result);
    //     }
    // }

    // function disconnectCall() {
    //     var chanel = getSwChanel();
    //     if(chanel){
    //         var result = chanel.swCloseCommand();
    //         alert('关闭喊话: ' + result);
    //     }
    // }

    function connectCall() {
        var chanel = getSwChanel();
        var rec = document.getElementById('id_open_audiorec').checked;
        var send = document.getElementById('id_open_audiosend').checked;
        if (chanel) {
            var result = chanel.swOpenIntercom({
                isSendAudio: send,
                isRecvAudio: rec,
                callback: function (options, response) {
                    if (response.emms.code == jSW.RcCode.RC_CODE_S_OK) {
                        // alert('打开对讲成功: ' + result);
                    }
                },
                tag: chanel
            });

            if (result != jSW.RcCode.RC_CODE_S_OK) {
                // alert('打开对讲: ' + result);
            }
        } else {
            alert('没有该设备通道');
        }
    }

    function disconnectCall() {
        var chanel = getSwChanel();
        if (chanel) {
            var rc = chanel.swCloseIntercom({
                callback: function (options, response) {
                    // alert('关闭对讲: ' + response.emms.code);
                }
            });
            if (rc != jSW.RcCode.RC_CODE_S_OK) {
                alert('关闭对讲失败: ' + rc);
            }
        }
    }


</script>
</body>
</html>