]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
Cosmetics (netsync).
[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  Open ( 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( Open, 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 Open( 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 = (intptr_t)p_intf->p_sys;
144
145     playlist_t *p_playlist = pl_Hold( p_intf );
146     int i_timeout = var_InheritInteger( p_intf, "netsync-timeout" );
147     if( i_timeout < 500 )
148         i_timeout = 500;
149     bool b_master = var_InheritBool( p_intf, "netsync-master" );
150
151     /* High priority thread */
152     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
153
154     while( vlc_object_alive( p_intf ) )
155     {
156         /* Update the input */
157         if( p_input == NULL )
158         {
159             p_input = playlist_CurrentInput( p_playlist );
160         }
161         else if( p_input->b_dead || !vlc_object_alive( p_input ) )
162         {
163             vlc_object_release( p_input );
164             p_input = NULL;
165         }
166
167         if( p_input == NULL )
168         {
169             /* Wait a bit */
170             msleep( INTF_IDLE_SLEEP );
171             continue;
172         }
173
174         /*
175          * We now have an input
176          */
177
178         /* Initialize file descriptor set and timeout (0.5s) */
179         /* FIXME: arbitrary tick */
180         struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
181
182         if( b_master )
183         {
184             struct sockaddr_storage from;
185             mtime_t i_master_system;
186             mtime_t i_client_system;
187             mtime_t i_date;
188             int i_struct_size, i_read, i_ret;
189
190             /* Don't block */
191             i_ret = poll( &ufd, 1, i_timeout );
192             if( i_ret <= 0 ) continue;
193
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 );
199
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);
203
204             i_date = mdate();
205
206             if( input_GetPcrSystem( p_input, &i_master_system ) )
207                 continue;
208
209             *((int64_t *)p_data) = hton64( i_date );
210             *(((int64_t *)p_data)+1) = hton64( i_master_system );
211
212             /* Reply to the sender */
213             sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
214                     (struct sockaddr *)&from, i_struct_size );
215
216 #if 0
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 );
221 #endif
222         }
223         else
224         {
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;
231
232             if( input_GetPcrSystem( p_input, &i_system ) )
233             {
234                 msleep( INTF_IDLE_SLEEP );
235                 continue;
236             }
237
238             /* Send clock request to the master */
239             i_send_date = mdate();
240             *((int64_t *)p_data) = hton64( i_system );
241
242             i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
243             if( i_sent <= 0 )
244             {
245                 msleep( INTF_IDLE_SLEEP );
246                 continue;
247             }
248
249             /* Don't block */
250             i_ret = poll( &ufd, 1, i_timeout );
251             if( i_ret == 0 ) continue;
252             if( i_ret < 0 )
253             {
254                 msleep( INTF_IDLE_SLEEP );
255                 continue;
256             }
257
258             i_receive_date = mdate();
259             i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
260             if( i_read <= 0 )
261             {
262                 msleep( INTF_IDLE_SLEEP );
263                 continue;
264             }
265
266             i_master_date = ntoh64(*(int64_t *)p_data);
267             i_master_system = ntoh64(*(((int64_t *)p_data)+1)); /* system date */
268
269             i_diff_date = i_receive_date -
270                           ((i_receive_date - i_send_date) / 2 + i_master_date);
271
272             if( p_input && i_master_system > 0 )
273             {
274                 mtime_t i_diff_system;
275
276                 if( input_GetPcrSystem( p_input, &i_client_system ) )
277                 {
278                     msleep( INTF_IDLE_SLEEP );
279                     continue;
280                 }
281
282                 i_diff_system = i_client_system - i_master_system - i_diff_date;
283                 if( i_diff_system != 0 )
284                 {
285                     input_ModifyPcrSystem( p_input, true, i_master_system - i_diff_date );
286 #if 0
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 );
291 #endif
292                 }
293             }
294             msleep( INTF_IDLE_SLEEP );
295         }
296     }
297
298     if( p_input ) vlc_object_release( p_input );
299     pl_Release( p_intf );
300     vlc_restorecancel( canc );
301 }
302