2005-11-16 17:52:04 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
|
2010-05-05 15:33:51 +00:00
|
|
|
* Copyright (C) 2010 Tim-Philipp Müller <tim centricular net>
|
|
|
|
* Copyright (C) 2012 Collabora Ltd. <tim.muller@collabora.co.uk>
|
2005-11-16 17:52:04 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2005-11-24 09:44:07 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstnettimepacket
|
|
|
|
* @short_description: Helper structure to construct clock packets used
|
|
|
|
* by network clocks.
|
|
|
|
* @see_also: #GstClock, #GstNetClientClock, #GstNetTimeProvider
|
|
|
|
*
|
|
|
|
* Various functions for receiving, sending an serializing #GstNetTimePacket
|
|
|
|
* structures.
|
|
|
|
*
|
|
|
|
* Last reviewed on 2005-11-23 (0.9.5)
|
|
|
|
*/
|
2005-11-16 17:52:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2006-09-05 08:35:20 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
2006-04-11 18:43:04 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-16 17:52:04 +00:00
|
|
|
#include "gstnettimepacket.h"
|
|
|
|
|
2012-06-20 07:22:40 +00:00
|
|
|
G_DEFINE_BOXED_TYPE (GstNetTimePacket, gst_net_time_packet,
|
|
|
|
gst_net_time_packet_copy, gst_net_time_packet_free);
|
2005-11-16 17:52:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_net_time_packet_new:
|
|
|
|
* @buffer: a buffer from which to construct the packet, or NULL
|
|
|
|
*
|
|
|
|
* Creates a new #GstNetTimePacket from a buffer received over the network. The
|
|
|
|
* caller is responsible for ensuring that @buffer is at least
|
|
|
|
* #GST_NET_TIME_PACKET_SIZE bytes long.
|
|
|
|
*
|
|
|
|
* If @buffer is #NULL, the local and remote times will be set to
|
|
|
|
* #GST_CLOCK_TIME_NONE.
|
|
|
|
*
|
2012-06-20 07:22:40 +00:00
|
|
|
* MT safe. Caller owns return value (gst_net_time_packet_free to free).
|
2005-11-16 17:52:04 +00:00
|
|
|
*
|
|
|
|
* Returns: The new #GstNetTimePacket.
|
|
|
|
*/
|
|
|
|
GstNetTimePacket *
|
|
|
|
gst_net_time_packet_new (const guint8 * buffer)
|
|
|
|
{
|
|
|
|
GstNetTimePacket *ret;
|
|
|
|
|
|
|
|
g_assert (sizeof (GstClockTime) == 8);
|
|
|
|
|
|
|
|
ret = g_new0 (GstNetTimePacket, 1);
|
|
|
|
|
|
|
|
if (buffer) {
|
|
|
|
ret->local_time = GST_READ_UINT64_BE (buffer);
|
|
|
|
ret->remote_time = GST_READ_UINT64_BE (buffer + sizeof (GstClockTime));
|
|
|
|
} else {
|
|
|
|
ret->local_time = GST_CLOCK_TIME_NONE;
|
|
|
|
ret->remote_time = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-06-20 07:22:40 +00:00
|
|
|
/**
|
|
|
|
* gst_net_time_packet_free:
|
|
|
|
* @packet: the #GstNetTimePacket
|
|
|
|
*
|
|
|
|
* Free @packet.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_net_time_packet_free (GstNetTimePacket * packet)
|
|
|
|
{
|
|
|
|
g_free (packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_net_time_packet_copy:
|
|
|
|
* @packet: the #GstNetTimePacket
|
|
|
|
*
|
|
|
|
* Make a copy of @packet.
|
|
|
|
*
|
|
|
|
* Returns: a copy of @packet, free with gst_net_time_packet_free().
|
|
|
|
*/
|
|
|
|
GstNetTimePacket *
|
|
|
|
gst_net_time_packet_copy (const GstNetTimePacket * packet)
|
|
|
|
{
|
|
|
|
GstNetTimePacket *ret;
|
|
|
|
|
|
|
|
ret = g_new0 (GstNetTimePacket, 1);
|
|
|
|
ret->local_time = packet->local_time;
|
|
|
|
ret->remote_time = packet->remote_time;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-11-16 17:52:04 +00:00
|
|
|
/**
|
|
|
|
* gst_net_time_packet_serialize:
|
|
|
|
* @packet: the #GstNetTimePacket
|
|
|
|
*
|
|
|
|
* Serialized a #GstNetTimePacket into a newly-allocated sequence of
|
|
|
|
* #GST_NET_TIME_PACKET_SIZE bytes, in network byte order. The value returned is
|
|
|
|
* suitable for passing to write(2) or sendto(2) for communication over the
|
|
|
|
* network.
|
|
|
|
*
|
|
|
|
* MT safe. Caller owns return value (g_free to free).
|
|
|
|
*
|
|
|
|
* Returns: A newly allocated sequence of #GST_NET_TIME_PACKET_SIZE bytes.
|
|
|
|
*/
|
|
|
|
guint8 *
|
|
|
|
gst_net_time_packet_serialize (const GstNetTimePacket * packet)
|
|
|
|
{
|
|
|
|
guint8 *ret;
|
|
|
|
|
|
|
|
g_assert (sizeof (GstClockTime) == 8);
|
|
|
|
|
|
|
|
ret = g_new0 (guint8, GST_NET_TIME_PACKET_SIZE);
|
|
|
|
|
|
|
|
GST_WRITE_UINT64_BE (ret, packet->local_time);
|
|
|
|
GST_WRITE_UINT64_BE (ret + sizeof (GstClockTime), packet->remote_time);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_net_time_packet_receive:
|
2010-05-05 15:33:51 +00:00
|
|
|
* @socket: socket to receive the time packet on
|
2012-06-20 02:55:30 +00:00
|
|
|
* @src_address: (out): address of variable to return sender address
|
|
|
|
* @error: return address for a #GError, or NULL
|
2005-11-16 17:52:04 +00:00
|
|
|
*
|
2010-05-05 15:33:51 +00:00
|
|
|
* Receives a #GstNetTimePacket over a socket. Handles interrupted system
|
|
|
|
* calls, but otherwise returns NULL on error.
|
2005-11-16 17:52:04 +00:00
|
|
|
*
|
2010-05-05 15:33:51 +00:00
|
|
|
* Returns: (transfer full): a new #GstNetTimePacket, or NULL on error. Free
|
2012-06-20 07:22:40 +00:00
|
|
|
* with gst_net_time_packet_free() when done.
|
2005-11-16 17:52:04 +00:00
|
|
|
*/
|
|
|
|
GstNetTimePacket *
|
2010-05-05 15:33:51 +00:00
|
|
|
gst_net_time_packet_receive (GSocket * socket,
|
|
|
|
GSocketAddress ** src_address, GError ** error)
|
2005-11-16 17:52:04 +00:00
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
gchar buffer[GST_NET_TIME_PACKET_SIZE];
|
|
|
|
GError *err = NULL;
|
|
|
|
gssize ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2005-11-16 17:52:04 +00:00
|
|
|
|
|
|
|
while (TRUE) {
|
2010-05-05 15:33:51 +00:00
|
|
|
ret = g_socket_receive_from (socket, src_address, buffer,
|
|
|
|
GST_NET_TIME_PACKET_SIZE, NULL, &err);
|
|
|
|
|
2005-11-16 17:52:04 +00:00
|
|
|
if (ret < 0) {
|
2010-05-05 15:33:51 +00:00
|
|
|
if (err->code == G_IO_ERROR_WOULD_BLOCK) {
|
|
|
|
g_error_free (err);
|
|
|
|
err = NULL;
|
2005-11-16 17:52:04 +00:00
|
|
|
continue;
|
2010-05-05 15:33:51 +00:00
|
|
|
} else {
|
|
|
|
goto receive_error;
|
|
|
|
}
|
2005-11-16 17:52:04 +00:00
|
|
|
} else if (ret < GST_NET_TIME_PACKET_SIZE) {
|
|
|
|
goto short_packet;
|
|
|
|
} else {
|
2010-05-05 15:33:51 +00:00
|
|
|
return gst_net_time_packet_new ((const guint8 *) buffer);
|
2005-11-16 17:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
receive_error:
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_DEBUG ("receive error: %s", err->message);
|
|
|
|
g_propagate_error (error, err);
|
2005-11-16 17:52:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
short_packet:
|
|
|
|
{
|
2012-01-26 08:02:48 +00:00
|
|
|
GST_DEBUG ("someone sent us a short packet (%" G_GSSIZE_FORMAT " < %d)",
|
2005-11-16 17:52:04 +00:00
|
|
|
ret, GST_NET_TIME_PACKET_SIZE);
|
2010-05-05 15:33:51 +00:00
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
|
|
|
|
"short time packet (%d < %d)", (int) ret, GST_NET_TIME_PACKET_SIZE);
|
2005-11-16 17:52:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_net_time_packet_send:
|
2010-05-05 15:33:51 +00:00
|
|
|
* @packet: the #GstNetTimePacket to send
|
|
|
|
* @socket: socket to send the time packet on
|
2012-06-20 02:55:30 +00:00
|
|
|
* @dest_address: address to send the time packet to
|
|
|
|
* @error: return address for a #GError, or NULL
|
2005-11-16 17:52:04 +00:00
|
|
|
*
|
2010-05-05 15:33:51 +00:00
|
|
|
* Sends a #GstNetTimePacket over a socket.
|
2005-11-16 17:52:04 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*
|
2010-05-05 15:33:51 +00:00
|
|
|
* Returns: TRUE if successful, FALSE in case an error occured.
|
2005-11-16 17:52:04 +00:00
|
|
|
*/
|
2010-05-05 15:33:51 +00:00
|
|
|
gboolean
|
|
|
|
gst_net_time_packet_send (const GstNetTimePacket * packet,
|
|
|
|
GSocket * socket, GSocketAddress * dest_address, GError ** error)
|
2005-11-16 17:52:04 +00:00
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
gboolean was_blocking;
|
2005-11-16 17:52:04 +00:00
|
|
|
guint8 *buffer;
|
2010-05-05 15:33:51 +00:00
|
|
|
gssize res;
|
2005-11-16 17:52:04 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
g_return_val_if_fail (packet != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_SOCKET_ADDRESS (dest_address), FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2005-11-16 17:52:04 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
was_blocking = g_socket_get_blocking (socket);
|
2006-04-11 18:43:04 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
if (was_blocking)
|
|
|
|
g_socket_set_blocking (socket, FALSE);
|
|
|
|
|
|
|
|
/* FIXME: avoid pointless alloc/free, serialise into stack-allocated buffer */
|
2005-11-16 17:52:04 +00:00
|
|
|
buffer = gst_net_time_packet_serialize (packet);
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
res = g_socket_send_to (socket, dest_address, (const gchar *) buffer,
|
|
|
|
GST_NET_TIME_PACKET_SIZE, NULL, error);
|
2007-10-31 22:01:03 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
/* datagram packets should be sent as a whole or not at all */
|
|
|
|
g_assert (res < 0 || res == GST_NET_TIME_PACKET_SIZE);
|
2005-11-16 17:52:04 +00:00
|
|
|
|
2005-11-16 18:37:57 +00:00
|
|
|
g_free (buffer);
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
if (was_blocking)
|
|
|
|
g_socket_set_blocking (socket, TRUE);
|
|
|
|
|
|
|
|
return (res == GST_NET_TIME_PACKET_SIZE);
|
2005-11-16 17:52:04 +00:00
|
|
|
}
|