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:
@@ -67,6 +67,7 @@ struct Frames final : StreamDelegate {
|
||||
size_t bytes = 0;
|
||||
uint64_t generation = 0, sequence = 0;
|
||||
std::atomic<int> codec{0};
|
||||
std::atomic<uint64_t> calibration_revision{0};
|
||||
std::atomic<bool> enabled{false};
|
||||
|
||||
void reset() {
|
||||
@@ -74,7 +75,7 @@ struct Frames final : StreamDelegate {
|
||||
queue.clear(); bytes = 0; ++generation;
|
||||
}
|
||||
void OnVideoData(const uint8_t* data, size_t size, int64_t timestamp,
|
||||
uint8_t, int stream_index) override {
|
||||
uint8_t stream_type, int stream_index) override {
|
||||
if (!enabled || !data || !size || size > MAX_PACKET || stream_index < 0 || stream_index > 1) return;
|
||||
// SDK owns the callback bytes. Copy into a bounded queue; never block
|
||||
// a vendor callback on a browser, disk, decoder or Python interpreter.
|
||||
@@ -84,7 +85,8 @@ struct Frames final : StreamDelegate {
|
||||
queue.clear(); bytes = 0; ++generation;
|
||||
}
|
||||
queue.push_back({{++sequence, generation, timestamp, stream_index,
|
||||
codec.load(), uint32_t(size)}, {data, data + size}});
|
||||
codec.load(), uint32_t(size), stream_type,
|
||||
calibration_revision.load()}, {data, data + size}});
|
||||
bytes += size;
|
||||
} catch (...) {
|
||||
// A memory allocation failure drops data; it must not unwind into
|
||||
@@ -171,12 +173,20 @@ struct mc_x4 {
|
||||
// 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";
|
||||
const auto revision = ++frames->calibration_revision;
|
||||
try {
|
||||
auto param = camera->GetPreviewParam();
|
||||
const auto count = param.GetCalibrationCount();
|
||||
if (!count || count > 4) return;
|
||||
std::ostringstream out;
|
||||
out << "{\"offsets\":[";
|
||||
out << "{\"revision\":" << revision
|
||||
<< ",\"camera_name\":" << quote(param.camera_name, 128)
|
||||
<< ",\"encode_type\":" << int(param.encode_type)
|
||||
<< ",\"delay_timestamp\":" << param.delay_timestamp
|
||||
<< ",\"sweep_time\":" << param.sweep_time
|
||||
<< ",\"acceleration_range\":" << param.acceleration_range
|
||||
<< ",\"gyro_range\":" << param.gyro_range
|
||||
<< ",\"offsets\":[";
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
if (i) out << ',';
|
||||
out << quote(param.GetCalibration(i), 8192);
|
||||
@@ -318,6 +328,7 @@ struct mc_x4 {
|
||||
params.enable_audio = false; params.enable_gyro = false; params.using_lrv = false;
|
||||
params.video_resolution = RES_1920_960P30; params.lrv_video_resulution = RES_1920_960P30;
|
||||
params.video_bitrate = 4000000;
|
||||
read_preview_calibration();
|
||||
frames->reset(); frames->codec = int(camera->GetVideoEncodeType()); frames->enabled = true;
|
||||
preview = -1;
|
||||
bool ok = camera->StartLiveStreaming(params);
|
||||
@@ -392,7 +403,7 @@ struct mc_x4 {
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
int mc_x4_abi(void) { return 1; }
|
||||
int mc_x4_abi(void) { return 2; }
|
||||
mc_x4* mc_x4_open(const char* serial, const char* log_directory) {
|
||||
try {
|
||||
if (!serial || !*serial || std::strlen(serial) > 256 || !log_directory || !*log_directory) return nullptr;
|
||||
@@ -439,6 +450,21 @@ const char* mc_x4_call(mc_x4* handle, int action, int mode, const char* key, dou
|
||||
return handle->result.c_str();
|
||||
} catch (...) { return UNKNOWN; }
|
||||
}
|
||||
int mc_x4_download(mc_x4* handle, const char* remote, const char* local, mc_x4_download_progress progress) {
|
||||
if (!handle || !remote || !local || !*remote || !*local ||
|
||||
std::strlen(remote) > 1024 || std::strlen(local) > 4096) return -1;
|
||||
try {
|
||||
if (!handle->camera->IsConnected() || handle->camera->CaptureCurrentStatus()) return -1;
|
||||
const auto files = handle->camera->GetCameraFilesList();
|
||||
if (files.size() > 100000 || !contains(files, std::string(remote))) return -1;
|
||||
return handle->camera->DownloadCameraFile(remote, local, [progress](int64_t downloaded, int64_t total) {
|
||||
if (progress) progress(downloaded, total);
|
||||
}) ? 1 : 0;
|
||||
} catch (...) { return 0; }
|
||||
}
|
||||
void mc_x4_cancel_download(mc_x4* handle) {
|
||||
try { if (handle) handle->camera->CancelDownload(); } catch (...) {}
|
||||
}
|
||||
int mc_x4_read_video(mc_x4* handle, mc_x4_video* info, uint8_t* buffer, size_t capacity) {
|
||||
if (!handle || !info || !buffer) return -1;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user