upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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 GST_DEBUG_CATEGORY_STATIC (gst_caps_debug_debug);
30 #define GST_CAT_DEFAULT gst_caps_debug_debug
31
32 /* prototypes */
33
34
35 static void gst_caps_debug_dispose (GObject * object);
36 static void gst_caps_debug_finalize (GObject * object);
37
38 static GstFlowReturn gst_caps_debug_sink_chain (GstPad * pad,
39     GstBuffer * buffer);
40 static GstCaps *gst_caps_debug_getcaps (GstPad * pad);
41 static gboolean gst_caps_debug_acceptcaps (GstPad * pad, GstCaps * caps);
42 static GstFlowReturn gst_caps_debug_bufferalloc (GstPad * pad,
43     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
44
45 static GstStateChangeReturn
46 gst_caps_debug_change_state (GstElement * element, GstStateChange transition);
47
48 /* pad templates */
49
50 static GstStaticPadTemplate gst_caps_debug_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55
56 static GstStaticPadTemplate gst_caps_debug_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS_ANY);
61
62 /* class initialization */
63
64 #define DEBUG_INIT(bla) \
65   GST_DEBUG_CATEGORY_INIT (gst_caps_debug_debug, "capsdebug", 0, \
66       "debug category for capsdebug element");
67
68 GST_BOILERPLATE_FULL (GstCapsDebug, gst_caps_debug, GstElement,
69     GST_TYPE_ELEMENT, DEBUG_INIT);
70
71 static void
72 gst_caps_debug_base_init (gpointer g_class)
73 {
74   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
75
76   gst_element_class_add_pad_template (element_class,
77       gst_static_pad_template_get (&gst_caps_debug_src_template));
78   gst_element_class_add_pad_template (element_class,
79       gst_static_pad_template_get (&gst_caps_debug_sink_template));
80
81   gst_element_class_set_details_simple (element_class, "Caps debug",
82       "Generic", "Debug caps negotiation", "David Schleef <ds@schleef.org>");
83 }
84
85 static void
86 gst_caps_debug_class_init (GstCapsDebugClass * klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
90
91   gobject_class->dispose = gst_caps_debug_dispose;
92   gobject_class->finalize = gst_caps_debug_finalize;
93   element_class->change_state = GST_DEBUG_FUNCPTR (gst_caps_debug_change_state);
94
95 }
96
97 static void
98 gst_caps_debug_init (GstCapsDebug * capsdebug,
99     GstCapsDebugClass * capsdebug_class)
100 {
101
102   capsdebug->srcpad =
103       gst_pad_new_from_static_template (&gst_caps_debug_src_template, "src");
104   gst_pad_set_getcaps_function (capsdebug->srcpad,
105       GST_DEBUG_FUNCPTR (gst_caps_debug_getcaps));
106   gst_pad_set_acceptcaps_function (capsdebug->srcpad,
107       GST_DEBUG_FUNCPTR (gst_caps_debug_acceptcaps));
108   gst_element_add_pad (GST_ELEMENT (capsdebug), capsdebug->srcpad);
109
110   capsdebug->sinkpad =
111       gst_pad_new_from_static_template (&gst_caps_debug_sink_template, "sink");
112   gst_pad_set_chain_function (capsdebug->sinkpad,
113       GST_DEBUG_FUNCPTR (gst_caps_debug_sink_chain));
114   gst_pad_set_bufferalloc_function (capsdebug->sinkpad,
115       GST_DEBUG_FUNCPTR (gst_caps_debug_bufferalloc));
116   gst_pad_set_getcaps_function (capsdebug->sinkpad,
117       GST_DEBUG_FUNCPTR (gst_caps_debug_getcaps));
118   gst_pad_set_acceptcaps_function (capsdebug->sinkpad,
119       GST_DEBUG_FUNCPTR (gst_caps_debug_acceptcaps));
120   gst_element_add_pad (GST_ELEMENT (capsdebug), capsdebug->sinkpad);
121
122 }
123
124 void
125 gst_caps_debug_dispose (GObject * object)
126 {
127   /* clean up as possible.  may be called multiple times */
128
129   G_OBJECT_CLASS (parent_class)->dispose (object);
130 }
131
132 void
133 gst_caps_debug_finalize (GObject * object)
134 {
135   /* clean up object here */
136
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139
140
141
142 static GstStateChangeReturn
143 gst_caps_debug_change_state (GstElement * element, GstStateChange transition)
144 {
145   GstStateChangeReturn ret;
146
147   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
148
149   return ret;
150 }
151
152
153 static GstFlowReturn
154 gst_caps_debug_sink_chain (GstPad * pad, GstBuffer * buffer)
155 {
156   GstFlowReturn ret;
157   GstCapsDebug *capsdebug;
158
159   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
160
161   ret = gst_pad_push (capsdebug->srcpad, buffer);
162
163   gst_object_unref (capsdebug);
164
165   return ret;
166 }
167
168 #define THISPAD ((pad == capsdebug->srcpad) ? "downstream" : "upstream")
169 #define OTHERPAD ((pad == capsdebug->srcpad) ? "upstream" : "downstream")
170
171 static GstCaps *
172 gst_caps_debug_getcaps (GstPad * pad)
173 {
174   GstCaps *caps;
175   GstCapsDebug *capsdebug;
176   gchar *s;
177   GstPad *otherpad;
178
179   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
180   otherpad =
181       (pad == capsdebug->srcpad) ? capsdebug->sinkpad : capsdebug->srcpad;
182
183   GST_INFO ("%s called getcaps", THISPAD);
184
185   caps = gst_pad_peer_get_caps (otherpad);
186
187   s = gst_caps_to_string (caps);
188   GST_INFO ("%s returned %s", OTHERPAD, s);
189   g_free (s);
190
191   if (caps == NULL)
192     caps = gst_caps_new_any ();
193
194   gst_object_unref (capsdebug);
195
196   return caps;
197 }
198
199
200 static gboolean
201 gst_caps_debug_acceptcaps (GstPad * pad, GstCaps * caps)
202 {
203   GstCapsDebug *capsdebug;
204   gchar *s;
205   gboolean ret;
206   GstPad *otherpad;
207
208   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
209   otherpad =
210       (pad == capsdebug->srcpad) ? capsdebug->sinkpad : capsdebug->srcpad;
211
212   s = gst_caps_to_string (caps);
213   GST_INFO ("%s called acceptcaps with %s", THISPAD, s);
214   g_free (s);
215
216   ret = gst_pad_peer_accept_caps (otherpad, caps);
217
218   GST_INFO ("%s returned %s", OTHERPAD, ret ? "TRUE" : "FALSE");
219
220   gst_object_unref (capsdebug);
221
222   return ret;
223 }
224
225 static GstFlowReturn
226 gst_caps_debug_bufferalloc (GstPad * pad, guint64 offset, guint size,
227     GstCaps * caps, GstBuffer ** buf)
228 {
229   GstCapsDebug *capsdebug;
230   gchar *s;
231   gchar *t;
232   GstFlowReturn ret;
233   GstPad *otherpad;
234   gboolean newcaps;
235
236   capsdebug = GST_CAPS_DEBUG (gst_pad_get_parent (pad));
237   otherpad =
238       (pad == capsdebug->srcpad) ? capsdebug->sinkpad : capsdebug->srcpad;
239
240   newcaps = (caps != GST_PAD_CAPS (pad));
241
242   if (newcaps) {
243     s = gst_caps_to_string (caps);
244     GST_INFO ("%s called bufferalloc with new caps, offset=%" G_GUINT64_FORMAT
245         " size=%d caps=%s", THISPAD, offset, size, s);
246     g_free (s);
247   }
248
249   ret = gst_pad_alloc_buffer_and_set_caps (otherpad, offset, size, caps, buf);
250
251   if (newcaps) {
252     GST_INFO ("%s returned %s", OTHERPAD, gst_flow_get_name (ret));
253   }
254   if (caps != GST_BUFFER_CAPS (*buf)) {
255     s = gst_caps_to_string (caps);
256     t = gst_caps_to_string (GST_BUFFER_CAPS (*buf));
257     GST_INFO
258         ("%s returned from bufferalloc with different caps, requested=%s returned=%s",
259         OTHERPAD, s, t);
260     g_free (s);
261     g_free (t);
262   }
263
264   gst_object_unref (capsdebug);
265
266   return ret;
267 }