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"
31 static GMemChunk *_gst_caps_chunk;
32 static GMutex *_gst_caps_chunk_lock;
35 _gst_caps_initialize (void)
37 _gst_caps_chunk = g_mem_chunk_new ("GstCaps",
38 sizeof (GstCaps), sizeof (GstCaps) * 256,
40 _gst_caps_chunk_lock = g_mutex_new ();
44 get_type_for_mime (const gchar *mime)
48 typeid = gst_type_find_by_mime (mime);
50 GstTypeFactory factory; // = g_new0 (GstTypeFactory, 1);
52 factory.mime = g_strdup (mime);
54 factory.typefindfunc = NULL;
56 typeid = gst_type_register (&factory);
63 * @name: the name of this capability
64 * @mime: the mime type to attach to the capability
66 * Create a new capability with the given mime type.
68 * Returns: a new capability
71 gst_caps_new (const gchar *name, const gchar *mime)
75 g_return_val_if_fail (mime != NULL, NULL);
77 g_mutex_lock (_gst_caps_chunk_lock);
78 caps = g_mem_chunk_alloc (_gst_caps_chunk);
79 g_mutex_unlock (_gst_caps_chunk_lock);
81 caps->name = g_strdup (name);
82 caps->id = get_type_for_mime (mime);
83 caps->properties = NULL;
91 * gst_caps_new_with_props:
92 * @name: the name of this capability
93 * @mime: the mime type to attach to the capability
94 * @props: the properties for this capability
96 * Create a new capability with the given mime type and the given properties.
98 * Returns: a new capability
101 gst_caps_new_with_props (const gchar *name, const gchar *mime, GstProps *props)
105 caps = gst_caps_new (name, mime);
106 caps->properties = props;
113 * @factory: the factory to register
115 * Register the factory.
117 * Returns: the registered capability
120 gst_caps_register (GstCapsFactory *factory)
124 return gst_caps_register_count (factory, &dummy);
128 * gst_caps_register_count:
129 * @factory: the factory to register
130 * @counter: count how many entries were consumed
132 * Register the factory.
134 * Returns: the registered capability
137 gst_caps_register_count (GstCapsFactory *factory, guint *counter)
139 GstCapsFactoryEntry tag;
144 g_return_val_if_fail (factory != NULL, NULL);
146 tag = (*factory)[i++];
147 g_return_val_if_fail (tag != NULL, NULL);
151 tag = (*factory)[i++];
152 g_return_val_if_fail (tag != NULL, NULL);
154 caps = gst_caps_new_with_props (name, (gchar *)tag,
155 gst_props_register_count (&(*factory)[i], counter));
164 * @caps: the caps to destroy
166 * Frees the memory used by this caps structure and all
167 * the chained caps and properties.
170 gst_caps_destroy (GstCaps *caps)
172 g_return_if_fail (caps != NULL);
175 gst_caps_unref (caps->next);
183 * @caps: the caps to unref
185 * Decrease the refcount of this caps structure,
186 * destroying it when the refcount is 0
189 gst_caps_unref (GstCaps *caps)
191 g_return_if_fail (caps != NULL);
196 gst_caps_unref (caps->next);
198 if (caps->refcount == 0)
199 gst_caps_destroy (caps);
204 * @caps: the caps to ref
206 * Increase the refcount of this caps structure
209 gst_caps_ref (GstCaps *caps)
211 g_return_if_fail (caps != NULL);
218 * @caps: the caps to copy
222 * Returns: a copy of the GstCaps structure.
225 gst_caps_copy (GstCaps *caps)
227 GstCaps *new = caps;;
229 g_return_val_if_fail (caps != NULL, NULL);
231 new = gst_caps_new_with_props (
233 (gst_type_find_by_id (caps->id))->mime,
234 gst_props_copy (caps->properties));
240 * gst_caps_copy_on_write:
241 * @caps: the caps to copy
243 * Copies the caps if the refcount is greater than 1
245 * Returns: a pointer to a GstCaps strcuture that can
246 * be safely written to
249 gst_caps_copy_on_write (GstCaps *caps)
251 GstCaps *new = caps;;
253 g_return_val_if_fail (caps != NULL, NULL);
255 if (caps->refcount > 1) {
256 new = gst_caps_copy (caps);
257 gst_caps_unref (caps);
265 * @caps: the caps to get the name from
267 * Get the name of a GstCaps structure.
269 * Returns: the name of the caps
272 gst_caps_get_name (GstCaps *caps)
274 g_return_val_if_fail (caps != NULL, NULL);
276 return (const gchar *)caps->name;
281 * @caps: the caps to set the name to
282 * @name: the name to set
284 * Set the name of a caps.
287 gst_caps_set_name (GstCaps *caps, const gchar *name)
289 g_return_if_fail (caps != NULL);
294 caps->name = g_strdup (name);
299 * @caps: the caps to get the mime type from
301 * Get the mime type of the caps as a string.
303 * Returns: the mime type of the caps
306 gst_caps_get_mime (GstCaps *caps)
310 g_return_val_if_fail (caps != NULL, NULL);
312 type = gst_type_find_by_id (caps->id);
317 return "unknown/unknown";
322 * @caps: the caps to set the mime type to
323 * @mime: the mime type to attach to the caps
325 * Set the mime type of the caps as a string.
328 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
330 g_return_if_fail (caps != NULL);
331 g_return_if_fail (mime != NULL);
333 caps->id = get_type_for_mime (mime);
337 * gst_caps_get_type_id:
338 * @caps: the caps to get the type id from
340 * Get the type id of the caps.
342 * Returns: the type id of the caps
345 gst_caps_get_type_id (GstCaps *caps)
347 g_return_val_if_fail (caps != NULL, 0);
353 * gst_caps_set_type_id:
354 * @caps: the caps to set the type id to
355 * @type_id: the type id to set
357 * Set the type id of the caps.
360 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
362 g_return_if_fail (caps != NULL);
368 * gst_caps_set_props:
369 * @caps: the caps to attach the properties to
370 * @props: the properties to attach
372 * Set the properties to the given caps.
374 * Returns: the new caps structure
377 gst_caps_set_props (GstCaps *caps, GstProps *props)
379 g_return_val_if_fail (caps != NULL, caps);
380 g_return_val_if_fail (props != NULL, caps);
381 g_return_val_if_fail (caps->properties == NULL, caps);
383 caps->properties = props;
389 * gst_caps_get_props:
390 * @caps: the caps to get the properties from
392 * Get the properties of the given caps.
394 * Returns: the properties of the caps
397 gst_caps_get_props (GstCaps *caps)
399 g_return_val_if_fail (caps != NULL, NULL);
401 return caps->properties;
407 * @capstoadd: the capability to append
409 * Appends a capability to the existing capability.
411 * Returns: the new capability
414 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
416 GstCaps *orig = caps;
424 caps->next = capstoadd;
432 * @capstoadd: a capabilty to prepend
434 * prepend the capability to the list of capabilities
436 * Returns: the new capability
439 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
441 GstCaps *orig = capstoadd;
443 if (capstoadd == NULL)
446 while (capstoadd->next) {
447 capstoadd = capstoadd->next;
449 capstoadd->next = caps;
455 * gst_caps_get_by_name:
457 * @name: the name of the capability to get
459 * Get the capability with the given name from this
460 * chain of capabilities.
462 * Returns: the first capability in the chain with the
466 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
468 g_return_val_if_fail (caps != NULL, NULL);
469 g_return_val_if_fail (name != NULL, NULL);
472 if (!strcmp (caps->name, name))
481 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
483 if (fromcaps->id != tocaps->id) {
484 GST_DEBUG (0,"gstcaps: mime types differ (%d to %d)\n",
485 fromcaps->id, tocaps->id);
489 if (tocaps->properties) {
490 if (fromcaps->properties) {
491 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
494 GST_DEBUG (0,"gstcaps: no source caps\n");
499 // assume it accepts everything
500 GST_DEBUG (0,"gstcaps: no caps\n");
506 * gst_caps_check_compatibility:
507 * @fromcaps: a capabilty
508 * @tocaps: a capabilty
510 * Checks whether two capabilities are compatible.
512 * Returns: TRUE if compatible, FALSE otherwise
515 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
517 if (fromcaps == NULL) {
518 if (tocaps == NULL) {
519 GST_DEBUG (0,"gstcaps: no caps\n");
523 GST_DEBUG (0,"gstcaps: no src but destination caps\n");
528 if (tocaps == NULL) {
529 GST_DEBUG (0,"gstcaps: src caps and no dest caps\n");
535 GstCaps *destcaps = tocaps;
538 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
541 destcaps = destcaps->next;
543 fromcaps = fromcaps->next;
549 * gst_caps_save_thyself:
550 * @caps: a capabilty to save
551 * @parent: the parent XML node pointer
553 * Save the capability into an XML representation.
555 * Returns: a new XML node pointer
558 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
561 xmlNodePtr subsubtree;
564 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
566 xmlNewChild (subtree, NULL, "name", caps->name);
567 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
568 if (caps->properties) {
569 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
571 gst_props_save_thyself (caps->properties, subsubtree);
581 * gst_caps_load_thyself:
582 * @parent: the parent XML node pointer
584 * Load a new caps from the XML representation.
586 * Returns: a new capability
589 gst_caps_load_thyself (xmlNodePtr parent)
591 GstCaps *result = NULL;
592 xmlNodePtr field = parent->xmlChildrenNode;
595 if (!strcmp (field->name, "capscomp")) {
596 xmlNodePtr subfield = field->xmlChildrenNode;
600 g_mutex_lock (_gst_caps_chunk_lock);
601 caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
602 g_mutex_unlock (_gst_caps_chunk_lock);
607 if (!strcmp (subfield->name, "name")) {
608 caps->name = xmlNodeGetContent (subfield);
610 if (!strcmp (subfield->name, "type")) {
611 content = xmlNodeGetContent (subfield);
612 caps->id = get_type_for_mime (content);
615 else if (!strcmp (subfield->name, "properties")) {
616 caps->properties = gst_props_load_thyself (subfield);
619 subfield = subfield->next;
621 result = gst_caps_append (result, caps);