3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
22 * SECTION:element-gtkglsink
30 #include <gst/gl/gstglfuncs.h>
32 #include "gstgtkglsink.h"
33 #include "gtkgstglwidget.h"
35 GST_DEBUG_CATEGORY (gst_debug_gtk_gl_sink);
36 #define GST_CAT_DEFAULT gst_debug_gtk_gl_sink
38 static gboolean gst_gtk_gl_sink_start (GstBaseSink * bsink);
39 static gboolean gst_gtk_gl_sink_stop (GstBaseSink * bsink);
40 static gboolean gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query);
41 static gboolean gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink,
43 static GstCaps *gst_gtk_gl_sink_get_caps (GstBaseSink * bsink,
46 static void gst_gtk_gl_sink_finalize (GObject * object);
48 static GstStaticPadTemplate gst_gtk_gl_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
53 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, "RGBA") "; "
54 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
55 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY ", "
56 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, "RGBA")));
58 #define gst_gtk_gl_sink_parent_class parent_class
59 G_DEFINE_TYPE_WITH_CODE (GstGtkGLSink, gst_gtk_gl_sink,
60 GST_TYPE_GTK_BASE_SINK, GST_DEBUG_CATEGORY_INIT (gst_debug_gtk_gl_sink,
61 "gtkglsink", 0, "Gtk GL Video Sink"));
64 gst_gtk_gl_sink_class_init (GstGtkGLSinkClass * klass)
66 GObjectClass *gobject_class;
67 GstElementClass *gstelement_class;
68 GstBaseSinkClass *gstbasesink_class;
69 GstGtkBaseSinkClass *gstgtkbasesink_class;
71 gobject_class = (GObjectClass *) klass;
72 gstelement_class = (GstElementClass *) klass;
73 gstbasesink_class = (GstBaseSinkClass *) klass;
74 gstgtkbasesink_class = (GstGtkBaseSinkClass *) klass;
76 gobject_class->finalize = gst_gtk_gl_sink_finalize;
78 gstbasesink_class->query = gst_gtk_gl_sink_query;
79 gstbasesink_class->propose_allocation = gst_gtk_gl_sink_propose_allocation;
80 gstbasesink_class->start = gst_gtk_gl_sink_start;
81 gstbasesink_class->stop = gst_gtk_gl_sink_stop;
82 gstbasesink_class->get_caps = gst_gtk_gl_sink_get_caps;
84 gstgtkbasesink_class->create_widget = gtk_gst_gl_widget_new;
85 gstgtkbasesink_class->window_title = "Gtk+ GL renderer";
87 gst_element_class_set_metadata (gstelement_class, "Gtk GL Video Sink",
88 "Sink/Video", "A video sink that renders to a GtkWidget using OpenGL",
89 "Matthew Waters <matthew@centricular.com>");
91 gst_element_class_add_static_pad_template (gstelement_class,
92 &gst_gtk_gl_sink_template);
96 gst_gtk_gl_sink_init (GstGtkGLSink * gtk_sink)
101 gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
103 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
104 gboolean res = FALSE;
106 switch (GST_QUERY_TYPE (query)) {
107 case GST_QUERY_CONTEXT:
109 if (gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
110 gtk_sink->display, gtk_sink->context, gtk_sink->gtk_context))
115 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
123 _size_changed_cb (GtkWidget * widget, GdkRectangle * rectangle,
124 GstGtkGLSink * gtk_sink)
126 gint scale_factor, width, height;
127 gboolean reconfigure;
129 scale_factor = gtk_widget_get_scale_factor (widget);
130 width = scale_factor * gtk_widget_get_allocated_width (widget);
131 height = scale_factor * gtk_widget_get_allocated_height (widget);
133 GST_OBJECT_LOCK (gtk_sink);
135 (width != gtk_sink->display_width || height != gtk_sink->display_height);
136 gtk_sink->display_width = width;
137 gtk_sink->display_height = height;
138 GST_OBJECT_UNLOCK (gtk_sink);
141 GST_DEBUG_OBJECT (gtk_sink, "Sending reconfigure event on sinkpad.");
142 gst_pad_push_event (GST_BASE_SINK (gtk_sink)->sinkpad,
143 gst_event_new_reconfigure ());
148 destroy_cb (GtkWidget * widget, GstGtkGLSink * gtk_sink)
150 if (gtk_sink->size_allocate_sig_handler) {
151 g_signal_handler_disconnect (widget, gtk_sink->size_allocate_sig_handler);
152 gtk_sink->size_allocate_sig_handler = 0;
155 if (gtk_sink->widget_destroy_sig_handler) {
156 g_signal_handler_disconnect (widget, gtk_sink->widget_destroy_sig_handler);
157 gtk_sink->widget_destroy_sig_handler = 0;
162 gst_gtk_gl_sink_start (GstBaseSink * bsink)
164 GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (bsink);
165 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
166 GtkGstGLWidget *gst_widget;
168 if (!GST_BASE_SINK_CLASS (parent_class)->start (bsink))
171 /* After this point, gtk_sink->widget will always be set */
172 gst_widget = GTK_GST_GL_WIDGET (base_sink->widget);
174 /* Track the allocation size */
175 gtk_sink->size_allocate_sig_handler =
176 g_signal_connect (gst_widget, "size-allocate",
177 G_CALLBACK (_size_changed_cb), gtk_sink);
179 gtk_sink->widget_destroy_sig_handler =
180 g_signal_connect (gst_widget, "destroy", G_CALLBACK (destroy_cb),
183 _size_changed_cb (GTK_WIDGET (gst_widget), NULL, gtk_sink);
185 if (!gtk_gst_gl_widget_init_winsys (gst_widget)) {
186 GST_ELEMENT_ERROR (bsink, RESOURCE, NOT_FOUND, ("%s",
187 "Failed to initialize OpenGL with Gtk"), (NULL));
191 gtk_sink->display = gtk_gst_gl_widget_get_display (gst_widget);
192 gtk_sink->context = gtk_gst_gl_widget_get_context (gst_widget);
193 gtk_sink->gtk_context = gtk_gst_gl_widget_get_gtk_context (gst_widget);
195 if (!gtk_sink->display || !gtk_sink->context || !gtk_sink->gtk_context) {
196 GST_ELEMENT_ERROR (bsink, RESOURCE, NOT_FOUND, ("%s",
197 "Failed to retrieve OpenGL context from Gtk"), (NULL));
201 gst_gl_element_propagate_display_context (GST_ELEMENT (bsink),
208 gst_gtk_gl_sink_stop (GstBaseSink * bsink)
210 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
212 if (gtk_sink->display) {
213 gst_object_unref (gtk_sink->display);
214 gtk_sink->display = NULL;
217 if (gtk_sink->context) {
218 gst_object_unref (gtk_sink->context);
219 gtk_sink->context = NULL;
222 if (gtk_sink->gtk_context) {
223 gst_object_unref (gtk_sink->gtk_context);
224 gtk_sink->gtk_context = NULL;
227 return GST_BASE_SINK_CLASS (parent_class)->stop (bsink);
231 gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
233 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
234 GstBufferPool *pool = NULL;
235 GstStructure *config;
240 GstStructure *allocation_meta = NULL;
241 gint display_width, display_height;
243 if (!gtk_sink->display || !gtk_sink->context)
246 gst_query_parse_allocation (query, &caps, &need_pool);
251 if (!gst_video_info_from_caps (&info, caps))
254 /* the normal size of a frame */
258 GST_DEBUG_OBJECT (gtk_sink, "create new pool");
259 pool = gst_gl_buffer_pool_new (gtk_sink->context);
261 config = gst_buffer_pool_get_config (pool);
262 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
263 gst_buffer_pool_config_add_option (config,
264 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
266 if (!gst_buffer_pool_set_config (pool, config))
270 /* we need at least 2 buffer because we hold on to the last one */
271 gst_query_add_allocation_pool (query, pool, size, 2, 0);
273 gst_object_unref (pool);
275 GST_OBJECT_LOCK (gtk_sink);
276 display_width = gtk_sink->display_width;
277 display_height = gtk_sink->display_height;
278 GST_OBJECT_UNLOCK (gtk_sink);
280 if (display_width != 0 && display_height != 0) {
281 GST_DEBUG_OBJECT (gtk_sink, "sending alloc query with size %dx%d",
282 display_width, display_height);
283 allocation_meta = gst_structure_new ("GstVideoOverlayCompositionMeta",
284 "width", G_TYPE_UINT, display_width,
285 "height", G_TYPE_UINT, display_height, NULL);
288 gst_query_add_allocation_meta (query,
289 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);
292 gst_structure_free (allocation_meta);
294 /* we also support various metadata */
295 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
297 if (gtk_sink->context->gl_vtable->FenceSync)
298 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
305 GST_DEBUG_OBJECT (bsink, "no caps specified");
310 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
315 GST_DEBUG_OBJECT (bsink, "failed setting config");
321 gst_gtk_gl_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
324 GstCaps *result = NULL;
326 tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
329 GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
332 result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
333 gst_caps_unref (tmp);
338 result = gst_gl_overlay_compositor_add_caps (result);
340 GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);
346 gst_gtk_gl_sink_finalize (GObject * object)
348 GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (object);
349 GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (object);
351 if (gtk_sink->size_allocate_sig_handler) {
352 g_signal_handler_disconnect (base_sink->widget,
353 gtk_sink->size_allocate_sig_handler);
354 gtk_sink->size_allocate_sig_handler = 0;
357 if (gtk_sink->widget_destroy_sig_handler) {
358 g_signal_handler_disconnect (base_sink->widget,
359 gtk_sink->widget_destroy_sig_handler);
360 gtk_sink->widget_destroy_sig_handler = 0;
363 G_OBJECT_CLASS (parent_class)->finalize (object);