fix(watch): tell a page's media choices in the order the page loaded them

The App listed a webpage's playlists, manifests and files by their random
ids, so the order a person saw meant nothing and no rule could pick among
them. A player asks for its multivariant playlist before the playlists and
segments that playlist names, so load order puts the complete source first.
Choices now keep the page's own order: the player's address, then the order
of loading. A test holds it with names that neither the alphabet nor an id
would sort this way.
This commit is contained in:
AAA committed 2026-11-28 22:41:23 +08:00
1 parent b07b4e3886
commit 3378bca278
1 file changed
+48 -15
@@ -27,6 +27,8 @@ pub struct Choice {
mime_type: String,
direct: bool,
indexed: bool,
/// Its place in the page's own order: the player's address, then the order of loading.
order: usize,
}
type ResponseFact = (
Format,
@@ -257,11 +259,12 @@ impl Media {
value
}
fn project_sources(&mut self, frame: Uuid, current: &str) -> Value {
let mut found: BTreeMap<String, (Format, Option<u64>, String, bool, bool)> =
BTreeMap::new();
// A player asks for its multivariant playlist before the playlists and segments that
// playlist names, so the order of loading puts the complete source first.
let mut found: Vec<(String, (Format, Option<u64>, String, bool, bool))> = Vec::new();
if let Some((format, size, mime, _)) = self.response(current) {
if format == Format::Hls || format == Format::Dash || size.is_some() {
found.insert(current.to_owned(), (format, size, mime, true, false));
found.push((current.to_owned(), (format, size, mime, true, false)));
}
}
if let Some(frame) = self.frames.get(&frame) {
@@ -271,26 +274,28 @@ impl Media {
let indexed = format == Format::File
&& size.is_some_and(|size| size > 0 && size <= 9_007_199_254_740_991);
if direct || indexed {
let entry = found
.entry(url.clone())
.or_insert((format, size, mime, false, false));
entry.3 |= direct;
entry.4 |= indexed;
let index = match found.iter().position(|(known, _)| known == url) {
Some(index) => index,
None => {
found.push((url.clone(), (format, size, mime, false, false)));
found.len() - 1
}
};
found[index].1 .3 |= direct;
found[index].1 .4 |= indexed;
}
}
}
}
self.choices
.retain(|_, choice| found.contains_key(&choice.url));
.retain(|_, choice| found.iter().any(|(url, _)| *url == choice.url));
for choice in self.choices.values_mut() {
choice.direct = false;
choice.indexed = false;
}
let mut direct_count = 0;
let mut indexed_count = 0;
let mut found: Vec<_> = found.into_iter().collect();
found.sort_by_key(|(url, _)| url != current);
for (url, (format, size, mime_type, direct, indexed)) in found {
for (order, (url, (format, size, mime_type, direct, indexed))) in found.into_iter().enumerate() {
let direct = direct && direct_count < 32;
let indexed = indexed && indexed_count < 32;
if !direct && !indexed {
@@ -318,11 +323,14 @@ impl Media {
request,
direct,
indexed,
order,
},
);
}
self.choices
.retain(|_, choice| choice.direct || choice.indexed);
let mut ordered: Vec<&Choice> = self.choices.values().collect();
ordered.sort_by_key(|choice| choice.order);
let name = |choice: &Choice| {
url::Url::parse(&choice.url)
.ok()
@@ -344,9 +352,9 @@ impl Media {
.unwrap_or_default()
};
json!({
"choices": self.choices.values().filter(|choice| choice.direct && choice.format != Format::Dash).map(|c|json!({"id":c.id,"displayURL":c.url,"name":name(c),"format":c.format})).collect::<Vec<_>>(),
"mpdCandidates":self.choices.values().filter(|c| c.format == Format::Dash).map(|c|json!({"id":c.id,"name":name(c),"hostname":hostname(c)})).collect::<Vec<_>>(),
"indexedCandidates": self.choices.values().filter(|choice| choice.indexed).map(|c|json!({"id":c.id,"name":name(c),"hostname":hostname(c),"mimeType":c.mime_type,"totalSize":c.size})).collect::<Vec<_>>()
"choices": ordered.iter().filter(|choice| choice.direct && choice.format != Format::Dash).map(|c|json!({"id":c.id,"displayURL":c.url,"name":name(c),"format":c.format})).collect::<Vec<_>>(),
"mpdCandidates": ordered.iter().filter(|c| c.format == Format::Dash).map(|c|json!({"id":c.id,"name":name(c),"hostname":hostname(c)})).collect::<Vec<_>>(),
"indexedCandidates": ordered.iter().filter(|choice| choice.indexed).map(|c|json!({"id":c.id,"name":name(c),"hostname":hostname(c),"mimeType":c.mime_type,"totalSize":c.size})).collect::<Vec<_>>()
})
}
}
@@ -619,6 +627,31 @@ mod tests {
assert_eq!(response_format("text/html", uri), None);
}
#[test]
fn choices_keep_the_order_the_page_loaded_them_after_its_own_address() {
let mut media = Media::default();
let frame = Uuid::new_v4();
// Named so that neither the alphabet nor a random id gives this order.
let urls = [
"https://media.test/z-master.m3u8",
"https://media.test/m-video-720.m3u8",
"https://media.test/a-audio.m3u8",
];
media.frame(frame, vec![], urls.iter().map(|url| (*url).into()).collect());
for url in urls {
media.observe_response(url.into(), (Format::Hls, Some(116), "application/vnd.apple.mpegurl".into(), None));
}
let listed = |sources: &Value| -> Vec<String> {
sources["choices"].as_array().unwrap().iter().map(|choice| choice["displayURL"].as_str().unwrap().to_owned()).collect()
};
for _ in 0..3 {
assert_eq!(listed(&media.sources(frame, "blob:owned")), urls);
}
let own = "https://media.test/film.mp4";
media.observe_response(own.into(), (Format::File, Some(4096), "video/mp4".into(), None));
assert_eq!(listed(&media.sources(frame, own)), [own, urls[0], urls[1], urls[2]]);
}
#[test]
fn indexed_sources_are_selected_frame_files_without_expanding_direct_choices() {
let mut media = Media::default();