Skip to content

Commit 531894d

Browse files
author
Igor Khomenko
committed
Merge pull request #44 from QuickBlox/develop.webrtc.QBWEBSDK-124_v2
QBWEBSDK-124
2 parents 42dc725 + 0d14535 commit 531894d

File tree

5 files changed

+87
-14574
lines changed

5 files changed

+87
-14574
lines changed

js/modules/webrtc/qbRTCPeerConnection.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
4848
this.onsignalingstatechange = this.onSignalingStateCallback;
4949
this.oniceconnectionstatechange = this.onIceConnectionStateCallback;
5050

51-
// We use this timer interval to dial a user - produce the call reqeusts each N seconds.
51+
// We use this timer interval to dial a user - produce the call requests each N seconds.
5252
//
5353
this.dialingTimer = null;
5454
this.answerTimeInterval = 0;
@@ -221,38 +221,44 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
221221

222222

223223
RTCPeerConnection.prototype._clearDialingTimer = function(){
224-
Helpers.trace("_clearDialingTimer");
225-
226224
if(this.dialingTimer){
225+
Helpers.trace("_clearDialingTimer");
226+
227227
clearInterval(this.dialingTimer);
228228
this.dialingTimer = null;
229229
this.answerTimeInterval = 0;
230230
}
231231
}
232232

233-
RTCPeerConnection.prototype._startDialingTimer = function(extension){
233+
RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAnswerCallback){
234234
var dialingTimeInterval = config.webrtc.dialingTimeInterval*1000;
235235

236236
Helpers.trace("_startDialingTimer, dialingTimeInterval: " + dialingTimeInterval);
237237

238238
var self = this;
239239

240-
var _dialingCallback = function(extension){
241-
self.answerTimeInterval += config.webrtc.dialingTimeInterval*1000;
240+
var _dialingCallback = function(extension, withOnNotAnswerCallback, skipIncrement){
241+
if(!skipIncrement){
242+
self.answerTimeInterval += config.webrtc.dialingTimeInterval*1000;
243+
}
242244

243245
Helpers.trace("_dialingCallback, answerTimeInterval: " + self.answerTimeInterval);
244246

245247
if(self.answerTimeInterval >= config.webrtc.answerTimeInterval*1000){
246248
self._clearDialingTimer();
247249

248-
self.delegate.processOnNotAnswer(self);
250+
if(withOnNotAnswerCallback){
251+
self.delegate.processOnNotAnswer(self);
252+
}
249253
}else{
250254
self.delegate.processCall(self, extension);
251255
}
252256
}
253257

254-
this.dialingTimer = setInterval(_dialingCallback, dialingTimeInterval, extension);
255-
_dialingCallback(extension);
258+
this.dialingTimer = setInterval(_dialingCallback, dialingTimeInterval, extension, withOnNotAnswerCallback, false);
259+
260+
// call for the 1st time
261+
_dialingCallback(extension, withOnNotAnswerCallback, true);
256262
}
257263

258264

js/modules/webrtc/qbWebRTCSession.js

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
418428
WebRTCSession.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

455467
WebRTCSession.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

471485
WebRTCSession.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
549565
WebRTCSession.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

573592
WebRTCSession.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

684706
WebRTCSession.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+
711764
WebRTCSession.prototype._uniqueOpponentsIDs = function(){
712765
var self = this;
713766
var opponents = [];

0 commit comments

Comments
 (0)