forked from max-mapper/screencat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.js
299 lines (258 loc) · 7.98 KB
/
peer.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
/* global screen, EventSource */
var zlib = require('zlib')
var events = require('events')
var SimplePeer = require('simple-peer')
var nets = require('nets')
var getUserMedia = require('./get-user-media.js')()
module.exports = function create () {
var server = 'http://catlobby.maxogden.com'
// var server = 'http://localhost:5005'
var remoteConfigUrl = 'https://instant.io/rtcConfig'
if (process.browser) remoteConfigUrl = 'http://cors.maxogden.com/' + remoteConfigUrl
var videoSize
var defaultConstraints = {
audio: false,
video: {
mandatory: {
chromeMediaSource: 'screen',
maxWidth: screen.availWidth,
maxHeight: screen.availHeight,
maxFrameRate: 25
},
optional: []
}
}
var pc = new events.EventEmitter()
pc.getRemoteConfig = getRemoteConfig
pc.verifyRoom = verifyRoom
pc.remotePeer = remotePeer
pc.hostPeer = hostPeer
pc.handleSignal = handleSignal
pc.videoElement = videoElement
pc.audioElement = audioElement
pc.onConnect = onConnect
pc.createRoom = createRoom
return pc
function verifyRoom (room, cb) {
// ensure room is still open
nets({method: 'POST', uri: server + '/v1/' + room + '/pong', json: {ready: true}}, function response (err, resp, data) {
if (err) return cb(err)
if (resp.statusCode !== 200) return cb(new Error('Invalid or expired invite code'))
cb()
})
}
// get remote webrtc config (ice/stun/turn)
function getRemoteConfig (cb) {
nets({url: remoteConfigUrl, json: true}, function gotConfig (err, resp, config) {
if (err || resp.statusCode > 299) config = undefined // ignore errors
cb(null, config)
})
}
// try getusermedia and then upload sdp pong. this causes host to ping sdp back
function getAudio (cb) {
getUserMedia({audio: true, video: false}, function ok (stream) {
cb(null, stream)
},
function error (err) {
// screenshare even if remote doesnt wanna do audio
if (err.name === 'PermissionDeniedError' || err.name === 'DevicesNotFoundError') {
cb()
} else {
cb(err)
}
})
}
function remotePeer (config, room, cb) {
// listen for pings
var pingsUrl = server + '/v1/' + room + '/pings'
console.log('getting pings', pingsUrl)
var events = new EventSource(pingsUrl)
events.onmessage = function onMessage (e) {
console.log('pings onmessage', e.data)
var row
try {
row = JSON.parse(e.data)
} catch (e) {
row = {}
return cb(new Error('Error connecting. Please start over.'))
}
if (!row.data) {
return
}
inflate(row.data, function inflated (err, stringified) {
if (err) return cb(err)
pc.emit('getting-audio')
getAudio(function got (err, audioStream) {
if (err) return handleRTCErr(err, cb)
var peer = new SimplePeer({ trickle: false, config: config })
if (audioStream) peer._pc.addStream(audioStream)
peer.signal(JSON.parse(stringified.toString()))
cb(null, peer)
})
})
events.close()
}
events.onerror = function onError (e) {
cb(new Error('Error connecting. Please start over.'))
events.close()
}
}
function createRoom (cb) {
nets({method: 'POST', uri: server + '/v1'}, function response (err, resp, body) {
if (err) return cb(err)
var room = JSON.parse(body)
cb(null, room.name)
})
}
function hostPeer (opts, cb) {
var room = opts.room
var config = opts.config
var constraints = opts.constraints || defaultConstraints
var peer
// listen for pongs
var events = new EventSource(server + '/v1/' + room + '/pongs')
events.onmessage = function onMessage (e) {
console.log('pongs onmessage', e.data)
var row
try {
row = JSON.parse(e.data)
} catch (e) {
return cb(new Error('Error connecting. Please start over.'))
}
// other side is ready
if (row.ready) {
connect(row.data)
}
// sdp from other side
if (row.data) {
inflate(row.data, function inflated (err, stringified) {
if (err) {
return cb(new Error('Error connecting. Please start over.'))
}
peer.signal(JSON.parse(stringified.toString()))
})
events.close()
}
function connect (pong) {
// screensharing
getUserMedia(constraints, function (videoStream) {
// audio
getUserMedia({audio: true, video: false}, function (audioStream) {
peer = new SimplePeer({ initiator: true, trickle: false, config: config })
peer._pc.addStream(videoStream)
peer._pc.addStream(audioStream)
pc.emit('waiting-for-peer')
cb(null, peer)
}, function (err) { handleRTCErr(err, cb) })
}, function (err) { handleRTCErr(err, cb) })
}
}
events.onerror = function onError (e) {
cb(e)
events.close()
}
}
function handleRTCErr (err, cb) {
if (err.name === 'PermissionDeniedError') {
console.error('permission denied')
console.error(err)
cb(new Error('Screensharing permission denied'))
} else {
console.error('Unknown error', err)
cb(err)
}
}
function handleSignal (sdp, peer, remote, room, cb) {
deflate(sdp, function deflated (err, data) {
if (err) return cb(err)
// upload sdp
var uploadURL = server + '/v1/' + room
if (remote) uploadURL += '/pong'
else uploadURL += '/ping'
console.log('POST', uploadURL)
nets({method: 'POST', json: {data: data}, uri: uploadURL}, function response (err, resp, body) {
if (err || resp.statusCode > 299) return cb(err)
cb(null)
})
})
}
function onConnect (peer, remote) {
pc.emit('connected', peer, remote)
var video
if (remote) {
window.addEventListener('mouseup', mouseupListener)
window.addEventListener('keydown', keydownListener)
}
if (!remote) {
peer.on('data', function (data) {
if (!pc.robot) return
console.log(data)
pc.robot(data)
})
return
}
peer.on('close', function cleanup () {
window.removeEventListener('mouseup', mouseupListener)
window.removeEventListener('keydown', keydownListener)
})
function mouseupListener (e) {
var data = getMouseData(e)
data.click = true
console.log('send mouseup', data)
peer.send(data)
}
function keydownListener (e) {
e.preventDefault()
var data = {
keyCode: e.keyCode,
shift: e.shiftKey,
meta: e.metaKey,
control: e.ctrlKey,
alt: e.altKey
}
console.log('send key', data)
peer.send(data)
}
function getMouseData (e) {
var data = {}
data.clientX = e.clientX
data.clientY = e.clientY
if (!video) video = document.querySelector('video')
if (video) {
videoSize = video.getBoundingClientRect()
data.canvasWidth = videoSize.width
data.canvasHeight = videoSize.height
}
return data
}
}
function videoElement (stream) {
var video = document.createElement('video')
video.src = window.URL.createObjectURL(stream)
video.autoplay = true
return video
}
function audioElement (stream) {
var audio = document.createElement('audio')
audio.src = window.URL.createObjectURL(stream)
audio.autoplay = true
return audio
}
function inflate (data, cb) {
data = decodeURIComponent(data.toString())
zlib.inflate(new Buffer(data, 'base64'), cb)
}
function deflate (data, cb) {
// sdp is ~2.5k usually, that's too big for a URL, so we zlib deflate it
var stringified = JSON.stringify(data)
zlib.deflate(stringified, function (err, deflated) {
if (err) {
cb(err)
return
}
var connectionString = deflated.toString('base64')
var code = encodeURIComponent(connectionString)
cb(null, code)
})
}
}