X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fcontrol%2Fnetsync.c;h=2f9e778f13e878c0c1a60747a04ac2625e9be7ca;hb=c60652e38ac6afd74bd8225e9dae5406f13aaa4f;hp=0eb95dd71b8bee88aef8a902a95e377290d2e26a;hpb=e584575d9499798af56c763c43fe32089c0a086c;p=vlc diff --git a/modules/control/netsync.c b/modules/control/netsync.c index 0eb95dd71b..2f9e778f13 100644 --- a/modules/control/netsync.c +++ b/modules/control/netsync.c @@ -1,332 +1,308 @@ -/***************************************************************************** - * netsync.c: synchronisation between several network clients. - ***************************************************************************** - * Copyright (C) 2004 VideoLAN - * $Id$ - * - * Authors: Gildas Bazin - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. - *****************************************************************************/ - -/***************************************************************************** - * Preamble - *****************************************************************************/ -#include -#include -#include - -#ifdef HAVE_UNISTD_H -# include -#endif - -#ifdef HAVE_SYS_TIME_H -# include -#endif -#include - -#ifdef WIN32 -# include -# include -# ifndef IN_MULTICAST -# define IN_MULTICAST(a) IN_CLASSD(a) -# endif -#else -# include -# include -# if HAVE_ARPA_INET_H -# include -# elif defined( SYS_BEOS ) -# include -# endif -#endif - -#ifdef UNDER_CE -# define close(a) CloseHandle(a); -#elif defined( WIN32 ) -# define close(a) closesocket(a); -#endif - -#include "network.h" -#include "input_ext-plugins.h" - -#define NETSYNC_PORT_MASTER 9875 -#define NETSYNC_PORT_SLAVE 9876 - -/***************************************************************************** - * Module descriptor - *****************************************************************************/ -static int Activate( vlc_object_t * ); -static void Close ( vlc_object_t * ); - -static mtime_t GetClockRef( intf_thread_t *, mtime_t ); - -#define NETSYNC_TEXT N_( "Act as master for network synchronisation" ) -#define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \ - "act as the master client for the network synchronisation." ) - -#define MIP_TEXT N_( "Master client ip address" ) -#define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \ - "the master client used for the network synchronisation." ) - -vlc_module_begin(); - set_description( _("Network synchronisation") ); - - add_bool( "netsync-master", 0, NULL, - NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE ); - add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT, - VLC_TRUE ); - - set_capability( "interface", 0 ); - set_callbacks( Activate, Close ); -vlc_module_end(); - -struct intf_sys_t -{ - input_thread_t *p_input; -}; - -/***************************************************************************** - * Local prototypes - *****************************************************************************/ -static void Run( intf_thread_t *p_intf ); - -/***************************************************************************** - * Activate: initialize and create stuff - *****************************************************************************/ -static int Activate( vlc_object_t *p_this ) -{ - intf_thread_t *p_intf = (intf_thread_t*)p_this; - - msg_Info( p_intf, "Using the netsync interface module..." ); - - p_intf->p_sys = malloc( sizeof( intf_sys_t ) ); - if( !p_intf->p_sys ) - { - msg_Err( p_intf, "no memory" ); - return VLC_ENOMEM; - } - - p_intf->p_sys->p_input = NULL; - - p_intf->pf_run = Run; - return VLC_SUCCESS; -} - -/***************************************************************************** - * Close: destroy interface - *****************************************************************************/ -void Close( vlc_object_t *p_this ) -{ - intf_thread_t *p_intf = (intf_thread_t*)p_this; - - free( p_intf->p_sys ); -} - -/***************************************************************************** - * Run: interface thread - *****************************************************************************/ -static void Run( intf_thread_t *p_intf ) -{ -#define MAX_MSG_LENGTH (2 * sizeof(int64_t)) - - vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" ); - char *psz_master = config_GetPsz( p_intf, "netsync-master-ip" ); - struct sockaddr_in master_addr; - char p_data[MAX_MSG_LENGTH]; - int i_socket; - - if( !b_master && (!psz_master || inet_addr( psz_master ) == INADDR_NONE) ) - { - if( psz_master ) free( psz_master ); - msg_Err( p_intf, "invalid master address." ); - return; - } - - memset( &master_addr, 0, sizeof( struct sockaddr_in ) ); - master_addr.sin_family = AF_INET; - master_addr.sin_port = htons( (uint16_t)NETSYNC_PORT_MASTER ); - master_addr.sin_addr.s_addr = inet_addr( psz_master ); - free( psz_master ); - - i_socket = net_OpenUDP( p_intf, NULL, b_master ? NETSYNC_PORT_MASTER : - NETSYNC_PORT_SLAVE, NULL, 0 ); - if( i_socket < 0 ) - { - msg_Err( p_intf, "failed opening UDP socket." ); - return; - } - - /* High priority thread */ - vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT ); - - while( !p_intf->b_die ) - { - struct timeval timeout; - fd_set fds_r; - - /* Update the input */ - if( p_intf->p_sys->p_input == NULL ) - { - p_intf->p_sys->p_input = - (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT, - FIND_ANYWHERE ); - } - else if( p_intf->p_sys->p_input->b_dead ) - { - vlc_object_release( p_intf->p_sys->p_input ); - p_intf->p_sys->p_input = NULL; - } - - if( p_intf->p_sys->p_input == NULL ) - { - /* Wait a bit */ - msleep( INTF_IDLE_SLEEP ); - continue; - } - - /* - * We now have an input - */ - - /* Initialize file descriptor set and timeout (0.5s) */ - FD_ZERO( &fds_r ); - FD_SET( i_socket, &fds_r ); - timeout.tv_sec = 0; - timeout.tv_usec = 500000; - - if( b_master ) - { - struct sockaddr_in from; - mtime_t i_date, i_clockref, i_master_clockref; - int i_struct_size, i_read, i_ret; - - /* Don't block */ - i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout ); - if( i_ret == 0 ) continue; - if( i_ret < 0 ) - { - /* Wait a bit */ - msleep( INTF_IDLE_SLEEP ); - continue; - } - - /* We received something */ - i_struct_size = sizeof(struct sockaddr_in); - i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0, - (struct sockaddr*)&from, &i_struct_size ); - - from.sin_port = htons( (uint16_t)NETSYNC_PORT_SLAVE ); - - i_clockref = ntoh64(*(int64_t *)p_data); - - i_date = mdate(); - *(int64_t *)p_data = hton64( i_date ); - - i_master_clockref = GetClockRef( p_intf, i_clockref ); - *(((int64_t *)p_data)+1) = hton64( i_master_clockref ); - - /* Reply to the sender */ - sendto( i_socket, p_data, 2 * sizeof(int64_t), 0, - (struct sockaddr *)&from, sizeof(struct sockaddr_in) ); - - msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s " - "(date: "I64Fd")", i_clockref, i_master_clockref, - inet_ntoa(from.sin_addr), i_date ); - } - else - { - mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date; - mtime_t i_master_clockref, i_client_clockref, i_drift; - mtime_t i_clockref = 0; - int i_sent, i_read, i_ret; - - /* Send clock request to the master */ - *(int64_t *)p_data = hton64( i_clockref ); - i_send_date = mdate(); - - i_sent = sendto( i_socket, p_data, sizeof(int64_t), 0, - (struct sockaddr *)&master_addr, - sizeof(struct sockaddr_in) ); - if( i_sent <= 0 ) - { - /* Wait a bit */ - msleep( INTF_IDLE_SLEEP ); - continue; - } - - /* Don't block */ - i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout); - if( i_ret == 0 ) continue; - if( i_ret < 0 ) - { - /* Wait a bit */ - msleep( INTF_IDLE_SLEEP ); - continue; - } - - i_receive_date = mdate(); - - i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 ); - if( i_read <= 0 ) - { - /* Wait a bit */ - msleep( INTF_IDLE_SLEEP ); - continue; - } - - i_master_date = ntoh64(*(int64_t *)p_data); - i_master_clockref = ntoh64(*(((int64_t *)p_data)+1)); - - i_diff_date = i_receive_date - - ((i_receive_date - i_send_date) / 2 + i_master_date); - - i_client_clockref = i_drift = 0; - if( p_intf->p_sys->p_input && i_master_clockref ) - { - i_client_clockref = GetClockRef( p_intf, i_clockref ); - i_drift = i_client_clockref - i_master_clockref - i_diff_date; - - /* Update our clock to match the master's one */ - if( i_client_clockref ) - p_intf->p_sys->p_input->i_pts_delay -= i_drift; - } - - msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", " - "clock diff: "I64Fd" drift: "I64Fd, - i_clockref, i_master_clockref, - i_client_clockref, i_diff_date, i_drift ); - - /* Wait a bit */ - msleep( INTF_IDLE_SLEEP ); - } - } - - if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input ); - net_Close( i_socket ); -} - -static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts ) -{ - input_thread_t *p_input = p_intf->p_sys->p_input; - pgrm_descriptor_t *p_pgrm; - - if( !p_input ) return 0; - - p_pgrm = p_input->stream.p_selected_program; - if( p_pgrm ) return input_ClockGetTS( p_input, p_pgrm, i_pts ); - - return 0; -} +/***************************************************************************** + * netsync.c: synchronization between several network clients. + ***************************************************************************** + * Copyright (C) 2004-2009 the VideoLAN team + * $Id$ + * + * Authors: Gildas Bazin + * Jean-Paul Saman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +/***************************************************************************** + * Preamble + *****************************************************************************/ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include + +#include +#include +#include +#include +#include + +#ifdef HAVE_UNISTD_H +# include +#endif +#include +#ifdef HAVE_POLL +# include +#endif + +#include + +#define NETSYNC_PORT 9875 + +/***************************************************************************** + * Module descriptor + *****************************************************************************/ +static int Open (vlc_object_t *); +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 synchronization " \ + "over clients listening on the masters network ip address") + +#define MIP_TEXT N_("Master server ip address") +#define MIP_LONGTEXT N_("The IP address of " \ + "The network master clock to use for clock synchronization.") + +#define NETSYNC_TIMEOUT_TEXT N_("UDP timeout (in ms)") +#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 synchronization")) + set_category(CAT_ADVANCED) + set_subcategory(SUBCAT_ADVANCED_MISC) + + add_bool("netsync-master", false, NULL, + NETSYNC_TEXT, NETSYNC_LONGTEXT, true) + add_string("netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT, + true) + add_integer("netsync-timeout", 500, NULL, + NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true) + + set_capability("interface", 0) + set_callbacks(Open, Close) +vlc_module_end() + +/***************************************************************************** + * Local prototypes + *****************************************************************************/ +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")) { + char *psz_master = var_InheritString(intf, "netsync-master-ip"); + if (psz_master == NULL) { + msg_Err(intf, "master address not specified"); + return VLC_EGENERIC; + } + fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1); + free(psz_master); + } else { + fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT); + } + + if (fd == -1) { + msg_Err(intf, "Netsync socket failure"); + return VLC_EGENERIC; + } + + intf->pf_run = NULL; + 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; +} + +/***************************************************************************** + * Close: destroy interface + *****************************************************************************/ +void Close(vlc_object_t *object) +{ + intf_thread_t *intf = (intf_thread_t*)object; + intf_sys_t *sys = intf->p_sys; + + assert(sys->input == NULL); + var_DelCallback(sys->playlist, "input-current", PlaylistEvent, intf); + net_Close(sys->fd); + free(sys); +} + +static mtime_t GetPcrSystem(input_thread_t *input) +{ + int canc = vlc_savecancel(); + /* TODO use the delay */ + mtime_t system; + if (input_GetPcrSystem(input, &system, NULL)) + system = -1; + vlc_restorecancel(canc); + + return system; +} + +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]; + + if (poll(&ufd, 1, -1) <= 0) + continue; + + /* We received something */ + struct sockaddr_storage from; + unsigned struct_size = sizeof(from); + recvfrom(sys->fd, data, sizeof(data), 0, + (struct sockaddr*)&from, &struct_size); + + mtime_t master_system = GetPcrSystem(sys->input); + if (master_system < 0) + continue; + + data[0] = hton64(mdate()); + data[1] = hton64(master_system); + + /* Reply to the sender */ + sendto(sys->fd, data, sizeof(data), 0, + (struct sockaddr *)&from, struct_size); +#if 0 + /* 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 + } +} + +static void *Slave(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]; + + mtime_t system = GetPcrSystem(sys->input); + if (system < 0) + goto wait; + + /* Send clock request to the master */ + data[0] = hton64(system); + + const mtime_t send_date = mdate(); + if (send(sys->fd, data, sizeof(data[0]), 0) <= 0) + goto wait; + + /* Don't block */ + int ret = poll(&ufd, 1, sys->timeout); + if (ret == 0) + continue; + if (ret < 0) + goto wait; + + const mtime_t receive_date = mdate(); + if (recv(sys->fd, data, sizeof(data), 0) <= 0) + goto wait; + + 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); + + 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(sys->input, true, master_system - diff_date); +#if 0 + msg_Dbg(intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64"," + " clock diff: %"PRId64", diff: %"PRId64"", + system, master_system, client_system, + diff_system, diff_date); +#endif + } + } + vlc_restorecancel(canc); + } + wait: + msleep(INTF_IDLE_SLEEP); + } +} + +static int InputEvent(vlc_object_t *object, char const *cmd, + vlc_value_t oldval, vlc_value_t newval, void *data) +{ + VLC_UNUSED(cmd); VLC_UNUSED(oldval); VLC_UNUSED(object); + intf_thread_t *intf = data; + intf_sys_t *sys = intf->p_sys; + + if (newval.i_int == INPUT_EVENT_DEAD && sys->input) { + msg_Err(intf, "InputEvent DEAD"); + vlc_cancel(sys->thread); + vlc_join(sys->thread, NULL); + vlc_object_release(sys->input); + sys->input = NULL; + } + return VLC_SUCCESS; +} + +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(oldval); VLC_UNUSED(object); + intf_thread_t *intf = data; + intf_sys_t *sys = intf->p_sys; + + input_thread_t *input = newval.p_address; + assert(sys->input == NULL); + sys->input = vlc_object_hold(input); + if (vlc_clone(&sys->thread, sys->is_master ? Master : Slave, intf, + VLC_THREAD_PRIORITY_INPUT)) { + vlc_object_release(input); + return VLC_SUCCESS; + } + var_AddCallback(input, "intf-event", InputEvent, intf); + return VLC_SUCCESS; +} +