feat(video): recover camera previews and reduce encoding and playout cost

Improve camera recovery, shared preview lifecycle and receiver playout diagnostics. Preserve qualified X4 runtime changes and restore async admission in loopback media tests.
This commit is contained in:
DCCONSTRUCTIONS
2026-10-29 09:10:01 +03:00
parent e06ff9a214
commit fa8bde584f
94 changed files with 8892 additions and 375 deletions
+20 -8
View File
@@ -83,6 +83,10 @@ def decode(raw):
or header["stream_index"] not in (0, 1)
or header["codec"] not in (0, 1)
or type(header.get("gap")) is not bool
or (
"stream_type" in header
and (type(header["stream_type"]) is not int or not 0 <= header["stream_type"] <= 255)
)
or not 0 < len(data) <= MAX_PACKET
):
raise ValueError("Invalid video metadata")
@@ -121,41 +125,49 @@ class Feed:
self.source_generation = source["generation"]
self.generation += 1
self.parameters = Parameters()
original_bytes = len(data)
data = self.parameters.packet(source, data)
prefix_bytes = len(data) - original_bytes
while self.queue and (
self.bytes + len(data) > MAX_BYTES or len(self.queue) >= MAX_ENTRIES
):
_, removed, _ = self.queue.popleft()
_, removed, _, _ = self.queue.popleft()
self.bytes -= len(removed)
self.cursor += 1
header = dict(source, cursor=self.cursor, generation=self.generation)
self.queue.append((header, data, time.monotonic()))
# Keep one bounded allocation. The SDK reader receives precisely
# the original callback bytes; browser readers retain late-join
# parameter replay without changing the vendor packet contract.
self.queue.append((header, data, time.monotonic(), prefix_bytes))
self.bytes += len(data)
self.lock.notify_all()
def read(self, cursor, wait=0.1):
def read(self, cursor, wait=0.1, *, original=False):
if type(cursor) is not int or not 0 <= cursor < 2**64:
raise ValueError("Invalid video cursor")
deadline = time.monotonic() + min(max(wait, 0), 0.2)
with self.lock:
while True:
for header, data, observed in self.queue:
for header, data, observed, prefix_bytes in self.queue:
if header["cursor"] > cursor and time.monotonic() - observed < 2:
payload = data[prefix_bytes:] if original else data
return dict(
header, gap=bool(cursor and header["cursor"] != cursor + 1)
), data
header,
bytes=len(payload),
gap=bool(cursor and header["cursor"] != cursor + 1),
), payload
remaining = deadline - time.monotonic()
if remaining <= 0 or self.closed.is_set():
return None
self.lock.wait(remaining)
def reader(self):
def reader(self, *, original=False):
with self.lock:
cursor = self.cursor
def read():
nonlocal cursor
value = self.read(cursor, wait=0)
value = self.read(cursor, wait=0, original=original)
if value is not None:
cursor = value[0]["cursor"]
return value