refactor: NDIHelper → NDIReceiver with cleaner API and naming

NDIReceiver (shared/NDIReceiver.hpp):
- Rename class NDIHelper → NDIReceiver, file NDIHelper.hpp → NDIReceiver.hpp
- Add SourceInfo struct (width, height, fps_num, fps_den, stride, fourcc)
  replacing raw public member variables (xres, yres, frame_N, frame_D, stride)
- find_sources() now returns std::vector<std::string> instead of output pointer
- select_source() + get_source_info() → connect() + probe() (cleaner sequence,
  probe() returns SourceInfo and stores it internally for capture_v210)
- getV210_video_frame() → capture_v210() — removes unused source_num parameter
- get_bytes_per_pixel, fourCCtoStr → private static bytes_per_pixel, fourcc_str
- u_int32_t → uint32_t; (uint32_t) casts → static_cast
- probe() checks g_running to avoid hanging if source never sends video

ndiin/main.cpp:
- Update to new NDIReceiver API
- malloc/free latest_buffer → std::vector<uint8_t> latest_frame
- Remove unused #include <Processing.NDI.Lib.h> and V210.hpp
- memcpy uses latest_frame.size() instead of separate frame_bytes variable

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
JohannesItten
2026-07-01 12:13:36 +03:00
parent 7a32fa20af
commit 12c87db4e2
3 changed files with 219 additions and 238 deletions
+28 -32
View File
@@ -1,37 +1,36 @@
#include <cstring>
#include <string>
#include <vector>
#include <mxl/flow.h>
#include <mxl/time.h>
#include "NodeBase.hpp"
#include "FlowDef.hpp"
#include "V210.hpp"
#include "NDIHelper.hpp"
#include <Processing.NDI.Lib.h>
#include <cstring>
#include "NDIReceiver.hpp"
class NDIInNode : public dmf::NodeBase {
void run() override {
const uint32_t source_num = config().value("source_num", 0);
const auto source_num = static_cast<uint32_t>(config().value("source_num", 0));
dmf::NDIHelper ndi_helper;
dmf::NDIReceiver ndi;
dmf::NDIReceiver::SourceInfo src;
try {
std::vector<std::string> ndi_sources;
ndi_helper.find_sources(&ndi_sources, 5000);
auto sources = ndi.find_sources(5000);
log("Available NDI sources:");
for (const auto& source_name : ndi_sources)
log(" %s", source_name.c_str());
ndi_helper.select_source(source_num);
ndi_helper.get_source_info(source_num);
for (const auto& name : sources)
log(" %s", name.c_str());
ndi.connect(source_num);
src = ndi.probe();
} catch (const std::runtime_error& e) {
log("Error: %s", e.what());
return;
}
const auto flow_info = config().at("flow_id");
const auto flow_id = flow_info.at("id").get<std::string>();
const int width = flow_info.value("width", ndi_helper.xres);
const int height = flow_info.value("height", ndi_helper.yres);
const int fps_num = flow_info.value("fps_num", ndi_helper.frame_N);
const int fps_den = flow_info.value("fps_den", ndi_helper.frame_D);
const int width = flow_info.value("width", src.width);
const int height = flow_info.value("height", src.height);
const int fps_num = flow_info.value("fps_num", src.fps_num);
const int fps_den = flow_info.value("fps_den", src.fps_den);
log("flow=%s %dx%d @ %d/%d fps", flow_id.c_str(), width, height, fps_num, fps_den);
@@ -56,23 +55,22 @@ class NDIInNode : public dmf::NodeBase {
const mxlRational rate = {fps_num, fps_den};
uint64_t index = mxlGetCurrentIndex(&rate);
log("start index=%llu", index);
// because NDI can drop to 1 FPS for static frames, even if source is 29.97p
const size_t frame_bytes = stride * height;
uint8_t* latest_buffer = (uint8_t*)malloc(frame_bytes);
bool last_ndi_frame_valid = false;
// NDI can drop to 1fps for static content — hold last valid frame
std::vector<uint8_t> latest_frame(stride * height);
bool have_frame = false;
while (dmf::g_running.load(std::memory_order_relaxed)) {
try {
if (ndi_helper.getV210_video_frame(source_num, latest_buffer, stride))
last_ndi_frame_valid = true;
if (ndi.capture_v210(latest_frame.data(), stride))
have_frame = true;
} catch (const std::runtime_error& e) {
log("NDI error: %s — stopping", e.what());
break;
}
uint8_t* buf = nullptr;
mxlGrainInfo grain{};
uint8_t* buf = nullptr;
st = mxlFlowWriterOpenGrain(writer, index, &grain, &buf);
if (st != MXL_STATUS_OK) {
@@ -81,14 +79,14 @@ class NDIInNode : public dmf::NodeBase {
continue;
}
if (last_ndi_frame_valid) {
std::memcpy(buf, latest_buffer, frame_bytes);
if (have_frame) {
std::memcpy(buf, latest_frame.data(), latest_frame.size());
grain.flags = 0;
} else {
grain.flags = MXL_GRAIN_FLAG_INVALID;
}
grain.validSlices = grain.totalSlices; // mark grain complete so readers can consume it
grain.validSlices = grain.totalSlices;
mxlFlowWriterCommitGrain(writer, &grain);
const uint64_t ns = mxlGetNsUntilIndex(index + 1, &rate);
@@ -97,13 +95,11 @@ class NDIInNode : public dmf::NodeBase {
}
log("stopped at index=%llu", index);
free(latest_buffer);
mxlReleaseFlowWriter(instance(), writer);
}
};
int main()
{
int main() {
NDIInNode node;
return node.execute();
}
}