LibVNCServer/LibVNCClient
camera.c
Go to the documentation of this file.
1 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <rfb/rfb.h>
44 #ifdef LIBVNCSERVER_HAVE_GETTIMEOFDAY
45 /* if we have gettimeofday(), it is in this header */
46 #include <sys/time.h>
47 #endif
48 #if !defined LIBVNCSERVER_HAVE_GETTIMEOFDAY && defined WIN32
49 #include <fcntl.h>
50 #include <conio.h>
51 #include <sys/timeb.h>
52 
53 static void gettimeofday(struct timeval* tv,char* dummy)
54 {
55  SYSTEMTIME t;
56  GetSystemTime(&t);
57  tv->tv_sec=t.wHour*3600+t.wMinute*60+t.wSecond;
58  tv->tv_usec=t.wMilliseconds*1000;
59 }
60 #endif
61 
62 
63 #define WIDTH 640
64 #define HEIGHT 480
65 #define BPP 4
66 
67 /* 15 frames per second (if we can) */
68 #define PICTURE_TIMEOUT (1.0/15.0)
69 
70 
71 /*
72  * throttle camera updates
73 */
75  static struct timeval now={0,0}, then={0,0};
76  double elapsed, dnow, dthen;
77 
78  gettimeofday(&now,NULL);
79 
80  dnow = now.tv_sec + (now.tv_usec /1000000.0);
81  dthen = then.tv_sec + (then.tv_usec/1000000.0);
82  elapsed = dnow - dthen;
83 
84  if (elapsed > PICTURE_TIMEOUT)
85  memcpy((char *)&then, (char *)&now, sizeof(struct timeval));
86  return elapsed > PICTURE_TIMEOUT;
87 }
88 
89 
90 
91 /*
92  * simulate grabbing a picture from some device
93  */
94 int TakePicture(unsigned char *buffer)
95 {
96  static int last_line=0, fps=0, fcount=0;
97  int line=0;
98  int i,j;
99  struct timeval now;
100 
101  /*
102  * simulate grabbing data from a device by updating the entire framebuffer
103  */
104 
105  for(j=0;j<HEIGHT;++j) {
106  for(i=0;i<WIDTH;++i) {
107  buffer[(j*WIDTH+i)*BPP+0]=(i+j)*128/(WIDTH+HEIGHT); /* red */
108  buffer[(j*WIDTH+i)*BPP+1]=i*128/WIDTH; /* green */
109  buffer[(j*WIDTH+i)*BPP+2]=j*256/HEIGHT; /* blue */
110  }
111  buffer[j*WIDTH*BPP+0]=0xff;
112  buffer[j*WIDTH*BPP+1]=0xff;
113  buffer[j*WIDTH*BPP+2]=0xff;
114  }
115 
116  /*
117  * simulate the passage of time
118  *
119  * draw a simple black line that moves down the screen. The faster the
120  * client, the more updates it will get, the smoother it will look!
121  */
122  gettimeofday(&now,NULL);
123  line = now.tv_usec / (1000000/HEIGHT);
124  if (line>=HEIGHT) line=HEIGHT-1;
125  memset(&buffer[(WIDTH * BPP) * line], 0, (WIDTH * BPP));
126 
127  /* frames per second (informational only) */
128  fcount++;
129  if (last_line > line) {
130  fps = fcount;
131  fcount = 0;
132  }
133  last_line = line;
134  fprintf(stderr,"%03d/%03d Picture (%03d fps)\r", line, HEIGHT, fps);
135 
136  /* success! We have a new picture! */
137  return (1==1);
138 }
139 
140 
141 
142 
143 /*
144  * Single-threaded application that interleaves client servicing with taking
145  * pictures from the camera. This way, we do not update the framebuffer
146  * while an encoding is working on it too (banding, and image artifacts).
147  */
148 int main(int argc,char** argv)
149 {
150  long usec;
151 
152  rfbScreenInfoPtr server=rfbGetScreen(&argc,argv,WIDTH,HEIGHT,8,3,BPP);
153  if(!server)
154  return 1;
155  server->desktopName = "Live Video Feed Example";
156  server->frameBuffer=(char*)malloc(WIDTH*HEIGHT*BPP);
157  server->alwaysShared=(1==1);
158 
159  /* Initialize the server */
160  rfbInitServer(server);
161 
162  /* Loop, processing clients and taking pictures */
163  while (rfbIsActive(server)) {
164  if (TimeToTakePicture())
165  if (TakePicture((unsigned char *)server->frameBuffer))
166  rfbMarkRectAsModified(server,0,0,WIDTH,HEIGHT);
167 
168  usec = server->deferUpdateTime*1000;
169  rfbProcessEvents(server,usec);
170  }
171  return(0);
172 }
#define WIDTH
Definition: camera.c:63
int TakePicture(unsigned char *buffer)
Definition: camera.c:94
int main(int argc, char **argv)
Definition: camera.c:148
#define PICTURE_TIMEOUT
Definition: camera.c:68
#define BPP
Definition: camera.c:65
#define HEIGHT
Definition: camera.c:64
int TimeToTakePicture()
Definition: camera.c:74
void rfbInitServer(rfbScreenInfoPtr rfbScreen)
void rfbMarkRectAsModified(rfbScreenInfoPtr rfbScreen, int x1, int y1, int x2, int y2)
rfbScreenInfoPtr rfbGetScreen(int *argc, char **argv, int width, int height, int bitsPerSample, int samplesPerPixel, int bytesPerPixel)
rfbBool rfbIsActive(rfbScreenInfoPtr screenInfo)
rfbBool rfbProcessEvents(rfbScreenInfoPtr screenInfo, long usec)