2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstcaps.c: Element capabilities subsystem
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.
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.
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.
23 //#define GST_DEBUG_ENABLED
24 #include "gst_private.h"
29 #include "gstpropsprivate.h"
33 _gst_caps_initialize (void)
38 get_type_for_mime (const gchar *mime)
42 typeid = gst_type_find_by_mime (mime);
44 GstTypeFactory factory; // = g_new0 (GstTypeFactory, 1);
46 factory.mime = g_strdup (mime);
48 factory.typefindfunc = NULL;
50 typeid = gst_type_register (&factory);
57 * @name: the name of this capability
58 * @mime: the mime type to attach to the capability
60 * Create a new capability with the given mime type.
62 * Returns: a new capability
65 gst_caps_new (const gchar *name, const gchar *mime)
69 g_return_val_if_fail (mime != NULL, NULL);
71 caps = g_new0 (GstCaps, 1);
72 caps->name = g_strdup (name);
73 caps->id = get_type_for_mime (mime);
74 caps->properties = NULL;
81 * gst_caps_new_with_props:
82 * @name: the name of this capability
83 * @mime: the mime type to attach to the capability
84 * @props: the properties for this capability
86 * Create a new capability with the given mime type and the given properties.
88 * Returns: a new capability
91 gst_caps_new_with_props (const gchar *name, const gchar *mime, GstProps *props)
95 caps = gst_caps_new (name, mime);
96 caps->properties = props;
103 * @factory: the factory to register
105 * Register the factory.
107 * Returns: the registered capability
110 gst_caps_register (GstCapsFactory *factory)
114 return gst_caps_register_count (factory, &dummy);
118 * gst_caps_register_count:
119 * @factory: the factory to register
120 * @counter: count how many entries were consumed
122 * Register the factory.
124 * Returns: the registered capability
127 gst_caps_register_count (GstCapsFactory *factory, guint *counter)
129 GstCapsFactoryEntry tag;
135 g_return_val_if_fail (factory != NULL, NULL);
137 tag = (*factory)[i++];
138 g_return_val_if_fail (tag != NULL, NULL);
142 tag = (*factory)[i++];
143 g_return_val_if_fail (tag != NULL, NULL);
145 typeid = get_type_for_mime ((gchar *)tag);
147 caps = g_new0 (GstCaps, 1);
148 g_return_val_if_fail (caps != NULL, NULL);
150 caps->name = g_strdup (name);
152 caps->properties = gst_props_register_count (&(*factory)[i], counter);
161 * @caps: the caps to get the name from
163 * Get the name of a GstCaps structure.
165 * Returns: the name of the caps
168 gst_caps_get_name (GstCaps *caps)
170 g_return_val_if_fail (caps != NULL, NULL);
172 return (const gchar *)caps->name;
177 * @caps: the caps to set the name to
178 * @name: the name to set
180 * Set the name of a caps.
183 gst_caps_set_name (GstCaps *caps, const gchar *name)
185 g_return_if_fail (caps != NULL);
190 caps->name = g_strdup (name);
195 * @caps: the caps to get the mime type from
197 * Get the mime type of the caps as a string.
199 * Returns: the mime type of the caps
202 gst_caps_get_mime (GstCaps *caps)
206 g_return_val_if_fail (caps != NULL, NULL);
208 type = gst_type_find_by_id (caps->id);
213 return "unknown/unknown";
218 * @caps: the caps to set the mime type to
219 * @mime: the mime type to attach to the caps
221 * Set the mime type of the caps as a string.
224 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
226 g_return_if_fail (caps != NULL);
227 g_return_if_fail (mime != NULL);
229 caps->id = get_type_for_mime (mime);
233 * gst_caps_get_type_id:
234 * @caps: the caps to get the type id from
236 * Get the type id of the caps.
238 * Returns: the type id of the caps
241 gst_caps_get_type_id (GstCaps *caps)
243 g_return_val_if_fail (caps != NULL, 0);
249 * gst_caps_set_type_id:
250 * @caps: the caps to set the type id to
251 * @typeid: the type id to set
253 * Set the type id of the caps.
256 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
258 g_return_if_fail (caps != NULL);
264 * gst_caps_set_props:
265 * @caps: the caps to attach the properties to
266 * @props: the properties to attach
268 * Set the properties to the given caps.
270 * Returns: the new caps structure
273 gst_caps_set_props (GstCaps *caps, GstProps *props)
275 g_return_val_if_fail (caps != NULL, caps);
276 g_return_val_if_fail (props != NULL, caps);
277 g_return_val_if_fail (caps->properties == NULL, caps);
279 caps->properties = props;
285 * gst_caps_get_props:
286 * @caps: the caps to get the properties from
288 * Get the properties of the given caps.
290 * Returns: the properties of the caps
293 gst_caps_get_props (GstCaps *caps)
295 g_return_val_if_fail (caps != NULL, NULL);
297 return caps->properties;
303 * @capstoadd: the capability to append
305 * Appends a capability to the existing capability.
307 * Returns: the new capability
310 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
312 GstCaps *orig = caps;
320 caps->next = capstoadd;
328 * @capstoadd: a capabilty to prepend
330 * prepend the capability to the list of capabilities
332 * Returns: the new capability
335 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
337 GstCaps *orig = capstoadd;
339 if (capstoadd == NULL)
342 while (capstoadd->next) {
343 capstoadd = capstoadd->next;
345 capstoadd->next = caps;
351 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
353 g_return_val_if_fail (caps != NULL, NULL);
354 g_return_val_if_fail (name != NULL, NULL);
357 if (!strcmp (caps->name, name))
366 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
368 if (fromcaps->id != tocaps->id) {
369 GST_DEBUG (0,"gstcaps: mime types differ (%d to %d)\n",
370 fromcaps->id, tocaps->id);
374 if (tocaps->properties) {
375 if (fromcaps->properties) {
376 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
379 GST_DEBUG (0,"gstcaps: no source caps\n");
384 // assume it accepts everything
385 GST_DEBUG (0,"gstcaps: no caps\n");
391 * gst_caps_list_check_compatibility:
392 * @fromcaps: a capabilty
393 * @tocaps: a capabilty
395 * Checks whether two capability lists are compatible.
397 * Returns: TRUE if compatible, FALSE otherwise
400 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
402 if (fromcaps == NULL) {
403 if (tocaps == NULL) {
404 GST_DEBUG (0,"gstcaps: no caps\n");
408 GST_DEBUG (0,"gstcaps: no src but destination caps\n");
413 if (tocaps == NULL) {
414 GST_DEBUG (0,"gstcaps: src caps and no dest caps\n");
420 GstCaps *destcaps = tocaps;
423 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
426 destcaps = destcaps->next;
428 fromcaps = fromcaps->next;
434 * gst_caps_save_thyself:
435 * @caps: a capabilty to save
436 * @parent: the parent XML node pointer
438 * Save the capability into an XML representation.
440 * Returns: a new XML node pointer
443 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
446 xmlNodePtr subsubtree;
449 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
451 xmlNewChild (subtree, NULL, "name", caps->name);
452 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
453 if (caps->properties) {
454 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
456 gst_props_save_thyself (caps->properties, subsubtree);
466 * gst_caps_load_thyself:
467 * @parent: the parent XML node pointer
469 * Load a new caps from the XML representation.
471 * Returns: a new capability
474 gst_caps_load_thyself (xmlNodePtr parent)
476 GstCaps *result = NULL;
477 xmlNodePtr field = parent->xmlChildrenNode;
480 if (!strcmp (field->name, "capscomp")) {
481 xmlNodePtr subfield = field->xmlChildrenNode;
482 GstCaps *caps = g_new0 (GstCaps, 1);
486 if (!strcmp (subfield->name, "name")) {
487 caps->name = xmlNodeGetContent (subfield);
489 if (!strcmp (subfield->name, "type")) {
490 content = xmlNodeGetContent (subfield);
491 caps->id = get_type_for_mime (content);
494 else if (!strcmp (subfield->name, "properties")) {
495 caps->properties = gst_props_load_thyself (subfield);
498 subfield = subfield->next;
500 result = gst_caps_append (result, caps);