Blame gst/gstmeta.c

Packit f546b1
/* GStreamer
Packit f546b1
 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
Packit f546b1
 *
Packit f546b1
 * gstmeta.c: metadata operations
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * SECTION:gstmeta
Packit f546b1
 * @title: GstMeta
Packit f546b1
 * @short_description: Buffer metadata
Packit f546b1
 *
Packit f546b1
 * The #GstMeta structure should be included as the first member of a #GstBuffer
Packit f546b1
 * metadata structure. The structure defines the API of the metadata and should
Packit f546b1
 * be accessible to all elements using the metadata.
Packit f546b1
 *
Packit f546b1
 * A metadata API is registered with gst_meta_api_type_register() which takes a
Packit f546b1
 * name for the metadata API and some tags associated with the metadata.
Packit f546b1
 * With gst_meta_api_type_has_tag() one can check if a certain metadata API
Packit f546b1
 * contains a given tag.
Packit f546b1
 *
Packit f546b1
 * Multiple implementations of a metadata API can be registered.
Packit f546b1
 * To implement a metadata API, gst_meta_register() should be used. This
Packit f546b1
 * function takes all parameters needed to create, free and transform metadata
Packit f546b1
 * along with the size of the metadata. The function returns a #GstMetaInfo
Packit f546b1
 * structure that contains the information for the implementation of the API.
Packit f546b1
 *
Packit f546b1
 * A specific implementation can be retrieved by name with gst_meta_get_info().
Packit f546b1
 *
Packit f546b1
 * See #GstBuffer for how the metadata can be added, retrieved and removed from
Packit f546b1
 * buffers.
Packit f546b1
 */
Packit f546b1
#include "gst_private.h"
Packit f546b1
Packit f546b1
#include "gstbuffer.h"
Packit f546b1
#include "gstmeta.h"
Packit f546b1
#include "gstinfo.h"
Packit f546b1
#include "gstutils.h"
Packit f546b1
Packit f546b1
static GHashTable *metainfo = NULL;
Packit f546b1
static GRWLock lock;
Packit f546b1
Packit f546b1
GQuark _gst_meta_transform_copy;
Packit f546b1
GQuark _gst_meta_tag_memory;
Packit f546b1
Packit f546b1
void
Packit f546b1
_priv_gst_meta_initialize (void)
Packit f546b1
{
Packit f546b1
  g_rw_lock_init (&lock);
Packit f546b1
  metainfo = g_hash_table_new (g_str_hash, g_str_equal);
Packit f546b1
Packit f546b1
  _gst_meta_transform_copy = g_quark_from_static_string ("gst-copy");
Packit f546b1
  _gst_meta_tag_memory = g_quark_from_static_string ("memory");
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_meta_api_type_register:
Packit f546b1
 * @api: an API to register
Packit f546b1
 * @tags: tags for @api
Packit f546b1
 *
Packit f546b1
 * Register and return a GType for the @api and associate it with
Packit f546b1
 * @tags.
Packit f546b1
 *
Packit f546b1
 * Returns: a unique GType for @api.
Packit f546b1
 */
Packit f546b1
GType
Packit f546b1
gst_meta_api_type_register (const gchar * api, const gchar ** tags)
Packit f546b1
{
Packit f546b1
  GType type;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (api != NULL, 0);
Packit f546b1
  g_return_val_if_fail (tags != NULL, 0);
Packit f546b1
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_META, "register API \"%s\"", api);
Packit f546b1
  type = g_pointer_type_register_static (api);
Packit f546b1
Packit f546b1
  if (type != 0) {
Packit f546b1
    gint i;
Packit f546b1
Packit f546b1
    for (i = 0; tags[i]; i++) {
Packit f546b1
      GST_CAT_DEBUG (GST_CAT_META, "  adding tag \"%s\"", tags[i]);
Packit f546b1
      g_type_set_qdata (type, g_quark_from_string (tags[i]),
Packit f546b1
          GINT_TO_POINTER (TRUE));
Packit f546b1
    }
Packit f546b1
  }
Packit f546b1
Packit f546b1
  g_type_set_qdata (type, g_quark_from_string ("tags"),
Packit f546b1
      g_strdupv ((gchar **) tags));
Packit f546b1
Packit f546b1
  return type;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_meta_api_type_has_tag:
Packit f546b1
 * @api: an API
Packit f546b1
 * @tag: the tag to check
Packit f546b1
 *
Packit f546b1
 * Check if @api was registered with @tag.
Packit f546b1
 *
Packit f546b1
 * Returns: %TRUE if @api was registered with @tag.
Packit f546b1
 */
Packit f546b1
gboolean
Packit f546b1
gst_meta_api_type_has_tag (GType api, GQuark tag)
Packit f546b1
{
Packit f546b1
  g_return_val_if_fail (api != 0, FALSE);
Packit f546b1
  g_return_val_if_fail (tag != 0, FALSE);
Packit f546b1
Packit f546b1
  return g_type_get_qdata (api, tag) != NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_meta_api_type_get_tags:
Packit f546b1
 * @api: an API
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): an array of tags as strings.
Packit f546b1
 *
Packit f546b1
 * Since: 1.2
Packit f546b1
 */
Packit f546b1
const gchar *const *
Packit f546b1
gst_meta_api_type_get_tags (GType api)
Packit f546b1
{
Packit f546b1
  const gchar **tags;
Packit f546b1
  g_return_val_if_fail (api != 0, FALSE);
Packit f546b1
Packit f546b1
  tags = g_type_get_qdata (api, g_quark_from_string ("tags"));
Packit f546b1
Packit f546b1
  if (!tags[0])
Packit f546b1
    return NULL;
Packit f546b1
Packit f546b1
  return (const gchar * const *) tags;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_meta_register:
Packit f546b1
 * @api: the type of the #GstMeta API
Packit f546b1
 * @impl: the name of the #GstMeta implementation
Packit f546b1
 * @size: the size of the #GstMeta structure
Packit f546b1
 * @init_func: (scope async): a #GstMetaInitFunction
Packit f546b1
 * @free_func: (scope async): a #GstMetaFreeFunction
Packit f546b1
 * @transform_func: (scope async): a #GstMetaTransformFunction
Packit f546b1
 *
Packit f546b1
 * Register a new #GstMeta implementation.
Packit f546b1
 *
Packit f546b1
 * The same @info can be retrieved later with gst_meta_get_info() by using
Packit f546b1
 * @impl as the key.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer none) (nullable): a #GstMetaInfo that can be used to
Packit f546b1
 * access metadata.
Packit f546b1
 */
Packit f546b1
Packit f546b1
const GstMetaInfo *
Packit f546b1
gst_meta_register (GType api, const gchar * impl, gsize size,
Packit f546b1
    GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
Packit f546b1
    GstMetaTransformFunction transform_func)
Packit f546b1
{
Packit f546b1
  GstMetaInfo *info;
Packit f546b1
  GType type;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (api != 0, NULL);
Packit f546b1
  g_return_val_if_fail (impl != NULL, NULL);
Packit f546b1
  g_return_val_if_fail (size != 0, NULL);
Packit f546b1
Packit f546b1
  if (init_func == NULL)
Packit f546b1
    g_critical ("Registering meta implementation '%s' without init function",
Packit f546b1
        impl);
Packit f546b1
Packit f546b1
  /* first try to register the implementation name. It's possible
Packit f546b1
   * that this fails because it was already registered. Don't warn,
Packit f546b1
   * glib did this for us already. */
Packit f546b1
  type = g_pointer_type_register_static (impl);
Packit f546b1
  if (type == 0)
Packit f546b1
    return NULL;
Packit f546b1
Packit f546b1
  info = g_slice_new (GstMetaInfo);
Packit f546b1
  info->api = api;
Packit f546b1
  info->type = type;
Packit f546b1
  info->size = size;
Packit f546b1
  info->init_func = init_func;
Packit f546b1
  info->free_func = free_func;
Packit f546b1
  info->transform_func = transform_func;
Packit f546b1
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_META,
Packit f546b1
      "register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT, impl,
Packit f546b1
      g_type_name (api), size);
Packit f546b1
Packit f546b1
  g_rw_lock_writer_lock (&lock);
Packit f546b1
  g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
Packit f546b1
  g_rw_lock_writer_unlock (&lock);
Packit f546b1
Packit f546b1
  return info;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_meta_get_info:
Packit f546b1
 * @impl: the name
Packit f546b1
 *
Packit f546b1
 * Lookup a previously registered meta info structure by its implementation name
Packit f546b1
 * @impl.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer none) (nullable): a #GstMetaInfo with @impl, or
Packit f546b1
 * %NULL when no such metainfo exists.
Packit f546b1
 */
Packit f546b1
const GstMetaInfo *
Packit f546b1
gst_meta_get_info (const gchar * impl)
Packit f546b1
{
Packit f546b1
  GstMetaInfo *info;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (impl != NULL, NULL);
Packit f546b1
Packit f546b1
  g_rw_lock_reader_lock (&lock);
Packit f546b1
  info = g_hash_table_lookup (metainfo, impl);
Packit f546b1
  g_rw_lock_reader_unlock (&lock);
Packit f546b1
Packit f546b1
  return info;
Packit f546b1
}