Merge pull request #997 from jitsi/gsm-bars-2

Calculates quality based on the resolution and upload.
This commit is contained in:
hristoterezov 2016-10-13 16:26:42 -05:00 committed by GitHub
commit 762420fcc8
2 changed files with 139 additions and 28 deletions

View File

@ -1302,7 +1302,12 @@ export default {
} }
room.on(ConferenceEvents.CONNECTION_STATS, function (stats) { room.on(ConferenceEvents.CONNECTION_STATS, function (stats) {
ConnectionQuality.updateLocalStats(stats, connectionIsInterrupted); ConnectionQuality.updateLocalStats(
stats,
connectionIsInterrupted,
localVideo.videoType,
localVideo.isMuted(),
localVideo.resolution);
}); });
ConnectionQuality.addListener(CQEvents.LOCALSTATS_UPDATED, ConnectionQuality.addListener(CQEvents.LOCALSTATS_UPDATED,
@ -1312,6 +1317,10 @@ export default {
let data = { let data = {
bitrate: stats.bitrate, bitrate: stats.bitrate,
packetLoss: stats.packetLoss}; packetLoss: stats.packetLoss};
if (localVideo && localVideo.resolution) {
data.resolution = localVideo.resolution;
}
try { try {
room.broadcastEndpointMessage({ room.broadcastEndpointMessage({
type: this.commands.defaults.CONNECTION_QUALITY, type: this.commands.defaults.CONNECTION_QUALITY,
@ -1324,10 +1333,16 @@ export default {
room.on(ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, room.on(ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
(participant, payload) => { (participant, payload) => {
switch(payload.type) { switch(payload.type) {
case this.commands.defaults.CONNECTION_QUALITY: case this.commands.defaults.CONNECTION_QUALITY: {
ConnectionQuality.updateRemoteStats(participant.getId(), let remoteVideo = participant.getTracks()
payload.values); .find(tr => tr.isVideoTrack());
ConnectionQuality.updateRemoteStats(
participant.getId(),
payload.values,
remoteVideo ? remoteVideo.videoType : undefined,
remoteVideo ? remoteVideo.isMuted() : undefined);
break; break;
}
default: default:
console.warn("Unknown datachannel message", payload); console.warn("Unknown datachannel message", payload);
} }

View File

@ -1,3 +1,4 @@
/* global config */
import EventEmitter from "events"; import EventEmitter from "events";
import CQEvents from "../../service/connectionquality/CQEvents"; import CQEvents from "../../service/connectionquality/CQEvents";
@ -35,22 +36,78 @@ function calculateQuality(newVal, oldVal) {
return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10; return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10;
} }
// webrtc table describing simulcast resolutions and used bandwidth
// https://chromium.googlesource.com/external/webrtc/+/master/webrtc/media/engine/simulcast.cc#42
const _bandwidthMap = [
{ width: 1920, height: 1080, layers:3, max: 5000, min: 800 },
{ width: 1280, height: 720, layers:3, max: 2500, min: 600 },
{ width: 960, height: 540, layers:3, max: 900, min: 450 },
{ width: 640, height: 360, layers:2, max: 700, min: 150 },
{ width: 480, height: 270, layers:2, max: 450, min: 150 },
{ width: 320, height: 180, layers:1, max: 200, min: 30 }
];
/**
* We disable quality calculations based on bandwidth if simulcast is disabled,
* or enable it in case of no simulcast and we force it.
* @type {boolean}
*/
const disableQualityBasedOnBandwidth =
config.forceQualityBasedOnBandwidth ? false : config.disableSimulcast;
/**
* Calculates the quality percentage based on the input resolution height and
* the upload reported by the client. The value is based on the interval from
* _bandwidthMap.
* @param inputHeight the resolution used to open the camera.
* @param upload the upload rate reported by client.
* @returns {int} the percent of upload based on _bandwidthMap and maximum value
* of 100, as values of the map are approximate and clients can stream above
* those values. Returns undefined if no result is found.
*/
function calculateQualityUsingUpload(inputHeight, upload) {
// found resolution from _bandwidthMap which height is equal or less than
// the inputHeight
let foundResolution = _bandwidthMap.find((r) => (r.height <= inputHeight));
if (!foundResolution)
return undefined;
if (upload <= foundResolution.min)
return 0;
return Math.min(
((upload - foundResolution.min)*100)
/ (foundResolution.max - foundResolution.min),
100);
}
export default { export default {
/** /**
* Updates the local statistics * Updates the local statistics
* @param data new statistics * @param data new statistics
* @param dontUpdateLocalConnectionQuality {boolean} if true - * @param dontUpdateLocalConnectionQuality {boolean} if true -
* localConnectionQuality wont be recalculated. * localConnectionQuality wont be recalculated.
* @param videoType the local video type
* @param isMuted current state of local video, whether it is muted
* @param resolution the current resolution used by local video
*/ */
updateLocalStats: function (data, dontUpdateLocalConnectionQuality) { updateLocalStats:
stats = data; function (data, dontUpdateLocalConnectionQuality,
if(!dontUpdateLocalConnectionQuality) { videoType, isMuted, resolution) {
var newVal = 100 - stats.packetLoss.total; stats = data;
localConnectionQuality = if(!dontUpdateLocalConnectionQuality) {
calculateQuality(newVal, localConnectionQuality); let val = this._getNewQualityValue(
} stats,
eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality, localConnectionQuality,
stats); videoType,
isMuted,
resolution);
if (val !== undefined)
localConnectionQuality = val;
}
eventEmitter.emit(
CQEvents.LOCALSTATS_UPDATED, localConnectionQuality, stats);
}, },
/** /**
@ -66,25 +123,64 @@ export default {
/** /**
* Updates remote statistics * Updates remote statistics
* @param id the id associated with the statistics * @param id the id associated with the statistics
* @param data the statistics * @param data the statistics received
* @param remoteVideoType the video type of the remote video
* @param isRemoteVideoMuted whether remote video is muted
*/ */
updateRemoteStats: function (id, data) { updateRemoteStats:
if (!data || !("packetLoss" in data) || !("total" in data.packetLoss)) { function (id, data, remoteVideoType, isRemoteVideoMuted) {
eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null); if (!data ||
return; !("packetLoss" in data) ||
} !("total" in data.packetLoss)) {
// Use only the fields we need eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
data = {bitrate: data.bitrate, packetLoss: data.packetLoss}; return;
}
remoteStats[id] = data; let inputResolution = data.resolution;
// Use only the fields we need
data = {bitrate: data.bitrate, packetLoss: data.packetLoss};
var newVal = 100 - data.packetLoss.total; remoteStats[id] = data;
var oldVal = remoteConnectionQuality[id];
remoteConnectionQuality[id] = calculateQuality(newVal, oldVal || 100);
eventEmitter.emit( let val = this._getNewQualityValue(
CQEvents.REMOTESTATS_UPDATED, id, remoteConnectionQuality[id], data,
remoteStats[id]); remoteConnectionQuality[id],
remoteVideoType,
isRemoteVideoMuted,
inputResolution);
if (val !== undefined)
remoteConnectionQuality[id] = val;
eventEmitter.emit(
CQEvents.REMOTESTATS_UPDATED, id,
remoteConnectionQuality[id], remoteStats[id]);
},
/**
* Returns the new quality value based on the input parameters.
* Used to calculate remote and local values.
* @param data the data
* @param lastQualityValue the last value we calculated
* @param videoType need to check whether we are screen sharing
* @param isMuted is video muted
* @param resolution the input resolution used by the camera
* @returns {*} the newly calculated value or undefined if no result
* @private
*/
_getNewQualityValue:
function (data, lastQualityValue, videoType, isMuted, resolution) {
if (disableQualityBasedOnBandwidth
|| isMuted
|| videoType === 'desktop'
|| !resolution) {
return calculateQuality(
100 - data.packetLoss.total,
lastQualityValue || 100);
} else {
return calculateQualityUsingUpload(
resolution,
data.bitrate.upload);
}
}, },
/** /**