@oplayer/plugins
Version:
oplayer's plugin
100 lines (99 loc) • 4.32 kB
JavaScript
/**
* name: @oplayer/chromecast
* version: v1.0.15-beta.4
* description: oplayer's plugin
* author: shiyiya
* homepage: https://github.com/shiyiya/oplayer
*/
import { isIOS as d, loadSDK as p } from "@oplayer/core";
const u = !!globalThis.chrome, C = '<svg viewBox="0 0 1024 1024" style="scale: 0.9;"><path d="M895.66 128H128a85.44 85.44 0 0 0-85.44 85.44v127.84H128v-127.84h767.66v597.12H597.28V896H896a85.44 85.44 0 0 0 85.44-85.44V213.44A85.44 85.44 0 0 0 896 128zM42.56 767.16v127.84h127.82a127.82 127.82 0 0 0-127.82-127.84z m0-170.56V682a213.26 213.26 0 0 1 213.28 213.32v0.68h85.44a298.38 298.38 0 0 0-298-298.72h-0.66z m0-170.54v85.44c212-0.2 384 171.5 384.16 383.5v1h85.44c-0.92-258.92-210.68-468.54-469.6-469.28z"></path></svg>';
class w {
constructor(t) {
this.options = t, this.name = "oplayer-plugin-chromecast", this.version = "1.0.15-beta.4";
}
apply(t) {
if (this.canPlay())
return this.player = t, this.registerUI(), this;
}
get cast() {
return cast.framework.CastContext.getInstance();
}
get castSessionMedia() {
var t;
return (t = this.cast.getCurrentSession()) == null ? void 0 : t.getSessionObj().media[0];
}
get isCastConnected() {
return this.cast.getCastState() === cast.framework.CastState.CONNECTED;
}
get device() {
var t;
return (t = this.cast.getCurrentSession()) == null ? void 0 : t.getCastDevice();
}
hasActiveCastSession(t) {
var e, s;
return ((s = (e = this.castSessionMedia) == null ? void 0 : e.media) == null ? void 0 : s.contentId) === (t == null ? void 0 : t.src);
}
canPlay() {
return u && !d;
}
async __requestChromeCast() {
var a;
if (this._player || (this._player = new cast.framework.RemotePlayer(), new cast.framework.RemotePlayerController(this._player)), this.hasActiveCastSession(this.player.options.source))
return;
this.cast.setOptions({
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,
resumeSavedSession: !0,
androidReceiverCompatible: !0,
...this.options
});
const t = await this.cast.requestSession();
if (t)
throw new Error(`Chrome Cast Error Code: ${t}`);
return (a = this.cast.getCurrentSession()) == null ? void 0 : a.loadMedia(this.__buildRequest());
}
__buildRequest() {
var c, h, l;
const { source: t, isLive: a } = this.player.options, e = new chrome.cast.media.MediaInfo(
t.src,
((c = this.player.loader) == null ? void 0 : c.key) == "hls" ? "application/x-mpegurl" : "video/mp4"
);
e.contentUrl = t.src, e.streamType = a ? chrome.cast.media.StreamType.LIVE : chrome.cast.media.StreamType.BUFFERED;
const s = new chrome.cast.media.GenericMediaMetadata();
t.title && (s.title = t.title), t.poster && (s.images = [{ url: t.poster, height: null, width: null }]), e.metadata = s;
const n = (l = (h = this.player.context.ui) == null ? void 0 : h.config.subtitle) == null ? void 0 : l.source;
n && (e.tracks = n.map((i, m) => {
const r = new chrome.cast.media.Track(m, chrome.cast.media.TrackType.TEXT);
return r.name = i.name, r.trackContentId = i.src, r.trackContentType = i.type || "text/vtt", r.language = i.language || i.name, r.subtype = chrome.cast.media.TextTrackType.CAPTIONS, r;
}));
const o = new chrome.cast.media.LoadRequest(e);
return o.autoplay = this.player.isPlaying, o.currentTime = this.player.currentTime, o;
}
async _loadCast() {
var t;
(t = window.cast) != null && t.framework || (await p("https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1", "cast"), await customElements.whenDefined("google-cast-launcher"));
}
async start() {
try {
await this._loadCast();
const t = await this.__requestChromeCast();
if (t)
throw new Error(`Chrome Cast Error Code: ${t}`);
} catch (t) {
this.player.emit("cast-error", t);
}
}
registerUI() {
if (!this.player.context.ui) return;
const { menu: t, icons: a } = this.player.context.ui;
t == null || t.register({
name: this.player.locales.get("Chromecast"),
position: "top",
icon: a.chromecast || C,
onClick: () => this.start()
});
}
}
export {
w as default
};