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