123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <sys/ioctl.h>
- #include "asterisk/file.h"
- #include "asterisk/utils.h"
- #include "console_video.h"
- #if defined(HAVE_VIDEO_CONSOLE)
- #ifdef HAVE_X11
- #include <X11/Xlib.h>
- struct grab_x11_desc {
- Display *dpy;
- XImage *image;
- int screen_width;
- int screen_height;
- struct fbuf_t b;
- };
- static void *grab_x11_close(void *desc);
- static void *grab_x11_open(const char *name, struct fbuf_t *geom, int fps)
- {
- XImage *im;
- int screen_num;
- struct grab_x11_desc *v;
- struct fbuf_t *b;
-
- if (strncasecmp(name, "X11", 3))
- return NULL;
- v = ast_calloc(1, sizeof(*v));
- if (v == NULL)
- return NULL;
-
- v->dpy = XOpenDisplay(NULL);
- if (v->dpy == NULL) {
- ast_log(LOG_WARNING, "error opening display\n");
- goto error;
- }
- v->b = *geom;
- b = &v->b;
-
- screen_num = DefaultScreen(v->dpy);
- v->screen_width = DisplayWidth(v->dpy, screen_num);
- v->screen_height = DisplayHeight(v->dpy, screen_num);
- v->image = im = XGetImage(v->dpy,
- RootWindow(v->dpy, DefaultScreen(v->dpy)),
- b->x, b->y, b->w, b->h, AllPlanes, ZPixmap);
- if (v->image == NULL) {
- ast_log(LOG_WARNING, "error creating Ximage\n");
- goto error;
- }
- switch (im->bits_per_pixel) {
- case 32:
- b->pix_fmt = PIX_FMT_RGBA32;
- break;
- case 16:
- b->pix_fmt = (im->green_mask == 0x7e0) ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
- break;
- }
- ast_log(LOG_NOTICE, "image: data %p %d bpp fmt %d, mask 0x%lx 0x%lx 0x%lx\n",
- im->data,
- im->bits_per_pixel,
- b->pix_fmt,
- im->red_mask, im->green_mask, im->blue_mask);
-
- b->data = (uint8_t *)im->data;
- return v;
- error:
- return grab_x11_close(v);
- }
- static struct fbuf_t *grab_x11_read(void *desc)
- {
-
- struct grab_x11_desc *v = desc;
- struct fbuf_t *b = &v->b;
- XGetSubImage(v->dpy,
- RootWindow(v->dpy, DefaultScreen(v->dpy)),
- b->x, b->y, b->w, b->h, AllPlanes, ZPixmap, v->image, 0, 0);
- b->data = (uint8_t *)v->image->data;
- return b;
- }
- static int boundary_checks(int x, int limit)
- {
- return (x <= 0) ? 0 : (x > limit ? limit : x);
- }
- static void grab_x11_move(void *desc, int dx, int dy)
- {
- struct grab_x11_desc *v = desc;
- v->b.x = boundary_checks(v->b.x + dx, v->screen_width - v->b.w);
- v->b.y = boundary_checks(v->b.y + dy, v->screen_height - v->b.h);
- }
- static void *grab_x11_close(void *desc)
- {
- struct grab_x11_desc *v = desc;
- if (v->dpy)
- XCloseDisplay(v->dpy);
- v->dpy = NULL;
- v->image = NULL;
- ast_free(v);
- return NULL;
- }
- static struct grab_desc grab_x11_desc = {
- .name = "X11",
- .open = grab_x11_open,
- .read = grab_x11_read,
- .move = grab_x11_move,
- .close = grab_x11_close,
- };
- #endif
- #ifdef HAVE_VIDEODEV_H
- #include <linux/videodev.h>
- struct grab_v4l1_desc {
- int fd;
- struct fbuf_t b;
- };
- static void *grab_v4l1_open(const char *dev, struct fbuf_t *geom, int fps)
- {
- struct video_window vw = { 0 };
- struct video_picture vp;
- int fd, i;
- struct grab_v4l1_desc *v;
- struct fbuf_t *b;
-
- if (strncmp(dev, "/dev/", 5))
- return NULL;
- fd = open(dev, O_RDONLY | O_NONBLOCK);
- if (fd < 0) {
- ast_log(LOG_WARNING, "error opening camera %s\n", dev);
- return NULL;
- }
- v = ast_calloc(1, sizeof(*v));
- if (v == NULL) {
- ast_log(LOG_WARNING, "no memory for camera %s\n", dev);
- close(fd);
- return NULL;
- }
- v->fd = fd;
- v->b = *geom;
- b = &v->b;
- i = fcntl(fd, F_GETFL);
- if (-1 == fcntl(fd, F_SETFL, i | O_NONBLOCK)) {
-
- ast_log(LOG_WARNING, "error F_SETFL for %s [%s]\n",
- dev, strerror(errno));
- }
-
- vw.width = b->w;
- vw.height = b->h;
- vw.flags = fps << 16;
- if (ioctl(fd, VIDIOCSWIN, &vw) == -1) {
- ast_log(LOG_WARNING, "error setting format for %s [%s]\n",
- dev, strerror(errno));
- goto error;
- }
- if (ioctl(fd, VIDIOCGPICT, &vp) == -1) {
- ast_log(LOG_WARNING, "error reading picture info\n");
- goto error;
- }
- ast_log(LOG_WARNING,
- "contrast %d bright %d colour %d hue %d white %d palette %d\n",
- vp.contrast, vp.brightness,
- vp.colour, vp.hue,
- vp.whiteness, vp.palette);
-
- b->pix_fmt = vp.palette;
- vp.palette = VIDEO_PALETTE_YUV420P;
- if (ioctl(v->fd, VIDIOCSPICT, &vp) == -1) {
- ast_log(LOG_WARNING, "error setting palette, using %d\n",
- b->pix_fmt);
- } else
- b->pix_fmt = vp.palette;
-
- b->size = (b->w * b->h * 3)/2;
- ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n",
- dev, b->w, b->h, b->size);
- b->data = ast_calloc(1, b->size);
- if (!b->data) {
- ast_log(LOG_WARNING, "error allocating buffer %d bytes\n",
- b->size);
- goto error;
- }
- ast_log(LOG_WARNING, "success opening camera\n");
- return v;
- error:
- close(v->fd);
- fbuf_free(b);
- ast_free(v);
- return NULL;
- }
- static struct fbuf_t *grab_v4l1_read(void *desc)
- {
- struct grab_v4l1_desc *v = desc;
- struct fbuf_t *b = &v->b;
- for (;;) {
- int r, l = b->size - b->used;
- r = read(v->fd, b->data + b->used, l);
-
- if (r < 0)
- break;
- if (r == 0)
- break;
- b->used += r;
- if (r == l) {
- b->used = 0;
- return b;
- }
- }
- return NULL;
- }
- static void *grab_v4l1_close(void *desc)
- {
- struct grab_v4l1_desc *v = desc;
- close(v->fd);
- v->fd = -1;
- fbuf_free(&v->b);
- ast_free(v);
- return NULL;
- }
- static struct grab_desc grab_v4l1_desc = {
- .name = "v4l1",
- .open = grab_v4l1_open,
- .read = grab_v4l1_read,
- .close = grab_v4l1_close,
- };
- #endif
- struct grab_desc *console_grabbers[] = {
- #ifdef HAVE_X11
- &grab_x11_desc,
- #endif
- #ifdef HAVE_VIDEODEV_H
- &grab_v4l1_desc,
- #endif
- NULL
- };
- #endif
|