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
+64 -2
View File
@@ -24,6 +24,8 @@ class Header(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),
]
@@ -56,6 +58,9 @@ def library(folder):
capture_output=True,
)
api = ctypes.CDLL(str(target))
api.mc_x4_abi.restype = ctypes.c_int
assert api.mc_x4_abi() == 2
assert ctypes.sizeof(Header) == 48
api.mc_x4_open.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
api.mc_x4_open.restype = ctypes.c_void_p
api.mc_x4_call.argtypes = [
@@ -98,9 +103,33 @@ def test_details_include_session_identity_and_optics_without_starting_preview(ca
result = call(camera, 1)["result"]
assert result["serial"] == "SYNTHETIC-X4"
assert len(result["preview_calibration"]["offsets"]) == 1
assert {
k: result["preview_calibration"][k]
for k in (
"camera_name",
"encode_type",
"delay_timestamp",
"sweep_time",
"acceleration_range",
"gyro_range",
"revision",
)
} == {
"camera_name": "Insta360 X4",
"encode_type": 0,
"delay_timestamp": 127,
"sweep_time": 391,
"acceleration_range": 16,
"gyro_range": 2000,
"revision": 1,
}
assert result["preview_calibration"]["crop"] == {
"source_width": 1920, "source_height": 960,
"width": 1920, "height": 960, "x": 0, "y": 0,
"source_width": 1920,
"source_height": 960,
"width": 1920,
"height": 960,
"x": 0,
"y": 0,
}
assert api.test_count(3) == 0 and api.test_count(6) == 0
@@ -165,6 +194,12 @@ def test_frames_copy_vendor_memory_preserve_stream_and_bound_backlog(camera):
info, buffer = Header(), (ctypes.c_uint8 * 24)()
assert api.mc_x4_read_video(handle, ctypes.byref(info), buffer, 24) == 1
assert (info.stream_index, info.timestamp, info.bytes) == (1, 12345, 24)
assert info.stream_type == 0x21 and info.codec == 0
assert (
info.calibration_revision
== call(camera, 1)["result"]["preview_calibration"]["revision"]
== 2
)
assert bytes(buffer) == bytes([91]) * 24
generation = info.generation
for i in range(65):
@@ -173,6 +208,11 @@ def test_frames_copy_vendor_memory_preserve_stream_and_bound_backlog(camera):
assert info.generation > generation # decoder must reset after this loss
assert bytes(buffer) == bytes([64]) * 24
assert api.mc_x4_read_video(handle, ctypes.byref(info), buffer, 24) == 0
call(camera, 5)
call(camera, 4)
api.test_packet(0, 24, 17)
assert api.mc_x4_read_video(handle, ctypes.byref(info), buffer, 24) == 1
assert info.calibration_revision == 3 and info.stream_type == 0x20
def test_camera_files_are_bounded_pages(camera):
@@ -191,6 +231,28 @@ def test_early_camera_stop_notification_wins_over_start_acknowledgement(camera):
assert api.test_count(4) == 1
def test_download_requires_current_list_entry_and_never_stops_recording(camera):
api, handle = camera
progress_type = ctypes.CFUNCTYPE(None, ctypes.c_int64, ctypes.c_int64)
api.mc_x4_download.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, progress_type]
api.mc_x4_download.restype = ctypes.c_int
observed = []
callback = progress_type(lambda current, total: observed.append((current, total)))
with tempfile.TemporaryDirectory(dir=ROOT / "build") as directory:
path = Path(directory) / "original.insv"
args = (handle, b"/DCIM/synthetic_0.insv", str(path).encode(), callback)
assert api.mc_x4_download(handle, b"/DCIM/not-listed.insv", str(path).encode(), callback) == -1
assert not path.exists()
api.test_recording(1)
assert api.mc_x4_download(*args) == -1
assert not path.exists() and api.test_count(5) == 0
api.test_recording(0)
assert api.mc_x4_download(*args) == 1
assert path.read_bytes() == b"synthetic camera original"
assert observed == [(25, 25)]
assert api.test_count(3) == 0 and api.test_count(5) == 0
def test_unavailable_calibration_preserves_camera_control(library):
for mode in (1, 2):
library.test_calibration(mode)
+17 -2
View File
@@ -4,6 +4,7 @@
#include <camera/device_discovery.h>
#include <algorithm>
#include <stdexcept>
#include <fstream>
namespace {
int discovered = 1, opens = 0, closes = 0, freed = 0, writes = 0;
@@ -32,7 +33,13 @@ uint32_t PreviewParam::GetCropDstWidth() const { return 1920; }
uint32_t PreviewParam::GetCropDstHeight() const { return 960; }
int32_t PreviewParam::GetCropOffsetX() const { return 0; }
int32_t PreviewParam::GetCropOffsetY() const { return 0; }
PreviewParam Camera::GetPreviewParam() const { return PreviewParam(); }
PreviewParam Camera::GetPreviewParam() const {
PreviewParam p;
p.camera_name = "Insta360 X4"; p.encode_type = VideoEncodeType::H264;
p.delay_timestamp = 127; p.sweep_time = 391;
p.acceleration_range = 16; p.gyro_range = 2000;
return p;
}
class ExposureSettingsPrivate {
public:
@@ -76,6 +83,14 @@ std::vector<std::string> Camera::GetCameraFilesList() const {
for (int i = 0; i < 65; ++i) out.push_back("/DCIM/synthetic_" + std::to_string(i) + ".insv");
return out;
}
bool Camera::DownloadCameraFile(const std::string&, const std::string& local, DownloadProgressCallBack progress) const {
std::ofstream stream(local, std::ios::binary);
stream << "synthetic camera original";
stream.close();
if (progress) progress(25, 25);
return bool(stream);
}
void Camera::CancelDownload() {}
std::vector<DeviceDescriptor> DeviceDiscovery::GetAvailableDevices() {
std::vector<DeviceDescriptor> out;
for (int i = 0; i < discovered; ++i) out.push_back({CameraType::Insta360X4, "SYNTHETIC-X4", "Insta360 X4", "TEST-FW", {ConnectionType::USB, "Insta360 X4", nullptr}});
@@ -139,7 +154,7 @@ void test_recording(int value) { recording = value; if (capture_callback) captur
void test_packet(int stream_index, int size, int marker) {
if (!delegate) return;
std::vector<uint8_t> packet(size, uint8_t(marker));
delegate->OnVideoData(packet.data(), packet.size(), 12345, 0, stream_index);
delegate->OnVideoData(packet.data(), packet.size(), 12345, 0x20 + stream_index, stream_index);
std::fill(packet.begin(), packet.end(), 0); // prove bridge owns copied bytes
}
}