@oplayer/mpegts
Version:
mpegts.js plugin for oplayer
80 lines (79 loc) • 2.49 kB
JavaScript
/**
* name: @oplayer/mpegts
* version: v1.2.26-beta.0
* description: mpegts.js plugin for oplayer
* author: shiyiya
* homepage: https://github.com/shiyiya/oplayer
*/
import { loadSDK } from "@oplayer/core";
const PLUGIN_NAME = "oplayer-plugin-mpegts";
const REG = /(flv|ts|m2ts)(#|\?|$)/i;
const defaultMatcher = (_, source) => {
if (source.format && ["flv", "m2ts", "mpegts"].includes(source.format)) {
return true;
}
return (source.format === "auto" || typeof source.format === "undefined") && REG.test(source.src);
};
const _MpegtsPlugin = class _MpegtsPlugin {
constructor(options) {
this.key = "mpegts";
this.name = PLUGIN_NAME;
this.version = "1.2.26-beta.0";
this.options = {
matcher: defaultMatcher,
config: void 0
};
Object.assign(this.options, options);
}
apply(player) {
this.player = player;
return this;
}
async load(_ref, source) {
var _a;
let $video = _ref.$video, options = _ref.options;
const _this$options = this.options, matcher = _this$options.matcher, library = _this$options.library;
if (!matcher($video, source)) return false;
if (!_MpegtsPlugin.library) {
_MpegtsPlugin.library = globalThis.mpegts || //@ts-expect-error
(library ? await loadSDK(library, "mpegts") : (await import("mpegts.js/dist/mpegts.js")).default);
_MpegtsPlugin.library.LoggingControl.applyConfig({
enableAll: false
});
}
if (!_MpegtsPlugin.library.isSupported()) return false;
_MpegtsPlugin.library.LoggingControl.addLogListener(this.logListener.bind(this));
this.instance = _MpegtsPlugin.library.createPlayer({
url: source.src,
isLive: options.isLive,
type: source.format || ((_a = REG.exec(source.src)) == null ? void 0 : _a[0])
// could also be mpegts, m2ts, flv
}, this.options.config);
this.instance.attachMediaElement($video);
this.instance.load();
return this;
}
destroy() {
var _a;
(_a = this.instance) == null ? void 0 : _a.destroy();
_MpegtsPlugin.library.LoggingControl.removeLogListener(this.logListener.bind(this));
}
logListener(level, msg) {
if (level === "error") {
this.player.emit("error", {
level,
msg,
pluginName: PLUGIN_NAME,
message: msg
});
}
}
};
_MpegtsPlugin.library = globalThis.mpegts;
let MpegtsPlugin = _MpegtsPlugin;
function create(options) {
return new MpegtsPlugin(options);
}
export {
create as default
};