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(fs): return a promise from requestFullscreen and exitFullscreen when we can #6424

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
75 changes: 75 additions & 0 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ class Player extends Component {
this.on(this.tech_, 'pause', this.handleTechPause_);
this.on(this.tech_, 'durationchange', this.handleTechDurationChange_);
this.on(this.tech_, 'fullscreenchange', this.handleTechFullscreenChange_);
this.on(this.tech_, 'fullscreenerror', this.handleTechFullscreenError_);
this.on(this.tech_, 'enterpictureinpicture', this.handleTechEnterPictureInPicture_);
this.on(this.tech_, 'leavepictureinpicture', this.handleTechLeavePictureInPicture_);
this.on(this.tech_, 'error', this.handleTechError_);
Expand Down Expand Up @@ -2047,6 +2048,10 @@ class Player extends Component {
}
}

handleTechFullscreenError_(event, err) {
this.trigger('fullscreenerror', err);
}

/**
* @private
*/
Expand Down Expand Up @@ -2732,6 +2737,41 @@ class Player extends Component {
* @fires Player#fullscreenchange
*/
requestFullscreen(fullscreenOptions) {
const PromiseClass = this.options_.Promise || window.Promise;

if (PromiseClass) {
const self = this;

return new PromiseClass((resolve, reject) => {
function offHandler() {
self.off(self.fsApi_.fullscreenerror, errorHandler);
self.off(self.fsApi_.fullscreenchange, changeHandler);
}
function changeHandler() {
offHandler();
resolve();
}
function errorHandler(e, err) {
offHandler();
reject(err);
}

self.one('fullscreenchange', changeHandler);
self.one('fullscreenerror', errorHandler);

const promise = self.requestFullscreenHelper_(fullscreenOptions);

if (promise) {
promise.then(offHandler, offHandler);
return promise;
}
});
}

return this.requestFullscreenHelper_();
}

requestFullscreenHelper_(fullscreenOptions) {
let fsOptions;

// Only pass fullscreen options to requestFullscreen in spec-compliant browsers.
Expand Down Expand Up @@ -2775,6 +2815,41 @@ class Player extends Component {
* @fires Player#fullscreenchange
*/
exitFullscreen() {
const PromiseClass = this.options_.Promise || window.Promise;

if (PromiseClass) {
const self = this;

return new PromiseClass((resolve, reject) => {
function offHandler() {
self.off(self.fsApi_.fullscreenerror, errorHandler);
self.off(self.fsApi_.fullscreenchange, changeHandler);
}
function changeHandler() {
offHandler();
resolve();
}
function errorHandler(e, err) {
offHandler();
reject(err);
}

self.one('fullscreenchange', changeHandler);
self.one('fullscreenerror', errorHandler);

const promise = self.exitFullscreenHelper_();

if (promise) {
promise.then(offHandler, offHandler);
return promise;
}
});
}

return this.exitFullscreenHelper_();
}

exitFullscreenHelper_() {
if (this.fsApi_.requestFullscreen) {
const promise = document[this.fsApi_.exitFullscreen]();

Expand Down
20 changes: 17 additions & 3 deletions src/js/tech/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {toTitleCase} from '../utils/string-cases.js';
import {NORMAL as TRACK_TYPES} from '../tracks/track-types';
import setupSourceset from './setup-sourceset';
import defineLazyProperty from '../utils/define-lazy-property.js';
import {silencePromise} from '../utils/promise';

/**
* HTML5 Media Controller - Wrapper for HTML5 Media API
Expand Down Expand Up @@ -639,23 +640,36 @@ class Html5 extends Tech {
if (video.paused && video.networkState <= video.HAVE_METADATA) {
// attempt to prime the video element for programmatic access
// this isn't necessary on the desktop but shouldn't hurt
this.el_.play();
silencePromise(this.el_.play());

// playing and pausing synchronously during the transition to fullscreen
// can get iOS ~6.1 devices into a play/pause loop
this.setTimeout(function() {
video.pause();
video.webkitEnterFullScreen();
try {
video.webkitEnterFullScreen();
} catch (e) {
this.trigger('fullscreenerror', e);
}
}, 0);
} else {
video.webkitEnterFullScreen();
try {
video.webkitEnterFullScreen();
} catch (e) {
this.trigger('fullscreenerror', e);
}
}
}

/**
* Request that the `HTML5` Tech exit fullscreen.
*/
exitFullScreen() {
if (!this.el_.webkitDisplayingFullscreen) {
this.trigger('fullscreenerror', new Error('The video is not fullscreen'));
return;
}

this.el_.webkitExitFullScreen();
}

Expand Down