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:
@@ -8,6 +8,7 @@ import uuid
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
|
||||
from .archive import Archive
|
||||
from .frames import Feed, encode
|
||||
from .http import Binary, Server
|
||||
from .identity import read_binding
|
||||
@@ -36,6 +37,8 @@ class Worker:
|
||||
self.feed = None
|
||||
self.operations = None
|
||||
self.runtime, self.state = runtime, state
|
||||
self.command_gate = threading.Lock()
|
||||
self.archive = None
|
||||
|
||||
def connect(self):
|
||||
# Bounded startup even if a vendor call never returns. No restart loop:
|
||||
@@ -55,6 +58,7 @@ class Worker:
|
||||
self.feed = Feed(self.camera)
|
||||
self.feed.thread.start()
|
||||
self.control = Control(self.camera, self.feed)
|
||||
self.archive = Archive(self.camera, self.state / "originals", self.command_gate)
|
||||
self.operations = Operations(
|
||||
self.binding.device_id, self.session, self.state / "operations", self.control
|
||||
)
|
||||
@@ -68,7 +72,12 @@ class Worker:
|
||||
def refresh(self):
|
||||
if read_binding(self.binding.port) != self.binding:
|
||||
raise RuntimeError("Camera transport changed")
|
||||
response = self.camera.call("details", {})
|
||||
if not self.command_gate.acquire(blocking=False):
|
||||
return
|
||||
try:
|
||||
response = self.camera.call("details", {})
|
||||
finally:
|
||||
self.command_gate.release()
|
||||
if response["state"] != "complete":
|
||||
raise RuntimeError("Camera status is unconfirmed")
|
||||
with self.status_lock:
|
||||
@@ -78,6 +87,7 @@ class Worker:
|
||||
self.revision += 1
|
||||
|
||||
def snapshot(self):
|
||||
transfer_active = bool(self.archive and self.archive.busy())
|
||||
with self.status_lock:
|
||||
state = dict(self.status)
|
||||
fresh = time.monotonic() - self.observed < 12
|
||||
@@ -98,7 +108,9 @@ class Worker:
|
||||
"verified": verified,
|
||||
"revision": revision,
|
||||
"observed_at": observed_at,
|
||||
"file_transfer": {"active": transfer_active},
|
||||
"preparation_safe": connected
|
||||
and not transfer_active
|
||||
and state.get("preview") == 0
|
||||
and state.get("recording") == 0,
|
||||
"status": state if fresh else {},
|
||||
@@ -108,19 +120,39 @@ class Worker:
|
||||
def dispatch(self, method, route, value, headers):
|
||||
if method == "GET" and route == "/snapshot":
|
||||
return self.snapshot()
|
||||
if method == "POST" and route == "/video":
|
||||
if method == "POST" and route in ("/archive/start", "/archive/status", "/archive/read"):
|
||||
keys = {"session_id", "id"} | (
|
||||
{"source"}
|
||||
if route.endswith("start")
|
||||
else {"offset"}
|
||||
if route.endswith("read")
|
||||
else set()
|
||||
)
|
||||
if not self.ready or value.get("session_id") != self.session or set(value) != keys:
|
||||
raise ValueError("Camera file session changed")
|
||||
if route == "/archive/start":
|
||||
return self.archive.start(value["id"], value["source"])
|
||||
if route == "/archive/status":
|
||||
return self.archive.status(value["id"])
|
||||
return Binary(self.archive.read(value["id"], value["offset"]))
|
||||
if method == "POST" and route in ("/video", "/sdk-video"):
|
||||
if (
|
||||
not self.ready
|
||||
or value.get("session_id") != self.session
|
||||
or set(value) != {"session_id", "cursor"}
|
||||
):
|
||||
raise ValueError("Camera video session changed")
|
||||
return Binary(encode(self.feed.read(value["cursor"])))
|
||||
return Binary(encode(self.feed.read(value["cursor"], original=route == "/sdk-video")))
|
||||
if method == "POST" and route == "/operation":
|
||||
if not self.ready:
|
||||
raise RuntimeError("Camera is unavailable")
|
||||
with acquisition():
|
||||
result = self.operations.execute(value)
|
||||
if not self.command_gate.acquire(blocking=False):
|
||||
raise RuntimeError("Camera file transfer or status read is active")
|
||||
try:
|
||||
with acquisition():
|
||||
result = self.operations.execute(value)
|
||||
finally:
|
||||
self.command_gate.release()
|
||||
# Poll asynchronously; a lost state read must not change a durable
|
||||
# acknowledged command result into a different receipt.
|
||||
if value.get("action_id") not in ("details", "settings.read", "files.list"):
|
||||
|
||||
Reference in New Issue
Block a user