aboutsummaryrefslogtreecommitdiff
path: root/web/script/player/mod.ts
blob: fa83a2b150dc47c53d2dbd37d7e271698e499ba3 (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
/*
    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) 2024 metamuffin <metamuffin.org>
*/
/// <reference lib="dom" />
import { OVar, show } from "../jshelper/mod.ts";
import { e } from "../jshelper/mod.ts";
import { Logger } from "../jshelper/src/log.ts";
import { EncodingProfile } from "./jhls.d.ts";
import { TrackKind, get_track_kind } from "./mediacaps.ts";
import { Player } from "./player.ts";
import { Popup } from "./popup.ts";
import { Playersync, playersync_controls } from "./sync.ts"
import { MSEPlayerTrack } from "./track/mse.ts";

globalThis.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()
        globalThis.dispatchEvent(new Event("navigationrequiresreload"))
        initialize_player(main, node_id)
    }
})

const MEDIA_KIND_ICONS: { [key in TrackKind]: [string, string] } = {
    video: ["tv_off", "tv"],
    audio: ["volume_off", "volume_up"],
    subtitles: ["subtitles_off", "subtitles"],
}

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

    const logger = new Logger<string>(s => e("p", s))
    const player = new Player(node_id, logger)
    const show_stats = new OVar(false);
    const idle_inhibit = new OVar(false)
    const sync_state = new OVar<Playersync | undefined>(undefined)

    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 popups = e("div")

    const track_select = (kind: TrackKind) => {
        const button = e("div", player.active_tracks.map(_ => {
            const active = player.active_tracks.value.filter(
                ts => get_track_kind(player.tracks![ts.track_index].kind) == kind)
            const enabled = active.length > 0
            return e("button", MEDIA_KIND_ICONS[kind][+enabled], {
                class: "icon",
                onclick: () => {
                    if (enabled) {
                        for (const t of active) {
                            player.set_track_enabled(t.track_index, false)
                        }
                    } else {
                        const all_kind = (player.tracks ?? [])
                            .map((track, index) => ({ index, track }))
                            .filter(({ track }) => get_track_kind(track.kind) == kind)
                        if (all_kind.length < 1) return
                        player.set_track_enabled(all_kind[0].index, true)
                    }

                }
            })
        }))
        const volume = () => {
            const slider = e("input")
            slider.type = "range"
            slider.min = "0"
            slider.max = "1"
            slider.step = "any"
            slider.valueAsNumber = player.video.volume
            slider.onchange = () => player.video.volume = slider.valueAsNumber
            slider.onmousemove = () => player.video.volume = slider.valueAsNumber
            return [e("div", { class: "jsp-controlgroup" }, e("label", "Volume", slider))]
        }

        new Popup(button, popups, () =>
            e("div", { class: "jsp-track-select-popup" },
                e("h2", `${kind[0].toUpperCase()}${kind.substring(1)}`),

                ...(kind == "audio" ? volume() : []),

                player.active_tracks.map(_ =>
                    e("ul", { class: "jsp-track-list" }, ...(player.tracks ?? [])
                        .map((track, index) => ({ index, track }))
                        .filter(({ track }) => get_track_kind(track.kind) == kind)
                        .map(({ track, index }): HTMLElement => {
                            const active = player.active_tracks.value.find(ts => ts.track_index == index) !== undefined
                            const onclick = () => {
                                player.set_track_enabled(index, !active)
                                // TODO show loading indicator
                            }
                            return e("li", { class: active ? ["active"] : [] },
                                e("button", { class: "jsp-track-state", onclick }, active ? "-" : "+"), " ",
                                e("span", { class: "jsp-track-name" }, `"${track.name}"`), " ",
                                e("span", { class: "jsp-track-lang" }, `(${track.language})`)
                            )
                        })
                    ))
            )
        )
        return button
    }

    const settings_popup = () => {
        const button = e("button", "settings", { class: "icon" })
        new Popup(button, popups, () => e("div", { class: "jsp-settings-popup" },
            e("h2", "Settings"),
            playersync_controls(sync_state, player),
            e("button", "Launch Native Player", {
                onclick: () => {
                    window.location.href = `jellynative://player-fullscreen/${window.location.protocol}//${window.location.host}/n/${encodeURIComponent(node_id)}/stream?format=hlsmaster`
                }
            })
        ))
        return button;
    }

    const controls = e("div", { class: "jsp-controls" },
        player.playing.map(playing =>
            e("button", { class: "icon" }, playing ? "pause" : "play_arrow", { onclick: toggle_playing })
        ),
        e("p", { class: "jsp-status" },
            player.position.map(v => e("span", show.duration(v))), e("br"),
            player.position.map(v => e("span", show.duration(v - player.duration.value)))
        ),
        pri = e("div", { class: "jsp-pri" },
            pri_current = e("div", { class: "jsp-pri-current" }),
            player.active_tracks.map(
                tracks => e("div", ...tracks.map((t, i) => t.buffered.map(
                    ranges => e("div", ...ranges.map(
                        r => e("div", {
                            class: ["jsp-pri-buffer", `jsp-pri-buffer-${r.status}`],
                            style: {
                                width: pri_map(r.end - r.start),
                                height: `calc(var(--csize)/${tracks.length})`,
                                top: `calc(var(--csize)/${tracks.length}*${i})`,
                                left: pri_map(r.start)
                            }
                        })
                    ))
                )))
            )
        ),
        e("div", { class: "jsp-track-select" },
            track_select("video"),
            track_select("audio"),
            track_select("subtitles")
        ),
        settings_popup(),
        e("button", "fullscreen", {
            class: "icon",
            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,
        show_stats.map(do_show => e("div", player.active_tracks.map(tracks =>
            !do_show ? e("div") : e("div", { class: "jsp-stats" },
                player.downloader.bandwidth.map(b => e("pre", `estimated bandwidth: ${show.metric(b, "B/s")} | ${show.metric(b * 8, "b/s")}`)),
                ...tracks.map((t, i) => t instanceof MSEPlayerTrack ? t.profile.map(p =>
                    e("pre", `mse track ${i}: ` + (p ? `profile ${p.id} (${show_profile(p)})` : `remux`))
                ) : e("pre", `vtt track ${i}: native jvtt`))
            )
        ))),
        logger.element,
        popups,
        controls,
    )
    el.append(pel)

    controls.onmouseenter = () => idle_inhibit.value = true
    controls.onmouseleave = () => idle_inhibit.value = false
    mouse_idle(pel, 1000)
        .liftA2(idle_inhibit, (x, y) => x && !y)
        .onchangeinit(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.ctrlKey) return
        if (k.code == "Period") player.pause(), player.frame_forward()
        if (k.code == "Space") toggle_playing()
        else if (k.code == "KeyV") show_stats.value = !show_stats.value
        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()
    })
    send_player_progress(node_id, player)
}

let sent_watched = false;
function send_player_progress(node_id: string, player: Player) {
    let t = 0;
    setInterval(() => {
        const nt = player.video.currentTime
        if (t != nt) {
            t = nt
            const start = nt < 1 * 60
            const end = nt > player.duration.value - 5 * 60

            if (!start) fetch(`/n/${encodeURIComponent(node_id)}/progress?t=${nt}`, { method: "POST" })
            if (end && !sent_watched) {
                fetch(`/n/${encodeURIComponent(node_id)}/watched?state=watched`, { method: "POST" })
                sent_watched = true;
            }
        }
    }, 10000)
}

function mouse_idle(e: HTMLElement, timeout: number): OVar<boolean> {
    let ct: number;
    const idle = new OVar(false)
    // e.onmouseleave = () => { 
    //     clearTimeout(ct)
    // }
    e.onmousemove = () => {
        clearTimeout(ct)
        if (idle) {
            idle.value = false
        }
        ct = setTimeout(() => {
            idle.value = true
        }, timeout)
    }
    return idle
}

function show_profile(profile: EncodingProfile): string {
    if (profile.audio) return `codec=${profile.audio.codec} br=${show.metric(profile.audio.bitrate, "b/s")}${profile.audio.sample_rate ? ` sr=${show.metric(profile.audio.sample_rate, "Hz")}` : ""}`
    if (profile.video) return `codec=${profile.video.codec} br=${show.metric(profile.video.bitrate, "b/s")} w=${profile.video.width} preset=${profile.video.preset}`
    if (profile.subtitles) return `codec=${profile.subtitles.codec}`
    return `???`
}