b2de7d573f3bf30ae06db78ad44846693e572973
[platform/upstream/gstreamer.git] / gst / debug / negotiation.c
1 /*
2  * GStreamer
3  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
4  * Copyright (C) 2002 David A. Schleef <ds@schleef.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gst.h>
26
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30
31
32 #define GST_TYPE_NEGOTIATION \
33   (gst_gst_negotiation_get_type())
34 #define GST_NEGOTIATION(obj) \
35   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_NEGOTIATION,GstNegotiation))
36 #define GST_NEGOTIATION_CLASS(klass) \
37   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_NEGOTIATION,GstNegotiation))
38 #define GST_IS_NEGOTIATION(obj) \
39   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_NEGOTIATION))
40 #define GST_IS_NEGOTIATION_CLASS(klass) \
41   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_NEGOTIATION))
42
43 typedef struct _GstNegotiation GstNegotiation;
44 typedef struct _GstNegotiationClass GstNegotiationClass;
45
46 struct _GstNegotiation
47 {
48   GstElement element;
49
50   GstPad *sinkpad, *srcpad;
51
52   GstCaps *caps;
53 };
54
55 struct _GstNegotiationClass
56 {
57   GstElementClass parent_class;
58 };
59
60 GType gst_gst_negotiation_get_type (void);
61
62
63 static const GstElementDetails plugin_details =
64 GST_ELEMENT_DETAILS ("Negotiation",
65     "Testing",
66     "This element acts like identity, except that one can control how "
67     "negotiation works",
68     "David A. Schleef <ds@schleef.org>");
69
70 /* Filter signals and args */
71 enum
72 {
73   /* FILL ME */
74   LAST_SIGNAL
75 };
76
77 enum
78 {
79   ARG_0,
80   ARG_ALLOWED_CAPS
81 };
82
83 static GstStaticPadTemplate gst_negotiation_sink_factory =
84 GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS_ANY);
88
89 static GstStaticPadTemplate gst_negotiation_src_factory =
90 GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS_ANY);
94
95 static void gst_negotiation_base_init (gpointer g_class);
96 static void gst_negotiation_class_init (GstNegotiationClass * klass);
97 static void gst_negotiation_init (GstNegotiation * filter);
98
99 static GstCaps *gst_negotiation_getcaps (GstPad * pad);
100 static GstPadLinkReturn gst_negotiation_pad_link (GstPad * pad,
101     const GstCaps * caps);
102
103 static void gst_negotiation_set_property (GObject * object, guint prop_id,
104     const GValue * value, GParamSpec * pspec);
105 static void gst_negotiation_get_property (GObject * object, guint prop_id,
106     GValue * value, GParamSpec * pspec);
107
108 static void gst_negotiation_update_caps (GstNegotiation * negotiation);
109 static void gst_negotiation_chain (GstPad * pad, GstData * _data);
110
111 static GstElementClass *parent_class = NULL;
112
113 GType
114 gst_gst_negotiation_get_type (void)
115 {
116   static GType plugin_type = 0;
117
118   if (!plugin_type) {
119     static const GTypeInfo plugin_info = {
120       sizeof (GstNegotiationClass),
121       gst_negotiation_base_init,
122       NULL,
123       (GClassInitFunc) gst_negotiation_class_init,
124       NULL,
125       NULL,
126       sizeof (GstNegotiation),
127       0,
128       (GInstanceInitFunc) gst_negotiation_init,
129     };
130
131     plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
132         "GstNegotiation", &plugin_info, 0);
133   }
134   return plugin_type;
135 }
136
137 static void
138 gst_negotiation_base_init (gpointer g_class)
139 {
140   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
141
142   gst_element_class_add_pad_template (element_class,
143       gst_static_pad_template_get (&gst_negotiation_sink_factory));
144   gst_element_class_add_pad_template (element_class,
145       gst_static_pad_template_get (&gst_negotiation_src_factory));
146   gst_element_class_set_details (element_class, &plugin_details);
147 }
148
149 static void
150 gst_negotiation_class_init (GstNegotiationClass * klass)
151 {
152   GObjectClass *gobject_class;
153   GstElementClass *gstelement_class;
154
155   gobject_class = (GObjectClass *) klass;
156   gstelement_class = (GstElementClass *) klass;
157
158   parent_class = g_type_class_peek_parent (klass);
159
160   gobject_class->set_property = gst_negotiation_set_property;
161   gobject_class->get_property = gst_negotiation_get_property;
162
163   g_object_class_install_property (gobject_class, ARG_ALLOWED_CAPS,
164       g_param_spec_boxed ("allowed-caps", "Caps",
165           "The range of formats allowed by " "this element's peers",
166           GST_TYPE_CAPS, G_PARAM_READABLE));
167 }
168
169 static void
170 gst_negotiation_init (GstNegotiation * filter)
171 {
172   filter->sinkpad =
173       gst_pad_new_from_static_template (&gst_negotiation_sink_factory, "sink");
174   gst_pad_set_getcaps_function (filter->sinkpad, gst_negotiation_getcaps);
175   gst_pad_set_link_function (filter->sinkpad, gst_negotiation_pad_link);
176   filter->srcpad =
177       gst_pad_new_from_static_template (&gst_negotiation_src_factory, "src");
178   gst_pad_set_getcaps_function (filter->srcpad, gst_negotiation_getcaps);
179   gst_pad_set_link_function (filter->srcpad, gst_negotiation_pad_link);
180
181   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
182   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
183   gst_pad_set_chain_function (filter->sinkpad, gst_negotiation_chain);
184 }
185
186 static GstCaps *
187 gst_negotiation_getcaps (GstPad * pad)
188 {
189   GstNegotiation *negotiation = GST_NEGOTIATION (gst_pad_get_parent (pad));
190   GstPad *otherpad;
191   GstCaps *caps;
192
193   otherpad = (pad == negotiation->sinkpad) ? negotiation->srcpad :
194       negotiation->sinkpad;
195
196   caps = gst_pad_get_allowed_caps (otherpad);
197
198   GST_ERROR ("getcaps called on %" GST_PTR_FORMAT ", returning %"
199       GST_PTR_FORMAT, pad, caps);
200
201   gst_negotiation_update_caps (negotiation);
202   gst_object_unref (negotiation);
203
204   return caps;
205 }
206
207 static GstPadLinkReturn
208 gst_negotiation_pad_link (GstPad * pad, const GstCaps * caps)
209 {
210   GstNegotiation *negotiation = GST_NEGOTIATION (gst_pad_get_parent (pad));
211   GstPad *otherpad;
212   GstPadLinkReturn ret;
213
214   otherpad = (pad == negotiation->sinkpad) ? negotiation->srcpad :
215       negotiation->sinkpad;
216
217   ret = gst_pad_try_set_caps (otherpad, caps);
218
219   GST_ERROR ("pad_link called on %" GST_PTR_FORMAT " with caps %"
220       GST_PTR_FORMAT ", returning %d", pad, caps, ret);
221   gst_object_unref (negotiation);
222
223   return ret;
224 }
225
226 static void
227 gst_negotiation_update_caps (GstNegotiation * negotiation)
228 {
229   GstCaps *srccaps;
230   GstCaps *sinkcaps;
231   GstCaps *icaps;
232
233   srccaps = gst_pad_get_allowed_caps (negotiation->srcpad);
234   sinkcaps = gst_pad_get_allowed_caps (negotiation->sinkpad);
235
236   icaps = gst_caps_intersect (srccaps, sinkcaps);
237   gst_caps_free (srccaps);
238   gst_caps_free (sinkcaps);
239
240   gst_caps_replace (&negotiation->caps, icaps);
241   g_object_notify (G_OBJECT (negotiation), "allowed-caps");
242   GST_DEBUG ("notify %" GST_PTR_FORMAT, icaps);
243 }
244
245 static void
246 gst_negotiation_chain (GstPad * pad, GstData * _data)
247 {
248   GstNegotiation *negotiation = GST_NEGOTIATION (gst_pad_get_parent (pad));
249
250   gst_pad_push (negotiation->srcpad, _data);
251   gst_object_unref (negotiation);
252 }
253
254 static void
255 gst_negotiation_set_property (GObject * object, guint prop_id,
256     const GValue * value, GParamSpec * pspec)
257 {
258   GstNegotiation *filter;
259
260   g_return_if_fail (GST_IS_NEGOTIATION (object));
261   filter = GST_NEGOTIATION (object);
262
263   switch (prop_id) {
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266       break;
267   }
268 }
269
270 static void
271 gst_negotiation_get_property (GObject * object, guint prop_id,
272     GValue * value, GParamSpec * pspec)
273 {
274   GstNegotiation *filter;
275
276   g_return_if_fail (GST_IS_NEGOTIATION (object));
277   filter = GST_NEGOTIATION (object);
278
279   switch (prop_id) {
280     case ARG_ALLOWED_CAPS:
281       g_value_set_boxed (value, filter->caps);
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285       break;
286   }
287 }
288
289 gboolean
290 gst_negotiation_plugin_init (GstPlugin * plugin)
291 {
292   if (!gst_element_register (plugin, "negotiation", GST_RANK_NONE,
293           GST_TYPE_NEGOTIATION))
294     return FALSE;
295
296   return TRUE;
297 }