getBitrate method
dynamic
getBitrate
(- int handleId
)
Implementation
getBitrate(int handleId) {
Plugin pluginHandle = this.pluginHandles[handleId.toString()];
if (pluginHandle == null) {
Janus.warn("Invalid handle");
return "Invalid handle";
}
if (pluginHandle.pc == null) return "Invalid PeerConnection";
// Start getting the bitrate, if getStats is supported
if (pluginHandle.pc.getStats() != null) {
if (pluginHandle.bitrate['timer'] != null) {
Janus.log("Starting bitrate timer (via getStats)");
pluginHandle.bitrate['timer'] = Timer(Duration(microseconds: 1000), () {
pluginHandle.pc.getStats().then((List<StatsReport> stats) {
stats.forEach((res) {
if (res == null) return;
bool inStats = false;
// Check if these are statistics on incoming media
if ((res.type == "video" ||
res.id.toLowerCase().indexOf("video") > -1) &&
res.type == "inbound-rtp" &&
res.id.indexOf("rtcp") < 0) {
// New stats
inStats = true;
} else if (res.type == 'ssrc') {
// Older Chromer versions
inStats = true;
}
// Parse stats now
if (inStats) {
pluginHandle.bitrate['bsnow'] = res.values['bytesReceived'];
pluginHandle.bitrate['tsnow'] = res.timestamp;
if (pluginHandle.bitrate['bsbefore'] == null ||
pluginHandle.bitrate['tsbefore'] == null) {
// Skip this round
pluginHandle.bitrate['bsbefore'] =
pluginHandle.bitrate['bsnow'];
pluginHandle.bitrate['tsbefore'] =
pluginHandle.bitrate['tsnow'];
} else {
// Calculate bitrate
var timePassed = pluginHandle.bitrate['tsnow'] -
pluginHandle.bitrate['tsbefore'];
if (Janus.webRTCAdapter['browserDetails']['browser'] ==
"safari")
timePassed = timePassed /
1000; // Apparently the timestamp is in microseconds, in Safari
var bitRate = ((pluginHandle.bitrate['bsnow'] -
pluginHandle.bitrate['bsbefore']) *
8 /
timePassed)
.round();
if (Janus.webRTCAdapter['browserDetails']['browser'] ==
"safari") bitRate = int.parse(bitRate / 1000);
pluginHandle.bitrate['value'] = bitRate + ' kbits/sec';
Janus.log(
"Estimated bitrate is " + pluginHandle.bitrate['value']);
pluginHandle.bitrate['bsbefore'] =
pluginHandle.bitrate['bsnow'];
pluginHandle.bitrate['tsbefore'] =
pluginHandle.bitrate['tsnow'];
}
}
});
});
});
return "0 kbits/sec"; // We don't have a bitrate value yet
}
return pluginHandle.bitrate['value'];
} else {
Janus.warn("Getting the video bitrate unsupported by browser");
return "Feature unsupported by browser";
}
}