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;
86 caps->lock = g_mutex_new ();
92 * gst_caps_new_with_props:
93 * @name: the name of this capability
94 * @mime: the mime type to attach to the capability
95 * @props: the properties for this capability
97 * Create a new capability with the given mime type and the given properties.
99 * Returns: a new capability
102 gst_caps_new_with_props (const gchar *name, const gchar *mime, GstProps *props)
106 caps = gst_caps_new (name, mime);
107 caps->properties = props;
114 * @factory: the factory to register
116 * Register the factory.
118 * Returns: the registered capability
121 gst_caps_register (GstCapsFactory *factory)
125 return gst_caps_register_count (factory, &dummy);
129 * gst_caps_register_count:
130 * @factory: the factory to register
131 * @counter: count how many entries were consumed
133 * Register the factory.
135 * Returns: the registered capability
138 gst_caps_register_count (GstCapsFactory *factory, guint *counter)
140 GstCapsFactoryEntry tag;
145 g_return_val_if_fail (factory != NULL, NULL);
147 tag = (*factory)[i++];
148 g_return_val_if_fail (tag != NULL, NULL);
152 tag = (*factory)[i++];
153 g_return_val_if_fail (tag != NULL, NULL);
155 caps = gst_caps_new_with_props (name, (gchar *)tag,
156 gst_props_register_count (&(*factory)[i], counter));
165 * @caps: the caps to destroy
167 * Frees the memory used by this caps structure and all
168 * the chained caps and properties.
171 gst_caps_destroy (GstCaps *caps)
175 g_return_if_fail (caps != NULL);
177 GST_CAPS_LOCK (caps);
181 GST_CAPS_UNLOCK (caps);
184 gst_caps_unref (next);
189 * @caps: the caps to unref
191 * Decrease the refcount of this caps structure,
192 * destroying it when the refcount is 0
195 gst_caps_unref (GstCaps *caps)
200 g_return_if_fail (caps != NULL);
202 GST_CAPS_LOCK (caps);
204 zero = (caps->refcount == 0);
206 GST_CAPS_UNLOCK (caps);
209 gst_caps_unref (next);
212 gst_caps_destroy (caps);
217 * @caps: the caps to ref
219 * Increase the refcount of this caps structure
222 gst_caps_ref (GstCaps *caps)
224 g_return_if_fail (caps != NULL);
226 GST_CAPS_LOCK (caps);
228 GST_CAPS_UNLOCK (caps);
233 * @caps: the caps to copy
237 * Returns: a copy of the GstCaps structure.
240 gst_caps_copy (GstCaps *caps)
242 GstCaps *new = caps;;
244 g_return_val_if_fail (caps != NULL, NULL);
246 GST_CAPS_LOCK (caps);
247 new = gst_caps_new_with_props (
249 (gst_type_find_by_id (caps->id))->mime,
250 gst_props_copy (caps->properties));
251 GST_CAPS_UNLOCK (caps);
257 * gst_caps_copy_on_write:
258 * @caps: the caps to copy
260 * Copies the caps if the refcount is greater than 1
262 * Returns: a pointer to a GstCaps strcuture that can
263 * be safely written to
266 gst_caps_copy_on_write (GstCaps *caps)
271 g_return_val_if_fail (caps != NULL, NULL);
273 GST_CAPS_LOCK (caps);
274 needcopy = (caps->refcount > 1);
275 GST_CAPS_UNLOCK (caps);
278 new = gst_caps_copy (caps);
279 gst_caps_unref (caps);
287 * @caps: the caps to get the name from
289 * Get the name of a GstCaps structure.
291 * Returns: the name of the caps
294 gst_caps_get_name (GstCaps *caps)
296 g_return_val_if_fail (caps != NULL, NULL);
298 return (const gchar *)caps->name;
303 * @caps: the caps to set the name to
304 * @name: the name to set
306 * Set the name of a caps.
309 gst_caps_set_name (GstCaps *caps, const gchar *name)
311 g_return_if_fail (caps != NULL);
316 caps->name = g_strdup (name);
321 * @caps: the caps to get the mime type from
323 * Get the mime type of the caps as a string.
325 * Returns: the mime type of the caps
328 gst_caps_get_mime (GstCaps *caps)
332 g_return_val_if_fail (caps != NULL, NULL);
334 type = gst_type_find_by_id (caps->id);
339 return "unknown/unknown";
344 * @caps: the caps to set the mime type to
345 * @mime: the mime type to attach to the caps
347 * Set the mime type of the caps as a string.
350 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
352 g_return_if_fail (caps != NULL);
353 g_return_if_fail (mime != NULL);
355 caps->id = get_type_for_mime (mime);
359 * gst_caps_get_type_id:
360 * @caps: the caps to get the type id from
362 * Get the type id of the caps.
364 * Returns: the type id of the caps
367 gst_caps_get_type_id (GstCaps *caps)
369 g_return_val_if_fail (caps != NULL, 0);
375 * gst_caps_set_type_id:
376 * @caps: the caps to set the type id to
377 * @type_id: the type id to set
379 * Set the type id of the caps.
382 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
384 g_return_if_fail (caps != NULL);
390 * gst_caps_set_props:
391 * @caps: the caps to attach the properties to
392 * @props: the properties to attach
394 * Set the properties to the given caps.
396 * Returns: the new caps structure
399 gst_caps_set_props (GstCaps *caps, GstProps *props)
401 g_return_val_if_fail (caps != NULL, caps);
402 g_return_val_if_fail (props != NULL, caps);
403 g_return_val_if_fail (caps->properties == NULL, caps);
405 caps->properties = props;
411 * gst_caps_get_props:
412 * @caps: the caps to get the properties from
414 * Get the properties of the given caps.
416 * Returns: the properties of the caps
419 gst_caps_get_props (GstCaps *caps)
421 g_return_val_if_fail (caps != NULL, NULL);
423 return caps->properties;
429 * @capstoadd: the capability to append
431 * Appends a capability to the existing capability.
433 * Returns: the new capability
436 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
438 GstCaps *orig = caps;
446 caps->next = capstoadd;
454 * @capstoadd: a capabilty to prepend
456 * prepend the capability to the list of capabilities
458 * Returns: the new capability
461 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
463 GstCaps *orig = capstoadd;
465 if (capstoadd == NULL)
468 while (capstoadd->next) {
469 capstoadd = capstoadd->next;
471 capstoadd->next = caps;
477 * gst_caps_get_by_name:
479 * @name: the name of the capability to get
481 * Get the capability with the given name from this
482 * chain of capabilities.
484 * Returns: the first capability in the chain with the
488 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
490 g_return_val_if_fail (caps != NULL, NULL);
491 g_return_val_if_fail (name != NULL, NULL);
494 if (!strcmp (caps->name, name))
503 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
505 if (fromcaps->id != tocaps->id) {
506 GST_DEBUG (0,"gstcaps: mime types differ (%d to %d)\n",
507 fromcaps->id, tocaps->id);
511 if (tocaps->properties) {
512 if (fromcaps->properties) {
513 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
516 GST_DEBUG (0,"gstcaps: no source caps\n");
521 // assume it accepts everything
522 GST_DEBUG (0,"gstcaps: no caps\n");
528 * gst_caps_check_compatibility:
529 * @fromcaps: a capabilty
530 * @tocaps: a capabilty
532 * Checks whether two capabilities are compatible.
534 * Returns: TRUE if compatible, FALSE otherwise
537 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
539 if (fromcaps == NULL) {
540 if (tocaps == NULL) {
541 GST_DEBUG (0,"gstcaps: no caps\n");
545 GST_DEBUG (0,"gstcaps: no src but destination caps\n");
550 if (tocaps == NULL) {
551 GST_DEBUG (0,"gstcaps: src caps and no dest caps\n");
557 GstCaps *destcaps = tocaps;
560 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
563 destcaps = destcaps->next;
565 fromcaps = fromcaps->next;
571 * gst_caps_save_thyself:
572 * @caps: a capabilty to save
573 * @parent: the parent XML node pointer
575 * Save the capability into an XML representation.
577 * Returns: a new XML node pointer
580 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
583 xmlNodePtr subsubtree;
586 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
588 xmlNewChild (subtree, NULL, "name", caps->name);
589 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
590 if (caps->properties) {
591 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
593 gst_props_save_thyself (caps->properties, subsubtree);
603 * gst_caps_load_thyself:
604 * @parent: the parent XML node pointer
606 * Load a new caps from the XML representation.
608 * Returns: a new capability
611 gst_caps_load_thyself (xmlNodePtr parent)
613 GstCaps *result = NULL;
614 xmlNodePtr field = parent->xmlChildrenNode;
617 if (!strcmp (field->name, "capscomp")) {
618 xmlNodePtr subfield = field->xmlChildrenNode;
622 g_mutex_lock (_gst_caps_chunk_lock);
623 caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
624 g_mutex_unlock (_gst_caps_chunk_lock);
627 caps->lock = g_mutex_new ();
630 if (!strcmp (subfield->name, "name")) {
631 caps->name = xmlNodeGetContent (subfield);
633 if (!strcmp (subfield->name, "type")) {
634 content = xmlNodeGetContent (subfield);
635 caps->id = get_type_for_mime (content);
638 else if (!strcmp (subfield->name, "properties")) {
639 caps->properties = gst_props_load_thyself (subfield);
642 subfield = subfield->next;
644 result = gst_caps_append (result, caps);