| 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 |
#include "asterisk.h" |
| 52 |
#include <sys/ioctl.h> |
| 53 |
#include "asterisk/file.h" |
| 54 |
#include "asterisk/utils.h" |
| 55 |
|
| 56 |
#include "console_video.h" |
| 57 |
|
| 58 |
#if defined(HAVE_VIDEO_CONSOLE) |
| 59 |
|
| 60 |
#ifdef HAVE_X11 |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#include <X11/Xlib.h> |
| 65 |
|
| 66 |
|
| 67 |
struct grab_x11_desc { |
| 68 |
Display *dpy; |
| 69 |
XImage *image; |
| 70 |
int screen_width; |
| 71 |
int screen_height; |
| 72 |
struct fbuf_t b; |
| 73 |
}; |
| 74 |
|
| 75 |
static void *grab_x11_close(void *desc); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static void *grab_x11_open(const char *name, struct fbuf_t *geom, int fps) |
| 81 |
{ |
| 82 |
XImage *im; |
| 83 |
int screen_num; |
| 84 |
struct grab_x11_desc *v; |
| 85 |
struct fbuf_t *b; |
| 86 |
|
| 87 |
|
| 88 |
if (strncasecmp(name, "X11", 3)) |
| 89 |
return NULL; |
| 90 |
v = ast_calloc(1, sizeof(*v)); |
| 91 |
if (v == NULL) |
| 92 |
return NULL; |
| 93 |
|
| 94 |
|
| 95 |
v->dpy = XOpenDisplay(NULL); |
| 96 |
if (v->dpy == NULL) { |
| 97 |
ast_log(LOG_WARNING, "error opening display\n"); |
| 98 |
goto error; |
| 99 |
} |
| 100 |
|
| 101 |
v->b = *geom; |
| 102 |
b = &v->b; |
| 103 |
|
| 104 |
screen_num = DefaultScreen(v->dpy); |
| 105 |
v->screen_width = DisplayWidth(v->dpy, screen_num); |
| 106 |
v->screen_height = DisplayHeight(v->dpy, screen_num); |
| 107 |
|
| 108 |
v->image = im = XGetImage(v->dpy, |
| 109 |
RootWindow(v->dpy, DefaultScreen(v->dpy)), |
| 110 |
b->x, b->y, b->w, b->h, AllPlanes, ZPixmap); |
| 111 |
if (v->image == NULL) { |
| 112 |
ast_log(LOG_WARNING, "error creating Ximage\n"); |
| 113 |
goto error; |
| 114 |
} |
| 115 |
switch (im->bits_per_pixel) { |
| 116 |
case 32: |
| 117 |
b->pix_fmt = PIX_FMT_RGBA32; |
| 118 |
break; |
| 119 |
case 16: |
| 120 |
b->pix_fmt = (im->green_mask == 0x7e0) ? PIX_FMT_RGB565 : PIX_FMT_RGB555; |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
ast_log(LOG_NOTICE, "image: data %p %d bpp fmt %d, mask 0x%lx 0x%lx 0x%lx\n", |
| 125 |
im->data, |
| 126 |
im->bits_per_pixel, |
| 127 |
b->pix_fmt, |
| 128 |
im->red_mask, im->green_mask, im->blue_mask); |
| 129 |
|
| 130 |
|
| 131 |
b->data = (uint8_t *)im->data; |
| 132 |
return v; |
| 133 |
|
| 134 |
error: |
| 135 |
return grab_x11_close(v); |
| 136 |
} |
| 137 |
|
| 138 |
static struct fbuf_t *grab_x11_read(void *desc) |
| 139 |
{ |
| 140 |
|
| 141 |
struct grab_x11_desc *v = desc; |
| 142 |
struct fbuf_t *b = &v->b; |
| 143 |
|
| 144 |
XGetSubImage(v->dpy, |
| 145 |
RootWindow(v->dpy, DefaultScreen(v->dpy)), |
| 146 |
b->x, b->y, b->w, b->h, AllPlanes, ZPixmap, v->image, 0, 0); |
| 147 |
|
| 148 |
b->data = (uint8_t *)v->image->data; |
| 149 |
return b; |
| 150 |
} |
| 151 |
|
| 152 |
static int boundary_checks(int x, int limit) |
| 153 |
{ |
| 154 |
return (x <= 0) ? 0 : (x > limit ? limit : x); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static void grab_x11_move(void *desc, int dx, int dy) |
| 161 |
{ |
| 162 |
struct grab_x11_desc *v = desc; |
| 163 |
|
| 164 |
v->b.x = boundary_checks(v->b.x + dx, v->screen_width - v->b.w); |
| 165 |
v->b.y = boundary_checks(v->b.y + dy, v->screen_height - v->b.h); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static void *grab_x11_close(void *desc) |
| 170 |
{ |
| 171 |
struct grab_x11_desc *v = desc; |
| 172 |
|
| 173 |
if (v->dpy) |
| 174 |
XCloseDisplay(v->dpy); |
| 175 |
v->dpy = NULL; |
| 176 |
v->image = NULL; |
| 177 |
ast_free(v); |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
static struct grab_desc grab_x11_desc = { |
| 182 |
.name = "X11", |
| 183 |
.open = grab_x11_open, |
| 184 |
.read = grab_x11_read, |
| 185 |
.move = grab_x11_move, |
| 186 |
.close = grab_x11_close, |
| 187 |
}; |
| 188 |
#endif |
| 189 |
|
| 190 |
#ifdef HAVE_VIDEODEV_H |
| 191 |
#include <linux/videodev.h> |
| 192 |
|
| 193 |
struct grab_v4l1_desc { |
| 194 |
int fd; |
| 195 |
struct fbuf_t b; |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static void *grab_v4l1_open(const char *dev, struct fbuf_t *geom, int fps) |
| 203 |
{ |
| 204 |
struct video_window vw = { 0 }; |
| 205 |
struct video_picture vp; |
| 206 |
int fd, i; |
| 207 |
struct grab_v4l1_desc *v; |
| 208 |
struct fbuf_t *b; |
| 209 |
|
| 210 |
|
| 211 |
if (strncmp(dev, "/dev/", 5)) |
| 212 |
return NULL; |
| 213 |
fd = open(dev, O_RDONLY | O_NONBLOCK); |
| 214 |
if (fd < 0) { |
| 215 |
ast_log(LOG_WARNING, "error opening camera %s\n", dev); |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
v = ast_calloc(1, sizeof(*v)); |
| 220 |
if (v == NULL) { |
| 221 |
ast_log(LOG_WARNING, "no memory for camera %s\n", dev); |
| 222 |
close(fd); |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
v->fd = fd; |
| 226 |
v->b = *geom; |
| 227 |
b = &v->b; |
| 228 |
|
| 229 |
ast_fd_set_flags(fd, O_NONBLOCK); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
vw.width = b->w; |
| 236 |
vw.height = b->h; |
| 237 |
vw.flags = fps << 16; |
| 238 |
if (ioctl(fd, VIDIOCSWIN, &vw) == -1) { |
| 239 |
ast_log(LOG_WARNING, "error setting format for %s [%s]\n", |
| 240 |
dev, strerror(errno)); |
| 241 |
goto error; |
| 242 |
} |
| 243 |
if (ioctl(fd, VIDIOCGPICT, &vp) == -1) { |
| 244 |
ast_log(LOG_WARNING, "error reading picture info\n"); |
| 245 |
goto error; |
| 246 |
} |
| 247 |
ast_log(LOG_WARNING, |
| 248 |
"contrast %d bright %d colour %d hue %d white %d palette %d\n", |
| 249 |
vp.contrast, vp.brightness, |
| 250 |
vp.colour, vp.hue, |
| 251 |
vp.whiteness, vp.palette); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
b->pix_fmt = vp.palette; |
| 257 |
vp.palette = VIDEO_PALETTE_YUV420P; |
| 258 |
if (ioctl(v->fd, VIDIOCSPICT, &vp) == -1) { |
| 259 |
ast_log(LOG_WARNING, "error setting palette, using %d\n", |
| 260 |
b->pix_fmt); |
| 261 |
} else |
| 262 |
b->pix_fmt = vp.palette; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
b->size = (b->w * b->h * 3)/2; |
| 268 |
ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n", |
| 269 |
dev, b->w, b->h, b->size); |
| 270 |
b->data = ast_calloc(1, b->size); |
| 271 |
if (!b->data) { |
| 272 |
ast_log(LOG_WARNING, "error allocating buffer %d bytes\n", |
| 273 |
b->size); |
| 274 |
goto error; |
| 275 |
} |
| 276 |
ast_log(LOG_WARNING, "success opening camera\n"); |
| 277 |
return v; |
| 278 |
|
| 279 |
error: |
| 280 |
close(v->fd); |
| 281 |
fbuf_free(b); |
| 282 |
ast_free(v); |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static struct fbuf_t *grab_v4l1_read(void *desc) |
| 291 |
{ |
| 292 |
struct grab_v4l1_desc *v = desc; |
| 293 |
struct fbuf_t *b = &v->b; |
| 294 |
for (;;) { |
| 295 |
int r, l = b->size - b->used; |
| 296 |
r = read(v->fd, b->data + b->used, l); |
| 297 |
|
| 298 |
if (r < 0) |
| 299 |
break; |
| 300 |
if (r == 0) |
| 301 |
break; |
| 302 |
b->used += r; |
| 303 |
if (r == l) { |
| 304 |
b->used = 0; |
| 305 |
return b; |
| 306 |
} |
| 307 |
} |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
static void *grab_v4l1_close(void *desc) |
| 312 |
{ |
| 313 |
struct grab_v4l1_desc *v = desc; |
| 314 |
|
| 315 |
close(v->fd); |
| 316 |
v->fd = -1; |
| 317 |
fbuf_free(&v->b); |
| 318 |
ast_free(v); |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
static struct grab_desc grab_v4l1_desc = { |
| 324 |
.name = "v4l1", |
| 325 |
.open = grab_v4l1_open, |
| 326 |
.read = grab_v4l1_read, |
| 327 |
.close = grab_v4l1_close, |
| 328 |
}; |
| 329 |
#endif |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
struct grab_desc *console_grabbers[] = { |
| 338 |
#ifdef HAVE_X11 |
| 339 |
&grab_x11_desc, |
| 340 |
#endif |
| 341 |
#ifdef HAVE_VIDEODEV_H |
| 342 |
&grab_v4l1_desc, |
| 343 |
#endif |
| 344 |
NULL |
| 345 |
}; |
| 346 |
|
| 347 |
#endif |