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