-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCVideoServer.cpp
More file actions
293 lines (263 loc) · 8.58 KB
/
Copy pathPCVideoServer.cpp
File metadata and controls
293 lines (263 loc) · 8.58 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
/********************************************************************************
* Project : FIRST Motor Controller
* File Name : PCVideoServer.cpp
* Contributors : SVK, ELF
* Creation Date : November 30, 2008
* Revision History : Source code & revision history maintained at sourceforge.WPI.edu
* File Description : C++ Axis camera control for the FIRST Vision API
*/
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include <vxWorks.h>
#include "sockLib.h"
#include "inetLib.h"
#include "hostLib.h"
#include "AxisCamera.h"
#include "BaeUtilities.h"
#include "FrcError.h"
#include "chipobject/NiRioStatus.h"
#include "Task.h"
#include "Timer.h"
#include "Utility.h"
#include "PCVideoServer.h"
bool g_stopImageToPCServer=false;
/**
@brief Implements an object that automatically does a close on a
camera socket on destruction.
*/
class ScopedSocket {
public:
ScopedSocket(int camSock, const ErrorBase* errorBase) :
m_camSock(camSock)
{
if (errorBase->StatusIsFatal()) {
return;
}
if (m_camSock == ERROR) {
wpi_setError(*errorBase, ERR_CAMERA_SOCKET_CREATE_FAILED);
return;
}
}
~ScopedSocket() {
if (m_camSock != ERROR) {
close(m_camSock);
}
}
// Cast to int allows you to pass this to any function that
// takes the socket as an int.
operator int() const {
return m_camSock;
}
private:
int m_camSock;
};
//============================================================================
// PCVideoServer
//============================================================================
/**
@brief Constructor.
*/
PCVideoServer::PCVideoServer(void)
: m_task("PCVideo", (FUNCPTR)ImageToPCServer)
{
try {
// Start the image communication task.
StartPcTask();
if (StatusIsFatal()) {
throw GetError().GetCode();
}
}
catch(Error::Code error) {
wpi_setError(*this, error);
return;
}
}
/**
@brief Destructor.
Stop serving images and destroy this class.
*/
PCVideoServer::~PCVideoServer() {
//printf ("START PCVideoServer DESTRUCTOR **********************\n");
// Stop the images to PC server.
Stop();
//StopImageToPCServer();
// Clear the error so that you can use this object to make a connection to
// the VIDEO_TO_PC_PORT to stop the ImageToPCServer if it is waiting to
// accept connections from a PC.
ClearError();
// Open a socket.
ScopedSocket camSock(socket (AF_INET, SOCK_STREAM, 0), this);
// If successful
if (!StatusIsFatal()) {
// Create a connection to the localhost.
struct sockaddr_in selfAddr;
int sockAddrSize = sizeof(selfAddr);
bzero ((char *) &selfAddr, sockAddrSize);
selfAddr.sin_family = AF_INET;
selfAddr.sin_len = (u_char) sockAddrSize;
selfAddr.sin_port = htons (VIDEO_TO_PC_PORT);
if (( (int)(selfAddr.sin_addr.s_addr = inet_addr (const_cast<char*>("localhost")) ) != ERROR) ||
( (int)(selfAddr.sin_addr.s_addr = hostGetByName (const_cast<char*>("localhost")) ) != ERROR))
{
connect (camSock, (struct sockaddr *) &selfAddr, sockAddrSize);
}
}
}
/**
* Start the task that is responsible for sending images to the PC.
*/
int PCVideoServer::StartPcTask() {
if (StatusIsFatal()) {
return -1;
}
int id = 0;
g_stopImageToPCServer = false;
// check for prior copy of running task
// TODO: Report error. You are in a bad state.
//char taskName[1024];
//sprintf(taskName, "%s%X", taskName, reinterpret_cast<int>(this));
int oldId = taskNameToId(m_task.GetName());
if(oldId != ERROR) {
taskDelete(oldId);
}
// spawn video server task
// this is done to ensure that the task is spawned with the
// floating point context save parameter.
bool started = m_task.Start();
id = m_task.GetID();
if (!started) {
wpi_setError(*this, ERR_CAMERA_TASK_SPAWN_FAILED);
return id;
}
taskDelay(1);
return id;
}
/**
@brief Start sending images to the PC.
*/
void PCVideoServer::Start() {
StartPcTask();
}
/**
@brief Stop sending images to the PC.
*/
void PCVideoServer::Stop() {
g_stopImageToPCServer = true;
}
//============================================================================
// C Functions
//============================================================================
/**
@brief Stop sending images to the PC.
*/
void StopImageToPCServer() {
g_stopImageToPCServer = true;
}
/**
@brief Check is server is shutting down.
@return bool true if time to stop
*/
bool ShouldStopImageToPCServer() {
// Return the current state of g_stopImageToPCServer
return g_stopImageToPCServer;
}
/**
@brief Initialize the socket and serve images to the PC.
*/
int ImageToPCServer() {
/* Setup to PC sockets */
struct sockaddr_in serverAddr;
int sockAddrSize = sizeof(serverAddr);
int pcSock = ERROR;
bzero ((char *) &serverAddr, sockAddrSize);
serverAddr.sin_len = (u_char) sockAddrSize;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons (VIDEO_TO_PC_PORT);
serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);
int success;
while (true) {
double lastImageTimestamp = 0;
taskSafe();
// Create the socket.
if ((pcSock = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) {
perror ("socket");
continue;
}
// Set the TCP socket so that it can be reused if it is in the wait state.
int reuseAddr = 1;
setsockopt(pcSock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&reuseAddr), sizeof(reuseAddr));
// Bind socket to local address.
// printf ("Binding to socket\n");
if (bind (pcSock, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
{
perror ("bind");
close (pcSock);
continue;
}
// Create queue for client connection requests.
//printf ("Listening on socket (port:%d)\n",VIDEO_TO_PC_PORT);
if (listen (pcSock, 1) == ERROR)
{
perror ("listen");
close (pcSock);
continue;
}
// Accept a new connect request
struct sockaddr_in clientAddr;
int clientAddrSize;
int newPCSock = accept (pcSock, reinterpret_cast<sockaddr*>(&clientAddr), &clientAddrSize);
if (newPCSock == ERROR) {
perror ("accept");
close(pcSock);
continue;
}
// Got a connection.
//TODO: check camera error
//if (!StatusIsFatal()) {
while(!ShouldStopImageToPCServer()) {
double newTimestamp;
int numBytes = 0;
char* imageData = NULL;
//printf ("Getting image from camera\n");
success = GetImageDataBlocking(&imageData, &numBytes, &newTimestamp, lastImageTimestamp);
if (!success) {
// If camera is not initialzed you will get failure and
// the timestamp is invalid. Reset this value and try again.
lastImageTimestamp = 0;
continue;
}
lastImageTimestamp = newTimestamp;
//printf ("Writing image to PC\n");
/* Write header to PC */
static const char header[4]={1,0,0,0};
int headerSend = write(newPCSock, const_cast<char*>(header), 4);
/* Write image length to PC */
int presend = write(newPCSock, reinterpret_cast<char*>(&numBytes), 4);
/* Write image to PC */
int sent = write (newPCSock, imageData, numBytes);
// Cleanup memory allocated for the image.
delete imageData;
imageData = NULL;
numBytes = 0;
// The PC probably closed connection. Get out of here
// and try listening again.
if (headerSend == ERROR || sent == ERROR || presend == ERROR) {
break;
}
//no point in running too fast -
//max camera frame rate is 30 fps
Wait(0.01);
}
// Clean up
close (newPCSock);
newPCSock = ERROR;
close (pcSock);
pcSock = ERROR;
taskUnsafe();
Wait(0.1);
}
return (OK);
}