Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst-libs / gst / netbuffer / gstnetbuffer.c
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_finalize (GstNetBuffer * nbuf);
38 static GstNetBuffer *gst_netbuffer_copy (GstNetBuffer * nbuf);
39
40 static GstBufferClass *parent_class;
41
42 G_DEFINE_TYPE (GstNetBuffer, gst_netbuffer, GST_TYPE_BUFFER);
43
44 static void
45 gst_netbuffer_class_init (GstNetBufferClass * netbuffer_class)
46 {
47   GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (netbuffer_class);
48
49   parent_class = g_type_class_peek_parent (netbuffer_class);
50
51   mo_class->copy = (GstMiniObjectCopyFunction) gst_netbuffer_copy;
52   mo_class->finalize = (GstMiniObjectFinalizeFunction) gst_netbuffer_finalize;
53 }
54
55 static void
56 gst_netbuffer_init (GstNetBuffer * instance)
57 {
58 }
59
60 static void
61 gst_netbuffer_finalize (GstNetBuffer * nbuf)
62 {
63   GST_MINI_OBJECT_CLASS (parent_class)->finalize (GST_MINI_OBJECT (nbuf));
64 }
65
66 static GstNetBuffer *
67 gst_netbuffer_copy (GstNetBuffer * nbuf)
68 {
69   GstNetBuffer *copy;
70
71   copy = gst_netbuffer_new ();
72
73   /* we simply copy everything from our parent */
74   GST_BUFFER_DATA (copy) =
75       g_memdup (GST_BUFFER_DATA (nbuf), GST_BUFFER_SIZE (nbuf));
76   /* make sure it gets freed (even if the parent is subclassed, we return a
77      normal buffer) */
78   GST_BUFFER_MALLOCDATA (copy) = GST_BUFFER_DATA (copy);
79   GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (nbuf);
80
81   memcpy (&copy->to, &nbuf->to, sizeof (nbuf->to));
82   memcpy (&copy->from, &nbuf->from, sizeof (nbuf->from));
83
84   /* copy metadata */
85   gst_buffer_copy_metadata (GST_BUFFER_CAST (copy),
86       GST_BUFFER_CAST (nbuf), GST_BUFFER_COPY_ALL);
87
88   return copy;
89 }
90
91 /**
92  * gst_netbuffer_new:
93  *
94  * Create a new network buffer.
95  *
96  * Returns: a new #GstNetBuffer.
97  */
98 GstNetBuffer *
99 gst_netbuffer_new (void)
100 {
101   GstNetBuffer *buf;
102
103   buf = (GstNetBuffer *) gst_mini_object_new (GST_TYPE_NETBUFFER);
104
105   return buf;
106 }
107
108 /**
109  * gst_netaddress_set_ip4_address:
110  * @naddr: a network address
111  * @address: an IPv4 network address.
112  * @port: a port number to set.
113  *
114  * Set @naddr with the IPv4 @address and @port pair.
115  *
116  * Note that @port and @address must be expressed in network byte order,
117  * use g_htons() and g_htonl() to convert them to network byte order.
118  */
119 void
120 gst_netaddress_set_ip4_address (GstNetAddress * naddr, guint32 address,
121     guint16 port)
122 {
123   g_return_if_fail (naddr != NULL);
124
125   naddr->type = GST_NET_TYPE_IP4;
126   naddr->address.ip4 = address;
127   naddr->port = port;
128 }
129
130 /**
131  * gst_netaddress_set_ip6_address:
132  * @naddr: a network address
133  * @address: an IPv6 network address.
134  * @port: a port number to set.
135  *
136  * Set @naddr with the IPv6 @address and @port pair.
137  *
138  * Note that @port must be expressed in network byte order, use g_htons() to convert
139  * it to network byte order.
140  */
141 void
142 gst_netaddress_set_ip6_address (GstNetAddress * naddr, guint8 address[16],
143     guint16 port)
144 {
145   g_return_if_fail (naddr != NULL);
146
147   naddr->type = GST_NET_TYPE_IP6;
148   memcpy (&naddr->address.ip6, address, 16);
149   naddr->port = port;
150 }
151
152 /**
153  * gst_netaddress_get_net_type:
154  * @naddr: a network address
155  *
156  * Get the type of address stored in @naddr.
157  *
158  * Returns: the network type stored in @naddr.
159  */
160 GstNetType
161 gst_netaddress_get_net_type (const GstNetAddress * naddr)
162 {
163   g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
164
165   return naddr->type;
166 }
167
168 /**
169  * gst_netaddress_get_ip4_address:
170  * @naddr: a network address
171  * @address: a location to store the address.
172  * @port: a location to store the port.
173  *
174  * Get the IPv4 address stored in @naddr into @address. This function requires
175  * that the address type of @naddr is of type #GST_NET_TYPE_IP4.
176  *
177  * Note that @port and @address are expressed in network byte order, use
178  * g_ntohs() and g_ntohl() to convert them to host order.
179  *
180  * Returns: TRUE if the address could be retrieved.
181  */
182 gboolean
183 gst_netaddress_get_ip4_address (const GstNetAddress * naddr, guint32 * address,
184     guint16 * port)
185 {
186   g_return_val_if_fail (naddr != NULL, FALSE);
187
188   if (naddr->type == GST_NET_TYPE_UNKNOWN || naddr->type == GST_NET_TYPE_IP6)
189     return FALSE;
190
191   if (address)
192     *address = naddr->address.ip4;
193   if (port)
194     *port = naddr->port;
195
196   return TRUE;
197 }
198
199 /**
200  * gst_netaddress_get_ip6_address:
201  * @naddr: a network address
202  * @address: a location to store the result.
203  * @port: a location to store the port.
204  *
205  * Get the IPv6 address stored in @naddr into @address.
206  *
207  * If @naddr is of type GST_NET_TYPE_IP4, the transitional IP6 address is
208  * returned.
209  *
210  * Note that @port is expressed in network byte order, use g_ntohs() to convert
211  * it to host order.
212  *
213  * Returns: TRUE if the address could be retrieved.
214  */
215 gboolean
216 gst_netaddress_get_ip6_address (const GstNetAddress * naddr, guint8 address[16],
217     guint16 * port)
218 {
219   static guint8 ip4_transition[16] =
220       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
221   g_return_val_if_fail (naddr != NULL, FALSE);
222
223   if (naddr->type == GST_NET_TYPE_UNKNOWN)
224     return FALSE;
225
226   if (address) {
227     if (naddr->type == GST_NET_TYPE_IP6) {
228       memcpy (address, naddr->address.ip6, 16);
229     } else {                    /* naddr->type == GST_NET_TYPE_IP4 */
230       memcpy (address, ip4_transition, 12);
231       memcpy (address + 12, (guint8 *) & (naddr->address.ip4), 4);
232     }
233   }
234   if (port)
235     *port = naddr->port;
236
237   return TRUE;
238 }
239
240 /**
241  * gst_netaddress_get_address_bytes:
242  * @naddr: a network address
243  * @address: a location to store the result.
244  * @port: a location to store the port.
245  *
246  * Get just the address bytes stored in @naddr into @address.
247  *
248  * Note that @port is expressed in network byte order, use g_ntohs() to convert
249  * it to host order. IP4 addresses are also stored in network byte order.
250  *
251  * Returns: number of bytes actually copied
252  *
253  * Since: 0.10.22
254  */
255 gint
256 gst_netaddress_get_address_bytes (const GstNetAddress * naddr,
257     guint8 address[16], guint16 * port)
258 {
259   gint ret = 0;
260
261   g_return_val_if_fail (naddr != NULL, FALSE);
262
263   if (naddr->type == GST_NET_TYPE_UNKNOWN)
264     return 0;
265
266   if (address) {
267     if (naddr->type == GST_NET_TYPE_IP6) {
268       memcpy (address, naddr->address.ip6, 16);
269       ret = 16;
270     } else {                    /* naddr->type == GST_NET_TYPE_IP4 */
271       memcpy (address, (guint8 *) & (naddr->address.ip4), 4);
272       ret = 4;
273     }
274   }
275   if (port)
276     *port = naddr->port;
277
278   return ret;
279 }
280
281 /**
282  * gst_netaddress_set_address_bytes:
283  * @naddr: a network address
284  * @type: the address type (IPv4 or IPV6)
285  * @address: a location to store the result.
286  * @port: a location to store the port.
287  *
288  * Set just the address bytes stored in @naddr into @address.
289  *
290  * Note that @port must be expressed in network byte order, use g_htons() to
291  * convert it to network byte order order. IP4 address bytes must also be
292  * stored in network byte order.
293  *
294  * Returns: number of bytes actually copied
295  *
296  * Since: 0.10.22
297  */
298 gint
299 gst_netaddress_set_address_bytes (GstNetAddress * naddr, GstNetType type,
300     guint8 address[16], guint16 port)
301 {
302   gint len = 0;
303
304   g_return_val_if_fail (naddr != NULL, 0);
305
306   naddr->type = type;
307   switch (naddr->type) {
308     case GST_NET_TYPE_UNKNOWN:
309     case GST_NET_TYPE_IP6:
310       len = 16;
311       memcpy (naddr->address.ip6, address, 16);
312       break;
313     case GST_NET_TYPE_IP4:
314       len = 4;
315       memcpy ((guint8 *) & (naddr->address.ip4), address, 4);
316       break;
317   }
318
319   if (port)
320     naddr->port = port;
321
322   return len;
323 }
324
325 /**
326  * gst_netaddress_equal:
327  * @naddr1: The first #GstNetAddress
328  * @naddr2: The second #GstNetAddress
329  *
330  * Compare two #GstNetAddress structures
331  *
332  * Returns: TRUE if they are identical, FALSE otherwise
333  *
334  * Since: 0.10.18
335  */
336 gboolean
337 gst_netaddress_equal (const GstNetAddress * naddr1,
338     const GstNetAddress * naddr2)
339 {
340   g_return_val_if_fail (naddr1 != NULL, FALSE);
341   g_return_val_if_fail (naddr2 != NULL, FALSE);
342
343   if (naddr1->type != naddr2->type)
344     return FALSE;
345
346   if (naddr1->port != naddr2->port)
347     return FALSE;
348
349   switch (naddr1->type) {
350     case GST_NET_TYPE_IP4:
351       if (naddr1->address.ip4 != naddr2->address.ip4)
352         return FALSE;
353       break;
354     case GST_NET_TYPE_IP6:
355       if (memcmp (naddr1->address.ip6, naddr2->address.ip6,
356               sizeof (naddr1->address.ip6)))
357         return FALSE;
358       break;
359     default:
360       break;
361   }
362   return TRUE;
363 }
364
365 /**
366  * gst_netaddress_to_string:
367  * @naddr: a #GstNetAddress
368  * @dest: destination
369  * @len: len of @dest
370  *
371  * Copies a string representation of @naddr into @dest. Up to @len bytes are
372  * copied.
373  *
374  * Returns: the number of bytes which would be produced if the buffer was large
375  * enough
376  *
377  * Since: 0.10.24
378  */
379 gint
380 gst_netaddress_to_string (const GstNetAddress * naddr, gchar * dest, gulong len)
381 {
382   gint result;
383
384   g_return_val_if_fail (naddr != NULL, FALSE);
385   g_return_val_if_fail (dest != NULL, FALSE);
386
387   switch (naddr->type) {
388     case GST_NET_TYPE_IP4:
389     {
390       guint32 address;
391       guint16 port;
392
393       gst_netaddress_get_ip4_address (naddr, &address, &port);
394       address = g_ntohl (address);
395
396       result = g_snprintf (dest, len, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
397           (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
398           g_ntohs (port));
399       break;
400     }
401     case GST_NET_TYPE_IP6:
402     {
403       guint8 address[16];
404       guint16 port;
405
406       gst_netaddress_get_ip6_address (naddr, address, &port);
407
408       result =
409           g_snprintf (dest, len, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%d",
410           (address[0] << 8) | address[1], (address[2] << 8) | address[3],
411           (address[4] << 8) | address[5], (address[6] << 8) | address[7],
412           (address[8] << 8) | address[9], (address[10] << 8) | address[11],
413           (address[12] << 8) | address[13], (address[14] << 8) | address[15],
414           g_ntohs (port));
415       break;
416     }
417     default:
418       dest[0] = 0;
419       result = 0;
420       break;
421   }
422   return result;
423 }