diff options
Diffstat (limited to 'web/script/player/track')
-rw-r--r-- | web/script/player/track/mse.ts | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/web/script/player/track/mse.ts b/web/script/player/track/mse.ts index 237b6f6..c69833b 100644 --- a/web/script/player/track/mse.ts +++ b/web/script/player/track/mse.ts @@ -3,7 +3,7 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin <metamuffin.org> */ -import { OVar } from "../../jshelper/mod.ts"; +import { OVar, show } from "../../jshelper/mod.ts"; import { test_media_capability, track_to_content_type } from "../mediacaps.ts"; import { BufferRange, Player } from "../player.ts"; import { PlayerTrack, AppendRange, TARGET_BUFFER_DURATION, MIN_BUFFER_DURATION } from "./mod.ts"; @@ -60,6 +60,7 @@ export class MSEPlayerTrack extends PlayerTrack { this.usable_formats.push({ container, format, format_index: i, usable_index: this.usable_formats.length }) } } + this.usable_formats.sort((a, b) => b.format.bitrate - a.format.bitrate) if (!this.usable_formats.length) return this.player.logger?.log("No availble format is supported by this device. The track can't be played back.") this.active_format.value = this.usable_formats[0] @@ -94,6 +95,24 @@ export class MSEPlayerTrack extends PlayerTrack { this.update(this.player.video.currentTime) } + choose_format() { + if (!this.active_format.value) return + const ai = this.active_format.value.usable_index + const current_br = this.active_format.value.format.bitrate + const avail_br = this.player.downloader.bandwidth_avail.value * 8 + const prev_format = this.active_format.value + if (ai < this.usable_formats.length - 1 && current_br > avail_br) { + this.active_format.value = this.usable_formats[ai + 1] + } + if (ai > 0 && avail_br > this.usable_formats[ai - 1].format.bitrate * 1.2) { + this.active_format.value = this.usable_formats[ai - 1] + } + if (prev_format != this.active_format.value) { + console.log(`${show.metric(prev_format.format.bitrate)} -> ${this.active_format.value.format.bitrate}`); + this.choose_format() + } + } + update_buf_ranges() { if (!this.index) return; const ranges: BufferRange[] = []; @@ -136,6 +155,7 @@ export class MSEPlayerTrack extends PlayerTrack { } async load(index: number) { + this.choose_format() this.loading.add(index); // TODO update format selection const url = `${this.base_url}?fragment&segment=${this.segment_index}&track=${this.track_index}&format=${this.active_format.value!.format_index}&index=${index}&container=${this.active_format.value!.container}`; |