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

Fix nasty race condition in AMP video #27740

Merged
merged 7 commits into from
Apr 15, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions extensions/amp-video/0.1/amp-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/

import {EMPTY_METADATA} from '../../../src/mediasession-helper';
import {
MEDIA_LOAD_FAILURE_SRC_PROPERTY,
listen,
} from '../../../src/event-helper';
import {Services} from '../../../src/services';
import {VideoEvents} from '../../../src/video-interface';
import {VisibilityState} from '../../../src/visibility-state';
Expand All @@ -35,7 +39,6 @@ import {htmlFor} from '../../../src/static-template';
import {installVideoManagerForDoc} from '../../../src/service/video-manager-impl';
import {isExperimentOn} from '../../../src/experiments';
import {isLayoutSizeDefined} from '../../../src/layout';
import {listen} from '../../../src/event-helper';
import {mutedOrUnmutedEvent} from '../../../src/iframe-video';
import {
propagateObjectFitStyles,
Expand Down Expand Up @@ -322,23 +325,39 @@ class AmpVideo extends AMP.BaseElement {
// If we are in prerender mode, only propagate cached sources and then
// when document becomes visible propagate origin sources and other children
// If not in prerender mode, propagate everything.
let pendingOriginPromise;
if (this.getAmpDoc().getVisibilityState() == VisibilityState.PRERENDER) {
if (!this.element.hasAttribute('preload')) {
this.video_.setAttribute('preload', 'auto');
}
this.getAmpDoc()
pendingOriginPromise = this.getAmpDoc()
.whenFirstVisible()
.then(() => {
this.propagateLayoutChildren_();
// We need to yield to the event queue before listing for loadPromise
// because this element may still be in error state from the pre-render
// load.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My kingdom for a way to know for sure when the media element ran its media selection process...

return Services.timerFor(this.win)
.promise(1)
.then(() => this.loadPromise(this.video_));
});
} else {
this.propagateLayoutChildren_();
}

// loadPromise for media elements listens to `loadedmetadata`.
const promise = this.loadPromise(this.video_).then(() => {
this.element.dispatchCustomEvent(VideoEvents.LOAD);
});
const promise = this.loadPromise(this.video_).then(
() => {
this.element.dispatchCustomEvent(VideoEvents.LOAD);
},
(reason) => {
if (pendingOriginPromise) {
this.video_[MEDIA_LOAD_FAILURE_SRC_PROPERTY] = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to keep this property set, in case the origin source also 404'd.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return pendingOriginPromise;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this one succeeds, we still want to dispatch this.element.dispatchCustomEvent(VideoEvents.LOAD);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Moved the success handler one further down the chain.

}
throw reason;
}
);

// Resolve layoutCallback right away if the video won't preload.
if (this.element.getAttribute('preload') === 'none') {
Expand Down