as3-server.c
Go to the documentation of this file.
1 /*
2  * This file is part of the OpenKinect Project. http://www.openkinect.org
3  *
4  * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5  * for details.
6  *
7  * This code is licensed to you under the terms of the Apache License, version
8  * 2.0, or, at your option, the terms of the GNU General Public License,
9  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  * or the following URLs:
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * http://www.gnu.org/licenses/gpl-2.0.txt
13  *
14  * If you redistribute this file in source form, modified or unmodified, you
15  * may:
16  * 1) Leave this header intact and distribute it under the same terms,
17  * accompanying it with the APACHE20 and GPL20 files, or
18  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  * In all cases you must keep the copyright notice intact and include a copy
21  * of the CONTRIB file.
22  *
23  * Binary distributions must follow the binary distribution requirements of
24  * either License.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #ifndef _WIN32
31  #include <unistd.h>
32  #include <arpa/inet.h>
33 #else
34  #include <winsock2.h>
35  #include <ws2tcpip.h>
36  #pragma comment(lib, "Ws2_32.lib")
37  #include <stdint.h>
38  #include <windows.h>
39  unsigned sleep(unsigned seconds)
40  {
41  Sleep(seconds*1000); // The Windows Sleep operates on milliseconds
42  return(0);
43  }
44 #endif
45 
46 #include <string.h>
47 #include "libfreenect_sync.h"
48 #include "libfreenect.h"
49 #include <pthread.h>
50 #include <math.h>
51 
52 #include "freenect_network.h"
53 #include "as3_jpeg.h"
54 #include "libfreenect_sync.h"
55 
56 int g_argc;
57 char **g_argv;
58 
59 char *_current_version = "v0.9c";
61 
62 #define AS3_BITMAPDATA_LEN 640 * 480 * 4
63 
68 
69 int die = 0;
70 int psent = 0;
71 
74  _min_depth = 600,
75  _max_depth = 800,
78 
80 
81 #ifdef _WIN32
82  unsigned sleep(unsigned seconds)
83  {
84  Sleep(seconds*1000); // The Windows Sleep operates on milliseconds
85  return(0);
86  }
87 #endif
88 
89 void send_policy_file(int child){
90  /*if(psent == 0){
91  char * str = "<?xml version='1.0'?><!DOCTYPE cross-domain-policy SYSTEM '/xml/dtds/cross-domain-policy.dtd'><cross-domain-policy><site-control permitted-cross-domain-policies='all'/><allow-access-from domain='*' to-ports='*'/></cross-domain-policy>\n";
92  #ifdef WIN32
93  int n = send(data_client_socket, (char*)str, 235, 0);
94  #else
95  int n = write(child,str , 235);
96  #endif
97 
98  if ( n < 0 || n != 235)
99  {
100  fprintf(stderr, "Error on write() for policy (%d instead of %d)\n",n, 235);
101  }
102  //psent = 1;
103  }*/
104 }
105 
106 //send depth ARGB to client
107 void sendDepth(){
108  int n;
109  uint32_t ts, x, y, i, j;
110  freenect_sync_get_depth(&buf_depth_temp, &ts, 0, FREENECT_DEPTH_11BIT);
111  uint16_t *depth = (uint16_t*) buf_depth_temp;
113  //uint16_t *tmp_depth = (uint16_t*) malloc(FREENECT_DEPTH_11BIT_SIZE);
114  uint16_t *tmp_depth = (uint16_t*) malloc(depth_mode.bytes);
115  if(_depth_mirrored){ //MIRROR DEPTH DATA
116  for(x = 0; x < depth_mode.width; x++){
117  for(y = 0; y < depth_mode.height; y++){
118  i = x + (y * depth_mode.width);
119  j = (depth_mode.width - x - 1) + (y * depth_mode.width);
120  tmp_depth[i] = depth[j];
121  }
122  }
123  depth = tmp_depth;
124  }
125 
126  for (i=0; i< depth_mode.width * depth_mode.height; i++) {
127  if(_depth_compression != 0) {
128  buf_depth[3 * i + 0] = 0x00;
129  buf_depth[3 * i + 1] = 0x00;
130  buf_depth[3 * i + 2] = 0x00;
131  } else {
132  buf_depth[4 * i + 0] = 0x00;
133  buf_depth[4 * i + 1] = 0x00;
134  buf_depth[4 * i + 2] = 0x00;
135  buf_depth[4 * i + 3] = 0xFF;
136  }
137  if(depth[i] < _max_depth && depth[i] > _min_depth){
138  unsigned char l = 0xFF - ((depth[i] - _min_depth) & 0xFF);
139  if(_depth_compression != 0) {
140  buf_depth[3 * i + 0] = l;
141  buf_depth[3 * i + 1] = l;
142  buf_depth[3 * i + 2] = l;
143  } else {
144  buf_depth[4 * i + 0] = l;
145  buf_depth[4 * i + 1] = l;
146  buf_depth[4 * i + 2] = l;
147  buf_depth[4 * i + 3] = 0xFF;
148  }
149  }
150  }
151  if(_depth_compression != 0) {
152  unsigned char *compressed_buff = (unsigned char *)malloc(AS3_BITMAPDATA_LEN);
153  unsigned long len = 0;
154  RGB_2_JPEG(buf_depth, &compressed_buff, &len, _depth_compression);
155  n = freenect_network_sendMessage(0, 0, compressed_buff, (int)len);
156  free(compressed_buff);
157  } else {
158  n = freenect_network_sendMessage(0, 0, buf_depth, AS3_BITMAPDATA_LEN);
159  }
160  if (n < 0)
161  {
162  printf("Error sending depth\n");
163  client_connected = 0;
164  }
165  free(tmp_depth);
166 }
167 
168 //send depth ARGB to client
170  uint32_t ts, x, y, i, j;
171  freenect_sync_get_depth(&buf_depth_temp, &ts, 0, FREENECT_DEPTH_11BIT);
172  uint16_t *depth = (uint16_t*) buf_depth_temp;
174  uint16_t *tmp_depth = (uint16_t*) malloc(depth_mode.bytes);
175  unsigned char *char_depth = (unsigned char *) malloc(depth_mode.bytes);
176 
177  if(_depth_mirrored){ //MIRROR DEPTH DATA
178  for(x = 0; x < depth_mode.width; x++){
179  for(y = 0; y < depth_mode.height; y++){
180  i = x + (y * depth_mode.width);
181  j = (depth_mode.width - x - 1) + (y * depth_mode.width);
182  tmp_depth[i] = depth[j];
183  }
184  }
185  depth = tmp_depth;
186  }
187 
188  for (i=0; i<640 * 480; i++) {
189  memcpy(char_depth + (i*2), &depth[i], 2);
190  }
191 
192  int n = freenect_network_sendMessage(0, 1, char_depth, depth_mode.bytes);
193  if (n < 0)
194  {
195  printf("Error sending raw depth\n");
196  client_connected = 0;
197  }
198  free(char_depth);
199  free(tmp_depth);
200 }
201 
202 //send video ARGB to client
203 void sendVideo(){
204  int n;
205  uint32_t ts,x, y, i, j;
206  freenect_sync_get_video(&buf_rgb_temp, &ts, 0, FREENECT_VIDEO_RGB);
207  uint8_t *rgb = (uint8_t*)buf_rgb_temp;
209 
210  //MIRROR RGB DATA AND ADD ALPHA
211  for(x = 0; x < video_mode.width; x++){
212  for(y = 0; y < video_mode.height; y++){
213  i = x + (y * video_mode.width);
214  if(!_video_mirrored)
215  j = i;
216  else
217  j = (video_mode.width - x - 1) + (y * video_mode.width);
218  if(_video_compression != 0) {
219  buf_rgb[3 * i + 2] = rgb[3 * j + 2];
220  buf_rgb[3 * i + 1] = rgb[3 * j + 1];
221  buf_rgb[3 * i + 0] = rgb[3 * j + 0];
222  } else {
223  buf_rgb[4 * i + 0] = rgb[3 * j + 2];
224  buf_rgb[4 * i + 1] = rgb[3 * j + 1];
225  buf_rgb[4 * i + 2] = rgb[3 * j + 0];
226  buf_rgb[4 * i + 3] = 0x00;
227  }
228  }
229  }
230  if(_video_compression != 0) {
231  unsigned char *compressed_buff = (unsigned char *)malloc(AS3_BITMAPDATA_LEN);
232  unsigned long len = 0;
233  RGB_2_JPEG(buf_rgb, &compressed_buff, &len, _video_compression);
234  n = freenect_network_sendMessage(0, 2, compressed_buff, (int)len);
235  free(compressed_buff);
236  } else {
238  }
239  if ( n < 0)
240  {
241  printf("Error sending Video\n");
242  client_connected = 0;
243  }
244 }
245 
246 //send accelerometer data to client
248  int16_t ax,ay,az;
249  double dx,dy,dz;
250  unsigned char buffer_send[3*2+3*8];
252  freenect_sync_get_tilt_state(&state, 0);
253  ax = state->accelerometer_x;
254  ay = state->accelerometer_y;
255  az = state->accelerometer_z;
256  freenect_get_mks_accel(state, &dx, &dy, &dz);
257  memcpy(&buffer_send,&ax, sizeof(int16_t));
258  memcpy(&buffer_send[2],&ay, sizeof(int16_t));
259  memcpy(&buffer_send[4],&az, sizeof(int16_t));
260  memcpy(&buffer_send[6],&dx, sizeof(double));
261  memcpy(&buffer_send[14],&dy, sizeof(double));
262  memcpy(&buffer_send[22],&dz, sizeof(double));
263  int n = freenect_network_sendMessage(1, 2, buffer_send, 3*2+3*8);
264  if ( n < 0)
265  {
266  printf("Error sending Accelerometers\n");
267  client_connected = 0;
268  }
269 }
270 
271 //Connection protocol handler
272 void *connection_handler(void *arg) {
273  int value;
274  int len = 8*10;
275  char *buff = (char*)malloc(len); //Command buffer
276  //send_policy_file(data_child);
277  while(client_connected) {
278  freenect_network_read(buff, &len);
279  //If command length is multiple of 6
280  if(len > 0 && len % 6 == 0){
281  //Get the number of commands received
282  int max = len / 6;
283  int i;
284  //For each command received
285  for(i = 0; i < max; i++){
286  memcpy(&value, &buff[2 + (i*6)], sizeof(int));
287  value = ntohl(value);
288  //The BIG switch (Communication protocol)
289  switch(buff[0 + (i*6)]){
290  case 0: //CAMERA
291  switch(buff[1 + (i*6)]){
292  case 0: //GET DEPTH
293  sendDepth();
294  break;
295  case 1: //GET RAW DEPTH
296  sendRawDepth();
297  break;
298  case 2: //GET RGB
299  sendVideo();
300  break;
301  case 3: //Mirror depth
302  _depth_mirrored = value;
303  break;
304  case 4: //Mirror video
305  _video_mirrored = value;
306  break;
307  case 5: //Min depth
308  _min_depth = value;
309  break;
310  case 6: //Max depth
311  _max_depth = value;
312  break;
313  case 7: //Depth compression
314  _depth_compression = value;
315  break;
316  case 8: //Video compression
317  _video_compression = value;
318  break;
319  }
320  break;
321  case 1: //MOTOR
322  switch(buff[1 + (i*6)]){
323  case 0: //MOVE
324  freenect_sync_set_tilt_degs(value, 0);
325  break;
326  case 1: //LED COLOR
328  break;
329  case 2: //Accelerometer
331  break;
332  }
333  break;
334  }
335  }
336  } else { //Command was not multiple of 6 (we received an invalid command)
337  if(!die) printf("got bad command (%d)\n", len );
338  client_connected = 0;
339  }
340  }
341  if(!die) {
342  printf("Disconecting client...\n");
344  //waiting for client led status
346  }
347  return NULL;
348 }
349 
351  printf("###### Got client\n");
352  //got client led status
354  client_connected = 1;
355  if (pthread_create(&connection_thread, NULL, &connection_handler, NULL)) {
356  fprintf(stderr, "Error on pthread_create() for SERVER\n");
357  }
358 }
359 
360 //Called when C-c or C-z is pressed
361 #ifdef _WIN32
362 void clean_exit(int){
363  die = 1;
364 }
365 #else
366 void clean_exit(){
367  printf("clean exit called\n");
368  die = 1;
370 }
371 #endif
372 //Main: we start here
373 int main(int argc, char **argv)
374 {
375  printf("as3kinect server %s\n", _current_version);
376  //waiting for client led status
378  //Listening to C-c
379  if (signal (SIGINT, clean_exit) == SIG_IGN)
380  signal (SIGINT, SIG_IGN);
381  //Listening to C-z
382  #ifndef _WIN32
383  if (signal (SIGTSTP, clean_exit) == SIG_IGN)
384  signal (SIGTSTP, SIG_IGN);
385  #endif
386  //Alloc memory for video and depth frames
387  buf_depth_temp = malloc(FREENECT_DEPTH_11BIT);
388  buf_rgb_temp = malloc(FREENECT_VIDEO_RGB);
389 
390  //Initialize network socket
392  die = 1;
393 
394  //Sleep main process until exit
395  while(!die)
396  sleep(2);
397 
398  //making a clean exit
399  free(buf_depth_temp);
400  free(buf_rgb_temp);
401  //device off led status
404  printf("\n[as3-server] cleanup done!\n");
405  return 0;
406 }
int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index, freenect_depth_format fmt)
void * connection_handler(void *arg)
Definition: as3-server.c:272
void freenect_get_mks_accel(freenect_raw_tilt_state *state, double *x, double *y, double *z)
Definition: fakenect.c:227
#define AS3_BITMAPDATA_LEN
Definition: as3-server.c:62
short int16_t
unsigned short uint16_t
int _depth_compression
Definition: as3-server.c:77
void server_connected()
Definition: as3-server.c:350
void sendAccelerometers()
Definition: as3-server.c:247
void freenect_network_wait()
int psent
Definition: as3-server.c:70
unsigned char uint8_t
int _depth_mirrored
Definition: as3-server.c:73
void freenect_network_read(char *buff, int *len)
uint8_t buf_depth[AS3_BITMAPDATA_LEN]
Definition: as3-server.c:64
int freenect_sync_set_tilt_degs(int angle, int index)
int client_connected
Definition: as3-server.c:79
void sendVideo()
Definition: as3-server.c:203
int _video_compression
Definition: as3-server.c:76
int die
Definition: as3-server.c:69
int freenect_sync_get_tilt_state(freenect_raw_tilt_state **state, int index)
void * buf_rgb_temp
Definition: as3-server.c:67
int g_argc
Definition: as3-server.c:56
freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt)
Definition: fakenect.c:260
void freenect_network_close()
void sendDepth()
Definition: as3-server.c:107
int _min_depth
Definition: as3-server.c:74
freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt)
Definition: fakenect.c:284
freenect_led_options
Definition: libfreenect.h:148
pthread_t connection_thread
Definition: as3-server.c:60
void freenect_sync_stop(void)
void sendRawDepth()
Definition: as3-server.c:169
uint8_t buf_rgb[AS3_BITMAPDATA_LEN]
Definition: as3-server.c:65
char ** g_argv
Definition: as3-server.c:57
Data from the tilt motor and accelerometer.
Definition: libfreenect.h:167
unsigned int uint32_t
void * buf_depth_temp
Definition: as3-server.c:66
int freenect_sync_get_video(void **video, uint32_t *timestamp, int index, freenect_video_format fmt)
int freenect_network_init(callback cb)
void send_policy_file(int child)
Definition: as3-server.c:89
capture state
Definition: micview.c:53
int freenect_sync_set_led(freenect_led_options led, int index)
int freenect_network_sendMessage(int first, int second, unsigned char *data, int len)
char * _current_version
Definition: as3-server.c:59
int _video_mirrored
Definition: as3-server.c:72
int _max_depth
Definition: as3-server.c:75
void clean_exit()
Definition: as3-server.c:366
int RGB_2_JPEG(unsigned char *buffer, unsigned char **compressed, long unsigned int *new_len, int quality)
Definition: as3_jpeg.c:32
int main(int argc, char **argv)
Definition: as3-server.c:373


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Thu Jun 6 2019 19:25:38