/*****************************************************************************
- * netsync.c: synchronisation between several network clients.
+ * netsync.c: synchronization between several network clients.
*****************************************************************************
* Copyright (C) 2004-2009 the VideoLAN team
* $Id$
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include <assert.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_input.h>
#include <vlc_playlist.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
#include <sys/types.h>
+#include <unistd.h>
#ifdef HAVE_POLL
# include <poll.h>
#endif
static void Close(vlc_object_t *);
#define NETSYNC_TEXT N_("Network master clock")
-#define NETSYNC_LONGTEXT N_("When set then " \
- "this vlc instance shall dictate its clock for synchronisation" \
- "over clients listening on the masters network ip address")
+#define NETSYNC_LONGTEXT N_("When set, " \
+ "this VLC instance will act as the master clock for synchronization " \
+ "for clients listening")
#define MIP_TEXT N_("Master server ip address")
#define MIP_LONGTEXT N_("The IP address of " \
- "the network master clock to use for clock synchronisation.")
+ "the network master clock to use for clock synchronization.")
#define NETSYNC_TIMEOUT_TEXT N_("UDP timeout (in ms)")
-#define NETSYNC_TIMEOUT_LONGTEXT N_("Amount of time (in ms) " \
- "to wait before aborting network reception of data.")
+#define NETSYNC_TIMEOUT_LONGTEXT N_("Length of time (in ms) " \
+ "until aborting data reception.")
vlc_module_begin()
set_shortname(N_("Network Sync"))
- set_description(N_("Network synchronisation"))
+ set_description(N_("Network synchronization"))
set_category(CAT_ADVANCED)
set_subcategory(SUBCAT_ADVANCED_MISC)
- add_bool("netsync-master", false, NULL,
+ add_bool("netsync-master", false,
NETSYNC_TEXT, NETSYNC_LONGTEXT, true)
- add_string("netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
+ add_string("netsync-master-ip", NULL, MIP_TEXT, MIP_LONGTEXT,
true)
- add_integer("netsync-timeout", 500, NULL,
+ add_integer("netsync-timeout", 500,
NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true)
set_capability("interface", 0)
/*****************************************************************************
* Local prototypes
*****************************************************************************/
-static void Run(intf_thread_t *intf);
+struct intf_sys_t {
+ int fd;
+ int timeout;
+ bool is_master;
+ playlist_t *playlist;
+
+ /* */
+ input_thread_t *input;
+ vlc_thread_t thread;
+};
+
+static int PlaylistEvent(vlc_object_t *, char const *cmd,
+ vlc_value_t oldval, vlc_value_t newval, void *data);
/*****************************************************************************
* Activate: initialize and create stuff
static int Open(vlc_object_t *object)
{
intf_thread_t *intf = (intf_thread_t*)object;
+ intf_sys_t *sys;
int fd;
if (!var_InheritBool(intf, "netsync-master")) {
}
fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1);
free(psz_master);
- }
- else
+ } else {
fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT);
+ }
if (fd == -1) {
msg_Err(intf, "Netsync socket failure");
return VLC_EGENERIC;
}
- intf->p_sys = (void *)(intptr_t)fd;
- intf->pf_run = Run;
+ intf->p_sys = sys = malloc(sizeof(*sys));
+ if (!sys) {
+ net_Close(fd);
+ return VLC_ENOMEM;
+ }
+
+ sys->fd = fd;
+ sys->is_master = var_InheritBool(intf, "netsync-master");
+ sys->timeout = var_InheritInteger(intf, "netsync-timeout");
+ if (sys->timeout < 500)
+ sys->timeout = 500;
+ sys->playlist = pl_Get(intf);
+ sys->input = NULL;
+
+ var_AddCallback(sys->playlist, "input-current", PlaylistEvent, intf);
return VLC_SUCCESS;
}
void Close(vlc_object_t *object)
{
intf_thread_t *intf = (intf_thread_t*)object;
+ intf_sys_t *sys = intf->p_sys;
+
+ var_DelCallback(sys->playlist, "input-current", PlaylistEvent, intf);
+
+ if (sys->input != NULL) {
+ vlc_cancel(sys->thread);
+ vlc_join(sys->thread, NULL);
+ }
- net_Close((intptr_t)intf->p_sys);
+ net_Close(sys->fd);
+ free(sys);
}
-/*****************************************************************************
- * Run: interface thread
- *****************************************************************************/
-static void Run(intf_thread_t *intf)
+static mtime_t GetPcrSystem(input_thread_t *input)
{
-#define MAX_MSG_LENGTH (2 * sizeof(int64_t))
int canc = vlc_savecancel();
- input_thread_t *input = NULL;
- char data[MAX_MSG_LENGTH];
- int fd = (intptr_t)intf->p_sys;
-
- playlist_t *playlist = pl_Hold(intf);
- int timeout = var_InheritInteger(intf, "netsync-timeout");
- if (timeout < 500)
- timeout = 500;
- bool is_master = var_InheritBool(intf, "netsync-master");
-
- /* High priority thread */
- vlc_thread_set_priority(intf, VLC_THREAD_PRIORITY_INPUT);
-
- while (vlc_object_alive(intf)) {
- /* Update the input */
- if (input == NULL) {
- input = playlist_CurrentInput(playlist);
- } else if (input->b_dead || !vlc_object_alive(input)) {
- vlc_object_release(input);
- input = NULL;
- }
-
- if (input == NULL) {
- /* Wait a bit */
- msleep(INTF_IDLE_SLEEP);
- continue;
- }
-
- /*
- * We now have an input
- */
-
- /* Initialize file descriptor set and timeout (0.5s) */
- /* FIXME: arbitrary tick */
- struct pollfd ufd = { .fd = fd, .events = POLLIN, };
-
- if (is_master) {
- struct sockaddr_storage from;
- mtime_t master_system;
- mtime_t client_system;
- mtime_t date;
- int struct_size, read_size, ret;
+ /* TODO use the delay */
+ mtime_t system;
+ if (input_GetPcrSystem(input, &system, NULL))
+ system = -1;
+ vlc_restorecancel(canc);
- /* Don't block */
- ret = poll(&ufd, 1, timeout);
- if (ret <= 0)
- continue;
+ return system;
+}
- /* We received something */
- struct_size = sizeof(from);
- read_size = recvfrom(fd, data, MAX_MSG_LENGTH, 0,
- (struct sockaddr*)&from,
- (unsigned int *)&struct_size);
+static void *Master(void *handle)
+{
+ intf_thread_t *intf = handle;
+ intf_sys_t *sys = intf->p_sys;
+ for (;;) {
+ struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
+ uint64_t data[2];
- /* not sure we need the client information to sync,
- since we are the master anyway */
- client_system = ntoh64(*(int64_t *)data);
+ if (poll(&ufd, 1, -1) < 0)
+ continue;
- date = mdate();
+ /* We received something */
+ struct sockaddr_storage from;
+ socklen_t fromlen = sizeof (from);
- if (input_GetPcrSystem(input, &master_system))
- continue;
+ if (recvfrom(sys->fd, data, 8, 0,
+ (struct sockaddr *)&from, &fromlen) < 8)
+ continue;
- *((int64_t *)data) = hton64(date);
- *(((int64_t *)data)+1) = hton64(master_system);
+ mtime_t master_system = GetPcrSystem(sys->input);
+ if (master_system < 0)
+ continue;
- /* Reply to the sender */
- sendto(fd, data, 2 * sizeof(int64_t), 0,
- (struct sockaddr *)&from, struct_size);
+ data[0] = hton64(mdate());
+ data[1] = hton64(master_system);
+ /* Reply to the sender */
+ sendto(sys->fd, data, 16, 0,
+ (struct sockaddr *)&from, fromlen);
#if 0
- msg_Dbg(intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
- "(date: %"PRId64")", client_system, master_system,
- (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
- : "non-IPv4", date);
+ /* not sure we need the client information to sync,
+ since we are the master anyway */
+ mtime_t client_system = ntoh64(data[0]);
+ msg_Dbg(intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
+ "(date: %"PRId64")", client_system, master_system,
+ (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
+ : "non-IPv4", /*date*/ 0);
#endif
- }
- else
- {
- mtime_t master_system;
- mtime_t client_system;
- mtime_t system = 0;
- mtime_t send_date, receive_date;
- mtime_t diff_date, master_date;
- int sent, read_size, ret;
-
- if (input_GetPcrSystem(input, &system)) {
- msleep(INTF_IDLE_SLEEP);
- continue;
- }
+ }
+ return NULL;
+}
- /* Send clock request to the master */
- send_date = mdate();
- *((int64_t *)data) = hton64(system);
+static void *Slave(void *handle)
+{
+ intf_thread_t *intf = handle;
+ intf_sys_t *sys = intf->p_sys;
- sent = send(fd, data, sizeof(int64_t), 0);
- if (sent <= 0) {
- msleep(INTF_IDLE_SLEEP);
- continue;
- }
+ for (;;) {
+ struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
+ uint64_t data[2];
- /* Don't block */
- ret = poll(&ufd, 1, timeout);
- if (ret == 0)
- continue;
- if (ret < 0) {
- msleep(INTF_IDLE_SLEEP);
- continue;
- }
+ mtime_t system = GetPcrSystem(sys->input);
+ if (system < 0)
+ goto wait;
- receive_date = mdate();
- read_size = recv(fd, data, MAX_MSG_LENGTH, 0);
- if (read_size <= 0) {
- msleep(INTF_IDLE_SLEEP);
- continue;
- }
+ /* Send clock request to the master */
+ const mtime_t send_date = mdate();
- master_date = ntoh64(*(int64_t *)data);
- master_system = ntoh64(*(((int64_t *)data)+1)); /* system date */
+ data[0] = hton64(system);
+ send(sys->fd, data, 8, 0);
- diff_date = receive_date -
- ((receive_date - send_date) / 2 + master_date);
+ /* Don't block */
+ if (poll(&ufd, 1, sys->timeout) <= 0)
+ continue;
- if (input && master_system > 0) {
- mtime_t diff_system;
+ const mtime_t receive_date = mdate();
+ if (recv(sys->fd, data, 16, 0) < 16)
+ goto wait;
- if (input_GetPcrSystem(input, &client_system)) {
- msleep(INTF_IDLE_SLEEP);
- continue;
- }
+ const mtime_t master_date = ntoh64(data[0]);
+ const mtime_t master_system = ntoh64(data[1]);
+ const mtime_t diff_date = receive_date -
+ ((receive_date - send_date) / 2 + master_date);
- diff_system = client_system - master_system - diff_date;
+ if (master_system > 0) {
+ int canc = vlc_savecancel();
+
+ mtime_t client_system;
+ if (!input_GetPcrSystem(sys->input, &client_system, NULL)) {
+ const mtime_t diff_system = client_system - master_system - diff_date;
if (diff_system != 0) {
- input_ModifyPcrSystem(input, true, master_system - diff_date);
+ input_ModifyPcrSystem(sys->input, true, master_system - diff_date);
#if 0
msg_Dbg(intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
" clock diff: %"PRId64", diff: %"PRId64"",
#endif
}
}
- msleep(INTF_IDLE_SLEEP);
+ vlc_restorecancel(canc);
}
+ wait:
+ msleep(INTF_IDLE_SLEEP);
}
+ return NULL;
+}
- if (input)
- vlc_object_release(input);
- pl_Release(intf);
- vlc_restorecancel(canc);
+static int PlaylistEvent(vlc_object_t *object, char const *cmd,
+ vlc_value_t oldval, vlc_value_t newval, void *data)
+{
+ VLC_UNUSED(cmd); VLC_UNUSED(object);
+ intf_thread_t *intf = data;
+ intf_sys_t *sys = intf->p_sys;
+ input_thread_t *input = newval.p_address;
+
+ if (sys->input != NULL) {
+ msg_Err(intf, "InputEvent DEAD");
+ assert(oldval.p_address == sys->input);
+
+ vlc_cancel(sys->thread);
+ vlc_join(sys->thread, NULL);
+ }
+
+ sys->input = input;
+
+ if (input != NULL) {
+ if (vlc_clone(&sys->thread, sys->is_master ? Master : Slave, intf,
+ VLC_THREAD_PRIORITY_INPUT))
+ sys->input = NULL;
+ }
+ return VLC_SUCCESS;
}