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 GstTypeDefinition definition;
51 GstTypeFactory *factory;
53 definition.name = "capstype";
54 definition.mime = g_strdup (mime);
55 definition.exts = NULL;
56 definition.typefindfunc = NULL;
58 factory = gst_typefactory_new (&definition);
60 typeid = gst_type_register (factory);
67 * @name: the name of this capability
68 * @mime: the mime type to attach to the capability
69 * @props: the properties to add to this capability
71 * Create a new capability with the given mime typei and properties.
73 * Returns: a new capability
76 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
80 g_return_val_if_fail (mime != NULL, NULL);
82 g_mutex_lock (_gst_caps_chunk_lock);
83 caps = g_mem_chunk_alloc (_gst_caps_chunk);
84 g_mutex_unlock (_gst_caps_chunk_lock);
86 caps->name = g_strdup (name);
87 caps->id = get_type_for_mime (mime);
88 caps->properties = props;
91 caps->lock = g_mutex_new ();
98 * @caps: the caps to destroy
100 * Frees the memory used by this caps structure and all
101 * the chained caps and properties.
104 gst_caps_destroy (GstCaps *caps)
108 g_return_if_fail (caps != NULL);
110 GST_CAPS_LOCK (caps);
114 GST_CAPS_UNLOCK (caps);
117 gst_caps_unref (next);
122 * @caps: the caps to unref
124 * Decrease the refcount of this caps structure,
125 * destroying it when the refcount is 0
127 * Returns: caps or NULL if the refcount reached 0
130 gst_caps_unref (GstCaps *caps)
135 g_return_val_if_fail (caps != NULL, NULL);
136 g_return_val_if_fail (caps->refcount > 0, NULL);
138 GST_CAPS_LOCK (caps);
140 zero = (caps->refcount == 0);
142 GST_CAPS_UNLOCK (caps);
145 *next = gst_caps_unref (*next);
148 gst_caps_destroy (caps);
156 * @caps: the caps to ref
158 * Increase the refcount of this caps structure
160 * Returns: the caps with the refcount incremented
163 gst_caps_ref (GstCaps *caps)
165 g_return_val_if_fail (caps != NULL, NULL);
167 GST_CAPS_LOCK (caps);
169 GST_CAPS_UNLOCK (caps);
176 * @caps: the caps to copy
180 * Returns: a copy of the GstCaps structure.
183 gst_caps_copy (GstCaps *caps)
185 GstCaps *new = caps;;
187 g_return_val_if_fail (caps != NULL, NULL);
189 GST_CAPS_LOCK (caps);
192 (gst_type_find_by_id (caps->id))->mime,
193 gst_props_copy (caps->properties));
194 GST_CAPS_UNLOCK (caps);
200 * gst_caps_copy_on_write:
201 * @caps: the caps to copy
203 * Copies the caps if the refcount is greater than 1
205 * Returns: a pointer to a GstCaps strcuture that can
206 * be safely written to
209 gst_caps_copy_on_write (GstCaps *caps)
214 g_return_val_if_fail (caps != NULL, NULL);
216 GST_CAPS_LOCK (caps);
217 needcopy = (caps->refcount > 1);
218 GST_CAPS_UNLOCK (caps);
221 new = gst_caps_copy (caps);
222 gst_caps_unref (caps);
230 * @caps: the caps to get the name from
232 * Get the name of a GstCaps structure.
234 * Returns: the name of the caps
237 gst_caps_get_name (GstCaps *caps)
239 g_return_val_if_fail (caps != NULL, NULL);
241 return (const gchar *)caps->name;
246 * @caps: the caps to set the name to
247 * @name: the name to set
249 * Set the name of a caps.
252 gst_caps_set_name (GstCaps *caps, const gchar *name)
254 g_return_if_fail (caps != NULL);
259 caps->name = g_strdup (name);
264 * @caps: the caps to get the mime type from
266 * Get the mime type of the caps as a string.
268 * Returns: the mime type of the caps
271 gst_caps_get_mime (GstCaps *caps)
275 g_return_val_if_fail (caps != NULL, NULL);
277 type = gst_type_find_by_id (caps->id);
282 return "unknown/unknown";
287 * @caps: the caps to set the mime type to
288 * @mime: the mime type to attach to the caps
290 * Set the mime type of the caps as a string.
293 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
295 g_return_if_fail (caps != NULL);
296 g_return_if_fail (mime != NULL);
298 caps->id = get_type_for_mime (mime);
302 * gst_caps_get_type_id:
303 * @caps: the caps to get the type id from
305 * Get the type id of the caps.
307 * Returns: the type id of the caps
310 gst_caps_get_type_id (GstCaps *caps)
312 g_return_val_if_fail (caps != NULL, 0);
318 * gst_caps_set_type_id:
319 * @caps: the caps to set the type id to
320 * @type_id: the type id to set
322 * Set the type id of the caps.
325 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
327 g_return_if_fail (caps != NULL);
333 * gst_caps_set_props:
334 * @caps: the caps to attach the properties to
335 * @props: the properties to attach
337 * Set the properties to the given caps.
339 * Returns: the new caps structure
342 gst_caps_set_props (GstCaps *caps, GstProps *props)
344 g_return_val_if_fail (caps != NULL, caps);
345 g_return_val_if_fail (props != NULL, caps);
346 g_return_val_if_fail (caps->properties == NULL, caps);
348 caps->properties = props;
354 * gst_caps_get_props:
355 * @caps: the caps to get the properties from
357 * Get the properties of the given caps.
359 * Returns: the properties of the caps
362 gst_caps_get_props (GstCaps *caps)
364 g_return_val_if_fail (caps != NULL, NULL);
366 return caps->properties;
372 * @...: more capabilities
374 * chains the given capabilities
376 * Returns: the new capability
379 gst_caps_chain (GstCaps *caps, ...)
381 GstCaps *orig = caps;
384 va_start (var_args, caps);
389 toadd = va_arg (var_args, GstCaps*);
390 gst_caps_append (caps, toadd);
402 * @capstoadd: the capability to append
404 * Appends a capability to the existing capability.
406 * Returns: the new capability
409 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
411 GstCaps *orig = caps;
413 g_return_val_if_fail (caps != capstoadd, caps);
421 caps->next = capstoadd;
429 * @capstoadd: a capabilty to prepend
431 * prepend the capability to the list of capabilities
433 * Returns: the new capability
436 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
438 GstCaps *orig = capstoadd;
440 g_return_val_if_fail (caps != capstoadd, caps);
442 if (capstoadd == NULL)
445 while (capstoadd->next) {
446 capstoadd = capstoadd->next;
448 capstoadd->next = caps;
454 * gst_caps_get_by_name:
456 * @name: the name of the capability to get
458 * Get the capability with the given name from this
459 * chain of capabilities.
461 * Returns: the first capability in the chain with the
465 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
467 g_return_val_if_fail (caps != NULL, NULL);
468 g_return_val_if_fail (name != NULL, NULL);
471 if (!strcmp (caps->name, name))
480 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
482 if (fromcaps->id != tocaps->id) {
483 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
484 gst_type_find_by_id (fromcaps->id)->mime,
485 gst_type_find_by_id (tocaps->id)->mime);
489 if (tocaps->properties) {
490 if (fromcaps->properties) {
491 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
494 GST_DEBUG (GST_CAT_CAPS,"no source caps\n");
499 /* assume it accepts everything */
500 GST_DEBUG (GST_CAT_CAPS,"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 (GST_CAT_CAPS,"no caps\n");
523 GST_DEBUG (GST_CAT_CAPS,"no source but destination caps\n");
528 if (tocaps == NULL) {
529 GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps\n");
535 GstCaps *destcaps = tocaps;
538 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
541 destcaps = destcaps->next;
543 fromcaps = fromcaps->next;
548 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
550 * gst_caps_save_thyself:
551 * @caps: a capabilty to save
552 * @parent: the parent XML node pointer
554 * Save the capability into an XML representation.
556 * Returns: a new XML node pointer
559 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
562 xmlNodePtr subsubtree;
565 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
567 xmlNewChild (subtree, NULL, "name", caps->name);
568 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
569 if (caps->properties) {
570 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
572 gst_props_save_thyself (caps->properties, subsubtree);
582 * gst_caps_load_thyself:
583 * @parent: the parent XML node pointer
585 * Load a new caps from the XML representation.
587 * Returns: a new capability
590 gst_caps_load_thyself (xmlNodePtr parent)
592 GstCaps *result = NULL;
593 xmlNodePtr field = parent->xmlChildrenNode;
596 if (!strcmp (field->name, "capscomp")) {
597 xmlNodePtr subfield = field->xmlChildrenNode;
601 g_mutex_lock (_gst_caps_chunk_lock);
602 caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
603 g_mutex_unlock (_gst_caps_chunk_lock);
606 caps->lock = g_mutex_new ();
610 if (!strcmp (subfield->name, "name")) {
611 caps->name = xmlNodeGetContent (subfield);
613 if (!strcmp (subfield->name, "type")) {
614 content = xmlNodeGetContent (subfield);
615 caps->id = get_type_for_mime (content);
618 else if (!strcmp (subfield->name, "properties")) {
619 caps->properties = gst_props_load_thyself (subfield);
622 subfield = subfield->next;
624 result = gst_caps_append (result, caps);
632 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */