2022-06-03 00:23:51 +03:00
|
|
|
/* eslint-disable no-plusplus */
|
|
|
|
const URL_PLAYBACK_METRICS = `/api/metrics/playback`;
|
2022-03-17 03:34:44 +03:00
|
|
|
const METRICS_SEND_INTERVAL = 10000;
|
2023-05-12 08:17:16 +03:00
|
|
|
const MAX_VALID_LATENCY_SECONDS = 100; // Anything > this gets thrown out.
|
2022-03-17 03:34:44 +03:00
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
function getCurrentlyPlayingSegment(tech) {
|
|
|
|
const targetMedia = tech.vhs.playlists.media();
|
|
|
|
const snapshotTime = tech.currentTime();
|
|
|
|
let segment;
|
|
|
|
|
|
|
|
// Iterate trough available segments and get first within which snapshot_time is
|
|
|
|
// eslint-disable-next-line no-plusplus
|
|
|
|
for (let i = 0, l = targetMedia.segments.length; i < l; i++) {
|
|
|
|
// Note: segment.end may be undefined or is not properly set
|
|
|
|
if (snapshotTime < targetMedia.segments[i].end) {
|
|
|
|
segment = targetMedia.segments[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!segment) {
|
|
|
|
[segment] = targetMedia.segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
|
2022-03-17 03:34:44 +03:00
|
|
|
class PlaybackMetrics {
|
2022-03-30 03:33:32 +03:00
|
|
|
constructor(player, videojs) {
|
|
|
|
this.player = player;
|
|
|
|
this.supportsDetailedMetrics = false;
|
2022-03-17 03:34:44 +03:00
|
|
|
this.hasPerformedInitialVariantChange = false;
|
2022-05-24 01:15:32 +03:00
|
|
|
this.clockSkewMs = 0;
|
2022-03-17 03:34:44 +03:00
|
|
|
|
|
|
|
this.segmentDownloadTime = [];
|
|
|
|
this.bandwidthTracking = [];
|
|
|
|
this.latencyTracking = [];
|
|
|
|
this.errors = 0;
|
|
|
|
this.qualityVariantChanges = 0;
|
|
|
|
this.isBuffering = false;
|
2022-03-28 00:45:21 +03:00
|
|
|
this.bufferingDurationTimer = 0;
|
2022-03-30 03:33:32 +03:00
|
|
|
this.collectPlaybackMetricsTimer = 0;
|
|
|
|
|
|
|
|
this.videoJSReady = this.videoJSReady.bind(this);
|
|
|
|
this.handlePlaying = this.handlePlaying.bind(this);
|
|
|
|
this.handleBuffering = this.handleBuffering.bind(this);
|
|
|
|
this.handleEnded = this.handleEnded.bind(this);
|
|
|
|
this.handleError = this.handleError.bind(this);
|
2022-07-13 00:12:58 +03:00
|
|
|
this.send = this.send.bind(this);
|
2022-03-30 03:33:32 +03:00
|
|
|
this.collectPlaybackMetrics = this.collectPlaybackMetrics.bind(this);
|
|
|
|
this.handleNoLongerBuffering = this.handleNoLongerBuffering.bind(this);
|
2022-10-13 04:08:14 +03:00
|
|
|
this.sendMetricsTimer = 0;
|
2022-03-30 03:33:32 +03:00
|
|
|
|
|
|
|
this.player.on('canplaythrough', this.handleNoLongerBuffering);
|
|
|
|
this.player.on('error', this.handleError);
|
|
|
|
this.player.on('stalled', this.handleBuffering);
|
|
|
|
this.player.on('waiting', this.handleBuffering);
|
|
|
|
this.player.on('playing', this.handlePlaying);
|
|
|
|
this.player.on('ended', this.handleEnded);
|
|
|
|
|
|
|
|
// Keep a reference of the standard vjs xhr function.
|
|
|
|
const oldVjsXhrCallback = videojs.xhr;
|
|
|
|
|
|
|
|
// Override the xhr function to track segment download time.
|
2022-06-03 00:23:51 +03:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2022-03-30 03:33:32 +03:00
|
|
|
videojs.Vhs.xhr = (...args) => {
|
|
|
|
if (args[0].uri.match('.ts')) {
|
|
|
|
const start = new Date();
|
|
|
|
|
|
|
|
const cb = args[1];
|
2022-06-03 00:23:51 +03:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2022-03-30 03:33:32 +03:00
|
|
|
args[1] = (request, error, response) => {
|
|
|
|
const end = new Date();
|
|
|
|
const delta = end.getTime() - start.getTime();
|
|
|
|
this.trackSegmentDownloadTime(delta);
|
|
|
|
cb(request, error, response);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldVjsXhrCallback(...args);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.videoJSReady();
|
2022-03-17 03:34:44 +03:00
|
|
|
|
2022-10-13 04:08:14 +03:00
|
|
|
this.sendMetricsTimer = setInterval(() => {
|
2022-03-17 03:34:44 +03:00
|
|
|
this.send();
|
|
|
|
}, METRICS_SEND_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2022-10-13 04:08:14 +03:00
|
|
|
stop() {
|
|
|
|
clearInterval(this.sendMetricsTimer);
|
|
|
|
this.player.off();
|
|
|
|
}
|
|
|
|
|
2022-05-24 01:15:32 +03:00
|
|
|
// Keep our client clock in sync with the server clock to determine
|
|
|
|
// accurate latency calculations.
|
|
|
|
setClockSkew(skewMs) {
|
|
|
|
this.clockSkewMs = skewMs;
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
videoJSReady() {
|
|
|
|
const tech = this.player.tech({ IWillNotUseThisInPlugins: true });
|
|
|
|
this.supportsDetailedMetrics = !!tech;
|
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
tech.on('usage', e => {
|
2022-03-30 03:33:32 +03:00
|
|
|
if (e.name === 'vhs-unknown-waiting') {
|
|
|
|
this.setIsBuffering(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.name === 'vhs-rendition-change-abr') {
|
|
|
|
// Quality variant has changed
|
|
|
|
this.incrementQualityVariantChanges();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Variant changed
|
|
|
|
const trackElements = this.player.textTracks();
|
2022-06-03 00:23:51 +03:00
|
|
|
trackElements.addEventListener('cuechange', () => {
|
2022-03-30 03:33:32 +03:00
|
|
|
this.incrementQualityVariantChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePlaying() {
|
|
|
|
clearInterval(this.collectPlaybackMetricsTimer);
|
|
|
|
this.collectPlaybackMetricsTimer = setInterval(() => {
|
|
|
|
this.collectPlaybackMetrics();
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEnded() {
|
|
|
|
clearInterval(this.collectPlaybackMetricsTimer);
|
|
|
|
}
|
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
handleBuffering() {
|
2022-03-30 03:33:32 +03:00
|
|
|
this.incrementErrorCount(1);
|
|
|
|
this.setIsBuffering(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNoLongerBuffering() {
|
|
|
|
this.setIsBuffering(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError() {
|
|
|
|
this.incrementErrorCount(1);
|
|
|
|
}
|
|
|
|
|
2022-03-17 03:34:44 +03:00
|
|
|
incrementErrorCount(count) {
|
|
|
|
this.errors += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementQualityVariantChanges() {
|
|
|
|
// We always start the player at the lowest quality, so let's just not
|
|
|
|
// count the first change.
|
|
|
|
if (!this.hasPerformedInitialVariantChange) {
|
|
|
|
this.hasPerformedInitialVariantChange = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.qualityVariantChanges++;
|
|
|
|
}
|
|
|
|
|
2022-03-28 00:45:21 +03:00
|
|
|
setIsBuffering(isBuffering) {
|
|
|
|
this.isBuffering = isBuffering;
|
|
|
|
|
|
|
|
if (!isBuffering) {
|
|
|
|
clearTimeout(this.bufferingDurationTimer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.bufferingDurationTimer = setTimeout(() => {
|
|
|
|
this.incrementErrorCount(1);
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
2022-03-17 03:34:44 +03:00
|
|
|
trackSegmentDownloadTime(seconds) {
|
|
|
|
this.segmentDownloadTime.push(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
trackBandwidth(bps) {
|
|
|
|
this.bandwidthTracking.push(bps);
|
|
|
|
}
|
|
|
|
|
|
|
|
trackLatency(latency) {
|
|
|
|
this.latencyTracking.push(latency);
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
collectPlaybackMetrics() {
|
|
|
|
const tech = this.player.tech({ IWillNotUseThisInPlugins: true });
|
|
|
|
if (!tech || !tech.vhs) {
|
2022-03-17 03:34:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:14:49 +03:00
|
|
|
// If we're paused then do nothing.
|
|
|
|
if (this.player.paused()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-07 23:53:23 +03:00
|
|
|
// Network state 2 means we're actively using the network.
|
|
|
|
// We only care about reporting metrics with network activity stats
|
|
|
|
// if it's actively being used, so don't report otherwise.
|
|
|
|
const networkState = this.player.networkState();
|
|
|
|
if (networkState !== 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
const bandwidth = tech.vhs.systemBandwidth;
|
|
|
|
this.trackBandwidth(bandwidth);
|
2022-03-17 03:34:44 +03:00
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
try {
|
|
|
|
const segment = getCurrentlyPlayingSegment(tech);
|
|
|
|
if (!segment || !segment.dateTimeObject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const segmentTime = segment.dateTimeObject.getTime();
|
2022-05-24 01:15:32 +03:00
|
|
|
const now = new Date().getTime() + this.clockSkewMs;
|
2022-03-30 03:33:32 +03:00
|
|
|
const latency = now - segmentTime;
|
2022-04-26 00:52:33 +03:00
|
|
|
|
|
|
|
// Throw away values that seem invalid.
|
|
|
|
if (latency < 0 || latency / 1000 >= MAX_VALID_LATENCY_SECONDS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
this.trackLatency(latency);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 03:34:44 +03:00
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
async send() {
|
2022-04-08 00:56:38 +03:00
|
|
|
if (this.segmentDownloadTime.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
// If we're paused then do nothing.
|
2022-07-13 00:12:58 +03:00
|
|
|
if (!this.player || this.player.paused()) {
|
2022-06-03 00:23:51 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:33:32 +03:00
|
|
|
const errorCount = this.errors;
|
2022-03-17 03:34:44 +03:00
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
let data;
|
2022-03-30 03:33:32 +03:00
|
|
|
if (this.supportsDetailedMetrics) {
|
2022-06-03 00:23:51 +03:00
|
|
|
const average = arr => arr.reduce((p, c) => p + c, 0) / arr.length;
|
2022-03-30 03:33:32 +03:00
|
|
|
|
|
|
|
const averageDownloadDuration = average(this.segmentDownloadTime) / 1000;
|
2022-06-03 00:23:51 +03:00
|
|
|
const roundedAverageDownloadDuration = Math.round(averageDownloadDuration * 1000) / 1000;
|
2022-03-30 03:33:32 +03:00
|
|
|
|
|
|
|
const averageBandwidth = average(this.bandwidthTracking) / 1000;
|
2022-06-03 00:23:51 +03:00
|
|
|
const roundedAverageBandwidth = Math.round(averageBandwidth * 1000) / 1000;
|
2022-03-30 03:33:32 +03:00
|
|
|
|
|
|
|
const averageLatency = average(this.latencyTracking) / 1000;
|
|
|
|
const roundedAverageLatency = Math.round(averageLatency * 1000) / 1000;
|
|
|
|
|
|
|
|
data = {
|
|
|
|
bandwidth: roundedAverageBandwidth,
|
|
|
|
latency: roundedAverageLatency,
|
|
|
|
downloadDuration: roundedAverageDownloadDuration,
|
2022-10-13 04:08:14 +03:00
|
|
|
errors: errorCount + (this.isBuffering ? 1 : 0),
|
2022-03-30 03:33:32 +03:00
|
|
|
qualityVariantChanges: this.qualityVariantChanges,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
data = {
|
2022-10-13 04:08:14 +03:00
|
|
|
errors: errorCount + (this.isBuffering ? 1 : 0),
|
2022-03-30 03:33:32 +03:00
|
|
|
};
|
|
|
|
}
|
2022-03-17 03:34:44 +03:00
|
|
|
|
|
|
|
this.errors = 0;
|
|
|
|
this.qualityVariantChanges = 0;
|
|
|
|
this.segmentDownloadTime = [];
|
|
|
|
this.bandwidthTracking = [];
|
|
|
|
this.latencyTracking = [];
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-05-24 01:15:32 +03:00
|
|
|
await fetch(URL_PLAYBACK_METRICS, options);
|
2022-03-17 03:34:44 +03:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PlaybackMetrics;
|