2 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * 2006 Edgard Lima <edgard.lima@indt.org.br>
5 * gstv4l2video_overlay.c: X-based overlay interface implementation for V4L2
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
31 #include <X11/extensions/Xv.h>
32 #include <X11/extensions/Xvlib.h>
34 /* for XkbKeycodeToKeysym */
35 #include <X11/XKBlib.h>
37 #include <gst/video/navigation.h>
39 #include "gstv4l2videooverlay.h"
40 #include "gstv4l2object.h"
41 #include "v4l2_calls.h"
43 #include "gst/gst-i18n-plugin.h"
44 #include <gst/glib-compat-private.h>
49 gint port, idle_id, event_id;
50 GMutex mutex; /* to serialize calls to X11 */
53 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
54 #define GST_CAT_DEFAULT v4l2xv_debug
57 gst_v4l2_video_overlay_interface_init (GstVideoOverlayInterface * iface)
59 GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
60 "V4L2 GstVideoOverlay interface debugging");
64 gst_v4l2_video_overlay_open (GstV4l2Object * v4l2object)
68 const gchar *name = g_getenv ("DISPLAY");
69 unsigned int ver, rel, req, ev, err, anum;
70 int i, id = 0, first_id = 0, min;
74 /* we need a display, obviously */
75 if (!name || !(dpy = XOpenDisplay (name))) {
76 GST_WARNING_OBJECT (v4l2object->element,
77 "No $DISPLAY set or failed to open - no overlay");
81 /* First let's check that XVideo extension is available */
82 if (!XQueryExtension (dpy, "XVideo", &i, &i, &i)) {
83 GST_WARNING_OBJECT (v4l2object->element,
84 "Xv extension not available - no overlay");
89 /* find port that belongs to this device */
90 if (XvQueryExtension (dpy, &ver, &rel, &req, &ev, &err) != Success) {
91 GST_WARNING_OBJECT (v4l2object->element,
92 "Xv extension not supported - no overlay");
96 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
97 GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
101 if (fstat (v4l2object->video_fd, &s) < 0) {
102 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
103 (_("Cannot identify device '%s'."), v4l2object->videodev),
108 min = s.st_rdev & 0xff;
109 for (i = 0; i < anum; i++) {
110 GST_DEBUG_OBJECT (v4l2object->element, "found adapter: %s", ai[i].name);
111 if (!strcmp (ai[i].name, "video4linux2") ||
112 !strcmp (ai[i].name, "video4linux")) {
114 first_id = ai[i].base_id;
116 GST_DEBUG_OBJECT (v4l2object->element,
117 "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
120 if (first_id != 0 && ai[i].base_id == first_id + min)
124 XvFreeAdaptorInfo (ai);
127 GST_WARNING_OBJECT (v4l2object->element,
128 "Did not find XvPortID for device - no overlay");
133 v4l2xv = g_new0 (GstV4l2Xv, 1);
136 g_mutex_init (&v4l2xv->mutex);
138 v4l2xv->event_id = 0;
139 v4l2object->xv = v4l2xv;
141 if (v4l2object->xwindow_id) {
142 gst_v4l2_video_overlay_set_window_handle (v4l2object,
143 v4l2object->xwindow_id);
148 gst_v4l2_video_overlay_close (GstV4l2Object * v4l2object)
150 GstV4l2Xv *v4l2xv = v4l2object->xv;
155 if (v4l2object->xwindow_id) {
156 gst_v4l2_video_overlay_set_window_handle (v4l2object, 0);
159 XCloseDisplay (v4l2xv->dpy);
160 g_mutex_clear (&v4l2xv->mutex);
162 g_source_remove (v4l2xv->idle_id);
163 if (v4l2xv->event_id)
164 g_source_remove (v4l2xv->event_id);
166 v4l2object->xv = NULL;
170 gst_v4l2_video_overlay_start (GstV4l2Object * v4l2object)
172 if (v4l2object->xwindow_id) {
173 gst_v4l2_video_overlay_open (v4l2object);
178 gst_v4l2_video_overlay_stop (GstV4l2Object * v4l2object)
180 gst_v4l2_video_overlay_close (v4l2object);
183 /* should be called with mutex held */
185 get_render_rect (GstV4l2Object * v4l2object, GstVideoRectangle * rect)
187 GstV4l2Xv *v4l2xv = v4l2object->xv;
188 if (v4l2xv && v4l2xv->dpy && v4l2object->xwindow_id) {
189 XWindowAttributes attr;
190 XGetWindowAttributes (v4l2xv->dpy, v4l2object->xwindow_id, &attr);
191 /* this is where we'd add support to maintain aspect ratio */
194 rect->w = attr.width;
195 rect->h = attr.height;
203 gst_v4l2_video_overlay_get_render_rect (GstV4l2Object * v4l2object,
204 GstVideoRectangle * rect)
206 GstV4l2Xv *v4l2xv = v4l2object->xv;
207 gboolean ret = FALSE;
209 g_mutex_lock (&v4l2xv->mutex);
210 ret = get_render_rect (v4l2object, rect);
211 g_mutex_unlock (&v4l2xv->mutex);
217 update_geometry (GstV4l2Object * v4l2object)
219 GstV4l2Xv *v4l2xv = v4l2object->xv;
220 GstVideoRectangle rect;
221 if (!get_render_rect (v4l2object, &rect))
223 /* note: we don't pass in valid video x/y/w/h.. currently the xserver
224 * doesn't need to know these, as they come from v4l2 by setting the
227 XvPutVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id,
228 DefaultGC (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)),
229 0, 0, rect.w, rect.h, rect.x, rect.y, rect.w, rect.h);
233 idle_refresh (gpointer data)
235 GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
236 GstV4l2Xv *v4l2xv = v4l2object->xv;
238 GST_LOG_OBJECT (v4l2object->element, "idle refresh");
241 g_mutex_lock (&v4l2xv->mutex);
243 update_geometry (v4l2object);
246 g_mutex_unlock (&v4l2xv->mutex);
255 event_refresh (gpointer data)
257 GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
258 GstV4l2Xv *v4l2xv = v4l2object->xv;
260 GST_LOG_OBJECT (v4l2object->element, "event refresh");
265 g_mutex_lock (&v4l2xv->mutex);
267 /* If the element supports navigation, collect the relavent input
268 * events and push them upstream as navigation events
270 if (GST_IS_NAVIGATION (v4l2object->element)) {
271 guint pointer_x = 0, pointer_y = 0;
272 gboolean pointer_moved = FALSE;
274 /* We get all pointer motion events, only the last position is
277 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
278 PointerMotionMask, &e)) {
281 pointer_x = e.xmotion.x;
282 pointer_y = e.xmotion.y;
283 pointer_moved = TRUE;
290 GST_DEBUG_OBJECT (v4l2object->element,
291 "pointer moved over window at %d,%d", pointer_x, pointer_y);
292 g_mutex_unlock (&v4l2xv->mutex);
293 gst_navigation_send_mouse_event (GST_NAVIGATION (v4l2object->element),
294 "mouse-move", 0, e.xbutton.x, e.xbutton.y);
295 g_mutex_lock (&v4l2xv->mutex);
298 /* We get all events on our window to throw them upstream
300 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
301 KeyPressMask | KeyReleaseMask |
302 ButtonPressMask | ButtonReleaseMask, &e)) {
304 const char *key_str = NULL;
306 g_mutex_unlock (&v4l2xv->mutex);
310 GST_DEBUG_OBJECT (v4l2object->element,
311 "button %d pressed over window at %d,%d",
312 e.xbutton.button, e.xbutton.x, e.xbutton.y);
313 gst_navigation_send_mouse_event (GST_NAVIGATION
314 (v4l2object->element), "mouse-button-press", e.xbutton.button,
315 e.xbutton.x, e.xbutton.y);
318 GST_DEBUG_OBJECT (v4l2object->element,
319 "button %d released over window at %d,%d",
320 e.xbutton.button, e.xbutton.x, e.xbutton.y);
321 gst_navigation_send_mouse_event (GST_NAVIGATION
322 (v4l2object->element), "mouse-button-release", e.xbutton.button,
323 e.xbutton.x, e.xbutton.y);
327 g_mutex_lock (&v4l2xv->mutex);
328 keysym = XkbKeycodeToKeysym (v4l2xv->dpy, e.xkey.keycode, 0, 0);
329 if (keysym != NoSymbol) {
330 key_str = XKeysymToString (keysym);
334 g_mutex_unlock (&v4l2xv->mutex);
335 GST_DEBUG_OBJECT (v4l2object->element,
336 "key %d pressed over window at %d,%d (%s)",
337 e.xkey.keycode, e.xkey.x, e.xkey.y, key_str);
338 gst_navigation_send_key_event (GST_NAVIGATION (v4l2object->element),
339 e.type == KeyPress ? "key-press" : "key-release", key_str);
342 GST_DEBUG_OBJECT (v4l2object->element,
343 "unhandled X event (%d)", e.type);
346 g_mutex_lock (&v4l2xv->mutex);
350 /* Handle ConfigureNotify */
351 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
352 StructureNotifyMask, &e)) {
354 case ConfigureNotify:
355 update_geometry (v4l2object);
361 g_mutex_unlock (&v4l2xv->mutex);
369 gst_v4l2_video_overlay_set_window_handle (GstV4l2Object * v4l2object,
374 gboolean change = (v4l2object->xwindow_id != xwindow_id);
376 GST_LOG_OBJECT (v4l2object->element, "Setting XID to %lx",
377 (gulong) xwindow_id);
379 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
380 gst_v4l2_video_overlay_open (v4l2object);
382 v4l2xv = v4l2object->xv;
385 g_mutex_lock (&v4l2xv->mutex);
388 if (v4l2object->xwindow_id && v4l2xv) {
389 GST_DEBUG_OBJECT (v4l2object->element,
390 "Deactivating old port %lx", v4l2object->xwindow_id);
392 XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 0);
393 XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 0);
394 XvStopVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id);
397 v4l2object->xwindow_id = xwindow_id;
400 if (!v4l2xv || xwindow_id == 0) {
402 g_mutex_unlock (&v4l2xv->mutex);
407 GST_DEBUG_OBJECT (v4l2object->element, "Activating new port %lx",
411 XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 1);
412 XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 1);
415 update_geometry (v4l2object);
418 g_source_remove (v4l2xv->idle_id);
419 v4l2xv->idle_id = g_idle_add (idle_refresh, v4l2object);
420 g_mutex_unlock (&v4l2xv->mutex);
424 * gst_v4l2_video_overlay_prepare_window_handle:
425 * @v4l2object: the v4l2object
426 * @required: %TRUE if display is required (ie. TRUE for v4l2sink, but
427 * FALSE for any other element with optional overlay capabilities)
429 * Helper function to create a windo if none is set from the application.
432 gst_v4l2_video_overlay_prepare_window_handle (GstV4l2Object * v4l2object,
435 GstVideoOverlay *overlay;
437 if (!GST_V4L2_IS_OVERLAY (v4l2object))
440 overlay = GST_VIDEO_OVERLAY (v4l2object->element);
441 gst_video_overlay_prepare_window_handle (overlay);
443 if (required && !v4l2object->xwindow_id) {
449 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
450 gst_v4l2_video_overlay_open (v4l2object);
452 v4l2xv = v4l2object->xv;
454 /* if video_overlay is not supported, just bail */
458 /* video_overlay is supported, but we don't have a window.. so create one */
459 GST_DEBUG_OBJECT (v4l2object->element, "creating window");
461 g_mutex_lock (&v4l2xv->mutex);
463 width = XDisplayWidth (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
464 height = XDisplayHeight (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
465 GST_DEBUG_OBJECT (v4l2object->element, "dpy=%p", v4l2xv->dpy);
467 win = XCreateSimpleWindow (v4l2xv->dpy,
468 DefaultRootWindow (v4l2xv->dpy),
469 0, 0, width, height, 0, 0,
470 XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
472 GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
474 event_mask = ExposureMask | StructureNotifyMask;
475 if (GST_IS_NAVIGATION (v4l2object->element)) {
476 event_mask |= PointerMotionMask |
477 KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
479 XSelectInput (v4l2xv->dpy, win, event_mask);
480 v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
482 XMapRaised (v4l2xv->dpy, win);
484 XSync (v4l2xv->dpy, FALSE);
486 g_mutex_unlock (&v4l2xv->mutex);
488 GST_DEBUG_OBJECT (v4l2object->element, "got window");
490 gst_v4l2_video_overlay_set_window_handle (v4l2object, win);