Skip to content

Commit 66faa42

Browse files
committed
Start and stop function works
1 parent d0d944b commit 66faa42

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

v4l2capture.cpp

+27-18
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ typedef struct {
5050
int buffer_count;
5151
} Video_device;
5252

53-
typedef struct {
53+
class Device_manager_cl{
54+
public:
5455
PyObject_HEAD
55-
std::map<std::string, int> fd;
56-
std::map<std::string, struct buffer *> buffers;
57-
std::map<std::string, int> buffer_count;
58-
} Device_manager;
56+
std::map<std::string, int> *fd;
57+
std::map<std::string, struct buffer *> *buffers;
58+
std::map<std::string, int> *buffer_counts;
59+
};
60+
typedef Device_manager_cl Device_manager;
5961

6062
struct capability {
6163
int id;
@@ -767,21 +769,28 @@ static PyObject *InsertHuffmanTable(PyObject *self, PyObject *args)
767769

768770
static void Device_manager_dealloc(Device_manager *self)
769771
{
772+
delete self->fd;
773+
delete self->buffers;
774+
delete self->buffer_counts;
770775
self->ob_type->tp_free((PyObject *)self);
771776
}
772777

773778
static int Device_manager_init(Device_manager *self, PyObject *args,
774779
PyObject *kwargs)
775780
{
781+
self->fd = new std::map<std::string, int>;
782+
self->buffers = new std::map<std::string, struct buffer *>;
783+
self->buffer_counts = new std::map<std::string, int>;
776784
return 0;
777785
}
778786

779787
static PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
780788
// self, dev = None, reqSize=(640, 480), reqFps = 30, fmt = "MJPEG", buffer_count = 10):
781789
{
790+
782791
//Process arguments
783792
const char *devarg = NULL;
784-
if(PyTuple_Size(args) < 1)
793+
if(PyTuple_Size(args) >= 1)
785794
{
786795
PyObject *pydevarg = PyTuple_GetItem(args, 0);
787796
devarg = PyString_AsString(pydevarg);
@@ -799,8 +808,8 @@ static PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
799808
}
800809

801810
//Check this device has not already been start
802-
std::map<std::string, int>::iterator it = self->fd.find(devarg);
803-
if(it!=self->fd.end())
811+
std::map<std::string, int>::iterator it = self->fd->find(devarg);
812+
if(it!=self->fd->end())
804813
{
805814
PyErr_Format(PyExc_RuntimeError, "Device already started.");
806815
Py_RETURN_NONE;
@@ -815,8 +824,8 @@ static PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
815824
Py_RETURN_NONE;
816825
}
817826

818-
self->fd[devarg] = fd;
819-
self->buffers[devarg] = NULL;
827+
(*self->fd)[devarg] = fd;
828+
(*self->buffers)[devarg] = NULL;
820829

821830
//Set other parameters for capture
822831
//TODO
@@ -854,9 +863,9 @@ static PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
854863
}
855864

856865
struct buffer *buffs = (struct buffer *)malloc(reqbuf.count * sizeof(struct buffer));
857-
self->buffers[devarg] = buffs;
866+
(*self->buffers)[devarg] = buffs;
858867

859-
if(!self->buffers[devarg])
868+
if(!buffs)
860869
{
861870
PyErr_NoMemory();
862871
Py_RETURN_NONE;
@@ -885,8 +894,8 @@ static PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
885894
}
886895
}
887896

888-
self->buffer_count[devarg] = reqbuf.count;
889-
buffer_count = self->buffer_count[devarg];
897+
(*self->buffer_counts)[devarg] = reqbuf.count;
898+
buffer_count = reqbuf.count;
890899

891900
// Send the buffer to the device. Some devices require this to be done
892901
// before calling 'start'.
@@ -916,7 +925,7 @@ static PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
916925
Py_RETURN_NONE;
917926
}
918927

919-
static PyObject *Device_manager_stop(Video_device *self, PyObject *args)
928+
static PyObject *Device_manager_stop(Device_manager *self, PyObject *args)
920929
{
921930
//Process arguments
922931
const char *devarg = NULL;
@@ -930,7 +939,7 @@ static PyObject *Device_manager_stop(Video_device *self, PyObject *args)
930939
devarg = "/dev/video0";
931940
}
932941

933-
if(self->fd[devarg] < 0)
942+
if((*self->fd)[devarg] < 0)
934943
{
935944
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
936945
Py_RETURN_NONE;
@@ -939,7 +948,7 @@ static PyObject *Device_manager_stop(Video_device *self, PyObject *args)
939948
enum v4l2_buf_type type;
940949
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
941950

942-
if(my_ioctl(self->fd[devarg], VIDIOC_STREAMOFF, &type))
951+
if(my_ioctl((*self->fd)[devarg], VIDIOC_STREAMOFF, &type))
943952
{
944953
Py_RETURN_NONE;
945954
}
@@ -1030,7 +1039,7 @@ static PyTypeObject Device_manager_type = {
10301039
PyObject_HEAD_INIT(NULL)
10311040
0, "v4l2capture.Device_manager", sizeof(Device_manager), 0,
10321041
(destructor)Device_manager_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1033-
0, Py_TPFLAGS_DEFAULT, "Video_device(path)\n\nOpens the video device at "
1042+
0, Py_TPFLAGS_DEFAULT, "Device_manager(path)\n\nOpens the video device at "
10341043
"the given path and returns an object that can capture images. The "
10351044
"constructor and all methods except close may raise IOError.", 0, 0, 0,
10361045
0, 0, 0, Device_manager_methods, 0, 0, 0, 0, 0, 0, 0,

0 commit comments

Comments
 (0)