Skip to content

Commit

Permalink
Story media performance metrics. (#23962)
Browse files Browse the repository at this point in the history
* Squash media performance metrics service commits.

* Reviews.
  • Loading branch information
gmajoulet authored Aug 20, 2019
1 parent df85b7d commit 1aa3fef
Show file tree
Hide file tree
Showing 10 changed files with 928 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from './story-mock';
import {Services} from '../../../../src/services';
import {macroTask} from '../../../../testing/yield';
import {registerServiceBuilder} from '../../../../src/service';

const NOOP = () => {};

Expand All @@ -57,6 +58,9 @@ describes.realWin(
doc = win.document;
const viewer = Services.viewerForDoc(env.ampdoc);
sandbox.stub(Services, 'viewerForDoc').returns(viewer);
registerServiceBuilder(win, 'performance', () => ({
isPerformanceTrackingOn: () => false,
}));
adElement = win.document.createElement('amp-story-auto-ads');
storyElement = win.document.createElement('amp-story');
win.document.body.appendChild(storyElement);
Expand Down
46 changes: 46 additions & 0 deletions extensions/amp-story/1.0/amp-story-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {getAmpdoc} from '../../../src/service';
import {getData, listen} from '../../../src/event-helper';
import {getFriendlyIframeEmbedOptional} from '../../../src/iframe-helper';
import {getLogEntries} from './logging';
import {getMediaPerformanceMetricsService} from './media-performance-metrics-service';
import {getMode} from '../../../src/mode';
import {htmlFor} from '../../../src/static-template';
import {isExperimentOn} from '../../../src/experiments';
Expand Down Expand Up @@ -246,6 +247,14 @@ export class AmpStoryPage extends AMP.BaseElement {

const deferred = new Deferred();

/** @private @const {!./media-performance-metrics-service.MediaPerformanceMetricsService} */
this.mediaPerformanceMetricsService_ = getMediaPerformanceMetricsService(
this.win
);

/** @private {!Array<!HTMLMediaElement>} */
this.performanceTrackedVideos_ = [];

/** @private @const {!Promise<!MediaPool>} */
this.mediaPoolPromise_ = deferred.promise;

Expand Down Expand Up @@ -422,6 +431,7 @@ export class AmpStoryPage extends AMP.BaseElement {
pauseCallback() {
this.advancement_.stop();

this.stopMeasuringVideoPerformance_();
this.stopListeningToVideoEvents_();
this.toggleErrorMessage_(false);
this.togglePlayMessage_(false);
Expand Down Expand Up @@ -455,6 +465,7 @@ export class AmpStoryPage extends AMP.BaseElement {
this.checkPageHasAudio_();
this.renderOpenAttachmentUI_();
this.findAndPrepareEmbeddedComponents_();
this.startMeasuringVideoPerformance_();
this.preloadAllMedia_()
.then(() => this.startListeningToVideoEvents_())
.then(() => this.playAllMedia_());
Expand Down Expand Up @@ -1258,6 +1269,41 @@ export class AmpStoryPage extends AMP.BaseElement {
});
}

/**
* Starts measuring video performance metrics, if performance tracking is on.
* Has to be called directly before playing the video.
* @private
*/
startMeasuringVideoPerformance_() {
if (!this.mediaPerformanceMetricsService_.isPerformanceTrackingOn()) {
return;
}

this.performanceTrackedVideos_ = /** @type {!Array<!HTMLMediaElement>} */ (this.getAllVideos_());
for (let i = 0; i < this.performanceTrackedVideos_.length; i++) {
this.mediaPerformanceMetricsService_.startMeasuring(
this.performanceTrackedVideos_[i]
);
}
}

/**
* Stops measuring video performance metrics, if performance tracking is on.
* Computes and sends the metrics.
* @private
*/
stopMeasuringVideoPerformance_() {
if (!this.mediaPerformanceMetricsService_.isPerformanceTrackingOn()) {
return;
}

for (let i = 0; i < this.performanceTrackedVideos_.length; i++) {
this.mediaPerformanceMetricsService_.stopMeasuring(
this.performanceTrackedVideos_[i]
);
}
}

/**
* Displays a loading spinner whenever the video is buffering.
* Has to be called after the mediaPool preload method, that swaps the video
Expand Down
Loading

0 comments on commit 1aa3fef

Please sign in to comment.