-
Notifications
You must be signed in to change notification settings - Fork 237
/
serverless-webrtc.js
336 lines (285 loc) · 9.41 KB
/
serverless-webrtc.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
/* See also:
http://www.html5rocks.com/en/tutorials/webrtc/basics/
https://code.google.com/p/webrtc-samples/source/browse/trunk/apprtc/index.html
https://webrtc-demos.appspot.com/html/pc1.html
*/
// Attach a media stream to an element.
attachMediaStream = function (element, stream) {
console.log('Attaching media stream')
element.srcObject = stream
element.play()
}
reattachMediaStream = function (to, from) {
console.log('Reattaching media stream')
to.srcObject = from.srcObject
to.play()
}
var cfg = {'iceServers': [{urls: 'stun:23.21.150.121'}]},
con = { 'optional': [{'DtlsSrtpKeyAgreement': true}] }
/* THIS IS ALICE, THE CALLER/SENDER */
var pc1 = new RTCPeerConnection(cfg, con),
dc1 = null, tn1 = null
// Since the same JS file contains code for both sides of the connection,
// activedc tracks which of the two possible datachannel variables we're using.
var activedc
var pc1icedone = false
var sdpConstraints = {
optional: [],
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true
}
}
$('#showLocalOffer').modal('hide')
$('#getRemoteAnswer').modal('hide')
$('#waitForConnection').modal('hide')
$('#createOrJoin').modal('show')
$('#createBtn').click(function () {
$('#createOrJoin').modal('hide')
$('#showLocalOffer').modal('show')
createLocalOffer()
})
$('#joinBtn').click(function () {
navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(function (stream) {
var video = document.getElementById('localVideo')
video.srcObject = stream;
video.play()
pc2.addStream(stream)
}).catch(function (error) {
console.log('Error adding stream to pc2: ' + error)
});
$('#createOrJoin').modal('hide')
$('#getRemoteOffer').modal('show')
})
$('#offerSentBtn').click(function () {
$('#showLocalOffer').modal('hide')
$('#getRemoteAnswer').modal('show')
})
$('#offerRecdBtn').click(function () {
var offer = $('#remoteOffer').val()
var offerDesc = new RTCSessionDescription(JSON.parse(offer))
console.log('Received remote offer', offerDesc)
writeToChatLog('Received remote offer', 'text-success')
handleOfferFromPC1(offerDesc)
$('#getRemoteOffer').modal('hide')
$('#showLocalAnswer').modal('show')
})
$('#answerSentBtn').click(function () {
$('#showLocalAnswer').modal('hide')
$('#waitForConnection').modal('show')
})
$('#answerRecdBtn').click(function () {
var answer = $('#remoteAnswer').val()
var answerDesc = new RTCSessionDescription(JSON.parse(answer))
handleAnswerFromPC2(answerDesc)
$('#getRemoteAnswer').modal('hide')
$('#waitForConnection').modal('show')
})
$('#fileBtn').change(function () {
var file = this.files[0]
console.log(file)
sendFile(file)
})
function fileSent (file) {
console.log(file + ' sent')
}
function fileProgress (file) {
console.log(file + ' progress')
}
function sendFile (data) {
if (data.size) {
FileSender.send({
file: data,
onFileSent: fileSent,
onFileProgress: fileProgress,
})
}
}
function sendMessage () {
if ($('#messageTextBox').val()) {
var channel = new RTCMultiSession()
writeToChatLog($('#messageTextBox').val(), 'text-success')
channel.send({message: $('#messageTextBox').val()})
$('#messageTextBox').val('')
// Scroll chat text area to the bottom on new input.
$('#chatlog').scrollTop($('#chatlog')[0].scrollHeight)
}
return false
}
function setupDC1 () {
try {
var fileReceiver1 = new FileReceiver()
dc1 = pc1.createDataChannel('test', {reliable: true})
activedc = dc1
console.log('Created datachannel (pc1)')
dc1.onopen = function (e) {
console.log('data channel connect')
$('#waitForConnection').modal('hide')
$('#waitForConnection').remove()
}
dc1.onmessage = function (e) {
console.log('Got message (pc1)', e.data)
if (e.data.size) {
fileReceiver1.receive(e.data, {})
} else {
if (e.data.charCodeAt(0) == 2) {
// The first message we get from Firefox (but not Chrome)
// is literal ASCII 2 and I don't understand why -- if we
// leave it in, JSON.parse() will barf.
return
}
console.log(e)
var data = JSON.parse(e.data)
if (data.type === 'file') {
fileReceiver1.receive(e.data, {})
} else {
writeToChatLog(data.message, 'text-info')
// Scroll chat text area to the bottom on new input.
$('#chatlog').scrollTop($('#chatlog')[0].scrollHeight)
}
}
}
} catch (e) { console.warn('No data channel (pc1)', e); }
}
function createLocalOffer () {
console.log('video1')
navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(function (stream) {
var video = document.getElementById('localVideo')
video.srcObject = stream;
video.play()
pc1.addStream(stream)
console.log(stream)
console.log('adding stream to pc1')
setupDC1()
pc1.createOffer(function (desc) {
pc1.setLocalDescription(desc, function () {}, function () {})
console.log('created local offer', desc)
},
function () { console.warn("Couldn't create offer") },
sdpConstraints)
}).catch(function (error) {
console.log('Error adding stream to pc1: ' + error)
});
}
pc1.onicecandidate = function (e) {
console.log('ICE candidate (pc1)', e)
if (e.candidate == null) {
$('#localOffer').html(JSON.stringify(pc1.localDescription))
}
}
function handleOnaddstream (e) {
console.log('Got remote stream', e.streams[0])
var el = document.getElementById('remoteVideo')
el.autoplay = true
attachMediaStream(el, e.streams[0])
}
pc1.ontrack = handleOnaddstream
function handleOnconnection () {
console.log('Datachannel connected')
writeToChatLog('Datachannel connected', 'text-success')
$('#waitForConnection').modal('hide')
// If we didn't call remove() here, there would be a race on pc2:
// - first onconnection() hides the dialog, then someone clicks
// on answerSentBtn which shows it, and it stays shown forever.
$('#waitForConnection').remove()
$('#showLocalAnswer').modal('hide')
$('#messageTextBox').focus()
}
pc1.onconnection = handleOnconnection
function onsignalingstatechange (state) {
console.info('signaling state change:', state)
}
function oniceconnectionstatechange (state) {
console.info('ice connection state change:', state)
}
function onicegatheringstatechange (state) {
console.info('ice gathering state change:', state)
}
pc1.onsignalingstatechange = onsignalingstatechange
pc1.oniceconnectionstatechange = oniceconnectionstatechange
pc1.onicegatheringstatechange = onicegatheringstatechange
function handleAnswerFromPC2 (answerDesc) {
console.log('Received remote answer: ', answerDesc)
writeToChatLog('Received remote answer', 'text-success')
pc1.setRemoteDescription(answerDesc)
}
function handleCandidateFromPC2 (iceCandidate) {
pc1.addIceCandidate(iceCandidate)
}
/* THIS IS BOB, THE ANSWERER/RECEIVER */
var pc2 = new RTCPeerConnection(cfg, con),
dc2 = null
var pc2icedone = false
pc2.ondatachannel = function (e) {
var fileReceiver2 = new FileReceiver()
var datachannel = e.channel || e; // Chrome sends event, FF sends raw channel
console.log('Received datachannel (pc2)', arguments)
dc2 = datachannel
activedc = dc2
dc2.onopen = function (e) {
console.log('data channel connect')
$('#waitForConnection').modal('hide')
$('#waitForConnection').remove()
}
dc2.onmessage = function (e) {
console.log('Got message (pc2)', e.data)
if (e.data.size) {
fileReceiver2.receive(e.data, {})
} else {
var data = JSON.parse(e.data)
if (data.type === 'file') {
fileReceiver2.receive(e.data, {})
} else {
writeToChatLog(data.message, 'text-info')
// Scroll chat text area to the bottom on new input.
$('#chatlog').scrollTop($('#chatlog')[0].scrollHeight)
}
}
}
}
function handleOfferFromPC1 (offerDesc) {
pc2.setRemoteDescription(offerDesc)
pc2.createAnswer(function (answerDesc) {
writeToChatLog('Created local answer', 'text-success')
console.log('Created local answer: ', answerDesc)
pc2.setLocalDescription(answerDesc)
},
function () { console.warn("Couldn't create offer") },
sdpConstraints)
}
pc2.onicecandidate = function (e) {
console.log('ICE candidate (pc2)', e)
if (e.candidate == null) {
$('#localAnswer').html(JSON.stringify(pc2.localDescription))
}
}
pc2.onsignalingstatechange = onsignalingstatechange
pc2.oniceconnectionstatechange = oniceconnectionstatechange
pc2.onicegatheringstatechange = onicegatheringstatechange
function handleCandidateFromPC1 (iceCandidate) {
pc2.addIceCandidate(iceCandidate)
}
pc2.ontrack = handleOnaddstream
pc2.onconnection = handleOnconnection
function getTimestamp () {
var totalSec = new Date().getTime() / 1000
var hours = parseInt(totalSec / 3600) % 24
var minutes = parseInt(totalSec / 60) % 60
var seconds = parseInt(totalSec % 60)
var result = (hours < 10 ? '0' + hours : hours) + ':' +
(minutes < 10 ? '0' + minutes : minutes) + ':' +
(seconds < 10 ? '0' + seconds : seconds)
return result
}
function escapeHTML (unsafe_str) {
return unsafe_str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\"/g, '"')
.replace(/\'/g, ''')
.replace(/\//g, '/')
}
function writeToChatLog (message, message_type) {
document.getElementById('chatlog').innerHTML += '<p class="' + message_type + '">' + '[' + getTimestamp() + '] ' + escapeHTML(message) + '</p>'
}