Skip to content
Merged
86 changes: 83 additions & 3 deletions js/modules/webrtc/qbRTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
/** We use this timer interval to dial a user - produce the call requests each N seconds. */
this.dialingTimer = null;
this.answerTimeInterval = 0;
this.statsReportTimer = null;

/** timer to detect network blips */
this.reconnectTimer = 0;
Expand All @@ -54,6 +55,7 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {

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

if(this.signalingState !== 'closed'){
this.close();
Expand Down Expand Up @@ -174,16 +176,19 @@ RTCPeerConnection.prototype.onIceCandidateCallback = function(event) {

/** handler of remote media stream */
RTCPeerConnection.prototype.onAddRemoteStreamCallback = function(event) {
var self = this;

if (typeof this.delegate._onRemoteStreamListener === 'function'){
this.delegate._onRemoteStreamListener(this.userID, event.stream);
}

self._getStatsWrap();
};

RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
var newIceConnectionState = this.iceConnectionState;

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

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

/**
* read more about all states:
Expand Down Expand Up @@ -226,6 +231,45 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
/**
* PRIVATE
*/
RTCPeerConnection.prototype._clearStatsReportTimer = function(){
if(this.statsReportTimer){
clearInterval(this.statsReportTimer);
this.statsReportTimer = null;
}
};

RTCPeerConnection.prototype._getStatsWrap = function() {
var self = this,
selector = self.delegate.callType == 1 ? self.getLocalStreams()[0].getVideoTracks()[0] : self.getLocalStreams()[0].getAudioTracks()[0],
statsReportInterval;

if (!config.webrtc && !config.webrtc.statsReportTimeInterval) {
return;
}

if (isNaN(+config.webrtc.statsReportTimeInterval)) {
Helpers.traceError('statsReportTimeInterval (' + config.webrtc.statsReportTimeInterval + ') must be integer.');
return;
}

statsReportInterval = config.webrtc.statsReportTimeInterval * 1000;

var _statsReportCallback = function() {
_getStats(self, selector,
function (results) {
self.delegate._onCallStatsReport(self.userID, results);
},
function errorLog(err) {
Helpers.traceError("_getStats error. " + err.name + ": " + err.message);
}
);
};

Helpers.trace('Stats tracker has been started.');

self.statsReportTimer = setInterval(_statsReportCallback, statsReportInterval);
};

RTCPeerConnection.prototype._clearWaitingReconnectTimer = function() {
if(this.waitingReconnectTimeoutCallback){
Helpers.trace('_clearWaitingReconnectTimer');
Expand Down Expand Up @@ -292,4 +336,40 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
_dialingCallback(extension, withOnNotAnswerCallback, true);
};

module.exports = RTCPeerConnection;
/**
* PRIVATE
*/
function _getStats(peer, selector, successCallback, errorCallback) {
/**
* http://stackoverflow.com/questions/25069419/webrtc-getstat-api-set-up
*/
if (navigator.mozGetUserMedia) {
peer.getStats(selector,
function (res) {
var items = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем мы перегоням массив res в массив items?
почему нельзя просто сделать cb(res); ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.forEach(function (result) {
items.push(result);
});
successCallback(items);
},
errorCallback
);
} else {
peer.getStats(function (res) {
var items = [];
res.result().forEach(function (result) {
var item = {};
result.names().forEach(function (name) {
item[name] = result.stat(name);
});
item.id = result.id;
item.type = result.type;
item.timestamp = result.timestamp;
items.push(item);
});
successCallback(items);
});
}
}

module.exports = RTCPeerConnection;
1 change: 1 addition & 0 deletions js/modules/webrtc/qbWebRTCClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, op
newSession.onRemoteStreamListener = this.onRemoteStreamListener;
newSession.onSessionConnectionStateChangedListener = this.onSessionConnectionStateChangedListener;
newSession.onSessionCloseListener = this.onSessionCloseListener;
newSession.onCallStatsReport = this.onCallStatsReport;

this.sessions[newSession.ID] = newSession;
return newSession;
Expand Down
25 changes: 25 additions & 0 deletions js/modules/webrtc/qbWebRTCSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* - onRemoteStreamListener(session, userID, stream)
* - onSessionConnectionStateChangedListener(session, userID, connectionState)
* - onSessionCloseListener(session)
* - onCallStatsReport(session, userId, stats)
*/

var config = require('../../qbConfig');
Expand Down Expand Up @@ -360,6 +361,24 @@ WebRTCSession.prototype.stop = function(extension) {
self._close();
};

/**
* [function close connection with user]
* @param {[type]} userId [id of user]
*/
WebRTCSession.prototype.closeConnection = function(userId) {
var self = this,
peer = this.peerConnections[userId];

if(peer) {
peer.release();

self._closeSessionIfAllConnectionsClosed();
} else {
Helpers.traceWarn('Not found connection with user (' + userId + ')');
}
};


/**
* Update a call
* @param {array} A map with custom parameters
Expand Down Expand Up @@ -594,6 +613,12 @@ WebRTCSession.prototype._onRemoteStreamListener = function(userID, stream) {
}
};

WebRTCSession.prototype._onCallStatsReport = function(userId, stats) {
if (typeof this.onCallStatsReport === 'function'){
Utils.safeCallbackCall(this.onCallStatsReport, this, userId, stats);
}
};

WebRTCSession.prototype._onSessionConnectionStateChangedListener = function(userID, connectionState) {
var self = this;

Expand Down
5 changes: 5 additions & 0 deletions js/qbConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*
* Configuration Module
*
* NOTE:
* - config.webrtc.statsReportTimeInterval [integer, sec]:
* could add listener onCallStatsReport(session, userId, bytesReceived) if
* want to get stats (bytesReceived) about peer every X sec;
*/

var config = {
Expand All @@ -26,6 +30,7 @@ var config = {
answerTimeInterval: 60,
dialingTimeInterval: 5,
disconnectTimeInterval: 30,
statsReportTimeInterval: 10,
iceServers: [
{
'url': 'stun:stun.l.google.com:19302'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"crypto-js": "3.1.2-2",
"jquery": "^2.2.0",
"request": "^2.48.0",
"strophe": "^1.2.3",
"strophe": "~1.2.4",
"xml2js": "^0.4.13"
},
"devDependencies": {
Expand Down
20 changes: 10 additions & 10 deletions quickblox.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion samples/chat/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ <h5 class="col-md-12 col-sm-12 col-xs-12" id="all_occupants"></h5>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-timeago/1.4.1/jquery.timeago.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js" type="text/javascript"></script>

<script src="../../quickblox.min.js"></script>
<script src="../../quickblox.js"></script>
<script src="js/config.js"></script>
<script src="js/connection.js"></script>
<script src="js/messages.js"></script>
Expand Down
7 changes: 4 additions & 3 deletions samples/webrtc/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Possible options:
* https://examples.com?users=prod
* https://examples.com?users=dev
* https://examples.com?users=dev
* https://examples.com - for qa by default
*/
var usersQuery = _getQueryVar('users');
Expand All @@ -15,7 +15,8 @@
webrtc: {
answerTimeInterval: 30,
dialingTimeInterval: 5,
disconnectTimeInterval: 30
disconnectTimeInterval: 30,
statsReportTimeInterval: 5
}
};

Expand Down Expand Up @@ -231,4 +232,4 @@
window.CONFIG = CONFIG;
window.QBUsers = QBUsers;
window.MESSAGES = MESSAGES;
}(window));
}(window));
Loading