Persist rover observation configuration and calibrate Insta360 panorama

This commit is contained in:
DCCONSTRUCTIONS
2026-10-09 18:17:54 +03:00
parent c43b097a3a
commit b4dab01f2a
36 changed files with 1271 additions and 50 deletions
+31 -3
View File
@@ -24,8 +24,8 @@ constexpr size_t MAX_PACKET = 4 * 1024 * 1024;
constexpr size_t MAX_QUEUE = 8 * 1024 * 1024;
constexpr size_t MAX_RESULT = 60 * 1024;
std::string quote(const std::string& value) {
if (value.size() > 1024) throw std::runtime_error("oversized SDK string");
std::string quote(const std::string& value, size_t limit = 1024) {
if (value.size() > limit) throw std::runtime_error("oversized SDK string");
std::ostringstream out;
out << '"';
for (unsigned char c : value) {
@@ -111,7 +111,7 @@ struct mc_x4 {
std::shared_ptr<Camera> camera;
std::shared_ptr<Frames> frames = std::make_shared<Frames>();
std::shared_ptr<Notifications> notifications = std::make_shared<Notifications>();
std::string firmware, result;
std::string firmware, serial, calibration = "null", result;
bool opened = false;
int preview = 0;
// Only the isolated worker calls control methods; callbacks use separate
@@ -145,6 +145,8 @@ struct mc_x4 {
}
}
std::string result = "{\"firmware\":" + quote(firmware) +
",\"serial\":" + quote(serial) +
",\"preview_calibration\":" + calibration +
",\"connected\":" + (connected ? "true" : "false") +
",\"preview\":" + std::to_string(preview) +
",\"recording\":" + std::to_string(notifications->recording) +
@@ -165,6 +167,30 @@ struct mc_x4 {
return complete(result);
}
void read_preview_calibration() {
// Read-only metadata from this session's camera. A missing/unknown
// calibration must not take down camera control or the raw lens feed.
calibration = "null";
try {
auto param = camera->GetPreviewParam();
const auto count = param.GetCalibrationCount();
if (!count || count > 4) return;
std::ostringstream out;
out << "{\"offsets\":[";
for (size_t i = 0; i < count; ++i) {
if (i) out << ',';
out << quote(param.GetCalibration(i), 8192);
}
out << "],\"crop\":{\"source_width\":" << param.GetCropSrcWidth()
<< ",\"source_height\":" << param.GetCropSrcHeight()
<< ",\"width\":" << param.GetCropDstWidth()
<< ",\"height\":" << param.GetCropDstHeight()
<< ",\"x\":" << param.GetCropOffsetX()
<< ",\"y\":" << param.GetCropOffsetY() << "}}";
calibration = out.str();
} catch (...) { calibration = "null"; }
}
std::string settings(CameraFunctionMode mode) {
if (!mode_supported(mode)) return INVALID;
if (!camera->SyncPhotographyOptions(mode)) return UNKNOWN;
@@ -380,11 +406,13 @@ mc_x4* mc_x4_open(const char* serial, const char* log_directory) {
const auto& device = handle->descriptors.front();
if (device.camera_type != CameraType::Insta360X4 || device.info.connection_type != ConnectionType::USB || device.serial_number != serial) return nullptr;
handle->firmware = device.fw_version;
handle->serial = device.serial_number;
handle->camera = std::make_shared<Camera>(device.info);
handle->camera->SetServicePort(9099); // private network namespace per instance
handle->camera->SetTimeout(10000);
handle->opened = handle->camera->Open();
if (!handle->opened) return nullptr;
handle->read_preview_calibration();
auto n = handle->notifications;
handle->camera->SetCaptureStateNotification([n](bool active) {
std::lock_guard<std::mutex> lock(n->mutex);
@@ -93,6 +93,18 @@ def test_discovery_never_selects_first_of_multiple_cameras(library):
assert library.test_count(0) == 0 and library.test_count(2) == 2
def test_details_include_session_identity_and_optics_without_starting_preview(camera):
api, _ = camera
result = call(camera, 1)["result"]
assert result["serial"] == "SYNTHETIC-X4"
assert len(result["preview_calibration"]["offsets"]) == 1
assert result["preview_calibration"]["crop"] == {
"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
def test_preview_stop_and_close_do_not_stop_recording(camera):
api, handle = camera
assert call(camera, 6)["state"] == "complete"
@@ -179,6 +191,20 @@ def test_early_camera_stop_notification_wins_over_start_acknowledgement(camera):
assert api.test_count(4) == 1
def test_unavailable_calibration_preserves_camera_control(library):
for mode in (1, 2):
library.test_calibration(mode)
handle = library.mc_x4_open(b"SYNTHETIC-X4", b"/unused")
assert handle
try:
result = call((library, handle), 1)["result"]
assert result["serial"] == "SYNTHETIC-X4"
assert result["preview_calibration"] is None
assert library.test_count(3) == 0 and library.test_count(6) == 0
finally:
library.mc_x4_close(handle)
class TestNativeBridge(unittest.TestCase):
@classmethod
def setUpClass(cls):
+18 -2
View File
@@ -10,7 +10,7 @@ int discovered = 1, opens = 0, closes = 0, freed = 0, writes = 0;
int record_starts = 0, record_stops = 0, previews = 0, preview_stops = 0;
bool recording = false, start_ack = true, preview_ack = true, connected = true;
bool stop_before_ack = false;
int function_mode = 7;
int function_mode = 7, calibration_mode = 0;
std::shared_ptr<ins_camera::StreamDelegate> delegate;
ins_camera::CaptureStateCallBack capture_callback;
std::shared_ptr<ins_camera::CaptureSettings> capture;
@@ -19,6 +19,21 @@ std::shared_ptr<ins_camera::ExposureSettings> exposure;
namespace ins_camera {
class CameraImpl {};
struct PreviewParam::Impl {};
PreviewParam::PreviewParam() = default;
PreviewParam::~PreviewParam() = default;
PreviewParam::PreviewParam(PreviewParam&&) noexcept = default;
PreviewParam& PreviewParam::operator=(PreviewParam&&) noexcept = default;
size_t PreviewParam::GetCalibrationCount() const { if (calibration_mode == 1) throw std::runtime_error("unavailable"); return calibration_mode == 2 ? 5 : 1; }
std::string PreviewParam::GetCalibration(size_t) const { return "2_480_480_480_0_0_0_480_1440_480_0_0_0_1920_960_0"; }
uint32_t PreviewParam::GetCropSrcWidth() const { return 1920; }
uint32_t PreviewParam::GetCropSrcHeight() const { return 960; }
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(); }
class ExposureSettingsPrivate {
public:
int iso = 100, ev = 0;
@@ -109,13 +124,14 @@ void ExposureSettings::SetExposureMode(PhotographyOptions_ExposureMode value) {
extern "C" {
void test_reset() {
discovered = 1; opens = closes = freed = writes = record_starts = record_stops = previews = preview_stops = 0;
recording = stop_before_ack = false; connected = start_ack = preview_ack = true; function_mode = 7;
recording = stop_before_ack = false; connected = start_ack = preview_ack = true; function_mode = 7; calibration_mode = 0;
delegate.reset(); capture_callback = nullptr;
capture = std::make_shared<ins_camera::CaptureSettings>(); exposure = std::make_shared<ins_camera::ExposureSettings>();
}
int test_count(int which) {
switch (which) { case 0: return opens; case 1: return closes; case 2: return freed; case 3: return writes; case 4: return record_starts; case 5: return record_stops; case 6: return previews; case 7: return preview_stops; default: return -1; }
}
void test_calibration(int mode) { calibration_mode = mode; }
void test_discovered(int count) { discovered = count; }
void test_ack(int type, int value) { if (type == 0) start_ack = value; else if (type == 1) preview_ack = value; else stop_before_ack = value; }
void test_connected(int value) { connected = value; }