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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
#include "module.hpp"
#include "compat/library-path.hpp"
#include "compat/sleep.hpp"
#include "compat/run-in-thread.hpp"
#include <cstddef>
#include <QCoreApplication>
#include <QMessageBox>
#include <libusb.h>
#ifdef __GNUG__
# pragma clang diagnostic ignored "-Wcast-qual"
#endif
int device_count()
{
libusb_context * ctx = nullptr;
libusb_device **list = nullptr;
ssize_t sz = 0;
int rc = 0, cnt = 0;
constexpr int vendor_id = 0x1415;
constexpr int product_id = 0x2000;
rc = libusb_init(&ctx);
if (rc)
goto end;
sz = libusb_get_device_list(ctx, &list);
if (sz < 0)
goto end;
for (int i = 0; i < sz; ++i) {
libusb_device *device = list[i];
libusb_device_descriptor desc = {};
if (libusb_get_device_descriptor(device, &desc))
goto end;
if (desc.idVendor == vendor_id && desc.idProduct == product_id)
cnt++;
}
end:
if (list)
libusb_free_device_list(list, 1);
if (ctx)
libusb_exit(ctx);
return cnt;
}
bool check_device_exists()
{
static bool ret = device_count() > 0;
return ret;
}
static const QString camera_name = QStringLiteral("PS3 Eye open driver");
std::vector<QString> ps3eye_camera_::camera_names() const
{
if (check_device_exists())
return { camera_name };
else
return {};
}
std::unique_ptr<camera> ps3eye_camera_::make_camera(const QString& name)
{
if (name == camera_name && check_device_exists())
return std::make_unique<ps3eye_camera>();
else
return {};
}
bool ps3eye_camera_::show_dialog(const QString&)
{
// TODO
return false;
}
bool ps3eye_camera_::can_show_dialog(const QString&)
{
return false;
}
ps3eye_camera::ps3eye_camera()
{
if (!shm.success())
return;
static const QString library_path(OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH);
wrapper.setWorkingDirectory(library_path);
#ifdef _WIN32
wrapper.setProgram("\"ps3eye-subprocess.exe\"");
#else
wrapper.setProgram("ps3eye-subprocess");
#endif
}
ps3eye_camera::~ps3eye_camera()
{
stop();
}
void ps3eye_camera::stop()
{
open = false;
if (wrapper.state() != QProcess::NotRunning)
{
if (shm.success())
{
volatile auto& ptr = *(ps3eye::shm*)shm.ptr();
ptr.in.do_exit = true;
if (!wrapper.waitForFinished(500))
wrapper.kill();
wrapper.waitForFinished(-1);
}
else
{
wrapper.kill();
wrapper.waitForFinished(-1);
}
}
}
bool ps3eye_camera::start(info& args)
{
if (!shm.success())
return false;
volatile auto& ptr = *(ps3eye::shm*)shm.ptr();
using mode = ps3eye::shm_in::mode;
open = false;
fr = {};
fr.channels = 3;
fr.channel_size = 1;
if (!args.width || args.width > 320)
{
ptr.in.resolution = mode::vga;
fr.width = 640; fr.height = 480;
}
else
{
ptr.in.resolution = mode::qvga;
fr.width = 320; fr.height = 240;
}
ptr.in.framerate = (uint8_t)std::clamp(args.fps, 30, 187);
ptr.in.gain = (uint8_t)s.gain;
ptr.in.exposure = (uint8_t)s.exposure;
wrapper.start();
constexpr int sleep_ms = 10, max_sleeps = 5000/sleep_ms;
for (int i = 0; i < max_sleeps; i++)
{
if (ptr.out.timecode > 0)
goto ok;
portable::sleep(sleep_ms);
}
run_in_thread_async(qApp, [&]() {
QString error;
if (ptr.out.error_string[0] == '\0')
error = "Unknown error";
else
error = QString::fromLatin1((const char*)ptr.out.error_string,
strnlen((const char*)ptr.out.error_string, sizeof(ptr.out.error_string)));
QMessageBox::critical(nullptr, "Can't open camera", "PS3 Eye driver error: " + error, QMessageBox::Close);
});
return false;
ok:
open = true;
return true;
}
std::tuple<const frame&, bool> ps3eye_camera::get_frame()
{
volatile auto& ptr = *(ps3eye::shm*)shm.ptr();
constexpr int sleep_ms = 10;
constexpr int max_sleeps = 2000/sleep_ms;
if (!open)
goto fail;
for (int i = 0; i < max_sleeps; i++)
{
unsigned new_timecode = ptr.out.timecode;
if (timecode != new_timecode)
{
timecode = new_timecode;
goto ok;
}
portable::sleep(sleep_ms);
}
fail:
stop();
return { fr, false };
static_assert(offsetof(decltype(ptr.out), data_640x480) == offsetof(decltype(ptr.out), data_320x240));
ok:
fr.data = (unsigned char*)ptr.out.data_640x480;
return { fr, true};
}
bool ps3eye_camera::show_dialog()
{
return false;
}
OTR_REGISTER_CAMERA(ps3eye_camera_)
|