capsfilter: Add more debug output and forward caps events immediately too
[platform/upstream/gstreamer.git] / plugins / elements / gstcapsfilter.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *                    2005 David Schleef <ds@schleef.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 /**
23  * SECTION:element-capsfilter
24  *
25  * The element does not modify data as such, but can enforce limitations on the
26  * data format.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch videotestsrc ! video/x-raw,format=GRAY8 ! videoconvert ! autovideosink
32  * ]| Limits acceptable video from videotestsrc to be grayscale.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "../../gst/gst-i18n-lib.h"
41 #include "gstcapsfilter.h"
42
43 enum
44 {
45   PROP_0,
46   PROP_FILTER_CAPS
47 };
48
49
50 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS_ANY);
54
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS_ANY);
59
60
61 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
62 #define GST_CAT_DEFAULT gst_capsfilter_debug
63
64 #define _do_init \
65     GST_DEBUG_CATEGORY_INIT (gst_capsfilter_debug, "capsfilter", 0, \
66     "capsfilter element");
67 #define gst_capsfilter_parent_class parent_class
68 G_DEFINE_TYPE_WITH_CODE (GstCapsFilter, gst_capsfilter, GST_TYPE_BASE_TRANSFORM,
69     _do_init);
70
71
72 static void gst_capsfilter_set_property (GObject * object, guint prop_id,
73     const GValue * value, GParamSpec * pspec);
74 static void gst_capsfilter_get_property (GObject * object, guint prop_id,
75     GValue * value, GParamSpec * pspec);
76 static void gst_capsfilter_dispose (GObject * object);
77
78 static GstCaps *gst_capsfilter_transform_caps (GstBaseTransform * base,
79     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
80 static gboolean gst_capsfilter_accept_caps (GstBaseTransform * base,
81     GstPadDirection direction, GstCaps * caps);
82 static GstFlowReturn gst_capsfilter_transform_ip (GstBaseTransform * base,
83     GstBuffer * buf);
84 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
85     GstBuffer * input, GstBuffer ** buf);
86 static gboolean gst_capsfilter_sink_event (GstBaseTransform * trans,
87     GstEvent * event);
88 static gboolean gst_capsfilter_stop (GstBaseTransform * trans);
89
90 static void
91 gst_capsfilter_class_init (GstCapsFilterClass * klass)
92 {
93   GObjectClass *gobject_class;
94   GstElementClass *gstelement_class;
95   GstBaseTransformClass *trans_class;
96
97   gobject_class = G_OBJECT_CLASS (klass);
98   gobject_class->set_property = gst_capsfilter_set_property;
99   gobject_class->get_property = gst_capsfilter_get_property;
100   gobject_class->dispose = gst_capsfilter_dispose;
101
102   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
103       g_param_spec_boxed ("caps", _("Filter caps"),
104           _("Restrict the possible allowed capabilities (NULL means ANY). "
105               "Setting this property takes a reference to the supplied GstCaps "
106               "object."), GST_TYPE_CAPS,
107           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108
109   gstelement_class = GST_ELEMENT_CLASS (klass);
110   gst_element_class_set_static_metadata (gstelement_class,
111       "CapsFilter",
112       "Generic",
113       "Pass data without modification, limiting formats",
114       "David Schleef <ds@schleef.org>");
115   gst_element_class_add_pad_template (gstelement_class,
116       gst_static_pad_template_get (&srctemplate));
117   gst_element_class_add_pad_template (gstelement_class,
118       gst_static_pad_template_get (&sinktemplate));
119
120   trans_class = GST_BASE_TRANSFORM_CLASS (klass);
121   trans_class->transform_caps =
122       GST_DEBUG_FUNCPTR (gst_capsfilter_transform_caps);
123   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_capsfilter_transform_ip);
124   trans_class->accept_caps = GST_DEBUG_FUNCPTR (gst_capsfilter_accept_caps);
125   trans_class->prepare_output_buffer =
126       GST_DEBUG_FUNCPTR (gst_capsfilter_prepare_buf);
127   trans_class->sink_event = GST_DEBUG_FUNCPTR (gst_capsfilter_sink_event);
128   trans_class->stop = GST_DEBUG_FUNCPTR (gst_capsfilter_stop);
129 }
130
131 static void
132 gst_capsfilter_init (GstCapsFilter * filter)
133 {
134   GstBaseTransform *trans = GST_BASE_TRANSFORM (filter);
135   gst_base_transform_set_gap_aware (trans, TRUE);
136   gst_base_transform_set_prefer_passthrough (trans, FALSE);
137   filter->filter_caps = gst_caps_new_any ();
138 }
139
140 static void
141 gst_capsfilter_set_property (GObject * object, guint prop_id,
142     const GValue * value, GParamSpec * pspec)
143 {
144   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
145
146   switch (prop_id) {
147     case PROP_FILTER_CAPS:{
148       GstCaps *new_caps;
149       GstCaps *old_caps;
150       const GstCaps *new_caps_val = gst_value_get_caps (value);
151
152       if (new_caps_val == NULL) {
153         new_caps = gst_caps_new_any ();
154       } else {
155         new_caps = (GstCaps *) new_caps_val;
156         gst_caps_ref (new_caps);
157       }
158
159       GST_OBJECT_LOCK (capsfilter);
160       old_caps = capsfilter->filter_caps;
161       capsfilter->filter_caps = new_caps;
162       GST_OBJECT_UNLOCK (capsfilter);
163
164       gst_caps_unref (old_caps);
165
166       GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
167
168       gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (object));
169       break;
170     }
171     default:
172       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173       break;
174   }
175 }
176
177 static void
178 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
179     GParamSpec * pspec)
180 {
181   GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
182
183   switch (prop_id) {
184     case PROP_FILTER_CAPS:
185       GST_OBJECT_LOCK (capsfilter);
186       gst_value_set_caps (value, capsfilter->filter_caps);
187       GST_OBJECT_UNLOCK (capsfilter);
188       break;
189     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191       break;
192   }
193 }
194
195 static void
196 gst_capsfilter_dispose (GObject * object)
197 {
198   GstCapsFilter *filter = GST_CAPSFILTER (object);
199
200   gst_caps_replace (&filter->filter_caps, NULL);
201   g_list_free_full (filter->pending_events, (GDestroyNotify) gst_event_unref);
202   filter->pending_events = NULL;
203
204   G_OBJECT_CLASS (parent_class)->dispose (object);
205 }
206
207 static GstCaps *
208 gst_capsfilter_transform_caps (GstBaseTransform * base,
209     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
210 {
211   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
212   GstCaps *ret, *filter_caps, *tmp;
213
214   GST_OBJECT_LOCK (capsfilter);
215   filter_caps = gst_caps_ref (capsfilter->filter_caps);
216   GST_OBJECT_UNLOCK (capsfilter);
217
218   if (filter) {
219     tmp =
220         gst_caps_intersect_full (filter, filter_caps, GST_CAPS_INTERSECT_FIRST);
221     gst_caps_unref (filter_caps);
222     filter_caps = tmp;
223   }
224
225   ret = gst_caps_intersect_full (filter_caps, caps, GST_CAPS_INTERSECT_FIRST);
226
227   GST_DEBUG_OBJECT (capsfilter, "input:     %" GST_PTR_FORMAT, caps);
228   GST_DEBUG_OBJECT (capsfilter, "filter:    %" GST_PTR_FORMAT, filter);
229   GST_DEBUG_OBJECT (capsfilter, "caps filter:    %" GST_PTR_FORMAT,
230       filter_caps);
231   GST_DEBUG_OBJECT (capsfilter, "intersect: %" GST_PTR_FORMAT, ret);
232
233   gst_caps_unref (filter_caps);
234
235   return ret;
236 }
237
238 static gboolean
239 gst_capsfilter_accept_caps (GstBaseTransform * base,
240     GstPadDirection direction, GstCaps * caps)
241 {
242   GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
243   GstCaps *filter_caps;
244   gboolean ret;
245
246   GST_OBJECT_LOCK (capsfilter);
247   filter_caps = gst_caps_ref (capsfilter->filter_caps);
248   GST_OBJECT_UNLOCK (capsfilter);
249
250   ret = gst_caps_can_intersect (caps, filter_caps);
251   GST_DEBUG_OBJECT (capsfilter, "can intersect: %d", ret);
252   if (ret) {
253     /* if we can intersect, see if the other end also accepts */
254     if (direction == GST_PAD_SRC)
255       ret =
256           gst_pad_peer_query_accept_caps (GST_BASE_TRANSFORM_SINK_PAD (base),
257           caps);
258     else
259       ret =
260           gst_pad_peer_query_accept_caps (GST_BASE_TRANSFORM_SRC_PAD (base),
261           caps);
262     GST_DEBUG_OBJECT (capsfilter, "peer accept: %d", ret);
263   }
264
265   gst_caps_unref (filter_caps);
266
267   return ret;
268 }
269
270 static GstFlowReturn
271 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
272 {
273   /* No actual work here. It's all done in the prepare output buffer
274    * func. */
275   return GST_FLOW_OK;
276 }
277
278 /* Ouput buffer preparation ... if the buffer has no caps, and our allowed
279  * output caps is fixed, then send the caps downstream, making sure caps are
280  * sent before segment event.
281  *
282  * This ensures that caps event is sent if we can, so that pipelines like:
283  *   gst-launch filesrc location=rawsamples.raw !
284  *       audio/x-raw,format=S16LE,rate=48000,channels=2 ! alsasink
285  * will work.
286  */
287 static GstFlowReturn
288 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
289     GstBuffer ** buf)
290 {
291   GstFlowReturn ret = GST_FLOW_OK;
292
293   /* always return the input as output buffer */
294   *buf = input;
295
296   if (!gst_pad_has_current_caps (trans->sinkpad)) {
297     /* No caps. See if the output pad only supports fixed caps */
298     GstCapsFilter *filter = GST_CAPSFILTER (trans);
299     GstCaps *out_caps;
300     GList *pending_events = filter->pending_events;
301
302     GST_LOG_OBJECT (trans, "Input pad does not have caps");
303
304     filter->pending_events = NULL;
305
306     out_caps = gst_pad_get_current_caps (trans->srcpad);
307     if (out_caps == NULL) {
308       out_caps = gst_pad_get_allowed_caps (trans->srcpad);
309       g_return_val_if_fail (out_caps != NULL, GST_FLOW_ERROR);
310     }
311
312     out_caps = gst_caps_simplify (out_caps);
313
314     if (gst_caps_is_fixed (out_caps) && !gst_caps_is_empty (out_caps)) {
315       GST_DEBUG_OBJECT (trans, "Have fixed output caps %"
316           GST_PTR_FORMAT " to apply to srcpad", out_caps);
317
318       if (!gst_pad_has_current_caps (trans->srcpad)) {
319         if (gst_pad_set_caps (trans->srcpad, out_caps)) {
320           if (pending_events) {
321             GList *l;
322
323             for (l = g_list_last (pending_events); l; l = l->prev) {
324               GST_LOG_OBJECT (trans, "Forwarding %s event",
325                   GST_EVENT_TYPE_NAME (l->data));
326               GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans,
327                   l->data);
328             }
329             g_list_free (pending_events);
330             pending_events = NULL;
331           }
332         } else {
333           ret = GST_FLOW_NOT_NEGOTIATED;
334         }
335       }
336
337       g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
338       gst_caps_unref (out_caps);
339     } else {
340       gchar *caps_str = gst_caps_to_string (out_caps);
341
342       GST_DEBUG_OBJECT (trans, "Cannot choose caps. Have unfixed output caps %"
343           GST_PTR_FORMAT, out_caps);
344       gst_caps_unref (out_caps);
345
346       GST_ELEMENT_ERROR (trans, STREAM, FORMAT,
347           ("Filter caps do not completely specify the output format"),
348           ("Output caps are unfixed: %s", caps_str));
349
350       g_free (caps_str);
351       g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
352
353       ret = GST_FLOW_ERROR;
354     }
355   }
356
357   return ret;
358 }
359
360 /* Queue the segment event if there was no caps event */
361 static gboolean
362 gst_capsfilter_sink_event (GstBaseTransform * trans, GstEvent * event)
363 {
364   GstCapsFilter *filter = GST_CAPSFILTER (trans);
365
366   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
367     GList *l;
368
369     for (l = filter->pending_events; l;) {
370       if (GST_EVENT_TYPE (l->data) == GST_EVENT_SEGMENT) {
371         gst_event_unref (l->data);
372         l = g_list_delete_link (l, l);
373       } else {
374         l = l->next;
375       }
376     }
377   }
378
379   if (!GST_EVENT_IS_STICKY (event) || GST_EVENT_TYPE (event) <= GST_EVENT_CAPS)
380     goto done;
381
382   /* If we get EOS before any buffers, just push all pending events */
383   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
384     GList *l;
385
386     for (l = g_list_last (filter->pending_events); l; l = l->prev) {
387       GST_LOG_OBJECT (trans, "Forwarding %s event",
388           GST_EVENT_TYPE_NAME (l->data));
389       GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, l->data);
390     }
391     g_list_free (filter->pending_events);
392     filter->pending_events = NULL;
393   } else if (!gst_pad_has_current_caps (trans->sinkpad)) {
394     GST_LOG_OBJECT (trans, "Got %s event before caps, queueing",
395         GST_EVENT_TYPE_NAME (event));
396
397     filter->pending_events = g_list_prepend (filter->pending_events, event);
398
399     return TRUE;
400   }
401
402 done:
403
404   GST_LOG_OBJECT (trans, "Forwarding %s event", GST_EVENT_TYPE_NAME (event));
405   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
406 }
407
408 static gboolean
409 gst_capsfilter_stop (GstBaseTransform * trans)
410 {
411   GstCapsFilter *filter = GST_CAPSFILTER (trans);
412
413   g_list_free_full (filter->pending_events, (GDestroyNotify) gst_event_unref);
414   filter->pending_events = NULL;
415
416   return TRUE;
417 }