Skip to content

Commit

Permalink
feat(fs): return a promise from requestFullscreen and exitFullscreen …
Browse files Browse the repository at this point in the history
…when we can (#6424)
  • Loading branch information
gkatsev authored Mar 26, 2020
1 parent a8d4d82 commit da37fab
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
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 @@ -2745,6 +2750,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 @@ -2788,6 +2828,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, REMOTE} 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 @@ -652,23 +653,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

0 comments on commit da37fab

Please sign in to comment.