gst_plugins_base/gst-libs/gst/netbuffer/gstnetbuffer.c
changeset 2 5505e8908944
parent 0 0e761a78d257
child 7 567bb019e3e3
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 /* GStreamer
       
     2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.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., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 /**
       
    21  * SECTION:gstnetbuffer
       
    22  * @short_description: Buffer for use in network sources and sinks
       
    23  *
       
    24  * #GstNetBuffer is a subclass of a normal #GstBuffer that contains two
       
    25  * additional metadata fields of type #GstNetAddress named 'to' and 'from'. The
       
    26  * buffer can be used to store additional information about the origin of the
       
    27  * buffer data and is used in various network elements to track the to and from
       
    28  * addresses.
       
    29  *
       
    30  * Last reviewed on 2006-08-21 (0.10.10)
       
    31  */
       
    32 
       
    33 #include <string.h>
       
    34 
       
    35 #include "gstnetbuffer.h"
       
    36 
       
    37 static void gst_netbuffer_init (GTypeInstance * instance, gpointer g_class);
       
    38 static void gst_netbuffer_class_init (gpointer g_class, gpointer class_data);
       
    39 static void gst_netbuffer_finalize (GstNetBuffer * nbuf);
       
    40 static GstNetBuffer *gst_netbuffer_copy (GstNetBuffer * nbuf);
       
    41 
       
    42 static GstBufferClass *parent_class;
       
    43 #ifdef __SYMBIAN32__
       
    44 EXPORT_C
       
    45 #endif
       
    46 
       
    47 
       
    48 GType
       
    49 gst_netbuffer_get_type (void)
       
    50 {
       
    51   static GType _gst_netbuffer_type = 0;
       
    52 
       
    53   if (G_UNLIKELY (_gst_netbuffer_type == 0)) {
       
    54     static const GTypeInfo netbuffer_info = {
       
    55       sizeof (GstNetBufferClass),
       
    56       NULL,
       
    57       NULL,
       
    58       gst_netbuffer_class_init,
       
    59       NULL,
       
    60       NULL,
       
    61       sizeof (GstNetBuffer),
       
    62       0,
       
    63       gst_netbuffer_init,
       
    64       NULL
       
    65     };
       
    66 
       
    67     _gst_netbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
       
    68         "GstNetBuffer", &netbuffer_info, 0);
       
    69   }
       
    70   return _gst_netbuffer_type;
       
    71 }
       
    72 
       
    73 static void
       
    74 gst_netbuffer_class_init (gpointer g_class, gpointer class_data)
       
    75 {
       
    76   GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (g_class);
       
    77 
       
    78   parent_class = g_type_class_peek_parent (g_class);
       
    79 
       
    80   mo_class->copy = (GstMiniObjectCopyFunction) gst_netbuffer_copy;
       
    81   mo_class->finalize = (GstMiniObjectFinalizeFunction) gst_netbuffer_finalize;
       
    82 }
       
    83 
       
    84 static void
       
    85 gst_netbuffer_init (GTypeInstance * instance, gpointer g_class)
       
    86 {
       
    87 }
       
    88 
       
    89 static void
       
    90 gst_netbuffer_finalize (GstNetBuffer * nbuf)
       
    91 {
       
    92   GST_MINI_OBJECT_CLASS (parent_class)->finalize (GST_MINI_OBJECT (nbuf));
       
    93 }
       
    94 
       
    95 static GstNetBuffer *
       
    96 gst_netbuffer_copy (GstNetBuffer * nbuf)
       
    97 {
       
    98   GstNetBuffer *copy;
       
    99 
       
   100   copy = gst_netbuffer_new ();
       
   101 
       
   102   /* we simply copy everything from our parent */
       
   103   GST_BUFFER_DATA (copy) =
       
   104       g_memdup (GST_BUFFER_DATA (nbuf), GST_BUFFER_SIZE (nbuf));
       
   105   /* make sure it gets freed (even if the parent is subclassed, we return a
       
   106      normal buffer) */
       
   107   GST_BUFFER_MALLOCDATA (copy) = GST_BUFFER_DATA (copy);
       
   108   GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (nbuf);
       
   109 
       
   110   memcpy (&copy->to, &nbuf->to, sizeof (nbuf->to));
       
   111   memcpy (&copy->from, &nbuf->from, sizeof (nbuf->from));
       
   112 
       
   113   /* copy metadata */
       
   114   gst_buffer_copy_metadata (GST_BUFFER_CAST (copy),
       
   115       GST_BUFFER_CAST (nbuf), GST_BUFFER_COPY_ALL);
       
   116 
       
   117   return copy;
       
   118 }
       
   119 
       
   120 /**
       
   121  * gst_netbuffer_new:
       
   122  *
       
   123  * Create a new network buffer.
       
   124  *
       
   125  * Returns: a new #GstNetBuffer.
       
   126  */
       
   127 #ifdef __SYMBIAN32__
       
   128 EXPORT_C
       
   129 #endif
       
   130 
       
   131 GstNetBuffer *
       
   132 gst_netbuffer_new (void)
       
   133 {
       
   134   GstNetBuffer *buf;
       
   135 
       
   136   buf = (GstNetBuffer *) gst_mini_object_new (GST_TYPE_NETBUFFER);
       
   137 
       
   138   return buf;
       
   139 }
       
   140 
       
   141 /**
       
   142  * gst_netaddress_set_ip4_address:
       
   143  * @naddr: a network address
       
   144  * @address: an IPv4 network address.
       
   145  * @port: a port number to set.
       
   146  *
       
   147  * Set @naddr with the IPv4 @address and @port pair.
       
   148  */
       
   149 #ifdef __SYMBIAN32__
       
   150 EXPORT_C
       
   151 #endif
       
   152 
       
   153 void
       
   154 gst_netaddress_set_ip4_address (GstNetAddress * naddr, guint32 address,
       
   155     guint16 port)
       
   156 {
       
   157   g_return_if_fail (naddr != NULL);
       
   158 
       
   159   naddr->type = GST_NET_TYPE_IP4;
       
   160   naddr->address.ip4 = address;
       
   161   naddr->port = port;
       
   162 }
       
   163 
       
   164 /**
       
   165  * gst_netaddress_set_ip6_address:
       
   166  * @naddr: a network address
       
   167  * @address: an IPv6 network address.
       
   168  * @port: a port number to set.
       
   169  *
       
   170  * Set @naddr with the IPv6 @address and @port pair.
       
   171  */
       
   172 #ifdef __SYMBIAN32__
       
   173 EXPORT_C
       
   174 #endif
       
   175 
       
   176 void
       
   177 gst_netaddress_set_ip6_address (GstNetAddress * naddr, guint8 address[16],
       
   178     guint16 port)
       
   179 {
       
   180   g_return_if_fail (naddr != NULL);
       
   181 
       
   182   naddr->type = GST_NET_TYPE_IP6;
       
   183   memcpy (&naddr->address.ip6, address, 16);
       
   184   naddr->port = port;
       
   185 }
       
   186 
       
   187 /**
       
   188  * gst_netaddress_get_net_type:
       
   189  * @naddr: a network address
       
   190  *
       
   191  * Get the type of address stored in @naddr.
       
   192  *
       
   193  * Returns: the network type stored in @naddr.
       
   194  */
       
   195 #ifdef __SYMBIAN32__
       
   196 EXPORT_C
       
   197 #endif
       
   198 
       
   199 GstNetType
       
   200 gst_netaddress_get_net_type (GstNetAddress * naddr)
       
   201 {
       
   202   g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
       
   203 
       
   204   return naddr->type;
       
   205 }
       
   206 
       
   207 /**
       
   208  * gst_netaddress_get_ip4_address:
       
   209  * @naddr: a network address
       
   210  * @address: a location to store the address.
       
   211  * @port: a location to store the port.
       
   212  *
       
   213  * Get the IPv4 address stored in @naddr into @address.
       
   214  *
       
   215  * Returns: TRUE if the address could be retrieved.
       
   216  */
       
   217 #ifdef __SYMBIAN32__
       
   218 EXPORT_C
       
   219 #endif
       
   220 
       
   221 gboolean
       
   222 gst_netaddress_get_ip4_address (GstNetAddress * naddr, guint32 * address,
       
   223     guint16 * port)
       
   224 {
       
   225   g_return_val_if_fail (naddr != NULL, FALSE);
       
   226 
       
   227   if (naddr->type == GST_NET_TYPE_UNKNOWN)
       
   228     return FALSE;
       
   229 
       
   230   if (address)
       
   231     *address = naddr->address.ip4;
       
   232   if (port)
       
   233     *port = naddr->port;
       
   234 
       
   235   return TRUE;
       
   236 }
       
   237 
       
   238 /**
       
   239  * gst_netaddress_get_ip6_address:
       
   240  * @naddr: a network address
       
   241  * @address: a location to store the result.
       
   242  * @port: a location to store the port.
       
   243  *
       
   244  * Get the IPv6 address stored in @naddr into @address.
       
   245  *
       
   246  * Returns: TRUE if the address could be retrieved.
       
   247  */
       
   248 #ifdef __SYMBIAN32__
       
   249 EXPORT_C
       
   250 #endif
       
   251 
       
   252 gboolean
       
   253 gst_netaddress_get_ip6_address (GstNetAddress * naddr, guint8 address[16],
       
   254     guint16 * port)
       
   255 {
       
   256   g_return_val_if_fail (naddr != NULL, FALSE);
       
   257 
       
   258   if (naddr->type == GST_NET_TYPE_UNKNOWN)
       
   259     return FALSE;
       
   260 
       
   261   if (address)
       
   262     memcpy (address, naddr->address.ip6, 16);
       
   263   if (port)
       
   264     *port = naddr->port;
       
   265 
       
   266   return TRUE;
       
   267 }
       
   268 
       
   269 /**
       
   270  * gst_netaddress_equal:
       
   271  * @naddr1: The first #GstNetAddress
       
   272  * @naddr2: The second #GstNetAddress
       
   273  *
       
   274  * Compare two #GstNetAddress structures
       
   275  *
       
   276  * Returns: TRUE if they are identical, FALSE otherwise
       
   277  *
       
   278  * Since: 0.10.18
       
   279  */
       
   280 #ifdef __SYMBIAN32__
       
   281 EXPORT_C
       
   282 #endif
       
   283 
       
   284 gboolean
       
   285 gst_netaddress_equal (const GstNetAddress * naddr1,
       
   286     const GstNetAddress * naddr2)
       
   287 {
       
   288   g_return_val_if_fail (naddr1 != NULL, FALSE);
       
   289   g_return_val_if_fail (naddr2 != NULL, FALSE);
       
   290 
       
   291   if (naddr1->type != naddr2->type)
       
   292     return FALSE;
       
   293 
       
   294   if (naddr1->port != naddr2->port)
       
   295     return FALSE;
       
   296 
       
   297   switch (naddr1->type) {
       
   298     case GST_NET_TYPE_IP4:
       
   299       if (naddr1->address.ip4 != naddr2->address.ip4)
       
   300         return FALSE;
       
   301       break;
       
   302     case GST_NET_TYPE_IP6:
       
   303       if (memcmp (naddr1->address.ip6, naddr2->address.ip6,
       
   304               sizeof (naddr1->address.ip6)))
       
   305         return FALSE;
       
   306       break;
       
   307     default:
       
   308       break;
       
   309   }
       
   310   return TRUE;
       
   311 }