aboutsummaryrefslogtreecommitdiff
path: root/web/script/player/mod.ts
blob: aed504a098c56726c2faf726740e1d8a785e0f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
import { OVar, e } from "../jshelper/mod.ts";

export interface Range { start: number, end: number }
export interface JhlsMetadata {
    tracks: JhlsTrack[],
    duration: number,
}
export interface JhlsTrack {
    info: SourceTrack,
    segments: Range[],
}
export interface SourceTrack {
    kind: SourceTrackKind,
    name: string,
    codec: string,
    language: string,
}
export interface SourceTrackKind {
    video?: {
        width: number,
        height: number,
        fps: number,
    },
    audio?: {
        channels: number,
        sample_rate: number,
        bit_depth: number,
    },
    subtitles?: boolean,
}

const TARGET_BUFFER_DURATION = 30

document.addEventListener("DOMContentLoaded", () => {
    if (document.body.classList.contains("player")) {
        if (!globalThis.MediaSource) return alert("Media Source Extension API required")
        const node_id = globalThis.location.pathname.split("/")[2];
        const main = document.getElementById("main")!;
        document.getElementsByTagName("footer")[0].remove()
        initialize_player(main, node_id)
    }
})

function initialize_player(el: HTMLElement, node_id: string) {
    el.innerHTML = "" // clear the body

    const player = new Player(node_id)

    const toggle_playing = () => player.playing.value ? player.pause() : player.play()
    const pri_map = (v: number) => (v / player.duration.value * 100) + "%"

    let pri_current: HTMLElement;
    let pri: HTMLElement;
    const controls = e("div", { class: "jsp-controls" },
        player.playing.map(playing => e("button", playing ? "||" : "|>", { onclick() { } })),
        e("p", { class: "jsp-status" },
            player.position.map(v => e("span", display_time(v))), e("br"),
            player.position.map(v => e("span", display_time(v - player.duration.value)))
        ),
        pri = e("div", { class: "jsp-pri" },
            pri_current = e("div", { class: "jsp-pri-current" }),
            player.buffered.map(
                ranges => e("div", ...ranges.map(
                    r => e("div", {
                        class: "jsp-pri-buffered", style: {
                            width: pri_map(r.end - r.start),
                            left: pri_map(r.start)
                        }
                    })
                ))
            )
        ),
        e("button", "X", {
            onclick() {
                if (document.fullscreenElement) document.exitFullscreen()
                else document.documentElement.requestFullscreen()
            }
        })
    )

    player.position.onchangeinit(p => pri_current.style.width = pri_map(p))

    const pel = e("div", { class: "jsp" },
        player.video,
        player.buffering_status.map(b => e("div", { class: "jsp-overlay" },
            b ? e("p", { class: "jsp-buffering" }, b) : undefined
        )),
        controls,
    )
    el.append(pel)

    mouse_idle(pel, 1000, idle => {
        controls.style.opacity = idle ? "0" : "1"
        pel.style.cursor = idle ? "none" : "default"
    })

    player.video.addEventListener("click", toggle_playing)
    pri.addEventListener("mousedown", ev => {
        const r = pri.getBoundingClientRect()
        const p = (ev.clientX - r.left) / (r.right - r.left)
        player.seek(p * player.duration.value)
    })
    document.body.addEventListener("keydown", k => {
        // if (k.code == "Period") vel["seekToNextFrame" as "play"]()
        if (k.code == "Space") toggle_playing()
        else if (k.code == "ArrowLeft") player.seek(player.position.value - 5)
        else if (k.code == "ArrowRight") player.seek(player.position.value + 5)
        else if (k.code == "ArrowUp") player.seek(player.position.value - 60)
        else if (k.code == "ArrowDown") player.seek(player.position.value + 60)
        else return;
        k.preventDefault()
    })

}

function mouse_idle(e: HTMLElement, timeout: number, cb: (b: boolean) => unknown) {
    let ct: number;
    let idle = false
    e.onmouseleave = () => { clearTimeout(ct) }
    e.onmousemove = () => {
        clearTimeout(ct)
        if (idle) {
            idle = false
            cb(idle)
        }
        ct = setTimeout(() => {
            idle = true
            cb(idle)
        }, timeout)
    }
}
function display_time(t: number): string {
    if (t < 0) return "-" + display_time(-t)
    let h = 0, m = 0, s = 0;
    while (t > 3600) t -= 3600, h++;
    while (t > 60) t -= 60, m++;
    while (t > 1) t -= 1, s++;
    return (h ? h + "h" : "") + (m ? m + "m" : "") + (s ? s + "s" : "")
}

interface BufferRange extends Range { status: "buffered" | "downloading" | "queued" }
class Player {
    public video = e("video")
    private media_source = new MediaSource();
    public tracks: PlayerTrack[] = [];

    public position = new OVar(0)
    public duration = new OVar(1)
    public playing = new OVar(false)
    public buffered = new OVar<BufferRange[]>([])
    public buffering_status = new OVar<string | undefined>(undefined)
    public error = new OVar<string | undefined>(undefined)

    constructor(private node_id: string) {
        this.video.onloadedmetadata = () => { }
        this.video.ondurationchange = () => { }
        this.video.ontimeupdate = () => {
            this.position.value = this.video.currentTime
            this.update() // TODO maybe not here
            this.update_buffered_ranges()
        }
        this.video.onplay = () => {
            this.buffering_status.value = undefined;
        }
        this.video.onwaiting = () => {
            this.buffering_status.value = "Buffering...";
        }
        this.video.onplaying = () => {
            this.playing.value = true;
        }
        this.video.onpause = () => {
            this.playing.value = false
        }
        this.fetch_meta()
    }

    update_buffered_ranges() {
        const o: BufferRange[] = []
        for (let i = 0; i < this.video.buffered.length; i++) {
            o.push({
                start: this.video.buffered.start(i),
                end: this.video.buffered.end(i),
                status: "buffered"
            })
        }
        this.buffered.value = o;
    }

    async fetch_meta() {
        this.buffering_status.value = "Loading JHLS metadata..."
        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.buffering_status.value = undefined

        this.duration.value = metadata.duration
        this.video.src = URL.createObjectURL(this.media_source)
        this.media_source.addEventListener("sourceopen", () => {
            this.tracks.push(new PlayerTrack(this.media_source, this.node_id, 0, metadata.tracks[0]))
            this.tracks.push(new PlayerTrack(this.media_source, this.node_id, 1, metadata.tracks[1]))
            this.buffering_status.value = "Fetching initial segments..."
            this.update()
        })
    }
    update() {
        this.tracks.forEach(t => t.update(this.video.currentTime))
    }

    play() {
        console.log("play");
        this.video.play()
    }
    pause() {
        console.log("pause");
        this.video.pause()
    }
    seek(p: number) {
        this.video.currentTime = p
        this.update()
    }

}

class PlayerTrack {
    private source_buffer: SourceBuffer
    private loaded = new Set<number>()
    private loading = new Set<number>()
    private append_queue: (Range & { buf: ArrayBuffer, cb: () => void })[] = []
    constructor(media_source: MediaSource, private node_id: string, private track_index: number, private metadata: JhlsTrack) {
        this.source_buffer = media_source.addSourceBuffer("video/webm")
        this.source_buffer.mode = "segments"
        this.source_buffer.addEventListener("updateend", () => {
            this.tick_append()
        })
        this.source_buffer.addEventListener("error", e => {
            console.error("sourcebuffer error", e);
        })
    }
    async update(target: number) {
        console.log(`update track ${this.track_index}`, target, this.loaded, this.loading);
        for (let i = 0; i < this.metadata.segments.length; i++) {
            const segment = this.metadata.segments[i];
            if (segment.start >= target - 1 && segment.end < target + TARGET_BUFFER_DURATION) {
                if (!this.loaded.has(i) && !this.loading.has(i)) {
                    this.loading.add(i)
                    this.load(i)
                }
            }
        }
    }
    async load(index: number) {
        const res = await fetch(`/n/${encodeURIComponent(this.node_id)}/stream?format=hlsseg&tracks=${this.track_index}&index=${index}`)
        if (!res.ok) throw new Error(`segment fail i=${index} t=${this.track_index}`);
        const buf = await res.arrayBuffer()

        this.loading.delete(index)
        this.loaded.add(index)
        await new Promise<void>(cb => {
            this.append_queue.push({ buf, ...this.metadata.segments[index], cb })
            this.tick_append()
        })
    }
    tick_append() {
        console.log("tick", this.append_queue);
        if (this.source_buffer.updating) return
        if (this.append_queue.length) {
            const seg = this.append_queue[0];
            console.log("append", this.track_index, seg);
            this.source_buffer.timestampOffset = seg.start
            this.source_buffer.appendBuffer(seg.buf);
            this.append_queue.splice(0, 1)
        }
    }
}