forked from muaz-khan/WebRTC-Scalable-Broadcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebRTC-Scalable-Broadcast.js
88 lines (72 loc) · 3.31 KB
/
WebRTC-Scalable-Broadcast.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
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// WebRTC Scalable Broadcast:
// this module simply initializes socket.io
// and configures it in a way that
// single broadcast can be relayed over unlimited users
// without any bandwidth/CPU usage issues.
// Everything happens peer-to-peer!
// Ref. discussion: https://github.com/muaz-khan/WebRTC-Experiment/issues/2
// Source Code: https://github.com/muaz-khan/WebRTC-Scalable-Broadcast
module.exports = exports = WebRTC_Scalable_Broadcast;
function WebRTC_Scalable_Broadcast(app) {
var io = require('socket.io').listen(app, {
log: false,
origins: '*:*'
});
io.set('transports', [
'websocket', // 'disconnect' EVENT will work only with 'websocket'
'xhr-polling',
'jsonp-polling'
]);
var listOfBroadcasts = {};
io.on('connection', function(socket) {
var currentUser;
socket.on('join-broadcast', function(user) {
currentUser = user;
user.numberOfViewers = 0;
if (!listOfBroadcasts[user.broadcastid]) {
listOfBroadcasts[user.broadcastid] = {
broadcasters: {},
allusers: {},
typeOfStreams: user.typeOfStreams // object-booleans: audio, video, screen
};
}
var firstAvailableBroadcaster = getFirstAvailableBraodcater(user);
if (firstAvailableBroadcaster) {
listOfBroadcasts[user.broadcastid].broadcasters[firstAvailableBroadcaster.userid].numberOfViewers++;
socket.emit('join-broadcaster', firstAvailableBroadcaster, listOfBroadcasts[user.broadcastid].typeOfStreams);
console.log('User <', user.userid, '> is trying to get stream from user <', firstAvailableBroadcaster.userid, '>');
} else {
currentUser.isInitiator = true;
socket.emit('start-broadcasting', listOfBroadcasts[user.broadcastid].typeOfStreams);
console.log('User <', user.userid, '> will be next to serve broadcast.');
}
listOfBroadcasts[user.broadcastid].broadcasters[user.userid] = user;
listOfBroadcasts[user.broadcastid].allusers[user.userid] = user;
});
socket.on('message', function(message) {
socket.broadcast.emit('message', message);
});
socket.on('disconnect', function() {
if (!currentUser) return;
if (!listOfBroadcasts[currentUser.broadcastid]) return;
if (!listOfBroadcasts[currentUser.broadcastid].broadcasters[currentUser.userid]) return;
delete listOfBroadcasts[currentUser.broadcastid].broadcasters[currentUser.userid];
if (currentUser.isInitiator) {
delete listOfBroadcasts[currentUser.broadcastid];
}
});
});
function getFirstAvailableBraodcater(user) {
var broadcasters = listOfBroadcasts[user.broadcastid].broadcasters;
var firstResult;
for (var userid in broadcasters) {
if (broadcasters[userid].numberOfViewers <= 3) {
firstResult = broadcasters[userid];
continue;
} else delete listOfBroadcasts[user.broadcastid].broadcasters[userid];
}
return firstResult;
}
}