2
2
2013, Muaz Khan<muazkh>--[github.com/muaz-khan]
3
3
4
4
Demo & Documentation: http://bit.ly/RTCPeerConnection-Documentation */
5
-
6
- window . moz = ! ! navigator . mozGetUserMedia ;
5
+ window . moz = ! ! navigator . mozGetUserMedia ;
7
6
var RTCPeerConnection = function ( options ) {
8
7
var w = window ,
9
8
PeerConnection = w . mozRTCPeerConnection || w . webkitRTCPeerConnection ,
@@ -23,21 +22,31 @@ var RTCPeerConnection = function (options) {
23
22
iceServers : options . iceServers || [ STUN ]
24
23
} ;
25
24
26
- if ( ! moz && ! options . iceServers ) iceServers . iceServers = [ TURN , STUN ] ;
25
+ if ( ! moz && ! options . iceServers ) {
26
+ if ( parseInt ( navigator . userAgent . match ( / C h r o m ( e | i u m ) \/ ( [ 0 - 9 ] + ) \. / ) [ 2 ] ) >= 28 )
27
+ TURN = {
28
+ url : 'turn:numb.viagenie.ca' ,
29
+ credential : 'muazkh' ,
30
+
31
+ } ;
32
+ iceServers . iceServers = [ TURN , STUN ] ;
33
+ }
27
34
28
35
var optional = {
29
36
optional : [ ]
30
37
} ;
31
38
32
39
if ( ! moz ) {
33
40
optional . optional = [ {
34
- DtlsSrtpKeyAgreement : true
35
- } ] ;
41
+ DtlsSrtpKeyAgreement : true
42
+ }
43
+ ] ;
36
44
37
45
if ( options . onChannelMessage )
38
46
optional . optional = [ {
39
- RtpDataChannels : true
40
- } ] ;
47
+ RtpDataChannels : true
48
+ }
49
+ ] ;
41
50
}
42
51
43
52
var peerConnection = new PeerConnection ( iceServers , optional ) ;
@@ -89,7 +98,7 @@ var RTCPeerConnection = function (options) {
89
98
var inline = getChars ( ) + '\r\n' + ( extractedChars = '' ) ;
90
99
sdp = sdp . indexOf ( 'a=crypto' ) == - 1 ? sdp . replace ( / c = I N / g,
91
100
'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:' + inline +
92
- 'c=IN' ) : sdp ;
101
+ 'c=IN' ) : sdp ;
93
102
94
103
if ( options . offerSDP ) {
95
104
info ( '\n--------offer sdp provided by offerer\n' ) ;
@@ -108,10 +117,10 @@ var RTCPeerConnection = function (options) {
108
117
if ( ! options . onOfferSDP ) return ;
109
118
110
119
peerConnection . createOffer ( function ( sessionDescription ) {
111
- sessionDescription . sdp = getInteropSDP ( sessionDescription . sdp ) ;
112
- peerConnection . setLocalDescription ( sessionDescription ) ;
113
- options . onOfferSDP ( sessionDescription ) ;
114
- } , null , constraints ) ;
120
+ sessionDescription . sdp = getInteropSDP ( sessionDescription . sdp ) ;
121
+ peerConnection . setLocalDescription ( sessionDescription ) ;
122
+ options . onOfferSDP ( sessionDescription ) ;
123
+ } , null , constraints ) ;
115
124
}
116
125
117
126
function createAnswer ( ) {
@@ -121,10 +130,10 @@ var RTCPeerConnection = function (options) {
121
130
peerConnection . setRemoteDescription ( options . offerSDP ) ;
122
131
123
132
peerConnection . createAnswer ( function ( sessionDescription ) {
124
- sessionDescription . sdp = getInteropSDP ( sessionDescription . sdp ) ;
125
- peerConnection . setLocalDescription ( sessionDescription ) ;
126
- options . onAnswerSDP ( sessionDescription ) ;
127
- } , null , constraints ) ;
133
+ sessionDescription . sdp = getInteropSDP ( sessionDescription . sdp ) ;
134
+ peerConnection . setLocalDescription ( sessionDescription ) ;
135
+ options . onAnswerSDP ( sessionDescription ) ;
136
+ } , null , constraints ) ;
128
137
}
129
138
130
139
if ( ( options . onChannelMessage && ! moz ) || ! options . onChannelMessage ) {
@@ -141,12 +150,12 @@ var RTCPeerConnection = function (options) {
141
150
142
151
if ( moz && ! options . attachStream ) {
143
152
navigator . mozGetUserMedia ( {
144
- audio : true ,
145
- fake : true
146
- } , function ( stream ) {
147
- peerConnection . addStream ( stream ) ;
148
- createOffer ( ) ;
149
- } , useless ) ;
153
+ audio : true ,
154
+ fake : true
155
+ } , function ( stream ) {
156
+ peerConnection . addStream ( stream ) ;
157
+ createOffer ( ) ;
158
+ } , useless ) ;
150
159
}
151
160
}
152
161
@@ -171,6 +180,8 @@ var RTCPeerConnection = function (options) {
171
180
} ;
172
181
channel . onclose = function ( event ) {
173
182
if ( options . onChannelClosed ) options . onChannelClosed ( event ) ;
183
+
184
+ console . warn ( 'WebRTC DataChannel closed' , event ) ;
174
185
} ;
175
186
channel . onerror = function ( event ) {
176
187
console . error ( event ) ;
@@ -189,17 +200,16 @@ var RTCPeerConnection = function (options) {
189
200
190
201
if ( moz && ! options . attachStream ) {
191
202
navigator . mozGetUserMedia ( {
192
- audio : true ,
193
- fake : true
194
- } , function ( stream ) {
195
- peerConnection . addStream ( stream ) ;
196
- createAnswer ( ) ;
197
- } , useless ) ;
203
+ audio : true ,
204
+ fake : true
205
+ } , function ( stream ) {
206
+ peerConnection . addStream ( stream ) ;
207
+ createAnswer ( ) ;
208
+ } , useless ) ;
198
209
}
199
210
}
200
211
201
- function useless ( ) {
202
- }
212
+ function useless ( ) { }
203
213
204
214
function info ( information ) {
205
215
console . log ( information ) ;
@@ -216,9 +226,9 @@ var RTCPeerConnection = function (options) {
216
226
addICE : function ( candidate ) {
217
227
info ( candidate . candidate ) ;
218
228
peerConnection . addIceCandidate ( new IceCandidate ( {
219
- sdpMLineIndex : candidate . sdpMLineIndex ,
220
- candidate : candidate . candidate
221
- } ) ) ;
229
+ sdpMLineIndex : candidate . sdpMLineIndex ,
230
+ candidate : candidate . candidate
231
+ } ) ) ;
222
232
} ,
223
233
224
234
peer : peerConnection ,
@@ -235,14 +245,15 @@ var video_constraints = {
235
245
} ;
236
246
237
247
function getUserMedia ( options ) {
238
- var n = navigator , media ;
248
+ var n = navigator ,
249
+ media ;
239
250
n . getMedia = n . webkitGetUserMedia || n . mozGetUserMedia ;
240
251
n . getMedia ( options . constraints || {
241
- audio : true ,
242
- video : video_constraints
243
- } , streaming , options . onerror || function ( e ) {
244
- console . error ( e ) ;
245
- } ) ;
252
+ audio : true ,
253
+ video : video_constraints
254
+ } , streaming , options . onerror || function ( e ) {
255
+ console . error ( e ) ;
256
+ } ) ;
246
257
247
258
function streaming ( stream ) {
248
259
var video = options . video ;
0 commit comments