From de6a21c0c2b22cf587914fe8c183a42ae37f6ca3 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sat, 14 Nov 2026 09:37:29 +0300 Subject: [PATCH 1/8] Machine shape: the mutation matrix re-checks only what each mutation changes (#1388) ci/test_check_machine_shape.py ran as one 316 s process in a 330 s host python/shell/C gates job, so it alone set the job's wall. A profile showed repeated work, not hard work: - readonly_dependencies._tokens sliced text[i:] at every probe (quadratic) and ran per crate source twice per certificate; it now matches at the cursor, and the crate token check is memoized on the file text. - machine_readonly.builtin_macro rescanned the whole file for macro_rules and use items at each of 77k macro calls; a read-only Tokens list carries a one-pass Shadows index instead. The certificate is verified once (certify is provenance() is not None), and paths resolve once per proof. - check-machine-shape.py lexed the same source three ways for each of 140 checks; rust_tokens, production_tokens and lexed() memoize on source text (never on path), whole-list facts are computed once per list, and the machine-body scan walks only the machine's impl tokens. Every mutation is still applied and rejected by the real checker (owned-field matrix: 10 machines, 101 fields, 707 rejected writers), check-machine-shape.py's output is byte-identical, and a golden token stream from the old tokenizer plus SharedTokens/Shadows tests pin the rewrites. Locally the step went from 96.8 s to 10.2 s, the checker from 9.8 s to 1.5 s and test_transition_arguments from 20.0 s to 2.4 s. Co-Authored-By: Claude Opus 5.5 --- ci/check-machine-shape.py | 87 ++++++++++++++++------------- ci/machine_readonly.py | 96 ++++++++++++++++++++++++++------ ci/readonly_dependencies.py | 33 ++++++++--- ci/test_machine_readonly.py | 26 +++++++++ ci/test_readonly_dependencies.py | 14 +++++ 5 files changed, 193 insertions(+), 63 deletions(-) diff --git a/ci/check-machine-shape.py b/ci/check-machine-shape.py index cd38c9440d..42a4972813 100755 --- a/ci/check-machine-shape.py +++ b/ci/check-machine-shape.py @@ -4,10 +4,11 @@ Every listed machine has a validated strict writer binding and an enum policy. Bool/Option counts remain report-only. See machines.ini for the conservative proof limits; Clippy classifies enums. """ +from functools import lru_cache from pathlib import Path import configparser import sys -from machine_readonly import macro_invocation +from machine_readonly import Tokens, derived, lexed, macro_invocation, shadows_of sys.path.insert(0, str(Path(__file__).resolve().parent)) import rust_test_modules as rtm # noqa: E402 @@ -21,7 +22,12 @@ def struct_fields(source, name): """[(is_pub, field name or index, type tokens)] of the braced or tuple struct `name`, or None when the source declares no such struct. Comments and strings are gone after lexing.""" - tokens = [t for t in source if t != ''] if isinstance(source, list) else rust_tokens(source) + tokens = source if isinstance(source, list) else rust_tokens(source) + return derived(tokens, ('struct_fields', name), lambda t: _struct_fields(t, name)) + + +def _struct_fields(source, name): + tokens = [t for t in source if t != ''] start = next((i for i in range(len(tokens) - 1) if tokens[i] == 'struct' and tokens[i + 1] == name), None) if start is None: return None body, angle = start + 2, 0 @@ -61,9 +67,17 @@ def shape(index, tokens): return is_pub, str(index), tokens # a tuple struct's field +# The lexing entry points are pure functions of the source TEXT, and one source is lexed over and over +# (every machine's analysis re-reads its production closure and every module a type resolves through, +# and the registry's mutation matrix re-checks each machine dozens of times with one file changed). +# They memoize on that text, never on a path or an mtime, and hand out one shared read-only `Tokens`. +@lru_cache(maxsize=64) def rust_tokens(source): """Code tokens with raw identifiers normalized; literals cannot join adjacent code.""" - raw = [text if kind == 'code' else '' for kind, text in rtm.lex(source)] + # Interned: a lexed file is mostly a few hundred distinct words repeated, and the memo below keeps + # many of these lists alive at once. + raw = [sys.intern(text) if kind == 'code' else '' for kind, text in rtm.lex(source)] + if not any(raw[i] == 'r' and raw[i + 1] == '#' for i in range(len(raw) - 2)): return Tokens(raw) out, i = [], 0 while i < len(raw): if raw[i:i + 2] == ['r', '#'] and i + 2 < len(raw): @@ -73,16 +87,17 @@ def rust_tokens(source): out.append(identifier); i += 3 else: out.append(raw[i]); i += 1 - return out + return Tokens(out) def ends_of(tokens): - return rtm.group_ends([('code', token) for token in tokens]) + return derived(tokens, 'ends', lambda t: rtm.group_ends([('code', token) for token in t])) +@lru_cache(maxsize=384) def production_tokens(source): """Erase items proven cfg(test)-only, keeping item boundaries.""" - tokens = rust_tokens(source) + tokens = list(rust_tokens(source)) ends = ends_of(tokens) i = 0 while i + 1 < len(tokens): @@ -110,11 +125,15 @@ def production_tokens(source): tokens[i:j] = [''] * (j - i) if j > i: tokens[j - 1] = boundary # retain an item boundary for the following production impl i = j - return [token for token in tokens if token != ''] + return Tokens(token for token in tokens if token != '') def item_bodies(tokens): """Return inherent impl and function spans; nested functions remain separate.""" + return derived(tokens, 'bodies', _item_bodies) + + +def _item_bodies(tokens): ends = ends_of(tokens) impls, functions = [], [] for i, token in enumerate(tokens): @@ -185,14 +204,8 @@ def builtin_macro(tokens, bang, macro): """Accept an unqualified builtin only when no local definition/import can shadow it.""" if bang < 1 or tokens[bang - 1] != macro: return False if bang >= 3 and tokens[bang - 3:bang - 1] == [':', ':']: return False - if any(tokens[j:j + 3] == ['macro_rules', '!', macro] for j in range(len(tokens) - 2)): - return False - for j, token in enumerate(tokens): - if token == 'use': - end = j + 1 - while end < len(tokens) and tokens[end] != ';': end += 1 - if macro in tokens[j + 1:end] or '*' in tokens[j + 1:end]: return False - return True + shadows = shadows_of(tokens) + return macro not in shadows.macro_rules and macro not in shadows.imported and '*' not in shadows.imported def readonly_match(tokens, bang, ends, field): @@ -217,6 +230,10 @@ def readonly_match(tokens, bang, ends, field): def copy_enum_types(tokens): + return derived(tokens, 'copy_enums', _copy_enum_types) + + +def _copy_enum_types(tokens): """Recognize derived-Copy fieldless enums; payloads and aliases stay conservative.""" ends = ends_of(tokens) safe = set() @@ -234,14 +251,8 @@ def copy_enum_types(tokens): enum_name = tokens[i + 1] declarations = sum(tokens[j] in ('enum', 'struct', 'type', 'union') and tokens[j + 1] == enum_name for j in range(len(tokens) - 1)) - imported = False - for j, token in enumerate(tokens): - if token == 'use': - end = j + 1 - while end < len(tokens) and tokens[end] != ';': end += 1 - if enum_name in tokens[j + 1:end] or '*' in tokens[j + 1:end]: - imported = True - break + shadows = shadows_of(tokens) + imported = enum_name in shadows.imported or '*' in shadows.imported if declarations == 1 and not imported and variants and all( t == ',' or (t.isidentifier() and t[0].isupper()) for t in variants): safe.add(enum_name) @@ -434,9 +445,10 @@ def leading(a, method): problems.append(f'{label}::{method}: consuming the machine must use an owner event') machine_methods = {n: [(a, b, sig) for a, b, m, sig in declared if m == n] for _, _, n, _ in declared} all_inert = proof is not None and substitutions is not None and all(proof.inert(state_path, [part for token in t for part in substitutions.get(token, [token])]) for _, _, t in fields) - def machine_body(i): return any(a < i < b and inherent_target(h[h.index('for') + 1:] if 'for' in h else h, name) for a, b, h in impls) - for i, token in enumerate(tokens): - if not machine_body(i): continue + machine_body = sorted({i for a, b, h in impls if inherent_target(h[h.index('for') + 1:] if 'for' in h else h, name) + for i in range(a + 1, b)}) + for i in machine_body: + token = tokens[i] if token == 'unsafe': problems.append(f'{label}: unsafe machine authority cannot be proved') if token == 'self' and tokens[i + 1:i + 2] == ['.'] and tokens[i + 3:i + 4] == ['(']: method = tokens[i + 2] @@ -468,7 +480,7 @@ def _field_writer_problems(source, name, field, transition, constructor, label, else: tokens, impls, functions, ends, contexts = analysis if analysis is None: contexts = {} owned = [(a, b) for a, b, h in impls if inherent_target(h, name)] - closures = closure_spans(tokens, ends) + closures = derived(tokens, 'closures', lambda t: closure_spans(t, ends_of(t))) def compute_context(index): if any(a <= index < b for a, b in closures): return None @@ -485,15 +497,15 @@ def context(index): problems = [] fields = struct_fields(tokens, name) or [] field_types = [typ for _, field_name, typ in fields if field_name == field] - plain_struct = any(tokens[j:j + 3] == ['struct', name, '{'] for j in range(len(tokens) - 2)) + plain_struct = derived(tokens, ('plain_struct', name), lambda t: any(t[j:j + 3] == ['struct', name, '{'] for j in range(len(t) - 2))) copy_field = (plain_struct and len(field_types) == 1 and len(field_types[0]) == 1 and field_types[0][0] in copy_enum_types(tokens)) - safe_matches = {i: span for i, token in enumerate(tokens) if token == '!' - and (span := readonly_match(tokens, i, ends, field)) is not None} + bangs = derived(tokens, 'bangs', lambda t: [i for i, token in enumerate(t) if token == '!']) + safe_matches = {i: span for i in bangs if (span := readonly_match(tokens, i, ends, field)) is not None} # These wrappers consume only the bool result, with no formatting expressions. safe_asserts = set() - for i, token in enumerate(tokens): - if token != '!' or i + 1 not in ends: continue + for i in bangs: + if i + 1 not in ends: continue if not any(builtin_macro(tokens, i, macro) for macro in ('assert', 'debug_assert')): continue inner = i + 3 if inner not in safe_matches or inner + 1 not in ends: continue @@ -521,8 +533,8 @@ def location(index): # Only a direct associated Self struct literal is an authorized constant constructor. constants = [] - for i in range(len(tokens) - 6): - if tokens[i:i + 6] != ['const', constructor, ':', 'Self', '=', 'Self']: + for i in derived(tokens, 'consts', lambda t: [j for j, token in enumerate(t) if token == 'const']): + if i >= len(tokens) - 6 or tokens[i:i + 6] != ['const', constructor, ':', 'Self', '=', 'Self']: continue brace = i + 6 if brace not in ends or tokens[brace] != '{' or tokens[ends[brace] + 1:ends[brace] + 2] != [';']: @@ -658,8 +670,7 @@ def owned_take(index, owner): for method in ('replace', 'take'): expected = ['std', ':', ':', 'mem', ':', ':', method, '(', '&', 'mut', 'self', '.'] if tokens[max(0, index - len(expected)):index] == expected: - if any(tokens[j:j + 2] in (['mod', 'std'], ['as', 'std']) for j in range(len(tokens) - 1)): return False - return True + return not shadows_of(tokens).std_named return False def owned_bool_match(index, owner): @@ -795,9 +806,7 @@ def production_sources(path, root): raise ValueError(f'production include escapes rust-modules: {current}') if current in seen: continue seen.add(current) - source = current.read_text(encoding='utf-8') - tokens = rtm.lex(source) - ends = rtm.group_ends(tokens) + tokens, ends = lexed(current.read_text(encoding='utf-8')) module_dir = current.parent if current.name in ('mod.rs', 'lib.rs', 'main.rs') else current.with_suffix('') def walk(start, stop, directory, attribute_base, inherited_test=False): i, attrs = start, [] diff --git a/ci/machine_readonly.py b/ci/machine_readonly.py index ac53f0d9e4..e5896234e7 100644 --- a/ci/machine_readonly.py +++ b/ci/machine_readonly.py @@ -4,6 +4,7 @@ module imports; opaque/ambiguous types, callbacks, raw pointers and interior authority fail closed. Safe Rust then prevents an &self method from changing the proved owned state. """ +from functools import lru_cache from pathlib import Path import re @@ -28,6 +29,58 @@ def cached(self, *args, **kwargs): return self.memo[key] return cached +class Tokens(list): + """A token list that is built once and never changed, so one lexed source can be shared and can + remember what is derived from it (`derived`) instead of every consumer recomputing it. + + Every mutator raises: a caller that needs a changeable copy writes `list(tokens)`.""" + def __init__(self, tokens=()): + super().__init__(tokens) + self.derived = {} + + def _frozen(self, *args, **kwargs): + raise TypeError('Tokens are shared and read-only; copy with list(tokens)') + __setitem__ = __delitem__ = __iadd__ = __imul__ = _frozen + append = extend = insert = pop = remove = clear = reverse = sort = _frozen + + +def derived(tokens, key, compute): + """compute(tokens), remembered on a shared `Tokens` list; a plain list is computed every time.""" + if not isinstance(tokens, Tokens): return compute(tokens) + if key not in tokens.derived: tokens.derived[key] = compute(tokens) + return tokens.derived[key] + + +class Shadows: + """What in one token list can shadow a builtin macro or the `std` crate: its macro definitions, + every token of its `use` items and whether `mod std` / `as std` appear. One pass over the list + answers what a rescan at every macro call used to.""" + def __init__(self, tokens): + self.macro_rules, self.macro_items, self.imported, self.std_named = set(), set(), set(), False + count = len(tokens) + for j, token in enumerate(tokens): + if token == 'macro_rules' and j + 2 < count and tokens[j + 1] == '!': self.macro_rules.add(tokens[j + 2]) + elif token == 'macro' and j + 2 < count: self.macro_items.add(tokens[j + 1]) + elif token == 'use': + end = j + 1 + while end < count and tokens[end] != ';': end += 1 + self.imported.update(tokens[j + 1:end]) + elif token in ('mod', 'as') and j + 1 < count and tokens[j + 1] == 'std': self.std_named = True + + +def shadows_of(tokens): + return derived(tokens, 'shadows', Shadows) + + +@lru_cache(maxsize=64) +def lexed(source): + """(raw lexer tokens, their bracket ends) of one source TEXT, shared and never changed: the module + walk and each child lookup read the same file's text once per check, so this lexes it once.""" + import rust_test_modules as rtm + tokens = rtm.lex(source) + return Tokens(tokens), rtm.group_ends(tokens) + + def macro_invocation(tokens, bang, ends): """A bang after an identifier is a macro, unlike logical negation after if/return.""" return (bang > 0 and bang + 1 in ends and tokens[bang - 1].isidentifier() and @@ -61,6 +114,8 @@ class Proof: def __init__(self, root, shape): self.root, self.shape = Path(root), shape self.cache, self.visiting, self.audit = {}, set(), set() + self.real_paths = {} + self.real_root = self.real(self.root) self.crates = {} for manifest in self.root.glob('*/Cargo.toml'): text = manifest.read_text(encoding='utf-8') @@ -70,13 +125,22 @@ def __init__(self, root, shape): name = match[1].replace('-', '_') if match else '' if name: self.crates[name] = manifest.parent / 'src/lib.rs' + def real(self, path): + """Path(path).resolve(), looked up once per path for this proof's lifetime: the tree is not + changed while a proof is being made (module() already memoizes every file it reads).""" + if path not in self.real_paths: self.real_paths[path] = Path(path).resolve() + return self.real_paths[path] + def module(self, path): - path = Path(path).resolve() + path = self.real(path) if path in self.cache: return self.cache[path] from readonly_generated import declarations generated = declarations(self.root, path) - tokens = self.shape.production_tokens(path.read_text(encoding='utf-8') + '\n' + generated) - if generated: self.audit.add('source-backed generated data ' + path.relative_to(self.root.resolve()).as_posix()) + text = path.read_text(encoding='utf-8') + # With nothing generated the appended newline lexes to no token; leaving it off lets the module + # share the memoized tokens of the same text the checker's own walk already lexed. + tokens = self.shape.production_tokens(text + '\n' + generated if generated else text) + if generated: self.audit.add('source-backed generated data ' + path.relative_to(self.real_root).as_posix()) ends = self.shape.ends_of(tokens) reverse = {end: begin for begin, end in ends.items()} definitions, uses = {}, [] @@ -155,11 +219,11 @@ def field_type(index, item): def child(self, path, name): """Resolve one declared child, including a literal path attribute; conditional paths fail.""" import rust_test_modules as rtm - path = Path(path).resolve() - tokens = rtm.lex(path.read_text(encoding='utf-8')) - ends = rtm.group_ends(tokens) + path = self.real(path) + tokens, ends = lexed(path.read_text(encoding='utf-8')) reverse = {b: a for a, b in ends.items()} - matches = [i for i in range(len(tokens) - 2) if tokens[i:i + 3] == [('code', 'mod'), ('code', name), ('code', ';')]] + mods = derived(tokens, 'mods', lambda t: [i for i in range(len(t) - 2) if t[i] == ('code', 'mod')]) + matches = [i for i in mods if tokens[i:i + 3] == [('code', 'mod'), ('code', name), ('code', ';')]] if len(matches) != 1: return None before = matches[0] - 1 if tokens[before:before + 1] == [('code', 'pub')]: before -= 1 @@ -176,7 +240,7 @@ def child(self, path, name): before = begin - 2 directory = path.parent if path.name in ('lib.rs', 'mod.rs', 'main.rs') else path.with_suffix('') candidates = [path.parent / paths[0]] if len(paths) == 1 else [directory / (name + '.rs'), directory / name / 'mod.rs'] if not paths else [] - found = [p.resolve() for p in candidates if p.is_file() and p.resolve().is_relative_to(self.root.resolve())] + found = [p.resolve() for p in candidates if p.is_file() and p.resolve().is_relative_to(self.real_root)] if len(found) != 1: return None from readonly_generated import canonical_child if not canonical_child(self.root, path, name, found[0]): return None @@ -194,7 +258,7 @@ def parent(self, path): @cache_proof def resolve(self, path, names, seen=None): - path, names = Path(path).resolve(), tuple(names) + path, names = self.real(path), tuple(names) key = (path, names) seen = set() if seen is None else seen if key in seen: return None @@ -306,7 +370,7 @@ def inert(self, path, tokens, substitutions=None): # Cache only completed roots: recursive coinductive successes are provisional. substitutions = substitutions or {} concrete = tuple(part for token in tokens for part in substitutions.get(token, [token])) - key = (Path(path).resolve(), concrete) + key = (self.real(path), concrete) if self.visiting: return self._inert(path, list(concrete)) if not hasattr(self, 'inert_roots'): self.inert_roots = {} if key not in self.inert_roots: @@ -352,20 +416,20 @@ def _inert(self, path, tokens, substitutions=None): sub = dict(zip(parameters, args)) result = all(self.inert(owner, field, sub) for field in payloads) self.visiting.remove(key) - if result: self.audit.add(owner.relative_to(self.root.resolve()).as_posix() + '::' + name) + if result: self.audit.add(owner.relative_to(self.real_root).as_posix() + '::' + name) return result @cache_proof def dependency_certificate(self, path, tokens): """Cache exact dependency evidence only within this immutable source snapshot.""" try: - from readonly_dependencies import certify, provenance + from readonly_dependencies import provenance except ModuleNotFoundError as error: if error.name == 'readonly_dependencies': return None raise namespace = self.module(path) context = {'imports': namespace[3], 'definitions': list(namespace[2])} - if not certify(self.root, Path(path), tokens, namespace_context=context): return None + # `certify` is `provenance(...) is not None`: one verification, not two. return provenance(self.root, Path(path), tokens, namespace_context=context) def shared_method(self, path, state_type, method): @@ -445,14 +509,14 @@ def shared_field(self, state_path, state_type, view_path, view_type, field): def builtin(self, path, name, seen=None): - path = Path(path).resolve() + path = self.real(path) seen = set() if seen is None else seen if path in seen: return True seen = seen | {path} tokens, _, _, uses = self.module(path) if 'macro_use' in tokens: return False - if any(tokens[i:i + 3] == ['macro_rules', '!', name] or tokens[i:i + 2] == ['macro', name] for i in range(len(tokens) - 2)): - return False + shadows = shadows_of(tokens) + if name in shadows.macro_rules or name in shadows.macro_items: return False if any(alias == name for alias, _ in uses): return False for alias, target in uses: if alias != '*': continue diff --git a/ci/readonly_dependencies.py b/ci/readonly_dependencies.py index 73a1348a81..b0ec65d11a 100644 --- a/ci/readonly_dependencies.py +++ b/ci/readonly_dependencies.py @@ -9,6 +9,7 @@ Cargo honoring the checked-in locked graph/config, and no out-of-tree compiler or Cargo overrides. This cannot attest an arbitrary build environment or unsafe code. """ +from functools import lru_cache from pathlib import Path import argparse import hashlib @@ -51,8 +52,17 @@ def _json(data): return json.dumps(data, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode('ascii') +_RAW_STRING = re.compile(r'(?:b|c)?r(#+)?"') +_LITERAL = re.compile(r'(?:b|c)?"(?:\\.|[^"\\])*"|(?:b)?\'(?:\\.|[^\'\\])\'') +_TOKEN = re.compile(r'r#[A-Za-z_]\w*|[A-Za-z_]\w*|::|[^\s]') + + def _tokens(text): - """Lex comments/literals before examining Rust identifiers; never expand macros.""" + """Lex comments/literals before examining Rust identifiers; never expand macros. + + Each pattern matches AT the cursor (`pattern.match(text, i)`): slicing `text[i:]` per token + copied the rest of the file every time and made a 700 KB source cost seconds. + """ result, i = [], 0 while i < len(text): if text[i].isspace(): @@ -67,17 +77,17 @@ def _tokens(text): elif text.startswith('*/', i): depth -= 1; i += 2 else: i += 1 else: - raw = re.match(r'(?:b|c)?r(#+)?"', text[i:]) + raw = _RAW_STRING.match(text, i) if raw: end = '"' + (raw[1] or '') - stop = text.find(end, i + raw.end()) + stop = text.find(end, raw.end()) if stop < 0: raise ValueError('unclosed raw string') result.append('""'); i = stop + len(end); continue - literal = re.match(r'(?:b|c)?"(?:\\.|[^"\\])*"|(?:b)?\'(?:\\.|[^\'\\])\'', text[i:]) + literal = _LITERAL.match(text, i) if literal: - result.append(literal[0]); i += literal.end(); continue - token = re.match(r'r#[A-Za-z_]\w*|[A-Za-z_]\w*|::|[^\s]', text[i:]) - result.append(token[0].removeprefix('r#')); i += token.end() + result.append(literal[0]); i = literal.end(); continue + token = _TOKEN.match(text, i) + result.append(token[0].removeprefix('r#')); i = token.end() return result @@ -401,6 +411,13 @@ def _root_modules_only(text): return True +@lru_cache(maxsize=128) +def _crate_tokens(text): + """_tokens of one crate source TEXT, shared by every namespace check of the same file; the list is + only read (a changed file is new text, so a stale entry cannot answer for it).""" + return _tokens(text) + + def _namespace_safe(crate, source, context): if context is not None: if not isinstance(context, dict) or set(context) - {'imports', 'definitions'}: return False @@ -408,7 +425,7 @@ def _namespace_safe(crate, source, context): if any(entry[0] == 'serde_json' for entry in context.get('imports', ())): return False for path in sorted((crate / 'src').rglob('*.rs')): if path.is_symlink() or not path.resolve().is_relative_to(crate.resolve()): return False - tokens = _tokens(path.read_text(encoding='utf-8')) + tokens = _crate_tokens(path.read_text(encoding='utf-8')) for i, token in enumerate(tokens): if token == 'serde_json' and i and tokens[i - 1] in ('mod', 'type', 'struct', 'enum', 'union', 'trait', 'as', 'crate'): return False diff --git a/ci/test_machine_readonly.py b/ci/test_machine_readonly.py index c9776231e3..2e916ac678 100644 --- a/ci/test_machine_readonly.py +++ b/ci/test_machine_readonly.py @@ -215,4 +215,30 @@ def test_generated_opaque_layout_is_not_a_name_or_copy_certificate(self): self.assertTrue(verdict(source, INI, extra)) self.assertTrue(verdict(source, INI, extra)) + +class SharedTokens(unittest.TestCase): + """The lexed-once token lists the proof and the machine-shape gate share.""" + def test_a_shared_list_is_read_only_and_one_text_is_one_list(self): + import importlib.util + spec = importlib.util.spec_from_file_location('check_machine_shape', Path(__file__).with_name('check-machine-shape.py')) + gate = importlib.util.module_from_spec(spec) + spec.loader.exec_module(gate) + tokens = gate.production_tokens('struct A { x: u8 } #[cfg(test)] mod tests { fn t() {} }') + self.assertIs(gate.production_tokens('struct A { x: u8 } #[cfg(test)] mod tests { fn t() {} }'), tokens) + self.assertNotIn('tests', tokens) + for mutate in (lambda: tokens.append('x'), lambda: tokens.__setitem__(0, 'x'), lambda: tokens.pop(), lambda: tokens.sort()): + with self.assertRaises(TypeError): mutate() + self.assertEqual(list(tokens)[:3], ['struct', 'A', '{']) + + def test_shadows_are_exactly_what_a_rescan_would_find(self): + from machine_readonly import Shadows + tokens = ['macro_rules', '!', 'm', '{', '}', 'macro', 'n', 'use', 'a', ':', ':', '*', ';', 'use', 'b', ';', + 'mod', 'std', ';', 'macro_rules', '!'] + facts = Shadows(tokens) + self.assertEqual(facts.macro_rules, {'m'}) # the trailing `macro_rules !` has no name to take + self.assertEqual(facts.macro_items, {'n'}) + self.assertEqual(facts.imported, {'a', ':', '*', 'b'}) + self.assertTrue(facts.std_named) + self.assertFalse(Shadows(['use', 'std', ':', ':', 'mem', ';']).std_named) # importing std is not naming it as a module + if __name__ == '__main__': unittest.main() diff --git a/ci/test_readonly_dependencies.py b/ci/test_readonly_dependencies.py index 0c1340de3c..d3ec2b5fbe 100644 --- a/ci/test_readonly_dependencies.py +++ b/ci/test_readonly_dependencies.py @@ -62,6 +62,20 @@ def test_current_canonical_locked_json(self): self.assertEqual(evidence['owner_manifest'], 'plex/Cargo.toml') self.assertEqual(evidence['certificate_sha256'], proof.CERTIFICATE_SHA256) + def test_lexer_tokens_are_the_cursor_matched_stream(self): + # Golden stream from the slicing implementation (text[i:] per probe) that matching at the + # cursor replaced: nested comments, a lifetime, r#ident, char/byte/string literals, raw strings. + source = ("use serde_json::Value; /* a /* nested */ comment */ // line\n" + "fn f<'a>(x: &'a str) -> r#type { let c = 'x'; let b = b'\\n'; let s = \"a\\\"b\"; let raw = r#\"q\"#; " + "let rb = br##\"x\"##;\n mod serde_json; ::serde_json::Value }\n") + self.assertEqual(proof._tokens(source), [ + 'use', 'serde_json', '::', 'Value', ';', 'fn', 'f', '<', "'", 'a', '>', '(', 'x', ':', '&', "'", 'a', + 'str', ')', '-', '>', 'type', '{', 'let', 'c', '=', "'x'", ';', 'let', 'b', '=', "b'\\n'", ';', 'let', + 's', '=', '"a\\"b"', ';', 'let', 'raw', '=', '""', ';', 'let', 'rb', '=', '""', ';', 'mod', + 'serde_json', ';', '::', 'serde_json', '::', 'Value', '}']) + with self.assertRaises(ValueError): proof._tokens('/* /* one */') + with self.assertRaises(ValueError): proof._tokens('let r = r#"open') + def test_separate_colon_tokens(self): self.assertTrue(self.valid([':', ':', 'serde_json', ':', ':', 'Value'])) From 8859e4bad1838f154a90bbb5885fd48d928c7d72 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sat, 14 Nov 2026 14:18:43 +0300 Subject: [PATCH 2/8] Dolby Vision: clear active-area offsets that cannot describe the picture (#1391) A 1920x802 Dolby Vision Profile 8.1 re-encode of a letterboxed 3840x2160 master still carries the master's Level 5 active-area offsets (top 276, bottom 277). An LG C8 on webOS 4.4.3 blanks everything outside them and showed a 249-row strip. W18O libkadaptor's dolbyGenerateRegNLuts_IP writes those offsets into the DM's Active_area registers. packet_to_annexb now checks every HEVC Dolby Vision RPU it feeds, including the Profile 7 to 8.1 conversion's output, against the stream's displayed raster. Foreign offsets are zeroed (DoviRpu::crop, what dovi_tool --crop does) and the RPU is rewritten; every other RPU is fed as it was. A wrong keep cuts picture, while a wrong clear only shows the encode's own black bars, so the rule leans to clear: - a raster that is not 16:9 has been cropped, so any nonzero offsets are cleared; - a 16:9 raster keeps offsets that can describe it (inside the frame, an area between 1:2 and 3:1); - a per-stream latch clears every later RPU once one has been foreign. The state is a DvActiveArea owned by the demux loop. Host tests cover the reporter's case, cropped rasters, genuine letterboxes kept byte for byte, the latch, the P7 path and the bounds. They failed against the old behaviour. No TV has shown the fix: the dev set's firmware does not read L5 the same way. The URI engine and AV1 Profile 10 are not covered. docs/dolby-vision.md section 1a states the rule and its limits. Closes #1367 Co-Authored-By: Claude Opus 5.5 --- docs/dolby-vision.md | 50 ++++ rust-modules/play-feed/src/ff.rs | 143 ++++++++++- .../play-feed/src/ff_fixture_bytes.rs | 14 +- .../play-feed/src/ff_packet_framing_tests.rs | 241 +++++++++++++++++- 4 files changed, 441 insertions(+), 7 deletions(-) diff --git a/docs/dolby-vision.md b/docs/dolby-vision.md index fd30b1b6ad..25e7bd7dc5 100644 --- a/docs/dolby-vision.md +++ b/docs/dolby-vision.md @@ -106,6 +106,56 @@ the same Dolby Vision tail was measured on a webOS 6 set (LG C1, webOS 6.5.3, 20 AV1 Profile 10.1 file direct-played with the node put the panel in Dolby Vision and played through, where without it the same file showed HDR10. It is unmeasured on any other firmware, and the webOS 4.5 dev set never reaches it (no AV1 row in its codec table). +### 1a. Active-area offsets from another raster (#1367) + +**The bug class.** An RPU's Level 5 block is the picture's active area, as four offsets (`left`, +`right`, `top`, `bottom`), and the TV's Dolby Vision engine blanks everything outside it. A +re-encode cropped from a letterboxed master keeps the master's L5 in every RPU. The report: a +1920x802 Profile 8.1 file cut from a 3840x2160 master (`dovi_tool info -s`: `L5 offsets: top=276, +bottom=277, left=0, right=0`) played on an LG C8 (webOS 4.4.3, Feed) as a strip 249 rows high, +802 - 553. With Dolby Vision off it played whole: the RPU NALs are still fed (nothing drops +`UNSPEC62` on that path), but the pipeline is built without Dolby Vision, so nothing applies them +and the HDR10 base layer shows. + +**The rule** (`ff::dv_active_area_is_foreign`, per RPU, against the raster the demuxer reports: +`codecpar`, the displayed one, 1920x802 and not the coded 808). A wrong keep cuts real picture; a +wrong clear only shows the black bars the encode itself carries, which is what `dovi_tool --crop` +does. So it leans to clear: +1. All-zero offsets, or an unknown raster: nothing to do. +2. **A raster that is not 16:9** (`width * 9 != height * 16`) was cropped and has no bars for an + active area to describe: ANY nonzero offsets are cleared. This is what catches 1920x1038 (a 1.85:1 + master's `top=bottom=42` leaves a plausible 2.01:1 and would still blank 84 rows), 1920x960 + (`120 / 120`, 2.67:1), 3840x1600 and 4096x2160, which a test of the area's shape alone keeps. +3. **A 16:9 raster** keeps offsets that can describe it (`ff::dv_active_area_fits`): `left + right < + width`, `top + bottom < height`, and an area no wider than 3:1 (`DV_AREA_WIDEST`) and no narrower + than 1:2 (`DV_AREA_NARROWEST`); no released picture is wider than 2.76:1 (Ultra Panavision 70) or + narrower than 9:16 portrait. A genuine 1920x1080 letterbox (`top=138`, `bottom=139`, 2.39:1) and + a 3840x2160 master's own `276 / 277` are kept. Plus a per-stream **latch** + (`ff::DvActiveArea::foreign_seen`, owned by the demux loop): once one RPU of the stream has been + cleared, every later RPU with nonzero offsets is cleared too, which catches a variable-aspect 4K + master downscaled to 1080p, whose 1.90:1 scenes (`69 / 70`) look plausible on their own. + +**What is fed.** `ff::packet_to_annexb` checks every HEVC RPU of every profile, the Profile 7 +conversion's output included. When the rule fires, `ff::clear_foreign_active_area` zeroes the four +offsets (`DoviRpu::crop`) and writes the RPU again, every other field kept; the event log says +`ff: DV RPU #n active area ... cannot describe WxH; cleared`. Any other RPU is fed as it reached the +check, not re-serialised: the original bytes, or for a converted Profile 7 the conversion's output. +That covers offsets that are kept, no L5 block, an RPU that does not parse (a Profile 7 one is +still dropped, as before) and a raster the demuxer did not report. + +**Firmware evidence.** W18O `libkadaptor.so.2.0.1`, `dolbyGenerateRegNLuts_IP` at `0xe620c`, writes +the L5 offsets into the DM registers `Active_area_*`. + +**Limits.** Three, none verified: +- The URI engine, where the TV reads the file itself, is not covered: nothing of the file passes + through this app. Nor is AV1 Profile 10 on the Feed path, whose RPUs go through + `av1_packet_to_tu` and are not checked. +- A 16:9 picture whose first foreign offsets still leave a plausible area is kept: a 1080p downscale + of a 1.85:1 master that kept its 4K offsets (`top=bottom=42`, 1.93:1 left) passes and blanks 21 + rows of picture at each edge. Nothing in the file says the offsets are not its own. +- It is not verified on a device: our dev set's W19H `libkadaptor` has no `active_area` string, so + it cannot reproduce the strip, and the reporter's C8 is where the failure was seen. + --- ## 2. The Load payload: two nodes, both recovered from the set's own binaries diff --git a/rust-modules/play-feed/src/ff.rs b/rust-modules/play-feed/src/ff.rs index e8b95aa2ea..a97b75dcdc 100644 --- a/rust-modules/play-feed/src/ff.rs +++ b/rust-modules/play-feed/src/ff.rs @@ -2301,6 +2301,7 @@ static SEI_STRIPPED: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64 static DV_EL_DROPPED: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0); static DV_RPU_REWRITTEN: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0); static DV_RPU_FAILED: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0); +static DV_AREA_CLEARED: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0); /// HEVC `UNSPEC62`: the NAL type Dolby Vision carries its RPU (the dynamic metadata) in. const HEVC_NAL_DV_RPU: u8 = 62; @@ -2350,6 +2351,130 @@ pub fn rewrite_dv_rpu_p7_to_p81(nal: &[u8]) -> Option> { rpu.write_hevc_unspec62_nalu().ok() } +/// The widest picture an RPU's active area (Dolby Vision Level 5) may describe, as width : height. +/// No released picture is wider than 2.76:1 (Ultra Panavision 70), so a 3:1 ceiling keeps every +/// real one. Together with [`DV_AREA_NARROWEST`] it tells offsets that belong to this picture from +/// offsets authored for another raster, which is the only way an area outside the range can arise. +const DV_AREA_WIDEST: (u64, u64) = (3, 1); +/// The narrowest: 1:2. No released picture is narrower than 9:16 portrait (0.5625). +const DV_AREA_NARROWEST: (u64, u64) = (1, 2); +/// The one raster whose letterbox bars are part of the picture: 16:9, the frame a mastered +/// 1.85:1 or 2.39:1 film is laid in. A picture of any other shape was cropped, so it has no bars +/// for an active area to describe. +const DV_FULL_FRAME: (u64, u64) = (16, 9); + +/// PURE: can the Level 5 active-area offsets `l`, `r`, `t`, `b` describe a `w` x `h` picture? +/// +/// The TV's Dolby Vision engine blanks everything outside the active area. An encode cropped from +/// a letterboxed master keeps the master's offsets in its RPUs (#1367: `top=276 bottom=277`, a 4K +/// master's bars, on a 1920x802 file, leaves 249 rows). Offsets cannot describe the picture when +/// they leave nothing (`l + r >= w` or `t + b >= h`) or leave an area wider than +/// [`DV_AREA_WIDEST`] or narrower than [`DV_AREA_NARROWEST`]: a genuine 1920x1080 letterbox +/// (`t = 138`, `b = 139`) leaves 2.39:1 and fits, the reporter's `276` / `277` on 1920x802 leaves +/// 7.7:1, and a 4:3 film cropped to 1440x1080 that still says `l = r = 480` leaves 0.44:1. +pub fn dv_active_area_fits(w: u32, h: u32, l: u32, r: u32, t: u32, b: u32) -> bool { + let [w, h, l, r, t, b] = [w, h, l, r, t, b].map(u64::from); + if l + r >= w || t + b >= h { + return false; + } + let (aw, ah) = (w - l - r, h - t - b); + aw * DV_AREA_WIDEST.1 <= ah * DV_AREA_WIDEST.0 && aw * DV_AREA_NARROWEST.1 >= ah * DV_AREA_NARROWEST.0 +} + +/// PURE: are these offsets authored for another raster, so that the `w` x `h` picture they travel +/// with is better fed without them? A wrong keep cuts real picture and a wrong clear only shows the +/// black bars the encode itself carries (what `dovi_tool --crop` does), so the rule leans to clear. +/// +/// 1. All-zero offsets are nothing to clear. +/// 2. A raster that is not 16:9 was cropped and has no bars to describe: any offsets are foreign. +/// A 1920x1038 crop of a 1.85:1 master (`t = b = 42`) leaves 2.01:1, a plausible area that would +/// still blank 84 rows, which is why the area's shape alone cannot decide it. +/// 3. On a 16:9 raster, offsets are foreign when they cannot describe it ([`dv_active_area_fits`]), +/// or when `foreign_seen`: an earlier RPU of the stream was foreign. A variable-aspect 4K master +/// downscaled to 1080p has 1.90:1 scenes whose offsets (`t = 69`, `b = 70`) look plausible on +/// their own, but the stream has already shown it carries the master's. +pub fn dv_active_area_is_foreign(w: u32, h: u32, l: u32, r: u32, t: u32, b: u32, foreign_seen: bool) -> bool { + if l == 0 && r == 0 && t == 0 && b == 0 { + return false; + } + if u64::from(w) * DV_FULL_FRAME.1 != u64::from(h) * DV_FULL_FRAME.0 { + return true; + } + foreign_seen || !dv_active_area_fits(w, h, l, r, t, b) +} + +/// The Dolby Vision active-area check's per-stream state, owned by the demux loop beside the +/// stream's other per-open values and handed to [`packet_to_annexb`]. +#[derive(Debug, Default)] +pub struct DvActiveArea { + /// The picture's `(width, height)` as the demuxer reports it (the displayed raster, not the + /// coded one); `None` when it reports none, and then nothing is judged. + raster: Option<(u32, u32)>, + /// An RPU of this stream has been judged foreign ([`dv_active_area_is_foreign`], rule 3). + foreign_seen: bool, +} + +impl DvActiveArea { + /// A stream whose demuxer reports `width` x `height`; a non-positive size is "not reported". + pub fn new(width: c_int, height: c_int) -> Self { + let raster = u32::try_from(width) + .ok() + .zip(u32::try_from(height).ok()) + .filter(|&(w, h)| w > 0 && h > 0); + Self { raster, foreign_seen: false } + } +} + +/// Zero the Level 5 offsets of a Dolby Vision RPU NAL (`UNSPEC62`, 2-byte header + payload) that +/// [`dv_active_area_is_foreign`] judges foreign to the stream's picture, as `dovi_tool --crop` +/// does, and write it again; every other field is kept. `Some` is the rewritten NAL; `None` means +/// the NAL goes out as it is: the offsets are not foreign, are already zero, or there are none (no +/// L5 block), the RPU does not parse (any profile), the raster is not known, or the rewrite failed. +/// A foreign verdict is recorded in `area` whether or not the rewrite succeeds. +pub fn clear_foreign_active_area(nal: &[u8], area: &mut DvActiveArea) -> Option> { + let (w, h) = area.raster?; + let mut rpu = dolby_vision::rpu::dovi_rpu::DoviRpu::parse_unspec62_nalu(nal).ok()?; + let (l, r, t, b) = rpu_active_area(&rpu)?; + if !dv_active_area_is_foreign(w, h, l.into(), r.into(), t.into(), b.into(), area.foreign_seen) { + return None; + } + area.foreign_seen = true; + rpu.crop().ok()?; + rpu.write_hevc_unspec62_nalu().ok() +} + +/// The L5 offsets `(left, right, top, bottom)` an RPU NAL carries, for the log line. +fn dv_rpu_active_area(nal: &[u8]) -> Option<(u16, u16, u16, u16)> { + let rpu = dolby_vision::rpu::dovi_rpu::DoviRpu::parse_unspec62_nalu(nal).ok()?; + rpu_active_area(&rpu) +} + +fn rpu_active_area(rpu: &dolby_vision::rpu::dovi_rpu::DoviRpu) -> Option<(u16, u16, u16, u16)> { + match rpu.vdr_dm_data.as_ref()?.get_block(5)? { + dolby_vision::rpu::extension_metadata::blocks::ExtMetadataBlock::Level5(l5) => Some(l5.get_offsets()), + _ => None, + } +} + +/// The RPU as fed: `nal` itself unless [`clear_foreign_active_area`] rewrote it for the stream's +/// picture. Counts and logs each clear. +fn fit_active_area<'a>(nal: &'a [u8], area: &mut DvActiveArea) -> std::borrow::Cow<'a, [u8]> { + match clear_foreign_active_area(nal, area) { + Some(cleared) => { + let n = DV_AREA_CLEARED.fetch_add(1, Ordering::Relaxed) + 1; + if n <= 3 || n % 5000 == 0 { + let (l, r, t, b) = dv_rpu_active_area(nal).unwrap_or_default(); + let (w, h) = area.raster.unwrap_or_default(); + plx_base::eventlog::log(&format!( + "ff: DV RPU #{n} active area l={l} r={r} t={t} b={b} cannot describe {w}x{h}; cleared" + )); + } + std::borrow::Cow::Owned(cleared) + } + None => std::borrow::Cow::Borrowed(nal), + } +} + /// What to do with one HEVC NAL on its way to the decoder. #[derive(Debug, PartialEq, Eq)] pub enum SeiStrip { @@ -2580,12 +2705,18 @@ impl Keyframe { /// ([`dovi_converts_to_p81`]). Every enhancement-layer NAL ([`is_dv_enhancement_layer`]) is /// dropped and every RPU is rewritten by [`rewrite_dv_rpu_p7_to_p81`], so what leaves here is the /// single-layer stream the Load payload declares (`DolbyHdrInfo profileId 8`). +/// +/// `dv_area`: the stream's active-area state ([`DvActiveArea`]). Every HEVC RPU, converted or not, +/// is checked against the picture, and one whose active area is foreign to it is fed with the +/// offsets zeroed ([`clear_foreign_active_area`]); any other RPU leaves as it arrived here (for a +/// converted Profile 7, as the conversion wrote it). pub unsafe fn packet_to_annexb( data: *const u8, size: usize, nls: usize, is_hevc: bool, dv_p7: bool, + dv_area: &mut DvActiveArea, container_key: bool, param: &[u8], out: &mut Vec, @@ -2668,7 +2799,7 @@ pub unsafe fn packet_to_annexb( )); } out.extend_from_slice(&sc); - out.extend_from_slice(&rpu); + out.extend_from_slice(&fit_active_area(&rpu, dv_area)); } None => { let n = DV_RPU_FAILED.fetch_add(1, Ordering::Relaxed) + 1; @@ -2685,7 +2816,12 @@ pub unsafe fn packet_to_annexb( } let mut nal = nal; let rewritten; + let fitted; if is_hevc { + if ((nal[0] >> 1) & 0x3f) == HEVC_NAL_DV_RPU { + fitted = fit_active_area(nal, dv_area); + nal = &fitted; + } match strip_hdr10plus_sei(nal) { SeiStrip::Keep => {} strip => { @@ -6608,6 +6744,10 @@ pub fn demux( "ff: Dolby Vision P7 -> P8.1 conversion armed (EL dropped, RPUs rewritten)", ); } + // The picture the RPUs travel with: the raster the demuxer reports (the displayed + // one, not the coded one), and the check's per-stream latch, which + // `packet_to_annexb` reads and sets for each RPU. + let mut dv_area = DvActiveArea::new((*vcp).width, (*vcp).height); let is_av1 = framing == VideoFraming::Av1; let is_vp9 = framing == VideoFraming::Vp9; let (param_blob, nal_len_size) = if is_av1 || is_vp9 { @@ -6808,6 +6948,7 @@ pub fn demux( nal_len_size, is_hevc, dv_p7, + &mut dv_area, (*pkt).flags & AV_PKT_FLAG_KEY != 0, ¶m_blob, &mut aubuf, diff --git a/rust-modules/play-feed/src/ff_fixture_bytes.rs b/rust-modules/play-feed/src/ff_fixture_bytes.rs index 64b63dad89..6ab375e706 100644 --- a/rust-modules/play-feed/src/ff_fixture_bytes.rs +++ b/rust-modules/play-feed/src/ff_fixture_bytes.rs @@ -19,24 +19,32 @@ pub fn to_annexb(buf: &[u8], is_hevc: bool, param: &[u8]) -> (bool, Vec) { pub fn to_annexb_class(buf: &[u8], is_hevc: bool, param: &[u8]) -> (Keyframe, Vec) { let mut out = Vec::new(); let key = - unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, is_hevc, false, false, param, &mut out) }; + unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, is_hevc, false, &mut DvActiveArea::default(), false, param, &mut out) }; (key, out) } /// [`to_annexb`] for an H.264 packet the container flags as a keyframe (`AV_PKT_FLAG_KEY`). pub fn to_annexb_flagged(buf: &[u8], param: &[u8]) -> (bool, Vec) { let mut out = Vec::new(); - let key = unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, false, false, true, param, &mut out) }; + let key = unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, false, false, &mut DvActiveArea::default(), true, param, &mut out) }; (key.is_key(), out) } /// [`to_annexb`] for an HEVC stream with the Dolby Vision Profile 7 -> 8.1 conversion armed. pub fn to_annexb_p7(buf: &[u8], param: &[u8]) -> (bool, Vec) { let mut out = Vec::new(); - let key = unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, true, true, false, param, &mut out) }; + let key = unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, true, true, &mut DvActiveArea::default(), false, param, &mut out) }; (key.is_key(), out) } +/// [`to_annexb`] for an HEVC stream with the active-area state `area` (`dv_p7`: with the Profile 7 -> 8.1 +/// conversion armed), so each RPU is checked against that picture and the state it has built up. +pub fn to_annexb_dv(buf: &[u8], dv_p7: bool, area: &mut DvActiveArea) -> Vec { + let mut out = Vec::new(); + unsafe { packet_to_annexb(buf.as_ptr(), buf.len(), 4, true, dv_p7, area, false, &[], &mut out) }; + out +} + /// One of `tests/fixtures/dovi-p7-*-rpu.bin` (an RPU as `dovi_tool` writes it: a 4-byte start /// code, then the payload after the `7C 01` NAL header) as the `UNSPEC62` NAL a demuxed packet /// carries. diff --git a/rust-modules/play-feed/src/ff_packet_framing_tests.rs b/rust-modules/play-feed/src/ff_packet_framing_tests.rs index 589d46dfb4..a0ef1c0997 100644 --- a/rust-modules/play-feed/src/ff_packet_framing_tests.rs +++ b/rust-modules/play-feed/src/ff_packet_framing_tests.rs @@ -258,7 +258,7 @@ fn irap_class_h264_av1_vp9_keyframes_are_clean() { let mut out = Vec::new(); let flagged = unsafe { let buf = avcc(&[&[0x21, 0x88, 0xAA, 0xBB]]); - packet_to_annexb(buf.as_ptr(), buf.len(), 4, false, false, true, &[], &mut out) + packet_to_annexb(buf.as_ptr(), buf.len(), 4, false, false, &mut DvActiveArea::default(), true, &[], &mut out) }; assert_eq!((flagged, flagged.au_key()), (Keyframe::Clean, crate::aq::AU_KEY)); // AV1 and VP9 answer a bool, which the demux loop wraps: a keyframe is always `Clean`. @@ -531,8 +531,9 @@ fn an_unconvertible_rpu_is_dropped_on_an_armed_feed() { assert_eq!(out, vec![0, 0, 0, 1, 0x02, 0x01, 0xAA]); } -/// Unarmed (every stream that is not a converted Profile 7) nothing changes: enhancement-layer -/// NALs and RPUs pass through exactly as they always did. +/// Unarmed (every stream that is not a converted Profile 7) the conversion changes nothing: +/// enhancement-layer NALs and RPUs pass through it exactly as they always did. (The active-area +/// check, which can rewrite an RPU, has no raster here, so it judges nothing.) #[test] fn an_unarmed_feed_is_untouched_by_the_conversion() { let el_wrapped = [0x7Eu8, 0x01, 0xBB]; @@ -549,6 +550,240 @@ fn an_unarmed_feed_is_untouched_by_the_conversion() { ); } +// -- Dolby Vision active area (L5) ------------------------------------------------------ +// +// The RPUs are built here with the `dolby_vision` crate from the fixtures `tests/fixtures/dovi-p7-*` +// (dovi_tool's own test RPUs): the fixture is parsed, its L5 block set or removed, and written back. + +use dolby_vision::rpu::dovi_rpu::DoviRpu; + +type Area = (u16, u16, u16, u16); + +fn parse_rpu(nal: &[u8]) -> DoviRpu { + DoviRpu::parse_unspec62_nalu(nal).expect("an RPU the crate wrote parses") +} + +/// `fixture` (an RPU file) as an RPU NAL whose L5 block is `area` (`None`: no L5 block at all). +fn rpu_with_l5(fixture: &[u8], area: Option) -> Vec { + let mut rpu = parse_rpu(&rpu_fixture_nal(fixture)); + match area { + Some((l, r, t, b)) => rpu.set_active_area_offsets(l, r, t, b).unwrap(), + None => { + rpu.vdr_dm_data.as_mut().unwrap().remove_metadata_level(5); + rpu.modified = true; + } + } + let nal = rpu.write_hevc_unspec62_nalu().unwrap(); + assert_eq!(dv_rpu_active_area(&nal), area, "the built RPU carries the L5 it was asked for"); + nal +} + +const P81: &[u8] = include_bytes!("../../../tests/fixtures/dovi-p7-mel-to-81-rpu.bin"); +const P7: &[u8] = include_bytes!("../../../tests/fixtures/dovi-p7-mel-rpu.bin"); + +/// Everything an RPU says except the four offsets of its L5 block, as text (the crate's types +/// are `Debug` but not `PartialEq`): the RPU with its L5 set to zero, through the crate. +fn all_but_the_offsets(nal: &[u8]) -> String { + let mut rpu = parse_rpu(nal); + rpu.set_active_area_offsets(0, 0, 0, 0).unwrap(); + format!("P{} {:?} {:?} {:?}", rpu.dovi_profile, rpu.header, rpu.rpu_data_mapping, rpu.vdr_dm_data) +} + +/// One RPU judged on a fresh stream whose demuxer reports `w` x `h`. +fn clear(nal: &[u8], w: i32, h: i32) -> Option> { + clear_foreign_active_area(nal, &mut DvActiveArea::new(w, h)) +} + +/// A packet holding a base-layer slice and `rpu`, as a demuxer hands it over. +fn packet_with_rpu(rpu: &[u8]) -> Vec { + avcc(&[&[0x02u8, 0x01, 0xAA], rpu]) +} + +/// The RPU NAL a fed packet carries (the bytes after the slice's start code and its own NAL). +fn fed_rpu(out: &[u8]) -> Vec { + let head = [0u8, 0, 0, 1, 0x02, 0x01, 0xAA, 0, 0, 0, 1]; + assert_eq!(&out[..head.len()], &head, "the slice is fed first, then the RPU"); + out[head.len()..].to_vec() +} + +/// The reporter's file (#1367): a 1920x802 encode cropped from a 3840x2160 master whose RPUs still +/// say `top=276 bottom=277`. The TV blanks everything outside that area, a strip 249 rows high. +#[test] +fn the_reporters_leftover_master_offsets_are_cleared_and_nothing_else_changes() { + let nal = rpu_with_l5(P81, Some((0, 0, 276, 277))); + let out = clear(&nal, 1920, 802).expect("offsets that leave 249 rows are cleared"); + assert_eq!(&out[..2], &nal[..2], "still the same UNSPEC62 NAL header"); + let after = parse_rpu(&out); + assert_eq!(dv_rpu_active_area(&out), Some((0, 0, 0, 0))); + assert_eq!(after.dovi_profile, 8); + assert_eq!(all_but_the_offsets(&nal), all_but_the_offsets(&out), "only the four offsets differ"); + assert!(after.vdr_dm_data.as_ref().unwrap().get_block(1).is_some(), "the L1 block is still there"); +} + +/// A genuine 1920x1080 letterbox (2.39:1 left over) is the file's own and is fed as it came: the +/// SAME bytes, not a re-serialisation. +#[test] +fn a_genuine_letterbox_is_fed_byte_for_byte() { + let nal = rpu_with_l5(P81, Some((0, 0, 138, 139))); + assert_eq!(clear(&nal, 1920, 1080), None); + let out = to_annexb_dv(&packet_with_rpu(&nal), false, &mut DvActiveArea::new(1920, 1080)); + assert_eq!(fed_rpu(&out), nal); +} + +#[test] +fn an_rpu_with_no_l5_block_is_fed_unchanged() { + let nal = rpu_with_l5(P81, None); + assert_eq!(clear(&nal, 1920, 802), None); + let out = to_annexb_dv(&packet_with_rpu(&nal), false, &mut DvActiveArea::new(1920, 802)); + assert_eq!(fed_rpu(&out), nal); +} + +/// The same failure on the pillarbox side: a 4:3 film cropped to 1440x1080 that still says +/// `left=480 right=480` leaves 480x1080, 0.44:1. +#[test] +fn leftover_pillarbox_offsets_are_cleared() { + let nal = rpu_with_l5(P81, Some((480, 480, 0, 0))); + let out = clear(&nal, 1440, 1080).expect("0.44:1 cannot be a picture"); + assert_eq!(dv_rpu_active_area(&out), Some((0, 0, 0, 0))); +} + +/// What leaves `packet_to_annexb` for a P8.1 stream: the clear is on the fed bytes, in the RPU's place. +#[test] +fn a_feed_clears_a_foreign_active_area_on_a_profile_8_stream() { + let nal = rpu_with_l5(P81, Some((0, 0, 276, 277))); + let out = to_annexb_dv(&packet_with_rpu(&nal), false, &mut DvActiveArea::new(1920, 802)); + let fed = fed_rpu(&out); + assert_ne!(fed, nal); + assert_eq!(dv_rpu_active_area(&fed), Some((0, 0, 0, 0))); +} + +/// The converted Profile 7 path checks the area too, on the RPU it has just made 8.1; with no +/// raster reported the offsets are carried through the conversion as they were. +#[test] +fn a_converted_profile_7_rpu_is_cleared_too() { + let nal = rpu_with_l5(P7, Some((0, 0, 276, 277))); + assert_eq!(parse_rpu(&nal).dovi_profile, 7); + let fed = fed_rpu(&to_annexb_dv(&packet_with_rpu(&nal), true, &mut DvActiveArea::new(1920, 802))); + assert_eq!(parse_rpu(&fed).dovi_profile, 8, "converted"); + assert_eq!(dv_rpu_active_area(&fed), Some((0, 0, 0, 0)), "and cleared"); + let kept = fed_rpu(&to_annexb_dv(&packet_with_rpu(&nal), true, &mut DvActiveArea::default())); + assert_eq!(dv_rpu_active_area(&kept), Some((0, 0, 276, 277)), "no raster, no judgement"); +} + +/// A raster the demuxer did not report cannot be judged against, and an RPU that does not parse +/// (or carries no area at all) is not ours to change. +#[test] +fn an_unknown_raster_or_an_unparsable_rpu_is_left_alone() { + let nal = rpu_with_l5(P81, Some((0, 0, 276, 277))); + assert_eq!(clear(&nal, 0, 802), None); + assert_eq!(clear(&nal, 1920, 0), None); + assert_eq!(clear(&[0x7C, 0x01, 0x19, 0x08], 1920, 802), None); + let out = to_annexb_dv(&packet_with_rpu(&nal), false, &mut DvActiveArea::default()); + assert_eq!(fed_rpu(&out), nal); + let junk = [0x7Cu8, 0x01, 0x19, 0x08]; + assert_eq!(fed_rpu(&to_annexb_dv(&packet_with_rpu(&junk), false, &mut DvActiveArea::new(1920, 802))), junk); +} + +/// An RPU that already says "no offsets" has nothing to clear, even on a picture so wide that the +/// bounds alone would call the whole raster foreign: it is not rewritten every frame. +#[test] +fn zero_offsets_are_never_rewritten() { + let nal = rpu_with_l5(P81, Some((0, 0, 0, 0))); + assert_eq!(clear(&nal, 2560, 700), None); +} + +/// A cropped picture has no bars for an active area to describe, whatever the area's shape: a +/// 1.85:1 master (`t = b = 42`) cropped to 1920x1038 still says 84 rows, and 2.01:1 looks like a +/// picture. Nor does a 2.00:1 master (`t = b = 120`) cropped to 1920x960, 2.67:1. +#[test] +fn a_cropped_raster_has_its_plausible_looking_master_offsets_cleared() { + for (w, h, (l, r, t, b)) in [ + (1920, 1038, (0, 0, 42, 42)), + (1920, 960, (0, 0, 120, 120)), + (3840, 1600, (0, 0, 100, 100)), + (4096, 2160, (0, 0, 138, 139)), + ] { + assert!( + dv_active_area_fits(w as u32, h as u32, l, r, t, b), + "{w}x{h} with {t}/{b}: the area alone would pass" + ); + let nal = rpu_with_l5(P81, Some((l as u16, r as u16, t as u16, b as u16))); + let out = clear(&nal, w, h).unwrap_or_else(|| panic!("{w}x{h} with {t}/{b} is cleared")); + assert_eq!(dv_rpu_active_area(&out), Some((0, 0, 0, 0))); + assert_eq!(all_but_the_offsets(&nal), all_but_the_offsets(&out)); + } +} + +/// A 3840x2160 master's own bars are the picture's: kept as they came. +#[test] +fn a_genuine_4k_letterbox_is_fed_byte_for_byte() { + let nal = rpu_with_l5(P81, Some((0, 0, 276, 277))); + assert_eq!(clear(&nal, 3840, 2160), None); + let out = to_annexb_dv(&packet_with_rpu(&nal), false, &mut DvActiveArea::new(3840, 2160)); + assert_eq!(fed_rpu(&out), nal); +} + +/// A variable-aspect 4K master downscaled to 1080p: the first scene's `276 / 277` cannot describe +/// 1920x1080, and once a stream has shown that it carries the master's offsets, its later RPUs +/// (the 1.90:1 scenes' `69 / 70`, plausible alone) are cleared too. The latch is the stream's: +/// the same RPU on a fresh stream is kept. +#[test] +fn once_a_stream_has_shown_foreign_offsets_its_later_plausible_ones_are_cleared_too() { + let first = rpu_with_l5(P81, Some((0, 0, 276, 277))); + let later = rpu_with_l5(P81, Some((0, 0, 69, 70))); + let mut area = DvActiveArea::new(1920, 1080); + assert!(clear_foreign_active_area(&first, &mut area).is_some()); + let out = clear_foreign_active_area(&later, &mut area).expect("cleared on the latched stream"); + assert_eq!(dv_rpu_active_area(&out), Some((0, 0, 0, 0))); + let zero = rpu_with_l5(P81, Some((0, 0, 0, 0))); + assert_eq!(clear_foreign_active_area(&zero, &mut area), None, "no offsets, nothing to clear"); + + assert_eq!(clear(&later, 1920, 1080), None, "alone, on a fresh stream, it is a letterbox"); + let kept = to_annexb_dv(&packet_with_rpu(&later), false, &mut DvActiveArea::new(1920, 1080)); + assert_eq!(fed_rpu(&kept), later); + + // the same two RPUs through the feed, one packet each + let mut area = DvActiveArea::new(1920, 1080); + to_annexb_dv(&packet_with_rpu(&first), false, &mut area); + let fed = fed_rpu(&to_annexb_dv(&packet_with_rpu(&later), false, &mut area)); + assert_eq!(dv_rpu_active_area(&fed), Some((0, 0, 0, 0))); +} + +#[test] +fn the_rule_for_foreign_offsets() { + // (w, h, l, r, t, b, foreign_seen) + assert!(!dv_active_area_is_foreign(1920, 802, 0, 0, 0, 0, true), "no offsets are nothing to clear"); + assert!(dv_active_area_is_foreign(1920, 802, 0, 0, 1, 0, false), "not 16:9: any offsets"); + assert!(dv_active_area_is_foreign(1920, 1038, 0, 0, 42, 42, false)); + assert!(dv_active_area_is_foreign(4096, 2160, 0, 0, 138, 139, false), "DCI is not 16:9"); + assert!(dv_active_area_is_foreign(1440, 1080, 480, 480, 0, 0, false)); + assert!(!dv_active_area_is_foreign(1920, 1080, 0, 0, 138, 139, false), "16:9 and plausible"); + assert!(!dv_active_area_is_foreign(3840, 2160, 0, 0, 276, 277, false)); + assert!(dv_active_area_is_foreign(1920, 1080, 0, 0, 276, 277, false), "16:9 and 3.6:1"); + assert!(dv_active_area_is_foreign(1920, 1080, 0, 0, 69, 70, true), "plausible, but latched"); + assert!(dv_active_area_is_foreign(1280, 720, 0, 0, 92, 92, true)); + assert!(!dv_active_area_is_foreign(1280, 720, 0, 0, 92, 92, false)); +} + +#[test] +fn the_bounds_of_dv_active_area_fits() { + // (w, h, l, r, t, b) + assert!(dv_active_area_fits(1920, 1080, 0, 0, 0, 0), "the whole picture"); + assert!(dv_active_area_fits(1920, 1080, 0, 0, 138, 139), "2.39:1 letterbox"); + assert!(dv_active_area_fits(3840, 2160, 0, 0, 276, 277), "the master the reporter's file came from"); + assert!(dv_active_area_fits(2760, 1000, 0, 0, 0, 0), "2.76:1, Ultra Panavision 70, is kept"); + assert!(dv_active_area_fits(3000, 1000, 0, 0, 0, 0), "3.0:1 itself is kept"); + assert!(!dv_active_area_fits(3100, 1000, 0, 0, 0, 0), "3.1:1 is not a picture"); + assert!(!dv_active_area_fits(1920, 802, 0, 0, 276, 277), "7.7:1, the reporter's file"); + assert!(dv_active_area_fits(1080, 1920, 0, 0, 0, 0), "9:16 portrait, 0.5625, is kept"); + assert!(dv_active_area_fits(1000, 2000, 0, 0, 0, 0), "0.5:1 itself is kept"); + assert!(!dv_active_area_fits(1440, 1080, 480, 480, 0, 0), "0.44:1 is not a picture"); + assert!(!dv_active_area_fits(1920, 1080, 0, 0, 540, 540), "nothing left vertically"); + assert!(!dv_active_area_fits(1920, 1080, 0, 0, 600, 600), "more than the picture vertically"); + assert!(!dv_active_area_fits(1920, 1080, 960, 960, 0, 0), "nothing left horizontally"); + assert!(!dv_active_area_fits(0, 0, 0, 0, 0, 0), "no picture at all"); +} + // -- AV1: leb128 ---------------------------------------------------------------------- #[test] From 5570be8233e9a16963e8a50f24972c16fe4c9d31 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sat, 14 Nov 2026 17:11:23 +0300 Subject: [PATCH 3/8] Image subtitles turned on mid-play show the read-ahead window's lines instead of a ~10 s gap (#1393) With subtitles Off, the main demuxer threw away image-subtitle (PGS, VobSub, DVB) packets as it read ahead of the playhead. Turning on an image track mid-play then showed nothing for as long as the read-ahead, about 10 s on a 4K direct play. Text tracks were unaffected, because their cues are kept for every track. `SubTracks` now holds owned copies of those packets in an `ImageBacklog` while subtitles are Off. On the first packet read after a subtitle is selected, it decodes them through the existing image decoders, in demux order across tracks, ahead of the live packet. Each track keeps packets from the last one at or before the anchor onward; the anchor is the new `CaptionRetention::anchor_ns()`, the earlier of playhead and seek target, which `subtitle_floor_ns` now also uses. For PGS the cut stops at the newest display set's PCS, because M2TS carries each PGS segment as its own packet. A seek or close discards the backlog. The per-packet subtitle step is one set of `SubTracks` methods that `demux()` and the tests share. The decode adds no FFmpeg symbol or struct offset: it uses an `av_packet_alloc` scratch packet over the owned, padded bytes, and `ci/ffabi-assert.c` pins the padding. The live subtitle path now reads the format's current stream table. Verified on the dev TV with a mock PGS clip: turning the track on at ~5 s emitted the 0 s and 10 s cues at once. Before this change the first cue would have been the 20 s one. Co-Authored-By: Claude Opus 5.5 --- ci/ffabi-assert.c | 3 + .../media/src/ff_image_subtitle_tests.rs | 202 +++++++- rust-modules/media/src/player/mod.rs | 10 +- rust-modules/play-feed/src/ff.rs | 22 +- rust-modules/play-feed/src/ff_subs.rs | 482 +++++++++++++++++- rust-modules/play-feed/src/lib.rs | 2 +- .../playback-contract/src/subtitle.rs | 32 ++ 7 files changed, 734 insertions(+), 19 deletions(-) diff --git a/ci/ffabi-assert.c b/ci/ffabi-assert.c index 7131bdb82a..0e286e5914 100644 --- a/ci/ffabi-assert.c +++ b/ci/ffabi-assert.c @@ -134,6 +134,9 @@ SAME(sizeof(((AVChannelLayout *)0)->u.mask), 8, "the channel mask is 64 bits"); /* `av_opt_set`'s search flag, passed by ff_pcm.rs to reach the DTS decoder's private `core_only` option; a moved value would make that call fail silently and a DTS-HD stream decode its extensions. */ SAME(AV_OPT_SEARCH_CHILDREN, 1, "ff_pcm::AV_OPT_SEARCH_CHILDREN"); +/* The zero tail a decoder may read past an AVPacket's `size`: play-feed's ff_subs.rs copies held image-subtitle + packets into buffers padded by its own INPUT_PADDING, so a larger bundled padding would be a silent overread. */ +SAME(AV_INPUT_BUFFER_PADDING_SIZE, 64, "ff_subs::INPUT_PADDING != AV_INPUT_BUFFER_PADDING_SIZE"); SAME(AV_SAMPLE_FMT_U8, 0, "speed_audio::SampleFormat::from_av U8"); SAME(AV_SAMPLE_FMT_S16, 1, "speed_audio::SampleFormat::from_av S16"); SAME(AV_SAMPLE_FMT_S32, 2, "speed_audio::SampleFormat::from_av S32"); diff --git a/rust-modules/media/src/ff_image_subtitle_tests.rs b/rust-modules/media/src/ff_image_subtitle_tests.rs index 3b1ae33253..2395e2b86c 100644 --- a/rust-modules/media/src/ff_image_subtitle_tests.rs +++ b/rust-modules/media/src/ff_image_subtitle_tests.rs @@ -16,7 +16,9 @@ use crate::player::subtitle_cue::{SideTracks, SubCue}; use crate::player::SHARED; use crate::player::{ass, ass_source}; use plx_media_codecs::subtitle_extract::{collect_manifest, detach_fonts, AssTrack}; -use plx_playback_contract::subtitle::{CaptionRetention, DecodeKind}; +use plx_playback_contract::demux::DemuxSubtitle; +use plx_playback_contract::subtitle::{CaptionRetention, DecodeKind, Manifest, RawCue, TrackInfo}; +use std::cell::Cell; use std::sync::Arc; const TERMINATED_ASS_HEADER: &[u8] = @@ -550,3 +552,201 @@ fn mkvmerge_zlib_subtitle_tracks_decode_like_uncompressed_ones() { "the zlib-compressed SRT packet must arrive inflated" ); } + +/// When, relative to the demux loop's two per-packet subtitle steps, the selection turns on: +/// `SubTracks::release_if_selected` (every packet read) and `SubTracks::on_packet` (a subtitle +/// stream's packet), the two calls `ff::demux` makes and these tests make. +#[derive(Clone, Copy, PartialEq)] +enum Selection { + On, + OffThenOn, + /// On from the first packet at or after this time (ns), seen by the release check. + OffUntil(i64), + /// On from the first packet at or after this time (ns), but only AFTER that packet's release + /// check: the selection lands between the check and the decode, the race the decode gate closes. + LandsAfterReleaseCheck(i64), + OffThenSeekThenOn, +} + +/// The subtitle owner `demux` talks to (`DemuxSubtitle`) with the selection as a flag the fixture +/// flips and every cue going to the common presentation, counting the image cues that were emitted +/// while the selection was off. +struct FixtureSubtitle<'a> { + on: &'a Cell, + presentation: &'a SideTracks, + image_cues_emitted_while_off: usize, +} + +impl DemuxSubtitle for FixtureSubtitle<'_> { + fn plan(&mut self, _: &[TrackInfo]) -> Vec { Vec::new() } + fn max_header_bytes(&self) -> usize { 0 } + fn begin_sources(&mut self, _: &str, _: Manifest) -> Option { None } + fn record_tracks(&self) {} + fn selected_track(&self) -> i32 { if self.on.get() { 0 } else { -1 } } + fn bitmap_budget(&self) -> usize { crate::player::SUB_BITMAP_BUDGET } + fn emit(&mut self, generation: u64, cue: RawCue) { + if matches!(cue, RawCue::Bitmap { .. }) && !self.on.get() { self.image_cues_emitted_while_off += 1; } + self.presentation.decode(cue).push(generation, fixture_retention()); + } + fn seek(&mut self, _: u64) {} +} + +/// The cues `mkv_zlib_subs.mkv`'s image tracks put in the store as `(track, start_ns, end_ns, +/// rects)`, and how many image cues were emitted while the selection was off, when every packet is +/// read the way the demux loop reads them under `selection`: `release_if_selected` on every packet +/// read, then `on_packet` for a subtitle stream's. It calls the `SubTracks` methods `ff::demux` +/// calls, so what it grades is what the loop does, not a copy of it. +unsafe fn present_zlib_fixture(selection: Selection) -> (Vec<(i32, i64, i64, usize)>, usize) { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../tests/fixtures/mkv_zlib_subs.mkv") + .canonicalize() + .expect("fixture present"); + let url = CString::new(path.to_str().unwrap()).unwrap(); + SHARED.sub_bitmaps.lock().unwrap().clear(); + FEED_SHARED.playpos_ns.store(0, Ordering::Relaxed); + + let mut fmt: *mut AVFormatContext = std::ptr::null_mut(); + assert!( + avformat_open_input(&mut fmt, url.as_ptr(), std::ptr::null_mut(), std::ptr::null_mut()) >= 0, + "open the fixture" + ); + let infos = SubTracks::inspect(fmt, (*fmt).streams); + let presentation = SideTracks::new(&infos); + let plan = presentation.decode_plan(); + let mut subs = SubTracks::open((*fmt).streams, infos, plan); + let on = Cell::new(selection == Selection::On); + let mut subtitle = FixtureSubtitle { on: &on, presentation: &presentation, image_cues_emitted_while_off: 0 }; + + let pkt = av_packet_alloc(); + while av_read_frame(fmt, pkt) >= 0 { + let si = (*pkt).stream_index; + // The demux loop's bounded lookup: a packet of a stream the table does not (or no longer) + // describe is skipped, not read through a pointer past the table. + let Some(st) = live_stream(fmt, si) else { + av_packet_unref(pkt); + continue; + }; + let read_ns = plx_media_codecs::subtitle_extract::pts_ns_opt(pkt, st); + let reached = |at_ns: i64| read_ns.is_some_and(|t| t >= at_ns); + if let Selection::OffUntil(at_ns) = selection { + if reached(at_ns) { on.set(true); } + } + subs.release_if_selected(fmt, &mut subtitle, 0); + if let Selection::LandsAfterReleaseCheck(at_ns) = selection { + if reached(at_ns) { on.set(true); } + } + if let Some(pos) = subs.position(si) { + subs.on_packet(pos, pkt, fmt, &mut subtitle, 0, || Some(fixture_retention())); + } + av_packet_unref(pkt); + } + let mut p = pkt; + av_packet_free(&mut p); + + if selection == Selection::OffThenSeekThenOn { + // Without this the "nothing lands" result below would hold for a backlog that was never kept. + assert!(subs.holds_images(), "the packets read while Off are held"); + subs.discard_held(); + } + // The first packet after the selection: everything held is decoded before it. + on.set(true); + subs.release_if_selected(fmt, &mut subtitle, 0); + assert!(!subs.holds_images(), "releasing leaves nothing held"); + let emitted_while_off = subtitle.image_cues_emitted_while_off; + subs.close(); + avformat_close_input(&mut fmt); + + let mut cues: Vec<(i32, i64, i64, usize)> = SHARED + .sub_bitmaps + .lock() + .unwrap() + .iter() + .map(|c| (c.track, c.start_ns, c.end_ns, c.rects.len())) + .collect(); + cues.sort(); + SHARED.sub_bitmaps.lock().unwrap().clear(); + (cues, emitted_while_off) +} + +/// **An image track turned on mid-play shows the lines the demuxer had already read.** The demuxer +/// reads ahead of the playhead; with subtitles Off the image packets of that window were dropped, so +/// a track chosen at 232 s showed nothing until the read-ahead had been used up (10 s), while a TEXT +/// track, whose cues are kept whatever the selection, answered at once. Held packets decode, in file +/// order, to exactly the cues the same packets give when they are decoded as they are read. +/// +/// The fixture's image tracks (PGS and VobSub, each also zlib-compressed) carry one cue at 0 s. +#[test] +#[ignore = "needs the host build of the bundled FFmpeg — run by `make check-ffmpeg`"] +fn image_packets_read_while_subtitles_are_off_decode_when_one_is_turned_on() { + let _g = plx_base::testlock::serial(); + let _ffmpeg = bind_host_ffmpeg(); + unsafe { + let (live, _) = present_zlib_fixture(Selection::On); + assert!( + live.iter().any(|cue| cue.0 == 0) && live.iter().any(|cue| cue.0 == 2), + "the control: the PGS and VobSub tracks decode when read live, got {live:?}" + ); + let (after_flip, emitted_while_off) = present_zlib_fixture(Selection::OffThenOn); + assert_eq!(emitted_while_off, 0, "nothing is decoded while Off"); + assert_eq!( + after_flip, live, + "the image cue read while Off must be emitted once an image track is on" + ); + } +} + +/// The same flip with the demuxer part-way through the file, which is how it happens on the +/// television: the 0 s display set was read while Off, the track is turned on, and the 8 s clear +/// then arrives live. The held set must be decoded BEFORE the clear, or the clear would close +/// nothing and the line would never be drawn. +#[test] +#[ignore = "needs the host build of the bundled FFmpeg — run by `make check-ffmpeg`"] +fn a_track_turned_on_between_two_packets_gets_the_held_set_before_the_live_one() { + let _g = plx_base::testlock::serial(); + let _ffmpeg = bind_host_ffmpeg(); + unsafe { + let (live, _) = present_zlib_fixture(Selection::On); + let (mid_read, emitted_while_off) = present_zlib_fixture(Selection::OffUntil(4_000_000_000)); + assert_eq!(emitted_while_off, 0, "nothing is decoded while Off"); + assert!( + mid_read.iter().any(|cue| cue.0 == 0 && cue.1 == 0 && cue.2 == 8_000_000_000), + "the PGS line is drawn from 0 s to the clear at 8 s, got {mid_read:?}" + ); + assert_eq!(mid_read, live, "held first, live after, exactly as if it had been on throughout"); + } +} + +/// A seek moves the playhead to a place the held packets do not describe: they are discarded, and +/// the selection that follows emits nothing of them. +#[test] +#[ignore = "needs the host build of the bundled FFmpeg — run by `make check-ffmpeg`"] +fn a_seek_discards_the_image_packets_held_while_subtitles_were_off() { + let _g = plx_base::testlock::serial(); + let _ffmpeg = bind_host_ffmpeg(); + unsafe { + let (cues, _) = present_zlib_fixture(Selection::OffThenSeekThenOn); + assert!(cues.is_empty(), "no stale cue lands after a seek, got {cues:?}"); + } +} + +/// The race the demux loop's decode gate closes. The selection turns on after a packet's release +/// check and before its decode; the held display set has not been released, so decoding this packet +/// now would put the 8 s clear ahead of the 0 s set it closes: the clear would close nothing, and +/// the set, decoded after it, would be drawn open-ended or not at all. The packet is held instead +/// and decoded with the rest at the next packet. +#[test] +#[ignore = "needs the host build of the bundled FFmpeg — run by `make check-ffmpeg`"] +fn a_selection_landing_between_the_release_check_and_the_decode_keeps_file_order() { + let _g = plx_base::testlock::serial(); + let _ffmpeg = bind_host_ffmpeg(); + unsafe { + let (live, _) = present_zlib_fixture(Selection::On); + let (raced, emitted_while_off) = present_zlib_fixture(Selection::LandsAfterReleaseCheck(4_000_000_000)); + assert_eq!(emitted_while_off, 0, "nothing is decoded while Off"); + assert!( + raced.iter().any(|cue| cue.0 == 0 && cue.1 == 0 && cue.2 == 8_000_000_000), + "the PGS line is drawn from 0 s to the clear at 8 s, got {raced:?}" + ); + assert_eq!(raced, live, "the held set first, the clear after, exactly as if it had been on throughout"); + } +} diff --git a/rust-modules/media/src/player/mod.rs b/rust-modules/media/src/player/mod.rs index 3ceb18d504..684fdb4063 100644 --- a/rust-modules/media/src/player/mod.rs +++ b/rust-modules/media/src/player/mod.rs @@ -2281,8 +2281,10 @@ pub fn desired_sub_idx() -> i32 { pub fn request_subtitle(idx: i32) { SHARED.desired_sub_idx.store(idx, Relaxed); if idx < 0 { - // subs Off: free the image-cue store now (the demuxer also stops decoding new - // bitmap cues while off — see ff.rs's desired_sub_idx gate) + // subs Off: free the image-cue store now. The demuxer also stops decoding new bitmap cues + // while off and HOLDS the image packets it reads instead (`SubTracks::on_packet` in + // play-feed's `ff_subs.rs`), so a track chosen later decodes the read-ahead window it + // would otherwise have lost. SHARED.sub_bitmaps.lock().unwrap().clear(); } } @@ -2458,8 +2460,8 @@ pub fn sidecar_clock_ns(ps: &crate::route::PlaybackSession, playhead_ns: i64) -> /// reads ahead of the playhead, which this floor never prunes. What bounds memory is each store's /// budget and eviction order ([`SUB_BITMAP_BUDGET`]), not this floor. fn subtitle_floor_ns(retention: plx_playback_contract::subtitle::CaptionRetention) -> i64 { - let Some(playhead_ns) = retention.playhead_ns else { return i64::MIN; }; - subtitle_floor_for(playhead_ns, retention.seek_target_ns.is_some(), retention.seek_target_ns.unwrap_or(-1)) + let Some(anchor_ns) = retention.anchor_ns() else { return i64::MIN; }; + subtitle_floor_for(anchor_ns, false, -1) } fn subtitle_floor_for(position_ns: i64, seeking: bool, target_ns: i64) -> i64 { diff --git a/rust-modules/play-feed/src/ff.rs b/rust-modules/play-feed/src/ff.rs index a97b75dcdc..a3e5ca3b29 100644 --- a/rust-modules/play-feed/src/ff.rs +++ b/rust-modules/play-feed/src/ff.rs @@ -6725,6 +6725,8 @@ pub fn demux( begin_ass_sources(session.as_mut(), &url, streams, &sub_tracks, font_attachments).unwrap_or(0) }; if !side_owner { session.subtitle().record_tracks(); } + // The playhead a held image packet is retired against (`SubTracks::on_packet`). + let observer = session.observer(); // Hand-roll length-prefix -> Annex-B + prepend VPS/SPS/PPS at every keyframe (the // format Starfish decodes). FFmpeg's hevc_mp4toannexb does NOT reliably prepend the @@ -6856,6 +6858,8 @@ pub fn demux( let ts = av_rescale_q(seek_ns.saturating_add(origin_ns), NS_TB, stream_time_base(vst)); // Fence completed renders from the old position before new packets arrive. session.subtitle().seek(ass_generation); + // Image packets held for a later selection belong to the old position too. + sub_tracks.discard_held(); session.media().record(&format!("ff: seek begin {}s", seek_ns / 1_000_000_000)); let seek_began = std::time::Instant::now(); let sr = av_seek_frame(fmt, vi, ts, AVSEEK_FLAG_BACKWARD); @@ -6887,6 +6891,9 @@ pub fn demux( } break; } + // A subtitle was turned on since the image packets held below were read: decode + // them now, on whatever packet comes next, ahead of it so cues stay in file order. + sub_tracks.release_if_selected(fmt, session.subtitle(), ass_generation); let si = (*pkt).stream_index; // The array is read LIVE, with its bound: a transport stream can grow new streams // while it is read (`AVFMTCTX_NOHEADER`), which reallocates `streams`, so the @@ -7173,14 +7180,13 @@ pub fn demux( } session.media().publish(DemuxEffect::AudioTailNs(pts)); } else if let Some(sub_pos) = sub_tracks.position(si) { - // Subtitle packet: `SubTracks::decode` yields a cue for EVERY text track - // (not just the selected one) and decodes every image track while subtitles - // are on; see ff_subs.rs. Subtitles carry no ES -> never fed to the pipeline. - let decode_image = session.subtitle().selected_track() >= 0; - let bitmap_budget = session.subtitle().bitmap_budget(); - if let Some(cue) = sub_tracks.decode(sub_pos, pkt, *streams.add(si as usize), decode_image, bitmap_budget) { - session.subtitle().emit(ass_generation, cue); - } + // Subtitle packet: a cue for EVERY text track (not just the selected one), a + // decode of every image track while subtitles are on, and while they are Off + // an image packet is held instead of dropped, so a track chosen later has the + // read-ahead window to show (the release above); see `SubTracks::on_packet`. + // Subtitles carry no ES -> never fed to the pipeline. + sub_tracks.on_packet(sub_pos, pkt, fmt, session.subtitle(), ass_generation, + || observer.caption_retention()); av_packet_unref(pkt); } else { av_packet_unref(pkt); diff --git a/rust-modules/play-feed/src/ff_subs.rs b/rust-modules/play-feed/src/ff_subs.rs index 72caf50616..c47ffbf6e0 100644 --- a/rust-modules/play-feed/src/ff_subs.rs +++ b/rust-modules/play-feed/src/ff_subs.rs @@ -3,7 +3,166 @@ use super::*; use plx_media_codecs::subtitle_extract::ImageDecoders; -use plx_playback_contract::subtitle::{DecodeKind, RawCue, TrackInfo}; +use plx_playback_contract::demux::DemuxSubtitle; +use plx_playback_contract::subtitle::{CaptionRetention, DecodeKind, RawCue, TrackInfo}; +use std::collections::VecDeque; + +/// `AV_INPUT_BUFFER_PADDING_SIZE` of the FFmpeg this app bundles: the zeroed tail a decoder may +/// read past a packet's `size`. `ci/ffabi-assert.c` pins it against the bundled headers, so a bump +/// of the bundled FFmpeg that moves it fails the build until this follows. +const INPUT_PADDING: usize = 64; + +/// The segment type that opens a PGS display set (`pgssubdec.c`'s `PRESENTATION_SEGMENT`). The +/// others, in a set's order, are the window (0x17), palette (0x14) and object (0x15) segments and +/// the closing END (0x80). +const PGS_PRESENTATION_SEGMENT: u8 = 0x16; + +/// How a track's packets group into the display sets a decoder needs whole. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SetFraming { + /// One packet is one display set (Matroska, which carries a set per block, and VobSub / DVB + /// wherever they are muxed): the packet on screen at the playhead supersedes every one before it. + PerPacket, + /// PGS. A set is one packet in Matroska, but libavformat's MPEG-TS demuxer emits one packet per + /// PES, and a Blu-ray stream carries one segment per PES (PCS, WDS, PDS, ODS, END): the set on + /// screen begins at the newest packet that opens with a presentation segment, which can be + /// several packets behind the one at the playhead. + PgsSegments, +} + +impl SetFraming { + /// The framing of a track by the codec name `avcodec_get_name` gives it. + pub fn of_codec(codec_name: &str) -> Self { + if codec_name == "hdmv_pgs_subtitle" { Self::PgsSegments } else { Self::PerPacket } + } +} + +/// One image-subtitle packet the demuxer read while no subtitle was on, kept until one is. It owns +/// its bytes (the demuxer's packet is unreferenced as soon as the loop moves on) with the zero +/// padding libavcodec's decoders may read past `size`. +pub struct HeldPacket { + /// The packet's place in the demuxer's read order across every track, stamped by + /// [`ImageBacklog::hold`]; what [`ImageBacklog::take`] merges the tracks back by. + seq: u64, + time_ns: Option, + pts: i64, + dts: i64, + duration: i64, + flags: c_int, + size: usize, + bytes: Vec, +} + +impl HeldPacket { + /// `time_ns` is the packet's clock for retention (`pts_ns_opt`); the rest is what a decoder + /// reads off an `AVPacket`. + pub fn new(time_ns: Option, pts: i64, dts: i64, duration: i64, flags: c_int, payload: &[u8]) -> Self { + let mut bytes = Vec::with_capacity(payload.len() + INPUT_PADDING); + bytes.extend_from_slice(payload); + bytes.resize(payload.len() + INPUT_PADDING, 0); + Self { seq: 0, time_ns, pts, dts, duration, flags, size: payload.len(), bytes } + } + + /// Whether the payload opens with a PGS presentation segment: the start of a display set. + fn opens_pgs_display_set(&self) -> bool { + self.size > 0 && self.bytes[0] == PGS_PRESENTATION_SEGMENT + } +} + +/// What the demuxer read of each image track while subtitles were Off. Text tracks are observed +/// whatever the selection, so the cues of the read-ahead window are in the store the moment a text +/// track is chosen; an image packet had no such afterlife, and the window it covers (the demuxer runs +/// ahead of the playhead) was thrown away, leaving a gap as long as the read-ahead after an image +/// track was chosen mid-play. This keeps the packets instead, and the choice decodes them in the +/// order they were read (demux order, every track interleaved as the file has them), through the +/// decoder a live packet takes. +/// +/// Retention is the read-ahead window, with no cap of its own: per track, a display set is kept from +/// the one on screen at the playhead onward, because that one supersedes everything behind it for +/// the one thing a selection made now can show (the line on screen at the playhead, and what +/// follows). Where a set is a packet, that is the last packet at or before the playhead; where a +/// set is a run of segment packets ([`SetFraming::PgsSegments`]), it is the run from the PCS at or +/// before it. With the playhead unknown nothing is retired. A seek discards all of it. +pub struct ImageBacklog { + tracks: Vec>, + framing: Vec, + held: usize, + /// The read sequence number the next held packet takes. Never reset: it only has to grow. + next_seq: u64, +} + +impl ImageBacklog { + /// One track per entry of `framing`, in file order. + pub fn new(framing: Vec) -> Self { + Self { tracks: framing.iter().map(|_| VecDeque::new()).collect(), framing, held: 0, next_seq: 0 } + } + + pub fn is_empty(&self) -> bool { self.held == 0 } + + /// Keep `packet` as the newest of track `pos`, then retire what the playhead (`anchor_ns`, + /// [`CaptionRetention::anchor_ns`]) leaves superseded. + pub fn hold(&mut self, pos: usize, mut packet: HeldPacket, anchor_ns: Option) { + let Some(track) = self.tracks.get_mut(pos) else { return; }; + packet.seq = self.next_seq; + self.next_seq += 1; + track.push_back(packet); + self.held += 1; + let Some(anchor_ns) = anchor_ns else { return; }; + let Some(newest_behind) = track.iter().rposition(|held| held.time_ns.is_some_and(|t| t <= anchor_ns)) else { + return; + }; + let keep_from = match self.framing.get(pos) { + Some(SetFraming::PgsSegments) => { + // The newest packet at or before the playhead can be the END of the set on screen: + // that set began at its PCS. With no PCS behind the playhead what is held there is the + // tail of a set whose start was never held (a seek landed inside it), and there is + // no start to cut at until the next set's PCS arrives. + match track.iter().take(newest_behind + 1).rposition(HeldPacket::opens_pgs_display_set) { + Some(set_start) => set_start, + None => return, + } + } + _ => newest_behind, + }; + track.drain(..keep_from); + self.held -= keep_from; + } + + pub fn clear(&mut self) { + for track in &mut self.tracks { track.clear(); } + self.held = 0; + } + + /// Everything held, in the order the demuxer read it across all tracks, leaving the backlog + /// empty. The sequence number [`hold`](Self::hold) stamped is demux order itself (it needs no + /// timestamp, which a packet may lack), and each track's own queue is already in it, so this is + /// a stable merge of the tracks. The bitmap store the cues are published to is in demux order + /// and evicts by it: a track-grouped batch would break that. + pub fn take(&mut self) -> Vec<(usize, HeldPacket)> { + self.held = 0; + let mut out = Vec::new(); + for (pos, track) in self.tracks.iter_mut().enumerate() { + out.extend(track.drain(..).map(|packet| (pos, packet))); + } + out.sort_by_key(|(_, packet)| packet.seq); + out + } +} + +/// Stream `index` of the context's table AS IT IS NOW. The table is read through `fmt` each time, +/// with its bound, because a transport stream can grow streams while it is read +/// (`AVFMTCTX_NOHEADER`), which reallocates `streams`: the pointer taken at open is not the table +/// the live `nb_streams` describes. `None` for a null table or an index outside `0..nb_streams`. +/// Public so the real-demux fixtures in `plx_media` resolve a packet's stream the same bounded way +/// the demux loop does. +/// +/// # Safety +/// `fmt` must point to a live `AVFormatContext`. +pub unsafe fn live_stream(fmt: *mut AVFormatContext, index: c_int) -> Option<*mut AVStream> { + let streams = (*fmt).streams; + if streams.is_null() || index < 0 || index as c_uint >= (*fmt).nb_streams { return None; } + Some(*streams.add(index as usize)) +} /// File-order subtitle streams and their image decoders. The player supplies /// one decode plan entry per inspected stream before any decoder is opened. @@ -11,6 +170,7 @@ pub struct SubTracks { infos: Vec, plan: Vec, decoders: ImageDecoders, + held: ImageBacklog, } impl SubTracks { @@ -38,11 +198,12 @@ impl SubTracks { .map(|(info, kind)| (info.stream_index, matches!(kind, DecodeKind::Bitmap))) .collect(); let decoders = ImageDecoders::open(streams, &specs); - Self { infos, plan, decoders } + let held = ImageBacklog::new(infos.iter().map(|info| SetFraming::of_codec(&info.codec_name)).collect()); + Self { infos, plan, decoders, held } } pub fn empty() -> Self { - Self { infos: Vec::new(), plan: Vec::new(), decoders: ImageDecoders::empty() } + Self { infos: Vec::new(), plan: Vec::new(), decoders: ImageDecoders::empty(), held: ImageBacklog::new(Vec::new()) } } pub fn entries(&self) -> &[TrackInfo] { &self.infos } @@ -52,7 +213,8 @@ impl SubTracks { } /// Every text track is observed; image tracks are decoded while subtitles - /// are enabled, matching live track-switch behavior. + /// are enabled, matching live track-switch behavior. The demux loop reaches it through + /// [`on_packet`](Self::on_packet), which holds an image packet instead while they are not. pub unsafe fn decode( &self, pos: usize, pkt: *mut AVPacket, st: *mut AVStream, decode_image: bool, bitmap_budget: usize, @@ -75,5 +237,315 @@ impl SubTracks { } } - pub unsafe fn close(&mut self) { self.decoders.close(); } + /// The per-packet step of the demux loop for a subtitle stream's packet at `pos`: observe a text + /// track's cue, decode an image track's while a subtitle is on, and hold an image track's packet + /// while none is (so a track chosen later has the read-ahead window to show). Cues go to + /// `subtitle` under `generation`; `retention` is asked for only when a packet is kept, and bounds + /// what is already held. The caller still owns `pkt` and unreferences it. + /// + /// Run [`release_if_selected`](Self::release_if_selected) first, on every packet read: a + /// selection that lands between that check and this call waits for the next packet's, because + /// decoding this one now would put it ahead of the held ones and the cues must stay in file order. + /// + /// # Safety + /// `pkt` must be a live packet of the track at `pos`, read from `fmt`. + pub unsafe fn on_packet( + &mut self, pos: usize, pkt: *mut AVPacket, fmt: *mut AVFormatContext, + subtitle: &mut dyn DemuxSubtitle, generation: u64, + retention: impl FnOnce() -> Option, + ) { + let Some(st) = live_stream(fmt, (*pkt).stream_index) else { return; }; + let decode_image = subtitle.selected_track() >= 0 && !self.holds_images(); + let bitmap_budget = subtitle.bitmap_budget(); + if let Some(cue) = self.decode(pos, pkt, st, decode_image, bitmap_budget) { + subtitle.emit(generation, cue); + } + if !decode_image { self.hold(pos, pkt, st, retention); } + } + + /// Run on EVERY packet the demuxer reads, whatever its stream, before the packet is looked at: + /// if a subtitle was turned on since image packets were held, decode them now (in the order they + /// were read) and hand the cues to `subtitle`, so they precede the cues of the packet that + /// comes next. + /// + /// # Safety + /// `fmt` must be the format context the tracks were opened from. + pub unsafe fn release_if_selected( + &mut self, fmt: *mut AVFormatContext, subtitle: &mut dyn DemuxSubtitle, generation: u64, + ) { + if !self.holds_images() || subtitle.selected_track() < 0 { return; } + let bitmap_budget = subtitle.bitmap_budget(); + self.release_held(fmt, bitmap_budget, |cue| subtitle.emit(generation, cue)); + } + + /// Keep an image track's packet for [`release_held`](Self::release_held) when `decode` was + /// not allowed to decode it (a text track's packet is ignored: `decode` already observed it). + unsafe fn hold( + &mut self, pos: usize, pkt: *const AVPacket, st: *mut AVStream, + retention: impl FnOnce() -> Option, + ) { + if !matches!(self.plan.get(pos), Some(DecodeKind::Bitmap)) { return; } + if (*pkt).data.is_null() || (*pkt).size <= 0 { return; } + let payload = std::slice::from_raw_parts((*pkt).data, (*pkt).size as usize); + let packet = HeldPacket::new( + pts_ns_opt(pkt, st), (*pkt).pts, (*pkt).dts, (*pkt).duration, (*pkt).flags, payload); + self.held.hold(pos, packet, retention().and_then(CaptionRetention::anchor_ns)); + } + + /// Whether any image packet waits for a subtitle to be turned on. + pub fn holds_images(&self) -> bool { !self.held.is_empty() } + + /// Decode everything [`hold`](Self::hold) kept, in the order it was read, and hand every cue to + /// `emit` exactly as a live packet's would be. If the scratch packet cannot be allocated nothing + /// is taken: the backlog stays held and the next packet's check retries. + unsafe fn release_held( + &mut self, fmt: *mut AVFormatContext, bitmap_budget: usize, mut emit: impl FnMut(RawCue), + ) { + if self.held.is_empty() { return; } + let mut scratch = av_packet_alloc(); + if scratch.is_null() { + plx_base::eventlog::log("ff: image-subtitle release deferred, av_packet_alloc failed"); + return; + } + let held = self.held.take(); + for (pos, packet) in &held { + let Some(info) = self.infos.get(*pos) else { continue; }; + let Some(st) = live_stream(fmt, info.stream_index) else { continue; }; + // The scratch packet points at the owned bytes and owns nothing (no `buf`), so the + // decode reads them in place and `av_packet_free` below releases no payload. + (*scratch).data = packet.bytes.as_ptr() as *mut u8; + (*scratch).size = packet.size as c_int; + (*scratch).pts = packet.pts; + (*scratch).dts = packet.dts; + (*scratch).duration = packet.duration; + (*scratch).flags = packet.flags; + (*scratch).stream_index = info.stream_index; + if let Some(cue) = self.decode(*pos, scratch, st, true, bitmap_budget) { emit(cue); } + } + (*scratch).data = std::ptr::null_mut(); + (*scratch).size = 0; + av_packet_free(&mut scratch); + } + + /// Forget what is held: a seek, after which none of it belongs to the playhead. + pub fn discard_held(&mut self) { self.held.clear(); } + + pub unsafe fn close(&mut self) { + self.held.clear(); + self.decoders.close(); + } +} + +#[cfg(test)] +mod backlog_tests { + use super::*; + + const SEC: i64 = 1_000_000_000; + + const PCS: u8 = 0x16; + const WDS: u8 = 0x17; + const PDS: u8 = 0x14; + const ODS: u8 = 0x15; + const END: u8 = 0x80; + + fn per_packet(tracks: usize) -> ImageBacklog { ImageBacklog::new(vec![SetFraming::PerPacket; tracks]) } + + fn pgs() -> ImageBacklog { ImageBacklog::new(vec![SetFraming::PgsSegments]) } + + /// One PGS segment as its own packet, the way an MPEG-TS PES carries it (type, then a length and + /// a body this backlog never reads). + fn segment(sec: i64, kind: u8) -> HeldPacket { + HeldPacket::new(Some(sec * SEC), sec * SEC, sec * SEC, 0, 0, &[kind, 0, 1, 0]) + } + + /// A display set sent segment by segment, all stamped `sec`. + fn set(sec: i64) -> Vec { [PCS, WDS, PDS, ODS, END].map(|kind| segment(sec, kind)).into() } + + /// The first payload byte of every packet held on track 0, in order. + fn kinds(backlog: &ImageBacklog) -> Vec { backlog.tracks[0].iter().map(|held| held.bytes[0]).collect() } + + fn packet(sec: i64) -> HeldPacket { packet_at(Some(sec * SEC)) } + + fn packet_at(time_ns: Option) -> HeldPacket { + HeldPacket::new(time_ns, time_ns.unwrap_or(0), time_ns.unwrap_or(0), 0, 0, &[1, 2, 3]) + } + + fn times(backlog: &ImageBacklog, pos: usize) -> Vec> { + backlog.tracks[pos].iter().map(|packet| packet.time_ns).collect() + } + + fn secs(backlog: &ImageBacklog, pos: usize) -> Vec { + times(backlog, pos).into_iter().map(|t| t.unwrap() / SEC).collect() + } + + /// The reported bug: subtitles Off, an image track chosen at 232 s with the demuxer ten seconds + /// ahead. Everything the track carried from the line on screen onward is still there. + #[test] + fn the_line_on_screen_at_the_playhead_and_the_read_ahead_survive() { + let mut backlog = per_packet(2); + for sec in [200, 210, 225, 228, 231, 235, 240] { + backlog.hold(1, packet(sec), Some(232 * SEC)); + } + assert_eq!(secs(&backlog, 1), [231, 235, 240], "from the last packet at or before 232 s"); + assert!(!backlog.is_empty()); + assert_eq!(secs(&backlog, 0), Vec::::new(), "another track holds nothing of this one's"); + } + + #[test] + fn a_packet_exactly_at_the_playhead_supersedes_the_ones_before_it() { + let mut backlog = per_packet(1); + for sec in [10, 20, 30] { backlog.hold(0, packet(sec), Some(20 * SEC)); } + assert_eq!(secs(&backlog, 0), [20, 30]); + } + + #[test] + fn nothing_is_retired_while_the_playhead_is_unknown() { + let mut backlog = per_packet(1); + for sec in [10, 20, 30, 40] { backlog.hold(0, packet(sec), None); } + assert_eq!(secs(&backlog, 0), [10, 20, 30, 40], "everything since the last seek"); + } + + #[test] + fn a_packet_ahead_of_the_playhead_supersedes_nothing() { + let mut backlog = per_packet(1); + for sec in [40, 50, 60] { backlog.hold(0, packet(sec), Some(10 * SEC)); } + assert_eq!(secs(&backlog, 0), [40, 50, 60]); + } + + #[test] + fn the_playhead_moving_on_retires_what_it_passed_at_the_next_packet() { + let mut backlog = per_packet(1); + for sec in [10, 20, 30] { backlog.hold(0, packet(sec), Some(5 * SEC)); } + assert_eq!(secs(&backlog, 0), [10, 20, 30]); + backlog.hold(0, packet(40), Some(32 * SEC)); + assert_eq!(secs(&backlog, 0), [30, 40]); + } + + #[test] + fn a_packet_with_no_time_never_supersedes_and_goes_when_a_timed_one_does() { + let mut backlog = per_packet(1); + backlog.hold(0, packet(10), Some(15 * SEC)); + backlog.hold(0, packet_at(None), Some(15 * SEC)); + backlog.hold(0, packet(20), Some(15 * SEC)); + assert_eq!(times(&backlog, 0), [Some(10 * SEC), None, Some(20 * SEC)]); + backlog.hold(0, packet(30), Some(25 * SEC)); + assert_eq!(secs(&backlog, 0), [20, 30]); + } + + #[test] + fn take_returns_every_track_interleaved_in_demux_order_and_empties_the_backlog() { + let mut backlog = per_packet(3); + backlog.hold(2, packet(1), None); + backlog.hold(0, packet(2), None); + backlog.hold(2, packet(3), None); + backlog.hold(0, packet(4), None); + let order: Vec<(usize, Option)> = backlog.take().into_iter() + .map(|(pos, held)| (pos, held.time_ns.map(|t| t / SEC))).collect(); + assert_eq!(order, [(2, Some(1)), (0, Some(2)), (2, Some(3)), (0, Some(4))]); + assert!(backlog.is_empty()); + assert!(backlog.take().is_empty()); + } + + /// The read order, not the timestamps, is what is kept: a packet with no time, or one stamped out + /// of step with its neighbours', stays where the demuxer read it, and retiring behind the playhead + /// leaves the survivors in that order. + #[test] + fn take_keeps_read_order_for_untimed_and_out_of_order_stamps_and_after_retirement() { + let mut backlog = per_packet(2); + backlog.hold(1, packet(50), None); + backlog.hold(0, packet_at(None), None); + backlog.hold(1, packet(10), None); + backlog.hold(0, packet(5), None); + let order: Vec<(usize, Option)> = backlog.take().into_iter() + .map(|(pos, held)| (pos, held.time_ns.map(|t| t / SEC))).collect(); + assert_eq!(order, [(1, Some(50)), (0, None), (1, Some(10)), (0, Some(5))]); + + let mut backlog = per_packet(2); + for (pos, sec) in [(0, 10), (1, 11), (0, 20), (1, 21), (0, 30), (1, 31)] { + backlog.hold(pos, packet(sec), Some(20 * SEC)); + } + let order: Vec<(usize, i64)> = backlog.take().into_iter() + .map(|(pos, held)| (pos, held.time_ns.unwrap() / SEC)).collect(); + assert_eq!(order, [(1, 11), (0, 20), (1, 21), (0, 30), (1, 31)], + "track 0 retires its 10 s packet at 20 s; track 1's 11 s one is the newest at or before the playhead"); + } + + #[test] + fn clear_forgets_everything_and_a_position_outside_the_plan_is_ignored() { + let mut backlog = per_packet(1); + backlog.hold(0, packet(1), None); + backlog.hold(7, packet(2), None); + assert!(!backlog.is_empty()); + backlog.clear(); + assert!(backlog.is_empty()); + assert!(backlog.take().is_empty()); + } + + #[test] + fn a_held_packet_carries_its_bytes_and_the_decoder_padding() { + let held = HeldPacket::new(Some(1), 1, 1, 0, 0, &[9, 8, 7]); + assert_eq!(&held.bytes[..held.size], [9, 8, 7]); + assert_eq!(held.bytes.len(), 3 + INPUT_PADDING); + assert!(held.bytes[3..].iter().all(|&b| b == 0)); + } + + /// A PGS set that reaches the demuxer one segment per packet (MPEG-TS, M2TS): the set on screen + /// at the playhead is kept from its PCS to its END, not from its last packet. + #[test] + fn a_pgs_set_on_screen_at_the_playhead_is_kept_whole_from_its_pcs() { + let mut backlog = pgs(); + for sec in [200, 210, 240] { + for packet in set(sec) { backlog.hold(0, packet, Some(232 * SEC)); } + } + assert_eq!(kinds(&backlog), [PCS, WDS, PDS, ODS, END, PCS, WDS, PDS, ODS, END], + "the set shown at 232 s (stamped 210 s) whole, then the read-ahead"); + assert_eq!(secs(&backlog, 0), [210, 210, 210, 210, 210, 240, 240, 240, 240, 240]); + } + + #[test] + fn a_pgs_set_straddling_the_playhead_is_kept_from_its_pcs() { + let mut backlog = pgs(); + for packet in set(200) { backlog.hold(0, packet, Some(231 * SEC)); } + backlog.hold(0, segment(231, PCS), Some(232 * SEC)); + backlog.hold(0, segment(231, PDS), Some(232 * SEC)); + backlog.hold(0, segment(233, ODS), Some(232 * SEC)); + backlog.hold(0, segment(233, END), Some(232 * SEC)); + assert_eq!(kinds(&backlog), [PCS, PDS, ODS, END], "the 200 s set is superseded, the 231 s one is whole"); + } + + #[test] + fn a_pgs_track_with_no_pcs_behind_the_playhead_retires_nothing() { + let mut backlog = pgs(); + for kind in [ODS, END] { backlog.hold(0, segment(200, kind), Some(232 * SEC)); } + assert_eq!(kinds(&backlog), [ODS, END], "the tail of a set whose start was never held is not a place to cut"); + for packet in set(210) { backlog.hold(0, packet, Some(232 * SEC)); } + assert_eq!(kinds(&backlog), [PCS, WDS, PDS, ODS, END], "the next set's PCS is"); + } + + /// Matroska carries a display set per packet, so every PGS packet there opens with its PCS and + /// the rule is the same one the other image codecs follow. + #[test] + fn a_pgs_set_in_one_packet_is_retired_like_any_other() { + let mut backlog = pgs(); + for sec in [200, 210, 225, 228, 231, 235, 240] { + backlog.hold(0, HeldPacket::new(Some(sec * SEC), sec, sec, 0, 0, &[PCS, 0, 1, 0, END]), Some(232 * SEC)); + } + assert_eq!(secs(&backlog, 0), [231, 235, 240]); + } + + /// VobSub and DVB carry a packet per set: a packet that happens to open with 0x16 is no PCS there. + #[test] + fn a_track_that_is_not_pgs_keeps_the_packet_at_the_playhead_not_a_segment_start() { + let mut backlog = per_packet(1); + for packet in set(210) { backlog.hold(0, packet, Some(232 * SEC)); } + assert_eq!(kinds(&backlog), [END], "the last packet at or before the playhead, as before"); + } + + #[test] + fn framing_follows_the_codec_name_ffmpeg_gives_a_track() { + assert_eq!(SetFraming::of_codec("hdmv_pgs_subtitle"), SetFraming::PgsSegments); + for codec in ["dvd_subtitle", "dvb_subtitle", "subrip", "ass"] { + assert_eq!(SetFraming::of_codec(codec), SetFraming::PerPacket, "{codec}"); + } + } } diff --git a/rust-modules/play-feed/src/lib.rs b/rust-modules/play-feed/src/lib.rs index d5be978b6b..9ce759e6c8 100644 --- a/rust-modules/play-feed/src/lib.rs +++ b/rust-modules/play-feed/src/lib.rs @@ -51,7 +51,7 @@ pub mod ff_test_support { // The counters the diag sample reads outside the mailboxes: a fresh session's are zero. store_diag_counters(0, 0, false); } - pub use crate::ff::subs::SubTracks; + pub use crate::ff::subs::{live_stream, SubTracks}; pub use crate::ff::{annexb_start, arm_active_stall_guard, candidate_reserve_deadline, classify_curl_open_err, classify_hls_avio_facts, classify_hls_deadline, classify_plaintext_open_failure, drain_keep_polling, diff --git a/rust-modules/playback-contract/src/subtitle.rs b/rust-modules/playback-contract/src/subtitle.rs index 36415b3bcd..d0efdf2873 100644 --- a/rust-modules/playback-contract/src/subtitle.rs +++ b/rust-modules/playback-contract/src/subtitle.rs @@ -126,6 +126,20 @@ pub struct CaptionRetention { pub seek_target_ns: Option, } +impl CaptionRetention { + /// The time every retention rule measures from: the playhead, or the seek target while that is + /// earlier. The demuxer can already be reading at a backward seek's target while the native + /// clock still reports the old picture, and whatever it reads there must survive the rebase. + /// `None` when the playhead is unknown: nothing may then be retired by time. + pub fn anchor_ns(self) -> Option { + let playhead = self.playhead_ns?; + Some(match self.seek_target_ns { + Some(target) if target >= 0 => playhead.min(target), + _ => playhead, + }) + } +} + /// The ordinal of a side-reader run that reads no subtitle and only measures the remux's clock /// against the Part's (a sidecar is drawn by the app). Negative, so it is never a track position and /// two runs of different roles never compare equal when a reader is asked to switch. @@ -293,6 +307,24 @@ impl std::fmt::Debug for Spec { } } +#[cfg(test)] +mod retention_tests { + use super::*; + + fn retention(playhead_ns: Option, seek_target_ns: Option) -> CaptionRetention { + CaptionRetention { playhead_ns, seek_target_ns } + } + + #[test] + fn the_anchor_is_the_playhead_or_the_earlier_seek_target() { + assert_eq!(retention(Some(90), None).anchor_ns(), Some(90)); + assert_eq!(retention(Some(90), Some(10)).anchor_ns(), Some(10), "a backward seek reads at its target"); + assert_eq!(retention(Some(10), Some(90)).anchor_ns(), Some(10), "a forward seek still shows the old picture"); + assert_eq!(retention(Some(90), Some(-1)).anchor_ns(), Some(90), "a negative target is no target"); + assert_eq!(retention(None, Some(10)).anchor_ns(), None, "an unknown playhead justifies no pruning"); + } +} + #[cfg(test)] mod spec_tests { use super::*; From 038e8e992774baf336a16b89d417753c770baf28 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sat, 14 Nov 2026 21:45:42 +0300 Subject: [PATCH 4/8] ACB: create the URI engine's handle at the first Experimental offer, not at boot (#1396) Since #1281 the app created a second ACB handle, player type 0 for the URI engine, at every boot, whatever the viewer's Playback engine. On an LG C8 (webOS 4.4.3) running 0.10.0, every Feed Load then returned ~30 s late and every play failed load_timeout. The reporter's build without that handle played. The firmware does not prove the link: the 30 s is libplayerAPIs' synchronous videosinkmanager/private/register call, and the code tying it to the handle was not decompiled. The handle is only ever needed by the Experimental engine. A boot now creates only Feed's handle (player type 10, with the same calls and log lines as before). The plane owner creates the URI engine's handle on demand: - Event::HandleWanted -> Fx::Create, executed by PlaneOwner::ensure_handle on the main thread; - at the first Experimental offer (UriDriver::ready_to_start, asked from offer_qualified_uri); - or, while Feed's off-frame Unload is still in flight, at the Start's gate after it. A create that fails at the offer declines it ("uri: original declined (the ACB handle for the URI engine could not be created)"), so Feed plays the title. The failure is recorded and later offers decline without retrying. A late boot record no longer resets a bound or releasing plane. The composition line reads handle=deferred on an ACB display and handle=none elsewhere; the binding result is unchanged. Host tests (red first) cover: - a boot that creates only Feed's handle; - the create at the offer, and no second create; - a declined offer handing the play to Feed; - no create during a release; - a Standard viewer creating nothing. On the webOS 4.5 dev set, a Standard boot logged only `acb handle=feed`, and Feed's Load returned in 677 ms. The production URI case logged `acb handle=uri ptype=0 create=2` once, at the offer, and played 70 s bound. The C8's stall itself is not reproduced on that set. Closes #1394 Co-Authored-By: Claude Opus 5.5 --- docs/uri-mode.md | 9 +- rust-modules/media/src/player/CLAUDE.md | 2 +- rust-modules/media/src/player/adapter.rs | 103 +++++++ rust-modules/media/src/player/mod.rs | 5 +- rust-modules/platform/src/tv/plane.rs | 262 ++++++++++++++++-- rust-modules/platform/src/tv/sink.rs | 39 ++- rust-modules/play-uri/src/acb_bind.rs | 2 +- rust-modules/play-uri/src/acb_drive.rs | 38 ++- rust-modules/play-uri/src/backend.rs | 194 ++++++++++--- rust-modules/play-uri/src/uriprobe.rs | 2 +- rust-modules/play-uri/src/uriprobe_drive.rs | 2 +- .../playback-contract/src/test_support.rs | 14 + rust-modules/playback-contract/src/uri.rs | 22 ++ rust-modules/src/app/adapters/backends.rs | 133 ++++----- rust-modules/src/app/run_uri_boot_tests.rs | 159 +++++++++++ src/starfish.h | 2 +- src/video_plane.c | 16 +- tests/README.md | 2 +- tests/manifest.json | 4 +- tests/run.py | 6 +- tests/test_harness.py | 2 +- 21 files changed, 822 insertions(+), 196 deletions(-) diff --git a/docs/uri-mode.md b/docs/uri-mode.md index a3582d9e29..a389f963af 100644 --- a/docs/uri-mode.md +++ b/docs/uri-mode.md @@ -230,9 +230,11 @@ an entry plays Standard, with no handoff. 2. Arm a variant in the install's runtime root: `echo load > /tmp/com.beb.plxnative.debug/plxnative-uriprobe` (empty, `load`, `late`, `none`, or `drive`). The separate controlled `acb-control` and `acb-dass` arms need no `ptype` trigger since the - boot gave the URI engine its own ACB handle (player type 0, created beside Feed's player type 10; - the boot log names both: `acb handle=uri ptype=0 create=.. initialized=true`, and `uri: product - binding=Acb ..` at composition), but the harness cases keep arming `ptype=0` as the + URI engine has its own ACB handle (player type 0), which the offer of an Experimental play's source (or the probe's gate) creates on + demand (the boot creates only Feed's, player type 10: `acb handle=feed ptype=10 ..`; the log names + the URI engine's `acb handle=uri ptype=0 create=.. initialized=true` when the probe or an + Experimental play first needs it, and `uri: product binding=Acb .. handle=deferred ..` at + composition), but the harness cases keep arming `ptype=0` as the 2026-11-02 measurements did (it now sets Feed's handle type, which those arms do not bind), and the fixture constraints in the measurement linked above. The 2026-11-02 pair each ran in a fresh process; a second arm in the same process binds only after the previous release's fence is proven. A `plex.direct` URL needs @@ -401,6 +403,7 @@ that fails over from the URI engine to Standard writes the `already tried` line | `subtitle streams keep a play in Standard today; try a file without subtitle streams` | a subtitle is selected and the certificate does not carry it: no qualifier certifies a subtitle render yet, so every selected embedded subtitle ends here (by design, not a bug) | a file without subtitle streams | | `no exported-window evidence for this firmware` | the display path has no evidence on this firmware (no exported window with a canvas, no ACB plane) | none: report the `uri: product binding=... major=...` boot line and the `devcaps: firmware=... build=...` line | | `the qualifier did not certify the file` | a developer qualifier declined it | none | +| `the ACB handle for the URI engine could not be created` | on a webOS 4.x ACB display the offer makes the URI engine's own handle (player type 0) and its create or initialize failed; the failure is recorded, so every later offer in this run declines at once | none: the play goes to Standard; report it with the `acb handle=uri` line | The reasons are local diagnostics: they route nothing, and nothing about them is sent to telemetry. diff --git a/rust-modules/media/src/player/CLAUDE.md b/rust-modules/media/src/player/CLAUDE.md index 6f4c6fd87b..39244560b2 100644 --- a/rust-modules/media/src/player/CLAUDE.md +++ b/rust-modules/media/src/player/CLAUDE.md @@ -44,7 +44,7 @@ accepted pause/resume to the plane once the bind has completed. Release is `UNLO plane owner's fence on that call's completion callback, pumped from the backend's `poll` (`docs/measurements/acb-release-fence-2026-11-04.md`); a quarantine until process exit is only the outcome of a failed fence, not the normal one. `plx_play_uri::backend::product_binding` selects it at -composition (ACB display mode and the URI engine's own boot handle, initialized, player type 0: the boot creates it beside Feed's player type 10 handle, each once and never destroyed, and the plane owner binds one at a time across both); media's `uri_preflight` only receives the result as +composition (an ACB display; the URI engine's own handle, player type 0, is created ON DEMAND when the first source is OFFERED (`UriDriver::ready_to_start`, asked by `offer_qualified_uri`, runs `PlaneOwner::ensure_handle`; the driver's gate `Op::Check` makes it if nothing did), while the boot creates only Feed's player type 10 handle, each created once and never destroyed, and the plane owner binds one at a time across both; a create or initialize that fails DECLINES the offer, `uri: original declined (the ACB handle for the URI engine could not be created)`, and Feed plays the title; the failure is recorded, so a later offer declines without a new attempt, and the Start's own gate stays the backstop); media's `uri_preflight` only receives the result as `ProductEvidence::unqualified_on(.., binding)`. `ProductEvidence::permits` and `ResolvePort::could_qualify_pms` accept `Binding::Acb` as an offerable display path beside an exported canvas, but only through `certified` capabilities: the production evidence certifies nothing and diff --git a/rust-modules/media/src/player/adapter.rs b/rust-modules/media/src/player/adapter.rs index 6e8b522335..3b67377c40 100644 --- a/rust-modules/media/src/player/adapter.rs +++ b/rust-modules/media/src/player/adapter.rs @@ -194,6 +194,95 @@ mod qualified_uri_offer_tests { audio_track: audio, subtitle_track: TrackChoice::Unspecified } } + /// #1394: the driver is asked whether it can take a Start when the source is offered. One that + /// cannot (the ACB handle was not made) declines the offer with its reason, keeps no + /// authorization and leaves Feed as the offer; it is asked again for the next offer. + #[test] + fn an_offer_the_driver_is_not_ready_for_is_declined_with_its_reason() { + let _serial = plx_base::testlock::serial(); + let mut session = crate::route::PlaybackSession::IDLE; + crate::route::reset_player_control_for_test(&session); + crate::route::set_url(&mut session, "https://example.invalid/original.mkv"); + let ticket = crate::route::begin_route_start().expect("route transaction"); + assert!(session.qualify_resolved_feed_offer( + crate::route::backend_policy::OfferId::FEED_ORIGINAL, + crate::route::CandidatePurpose::NewCandidate)); + let mut adapter = PlayerAdapter::new_fed(unsafe { MainThread::assume() }); + let (driver, handle) = FakeUriDriver::new(Capabilities { progressive_uri: true, + hls_uri: false, packet_feed: false, pause: true, seek: true, audio_track: true, + subtitle_track: true, audio_off: false, subtitle_off: false }); + assert!(adapter.install_uri_driver(Box::new(driver)).is_ok()); + let selection = session.uri_selection(); + let initial = InitialPlayback { position: None, paused: false }; + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + let offer = || QualifiedUriOffer::new( + crate::route::backend_policy::OfferId::URI_ORIGINAL, + UriSourceRecipe::OriginalPart, ticket, selection, + UriSource::new("https://example.invalid/original.mkv".to_owned(), vec![]), + requirements(TrackChoice::Unspecified), initial, 60_000, deadline).expect("typed offer"); + + handle.set_not_ready(Some(plx_playback_contract::uri::UriNotReady::AcbHandle)); + let (declined, log) = plx_base::eventlog::with_private_log(|| { + let declined = adapter.offer_qualified_uri(&mut session, offer()); + (declined, std::fs::read_to_string(plx_base::eventlog::events_log()).unwrap_or_default()) + }); + assert!(matches!(declined, Err(Failure::Unavailable))); + assert!(log.contains("uri: original declined (the ACB handle for the URI engine could not be created)\n"), + "{log}"); + assert!(adapter.qualified_uri.is_none(), "a declined offer keeps no authorization"); + assert!(matches!(session.backend_offer(), + crate::route::backend_policy::Selection::Open(offer) if offer.backend == BackendKind::Feed), + "Feed stays the offer"); + assert_eq!(handle.ready_asked(), 1); + + handle.set_not_ready(None); + assert!(adapter.offer_qualified_uri(&mut session, offer()).is_ok()); + assert_eq!(handle.ready_asked(), 2, "each offer asks the driver"); + } + + /// The previous Feed object may still be inside its off-frame `Unload` when an offer lands, and no + /// create may run against the display then. While a release is in flight the driver is not asked to + /// set anything up: the offer is taken, and the Start's gate makes the handle once the release is + /// done. After it, the next offer asks again. + #[test] + fn an_offer_while_a_release_is_in_flight_is_taken_without_asking_the_driver() { + use crate::player::release::ReleaseEvent; + let _serial = plx_base::testlock::serial(); + let mut session = crate::route::PlaybackSession::IDLE; + crate::route::reset_player_control_for_test(&session); + crate::route::set_url(&mut session, "https://example.invalid/original.mkv"); + let ticket = crate::route::begin_route_start().expect("route transaction"); + assert!(session.qualify_resolved_feed_offer( + crate::route::backend_policy::OfferId::FEED_ORIGINAL, + crate::route::CandidatePurpose::NewCandidate)); + let mut adapter = PlayerAdapter::new_fed(unsafe { MainThread::assume() }); + let (driver, handle) = FakeUriDriver::new(Capabilities { progressive_uri: true, + hls_uri: false, packet_feed: false, pause: true, seek: true, audio_track: true, + subtitle_track: true, audio_off: false, subtitle_off: false }); + assert!(adapter.install_uri_driver(Box::new(driver)).is_ok()); + let selection = session.uri_selection(); + let initial = InitialPlayback { position: None, paused: false }; + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + let offer = || QualifiedUriOffer::new( + crate::route::backend_policy::OfferId::URI_ORIGINAL, + UriSourceRecipe::OriginalPart, ticket, selection, + UriSource::new("https://example.invalid/original.mkv".to_owned(), vec![]), + requirements(TrackChoice::Unspecified), initial, 60_000, deadline).expect("typed offer"); + + // A driver that would decline if it were asked. + handle.set_not_ready(Some(plx_playback_contract::uri::UriNotReady::AcbHandle)); + let mut fx = Vec::new(); + adapter.release.step(&ReleaseEvent::UnloadHandedOff { epoch: 1, at: std::time::Instant::now() }, &mut fx); + assert!(adapter.release_busy()); + assert!(adapter.offer_qualified_uri(&mut session, offer()).is_ok(), "the offer is taken"); + assert_eq!(handle.ready_asked(), 0, "nothing is created while Feed's Unload runs"); + + adapter.release.step(&ReleaseEvent::UnloadReturned { epoch: 1 }, &mut fx); + assert!(!adapter.release_busy()); + assert!(matches!(adapter.offer_qualified_uri(&mut session, offer()), Err(Failure::Unavailable))); + assert_eq!(handle.ready_asked(), 1, "once the release is done the offer asks again"); + } + #[test] fn qualified_source_is_bound_to_resolved_selection_and_never_inferred_from_feed() { let _serial = plx_base::testlock::serial(); @@ -1073,6 +1162,20 @@ impl PlayerAdapter { } if !uri_offer_executable(ps, &offer) { return Err(Failure::Unsupported); } if !ps.ensure_backend_offers() { return Err(Failure::Stale); } + // Last, because it may do the driver's one-time setup (the ACB handle): every cheaper + // refusal above has already declined. A driver that cannot take the Start declines the + // offer, and the caller hands the same play to Feed. Not while a release is in flight: the + // previous Feed object may still be inside its off-frame `Unload`, and no create may run + // against the display then (Feed's own sink fence refuses it, and a Start waits for the + // `Unload` for the same reason). The offer is taken and the Start's gate makes the handle once + // the release is done; a create that fails THEN refuses that Start. + let mt = &self.mt; + let busy = self.release.is_busy(); + if let Some(why) = self.physical.uri_mut().filter(|_| !busy) + .and_then(|driver| driver.ready_to_start(mt).err()) { + plx_base::eventlog::log(&format!("uri: original declined ({})", why.log_name())); + return Err(Failure::Unavailable); + } let offer_id = offer.offer_id; if let Some(previous) = self.qualified_uri.replace(offer) { ps.qualify_backend_offer(BackendKind::Uri, previous.offer_id, diff --git a/rust-modules/media/src/player/mod.rs b/rust-modules/media/src/player/mod.rs index 684fdb4063..c7879bdbe0 100644 --- a/rust-modules/media/src/player/mod.rs +++ b/rust-modules/media/src/player/mod.rs @@ -1694,8 +1694,9 @@ pub struct Diag { pub feed_composed: bool, pub vp_mode: c_int, pub window_id: String, - /// This boot created an ACB handle (either backend's): a URI-only build has the URI engine's with - /// Feed's absent, and its diagnostics must not read that as a missing video plane. + /// An ACB handle has been created (either backend's; Feed's at the boot, the URI engine's at the + /// first Experimental offer or, while a release is in flight, at the Start's gate). A URI-only ACB + /// build therefore reads false until then. pub acb_ok: bool, pub place_rv: i32, pub placed_w: i32, diff --git a/rust-modules/platform/src/tv/plane.rs b/rust-modules/platform/src/tv/plane.rs index ae525b3b89..d54e265c6f 100644 --- a/rust-modules/platform/src/tv/plane.rs +++ b/rust-modules/platform/src/tv/plane.rs @@ -2,9 +2,11 @@ //! its ACB handles, and a release that waits for the firmware's completion callback. //! //! **Why.** A handle is created and initialized once per process (`AcbAPI_create` + -//! `AcbAPI_initialize`; a repeat initialize returns 0). The boot makes up to two, one per backend +//! `AcbAPI_initialize`; a repeat initialize returns 0). There are up to two, one per backend //! ([`PlaneHandle`]: Feed's, player type 10, and the URI engine's, player type 0), each created once -//! and never destroyed. Two cores in one process are independent (each has its own anonymous bus +//! and never destroyed. The boot creates Feed's; the URI engine's is created on demand, when the first +//! source the route would send to it is offered (an Experimental viewer's play; the Start's gate makes it if nothing did), so a viewer +//! on Standard never has a second core (#1394). Two cores in one process are independent (each has its own anonymous bus //! client, callback, appId, sink and type; `/Volumes/DAS/lgfw/reports/acb-two-cores/report.md`), so //! this owner keeps the invariant that matters on top of them: ONE binding at a time, across both. //! `setMediaId` may be called again on the same @@ -28,14 +30,18 @@ //! //! **Two handles.** The machine knows only roles ([`PlaneHandle`]); the owner maps a role to the id //! the sink's `plane_create` returned and, when it grants a bind, makes that handle the one the sink's -//! verbs address ([`DisplaySink::plane_select`]). A bind of one handle while the other holds the +//! verbs address ([`DisplaySink::plane_select`]). A handle's creation is an effect of the machine +//! ([`Event::HandleWanted`] answers [`Fx::Create`] until that handle's boot is recorded, whatever it +//! found), which [`PlaneOwner::ensure_handle`] runs through the sink on the main thread and answers +//! with the handle's own boot event; a failed create is recorded too and is not retried. A bind of one handle while the other holds the //! plane is refused, and while the other's release is in flight it waits for that release's UNLOADED, //! exactly as a second bind of one handle always did. Both handles' sinks are pinned to MAIN by the //! bind's first verb before their first bind (`setSinkType` in `acb_media_id`): the firmware //! recomputes the sink of every un-pinned object of an app on a foreground change, so an un-pinned //! handle could have its sink rewritten by the other's foreground transition. The released handle //! stays registered (UNLOADED) while the other is bound, as the owner has always left a handle -//! between titles. +//! between titles. The sink makes the newest handle current, so a handle created after the owner has +//! selected one puts that one back (and a boot, which has selected none, leaves the newest current). //! //! **Shape.** [`Plane`] is the pure machine (no clock, no global, no IO: time arrives as `now_ms` //! in events, work leaves as [`Fx`]). [`PlaneOwner`] is its runner: the single holder of the @@ -46,7 +52,8 @@ //! (`webos::video_sink`), which is the only place that has a library thread to receive from. use super::sink::{DisplaySink, PlaneFinish, PlaneUnload}; use plx_base::task::MainThread; -use std::os::raw::c_long; +use std::ffi::CStr; +use std::os::raw::{c_int, c_long}; use std::collections::VecDeque; use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; use std::sync::{Mutex, MutexGuard}; @@ -70,7 +77,7 @@ pub const DEFAULT_PLAYER_TYPE: i32 = 10; /// on (`play-uri`'s `acb_bind::gate`), the sequence the device measurements ran. pub const URI_PLAYER_TYPE: i32 = 0; -/// Which of the boot's ACB handles a binding runs over. +/// Which of the process's ACB handles a binding runs over. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum PlaneHandle { /// Buffer-feed's handle: [`DEFAULT_PLAYER_TYPE`] unless `/tmp/plxnative-ptype` says otherwise. @@ -97,7 +104,7 @@ impl std::fmt::Display for PlaneHandle { } } -/// What the boot learned about one ACB handle. Owned by the [`PlaneOwner`] from the `Boot` event on +/// What was learned when one ACB handle was created. Owned by the [`PlaneOwner`] from the `Boot` event on /// (the Feed handle's is what [`PlaneOwner::facts`] answers); it replaces the old `ACB_OK`/`PTYPE` /// statics. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -210,6 +217,9 @@ pub enum Event { Boot { facts: PlaneFacts }, /// The URI handle's boot. BootUri { facts: PlaneFacts }, + /// A backend is about to bind `handle` and needs it to exist. Answered with [`Fx::Create`] only + /// while that handle's boot has not been recorded; the result of the create is that boot event. + HandleWanted { handle: PlaneHandle }, BindRequested { handle: PlaneHandle, media_id: String, policy: BindPolicy }, /// `setState(UNLOADED)` was called: its answer, the task id it wrote (trusted only for `rv == 1`) /// and when. @@ -230,6 +240,9 @@ pub enum Event { #[derive(Debug, PartialEq, Eq)] pub enum Fx { + /// Create and initialize `handle` through the sink, then report what came of it as its boot + /// event ([`Event::Boot`], [`Event::BootUri`]). Asked once per handle: a failed create is a boot too. + Create { handle: PlaneHandle }, /// Run the bind verbs now; the plane is this media id's. Bind { media_id: String }, /// The previous release is still in flight: ask again later. @@ -316,6 +329,13 @@ impl Plane { PlaneHandle::Uri => self.uri, } } + /// Whether `handle`'s boot has been recorded, whatever it found. + pub fn booted_of(&self, handle: PlaneHandle) -> bool { + match handle { + PlaneHandle::Feed => self.booted, + PlaneHandle::Uri => self.uri_booted, + } + } /// The handle the latest granted binding ran over (its unload is the one in flight or done). pub fn bound_handle(&self) -> Option { self.bound_by @@ -325,6 +345,14 @@ impl Plane { self.facts.handle || self.uri.handle } + /// A boot that finds a handle makes an unavailable plane available. Only that: a boot after the + /// first (the URI handle's, on demand) must never touch a binding, a release or a quarantine. + fn promote(&mut self) { + if self.state == State::Unavailable && self.any_handle() { + self.state = State::Idle; + } + } + fn settle(&mut self, ev: i32, task: i64, fx: &mut Vec) { if ev == 1 { self.state = State::Released; @@ -340,14 +368,16 @@ impl Plane { Event::Boot { facts } if !self.booted => { self.booted = true; self.facts = facts; - self.state = if self.any_handle() { State::Idle } else { State::Unavailable }; + self.promote(); } Event::BootUri { facts } if !self.uri_booted => { self.uri_booted = true; self.uri = facts; - self.state = if self.any_handle() { State::Idle } else { State::Unavailable }; + self.promote(); } Event::Boot { .. } | Event::BootUri { .. } => {} + Event::HandleWanted { handle } if !self.booted_of(handle) => fx.push(Fx::Create { handle }), + Event::HandleWanted { .. } => {} Event::BindRequested { handle, .. } if !self.facts_of(handle).handle => { fx.push(Fx::Refuse(Refusal::NoPlane)); } @@ -497,9 +527,9 @@ pub enum BindVerdict { Refused(Refusal), } -/// What the runner holds of one boot handle beyond the machine's facts: the id the sink's +/// What the runner holds of one ACB handle beyond the machine's facts: the id the sink's /// `plane_create` returned (0 = none, nothing to select) and the library's confirmation of -/// `AcbAPI_initialize`, read once at the boot while that handle was the sink's current one. +/// `AcbAPI_initialize`, read once at its create while that handle was the sink's current one. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HandleSeat { pub id: c_long, @@ -552,6 +582,40 @@ impl Inner { self.selected = Some(handle); } } + /// Record `handle`'s boot: the machine's event and, the first time, the seat the sink knows it by. + fn boot(&mut self, handle: PlaneHandle, facts: PlaneFacts, seat: HandleSeat) { + let first = !self.plane.booted_of(handle); + self.run(match handle { + PlaneHandle::Feed => Event::Boot { facts }, + PlaneHandle::Uri => Event::BootUri { facts }, + }); + if first { + self.seats[handle.slot()] = seat; + } + } + /// Run [`Fx::Create`] for `handle`: create and initialize it through the sink, then record what + /// came of it as the handle's boot. One log line says all of it (Feed's boot adds `acb create=`). + fn create(&mut self, sink: &dyn DisplaySink, mt: &MainThread, handle: PlaneHandle, app_id: Option<&CStr>, + player_type: c_int) { + let held = self.selected; + // SAFETY: main thread (the witness); `app_id` is a NUL-terminated string that outlives the + // call, or null, which the seam refuses; the owner is the only caller of the create verb. + let id = unsafe { sink.plane_create(mt, app_id.map_or(std::ptr::null(), CStr::as_ptr), player_type) }; + // SAFETY: as above. The library's confirmation is of the handle just created, still the + // sink's current one. + let initialized = id != 0 && unsafe { sink.plane_initialized(mt) }; + if handle == PlaneHandle::Feed { + plx_base::eventlog::log(&format!("acb create={id}")); + } + plx_base::eventlog::log(&format!("acb handle={handle} ptype={player_type} create={id} initialized={initialized}")); + self.boot(handle, PlaneFacts { handle: id != 0, player_type }, HandleSeat { id, initialized }); + // The sink made the new handle current. What the owner believed it had selected is no longer + // so: forget it, and put back the handle that held the plane so its verbs address it still. + self.selected = None; + if let Some(held) = held { + self.select(sink, mt, held); + } + } fn now_ms(&mut self) -> u64 { let epoch = *self.epoch.get_or_insert_with(Instant::now); u64::try_from(epoch.elapsed().as_millis()).unwrap_or(u64::MAX) @@ -572,7 +636,7 @@ impl Inner { self.defer_logged = true; plx_base::eventlog::log("plane: a bind waits for the previous release's completion"); } - Fx::Defer | Fx::Bind { .. } | Fx::Refuse(_) => {} + Fx::Defer | Fx::Bind { .. } | Fx::Refuse(_) | Fx::Create { .. } => {} } } if fx.iter().any(|effect| !matches!(effect, Fx::Defer | Fx::Lost { .. } | Fx::Foreign { .. })) { @@ -612,23 +676,36 @@ impl PlaneOwner { /// Record what the boot learned of the Feed handle. Taken once; a later call is ignored. pub fn boot(&self, facts: PlaneFacts) { - self.boot_handle(PlaneHandle::Feed, facts, HandleSeat::NONE); + self.lock().boot(PlaneHandle::Feed, facts, HandleSeat::NONE); } - /// Record what the boot learned of `handle`, and the `seat` the sink knows it by. Taken once per - /// handle; a later call is ignored. + /// Record what a boot learned of `handle`, and the `seat` the sink knows it by, without a sink + /// (a fixture's way to seat a handle). Taken once per handle; a later call is ignored. Production + /// never calls it: Feed's handle is seated by [`ensure_handle`](Self::ensure_handle) (or + /// [`boot`](Self::boot) when the build has no Feed) and the URI engine's only by `ensure_handle`. + #[cfg(any(test, feature = "test-support"))] pub fn boot_handle(&self, handle: PlaneHandle, facts: PlaneFacts, seat: HandleSeat) { + self.lock().boot(handle, facts, seat); + } + /// Make `handle` exist, once: the first call creates and initializes it through `sink` (the + /// boot's Feed, or the URI engine's when its first source is offered) and records the boot; every later + /// call, and a handle whose create failed, answers what the first found without touching the + /// sink. Main thread only (the witness): the create verb and the library's confirmation of the + /// initialize are the sink's current handle's. + pub fn ensure_handle(&self, sink: &dyn DisplaySink, mt: &MainThread, handle: PlaneHandle, + app_id: Option<&CStr>, player_type: c_int) -> PlaneFacts { let mut inner = self.lock(); - let first = !match handle { - PlaneHandle::Feed => inner.plane.booted, - PlaneHandle::Uri => inner.plane.uri_booted, - }; - inner.run(match handle { - PlaneHandle::Feed => Event::Boot { facts }, - PlaneHandle::Uri => Event::BootUri { facts }, - }); - if first { - inner.seats[handle.slot()] = seat; + for effect in inner.run(Event::HandleWanted { handle }) { + if let Fx::Create { handle } = effect { + inner.create(sink, mt, handle, app_id, player_type); + } } + inner.plane.facts_of(handle) + } + /// Whether `handle`'s boot is recorded: it was created (or its create failed), so no later + /// [`ensure_handle`](Self::ensure_handle) will make it again. + #[cfg(any(test, feature = "test-support"))] + pub fn attempted(&self, handle: PlaneHandle) -> bool { + self.lock().plane.booted_of(handle) } /// The Feed handle's facts. pub fn facts(&self) -> PlaneFacts { @@ -637,13 +714,14 @@ impl PlaneOwner { pub fn facts_of(&self, handle: PlaneHandle) -> PlaneFacts { self.lock().plane.facts_of(handle) } - /// The library confirmed `AcbAPI_initialize` for `handle` at the boot (a created handle alone is + /// The library confirmed `AcbAPI_initialize` for `handle` at its create (a created handle alone is /// not that); false for a handle that was never booted. pub fn initialized(&self, handle: PlaneHandle) -> bool { self.lock().seats[handle.slot()].initialized } - /// Whether this boot created either ACB handle (Feed's or the URI engine's): the device has an - /// ACB plane at all, which a build with only the URI engine has with Feed's handle absent. + /// Whether either ACB handle has been created (Feed's at the boot, the URI engine's at the first + /// Experimental offer or, while a release is in flight, at the Start's gate): on a build with + /// only the URI engine this is false until then. pub fn any_handle(&self) -> bool { self.lock().plane.any_handle() } @@ -1291,6 +1369,134 @@ mod tests { "plane_unload".to_owned()], "a handle the owner never saw selected is selected again before the unload"); } + // ---- the URI engine's handle is created on demand ---- + + #[test] + fn a_handle_nobody_has_made_is_asked_for_once_and_its_boot_ends_the_asking() { + let mut m = booted(); + assert_eq!(step(&mut m, Event::HandleWanted { handle: PlaneHandle::Uri }), + [Fx::Create { handle: PlaneHandle::Uri }]); + // Until the result is recorded the machine keeps asking; the runner answers within the same call. + assert_eq!(step(&mut m, Event::HandleWanted { handle: PlaneHandle::Uri }), + [Fx::Create { handle: PlaneHandle::Uri }]); + step(&mut m, Event::BootUri { facts: PlaneFacts { handle: true, player_type: URI_PLAYER_TYPE } }); + assert!(step(&mut m, Event::HandleWanted { handle: PlaneHandle::Uri }).is_empty()); + assert!(step(&mut m, Event::HandleWanted { handle: PlaneHandle::Feed }).is_empty(), "Feed's boot is recorded"); + assert_eq!(bind_of(&mut m, PlaneHandle::Uri, BindPolicy::Strict), [Fx::Bind { media_id: "m".into() }]); + } + + #[test] + fn a_create_that_failed_is_recorded_and_never_asked_for_again() { + let mut m = booted(); + step(&mut m, Event::HandleWanted { handle: PlaneHandle::Uri }); + step(&mut m, Event::BootUri { facts: PlaneFacts { handle: false, player_type: URI_PLAYER_TYPE } }); + assert!(step(&mut m, Event::HandleWanted { handle: PlaneHandle::Uri }).is_empty()); + assert_eq!(bind_of(&mut m, PlaneHandle::Uri, BindPolicy::Strict), [Fx::Refuse(Refusal::NoPlane)]); + assert_eq!(m.state(), &State::Idle, "Feed's plane is untouched"); + } + + /// The bug a late boot would add: the URI handle's boot used to set the state to Idle outright, + /// which erases a binding, a release in flight or a quarantine that exists once the boot is no + /// longer the first thing that happens. + #[test] + fn a_late_uri_boot_leaves_the_plane_state_as_it_found_it() { + let uri = PlaneFacts { handle: true, player_type: URI_PLAYER_TYPE }; + let mut bound = booted(); + bind(&mut bound, BindPolicy::Strict); + let mut released = releasing(5); + step(&mut released, Event::Completion { from: None, task: 5, ev: 1 }); + let mut quarantined = releasing(5); + step(&mut quarantined, Event::Tick { now_ms: 100 + RELEASE_FENCE_MS }); + for (name, mut m) in [("bound", bound), ("releasing", releasing(5)), ("released", released), + ("quarantined", quarantined)] { + let before = m.state().clone(); + step(&mut m, Event::BootUri { facts: uri }); + assert_eq!(m.state(), &before, "{name}"); + } + // The consequence: a URI bind over an unproven release is still refused, and one over a + // release in flight still waits. + let mut m = releasing(5); + step(&mut m, Event::BootUri { facts: uri }); + assert_eq!(bind_of(&mut m, PlaneHandle::Uri, BindPolicy::Strict), [Fx::Defer]); + step(&mut m, Event::Tick { now_ms: 100 + RELEASE_FENCE_MS }); + assert_eq!(bind_of(&mut m, PlaneHandle::Uri, BindPolicy::Strict), + [Fx::Refuse(Refusal::Quarantined(Why::Deadline))]); + } + + #[test] + fn the_owner_creates_the_uri_handle_once_through_the_sink_and_records_it() { + use super::super::sink::ScriptedPlaneSink; + let sink = ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }); + sink.create_ids.lock().unwrap().extend([FEED_ID, URI_ID]); + let owner = PlaneOwner::new(); + let mt = mt(); + let app = c"app.test"; + assert!(!owner.attempted(PlaneHandle::Uri)); + let feed = owner.ensure_handle(&sink, &mt, PlaneHandle::Feed, Some(app), DEFAULT_PLAYER_TYPE); + assert_eq!(feed, PlaneFacts { handle: true, player_type: DEFAULT_PLAYER_TYPE }); + assert!(!owner.attempted(PlaneHandle::Uri), "Feed's boot made no URI handle"); + let uri = owner.ensure_handle(&sink, &mt, PlaneHandle::Uri, Some(app), URI_PLAYER_TYPE); + assert_eq!(uri, PlaneFacts { handle: true, player_type: URI_PLAYER_TYPE }); + assert!(owner.initialized(PlaneHandle::Uri) && owner.attempted(PlaneHandle::Uri)); + // A second ask, with any player type, creates nothing and answers what the first found. + assert_eq!(owner.ensure_handle(&sink, &mt, PlaneHandle::Uri, Some(app), 99), uri); + assert_eq!(owner.ensure_handle(&sink, &mt, PlaneHandle::Feed, Some(app), 99), feed); + assert_eq!(sink.created_player_types(), [DEFAULT_PLAYER_TYPE, URI_PLAYER_TYPE]); + // The id the sink answered is the seat: the URI bind selects it. + assert_eq!(owner.bind(&sink, &mt, PlaneHandle::Uri, BindPolicy::Strict, "m1"), BindVerdict::Granted); + assert_eq!(sink.calls().last().unwrap(), &format!("plane_select {URI_ID}")); + } + + #[test] + fn a_create_that_fails_is_final_and_a_create_the_library_did_not_confirm_is_not_initialized() { + use super::super::sink::ScriptedPlaneSink; + let mt = mt(); + let sink = ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }); + let owner = PlaneOwner::new(); + let facts = owner.ensure_handle(&sink, &mt, PlaneHandle::Uri, Some(c"app.test"), URI_PLAYER_TYPE); + assert_eq!(facts, PlaneFacts { handle: false, player_type: URI_PLAYER_TYPE }); + assert!(!owner.initialized(PlaneHandle::Uri)); + owner.ensure_handle(&sink, &mt, PlaneHandle::Uri, Some(c"app.test"), URI_PLAYER_TYPE); + assert_eq!(sink.created_player_types(), [URI_PLAYER_TYPE], "a failed create is not retried"); + assert_eq!(owner.bind(&sink, &mt, PlaneHandle::Uri, BindPolicy::Strict, "m1"), + BindVerdict::Refused(Refusal::NoPlane)); + assert_eq!(owner.phase(), Phase::Unavailable, "no handle at all is no plane"); + // Created, but the library never confirmed the initialize. + let sink = ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }); + sink.create_ids.lock().unwrap().push(URI_ID); + sink.initialized.store(false, std::sync::atomic::Ordering::Relaxed); + let owner = PlaneOwner::new(); + let facts = owner.ensure_handle(&sink, &mt, PlaneHandle::Uri, Some(c"app.test"), URI_PLAYER_TYPE); + assert_eq!(facts, PlaneFacts { handle: true, player_type: URI_PLAYER_TYPE }); + assert!(!owner.initialized(PlaneHandle::Uri)); + } + + /// The sink makes the newest handle current. A handle created while the owner has Feed's selected + /// (Feed played before the first URI play) must not leave the next Feed bind addressing the URI + /// handle: the owner puts Feed's back, and its next grant needs no second select. + #[test] + fn creating_the_uri_handle_after_feed_was_selected_leaves_feeds_handle_the_one_addressed() { + use super::super::sink::ScriptedPlaneSink; + let sink = ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }); + sink.create_ids.lock().unwrap().extend([FEED_ID, URI_ID]); + let owner = PlaneOwner::new(); + let mt = mt(); + let app = Some(c"app.test"); + owner.ensure_handle(&sink, &mt, PlaneHandle::Feed, app, DEFAULT_PLAYER_TYPE); + assert_eq!(owner.bind(&sink, &mt, PlaneHandle::Feed, BindPolicy::Rebind, "m1"), BindVerdict::Granted); + owner.ensure_handle(&sink, &mt, PlaneHandle::Uri, app, URI_PLAYER_TYPE); + assert_eq!(owner.bind(&sink, &mt, PlaneHandle::Feed, BindPolicy::Rebind, "m2"), BindVerdict::Granted); + assert_eq!(sink.calls(), ["plane_create 10".to_owned(), format!("plane_select {FEED_ID}"), + "plane_create 0".to_owned(), format!("plane_select {FEED_ID}")], + "Feed's handle was selected, the URI create made the sink address the new handle, and Feed's was put back"); + // The URI engine's own grant, once Feed has released, selects its handle. + assert_eq!(owner.unload(&sink, &mt), Phase::Releasing); + sink.mailbox.push_from(FEED_ID.into(), 5, 1); + assert_eq!(owner.pump(&sink, &mt), Phase::Released); + assert_eq!(owner.bind(&sink, &mt, PlaneHandle::Uri, BindPolicy::Strict, "m3"), BindVerdict::Granted); + assert_eq!(sink.calls().last().unwrap(), &format!("plane_select {URI_ID}")); + } + // ---- completions are matched on (handle, task): each core numbers its own tasks ---- #[test] diff --git a/rust-modules/platform/src/tv/sink.rs b/rust-modules/platform/src/tv/sink.rs index 39848d8d3b..65801bcf91 100644 --- a/rust-modules/platform/src/tv/sink.rs +++ b/rust-modules/platform/src/tv/sink.rs @@ -9,7 +9,7 @@ pub use super::plane::PlaneCompletion; /// Which video-plane binding this television has. See `src/starfish.h`'s `VP_*` and the long /// comment at the top of `src/video_plane.c`. pub const VP_NONE: c_int = 0; -/// The legacy ACB plane; controlled URI experiments require this mode and a boot-created handle. +/// The legacy ACB plane; controlled URI experiments require this mode. pub const VP_ACB: c_int = 1; pub const VP_EXPORTED: c_int = 2; @@ -99,15 +99,16 @@ pub trait DisplaySink: Sync { unsafe fn destroy_window(&self, mt: &MainThread); unsafe fn plane_create(&self, mt: &MainThread, app_id: *const c_char, player_type: c_int) -> c_long; /// Make `handle` (a value [`plane_create`](Self::plane_create) returned) the one every other - /// `plane_*` verb addresses; true when the sink knows it. The boot creates up to two handles - /// (`tv::plane::PlaneHandle`), and only the plane owner calls this, when it grants a bind: one + /// `plane_*` verb addresses; true when the sink knows it. A process creates up to two handles + /// (`tv::plane::PlaneHandle`: Feed's at the boot, the URI engine's on demand), and only the plane owner calls this, when it grants a bind: one /// binding at a time makes the current handle the bound one. A sink that holds one handle has /// nothing to select, and says so. unsafe fn plane_select(&self, _mt: &MainThread, _handle: c_long) -> bool { false } /// The library confirmed `AcbAPI_initialize` (its answer was exactly 1) for the CURRENT handle. A - /// created handle alone is not that. Only meaningful at the boot, right after that handle's - /// create: the boot reads it once per handle into the plane owner (`HandleSeat::initialized`), - /// which is what the paced URI bind's gate and the product binding read afterwards. + /// created handle alone is not that. Only meaningful right after that handle's create: the plane + /// owner reads it once per handle (`PlaneOwner::ensure_handle`: at the boot for Feed's, when its + /// first source is offered, for the URI engine's) into `HandleSeat::initialized`, which is what the paced URI + /// bind's gate and the product binding read afterwards. unsafe fn plane_initialized(&self, mt: &MainThread) -> bool; #[cfg(feature = "devtriggers")] unsafe fn plane_probe_dass_available(&self, mt: &MainThread) -> bool; @@ -327,9 +328,10 @@ pub const SCRIPTED_PLAYING_TASK: c_long = 4; /// A display sink for tests of the plane owner and of the backends that use it. Every verb is /// refused as `NoSink` does except the plane unload (whose answer a test scripts), the completion -/// mailbox (which a test, or the unload itself with `complete_at_once`, fills), and the ACB bind -/// verbs, which answer success unless a test lists them in `failing` and record each call in -/// `calls` (`plane_send_video_data` records the length of its payload, never its content). +/// mailbox (which a test, or the unload itself with `complete_at_once`, fills), the handle create +/// (which answers `create_ids` in order, the last repeating, and 0 when none is scripted), and the +/// ACB bind verbs, which answer success unless a test lists them in `failing` and record each call +/// in `calls` (`plane_send_video_data` records the length of its payload, never its content). #[cfg(any(test, feature = "test-support"))] pub struct ScriptedPlaneSink { pub unload_answer: std::sync::Mutex, @@ -339,6 +341,8 @@ pub struct ScriptedPlaneSink { pub unloads: std::sync::atomic::AtomicUsize, /// What `plane_initialized` answers. pub initialized: std::sync::atomic::AtomicBool, + /// What each `plane_create` answers: the handle id, one per call, the last repeating. + pub create_ids: std::sync::Mutex>, /// Verb names (`"plane_media_id"`, ...) that answer failure. pub failing: std::sync::Mutex>, /// Every ACB verb called, in order. @@ -354,6 +358,7 @@ impl ScriptedPlaneSink { mailbox: super::plane::CompletionQueue::new(8), unloads: std::sync::atomic::AtomicUsize::new(0), initialized: std::sync::atomic::AtomicBool::new(true), + create_ids: std::sync::Mutex::new(Vec::new()), failing: std::sync::Mutex::new(Vec::new()), calls: std::sync::Mutex::new(Vec::new()), } @@ -361,6 +366,10 @@ impl ScriptedPlaneSink { pub fn unload_count(&self) -> usize { self.unloads.load(std::sync::atomic::Ordering::Relaxed) } /// The ACB verbs called so far, in order. pub fn calls(&self) -> Vec { self.calls.lock().unwrap().clone() } + /// The player types `plane_create` was asked for so far, in order. + pub fn created_player_types(&self) -> Vec { + self.calls().iter().filter_map(|call| call.strip_prefix("plane_create ")?.parse().ok()).collect() + } /// Record `verb` and answer 1 (accepted) unless it is listed in `failing`. fn answer(&self, verb: &'static str, detail: String) -> c_int { self.calls.lock().unwrap().push(if detail.is_empty() { verb.to_owned() } else { format!("{verb} {detail}") }); @@ -370,10 +379,20 @@ impl ScriptedPlaneSink { #[cfg(any(test, feature = "test-support"))] impl DisplaySink for ScriptedPlaneSink { - refuse_window_verbs!(); + unsafe fn create_window(&self, _mt: &MainThread) -> *const c_char { std::ptr::null() } + #[allow(clippy::too_many_arguments)] + unsafe fn place_window(&self, _mt: &MainThread, _src_w: c_int, _src_h: c_int, _dst_x: c_int, + _dst_y: c_int, _dst_w: c_int, _dst_h: c_int) -> c_int { 0 } + unsafe fn destroy_window(&self, _mt: &MainThread) {} refuse_dev_plane_verbs!(); fn window_mode(&self) -> c_int { VP_ACB } fn window_id(&self) -> &'static CStr { c"" } + unsafe fn plane_create(&self, _mt: &MainThread, _app_id: *const c_char, player_type: c_int) -> c_long { + let asked = self.calls.lock().unwrap().iter().filter(|call| call.starts_with("plane_create ")).count(); + self.calls.lock().unwrap().push(format!("plane_create {player_type}")); + let ids = self.create_ids.lock().unwrap(); + ids.get(asked).or(ids.last()).copied().unwrap_or(0) + } unsafe fn plane_initialized(&self, _mt: &MainThread) -> bool { self.initialized.load(std::sync::atomic::Ordering::Relaxed) } diff --git a/rust-modules/play-uri/src/acb_bind.rs b/rust-modules/play-uri/src/acb_bind.rs index d31c1a57a3..e7465f313c 100644 --- a/rust-modules/play-uri/src/acb_bind.rs +++ b/rust-modules/play-uri/src/acb_bind.rs @@ -248,7 +248,7 @@ pub struct Gate { pub ready: bool, } -/// The ACB binding needs the television's ACB display mode, the URI engine's own boot handle, a handle the library +/// The ACB binding needs the television's ACB display mode, the URI engine's own handle (created on demand before this gate reads it), a handle the library /// confirmed initialized (a created handle and `playerType 0` alone do not prove it), player type /// 0 (what the measured sequence initialized), and, for the `connectDass` arm, the entry point. pub fn gate(dass: bool, mode: i32, boot_handle: bool, ptype: i32, dass_available: bool, initialized: bool) -> Gate { diff --git a/rust-modules/play-uri/src/acb_drive.rs b/rust-modules/play-uri/src/acb_drive.rs index 67cc35aff0..3c7e67317e 100644 --- a/rust-modules/play-uri/src/acb_drive.rs +++ b/rust-modules/play-uri/src/acb_drive.rs @@ -8,8 +8,8 @@ use crate::acb_bind::{gate, Op}; use crate::uri_protocol::{MediaId, VideoFacts}; use plx_base::task::MainThread; -use plx_platform::tv::plane::{BindPolicy, BindVerdict, PlaneHandle, PlaneOwner, Refusal}; -use plx_platform::tv::sink::DisplaySink; +use plx_platform::tv::plane::{BindPolicy, BindVerdict, PlaneHandle, PlaneOwner, Refusal, URI_PLAYER_TYPE}; +use plx_platform::tv::sink::{DisplaySink, VP_ACB}; /// How the caller dresses a log line: `Timed` lines carry the caller's own `t=` stamp. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -39,24 +39,40 @@ impl Executed { fn invoked(accepted: bool) -> Self { Self { verdict: Verdict::Invoked(accepted), touched: false } } } -/// Perform `op`. `descriptor` resolves the video-data envelope for the media id and the observed -/// facts, or says why it cannot (nothing is fabricated here). -pub fn execute(op: &Op, mt: &MainThread, sink: &dyn DisplaySink, plane: &PlaneOwner, +/// The ACB gate's facts for the URI engine's handle, making the handle first when it does not exist. +/// The URI engine's own handle (player type 0) is not a boot's: the owner creates it here, once, on +/// the main thread (`PlaneOwner::ensure_handle` logs `acb handle=uri ..`), and every later call reads +/// what that found, a failed create included. Its facts and the library's confirmation of its +/// `AcbAPI_initialize` are the owner's. A display that is not ACB has no handle to make. Both the +/// first check of a bind ([`Op::Check`]) and the offer-time readiness of the production driver ask +/// it, so the handle is made by whichever comes first. +pub fn uri_handle_gate(mt: &MainThread, sink: &dyn DisplaySink, plane: &PlaneOwner, app_id: &str, + dass: bool, dass_available: bool) -> crate::acb_bind::Gate { + let facts = if sink.window_mode() == VP_ACB { + let app_id = std::ffi::CString::new(app_id).ok(); + plane.ensure_handle(sink, mt, PlaneHandle::Uri, app_id.as_deref(), URI_PLAYER_TYPE) + } else { + plane.facts_of(PlaneHandle::Uri) + }; + gate(dass, sink.window_mode(), facts.handle, facts.player_type, dass_available, + plane.initialized(PlaneHandle::Uri)) +} + +/// Perform `op`. `app_id` is the install's app id, the one the URI engine's handle is initialized +/// for (the boot's `paths::app_id`). `descriptor` resolves the video-data envelope for the media id +/// and the observed facts, or says why it cannot (nothing is fabricated here). +pub fn execute(op: &Op, mt: &MainThread, sink: &dyn DisplaySink, plane: &PlaneOwner, app_id: &str, descriptor: &mut dyn FnMut(&MediaId, VideoFacts) -> Result, log: &mut dyn FnMut(Stamp, &str)) -> Executed { // SAFETY (every `unsafe` below): `mt` witnesses the main thread, the one the verbs require, // and each pointer argument is a NUL-terminated string that outlives the call. match op { Op::Check { dass } => { - // The URI engine's own handle (player type 0), booted beside Feed's: its facts and the - // library's confirmation of its `AcbAPI_initialize` are boot facts the owner holds. - let facts = plane.facts_of(PlaneHandle::Uri); #[cfg(feature = "devtriggers")] let dass_available = unsafe { sink.plane_probe_dass_available(mt) }; #[cfg(not(feature = "devtriggers"))] let dass_available = false; - let check = gate(*dass, sink.window_mode(), facts.handle, facts.player_type, dass_available, - plane.initialized(PlaneHandle::Uri)); + let check = uri_handle_gate(mt, sink, plane, app_id, *dass, dass_available); log(Stamp::Timed, &format!("acb gate mode={} boot_handle={} actual_ptype={} dass_available={} initialized={}", check.mode, check.boot_handle, check.ptype, check.dass_available, check.initialized)); Executed::invoked(check.ready) @@ -131,7 +147,7 @@ mod tests { fn run(&mut self, op: &Op, descriptor: Result) -> Executed { let mut descriptor = Some(descriptor); let lines = &mut self.lines; - execute(op, &self.mt, &self.sink, &self.plane, + execute(op, &self.mt, &self.sink, &self.plane, "app.test", &mut |_, _| descriptor.take().expect("one descriptor"), &mut |stamp, line| lines.push((stamp, line.to_owned()))) } diff --git a/rust-modules/play-uri/src/backend.rs b/rust-modules/play-uri/src/backend.rs index 1cc8228d82..f8669c3ff3 100644 --- a/rust-modules/play-uri/src/backend.rs +++ b/rust-modules/play-uri/src/backend.rs @@ -35,9 +35,11 @@ pub use plx_playback_contract::uri::Binding; /// The display path composition chooses for the executor, a pure function of what the set and the /// boot report: /// -/// - the ACB plane when the display is in ACB mode and the same gate the executor re-checks at -/// every Start holds (a handle the library confirmed initialized, player type 0 (the URI -/// engine's own boot handle); `connectDass` is a developer arm and is not asked for); +/// - the ACB plane when the display is in ACB mode: the URI engine's own handle (player type 0) +/// is created when the first source is offered (the Start's gate makes it if nothing did), and the gate the executor re-checks at every +/// Start (a handle the library confirmed initialized, player type 0; `connectDass` is a +/// developer arm and is not asked for) then decides that play, so composition cannot say +/// "unbound" for a handle that has not been asked for yet; /// - the exported window ([`Binding::Exported`] on [`EXPORTED_CANVAS`]) when the display is in /// `VP_EXPORTED` mode and `firmware_major` is one the table `strata` lists. Production passes /// [`EXPORTED_STRATA`], whose doc comment is the evidence rule for an entry; the table is a @@ -46,14 +48,9 @@ pub use plx_playback_contract::uri::Binding; /// neighbouring major; /// - otherwise nothing: a webOS 5+ major without an entry plays Standard. /// -/// `plane` and `initialized` are the URI engine's own handle's (player type 0, the plane owner's -/// `facts_of(PlaneHandle::Uri)` and `initialized(PlaneHandle::Uri)`), never Feed's: a normal boot -/// initializes Feed's handle as player type 10 and the URI engine's as 0. A 5+ set has no ACB -/// handle, so the exported window's answer does not read them. -pub fn product_binding(display_mode: i32, plane: plx_platform::tv::plane::PlaneFacts, initialized: bool, - firmware_major: u32, strata: &[u32]) -> Binding { - let gate = crate::acb_bind::gate(false, display_mode, plane.handle, plane.player_type, false, initialized); - if gate.ready { +/// A 5+ set has no ACB handle, and the exported window's answer reads none. +pub fn product_binding(display_mode: i32, firmware_major: u32, strata: &[u32]) -> Binding { + if display_mode == VP_ACB { Binding::Acb } else if display_mode == VP_EXPORTED && firmware_major != 0 && strata.contains(&firmware_major) { Binding::Exported { canvas: EXPORTED_CANVAS } @@ -128,7 +125,7 @@ pub struct Config { } pub use plx_playback_contract::uri::{NativeTrackIndex, UriSource}; -use plx_playback_contract::uri::native_index; +use plx_playback_contract::uri::{native_index, UriNotReady}; struct StagedSource { attempt: AttemptId, key: SourceKey, source: UriSource, deadline_ms: u64 } @@ -309,6 +306,8 @@ impl UriBackend { } if self.config.binding == Binding::Acb && (self.config.display.window_mode() != VP_ACB || !self.acb_gate(mt)) { + plx_base::eventlog::log( + "uri-acb: the ACB gate refused this start (see the `acb gate` and `acb handle=uri` lines); nothing was opened and the Start is refused"); return self.refuse_before_open(attempt, Failure::Unsupported); } let window_id = match self.config.binding { @@ -784,7 +783,8 @@ impl UriBackend { label: if source.is_derived() { "derived-envelope" } else { "observed-envelope" } }) .ok_or("the service reported no source envelope for these video facts") }; - let executed = acb_drive::execute(op, mt, self.config.display, self.config.plane, &mut descriptor, + let executed = acb_drive::execute(op, mt, self.config.display, self.config.plane, &self.config.app_id, + &mut descriptor, &mut |_, line| plx_base::eventlog::log(&format!("uri-acb: {line}"))); if let (true, Some(session)) = (executed.touched, self.lifecycle.session()) { self.feed(LifeEvent::AcbTouched { session }, mt, now_ms); @@ -1080,24 +1080,33 @@ impl UriDriver for UriBackend { fn plane_owned(&self) -> bool { UriBackend::plane_owned(self) } fn maps_track(&self, selection: TrackSelection) -> bool { UriBackend::maps_track(self, selection) } + + /// On an ACB display the URI engine's handle is made here, when the first source is offered + /// (#1394), so a create or initialize that fails declines the offer and Feed plays the title. + /// The Start's own gate stays the backstop. Every other binding has nothing to set up. + fn ready_to_start(&mut self, mt: &MainThread) -> Result<(), UriNotReady> { + if self.config.binding != Binding::Acb || self.config.display.window_mode() != VP_ACB { + return Ok(()); + } + let gate = acb_drive::uri_handle_gate(mt, self.config.display, self.config.plane, + &self.config.app_id, false, false); + if gate.ready { Ok(()) } else { Err(UriNotReady::AcbHandle) } + } } #[cfg(test)] mod tests { #[test] - fn composition_selects_the_acb_plane_only_when_the_gate_holds() { - use plx_platform::tv::plane::PlaneFacts; + fn composition_selects_the_acb_plane_on_an_acb_display_before_its_handle_exists() { use plx_platform::tv::sink::{VP_ACB, VP_NONE}; - let facts = PlaneFacts { handle: true, player_type: 0 }; - // The exported-window table has no say on an ACB display: not even a listed major changes - // the ACB answer, and a failed gate stays Unbound rather than falling to the exported window. + // The URI engine's handle is created when the first source is offered (#1394), so an ACB + // display is able to take URI plays at composition: "not created yet" is not "unbound". The + // gate the Start re-checks decides each play. The exported-window table has no say on an + // ACB display: not even a listed major changes the answer. for (major, strata) in [(0, &[][..]), (4, &[][..]), (11, &[11][..]), (11, &[][..])] { - let at = |mode, plane, initialized| product_binding(mode, plane, initialized, major, strata); - assert_eq!(at(VP_ACB, facts, true), Binding::Acb, "major {major}"); - assert_eq!(at(VP_ACB, facts, false), Binding::Unbound, "a created handle is not an initialized one"); - assert_eq!(at(VP_ACB, PlaneFacts { handle: true, player_type: 10 }, true), Binding::Unbound); - assert_eq!(at(VP_ACB, PlaneFacts::ABSENT, true), Binding::Unbound); - assert_eq!(at(VP_NONE, facts, true), Binding::Unbound); + let at = |mode| product_binding(mode, major, strata); + assert_eq!(at(VP_ACB), Binding::Acb, "major {major}"); + assert_eq!(at(VP_NONE), Binding::Unbound); } } @@ -1105,25 +1114,19 @@ mod tests { /// (production's is `EXPORTED_STRATA`; a test passes its own, so no table forks the build). #[test] fn the_exported_window_is_selected_only_for_a_listed_firmware_major() { - use plx_platform::tv::plane::PlaneFacts; use plx_platform::tv::sink::{VP_ACB, VP_NONE}; let exported = Binding::Exported { canvas: EXPORTED_CANVAS }; assert_eq!(EXPORTED_CANVAS, (1_920, 1_080)); - // A 5+ set has no ACB handle; a handle that exists must not change the answer either. - for plane in [PlaneFacts::ABSENT, PlaneFacts { handle: true, player_type: 0 }] { - for initialized in [false, true] { - let at = |mode, major, strata: &[u32]| product_binding(mode, plane, initialized, major, strata); - assert_eq!(at(VP_EXPORTED, 11, &[11]), exported, "a listed major"); - assert_eq!(at(VP_EXPORTED, 5, &[5, 11]), exported, "one of several"); - assert_eq!(at(VP_EXPORTED, 10, &[11]), Binding::Unbound, "an unlisted major"); - assert_eq!(at(VP_EXPORTED, 12, &[11]), Binding::Unbound, "nothing is inferred from a neighbour"); - assert_eq!(at(VP_EXPORTED, 0, &[11]), Binding::Unbound, "unknown firmware"); - assert_eq!(at(VP_EXPORTED, 0, &[0, 11]), Binding::Unbound, "0 is never admitted, even if listed"); - assert_eq!(at(VP_EXPORTED, 11, &[]), Binding::Unbound, "an empty table admits nothing"); - assert_eq!(at(VP_NONE, 11, &[11]), Binding::Unbound, "no window path to bind"); - assert_ne!(at(VP_ACB, 11, &[11]), exported, "the table is for the exported window only"); - } - } + let at = |mode, major, strata: &[u32]| product_binding(mode, major, strata); + assert_eq!(at(VP_EXPORTED, 11, &[11]), exported, "a listed major"); + assert_eq!(at(VP_EXPORTED, 5, &[5, 11]), exported, "one of several"); + assert_eq!(at(VP_EXPORTED, 10, &[11]), Binding::Unbound, "an unlisted major"); + assert_eq!(at(VP_EXPORTED, 12, &[11]), Binding::Unbound, "nothing is inferred from a neighbour"); + assert_eq!(at(VP_EXPORTED, 0, &[11]), Binding::Unbound, "unknown firmware"); + assert_eq!(at(VP_EXPORTED, 0, &[0, 11]), Binding::Unbound, "0 is never admitted, even if listed"); + assert_eq!(at(VP_EXPORTED, 11, &[]), Binding::Unbound, "an empty table admits nothing"); + assert_eq!(at(VP_NONE, 11, &[11]), Binding::Unbound, "no window path to bind"); + assert_ne!(at(VP_ACB, 11, &[11]), exported, "the table is for the exported window only"); } use super::*; @@ -1219,9 +1222,9 @@ mod tests { Self::with(Binding::Unbound, Box::leak(Box::new(NoSink)), Box::leak(Box::new(plx_platform::tv::plane::PlaneOwner::new()))) } - /// An ACB-bound backend over a scripted sink and its own plane owner, booted as a normal boot - /// boots it: Feed's handle (player type 10) and the URI engine's (player type 0, the - /// measured sequence), each confirmed initialized. + /// An ACB-bound backend over a scripted sink and its own plane owner with both handles already + /// made: Feed's (player type 10, a boot's) and the URI engine's (player type 0, the measured + /// sequence, which an offer would otherwise create on demand), each confirmed initialized. fn acb() -> (Self, &'static ScriptedPlaneSink, &'static plx_platform::tv::plane::PlaneOwner) { let sink: &'static ScriptedPlaneSink = Box::leak(Box::new(ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }))); @@ -2290,6 +2293,111 @@ mod tests { } } + /// What a normal boot leaves: Feed's handle (player type 10) booted and the URI engine's NOT. The + /// sink answers `ids` to the creates it is asked for. + fn boot_without_the_uri_handle(ids: &[std::os::raw::c_long]) + -> (&'static ScriptedPlaneSink, &'static plx_platform::tv::plane::PlaneOwner) { + let sink: &'static ScriptedPlaneSink = + Box::leak(Box::new(ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }))); + sink.create_ids.lock().unwrap().extend(ids.iter().copied()); + let plane: &'static plx_platform::tv::plane::PlaneOwner = + Box::leak(Box::new(plx_platform::tv::plane::PlaneOwner::new())); + plane.boot(plx_platform::tv::plane::PlaneFacts { handle: true, player_type: 10 }); + (sink, plane) + } + + /// #1394: the boot no longer makes the URI engine's handle. The offer makes it; where no offer did + /// (the probe, a driver started without one) the Start's own gate makes it (player type 0, on the + /// main thread, before anything is opened) and every later Start reuses it. + #[test] + fn the_first_acb_start_creates_the_uri_handle_once_and_a_later_start_reuses_it() { + let (sink, plane) = boot_without_the_uri_handle(&[22]); + let mut first = Rig::with(Binding::Acb, sink, plane); + assert!(sink.created_player_types().is_empty(), "composing the driver creates nothing"); + let log = plx_base::eventlog::with_private_log(|| { + assert_eq!(first.start(A), DispatchResult::Queued); + std::fs::read_to_string(plx_base::eventlog::events_log()).unwrap_or_default() + }); + assert_eq!(sink.created_player_types(), [0], "player type 0, once"); + assert!(plane.initialized(plx_platform::tv::plane::PlaneHandle::Uri)); + assert!(log.contains("acb handle=uri ptype=0 create=22 initialized=true\n"), "{log}"); + let mut second = Rig::with(Binding::Acb, sink, plane); + assert_eq!(second.start(A), DispatchResult::Queued); + assert_eq!(sink.created_player_types(), [0], "a second play creates no second handle"); + } + + /// A create that fails refuses THAT Start (`Failure::Unsupported`, the route's existing refused-Start + /// path), with the reason in the log, before anything is opened; it is not retried. + #[test] + fn a_failed_on_demand_create_refuses_the_start_with_a_reason_and_is_not_retried() { + let (sink, plane) = boot_without_the_uri_handle(&[]); + let mut rig = Rig::with(Binding::Acb, sink, plane); + let log = plx_base::eventlog::with_private_log(|| { + assert_eq!(rig.start(A), DispatchResult::Refused(Failure::Unsupported)); + std::fs::read_to_string(plx_base::eventlog::events_log()).unwrap_or_default() + }); + assert!(log.contains("acb handle=uri ptype=0 create=0 initialized=false\n"), "{log}"); + assert!(log.contains("nothing was opened and the Start is refused"), "{log}"); + assert!(rig.bus_methods().is_empty(), "nothing was opened"); + assert!(rig.driver.exit_disposition() == ExitDisposition::Clean); + let mut again = Rig::with(Binding::Acb, sink, plane); + assert_eq!(again.start(A), DispatchResult::Refused(Failure::Unsupported)); + assert_eq!(sink.created_player_types(), [0], "a failed create is not retried"); + } + + /// #1394: the offer is where the handle is made. A driver on an ACB display whose handle is not + /// there yet makes it when asked whether it is ready, once, and a Start after it finds it. + #[test] + fn readiness_on_an_acb_display_creates_the_uri_handle_once() { + let (sink, plane) = boot_without_the_uri_handle(&[22]); + let mut rig = Rig::with(Binding::Acb, sink, plane); + assert!(sink.created_player_types().is_empty()); + let log = plx_base::eventlog::with_private_log(|| { + assert_eq!(rig.driver.ready_to_start(&rig.mt), Ok(())); + std::fs::read_to_string(plx_base::eventlog::events_log()).unwrap_or_default() + }); + assert_eq!(sink.created_player_types(), [0]); + assert!(log.contains("acb handle=uri ptype=0 create=22 initialized=true\n"), "{log}"); + assert_eq!(rig.driver.ready_to_start(&rig.mt), Ok(())); + assert_eq!(rig.start(A), DispatchResult::Queued); + assert_eq!(sink.created_player_types(), [0], "neither the second ask nor the Start made another"); + } + + /// A create that fails makes the driver not ready, with the reason the offer logs; the failure is + /// recorded, so asking again makes no second attempt. The Start's gate still refuses. + #[test] + fn readiness_after_a_failed_create_says_so_and_is_not_retried() { + let (sink, plane) = boot_without_the_uri_handle(&[]); + let mut rig = Rig::with(Binding::Acb, sink, plane); + assert_eq!(rig.driver.ready_to_start(&rig.mt), Err(UriNotReady::AcbHandle)); + assert_eq!(rig.driver.ready_to_start(&rig.mt), Err(UriNotReady::AcbHandle)); + assert_eq!(sink.created_player_types(), [0], "one attempt"); + assert_eq!(rig.start(A), DispatchResult::Refused(Failure::Unsupported), "the backstop"); + assert_eq!(sink.created_player_types(), [0]); + } + + /// Nothing to set up off the ACB display: an exported-window driver is ready, as is an ACB-bound + /// driver over a display that is not ACB (its Start refuses), and neither makes a handle. + #[test] + fn readiness_off_the_acb_display_is_ready_and_makes_no_handle() { + let (sink, plane) = boot_without_the_uri_handle(&[22]); + let mut exported = Rig::with(Binding::Exported { canvas: (1920, 1080) }, sink, plane); + assert_eq!(exported.driver.ready_to_start(&exported.mt), Ok(())); + let mut elsewhere = Rig::with(Binding::Acb, Box::leak(Box::new(NoSink)), plane); + assert_eq!(elsewhere.driver.ready_to_start(&elsewhere.mt), Ok(())); + assert!(sink.created_player_types().is_empty()); + } + + /// The handle is made for a display that can have one: a driver composed over any other display + /// refuses its Start before the gate and so makes none. + #[test] + fn a_start_on_a_display_that_is_not_acb_creates_no_handle() { + let (sink, plane) = boot_without_the_uri_handle(&[22]); + let mut rig = Rig::with(Binding::Acb, Box::leak(Box::new(NoSink)), plane); + assert_eq!(rig.start(A), DispatchResult::Refused(Failure::Unsupported)); + assert!(sink.created_player_types().is_empty()); + } + #[test] fn acb_load_carries_the_dummy_window_and_the_foreground_notice_precedes_subscribe() { let (mut rig, _, _) = Rig::acb(); diff --git a/rust-modules/play-uri/src/uriprobe.rs b/rust-modules/play-uri/src/uriprobe.rs index 0b102e0817..9e6fb764a7 100644 --- a/rust-modules/play-uri/src/uriprobe.rs +++ b/rust-modules/play-uri/src/uriprobe.rs @@ -27,7 +27,7 @@ //! retry is the product driver's ladder (`register_ladder`, the one decision both call): it waits in //! `AwaitingClose`, then arms the new register deadline after `Closed`. //! -//! The ACB arms require an existing boot-created playerType0 handle and a known SDR720p24 +//! The ACB arms require an ACB display (the gate creates the playerType0 handle on demand) and a known SDR720p24 //! fixture unless a validated native sourceInfo envelope arrives. They start service Play at //! loadCompleted independently of the staged 200/200/200/300ms bind. Bus release triggers one //! main-thread UNLOADED, then plane ownership stays quarantined until process exit: each arm diff --git a/rust-modules/play-uri/src/uriprobe_drive.rs b/rust-modules/play-uri/src/uriprobe_drive.rs index 1a388ae5be..0c90144165 100644 --- a/rust-modules/play-uri/src/uriprobe_drive.rs +++ b/rust-modules/play-uri/src/uriprobe_drive.rs @@ -345,7 +345,7 @@ impl UriSession { .ok_or("no observed envelope or exact SDR720p24 fixture") }; let log = self.log; - let executed = execute(op, mt, self.sink, self.plane, &mut descriptor, &mut |stamp, line| match stamp { + let executed = execute(op, mt, self.sink, self.plane, &self.app_id, &mut descriptor, &mut |stamp, line| match stamp { Stamp::Plain => log(&format!("uriprobe: {line}")), Stamp::Timed => log(&format!("uriprobe: t={now_ms} {line}")), }); diff --git a/rust-modules/playback-contract/src/test_support.rs b/rust-modules/playback-contract/src/test_support.rs index 6b1b511281..7f3f53455f 100644 --- a/rust-modules/playback-contract/src/test_support.rs +++ b/rust-modules/playback-contract/src/test_support.rs @@ -22,6 +22,8 @@ struct Script { refuse_start: Option, refuse_track: Option, exit_preserved: bool, + not_ready: Option, + ready_asked: u32, } /// A scriptable [`UriDriver`]. It answers [`DriverSet::capabilities`] with the set it was built @@ -82,6 +84,12 @@ impl FakeUriHandle { pub fn set_exit_preserved(&self, preserved: bool) { self.0.lock().unwrap().exit_preserved = preserved; } + /// Make [`UriDriver::ready_to_start`] answer this (`None`: ready). + pub fn set_not_ready(&self, why: Option) { + self.0.lock().unwrap().not_ready = why; + } + /// How many times the adapter has asked whether the driver is ready. + pub fn ready_asked(&self) -> u32 { self.0.lock().unwrap().ready_asked } } impl DriverSet for FakeUriDriver { @@ -151,6 +159,12 @@ impl UriDriver for FakeUriDriver { fn maps_track(&self, selection: TrackSelection) -> bool { self.script.lock().unwrap().mapped.contains(&selection) } + + fn ready_to_start(&mut self, _: &MainThread) -> Result<(), crate::uri::UriNotReady> { + let mut script = self.script.lock().unwrap(); + script.ready_asked += 1; + script.not_ready.map_or(Ok(()), Err) + } } #[cfg(test)] diff --git a/rust-modules/playback-contract/src/uri.rs b/rust-modules/playback-contract/src/uri.rs index f96fa7c70a..2a6fbb057f 100644 --- a/rust-modules/playback-contract/src/uri.rs +++ b/rust-modules/playback-contract/src/uri.rs @@ -69,4 +69,26 @@ pub trait UriDriver: DriverSet { fn plane_owned(&self) -> bool; /// Whether the active source has an exact native index for this selected source track. fn maps_track(&self, selection: TrackSelection) -> bool; + /// Whether this driver can take a Start now, asked on the main thread when a qualified source is + /// OFFERED, before the route commits to the URI engine: a driver that has something to set up + /// first (the ACB display's own handle, made once) does it here, so a setup that fails declines + /// the offer and the play stays with Feed instead of failing at its Start. The Start re-checks + /// whatever it needs. A driver with nothing to set up is always ready. + fn ready_to_start(&mut self, _ctx: &Self::Context) -> Result<(), UriNotReady> { Ok(()) } +} + +/// Why a [`UriDriver`] cannot take a Start ([`UriDriver::ready_to_start`]). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum UriNotReady { + /// The ACB display's handle for the URI engine could not be created or initialized. + AcbHandle, +} + +impl UriNotReady { + /// The reason as `uri: original declined ()` says it. + pub fn log_name(self) -> &'static str { + match self { + Self::AcbHandle => "the ACB handle for the URI engine could not be created", + } + } } diff --git a/rust-modules/src/app/adapters/backends.rs b/rust-modules/src/app/adapters/backends.rs index c22dcdddae..59ca44ec0d 100644 --- a/rust-modules/src/app/adapters/backends.rs +++ b/rust-modules/src/app/adapters/backends.rs @@ -5,8 +5,10 @@ //! - [`acb_init`] boots the video plane owner (`plx_platform::tv::plane`) once per process, for //! EVERY backend set. It used to live in the Feed engine, so a URI-only build never created the //! ACB handle, and a webOS 4.x television then stalled URI HLS for want of a bound plane. On an -//! ACB display it creates one handle per backend the build composes: Feed's (player type 10) and -//! the URI engine's (player type 0, which its product binding is gated on), so a normal boot +//! ACB display it creates Feed's handle (player type 10) and no other: the URI engine's handle +//! (player type 0, which its product binding is gated on) is created when the first source is offered +//! to it (`PlaneOwner::ensure_handle`, run by `UriDriver::ready_to_start`), so a viewer who +//! never plays through it never has a second ACB core (#1394), while a normal boot still //! composes the URI driver `Acb` with no `ptype` trigger. //! - [`compose_backends`] installs the product URI driver and then tells the route session which //! backends the installed drivers form, so policy offers what is physically there instead of @@ -23,12 +25,12 @@ use plx_media::route::PlaybackSession; use plx_platform::tv::plane::{self, PlaneFacts, PlaneOwner}; #[cfg(feature = "play-feed")] use plx_platform::tv::plane::DEFAULT_PLAYER_TYPE; -#[cfg(any(feature = "play-feed", feature = "play-uri"))] -use plx_platform::tv::plane::{HandleSeat, PlaneHandle}; +#[cfg(feature = "play-feed")] +use plx_platform::tv::plane::PlaneHandle; #[cfg(feature = "play-uri")] use plx_platform::tv::plane::URI_PLAYER_TYPE; use plx_platform::tv::sink::{self, DisplaySink, VP_EXPORTED, VP_NONE}; -#[cfg(any(feature = "play-feed", feature = "play-uri"))] +#[cfg(feature = "play-feed")] use std::os::raw::c_int; #[cfg(feature = "play-feed")] use plx_play_feed::{control::FeedBackend, feed_resources::{FeedResources, LoadReceipt}, @@ -97,14 +99,14 @@ fn feed_player_type() -> c_int { pt } -/// The webOS 4.x half of [`acb_init_with`]: one handle per backend this build composes, each -/// created and initialized ONCE and never destroyed (a destroy races `LSClient::stop`). Feed's is -/// player type 10 (what a normal boot has always initialized) and the URI engine's is player type 0, -/// the one its product binding is gated on; two cores in one process are independent -/// (`tv::plane`'s module doc). Feed's is created first and with exactly the calls it always made, so -/// its handle, its facts and its log lines are what they were. A build with no Feed boots Feed's -/// facts absent; a build with no URI engine creates no second handle. -#[cfg_attr(not(any(feature = "play-feed", feature = "play-uri")), allow(unused_variables))] +/// The webOS 4.x half of [`acb_init_with`]: Feed's handle, created and initialized ONCE and never +/// destroyed (a destroy races `LSClient::stop`), player type 10 (what a normal boot has always +/// initialized), with exactly the calls and log lines it always had. The URI engine's handle (player +/// type 0, the one its product binding is gated on) is NOT a boot's: the offer of its first source +/// creates it, once, on the main thread (`PlaneOwner::ensure_handle`, reached from the URI +/// driver's `ready_to_start`), so a viewer on Standard never has a second core; two cores in one process are +/// independent (`tv::plane`'s module doc). A build with no Feed boots Feed's facts absent. +#[cfg_attr(not(feature = "play-feed"), allow(unused_variables))] fn acb_init_acb(mt: &MainThread, display: &dyn DisplaySink, owner: &PlaneOwner) { // Which app ACB is being initialized for. The third argument of `AcbAPI_initialize` is an app // id — that much is certain, and both reference implementations pass one (Kodi's webOS port @@ -120,31 +122,16 @@ fn acb_init_acb(mt: &MainThread, display: &dyn DisplaySink, owner: &PlaneOwner) // registered by definition. The environment is still logged, one line at boot, as the // independent witness for whether SAM sets it at all on this firmware. let app_c = std::ffi::CString::new(plx_base::paths::app_id()).ok(); - let app_ptr = app_c.as_ref().map_or(std::ptr::null(), |c| c.as_ptr()); #[cfg(feature = "play-feed")] { let pt = feed_player_type(); - boot_handle(mt, display, owner, app_ptr, PlaneHandle::Feed, pt); + // The owner creates it through the sink, records the facts, the id the sink knows it by and + // the library's confirmation of the initialize (read while it is the sink's current handle), + // and logs `acb create=..` and `acb handle=feed ptype=.. create=.. initialized=..`. + owner.ensure_handle(display, mt, PlaneHandle::Feed, app_c.as_deref(), pt); } #[cfg(not(feature = "play-feed"))] owner.boot(PlaneFacts::ABSENT); - #[cfg(feature = "play-uri")] - boot_handle(mt, display, owner, app_ptr, PlaneHandle::Uri, URI_PLAYER_TYPE); -} - -/// Create and initialize one boot handle of player type `pt` and tell the owner what came of it: -/// the facts, the id the sink knows it by and the library's confirmation of the initialize (read -/// now, while this is the sink's current handle). One log line says all of it. -#[cfg(any(feature = "play-feed", feature = "play-uri"))] -fn boot_handle(mt: &MainThread, display: &dyn DisplaySink, owner: &PlaneOwner, app_ptr: *const std::os::raw::c_char, - handle: PlaneHandle, pt: c_int) { - let acb = unsafe { display.plane_create(mt, app_ptr, pt) }; - let initialized = acb != 0 && unsafe { display.plane_initialized(mt) }; - if handle == PlaneHandle::Feed { - log(&format!("acb create={acb}")); - } - log(&format!("acb handle={handle} ptype={pt} create={acb} initialized={initialized}")); - owner.boot_handle(handle, PlaneFacts { handle: acb != 0, player_type: pt }, HandleSeat { id: acb, initialized }); } /// Compose the backends this boot supplies. A normal boot installs the product URI driver first @@ -357,9 +344,10 @@ impl MonotonicClock for ProductUriClock { } /// The display path the product URI driver is composed with, and the boot log line that names it -/// and the gate's inputs (so the television shows it without a trigger). It reads the URI engine's -/// own handle (player type 0), booted beside Feed's by `acb_init`, and the library's confirmation of -/// its initialize, read there. `firmware_major` (`device::info().major`, read by the caller at the +/// and the gate's inputs (so the television shows it without a trigger). On an ACB display the URI +/// engine's own handle (player type 0) does not exist yet: the offer of its first source creates it, so the +/// line says `handle=deferred` and the binding is `Acb`, and each Start's gate decides that play. +/// `firmware_major` (`device::info().major`, read by the caller at the /// IO side, 0 when unknown) and `strata` (production's `EXPORTED_STRATA`) are parameters, never a /// global read here, so a test passes its own. The line ends `major= evidence=table|none`: /// `table` when `strata` admitted that major's exported window, `none` otherwise. `none` says only @@ -368,17 +356,13 @@ impl MonotonicClock for ProductUriClock { /// reads `none` and plays Standard. The debug override logs its own `evidence=override` line after /// this one. #[cfg(feature = "play-uri")] -fn uri_product_binding(display: &dyn DisplaySink, owner: &PlaneOwner, firmware_major: u32, - strata: &[u32]) -> UriBinding { - let facts = owner.facts_of(PlaneHandle::Uri); - let initialized = owner.initialized(PlaneHandle::Uri); - let binding = plx_play_uri::backend::product_binding(display.window_mode(), facts, initialized, - firmware_major, strata); +fn uri_product_binding(display: &dyn DisplaySink, firmware_major: u32, strata: &[u32]) -> UriBinding { + let binding = plx_play_uri::backend::product_binding(display.window_mode(), firmware_major, strata); + let handle = if matches!(binding, UriBinding::Acb) { "deferred" } else { "none" }; // Only the table composes the exported window here, so an exported binding IS table evidence. let evidence = if matches!(binding, UriBinding::Exported { .. }) { "table" } else { "none" }; - log(&format!("uri: product binding={binding:?} mode={} handle={} ptype={} initialized={initialized} \ - major={firmware_major} evidence={evidence}", - display.window_mode(), facts.handle, facts.player_type)); + log(&format!("uri: product binding={binding:?} mode={} handle={handle} ptype={URI_PLAYER_TYPE} \ + major={firmware_major} evidence={evidence}", display.window_mode())); binding } @@ -391,7 +375,7 @@ fn uri_product_binding(display: &dyn DisplaySink, owner: &PlaneOwner, firmware_m #[cfg(feature = "play-uri")] pub(crate) fn install_uri_product(adapter: &mut PlayerAdapter, session: &mut PlaybackSession) -> bool { let display = sink::display_installed(); - let binding = uri_product_binding(display, plane::installed(), plx_platform::tv::device::info().major, + let binding = uri_product_binding(display, plx_platform::tv::device::info().major, plx_play_uri::backend::EXPORTED_STRATA); // The exported-window override (`dev::uri_exported`): a debug build armed with its trigger, on a // display in `VP_EXPORTED` mode, composes the exported window; otherwise the binding is returned @@ -471,6 +455,7 @@ pub(crate) fn install_uri_backend_with_ports(adapter: &mut PlayerAdapter, #[cfg(test)] mod tests { use super::*; + use plx_platform::tv::plane::PlaneHandle; use plx_base::task::MainThread; use plx_platform::tv::plane::DEFAULT_PLAYER_TYPE; use plx_platform::tv::sink::{NoSink, VP_ACB}; @@ -591,23 +576,23 @@ mod tests { assert!(sink.asked.lock().unwrap().is_empty()); } - /// The player types an ACB boot of this build creates, in order: Feed's, then the URI engine's. + /// The player types an ACB boot of this build creates: Feed's alone. The URI engine's handle is + /// created on demand, when its first source is offered (`PlaneOwner::ensure_handle`). fn created_player_types() -> Vec { let mut types = Vec::new(); #[cfg(feature = "play-feed")] types.push(plx_platform::tv::plane::DEFAULT_PLAYER_TYPE); - #[cfg(feature = "play-uri")] - types.push(plx_platform::tv::plane::URI_PLAYER_TYPE); types } #[test] - fn an_acb_device_creates_one_handle_per_backend_for_this_install_and_records_them() { + fn an_acb_boot_creates_only_feeds_handle_and_records_it() { let sink = BootSink::answering(VP_ACB, vec![7, 8]); let owner = booted(&sink); let asked = sink.asked.lock().unwrap(); let types = created_player_types(); - assert_eq!(*asked, types.iter().map(|t| (plx_base::paths::app_id().to_owned(), *t)).collect::>()); + assert_eq!(*asked, types.iter().map(|t| (plx_base::paths::app_id().to_owned(), *t)).collect::>(), + "a boot creates Feed's handle (player type 10) and no player type 0 handle"); // Feed's handle, its facts and its player type are what a one-handle boot always made. #[cfg(feature = "play-feed")] { @@ -616,13 +601,8 @@ mod tests { } #[cfg(not(feature = "play-feed"))] assert_eq!(owner.facts(), PlaneFacts::ABSENT, "a build with no Feed has no Feed handle"); - #[cfg(feature = "play-uri")] - { - assert_eq!(owner.facts_of(PlaneHandle::Uri), PlaneFacts { handle: true, player_type: 0 }); - assert!(owner.initialized(PlaneHandle::Uri)); - } - #[cfg(not(feature = "play-uri"))] - assert_eq!(owner.facts_of(PlaneHandle::Uri), PlaneFacts::ABSENT, "a build with no URI engine creates no second handle"); + assert_eq!(owner.facts_of(PlaneHandle::Uri), PlaneFacts::ABSENT, "the URI engine's handle is not a boot's"); + assert!(!owner.initialized(PlaneHandle::Uri)); } #[test] @@ -637,30 +617,25 @@ mod tests { /// The bug: a normal boot (no `ptype` trigger) initialized ONE handle as player type 10, so the /// URI product binding (which needs player type 0) was composed `Unbound` and the Settings - /// opt-in could never play URI. + /// opt-in could never play URI. Now the handle is not a boot's, so the binding must not wait for it. #[cfg(feature = "play-uri")] #[test] - fn a_normal_boot_composes_the_uri_product_binding_acb_while_feed_keeps_player_type_10() { + fn a_normal_boot_composes_the_uri_product_binding_acb_before_the_uri_handle_exists() { let sink = BootSink::answering(VP_ACB, vec![7, 8]); let owner = booted(&sink); - assert_eq!(uri_product_binding(&sink, &owner, 4, &[]), UriBinding::Acb); + assert_eq!(uri_product_binding(&sink, 4, &[]), UriBinding::Acb); + assert!(!owner.attempted(PlaneHandle::Uri), "composing the binding creates no handle"); + assert_eq!(sink.asked.lock().unwrap().len(), created_player_types().len()); #[cfg(feature = "play-feed")] assert_eq!(owner.facts().player_type, DEFAULT_PLAYER_TYPE, "Feed's handle is still type 10"); } + /// A display with no window path binds nothing, not even for a table that lists the major. #[cfg(feature = "play-uri")] #[test] - fn the_uri_product_binding_is_unbound_unless_the_uri_handle_was_created_and_confirmed() { - // A second create that fails leaves Feed's handle alone and the URI engine unbound. - let sink = BootSink::answering(VP_ACB, vec![7, 0]); - let owner = booted(&sink); - #[cfg(feature = "play-feed")] - assert_eq!(owner.facts(), PlaneFacts { handle: true, player_type: DEFAULT_PLAYER_TYPE }); - #[cfg(feature = "play-feed")] - assert_eq!(uri_product_binding(&sink, &owner, 4, &[]), UriBinding::Unbound); - // No window path: not even a table that lists the major binds it. + fn the_uri_product_binding_is_unbound_without_a_window_path() { let sink = BootSink::answering(VP_NONE, vec![7, 8]); - assert_eq!(uri_product_binding(&sink, &booted(&sink), 11, &[11]), UriBinding::Unbound); + assert_eq!(uri_product_binding(&sink, 11, &[11]), UriBinding::Unbound); } /// The exported window follows the firmware major through the composition: production's table @@ -673,26 +648,25 @@ mod tests { use plx_play_uri::backend::{EXPORTED_CANVAS, EXPORTED_STRATA}; let exported = UriBinding::Exported { canvas: EXPORTED_CANVAS }; let sink = BootSink::answering(VP_EXPORTED, vec![7, 8]); - let owner = booted(&sink); // Production's table admits major 5 and nothing else: every other release, known or not, // composes Unbound on this display. An entry lands with its Results row and the change to // this test. for major in 0..=30 { let want = if major == 5 { exported } else { UriBinding::Unbound }; - assert_eq!(uri_product_binding(&sink, &owner, major, EXPORTED_STRATA), want, "major {major}"); + assert_eq!(uri_product_binding(&sink, major, EXPORTED_STRATA), want, "major {major}"); } // The same display on a set whose mode is not the exported window: major 5 binds nothing there. for mode in [VP_NONE, plx_platform::tv::sink::VP_ACB] { let sink = BootSink::answering(mode, vec![7, 8]); - assert_ne!(uri_product_binding(&sink, &booted(&sink), 5, EXPORTED_STRATA), exported, "mode {mode}"); + assert_ne!(uri_product_binding(&sink, 5, EXPORTED_STRATA), exported, "mode {mode}"); } - assert_eq!(uri_product_binding(&sink, &owner, 11, &[11]), exported); - assert_eq!(uri_product_binding(&sink, &owner, 10, &[11]), UriBinding::Unbound, "an unlisted major"); - assert_eq!(uri_product_binding(&sink, &owner, 0, &[11]), UriBinding::Unbound, "unknown firmware"); + assert_eq!(uri_product_binding(&sink, 11, &[11]), exported); + assert_eq!(uri_product_binding(&sink, 10, &[11]), UriBinding::Unbound, "an unlisted major"); + assert_eq!(uri_product_binding(&sink, 0, &[11]), UriBinding::Unbound, "unknown firmware"); // The same table on a display that is not in the exported-window mode. for mode in [VP_NONE, plx_platform::tv::sink::VP_ACB] { let sink = BootSink::answering(mode, vec![7, 8]); - assert_ne!(uri_product_binding(&sink, &booted(&sink), 11, &[11]), exported, "mode {mode}"); + assert_ne!(uri_product_binding(&sink, 11, &[11]), exported, "mode {mode}"); } } @@ -703,7 +677,7 @@ mod tests { fn the_product_binding_boot_line_names_the_major_and_its_evidence() { let line = |mode, major, strata: &[u32]| plx_base::eventlog::with_private_log(|| { let sink = BootSink::answering(mode, vec![7, 8]); - uri_product_binding(&sink, &booted(&sink), major, strata); + uri_product_binding(&sink, major, strata); std::fs::read_to_string(plx_base::eventlog::events_log()).unwrap_or_default() }); let listed = line(VP_EXPORTED, 11, &[11]); @@ -717,7 +691,8 @@ mod tests { assert!(shipped.contains("uri: product binding=Exported { canvas: (1920, 1080) } mode=2 "), "{shipped}"); assert!(shipped.contains(" major=5 evidence=table\n"), "{shipped}"); let acb = line(plx_platform::tv::sink::VP_ACB, 4, &[]); - assert!(acb.contains("uri: product binding=Acb ") && acb.contains(" major=4 evidence=none\n"), "{acb}"); + assert!(acb.contains("uri: product binding=Acb mode=1 handle=deferred ptype=0 "), "{acb}"); + assert!(acb.contains(" major=4 evidence=none\n"), "{acb}"); } #[test] diff --git a/rust-modules/src/app/run_uri_boot_tests.rs b/rust-modules/src/app/run_uri_boot_tests.rs index 6f53869b1c..eb11f2ce7b 100644 --- a/rust-modules/src/app/run_uri_boot_tests.rs +++ b/rust-modules/src/app/run_uri_boot_tests.rs @@ -1644,4 +1644,163 @@ mod uri_boot_tests { assert!(matches!(started, plx_media::player::BufferfeedStartOutcome::Failed), "the start over the quarantined registration is refused, not launched: {started:?}"); } + // ---- #1394: the URI engine's ACB handle is created when its first play is OFFERED, not by the boot ---- + + /// The URI driver composed over a scripted ACB display (`create_ids` is what its creates answer) + /// with production's ACB evidence and `qualifier` standing in for the viewer's engine choice. + fn compose_acb(rig: &mut Rig, create_ids: &[c_long], qualifier: Arc) + -> (&'static Bus, &'static plx_platform::tv::sink::ScriptedPlaneSink) { + use plx_platform::tv::sink::{PlaneUnload, ScriptedPlaneSink}; + plx_platform::tv::plane::installed().reset_for_test(); + plx_plex::plex::client_for(rig.sid).unwrap() + .set_link(plx_plex::plex::probe::Location::Local); + let bus: &'static Bus = Box::leak(Box::new(Bus::new())); + let display: &'static ScriptedPlaneSink = + Box::leak(Box::new(ScriptedPlaneSink::new(PlaneUnload { rv: 1, task: 5 }))); + display.create_ids.lock().unwrap().extend(create_ids.iter().copied()); + let caps = Capabilities { progressive_uri: true, hls_uri: false, + packet_feed: false, pause: true, seek: true, audio_track: true, + subtitle_track: true, audio_off: false, subtitle_off: false }; + let port = ResolvePort::new(ProductEvidence { display_exported: false, binding: Binding::Acb, + callable: caps, certified: caps, preview_rectangle: false }, qualifier); + assert!(crate::app::adapters::backends::install_uri_product_with_ports( + &mut rig.app.adapters.player, &mut rig.app.player.session, port, bus, display)); + assert!(display.created_player_types().is_empty(), "composing the driver creates no handle"); + (bus, display) + } + + /// Let the pending request resolve and return what the route landed. + fn resolve_pending(rig: &mut Rig) -> plx_media::route::ResolvedStart { + rig.release.send(()).unwrap(); + let mut resolved = None; + poll_until("the request did not resolve", || { + resolved = plx_media::route::pump_play(&mut rig.app.player.session, + rig.app.bridge.metadata_mut()); + resolved.is_some() + }); + resolved.unwrap() + } + + /// `offer_qualified_uri` as the app loop calls it, with the event log it wrote. + fn offer_with_log(rig: &mut Rig, offer: plx_media::route::QualifiedUriOffer) + -> (Result<(), plx_playback_contract::Failure>, String) { + plx_base::eventlog::with_private_log(|| { + let offered = rig.app.adapters.player.offer_qualified_uri(&mut rig.app.player.session, offer); + (offered, std::fs::read_to_string(plx_base::eventlog::events_log()).unwrap_or_default()) + }) + } + + /// An offer whose handle cannot be made, declined as the app loop declines it, and the Feed + /// resolve it hands the same play to. + fn decline_and_resolve_feed(rig: &mut Rig, resolved: plx_media::route::ResolvedStart) -> (String, plx_media::route::ResolvedStart) { + let offer = resolved.uri_offer.expect("the viewer's original is offered"); + let offer_id = offer.offer_id(); + let (offered, log) = offer_with_log(rig, offer); + assert!(offered.is_err(), "the offer is declined"); + assert!(plx_media::route::retry_declined_uri_source(&mut rig.app.player.session, + rig.app.bridge.metadata_mut(), resolved.route_start.expect("route start"), offer_id)); + let feed = resolve_pending(rig); + assert!(feed.uri_offer.is_none(), "a declined original is not offered twice"); + assert!(matches!(rig.app.player.session.backend_offer(), + plx_media::route::backend_policy::Selection::Open(choice) + if choice.backend == BackendKind::Feed)); + (log, feed) + } + + /// Start the Feed play a declined offer was handed to. + fn start_feed(rig: &mut Rig, feed: &plx_media::route::ResolvedStart) { + assert!(matches!(plx_media::player::begin_resume(&mut rig.app.player.session, + &mut rig.app.adapters.player, feed.resume_ns), plx_media::player::ResumeStart::Settled( + plx_media::player::ResumeOutcome::Prepared))); + assert!(matches!(plx_media::player::start_playback_tracked( + &mut rig.app.player.session, &mut rig.app.adapters.player), + plx_media::player::BufferfeedStartOutcome::Launched(_)), "Feed starts the play"); + } + + /// An Experimental viewer's offer on an ACB display makes the URI engine's handle (player type 0) + /// at the offer, once, and is taken; the Start after it finds the handle there. + #[test] + fn an_acb_offer_creates_the_uri_handle_at_the_offer_and_is_taken() { + let _serial = plx_base::testlock::serial(); + let Some(mut rig) = Rig::serving(false) else { return }; + let (bus, display) = compose_acb(&mut rig, &[22], Arc::new(QualifiedOriginal)); + rig.request(); + let resolved = resolve_pending(&mut rig); + let (offered, log) = offer_with_log(&mut rig, resolved.uri_offer.expect("original offered")); + assert!(offered.is_ok()); + assert_eq!(display.created_player_types(), [plx_platform::tv::plane::URI_PLAYER_TYPE], + "the offer made the handle, before any Start"); + assert!(log.contains("acb handle=uri ptype=0 create=22 initialized=true\n"), "{log}"); + assert!(bus.shapes().is_empty(), "nothing is opened by an offer"); + assert!(matches!(plx_media::player::start_playback_tracked( + &mut rig.app.player.session, &mut rig.app.adapters.player), + plx_media::player::BufferfeedStartOutcome::Launched(_))); + window_frame(&mut rig); + assert_eq!(bus.shapes(), [Shape::Named], "the service is opened over the handle the offer made"); + assert_eq!(display.created_player_types(), [0], "the Start made no second handle"); + plx_platform::tv::plane::installed().reset_for_test(); + } + + /// A create that fails declines the offer with the reason, and the same play goes to Feed. + #[test] + fn an_acb_offer_whose_handle_cannot_be_created_is_declined_and_feed_plays() { + let _serial = plx_base::testlock::serial(); + let Some(mut rig) = Rig::serving(false) else { return }; + let (bus, display) = compose_acb(&mut rig, &[], Arc::new(QualifiedOriginal)); + rig.request(); + let resolved = resolve_pending(&mut rig); + let (log, feed) = decline_and_resolve_feed(&mut rig, resolved); + assert!(log.contains("acb handle=uri ptype=0 create=0 initialized=false\n"), "{log}"); + assert!(log.contains("uri: original declined (the ACB handle for the URI engine could not be created)\n"), + "{log}"); + start_feed(&mut rig, &feed); + assert_eq!(rig.app.adapters.player.backend_snapshot().backend, Some(BackendKind::Feed)); + assert!(bus.shapes().is_empty(), "the URI service was never opened"); + assert_eq!(display.created_player_types(), [0], "one attempt"); + plx_platform::tv::plane::installed().reset_for_test(); + } + + /// The failed create is recorded: a later offer in the same process declines at once and makes + /// no second attempt. + #[test] + fn a_second_acb_offer_after_a_failed_create_declines_without_another_attempt() { + let _serial = plx_base::testlock::serial(); + let Some(mut rig) = Rig::serving(false) else { return }; + let (_, display) = compose_acb(&mut rig, &[], Arc::new(QualifiedOriginal)); + rig.request(); + let resolved = resolve_pending(&mut rig); + let (_, feed) = decline_and_resolve_feed(&mut rig, resolved); + start_feed(&mut rig, &feed); + plx_media::player::stop_playback(&mut rig.app.player.session, &mut rig.app.adapters.player); + assert_eq!(display.created_player_types(), [0]); + assert!(plx_media::route::request_play(&mut rig.app.player.session, + rig.app.bridge.metadata_mut(), plx_platform::devcaps::caps(), rig.sid, "1", + "/library/parts/1/file.mkv", "h264", "aac", "First", "", None, "")); + let resolved = resolve_pending(&mut rig); + let (log, _) = decline_and_resolve_feed(&mut rig, resolved); + assert!(log.contains("uri: original declined (the ACB handle for the URI engine could not be created)\n"), + "{log}"); + assert!(!log.contains("acb handle=uri"), "no second attempt is logged: {log}"); + assert_eq!(display.created_player_types(), [0], "a failed create is not retried"); + plx_platform::tv::plane::installed().reset_for_test(); + } + + /// A viewer on Standard (the production qualifier answers `NotOptedIn`) is never offered the URI + /// engine, so nothing creates its handle: the boot made Feed's alone and a play leaves it so. + #[test] + fn a_standard_viewers_play_on_an_acb_display_creates_no_uri_handle() { + let _serial = plx_base::testlock::serial(); + let Some(mut rig) = Rig::serving(false) else { return }; + let (bus, display) = compose_acb(&mut rig, &[22], Arc::new(plx_media::route::uri_preflight::ProductQualifier)); + rig.request(); + let resolved = resolve_pending(&mut rig); + assert!(resolved.uri_offer.is_none(), "Standard is never offered the URI engine"); + assert!(matches!(rig.app.player.session.backend_offer(), + plx_media::route::backend_policy::Selection::Open(choice) + if choice.backend == BackendKind::Feed)); + window_frame(&mut rig); + assert!(display.created_player_types().is_empty(), "no handle was made for a Standard play"); + assert!(bus.shapes().is_empty()); + plx_platform::tv::plane::installed().reset_for_test(); + } } diff --git a/src/starfish.h b/src/starfish.h index 94a240f534..36e40bceb8 100644 --- a/src/starfish.h +++ b/src/starfish.h @@ -65,7 +65,7 @@ void vp_destroy_window(void); /* ACB (App Common Binding) — VP_ACB only; every verb is a no-op in the other modes, so callers * need not branch. On webOS 5 this whole sequence has no replacement: it is deleted outright. */ -long acb_create(const char *appId, int playerType); /* create+initialize; 0 = failed. At most two per process (one per backend), none ever destroyed; the newest is current */ +long acb_create(const char *appId, int playerType); /* create+initialize; 0 = failed. At most two per process (one per backend: Feed's at the boot, the URI engine's on demand), none ever destroyed; the newest is current */ int acb_select(long handle); /* make a handle acb_create returned the one every verb below addresses; 1 = done, 0 = not ours */ /* The paced URI bind (play-uri `acb_bind`): the same calls acb_bind() makes, split so the caller can * pace them. 1 = the library confirmed AcbAPI_initialize (a created handle alone is not that), 0 not. */ diff --git a/src/video_plane.c b/src/video_plane.c index 4e593c370f..1db15b0f30 100644 --- a/src/video_plane.c +++ b/src/video_plane.c @@ -185,9 +185,9 @@ void vp_destroy_window(void) { #define PLAYSTATE_PLAYING 2 #define PLAYSTATE_PAUSED 3 -/* `g_acb` is the handle every ACB verb below addresses. The boot may create two (one per backend: - * `tv::plane::PlaneHandle`, Feed's and the URI engine's, each created and initialized once and never - * destroyed); `acb_select` moves `g_acb` between them and only the plane owner calls it, on the +/* `g_acb` is the handle every ACB verb below addresses. A process may create two (one per backend: + * `tv::plane::PlaneHandle`, Feed's at the boot and the URI engine's on demand, each created and + * initialized once and never destroyed); `acb_select` moves `g_acb` between them and only the plane owner calls it, on the * grant of a bind, so exactly the bound handle is current. A slot keeps the library's answer to its * own `AcbAPI_initialize`, which `acb_initialized` reports for the current handle. */ #define ACB_SLOTS 2 @@ -279,7 +279,7 @@ long acb_create(const char *appId, int playerType) { return handle; } /* Make `handle` (a value `acb_create` returned) the one every verb below addresses. 1 = it is one of - * this process's boot handles and now current, 0 = unknown, nothing changed. Only the plane owner + * this process's handles and now current, 0 = unknown, nothing changed. Only the plane owner * calls it, when it grants a bind: one binding at a time means the current handle IS the bound one. */ int acb_select(long handle) { for (int i = 0; i < g_nslots; i++) { @@ -292,14 +292,14 @@ int acb_select(long handle) { return 0; } /* The URI bind (the production backend and the developer probe, both through play-uri's - * `acb_bind`) uses its own process-lifetime boot handle (player type 0, selected by the plane owner) + * `acb_bind`) uses its own process-lifetime handle (player type 0, created on demand when its first source is offered, selected by the plane owner) * and the same callback as BF. These split the existing bind solely to preserve observed inter-call * delays. */ /* Same device SHA pinned above: initialize wrapper@0x15c4c forwards core@0xcd5c. * Core returns exactly 1 only after valid parameters and callback installation; 0 refuses. - * Answers for the CURRENT handle, so it is only meaningful at the boot right after that handle's - * acb_create: the boot reads it once per handle into the plane owner, which the URI gate reads - * afterwards. normal acb_create still returns its historical handle result. */ + * Answers for the CURRENT handle, so it is only meaningful right after that handle's acb_create: + * the plane owner reads it once per handle (`PlaneOwner::ensure_handle`: at the boot for Feed's, when + * its first source is offered, for the URI engine's), which the URI gate reads afterwards. normal acb_create still returns its historical handle result. */ int acb_initialized(void) { return g_acb && acb.probe_initialize_result == 1; } int acb_probe_dass_available(void) { return g_acb && acb.connectDass; } int acb_media_id(const char *mediaId) { diff --git a/tests/README.md b/tests/README.md index 6d8129f8fc..e40fc487af 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1046,7 +1046,7 @@ its production URI backend, bound through ACB, via the debug-only dev route `plx harness's own mock PMS is the plan carrier: it starts on the configured `PMS_HOST:PMS_PORT` with the `pipe_uri_carrier_720p24.mp4` clip (a 90 s 720p24 progressive twin; one audio stream, no subtitles; built by `make fixtures-pipeline`) as ratingKey 990001, the app signs in as the synthetic guest and opens that -item (`plxnative-play=990001`, with `quality=original`, `audioenh=off` pinned; no `ptype`, since the boot gives the URI engine its own player type 0 handle), and the runner +item (`plxnative-play=990001`, with `quality=original`, `audioenh=off` pinned; no `ptype`, since the URI engine has its own player type 0 handle, created when its first play is offered), and the runner writes `plxnative-uridev` as `acb part=991001 pms=: url=/`, so the app's qualifier answers with the fixture's URL and the TV fetches the HLS stream from the fixture server (shaper and wire counters keep working). `pms=` pins the server by ORIGIN, not by machine diff --git a/tests/manifest.json b/tests/manifest.json index 4555b673a0..27e787d932 100644 --- a/tests/manifest.json +++ b/tests/manifest.json @@ -5046,7 +5046,7 @@ "uri_presented": true, "uri_service_playing": true }, - "_uri_note": "PRODUCTION ROUTE (debug builds, `--guest --mock`). The same 20 Mbit/s HLS stream as pipe_uri_hls_720p24_20m_bound, but played by the PRODUCTION URI backend instead of the developer probe: the app resolves the carrier clip (`carrier`, a 90 s 720p24 progressive twin on the harness's own mock PMS, ratingKey 990001), the dev route (`plxnative-uridev`, written by the runner) answers the qualification with the fixture's URL, and the backend binds the ACB plane over the URI engine's own player-type-0 boot handle (no `ptype` trigger) and plays it. The runner writes `part=` from the carrier, `pms=` from the configured mock address (the origin the app's own client holds), `url=` from the fixture server, pins `quality=original` and `audioenh=off`, never arms `uriprobe`, and deletes `plxnative-uridev` afterwards. Graded from `uri-trace:` lines (the probe's event text under a new prefix) and the lines production writes: `uri: service playing`, `uri: presented (... from P to N ms)` with N > P, `videoplane: BOUND`, and the display and PLAYING ACB tasks read from the log, each with an `acb callback ... ev=1`. The probe pair stays as the instrument baseline; compare the two on the same stream." + "_uri_note": "PRODUCTION ROUTE (debug builds, `--guest --mock`). The same 20 Mbit/s HLS stream as pipe_uri_hls_720p24_20m_bound, but played by the PRODUCTION URI backend instead of the developer probe: the app resolves the carrier clip (`carrier`, a 90 s 720p24 progressive twin on the harness's own mock PMS, ratingKey 990001), the dev route (`plxnative-uridev`, written by the runner) answers the qualification with the fixture's URL, and the backend binds the ACB plane over the URI engine's own player-type-0 handle (created when its first play is offered, not by the boot; no `ptype` trigger) and plays it. The runner writes `part=` from the carrier, `pms=` from the configured mock address (the origin the app's own client holds), `url=` from the fixture server, pins `quality=original` and `audioenh=off`, never arms `uriprobe`, and deletes `plxnative-uridev` afterwards. Graded from `uri-trace:` lines (the probe's event text under a new prefix) and the lines production writes: `uri: service playing`, `uri: presented (... from P to N ms)` with N > P, `videoplane: BOUND`, and the display and PLAYING ACB tasks read from the log, each with an `acb callback ... ev=1`. The probe pair stays as the instrument baseline; compare the two on the same stream." }, { "name": "pipe_uri_product_hls_720p24_20m_bound_collapse", @@ -5091,7 +5091,7 @@ "uri_presented": true, "uri_service_playing": true }, - "_uri_note": "PRODUCTION ROUTE (debug builds, `--guest --mock`). The same 20 Mbit/s HLS stream as pipe_uri_hls_720p24_20m_bound_collapse, but played by the PRODUCTION URI backend instead of the developer probe: the app resolves the carrier clip (`carrier`, a 90 s 720p24 progressive twin on the harness's own mock PMS, ratingKey 990001), the dev route (`plxnative-uridev`, written by the runner) answers the qualification with the fixture's URL, and the backend binds the ACB plane over the URI engine's own player-type-0 boot handle (no `ptype` trigger) and plays it. The runner writes `part=` from the carrier, `pms=` from the configured mock address (the origin the app's own client holds), `url=` from the fixture server, pins `quality=original` and `audioenh=off`, never arms `uriprobe`, and deletes `plxnative-uridev` afterwards. Graded from `uri-trace:` lines (the probe's event text under a new prefix) and the lines production writes: `uri: service playing`, `uri: presented (... from P to N ms)` with N > P, `videoplane: BOUND`, and the display and PLAYING ACB tasks read from the log, each with an `acb callback ... ev=1`. The probe pair stays as the instrument baseline; compare the two on the same stream." + "_uri_note": "PRODUCTION ROUTE (debug builds, `--guest --mock`). The same 20 Mbit/s HLS stream as pipe_uri_hls_720p24_20m_bound_collapse, but played by the PRODUCTION URI backend instead of the developer probe: the app resolves the carrier clip (`carrier`, a 90 s 720p24 progressive twin on the harness's own mock PMS, ratingKey 990001), the dev route (`plxnative-uridev`, written by the runner) answers the qualification with the fixture's URL, and the backend binds the ACB plane over the URI engine's own player-type-0 handle (created when its first play is offered, not by the boot; no `ptype` trigger) and plays it. The runner writes `part=` from the carrier, `pms=` from the configured mock address (the origin the app's own client holds), `url=` from the fixture server, pins `quality=original` and `audioenh=off`, never arms `uriprobe`, and deletes `plxnative-uridev` afterwards. Graded from `uri-trace:` lines (the probe's event text under a new prefix) and the lines production writes: `uri: service playing`, `uri: presented (... from P to N ms)` with N > P, `videoplane: BOUND`, and the display and PLAYING ACB tasks read from the log, each with an `acb callback ... ev=1`. The probe pair stays as the instrument baseline; compare the two on the same stream." }, { "name": "pipe_uri_product_hls_720p24_2a_audio2", diff --git a/tests/run.py b/tests/run.py index dc593587ab..a79681e0a0 100755 --- a/tests/run.py +++ b/tests/run.py @@ -2178,7 +2178,7 @@ def uri_dev_trigger(part_id, host, port, url): # What a product run must never inherit from the case or the command line: `uriprobe` would divert the # route under test, `playurl` would play the Feed, and the rest are written by the runner itself, except # `ptype`: the runner no longer writes it, a product run boots as production does (the URI engine has -# its own player type 0 handle), and `ptype` would only retype Feed's handle. +# its own player type 0 handle, created when its first play is offered), and `ptype` would only retype Feed's handle. # `pms-origin` would move the app's primary off the configured address the trigger's `pms=` names. URI_PRODUCT_DIVERTING = ("plxnative-uriprobe", "plxnative-playurl", "plxnative-uridev", "plxnative-pms-origin") URI_PRODUCT_RESERVED = URI_PRODUCT_DIVERTING + ("plxnative-play", "plxnative-ptype", "plxnative-quality") @@ -2186,7 +2186,7 @@ def uri_dev_trigger(part_id, host, port, url): def uri_product_files(case, url_base, pms, extra=()): """The trigger files of a product case: the integration tier's routing pins (`play=`, - `quality=original`, `audioenh=off`; no `ptype`: the boot gives the URI engine its own player type 0 handle) pointed at the CARRIER clip's ratingKey, plus + `quality=original`, `audioenh=off`; no `ptype`: the URI engine has its own player type 0 handle, created when its first play is offered) pointed at the CARRIER clip's ratingKey, plus `plxnative-uridev` carrying the carrier's part, the mock's origin and the fixture URL. `pms` is the mock's `(host, port)` as `configured_endpoint` reads it from `src/config.local.h`.""" for name in (f"plxnative-{k}" for k in (case.get("triggers") or {})): @@ -2196,7 +2196,7 @@ def uri_product_files(case, url_base, pms, extra=()): if name in URI_PRODUCT_RESERVED: if name == "plxnative-ptype": raise UriProductRefused("--extra-trigger ptype: a product run boots as production does (the URI " - "engine has its own player type 0 handle); ptype would only retype Feed's") + "engine has its own player type 0 handle, created when its first play is offered); ptype would only retype Feed's") raise UriProductRefused(f"--extra-trigger {name} would replace what the product route pins " f"(or arm the probe on top of it)") carrier = dict(case, rk=str(mock_fps.CARRIER_RK)) diff --git a/tests/test_harness.py b/tests/test_harness.py index c8fe95c038..d56dce1cb5 100644 --- a/tests/test_harness.py +++ b/tests/test_harness.py @@ -6825,7 +6825,7 @@ def test_the_files_pin_the_routing_contract_and_name_the_carrier(self): self.assertEqual(got["plxnative-play"], str(run.mock_fps.CARRIER_RK)) self.assertEqual((got["plxnative-quality"], got["plxnative-audioenh"]), ("original", "off")) self.assertNotIn("plxnative-ptype", got, - "a product run boots as production does: the URI engine has its own player type 0 handle") + "a product run boots as production does: the URI engine has its own player type 0 handle, created when its first play is offered") self.assertEqual(got["plxnative-uridev"], f"acb part={run.mock_fps.CARRIER_PART_ID} pms={self.HOST}:{self.PORT} " f"url=http://192.0.2.10:8020/pipe_hls_720p24_20m/index.m3u8") From bc126230982deb5ee5f4f48069dbce645fdab1f5 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sat, 14 Nov 2026 23:28:12 +0300 Subject: [PATCH 5/8] Host test builds carry line tables only, not full DWARF (#1389) cargo test builds every unit of the host unit suite (the 18 test binaries, the workspace libs under them and the third-party libs) with the test profile. That profile inherited the main checkout's full DWARF, which lanes and CI had already dropped. rust-modules/Cargo.toml now sets [profile.test] debug = "line-tables-only". Panic backtraces keep file:line; only debugger locals in host test builds are lost. CARGO_PROFILE_TEST_DEBUG=2 (or --config profile.test.debug=2) brings full DWARF back. The simulator's cargo build, the ARM library and clippy/check do not use this profile and are unchanged. In lanes and CI the unit graph is byte-identical, because their configs already set dev to line tables and test inherits it. Measured on an M4 in scratch trees with no lane config (build-bench, base/head alternated, medians of 3 per run): the non-incremental leaf-edit rebuild went from 43.3 s to 34.7 s for plx_base (-20%), from 23.2 s to 19.1 s for plx_ui dwell.rs (-18%) and from 23.0 s to 19.2 s for the plx_ui hub (-17%). The incremental loop shows no clear change. target plus target-fast shrink by 19%. The root manifest is pinned by two read-only certificates. With the owner's approval, both were re-certified: - ci/proofs/serde-json-immutable.json: the manifest hash, and the ui/src/lib.rs hash, which had been stale since 6a74d2a8b - ci/proofs/platform-generated-enums.json: the manifest hash The comments above check-python-harness (Makefile) and the harness job (ci.yml) now describe #1386's per-rule `ci/check-deps.sh --only` runs. Prose about the test profile's debug level in build-gc.sh, agent-reference.md, module-layers.md, cargo-seed.py and the host-rust action is corrected. Co-Authored-By: Claude Opus 5.5 --- .github/actions/host-rust/action.yml | 18 ++++++++------ .github/workflows/ci.yml | 8 ++++--- Makefile | 18 +++++++------- ci/proofs/platform-generated-enums.json | 2 +- ci/proofs/serde-json-immutable.json | 4 ++-- ci/readonly_dependencies.py | 2 +- ci/readonly_generated.py | 2 +- docs/agent-reference.md | 5 +++- docs/module-layers.md | 4 ++-- rust-modules/Cargo.toml | 25 ++++++++++++++++--- tools/build-gc.sh | 32 +++++++++++++++---------- tools/cargo-seed.py | 2 +- 12 files changed, 79 insertions(+), 43 deletions(-) diff --git a/.github/actions/host-rust/action.yml b/.github/actions/host-rust/action.yml index 02caeadb0f..abd07c37a3 100644 --- a/.github/actions/host-rust/action.yml +++ b/.github/actions/host-rust/action.yml @@ -44,17 +44,21 @@ runs: - name: Build the host crates with line tables only # Nothing in CI attaches a debugger to a host test binary, and a panic backtrace needs only the - # line table. The default `dev`/`test` profile writes full DWARF for every crate, dependencies - # included, which is most of what rustc's codegen, the linker and the cache archive carry for - # the 18 test binaries. This is the per-user cargo config, so it reaches the bare `cargo` - # lines and every `make` target of the host jobs alike. A dependency built with the old + # line table. A `dev` build writes full DWARF for every crate, dependencies included, which is + # most of what rustc's codegen, the linker and the cache archive carry (the 18 test binaries + # too, before rust-modules/Cargo.toml gave `[profile.test]` line tables of its own: `cargo test` + # builds with `test`, which inherits `dev`). This is the per-user cargo config, so it reaches + # the bare `cargo` lines and every `make` target of the host jobs alike, and it trims what the + # manifest does not: the `dev` builds (clippy, `cargo check`) and the third-party crates' + # debug info (`debug = false`). A dependency built with the old # profile is a different unit to cargo, and rust-cache's key does not see this file (it hashes # the manifests, the lockfile, the toolchain files and the workspace's `.cargo/config.toml`, # not the home one), so the cache step below carries its own `prefix-key`: without it the # full-DWARF cache restores as an exact hit, every dependency is rebuilt, and the post step - # finds the cache up to date and saves nothing. It is NOT the manifest: a developer's checkout - # keeps full DWARF (docs/agent-reference.md, the lane policy), and the profiles the ARM binary - # is built with do not inherit `dev`, so the shipped library is untouched. + # finds the cache up to date and saves nothing. The `dev` half is NOT in the manifest: a developer's checkout + # keeps full DWARF in `dev` builds (docs/agent-reference.md, the lane policy; its host tests + # get line tables from the manifest's `[profile.test]`), and the profiles the ARM binary is + # built with do not inherit `dev`, so the shipped library is untouched. shell: bash run: | mkdir -p "$HOME/.cargo" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d3340e629..3389a59845 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -446,9 +446,11 @@ jobs: - name: make check-python-rest run: make check-python-rest - # tests/test_harness.py alone: about 190 s of what used to be a 360 s `check-python` job. Most of - # it is its `DepGates` class running ci/check-deps.sh over private copies of the tree on every - # core of the runner, so it gains nothing from sharing a runner with the other gates. + # tests/test_harness.py alone, split off a 360 s `check-python` job. Most of it is its `DepGates` + # class: about 70 self-tests that each run only the rules they plant a violation for + # (`ci/check-deps.sh --only`, #1386) over private copies of the tree, spread over every core of + # the runner, so it gains nothing from sharing a runner with the other gates. The step took + # 144-196 s on main's commits of 2026-11-14 after #1386 (380-500 s on the ones before it). # `make check-python` is still the two halves, run side by side locally. host-python-harness: name: host harness tests diff --git a/Makefile b/Makefile index bb7d53d69a..1a89831757 100644 --- a/Makefile +++ b/Makefile @@ -1957,15 +1957,15 @@ check-python: harness='$(MAKE) --no-print-directory check-python-harness' check-python-harness: - @# The harness's own host unit tests (tests/test_harness.py, stdlib unittest). THE MOST - @# EXPENSIVE STEP IN `check` BY FAR — 661 s of a 1100 s run, measured 2026-10-01 on a cold lane - @# at load ~5, and 630 s of that is the `DepGates` class running the whole of - @# `ci/check-deps.sh` (11 s a run) once or more per test to prove each structure gate still - @# catches a planted violation. Those self-tests now edit a private copy of the gate's inputs - @# and run on a thread pool (`PLX_TEST_JOBS`, default min(8, cpus)): the module takes 175 s - @# instead of 661 s. (It was 980 s of 1217 s before `check-deps.sh` stopped forking a process - @# per candidate line.) Measure before budgeting, and if this number needs to come down - @# further, `check-deps.sh` itself is the place. run.py + @# The harness's own host unit tests (tests/test_harness.py, stdlib unittest): the harness + @# branch of `check-python` and the whole of CI's `host-python-harness` job. Most of it is the + @# `DepGates` class: about 70 self-tests, each planting ONE violation in a private copy of the + @# gate's inputs and running `ci/check-deps.sh --only ` to prove that + @# rule still catches it (since #1386 a self-test runs its own rules, not all 43), on a thread + @# pool (`PLX_TEST_JOBS`, default min(8, cpus)). Measured 2026-11-14 on CI: this step took + @# 144-196 s on the commits since #1386 and 380-500 s on the ones before it. Measure before + @# budgeting; `check-deps.sh`'s per-rule cost, then the self-test count, is where more would + @# come from. run.py @# decides WHAT gets driven on the one television and had no test of any kind until 2026-08-22. @# What it pins is the code path a full manifest.local.json never enters: an `item` key this @# installation cannot resolve SKIPS the cases that need it instead of killing the run. A diff --git a/ci/proofs/platform-generated-enums.json b/ci/proofs/platform-generated-enums.json index d605a09d8a..fcfd161a24 100644 --- a/ci/proofs/platform-generated-enums.json +++ b/ci/proofs/platform-generated-enums.json @@ -26,7 +26,7 @@ }, "text_hashing": "sha256-utf8-bytes-crlf-to-lf-local-root-version-v1", "manifests": { - "Cargo.toml": "fbeb900767c6d3a7091b0366cdaf79753f45384be9b39a649dd8e7decbf40fb6", + "Cargo.toml": "c57b1c2d662d460a6d71f6de158397f1cb2f2fecb167bfa738631d77b661248b", "appkit/Cargo.toml": "1e3a33f392110f269537b12f12879c6bc15309aca304b189d6b0346edf8a1186", "base/Cargo.toml": "ebe0b95553bd44f33f422f7d8e22b75ddeb53c7ee81bbe738f675690694568f7", "data/Cargo.toml": "90a763a8a9e22bda7e95f8ede4dc10ec6e92a62b90bd0fd2e622c617d75ec6c7", diff --git a/ci/proofs/serde-json-immutable.json b/ci/proofs/serde-json-immutable.json index 6a423f3f0f..b84b7d7881 100644 --- a/ci/proofs/serde-json-immutable.json +++ b/ci/proofs/serde-json-immutable.json @@ -373,7 +373,7 @@ }, "lock_sha256": "c56af60124266be5ee0b7dfa7ae81b4f0ff38b047fe672f40a009f6e1c22d3d3", "manifests": { - "Cargo.toml": "fbeb900767c6d3a7091b0366cdaf79753f45384be9b39a649dd8e7decbf40fb6", + "Cargo.toml": "c57b1c2d662d460a6d71f6de158397f1cb2f2fecb167bfa738631d77b661248b", "appkit/Cargo.toml": "1e3a33f392110f269537b12f12879c6bc15309aca304b189d6b0346edf8a1186", "base/Cargo.toml": "ebe0b95553bd44f33f422f7d8e22b75ddeb53c7ee81bbe738f675690694568f7", "data/Cargo.toml": "90a763a8a9e22bda7e95f8ede4dc10ec6e92a62b90bd0fd2e622c617d75ec6c7", @@ -602,7 +602,7 @@ ], "edition": "2021", "lib": "ui/src/lib.rs", - "lib_sha256": "c77775a6faa9583579ee576ea9f9a51a7b9c92d7ef692161761c1e471faf3873" + "lib_sha256": "616bd784cbf479441275a483f83ed2d88df05db406731bb2ead7a8d70845e557" } }, "text_hashing": "sha256-utf8-bytes-crlf-to-lf-local-root-version-v1" diff --git a/ci/readonly_dependencies.py b/ci/readonly_dependencies.py index b0ec65d11a..70f0661c9e 100644 --- a/ci/readonly_dependencies.py +++ b/ci/readonly_dependencies.py @@ -18,7 +18,7 @@ import tarfile CERTIFICATE_PATH = Path(__file__).with_name('proofs') / 'serde-json-immutable.json' -CERTIFICATE_SHA256 = '7a299a63a440720a7f7313fe985519efb77b6f6a2963b5b0d92fc3bc3be987be' +CERTIFICATE_SHA256 = '29e62dc168d2691cebe44a1ce21b59d186002fed59df18bdb2c2b86d76f0497d' SCHEMA = 'plx.readonly.locked-json.v1' REGISTRY = 'registry+https://github.com/rust-lang/crates.io-index' SUPPORTED = ['alloc', 'arbitrary_precision', 'default', 'float_roundtrip', 'raw_value', 'std', 'unbounded_depth'] diff --git a/ci/readonly_generated.py b/ci/readonly_generated.py index 8e222d851a..a0b8bdd6e9 100644 --- a/ci/readonly_generated.py +++ b/ci/readonly_generated.py @@ -13,7 +13,7 @@ from readonly_dependencies import _local_sha, _sha, _json, _manifest_paths, _configs, _workspace_hashes, WORKSPACE_HASHING CERTIFICATE_PATH = Path(__file__).with_name('proofs') / 'platform-generated-enums.json' -CERTIFICATE_SHA256 = 'b61be9b470960a80bbb097dfa466fa5fd4229f89ef8f1234bb302edabf9f8396' +CERTIFICATE_SHA256 = 'c737368bf0642a51f34c3424f9545eaa8becb72dc2206ae5f1a40ddcc3c7f67e' SOURCES = ['platform/Cargo.toml', 'platform/src/lib.rs', 'platform/src/i18n/mod.rs', 'platform/build.rs', 'platform/build_support/catalog.rs', 'build_support/install_identities.rs', 'machine/src/lib.rs', 'machine/src/machine.rs'] diff --git a/docs/agent-reference.md b/docs/agent-reference.md index 6bd6f0005f..c9a2c32d1d 100644 --- a/docs/agent-reference.md +++ b/docs/agent-reference.md @@ -753,7 +753,10 @@ the pinned Sentry Native cross-build), and `sshpass` (Homebrew; deploy/run use y `tools/build-gc.sh` now installs `.claude/worktrees/.cargo/config.toml` with `incremental = false`, which every cargo reads and which stops above the main checkout. Since 2026-09-18 the same file also sets `[profile.dev] debug = "line-tables-only"` and `debug = false` - for third-party packages in lanes (main keeps full DWARF). A lane's target dirs are also no longer + for third-party packages in lanes (main keeps full DWARF for `dev`: the simulator, clippy and + `cargo check`). A host `cargo test` build is the `test` profile, whose line tables + `rust-modules/Cargo.toml` sets for every checkout, main included; a debugger on a host test needs + `CARGO_PROFILE_TEST_DEBUG=2`. A lane's target dirs are also no longer all built from scratch: in a linked worktree the Makefile seeds an ABSENT target dir with an APFS clone of the third-party output (registry crates, once also the build-std sysroot, about 40% of a lane's target bytes then) from `~/.cache/plxnative/cargo-seed/`, via `tools/cargo-seed.py`; the app crate is diff --git a/docs/module-layers.md b/docs/module-layers.md index 6f91e95f6b..7acababeac 100644 --- a/docs/module-layers.md +++ b/docs/module-layers.md @@ -1941,8 +1941,8 @@ The last no-LTO figure is over the 12,426,000 limit, which made Splits 9 and 10 because whole-program optimisation with a single codegen unit sees the same code the single crate did. They also restore the cross-crate inlining the single crate had, which the per-frame UI code leans on. Only release-profile builds pay for it (the ARM staticlib and the storage helper when built as `release`, the -Linux simulator of `make sim-linux` and `make macapp`); the dev profile that `make check`, the host -tests and the macOS simulator use is untouched. The price is build time: CI's ARM library build +Linux simulator of `make sim-linux` and `make macapp`); the `dev` and `test` profiles that the macOS +simulator, `make check` and the host tests use are untouched. The price is build time: CI's ARM library build went from 2m40 to 4m15, and a local `ARM_PROFILE=release` one from 44 s to 94 s. A plain local `make` / `make deploy` no longer pays it: the Makefile builds the ARM staticlib with `[profile.tvdev]` (no LTO, 16 codegen units), which is faster and larger, and keeps `release` for CI, `RELEASE=1`, `SYMBOLS=1` and diff --git a/rust-modules/Cargo.toml b/rust-modules/Cargo.toml index d76401c786..a9912a2090 100644 --- a/rust-modules/Cargo.toml +++ b/rust-modules/Cargo.toml @@ -299,9 +299,9 @@ serde = { version = "1", default-features = false, features = ["derive", "alloc" # back the cross-crate inlining that the UI's per-frame code relied on. # # What it costs is build time, paid only by release-profile builds: CI's ARM library build went from -# 2m40 to 4m15 and a local `ARM_PROFILE=release` one from 44 s to 94 s. `make check` and the host tests run the dev profile -# and do not pay it, and neither does a plain local `make` / `make deploy`: the Makefile builds those -# with `[profile.tvdev]` below. Release-profile builds are CI, `RELEASE=1`, `SYMBOLS=1`, +# 2m40 to 4m15 and a local `ARM_PROFILE=release` one from 44 s to 94 s. `make check` and the host tests (the `test` +# profile, which inherits `dev`) do not pay it, and neither does a plain local `make` / `make deploy`: +# the Makefile builds those with `[profile.tvdev]` below. Release-profile builds are CI, `RELEASE=1`, `SYMBOLS=1`, # `FLAVOR=stable|nightly` and `make ARM_PROFILE=release`. [profile.release] opt-level = 2 @@ -321,6 +321,25 @@ inherits = "release" lto = "off" codegen-units = 16 +# The host UNIT-TEST build (`cargo test`: `make check`'s `check-cargo-unit-*`, `make test-fast`, +# `make test-crate`, `make test-hostsim`, `make build-bench`): every unit of such a build, the +# workspace libraries and the third-party crates under the test binaries included, is built with +# THIS profile and not with `dev` (`cargo test --unit-graph` names the profile of each unit). Line +# tables keep a panic backtrace's file:line and drop the variable-level DWARF; nothing here attaches +# a debugger to a test binary. Measured 2026-11-14 on a checkout with full-DWARF `dev` (the main +# checkout's setting), `make build-bench`, medians: the non-incremental edit rebuild of the test +# build took 14-22% less (plx_base leaf 39.8-43.5 s -> 33.9-35.9 s; plx_ui leaf 21.7-23.4 s -> +# 18.5-20.1 s) and `target/` + `target-fast/` shrank 19% (9.6 GB -> 7.8 GB); the incremental +# `make test-fast` loop was not clearly moved (a plx_ui edit 6.0 -> 6.2 s, a plx_base edit 16.1-21.8 s +# -> 15.5-16.1 s with the baseline drifting between runs). `[profile.dev]` is untouched: the +# simulator, clippy and `cargo check` keep full DWARF in a developer's checkout. Lanes and CI already +# trim `dev` through a cargo config (tools/build-gc.sh, .github/actions/host-rust/action.yml), +# which `test` inherits, so for them this line changes nothing (the unit graph is identical); the +# main checkout is where it takes effect. A debugger on a host test needs +# `CARGO_PROFILE_TEST_DEBUG=2` (`CARGO_PROFILE_DEV_DEBUG` no longer reaches a `cargo test` build). +[profile.test] +debug = "line-tables-only" + [dev-dependencies] # The `cfg(test)` fixtures of the base layer (`testlock`, `testnet`, ...): a dependency is never # built `cfg(test)`, so the tests here ask for them by feature. diff --git a/tools/build-gc.sh b/tools/build-gc.sh index 7a97c3a38f..af50bea831 100755 --- a/tools/build-gc.sh +++ b/tools/build-gc.sh @@ -107,8 +107,8 @@ install_worktree_cargo_policy() { # # Cargo merges config upward from the working directory, so this file applies to every linked # worktree under `.claude/worktrees/` and to NOTHING else: the main checkout lives above this -# directory and keeps its incremental cache and full DWARF, which is the policy the Makefile -# states beside `RUST_FEATFLAGS`. The Makefile can only enforce it for its own cargo invocations; +# directory and keeps its incremental cache and full DWARF in `dev` builds, which is the policy the +# Makefile states beside `RUST_FEATFLAGS`. The Makefile can only enforce it for its own cargo invocations; # an agent running `cargo test` or `cargo check` by hand bypassed it, and 12.9 GB of lane # incremental caches is what that cost. # @@ -137,17 +137,25 @@ incremental = false # 11% cut to the whole tree and a 26% cut to the object files directly, from ONE crate's `--lib` # closure. The saving is modest, plainly: most of the object-file bulk is code, not DWARF. # -# `CARGO_PROFILE_DEV_DEBUG=2` in the environment still overrides this, the same escape hatch as -# `CARGO_INCREMENTAL=1` above: an env var outranks a config file, so a lane genuinely attaching -# `lldb`/`rust-lldb` to a host test can buy full DWARF back for itself without touching this file. +# `CARGO_PROFILE_DEV_DEBUG=2` in the environment still overrides this for a `dev` build (the +# simulator, clippy, `cargo check`), the same escape hatch as `CARGO_INCREMENTAL=1` above: an env +# var outranks a config file. A host TEST is another profile (since 2026-11-14): `cargo test` builds +# every unit, third-party crates included, with `[profile.test]`, and rust-modules/Cargo.toml sets +# that one to line tables itself, which a `[profile.dev]` setting (this file's or the +# environment's) no longer reaches. A lane genuinely attaching `lldb`/`rust-lldb` to a host test +# buys full DWARF back for itself with `CARGO_PROFILE_TEST_DEBUG=2`; third-party crates keep the +# `debug = false` below either way. # -# Checked before landing that nothing else in the nearer config chain already sets this and would -# make the addition a no-op or a conflict: neither `rust-modules/Cargo.toml` (only -# `[profile.release]`) nor `rust-modules/.cargo/config.toml` (only `[target...]` rustflags and -# formerly `[unstable] build-std`) mentions `[profile.dev]` debug. Cargo resolves a manifest `[profile]` -# against a config `[profile]` by letting config win, and the nearest config wins over a farther -# one — both rules favour this file over anything upstream, which is the reason it is safe to add -# here rather than in the crate manifest. +# Checked before landing, for `dev` builds, that nothing else in the nearer config chain already +# sets this and would make the addition a no-op or a conflict: neither `rust-modules/Cargo.toml` +# (its profiles are `release`, `tvdev` and, since 2026-11-14, `test`; none is `dev`) nor +# `rust-modules/.cargo/config.toml` (only `[target...]` rustflags and formerly `[unstable] +# build-std`) mentions `[profile.dev]` debug. Cargo resolves a manifest `[profile]` against a +# config `[profile]` by letting config win, and the nearest config wins over a farther one — both +# rules favour this file over anything upstream, which is the reason it is safe to add here rather +# than in the crate manifest. A `cargo test` build takes its debug level from `[profile.test]` +# instead (the paragraph above), so for it the `debug` line below is redundant; the +# `debug = false` for third-party crates is what this file still adds there. [profile.dev] debug = "line-tables-only" [profile.dev.package."*"] diff --git a/tools/cargo-seed.py b/tools/cargo-seed.py index cfd4abea9e..8ead422b34 100755 --- a/tools/cargo-seed.py +++ b/tools/cargo-seed.py @@ -26,7 +26,7 @@ * **It clones or it does nothing.** `clonefile(2)` is called directly (not `cp -c`, which quietly falls back to a full copy, spending the very disk this exists to save). Another volume, a filesystem without cloning, or any platform other than macOS: a no-op, never a copy. -* **Linked worktrees only.** The main checkout keeps its own incremental cache and full DWARF, +* **Linked worktrees only.** The main checkout keeps its own incremental cache and full DWARF in `dev` builds, which no lane could share, so the Makefile does not call this there and the script refuses too. `PLX_CARGO_SEED=off` disables it everywhere. * **A key guards the seed**: the sha256 of the toolchain (`rustc + -vV` and `rustc -vV`), From e6200feceb74f1450b67af09490c0b02d3e158d1 Mon Sep 17 00:00:00 2001 From: valentinBoch <33367816+valentinBoch@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:56:32 +0100 Subject: [PATCH 6/8] feat(network): prefer IPv6 with a bounded IPv4 fallback (#1320) --- rust-modules/plex/src/plex/CLAUDE.md | 11 +- rust-modules/plex/src/plex/probe.rs | 32 +-- rust-modules/session/src/auth.rs | 142 ++++++++-- rust-modules/session/src/auth/ports.rs | 42 ++- .../session/src/auth_discovery_tests.rs | 254 ++++++++++++++++-- 5 files changed, 415 insertions(+), 66 deletions(-) diff --git a/rust-modules/plex/src/plex/CLAUDE.md b/rust-modules/plex/src/plex/CLAUDE.md index f12d8f4220..03473b938e 100644 --- a/rust-modules/plex/src/plex/CLAUDE.md +++ b/rust-modules/plex/src/plex/CLAUDE.md @@ -300,8 +300,15 @@ be registered. Direct identity candidates settle as one race, and a relay fallba local/remote probe opportunity. **The race does not wait for a route that cannot change the answer** (`auth::race_decided`): once a verified, credential-eligible winner exists, a candidate that cannot outrank it (`candidate_score` is a function of the candidate alone) is left behind, and -a better-ranked one that has not answered gets `ProbeDeadlines::grace` (1.5 s) from the first -win, not its own 5 s/10 s deadline. Waiting for every candidate made a healthy sign-in cost a +a higher-scored or better-tier route that has not answered gets `ProbeDeadlines::grace` +(1.5 s) from the first verified answer, not its own 5 s/10 s deadline. An IPv6 +route at the same location, scheme and score as the current IPv4 winner gets only +250 ms **from that IPv4 route's own verified reply**, even if a different higher +tier remains outstanding. Another address of the same family and score cannot +displace the accepted route or extend its window. These are per-route eligibility +deadlines, enforced both when a reply arrives and while deciding which pending +routes to wait for. Candidate ranking is IPv6-first within location/scheme, +with both families retained and no user preference. Waiting for every candidate made a healthy sign-in cost a dead public address's full deadline, so "taking longer than usual" ran on every sign-in (`screens/login.rs`'s `DISCOVERY_ESCAPE_AFTER_MS` derives its number from this cost). A race with no winner still runs every candidate to its deadline, and the relay phase still starts only then. Authenticated fallback attempts share a separate 20-second budget, diff --git a/rust-modules/plex/src/plex/probe.rs b/rust-modules/plex/src/plex/probe.rs index 6fbcd7e6b4..55c366b622 100644 --- a/rust-modules/plex/src/plex/probe.rs +++ b/rust-modules/plex/src/plex/probe.rs @@ -26,8 +26,9 @@ //! transcodes down to fit, a last resort and never a preference. The SCHEME order flipped when //! the TLS control plane landed — https is the only connection a certificate can authenticate //! and the only one that works from outside the LAN, so it now leads (see -//! [`Scheme`](super::origin::Scheme), whose declaration order IS this ranking). Then IPv4 before -//! IPv6, and a numeric literal before a HOSTNAME ([`is_numeric_address`]) — a WEAKER tiebreak +//! [`Scheme`](super::origin::Scheme), whose declaration order IS this ranking). Then IPv6 before +//! IPv4 within a tier (IPv4 remains eligible), and a numeric literal before a HOSTNAME +//! ([`is_numeric_address`]) — a WEAKER tiebreak //! than it was, because both transports resolve names now, and one that is still worth having: //! a literal costs no DNS round trip, and on a LAN with no route to the internet it is the only //! thing that resolves at all. @@ -469,16 +470,16 @@ pub fn candidates(res: &Resource, policy: CredentialPolicy) -> Vec { ); } } - // Stable, so plex.tv's own order survives inside a tier — it is the only tiebreak left once - // location, scheme, resolvability and address family have spoken, and it is not ours to reorder. - // Resolvability is read off the URL's own host, not `address` — see `origin::url_host`, which - // is the difference between scoring the `plex.direct` uri and scoring the quad hiding behind it. + // IPv6 gets the first opportunity within the same location/scheme tier; IPv4 + // stays in the race as fallback. Keep the DNS-independent literal preference inside + // each family, and preserve Plex's advertised order for equal keys (stable sort). + // Read resolvability from the URL's own host, not the advertised address. out.sort_by_key(|c| { ( c.location, c.scheme, + !c.ipv6, !is_numeric_address(url_host(&c.url)), - c.ipv6, ) }); out @@ -1391,23 +1392,22 @@ mod tests { ], "{cs:#?}" ); - // https first inside the tier, and IPv4 before IPv6 within that — the second key and the - // fourth, in that order. `2001-db8--1.hash1.plex.direct` is a NAME whose address family is - // v6, which is exactly why `Candidate::ipv6` is carried rather than re-read off the url. + // Within the same location and scheme, IPv6 leads and IPv4 remains a fallback. + // `Candidate::ipv6` is metadata from Plex, not inferred from the plex.direct hostname. assert_eq!( - cs[0].url, "https://192-168-0-10.hash1.plex.direct:32400", - "TLS leads its tier: {cs:#?}" + cs[0].url, "https://2001-db8--1.hash1.plex.direct:32400", + "IPv6 TLS leads its tier: {cs:#?}" ); assert!( - !cs[0].ipv6 && cs[1].ipv6, - "the v6 uri is next, not first: {cs:#?}" + cs[0].ipv6 && !cs[1].ipv6, + "IPv4 must remain eligible immediately after IPv6: {cs:#?}" ); assert_eq!( cs.iter() .find(|c| c.scheme == Scheme::Http) .map(|c| c.url.as_str()), - Some("http://192.168.0.10:32400"), - "…and the plaintext fallbacks are ordered the same way: {cs:#?}" + Some("http://[2001:db8::1]:32400"), + "the IPv6 plaintext twin leads the IPv4 fallback: {cs:#?}" ); let last = cs.last().expect("a relay candidate"); diff --git a/rust-modules/session/src/auth.rs b/rust-modules/session/src/auth.rs index b01684c64a..bced03c971 100644 --- a/rust-modules/session/src/auth.rs +++ b/rust-modules/session/src/auth.rs @@ -2011,6 +2011,10 @@ struct Winner { candidate: Candidate, origin: Origin, score: i32, + /// Network round-trip of the verified identity probe (not the whole race). + latency: Duration, + /// Moment the worker completed its verified reply, not when the coordinator read it. + answered_at: Instant, } struct ProbeMessage { @@ -2018,6 +2022,9 @@ struct ProbeMessage { on_time: bool, outcome: Outcome, route: RouteOutcome, + /// Wall time spent dialling this candidate, for family-selection diagnostics. + latency: Duration, + answered_at: Instant, /// [`ProbeReply::untrusted`] of this candidate's reply. untrusted: Option, } @@ -2028,6 +2035,7 @@ const PROBE_EXPIRED: u8 = 2; #[derive(Clone)] struct PendingProbe { + started: Instant, deadline: Instant, state: Arc, } @@ -2103,6 +2111,7 @@ fn settle_probe_message( pending: &mut [Option], live: &mut usize, result: &mut BatchResult, + grace: Duration, activate: &mut dyn FnMut(&ProbePlan, &Candidate, &Origin), ) { let Some(_pending) = pending.get_mut(message.index).and_then(Option::take) else { @@ -2117,12 +2126,30 @@ fn settle_probe_message( return; } let c = &plan.candidates[message.index]; + let family = if c.ipv6 { "IPv6" } else { "IPv4" }; + let outcome = match message.outcome { + Outcome::Reachable => "verified", + Outcome::Unauthorized => "unauthorized", + Outcome::WrongServer => "wrong_server", + Outcome::Unreachable => "unreachable", + Outcome::InsecureOnly => "insecure_only", + }; + // The transport already logs unreachable probes; avoid turning a DNS/timeout failure + // into an additional per-candidate diagnostic line (see Outcome::Unreachable below). + if !matches!(message.outcome, Outcome::Unreachable) { + log(&format!( + "auth: '{}' probe at {}:{} tier={:?} scheme={:?} family={family} result={outcome} latency_ms={}", + plan.name, c.address, c.port, c.location, c.scheme, message.latency.as_millis(), + )); + } match message.outcome { Outcome::Reachable => { let Some(origin) = dial_target(c) else { return }; let winner = Winner { index: message.index, score: candidate_score(c, &origin), + latency: message.latency, + answered_at: message.answered_at, candidate: c.clone(), origin, }; @@ -2155,7 +2182,12 @@ fn settle_probe_message( activate(plan, &winner.candidate, &winner.origin); result.first = Some(winner.clone()); } - if result.best.as_ref().is_none_or(|old| better(&winner, old)) { + // A candidate that finishes after its individual winning window cannot + // displace an already-verified route, even if another tier keeps the race open. + let first_at = result.first.as_ref().expect("first set above").answered_at; + if result.best.as_ref().is_none_or(|old| { + candidate_can_replace(&winner, old, first_at, grace) + }) { result.best = Some(winner); } } @@ -2185,37 +2217,98 @@ fn settle_probe_message( } } -/// Could candidate `index`, which has not answered yet, still replace `best`? Its score is a -/// function of the candidate alone ([`candidate_score`]), so this is known BEFORE it answers; a -/// candidate this build cannot put a credential on never becomes `best` ([`settle_probe_message`]) -/// and so never counts. -fn could_outrank(plan: &ProbePlan, index: usize, best: &Winner) -> bool { - let c = &plan.candidates[index]; - if !c.credential_eligible { - return false; +/// An individual candidate's ability to improve a verified winner. Candidate score is +/// authoritative, but index *alone* cannot replace a successful same-family address. +/// An IPv6 candidate at the same location/scheme/score gets a bounded opportunity +/// after its IPv4 counterpart answers; genuinely better routes retain the usual grace. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum WinWindow { + BetterTier, + Ipv6Twin, +} + +const IPV6_FALLBACK_GRACE: Duration = Duration::from_millis(250); + +fn winning_window(index: usize, candidate: &Candidate, score: i32, best: &Winner) -> Option { + if !candidate.credential_eligible { + return None; + } + let better_tier = score > best.score + || (score == best.score + && index < best.index + && (candidate.location, candidate.scheme) + < (best.candidate.location, best.candidate.scheme)); + if better_tier { + return Some(WinWindow::BetterTier); + } + if score == best.score + && index < best.index + && candidate.ipv6 && !best.candidate.ipv6 + && candidate.location == best.candidate.location + && candidate.scheme == best.candidate.scheme + { + return Some(WinWindow::Ipv6Twin); } - let Some(origin) = dial_target(c) else { return false }; - let score = candidate_score(c, &origin); - score > best.score || (score == best.score && index < best.index) + None } -/// **Is the race over, with some candidates still unanswered?** Pure, so the rule is gradeable -/// on the host without a socket or a clock. -/// -/// A race has to wait for every candidate only while one of them could still change the answer. -/// Once a verified, eligible winner exists, a candidate that cannot outrank it is irrelevant, and -/// one that could (a better-ranked route) is given `grace` from the first win and no longer. -/// `since_first_win` is `None` before any win. This is what keeps a healthy sign-in from waiting -/// out a dead route's full deadline (5 s local, 10 s remote) behind an answer it already has. +fn candidate_can_replace( + candidate: &Winner, best: &Winner, first_at: Instant, grace: Duration, +) -> bool { + match winning_window(candidate.index, &candidate.candidate, candidate.score, best) { + Some(WinWindow::BetterTier) => candidate.answered_at < first_at + grace, + Some(WinWindow::Ipv6Twin) => + candidate.answered_at < best.answered_at + IPV6_FALLBACK_GRACE, + None => false, + } +} + +/// A pending candidate's remaining *individual* opportunity to become the winner. +/// Pure, so test clocks can advance without any worker, socket or real sleep. +fn pending_win_time( + plan: &ProbePlan, index: usize, best: &Winner, + since_first: Duration, since_best: Duration, grace: Duration, +) -> Option { + let candidate = plan.candidates.get(index)?; + if !candidate.credential_eligible { + return None; + } + let origin = dial_target(candidate)?; + let score = candidate_score(candidate, &origin); + let window = match winning_window(index, candidate, score, best) { + Some(WinWindow::BetterTier) => grace.saturating_sub(since_first), + Some(WinWindow::Ipv6Twin) => IPV6_FALLBACK_GRACE.saturating_sub(since_best), + None => Duration::ZERO, + }; + (window > Duration::ZERO).then_some(window) +} + +fn max_pending_win_time( + plan: &ProbePlan, undecided: &[usize], best: Option<&Winner>, + since_first: Option, since_best: Option, grace: Duration, +) -> Option { + let (Some(best), Some(since_first), Some(since_best)) = (best, since_first, since_best) + else { return None }; + undecided.iter().filter_map(|&index| { + pending_win_time(plan, index, best, since_first, since_best, grace) + }).max() +} + +/// The decision is complete when *no* pending route still has a winning window. +/// Better tiers get 1.5 s from the first answer; equivalent IPv6 gets 250 ms from +/// the current IPv4 winner's own answer, even if another tier keeps the race open. +/// Pending same-family ties never hold the race open. fn race_decided( plan: &ProbePlan, undecided: &[usize], best: Option<&Winner>, since_first_win: Option, + since_best_win: Option, grace: Duration, ) -> bool { - let (Some(best), Some(since)) = (best, since_first_win) else { return false }; - since >= grace || !undecided.iter().any(|&i| could_outrank(plan, i, best)) + best.is_some() && max_pending_win_time( + plan, undecided, best, since_first_win, since_best_win, grace, + ).is_none() } /// Race one phase of a server's candidates. The spawner is injected because refusal is a result @@ -2298,6 +2391,11 @@ fn probe_server_racing( if first.index != best.index { activate(plan, &best.candidate, &best.origin); } + log(&format!( + "auth: selected family={} probe_latency_ms={}", + if best.candidate.ipv6 { "IPv6" } else { "IPv4" }, + best.latency.as_millis(), + )); Reach::At(best.candidate, best.origin) } diff --git a/rust-modules/session/src/auth/ports.rs b/rust-modules/session/src/auth/ports.rs index 8a46178e9d..0bcc6aa990 100644 --- a/rust-modules/session/src/auth/ports.rs +++ b/rust-modules/session/src/auth/ports.rs @@ -211,7 +211,10 @@ pub(super) fn race_batch( let pin = plx_plex::plex::ResolvePin::for_origin(&origin, &c.address); let location = c.location; let job = Box::new(move || { + let dial_started = clock_now(); let reply = dial(&origin, pin.as_ref(), budget); + let answered_at = clock_now(); + let latency = answered_at.saturating_duration_since(dial_started); let untrusted = reply.untrusted(); let (outcome, route) = reply.grade_learning(&machine_id, location); let on_time = clock_now() <= deadline; @@ -233,12 +236,14 @@ pub(super) fn race_batch( on_time, outcome, route, + latency, + answered_at, untrusted, }); } }); if spawn(index, job) { - pending[index] = Some(PendingProbe { deadline, state }); + pending[index] = Some(PendingProbe { started, deadline, state }); live += 1; } } @@ -250,7 +255,6 @@ pub(super) fn race_batch( observed: vec![None; plan.candidates.len()], ..BatchResult::default() }; - let mut first_win: Option = None; while live > 0 { // Drain results that completed on time BEFORE expiring by the coordinator's current clock. // Spawn setup and queue backlog are allowed to delay observation; `finished` is the fact @@ -263,6 +267,7 @@ pub(super) fn race_batch( &mut pending, &mut live, &mut result, + policy.grace, activate, ), Err(mpsc::TryRecvError::Empty) => break, @@ -292,22 +297,27 @@ pub(super) fn race_batch( .is_ok() }); if expired { + let waited = pending[index].as_ref() + .map_or(Duration::ZERO, |p| clock_elapsed(p.started)); pending[index] = None; live -= 1; result.observed[index] = Some(RouteOutcome::Timeout); let c = &plan.candidates[index]; log(&format!( - "auth: '{}' probe timed out at {}:{}", - plan.name, c.address, c.port + "auth: '{}' probe timed out at {}:{} tier={:?} scheme={:?} family={} waited_ms={}", + plan.name, c.address, c.port, c.location, c.scheme, + if c.ipv6 { "IPv6" } else { "IPv4" }, waited.as_millis(), )); } } if live == 0 { break; } - if first_win.is_none() && result.first.is_some() { - first_win = Some(clock_now()); - } + // Both deadlines are anchored to the worker's verified answer, not the time + // the coordinator read the message. A newly promoted IPv4 route gets its OWN + // 250ms IPv6 window, independently of an earlier remote answer. + let first_win = result.first.as_ref().map(|w| w.answered_at); + let best_win = result.best.as_ref().map(|w| w.answered_at); // A candidate whose worker already claimed completion owes a message that is about to // land, so it is never skipped; only one still dialling can be left behind. let undecided: Vec = indices @@ -325,6 +335,7 @@ pub(super) fn race_batch( &undecided, result.best.as_ref(), first_win.map(clock_elapsed), + best_win.map(clock_elapsed), policy.grace, ) { @@ -342,6 +353,13 @@ pub(super) fn race_batch( .is_ok() { left_behind += 1; + let c = &plan.candidates[index]; + log(&format!( + "auth: '{}' probe not waited for at {}:{} tier={:?} scheme={:?} family={} waited_ms={}", + plan.name, c.address, c.port, c.location, c.scheme, + if c.ipv6 { "IPv6" } else { "IPv4" }, + clock_elapsed(p.started).as_millis(), + )); } } } @@ -358,9 +376,12 @@ pub(super) fn race_batch( .filter_map(|&i| pending[i].as_ref()) .filter(|p| p.state.load(Ordering::Acquire) == PROBE_PENDING) .map(|p| p.deadline) - // Wake for the end of the grace too, but only while it is still ahead: a past one - // would turn the wait below into a zero-timeout spin. - .chain(first_win.map(|at| at + policy.grace).filter(|end| *end > clock_now())) + // Wake when the *last eligible* pending route's window expires; a + // same-family dead address cannot extend another IPv6 answer's window. + .chain(max_pending_win_time( + plan, &undecided, result.best.as_ref(), + first_win.map(clock_elapsed), best_win.map(clock_elapsed), policy.grace, + ).map(|wait| clock_now() + wait)) .min(); let received = match next { Some(next) => rx.recv_timeout(next.saturating_duration_since(clock_now())), @@ -375,6 +396,7 @@ pub(super) fn race_batch( &mut pending, &mut live, &mut result, + policy.grace, activate, ), Err(mpsc::RecvTimeoutError::Timeout) => {} diff --git a/rust-modules/session/src/auth_discovery_tests.rs b/rust-modules/session/src/auth_discovery_tests.rs index 52b8342a84..71a76ca4e5 100644 --- a/rust-modules/session/src/auth_discovery_tests.rs +++ b/rust-modules/session/src/auth_discovery_tests.rs @@ -334,17 +334,238 @@ fn the_race_ends_early_only_when_no_unanswered_route_could_outrank_the_winner() let winner = |index: usize| { let candidate = plan.candidates[index].clone(); let origin = dial_target(&candidate).unwrap(); - Winner { index, score: candidate_score(&candidate, &origin), candidate, origin } + Winner { index, score: candidate_score(&candidate, &origin), + latency: Duration::ZERO, answered_at: Instant::now(), candidate, origin } }; let (local, remote) = (winner(0), winner(1)); - assert!(!race_decided(&plan, &[1], None, None, grace), "no winner, nothing to decide"); - assert!(race_decided(&plan, &[1], Some(&local), Some(Duration::ZERO), grace), + assert!(!race_decided(&plan, &[1], None, None, None, grace), "no winner, nothing to decide"); + assert!(race_decided(&plan, &[1], Some(&local), Some(Duration::ZERO), Some(Duration::ZERO), grace), "a pending remote cannot outrank the local winner"); - assert!(!race_decided(&plan, &[0], Some(&remote), Some(Duration::from_millis(1_499)), grace), + assert!(!race_decided(&plan, &[0], Some(&remote), Some(Duration::from_millis(1_499)), Some(Duration::from_millis(1_499)), grace), "a pending local could still outrank a remote winner, inside the grace"); - assert!(race_decided(&plan, &[0], Some(&remote), Some(grace), grace), + assert!(race_decided(&plan, &[0], Some(&remote), Some(grace), Some(grace), grace), "…and is not waited for once the grace is spent"); - assert!(race_decided(&plan, &[], Some(&remote), Some(Duration::ZERO), grace)); + assert!(race_decided(&plan, &[], Some(&remote), Some(Duration::ZERO), Some(Duration::ZERO), grace)); +} + +/// One Plex server with a v6/v4 pair in the same location/security tier. +fn dual_stack_race_plan() -> ProbePlan { + let mut plan = race_plan(); + plan.candidates[0].address = "2001:db8::10".into(); + plan.candidates[0].url = "https://[2001:db8::10]:32400".into(); + plan.candidates[0].ipv6 = true; + plan.candidates[1].location = probe::Location::Local; + plan.candidates[1].address = "192.0.2.10".into(); + plan.candidates[1].url = "https://192.0.2.10:32400".into(); + plan +} + +/// A black-holed IPv6 route must not make the working IPv4 route wait 1.5 seconds. +#[test] +fn dual_stack_dead_ipv6_costs_at_most_a_quarter_second_after_ipv4_answers() { + let plan = dual_stack_race_plan(); + let dial = status_dial(|origin, _, _| { + if origin.host().contains(':') { + std::thread::sleep(Duration::from_secs(1)); + return (0, vec![]); + } + (200, identity_json("race-machine")) + }); + let policy = ProbeDeadlines { + local: Duration::from_secs(3), + remote: Duration::from_secs(3), + grace: Duration::from_millis(1_500), + }; + let started = Instant::now(); + let reached = probe_server_racing(&plan, dial, &threaded_spawn, policy, &mut |_, _, _| {}); + let elapsed = started.elapsed(); + assert!(matches!(reached, Reach::At(ref c, _) if !c.ipv6)); + assert!(elapsed >= Duration::from_millis(230), "IPv6 gets a real chance: {elapsed:?}"); + assert!(elapsed < Duration::from_millis(750), "IPv4 must not pay the 1.5s grace: {elapsed:?}"); +} + +#[test] +fn dual_stack_ipv6_that_answers_inside_the_quarter_second_wins() { + // Fixed-clock replacement check: no scheduler race or exact activation-order assertion. + let plan = dual_stack_race_plan(); + let base = Instant::now(); + let mut ipv4 = review_winner(&plan, 1); + let mut ipv6 = review_winner(&plan, 0); + ipv4.answered_at = base + Duration::from_millis(20); + ipv6.answered_at = base + Duration::from_millis(120); + assert!(candidate_can_replace(&ipv6, &ipv4, ipv4.answered_at, PROBE_DEADLINES.grace), + "IPv6 remains the preferred winner when it replies 100ms after same-tier IPv4"); +} + +#[test] +fn dual_stack_slow_but_working_ipv6_does_not_override_fast_ipv4() { + let plan = dual_stack_race_plan(); + let dial = status_dial(|origin, _, _| { + if origin.host().contains(':') { + std::thread::sleep(Duration::from_millis(700)); + } + (200, identity_json("race-machine")) + }); + let started = Instant::now(); + let reached = probe_server_racing(&plan, dial, &threaded_spawn, + PROBE_DEADLINES, &mut |_, _, _| {}); + assert!(matches!(reached, Reach::At(ref c, _) if !c.ipv6)); + assert!(started.elapsed() < Duration::from_millis(600), + "the fast IPv4 answer is not displaced after 250ms"); +} + +#[test] +fn short_ipv6_grace_applies_only_to_the_same_tier_and_scheme() { + let plan = dual_stack_race_plan(); + let winner = |plan: &ProbePlan, index: usize| { + let candidate = plan.candidates[index].clone(); + let origin = dial_target(&candidate).unwrap(); + Winner { index, score: candidate_score(&candidate, &origin), + latency: Duration::ZERO, answered_at: Instant::now(), candidate, origin } + }; + let ipv4 = winner(&plan, 1); + assert_eq!( + pending_win_time(&plan, 0, &ipv4, Duration::ZERO, Duration::ZERO, PROBE_DEADLINES.grace), + Some(Duration::from_millis(250)), + ); + assert_eq!( + max_pending_win_time(&plan, &[0], None, None, None, PROBE_DEADLINES.grace), + None, + ); + let mut different_location = plan; + different_location.candidates[1].location = probe::Location::Remote; + let remote = winner(&different_location, 1); + assert_eq!( + pending_win_time(&different_location, 0, &remote, Duration::ZERO, Duration::ZERO, PROBE_DEADLINES.grace), + Some(PROBE_DEADLINES.grace), + "local IPv6 is a genuinely better tier, not a same-tier twin", + ); + let mut different_scheme = different_location; + different_scheme.candidates[1].location = probe::Location::Local; + different_scheme.candidates[1].scheme = probe::Scheme::Http; + let insecure = winner(&different_scheme, 1); + assert_eq!( + pending_win_time(&different_scheme, 0, &insecure, Duration::ZERO, Duration::ZERO, PROBE_DEADLINES.grace), + Some(PROBE_DEADLINES.grace), + "HTTPS has a stronger security tier, not a same-scheme twin", + ); +} + +// Review #1320: fixed-clock regressions, deliberately added before the race fix. +fn review_winner(plan: &ProbePlan, index: usize) -> Winner { + let candidate = plan.candidates[index].clone(); + let origin = dial_target(&candidate).expect("fixture origin"); + Winner { + index, score: candidate_score(&candidate, &origin), + latency: Duration::ZERO, answered_at: Instant::now(), candidate, origin, + } +} + +#[test] +fn review_two_ipv6_addresses_do_not_wait_for_a_dead_same_family_peer() { + let mut plan = dual_stack_race_plan(); + let mut v6b = plan.candidates[0].clone(); + v6b.url = "https://[2001:db8::11]:32400".into(); + v6b.address = "2001:db8::11".into(); + plan.candidates.insert(1, v6b); + // v4 answered at t=0; working v6b answers at t=100ms; v6a is silent. + let v6b = review_winner(&plan, 1); + assert!(race_decided(&plan, &[0], Some(&v6b), + Some(Duration::from_millis(100)), Some(Duration::ZERO), Duration::from_millis(1_500)), + "an earlier index of the same family/tier must not hold a completed IPv6 race open"); +} + +#[test] +fn review_slow_ipv6_cannot_replace_remote_ipv4_while_local_is_hung() { + let mut plan = dual_stack_race_plan(); + plan.candidates[0].location = probe::Location::Remote; + plan.candidates[1].location = probe::Location::Remote; + let local = race_plan().candidates[0].clone(); // hanging local HTTPS + plan.candidates.insert(0, local); + let v4 = review_winner(&plan, 2); + let mut v6 = review_winner(&plan, 1); + // The two remote routes are equivalent; the actual reply timings are fixed here. + // v4 replies at 30ms; v6 replies at 1230ms while local is still hanging. + let base = Instant::now(); + let mut fast_v4 = v4; + fast_v4.answered_at = base + Duration::from_millis(30); + v6.answered_at = base + Duration::from_millis(1_230); + assert!(!candidate_can_replace(&v6, &fast_v4, fast_v4.answered_at, + Duration::from_millis(1_500)), + "same-tier IPv6 answering 1.2s later cannot replace an already-usable IPv4 route"); +} + +#[test] +fn review_new_local_ipv4_starts_its_own_ipv6_window_not_the_remote_first_win() { + let mut plan = dual_stack_race_plan(); + // Remote v4 wins at t=0; local v4 becomes best at t=800ms. + let mut remote_v4 = plan.candidates[1].clone(); + remote_v4.location = probe::Location::Remote; + plan.candidates.push(remote_v4); + let mut local_v4 = review_winner(&plan, 1); + let mut remote = review_winner(&plan, 2); + let base = Instant::now(); + remote.answered_at = base; + local_v4.answered_at = base + Duration::from_millis(800); + assert!(candidate_can_replace(&local_v4, &remote, remote.answered_at, + Duration::from_millis(1_500)), "a better local route can replace remote inside the full grace"); + assert!(!race_decided(&plan, &[0], Some(&local_v4), + Some(Duration::from_millis(800)), Some(Duration::ZERO), Duration::from_millis(1_500)), + "local v6 still has 250ms from the local v4 answer despite the earlier remote win"); + assert!(race_decided(&plan, &[0], Some(&local_v4), + Some(Duration::from_millis(1_050)), Some(Duration::from_millis(250)), Duration::from_millis(1_500)), + "the local IPv6 window must close 250ms after its local IPv4 twin answered"); +} + +#[test] +fn review_higher_rank_local_route_keeps_full_grace_from_first_answer() { + let plan = race_plan(); + let remote = review_winner(&plan, 1); + assert!(!race_decided(&plan, &[0], Some(&remote), + Some(Duration::from_millis(1_499)), Some(Duration::from_millis(1_499)), Duration::from_millis(1_500))); + assert!(race_decided(&plan, &[0], Some(&remote), + Some(Duration::from_millis(1_500)), Some(Duration::from_millis(1_500)), Duration::from_millis(1_500))); +} + +#[test] +fn review_same_tier_ipv6_fallback_window_works_for_relay_too() { + let mut plan = dual_stack_race_plan(); + for candidate in &mut plan.candidates { + candidate.location = probe::Location::Relay; + } + let relay_v4 = review_winner(&plan, 1); + assert!(!race_decided(&plan, &[0], Some(&relay_v4), + Some(Duration::from_millis(249)), Some(Duration::from_millis(249)), Duration::from_millis(1_500))); + assert!(race_decided(&plan, &[0], Some(&relay_v4), + Some(Duration::from_millis(250)), Some(Duration::from_millis(250)), Duration::from_millis(1_500))); +} + +#[test] +fn review_ipv6_twin_window_survives_first_win_grace_expiry() { + let plan = dual_stack_race_plan(); + let local_v4 = review_winner(&plan, 1); + // A remote answer arrived first, then this local IPv4 only at t=1490ms. + // At t=1600ms the generic first-answer grace is over, but this IPv6 twin + // still has 140ms left in its OWN window. + assert!(!race_decided(&plan, &[0], Some(&local_v4), + Some(Duration::from_millis(1_600)), Some(Duration::from_millis(110)), + Duration::from_millis(1_500))); + assert!(race_decided(&plan, &[0], Some(&local_v4), + Some(Duration::from_millis(1_740)), Some(Duration::from_millis(250)), + Duration::from_millis(1_500))); +} + +#[test] +fn review_ipv6_candidate_is_not_promoted_at_or_after_its_250ms_deadline() { + let plan = dual_stack_race_plan(); + let base = Instant::now(); + let mut v4 = review_winner(&plan, 1); + let mut v6 = review_winner(&plan, 0); + v4.answered_at = base; + v6.answered_at = base + Duration::from_millis(249); + assert!(candidate_can_replace(&v6, &v4, base, Duration::from_millis(1_500))); + v6.answered_at = base + Duration::from_millis(250); + assert!(!candidate_can_replace(&v6, &v4, base, Duration::from_millis(1_500))); } /// Pending means a worker really exists. Refusing one launch cannot leave the coordinator @@ -1957,31 +2178,31 @@ fn only_an_address_that_answered_is_ever_chosen_and_stored() { match probe_server(&plan, &|o| d.dial(o)) { Reach::At(c, o) => { assert_eq!( - c.address, "192.168.0.10", - "IPv4 leads the plaintext fallbacks" + c.address, "2001:db8::1", + "IPv6 leads the plaintext fallbacks" ); assert_eq!( o.base(), - "http://192.168.0.10:32400", + "http://[2001:db8::1]:32400", "…and the origin recorded is what was dialled" ); } - _ => panic!("the LAN IPv4 answers: {:?}", d.seen()), + _ => panic!("the LAN IPv6 answers: {:?}", d.seen()), } assert_eq!( d.seen(), vec![ - "https://192-168-0-10.h.plex.direct:32400", "https://2001-db8--1.h.plex.direct:32400", - "192.168.0.10:32400", + "https://192-168-0-10.h.plex.direct:32400", + "[2001:db8::1]:32400", ], - "TLS is tried first and costs two probes here; the twin is the fallback that answers" + "TLS is tried first, then the IPv6 plaintext fallback answers" ); - // …and the v6 addresses are never reached, because a candidate that answers ends the walk + // The first working IPv6 candidate answers; later fallback addresses need no probe. assert!( !d.seen() .iter() - .any(|s| s.contains("fd00") || s.contains("2001:db8")), + .any(|s| s.contains("fd00") || s.contains("192.168.0.10")), "{:?}", d.seen() ); @@ -2072,8 +2293,9 @@ fn a_sign_in_to_a_two_server_account_settles_on_one_address_each_ours_first() { assert_eq!( d.seen(), vec![ - "https://192-168-0-10.h.plex.direct:32400", "https://2001-db8--1.h.plex.direct:32400", + "https://192-168-0-10.h.plex.direct:32400", + "[2001:db8::1]:32400", "192.168.0.10:32400", "https://172-20-4-7.h.plex.direct:32400", "https://media.example.internal:31234", From 9f6ac66953746bc6b7a05cbed75a85096e2c4133 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sun, 15 Nov 2026 01:57:36 +0300 Subject: [PATCH 7/8] URI on webOS 25: admit firmware major 10 to the exported-window table (#1377) EXPORTED_STRATA listed firmware major 5 only. It now lists major 10 too. On a set reporting major 10 (an LG C2 on webOS 25, firmware 33.31.68, is the one set that has run), a viewer on the Experimental engine now plays a qualifying original through the TV's own pipeline in the exported window. A qualifying original is a direct play with one audio track and no subtitle stream, on quality Original or on Auto over a local link. The boot line reads `major=10 evidence=table`. Every other webOS 5+ major (6 to 9, 11 and later) still plays Standard, and major 9 is not bracketed, because major 8 has no entry. The evidence is the docs/uri-mode.md admission rule, met on one tester's set (#1064): - (a) The 10.3 image reads option.windowId at load. - (b) The product run of 2026-11-14 had the override armed (`evidence=override`). Its 2.39:1 original was placed from src=3840x1608, "full width with black bars". There was sound, and two seeks were logged landing on seekDone. Pause and resume rest on the tester's words. Two earlier product runs on the same set support it. - (c) The drive probe, with amendment (1). A replay test, uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume, has the current grader answer pause=froze from the run's recorded numbers. Host tests pin majors 5 and 10 to Exported on the exported-window display, every other major 0..=30 to Unbound, and both admitted majors to nothing on ACB and on no display. A boot-line test pins `major=10 evidence=table`. The merge of main kept #1396's binding signature and its boot line. Not checked on major 10: subtitles, HLS, track switching and end of stream through the product, other 10.x minors, and a build carrying this entry. Part of the webOS 5+ URI work tracked in #1064; does not close it. Co-Authored-By: Claude Opus 5.5 --- docs/playback-settings.md | 4 +-- docs/uri-mode.md | 30 +++++++++++-------- rust-modules/play-uri/src/backend.rs | 20 +++++++++++-- rust-modules/play-uri/src/uriprobe.rs | 26 ++++++++++++++++ rust-modules/src/app/adapters/backends.rs | 36 +++++++++++++---------- 5 files changed, 84 insertions(+), 32 deletions(-) diff --git a/docs/playback-settings.md b/docs/playback-settings.md index 3447afbae2..ed29c43525 100644 --- a/docs/playback-settings.md +++ b/docs/playback-settings.md @@ -70,8 +70,8 @@ that carries a subtitle stream (whether or not a subtitle is switched on) or a b subtitle, a trailer preview, a non-1x speed, a relay link and Auto quality away from the LAN also still play in Standard. On webOS 5+, originals with multiple audio tracks also stay in Standard because the TV service can order tracks differently from Plex. Which sets reach the TV's pipeline at all -is decided per firmware major: webOS 4.x through ACB, and webOS 5.x through the TV's exported window (its -PRODUCT run is the 5.6.2 row of `docs/uri-mode.md`); a later major (6 and up) plays Standard until `EXPORTED_STRATA` admits it under the rule in `docs/uri-mode.md`. An Experimental failure takes at most one handoff per play: the video restarts in Standard +is decided per firmware major: webOS 4.x through ACB, and webOS 5.x and firmware major 10 (every 10.x) through the TV's exported window (their +PRODUCT runs are the 5.6.2 and the 9.2.4 / 10.3.1 rows of `docs/uri-mode.md`); every other major (6 to 9, 11 and up) plays Standard until `EXPORTED_STRATA` admits it under the rule in `docs/uri-mode.md`. An Experimental failure takes at most one handoff per play: the video restarts in Standard (`route::backend_policy`). On webOS 4.x the Experimental engine binds the screen through ACB and waits for each release's completion callback before the next title; when that never arrives the plane stays quarantined until the app restarts, the next title's Experimental bind is refused and that title takes the diff --git a/docs/uri-mode.md b/docs/uri-mode.md index a389f963af..e9bbfa5a26 100644 --- a/docs/uri-mode.md +++ b/docs/uri-mode.md @@ -25,7 +25,7 @@ displayed its URI fixture on the 4.5 dev set, as recorded below. | webOS 4.10.0 / 4.4.2 | unpacked images + this project's 4.5 set | No `AVProxy` or `SDL_webOS*ExportedWindow`; the window option is ignored. Earlier probe loads decoded without a visible picture, and earlier ACB attempts returned `ERROR_06`. The controlled 2026-11-02 ACB pair later displayed moving SDR720p24 H.264/AAC URI video on the 4.5 dev set, both with and without `connectDass`; this does not establish other 4.x sets or formats. | | webOS 5.4.2 | LG 65UN80006LA (issue #1064) | `windowId` in `load` → picture, moving, with sound. `late` (`setWindowId` after `loadCompleted`) and `none` → `error code=104 "fail to create AVProxy"`. Both a `plex.direct` HTTPS direct play and an HLS transcode played. Where the picture sat and its aspect were not reported (the tester's report names picture and sound only), and the registration ladder ended on the named client (`shape=named`). | | webOS 5.6.2 (build 5.6.2-21) | LG OLED CX (issue #1064: [the first run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6758688148), [the single-audio product run and drive probe](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6844822550)) | **Admitted: major 5 is in `EXPORTED_STRATA`.** (a) The 5.5.0 and 5.6.2 images read `option.windowId` at load (decompile, below). (b) The PRODUCT run, on the CI build of run 43017142892 (main at `e8442d5e`, which carries #1314) installed over the existing debug app, Playback engine Experimental, quality Original, `plxnative-uriexported` armed at the jail's persistent path: a 1080p HEVC (1920x1040) MKV with ONE AAC 2.0 track and no subtitle streams, direct play. Every boot: `uri: product binding=Unbound mode=2 handle=false ptype=10 initialized=false major=5 evidence=none`, then `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override`. Play: `ls2uri: register shape=named ok after 6ms`, `uri: exported window placed src=1920x1040 rv=1`, `uri: audio index=0 is already selected (audioInfo); no selectTrack sent`, `uri: service playing`. The picture moved, full width with thin bars for 1.85:1, not stretched. Back: `ls2uri: released`. (c) The drive probe on the same build printed `load=ok playing=yes ... currentTime_advanced=yes videoInfo=seen audioInfo=seen window=exported ... shape=named end=script audio_at_load=0 clock_period_ms=200 pause=froze pause_delta=- resume=sent seek=hit seek_delta=-3898 text=sent audio=sent subs=ok eos=no pause_basis=paused_event_and_resume pause_samples=0 ... paused_event=yes seekdone=yes resume_from=2568`. `pause=froze` is printed by the current grader (#1314), so no amendment is used for it. The tester gives no paused position for the probe (no `drive pause pos=` line), so `resume_from=2568` is not compared here against rule (c)'s paused position minus 1000 ms; `pause_basis=paused_event_and_resume` is the grader's own check of the resume against the position it recorded. The audio token is `audio=sent` with `audio_at_load=0`, the rule's `audio_at_load=` alternative. Product controls: pause logged `ev=paused` and two service screenshots 2 s apart were identical (mean pixel difference 0.007 of 255); Play resumed at 39383 ms, where it paused (`timeline paused t=39s`); seek: `uri: control Seek(ContentTime(65606)) landed on the service's seekDone (its reply acknowledged first: yes)`, first `currentTime` after it 62692; a listener in the room confirmed sound in a second play: throughout, silent during a 5 s pause, back on resume and after a seek. Extra datapoint: a 4K HEVC Dolby Vision profile 8.6 MKV (3840x1604, one AC3 5.1 track) showed the TV's Dolby Vision badge, letterboxed for 2.39:1, with the same pause, seek and sound. The multi-audio film now starts in Standard and logs `uri: original declined (several audio tracks on the exported window)`; `exported_uri_leaves_multi_audio_originals_on_standard` (`rust-modules/media/src/route/uri_preflight.rs`) pins that route. History: the first run (a debug build of `main` at 5011614a, before #1314) printed `pause=unconfirmed` from its drive probe (no `currentTime` while paused, so no samples to grade), and its product run of that four-track file (AAC 5.1, AAC 5.1, Vorbis 5.1, Vorbis 5.1) failed the audio-track switch: the service lists the tracks as AAC, Vorbis, AAC, Vorbis, so `uri: selectTrack sent kind=audio index=1 stream=364534` selected Vorbis, the bus answered `{"error":{"errorCode":200,"errorText":"Audio Codec Not Supported"}}` and the play fell back to Standard. That is rule (c)'s amendment (2) case: such a file now starts in Standard, and the admission covers only the route as it now stands (audio switching on the URI path is avoided, not admitted). Not checked: subtitles on the URI path, HLS. | -| webOS 9.2.4 / 10.3.1 (33.31.68) | unpacked images; LG C2 on webOS 25 (10.x, 33.31.68; issue #1064) | The images show the same mechanism as 5.5 and later (below). The C2 ran the drive probe on an older build (the earlier grader), not the product backend, so **no major 10 entry**: millisecond `currentTime` clock with about a 1 s period; a `paused` event with no positions while paused; resume from 2729 after a pause at 2410; seek target 24937 landed on `seekDone` at 24195; audio `trackSelected` index 1 ok; subtitles rendered by the firmware. | +| webOS 9.2.4 / 10.3.1 (33.31.68) | unpacked images; LG C2 on webOS 25 (10.x, 33.31.68; issue #1064: [the set](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6507267775), [the drive probe](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6749905096), [the first product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6806253745), [the Dolby Vision product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6834626267), [a DTS file](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6834728153), [the wider-than-16:9 product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6884620557)) | **Admitted: major 10 is in `EXPORTED_STRATA`. Major 9 is not:** the 9.2.4 image shows the same mechanism as 5.5 and later (below), but no run exists on a 9.x set and major 8 is not admitted either, so major 9 is not bracketed and plays Standard. For major 10: (a) The 10.3 image (33.31.68) reads `option.windowId` at load (decompile, below). (b) The PRODUCT run of 2026-11-14 ([the tester's comment](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6884620557)), on a debug build the tester does not name. It answers the maintainer's request of 2026-11-13 for the CI build of run 43421880266 (main at `2d565dc9f`, #1331's merge) with the override armed, Playback engine Experimental, quality Original and an original wider than 16:9 with one audio track and no subtitles. It opens "Everything was successful again: aspect ratio correct, picture is full width with black bars, audio is playing, pause, resume, seek working." and pastes the event log's `grep -E 'uri(probe)?: |ls2uri: '`: boot `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override` (the paste has no `uri: product binding=Unbound ... major=10 evidence=none` line before it, which the earlier Dolby Vision paste had, so no line of this paste names the firmware major: the run is attributed to this row by the tester's own answer in the thread, on their set); `uri: original declined (a trailer preview)`; `ls2uri: register shape=named ok after 2ms`; `uri: exported window placed src=1920x1080 rv=1`, then `uri: exported window placed src=3840x1608 rv=1` (3840x1608 is 2.39:1); `uri: audio index=0 is already selected (audioInfo); no selectTrack sent`; `uri: service playing`; `uri: control Seek(ContentTime(54901)) landed on the service's seekDone (its reply acknowledged first: yes)` and `uri: control Seek(ContentTime(130201)) landed on the service's seekDone (its reply acknowledged first: yes)`; `ls2uri: released`. This run meets rule (b): the override boot line; a picture placed from a 2.39:1 source, "full width with black bars" in the tester's words; sound ("audio is playing", with the audio and `service playing` lines logged); and controls, the two seeks logged landing on `seekDone`. Pause and resume rest on the tester's words alone: the paste has no pause line, and the backend logs none for the service's `paused` event. Two earlier product runs on the same set support it and are not relied on for the rule. On 2026-11-11 the first, answering the maintainer's request for the debug build of main at `68527cb2` (which carries #1314), pasted no lines and reported "I can confirm that everything worked: picture size/aspect, spinner clears, remaining time counts down, pause/ resume and seek also working." On 2026-11-12 the second, a 16:9 Dolby Vision MKV with one audio track and no subtitles, was "also successful" ("Audio plays, pause, resume and seek works"); its paste has the override line, `uri: exported window placed src=3840x2160 rv=1` and seeks at 31724 and 87765, after the boot line from before this entry, `uri: product binding=Unbound mode=2 handle=false ptype=10 initialized=false major=10 evidence=none`. (c) The drive probe, on an older build (the earlier grader), printed `load=ok playing=yes ... currentTime_advanced=yes ... window=exported ... shape=named end=script pause=unconfirmed ... resume=sent seek=hit seek_delta=-742 ... audio=ok ... pause_basis=reported_positions pause_samples=0 ... paused_event=yes`. Its lines: `drive pause pos=2410`, `ev=paused` at t=4701, no `currentTime` until after `drive resume pos=2410`, then `ev=currentTime value=2729`; `drive seek target=24937`, `ev=seekDone`, the first `currentTime` after it 24195; `drive selectTrack type=audio index=1`, `ev=trackSelected type=audio index=1` (its file had at least two audio tracks; the product runs' had one); `currentTime` in milliseconds about 1 s apart; the tester's answers: pause froze the picture and resume carried on from there ("Yes"), the seek "worked", "Subtitles worked, audio switch also worked" (the subtitles are the firmware's, not our overlay). The tester also reported on that probe run that the video "shows ‘connecting…’ and the buffering icon all the time" and the remaining time "only shows -0:00"; the first product run (2026-11-11) reports the spinner clearing and the remaining time counting down. **Amendment (1) is used.** The run printed `pause=unconfirmed`: with `pause_samples=0` the grader had no position to compare, because the service sends none while paused. The current grader's verdict on the run's recorded numbers stands in for the printed `pause=froze`: `uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume` (`rust-modules/play-uri/src/uriprobe.rs`) replays them (a pause at 2410, a `paused` event inside the dwell, no samples, 2729 as the first position after the resume) and the current grader answers `pause=froze` on `pause_basis=paused_event_and_resume`; the same test shows that a resume more than 1000 ms from the paused position (3411, 1409) confirms nothing. That verdict is the test's, not a printed one. This older summary prints no `resume_from=` token, so rule (c)'s resume comparison is read from the log: first position after the resume 2729, 319 ms past the paused 2410, inside the bound. Amendment (2) is not used: no control failed. Extra datapoint, not part of the rule: a file with a DTS audio track also played as a direct play on this C2 (the tester says the set uses `dts_restore_plus`), and Diagnostics read `Audio dca -> unknown`: the row's third stage is what we declared to Starfish, and it reads `unknown` for a play on the URI backend (`feed_attempt` in `rust-modules/src/app/diagnostics.rs` on main today; the report names no build), which is expected on a URI play. Not checked: subtitles on the URI path through the product, HLS, audio-track switching through the product (a multi-audio original goes to Standard), end of stream, other 10.x minors, and a build with this table entry on a 10.x set. The 2026-11-14 run adds none of these: one audio track, no subtitles, no `evidence=table` line in its paste, and no end-of-stream line (the backend logs none for it, so a paste could not show one). | | webOS 26: set on 11.1.0-3701, mechanism read on the 11.2.0 image | LG G4 (issue #1064) | `windowId` in `load` → 4K picture, Dolby Vision and Atmos through the TV's pipeline, our overlays composited over it, correct size and place. The set runs 11.1.0-3701; 11.2.0 is the unpacked image the mechanism above was read from, not the set. | | webOS 3.x | unpacked images | no `libSDL2` at all; out of scope. | @@ -145,7 +145,7 @@ paused; `play` after `pause` resumes from the paused position rather than restar on 5.6.2; 2410, then 2729 on 10.x); and a `seek` lands on the service's `seekDone`. Still open on 5+: subtitles on the URI path (the 10.x set rendered them in the firmware, a probe report, not a product run), native track identity and ordering (the 5.6.2 service lists audio tracks in a different order -from the container), HLS, majors other than 5, and what the seek bar can be fed — the service reports +from the container), HLS, majors other than 5 and 10, and what the seek bar can be fed — the service reports `currentTime` but no duration, so a duration has to come from `sourceInfo` or from Plex. The production backend's first Play waits behind the startup selection of the viewer's audio track @@ -179,7 +179,7 @@ On the exported window (webOS 5+) the production backend places the window as th it creates it at the start and places it with the 1920x1080 canvas as the source, then, when the service's `videoInfo` reports the coded size, again with that size as the source; the destination is always the whole canvas (`uri: exported window placed src=WxH rv=N`). Production selects that binding -only for a firmware major in the evidence table described below, which admits major 5 today. A debug build +only for a firmware major in the evidence table described below, which admits majors 5 and 10 today. A debug build composes it for one boot when the file `plxnative-uriexported` exists in the install's runtime root (content ignored). Unlike `plxnative-uriprobe`, which is read at each Play, this trigger is read once, at boot, so it can be armed only by writing the file BEFORE the app launches: on @@ -187,19 +187,22 @@ plain `/tmp`, or at the jail's persistent path (routes 1 and 3 under "Arming the run below). Route 2, through `/proc//root` while the app runs, cannot arm it: that write comes after the boot read and the override stays off. The line that confirms it is `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override`, which follows the -production `uri: product binding=Unbound ..` line on a major the table has not admitted (on a 5.x set the -production line is already `binding=Exported { canvas: (1920, 1080) } ... major=5 evidence=table`, and -the override line, when armed, still follows it). A log without it proves nothing about the product +production `uri: product binding=Unbound ..` line on a major the table has not admitted (on a 5.x or +10.x set the production line is already `binding=Exported { canvas: (1920, 1080) } ... major=5 +evidence=table` or `... major=10 evidence=table`, and the override line, when armed, still follows it). A log without it proves nothing about the product backend of a major the table has not admitted. The viewer still has to opt in to the URI engine, and the override does nothing on a display whose mode is not the exported window. Whether the service sends `videoInfo` before the first frame is not determined, so the first frames may be placed from the canvas source rectangle. Production selects the exported window per firmware major, from the table `EXPORTED_STRATA` -(`rust-modules/play-uri/src/backend.rs`), which admits major 5 today (the webOS 5.6.2 Results row -above): a 5.x set's boot line reads `major=5 evidence=table`. Every other webOS 5+ major still plays -Standard (its boot line reads `major= evidence=none`; a 4.x set reads `none` too and keeps its ACB -path). An entry is a webOS firmware major as the set reports it, so major 5 covers every 5.x release: -the product run was on 5.6.2, the earlier probe-only run on 5.4.2, and no other 5.x minor has a run. +(`rust-modules/play-uri/src/backend.rs`), which admits majors 5 and 10 today (the webOS 5.6.2 and the +webOS 9.2.4 / 10.3.1 Results rows above): a 5.x set's boot line reads `major=5 evidence=table` and a +10.x set's `major=10 evidence=table`. Every other webOS 5+ major still plays Standard (its boot line +reads `major= evidence=none`; a 4.x set reads `none` too and keeps its ACB path). An entry is a +webOS firmware major as the set reports it, so major 5 covers every 5.x release: the product run was +on 5.6.2, the earlier probe-only run on 5.4.2, and no other 5.x minor has a run. Major 10 covers every +10.x release the same way: the product runs (the rule's is the 2026-11-14 one, on a 2.39:1 original) and +the probe were on one LG C2 on 10.3.1 (33.31.68), and no other 10.x minor has a run. Major 9 has an image and no run, and is not admitted. An entry needs three things in this file's Results table, each with the tester log's decisive lines. A release is never admitted on the probe's evidence alone: a run of the product backend is required. @@ -214,7 +217,8 @@ required. amendments, each named in the row that uses it. (1) For a run made before the current grader, the current grader's verdict on that run's recorded numbers, replayed in a unit test, stands in for the printed `pause=froze`: the row names the test and says the run itself printed `pause=unconfirmed`. The - 5.6.2 row does not use it: its probe run printed `pause=froze`. (2) A control the product run showed + 5.6.2 row does not use it (its probe run printed `pause=froze`); the 10.x row does, with + `uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume`. (2) A control the product run showed failing is acceptable only when the route now sends that case to Standard (the 5.6.2 audio-track switch, routed by #1314 and since observed on the set as `uri: original declined (several audio tracks on the exported window)`); the row names it, and the admission covers only the route as it now stands. @@ -411,7 +415,7 @@ The reasons are local diagnostics: they route nothing, and nothing about them is - Whether webOS 5+ can be driven fully as a player: pause, resume and seek are settled on 5.6.2 and on the 10.x probe (Results). Open: where a duration comes from, native track identity and ordering, - end-of-stream (no run has reached it), and HLS and majors other than 5 on the product path. + end-of-stream (no run has reached it), and HLS and majors other than 5 and 10 on the product path. - Whether a descriptor (PGS) or text track renders into the exported window, or whether subtitles must stay on our own overlay path. - Which formats the TV's pipeline plays that our buffer-feed path refuses, on stock firmware. diff --git a/rust-modules/play-uri/src/backend.rs b/rust-modules/play-uri/src/backend.rs index f8669c3ff3..969ff84ec3 100644 --- a/rust-modules/play-uri/src/backend.rs +++ b/rust-modules/play-uri/src/backend.rs @@ -62,8 +62,9 @@ pub fn product_binding(display_mode: i32, firmware_major: u32, strata: &[u32]) - /// The firmware majors on which the product backend selects the exported window /// ([`product_binding`]): the only way production ever composes [`Binding::Exported`]. An entry is /// a webOS firmware major as `device::info().major` reports it (5 for 5.x, 11 for 11.x). The table -/// admits major 5 and no other, so every other webOS 5+ major plays Standard until it earns an entry -/// (a webOS 4.x set takes the ACB plane, which this table does not touch). +/// admits majors 5 and 10 and no other, so every other webOS 5+ major (9 and 11 included) plays +/// Standard until it earns an entry (a webOS 4.x set takes the ACB plane, which this table does not +/// touch). /// /// A major is admitted only with all three parts of the evidence in `docs/uri-mode.md`'s Results /// table, each recorded there with the tester log's decisive lines. A release is never admitted on @@ -101,6 +102,21 @@ pub const EXPORTED_STRATA: &[u32] = &[ // four-audio-track run's audio-track switch failed; such a file now starts in Standard (#1314, // amendment (2); `exported_uri_leaves_multi_audio_originals_on_standard` pins the route). 5, + // webOS 25 (firmware 10.3.1, 33.31.68), LG C2: `docs/uri-mode.md` Results row "webOS 9.2.4 / 10.3.1 + // (33.31.68)", #1064. (a) the 10.3 image reads `option.windowId` at load; (b) the product run of + // 2026-11-14 (a debug build the tester does not name, answering the request for the CI build of + // run 43421880266, main at 2d565dc9f; one audio track, no subtitles) logged `evidence=override` + // and `uri: exported window placed src=3840x1608 rv=1` (2.39:1), both seeks landing on seekDone, + // and reported "aspect ratio correct, picture is full width with black bars, audio is playing, + // pause, resume, seek working" (pause and resume rest on those words: the paste has no pause + // line); two earlier product runs, one on main at 68527cb2 that pasted no lines and one on a 16:9 + // Dolby Vision MKV, support it and are not relied on; (c) a drive probe on an older build + // printed seek=hit and audio=ok but pause=unconfirmed (paused_event=yes, no position while + // paused), so amendment (1) is used: the replay test + // `uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume` grades its + // recorded numbers (pause at 2410, first position after resume 2729) pause=froze on + // pause_basis=paused_event_and_resume. Amendment (2) is not used; major 9 has no entry. + 10, ]; /// The canvas the exported window is placed over: the destination of EVERY placement, the whole diff --git a/rust-modules/play-uri/src/uriprobe.rs b/rust-modules/play-uri/src/uriprobe.rs index 9e6fb764a7..47f93a1e1b 100644 --- a/rust-modules/play-uri/src/uriprobe.rs +++ b/rust-modules/play-uri/src/uriprobe.rs @@ -2511,6 +2511,32 @@ mod tests { } } + /// The webOS 25 C2 drive run (LG C2, firmware 10.x, #1064), replayed: its probe printed + /// `pause=unconfirmed` on the grader of the day. The run's recorded numbers are a pause sent at + /// position 2410, a `paused` event inside the dwell, no position report while paused, and 2729 + /// as the first position after `resume` (134 ms after it was sent). This grader's verdict on + /// them is `pause=froze` on `pause_basis=paused_event_and_resume`; the major 10 row of + /// `docs/uri-mode.md` stands on this test (rule (c), amendment (1)). The bound is 1000 ms from + /// the paused position: 3410 and 1410 are the last resumes that confirm it, 3411 and 1409 the + /// first that do not. + #[test] + fn uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume() { + for (resumed, expected) in [(2729, "froze"), (3410, "froze"), (1410, "froze"), (3411, "unconfirmed"), (1409, "unconfirmed")] { + let mut rig = drive_pause_rig_at(2410); + rig.msg("subscribe", r#"{"mediaId":"_m1","paused":true}"#); + rig.tick(DRIVE_STEP_MS); + drive_report_after(&mut rig, 134, resumed); + let summary = summary_after_back(&mut rig); + assert!(summary.contains(&format!("pause={expected} ")), "resume at {resumed}: {summary}"); + if resumed == 2729 { + // The run itself: no samples, so the event and the resume position are the whole basis. + for token in ["pause_basis=paused_event_and_resume ", "pause_samples=0 ", "paused_event=yes ", "resume_from=2729 "] { + assert!(summary.contains(token), "{token}: {summary}"); + } + } + } + } + #[test] fn uri_drive_review_early_reports_then_silence_do_not_confirm_pause() { let mut rig=drive_pause_rig(); diff --git a/rust-modules/src/app/adapters/backends.rs b/rust-modules/src/app/adapters/backends.rs index 59ca44ec0d..5257198031 100644 --- a/rust-modules/src/app/adapters/backends.rs +++ b/rust-modules/src/app/adapters/backends.rs @@ -639,26 +639,29 @@ mod tests { } /// The exported window follows the firmware major through the composition: production's table - /// binds major 5 (webOS 5.6.2, `docs/uri-mode.md`) and no other (a webOS 5+ release without an - /// entry plays Standard), and a table that lists a major binds it, on a display in the - /// exported-window mode only. The URI engine's own handle is not asked for: a 5+ set has none. + /// binds majors 5 (webOS 5.6.2) and 10 (the LG C2 on webOS 25), each with its `docs/uri-mode.md` + /// Results row, and no other (a webOS 5+ release without an entry plays Standard), and a table + /// that lists a major binds it, on a display in the exported-window mode only. The URI engine's + /// own handle is not asked for: a 5+ set has none. #[cfg(feature = "play-uri")] #[test] fn the_exported_window_is_composed_only_for_a_major_the_table_lists() { use plx_play_uri::backend::{EXPORTED_CANVAS, EXPORTED_STRATA}; let exported = UriBinding::Exported { canvas: EXPORTED_CANVAS }; let sink = BootSink::answering(VP_EXPORTED, vec![7, 8]); - // Production's table admits major 5 and nothing else: every other release, known or not, - // composes Unbound on this display. An entry lands with its Results row and the change to - // this test. + // Production's table admits majors 5 and 10 and nothing else: every other release, known or + // not (9 and 11 included), composes Unbound on this display. An entry lands with its Results + // row and the change to this test. for major in 0..=30 { - let want = if major == 5 { exported } else { UriBinding::Unbound }; + let want = if matches!(major, 5 | 10) { exported } else { UriBinding::Unbound }; assert_eq!(uri_product_binding(&sink, major, EXPORTED_STRATA), want, "major {major}"); } - // The same display on a set whose mode is not the exported window: major 5 binds nothing there. - for mode in [VP_NONE, plx_platform::tv::sink::VP_ACB] { - let sink = BootSink::answering(mode, vec![7, 8]); - assert_ne!(uri_product_binding(&sink, 5, EXPORTED_STRATA), exported, "mode {mode}"); + // The same display on a set whose mode is not the exported window: an admitted major binds nothing there. + for major in [5, 10] { + for mode in [VP_NONE, plx_platform::tv::sink::VP_ACB] { + let sink = BootSink::answering(mode, vec![7, 8]); + assert_ne!(uri_product_binding(&sink, major, EXPORTED_STRATA), exported, "major {major} mode {mode}"); + } } assert_eq!(uri_product_binding(&sink, 11, &[11]), exported); assert_eq!(uri_product_binding(&sink, 10, &[11]), UriBinding::Unbound, "an unlisted major"); @@ -686,10 +689,13 @@ mod tests { let unlisted = line(VP_EXPORTED, 11, &[]); assert!(unlisted.contains("uri: product binding=Unbound mode=2 "), "{unlisted}"); assert!(unlisted.contains(" major=11 evidence=none\n"), "{unlisted}"); - // Production's own table: the webOS 5.6.2 television's boot line reads `major=5 evidence=table`. - let shipped = line(VP_EXPORTED, 5, plx_play_uri::backend::EXPORTED_STRATA); - assert!(shipped.contains("uri: product binding=Exported { canvas: (1920, 1080) } mode=2 "), "{shipped}"); - assert!(shipped.contains(" major=5 evidence=table\n"), "{shipped}"); + // Production's own table: a webOS 5.6.2 television's boot line reads `major=5 evidence=table` + // and the LG C2 on webOS 25's reads `major=10 evidence=table`. + for major in [5, 10] { + let shipped = line(VP_EXPORTED, major, plx_play_uri::backend::EXPORTED_STRATA); + assert!(shipped.contains("uri: product binding=Exported { canvas: (1920, 1080) } mode=2 "), "{shipped}"); + assert!(shipped.contains(&format!(" major={major} evidence=table\n")), "{shipped}"); + } let acb = line(plx_platform::tv::sink::VP_ACB, 4, &[]); assert!(acb.contains("uri: product binding=Acb mode=1 handle=deferred ptype=0 "), "{acb}"); assert!(acb.contains(" major=4 evidence=none\n"), "{acb}"); From 92e63a8ec3d452ce83f8e75f6c5a40e0bbef70b3 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sun, 15 Nov 2026 02:35:13 +0300 Subject: [PATCH 8/8] URI on webOS 5+: admit firmware majors 6, 7 and 9, bracketed, no tester run (#1397) EXPORTED_STRATA is now &[5, 6, 7, 9, 10]. On a set whose firmware reports major 6, 7 or 9, a viewer on the Experimental engine now plays a qualifying original through the TV's own pipeline in the exported window. A qualifying original is a direct play with one audio track and no subtitle stream, on quality Original or on Auto over a local link. The boot line reads `evidence=table`. No tester has run these majors. They are admitted under the bracketing exception, which now reads "nearest admitted major on each side" (the maintainer's decision of 2026-11-13). A major with image evidence (a) but no tester run is admitted when the nearest majors below and above it were both admitted on tester evidence; a major admitted only as bracketed is skipped when looking for a side. Here those sides are 5 (#1331) and 10 (#1377). The old text required both major - 1 and major + 1. (a) per major: libplayerAPIs.so.1.0.0, StarfishMediaAPIs::parseLoadMessage, reads option.windowId at load: - 6.5.3, HE_DTV_W21O, sha 0b0f6654, at 0x41556; - 7.6.0, HE_DTV_W22P, sha 0969903e, at 0x467c6; - 9.2.4, HE_DTV_W24G, sha cc08e2b8, at 0x4385c. A virtual call follows the store on each image; the AVProxy builder is identified on 11.2 only. Major 8 has no image read, so it has no (a) and plays Standard. docs/uri-mode.md gains Results rows for 6.x and 7.x, rewrites the major-9 part of the 9.2.4 / 10.3.1 row, and corrects the windowId store offset: +0x90 on 6.5.3 and 7.6.0, +0x94 on 9.2.4 and 10.3.1. Host tests (red first against &[5, 10]) pin 5, 6, 7, 9 and 10 to Exported on the exported-window display, every other major 0..=30 (8 included) to Unbound, and the boot lines, including `major=8 evidence=none`. Part of the webOS 5+ URI work tracked in #1064; does not close it. Co-Authored-By: Claude Opus 5.5 --- docs/playback-settings.md | 4 +- docs/uri-mode.md | 46 +++++++++++++++-------- rust-modules/play-uri/src/backend.rs | 41 +++++++++++++++----- rust-modules/src/app/adapters/backends.rs | 31 +++++++++------ 4 files changed, 82 insertions(+), 40 deletions(-) diff --git a/docs/playback-settings.md b/docs/playback-settings.md index ed29c43525..114aef3f2c 100644 --- a/docs/playback-settings.md +++ b/docs/playback-settings.md @@ -70,8 +70,8 @@ that carries a subtitle stream (whether or not a subtitle is switched on) or a b subtitle, a trailer preview, a non-1x speed, a relay link and Auto quality away from the LAN also still play in Standard. On webOS 5+, originals with multiple audio tracks also stay in Standard because the TV service can order tracks differently from Plex. Which sets reach the TV's pipeline at all -is decided per firmware major: webOS 4.x through ACB, and webOS 5.x and firmware major 10 (every 10.x) through the TV's exported window (their -PRODUCT runs are the 5.6.2 and the 9.2.4 / 10.3.1 rows of `docs/uri-mode.md`); every other major (6 to 9, 11 and up) plays Standard until `EXPORTED_STRATA` admits it under the rule in `docs/uri-mode.md`. An Experimental failure takes at most one handoff per play: the video restarts in Standard +is decided per firmware major: webOS 4.x through ACB, and firmware majors 5, 6, 7, 9 and 10 (every release of each) through the TV's exported window (the PRODUCT runs are +the 5.6.2 and the 9.2.4 / 10.3.1 rows of `docs/uri-mode.md`, for majors 5 and 10; majors 6, 7 and 9 are admitted as bracketed by them, with no tester run); every other major (8, 11 and up) plays Standard until `EXPORTED_STRATA` admits it under the rule in `docs/uri-mode.md`. An Experimental failure takes at most one handoff per play: the video restarts in Standard (`route::backend_policy`). On webOS 4.x the Experimental engine binds the screen through ACB and waits for each release's completion callback before the next title; when that never arrives the plane stays quarantined until the app restarts, the next title's Experimental bind is refused and that title takes the diff --git a/docs/uri-mode.md b/docs/uri-mode.md index e9bbfa5a26..497859086b 100644 --- a/docs/uri-mode.md +++ b/docs/uri-mode.md @@ -25,7 +25,9 @@ displayed its URI fixture on the 4.5 dev set, as recorded below. | webOS 4.10.0 / 4.4.2 | unpacked images + this project's 4.5 set | No `AVProxy` or `SDL_webOS*ExportedWindow`; the window option is ignored. Earlier probe loads decoded without a visible picture, and earlier ACB attempts returned `ERROR_06`. The controlled 2026-11-02 ACB pair later displayed moving SDR720p24 H.264/AAC URI video on the 4.5 dev set, both with and without `connectDass`; this does not establish other 4.x sets or formats. | | webOS 5.4.2 | LG 65UN80006LA (issue #1064) | `windowId` in `load` → picture, moving, with sound. `late` (`setWindowId` after `loadCompleted`) and `none` → `error code=104 "fail to create AVProxy"`. Both a `plex.direct` HTTPS direct play and an HLS transcode played. Where the picture sat and its aspect were not reported (the tester's report names picture and sound only), and the registration ladder ended on the named client (`shape=named`). | | webOS 5.6.2 (build 5.6.2-21) | LG OLED CX (issue #1064: [the first run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6758688148), [the single-audio product run and drive probe](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6844822550)) | **Admitted: major 5 is in `EXPORTED_STRATA`.** (a) The 5.5.0 and 5.6.2 images read `option.windowId` at load (decompile, below). (b) The PRODUCT run, on the CI build of run 43017142892 (main at `e8442d5e`, which carries #1314) installed over the existing debug app, Playback engine Experimental, quality Original, `plxnative-uriexported` armed at the jail's persistent path: a 1080p HEVC (1920x1040) MKV with ONE AAC 2.0 track and no subtitle streams, direct play. Every boot: `uri: product binding=Unbound mode=2 handle=false ptype=10 initialized=false major=5 evidence=none`, then `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override`. Play: `ls2uri: register shape=named ok after 6ms`, `uri: exported window placed src=1920x1040 rv=1`, `uri: audio index=0 is already selected (audioInfo); no selectTrack sent`, `uri: service playing`. The picture moved, full width with thin bars for 1.85:1, not stretched. Back: `ls2uri: released`. (c) The drive probe on the same build printed `load=ok playing=yes ... currentTime_advanced=yes videoInfo=seen audioInfo=seen window=exported ... shape=named end=script audio_at_load=0 clock_period_ms=200 pause=froze pause_delta=- resume=sent seek=hit seek_delta=-3898 text=sent audio=sent subs=ok eos=no pause_basis=paused_event_and_resume pause_samples=0 ... paused_event=yes seekdone=yes resume_from=2568`. `pause=froze` is printed by the current grader (#1314), so no amendment is used for it. The tester gives no paused position for the probe (no `drive pause pos=` line), so `resume_from=2568` is not compared here against rule (c)'s paused position minus 1000 ms; `pause_basis=paused_event_and_resume` is the grader's own check of the resume against the position it recorded. The audio token is `audio=sent` with `audio_at_load=0`, the rule's `audio_at_load=` alternative. Product controls: pause logged `ev=paused` and two service screenshots 2 s apart were identical (mean pixel difference 0.007 of 255); Play resumed at 39383 ms, where it paused (`timeline paused t=39s`); seek: `uri: control Seek(ContentTime(65606)) landed on the service's seekDone (its reply acknowledged first: yes)`, first `currentTime` after it 62692; a listener in the room confirmed sound in a second play: throughout, silent during a 5 s pause, back on resume and after a seek. Extra datapoint: a 4K HEVC Dolby Vision profile 8.6 MKV (3840x1604, one AC3 5.1 track) showed the TV's Dolby Vision badge, letterboxed for 2.39:1, with the same pause, seek and sound. The multi-audio film now starts in Standard and logs `uri: original declined (several audio tracks on the exported window)`; `exported_uri_leaves_multi_audio_originals_on_standard` (`rust-modules/media/src/route/uri_preflight.rs`) pins that route. History: the first run (a debug build of `main` at 5011614a, before #1314) printed `pause=unconfirmed` from its drive probe (no `currentTime` while paused, so no samples to grade), and its product run of that four-track file (AAC 5.1, AAC 5.1, Vorbis 5.1, Vorbis 5.1) failed the audio-track switch: the service lists the tracks as AAC, Vorbis, AAC, Vorbis, so `uri: selectTrack sent kind=audio index=1 stream=364534` selected Vorbis, the bus answered `{"error":{"errorCode":200,"errorText":"Audio Codec Not Supported"}}` and the play fell back to Standard. That is rule (c)'s amendment (2) case: such a file now starts in Standard, and the admission covers only the route as it now stands (audio switching on the URI path is avoided, not admitted). Not checked: subtitles on the URI path, HLS. | -| webOS 9.2.4 / 10.3.1 (33.31.68) | unpacked images; LG C2 on webOS 25 (10.x, 33.31.68; issue #1064: [the set](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6507267775), [the drive probe](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6749905096), [the first product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6806253745), [the Dolby Vision product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6834626267), [a DTS file](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6834728153), [the wider-than-16:9 product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6884620557)) | **Admitted: major 10 is in `EXPORTED_STRATA`. Major 9 is not:** the 9.2.4 image shows the same mechanism as 5.5 and later (below), but no run exists on a 9.x set and major 8 is not admitted either, so major 9 is not bracketed and plays Standard. For major 10: (a) The 10.3 image (33.31.68) reads `option.windowId` at load (decompile, below). (b) The PRODUCT run of 2026-11-14 ([the tester's comment](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6884620557)), on a debug build the tester does not name. It answers the maintainer's request of 2026-11-13 for the CI build of run 43421880266 (main at `2d565dc9f`, #1331's merge) with the override armed, Playback engine Experimental, quality Original and an original wider than 16:9 with one audio track and no subtitles. It opens "Everything was successful again: aspect ratio correct, picture is full width with black bars, audio is playing, pause, resume, seek working." and pastes the event log's `grep -E 'uri(probe)?: |ls2uri: '`: boot `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override` (the paste has no `uri: product binding=Unbound ... major=10 evidence=none` line before it, which the earlier Dolby Vision paste had, so no line of this paste names the firmware major: the run is attributed to this row by the tester's own answer in the thread, on their set); `uri: original declined (a trailer preview)`; `ls2uri: register shape=named ok after 2ms`; `uri: exported window placed src=1920x1080 rv=1`, then `uri: exported window placed src=3840x1608 rv=1` (3840x1608 is 2.39:1); `uri: audio index=0 is already selected (audioInfo); no selectTrack sent`; `uri: service playing`; `uri: control Seek(ContentTime(54901)) landed on the service's seekDone (its reply acknowledged first: yes)` and `uri: control Seek(ContentTime(130201)) landed on the service's seekDone (its reply acknowledged first: yes)`; `ls2uri: released`. This run meets rule (b): the override boot line; a picture placed from a 2.39:1 source, "full width with black bars" in the tester's words; sound ("audio is playing", with the audio and `service playing` lines logged); and controls, the two seeks logged landing on `seekDone`. Pause and resume rest on the tester's words alone: the paste has no pause line, and the backend logs none for the service's `paused` event. Two earlier product runs on the same set support it and are not relied on for the rule. On 2026-11-11 the first, answering the maintainer's request for the debug build of main at `68527cb2` (which carries #1314), pasted no lines and reported "I can confirm that everything worked: picture size/aspect, spinner clears, remaining time counts down, pause/ resume and seek also working." On 2026-11-12 the second, a 16:9 Dolby Vision MKV with one audio track and no subtitles, was "also successful" ("Audio plays, pause, resume and seek works"); its paste has the override line, `uri: exported window placed src=3840x2160 rv=1` and seeks at 31724 and 87765, after the boot line from before this entry, `uri: product binding=Unbound mode=2 handle=false ptype=10 initialized=false major=10 evidence=none`. (c) The drive probe, on an older build (the earlier grader), printed `load=ok playing=yes ... currentTime_advanced=yes ... window=exported ... shape=named end=script pause=unconfirmed ... resume=sent seek=hit seek_delta=-742 ... audio=ok ... pause_basis=reported_positions pause_samples=0 ... paused_event=yes`. Its lines: `drive pause pos=2410`, `ev=paused` at t=4701, no `currentTime` until after `drive resume pos=2410`, then `ev=currentTime value=2729`; `drive seek target=24937`, `ev=seekDone`, the first `currentTime` after it 24195; `drive selectTrack type=audio index=1`, `ev=trackSelected type=audio index=1` (its file had at least two audio tracks; the product runs' had one); `currentTime` in milliseconds about 1 s apart; the tester's answers: pause froze the picture and resume carried on from there ("Yes"), the seek "worked", "Subtitles worked, audio switch also worked" (the subtitles are the firmware's, not our overlay). The tester also reported on that probe run that the video "shows ‘connecting…’ and the buffering icon all the time" and the remaining time "only shows -0:00"; the first product run (2026-11-11) reports the spinner clearing and the remaining time counting down. **Amendment (1) is used.** The run printed `pause=unconfirmed`: with `pause_samples=0` the grader had no position to compare, because the service sends none while paused. The current grader's verdict on the run's recorded numbers stands in for the printed `pause=froze`: `uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume` (`rust-modules/play-uri/src/uriprobe.rs`) replays them (a pause at 2410, a `paused` event inside the dwell, no samples, 2729 as the first position after the resume) and the current grader answers `pause=froze` on `pause_basis=paused_event_and_resume`; the same test shows that a resume more than 1000 ms from the paused position (3411, 1409) confirms nothing. That verdict is the test's, not a printed one. This older summary prints no `resume_from=` token, so rule (c)'s resume comparison is read from the log: first position after the resume 2729, 319 ms past the paused 2410, inside the bound. Amendment (2) is not used: no control failed. Extra datapoint, not part of the rule: a file with a DTS audio track also played as a direct play on this C2 (the tester says the set uses `dts_restore_plus`), and Diagnostics read `Audio dca -> unknown`: the row's third stage is what we declared to Starfish, and it reads `unknown` for a play on the URI backend (`feed_attempt` in `rust-modules/src/app/diagnostics.rs` on main today; the report names no build), which is expected on a URI play. Not checked: subtitles on the URI path through the product, HLS, audio-track switching through the product (a multi-audio original goes to Standard), end of stream, other 10.x minors, and a build with this table entry on a 10.x set. The 2026-11-14 run adds none of these: one audio track, no subtitles, no `evidence=table` line in its paste, and no end-of-stream line (the backend logs none for it, so a paste could not show one). | +| webOS 6.5.3 (firmware major 6) | unpacked image `HE_DTV_W21O_AFABATAA` (webOS TV 6.5.3, release 6.5.3-47); no tester run | **Admitted, bracketed, no tester run: major 6 is in `EXPORTED_STRATA`.** (a) The 6.5.3 image reads `option.windowId` at load: `libplayerAPIs.so.1.0.0` (sha256 prefix `0b0f6654`), `StarfishMediaAPIs::parseLoadMessage`. The string `option.windowId` (file offset 0x874bc) is referenced at 0x41556 and is read as an optional string from the load JSON; when the key is present the function stores it at `MediaApiPrivate+0x90` and passes it to a virtual call (vtable slot +0x74 of the object at `MediaApiPrivate+0x5c`; the 11.2 read names the corresponding call (slot +0x78 of `+0x60`) the AVProxy builder, and the slot was not separately identified on this image). The same image's string table has `fail to create AVProxy` (0x88b44), the error 104 text. Addresses are file VAs (`objdump -d`; Ghidra's are +0x10000). Bracketed: majors 5 and 10 are each admitted on a tester's product run (the rows above and below), and they are the nearest major admitted on tester evidence on each side of 6. No tester run exists on a 6.x set, so there is no (b) and no (c): nothing says a picture, sound, pause, seek or an audio switch works there, and no other 6.x release has been read. The boot line reads `major=6 evidence=table` like any admitted major's; it does not say a tester ran it. | +| webOS 7.6.0 (firmware major 7) | unpacked image `HE_DTV_W22P_AFADATAA` (webOS TV 7.6.0, release 7.6.0-1901); no tester run | **Admitted, bracketed, no tester run: major 7 is in `EXPORTED_STRATA`.** (a) The 7.6.0 image reads `option.windowId` at load: `libplayerAPIs.so.1.0.0` (sha256 prefix `0969903e`), `StarfishMediaAPIs::parseLoadMessage`. The string `option.windowId` (file offset 0x971d8) is referenced at 0x467c6 and is read as an optional string; when the key is present the function stores it at `MediaApiPrivate+0x90` and passes it to a virtual call (vtable slot +0x74 of the object at `MediaApiPrivate+0x5c`, not separately identified on this image). The same image's string table has `fail to create AVProxy` (0x98b9c). Addresses are file VAs. Bracketed: majors 5 and 10 are each admitted on a tester's product run, and they are the nearest major admitted on tester evidence on each side of 7. No tester run exists on a 7.x set, so there is no (b) and no (c), and no other 7.x release has been read. The boot line reads `major=7 evidence=table`. | +| webOS 9.2.4 / 10.3.1 (33.31.68) | unpacked images; LG C2 on webOS 25 (10.x, 33.31.68; issue #1064: [the set](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6507267775), [the drive probe](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6749905096), [the first product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6806253745), [the Dolby Vision product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6834626267), [a DTS file](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6834728153), [the wider-than-16:9 product run](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6884620557)) | **Admitted: major 10 is in `EXPORTED_STRATA`, on tester evidence. Major 9 is admitted too, bracketed, no tester run.** Major 9, (a): the 9.2.4 image (`HE_DTV_W24G_AFABATAA`, webOS TV 9.2.4, release 9.2.4-48) reads `option.windowId` at load: `libplayerAPIs.so.1.0.0` (sha256 prefix `cc08e2b8`), `StarfishMediaAPIs::parseLoadMessage`. The string `option.windowId` (file offset 0x9a7c0) is referenced at 0x4385c and is read as an optional string; when the key is present the function stores it at `MediaApiPrivate+0x94` and passes it to a virtual call (vtable slot +0x78 of the object at `MediaApiPrivate+0x60`, not separately identified on this image). The same image's string table has `fail to create AVProxy` (0x99884). Addresses are file VAs. Bracketed: majors 5 and 10 are the nearest major admitted on tester evidence on each side of 9. No run exists on a 9.x set, so there is no (b) and no (c) for major 9, and no other 9.x release has been read. For major 10: (a) The 10.3 image (33.31.68) reads `option.windowId` at load (decompile, below). (b) The PRODUCT run of 2026-11-14 ([the tester's comment](https://github.com/GLinnik21/plx-native/issues/1064#issuecomment-6884620557)), on a debug build the tester does not name. It answers the maintainer's request of 2026-11-13 for the CI build of run 43421880266 (main at `2d565dc9f`, #1331's merge) with the override armed, Playback engine Experimental, quality Original and an original wider than 16:9 with one audio track and no subtitles. It opens "Everything was successful again: aspect ratio correct, picture is full width with black bars, audio is playing, pause, resume, seek working." and pastes the event log's `grep -E 'uri(probe)?: |ls2uri: '`: boot `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override` (the paste has no `uri: product binding=Unbound ... major=10 evidence=none` line before it, which the earlier Dolby Vision paste had, so no line of this paste names the firmware major: the run is attributed to this row by the tester's own answer in the thread, on their set); `uri: original declined (a trailer preview)`; `ls2uri: register shape=named ok after 2ms`; `uri: exported window placed src=1920x1080 rv=1`, then `uri: exported window placed src=3840x1608 rv=1` (3840x1608 is 2.39:1); `uri: audio index=0 is already selected (audioInfo); no selectTrack sent`; `uri: service playing`; `uri: control Seek(ContentTime(54901)) landed on the service's seekDone (its reply acknowledged first: yes)` and `uri: control Seek(ContentTime(130201)) landed on the service's seekDone (its reply acknowledged first: yes)`; `ls2uri: released`. This run meets rule (b): the override boot line; a picture placed from a 2.39:1 source, "full width with black bars" in the tester's words; sound ("audio is playing", with the audio and `service playing` lines logged); and controls, the two seeks logged landing on `seekDone`. Pause and resume rest on the tester's words alone: the paste has no pause line, and the backend logs none for the service's `paused` event. Two earlier product runs on the same set support it and are not relied on for the rule. On 2026-11-11 the first, answering the maintainer's request for the debug build of main at `68527cb2` (which carries #1314), pasted no lines and reported "I can confirm that everything worked: picture size/aspect, spinner clears, remaining time counts down, pause/ resume and seek also working." On 2026-11-12 the second, a 16:9 Dolby Vision MKV with one audio track and no subtitles, was "also successful" ("Audio plays, pause, resume and seek works"); its paste has the override line, `uri: exported window placed src=3840x2160 rv=1` and seeks at 31724 and 87765, after the boot line from before this entry, `uri: product binding=Unbound mode=2 handle=false ptype=10 initialized=false major=10 evidence=none`. (c) The drive probe, on an older build (the earlier grader), printed `load=ok playing=yes ... currentTime_advanced=yes ... window=exported ... shape=named end=script pause=unconfirmed ... resume=sent seek=hit seek_delta=-742 ... audio=ok ... pause_basis=reported_positions pause_samples=0 ... paused_event=yes`. Its lines: `drive pause pos=2410`, `ev=paused` at t=4701, no `currentTime` until after `drive resume pos=2410`, then `ev=currentTime value=2729`; `drive seek target=24937`, `ev=seekDone`, the first `currentTime` after it 24195; `drive selectTrack type=audio index=1`, `ev=trackSelected type=audio index=1` (its file had at least two audio tracks; the product runs' had one); `currentTime` in milliseconds about 1 s apart; the tester's answers: pause froze the picture and resume carried on from there ("Yes"), the seek "worked", "Subtitles worked, audio switch also worked" (the subtitles are the firmware's, not our overlay). The tester also reported on that probe run that the video "shows ‘connecting…’ and the buffering icon all the time" and the remaining time "only shows -0:00"; the first product run (2026-11-11) reports the spinner clearing and the remaining time counting down. **Amendment (1) is used.** The run printed `pause=unconfirmed`: with `pause_samples=0` the grader had no position to compare, because the service sends none while paused. The current grader's verdict on the run's recorded numbers stands in for the printed `pause=froze`: `uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume` (`rust-modules/play-uri/src/uriprobe.rs`) replays them (a pause at 2410, a `paused` event inside the dwell, no samples, 2729 as the first position after the resume) and the current grader answers `pause=froze` on `pause_basis=paused_event_and_resume`; the same test shows that a resume more than 1000 ms from the paused position (3411, 1409) confirms nothing. That verdict is the test's, not a printed one. This older summary prints no `resume_from=` token, so rule (c)'s resume comparison is read from the log: first position after the resume 2729, 319 ms past the paused 2410, inside the bound. Amendment (2) is not used: no control failed. Extra datapoint, not part of the rule: a file with a DTS audio track also played as a direct play on this C2 (the tester says the set uses `dts_restore_plus`), and Diagnostics read `Audio dca -> unknown`: the row's third stage is what we declared to Starfish, and it reads `unknown` for a play on the URI backend (`feed_attempt` in `rust-modules/src/app/diagnostics.rs` on main today; the report names no build), which is expected on a URI play. Not checked: subtitles on the URI path through the product, HLS, audio-track switching through the product (a multi-audio original goes to Standard), end of stream, other 10.x minors, and a build with this table entry on a 10.x set. The 2026-11-14 run adds none of these: one audio track, no subtitles, no `evidence=table` line in its paste, and no end-of-stream line (the backend logs none for it, so a paste could not show one). | | webOS 26: set on 11.1.0-3701, mechanism read on the 11.2.0 image | LG G4 (issue #1064) | `windowId` in `load` → 4K picture, Dolby Vision and Atmos through the TV's pipeline, our overlays composited over it, correct size and place. The set runs 11.1.0-3701; 11.2.0 is the unpacked image the mechanism above was read from, not the set. | | webOS 3.x | unpacked images | no `libSDL2` at all; out of scope. | @@ -38,9 +40,13 @@ Read from `libplayerAPIs`, `libav-proxy`, `libav-connector`, `libav`, `videosink `payload`, `option` and `payload.appId`, requires `uri` then `type`, and hands the payload to the registered pipeline. Its only `windowId` reader is the separate `setWindowId` command — which is why sending it after the load is too late. -- **`libplayerAPIs::parseLoadMessage` parses `option.windowId`** (identical on 5.5, 5.6, 6.5, 7.6, - 9.2, 10.3, 11.2; the field lands at `MediaApiPrivate+0x7c` on 5.x and `+0x90` on 6.5+), stores it, - and `createAVProxy` hands it to `AVProxy::Builder`. +- **`libplayerAPIs::parseLoadMessage` parses `option.windowId`** (the `get_optional("option.windowId")` + parse is identical on 5.5, 5.6, 6.5, 7.6, 9.2, 10.3, 11.2; the field lands at `MediaApiPrivate+0x7c` + on 5.x, `+0x90` on 6.5.3 and 7.6.0, `+0x94` on 9.2.4 and 10.3.1, and `+0x9c` on 11.2 per the firmware + report), stores it, and passes it to a virtual call. The firmware report identifies that call on 11.2 + (slot +0x78 of the object at `MediaApiPrivate+0x60`) as the AVProxy builder, which `createAVProxy` + uses. On 6.5.3 (slot +0x74 of `+0x5c`), 7.6.0 (+0x74 of `+0x5c`) and 9.2.4 (+0x78 of `+0x60`) only a + virtual call at the same point in the code was seen, and the slot was not separately identified there. - **`option.windowType`** is decoded alongside it: `"InitialWindow"` → 402, `"NoWindow"` → 401, `"NoDisplayControl"` → 403, absent or unrecognised → 400. `AVProxy::checkValidation` requires a non-empty window id **only when the type is the default 400**, which is what our payload sends. @@ -179,7 +185,7 @@ On the exported window (webOS 5+) the production backend places the window as th it creates it at the start and places it with the 1920x1080 canvas as the source, then, when the service's `videoInfo` reports the coded size, again with that size as the source; the destination is always the whole canvas (`uri: exported window placed src=WxH rv=N`). Production selects that binding -only for a firmware major in the evidence table described below, which admits majors 5 and 10 today. A debug build +only for a firmware major in the evidence table described below, which admits majors 5, 6, 7, 9 and 10 today. A debug build composes it for one boot when the file `plxnative-uriexported` exists in the install's runtime root (content ignored). Unlike `plxnative-uriprobe`, which is read at each Play, this trigger is read once, at boot, so it can be armed only by writing the file BEFORE the app launches: on @@ -187,24 +193,28 @@ plain `/tmp`, or at the jail's persistent path (routes 1 and 3 under "Arming the run below). Route 2, through `/proc//root` while the app runs, cannot arm it: that write comes after the boot read and the override stays off. The line that confirms it is `uri: product binding=Exported { canvas: (1920, 1080) } mode=2 evidence=override`, which follows the -production `uri: product binding=Unbound ..` line on a major the table has not admitted (on a 5.x or +production `uri: product binding=Unbound ..` line on a major the table has not admitted (on a 5.x, 6.x, 7.x, 9.x or 10.x set the production line is already `binding=Exported { canvas: (1920, 1080) } ... major=5 -evidence=table` or `... major=10 evidence=table`, and the override line, when armed, still follows it). A log without it proves nothing about the product +evidence=table` (or `major=6`, `7`, `9` or `10`), and the override line, when armed, still follows it). A log without it proves nothing about the product backend of a major the table has not admitted. The viewer still has to opt in to the URI engine, and the override does nothing on a display whose mode is not the exported window. Whether the service sends `videoInfo` before the first frame is not determined, so the first frames may be placed from the canvas source rectangle. Production selects the exported window per firmware major, from the table `EXPORTED_STRATA` -(`rust-modules/play-uri/src/backend.rs`), which admits majors 5 and 10 today (the webOS 5.6.2 and the -webOS 9.2.4 / 10.3.1 Results rows above): a 5.x set's boot line reads `major=5 evidence=table` and a -10.x set's `major=10 evidence=table`. Every other webOS 5+ major still plays Standard (its boot line -reads `major= evidence=none`; a 4.x set reads `none` too and keeps its ACB path). An entry is a +(`rust-modules/play-uri/src/backend.rs`), which admits majors 5, 6, 7, 9 and 10 today (the Results +rows above: 5.6.2; 6.5.3; 7.6.0; 9.2.4 / 10.3.1): a 5.x set's boot line reads `major=5 evidence=table`, +a 10.x set's `major=10 evidence=table`, and a 6.x, 7.x or 9.x set's the same with its own major. Majors +5 and 10 were admitted on tester runs; 6, 7 and 9 were admitted as bracketed, with no tester run. Major +8 has no row: no 8.x image has been read, so it has no (a) and plays Standard. Every other webOS 5+ +major still plays Standard too (its boot line reads `major= evidence=none`; a 4.x set reads `none` +too and keeps its ACB path). An entry is a webOS firmware major as the set reports it, so major 5 covers every 5.x release: the product run was on 5.6.2, the earlier probe-only run on 5.4.2, and no other 5.x minor has a run. Major 10 covers every 10.x release the same way: the product runs (the rule's is the 2026-11-14 one, on a 2.39:1 original) and -the probe were on one LG C2 on 10.3.1 (33.31.68), and no other 10.x minor has a run. Major 9 has an image and no run, and is not admitted. +the probe were on one LG C2 on 10.3.1 (33.31.68), and no other 10.x minor has a run. Majors 6, 7 and 9 each cover every release of that major the same +way, though one image per major is all that has been read (6.5.3, 7.6.0, 9.2.4) and no set has run. An entry needs three things in this file's Results table, each with the tester log's decisive -lines. A release is never admitted on the probe's evidence alone: a run of the product backend is +lines (one exception follows the list). A release is never admitted on the probe's evidence alone: a run of the product backend is required. - (a) the major's own firmware image reads `option.windowId` at load (decompile); @@ -223,9 +233,13 @@ required. switch, routed by #1314 and since observed on the set as `uri: original declined (several audio tracks on the exported window)`); the row names it, and the admission covers only the route as it now stands. -One exception: a major with the image evidence but no tester run is admitted when both neighbouring -majors are admitted on tester evidence, and its row then says "bracketed, no tester run". Nothing else -is inferred from a neighbouring major. Each entry names its Results row, and a webOS 5+ major without +One exception, the owner's decision of 2026-11-13, "nearest admitted major on each side": a major +with (a), its own image showing the same `windowId` load path, but no tester run is admitted when the +nearest admitted major below it and the nearest admitted major above it were both admitted on tester +evidence, and its Results row says "bracketed, no tester run". A major admitted only as bracketed was +not admitted on tester evidence, so it is skipped when looking for a side and never extends a +bracket: 6, 7 and 9 each stand between 5 and 10, never between one another. A major with no image to +read has no (a) and is not admitted (major 8). Nothing else is inferred from a neighbouring major. Each entry names its Results row, and a webOS 5+ major without an entry plays Standard, with no handoff. ## Reproducing it diff --git a/rust-modules/play-uri/src/backend.rs b/rust-modules/play-uri/src/backend.rs index 969ff84ec3..ca26009ea1 100644 --- a/rust-modules/play-uri/src/backend.rs +++ b/rust-modules/play-uri/src/backend.rs @@ -62,13 +62,14 @@ pub fn product_binding(display_mode: i32, firmware_major: u32, strata: &[u32]) - /// The firmware majors on which the product backend selects the exported window /// ([`product_binding`]): the only way production ever composes [`Binding::Exported`]. An entry is /// a webOS firmware major as `device::info().major` reports it (5 for 5.x, 11 for 11.x). The table -/// admits majors 5 and 10 and no other, so every other webOS 5+ major (9 and 11 included) plays -/// Standard until it earns an entry (a webOS 4.x set takes the ACB plane, which this table does not -/// touch). +/// admits majors 5 and 10 on tester evidence, and majors 6, 7 and 9 as bracketed by them with no +/// tester run, and no other, so every other webOS 5+ major (8 and 11 included) plays Standard until +/// it earns an entry (a webOS 4.x set takes the ACB plane, which this table does not touch). /// /// A major is admitted only with all three parts of the evidence in `docs/uri-mode.md`'s Results -/// table, each recorded there with the tester log's decisive lines. A release is never admitted on -/// the developer probe's evidence alone: a run of the product backend is required. +/// table, each recorded there with the tester log's decisive lines (one exception follows the +/// list). A release is never admitted on the developer probe's evidence alone: a run of the product +/// backend is required. /// /// - (a) decompile: that major's own firmware image reads `option.windowId` at load /// (`parseLoadMessage`); @@ -85,10 +86,14 @@ pub fn product_binding(display_mode: i32, firmware_major: u32, strata: &[u32]) - /// (2) A control the product run showed failing is acceptable only when the route now sends that /// case to Standard; the row names it, and the admission covers only the route as it now stands. /// -/// One exception: a major with (a) but no tester run is admitted when BOTH neighbouring majors -/// (`major - 1` and `major + 1`) are already admitted on tester evidence, and its own image shows -/// the same `windowId` path. Its Results row then says "bracketed, no tester run". One admitted -/// neighbour is not a bracket, and a major with no image to read has no (a). +/// One exception, the owner's rule of 2026-11-13, "nearest admitted major on each side": a major +/// with (a), its own image showing the same `windowId` load path, but no tester run is admitted +/// when the nearest admitted major below it and the nearest admitted major above it were both +/// admitted on tester evidence. Its Results row then says "bracketed, no tester run". A major +/// admitted only as bracketed was not admitted on tester evidence, so it is skipped when looking +/// for a side and never extends a bracket: 6, 7 and 9 each stand between 5 and 10, never between +/// one another. One side is not a bracket, a major with no image to read has no (a) (major 8 has +/// none and stays out), and nothing else is inferred from a neighbouring major. /// /// Each entry carries a comment naming its Results row. `0` is never an entry: it is the unknown /// answer. @@ -102,6 +107,22 @@ pub const EXPORTED_STRATA: &[u32] = &[ // four-audio-track run's audio-track switch failed; such a file now starts in Standard (#1314, // amendment (2); `exported_uri_leaves_multi_audio_originals_on_standard` pins the route). 5, + // Firmware major 6 (the 6.5.3 image): `docs/uri-mode.md` Results row "webOS 6.5.3 (firmware major + // 6)", #1064. Bracketed, no tester run. (a) the 6.5.3 image (HE_DTV_W21O_AFABATAA, libplayerAPIs + // sha256 0b0f6654) reads `option.windowId` at load in `parseLoadMessage` (0x41556); bracketed by + // majors 5 and 10, both admitted on tester evidence; no tester run. + 6, + // Firmware major 7 (the 7.6.0 image): `docs/uri-mode.md` Results row "webOS 7.6.0 (firmware major + // 7)", #1064. Bracketed, no tester run. (a) the 7.6.0 image (HE_DTV_W22P_AFADATAA, libplayerAPIs + // sha256 0969903e) reads `option.windowId` at load in `parseLoadMessage` (0x467c6); bracketed by + // majors 5 and 10, both admitted on tester evidence; no tester run. + 7, + // Firmware major 9 (the 9.2.4 image): `docs/uri-mode.md` Results row "webOS 9.2.4 / 10.3.1 + // (33.31.68)", #1064. Bracketed, no tester run. (a) the 9.2.4 image (HE_DTV_W24G_AFABATAA, + // libplayerAPIs sha256 cc08e2b8) reads `option.windowId` at load in `parseLoadMessage` (0x4385c); + // bracketed by majors 5 and 10, both admitted on tester evidence; no tester run. Major 8 has no + // image read, so it has no (a) and no entry. + 9, // webOS 25 (firmware 10.3.1, 33.31.68), LG C2: `docs/uri-mode.md` Results row "webOS 9.2.4 / 10.3.1 // (33.31.68)", #1064. (a) the 10.3 image reads `option.windowId` at load; (b) the product run of // 2026-11-14 (a debug build the tester does not name, answering the request for the CI build of @@ -115,7 +136,7 @@ pub const EXPORTED_STRATA: &[u32] = &[ // paused), so amendment (1) is used: the replay test // `uri_drive_webos_25_c2_run_replays_to_pause_froze_from_the_paused_event_and_resume` grades its // recorded numbers (pause at 2410, first position after resume 2729) pause=froze on - // pause_basis=paused_event_and_resume. Amendment (2) is not used; major 9 has no entry. + // pause_basis=paused_event_and_resume. Amendment (2) is not used. 10, ]; diff --git a/rust-modules/src/app/adapters/backends.rs b/rust-modules/src/app/adapters/backends.rs index 5257198031..ac26cce58a 100644 --- a/rust-modules/src/app/adapters/backends.rs +++ b/rust-modules/src/app/adapters/backends.rs @@ -639,25 +639,27 @@ mod tests { } /// The exported window follows the firmware major through the composition: production's table - /// binds majors 5 (webOS 5.6.2) and 10 (the LG C2 on webOS 25), each with its `docs/uri-mode.md` - /// Results row, and no other (a webOS 5+ release without an entry plays Standard), and a table - /// that lists a major binds it, on a display in the exported-window mode only. The URI engine's - /// own handle is not asked for: a 5+ set has none. + /// binds majors 5 (webOS 5.6.2) and 10 (the LG C2 on webOS 25), each admitted on a tester's run, + /// and majors 6, 7 and 9, each bracketed by those two with no tester run of its own, every one + /// with its `docs/uri-mode.md` Results row, and no other (major 8 has no image read, so it and + /// every webOS 5+ release without an entry plays Standard), and a table that lists a major binds + /// it, on a display in the exported-window mode only. The URI engine's own handle is not asked + /// for: a 5+ set has none. #[cfg(feature = "play-uri")] #[test] fn the_exported_window_is_composed_only_for_a_major_the_table_lists() { use plx_play_uri::backend::{EXPORTED_CANVAS, EXPORTED_STRATA}; let exported = UriBinding::Exported { canvas: EXPORTED_CANVAS }; let sink = BootSink::answering(VP_EXPORTED, vec![7, 8]); - // Production's table admits majors 5 and 10 and nothing else: every other release, known or - // not (9 and 11 included), composes Unbound on this display. An entry lands with its Results - // row and the change to this test. + // Production's table admits majors 5, 6, 7, 9 and 10 and nothing else: every other release, + // known or not (8, which has no image read, and 11 included), composes Unbound on this + // display. An entry lands with its Results row and the change to this test. for major in 0..=30 { - let want = if matches!(major, 5 | 10) { exported } else { UriBinding::Unbound }; + let want = if matches!(major, 5 | 6 | 7 | 9 | 10) { exported } else { UriBinding::Unbound }; assert_eq!(uri_product_binding(&sink, major, EXPORTED_STRATA), want, "major {major}"); } // The same display on a set whose mode is not the exported window: an admitted major binds nothing there. - for major in [5, 10] { + for major in [5, 6, 7, 9, 10] { for mode in [VP_NONE, plx_platform::tv::sink::VP_ACB] { let sink = BootSink::answering(mode, vec![7, 8]); assert_ne!(uri_product_binding(&sink, major, EXPORTED_STRATA), exported, "major {major} mode {mode}"); @@ -689,13 +691,18 @@ mod tests { let unlisted = line(VP_EXPORTED, 11, &[]); assert!(unlisted.contains("uri: product binding=Unbound mode=2 "), "{unlisted}"); assert!(unlisted.contains(" major=11 evidence=none\n"), "{unlisted}"); - // Production's own table: a webOS 5.6.2 television's boot line reads `major=5 evidence=table` - // and the LG C2 on webOS 25's reads `major=10 evidence=table`. - for major in [5, 10] { + // Production's own table: a webOS 5.6.2 television's boot line reads `major=5 evidence=table`, + // the LG C2 on webOS 25's `major=10 evidence=table`, and a bracketed major's (6, 7, 9) the + // same `evidence=table`: the line does not say whether a tester ran it. Major 8 has no + // image read and reads `major=8 evidence=none` on the same display. + for major in [5, 6, 7, 9, 10] { let shipped = line(VP_EXPORTED, major, plx_play_uri::backend::EXPORTED_STRATA); assert!(shipped.contains("uri: product binding=Exported { canvas: (1920, 1080) } mode=2 "), "{shipped}"); assert!(shipped.contains(&format!(" major={major} evidence=table\n")), "{shipped}"); } + let eight = line(VP_EXPORTED, 8, plx_play_uri::backend::EXPORTED_STRATA); + assert!(eight.contains("uri: product binding=Unbound mode=2 "), "{eight}"); + assert!(eight.contains(" major=8 evidence=none\n"), "{eight}"); let acb = line(plx_platform::tv::sink::VP_ACB, 4, &[]); assert!(acb.contains("uri: product binding=Acb mode=1 handle=deferred ptype=0 "), "{acb}"); assert!(acb.contains(" major=4 evidence=none\n"), "{acb}");