1- /*
1+ /**
22 * QuickBlox JavaScript SDK
3- *
43 * WebRTC Module (WebRTC peer connection model)
5- *
64 */
75
8- // Modules
9- //
6+ /** Modules */
107var config = require ( '../../qbConfig' ) ;
118var Helpers = require ( './qbWebRTCHelpers' ) ;
129
13- // Variable
14- //
15- // cross-browser polyfill
1610var RTCPeerConnection = window . RTCPeerConnection || window . webkitRTCPeerConnection || window . mozRTCPeerConnection ;
1711var RTCSessionDescription = window . RTCSessionDescription || window . mozRTCSessionDescription ;
1812var RTCIceCandidate = window . RTCIceCandidate || window . mozRTCIceCandidate ;
@@ -48,19 +42,19 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
4842 this . onsignalingstatechange = this . onSignalingStateCallback ;
4943 this . oniceconnectionstatechange = this . onIceConnectionStateCallback ;
5044
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. */
5346 this . dialingTimer = null ;
5447 this . answerTimeInterval = 0 ;
5548
56- // timer to detect network blips
49+ /** timer to detect network blips */
5750 this . reconnectTimer = 0 ;
5851
5952 this . iceCandidates = [ ] ;
6053} ;
6154
6255RTCPeerConnection . prototype . release = function ( ) {
6356 this . _clearDialingTimer ( ) ;
57+
6458 if ( this . signalingState !== 'closed' ) {
6559 this . close ( ) ;
6660 }
@@ -69,8 +63,9 @@ RTCPeerConnection.prototype.release = function(){
6963RTCPeerConnection . prototype . updateRemoteSDP = function ( newSDP ) {
7064 if ( ! newSDP ) {
7165 throw new Error ( "sdp string can't be empty." ) ;
66+ } else {
67+ this . remoteSDP = newSDP ;
7268 }
73- this . remoteSDP = newSDP ;
7469} ;
7570
7671RTCPeerConnection . prototype . getRemoteSDP = function ( ) {
@@ -88,19 +83,20 @@ RTCPeerConnection.prototype.setRemoteSessionDescription = function(type, remoteS
8883 }
8984
9085 this . setRemoteDescription ( desc , successCallback , errorCallback ) ;
91- }
86+ } ;
9287
9388RTCPeerConnection . prototype . addLocalStream = function ( localStream ) {
94- if ( localStream == null ) {
89+ if ( localStream ) {
90+ this . addStream ( localStream ) ;
91+ } else {
9592 throw new Error ( "'RTCPeerConnection.addStream' error: stream is 'null'." ) ;
9693 }
97- this . addStream ( localStream ) ;
98- }
94+ } ;
9995
10096RTCPeerConnection . prototype . getAndSetLocalSessionDescription = function ( callback ) {
10197 var self = this ;
10298
103- this . state = RTCPeerConnection . State . CONNECTING ;
99+ self . state = RTCPeerConnection . State . CONNECTING ;
104100
105101 if ( self . type === 'offer' ) {
106102 // Additional parameters for SDP Constraints
@@ -123,35 +119,31 @@ RTCPeerConnection.prototype.getAndSetLocalSessionDescription = function(callback
123119
124120RTCPeerConnection . prototype . addCandidates = function ( iceCandidates ) {
125121 var candidate ;
122+
126123 for ( var i = 0 , len = iceCandidates . length ; i < len ; i ++ ) {
127124 candidate = {
128125 sdpMLineIndex : iceCandidates [ i ] . sdpMLineIndex ,
129126 sdpMid : iceCandidates [ i ] . sdpMid ,
130127 candidate : iceCandidates [ i ] . candidate
131128 } ;
132- this . addIceCandidate ( new RTCIceCandidate ( candidate ) ,
133- function ( ) {
134-
135- } , function ( error ) {
129+ this . addIceCandidate (
130+ new RTCIceCandidate ( candidate ) ,
131+ function ( ) { } ,
132+ function ( error ) {
136133 Helpers . traceError ( "Error on 'addIceCandidate': " + error ) ;
137- } ) ;
134+ }
135+ ) ;
138136 }
139137} ;
140138
141139RTCPeerConnection . 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+ } ;
151142
143+ /**
144+ * CALLBACKS
145+ */
152146RTCPeerConnection . prototype . onSignalingStateCallback = function ( ) {
153- // send candidates
154- //
155147 if ( this . signalingState === 'stable' && this . iceCandidates . length > 0 ) {
156148 this . delegate . processIceCandidates ( this , this . iceCandidates ) ;
157149 this . iceCandidates . length = 0 ;
@@ -162,43 +154,43 @@ RTCPeerConnection.prototype.onIceCandidateCallback = function(event) {
162154 var candidate = event . candidate ;
163155
164156 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+ */
168161 var ICECandidate = {
169162 sdpMLineIndex : candidate . sdpMLineIndex ,
170163 sdpMid : candidate . sdpMid ,
171164 candidate : candidate . candidate
172165 } ;
173166
174167 if ( this . signalingState === 'stable' ) {
175- this . delegate . processIceCandidates ( this , [ ICECandidate ] )
168+ this . delegate . processIceCandidates ( this , [ ICECandidate ] ) ;
176169 } else {
177170 this . iceCandidates . push ( ICECandidate ) ;
178171 }
179172 }
180173} ;
181174
182- // handler of remote media stream
175+ /** handler of remote media stream */
183176RTCPeerConnection . prototype . onAddRemoteStreamCallback = function ( event ) {
184177 if ( typeof this . delegate . _onRemoteStreamListener === 'function' ) {
185178 this . delegate . _onRemoteStreamListener ( this . userID , event . stream ) ;
186179 }
187180} ;
188181
189182RTCPeerConnection . prototype . onIceConnectionStateCallback = function ( ) {
190- Helpers . trace ( "onIceConnectionStateCallback: " + this . iceConnectionState ) ;
191-
192183 var newIceConnectionState = this . iceConnectionState ;
184+
185+ Helpers . trace ( "onIceConnectionStateCallback: " + this . iceConnectionState ) ;
193186
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.
199187
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+ */
202194 if ( typeof this . delegate . _onSessionConnectionStateChangedListener === 'function' ) {
203195 var connectionState = null ;
204196
@@ -225,18 +217,15 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
225217 connectionState = Helpers . SessionConnectionState . CLOSED ;
226218 }
227219
228- if ( connectionState != null ) {
220+ if ( connectionState ) {
229221 this . delegate . _onSessionConnectionStateChangedListener ( this . userID , connectionState ) ;
230222 }
231223 }
232224} ;
233225
234-
235- //
236- /////////////////////////////////// Private ////////////////////////////////////
237- //
238-
239-
226+ /**
227+ * PRIVATE
228+ */
240229RTCPeerConnection . prototype . _clearWaitingReconnectTimer = function ( ) {
241230 if ( this . waitingReconnectTimeoutCallback ) {
242231 Helpers . trace ( '_clearWaitingReconnectTimer' ) ;
@@ -260,7 +249,7 @@ RTCPeerConnection.prototype._startWaitingReconnectTimer = function() {
260249
261250 Helpers . trace ( '_startWaitingReconnectTimer, timeout: ' + timeout ) ;
262251
263- this . waitingReconnectTimeoutCallback = setTimeout ( waitingReconnectTimeoutCallback , timeout ) ;
252+ self . waitingReconnectTimeoutCallback = setTimeout ( waitingReconnectTimeoutCallback , timeout ) ;
264253} ;
265254
266255RTCPeerConnection . prototype . _clearDialingTimer = function ( ) {
@@ -274,12 +263,11 @@ RTCPeerConnection.prototype._clearDialingTimer = function(){
274263} ;
275264
276265RTCPeerConnection . prototype . _startDialingTimer = function ( extension , withOnNotAnswerCallback ) {
266+ var self = this ;
277267 var dialingTimeInterval = config . webrtc . dialingTimeInterval * 1000 ;
278268
279269 Helpers . trace ( '_startDialingTimer, dialingTimeInterval: ' + dialingTimeInterval ) ;
280270
281- var self = this ;
282-
283271 var _dialingCallback = function ( extension , withOnNotAnswerCallback , skipIncrement ) {
284272 if ( ! skipIncrement ) {
285273 self . answerTimeInterval += config . webrtc . dialingTimeInterval * 1000 ;
@@ -298,10 +286,10 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
298286 }
299287 } ;
300288
301- this . dialingTimer = setInterval ( _dialingCallback , dialingTimeInterval , extension , withOnNotAnswerCallback , false ) ;
289+ self . dialingTimer = setInterval ( _dialingCallback , dialingTimeInterval , extension , withOnNotAnswerCallback , false ) ;
302290
303291 // call for the 1st time
304292 _dialingCallback ( extension , withOnNotAnswerCallback , true ) ;
305293} ;
306294
307- module . exports = RTCPeerConnection ;
295+ module . exports = RTCPeerConnection ;
0 commit comments