UNPKG

@epiclabs/epic-video-player

Version:

Video player wrapper to support different video sources with an unified interface

196 lines (195 loc) 6.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlayerDash = void 0; var tslib_1 = require("tslib"); var dashjs = tslib_1.__importStar(require("dashjs")); var player_1 = require("./player"); var PlayerDash = /** @class */ (function (_super) { tslib_1.__extends(PlayerDash, _super); function PlayerDash(url, htmlPlayer, config) { return _super.call(this, url, htmlPlayer, config) || this; } PlayerDash.getCodecName = function (codec) { var re = /"(.*?)"/g; var codecName = re.exec(codec); if (codecName && codecName.length > 0 && !!codecName[1]) { return codecName[1]; } return; }; PlayerDash.prototype.load = function () { this.reset(); try { this.player = dashjs.MediaPlayer().create(); this.player.updateSettings({ debug: { logLevel: 0, }, }); this.player.initialize(this.htmlPlayer, this.url, false); // an initial rendition needs to be loaded if (this.config && typeof this.config.initialRenditionKbps === 'number') { if (this.config.initialRenditionKbps >= 0) { this.player.updateSettings({ streaming: { abr: { autoSwitchBitrate: { audio: true, video: false, }, initialBitrate: { video: this.config.initialRenditionKbps, }, }, lastBitrateCachingInfo: { enabled: false, }, }, }); } else { this.player.updateSettings({ streaming: { abr: { autoSwitchBitrate: { audio: true, video: true, }, }, lastBitrateCachingInfo: { enabled: false, }, }, }); } } this.initListeners(); this.playerType = 'DASH'; } catch (e) { console.error(e); } }; PlayerDash.prototype.destroy = function () { try { this.htmlPlayer.src = ''; if (this.player !== undefined) { this.player.reset(); } } catch (e) { console.error(e); } finally { this.htmlPlayer.pause(); this.htmlPlayer.removeAttribute('src'); this.htmlPlayer.load(); this.playerType = undefined; this.reset(); } }; PlayerDash.prototype.getRenditions = function () { if (this.player === undefined) { return; } return this.convertBitratesToIRenditions(this.player.getBitrateInfoListFor('video')); }; PlayerDash.prototype.setRendition = function (rendition, immediately) { if (this.player === undefined) { return; } if (typeof rendition === 'number') { if (rendition === -1) { this.player.updateSettings({ streaming: { abr: { autoSwitchBitrate: { audio: true, video: true, }, }, }, }); } else { this.player.updateSettings({ streaming: { abr: { autoSwitchBitrate: { audio: true, video: false, }, }, lastBitrateCachingInfo: { enabled: false, }, }, }); this.player.setQualityFor('video', rendition); if (immediately) { // dash.js does not provide this feature yet } } return; } else { var renditions = this.getRenditions(); if (renditions !== undefined && renditions.length > 0) { for (var i = 0; i < renditions.length; i++) { if (renditions[i].bitrate === rendition.bitrate) { return this.setRendition(i, immediately); } } } } return this.setRendition(-1, immediately); }; PlayerDash.prototype.getCurrentRendition = function () { if (this.player === undefined) { return; } var renditions = this.getRenditions(); if (renditions !== undefined && renditions.length > 0) { var currentQuality = this.player.getQualityFor('video'); if (currentQuality >= 0 && renditions.length > currentQuality) { return renditions[currentQuality]; } } return; }; PlayerDash.prototype.convertBitratesToIRenditions = function (bitrates) { if (bitrates === undefined || bitrates.length === 0) { return; } var videoInfo; try { videoInfo = this.player.getCurrentTrackFor('video'); } catch (e) { // nothing to do } var audioInfo; try { audioInfo = this.player.getCurrentTrackFor('audio'); } catch (e) { // nothing to do } return bitrates.map(function (b) { return { audioCodec: audioInfo && audioInfo.codec ? PlayerDash.getCodecName(audioInfo.codec) : undefined, bitrate: b.bitrate !== undefined ? b.bitrate : undefined, height: b.height !== undefined ? b.height : undefined, level: b.qualityIndex !== undefined ? b.qualityIndex : undefined, videoCodec: videoInfo && videoInfo.codec ? PlayerDash.getCodecName(videoInfo.codec) : undefined, width: b.width !== undefined ? b.width : undefined, }; }); }; return PlayerDash; }(player_1.Player)); exports.PlayerDash = PlayerDash;