import { OVar, e } from "../jshelper/mod.ts"; import { JhlsMetadata, TimeRange } from "./jhls.d.ts"; import { SegmentDownloader } from "./download.ts"; import { PlayerTrack } from "./track.ts"; import { ProfileSelector } from "./profiles.ts"; import { Logger } from "../jshelper/src/log.ts"; export interface BufferRange extends TimeRange { status: "buffered" | "loading" | "queued" } export class Player { public video = e("video") public media_source = new MediaSource(); public tracks = new OVar([]); public downloader: SegmentDownloader = new SegmentDownloader(); public profile_selector!: ProfileSelector public position = new OVar(0) public duration = new OVar(1) public playing = new OVar(false) public canplay = new OVar(false) public error = new OVar(undefined) private cancel_buffering_pers: undefined | (() => void) set_pers(s?: string) { if (this.cancel_buffering_pers) this.cancel_buffering_pers(), this.cancel_buffering_pers = undefined if (s) this.cancel_buffering_pers = this.logger?.log_persistent(s) } constructor(private node_id: string, public logger?: Logger) { this.video.onloadedmetadata = () => { } this.video.ondurationchange = () => { } this.video.ontimeupdate = () => { this.position.value = this.video.currentTime this.update() // TODO maybe not here } this.video.onplay = () => { console.log("play"); this.set_pers("Resuming playback...") } this.video.onwaiting = () => { console.log("waiting"); if (this.video.currentTime > this.duration.value - 0.2) return this.set_pers("Playback finished") this.set_pers("Buffering...") this.canplay.value = false; } this.video.onplaying = () => { console.log("playing"); this.playing.value = true; this.set_pers() } this.video.onpause = () => { console.log("pause"); this.playing.value = false } this.video.oncanplay = () => { console.log("canplay"); this.set_pers() this.canplay.value = true } this.video.onseeking = () => { console.log("seeking"); this.set_pers("Seeking...") } this.video.onseeked = () => { console.log("seeked"); this.set_pers() } this.video.onerror = e => { console.error("video element error:", e); } this.video.onabort = e => { console.error("video element abort:", e); } this.fetch_meta() } async fetch_meta() { this.set_pers("Loading media manifest...") const res = await fetch(`/n/${encodeURIComponent(this.node_id)}/stream?format=jhls`) if (!res.ok) return this.error.value = "Cannot download JHLS metadata" const metadata = await res.json() as JhlsMetadata this.set_pers() this.profile_selector = new ProfileSelector(this, this.downloader.bandwidth, metadata) this.set_pers("Checking codec support...") await this.profile_selector.init() this.duration.value = metadata.duration this.video.src = URL.createObjectURL(this.media_source) this.media_source.addEventListener("sourceopen", async () => { this.set_pers("Initializing Media Extensions...") this.tracks.value.push(await PlayerTrack.new(this, this.node_id, 0, metadata.tracks[0])) this.tracks.value.push(await PlayerTrack.new(this, this.node_id, 1, metadata.tracks[1])) this.tracks.change() this.set_pers("Fetching initial segments...") this.update() await this.canplay.wait_for(true) this.set_pers() }) } async update(newt?: number) { await Promise.all(this.tracks.value.map(t => t.update(newt ?? this.video.currentTime))) } play() { this.video.play() } pause() { this.video.pause() } frame_forward() { //@ts-ignore trust me bro this.video["seekToNextFrame"]() } async seek(p: number) { this.set_pers("Buffering at target...") await this.update(p) this.video.currentTime = p } }