Feature/ndi out node #2
Vendored
+1
@@ -4,6 +4,7 @@
|
|||||||
"name": "Linux",
|
"name": "Linux",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"${workspaceFolder}/**",
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/shared",
|
||||||
"${HOME}/SDK/NDI/include"
|
"${HOME}/SDK/NDI/include"
|
||||||
],
|
],
|
||||||
"defines": [],
|
"defines": [],
|
||||||
|
|||||||
+99
-23
@@ -53,15 +53,15 @@ class NDIOutNode : public dmf::NodeBase {
|
|||||||
if (!dmf::g_running) return;
|
if (!dmf::g_running) return;
|
||||||
log("flow active — starting read");
|
log("flow active — starting read");
|
||||||
|
|
||||||
mxlFlowReader reader{};
|
mxlFlowReader video_reader{};
|
||||||
mxlStatus st = mxlCreateFlowReader(instance(), flow_id.c_str(), nullptr, &reader);
|
mxlStatus vst = mxlCreateFlowReader(instance(), flow_id.c_str(), nullptr, &video_reader);
|
||||||
if (st != MXL_STATUS_OK) {
|
if (vst != MXL_STATUS_OK) {
|
||||||
log("mxlCreateFlowReader failed (status=%d)", st);
|
log("mxlCreateFlowReader failed (status=%d)", vst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mxlFlowConfigInfo cfg_info{};
|
mxlFlowConfigInfo video_cfg{};
|
||||||
mxlFlowReaderGetConfigInfo(reader, &cfg_info);
|
mxlFlowReaderGetConfigInfo(video_reader, &video_cfg);
|
||||||
const uint32_t mxl_stride = cfg_info.discrete.sliceSizes[0];
|
const uint32_t mxl_stride = video_cfg.discrete.sliceSizes[0];
|
||||||
|
|
||||||
NDIContext ndi(flow_id.c_str());
|
NDIContext ndi(flow_id.c_str());
|
||||||
|
|
||||||
@@ -72,6 +72,8 @@ class NDIOutNode : public dmf::NodeBase {
|
|||||||
NDIlib_video_frame_v2_t ndi_frame_10bit{};
|
NDIlib_video_frame_v2_t ndi_frame_10bit{};
|
||||||
ndi_frame_10bit.xres = width;
|
ndi_frame_10bit.xres = width;
|
||||||
ndi_frame_10bit.yres = height;
|
ndi_frame_10bit.yres = height;
|
||||||
|
ndi_frame_10bit.frame_rate_N = fps_num;
|
||||||
|
ndi_frame_10bit.frame_rate_D = fps_den;
|
||||||
ndi_frame_10bit.FourCC = static_cast<NDIlib_FourCC_video_type_e>(NDI_LIB_FOURCC('V','2','1','0'));
|
ndi_frame_10bit.FourCC = static_cast<NDIlib_FourCC_video_type_e>(NDI_LIB_FOURCC('V','2','1','0'));
|
||||||
ndi_frame_10bit.line_stride_in_bytes = mxl_stride;
|
ndi_frame_10bit.line_stride_in_bytes = mxl_stride;
|
||||||
ndi_frame_10bit.p_data = buf_10bit.data();
|
ndi_frame_10bit.p_data = buf_10bit.data();
|
||||||
@@ -84,8 +86,48 @@ class NDIOutNode : public dmf::NodeBase {
|
|||||||
ndi_frame_16bit.line_stride_in_bytes = width * static_cast<int>(sizeof(uint16_t));
|
ndi_frame_16bit.line_stride_in_bytes = width * static_cast<int>(sizeof(uint16_t));
|
||||||
ndi_frame_16bit.p_data = buf_16bit.data();
|
ndi_frame_16bit.p_data = buf_16bit.data();
|
||||||
|
|
||||||
const mxlRational rate = {fps_num, fps_den};
|
// audio
|
||||||
uint64_t index = mxlGetCurrentIndex(&rate);
|
mxlFlowReader audio_reader{};
|
||||||
|
mxlFlowConfigInfo audio_cfg{};
|
||||||
|
mxlStatus ast;
|
||||||
|
|
||||||
|
int sample_rate = 0;
|
||||||
|
int channels = 0;
|
||||||
|
int bit_depth = 32;
|
||||||
|
int no_samples = 0;
|
||||||
|
bool has_audio = config().contains("audio_flow_id");
|
||||||
|
|
||||||
|
if (has_audio)
|
||||||
|
{
|
||||||
|
const auto audio_flow_info = config().at("audio_flow_id");
|
||||||
|
const auto audio_flow_id = audio_flow_info.at("id").get<std::string>();
|
||||||
|
ast = mxlCreateFlowReader(instance(), audio_flow_id.c_str(), nullptr, &audio_reader);
|
||||||
|
if (ast != MXL_STATUS_OK) {
|
||||||
|
log("audio mxlCreateFlowReader failed (status=%d) — continuing without audio", ast);
|
||||||
|
has_audio = false;
|
||||||
|
} else {
|
||||||
|
mxlFlowReaderGetConfigInfo(audio_reader, &audio_cfg);
|
||||||
|
sample_rate = audio_flow_info.at("sample_rate").get<int>();
|
||||||
|
channels = audio_cfg.continuous.channelCount;
|
||||||
|
no_samples = sample_rate / fps_num;
|
||||||
|
log("audio channels: %i, samples: %i", channels, audio_cfg.continuous.bufferLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
NDIlib_audio_frame_v3_t ndi_audio_frame;
|
||||||
|
ndi_audio_frame.sample_rate = sample_rate;
|
||||||
|
ndi_audio_frame.no_channels = channels;
|
||||||
|
ndi_audio_frame.no_samples = no_samples;
|
||||||
|
ndi_audio_frame.FourCC = NDIlib_FourCC_audio_type_FLTP;
|
||||||
|
ndi_audio_frame.channel_stride_in_bytes = ndi_audio_frame.no_samples * sizeof(float);
|
||||||
|
|
||||||
|
// core loop
|
||||||
|
const mxlRational video_rate = {fps_num, fps_den};
|
||||||
|
const mxlRational audio_rate = {sample_rate, 1};
|
||||||
|
|
||||||
|
uint64_t video_index = mxlGetCurrentIndex(&video_rate);
|
||||||
|
uint64_t audio_index = has_audio ? mxlGetCurrentIndex(&audio_rate) : 0;
|
||||||
|
|
||||||
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;
|
||||||
@@ -93,19 +135,50 @@ class NDIOutNode : public dmf::NodeBase {
|
|||||||
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;
|
||||||
|
|
||||||
|
std::vector<float> audio_planar(static_cast<size_t>(channels) * no_samples);
|
||||||
|
ndi_audio_frame.p_data = reinterpret_cast<uint8_t*>(audio_planar.data());
|
||||||
|
|
||||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||||
mxlGrainInfo grain{};
|
mxlGrainInfo video_grain{};
|
||||||
uint8_t* buf = nullptr;
|
mxlGrainInfo audio_grain{};
|
||||||
|
uint8_t* video_buf = nullptr;
|
||||||
|
mxlWrappedMultiBufferSlice audio_slices;
|
||||||
|
|
||||||
st = mxlFlowReaderGetGrainNonBlocking(reader, index, &grain, &buf);
|
vst = mxlFlowReaderGetGrainNonBlocking(video_reader, video_index, &video_grain, &video_buf);
|
||||||
|
bool ndi_has_connections = NDIlib_send_get_no_connections(ndi.sender, 0) > 0;
|
||||||
|
|
||||||
if (st == MXL_STATUS_OK) {
|
if (has_audio) {
|
||||||
|
ast = mxlFlowReaderGetSamplesNonBlocking(audio_reader, audio_index, no_samples, &audio_slices);
|
||||||
|
if (ast == MXL_STATUS_OK) {
|
||||||
|
for (size_t c = 0; c < audio_slices.count; c++) {
|
||||||
|
float* dst = audio_planar.data() + c * 1920;
|
||||||
|
size_t frag0_samples = audio_slices.base.fragments[0].size / sizeof(float);
|
||||||
|
const uint8_t* src0 = static_cast<const uint8_t*>(audio_slices.base.fragments[0].pointer)
|
||||||
|
+ c * audio_slices.stride;
|
||||||
|
std::memcpy(dst, src0, frag0_samples * sizeof(float));
|
||||||
|
|
||||||
|
if (audio_slices.base.fragments[1].size > 0) {
|
||||||
|
size_t frag1_samples = audio_slices.base.fragments[1].size / sizeof(float);
|
||||||
|
const uint8_t* src1 = static_cast<const uint8_t*>(audio_slices.base.fragments[1].pointer)
|
||||||
|
+ c * audio_slices.stride;
|
||||||
|
std::memcpy(dst + frag0_samples, src1, frag1_samples * sizeof(float));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NDIlib_send_send_audio_v3(ndi.sender, &ndi_audio_frame);
|
||||||
|
audio_index += no_samples;
|
||||||
|
} else if (ast == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
|
||||||
|
mxlFlowRuntimeInfo ari{};
|
||||||
|
mxlFlowReaderGetRuntimeInfo(audio_reader, &ari);
|
||||||
|
audio_index = ari.headIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vst == MXL_STATUS_OK) {
|
||||||
frame_count++;
|
frame_count++;
|
||||||
if (grain.flags & MXL_GRAIN_FLAG_INVALID) invalid_count++;
|
if (video_grain.flags & MXL_GRAIN_FLAG_INVALID) invalid_count++;
|
||||||
index++;
|
|
||||||
|
|
||||||
if (NDIlib_send_get_no_connections(ndi.sender, 0) > 0) {
|
if (ndi_has_connections) {
|
||||||
std::memcpy(ndi_frame_10bit.p_data, buf, mxl_stride * height);
|
std::memcpy(ndi_frame_10bit.p_data, video_buf, mxl_stride * height);
|
||||||
NDIlib_util_V210_to_P216(&ndi_frame_10bit, &ndi_frame_16bit);
|
NDIlib_util_V210_to_P216(&ndi_frame_10bit, &ndi_frame_16bit);
|
||||||
NDIlib_send_send_video_v2(ndi.sender, &ndi_frame_16bit);
|
NDIlib_send_send_video_v2(ndi.sender, &ndi_frame_16bit);
|
||||||
if (++ndi_frame_count == 1)
|
if (++ndi_frame_count == 1)
|
||||||
@@ -114,17 +187,19 @@ class NDIOutNode : public dmf::NodeBase {
|
|||||||
ndi_frame_count = 0;
|
ndi_frame_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (st == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) {
|
video_index++;
|
||||||
|
|
||||||
|
} else if (vst == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) {
|
||||||
mxlSleepForNs(1'000'000);
|
mxlSleepForNs(1'000'000);
|
||||||
|
|
||||||
} else if (st == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
|
} else if (vst == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
|
||||||
late_count++;
|
late_count++;
|
||||||
mxlFlowRuntimeInfo ri{};
|
mxlFlowRuntimeInfo ri{};
|
||||||
mxlFlowReaderGetRuntimeInfo(reader, &ri);
|
mxlFlowReaderGetRuntimeInfo(video_reader, &ri);
|
||||||
index = ri.headIndex;
|
video_index = ri.headIndex;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log("unexpected status=%d on index=%llu", st, index);
|
log("unexpected status=%d on index=%llu", vst, video_index);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +215,8 @@ class NDIOutNode : public dmf::NodeBase {
|
|||||||
|
|
||||||
log("stopped — total frames=%llu invalid=%llu late=%llu",
|
log("stopped — total frames=%llu invalid=%llu late=%llu",
|
||||||
frame_count, invalid_count, late_count);
|
frame_count, invalid_count, late_count);
|
||||||
mxlReleaseFlowReader(instance(), reader);
|
mxlReleaseFlowReader(instance(), video_reader);
|
||||||
|
if (has_audio) mxlReleaseFlowReader(instance(), audio_reader);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ static dmf::FlowGraph build_graph() {
|
|||||||
const std::string ndi_audio_flow = gen_uuid();
|
const std::string ndi_audio_flow = gen_uuid();
|
||||||
g.nodes = {
|
g.nodes = {
|
||||||
{ "testpattern", "testpattern", {{"pattern", "bars"}} },
|
{ "testpattern", "testpattern", {{"pattern", "bars"}} },
|
||||||
{ "ndiin", "ndiin", {} },
|
// { "ndiin", "ndiin", {} },
|
||||||
{ "fakesink", "fakesink", {} },
|
// { "fakesink", "fakesink", {} },
|
||||||
{ "ndiout", "ndiout", {} },
|
{ "ndiout", "ndiout", {} },
|
||||||
};
|
};
|
||||||
const nlohmann::json video_fmt = {
|
const nlohmann::json video_fmt = {
|
||||||
@@ -55,10 +55,12 @@ static dmf::FlowGraph build_graph() {
|
|||||||
{"kind","audio"}, {"sample_rate",48000}, {"channels",2}, {"bit_depth",32}
|
{"kind","audio"}, {"sample_rate",48000}, {"channels",2}, {"bit_depth",32}
|
||||||
};
|
};
|
||||||
g.edges = {
|
g.edges = {
|
||||||
{ tp_video_flow, "testpattern", "flow_id", "fakesink", "flow_id", video_fmt },
|
// { tp_video_flow, "testpattern", "flow_id", "fakesink", "flow_id", video_fmt },
|
||||||
{ tp_audio_flow, "testpattern", "audio_flow_id", "", "", audio_fmt },
|
// { tp_audio_flow, "testpattern", "audio_flow_id", "", "", audio_fmt },
|
||||||
{ ndi_video_flow, "ndiin", "video_flow_id", "ndiout", "flow_id", video_fmt },
|
// { ndi_video_flow, "ndiin", "video_flow_id", "ndiout", "flow_id", video_fmt },
|
||||||
{ ndi_audio_flow, "ndiin", "audio_flow_id", "", "", audio_fmt },
|
// { ndi_audio_flow, "ndiin", "audio_flow_id", "", "", audio_fmt },
|
||||||
|
{ tp_video_flow, "testpattern", "flow_id", "ndiout", "flow_id", video_fmt },
|
||||||
|
{ tp_audio_flow, "testpattern", "audio_flow_id", "ndiout", "audio_flow_id", audio_fmt },
|
||||||
};
|
};
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user