Files
dmf-studio-rnd/nodes/ndiin/main.cpp
T
JohannesItten 12c87db4e2 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>
2026-07-01 12:13:36 +03:00

106 lines
3.6 KiB
C++

#include <cstring>
#include <string>
#include <vector>
#include <mxl/flow.h>
#include <mxl/time.h>
#include "NodeBase.hpp"
#include "FlowDef.hpp"
#include "NDIReceiver.hpp"
class NDIInNode : public dmf::NodeBase {
void run() override {
const auto source_num = static_cast<uint32_t>(config().value("source_num", 0));
dmf::NDIReceiver ndi;
dmf::NDIReceiver::SourceInfo src;
try {
auto sources = ndi.find_sources(5000);
log("Available NDI sources:");
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", 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);
const std::string flow_def =
dmf::make_video_flow_def(flow_id, node_id(), width, height, fps_num, fps_den);
mxlFlowWriter writer{};
mxlFlowConfigInfo cfg_info{};
bool created = false;
mxlStatus st = mxlCreateFlowWriter(
instance(), flow_def.c_str(), nullptr, &writer, &cfg_info, &created);
if (st != MXL_STATUS_OK) {
log("mxlCreateFlowWriter failed (status=%d)", st);
return;
}
const uint32_t stride = cfg_info.discrete.sliceSizes[0];
log("stride=%u B/line grain=%u B ring=%u grains",
stride, stride * static_cast<uint32_t>(height), cfg_info.discrete.grainCount);
const mxlRational rate = {fps_num, fps_den};
uint64_t index = mxlGetCurrentIndex(&rate);
log("start index=%llu", index);
// 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.capture_v210(latest_frame.data(), stride))
have_frame = true;
} catch (const std::runtime_error& e) {
log("NDI error: %s — stopping", e.what());
break;
}
mxlGrainInfo grain{};
uint8_t* buf = nullptr;
st = mxlFlowWriterOpenGrain(writer, index, &grain, &buf);
if (st != MXL_STATUS_OK) {
log("OpenGrain failed (status=%d), skipping index=%llu", st, index);
index++;
continue;
}
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;
mxlFlowWriterCommitGrain(writer, &grain);
const uint64_t ns = mxlGetNsUntilIndex(index + 1, &rate);
if (ns > 0 && ns < 2'000'000'000ULL) mxlSleepForNs(ns);
index++;
}
log("stopped at index=%llu", index);
mxlReleaseFlowWriter(instance(), writer);
}
};
int main() {
NDIInNode node;
return node.execute();
}