czt
2 天以前 b1e6bf7d3ed1a1d64182127f83fdc3fad2233a7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var layer;
var playUrl= null;
var loginTag = false;
 
$(function () {
    layui.use(['layer'], function () {
        layer = layui.layer;
    });
 
    getMedia();
});
 
/**
 * 获取播放信息
 */
function getMedia() {
    var data = {
        id: cameraData.id,
        playType: cameraData.playType
    };
    $.ajax({
        type: 'POST',
        url: "/security/get-media",
        dataType: 'JSON',
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(data),
        success: function (result) {
            if (result.code != "SUCCESS") {
                layer.msg(result.msg);
            } else {
                playUrl = result.playUrl;
                toPlay();
                onLogin();
            }
        },
        error: function (result) {
            layer.msg(result.msg);
        }
    });
}
 
/*============= webRtc视频播放 ----- 开始 ===============*/
/**
 * 开始播放
 * @param winTag  播放窗口
 * @returns {Promise<void>}
 */
async function toPlay() {
    if(playUrl){
        mediaStream = new MediaStream();
        $("#video")[0].srcObject = mediaStream;
        webrtc = new RTCPeerConnection({
            iceServers: [{
                urls: ["stun:stun.l.google.com:19302"]
            }],
            sdpSemantics: "unified-plan"
        });
        webrtc.onsignalingstatechange = signalingstatechange;
 
        webrtc.ontrack = ontrack
        let offer = await webrtc.createOffer({
 
            offerToReceiveAudio: true,
            offerToReceiveVideo: true
        });
        await webrtc.setLocalDescription(offer);
    }
}
 
function ontrack(event) {
    mediaStream.addTrack(event.track);
}
 
async function signalingstatechange() {
    switch (webrtc.signalingState) {
        case 'have-local-offer':
            // let uuid = $('#uuid').val();
            let url = playUrl + "?uuid=" + cameraData.id + "&channel=0";
            $.post(url, {
                data: btoa(webrtc.localDescription.sdp)
            }, function (data) {
                try {
                    console.log(data);
                    webrtc.setRemoteDescription(new RTCSessionDescription({
                        type: 'answer',
                        sdp: atob(data)
                    }))
                } catch (e) {
                    console.warn(e);
                }
 
            });
            break;
        case 'stable':
            break;
        case 'closed':
            break;
        default:
            console.log(`unhandled signalingState is ${webrtc.signalingState}`);
            break;
    }
}
/*============= 视频播放 ----- 结束 ===============*/
 
/**
 *
 * @param command 云台事件类型
 * @param flag 是否停止相应事件
 */
function handPTZ(command, flag){
    if(!loginTag){
        layer.alert("设备云台登录失败,请重试!");
        return;
    }
 
    //大华云台控制
    dhPlayerPTZ(command, flag);
}
 
/**
 * 云台控制 -预置位置调用
 */
function handPreset() {
 
}
 
/*============= 大华JS云台控制 ----- 开始 ===============*/
/**
 * @description 设备登录
 */
function onLogin (){
    onLogout();
    let ip = cameraData.ip;
    let port= cameraData.webPort;
    let user= cameraData.loginId;
    let pwd=  cameraData.pwd;
 
    let target = ip + ':' + port;
    setIP(target);
    /**
     * RPC.login 登录
     * @param {string} $user.value 用户名
     * @param {string} $password.value 密码
     * @param {boolean} false 是否httpOnly,默认false
     * @returns {Promise}
     */
    RPC.login(user, pwd, false).then((res) => {
 
        setCookie('DWebClientSessionID', '', -1);
        setCookie('DhWebClientSessionID', '', -1);
        /**
         * RPC.keepAlive 保活
         */
        RPC.keepAlive(300, 60000, _getSession(), target, 0);
        const browser = BrowserType();
        if (browser.includes('ie')) {
            window.onunload = () => {
                ajax({
                    url: 'global.logout'
                });
            };
        } else if (browser.includes('chrome')) {
            const params = {
                method: 'global.logout',
                params: null,
                id: 10000,
                session: _getSession()
            };
            pubsub.subscribe('onbeforeunload',() => {
                navigator.sendBeacon('/RPC2', JSON.stringify(params));
            });
        } else {
            pubsub.subscribe('onbeforeunload',() => {
                ajax({
                    url: 'global.logout'
                });
            });
        }
        //登录成功,记录登录成功的硬盘录像机
        loginTag = true;
 
    }).catch((err) => {
        console.log(err);
    });
}
/**
 * @description 设备注销
 */
function onLogout (){
    /**
     * RPC.Global.logout 注销接口
     * @returns {Promise}
     */
    RPC.Global.logout().then(function() {
        setLoginState(false);
        playerInstance.forEach(item => {
            if(item) {
                item.stop();
                item.close();
                item = null;
            }
            loginTag = false;
        });
    });
}
 
/**
 * @description 设备云台事件
 * @param {string} type 云台事件类型
 * @param {boolean} isStop 是否停止相应事件
 */
function dhPlayerPTZ (type, isStop) {
 
    //云台步长
    // let stepVal = $_('#h5_ptz_step').value - 0;
    let stepVal = 5;
    let arg2 = 0;
    let arg2Arr = ['LeftUp', 'RightUp', 'LeftDown', 'RightDown'];
    let presetArr = ['GotoPreset','SetPreset', 'ClearPreset'];
    //预置点
    // let presetNum = $_('#h5_preset').value - 0;
    let presetNum =  0;
    if(arg2Arr.indexOf(type) > -1) {
        arg2 = stepVal;
    }
    if(!isStop) {
        if(presetArr.indexOf(type) > -1) {
            /**
             * RPC.PTZManager 云台相关
             * @param {string} 方法
             * @param {number} channel 通道
             * @param {object} 参数集合
             */
            RPC.PTZManager('start', 0, { 'code': type, 'arg1': presetNum, 'arg2': 0, 'arg3': 0 });
        } else {
            RPC.PTZManager('start', 0, { 'code': type, 'arg1': stepVal, 'arg2': arg2, 'arg3': 0 });
        }
    } else {
        RPC.PTZManager('stop', 0, { 'code': type, 'arg1': stepVal, 'arg2': arg2, 'arg3': 0 });
    }
}
/*============= 大华JS云台控制 ----- 结束 ===============*/