-
Notifications
You must be signed in to change notification settings - Fork 1
/
cameraman.cpp
327 lines (266 loc) · 9.2 KB
/
cameraman.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "cameraman.h"
#include <gst/gst.h>
#include <QObject>
namespace {
gchar* get_launch_line(GstDevice * device)
{
static const char *const ignored_propnames[] =
{ "name", "parent", "direction", "template", "caps", nullptr };
auto element = gst_device_create_element(device, nullptr);
if (!element) {
return nullptr;
}
auto factory = gst_element_get_factory(element);
if (!factory) {
gst_object_unref(element);
return nullptr;
}
if (!gst_plugin_feature_get_name(factory)) {
gst_object_unref(element);
return nullptr;
}
auto launch_line = g_string_new(gst_plugin_feature_get_name(factory));
auto pureelement = gst_element_factory_create(factory, nullptr);
/* get paramspecs and show non-default properties */
guint number_of_properties;
auto properties =
g_object_class_list_properties(G_OBJECT_GET_CLASS(element),
&number_of_properties);
if (properties) {
GValue value = G_VALUE_INIT;
GValue pvalue = G_VALUE_INIT;
for (int i = 0; i < number_of_properties; i++) {
bool ignore = false;
auto property = properties[i];
/* skip some properties */
if ((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) {
continue;
}
for (int j = 0; ignored_propnames[j]; j++) {
if (!g_strcmp0(ignored_propnames[j], property->name)) {
ignore = true;
break;
}
}
if (ignore) {
continue;
}
/* Can't use _param_value_defaults () because sub-classes modify the
* values already.
*/
g_value_init(&value, property->value_type);
g_value_init(&pvalue, property->value_type);
g_object_get_property(G_OBJECT(element), property->name, &value);
g_object_get_property(G_OBJECT(pureelement), property->name, &pvalue);
if (gst_value_compare(&value, &pvalue) != GST_VALUE_EQUAL) {
gchar *valuestr = gst_value_serialize(&value);
if (!valuestr) {
GST_WARNING("Could not serialize property %s:%s",
GST_OBJECT_NAME(element), property->name);
g_free(valuestr);
goto next;
}
g_string_append_printf(launch_line, " %s=%s",
property->name, valuestr);
g_free(valuestr);
}
next:
g_value_unset(&value);
g_value_unset(&pvalue);
}
g_free(properties);
}
gst_object_unref(element);
gst_object_unref(pureelement);
return g_string_free(launch_line, FALSE);
}
void unescape_value_string(gchar * s)
{
if (*s == 0) {
return;
}
if (*s != '"') {
return;
}
/* Find the closing quotes */
gchar* w = s;
s++;
while (*s != '"') {
if (*s == 0) {
break;
}
if (*s == '\\') {
s++;
if (*s == 0) {
break;
}
}
*w = *s;
w++;
s++;
}
*w = 0;
}
template<typename T>
void get_device_header(GstDevice * device, T& desc)
{
auto str = get_launch_line(device);
desc.launchLine = str;
g_free(str);
bool has_id = false;
if (auto element = gst_device_create_element(device, nullptr))
{
GValue value = G_VALUE_INIT;
g_object_get_property(G_OBJECT(element), "device-path", &value);
if (gchar *valuestr = gst_value_serialize(&value))
{
unescape_value_string(valuestr);
desc.id = valuestr;
g_free(valuestr);
has_id = true;
}
g_value_unset(&value);
gst_object_unref(element);
}
if (has_id) {}
else if (auto props = gst_device_get_properties(device))
{
for (auto v : {"device.path", "device.id", "device.guid", "device.strid"})
if (const char * path = gst_structure_get_string(props, v))
{
desc.id = path;
has_id = true;
break;
}
gst_structure_free(props);
}
auto name = gst_device_get_display_name(device);
desc.description = name;
if (!has_id)
{
desc.id = name;
}
g_free(name);
}
void print_device(GstDevice * device, std::vector<CameraDesc>& result)
{
CameraDesc desc;
get_device_header(device, desc);
if (auto caps = gst_device_get_caps(device))
{
const auto size = gst_caps_get_size(caps);
for (int idx = 0; idx < size; ++idx) {
GstStructure *s = gst_caps_get_structure(caps, idx);
CameraMode mode{};
gst_structure_get_int(s, "width", &mode.w);
gst_structure_get_int(s, "height", &mode.h);
if (const char* format = gst_structure_get_string(s, "format"))
{
mode.format = format;
}
else
{
continue;
}
if (auto framerates = gst_structure_get_value(s, "framerate")) {
if (GST_VALUE_HOLDS_FRACTION(framerates))
{
mode.den = gst_value_get_fraction_numerator(framerates);
mode.num = gst_value_get_fraction_denominator(framerates);
desc.modes.push_back(mode);
}
else if (GST_VALUE_HOLDS_LIST(framerates))
{
const auto num_framerates = gst_value_list_get_size(framerates);
for (int i = 0; i < num_framerates; ++i)
{
const GValue *value;
value = gst_value_list_get_value(framerates, i);
mode.den = gst_value_get_fraction_numerator(value);
mode.num = gst_value_get_fraction_denominator(value);
desc.modes.push_back(mode);
}
}
else if (GST_VALUE_HOLDS_FRACTION_RANGE(framerates))
{
const auto fraction_range_min = gst_value_get_fraction_range_min(framerates);
const auto numerator_min = gst_value_get_fraction_numerator(fraction_range_min);
const auto denominator_min = gst_value_get_fraction_denominator(fraction_range_min);
const auto fraction_range_max = gst_value_get_fraction_range_max(framerates);
const auto numerator_max = gst_value_get_fraction_numerator(fraction_range_max);
const auto denominator_max = gst_value_get_fraction_denominator(fraction_range_max);
for (int i = numerator_max; i >= numerator_min; --i)
{
for (int j = denominator_min; j <= denominator_max; ++j)
{
mode.den = i;
mode.num = j;
desc.modes.push_back(mode);
}
}
}
}
}
gst_caps_unref(caps);
}
if (!desc.modes.empty())
{
result.push_back(std::move(desc));
}
}
void print_device(GstDevice * device, std::vector<AudioDesc>& result)
{
AudioDesc desc;
get_device_header(device, desc);
result.push_back(std::move(desc));
}
// https://github.com/huskyroboticsteam/Orpheus/blob/084ccacf19f520836f15db4abb37f678d3f20993/Rover/GStreamer/device-scanner.cpp
GstDeviceMonitor *device_monitor(const char *media_type, const gchar* classes)
{
// starts the monitor for the devices
auto monitor = gst_device_monitor_new();
// adds a filter to scan for only video devices
auto caps = gst_caps_new_empty_simple(media_type);
gst_device_monitor_add_filter(monitor, classes, caps);
gst_caps_unref(caps);
return monitor;
}
} // namespace
std::vector<CameraDesc> getCameraDescriptions()
{
std::vector<CameraDesc> result;
// create the monitor
auto monitor = device_monitor("video/x-raw", "Video/Source");
auto dev = gst_device_monitor_get_devices(monitor);
// loop for the lists
for (GList *cur = g_list_first(dev); cur != nullptr; cur = g_list_next(cur))
{
auto * device = static_cast<GstDevice *>(cur->data);
print_device(device, result);
}
gst_object_unref(monitor);
return result;
}
std::vector<AudioDesc> getAudioDescriptions()
{
std::vector<AudioDesc> result;
// create the monitor
auto monitor = device_monitor("audio/x-raw", "Audio/Source");
auto dev = gst_device_monitor_get_devices(monitor);
// loop for the lists
for (GList *cur = g_list_first(dev); cur != nullptr; cur = g_list_next(cur))
{
auto * device = static_cast<GstDevice *>(cur->data);
print_device(device, result);
}
gst_object_unref(monitor);
return result;
}
double CameraMode::fps() const
{
return static_cast<double>(den) / num;
}
QString CameraMode::getDescr() const
{
return QObject::tr("%1 %2 x %3 @ %4 FPS [ %5 / %6 sec ]").arg(format).arg(w).arg(h).arg(fps()).arg(num).arg(den);
}