-
Notifications
You must be signed in to change notification settings - Fork 79
/
v4l2camera.cc
445 lines (399 loc) · 14.7 KB
/
v4l2camera.cc
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "capture.h"
#include <nan.h>
#include <errno.h>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
namespace {
struct CallbackData {
Nan::Persistent<v8::Object> thisObj;
std::unique_ptr<Nan::Callback> callback;
};
class Camera : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
private:
static NAN_METHOD(New);
static NAN_METHOD(Start);
static NAN_METHOD(Stop);
static NAN_METHOD(Capture);
static NAN_METHOD(FrameRaw);
static NAN_METHOD(FrameYUYVToRGB);
static NAN_METHOD(ConfigGet);
static NAN_METHOD(ConfigSet);
static NAN_METHOD(ControlGet);
static NAN_METHOD(ControlSet);
static void StopCB(uv_poll_t* handle, int status, int events);
static void CaptureCB(uv_poll_t* handle, int status, int events);
static void
WatchCB(uv_poll_t* handle, void (*callbackCall)(CallbackData* data));
static void
Watch(const Nan::FunctionCallbackInfo<v8::Value>& info, uv_poll_cb cb);
Camera();
~Camera();
camera_t* camera;
};
//[error message handling]
struct LogContext {
std::string msg;
};
static void logRecord(camera_log_t type, const char* msg, void* pointer) {
std::stringstream ss;
switch (type) {
case CAMERA_ERROR:
ss << "CAMERA ERROR [" << msg << "] " << errno << " " << strerror(errno);
break;
case CAMERA_FAIL:
ss << "CAMERA FAIL [" << msg << "]";
break;
case CAMERA_INFO:
ss << "CAMERA INFO [" << msg << "]";
break;
}
static_cast<LogContext*>(pointer)->msg = ss.str();
}
static inline v8::Local<v8::Value> cameraError(const camera_t* camera) {
const auto ctx = static_cast<LogContext*>(camera->context.pointer);
return Nan::Error(ctx->msg.c_str());
}
//[helpers]
static inline v8::Local<v8::Value>
getValue(const v8::Local<v8::Object>& self, const char* name) {
return Nan::Get(self, Nan::New(name).ToLocalChecked()).ToLocalChecked();
}
static inline std::int32_t
getInt(const v8::Local<v8::Object>& self, const char* name) {
return Nan::To<std::int32_t>(getValue(self, name)).FromJust();
}
static inline std::uint32_t
getUint(const v8::Local<v8::Object>& self, const char* name) {
return Nan::To<std::uint32_t>(getValue(self, name)).FromJust();
}
static inline void
setValue(const v8::Local<v8::Object>& self, const char* name,
const v8::Local<v8::Value>& value) {
Nan::Set(self, Nan::New(name).ToLocalChecked(), value);
}
static inline void
setInt(const v8::Local<v8::Object>& self, const char* name,
std::int32_t value) {
setValue(self, name, Nan::New(value));
}
static inline void
setUint(const v8::Local<v8::Object>& self, const char* name,
std::uint32_t value) {
setValue(self, name, Nan::New(value));
}
static inline void
setString(const v8::Local<v8::Object>& self, const char* name,
const char* value) {
setValue(self, name, Nan::New(value).ToLocalChecked());
}
static inline void
setBool(const v8::Local<v8::Object>& self, const char* name, bool value) {
setValue(self, name, Nan::New<v8::Boolean>(value));
}
//[callback helpers]
void Camera::WatchCB(uv_poll_t* handle,
void (*callbackCall)(CallbackData* data)) {
Nan::HandleScope scope;
auto data = static_cast<CallbackData*>(handle->data);
uv_poll_stop(handle);
uv_close(reinterpret_cast<uv_handle_t*>(handle),
[](uv_handle_t* handle) -> void {delete handle;});
callbackCall(data);
data->thisObj.Reset();
delete data;
}
void Camera::Watch(const Nan::FunctionCallbackInfo<v8::Value>& info,
uv_poll_cb cb) {
auto data = new CallbackData;
data->thisObj.Reset(info.Holder());
data->callback.reset(new Nan::Callback(info[0].As<v8::Function>()));
auto camera = Nan::ObjectWrap::Unwrap<Camera>(info.Holder())->camera;
auto handle = new uv_poll_t;
handle->data = data;
uv_poll_init(uv_default_loop(), handle, camera->fd);
uv_poll_start(handle, UV_READABLE, cb);
}
//[methods]
static const char* control_type_names[] = {
"invalid",
"int",
"bool",
"menu",
"int64",
"class"
"string",
"bitmask",
"int_menu",
};
static v8::Local<v8::Object> cameraControls(const camera_t* camera) {
auto ccontrols = camera_controls_new(camera);
auto controls = Nan::New<v8::Array>(ccontrols->length);
for (auto i = std::size_t{0}; i < ccontrols->length; ++i) {
const auto ccontrol = &ccontrols->head[i];
auto control = Nan::New<v8::Object>();
const auto name =
Nan::New(reinterpret_cast<char*>(ccontrol->name)).ToLocalChecked();
Nan::Set(controls, i, control);
Nan::Set(controls, name, control);
setUint(control, "id", ccontrol->id);
setValue(control, "name", name);
setString(control, "type", control_type_names[ccontrol->type]);
setInt(control, "min", ccontrol->min);
setInt(control, "max", ccontrol->max);
setInt(control, "step", ccontrol->step);
setInt(control, "default", ccontrol->default_value);
auto flags = Nan::New<v8::Object>();
setValue(control, "flags", flags);
setBool(flags, "disabled", ccontrol->flags.disabled);
setBool(flags, "grabbed", ccontrol->flags.grabbed);
setBool(flags, "readOnly", ccontrol->flags.read_only);
setBool(flags, "update", ccontrol->flags.update);
setBool(flags, "inactive", ccontrol->flags.inactive);
setBool(flags, "slider", ccontrol->flags.slider);
setBool(flags, "writeOnly", ccontrol->flags.write_only);
setBool(flags, "volatile", ccontrol->flags.volatile_value);
auto menu = Nan::New<v8::Array>(ccontrol->menus.length);
setValue(control, "menu", menu);
switch (ccontrol->type) {
case CAMERA_CTRL_MENU:
for (auto j = std::size_t{0}; j < ccontrol->menus.length; ++j) {
const auto value =
reinterpret_cast<char*>(ccontrol->menus.head[j].name);
Nan::Set(menu, j, Nan::New(value).ToLocalChecked());
}
break;
#ifndef CAMERA_OLD_VIDEODEV2_H
case CAMERA_CTRL_INTEGER_MENU:
for (auto j = std::size_t{0}; j < ccontrol->menus.length; ++j) {
const auto value =
static_cast<std::int32_t>(ccontrol->menus.head[j].value);
Nan::Set(menu, j, Nan::New(value));
}
break;
#endif
default: break;
}
}
camera_controls_delete(ccontrols);
return controls;
}
static camera_format_t convertCFormat(const v8::Local<v8::Object> format) {
const auto pixformat = getUint(format, "format");
const auto width = getUint(format, "width");
const auto height = getUint(format, "height");
auto numerator = std::uint32_t{0};
auto denominator = std::uint32_t{0};
const auto finterval = getValue(format, "interval");
if (finterval->IsObject()) {
const auto interval = finterval->ToObject();
numerator = getUint(interval, "numerator");
denominator = getUint(interval, "denominator");
}
return {
pixformat, width, height, {numerator, denominator}
};
}
static v8::Local<v8::Object> convertFormat(const camera_format_t* cformat) {
char name[5];
camera_format_name(cformat->format, name);
auto format = Nan::New<v8::Object>();
setString(format, "formatName", name);
setUint(format, "format", cformat->format);
setUint(format, "width", cformat->width);
setUint(format, "height", cformat->height);
auto interval = Nan::New<v8::Object>();
setValue(format, "interval", interval);
setUint(interval, "numerator", cformat->interval.numerator);
setUint(interval, "denominator", cformat->interval.denominator);
return format;
}
static v8::Local<v8::Object> cameraFormats(const camera_t* camera) {
auto cformats = camera_formats_new(camera);
auto formats = Nan::New<v8::Array>(cformats->length);
for (auto i = std::size_t{0}; i < cformats->length; ++i) {
auto cformat = &cformats->head[i];
auto format = convertFormat(cformat);
Nan::Set(formats, i, format);
}
return formats;
}
NAN_METHOD(Camera::New) {
if (!info.IsConstructCall()) {
// [NOTE] generic recursive call with `new`
std::vector<v8::Local<v8::Value>> args(info.Length());
for (auto i = std::size_t{0}; i < args.size(); ++i) args[i] = info[i];
auto inst = Nan::NewInstance(info.Callee(), args.size(), args.data());
if (!inst.IsEmpty()) info.GetReturnValue().Set(inst.ToLocalChecked());
return;
}
if (info.Length() < 1) {
Nan::ThrowTypeError("argument required: device");
return;
}
const auto device = Nan::To<v8::String>(info[0]).ToLocalChecked();
auto camera = camera_open(*Nan::Utf8String(device));
if (!camera) {
Nan::ThrowError(strerror(errno));
return;
}
camera->context.pointer = new LogContext;
camera->context.log = &logRecord;
auto thisObj = info.This();
auto self = new Camera;
self->camera = camera;
self->Wrap(thisObj);
setValue(thisObj, "device", info[0]);
setValue(thisObj, "formats", cameraFormats(camera));
setValue(thisObj, "controls", cameraControls(camera));
}
NAN_METHOD(Camera::Start) {
auto thisObj = info.Holder();
auto camera = Nan::ObjectWrap::Unwrap<Camera>(thisObj)->camera;
if (!camera_start(camera)) {
Nan::ThrowError(cameraError(camera));
return;
}
setUint(thisObj, "width", camera->width);
setUint(thisObj, "height", camera->height);
info.GetReturnValue().Set(thisObj);
}
void Camera::StopCB(uv_poll_t* handle, int /*status*/, int /*events*/) {
auto callCallback = [](CallbackData* data) -> void {
Nan::HandleScope scope;
auto thisObj = Nan::New<v8::Object>(data->thisObj);
data->callback->Call(thisObj, 0, nullptr);
};
WatchCB(handle, callCallback);
}
NAN_METHOD(Camera::Stop) {
auto camera = Nan::ObjectWrap::Unwrap<Camera>(info.Holder())->camera;
if (!camera_stop(camera)) {
Nan::ThrowError(cameraError(camera));
return;
}
Watch(info, StopCB);
}
void Camera::CaptureCB(uv_poll_t* handle, int /*status*/, int /*events*/) {
auto callCallback = [](CallbackData* data) -> void {
Nan::HandleScope scope;
auto thisObj = Nan::New<v8::Object>(data->thisObj);
auto camera = Nan::ObjectWrap::Unwrap<Camera>(thisObj)->camera;
auto captured = bool{camera_capture(camera)};
std::vector<v8::Local<v8::Value>> args{{Nan::New(captured)}};
data->callback->Call(thisObj, args.size(), args.data());
};
WatchCB(handle, callCallback);
}
NAN_METHOD(Camera::Capture) {
Watch(info, CaptureCB);
}
NAN_METHOD(Camera::FrameRaw) {
const auto camera = Nan::ObjectWrap::Unwrap<Camera>(info.Holder())->camera;
const auto size = camera->head.length;
auto data = new uint8_t[size];
std::copy(camera->head.start, camera->head.start + size, data);
const auto flag = v8::ArrayBufferCreationMode::kInternalized;
auto buf = v8::ArrayBuffer::New(info.GetIsolate(), data, size, flag);
auto array = v8::Uint8Array::New(buf, 0, size);
info.GetReturnValue().Set(array);
}
NAN_METHOD(Camera::FrameYUYVToRGB) {
// TBD: check the current format as YUYV
const auto camera = Nan::ObjectWrap::Unwrap<Camera>(info.Holder())->camera;
auto rgb = yuyv2rgb(camera->head.start, camera->width, camera->height);
const auto size = camera->width * camera->height * 3;
const auto flag = v8::ArrayBufferCreationMode::kInternalized;
auto buf = v8::ArrayBuffer::New(info.GetIsolate(), rgb, size, flag);
auto array = v8::Uint8Array::New(buf, 0, size);
info.GetReturnValue().Set(array);
}
NAN_METHOD(Camera::ConfigGet) {
const auto camera = Nan::ObjectWrap::Unwrap<Camera>(info.Holder())->camera;
camera_format_t cformat;
if (!camera_config_get(camera, &cformat)) {
Nan::ThrowError(cameraError(camera));
return;
}
auto format = convertFormat(&cformat);
info.GetReturnValue().Set(format);
}
NAN_METHOD(Camera::ConfigSet) {
if (info.Length() < 1) {
Nan::ThrowTypeError("argument required: config");
return;
}
const auto cformat = convertCFormat(info[0]->ToObject());
auto thisObj = info.Holder();
auto camera = Nan::ObjectWrap::Unwrap<Camera>(thisObj)->camera;
if (!camera_config_set(camera, &cformat)) {
Nan::ThrowError(cameraError(camera));
return;
}
setUint(thisObj, "width", camera->width);
setUint(thisObj, "height", camera->height);
info.GetReturnValue().Set(thisObj);
}
NAN_METHOD(Camera::ControlGet) {
if (info.Length() < 1) {
Nan::ThrowTypeError("an argument required: id");
return;
}
const auto id = info[0]->Uint32Value();
auto camera = Nan::ObjectWrap::Unwrap<Camera>(info.Holder())->camera;
auto value = std::int32_t{0};
auto success = bool{camera_control_get(camera, id, &value)};
if (!success) {
Nan::ThrowError(cameraError(camera));
return;
}
info.GetReturnValue().Set(Nan::New(value));
}
NAN_METHOD(Camera::ControlSet) {
if (info.Length() < 2) {
Nan::ThrowTypeError("arguments required: id, value");
return;
}
const auto id = info[0]->Uint32Value();
const auto value = info[1]->Int32Value();
auto thisObj = info.Holder();
auto camera = Nan::ObjectWrap::Unwrap<Camera>(thisObj)->camera;
auto success = bool{camera_control_set(camera, id, value)};
if (!success) {
Nan::ThrowError(cameraError(camera));
return;
}
info.GetReturnValue().Set(thisObj);
}
Camera::Camera() : camera(nullptr) {}
Camera::~Camera() {
if (camera) {
auto ctx = static_cast<LogContext*>(camera->context.pointer);
camera_close(camera);
delete ctx;
}
}
//[module init]
NAN_MODULE_INIT(Camera::Init) {
const auto name = Nan::New("Camera").ToLocalChecked();
auto ctor = Nan::New<v8::FunctionTemplate>(New);
auto ctorInst = ctor->InstanceTemplate();
ctor->SetClassName(name);
ctorInst->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(ctor, "start", Start);
Nan::SetPrototypeMethod(ctor, "stop", Stop);
Nan::SetPrototypeMethod(ctor, "capture", Capture);
Nan::SetPrototypeMethod(ctor, "frameRaw", FrameRaw);
Nan::SetPrototypeMethod(ctor, "toYUYV", FrameRaw);
Nan::SetPrototypeMethod(ctor, "toRGB", FrameYUYVToRGB);
Nan::SetPrototypeMethod(ctor, "configGet", ConfigGet);
Nan::SetPrototypeMethod(ctor, "configSet", ConfigSet);
Nan::SetPrototypeMethod(ctor, "controlGet", ControlGet);
Nan::SetPrototypeMethod(ctor, "controlSet", ControlSet);
Nan::Set(target, name, Nan::GetFunction(ctor).ToLocalChecked());
}
}
NODE_MODULE(v4l2camera, Camera::Init)