-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
485 lines (370 loc) · 14.1 KB
/
bundle.js
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
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/*This is the part of code give here:
https://github.com/muaz-khan/RTCMultiConnection/blob/master/docs/getting-started.md
for
"Getting Started from Scratch"
and from this exemple with room ID and chat box:
https://jsfiddle.net/zd9Lsdfk/50/
*/
const timeDisplay = document.querySelector("p");
const startButton = document.getElementById("open-room");
const stopButton = document.getElementById("leave-room");
//const sendButton = document.getElementById("send-message");
let localAudio = document.querySelector("#localAudio");
let localContainer = document.getElementById("localContainer");
let remoteContainer = document.getElementById("remoteContainer");
let roomID = document.getElementById("room-id");
let connection = new RTCMultiConnection();
// ......................................................
// .......................UI Code........................
// ......................................................
module.exports = function () {
startButton.onclick = function() {
disableInputButtons();
connection.openOrJoin(roomID.value, function(isRoomExist, roomid) {
if (isRoomExist === false && connection.isInitiator === true) {
// if room doesn't exist, it means that current user will create the room
showRoomURL(roomid);
}
if(isRoomExist) {
connection.sdpConstraints.mandatory = {
OfferToReceiveAudio: true,
OfferToReceiveVideo: false
};
}
showRoomURL(connection.sessionid);
});
};
}
stopButton.onclick = function() {
startButton.disabled = false;
connection.getAllParticipants().forEach(function(participantId) {
connection.disconnectWith(participantId);
});
// close the URL display
document.getElementById('room-urls').style.display = "none";
// close socket.io connection
connection.closeSocket();
};
// ......................................................
// .....................Socket.io........................
// ......................................................
/*
roomID.value = roomid;
function buttonClicked() {
socket.emit('clicked',roomid);
}
connection.connectSocket(function buttonClicked() {
console.info('Successfully connected to socket.io server.');
connection.socket.emit('clicked',connection.sessionid);
connection.socket.emit('clicked', roomID);
// each and every user can open unique room
connection.open(roomID);
});
*/
//connection.socketMessageEvent = roomID;
/*
/// connection.socket:
ex 1:
connection.open('roomid', function() {
connection.socket.emit('whatever', 'hmm');
connection.socket.disconnect();
});
ex 2:
connection.onstream = function(event) {
if(event.type === 'remote') {
connection.socket.emit('get-remote-user-extra-data', event.userid, function(extra) {
alert( extra.joinTime );
});
}
}:
///
///This method allows you set custom socket listener before starting or joining a room.
connection.socketCustomEvent = 'abcdef';
connection.openOrJoin('roomid', function() {
connection.socket.on(connection.socketCustomEvent, function(message) {
alert(message);
});
connection.socket.emit(connection.socketCustomEvent, 'My userid is: ' + connection.userid);
});
//Pour récupérer le nom de chaque participant
//This method allows you set custom socket listeners anytime, during a live session.
connection.setCustomSocketEvent('abcdef');
connection.socket.on('abcdef', function(message) {
alert(message);
});
connection.socket.emit('abcdef', 'My userid is: ' + connection.userid);
*/
// ......................................................
// ..................RTCMultiConnection Code.............
// ......................................................
//WebRTC Supported Detection
if (connection.DetectRTC.isWebRTCSupported === false) {
alert('Please try a WebRTC compatible web browser e.g. Chrome, Firefox or Opera.');
}
connection.enableFileSharing = true; // by default, it is "false".
// keep room opened even if owner leaves
connection.autoCloseEntireSession = true;
// this line is VERY_important
connection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/';
connection.onEntireSessionClosed = function(event) {
console.info('Entire session is closed: ', event.sessionid, event.extra);
};
// all below lines are optional; however recommended.
connection.session = {
audio: true,
video: false,
data: true,
broadcast: true
};
// allow 6 users
connection.maxParticipantsAllowed = 7;
connection.onRoomFull = function(roomid) {
alert('Room is full.');
};
connection.mediaConstraints = {
audio: {
sampleRate: 48000,
channelCount: 2,
volume: 1.0,
echoCancellation:false,
autoGainControl:false,
noiseSuppression:false,
highPassFilter:false
},
video: false
};
connection.sdpConstraints.mandatory = {
OfferToReceiveAudio: {
sampleRate: 48000,
channelCount: 2,
volume: 1.0,
echoCancellation:false,
autoGainControl:false,
noiseSuppression:false,
highPassFilter:false
},
OfferToReceiveVideo: false
};
// https://www.rtcmulticonnection.org/docs/iceServers/
// use your own TURN-server here!
connection.iceServers = [{
'urls': [
'stun:stun.l.google.com:19302',
'stun:stun1.l.google.com:19302',
'stun:stun2.l.google.com:19302',
'stun:stun.l.google.com:19302?transport=udp',
]
}];
connection.onmessage = appendDIV;
connection.filesContainer = document.getElementById('file-container');
connection.onopen = function() {
document.getElementById('input-text-chat').disabled = false;
document.getElementById('share-file').disabled = false;
};
connection.onclose = connection.onleave = function(event) {
alert(event.userid + ' leave the session');
};
function disableInputButtons() {
roomID.onkeyup();
startButton.disabled = true;
stopButton.disabled = false;
roomID.disabled = true;
}
connection.onstream = function(event) {
var audioElement = event.mediaElement;
//Create each element type into a particuliar container
if (event.type === 'local') {
localContainer.appendChild(audioElement);
}
if (event.type === 'remote') {
remoteContainer.appendChild(audioElement);
alert(event.userid + ' join the session');
}
};
connection.onstreamended = function(event) {
var mediaElement = document.getElementById(event.streamid);
if (mediaElement) {
mediaElement.parentNode.removeChild(mediaElement);
}
};
// ......................................................
// .................Select Input Part....................
// ......................................................
/*
connection.onMediaError = function(e) {
if (e.message === 'Concurrent mic process limit.') {
if (DetectRTC.audioInputDevices.length <= 1) {
alert('Please select external microphone. Check github issue number 483.');
return;
}
var secondaryMic = DetectRTC.audioInputDevices[1].deviceId;
connection.mediaConstraints.audio = {
deviceId: secondaryMic
};
connection.join(connection.sessionid);
}
};
*/
// ......................................................
// .............Scalable Broadcast Part..................
// ......................................................
//document.getElementById('broadcast-id').value = connection.userid;
// user need to connect server, so that others can reach him.
connection.connectSocket(function(socket) {
socket.on('logs', function(log) {
document.querySelector('h1').innerHTML = log.replace(/</g, '----').replace(/>/g, '___').replace(/----/g, '(<span style="color:red;">').replace(/___/g, '</span>)');
});
// this event is emitted when a broadcast is already created.
socket.on('join-broadcaster', function(hintsToJoinBroadcast) {
console.log('join-broadcaster', hintsToJoinBroadcast);
connection.session = hintsToJoinBroadcast.typeOfStreams;
connection.sdpConstraints.mandatory = {
OfferToReceiveVideo: !!connection.session.video,
OfferToReceiveAudio: !!connection.session.audio
};
connection.broadcastId = hintsToJoinBroadcast.broadcastId;
connection.join(hintsToJoinBroadcast.userid);
});
socket.on('rejoin-broadcast', function(broadcastId) {
console.log('rejoin-broadcast', broadcastId);
connection.attachStreams = [];
socket.emit('check-broadcast-presence', broadcastId, function(isBroadcastExists) {
if (!isBroadcastExists) {
// the first person (i.e. real-broadcaster) MUST set his user-id
connection.userid = broadcastId;
}
socket.emit('join-broadcast', {
broadcastId: broadcastId,
userid: connection.userid,
typeOfStreams: connection.session
});
});
});
socket.on('broadcast-stopped', function(broadcastId) {
// alert('Broadcast has been stopped.');
// location.reload();
console.error('broadcast-stopped', broadcastId);
alert('This broadcast has been stopped.');
});
// this event is emitted when a broadcast is absent.
socket.on('start-broadcasting', function(typeOfStreams) {
console.log('start-broadcasting', typeOfStreams);
// host i.e. sender should always use this!
connection.sdpConstraints.mandatory = {
OfferToReceiveVideo: false,
OfferToReceiveAudio: false
};
connection.session = typeOfStreams;
// "open" method here will capture media-stream
// we can skip this function always; it is totally optional here.
// we can use "connection.getUserMediaHandler" instead
connection.open(connection.userid);
});
});
window.onbeforeunload = function() {
// Firefox is ugly.
startButton.disabled = false;
};
/*document.getElementById('share-session').onclick = function(){
this.disabled = true;
var allUserStreams = connection.getRemoteStreams();
connection.dontCaptureUserMedia = false;
connection.mediaConstraints.audio = true;
connection.attachStreams = [allUserStreams];
connection.addStream({
audio: true,
oneway: true,
allUserStreams: true
});
};
*/
// ......................................................
// .................MultiStreamsMixer....................
// ......................................................
//// all remote users
//var allUserStreams = connection.getRemoteStreams();
//connection.addStream(audioMixer.getMixedStream());
// ......................................................
// ................FileSharing/TextChat Code.............
// ......................................................
document.getElementById('share-file').onclick = function() {
var fileSelector = new FileSelector();
fileSelector.selectSingleFile(function(file) {
connection.send(file);
});
};
document.getElementById('input-text-chat').onkeyup = function send(e) {
if(e.keyCode != 13) return;
// removing trailing/leading whitespace
this.value = this.value.replace(/^\s+|\s+$/g, '');
if (!this.value.length) return;
connection.send(this.value);
appendDIV(this.value);
this.value = '';
};
var chatContainer = document.querySelector('#conversation-panel');
function appendDIV(event) {
var div = document.createElement('div');
div.innerHTML = event.data || event ;
chatContainer.insertBefore(div, chatContainer.firstChild);
div.tabIndex = 0;
div.focus();
document.getElementById('input-text-chat').focus();
}
// ......................................................
// ......................Handling Room-ID................
// ......................................................
function showRoomURL(roomid) {
var roomHashURL = '#' + roomid;
var html = '<h4>Session Open</h4>';
html += 'Share URL: <a href="' + roomHashURL + '" target="_blank">' + roomHashURL + '</a>';
var roomURLsDiv = document.getElementById('room-urls');
roomURLsDiv.innerHTML = html;
roomURLsDiv.style.display = 'block';
}
(function() {
var params = {},
r = /([^&=]+)=?([^&]*)/g;
function d(s) {
return decodeURIComponent(s.replace(/\+/g, ' '));
}
var match, search = window.location.search;
while (match = r.exec(search.substring(1)))
params[d(match[1])] = d(match[2]);
window.params = params;
})();
var roomid = '';
if (localStorage.getItem(connection.socketMessageEvent)) {
roomid = localStorage.getItem(connection.socketMessageEvent);
} else {
roomid = connection.token();
}
document.getElementById('room-id').value = roomid;
document.getElementById('room-id').onkeyup = function() {
localStorage.setItem(connection.socketMessageEvent, this.value);
};
var hashString = location.hash.replace('#', '');
if(hashString.length && hashString.indexOf('comment-') == 0) {
hashString = '';
}
var roomid = params.roomid;
if(!roomid && hashString.length) {
roomid = hashString;
}
if(roomid && roomid.length) {
document.getElementById('room-id').value = roomid;
localStorage.setItem(connection.socketMessageEvent, roomid);
// auto-join-room
(function reCheckRoomPresence() {
connection.checkPresence(roomid, function(isRoomExists) {
if(isRoomExists) {
connection.join(roomid);
return;
}
setTimeout(reCheckRoomPresence, 5000);
});
})();
disableInputButtons();
}
},{}]},{},[1]);