]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
Copyright fixes
[vlc] / modules / control / netsync.c
1 /*****************************************************************************
2  * netsync.c: synchronisation between several network clients.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <vlc/vlc.h>
29 #include <vlc/intf.h>
30 #include <vlc/input.h>
31
32 #ifdef HAVE_UNISTD_H
33 #    include <unistd.h>
34 #endif
35
36 #ifdef HAVE_SYS_TIME_H
37 #    include <sys/time.h>
38 #endif
39 #include <sys/types.h>
40
41 #ifdef WIN32
42 #   include <winsock2.h>
43 #   include <ws2tcpip.h>
44 #   ifndef IN_MULTICAST
45 #       define IN_MULTICAST(a) IN_CLASSD(a)
46 #   endif
47 #else
48 #   include <sys/socket.h>
49 #   include <netinet/in.h>
50 #   if HAVE_ARPA_INET_H
51 #      include <arpa/inet.h>
52 #   elif defined( SYS_BEOS )
53 #      include <net/netdb.h>
54 #   endif
55 #endif
56
57 #ifdef UNDER_CE
58 #   define close(a) CloseHandle(a);
59 #elif defined( WIN32 )
60 #   define close(a) closesocket(a);
61 #endif
62
63 #include "network.h"
64
65 #define NETSYNC_PORT_MASTER 9875
66 #define NETSYNC_PORT_SLAVE  9876
67
68 /* Needed for Solaris */
69 #ifndef INADDR_NONE
70 #define INADDR_NONE 0xffffffff
71 #endif
72
73 /*****************************************************************************
74  * Module descriptor
75  *****************************************************************************/
76 static int  Activate( vlc_object_t * );
77 static void Close   ( vlc_object_t * );
78
79 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
80
81 #define NETSYNC_TEXT N_( "Act as master for network synchronisation" )
82 #define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \
83   "act as the master client for the network synchronisation." )
84
85 #define MIP_TEXT N_( "Master client ip address" )
86 #define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \
87   "the master client used for the network synchronisation." )
88
89 vlc_module_begin();
90     set_shortname( _("Netsync"));
91     set_description( _("Network synchronisation") );
92     set_category( CAT_INTERFACE );
93     set_subcategory( SUBCAT_INTERFACE_CONTROL );
94
95     add_bool( "netsync-master", 0, NULL,
96               NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );
97     add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
98                 VLC_TRUE );
99
100     set_capability( "interface", 0 );
101     set_callbacks( Activate, Close );
102 vlc_module_end();
103
104 struct intf_sys_t
105 {
106     input_thread_t *p_input;
107 };
108
109 /*****************************************************************************
110  * Local prototypes
111  *****************************************************************************/
112 static void Run( intf_thread_t *p_intf );
113
114 /*****************************************************************************
115  * Activate: initialize and create stuff
116  *****************************************************************************/
117 static int Activate( vlc_object_t *p_this )
118 {
119     intf_thread_t *p_intf = (intf_thread_t*)p_this;
120
121     msg_Info( p_intf, "Using the netsync interface module..." );
122
123     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
124     if( !p_intf->p_sys )
125     {
126         msg_Err( p_intf, "no memory" );
127         return VLC_ENOMEM;
128     }
129
130     p_intf->p_sys->p_input = NULL;
131
132     p_intf->pf_run = Run;
133     return VLC_SUCCESS;
134 }
135
136 /*****************************************************************************
137  * Close: destroy interface
138  *****************************************************************************/
139 void Close( vlc_object_t *p_this )
140 {
141     intf_thread_t *p_intf = (intf_thread_t*)p_this;
142
143     free( p_intf->p_sys );
144 }
145
146 /*****************************************************************************
147  * Run: interface thread
148  *****************************************************************************/
149 static void Run( intf_thread_t *p_intf )
150 {
151 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
152
153     vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );
154     char *psz_master = NULL;
155     char p_data[MAX_MSG_LENGTH];
156     int i_socket;
157
158     if( !b_master )
159     {
160         psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
161         if( psz_master == NULL )
162         {
163             msg_Err( p_intf, "master address not specified" );
164             return;
165         }
166     }
167
168     i_socket = net_OpenUDP( p_intf, NULL,
169                    b_master ? NETSYNC_PORT_MASTER : NETSYNC_PORT_SLAVE,
170                    b_master ? NULL : psz_master,
171                    b_master ? 0 : NETSYNC_PORT_MASTER );
172
173     if( psz_master ) free( psz_master );
174
175     if( i_socket < 0 )
176     {
177         msg_Err( p_intf, "failed opening UDP socket." );
178         return;
179     }
180
181     /* High priority thread */
182     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
183
184     while( !p_intf->b_die )
185     {
186         struct timeval timeout;
187         fd_set fds_r;
188
189         /* Update the input */
190         if( p_intf->p_sys->p_input == NULL )
191         {
192             p_intf->p_sys->p_input =
193                 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
194                                                    FIND_ANYWHERE );
195         }
196         else if( p_intf->p_sys->p_input->b_dead )
197         {
198             vlc_object_release( p_intf->p_sys->p_input );
199             p_intf->p_sys->p_input = NULL;
200         }
201
202         if( p_intf->p_sys->p_input == NULL )
203         {
204             /* Wait a bit */
205             msleep( INTF_IDLE_SLEEP );
206             continue;
207         }
208
209         /*
210          * We now have an input
211          */
212
213         /* Initialize file descriptor set and timeout (0.5s) */
214         FD_ZERO( &fds_r );
215         FD_SET( i_socket, &fds_r );
216         timeout.tv_sec = 0;
217         timeout.tv_usec = 500000;
218
219         if( b_master )
220         {
221             struct sockaddr_storage from;
222             mtime_t i_date, i_clockref, i_master_clockref;
223             int i_struct_size, i_read, i_ret;
224
225             /* Don't block */
226             i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );
227             if( i_ret == 0 ) continue;
228             if( i_ret < 0 )
229             {
230                 /* Wait a bit */
231                 msleep( INTF_IDLE_SLEEP );
232                 continue;
233             }
234
235             /* We received something */
236             i_struct_size = sizeof( from );
237             i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
238                                (struct sockaddr*)&from, &i_struct_size );
239
240             i_clockref = ntoh64(*(int64_t *)p_data);
241
242             i_date = mdate();
243             *(int64_t *)p_data = hton64( i_date );
244
245             i_master_clockref = GetClockRef( p_intf, i_clockref );
246             *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
247
248             /* Reply to the sender */
249             sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
250                     (struct sockaddr *)&from, i_struct_size );
251
252 #if 0
253             msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "
254                      "(date: "I64Fd")", i_clockref, i_master_clockref,
255                      from.ss_family == AF_INET
256                      ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
257                      : "non-IPv4", i_date );
258 #endif
259         }
260         else
261         {
262             mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
263             mtime_t i_master_clockref, i_client_clockref, i_drift;
264             mtime_t i_clockref = 0;
265             int i_sent, i_read, i_ret;
266
267             /* Send clock request to the master */
268             *(int64_t *)p_data = hton64( i_clockref );
269             i_send_date = mdate();
270
271             i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
272             if( i_sent <= 0 )
273             {
274                 /* Wait a bit */
275                 msleep( INTF_IDLE_SLEEP );
276                 continue;
277             }
278
279             /* Don't block */
280             i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);
281             if( i_ret == 0 ) continue;
282             if( i_ret < 0 )
283             {
284                 /* Wait a bit */
285                 msleep( INTF_IDLE_SLEEP );
286                 continue;
287             }
288
289             i_receive_date = mdate();
290
291             i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
292             if( i_read <= 0 )
293             {
294                 /* Wait a bit */
295                 msleep( INTF_IDLE_SLEEP );
296                 continue;
297             }
298
299             i_master_date = ntoh64(*(int64_t *)p_data);
300             i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
301
302             i_diff_date = i_receive_date -
303                           ((i_receive_date - i_send_date) / 2 + i_master_date);
304
305             i_client_clockref = i_drift = 0;
306             if( p_intf->p_sys->p_input && i_master_clockref )
307             {
308                 i_client_clockref = GetClockRef( p_intf, i_clockref );
309                 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
310
311                 /* Update our clock to match the master's one */
312                 if( i_client_clockref )
313                     p_intf->p_sys->p_input->i_pts_delay -= i_drift;
314             }
315
316 #if 0
317             msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "
318                      "clock diff: "I64Fd" drift: "I64Fd,
319                      i_clockref, i_master_clockref, 
320                      i_client_clockref, i_diff_date, i_drift );
321 #endif
322
323             /* Wait a bit */
324             msleep( INTF_IDLE_SLEEP );
325         }
326     }
327
328     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
329     net_Close( i_socket );
330 }
331
332 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
333 {
334     input_thread_t *p_input = p_intf->p_sys->p_input;
335     mtime_t i_ts;
336
337     if( !p_input || !p_input->p_es_out ) return 0;
338
339     if( es_out_Control( p_input->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==
340         VLC_SUCCESS )
341     {
342         return i_ts;
343     }
344
345     return 0;
346 }