The typefind function now returns a GstCaps structure instead of a gboolean. modified...
[platform/upstream/gstreamer.git] / gst / gstcaps.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 //#define DEBUG_ENABLED
21
22 #include "gstcaps.h"
23 #include "gsttype.h"
24
25 #include "gstpropsprivate.h"
26 void 
27 _gst_caps_initialize (void) 
28 {
29 }
30
31 static guint16
32 get_type_for_mime (gchar *mime)
33 {
34   guint16 typeid;
35
36   typeid = gst_type_find_by_mime (mime);
37   if (typeid == 0) {
38      GstTypeFactory *factory = g_new0 (GstTypeFactory, 1);
39
40      factory->mime = g_strdup (mime);
41      factory->exts = NULL;
42      factory->typefindfunc = NULL;
43
44      typeid = gst_type_register (factory);
45   }
46   return typeid;
47 }
48
49 /**
50  * gst_caps_new:
51  * @mime: the mime type to attach to the capability
52  *
53  * create a new capability with the given mime type
54  *
55  * Returns: a new capability
56  */
57 GstCaps*
58 gst_caps_new (gchar *mime)
59 {
60   GstCaps *caps;
61
62   g_return_val_if_fail (mime != NULL, NULL);
63   
64   caps = g_new0 (GstCaps, 1);
65   caps->id = get_type_for_mime (mime);
66   caps->properties = NULL;
67   
68   return caps;
69 }
70
71 /**
72  * gst_caps_register:
73  * @factory: the factory to register
74  *
75  * Register the factory. 
76  *
77  * Returns: The registered capability
78  */
79 GstCaps*
80 gst_caps_register (GstCapsFactory *factory)
81 {
82   GstCapsFactoryEntry tag;
83   gint i = 0;
84   guint16 typeid;
85   GstCaps *caps;
86   
87   g_return_val_if_fail (factory != NULL, NULL);
88
89   tag = (*factory)[i++];
90
91   g_return_val_if_fail (tag != NULL, NULL);
92   
93   typeid = get_type_for_mime ((gchar *)tag);
94
95   caps = g_new0 (GstCaps, 1);
96   g_return_val_if_fail (caps != NULL, NULL);
97
98   caps->id = typeid;
99   caps->properties = gst_props_register (&(*factory)[i]);
100
101   return caps;
102 }
103
104
105 /**
106  * gst_caps_check_compatibility:
107  * @fromcaps: a capabilty
108  * @tocaps: a capabilty
109  *
110  * Checks whether two capabilities are compatible
111  *
112  * Returns: true if compatible, false otherwise
113  */
114 gboolean
115 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
116 {
117   g_return_val_if_fail (fromcaps != NULL, FALSE);
118   g_return_val_if_fail (tocaps != NULL, FALSE);
119         
120   if (fromcaps->id != tocaps->id)
121     return FALSE;
122
123   if (tocaps->properties) {
124     GstPropsEntry *entry = (GstPropsEntry *)tocaps->properties;
125
126     if (fromcaps->properties) {
127       return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
128     }
129     else {
130       g_print ("gstcaps: no source caps\n");
131       return FALSE;
132     }
133   }
134   else {
135     // assume it accepts everything
136     g_print ("gstcaps: no caps\n");
137     return TRUE;
138   }
139 }
140
141
142 xmlNodePtr      
143 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
144 {
145   xmlNodePtr subtree;
146
147   g_return_val_if_fail (caps != NULL, NULL);
148
149   xmlNewChild (parent, NULL, "type", gst_type_find_by_id (caps->id)->mime);
150   if (caps->properties) {
151     subtree = xmlNewChild (parent, NULL, "properties", NULL);
152
153     gst_props_save_thyself (caps->properties, subtree);
154   }
155
156   return parent;
157 }
158
159 GstCaps*        
160 gst_caps_load_thyself (xmlNodePtr parent)
161 {
162   GstCaps *caps = g_new0 (GstCaps, 1);
163   xmlNodePtr field = parent->childs;
164
165   while (field) {
166     if (!strcmp (field->name, "type")) {
167       caps->id = get_type_for_mime (xmlNodeGetContent (field));
168     }
169     else if (!strcmp (field->name, "properties")) {
170       caps->properties = gst_props_load_thyself (field);
171     }
172     field = field->next;
173   }
174
175   return caps;
176 }
177
178
179