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
+40 -3
View File
@@ -20,6 +20,7 @@ ACTIONS = {
"files.list": 9,
}
MAX_PACKET = 4 * 1024 * 1024
DownloadProgress = ctypes.CFUNCTYPE(None, ctypes.c_int64, ctypes.c_int64)
class VideoHeader(ctypes.Structure):
@@ -30,6 +31,8 @@ class VideoHeader(ctypes.Structure):
("stream_index", ctypes.c_int32),
("codec", ctypes.c_int32),
("bytes", ctypes.c_uint32),
("stream_type", ctypes.c_int32),
("calibration_revision", ctypes.c_uint64),
]
@@ -52,7 +55,8 @@ def parameters(action, value):
if action == "offer":
if (
not isinstance(value, dict)
or set(value) != {"sdp", "layer"}
or set(value) not in ({"sdp", "layer"}, {"sdp", "layer", "primary"})
or type(value.get("primary", False)) is not bool
or value["layer"] != "preview"
or not isinstance(value["sdp"], str)
or not 0 < len(value["sdp"]) <= 32768
@@ -123,7 +127,7 @@ class NativeCamera:
self.video_lock = threading.Lock()
self.api = ctypes.CDLL(str(library_path))
self.api.mc_x4_abi.restype = ctypes.c_int
if self.api.mc_x4_abi() != 1:
if self.api.mc_x4_abi() != 2:
raise RuntimeError("Incompatible camera adapter")
self.api.mc_x4_open.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
self.api.mc_x4_open.restype = ctypes.c_void_p
@@ -135,6 +139,15 @@ class NativeCamera:
ctypes.c_double,
]
self.api.mc_x4_call.restype = ctypes.c_char_p
self.api.mc_x4_download.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
DownloadProgress,
]
self.api.mc_x4_download.restype = ctypes.c_int
self.api.mc_x4_cancel_download.argtypes = [ctypes.c_void_p]
self.api.mc_x4_cancel_download.restype = None
self.api.mc_x4_read_video.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(VideoHeader),
@@ -166,6 +179,24 @@ class NativeCamera:
raise RuntimeError("Camera returned an invalid response")
return result
def download(self, source, destination, progress):
callback = DownloadProgress(progress)
with self.lock:
if not self.handle:
raise RuntimeError("Camera session is closed")
return (
self.api.mc_x4_download(
self.handle, source.encode(), str(destination).encode(), callback
)
== 1
)
def cancel_download(self):
# Called only by the active transfer's watchdog. Taking the command
# lock here would deadlock with synchronous DownloadCameraFile.
if self.handle:
self.api.mc_x4_cancel_download(self.handle)
def video(self):
# SDK control calls may block for seconds. The callback queue has its
# own native mutex and can be drained without taking the command lock.
@@ -180,7 +211,13 @@ class NativeCamera:
raise RuntimeError("Invalid video packet")
if state == 0:
return None
if header.stream_index not in (0, 1) or header.codec not in (0, 1) or not header.bytes:
if (
header.stream_index not in (0, 1)
or header.codec not in (0, 1)
or not 0 <= header.stream_type <= 255
or not header.calibration_revision
or not header.bytes
):
raise RuntimeError("Unsupported video packet")
return {name: getattr(header, name) for name, _ in VideoHeader._fields_}, bytes(
self.buffer[: header.bytes]