Files
GVFG_Standalone/GVFG_Qt_Sample/capture_controller.h
T

127 lines
4.2 KiB
C++

#pragma once
#include "audio_playback.h"
#if GVFG_INTERNAL_DIAGNOSTICS
#include "internal_diagnostics.h"
#endif
#include <gvfg_capture.h>
#include <gvfg_preview.h>
#include <QObject>
#include <QStringList>
#include <array>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <thread>
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 setChannelStatusVisible(int channel, bool visible);
void setPreviewTarget(int channel, void *nativeWindow);
void setPreviewVisible(int channel, bool visible);
bool deviceOpen() const { return handle_ != nullptr; }
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;
QString gigabyteLibVersion() const;
public slots:
void refreshDevices();
void closeDevice();
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 sdiInfoChanged(const QString &text);
void logMessage(const QString &message);
#if GVFG_INTERNAL_DIAGNOSTICS
void diagnosticMessage(const QString &message);
#endif
void previewSourceSizeChanged(int channel, int width, int height);
void previewShowRequested(int channel);
void previewCloseRequested(int channel);
private:
bool openDevice();
bool applyOutputFormat(int channel);
bool prepareAudio(int channel);
void closeDeviceSession(bool clearSelection);
void closeDeviceIfIdle();
struct ChannelRuntime
{
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::atomic<bool> frameAvailable{false}, previewVisible{false}, captureThreadExited{true};
std::thread captureThread, audioThread;
AudioPlayback audioPlayback;
bool audioEnabled = false;
gvfg_audio_format_t audioFormat{};
std::atomic<uint64_t> videoFailed{0};
gvfg_preview_delivery_stats_t previewBaseline{};
uint64_t lastLoggedPreviewFailures = 0;
uint64_t lastLoggedAudioReleaseFailures = 0, lastLoggedAudioOutputFailures = 0;
qint64 lastDeliveryLogMs = 0;
uint64_t previewFailureCount = 0;
gvfg_signal_status_t cachedSignalStatus{};
bool haveCachedSignalStatus = false;
QString cachedSdiInfoText;
QString lastLoggedInputStatus;
};
void reportError(const QString &apiName, gvfg_status_t status, int channel = -1);
void appendLog(const QString &message);
bool openChannel(int channel);
void refreshSdiInfo(int channel);
#if GVFG_INTERNAL_DIAGNOSTICS
void writeDiagnosticSnapshot(const QString &statusText);
#endif
void captureReadLoop(int channel);
void audioReadLoop(int channel);
void joinCaptureThread(int channel);
void joinAudioThread(int channel);
void logDeliveryStatus(int channel, bool finalSnapshot = false);
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_{};
#if GVFG_INTERNAL_DIAGNOSTICS
InternalDiagnostics internalDiagnostics_;
#endif
std::array<bool, 2> channelStatusVisible_{{true, true}};
QTimer *runtimeStatusTimer_ = nullptr;
QString lastSignalStatusText_;
bool closingDevice_ = false;
};