File: xvcam.c

package info (click to toggle)
came 1.3-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 200 kB
  • ctags: 179
  • sloc: ansic: 1,636; makefile: 64
file content (239 lines) | stat: -rw-r--r-- 6,112 bytes parent folder | download
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
/* View streams from a webcam with Xv */
/* Parts from an app used to test Trident drivers, and camE */
/* $Id: xvcam.c,v 1.1 2001/06/21 18:03:49 gilbertt Exp $ */
/* gcc xvcam.c -o xvcam -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lXext -lXv */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <X11/extensions/XShm.h>

#include <linux/videodev.h>

#define GUID_I420_PLANAR 0x30323449


extern int XShmGetEventBase(Display *);
extern XvImage *XvShmCreateImage(Display *, XvPortID, int, char *, int, int,
                                 XShmSegmentInfo *);

int grab_fd, grab_size;
static struct video_mmap grab_buf;
int yuv_width = 320;
int yuv_height = 240;
static unsigned char *grab_data = NULL;

void
deinit()
{
   munmap(grab_data, grab_size);
   grab_data = NULL;
   close(grab_fd);
}

void
init()
{
   struct video_capability grab_cap;
   struct video_channel grab_chan;
   struct video_mbuf vid_mbuf;

   atexit(deinit);

   if ((grab_fd = open("/dev/video0", O_RDWR)) == -1) {
      fprintf(stderr, "Couldn't open /dev/video0: %s\n", strerror(errno));
      exit(-1);
   }

   if (ioctl(grab_fd, VIDIOCGCAP, &grab_cap) == -1) {
      perror("ioctl VIDIOCGCAP");
      exit(-1);
   }

   grab_chan.channel = 0;

   if (ioctl(grab_fd, VIDIOCGCHAN, &grab_chan) == -1) {
      perror("ioctl VIDOCGCHAN");
      exit(-1);
   }

   grab_chan.norm = 0;

   if (ioctl(grab_fd, VIDIOCSCHAN, &grab_chan) == -1) {
      perror("ioctl VIDIOCSCHAN");
      exit(-1);
   }

   grab_buf.format = VIDEO_PALETTE_YUV420P;
   grab_buf.frame = 0;
   grab_buf.width = yuv_width;
   grab_buf.height = yuv_height;

   ioctl(grab_fd, VIDIOCGMBUF, &vid_mbuf);

   grab_size = vid_mbuf.size;
   grab_data =
       mmap(0, grab_size, PROT_READ | PROT_WRITE, MAP_SHARED, grab_fd, 0);

   if ((grab_data == NULL) || (-1 == (int) grab_data)) {
      fprintf(stderr, "Couldn't mmap\n");
      exit(1);
   }

   /* Useless? probably. */
   setpriority(PRIO_PROCESS, 0, 20);
   nice(20);
}


unsigned char *
grab_image()
{
   int i = 0;
   fd_set fds;

   FD_ZERO(&fds);
   FD_SET(0, &fds);
   FD_SET(grab_fd, &fds);

   /* Maybe a little nicer to the cpu ? */
   select(grab_fd + 1, &fds, NULL, NULL, NULL); 

   if (ioctl(grab_fd, VIDIOCMCAPTURE, &grab_buf) == -1) {
      perror("ioctl VIDIOCMCAPTURE");
      return NULL;
   }

   if (ioctl(grab_fd, VIDIOCSYNC, &i) == -1) {
      perror("ioctrl VIDIOCSYNC");
      return NULL;
   }

   return grab_data;
}

int
main(int argc, char **argv)
{
   int xv_port = -1, i, d, screen, CompletionType;
   unsigned int ud, width, height;
   long frames;
   unsigned int ver, rel, req, ev, err, adapt;
   Display *dpy;
   Window window, _dw;
   XSizeHints hint;
   XSetWindowAttributes xswa;
   XWindowAttributes attribs;
   XVisualInfo vinfo;
   XEvent event;
   GC gc;
   XvAdaptorInfo *ai;
   XvImage *yuv_image;
   XShmSegmentInfo yuv_shminfo;
   Atom wmDeleteWindow;

   dpy = XOpenDisplay(NULL);
   screen = DefaultScreen(dpy);

   XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &attribs);
   XMatchVisualInfo(dpy, screen, attribs.depth, TrueColor, &vinfo);
   wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);

   hint.x = 1;
   hint.y = 1;
   hint.width = yuv_width;
   hint.height = yuv_height;
   hint.flags = PPosition | PSize;

   xswa.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vinfo.visual, 
                                   AllocNone);
   xswa.event_mask = StructureNotifyMask | ExposureMask;
   xswa.background_pixel = 0;
   xswa.border_pixel = 0;

   window = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, yuv_width, 
                          yuv_height, 0, vinfo.depth, InputOutput, vinfo.visual, 
                          CWBackPixel | CWBorderPixel | CWColormap | 
                          CWEventMask, &xswa);

   XSelectInput(dpy, window, StructureNotifyMask);
   XSetStandardProperties(dpy, window, "xvcam", "xvcam", None, NULL, 0,
                          &hint);

   XSetWMProtocols(dpy, window, &wmDeleteWindow, 1);
   XMapWindow(dpy, window);

   if (XShmQueryExtension(dpy))
      CompletionType = XShmGetEventBase(dpy) + ShmCompletion;
   else
      exit(-1);

   if (Success !=
       XvQueryExtension(dpy, &ver, &rel, &req, &ev, &err))
      fprintf(stderr, "Couldn't do Xv shit\n");

   if (Success !=
       XvQueryAdaptors(dpy, DefaultRootWindow(dpy), &adapt, &ai))
      fprintf(stderr, "Couldn't do Xv shit\n");

   for (i = 0; i < (int) adapt; i++) {
      xv_port = ai[i].base_id;
   }
   
   if (adapt > 0)
      XvFreeAdaptorInfo(ai);
   
   gc = XCreateGC(dpy, window, 0, 0);

   yuv_image = XvShmCreateImage(dpy, xv_port, GUID_I420_PLANAR, 0, yuv_width,
                                yuv_height, &yuv_shminfo);
   yuv_shminfo.shmid = shmget(IPC_PRIVATE, yuv_image->data_size, 
                              IPC_CREAT | 0777);
   yuv_shminfo.shmaddr = (char *) shmat(yuv_shminfo.shmid, 0, 0);
   yuv_image->data = yuv_shminfo.shmaddr;
   yuv_shminfo.readOnly = False;

   if (!XShmAttach(dpy, &yuv_shminfo)) {
      printf("XShmAttach go boom boom!\n");
      exit(-1);
   }

   init();

   while (1) {
      grab_image();
      memcpy(yuv_image->data, grab_data, yuv_image->data_size);

      XGetGeometry(dpy, window, &_dw, &d, &d, &width, &height, &ud, &ud);
      XvShmPutImage(dpy, xv_port, window, gc, yuv_image, 0, 0,
                    yuv_image->width, yuv_image->height, 0, 0, width, height,
                    True);
      frames++;

      while (XPending(dpy)) {
         XNextEvent(dpy, &event);
            if (event.type == ClientMessage) {
               if (event.xclient.format == 32 &&
                   event.xclient.data.l[0] == (signed) wmDeleteWindow)
                  exit(0);
            }
      }
               
   }
   return 0;
}