75419953257907045d5d529952b241d520f7da68
[platform/upstream/gstreamer.git] / libs / gst / net / gstnettimepacket.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2012 Collabora Ltd. <tim.muller@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /**
22  * SECTION:gstnettimepacket
23  * @short_description: Helper structure to construct clock packets used
24  *                     by network clocks.
25  * @see_also: #GstClock, #GstNetClientClock, #GstNetTimeProvider
26  *
27  * Various functions for receiving, sending an serializing #GstNetTimePacket
28  * structures.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <glib.h>
36
37 #ifdef __CYGWIN__
38 # include <unistd.h>
39 # include <fcntl.h>
40 #endif
41
42 #include "gstnettimepacket.h"
43
44 G_DEFINE_BOXED_TYPE (GstNetTimePacket, gst_net_time_packet,
45     gst_net_time_packet_copy, gst_net_time_packet_free);
46
47 /**
48  * gst_net_time_packet_new:
49  * @buffer: (array): a buffer from which to construct the packet, or NULL
50  *
51  * Creates a new #GstNetTimePacket from a buffer received over the network. The
52  * caller is responsible for ensuring that @buffer is at least
53  * #GST_NET_TIME_PACKET_SIZE bytes long.
54  *
55  * If @buffer is #NULL, the local and remote times will be set to
56  * #GST_CLOCK_TIME_NONE.
57  *
58  * MT safe. Caller owns return value (gst_net_time_packet_free to free).
59  *
60  * Returns: The new #GstNetTimePacket.
61  */
62 GstNetTimePacket *
63 gst_net_time_packet_new (const guint8 * buffer)
64 {
65   GstNetTimePacket *ret;
66
67   g_assert (sizeof (GstClockTime) == 8);
68
69   ret = g_new0 (GstNetTimePacket, 1);
70
71   if (buffer) {
72     ret->local_time = GST_READ_UINT64_BE (buffer);
73     ret->remote_time = GST_READ_UINT64_BE (buffer + sizeof (GstClockTime));
74   } else {
75     ret->local_time = GST_CLOCK_TIME_NONE;
76     ret->remote_time = GST_CLOCK_TIME_NONE;
77   }
78
79   return ret;
80 }
81
82 /**
83  * gst_net_time_packet_free:
84  * @packet: the #GstNetTimePacket
85  *
86  * Free @packet.
87  */
88 void
89 gst_net_time_packet_free (GstNetTimePacket * packet)
90 {
91   g_free (packet);
92 }
93
94 /**
95  * gst_net_time_packet_copy:
96  * @packet: the #GstNetTimePacket
97  *
98  * Make a copy of @packet.
99  *
100  * Returns: a copy of @packet, free with gst_net_time_packet_free().
101  */
102 GstNetTimePacket *
103 gst_net_time_packet_copy (const GstNetTimePacket * packet)
104 {
105   GstNetTimePacket *ret;
106
107   ret = g_new0 (GstNetTimePacket, 1);
108   ret->local_time = packet->local_time;
109   ret->remote_time = packet->remote_time;
110
111   return ret;
112 }
113
114 /**
115  * gst_net_time_packet_serialize:
116  * @packet: the #GstNetTimePacket
117  *
118  * Serialized a #GstNetTimePacket into a newly-allocated sequence of
119  * #GST_NET_TIME_PACKET_SIZE bytes, in network byte order. The value returned is
120  * suitable for passing to write(2) or sendto(2) for communication over the
121  * network.
122  *
123  * MT safe. Caller owns return value (g_free to free).
124  *
125  * Returns: A newly allocated sequence of #GST_NET_TIME_PACKET_SIZE bytes.
126  */
127 guint8 *
128 gst_net_time_packet_serialize (const GstNetTimePacket * packet)
129 {
130   guint8 *ret;
131
132   g_assert (sizeof (GstClockTime) == 8);
133
134   ret = g_new0 (guint8, GST_NET_TIME_PACKET_SIZE);
135
136   GST_WRITE_UINT64_BE (ret, packet->local_time);
137   GST_WRITE_UINT64_BE (ret + sizeof (GstClockTime), packet->remote_time);
138
139   return ret;
140 }
141
142 /**
143  * gst_net_time_packet_receive:
144  * @socket: socket to receive the time packet on
145  * @src_address: (out): address of variable to return sender address
146  * @error: return address for a #GError, or NULL
147  *
148  * Receives a #GstNetTimePacket over a socket. Handles interrupted system
149  * calls, but otherwise returns NULL on error.
150  *
151  * Returns: (transfer full): a new #GstNetTimePacket, or NULL on error. Free
152  *    with gst_net_time_packet_free() when done.
153  */
154 GstNetTimePacket *
155 gst_net_time_packet_receive (GSocket * socket,
156     GSocketAddress ** src_address, GError ** error)
157 {
158   gchar buffer[GST_NET_TIME_PACKET_SIZE];
159   GError *err = NULL;
160   gssize ret;
161
162   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
163   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
164
165   while (TRUE) {
166     ret = g_socket_receive_from (socket, src_address, buffer,
167         GST_NET_TIME_PACKET_SIZE, NULL, &err);
168
169     if (ret < 0) {
170       if (err->code == G_IO_ERROR_WOULD_BLOCK) {
171         g_error_free (err);
172         err = NULL;
173         continue;
174       } else {
175         goto receive_error;
176       }
177     } else if (ret < GST_NET_TIME_PACKET_SIZE) {
178       goto short_packet;
179     } else {
180       return gst_net_time_packet_new ((const guint8 *) buffer);
181     }
182   }
183
184 receive_error:
185   {
186     GST_DEBUG ("receive error: %s", err->message);
187     g_propagate_error (error, err);
188     return NULL;
189   }
190 short_packet:
191   {
192     GST_DEBUG ("someone sent us a short packet (%" G_GSSIZE_FORMAT " < %d)",
193         ret, GST_NET_TIME_PACKET_SIZE);
194     g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
195         "short time packet (%d < %d)", (int) ret, GST_NET_TIME_PACKET_SIZE);
196     return NULL;
197   }
198 }
199
200 /**
201  * gst_net_time_packet_send:
202  * @packet: the #GstNetTimePacket to send
203  * @socket: socket to send the time packet on
204  * @dest_address: address to send the time packet to
205  * @error: return address for a #GError, or NULL
206  *
207  * Sends a #GstNetTimePacket over a socket.
208  *
209  * MT safe.
210  *
211  * Returns: TRUE if successful, FALSE in case an error occurred.
212  */
213 gboolean
214 gst_net_time_packet_send (const GstNetTimePacket * packet,
215     GSocket * socket, GSocketAddress * dest_address, GError ** error)
216 {
217   gboolean was_blocking;
218   guint8 *buffer;
219   gssize res;
220
221   g_return_val_if_fail (packet != NULL, FALSE);
222   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
223   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (dest_address), FALSE);
224   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
225
226   was_blocking = g_socket_get_blocking (socket);
227
228   if (was_blocking)
229     g_socket_set_blocking (socket, FALSE);
230
231   /* FIXME: avoid pointless alloc/free, serialise into stack-allocated buffer */
232   buffer = gst_net_time_packet_serialize (packet);
233
234   res = g_socket_send_to (socket, dest_address, (const gchar *) buffer,
235       GST_NET_TIME_PACKET_SIZE, NULL, error);
236
237   /* datagram packets should be sent as a whole or not at all */
238   g_assert (res < 0 || res == GST_NET_TIME_PACKET_SIZE);
239
240   g_free (buffer);
241
242   if (was_blocking)
243     g_socket_set_blocking (socket, TRUE);
244
245   return (res == GST_NET_TIME_PACKET_SIZE);
246 }