3 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2xoverlay.c: X-based overlay interface implementation for V4L2
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
32 #include <X11/extensions/Xv.h>
33 #include <X11/extensions/Xvlib.h>
36 #include <gst/interfaces/navigation.h>
38 #include "gstv4l2xoverlay.h"
39 #include "gstv4l2object.h"
40 #include "v4l2_calls.h"
42 #include "gst/gst-i18n-plugin.h"
43 #include <gst/glib-compat-private.h>
48 gint port, idle_id, event_id;
49 GMutex *mutex; /* to serialize calls to X11 */
52 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
53 #define GST_CAT_DEFAULT v4l2xv_debug
56 gst_v4l2_xoverlay_interface_init (GstXOverlayClass * klass)
58 GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
59 "V4L2 XOverlay interface debugging");
63 gst_v4l2_xoverlay_open (GstV4l2Object * v4l2object)
67 const gchar *name = g_getenv ("DISPLAY");
68 unsigned int ver, rel, req, ev, err, anum;
69 int i, id = 0, first_id = 0, min;
73 /* we need a display, obviously */
74 if (!name || !(dpy = XOpenDisplay (name))) {
75 GST_WARNING_OBJECT (v4l2object->element,
76 "No $DISPLAY set or failed to open - no overlay");
80 /* First let's check that XVideo extension is available */
81 if (!XQueryExtension (dpy, "XVideo", &i, &i, &i)) {
82 GST_WARNING_OBJECT (v4l2object->element,
83 "Xv extension not available - no overlay");
88 /* find port that belongs to this device */
89 if (XvQueryExtension (dpy, &ver, &rel, &req, &ev, &err) != Success) {
90 GST_WARNING_OBJECT (v4l2object->element,
91 "Xv extension not supported - no overlay");
95 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
96 GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
100 if (fstat (v4l2object->video_fd, &s) < 0) {
101 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
102 (_("Cannot identify device '%s'."), v4l2object->videodev),
107 min = s.st_rdev & 0xff;
108 for (i = 0; i < anum; i++) {
109 GST_DEBUG_OBJECT (v4l2object->element, "found adapter: %s", ai[i].name);
110 if (!strcmp (ai[i].name, "video4linux2") ||
111 !strcmp (ai[i].name, "video4linux")) {
113 first_id = ai[i].base_id;
115 GST_DEBUG_OBJECT (v4l2object->element,
116 "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
119 if (first_id != 0 && ai[i].base_id == first_id + min)
123 XvFreeAdaptorInfo (ai);
126 GST_WARNING_OBJECT (v4l2object->element,
127 "Did not find XvPortID for device - no overlay");
132 v4l2xv = g_new0 (GstV4l2Xv, 1);
135 v4l2xv->mutex = g_mutex_new ();
137 v4l2xv->event_id = 0;
138 v4l2object->xv = v4l2xv;
140 if (v4l2object->xwindow_id) {
141 gst_v4l2_xoverlay_set_window_handle (v4l2object, v4l2object->xwindow_id);
146 gst_v4l2_xoverlay_close (GstV4l2Object * v4l2object)
148 GstV4l2Xv *v4l2xv = v4l2object->xv;
153 if (v4l2object->xwindow_id) {
154 gst_v4l2_xoverlay_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_xoverlay_start (GstV4l2Object * v4l2object)
170 if (v4l2object->xwindow_id) {
171 gst_v4l2_xoverlay_open (v4l2object);
176 gst_v4l2_xoverlay_stop (GstV4l2Object * v4l2object)
178 gst_v4l2_xoverlay_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_xoverlay_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_xoverlay_set_window_handle (GstV4l2Object * v4l2object, guintptr id)
371 gboolean change = (v4l2object->xwindow_id != xwindow_id);
373 GST_LOG_OBJECT (v4l2object->element, "Setting XID to %lx",
374 (gulong) xwindow_id);
376 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
377 gst_v4l2_xoverlay_open (v4l2object);
379 v4l2xv = v4l2object->xv;
382 g_mutex_lock (v4l2xv->mutex);
385 if (v4l2object->xwindow_id && v4l2xv) {
386 GST_DEBUG_OBJECT (v4l2object->element,
387 "Deactivating old port %lx", v4l2object->xwindow_id);
389 XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 0);
390 XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 0);
391 XvStopVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id);
394 v4l2object->xwindow_id = xwindow_id;
397 if (!v4l2xv || xwindow_id == 0) {
399 g_mutex_unlock (v4l2xv->mutex);
404 GST_DEBUG_OBJECT (v4l2object->element, "Activating new port %lx",
408 XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 1);
409 XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 1);
412 update_geometry (v4l2object);
415 g_source_remove (v4l2xv->idle_id);
416 v4l2xv->idle_id = g_idle_add (idle_refresh, v4l2object);
417 g_mutex_unlock (v4l2xv->mutex);
421 * gst_v4l2_xoverlay_prepare_xwindow_id:
422 * @v4l2object: the v4l2object
423 * @required: %TRUE if display is required (ie. TRUE for v4l2sink, but
424 * FALSE for any other element with optional overlay capabilities)
426 * Helper function to create a windo if none is set from the application.
429 gst_v4l2_xoverlay_prepare_xwindow_id (GstV4l2Object * v4l2object,
432 if (!GST_V4L2_IS_OVERLAY (v4l2object))
435 gst_x_overlay_prepare_xwindow_id (GST_X_OVERLAY (v4l2object->element));
437 if (required && !v4l2object->xwindow_id) {
443 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
444 gst_v4l2_xoverlay_open (v4l2object);
446 v4l2xv = v4l2object->xv;
448 /* if xoverlay is not supported, just bail */
452 /* xoverlay is supported, but we don't have a window.. so create one */
453 GST_DEBUG_OBJECT (v4l2object->element, "creating window");
455 g_mutex_lock (v4l2xv->mutex);
457 width = XDisplayWidth (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
458 height = XDisplayHeight (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
459 GST_DEBUG_OBJECT (v4l2object->element, "dpy=%p", v4l2xv->dpy);
461 win = XCreateSimpleWindow (v4l2xv->dpy,
462 DefaultRootWindow (v4l2xv->dpy),
463 0, 0, width, height, 0, 0,
464 XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
466 GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
468 event_mask = ExposureMask | StructureNotifyMask;
469 if (GST_IS_NAVIGATION (v4l2object->element)) {
470 event_mask |= PointerMotionMask |
471 KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
473 XSelectInput (v4l2xv->dpy, win, event_mask);
474 v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
476 XMapRaised (v4l2xv->dpy, win);
478 XSync (v4l2xv->dpy, FALSE);
480 g_mutex_unlock (v4l2xv->mutex);
482 GST_DEBUG_OBJECT (v4l2object->element, "got window");
484 gst_v4l2_xoverlay_set_window_handle (v4l2object, win);