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