00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #include "asterisk.h"
00052 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00053 #include <sys/ioctl.h>
00054 #include "asterisk/file.h"
00055 #include "asterisk/utils.h"
00056
00057 #include "console_video.h"
00058
00059 #if defined(HAVE_VIDEO_CONSOLE)
00060
00061 #ifdef HAVE_X11
00062
00063
00064
00065 #include <X11/Xlib.h>
00066
00067
00068 struct grab_x11_desc {
00069 Display *dpy;
00070 XImage *image;
00071 int screen_width;
00072 int screen_height;
00073 struct fbuf_t b;
00074 };
00075
00076 static void *grab_x11_close(void *desc);
00077
00078
00079
00080
00081 static void *grab_x11_open(const char *name, struct fbuf_t *geom, int fps)
00082 {
00083 XImage *im;
00084 int screen_num;
00085 struct grab_x11_desc *v;
00086 struct fbuf_t *b;
00087
00088
00089 if (strncasecmp(name, "X11", 3))
00090 return NULL;
00091 v = ast_calloc(1, sizeof(*v));
00092 if (v == NULL)
00093 return NULL;
00094
00095
00096 v->dpy = XOpenDisplay(NULL);
00097 if (v->dpy == NULL) {
00098 ast_log(LOG_WARNING, "error opening display\n");
00099 goto error;
00100 }
00101
00102 v->b = *geom;
00103 b = &v->b;
00104
00105 screen_num = DefaultScreen(v->dpy);
00106 v->screen_width = DisplayWidth(v->dpy, screen_num);
00107 v->screen_height = DisplayHeight(v->dpy, screen_num);
00108
00109 v->image = im = XGetImage(v->dpy,
00110 RootWindow(v->dpy, DefaultScreen(v->dpy)),
00111 b->x, b->y, b->w, b->h, AllPlanes, ZPixmap);
00112 if (v->image == NULL) {
00113 ast_log(LOG_WARNING, "error creating Ximage\n");
00114 goto error;
00115 }
00116 switch (im->bits_per_pixel) {
00117 case 32:
00118 b->pix_fmt = PIX_FMT_RGBA32;
00119 break;
00120 case 16:
00121 b->pix_fmt = (im->green_mask == 0x7e0) ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
00122 break;
00123 }
00124
00125 ast_log(LOG_NOTICE, "image: data %p %d bpp fmt %d, mask 0x%lx 0x%lx 0x%lx\n",
00126 im->data,
00127 im->bits_per_pixel,
00128 b->pix_fmt,
00129 im->red_mask, im->green_mask, im->blue_mask);
00130
00131
00132 b->data = (uint8_t *)im->data;
00133 return v;
00134
00135 error:
00136 return grab_x11_close(v);
00137 }
00138
00139 static struct fbuf_t *grab_x11_read(void *desc)
00140 {
00141
00142 struct grab_x11_desc *v = desc;
00143 struct fbuf_t *b = &v->b;
00144
00145 XGetSubImage(v->dpy,
00146 RootWindow(v->dpy, DefaultScreen(v->dpy)),
00147 b->x, b->y, b->w, b->h, AllPlanes, ZPixmap, v->image, 0, 0);
00148
00149 b->data = (uint8_t *)v->image->data;
00150 return b;
00151 }
00152
00153 static int boundary_checks(int x, int limit)
00154 {
00155 return (x <= 0) ? 0 : (x > limit ? limit : x);
00156 }
00157
00158
00159
00160
00161 static void grab_x11_move(void *desc, int dx, int dy)
00162 {
00163 struct grab_x11_desc *v = desc;
00164
00165 v->b.x = boundary_checks(v->b.x + dx, v->screen_width - v->b.w);
00166 v->b.y = boundary_checks(v->b.y + dy, v->screen_height - v->b.h);
00167 }
00168
00169
00170 static void *grab_x11_close(void *desc)
00171 {
00172 struct grab_x11_desc *v = desc;
00173
00174 if (v->dpy)
00175 XCloseDisplay(v->dpy);
00176 v->dpy = NULL;
00177 v->image = NULL;
00178 ast_free(v);
00179 return NULL;
00180 }
00181
00182 static struct grab_desc grab_x11_desc = {
00183 .name = "X11",
00184 .open = grab_x11_open,
00185 .read = grab_x11_read,
00186 .move = grab_x11_move,
00187 .close = grab_x11_close,
00188 };
00189 #endif
00190
00191 #ifdef HAVE_VIDEODEV_H
00192 #include <linux/videodev.h>
00193
00194 struct grab_v4l1_desc {
00195 int fd;
00196 struct fbuf_t b;
00197 };
00198
00199
00200
00201
00202
00203 static void *grab_v4l1_open(const char *dev, struct fbuf_t *geom, int fps)
00204 {
00205 struct video_window vw = { 0 };
00206 struct video_picture vp;
00207 int fd, i;
00208 struct grab_v4l1_desc *v;
00209 struct fbuf_t *b;
00210
00211
00212 if (strncmp(dev, "/dev/", 5))
00213 return NULL;
00214 fd = open(dev, O_RDONLY | O_NONBLOCK);
00215 if (fd < 0) {
00216 ast_log(LOG_WARNING, "error opening camera %s\n", dev);
00217 return NULL;
00218 }
00219
00220 v = ast_calloc(1, sizeof(*v));
00221 if (v == NULL) {
00222 ast_log(LOG_WARNING, "no memory for camera %s\n", dev);
00223 close(fd);
00224 return NULL;
00225 }
00226 v->fd = fd;
00227 v->b = *geom;
00228 b = &v->b;
00229
00230 i = fcntl(fd, F_GETFL);
00231 if (-1 == fcntl(fd, F_SETFL, i | O_NONBLOCK)) {
00232
00233 ast_log(LOG_WARNING, "error F_SETFL for %s [%s]\n",
00234 dev, strerror(errno));
00235 }
00236
00237
00238
00239
00240 vw.width = b->w;
00241 vw.height = b->h;
00242 vw.flags = fps << 16;
00243 if (ioctl(fd, VIDIOCSWIN, &vw) == -1) {
00244 ast_log(LOG_WARNING, "error setting format for %s [%s]\n",
00245 dev, strerror(errno));
00246 goto error;
00247 }
00248 if (ioctl(fd, VIDIOCGPICT, &vp) == -1) {
00249 ast_log(LOG_WARNING, "error reading picture info\n");
00250 goto error;
00251 }
00252 ast_log(LOG_WARNING,
00253 "contrast %d bright %d colour %d hue %d white %d palette %d\n",
00254 vp.contrast, vp.brightness,
00255 vp.colour, vp.hue,
00256 vp.whiteness, vp.palette);
00257
00258
00259
00260
00261 b->pix_fmt = vp.palette;
00262 vp.palette = VIDEO_PALETTE_YUV420P;
00263 if (ioctl(v->fd, VIDIOCSPICT, &vp) == -1) {
00264 ast_log(LOG_WARNING, "error setting palette, using %d\n",
00265 b->pix_fmt);
00266 } else
00267 b->pix_fmt = vp.palette;
00268
00269
00270
00271
00272 b->size = (b->w * b->h * 3)/2;
00273 ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n",
00274 dev, b->w, b->h, b->size);
00275 b->data = ast_calloc(1, b->size);
00276 if (!b->data) {
00277 ast_log(LOG_WARNING, "error allocating buffer %d bytes\n",
00278 b->size);
00279 goto error;
00280 }
00281 ast_log(LOG_WARNING, "success opening camera\n");
00282 return v;
00283
00284 error:
00285 close(v->fd);
00286 fbuf_free(b);
00287 ast_free(v);
00288 return NULL;
00289 }
00290
00291
00292
00293
00294
00295 static struct fbuf_t *grab_v4l1_read(void *desc)
00296 {
00297 struct grab_v4l1_desc *v = desc;
00298 struct fbuf_t *b = &v->b;
00299 for (;;) {
00300 int r, l = b->size - b->used;
00301 r = read(v->fd, b->data + b->used, l);
00302
00303 if (r < 0)
00304 break;
00305 if (r == 0)
00306 break;
00307 b->used += r;
00308 if (r == l) {
00309 b->used = 0;
00310 return b;
00311 }
00312 }
00313 return NULL;
00314 }
00315
00316 static void *grab_v4l1_close(void *desc)
00317 {
00318 struct grab_v4l1_desc *v = desc;
00319
00320 close(v->fd);
00321 v->fd = -1;
00322 fbuf_free(&v->b);
00323 ast_free(v);
00324 return NULL;
00325 }
00326
00327
00328 static struct grab_desc grab_v4l1_desc = {
00329 .name = "v4l1",
00330 .open = grab_v4l1_open,
00331 .read = grab_v4l1_read,
00332 .close = grab_v4l1_close,
00333 };
00334 #endif
00335
00336
00337
00338
00339
00340
00341
00342 struct grab_desc *console_grabbers[] = {
00343 #ifdef HAVE_X11
00344 &grab_x11_desc,
00345 #endif
00346 #ifdef HAVE_VIDEODEV_H
00347 &grab_v4l1_desc,
00348 #endif
00349 NULL
00350 };
00351
00352 #endif