UNPKG

playable

Version:

Video player based on HTML5Video

212 lines 8.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var index_mediaplayerOnly_1 = require("dashjs/build/es5/index_mediaplayerOnly"); var video_data_1 = require("../utils/video-data"); var environment_detection_1 = require("../utils/environment-detection"); var constants_1 = require("../constants"); var INITIAL_BITRATE = 5000; var DashEvents = index_mediaplayerOnly_1.MediaPlayer.events; var DashAdapter = /** @class */ (function () { function DashAdapter(eventEmitter) { this.eventEmitter = eventEmitter; this.dashPlayer = null; this.mediaStream = null; this.videoElement = null; this._bindCallbacks(); } DashAdapter.isSupported = function () { return environment_detection_1.NativeEnvironmentSupport.MSE; }; DashAdapter.prototype.canPlay = function (mediaType) { return mediaType === constants_1.MediaStreamType.DASH; }; Object.defineProperty(DashAdapter.prototype, "mediaStreamDeliveryPriority", { get: function () { return constants_1.MediaStreamDeliveryPriority.ADAPTIVE_VIA_MSE; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "currentUrl", { get: function () { return this.mediaStream.url; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "syncWithLiveTime", { get: function () { // TODO: implement syncWithLiveTime for `dash` return undefined; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "isDynamicContent", { get: function () { return false; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "isDynamicContentEnded", { get: function () { // TODO: implement isDynamicContentEnded return false; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "isSyncWithLive", { get: function () { return false; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "isSeekAvailable", { get: function () { return true; }, enumerable: false, configurable: true }); Object.defineProperty(DashAdapter.prototype, "debugInfo", { get: function () { var currentStream = this.dashPlayer.getActiveStream(); var currentTime = 0; if (currentStream) { currentTime = this.dashPlayer.time(currentStream.getId()); } var bitrates = this.dashPlayer .getBitrateInfoListFor('video') .map(function (bitrateInfo) { return bitrateInfo.bitrate; }); var currentBitrate = null; if (this.dashPlayer.getQualityFor('video') && bitrates) { currentBitrate = bitrates[this.dashPlayer.getQualityFor('video')]; } var overallBufferLength = this.dashPlayer.getBufferLength('video'); var currentTrack = this.dashPlayer.getCurrentTrackFor('video'); var nearestBufferSegInfo = (0, video_data_1.getNearestBufferSegmentInfo)(this.dashPlayer.getVideoElement().buffered, currentTime); var bwEstimate = this.dashPlayer.getAverageThroughput('video'); return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.mediaStream), { bwEstimate: bwEstimate, deliveryPriority: this.mediaStreamDeliveryPriority, bitrates: bitrates, currentBitrate: currentBitrate, overallBufferLength: overallBufferLength, currentTrack: currentTrack, nearestBufferSegInfo: nearestBufferSegInfo }); }, enumerable: false, configurable: true }); DashAdapter.prototype._bindCallbacks = function () { this._broadcastError = this._broadcastError.bind(this); }; DashAdapter.prototype.setMediaStreams = function (mediaStreams) { if (mediaStreams.length === 1) { this.mediaStream = mediaStreams[0]; } else { throw new Error("Can only handle a single DASH stream. Received ".concat(mediaStreams.length, " streams.")); } }; DashAdapter.prototype._logError = function (error, errorEvent) { this.eventEmitter.emitAsync(constants_1.VideoEvent.ERROR, { errorType: error, streamType: constants_1.MediaStreamType.DASH, streamProvider: 'dash.js', errorInstance: errorEvent, }); }; DashAdapter.prototype._broadcastError = function (errorEvent) { if (!errorEvent) { return; } if (errorEvent.error === 'download') { switch (errorEvent.event.id) { case 'manifest': this._logError(constants_1.Error.MANIFEST_LOAD, errorEvent); break; case 'content': this._logError(constants_1.Error.CONTENT_LOAD, errorEvent); break; case 'initialization': this._logError(constants_1.Error.LEVEL_LOAD, errorEvent); break; default: this._logError(constants_1.Error.UNKNOWN, errorEvent); } } else if (errorEvent.error === 'manifestError') { switch (errorEvent.event.id) { case 'codec': this._logError(constants_1.Error.MANIFEST_INCOMPATIBLE, errorEvent); break; case 'parse': this._logError(constants_1.Error.MANIFEST_PARSE, errorEvent); break; default: this._logError(constants_1.Error.UNKNOWN, errorEvent); } } else if (errorEvent.error === 'mediasource') { this._logError(constants_1.Error.MEDIA, errorEvent); } else { this._logError(constants_1.Error.UNKNOWN, errorEvent); } }; DashAdapter.prototype.attach = function (videoOutput) { if (!this.mediaStream) { return; } this.videoElement = videoOutput; this.dashPlayer = (0, index_mediaplayerOnly_1.MediaPlayer)().create(); this.dashPlayer.updateSettings({ debug: { logLevel: index_mediaplayerOnly_1.LogLevel.LOG_LEVEL_NONE, }, }); this.dashPlayer.on(DashEvents.ERROR, this._broadcastError); if (videoOutput.preload === 'none') { this._startDelayedInitPlayer(); } else { this._initPlayer(); } }; DashAdapter.prototype._delayedInitPlayer = function () { this._stopDelayedInitPlayer(); this._initPlayer(true); }; DashAdapter.prototype._startDelayedInitPlayer = function () { this.eventEmitter.on(constants_1.VideoEvent.PLAY_REQUEST, this._delayedInitPlayer, this); }; DashAdapter.prototype._stopDelayedInitPlayer = function () { this.eventEmitter.off(constants_1.VideoEvent.PLAY_REQUEST, this._delayedInitPlayer, this); }; DashAdapter.prototype._initPlayer = function (forceAutoplay) { this.dashPlayer.initialize(this.videoElement, this.mediaStream.url, forceAutoplay || this.videoElement.autoplay); this.dashPlayer.updateSettings({ streaming: { abr: { initialBitrate: { video: INITIAL_BITRATE, }, }, liveCatchup: {}, }, }); //this.dashPlayer.setInitialBitrateFor('video', INITIAL_BITRATE); }; DashAdapter.prototype.detach = function () { this._stopDelayedInitPlayer(); if (!this.mediaStream) { return; } this.dashPlayer.reset(); this.dashPlayer.off(DashEvents.ERROR, this._broadcastError); this.dashPlayer = null; this.videoElement.removeAttribute('src'); this.videoElement = null; }; return DashAdapter; }()); exports.default = DashAdapter; //# sourceMappingURL=dash.js.map