forked from jnohlgard/python-v4l2capture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
videoin.cpp
279 lines (234 loc) · 7.13 KB
/
videoin.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
#include <iostream>
#include "videoin.h"
#include <pthread.h>
#ifdef _NT
#include "mfvideoin.h"
#endif
#ifdef _POSIX
#include "v4l2capture.h"
#endif
void Device_manager_dealloc(Device_manager *self)
{
//Stop high level threads
for(std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->begin();
it != self->threadArgStore->end(); it++)
{
PyObject *args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyString_FromString(it->first.c_str()));
Device_manager_stop(self, args);
Py_DECREF(args);
}
delete self->threadArgStore;
self->ob_type->tp_free((PyObject *)self);
}
int Device_manager_init(Device_manager *self, PyObject *args,
PyObject *kwargs)
{
self->threadArgStore = new std::map<std::string, class Base_Video_In*>;
return 0;
}
PyObject *Device_manager_open(Device_manager *self, PyObject *args)
{
//Process arguments
const char *devarg = "/dev/video0";
if(PyTuple_Size(args) >= 1)
{
PyObject *pydevarg = PyTuple_GetItem(args, 0);
devarg = PyString_AsString(pydevarg);
}
//Check this device has not already been opened
std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->find(devarg);
if(it!=self->threadArgStore->end())
{
PyErr_SetString(PyExc_RuntimeError, "Device already opened.");
return NULL;
}
pthread_t thread;
#ifdef _POSIX
Video_in_Manager *threadArgs = new Video_in_Manager(devarg);
#endif
#ifdef _NT
wchar_t *tmpDevName = new wchar_t[strlen(devarg)+1];
size_t returnValue;
mbstowcs_s(&returnValue, tmpDevName, strlen(devarg)+1, devarg, strlen(devarg)+1);
std::wstring tmpDevName2(tmpDevName);
delete [] tmpDevName;
MfVideoIn *threadArgs = new MfVideoIn(tmpDevName2.c_str());
#endif
(*self->threadArgStore)[devarg] = threadArgs;
#ifdef _POSIX
pthread_create(&thread, NULL, Video_in_Worker_thread, threadArgs);
#endif
#ifdef _NT
pthread_create(&thread, NULL, MfVideoIn_Worker_thread, threadArgs);
#endif
threadArgs->OpenDevice();
Py_RETURN_NONE;
}
PyObject *Device_manager_set_format(Device_manager *self, PyObject *args)
{
int size_x;
int size_y;
const char *fmt = NULL;
const char *devarg = NULL;
if(!PyArg_ParseTuple(args, "sii|s", &devarg, &size_x, &size_y, &fmt))
{
PyErr_BadArgument();
return NULL;
}
//Check this device is valid
std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->find(devarg);
if(it==self->threadArgStore->end())
{
PyErr_SetString(PyExc_RuntimeError, "Device already not ready.");
return NULL;
}
class Base_Video_In *threadArgs = (*self->threadArgStore)[devarg];
threadArgs->SetFormat(fmt, size_x, size_y);
Py_RETURN_NONE;
}
PyObject *Device_manager_Start(Device_manager *self, PyObject *args)
{
//Process arguments
const char *devarg = "/dev/video0";
if(PyTuple_Size(args) >= 1)
{
PyObject *pydevarg = PyTuple_GetItem(args, 0);
devarg = PyString_AsString(pydevarg);
}
long buffer_count = 10;
if(PyTuple_Size(args) >= 4)
{
PyObject *pybufferarg = PyTuple_GetItem(args, 4);
buffer_count = PyInt_AsLong(pybufferarg);
}
//Check this device is valid
std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->find(devarg);
if(it==self->threadArgStore->end())
{
PyErr_SetString(PyExc_RuntimeError, "Device already not ready.");
return NULL;
}
class Base_Video_In *threadArgs = (*self->threadArgStore)[devarg];
threadArgs->StartDevice(buffer_count);
Py_RETURN_NONE;
}
void PyDict_SetItemString_Decref(PyObject *dic, const char *key, PyObject *val)
{
PyDict_SetItemString(dic, key, val);
Py_DECREF(val);
}
PyObject *Device_manager_Get_frame(Device_manager *self, PyObject *args)
{
//std::cout << "Device_manager_Get_frame" << std::endl;
//Process arguments
const char *devarg = "/dev/video0";
if(PyTuple_Size(args) >= 1)
{
PyObject *pydevarg = PyTuple_GetItem(args, 0);
devarg = PyString_AsString(pydevarg);
}
//Check this device is valid
std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->find(devarg);
if(it==self->threadArgStore->end())
{
PyErr_SetString(PyExc_RuntimeError, "Device already not ready.");
return NULL;
}
class Base_Video_In *threadArgs = (*self->threadArgStore)[devarg];
unsigned char *buffOut = NULL;
class FrameMetaData metaOut;
int ok = 0;
try
{
ok = threadArgs->GetFrame(&buffOut, &metaOut);
}
catch(std::exception &err)
{
PyErr_SetString(PyExc_RuntimeError, err.what());
return NULL;
}
if(ok && buffOut != NULL)
{
//Format output to python
PyObject *pymeta = PyDict_New();
PyDict_SetItemString_Decref(pymeta, "width", PyInt_FromLong(metaOut.width));
PyDict_SetItemString_Decref(pymeta, "height", PyInt_FromLong(metaOut.height));
PyDict_SetItemString_Decref(pymeta, "format", PyString_FromString(metaOut.fmt.c_str()));
PyDict_SetItemString_Decref(pymeta, "sequence", PyInt_FromLong(metaOut.sequence));
PyDict_SetItemString_Decref(pymeta, "tv_sec", PyInt_FromLong(metaOut.tv_sec));
PyDict_SetItemString_Decref(pymeta, "tv_usec", PyInt_FromLong(metaOut.tv_usec));
PyObject *out = PyTuple_New(2);
PyTuple_SetItem(out, 0, PyByteArray_FromStringAndSize((char *)buffOut, metaOut.buffLen));
PyTuple_SetItem(out, 1, pymeta);
delete [] buffOut;
return out;
}
if(!ok && buffOut!= NULL) //This generally should not happen
delete [] buffOut;
Py_RETURN_NONE;
}
PyObject *Device_manager_stop(Device_manager *self, PyObject *args)
{
//Process arguments
const char *devarg = "/dev/video0";
if(PyTuple_Size(args) >= 1)
{
PyObject *pydevarg = PyTuple_GetItem(args, 0);
devarg = PyString_AsString(pydevarg);
}
//Check this device is valid
std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->find(devarg);
if(it==self->threadArgStore->end())
{
PyErr_SetString(PyExc_RuntimeError, "Device already not ready.");
return NULL;
}
class Base_Video_In *threadArgs = (*self->threadArgStore)[devarg];
threadArgs->StopDevice();
Py_RETURN_NONE;
}
PyObject *Device_manager_close(Device_manager *self, PyObject *args)
{
//Process arguments
const char *devarg = "/dev/video0";
if(PyTuple_Size(args) >= 1)
{
PyObject *pydevarg = PyTuple_GetItem(args, 0);
devarg = PyString_AsString(pydevarg);
}
//Check this device is valid
std::map<std::string, class Base_Video_In *>::iterator it = self->threadArgStore->find(devarg);
if(it==self->threadArgStore->end())
{
PyErr_SetString(PyExc_RuntimeError, "Device already not ready.");
return NULL;
}
class Base_Video_In *threadArgs = (*self->threadArgStore)[devarg];
threadArgs->CloseDevice();
//Stop worker thread
threadArgs->Stop();
//Release memeory
threadArgs->WaitForStop();
delete threadArgs;
self->threadArgStore->erase(devarg);
Py_RETURN_NONE;
}
PyObject *Device_manager_list_devices(Device_manager *self)
{
PyObject *out = PyList_New(0);
std::vector<std::vector<std::wstring> > devLi = List_in_devices();
for(unsigned i=0; i<devLi.size(); i++)
{
PyObject *deviceTuple = PyTuple_New(devLi[i].size());
for(unsigned j=0; j<devLi[i].size(); j++)
{
const wchar_t *str = devLi[i][j].c_str();
PyTuple_SetItem(deviceTuple, j, PyUnicode_FromWideChar(str, wcslen(str)));
}
PyList_Append(out, deviceTuple);
Py_CLEAR(deviceTuple);
}
PyList_Sort(out);
return out;
}