1 /*****************************************************************************
\r
2 * netsync.c: synchronisation between several network clients.
\r
3 *****************************************************************************
\r
4 * Copyright (C) 2004 VideoLAN
\r
7 * Authors: Gildas Bazin <gbazin@videolan.org>
\r
9 * This program is free software; you can redistribute it and/or modify
\r
10 * it under the terms of the GNU General Public License as published by
\r
11 * the Free Software Foundation; either version 2 of the License, or
\r
12 * (at your option) any later version.
\r
14 * This program is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU General Public License for more details.
\r
19 * You should have received a copy of the GNU General Public License
\r
20 * along with this program; if not, write to the Free Software
\r
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
\r
22 *****************************************************************************/
\r
24 /*****************************************************************************
\r
26 *****************************************************************************/
\r
28 #include <vlc/vlc.h>
\r
29 #include <vlc/intf.h>
\r
31 #ifdef HAVE_UNISTD_H
\r
32 # include <unistd.h>
\r
35 #ifdef HAVE_SYS_TIME_H
\r
36 # include <sys/time.h>
\r
38 #include <sys/types.h>
\r
41 # include <winsock2.h>
\r
42 # include <ws2tcpip.h>
\r
43 # ifndef IN_MULTICAST
\r
44 # define IN_MULTICAST(a) IN_CLASSD(a)
\r
47 # include <sys/socket.h>
\r
48 # include <netinet/in.h>
\r
49 # if HAVE_ARPA_INET_H
\r
50 # include <arpa/inet.h>
\r
51 # elif defined( SYS_BEOS )
\r
52 # include <net/netdb.h>
\r
57 # define close(a) CloseHandle(a);
\r
58 #elif defined( WIN32 )
\r
59 # define close(a) closesocket(a);
\r
62 #include "network.h"
\r
64 #define NETSYNC_PORT_MASTER 9875
\r
65 #define NETSYNC_PORT_SLAVE 9876
\r
67 /* Needed for Solaris */
\r
69 #define INADDR_NONE 0xffffffff
\r
72 /*****************************************************************************
\r
74 *****************************************************************************/
\r
75 static int Activate( vlc_object_t * );
\r
76 static void Close ( vlc_object_t * );
\r
78 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
\r
80 #define NETSYNC_TEXT N_( "Act as master for network synchronisation" )
\r
81 #define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \
\r
82 "act as the master client for the network synchronisation." )
\r
84 #define MIP_TEXT N_( "Master client ip address" )
\r
85 #define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \
\r
86 "the master client used for the network synchronisation." )
\r
89 set_description( _("Network synchronisation") );
\r
91 add_bool( "netsync-master", 0, NULL,
\r
92 NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );
\r
93 add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
\r
96 set_capability( "interface", 0 );
\r
97 set_callbacks( Activate, Close );
\r
102 input_thread_t *p_input;
\r
105 /*****************************************************************************
\r
107 *****************************************************************************/
\r
108 static void Run( intf_thread_t *p_intf );
\r
110 /*****************************************************************************
\r
111 * Activate: initialize and create stuff
\r
112 *****************************************************************************/
\r
113 static int Activate( vlc_object_t *p_this )
\r
115 intf_thread_t *p_intf = (intf_thread_t*)p_this;
\r
117 msg_Info( p_intf, "Using the netsync interface module..." );
\r
119 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
\r
120 if( !p_intf->p_sys )
\r
122 msg_Err( p_intf, "no memory" );
\r
126 p_intf->p_sys->p_input = NULL;
\r
128 p_intf->pf_run = Run;
\r
129 return VLC_SUCCESS;
\r
132 /*****************************************************************************
\r
133 * Close: destroy interface
\r
134 *****************************************************************************/
\r
135 void Close( vlc_object_t *p_this )
\r
137 intf_thread_t *p_intf = (intf_thread_t*)p_this;
\r
139 free( p_intf->p_sys );
\r
142 /*****************************************************************************
\r
143 * Run: interface thread
\r
144 *****************************************************************************/
\r
145 static void Run( intf_thread_t *p_intf )
\r
147 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
\r
149 vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );
\r
150 char *psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
\r
151 struct sockaddr_in master_addr;
\r
152 char p_data[MAX_MSG_LENGTH];
\r
155 if( !psz_master || inet_addr( psz_master ) == INADDR_NONE )
\r
159 if( psz_master ) free( psz_master );
\r
160 msg_Err( p_intf, "invalid master address." );
\r
164 if( !psz_master ) psz_master = strdup("");
\r
167 memset( &master_addr, 0, sizeof( struct sockaddr_in ) );
\r
168 master_addr.sin_family = AF_INET;
\r
169 master_addr.sin_port = htons( (uint16_t)NETSYNC_PORT_MASTER );
\r
170 master_addr.sin_addr.s_addr = inet_addr( psz_master );
\r
171 free( psz_master );
\r
173 i_socket = net_OpenUDP( p_intf, NULL, b_master ? NETSYNC_PORT_MASTER :
\r
174 NETSYNC_PORT_SLAVE, NULL, 0 );
\r
177 msg_Err( p_intf, "failed opening UDP socket." );
\r
181 /* High priority thread */
\r
182 vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
\r
184 while( !p_intf->b_die )
\r
186 struct timeval timeout;
\r
189 /* Update the input */
\r
190 if( p_intf->p_sys->p_input == NULL )
\r
192 p_intf->p_sys->p_input =
\r
193 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
\r
196 else if( p_intf->p_sys->p_input->b_dead )
\r
198 vlc_object_release( p_intf->p_sys->p_input );
\r
199 p_intf->p_sys->p_input = NULL;
\r
202 if( p_intf->p_sys->p_input == NULL )
\r
205 msleep( INTF_IDLE_SLEEP );
\r
210 * We now have an input
\r
213 /* Initialize file descriptor set and timeout (0.5s) */
\r
215 FD_SET( i_socket, &fds_r );
\r
216 timeout.tv_sec = 0;
\r
217 timeout.tv_usec = 500000;
\r
221 struct sockaddr_in from;
\r
222 mtime_t i_date, i_clockref, i_master_clockref;
\r
223 int i_struct_size, i_read, i_ret;
\r
226 i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );
\r
227 if( i_ret == 0 ) continue;
\r
231 msleep( INTF_IDLE_SLEEP );
\r
235 /* We received something */
\r
236 i_struct_size = sizeof(struct sockaddr_in);
\r
237 i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
\r
238 (struct sockaddr*)&from, &i_struct_size );
\r
240 from.sin_port = htons( (uint16_t)NETSYNC_PORT_SLAVE );
\r
242 i_clockref = ntoh64(*(int64_t *)p_data);
\r
245 *(int64_t *)p_data = hton64( i_date );
\r
247 i_master_clockref = GetClockRef( p_intf, i_clockref );
\r
248 *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
\r
250 /* Reply to the sender */
\r
251 sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
\r
252 (struct sockaddr *)&from, sizeof(struct sockaddr_in) );
\r
254 msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "
\r
255 "(date: "I64Fd")", i_clockref, i_master_clockref,
\r
256 inet_ntoa(from.sin_addr), i_date );
\r
260 mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
\r
261 mtime_t i_master_clockref, i_client_clockref, i_drift;
\r
262 mtime_t i_clockref = 0;
\r
263 int i_sent, i_read, i_ret;
\r
265 /* Send clock request to the master */
\r
266 *(int64_t *)p_data = hton64( i_clockref );
\r
267 i_send_date = mdate();
\r
269 i_sent = sendto( i_socket, p_data, sizeof(int64_t), 0,
\r
270 (struct sockaddr *)&master_addr,
\r
271 sizeof(struct sockaddr_in) );
\r
275 msleep( INTF_IDLE_SLEEP );
\r
280 i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);
\r
281 if( i_ret == 0 ) continue;
\r
285 msleep( INTF_IDLE_SLEEP );
\r
289 i_receive_date = mdate();
\r
291 i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
\r
295 msleep( INTF_IDLE_SLEEP );
\r
299 i_master_date = ntoh64(*(int64_t *)p_data);
\r
300 i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
\r
302 i_diff_date = i_receive_date -
\r
303 ((i_receive_date - i_send_date) / 2 + i_master_date);
\r
305 i_client_clockref = i_drift = 0;
\r
306 if( p_intf->p_sys->p_input && i_master_clockref )
\r
308 i_client_clockref = GetClockRef( p_intf, i_clockref );
\r
309 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
\r
311 /* Update our clock to match the master's one */
\r
312 if( i_client_clockref )
\r
313 p_intf->p_sys->p_input->i_pts_delay -= i_drift;
\r
316 msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "
\r
317 "clock diff: "I64Fd" drift: "I64Fd,
\r
318 i_clockref, i_master_clockref,
\r
319 i_client_clockref, i_diff_date, i_drift );
\r
322 msleep( INTF_IDLE_SLEEP );
\r
326 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
\r
327 net_Close( i_socket );
\r
330 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
\r
332 input_thread_t *p_input = p_intf->p_sys->p_input;
\r
333 pgrm_descriptor_t *p_pgrm;
\r
335 if( !p_input ) return 0;
\r
337 p_pgrm = p_input->stream.p_selected_program;
\r
338 if( p_pgrm ) return input_ClockGetTS( p_input, p_pgrm, i_pts );
\r