Reset display-name if the user provided his own X11 display.
[vaapi:gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidisplay_x11.c
1 /*
2  *  gstvaapidisplay_x11.c - VA/X11 display abstraction
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "vaapi_utils.h"
23 #include "gstvaapidisplay_x11.h"
24
25 #define DEBUG 1
26 #include "vaapi_debug.h"
27
28 G_DEFINE_TYPE(GstVaapiDisplayX11,
29               gst_vaapi_display_x11,
30               GST_VAAPI_TYPE_DISPLAY);
31
32 #define GST_VAAPI_DISPLAY_X11_GET_PRIVATE(obj)                  \
33     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
34                                  GST_VAAPI_TYPE_DISPLAY_X11,    \
35                                  GstVaapiDisplayX11Private))
36
37 struct _GstVaapiDisplayX11Private {
38     gboolean    create_display;
39     gchar      *display_name;
40     Display    *x11_display;
41     VADisplay  *va_display;
42 };
43
44 enum {
45     PROP_0,
46
47     PROP_DISPLAY_NAME,
48     PROP_X11_DISPLAY
49 };
50
51 static void
52 gst_vaapi_display_x11_finalize(GObject *object)
53 {
54     G_OBJECT_CLASS(gst_vaapi_display_x11_parent_class)->finalize(object);
55 }
56
57 static void
58 set_display_name(GstVaapiDisplayX11 *display, const gchar *display_name)
59 {
60     GstVaapiDisplayX11Private * const priv = display->priv;
61
62     g_free(priv->display_name);
63
64     if (display_name)
65         priv->display_name = g_strdup(display_name);
66     else
67         priv->display_name = NULL;
68 }
69
70 static void
71 gst_vaapi_display_x11_set_property(
72     GObject      *object,
73     guint         prop_id,
74     const GValue *value,
75     GParamSpec   *pspec
76 )
77 {
78     GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
79
80     switch (prop_id) {
81     case PROP_DISPLAY_NAME:
82         set_display_name(display, g_value_get_string(value));
83         break;
84     case PROP_X11_DISPLAY:
85         display->priv->x11_display = g_value_get_pointer(value);
86         break;
87     default:
88         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
89         break;
90     }
91 }
92
93 static void
94 gst_vaapi_display_x11_get_property(
95     GObject    *object,
96     guint       prop_id,
97     GValue     *value,
98     GParamSpec *pspec
99 )
100 {
101     GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
102
103     switch (prop_id) {
104     case PROP_DISPLAY_NAME:
105         g_value_set_string(value, display->priv->display_name);
106         break;
107     case PROP_X11_DISPLAY:
108         g_value_set_pointer(value, gst_vaapi_display_x11_get_display(display));
109         break;
110     default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
112         break;
113     }
114 }
115
116 static void
117 gst_vaapi_display_x11_constructed(GObject *object)
118 {
119     GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
120     GObjectClass *parent_class;
121
122     display->priv->create_display = display->priv->x11_display == NULL;
123
124     /* Reset display-name if the user provided his own X11 display */
125     if (!display->priv->create_display)
126         set_display_name(display, XDisplayString(display->priv->x11_display));
127
128     parent_class = G_OBJECT_CLASS(gst_vaapi_display_x11_parent_class);
129     if (parent_class->constructed)
130         parent_class->constructed(object);
131 }
132
133 static gboolean
134 gst_vaapi_display_x11_open_display(GstVaapiDisplay *display)
135 {
136     GstVaapiDisplayX11Private * const priv =
137         GST_VAAPI_DISPLAY_X11(display)->priv;
138
139     /* XXX: maintain an X11 display cache */
140     if (!priv->x11_display && priv->create_display)
141         priv->x11_display = XOpenDisplay(priv->display_name);
142     if (!priv->x11_display)
143         return FALSE;
144
145     priv->va_display = vaGetDisplay(priv->x11_display);
146     return priv->va_display != NULL;
147 }
148
149 static void
150 gst_vaapi_display_x11_close_display(GstVaapiDisplay *display)
151 {
152     GstVaapiDisplayX11Private * const priv =
153         GST_VAAPI_DISPLAY_X11(display)->priv;
154
155     if (priv->x11_display) {
156         if (priv->create_display)
157             XCloseDisplay(priv->x11_display);
158         priv->x11_display = NULL;
159     }
160
161     if (priv->display_name) {
162         g_free(priv->display_name);
163         priv->display_name = NULL;
164     }
165
166     priv->va_display = NULL;
167 }
168
169 static VADisplay
170 gst_vaapi_display_x11_get_va_display(GstVaapiDisplay *display)
171 {
172     return GST_VAAPI_DISPLAY_X11(display)->priv->va_display;
173 }
174
175 static void
176 gst_vaapi_display_x11_class_init(GstVaapiDisplayX11Class *klass)
177 {
178     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
179     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
180
181     g_type_class_add_private(klass, sizeof(GstVaapiDisplayX11Private));
182
183     object_class->finalize      = gst_vaapi_display_x11_finalize;
184     object_class->set_property  = gst_vaapi_display_x11_set_property;
185     object_class->get_property  = gst_vaapi_display_x11_get_property;
186     object_class->constructed   = gst_vaapi_display_x11_constructed;
187
188     dpy_class->open_display     = gst_vaapi_display_x11_open_display;
189     dpy_class->close_display    = gst_vaapi_display_x11_close_display;
190     dpy_class->get_display      = gst_vaapi_display_x11_get_va_display;
191
192     g_object_class_install_property
193         (object_class,
194          PROP_X11_DISPLAY,
195          g_param_spec_pointer("x11-display",
196                               "X11 display",
197                               "X11 display",
198                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
199
200     g_object_class_install_property
201         (object_class,
202          PROP_DISPLAY_NAME,
203          g_param_spec_string("display-name",
204                              "X11 display name",
205                              "X11 display name",
206                              NULL,
207                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
208 }
209
210 static void
211 gst_vaapi_display_x11_init(GstVaapiDisplayX11 *display)
212 {
213     GstVaapiDisplayX11Private *priv = GST_VAAPI_DISPLAY_X11_GET_PRIVATE(display);
214
215     display->priv        = priv;
216     priv->create_display = TRUE;
217     priv->x11_display    = NULL;
218     priv->display_name   = NULL;
219 }
220
221 GstVaapiDisplay *
222 gst_vaapi_display_x11_new(const gchar *display_name)
223 {
224     return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
225                         "display-name", display_name,
226                         NULL);
227 }
228
229 GstVaapiDisplay *
230 gst_vaapi_display_x11_new_with_display(Display *x11_display)
231 {
232     return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
233                         "x11-display", x11_display,
234                         NULL);
235 }
236
237 Display *
238 gst_vaapi_display_x11_get_display(GstVaapiDisplayX11 *display)
239 {
240     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
241
242     return display->priv->x11_display;
243 }