2005-11-17 17:55:17 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2005 Wim Taymans <wim@fluendo.com>
|
|
|
|
* 2005 Andy Wingo <wingo@pobox.com>
|
2010-05-05 15:33:51 +00:00
|
|
|
* Copyright (C) 2012 Collabora Ltd. <tim.muller@collabora.co.uk>
|
2015-06-06 21:05:32 +00:00
|
|
|
* Copyright (C) 2015 Sebastian Dröge <sebastian@centricular.com>
|
2005-11-17 17:55:17 +00:00
|
|
|
*
|
|
|
|
* gstnetclientclock.h: clock that synchronizes itself to a time provider over
|
|
|
|
* the network
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2005-11-17 17:55:17 +00:00
|
|
|
*/
|
2005-11-24 09:44:07 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstnetclientclock
|
|
|
|
* @short_description: Special clock that synchronizes to a remote time
|
|
|
|
* provider.
|
|
|
|
* @see_also: #GstClock, #GstNetTimeProvider, #GstPipeline
|
|
|
|
*
|
2015-06-06 19:43:05 +00:00
|
|
|
* #GstNetClientClock implements a custom #GstClock that synchronizes its time
|
|
|
|
* to a remote time provider such as #GstNetTimeProvider. #GstNtpClock
|
|
|
|
* implements a #GstClock that synchronizes its time to a remote NTPv4 server.
|
2005-11-24 09:44:07 +00:00
|
|
|
*
|
2015-06-06 19:43:05 +00:00
|
|
|
* A new clock is created with gst_net_client_clock_new() or
|
|
|
|
* gst_ntp_clock_new(), which takes the address and port of the remote time
|
|
|
|
* provider along with a name and an initial time.
|
2005-11-24 09:44:07 +00:00
|
|
|
*
|
|
|
|
* This clock will poll the time provider and will update its calibration
|
|
|
|
* parameters based on the local and remote observations.
|
|
|
|
*
|
2013-11-26 10:56:46 +00:00
|
|
|
* The "round-trip" property limits the maximum round trip packets can take.
|
|
|
|
*
|
2005-11-24 09:44:07 +00:00
|
|
|
* Various parameters of the clock can be configured with the parent #GstClock
|
|
|
|
* "timeout", "window-size" and "window-threshold" object properties.
|
|
|
|
*
|
2015-06-06 19:43:05 +00:00
|
|
|
* A #GstNetClientClock and #GstNtpClock is typically set on a #GstPipeline with
|
2005-11-24 09:44:07 +00:00
|
|
|
* gst_pipeline_use_clock().
|
2015-01-10 10:42:00 +00:00
|
|
|
*
|
|
|
|
* If you set a #GstBus on the clock via the "bus" object property, it will
|
2015-01-21 11:44:59 +00:00
|
|
|
* send @GST_MESSAGE_ELEMENT messages with an attached #GstStructure containing
|
2015-01-10 10:42:00 +00:00
|
|
|
* statistics about clock accuracy and network traffic.
|
2005-11-24 09:44:07 +00:00
|
|
|
*/
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstnettimepacket.h"
|
2015-06-06 19:43:05 +00:00
|
|
|
#include "gstntppacket.h"
|
2005-11-17 17:55:17 +00:00
|
|
|
#include "gstnetclientclock.h"
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
#include <gio/gio.h>
|
2007-10-31 22:01:03 +00:00
|
|
|
|
2015-06-06 12:31:16 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-06-23 13:16:46 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (ncc_debug);
|
2005-11-17 17:55:17 +00:00
|
|
|
#define GST_CAT_DEFAULT (ncc_debug)
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstClock *clock; /* GstNetClientInternalClock */
|
|
|
|
|
|
|
|
GList *clocks; /* GstNetClientClocks */
|
2016-01-04 15:18:07 +00:00
|
|
|
|
|
|
|
GstClockID remove_id;
|
2016-01-04 14:31:23 +00:00
|
|
|
} ClockCache;
|
|
|
|
|
|
|
|
G_LOCK_DEFINE_STATIC (clocks_lock);
|
|
|
|
static GList *clocks = NULL;
|
|
|
|
|
|
|
|
#define GST_TYPE_NET_CLIENT_INTERNAL_CLOCK \
|
|
|
|
(gst_net_client_internal_clock_get_type())
|
|
|
|
#define GST_NET_CLIENT_INTERNAL_CLOCK(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_NET_CLIENT_INTERNAL_CLOCK,GstNetClientInternalClock))
|
|
|
|
#define GST_NET_CLIENT_INTERNAL_CLOCK_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_NET_CLIENT_INTERNAL_CLOCK,GstNetClientInternalClockClass))
|
|
|
|
#define GST_IS_NET_CLIENT_INTERNAL_CLOCK(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_NET_CLIENT_INTERNAL_CLOCK))
|
|
|
|
#define GST_IS_NET_CLIENT_INTERNAL_CLOCK_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_NET_CLIENT_INTERNAL_CLOCK))
|
|
|
|
|
|
|
|
typedef struct _GstNetClientInternalClock GstNetClientInternalClock;
|
|
|
|
typedef struct _GstNetClientInternalClockClass GstNetClientInternalClockClass;
|
|
|
|
|
|
|
|
G_GNUC_INTERNAL GType gst_net_client_internal_clock_get_type (void);
|
|
|
|
|
2005-12-06 19:29:15 +00:00
|
|
|
#define DEFAULT_ADDRESS "127.0.0.1"
|
|
|
|
#define DEFAULT_PORT 5637
|
|
|
|
#define DEFAULT_TIMEOUT GST_SECOND
|
2013-11-26 10:56:46 +00:00
|
|
|
#define DEFAULT_ROUNDTRIP_LIMIT GST_SECOND
|
2015-01-15 11:32:28 +00:00
|
|
|
/* Minimum timeout will be immediately (ie, as fast as one RTT), but no
|
|
|
|
* more often than 1/20th second (arbitrarily, to spread observations a little) */
|
|
|
|
#define DEFAULT_MINIMUM_UPDATE_INTERVAL (GST_SECOND / 20)
|
2015-06-06 16:42:18 +00:00
|
|
|
#define DEFAULT_BASE_TIME 0
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2015-02-06 17:22:22 +00:00
|
|
|
/* Maximum number of clock updates we can skip before updating */
|
|
|
|
#define MAX_SKIPPED_UPDATES 5
|
|
|
|
|
2015-06-06 12:31:16 +00:00
|
|
|
#define MEDIAN_PRE_FILTERING_WINDOW 9
|
|
|
|
|
2005-11-17 17:55:17 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ADDRESS,
|
2013-11-26 10:56:46 +00:00
|
|
|
PROP_PORT,
|
2015-01-10 10:42:00 +00:00
|
|
|
PROP_ROUNDTRIP_LIMIT,
|
2015-01-15 11:32:28 +00:00
|
|
|
PROP_MINIMUM_UPDATE_INTERVAL,
|
2015-06-06 16:42:18 +00:00
|
|
|
PROP_BUS,
|
2015-06-08 15:04:55 +00:00
|
|
|
PROP_BASE_TIME,
|
2016-01-04 14:31:23 +00:00
|
|
|
PROP_INTERNAL_CLOCK,
|
|
|
|
PROP_IS_NTP
|
2005-11-17 17:55:17 +00:00
|
|
|
};
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
struct _GstNetClientInternalClock
|
Use a private stuct to not break ABI.
Original commit message from CVS:
* docs/libs/gstreamer-libs-sections.txt:
* libs/gst/net/gstnetclientclock.c:
(gst_net_client_clock_class_init), (gst_net_client_clock_init),
(gst_net_client_clock_finalize), (gst_net_client_clock_do_select),
(gst_net_client_clock_thread), (gst_net_client_clock_start),
(gst_net_client_clock_stop), (gst_net_client_clock_new):
* libs/gst/net/gstnetclientclock.h:
* libs/gst/net/gstnettimeprovider.c:
(gst_net_time_provider_class_init), (gst_net_time_provider_init),
(gst_net_time_provider_finalize), (gst_net_time_provider_thread),
(gst_net_time_provider_start), (gst_net_time_provider_stop),
(gst_net_time_provider_new):
* libs/gst/net/gstnettimeprovider.h:
Use a private stuct to not break ABI.
2008-02-27 19:01:12 +00:00
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstSystemClock clock;
|
2015-06-08 15:04:55 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
GSocket *socket;
|
|
|
|
GSocketAddress *servaddr;
|
|
|
|
GCancellable *cancel;
|
2015-05-19 13:34:04 +00:00
|
|
|
gboolean made_cancel_fd;
|
2010-05-05 15:33:51 +00:00
|
|
|
|
|
|
|
GstClockTime timeout_expiration;
|
2013-11-26 10:56:46 +00:00
|
|
|
GstClockTime roundtrip_limit;
|
2013-11-25 15:17:36 +00:00
|
|
|
GstClockTime rtt_avg;
|
2015-01-15 11:32:28 +00:00
|
|
|
GstClockTime minimum_update_interval;
|
2015-06-06 19:43:05 +00:00
|
|
|
GstClockTime last_remote_poll_interval;
|
2015-02-06 17:22:22 +00:00
|
|
|
guint skipped_updates;
|
2015-06-06 12:31:16 +00:00
|
|
|
GstClockTime last_rtts[MEDIAN_PRE_FILTERING_WINDOW];
|
|
|
|
gint last_rtts_missing;
|
2010-05-05 15:33:51 +00:00
|
|
|
|
|
|
|
gchar *address;
|
|
|
|
gint port;
|
2016-01-04 14:31:23 +00:00
|
|
|
gboolean is_ntp;
|
2015-01-10 10:42:00 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
/* Protected by OBJECT_LOCK */
|
|
|
|
GList *busses;
|
|
|
|
};
|
2015-06-06 19:43:05 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
struct _GstNetClientInternalClockClass
|
|
|
|
{
|
|
|
|
GstSystemClockClass parent_class;
|
Use a private stuct to not break ABI.
Original commit message from CVS:
* docs/libs/gstreamer-libs-sections.txt:
* libs/gst/net/gstnetclientclock.c:
(gst_net_client_clock_class_init), (gst_net_client_clock_init),
(gst_net_client_clock_finalize), (gst_net_client_clock_do_select),
(gst_net_client_clock_thread), (gst_net_client_clock_start),
(gst_net_client_clock_stop), (gst_net_client_clock_new):
* libs/gst/net/gstnetclientclock.h:
* libs/gst/net/gstnettimeprovider.c:
(gst_net_time_provider_class_init), (gst_net_time_provider_init),
(gst_net_time_provider_finalize), (gst_net_time_provider_thread),
(gst_net_time_provider_start), (gst_net_time_provider_stop),
(gst_net_time_provider_new):
* libs/gst/net/gstnettimeprovider.h:
Use a private stuct to not break ABI.
2008-02-27 19:01:12 +00:00
|
|
|
};
|
|
|
|
|
2011-04-18 15:33:34 +00:00
|
|
|
#define _do_init \
|
2005-11-17 17:55:17 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (ncc_debug, "netclock", 0, "Network client clock");
|
2016-01-04 14:31:23 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstNetClientInternalClock,
|
|
|
|
gst_net_client_internal_clock, GST_TYPE_SYSTEM_CLOCK, _do_init);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
static void gst_net_client_internal_clock_finalize (GObject * object);
|
|
|
|
static void gst_net_client_internal_clock_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_net_client_internal_clock_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_net_client_internal_clock_constructed (GObject * object);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
static gboolean gst_net_client_internal_clock_start (GstNetClientInternalClock *
|
|
|
|
self);
|
|
|
|
static void gst_net_client_internal_clock_stop (GstNetClientInternalClock *
|
|
|
|
self);
|
2015-06-08 15:04:55 +00:00
|
|
|
|
2005-11-17 17:55:17 +00:00
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_class_init (GstNetClientInternalClockClass *
|
|
|
|
klass)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
2006-05-11 18:10:34 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
Use a private stuct to not break ABI.
Original commit message from CVS:
* docs/libs/gstreamer-libs-sections.txt:
* libs/gst/net/gstnetclientclock.c:
(gst_net_client_clock_class_init), (gst_net_client_clock_init),
(gst_net_client_clock_finalize), (gst_net_client_clock_do_select),
(gst_net_client_clock_thread), (gst_net_client_clock_start),
(gst_net_client_clock_stop), (gst_net_client_clock_new):
* libs/gst/net/gstnetclientclock.h:
* libs/gst/net/gstnettimeprovider.c:
(gst_net_time_provider_class_init), (gst_net_time_provider_init),
(gst_net_time_provider_finalize), (gst_net_time_provider_thread),
(gst_net_time_provider_start), (gst_net_time_provider_stop),
(gst_net_time_provider_new):
* libs/gst/net/gstnettimeprovider.h:
Use a private stuct to not break ABI.
2008-02-27 19:01:12 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
gobject_class->finalize = gst_net_client_internal_clock_finalize;
|
|
|
|
gobject_class->get_property = gst_net_client_internal_clock_get_property;
|
|
|
|
gobject_class->set_property = gst_net_client_internal_clock_set_property;
|
|
|
|
gobject_class->constructed = gst_net_client_internal_clock_constructed;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ADDRESS,
|
|
|
|
g_param_spec_string ("address", "address",
|
2010-05-05 15:33:51 +00:00
|
|
|
"The IP address of the machine providing a time server",
|
2015-06-06 16:42:18 +00:00
|
|
|
DEFAULT_ADDRESS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2006-05-11 18:10:34 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_PORT,
|
2005-11-17 17:55:17 +00:00
|
|
|
g_param_spec_int ("port", "port",
|
2005-11-18 11:57:30 +00:00
|
|
|
"The port on which the remote server is listening", 0, G_MAXUINT16,
|
2015-06-06 16:42:18 +00:00
|
|
|
DEFAULT_PORT,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2016-01-04 14:31:23 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_IS_NTP,
|
|
|
|
g_param_spec_boolean ("is-ntp", "Is NTP",
|
|
|
|
"The clock is using the NTPv4 protocol", FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_init (GstNetClientInternalClock * self)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2015-06-08 15:10:56 +00:00
|
|
|
GST_OBJECT_FLAG_SET (self, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC);
|
2015-06-08 15:04:55 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->port = DEFAULT_PORT;
|
|
|
|
self->address = g_strdup (DEFAULT_ADDRESS);
|
|
|
|
self->is_ntp = FALSE;
|
2015-06-08 15:04:55 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_set_timeout (GST_CLOCK (self), DEFAULT_TIMEOUT);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->thread = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->servaddr = NULL;
|
|
|
|
self->rtt_avg = GST_CLOCK_TIME_NONE;
|
|
|
|
self->roundtrip_limit = DEFAULT_ROUNDTRIP_LIMIT;
|
|
|
|
self->minimum_update_interval = DEFAULT_MINIMUM_UPDATE_INTERVAL;
|
|
|
|
self->last_remote_poll_interval = GST_CLOCK_TIME_NONE;
|
|
|
|
self->skipped_updates = 0;
|
|
|
|
self->last_rtts_missing = MEDIAN_PRE_FILTERING_WINDOW;
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_finalize (GObject * object)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstNetClientInternalClock *self = GST_NET_CLIENT_INTERNAL_CLOCK (object);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->thread) {
|
|
|
|
gst_net_client_internal_clock_stop (self);
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_free (self->address);
|
|
|
|
self->address = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->servaddr != NULL) {
|
|
|
|
g_object_unref (self->servaddr);
|
|
|
|
self->servaddr = NULL;
|
2010-05-05 15:33:51 +00:00
|
|
|
}
|
2006-09-05 08:35:20 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->socket != NULL) {
|
2016-01-15 08:48:32 +00:00
|
|
|
if (!g_socket_close (self->socket, NULL))
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to close socket");
|
2016-01-04 14:31:23 +00:00
|
|
|
g_object_unref (self->socket);
|
|
|
|
self->socket = NULL;
|
2015-01-10 10:42:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_warn_if_fail (self->busses == NULL);
|
2015-06-08 15:04:55 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
G_OBJECT_CLASS (gst_net_client_internal_clock_parent_class)->finalize
|
|
|
|
(object);
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_set_property (GObject * object, guint prop_id,
|
2005-11-17 17:55:17 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstNetClientInternalClock *self = GST_NET_CLIENT_INTERNAL_CLOCK (object);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ADDRESS:
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
2016-01-04 14:31:23 +00:00
|
|
|
g_free (self->address);
|
|
|
|
self->address = g_value_dup_string (value);
|
|
|
|
if (self->address == NULL)
|
|
|
|
self->address = g_strdup (DEFAULT_ADDRESS);
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2005-11-17 17:55:17 +00:00
|
|
|
break;
|
|
|
|
case PROP_PORT:
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
2016-01-04 14:31:23 +00:00
|
|
|
self->port = g_value_get_int (value);
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
2016-01-04 14:31:23 +00:00
|
|
|
case PROP_IS_NTP:
|
2015-01-15 11:32:28 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
2016-01-04 14:31:23 +00:00
|
|
|
self->is_ntp = g_value_get_boolean (value);
|
2015-01-15 11:32:28 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
2005-11-17 17:55:17 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_get_property (GObject * object, guint prop_id,
|
2005-11-17 17:55:17 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstNetClientInternalClock *self = GST_NET_CLIENT_INTERNAL_CLOCK (object);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ADDRESS:
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
2016-01-04 14:31:23 +00:00
|
|
|
g_value_set_string (value, self->address);
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2005-11-17 17:55:17 +00:00
|
|
|
break;
|
|
|
|
case PROP_PORT:
|
2016-01-04 14:31:23 +00:00
|
|
|
g_value_set_int (value, self->port);
|
2015-01-10 10:42:00 +00:00
|
|
|
break;
|
2016-01-04 14:31:23 +00:00
|
|
|
case PROP_IS_NTP:
|
|
|
|
g_value_set_boolean (value, self->is_ntp);
|
2015-06-08 15:04:55 +00:00
|
|
|
break;
|
2005-11-17 17:55:17 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-06 16:42:18 +00:00
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_constructed (GObject * object)
|
2015-06-06 16:42:18 +00:00
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstNetClientInternalClock *self = GST_NET_CLIENT_INTERNAL_CLOCK (object);
|
2015-06-06 16:42:18 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
G_OBJECT_CLASS (gst_net_client_internal_clock_parent_class)->constructed
|
|
|
|
(object);
|
2015-06-06 16:42:18 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (!gst_net_client_internal_clock_start (self)) {
|
2015-06-06 16:42:18 +00:00
|
|
|
g_warning ("failed to start clock '%s'", GST_OBJECT_NAME (self));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all systems go, cap'n */
|
|
|
|
}
|
|
|
|
|
2015-06-06 12:31:16 +00:00
|
|
|
static gint
|
|
|
|
compare_clock_time (const GstClockTime * a, const GstClockTime * b)
|
|
|
|
{
|
|
|
|
if (*a < *b)
|
|
|
|
return -1;
|
|
|
|
else if (*a > *b)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 17:55:17 +00:00
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_observe_times (GstNetClientInternalClock * self,
|
2015-06-06 18:39:47 +00:00
|
|
|
GstClockTime local_1, GstClockTime remote_1, GstClockTime remote_2,
|
|
|
|
GstClockTime local_2)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2015-02-06 17:22:22 +00:00
|
|
|
GstClockTime current_timeout = 0;
|
2015-06-06 18:39:47 +00:00
|
|
|
GstClockTime local_avg, remote_avg;
|
gst/base/gstbasesink.*: No need to store the clock, the parent element class already has it.
Original commit message from CVS:
* gst/base/gstbasesink.c: (gst_base_sink_class_init),
(gst_base_sink_wait), (gst_base_sink_do_sync),
(gst_base_sink_handle_event):
* gst/base/gstbasesink.h:
No need to store the clock, the parent element class already
has it.
* gst/gstbin.c: (gst_bin_set_clock_func), (gst_bin_add_func):
Updates for clock_set returning a gboolean
* gst/gstclock.c: (gst_clock_entry_new), (gst_clock_id_wait),
(gst_clock_id_wait_async), (gst_clock_class_init),
(gst_clock_init), (gst_clock_finalize),
(gst_clock_get_internal_time), (gst_clock_get_time),
(gst_clock_slave_callback), (gst_clock_set_master),
(gst_clock_get_master), (do_linear_regression),
(gst_clock_add_observation), (gst_clock_set_property),
(gst_clock_get_property):
* gst/gstclock.h:
Implement master/slave. When setting a clock as a slave, a
periodic timeout is scheduled to sample master and slave times.
Then the slave clock is recalibrated to match offset and rate
of the master clock.
Update logging a bit.
Add flag so that a clock can state that is cannot be slaved to
another clock.
* gst/gstelement.c: (gst_element_set_clock):
* gst/gstelement.h:
The set_clock returns a gboolean for when an element cannot
deal with the selected clock in the pipeline.
* gst/gstpipeline.c: (gst_pipeline_change_state),
(gst_pipeline_set_clock):
* gst/gstpipeline.h:
Handle the case where the selected clock cannot be set on
the pipeline.
* gst/net/gstnetclientclock.c: (gst_net_client_clock_class_init),
(gst_net_client_clock_init), (gst_net_client_clock_finalize),
(gst_net_client_clock_set_property),
(gst_net_client_clock_get_property),
(gst_net_client_clock_observe_times):
* gst/net/gstnetclientclock.h:
Use regression code in GstClock parent, remove duplicated
functionality.
2005-11-22 18:28:44 +00:00
|
|
|
gdouble r_squared;
|
|
|
|
GstClock *clock;
|
2015-01-15 11:32:28 +00:00
|
|
|
GstClockTime rtt, rtt_limit, min_update_interval;
|
2015-01-10 10:42:00 +00:00
|
|
|
/* Use for discont tracking */
|
|
|
|
GstClockTime time_before = 0;
|
|
|
|
GstClockTime min_guess = 0;
|
2015-01-15 11:32:28 +00:00
|
|
|
GstClockTimeDiff time_discont = 0;
|
2015-02-06 17:22:22 +00:00
|
|
|
gboolean synched, now_synched;
|
2015-01-10 10:42:00 +00:00
|
|
|
GstClockTime internal_time, external_time, rate_num, rate_den;
|
2015-02-06 17:22:22 +00:00
|
|
|
GstClockTime orig_internal_time, orig_external_time, orig_rate_num,
|
|
|
|
orig_rate_den;
|
2015-01-15 11:32:28 +00:00
|
|
|
GstClockTime max_discont;
|
2015-06-06 12:31:16 +00:00
|
|
|
GstClockTime last_rtts[MEDIAN_PRE_FILTERING_WINDOW];
|
|
|
|
GstClockTime median;
|
|
|
|
gint i;
|
2015-01-10 10:42:00 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
2016-01-04 14:31:23 +00:00
|
|
|
rtt_limit = self->roundtrip_limit;
|
2015-06-06 19:43:05 +00:00
|
|
|
|
2016-03-03 16:14:35 +00:00
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"local1 %" G_GUINT64_FORMAT " remote1 %" G_GUINT64_FORMAT " remote2 %"
|
|
|
|
G_GUINT64_FORMAT " local2 %" G_GUINT64_FORMAT, local_1, remote_1,
|
|
|
|
remote_2, local_2);
|
|
|
|
|
2015-06-06 19:43:05 +00:00
|
|
|
/* If the server told us a poll interval and it's bigger than the
|
|
|
|
* one configured via the property, use the server's */
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->last_remote_poll_interval != GST_CLOCK_TIME_NONE &&
|
|
|
|
self->last_remote_poll_interval > self->minimum_update_interval)
|
|
|
|
min_update_interval = self->last_remote_poll_interval;
|
2015-06-06 19:43:05 +00:00
|
|
|
else
|
2016-01-04 14:31:23 +00:00
|
|
|
min_update_interval = self->minimum_update_interval;
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2013-11-26 10:56:46 +00:00
|
|
|
if (local_2 < local_1) {
|
|
|
|
GST_LOG_OBJECT (self, "Dropping observation: receive time %" GST_TIME_FORMAT
|
|
|
|
" < send time %" GST_TIME_FORMAT, GST_TIME_ARGS (local_1),
|
|
|
|
GST_TIME_ARGS (local_2));
|
2005-11-17 17:55:17 +00:00
|
|
|
goto bogus_observation;
|
2013-11-26 10:56:46 +00:00
|
|
|
}
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2015-06-06 18:39:47 +00:00
|
|
|
if (remote_2 < remote_1) {
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Dropping observation: remote receive time %" GST_TIME_FORMAT
|
|
|
|
" < send time %" GST_TIME_FORMAT, GST_TIME_ARGS (remote_1),
|
|
|
|
GST_TIME_ARGS (remote_2));
|
|
|
|
goto bogus_observation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The round trip time is (assuming symmetric path delays)
|
|
|
|
* delta = (local_2 - local_1) - (remote_2 - remote_1)
|
|
|
|
*/
|
|
|
|
|
|
|
|
rtt = GST_CLOCK_DIFF (local_1, local_2) - GST_CLOCK_DIFF (remote_1, remote_2);
|
2013-11-26 10:56:46 +00:00
|
|
|
|
2015-01-10 10:42:00 +00:00
|
|
|
if ((rtt_limit > 0) && (rtt > rtt_limit)) {
|
2013-11-26 10:56:46 +00:00
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Dropping observation: RTT %" GST_TIME_FORMAT " > limit %"
|
2015-01-10 10:42:00 +00:00
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (rtt), GST_TIME_ARGS (rtt_limit));
|
2013-11-26 10:56:46 +00:00
|
|
|
goto bogus_observation;
|
|
|
|
}
|
2013-11-25 15:17:36 +00:00
|
|
|
|
2015-06-06 12:31:16 +00:00
|
|
|
for (i = 1; i < MEDIAN_PRE_FILTERING_WINDOW; i++)
|
2016-01-04 14:31:23 +00:00
|
|
|
self->last_rtts[i - 1] = self->last_rtts[i];
|
|
|
|
self->last_rtts[i - 1] = rtt;
|
2015-06-06 12:31:16 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->last_rtts_missing) {
|
|
|
|
self->last_rtts_missing--;
|
2015-06-06 12:31:16 +00:00
|
|
|
} else {
|
2016-01-04 14:31:23 +00:00
|
|
|
memcpy (&last_rtts, &self->last_rtts, sizeof (last_rtts));
|
2015-06-06 12:31:16 +00:00
|
|
|
g_qsort_with_data (&last_rtts,
|
|
|
|
MEDIAN_PRE_FILTERING_WINDOW, sizeof (GstClockTime),
|
|
|
|
(GCompareDataFunc) compare_clock_time, NULL);
|
|
|
|
|
|
|
|
median = last_rtts[MEDIAN_PRE_FILTERING_WINDOW / 2];
|
|
|
|
|
|
|
|
/* FIXME: We might want to use something else here, like only allowing
|
|
|
|
* things in the interquartile range, or also filtering away delays that
|
|
|
|
* are too small compared to the median. This here worked well enough
|
|
|
|
* in tests so far.
|
|
|
|
*/
|
|
|
|
if (rtt > 2 * median) {
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Dropping observation, long RTT %" GST_TIME_FORMAT " > 2 * median %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (rtt), GST_TIME_ARGS (median));
|
|
|
|
goto bogus_observation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 15:17:36 +00:00
|
|
|
/* Track an average round trip time, for a bit of smoothing */
|
|
|
|
/* Always update before discarding a sample, so genuine changes in
|
|
|
|
* the network get picked up, eventually */
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->rtt_avg == GST_CLOCK_TIME_NONE)
|
|
|
|
self->rtt_avg = rtt;
|
|
|
|
else if (rtt < self->rtt_avg) /* Shorter RTTs carry more weight than longer */
|
|
|
|
self->rtt_avg = (3 * self->rtt_avg + rtt) / 4;
|
2013-11-25 15:17:36 +00:00
|
|
|
else
|
2016-01-04 14:31:23 +00:00
|
|
|
self->rtt_avg = (15 * self->rtt_avg + rtt) / 16;
|
2013-11-25 15:17:36 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (rtt > 2 * self->rtt_avg) {
|
2013-11-25 15:17:36 +00:00
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Dropping observation, long RTT %" GST_TIME_FORMAT " > 2 * avg %"
|
2016-01-04 14:31:23 +00:00
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (rtt), GST_TIME_ARGS (self->rtt_avg));
|
2013-11-25 15:17:36 +00:00
|
|
|
goto bogus_observation;
|
|
|
|
}
|
|
|
|
|
2015-06-06 18:39:47 +00:00
|
|
|
/* The difference between the local and remote clock (again assuming
|
|
|
|
* symmetric path delays):
|
|
|
|
*
|
|
|
|
* local_1 + delta / 2 - remote_1 = theta
|
|
|
|
* or
|
|
|
|
* local_2 - delta / 2 - remote_2 = theta
|
|
|
|
*
|
|
|
|
* which gives after some simple algebraic transformations:
|
|
|
|
*
|
|
|
|
* (remote_1 - local_1) + (remote_2 - local_2)
|
|
|
|
* theta = -------------------------------------------
|
|
|
|
* 2
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Thus remote time at local_avg is equal to:
|
|
|
|
*
|
|
|
|
* local_avg + theta =
|
|
|
|
*
|
|
|
|
* local_1 + local_2 (remote_1 - local_1) + (remote_2 - local_2)
|
|
|
|
* ----------------- + -------------------------------------------
|
|
|
|
* 2 2
|
|
|
|
*
|
|
|
|
* =
|
|
|
|
*
|
|
|
|
* remote_1 + remote_2
|
|
|
|
* ------------------- = remote_avg
|
|
|
|
* 2
|
|
|
|
*
|
|
|
|
* We use this for our clock estimation, i.e. local_avg at remote clock
|
|
|
|
* being the same as remote_avg.
|
|
|
|
*/
|
|
|
|
|
2005-11-18 14:13:28 +00:00
|
|
|
local_avg = (local_2 + local_1) / 2;
|
2015-06-06 18:39:47 +00:00
|
|
|
remote_avg = (remote_2 + remote_1) / 2;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2015-06-06 18:39:47 +00:00
|
|
|
GST_LOG_OBJECT (self,
|
2016-03-03 16:14:35 +00:00
|
|
|
"remoteavg %" G_GUINT64_FORMAT " localavg %" G_GUINT64_FORMAT,
|
|
|
|
remote_avg, local_avg);
|
2013-11-25 15:17:36 +00:00
|
|
|
|
gst/base/gstbasesink.*: No need to store the clock, the parent element class already has it.
Original commit message from CVS:
* gst/base/gstbasesink.c: (gst_base_sink_class_init),
(gst_base_sink_wait), (gst_base_sink_do_sync),
(gst_base_sink_handle_event):
* gst/base/gstbasesink.h:
No need to store the clock, the parent element class already
has it.
* gst/gstbin.c: (gst_bin_set_clock_func), (gst_bin_add_func):
Updates for clock_set returning a gboolean
* gst/gstclock.c: (gst_clock_entry_new), (gst_clock_id_wait),
(gst_clock_id_wait_async), (gst_clock_class_init),
(gst_clock_init), (gst_clock_finalize),
(gst_clock_get_internal_time), (gst_clock_get_time),
(gst_clock_slave_callback), (gst_clock_set_master),
(gst_clock_get_master), (do_linear_regression),
(gst_clock_add_observation), (gst_clock_set_property),
(gst_clock_get_property):
* gst/gstclock.h:
Implement master/slave. When setting a clock as a slave, a
periodic timeout is scheduled to sample master and slave times.
Then the slave clock is recalibrated to match offset and rate
of the master clock.
Update logging a bit.
Add flag so that a clock can state that is cannot be slaved to
another clock.
* gst/gstelement.c: (gst_element_set_clock):
* gst/gstelement.h:
The set_clock returns a gboolean for when an element cannot
deal with the selected clock in the pipeline.
* gst/gstpipeline.c: (gst_pipeline_change_state),
(gst_pipeline_set_clock):
* gst/gstpipeline.h:
Handle the case where the selected clock cannot be set on
the pipeline.
* gst/net/gstnetclientclock.c: (gst_net_client_clock_class_init),
(gst_net_client_clock_init), (gst_net_client_clock_finalize),
(gst_net_client_clock_set_property),
(gst_net_client_clock_get_property),
(gst_net_client_clock_observe_times):
* gst/net/gstnetclientclock.h:
Use regression code in GstClock parent, remove duplicated
functionality.
2005-11-22 18:28:44 +00:00
|
|
|
clock = GST_CLOCK_CAST (self);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2015-01-10 10:42:00 +00:00
|
|
|
/* Store what the clock produced as 'now' before this update */
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_get_calibration (GST_CLOCK_CAST (self), &orig_internal_time,
|
2015-02-06 17:22:22 +00:00
|
|
|
&orig_external_time, &orig_rate_num, &orig_rate_den);
|
|
|
|
internal_time = orig_internal_time;
|
|
|
|
external_time = orig_external_time;
|
|
|
|
rate_num = orig_rate_num;
|
|
|
|
rate_den = orig_rate_den;
|
2015-01-10 10:42:00 +00:00
|
|
|
|
|
|
|
min_guess =
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_1,
|
2015-01-10 10:42:00 +00:00
|
|
|
internal_time, external_time, rate_num, rate_den);
|
|
|
|
time_before =
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_2,
|
2015-01-10 10:42:00 +00:00
|
|
|
internal_time, external_time, rate_num, rate_den);
|
|
|
|
|
2015-01-15 11:32:28 +00:00
|
|
|
/* Maximum discontinuity, when we're synched with the master. Could make this a property,
|
|
|
|
* but this value seems to work fine */
|
2016-01-04 14:31:23 +00:00
|
|
|
max_discont = self->rtt_avg / 4;
|
2015-01-15 11:32:28 +00:00
|
|
|
|
2015-02-06 17:22:22 +00:00
|
|
|
/* If the remote observation was within a max_discont window around our min/max estimates, we're synched */
|
2015-01-15 11:32:28 +00:00
|
|
|
synched =
|
2015-06-06 18:39:47 +00:00
|
|
|
(GST_CLOCK_DIFF (remote_avg, min_guess) < (GstClockTimeDiff) (max_discont)
|
2015-01-15 11:32:28 +00:00
|
|
|
&& GST_CLOCK_DIFF (time_before,
|
2015-06-06 18:39:47 +00:00
|
|
|
remote_avg) < (GstClockTimeDiff) (max_discont));
|
2015-01-10 10:42:00 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (gst_clock_add_observation_unapplied (GST_CLOCK_CAST (self),
|
2015-06-08 15:04:55 +00:00
|
|
|
local_avg, remote_avg, &r_squared, &internal_time, &external_time,
|
|
|
|
&rate_num, &rate_den)) {
|
2015-01-15 11:32:28 +00:00
|
|
|
|
|
|
|
/* Now compare the difference (discont) in the clock
|
|
|
|
* after this observation */
|
|
|
|
time_discont = GST_CLOCK_DIFF (time_before,
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_2,
|
2015-01-15 11:32:28 +00:00
|
|
|
internal_time, external_time, rate_num, rate_den));
|
|
|
|
|
|
|
|
/* If we were in sync with the remote clock, clamp the allowed
|
|
|
|
* discontinuity to within quarter of one RTT. In sync means our send/receive estimates
|
|
|
|
* of remote time correctly windowed the actual remote time observation */
|
|
|
|
if (synched && ABS (time_discont) > max_discont) {
|
|
|
|
GstClockTimeDiff offset;
|
|
|
|
GST_DEBUG_OBJECT (clock,
|
|
|
|
"Too large a discont, clamping to 1/4 average RTT = %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (max_discont));
|
|
|
|
if (time_discont > 0) { /* Too large a forward step - add a -ve offset */
|
|
|
|
offset = max_discont - time_discont;
|
|
|
|
if (-offset > external_time)
|
|
|
|
external_time = 0;
|
|
|
|
else
|
|
|
|
external_time += offset;
|
|
|
|
} else { /* Too large a backward step - add a +ve offset */
|
|
|
|
offset = -(max_discont + time_discont);
|
|
|
|
external_time += offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_discont += offset;
|
|
|
|
}
|
|
|
|
|
2015-02-06 17:22:22 +00:00
|
|
|
/* Check if the new clock params would have made our observation within range */
|
|
|
|
now_synched =
|
2015-06-06 18:39:47 +00:00
|
|
|
(GST_CLOCK_DIFF (remote_avg,
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self),
|
2015-06-08 15:04:55 +00:00
|
|
|
local_1, internal_time, external_time, rate_num,
|
2015-02-06 17:22:22 +00:00
|
|
|
rate_den)) < (GstClockTimeDiff) (max_discont))
|
2015-06-08 15:04:55 +00:00
|
|
|
&&
|
|
|
|
(GST_CLOCK_DIFF (gst_clock_adjust_with_calibration
|
2016-01-04 14:31:23 +00:00
|
|
|
(GST_CLOCK_CAST (self), local_2, internal_time, external_time,
|
2015-06-08 15:04:55 +00:00
|
|
|
rate_num, rate_den),
|
2015-06-06 18:39:47 +00:00
|
|
|
remote_avg) < (GstClockTimeDiff) (max_discont));
|
2015-02-06 17:22:22 +00:00
|
|
|
|
|
|
|
/* Only update the clock if we had synch or just gained it */
|
2016-01-04 14:31:23 +00:00
|
|
|
if (synched || now_synched || self->skipped_updates > MAX_SKIPPED_UPDATES) {
|
|
|
|
gst_clock_set_calibration (GST_CLOCK_CAST (self), internal_time,
|
2015-06-08 15:04:55 +00:00
|
|
|
external_time, rate_num, rate_den);
|
2015-02-06 17:22:22 +00:00
|
|
|
/* ghetto formula - shorter timeout for bad correlations */
|
|
|
|
current_timeout = (1e-3 / (1 - MIN (r_squared, 0.99999))) * GST_SECOND;
|
2015-06-08 15:04:55 +00:00
|
|
|
current_timeout =
|
2016-01-04 14:31:23 +00:00
|
|
|
MIN (current_timeout, gst_clock_get_timeout (GST_CLOCK_CAST (self)));
|
|
|
|
self->skipped_updates = 0;
|
2015-06-08 15:10:56 +00:00
|
|
|
|
|
|
|
/* FIXME: When do we consider the clock absolutely not synced anymore? */
|
|
|
|
gst_clock_set_synced (GST_CLOCK (self), TRUE);
|
2015-02-06 17:22:22 +00:00
|
|
|
} else {
|
|
|
|
/* Restore original calibration vars for the report, we're not changing the clock */
|
|
|
|
internal_time = orig_internal_time;
|
|
|
|
external_time = orig_external_time;
|
|
|
|
rate_num = orig_rate_num;
|
|
|
|
rate_den = orig_rate_den;
|
|
|
|
time_discont = 0;
|
2016-01-04 14:31:23 +00:00
|
|
|
self->skipped_updates++;
|
2015-02-06 17:22:22 +00:00
|
|
|
}
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
2012-02-27 08:02:07 +00:00
|
|
|
|
2015-01-15 11:32:28 +00:00
|
|
|
/* Limit the polling to at most one per minimum_update_interval */
|
|
|
|
if (rtt < min_update_interval)
|
|
|
|
current_timeout = MAX (min_update_interval - rtt, current_timeout);
|
2015-01-10 10:42:00 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
if (self->busses) {
|
2015-01-10 10:42:00 +00:00
|
|
|
GstStructure *s;
|
|
|
|
GstMessage *msg;
|
2016-01-04 14:31:23 +00:00
|
|
|
GList *l;
|
2015-01-10 10:42:00 +00:00
|
|
|
|
2015-01-15 11:32:28 +00:00
|
|
|
/* Output a stats message, whether we updated the clock or not */
|
2015-01-10 10:42:00 +00:00
|
|
|
s = gst_structure_new ("gst-netclock-statistics",
|
|
|
|
"synchronised", G_TYPE_BOOLEAN, synched,
|
|
|
|
"rtt", G_TYPE_UINT64, rtt,
|
2016-01-04 14:31:23 +00:00
|
|
|
"rtt-average", G_TYPE_UINT64, self->rtt_avg,
|
2015-01-10 10:42:00 +00:00
|
|
|
"local", G_TYPE_UINT64, local_avg,
|
2015-06-06 18:39:47 +00:00
|
|
|
"remote", G_TYPE_UINT64, remote_avg,
|
2015-01-10 10:42:00 +00:00
|
|
|
"discontinuity", G_TYPE_INT64, time_discont,
|
|
|
|
"remote-min-estimate", G_TYPE_UINT64, min_guess,
|
|
|
|
"remote-max-estimate", G_TYPE_UINT64, time_before,
|
2015-06-06 18:39:47 +00:00
|
|
|
"remote-min-error", G_TYPE_INT64, GST_CLOCK_DIFF (remote_avg,
|
|
|
|
min_guess), "remote-max-error", G_TYPE_INT64,
|
|
|
|
GST_CLOCK_DIFF (remote_avg, time_before), "request-send", G_TYPE_UINT64,
|
|
|
|
local_1, "request-receive", G_TYPE_UINT64, local_2, "r-squared",
|
|
|
|
G_TYPE_DOUBLE, r_squared, "timeout", G_TYPE_UINT64, current_timeout,
|
|
|
|
"internal-time", G_TYPE_UINT64, internal_time, "external-time",
|
|
|
|
G_TYPE_UINT64, external_time, "rate-num", G_TYPE_UINT64, rate_num,
|
|
|
|
"rate-den", G_TYPE_UINT64, rate_den, "rate", G_TYPE_DOUBLE,
|
|
|
|
(gdouble) (rate_num) / rate_den, "local-clock-offset", G_TYPE_INT64,
|
|
|
|
GST_CLOCK_DIFF (internal_time, external_time), NULL);
|
2015-01-10 10:42:00 +00:00
|
|
|
msg = gst_message_new_element (GST_OBJECT (self), s);
|
2016-01-04 14:31:23 +00:00
|
|
|
|
|
|
|
for (l = self->busses; l; l = l->next)
|
|
|
|
gst_bus_post (l->data, gst_message_ref (msg));
|
|
|
|
gst_message_unref (msg);
|
2015-01-10 10:42:00 +00:00
|
|
|
}
|
2016-01-04 14:31:23 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2015-01-10 10:42:00 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_INFO ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (current_timeout));
|
2016-01-04 14:31:23 +00:00
|
|
|
self->timeout_expiration = gst_util_get_timestamp () + current_timeout;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
bogus_observation:
|
2013-11-27 07:32:22 +00:00
|
|
|
/* Schedule a new packet again soon */
|
2016-01-04 14:31:23 +00:00
|
|
|
self->timeout_expiration = gst_util_get_timestamp () + (GST_SECOND / 4);
|
2013-11-27 07:32:22 +00:00
|
|
|
return;
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_thread (gpointer data)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstNetClientInternalClock *self = data;
|
|
|
|
GSocket *socket = self->socket;
|
2010-05-05 15:33:51 +00:00
|
|
|
GError *err = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_INFO_OBJECT (self, "net client clock thread running, socket=%p", socket);
|
|
|
|
|
|
|
|
g_socket_set_blocking (socket, TRUE);
|
|
|
|
g_socket_set_timeout (socket, 0);
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
while (!g_cancellable_is_cancelled (self->cancel)) {
|
|
|
|
GstClockTime expiration_time = self->timeout_expiration;
|
2012-08-10 08:19:25 +00:00
|
|
|
GstClockTime now = gst_util_get_timestamp ();
|
|
|
|
gint64 socket_timeout;
|
|
|
|
|
|
|
|
if (now >= expiration_time || (expiration_time - now) <= GST_MSECOND) {
|
|
|
|
socket_timeout = 0;
|
|
|
|
} else {
|
|
|
|
socket_timeout = (expiration_time - now) / GST_USECOND;
|
|
|
|
}
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-08-10 12:50:41 +00:00
|
|
|
GST_TRACE_OBJECT (self, "timeout: %" G_GINT64_FORMAT "us", socket_timeout);
|
2005-11-18 14:13:28 +00:00
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
if (!g_socket_condition_timed_wait (socket, G_IO_IN, socket_timeout,
|
2016-01-04 14:31:23 +00:00
|
|
|
self->cancel, &err)) {
|
2012-08-10 08:19:25 +00:00
|
|
|
/* cancelled, timeout or error */
|
|
|
|
if (err->code == G_IO_ERROR_CANCELLED) {
|
|
|
|
GST_INFO_OBJECT (self, "cancelled");
|
|
|
|
g_clear_error (&err);
|
|
|
|
break;
|
|
|
|
} else if (err->code == G_IO_ERROR_TIMED_OUT) {
|
|
|
|
/* timed out, let's send another packet */
|
|
|
|
GST_DEBUG_OBJECT (self, "timed out");
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->is_ntp) {
|
2015-06-06 19:43:05 +00:00
|
|
|
GstNtpPacket *packet;
|
|
|
|
|
|
|
|
packet = gst_ntp_packet_new (NULL, NULL);
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
packet->transmit_time =
|
|
|
|
gst_clock_get_internal_time (GST_CLOCK_CAST (self));
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"sending packet, local time = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->transmit_time));
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_ntp_packet_send (packet, self->socket, self->servaddr, NULL);
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2015-06-06 19:43:05 +00:00
|
|
|
g_free (packet);
|
|
|
|
} else {
|
|
|
|
GstNetTimePacket *packet;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2015-06-06 19:43:05 +00:00
|
|
|
packet = gst_net_time_packet_new (NULL);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
packet->local_time =
|
|
|
|
gst_clock_get_internal_time (GST_CLOCK_CAST (self));
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"sending packet, local time = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->local_time));
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_time_packet_send (packet, self->socket, self->servaddr, NULL);
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
g_free (packet);
|
|
|
|
}
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
/* reset timeout (but are expecting a response sooner anyway) */
|
2016-01-04 14:31:23 +00:00
|
|
|
self->timeout_expiration =
|
|
|
|
gst_util_get_timestamp () +
|
|
|
|
gst_clock_get_timeout (GST_CLOCK_CAST (self));
|
2012-08-10 08:19:25 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (self, "socket error: %s", err->message);
|
|
|
|
g_usleep (G_USEC_PER_SEC / 10); /* throttle */
|
|
|
|
}
|
|
|
|
g_clear_error (&err);
|
|
|
|
} else {
|
2010-05-05 15:33:51 +00:00
|
|
|
GstClockTime new_local;
|
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
/* got packet */
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
new_local = gst_clock_get_internal_time (GST_CLOCK_CAST (self));
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->is_ntp) {
|
2015-06-06 19:43:05 +00:00
|
|
|
GstNtpPacket *packet;
|
|
|
|
|
|
|
|
packet = gst_ntp_packet_receive (socket, NULL, &err);
|
|
|
|
|
|
|
|
if (packet != NULL) {
|
|
|
|
GST_LOG_OBJECT (self, "got packet back");
|
|
|
|
GST_LOG_OBJECT (self, "local_1 = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->origin_time));
|
|
|
|
GST_LOG_OBJECT (self, "remote_1 = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->receive_time));
|
|
|
|
GST_LOG_OBJECT (self, "remote_2 = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->transmit_time));
|
|
|
|
GST_LOG_OBJECT (self, "local_2 = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (new_local));
|
|
|
|
GST_LOG_OBJECT (self, "poll_interval = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->poll_interval));
|
|
|
|
|
|
|
|
/* Remember the last poll interval we ever got from the server */
|
|
|
|
if (packet->poll_interval != GST_CLOCK_TIME_NONE)
|
2016-01-04 14:31:23 +00:00
|
|
|
self->last_remote_poll_interval = packet->poll_interval;
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
/* observe_times will reset the timeout */
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_observe_times (self,
|
|
|
|
packet->origin_time, packet->receive_time, packet->transmit_time,
|
|
|
|
new_local);
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
g_free (packet);
|
|
|
|
} else if (err != NULL) {
|
|
|
|
if (g_error_matches (err, GST_NTP_ERROR, GST_NTP_ERROR_WRONG_VERSION)
|
|
|
|
|| g_error_matches (err, GST_NTP_ERROR, GST_NTP_ERROR_KOD_DENY)) {
|
|
|
|
GST_ERROR_OBJECT (self, "fatal receive error: %s", err->message);
|
2016-01-13 21:32:20 +00:00
|
|
|
g_clear_error (&err);
|
2015-06-06 19:43:05 +00:00
|
|
|
break;
|
|
|
|
} else if (g_error_matches (err, GST_NTP_ERROR,
|
|
|
|
GST_NTP_ERROR_KOD_RATE)) {
|
|
|
|
GST_WARNING_OBJECT (self, "need to limit rate");
|
|
|
|
|
|
|
|
/* If the server did not tell us a poll interval before, double
|
|
|
|
* our minimum poll interval. Otherwise we assume that the server
|
|
|
|
* already told us something sensible and that this error here
|
|
|
|
* was just a spurious error */
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->last_remote_poll_interval == GST_CLOCK_TIME_NONE)
|
|
|
|
self->minimum_update_interval *= 2;
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
/* And wait a bit before we send the next packet instead of
|
|
|
|
* sending it immediately */
|
2016-01-04 14:31:23 +00:00
|
|
|
self->timeout_expiration =
|
2015-06-08 15:04:55 +00:00
|
|
|
gst_util_get_timestamp () +
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_clock_get_timeout (GST_CLOCK_CAST (self));
|
2015-06-06 19:43:05 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (self, "receive error: %s", err->message);
|
|
|
|
}
|
|
|
|
g_clear_error (&err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GstNetTimePacket *packet;
|
|
|
|
|
|
|
|
packet = gst_net_time_packet_receive (socket, NULL, &err);
|
|
|
|
|
|
|
|
if (packet != NULL) {
|
|
|
|
GST_LOG_OBJECT (self, "got packet back");
|
|
|
|
GST_LOG_OBJECT (self, "local_1 = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->local_time));
|
|
|
|
GST_LOG_OBJECT (self, "remote = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->remote_time));
|
|
|
|
GST_LOG_OBJECT (self, "local_2 = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (new_local));
|
|
|
|
|
|
|
|
/* observe_times will reset the timeout */
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_observe_times (self, packet->local_time,
|
2015-06-06 19:43:05 +00:00
|
|
|
packet->remote_time, packet->remote_time, new_local);
|
|
|
|
|
|
|
|
g_free (packet);
|
|
|
|
} else if (err != NULL) {
|
|
|
|
GST_WARNING_OBJECT (self, "receive error: %s", err->message);
|
|
|
|
g_clear_error (&err);
|
|
|
|
}
|
2010-05-05 15:33:51 +00:00
|
|
|
}
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_INFO_OBJECT (self, "shutting down net client clock thread");
|
2005-11-17 17:55:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_start (GstNetClientInternalClock * self)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GSocketAddress *servaddr;
|
|
|
|
GSocketAddress *myaddr;
|
2013-04-09 09:17:45 +00:00
|
|
|
GSocketAddress *anyaddr;
|
2010-05-05 15:33:51 +00:00
|
|
|
GInetAddress *inetaddr;
|
|
|
|
GSocket *socket;
|
2011-12-04 13:04:35 +00:00
|
|
|
GError *error = NULL;
|
2013-04-24 13:58:49 +00:00
|
|
|
GSocketFamily family;
|
2015-05-19 13:34:04 +00:00
|
|
|
GPollFD dummy_pollfd;
|
2015-12-30 14:57:29 +00:00
|
|
|
GResolver *resolver = NULL;
|
|
|
|
GError *err = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_return_val_if_fail (self->address != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (self->servaddr == NULL, FALSE);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2013-04-24 13:58:49 +00:00
|
|
|
/* create target address */
|
2016-01-04 14:31:23 +00:00
|
|
|
inetaddr = g_inet_address_new_from_string (self->address);
|
2015-12-30 14:57:29 +00:00
|
|
|
if (inetaddr == NULL) {
|
|
|
|
GList *results;
|
|
|
|
|
|
|
|
resolver = g_resolver_get_default ();
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
results = g_resolver_lookup_by_name (resolver, self->address, NULL, &err);
|
2015-12-30 14:57:29 +00:00
|
|
|
if (!results)
|
|
|
|
goto failed_to_resolve;
|
|
|
|
|
|
|
|
inetaddr = G_INET_ADDRESS (g_object_ref (results->data));
|
|
|
|
g_resolver_free_addresses (results);
|
|
|
|
g_object_unref (resolver);
|
|
|
|
}
|
2013-04-24 13:58:49 +00:00
|
|
|
|
|
|
|
family = g_inet_address_get_family (inetaddr);
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
servaddr = g_inet_socket_address_new (inetaddr, self->port);
|
2013-04-24 13:58:49 +00:00
|
|
|
g_object_unref (inetaddr);
|
|
|
|
|
|
|
|
g_assert (servaddr != NULL);
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "will communicate with %s:%d", self->address,
|
|
|
|
self->port);
|
2013-04-24 13:58:49 +00:00
|
|
|
|
|
|
|
socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
|
2010-05-05 15:33:51 +00:00
|
|
|
G_SOCKET_PROTOCOL_UDP, &error);
|
|
|
|
|
|
|
|
if (socket == NULL)
|
2005-11-17 17:55:17 +00:00
|
|
|
goto no_socket;
|
|
|
|
|
2013-04-09 09:17:45 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "binding socket");
|
2013-04-24 13:58:49 +00:00
|
|
|
inetaddr = g_inet_address_new_any (family);
|
2013-04-09 09:17:45 +00:00
|
|
|
anyaddr = g_inet_socket_address_new (inetaddr, 0);
|
|
|
|
g_socket_bind (socket, anyaddr, TRUE, &error);
|
|
|
|
g_object_unref (anyaddr);
|
|
|
|
g_object_unref (inetaddr);
|
|
|
|
|
|
|
|
if (error != NULL)
|
|
|
|
goto bind_error;
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
/* check address we're bound to, mostly for debugging purposes */
|
|
|
|
myaddr = g_socket_get_local_address (socket, &error);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
if (myaddr == NULL)
|
2005-11-17 17:55:17 +00:00
|
|
|
goto getsockname_error;
|
|
|
|
|
2015-06-06 16:42:18 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "socket opened on UDP port %d",
|
2010-05-05 15:33:51 +00:00
|
|
|
g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (myaddr)));
|
2006-05-25 15:52:19 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
g_object_unref (myaddr);
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->cancel = g_cancellable_new ();
|
|
|
|
self->made_cancel_fd =
|
|
|
|
g_cancellable_make_pollfd (self->cancel, &dummy_pollfd);
|
2015-05-19 13:34:04 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->socket = socket;
|
|
|
|
self->servaddr = G_SOCKET_ADDRESS (servaddr);
|
libs/gst/net/: Massive code removal and cleanups because of GstPoll.
Original commit message from CVS:
Patch by: Peter Kjellerstedt <pkj at axis dot com>
* libs/gst/net/gstnetclientclock.c: (gst_net_client_clock_init),
(gst_net_client_clock_finalize), (gst_net_client_clock_do_select),
(gst_net_client_clock_thread), (gst_net_client_clock_start),
(gst_net_client_clock_stop), (gst_net_client_clock_new):
* libs/gst/net/gstnetclientclock.h:
* libs/gst/net/gstnettimeprovider.c: (gst_net_time_provider_init),
(gst_net_time_provider_finalize), (gst_net_time_provider_thread),
(gst_net_time_provider_start), (gst_net_time_provider_stop),
(gst_net_time_provider_new):
* libs/gst/net/gstnettimeprovider.h:
Massive code removal and cleanups because of GstPoll.
Fixes #505417.
2008-02-27 18:27:59 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->thread = g_thread_try_new ("GstNetClientInternalClock",
|
|
|
|
gst_net_client_internal_clock_thread, self, &error);
|
2011-12-03 13:58:51 +00:00
|
|
|
|
|
|
|
if (error != NULL)
|
2005-11-17 17:55:17 +00:00
|
|
|
goto no_thread;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
no_socket:
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_ERROR_OBJECT (self, "socket_new() failed: %s", error->message);
|
|
|
|
g_error_free (error);
|
2005-11-17 17:55:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-09 09:17:45 +00:00
|
|
|
bind_error:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (self, "bind failed: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
g_object_unref (socket);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-11-17 17:55:17 +00:00
|
|
|
getsockname_error:
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_ERROR_OBJECT (self, "get_local_address() failed: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
g_object_unref (socket);
|
2005-11-17 17:55:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2015-12-30 14:57:29 +00:00
|
|
|
failed_to_resolve:
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2015-12-30 14:57:29 +00:00
|
|
|
GST_ERROR_OBJECT (self, "resolving '%s' failed: %s",
|
2016-01-04 14:31:23 +00:00
|
|
|
self->address, err->message);
|
2015-12-30 14:57:29 +00:00
|
|
|
g_clear_error (&err);
|
|
|
|
g_object_unref (resolver);
|
2005-11-17 17:55:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_thread:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
|
2016-01-04 14:31:23 +00:00
|
|
|
g_object_unref (self->servaddr);
|
|
|
|
self->servaddr = NULL;
|
|
|
|
g_object_unref (self->socket);
|
|
|
|
self->socket = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
g_error_free (error);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-04 14:31:23 +00:00
|
|
|
gst_net_client_internal_clock_stop (GstNetClientInternalClock * self)
|
2005-11-17 17:55:17 +00:00
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->thread == NULL)
|
2010-05-05 15:33:51 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "stopping...");
|
2016-01-04 14:31:23 +00:00
|
|
|
g_cancellable_cancel (self->cancel);
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_thread_join (self->thread);
|
|
|
|
self->thread = NULL;
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
if (self->made_cancel_fd)
|
|
|
|
g_cancellable_release_fd (self->cancel);
|
2015-05-19 13:34:04 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_object_unref (self->cancel);
|
|
|
|
self->cancel = NULL;
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_object_unref (self->servaddr);
|
|
|
|
self->servaddr = NULL;
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
g_object_unref (self->socket);
|
|
|
|
self->socket = NULL;
|
2010-05-05 15:33:51 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "stopped");
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
#define GST_NET_CLIENT_CLOCK_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_CLIENT_CLOCK, GstNetClientClockPrivate))
|
|
|
|
|
|
|
|
struct _GstNetClientClockPrivate
|
|
|
|
{
|
|
|
|
GstClock *internal_clock;
|
|
|
|
|
|
|
|
GstClockTime roundtrip_limit;
|
|
|
|
GstClockTime minimum_update_interval;
|
|
|
|
|
2016-03-09 11:44:24 +00:00
|
|
|
GstClockTime base_time, internal_base_time;
|
2016-01-04 14:31:23 +00:00
|
|
|
|
|
|
|
gchar *address;
|
|
|
|
gint port;
|
|
|
|
|
|
|
|
GstBus *bus;
|
|
|
|
|
|
|
|
gboolean is_ntp;
|
|
|
|
|
|
|
|
gulong synced_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstNetClientClock, gst_net_client_clock, GST_TYPE_SYSTEM_CLOCK);
|
|
|
|
|
|
|
|
static void gst_net_client_clock_finalize (GObject * object);
|
|
|
|
static void gst_net_client_clock_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_net_client_clock_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_net_client_clock_constructed (GObject * object);
|
|
|
|
|
|
|
|
static GstClockTime gst_net_client_clock_get_internal_time (GstClock * clock);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_class_init (GstNetClientClockClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstClockClass *clock_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
clock_class = GST_CLOCK_CLASS (klass);
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GstNetClientClockPrivate));
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_net_client_clock_finalize;
|
|
|
|
gobject_class->get_property = gst_net_client_clock_get_property;
|
|
|
|
gobject_class->set_property = gst_net_client_clock_set_property;
|
|
|
|
gobject_class->constructed = gst_net_client_clock_constructed;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ADDRESS,
|
|
|
|
g_param_spec_string ("address", "address",
|
|
|
|
"The IP address of the machine providing a time server",
|
|
|
|
DEFAULT_ADDRESS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PORT,
|
|
|
|
g_param_spec_int ("port", "port",
|
|
|
|
"The port on which the remote server is listening", 0, G_MAXUINT16,
|
|
|
|
DEFAULT_PORT,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_BUS,
|
|
|
|
g_param_spec_object ("bus", "bus",
|
|
|
|
"A GstBus on which to send clock status information", GST_TYPE_BUS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GstNetClientInternalClock::round-trip-limit:
|
|
|
|
*
|
|
|
|
* Maximum allowed round-trip for packets. If this property is set to a nonzero
|
|
|
|
* value, all packets with a round-trip interval larger than this limit will be
|
|
|
|
* ignored. This is useful for networks with severe and fluctuating transport
|
|
|
|
* delays. Filtering out these packets increases stability of the synchronization.
|
|
|
|
* On the other hand, the lower the limit, the higher the amount of filtered
|
|
|
|
* packets. Empirical tests are typically necessary to estimate a good value
|
|
|
|
* for the limit.
|
|
|
|
* If the property is set to zero, the limit is disabled.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ROUNDTRIP_LIMIT,
|
|
|
|
g_param_spec_uint64 ("round-trip-limit", "round-trip limit",
|
|
|
|
"Maximum tolerable round-trip interval for packets, in nanoseconds "
|
|
|
|
"(0 = no limit)", 0, G_MAXUINT64, DEFAULT_ROUNDTRIP_LIMIT,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_MINIMUM_UPDATE_INTERVAL,
|
|
|
|
g_param_spec_uint64 ("minimum-update-interval", "minimum update interval",
|
|
|
|
"Minimum polling interval for packets, in nanoseconds"
|
|
|
|
"(0 = no limit)", 0, G_MAXUINT64, DEFAULT_MINIMUM_UPDATE_INTERVAL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_BASE_TIME,
|
|
|
|
g_param_spec_uint64 ("base-time", "Base Time",
|
|
|
|
"Initial time that is reported before synchronization", 0,
|
|
|
|
G_MAXUINT64, DEFAULT_BASE_TIME,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_INTERNAL_CLOCK,
|
|
|
|
g_param_spec_object ("internal-clock", "Internal Clock",
|
|
|
|
"Internal clock that directly slaved to the remote clock",
|
|
|
|
GST_TYPE_CLOCK, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
clock_class->get_internal_time = gst_net_client_clock_get_internal_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_init (GstNetClientClock * self)
|
|
|
|
{
|
|
|
|
GstNetClientClockPrivate *priv;
|
2016-03-09 11:44:24 +00:00
|
|
|
GstClock *clock;
|
2016-01-04 14:31:23 +00:00
|
|
|
|
|
|
|
self->priv = priv = GST_NET_CLIENT_CLOCK_GET_PRIVATE (self);
|
|
|
|
|
|
|
|
GST_OBJECT_FLAG_SET (self, GST_CLOCK_FLAG_CAN_SET_MASTER);
|
|
|
|
GST_OBJECT_FLAG_SET (self, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC);
|
|
|
|
|
|
|
|
priv->port = DEFAULT_PORT;
|
|
|
|
priv->address = g_strdup (DEFAULT_ADDRESS);
|
|
|
|
|
|
|
|
priv->roundtrip_limit = DEFAULT_ROUNDTRIP_LIMIT;
|
|
|
|
priv->minimum_update_interval = DEFAULT_MINIMUM_UPDATE_INTERVAL;
|
2016-03-09 11:44:24 +00:00
|
|
|
|
|
|
|
clock = gst_system_clock_obtain ();
|
2016-01-04 14:31:23 +00:00
|
|
|
priv->base_time = DEFAULT_BASE_TIME;
|
2016-03-09 11:44:24 +00:00
|
|
|
priv->internal_base_time = gst_clock_get_time (clock);
|
|
|
|
gst_object_unref (clock);
|
2016-01-04 14:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called with clocks_lock */
|
|
|
|
static void
|
|
|
|
update_clock_cache (ClockCache * cache)
|
|
|
|
{
|
|
|
|
GstClockTime roundtrip_limit = 0, minimum_update_interval = 0;
|
|
|
|
GList *l, *busses = NULL;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (cache->clock);
|
|
|
|
g_list_free_full (GST_NET_CLIENT_INTERNAL_CLOCK (cache->clock)->busses,
|
|
|
|
(GDestroyNotify) gst_object_unref);
|
|
|
|
|
|
|
|
for (l = cache->clocks; l; l = l->next) {
|
|
|
|
GstNetClientClock *clock = l->data;
|
|
|
|
|
|
|
|
if (clock->priv->bus)
|
|
|
|
busses = g_list_prepend (busses, gst_object_ref (clock->priv->bus));
|
|
|
|
|
|
|
|
if (roundtrip_limit == 0)
|
|
|
|
roundtrip_limit = clock->priv->roundtrip_limit;
|
|
|
|
else
|
|
|
|
roundtrip_limit = MAX (roundtrip_limit, clock->priv->roundtrip_limit);
|
|
|
|
|
|
|
|
if (minimum_update_interval == 0)
|
|
|
|
minimum_update_interval = clock->priv->minimum_update_interval;
|
|
|
|
else
|
|
|
|
minimum_update_interval =
|
|
|
|
MIN (minimum_update_interval, clock->priv->minimum_update_interval);
|
|
|
|
}
|
|
|
|
GST_NET_CLIENT_INTERNAL_CLOCK (cache->clock)->busses = busses;
|
|
|
|
GST_NET_CLIENT_INTERNAL_CLOCK (cache->clock)->roundtrip_limit =
|
|
|
|
roundtrip_limit;
|
|
|
|
GST_NET_CLIENT_INTERNAL_CLOCK (cache->clock)->minimum_update_interval =
|
|
|
|
minimum_update_interval;
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (cache->clock);
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:18:07 +00:00
|
|
|
static gboolean
|
|
|
|
remove_clock_cache (GstClock * clock, GstClockTime time, GstClockID id,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
ClockCache *cache = user_data;
|
|
|
|
|
|
|
|
G_LOCK (clocks_lock);
|
|
|
|
if (!cache->clocks) {
|
|
|
|
gst_clock_id_unref (cache->remove_id);
|
|
|
|
gst_object_unref (cache->clock);
|
|
|
|
clocks = g_list_remove (clocks, cache);
|
2016-01-15 08:02:42 +00:00
|
|
|
g_free (cache);
|
2016-01-04 15:18:07 +00:00
|
|
|
}
|
|
|
|
G_UNLOCK (clocks_lock);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
static void
|
|
|
|
gst_net_client_clock_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
if (self->priv->synced_id)
|
2016-01-05 12:59:34 +00:00
|
|
|
g_signal_handler_disconnect (self->priv->internal_clock,
|
|
|
|
self->priv->synced_id);
|
2016-01-04 14:31:23 +00:00
|
|
|
self->priv->synced_id = 0;
|
|
|
|
|
|
|
|
G_LOCK (clocks_lock);
|
|
|
|
for (l = clocks; l; l = l->next) {
|
|
|
|
ClockCache *cache = l->data;
|
|
|
|
|
|
|
|
if (cache->clock == self->priv->internal_clock) {
|
|
|
|
cache->clocks = g_list_remove (cache->clocks, self);
|
|
|
|
|
|
|
|
if (cache->clocks) {
|
|
|
|
update_clock_cache (cache);
|
|
|
|
} else {
|
2016-01-04 15:18:07 +00:00
|
|
|
GstClock *sysclock = gst_system_clock_obtain ();
|
|
|
|
GstClockTime time = gst_clock_get_time (sysclock) + 60 * GST_SECOND;
|
|
|
|
|
|
|
|
cache->remove_id = gst_clock_new_single_shot_id (sysclock, time);
|
|
|
|
gst_clock_id_wait_async (cache->remove_id, remove_clock_cache, cache,
|
|
|
|
NULL);
|
|
|
|
gst_object_unref (sysclock);
|
2016-01-04 14:31:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
G_UNLOCK (clocks_lock);
|
|
|
|
|
|
|
|
g_free (self->priv->address);
|
|
|
|
self->priv->address = NULL;
|
|
|
|
|
|
|
|
if (self->priv->bus != NULL) {
|
|
|
|
gst_object_unref (self->priv->bus);
|
|
|
|
self->priv->bus = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_net_client_clock_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
|
|
|
|
gboolean update = FALSE;
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ADDRESS:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
g_free (self->priv->address);
|
|
|
|
self->priv->address = g_value_dup_string (value);
|
|
|
|
if (self->priv->address == NULL)
|
|
|
|
self->priv->address = g_strdup (DEFAULT_ADDRESS);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
case PROP_PORT:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->priv->port = g_value_get_int (value);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
case PROP_ROUNDTRIP_LIMIT:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->priv->roundtrip_limit = g_value_get_uint64 (value);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
update = TRUE;
|
|
|
|
break;
|
|
|
|
case PROP_MINIMUM_UPDATE_INTERVAL:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->priv->minimum_update_interval = g_value_get_uint64 (value);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
update = TRUE;
|
|
|
|
break;
|
|
|
|
case PROP_BUS:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
if (self->priv->bus)
|
|
|
|
gst_object_unref (self->priv->bus);
|
|
|
|
self->priv->bus = g_value_dup_object (value);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
update = TRUE;
|
|
|
|
break;
|
2016-03-09 11:44:24 +00:00
|
|
|
case PROP_BASE_TIME:{
|
|
|
|
GstClock *clock;
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
self->priv->base_time = g_value_get_uint64 (value);
|
2016-03-09 11:44:24 +00:00
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
self->priv->internal_base_time = gst_clock_get_time (clock);
|
|
|
|
gst_object_unref (clock);
|
2016-01-04 14:31:23 +00:00
|
|
|
break;
|
2016-03-09 11:44:24 +00:00
|
|
|
}
|
2016-01-04 14:31:23 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update && self->priv->internal_clock) {
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
G_LOCK (clocks_lock);
|
|
|
|
for (l = clocks; l; l = l->next) {
|
|
|
|
ClockCache *cache = l->data;
|
|
|
|
|
|
|
|
if (cache->clock == self->priv->internal_clock) {
|
|
|
|
update_clock_cache (cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
G_UNLOCK (clocks_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ADDRESS:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
g_value_set_string (value, self->priv->address);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
case PROP_PORT:
|
|
|
|
g_value_set_int (value, self->priv->port);
|
|
|
|
break;
|
|
|
|
case PROP_ROUNDTRIP_LIMIT:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
g_value_set_uint64 (value, self->priv->roundtrip_limit);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
case PROP_MINIMUM_UPDATE_INTERVAL:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
g_value_set_uint64 (value, self->priv->minimum_update_interval);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
case PROP_BUS:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
g_value_set_object (value, self->priv->bus);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
case PROP_BASE_TIME:
|
|
|
|
g_value_set_uint64 (value, self->priv->base_time);
|
|
|
|
break;
|
|
|
|
case PROP_INTERNAL_CLOCK:
|
|
|
|
g_value_set_object (value, self->priv->internal_clock);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-05 14:44:53 +00:00
|
|
|
gst_net_client_clock_synced_cb (GstClock * internal_clock, gboolean synced,
|
2016-01-04 14:31:23 +00:00
|
|
|
GstClock * self)
|
|
|
|
{
|
|
|
|
gst_clock_set_synced (self, synced);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_constructed (GObject * object)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
|
|
|
|
GstClock *internal_clock;
|
|
|
|
GList *l;
|
|
|
|
ClockCache *cache = NULL;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_net_client_clock_parent_class)->constructed (object);
|
|
|
|
|
|
|
|
G_LOCK (clocks_lock);
|
|
|
|
for (l = clocks; l; l = l->next) {
|
|
|
|
ClockCache *tmp = l->data;
|
|
|
|
GstNetClientInternalClock *internal_clock =
|
|
|
|
GST_NET_CLIENT_INTERNAL_CLOCK (tmp->clock);
|
|
|
|
|
|
|
|
if (strcmp (internal_clock->address, self->priv->address) == 0 &&
|
|
|
|
internal_clock->port == self->priv->port) {
|
|
|
|
cache = tmp;
|
2016-01-04 15:18:07 +00:00
|
|
|
|
|
|
|
if (cache->remove_id) {
|
|
|
|
gst_clock_id_unschedule (cache->remove_id);
|
|
|
|
cache->remove_id = NULL;
|
|
|
|
}
|
2016-01-04 14:31:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cache) {
|
|
|
|
cache = g_new0 (ClockCache, 1);
|
|
|
|
|
|
|
|
cache->clock =
|
|
|
|
g_object_new (GST_TYPE_NET_CLIENT_INTERNAL_CLOCK, "address",
|
|
|
|
self->priv->address, "port", self->priv->port, "is-ntp",
|
|
|
|
self->priv->is_ntp, NULL);
|
|
|
|
clocks = g_list_prepend (clocks, cache);
|
2016-05-30 10:11:13 +00:00
|
|
|
|
|
|
|
/* Not actually leaked but is cached for a while before being disposed,
|
|
|
|
* see gst_net_client_clock_finalize, so pretend it is to not confuse
|
|
|
|
* tests. */
|
|
|
|
GST_OBJECT_FLAG_SET (cache->clock, GST_OBJECT_FLAG_MAY_BE_LEAKED);
|
2016-01-04 14:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cache->clocks = g_list_prepend (cache->clocks, self);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (cache->clock);
|
|
|
|
if (gst_clock_is_synced (cache->clock))
|
|
|
|
gst_clock_set_synced (GST_CLOCK (self), TRUE);
|
|
|
|
self->priv->synced_id =
|
|
|
|
g_signal_connect (cache->clock, "synced",
|
2016-01-05 14:44:53 +00:00
|
|
|
G_CALLBACK (gst_net_client_clock_synced_cb), self);
|
2016-01-04 14:31:23 +00:00
|
|
|
GST_OBJECT_UNLOCK (cache->clock);
|
|
|
|
|
|
|
|
G_UNLOCK (clocks_lock);
|
|
|
|
|
|
|
|
self->priv->internal_clock = internal_clock = cache->clock;
|
|
|
|
|
|
|
|
/* all systems go, cap'n */
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
|
|
|
gst_net_client_clock_get_internal_time (GstClock * clock)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = GST_NET_CLIENT_CLOCK (clock);
|
|
|
|
|
2016-03-09 11:44:24 +00:00
|
|
|
if (!gst_clock_is_synced (self->priv->internal_clock)) {
|
|
|
|
GstClockTime now = gst_clock_get_internal_time (self->priv->internal_clock);
|
|
|
|
return gst_clock_adjust_with_calibration (self->priv->internal_clock, now,
|
|
|
|
self->priv->internal_base_time, self->priv->base_time, 1, 1);
|
|
|
|
}
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
return gst_clock_get_time (self->priv->internal_clock);
|
|
|
|
}
|
|
|
|
|
2005-11-23 16:10:38 +00:00
|
|
|
/**
|
|
|
|
* gst_net_client_clock_new:
|
|
|
|
* @name: a name for the clock
|
2015-12-30 14:57:29 +00:00
|
|
|
* @remote_address: the address or hostname of the remote clock provider
|
2005-11-23 16:10:38 +00:00
|
|
|
* @remote_port: the port of the remote clock provider
|
|
|
|
* @base_time: initial time of the clock
|
|
|
|
*
|
2016-01-04 14:31:23 +00:00
|
|
|
* Create a new #GstNetClientInternalClock that will report the time
|
2009-11-27 14:39:37 +00:00
|
|
|
* provided by the #GstNetTimeProvider on @remote_address and
|
2005-11-23 16:10:38 +00:00
|
|
|
* @remote_port.
|
|
|
|
*
|
|
|
|
* Returns: a new #GstClock that receives a time from the remote
|
|
|
|
* clock.
|
|
|
|
*/
|
2005-11-17 17:55:17 +00:00
|
|
|
GstClock *
|
2013-10-18 07:58:05 +00:00
|
|
|
gst_net_client_clock_new (const gchar * name, const gchar * remote_address,
|
2005-11-17 17:55:17 +00:00
|
|
|
gint remote_port, GstClockTime base_time)
|
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstClock *ret;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (remote_address != NULL, NULL);
|
|
|
|
g_return_val_if_fail (remote_port > 0, NULL);
|
2005-11-18 11:57:30 +00:00
|
|
|
g_return_val_if_fail (remote_port <= G_MAXUINT16, NULL);
|
2005-11-17 17:55:17 +00:00
|
|
|
g_return_val_if_fail (base_time != GST_CLOCK_TIME_NONE, NULL);
|
|
|
|
|
|
|
|
ret = g_object_new (GST_TYPE_NET_CLIENT_CLOCK, "address", remote_address,
|
2015-06-06 16:42:18 +00:00
|
|
|
"port", remote_port, "base-time", base_time, NULL);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
return ret;
|
2015-06-06 16:42:18 +00:00
|
|
|
}
|
2015-06-06 19:43:05 +00:00
|
|
|
|
2016-01-05 11:41:08 +00:00
|
|
|
G_DEFINE_TYPE (GstNtpClock, gst_ntp_clock, GST_TYPE_NET_CLIENT_CLOCK);
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ntp_clock_class_init (GstNtpClockClass * klass)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ntp_clock_init (GstNtpClock * self)
|
|
|
|
{
|
|
|
|
GST_NET_CLIENT_CLOCK (self)->priv->is_ntp = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_ntp_clock_new:
|
|
|
|
* @name: a name for the clock
|
2015-12-30 14:57:29 +00:00
|
|
|
* @remote_address: the address or hostname of the remote clock provider
|
2015-06-06 19:43:05 +00:00
|
|
|
* @remote_port: the port of the remote clock provider
|
|
|
|
* @base_time: initial time of the clock
|
|
|
|
*
|
|
|
|
* Create a new #GstNtpClock that will report the time provided by
|
|
|
|
* the NTPv4 server on @remote_address and @remote_port.
|
|
|
|
*
|
|
|
|
* Returns: a new #GstClock that receives a time from the remote
|
|
|
|
* clock.
|
2015-06-07 07:32:39 +00:00
|
|
|
*
|
|
|
|
* Since: 1.6
|
2015-06-06 19:43:05 +00:00
|
|
|
*/
|
|
|
|
GstClock *
|
|
|
|
gst_ntp_clock_new (const gchar * name, const gchar * remote_address,
|
|
|
|
gint remote_port, GstClockTime base_time)
|
|
|
|
{
|
2016-01-04 14:31:23 +00:00
|
|
|
GstClock *ret;
|
2015-06-06 19:43:05 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (remote_address != NULL, NULL);
|
|
|
|
g_return_val_if_fail (remote_port > 0, NULL);
|
|
|
|
g_return_val_if_fail (remote_port <= G_MAXUINT16, NULL);
|
|
|
|
g_return_val_if_fail (base_time != GST_CLOCK_TIME_NONE, NULL);
|
|
|
|
|
|
|
|
ret = g_object_new (GST_TYPE_NTP_CLOCK, "address", remote_address,
|
|
|
|
"port", remote_port, "base-time", base_time, NULL);
|
|
|
|
|
2016-01-04 14:31:23 +00:00
|
|
|
return ret;
|
2015-06-06 19:43:05 +00:00
|
|
|
}
|