Send seek event to baseparse when aacparse seek failed in push mode
[platform/upstream/gst-plugins-good.git] / ext / gtk / gstgtkglsink.c
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
32 #include "gstgtkglsink.h"
33 #include "gtkgstglwidget.h"
34
35 GST_DEBUG_CATEGORY (gst_debug_gtk_gl_sink);
36 #define GST_CAT_DEFAULT gst_debug_gtk_gl_sink
37
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,
42     GstQuery * query);
43 static GstCaps *gst_gtk_gl_sink_get_caps (GstBaseSink * bsink,
44     GstCaps * filter);
45
46 static void gst_gtk_gl_sink_finalize (GObject * object);
47
48 static GstStaticPadTemplate gst_gtk_gl_sink_template =
49     GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
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")));
57
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"));
62
63 static void
64 gst_gtk_gl_sink_class_init (GstGtkGLSinkClass * klass)
65 {
66   GObjectClass *gobject_class;
67   GstElementClass *gstelement_class;
68   GstBaseSinkClass *gstbasesink_class;
69   GstGtkBaseSinkClass *gstgtkbasesink_class;
70
71   gobject_class = (GObjectClass *) klass;
72   gstelement_class = (GstElementClass *) klass;
73   gstbasesink_class = (GstBaseSinkClass *) klass;
74   gstgtkbasesink_class = (GstGtkBaseSinkClass *) klass;
75
76   gobject_class->finalize = gst_gtk_gl_sink_finalize;
77
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;
83
84   gstgtkbasesink_class->create_widget = gtk_gst_gl_widget_new;
85   gstgtkbasesink_class->window_title = "Gtk+ GL renderer";
86
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>");
90
91   gst_element_class_add_static_pad_template (gstelement_class,
92       &gst_gtk_gl_sink_template);
93 }
94
95 static void
96 gst_gtk_gl_sink_init (GstGtkGLSink * gtk_sink)
97 {
98 }
99
100 static gboolean
101 gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
102 {
103   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
104   gboolean res = FALSE;
105
106   switch (GST_QUERY_TYPE (query)) {
107     case GST_QUERY_CONTEXT:
108     {
109       if (gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
110               gtk_sink->display, gtk_sink->context, gtk_sink->gtk_context))
111         return TRUE;
112       break;
113     }
114     default:
115       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
116       break;
117   }
118
119   return res;
120 }
121
122 static void
123 _size_changed_cb (GtkWidget * widget, GdkRectangle * rectangle,
124     GstGtkGLSink * gtk_sink)
125 {
126   gint scale_factor, width, height;
127   gboolean reconfigure;
128
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);
132
133   GST_OBJECT_LOCK (gtk_sink);
134   reconfigure =
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);
139
140   if (reconfigure) {
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 ());
144   }
145 }
146
147 static void
148 destroy_cb (GtkWidget * widget, GstGtkGLSink * gtk_sink)
149 {
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;
153   }
154
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;
158   }
159 }
160
161 static gboolean
162 gst_gtk_gl_sink_start (GstBaseSink * bsink)
163 {
164   GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (bsink);
165   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
166   GtkGstGLWidget *gst_widget;
167
168   if (!GST_BASE_SINK_CLASS (parent_class)->start (bsink))
169     return FALSE;
170
171   /* After this point, gtk_sink->widget will always be set */
172   gst_widget = GTK_GST_GL_WIDGET (base_sink->widget);
173
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);
178
179   gtk_sink->widget_destroy_sig_handler =
180       g_signal_connect (gst_widget, "destroy", G_CALLBACK (destroy_cb),
181       gtk_sink);
182
183   _size_changed_cb (GTK_WIDGET (gst_widget), NULL, gtk_sink);
184
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));
188     return FALSE;
189   }
190
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);
194
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));
198     return FALSE;
199   }
200
201   gst_gl_element_propagate_display_context (GST_ELEMENT (bsink),
202       gtk_sink->display);
203
204   return TRUE;
205 }
206
207 static gboolean
208 gst_gtk_gl_sink_stop (GstBaseSink * bsink)
209 {
210   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
211
212   if (gtk_sink->display) {
213     gst_object_unref (gtk_sink->display);
214     gtk_sink->display = NULL;
215   }
216
217   if (gtk_sink->context) {
218     gst_object_unref (gtk_sink->context);
219     gtk_sink->context = NULL;
220   }
221
222   if (gtk_sink->gtk_context) {
223     gst_object_unref (gtk_sink->gtk_context);
224     gtk_sink->gtk_context = NULL;
225   }
226
227   return GST_BASE_SINK_CLASS (parent_class)->stop (bsink);
228 }
229
230 static gboolean
231 gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
232 {
233   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
234   GstBufferPool *pool = NULL;
235   GstStructure *config;
236   GstCaps *caps;
237   GstVideoInfo info;
238   guint size;
239   gboolean need_pool;
240   GstStructure *allocation_meta = NULL;
241   gint display_width, display_height;
242
243   if (!gtk_sink->display || !gtk_sink->context)
244     return FALSE;
245
246   gst_query_parse_allocation (query, &caps, &need_pool);
247
248   if (caps == NULL)
249     goto no_caps;
250
251   if (!gst_video_info_from_caps (&info, caps))
252     goto invalid_caps;
253
254   /* the normal size of a frame */
255   size = info.size;
256
257   if (need_pool) {
258     GST_DEBUG_OBJECT (gtk_sink, "create new pool");
259     pool = gst_gl_buffer_pool_new (gtk_sink->context);
260
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);
265
266     if (!gst_buffer_pool_set_config (pool, config))
267       goto config_failed;
268   }
269
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);
272   if (pool)
273     gst_object_unref (pool);
274
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);
279
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);
286   }
287
288   gst_query_add_allocation_meta (query,
289       GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);
290
291   if (allocation_meta)
292     gst_structure_free (allocation_meta);
293
294   /* we also support various metadata */
295   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
296
297   if (gtk_sink->context->gl_vtable->FenceSync)
298     gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
299
300   return TRUE;
301
302   /* ERRORS */
303 no_caps:
304   {
305     GST_DEBUG_OBJECT (bsink, "no caps specified");
306     return FALSE;
307   }
308 invalid_caps:
309   {
310     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
311     return FALSE;
312   }
313 config_failed:
314   {
315     GST_DEBUG_OBJECT (bsink, "failed setting config");
316     return FALSE;
317   }
318 }
319
320 static GstCaps *
321 gst_gtk_gl_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
322 {
323   GstCaps *tmp = NULL;
324   GstCaps *result = NULL;
325
326   tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
327
328   if (filter) {
329     GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
330         filter);
331
332     result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
333     gst_caps_unref (tmp);
334   } else {
335     result = tmp;
336   }
337
338   result = gst_gl_overlay_compositor_add_caps (result);
339
340   GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);
341
342   return result;
343 }
344
345 static void
346 gst_gtk_gl_sink_finalize (GObject * object)
347 {
348   GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (object);
349   GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (object);
350
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;
355   }
356
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;
361   }
362
363   G_OBJECT_CLASS (parent_class)->finalize (object);
364 }