3 * gstv4lxoverlay.c: X-based overlay interface implementation for V4L
5 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
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.
32 #include <X11/extensions/Xv.h>
33 #include <X11/extensions/Xvlib.h>
35 #include "gstv4lxoverlay.h"
36 #include "gstv4lelement.h"
37 #include "v4l_calls.h"
39 GST_DEBUG_CATEGORY_STATIC (v4lxv_debug);
40 #define GST_CAT_DEFAULT v4lxv_debug
49 static void gst_v4l_xoverlay_set_window_handle (GstXOverlay * overlay,
53 gst_v4l_xoverlay_interface_init (GstXOverlayClass * klass)
55 /* default virtual functions */
56 klass->set_window_handle = gst_v4l_xoverlay_set_window_handle;
58 GST_DEBUG_CATEGORY_INIT (v4lxv_debug, "v4lxv", 0,
59 "V4L XOverlay interface debugging");
63 gst_v4l_xoverlay_open (GstV4lElement * v4lelement)
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 ("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 ("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 ("Xv extension not supported - no overlay");
92 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
93 GST_WARNING ("Failed to query Xv adaptors");
97 if (fstat (v4lelement->video_fd, &s) < 0) {
98 GST_ERROR ("Failed to stat() file descriptor: %s", g_strerror (errno));
102 min = s.st_rdev & 0xff;
103 for (i = 0; i < anum; i++) {
104 if (!strcmp (ai[i].name, "video4linux")) {
106 first_id = ai[i].base_id;
109 if (first_id != 0 && ai[i].base_id == first_id + min)
113 XvFreeAdaptorInfo (ai);
116 GST_WARNING ("Did not find XvPortID for device - no overlay");
121 v4lxv = g_new0 (GstV4lXv, 1);
124 v4lxv->mutex = g_mutex_new ();
126 v4lelement->xv = v4lxv;
128 if (v4lelement->xwindow_id) {
129 gst_v4l_xoverlay_set_window_handle (GST_X_OVERLAY (v4lelement),
130 v4lelement->xwindow_id);
135 gst_v4l_xoverlay_close (GstV4lElement * v4lelement)
137 GstV4lXv *v4lxv = v4lelement->xv;
142 if (v4lelement->xwindow_id) {
143 gst_v4l_xoverlay_set_window_handle (GST_X_OVERLAY (v4lelement), 0);
146 XCloseDisplay (v4lxv->dpy);
147 g_mutex_free (v4lxv->mutex);
149 g_source_remove (v4lxv->idle_id);
151 v4lelement->xv = NULL;
155 gst_v4l_xoverlay_start (GstV4lElement * v4lelement)
157 if (v4lelement->xwindow_id) {
158 gst_v4l_xoverlay_open (v4lelement);
163 gst_v4l_xoverlay_stop (GstV4lElement * v4lelement)
165 gst_v4l_xoverlay_close (v4lelement);
169 idle_refresh (gpointer data)
171 GstV4lElement *v4lelement = GST_V4LELEMENT (data);
172 GstV4lXv *v4lxv = v4lelement->xv;
173 XWindowAttributes attr;
176 g_mutex_lock (v4lxv->mutex);
178 XGetWindowAttributes (v4lxv->dpy, v4lelement->xwindow_id, &attr);
179 XvPutVideo (v4lxv->dpy, v4lxv->port, v4lelement->xwindow_id,
180 DefaultGC (v4lxv->dpy, DefaultScreen (v4lxv->dpy)),
181 0, 0, attr.width, attr.height, 0, 0, attr.width, attr.height);
184 g_mutex_unlock (v4lxv->mutex);
192 gst_v4l_xoverlay_set_window_handle (GstXOverlay * overlay, guintptr id)
195 GstV4lElement *v4lelement = GST_V4LELEMENT (overlay);
197 XWindowAttributes attr;
198 gboolean change = (v4lelement->xwindow_id != xwindow_id);
200 GST_LOG_OBJECT (v4lelement, "Changing port to %lx", xwindow_id);
202 if (!v4lelement->xv && GST_V4L_IS_OPEN (v4lelement))
203 gst_v4l_xoverlay_open (v4lelement);
205 v4lxv = v4lelement->xv;
208 g_mutex_lock (v4lxv->mutex);
211 if (v4lelement->xwindow_id && v4lxv) {
212 GST_DEBUG_OBJECT (v4lelement,
213 "Disabling port %lx", v4lelement->xwindow_id);
215 XvSelectPortNotify (v4lxv->dpy, v4lxv->port, 0);
216 XvSelectVideoNotify (v4lxv->dpy, v4lelement->xwindow_id, 0);
217 XvStopVideo (v4lxv->dpy, v4lxv->port, v4lelement->xwindow_id);
220 v4lelement->xwindow_id = xwindow_id;
223 if (!v4lxv || xwindow_id == 0) {
225 g_mutex_unlock (v4lxv->mutex);
230 GST_DEBUG_OBJECT (v4lelement, "Enabling port %lx", xwindow_id);
233 XvSelectPortNotify (v4lxv->dpy, v4lxv->port, 1);
234 XvSelectVideoNotify (v4lxv->dpy, v4lelement->xwindow_id, 1);
237 XGetWindowAttributes (v4lxv->dpy, v4lelement->xwindow_id, &attr);
238 XvPutVideo (v4lxv->dpy, v4lxv->port, v4lelement->xwindow_id,
239 DefaultGC (v4lxv->dpy, DefaultScreen (v4lxv->dpy)),
240 0, 0, attr.width, attr.height, 0, 0, attr.width, attr.height);
243 g_source_remove (v4lxv->idle_id);
244 v4lxv->idle_id = g_idle_add (idle_refresh, v4lelement);
245 g_mutex_unlock (v4lxv->mutex);