/* The create() virtual function is responsible for returning the next buffer. * We just pop buffers off of the queue and block if necessary. */ static GstFlowReturn shell_recorder_src_create (GstPushSrc *push_src, GstBuffer **buffer_out) { ShellRecorderSrc *src = SHELL_RECORDER_SRC (push_src); GstBuffer *buffer; if (src->closed) return GST_FLOW_EOS; buffer = g_async_queue_pop (src->queue); if (src->last_frame_time == 0) src->last_frame_time = gst_clock_get_time (GST_CLOCK (src->clock)); if (buffer == RECORDER_QUEUE_END) { /* Returning UNEXPECTED here will cause a EOS message to be sent */ src->closed = TRUE; return GST_FLOW_EOS; } shell_recorder_src_update_memory_used (src, - (int)(gst_buffer_get_size(buffer) / 1024)); *buffer_out = buffer; GST_BUFFER_DURATION(*buffer_out) = GST_CLOCK_DIFF (src->last_frame_time, gst_clock_get_time (GST_CLOCK (src->clock))); src->last_frame_time = gst_clock_get_time (GST_CLOCK (src->clock)); return GST_FLOW_OK; }
/** * 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 * provided by the #GstNetClockProvider on @remote_address and * @remote_port. * * Returns: a new #GstClock that receives a time from the remote * clock. */ GstClock * gst_net_client_clock_new (gchar * name, const gchar * remote_address, gint remote_port, GstClockTime base_time) { GstNetClientClock *ret; GstClockTime internal; 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_NET_CLIENT_CLOCK, "address", remote_address, "port", remote_port, NULL); /* 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)); gst_clock_set_calibration (GST_CLOCK (ret), internal, base_time, 1, 1); { GstClockTime now = gst_clock_get_time (GST_CLOCK (ret)); if (now < base_time || now > base_time + GST_SECOND) g_warning ("unable to set the base time, expect sync problems!"); } if ((ret->priv->fdset = gst_poll_new (TRUE)) == NULL) goto no_fdset; if (!gst_net_client_clock_start (ret)) goto failed_start; /* all systems go, cap'n */ return (GstClock *) ret; no_fdset: { GST_ERROR_OBJECT (ret, "could not create an fdset: %s (%d)", g_strerror (errno), errno); gst_object_unref (ret); return NULL; } failed_start: { /* already printed a nice error */ gst_object_unref (ret); return NULL; } }
/** * 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 * provided by the #GstNetTimeProvider on @remote_address and * @remote_port. * * Returns: a new #GstClock that receives a time from the remote * clock. */ GstClock * gst_net_client_clock_new (gchar * name, const gchar * remote_address, gint remote_port, GstClockTime base_time) { /* FIXME: gst_net_client_clock_new() should be a thin wrapper for g_object_new() */ GstNetClientClock *ret; GstClockTime internal; 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_NET_CLIENT_CLOCK, "address", remote_address, "port", remote_port, NULL); /* 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)); gst_clock_set_calibration (GST_CLOCK (ret), internal, base_time, 1, 1); { GstClockTime now = gst_clock_get_time (GST_CLOCK (ret)); if (GST_CLOCK_DIFF (now, base_time) > 0 || GST_CLOCK_DIFF (now, base_time + GST_SECOND) < 0) { g_warning ("unable to set the base time, expect sync problems!"); } } 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; } }
int main (void) { GstClock *clock; gboolean lockstep = TRUE; clock = GST_CLOCK (g_object_new (LP_TYPE_CLOCK, NULL)); g_assert_nonnull (clock); g_object_get (clock, "lockstep", &lockstep, NULL); g_assert (!lockstep); g_object_set (clock, "lockstep", TRUE, NULL); g_object_get (clock, "lockstep", &lockstep, NULL); g_assert (lockstep); lockstep = FALSE; g_object_set (clock, "lockstep", TRUE, NULL); /* vacuous set */ g_object_get (clock, "lockstep", &lockstep, NULL); g_assert (lockstep); g_object_unref (clock); exit (EXIT_SUCCESS); }
static void gst_clock_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstClock *clock; clock = GST_CLOCK (object); switch (prop_id) { case PROP_STATS: GST_OBJECT_LOCK (clock); g_value_set_boolean (value, clock->stats); GST_OBJECT_UNLOCK (clock); break; case PROP_WINDOW_SIZE: GST_CLOCK_SLAVE_LOCK (clock); g_value_set_int (value, clock->window_size); GST_CLOCK_SLAVE_UNLOCK (clock); break; case PROP_WINDOW_THRESHOLD: GST_CLOCK_SLAVE_LOCK (clock); g_value_set_int (value, clock->window_threshold); GST_CLOCK_SLAVE_UNLOCK (clock); break; case PROP_TIMEOUT: GST_CLOCK_SLAVE_LOCK (clock); g_value_set_uint64 (value, clock->timeout); GST_CLOCK_SLAVE_UNLOCK (clock); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static GstClock * alsaspdifsink_provide_clock (GstElement * elem) { AlsaSPDIFSink *sink = ALSASPDIFSINK (elem); return GST_CLOCK (gst_object_ref (sink->clock)); }
static void process_entry_context_unlocked (GstTestClock * test_clock, GstClockEntryContext * ctx) { GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); GstClockEntry *entry = ctx->clock_entry; if (ctx->time_diff >= 0) GST_CLOCK_ENTRY_STATUS (entry) = GST_CLOCK_OK; else GST_CLOCK_ENTRY_STATUS (entry) = GST_CLOCK_EARLY; if (entry->func != NULL) { GST_OBJECT_UNLOCK (test_clock); entry->func (GST_CLOCK (test_clock), priv->internal_time, entry, entry->user_data); GST_OBJECT_LOCK (test_clock); } gst_test_clock_remove_entry (test_clock, entry); if (GST_CLOCK_ENTRY_TYPE (entry) == GST_CLOCK_ENTRY_PERIODIC) { GST_CLOCK_ENTRY_TIME (entry) += GST_CLOCK_ENTRY_INTERVAL (entry); if (entry->func != NULL) gst_test_clock_add_entry (test_clock, entry, NULL); } }
static void gst_clock_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstClock *clock; GstClockPrivate *priv; clock = GST_CLOCK (object); priv = clock->priv; switch (prop_id) { case PROP_WINDOW_SIZE: GST_CLOCK_SLAVE_LOCK (clock); priv->window_size = g_value_get_int (value); priv->window_threshold = MIN (priv->window_threshold, priv->window_size); priv->times = g_renew (GstClockTime, priv->times, 4 * priv->window_size); /* restart calibration */ priv->filling = TRUE; priv->time_index = 0; GST_CLOCK_SLAVE_UNLOCK (clock); break; case PROP_WINDOW_THRESHOLD: GST_CLOCK_SLAVE_LOCK (clock); priv->window_threshold = MIN (g_value_get_int (value), priv->window_size); GST_CLOCK_SLAVE_UNLOCK (clock); break; case PROP_TIMEOUT: gst_clock_set_timeout (clock, g_value_get_uint64 (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static gboolean gst_adaptive_demux_update_test_clock (gpointer user_data) { GstAdaptiveDemuxTestEnginePrivate *priv = (GstAdaptiveDemuxTestEnginePrivate *) user_data; GstClockID id; GstClockTime next_entry; GstTestClock *clock = GST_TEST_CLOCK (priv->engine.clock); fail_unless (clock != NULL); next_entry = gst_test_clock_get_next_entry_time (clock); if (next_entry != GST_CLOCK_TIME_NONE) { /* tests that do not want the manifest to update will set the update period * to a big value, eg 500s. The manifest update task will register an alarm * for that value. * We do not want the clock to jump to that. If it does, the manifest update * task will keep scheduling and use all the cpu power, starving the other * threads. * Usually the test require the clock to update with approx 3s, so we will * allow only updates smaller than 100s */ GstClockTime curr_time = gst_clock_get_time (GST_CLOCK (clock)); if (next_entry - curr_time < 100 * GST_SECOND) { gst_test_clock_set_time (clock, next_entry); id = gst_test_clock_process_next_clock_id (clock); fail_unless (id != NULL); gst_clock_id_unref (id); } } return TRUE; }
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; if (cache->remove_id) { gst_clock_id_unschedule (cache->remove_id); cache->remove_id = NULL; } 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); /* 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); } 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", G_CALLBACK (gst_net_client_clock_synced_cb), self); GST_OBJECT_UNLOCK (cache->clock); G_UNLOCK (clocks_lock); self->priv->internal_clock = internal_clock = cache->clock; /* all systems go, cap'n */ }
static gint gst_net_client_clock_do_select (GstNetClientClock * self) { while (TRUE) { GstClockTime diff; gint ret; GST_LOG_OBJECT (self, "doing select"); diff = gst_clock_get_internal_time (GST_CLOCK (self)); ret = gst_poll_wait (self->priv->fdset, self->current_timeout); diff = gst_clock_get_internal_time (GST_CLOCK (self)) - diff; if (diff > self->current_timeout) self->current_timeout = 0; else self->current_timeout -= diff; GST_LOG_OBJECT (self, "select returned %d", ret); if (ret < 0 && errno != EBUSY) { if (errno != EAGAIN && errno != EINTR) goto select_error; else continue; } else { return ret; } g_assert_not_reached (); /* log errors and keep going */ select_error: { GST_WARNING_OBJECT (self, "select error %d: %s (%d)", ret, g_strerror (errno), errno); continue; } } g_assert_not_reached (); return -1; }
static void gst_uri_transcode_bin_constructed (GObject * object) { #if HAVE_GETRUSAGE GstUriTranscodeBin *self = GST_URI_TRANSCODE_BIN (object); self->cpu_clock = GST_CLOCK (gst_cpu_throttling_clock_new (self->wanted_cpu_usage)); gst_pipeline_use_clock (GST_PIPELINE (self), self->cpu_clock); #endif ((GObjectClass *) parent_class)->constructed (object); }
static void gst_clock_dispose (GObject * object) { GstClock *clock = GST_CLOCK (object); GstClock **master_p; GST_OBJECT_LOCK (clock); master_p = &clock->master; gst_object_replace ((GstObject **) master_p, NULL); GST_OBJECT_UNLOCK (clock); G_OBJECT_CLASS (parent_class)->dispose (object); }
int main (void) { GstClock *clock; int nonexistent; clock = GST_CLOCK (g_object_new (LP_TYPE_CLOCK, NULL)); g_assert_nonnull (clock); g_object_get (clock, "nonexistent", &nonexistent, NULL); g_object_unref (clock); exit (EXIT_SUCCESS); }
static void gst_cpu_throttling_clock_init (GstCpuThrottlingClock * self) { self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_CPU_THROTTLING_CLOCK, GstCpuThrottlingClockPrivate); self->priv->current_wait_time = GST_MSECOND; self->priv->wanted_cpu_usage = 100; self->priv->timer = gst_poll_new_timer (); self->priv->time_between_evals = GST_SECOND / 4; self->priv->sclock = GST_CLOCK (gst_system_clock_obtain ()); getrusage (RUSAGE_SELF, &self->priv->last_usage); }
static void gst_clock_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstClock *clock; clock = GST_CLOCK (object); switch (prop_id) { case PROP_STATS: GST_OBJECT_LOCK (clock); clock->stats = g_value_get_boolean (value); GST_OBJECT_UNLOCK (clock); g_object_notify (object, "stats"); break; case PROP_WINDOW_SIZE: GST_CLOCK_SLAVE_LOCK (clock); clock->window_size = g_value_get_int (value); clock->window_threshold = MIN (clock->window_threshold, clock->window_size); clock->times = g_renew (GstClockTime, clock->times, 4 * clock->window_size); /* restart calibration */ clock->filling = TRUE; clock->time_index = 0; GST_CLOCK_SLAVE_UNLOCK (clock); break; case PROP_WINDOW_THRESHOLD: GST_CLOCK_SLAVE_LOCK (clock); clock->window_threshold = MIN (g_value_get_int (value), clock->window_size); GST_CLOCK_SLAVE_UNLOCK (clock); break; case PROP_TIMEOUT: GST_CLOCK_SLAVE_LOCK (clock); clock->timeout = g_value_get_uint64 (value); GST_CLOCK_SLAVE_UNLOCK (clock); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void gst_clock_finalize (GObject * object) { GstClock *clock = GST_CLOCK (object); GST_CLOCK_SLAVE_LOCK (clock); if (clock->priv->clockid) { gst_clock_id_unschedule (clock->priv->clockid); gst_clock_id_unref (clock->priv->clockid); clock->priv->clockid = NULL; } g_free (clock->priv->times); clock->priv->times = NULL; GST_CLOCK_SLAVE_UNLOCK (clock); g_mutex_clear (&clock->priv->slave_lock); G_OBJECT_CLASS (parent_class)->finalize (object); }
gint main (gint argc, gchar ** argv) { GOptionContext *opt_ctx; GstClock *clock; GError *err = NULL; opt_ctx = g_option_context_new ("- GStreamer PTP clock test app"); g_option_context_add_main_entries (opt_ctx, opt_entries, NULL); g_option_context_add_group (opt_ctx, gst_init_get_option_group ()); if (!g_option_context_parse (opt_ctx, &argc, &argv, &err)) g_error ("Error parsing options: %s", err->message); g_clear_error (&err); g_option_context_free (opt_ctx); if (!gst_ptp_init (GST_PTP_CLOCK_ID_NONE, NULL)) g_error ("failed to init ptp"); if (stats) gst_ptp_statistics_callback_add (stats_cb, NULL, NULL); clock = gst_ptp_clock_new ("test-clock", domain); gst_clock_wait_for_sync (GST_CLOCK (clock), GST_CLOCK_TIME_NONE); while (TRUE) { GstClockTime local, remote; GstClockTimeDiff diff; local = g_get_real_time () * 1000; remote = gst_clock_get_time (clock); diff = GST_CLOCK_DIFF (local, remote); g_print ("local: %" GST_TIME_FORMAT " ptp: %" GST_TIME_FORMAT " diff: %s%" GST_TIME_FORMAT "\n", GST_TIME_ARGS (local), GST_TIME_ARGS (remote), (diff < 0 ? "-" : " "), GST_TIME_ARGS (ABS (diff))); g_usleep (100000); } return 0; }
static void gst_net_client_internal_clock_init (GstNetClientInternalClock * self) { GST_OBJECT_FLAG_SET (self, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC); self->port = DEFAULT_PORT; self->address = g_strdup (DEFAULT_ADDRESS); self->is_ntp = FALSE; gst_clock_set_timeout (GST_CLOCK (self), DEFAULT_TIMEOUT); self->thread = NULL; 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; }
static void gst_test_clock_add_entry (GstTestClock * test_clock, GstClockEntry * entry, GstClockTimeDiff * jitter) { GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); GstClockTime now; GstClockEntryContext *ctx; now = gst_clock_adjust_unlocked (GST_CLOCK (test_clock), priv->internal_time); if (jitter != NULL) *jitter = GST_CLOCK_DIFF (GST_CLOCK_ENTRY_TIME (entry), now); ctx = g_slice_new (GstClockEntryContext); ctx->clock_entry = GST_CLOCK_ENTRY (gst_clock_id_ref (entry)); ctx->time_diff = GST_CLOCK_DIFF (now, GST_CLOCK_ENTRY_TIME (entry)); priv->entry_contexts = g_list_insert_sorted (priv->entry_contexts, ctx, gst_clock_entry_context_compare_func); g_cond_broadcast (&priv->entry_added_cond); }
static void gst_net_client_clock_observe_times (GstNetClientClock * self, GstClockTime local_1, GstClockTime remote, GstClockTime local_2) { GstClockTime current_timeout; GstClockTime local_avg; gdouble r_squared; GstClock *clock; if (local_2 < local_1) goto bogus_observation; local_avg = (local_2 + local_1) / 2; clock = GST_CLOCK_CAST (self); if (gst_clock_add_observation (GST_CLOCK (self), local_avg, remote, &r_squared)) { /* geto formula */ current_timeout = (1e-3 / (1 - MIN (r_squared, 0.99999))) * GST_SECOND; current_timeout = MIN (current_timeout, gst_clock_get_timeout (clock)); } else { current_timeout = 0; } GST_INFO ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (current_timeout)); self->priv->timeout_expiration = gst_util_get_timestamp () + current_timeout; return; bogus_observation: { GST_WARNING_OBJECT (self, "time packet receive time < send time (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (local_1), GST_TIME_ARGS (local_2)); return; } }
static void gst_net_client_clock_observe_times (GstNetClientClock * self, GstClockTime local_1, GstClockTime remote, GstClockTime local_2) { GstClockTime local_avg; gdouble r_squared; GstClock *clock; if (local_2 < local_1) goto bogus_observation; local_avg = (local_2 + local_1) / 2; clock = GST_CLOCK_CAST (self); gst_clock_add_observation (GST_CLOCK (self), local_avg, remote, &r_squared); GST_CLOCK_SLAVE_LOCK (self); if (clock->filling) { self->current_timeout = 0; } else { /* geto formula */ self->current_timeout = (1e-3 / (1 - MIN (r_squared, 0.99999))) * GST_SECOND; self->current_timeout = MIN (self->current_timeout, clock->timeout); } GST_CLOCK_SLAVE_UNLOCK (clock); return; bogus_observation: { GST_WARNING_OBJECT (self, "time packet receive time < send time (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (local_1), GST_TIME_ARGS (local_2)); return; } }
static GstClock * gst_wasapi_src_provide_clock (GstElement * element) { GstWasapiSrc *self = GST_WASAPI_SRC (element); GstClock *clock; GST_OBJECT_LOCK (self); if (self->client_clock == NULL) goto wrong_state; clock = GST_CLOCK (gst_object_ref (self->clock)); GST_OBJECT_UNLOCK (self); return clock; /* ERRORS */ wrong_state: { GST_OBJECT_UNLOCK (self); GST_DEBUG_OBJECT (self, "IAudioClock not acquired"); return NULL; } }
static void gst_net_client_internal_clock_observe_times (GstNetClientInternalClock * self, GstClockTime local_1, GstClockTime remote_1, GstClockTime remote_2, GstClockTime local_2) { GstClockTime current_timeout = 0; GstClockTime local_avg, remote_avg; gdouble r_squared; GstClock *clock; GstClockTime rtt, rtt_limit, min_update_interval; /* Use for discont tracking */ GstClockTime time_before = 0; GstClockTime min_guess = 0; GstClockTimeDiff time_discont = 0; gboolean synched, now_synched; GstClockTime internal_time, external_time, rate_num, rate_den; GstClockTime orig_internal_time, orig_external_time, orig_rate_num, orig_rate_den; GstClockTime max_discont; GstClockTime last_rtts[MEDIAN_PRE_FILTERING_WINDOW]; GstClockTime median; gint i; GST_OBJECT_LOCK (self); rtt_limit = self->roundtrip_limit; 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); /* If the server told us a poll interval and it's bigger than the * one configured via the property, use the server's */ 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; else min_update_interval = self->minimum_update_interval; GST_OBJECT_UNLOCK (self); 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)); goto bogus_observation; } 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); if ((rtt_limit > 0) && (rtt > rtt_limit)) { GST_LOG_OBJECT (self, "Dropping observation: RTT %" GST_TIME_FORMAT " > limit %" GST_TIME_FORMAT, GST_TIME_ARGS (rtt), GST_TIME_ARGS (rtt_limit)); goto bogus_observation; } for (i = 1; i < MEDIAN_PRE_FILTERING_WINDOW; i++) self->last_rtts[i - 1] = self->last_rtts[i]; self->last_rtts[i - 1] = rtt; if (self->last_rtts_missing) { self->last_rtts_missing--; } else { memcpy (&last_rtts, &self->last_rtts, sizeof (last_rtts)); 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; } } /* 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 */ 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; else self->rtt_avg = (15 * self->rtt_avg + rtt) / 16; if (rtt > 2 * self->rtt_avg) { GST_LOG_OBJECT (self, "Dropping observation, long RTT %" GST_TIME_FORMAT " > 2 * avg %" GST_TIME_FORMAT, GST_TIME_ARGS (rtt), GST_TIME_ARGS (self->rtt_avg)); goto bogus_observation; } /* 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. */ local_avg = (local_2 + local_1) / 2; remote_avg = (remote_2 + remote_1) / 2; GST_LOG_OBJECT (self, "remoteavg %" G_GUINT64_FORMAT " localavg %" G_GUINT64_FORMAT, remote_avg, local_avg); clock = GST_CLOCK_CAST (self); /* Store what the clock produced as 'now' before this update */ gst_clock_get_calibration (GST_CLOCK_CAST (self), &orig_internal_time, &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; min_guess = gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_1, internal_time, external_time, rate_num, rate_den); time_before = gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_2, internal_time, external_time, rate_num, rate_den); /* Maximum discontinuity, when we're synched with the master. Could make this a property, * but this value seems to work fine */ max_discont = self->rtt_avg / 4; /* If the remote observation was within a max_discont window around our min/max estimates, we're synched */ synched = (GST_CLOCK_DIFF (remote_avg, min_guess) < (GstClockTimeDiff) (max_discont) && GST_CLOCK_DIFF (time_before, remote_avg) < (GstClockTimeDiff) (max_discont)); if (gst_clock_add_observation_unapplied (GST_CLOCK_CAST (self), local_avg, remote_avg, &r_squared, &internal_time, &external_time, &rate_num, &rate_den)) { /* Now compare the difference (discont) in the clock * after this observation */ time_discont = GST_CLOCK_DIFF (time_before, gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_2, 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; } /* Check if the new clock params would have made our observation within range */ now_synched = (GST_CLOCK_DIFF (remote_avg, gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_1, internal_time, external_time, rate_num, rate_den)) < (GstClockTimeDiff) (max_discont)) && (GST_CLOCK_DIFF (gst_clock_adjust_with_calibration (GST_CLOCK_CAST (self), local_2, internal_time, external_time, rate_num, rate_den), remote_avg) < (GstClockTimeDiff) (max_discont)); /* Only update the clock if we had synch or just gained it */ if (synched || now_synched || self->skipped_updates > MAX_SKIPPED_UPDATES) { gst_clock_set_calibration (GST_CLOCK_CAST (self), internal_time, external_time, rate_num, rate_den); /* ghetto formula - shorter timeout for bad correlations */ current_timeout = (1e-3 / (1 - MIN (r_squared, 0.99999))) * GST_SECOND; current_timeout = MIN (current_timeout, gst_clock_get_timeout (GST_CLOCK_CAST (self))); self->skipped_updates = 0; /* FIXME: When do we consider the clock absolutely not synced anymore? */ gst_clock_set_synced (GST_CLOCK (self), TRUE); } 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; self->skipped_updates++; } } /* 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); GST_OBJECT_LOCK (self); if (self->busses) { GstStructure *s; GstMessage *msg; GList *l; /* Output a stats message, whether we updated the clock or not */ s = gst_structure_new ("gst-netclock-statistics", "synchronised", G_TYPE_BOOLEAN, synched, "rtt", G_TYPE_UINT64, rtt, "rtt-average", G_TYPE_UINT64, self->rtt_avg, "local", G_TYPE_UINT64, local_avg, "remote", G_TYPE_UINT64, remote_avg, "discontinuity", G_TYPE_INT64, time_discont, "remote-min-estimate", G_TYPE_UINT64, min_guess, "remote-max-estimate", G_TYPE_UINT64, time_before, "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); msg = gst_message_new_element (GST_OBJECT (self), s); for (l = self->busses; l; l = l->next) gst_bus_post (l->data, gst_message_ref (msg)); gst_message_unref (msg); } GST_OBJECT_UNLOCK (self); GST_INFO ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (current_timeout)); self->timeout_expiration = gst_util_get_timestamp () + current_timeout; return; bogus_observation: /* Schedule a new packet again soon */ self->timeout_expiration = gst_util_get_timestamp () + (GST_SECOND / 4); return; }
static gpointer gst_net_client_clock_thread (gpointer data) { GstNetClientClock *self = data; GstNetTimePacket *packet; GSocket *socket = self->priv->socket; GError *err = NULL; GstClock *clock = data; 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)) { 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; } GST_TRACE_OBJECT (self, "timeout: %" G_GINT64_FORMAT "us", socket_timeout); 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"); packet = gst_net_time_packet_new (NULL); packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self)); GST_DEBUG_OBJECT (self, "sending packet, local time = %" GST_TIME_FORMAT, GST_TIME_ARGS (packet->local_time)); gst_net_time_packet_send (packet, self->priv->socket, self->priv->servaddr, NULL); g_free (packet); /* 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 { GstClockTime new_local; /* got packet */ new_local = gst_clock_get_internal_time (GST_CLOCK (self)); 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 */ gst_net_client_clock_observe_times (self, packet->local_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); } } } GST_INFO_OBJECT (self, "shutting down net client clock thread"); return NULL; }
static gpointer gst_net_client_clock_thread (gpointer data) { GstNetClientClock *self = data; struct sockaddr_in tmpaddr; socklen_t len; GstNetTimePacket *packet; gint ret; GstClock *clock = data; while (TRUE) { ret = gst_net_client_clock_do_select (self); if (ret < 0 && errno == EBUSY) { GST_LOG_OBJECT (self, "stop"); goto stopped; } else if (ret == 0) { /* timed out, let's send another packet */ GST_DEBUG_OBJECT (self, "timed out"); packet = gst_net_time_packet_new (NULL); packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self)); GST_DEBUG_OBJECT (self, "sending packet, local time = %" GST_TIME_FORMAT, GST_TIME_ARGS (packet->local_time)); gst_net_time_packet_send (packet, self->priv->sock.fd, (struct sockaddr *) self->servaddr, sizeof (struct sockaddr_in)); g_free (packet); /* reset timeout */ self->current_timeout = clock->timeout; continue; } else if (gst_poll_fd_can_read (self->priv->fdset, &self->priv->sock)) { /* got data in */ GstClockTime new_local = gst_clock_get_internal_time (GST_CLOCK (self)); len = sizeof (struct sockaddr); packet = gst_net_time_packet_receive (self->priv->sock.fd, (struct sockaddr *) &tmpaddr, &len); if (!packet) goto receive_error; 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); continue; } else { GST_WARNING_OBJECT (self, "unhandled select return state?"); continue; } g_assert_not_reached (); stopped: { GST_DEBUG_OBJECT (self, "shutting down"); /* socket gets closed in _stop() */ return NULL; } receive_error: { GST_WARNING_OBJECT (self, "receive error"); continue; } g_assert_not_reached (); } g_assert_not_reached (); return NULL; }
static gpointer gst_net_client_clock_thread (gpointer data) { GstNetClientClock *self = data; GstNetTimePacket *packet; GMainContext *ctx; GSourceFuncs funcs = { NULL, }; GSource *source; GIOCondition cond; gboolean timeout; GSocket *socket = self->priv->socket; GError *err = NULL; GstClock *clock = data; GST_INFO_OBJECT (self, "net client clock thread running, socket=%p", socket); g_socket_set_blocking (socket, TRUE); g_socket_set_timeout (socket, 0); ctx = g_main_context_new (); source = g_socket_create_source (socket, G_IO_IN, self->priv->cancel); g_source_set_name (source, "GStreamer net client clock thread socket"); g_source_set_callback (source, (GSourceFunc) gst_net_client_clock_socket_cb, &cond, NULL); g_source_attach (source, ctx); g_source_unref (source); /* GSocket only support second granularity for timeouts, so roll our own * timeout source (so we don't have to create a new source whenever the * timeout changes, as we would have to do with the default timeout source) */ funcs.prepare = gst_net_client_clock_timeout_source_prepare; funcs.check = gst_net_client_clock_timeout_source_check; funcs.dispatch = gst_net_client_clock_timeout_source_dispatch; funcs.finalize = NULL; source = g_source_new (&funcs, sizeof (GstNetClientClockTimeoutSource)); ((GstNetClientClockTimeoutSource *) source)->clock = self; ((GstNetClientClockTimeoutSource *) source)->p_timeout = &timeout; g_source_set_name (source, "GStreamer net client clock timeout"); g_source_attach (source, ctx); g_source_unref (source); while (!g_cancellable_is_cancelled (self->priv->cancel)) { cond = 0; timeout = FALSE; g_main_context_iteration (ctx, TRUE); if (g_cancellable_is_cancelled (self->priv->cancel)) break; if (timeout) { /* timed out, let's send another packet */ GST_DEBUG_OBJECT (self, "timed out"); packet = gst_net_time_packet_new (NULL); packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self)); GST_DEBUG_OBJECT (self, "sending packet, local time = %" GST_TIME_FORMAT, GST_TIME_ARGS (packet->local_time)); gst_net_time_packet_send (packet, self->priv->socket, self->priv->servaddr, NULL); g_free (packet); /* reset timeout (but are expecting a response sooner anyway) */ self->priv->timeout_expiration = gst_util_get_timestamp () + gst_clock_get_timeout (clock); continue; } /* got data to read? */ if ((cond & G_IO_IN)) { GstClockTime new_local; new_local = gst_clock_get_internal_time (GST_CLOCK (self)); packet = gst_net_time_packet_receive (socket, NULL, &err); if (err != NULL) { GST_WARNING_OBJECT (self, "receive error: %s", err->message); g_error_free (err); err = NULL; continue; } 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); continue; } if ((cond & (G_IO_ERR | G_IO_HUP))) { GST_DEBUG_OBJECT (self, "socket error?! %s", g_strerror (errno)); g_usleep (G_USEC_PER_SEC / 10); continue; } } GST_INFO_OBJECT (self, "shutting down net client clock thread"); g_main_context_unref (ctx); return NULL; }
static void gst_net_client_clock_constructed (GObject * object) { GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object); GstClock *internal_clock; GstClockTime internal; 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; if (cache->remove_id) { gst_clock_id_unschedule (cache->remove_id); cache->remove_id = NULL; } 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); } 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", G_CALLBACK (gst_net_client_clock_synced_cb), self); GST_OBJECT_UNLOCK (cache->clock); G_UNLOCK (clocks_lock); self->priv->internal_clock = internal_clock = cache->clock; /* 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 (internal_clock); gst_clock_set_calibration (internal_clock, internal, self->priv->base_time, 1, 1); { GstClockTime now = gst_clock_get_time (internal_clock); if (GST_CLOCK_DIFF (now, self->priv->base_time) > 0 || GST_CLOCK_DIFF (now, self->priv->base_time + GST_SECOND) < 0) { g_warning ("unable to set the base time, expect sync problems!"); } } /* all systems go, cap'n */ }