fixes + refactoring + plan
This commit is contained in:
+67
-59
@@ -1,38 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include "DeckLinkSender.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mxl/flow.h>
|
||||
#include <mxl/time.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include "Signal.hpp"
|
||||
#include "NodeBase.hpp"
|
||||
#include "DeckLinkSender.hpp"
|
||||
|
||||
namespace dmf {
|
||||
class DeckLinkOutNode : public dmf::NodeBase {
|
||||
void run() override {
|
||||
// --- common params (optional) ---
|
||||
const uint32_t device_index = config().value("device_index", 0u);
|
||||
const uint32_t device_index = config().value("device_index", 0u);
|
||||
|
||||
// --- video flow (optional) ---
|
||||
bool has_video = config().contains("video_flow_id");
|
||||
|
||||
int width = 1920;
|
||||
int height = 1080;
|
||||
int fps_num = 25;
|
||||
int fps_den = 1;
|
||||
int width = 1920;
|
||||
int height = 1080;
|
||||
int fps_num = 25;
|
||||
int fps_den = 1;
|
||||
std::string flow_id;
|
||||
mxlFlowReader video_reader{};
|
||||
uint32_t video_stride = 0;
|
||||
mxlFlowReader video_reader{};
|
||||
uint32_t video_stride = 0;
|
||||
|
||||
if (has_video) {
|
||||
const auto flow_info = config().at("video_flow_id");
|
||||
flow_id = flow_info.at("id").get<std::string>();
|
||||
width = flow_info.value("width", 1920);
|
||||
height = flow_info.value("height", 1080);
|
||||
fps_num = flow_info.value("fps_num", 25);
|
||||
fps_den = flow_info.value("fps_den", 1);
|
||||
|
||||
flow_id = flow_info.at("id").get<std::string>();
|
||||
width = flow_info.value("width", 1920);
|
||||
height = flow_info.value("height", 1080);
|
||||
fps_num = flow_info.value("fps_num", 25);
|
||||
fps_den = flow_info.value("fps_den", 1);
|
||||
log("video flow=%s %dx%d @ %d/%d fps", flow_id.c_str(), width, height, fps_num, fps_den);
|
||||
|
||||
log("waiting for flow to become active...");
|
||||
@@ -46,15 +42,17 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
|
||||
mxlFlowConfigInfo video_cfg{};
|
||||
mxlStatus vst = mxlCreateFlowReader(instance(), flow_id.c_str(), "", &video_reader);
|
||||
if (vst != MXL_STATUS_OK) { log("video mxlCreateFlowReader failed (%s)", dmf::mxl_status_str(vst)); return; }
|
||||
if (vst != MXL_STATUS_OK) {
|
||||
log("video mxlCreateFlowReader failed (%s)", dmf::mxl_status_str(vst));
|
||||
return;
|
||||
}
|
||||
mxlFlowReaderGetConfigInfo(video_reader, &video_cfg);
|
||||
video_stride = video_cfg.discrete.sliceSizes[0];
|
||||
}
|
||||
|
||||
// --- audio flow (optional) ---
|
||||
mxlFlowReader audio_reader{};
|
||||
mxlFlowConfigInfo audio_cfg{};
|
||||
int sample_rate = 0;
|
||||
mxlFlowReader audio_reader{};
|
||||
int sample_rate = 48000;
|
||||
int channels = 0;
|
||||
int samples_per_frame = 0;
|
||||
bool has_audio = config().contains("audio_flow_id");
|
||||
@@ -62,19 +60,20 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
|
||||
if (has_audio) {
|
||||
const auto audio_flow_info = config().at("audio_flow_id");
|
||||
audio_flow_id = audio_flow_info.at("id").get<std::string>();
|
||||
audio_flow_id = audio_flow_info.at("id").get<std::string>();
|
||||
sample_rate = audio_flow_info.value("sample_rate", 48000);
|
||||
channels = audio_flow_info.value("channels", 2);
|
||||
samples_per_frame = sample_rate * fps_den / fps_num;
|
||||
|
||||
log("audio flow=%s %d Hz %dch %d samples/frame",
|
||||
audio_flow_id.c_str(), sample_rate, channels, samples_per_frame);
|
||||
|
||||
mxlStatus ast = mxlCreateFlowReader(instance(), audio_flow_id.c_str(), "", &audio_reader);
|
||||
if (ast != MXL_STATUS_OK) {
|
||||
log("audio mxlCreateFlowReader failed (%s) — continuing without audio", dmf::mxl_status_str(ast));
|
||||
log("audio mxlCreateFlowReader failed (%s) — continuing without audio",
|
||||
dmf::mxl_status_str(ast));
|
||||
has_audio = false;
|
||||
} else {
|
||||
mxlFlowConfigInfo audio_cfg{};
|
||||
mxlFlowReaderGetConfigInfo(audio_reader, &audio_cfg);
|
||||
log("audio channels=%u buffer=%u samples",
|
||||
audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength);
|
||||
@@ -85,25 +84,24 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
|
||||
dmf::DeckLinkSender sender;
|
||||
try {
|
||||
log("Available DeckLink output devices:\n");
|
||||
for (const auto& d : sender.devices) {
|
||||
log("Available DeckLink output devices:");
|
||||
for (const auto& d : sender.devices)
|
||||
log(" %u) %s", d.index, d.name.c_str());
|
||||
}
|
||||
sender.start_output(device_index, width, height, fps_num, fps_den, channels);
|
||||
} catch (const std::runtime_error& e) {
|
||||
log("DeckLink init error: %s", e.what());
|
||||
if (has_video && video_reader) mxlReleaseFlowReader(instance(), video_reader);
|
||||
if (has_audio && audio_reader) mxlReleaseFlowReader(instance(), audio_reader);
|
||||
return;
|
||||
}
|
||||
|
||||
// --- main loop ---
|
||||
uint64_t video_index = 0;
|
||||
uint64_t audio_index = 0;
|
||||
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;
|
||||
// Pre-allocate audio staging buffer (planar float32)
|
||||
std::vector<float> audio_planar(
|
||||
static_cast<size_t>(channels) * static_cast<size_t>(samples_per_frame));
|
||||
|
||||
// --- Clock init ---
|
||||
uint64_t video_index = 0;
|
||||
uint64_t audio_index = 0;
|
||||
if (has_video) {
|
||||
const mxlRational video_rate = {fps_num, fps_den};
|
||||
video_index = mxlGetCurrentIndex(&video_rate);
|
||||
@@ -113,19 +111,21 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
audio_index = mxlGetCurrentIndex(&audio_rate);
|
||||
}
|
||||
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
// --- audio: non-blocking, one chunk per video frame (or free-running) ---
|
||||
bool audio_advanced = false;
|
||||
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;
|
||||
|
||||
// --- Main loop ---
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
// Audio: non-blocking, one chunk per video frame
|
||||
bool audio_advanced = false;
|
||||
if (has_audio) {
|
||||
mxlWrappedMultiBufferSlice audio_slices{};
|
||||
mxlStatus ast = mxlFlowReaderGetSamplesNonBlocking(
|
||||
audio_reader, audio_index, samples_per_frame, &audio_slices);
|
||||
if (ast == MXL_STATUS_OK) {
|
||||
// Extract planar float32 samples from MXL ring buffer
|
||||
// (per-channel: ring buffer can wrap, so 2 fragments)
|
||||
std::vector<float> audio_planar(
|
||||
static_cast<size_t>(channels) * samples_per_frame);
|
||||
const size_t frag0 = audio_slices.base.fragments[0].size / sizeof(float);
|
||||
const size_t frag1 = audio_slices.base.fragments[1].size / sizeof(float);
|
||||
for (int c = 0; c < channels; ++c) {
|
||||
@@ -142,7 +142,7 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
}
|
||||
}
|
||||
sender.submit_audio(audio_planar.data(), samples_per_frame);
|
||||
audio_index += samples_per_frame;
|
||||
audio_index += samples_per_frame;
|
||||
audio_advanced = true;
|
||||
} else if (ast == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
|
||||
mxlFlowRuntimeInfo ari{};
|
||||
@@ -161,7 +161,7 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
}
|
||||
}
|
||||
|
||||
// --- video ---
|
||||
// Video
|
||||
if (has_video) {
|
||||
mxlGrainInfo video_grain{};
|
||||
uint8_t* video_buf = nullptr;
|
||||
@@ -191,7 +191,8 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
video_index = mxlGetCurrentIndex(&r);
|
||||
}
|
||||
} else {
|
||||
log("unexpected video status (%s) on index=%llu", dmf::mxl_status_str(vst), video_index);
|
||||
log("unexpected video status (%s) on index=%llu",
|
||||
dmf::mxl_status_str(vst), static_cast<unsigned long long>(video_index));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -199,24 +200,31 @@ class DeckLinkOutNode : public dmf::NodeBase {
|
||||
if (std::chrono::duration<double>(now - last_log_time).count() >= 1.0) {
|
||||
const double elapsed = std::chrono::duration<double>(now - wall_start).count();
|
||||
log("frames=%llu invalid=%llu late=%llu avg=%.2f fps",
|
||||
frame_count, invalid_count, late_count,
|
||||
static_cast<unsigned long long>(frame_count),
|
||||
static_cast<unsigned long long>(invalid_count),
|
||||
static_cast<unsigned long long>(late_count),
|
||||
static_cast<double>(frame_count) / elapsed);
|
||||
last_log_time = now;
|
||||
}
|
||||
} else if (!audio_advanced) {
|
||||
// audio-only and nothing was ready — avoid busy spin
|
||||
mxlSleepForNs(1'000'000);
|
||||
}
|
||||
}
|
||||
|
||||
if (has_video)
|
||||
log("stopped — total frames=%llu invalid=%llu late=%llu",
|
||||
static_cast<unsigned long long>(frame_count),
|
||||
static_cast<unsigned long long>(invalid_count),
|
||||
static_cast<unsigned long long>(late_count));
|
||||
else
|
||||
log("stopped");
|
||||
|
||||
if (has_video && video_reader) mxlReleaseFlowReader(instance(), video_reader);
|
||||
if (has_audio && audio_reader) mxlReleaseFlowReader(instance(), audio_reader);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
dmf::DeckLinkOutNode node;
|
||||
node.execute();
|
||||
return 0;
|
||||
}
|
||||
int main() {
|
||||
DeckLinkOutNode node;
|
||||
return node.execute();
|
||||
}
|
||||
|
||||
@@ -147,11 +147,11 @@ class NDIInNode : public dmf::NodeBase {
|
||||
mxlFlowWriterCommitGrain(video_writer, &grain);
|
||||
const uint64_t ns = mxlGetNsUntilIndex(video_index + 1, &video_rate);
|
||||
if (ns > 0 && ns < 2'000'000'000ULL) mxlSleepForNs(ns);
|
||||
video_index++;
|
||||
video_index = mxlGetCurrentIndex(&video_rate);
|
||||
} else {
|
||||
log("video OpenGrain failed (%s) at index=%llu", dmf::mxl_status_str(st), video_index);
|
||||
video_index = current + 1;
|
||||
}
|
||||
video_index = current + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user