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"
46 gint port, idle_id, event_id;
47 GMutex *mutex; /* to serialize calls to X11 */
50 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
51 #define GST_CAT_DEFAULT v4l2xv_debug
54 gst_v4l2_video_overlay_interface_init (GstVideoOverlayIface * klass)
56 GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
57 "V4L2 GstVideoOverlay interface debugging");
61 gst_v4l2_video_overlay_open (GstV4l2Object * v4l2object)
65 const gchar *name = g_getenv ("DISPLAY");
66 unsigned int ver, rel, req, ev, err, anum;
67 int i, id = 0, first_id = 0, min;
71 /* we need a display, obviously */
72 if (!name || !(dpy = XOpenDisplay (name))) {
73 GST_WARNING_OBJECT (v4l2object->element,
74 "No $DISPLAY set or failed to open - no overlay");
78 /* First let's check that XVideo extension is available */
79 if (!XQueryExtension (dpy, "XVideo", &i, &i, &i)) {
80 GST_WARNING_OBJECT (v4l2object->element,
81 "Xv extension not available - no overlay");
86 /* find port that belongs to this device */
87 if (XvQueryExtension (dpy, &ver, &rel, &req, &ev, &err) != Success) {
88 GST_WARNING_OBJECT (v4l2object->element,
89 "Xv extension not supported - no overlay");
93 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
94 GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
98 if (fstat (v4l2object->video_fd, &s) < 0) {
99 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
100 (_("Cannot identify device '%s'."), v4l2object->videodev),
105 min = s.st_rdev & 0xff;
106 for (i = 0; i < anum; i++) {
107 GST_DEBUG_OBJECT (v4l2object->element, "found adapter: %s", ai[i].name);
108 if (!strcmp (ai[i].name, "video4linux2") ||
109 !strcmp (ai[i].name, "video4linux")) {
111 first_id = ai[i].base_id;
113 GST_DEBUG_OBJECT (v4l2object->element,
114 "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
117 if (first_id != 0 && ai[i].base_id == first_id + min)
121 XvFreeAdaptorInfo (ai);
124 GST_WARNING_OBJECT (v4l2object->element,
125 "Did not find XvPortID for device - no overlay");
130 v4l2xv = g_new0 (GstV4l2Xv, 1);
133 v4l2xv->mutex = g_mutex_new ();
135 v4l2xv->event_id = 0;
136 v4l2object->xv = v4l2xv;
138 if (v4l2object->xwindow_id) {
139 gst_v4l2_video_overlay_set_window_handle (v4l2object,
140 v4l2object->xwindow_id);
145 gst_v4l2_video_overlay_close (GstV4l2Object * v4l2object)
147 GstV4l2Xv *v4l2xv = v4l2object->xv;
152 if (v4l2object->xwindow_id) {
153 gst_v4l2_video_overlay_set_window_handle (v4l2object, 0);
156 XCloseDisplay (v4l2xv->dpy);
157 g_mutex_free (v4l2xv->mutex);
159 g_source_remove (v4l2xv->idle_id);
160 if (v4l2xv->event_id)
161 g_source_remove (v4l2xv->event_id);
163 v4l2object->xv = NULL;
167 gst_v4l2_video_overlay_start (GstV4l2Object * v4l2object)
169 if (v4l2object->xwindow_id) {
170 gst_v4l2_video_overlay_open (v4l2object);
175 gst_v4l2_video_overlay_stop (GstV4l2Object * v4l2object)
177 gst_v4l2_video_overlay_close (v4l2object);
180 /* should be called with mutex held */
182 get_render_rect (GstV4l2Object * v4l2object, GstVideoRectangle * rect)
184 GstV4l2Xv *v4l2xv = v4l2object->xv;
185 if (v4l2xv && v4l2xv->dpy && v4l2object->xwindow_id) {
186 XWindowAttributes attr;
187 XGetWindowAttributes (v4l2xv->dpy, v4l2object->xwindow_id, &attr);
188 /* this is where we'd add support to maintain aspect ratio */
191 rect->w = attr.width;
192 rect->h = attr.height;
200 gst_v4l2_video_overlay_get_render_rect (GstV4l2Object * v4l2object,
201 GstVideoRectangle * rect)
203 GstV4l2Xv *v4l2xv = v4l2object->xv;
204 gboolean ret = FALSE;
206 g_mutex_lock (v4l2xv->mutex);
207 ret = get_render_rect (v4l2object, rect);
208 g_mutex_unlock (v4l2xv->mutex);
214 update_geometry (GstV4l2Object * v4l2object)
216 GstV4l2Xv *v4l2xv = v4l2object->xv;
217 GstVideoRectangle rect;
218 if (!get_render_rect (v4l2object, &rect))
220 /* note: we don't pass in valid video x/y/w/h.. currently the xserver
221 * doesn't need to know these, as they come from v4l2 by setting the
224 XvPutVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id,
225 DefaultGC (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)),
226 0, 0, rect.w, rect.h, rect.x, rect.y, rect.w, rect.h);
230 idle_refresh (gpointer data)
232 GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
233 GstV4l2Xv *v4l2xv = v4l2object->xv;
235 GST_LOG_OBJECT (v4l2object->element, "idle refresh");
238 g_mutex_lock (v4l2xv->mutex);
240 update_geometry (v4l2object);
243 g_mutex_unlock (v4l2xv->mutex);
252 event_refresh (gpointer data)
254 GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
255 GstV4l2Xv *v4l2xv = v4l2object->xv;
257 GST_LOG_OBJECT (v4l2object->element, "event refresh");
262 g_mutex_lock (v4l2xv->mutex);
264 /* If the element supports navigation, collect the relavent input
265 * events and push them upstream as navigation events
267 if (GST_IS_NAVIGATION (v4l2object->element)) {
268 guint pointer_x = 0, pointer_y = 0;
269 gboolean pointer_moved = FALSE;
271 /* We get all pointer motion events, only the last position is
274 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
275 PointerMotionMask, &e)) {
278 pointer_x = e.xmotion.x;
279 pointer_y = e.xmotion.y;
280 pointer_moved = TRUE;
287 GST_DEBUG_OBJECT (v4l2object->element,
288 "pointer moved over window at %d,%d", pointer_x, pointer_y);
289 g_mutex_unlock (v4l2xv->mutex);
290 gst_navigation_send_mouse_event (GST_NAVIGATION (v4l2object->element),
291 "mouse-move", 0, e.xbutton.x, e.xbutton.y);
292 g_mutex_lock (v4l2xv->mutex);
295 /* We get all events on our window to throw them upstream
297 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
298 KeyPressMask | KeyReleaseMask |
299 ButtonPressMask | ButtonReleaseMask, &e)) {
301 const char *key_str = NULL;
303 g_mutex_unlock (v4l2xv->mutex);
307 GST_DEBUG_OBJECT (v4l2object->element,
308 "button %d pressed over window at %d,%d",
309 e.xbutton.button, e.xbutton.x, e.xbutton.y);
310 gst_navigation_send_mouse_event (GST_NAVIGATION
311 (v4l2object->element), "mouse-button-press", e.xbutton.button,
312 e.xbutton.x, e.xbutton.y);
315 GST_DEBUG_OBJECT (v4l2object->element,
316 "button %d released over window at %d,%d",
317 e.xbutton.button, e.xbutton.x, e.xbutton.y);
318 gst_navigation_send_mouse_event (GST_NAVIGATION
319 (v4l2object->element), "mouse-button-release", e.xbutton.button,
320 e.xbutton.x, e.xbutton.y);
324 g_mutex_lock (v4l2xv->mutex);
325 keysym = XKeycodeToKeysym (v4l2xv->dpy, e.xkey.keycode, 0);
326 if (keysym != NoSymbol) {
327 key_str = XKeysymToString (keysym);
331 g_mutex_unlock (v4l2xv->mutex);
332 GST_DEBUG_OBJECT (v4l2object->element,
333 "key %d pressed over window at %d,%d (%s)",
334 e.xkey.keycode, e.xkey.x, e.xkey.y, key_str);
335 gst_navigation_send_key_event (GST_NAVIGATION (v4l2object->element),
336 e.type == KeyPress ? "key-press" : "key-release", key_str);
339 GST_DEBUG_OBJECT (v4l2object->element,
340 "unhandled X event (%d)", e.type);
343 g_mutex_lock (v4l2xv->mutex);
347 /* Handle ConfigureNotify */
348 while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
349 StructureNotifyMask, &e)) {
351 case ConfigureNotify:
352 update_geometry (v4l2object);
358 g_mutex_unlock (v4l2xv->mutex);
366 gst_v4l2_video_overlay_set_window_handle (GstV4l2Object * v4l2object,
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_video_overlay_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_video_overlay_prepare_window_handle:
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_video_overlay_prepare_window_handle (GstV4l2Object * v4l2object,
432 GstVideoOverlay *overlay;
434 if (!GST_V4L2_IS_OVERLAY (v4l2object))
437 overlay = GST_VIDEO_OVERLAY (v4l2object->element);
438 gst_video_overlay_prepare_window_handle (overlay);
440 if (required && !v4l2object->xwindow_id) {
446 if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
447 gst_v4l2_video_overlay_open (v4l2object);
449 v4l2xv = v4l2object->xv;
451 /* if video_overlay is not supported, just bail */
455 /* video_overlay is supported, but we don't have a window.. so create one */
456 GST_DEBUG_OBJECT (v4l2object->element, "creating window");
458 g_mutex_lock (v4l2xv->mutex);
460 width = XDisplayWidth (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
461 height = XDisplayHeight (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
462 GST_DEBUG_OBJECT (v4l2object->element, "dpy=%p", v4l2xv->dpy);
464 win = XCreateSimpleWindow (v4l2xv->dpy,
465 DefaultRootWindow (v4l2xv->dpy),
466 0, 0, width, height, 0, 0,
467 XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
469 GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
471 event_mask = ExposureMask | StructureNotifyMask;
472 if (GST_IS_NAVIGATION (v4l2object->element)) {
473 event_mask |= PointerMotionMask |
474 KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
476 XSelectInput (v4l2xv->dpy, win, event_mask);
477 v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
479 XMapRaised (v4l2xv->dpy, win);
481 XSync (v4l2xv->dpy, FALSE);
483 g_mutex_unlock (v4l2xv->mutex);
485 GST_DEBUG_OBJECT (v4l2object->element, "got window");
487 gst_v4l2_video_overlay_set_window_handle (v4l2object, win);