diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-15 13:54:52 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-15 13:54:52 +0200 |
commit | c59abb792391e2f7540a80bb8d989021fe0a5b80 (patch) | |
tree | 33688772e3822b38441f86a08c0c595ea1ef14b0 /web/script/player/player.ts | |
parent | 3b147cb1dfcbd5c7218e0accd5784d992d5ae21c (diff) | |
download | jellything-c59abb792391e2f7540a80bb8d989021fe0a5b80.tar jellything-c59abb792391e2f7540a80bb8d989021fe0a5b80.tar.bz2 jellything-c59abb792391e2f7540a80bb8d989021fe0a5b80.tar.zst |
refactor jsp, part 1
Diffstat (limited to 'web/script/player/player.ts')
-rw-r--r-- | web/script/player/player.ts | 62 |
1 files changed, 22 insertions, 40 deletions
diff --git a/web/script/player/player.ts b/web/script/player/player.ts index e0a6ddf..f44c14f 100644 --- a/web/script/player/player.ts +++ b/web/script/player/player.ts @@ -5,20 +5,18 @@ */ /// <reference lib="dom" /> import { OVar, e } from "../jshelper/mod.ts"; -import { NodePublic, NodeUserData, SourceTrack, TimeRange } from "./jhls.d.ts"; import { SegmentDownloader } from "./download.ts"; import { PlayerTrack } from "./track/mod.ts"; import { Logger } from "../jshelper/src/log.ts"; -import { WatchedState, Chapter } from "./jhls.d.ts"; -import { get_track_kind } from "./mediacaps.ts"; import { create_track } from "./track/create.ts"; +import { StreamInfo, TimeRange, TrackInfo } from "./types_stream.ts"; export interface BufferRange extends TimeRange { status: "buffered" | "loading" | "queued" } export class Player { public video = e("video") public media_source = new MediaSource(); - public tracks?: SourceTrack[]; - public chapters = new OVar<Chapter[]>([]); + public streaminfo?: StreamInfo; + public tracks?: TrackInfo[]; public active_tracks = new OVar<PlayerTrack[]>([]); public downloader: SegmentDownloader = new SegmentDownloader(); @@ -35,8 +33,8 @@ export class Player { if (s) this.cancel_buffering_pers = this.logger?.log_persistent(s) } - constructor(public node_id: string, public logger?: Logger<string>) { - this.video.poster = `/n/${encodeURIComponent(node_id)}/poster` + constructor(public base_url: string, poster: string, private start_time: number, public logger?: Logger<string>) { + this.video.poster = poster this.volume.value = this.video.volume let skip_change = false; this.volume.onchange(v => { @@ -100,40 +98,38 @@ export class Player { } async fetch_meta() { - this.set_pers("Loading metadata...") - const res = await fetch(`/n/${encodeURIComponent(this.node_id)}`, { headers: { "Accept": "application/json" } }) - if (!res.ok) return this.error.value = "Cannot download node." + this.set_pers("Loading stream metadata...") + const res = await fetch(`${this.base_url}?info`, { headers: { "Accept": "application/json" } }) + if (!res.ok) return this.error.value = "Cannot download stream info." - let ndata!: { node: NodePublic, userdata: NodeUserData } & { error: string } - try { ndata = await res.json() } + let streaminfo!: StreamInfo & { error: string } + try { streaminfo = await res.json() } catch (_) { this.set_pers("Error: Node data invalid") } - if (ndata.error) return this.set_pers("server error: " + ndata.error) + if (streaminfo.error) return this.set_pers("server error: " + streaminfo.error) this.set_pers() //! bad code: assignment order is important because chapter callbacks use duration - this.duration.value = ndata.node.media!.duration - this.chapters.value = ndata.node.media!.chapters - this.tracks = ndata.node.media!.tracks + this.duration.value = streaminfo.segments[0].duration + this.streaminfo = streaminfo + this.tracks = streaminfo!.segments[0].tracks; this.video.src = URL.createObjectURL(this.media_source) this.media_source.addEventListener("sourceopen", async () => { let video = false, audio = false, subtitles = false; for (let i = 0; i < this.tracks!.length; i++) { const t = this.tracks![i]; - const kind = get_track_kind(t.kind) - if (kind == "video" && !video) + if (t.kind == "video" && !video) video = true, await this.set_track_enabled(i, true, false) - if (kind == "audio" && !audio) + if (t.kind == "audio" && !audio) audio = true, await this.set_track_enabled(i, true, false) - if (kind == "subtitles" && !subtitles) + if (t.kind == "subtitles" && !subtitles) subtitles = true, await this.set_track_enabled(i, true, false) } this.set_pers("Buffering initial stream fragments...") - const start_time = get_query_start_time() ?? get_continue_time(ndata.userdata.watched); - this.update(start_time) - this.video.currentTime = start_time + this.update(this.start_time) + this.video.currentTime = this.start_time await this.canplay.wait_for(true) this.set_pers() @@ -153,7 +149,7 @@ export class Player { track.abort.abort() } else if (state && active_index == -1) { this.logger?.log(`Enabled track ${index}: ${display_track(this.tracks![index])}`) - this.active_tracks.value.push(create_track(this, this.node_id, index, this.tracks![index])!) + this.active_tracks.value.push(create_track(this, this.base_url, 0, index, this.tracks![index])!) if (update) await this.update() } this.active_tracks.change() @@ -172,20 +168,6 @@ export class Player { } } -function get_continue_time(w: WatchedState): number { - if (typeof w == "string") return 0 - else return w.progress -} - -function get_query_start_time() { - const u = new URL(globalThis.location.href) - const p = u.searchParams.get("t") - if (!p) return - const x = parseFloat(p) - if (Number.isNaN(x)) return - return x -} - -function display_track(t: SourceTrack): string { - return `"${t.name}" (${t.language})` +function display_track(t: TrackInfo): string { + return `${t.name}` } |