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>
|
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
|
|
|
|
*
|
|
|
|
* This object implements a custom #GstClock that synchronizes its time
|
|
|
|
* to a remote time provider such as #GstNetTimeProvider.
|
|
|
|
*
|
|
|
|
* A new clock is created with gst_net_client_clock_new() which takes the
|
|
|
|
* address and port of the remote time provider along with a name and
|
|
|
|
* an initial time.
|
|
|
|
*
|
|
|
|
* This clock will poll the time provider and will update its calibration
|
|
|
|
* parameters based on the local and remote observations.
|
|
|
|
*
|
|
|
|
* Various parameters of the clock can be configured with the parent #GstClock
|
|
|
|
* "timeout", "window-size" and "window-threshold" object properties.
|
|
|
|
*
|
|
|
|
* A #GstNetClientClock is typically set on a #GstPipeline with
|
|
|
|
* gst_pipeline_use_clock().
|
|
|
|
*
|
|
|
|
* Last reviewed on 2005-11-23 (0.9.5)
|
|
|
|
*/
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstnettimepacket.h"
|
|
|
|
#include "gstnetclientclock.h"
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
#include <gio/gio.h>
|
2007-10-31 22:01:03 +00:00
|
|
|
|
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)
|
|
|
|
|
2005-12-06 19:29:15 +00:00
|
|
|
#define DEFAULT_ADDRESS "127.0.0.1"
|
|
|
|
#define DEFAULT_PORT 5637
|
|
|
|
#define DEFAULT_TIMEOUT GST_SECOND
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ADDRESS,
|
2010-05-05 15:33:51 +00:00
|
|
|
PROP_PORT
|
2005-11-17 17:55:17 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
#define GST_NET_CLIENT_CLOCK_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NET_CLIENT_CLOCK, GstNetClientClockPrivate))
|
|
|
|
|
|
|
|
struct _GstNetClientClockPrivate
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
GSocket *socket;
|
|
|
|
GSocketAddress *servaddr;
|
|
|
|
GCancellable *cancel;
|
|
|
|
|
|
|
|
GstClockTime timeout_expiration;
|
|
|
|
|
|
|
|
gchar *address;
|
|
|
|
gint port;
|
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");
|
2011-04-18 15:33:34 +00:00
|
|
|
#define gst_net_client_clock_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstNetClientClock, gst_net_client_clock,
|
|
|
|
GST_TYPE_SYSTEM_CLOCK, _do_init);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
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_stop (GstNetClientClock * self);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_class_init (GstNetClientClockClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
2006-05-11 18:10:34 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
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
|
|
|
g_type_class_add_private (klass, sizeof (GstNetClientClockPrivate));
|
|
|
|
|
2005-11-17 17:55:17 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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",
|
|
|
|
DEFAULT_ADDRESS, G_PARAM_READWRITE | 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,
|
2008-03-22 14:56:17 +00:00
|
|
|
DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-18 15:33:34 +00:00
|
|
|
gst_net_client_clock_init (GstNetClientClock * self)
|
2005-11-17 17:55:17 +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
|
|
|
GstClock *clock = GST_CLOCK_CAST (self);
|
2012-02-27 08:02:07 +00:00
|
|
|
GstNetClientClockPrivate *priv;
|
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
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
self->priv = priv = GST_NET_CLIENT_CLOCK_GET_PRIVATE (self);
|
2006-09-05 08:35:20 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->port = DEFAULT_PORT;
|
|
|
|
priv->address = g_strdup (DEFAULT_ADDRESS);
|
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
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
gst_clock_set_timeout (clock, DEFAULT_TIMEOUT);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->thread = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
priv->servaddr = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
if (self->priv->thread) {
|
2005-11-17 17:55:17 +00:00
|
|
|
gst_net_client_clock_stop (self);
|
|
|
|
}
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
g_free (self->priv->address);
|
|
|
|
self->priv->address = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
if (self->priv->servaddr != NULL) {
|
|
|
|
g_object_unref (self->priv->servaddr);
|
|
|
|
self->priv->servaddr = NULL;
|
|
|
|
}
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
if (self->priv->socket != NULL) {
|
|
|
|
g_socket_close (self->priv->socket, NULL);
|
|
|
|
g_object_unref (self->priv->socket);
|
|
|
|
self->priv->socket = NULL;
|
|
|
|
}
|
2006-09-05 08:35:20 +00:00
|
|
|
|
2005-11-17 17:55:17 +00:00
|
|
|
G_OBJECT_CLASS (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);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ADDRESS:
|
2010-05-05 15:33:51 +00:00
|
|
|
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);
|
2005-11-17 17:55:17 +00:00
|
|
|
break;
|
|
|
|
case PROP_PORT:
|
2010-05-05 15:33:51 +00:00
|
|
|
self->priv->port = g_value_get_int (value);
|
2005-11-17 17:55:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2010-05-05 15:33:51 +00:00
|
|
|
g_value_set_string (value, self->priv->address);
|
2005-11-17 17:55:17 +00:00
|
|
|
break;
|
|
|
|
case PROP_PORT:
|
2010-05-05 15:33:51 +00:00
|
|
|
g_value_set_int (value, self->priv->port);
|
2005-11-17 17:55:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_observe_times (GstNetClientClock * self,
|
|
|
|
GstClockTime local_1, GstClockTime remote, GstClockTime local_2)
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GstClockTime current_timeout;
|
2005-11-17 17:55:17 +00:00
|
|
|
GstClockTime local_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;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
if (local_2 < local_1)
|
|
|
|
goto bogus_observation;
|
|
|
|
|
2005-11-18 14:13:28 +00:00
|
|
|
local_avg = (local_2 + local_1) / 2;
|
2005-11-17 17:55:17 +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
|
|
|
|
2012-02-27 08:02:07 +00:00
|
|
|
if (gst_clock_add_observation (GST_CLOCK (self), local_avg, remote,
|
|
|
|
&r_squared)) {
|
2005-11-17 17:55:17 +00:00
|
|
|
/* geto formula */
|
2010-05-05 15:33:51 +00:00
|
|
|
current_timeout = (1e-3 / (1 - MIN (r_squared, 0.99999))) * GST_SECOND;
|
2012-02-27 08:02:07 +00:00
|
|
|
current_timeout = MIN (current_timeout, gst_clock_get_timeout (clock));
|
|
|
|
} else {
|
|
|
|
current_timeout = 0;
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
2012-02-27 08:02:07 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_INFO ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (current_timeout));
|
|
|
|
self->priv->timeout_expiration = gst_util_get_timestamp () + current_timeout;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
bogus_observation:
|
|
|
|
{
|
2006-10-05 14:26:08 +00:00
|
|
|
GST_WARNING_OBJECT (self, "time packet receive time < send time (%"
|
|
|
|
GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (local_1),
|
2005-11-17 17:55:17 +00:00
|
|
|
GST_TIME_ARGS (local_2));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gst_net_client_clock_thread (gpointer data)
|
|
|
|
{
|
|
|
|
GstNetClientClock *self = data;
|
|
|
|
GstNetTimePacket *packet;
|
2010-05-05 15:33:51 +00:00
|
|
|
GSocket *socket = self->priv->socket;
|
|
|
|
GError *err = NULL;
|
2005-11-23 13:14:46 +00:00
|
|
|
GstClock *clock = data;
|
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);
|
|
|
|
|
|
|
|
while (!g_cancellable_is_cancelled (self->priv->cancel)) {
|
2012-08-10 08:19:25 +00:00
|
|
|
GstClockTime expiration_time = self->priv->timeout_expiration;
|
|
|
|
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,
|
|
|
|
self->priv->cancel, &err)) {
|
|
|
|
/* 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
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
packet = gst_net_time_packet_new (NULL);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self));
|
2010-05-05 15:33:51 +00:00
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"sending packet, local time = %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (packet->local_time));
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
gst_net_time_packet_send (packet, self->priv->socket,
|
|
|
|
self->priv->servaddr, NULL);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-08-10 08:19:25 +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) */
|
|
|
|
self->priv->timeout_expiration =
|
|
|
|
gst_util_get_timestamp () + gst_clock_get_timeout (clock);
|
|
|
|
} 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 */
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
new_local = gst_clock_get_internal_time (GST_CLOCK (self));
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
packet = gst_net_time_packet_receive (socket, NULL, &err);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2012-08-10 08:19:25 +00:00
|
|
|
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 */
|
|
|
|
gst_net_client_clock_observe_times (self, packet->local_time,
|
|
|
|
packet->remote_time, new_local);
|
|
|
|
|
|
|
|
g_free (packet);
|
|
|
|
} else if (err != NULL) {
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_WARNING_OBJECT (self, "receive error: %s", err->message);
|
2012-08-10 08:19:25 +00:00
|
|
|
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
|
|
|
|
gst_net_client_clock_start (GstNetClientClock * self)
|
|
|
|
{
|
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;
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
g_return_val_if_fail (self->priv->address != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (self->priv->servaddr == NULL, FALSE);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
|
|
|
|
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");
|
|
|
|
inetaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
|
|
|
|
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;
|
|
|
|
|
2006-05-25 15:52:19 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "socket opened on UDP port %hd",
|
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);
|
|
|
|
|
|
|
|
/* create target address */
|
|
|
|
inetaddr = g_inet_address_new_from_string (self->priv->address);
|
|
|
|
|
|
|
|
if (inetaddr == NULL)
|
2005-11-17 17:55:17 +00:00
|
|
|
goto bad_address;
|
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
servaddr = g_inet_socket_address_new (inetaddr, self->priv->port);
|
|
|
|
g_object_unref (inetaddr);
|
|
|
|
|
|
|
|
g_assert (servaddr != NULL);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "will communicate with %s:%d", self->priv->address,
|
|
|
|
self->priv->port);
|
2005-11-17 17:55:17 +00:00
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
self->priv->cancel = g_cancellable_new ();
|
|
|
|
self->priv->socket = socket;
|
|
|
|
self->priv->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
|
|
|
|
2010-05-05 15:33:51 +00:00
|
|
|
self->priv->thread = g_thread_try_new ("GstNetClientClock",
|
2011-12-03 13:58:51 +00:00
|
|
|
gst_net_client_clock_thread, self, &error);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
bad_address:
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
GST_ERROR_OBJECT (self, "inet_address_new_from_string('%s') failed",
|
|
|
|
self->priv->address);
|
|
|
|
g_object_unref (socket);
|
2005-11-17 17:55:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_thread:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (self, "could not create thread: %s", error->message);
|
2010-05-05 15:33:51 +00:00
|
|
|
g_object_unref (self->priv->servaddr);
|
|
|
|
self->priv->servaddr = NULL;
|
|
|
|
g_object_unref (self->priv->socket);
|
|
|
|
self->priv->socket = NULL;
|
2005-11-17 17:55:17 +00:00
|
|
|
g_error_free (error);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_net_client_clock_stop (GstNetClientClock * self)
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
if (self->priv->thread == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "stopping...");
|
|
|
|
g_cancellable_cancel (self->priv->cancel);
|
|
|
|
|
|
|
|
g_thread_join (self->priv->thread);
|
|
|
|
self->priv->thread = NULL;
|
|
|
|
|
|
|
|
g_object_unref (self->priv->cancel);
|
|
|
|
self->priv->cancel = NULL;
|
|
|
|
|
|
|
|
g_object_unref (self->priv->servaddr);
|
|
|
|
self->priv->servaddr = NULL;
|
|
|
|
|
|
|
|
g_object_unref (self->priv->socket);
|
|
|
|
self->priv->socket = NULL;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "stopped");
|
2005-11-17 17:55:17 +00:00
|
|
|
}
|
|
|
|
|
2005-11-23 16:10:38 +00:00
|
|
|
/**
|
|
|
|
* gst_net_client_clock_new:
|
|
|
|
* @name: a name for the clock
|
|
|
|
* @remote_address: the address of the remote clock provider
|
|
|
|
* @remote_port: the port of the remote clock provider
|
|
|
|
* @base_time: initial time of the clock
|
|
|
|
*
|
|
|
|
* Create a new #GstNetClientClock 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 *
|
|
|
|
gst_net_client_clock_new (gchar * name, const gchar * remote_address,
|
|
|
|
gint remote_port, GstClockTime base_time)
|
|
|
|
{
|
2010-05-05 15:33:51 +00:00
|
|
|
/* FIXME: gst_net_client_clock_new() should be a thin wrapper for g_object_new() */
|
2005-11-17 17:55:17 +00:00
|
|
|
GstNetClientClock *ret;
|
2005-11-18 10:54:55 +00:00
|
|
|
GstClockTime internal;
|
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,
|
|
|
|
"port", remote_port, NULL);
|
|
|
|
|
2005-11-18 10:54:55 +00:00
|
|
|
/* gst_clock_get_time() values are guaranteed to be increasing. because no one
|
|
|
|
* has called get_time on this clock yet we are free to adjust to any value
|
|
|
|
* without worrying about worrying about MAX() issues with the clock's
|
|
|
|
* internal time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* update our internal time so get_time() give something around base_time.
|
|
|
|
assume that the rate is 1 in the beginning. */
|
|
|
|
internal = gst_clock_get_internal_time (GST_CLOCK (ret));
|
2005-11-23 12:36:00 +00:00
|
|
|
gst_clock_set_calibration (GST_CLOCK (ret), internal, base_time, 1, 1);
|
2005-11-18 10:54:55 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GstClockTime now = gst_clock_get_time (GST_CLOCK (ret));
|
|
|
|
|
2009-10-13 16:12:50 +00:00
|
|
|
if (GST_CLOCK_DIFF (now, base_time) > 0 ||
|
|
|
|
GST_CLOCK_DIFF (now, base_time + GST_SECOND) < 0) {
|
2005-11-18 10:54:55 +00:00
|
|
|
g_warning ("unable to set the base time, expect sync problems!");
|
2009-10-13 16:12:50 +00:00
|
|
|
}
|
2005-11-18 10:54:55 +00:00
|
|
|
}
|
|
|
|
|
2005-11-17 17:55:17 +00:00
|
|
|
if (!gst_net_client_clock_start (ret))
|
|
|
|
goto failed_start;
|
|
|
|
|
|
|
|
/* all systems go, cap'n */
|
|
|
|
return (GstClock *) ret;
|
|
|
|
|
|
|
|
failed_start:
|
|
|
|
{
|
|
|
|
/* already printed a nice error */
|
|
|
|
gst_object_unref (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|