forked from jnohlgard/python-v4l2capture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideoout.cpp
167 lines (139 loc) · 3.89 KB
/
videoout.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
#include <vector>
#include <pthread.h>
#include <iostream>
#include "videoout.h"
#ifdef _NT
#include "namedpipeout.h"
#endif
#if _POSIX
#include "v4l2out.h"
#endif
int Video_out_manager_init(Video_out_manager *self, PyObject *args,
PyObject *kwargs)
{
self->threads = new std::map<std::string, class Base_Video_Out *>;
return 0;
}
void Video_out_manager_dealloc(Video_out_manager *self)
{
//Stop high level threads
for(std::map<std::string, class Base_Video_Out *>::iterator it = self->threads->begin();
it != self->threads->end(); it++)
{
it->second->Stop();
it->second->WaitForStop();
}
delete self->threads;
self->threads = NULL;
self->ob_type->tp_free((PyObject *)self);
}
PyObject *Video_out_manager_open(Video_out_manager *self, PyObject *args)
{
std::cout << "Video_out_manager_open" << std::endl;
//Process arguments
const char *devarg = NULL;
const char *pxFmtIn = NULL;
int widthIn = 0;
int heightIn = 0;
if(!PyArg_ParseTuple(args, "ssii", &devarg, &pxFmtIn, &widthIn, &heightIn))
{
PyErr_SetString(PyExc_RuntimeError, "Incorrect arguments to function.");
return NULL;
}
//Create worker thread
pthread_t thread;
#ifdef _POSIX
Video_out *threadArgs = new Video_out(devarg);
#endif
#ifdef _NT
NamedPipeOut *threadArgs = new NamedPipeOut(devarg);
#endif
(*self->threads)[devarg] = threadArgs;
threadArgs->SetOutputSize(widthIn, heightIn);
threadArgs->SetOutputPxFmt(pxFmtIn);
#ifdef _POSIX
pthread_create(&thread, NULL, Video_out_manager_Worker_thread, threadArgs);
#endif
#ifdef _NT
pthread_create(&thread, NULL, NamedPipeOut_Worker_thread, threadArgs);
#endif
Py_RETURN_NONE;
}
PyObject *Video_out_manager_Send_frame(Video_out_manager *self, PyObject *args)
{
//printf("Video_out_manager_Send_frame\n");
//dev = '\\dev\\video0', img, pixel_format, width, height
//Process arguments
const char *devarg = NULL;
const char *imgIn = NULL;
const char *pxFmtIn = NULL;
int widthIn = 0;
int heightIn = 0;
if(PyObject_Length(args) < 5)
{
PyErr_SetString(PyExc_RuntimeError, "Too few arguments.");
return NULL;
}
PyObject *pydev = PyTuple_GetItem(args, 0);
devarg = PyString_AsString(pydev);
PyObject *pyimg = PyTuple_GetItem(args, 1);
imgIn = NULL;
if(imgIn==NULL && PyString_Check(pyimg)) imgIn = PyString_AsString(pyimg);
if(imgIn==NULL && PyByteArray_Check(pyimg)) imgIn = PyByteArray_AsString(pyimg);
Py_ssize_t imgLen = PyObject_Length(pyimg);
if(imgIn == NULL)
{PyErr_SetString(PyExc_RuntimeError, "Argument 2 must be a string or bytearray."); return NULL;}
PyObject *pyPxFmt = PyTuple_GetItem(args, 2);
pxFmtIn = PyString_AsString(pyPxFmt);
PyObject *pyWidth = PyTuple_GetItem(args, 3);
widthIn = PyInt_AsLong(pyWidth);
PyObject *pyHeight = PyTuple_GetItem(args, 4);
heightIn = PyInt_AsLong(pyHeight);
std::map<std::string, class Base_Video_Out *>::iterator it = self->threads->find(devarg);
if(it != self->threads->end())
{
try
{
it->second->SendFrame(imgIn, imgLen, pxFmtIn, widthIn, heightIn);
}
catch(std::exception &err)
{
PyErr_SetString(PyExc_RuntimeError, err.what());
return NULL;
}
}
else
{
PyErr_SetString(PyExc_RuntimeError, "Device not found.");
return NULL;
}
Py_RETURN_NONE;
}
PyObject *Video_out_manager_close(Video_out_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);
}
//Stop worker thread
std::map<std::string, class Base_Video_Out *>::iterator it = self->threads->find(devarg);
if(it != self->threads->end())
{
it->second->Stop();
}
Py_RETURN_NONE;
}
PyObject *Video_out_manager_list_devices(Video_out_manager *self)
{
PyObject *out = PyList_New(0);
std::vector<std::string> devLi = List_out_devices();
for(unsigned i=0; i<devLi.size(); i++)
{
PyList_Append(out, PyString_FromString(devLi[i].c_str()));
}
PyList_Sort(out);
return out;
}