refactor: DeckLinkReceiver cleanup and encapsulation
DeckLinkReceiver:
- Make InputCallback a private nested class — no longer exposed publicly
- DeckLinkReceiver owns all shared state (mutex, CVs, frame buffer)
- Replace get_input_callback() with clean wait_for_frame() API
- Fix device list leak: enumerate_devices stores raw IDeckLink* in
raw_devices; destructor releases all of them + selected_device's AddRef
- Remove dead members: device_config, device_status, deckLink_notification
- Remove dead SourceInfo::stride field; rename SourceInfo → VideoInfo
- Remove dead video_source_info public member
- Remove printf; use no logging in receiver (caller logs)
- Remove commented-out notification code
- Fix dead return false after throw in enumerate_devices
- Fix dead null check after new InputCallback
- Replace IDeckLinkVideoBuffer QueryInterface with simpler GetBytes()
- Replace plain bool frame_ready/format_detected with consistent usage
under mutex (no longer mixing atomic + CV pattern)
- Call StopStreams/DisableVideoInput in destructor
- Consistent snake_case naming throughout
decklinkin main.cpp:
- Rename NodeDeckLinkIn → DeckLinkInNode
- Get device_index from config().value("device_index", 0u)
- Remove unused includes: <time.h>, <algorithm>, <cstdio>, <DeckLinkAPI.h>
- Use clean receiver.wait_for_frame() instead of reaching into callback
- Use mxlGetCurrentIndex resync after sleep (consistent with other nodes)
- Fix return node.execute() (was node.execute(); return 0)
- Fix main() spacing
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+45
-83
@@ -1,123 +1,85 @@
|
||||
#include "NodeBase.hpp"
|
||||
#include "DeckLinkReceiver.hpp"
|
||||
#include "Signal.hpp"
|
||||
#include "FlowDef.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <DeckLinkAPI.h>
|
||||
#include <time.h>
|
||||
#include <mxl/flow.h>
|
||||
#include <mxl/time.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "NodeBase.hpp"
|
||||
#include "FlowDef.hpp"
|
||||
#include "DeckLinkReceiver.hpp"
|
||||
|
||||
class NodeDeckLinkIn: public dmf::NodeBase {
|
||||
class DeckLinkInNode : public dmf::NodeBase {
|
||||
void run() override {
|
||||
dmf::DeckLinkReceiver decklink_receiver;
|
||||
log("Available DeckLink input devices:");
|
||||
for (auto device : decklink_receiver.devices_list) {
|
||||
log("%i) %s", device.index, device.display_name.c_str());
|
||||
}
|
||||
int selected_device = 0;
|
||||
decklink_receiver.start_capture(selected_device);
|
||||
log(
|
||||
"DeckLink feed for device '%s':",
|
||||
decklink_receiver.devices_list.at(selected_device).display_name.c_str()
|
||||
);
|
||||
dmf::DeckLinkReceiver receiver;
|
||||
|
||||
dmf::SourceInfo video_source_info{};
|
||||
if (!decklink_receiver.wait_for_format(5000)) {
|
||||
log("Available DeckLink devices:");
|
||||
for (const auto& d : receiver.devices)
|
||||
log(" %u) %s", d.index, d.name.c_str());
|
||||
|
||||
const uint32_t device_index = config().value("device_index", 0u);
|
||||
receiver.start_capture(device_index);
|
||||
log("Capturing from: %s", receiver.devices[device_index].name.c_str());
|
||||
|
||||
if (!receiver.wait_for_format(5000)) {
|
||||
log("Timeout waiting for format detection");
|
||||
return;
|
||||
}
|
||||
video_source_info = decklink_receiver.get_input_callback()->video_info;
|
||||
if (video_source_info.width == 0 || video_source_info.fps_num == 0) {
|
||||
log("Invalid format detected");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& vi = receiver.video_info;
|
||||
if (vi.width == 0 || vi.fps_num == 0) { log("Invalid format detected"); return; }
|
||||
log("Detected: %dx%d @ %d/%d fps", vi.width, vi.height, vi.fps_num, vi.fps_den);
|
||||
|
||||
const auto video_flow_info = config().at("video_flow_id");
|
||||
const auto video_flow_id = video_flow_info.at("id").get<std::string>();
|
||||
const int width = video_flow_info.value("width", video_source_info.width);
|
||||
const int height = video_flow_info.value("height", video_source_info.height);
|
||||
const int fps_num = video_flow_info.value("fps_num", video_source_info.fps_num);
|
||||
const int fps_den = video_flow_info.value("fps_den", video_source_info.fps_den);
|
||||
const int width = video_flow_info.value("width", vi.width);
|
||||
const int height = video_flow_info.value("height", vi.height);
|
||||
const int fps_num = video_flow_info.value("fps_num", vi.fps_num);
|
||||
const int fps_den = video_flow_info.value("fps_den", vi.fps_den);
|
||||
|
||||
log("video flow=%s %dx%d @ %d/%d fps", video_flow_id.c_str(), width, height, fps_num, fps_den);
|
||||
|
||||
mxlFlowWriter video_writer{};
|
||||
mxlFlowWriter video_writer = nullptr;
|
||||
mxlFlowConfigInfo video_cfg{};
|
||||
bool created = false;
|
||||
mxlStatus vst = mxlCreateFlowWriter(
|
||||
instance(),
|
||||
dmf::make_video_flow_def(video_flow_id, node_id(), width, height, fps_num, fps_den).c_str(),
|
||||
"", &video_writer, &video_cfg, &created);
|
||||
if (vst != MXL_STATUS_OK) { log("video mxlCreateFlowWriter failed (%s)", dmf::mxl_status_str(vst)); return; }
|
||||
if (vst != MXL_STATUS_OK) {
|
||||
log("mxlCreateFlowWriter failed (%s)", dmf::mxl_status_str(vst));
|
||||
return;
|
||||
}
|
||||
|
||||
const uint32_t video_stride = video_cfg.discrete.sliceSizes[0];
|
||||
log("video stride=%u B/line grain=%u B ring=%u grains",
|
||||
video_stride, video_stride * static_cast<uint32_t>(height), video_cfg.discrete.grainCount);
|
||||
|
||||
const mxlRational video_rate = {fps_num, fps_den};
|
||||
uint64_t video_index = mxlGetCurrentIndex(&video_rate);
|
||||
log("start video_index=%lu", static_cast<unsigned long>(video_index));
|
||||
const mxlRational video_rate = {fps_num, fps_den};
|
||||
uint64_t video_index = mxlGetCurrentIndex(&video_rate);
|
||||
log("start video_index=%llu", video_index);
|
||||
|
||||
auto* cb = decklink_receiver.get_input_callback();
|
||||
const uint32_t dst_row = video_stride;
|
||||
std::vector<uint8_t> local_frame(dst_row * height, 0);
|
||||
std::vector<uint8_t> frame_buf(static_cast<size_t>(video_stride) * static_cast<size_t>(height), 0);
|
||||
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
if (!receiver.wait_for_frame(frame_buf.data(), video_stride, width, height)) break;
|
||||
|
||||
while(dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
// Wait for a fresh frame from the callback
|
||||
uint32_t src_row;
|
||||
int src_w, src_h;
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(cb->frame_mutex);
|
||||
cb->frame_cv.wait(lk, [&] {
|
||||
return cb->frame_ready.load(std::memory_order_acquire)
|
||||
|| !dmf::g_running.load(std::memory_order_relaxed);
|
||||
});
|
||||
if (!dmf::g_running.load(std::memory_order_relaxed)) break;
|
||||
src_row = cb->frame_row_bytes ? cb->frame_row_bytes : dst_row;
|
||||
src_w = cb->frame_width;
|
||||
src_h = cb->frame_height;
|
||||
// Row-by-row copy with zero padding for stride difference
|
||||
const uint8_t* src = cb->frame_buffer.data();
|
||||
uint8_t* dst = local_frame.data();
|
||||
const uint32_t copy_row = std::min<uint32_t>(src_row, dst_row);
|
||||
const int rows = std::min(src_h, height);
|
||||
std::memset(local_frame.data(), 0, local_frame.size());
|
||||
for (int y = 0; y < rows; ++y) {
|
||||
std::memcpy(dst, src, copy_row);
|
||||
src += src_row;
|
||||
dst += dst_row;
|
||||
}
|
||||
cb->frame_ready.store(false, std::memory_order_release);
|
||||
}
|
||||
if (src_w != width || src_h != height) {
|
||||
log("frame dim mismatch: src=%dx%d mxl=%dx%d (skipping)", src_w, src_h, width, height);
|
||||
continue;
|
||||
}
|
||||
// MXL write — no lock held, callback can fill next frame in parallel
|
||||
mxlGrainInfo grain{};
|
||||
uint8_t* video_buf = nullptr;
|
||||
uint8_t* video_buf = nullptr;
|
||||
vst = mxlFlowWriterOpenGrain(video_writer, video_index, &grain, &video_buf);
|
||||
if (vst == MXL_STATUS_OK) {
|
||||
std::memcpy(video_buf, local_frame.data(), local_frame.size());
|
||||
grain.flags = 0;
|
||||
std::memcpy(video_buf, frame_buf.data(), frame_buf.size());
|
||||
grain.flags = 0;
|
||||
grain.validSlices = grain.totalSlices;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
log("stopped at video_index=%llu", video_index);
|
||||
mxlReleaseFlowWriter(instance(), video_writer);
|
||||
}
|
||||
};
|
||||
|
||||
int main ()
|
||||
{
|
||||
NodeDeckLinkIn node;
|
||||
node.execute();
|
||||
return 0;
|
||||
}
|
||||
int main() {
|
||||
DeckLinkInNode node;
|
||||
return node.execute();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user