UNPKG

@jxstjh/jhvideo

Version:

HTML5 jhvideo base on MPEG2-TS Stream Player

258 lines 9.9 kB
/* * Copyright (C) 2016 Bilibili. All Rights Reserved. * * @author zheng qian <xqq@xqq.im> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import EventEmitter from 'events'; import PlayerEvents from './player-events.js'; import { createDefaultConfig } from '../config.js'; import { InvalidArgumentException, IllegalStateException } from '../utils/exception.js'; // Player wrapper for browser's native player (HTMLVideoElement) without MediaSource src. var NativePlayer = /** @class */ (function () { function NativePlayer(mediaDataSource, config) { this.TAG = 'NativePlayer'; this._type = 'NativePlayer'; this._emitter = new EventEmitter(); this._config = createDefaultConfig(); if (typeof config === 'object') { Object.assign(this._config, config); } var typeLowerCase = mediaDataSource.type.toLowerCase(); if (typeLowerCase === 'mse' || typeLowerCase === 'mpegts' || typeLowerCase === 'm2ts' || typeLowerCase === 'flv') { throw new InvalidArgumentException('NativePlayer does\'t support mse/mpegts/m2ts/flv MediaDataSource input!'); } if (mediaDataSource.hasOwnProperty('segments')) { throw new InvalidArgumentException("NativePlayer(".concat(mediaDataSource.type, ") doesn't support multipart playback!")); } this.e = { onvLoadedMetadata: this._onvLoadedMetadata.bind(this) }; this._pendingSeekTime = null; this._statisticsReporter = null; this._mediaDataSource = mediaDataSource; this._mediaElement = null; } NativePlayer.prototype.destroy = function () { if (this._mediaElement) { this.unload(); this.detachMediaElement(); } this.e = null; this._mediaDataSource = null; this._emitter.removeAllListeners(); this._emitter = null; }; NativePlayer.prototype.on = function (event, listener) { var _this = this; if (event === PlayerEvents.MEDIA_INFO) { if (this._mediaElement != null && this._mediaElement.readyState !== 0) { // HAVE_NOTHING Promise.resolve().then(function () { _this._emitter.emit(PlayerEvents.MEDIA_INFO, _this.mediaInfo); }); } } else if (event === PlayerEvents.STATISTICS_INFO) { if (this._mediaElement != null && this._mediaElement.readyState !== 0) { Promise.resolve().then(function () { _this._emitter.emit(PlayerEvents.STATISTICS_INFO, _this.statisticsInfo); }); } } this._emitter.addListener(event, listener); }; NativePlayer.prototype.off = function (event, listener) { this._emitter.removeListener(event, listener); }; NativePlayer.prototype.attachMediaElement = function (mediaElement) { this._mediaElement = mediaElement; mediaElement.addEventListener('loadedmetadata', this.e.onvLoadedMetadata); if (this._pendingSeekTime != null) { try { mediaElement.currentTime = this._pendingSeekTime; this._pendingSeekTime = null; } catch (e) { // IE11 may throw InvalidStateError if readyState === 0 // Defer set currentTime operation after loadedmetadata } } }; NativePlayer.prototype.detachMediaElement = function () { if (this._mediaElement) { this._mediaElement.src = ''; this._mediaElement.removeAttribute('src'); this._mediaElement.removeEventListener('loadedmetadata', this.e.onvLoadedMetadata); this._mediaElement = null; } if (this._statisticsReporter != null) { window.clearInterval(this._statisticsReporter); this._statisticsReporter = null; } }; NativePlayer.prototype.load = function () { if (!this._mediaElement) { throw new IllegalStateException('HTMLMediaElement must be attached before load()!'); } this._mediaElement.src = this._mediaDataSource.url; if (this._mediaElement.readyState > 0) { this._mediaElement.currentTime = 0; } this._mediaElement.preload = 'auto'; this._mediaElement.load(); this._statisticsReporter = window.setInterval(this._reportStatisticsInfo.bind(this), this._config.statisticsInfoReportInterval); }; NativePlayer.prototype.unload = function () { if (this._mediaElement) { this._mediaElement.src = ''; this._mediaElement.removeAttribute('src'); } if (this._statisticsReporter != null) { window.clearInterval(this._statisticsReporter); this._statisticsReporter = null; } }; NativePlayer.prototype.play = function () { return this._mediaElement.play(); }; NativePlayer.prototype.pause = function () { this._mediaElement.pause(); }; Object.defineProperty(NativePlayer.prototype, "type", { get: function () { return this._type; }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "buffered", { get: function () { return this._mediaElement.buffered; }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "duration", { get: function () { return this._mediaElement.duration; }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "volume", { get: function () { return this._mediaElement.volume; }, set: function (value) { this._mediaElement.volume = value; }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "muted", { get: function () { return this._mediaElement.muted; }, set: function (muted) { this._mediaElement.muted = muted; }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "currentTime", { get: function () { if (this._mediaElement) { return this._mediaElement.currentTime; } return 0; }, set: function (seconds) { if (this._mediaElement) { this._mediaElement.currentTime = seconds; } else { this._pendingSeekTime = seconds; } }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "mediaInfo", { get: function () { var mediaPrefix = (this._mediaElement instanceof HTMLAudioElement) ? 'audio/' : 'video/'; var info = { mimeType: mediaPrefix + this._mediaDataSource.type }; if (this._mediaElement) { info.duration = Math.floor(this._mediaElement.duration * 1000); if (this._mediaElement instanceof HTMLVideoElement) { info.width = this._mediaElement.videoWidth; info.height = this._mediaElement.videoHeight; } } return info; }, enumerable: false, configurable: true }); Object.defineProperty(NativePlayer.prototype, "statisticsInfo", { get: function () { var info = { playerType: this._type, url: this._mediaDataSource.url }; if (!(this._mediaElement instanceof HTMLVideoElement)) { return info; } var hasQualityInfo = true; var decoded = 0; var dropped = 0; if (this._mediaElement.getVideoPlaybackQuality) { var quality = this._mediaElement.getVideoPlaybackQuality(); decoded = quality.totalVideoFrames; dropped = quality.droppedVideoFrames; } else if (this._mediaElement.webkitDecodedFrameCount != undefined) { decoded = this._mediaElement.webkitDecodedFrameCount; dropped = this._mediaElement.webkitDroppedFrameCount; } else { hasQualityInfo = false; } if (hasQualityInfo) { info.decodedFrames = decoded; info.droppedFrames = dropped; } return info; }, enumerable: false, configurable: true }); NativePlayer.prototype._onvLoadedMetadata = function (e) { if (this._pendingSeekTime != null) { this._mediaElement.currentTime = this._pendingSeekTime; this._pendingSeekTime = null; } this._emitter.emit(PlayerEvents.MEDIA_INFO, this.mediaInfo); }; NativePlayer.prototype._reportStatisticsInfo = function () { this._emitter.emit(PlayerEvents.STATISTICS_INFO, this.statisticsInfo); }; return NativePlayer; }()); export default NativePlayer; //# sourceMappingURL=native-player.js.map