f4a1743cee14a73f13aa23ba70b4beb367b98bf7
[platform/upstream/gst-plugins-good.git] / gst / debugutils / gstcapsdebug.c
1 /* GStreamer
2  * Copyright (C) 2010 David Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <gst/gst.h>
27 #include "gstcapsdebug.h"
28
29 /* prototypes */
30
31
32 static void gst_caps_debug_set_property (GObject * object,
33     guint property_id, const GValue * value, GParamSpec * pspec);
34 static void gst_caps_debug_get_property (GObject * object,
35     guint property_id, GValue * value, GParamSpec * pspec);
36 static void gst_caps_debug_dispose (GObject * object);
37 static void gst_caps_debug_finalize (GObject * object);
38
39 static GstFlowReturn gst_caps_debug_sink_chain (GstPad * pad,
40     GstBuffer * buffer);
41 static GstCaps *gst_caps_debug_getcaps (GstPad * pad);
42 static gboolean gst_caps_debug_acceptcaps (GstPad * pad, GstCaps * caps);
43 static GstFlowReturn gst_caps_debug_bufferalloc (GstPad * pad,
44     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
45
46 static GstStateChangeReturn
47 gst_caps_debug_change_state (GstElement * element, GstStateChange transition);
48
49 enum
50 {
51   PROP_0
52 };
53
54 /* pad templates */
55
56 static GstStaticPadTemplate gst_caps_debug_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS_ANY);
61
62 static GstStaticPadTemplate gst_caps_debug_src_template =
63 GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS_ANY);
67
68 /* class initialization */
69
70 GST_BOILERPLATE (GstCapsDebug, gst_caps_debug, GstElement, GST_TYPE_ELEMENT);
71
72 static void
73 gst_caps_debug_base_init (gpointer g_class)
74 {
75   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
76
77   gst_element_class_add_pad_template (element_class,
78       gst_static_pad_template_get (&gst_caps_debug_src_template));
79   gst_element_class_add_pad_template (element_class,
80       gst_static_pad_template_get (&gst_caps_debug_sink_template));
81
82   gst_element_class_set_details_simple (element_class, "Caps debug",
83       "Generic", "Debug caps negotiation", "David Schleef <ds@schleef.org>");
84 }
85
86 static void
87 gst_caps_debug_class_init (GstCapsDebugClass * klass)
88 {
89   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
91
92   gobject_class->set_property = gst_caps_debug_set_property;
93   gobject_class->get_property = gst_caps_debug_get_property;
94   gobject_class->dispose = gst_caps_debug_dispose;
95   gobject_class->finalize = gst_caps_debug_finalize;
96   element_class->change_state = GST_DEBUG_FUNCPTR (gst_caps_debug_change_state);
97
98 }
99
100 static void
101 gst_caps_debug_init (GstCapsDebug * capsdebug,
102     GstCapsDebugClass * capsdebug_class)
103 {
104
105   capsdebug->srcpad =
106       gst_pad_new_from_template (gst_static_pad_template_get
107       (&gst_caps_debug_src_template), "src");
108   gst_pad_set_getcaps_function (capsdebug->srcpad,
109       GST_DEBUG_FUNCPTR (gst_caps_debug_getcaps));
110   gst_pad_set_acceptcaps_function (capsdebug->srcpad,
111       GST_DEBUG_FUNCPTR (gst_caps_debug_acceptcaps));
112   gst_element_add_pad (GST_ELEMENT (capsdebug), capsdebug->srcpad);
113
114   capsdebug->sinkpad =
115       gst_pad_new_from_template (gst_static_pad_template_get
116       (&gst_caps_debug_sink_template), "sink");
117   gst_pad_set_chain_function (capsdebug->sinkpad,
118       GST_DEBUG_FUNCPTR (gst_caps_debug_sink_chain));
119   gst_pad_set_bufferalloc_function (capsdebug->sinkpad,
120       GST_DEBUG_FUNCPTR (gst_caps_debug_bufferalloc));
121   gst_pad_set_getcaps_function (capsdebug->sinkpad,
122       GST_DEBUG_FUNCPTR (gst_caps_debug_getcaps));
123   gst_pad_set_acceptcaps_function (capsdebug->sinkpad,
124       GST_DEBUG_FUNCPTR (gst_caps_debug_acceptcaps));
125   gst_element_add_pad (GST_ELEMENT (capsdebug), capsdebug->sinkpad);
126
127 }
128
129 void
130 gst_caps_debug_set_property (GObject * object, guint property_id,
131     const GValue * value, GParamSpec * pspec)
132 {
133   GstCapsDebug *capsdebug;
134
135   g_return_if_fail (GST_IS_CAPS_DEBUG (object));
136   capsdebug = GST_CAPS_DEBUG (object);
137
138   switch (property_id) {
139     default:
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
141       break;
142   }
143 }
144
145 void
146 gst_caps_debug_get_property (GObject * object, guint property_id,
147     GValue * value, GParamSpec * pspec)
148 {
149   GstCapsDebug *capsdebug;
150
151   g_return_if_fail (GST_IS_CAPS_DEBUG (object));
152   capsdebug = GST_CAPS_DEBUG (object);
153
154   switch (property_id) {
155     default:
156       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
157       break;
158   }
159 }
160
161 void
162 gst_caps_debug_dispose (GObject * object)
163 {
164   GstCapsDebug *capsdebug;
165
166   g_return_if_fail (GST_IS_CAPS_DEBUG (object));
167   capsdebug = GST_CAPS_DEBUG (object);
168
169   /* clean up as possible.  may be called multiple times */
170
171   G_OBJECT_CLASS (parent_class)->dispose (object);
172 }
173
174 void
175 gst_caps_debug_finalize (GObject * object)
176 {
177   GstCapsDebug *capsdebug;
178
179   g_return_if_fail (GST_IS_CAPS_DEBUG (object));
180   capsdebug = GST_CAPS_DEBUG (object);
181
182   /* clean up object here */
183
184   G_OBJECT_CLASS (parent_class)->finalize (object);
185 }
186
187
188
189 static GstStateChangeReturn
190 gst_caps_debug_change_state (GstElement * element, GstStateChange transition)
191 {
192   GstStateChangeReturn ret;
193
194   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
195
196   return GST_STATE_CHANGE_SUCCESS;
197 }
198
199
200 static GstFlowReturn
201 gst_caps_debug_sink_chain (GstPad * pad, GstBuffer * buffer)
202 {
203   GstFlowReturn ret;
204   GstCapsDebug *capsdebug;
205
206   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
207
208   ret = gst_pad_push (capsdebug->srcpad, buffer);
209
210   gst_object_unref (capsdebug);
211
212   return ret;
213 }
214
215 #define THISPAD ((pad == capsdebug->srcpad) ? "downstream" : "upstream")
216 #define OTHERPAD ((pad == capsdebug->srcpad) ? "upstream" : "downstream")
217
218 static GstCaps *
219 gst_caps_debug_getcaps (GstPad * pad)
220 {
221   GstCaps *caps;
222   GstCapsDebug *capsdebug;
223   gchar *s;
224   GstPad *otherpad;
225
226   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
227   otherpad =
228       (pad == capsdebug->srcpad) ? capsdebug->sinkpad : capsdebug->srcpad;
229
230   GST_INFO ("%s called getcaps", THISPAD);
231
232   caps = gst_pad_peer_get_caps (otherpad);
233
234   s = gst_caps_to_string (caps);
235   GST_INFO ("%s returned %s", OTHERPAD, s);
236   g_free (s);
237
238   if (caps == NULL)
239     caps = gst_caps_new_any ();
240
241   gst_object_unref (capsdebug);
242
243   return caps;
244 }
245
246
247 static gboolean
248 gst_caps_debug_acceptcaps (GstPad * pad, GstCaps * caps)
249 {
250   GstCapsDebug *capsdebug;
251   gchar *s;
252   gboolean ret;
253   GstPad *otherpad;
254
255   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
256   otherpad =
257       (pad == capsdebug->srcpad) ? capsdebug->sinkpad : capsdebug->srcpad;
258
259   s = gst_caps_to_string (caps);
260   GST_INFO ("%s called acceptcaps with %s", THISPAD, s);
261   g_free (s);
262
263   ret = gst_pad_peer_accept_caps (otherpad, caps);
264
265   GST_INFO ("%s returned %s", OTHERPAD, ret ? "TRUE" : "FALSE");
266
267   gst_object_unref (capsdebug);
268
269   return ret;
270 }
271
272 static GstFlowReturn
273 gst_caps_debug_bufferalloc (GstPad * pad, guint64 offset, guint size,
274     GstCaps * caps, GstBuffer ** buf)
275 {
276   GstCapsDebug *capsdebug;
277   gchar *s;
278   gchar *t;
279   GstFlowReturn ret;
280   GstPad *otherpad;
281   gboolean newcaps;
282
283   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
284   otherpad =
285       (pad == capsdebug->srcpad) ? capsdebug->sinkpad : capsdebug->srcpad;
286
287   newcaps = (caps != GST_PAD_CAPS (pad));
288
289   if (newcaps) {
290     s = gst_caps_to_string (caps);
291     GST_INFO ("%s called bufferalloc with new caps, offset=%" G_GUINT64_FORMAT
292         " size=%d caps=%s", THISPAD, offset, size, s);
293     g_free (s);
294   }
295
296   ret = gst_pad_alloc_buffer_and_set_caps (otherpad, offset, size, caps, buf);
297
298   if (newcaps) {
299     GST_INFO ("%s returned %s", OTHERPAD, gst_flow_get_name (ret));
300   }
301   if (caps != GST_BUFFER_CAPS (*buf)) {
302     s = gst_caps_to_string (caps);
303     t = gst_caps_to_string (GST_BUFFER_CAPS (*buf));
304     GST_INFO
305         ("%s returned from bufferalloc with different caps, requested=%s returned=%s",
306         OTHERPAD, s, t);
307     g_free (s);
308     g_free (t);
309   }
310
311   gst_object_unref (capsdebug);
312
313   return ret;
314 }