summaryrefslogtreecommitdiffhomepage
path: root/cv/camera-dialog.cpp
blob: c3e8f6fe2f66203d7b68c9c0ac6877c889180a10 (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
/* Copyright (c) 2015, Stanislaw Halik <sthalik@misaki.pl>

 * 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 "cv/camera-dialog.hpp"
#include <QDebug>
#include <QMutexLocker>

void camera_dialog::maybe_grab_frame(cv::VideoCapture& cap)
{
    for (int i = 0; i < 60; i++)
    {
        if (cap.grab())
            break;
        portable::sleep(50);
    }
}

camera_dialog::~camera_dialog() {}

void camera_dialog::open_camera_settings(cv::VideoCapture* cap, const QString& camera_name, QMutex* camera_mtx)
{
#ifdef _WIN32
    init_com_threading(com_apartment);

    if (cap)
    {
        QMutexLocker l(camera_mtx);

        if (cap->isOpened())
        {
            maybe_grab_frame(*cap);
            cap->set(cv::CAP_PROP_SETTINGS, 1);
            return;
        }
    }

    if (t.isActive())
        t.stop();

    // don't hog the camera capture
    if (!t.isSingleShot())
        QObject::connect(&t, &QTimer::timeout, [&]() -> void { delete_capture(); });

    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();
#elif defined(__linux)
    int idx = camera_name_to_index(camera_name);
    QProcess::startDetached("qv4l2", QStringList() << "-d" << ("/dev/video" + QString::number(idx)));
#else
    // nothing
#endif
}

#ifdef _WIN32
void camera_dialog::delete_capture()
{
    fake_capture.open("");
}
#endif