home.js
8.26 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
new Vue({
el: '#app',
data(){
return {
roomName: '吴波波',
roomId: '88888888',
iconList: [
{sign: '#iconicon-microphone-on',name: '麦克风'},
{sign: '#iconicon-volume',name: '声音'},
{sign: '#iconicon-camera-on',name: '摄像头'},
{sign: '#iconicon-sharescreen',name: '屏幕分享'},
{sign: '#iconicon-share',name: '邀请加人'},
{sign: '#iconicon-stop',name: '结束'}
],
server: 'https://janusctest.uccc.cc:24000/janus',
janus: '',
opaqueId: "videoroomtest-"+Janus.randomString(12),
handle: ''
}
},
created() {
this.initVideo();
},
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;
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();
},
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) {
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 + ")");
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 + ")");
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) {
}
},
nremotestream: function(stream) {
},
oncleanup: function() {
Janus.log(" ::: Got a cleanup notification: we are unpublished now :::");
}
})
},
registerUsername() {
var register = { "request": "join", "room": 1008611, "ptype": "publisher", "display": "hhh", "pin": "123456" };
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, " "));
},
getClick() {
console.log("---------");
}
}
})