feat: NDI in node receives and writes audio flow

- FlowDef.hpp: add make_audio_flow_def (NMOS IS-04 audio/Lnn flow)
- NDIReceiver: replace capture_v210 with capture() — single recv call
  dispatches to FrameKind::{Video,Audio,None}; AudioInfo struct carries
  sample_rate/channels/samples/channel_stride
- ndiin: optional audio writer when graph wires audio_flow_id; single
  loop with 5ms NDI poll; audio grains written immediately on arrival;
  video grains written when mxlGetCurrentIndex reaches video_index
- build_graph: rename ndiin video port flow_id → video_flow_id; add
  audio_flow_id edge (no sink yet — wired to ndiin only)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
JohannesItten
2026-07-01 12:39:35 +03:00
parent ee47d85cbd
commit cc1011cced
4 changed files with 196 additions and 80 deletions
+32
View File
@@ -36,4 +36,36 @@ inline std::string make_video_flow_def(
}.dump();
}
// Generates a minimal but valid NMOS IS-04 flow definition JSON string
// for a raw PCM audio flow. grain_rate_num/den sets the grain delivery rate
// (default 25/1 = one grain per video frame). MXL computes grain size as
// sample_rate / grain_rate samples per grain.
inline std::string make_audio_flow_def(
const std::string& flow_id,
const std::string& label,
int sample_rate,
int channels,
int bit_depth = 32,
int grain_rate_num = 25,
int grain_rate_den = 1)
{
using json = nlohmann::json;
static const char* ch_labels[] = {"L","R","C","LFE","Ls","Rs","Lss","Rss"};
json ch_arr = json::array();
for (int i = 0; i < channels; ++i)
ch_arr.push_back({{"label", i < 8 ? ch_labels[i] : ("Ch" + std::to_string(i + 1))}});
return json{
{"id", flow_id},
{"format", "urn:x-nmos:format:audio"},
{"label", label},
{"description", label + " MXL Audio Flow"},
{"media_type", "audio/L" + std::to_string(bit_depth)},
{"parents", json::array()},
{"grain_rate", {{"numerator", grain_rate_num}, {"denominator", grain_rate_den}}},
{"sample_rate", {{"numerator", sample_rate}, {"denominator", 1}}},
{"channels", ch_arr},
{"bit_depth", bit_depth},
}.dump();
}
} // namespace dmf
+50 -26
View File
@@ -112,12 +112,24 @@ public:
throw std::runtime_error("Interrupted during probe");
}
// Captures one video frame and converts it to V210 in frame_buffer.
// Returns false if no frame was available this tick (caller should repeat last frame).
// Throws on source lost or unsupported format.
bool capture_v210(uint8_t* frame_buffer, uint32_t frame_stride) {
NDIlib_video_frame_v2_t frame;
auto type = NDIlib_recv_capture_v3(recv_, &frame, nullptr, nullptr, 5);
enum class FrameKind { None, Video, Audio };
struct AudioInfo {
int sample_rate = 0;
int channels = 0;
int samples = 0;
int channel_stride = 0; // floats between channel planes (NDI planar layout)
};
// Receives one NDI frame. On video: converts to V210 in frame_buffer/frame_stride.
// On audio: copies float32 planar samples into audio_out and fills audio_info.
// Returns FrameKind::None on timeout or non-A/V frames.
// Throws on source lost or video format change.
FrameKind capture(uint8_t* frame_buffer, uint32_t frame_stride,
std::vector<float>& audio_out, AudioInfo& audio_info) {
NDIlib_video_frame_v2_t video_frame{};
NDIlib_audio_frame_v3_t audio_frame{};
auto type = NDIlib_recv_capture_v3(recv_, &video_frame, &audio_frame, nullptr, 5);
if (type == NDIlib_frame_type_error)
throw std::runtime_error("NDI source lost");
@@ -135,30 +147,42 @@ public:
std::to_string(info_.width) + "x" + std::to_string(info_.height) +
" @" + std::to_string(info_.fps_num) + "/" + std::to_string(info_.fps_den));
}
return false;
return FrameKind::None;
}
if (type != NDIlib_frame_type_video)
return false;
switch (info_.fourcc) {
case NDIlib_FourCC_type_UYVY:
v210::UYVYtoV210(frame.p_data, frame_buffer,
info_.width, info_.height, info_.stride, frame_stride);
break;
case NDIlib_FourCC_type_P216: {
NDIlib_video_frame_v2_t dst{};
dst.p_data = frame_buffer;
dst.line_stride_in_bytes = frame_stride;
NDIlib_util_P216_to_V210(&frame, &dst);
break;
if (type == NDIlib_frame_type_video) {
switch (info_.fourcc) {
case NDIlib_FourCC_type_UYVY:
v210::UYVYtoV210(video_frame.p_data, frame_buffer,
info_.width, info_.height, info_.stride, frame_stride);
break;
case NDIlib_FourCC_type_P216: {
NDIlib_video_frame_v2_t dst{};
dst.p_data = frame_buffer;
dst.line_stride_in_bytes = frame_stride;
NDIlib_util_P216_to_V210(&video_frame, &dst);
break;
}
default:
NDIlib_recv_free_video_v2(recv_, &video_frame);
throw std::runtime_error("Unsupported NDI color format: " + fourcc_str(info_.fourcc));
}
default:
NDIlib_recv_free_video_v2(recv_, &frame);
throw std::runtime_error("Unsupported NDI color format: " + fourcc_str(info_.fourcc));
NDIlib_recv_free_video_v2(recv_, &video_frame);
return FrameKind::Video;
}
NDIlib_recv_free_video_v2(recv_, &frame);
return true;
if (type == NDIlib_frame_type_audio) {
audio_info.sample_rate = audio_frame.sample_rate;
audio_info.channels = audio_frame.no_channels;
audio_info.samples = audio_frame.no_samples;
audio_info.channel_stride = audio_frame.channel_stride_in_bytes / sizeof(float);
const int total = audio_frame.no_channels * audio_info.channel_stride;
audio_out.assign(audio_frame.p_data, audio_frame.p_data + total);
NDIlib_recv_free_audio_v3(recv_, &audio_frame);
return FrameKind::Audio;
}
return FrameKind::None;
}
private: