Updated copyright in all the libgst files.
[platform/upstream/gstreamer.git] / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstcaps.c: Element capabilities subsystem
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 //#define GST_DEBUG_ENABLED
24 #include "gst_private.h"
25
26 #include "gstcaps.h"
27 #include "gsttype.h"
28
29 #include "gstpropsprivate.h"
30
31
32 void 
33 _gst_caps_initialize (void) 
34 {
35 }
36
37 static guint16
38 get_type_for_mime (gchar *mime)
39 {
40   guint16 typeid;
41
42   typeid = gst_type_find_by_mime (mime);
43   if (typeid == 0) {
44      GstTypeFactory *factory = g_new0 (GstTypeFactory, 1);
45
46      factory->mime = g_strdup (mime);
47      factory->exts = NULL;
48      factory->typefindfunc = NULL;
49
50      typeid = gst_type_register (factory);
51   }
52   return typeid;
53 }
54
55 /**
56  * gst_caps_new:
57  * @mime: the mime type to attach to the capability
58  *
59  * create a new capability with the given mime type
60  *
61  * Returns: a new capability
62  */
63 GstCaps*
64 gst_caps_new (gchar *mime)
65 {
66   GstCaps *caps;
67
68   g_return_val_if_fail (mime != NULL, NULL);
69   
70   caps = g_new0 (GstCaps, 1);
71   caps->id = get_type_for_mime (mime);
72   caps->properties = NULL;
73   
74   return caps;
75 }
76
77 /**
78  * gst_caps_new_with_props:
79  * @mime: the mime type to attach to the capability
80  * @props: the properties for this capability
81  *
82  * create a new capability with the given mime type
83  * and the given properties
84  *
85  * Returns: a new capability
86  */
87 GstCaps*
88 gst_caps_new_with_props (gchar *mime, GstProps *props)
89 {
90   GstCaps *caps;
91   
92   caps = gst_caps_new (mime);
93   caps->properties = props;
94
95   return caps;
96 }
97
98 /**
99  * gst_caps_register:
100  * @factory: the factory to register
101  *
102  * Register the factory. 
103  *
104  * Returns: The registered capability
105  */
106 GstCaps*
107 gst_caps_register (GstCapsFactory *factory)
108 {
109   GstCapsFactoryEntry tag;
110   gint i = 0;
111   guint16 typeid;
112   GstCaps *caps;
113   
114   g_return_val_if_fail (factory != NULL, NULL);
115
116   tag = (*factory)[i++];
117
118   g_return_val_if_fail (tag != NULL, NULL);
119   
120   typeid = get_type_for_mime ((gchar *)tag);
121
122   caps = g_new0 (GstCaps, 1);
123   g_return_val_if_fail (caps != NULL, NULL);
124
125   caps->id = typeid;
126   caps->properties = gst_props_register (&(*factory)[i]);
127
128   return caps;
129 }
130
131 /**
132  * gst_caps_set_props:
133  * @caps: the caps to attach the properties to
134  * @props: the properties to attach
135  *
136  * set the properties to the given caps
137  *
138  * Returns: The new caps structure
139  */
140 GstCaps*
141 gst_caps_set_props (GstCaps *caps, GstProps *props)
142 {
143   g_return_val_if_fail (caps != NULL, caps);
144   g_return_val_if_fail (props != NULL, caps);
145   g_return_val_if_fail (caps->properties == NULL, caps);
146
147   caps->properties = props;
148   
149   return caps;
150 }
151
152 /**
153  * gst_caps_get_props:
154  * @caps: the caps to get the properties from
155  *
156  * get the properties of the given caps
157  *
158  * Returns: The properties of the caps
159  */
160 GstProps*
161 gst_caps_get_props (GstCaps *caps)
162 {
163   g_return_val_if_fail (caps != NULL, caps);
164
165   return caps->properties;
166 }
167
168 /**
169  * gst_caps_check_compatibility:
170  * @fromcaps: a capabilty
171  * @tocaps: a capabilty
172  *
173  * Checks whether two capabilities are compatible
174  *
175  * Returns: true if compatible, false otherwise
176  */
177 gboolean
178 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
179 {
180   g_return_val_if_fail (fromcaps != NULL, FALSE);
181   g_return_val_if_fail (tocaps != NULL, FALSE);
182         
183   if (fromcaps->id != tocaps->id) {
184     DEBUG ("gstcaps: mime types wrong\n");
185     return FALSE;
186   }
187
188   if (tocaps->properties) {
189     if (fromcaps->properties) {
190       return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
191     }
192     else {
193       DEBUG ("gstcaps: no source caps\n");
194       return FALSE;
195     }
196   }
197   else {
198     // assume it accepts everything
199     DEBUG ("gstcaps: no caps\n");
200     return TRUE;
201   }
202 }
203
204
205 /**
206  * gst_caps_save_thyself:
207  * @caps: a capabilty to save
208  * @parent: the parent XML node pointer
209  *
210  * save the capability into an XML representation
211  *
212  * Returns: a new XML node pointer
213  */
214 xmlNodePtr      
215 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
216 {
217   xmlNodePtr subtree;
218
219   g_return_val_if_fail (caps != NULL, NULL);
220
221   xmlNewChild (parent, NULL, "type", gst_type_find_by_id (caps->id)->mime);
222   if (caps->properties) {
223     subtree = xmlNewChild (parent, NULL, "properties", NULL);
224
225     gst_props_save_thyself (caps->properties, subtree);
226   }
227
228   return parent;
229 }
230
231 /**
232  * gst_caps_load_thyself:
233  * @parent: the parent XML node pointer
234  *
235  * load a new caps from the XML representation
236  *
237  * Returns: a new capability
238  */
239 GstCaps*        
240 gst_caps_load_thyself (xmlNodePtr parent)
241 {
242   GstCaps *caps = g_new0 (GstCaps, 1);
243   xmlNodePtr field = parent->childs;
244
245   while (field) {
246     if (!strcmp (field->name, "type")) {
247       caps->id = get_type_for_mime (xmlNodeGetContent (field));
248     }
249     else if (!strcmp (field->name, "properties")) {
250       caps->properties = gst_props_load_thyself (field);
251     }
252     field = field->next;
253   }
254
255   return caps;
256 }
257
258
259