2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
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.
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.
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.
20 * SECTION:gstnettimepacket
21 * @short_description: Helper structure to construct clock packets used
23 * @see_also: #GstClock, #GstNetClientClock, #GstNetTimeProvider
25 * Various functions for receiving, sending an serializing #GstNetTimePacket
28 * Last reviewed on 2005-11-23 (0.9.5)
40 #include "gstnettimepacket.h"
44 * gst_net_time_packet_new:
45 * @buffer: a buffer from which to construct the packet, or NULL
47 * Creates a new #GstNetTimePacket from a buffer received over the network. The
48 * caller is responsible for ensuring that @buffer is at least
49 * #GST_NET_TIME_PACKET_SIZE bytes long.
51 * If @buffer is #NULL, the local and remote times will be set to
52 * #GST_CLOCK_TIME_NONE.
54 * MT safe. Caller owns return value (g_free to free).
56 * Returns: The new #GstNetTimePacket.
59 gst_net_time_packet_new (const guint8 * buffer)
61 GstNetTimePacket *ret;
63 g_assert (sizeof (GstClockTime) == 8);
65 ret = g_new0 (GstNetTimePacket, 1);
68 ret->local_time = GST_READ_UINT64_BE (buffer);
69 ret->remote_time = GST_READ_UINT64_BE (buffer + sizeof (GstClockTime));
71 ret->local_time = GST_CLOCK_TIME_NONE;
72 ret->remote_time = GST_CLOCK_TIME_NONE;
79 * gst_net_time_packet_serialize:
80 * @packet: the #GstNetTimePacket
82 * Serialized a #GstNetTimePacket into a newly-allocated sequence of
83 * #GST_NET_TIME_PACKET_SIZE bytes, in network byte order. The value returned is
84 * suitable for passing to write(2) or sendto(2) for communication over the
87 * MT safe. Caller owns return value (g_free to free).
89 * Returns: A newly allocated sequence of #GST_NET_TIME_PACKET_SIZE bytes.
92 gst_net_time_packet_serialize (const GstNetTimePacket * packet)
96 g_assert (sizeof (GstClockTime) == 8);
98 ret = g_new0 (guint8, GST_NET_TIME_PACKET_SIZE);
100 GST_WRITE_UINT64_BE (ret, packet->local_time);
101 GST_WRITE_UINT64_BE (ret + sizeof (GstClockTime), packet->remote_time);
107 * gst_net_time_packet_receive:
108 * @fd: a file descriptor created by socket(2)
109 * @addr: a pointer to a sockaddr to hold the address of the sender
110 * @len: a pointer to the size of the data pointed to by @addr
112 * Receives a #GstNetTimePacket over a socket. Handles interrupted system calls,
113 * but otherwise returns NULL on error. See recvfrom(2) for more information on
114 * how to interpret @sockaddr.
116 * MT safe. Caller owns return value (g_free to free).
118 * Returns: The new #GstNetTimePacket.
121 gst_net_time_packet_receive (gint fd, struct sockaddr * addr, socklen_t * len)
123 guint8 buffer[GST_NET_TIME_PACKET_SIZE];
127 ret = recvfrom (fd, buffer, GST_NET_TIME_PACKET_SIZE,
128 0, (struct sockaddr *) addr, len);
130 if (errno != EAGAIN && errno != EINTR)
134 } else if (ret < GST_NET_TIME_PACKET_SIZE) {
137 return gst_net_time_packet_new (buffer);
143 GST_DEBUG ("receive error %d: %s (%d)", ret, g_strerror (errno), errno);
148 GST_DEBUG ("someone sent us a short packet (%d < %d)",
149 ret, GST_NET_TIME_PACKET_SIZE);
155 * gst_net_time_packet_send:
156 * @packet: the #GstNetTimePacket
157 * @fd: a file descriptor created by socket(2)
158 * @addr: a pointer to a sockaddr to hold the address of the sender
159 * @len: the size of the data pointed to by @addr
161 * Sends a #GstNetTimePacket over a socket. Essentially a thin wrapper around
162 * sendto(2) and gst_net_time_packet_serialize().
166 * Returns: The return value of sendto(2).
169 gst_net_time_packet_send (const GstNetTimePacket * packet, gint fd,
170 struct sockaddr * addr, socklen_t len)
176 gint ret, send_flags;
178 g_return_val_if_fail (packet != NULL, -EINVAL);
182 fdflags = fcntl (fd, F_GETFL);
183 fcntl (fd, F_SETFL, fdflags | O_NONBLOCK);
185 send_flags = MSG_DONTWAIT;
188 buffer = gst_net_time_packet_serialize (packet);
190 ret = sendto (fd, buffer, GST_NET_TIME_PACKET_SIZE, send_flags, addr, len);
193 fcntl (fd, F_SETFL, fdflags);