Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashviewer): Video performance instrumentation #1071

Merged
merged 15 commits into from
Sep 17, 2019
14 changes: 14 additions & 0 deletions src/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,17 @@ export const USER_DOCUMENT_THUMBNAIL_EVENTS = {
};

export const MISSING_EXTERNAL_REFS = 'missing_x_refs';

export const MEDIA_METRIC = {
bufferFill: 'bufferFill',
duration: 'duration',
lagRatio: 'lagRatio',
seeked: 'seeked',
totalBufferLag: 'totalBufferLag',
watchLength: 'watchLength',
};

export const MEDIA_METRIC_EVENTS = {
bufferFill: 'media_metric_buffer_fill',
endPlayback: 'media_metric_end_playback',
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('lib/viewers/box3d/video360/Video360Viewer', () => {
options.container = containerEl;
options.location = {};
viewer = new Video360Viewer(options);
sandbox.stub(viewer, 'processMetrics');
});

afterEach(() => {
Expand Down
93 changes: 86 additions & 7 deletions src/lib/viewers/media/DashViewer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import VideoBaseViewer from './VideoBaseViewer';
import PreviewError from '../../PreviewError';
import fullscreen from '../../Fullscreen';
import Timer from '../../Timer';
import { appendQueryParams, getProp } from '../../util';
import { getRepresentation } from '../../file';
import { MEDIA_STATIC_ASSETS_VERSION } from '../../constants';
import getLanguageName from '../../lang';
import { ERROR_CODE, VIEWER_EVENT } from '../../events';
import { ERROR_CODE, VIEWER_EVENT, MEDIA_METRIC, MEDIA_METRIC_EVENTS } from '../../events';
import './Dash.scss';

const CSS_CLASS_DASH = 'bp-media-dash';
Expand All @@ -30,14 +31,15 @@ class DashViewer extends VideoBaseViewer {

this.api = options.api;
// Bind context for callbacks
this.loadeddataHandler = this.loadeddataHandler.bind(this);
this.adaptationHandler = this.adaptationHandler.bind(this);
this.shakaErrorHandler = this.shakaErrorHandler.bind(this);
this.requestFilter = this.requestFilter.bind(this);
this.handleBuffering = this.handleBuffering.bind(this);
this.getBandwidthInterval = this.getBandwidthInterval.bind(this);
this.handleAudioTrack = this.handleAudioTrack.bind(this);
this.handleQuality = this.handleQuality.bind(this);
this.handleSubtitle = this.handleSubtitle.bind(this);
this.handleAudioTrack = this.handleAudioTrack.bind(this);
this.getBandwidthInterval = this.getBandwidthInterval.bind(this);
this.loadeddataHandler = this.loadeddataHandler.bind(this);
this.requestFilter = this.requestFilter.bind(this);
this.shakaErrorHandler = this.shakaErrorHandler.bind(this);
}

/**
Expand Down Expand Up @@ -106,7 +108,8 @@ class DashViewer extends VideoBaseViewer {
load() {
this.mediaUrl = this.options.representation.content.url_template;
this.watermarkCacheBust = Date.now();
this.mediaEl.addEventListener('loadeddata', this.loadeddataHandler);

this.addEventListenersForMediaLoad();

return Promise.all([this.loadAssets(this.getJSAssets()), this.getRepStatus().getPromise()])
.then(() => {
Expand Down Expand Up @@ -161,6 +164,7 @@ class DashViewer extends VideoBaseViewer {
this.player = new shaka.Player(this.mediaEl);
this.player.addEventListener('adaptation', this.adaptationHandler);
this.player.addEventListener('error', this.shakaErrorHandler);
this.player.addEventListener('buffering', this.handleBuffering);
this.player.configure({
abr: {
enabled: false,
Expand All @@ -182,6 +186,81 @@ class DashViewer extends VideoBaseViewer {
this.player.load(this.mediaUrl, this.startTimeInSeconds).catch(this.shakaErrorHandler);
}

/**
* Handles the buffering events from shaka player
*
* @see {@link https://shaka-player-demo.appspot.com/docs/api/shaka.Player.html#.event:BufferingEvent}
* @param {Object} object - BufferingEvent object
* @param {boolean} object.buffering - Indicates whether the player is buffering or not
*/
handleBuffering({ buffering }) {
const tag = Timer.createTag(this.options.file.id, MEDIA_METRIC.totalBufferLag);
jstoffan marked this conversation as resolved.
Show resolved Hide resolved

if (buffering) {
Timer.start(tag);
} else {
Timer.stop(tag);
this.metrics[MEDIA_METRIC.totalBufferLag] += Timer.get(tag).elapsed;
Timer.reset(tag);
}
}

/**
* Processes the buffer fill metric which represents the initial buffer time before playback begins
* @override
* @emits MEDIA_METRIC_EVENTS.bufferFill
* @return {void}
*/
processBufferFillMetric() {
const tag = Timer.createTag(this.options.file.id, MEDIA_METRIC.bufferFill);
const bufferFill = Timer.get(tag).elapsed;
this.metrics[MEDIA_METRIC.bufferFill] = bufferFill;

this.emitMetric(MEDIA_METRIC_EVENTS.bufferFill, bufferFill);
}

/**
* Processes the media playback metrics
* @override
* @emits MEDIA_METRIC_EVENTS.endPlayback
* @return {void}
*/
processMetrics() {
if (!this.loaded) {
return;
}

const lagLength = this.metrics[MEDIA_METRIC.totalBufferLag];
const playLength = this.determinePlayLength();

this.metrics[MEDIA_METRIC.totalBufferLag] = lagLength;
this.metrics[MEDIA_METRIC.lagRatio] = lagLength / playLength;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
this.metrics[MEDIA_METRIC.duration] = this.mediaEl ? this.mediaEl.duration * 1000 : 0;
this.metrics[MEDIA_METRIC.watchLength] = playLength;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved

this.emitMetric(MEDIA_METRIC_EVENTS.endPlayback, { ...this.metrics });
}

/**
* Determines the play length, or how much of the media was consumed
* @return {number} - The play length in milliseconds
*/
determinePlayLength() {
if (!this.mediaEl || !this.mediaEl.played) {
return -1;
}

const playedParts = this.mediaEl.played;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
let playLength = 0;
for (let i = 0; i < playedParts.length; i += 1) {
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
const start = playedParts.start(i);
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
const end = playedParts.end(i);
playLength += end - start;
}

return playLength * 1000;
}

/**
* A networking filter to append representation URLs with tokens
* Manifest type will use an asset name. Segments will not.
Expand Down
Loading