1 /*****************************************************************************
2 * netsync.c: synchronisation between several network clients.
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_interface.h>
30 #include <vlc_input.h>
31 #include <vlc_es_out.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
43 #include <vlc_network.h>
45 #define NETSYNC_PORT 9875
47 /* Needed for Solaris */
49 #define INADDR_NONE 0xffffffff
52 /* FIXME: UGLY UGLY !! Netsync should be totally reworked */
53 #include "../../src/input/input_internal.h"
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Activate( vlc_object_t * );
59 static void Close ( vlc_object_t * );
61 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
63 /// \bug [String] This string is BAD.
64 #define NETSYNC_TEXT N_( "Act as master" )
65 #define NETSYNC_LONGTEXT N_( "Should " \
66 "act as the master client for the network synchronisation?" )
68 /// \bug [String] This string is BAD.
69 #define MIP_TEXT N_( "Master client ip address" )
70 #define MIP_LONGTEXT N_( "IP address of " \
71 "the master client used for the network synchronisation." )
74 set_shortname( _("Network Sync"));
75 set_description( _("Network synchronisation") );
76 set_category( CAT_ADVANCED );
77 set_subcategory( SUBCAT_ADVANCED_MISC );
79 add_bool( "netsync-master", 0, NULL,
80 NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );
81 add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
84 set_capability( "interface", 0 );
85 set_callbacks( Activate, Close );
90 input_thread_t *p_input;
93 /*****************************************************************************
95 *****************************************************************************/
96 static void Run( intf_thread_t *p_intf );
98 /*****************************************************************************
99 * Activate: initialize and create stuff
100 *****************************************************************************/
101 static int Activate( vlc_object_t *p_this )
103 intf_thread_t *p_intf = (intf_thread_t*)p_this;
105 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
108 msg_Err( p_intf, "no memory" );
112 p_intf->p_sys->p_input = NULL;
114 p_intf->pf_run = Run;
118 /*****************************************************************************
119 * Close: destroy interface
120 *****************************************************************************/
121 void Close( vlc_object_t *p_this )
123 intf_thread_t *p_intf = (intf_thread_t*)p_this;
125 free( p_intf->p_sys );
128 /*****************************************************************************
129 * Run: interface thread
130 *****************************************************************************/
131 static void Run( intf_thread_t *p_intf )
133 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
135 vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );
136 char *psz_master = NULL;
137 char p_data[MAX_MSG_LENGTH];
142 psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
143 if( psz_master == NULL )
145 msg_Err( p_intf, "master address not specified" );
151 i_socket = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
153 i_socket = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, 0 );
155 if( psz_master ) free( psz_master );
159 msg_Err( p_intf, "failed opening UDP socket" ); /* str review: is this good enough? */
163 /* High priority thread */
164 vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
166 while( !intf_ShouldDie( p_intf ) )
168 struct timeval timeout;
171 /* Update the input */
172 if( p_intf->p_sys->p_input == NULL )
174 p_intf->p_sys->p_input =
175 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
178 else if( p_intf->p_sys->p_input->b_dead )
180 vlc_object_release( p_intf->p_sys->p_input );
181 p_intf->p_sys->p_input = NULL;
184 if( p_intf->p_sys->p_input == NULL )
187 msleep( INTF_IDLE_SLEEP );
192 * We now have an input
195 /* Initialize file descriptor set and timeout (0.5s) */
197 FD_SET( i_socket, &fds_r );
199 timeout.tv_usec = 500000;
203 struct sockaddr_storage from;
204 mtime_t i_date, i_clockref, i_master_clockref;
205 int i_struct_size, i_read, i_ret;
208 i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );
209 if( i_ret == 0 ) continue;
213 msleep( INTF_IDLE_SLEEP );
217 /* We received something */
218 i_struct_size = sizeof( from );
219 i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
220 (struct sockaddr*)&from,
221 (unsigned int *)&i_struct_size );
223 i_clockref = ntoh64(*(int64_t *)p_data);
226 *(int64_t *)p_data = hton64( i_date );
228 i_master_clockref = GetClockRef( p_intf, i_clockref );
229 *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
231 /* Reply to the sender */
232 sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
233 (struct sockaddr *)&from, i_struct_size );
236 msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "
237 "(date: "I64Fd")", i_clockref, i_master_clockref,
238 from.ss_family == AF_INET
239 ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
240 : "non-IPv4", i_date );
245 mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
246 mtime_t i_master_clockref, i_client_clockref, i_drift;
247 mtime_t i_clockref = 0;
248 int i_sent, i_read, i_ret;
250 /* Send clock request to the master */
251 *(int64_t *)p_data = hton64( i_clockref );
252 i_send_date = mdate();
254 i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
258 msleep( INTF_IDLE_SLEEP );
263 i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);
264 if( i_ret == 0 ) continue;
268 msleep( INTF_IDLE_SLEEP );
272 i_receive_date = mdate();
274 i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
278 msleep( INTF_IDLE_SLEEP );
282 i_master_date = ntoh64(*(int64_t *)p_data);
283 i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
285 i_diff_date = i_receive_date -
286 ((i_receive_date - i_send_date) / 2 + i_master_date);
288 i_client_clockref = i_drift = 0;
289 if( p_intf->p_sys->p_input && i_master_clockref )
291 i_client_clockref = GetClockRef( p_intf, i_clockref );
292 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
294 /* Update our clock to match the master's one */
295 if( i_client_clockref )
296 p_intf->p_sys->p_input->i_pts_delay -= i_drift;
300 msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "
301 "clock diff: "I64Fd" drift: "I64Fd,
302 i_clockref, i_master_clockref,
303 i_client_clockref, i_diff_date, i_drift );
307 msleep( INTF_IDLE_SLEEP );
311 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
312 net_Close( i_socket );
315 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
317 input_thread_t *p_input = p_intf->p_sys->p_input;
320 if( !p_input || !p_input->p->p_es_out ) return 0;
322 if( es_out_Control( p_input->p->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==