main.js
17.1 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// //////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016-present All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// http://www.apache.org/licenses/LICENSE-2.0
//
// Github Home: https://github.com/AlexWang1987
// Author: AlexWang
// Date: 2017-05-20 16:54:13
// QQ Email: 1669499355@qq.com
// Last Modified time: 2017-05-26 18:44:59
// Description: saas-main
//
// //////////////////////////////////////////////////////////////////////////////
import Emiter from './Emiter.js';
const e = new Emiter();
function utf16to8(str) {
var out, i, len, c;
len = str.length;
out = new Uint8Array(len * 3);
var utf8len = 0;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out[utf8len++] = c;
} else if (c > 0x07FF) {
out[utf8len++] = (0xE0 | ((c >> 12) & 0x0F));
out[utf8len++] = (0x80 | ((c >> 6) & 0x3F));
out[utf8len++] = (0x80 | ((c >> 0) & 0x3F));
} else {
out[utf8len++] = (0xC0 | ((c >> 6) & 0x1F));
out[utf8len++] = (0x80 | ((c >> 0) & 0x3F));
}
}
return new Uint8Array(out.buffer, 0, utf8len);
}
function utf8to16(str) {
var out, i, len, c;
var char2, char3;
out = "";
len = str.length;
i = 0;
while (i < len) {
c = str[i++];
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = str[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str[i++];
char3 = str[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
const CODE = [
"MOV_START_SUC",
"MOV_START_FAIL",
"MOV_STOP_SUC",
"MOV_STOP_FAIL",
"MOV_PAUSE_SUC",
"MOV_PAUSE_FAIL",
"MOV_RESUME_SUC",
"MOV_RESUME_FAIL",
"MOV_SEEK_SUC",
"MOV_SEEK_FAIL",
"MOV_PROPERTY",
"SS_START_SUC",
"SS_START_FAIL",
"SS_STOP_SUC",
"SS_STOP_FAIL",
"REC_START_SUC",
"REC_START_FAIL",
"REC_STOP_SUC",
"REC_STOP_FAIL",
"REC_PAUSE_SUC",
"REC_PAUSE_FAIL",
"REC_RESUME_SUC",
"REC_RESUME_FAIL",
"MOV_PLAY_FINISH",
"MOV_VERSION",
"SS_VERSION",
"REC_VERSION",
"MOV_SELECT_FILE_CANCEL",
"QAV_LOGIN_SUC",
"QAV_LOGIN_FAIL",
"QAV_LOGOUT_SUC",
"QAV_LOGOUT_FAIL",
"QAV_ENTERROOM_SUC",
"QAV_ENTERROOM_FAIL",
"QAV_LEAVEROOM_SUC",
"QAV_LEAVEROOM_FAIL",
"QAV_OPEN_MIC_SUC",
"QAV_OPEN_MIC_FAIL",
"QAV_OPEN_MIC_NODEV",
"QAV_CLOSE_MIC_SUC",
"QAV_CLOSE_MIC_FAIL",
"QAV_CLOSE_MIC_NODEV",
"QAV_OPEN_PLAYER_SUC",
"QAV_OPEN_PLAYER_FAIL",
"QAV_OPEN_PLAYER_NODEV",
"QAV_CLOSE_PLAYER_SUC",
"QAV_CLOSE_PLAYER_FAIL",
"QAV_CLOSE_PLAYER_NODEV",
"QAV_MUTE_AUDIO_SUC",
"QAV_MUTE_AUDIO_FAIL",
"QAV_UNMUTE_AUDIO_SUC",
"QAV_UNMUTE_AUDIO_FAIL",
"REC_SAVE_FILE_CANCEL",
"GET_MIC_SUC",
"GET_MIC_FAIL",
"DS_START_SUC",
"DS_START_FAIL",
"DS_STOP_SUC",
"DS_STOP_FAIL",
"AUD_START_SUC",
"AUD_START_FAIL",
"AUD_STOP_SUC",
"AUD_STOP_FAIL",
"AUD_DATA",
"SS_DATA",
"MOV_DATA",
"PEN_START_SUC",
"PEN_START_FAIL",
"PEN_STOP_SUC",
"PEN_STOP_FAIL",
"PEN_VERSION",
"QAV_DEVICE_SUC",
"QAV_DEVICE_FAIL",
"CBD_QUERY_SUC",
"CBD_QUERY_FAIL",
"CBD_GET_SUC",
"CBD_GET_FAIL",
"FILE_GET_SUC",
"FILE_GET_FAIL",
"FILE_SET_SUC",
"FILE_SET_FAIL",
"RESTART_SUC",
"RESTART_FAIL",
"GET_CAM_SUC",
"GET_CAM_FAIL",
"CAM_START_SUC",
"CAM_START_FAIL",
"CAM_STOP_SUC",
"CAM_STOP_FAIL",
"SWITCH_SCENE_SUC",
"SWITCH_SCENE_FAIL",
"PUSH_STREAM_FAIL",
"SHOW_UI_SUC",
"SET_VIDEO_PARAM_SUC",
"SET_VIDEO_PARAM_FAIL",
"GET_VIDEO_PARAM_SUC",
"GET_VIDEO_PARAM_FAIL",
"START_RECORD_SUC",
"START_RECORD_FAIL",
"STOP_RECORD_SUC",
"STOP_RECORD_FAIL",
"START_PUSH_SUC",
"START_PUSH_FAIL",
"STOP_PUSH_SUC",
"STOP_PUSH_FAIL",
"GET_RTMP_PORT_SUC",
"GET_RTMP_PORT_FAIL",
"SET_MIC_VOL_SUC",
"SET_MIC_VOL_FAIL",
"GET_MIC_VOL_SUC",
"GET_MIC_VOL_FAIL",
"RTMP_CONNECTING",
"RTMP_CONNECT_SUC",
"RTMP_CONNECT_FAIL",
"RTMP_CONNECT_CLOSED"
];
function sendCommand(cmdName, cmdParams = '') {
var cmd = new Object();
cmd.data_len = 0;
cmd.data = "";
cmd.name = cmdName;
if (cmdName == "mov_start") {
cmd.type = 1;
} else if (cmdName == "mov_stop") {
cmd.type = 2;
} else if (cmdName == "mov_pause") {
cmd.type = 3;
// var pos = document.getElementById("mov_pos").value;
pos = cmdParams;
if (pos.length > 0) {
cmd.data_len = 4;
var buffer = new ArrayBuffer(cmd.data_len);
var Uint32param = new Uint32Array(buffer, 0, 1);
Uint32param[0] = parseInt(pos);
cmd.data = new Uint8Array(buffer, 0, 4);
}
} else if (cmdName == "mov_resume") {
cmd.type = 4;
} else if (cmdName == "mov_seek") {
cmd.type = 5;
// var pos = document.getElementById("mov_pos").value;
pos = cmdParams;
if (pos.length > 0) {
cmd.data_len = 4;
var buffer = new ArrayBuffer(cmd.data_len);
var Uint32param = new Uint32Array(buffer, 0, 1);
Uint32param[0] = parseInt(pos);
cmd.data = new Uint8Array(buffer, 0, 4);
}
} else if (cmdName == "ss_start") {
cmd.type = 6;
// var param = document.getElementById("ss_start_input");
// param = cmdParams;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "ss_stop") {
cmd.type = 7;
} else if (cmdName == "ss_version") {
cmd.type = 14;
} else if (cmdName == "get_mic") {
cmd.type = 26;
} else if (cmdName == "ds_start") {
cmd.type = 27;
cmd.data_len = 4 * 6 + 256 + 256;
var buffer = new ArrayBuffer(cmd.data_len);
var int32param = new Uint32Array(buffer, 0, 6);
// var coordstr = document.getElementById("ds_start_coord").value;
coordstr = cmdParams;
var coordarray = coordstr.split(",");
if (coordarray.length == 4) {
int32param[0] = parseInt(coordarray[0]);
int32param[1] = parseInt(coordarray[1]);
int32param[2] = parseInt(coordarray[2]);
int32param[3] = parseInt(coordarray[3]);
} else {
//promopt error parameter
return; //
}
var window_name_buffer = new Uint8Array(buffer, 24, 256);
// var window_name = document.getElementById("ds_start_window_name").value;
window_name = cmdParams;
//var window_name_array = new TextEncoder("utf-8").encode(window_name);
var window_name_array = utf16to8(window_name);
for (var i = 0; i < window_name_array.length; i++) {
window_name_buffer[i] = window_name_array[i];
}
int32param[4] = window_name_array.length;
int32param[5] = 0;
var url_name = new Uint8Array(buffer, 24 + 256, 256); //todo: set push url
cmd.data = new Uint8Array(buffer, 0, cmd.data_len);
_sendCommand(cmd);
return;
} else if (cmdName == "ds_stop") {
cmd.type = 28;
} else if (cmdName == "aud_start") {
cmd.type = 29;
cmd.data_len = 4 + 4 + 256 + 256;
var buffer = new ArrayBuffer(cmd.data_len);
var mic_name_len = new Uint32Array(buffer, 0, 1);
var sel_mic_name = cmdParams;
mic_name_len[0] = sel_mic_name.length;
var urlname_len = new Uint32Array(buffer, 4, 1);
urlname_len[0] = 0;
var mic_name = new Uint8Array(buffer, 8, 256);
var mic_name_array = utf16to8(sel_mic_name);
for (var i = 0; i < mic_name_array.length; i++) {
mic_name[i] = mic_name_array[i];
}
var url_name = new Uint8Array(buffer, 8 + 256, 256);
cmd.data = new Uint8Array(buffer, 0, cmd.data_len);
_sendCommand(cmd);
return;
} else if (cmdName == "aud_stop") {
cmd.type = 30;
} else if (cmdName == "get_cam") {
cmd.type = 41;
} else if (cmdName == "cam_start") {
cmd.type = 42;
cmd.data_len = 4 + 4 + 256 + 256;
var buffer = new ArrayBuffer(cmd.data_len);
var cam_name_len = new Uint32Array(buffer, 0, 1);
var sel_cam_name = cmdParams;
cam_name_len[0] = sel_cam_name.length;
var urlname_len = new Uint32Array(buffer, 4, 1);
urlname_len[0] = 0;
var cam_name = new Uint8Array(buffer, 8, 256);
//var cam_name_array = new TextEncoder("utf-8").encode(sel_cam_name);
var cam_name_array = utf16to8(sel_cam_name);
for (var i = 0; i < cam_name_array.length; i++) {
cam_name[i] = cam_name_array[i];
}
var url_name = new Uint8Array(buffer, 8 + 256, 256); //todo: set push url
cmd.data = new Uint8Array(buffer, 0, cmd.data_len);
_sendCommand(cmd);
return;
} else if (cmdName == "cam_stop") {
cmd.type = 43;
} else if (cmdName == "set_videoparam") {
cmd.type = 46;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "get_videoparam") {
cmd.type = 47;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "start_record") {
cmd.type = 48;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "stop_record") {
cmd.type = 49;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "start_push") {
cmd.type = 50;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "stop_push") {
cmd.type = 51;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "get_rtmp_port") {
cmd.type = 52;
} else if (cmdName == "set_mic_vol") {
cmd.type = 53;
cmd.data = utf16to8(cmdParams);
cmd.data_len = cmd.data.length;
} else if (cmdName == "get_mic_vol") {
cmd.type = 54;
}
_sendCommand(cmd);
}
function _sendCommand(wsCmd) {
var cmd_buf_len = 2 + 1 * 2 + 4 + wsCmd.data_len;
var buffer = new ArrayBuffer(cmd_buf_len);
var cmd = new Uint16Array(buffer, 0, 1);
var byte_order = new Uint8Array(buffer, 2, 1);
var data_len = new Uint32Array(buffer, 4, 1);
cmd[0] = wsCmd.type;
byte_order[0] = 0;
data_len[0] = wsCmd.data_len;
if (wsCmd.data_len > 0) {
var data_buf = new Uint8Array(buffer, 8, wsCmd.data_len);
for (var i = 0; i < wsCmd.data_len; i++) {
data_buf[i] = wsCmd.data[i];
}
}
if (ws) ws.send(buffer);
}
let movieInfo = null;
let versionInfo = '';
let cameraInfo = [];
let micInfo = [];
let videoInfo = '';
let rtmpPort = '';
let micVol = '';
let ws = null;
let host = '127.0.0.1';
let port = 8090;
let wstimer = 0;
function clear() {
try {
if (ws) {
ws.close();
ws = ws.onopen = ws.onclose = ws.onerror = ws.onmessage = null;
e.emit(e.CLOSE);
}
} catch (er) {
console.log(er);
}
}
function stop() {
clear();
}
function start() {
if (ws) {
clear();
}
ws = new WebSocket('ws://' + host + ':' + port);
wstimer = setTimeout(() => {
clearTimeout(wstimer);
clear();
}, 1500)
ws.timeout = 1500;
ws.binaryType = 'arraybuffer';
ws.onopen = () => {
clearTimeout(wstimer);
sendCommand("get_rtmp_port");
sendCommand("get_mic");
sendCommand("get_cam");
sendCommand("get_mic_vol");
e.emit(e.OPEN);
}
ws.onclose = () => {
clear();
};
ws.onerror = () => {
clear();
}
ws.onmessage = (evt) => {
const msg = evt.data;
if (typeof (evt.data) === "string") {
console.log('on message [string] ignore.');
} else {
var dv = new DataView(msg);
var result = dv.getUint16(0, true);
console.log('onmessage code->', result);
var byte_order = dv.getUint8(2, true);
var data_len = dv.getUint32(4, true);
if (result >= 0 && result < CODE.length) {
var resultString = CODE[result];
} else {}
if (0 == result || 6 == result || 8 == result) { //mov_start,mov_pause,mov_seek success
console.log('mov_start,mov_pause,mov_seek success');
//playRTMPStream("rtmp://127.0.0.1:" + rtmp_port + "/live/ms");
} else if (10 == result) { //movie property
if (data_len > 7 * 4 + 8) {
var uint32params = new Uint32Array(msg, 8, 8);
var info = "width:" + uint32params[0] + " height:" + uint32params[1];
info = "framerate:" + uint32params[2] + " duration:" + uint32params[3];
info = "playpos:" + uint32params[4] + " filesize:" + uint32params[5];
movieInfo = info;
var file_name_len = uint32params[6];
var file_name = new Uint8Array(msg, 40, file_name_len);
var file_name_string = utf8to16(file_name);
movieInfo.filename = file_name_string;
e.emit(e.MOVIE_INFO_CHANGE, movieInfo);
}
} else if (11 == result) {
console.log('ss_start success');
e.emit(e.SS_START);
} else if (24 <= result && 26 >= result) {
if (data_len >= 4) {
var version = dv.getUint32(8, true);
versionInfo = version.toString();
e.emit(e.VERSION_INFO_CHANGE, versionInfo);
}
} else if (55 == result) { //ds_start success
console.log('ds_start success');
//playRTMPStream("rtmp://127.0.0.1:" + rtmp_port + "/live/ds");
} else if (59 == result) { //as start success
console.log('as start success');
//playRTMPStream("rtmp://127.0.0.1:" + rtmp_port + "/live/as");
} else if (result == 83) { //get camera sucess
console.log('get camera sucess');
if (data_len > 4) {
var cam_num = dv.getUint32(8, true);
var offset = 12;
cameraInfo.length = 0;
for (var i = 0; i < cam_num; i++) {
var cam_name_len = dv.getUint32(offset, true);
offset += 4;
var cam_name = new Uint8Array(msg, offset, cam_name_len - 1);
offset += cam_name_len;
//var data_sting = new TextDecoder("utf-8").decode(cam_name);
var data_sting = utf8to16(cam_name);
cameraInfo.push(data_sting)
}
e.emit(e.CAMERA_INFO_CHANGE, cameraInfo);
}
} else if (53 == result) { //get mic success
if (data_len > 4) {
var mic_num = dv.getUint32(8, true);
var offset = 12;
micInfo.length = 0;
for (var i = 0; i < mic_num; i++) {
var mic_name_len = dv.getUint32(offset, true);
offset += 4;
var mic_name = new Uint8Array(msg, offset, mic_name_len - 1);
offset += mic_name_len;
//var data_sting = new TextDecoder("utf-8").decode(mic_name);
var data_sting = utf8to16(mic_name);
micInfo.push(data_sting);
}
e.emit(e.MIC_INFO_CHANGE, micInfo);
}
} else if (85 == result) { //cam_share success
console.log('cam_share success');
//playRTMPStream("rtmp://127.0.0.1:" + rtmp_port + "/live/cs");
} else if (95 == result) { //get_videoparam success
console.log('get_videoparam success');
var data = new Uint8Array(msg, 8, msg.byteLength - 8 - 1);
//var data_sting = new TextDecoder("utf-8").decode(data);
var data_sting = utf8to16(data);
videoInfo = data_sting;
e.emit(e.VIDEO_INFO_CHANGE, videoInfo);
} else if (105 == result) { //get_rtmp_port_success
console.log('get_rtmp_port_success');
var data = new Uint8Array(msg, 8, msg.byteLength - 8 - 1);
//var data_sting = new TextDecoder("utf-8").decode(data);
var data_sting = utf8to16(data);
rtmpPort = data_sting;
e.emit(e.RTMP_PORT_CHANGE, rtmpPort);
} else if (109 == result) { //get_mic_vol success
console.log('get_mic_vol success');
var data = new Uint8Array(msg, 8, msg.byteLength - 8 - 1);
//var data_sting = new TextDecoder("utf-8").decode(data);
var data_sting = utf8to16(data);
micVol = data_sting;
e.emit(e.MIC_VOL_CHANGE, micVol);
} else if (111 <= result && 114 >= result) {
var data = new Uint8Array(msg, 8, msg.byteLength - 8);
var url = utf8to16(data);
console.log('url->', url);
} else if (13 == result) {
e.emit(e.SS_STOP);
} else {
var data = new Uint8Array(msg, 8, msg.byteLength - 8);
//var data_sting = new TextDecoder("utf-8").decode(data);
var data_sting = utf8to16(data);
console.log('data ->', data_sting);
}
}
}
}
e.infos = { movieInfo, versionInfo, cameraInfo, micInfo, videoInfo, rtmpPort, micVol }
e.MOVIE_INFO_CHANGE = 'MOVIE_INFO_CHANGE';
e.VERSION_INFO_CHANGE = 'VERSION_INFO_CHANGE';
e.CAMERA_INFO_CHANGE = 'CAMERA_INFO_CHANGE';
e.MIC_INFO_CHANGE = 'MIC_INFO_CHANGE';
e.VIDEO_INFO_CHANGE = 'VIDEO_INFO_CHANGE';
e.RTMP_PORT_CHANGE = 'RTMP_PORT_CHANGE';
e.MIC_VOL_CHANGE = 'MIC_VOL_CHANGE';
e.SS_START = 'SS_START';
e.SS_STOP = 'SS_STOP';
e.OPEN = 'OPEN';
e.CLOSE = 'CLOSE';
e.ERROR = 'ERROR';
e.getRTMPPort = () => {
sendCommand('get_rtmp_port');
}
e.getMicInfo = () => {
sendCommand('get_mic');
}
e.getCamInfo = () => {
sendCommand('get_cam');
}
e.getMicVol = () => {
sendCommand('get_mic_vol');
}
e.start = (_host = '127.0.0.1', _port) => {
host = _host;
port = _port;
start();
}
e.stop = () => {
stop();
}
e.startShareScreen = (params = '') => {
sendCommand('ss_start', params);
}
e.stopShareScreen = () => {
sendCommand('ss_stop');
}
export default e;