1 /*****************************************************************************
2 * netsync.c: synchronisation between several network clients.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Jean-Paul Saman <jpsaman@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_input.h>
36 #include <vlc_playlist.h>
41 #include <sys/types.h>
46 #include <vlc_network.h>
48 #define NETSYNC_PORT 9875
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
56 #define NETSYNC_TEXT N_( "Network master clock" )
57 #define NETSYNC_LONGTEXT N_( "When set then " \
58 "this vlc instance shall dictate its clock for synchronisation" \
59 "over clients listening on the masters network ip address" )
61 #define MIP_TEXT N_( "Master server ip address" )
62 #define MIP_LONGTEXT N_( "The IP address of " \
63 "the network master clock to use for clock synchronisation." )
65 #define NETSYNC_TIMEOUT_TEXT N_( "UDP timeout (in ms)" )
66 #define NETSYNC_TIMEOUT_LONGTEXT N_("Amount of time (in ms) " \
67 "to wait before aborting network reception of data." )
70 set_shortname( N_("Network Sync"))
71 set_description( N_("Network synchronisation") )
72 set_category( CAT_ADVANCED )
73 set_subcategory( SUBCAT_ADVANCED_MISC )
75 add_bool( "netsync-master", false, NULL,
76 NETSYNC_TEXT, NETSYNC_LONGTEXT, true )
77 add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
79 add_integer( "netsync-timeout", 500, NULL,
80 NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true )
82 set_capability( "interface", 0 )
83 set_callbacks( Open, Close )
86 /*****************************************************************************
88 *****************************************************************************/
89 static void Run( intf_thread_t *p_intf );
91 /*****************************************************************************
92 * Activate: initialize and create stuff
93 *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
96 intf_thread_t *p_intf = (intf_thread_t*)p_this;
99 if( !var_InheritBool( p_intf, "netsync-master" ) )
101 char *psz_master = var_InheritString( p_intf, "netsync-master-ip" );
102 if( psz_master == NULL )
104 msg_Err( p_intf, "master address not specified" );
107 fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 );
111 fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
115 msg_Err( p_intf, "Netsync socket failure" );
119 p_intf->p_sys = (void *)(intptr_t)fd;
120 p_intf->pf_run = Run;
124 /*****************************************************************************
125 * Close: destroy interface
126 *****************************************************************************/
127 void Close( vlc_object_t *p_this )
129 intf_thread_t *p_intf = (intf_thread_t*)p_this;
131 net_Close( (intptr_t)p_intf->p_sys );
134 /*****************************************************************************
135 * Run: interface thread
136 *****************************************************************************/
137 static void Run( intf_thread_t *p_intf )
139 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
140 int canc = vlc_savecancel();
141 input_thread_t *p_input = NULL;
142 char p_data[MAX_MSG_LENGTH];
143 int i_socket = (intptr_t)p_intf->p_sys;
145 playlist_t *p_playlist = pl_Hold( p_intf );
146 int i_timeout = var_InheritInteger( p_intf, "netsync-timeout" );
147 if( i_timeout < 500 )
149 bool b_master = var_InheritBool( p_intf, "netsync-master" );
151 /* High priority thread */
152 vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
154 while( vlc_object_alive( p_intf ) )
156 /* Update the input */
157 if( p_input == NULL )
159 p_input = playlist_CurrentInput( p_playlist );
161 else if( p_input->b_dead || !vlc_object_alive( p_input ) )
163 vlc_object_release( p_input );
167 if( p_input == NULL )
170 msleep( INTF_IDLE_SLEEP );
175 * We now have an input
178 /* Initialize file descriptor set and timeout (0.5s) */
179 /* FIXME: arbitrary tick */
180 struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
184 struct sockaddr_storage from;
185 mtime_t i_master_system;
186 mtime_t i_client_system;
188 int i_struct_size, i_read, i_ret;
191 i_ret = poll( &ufd, 1, i_timeout );
192 if( i_ret <= 0 ) continue;
194 /* We received something */
195 i_struct_size = sizeof( from );
196 i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
197 (struct sockaddr*)&from,
198 (unsigned int *)&i_struct_size );
200 /* not sure we need the client information to sync,
201 since we are the master anyway */
202 i_client_system = ntoh64(*(int64_t *)p_data);
206 if( input_GetPcrSystem( p_input, &i_master_system ) )
209 *((int64_t *)p_data) = hton64( i_date );
210 *(((int64_t *)p_data)+1) = hton64( i_master_system );
212 /* Reply to the sender */
213 sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
214 (struct sockaddr *)&from, i_struct_size );
217 msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
218 "(date: %"PRId64")", i_client_system, i_master_system,
219 (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
220 : "non-IPv4", i_date );
225 mtime_t i_master_system;
226 mtime_t i_client_system;
227 mtime_t i_system = 0;
228 mtime_t i_send_date, i_receive_date;
229 mtime_t i_diff_date, i_master_date;
230 int i_sent, i_read, i_ret;
232 if( input_GetPcrSystem( p_input, &i_system ) )
234 msleep( INTF_IDLE_SLEEP );
238 /* Send clock request to the master */
239 i_send_date = mdate();
240 *((int64_t *)p_data) = hton64( i_system );
242 i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
245 msleep( INTF_IDLE_SLEEP );
250 i_ret = poll( &ufd, 1, i_timeout );
251 if( i_ret == 0 ) continue;
254 msleep( INTF_IDLE_SLEEP );
258 i_receive_date = mdate();
259 i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
262 msleep( INTF_IDLE_SLEEP );
266 i_master_date = ntoh64(*(int64_t *)p_data);
267 i_master_system = ntoh64(*(((int64_t *)p_data)+1)); /* system date */
269 i_diff_date = i_receive_date -
270 ((i_receive_date - i_send_date) / 2 + i_master_date);
272 if( p_input && i_master_system > 0 )
274 mtime_t i_diff_system;
276 if( input_GetPcrSystem( p_input, &i_client_system ) )
278 msleep( INTF_IDLE_SLEEP );
282 i_diff_system = i_client_system - i_master_system - i_diff_date;
283 if( i_diff_system != 0 )
285 input_ModifyPcrSystem( p_input, true, i_master_system - i_diff_date );
287 msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
288 " clock diff: %"PRId64", diff: %"PRId64"",
289 i_system, i_master_system, i_client_system,
290 i_diff_system, i_diff_date );
294 msleep( INTF_IDLE_SLEEP );
298 if( p_input ) vlc_object_release( p_input );
299 pl_Release( p_intf );
300 vlc_restorecancel( canc );