Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / sys / v4l2 / gstv4l2videooverlay.c
1 /* GStreamer
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *               2006 Edgard Lima <edgard.lima@indt.org.br>
4  *
5  * gstv4l2video_overlay.c: X-based overlay interface implementation for V4L2
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <gst/gst.h>
29 #include <X11/X.h>
30 #include <X11/Xlib.h>
31 #include <X11/extensions/Xv.h>
32 #include <X11/extensions/Xvlib.h>
33 #include <sys/stat.h>
34
35 #include <gst/interfaces/navigation.h>
36
37 #include "gstv4l2videooverlay.h"
38 #include "gstv4l2object.h"
39 #include "v4l2_calls.h"
40
41 #include "gst/gst-i18n-plugin.h"
42 #include <gst/glib-compat-private.h>
43
44 struct _GstV4l2Xv
45 {
46   Display *dpy;
47   gint port, idle_id, event_id;
48   GMutex *mutex;                /* to serialize calls to X11 */
49 };
50
51 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
52 #define GST_CAT_DEFAULT v4l2xv_debug
53
54 void
55 gst_v4l2_video_overlay_interface_init (GstVideoOverlayInterface * iface)
56 {
57   GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
58       "V4L2 GstVideoOverlay interface debugging");
59 }
60
61 static void
62 gst_v4l2_video_overlay_open (GstV4l2Object * v4l2object)
63 {
64   struct stat s;
65   GstV4l2Xv *v4l2xv;
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;
69   XvAdaptorInfo *ai;
70   Display *dpy;
71
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");
76     return;
77   }
78
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");
83     XCloseDisplay (dpy);
84     return;
85   }
86
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");
91     XCloseDisplay (dpy);
92     return;
93   }
94   if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
95     GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
96     XCloseDisplay (dpy);
97     return;
98   }
99   if (fstat (v4l2object->video_fd, &s) < 0) {
100     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
101         (_("Cannot identify device '%s'."), v4l2object->videodev),
102         GST_ERROR_SYSTEM);
103     XCloseDisplay (dpy);
104     return;
105   }
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")) {
111       if (first_id == 0)
112         first_id = ai[i].base_id;
113
114       GST_DEBUG_OBJECT (v4l2object->element,
115           "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
116
117       /* hmm... */
118       if (first_id != 0 && ai[i].base_id == first_id + min)
119         id = ai[i].base_id;
120     }
121   }
122   XvFreeAdaptorInfo (ai);
123
124   if (id == 0) {
125     GST_WARNING_OBJECT (v4l2object->element,
126         "Did not find XvPortID for device - no overlay");
127     XCloseDisplay (dpy);
128     return;
129   }
130
131   v4l2xv = g_new0 (GstV4l2Xv, 1);
132   v4l2xv->dpy = dpy;
133   v4l2xv->port = id;
134   v4l2xv->mutex = g_mutex_new ();
135   v4l2xv->idle_id = 0;
136   v4l2xv->event_id = 0;
137   v4l2object->xv = v4l2xv;
138
139   if (v4l2object->xwindow_id) {
140     gst_v4l2_video_overlay_set_window_handle (v4l2object,
141         v4l2object->xwindow_id);
142   }
143 }
144
145 static void
146 gst_v4l2_video_overlay_close (GstV4l2Object * v4l2object)
147 {
148   GstV4l2Xv *v4l2xv = v4l2object->xv;
149
150   if (!v4l2object->xv)
151     return;
152
153   if (v4l2object->xwindow_id) {
154     gst_v4l2_video_overlay_set_window_handle (v4l2object, 0);
155   }
156
157   XCloseDisplay (v4l2xv->dpy);
158   g_mutex_free (v4l2xv->mutex);
159   if (v4l2xv->idle_id)
160     g_source_remove (v4l2xv->idle_id);
161   if (v4l2xv->event_id)
162     g_source_remove (v4l2xv->event_id);
163   g_free (v4l2xv);
164   v4l2object->xv = NULL;
165 }
166
167 void
168 gst_v4l2_video_overlay_start (GstV4l2Object * v4l2object)
169 {
170   if (v4l2object->xwindow_id) {
171     gst_v4l2_video_overlay_open (v4l2object);
172   }
173 }
174
175 void
176 gst_v4l2_video_overlay_stop (GstV4l2Object * v4l2object)
177 {
178   gst_v4l2_video_overlay_close (v4l2object);
179 }
180
181 /* should be called with mutex held */
182 static gboolean
183 get_render_rect (GstV4l2Object * v4l2object, GstVideoRectangle * rect)
184 {
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 */
190     rect->x = 0;
191     rect->y = 0;
192     rect->w = attr.width;
193     rect->h = attr.height;
194     return TRUE;
195   } else {
196     return FALSE;
197   }
198 }
199
200 gboolean
201 gst_v4l2_video_overlay_get_render_rect (GstV4l2Object * v4l2object,
202     GstVideoRectangle * rect)
203 {
204   GstV4l2Xv *v4l2xv = v4l2object->xv;
205   gboolean ret = FALSE;
206   if (v4l2xv) {
207     g_mutex_lock (v4l2xv->mutex);
208     ret = get_render_rect (v4l2object, rect);
209     g_mutex_unlock (v4l2xv->mutex);
210   }
211   return ret;
212 }
213
214 static void
215 update_geometry (GstV4l2Object * v4l2object)
216 {
217   GstV4l2Xv *v4l2xv = v4l2object->xv;
218   GstVideoRectangle rect;
219   if (!get_render_rect (v4l2object, &rect))
220     return;
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
223    * crop..
224    */
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);
228 }
229
230 static gboolean
231 idle_refresh (gpointer data)
232 {
233   GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
234   GstV4l2Xv *v4l2xv = v4l2object->xv;
235
236   GST_LOG_OBJECT (v4l2object->element, "idle refresh");
237
238   if (v4l2xv) {
239     g_mutex_lock (v4l2xv->mutex);
240
241     update_geometry (v4l2object);
242
243     v4l2xv->idle_id = 0;
244     g_mutex_unlock (v4l2xv->mutex);
245   }
246
247   /* once */
248   return FALSE;
249 }
250
251
252 static gboolean
253 event_refresh (gpointer data)
254 {
255   GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
256   GstV4l2Xv *v4l2xv = v4l2object->xv;
257
258   GST_LOG_OBJECT (v4l2object->element, "event refresh");
259
260   if (v4l2xv) {
261     XEvent e;
262
263     g_mutex_lock (v4l2xv->mutex);
264
265     /* If the element supports navigation, collect the relavent input
266      * events and push them upstream as navigation events
267      */
268     if (GST_IS_NAVIGATION (v4l2object->element)) {
269       guint pointer_x = 0, pointer_y = 0;
270       gboolean pointer_moved = FALSE;
271
272       /* We get all pointer motion events, only the last position is
273        * interesting.
274        */
275       while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
276               PointerMotionMask, &e)) {
277         switch (e.type) {
278           case MotionNotify:
279             pointer_x = e.xmotion.x;
280             pointer_y = e.xmotion.y;
281             pointer_moved = TRUE;
282             break;
283           default:
284             break;
285         }
286       }
287       if (pointer_moved) {
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);
294       }
295
296       /* We get all events on our window to throw them upstream
297        */
298       while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
299               KeyPressMask | KeyReleaseMask |
300               ButtonPressMask | ButtonReleaseMask, &e)) {
301         KeySym keysym;
302         const char *key_str = NULL;
303
304         g_mutex_unlock (v4l2xv->mutex);
305
306         switch (e.type) {
307           case ButtonPress:
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);
314             break;
315           case ButtonRelease:
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);
322             break;
323           case KeyPress:
324           case KeyRelease:
325             g_mutex_lock (v4l2xv->mutex);
326             keysym = XKeycodeToKeysym (v4l2xv->dpy, e.xkey.keycode, 0);
327             if (keysym != NoSymbol) {
328               key_str = XKeysymToString (keysym);
329             } else {
330               key_str = "unknown";
331             }
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);
338             break;
339           default:
340             GST_DEBUG_OBJECT (v4l2object->element,
341                 "unhandled X event (%d)", e.type);
342         }
343
344         g_mutex_lock (v4l2xv->mutex);
345       }
346     }
347
348     /* Handle ConfigureNotify */
349     while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
350             StructureNotifyMask, &e)) {
351       switch (e.type) {
352         case ConfigureNotify:
353           update_geometry (v4l2object);
354           break;
355         default:
356           break;
357       }
358     }
359     g_mutex_unlock (v4l2xv->mutex);
360   }
361
362   /* repeat */
363   return TRUE;
364 }
365
366 void
367 gst_v4l2_video_overlay_set_window_handle (GstV4l2Object * v4l2object,
368     guintptr id)
369 {
370   GstV4l2Xv *v4l2xv;
371   XID xwindow_id = id;
372   gboolean change = (v4l2object->xwindow_id != xwindow_id);
373
374   GST_LOG_OBJECT (v4l2object->element, "Setting XID to %lx",
375       (gulong) xwindow_id);
376
377   if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
378     gst_v4l2_video_overlay_open (v4l2object);
379
380   v4l2xv = v4l2object->xv;
381
382   if (v4l2xv)
383     g_mutex_lock (v4l2xv->mutex);
384
385   if (change) {
386     if (v4l2object->xwindow_id && v4l2xv) {
387       GST_DEBUG_OBJECT (v4l2object->element,
388           "Deactivating old port %lx", v4l2object->xwindow_id);
389
390       XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 0);
391       XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 0);
392       XvStopVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id);
393     }
394
395     v4l2object->xwindow_id = xwindow_id;
396   }
397
398   if (!v4l2xv || xwindow_id == 0) {
399     if (v4l2xv)
400       g_mutex_unlock (v4l2xv->mutex);
401     return;
402   }
403
404   if (change) {
405     GST_DEBUG_OBJECT (v4l2object->element, "Activating new port %lx",
406         xwindow_id);
407
408     /* draw */
409     XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 1);
410     XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 1);
411   }
412
413   update_geometry (v4l2object);
414
415   if (v4l2xv->idle_id)
416     g_source_remove (v4l2xv->idle_id);
417   v4l2xv->idle_id = g_idle_add (idle_refresh, v4l2object);
418   g_mutex_unlock (v4l2xv->mutex);
419 }
420
421 /**
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)
426  *
427  * Helper function to create a windo if none is set from the application.
428  */
429 void
430 gst_v4l2_video_overlay_prepare_window_handle (GstV4l2Object * v4l2object,
431     gboolean required)
432 {
433   GstVideoOverlay *overlay;
434
435   if (!GST_V4L2_IS_OVERLAY (v4l2object))
436     return;
437
438   overlay = GST_VIDEO_OVERLAY (v4l2object->element);
439   gst_video_overlay_prepare_window_handle (overlay);
440
441   if (required && !v4l2object->xwindow_id) {
442     GstV4l2Xv *v4l2xv;
443     Window win;
444     int width, height;
445     long event_mask;
446
447     if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
448       gst_v4l2_video_overlay_open (v4l2object);
449
450     v4l2xv = v4l2object->xv;
451
452     /* if video_overlay is not supported, just bail */
453     if (!v4l2xv)
454       return;
455
456     /* video_overlay is supported, but we don't have a window.. so create one */
457     GST_DEBUG_OBJECT (v4l2object->element, "creating window");
458
459     g_mutex_lock (v4l2xv->mutex);
460
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);
464
465     win = XCreateSimpleWindow (v4l2xv->dpy,
466         DefaultRootWindow (v4l2xv->dpy),
467         0, 0, width, height, 0, 0,
468         XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
469
470     GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
471
472     event_mask = ExposureMask | StructureNotifyMask;
473     if (GST_IS_NAVIGATION (v4l2object->element)) {
474       event_mask |= PointerMotionMask |
475           KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
476     }
477     XSelectInput (v4l2xv->dpy, win, event_mask);
478     v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
479
480     XMapRaised (v4l2xv->dpy, win);
481
482     XSync (v4l2xv->dpy, FALSE);
483
484     g_mutex_unlock (v4l2xv->mutex);
485
486     GST_DEBUG_OBJECT (v4l2object->element, "got window");
487
488     gst_v4l2_video_overlay_set_window_handle (v4l2object, win);
489   }
490 }