aboutsummaryrefslogtreecommitdiff
path: root/web/script/player/profiles.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/script/player/profiles.ts')
-rw-r--r--web/script/player/profiles.ts56
1 files changed, 23 insertions, 33 deletions
diff --git a/web/script/player/profiles.ts b/web/script/player/profiles.ts
index 9284ec5..ec82a6d 100644
--- a/web/script/player/profiles.ts
+++ b/web/script/player/profiles.ts
@@ -4,67 +4,57 @@
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
import { OVar } from "../jshelper/mod.ts";
-import { EncodingProfile, JhlsMetadata } from "./jhls.d.ts";
+import { EncodingProfile } from "./jhls.d.ts";
import { profile_to_partial_track, test_media_capability } from "./mediacaps.ts";
import { Player } from "./player.ts";
+import { PlayerTrack } from "./track.ts";
const PROFILE_UP_FAC = 0.6
const PROFILE_DOWN_FAC = 0.8
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 }>()
+ profiles: EncodingProfileExt[] = []
+ is_init = false
- constructor(private player: Player, private bandwidth: OVar<number>, private metadata: JhlsMetadata) {
+ constructor(
+ private player: Player,
+ private track: PlayerTrack,
+ private bandwidth: OVar<number>
+ ) {
}
async init() {
- for (let id = 0; id < this.metadata.extra_profiles.length; id++) {
- const p = this.metadata.extra_profiles[id];
+ for (let id = 0; id < this.track.index.extra_profiles.length; id++) {
+ const p = this.track.index.extra_profiles[id];
if (!await test_media_capability(profile_to_partial_track(p))) continue
- 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.push({ id, order: 0, ...p })
}
- this.profiles_audio.sort((a, b) => profile_byterate(b) - profile_byterate(a))
- this.profiles_video.sort((a, b) => profile_byterate(b) - profile_byterate(a))
- this.profiles_subtitles.sort((a, b) => profile_byterate(b) - profile_byterate(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 []
+ this.profiles.sort((a, b) => profile_byterate(b) - profile_byterate(a))
+ for (let i = 0; i < this.profiles.length; i++) this.profiles[i].order = i
}
async remux_supported(track: number): Promise<boolean> {
- return await test_media_capability(this.metadata.tracks[track].info)
+ return await test_media_capability(this.player.tracks![track])
}
async select_optimal_profile(track: number, profile: OVar<EncodingProfileExt | undefined>) {
- const profs = this.profile_list_for_track(track)
+ if (!this.is_init) await this.init(), this.is_init = true;
const sup_remux = await this.remux_supported(track);
- if (!sup_remux && !profs.length) return this.player.logger?.log("None of the available codecs are supported. The Media can't be played back.")
+ if (!sup_remux && !this.profiles.length) return this.player.logger?.log("None of the available codecs are supported. The Media can't be played back.")
const min_prof = sup_remux ? -1 : 0
const co = profile.value?.order ?? min_prof
// TODO use actual bitrate as a fallback. the server should supply it.
- const current_bitrate = profile_byterate(profs[co], 500 * 1000)
- const next_bitrate = profile_byterate(profs[co - 1], 500 * 1000)
+ const current_bitrate = profile_byterate(this.profiles[co], 500 * 1000)
+ const next_bitrate = profile_byterate(this.profiles[co - 1], 500 * 1000)
// console.log({ current_bitrate, next_bitrate, co, bandwidth: this.bandwidth.value * 8 });
- if (!sup_remux && !profile.value) profile.value = profs[co];
- if (current_bitrate > this.bandwidth.value * PROFILE_DOWN_FAC && co + 1 < profs.length) {
+ if (!sup_remux && !profile.value) profile.value = this.profiles[co];
+ if (current_bitrate > this.bandwidth.value * PROFILE_DOWN_FAC && co + 1 < this.profiles.length) {
console.log("profile up");
- profile.value = profs[co + 1]
+ profile.value = this.profiles[co + 1]
this.log_change(track, profile.value)
}
if (next_bitrate < this.bandwidth.value * PROFILE_UP_FAC && co > min_prof) {
console.log("profile down");
- profile.value = profs[co - 1]
+ profile.value = this.profiles[co - 1]
this.log_change(track, profile.value)
}