forgeplus-react/node_modules/react-player/lib/players/FilePlayer.js

529 lines
21 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.FilePlayer = void 0;
var _react = _interopRequireWildcard(require("react"));
var _utils = require("../utils");
var _singlePlayer = _interopRequireDefault(require("../singlePlayer"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var IOS = typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
var AUDIO_EXTENSIONS = /\.(m4a|mp4a|mpga|mp2|mp2a|mp3|m2a|m3a|wav|weba|aac|oga|spx)($|\?)/i;
var VIDEO_EXTENSIONS = /\.(mp4|og[gv]|webm|mov|m4v)($|\?)/i;
var HLS_EXTENSIONS = /\.(m3u8)($|\?)/i;
var HLS_SDK_URL = 'https://cdn.jsdelivr.net/npm/hls.js@VERSION/dist/hls.min.js';
var HLS_GLOBAL = 'Hls';
var DASH_EXTENSIONS = /\.(mpd)($|\?)/i;
var DASH_SDK_URL = 'https://cdnjs.cloudflare.com/ajax/libs/dashjs/VERSION/dash.all.min.js';
var DASH_GLOBAL = 'dashjs';
var MATCH_DROPBOX_URL = /www\.dropbox\.com\/.+/;
function canPlay(url) {
if (url instanceof Array) {
var _iterator = _createForOfIteratorHelper(url),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var item = _step.value;
if (typeof item === 'string' && canPlay(item)) {
return true;
}
if (canPlay(item.src)) {
return true;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return false;
}
if ((0, _utils.isMediaStream)(url)) {
return true;
}
return AUDIO_EXTENSIONS.test(url) || VIDEO_EXTENSIONS.test(url) || HLS_EXTENSIONS.test(url) || DASH_EXTENSIONS.test(url);
}
function supportsWebKitPresentationMode(video) {
if (!video) video = document.createElement('video'); // Check if Safari supports PiP, and is not on mobile (other than iPad)
// iPhone safari appears to "support" PiP through the check, however PiP does not function
return video.webkitSupportsPresentationMode && typeof video.webkitSetPresentationMode === 'function' && !/iPhone|iPod/.test(navigator.userAgent);
}
function canEnablePIP(url) {
return canPlay(url) && (!!document.pictureInPictureEnabled || supportsWebKitPresentationMode()) && !AUDIO_EXTENSIONS.test(url);
}
var FilePlayer = /*#__PURE__*/function (_Component) {
_inherits(FilePlayer, _Component);
var _super = _createSuper(FilePlayer);
function FilePlayer() {
var _this;
_classCallCheck(this, FilePlayer);
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(_args));
_defineProperty(_assertThisInitialized(_this), "onReady", function () {
var _this$props;
return (_this$props = _this.props).onReady.apply(_this$props, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onPlay", function () {
var _this$props2;
return (_this$props2 = _this.props).onPlay.apply(_this$props2, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onBuffer", function () {
var _this$props3;
return (_this$props3 = _this.props).onBuffer.apply(_this$props3, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onBufferEnd", function () {
var _this$props4;
return (_this$props4 = _this.props).onBufferEnd.apply(_this$props4, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onPause", function () {
var _this$props5;
return (_this$props5 = _this.props).onPause.apply(_this$props5, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onEnded", function () {
var _this$props6;
return (_this$props6 = _this.props).onEnded.apply(_this$props6, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onError", function () {
var _this$props7;
return (_this$props7 = _this.props).onError.apply(_this$props7, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onEnablePIP", function () {
var _this$props8;
return (_this$props8 = _this.props).onEnablePIP.apply(_this$props8, arguments);
});
_defineProperty(_assertThisInitialized(_this), "onDisablePIP", function (e) {
var _this$props9 = _this.props,
onDisablePIP = _this$props9.onDisablePIP,
playing = _this$props9.playing;
onDisablePIP(e);
if (playing) {
_this.play();
}
});
_defineProperty(_assertThisInitialized(_this), "onPresentationModeChange", function (e) {
if (_this.player && supportsWebKitPresentationMode(_this.player)) {
var webkitPresentationMode = _this.player.webkitPresentationMode;
if (webkitPresentationMode === 'picture-in-picture') {
_this.onEnablePIP(e);
} else if (webkitPresentationMode === 'inline') {
_this.onDisablePIP(e);
}
}
});
_defineProperty(_assertThisInitialized(_this), "onSeek", function (e) {
_this.props.onSeek(e.target.currentTime);
});
_defineProperty(_assertThisInitialized(_this), "mute", function () {
_this.player.muted = true;
});
_defineProperty(_assertThisInitialized(_this), "unmute", function () {
_this.player.muted = false;
});
_defineProperty(_assertThisInitialized(_this), "renderSourceElement", function (source, index) {
if (typeof source === 'string') {
return /*#__PURE__*/_react["default"].createElement("source", {
key: index,
src: source
});
}
return /*#__PURE__*/_react["default"].createElement("source", _extends({
key: index
}, source));
});
_defineProperty(_assertThisInitialized(_this), "renderTrack", function (track, index) {
return /*#__PURE__*/_react["default"].createElement("track", _extends({
key: index
}, track));
});
_defineProperty(_assertThisInitialized(_this), "ref", function (player) {
if (_this.player) {
// Store previous player to be used by removeListeners()
_this.prevPlayer = _this.player;
}
_this.player = player;
});
return _this;
}
_createClass(FilePlayer, [{
key: "componentDidMount",
value: function componentDidMount() {
this.addListeners(this.player);
if (IOS) {
this.player.load();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.shouldUseAudio(this.props) !== this.shouldUseAudio(prevProps)) {
this.removeListeners(this.prevPlayer);
this.addListeners(this.player);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.removeListeners(this.player);
if (this.hls) {
this.hls.destroy();
}
}
}, {
key: "addListeners",
value: function addListeners(player) {
var playsinline = this.props.playsinline;
player.addEventListener('canplay', this.onReady);
player.addEventListener('play', this.onPlay);
player.addEventListener('waiting', this.onBuffer);
player.addEventListener('playing', this.onBufferEnd);
player.addEventListener('pause', this.onPause);
player.addEventListener('seeked', this.onSeek);
player.addEventListener('ended', this.onEnded);
player.addEventListener('error', this.onError);
player.addEventListener('enterpictureinpicture', this.onEnablePIP);
player.addEventListener('leavepictureinpicture', this.onDisablePIP);
player.addEventListener('webkitpresentationmodechanged', this.onPresentationModeChange);
if (playsinline) {
player.setAttribute('playsinline', '');
player.setAttribute('webkit-playsinline', '');
player.setAttribute('x5-playsinline', '');
}
}
}, {
key: "removeListeners",
value: function removeListeners(player) {
player.removeEventListener('canplay', this.onReady);
player.removeEventListener('play', this.onPlay);
player.removeEventListener('waiting', this.onBuffer);
player.removeEventListener('playing', this.onBufferEnd);
player.removeEventListener('pause', this.onPause);
player.removeEventListener('seeked', this.onSeek);
player.removeEventListener('ended', this.onEnded);
player.removeEventListener('error', this.onError);
player.removeEventListener('enterpictureinpicture', this.onEnablePIP);
player.removeEventListener('leavepictureinpicture', this.onDisablePIP);
player.removeEventListener('webkitpresentationmodechanged', this.onPresentationModeChange);
} // Proxy methods to prevent listener leaks
}, {
key: "shouldUseAudio",
value: function shouldUseAudio(props) {
if (props.config.file.forceVideo) {
return false;
}
if (props.config.file.attributes.poster) {
return false; // Use <video> so that poster is shown
}
return AUDIO_EXTENSIONS.test(props.url) || props.config.file.forceAudio;
}
}, {
key: "shouldUseHLS",
value: function shouldUseHLS(url) {
return HLS_EXTENSIONS.test(url) && !IOS || this.props.config.file.forceHLS;
}
}, {
key: "shouldUseDASH",
value: function shouldUseDASH(url) {
return DASH_EXTENSIONS.test(url) || this.props.config.file.forceDASH;
}
}, {
key: "load",
value: function load(url) {
var _this2 = this;
var _this$props$config$fi = this.props.config.file,
hlsVersion = _this$props$config$fi.hlsVersion,
dashVersion = _this$props$config$fi.dashVersion;
if (this.shouldUseHLS(url)) {
(0, _utils.getSDK)(HLS_SDK_URL.replace('VERSION', hlsVersion), HLS_GLOBAL).then(function (Hls) {
_this2.hls = new Hls(_this2.props.config.file.hlsOptions);
_this2.hls.on(Hls.Events.ERROR, function (e, data) {
_this2.props.onError(e, data, _this2.hls, Hls);
});
_this2.hls.loadSource(url);
_this2.hls.attachMedia(_this2.player);
});
}
if (this.shouldUseDASH(url)) {
(0, _utils.getSDK)(DASH_SDK_URL.replace('VERSION', dashVersion), DASH_GLOBAL).then(function (dashjs) {
_this2.dash = dashjs.MediaPlayer().create();
_this2.dash.initialize(_this2.player, url, _this2.props.playing);
_this2.dash.on('error', _this2.props.onError);
_this2.dash.getDebug().setLogToBrowserConsole(false);
});
}
if (url instanceof Array) {
// When setting new urls (<source>) on an already loaded video,
// HTMLMediaElement.load() is needed to reset the media element
// and restart the media resource. Just replacing children source
// dom nodes is not enough
this.player.load();
} else if ((0, _utils.isMediaStream)(url)) {
try {
this.player.srcObject = url;
} catch (e) {
this.player.src = window.URL.createObjectURL(url);
}
}
}
}, {
key: "play",
value: function play() {
var promise = this.player.play();
if (promise) {
promise["catch"](this.props.onError);
}
}
}, {
key: "pause",
value: function pause() {
this.player.pause();
}
}, {
key: "stop",
value: function stop() {
this.player.removeAttribute('src');
if (this.dash) {
this.dash.reset();
}
}
}, {
key: "seekTo",
value: function seekTo(seconds) {
this.player.currentTime = seconds;
}
}, {
key: "setVolume",
value: function setVolume(fraction) {
this.player.volume = fraction;
}
}, {
key: "enablePIP",
value: function enablePIP() {
if (this.player.requestPictureInPicture && document.pictureInPictureElement !== this.player) {
this.player.requestPictureInPicture();
} else if (supportsWebKitPresentationMode(this.player) && this.player.webkitPresentationMode !== 'picture-in-picture') {
this.player.webkitSetPresentationMode('picture-in-picture');
}
}
}, {
key: "disablePIP",
value: function disablePIP() {
if (document.exitPictureInPicture && document.pictureInPictureElement === this.player) {
document.exitPictureInPicture();
} else if (supportsWebKitPresentationMode(this.player) && this.player.webkitPresentationMode !== 'inline') {
this.player.webkitSetPresentationMode('inline');
}
}
}, {
key: "setPlaybackRate",
value: function setPlaybackRate(rate) {
this.player.playbackRate = rate;
}
}, {
key: "getDuration",
value: function getDuration() {
if (!this.player) return null;
var _this$player = this.player,
duration = _this$player.duration,
seekable = _this$player.seekable; // on iOS, live streams return Infinity for the duration
// so instead we use the end of the seekable timerange
if (duration === Infinity && seekable.length > 0) {
return seekable.end(seekable.length - 1);
}
return duration;
}
}, {
key: "getCurrentTime",
value: function getCurrentTime() {
if (!this.player) return null;
return this.player.currentTime;
}
}, {
key: "getSecondsLoaded",
value: function getSecondsLoaded() {
if (!this.player) return null;
var buffered = this.player.buffered;
if (buffered.length === 0) {
return 0;
}
var end = buffered.end(buffered.length - 1);
var duration = this.getDuration();
if (end > duration) {
return duration;
}
return end;
}
}, {
key: "getSource",
value: function getSource(url) {
var useHLS = this.shouldUseHLS(url);
var useDASH = this.shouldUseDASH(url);
if (url instanceof Array || (0, _utils.isMediaStream)(url) || useHLS || useDASH) {
return undefined;
}
if (MATCH_DROPBOX_URL.test(url)) {
return url.replace('www.dropbox.com', 'dl.dropboxusercontent.com');
}
return url;
}
}, {
key: "render",
value: function render() {
var _this$props10 = this.props,
url = _this$props10.url,
playing = _this$props10.playing,
loop = _this$props10.loop,
controls = _this$props10.controls,
muted = _this$props10.muted,
config = _this$props10.config,
width = _this$props10.width,
height = _this$props10.height;
var useAudio = this.shouldUseAudio(this.props);
var Element = useAudio ? 'audio' : 'video';
var style = {
width: width === 'auto' ? width : '100%',
height: height === 'auto' ? height : '100%'
};
return /*#__PURE__*/_react["default"].createElement(Element, _extends({
ref: this.ref,
src: this.getSource(url),
style: style,
preload: "auto",
autoPlay: playing || undefined,
controls: controls,
muted: muted,
loop: loop
}, config.file.attributes), url instanceof Array && url.map(this.renderSourceElement), config.file.tracks.map(this.renderTrack));
}
}]);
return FilePlayer;
}(_react.Component);
exports.FilePlayer = FilePlayer;
_defineProperty(FilePlayer, "displayName", 'FilePlayer');
_defineProperty(FilePlayer, "canPlay", canPlay);
_defineProperty(FilePlayer, "canEnablePIP", canEnablePIP);
var _default = (0, _singlePlayer["default"])(FilePlayer);
exports["default"] = _default;