Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / sys / v4l2 / gstv4l2xoverlay.c
1 /* GStreamer
2  *
3  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *
6  * gstv4l2xoverlay.c: X-based overlay interface implementation for V4L2
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <string.h>
29 #include <gst/gst.h>
30 #include <X11/X.h>
31 #include <X11/Xlib.h>
32 #include <X11/extensions/Xv.h>
33 #include <X11/extensions/Xvlib.h>
34 #include <sys/stat.h>
35
36 #include <gst/interfaces/navigation.h>
37
38 #include "gstv4l2xoverlay.h"
39 #include "gstv4l2object.h"
40 #include "v4l2_calls.h"
41
42 #include "gst/gst-i18n-plugin.h"
43 #include <gst/glib-compat-private.h>
44
45 struct _GstV4l2Xv
46 {
47   Display *dpy;
48   gint port, idle_id, event_id;
49   GMutex *mutex;                /* to serialize calls to X11 */
50 };
51
52 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
53 #define GST_CAT_DEFAULT v4l2xv_debug
54
55 void
56 gst_v4l2_xoverlay_interface_init (GstXOverlayClass * klass)
57 {
58   GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
59       "V4L2 XOverlay interface debugging");
60 }
61
62 static void
63 gst_v4l2_xoverlay_open (GstV4l2Object * v4l2object)
64 {
65   struct stat s;
66   GstV4l2Xv *v4l2xv;
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;
70   XvAdaptorInfo *ai;
71   Display *dpy;
72
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");
77     return;
78   }
79
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");
84     XCloseDisplay (dpy);
85     return;
86   }
87
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");
92     XCloseDisplay (dpy);
93     return;
94   }
95   if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
96     GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
97     XCloseDisplay (dpy);
98     return;
99   }
100   if (fstat (v4l2object->video_fd, &s) < 0) {
101     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
102         (_("Cannot identify device '%s'."), v4l2object->videodev),
103         GST_ERROR_SYSTEM);
104     XCloseDisplay (dpy);
105     return;
106   }
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")) {
112       if (first_id == 0)
113         first_id = ai[i].base_id;
114
115       GST_DEBUG_OBJECT (v4l2object->element,
116           "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
117
118       /* hmm... */
119       if (first_id != 0 && ai[i].base_id == first_id + min)
120         id = ai[i].base_id;
121     }
122   }
123   XvFreeAdaptorInfo (ai);
124
125   if (id == 0) {
126     GST_WARNING_OBJECT (v4l2object->element,
127         "Did not find XvPortID for device - no overlay");
128     XCloseDisplay (dpy);
129     return;
130   }
131
132   v4l2xv = g_new0 (GstV4l2Xv, 1);
133   v4l2xv->dpy = dpy;
134   v4l2xv->port = id;
135   v4l2xv->mutex = g_mutex_new ();
136   v4l2xv->idle_id = 0;
137   v4l2xv->event_id = 0;
138   v4l2object->xv = v4l2xv;
139
140   if (v4l2object->xwindow_id) {
141     gst_v4l2_xoverlay_set_window_handle (v4l2object, v4l2object->xwindow_id);
142   }
143 }
144
145 static void
146 gst_v4l2_xoverlay_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_xoverlay_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_xoverlay_start (GstV4l2Object * v4l2object)
169 {
170   if (v4l2object->xwindow_id) {
171     gst_v4l2_xoverlay_open (v4l2object);
172   }
173 }
174
175 void
176 gst_v4l2_xoverlay_stop (GstV4l2Object * v4l2object)
177 {
178   gst_v4l2_xoverlay_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_xoverlay_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_xoverlay_set_window_handle (GstV4l2Object * v4l2object, guintptr id)
368 {
369   GstV4l2Xv *v4l2xv;
370   XID xwindow_id = id;
371   gboolean change = (v4l2object->xwindow_id != xwindow_id);
372
373   GST_LOG_OBJECT (v4l2object->element, "Setting XID to %lx",
374       (gulong) xwindow_id);
375
376   if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
377     gst_v4l2_xoverlay_open (v4l2object);
378
379   v4l2xv = v4l2object->xv;
380
381   if (v4l2xv)
382     g_mutex_lock (v4l2xv->mutex);
383
384   if (change) {
385     if (v4l2object->xwindow_id && v4l2xv) {
386       GST_DEBUG_OBJECT (v4l2object->element,
387           "Deactivating old port %lx", v4l2object->xwindow_id);
388
389       XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 0);
390       XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 0);
391       XvStopVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id);
392     }
393
394     v4l2object->xwindow_id = xwindow_id;
395   }
396
397   if (!v4l2xv || xwindow_id == 0) {
398     if (v4l2xv)
399       g_mutex_unlock (v4l2xv->mutex);
400     return;
401   }
402
403   if (change) {
404     GST_DEBUG_OBJECT (v4l2object->element, "Activating new port %lx",
405         xwindow_id);
406
407     /* draw */
408     XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 1);
409     XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 1);
410   }
411
412   update_geometry (v4l2object);
413
414   if (v4l2xv->idle_id)
415     g_source_remove (v4l2xv->idle_id);
416   v4l2xv->idle_id = g_idle_add (idle_refresh, v4l2object);
417   g_mutex_unlock (v4l2xv->mutex);
418 }
419
420 /**
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)
425  *
426  * Helper function to create a windo if none is set from the application.
427  */
428 void
429 gst_v4l2_xoverlay_prepare_xwindow_id (GstV4l2Object * v4l2object,
430     gboolean required)
431 {
432   if (!GST_V4L2_IS_OVERLAY (v4l2object))
433     return;
434
435   gst_x_overlay_prepare_xwindow_id (GST_X_OVERLAY (v4l2object->element));
436
437   if (required && !v4l2object->xwindow_id) {
438     GstV4l2Xv *v4l2xv;
439     Window win;
440     int width, height;
441     long event_mask;
442
443     if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
444       gst_v4l2_xoverlay_open (v4l2object);
445
446     v4l2xv = v4l2object->xv;
447
448     /* if xoverlay is not supported, just bail */
449     if (!v4l2xv)
450       return;
451
452     /* xoverlay is supported, but we don't have a window.. so create one */
453     GST_DEBUG_OBJECT (v4l2object->element, "creating window");
454
455     g_mutex_lock (v4l2xv->mutex);
456
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);
460
461     win = XCreateSimpleWindow (v4l2xv->dpy,
462         DefaultRootWindow (v4l2xv->dpy),
463         0, 0, width, height, 0, 0,
464         XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
465
466     GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
467
468     event_mask = ExposureMask | StructureNotifyMask;
469     if (GST_IS_NAVIGATION (v4l2object->element)) {
470       event_mask |= PointerMotionMask |
471           KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
472     }
473     XSelectInput (v4l2xv->dpy, win, event_mask);
474     v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
475
476     XMapRaised (v4l2xv->dpy, win);
477
478     XSync (v4l2xv->dpy, FALSE);
479
480     g_mutex_unlock (v4l2xv->mutex);
481
482     GST_DEBUG_OBJECT (v4l2object->element, "got window");
483
484     gst_v4l2_xoverlay_set_window_handle (v4l2object, win);
485   }
486 }