-
Notifications
You must be signed in to change notification settings - Fork 367
/
connect.js
108 lines (94 loc) · 3.11 KB
/
connect.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
module.exports.verifyUserRoom = function (peerConnection, ui, cb) {
peerConnection.getRemoteConfig(function (err, config) {
if (err) return cb(err)
ui.inputs.paste.value = ''
// first, wait for user to enter room name
ui.buttons.paste.addEventListener('click', onJoinClick)
function onJoinClick (ev) {
ev.preventDefault()
var room = ui.inputs.paste.value
ui.inputs.paste.value = 'Connecting...'
if (!room) return
peerConnection.verifyRoom(room, function (err) {
cb(err, room, config)
})
}
})
}
module.exports.remote = function (peerConnection, ui, config, room) {
peerConnection.remotePeer(config, room, function (err, peer) {
if (err) {
ui.inputs.paste.value = 'Error! ' + err.message
return
}
if (!room) {
ui.inputs.paste.value = 'Error! Please Quit'
return
}
peer.on('stream', function (stream) { renderStreams(peerConnection, ui, stream) })
peer.on('signal', function (sdp) {
peerConnection.handleSignal(sdp, peer, true, room, function (err) {
if (err) {
ui.containers.content.innerHTML = 'Error! Please Quit. ' + err.message
return
}
console.log('SDP POST DONE')
})
})
if (peer.connected) peerConnection.onConnect(peer, true)
else peer.on('connect', function () { peerConnection.onConnect(peer, true) })
})
}
module.exports.host = function (peerConnection, ui, opts) {
if (!opts) opts = {}
getARoom(peerConnection, ui, function (err, room, config) {
if (err) {
ui.inputs.copy.value = 'Error! ' + err.message
return
}
ui.inputs.copy.value = room
opts.room = room
opts.config = config
peerConnection.hostPeer(opts, function (err, peer) {
if (err) {
ui.inputs.copy.value = 'Error! ' + err.message
return
}
if (!room) {
ui.inputs.copy.value = 'Error! Please Quit'
return
}
peer.on('stream', function (stream) { renderStreams(peerConnection, ui, stream) })
peer.on('signal', function (sdp) {
peerConnection.handleSignal(sdp, peer, false, room, function (err) {
if (err) {
ui.containers.content.innerHTML = 'Error! Please Quit. ' + err.message
return
}
})
})
if (peer.connected) peerConnection.onConnect(peer, false)
else peer.on('connect', function () { peerConnection.onConnect(peer, false) })
})
})
}
function renderStreams (peerConnection, ui, stream) {
stream.getAudioTracks().forEach(function each (track) {
var audio = peerConnection.audioElement(stream)
ui.containers.multimedia.appendChild(audio)
ui.hide(ui.containers.multimedia)
})
stream.getVideoTracks().forEach(function each (track) {
var video = peerConnection.videoElement(stream)
ui.containers.multimedia.appendChild(video)
ui.hide(ui.containers.multimedia)
})
}
function getARoom (peerConnection, ui, cb) {
peerConnection.getRemoteConfig(function (err, config) {
if (err) return cb(err)
peerConnection.createRoom(function (err, room) {
cb(err, room, config)
})
})
}