audio support
This commit is contained in:
+71
-12
@@ -24,6 +24,11 @@ public:
|
||||
int fps_den = 0;
|
||||
};
|
||||
|
||||
struct AudioInfo {
|
||||
int sample_rate = 48000; // DeckLink always delivers 48 kHz
|
||||
int channels = 0;
|
||||
};
|
||||
|
||||
struct DeviceInfo {
|
||||
uint32_t index;
|
||||
std::string name;
|
||||
@@ -31,6 +36,8 @@ public:
|
||||
|
||||
std::vector<DeviceInfo> devices;
|
||||
VideoInfo video_info{};
|
||||
AudioInfo audio_info{};
|
||||
bool has_audio = false;
|
||||
|
||||
DeckLinkReceiver() { enumerate_devices(); }
|
||||
|
||||
@@ -38,6 +45,7 @@ public:
|
||||
if (decklink_input) {
|
||||
decklink_input->StopStreams();
|
||||
decklink_input->DisableVideoInput();
|
||||
if (has_audio) decklink_input->DisableAudioInput();
|
||||
decklink_input->SetCallback(nullptr);
|
||||
decklink_input->Release();
|
||||
}
|
||||
@@ -46,7 +54,8 @@ public:
|
||||
if (selected_device) selected_device->Release();
|
||||
}
|
||||
|
||||
void start_capture(uint32_t device_index) {
|
||||
// audio_channels > 0 enables audio capture at 48 kHz / 32-bit int.
|
||||
void start_capture(uint32_t device_index, int audio_channels = 0) {
|
||||
if (device_index >= raw_devices.size())
|
||||
throw std::runtime_error("Device index out of range");
|
||||
|
||||
@@ -64,6 +73,15 @@ public:
|
||||
bmdVideoInputEnableFormatDetection);
|
||||
if (r != S_OK) throw std::runtime_error("Could not enable video input");
|
||||
|
||||
if (audio_channels > 0) {
|
||||
r = decklink_input->EnableAudioInput(bmdAudioSampleRate48kHz,
|
||||
bmdAudioSampleType32bitInteger,
|
||||
static_cast<uint32_t>(audio_channels));
|
||||
if (r != S_OK) throw std::runtime_error("Could not enable audio input");
|
||||
audio_info.channels = audio_channels;
|
||||
has_audio = true;
|
||||
}
|
||||
|
||||
r = decklink_input->StartStreams();
|
||||
if (r != S_OK) throw std::runtime_error("Could not start streams");
|
||||
}
|
||||
@@ -75,24 +93,40 @@ public:
|
||||
}
|
||||
|
||||
// Blocks until a fresh frame arrives or g_running goes false.
|
||||
// Copies frame data into dst with MXL stride. Returns false on shutdown.
|
||||
bool wait_for_frame(uint8_t* dst, uint32_t dst_stride, int width, int height) {
|
||||
// Copies video into video_dst and (if audio_dst != nullptr) deinterleaved
|
||||
// float32 audio into audio_dst[channel * max_samples + sample].
|
||||
// Returns false on shutdown.
|
||||
bool wait_for_frame(uint8_t* video_dst, uint32_t dst_stride, int width, int height,
|
||||
float* audio_dst = nullptr, int max_samples = 0, int* samples_written = nullptr) {
|
||||
std::unique_lock<std::mutex> lk(mutex);
|
||||
frame_cv.wait(lk, [this] {
|
||||
return frame_ready || !dmf::g_running.load(std::memory_order_relaxed);
|
||||
});
|
||||
if (!dmf::g_running.load(std::memory_order_relaxed)) return false;
|
||||
|
||||
const uint32_t src_stride = frame_row_bytes;
|
||||
const int rows = std::min(frame_height, height);
|
||||
const uint32_t copy_bytes = std::min(src_stride, dst_stride);
|
||||
|
||||
std::memset(dst, 0, static_cast<size_t>(dst_stride) * static_cast<size_t>(height));
|
||||
// Video copy
|
||||
const uint32_t src_stride = frame_row_bytes;
|
||||
const int rows = std::min(frame_height, height);
|
||||
const uint32_t copy_bytes = std::min(src_stride, dst_stride);
|
||||
std::memset(video_dst, 0, static_cast<size_t>(dst_stride) * static_cast<size_t>(height));
|
||||
const uint8_t* src = frame_buffer.data();
|
||||
uint8_t* d = dst;
|
||||
uint8_t* d = video_dst;
|
||||
for (int y = 0; y < rows; ++y, src += src_stride, d += dst_stride)
|
||||
std::memcpy(d, src, copy_bytes);
|
||||
|
||||
// Audio copy — planar float32: channel c starts at audio_dst + c * max_samples
|
||||
if (audio_dst && max_samples > 0 && samples_written) {
|
||||
const int n = std::min(audio_samples, max_samples);
|
||||
const int ch = audio_info.channels;
|
||||
*samples_written = n;
|
||||
for (int c = 0; c < ch; ++c)
|
||||
std::memcpy(audio_dst + c * max_samples,
|
||||
audio_buffer.data() + c * audio_samples,
|
||||
static_cast<size_t>(n) * sizeof(float));
|
||||
} else if (samples_written) {
|
||||
*samples_written = 0;
|
||||
}
|
||||
|
||||
frame_ready = false;
|
||||
return true;
|
||||
}
|
||||
@@ -121,7 +155,7 @@ private:
|
||||
const int g = std::gcd(owner.video_info.fps_num, owner.video_info.fps_den);
|
||||
if (g > 1) { owner.video_info.fps_num /= g; owner.video_info.fps_den /= g; }
|
||||
}
|
||||
owner.frame_ready = false;
|
||||
owner.frame_ready = false;
|
||||
owner.frame_buffer.clear();
|
||||
owner.format_detected = true;
|
||||
}
|
||||
@@ -139,7 +173,7 @@ private:
|
||||
|
||||
HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
|
||||
IDeckLinkVideoInputFrame* video_frame,
|
||||
IDeckLinkAudioInputPacket* /*audio_packet*/) override
|
||||
IDeckLinkAudioInputPacket* audio_packet) override
|
||||
{
|
||||
if (!video_frame) return S_OK;
|
||||
|
||||
@@ -157,12 +191,33 @@ private:
|
||||
const size_t sz = static_cast<size_t>(row_bytes) * static_cast<size_t>(fh);
|
||||
|
||||
std::lock_guard<std::mutex> lk(owner.mutex);
|
||||
|
||||
// Video
|
||||
if (owner.frame_buffer.size() < sz) owner.frame_buffer.resize(sz);
|
||||
std::memcpy(owner.frame_buffer.data(), src, sz);
|
||||
owner.frame_row_bytes = row_bytes;
|
||||
owner.frame_width = fw;
|
||||
owner.frame_height = fh;
|
||||
owner.frame_ready = true;
|
||||
|
||||
// Audio — deinterleave int32 → float32 planar under the same lock
|
||||
if (owner.has_audio && audio_packet) {
|
||||
void* asrc = nullptr;
|
||||
audio_packet->GetBytes(&asrc);
|
||||
const long nb = audio_packet->GetSampleFrameCount();
|
||||
const int ch = owner.audio_info.channels;
|
||||
if (asrc && nb > 0 && ch > 0) {
|
||||
const auto* in = static_cast<const int32_t*>(asrc);
|
||||
const size_t need = static_cast<size_t>(ch) * static_cast<size_t>(nb);
|
||||
if (owner.audio_buffer.size() < need) owner.audio_buffer.resize(need);
|
||||
for (long s = 0; s < nb; ++s)
|
||||
for (int c = 0; c < ch; ++c)
|
||||
owner.audio_buffer[static_cast<size_t>(c) * static_cast<size_t>(nb) + static_cast<size_t>(s)]
|
||||
= static_cast<float>(in[s * ch + c]) / 2147483648.0f;
|
||||
owner.audio_samples = static_cast<int>(nb);
|
||||
}
|
||||
}
|
||||
|
||||
owner.frame_ready = true;
|
||||
}
|
||||
|
||||
buf->EndAccess(bmdBufferAccessRead);
|
||||
@@ -199,6 +254,10 @@ private:
|
||||
int frame_height = 0;
|
||||
bool frame_ready = false;
|
||||
|
||||
// Audio state — planar float32: channel c at audio_buffer[c * audio_samples + s]
|
||||
std::vector<float> audio_buffer;
|
||||
int audio_samples = 0;
|
||||
|
||||
void enumerate_devices() {
|
||||
IDeckLinkIterator* it = CreateDeckLinkIteratorInstance();
|
||||
if (!it) throw std::runtime_error("DeckLink drivers not installed");
|
||||
|
||||
Reference in New Issue
Block a user