• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:gstnetaddressmeta
22  * @title: GstNetAddressMeta
23  * @short_description: Network address metadata
24  *
25  * #GstNetAddressMeta can be used to store a network address (a #GSocketAddress)
26  * in a #GstBuffer so that it network elements can track the to and from address
27  * of the buffer.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <string.h>
34 
35 #include "gstnetaddressmeta.h"
36 
37 static gboolean
net_address_meta_init(GstMeta * meta,gpointer params,GstBuffer * buffer)38 net_address_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
39 {
40   GstNetAddressMeta *nmeta = (GstNetAddressMeta *) meta;
41 
42   nmeta->addr = NULL;
43 
44   return TRUE;
45 }
46 
47 static gboolean
net_address_meta_transform(GstBuffer * transbuf,GstMeta * meta,GstBuffer * buffer,GQuark type,gpointer data)48 net_address_meta_transform (GstBuffer * transbuf, GstMeta * meta,
49     GstBuffer * buffer, GQuark type, gpointer data)
50 {
51   GstNetAddressMeta *smeta, *dmeta;
52   smeta = (GstNetAddressMeta *) meta;
53 
54   /* we always copy no matter what transform */
55   dmeta = gst_buffer_add_net_address_meta (transbuf, smeta->addr);
56   if (!dmeta)
57     return FALSE;
58 
59   return TRUE;
60 }
61 
62 static void
net_address_meta_free(GstMeta * meta,GstBuffer * buffer)63 net_address_meta_free (GstMeta * meta, GstBuffer * buffer)
64 {
65   GstNetAddressMeta *nmeta = (GstNetAddressMeta *) meta;
66 
67   if (nmeta->addr)
68     g_object_unref (nmeta->addr);
69   nmeta->addr = NULL;
70 }
71 
72 GType
gst_net_address_meta_api_get_type(void)73 gst_net_address_meta_api_get_type (void)
74 {
75   static GType type;
76   static const gchar *tags[] = { "origin", NULL };
77 
78   if (g_once_init_enter (&type)) {
79     GType _type = gst_meta_api_type_register ("GstNetAddressMetaAPI", tags);
80     g_once_init_leave (&type, _type);
81   }
82   return type;
83 }
84 
85 const GstMetaInfo *
gst_net_address_meta_get_info(void)86 gst_net_address_meta_get_info (void)
87 {
88   static const GstMetaInfo *meta_info = NULL;
89 
90   if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
91     const GstMetaInfo *mi = gst_meta_register (GST_NET_ADDRESS_META_API_TYPE,
92         "GstNetAddressMeta",
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);
97   }
98   return meta_info;
99 }
100 
101 /**
102  * gst_buffer_add_net_address_meta:
103  * @buffer: a #GstBuffer
104  * @addr: a @GSocketAddress to connect to @buffer
105  *
106  * Attaches @addr as metadata in a #GstNetAddressMeta to @buffer.
107  *
108  * Returns: (transfer none): a #GstNetAddressMeta connected to @buffer
109  */
110 GstNetAddressMeta *
gst_buffer_add_net_address_meta(GstBuffer * buffer,GSocketAddress * addr)111 gst_buffer_add_net_address_meta (GstBuffer * buffer, GSocketAddress * addr)
112 {
113   GstNetAddressMeta *meta;
114 
115   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
116   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (addr), NULL);
117 
118   meta =
119       (GstNetAddressMeta *) gst_buffer_add_meta (buffer,
120       GST_NET_ADDRESS_META_INFO, NULL);
121 
122   meta->addr = g_object_ref (addr);
123 
124   return meta;
125 }
126 
127 /**
128  * gst_buffer_get_net_address_meta:
129  * @buffer: a #GstBuffer
130  *
131  * Find the #GstNetAddressMeta on @buffer.
132  *
133  * Returns: (transfer none): the #GstNetAddressMeta or %NULL when there
134  * is no such metadata on @buffer.
135  */
136 GstNetAddressMeta *
gst_buffer_get_net_address_meta(GstBuffer * buffer)137 gst_buffer_get_net_address_meta (GstBuffer * buffer)
138 {
139   return (GstNetAddressMeta *)
140       gst_buffer_get_meta (buffer, GST_NET_ADDRESS_META_API_TYPE);
141 }
142