Separate Qt capture control from the UI

Compared with 1214e72:

- Move device discovery, channel lifecycle, signal monitoring, frame delivery, preview submission, runtime logging, and shutdown coordination from MainWindow into a dedicated CaptureController.

- Add a GvfgClient RAII wrapper and gvfg_api namespace so one application module owns all direct calls to the public capture and preview C APIs.

- Keep MainWindow focused on widget creation, user actions, option synchronization, preview-window presentation, and UI state updates through controller signals.

- Preserve per-channel video workers, exact frame release handling, preview delivery accounting, startup timing, signal recovery, and diagnostic reporting while removing their implementation details from the UI class.

- Preserve the audio read-copy-release path, bounded playback queue, playback-thread QAudioSink ownership, recovery behavior, and byte/frame accounting inside the controller.

- Register the new source files in the standalone Qt sample target and document the resulting UI, capture-control, and API-adapter source boundaries.

Validation: git diff --cached --check passed before commit. No build, runtime launch, capture, audio playback, device-event, shutdown, or hardware validation was run.
This commit is contained in:
Mark.lin
2026-09-11 16:03:26 +08:00
parent 1214e720d2
commit 57ad00c40e
8 changed files with 1776 additions and 1669 deletions
+4
View File
@@ -95,6 +95,10 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Multimedia)
set(GVFG_QT_PREVIEW_SOURCES
main.cpp
capture_controller.cpp
capture_controller.h
gvfg_api.cpp
gvfg_api.h
mainwindow.cpp
mainwindow.h
mainwindow.ui
File diff suppressed because it is too large Load Diff
+136
View File
@@ -0,0 +1,136 @@
#pragma once
#include "gvfg_api.h"
#include <gvfg_capture.h>
#include <gvfg_preview.h>
#include <QFile>
#include <QObject>
#include <QStringList>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <mutex>
#include <thread>
#include <vector>
class QTimer;
class CaptureController final : public QObject
{
Q_OBJECT
public:
explicit CaptureController(QObject *parent = nullptr);
~CaptureController() override;
void setSelectedDeviceIndex(int index) { selectedDeviceIndex_ = index; }
void setChannelOptions(int channel, bool zeroCopy, gvfg_pixel_format_t format, bool audioEnabled);
void setPreviewTarget(int channel, void *nativeWindow);
void setPreviewVisible(int channel, bool visible);
bool deviceOpen() const { return client_.valid(); }
bool channelOpened(int channel) const;
bool channelRunning(int channel) const;
bool frameAvailable(int channel) const;
bool cachedSignalStatus(int channel, gvfg_signal_status_t *status) const;
QString sdkVersion() const;
public slots:
void refreshDevices();
bool openDevice();
void closeDevice();
bool applyOutputFormat(int channel);
void startCapture(int channel);
void stopCapture(int channel);
void stopAllCaptures();
bool applyPreview(int channel);
void processPendingEvents();
void updateSignalStatus(bool queryHardware = true);
signals:
void devicesChanged(const QStringList &names);
void stateChanged();
void statusChanged(const QString &text);
void logMessage(const QString &message);
void errorOccurred(const QString &apiName, gvfg_status_t status, int channel, const QString &detail);
void previewSourceSizeChanged(int channel, int width, int height);
void previewShowRequested(int channel);
void previewCloseRequested(int channel);
private:
struct ChannelRuntime
{
struct AudioPacket { std::vector<uint8_t> pcm; };
bool opened = false;
bool zeroCopy = false;
bool requestedAudio = false;
gvfg_pixel_format_t requestedFormat = GVFG_PIXFMT_YUY2;
void *previewTarget = nullptr;
gvfg_preview_handle previewHandle = nullptr;
std::atomic<bool> running{false}, stopRequested{false}, signalConnected{false};
std::mutex signalMutex;
std::condition_variable signalReady;
std::atomic<bool> frameAvailable{false}, previewVisible{false}, captureThreadExited{true};
std::thread captureThread, audioThread, audioPlaybackThread;
std::mutex audioQueueMutex;
std::condition_variable audioQueueReady;
std::deque<AudioPacket> audioQueue;
bool audioEnabled = false;
gvfg_audio_format_t audioFormat{};
uint64_t audioReceivedFrames = 0, audioReceivedBytes = 0, audioAcceptedBytes = 0;
uint64_t audioQueuedBytes = 0, audioDroppedBytes = 0, audioCancelledBytes = 0;
uint64_t audioFailedBytes = 0, audioIdGaps = 0, audioIdResets = 0, audioLastId = 0;
std::atomic<uint64_t> videoReceived{0}, videoSubmitted{0}, videoSkipped{0}, videoFailed{0};
std::atomic<uint64_t> videoIdGaps{0}, videoIdResets{0}, videoLastId{0};
gvfg_preview_delivery_stats_t previewBaseline{};
std::chrono::steady_clock::time_point startupStartTime{};
double startupStartCallMs = 0.0;
uint64_t lastLoggedVideoIssues = 0, lastLoggedAudioIssues = 0;
qint64 lastDeliveryLogMs = 0;
std::atomic<double> getFrameAverageMs{0.0}, getFrameMaximumMs{0.0}, getFrameWindowMaximumMs{0.0};
std::atomic<uint64_t> getFrameSamples{0};
uint64_t previewFailureCount = 0;
gvfg_signal_status_t cachedSignalStatus{};
bool haveCachedSignalStatus = false;
QString lastLoggedInputStatus;
#if GVFG_INTERNAL_DIAGNOSTICS
uint64_t lastDebugDmaErrors = 0;
bool haveDebugBaseline = false;
#endif
};
void reportError(const QString &apiName, gvfg_status_t status, int channel = -1);
void appendLog(const QString &message);
bool openChannel(int channel);
void openLogFile();
bool openLogFilePart();
void rotateLogFileIfNeeded();
void writeLogFileLine(const QString &line);
#if GVFG_INTERNAL_DIAGNOSTICS
void writeDiagnosticSnapshot(const QString &statusText);
#endif
void captureReadLoop(int channel);
void audioReadLoop(int channel);
void audioPlaybackLoop(int channel);
void joinCaptureThread(int channel);
void joinAudioThread(int channel);
void logDeliveryStatus(int channel, bool finalSnapshot = false);
static constexpr qint64 kMaxLogFileBytes = 20ll * 1024ll * 1024ll;
std::array<gvfg_device_info_t, GVFG_MAX_DEVICES> devices_{};
int deviceCount_ = 0;
GvfgClient client_;
int selectedDeviceIndex_ = -1;
std::array<ChannelRuntime, 2> channels_{};
QTimer *runtimeStatusTimer_ = nullptr;
QString lastSignalStatusText_;
QFile logFile_;
std::mutex logFileMutex_;
QString logDirPath_, logFilePath_, logSessionStamp_;
int logPartIndex_ = 1;
};
+47
View File
@@ -0,0 +1,47 @@
#include "gvfg_api.h"
GvfgClient::~GvfgClient() { destroy(); }
gvfg_status_t GvfgClient::create() { return handle_ ? GVFG_OK : gvfg_create(&handle_); }
gvfg_status_t GvfgClient::destroy()
{
if (!handle_) return GVFG_OK;
const gvfg_status_t status = gvfg_destroy(handle_);
handle_ = nullptr;
return status;
}
gvfg_status_t GvfgClient::openChannel(int device, int channel) { return gvfg_open_channel(handle_, device, channel); }
gvfg_status_t GvfgClient::setZeroCopy(int channel, int enabled) { return gvfg_set_channel_zero_copy_enabled(handle_, channel, enabled); }
gvfg_status_t GvfgClient::setVideoFormat(int channel, gvfg_pixel_format_t format) { return gvfg_set_channel_video_format(handle_, channel, format); }
gvfg_status_t GvfgClient::setAudioEnabled(int channel, int enabled) { return gvfg_set_channel_audio_enabled(handle_, channel, enabled); }
gvfg_status_t GvfgClient::getAudioFormat(int channel, gvfg_audio_format_t *format) const { return gvfg_get_channel_audio_format(handle_, channel, format); }
gvfg_status_t GvfgClient::startChannel(int channel) { return gvfg_start_channel(handle_, channel); }
gvfg_status_t GvfgClient::stopChannel(int channel) { return gvfg_stop_channel(handle_, channel); }
gvfg_status_t GvfgClient::readFrame(int channel, gvfg_frame_t *frame, uint32_t timeoutMs) { return gvfg_read_channel_frame(handle_, channel, frame, timeoutMs); }
gvfg_status_t GvfgClient::releaseFrame(int channel, const gvfg_frame_t *frame) { return gvfg_release_channel_frame(handle_, channel, frame); }
gvfg_status_t GvfgClient::readAudioFrame(int channel, gvfg_audio_frame_t *frame, uint32_t timeoutMs) { return gvfg_read_channel_audio_frame(handle_, channel, frame, timeoutMs); }
gvfg_status_t GvfgClient::releaseAudioFrame(int channel, const gvfg_audio_frame_t *frame) { return gvfg_release_channel_audio_frame(handle_, channel, frame); }
gvfg_status_t GvfgClient::pollEvent(int channel, gvfg_event_t *event, uint32_t timeoutMs) { return gvfg_poll_channel_event(handle_, channel, event, timeoutMs); }
gvfg_status_t GvfgClient::getSignalStatus(int channel, gvfg_signal_status_t *status) const { return gvfg_get_channel_signal_status(handle_, channel, status); }
gvfg_status_t GvfgClient::getRuntimeInfo(int channel, gvfg_runtime_info_t *info) const { return gvfg_get_channel_runtime_info(handle_, channel, info); }
gvfg_status_t GvfgClient::getLastError(int channel, char *message, uint32_t messageSize) const { return gvfg_get_channel_last_error_detail(handle_, channel, message, messageSize); }
gvfg_status_t GvfgClient::getDebugStats(int channel, gvfg_debug_backend_stats_t *stats) const { return gvfg_debug_get_channel_backend_stats(handle_, channel, stats); }
namespace gvfg_api
{
int enumerateDevices(gvfg_device_info_t *devices, int capacity) { return gvfg_enumerate_devices(devices, capacity); }
const char *version() { return gvfg_get_version(); }
const char *pixelFormatName(int format) { return gvfg_pixel_format_name(format); }
const char *statusText(gvfg_status_t status) { return gvfg_strerror(status); }
gvfg_preview_status_t previewCreate(gvfg_preview_handle *handle) { return gvfg_preview_create(handle); }
gvfg_preview_status_t previewDestroy(gvfg_preview_handle handle) { return gvfg_preview_destroy(handle); }
gvfg_preview_status_t previewAttachWindow(gvfg_preview_handle handle, void *window) { return gvfg_preview_attach_window(handle, window); }
gvfg_preview_status_t previewPrepare(gvfg_preview_handle handle, int width, int height, int bitDepth) { return gvfg_preview_prepare(handle, width, height, bitDepth); }
gvfg_preview_status_t previewRenderFrame(gvfg_preview_handle handle, const gvfg_preview_frame_t *frame) { return gvfg_preview_render_frame(handle, frame); }
gvfg_preview_status_t previewClear(gvfg_preview_handle handle) { return gvfg_preview_clear(handle); }
gvfg_preview_status_t previewWaitIdle(gvfg_preview_handle handle, uint32_t timeoutMs) { return gvfg_preview_wait_idle(handle, timeoutMs); }
gvfg_preview_status_t previewGetInfo(gvfg_preview_handle handle, gvfg_preview_info_t *info) { return gvfg_preview_get_info(handle, info); }
gvfg_preview_status_t previewGetStats(gvfg_preview_handle handle, gvfg_preview_stats_t *stats) { return gvfg_preview_get_stats(handle, stats); }
gvfg_preview_status_t previewGetDeliveryStats(gvfg_preview_handle handle, gvfg_preview_delivery_stats_t *stats) { return gvfg_preview_get_delivery_stats(handle, stats); }
const char *previewStatusText(gvfg_preview_status_t status) { return gvfg_preview_strerror(status); }
}
+59
View File
@@ -0,0 +1,59 @@
#pragma once
#include <gvfg_capture.h>
#include <gvfg_debug.h>
#include <gvfg_preview.h>
class GvfgClient final
{
public:
GvfgClient() = default;
~GvfgClient();
GvfgClient(const GvfgClient &) = delete;
GvfgClient &operator=(const GvfgClient &) = delete;
bool valid() const { return handle_ != nullptr; }
gvfg_status_t create();
gvfg_status_t destroy();
gvfg_status_t openChannel(int device, int channel);
gvfg_status_t setZeroCopy(int channel, int enabled);
gvfg_status_t setVideoFormat(int channel, gvfg_pixel_format_t format);
gvfg_status_t setAudioEnabled(int channel, int enabled);
gvfg_status_t getAudioFormat(int channel, gvfg_audio_format_t *format) const;
gvfg_status_t startChannel(int channel);
gvfg_status_t stopChannel(int channel);
gvfg_status_t readFrame(int channel, gvfg_frame_t *frame, uint32_t timeoutMs);
gvfg_status_t releaseFrame(int channel, const gvfg_frame_t *frame);
gvfg_status_t readAudioFrame(int channel, gvfg_audio_frame_t *frame, uint32_t timeoutMs);
gvfg_status_t releaseAudioFrame(int channel, const gvfg_audio_frame_t *frame);
gvfg_status_t pollEvent(int channel, gvfg_event_t *event, uint32_t timeoutMs);
gvfg_status_t getSignalStatus(int channel, gvfg_signal_status_t *status) const;
gvfg_status_t getRuntimeInfo(int channel, gvfg_runtime_info_t *info) const;
gvfg_status_t getLastError(int channel, char *message, uint32_t messageSize) const;
gvfg_status_t getDebugStats(int channel, gvfg_debug_backend_stats_t *stats) const;
private:
gvfg_handle handle_ = nullptr;
};
// This namespace is the only application module that calls the public GVFG C
// ABI directly. UI and capture-control code depend on these adapters instead.
namespace gvfg_api
{
int enumerateDevices(gvfg_device_info_t *devices, int capacity);
const char *version();
const char *pixelFormatName(int format);
const char *statusText(gvfg_status_t status);
gvfg_preview_status_t previewCreate(gvfg_preview_handle *handle);
gvfg_preview_status_t previewDestroy(gvfg_preview_handle handle);
gvfg_preview_status_t previewAttachWindow(gvfg_preview_handle handle, void *window);
gvfg_preview_status_t previewPrepare(gvfg_preview_handle handle, int width, int height, int bitDepth);
gvfg_preview_status_t previewRenderFrame(gvfg_preview_handle handle, const gvfg_preview_frame_t *frame);
gvfg_preview_status_t previewClear(gvfg_preview_handle handle);
gvfg_preview_status_t previewWaitIdle(gvfg_preview_handle handle, uint32_t timeoutMs);
gvfg_preview_status_t previewGetInfo(gvfg_preview_handle handle, gvfg_preview_info_t *info);
gvfg_preview_status_t previewGetStats(gvfg_preview_handle handle, gvfg_preview_stats_t *stats);
gvfg_preview_status_t previewGetDeliveryStats(gvfg_preview_handle handle, gvfg_preview_delivery_stats_t *stats);
const char *previewStatusText(gvfg_preview_status_t status);
}
File diff suppressed because it is too large Load Diff
+8 -116
View File
@@ -1,31 +1,16 @@
#pragma once
#include <gvfg_capture.h>
#include <gvfg_preview.h>
#include <QFile>
#include <QString>
#include <QWidget>
#include <array>
#include <atomic>
#include <condition_variable>
#include <chrono>
#include <cstdint>
#include <deque>
#include <mutex>
#include <thread>
#include <vector>
class CaptureController;
class QCloseEvent;
class PreviewWindow;
class QTimer;
QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow final : public QWidget
@@ -40,110 +25,17 @@ protected:
void closeEvent(QCloseEvent *event) override;
private:
struct ChannelRuntime
{
bool opened = false;
gvfg_preview_handle previewHandle = nullptr;
std::atomic<bool> running{false};
std::atomic<bool> stopRequested{false};
std::atomic<bool> signalConnected{false};
std::mutex signalMutex;
std::condition_variable signalReady;
std::atomic<bool> frameAvailable{false};
std::atomic<bool> previewVisible{false};
std::atomic<bool> captureThreadExited{true};
std::thread captureThread;
std::thread audioThread;
std::thread audioPlaybackThread;
std::mutex audioQueueMutex;
std::condition_variable audioQueueReady;
struct AudioPacket { std::vector<uint8_t> pcm; };
std::deque<AudioPacket> audioQueue;
bool audioEnabled = false;
gvfg_audio_format_t audioFormat{};
// Audio accounting is protected by audioQueueMutex. Accepted means
// written to QAudioSink, not physically played by the device.
uint64_t audioReceivedFrames = 0;
uint64_t audioReceivedBytes = 0;
uint64_t audioAcceptedBytes = 0;
uint64_t audioQueuedBytes = 0;
uint64_t audioDroppedBytes = 0;
uint64_t audioCancelledBytes = 0;
uint64_t audioFailedBytes = 0;
uint64_t audioIdGaps = 0;
uint64_t audioIdResets = 0;
uint64_t audioLastId = 0;
std::atomic<uint64_t> videoReceived{0}, videoSubmitted{0}, videoSkipped{0}, videoFailed{0};
std::atomic<uint64_t> videoIdGaps{0}, videoIdResets{0};
std::atomic<uint64_t> videoLastId{0};
gvfg_preview_delivery_stats_t previewBaseline{};
std::chrono::steady_clock::time_point startupStartTime{};
double startupStartCallMs = 0.0;
uint64_t lastLoggedVideoIssues = 0, lastLoggedAudioIssues = 0;
qint64 lastDeliveryLogMs = 0;
std::atomic<double> getFrameAverageMs{0.0};
std::atomic<double> getFrameMaximumMs{0.0};
std::atomic<double> getFrameWindowMaximumMs{0.0};
std::atomic<uint64_t> getFrameSamples{0};
uint64_t previewFailureCount = 0;
gvfg_signal_status_t cachedSignalStatus{};
bool haveCachedSignalStatus = false;
QString lastLoggedInputStatus;
#if GVFG_INTERNAL_DIAGNOSTICS
uint64_t lastDebugDmaErrors = 0;
bool haveDebugBaseline = false;
#endif
};
void refreshDevices();
void showPreviewWindow(int channel);
void showFullscreenPreviewWindow(int channel);
bool openDevice();
bool openChannel(int channel);
bool applyOutputFormat(int channel);
void syncControllerOptions(int channel);
void showPreviewWindow(int channel, bool fullscreen);
void updateOutputFormatOptions(int changedChannel = -1);
void closeDevice();
void startCapture(int channel);
void stopCapture(int channel);
void stopAllCaptures();
bool applyPreview(int channel);
void updatePreviewSourceSize(int channel, const gvfg_signal_status_t &signal);
void processPendingEvents();
void updateSignalStatus(bool queryHardware = true);
void updateUiState();
void showError(const QString &apiName, gvfg_status_t status, int channel = -1);
void openLogFile();
bool openLogFilePart();
void rotateLogFileIfNeeded();
void writeLogFileLine(const QString &line);
#if GVFG_INTERNAL_DIAGNOSTICS
void writeDiagnosticSnapshot(const QString &statusText);
#endif
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);
void logDeliveryStatus(int channel, bool finalSnapshot = false);
static constexpr qint64 kMaxLogFileBytes = 20ll * 1024ll * 1024ll;
void showError(const QString &apiName, gvfg_status_t status, int channel,
const QString &detail);
void appendLogLine(const QString &line);
Ui::MainWindow *ui_ = nullptr;
CaptureController *controller_ = nullptr;
std::array<PreviewWindow *, 2> previewWindows_{};
std::array<gvfg_device_info_t, GVFG_MAX_DEVICES> devices_{};
int deviceCount_ = 0;
gvfg_handle handle_ = nullptr;
int selectedDeviceIndex_ = -1;
std::array<ChannelRuntime, 2> channels_{};
QTimer *runtimeStatusTimer_ = nullptr;
QString lastSignalStatusText_;
QFile logFile_;
std::mutex logFileMutex_;
QString logDirPath_;
QString logFilePath_;
QString logSessionStamp_;
int logPartIndex_ = 1;
};
QWidget *createMainWindow();