diff options
Diffstat (limited to 'opentrack/opencv-camera-dialog.hpp')
-rw-r--r-- | opentrack/opencv-camera-dialog.hpp | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/opentrack/opencv-camera-dialog.hpp b/opentrack/opencv-camera-dialog.hpp index 96c7a643..eac26bde 100644 --- a/opentrack/opencv-camera-dialog.hpp +++ b/opentrack/opencv-camera-dialog.hpp @@ -8,6 +8,8 @@ #pragma once +#if !defined(QT_MOC_RUN) + #include <QTimer> #include <QMutex> #include <QMutexLocker> @@ -15,30 +17,45 @@ #include "opentrack-compat/camera-names.hpp" #ifdef __linux -#include <QProcess> +# include <QProcess> +#else +# include "opentrack-compat/sleep.hpp" #endif -template<typename tracker> +#ifdef _WIN32 +# include <objbase.h> +# include <winerror.h> +# include <windows.h> +#endif + +static void init_com_threading(void) +{ + HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + if (FAILED(hr)) + qDebug() << "failed CoInitializeEx" << hr << "code" << GetLastError(); +} + +static void maybe_grab_frame(cv::VideoCapture& cap) +{ + for (int i = 0; i < 60; i++) + { + if (cap.grab()) + break; + portable::sleep(50); + } +} + class camera_dialog { -#ifdef __linux public: + inline virtual ~camera_dialog() {} +#ifdef __linux void open_camera_settings(cv::VideoCapture *, const QString &camera_name, QMutex *) { int idx = camera_name_to_index(camera_name); QProcess::startDetached("qv4l2", QStringList() << "-d" << ("/dev/video" + QString::number(idx))); } #else - cv::VideoCapture fake_capture; - QTimer t; - -private: - void delete_capture() - { - fake_capture.open(""); - } - -public: void open_camera_settings(cv::VideoCapture* cap, const QString& camera_name, QMutex* camera_mtx) { if (cap) @@ -47,6 +64,8 @@ public: if (cap->isOpened()) { + init_com_threading(); + maybe_grab_frame(*cap); cap->set(cv::CAP_PROP_SETTINGS, 1); return; } @@ -59,14 +78,26 @@ public: if (!t.isSingleShot()) QObject::connect(&t, &QTimer::timeout, [&]() -> void { delete_capture(); }); - t.setSingleShot(true); - t.setInterval(3000); - + init_com_threading(); fake_capture = cv::VideoCapture(camera_name_to_index(camera_name)); + maybe_grab_frame(fake_capture); fake_capture.set(cv::CAP_PROP_SETTINGS, 1); + + t.setSingleShot(true); + t.setInterval(5000); + // HACK: we're not notified when it's safe to close the capture t.start(); } +private: + cv::VideoCapture fake_capture; + QTimer t; + void delete_capture() + { + fake_capture.open(""); + } #endif }; +#endif + |