home.js
14 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
new Vue({
el: '#app',
data(){
return {
roomName: '吴波波',
roomId: 0,
pin: '',
bitrate: 512000,
iconList: [
{sign: '#iconicon-microphone-on',name: '麦克风', status: 1},
{sign: '#iconicon-volume',name: '声音', status: 1},
{sign: '#iconicon-camera-on',name: '摄像头', status: 1},
{sign: '#iconicon-sharescreen',name: '屏幕分享', status: 0},
{sign: '#iconicon-share',name: '邀请加人', status: 0},
{sign: '#iconicon-stop',name: '结束', status: 0}
], //0-未使用 1-使用中
server: 'https://janusctest.uccc.cc:24000/janus',
janus: '',
opaqueId: "videoroomtest-"+Janus.randomString(12),
handle: '',
feeds: [],
description:''
}
},
created() {
this.roomId = parseInt(sessionStorage.getItem("room"))
this.roomName = sessionStorage.getItem("roomname")
this.description = sessionStorage.getItem("description")
this.pin = sessionStorage.getItem("pin")
sessionStorage.clear();
//this.room = storage.getItem("checks")
if (this.roomId) {
this.initVideo();
} else {
location.href ="./index.html";
}
},
methods: {
initVideo() {
let that = this;
// //初始化
Janus.init({debug: "all", callback: function() {
if(!Janus.isWebrtcSupported()) {
bootbox.alert("No WebRTC support... ");
return;
}
that._data.janus = new Janus({
server: that._data.server,
success: function() {
that.creatAttach();
},
error: function(error) {
Janus.error(error);
bootbox.alert(error, function() {
window.location.reload();
});
},
destroyed: function() {
window.location.reload();
}
})
}
})
},
//创建会话
creatAttach() {
let that = this;
var janus = that._data.janus;
var feeds = that._data.feeds;
var room = that._data.roomId;
var pin = that._data.pin;
var bitrate = that._data.bitrate;
janus.attach({
plugin: "janus.plugin.videoroom",
opaqueId: that._data.opaqueId,
success: function(pluginHandle) {
that._data.handle = pluginHandle;
Janus.log("Plugin attached! (" + that._data.handle.getPlugin() + ", id=" + that._data.handle.getId() + ")");
that.registerUsername(room, pin);
},
error: function(error) {
Janus.error(" -- Error attaching plugin...", error);
bootbox.alert("Error attaching plugin... " + error);
},
consentDialog: function(on) {
Janus.debug("Consent dialog should be " + (on ? "on" : "off") + " now");
if(on) {
}
},
mediaState: function(medium, on) {
Janus.log("Janus " + (on ? "started" : "stopped") + " receiving our " + medium);
},
webrtcState: function(on) {
Janus.log("Janus says our WebRTC PeerConnection is " + (on ? "up" : "down") + " now");
if(!on) return;
that._data.handle.send({"message": { "request": "configure", "bitrate": bitrate }});
},
onmessage: function(msg, jsep) {
console.log(msg)
Janus.debug(" ::: Got a message (publisher) :::");
Janus.debug(msg);
var event = msg["videoroom"];
Janus.debug("Event: " + event);
if(event != undefined && event != null) {
if(event === "joined") {
myid = msg["id"];
mypvtid = msg["private_id"];
Janus.log("Successfully joined room " + msg["room"] + " with ID " + myid);
that.publishOwnFeed(true);
if(msg["publishers"] !== undefined && msg["publishers"] !== null) {
var list = msg["publishers"];
Janus.debug("Got a list of available publishers/feeds:");
Janus.debug(list);
for(var f in list) {
var id = list[f]["id"];
var display = list[f]["display"];
var audio = list[f]["audio_codec"];
var video = list[f]["video_codec"];
Janus.debug(" >> [" + id + "] " + display + " (audio: " + audio + ", video: " + video + ")");
that.newRemoteFeed(id, display, audio, video);
}
}
} else if(event === "destroyed") {
Janus.warn("The room has been destroyed!");
bootbox.alert("The room has been destroyed", function() {
window.location.reload();
});
} else if(event === "event") {
if(msg["publishers"] !== undefined && msg["publishers"] !== null) {
var list = msg["publishers"];
Janus.debug("Got a list of available publishers/feeds:");
Janus.debug(list);
for(var f in list) {
var id = list[f]["id"];
var display = list[f]["display"];
var audio = list[f]["audio_codec"];
var video = list[f]["video_codec"];
Janus.debug(" >> [" + id + "] " + display + " (audio: " + audio + ", video: " + video + ")");
that.newRemoteFeed(id, display, audio, video);
}
} else if(msg["leaving"] !== undefined && msg["leaving"] !== null) {
var leaving = msg["leaving"];
Janus.log("Publisher left: " + leaving);
var remoteFeed = null;
for(var i=1; i<6; i++) {
if(feeds[i] != null && feeds[i] != undefined && feeds[i].rfid == leaving) {
remoteFeed = feeds[i];
break;
}
}
if(remoteFeed != null) {
Janus.debug("Feed " + remoteFeed.rfid + " (" + remoteFeed.rfdisplay + ") has left the room, detaching");
$('#remote'+remoteFeed.rfindex).empty().hide();
$('#videoremote'+remoteFeed.rfindex).empty();
feeds[remoteFeed.rfindex] = null;
remoteFeed.detach();
}
} else if(msg["unpublished"] !== undefined && msg["unpublished"] !== null) {
var unpublished = msg["unpublished"];
Janus.log("Publisher left: " + unpublished);
if(unpublished === 'ok') {
that._data.handle.hangup();
return;
}
var remoteFeed = null;
for(var i=1; i<6; i++) {
if(feeds[i] != null && feeds[i] != undefined && feeds[i].rfid == unpublished) {
remoteFeed = feeds[i];
break;
}
}
if(remoteFeed != null) {
Janus.debug("Feed " + remoteFeed.rfid + " (" + remoteFeed.rfdisplay + ") has left the room, detaching");
$('#remote'+remoteFeed.rfindex).empty().hide();
$('#videoremote'+remoteFeed.rfindex).empty();
feeds[remoteFeed.rfindex] = null;
remoteFeed.detach();
}
} else if(msg["error"] !== undefined && msg["error"] !== null) {
if(msg["error_code"] === 426) {
}
}
}
if(jsep !== undefined && jsep !== null) {
Janus.debug("Handling SDP as well...");
Janus.debug(jsep);
that._data.handle.handleRemoteJsep({jsep: jsep});
var audio = msg["audio_codec"];
if(mystream && mystream.getAudioTracks() && mystream.getAudioTracks().length > 0 && !audio) {
toastr.warning("Our audio stream has been rejected, viewers won't hear us");
}
var video = msg["video_codec"];
if(mystream && mystream.getVideoTracks() && mystream.getVideoTracks().length > 0 && !video) {
toastr.warning("Our video stream has been rejected, viewers won't see us");
}
}
}
},
onlocalstream: function(stream) {
Janus.debug(" ::: Got a local stream :::");
mystream = stream;
Janus.debug(stream);
Janus.attachMediaStream($('#mainVideo_html5_api').get(0), stream);
var videoTracks = stream.getVideoTracks();
if(videoTracks === null || videoTracks === undefined || videoTracks.length === 0) {
}
},
onremotestream: function(stream) {
},
oncleanup: function() {
Janus.log(" ::: Got a cleanup notification: we are unpublished now :::");
}
})
},
registerUsername(room,pin) {
var register = { "request": "join", "room": room, "ptype": "publisher", "display": "hhh", "pin": pin };
this._data.handle.send({"message": register});
},
publishOwnFeed(useAudio) {
var doSimulcast = (this.getQueryStringValue("simulcast") === "yes" || this.getQueryStringValue("simulcast") === "true");
var doSimulcast2 = (this.getQueryStringValue("simulcast2") === "yes" || this.getQueryStringValue("simulcast2") === "true");
var canshu = this._data.handle;
canshu.createOffer({
media: { audioRecv: false, videoRecv: false, audioSend: useAudio, videoSend: true },
simulcast: doSimulcast,
simulcast2: doSimulcast2,
success: function(jsep) {
Janus.debug("Got publisher SDP!");
Janus.debug(jsep);
var publish = { "request": "configure", "audio": useAudio, "video": true };
canshu.send({"message": publish, "jsep": jsep});
},
error: function(error) {
Janus.error("WebRTC error:", error);
if (useAudio) {
this.publishOwnFeed(false);
} else {
bootbox.alert("WebRTC error... " + JSON.stringify(error));
}
}
});
},
getQueryStringValue(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
},
newRemoteFeed(id, display, audio, video) {
var remoteFeed = null;
let that = this;
var janus = that._data.janus;
var feeds = that._data.feeds;
var room = that._data.roomId;
var pin = that._data.pin;
janus.attach(
{
plugin: "janus.plugin.videoroom",
opaqueId: that._data.opaqueId,
success: function(pluginHandle) {
remoteFeed = pluginHandle;
remoteFeed.simulcastStarted = false;
Janus.log("Plugin attached! (" + remoteFeed.getPlugin() + ", id=" + remoteFeed.getId() + ")");
Janus.log(" -- This is a subscriber");
var subscribe = { "request": "join", "room": room, "ptype": "subscriber", "feed": id, "private_id": mypvtid, "pin": pin };
if(Janus.webRTCAdapter.browserDetails.browser === "safari" &&
(video === "vp9" || (video === "vp8" && !Janus.safariVp8))) {
if(video)
video = video.toUpperCase()
toastr.warning("Publisher is using " + video + ", but Safari doesn't support it: disabling video");
subscribe["offer_video"] = false;
}
remoteFeed.videoCodec = video;
remoteFeed.send({"message": subscribe});
},
error: function(error) {
Janus.error(" -- Error attaching plugin...", error);
bootbox.alert("Error attaching plugin... " + error);
},
onmessage: function(msg, jsep) {
console.log(msg)
Janus.debug(" ::: Got a message (subscriber) :::");
Janus.debug(msg);
var event = msg["videoroom"];
Janus.debug("Event: " + event);
if(msg["error"] !== undefined && msg["error"] !== null) {
bootbox.alert(msg["error"]);
} else if(event != undefined && event != null) {
if(event === "attached") {
for(var i=1;i<6;i++) {
if(feeds[i] === undefined || feeds[i] === null) {
feeds[i] = remoteFeed;
remoteFeed.rfindex = i;
break;
}
}
remoteFeed.rfid = msg["id"];
remoteFeed.rfdisplay = msg["display"];
Janus.log("Successfully attached to feed " + remoteFeed.rfid + " (" + remoteFeed.rfdisplay + ") in room " + msg["room"]);
} else if(event === "event") {
var substream = msg["substream"];
var temporal = msg["temporal"];
if((substream !== null && substream !== undefined) || (temporal !== null && temporal !== undefined)) {
if(!remoteFeed.simulcastStarted) {
remoteFeed.simulcastStarted = true;
addSimulcastButtons(remoteFeed.rfindex, remoteFeed.videoCodec === "vp8" || remoteFeed.videoCodec === "h264");
}
updateSimulcastButtons(remoteFeed.rfindex, substream, temporal);
}
} else {
}
}
if(jsep !== undefined && jsep !== null) {
Janus.debug("Handling SDP as well...");
Janus.debug(jsep);
remoteFeed.createAnswer(
{
jsep: jsep,
media: { audioSend: false, videoSend: false },
success: function(jsep) {
Janus.debug("Got SDP!");
Janus.debug(jsep);
var body = { "request": "start", "room": room };
remoteFeed.send({"message": body, "jsep": jsep});
},
error: function(error) {
Janus.error("WebRTC error:", error);
bootbox.alert("WebRTC error... " + JSON.stringify(error));
}
});
}
},
webrtcState: function(on) {
Janus.log("Janus says this WebRTC PeerConnection (feed #" + remoteFeed.rfindex + ") is " + (on ? "up" : "down") + " now");
},
onlocalstream: function(stream) {
},
onremotestream: function(stream) {
Janus.debug("Remote feed #" + remoteFeed.rfindex);
Janus.attachMediaStream($('#sideVideo'+remoteFeed.rfindex + '_html5_api').get(0), stream);
},
oncleanup: function() {
Janus.log(" ::: Got a cleanup notification (remote feed " + id + ") :::");
remoteFeed.simulcastStarted = false;
}
});
},
myOption(index) {
console.log(index);
if(this.iconList[index].status === 1) {
this.iconList[index].status = 0;
switch(index)
{
case 0:
this.iconList[index].sign = '#iconicon-microphone-off'
//操控
break;
case 1:
this.iconList[index].sign = '#iconicon-volume-off'
break;
case 2:
this.iconList[index].sign = '#iconicon-camera-off'
break;
case 3:
this.publicNotify('关闭屏幕分享..')
break;
case 4:
this.publicNotify('关闭屏幕分享..')
break;
case 5:
this.janus.destroy()
location.href ="./../index.html"
break;
}
}else {
if (index != 4) {
this.iconList[index].status = 1;
}
switch(index)
{
case 0:
this.iconList[index].sign = '#iconicon-microphone-on'
//操控
break;
case 1:
this.iconList[index].sign = '#iconicon-volume'
break;
case 2:
this.iconList[index].sign = '#iconicon-camera-on'
break;
case 3:
this.publicNotify('开启屏幕分享..')
break;
case 4:
this.publicNotify('开启..')
break;
case 5:
this.janus.destroy()
location.href ="./../index.html"
break;
}
}
},
publicNotify(msg) {
this.$notify({
title: 'Tip',
message: msg
});
}
}
})