1
- /*
1
+ /**
2
2
* QuickBlox JavaScript SDK
3
- *
4
3
* WebRTC Module (WebRTC peer connection model)
5
- *
6
4
*/
7
5
8
- // Modules
9
- //
6
+ /** Modules */
10
7
var config = require ( '../../qbConfig' ) ;
11
8
var Helpers = require ( './qbWebRTCHelpers' ) ;
12
9
13
- // Variable
14
- //
15
- // cross-browser polyfill
16
10
var RTCPeerConnection = window . RTCPeerConnection || window . webkitRTCPeerConnection || window . mozRTCPeerConnection ;
17
11
var RTCSessionDescription = window . RTCSessionDescription || window . mozRTCSessionDescription ;
18
12
var RTCIceCandidate = window . RTCIceCandidate || window . mozRTCIceCandidate ;
@@ -48,19 +42,19 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
48
42
this . onsignalingstatechange = this . onSignalingStateCallback ;
49
43
this . oniceconnectionstatechange = this . onIceConnectionStateCallback ;
50
44
51
- // We use this timer interval to dial a user - produce the call requests each N seconds.
52
- //
45
+ /** We use this timer interval to dial a user - produce the call requests each N seconds. */
53
46
this . dialingTimer = null ;
54
47
this . answerTimeInterval = 0 ;
55
48
56
- // timer to detect network blips
49
+ /** timer to detect network blips */
57
50
this . reconnectTimer = 0 ;
58
51
59
52
this . iceCandidates = [ ] ;
60
53
} ;
61
54
62
55
RTCPeerConnection . prototype . release = function ( ) {
63
56
this . _clearDialingTimer ( ) ;
57
+
64
58
if ( this . signalingState !== 'closed' ) {
65
59
this . close ( ) ;
66
60
}
@@ -69,8 +63,9 @@ RTCPeerConnection.prototype.release = function(){
69
63
RTCPeerConnection . prototype . updateRemoteSDP = function ( newSDP ) {
70
64
if ( ! newSDP ) {
71
65
throw new Error ( "sdp string can't be empty." ) ;
66
+ } else {
67
+ this . remoteSDP = newSDP ;
72
68
}
73
- this . remoteSDP = newSDP ;
74
69
} ;
75
70
76
71
RTCPeerConnection . prototype . getRemoteSDP = function ( ) {
@@ -88,19 +83,20 @@ RTCPeerConnection.prototype.setRemoteSessionDescription = function(type, remoteS
88
83
}
89
84
90
85
this . setRemoteDescription ( desc , successCallback , errorCallback ) ;
91
- }
86
+ } ;
92
87
93
88
RTCPeerConnection . prototype . addLocalStream = function ( localStream ) {
94
- if ( localStream == null ) {
89
+ if ( localStream ) {
90
+ this . addStream ( localStream ) ;
91
+ } else {
95
92
throw new Error ( "'RTCPeerConnection.addStream' error: stream is 'null'." ) ;
96
93
}
97
- this . addStream ( localStream ) ;
98
- }
94
+ } ;
99
95
100
96
RTCPeerConnection . prototype . getAndSetLocalSessionDescription = function ( callback ) {
101
97
var self = this ;
102
98
103
- this . state = RTCPeerConnection . State . CONNECTING ;
99
+ self . state = RTCPeerConnection . State . CONNECTING ;
104
100
105
101
if ( self . type === 'offer' ) {
106
102
// Additional parameters for SDP Constraints
@@ -123,35 +119,31 @@ RTCPeerConnection.prototype.getAndSetLocalSessionDescription = function(callback
123
119
124
120
RTCPeerConnection . prototype . addCandidates = function ( iceCandidates ) {
125
121
var candidate ;
122
+
126
123
for ( var i = 0 , len = iceCandidates . length ; i < len ; i ++ ) {
127
124
candidate = {
128
125
sdpMLineIndex : iceCandidates [ i ] . sdpMLineIndex ,
129
126
sdpMid : iceCandidates [ i ] . sdpMid ,
130
127
candidate : iceCandidates [ i ] . candidate
131
128
} ;
132
- this . addIceCandidate ( new RTCIceCandidate ( candidate ) ,
133
- function ( ) {
134
-
135
- } , function ( error ) {
129
+ this . addIceCandidate (
130
+ new RTCIceCandidate ( candidate ) ,
131
+ function ( ) { } ,
132
+ function ( error ) {
136
133
Helpers . traceError ( "Error on 'addIceCandidate': " + error ) ;
137
- } ) ;
134
+ }
135
+ ) ;
138
136
}
139
137
} ;
140
138
141
139
RTCPeerConnection . prototype . toString = function sessionToString ( ) {
142
- var ret = 'sessionID: ' + this . sessionID + ', userID: ' + this . userID + ', type: ' +
143
- this . type + ', state: ' + this . state ;
144
- return ret ;
145
- }
146
-
147
- //
148
- ////////////////////////////////// Callbacks ///////////////////////////////////
149
- //
150
-
140
+ return 'sessionID: ' + this . sessionID + ', userID: ' + this . userID + ', type: ' + this . type + ', state: ' + this . state ;
141
+ } ;
151
142
143
+ /**
144
+ * CALLBACKS
145
+ */
152
146
RTCPeerConnection . prototype . onSignalingStateCallback = function ( ) {
153
- // send candidates
154
- //
155
147
if ( this . signalingState === 'stable' && this . iceCandidates . length > 0 ) {
156
148
this . delegate . processIceCandidates ( this , this . iceCandidates ) ;
157
149
this . iceCandidates . length = 0 ;
@@ -162,43 +154,43 @@ RTCPeerConnection.prototype.onIceCandidateCallback = function(event) {
162
154
var candidate = event . candidate ;
163
155
164
156
if ( candidate ) {
165
- // collecting internally the ice candidates
166
- // will send a bit later
167
- //
157
+ /**
158
+ * collecting internally the ice candidates
159
+ * will send a bit later
160
+ */
168
161
var ICECandidate = {
169
162
sdpMLineIndex : candidate . sdpMLineIndex ,
170
163
sdpMid : candidate . sdpMid ,
171
164
candidate : candidate . candidate
172
165
} ;
173
166
174
167
if ( this . signalingState === 'stable' ) {
175
- this . delegate . processIceCandidates ( this , [ ICECandidate ] )
168
+ this . delegate . processIceCandidates ( this , [ ICECandidate ] ) ;
176
169
} else {
177
170
this . iceCandidates . push ( ICECandidate ) ;
178
171
}
179
172
}
180
173
} ;
181
174
182
- // handler of remote media stream
175
+ /** handler of remote media stream */
183
176
RTCPeerConnection . prototype . onAddRemoteStreamCallback = function ( event ) {
184
177
if ( typeof this . delegate . _onRemoteStreamListener === 'function' ) {
185
178
this . delegate . _onRemoteStreamListener ( this . userID , event . stream ) ;
186
179
}
187
180
} ;
188
181
189
182
RTCPeerConnection . prototype . onIceConnectionStateCallback = function ( ) {
190
- Helpers . trace ( "onIceConnectionStateCallback: " + this . iceConnectionState ) ;
191
-
192
183
var newIceConnectionState = this . iceConnectionState ;
184
+
185
+ Helpers . trace ( "onIceConnectionStateCallback: " + this . iceConnectionState ) ;
193
186
194
- // read more about all states:
195
- // http://w3c.github.io/webrtc-pc/#idl-def-RTCIceConnectionState
196
- //
197
- // 'disconnected' happens in a case when a user has killed an application (for example, on iOS/Android via task manager).
198
- // So we should notify our user about it.
199
187
200
- // notify user about state changes
201
- //
188
+ /**
189
+ * read more about all states:
190
+ * http://w3c.github.io/webrtc-pc/#idl-def-RTCIceConnectionState
191
+ * 'disconnected' happens in a case when a user has killed an application (for example, on iOS/Android via task manager).
192
+ * So we should notify our user about it.
193
+ */
202
194
if ( typeof this . delegate . _onSessionConnectionStateChangedListener === 'function' ) {
203
195
var connectionState = null ;
204
196
@@ -225,18 +217,15 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
225
217
connectionState = Helpers . SessionConnectionState . CLOSED ;
226
218
}
227
219
228
- if ( connectionState != null ) {
220
+ if ( connectionState ) {
229
221
this . delegate . _onSessionConnectionStateChangedListener ( this . userID , connectionState ) ;
230
222
}
231
223
}
232
224
} ;
233
225
234
-
235
- //
236
- /////////////////////////////////// Private ////////////////////////////////////
237
- //
238
-
239
-
226
+ /**
227
+ * PRIVATE
228
+ */
240
229
RTCPeerConnection . prototype . _clearWaitingReconnectTimer = function ( ) {
241
230
if ( this . waitingReconnectTimeoutCallback ) {
242
231
Helpers . trace ( '_clearWaitingReconnectTimer' ) ;
@@ -260,7 +249,7 @@ RTCPeerConnection.prototype._startWaitingReconnectTimer = function() {
260
249
261
250
Helpers . trace ( '_startWaitingReconnectTimer, timeout: ' + timeout ) ;
262
251
263
- this . waitingReconnectTimeoutCallback = setTimeout ( waitingReconnectTimeoutCallback , timeout ) ;
252
+ self . waitingReconnectTimeoutCallback = setTimeout ( waitingReconnectTimeoutCallback , timeout ) ;
264
253
} ;
265
254
266
255
RTCPeerConnection . prototype . _clearDialingTimer = function ( ) {
@@ -274,12 +263,11 @@ RTCPeerConnection.prototype._clearDialingTimer = function(){
274
263
} ;
275
264
276
265
RTCPeerConnection . prototype . _startDialingTimer = function ( extension , withOnNotAnswerCallback ) {
266
+ var self = this ;
277
267
var dialingTimeInterval = config . webrtc . dialingTimeInterval * 1000 ;
278
268
279
269
Helpers . trace ( '_startDialingTimer, dialingTimeInterval: ' + dialingTimeInterval ) ;
280
270
281
- var self = this ;
282
-
283
271
var _dialingCallback = function ( extension , withOnNotAnswerCallback , skipIncrement ) {
284
272
if ( ! skipIncrement ) {
285
273
self . answerTimeInterval += config . webrtc . dialingTimeInterval * 1000 ;
@@ -298,10 +286,10 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
298
286
}
299
287
} ;
300
288
301
- this . dialingTimer = setInterval ( _dialingCallback , dialingTimeInterval , extension , withOnNotAnswerCallback , false ) ;
289
+ self . dialingTimer = setInterval ( _dialingCallback , dialingTimeInterval , extension , withOnNotAnswerCallback , false ) ;
302
290
303
291
// call for the 1st time
304
292
_dialingCallback ( extension , withOnNotAnswerCallback , true ) ;
305
293
} ;
306
294
307
- module . exports = RTCPeerConnection ;
295
+ module . exports = RTCPeerConnection ;
0 commit comments