Feature/ndi out node #1
+87
-21
@@ -1,3 +1,5 @@
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
#include <string>
|
||||
#include <mxl/flow.h>
|
||||
#include <mxl/time.h>
|
||||
@@ -7,43 +9,77 @@
|
||||
|
||||
class TestPatternNode : public dmf::NodeBase {
|
||||
void run() override {
|
||||
// --- video flow ---
|
||||
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);
|
||||
const auto pattern = config().value("pattern", "bars");
|
||||
|
||||
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);
|
||||
log("flow=%s %dx%d @ %d/%d fps pattern=%s",
|
||||
flow_id.c_str(), width, height, fps_num, fps_den, pattern.c_str());
|
||||
|
||||
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;
|
||||
}
|
||||
instance(),
|
||||
dmf::make_video_flow_def(flow_id, node_id(), width, height, fps_num, fps_den).c_str(),
|
||||
nullptr, &writer, &cfg_info, &created);
|
||||
if (st != MXL_STATUS_OK) { log("video mxlCreateFlowWriter failed (status=%d)", st); return; }
|
||||
|
||||
const uint32_t stride = cfg_info.discrete.sliceSizes[0];
|
||||
const auto pattern = config().value("pattern", "bars");
|
||||
log("stride=%u B/line grain=%u B ring=%u grains pattern=%s",
|
||||
stride, stride * static_cast<uint32_t>(height), cfg_info.discrete.grainCount,
|
||||
pattern.c_str());
|
||||
log("video 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);
|
||||
// --- audio flow (optional) ---
|
||||
mxlFlowWriter audio_writer{};
|
||||
mxlFlowConfigInfo audio_cfg{};
|
||||
int sample_rate = 48000;
|
||||
int channels = 2;
|
||||
bool has_audio = config().contains("audio_flow_id");
|
||||
|
||||
if (has_audio) {
|
||||
const auto audio_info = config().at("audio_flow_id");
|
||||
const auto audio_id = audio_info.at("id").get<std::string>();
|
||||
sample_rate = audio_info.value("sample_rate", 48000);
|
||||
channels = audio_info.value("channels", 2);
|
||||
|
||||
log("audio flow=%s %d Hz %dch", audio_id.c_str(), sample_rate, channels);
|
||||
|
||||
mxlStatus ast = mxlCreateFlowWriter(
|
||||
instance(),
|
||||
dmf::make_audio_flow_def(audio_id, node_id(), sample_rate, channels, 32,
|
||||
fps_num, fps_den).c_str(),
|
||||
nullptr, &audio_writer, &audio_cfg, &created);
|
||||
if (ast != MXL_STATUS_OK) {
|
||||
log("audio mxlCreateFlowWriter failed (status=%d) — continuing without audio", ast);
|
||||
has_audio = false;
|
||||
} else {
|
||||
log("audio channels=%u buffer=%u samples",
|
||||
audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength);
|
||||
}
|
||||
}
|
||||
|
||||
// --- main loop ---
|
||||
const mxlRational video_rate = {fps_num, fps_den};
|
||||
const mxlRational audio_rate = {sample_rate, 1};
|
||||
const size_t samples_per_frame =
|
||||
static_cast<size_t>(sample_rate) * static_cast<size_t>(fps_den) / static_cast<size_t>(fps_num);
|
||||
|
||||
// -18 dBFS broadcast reference level
|
||||
const float amplitude = static_cast<float>(std::pow(10.0, -18.0 / 20.0));
|
||||
|
||||
uint64_t index = mxlGetCurrentIndex(&video_rate);
|
||||
uint64_t audio_index = has_audio ? mxlGetCurrentIndex(&audio_rate) : 0;
|
||||
log("start video_index=%llu", index);
|
||||
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
// --- video grain ---
|
||||
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);
|
||||
@@ -57,16 +93,46 @@ class TestPatternNode : public dmf::NodeBase {
|
||||
else if (pattern == "white") dmf::v210::fill_white (buf, width, height, stride);
|
||||
else dmf::v210::fill_colorbars (buf, width, height, stride);
|
||||
grain.flags = 0;
|
||||
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);
|
||||
// --- audio: sine tones, channel c = (c+1) * 1000 Hz ---
|
||||
if (has_audio) {
|
||||
mxlMutableWrappedMultiBufferSlice slice{};
|
||||
if (mxlFlowWriterOpenSamples(audio_writer, audio_index,
|
||||
samples_per_frame, &slice) == MXL_STATUS_OK) {
|
||||
const size_t frag0 = slice.base.fragments[0].size / sizeof(float);
|
||||
const size_t frag1 = slice.base.fragments[1].size / sizeof(float);
|
||||
for (int c = 0; c < channels; ++c) {
|
||||
const double freq = 1000.0 * (c + 1);
|
||||
const double period = static_cast<double>(sample_rate) / freq;
|
||||
auto write_samples = [&](float* dst, size_t count, size_t offset) {
|
||||
for (size_t s = 0; s < count; ++s)
|
||||
dst[s] = amplitude * static_cast<float>(
|
||||
std::sin(2.0 * std::numbers::pi * (audio_index + offset + s) / period));
|
||||
};
|
||||
auto* dst0 = reinterpret_cast<float*>(
|
||||
static_cast<uint8_t*>(slice.base.fragments[0].pointer) + c * slice.stride);
|
||||
write_samples(dst0, frag0, 0);
|
||||
if (frag1 > 0) {
|
||||
auto* dst1 = reinterpret_cast<float*>(
|
||||
static_cast<uint8_t*>(slice.base.fragments[1].pointer) + c * slice.stride);
|
||||
write_samples(dst1, frag1, frag0);
|
||||
}
|
||||
}
|
||||
mxlFlowWriterCommitSamples(audio_writer);
|
||||
}
|
||||
audio_index += samples_per_frame;
|
||||
}
|
||||
|
||||
const uint64_t ns = mxlGetNsUntilIndex(index + 1, &video_rate);
|
||||
if (ns > 0 && ns < 2'000'000'000ULL) mxlSleepForNs(ns);
|
||||
index++;
|
||||
}
|
||||
|
||||
log("stopped at index=%llu", index);
|
||||
log("stopped at video_index=%llu", index);
|
||||
mxlReleaseFlowWriter(instance(), writer);
|
||||
if (has_audio) mxlReleaseFlowWriter(instance(), audio_writer);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -38,8 +38,10 @@ static std::string gen_uuid() {
|
||||
|
||||
static dmf::FlowGraph build_graph() {
|
||||
dmf::FlowGraph g;
|
||||
const std::string video_flow = gen_uuid();
|
||||
const std::string audio_flow = gen_uuid();
|
||||
const std::string tp_video_flow = gen_uuid();
|
||||
const std::string tp_audio_flow = gen_uuid();
|
||||
const std::string ndi_video_flow = gen_uuid();
|
||||
const std::string ndi_audio_flow = gen_uuid();
|
||||
g.nodes = {
|
||||
{ "testpattern", "testpattern", {{"pattern", "bars"}} },
|
||||
{ "ndiin", "ndiin", {} },
|
||||
@@ -53,10 +55,10 @@ static dmf::FlowGraph build_graph() {
|
||||
{"kind","audio"}, {"sample_rate",48000}, {"channels",2}, {"bit_depth",32}
|
||||
};
|
||||
g.edges = {
|
||||
{ video_flow, "ndiin", "video_flow_id", "fakesink", "flow_id", video_fmt },
|
||||
{ video_flow, "ndiin", "video_flow_id", "ndiout", "flow_id", video_fmt },
|
||||
// audio_flow_id wired to ndiin only — no sink node yet, readers added later
|
||||
{ audio_flow, "ndiin", "audio_flow_id", "", "", audio_fmt },
|
||||
{ tp_video_flow, "testpattern", "flow_id", "fakesink", "flow_id", video_fmt },
|
||||
{ tp_audio_flow, "testpattern", "audio_flow_id", "", "", audio_fmt },
|
||||
{ ndi_video_flow, "ndiin", "video_flow_id", "ndiout", "flow_id", video_fmt },
|
||||
{ ndi_audio_flow, "ndiin", "audio_flow_id", "", "", audio_fmt },
|
||||
};
|
||||
return g;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user