Feature/ndi out node #1

Merged
itten merged 15 commits from feature/ndi-out-node into main 2026-07-01 18:22:26 +03:00
3 changed files with 60 additions and 34 deletions
Showing only changes of commit 7a32fa20af - Show all commits
+10 -6
View File
@@ -10,15 +10,15 @@
class NDIInNode : public dmf::NodeBase { class NDIInNode : public dmf::NodeBase {
void run() override { void run() override {
const uint32_t source_num = config().value("source_num", 0);
dmf::NDIHelper ndi_helper; dmf::NDIHelper ndi_helper;
try { try {
std::vector<std::string> ndi_sources; std::vector<std::string> ndi_sources;
ndi_helper.find_sources(&ndi_sources, 5000); ndi_helper.find_sources(&ndi_sources, 5000);
log("Available NDI sources:"); log("Available NDI sources:");
for (const auto& source_name : ndi_sources){ for (const auto& source_name : ndi_sources)
log("%s", source_name.c_str()); log(" %s", source_name.c_str());
}
uint32_t source_num = 0;
ndi_helper.select_source(source_num); ndi_helper.select_source(source_num);
ndi_helper.get_source_info(source_num); ndi_helper.get_source_info(source_num);
} catch (const std::runtime_error& e) { } catch (const std::runtime_error& e) {
@@ -63,8 +63,12 @@ class NDIInNode : public dmf::NodeBase {
bool last_ndi_frame_valid = false; bool last_ndi_frame_valid = false;
while (dmf::g_running.load(std::memory_order_relaxed)) { while (dmf::g_running.load(std::memory_order_relaxed)) {
if (ndi_helper.getV210_video_frame(0, latest_buffer, stride)) { try {
last_ndi_frame_valid = true; if (ndi_helper.getV210_video_frame(source_num, latest_buffer, stride))
last_ndi_frame_valid = true;
} catch (const std::runtime_error& e) {
log("NDI error: %s — stopping", e.what());
break;
} }
uint8_t* buf = nullptr; uint8_t* buf = nullptr;
+22 -12
View File
@@ -1,3 +1,4 @@
#include <chrono>
#include <string> #include <string>
#include <mxl/flow.h> #include <mxl/flow.h>
#include <mxl/time.h> #include <mxl/time.h>
@@ -38,12 +39,12 @@ class NDIOutNode : public dmf::NodeBase {
const mxlRational rate = {fps_num, fps_den}; const mxlRational rate = {fps_num, fps_den};
uint64_t index = mxlGetCurrentIndex(&rate); uint64_t index = mxlGetCurrentIndex(&rate);
uint64_t frame_count = 0; uint64_t frame_count = 0;
uint64_t invalid_count = 0; uint64_t invalid_count = 0;
uint64_t late_count = 0; uint64_t late_count = 0;
auto wall_start = std::chrono::steady_clock::now(); auto wall_start = std::chrono::steady_clock::now();
auto last_log_time = wall_start; auto last_log_time = wall_start;
// NDI part // NDI part
if (!NDIlib_initialize()) { if (!NDIlib_initialize()) {
@@ -73,9 +74,10 @@ class NDIOutNode : public dmf::NodeBase {
NDI_video_frame_10bit.p_data = (uint8_t*)malloc(NDI_video_frame_10bit.line_stride_in_bytes * NDI_video_frame_10bit.yres); NDI_video_frame_10bit.p_data = (uint8_t*)malloc(NDI_video_frame_10bit.line_stride_in_bytes * NDI_video_frame_10bit.yres);
NDIlib_video_frame_v2_t NDI_video_frame_16bit; NDIlib_video_frame_v2_t NDI_video_frame_16bit;
NDI_video_frame_16bit.xres = NDI_video_frame_10bit.xres; NDI_video_frame_16bit.xres = NDI_video_frame_10bit.xres;
NDI_video_frame_16bit.yres = NDI_video_frame_10bit.yres; NDI_video_frame_16bit.yres = NDI_video_frame_10bit.yres;
NDI_video_frame_16bit.frame_rate_N = fps_num;
NDI_video_frame_16bit.frame_rate_D = fps_den;
NDI_video_frame_16bit.line_stride_in_bytes = NDI_video_frame_16bit.xres * sizeof(uint16_t); NDI_video_frame_16bit.line_stride_in_bytes = NDI_video_frame_16bit.xres * sizeof(uint16_t);
NDI_video_frame_16bit.p_data = (uint8_t*)malloc(NDI_video_frame_16bit.line_stride_in_bytes * 2 * NDI_video_frame_16bit.yres); NDI_video_frame_16bit.p_data = (uint8_t*)malloc(NDI_video_frame_16bit.line_stride_in_bytes * 2 * NDI_video_frame_16bit.yres);
// //
@@ -98,9 +100,8 @@ class NDIOutNode : public dmf::NodeBase {
NDIlib_util_V210_to_P216(&NDI_video_frame_10bit, &NDI_video_frame_16bit); NDIlib_util_V210_to_P216(&NDI_video_frame_10bit, &NDI_video_frame_16bit);
NDIlib_send_send_video_v2(pNDI_send, &NDI_video_frame_16bit); NDIlib_send_send_video_v2(pNDI_send, &NDI_video_frame_16bit);
ndi_frame_counter++; ndi_frame_counter++;
if (ndi_frame_counter == 1) { if (ndi_frame_counter == 1)
log("NDI reciever got feed"); log("NDI receiver connected");
}
} else if (st == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) { } else if (st == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) {
mxlSleepForNs(1'000'000); // 1 ms poll mxlSleepForNs(1'000'000); // 1 ms poll
@@ -114,6 +115,15 @@ class NDIOutNode : public dmf::NodeBase {
log("unexpected status=%d on index=%llu", st, index); log("unexpected status=%d on index=%llu", st, index);
break; break;
} }
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration<double>(now - last_log_time).count() >= 1.0) {
const double elapsed = std::chrono::duration<double>(now - wall_start).count();
log("frames=%llu invalid=%llu late=%llu avg=%.2f fps",
frame_count, invalid_count, late_count,
static_cast<double>(frame_count) / elapsed);
last_log_time = now;
}
} }
log("stopped — total frames=%llu invalid=%llu late=%llu", log("stopped — total frames=%llu invalid=%llu late=%llu",
+28 -16
View File
@@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <Processing.NDI.Lib.h> #include <Processing.NDI.Lib.h>
#include "Signal.hpp"
#include "V210.hpp" #include "V210.hpp"
namespace dmf { namespace dmf {
@@ -35,6 +36,11 @@ class NDIHelper {
uint32_t sources_amount = 0; uint32_t sources_amount = 0;
const NDIlib_source_t* p_sources = nullptr; const NDIlib_source_t* p_sources = nullptr;
for (int attempt = 0; !sources_amount && attempt < max_attempts; ++attempt) { for (int attempt = 0; !sources_amount && attempt < max_attempts; ++attempt) {
if (!dmf::g_running.load(std::memory_order_relaxed)) {
NDIlib_find_destroy(pNDI_find);
pNDI_find = nullptr;
throw std::runtime_error("Interrupted while searching for NDI sources");
}
NDIlib_find_wait_for_sources(pNDI_find, timeout_ms); NDIlib_find_wait_for_sources(pNDI_find, timeout_ms);
p_sources = NDIlib_find_get_current_sources(pNDI_find, &sources_amount); p_sources = NDIlib_find_get_current_sources(pNDI_find, &sources_amount);
} }
@@ -68,7 +74,7 @@ class NDIHelper {
pNDI_recv = NDIlib_recv_create_v3(); pNDI_recv = NDIlib_recv_create_v3();
if (!pNDI_recv) { if (!pNDI_recv) {
throw std::runtime_error("Cannot create NDI recieve instance"); throw std::runtime_error("Cannot create NDI receive instance");
} }
NDIlib_recv_connect(pNDI_recv, &cached_sources[source_num]); NDIlib_recv_connect(pNDI_recv, &cached_sources[source_num]);
} }
@@ -154,28 +160,34 @@ class NDIHelper {
NDIlib_video_frame_v2_t video_frame; NDIlib_video_frame_v2_t video_frame;
NDIlib_frame_type_e frame_type; NDIlib_frame_type_e frame_type;
frame_type = NDIlib_recv_capture_v3(pNDI_recv, &video_frame, nullptr, nullptr, 5); frame_type = NDIlib_recv_capture_v3(pNDI_recv, &video_frame, nullptr, nullptr, 5);
switch(frame_type)
{
case NDIlib_frame_type_error:
throw std::runtime_error("NDI source lost");
case NDIlib_frame_type_status_change:
throw std::runtime_error("NDI source resolution or framerate are changed");
} if (frame_type == NDIlib_frame_type_error)
if (frame_type != NDIlib_frame_type_video) { throw std::runtime_error("NDI source lost");
if (frame_type == NDIlib_frame_type_status_change) {
// Source changed resolution or framerate — refresh internal info, repeat last frame
get_source_info(source_num);
return false; return false;
} }
switch(fourCC)
{ if (frame_type != NDIlib_frame_type_video)
return false;
switch (fourCC) {
case NDIlib_FourCC_type_UYVY: case NDIlib_FourCC_type_UYVY:
v210::UYVYtoV210(video_frame.p_data, frame_buffer, xres, yres, stride, frame_stride); v210::UYVYtoV210(video_frame.p_data, frame_buffer, xres, yres, stride, frame_stride);
break; break;
case NDIlib_FourCC_type_P216: case NDIlib_FourCC_type_P216: {
NDIlib_video_frame_v2_t video_frame_10bit; // P216→V210: write directly into caller's buffer via a wrapper frame
NDIlib_util_P216_to_V210(&video_frame, &video_frame_10bit); NDIlib_video_frame_v2_t dst{};
frame_buffer = video_frame.p_data; dst.p_data = frame_buffer;
dst.line_stride_in_bytes = frame_stride;
NDIlib_util_P216_to_V210(&video_frame, &dst);
break;
}
default: default:
throw std::runtime_error("Color format is not supported yet"); NDIlib_recv_free_video_v2(pNDI_recv, &video_frame);
throw std::runtime_error("Unsupported NDI color format: " + fourCCtoStr());
} }
NDIlib_recv_free_video_v2(pNDI_recv, &video_frame); NDIlib_recv_free_video_v2(pNDI_recv, &video_frame);
return true; return true;