1 /*
2 * GStreamer
3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.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 /**
22 * SECTION:element-gtkglsink
23 * @title: gtkglsink
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <gst/gl/gstglfuncs.h>
31 #include <gst/video/gstvideoaffinetransformationmeta.h>
32
33 #include "gstgtkglsink.h"
34 #include "gtkgstglwidget.h"
35
36 GST_DEBUG_CATEGORY (gst_debug_gtk_gl_sink);
37 #define GST_CAT_DEFAULT gst_debug_gtk_gl_sink
38
39 static void gst_gtk_gl_sink_set_property (GObject * object, guint prop_id,
40 const GValue * value, GParamSpec * pspec);
41 static void gst_gtk_gl_sink_get_property (GObject * object, guint prop_id,
42 GValue * value, GParamSpec * pspec);
43
44 static gboolean gst_gtk_gl_sink_start (GstBaseSink * bsink);
45 static gboolean gst_gtk_gl_sink_stop (GstBaseSink * bsink);
46 static gboolean gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query);
47 static gboolean gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink,
48 GstQuery * query);
49 static GstCaps *gst_gtk_gl_sink_get_caps (GstBaseSink * bsink,
50 GstCaps * filter);
51 static gboolean gst_gtk_gl_sink_event (GstBaseSink * sink, GstEvent * event);
52
53 static void gst_gtk_gl_sink_finalize (GObject * object);
54
55 static GstStaticPadTemplate gst_gtk_gl_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_PAD_SINK,
58 GST_PAD_ALWAYS,
59 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
60 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, "RGBA") "; "
61 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
62 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY ", "
63 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, "RGBA")));
64
65 #define gst_gtk_gl_sink_parent_class parent_class
66 G_DEFINE_TYPE_WITH_CODE (GstGtkGLSink, gst_gtk_gl_sink,
67 GST_TYPE_GTK_BASE_SINK, GST_DEBUG_CATEGORY_INIT (gst_debug_gtk_gl_sink,
68 "gtkglsink", 0, "Gtk GL Video Sink"));
69 GST_ELEMENT_REGISTER_DEFINE (gtkglsink, "gtkglsink", GST_RANK_NONE,
70 GST_TYPE_GTK_GL_SINK);
71
72
73 enum
74 {
75 PROP_0,
76 PROP_ROTATE_METHOD,
77 };
78
79 static void
gst_gtk_gl_sink_class_init(GstGtkGLSinkClass * klass)80 gst_gtk_gl_sink_class_init (GstGtkGLSinkClass * klass)
81 {
82 GObjectClass *gobject_class;
83 GstElementClass *gstelement_class;
84 GstBaseSinkClass *gstbasesink_class;
85 GstGtkBaseSinkClass *gstgtkbasesink_class;
86
87 gobject_class = (GObjectClass *) klass;
88 gstelement_class = (GstElementClass *) klass;
89 gstbasesink_class = (GstBaseSinkClass *) klass;
90 gstgtkbasesink_class = (GstGtkBaseSinkClass *) klass;
91
92 gobject_class->set_property = gst_gtk_gl_sink_set_property;
93 gobject_class->get_property = gst_gtk_gl_sink_get_property;
94 gobject_class->finalize = gst_gtk_gl_sink_finalize;
95
96 gstbasesink_class->query = gst_gtk_gl_sink_query;
97 gstbasesink_class->propose_allocation = gst_gtk_gl_sink_propose_allocation;
98 gstbasesink_class->start = gst_gtk_gl_sink_start;
99 gstbasesink_class->stop = gst_gtk_gl_sink_stop;
100 gstbasesink_class->get_caps = gst_gtk_gl_sink_get_caps;
101 gstbasesink_class->event = gst_gtk_gl_sink_event;
102
103 gstgtkbasesink_class->create_widget = gtk_gst_gl_widget_new;
104 gstgtkbasesink_class->window_title = "Gtk+ GL renderer";
105
106 /**
107 * gtkglsink:rotate-method:
108 *
109 * Rotation method #GstVideoOrientationMethod used to render the media
110 *
111 * Since: 1.20
112 */
113 g_object_class_install_property (gobject_class, PROP_ROTATE_METHOD,
114 g_param_spec_enum ("rotate-method",
115 "rotate method",
116 "rotate method",
117 GST_TYPE_VIDEO_ORIENTATION_METHOD, GST_VIDEO_ORIENTATION_IDENTITY,
118 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119
120 gst_element_class_set_metadata (gstelement_class, "Gtk GL Video Sink",
121 "Sink/Video", "A video sink that renders to a GtkWidget using OpenGL",
122 "Matthew Waters <matthew@centricular.com>");
123
124 gst_element_class_add_static_pad_template (gstelement_class,
125 &gst_gtk_gl_sink_template);
126 }
127
128 static void
gst_gtk_gl_sink_init(GstGtkGLSink * gtk_sink)129 gst_gtk_gl_sink_init (GstGtkGLSink * gtk_sink)
130 {
131 }
132
133 static void
gst_gtk_gl_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)134 gst_gtk_gl_sink_set_property (GObject * object, guint prop_id,
135 const GValue * value, GParamSpec * pspec)
136 {
137 switch (prop_id) {
138 case PROP_ROTATE_METHOD:
139 {
140 GtkWidget *widget =
141 gst_gtk_base_sink_acquire_widget (GST_GTK_BASE_SINK (object));
142 if (widget != NULL) {
143 gtk_gst_gl_widget_set_rotate_method (GTK_GST_GL_WIDGET (widget),
144 g_value_get_enum (value), FALSE);
145 g_object_unref (widget);
146 }
147 break;
148 }
149 default:
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 break;
152 }
153 }
154
155
156 static void
gst_gtk_gl_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)157 gst_gtk_gl_sink_get_property (GObject * object, guint prop_id,
158 GValue * value, GParamSpec * pspec)
159 {
160 switch (prop_id) {
161 case PROP_ROTATE_METHOD:
162 {
163 GtkWidget *widget =
164 gst_gtk_base_sink_acquire_widget (GST_GTK_BASE_SINK (object));
165
166 if (widget != NULL) {
167 g_value_set_enum (value,
168 gtk_gst_gl_widget_get_rotate_method (GTK_GST_GL_WIDGET (widget)));
169 g_object_unref (widget);
170 } else {
171 g_value_set_enum (value, GST_VIDEO_ORIENTATION_IDENTITY);
172 }
173
174 break;
175 }
176 default:
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 break;
179 }
180 }
181
182 static gboolean
gst_gtk_gl_sink_query(GstBaseSink * bsink,GstQuery * query)183 gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
184 {
185 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
186 gboolean res = FALSE;
187
188 switch (GST_QUERY_TYPE (query)) {
189 case GST_QUERY_CONTEXT:
190 {
191 if (gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
192 gtk_sink->display, gtk_sink->context, gtk_sink->gtk_context))
193 return TRUE;
194 break;
195 }
196 default:
197 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
198 break;
199 }
200
201 return res;
202 }
203
204 static void
_size_changed_cb(GtkWidget * widget,GdkRectangle * rectangle,GstGtkGLSink * gtk_sink)205 _size_changed_cb (GtkWidget * widget, GdkRectangle * rectangle,
206 GstGtkGLSink * gtk_sink)
207 {
208 gint scale_factor, width, height;
209 gboolean reconfigure;
210
211 scale_factor = gtk_widget_get_scale_factor (widget);
212 width = scale_factor * gtk_widget_get_allocated_width (widget);
213 height = scale_factor * gtk_widget_get_allocated_height (widget);
214
215 GST_OBJECT_LOCK (gtk_sink);
216 reconfigure =
217 (width != gtk_sink->display_width || height != gtk_sink->display_height);
218 gtk_sink->display_width = width;
219 gtk_sink->display_height = height;
220 GST_OBJECT_UNLOCK (gtk_sink);
221
222 if (reconfigure) {
223 GST_DEBUG_OBJECT (gtk_sink, "Sending reconfigure event on sinkpad.");
224 gst_pad_push_event (GST_BASE_SINK (gtk_sink)->sinkpad,
225 gst_event_new_reconfigure ());
226 }
227 }
228
229 static void
destroy_cb(GtkWidget * widget,GstGtkGLSink * gtk_sink)230 destroy_cb (GtkWidget * widget, GstGtkGLSink * gtk_sink)
231 {
232 if (gtk_sink->size_allocate_sig_handler) {
233 g_signal_handler_disconnect (widget, gtk_sink->size_allocate_sig_handler);
234 gtk_sink->size_allocate_sig_handler = 0;
235 }
236
237 if (gtk_sink->widget_destroy_sig_handler) {
238 g_signal_handler_disconnect (widget, gtk_sink->widget_destroy_sig_handler);
239 gtk_sink->widget_destroy_sig_handler = 0;
240 }
241 }
242
243 static gboolean
gst_gtk_gl_sink_start(GstBaseSink * bsink)244 gst_gtk_gl_sink_start (GstBaseSink * bsink)
245 {
246 GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (bsink);
247 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
248 GtkGstGLWidget *gst_widget;
249
250 if (!GST_BASE_SINK_CLASS (parent_class)->start (bsink))
251 return FALSE;
252
253 /* After this point, gtk_sink->widget will always be set */
254 gst_widget = GTK_GST_GL_WIDGET (base_sink->widget);
255
256 /* Track the allocation size */
257 gtk_sink->size_allocate_sig_handler =
258 g_signal_connect (gst_widget, "size-allocate",
259 G_CALLBACK (_size_changed_cb), gtk_sink);
260
261 gtk_sink->widget_destroy_sig_handler =
262 g_signal_connect (gst_widget, "destroy", G_CALLBACK (destroy_cb),
263 gtk_sink);
264
265 _size_changed_cb (GTK_WIDGET (gst_widget), NULL, gtk_sink);
266
267 if (!gtk_gst_gl_widget_init_winsys (gst_widget)) {
268 GST_ELEMENT_ERROR (bsink, RESOURCE, NOT_FOUND, ("%s",
269 "Failed to initialize OpenGL with Gtk"), (NULL));
270 return FALSE;
271 }
272
273 gtk_sink->display = gtk_gst_gl_widget_get_display (gst_widget);
274 gtk_sink->context = gtk_gst_gl_widget_get_context (gst_widget);
275 gtk_sink->gtk_context = gtk_gst_gl_widget_get_gtk_context (gst_widget);
276
277 if (!gtk_sink->display || !gtk_sink->context || !gtk_sink->gtk_context) {
278 GST_ELEMENT_ERROR (bsink, RESOURCE, NOT_FOUND, ("%s",
279 "Failed to retrieve OpenGL context from Gtk"), (NULL));
280 return FALSE;
281 }
282
283 gst_gl_element_propagate_display_context (GST_ELEMENT (bsink),
284 gtk_sink->display);
285
286 return TRUE;
287 }
288
289 static gboolean
gst_gtk_gl_sink_stop(GstBaseSink * bsink)290 gst_gtk_gl_sink_stop (GstBaseSink * bsink)
291 {
292 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
293
294 if (gtk_sink->display) {
295 gst_object_unref (gtk_sink->display);
296 gtk_sink->display = NULL;
297 }
298
299 if (gtk_sink->context) {
300 gst_object_unref (gtk_sink->context);
301 gtk_sink->context = NULL;
302 }
303
304 if (gtk_sink->gtk_context) {
305 gst_object_unref (gtk_sink->gtk_context);
306 gtk_sink->gtk_context = NULL;
307 }
308
309 return GST_BASE_SINK_CLASS (parent_class)->stop (bsink);
310 }
311
312 static gboolean
gst_gtk_gl_sink_propose_allocation(GstBaseSink * bsink,GstQuery * query)313 gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
314 {
315 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
316 GstBufferPool *pool = NULL;
317 GstStructure *config;
318 GstCaps *caps;
319 GstVideoInfo info;
320 guint size;
321 gboolean need_pool;
322 GstStructure *allocation_meta = NULL;
323 gint display_width, display_height;
324
325 if (!gtk_sink->display || !gtk_sink->context)
326 return FALSE;
327
328 gst_query_parse_allocation (query, &caps, &need_pool);
329
330 if (caps == NULL)
331 goto no_caps;
332
333 if (!gst_video_info_from_caps (&info, caps))
334 goto invalid_caps;
335
336 /* the normal size of a frame */
337 size = info.size;
338
339 if (need_pool) {
340 GST_DEBUG_OBJECT (gtk_sink, "create new pool");
341 pool = gst_gl_buffer_pool_new (gtk_sink->context);
342
343 config = gst_buffer_pool_get_config (pool);
344 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
345 gst_buffer_pool_config_add_option (config,
346 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
347
348 if (!gst_buffer_pool_set_config (pool, config))
349 goto config_failed;
350 }
351
352 /* we need at least 2 buffer because we hold on to the last one */
353 gst_query_add_allocation_pool (query, pool, size, 2, 0);
354 if (pool)
355 gst_object_unref (pool);
356
357 GST_OBJECT_LOCK (gtk_sink);
358 display_width = gtk_sink->display_width;
359 display_height = gtk_sink->display_height;
360 GST_OBJECT_UNLOCK (gtk_sink);
361
362 if (display_width != 0 && display_height != 0) {
363 GST_DEBUG_OBJECT (gtk_sink, "sending alloc query with size %dx%d",
364 display_width, display_height);
365 allocation_meta = gst_structure_new ("GstVideoOverlayCompositionMeta",
366 "width", G_TYPE_UINT, display_width,
367 "height", G_TYPE_UINT, display_height, NULL);
368 }
369
370 gst_query_add_allocation_meta (query,
371 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);
372
373 if (allocation_meta)
374 gst_structure_free (allocation_meta);
375
376 /* we also support various metadata */
377 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
378 gst_query_add_allocation_meta (query,
379 GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, 0);
380
381 if (gtk_sink->context->gl_vtable->FenceSync)
382 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
383
384 return TRUE;
385
386 /* ERRORS */
387 no_caps:
388 {
389 GST_DEBUG_OBJECT (bsink, "no caps specified");
390 return FALSE;
391 }
392 invalid_caps:
393 {
394 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
395 return FALSE;
396 }
397 config_failed:
398 {
399 GST_DEBUG_OBJECT (bsink, "failed setting config");
400 return FALSE;
401 }
402 }
403
404 static GstCaps *
gst_gtk_gl_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)405 gst_gtk_gl_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
406 {
407 GstCaps *tmp = NULL;
408 GstCaps *result = NULL;
409
410 tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
411
412 if (filter) {
413 GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
414 filter);
415
416 result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
417 gst_caps_unref (tmp);
418 } else {
419 result = tmp;
420 }
421
422 result = gst_gl_overlay_compositor_add_caps (result);
423
424 GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);
425
426 return result;
427 }
428
429 static void
gst_gtk_gl_sink_finalize(GObject * object)430 gst_gtk_gl_sink_finalize (GObject * object)
431 {
432 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (object);
433 GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (object);
434
435 if (gtk_sink->size_allocate_sig_handler) {
436 g_signal_handler_disconnect (base_sink->widget,
437 gtk_sink->size_allocate_sig_handler);
438 gtk_sink->size_allocate_sig_handler = 0;
439 }
440
441 if (gtk_sink->widget_destroy_sig_handler) {
442 g_signal_handler_disconnect (base_sink->widget,
443 gtk_sink->widget_destroy_sig_handler);
444 gtk_sink->widget_destroy_sig_handler = 0;
445 }
446
447 G_OBJECT_CLASS (parent_class)->finalize (object);
448 }
449
450
451 static gboolean
gst_gtk_gl_sink_event(GstBaseSink * sink,GstEvent * event)452 gst_gtk_gl_sink_event (GstBaseSink * sink, GstEvent * event)
453 {
454 GstTagList *taglist;
455 GstVideoOrientationMethod orientation;
456 gboolean ret;
457 GtkGstGLWidget *widget;
458
459 GST_DEBUG_OBJECT (sink, "handling %s event", GST_EVENT_TYPE_NAME (event));
460
461 switch (GST_EVENT_TYPE (event)) {
462 case GST_EVENT_TAG:
463 gst_event_parse_tag (event, &taglist);
464
465 if (gst_video_orientation_from_tag (taglist, &orientation)) {
466
467 widget = GTK_GST_GL_WIDGET
468 (gst_gtk_base_sink_acquire_widget (GST_GTK_BASE_SINK (sink)));
469 if (widget == NULL) {
470 GST_ERROR_OBJECT (sink, "Could not ensure GTK initialization.");
471 break;
472 }
473
474 gtk_gst_gl_widget_set_rotate_method (widget, orientation, TRUE);
475
476 g_object_unref (widget);
477 }
478 break;
479 default:
480 break;
481 }
482
483 ret = GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
484
485 return ret;
486 }
487