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
+55
View File
@@ -0,0 +1,55 @@
"""Preview adaptation consumes new hardware frames, never repeats a stalled source."""
import asyncio
import importlib.util
import time
from pathlib import Path
from types import SimpleNamespace
import aioice.ice
import numpy as np
import pytest
from missioncore_plugin_sdk.media_budget import MediaBudget
def test_realsense_preview_changes_size_without_replaying_a_stalled_source(monkeypatch):
original = aioice.ice.get_host_addresses
monkeypatch.setattr(aioice.ice, "get_host_addresses", original)
path = Path(__file__).parents[2] / "apps/node-agent/sensors/media.py"
spec = importlib.util.spec_from_file_location("sensor_media_test", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
async def run():
device = SimpleNamespace(
images={"color": np.zeros((480, 640, 3), dtype=np.uint8)},
frames={"color": 1},
last_frame={"monotonic_ns": time.monotonic_ns()},
)
budget = MediaBudget("video")
budget.update(
{"schema": "missioncore.media-budget/v1", "allocation_bps": 800_000, "ttl_ms": 3000}
)
track = module.CameraTrack(device, "color", budget)
try:
first = await asyncio.wait_for(track.recv(), 0.5)
assert (first.width, first.height) == (640, 480)
with pytest.raises(TimeoutError):
await asyncio.wait_for(track.recv(), 0.15)
device.frames["color"] = 2
device.last_frame["monotonic_ns"] = time.monotonic_ns()
budget.update(
{"schema": "missioncore.media-budget/v1", "allocation_bps": 100_000, "ttl_ms": 3000}
)
second = await asyncio.wait_for(track.recv(), 0.5)
assert (second.width, second.height) == (160, 120) and second.pts > first.pts
assert device.images["color"].shape == (480, 640, 3)
device.frames["color"] = 3
device.last_frame["monotonic_ns"] = time.monotonic_ns() - 3_000_000_000
with pytest.raises(TimeoutError):
await asyncio.wait_for(track.recv(), 0.6)
finally:
track.stop()
budget.connection.close()
asyncio.run(run())
+20
View File
@@ -81,3 +81,23 @@ def test_disconnect_finalizes_failed_record_and_clears_live_frames(monkeypatch,
assert value["state"] == "failed" and value["sha256"]
device.disconnect()
assert stopped == [True]
def test_preview_point_bound_does_not_modify_depth_capture(monkeypatch, tmp_path):
import numpy as np
rs = types.ModuleType("pyrealsense2")
rs.rs2_deproject_pixel_to_point = lambda intrinsics, point, z: [*point, z]
monkeypatch.setitem(__import__("sys").modules, "pyrealsense2", rs)
path = Path(__file__).parents[2] / "apps/node-agent/sensors/device.py"
spec = importlib.util.spec_from_file_location("points_device", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
device = module.Device("synthetic-camera", tmp_path, {})
device.depth = np.ones((479, 641), dtype=np.uint16)
device.intrinsics = object()
device.depth_scale = 0.001
before = device.depth.copy()
assert len(device.points(7)) <= 21 and len(device.points(2000)) <= 6000
assert device.points(0) == []
assert np.array_equal(device.depth, before)
+6 -3
View File
@@ -23,6 +23,8 @@ def test_node_update_restores_only_previously_running_configured_plugin(
state = tmp_path / "active"
if active:
state.touch()
cache_helper = tmp_path / "clear_runtime_cache.py"
cache_helper.write_text("# OS cache cleanup is independently qualified.\n")
events = tmp_path / "events"
systemctl = binary / "systemctl"
systemctl.write_text('''#!/bin/sh
@@ -30,8 +32,8 @@ printf '%s\\n' "$*" >> "$TEST_EVENTS"
case "$1" in
is-active) [ -f "$TEST_ACTIVE" ]; exit $? ;;
show) echo inactive ;;
stop) rm -f "$TEST_ACTIVE" ;;
start) touch "$TEST_ACTIVE" ;;
stop) [ "$2" != mission-core-k1.service ] || rm -f "$TEST_ACTIVE" ;;
start) [ "$2" != mission-core-k1.service ] || touch "$TEST_ACTIVE" ;;
esac
''')
(binary / "getent").write_text("#!/bin/sh\nexit 0\n")
@@ -48,7 +50,8 @@ esac
script.write_text((PACKAGING / name).read_text()
.replace("/run/", str(run) + "/")
.replace("/usr/lib/mission-core-node/setup-monitor", str(binary / "setup-monitor"))
.replace("/etc/os-release", str(release)))
.replace("/etc/os-release", str(release))
.replace("/usr/lib/mission-core-vesc/clear_runtime_cache.py", str(cache_helper)))
subprocess.run(["/bin/sh", str(script), *args], env=env, check=True,
capture_output=True, text=True)
if name == "preinst":
+14 -2
View File
@@ -7,6 +7,18 @@ from aiortc import RTCConfiguration, RTCPeerConnection, RTCSessionDescription
from k1link.viewer.node_media import MEDIA_PROTOCOL, NodeMediaPeers, admit_sdp
@pytest.fixture(autouse=True)
def synthetic_link_budget(monkeypatch):
from missioncore_plugin_sdk.media_budget import MediaBudget
monkeypatch.setattr(MediaBudget, "exchange", lambda self, active=True: {
"schema": "missioncore.media-budget/v1", "allocation_bps": 4_000_000, "ttl_ms": 3000})
from missioncore_plugin_sdk.link_budget import BudgetClient
async def permit(self, size):
return True
monkeypatch.setattr(BudgetClient, "_permit", permit)
SDP_HEADER = "v=0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n"
@@ -19,7 +31,7 @@ def test_media_rejects_non_private_or_relay_candidates(address, kind):
def test_media_accepts_paired_lan_and_tailnet_candidates():
for address in ("192.168.1.2", "100.80.6.113", "peer.local"):
for address in ("192.168.1.2", "100.64.0.10", "peer.local"):
admit_sdp(SDP_HEADER + f"a=candidate:1 1 udp 1 {address} 12345 typ host\r\n")
@@ -72,7 +84,7 @@ def test_native_webrtc_roundtrip_and_missing_camera_preserve_rrd(monkeypatch):
pass
class Hub:
def subscribe(self, view_id, after):
def subscribe(self, view_id, after, **kwargs):
return Subscription()
class Camera: