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
8 changed files with 445 additions and 3 deletions
Showing only changes of commit 51a1b1b2c8 - Show all commits
+17
View File
@@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${HOME}/SDK/NDI/include"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
+7
View File
@@ -80,4 +80,11 @@ target_link_libraries(dmf-shared INTERFACE mxl nlohmann_json::nlohmann_json)
add_subdirectory(nodes/testpattern)
add_subdirectory(nodes/fakesink)
set(NDI_SDK_DIR "" CACHE PATH "Path to NDI SDK root")
if(NDI_SDK_DIR)
add_subdirectory(nodes/ndiout)
add_subdirectory(nodes/ndiin)
endif()
add_subdirectory(studio-manager)
+11
View File
@@ -0,0 +1,11 @@
add_executable(dmf-node-ndiin main.cpp)
target_compile_features(dmf-node-ndiin PRIVATE cxx_std_20)
target_link_libraries(dmf-node-ndiin PRIVATE dmf-shared)
install(TARGETS dmf-node-ndiin RUNTIME DESTINATION bin)
set(NDI_INCLUDE "${NDI_SDK_DIR}/include")
target_include_directories(dmf-node-ndiin PRIVATE
"${NDI_INCLUDE}"
)
find_library(NDI_LIB NAMES ndi PATHS "${NDI_SDK_DIR}/lib/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu" "${NDI_SDK_DIR}/lib/x64" "${NDI_SDK_DIR}/lib" NO_DEFAULT_PATH)
target_link_libraries(dmf-node-ndiin PRIVATE ${NDI_LIB})
+192
View File
@@ -0,0 +1,192 @@
#include <string>
#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>
class NDIInNode : public dmf::NodeBase {
void run() override {
try {
dmf::NDIHelper ndi_helper;
} catch (const std::runtime_error& e) {
log("Error: %s", e.what());
return;
}
// Create a finder
NDIlib_find_instance_t pNDI_find = NDIlib_find_create_v2();
if (!pNDI_find) {
log("Cannot create NDI find instance");
return;
}
// Wait until there is one source
uint32_t NDI_sources_amount = 0;
const NDIlib_source_t* p_sources = NULL;
while (!NDI_sources_amount) {
// Wait until the sources on the network have changed
log("Looking for NDI sources ...");
NDIlib_find_wait_for_sources(pNDI_find, 1000/* One second */);
p_sources = NDIlib_find_get_current_sources(pNDI_find, &NDI_sources_amount);
}
log("Found %i NDI sources:", NDI_sources_amount);
for (int i = 0; i < NDI_sources_amount; ++i){
log("%i) %s (%s)", i + 1, p_sources[i].p_ndi_name, p_sources[i].p_url_address);
}
NDIlib_recv_instance_t pNDI_recv = NDIlib_recv_create_v3();
if (!pNDI_recv) {
log("Cannot create NDI recieve instance");
return;
}
NDIlib_recv_connect(pNDI_recv, p_sources + 0);
// Destroy the NDI finder. We needed to have access to the pointers to p_sources[0]
NDIlib_find_destroy(pNDI_find);
// Recieve first frame to get info about NDI source data
NDIlib_video_frame_v2_t NDI_video_frame;
NDIlib_frame_type_e NDI_frame_type =
NDIlib_recv_capture_v2(pNDI_recv, &NDI_video_frame, nullptr, nullptr, 1000);
if (NDI_frame_type == NDIlib_frame_type_none) {
log("Can't recieve NDI frame data");
NDIlib_recv_destroy(pNDI_recv);
NDIlib_destroy();
return;
} else if (NDI_frame_type == NDIlib_frame_type_audio) {
log("Audio still not supported");
NDIlib_recv_destroy(pNDI_recv);
NDIlib_destroy();
return;
}
log("NDI params: %i %i", NDI_video_frame.frame_rate_N, NDI_video_frame.frame_rate_D);
char fourcc_str[5];
uint32_t fourcc = (uint32_t)NDI_video_frame.FourCC;
fourcc_str[0] = (fourcc >> 0) & 0xFF;
fourcc_str[1] = (fourcc >> 8) & 0xFF;
fourcc_str[2] = (fourcc >> 16) & 0xFF;
fourcc_str[3] = (fourcc >> 24) & 0xFF;
fourcc_str[4] = '\0';
log("NDI fourCC=%s (0x%08x)", fourcc_str, fourcc);
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_video_frame.xres);
const int height = flow_info.value("height", NDI_video_frame.yres);
// const int fps_num = flow_info.value("fps_num", NDI_video_frame.frame_rate_N);
// const int fps_den = flow_info.value("fps_den", NDI_video_frame.frame_rate_D);
const int fps_num = flow_info.value("fps_num", 25);
const int fps_den = flow_info.value("fps_den", 1);
const uint32_t ndi_stride = NDI_video_frame.line_stride_in_bytes > 0
? static_cast<uint32_t>(NDI_video_frame.line_stride_in_bytes)
: static_cast<uint32_t>(width * 2);
NDIlib_recv_free_video_v2(pNDI_recv, &NDI_video_frame);
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);
// in case of P216
NDIlib_video_frame_v2_t NDI_video_frame_10bit;
NDI_video_frame_10bit.xres = width;
NDI_video_frame_10bit.yres = height;
NDI_video_frame_10bit.FourCC = (NDIlib_FourCC_video_type_e)NDI_LIB_FOURCC('V', '2', '1', '0');
NDI_video_frame_10bit.line_stride_in_bytes = stride;
// for UYVY and 1 FPS ndi static frames
const size_t frame_bytes = ndi_stride * height;
uint8_t* latest_buffer = (uint8_t*)malloc(frame_bytes);
bool last_ndi_frame_valid = false;
while (dmf::g_running.load(std::memory_order_relaxed)) {
if (NDIlib_recv_capture_v2(pNDI_recv, &NDI_video_frame, nullptr, nullptr, 5) == NDIlib_frame_type_video) {
memcpy(latest_buffer, NDI_video_frame.p_data, frame_bytes);
NDIlib_recv_free_video_v2(pNDI_recv, &NDI_video_frame);
last_ndi_frame_valid = true;
}
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 (last_ndi_frame_valid) {
log("stride: %i ndi_stride: %i", stride, ndi_stride);
// UYVY -> v210
const uint8_t* src = latest_buffer;
uint8_t* dst = buf;
const int blocks = width / 6;
for (int y = 0; y < height; y++) {
for (int b = 0; b < blocks; b++) {
const uint8_t* mp = src + b * 12; // 3 macropixels = 12 bytes
// mp[0]=U0, mp[1]=Y0, mp[2]=V0, mp[3]=Y1
// mp[4]=U1, mp[5]=Y2, mp[6]=V1, mp[7]=Y3
// mp[8]=U2, mp[9]=Y4, mp[10]=V2, mp[11]=Y5
dmf::v210::pack_block(dst + b * 16,
{0, (uint16_t)(mp[0]<<2), (uint16_t)(mp[2]<<2)}, (uint16_t)(mp[1]<<2), (uint16_t)(mp[3]<<2),
{0, (uint16_t)(mp[4]<<2), (uint16_t)(mp[6]<<2)}, (uint16_t)(mp[5]<<2), (uint16_t)(mp[7]<<2),
{0, (uint16_t)(mp[8]<<2), (uint16_t)(mp[10]<<2)}, (uint16_t)(mp[9]<<2), (uint16_t)(mp[11]<<2)
);
}
src += ndi_stride;
dst += stride;
}
grain.flags = 0;
} else {
grain.flags = MXL_GRAIN_FLAG_INVALID;
}
grain.validSlices = grain.totalSlices; // mark grain complete so readers can consume it
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);
free(latest_buffer);
mxlReleaseFlowWriter(instance(), writer);
NDIlib_recv_destroy(pNDI_recv);
NDIlib_destroy();
}
};
int main()
{
NDIInNode node;
return node.execute();
}
+11
View File
@@ -0,0 +1,11 @@
add_executable(dmf-node-ndiout main.cpp)
target_compile_features(dmf-node-ndiout PRIVATE cxx_std_20)
target_link_libraries(dmf-node-ndiout PRIVATE dmf-shared)
install(TARGETS dmf-node-ndiout RUNTIME DESTINATION bin)
set(NDI_INCLUDE "${NDI_SDK_DIR}/include")
target_include_directories(dmf-node-ndiout PRIVATE
"${NDI_INCLUDE}"
)
find_library(NDI_LIB NAMES ndi PATHS "${NDI_SDK_DIR}/lib/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu" "${NDI_SDK_DIR}/lib/x64" "${NDI_SDK_DIR}/lib" NO_DEFAULT_PATH)
target_link_libraries(dmf-node-ndiout PRIVATE ${NDI_LIB})
+132
View File
@@ -0,0 +1,132 @@
#include <string>
#include <mxl/flow.h>
#include <mxl/time.h>
#include "NodeBase.hpp"
#include "FlowDef.hpp"
#include "V210.hpp"
#include <Processing.NDI.Lib.h>
class NDIOutNode : public dmf::NodeBase {
void run() override {
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", 1920);
const int height = flow_info.value("height", 1080);
const int fps_num = flow_info.value("fps_num", 25);
const int fps_den = flow_info.value("fps_den", 1);
log("flow=%s", flow_id.c_str());
log("waiting for flow to become active...");
bool active = false;
while (!active && dmf::g_running.load(std::memory_order_relaxed)) {
mxlIsFlowActive(instance(), flow_id.c_str(), &active);
if (!active) mxlSleepForNs(100'000'000);
}
if (!dmf::g_running) return;
log("flow active — starting read");
mxlFlowReader reader{};
mxlStatus st = mxlCreateFlowReader(instance(), flow_id.c_str(), nullptr, &reader);
if (st != MXL_STATUS_OK) {
log("mxlCreateFlowReader failed (status=%d)", st);
return;
}
mxlFlowConfigInfo cfg_info{};
mxlFlowReaderGetConfigInfo(reader, &cfg_info);
const uint32_t mxl_stride = cfg_info.discrete.sliceSizes[0];
const mxlRational rate = {fps_num, fps_den};
uint64_t index = mxlGetCurrentIndex(&rate);
uint64_t frame_count = 0;
uint64_t invalid_count = 0;
uint64_t late_count = 0;
auto wall_start = std::chrono::steady_clock::now();
auto last_log_time = wall_start;
// NDI part
if (!NDIlib_initialize()) {
// Cannot run NDI. Most likely because the CPU is not sufficient (see SDK documentation).
log("Cannot run NDI");
if (!NDIlib_is_supported_CPU()) {
log("CPU is not sufficient for NDI");
}
return;
}
NDIlib_send_create_t NDI_send_create_desc;
NDI_send_create_desc.p_ndi_name = flow_id.c_str();
NDIlib_send_instance_t pNDI_send = NDIlib_send_create(&NDI_send_create_desc);
if (!pNDI_send) {
log("Cannot create NDI send instance");
return;
}
NDIlib_video_frame_v2_t NDI_video_frame_10bit;
NDI_video_frame_10bit.xres = width;
NDI_video_frame_10bit.yres = height;
NDI_video_frame_10bit.FourCC = (NDIlib_FourCC_video_type_e)NDI_LIB_FOURCC('V', '2', '1', '0');
NDI_video_frame_10bit.line_stride_in_bytes = mxl_stride;
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;
NDI_video_frame_16bit.xres = NDI_video_frame_10bit.xres;
NDI_video_frame_16bit.yres = NDI_video_frame_10bit.yres;
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);
//
uint64_t ndi_frame_counter = 0;
while (dmf::g_running.load(std::memory_order_relaxed)) {
mxlGrainInfo grain{};
uint8_t* buf = nullptr;
st = mxlFlowReaderGetGrainNonBlocking(reader, index, &grain, &buf);
if (st == MXL_STATUS_OK) {
frame_count++;
if (grain.flags & MXL_GRAIN_FLAG_INVALID) invalid_count++;
index++;
if (NDIlib_send_get_no_connections(pNDI_send,0) == 0) {
ndi_frame_counter = 0;
continue;
}
memcpy(NDI_video_frame_10bit.p_data, buf, mxl_stride * height);
NDIlib_util_V210_to_P216(&NDI_video_frame_10bit, &NDI_video_frame_16bit);
NDIlib_send_send_video_v2(pNDI_send, &NDI_video_frame_16bit);
ndi_frame_counter++;
if (ndi_frame_counter == 1) {
log("NDI reciever got feed");
}
} else if (st == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) {
mxlSleepForNs(1'000'000); // 1 ms poll
} else if (st == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
late_count++;
// Jump to the most recent frame in the ring buffer
mxlFlowRuntimeInfo ri{};
mxlFlowReaderGetRuntimeInfo(reader, &ri);
index = ri.headIndex;
} else {
log("unexpected status=%d on index=%llu", st, index);
break;
}
}
log("stopped — total frames=%llu invalid=%llu late=%llu",
frame_count, invalid_count, late_count);
mxlReleaseFlowReader(instance(), reader);
free(NDI_video_frame_10bit.p_data);
free(NDI_video_frame_16bit.p_data);
NDIlib_send_destroy(pNDI_send);
NDIlib_destroy();
}
};
int main() {
NDIOutNode node;
return node.execute();
}
+72
View File
@@ -0,0 +1,72 @@
#include <stdexcept>
#include <vector>
#include <string>
#include <chrono>
#include <Processing.NDI.Lib.h>
namespace dmf {
class NDIHelper {
public:
NDIHelper() {
if (!NDIlib_initialize()) {
throw std::runtime_error("NDI lib init failed");
if (!NDIlib_is_supported_CPU()) {
throw std::runtime_error("CPU is not sufficient for NDI");
}
}
}
~NDIHelper() {
NDIlib_destroy();
NDIlib_recv_destroy(pNDI_recv);
}
void find_sources(std::vector<std::string>* sources, u_int32_t timeout_ms) {
pNDI_find = NDIlib_find_create_v2();
if (!pNDI_find) {
throw std::runtime_error("Cannot create NDI finder");
}
while (!sources_amount) {
NDIlib_find_wait_for_sources(pNDI_find, timeout_ms);
p_sources = NDIlib_find_get_current_sources(pNDI_find, &sources_amount);
}
for (int i = 0; i < sources_amount; ++i){
sources->push_back(p_sources[i].p_ndi_name);
}
NDIlib_find_destroy(pNDI_find);
}
void select_source(uint32_t source_num) {
if (sources_amount == 0) {
throw std::runtime_error("0 sources found");
} else if (source_num > sources_amount) {
throw std::runtime_error("Source_num bigger that sources amount");
}
pNDI_recv = NDIlib_recv_create_v3();
if (!pNDI_recv) {
NDIlib_recv_destroy(pNDI_recv);
throw std::runtime_error("Cannot create NDI recieve instance");
}
NDIlib_recv_connect(pNDI_recv, p_sources + source_num);
}
private:
// receive
NDIlib_find_instance_t pNDI_find = nullptr;
uint32_t sources_amount = 0;
const NDIlib_source_t* p_sources = NULL;
NDIlib_recv_instance_t pNDI_recv = nullptr;
void get_source_info(uint32_t source_num) {
NDIlib_video_frame_v2_t video_frame;
uint8_t frames = 0;
while (frames < 2) {
NDIlib_recv_capture_v2(pNDI_recv, &video_frame, nullptr, nullptr, 1000);
NDIlib_recv_free_video_v2(pNDI_recv, &video_frame);
}
}
};
}
+2 -2
View File
@@ -39,11 +39,11 @@ static std::string gen_uuid() {
static dmf::FlowGraph build_graph() {
dmf::FlowGraph g;
g.nodes = {
{ "testpattern", "testpattern", {} },
{ "ndiin", "ndiin", {} },
{ "fakesink", "fakesink", {}},
};
g.edges = {
{ gen_uuid(), "testpattern", "flow_id", "fakesink", "flow_id",
{ gen_uuid(), "ndiin", "flow_id", "fakesink", "flow_id",
{ {"kind","video"}, {"width",1920}, {"height",1080}, {"fps_num",25}, {"fps_den",1} } },
};
return g;