maestro-react-player
Version:
A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
300 lines (264 loc) • 10.6 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PhenixPlayer = undefined;
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _utils = require('../utils');
var _singlePlayer = require('../singlePlayer');
var _singlePlayer2 = _interopRequireDefault(_singlePlayer);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // TODO: ReactPlayer's listener logic is very shaky because if you change the function identity
// it won't get cleaned up. This is an existing problem so we're not gonna fix it here.
var PHENIX_SDK_URL = 'https://unpkg.com/phenix-web-sdk@2019.2.3/dist/phenix-web-sdk.min.js';
var PHENIX_SDK_GLOBAL = 'phenix-web-sdk';
// TODO: Add optional auth data parameter at the end
var PHENIX_URL_REGEX = /^phenix:(.+?)\|(.+?)(?:\|(.+?))?$/i; // i hate this so much
function getPhenixSdk() {
return (0, _utils.getSDK)(PHENIX_SDK_URL, PHENIX_SDK_GLOBAL);
}
function canPlay(url) {
return PHENIX_URL_REGEX.test(url);
}
var PhenixPlayer = exports.PhenixPlayer = function (_Component) {
_inherits(PhenixPlayer, _Component);
function PhenixPlayer() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, PhenixPlayer);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = PhenixPlayer.__proto__ || Object.getPrototypeOf(PhenixPlayer)).call.apply(_ref, [this].concat(args))), _this), _this.player = null, _this.channelExpress = null, _this.playerRef = function (player) {
if (player === _this.player) {
return;
}
if (_this.player) {
_this.removeListeners();
}
_this.player = player;
if (_this.player) {
_this.addListeners();
}
}, _this.onSeek = function (e) {
_this.props.onSeek(e.target.currentTime);
}, _this.mute = function () {
_this.player.muted = true;
}, _this.unmute = function () {
_this.player.muted = false;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(PhenixPlayer, [{
key: 'componentWillUnmount',
value: function componentWillUnmount() {
// TODO: If refs get called with null on unmount, no reason to do this
if (this.player) {
this.removeListeners();
this.player = null;
}
if (this.channelExpress) {
this.channelExpress.dispose();
this.channelExpress = null;
}
}
}, {
key: 'addListeners',
value: function addListeners() {
var _props = this.props,
onReady = _props.onReady,
onPlay = _props.onPlay,
onPause = _props.onPause,
onEnded = _props.onEnded,
onVolumeChange = _props.onVolumeChange,
onError = _props.onError,
playsinline = _props.playsinline,
videoElementId = _props.videoElementId;
this.player.addEventListener('canplay', onReady);
this.player.addEventListener('play', onPlay);
this.player.addEventListener('pause', onPause);
this.player.addEventListener('seeked', this.onSeek);
this.player.addEventListener('ended', onEnded);
this.player.addEventListener('error', onError);
this.player.addEventListener('volumechange', onVolumeChange);
// wow
this.player.setAttribute('id', videoElementId);
if (playsinline) {
this.player.setAttribute('playsinline', '');
this.player.setAttribute('webkit-playsinline', '');
}
}
}, {
key: 'removeListeners',
value: function removeListeners() {
var _props2 = this.props,
onReady = _props2.onReady,
onPlay = _props2.onPlay,
onPause = _props2.onPause,
onEnded = _props2.onEnded,
onVolumeChange = _props2.onVolumeChange,
onError = _props2.onError;
this.player.removeEventListener('canplay', onReady);
this.player.removeEventListener('play', onPlay);
this.player.removeEventListener('pause', onPause);
this.player.removeEventListener('seeked', this.onSeek);
this.player.removeEventListener('ended', onEnded);
this.player.removeEventListener('error', onError);
this.player.removeEventListener('volumechange', onVolumeChange);
}
}, {
key: 'getPhenixBackendUri',
value: function getPhenixBackendUri() {
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props.url;
return PHENIX_URL_REGEX.exec(url)[1];
}
}, {
key: 'getPhenixChannelId',
value: function getPhenixChannelId() {
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props.url;
return PHENIX_URL_REGEX.exec(url)[2];
}
}, {
key: 'getPhenixAuthenticationData',
value: function getPhenixAuthenticationData() {
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props.url;
var match = PHENIX_URL_REGEX.exec(url)[3];
return match ? JSON.parse(match) : {};
}
}, {
key: 'load',
value: function load(url) {
var _this2 = this;
var backendUri = this.getPhenixBackendUri(url);
var channelId = this.getPhenixChannelId(url);
var authenticationData = this.getPhenixAuthenticationData(url);
var joinChannelCallback = function joinChannelCallback(err, response) {
var success = !err && response.status === 'ok';
if (!success) {
var error = err || new Error('Response status: ' + response.status);
_this2.props.onError(error);
}
};
var subscriberCallback = function subscriberCallback(err, response) {
var success = !err && ['ok', 'no-stream-playing'].includes(response.status);
if (!success) {
var error = err || new Error('Response status: ' + response.status);
_this2.props.onError(error);
}
// otherwise, response.mediaStream.getStreamId() will be a thing
};
getPhenixSdk().then(function (phenix) {
// TODO: Does this check do anything?
if (url !== _this2.props.url) {
return;
}
if (_this2.channelExpress) {
_this2.channelExpress.dispose();
_this2.channelExpress = null;
}
_this2.channelExpress = new phenix.express.ChannelExpress({
authenticationData: authenticationData,
backendUri: backendUri
});
_this2.channelExpress.joinChannel({
channelId: channelId,
videoElement: _this2.player
}, joinChannelCallback, subscriberCallback);
});
}
}, {
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() {
if (this.channelExpress) {
this.channelExpress.dispose();
this.channelExpress = null;
}
}
}, {
key: 'seekTo',
value: function seekTo(seconds) {
if (seconds === Infinity || this.getDuration() === Infinity) {
return;
}
this.player.currentTime = seconds;
}
}, {
key: 'setVolume',
value: function setVolume(fraction) {
this.player.volume = fraction;
}
}, {
key: 'setPlaybackRate',
value: function setPlaybackRate(rate) {
this.player.playbackRate = rate;
}
}, {
key: 'getDuration',
value: function getDuration() {
return this.player.duration;
}
}, {
key: 'getCurrentTime',
value: function getCurrentTime() {
return this.player.currentTime;
}
}, {
key: 'getSecondsLoaded',
value: function getSecondsLoaded() {
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: 'render',
value: function render() {
var _props3 = this.props,
playing = _props3.playing,
loop = _props3.loop,
controls = _props3.controls,
muted = _props3.muted,
width = _props3.width,
height = _props3.height;
var style = {
width: width === 'auto' ? width : '100%',
height: height === 'auto' ? height : '100%'
};
return _react2['default'].createElement('video', {
ref: this.playerRef,
style: style,
preload: 'auto' // TODO
, autoPlay: playing // TODO
, controls: controls // TODO
, muted: muted,
loop: loop
});
}
}]);
return PhenixPlayer;
}(_react.Component);
PhenixPlayer.displayName = 'PhenixPlayer';
PhenixPlayer.canPlay = canPlay;
exports['default'] = (0, _singlePlayer2['default'])(PhenixPlayer); // TODO: WTF does this even do?