gstnetaddressmeta.c 3.78 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Wim Taymans's avatar
Wim Taymans committed
18 19 20 21
 */

/**
 * SECTION:gstnetaddressmeta
22
 * @title: GstNetAddressMeta
Wim Taymans's avatar
Wim Taymans committed
23 24
 * @short_description: Network address metadata
 *
25 26 27
 * #GstNetAddressMeta can be used to store a network address (a #GSocketAddress)
 * in a #GstBuffer so that it network elements can track the to and from address
 * of the buffer.
Wim Taymans's avatar
Wim Taymans committed
28
 */
29 30 31
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Wim Taymans's avatar
Wim Taymans committed
32 33 34 35 36

#include <string.h>

#include "gstnetaddressmeta.h"

37
static gboolean
38
net_address_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
39
{
40 41 42
  GstNetAddressMeta *nmeta = (GstNetAddressMeta *) meta;

  nmeta->addr = NULL;
43 44 45 46

  return TRUE;
}

47 48
static gboolean
net_address_meta_transform (GstBuffer * transbuf, GstMeta * meta,
49
    GstBuffer * buffer, GQuark type, gpointer data)
Wim Taymans's avatar
Wim Taymans committed
50
{
51 52
  GstNetAddressMeta *smeta, *dmeta;
  smeta = (GstNetAddressMeta *) meta;
53

54
  /* we always copy no matter what transform */
55 56 57
  dmeta = gst_buffer_add_net_address_meta (transbuf, smeta->addr);
  if (!dmeta)
    return FALSE;
58 59

  return TRUE;
60
}
Wim Taymans's avatar
Wim Taymans committed
61

62
static void
63
net_address_meta_free (GstMeta * meta, GstBuffer * buffer)
64
{
65 66 67 68 69
  GstNetAddressMeta *nmeta = (GstNetAddressMeta *) meta;

  if (nmeta->addr)
    g_object_unref (nmeta->addr);
  nmeta->addr = NULL;
Wim Taymans's avatar
Wim Taymans committed
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84
GType
gst_net_address_meta_api_get_type (void)
{
  static volatile GType type;
  static const gchar *tags[] = { "origin", NULL };

  if (g_once_init_enter (&type)) {
    GType _type = gst_meta_api_type_register ("GstNetAddressMetaAPI", tags);
    g_once_init_leave (&type, _type);
  }
  return type;
}

Wim Taymans's avatar
Wim Taymans committed
85 86 87 88 89
const GstMetaInfo *
gst_net_address_meta_get_info (void)
{
  static const GstMetaInfo *meta_info = NULL;

90
  if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
91
    const GstMetaInfo *mi = gst_meta_register (GST_NET_ADDRESS_META_API_TYPE,
92
        "GstNetAddressMeta",
Wim Taymans's avatar
Wim Taymans committed
93
        sizeof (GstNetAddressMeta),
94
        net_address_meta_init,
95
        net_address_meta_free, net_address_meta_transform);
96
    g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
Wim Taymans's avatar
Wim Taymans committed
97 98 99 100
  }
  return meta_info;
}

101 102 103 104 105 106 107 108 109
/**
 * gst_buffer_add_net_address_meta:
 * @buffer: a #GstBuffer
 * @addr: a @GSocketAddress to connect to @buffer
 *
 * Attaches @addr as metadata in a #GstNetAddressMeta to @buffer.
 *
 * Returns: (transfer none): a #GstNetAddressMeta connected to @buffer
 */
110 111
GstNetAddressMeta *
gst_buffer_add_net_address_meta (GstBuffer * buffer, GSocketAddress * addr)
Wim Taymans's avatar
Wim Taymans committed
112
{
113
  GstNetAddressMeta *meta;
Wim Taymans's avatar
Wim Taymans committed
114

115 116
  g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
  g_return_val_if_fail (G_IS_SOCKET_ADDRESS (addr), NULL);
Wim Taymans's avatar
Wim Taymans committed
117

118 119 120
  meta =
      (GstNetAddressMeta *) gst_buffer_add_meta (buffer,
      GST_NET_ADDRESS_META_INFO, NULL);
Wim Taymans's avatar
Wim Taymans committed
121

122
  meta->addr = g_object_ref (addr);
Wim Taymans's avatar
Wim Taymans committed
123

124
  return meta;
Wim Taymans's avatar
Wim Taymans committed
125
}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

/**
 * gst_buffer_get_net_address_meta:
 * @buffer: a #GstBuffer
 *
 * Find the #GstNetAddressMeta on @buffer.
 *
 * Returns: (transfer none): the #GstNetAddressMeta or %NULL when there
 * is no such metadata on @buffer.
 */
GstNetAddressMeta *
gst_buffer_get_net_address_meta (GstBuffer * buffer)
{
  return (GstNetAddressMeta *)
      gst_buffer_get_meta (buffer, GST_NET_ADDRESS_META_API_TYPE);
}