-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·241 lines (201 loc) · 7.09 KB
/
index.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
// Module for handling connections to multiple peers.
var io = require('socket.io-client')
var SimplePeer = require('simple-peer')
var extend = Object.assign
var events = require('events').EventEmitter
var inherits = require('inherits')
const shortid = require('shortid')
var PatchBay = function (options) {
// connect to websocket signalling server. To DO: error validation
this.signaller = io(options.server)
//assign unique id to this peer, or use id passed in
this.id = options.id || shortid.generate()
this.stream = options.stream || null
//options to be sent to simple peer
this._peerOptions = options.peerOptions || {}
this._room = options.room
this.settings = {
shareMediaWhenRequested: true,
shareMediaWhenInitiating: false,
requestMediaWhenInitiating: true,
autoconnect: false
}
//object containing ALL peers in room
this.peers = {}
//object containing peers connected via webrtc
this.rtcPeers = {}
// Handle events from signalling server
this.signaller.on('ready', this._readyForSignalling.bind(this))
// this.signaller.on('peers', )
// this.signaller.on('signal', this._handleSignal.bind(this))
this.signaller.on('message', this._handleMessage.bind(this))
// Received message via websockets to all peers in room
this.signaller.on('broadcast', this._receivedBroadcast.bind(this))
// emit 'join' event to signalling server
this.signaller.emit('join', this._room, {uuid: this.id})
this.signaller.on('new peer', this._newPeer.bind(this))
}
// inherits from events module in order to trigger events
inherits(PatchBay, events)
// send data to all connected peers via data channels
PatchBay.prototype.sendToAll = function (data) {
Object.keys(this.rtcPeers).forEach(function (id) {
this.rtcPeers[id].send(data)
}, this)
}
// sends to peer specified b
PatchBay.prototype.sendToPeer = function (peerId, data) {
if (peerId in this.rtcPeers) {
this.rtcPeers[peerId].send(data)
}
}
PatchBay.prototype.reinitAll = function(){
Object.keys(this.rtcPeers).forEach(function (id) {
this.reinitPeer(id)
}.bind(this))
// this._connectToPeers.bind(this)
}
PatchBay.prototype.initRtcPeer = function(id, opts) {
this.emit('new peer', {id: id})
var newOptions = opts
if(opts.initiator === true) {
if (this.stream != null) {
if(this.settings.shareMediaWhenInitiating === true){
newOptions.stream = this.stream
}
}
if(this.settings.requestMediaWhenInitiating === true){
newOptions.offerConstraints = {
offerToReceiveVideo: true,
offerToReceiveAudio: true
}
}
} else {
if(this.settings.shareMediaWhenRequested === true){
if (this.stream != null) {
newOptions.stream = this.stream
}
}
}
var options = extend(this._peerOptions, newOptions)
this.rtcPeers[id] = new SimplePeer(options)
this._attachPeerEvents(this.rtcPeers[id], id)
}
PatchBay.prototype.reinitRtcConnection = function(id, opts){
// Because renegotiation is not implemeneted in SimplePeer, reinitiate connection when configuration has changed
this.rtcPeers[id]._destroy(null, function(e){
this.initRtcPeer(id, {
stream: this.stream,
initiator: true
})
}.bind(this))
}
// //new peer connected to signalling server
PatchBay.prototype._newPeer = function (peer){
// this.connectedIds.push(peer)
// Configuration for specified peer.
// Individual configuration controls whether will receive media from
// and/or send media to a specific peer.
this.peers[peer] = {
rtcPeer: null
}
this.emit('new peer', peer)
// this.emit('updated peer list', this.connectedIds)
}
// // Once the new peer receives a list of connected peers from the server,
// // creates new simple peer object for each connected peer.
PatchBay.prototype._readyForSignalling = function (_t, peers) {
console.log("received peer list", this.peers)
peers.forEach((peer) => {
this._newPeer(peer)
})
// this.peers = peers
this.emit('ready')
}
// Init connection to RECEIVE video
PatchBay.prototype.initConnectionFromId = function(id, callback){
// console.log("initianing connection")
if(id in this.rtcPeers){
console.log("Already connected to..", id, this.rtcPeers)
//if this peer was originally only sending a stream (not receiving), recreate connecting but this time two-way
if(this.rtcPeers[id].initiator===false){
this.reinitRtcConnection(id)
} else {
//already connected, do nothing
}
} else {
this.initRtcPeer(id, {
initiator: true
})
}
}
// receive signal from signalling server, forward to simple-peer
PatchBay.prototype._handleMessage = function (data) {
// if there is currently no peer object for a peer id, that peer is initiating a new connection.
if (data.type === 'signal'){
this._handleSignal(data)
} else {
this.emit('message', data)
}
}
// receive signal from signalling server, forward to simple-peer
PatchBay.prototype._handleSignal = function (data) {
// if there is currently no peer object for a peer id, that peer is initiating a new connection.
if (!this.rtcPeers[data.id]) {
// this.emit('new peer', data)
// var options = extend({stream: this.stream}, this._peerOptions)
// this.rtcPeers[data.id] = new SimplePeer(options)
// this._attachPeerEvents(this.rtcPeers[data.id], data.id)
this.initRtcPeer(data.id, {initiator: false})
}
this.rtcPeers[data.id].signal(data.message)
}
// sendToAll send through rtc connections, whereas broadcast
// send through the signalling server. Useful in cases where
// not all peers are connected via webrtc with other peers
PatchBay.prototype._receivedBroadcast = function(data) {
//console.log("RECEIVED BROADCAST", data)
this.emit('broadcast', data)
}
//sends via signalling server
PatchBay.prototype.broadcast = function (data) {
this.signaller.emit('broadcast', data)
}
// handle events for each connected peer
PatchBay.prototype._attachPeerEvents = function (p, _id) {
p.on('signal', function (id, signal) {
// console.log('signal', id, signal)
// console.log("peer signal sending over sockets", id, signal)
// this.signaller.emit('signal', {id: id, signal: signal})
this.signaller.emit('message', {id: id, message: signal, type: 'signal'})
}.bind(this, _id))
p.on('stream', function (id, stream) {
this.rtcPeers[id].stream = stream
// console.log('E: stream', id, stream)
// console.log("received a stream", stream)
this.emit('stream', id, stream)
}.bind(this, _id))
p.on('connect', function (id) {
// console.log("connected to ", id)
this.emit('connect', id)
}.bind(this, _id))
p.on('data', function (id, data) {
// console.log('data', id)
this.emit('data', {id: id, data: JSON.parse(data)})
}.bind(this, _id))
p.on('close', function (id) {
//console.log('CLOSED')
delete (this.rtcPeers[id])
this.emit('close', id)
}.bind(this, _id))
p.on('error', function(e){
console.log("simple peer error", e)
})
}
PatchBay.prototype._destroy = function () {
Object.values(this.rtcPeers).forEach( function (peer) {
peer.destroy()
})
this.signaller.close()
}
module.exports = PatchBay