1 /*
2 * GStreamer
3 * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24
25 #include "gstvdpdevice.h"
26
27 GST_DEBUG_CATEGORY_STATIC (gst_vdp_device_debug);
28 #define GST_CAT_DEFAULT gst_vdp_device_debug
29
30 #define DEBUG_INIT(bla) \
31 GST_DEBUG_CATEGORY_INIT (gst_vdp_device_debug, "vdpdevice", 0, "VDPAU device object");
32
33 enum
34 {
35 PROP_0,
36 PROP_DISPLAY
37 };
38
39 G_DEFINE_TYPE_WITH_CODE (GstVdpDevice, gst_vdp_device, G_TYPE_OBJECT,
40 DEBUG_INIT ());
41
42 static gboolean
gst_vdp_device_open(GstVdpDevice * device,GError ** error)43 gst_vdp_device_open (GstVdpDevice * device, GError ** error)
44 {
45 gint screen;
46 VdpStatus status;
47 gint i;
48
49 typedef struct
50 {
51 gint id;
52 void *func;
53 } VdpFunction;
54
55 VdpFunction vdp_function[] = {
56 {VDP_FUNC_ID_DEVICE_DESTROY, &device->vdp_device_destroy},
57 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE,
58 &device->vdp_video_surface_create},
59 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY,
60 &device->vdp_video_surface_destroy},
61 {VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
62 &device->vdp_video_surface_query_capabilities},
63 {VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
64 &device->vdp_video_surface_query_ycbcr_capabilities},
65 {VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR,
66 &device->vdp_video_surface_get_bits_ycbcr},
67 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
68 &device->vdp_video_surface_put_bits_ycbcr},
69 {VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS,
70 &device->vdp_video_surface_get_parameters},
71 {VDP_FUNC_ID_DECODER_CREATE, &device->vdp_decoder_create},
72 {VDP_FUNC_ID_DECODER_RENDER, &device->vdp_decoder_render},
73 {VDP_FUNC_ID_DECODER_DESTROY, &device->vdp_decoder_destroy},
74 {VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
75 &device->vdp_decoder_query_capabilities},
76 {VDP_FUNC_ID_DECODER_GET_PARAMETERS,
77 &device->vdp_decoder_get_parameters},
78 {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &device->vdp_video_mixer_create},
79 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &device->vdp_video_mixer_destroy},
80 {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &device->vdp_video_mixer_render},
81 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
82 &device->vdp_video_mixer_set_feature_enables},
83 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
84 &device->vdp_video_mixer_set_attribute_values},
85 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &device->vdp_output_surface_create},
86 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &device->vdp_output_surface_destroy},
87 {VDP_FUNC_ID_OUTPUT_SURFACE_QUERY_CAPABILITIES,
88 &device->vdp_output_surface_query_capabilities},
89 {VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE,
90 &device->vdp_output_surface_get_bits_native},
91 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
92 &device->vdp_presentation_queue_target_create_x11},
93 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
94 &device->vdp_presentation_queue_target_destroy},
95 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE,
96 &device->vdp_presentation_queue_create},
97 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
98 &device->vdp_presentation_queue_destroy},
99 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
100 &device->vdp_presentation_queue_display},
101 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
102 &device->vdp_presentation_queue_block_until_surface_idle},
103 {VDP_FUNC_ID_PRESENTATION_QUEUE_SET_BACKGROUND_COLOR,
104 &device->vdp_presentation_queue_set_background_color},
105 {VDP_FUNC_ID_PRESENTATION_QUEUE_QUERY_SURFACE_STATUS,
106 &device->vdp_presentation_queue_query_surface_status}
107 };
108
109 GST_DEBUG_OBJECT (device, "Opening the device for display '%s'",
110 device->display_name);
111
112 device->display = XOpenDisplay (device->display_name);
113 if (!device->display)
114 goto create_display_error;
115
116 screen = DefaultScreen (device->display);
117 status =
118 vdp_device_create_x11 (device->display, screen, &device->device,
119 &device->vdp_get_proc_address);
120 if (status != VDP_STATUS_OK)
121 goto create_device_error;
122
123 status = device->vdp_get_proc_address (device->device,
124 VDP_FUNC_ID_GET_ERROR_STRING, (void **) &device->vdp_get_error_string);
125 if (status != VDP_STATUS_OK)
126 goto get_error_string_error;
127
128 for (i = 0; i < G_N_ELEMENTS (vdp_function); i++) {
129 status = device->vdp_get_proc_address (device->device,
130 vdp_function[i].id, vdp_function[i].func);
131
132 if (status != VDP_STATUS_OK)
133 goto function_error;
134 }
135
136 GST_DEBUG_OBJECT (device, "Succesfully opened the device");
137
138 return TRUE;
139
140 create_display_error:
141 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_READ,
142 "Could not open X display with name: %s", device->display_name);
143 return FALSE;
144
145 create_device_error:
146 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_READ,
147 "Could not create VDPAU device for display: %s", device->display_name);
148 return FALSE;
149
150 get_error_string_error:
151 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_READ,
152 "Could not get vdp_get_error_string function pointer from VDPAU");
153 return FALSE;
154
155 function_error:
156 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_READ,
157 "Could not get function pointer from VDPAU, error returned was: %s",
158 device->vdp_get_error_string (status));
159 return FALSE;
160 }
161
162 static GstVdpDevice *
gst_vdp_device_new(const gchar * display_name,GError ** error)163 gst_vdp_device_new (const gchar * display_name, GError ** error)
164 {
165 GstVdpDevice *device;
166
167 device = g_object_new (GST_TYPE_VDP_DEVICE, "display", display_name, NULL);
168
169 if (!gst_vdp_device_open (device, error)) {
170 g_object_unref (device);
171 return NULL;
172 }
173
174 return device;
175 }
176
177 static void
gst_vdp_device_init(GstVdpDevice * device)178 gst_vdp_device_init (GstVdpDevice * device)
179 {
180 device->display_name = NULL;
181 device->display = NULL;
182 device->device = VDP_INVALID_HANDLE;
183 device->vdp_decoder_destroy = NULL;
184 }
185
186 static void
gst_vdp_device_finalize(GObject * object)187 gst_vdp_device_finalize (GObject * object)
188 {
189 GstVdpDevice *device = (GstVdpDevice *) object;
190
191 if (device->device != VDP_INVALID_HANDLE && device->vdp_decoder_destroy) {
192 device->vdp_device_destroy (device->device);
193 device->device = VDP_INVALID_HANDLE;
194 }
195
196 if (device->display) {
197 XCloseDisplay (device->display);
198 device->display = NULL;
199 }
200
201 g_free (device->display_name);
202 device->display_name = NULL;
203
204 G_OBJECT_CLASS (gst_vdp_device_parent_class)->finalize (object);
205 }
206
207 static void
gst_vdp_device_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)208 gst_vdp_device_set_property (GObject * object, guint prop_id,
209 const GValue * value, GParamSpec * pspec)
210 {
211 GstVdpDevice *device;
212
213 g_return_if_fail (GST_IS_VDP_DEVICE (object));
214
215 device = (GstVdpDevice *) object;
216
217 switch (prop_id) {
218 case PROP_DISPLAY:
219 device->display_name = g_value_dup_string (value);
220 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 break;
224 }
225 }
226
227 static void
gst_vdp_device_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)228 gst_vdp_device_get_property (GObject * object, guint prop_id, GValue * value,
229 GParamSpec * pspec)
230 {
231 GstVdpDevice *device;
232
233 g_return_if_fail (GST_IS_VDP_DEVICE (object));
234
235 device = (GstVdpDevice *) object;
236
237 switch (prop_id) {
238 case PROP_DISPLAY:
239 g_value_set_string (value, device->display_name);
240 break;
241 default:
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 break;
244 }
245 }
246
247 static void
gst_vdp_device_class_init(GstVdpDeviceClass * klass)248 gst_vdp_device_class_init (GstVdpDeviceClass * klass)
249 {
250 GObjectClass *object_class = G_OBJECT_CLASS (klass);
251
252 object_class->finalize = gst_vdp_device_finalize;
253 object_class->get_property = gst_vdp_device_get_property;
254 object_class->set_property = gst_vdp_device_set_property;
255
256 g_object_class_install_property (object_class,
257 PROP_DISPLAY,
258 g_param_spec_string ("display",
259 "Display",
260 "X Display Name",
261 "", G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
262 }
263
264 typedef struct
265 {
266 GHashTable *hash_table;
267 GMutex mutex;
268 } GstVdpDeviceCache;
269
270 static void
device_destroyed_cb(gpointer data,GObject * object)271 device_destroyed_cb (gpointer data, GObject * object)
272 {
273 GstVdpDeviceCache *device_cache = data;
274 GHashTableIter iter;
275 gpointer device;
276
277 GST_DEBUG ("Removing object from hash table");
278
279 g_mutex_lock (&device_cache->mutex);
280
281 g_hash_table_iter_init (&iter, device_cache->hash_table);
282 while (g_hash_table_iter_next (&iter, NULL, &device)) {
283 if (device == object) {
284 g_hash_table_iter_remove (&iter);
285 break;
286 }
287 }
288
289 g_mutex_unlock (&device_cache->mutex);
290 }
291
292 GstVdpDevice *
gst_vdp_get_device(const gchar * display_name,GError ** error)293 gst_vdp_get_device (const gchar * display_name, GError ** error)
294 {
295 static gsize once = 0;
296 static GstVdpDeviceCache device_cache;
297 GstVdpDevice *device;
298
299 GST_DEBUG ("display_name '%s'", display_name);
300
301 if (g_once_init_enter (&once)) {
302 device_cache.hash_table =
303 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
304 g_mutex_init (&device_cache.mutex);
305
306 g_once_init_leave (&once, 1);
307 }
308
309 g_mutex_lock (&device_cache.mutex);
310
311 if (display_name)
312 device = g_hash_table_lookup (device_cache.hash_table, display_name);
313 else
314 device = g_hash_table_lookup (device_cache.hash_table, "");
315
316 if (!device) {
317 GST_DEBUG ("No cached device, creating a new one");
318 device = gst_vdp_device_new (display_name, error);
319 if (device) {
320 g_object_weak_ref (G_OBJECT (device), device_destroyed_cb, &device_cache);
321 if (display_name)
322 g_hash_table_insert (device_cache.hash_table, g_strdup (display_name),
323 device);
324 else
325 g_hash_table_insert (device_cache.hash_table, g_strdup (""), device);
326 } else
327 GST_ERROR ("Could not create GstVdpDevice !");
328 } else
329 g_object_ref (device);
330
331 g_mutex_unlock (&device_cache.mutex);
332
333 return device;
334 }
335