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
|
/* global module */
const util = require('util')
const webrtcSupport = require('webrtcsupport')
const mockconsole = require('mockconsole')
const localMedia = require('./localmedia')
const Peer = require('./peer')
/**
* @param opts
*/
function WebRTC(opts) {
const self = this
const options = opts || {}
this.config = {
debug: false,
// makes the entire PC config overridable
peerConnectionConfig: {
iceServers: [],
},
receiveMedia: {
offerToReceiveAudio: 1,
offerToReceiveVideo: 1,
},
enableDataChannels: true,
enableSimulcast: false,
maxBitrates: {
high: 900000,
medium: 300000,
low: 100000,
},
}
let item
// We also allow a 'logger' option. It can be any object that implements
// log, warn, and error methods.
// We log nothing by default, following "the rule of silence":
// http://www.linfo.org/rule_of_silence.html
this.logger = (function() {
// we assume that if you're in debug mode and you didn't
// pass in a logger, you actually want to log as much as
// possible.
if (opts.debug) {
return opts.logger || console
} else {
// or we'll use your logger which should have its own logic
// for output. Or we'll return the no-op.
return opts.logger || mockconsole
}
}())
// set options
for (item in options) {
if (Object.prototype.hasOwnProperty.call(options, item)) {
this.config[item] = options[item]
}
}
// check for support
if (!webrtcSupport.support) {
this.logger.error('Your browser doesn\'t seem to support WebRTC')
}
// where we'll store our peer connections
this.peers = []
// call localMedia constructor
localMedia.call(this, this.config)
this.on('unshareScreen', function(message) {
// End peers we were receiving the screensharing stream from.
const peers = self.getPeers(message.id, 'screen')
peers.forEach(function(peer) {
if (!peer.sharemyscreen) {
peer.end()
}
})
})
// log events in debug mode
if (this.config.debug) {
this.on('*', function(event, val1, val2) {
let logger
// if you didn't pass in a logger and you explicitly turning on debug
// we're just going to assume you're wanting log output with console
if (self.config.logger === mockconsole) {
logger = console
} else {
logger = self.logger
}
logger.log('event:', event, val1, val2)
})
}
}
util.inherits(WebRTC, localMedia)
WebRTC.prototype.createPeer = function(opts) {
opts.parent = this
const peer = new Peer(opts)
this.peers.push(peer)
return peer
}
// removes peers
WebRTC.prototype.removePeers = function(id, type) {
this.getPeers(id, type).forEach(function(peer) {
peer.end()
})
}
// fetches all Peer objects by session id and/or type
WebRTC.prototype.getPeers = function(sessionId, type) {
return this.peers.filter(function(peer) {
return (!sessionId || peer.id === sessionId) && (!type || peer.type === type)
})
}
// sends message to all
WebRTC.prototype.sendToAll = function(message, payload) {
this.peers.forEach(function(peer) {
peer.send(message, payload)
})
this.emit('sendToAll', message, payload)
}
// sends message to all using a datachannel
// only sends to anyone who has an open datachannel
WebRTC.prototype.sendDirectlyToAll = function(channel, message, payload) {
this.peers.forEach(function(peer) {
if (peer.enableDataChannels) {
peer.sendDirectly(channel, message, payload)
}
})
}
module.exports = WebRTC
|