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>
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.
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.
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.
23 * SECTION:element-capsfilter
25 * The element does not modify data as such, but can enforce limitations on the
29 * <title>Example launch line</title>
31 * gst-launch videotestsrc ! video/x-raw,format=GRAY8 ! videoconvert ! autovideosink
32 * ]| Limits acceptable video from videotestsrc to be grayscale.
40 #include "../../gst/gst-i18n-lib.h"
41 #include "gstcapsfilter.h"
50 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_DEBUG_CATEGORY_STATIC (gst_capsfilter_debug);
62 #define GST_CAT_DEFAULT gst_capsfilter_debug
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,
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);
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,
84 static GstFlowReturn gst_capsfilter_prepare_buf (GstBaseTransform * trans,
85 GstBuffer * input, GstBuffer ** buf);
86 static gboolean gst_capsfilter_sink_event (GstBaseTransform * trans,
88 static gboolean gst_capsfilter_stop (GstBaseTransform * trans);
91 gst_capsfilter_class_init (GstCapsFilterClass * klass)
93 GObjectClass *gobject_class;
94 GstElementClass *gstelement_class;
95 GstBaseTransformClass *trans_class;
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;
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));
109 gstelement_class = GST_ELEMENT_CLASS (klass);
110 gst_element_class_set_static_metadata (gstelement_class,
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));
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);
132 gst_capsfilter_init (GstCapsFilter * filter)
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 ();
141 gst_capsfilter_set_property (GObject * object, guint prop_id,
142 const GValue * value, GParamSpec * pspec)
144 GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
147 case PROP_FILTER_CAPS:{
150 const GstCaps *new_caps_val = gst_value_get_caps (value);
152 if (new_caps_val == NULL) {
153 new_caps = gst_caps_new_any ();
155 new_caps = (GstCaps *) new_caps_val;
156 gst_caps_ref (new_caps);
159 GST_OBJECT_LOCK (capsfilter);
160 old_caps = capsfilter->filter_caps;
161 capsfilter->filter_caps = new_caps;
162 GST_OBJECT_UNLOCK (capsfilter);
164 gst_caps_unref (old_caps);
166 GST_DEBUG_OBJECT (capsfilter, "set new caps %" GST_PTR_FORMAT, new_caps);
168 gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (object));
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 gst_capsfilter_get_property (GObject * object, guint prop_id, GValue * value,
181 GstCapsFilter *capsfilter = GST_CAPSFILTER (object);
184 case PROP_FILTER_CAPS:
185 GST_OBJECT_LOCK (capsfilter);
186 gst_value_set_caps (value, capsfilter->filter_caps);
187 GST_OBJECT_UNLOCK (capsfilter);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 gst_capsfilter_dispose (GObject * object)
198 GstCapsFilter *filter = GST_CAPSFILTER (object);
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;
204 G_OBJECT_CLASS (parent_class)->dispose (object);
208 gst_capsfilter_transform_caps (GstBaseTransform * base,
209 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
211 GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
212 GstCaps *ret, *filter_caps, *tmp;
214 GST_OBJECT_LOCK (capsfilter);
215 filter_caps = gst_caps_ref (capsfilter->filter_caps);
216 GST_OBJECT_UNLOCK (capsfilter);
220 gst_caps_intersect_full (filter, filter_caps, GST_CAPS_INTERSECT_FIRST);
221 gst_caps_unref (filter_caps);
225 ret = gst_caps_intersect_full (filter_caps, caps, GST_CAPS_INTERSECT_FIRST);
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,
231 GST_DEBUG_OBJECT (capsfilter, "intersect: %" GST_PTR_FORMAT, ret);
233 gst_caps_unref (filter_caps);
239 gst_capsfilter_accept_caps (GstBaseTransform * base,
240 GstPadDirection direction, GstCaps * caps)
242 GstCapsFilter *capsfilter = GST_CAPSFILTER (base);
243 GstCaps *filter_caps;
246 GST_OBJECT_LOCK (capsfilter);
247 filter_caps = gst_caps_ref (capsfilter->filter_caps);
248 GST_OBJECT_UNLOCK (capsfilter);
250 ret = gst_caps_can_intersect (caps, filter_caps);
251 GST_DEBUG_OBJECT (capsfilter, "can intersect: %d", ret);
253 /* if we can intersect, see if the other end also accepts */
254 if (direction == GST_PAD_SRC)
256 gst_pad_peer_query_accept_caps (GST_BASE_TRANSFORM_SINK_PAD (base),
260 gst_pad_peer_query_accept_caps (GST_BASE_TRANSFORM_SRC_PAD (base),
262 GST_DEBUG_OBJECT (capsfilter, "peer accept: %d", ret);
265 gst_caps_unref (filter_caps);
271 gst_capsfilter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
273 /* No actual work here. It's all done in the prepare output buffer
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.
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
288 gst_capsfilter_prepare_buf (GstBaseTransform * trans, GstBuffer * input,
291 GstFlowReturn ret = GST_FLOW_OK;
293 /* always return the input as output buffer */
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);
300 GList *pending_events = filter->pending_events;
302 GST_LOG_OBJECT (trans, "Input pad does not have caps");
304 filter->pending_events = NULL;
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);
312 out_caps = gst_caps_simplify (out_caps);
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);
318 if (!gst_pad_has_current_caps (trans->srcpad)) {
319 if (gst_pad_set_caps (trans->srcpad, out_caps)) {
320 if (pending_events) {
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,
329 g_list_free (pending_events);
330 pending_events = NULL;
333 ret = GST_FLOW_NOT_NEGOTIATED;
337 g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
338 gst_caps_unref (out_caps);
340 gchar *caps_str = gst_caps_to_string (out_caps);
342 GST_DEBUG_OBJECT (trans, "Cannot choose caps. Have unfixed output caps %"
343 GST_PTR_FORMAT, out_caps);
344 gst_caps_unref (out_caps);
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));
351 g_list_free_full (pending_events, (GDestroyNotify) gst_event_unref);
353 ret = GST_FLOW_ERROR;
360 /* Queue the segment event if there was no caps event */
362 gst_capsfilter_sink_event (GstBaseTransform * trans, GstEvent * event)
364 GstCapsFilter *filter = GST_CAPSFILTER (trans);
366 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
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);
379 if (!GST_EVENT_IS_STICKY (event) || GST_EVENT_TYPE (event) <= GST_EVENT_CAPS)
382 /* If we get EOS before any buffers, just push all pending events */
383 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
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);
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));
397 filter->pending_events = g_list_prepend (filter->pending_events, event);
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);
409 gst_capsfilter_stop (GstBaseTransform * trans)
411 GstCapsFilter *filter = GST_CAPSFILTER (trans);
413 g_list_free_full (filter->pending_events, (GDestroyNotify) gst_event_unref);
414 filter->pending_events = NULL;