aboutsummaryrefslogtreecommitdiff
path: root/web/script/player/profiles.ts
blob: b4de534f51a54b7e69857f7a7bde6f0cd8ed45cb (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
import { OVar } from "../jshelper/mod.ts";
import { EncodingProfile, JhlsMetadata } from "./jhls.d.ts";

export interface EncodingProfileExt extends EncodingProfile { id: number, order: number }
export class ProfileSelector {
    profiles_video: EncodingProfileExt[] = []
    profiles_audio: EncodingProfileExt[] = []
    profiles_subtitles: EncodingProfileExt[] = []
    remux_bandwidth = new Map<number, { size: number, duration: number }>()

    constructor(private bandwidth: OVar<number>, private metadata: JhlsMetadata) {
        for (let id = 0; id < metadata.extra_profiles.length; id++) {
            const p = metadata.extra_profiles[id];
            if (p.audio) this.profiles_audio.push({ id, order: 0, ...p })
            if (p.video) this.profiles_video.push({ id, order: 0, ...p })
            if (p.subtitles) this.profiles_subtitles.push({ id, order: 0, ...p })
        }
        this.profiles_audio.sort((a, b) => profile_bw(b) - profile_bw(a))
        this.profiles_video.sort((a, b) => profile_bw(b) - profile_bw(a))
        this.profiles_subtitles.sort((a, b) => profile_bw(b) - profile_bw(a))
        for (let i = 0; i < this.profiles_audio.length; i++) this.profiles_audio[i].order = i
        for (let i = 0; i < this.profiles_video.length; i++) this.profiles_video[i].order = i
        for (let i = 0; i < this.profiles_subtitles.length; i++) this.profiles_subtitles[i].order = i
    }
    profile_list_for_track(track: number): EncodingProfileExt[] {
        const i = this.metadata.tracks[track].info.kind
        if (i.audio) return this.profiles_audio
        if (i.video) return this.profiles_video
        if (i.subtitles) return this.profiles_subtitles
        return []
    }
    select_optimal_profile(track: number, profile: OVar<EncodingProfileExt | undefined>) {
        if (this.bandwidth.value < 5000 * 1000) {
            profile.value = this.profile_list_for_track(track)[0]
        } else {
            profile.value = undefined
        }
    }
}

function profile_bw(p: EncodingProfile): number {
    if (p.audio) return p.audio.bitrate / 8
    if (p.video) return p.video.bitrate / 8
    if (p.subtitles) return 100
    return 0
}