Skip to content

Commit 86dd7dd

Browse files
author
Igor Khomenko
committed
Merge pull request #95 from QuickBlox/develop.webrtc.bf28122015
bug fixing from 28122015
2 parents 9f36691 + 56e6efd commit 86dd7dd

14 files changed

+321
-328
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = function (grunt) {
5757
connect: {
5858
server: {
5959
options: {
60-
// protocol: 'https',
60+
protocol: 'https',
6161
hostname: 'localhost',
6262
port: 8080,
6363
open: true,

js/modules/webrtc/qbRTCPeerConnection.js

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
/*
1+
/**
22
* QuickBlox JavaScript SDK
3-
*
43
* WebRTC Module (WebRTC peer connection model)
5-
*
64
*/
75

8-
// Modules
9-
//
6+
/** Modules */
107
var config = require('../../qbConfig');
118
var Helpers = require('./qbWebRTCHelpers');
129

13-
// Variable
14-
//
15-
// cross-browser polyfill
1610
var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
1711
var RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription;
1812
var 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

6255
RTCPeerConnection.prototype.release = function(){
6356
this._clearDialingTimer();
57+
6458
if(this.signalingState !== 'closed'){
6559
this.close();
6660
}
@@ -69,8 +63,9 @@ RTCPeerConnection.prototype.release = function(){
6963
RTCPeerConnection.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

7671
RTCPeerConnection.prototype.getRemoteSDP = function(){
@@ -88,19 +83,20 @@ RTCPeerConnection.prototype.setRemoteSessionDescription = function(type, remoteS
8883
}
8984

9085
this.setRemoteDescription(desc, successCallback, errorCallback);
91-
}
86+
};
9287

9388
RTCPeerConnection.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

10096
RTCPeerConnection.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

124120
RTCPeerConnection.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

141139
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+
};
151142

143+
/**
144+
* CALLBACKS
145+
*/
152146
RTCPeerConnection.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 */
183176
RTCPeerConnection.prototype.onAddRemoteStreamCallback = function(event) {
184177
if (typeof this.delegate._onRemoteStreamListener === 'function'){
185178
this.delegate._onRemoteStreamListener(this.userID, event.stream);
186179
}
187180
};
188181

189182
RTCPeerConnection.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+
*/
240229
RTCPeerConnection.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

266255
RTCPeerConnection.prototype._clearDialingTimer = function(){
@@ -274,12 +263,11 @@ RTCPeerConnection.prototype._clearDialingTimer = function(){
274263
};
275264

276265
RTCPeerConnection.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;

js/modules/webrtc/qbWebRTCClient.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/*
1+
/**
22
* QuickBlox JavaScript SDK
3-
*
43
* WebRTC Module (WebRTC client)
5-
*
64
*/
75

86
/*
@@ -50,9 +48,9 @@ WebRTCClient.prototype.sessions = {};
5048

5149
/**
5250
* Creates the new session.
53-
* @param {number} Initiator ID
54-
* @param {array} Opponents IDs
55-
* @param {enum} Call type
51+
* @param {array} opponentsIDs Opponents IDs
52+
* @param {number} ct Call type
53+
* @param {number} cID Initiator ID
5654
*/
5755
WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID) {
5856
var opponentsIdNASessions = getOpponentsIdNASessions(this.sessions),
@@ -76,7 +74,7 @@ WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID) {
7674
WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, opponentsIDs, callType) {
7775
var newSession = new WebRTCSession(sessionID, callerID, opponentsIDs, callType, this.signalingProvider, Helpers.getIdFromNode(this.connection.jid));
7876

79-
// set callbacks
77+
/** set callbacks */
8078
newSession.onUserNotAnswerListener = this.onUserNotAnswerListener;
8179
newSession.onRemoteStreamListener = this.onRemoteStreamListener;
8280
newSession.onSessionConnectionStateChangedListener = this.onSessionConnectionStateChangedListener;
@@ -100,7 +98,6 @@ WebRTCClient.prototype.clearSession = function(sessionId){
10098
* @returns {boolean} if active or new session exist
10199
*/
102100
WebRTCClient.prototype.isExistNewOrActiveSessionExceptSessionID = function(sessionID){
103-
104101
var self = this;
105102
var exist = false;
106103

@@ -115,13 +112,13 @@ WebRTCClient.prototype.isExistNewOrActiveSessionExceptSessionID = function(sessi
115112
}
116113
});
117114
}
115+
118116
return exist;
119117
};
120118

121119
/**
122120
* DELEGATE (signaling)
123121
*/
124-
125122
WebRTCClient.prototype._onCallListener = function(userID, sessionID, extension) {
126123
Helpers.trace("onCall. UserID:" + userID + ". SessionID: " + sessionID);
127124

@@ -146,15 +143,16 @@ WebRTCClient.prototype._onCallListener = function(userID, sessionID, extension)
146143
this.onCallListener(session, extensionClone);
147144
}
148145
}
146+
149147
session.processOnCall(userID, extension);
150148
}
151149
};
152150

153151
WebRTCClient.prototype._onAcceptListener = function(userID, sessionID, extension) {
154-
Helpers.trace("onAccept. UserID:" + userID + ". SessionID: " + sessionID);
155-
156152
var session = this.sessions[sessionID];
157153

154+
Helpers.trace("onAccept. UserID:" + userID + ". SessionID: " + sessionID);
155+
158156
if(session){
159157
if(session.state === WebRTCSession.State.ACTIVE ) {
160158
var extensionClone = JSON.parse(JSON.stringify(extension));
@@ -174,10 +172,10 @@ WebRTCClient.prototype._onAcceptListener = function(userID, sessionID, extension
174172
};
175173

176174
WebRTCClient.prototype._onRejectListener = function(userID, sessionID, extension) {
177-
Helpers.trace("onReject. UserID:" + userID + ". SessionID: " + sessionID);
178-
179175
var session = this.sessions[sessionID];
180176

177+
Helpers.trace("onReject. UserID:" + userID + ". SessionID: " + sessionID);
178+
181179
if(session){
182180
var extensionClone = JSON.parse(JSON.stringify(extension));
183181
this._cleanupExtension(extensionClone);
@@ -212,10 +210,10 @@ WebRTCClient.prototype._onStopListener = function(userID, sessionID, extension)
212210
};
213211

214212
WebRTCClient.prototype._onIceCandidatesListener = function(userID, sessionID, extension) {
215-
Helpers.trace("onIceCandidates. UserID:" + userID + ". SessionID: " + sessionID + ". ICE candidates count: " + extension.iceCandidates.length);
216-
217213
var session = this.sessions[sessionID];
218214

215+
Helpers.trace("onIceCandidates. UserID:" + userID + ". SessionID: " + sessionID + ". ICE candidates count: " + extension.iceCandidates.length);
216+
219217
if(session){
220218
if(session.state === WebRTCSession.State.ACTIVE) {
221219
session.processOnIceCandidates(userID, extension);
@@ -228,10 +226,10 @@ WebRTCClient.prototype._onIceCandidatesListener = function(userID, sessionID, ex
228226
};
229227

230228
WebRTCClient.prototype._onUpdateListener = function(userID, sessionID, extension) {
231-
Helpers.trace("onUpdate. UserID:" + userID + ". SessionID: " + sessionID + ". Extension: " + JSON.stringify(extension));
232-
233229
var session = this.sessions[sessionID];
234230

231+
Helpers.trace("onUpdate. UserID:" + userID + ". SessionID: " + sessionID + ". Extension: " + JSON.stringify(extension));
232+
235233
if (typeof this.onUpdateCallListener === 'function'){
236234
this.onUpdateCallListener(session, userID, extension);
237235
}
@@ -280,4 +278,4 @@ function getOpponentsIdNASessions(sessions) {
280278
}
281279

282280
return opponents;
283-
}
281+
}

0 commit comments

Comments
 (0)