-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
217 lines (191 loc) · 5.3 KB
/
app.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
(function(){
// UI
var local = document.querySelector('#local'),
remote = document.querySelector('#remote'),
callButton = document.querySelector('#callButton'),
hangupButton = document.querySelector('#hangupButton'),
localId = document.querySelector('#localId'),
remoteId = document.querySelector('#remoteId'),
console_ = document.querySelector('#console')
// EVENTS
callButton.onclick = call
hangupButton.onclick = hangup
remoteId.onkeyup = onChangeRemoteId
// DEBUG
// console = console || {}
// console.log = function(message){
// var el = document.createElement('p')
// var log = message.length ? message : JSON.stringify(message)
// log = log.slice(0, 300)
// el.innerHTML = log
// console_.appendChild(el)
// }
// SIGNALLING TRANSPORT
var socket = io()
// IDs
var selfId = parseInt(Math.random() * 99999)
localId.innerHTML = selfId
// STUN/TURN
var isChrome = !!navigator.webkitGetUserMedia,
STUN = {
url: isChrome
? 'stun:stun.l.google.com:19302'
: 'stun:23.21.150.121'
},
TURN = {
url: 'turn:[email protected]:80',
credential: 'homeo'
},
iceServers = {
iceServers: [STUN, TURN]
},
pcConstraints = {
optional: [{DtlsSrtpKeyAgreement: true}] // DTLS/SRTP is preferred on chrome to interop with Firefox which supports them by default
},
sdpConstraints = {
optional: [],
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true
}
}
// LOCAL MEDIA
var localMedia = null
getUserMedia({audio:true, video:true}, function(media){
localMedia = media
attachMediaStream(local, media)
updateReadiness()
}, function(e){
console.error('Can not get user media', e)
})
// STATE
function remotePeerId(){
return parseInt(remoteId.value)
}
function onChangeRemoteId(){
updateReadiness()
}
function updateReadiness(){
var remoteIdReady = !isNaN(remotePeerId()) && remotePeerId() > 0
if (localMedia && remoteIdReady){
setState('idle')
} else {
setState('notReady')
}
}
function setState(state){
if (state == 'notReady'){
callButton.disabled = 'disabled'
hangupButton.disabled = 'disabled'
} else if (state == 'idle'){
callButton.disabled = ''
hangupButton.disabled = 'disabled'
} else if (state == 'calling'){
callButton.disabled = 'disabled'
hangupButton.disabled = ''
}
}
// ACTIONS
var sender = null,
receiver = null
function call(){
sender = new Peer(selfId, 'sender', localMedia, remotePeerId())
sender.connection.createOffer(sender.genMessage('offer'), function(e){console.error(e)}, sdpConstraints)
setState('calling')
}
function hangup(){
sender.connection.close()
socket.send({type: 'close'})
setState('idle')
}
// REACTIONS
socket.on('message', function(data){
var peer = null
if (data.remote == 's' + selfId){
peer = sender
} else if (data.remote == 'r' + selfId){
peer = receiver
} else {
return
}
if (data.type != 'candidate'){
console.log('get message')
console.log(data)
}
switch (data.type){
case 'offer': {
if (!localMedia) alert('No video captured, try to allow')
receiver = new Peer(selfId, 'receiver', localMedia, data.sender)
receiver.setRemoteDescription(data.sdp)
receiver.connection.createAnswer(receiver.genMessage('answer'), function(e){console.error(e)}, sdpConstraints)
setState('calling')
break
}
case 'answer': {
sender.setRemoteDescription(data.sdp)
break
}
case 'candidate': {
peer.connection.addIceCandidate(new RTCIceCandidate({
sdpMLineIndex: data.candidate.sdpMLineIndex,
candidate: data.candidate.candidate
}))
break
}
case 'close': {
receiver.connection.close()
setState('idle ')
}
}
})
// PEER CONNECTION
var Peer = function(id, type, stream, remoteId){
if (!(type == 'sender' || type == 'receiver')) throw new Error('Invalid peer type')
if (type == 'sender' && !remoteId) throw new Error('Remote not specified')
if (!stream) throw new Error('Local stream not specified')
if (!remoteId) throw new Error('Remote id not specified')
this.id = type.slice(0, 1) + id
this.connection = this.createConnection(stream)
this.remoteId = remoteId.length ? remoteId : 'r' + remoteId // adding 's' or 'r' to id to enable connecting to peer on the same page
}
Peer.prototype.createConnection = function(stream){
var connection = new RTCPeerConnection(iceServers, pcConstraints)
connection.addStream(localMedia)
connection.onaddstream = function(event) {
console.log('Attaching remote stream')
console.log(event)
attachMediaStream(remote, event.stream) //TODO extract to event
}
var self = this
connection.onicecandidate = function(event){
if(event.candidate) {
self.send({
type: 'candidate',
candidate: event.candidate
})
}
}
return connection
}
Peer.prototype.setRemoteDescription = function(sdp){
this.connection.setRemoteDescription(new RTCSessionDescription(sdp))
}
Peer.prototype.send = function(message){
message.sender = this.id
message.remote = this.remoteId
socket.emit('message', message)
}
Peer.prototype.genMessage = function(type){
return genMessage(this, type)
}
var genMessage = function(peer, type){
return function(sdp){
console.log(type + ' sent, from ' + peer.id)
peer.connection.setLocalDescription(sdp)
peer.send({
type: type,
sdp: sdp
})
}
}
}())