@@ -62,6 +62,9 @@ function WebRTCSession(sessionID, initiatorID, opIDs, callType, signalingProvide
6262 // We need a way to hide it if sach situation happened."
6363 //
6464 this . answerTimer = null ;
65+
66+ this . startCallTime = 0 ;
67+ this . acceptCallTime = 0 ;
6568}
6669
6770/**
@@ -170,11 +173,11 @@ WebRTCSession.prototype.call = function(extension) {
170173
171174 // create a peer connection for each opponent
172175 self . opponentsIDs . forEach ( function ( userID , i , arr ) {
173- self . _callInternal ( userID , ext ) ;
176+ self . _callInternal ( userID , ext , true ) ;
174177 } ) ;
175178} ;
176179
177- WebRTCSession . prototype . _callInternal = function ( userID , extension ) {
180+ WebRTCSession . prototype . _callInternal = function ( userID , extension , withOnNotAnswerCallback ) {
178181
179182 var peer = this . _createPeer ( userID , 'offer' ) ;
180183 peer . addLocalStream ( this . localStream ) ;
@@ -187,7 +190,7 @@ WebRTCSession.prototype._callInternal = function(userID, extension) {
187190 Helpers . trace ( "getAndSetLocalSessionDescription success" ) ;
188191 // let's send call requests to user
189192 //
190- peer . _startDialingTimer ( extension ) ;
193+ peer . _startDialingTimer ( extension , withOnNotAnswerCallback ) ;
191194 }
192195 } ) ;
193196} ;
@@ -206,27 +209,34 @@ WebRTCSession.prototype.accept = function(extension) {
206209 * Check state of current session
207210 */
208211 if ( self . state === WebRTCSession . State . ACTIVE ) {
209- Helpers . traceError ( 'Session already active' ) ;
212+ Helpers . traceError ( "Can't accept, the session is already active, return." ) ;
210213 return ;
211214 }
212215
213216 self . state = WebRTCSession . State . ACTIVE ;
214217
218+ self . acceptCallTime = new Date ( ) ;
219+
215220 self . _clearAnswerTimer ( ) ;
216221
217222 self . _acceptInternal ( self . initiatorID , ext ) ;
218223
219224 // The group call logic starts here
220225 var oppIDs = self . _uniqueOpponentsIDsWithoutInitiator ( ) ;
221226
227+ // in a case of group video chat
222228 if ( oppIDs . length > 0 ) {
229+
230+ var offerTime = ( self . acceptCallTime - self . startCallTime ) / 1000 ;
231+ self . _startWaitingOfferOrAnswerTimer ( offerTime ) ;
232+
223233 // here we have to decide to which users the user should call.
224234 // We have a rule: If a userID1 > userID2 then a userID1 should call to userID2.
225235 //
226236 oppIDs . forEach ( function ( opID , i , arr ) {
227237 if ( self . currentUserID > opID ) {
228238 // call to the user
229- self . _callInternal ( opID , { } ) ;
239+ self . _callInternal ( opID , { } , false ) ;
230240 }
231241 } ) ;
232242 }
@@ -418,6 +428,8 @@ WebRTCSession.filter = function(id, filters) {
418428WebRTCSession . prototype . processOnCall = function ( callerID , extension ) {
419429 var self = this ;
420430
431+ this . _clearWaitingOfferOrAnswerTimer ( ) ;
432+
421433 var oppIDs = this . _uniqueOpponentsIDs ( ) ;
422434 oppIDs . forEach ( function ( opID , i , arr ) {
423435
@@ -453,6 +465,8 @@ WebRTCSession.prototype.processOnCall = function(callerID, extension) {
453465} ;
454466
455467WebRTCSession . prototype . processOnAccept = function ( userID , extension ) {
468+ this . _clearWaitingOfferOrAnswerTimer ( ) ;
469+
456470 var peerConnection = this . peerConnections [ userID ] ;
457471 if ( peerConnection ) {
458472 peerConnection . _clearDialingTimer ( ) ;
@@ -469,6 +483,8 @@ WebRTCSession.prototype.processOnAccept = function(userID, extension) {
469483} ;
470484
471485WebRTCSession . prototype . processOnReject = function ( userID , extension ) {
486+ this . _clearWaitingOfferOrAnswerTimer ( ) ;
487+
472488 var peerConnection = this . peerConnections [ userID ] ;
473489 if ( peerConnection ) {
474490 peerConnection . _clearDialingTimer ( ) ;
@@ -549,6 +565,9 @@ WebRTCSession.prototype.processIceCandidates = function(peerConnection, iceCandi
549565WebRTCSession . prototype . processOnNotAnswer = function ( peerConnection ) {
550566 Helpers . trace ( "Answer timeout callback for session " + this . ID + " for user " + peerConnection . userID ) ;
551567
568+ this . _clearWaitingOfferOrAnswerTimer ( ) ;
569+
570+ peerConnection . _clearDialingTimer ( ) ;
552571 peerConnection . release ( ) ;
553572
554573 if ( typeof this . onUserNotAnswerListener === 'function' ) {
@@ -571,9 +590,10 @@ WebRTCSession.prototype._onRemoteStreamListener = function(userID, stream) {
571590} ;
572591
573592WebRTCSession . prototype . _onSessionConnectionStateChangedListener = function ( userID , connectionState ) {
593+ var self = this ;
574594
575- if ( typeof this . onSessionConnectionStateChangedListener === 'function' ) {
576- this . onSessionConnectionStateChangedListener ( this , userID , connectionState ) ;
595+ if ( typeof self . onSessionConnectionStateChangedListener === 'function' ) {
596+ self . onSessionConnectionStateChangedListener ( self , userID , connectionState ) ;
577597 }
578598
579599 if ( connectionState === Helpers . SessionConnectionState . CLOSED ) {
@@ -591,6 +611,8 @@ WebRTCSession.prototype._createPeer = function(userID, peerConnectionType) {
591611
592612 if ( ! RTCPeerConnection ) throw new Error ( 'RTCPeerConnection() is not supported in your browser' ) ;
593613
614+ this . startCallTime = new Date ( ) ;
615+
594616 // Additional parameters for RTCPeerConnection options
595617 // new RTCPeerConnection(pcConfig, options)
596618 /**********************************************
@@ -682,9 +704,8 @@ WebRTCSession.prototype._muteStream = function(bool, type) {
682704} ;
683705
684706WebRTCSession . prototype . _clearAnswerTimer = function ( ) {
685- Helpers . trace ( "_clearAnswerTimer" ) ;
686-
687707 if ( this . answerTimer ) {
708+ Helpers . trace ( "_clearAnswerTimer" ) ;
688709 clearTimeout ( this . answerTimer ) ;
689710 this . answerTimer = null ;
690711 }
@@ -708,6 +729,38 @@ WebRTCSession.prototype._startAnswerTimer = function(){
708729 this . answerTimer = setTimeout ( answerTimeoutCallback , answerTimeInterval ) ;
709730} ;
710731
732+ WebRTCSession . prototype . _clearWaitingOfferOrAnswerTimer = function ( ) {
733+ if ( this . waitingOfferOrAnswerTimer ) {
734+ Helpers . trace ( "_clearWaitingOfferOrAnswerTimer" ) ;
735+ clearTimeout ( this . waitingOfferOrAnswerTimer ) ;
736+ this . waitingOfferOrAnswerTimer = null ;
737+ }
738+ }
739+
740+ WebRTCSession . prototype . _startWaitingOfferOrAnswerTimer = function ( time ) {
741+
742+ var self = this ,
743+ timeout = ( config . webrtc . answerTimeInterval - time ) < 0 ? 1 : config . webrtc . answerTimeInterval - time ,
744+ waitingOfferOrAnswerTimeoutCallback = function ( ) {
745+ Helpers . trace ( "waitingOfferOrAnswerTimeoutCallback" ) ;
746+
747+ if ( Object . keys ( self . peerConnections ) . length > 0 ) {
748+ Object . keys ( self . peerConnections ) . forEach ( function ( key ) {
749+ var peerConnection = self . peerConnections [ key ] ;
750+ if ( peerConnection . state !== RTCPeerConnection . State . CONNECTED ) {
751+ self . processOnNotAnswer ( peerConnection ) ;
752+ }
753+ } ) ;
754+ }
755+
756+ self . waitingOfferOrAnswerTimer = null ;
757+ } ;
758+
759+ Helpers . trace ( "_startWaitingOfferOrAnswerTimer, timeout: " + timeout ) ;
760+
761+ this . waitingOfferOrAnswerTimer = setTimeout ( waitingOfferOrAnswerTimeoutCallback , timeout * 1000 ) ;
762+ } ;
763+
711764WebRTCSession . prototype . _uniqueOpponentsIDs = function ( ) {
712765 var self = this ;
713766 var opponents = [ ] ;
0 commit comments