Skip to content

Commit b123b54

Browse files
committed
Test function works, does all code need to be in worker?
1 parent 74535a2 commit b123b54

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

v4l2capture.cpp

+100-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ class Device_manager_Worker_thread_args
909909
usleep(1000);
910910
try
911911
{
912-
this->ReadFrame();
912+
//this->ReadFrame();
913913
}
914914
catch(std::exception)
915915
{
@@ -1256,6 +1256,102 @@ static PyObject *Device_manager_close(Device_manager *self, PyObject *args)
12561256
Py_RETURN_NONE;
12571257
}
12581258

1259+
static PyObject *Test(Device_manager *self, PyObject *args)
1260+
{
1261+
//Process arguments
1262+
const char *devarg = NULL;
1263+
if(PyTuple_Size(args) >= 1)
1264+
{
1265+
PyObject *pydevarg = PyTuple_GetItem(args, 0);
1266+
devarg = PyString_AsString(pydevarg);
1267+
}
1268+
else
1269+
{
1270+
devarg = "/dev/video0";
1271+
}
1272+
1273+
std::map<std::string, struct buffer *>::iterator it = self->buffers->find(devarg);
1274+
if(it == self->buffers->end())
1275+
{
1276+
throw std::runtime_error("Buffers have not been created");
1277+
Py_RETURN_NONE;
1278+
}
1279+
1280+
struct v4l2_buffer buffer;
1281+
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1282+
buffer.memory = V4L2_MEMORY_MMAP;
1283+
int fd = (*self->fd)[devarg];
1284+
printf("a %d\n", fd);
1285+
if(my_ioctl(fd, VIDIOC_DQBUF, &buffer))
1286+
{
1287+
throw std::runtime_error("VIDIOC_DQBUF failed");
1288+
Py_RETURN_NONE;
1289+
}
1290+
printf("b\n");
1291+
#ifdef USE_LIBV4L
1292+
printf("Rx %d\n", buffer.bytesused); //self->buffers[buffer.index].start, buffer.bytesused
1293+
1294+
#else
1295+
// Convert buffer from YUYV to RGB.
1296+
// For the byte order, see: http://v4l2spec.bytesex.org/spec/r4339.htm
1297+
// For the color conversion, see: http://v4l2spec.bytesex.org/spec/x2123.htm
1298+
int length = buffer.bytesused * 6 / 4;
1299+
PyObject *result = PyString_FromStringAndSize(NULL, length);
1300+
1301+
if(!result)
1302+
{
1303+
throw std::runtime_error("String convert failed");
1304+
Py_RETURN_NONE;
1305+
}
1306+
1307+
char *rgb = PyString_AS_STRING(result);
1308+
char *rgb_max = rgb + length;
1309+
unsigned char *yuyv = self->buffers[buffer.index].start;
1310+
1311+
#define CLAMP(c) ((c) <= 0 ? 0 : (c) >= 65025 ? 255 : (c) >> 8)
1312+
while(rgb < rgb_max)
1313+
{
1314+
int u = yuyv[1] - 128;
1315+
int v = yuyv[3] - 128;
1316+
int uv = 100 * u + 208 * v;
1317+
u *= 516;
1318+
v *= 409;
1319+
1320+
int y = 298 * (yuyv[0] - 16);
1321+
rgb[0] = CLAMP(y + v);
1322+
rgb[1] = CLAMP(y - uv);
1323+
rgb[2] = CLAMP(y + u);
1324+
1325+
y = 298 * (yuyv[2] - 16);
1326+
rgb[3] = CLAMP(y + v);
1327+
rgb[4] = CLAMP(y - uv);
1328+
rgb[5] = CLAMP(y + u);
1329+
1330+
rgb += 6;
1331+
yuyv += 4;
1332+
}
1333+
#undef CLAMP
1334+
#endif
1335+
1336+
/*if(1)
1337+
{
1338+
out = PyTuple_New(4);
1339+
PyTuple_SetItem(out, 0, result);
1340+
PyTuple_SetItem(out, 1, PyInt_FromLong(buffer.timestamp.tv_sec));
1341+
PyTuple_SetItem(out, 2, PyInt_FromLong(buffer.timestamp.tv_usec));
1342+
PyTuple_SetItem(out, 3, PyInt_FromLong(buffer.sequence));
1343+
}*/
1344+
1345+
//Queue next frame read
1346+
if(my_ioctl(fd, VIDIOC_QBUF, &buffer))
1347+
{
1348+
throw std::runtime_error("VIDIOC_QBUF failed");
1349+
Py_RETURN_NONE;
1350+
}
1351+
1352+
Py_RETURN_NONE;
1353+
}
1354+
12591355
// *********************************************************************
12601356

12611357
static PyMethodDef Video_device_methods[] = {
@@ -1337,6 +1433,9 @@ static PyMethodDef Device_manager_methods[] = {
13371433
{"close", (PyCFunction)Device_manager_close, METH_VARARGS,
13381434
"close(dev = '\\dev\\video0')\n\n"
13391435
"Close video device. Subsequent calls to other methods will fail."},
1436+
{"test", (PyCFunction)Test, METH_VARARGS,
1437+
"test(dev = '\\dev\\video0')\n\n"
1438+
"testfunc."},
13401439
{NULL}
13411440
};
13421441

0 commit comments

Comments
 (0)