1 /*****************************************************************************
2 * netsync.c: synchronisation between several network clients.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
39 #include <sys/types.h>
42 # include <winsock2.h>
43 # include <ws2tcpip.h>
45 # define IN_MULTICAST(a) IN_CLASSD(a)
48 # include <sys/socket.h>
49 # include <netinet/in.h>
51 # include <arpa/inet.h>
52 # elif defined( SYS_BEOS )
53 # include <net/netdb.h>
58 # define close(a) CloseHandle(a);
59 #elif defined( WIN32 )
60 # define close(a) closesocket(a);
65 #define NETSYNC_PORT_MASTER 9875
66 #define NETSYNC_PORT_SLAVE 9876
68 /* Needed for Solaris */
70 #define INADDR_NONE 0xffffffff
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Activate( vlc_object_t * );
77 static void Close ( vlc_object_t * );
79 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
81 #define NETSYNC_TEXT N_( "Act as master for network synchronisation" )
82 #define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \
83 "act as the master client for the network synchronisation." )
85 #define MIP_TEXT N_( "Master client ip address" )
86 #define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \
87 "the master client used for the network synchronisation." )
90 set_description( _("Network synchronisation") );
92 add_bool( "netsync-master", 0, NULL,
93 NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );
94 add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
97 set_capability( "interface", 0 );
98 set_callbacks( Activate, Close );
103 input_thread_t *p_input;
106 /*****************************************************************************
108 *****************************************************************************/
109 static void Run( intf_thread_t *p_intf );
111 /*****************************************************************************
112 * Activate: initialize and create stuff
113 *****************************************************************************/
114 static int Activate( vlc_object_t *p_this )
116 intf_thread_t *p_intf = (intf_thread_t*)p_this;
118 msg_Info( p_intf, "Using the netsync interface module..." );
120 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
123 msg_Err( p_intf, "no memory" );
127 p_intf->p_sys->p_input = NULL;
129 p_intf->pf_run = Run;
133 /*****************************************************************************
134 * Close: destroy interface
135 *****************************************************************************/
136 void Close( vlc_object_t *p_this )
138 intf_thread_t *p_intf = (intf_thread_t*)p_this;
140 free( p_intf->p_sys );
143 /*****************************************************************************
144 * Run: interface thread
145 *****************************************************************************/
146 static void Run( intf_thread_t *p_intf )
148 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
150 vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );
151 char *psz_master = NULL;
152 char p_data[MAX_MSG_LENGTH];
157 psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
158 if( psz_master == NULL )
160 msg_Err( p_intf, "master address not specified" );
165 i_socket = net_OpenUDP( p_intf, NULL,
166 b_master ? NETSYNC_PORT_MASTER : NETSYNC_PORT_SLAVE,
167 b_master ? NULL : psz_master,
168 b_master ? 0 : NETSYNC_PORT_MASTER );
170 if( psz_master ) free( psz_master );
174 msg_Err( p_intf, "failed opening UDP socket." );
178 /* High priority thread */
179 vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
181 while( !p_intf->b_die )
183 struct timeval timeout;
186 /* Update the input */
187 if( p_intf->p_sys->p_input == NULL )
189 p_intf->p_sys->p_input =
190 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
193 else if( p_intf->p_sys->p_input->b_dead )
195 vlc_object_release( p_intf->p_sys->p_input );
196 p_intf->p_sys->p_input = NULL;
199 if( p_intf->p_sys->p_input == NULL )
202 msleep( INTF_IDLE_SLEEP );
207 * We now have an input
210 /* Initialize file descriptor set and timeout (0.5s) */
212 FD_SET( i_socket, &fds_r );
214 timeout.tv_usec = 500000;
218 struct sockaddr_storage from;
219 mtime_t i_date, i_clockref, i_master_clockref;
220 int i_struct_size, i_read, i_ret;
223 i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );
224 if( i_ret == 0 ) continue;
228 msleep( INTF_IDLE_SLEEP );
232 /* We received something */
233 i_struct_size = sizeof( from );
234 i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
235 (struct sockaddr*)&from, &i_struct_size );
237 i_clockref = ntoh64(*(int64_t *)p_data);
240 *(int64_t *)p_data = hton64( i_date );
242 i_master_clockref = GetClockRef( p_intf, i_clockref );
243 *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
245 /* Reply to the sender */
246 sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
247 (struct sockaddr *)&from, i_struct_size );
250 msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "
251 "(date: "I64Fd")", i_clockref, i_master_clockref,
252 from.ss_family == AF_INET
253 ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
254 : "non-IPv4", i_date );
259 mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
260 mtime_t i_master_clockref, i_client_clockref, i_drift;
261 mtime_t i_clockref = 0;
262 int i_sent, i_read, i_ret;
264 /* Send clock request to the master */
265 *(int64_t *)p_data = hton64( i_clockref );
266 i_send_date = mdate();
268 i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
272 msleep( INTF_IDLE_SLEEP );
277 i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);
278 if( i_ret == 0 ) continue;
282 msleep( INTF_IDLE_SLEEP );
286 i_receive_date = mdate();
288 i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
292 msleep( INTF_IDLE_SLEEP );
296 i_master_date = ntoh64(*(int64_t *)p_data);
297 i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
299 i_diff_date = i_receive_date -
300 ((i_receive_date - i_send_date) / 2 + i_master_date);
302 i_client_clockref = i_drift = 0;
303 if( p_intf->p_sys->p_input && i_master_clockref )
305 i_client_clockref = GetClockRef( p_intf, i_clockref );
306 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
308 /* Update our clock to match the master's one */
309 if( i_client_clockref )
310 p_intf->p_sys->p_input->i_pts_delay -= i_drift;
314 msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "
315 "clock diff: "I64Fd" drift: "I64Fd,
316 i_clockref, i_master_clockref,
317 i_client_clockref, i_diff_date, i_drift );
321 msleep( INTF_IDLE_SLEEP );
325 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
326 net_Close( i_socket );
329 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
331 input_thread_t *p_input = p_intf->p_sys->p_input;
334 if( !p_input || !p_input->p_es_out ) return 0;
336 if( es_out_Control( p_input->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==