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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
31 #include <X11/extensions/Xv.h>
32 #include <X11/extensions/Xvlib.h>
35 #include <gst/interfaces/navigation.h>
37 #include "gstv4l2videooverlay.h"
38 #include "gstv4l2object.h"
39 #include "v4l2_calls.h"
41 #include "gst/gst-i18n-plugin.h"
42 #include <gst/glib-compat-private.h>
47 gint port, idle_id, event_id;
48 GMutex *mutex; /* to serialize calls to X11 */
51 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
52 #define GST_CAT_DEFAULT v4l2xv_debug
55 gst_v4l2_video_overlay_interface_init (GstVideoOverlayInterface * iface)
57 GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
58 "V4L2 GstVideoOverlay interface debugging");
62 gst_v4l2_video_overlay_open (GstV4l2Object * v4l2object)
66 const gchar *name = g_getenv ("DISPLAY");
67 unsigned int ver, rel, req, ev, err, anum;
68 int i, id = 0, first_id = 0, min;
72 /* we need a display, obviously */
73 if (!name || !(dpy = XOpenDisplay (name))) {
74 GST_WARNING_OBJECT (v4l2object->element,
75 "No $DISPLAY set or failed to open - no overlay");
79 /* First let's check that XVideo extension is available */
80 if (!XQueryExtension (dpy, "XVideo", &i, &i, &i)) {
81 GST_WARNING_OBJECT (v4l2object->element,
82 "Xv extension not available - no overlay");
87 /* find port that belongs to this device */
88 if (XvQueryExtension (dpy, &ver, &rel, &req, &ev, &err) != Success) {
89 GST_WARNING_OBJECT (v4l2object->element,
90 "Xv extension not supported - no overlay");
94 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
95 GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
99 if (fstat (v4l2object->video_fd, &s) < 0) {
100 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
101 (_("Cannot identify device '%s'."), v4l2object->videodev),
106 min = s.st_rdev & 0xff;
107 for (i = 0; i < anum; i++) {
108 GST_DEBUG_OBJECT (v4l2object->element, "found adapter: %s", ai[i].name);
109 if (!strcmp (ai[i].name, "video4linux2") ||
110 !strcmp (ai[i].name, "video4linux")) {
112 first_id = ai[i].base_id;
114 GST_DEBUG_OBJECT (v4l2object->element,
115 "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
118 if (first_id != 0 && ai[i].base_id == first_id + min)
122 XvFreeAdaptorInfo (ai);
125 GST_WARNING_OBJECT (v4l2object->element,
126 "Did not find XvPortID for device - no overlay");
131 v4l2xv = g_new0 (GstV4l2Xv, 1);
134 v4l2xv->mutex = g_mutex_new ();
136 v4l2xv->event_id = 0;
137 v4l2object->xv = v4l2xv;
139 if (v4l2object->xwindow_id) {
140 gst_v4l2_video_overlay_set_window_handle (v4l2object,
141 v4l2object->xwindow_id);
146 gst_v4l2_video_overlay_close (GstV4l2Object * v4l2object)
148 GstV4l2Xv *v4l2xv = v4l2object->xv;
153 if (v4l2object->xwindow_id) {
154 gst_v4l2_video_overlay_set_window_handle (v4l2object, 0);
157 XCloseDisplay (v4l2xv->dpy);
158 g_mutex_free (v4l2xv->mutex);
160 g_source_remove (v4l2xv->idle_id);
161 if (v4l2xv->event_id)
162 g_source_remove (v4l2xv->event_id);
164 v4l2object->xv = NULL;
168 gst_v4l2_video_overlay_start (GstV4l2Object * v4l2object)
170 if (v4l2object->xwindow_id) {
171 gst_v4l2_video_overlay_open (v4l2object);
176 gst_v4l2_video_overlay_stop (GstV4l2Object * v4l2object)
178 gst_v4l2_video_overlay_close (v4l2object);
181 /* should be called with mutex held */
183 get_render_rect (GstV4l2Object * v4l2object, GstVideoRectangle * rect)
185 GstV4l2Xv *v4l2xv = v4l2object->xv;
186 if (v4l2xv && v4l2xv->dpy && v4l2object->xwindow_id) {
187 XWindowAttributes attr;
188 XGetWindowAttributes (v4l2xv->dpy, v4l2object->xwindow_id, &attr);
189 /* this is where we'd add support to maintain aspect ratio */
192 rect->w = attr.width;
193 rect->h = attr.height;
201 gst_v4l2_video_overlay_get_render_rect (GstV4l2Object * v4l2object,
202 GstVideoRectangle * rect)
204 GstV4l2Xv *v4l2xv = v4l2object->xv;
205 gboolean ret = FALSE;
207 g_mutex_lock (v4l2xv->mutex);
208 ret = get_render_rect (v4l2object, rect);
209 g_mutex_unlock (v4l2xv->mutex);
215 update_geometry (GstV4l2Object * v4l2object)
217 GstV4l2Xv *v4l2xv = v4l2object->xv;
218 GstVideoRectangle rect;
219 if (!get_render_rect (v4l2object, &rect))
221 /* note: we don't pass in valid video x/y/w/h.. currently the xserver
222 * doesn't need to know these, as they come from v4l2 by setting the
225 XvPutVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id,
226 DefaultGC (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)),
227 0, 0, rect.w, rect.h, rect.x, rect.y, rect.w, rect.h);
231 idle_refresh (gpointer data)
233 GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
234 GstV4l2Xv *v4l2xv = v4l2object->xv;
236 GST_LOG_OBJECT (v4l2object->element, "idle refresh");
239 g_mutex_lock (v4l2xv->mutex);
241 update_geometry (v4l2object);
244 g_mutex_unlock (v4l2xv->mutex);
253 event_refresh (gpointer data)
255 GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
256 GstV4l2Xv *v4l2xv = v4l2object->xv;
258 GST_LOG_OBJECT (v4l2object->element, "event refresh");
263 g_mutex_lock (v4l2xv->mutex);
265 /* If the element supports navigation, collect the relavent input
266 * events and push them upstream as navigation events
268 if (GST_IS_NAVIGATION (v4l2object->element)) {
269 guint pointer_x = 0, pointer_y = 0;
270 gboolean pointer_moved = FALSE;
272 /* We get all pointer motion events, only the last position is
275 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
276 PointerMotionMask, &e)) {
279 pointer_x = e.xmotion.x;
280 pointer_y = e.xmotion.y;
281 pointer_moved = TRUE;
288 GST_DEBUG_OBJECT (v4l2object->element,
289 "pointer moved over window at %d,%d", pointer_x, pointer_y);
290 g_mutex_unlock (v4l2xv->mutex);
291 gst_navigation_send_mouse_event (GST_NAVIGATION (v4l2object->element),
292 "mouse-move", 0, e.xbutton.x, e.xbutton.y);
293 g_mutex_lock (v4l2xv->mutex);
296 /* We get all events on our window to throw them upstream
298 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
299 KeyPressMask | KeyReleaseMask |
300 ButtonPressMask | ButtonReleaseMask, &e)) {
302 const char *key_str = NULL;
304 g_mutex_unlock (v4l2xv->mutex);
308 GST_DEBUG_OBJECT (v4l2object->element,
309 "button %d pressed over window at %d,%d",
310 e.xbutton.button, e.xbutton.x, e.xbutton.y);
311 gst_navigation_send_mouse_event (GST_NAVIGATION
312 (v4l2object->element), "mouse-button-press", e.xbutton.button,
313 e.xbutton.x, e.xbutton.y);
316 GST_DEBUG_OBJECT (v4l2object->element,
317 "button %d released over window at %d,%d",
318 e.xbutton.button, e.xbutton.x, e.xbutton.y);
319 gst_navigation_send_mouse_event (GST_NAVIGATION
320 (v4l2object->element), "mouse-button-release", e.xbutton.button,
321 e.xbutton.x, e.xbutton.y);
325 g_mutex_lock (v4l2xv->mutex);
326 keysym = XKeycodeToKeysym (v4l2xv->dpy, e.xkey.keycode, 0);
327 if (keysym != NoSymbol) {
328 key_str = XKeysymToString (keysym);
332 g_mutex_unlock (v4l2xv->mutex);
333 GST_DEBUG_OBJECT (v4l2object->element,
334 "key %d pressed over window at %d,%d (%s)",
335 e.xkey.keycode, e.xkey.x, e.xkey.y, key_str);
336 gst_navigation_send_key_event (GST_NAVIGATION (v4l2object->element),
337 e.type == KeyPress ? "key-press" : "key-release", key_str);
340 GST_DEBUG_OBJECT (v4l2object->element,
341 "unhandled X event (%d)", e.type);
344 g_mutex_lock (v4l2xv->mutex);
348 /* Handle ConfigureNotify */
349 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
350 StructureNotifyMask, &e)) {
352 case ConfigureNotify:
353 update_geometry (v4l2object);
359 g_mutex_unlock (v4l2xv->mutex);
367 gst_v4l2_video_overlay_set_window_handle (GstV4l2Object * v4l2object,
372 gboolean change = (v4l2object->xwindow_id != xwindow_id);
374 GST_LOG_OBJECT (v4l2object->element, "Setting XID to %lx",
375 (gulong) xwindow_id);
377 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
378 gst_v4l2_video_overlay_open (v4l2object);
380 v4l2xv = v4l2object->xv;
383 g_mutex_lock (v4l2xv->mutex);
386 if (v4l2object->xwindow_id && v4l2xv) {
387 GST_DEBUG_OBJECT (v4l2object->element,
388 "Deactivating old port %lx", v4l2object->xwindow_id);
390 XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 0);
391 XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 0);
392 XvStopVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id);
395 v4l2object->xwindow_id = xwindow_id;
398 if (!v4l2xv || xwindow_id == 0) {
400 g_mutex_unlock (v4l2xv->mutex);
405 GST_DEBUG_OBJECT (v4l2object->element, "Activating new port %lx",
409 XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 1);
410 XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 1);
413 update_geometry (v4l2object);
416 g_source_remove (v4l2xv->idle_id);
417 v4l2xv->idle_id = g_idle_add (idle_refresh, v4l2object);
418 g_mutex_unlock (v4l2xv->mutex);
422 * gst_v4l2_video_overlay_prepare_window_handle:
423 * @v4l2object: the v4l2object
424 * @required: %TRUE if display is required (ie. TRUE for v4l2sink, but
425 * FALSE for any other element with optional overlay capabilities)
427 * Helper function to create a windo if none is set from the application.
430 gst_v4l2_video_overlay_prepare_window_handle (GstV4l2Object * v4l2object,
433 GstVideoOverlay *overlay;
435 if (!GST_V4L2_IS_OVERLAY (v4l2object))
438 overlay = GST_VIDEO_OVERLAY (v4l2object->element);
439 gst_video_overlay_prepare_window_handle (overlay);
441 if (required && !v4l2object->xwindow_id) {
447 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
448 gst_v4l2_video_overlay_open (v4l2object);
450 v4l2xv = v4l2object->xv;
452 /* if video_overlay is not supported, just bail */
456 /* video_overlay is supported, but we don't have a window.. so create one */
457 GST_DEBUG_OBJECT (v4l2object->element, "creating window");
459 g_mutex_lock (v4l2xv->mutex);
461 width = XDisplayWidth (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
462 height = XDisplayHeight (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
463 GST_DEBUG_OBJECT (v4l2object->element, "dpy=%p", v4l2xv->dpy);
465 win = XCreateSimpleWindow (v4l2xv->dpy,
466 DefaultRootWindow (v4l2xv->dpy),
467 0, 0, width, height, 0, 0,
468 XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
470 GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
472 event_mask = ExposureMask | StructureNotifyMask;
473 if (GST_IS_NAVIGATION (v4l2object->element)) {
474 event_mask |= PointerMotionMask |
475 KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
477 XSelectInput (v4l2xv->dpy, win, event_mask);
478 v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
480 XMapRaised (v4l2xv->dpy, win);
482 XSync (v4l2xv->dpy, FALSE);
484 g_mutex_unlock (v4l2xv->mutex);
486 GST_DEBUG_OBJECT (v4l2object->element, "got window");
488 gst_v4l2_video_overlay_set_window_handle (v4l2object, win);