Improve GVFG audio playback and event shutdown
- Split PCM acquisition from Qt audio playback in the preview sample. - Copy SDK-owned audio frames into a bounded application queue before release. - Track and display dropped audio queue entries. - Add a dedicated stop event for the SDK event-monitor thread. - Replace periodic event-monitor wakeups with an interruptible infinite wait. - Poll public channel events every 200 ms for responsive format and signal updates. - Preserve finite 200 ms video and audio reads for ownership-safe worker shutdown.
This commit is contained in:
@@ -22,11 +22,14 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr bool kChannel1UiVisible = false;
|
||||
constexpr size_t kMaxQueuedAudioFrames = 10;
|
||||
|
||||
QString logFilePrefix()
|
||||
{
|
||||
@@ -176,7 +179,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
new LogHighlighter(ui_->logEdit->document());
|
||||
ui_->statusLabel->setWordWrap(true);
|
||||
runtimeStatusTimer_ = new QTimer(this);
|
||||
runtimeStatusTimer_->setInterval(1000);
|
||||
runtimeStatusTimer_->setInterval(200);
|
||||
openLogFile();
|
||||
|
||||
connect(ui_->refreshButton, &QPushButton::clicked, this, [this]()
|
||||
@@ -566,6 +569,11 @@ void MainWindow::startCapture(int channelIndex)
|
||||
channel.audioEnabled = audioEnabled;
|
||||
channel.audioFrames.store(0, std::memory_order_relaxed);
|
||||
channel.audioBytes.store(0, std::memory_order_relaxed);
|
||||
channel.audioQueueDrops.store(0, std::memory_order_relaxed);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(channel.audioQueueMutex);
|
||||
channel.audioQueue.clear();
|
||||
}
|
||||
#if GVFG_INTERNAL_DIAGNOSTICS
|
||||
channel.haveDebugBaseline = false;
|
||||
channel.lastDebugDmaErrors = 0;
|
||||
@@ -575,8 +583,12 @@ void MainWindow::startCapture(int channelIndex)
|
||||
channel.captureThread = std::thread([this, channelIndex]()
|
||||
{ captureReadLoop(channelIndex); });
|
||||
if (audioEnabled)
|
||||
{
|
||||
channel.audioPlaybackThread = std::thread([this, channelIndex]()
|
||||
{ audioPlaybackLoop(channelIndex); });
|
||||
channel.audioThread = std::thread([this, channelIndex]()
|
||||
{ audioReadLoop(channelIndex); });
|
||||
}
|
||||
updateUiState();
|
||||
appendLog(audioEnabled
|
||||
? QStringLiteral("CH%1 Started video + audio | %2 Hz %3 ch %4-bit")
|
||||
@@ -594,6 +606,7 @@ void MainWindow::stopCapture(int channelIndex)
|
||||
if (handle_ && channel.running.load(std::memory_order_acquire))
|
||||
{
|
||||
channel.stopRequested.store(true, std::memory_order_release);
|
||||
channel.audioQueueReady.notify_all();
|
||||
joinCaptureThread(channelIndex);
|
||||
joinAudioThread(channelIndex);
|
||||
gvfg_stop_channel(handle_, channelIndex);
|
||||
@@ -757,13 +770,14 @@ void MainWindow::updateSignalStatus(bool queryHardware)
|
||||
.arg(zeroCopy ? QStringLiteral("Zero-copy") : QStringLiteral("Copy"));
|
||||
if (channel.audioEnabled)
|
||||
{
|
||||
statusLines << QStringLiteral("CH%1 Audio | %2 Hz %3 ch %4-bit | frames=%5 bytes=%6")
|
||||
statusLines << QStringLiteral("CH%1 Audio | %2 Hz %3 ch %4-bit | frames=%5 bytes=%6 queue_drops=%7")
|
||||
.arg(channelIndex)
|
||||
.arg(channel.audioFormat.sample_rate)
|
||||
.arg(channel.audioFormat.channels)
|
||||
.arg(channel.audioFormat.bits_per_sample)
|
||||
.arg(static_cast<qulonglong>(channel.audioFrames.load(std::memory_order_relaxed)))
|
||||
.arg(static_cast<qulonglong>(channel.audioBytes.load(std::memory_order_relaxed)));
|
||||
.arg(static_cast<qulonglong>(channel.audioBytes.load(std::memory_order_relaxed)))
|
||||
.arg(static_cast<qulonglong>(channel.audioQueueDrops.load(std::memory_order_relaxed)));
|
||||
}
|
||||
statusLines << QStringLiteral("CH%1 Preview | %2 FPS | %3")
|
||||
.arg(channelIndex)
|
||||
@@ -1197,6 +1211,54 @@ void MainWindow::joinCaptureThread(int channel)
|
||||
}
|
||||
|
||||
void MainWindow::audioReadLoop(int channelIndex)
|
||||
{
|
||||
ChannelRuntime &channel = channels_[channelIndex];
|
||||
while (!channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
gvfg_audio_frame_t frame{};
|
||||
const gvfg_status_t status = gvfg_read_channel_audio_frame(
|
||||
handle_, channelIndex, &frame, 200);
|
||||
if (status == GVFG_ETIMEOUT)
|
||||
continue;
|
||||
if (status != GVFG_OK)
|
||||
{
|
||||
if (!channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex, status]()
|
||||
{ showError(QStringLiteral("gvfg_read_channel_audio_frame"), status, channelIndex); }, Qt::QueuedConnection);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> pcm(frame.data_size);
|
||||
std::memcpy(pcm.data(), frame.data, frame.data_size);
|
||||
const gvfg_status_t releaseStatus =
|
||||
gvfg_release_channel_audio_frame(handle_, channelIndex, &frame);
|
||||
if (releaseStatus != GVFG_OK && !channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex, releaseStatus]()
|
||||
{ showError(QStringLiteral("gvfg_release_channel_audio_frame"), releaseStatus, channelIndex); }, Qt::QueuedConnection);
|
||||
break;
|
||||
}
|
||||
|
||||
if (releaseStatus == GVFG_OK)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(channel.audioQueueMutex);
|
||||
if (channel.audioQueue.size() >= kMaxQueuedAudioFrames)
|
||||
{
|
||||
channel.audioQueue.pop_front();
|
||||
channel.audioQueueDrops.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
channel.audioQueue.push_back(std::move(pcm));
|
||||
}
|
||||
channel.audioQueueReady.notify_one();
|
||||
}
|
||||
}
|
||||
channel.audioQueueReady.notify_all();
|
||||
}
|
||||
|
||||
void MainWindow::audioPlaybackLoop(int channelIndex)
|
||||
{
|
||||
ChannelRuntime &channel = channels_[channelIndex];
|
||||
const gvfg_audio_format_t format = channel.audioFormat;
|
||||
@@ -1244,37 +1306,34 @@ void MainWindow::audioReadLoop(int channelIndex)
|
||||
return;
|
||||
}
|
||||
|
||||
while (!channel.stopRequested.load(std::memory_order_acquire))
|
||||
for (;;)
|
||||
{
|
||||
gvfg_audio_frame_t frame{};
|
||||
const gvfg_status_t status = gvfg_read_channel_audio_frame(
|
||||
handle_, channelIndex, &frame, 200);
|
||||
if (status == GVFG_ETIMEOUT)
|
||||
continue;
|
||||
if (status != GVFG_OK)
|
||||
std::vector<uint8_t> pcm;
|
||||
{
|
||||
if (!channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex, status]()
|
||||
{ showError(QStringLiteral("gvfg_read_channel_audio_frame"), status, channelIndex); }, Qt::QueuedConnection);
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(channel.audioQueueMutex);
|
||||
channel.audioQueueReady.wait(lock, [&channel]() {
|
||||
return channel.stopRequested.load(std::memory_order_acquire) ||
|
||||
!channel.audioQueue.empty();
|
||||
});
|
||||
if (channel.stopRequested.load(std::memory_order_acquire))
|
||||
break;
|
||||
pcm = std::move(channel.audioQueue.front());
|
||||
channel.audioQueue.pop_front();
|
||||
}
|
||||
|
||||
qint64 written = 0;
|
||||
while (written < static_cast<qint64>(frame.data_size) &&
|
||||
while (written < static_cast<qint64>(pcm.size()) &&
|
||||
!channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
const qint64 result = audioOutput->write(
|
||||
static_cast<const char *>(frame.data) + written,
|
||||
static_cast<qint64>(frame.data_size) - written);
|
||||
reinterpret_cast<const char *>(pcm.data()) + written,
|
||||
static_cast<qint64>(pcm.size()) - written);
|
||||
if (result < 0)
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex]()
|
||||
{
|
||||
appendLog(QStringLiteral("CH%1 Audio output write failed").arg(channelIndex));
|
||||
stopCapture(channelIndex); }, Qt::QueuedConnection);
|
||||
gvfg_release_channel_audio_frame(handle_, channelIndex, &frame);
|
||||
audioSink.stop();
|
||||
return;
|
||||
}
|
||||
@@ -1285,18 +1344,10 @@ void MainWindow::audioReadLoop(int channelIndex)
|
||||
}
|
||||
written += result;
|
||||
}
|
||||
if (written == static_cast<qint64>(frame.data_size))
|
||||
if (written == static_cast<qint64>(pcm.size()))
|
||||
{
|
||||
channel.audioFrames.fetch_add(1, std::memory_order_relaxed);
|
||||
channel.audioBytes.fetch_add(frame.data_size, std::memory_order_relaxed);
|
||||
}
|
||||
const gvfg_status_t releaseStatus =
|
||||
gvfg_release_channel_audio_frame(handle_, channelIndex, &frame);
|
||||
if (releaseStatus != GVFG_OK && !channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex, releaseStatus]()
|
||||
{ showError(QStringLiteral("gvfg_release_channel_audio_frame"), releaseStatus, channelIndex); }, Qt::QueuedConnection);
|
||||
break;
|
||||
channel.audioBytes.fetch_add(pcm.size(), std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
audioSink.stop();
|
||||
@@ -1306,4 +1357,9 @@ void MainWindow::joinAudioThread(int channel)
|
||||
{
|
||||
if (channels_[channel].audioThread.joinable())
|
||||
channels_[channel].audioThread.join();
|
||||
channels_[channel].audioQueueReady.notify_all();
|
||||
if (channels_[channel].audioPlaybackThread.joinable())
|
||||
channels_[channel].audioPlaybackThread.join();
|
||||
std::lock_guard<std::mutex> lock(channels_[channel].audioQueueMutex);
|
||||
channels_[channel].audioQueue.clear();
|
||||
}
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
class QCloseEvent;
|
||||
class PreviewWindow;
|
||||
@@ -45,10 +48,15 @@ private:
|
||||
std::atomic<bool> frameAvailable{false};
|
||||
std::thread captureThread;
|
||||
std::thread audioThread;
|
||||
std::thread audioPlaybackThread;
|
||||
std::mutex audioQueueMutex;
|
||||
std::condition_variable audioQueueReady;
|
||||
std::deque<std::vector<uint8_t>> audioQueue;
|
||||
bool audioEnabled = false;
|
||||
gvfg_audio_format_t audioFormat{};
|
||||
std::atomic<uint64_t> audioFrames{0};
|
||||
std::atomic<uint64_t> audioBytes{0};
|
||||
std::atomic<uint64_t> audioQueueDrops{0};
|
||||
std::atomic<double> getFrameAverageMs{0.0};
|
||||
std::atomic<double> getFrameMaximumMs{0.0};
|
||||
std::atomic<double> getFrameWindowMaximumMs{0.0};
|
||||
@@ -94,6 +102,7 @@ private:
|
||||
void appendLog(const QString &message);
|
||||
void captureReadLoop(int channel);
|
||||
void audioReadLoop(int channel);
|
||||
void audioPlaybackLoop(int channel);
|
||||
void joinCaptureThread(int channel);
|
||||
void joinAudioThread(int channel);
|
||||
|
||||
|
||||
@@ -1005,9 +1005,11 @@ namespace gvfg::internal
|
||||
format_change_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
plug_in_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
plug_out_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
monitor_stop_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
if (!dma_event_ || !extra_video_event_ ||
|
||||
(audio_enabled_ && (!audio_event_ || !extra_audio_event_)) ||
|
||||
!format_change_event_ || !plug_in_event_ || !plug_out_event_)
|
||||
!format_change_event_ || !plug_in_event_ || !plug_out_event_ ||
|
||||
!monitor_stop_event_)
|
||||
return false;
|
||||
|
||||
if (!register_event(channelIndex, GIGA_IOCTL_EVENT_VIDEO_DMA, dma_event_))
|
||||
@@ -1084,6 +1086,11 @@ namespace gvfg::internal
|
||||
CloseHandle(plug_out_event_);
|
||||
plug_out_event_ = nullptr;
|
||||
}
|
||||
if (monitor_stop_event_)
|
||||
{
|
||||
CloseHandle(monitor_stop_event_);
|
||||
monitor_stop_event_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool PcieS2mmCaptureSession::start_event_monitoring()
|
||||
@@ -1128,6 +1135,8 @@ namespace gvfg::internal
|
||||
SetEvent(extra_video_event_);
|
||||
if (extra_audio_event_)
|
||||
SetEvent(extra_audio_event_);
|
||||
if (monitor_stop_event_)
|
||||
SetEvent(monitor_stop_event_);
|
||||
if (capture_thread_.joinable())
|
||||
capture_thread_.join();
|
||||
|
||||
@@ -1162,15 +1171,19 @@ namespace gvfg::internal
|
||||
void PcieS2mmCaptureSession::capture_thread_proc()
|
||||
{
|
||||
const uint32_t channel = active_channel();
|
||||
HANDLE waitHandles[] = {format_change_event_, plug_in_event_, plug_out_event_};
|
||||
constexpr DWORD waitHandleCount = 3;
|
||||
HANDLE waitHandles[] = {
|
||||
monitor_stop_event_,
|
||||
format_change_event_,
|
||||
plug_in_event_,
|
||||
plug_out_event_};
|
||||
constexpr DWORD waitHandleCount = 4;
|
||||
|
||||
while (monitoring_.load(std::memory_order_acquire))
|
||||
{
|
||||
const DWORD waitResult = WaitForMultipleObjects(waitHandleCount,
|
||||
waitHandles,
|
||||
FALSE,
|
||||
1000);
|
||||
INFINITE);
|
||||
if (!monitoring_.load(std::memory_order_acquire))
|
||||
break;
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
@@ -1188,14 +1201,16 @@ namespace gvfg::internal
|
||||
switch (waitResult - WAIT_OBJECT_0)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
PCIES2MM_LOG("event: FORMAT_CHANGE");
|
||||
handle_format_change_event(channel);
|
||||
break;
|
||||
case 1:
|
||||
case 2:
|
||||
PCIES2MM_LOG("event: PLUG_IN");
|
||||
handle_plugin_event(channel);
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
PCIES2MM_LOG("event: PLUG_OUT");
|
||||
handle_unplug_event(channel);
|
||||
break;
|
||||
|
||||
@@ -113,6 +113,7 @@ namespace gvfg::internal
|
||||
HANDLE format_change_event_ = nullptr;
|
||||
HANDLE plug_in_event_ = nullptr;
|
||||
HANDLE plug_out_event_ = nullptr;
|
||||
HANDLE monitor_stop_event_ = nullptr;
|
||||
|
||||
pcies2mm_stream_desc_t stream_desc_{};
|
||||
uint32_t stream_bit_depth_ = 8;
|
||||
|
||||
Reference in New Issue
Block a user