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>
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.
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.
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.
22 * SECTION:gstnettimepacket
23 * @short_description: Helper structure to construct clock packets used
25 * @see_also: #GstClock, #GstNetClientClock, #GstNetTimeProvider
27 * Various functions for receiving, sending an serializing #GstNetTimePacket
42 #include "gstnettimepacket.h"
44 G_DEFINE_BOXED_TYPE (GstNetTimePacket, gst_net_time_packet,
45 gst_net_time_packet_copy, gst_net_time_packet_free);
48 * gst_net_time_packet_new:
49 * @buffer: (array): a buffer from which to construct the packet, or NULL
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.
55 * If @buffer is #NULL, the local and remote times will be set to
56 * #GST_CLOCK_TIME_NONE.
58 * MT safe. Caller owns return value (gst_net_time_packet_free to free).
60 * Returns: The new #GstNetTimePacket.
63 gst_net_time_packet_new (const guint8 * buffer)
65 GstNetTimePacket *ret;
67 g_assert (sizeof (GstClockTime) == 8);
69 ret = g_new0 (GstNetTimePacket, 1);
72 ret->local_time = GST_READ_UINT64_BE (buffer);
73 ret->remote_time = GST_READ_UINT64_BE (buffer + sizeof (GstClockTime));
75 ret->local_time = GST_CLOCK_TIME_NONE;
76 ret->remote_time = GST_CLOCK_TIME_NONE;
83 * gst_net_time_packet_free:
84 * @packet: the #GstNetTimePacket
89 gst_net_time_packet_free (GstNetTimePacket * packet)
95 * gst_net_time_packet_copy:
96 * @packet: the #GstNetTimePacket
98 * Make a copy of @packet.
100 * Returns: a copy of @packet, free with gst_net_time_packet_free().
103 gst_net_time_packet_copy (const GstNetTimePacket * packet)
105 GstNetTimePacket *ret;
107 ret = g_new0 (GstNetTimePacket, 1);
108 ret->local_time = packet->local_time;
109 ret->remote_time = packet->remote_time;
115 * gst_net_time_packet_serialize:
116 * @packet: the #GstNetTimePacket
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
123 * MT safe. Caller owns return value (g_free to free).
125 * Returns: A newly allocated sequence of #GST_NET_TIME_PACKET_SIZE bytes.
128 gst_net_time_packet_serialize (const GstNetTimePacket * packet)
132 g_assert (sizeof (GstClockTime) == 8);
134 ret = g_new0 (guint8, GST_NET_TIME_PACKET_SIZE);
136 GST_WRITE_UINT64_BE (ret, packet->local_time);
137 GST_WRITE_UINT64_BE (ret + sizeof (GstClockTime), packet->remote_time);
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
148 * Receives a #GstNetTimePacket over a socket. Handles interrupted system
149 * calls, but otherwise returns NULL on error.
151 * Returns: (transfer full): a new #GstNetTimePacket, or NULL on error. Free
152 * with gst_net_time_packet_free() when done.
155 gst_net_time_packet_receive (GSocket * socket,
156 GSocketAddress ** src_address, GError ** error)
158 gchar buffer[GST_NET_TIME_PACKET_SIZE];
162 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
163 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
166 ret = g_socket_receive_from (socket, src_address, buffer,
167 GST_NET_TIME_PACKET_SIZE, NULL, &err);
170 if (err->code == G_IO_ERROR_WOULD_BLOCK) {
177 } else if (ret < GST_NET_TIME_PACKET_SIZE) {
180 return gst_net_time_packet_new ((const guint8 *) buffer);
186 GST_DEBUG ("receive error: %s", err->message);
187 g_propagate_error (error, err);
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);
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
207 * Sends a #GstNetTimePacket over a socket.
211 * Returns: TRUE if successful, FALSE in case an error occurred.
214 gst_net_time_packet_send (const GstNetTimePacket * packet,
215 GSocket * socket, GSocketAddress * dest_address, GError ** error)
217 gboolean was_blocking;
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);
226 was_blocking = g_socket_get_blocking (socket);
229 g_socket_set_blocking (socket, FALSE);
231 /* FIXME: avoid pointless alloc/free, serialise into stack-allocated buffer */
232 buffer = gst_net_time_packet_serialize (packet);
234 res = g_socket_send_to (socket, dest_address, (const gchar *) buffer,
235 GST_NET_TIME_PACKET_SIZE, NULL, error);
237 /* datagram packets should be sent as a whole or not at all */
238 g_assert (res < 0 || res == GST_NET_TIME_PACKET_SIZE);
243 g_socket_set_blocking (socket, TRUE);
245 return (res == GST_NET_TIME_PACKET_SIZE);