move level filtering to manager

This commit is contained in:
mertalev
2026-05-21 16:50:15 -04:00
parent f40080920e
commit 0ec540190b
2 changed files with 19 additions and 16 deletions
@@ -158,22 +158,7 @@
activeSession = { assetId, id };
}
const decodingInfo = await Promise.all(api.levels.map((level) => mediaCapabilitiesManager.decodingInfo(level)));
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const lowestBitrateByHeight = new Map<number, number>();
for (let i = 0; i < api.levels.length; i++) {
if (!decodingInfo[i].powerEfficient) {
continue;
}
const { bitrate, height } = api.levels[i];
const cur = lowestBitrateByHeight.get(height);
if (cur === undefined || bitrate < api.levels[cur].bitrate) {
lowestBitrateByHeight.set(height, i);
}
}
const keep = new Set(lowestBitrateByHeight.values());
const keep = await mediaCapabilitiesManager.efficientLevels(api.levels);
for (let i = api.levels.length - 1; i >= 0; i--) {
if (!keep.has(i)) {
api.removeLevel(i);
@@ -34,6 +34,24 @@ class MediaCapabilitiesManager {
}
}
async efficientLevels(levels: Level[]) {
const decodingInfo = await Promise.all(levels.map((level) => this.decodingInfo(level)));
const lowestBitrateByHeight = new Map<number, number>();
for (let i = 0; i < levels.length; i++) {
if (!decodingInfo[i].powerEfficient) {
continue;
}
const { bitrate, height } = levels[i];
const cur = lowestBitrateByHeight.get(height);
if (cur === undefined || bitrate < levels[cur].bitrate) {
lowestBitrateByHeight.set(height, i);
}
}
return new Set(lowestBitrateByHeight.values());
}
decodingInfo(level: Level) {
const key = this.cacheKey(level);
const existing = this.cache.get(key);