Skip to content

Commit d16dfe6

Browse files
Vladlukhanindimaspirit
authored andcommitted
Bandwidth / onCallStatsReport
1 parent 2c5f59b commit d16dfe6

File tree

8 files changed

+318
-181
lines changed

8 files changed

+318
-181
lines changed

quickblox.min.js

Lines changed: 130 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -42353,26 +42353,29 @@ RTCPeerConnection.prototype.updateRemoteSDP = function(newSDP){
4235342353
};
4235442354

4235542355
RTCPeerConnection.prototype.getRemoteSDP = function(){
42356-
return this.remoteSDP;
42356+
return this.remoteSDP;
4235742357
};
4235842358

4235942359
RTCPeerConnection.prototype.setRemoteSessionDescription = function(type, remoteSessionDescription, callback) {
42360-
var desc = new RTCSessionDescription({sdp: remoteSessionDescription, type: type});
42361-
42362-
var ffVersion = Helpers.getVersionFirefox();
42363-
42364-
if(ffVersion !== null && (ffVersion === 56 || ffVersion === 57) ) {
42365-
desc.sdp = _modifySDPforFixIssueFFAndFreezes(desc.sdp);
42366-
}
42360+
var self = this,
42361+
desc = new RTCSessionDescription({sdp: remoteSessionDescription, type: type}),
42362+
ffVersion = Helpers.getVersionFirefox();
42363+
42364+
if (ffVersion !== null && (ffVersion === 56 || ffVersion === 57) && !self.delegate.bandwidth) {
42365+
desc.sdp = _modifySDPforFixIssueFFAndFreezes(desc.sdp);
42366+
} else {
42367+
desc.sdp = setMediaBitrate(desc.sdp, 'video', self.delegate.bandwidth);
42368+
}
42369+
42370+
function successCallback(desc) {
42371+
callback(null);
42372+
}
4236742373

42368-
function successCallback() {
42369-
callback(null);
42370-
}
42371-
function errorCallback(error) {
42372-
callback(error);
42373-
}
42374+
function errorCallback(error) {
42375+
callback(error);
42376+
}
4237442377

42375-
this.setRemoteDescription(desc, successCallback, errorCallback);
42378+
self.setRemoteDescription(desc).then(successCallback, errorCallback);
4237642379
};
4237742380

4237842381
RTCPeerConnection.prototype.addLocalStream = function(localStream){
@@ -42392,20 +42395,20 @@ RTCPeerConnection.prototype.getAndSetLocalSessionDescription = function(callType
4239242395
// Additional parameters for SDP Constraints
4239342396
// http://www.w3.org/TR/webrtc/#h-offer-answer-options
4239442397

42395-
// self.createOffer(successCallback, errorCallback, constraints)
42398+
self.createOffer().then(function(offer) {
42399+
offer.sdp = setMediaBitrate(offer.sdp, 'video', self.delegate.bandwidth);
42400+
successCallback(offer);
42401+
}).catch(function(reason) {
42402+
errorCallback(reason);
42403+
});
4239642404

42397-
if (Helpers.getVersionSafari() >= 11) {
42398-
self.createOffer().then(function(offer) {
42399-
successCallback(offer);
42400-
}).catch(function(reason) {
42401-
errorCallback(reason);
42402-
});
42403-
// TODO for safari
42404-
} else {
42405-
self.createOffer(successCallback, errorCallback);
42406-
}
4240742405
} else {
42408-
self.createAnswer(successCallback, errorCallback);
42406+
self.createAnswer().then(function(answer) {
42407+
answer.sdp = setMediaBitrate(answer.sdp, 'video', self.delegate.bandwidth);
42408+
successCallback(answer);
42409+
}).catch(function(reason) {
42410+
errorCallback(reason);
42411+
});
4240942412
}
4241042413

4241142414
function successCallback(desc) {
@@ -42415,22 +42418,16 @@ RTCPeerConnection.prototype.getAndSetLocalSessionDescription = function(callType
4241542418
* callType === 2 is audio only
4241642419
*/
4241742420
var ffVersion = Helpers.getVersionFirefox();
42418-
42421+
4241942422
if (ffVersion !== null && ffVersion < 55 && callType === 2 && self.type === 'offer') {
4242042423
desc.sdp = _modifySDPforFixIssue(desc.sdp);
4242142424
}
42422-
42423-
if(Helpers.getVersionSafari() >= 11) {
42424-
self.setLocalDescription(desc).then(function() {
42425-
callback(null);
42426-
}).catch(function(error) {
42427-
errorCallback(error);
42428-
});
42429-
} else {
42430-
self.setLocalDescription(desc, function() {
42431-
callback(null);
42432-
}, errorCallback);
42433-
}
42425+
42426+
self.setLocalDescription(desc).then(function() {
42427+
callback(null);
42428+
}).catch(function(error) {
42429+
errorCallback(error);
42430+
});
4243442431
}
4243542432

4243642433
function errorCallback(error) {
@@ -42595,8 +42592,8 @@ RTCPeerConnection.prototype._clearStatsReportTimer = function(){
4259542592
RTCPeerConnection.prototype._getStatsWrap = function() {
4259642593
var self = this,
4259742594
localStream = self.getLocalStreams().length ? self.getLocalStreams()[0] : self.delegate.localStream,
42598-
selector = self.delegate.callType == 1 ? localStream.getVideoTracks()[0] : localStream.getAudioTracks()[0],
42599-
statsReportInterval;
42595+
statsReportInterval,
42596+
lastResult;
4260042597

4260142598
if (config.webrtc && config.webrtc.statsReportTimeInterval) {
4260242599
if (isNaN(+config.webrtc.statsReportTimeInterval)) {
@@ -42606,15 +42603,13 @@ RTCPeerConnection.prototype._getStatsWrap = function() {
4260642603
statsReportInterval = config.webrtc.statsReportTimeInterval * 1000;
4260742604

4260842605
var _statsReportCallback = function() {
42609-
_getStats(self, selector,
42610-
function (results) {
42611-
self.delegate._onCallStatsReport(self.userID, results, null);
42612-
},
42613-
function errorLog(err) {
42614-
Helpers.traceError('_getStats error. ' + err.name + ': ' + err.message);
42615-
self.delegate._onCallStatsReport(self.userID, null, err);
42616-
}
42617-
);
42606+
_getStats(self, lastResult, function(results, lastResults) {
42607+
lastResult = lastResults;
42608+
self.delegate._onCallStatsReport(self.userID, results, null);
42609+
}, function errorLog(err) {
42610+
Helpers.traceError('_getStats error. ' + err.name + ': ' + err.message);
42611+
self.delegate._onCallStatsReport(self.userID, null, err);
42612+
});
4261842613
};
4261942614

4262042615
Helpers.trace('Stats tracker has been started.');
@@ -42691,18 +42686,63 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
4269142686
/**
4269242687
* PRIVATE
4269342688
*/
42694-
function _getStats(peer, selector, successCallback, errorCallback) {
42695-
peer.getStats(selector, function (res) {
42696-
var items = [];
42697-
res.forEach(function (result) {
42689+
function _getStats(peer, lastResults, successCallback, errorCallback) {
42690+
var statistic = {
42691+
'inbound_audio': null,
42692+
'inbound_video': null,
42693+
'outbound_audio': null,
42694+
'outbound_video': null,
42695+
'total_transport': null,
42696+
};
42697+
42698+
peer.getStats(null).then(function(results) {
42699+
results.forEach(function(result) {
4269842700
var item = {};
42699-
item.id = result.id;
42700-
item.type = result.type;
42701-
item.timestamp = result.timestamp;
42702-
items.push(item);
42701+
if (result.bytesSent && result.type === 'outbound-rtp') {
42702+
item.bitrate = _getBitratePerSecond(result, lastResults, true);
42703+
item.type = result.type;
42704+
item.mediaType = result.mediaType;
42705+
item.bytesSent = result.bytesSent;
42706+
item.packetsSent = result.packetsSent;
42707+
item.timestamp = result.timestamp;
42708+
statistic['outbound_' + result.mediaType] = item;
42709+
}
42710+
if (result.bytesReceived && result.type === 'inbound-rtp') {
42711+
item.bitrate = _getBitratePerSecond(result, lastResults, false);
42712+
item.type = result.type;
42713+
item.mediaType = result.mediaType;
42714+
item.bytesReceived = result.bytesReceived;
42715+
item.packetsReceived = result.packetsReceived;
42716+
item.timestamp = result.timestamp;
42717+
statistic['inbound_' + result.mediaType] = item;
42718+
}
42719+
if (result.bytesSent && result.bytesReceived && result.type === 'transport') {
42720+
item.bitrate_out = _getBitratePerSecond(result, lastResults, true);
42721+
item.bitrate_in = _getBitratePerSecond(result, lastResults, false);
42722+
item.type = result.type;
42723+
item.bytesSent = result.bytesSent;
42724+
item.bytesReceived = result.bytesReceived;
42725+
item.timestamp = result.timestamp;
42726+
statistic['total_' + result.type] = item;
42727+
}
4270342728
});
42704-
successCallback(items);
42729+
successCallback(statistic, results);
4270542730
}, errorCallback);
42731+
42732+
function _getBitratePerSecond(result, lastResults, isSent) {
42733+
var lastResult = lastResults && lastResults.get(result.id),
42734+
bit = 8,
42735+
bitrate;
42736+
42737+
if (!lastResult) {
42738+
bitrate = 0;
42739+
} else if (isSent) {
42740+
bitrate = bit*(result.bytesSent-lastResult.bytesSent)/(result.timestamp-lastResult.timestamp);
42741+
} else {
42742+
bitrate = bit*(result.bytesReceived-lastResult.bytesReceived)/(result.timestamp-lastResult.timestamp);
42743+
}
42744+
return Math.round(bitrate);
42745+
}
4270642746
}
4270742747

4270842748
/**
@@ -42730,8 +42770,9 @@ function _modifySDPforFixIssueFFAndFreezes(sdp) {
4273042770
}
4273142771

4273242772
function setMediaBitrate(sdp, media, bitrate) {
42733-
var lines = sdp.split("\n");
42773+
var lines = sdp.split('\n');
4273442774
var line = -1;
42775+
var modifier = 'AS';
4273542776

4273642777
for (var i = 0; i < lines.length; i++) {
4273742778
if (lines[i].indexOf("m="+media) === 0) {
@@ -42741,32 +42782,26 @@ function setMediaBitrate(sdp, media, bitrate) {
4274142782
}
4274242783

4274342784
if (line === -1) {
42744-
console.debug("Could not find the m line for", media);
4274542785
return sdp;
4274642786
}
42747-
console.debug("Found the m line for", media, "at line", line);
4274842787

42749-
// Pass the m line
4275042788
line++;
4275142789

42752-
// Skip i and c lines
42753-
while(lines[line].indexOf("i=") === 0 || lines[line].indexOf("c=") === 0) {
42790+
while(lines[line].indexOf('i=') === 0 || lines[line].indexOf('c=') === 0) {
4275442791
line++;
4275542792
}
4275642793

42757-
// If we're on a b line, replace it
42758-
if (lines[line].indexOf("b") === 0) {
42759-
console.debug("Replaced b line at line", line);
42760-
lines[line] = "b=AS:"+bitrate;
42761-
return lines.join("\n");
42794+
if (lines[line].indexOf('b') === 0) {
42795+
lines[line] = 'b='+modifier+':'+bitrate;
42796+
return lines.join('\n');
4276242797
}
4276342798

42764-
// Add a new b line
42765-
console.debug("Adding new b line before line", line);
4276642799
var newLines = lines.slice(0, line);
42767-
newLines.push("b=AS:"+bitrate);
42800+
42801+
newLines.push('b='+modifier+':'+bitrate);
4276842802
newLines = newLines.concat(lines.slice(line, lines.length));
42769-
return newLines.join("\n");
42803+
42804+
return newLines.join('\n');
4277042805
}
4277142806

4277242807
module.exports = RTCPeerConnection;
@@ -42858,13 +42893,15 @@ WebRTCClient.prototype.sessions = {};
4285842893

4285942894
/**
4286042895
* Creates the new session.
42861-
* @param {array} opponentsIDs Opponents IDs
42862-
* @param {number} ct Call type
42863-
* @param {number} cID Initiator ID
42896+
* @param {array} opponentsIDs - Opponents IDs
42897+
* @param {number} ct - Call type
42898+
* @param {number} [cID=yourUserId] - Initiator ID
42899+
* @param {number} [bw=0] - Bandwidth limit (kbps)
4286442900
*/
42865-
WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID) {
42901+
WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID, bw) {
4286642902
var opponentsIdNASessions = getOpponentsIdNASessions(this.sessions),
4286742903
callerID = cID || Helpers.getIdFromNode(this.connection.jid),
42904+
bandwidth = isNaN(bw) ? 0 : +bw,
4286842905
isIdentifyOpponents = false,
4286942906
callType = ct || 2;
4287042907

@@ -42875,14 +42912,14 @@ WebRTCClient.prototype.createNewSession = function(opponentsIDs, ct, cID) {
4287542912
isIdentifyOpponents = isOpponentsEqual(opponentsIdNASessions, opponentsIDs);
4287642913

4287742914
if (!isIdentifyOpponents) {
42878-
return this._createAndStoreSession(null, callerID, opponentsIDs, callType);
42915+
return this._createAndStoreSession(null, callerID, opponentsIDs, callType, bandwidth);
4287942916
} else {
4288042917
throw new Error('Can\'t create a session with the same opponentsIDs. There is a session already in NEW or ACTIVE state.');
4288142918
}
4288242919
};
4288342920

42884-
WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, opponentsIDs, callType) {
42885-
var newSession = new WebRTCSession(sessionID, callerID, opponentsIDs, callType, this.signalingProvider, Helpers.getIdFromNode(this.connection.jid));
42921+
WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, opponentsIDs, callType, bandwidth) {
42922+
var newSession = new WebRTCSession(sessionID, callerID, opponentsIDs, callType, this.signalingProvider, Helpers.getIdFromNode(this.connection.jid), bandwidth);
4288642923

4288742924
/** set callbacks */
4288842925
newSession.onUserNotAnswerListener = this.onUserNotAnswerListener;
@@ -42952,7 +42989,7 @@ WebRTCClient.prototype._onCallListener = function(userID, sessionID, extension)
4295242989
var session = this.sessions[sessionID];
4295342990

4295442991
if (!session) {
42955-
session = this._createAndStoreSession(sessionID, extension.callerID, extension.opponentsIDs, extension.callType);
42992+
session = this._createAndStoreSession(sessionID, extension.callerID, extension.opponentsIDs, extension.callType, +extension.userInfo.bandwidth);
4295642993

4295742994
if (typeof this.onCallListener === 'function') {
4295842995
Utils.safeCallbackCall(this.onCallListener, session, userInfo);
@@ -43256,7 +43293,7 @@ WebRTCSession.State = {
4325643293
* @param {array} An array with opponents
4325743294
* @param {enum} Type of a call
4325843295
*/
43259-
function WebRTCSession(sessionID, initiatorID, opIDs, callType, signalingProvider, currentUserID) {
43296+
function WebRTCSession(sessionID, initiatorID, opIDs, callType, signalingProvider, currentUserID, bandwidth) {
4326043297
this.ID = sessionID ? sessionID : generateUUID();
4326143298
this.state = WebRTCSession.State.NEW;
4326243299

@@ -43272,6 +43309,8 @@ function WebRTCSession(sessionID, initiatorID, opIDs, callType, signalingProvide
4327243309

4327343310
this.currentUserID = currentUserID;
4327443311

43312+
this.bandwidth = bandwidth;
43313+
4327543314
/**
4327643315
* We use this timeout to fix next issue:
4327743316
* "From Android/iOS make a call to Web and kill the Android/iOS app instantly. Web accept/reject popup will be still visible.
@@ -43786,6 +43825,10 @@ WebRTCSession.prototype.processCall = function(peerConnection, ext) {
4378643825
extension.opponentsIDs = this.opponentsIDs;
4378743826
extension.sdp = peerConnection.localDescription.sdp;
4378843827

43828+
//TODO: set bandwidth to the userInfo object
43829+
extension.userInfo = ext.userInfo || {};
43830+
extension.userInfo.bandwidth = this.bandwidth;
43831+
4378943832
this.signalingProvider.sendMessage(peerConnection.userID, extension, SignalingConstants.SignalingType.CALL);
4379043833
};
4379143834

@@ -43838,10 +43881,8 @@ WebRTCSession.prototype._onCallStatsReport = function(userId, stats, error) {
4383843881
};
4383943882

4384043883
WebRTCSession.prototype._onSessionConnectionStateChangedListener = function(userID, connectionState) {
43841-
var self = this;
43842-
43843-
if (typeof self.onSessionConnectionStateChangedListener === 'function') {
43844-
Utils.safeCallbackCall(self.onSessionConnectionStateChangedListener, self, userID, connectionState);
43884+
if (typeof this.onSessionConnectionStateChangedListener === 'function') {
43885+
Utils.safeCallbackCall(this.onSessionConnectionStateChangedListener, this, userID, connectionState);
4384543886
}
4384643887
};
4384743888

@@ -44617,7 +44658,7 @@ module.exports = StreamManagement;
4461744658

4461844659
var config = {
4461944660
version: '2.8.1',
44620-
buildNumber: '1067',
44661+
buildNumber: '1068',
4462144662
creds: {
4462244663
appId: '',
4462344664
authKey: '',

0 commit comments

Comments
 (0)