| 1 |
/* GStreamer |
| 2 |
* Copyright (C) 2015 Sebastian Dröge <[email protected]> |
| 3 |
* |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
/** |
| 21 |
* SECTION:gstptpclock |
| 22 |
* @title: GstPtpClock |
| 23 |
* @short_description: Special clock that synchronizes to a remote time |
| 24 |
* provider via PTP (IEEE1588:2008). |
| 25 |
* @see_also: #GstClock, #GstNetClientClock, #GstPipeline |
| 26 |
* |
| 27 |
* GstPtpClock implements a PTP (IEEE1588:2008) ordinary clock in slave-only |
| 28 |
* mode, that allows a GStreamer pipeline to synchronize to a PTP network |
| 29 |
* clock in some specific domain. |
| 30 |
* |
| 31 |
* The PTP subsystem can be initialized with gst_ptp_init(), which then starts |
| 32 |
* a helper process to do the actual communication via the PTP ports. This is |
| 33 |
* required as PTP listens on ports < 1024 and thus requires special |
| 34 |
* privileges. Once this helper process is started, the main process will |
| 35 |
* synchronize to all PTP domains that are detected on the selected |
| 36 |
* interfaces. |
| 37 |
* |
| 38 |
* gst_ptp_clock_new() then allows to create a GstClock that provides the PTP |
| 39 |
* time from a master clock inside a specific PTP domain. This clock will only |
| 40 |
* return valid timestamps once the timestamps in the PTP domain are known. To |
| 41 |
* check this, you can use gst_clock_wait_for_sync(), the GstClock::synced |
| 42 |
* signal and gst_clock_is_synced(). |
| 43 |
* |
| 44 |
* To gather statistics about the PTP clock synchronization, |
| 45 |
* gst_ptp_statistics_callback_add() can be used. This gives the application |
| 46 |
* the possibility to collect all kinds of statistics from the clock |
| 47 |
* synchronization. |
| 48 |
* |
| 49 |
* Since: 1.6 |
| 50 |
* |
| 51 |
*/ |
| 52 |
#define _GNU_SOURCE 1 |
| 53 |
#ifdef HAVE_CONFIG_H |
| 54 |
#include "config.h" |
| 55 |
#endif |
| 56 |
|
| 57 |
#include "gstptpclock.h" |
| 58 |
|
| 59 |
#include <gio/gio.h> |
| 60 |
|
| 61 |
#include <gst/base/base.h> |
| 62 |
|
| 63 |
#include "gst/glib-compat-private.h" |
| 64 |
|
| 65 |
#ifdef G_OS_WIN32 |
| 66 |
#define WIN32_LEAN_AND_MEAN |
| 67 |
#include <windows.h> |
| 68 |
#include <processthreadsapi.h> /* GetCurrentProcessId */ |
| 69 |
#endif |
| 70 |
|
| 71 |
#ifdef HAVE_UNISTD_H |
| 72 |
#include <unistd.h> |
| 73 |
#endif |
| 74 |
|
| 75 |
#ifdef G_OS_WIN32 |
| 76 |
#include <windows.h> |
| 77 |
static HMODULE gstnet_dll_handle = NULL; |
| 78 |
#endif |
| 79 |
|
| 80 |
#ifdef HAVE_DLFCN_H |
| 81 |
#include <dlfcn.h> |
| 82 |
#endif |
| 83 |
|
| 84 |
GST_DEBUG_CATEGORY_STATIC (ptp_debug); |
| 85 |
#define GST_CAT_DEFAULT (ptp_debug) |
| 86 |
|
| 87 |
/* IEEE 1588 7.7.3.1 */ |
| 88 |
#define PTP_ANNOUNCE_RECEIPT_TIMEOUT 4 |
| 89 |
|
| 90 |
/* Use a running average for calculating the mean path delay instead |
| 91 |
* of just using the last measurement. Enabling this helps in unreliable |
| 92 |
* networks, like wifi, with often changing delays |
| 93 |
* |
| 94 |
* Undef for following IEEE1588-2008 by the letter |
| 95 |
*/ |
| 96 |
#define USE_RUNNING_AVERAGE_DELAY 1 |
| 97 |
|
| 98 |
/* Filter out any measurements that are above a certain threshold compared to |
| 99 |
* previous measurements. Enabling this helps filtering out outliers that |
| 100 |
* happen fairly often in unreliable networks, like wifi. |
| 101 |
* |
| 102 |
* Undef for following IEEE1588-2008 by the letter |
| 103 |
*/ |
| 104 |
#define USE_MEASUREMENT_FILTERING 1 |
| 105 |
|
| 106 |
/* Select the first clock from which we capture a SYNC message as the master |
| 107 |
* clock of the domain until we are ready to run the best master clock |
| 108 |
* algorithm. This allows faster syncing but might mean a change of the master |
| 109 |
* clock in the beginning. As all clocks in a domain are supposed to use the |
| 110 |
* same time, this shouldn't be much of a problem. |
| 111 |
* |
| 112 |
* Undef for following IEEE1588-2008 by the letter |
| 113 |
*/ |
| 114 |
#define USE_OPPORTUNISTIC_CLOCK_SELECTION 1 |
| 115 |
|
| 116 |
/* Only consider SYNC messages for which we are allowed to send a DELAY_REQ |
| 117 |
* afterwards. This allows better synchronization in networks with varying |
| 118 |
* delays, as for every other SYNC message we would have to assume that it's |
| 119 |
* the average of what we saw before. But that might be completely off |
| 120 |
*/ |
| 121 |
#define USE_ONLY_SYNC_WITH_DELAY 1 |
| 122 |
|
| 123 |
/* Filter out delay measurements that are too far away from the median of the |
| 124 |
* last delay measurements, currently those that are more than 2 times as big. |
| 125 |
* This increases accuracy a lot on wifi. |
| 126 |
*/ |
| 127 |
#define USE_MEDIAN_PRE_FILTERING 1 |
| 128 |
#define MEDIAN_PRE_FILTERING_WINDOW 9 |
| 129 |
|
| 130 |
/* How many updates should be skipped at maximum when using USE_MEASUREMENT_FILTERING */ |
| 131 |
#define MAX_SKIPPED_UPDATES 5 |
| 132 |
|
| 133 |
typedef enum |
| 134 |
{ |
| 135 |
PTP_MESSAGE_TYPE_SYNC = 0x0, |
| 136 |
PTP_MESSAGE_TYPE_DELAY_REQ = 0x1, |
| 137 |
PTP_MESSAGE_TYPE_PDELAY_REQ = 0x2, |
| 138 |
PTP_MESSAGE_TYPE_PDELAY_RESP = 0x3, |
| 139 |
PTP_MESSAGE_TYPE_FOLLOW_UP = 0x8, |
| 140 |
PTP_MESSAGE_TYPE_DELAY_RESP = 0x9, |
| 141 |
PTP_MESSAGE_TYPE_PDELAY_RESP_FOLLOW_UP = 0xA, |
| 142 |
PTP_MESSAGE_TYPE_ANNOUNCE = 0xB, |
| 143 |
PTP_MESSAGE_TYPE_SIGNALING = 0xC, |
| 144 |
PTP_MESSAGE_TYPE_MANAGEMENT = 0xD |
| 145 |
} PtpMessageType; |
| 146 |
|
| 147 |
typedef struct |
| 148 |
{ |
| 149 |
guint64 seconds_field; /* 48 bits valid */ |
| 150 |
guint32 nanoseconds_field; |
| 151 |
} PtpTimestamp; |
| 152 |
|
| 153 |
#define PTP_TIMESTAMP_TO_GST_CLOCK_TIME(ptp) (ptp.seconds_field * GST_SECOND + ptp.nanoseconds_field) |
| 154 |
#define GST_CLOCK_TIME_TO_PTP_TIMESTAMP_SECONDS(gst) (((GstClockTime) gst) / GST_SECOND) |
| 155 |
#define GST_CLOCK_TIME_TO_PTP_TIMESTAMP_NANOSECONDS(gst) (((GstClockTime) gst) % GST_SECOND) |
| 156 |
|
| 157 |
typedef struct |
| 158 |
{ |
| 159 |
guint64 clock_identity; |
| 160 |
guint16 port_number; |
| 161 |
} PtpClockIdentity; |
| 162 |
|
| 163 |
static gint |
| 164 |
compare_clock_identity (const PtpClockIdentity * a, const PtpClockIdentity * b) |
| 165 |
{ |
| 166 |
if (a->clock_identity < b->clock_identity) |
| 167 |
return -1; |
| 168 |
else if (a->clock_identity > b->clock_identity) |
| 169 |
return 1; |
| 170 |
|
| 171 |
if (a->port_number < b->port_number) |
| 172 |
return -1; |
| 173 |
else if (a->port_number > b->port_number) |
| 174 |
return 1; |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
typedef struct |
| 180 |
{ |
| 181 |
guint8 clock_class; |
| 182 |
guint8 clock_accuracy; |
| 183 |
guint16 offset_scaled_log_variance; |
| 184 |
} PtpClockQuality; |
| 185 |
|
| 186 |
typedef struct |
| 187 |
{ |
| 188 |
guint8 transport_specific; |
| 189 |
PtpMessageType message_type; |
| 190 |
/* guint8 reserved; */ |
| 191 |
guint8 version_ptp; |
| 192 |
guint16 message_length; |
| 193 |
guint8 domain_number; |
| 194 |
/* guint8 reserved; */ |
| 195 |
guint16 flag_field; |
| 196 |
gint64 correction_field; /* 48.16 fixed point nanoseconds */ |
| 197 |
/* guint32 reserved; */ |
| 198 |
PtpClockIdentity source_port_identity; |
| 199 |
guint16 sequence_id; |
| 200 |
guint8 control_field; |
| 201 |
gint8 log_message_interval; |
| 202 |
|
| 203 |
union |
| 204 |
{ |
| 205 |
struct |
| 206 |
{ |
| 207 |
PtpTimestamp origin_timestamp; |
| 208 |
gint16 current_utc_offset; |
| 209 |
/* guint8 reserved; */ |
| 210 |
guint8 grandmaster_priority_1; |
| 211 |
PtpClockQuality grandmaster_clock_quality; |
| 212 |
guint8 grandmaster_priority_2; |
| 213 |
guint64 grandmaster_identity; |
| 214 |
guint16 steps_removed; |
| 215 |
guint8 time_source; |
| 216 |
} announce; |
| 217 |
|
| 218 |
struct |
| 219 |
{ |
| 220 |
PtpTimestamp origin_timestamp; |
| 221 |
} sync; |
| 222 |
|
| 223 |
struct |
| 224 |
{ |
| 225 |
PtpTimestamp precise_origin_timestamp; |
| 226 |
} follow_up; |
| 227 |
|
| 228 |
struct |
| 229 |
{ |
| 230 |
PtpTimestamp origin_timestamp; |
| 231 |
} delay_req; |
| 232 |
|
| 233 |
struct |
| 234 |
{ |
| 235 |
PtpTimestamp receive_timestamp; |
| 236 |
PtpClockIdentity requesting_port_identity; |
| 237 |
} delay_resp; |
| 238 |
|
| 239 |
} message_specific; |
| 240 |
} PtpMessage; |
| 241 |
|
| 242 |
typedef enum |
| 243 |
{ |
| 244 |
TYPE_EVENT = 0, /* 8-bit interface index, 64-bit monotonic clock time and PTP message is payload */ |
| 245 |
TYPE_GENERAL = 1, /* 8-bit interface index, 64-bit monotonic clock time and PTP message is payload */ |
| 246 |
TYPE_CLOCK_ID = 2, /* 64-bit clock ID is payload */ |
| 247 |
TYPE_SEND_TIME_ACK = 3, /* 64-bit monotonic clock time, 8-bit message type, 8-bit domain number and 16-bit sequence number is payload */ |
| 248 |
} StdIOMessageType; |
| 249 |
|
| 250 |
/* 2 byte BE payload size plus 1 byte message type */ |
| 251 |
#define STDIO_MESSAGE_HEADER_SIZE (3) |
| 252 |
|
| 253 |
/* 2 byte BE payload size. Payload format: |
| 254 |
* - 1 byte GstDebugLevel |
| 255 |
* - 2 byte BE filename length |
| 256 |
* - filename UTF-8 string |
| 257 |
* - 2 byte BE module path length |
| 258 |
* - module path UTF-8 string |
| 259 |
* - 4 byte BE line number |
| 260 |
* - remainder is UTF-8 string |
| 261 |
*/ |
| 262 |
#define STDERR_MESSAGE_HEADER_SIZE (2) |
| 263 |
|
| 264 |
static GMutex ptp_lock; |
| 265 |
static GCond ptp_cond; |
| 266 |
static gboolean initted = FALSE; |
| 267 |
#ifdef HAVE_PTP |
| 268 |
static gboolean supported = TRUE; |
| 269 |
#else |
| 270 |
static gboolean supported = FALSE; |
| 271 |
#endif |
| 272 |
static GSubprocess *ptp_helper_process; |
| 273 |
static GInputStream *stdout_pipe; |
| 274 |
static GInputStream *stderr_pipe; |
| 275 |
static GOutputStream *stdin_pipe; |
| 276 |
static guint8 stdio_header[STDIO_MESSAGE_HEADER_SIZE]; /* buffer for reading the message header */ |
| 277 |
static guint8 stdout_buffer[8192]; /* buffer for reading the message payload */ |
| 278 |
static guint8 stderr_header[STDERR_MESSAGE_HEADER_SIZE]; /* buffer for reading the message header */ |
| 279 |
static guint8 stderr_buffer[8192]; /* buffer for reading the message payload */ |
| 280 |
static GThread *ptp_helper_thread; |
| 281 |
static GMainContext *main_context; |
| 282 |
static GMainLoop *main_loop; |
| 283 |
static GRand *delay_req_rand; |
| 284 |
static GstClock *observation_system_clock; |
| 285 |
static PtpClockIdentity ptp_clock_id = { GST_PTP_CLOCK_ID_NONE, 0 }; |
| 286 |
|
| 287 |
#define CUR_STDIO_HEADER_SIZE (GST_READ_UINT16_BE (stdio_header)) |
| 288 |
#define CUR_STDIO_HEADER_TYPE ((StdIOMessageType) stdio_header[2]) |
| 289 |
|
| 290 |
#define CUR_STDERR_HEADER_SIZE (GST_READ_UINT16_BE (stderr_header)) |
| 291 |
|
| 292 |
typedef struct |
| 293 |
{ |
| 294 |
PtpClockIdentity master_clock_identity; |
| 295 |
guint8 iface_idx; |
| 296 |
|
| 297 |
GstClockTime announce_interval; /* last interval we received */ |
| 298 |
GQueue announce_messages; |
| 299 |
guint64 timed_out_sync; /* how often did this sender continuously time out a FOLLOW_UP */ |
| 300 |
guint64 timed_out_delay_resp; /* how often did this sender continuously time out a DELAY_RESP */ |
| 301 |
} PtpAnnounceSender; |
| 302 |
|
| 303 |
typedef struct |
| 304 |
{ |
| 305 |
PtpAnnounceSender *sender; |
| 306 |
|
| 307 |
GstClockTime receive_time; |
| 308 |
|
| 309 |
PtpClockIdentity master_clock_identity; |
| 310 |
guint8 iface_idx; |
| 311 |
|
| 312 |
guint8 grandmaster_priority_1; |
| 313 |
PtpClockQuality grandmaster_clock_quality; |
| 314 |
guint8 grandmaster_priority_2; |
| 315 |
guint64 grandmaster_identity; |
| 316 |
guint16 steps_removed; |
| 317 |
guint8 time_source; |
| 318 |
|
| 319 |
guint16 sequence_id; |
| 320 |
} PtpAnnounceMessage; |
| 321 |
|
| 322 |
typedef struct |
| 323 |
{ |
| 324 |
guint domain; |
| 325 |
|
| 326 |
guint32 sync_seqnum; |
| 327 |
GstClockTime sync_recv_time_local; /* t2 */ |
| 328 |
GstClockTime sync_send_time_remote; /* t1, might be -1 if FOLLOW_UP pending */ |
| 329 |
GstClockTime follow_up_recv_time_local; |
| 330 |
|
| 331 |
GSource *timeout_source; |
| 332 |
guint8 iface_idx; |
| 333 |
guint32 delay_req_seqnum; |
| 334 |
GstClockTime delay_req_send_time_local; /* t3, -1 if we wait for FOLLOW_UP */ |
| 335 |
GstClockTime delay_req_recv_time_remote; /* t4, -1 if we wait */ |
| 336 |
GstClockTime delay_resp_recv_time_local; |
| 337 |
|
| 338 |
gint64 correction_field_sync; /* sum of the correction fields of SYNC/FOLLOW_UP */ |
| 339 |
gint64 correction_field_delay; /* sum of the correction fields of DELAY_RESP */ |
| 340 |
} PtpPendingSync; |
| 341 |
|
| 342 |
static void |
| 343 |
ptp_pending_sync_free (PtpPendingSync * sync) |
| 344 |
{ |
| 345 |
if (sync->timeout_source) { |
| 346 |
g_source_destroy (sync->timeout_source); |
| 347 |
g_source_unref (sync->timeout_source); |
| 348 |
} |
| 349 |
g_free (sync); |
| 350 |
} |
| 351 |
|
| 352 |
typedef struct |
| 353 |
{ |
| 354 |
guint domain; |
| 355 |
|
| 356 |
GstClockTime last_ptp_time; |
| 357 |
GstClockTime last_local_time; |
| 358 |
gint skipped_updates; |
| 359 |
|
| 360 |
/* Used for selecting the master/grandmaster */ |
| 361 |
GList *announce_senders; |
| 362 |
|
| 363 |
/* Last selected master clock */ |
| 364 |
gboolean have_master_clock; |
| 365 |
PtpClockIdentity master_clock_identity; |
| 366 |
guint8 iface_idx; |
| 367 |
guint64 grandmaster_identity; |
| 368 |
|
| 369 |
/* Last SYNC or FOLLOW_UP timestamp we received */ |
| 370 |
GstClockTime last_ptp_sync_time; |
| 371 |
GstClockTime last_ptp_sync_time_local; |
| 372 |
GstClockTime sync_interval; |
| 373 |
|
| 374 |
GstClockTime mean_path_delay; |
| 375 |
GstClockTime last_delay_req, min_delay_req_interval; |
| 376 |
guint16 last_delay_req_seqnum; |
| 377 |
GstClockTime last_ptp_delay_resp_time_local; |
| 378 |
|
| 379 |
GstClockTime last_path_delays[MEDIAN_PRE_FILTERING_WINDOW]; |
| 380 |
gint last_path_delays_missing; |
| 381 |
|
| 382 |
GQueue pending_syncs; |
| 383 |
|
| 384 |
GstClock *domain_clock; |
| 385 |
} PtpDomainData; |
| 386 |
|
| 387 |
// The lists domain_clocks and domain_data are same but the former is protected |
| 388 |
// by the domain_clocks_lock. |
| 389 |
// It is needed because sometimes other threads than the PTP thread will need |
| 390 |
// to access the list, and without a mutex it might happen that the original |
| 391 |
// list (domain_data) is modified at the same time (prepending a new domain). |
| 392 |
static GList *domain_data; |
| 393 |
static GMutex domain_clocks_lock; |
| 394 |
static GList *domain_clocks; |
| 395 |
|
| 396 |
/* Protected by PTP lock */ |
| 397 |
static void emit_ptp_statistics (guint8 domain, const GstStructure * stats); |
| 398 |
static GHookList domain_stats_hooks; |
| 399 |
static gint domain_stats_n_hooks; |
| 400 |
static gboolean domain_stats_hooks_initted = FALSE; |
| 401 |
|
| 402 |
/* Only ever accessed from the PTP thread */ |
| 403 |
/* PTPD in hybrid mode (default) sends multicast PTP messages with an invalid |
| 404 |
* logMessageInterval. We work around this here and warn once */ |
| 405 |
static gboolean ptpd_hybrid_workaround_warned_once = FALSE; |
| 406 |
|
| 407 |
/* Converts log2 seconds to GstClockTime */ |
| 408 |
static GstClockTime |
| 409 |
log2_to_clock_time (gint l) |
| 410 |
{ |
| 411 |
if (l < 0) |
| 412 |
return GST_SECOND >> (-l); |
| 413 |
else |
| 414 |
return GST_SECOND << l; |
| 415 |
} |
| 416 |
|
| 417 |
static void |
| 418 |
dump_ptp_message (PtpMessage * msg) |
| 419 |
{ |
| 420 |
GST_TRACE ("PTP message:"); |
| 421 |
GST_TRACE ("\ttransport_specific: %u", msg->transport_specific); |
| 422 |
GST_TRACE ("\tmessage_type: 0x%01x", msg->message_type); |
| 423 |
GST_TRACE ("\tversion_ptp: %u", msg->version_ptp); |
| 424 |
GST_TRACE ("\tmessage_length: %u", msg->message_length); |
| 425 |
GST_TRACE ("\tdomain_number: %u", msg->domain_number); |
| 426 |
GST_TRACE ("\tflag_field: 0x%04x", msg->flag_field); |
| 427 |
GST_TRACE ("\tcorrection_field: %" G_GINT64_FORMAT ".%03u", |
| 428 |
(msg->correction_field / 65536), |
| 429 |
(guint) ((msg->correction_field & 0xffff) * 1000) / 65536); |
| 430 |
GST_TRACE ("\tsource_port_identity: 0x%016" G_GINT64_MODIFIER "x %u", |
| 431 |
msg->source_port_identity.clock_identity, |
| 432 |
msg->source_port_identity.port_number); |
| 433 |
GST_TRACE ("\tsequence_id: %u", msg->sequence_id); |
| 434 |
GST_TRACE ("\tcontrol_field: 0x%02x", msg->control_field); |
| 435 |
GST_TRACE ("\tmessage_interval: %" GST_TIME_FORMAT, |
| 436 |
GST_TIME_ARGS (log2_to_clock_time (msg->log_message_interval))); |
| 437 |
|
| 438 |
switch (msg->message_type) { |
| 439 |
case PTP_MESSAGE_TYPE_ANNOUNCE: |
| 440 |
GST_TRACE ("\tANNOUNCE:"); |
| 441 |
GST_TRACE ("\t\torigin_timestamp: %" G_GUINT64_FORMAT ".%09u", |
| 442 |
msg->message_specific.announce.origin_timestamp.seconds_field, |
| 443 |
msg->message_specific.announce.origin_timestamp.nanoseconds_field); |
| 444 |
GST_TRACE ("\t\tcurrent_utc_offset: %d", |
| 445 |
msg->message_specific.announce.current_utc_offset); |
| 446 |
GST_TRACE ("\t\tgrandmaster_priority_1: %u", |
| 447 |
msg->message_specific.announce.grandmaster_priority_1); |
| 448 |
GST_TRACE ("\t\tgrandmaster_clock_quality: 0x%02x 0x%02x %u", |
| 449 |
msg->message_specific.announce.grandmaster_clock_quality.clock_class, |
| 450 |
msg->message_specific.announce. |
| 451 |
grandmaster_clock_quality.clock_accuracy, |
| 452 |
msg->message_specific.announce. |
| 453 |
grandmaster_clock_quality.offset_scaled_log_variance); |
| 454 |
GST_TRACE ("\t\tgrandmaster_priority_2: %u", |
| 455 |
msg->message_specific.announce.grandmaster_priority_2); |
| 456 |
GST_TRACE ("\t\tgrandmaster_identity: 0x%016" G_GINT64_MODIFIER "x", |
| 457 |
msg->message_specific.announce.grandmaster_identity); |
| 458 |
GST_TRACE ("\t\tsteps_removed: %u", |
| 459 |
msg->message_specific.announce.steps_removed); |
| 460 |
GST_TRACE ("\t\ttime_source: 0x%02x", |
| 461 |
msg->message_specific.announce.time_source); |
| 462 |
break; |
| 463 |
case PTP_MESSAGE_TYPE_SYNC: |
| 464 |
GST_TRACE ("\tSYNC:"); |
| 465 |
GST_TRACE ("\t\torigin_timestamp: %" G_GUINT64_FORMAT ".%09u", |
| 466 |
msg->message_specific.sync.origin_timestamp.seconds_field, |
| 467 |
msg->message_specific.sync.origin_timestamp.nanoseconds_field); |
| 468 |
break; |
| 469 |
case PTP_MESSAGE_TYPE_FOLLOW_UP: |
| 470 |
GST_TRACE ("\tFOLLOW_UP:"); |
| 471 |
GST_TRACE ("\t\tprecise_origin_timestamp: %" G_GUINT64_FORMAT ".%09u", |
| 472 |
msg->message_specific.follow_up. |
| 473 |
precise_origin_timestamp.seconds_field, |
| 474 |
msg->message_specific.follow_up. |
| 475 |
precise_origin_timestamp.nanoseconds_field); |
| 476 |
break; |
| 477 |
case PTP_MESSAGE_TYPE_DELAY_REQ: |
| 478 |
GST_TRACE ("\tDELAY_REQ:"); |
| 479 |
GST_TRACE ("\t\torigin_timestamp: %" G_GUINT64_FORMAT ".%09u", |
| 480 |
msg->message_specific.delay_req.origin_timestamp.seconds_field, |
| 481 |
msg->message_specific.delay_req.origin_timestamp.nanoseconds_field); |
| 482 |
break; |
| 483 |
case PTP_MESSAGE_TYPE_DELAY_RESP: |
| 484 |
GST_TRACE ("\tDELAY_RESP:"); |
| 485 |
GST_TRACE ("\t\treceive_timestamp: %" G_GUINT64_FORMAT ".%09u", |
| 486 |
msg->message_specific.delay_resp.receive_timestamp.seconds_field, |
| 487 |
msg->message_specific.delay_resp.receive_timestamp.nanoseconds_field); |
| 488 |
GST_TRACE ("\t\trequesting_port_identity: 0x%016" G_GINT64_MODIFIER |
| 489 |
"x %u", |
| 490 |
msg->message_specific.delay_resp. |
| 491 |
requesting_port_identity.clock_identity, |
| 492 |
msg->message_specific.delay_resp. |
| 493 |
requesting_port_identity.port_number); |
| 494 |
break; |
| 495 |
default: |
| 496 |
break; |
| 497 |
} |
| 498 |
GST_TRACE (" "); |
| 499 |
} |
| 500 |
|
| 501 |
/* IEEE 1588-2008 5.3.3 */ |
| 502 |
static gboolean |
| 503 |
parse_ptp_timestamp (PtpTimestamp * timestamp, GstByteReader * reader) |
| 504 |
{ |
| 505 |
g_return_val_if_fail (gst_byte_reader_get_remaining (reader) >= 10, FALSE); |
| 506 |
|
| 507 |
timestamp->seconds_field = |
| 508 |
(((guint64) gst_byte_reader_get_uint32_be_unchecked (reader)) << 16) | |
| 509 |
gst_byte_reader_get_uint16_be_unchecked (reader); |
| 510 |
timestamp->nanoseconds_field = |
| 511 |
gst_byte_reader_get_uint32_be_unchecked (reader); |
| 512 |
|
| 513 |
if (timestamp->nanoseconds_field >= 1000000000) |
| 514 |
return FALSE; |
| 515 |
|
| 516 |
return TRUE; |
| 517 |
} |
| 518 |
|
| 519 |
/* IEEE 1588-2008 13.3 */ |
| 520 |
static gboolean |
| 521 |
parse_ptp_message_header (PtpMessage * msg, GstByteReader * reader) |
| 522 |
{ |
| 523 |
guint8 b; |
| 524 |
|
| 525 |
g_return_val_if_fail (gst_byte_reader_get_remaining (reader) >= 34, FALSE); |
| 526 |
|
| 527 |
b = gst_byte_reader_get_uint8_unchecked (reader); |
| 528 |
msg->transport_specific = b >> 4; |
| 529 |
msg->message_type = b & 0x0f; |
| 530 |
|
| 531 |
b = gst_byte_reader_get_uint8_unchecked (reader); |
| 532 |
msg->version_ptp = b & 0x0f; |
| 533 |
if (msg->version_ptp != 2) { |
| 534 |
GST_WARNING ("Unsupported PTP message version (%u != 2)", msg->version_ptp); |
| 535 |
return FALSE; |
| 536 |
} |
| 537 |
|
| 538 |
msg->message_length = gst_byte_reader_get_uint16_be_unchecked (reader); |
| 539 |
if (gst_byte_reader_get_remaining (reader) + 4 < msg->message_length) { |
| 540 |
GST_WARNING ("Not enough data (%u < %u)", |
| 541 |
gst_byte_reader_get_remaining (reader) + 4, msg->message_length); |
| 542 |
return FALSE; |
| 543 |
} |
| 544 |
|
| 545 |
msg->domain_number = gst_byte_reader_get_uint8_unchecked (reader); |
| 546 |
gst_byte_reader_skip_unchecked (reader, 1); |
| 547 |
|
| 548 |
msg->flag_field = gst_byte_reader_get_uint16_be_unchecked (reader); |
| 549 |
msg->correction_field = gst_byte_reader_get_uint64_be_unchecked (reader); |
| 550 |
gst_byte_reader_skip_unchecked (reader, 4); |
| 551 |
|
| 552 |
msg->source_port_identity.clock_identity = |
| 553 |
gst_byte_reader_get_uint64_be_unchecked (reader); |
| 554 |
msg->source_port_identity.port_number = |
| 555 |
gst_byte_reader_get_uint16_be_unchecked (reader); |
| 556 |
|
| 557 |
msg->sequence_id = gst_byte_reader_get_uint16_be_unchecked (reader); |
| 558 |
msg->control_field = gst_byte_reader_get_uint8_unchecked (reader); |
| 559 |
msg->log_message_interval = gst_byte_reader_get_uint8_unchecked (reader); |
| 560 |
|
| 561 |
return TRUE; |
| 562 |
} |
| 563 |
|
| 564 |
/* IEEE 1588-2008 13.5 */ |
| 565 |
static gboolean |
| 566 |
parse_ptp_message_announce (PtpMessage * msg, GstByteReader * reader) |
| 567 |
{ |
| 568 |
g_return_val_if_fail (msg->message_type == PTP_MESSAGE_TYPE_ANNOUNCE, FALSE); |
| 569 |
|
| 570 |
if (gst_byte_reader_get_remaining (reader) < 30) |
| 571 |
return FALSE; |
| 572 |
|
| 573 |
if (!parse_ptp_timestamp (&msg->message_specific.announce.origin_timestamp, |
| 574 |
reader)) |
| 575 |
return FALSE; |
| 576 |
|
| 577 |
msg->message_specific.announce.current_utc_offset = |
| 578 |
gst_byte_reader_get_uint16_be_unchecked (reader); |
| 579 |
gst_byte_reader_skip_unchecked (reader, 1); |
| 580 |
|
| 581 |
msg->message_specific.announce.grandmaster_priority_1 = |
| 582 |
gst_byte_reader_get_uint8_unchecked (reader); |
| 583 |
msg->message_specific.announce.grandmaster_clock_quality.clock_class = |
| 584 |
gst_byte_reader_get_uint8_unchecked (reader); |
| 585 |
msg->message_specific.announce.grandmaster_clock_quality.clock_accuracy = |
| 586 |
gst_byte_reader_get_uint8_unchecked (reader); |
| 587 |
msg->message_specific.announce. |
| 588 |
grandmaster_clock_quality.offset_scaled_log_variance = |
| 589 |
gst_byte_reader_get_uint16_be_unchecked (reader); |
| 590 |
msg->message_specific.announce.grandmaster_priority_2 = |
| 591 |
gst_byte_reader_get_uint8_unchecked (reader); |
| 592 |
msg->message_specific.announce.grandmaster_identity = |
| 593 |
gst_byte_reader_get_uint64_be_unchecked (reader); |
| 594 |
msg->message_specific.announce.steps_removed = |
| 595 |
gst_byte_reader_get_uint16_be_unchecked (reader); |
| 596 |
msg->message_specific.announce.time_source = |
| 597 |
gst_byte_reader_get_uint8_unchecked (reader); |
| 598 |
|
| 599 |
return TRUE; |
| 600 |
} |
| 601 |
|
| 602 |
/* IEEE 1588-2008 13.6 */ |
| 603 |
static gboolean |
| 604 |
parse_ptp_message_sync (PtpMessage * msg, GstByteReader * reader) |
| 605 |
{ |
| 606 |
g_return_val_if_fail (msg->message_type == PTP_MESSAGE_TYPE_SYNC, FALSE); |
| 607 |
|
| 608 |
if (gst_byte_reader_get_remaining (reader) < 10) |
| 609 |
return FALSE; |
| 610 |
|
| 611 |
if (!parse_ptp_timestamp (&msg->message_specific.sync.origin_timestamp, |
| 612 |
reader)) |
| 613 |
return FALSE; |
| 614 |
|
| 615 |
return TRUE; |
| 616 |
} |
| 617 |
|
| 618 |
/* IEEE 1588-2008 13.6 */ |
| 619 |
static gboolean |
| 620 |
parse_ptp_message_delay_req (PtpMessage * msg, GstByteReader * reader) |
| 621 |
{ |
| 622 |
g_return_val_if_fail (msg->message_type == PTP_MESSAGE_TYPE_DELAY_REQ, FALSE); |
| 623 |
|
| 624 |
if (gst_byte_reader_get_remaining (reader) < 10) |
| 625 |
return FALSE; |
| 626 |
|
| 627 |
if (!parse_ptp_timestamp (&msg->message_specific.delay_req.origin_timestamp, |
| 628 |
reader)) |
| 629 |
return FALSE; |
| 630 |
|
| 631 |
return TRUE; |
| 632 |
} |
| 633 |
|
| 634 |
/* IEEE 1588-2008 13.7 */ |
| 635 |
static gboolean |
| 636 |
parse_ptp_message_follow_up (PtpMessage * msg, GstByteReader * reader) |
| 637 |
{ |
| 638 |
g_return_val_if_fail (msg->message_type == PTP_MESSAGE_TYPE_FOLLOW_UP, FALSE); |
| 639 |
|
| 640 |
if (gst_byte_reader_get_remaining (reader) < 10) |
| 641 |
return FALSE; |
| 642 |
|
| 643 |
if (!parse_ptp_timestamp (&msg->message_specific. |
| 644 |
follow_up.precise_origin_timestamp, reader)) |
| 645 |
return FALSE; |
| 646 |
|
| 647 |
return TRUE; |
| 648 |
} |
| 649 |
|
| 650 |
/* IEEE 1588-2008 13.8 */ |
| 651 |
static gboolean |
| 652 |
parse_ptp_message_delay_resp (PtpMessage * msg, GstByteReader * reader) |
| 653 |
{ |
| 654 |
g_return_val_if_fail (msg->message_type == PTP_MESSAGE_TYPE_DELAY_RESP, |
| 655 |
FALSE); |
| 656 |
|
| 657 |
if (gst_byte_reader_get_remaining (reader) < 20) |
| 658 |
return FALSE; |
| 659 |
|
| 660 |
if (!parse_ptp_timestamp (&msg->message_specific.delay_resp.receive_timestamp, |
| 661 |
reader)) |
| 662 |
return FALSE; |
| 663 |
|
| 664 |
msg->message_specific.delay_resp.requesting_port_identity.clock_identity = |
| 665 |
gst_byte_reader_get_uint64_be_unchecked (reader); |
| 666 |
msg->message_specific.delay_resp.requesting_port_identity.port_number = |
| 667 |
gst_byte_reader_get_uint16_be_unchecked (reader); |
| 668 |
|
| 669 |
return TRUE; |
| 670 |
} |
| 671 |
|
| 672 |
static gboolean |
| 673 |
parse_ptp_message (PtpMessage * msg, const guint8 * data, gsize size) |
| 674 |
{ |
| 675 |
GstByteReader reader; |
| 676 |
gboolean ret = FALSE; |
| 677 |
|
| 678 |
gst_byte_reader_init (&reader, data, size); |
| 679 |
|
| 680 |
if (!parse_ptp_message_header (msg, &reader)) { |
| 681 |
GST_WARNING ("Failed to parse PTP message header"); |
| 682 |
return FALSE; |
| 683 |
} |
| 684 |
|
| 685 |
switch (msg->message_type) { |
| 686 |
case PTP_MESSAGE_TYPE_SYNC: |
| 687 |
ret = parse_ptp_message_sync (msg, &reader); |
| 688 |
break; |
| 689 |
case PTP_MESSAGE_TYPE_FOLLOW_UP: |
| 690 |
ret = parse_ptp_message_follow_up (msg, &reader); |
| 691 |
break; |
| 692 |
case PTP_MESSAGE_TYPE_DELAY_REQ: |
| 693 |
ret = parse_ptp_message_delay_req (msg, &reader); |
| 694 |
break; |
| 695 |
case PTP_MESSAGE_TYPE_DELAY_RESP: |
| 696 |
ret = parse_ptp_message_delay_resp (msg, &reader); |
| 697 |
break; |
| 698 |
case PTP_MESSAGE_TYPE_ANNOUNCE: |
| 699 |
ret = parse_ptp_message_announce (msg, &reader); |
| 700 |
break; |
| 701 |
default: |
| 702 |
/* ignore for now */ |
| 703 |
break; |
| 704 |
} |
| 705 |
|
| 706 |
return ret; |
| 707 |
} |
| 708 |
|
| 709 |
static gint |
| 710 |
compare_announce_message (const PtpAnnounceMessage * a, |
| 711 |
const PtpAnnounceMessage * b, gboolean skip_tiebreakers) |
| 712 |
{ |
| 713 |
/* IEEE 1588 Figure 27 */ |
| 714 |
if (a->grandmaster_identity == b->grandmaster_identity) { |
| 715 |
// Random threshold of 4 timeouts to completely ignore all steps removed |
| 716 |
if (a->sender->timed_out_delay_resp + a->sender->timed_out_sync + 4 < |
| 717 |
b->sender->timed_out_delay_resp + b->sender->timed_out_sync) |
| 718 |
return -1; |
| 719 |
if (a->sender->timed_out_delay_resp + a->sender->timed_out_sync > |
| 720 |
b->sender->timed_out_delay_resp + b->sender->timed_out_sync + 4) |
| 721 |
return 1; |
| 722 |
|
| 723 |
if (a->steps_removed < b->steps_removed) |
| 724 |
return -1; |
| 725 |
else if (a->steps_removed > b->steps_removed) |
| 726 |
return 1; |
| 727 |
|
| 728 |
// If both are the same number of steps removed, prefer the clock with |
| 729 |
// fewer timeouts before going to the tie breakers based on clock |
| 730 |
// identities and interface indices |
| 731 |
if (a->sender->timed_out_delay_resp + a->sender->timed_out_sync < |
| 732 |
b->sender->timed_out_delay_resp + b->sender->timed_out_sync) |
| 733 |
return -1; |
| 734 |
if (a->sender->timed_out_delay_resp + a->sender->timed_out_sync > |
| 735 |
b->sender->timed_out_delay_resp + b->sender->timed_out_sync) |
| 736 |
return 1; |
| 737 |
|
| 738 |
if (skip_tiebreakers) |
| 739 |
return 0; |
| 740 |
|
| 741 |
/* Error cases are filtered out earlier */ |
| 742 |
if (a->master_clock_identity.clock_identity < |
| 743 |
b->master_clock_identity.clock_identity) |
| 744 |
return -1; |
| 745 |
else if (a->master_clock_identity.clock_identity > |
| 746 |
b->master_clock_identity.clock_identity) |
| 747 |
return 1; |
| 748 |
|
| 749 |
/* Error cases are filtered out earlier */ |
| 750 |
if (a->master_clock_identity.port_number < |
| 751 |
b->master_clock_identity.port_number) |
| 752 |
return -1; |
| 753 |
else if (a->master_clock_identity.port_number > |
| 754 |
b->master_clock_identity.port_number) |
| 755 |
return 1; |
| 756 |
|
| 757 |
if (a->iface_idx < b->iface_idx) |
| 758 |
return -1; |
| 759 |
else if (a->iface_idx > b->iface_idx) |
| 760 |
return 1; |
| 761 |
|
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
if (a->grandmaster_priority_1 < b->grandmaster_priority_1) |
| 766 |
return -1; |
| 767 |
else if (a->grandmaster_priority_1 > b->grandmaster_priority_1) |
| 768 |
return 1; |
| 769 |
|
| 770 |
if (a->grandmaster_clock_quality.clock_class < |
| 771 |
b->grandmaster_clock_quality.clock_class) |
| 772 |
return -1; |
| 773 |
else if (a->grandmaster_clock_quality.clock_class > |
| 774 |
b->grandmaster_clock_quality.clock_class) |
| 775 |
return 1; |
| 776 |
|
| 777 |
if (a->grandmaster_clock_quality.clock_accuracy < |
| 778 |
b->grandmaster_clock_quality.clock_accuracy) |
| 779 |
return -1; |
| 780 |
else if (a->grandmaster_clock_quality.clock_accuracy > |
| 781 |
b->grandmaster_clock_quality.clock_accuracy) |
| 782 |
return 1; |
| 783 |
|
| 784 |
if (a->grandmaster_clock_quality.offset_scaled_log_variance < |
| 785 |
b->grandmaster_clock_quality.offset_scaled_log_variance) |
| 786 |
return -1; |
| 787 |
else if (a->grandmaster_clock_quality.offset_scaled_log_variance > |
| 788 |
b->grandmaster_clock_quality.offset_scaled_log_variance) |
| 789 |
return 1; |
| 790 |
|
| 791 |
if (a->grandmaster_priority_2 < b->grandmaster_priority_2) |
| 792 |
return -1; |
| 793 |
else if (a->grandmaster_priority_2 > b->grandmaster_priority_2) |
| 794 |
return 1; |
| 795 |
|
| 796 |
if (a->grandmaster_identity < b->grandmaster_identity) |
| 797 |
return -1; |
| 798 |
else if (a->grandmaster_identity > b->grandmaster_identity) |
| 799 |
return 1; |
| 800 |
else |
| 801 |
g_assert_not_reached (); |
| 802 |
|
| 803 |
return 0; |
| 804 |
} |
| 805 |
|
| 806 |
static void |
| 807 |
select_best_master_clock (PtpDomainData * domain, GstClockTime now) |
| 808 |
{ |
| 809 |
GList *qualified_messages = NULL; |
| 810 |
GList *l, *m; |
| 811 |
PtpAnnounceMessage *best = NULL; |
| 812 |
|
| 813 |
/* IEEE 1588 9.3.2.5 */ |
| 814 |
for (l = domain->announce_senders; l; l = l->next) { |
| 815 |
PtpAnnounceSender *sender = l->data; |
| 816 |
GstClockTime window = 4 * sender->announce_interval; |
| 817 |
gint count = 0; |
| 818 |
|
| 819 |
for (m = sender->announce_messages.head; m; m = m->next) { |
| 820 |
PtpAnnounceMessage *msg = m->data; |
| 821 |
|
| 822 |
if (now - msg->receive_time <= window) |
| 823 |
count++; |
| 824 |
} |
| 825 |
|
| 826 |
/* Only include the newest message of announce senders that had at least 2 |
| 827 |
* announce messages in the last 4 announce intervals. Which also means |
| 828 |
* that we wait at least 4 announce intervals before we select a master |
| 829 |
* clock. Until then we just report based on the newest SYNC we received |
| 830 |
*/ |
| 831 |
if (count >= 2) { |
| 832 |
qualified_messages = |
| 833 |
g_list_prepend (qualified_messages, |
| 834 |
g_queue_peek_tail (&sender->announce_messages)); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if (!qualified_messages) { |
| 839 |
GST_DEBUG |
| 840 |
("No qualified announce messages for domain %u, can't select a master clock", |
| 841 |
domain->domain); |
| 842 |
domain->have_master_clock = FALSE; |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
for (l = qualified_messages; l; l = l->next) { |
| 847 |
PtpAnnounceMessage *msg = l->data; |
| 848 |
|
| 849 |
if (!best || compare_announce_message (msg, best, FALSE) < 0) |
| 850 |
best = msg; |
| 851 |
} |
| 852 |
|
| 853 |
GST_DEBUG ("Found master clock for domain %u: 0x%016" G_GINT64_MODIFIER |
| 854 |
"x %u on interface %u with grandmaster clock 0x%016" G_GINT64_MODIFIER |
| 855 |
"x", domain->domain, best->master_clock_identity.clock_identity, |
| 856 |
best->master_clock_identity.port_number, best->iface_idx, |
| 857 |
best->grandmaster_identity); |
| 858 |
|
| 859 |
// Check if the newly selected best clock and the previous one are |
| 860 |
// equivalent except for tiebreakers. In that case, don't actually switch |
| 861 |
// to avoid switching regularly between clocks. |
| 862 |
if (domain->have_master_clock && |
| 863 |
(compare_clock_identity (&domain->master_clock_identity, |
| 864 |
&best->master_clock_identity) != 0 |
| 865 |
|| domain->iface_idx != best->iface_idx) |
| 866 |
) { |
| 867 |
// Find announce sender for the currently selected clock |
| 868 |
for (l = domain->announce_senders; l; l = l->next) { |
| 869 |
PtpAnnounceSender *sender = l->data; |
| 870 |
|
| 871 |
if (compare_clock_identity (&domain->master_clock_identity, |
| 872 |
&sender->master_clock_identity) == 0 |
| 873 |
&& domain->iface_idx == sender->iface_idx) { |
| 874 |
|
| 875 |
// Find qualified message for it (if there is none then it timed out) |
| 876 |
for (m = qualified_messages; m; m = m->next) { |
| 877 |
PtpAnnounceMessage *msg = m->data; |
| 878 |
|
| 879 |
if (compare_clock_identity (&sender->master_clock_identity, |
| 880 |
&msg->master_clock_identity) == 0 |
| 881 |
&& sender->iface_idx == msg->iface_idx) { |
| 882 |
|
| 883 |
if (compare_announce_message (msg, best, TRUE) == 0) { |
| 884 |
GST_DEBUG |
| 885 |
("Currently selected master clock for domain %u is equivalent", |
| 886 |
domain->domain); |
| 887 |
best = msg; |
| 888 |
} |
| 889 |
|
| 890 |
break; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
break; |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
g_clear_pointer (&qualified_messages, g_list_free); |
| 900 |
|
| 901 |
if (domain->have_master_clock |
| 902 |
&& compare_clock_identity (&domain->master_clock_identity, |
| 903 |
&best->master_clock_identity) == 0 |
| 904 |
&& domain->iface_idx == best->iface_idx) { |
| 905 |
GST_DEBUG ("Master clock in domain %u did not change", domain->domain); |
| 906 |
} else { |
| 907 |
GST_DEBUG ("Selected new master clock for domain %u: 0x%016" |
| 908 |
G_GINT64_MODIFIER "x %u on interface %u with grandmaster clock 0x%016" |
| 909 |
G_GINT64_MODIFIER "x", domain->domain, |
| 910 |
best->master_clock_identity.clock_identity, |
| 911 |
best->master_clock_identity.port_number, best->iface_idx, |
| 912 |
best->grandmaster_identity); |
| 913 |
|
| 914 |
domain->have_master_clock = TRUE; |
| 915 |
domain->grandmaster_identity = best->grandmaster_identity; |
| 916 |
|
| 917 |
/* Opportunistic master clock selection likely gave us the same master |
| 918 |
* clock before, no need to reset all statistics */ |
| 919 |
if (compare_clock_identity (&domain->master_clock_identity, |
| 920 |
&best->master_clock_identity) != 0 |
| 921 |
|| domain->iface_idx != best->iface_idx) { |
| 922 |
memcpy (&domain->master_clock_identity, &best->master_clock_identity, |
| 923 |
sizeof (PtpClockIdentity)); |
| 924 |
domain->iface_idx = best->iface_idx; |
| 925 |
domain->mean_path_delay = 0; |
| 926 |
domain->last_delay_req = 0; |
| 927 |
domain->last_path_delays_missing = 9; |
| 928 |
domain->last_ptp_delay_resp_time_local = 0; |
| 929 |
domain->min_delay_req_interval = 0; |
| 930 |
domain->sync_interval = 0; |
| 931 |
domain->last_ptp_sync_time = 0; |
| 932 |
domain->last_ptp_sync_time_local = 0; |
| 933 |
domain->skipped_updates = 0; |
| 934 |
g_queue_foreach (&domain->pending_syncs, (GFunc) ptp_pending_sync_free, |
| 935 |
NULL); |
| 936 |
g_queue_clear (&domain->pending_syncs); |
| 937 |
} |
| 938 |
|
| 939 |
if (g_atomic_int_get (&domain_stats_n_hooks)) { |
| 940 |
GstStructure *stats = |
| 941 |
gst_structure_new (GST_PTP_STATISTICS_BEST_MASTER_CLOCK_SELECTED, |
| 942 |
"domain", G_TYPE_UINT, domain->domain, |
| 943 |
"master-clock-id", G_TYPE_UINT64, |
| 944 |
domain->master_clock_identity.clock_identity, |
| 945 |
"master-clock-port", G_TYPE_UINT, |
| 946 |
domain->master_clock_identity.port_number, |
| 947 |
"grandmaster-clock-id", G_TYPE_UINT64, domain->grandmaster_identity, |
| 948 |
NULL); |
| 949 |
emit_ptp_statistics (domain->domain, stats); |
| 950 |
gst_structure_free (stats); |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
static void |
| 956 |
handle_announce_message (PtpMessage * msg, guint8 iface_idx, |
| 957 |
GstClockTime receive_time) |
| 958 |
{ |
| 959 |
GList *l; |
| 960 |
PtpDomainData *domain = NULL; |
| 961 |
PtpAnnounceSender *sender = NULL; |
| 962 |
PtpAnnounceMessage *announce; |
| 963 |
|
| 964 |
/* IEEE1588 9.3.2.2 e) |
| 965 |
* Don't consider messages with the alternate master flag set |
| 966 |
*/ |
| 967 |
if ((msg->flag_field & 0x0100)) |
| 968 |
return; |
| 969 |
|
| 970 |
/* IEEE 1588 9.3.2.5 d) |
| 971 |
* Don't consider announce messages with steps_removed>=255 |
| 972 |
*/ |
| 973 |
if (msg->message_specific.announce.steps_removed >= 255) |
| 974 |
return; |
| 975 |
|
| 976 |
for (l = domain_data; l; l = l->next) { |
| 977 |
PtpDomainData *tmp = l->data; |
| 978 |
|
| 979 |
if (tmp->domain == msg->domain_number) { |
| 980 |
domain = tmp; |
| 981 |
break; |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
if (!domain) { |
| 986 |
gchar *clock_name; |
| 987 |
|
| 988 |
domain = g_new0 (PtpDomainData, 1); |
| 989 |
domain->domain = msg->domain_number; |
| 990 |
clock_name = g_strdup_printf ("ptp-clock-%u", domain->domain); |
| 991 |
domain->domain_clock = |
| 992 |
g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", clock_name, NULL); |
| 993 |
gst_object_ref_sink (domain->domain_clock); |
| 994 |
g_free (clock_name); |
| 995 |
g_queue_init (&domain->pending_syncs); |
| 996 |
domain->last_path_delays_missing = 9; |
| 997 |
domain_data = g_list_prepend (domain_data, domain); |
| 998 |
|
| 999 |
g_mutex_lock (&domain_clocks_lock); |
| 1000 |
domain_clocks = g_list_prepend (domain_clocks, domain); |
| 1001 |
g_mutex_unlock (&domain_clocks_lock); |
| 1002 |
|
| 1003 |
if (g_atomic_int_get (&domain_stats_n_hooks)) { |
| 1004 |
GstStructure *stats = |
| 1005 |
gst_structure_new (GST_PTP_STATISTICS_NEW_DOMAIN_FOUND, "domain", |
| 1006 |
G_TYPE_UINT, domain->domain, "clock", GST_TYPE_CLOCK, |
| 1007 |
domain->domain_clock, NULL); |
| 1008 |
emit_ptp_statistics (domain->domain, stats); |
| 1009 |
gst_structure_free (stats); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
for (l = domain->announce_senders; l; l = l->next) { |
| 1014 |
PtpAnnounceSender *tmp = l->data; |
| 1015 |
|
| 1016 |
if (compare_clock_identity (&tmp->master_clock_identity, |
| 1017 |
&msg->source_port_identity) == 0 && tmp->iface_idx == iface_idx) { |
| 1018 |
sender = tmp; |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (!sender) { |
| 1024 |
sender = g_new0 (PtpAnnounceSender, 1); |
| 1025 |
|
| 1026 |
memcpy (&sender->master_clock_identity, &msg->source_port_identity, |
| 1027 |
sizeof (PtpClockIdentity)); |
| 1028 |
sender->iface_idx = iface_idx; |
| 1029 |
g_queue_init (&sender->announce_messages); |
| 1030 |
domain->announce_senders = |
| 1031 |
g_list_prepend (domain->announce_senders, sender); |
| 1032 |
} |
| 1033 |
|
| 1034 |
for (l = sender->announce_messages.head; l; l = l->next) { |
| 1035 |
PtpAnnounceMessage *tmp = l->data; |
| 1036 |
|
| 1037 |
/* IEEE 1588 9.3.2.5 c) |
| 1038 |
* Don't consider identical messages, i.e. duplicates |
| 1039 |
*/ |
| 1040 |
if (tmp->sequence_id == msg->sequence_id) |
| 1041 |
return; |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (msg->log_message_interval == 0x7f) { |
| 1045 |
sender->announce_interval = 2 * GST_SECOND; |
| 1046 |
|
| 1047 |
if (!ptpd_hybrid_workaround_warned_once) { |
| 1048 |
GST_WARNING ("Working around ptpd bug: ptpd sends multicast PTP packets " |
| 1049 |
"with invalid logMessageInterval"); |
| 1050 |
ptpd_hybrid_workaround_warned_once = TRUE; |
| 1051 |
} |
| 1052 |
} else { |
| 1053 |
sender->announce_interval = log2_to_clock_time (msg->log_message_interval); |
| 1054 |
} |
| 1055 |
|
| 1056 |
announce = g_new0 (PtpAnnounceMessage, 1); |
| 1057 |
announce->sender = sender; |
| 1058 |
announce->receive_time = receive_time; |
| 1059 |
announce->sequence_id = msg->sequence_id; |
| 1060 |
memcpy (&announce->master_clock_identity, &msg->source_port_identity, |
| 1061 |
sizeof (PtpClockIdentity)); |
| 1062 |
announce->iface_idx = iface_idx; |
| 1063 |
announce->grandmaster_identity = |
| 1064 |
msg->message_specific.announce.grandmaster_identity; |
| 1065 |
announce->grandmaster_priority_1 = |
| 1066 |
msg->message_specific.announce.grandmaster_priority_1; |
| 1067 |
announce->grandmaster_clock_quality.clock_class = |
| 1068 |
msg->message_specific.announce.grandmaster_clock_quality.clock_class; |
| 1069 |
announce->grandmaster_clock_quality.clock_accuracy = |
| 1070 |
msg->message_specific.announce.grandmaster_clock_quality.clock_accuracy; |
| 1071 |
announce->grandmaster_clock_quality.offset_scaled_log_variance = |
| 1072 |
msg->message_specific.announce. |
| 1073 |
grandmaster_clock_quality.offset_scaled_log_variance; |
| 1074 |
announce->grandmaster_priority_2 = |
| 1075 |
msg->message_specific.announce.grandmaster_priority_2; |
| 1076 |
announce->steps_removed = msg->message_specific.announce.steps_removed; |
| 1077 |
announce->time_source = msg->message_specific.announce.time_source; |
| 1078 |
g_queue_push_tail (&sender->announce_messages, announce); |
| 1079 |
|
| 1080 |
select_best_master_clock (domain, receive_time); |
| 1081 |
} |
| 1082 |
|
| 1083 |
static gboolean |
| 1084 |
send_delay_req_timeout (PtpPendingSync * sync) |
| 1085 |
{ |
| 1086 |
guint8 message[STDIO_MESSAGE_HEADER_SIZE + 1 + 8 + 44] = { 0, }; |
| 1087 |
GstByteWriter writer; |
| 1088 |
gsize written; |
| 1089 |
GError *err = NULL; |
| 1090 |
GstClockTime send_time; |
| 1091 |
|
| 1092 |
GST_TRACE ("Sending delay_req to domain %u", sync->domain); |
| 1093 |
|
| 1094 |
sync->delay_req_send_time_local = send_time = |
| 1095 |
gst_clock_get_time (observation_system_clock); |
| 1096 |
|
| 1097 |
gst_byte_writer_init_with_data (&writer, message, sizeof (message), FALSE); |
| 1098 |
gst_byte_writer_put_uint16_be_unchecked (&writer, 1 + 8 + 44); |
| 1099 |
gst_byte_writer_put_uint8_unchecked (&writer, TYPE_EVENT); |
| 1100 |
gst_byte_writer_put_uint8_unchecked (&writer, sync->iface_idx); |
| 1101 |
gst_byte_writer_put_uint64_be_unchecked (&writer, send_time); |
| 1102 |
gst_byte_writer_put_uint8_unchecked (&writer, PTP_MESSAGE_TYPE_DELAY_REQ); |
| 1103 |
gst_byte_writer_put_uint8_unchecked (&writer, 2); |
| 1104 |
gst_byte_writer_put_uint16_be_unchecked (&writer, 44); |
| 1105 |
gst_byte_writer_put_uint8_unchecked (&writer, sync->domain); |
| 1106 |
gst_byte_writer_put_uint8_unchecked (&writer, 0); |
| 1107 |
gst_byte_writer_put_uint16_be_unchecked (&writer, 0); |
| 1108 |
gst_byte_writer_put_uint64_be_unchecked (&writer, 0); |
| 1109 |
gst_byte_writer_put_uint32_be_unchecked (&writer, 0); |
| 1110 |
gst_byte_writer_put_uint64_be_unchecked (&writer, |
| 1111 |
ptp_clock_id.clock_identity); |
| 1112 |
gst_byte_writer_put_uint16_be_unchecked (&writer, ptp_clock_id.port_number); |
| 1113 |
gst_byte_writer_put_uint16_be_unchecked (&writer, sync->delay_req_seqnum); |
| 1114 |
gst_byte_writer_put_uint8_unchecked (&writer, 0x01); |
| 1115 |
gst_byte_writer_put_uint8_unchecked (&writer, 0x7f); |
| 1116 |
gst_byte_writer_put_uint64_be_unchecked (&writer, 0); |
| 1117 |
gst_byte_writer_put_uint16_be_unchecked (&writer, 0); |
| 1118 |
|
| 1119 |
if (!g_output_stream_write_all (stdin_pipe, message, sizeof (message), |
| 1120 |
&written, NULL, &err)) { |
| 1121 |
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CLOSED) |
| 1122 |
|| g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) { |
| 1123 |
GST_ERROR ("Got EOF on stdout"); |
| 1124 |
} else { |
| 1125 |
GST_ERROR ("Failed to write delay-req to stdin: %s", err->message); |
| 1126 |
} |
| 1127 |
|
| 1128 |
g_message ("EOF on stdout"); |
| 1129 |
g_main_loop_quit (main_loop); |
| 1130 |
return G_SOURCE_REMOVE; |
| 1131 |
} else if (written != sizeof (message)) { |
| 1132 |
GST_ERROR ("Unexpected write size: %" G_GSIZE_FORMAT, written); |
| 1133 |
g_main_loop_quit (main_loop); |
| 1134 |
return G_SOURCE_REMOVE; |
| 1135 |
} |
| 1136 |
|
| 1137 |
return G_SOURCE_REMOVE; |
| 1138 |
} |
| 1139 |
|
| 1140 |
static gboolean |
| 1141 |
send_delay_req (PtpDomainData * domain, PtpPendingSync * sync) |
| 1142 |
{ |
| 1143 |
GstClockTime now = gst_clock_get_time (observation_system_clock); |
| 1144 |
guint timeout; |
| 1145 |
GSource *timeout_source; |
| 1146 |
|
| 1147 |
if (domain->last_delay_req != 0 |
| 1148 |
&& domain->last_delay_req + domain->min_delay_req_interval > now) { |
| 1149 |
GST_TRACE ("Too soon to send new DELAY_REQ"); |
| 1150 |
return FALSE; |
| 1151 |
} |
| 1152 |
|
| 1153 |
domain->last_delay_req = now; |
| 1154 |
sync->iface_idx = domain->iface_idx; |
| 1155 |
sync->delay_req_seqnum = domain->last_delay_req_seqnum++; |
| 1156 |
|
| 1157 |
/* IEEE 1588 9.5.11.2 */ |
| 1158 |
if (domain->last_delay_req == 0 || domain->min_delay_req_interval == 0) |
| 1159 |
timeout = 0; |
| 1160 |
else |
| 1161 |
timeout = |
| 1162 |
g_rand_int_range (delay_req_rand, 0, |
| 1163 |
(domain->min_delay_req_interval * 2) / GST_MSECOND); |
| 1164 |
|
| 1165 |
sync->timeout_source = timeout_source = g_timeout_source_new (timeout); |
| 1166 |
g_source_set_priority (timeout_source, G_PRIORITY_DEFAULT); |
| 1167 |
g_source_set_callback (timeout_source, (GSourceFunc) send_delay_req_timeout, |
| 1168 |
sync, NULL); |
| 1169 |
g_source_attach (timeout_source, main_context); |
| 1170 |
|
| 1171 |
return TRUE; |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* Filtering of outliers for RTT and time calculations inspired |
| 1175 |
* by the code from gstnetclientclock.c |
| 1176 |
*/ |
| 1177 |
static void |
| 1178 |
update_ptp_time (PtpDomainData * domain, PtpPendingSync * sync) |
| 1179 |
{ |
| 1180 |
GstClockTime internal_time, external_time, rate_num, rate_den; |
| 1181 |
GstClockTime corrected_ptp_time, corrected_local_time; |
| 1182 |
gdouble r_squared = 0.0; |
| 1183 |
gboolean synced; |
| 1184 |
GstClockTimeDiff discont = 0; |
| 1185 |
GstClockTime estimated_ptp_time = GST_CLOCK_TIME_NONE; |
| 1186 |
#ifdef USE_MEASUREMENT_FILTERING |
| 1187 |
GstClockTime orig_internal_time, orig_external_time, orig_rate_num, |
| 1188 |
orig_rate_den; |
| 1189 |
GstClockTime new_estimated_ptp_time; |
| 1190 |
GstClockTime max_discont, estimated_ptp_time_min, estimated_ptp_time_max; |
| 1191 |
gboolean now_synced; |
| 1192 |
#endif |
| 1193 |
#ifdef USE_ONLY_SYNC_WITH_DELAY |
| 1194 |
GstClockTime mean_path_delay; |
| 1195 |
#endif |
| 1196 |
|
| 1197 |
GST_TRACE ("Updating PTP time"); |
| 1198 |
|
| 1199 |
#ifdef USE_ONLY_SYNC_WITH_DELAY |
| 1200 |
if (sync->delay_req_send_time_local == GST_CLOCK_TIME_NONE) { |
| 1201 |
GST_TRACE ("Not updating - no delay_req sent"); |
| 1202 |
return; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* IEEE 1588 11.3 */ |
| 1206 |
mean_path_delay = |
| 1207 |
(sync->delay_req_recv_time_remote - sync->sync_send_time_remote + |
| 1208 |
sync->sync_recv_time_local - sync->delay_req_send_time_local - |
| 1209 |
(sync->correction_field_sync + sync->correction_field_delay + |
| 1210 |
32768) / 65536) / 2; |
| 1211 |
#endif |
| 1212 |
|
| 1213 |
/* IEEE 1588 11.2 */ |
| 1214 |
corrected_ptp_time = |
| 1215 |
sync->sync_send_time_remote + |
| 1216 |
(sync->correction_field_sync + 32768) / 65536; |
| 1217 |
|
| 1218 |
#ifdef USE_ONLY_SYNC_WITH_DELAY |
| 1219 |
corrected_local_time = sync->sync_recv_time_local - mean_path_delay; |
| 1220 |
#else |
| 1221 |
corrected_local_time = sync->sync_recv_time_local - domain->mean_path_delay; |
| 1222 |
#endif |
| 1223 |
|
| 1224 |
#ifdef USE_MEASUREMENT_FILTERING |
| 1225 |
/* We check this here and when updating the mean path delay, because |
| 1226 |
* we can get here without a delay response too. The tolerance on |
| 1227 |
* accepting follow-up after a sync is high, because a PTP server |
| 1228 |
* doesn't have to prioritise sending FOLLOW_UP - its purpose is |
| 1229 |
* just to give us the accurate timestamp of the preceding SYNC. |
| 1230 |
* |
| 1231 |
* For that reason also allow at least 100ms delay in case of delays smaller |
| 1232 |
* than 5ms. */ |
| 1233 |
if (sync->follow_up_recv_time_local != GST_CLOCK_TIME_NONE |
| 1234 |
&& sync->follow_up_recv_time_local > |
| 1235 |
sync->sync_recv_time_local + MAX (100 * GST_MSECOND, |
| 1236 |
20 * domain->mean_path_delay)) { |
| 1237 |
GstClockTimeDiff delay = |
| 1238 |
sync->follow_up_recv_time_local - sync->sync_recv_time_local; |
| 1239 |
GST_WARNING ("Sync-follow-up delay for domain %u too big: %" |
| 1240 |
GST_STIME_FORMAT " > MAX(100ms, 20 * %" GST_TIME_FORMAT ")", |
| 1241 |
domain->domain, GST_STIME_ARGS (delay), |
| 1242 |
GST_TIME_ARGS (domain->mean_path_delay)); |
| 1243 |
synced = FALSE; |
| 1244 |
gst_clock_get_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1245 |
&internal_time, &external_time, &rate_num, &rate_den); |
| 1246 |
goto out; |
| 1247 |
} |
| 1248 |
#endif |
| 1249 |
|
| 1250 |
/* Set an initial local-remote relation */ |
| 1251 |
if (domain->last_ptp_time == 0) |
| 1252 |
gst_clock_set_calibration (domain->domain_clock, corrected_local_time, |
| 1253 |
corrected_ptp_time, 1, 1); |
| 1254 |
|
| 1255 |
#ifdef USE_MEASUREMENT_FILTERING |
| 1256 |
/* Check if the corrected PTP time is +/- 3/4 RTT around what we would |
| 1257 |
* estimate with our present knowledge about the clock |
| 1258 |
*/ |
| 1259 |
/* Store what the clock produced as 'now' before this update */ |
| 1260 |
gst_clock_get_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1261 |
&orig_internal_time, &orig_external_time, &orig_rate_num, &orig_rate_den); |
| 1262 |
internal_time = orig_internal_time; |
| 1263 |
external_time = orig_external_time; |
| 1264 |
rate_num = orig_rate_num; |
| 1265 |
rate_den = orig_rate_den; |
| 1266 |
|
| 1267 |
/* 3/4 RTT window around the estimation */ |
| 1268 |
max_discont = domain->mean_path_delay * 3 / 2; |
| 1269 |
|
| 1270 |
/* Check if the estimated sync time is inside our window */ |
| 1271 |
estimated_ptp_time_min = corrected_local_time - max_discont; |
| 1272 |
estimated_ptp_time_min = |
| 1273 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1274 |
estimated_ptp_time_min, internal_time, external_time, rate_num, rate_den); |
| 1275 |
estimated_ptp_time_max = corrected_local_time + max_discont; |
| 1276 |
estimated_ptp_time_max = |
| 1277 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1278 |
estimated_ptp_time_max, internal_time, external_time, rate_num, rate_den); |
| 1279 |
|
| 1280 |
synced = (estimated_ptp_time_min < corrected_ptp_time |
| 1281 |
&& corrected_ptp_time < estimated_ptp_time_max); |
| 1282 |
|
| 1283 |
GST_DEBUG ("Adding observation for domain %u: %" GST_TIME_FORMAT " - %" |
| 1284 |
GST_TIME_FORMAT, domain->domain, |
| 1285 |
GST_TIME_ARGS (corrected_ptp_time), GST_TIME_ARGS (corrected_local_time)); |
| 1286 |
|
| 1287 |
GST_DEBUG ("Synced %d: %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT " < %" |
| 1288 |
GST_TIME_FORMAT, synced, GST_TIME_ARGS (estimated_ptp_time_min), |
| 1289 |
GST_TIME_ARGS (corrected_ptp_time), |
| 1290 |
GST_TIME_ARGS (estimated_ptp_time_max)); |
| 1291 |
|
| 1292 |
if (gst_clock_add_observation_unapplied (domain->domain_clock, |
| 1293 |
corrected_local_time, corrected_ptp_time, &r_squared, |
| 1294 |
&internal_time, &external_time, &rate_num, &rate_den)) { |
| 1295 |
GST_DEBUG ("Regression gave r_squared: %f", r_squared); |
| 1296 |
|
| 1297 |
/* Old estimated PTP time based on receive time and path delay */ |
| 1298 |
estimated_ptp_time = corrected_local_time; |
| 1299 |
estimated_ptp_time = |
| 1300 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST |
| 1301 |
(domain->domain_clock), estimated_ptp_time, orig_internal_time, |
| 1302 |
orig_external_time, orig_rate_num, orig_rate_den); |
| 1303 |
|
| 1304 |
/* New estimated PTP time based on receive time and path delay */ |
| 1305 |
new_estimated_ptp_time = corrected_local_time; |
| 1306 |
new_estimated_ptp_time = |
| 1307 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST |
| 1308 |
(domain->domain_clock), new_estimated_ptp_time, internal_time, |
| 1309 |
external_time, rate_num, rate_den); |
| 1310 |
|
| 1311 |
discont = GST_CLOCK_DIFF (estimated_ptp_time, new_estimated_ptp_time); |
| 1312 |
if (synced && ABS (discont) > max_discont) { |
| 1313 |
GstClockTimeDiff offset; |
| 1314 |
GST_DEBUG ("Too large a discont %s%" GST_TIME_FORMAT |
| 1315 |
", clamping to 1/4 average RTT = %" GST_TIME_FORMAT, |
| 1316 |
(discont < 0 ? "-" : ""), GST_TIME_ARGS (ABS (discont)), |
| 1317 |
GST_TIME_ARGS (max_discont)); |
| 1318 |
if (discont > 0) { /* Too large a forward step - add a -ve offset */ |
| 1319 |
offset = max_discont - discont; |
| 1320 |
if (-offset > external_time) |
| 1321 |
external_time = 0; |
| 1322 |
else |
| 1323 |
external_time += offset; |
| 1324 |
} else { /* Too large a backward step - add a +ve offset */ |
| 1325 |
offset = -(max_discont + discont); |
| 1326 |
external_time += offset; |
| 1327 |
} |
| 1328 |
|
| 1329 |
discont += offset; |
| 1330 |
} else { |
| 1331 |
GST_DEBUG ("Discont %s%" GST_TIME_FORMAT " (max: %" GST_TIME_FORMAT ")", |
| 1332 |
(discont < 0 ? "-" : ""), GST_TIME_ARGS (ABS (discont)), |
| 1333 |
GST_TIME_ARGS (max_discont)); |
| 1334 |
} |
| 1335 |
|
| 1336 |
/* Check if the estimated sync time is now (still) inside our window */ |
| 1337 |
estimated_ptp_time_min = corrected_local_time - max_discont; |
| 1338 |
estimated_ptp_time_min = |
| 1339 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST |
| 1340 |
(domain->domain_clock), estimated_ptp_time_min, internal_time, |
| 1341 |
external_time, rate_num, rate_den); |
| 1342 |
estimated_ptp_time_max = corrected_local_time + max_discont; |
| 1343 |
estimated_ptp_time_max = |
| 1344 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST |
| 1345 |
(domain->domain_clock), estimated_ptp_time_max, internal_time, |
| 1346 |
external_time, rate_num, rate_den); |
| 1347 |
|
| 1348 |
now_synced = (estimated_ptp_time_min < corrected_ptp_time |
| 1349 |
&& corrected_ptp_time < estimated_ptp_time_max); |
| 1350 |
|
| 1351 |
GST_DEBUG ("Now synced %d: %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT " < %" |
| 1352 |
GST_TIME_FORMAT, now_synced, GST_TIME_ARGS (estimated_ptp_time_min), |
| 1353 |
GST_TIME_ARGS (corrected_ptp_time), |
| 1354 |
GST_TIME_ARGS (estimated_ptp_time_max)); |
| 1355 |
|
| 1356 |
if (synced || now_synced || domain->skipped_updates > MAX_SKIPPED_UPDATES) { |
| 1357 |
gst_clock_set_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1358 |
internal_time, external_time, rate_num, rate_den); |
| 1359 |
domain->skipped_updates = 0; |
| 1360 |
|
| 1361 |
domain->last_ptp_time = corrected_ptp_time; |
| 1362 |
domain->last_local_time = corrected_local_time; |
| 1363 |
} else { |
| 1364 |
domain->skipped_updates++; |
| 1365 |
} |
| 1366 |
} else { |
| 1367 |
domain->last_ptp_time = corrected_ptp_time; |
| 1368 |
domain->last_local_time = corrected_local_time; |
| 1369 |
} |
| 1370 |
|
| 1371 |
#else |
| 1372 |
GST_DEBUG ("Adding observation for domain %u: %" GST_TIME_FORMAT " - %" |
| 1373 |
GST_TIME_FORMAT, domain->domain, |
| 1374 |
GST_TIME_ARGS (corrected_ptp_time), GST_TIME_ARGS (corrected_local_time)); |
| 1375 |
|
| 1376 |
gst_clock_get_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1377 |
&internal_time, &external_time, &rate_num, &rate_den); |
| 1378 |
|
| 1379 |
estimated_ptp_time = corrected_local_time; |
| 1380 |
estimated_ptp_time = |
| 1381 |
gst_clock_adjust_with_calibration (GST_CLOCK_CAST |
| 1382 |
(domain->domain_clock), estimated_ptp_time, internal_time, |
| 1383 |
external_time, rate_num, rate_den); |
| 1384 |
|
| 1385 |
gst_clock_add_observation (domain->domain_clock, |
| 1386 |
corrected_local_time, corrected_ptp_time, &r_squared); |
| 1387 |
|
| 1388 |
gst_clock_get_calibration (GST_CLOCK_CAST (domain->domain_clock), |
| 1389 |
&internal_time, &external_time, &rate_num, &rate_den); |
| 1390 |
|
| 1391 |
synced = TRUE; |
| 1392 |
domain->last_ptp_time = corrected_ptp_time; |
| 1393 |
domain->last_local_time = corrected_local_time; |
| 1394 |
#endif |
| 1395 |
|
| 1396 |
#ifdef USE_MEASUREMENT_FILTERING |
| 1397 |
out: |
| 1398 |
#endif |
| 1399 |
if (g_atomic_int_get (&domain_stats_n_hooks)) { |
| 1400 |
GstStructure *stats = gst_structure_new (GST_PTP_STATISTICS_TIME_UPDATED, |
| 1401 |
"domain", G_TYPE_UINT, domain->domain, |
| 1402 |
"mean-path-delay-avg", GST_TYPE_CLOCK_TIME, domain->mean_path_delay, |
| 1403 |
"local-time", GST_TYPE_CLOCK_TIME, corrected_local_time, |
| 1404 |
"ptp-time", GST_TYPE_CLOCK_TIME, corrected_ptp_time, |
| 1405 |
"estimated-ptp-time", GST_TYPE_CLOCK_TIME, estimated_ptp_time, |
| 1406 |
"discontinuity", G_TYPE_INT64, discont, |
| 1407 |
"synced", G_TYPE_BOOLEAN, synced, |
| 1408 |
"r-squared", G_TYPE_DOUBLE, r_squared, |
| 1409 |
"internal-time", GST_TYPE_CLOCK_TIME, internal_time, |
| 1410 |
"external-time", GST_TYPE_CLOCK_TIME, external_time, |
| 1411 |
"rate-num", G_TYPE_UINT64, rate_num, |
| 1412 |
"rate-den", G_TYPE_UINT64, rate_den, |
| 1413 |
"rate", G_TYPE_DOUBLE, (gdouble) (rate_num) / rate_den, |
| 1414 |
NULL); |
| 1415 |
emit_ptp_statistics (domain->domain, stats); |
| 1416 |
gst_structure_free (stats); |
| 1417 |
} |
| 1418 |
|
| 1419 |
} |
| 1420 |
|
| 1421 |
#ifdef USE_MEDIAN_PRE_FILTERING |
| 1422 |
static gint |
| 1423 |
compare_clock_time (const GstClockTime * a, const GstClockTime * b) |
| 1424 |
{ |
| 1425 |
if (*a < *b) |
| 1426 |
return -1; |
| 1427 |
else if (*a > *b) |
| 1428 |
return 1; |
| 1429 |
return 0; |
| 1430 |
} |
| 1431 |
#endif |
| 1432 |
|
| 1433 |
static gboolean |
| 1434 |
update_mean_path_delay (PtpDomainData * domain, PtpPendingSync * sync) |
| 1435 |
{ |
| 1436 |
#ifdef USE_MEDIAN_PRE_FILTERING |
| 1437 |
GstClockTime last_path_delays[MEDIAN_PRE_FILTERING_WINDOW]; |
| 1438 |
GstClockTime median; |
| 1439 |
gint i; |
| 1440 |
#endif |
| 1441 |
|
| 1442 |
GstClockTime mean_path_delay, delay_req_delay = 0; |
| 1443 |
gboolean ret; |
| 1444 |
|
| 1445 |
/* IEEE 1588 11.3 */ |
| 1446 |
mean_path_delay = |
| 1447 |
(sync->delay_req_recv_time_remote - sync->sync_send_time_remote + |
| 1448 |
sync->sync_recv_time_local - sync->delay_req_send_time_local - |
| 1449 |
(sync->correction_field_sync + sync->correction_field_delay + |
| 1450 |
32768) / 65536) / 2; |
| 1451 |
|
| 1452 |
#ifdef USE_MEDIAN_PRE_FILTERING |
| 1453 |
for (i = 1; i < MEDIAN_PRE_FILTERING_WINDOW; i++) |
| 1454 |
domain->last_path_delays[i - 1] = domain->last_path_delays[i]; |
| 1455 |
domain->last_path_delays[i - 1] = mean_path_delay; |
| 1456 |
|
| 1457 |
if (domain->last_path_delays_missing) { |
| 1458 |
domain->last_path_delays_missing--; |
| 1459 |
} else { |
| 1460 |
memcpy (&last_path_delays, &domain->last_path_delays, |
| 1461 |
sizeof (last_path_delays)); |
| 1462 |
g_sort_array (&last_path_delays, |
| 1463 |
MEDIAN_PRE_FILTERING_WINDOW, sizeof (GstClockTime), |
| 1464 |
(GCompareDataFunc) compare_clock_time, NULL); |
| 1465 |
|
| 1466 |
median = last_path_delays[MEDIAN_PRE_FILTERING_WINDOW / 2]; |
| 1467 |
|
| 1468 |
/* FIXME: We might want to use something else here, like only allowing |
| 1469 |
* things in the interquartile range, or also filtering away delays that |
| 1470 |
* are too small compared to the median. This here worked well enough |
| 1471 |
* in tests so far. |
| 1472 |
*/ |
| 1473 |
if (mean_path_delay > 2 * median) { |
| 1474 |
GST_WARNING ("Path delay for domain %u too big compared to median: %" |
| 1475 |
GST_TIME_FORMAT " > 2 * %" GST_TIME_FORMAT, domain->domain, |
| 1476 |
GST_TIME_ARGS (mean_path_delay), GST_TIME_ARGS (median)); |
| 1477 |
ret = FALSE; |
| 1478 |
goto out; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
#endif |
| 1482 |
|
| 1483 |
#ifdef USE_RUNNING_AVERAGE_DELAY |
| 1484 |
/* Track an average round trip time, for a bit of smoothing */ |
| 1485 |
/* Always update before discarding a sample, so genuine changes in |
| 1486 |
* the network get picked up, eventually */ |
| 1487 |
if (domain->mean_path_delay == 0) |
| 1488 |
domain->mean_path_delay = mean_path_delay; |
| 1489 |
else if (mean_path_delay < domain->mean_path_delay) /* Shorter RTTs carry more weight than longer */ |
| 1490 |
domain->mean_path_delay = |
| 1491 |
(3 * domain->mean_path_delay + mean_path_delay) / 4; |
| 1492 |
else |
| 1493 |
domain->mean_path_delay = |
| 1494 |
(15 * domain->mean_path_delay + mean_path_delay) / 16; |
| 1495 |
#else |
| 1496 |
domain->mean_path_delay = mean_path_delay; |
| 1497 |
#endif |
| 1498 |
|
| 1499 |
#ifdef USE_MEASUREMENT_FILTERING |
| 1500 |
/* The tolerance on accepting follow-up after a sync is high, because |
| 1501 |
* a PTP server doesn't have to prioritise sending FOLLOW_UP - its purpose is |
| 1502 |
* just to give us the accurate timestamp of the preceding SYNC. |
| 1503 |
* |
| 1504 |
* For that reason also allow at least 100ms delay in case of delays smaller |
| 1505 |
* than 5ms. */ |
| 1506 |
if (sync->follow_up_recv_time_local != GST_CLOCK_TIME_NONE && |
| 1507 |
domain->mean_path_delay != 0 |
| 1508 |
&& sync->follow_up_recv_time_local > |
| 1509 |
sync->sync_recv_time_local + MAX (100 * GST_MSECOND, |
| 1510 |
20 * domain->mean_path_delay)) { |
| 1511 |
GST_WARNING ("Sync-follow-up delay for domain %u too big: %" GST_TIME_FORMAT |
| 1512 |
" > MAX(100ms, 20 * %" GST_TIME_FORMAT ")", domain->domain, |
| 1513 |
GST_TIME_ARGS (sync->follow_up_recv_time_local - |
| 1514 |
sync->sync_recv_time_local), |
| 1515 |
GST_TIME_ARGS (domain->mean_path_delay)); |
| 1516 |
ret = FALSE; |
| 1517 |
goto out; |
| 1518 |
} |
| 1519 |
|
| 1520 |
if (mean_path_delay > 2 * domain->mean_path_delay) { |
| 1521 |
GST_WARNING ("Mean path delay for domain %u too big: %" GST_TIME_FORMAT |
| 1522 |
" > 2 * %" GST_TIME_FORMAT, domain->domain, |
| 1523 |
GST_TIME_ARGS (mean_path_delay), |
| 1524 |
GST_TIME_ARGS (domain->mean_path_delay)); |
| 1525 |
ret = FALSE; |
| 1526 |
goto out; |
| 1527 |
} |
| 1528 |
#endif |
| 1529 |
|
| 1530 |
delay_req_delay = |
| 1531 |
sync->delay_resp_recv_time_local - sync->delay_req_send_time_local; |
| 1532 |
|
| 1533 |
#ifdef USE_MEASUREMENT_FILTERING |
| 1534 |
/* delay_req_delay is a RTT, so 2 times the path delay is what we'd |
| 1535 |
* hope for, but some PTP systems don't prioritise sending DELAY_RESP, |
| 1536 |
* but they must still have placed an accurate reception timestamp. |
| 1537 |
* That means we should be quite tolerant about late DELAY_RESP, and |
| 1538 |
* mostly rely on filtering out jumps in the mean-path-delay elsewhere. |
| 1539 |
* |
| 1540 |
* For that reason also allow at least 100ms delay in case of delays smaller |
| 1541 |
* than 5ms. */ |
| 1542 |
if (delay_req_delay > MAX (100 * GST_MSECOND, 20 * domain->mean_path_delay)) { |
| 1543 |
GST_WARNING ("Delay-request-response delay for domain %u too big: %" |
| 1544 |
GST_TIME_FORMAT " > MAX(100ms, 20 * %" GST_TIME_FORMAT ")", |
| 1545 |
domain->domain, GST_TIME_ARGS (delay_req_delay), |
| 1546 |
GST_TIME_ARGS (domain->mean_path_delay)); |
| 1547 |
ret = FALSE; |
| 1548 |
goto out; |
| 1549 |
} |
| 1550 |
#endif |
| 1551 |
|
| 1552 |
ret = TRUE; |
| 1553 |
|
| 1554 |
GST_DEBUG ("Got mean path delay for domain %u: %" GST_TIME_FORMAT " (new: %" |
| 1555 |
GST_TIME_FORMAT ")", domain->domain, |
| 1556 |
GST_TIME_ARGS (domain->mean_path_delay), GST_TIME_ARGS (mean_path_delay)); |
| 1557 |
GST_DEBUG ("Delay request delay for domain %u: %" GST_TIME_FORMAT, |
| 1558 |
domain->domain, GST_TIME_ARGS (delay_req_delay)); |
| 1559 |
|
| 1560 |
#if defined(USE_MEASUREMENT_FILTERING) || defined(USE_MEDIAN_PRE_FILTERING) |
| 1561 |
out: |
| 1562 |
#endif |
| 1563 |
if (g_atomic_int_get (&domain_stats_n_hooks)) { |
| 1564 |
GstStructure *stats = |
| 1565 |
gst_structure_new (GST_PTP_STATISTICS_PATH_DELAY_MEASURED, |
| 1566 |
"domain", G_TYPE_UINT, domain->domain, |
| 1567 |
"mean-path-delay-avg", GST_TYPE_CLOCK_TIME, domain->mean_path_delay, |
| 1568 |
"mean-path-delay", GST_TYPE_CLOCK_TIME, mean_path_delay, |
| 1569 |
"delay-request-delay", GST_TYPE_CLOCK_TIME, delay_req_delay, NULL); |
| 1570 |
emit_ptp_statistics (domain->domain, stats); |
| 1571 |
gst_structure_free (stats); |
| 1572 |
} |
| 1573 |
|
| 1574 |
return ret; |
| 1575 |
} |
| 1576 |
|
| 1577 |
static void |
| 1578 |
handle_sync_message (PtpMessage * msg, guint8 iface_idx, |
| 1579 |
GstClockTime receive_time) |
| 1580 |
{ |
| 1581 |
GList *l; |
| 1582 |
PtpDomainData *domain = NULL; |
| 1583 |
PtpPendingSync *sync = NULL; |
| 1584 |
|
| 1585 |
/* Don't consider messages with the alternate master flag set */ |
| 1586 |
if ((msg->flag_field & 0x0100)) { |
| 1587 |
GST_TRACE ("Ignoring sync message with alternate-master flag"); |
| 1588 |
return; |
| 1589 |
} |
| 1590 |
|
| 1591 |
for (l = domain_data; l; l = l->next) { |
| 1592 |
PtpDomainData *tmp = l->data; |
| 1593 |
|
| 1594 |
if (msg->domain_number == tmp->domain) { |
| 1595 |
domain = tmp; |
| 1596 |
break; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (!domain) { |
| 1601 |
gchar *clock_name; |
| 1602 |
|
| 1603 |
domain = g_new0 (PtpDomainData, 1); |
| 1604 |
domain->domain = msg->domain_number; |
| 1605 |
clock_name = g_strdup_printf ("ptp-clock-%u", domain->domain); |
| 1606 |
domain->domain_clock = |
| 1607 |
g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", clock_name, NULL); |
| 1608 |
gst_object_ref_sink (domain->domain_clock); |
| 1609 |
g_free (clock_name); |
| 1610 |
g_queue_init (&domain->pending_syncs); |
| 1611 |
domain->last_path_delays_missing = 9; |
| 1612 |
domain_data = g_list_prepend (domain_data, domain); |
| 1613 |
|
| 1614 |
g_mutex_lock (&domain_clocks_lock); |
| 1615 |
domain_clocks = g_list_prepend (domain_clocks, domain); |
| 1616 |
g_mutex_unlock (&domain_clocks_lock); |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* If we have a master clock, ignore this message if it's not coming from there */ |
| 1620 |
if (domain->have_master_clock |
| 1621 |
&& (compare_clock_identity (&domain->master_clock_identity, |
| 1622 |
&msg->source_port_identity) != 0 |
| 1623 |
|| domain->iface_idx != iface_idx)) { |
| 1624 |
GST_TRACE ("SYNC msg not from current clock master. Ignoring"); |
| 1625 |
return; |
| 1626 |
} |
| 1627 |
|
| 1628 |
#ifdef USE_OPPORTUNISTIC_CLOCK_SELECTION |
| 1629 |
/* Opportunistic selection of master clock */ |
| 1630 |
if (!domain->have_master_clock) { |
| 1631 |
memcpy (&domain->master_clock_identity, &msg->source_port_identity, |
| 1632 |
sizeof (PtpClockIdentity)); |
| 1633 |
domain->iface_idx = iface_idx; |
| 1634 |
} |
| 1635 |
#else |
| 1636 |
if (!domain->have_master_clock) |
| 1637 |
return; |
| 1638 |
#endif |
| 1639 |
|
| 1640 |
if (msg->log_message_interval == 0x7f) { |
| 1641 |
domain->sync_interval = GST_SECOND; |
| 1642 |
|
| 1643 |
if (!ptpd_hybrid_workaround_warned_once) { |
| 1644 |
GST_WARNING ("Working around ptpd bug: ptpd sends multicast PTP packets " |
| 1645 |
"with invalid logMessageInterval"); |
| 1646 |
ptpd_hybrid_workaround_warned_once = TRUE; |
| 1647 |
} |
| 1648 |
} else { |
| 1649 |
domain->sync_interval = log2_to_clock_time (msg->log_message_interval); |
| 1650 |
} |
| 1651 |
|
| 1652 |
/* Check if duplicated */ |
| 1653 |
for (l = domain->pending_syncs.head; l; l = l->next) { |
| 1654 |
PtpPendingSync *tmp = l->data; |
| 1655 |
|
| 1656 |
if (tmp->sync_seqnum == msg->sequence_id) |
| 1657 |
return; |
| 1658 |
} |
| 1659 |
|
| 1660 |
if (msg->message_specific.sync.origin_timestamp.seconds_field > |
| 1661 |
GST_CLOCK_TIME_NONE / GST_SECOND) { |
| 1662 |
GST_FIXME ("Unsupported sync message seconds field value: %" |
| 1663 |
G_GUINT64_FORMAT " > %" G_GUINT64_FORMAT, |
| 1664 |
msg->message_specific.sync.origin_timestamp.seconds_field, |
| 1665 |
GST_CLOCK_TIME_NONE / GST_SECOND); |
| 1666 |
return; |
| 1667 |
} |
| 1668 |
|
| 1669 |
sync = g_new0 (PtpPendingSync, 1); |
| 1670 |
sync->domain = domain->domain; |
| 1671 |
sync->sync_seqnum = msg->sequence_id; |
| 1672 |
sync->sync_recv_time_local = receive_time; |
| 1673 |
sync->sync_send_time_remote = GST_CLOCK_TIME_NONE; |
| 1674 |
sync->follow_up_recv_time_local = GST_CLOCK_TIME_NONE; |
| 1675 |
sync->delay_req_send_time_local = GST_CLOCK_TIME_NONE; |
| 1676 |
sync->delay_req_recv_time_remote = GST_CLOCK_TIME_NONE; |
| 1677 |
sync->delay_resp_recv_time_local = GST_CLOCK_TIME_NONE; |
| 1678 |
sync->delay_req_seqnum = G_MAXUINT32; |
| 1679 |
|
| 1680 |
/* 0.5 correction factor for division later */ |
| 1681 |
sync->correction_field_sync = msg->correction_field; |
| 1682 |
|
| 1683 |
if ((msg->flag_field & 0x0200)) { |
| 1684 |
/* Wait for FOLLOW_UP */ |
| 1685 |
GST_TRACE ("Waiting for FOLLOW_UP msg"); |
| 1686 |
} else { |
| 1687 |
sync->sync_send_time_remote = |
| 1688 |
PTP_TIMESTAMP_TO_GST_CLOCK_TIME (msg->message_specific. |
| 1689 |
sync.origin_timestamp); |
| 1690 |
|
| 1691 |
if (domain->last_ptp_sync_time != 0 |
| 1692 |
&& domain->last_ptp_sync_time >= sync->sync_send_time_remote) { |
| 1693 |
GST_WARNING ("Backwards PTP times in domain %u: %" GST_TIME_FORMAT " >= %" |
| 1694 |
GST_TIME_FORMAT, domain->domain, |
| 1695 |
GST_TIME_ARGS (domain->last_ptp_sync_time), |
| 1696 |
GST_TIME_ARGS (sync->sync_send_time_remote)); |
| 1697 |
ptp_pending_sync_free (sync); |
| 1698 |
sync = NULL; |
| 1699 |
return; |
| 1700 |
} |
| 1701 |
domain->last_ptp_sync_time = sync->sync_send_time_remote; |
| 1702 |
domain->last_ptp_sync_time_local = receive_time; |
| 1703 |
|
| 1704 |
for (l = domain->announce_senders; l; l = l->next) { |
| 1705 |
PtpAnnounceSender *sender = l->data; |
| 1706 |
|
| 1707 |
if (compare_clock_identity (&domain->master_clock_identity, |
| 1708 |
&sender->master_clock_identity) == 0 |
| 1709 |
&& domain->iface_idx == sender->iface_idx) { |
| 1710 |
|
| 1711 |
sender->timed_out_sync = 0; |
| 1712 |
|
| 1713 |
break; |
| 1714 |
} |
| 1715 |
} |
| 1716 |
|
| 1717 |
if (send_delay_req (domain, sync)) { |
| 1718 |
/* Sent delay request */ |
| 1719 |
} else { |
| 1720 |
update_ptp_time (domain, sync); |
| 1721 |
ptp_pending_sync_free (sync); |
| 1722 |
sync = NULL; |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (sync) |
| 1727 |
g_queue_push_tail (&domain->pending_syncs, sync); |
| 1728 |
} |
| 1729 |
|
| 1730 |
static void |
| 1731 |
handle_follow_up_message (PtpMessage * msg, guint8 iface_idx, |
| 1732 |
GstClockTime receive_time) |
| 1733 |
{ |
| 1734 |
GList *l; |
| 1735 |
PtpDomainData *domain = NULL; |
| 1736 |
PtpPendingSync *sync = NULL; |
| 1737 |
|
| 1738 |
GST_TRACE ("Processing FOLLOW_UP message"); |
| 1739 |
|
| 1740 |
/* Don't consider messages with the alternate master flag set */ |
| 1741 |
if ((msg->flag_field & 0x0100)) { |
| 1742 |
GST_TRACE ("Ignoring FOLLOW_UP with alternate-master flag"); |
| 1743 |
return; |
| 1744 |
} |
| 1745 |
|
| 1746 |
for (l = domain_data; l; l = l->next) { |
| 1747 |
PtpDomainData *tmp = l->data; |
| 1748 |
|
| 1749 |
if (msg->domain_number == tmp->domain) { |
| 1750 |
domain = tmp; |
| 1751 |
break; |
| 1752 |
} |
| 1753 |
} |
| 1754 |
|
| 1755 |
if (!domain) { |
| 1756 |
GST_TRACE ("No domain match for FOLLOW_UP msg"); |
| 1757 |
return; |
| 1758 |
} |
| 1759 |
|
| 1760 |
/* If we have a master clock, ignore this message if it's not coming from there */ |
| 1761 |
if (domain->have_master_clock |
| 1762 |
&& (compare_clock_identity (&domain->master_clock_identity, |
| 1763 |
&msg->source_port_identity) != 0 |
| 1764 |
|| domain->iface_idx != iface_idx)) { |
| 1765 |
GST_TRACE ("FOLLOW_UP msg not from current clock master. Ignoring"); |
| 1766 |
return; |
| 1767 |
} |
| 1768 |
|
| 1769 |
/* Check if we know about this one */ |
| 1770 |
for (l = domain->pending_syncs.head; l; l = l->next) { |
| 1771 |
PtpPendingSync *tmp = l->data; |
| 1772 |
|
| 1773 |
if (tmp->sync_seqnum == msg->sequence_id) { |
| 1774 |
sync = tmp; |
| 1775 |
break; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
if (!sync) { |
| 1780 |
GST_TRACE ("Ignoring FOLLOW_UP with no pending SYNC"); |
| 1781 |
return; |
| 1782 |
} |
| 1783 |
|
| 1784 |
/* Got a FOLLOW_UP for this already */ |
| 1785 |
if (sync->sync_send_time_remote != GST_CLOCK_TIME_NONE) { |
| 1786 |
GST_TRACE ("Got repeat FOLLOW_UP. Ignoring"); |
| 1787 |
return; |
| 1788 |
} |
| 1789 |
|
| 1790 |
if (sync->sync_recv_time_local >= receive_time) { |
| 1791 |
GST_ERROR ("Got bogus follow up in domain %u: %" GST_TIME_FORMAT " > %" |
| 1792 |
GST_TIME_FORMAT, domain->domain, |
| 1793 |
GST_TIME_ARGS (sync->sync_recv_time_local), |
| 1794 |
GST_TIME_ARGS (receive_time)); |
| 1795 |
g_queue_remove (&domain->pending_syncs, sync); |
| 1796 |
ptp_pending_sync_free (sync); |
| 1797 |
return; |
| 1798 |
} |
| 1799 |
|
| 1800 |
sync->correction_field_sync += msg->correction_field; |
| 1801 |
sync->sync_send_time_remote = |
| 1802 |
PTP_TIMESTAMP_TO_GST_CLOCK_TIME (msg->message_specific. |
| 1803 |
follow_up.precise_origin_timestamp); |
| 1804 |
sync->follow_up_recv_time_local = receive_time; |
| 1805 |
|
| 1806 |
if (domain->last_ptp_sync_time >= sync->sync_send_time_remote) { |
| 1807 |
GST_WARNING ("Backwards PTP times in domain %u: %" GST_TIME_FORMAT " >= %" |
| 1808 |
GST_TIME_FORMAT, domain->domain, |
| 1809 |
GST_TIME_ARGS (domain->last_ptp_sync_time), |
| 1810 |
GST_TIME_ARGS (sync->sync_send_time_remote)); |
| 1811 |
g_queue_remove (&domain->pending_syncs, sync); |
| 1812 |
ptp_pending_sync_free (sync); |
| 1813 |
sync = NULL; |
| 1814 |
return; |
| 1815 |
} |
| 1816 |
domain->last_ptp_sync_time = sync->sync_send_time_remote; |
| 1817 |
domain->last_ptp_sync_time_local = receive_time; |
| 1818 |
|
| 1819 |
for (l = domain->announce_senders; l; l = l->next) { |
| 1820 |
PtpAnnounceSender *sender = l->data; |
| 1821 |
|
| 1822 |
if (compare_clock_identity (&domain->master_clock_identity, |
| 1823 |
&sender->master_clock_identity) == 0 |
| 1824 |
&& domain->iface_idx == sender->iface_idx) { |
| 1825 |
|
| 1826 |
sender->timed_out_sync = 0; |
| 1827 |
|
| 1828 |
break; |
| 1829 |
} |
| 1830 |
} |
| 1831 |
|
| 1832 |
if (send_delay_req (domain, sync)) { |
| 1833 |
/* Sent delay request */ |
| 1834 |
} else { |
| 1835 |
update_ptp_time (domain, sync); |
| 1836 |
g_queue_remove (&domain->pending_syncs, sync); |
| 1837 |
ptp_pending_sync_free (sync); |
| 1838 |
sync = NULL; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
static void |
| 1843 |
handle_delay_resp_message (PtpMessage * msg, guint8 iface_idx, |
| 1844 |
GstClockTime receive_time) |
| 1845 |
{ |
| 1846 |
GList *l; |
| 1847 |
PtpDomainData *domain = NULL; |
| 1848 |
PtpPendingSync *sync = NULL; |
| 1849 |
|
| 1850 |
/* Not for us */ |
| 1851 |
if (msg->message_specific.delay_resp. |
| 1852 |
requesting_port_identity.clock_identity != ptp_clock_id.clock_identity |
| 1853 |
|| msg->message_specific.delay_resp. |
| 1854 |
requesting_port_identity.port_number != ptp_clock_id.port_number) |
| 1855 |
return; |
| 1856 |
|
| 1857 |
/* Don't consider messages with the alternate master flag set */ |
| 1858 |
if ((msg->flag_field & 0x0100)) |
| 1859 |
return; |
| 1860 |
|
| 1861 |
for (l = domain_data; l; l = l->next) { |
| 1862 |
PtpDomainData *tmp = l->data; |
| 1863 |
|
| 1864 |
if (msg->domain_number == tmp->domain) { |
| 1865 |
domain = tmp; |
| 1866 |
break; |
| 1867 |
} |
| 1868 |
} |
| 1869 |
|
| 1870 |
if (!domain) |
| 1871 |
return; |
| 1872 |
|
| 1873 |
/* If we have a master clock, ignore this message if it's not coming from there */ |
| 1874 |
if (domain->have_master_clock |
| 1875 |
&& (compare_clock_identity (&domain->master_clock_identity, |
| 1876 |
&msg->source_port_identity) != 0 |
| 1877 |
|| domain->iface_idx != iface_idx)) { |
| 1878 |
GST_TRACE ("DELAY_RESP msg not from current clock master. Ignoring"); |
| 1879 |
return; |
| 1880 |
} |
| 1881 |
|
| 1882 |
if (msg->log_message_interval == 0x7f) { |
| 1883 |
domain->min_delay_req_interval = GST_SECOND; |
| 1884 |
|
| 1885 |
if (!ptpd_hybrid_workaround_warned_once) { |
| 1886 |
GST_WARNING ("Working around ptpd bug: ptpd sends multicast PTP packets " |
| 1887 |
"with invalid logMessageInterval"); |
| 1888 |
ptpd_hybrid_workaround_warned_once = TRUE; |
| 1889 |
} |
| 1890 |
} else { |
| 1891 |
domain->min_delay_req_interval = |
| 1892 |
log2_to_clock_time (msg->log_message_interval); |
| 1893 |
} |
| 1894 |
|
| 1895 |
/* Check if we know about this one */ |
| 1896 |
for (l = domain->pending_syncs.head; l; l = l->next) { |
| 1897 |
PtpPendingSync *tmp = l->data; |
| 1898 |
|
| 1899 |
if (tmp->delay_req_seqnum == msg->sequence_id) { |
| 1900 |
sync = tmp; |
| 1901 |
break; |
| 1902 |
} |
| 1903 |
} |
| 1904 |
|
| 1905 |
if (!sync) |
| 1906 |
return; |
| 1907 |
|
| 1908 |
/* Got a DELAY_RESP for this already */ |
| 1909 |
if (sync->delay_req_recv_time_remote != GST_CLOCK_TIME_NONE) |
| 1910 |
return; |
| 1911 |
|
| 1912 |
if (sync->delay_req_send_time_local > receive_time) { |
| 1913 |
GST_ERROR ("Got bogus delay response in domain %u: %" GST_TIME_FORMAT " > %" |
| 1914 |
GST_TIME_FORMAT, domain->domain, |
| 1915 |
GST_TIME_ARGS (sync->delay_req_send_time_local), |
| 1916 |
GST_TIME_ARGS (receive_time)); |
| 1917 |
g_queue_remove (&domain->pending_syncs, sync); |
| 1918 |
ptp_pending_sync_free (sync); |
| 1919 |
return; |
| 1920 |
} |
| 1921 |
|
| 1922 |
sync->correction_field_delay = msg->correction_field; |
| 1923 |
|
| 1924 |
sync->delay_req_recv_time_remote = |
| 1925 |
PTP_TIMESTAMP_TO_GST_CLOCK_TIME (msg->message_specific. |
| 1926 |
delay_resp.receive_timestamp); |
| 1927 |
sync->delay_resp_recv_time_local = receive_time; |
| 1928 |
|
| 1929 |
if (domain->mean_path_delay != 0 |
| 1930 |
&& sync->sync_send_time_remote > sync->delay_req_recv_time_remote) { |
| 1931 |
GST_WARNING ("Sync send time after delay req receive time for domain %u: %" |
| 1932 |
GST_TIME_FORMAT " > %" GST_TIME_FORMAT, domain->domain, |
| 1933 |
GST_TIME_ARGS (sync->sync_send_time_remote), |
| 1934 |
GST_TIME_ARGS (sync->delay_req_recv_time_remote)); |
| 1935 |
g_queue_remove (&domain->pending_syncs, sync); |
| 1936 |
ptp_pending_sync_free (sync); |
| 1937 |
return; |
| 1938 |
} |
| 1939 |
|
| 1940 |
domain->last_ptp_delay_resp_time_local = receive_time; |
| 1941 |
|
| 1942 |
for (l = domain->announce_senders; l; l = l->next) { |
| 1943 |
PtpAnnounceSender *sender = l->data; |
| 1944 |
|
| 1945 |
if (compare_clock_identity (&domain->master_clock_identity, |
| 1946 |
&sender->master_clock_identity) == 0 |
| 1947 |
&& domain->iface_idx == sender->iface_idx) { |
| 1948 |
|
| 1949 |
sender->timed_out_delay_resp = 0; |
| 1950 |
|
| 1951 |
break; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
|
| 1955 |
if (update_mean_path_delay (domain, sync)) |
| 1956 |
update_ptp_time (domain, sync); |
| 1957 |
g_queue_remove (&domain->pending_syncs, sync); |
| 1958 |
ptp_pending_sync_free (sync); |
| 1959 |
} |
| 1960 |
|
| 1961 |
static void |
| 1962 |
handle_ptp_message (PtpMessage * msg, guint8 iface_idx, |
| 1963 |
GstClockTime receive_time) |
| 1964 |
{ |
| 1965 |
/* Ignore our own messages */ |
| 1966 |
if (msg->source_port_identity.clock_identity == ptp_clock_id.clock_identity && |
| 1967 |
msg->source_port_identity.port_number == ptp_clock_id.port_number) { |
| 1968 |
GST_TRACE ("Ignoring our own message"); |
| 1969 |
return; |
| 1970 |
} |
| 1971 |
|
| 1972 |
GST_TRACE ("Message type %d iface idx %d receive_time %" GST_TIME_FORMAT, |
| 1973 |
msg->message_type, iface_idx, GST_TIME_ARGS (receive_time)); |
| 1974 |
switch (msg->message_type) { |
| 1975 |
case PTP_MESSAGE_TYPE_ANNOUNCE: |
| 1976 |
handle_announce_message (msg, iface_idx, receive_time); |
| 1977 |
break; |
| 1978 |
case PTP_MESSAGE_TYPE_SYNC: |
| 1979 |
handle_sync_message (msg, iface_idx, receive_time); |
| 1980 |
break; |
| 1981 |
case PTP_MESSAGE_TYPE_FOLLOW_UP: |
| 1982 |
handle_follow_up_message (msg, iface_idx, receive_time); |
| 1983 |
break; |
| 1984 |
case PTP_MESSAGE_TYPE_DELAY_RESP: |
| 1985 |
handle_delay_resp_message (msg, iface_idx, receive_time); |
| 1986 |
break; |
| 1987 |
default: |
| 1988 |
break; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
|
| 1992 |
static void |
| 1993 |
handle_send_time_ack (const guint8 * data, gsize size, |
| 1994 |
GstClockTime receive_time) |
| 1995 |
{ |
| 1996 |
GstByteReader breader; |
| 1997 |
GstClockTime helper_send_time; |
| 1998 |
guint8 message_type; |
| 1999 |
guint8 domain_number; |
| 2000 |
guint16 seqnum; |
| 2001 |
GList *l; |
| 2002 |
PtpDomainData *domain = NULL; |
| 2003 |
PtpPendingSync *sync = NULL; |
| 2004 |
|
| 2005 |
gst_byte_reader_init (&breader, data, size); |
| 2006 |
helper_send_time = gst_byte_reader_get_uint64_be_unchecked (&breader); |
| 2007 |
message_type = gst_byte_reader_get_uint8_unchecked (&breader); |
| 2008 |
domain_number = gst_byte_reader_get_uint8_unchecked (&breader); |
| 2009 |
seqnum = gst_byte_reader_get_uint16_be_unchecked (&breader); |
| 2010 |
|
| 2011 |
GST_TRACE |
| 2012 |
("Received SEND_TIME_ACK for message type %d, domain number %d, seqnum %d with send time %" |
| 2013 |
GST_TIME_FORMAT " at receive_time %" GST_TIME_FORMAT, message_type, |
| 2014 |
domain_number, seqnum, GST_TIME_ARGS (helper_send_time), |
| 2015 |
GST_TIME_ARGS (receive_time)); |
| 2016 |
|
| 2017 |
if (message_type != PTP_MESSAGE_TYPE_DELAY_REQ) |
| 2018 |
return; |
| 2019 |
|
| 2020 |
for (l = domain_data; l; l = l->next) { |
| 2021 |
PtpDomainData *tmp = l->data; |
| 2022 |
|
| 2023 |
if (domain_number == tmp->domain) { |
| 2024 |
domain = tmp; |
| 2025 |
break; |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
if (!domain) |
| 2030 |
return; |
| 2031 |
|
| 2032 |
/* Check if we know about this one */ |
| 2033 |
for (l = domain->pending_syncs.head; l; l = l->next) { |
| 2034 |
PtpPendingSync *tmp = l->data; |
| 2035 |
|
| 2036 |
if (tmp->delay_req_seqnum == seqnum) { |
| 2037 |
sync = tmp; |
| 2038 |
break; |
| 2039 |
} |
| 2040 |
} |
| 2041 |
|
| 2042 |
if (!sync) |
| 2043 |
return; |
| 2044 |
|
| 2045 |
/* Got a DELAY_RESP for this already */ |
| 2046 |
if (sync->delay_req_recv_time_remote != GST_CLOCK_TIME_NONE) |
| 2047 |
return; |
| 2048 |
|
| 2049 |
if (helper_send_time != 0) { |
| 2050 |
GST_TRACE ("DELAY_REQ message took %" GST_STIME_FORMAT |
| 2051 |
" to helper process, SEND_TIME_ACK took %" GST_STIME_FORMAT |
| 2052 |
" from helper process", |
| 2053 |
GST_STIME_ARGS ((GstClockTimeDiff) (helper_send_time - |
| 2054 |
sync->delay_req_send_time_local)), |
| 2055 |
GST_STIME_ARGS ((GstClockTimeDiff) (receive_time - helper_send_time))); |
| 2056 |
sync->delay_req_send_time_local = helper_send_time; |
| 2057 |
} |
| 2058 |
} |
| 2059 |
|
| 2060 |
static void have_stdout_header (GInputStream * stdout_pipe, GAsyncResult * res, |
| 2061 |
gpointer user_data); |
| 2062 |
|
| 2063 |
static void |
| 2064 |
have_stdout_body (GInputStream * stdout_pipe, GAsyncResult * res, |
| 2065 |
gpointer user_data) |
| 2066 |
{ |
| 2067 |
GError *err = NULL; |
| 2068 |
gsize read; |
| 2069 |
|
| 2070 |
/* Finish reading the body */ |
| 2071 |
if (!g_input_stream_read_all_finish (stdout_pipe, res, &read, &err)) { |
| 2072 |
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CLOSED) || |
| 2073 |
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) { |
| 2074 |
GST_ERROR ("Got EOF on stdout"); |
| 2075 |
} else { |
| 2076 |
GST_ERROR ("Failed to read header from stdout: %s", err->message); |
| 2077 |
} |
| 2078 |
g_clear_error (&err); |
| 2079 |
g_main_loop_quit (main_loop); |
| 2080 |
return; |
| 2081 |
} else if (read == 0) { |
| 2082 |
GST_ERROR ("Got EOF on stdin"); |
| 2083 |
g_main_loop_quit (main_loop); |
| 2084 |
return; |
| 2085 |
} else if (read != CUR_STDIO_HEADER_SIZE) { |
| 2086 |
GST_ERROR ("Unexpected read size: %" G_GSIZE_FORMAT, read); |
| 2087 |
g_main_loop_quit (main_loop); |
| 2088 |
return; |
| 2089 |
} |
| 2090 |
|
| 2091 |
switch (CUR_STDIO_HEADER_TYPE) { |
| 2092 |
case TYPE_EVENT: |
| 2093 |
case TYPE_GENERAL:{ |
| 2094 |
GstClockTime receive_time = gst_clock_get_time (observation_system_clock); |
| 2095 |
guint8 iface_idx; |
| 2096 |
GstClockTime helper_receive_time; |
| 2097 |
PtpMessage msg; |
| 2098 |
|
| 2099 |
memset (&msg, 0, sizeof (msg)); |
| 2100 |
|
| 2101 |
iface_idx = GST_READ_UINT8 (stdout_buffer); |
| 2102 |
helper_receive_time = GST_READ_UINT64_BE (stdout_buffer + 1); |
| 2103 |
|
| 2104 |
if (parse_ptp_message (&msg, (const guint8 *) stdout_buffer + 1 + 8, |
| 2105 |
CUR_STDIO_HEADER_SIZE)) { |
| 2106 |
dump_ptp_message (&msg); |
| 2107 |
if (helper_receive_time != 0) { |
| 2108 |
GST_TRACE ("Message took %" GST_STIME_FORMAT " from helper process", |
| 2109 |
GST_STIME_ARGS ((GstClockTimeDiff) (receive_time - |
| 2110 |
helper_receive_time))); |
| 2111 |
receive_time = helper_receive_time; |
| 2112 |
} |
| 2113 |
handle_ptp_message (&msg, iface_idx, receive_time); |
| 2114 |
} |
| 2115 |
break; |
| 2116 |
} |
| 2117 |
case TYPE_CLOCK_ID:{ |
| 2118 |
if (CUR_STDIO_HEADER_SIZE != 8) { |
| 2119 |
GST_ERROR ("Unexpected clock id size (%u != 8)", CUR_STDIO_HEADER_SIZE); |
| 2120 |
g_main_loop_quit (main_loop); |
| 2121 |
return; |
| 2122 |
} |
| 2123 |
g_mutex_lock (&ptp_lock); |
| 2124 |
ptp_clock_id.clock_identity = GST_READ_UINT64_BE (stdout_buffer); |
| 2125 |
#ifdef G_OS_WIN32 |
| 2126 |
ptp_clock_id.port_number = (guint16) GetCurrentProcessId (); |
| 2127 |
#else |
| 2128 |
ptp_clock_id.port_number = getpid (); |
| 2129 |
#endif |
| 2130 |
GST_DEBUG ("Got clock id 0x%016" G_GINT64_MODIFIER "x %u", |
| 2131 |
ptp_clock_id.clock_identity, ptp_clock_id.port_number); |
| 2132 |
g_cond_signal (&ptp_cond); |
| 2133 |
g_mutex_unlock (&ptp_lock); |
| 2134 |
break; |
| 2135 |
} |
| 2136 |
case TYPE_SEND_TIME_ACK:{ |
| 2137 |
GstClockTime receive_time = gst_clock_get_time (observation_system_clock); |
| 2138 |
|
| 2139 |
if (CUR_STDIO_HEADER_SIZE != 12) { |
| 2140 |
GST_ERROR ("Unexpected send time ack size (%u != 12)", |
| 2141 |
CUR_STDIO_HEADER_SIZE); |
| 2142 |
g_main_loop_quit (main_loop); |
| 2143 |
return; |
| 2144 |
} |
| 2145 |
|
| 2146 |
handle_send_time_ack (stdout_buffer, CUR_STDIO_HEADER_SIZE, receive_time); |
| 2147 |
break; |
| 2148 |
} |
| 2149 |
default: |
| 2150 |
break; |
| 2151 |
} |
| 2152 |
|
| 2153 |
/* And read the next header */ |
| 2154 |
memset (&stdio_header, 0, STDIO_MESSAGE_HEADER_SIZE); |
| 2155 |
g_input_stream_read_all_async (stdout_pipe, stdio_header, |
| 2156 |
STDIO_MESSAGE_HEADER_SIZE, G_PRIORITY_DEFAULT, NULL, |
| 2157 |
(GAsyncReadyCallback) have_stdout_header, NULL); |
| 2158 |
} |
| 2159 |
|
| 2160 |
static void |
| 2161 |
have_stdout_header (GInputStream * stdout_pipe, GAsyncResult * res, |
| 2162 |
gpointer user_data) |
| 2163 |
{ |
| 2164 |
GError *err = NULL; |
| 2165 |
gsize read; |
| 2166 |
|
| 2167 |
/* Finish reading the header */ |
| 2168 |
if (!g_input_stream_read_all_finish (stdout_pipe, res, &read, &err)) { |
| 2169 |
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CLOSED) || |
| 2170 |
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) { |
| 2171 |
GST_ERROR ("Got EOF on stdout"); |
| 2172 |
} else { |
| 2173 |
GST_ERROR ("Failed to read header from stdout: %s", err->message); |
| 2174 |
} |
| 2175 |
g_clear_error (&err); |
| 2176 |
g_main_loop_quit (main_loop); |
| 2177 |
return; |
| 2178 |
} else if (read == 0) { |
| 2179 |
GST_ERROR ("Got EOF on stdin"); |
| 2180 |
return; |
| 2181 |
} else if (read != STDIO_MESSAGE_HEADER_SIZE) { |
| 2182 |
GST_ERROR ("Unexpected read size: %" G_GSIZE_FORMAT, read); |
| 2183 |
g_main_loop_quit (main_loop); |
| 2184 |
return; |
| 2185 |
} else if (CUR_STDIO_HEADER_SIZE > 8192) { |
| 2186 |
GST_ERROR ("Unexpected size: %u", CUR_STDIO_HEADER_SIZE); |
| 2187 |
g_main_loop_quit (main_loop); |
| 2188 |
return; |
| 2189 |
} |
| 2190 |
|
| 2191 |
/* And now read the body */ |
| 2192 |
g_input_stream_read_all_async (stdout_pipe, stdout_buffer, |
| 2193 |
CUR_STDIO_HEADER_SIZE, G_PRIORITY_DEFAULT, NULL, |
| 2194 |
(GAsyncReadyCallback) have_stdout_body, NULL); |
| 2195 |
} |
| 2196 |
|
| 2197 |
static void have_stderr_header (GInputStream * stderr_pipe, GAsyncResult * res, |
| 2198 |
gpointer user_data); |
| 2199 |
|
| 2200 |
static void |
| 2201 |
have_stderr_body (GInputStream * stderr_pipe, GAsyncResult * res, |
| 2202 |
gpointer user_data) |
| 2203 |
{ |
| 2204 |
GError *err = NULL; |
| 2205 |
gsize read; |
| 2206 |
#ifndef GST_DISABLE_GST_DEBUG |
| 2207 |
GstByteReader breader; |
| 2208 |
GstDebugLevel level; |
| 2209 |
guint16 filename_length; |
| 2210 |
gchar *filename = NULL; |
| 2211 |
guint16 module_path_length; |
| 2212 |
gchar *module_path = NULL; |
| 2213 |
guint32 line_number; |
| 2214 |
gchar *message = NULL; |
| 2215 |
guint16 message_length; |
| 2216 |
guint8 b; |
| 2217 |
#endif |
| 2218 |
|
| 2219 |
/* Finish reading the body */ |
| 2220 |
if (!g_input_stream_read_all_finish (stderr_pipe, res, &read, &err)) { |
| 2221 |
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CLOSED) || |
| 2222 |
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) { |
| 2223 |
GST_ERROR ("Got EOF on stderr"); |
| 2224 |
} else { |
| 2225 |
GST_ERROR ("Failed to read header from stderr: %s", err->message); |
| 2226 |
} |
| 2227 |
g_clear_error (&err); |
| 2228 |
g_main_loop_quit (main_loop); |
| 2229 |
return; |
| 2230 |
} else if (read == 0) { |
| 2231 |
GST_ERROR ("Got EOF on stderr"); |
| 2232 |
g_main_loop_quit (main_loop); |
| 2233 |
return; |
| 2234 |
} else if (read != CUR_STDERR_HEADER_SIZE) { |
| 2235 |
GST_ERROR ("Unexpected read size: %" G_GSIZE_FORMAT, read); |
| 2236 |
g_main_loop_quit (main_loop); |
| 2237 |
return; |
| 2238 |
} |
| 2239 |
|
| 2240 |
#ifndef GST_DISABLE_GST_DEBUG |
| 2241 |
gst_byte_reader_init (&breader, stderr_buffer, CUR_STDERR_HEADER_SIZE); |
| 2242 |
|
| 2243 |
if (!gst_byte_reader_get_uint8 (&breader, &b) || b > GST_LEVEL_MAX) |
| 2244 |
goto err; |
| 2245 |
level = (GstDebugLevel) b; |
| 2246 |
if (!gst_byte_reader_get_uint16_be (&breader, &filename_length) |
| 2247 |
|| filename_length > gst_byte_reader_get_remaining (&breader)) |
| 2248 |
goto err; |
| 2249 |
filename = |
| 2250 |
g_strndup ((const gchar *) gst_byte_reader_get_data_unchecked (&breader, |
| 2251 |
filename_length), filename_length); |
| 2252 |
|
| 2253 |
if (!gst_byte_reader_get_uint16_be (&breader, &module_path_length) |
| 2254 |
|| module_path_length > gst_byte_reader_get_remaining (&breader)) |
| 2255 |
goto err; |
| 2256 |
module_path = |
| 2257 |
g_strndup ((const gchar *) gst_byte_reader_get_data_unchecked (&breader, |
| 2258 |
module_path_length), module_path_length); |
| 2259 |
|
| 2260 |
if (!gst_byte_reader_get_uint32_be (&breader, &line_number)) |
| 2261 |
goto err; |
| 2262 |
|
| 2263 |
message_length = gst_byte_reader_get_remaining (&breader); |
| 2264 |
message = |
| 2265 |
g_strndup ((const gchar *) gst_byte_reader_get_data_unchecked (&breader, |
| 2266 |
message_length), message_length); |
| 2267 |
|
| 2268 |
gst_debug_log_literal (GST_CAT_DEFAULT, level, filename, module_path, |
| 2269 |
line_number, NULL, message); |
| 2270 |
|
| 2271 |
g_clear_pointer (&filename, g_free); |
| 2272 |
g_clear_pointer (&module_path, g_free); |
| 2273 |
g_clear_pointer (&message, g_free); |
| 2274 |
#endif |
| 2275 |
|
| 2276 |
/* And read the next header */ |
| 2277 |
memset (&stderr_header, 0, STDERR_MESSAGE_HEADER_SIZE); |
| 2278 |
g_input_stream_read_all_async (stderr_pipe, stderr_header, |
| 2279 |
STDERR_MESSAGE_HEADER_SIZE, G_PRIORITY_DEFAULT, NULL, |
| 2280 |
(GAsyncReadyCallback) have_stderr_header, NULL); |
| 2281 |
return; |
| 2282 |
|
| 2283 |
#ifndef GST_DISABLE_GST_DEBUG |
| 2284 |
err: |
| 2285 |
{ |
| 2286 |
GST_ERROR ("Unexpected stderr data"); |
| 2287 |
g_clear_pointer (&filename, g_free); |
| 2288 |
g_clear_pointer (&module_path, g_free); |
| 2289 |
g_clear_pointer (&message, g_free); |
| 2290 |
g_main_loop_quit (main_loop); |
| 2291 |
return; |
| 2292 |
} |
| 2293 |
#endif |
| 2294 |
} |
| 2295 |
|
| 2296 |
static void |
| 2297 |
have_stderr_header (GInputStream * stderr_pipe, GAsyncResult * res, |
| 2298 |
gpointer user_data) |
| 2299 |
{ |
| 2300 |
GError *err = NULL; |
| 2301 |
gsize read; |
| 2302 |
|
| 2303 |
/* Finish reading the header */ |
| 2304 |
if (!g_input_stream_read_all_finish (stderr_pipe, res, &read, &err)) { |
| 2305 |
if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CLOSED) || |
| 2306 |
g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED)) { |
| 2307 |
GST_ERROR ("Got EOF on stderr"); |
| 2308 |
} else { |
| 2309 |
GST_ERROR ("Failed to read header from stderr: %s", err->message); |
| 2310 |
} |
| 2311 |
g_clear_error (&err); |
| 2312 |
g_main_loop_quit (main_loop); |
| 2313 |
return; |
| 2314 |
} else if (read == 0) { |
| 2315 |
GST_ERROR ("Got EOF on stderr"); |
| 2316 |
g_main_loop_quit (main_loop); |
| 2317 |
return; |
| 2318 |
} else if (read != STDERR_MESSAGE_HEADER_SIZE) { |
| 2319 |
GST_ERROR ("Unexpected read size: %" G_GSIZE_FORMAT, read); |
| 2320 |
g_main_loop_quit (main_loop); |
| 2321 |
return; |
| 2322 |
} else if (CUR_STDERR_HEADER_SIZE > 8192 || CUR_STDERR_HEADER_SIZE < 9) { |
| 2323 |
GST_ERROR ("Unexpected size: %u", CUR_STDERR_HEADER_SIZE); |
| 2324 |
g_main_loop_quit (main_loop); |
| 2325 |
return; |
| 2326 |
} |
| 2327 |
|
| 2328 |
/* And now read the body */ |
| 2329 |
g_input_stream_read_all_async (stderr_pipe, stderr_buffer, |
| 2330 |
CUR_STDERR_HEADER_SIZE, G_PRIORITY_DEFAULT, NULL, |
| 2331 |
(GAsyncReadyCallback) have_stderr_body, NULL); |
| 2332 |
} |
| 2333 |
|
| 2334 |
/* Cleanup all announce messages and announce message senders |
| 2335 |
* that are timed out by now, and clean up all pending syncs |
| 2336 |
* that are missing their FOLLOW_UP or DELAY_RESP */ |
| 2337 |
static gboolean |
| 2338 |
cleanup_cb (gpointer data) |
| 2339 |
{ |
| 2340 |
GstClockTime now = gst_clock_get_time (observation_system_clock); |
| 2341 |
GList *l, *m, *n; |
| 2342 |
|
| 2343 |
for (l = domain_data; l; l = l->next) { |
| 2344 |
PtpDomainData *domain = l->data; |
| 2345 |
|
| 2346 |
for (n = domain->announce_senders; n;) { |
| 2347 |
PtpAnnounceSender *sender = n->data; |
| 2348 |
gboolean timed_out = TRUE; |
| 2349 |
|
| 2350 |
/* Keep only 5 messages per sender around */ |
| 2351 |
while (g_queue_get_length (&sender->announce_messages) > 5) { |
| 2352 |
PtpAnnounceMessage *msg = g_queue_pop_head (&sender->announce_messages); |
| 2353 |
g_free (msg); |
| 2354 |
} |
| 2355 |
|
| 2356 |
for (m = sender->announce_messages.head; m; m = m->next) { |
| 2357 |
PtpAnnounceMessage *msg = m->data; |
| 2358 |
|
| 2359 |
if (msg->receive_time + |
| 2360 |
sender->announce_interval * PTP_ANNOUNCE_RECEIPT_TIMEOUT > now) { |
| 2361 |
timed_out = FALSE; |
| 2362 |
break; |
| 2363 |
} |
| 2364 |
} |
| 2365 |
|
| 2366 |
if (timed_out) { |
| 2367 |
GST_DEBUG ("Announce sender 0x%016" G_GINT64_MODIFIER "x %u timed out", |
| 2368 |
sender->master_clock_identity.clock_identity, |
| 2369 |
sender->master_clock_identity.port_number); |
| 2370 |
g_queue_foreach (&sender->announce_messages, (GFunc) g_free, NULL); |
| 2371 |
g_queue_clear (&sender->announce_messages); |
| 2372 |
} |
| 2373 |
|
| 2374 |
if (g_queue_get_length (&sender->announce_messages) == 0) { |
| 2375 |
GList *tmp = n->next; |
| 2376 |
|
| 2377 |
if (compare_clock_identity (&sender->master_clock_identity, |
| 2378 |
&domain->master_clock_identity) == 0 |
| 2379 |
&& sender->iface_idx == domain->iface_idx) |
| 2380 |
GST_WARNING ("currently selected master clock timed out"); |
| 2381 |
g_free (sender); |
| 2382 |
domain->announce_senders = |
| 2383 |
g_list_delete_link (domain->announce_senders, n); |
| 2384 |
n = tmp; |
| 2385 |
} else { |
| 2386 |
n = n->next; |
| 2387 |
} |
| 2388 |
} |
| 2389 |
|
| 2390 |
/* Clean up any pending syncs */ |
| 2391 |
for (n = domain->pending_syncs.head; n;) { |
| 2392 |
PtpPendingSync *sync = n->data; |
| 2393 |
gboolean timed_out = FALSE; |
| 2394 |
gboolean clock_timed_out_sync = FALSE; |
| 2395 |
gboolean clock_timed_out_delay_resp = FALSE; |
| 2396 |
|
| 2397 |
/* Time out pending syncs after 4 sync intervals or 10 seconds, |
| 2398 |
* and pending delay reqs after 4 delay req intervals or 10 seconds |
| 2399 |
*/ |
| 2400 |
if (sync->delay_req_send_time_local != GST_CLOCK_TIME_NONE && |
| 2401 |
((domain->min_delay_req_interval != 0 |
| 2402 |
&& sync->delay_req_send_time_local + |
| 2403 |
4 * domain->min_delay_req_interval < now) |
| 2404 |
|| (sync->delay_req_send_time_local + 10 * GST_SECOND < now))) { |
| 2405 |
timed_out = TRUE; |
| 2406 |
|
| 2407 |
// If no newer delay resp received in the meantime, downgrade the |
| 2408 |
// selected clock |
| 2409 |
if (domain->last_ptp_delay_resp_time_local < |
| 2410 |
sync->delay_req_send_time_local) { |
| 2411 |
clock_timed_out_delay_resp = TRUE; |
| 2412 |
} |
| 2413 |
} else if (sync->follow_up_recv_time_local == GST_CLOCK_TIME_NONE && ( |
| 2414 |
(domain->sync_interval != 0 |
| 2415 |
&& sync->sync_recv_time_local + 4 * domain->sync_interval < |
| 2416 |
now) |
| 2417 |
|| (sync->sync_recv_time_local + 10 * GST_SECOND < now))) { |
| 2418 |
timed_out = TRUE; |
| 2419 |
|
| 2420 |
// If no newer sync/follow-up received in the meantime, downgrade the |
| 2421 |
// selected clock |
| 2422 |
if (domain->last_ptp_sync_time_local < sync->sync_recv_time_local) { |
| 2423 |
clock_timed_out_sync = TRUE; |
| 2424 |
} |
| 2425 |
} |
| 2426 |
|
| 2427 |
// If the clock is timed out then downgrade it now in case there is a |
| 2428 |
// better clock for the domain that can be selected below. |
| 2429 |
if (domain->have_master_clock && (clock_timed_out_sync |
| 2430 |
|| clock_timed_out_delay_resp)) { |
| 2431 |
GST_DEBUG ("Currently selected clock timed out, downgrading"); |
| 2432 |
|
| 2433 |
for (m = domain->announce_senders; m; m = m->next) { |
| 2434 |
PtpAnnounceSender *sender = m->data; |
| 2435 |
|
| 2436 |
if (compare_clock_identity (&domain->master_clock_identity, |
| 2437 |
&sender->master_clock_identity) == 0 |
| 2438 |
&& domain->iface_idx == sender->iface_idx) { |
| 2439 |
|
| 2440 |
if (clock_timed_out_sync) |
| 2441 |
sender->timed_out_sync++; |
| 2442 |
if (clock_timed_out_delay_resp) |
| 2443 |
sender->timed_out_delay_resp++; |
| 2444 |
|
| 2445 |
break; |
| 2446 |
} |
| 2447 |
} |
| 2448 |
} |
| 2449 |
|
| 2450 |
if (timed_out) { |
| 2451 |
GList *tmp = n->next; |
| 2452 |
ptp_pending_sync_free (sync); |
| 2453 |
g_queue_delete_link (&domain->pending_syncs, n); |
| 2454 |
n = tmp; |
| 2455 |
} else { |
| 2456 |
n = n->next; |
| 2457 |
} |
| 2458 |
} |
| 2459 |
|
| 2460 |
select_best_master_clock (domain, now); |
| 2461 |
} |
| 2462 |
|
| 2463 |
return G_SOURCE_CONTINUE; |
| 2464 |
} |
| 2465 |
|
| 2466 |
static gpointer |
| 2467 |
ptp_helper_main (gpointer data) |
| 2468 |
{ |
| 2469 |
GSource *cleanup_source; |
| 2470 |
|
| 2471 |
GST_DEBUG ("Starting PTP helper loop"); |
| 2472 |
|
| 2473 |
g_main_context_push_thread_default (main_context); |
| 2474 |
|
| 2475 |
memset (&stdio_header, 0, STDIO_MESSAGE_HEADER_SIZE); |
| 2476 |
g_input_stream_read_all_async (stdout_pipe, stdio_header, |
| 2477 |
STDIO_MESSAGE_HEADER_SIZE, G_PRIORITY_DEFAULT, NULL, |
| 2478 |
(GAsyncReadyCallback) have_stdout_header, NULL); |
| 2479 |
|
| 2480 |
memset (&stderr_header, 0, STDERR_MESSAGE_HEADER_SIZE); |
| 2481 |
g_input_stream_read_all_async (stderr_pipe, stderr_header, |
| 2482 |
STDERR_MESSAGE_HEADER_SIZE, G_PRIORITY_DEFAULT, NULL, |
| 2483 |
(GAsyncReadyCallback) have_stderr_header, NULL); |
| 2484 |
|
| 2485 |
/* Check every 1 seconds, if we have to cleanup ANNOUNCE or pending syncs message */ |
| 2486 |
cleanup_source = g_timeout_source_new_seconds (1); |
| 2487 |
g_source_set_priority (cleanup_source, G_PRIORITY_DEFAULT); |
| 2488 |
g_source_set_callback (cleanup_source, (GSourceFunc) cleanup_cb, NULL, NULL); |
| 2489 |
g_source_attach (cleanup_source, main_context); |
| 2490 |
g_source_unref (cleanup_source); |
| 2491 |
|
| 2492 |
g_main_loop_run (main_loop); |
| 2493 |
GST_DEBUG ("Stopped PTP helper loop"); |
| 2494 |
|
| 2495 |
g_main_context_pop_thread_default (main_context); |
| 2496 |
|
| 2497 |
g_mutex_lock (&ptp_lock); |
| 2498 |
ptp_clock_id.clock_identity = GST_PTP_CLOCK_ID_NONE; |
| 2499 |
ptp_clock_id.port_number = 0; |
| 2500 |
initted = FALSE; |
| 2501 |
g_cond_signal (&ptp_cond); |
| 2502 |
g_mutex_unlock (&ptp_lock); |
| 2503 |
|
| 2504 |
return NULL; |
| 2505 |
} |
| 2506 |
|
| 2507 |
/** |
| 2508 |
* gst_ptp_is_supported: |
| 2509 |
* |
| 2510 |
* Check if PTP clocks are generally supported on this system, and if previous |
| 2511 |
* initializations did not fail. |
| 2512 |
* |
| 2513 |
* Returns: %TRUE if PTP clocks are generally supported on this system, and |
| 2514 |
* previous initializations did not fail. |
| 2515 |
* |
| 2516 |
* Since: 1.6 |
| 2517 |
*/ |
| 2518 |
gboolean |
| 2519 |
gst_ptp_is_supported (void) |
| 2520 |
{ |
| 2521 |
return supported; |
| 2522 |
} |
| 2523 |
|
| 2524 |
/** |
| 2525 |
* gst_ptp_is_initialized: |
| 2526 |
* |
| 2527 |
* Check if the GStreamer PTP clock subsystem is initialized. |
| 2528 |
* |
| 2529 |
* Returns: %TRUE if the GStreamer PTP clock subsystem is initialized. |
| 2530 |
* |
| 2531 |
* Since: 1.6 |
| 2532 |
*/ |
| 2533 |
gboolean |
| 2534 |
gst_ptp_is_initialized (void) |
| 2535 |
{ |
| 2536 |
return initted; |
| 2537 |
} |
| 2538 |
|
| 2539 |
#if defined(G_OS_WIN32) && !defined(GST_STATIC_COMPILATION) |
| 2540 |
/* Note: DllMain is only called when DLLs are loaded or unloaded, so this will |
| 2541 |
* never be called if libgstnet-1.0 is linked statically. Do not add any code |
| 2542 |
* here to, say, initialize variables or set things up since that will only |
| 2543 |
* happen for dynamically-built GStreamer. |
| 2544 |
*/ |
| 2545 |
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); |
| 2546 |
BOOL WINAPI |
| 2547 |
DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 2548 |
{ |
| 2549 |
if (fdwReason == DLL_PROCESS_ATTACH) |
| 2550 |
gstnet_dll_handle = (HMODULE) hinstDLL; |
| 2551 |
return TRUE; |
| 2552 |
} |
| 2553 |
|
| 2554 |
#endif |
| 2555 |
|
| 2556 |
static char * |
| 2557 |
get_relocated_libgstnet (void) |
| 2558 |
{ |
| 2559 |
char *dir = NULL; |
| 2560 |
|
| 2561 |
#ifdef G_OS_WIN32 |
| 2562 |
{ |
| 2563 |
char *base_dir; |
| 2564 |
|
| 2565 |
GST_DEBUG ("attempting to retrieve libgstnet-1.0 location using " |
| 2566 |
"Win32-specific method"); |
| 2567 |
|
| 2568 |
base_dir = |
| 2569 |
g_win32_get_package_installation_directory_of_module |
| 2570 |
(gstnet_dll_handle); |
| 2571 |
if (!base_dir) |
| 2572 |
return NULL; |
| 2573 |
|
| 2574 |
dir = g_build_filename (base_dir, GST_PLUGIN_SUBDIR, NULL); |
| 2575 |
GST_DEBUG ("using DLL dir %s", dir); |
| 2576 |
|
| 2577 |
g_free (base_dir); |
| 2578 |
} |
| 2579 |
#elif defined(HAVE_DLADDR) |
| 2580 |
{ |
| 2581 |
Dl_info info; |
| 2582 |
char *real_fname = NULL; |
| 2583 |
long path_max = 0; |
| 2584 |
|
| 2585 |
GST_DEBUG ("attempting to retrieve libgstnet-1.0 location using " |
| 2586 |
"dladdr()"); |
| 2587 |
|
| 2588 |
if (dladdr (&gst_ptp_init, &info)) { |
| 2589 |
GST_LOG ("dli_fname: %s", info.dli_fname); |
| 2590 |
|
| 2591 |
if (!info.dli_fname) { |
| 2592 |
return NULL; |
| 2593 |
} |
| 2594 |
#ifdef PATH_MAX |
| 2595 |
path_max = PATH_MAX; |
| 2596 |
#else |
| 2597 |
path_max = pathconf (info.dli_fname, _PC_PATH_MAX); |
| 2598 |
if (path_max <= 0) |
| 2599 |
path_max = 4096; |
| 2600 |
#endif |
| 2601 |
|
| 2602 |
real_fname = g_malloc (path_max); |
| 2603 |
if (realpath (info.dli_fname, real_fname)) { |
| 2604 |
dir = g_path_get_dirname (real_fname); |
| 2605 |
GST_DEBUG ("real directory location: %s", dir); |
| 2606 |
} else { |
| 2607 |
GST_ERROR ("could not canonicalize path %s: %s", info.dli_fname, |
| 2608 |
g_strerror (errno)); |
| 2609 |
dir = g_path_get_dirname (info.dli_fname); |
| 2610 |
} |
| 2611 |
g_free (real_fname); |
| 2612 |
|
| 2613 |
} else { |
| 2614 |
GST_LOG ("dladdr() failed"); |
| 2615 |
return NULL; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
#else |
| 2619 |
#warning "Unsupported platform for retrieving the current location of a shared library." |
| 2620 |
#warning "Relocatable builds will not work." |
| 2621 |
GST_WARNING ("Don't know how to retrieve the location of the shared " |
| 2622 |
"library libgstnet-" GST_API_VERSION); |
| 2623 |
#endif |
| 2624 |
|
| 2625 |
return dir; |
| 2626 |
} |
| 2627 |
|
| 2628 |
static int |
| 2629 |
count_directories (const char *filepath) |
| 2630 |
{ |
| 2631 |
int i = 0; |
| 2632 |
char *tmp; |
| 2633 |
gsize len; |
| 2634 |
|
| 2635 |
g_return_val_if_fail (!g_path_is_absolute (filepath), 0); |
| 2636 |
|
| 2637 |
tmp = g_strdup (filepath); |
| 2638 |
len = strlen (tmp); |
| 2639 |
|
| 2640 |
/* ignore UNC share paths entirely */ |
| 2641 |
if (len >= 3 && G_IS_DIR_SEPARATOR (tmp[0]) && G_IS_DIR_SEPARATOR (tmp[1]) |
| 2642 |
&& !G_IS_DIR_SEPARATOR (tmp[2])) { |
| 2643 |
GST_WARNING ("found a UNC share path, ignoring"); |
| 2644 |
g_clear_pointer (&tmp, g_free); |
| 2645 |
return 0; |
| 2646 |
} |
| 2647 |
|
| 2648 |
/* remove trailing slashes if they exist */ |
| 2649 |
while ( |
| 2650 |
/* don't remove the trailing slash for C:\. |
| 2651 |
* UNC paths are at least \\s\s */ |
| 2652 |
len > 3 && G_IS_DIR_SEPARATOR (tmp[len - 1])) { |
| 2653 |
tmp[len - 1] = '\0'; |
| 2654 |
len--; |
| 2655 |
} |
| 2656 |
|
| 2657 |
while (tmp) { |
| 2658 |
char *dirname, *basename; |
| 2659 |
len = strlen (tmp); |
| 2660 |
|
| 2661 |
if (g_strcmp0 (tmp, ".") == 0) |
| 2662 |
break; |
| 2663 |
if (g_strcmp0 (tmp, "/") == 0) |
| 2664 |
break; |
| 2665 |
|
| 2666 |
/* g_path_get_dirname() may return something of the form 'C:.', where C is |
| 2667 |
* a drive letter */ |
| 2668 |
if (len == 3 && g_ascii_isalpha (tmp[0]) && tmp[1] == ':' && tmp[2] == '.') |
| 2669 |
break; |
| 2670 |
|
| 2671 |
basename = g_path_get_basename (tmp); |
| 2672 |
dirname = g_path_get_dirname (tmp); |
| 2673 |
|
| 2674 |
if (g_strcmp0 (basename, "..") == 0) { |
| 2675 |
i--; |
| 2676 |
} else if (g_strcmp0 (basename, ".") == 0) { |
| 2677 |
/* nothing to do */ |
| 2678 |
} else { |
| 2679 |
i++; |
| 2680 |
} |
| 2681 |
|
| 2682 |
g_clear_pointer (&basename, g_free); |
| 2683 |
g_clear_pointer (&tmp, g_free); |
| 2684 |
tmp = dirname; |
| 2685 |
} |
| 2686 |
|
| 2687 |
g_clear_pointer (&tmp, g_free); |
| 2688 |
|
| 2689 |
if (i < 0) { |
| 2690 |
g_critical ("path counting resulted in a negative directory count!"); |
| 2691 |
return 0; |
| 2692 |
} |
| 2693 |
|
| 2694 |
return i; |
| 2695 |
} |
| 2696 |
|
| 2697 |
|
| 2698 |
/** |
| 2699 |
* gst_ptp_init_full: |
| 2700 |
* @config: Configuration for initializing the GStreamer PTP subsystem |
| 2701 |
* |
| 2702 |
* Initialize the GStreamer PTP subsystem and create a PTP ordinary clock in |
| 2703 |
* slave-only mode according to the @config. |
| 2704 |
* |
| 2705 |
* @config is a #GstStructure with the following optional fields: |
| 2706 |
* * #guint64 `clock-id`: The clock ID to use for the local clock. If the |
| 2707 |
* clock-id is not provided or %GST_PTP_CLOCK_ID_NONE is provided, a clock |
| 2708 |
* id is automatically generated from the MAC address of the first network |
| 2709 |
* interface. |
| 2710 |
* * #GStrv `interfaces`: The interface names to listen on for PTP packets. If |
| 2711 |
* none are provided then all compatible interfaces will be used. |
| 2712 |
* * #guint `ttl`: The TTL to use for multicast packets sent out by GStreamer. |
| 2713 |
* This defaults to 1, i.e. packets will not leave the local network. |
| 2714 |
* |
| 2715 |
* This function is automatically called by gst_ptp_clock_new() with default |
| 2716 |
* parameters if it wasn't called before. |
| 2717 |
* |
| 2718 |
* Returns: %TRUE if the GStreamer PTP clock subsystem could be initialized. |
| 2719 |
* |
| 2720 |
* Since: 1.24 |
| 2721 |
*/ |
| 2722 |
gboolean |
| 2723 |
gst_ptp_init_full (const GstStructure * config) |
| 2724 |
{ |
| 2725 |
gboolean ret; |
| 2726 |
const gchar *env; |
| 2727 |
gchar **argv = NULL; |
| 2728 |
gint argc, argc_c; |
| 2729 |
guint64 clock_id = GST_CLOCK_TIME_NONE; |
| 2730 |
const GValue *v; |
| 2731 |
gchar **interfaces = NULL; |
| 2732 |
guint ttl = 1; |
| 2733 |
GError *err = NULL; |
| 2734 |
|
| 2735 |
GST_DEBUG_CATEGORY_INIT (ptp_debug, "ptp", 0, "PTP clock"); |
| 2736 |
|
| 2737 |
g_mutex_lock (&ptp_lock); |
| 2738 |
if (!supported) { |
| 2739 |
GST_ERROR ("PTP not supported"); |
| 2740 |
ret = FALSE; |
| 2741 |
goto done; |
| 2742 |
} |
| 2743 |
|
| 2744 |
if (initted) { |
| 2745 |
GST_DEBUG ("PTP already initialized"); |
| 2746 |
ret = TRUE; |
| 2747 |
goto done; |
| 2748 |
} |
| 2749 |
|
| 2750 |
if (ptp_helper_process) { |
| 2751 |
GST_DEBUG ("PTP currently initializing"); |
| 2752 |
goto wait; |
| 2753 |
} |
| 2754 |
|
| 2755 |
if (!domain_stats_hooks_initted) { |
| 2756 |
g_hook_list_init (&domain_stats_hooks, sizeof (GHook)); |
| 2757 |
domain_stats_hooks_initted = TRUE; |
| 2758 |
} |
| 2759 |
|
| 2760 |
argc = 1; |
| 2761 |
gst_structure_get_uint64 (config, "clock-id", &clock_id); |
| 2762 |
if (clock_id != GST_PTP_CLOCK_ID_NONE) |
| 2763 |
argc += 2; |
| 2764 |
v = gst_structure_get_value (config, "interfaces"); |
| 2765 |
if (v && G_VALUE_HOLDS (v, G_TYPE_STRV)) |
| 2766 |
interfaces = g_value_get_boxed (v); |
| 2767 |
if (interfaces != NULL) |
| 2768 |
argc += 2 * g_strv_length (interfaces); |
| 2769 |
gst_structure_get_uint (config, "ttl", &ttl); |
| 2770 |
argc += 2; |
| 2771 |
|
| 2772 |
// 3 for: executable, -v and NULL |
| 2773 |
argv = g_new0 (gchar *, argc + 3); |
| 2774 |
argc_c = 0; |
| 2775 |
|
| 2776 |
/* Find the gst-ptp-helper */ |
| 2777 |
env = g_getenv ("GST_PTP_HELPER_1_0"); |
| 2778 |
if (!env) |
| 2779 |
env = g_getenv ("GST_PTP_HELPER"); |
| 2780 |
|
| 2781 |
if (env && *env != '\0') { |
| 2782 |
/* use the env-var if it is set */ |
| 2783 |
argv[argc_c++] = g_strdup (env); |
| 2784 |
} else { |
| 2785 |
char *relocated_libgstnet; |
| 2786 |
|
| 2787 |
/* use the installed version */ |
| 2788 |
GST_LOG ("Trying installed PTP helper process"); |
| 2789 |
|
| 2790 |
#define MAX_PATH_DEPTH 64 |
| 2791 |
|
| 2792 |
relocated_libgstnet = get_relocated_libgstnet (); |
| 2793 |
if (relocated_libgstnet) { |
| 2794 |
int plugin_subdir_depth = count_directories (GST_PLUGIN_SUBDIR); |
| 2795 |
|
| 2796 |
GST_DEBUG ("found libgstnet-" GST_API_VERSION " library " |
| 2797 |
"at %s", relocated_libgstnet); |
| 2798 |
|
| 2799 |
if (plugin_subdir_depth < MAX_PATH_DEPTH) { |
| 2800 |
const char *filenamev[MAX_PATH_DEPTH + 5]; |
| 2801 |
int i = 0, j; |
| 2802 |
|
| 2803 |
filenamev[i++] = relocated_libgstnet; |
| 2804 |
for (j = 0; j < plugin_subdir_depth; j++) |
| 2805 |
filenamev[i++] = ".."; |
| 2806 |
filenamev[i++] = GST_PTP_HELPER_SUBDIR; |
| 2807 |
filenamev[i++] = "gstreamer-" GST_API_VERSION; |
| 2808 |
#ifdef G_OS_WIN32 |
| 2809 |
filenamev[i++] = "gst-ptp-helper.exe"; |
| 2810 |
#else |
| 2811 |
filenamev[i++] = "gst-ptp-helper"; |
| 2812 |
#endif |
| 2813 |
filenamev[i++] = NULL; |
| 2814 |
g_assert (i <= MAX_PATH_DEPTH + 5); |
| 2815 |
|
| 2816 |
GST_DEBUG ("constructing path to system PTP helper using " |
| 2817 |
"plugin dir: \'%s\', PTP helper dir: \'%s\'", |
| 2818 |
GST_PLUGIN_SUBDIR, GST_PTP_HELPER_SUBDIR); |
| 2819 |
|
| 2820 |
argv[argc_c++] = g_build_filenamev ((char **) filenamev); |
| 2821 |
} else { |
| 2822 |
GST_WARNING ("GST_PLUGIN_SUBDIR: \'%s\' has too many path segments", |
| 2823 |
GST_PLUGIN_SUBDIR); |
| 2824 |
argv[argc_c++] = g_strdup (GST_PTP_HELPER_INSTALLED); |
| 2825 |
} |
| 2826 |
} else { |
| 2827 |
argv[argc_c++] = g_strdup (GST_PTP_HELPER_INSTALLED); |
| 2828 |
} |
| 2829 |
} |
| 2830 |
|
| 2831 |
#undef MAX_PATH_DEPTH |
| 2832 |
|
| 2833 |
GST_LOG ("Using PTP helper process: %s", argv[argc_c - 1]); |
| 2834 |
|
| 2835 |
if (clock_id != GST_PTP_CLOCK_ID_NONE) { |
| 2836 |
argv[argc_c++] = g_strdup ("-c"); |
| 2837 |
argv[argc_c++] = g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", clock_id); |
| 2838 |
GST_LOG ("Using clock ID: %s", argv[argc_c - 1]); |
| 2839 |
} |
| 2840 |
|
| 2841 |
if (interfaces != NULL) { |
| 2842 |
gchar **ptr = interfaces; |
| 2843 |
|
| 2844 |
while (*ptr) { |
| 2845 |
argv[argc_c++] = g_strdup ("-i"); |
| 2846 |
argv[argc_c++] = g_strdup (*ptr); |
| 2847 |
GST_LOG ("Using interface: %s", argv[argc_c - 1]); |
| 2848 |
ptr++; |
| 2849 |
} |
| 2850 |
} |
| 2851 |
|
| 2852 |
argv[argc_c++] = g_strdup ("--ttl"); |
| 2853 |
argv[argc_c++] = g_strdup_printf ("%u", ttl); |
| 2854 |
|
| 2855 |
/* Check if the helper process should be verbose */ |
| 2856 |
env = g_getenv ("GST_PTP_HELPER_VERBOSE"); |
| 2857 |
if (env && g_ascii_strcasecmp (env, "no") != 0) { |
| 2858 |
argv[argc_c++] = g_strdup ("-v"); |
| 2859 |
} |
| 2860 |
|
| 2861 |
ptp_helper_process = |
| 2862 |
g_subprocess_newv ((const gchar * const *) argv, |
| 2863 |
G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE | |
| 2864 |
G_SUBPROCESS_FLAGS_STDERR_PIPE, &err); |
| 2865 |
if (!ptp_helper_process) { |
| 2866 |
GST_ERROR ("Failed to start ptp helper process: %s", err->message); |
| 2867 |
g_clear_error (&err); |
| 2868 |
ret = FALSE; |
| 2869 |
supported = FALSE; |
| 2870 |
goto done; |
| 2871 |
} |
| 2872 |
|
| 2873 |
stdin_pipe = g_subprocess_get_stdin_pipe (ptp_helper_process); |
| 2874 |
if (stdin_pipe) |
| 2875 |
g_object_ref (stdin_pipe); |
| 2876 |
stdout_pipe = g_subprocess_get_stdout_pipe (ptp_helper_process); |
| 2877 |
if (stdout_pipe) |
| 2878 |
g_object_ref (stdout_pipe); |
| 2879 |
stderr_pipe = g_subprocess_get_stderr_pipe (ptp_helper_process); |
| 2880 |
if (stderr_pipe) |
| 2881 |
g_object_ref (stderr_pipe); |
| 2882 |
if (!stdin_pipe || !stdout_pipe || !stderr_pipe) { |
| 2883 |
GST_ERROR ("Failed to get ptp helper process pipes"); |
| 2884 |
ret = FALSE; |
| 2885 |
supported = FALSE; |
| 2886 |
goto done; |
| 2887 |
} |
| 2888 |
|
| 2889 |
delay_req_rand = g_rand_new (); |
| 2890 |
observation_system_clock = |
| 2891 |
g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "ptp-observation-clock", |
| 2892 |
NULL); |
| 2893 |
gst_object_ref_sink (observation_system_clock); |
| 2894 |
|
| 2895 |
main_context = g_main_context_new (); |
| 2896 |
main_loop = g_main_loop_new (main_context, FALSE); |
| 2897 |
|
| 2898 |
ptp_helper_thread = |
| 2899 |
g_thread_try_new ("ptp-helper-thread", ptp_helper_main, NULL, &err); |
| 2900 |
if (!ptp_helper_thread) { |
| 2901 |
GST_ERROR ("Failed to start PTP helper thread: %s", err->message); |
| 2902 |
g_clear_error (&err); |
| 2903 |
ret = FALSE; |
| 2904 |
goto done; |
| 2905 |
} |
| 2906 |
|
| 2907 |
initted = TRUE; |
| 2908 |
|
| 2909 |
wait: |
| 2910 |
GST_DEBUG ("Waiting for PTP to be initialized"); |
| 2911 |
|
| 2912 |
while (ptp_clock_id.clock_identity == GST_PTP_CLOCK_ID_NONE && initted) |
| 2913 |
g_cond_wait (&ptp_cond, &ptp_lock); |
| 2914 |
|
| 2915 |
ret = initted; |
| 2916 |
if (ret) { |
| 2917 |
GST_DEBUG ("Initialized and got clock id 0x%016" G_GINT64_MODIFIER "x %u", |
| 2918 |
ptp_clock_id.clock_identity, ptp_clock_id.port_number); |
| 2919 |
} else { |
| 2920 |
GST_ERROR ("Failed to initialize"); |
| 2921 |
supported = FALSE; |
| 2922 |
} |
| 2923 |
|
| 2924 |
done: |
| 2925 |
g_strfreev (argv); |
| 2926 |
|
| 2927 |
if (!ret) { |
| 2928 |
if (ptp_helper_process) { |
| 2929 |
g_clear_object (&stdin_pipe); |
| 2930 |
g_clear_object (&stdout_pipe); |
| 2931 |
g_clear_object (&stderr_pipe); |
| 2932 |
g_subprocess_force_exit (ptp_helper_process); |
| 2933 |
g_clear_object (&ptp_helper_process); |
| 2934 |
} |
| 2935 |
|
| 2936 |
if (main_loop && ptp_helper_thread) { |
| 2937 |
g_main_loop_quit (main_loop); |
| 2938 |
g_thread_join (ptp_helper_thread); |
| 2939 |
} |
| 2940 |
ptp_helper_thread = NULL; |
| 2941 |
if (main_loop) |
| 2942 |
g_main_loop_unref (main_loop); |
| 2943 |
main_loop = NULL; |
| 2944 |
if (main_context) |
| 2945 |
g_main_context_unref (main_context); |
| 2946 |
main_context = NULL; |
| 2947 |
|
| 2948 |
if (delay_req_rand) |
| 2949 |
g_rand_free (delay_req_rand); |
| 2950 |
delay_req_rand = NULL; |
| 2951 |
|
| 2952 |
if (observation_system_clock) |
| 2953 |
gst_object_unref (observation_system_clock); |
| 2954 |
observation_system_clock = NULL; |
| 2955 |
} |
| 2956 |
|
| 2957 |
g_mutex_unlock (&ptp_lock); |
| 2958 |
|
| 2959 |
return ret; |
| 2960 |
} |
| 2961 |
|
| 2962 |
/** |
| 2963 |
* gst_ptp_init: |
| 2964 |
* @clock_id: PTP clock id of this process' clock or %GST_PTP_CLOCK_ID_NONE |
| 2965 |
* @interfaces: (transfer none) (array zero-terminated=1) (allow-none): network interfaces to run the clock on |
| 2966 |
* |
| 2967 |
* Initialize the GStreamer PTP subsystem and create a PTP ordinary clock in |
| 2968 |
* slave-only mode for all domains on the given @interfaces with the |
| 2969 |
* given @clock_id. |
| 2970 |
* |
| 2971 |
* If @clock_id is %GST_PTP_CLOCK_ID_NONE, a clock id is automatically |
| 2972 |
* generated from the MAC address of the first network interface. |
| 2973 |
* |
| 2974 |
* This function is automatically called by gst_ptp_clock_new() with default |
| 2975 |
* parameters if it wasn't called before. |
| 2976 |
* |
| 2977 |
* Returns: %TRUE if the GStreamer PTP clock subsystem could be initialized. |
| 2978 |
* |
| 2979 |
* Since: 1.6 |
| 2980 |
*/ |
| 2981 |
gboolean |
| 2982 |
gst_ptp_init (guint64 clock_id, gchar ** interfaces) |
| 2983 |
{ |
| 2984 |
GstStructure *config; |
| 2985 |
gboolean ret; |
| 2986 |
|
| 2987 |
config = gst_structure_new ("config/ptp", |
| 2988 |
"clock-id", G_TYPE_UINT64, clock_id, |
| 2989 |
"interfaces", G_TYPE_STRV, interfaces, NULL); |
| 2990 |
ret = gst_ptp_init_full (config); |
| 2991 |
gst_structure_free (config); |
| 2992 |
|
| 2993 |
return ret; |
| 2994 |
} |
| 2995 |
|
| 2996 |
/** |
| 2997 |
* gst_ptp_deinit: |
| 2998 |
* |
| 2999 |
* Deinitialize the GStreamer PTP subsystem and stop the PTP clock. If there |
| 3000 |
* are any remaining GstPtpClock instances, they won't be further synchronized |
| 3001 |
* to the PTP network clock. |
| 3002 |
* |
| 3003 |
* Since: 1.6 |
| 3004 |
*/ |
| 3005 |
void |
| 3006 |
gst_ptp_deinit (void) |
| 3007 |
{ |
| 3008 |
GList *l, *m; |
| 3009 |
|
| 3010 |
g_mutex_lock (&ptp_lock); |
| 3011 |
|
| 3012 |
if (ptp_helper_process) { |
| 3013 |
g_clear_object (&stdin_pipe); |
| 3014 |
g_clear_object (&stdout_pipe); |
| 3015 |
g_clear_object (&stderr_pipe); |
| 3016 |
g_subprocess_force_exit (ptp_helper_process); |
| 3017 |
g_clear_object (&ptp_helper_process); |
| 3018 |
} |
| 3019 |
|
| 3020 |
if (main_loop && ptp_helper_thread) { |
| 3021 |
GThread *tmp = ptp_helper_thread; |
| 3022 |
ptp_helper_thread = NULL; |
| 3023 |
g_mutex_unlock (&ptp_lock); |
| 3024 |
g_main_loop_quit (main_loop); |
| 3025 |
g_thread_join (tmp); |
| 3026 |
g_mutex_lock (&ptp_lock); |
| 3027 |
} |
| 3028 |
if (main_loop) |
| 3029 |
g_main_loop_unref (main_loop); |
| 3030 |
main_loop = NULL; |
| 3031 |
if (main_context) |
| 3032 |
g_main_context_unref (main_context); |
| 3033 |
main_context = NULL; |
| 3034 |
|
| 3035 |
if (delay_req_rand) |
| 3036 |
g_rand_free (delay_req_rand); |
| 3037 |
delay_req_rand = NULL; |
| 3038 |
if (observation_system_clock) |
| 3039 |
gst_object_unref (observation_system_clock); |
| 3040 |
observation_system_clock = NULL; |
| 3041 |
|
| 3042 |
for (l = domain_data; l; l = l->next) { |
| 3043 |
PtpDomainData *domain = l->data; |
| 3044 |
|
| 3045 |
for (m = domain->announce_senders; m; m = m->next) { |
| 3046 |
PtpAnnounceSender *sender = m->data; |
| 3047 |
|
| 3048 |
g_queue_foreach (&sender->announce_messages, (GFunc) g_free, NULL); |
| 3049 |
g_queue_clear (&sender->announce_messages); |
| 3050 |
g_free (sender); |
| 3051 |
} |
| 3052 |
g_list_free (domain->announce_senders); |
| 3053 |
|
| 3054 |
g_queue_foreach (&domain->pending_syncs, (GFunc) ptp_pending_sync_free, |
| 3055 |
NULL); |
| 3056 |
g_queue_clear (&domain->pending_syncs); |
| 3057 |
gst_object_unref (domain->domain_clock); |
| 3058 |
g_free (domain); |
| 3059 |
} |
| 3060 |
g_list_free (domain_data); |
| 3061 |
domain_data = NULL; |
| 3062 |
// The domain_clocks list is same as domain_data |
| 3063 |
// and the elements are freed above already |
| 3064 |
g_list_free (domain_clocks); |
| 3065 |
domain_clocks = NULL; |
| 3066 |
|
| 3067 |
ptp_clock_id.clock_identity = GST_PTP_CLOCK_ID_NONE; |
| 3068 |
ptp_clock_id.port_number = 0; |
| 3069 |
|
| 3070 |
initted = FALSE; |
| 3071 |
|
| 3072 |
g_mutex_unlock (&ptp_lock); |
| 3073 |
} |
| 3074 |
|
| 3075 |
#define DEFAULT_DOMAIN 0 |
| 3076 |
|
| 3077 |
enum |
| 3078 |
{ |
| 3079 |
PROP_0, |
| 3080 |
PROP_DOMAIN, |
| 3081 |
PROP_INTERNAL_CLOCK, |
| 3082 |
PROP_MASTER_CLOCK_ID, |
| 3083 |
PROP_GRANDMASTER_CLOCK_ID |
| 3084 |
}; |
| 3085 |
|
| 3086 |
struct _GstPtpClockPrivate |
| 3087 |
{ |
| 3088 |
guint domain; |
| 3089 |
GstClock *domain_clock; |
| 3090 |
gulong domain_stats_id; |
| 3091 |
}; |
| 3092 |
|
| 3093 |
#define gst_ptp_clock_parent_class parent_class |
| 3094 |
G_DEFINE_TYPE_WITH_PRIVATE (GstPtpClock, gst_ptp_clock, GST_TYPE_SYSTEM_CLOCK); |
| 3095 |
|
| 3096 |
static void gst_ptp_clock_set_property (GObject * object, guint prop_id, |
| 3097 |
const GValue * value, GParamSpec * pspec); |
| 3098 |
static void gst_ptp_clock_get_property (GObject * object, guint prop_id, |
| 3099 |
GValue * value, GParamSpec * pspec); |
| 3100 |
static void gst_ptp_clock_finalize (GObject * object); |
| 3101 |
|
| 3102 |
static GstClockTime gst_ptp_clock_get_internal_time (GstClock * clock); |
| 3103 |
|
| 3104 |
static void |
| 3105 |
gst_ptp_clock_class_init (GstPtpClockClass * klass) |
| 3106 |
{ |
| 3107 |
GObjectClass *gobject_class; |
| 3108 |
GstClockClass *clock_class; |
| 3109 |
|
| 3110 |
gobject_class = G_OBJECT_CLASS (klass); |
| 3111 |
clock_class = GST_CLOCK_CLASS (klass); |
| 3112 |
|
| 3113 |
gobject_class->finalize = gst_ptp_clock_finalize; |
| 3114 |
gobject_class->get_property = gst_ptp_clock_get_property; |
| 3115 |
gobject_class->set_property = gst_ptp_clock_set_property; |
| 3116 |
|
| 3117 |
g_object_class_install_property (gobject_class, PROP_DOMAIN, |
| 3118 |
g_param_spec_uint ("domain", "Domain", |
| 3119 |
"The PTP domain", 0, G_MAXUINT8, |
| 3120 |
DEFAULT_DOMAIN, |
| 3121 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 3122 |
|
| 3123 |
g_object_class_install_property (gobject_class, PROP_INTERNAL_CLOCK, |
| 3124 |
g_param_spec_object ("internal-clock", "Internal Clock", |
| 3125 |
"Internal clock", GST_TYPE_CLOCK, |
| 3126 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 3127 |
|
| 3128 |
g_object_class_install_property (gobject_class, PROP_MASTER_CLOCK_ID, |
| 3129 |
g_param_spec_uint64 ("master-clock-id", "Master Clock ID", |
| 3130 |
"Master Clock ID", 0, G_MAXUINT64, 0, |
| 3131 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 3132 |
|
| 3133 |
g_object_class_install_property (gobject_class, PROP_GRANDMASTER_CLOCK_ID, |
| 3134 |
g_param_spec_uint64 ("grandmaster-clock-id", "Grand Master Clock ID", |
| 3135 |
"Grand Master Clock ID", 0, G_MAXUINT64, 0, |
| 3136 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 3137 |
|
| 3138 |
clock_class->get_internal_time = gst_ptp_clock_get_internal_time; |
| 3139 |
} |
| 3140 |
|
| 3141 |
static void |
| 3142 |
gst_ptp_clock_init (GstPtpClock * self) |
| 3143 |
{ |
| 3144 |
GstPtpClockPrivate *priv; |
| 3145 |
|
| 3146 |
self->priv = priv = gst_ptp_clock_get_instance_private (self); |
| 3147 |
|
| 3148 |
GST_OBJECT_FLAG_SET (self, GST_CLOCK_FLAG_CAN_SET_MASTER); |
| 3149 |
GST_OBJECT_FLAG_SET (self, GST_CLOCK_FLAG_NEEDS_STARTUP_SYNC); |
| 3150 |
|
| 3151 |
priv->domain = DEFAULT_DOMAIN; |
| 3152 |
} |
| 3153 |
|
| 3154 |
static gboolean |
| 3155 |
gst_ptp_clock_ensure_domain_clock (GstPtpClock * self) |
| 3156 |
{ |
| 3157 |
gboolean got_clock = TRUE; |
| 3158 |
|
| 3159 |
if (G_UNLIKELY (!self->priv->domain_clock)) { |
| 3160 |
g_mutex_lock (&domain_clocks_lock); |
| 3161 |
if (!self->priv->domain_clock) { |
| 3162 |
GList *l; |
| 3163 |
|
| 3164 |
got_clock = FALSE; |
| 3165 |
|
| 3166 |
for (l = domain_clocks; l; l = l->next) { |
| 3167 |
PtpDomainData *clock_data = l->data; |
| 3168 |
|
| 3169 |
if (clock_data->domain == self->priv->domain && |
| 3170 |
clock_data->have_master_clock && clock_data->last_ptp_time != 0) { |
| 3171 |
GST_DEBUG ("Switching domain clock on domain %d", clock_data->domain); |
| 3172 |
self->priv->domain_clock = clock_data->domain_clock; |
| 3173 |
got_clock = TRUE; |
| 3174 |
break; |
| 3175 |
} |
| 3176 |
} |
| 3177 |
} |
| 3178 |
g_mutex_unlock (&domain_clocks_lock); |
| 3179 |
if (got_clock) { |
| 3180 |
g_object_notify (G_OBJECT (self), "internal-clock"); |
| 3181 |
gst_clock_set_synced (GST_CLOCK (self), TRUE); |
| 3182 |
} |
| 3183 |
} |
| 3184 |
|
| 3185 |
return got_clock; |
| 3186 |
} |
| 3187 |
|
| 3188 |
static gboolean |
| 3189 |
gst_ptp_clock_stats_callback (guint8 domain, const GstStructure * stats, |
| 3190 |
gpointer user_data) |
| 3191 |
{ |
| 3192 |
GstPtpClock *self = user_data; |
| 3193 |
|
| 3194 |
if (domain != self->priv->domain |
| 3195 |
|| !gst_structure_has_name (stats, GST_PTP_STATISTICS_TIME_UPDATED)) |
| 3196 |
return TRUE; |
| 3197 |
|
| 3198 |
/* Let's set our internal clock */ |
| 3199 |
if (!gst_ptp_clock_ensure_domain_clock (self)) |
| 3200 |
return TRUE; |
| 3201 |
|
| 3202 |
self->priv->domain_stats_id = 0; |
| 3203 |
|
| 3204 |
return FALSE; |
| 3205 |
} |
| 3206 |
|
| 3207 |
static void |
| 3208 |
gst_ptp_clock_set_property (GObject * object, guint prop_id, |
| 3209 |
const GValue * value, GParamSpec * pspec) |
| 3210 |
{ |
| 3211 |
GstPtpClock *self = GST_PTP_CLOCK (object); |
| 3212 |
|
| 3213 |
switch (prop_id) { |
| 3214 |
case PROP_DOMAIN: |
| 3215 |
self->priv->domain = g_value_get_uint (value); |
| 3216 |
gst_ptp_clock_ensure_domain_clock (self); |
| 3217 |
if (!self->priv->domain_clock) |
| 3218 |
self->priv->domain_stats_id = |
| 3219 |
gst_ptp_statistics_callback_add (gst_ptp_clock_stats_callback, self, |
| 3220 |
NULL); |
| 3221 |
break; |
| 3222 |
default: |
| 3223 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 3224 |
break; |
| 3225 |
} |
| 3226 |
} |
| 3227 |
|
| 3228 |
static void |
| 3229 |
gst_ptp_clock_get_property (GObject * object, guint prop_id, |
| 3230 |
GValue * value, GParamSpec * pspec) |
| 3231 |
{ |
| 3232 |
GstPtpClock *self = GST_PTP_CLOCK (object); |
| 3233 |
|
| 3234 |
switch (prop_id) { |
| 3235 |
case PROP_DOMAIN: |
| 3236 |
g_value_set_uint (value, self->priv->domain); |
| 3237 |
break; |
| 3238 |
case PROP_INTERNAL_CLOCK: |
| 3239 |
gst_ptp_clock_ensure_domain_clock (self); |
| 3240 |
g_value_set_object (value, self->priv->domain_clock); |
| 3241 |
break; |
| 3242 |
case PROP_MASTER_CLOCK_ID: |
| 3243 |
case PROP_GRANDMASTER_CLOCK_ID:{ |
| 3244 |
GList *l; |
| 3245 |
|
| 3246 |
g_mutex_lock (&domain_clocks_lock); |
| 3247 |
g_value_set_uint64 (value, 0); |
| 3248 |
|
| 3249 |
for (l = domain_clocks; l; l = l->next) { |
| 3250 |
PtpDomainData *clock_data = l->data; |
| 3251 |
|
| 3252 |
if (clock_data->domain == self->priv->domain) { |
| 3253 |
if (prop_id == PROP_MASTER_CLOCK_ID) |
| 3254 |
g_value_set_uint64 (value, |
| 3255 |
clock_data->master_clock_identity.clock_identity); |
| 3256 |
else |
| 3257 |
g_value_set_uint64 (value, clock_data->grandmaster_identity); |
| 3258 |
break; |
| 3259 |
} |
| 3260 |
} |
| 3261 |
g_mutex_unlock (&domain_clocks_lock); |
| 3262 |
break; |
| 3263 |
} |
| 3264 |
default: |
| 3265 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 3266 |
break; |
| 3267 |
} |
| 3268 |
} |
| 3269 |
|
| 3270 |
static void |
| 3271 |
gst_ptp_clock_finalize (GObject * object) |
| 3272 |
{ |
| 3273 |
GstPtpClock *self = GST_PTP_CLOCK (object); |
| 3274 |
|
| 3275 |
if (self->priv->domain_stats_id) |
| 3276 |
gst_ptp_statistics_callback_remove (self->priv->domain_stats_id); |
| 3277 |
|
| 3278 |
G_OBJECT_CLASS (gst_ptp_clock_parent_class)->finalize (object); |
| 3279 |
} |
| 3280 |
|
| 3281 |
static GstClockTime |
| 3282 |
gst_ptp_clock_get_internal_time (GstClock * clock) |
| 3283 |
{ |
| 3284 |
GstPtpClock *self = GST_PTP_CLOCK (clock); |
| 3285 |
|
| 3286 |
gst_ptp_clock_ensure_domain_clock (self); |
| 3287 |
|
| 3288 |
if (!self->priv->domain_clock) { |
| 3289 |
GST_ERROR_OBJECT (self, "Domain %u has no clock yet and is not synced", |
| 3290 |
self->priv->domain); |
| 3291 |
return GST_CLOCK_TIME_NONE; |
| 3292 |
} |
| 3293 |
|
| 3294 |
return gst_clock_get_time (self->priv->domain_clock); |
| 3295 |
} |
| 3296 |
|
| 3297 |
/** |
| 3298 |
* gst_ptp_clock_new: |
| 3299 |
* @name: (nullable): Name of the clock |
| 3300 |
* @domain: PTP domain |
| 3301 |
* |
| 3302 |
* Creates a new PTP clock instance that exports the PTP time of the master |
| 3303 |
* clock in @domain. This clock can be slaved to other clocks as needed. |
| 3304 |
* |
| 3305 |
* If gst_ptp_init() was not called before, this will call gst_ptp_init() with |
| 3306 |
* default parameters. |
| 3307 |
* |
| 3308 |
* This clock only returns valid timestamps after it received the first |
| 3309 |
* times from the PTP master clock on the network. Once this happens the |
| 3310 |
* GstPtpClock::internal-clock property will become non-NULL. You can |
| 3311 |
* check this with gst_clock_wait_for_sync(), the GstClock::synced signal and |
| 3312 |
* gst_clock_is_synced(). |
| 3313 |
* |
| 3314 |
* Returns: (transfer full) (nullable): A new #GstClock |
| 3315 |
* |
| 3316 |
* Since: 1.6 |
| 3317 |
*/ |
| 3318 |
GstClock * |
| 3319 |
gst_ptp_clock_new (const gchar * name, guint domain) |
| 3320 |
{ |
| 3321 |
GstClock *clock; |
| 3322 |
|
| 3323 |
g_return_val_if_fail (domain <= G_MAXUINT8, NULL); |
| 3324 |
|
| 3325 |
if (!initted && !gst_ptp_init (GST_PTP_CLOCK_ID_NONE, NULL)) { |
| 3326 |
GST_ERROR ("Failed to initialize PTP"); |
| 3327 |
return NULL; |
| 3328 |
} |
| 3329 |
|
| 3330 |
clock = g_object_new (GST_TYPE_PTP_CLOCK, "name", name, "domain", domain, |
| 3331 |
NULL); |
| 3332 |
|
| 3333 |
/* Clear floating flag */ |
| 3334 |
gst_object_ref_sink (clock); |
| 3335 |
|
| 3336 |
return clock; |
| 3337 |
} |
| 3338 |
|
| 3339 |
typedef struct |
| 3340 |
{ |
| 3341 |
guint8 domain; |
| 3342 |
const GstStructure *stats; |
| 3343 |
} DomainStatsMarshalData; |
| 3344 |
|
| 3345 |
static void |
| 3346 |
domain_stats_marshaller (GHook * hook, DomainStatsMarshalData * data) |
| 3347 |
{ |
| 3348 |
GstPtpStatisticsCallback callback = (GstPtpStatisticsCallback) hook->func; |
| 3349 |
|
| 3350 |
if (!callback (data->domain, data->stats, hook->data)) |
| 3351 |
g_hook_destroy (&domain_stats_hooks, hook->hook_id); |
| 3352 |
} |
| 3353 |
|
| 3354 |
static void |
| 3355 |
emit_ptp_statistics (guint8 domain, const GstStructure * stats) |
| 3356 |
{ |
| 3357 |
DomainStatsMarshalData data = { domain, stats }; |
| 3358 |
|
| 3359 |
g_mutex_lock (&ptp_lock); |
| 3360 |
g_hook_list_marshal (&domain_stats_hooks, TRUE, |
| 3361 |
(GHookMarshaller) domain_stats_marshaller, &data); |
| 3362 |
g_mutex_unlock (&ptp_lock); |
| 3363 |
} |
| 3364 |
|
| 3365 |
/** |
| 3366 |
* gst_ptp_statistics_callback_add: |
| 3367 |
* @callback: GstPtpStatisticsCallback to call |
| 3368 |
* @user_data: Data to pass to the callback |
| 3369 |
* @destroy_data: GDestroyNotify to destroy the data |
| 3370 |
* |
| 3371 |
* Installs a new statistics callback for gathering PTP statistics. See |
| 3372 |
* GstPtpStatisticsCallback for a list of statistics that are provided. |
| 3373 |
* |
| 3374 |
* Returns: Id for the callback that can be passed to |
| 3375 |
* gst_ptp_statistics_callback_remove() |
| 3376 |
* |
| 3377 |
* Since: 1.6 |
| 3378 |
*/ |
| 3379 |
gulong |
| 3380 |
gst_ptp_statistics_callback_add (GstPtpStatisticsCallback callback, |
| 3381 |
gpointer user_data, GDestroyNotify destroy_data) |
| 3382 |
{ |
| 3383 |
GHook *hook; |
| 3384 |
|
| 3385 |
g_mutex_lock (&ptp_lock); |
| 3386 |
|
| 3387 |
if (!domain_stats_hooks_initted) { |
| 3388 |
g_hook_list_init (&domain_stats_hooks, sizeof (GHook)); |
| 3389 |
domain_stats_hooks_initted = TRUE; |
| 3390 |
} |
| 3391 |
|
| 3392 |
hook = g_hook_alloc (&domain_stats_hooks); |
| 3393 |
hook->func = callback; |
| 3394 |
hook->data = user_data; |
| 3395 |
hook->destroy = destroy_data; |
| 3396 |
g_hook_prepend (&domain_stats_hooks, hook); |
| 3397 |
g_atomic_int_add (&domain_stats_n_hooks, 1); |
| 3398 |
|
| 3399 |
g_mutex_unlock (&ptp_lock); |
| 3400 |
|
| 3401 |
return hook->hook_id; |
| 3402 |
} |
| 3403 |
|
| 3404 |
/** |
| 3405 |
* gst_ptp_statistics_callback_remove: |
| 3406 |
* @id: Callback id to remove |
| 3407 |
* |
| 3408 |
* Removes a PTP statistics callback that was previously added with |
| 3409 |
* gst_ptp_statistics_callback_add(). |
| 3410 |
* |
| 3411 |
* Since: 1.6 |
| 3412 |
*/ |
| 3413 |
void |
| 3414 |
gst_ptp_statistics_callback_remove (gulong id) |
| 3415 |
{ |
| 3416 |
g_mutex_lock (&ptp_lock); |
| 3417 |
if (g_hook_destroy (&domain_stats_hooks, id)) |
| 3418 |
g_atomic_int_add (&domain_stats_n_hooks, -1); |
| 3419 |
g_mutex_unlock (&ptp_lock); |
| 3420 |
} |