decklink + audio
This commit is contained in:
+2
-1
@@ -102,14 +102,15 @@ add_subdirectory(nodes/fakesink)
|
|||||||
# ── NDI nodes ────────────────────────────────────────────────────────────────
|
# ── NDI nodes ────────────────────────────────────────────────────────────────
|
||||||
set(NDI_SDK_DIR "" CACHE PATH "Path to NDI SDK root")
|
set(NDI_SDK_DIR "" CACHE PATH "Path to NDI SDK root")
|
||||||
if(NDI_SDK_DIR)
|
if(NDI_SDK_DIR)
|
||||||
add_subdirectory(nodes/ndiout)
|
|
||||||
add_subdirectory(nodes/ndiin)
|
add_subdirectory(nodes/ndiin)
|
||||||
|
add_subdirectory(nodes/ndiout)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# ── DeckLink nodes ────────────────────────────────────────────────────────────────
|
# ── DeckLink nodes ────────────────────────────────────────────────────────────────
|
||||||
set(DECKLINK_SDK_DIR "" CACHE PATH "Path to DeckLink SDK root")
|
set(DECKLINK_SDK_DIR "" CACHE PATH "Path to DeckLink SDK root")
|
||||||
if(DECKLINK_SDK_DIR)
|
if(DECKLINK_SDK_DIR)
|
||||||
add_subdirectory(nodes/decklinkin)
|
add_subdirectory(nodes/decklinkin)
|
||||||
|
add_subdirectory(nodes/decklinkout)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_subdirectory(nodes/videoin)
|
add_subdirectory(nodes/videoin)
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"from": "decklinkin", "from_port": "audio_flow_id",
|
"from": "decklinkin", "from_port": "audio_flow_id",
|
||||||
"to": "ndiout", "to_port": "audio_flow_id",
|
"to": "ndiout", "to_port": "audio_flow_id",
|
||||||
"format": { "kind": "audio", "sample_rate": 48000, "channels": 2, "bit_depth": 32 }
|
"format": { "kind": "audio", "sample_rate": 48000, "channels": 2, "bit_depth": 32 }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{ "id": "ndiin", "type": "ndiin", "params": {} },
|
||||||
|
{ "id": "decklinkout", "type": "decklinkout", "params": { "device_index": 0 } }
|
||||||
|
],
|
||||||
|
"edges": [
|
||||||
|
{
|
||||||
|
"from": "ndiin", "from_port": "video_flow_id",
|
||||||
|
"to": "decklinkout", "to_port": "video_flow_id",
|
||||||
|
"format": { "kind": "video", "width": 1920, "height": 1080, "fps_num": 25, "fps_den": 1 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"from": "ndiin", "from_port": "audio_flow_id",
|
||||||
|
"to": "decklinkout", "to_port": "audio_flow_id",
|
||||||
|
"format": { "kind": "audio", "sample_rate": 48000, "channels": 2, "bit_depth": 32 }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{ "id": "ndiin", "type": "ndiin", "params": {} },
|
||||||
|
{ "id": "ndiout", "type": "ndiout", "params": { "device_index": 0 } }
|
||||||
|
],
|
||||||
|
"edges": [
|
||||||
|
{
|
||||||
|
"from": "ndiin", "from_port": "video_flow_id",
|
||||||
|
"to": "ndiout", "to_port": "video_flow_id",
|
||||||
|
"format": { "kind": "video", "width": 1920, "height": 1080, "fps_num": 25, "fps_den": 1 }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
set(DECKLINK_INCLUDE "${DECKLINK_SDK_DIR}/include"
|
||||||
|
CACHE PATH "Path to DeckLink API headers")
|
||||||
|
|
||||||
|
add_executable(dmf-node-decklinkout
|
||||||
|
main.cpp
|
||||||
|
"${DECKLINK_INCLUDE}/DeckLinkAPIDispatch.cpp"
|
||||||
|
)
|
||||||
|
target_compile_features(dmf-node-decklinkout PRIVATE cxx_std_20)
|
||||||
|
target_include_directories(dmf-node-decklinkout PRIVATE "${DECKLINK_INCLUDE}")
|
||||||
|
target_link_libraries(dmf-node-decklinkout PRIVATE dmf-shared ${CMAKE_DL_LIBS})
|
||||||
|
install(TARGETS dmf-node-decklinkout RUNTIME DESTINATION bin)
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "DeckLinkSender.hpp"
|
||||||
|
|
||||||
|
#include <mxl/flow.h>
|
||||||
|
#include <mxl/time.h>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
#include "Signal.hpp"
|
||||||
|
#include "NodeBase.hpp"
|
||||||
|
|
||||||
|
namespace dmf {
|
||||||
|
class DeckLinkOutNode : public dmf::NodeBase {
|
||||||
|
void run() override {
|
||||||
|
// --- common params (optional) ---
|
||||||
|
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;
|
||||||
|
std::string flow_id;
|
||||||
|
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);
|
||||||
|
|
||||||
|
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...");
|
||||||
|
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");
|
||||||
|
|
||||||
|
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; }
|
||||||
|
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;
|
||||||
|
int channels = 0;
|
||||||
|
int samples_per_frame = 0;
|
||||||
|
bool has_audio = config().contains("audio_flow_id");
|
||||||
|
std::string audio_flow_id;
|
||||||
|
|
||||||
|
if (has_audio) {
|
||||||
|
const auto audio_flow_info = config().at("audio_flow_id");
|
||||||
|
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));
|
||||||
|
has_audio = false;
|
||||||
|
} else {
|
||||||
|
mxlFlowReaderGetConfigInfo(audio_reader, &audio_cfg);
|
||||||
|
log("audio channels=%u buffer=%u samples",
|
||||||
|
audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!has_video && !has_audio) { log("no flows configured — exiting"); return; }
|
||||||
|
|
||||||
|
dmf::DeckLinkSender sender;
|
||||||
|
try {
|
||||||
|
log("Available DeckLink output devices:\n");
|
||||||
|
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());
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (has_video) {
|
||||||
|
const mxlRational video_rate = {fps_num, fps_den};
|
||||||
|
video_index = mxlGetCurrentIndex(&video_rate);
|
||||||
|
}
|
||||||
|
if (has_audio) {
|
||||||
|
const mxlRational audio_rate = {sample_rate, 1};
|
||||||
|
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;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
float* dst = audio_planar.data() + c * samples_per_frame;
|
||||||
|
const auto* src0 = reinterpret_cast<const float*>(
|
||||||
|
static_cast<const uint8_t*>(audio_slices.base.fragments[0].pointer)
|
||||||
|
+ static_cast<size_t>(c) * audio_slices.stride);
|
||||||
|
std::memcpy(dst, src0, frag0 * sizeof(float));
|
||||||
|
if (frag1 > 0) {
|
||||||
|
const auto* src1 = reinterpret_cast<const float*>(
|
||||||
|
static_cast<const uint8_t*>(audio_slices.base.fragments[1].pointer)
|
||||||
|
+ static_cast<size_t>(c) * audio_slices.stride);
|
||||||
|
std::memcpy(dst + frag0, src1, frag1 * sizeof(float));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sender.submit_audio(audio_planar.data(), samples_per_frame);
|
||||||
|
audio_index += samples_per_frame;
|
||||||
|
audio_advanced = true;
|
||||||
|
} else if (ast == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
|
||||||
|
mxlFlowRuntimeInfo ari{};
|
||||||
|
mxlFlowReaderGetRuntimeInfo(audio_reader, &ari);
|
||||||
|
audio_index = ari.headIndex;
|
||||||
|
} else if (ast == MXL_ERR_FLOW_INVALID) {
|
||||||
|
log("audio flow invalidated — reconnecting...");
|
||||||
|
mxlReleaseFlowReader(instance(), audio_reader);
|
||||||
|
audio_reader = nullptr;
|
||||||
|
mxlSleepForNs(100'000'000);
|
||||||
|
if (mxlCreateFlowReader(instance(), audio_flow_id.c_str(), "", &audio_reader) == MXL_STATUS_OK) {
|
||||||
|
log("audio flow reconnected");
|
||||||
|
const mxlRational r = {sample_rate, 1};
|
||||||
|
audio_index = mxlGetCurrentIndex(&r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- video ---
|
||||||
|
if (has_video) {
|
||||||
|
mxlGrainInfo video_grain{};
|
||||||
|
uint8_t* video_buf = nullptr;
|
||||||
|
mxlStatus vst = mxlFlowReaderGetGrainNonBlocking(
|
||||||
|
video_reader, video_index, &video_grain, &video_buf);
|
||||||
|
|
||||||
|
if (vst == MXL_STATUS_OK) {
|
||||||
|
if (video_grain.flags & MXL_GRAIN_FLAG_INVALID) invalid_count++;
|
||||||
|
sender.submit_frame(video_buf, video_stride);
|
||||||
|
frame_count++;
|
||||||
|
video_index++;
|
||||||
|
} else if (vst == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) {
|
||||||
|
mxlSleepForNs(1'000'000);
|
||||||
|
} else if (vst == MXL_ERR_OUT_OF_RANGE_TOO_LATE) {
|
||||||
|
late_count++;
|
||||||
|
mxlFlowRuntimeInfo ri{};
|
||||||
|
mxlFlowReaderGetRuntimeInfo(video_reader, &ri);
|
||||||
|
video_index = ri.headIndex;
|
||||||
|
} else if (vst == MXL_ERR_FLOW_INVALID) {
|
||||||
|
log("video flow invalidated — reconnecting...");
|
||||||
|
mxlReleaseFlowReader(instance(), video_reader);
|
||||||
|
video_reader = nullptr;
|
||||||
|
mxlSleepForNs(100'000'000);
|
||||||
|
if (mxlCreateFlowReader(instance(), flow_id.c_str(), "", &video_reader) == MXL_STATUS_OK) {
|
||||||
|
log("video flow reconnected");
|
||||||
|
const mxlRational r = {fps_num, fps_den};
|
||||||
|
video_index = mxlGetCurrentIndex(&r);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log("unexpected video status (%s) on index=%llu", dmf::mxl_status_str(vst), video_index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto now = std::chrono::steady_clock::now();
|
||||||
|
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<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 && video_reader) mxlReleaseFlowReader(instance(), video_reader);
|
||||||
|
if (has_audio && audio_reader) mxlReleaseFlowReader(instance(), audio_reader);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
dmf::DeckLinkOutNode node;
|
||||||
|
node.execute();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -98,7 +98,7 @@ class NDIInNode : public dmf::NodeBase {
|
|||||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||||
dmf::NDIReceiver::FrameKind kind;
|
dmf::NDIReceiver::FrameKind kind;
|
||||||
try {
|
try {
|
||||||
kind = ndi.capture(latest_video.data(), video_stride, audio_buf, audio_info);
|
kind = ndi.capture(latest_video.data(), video_stride, audio_buf, audio_info, has_audio);
|
||||||
} catch (const std::runtime_error& e) {
|
} catch (const std::runtime_error& e) {
|
||||||
log("NDI error: %s — stopping", e.what());
|
log("NDI error: %s — stopping", e.what());
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -0,0 +1,250 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <DeckLinkAPI.h>
|
||||||
|
|
||||||
|
#include "V210.hpp"
|
||||||
|
|
||||||
|
namespace dmf {
|
||||||
|
|
||||||
|
class DeckLinkSender {
|
||||||
|
public:
|
||||||
|
struct VideoInfo {
|
||||||
|
int width = 0;
|
||||||
|
int height = 0;
|
||||||
|
int fps_num = 0;
|
||||||
|
int fps_den = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AudioInfo {
|
||||||
|
int sample_rate = 48000;
|
||||||
|
int channels = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DeviceInfo {
|
||||||
|
uint32_t index;
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<DeviceInfo> devices;
|
||||||
|
VideoInfo video_info{};
|
||||||
|
AudioInfo audio_info{};
|
||||||
|
bool has_audio = false;
|
||||||
|
int64_t frame_count = 0;
|
||||||
|
std::atomic<int64_t> audio_stream_time{0};
|
||||||
|
|
||||||
|
DeckLinkSender() { enumerate_devices(); }
|
||||||
|
~DeckLinkSender() {
|
||||||
|
if (decklink_output) {
|
||||||
|
decklink_output->StopScheduledPlayback(frame_count * video_info.fps_den,
|
||||||
|
nullptr,
|
||||||
|
video_info.fps_num);
|
||||||
|
decklink_output->DisableVideoOutput();
|
||||||
|
if (has_audio) decklink_output->DisableAudioOutput();;
|
||||||
|
decklink_output->SetScheduledFrameCompletionCallback(nullptr);
|
||||||
|
decklink_output->Release();
|
||||||
|
for (auto* vf : frame_pool) vf->Release();
|
||||||
|
frame_pool.clear();
|
||||||
|
}
|
||||||
|
delete output_callback;
|
||||||
|
for (auto* d : raw_devices) d->Release();
|
||||||
|
if (selected_device) selected_device->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// channels > 0 enables audio output at 48 kHz / 32-bit int.
|
||||||
|
void start_output(uint32_t device_index, int width, int height, int fps_num, int fps_den, int channels = 0) {
|
||||||
|
if (device_index >= raw_devices.size())
|
||||||
|
throw std::runtime_error("Device index out of range");
|
||||||
|
|
||||||
|
video_info.width = width;
|
||||||
|
video_info.height = height;
|
||||||
|
video_info.fps_num = fps_num;
|
||||||
|
video_info.fps_den = fps_den;
|
||||||
|
|
||||||
|
BMDDisplayMode bm_display_mode = bmdModeHD1080p25; //need to create a func for detection
|
||||||
|
|
||||||
|
selected_device = raw_devices[device_index];
|
||||||
|
selected_device->AddRef();
|
||||||
|
|
||||||
|
HRESULT r = selected_device->QueryInterface(IID_IDeckLinkOutput, (void**)&decklink_output);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not obtain IDeckLinkOutput");
|
||||||
|
|
||||||
|
output_callback = new OutputCallback(*this);
|
||||||
|
r = decklink_output->SetScheduledFrameCompletionCallback(output_callback);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not set output callback");
|
||||||
|
|
||||||
|
r = decklink_output->EnableVideoOutput(bmdModeHD1080p25, bmdVideoOutputFlagDefault);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not enable video output");
|
||||||
|
|
||||||
|
if (channels > 0) {
|
||||||
|
r = decklink_output->EnableAudioOutput(bmdAudioSampleRate48kHz,
|
||||||
|
bmdAudioSampleType32bitInteger,
|
||||||
|
static_cast<uint32_t>(channels),
|
||||||
|
bmdAudioOutputStreamTimestamped);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not enable audio output");
|
||||||
|
// r = decklink_output->SetAudioCallback
|
||||||
|
has_audio = true;
|
||||||
|
audio_info.channels = channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_supported = false;
|
||||||
|
BMDDisplayMode actual_mode;
|
||||||
|
r = decklink_output->DoesSupportVideoMode(
|
||||||
|
bmdVideoConnectionUnspecified, // TODO: create selection between sdi, hdmi, etc.
|
||||||
|
bmdModeHD1080p25,
|
||||||
|
bmdFormat10BitYUV,
|
||||||
|
bmdNoVideoOutputConversion,
|
||||||
|
bmdSupportedVideoModeDefault,
|
||||||
|
&actual_mode,
|
||||||
|
&is_supported
|
||||||
|
);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Selected mode is not supported for bmdFormat10BitYUV");
|
||||||
|
|
||||||
|
int32_t row_bytes;
|
||||||
|
r = decklink_output->RowBytesForPixelFormat(bmdFormat10BitYUV, width, &row_bytes);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not get row bytes for display mode");
|
||||||
|
|
||||||
|
// prefill frame pool
|
||||||
|
const int64_t duration = fps_den;
|
||||||
|
const int64_t timescale = fps_num;
|
||||||
|
const size_t preroll_pool_size = 3; // min=3, cause 1 displaying, 1 queued, 1 writable
|
||||||
|
for (size_t i = 0; i < preroll_pool_size; ++i) {
|
||||||
|
IDeckLinkMutableVideoFrame* vf = nullptr;
|
||||||
|
r = decklink_output->CreateVideoFrame(width, height, row_bytes, bmdFormat10BitYUV, bmdFrameFlagDefault, &vf);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not create a video frame");
|
||||||
|
IDeckLinkVideoBuffer* buf = nullptr;
|
||||||
|
vf->QueryInterface(IID_IDeckLinkVideoBuffer, (void**)&buf);
|
||||||
|
buf->StartAccess(bmdBufferAccessWrite);
|
||||||
|
void* ptr = nullptr;
|
||||||
|
buf->GetBytes(&ptr);
|
||||||
|
if (ptr) dmf::v210::fill_black(static_cast<uint8_t*>(ptr), width, height, row_bytes);
|
||||||
|
buf->EndAccess(bmdBufferAccessWrite);
|
||||||
|
buf->Release();
|
||||||
|
|
||||||
|
decklink_output->ScheduleVideoFrame(vf, i * duration, duration, timescale);
|
||||||
|
frame_pool.push_back(vf);
|
||||||
|
}
|
||||||
|
output_callback->next_time.store(preroll_pool_size * duration);
|
||||||
|
|
||||||
|
// start playback
|
||||||
|
r = decklink_output->StartScheduledPlayback(0, timescale, 1.0);
|
||||||
|
if (r != S_OK) throw std::runtime_error("Could not start streams");
|
||||||
|
}
|
||||||
|
|
||||||
|
void submit_frame(const uint8_t* src, uint32_t stride) {
|
||||||
|
// Grab a free frame from the pool (round-robin)
|
||||||
|
IDeckLinkMutableVideoFrame* vf = frame_pool[frame_count % frame_pool.size()];
|
||||||
|
frame_count++;
|
||||||
|
|
||||||
|
IDeckLinkVideoBuffer* buf = nullptr;
|
||||||
|
if (vf->QueryInterface(IID_IDeckLinkVideoBuffer, (void**)&buf) != S_OK) return;
|
||||||
|
buf->StartAccess(bmdBufferAccessWrite);
|
||||||
|
void* ptr = nullptr;
|
||||||
|
buf->GetBytes(&ptr);
|
||||||
|
if (ptr) {
|
||||||
|
// Row-by-row copy with stride adaptation
|
||||||
|
const uint32_t dst_stride = vf->GetRowBytes();
|
||||||
|
const uint32_t copy_row = std::min(stride, dst_stride);
|
||||||
|
for (int y = 0; y < video_info.height; ++y) {
|
||||||
|
std::memcpy(static_cast<uint8_t*>(ptr) + y * dst_stride,
|
||||||
|
src + y * stride, copy_row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf->EndAccess(bmdBufferAccessWrite);
|
||||||
|
buf->Release();
|
||||||
|
|
||||||
|
// Schedule for display
|
||||||
|
BMDTimeValue display_time = output_callback->next_time.fetch_add(video_info.fps_den);
|
||||||
|
decklink_output->ScheduleVideoFrame(vf, display_time,
|
||||||
|
video_info.fps_den, video_info.fps_num);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void submit_audio(const float* planar, int samples) {
|
||||||
|
if (!has_audio || samples <= 0) return;
|
||||||
|
// float32 planar → interleaved int32 (4-byte PCM) scaled to int32 range
|
||||||
|
const int channels = audio_info.channels;
|
||||||
|
std::vector<int32_t> interleaved(
|
||||||
|
static_cast<size_t>(channels) * static_cast<size_t>(samples));
|
||||||
|
for (int s = 0; s < samples; ++s) {
|
||||||
|
for (int c = 0; c < channels; ++c) {
|
||||||
|
float v = planar[c * samples + s]; // planar: channel-major
|
||||||
|
// clamp to safe range and scale
|
||||||
|
if (v > 1.0f) v = 1.0f;
|
||||||
|
else if (v < -1.0f) v = -1.0f;
|
||||||
|
interleaved[static_cast<size_t>(s) * channels + c] =
|
||||||
|
static_cast<int32_t>(v * 2147483647.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const int64_t stream_time = audio_stream_time.fetch_add(samples);
|
||||||
|
uint32_t written = 0;
|
||||||
|
decklink_output->ScheduleAudioSamples(
|
||||||
|
interleaved.data(), static_cast<uint32_t>(samples),
|
||||||
|
stream_time, audio_info.sample_rate, &written);
|
||||||
|
(void)written;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
class OutputCallback: public IDeckLinkVideoOutputCallback {
|
||||||
|
public:
|
||||||
|
explicit OutputCallback(DeckLinkSender& owner) : owner(owner) {}
|
||||||
|
|
||||||
|
HRESULT ScheduledFrameCompleted (IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult result) override {
|
||||||
|
// Frame is done displaying — return it to the pool.
|
||||||
|
// submit_frame will overwrite and reschedule it with fresh MXL data.
|
||||||
|
return S_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT ScheduledPlaybackHasStopped (void) override {
|
||||||
|
return S_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, LPVOID*) override { return E_NOINTERFACE; }
|
||||||
|
ULONG STDMETHODCALLTYPE AddRef() override { return ++ref_count; }
|
||||||
|
ULONG STDMETHODCALLTYPE Release() override { return --ref_count; }
|
||||||
|
|
||||||
|
std::atomic<int64_t> next_time{0};
|
||||||
|
|
||||||
|
private:
|
||||||
|
DeckLinkSender& owner;
|
||||||
|
std::atomic<uint64_t> next_frame_idx{0};
|
||||||
|
std::atomic<int32_t> ref_count{1};
|
||||||
|
};
|
||||||
|
|
||||||
|
// DeckLink SDK objects
|
||||||
|
std::vector<IDeckLink*> raw_devices;
|
||||||
|
IDeckLink* selected_device = nullptr;
|
||||||
|
IDeckLinkOutput* decklink_output = nullptr;
|
||||||
|
OutputCallback* output_callback = nullptr;
|
||||||
|
|
||||||
|
std::vector<IDeckLinkMutableVideoFrame*> frame_pool;
|
||||||
|
|
||||||
|
void enumerate_devices() {
|
||||||
|
IDeckLinkIterator* it = CreateDeckLinkIteratorInstance();
|
||||||
|
if (!it) throw std::runtime_error("DeckLink drivers not installed");
|
||||||
|
|
||||||
|
IDeckLink* device = nullptr;
|
||||||
|
uint32_t index = 0;
|
||||||
|
while (it->Next(&device) == S_OK) {
|
||||||
|
IDeckLinkInput* inp = nullptr;
|
||||||
|
if (device->QueryInterface(IID_IDeckLinkOutput, (void**)&inp) == S_OK) {
|
||||||
|
inp->Release();
|
||||||
|
const char* name = nullptr;
|
||||||
|
device->GetDisplayName(&name);
|
||||||
|
devices.push_back({index, name ? name : "?"});
|
||||||
|
raw_devices.push_back(device);
|
||||||
|
index++;
|
||||||
|
} else {
|
||||||
|
device->Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it->Release();
|
||||||
|
if (raw_devices.empty())
|
||||||
|
throw std::runtime_error("No DeckLink output devices found");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -126,10 +126,13 @@ public:
|
|||||||
// Returns FrameKind::None on timeout or non-A/V frames.
|
// Returns FrameKind::None on timeout or non-A/V frames.
|
||||||
// Throws on source lost or video format change.
|
// Throws on source lost or video format change.
|
||||||
FrameKind capture(uint8_t* frame_buffer, uint32_t frame_stride,
|
FrameKind capture(uint8_t* frame_buffer, uint32_t frame_stride,
|
||||||
std::vector<float>& audio_out, AudioInfo& audio_info) {
|
std::vector<float>& audio_out, AudioInfo& audio_info,
|
||||||
|
bool want_audio = false) {
|
||||||
NDIlib_video_frame_v2_t video_frame{};
|
NDIlib_video_frame_v2_t video_frame{};
|
||||||
NDIlib_audio_frame_v3_t audio_frame{};
|
NDIlib_audio_frame_v3_t audio_frame{};
|
||||||
auto type = NDIlib_recv_capture_v3(recv_, &video_frame, &audio_frame, nullptr, 5);
|
auto type = NDIlib_recv_capture_v3(recv_, &video_frame,
|
||||||
|
want_audio ? &audio_frame : nullptr,
|
||||||
|
nullptr, 5);
|
||||||
|
|
||||||
if (type == NDIlib_frame_type_error)
|
if (type == NDIlib_frame_type_error)
|
||||||
throw std::runtime_error("NDI source lost");
|
throw std::runtime_error("NDI source lost");
|
||||||
|
|||||||
Reference in New Issue
Block a user