-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArVideoExternalSource.h
More file actions
184 lines (165 loc) · 5.18 KB
/
ArVideoExternalSource.h
File metadata and controls
184 lines (165 loc) · 5.18 KB
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
#ifndef ARVIDEOEXTERNALSOURCE_H
#define ARVIDEOEXTERNALSOURCE_H
#include "Aria.h"
#include "ariaUtil.h"
#include "ArVideoInterface.h"
/** An easy way to supply image data to ArVideoServer from an external source
* such as image frames generated by or stored in your own code, or read
* asynchronously from some source. You can supply a pointer to read RGB image data from, or you can copy
* data of an image to this class, which will then be used if any network
* requests are received.
* Image data is array of RGB pixel structures with one unsigned 8-bit byte for
* each of R, G and B components of that pixel structure (so each pixel is 24
* bits). This corresponds to the CV_8UC3 format in OpenCV.
*
* For example, If you have data already in RGB unsigned char array,
* you can just set it:
* @code
// ... set up ARIA, ArNetworking and ArVideo ...
unsigned char image[width*height*3];
// ... initialize image data ...
ArVideoExternalSource videoSource("OpenCV Example");
ArVideoServer *videoServer = ArVideo::createVideoServer(&serverBase, &videoSource);
videoSource.open();
// You can copy updated data:
while(true)
{
// ... do some image processingand update the im object ...
videoSource.setDataCopy(image, width, height);
}
// Or you can just supply a pointer but be sure to lock it while modifying:
videoSource.setDataPtr(image, width, height);
while(true)
{
videoSource.lock();
// ... do some image processing and update the im object ...
videoSource.unlock();
videoSource.updated();
}
videoSource.close();
@endcode
* @todo XXX does ArVideoServer make a copy? If so does it do that during
* update()? If so we can avoid copying here, and lock during update().
*
*/
class ArVideoExternalSource : public virtual ArVideoInterface {
protected:
std::string myName;
bool myOpen;
bool myUpdated;
int myWidth;
int myHeight;
ArTime myUpdateTime;
bool myDataAllocated;
unsigned char *myData;
ArMutex myDataMutex;
ArCallbackList4<unsigned char* , int, int, ArTime> myVideoDataCallbacks;
public:
ArVideoExternalSource(const char *name) :
ArVideoInterface(),
myName(name), myOpen(false), myUpdated(false),
myWidth(0), myHeight(0),
myDataAllocated(false), myData(0),
myVideoDataCallbacks(name)
{
}
virtual ~ArVideoExternalSource() {
lock();
if(myDataAllocated)
delete[] myData;
unlock();
}
/// After setting the pointer, you must use lock() and unlock() while
/// accessing the data, and call updated() each time it is changed.
virtual bool setVideoDataPtr(unsigned char *ptr, int width, int height) {
if(myDataAllocated)
{
delete[] myData;
myDataAllocated = false;
}
myData = ptr;
myWidth = width;
myHeight = height;
return true;
}
void updated() {
myUpdateTime.setToNow();
myUpdated = true;
myVideoDataCallbacks.invoke(myData, myWidth, myHeight, myUpdateTime);
}
void lock() {
myDataMutex.lock();
}
void unlock() {
myDataMutex.unlock();
}
/// Copy data from @a data. You do not need to lock.
virtual bool updateVideoDataCopy(unsigned char *data, int width, int height) {
lock();
if(!myDataAllocated)
{
myData = new unsigned char[width * height * bytesPerPixel()];
myDataAllocated = true;
}
// TODO reallocate if needed
memcpy(myData, data, width*height*bytesPerPixel());
updated();
unlock();
return true;
}
virtual VideoFormat videoFormat() { return VIDEO_RGB24; }
virtual int bytesPerPixel() { return 3; }
virtual bool updateVideo() { return true; }
virtual bool updateVideoNow() { return myUpdated; } // todo could wait on a condition broadcast by updated
virtual void addDataCallback(ArVideoDataCallback *functor) {
myVideoDataCallbacks.addCallback(functor); // todo move to default implementation in ArVideoInterface?
}
virtual void remDataCallback(ArVideoDataCallback *functor) {
myVideoDataCallbacks.remCallback(functor); // todo move to default implementation in ArVideoInterface?
}
void open() {
myOpen = true;
}
void close() {
myOpen = false;
}
virtual bool isOpen() {
return myOpen;
}
virtual int getWidth() {
return myWidth;
}
virtual int getHeight() {
return myHeight;
}
virtual unsigned char *getData() {
return myData;
}
virtual const char *getConfigSectionName() {
return myName.c_str();
}
virtual const char *getVideoSize() {
return "default";
}
virtual int getCaptureTimeSubtrahendMsecs() {
// todo
return 0;
}
virtual bool getCaptureTime(ArTime *t) {
if(t) {
*t = myUpdateTime;
return true;
}
return false;
}
virtual std::list<std::string> getCameraParameterNames() {
return std::list<std::string>();
}
virtual unsigned int getCameraParameterValue(const std::string& param) {
return 0;
}
virtual ArPriority::Priority getMaxConfigParamPriority() {
return ArPriority::NORMAL;
}
};
#endif