Skip to content

bug fixing from 28122015 #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = function (grunt) {
connect: {
server: {
options: {
// protocol: 'https',
protocol: 'https',
hostname: 'localhost',
port: 8080,
open: true,
Expand Down
106 changes: 47 additions & 59 deletions js/modules/webrtc/qbRTCPeerConnection.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
/*
/**
* QuickBlox JavaScript SDK
*
* WebRTC Module (WebRTC peer connection model)
*
*/

// Modules
//
/** Modules */
var config = require('../../qbConfig');
var Helpers = require('./qbWebRTCHelpers');

// Variable
//
// cross-browser polyfill
var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
var RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription;
var RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate;
Expand Down Expand Up @@ -48,19 +42,19 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
this.onsignalingstatechange = this.onSignalingStateCallback;
this.oniceconnectionstatechange = this.onIceConnectionStateCallback;

// We use this timer interval to dial a user - produce the call requests each N seconds.
//
/** We use this timer interval to dial a user - produce the call requests each N seconds. */
this.dialingTimer = null;
this.answerTimeInterval = 0;

// timer to detect network blips
/** timer to detect network blips */
this.reconnectTimer = 0;

this.iceCandidates = [];
};

RTCPeerConnection.prototype.release = function(){
this._clearDialingTimer();

if(this.signalingState !== 'closed'){
this.close();
}
Expand All @@ -69,8 +63,9 @@ RTCPeerConnection.prototype.release = function(){
RTCPeerConnection.prototype.updateRemoteSDP = function(newSDP){
if(!newSDP){
throw new Error("sdp string can't be empty.");
} else {
this.remoteSDP = newSDP;
}
this.remoteSDP = newSDP;
};

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

this.setRemoteDescription(desc, successCallback, errorCallback);
}
};

RTCPeerConnection.prototype.addLocalStream = function(localStream){
if(localStream == null){
if(localStream){
this.addStream(localStream);
} else {
throw new Error("'RTCPeerConnection.addStream' error: stream is 'null'.");
}
this.addStream(localStream);
}
};

RTCPeerConnection.prototype.getAndSetLocalSessionDescription = function(callback) {
var self = this;

this.state = RTCPeerConnection.State.CONNECTING;
self.state = RTCPeerConnection.State.CONNECTING;

if (self.type === 'offer') {
// Additional parameters for SDP Constraints
Expand All @@ -123,35 +119,31 @@ RTCPeerConnection.prototype.getAndSetLocalSessionDescription = function(callback

RTCPeerConnection.prototype.addCandidates = function(iceCandidates) {
var candidate;

for (var i = 0, len = iceCandidates.length; i < len; i++) {
candidate = {
sdpMLineIndex: iceCandidates[i].sdpMLineIndex,
sdpMid: iceCandidates[i].sdpMid,
candidate: iceCandidates[i].candidate
};
this.addIceCandidate(new RTCIceCandidate(candidate),
function() {

}, function(error){
this.addIceCandidate(
new RTCIceCandidate(candidate),
function() {},
function(error){
Helpers.traceError("Error on 'addIceCandidate': " + error);
});
}
);
}
};

RTCPeerConnection.prototype.toString = function sessionToString() {
var ret = 'sessionID: ' + this.sessionID + ', userID: ' + this.userID + ', type: ' +
this.type + ', state: ' + this.state;
return ret;
}

//
////////////////////////////////// Callbacks ///////////////////////////////////
//

return 'sessionID: ' + this.sessionID + ', userID: ' + this.userID + ', type: ' + this.type + ', state: ' + this.state;
};

/**
* CALLBACKS
*/
RTCPeerConnection.prototype.onSignalingStateCallback = function() {
// send candidates
//
if (this.signalingState === 'stable' && this.iceCandidates.length > 0){
this.delegate.processIceCandidates(this, this.iceCandidates);
this.iceCandidates.length = 0;
Expand All @@ -162,43 +154,43 @@ RTCPeerConnection.prototype.onIceCandidateCallback = function(event) {
var candidate = event.candidate;

if (candidate) {
// collecting internally the ice candidates
// will send a bit later
//
/**
* collecting internally the ice candidates
* will send a bit later
*/
var ICECandidate = {
sdpMLineIndex: candidate.sdpMLineIndex,
sdpMid: candidate.sdpMid,
candidate: candidate.candidate
};

if(this.signalingState === 'stable'){
this.delegate.processIceCandidates(this, [ICECandidate])
this.delegate.processIceCandidates(this, [ICECandidate]);
}else{
this.iceCandidates.push(ICECandidate);
}
}
};

// handler of remote media stream
/** handler of remote media stream */
RTCPeerConnection.prototype.onAddRemoteStreamCallback = function(event) {
if (typeof this.delegate._onRemoteStreamListener === 'function'){
this.delegate._onRemoteStreamListener(this.userID, event.stream);
}
};

RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
Helpers.trace("onIceConnectionStateCallback: " + this.iceConnectionState);

var newIceConnectionState = this.iceConnectionState;

Helpers.trace("onIceConnectionStateCallback: " + this.iceConnectionState);

// read more about all states:
// http://w3c.github.io/webrtc-pc/#idl-def-RTCIceConnectionState
//
// 'disconnected' happens in a case when a user has killed an application (for example, on iOS/Android via task manager).
// So we should notify our user about it.

// notify user about state changes
//
/**
* read more about all states:
* http://w3c.github.io/webrtc-pc/#idl-def-RTCIceConnectionState
* 'disconnected' happens in a case when a user has killed an application (for example, on iOS/Android via task manager).
* So we should notify our user about it.
*/
if(typeof this.delegate._onSessionConnectionStateChangedListener === 'function'){
var connectionState = null;

Expand All @@ -225,18 +217,15 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
connectionState = Helpers.SessionConnectionState.CLOSED;
}

if(connectionState != null){
if(connectionState){
this.delegate._onSessionConnectionStateChangedListener(this.userID, connectionState);
}
}
};


//
/////////////////////////////////// Private ////////////////////////////////////
//


/**
* PRIVATE
*/
RTCPeerConnection.prototype._clearWaitingReconnectTimer = function() {
if(this.waitingReconnectTimeoutCallback){
Helpers.trace('_clearWaitingReconnectTimer');
Expand All @@ -260,7 +249,7 @@ RTCPeerConnection.prototype._startWaitingReconnectTimer = function() {

Helpers.trace('_startWaitingReconnectTimer, timeout: ' + timeout);

this.waitingReconnectTimeoutCallback = setTimeout(waitingReconnectTimeoutCallback, timeout);
self.waitingReconnectTimeoutCallback = setTimeout(waitingReconnectTimeoutCallback, timeout);
};

RTCPeerConnection.prototype._clearDialingTimer = function(){
Expand All @@ -274,12 +263,11 @@ RTCPeerConnection.prototype._clearDialingTimer = function(){
};

RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAnswerCallback){
var self = this;
var dialingTimeInterval = config.webrtc.dialingTimeInterval*1000;

Helpers.trace('_startDialingTimer, dialingTimeInterval: ' + dialingTimeInterval);

var self = this;

var _dialingCallback = function(extension, withOnNotAnswerCallback, skipIncrement){
if(!skipIncrement){
self.answerTimeInterval += config.webrtc.dialingTimeInterval*1000;
Expand All @@ -298,10 +286,10 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
}
};

this.dialingTimer = setInterval(_dialingCallback, dialingTimeInterval, extension, withOnNotAnswerCallback, false);
self.dialingTimer = setInterval(_dialingCallback, dialingTimeInterval, extension, withOnNotAnswerCallback, false);

// call for the 1st time
_dialingCallback(extension, withOnNotAnswerCallback, true);
};

module.exports = RTCPeerConnection;
module.exports = RTCPeerConnection;
34 changes: 16 additions & 18 deletions js/modules/webrtc/qbWebRTCClient.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*
/**
* QuickBlox JavaScript SDK
*
* WebRTC Module (WebRTC client)
*
*/

/*
Expand Down Expand Up @@ -50,9 +48,9 @@ WebRTCClient.prototype.sessions = {};

/**
* Creates the new session.
* @param {number} Initiator ID
* @param {array} Opponents IDs
* @param {enum} Call type
* @param {array} opponentsIDs Opponents IDs
* @param {number} ct Call type
* @param {number} cID Initiator ID
*/
WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID) {
var opponentsIdNASessions = getOpponentsIdNASessions(this.sessions),
Expand All @@ -76,7 +74,7 @@ WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID) {
WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, opponentsIDs, callType) {
var newSession = new WebRTCSession(sessionID, callerID, opponentsIDs, callType, this.signalingProvider, Helpers.getIdFromNode(this.connection.jid));

// set callbacks
/** set callbacks */
newSession.onUserNotAnswerListener = this.onUserNotAnswerListener;
newSession.onRemoteStreamListener = this.onRemoteStreamListener;
newSession.onSessionConnectionStateChangedListener = this.onSessionConnectionStateChangedListener;
Expand All @@ -100,7 +98,6 @@ WebRTCClient.prototype.clearSession = function(sessionId){
* @returns {boolean} if active or new session exist
*/
WebRTCClient.prototype.isExistNewOrActiveSessionExceptSessionID = function(sessionID){

var self = this;
var exist = false;

Expand All @@ -115,13 +112,13 @@ WebRTCClient.prototype.isExistNewOrActiveSessionExceptSessionID = function(sessi
}
});
}

return exist;
};

/**
* DELEGATE (signaling)
*/

WebRTCClient.prototype._onCallListener = function(userID, sessionID, extension) {
Helpers.trace("onCall. UserID:" + userID + ". SessionID: " + sessionID);

Expand All @@ -146,15 +143,16 @@ WebRTCClient.prototype._onCallListener = function(userID, sessionID, extension)
this.onCallListener(session, extensionClone);
}
}

session.processOnCall(userID, extension);
}
};

WebRTCClient.prototype._onAcceptListener = function(userID, sessionID, extension) {
Helpers.trace("onAccept. UserID:" + userID + ". SessionID: " + sessionID);

var session = this.sessions[sessionID];

Helpers.trace("onAccept. UserID:" + userID + ". SessionID: " + sessionID);

if(session){
if(session.state === WebRTCSession.State.ACTIVE ) {
var extensionClone = JSON.parse(JSON.stringify(extension));
Expand All @@ -174,10 +172,10 @@ WebRTCClient.prototype._onAcceptListener = function(userID, sessionID, extension
};

WebRTCClient.prototype._onRejectListener = function(userID, sessionID, extension) {
Helpers.trace("onReject. UserID:" + userID + ". SessionID: " + sessionID);

var session = this.sessions[sessionID];

Helpers.trace("onReject. UserID:" + userID + ". SessionID: " + sessionID);

if(session){
var extensionClone = JSON.parse(JSON.stringify(extension));
this._cleanupExtension(extensionClone);
Expand Down Expand Up @@ -212,10 +210,10 @@ WebRTCClient.prototype._onStopListener = function(userID, sessionID, extension)
};

WebRTCClient.prototype._onIceCandidatesListener = function(userID, sessionID, extension) {
Helpers.trace("onIceCandidates. UserID:" + userID + ". SessionID: " + sessionID + ". ICE candidates count: " + extension.iceCandidates.length);

var session = this.sessions[sessionID];

Helpers.trace("onIceCandidates. UserID:" + userID + ". SessionID: " + sessionID + ". ICE candidates count: " + extension.iceCandidates.length);

if(session){
if(session.state === WebRTCSession.State.ACTIVE) {
session.processOnIceCandidates(userID, extension);
Expand All @@ -228,10 +226,10 @@ WebRTCClient.prototype._onIceCandidatesListener = function(userID, sessionID, ex
};

WebRTCClient.prototype._onUpdateListener = function(userID, sessionID, extension) {
Helpers.trace("onUpdate. UserID:" + userID + ". SessionID: " + sessionID + ". Extension: " + JSON.stringify(extension));

var session = this.sessions[sessionID];

Helpers.trace("onUpdate. UserID:" + userID + ". SessionID: " + sessionID + ". Extension: " + JSON.stringify(extension));

if (typeof this.onUpdateCallListener === 'function'){
this.onUpdateCallListener(session, userID, extension);
}
Expand Down Expand Up @@ -280,4 +278,4 @@ function getOpponentsIdNASessions(sessions) {
}

return opponents;
}
}
Loading