]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
netsync: rewrite
[vlc] / modules / control / netsync.c
1 /*****************************************************************************
2  * netsync.c: synchronisation between several network clients.
3  *****************************************************************************
4  * Copyright (C) 2004-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Jean-Paul Saman <jpsaman@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
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>
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>
40 #endif
41 #include <sys/types.h>
42 #ifdef HAVE_POLL
43 #   include <poll.h>
44 #endif
45
46 #include <vlc_network.h>
47
48 #define NETSYNC_PORT 9875
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 static int  Activate( vlc_object_t * );
54 static void Close   ( vlc_object_t * );
55
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" )
60
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." )
64
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." )
68
69 vlc_module_begin ()
70     set_shortname( N_("Network Sync"))
71     set_description( N_("Network synchronisation") )
72     set_category( CAT_ADVANCED )
73     set_subcategory( SUBCAT_ADVANCED_MISC )
74
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,
78                 true )
79     add_integer( "netsync-timeout", 500, NULL,
80                  NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true )
81
82     set_capability( "interface", 0 )
83     set_callbacks( Activate, Close )
84 vlc_module_end ()
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 static void Run( intf_thread_t *p_intf );
90
91 /*****************************************************************************
92  * Activate: initialize and create stuff
93  *****************************************************************************/
94 static int Activate( vlc_object_t *p_this )
95 {
96     intf_thread_t *p_intf = (intf_thread_t*)p_this;
97     int fd;
98
99     if( !var_InheritBool( p_intf, "netsync-master" ) )
100     {
101         char *psz_master = var_InheritString( p_intf, "netsync-master-ip" );
102         if( psz_master == NULL )
103         {
104             msg_Err( p_intf, "master address not specified" );
105             return VLC_EGENERIC;
106         }
107         fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 );
108         free( psz_master );
109     }
110     else
111         fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
112
113     if( fd == -1 )
114     {
115         msg_Err( p_intf, "Netsync socket failure" );
116         return VLC_EGENERIC;
117     }
118
119     p_intf->p_sys = (void *)(intptr_t)fd;
120     p_intf->pf_run = Run;
121     return VLC_SUCCESS;
122 }
123
124 /*****************************************************************************
125  * Close: destroy interface
126  *****************************************************************************/
127 void Close( vlc_object_t *p_this )
128 {
129     intf_thread_t *p_intf = (intf_thread_t*)p_this;
130
131     net_Close( (intptr_t)p_intf->p_sys );
132 }
133
134 /*****************************************************************************
135  * Run: interface thread
136  *****************************************************************************/
137 static void Run( intf_thread_t *p_intf )
138 {
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;
144
145     playlist_t *p_playlist = pl_Hold( p_intf );
146     int i_timeout = __MIN( 500, var_InheritInteger( p_intf, "netsync-timeout" ) );
147     bool b_master = var_InheritBool( p_intf, "netsync-master" );
148     i_socket = (intptr_t)p_intf->p_sys;
149
150     /* High priority thread */
151     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
152
153     while( vlc_object_alive( p_intf ) )
154     {
155         /* Update the input */
156         if( p_input == NULL )
157         {
158             p_input = playlist_CurrentInput( p_playlist );
159         }
160         else if( p_input->b_dead || !vlc_object_alive( p_input ) )
161         {
162             vlc_object_release( p_input );
163             p_input = NULL;
164         }
165
166         if( p_input == NULL )
167         {
168             /* Wait a bit */
169             msleep( INTF_IDLE_SLEEP );
170             continue;
171         }
172
173         /*
174          * We now have an input
175          */
176
177         /* Initialize file descriptor set and timeout (0.5s) */
178         /* FIXME: arbitrary tick */
179         struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
180
181         if( b_master )
182         {
183             struct sockaddr_storage from;
184             mtime_t i_master_system;
185             mtime_t i_client_system;
186             mtime_t i_date;
187             int i_struct_size, i_read, i_ret;
188
189             /* Don't block */
190             i_ret = poll( &ufd, 1, i_timeout );
191             if( i_ret <= 0 ) continue;
192
193             /* We received something */
194             i_struct_size = sizeof( from );
195             i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
196                                (struct sockaddr*)&from,
197                                (unsigned int *)&i_struct_size );
198
199             /* not sure we need the client information to sync,
200                since we are the master anyway */
201             i_client_system = ntoh64(*(int64_t *)p_data);
202
203             i_date = mdate();
204
205             if( input_GetPcrSystem( p_input, &i_master_system ) )
206                 continue;
207
208             *((int64_t *)p_data) = hton64( i_date );
209             *(((int64_t *)p_data)+1) = hton64( i_master_system );
210
211             /* Reply to the sender */
212             sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
213                     (struct sockaddr *)&from, i_struct_size );
214
215 #if 0
216             msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
217                      "(date: %"PRId64")", i_client_system, i_master_system,
218                      (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
219                      : "non-IPv4", i_date );
220 #endif
221         }
222         else
223         {
224             mtime_t i_master_system;
225             mtime_t i_client_system;
226             mtime_t i_system = 0;
227             mtime_t i_send_date, i_receive_date;
228             mtime_t i_diff_date, i_master_date;
229             int i_sent, i_read, i_ret;
230
231             if( input_GetPcrSystem( p_input, &i_system ) )
232             {
233                 msleep( INTF_IDLE_SLEEP );
234                 continue;
235             }
236
237             /* Send clock request to the master */
238             i_send_date = mdate();
239             *((int64_t *)p_data) = hton64( i_system );
240
241             i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
242             if( i_sent <= 0 )
243             {
244                 msleep( INTF_IDLE_SLEEP );
245                 continue;
246             }
247
248             /* Don't block */
249             i_ret = poll( &ufd, 1, i_timeout );
250             if( i_ret == 0 ) continue;
251             if( i_ret < 0 )
252             {
253                 msleep( INTF_IDLE_SLEEP );
254                 continue;
255             }
256
257             i_receive_date = mdate();
258             i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
259             if( i_read <= 0 )
260             {
261                 msleep( INTF_IDLE_SLEEP );
262                 continue;
263             }
264
265             i_master_date = ntoh64(*(int64_t *)p_data);
266             i_master_system = ntoh64(*(((int64_t *)p_data)+1)); /* system date */
267
268             i_diff_date = i_receive_date -
269                           ((i_receive_date - i_send_date) / 2 + i_master_date);
270
271             if( p_input && i_master_system > 0 )
272             {
273                 mtime_t i_diff_system;
274
275                 if( input_GetPcrSystem( p_input, &i_client_system ) )
276                 {
277                     msleep( INTF_IDLE_SLEEP );
278                     continue;
279                 }
280
281                 i_diff_system = i_client_system - i_master_system - i_diff_date;
282                 if( i_diff_system != 0 )
283                 {
284                     input_ModifyPcrSystem( p_input, true, i_master_system - i_diff_date );
285 #if 0
286                     msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
287                              " clock diff: %"PRId64", diff: %"PRId64"",
288                              i_system, i_master_system, i_client_system,
289                              i_diff_system, i_diff_date );
290 #endif
291                 }
292             }
293             msleep( INTF_IDLE_SLEEP );
294         }
295     }
296
297     if( p_input ) vlc_object_release( p_input );
298     pl_Release( p_intf );
299     vlc_restorecancel( canc );
300 }
301