summaryrefslogtreecommitdiffhomepage
path: root/cv/video-property-page.cpp
blob: f7126a85a1bc1f6a870c3e4a71a63c66ba231e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (c) 2016 Stanislaw Halik
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#include "video-property-page.hpp"

#ifdef _WIN32

#include "compat/camera-names.hpp"
#include "compat/sleep.hpp"
#include "compat/run-in-thread.hpp"
#include "opentrack-library-path.h"

#include <cstring>

#include <opencv2/videoio.hpp>

#include <QApplication>
#include <QProcess>
#include <QThread>
#include <QMessageBox>

#include <QDebug>

#include <dshow.h>

#define CHECK(expr) if (FAILED(hr = (expr))) { qDebug() << QLatin1String(#expr) << hr; goto done; }
#define CHECK2(expr) if (!(expr)) { qDebug() << QLatin1String(#expr); goto done; }

bool video_property_page::show_from_capture(cv::VideoCapture& cap, int /*index */)
{
    cap.set(cv::CAP_PROP_SETTINGS, 0);
    return true;
}

struct prop_settings_worker final : QThread
{
    prop_settings_worker(int idx);
    ~prop_settings_worker() override;
    void _open_prop_page();
    void run() override;

    cv::VideoCapture cap;
    int _idx = -1;
};

prop_settings_worker::prop_settings_worker(int idx)
{
    if (cap.get(cv::CAP_PROP_SETTINGS) < 0)
        run_in_thread_async(qApp, []() {
            QMessageBox msg;
            msg.setTextFormat(Qt::RichText);
            msg.setWindowTitle("Camera properties");

            static const char* uri = "https://github.com/opentrack/opencv/tree/fork";

            msg.setText(QString("<p>Must use the opencv fork.</p>"
                                "<p>See &lt;<a href='%1'>%1</a>&gt;</p>").arg(uri));
            msg.setStandardButtons(QMessageBox::Cancel);
            msg.setIcon(QMessageBox::Warning);
            msg.exec();
        });
    else if (cap.get(cv::CAP_PROP_SETTINGS > 0))
        run_in_thread_async(qApp, []() {
            QMessageBox::warning(nullptr,
                                 "Camera properties",
                                 "Dialog already opened",
                                 QMessageBox::Cancel,
                                 QMessageBox::NoButton);
        });
    else
    {
        _idx = idx;
        // DON'T MOVE IT
        // ps3 eye will reset to default settings if done from another thread
        _open_prop_page();
    }
}

void prop_settings_worker::_open_prop_page()
{
    cap.open(_idx);

    if (cap.isOpened())
    {
        cv::Mat tmp;

        for (unsigned k = 0; k < 2000/50; k++)
        {
            if (cap.read(tmp))
                goto ok;
            portable::sleep(50);
        }
    }

    qDebug() << "property-page: can't open camera";
    _idx = -1;

    return;

ok:
    portable::sleep(100);

    qDebug() << "property-page: opening for" << _idx;
    cap.set(cv::CAP_PROP_SETTINGS, 0);
}

prop_settings_worker::~prop_settings_worker()
{
    if (_idx != -1)
    {
        // ax filter is race condition-prone
        portable::sleep(250);
        cap.release();
        // idem
        portable::sleep(250);

        qDebug() << "property-page: closed" << _idx;
    }
}

void prop_settings_worker::run()
{
    if (_idx != -1)
    {
        while (cap.get(cv::CAP_PROP_SETTINGS) > 0)
            portable::sleep(1000);
    }

    connect(this, &QThread::finished, this, &QObject::deleteLater);
}

bool video_property_page::show(int idx)
{
    auto thread = new prop_settings_worker(idx);
    thread->start();

    return true;
}

#elif defined(__linux)
#   include <QProcess>
#   include "compat/camera-names.hpp"

bool video_property_page::show(int idx)
{
    const QList<QString> camera_names(get_camera_names());

    if (idx >= 0 && idx < camera_names.size())
        return QProcess::startDetached("qv4l2", QStringList() << "-d" << camera_names[idx]);
    else
        return false;
}

bool video_property_page::show_from_capture(cv::VideoCapture&, int idx)
{
    return show(idx);
}
#else
bool video_property_page::show(int) { return false; }
bool video_property_page::show_from_capture(cv::VideoCapture&, int) { return false; }
#endif