]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
5652d0e15a3c32975a8dcca2540bf14c4597a591
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
34 #include <vlc_input.h>
35 #include <vlc_es_out.h>
36
37 #ifdef HAVE_UNISTD_H
38 #    include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_TYPES_H
41 #   include <sys/types.h>
42 #endif
43 #ifdef HAVE_POLL
44 #   include <poll.h>
45 #endif
46
47 #include <vlc_network.h>
48
49 #define NETSYNC_PORT 9875
50
51 /* FIXME: UGLY UGLY !! Netsync should be totally reworked */
52 #include "../../src/input/input_internal.h"
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Activate( vlc_object_t * );
58 static void Close   ( vlc_object_t * );
59
60 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
61
62 /// \bug [String] This string is BAD.
63 #define NETSYNC_TEXT N_( "Act as master" )
64 #define NETSYNC_LONGTEXT N_( "Should " \
65   "act as the master client for the network synchronisation?" )
66
67 /// \bug [String] This string is BAD.
68 #define MIP_TEXT N_( "Master client ip address" )
69 #define MIP_LONGTEXT N_( "IP address of " \
70   "the master client used for the network synchronisation." )
71
72 vlc_module_begin ()
73     set_shortname( N_("Network Sync"))
74     set_description( N_("Network synchronisation") )
75     set_category( CAT_ADVANCED )
76     set_subcategory( SUBCAT_ADVANCED_MISC )
77
78     add_bool( "netsync-master", false, NULL,
79               NETSYNC_TEXT, NETSYNC_LONGTEXT, true )
80     add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
81                 true )
82
83     set_capability( "interface", 0 )
84     set_callbacks( Activate, Close )
85 vlc_module_end ()
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     int fd;
99
100     if( !var_InheritInteger( p_intf, "netsync-master" ) )
101     {
102         char *psz_master = var_InheritString( p_intf, "netsync-master-ip" );
103         if( psz_master == NULL )
104         {
105             msg_Err( p_intf, "master address not specified" );
106             return VLC_EGENERIC;
107         }
108         fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 );
109         free( psz_master );
110     }
111     else
112         fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
113
114     if( fd == -1 )
115     {
116         msg_Err( p_intf, "Netsync socket failure" );
117         return VLC_EGENERIC;
118     }
119
120     p_intf->p_sys = (void *)(intptr_t)fd;
121     p_intf->pf_run = Run;
122     return VLC_SUCCESS;
123 }
124
125 /*****************************************************************************
126  * Close: destroy interface
127  *****************************************************************************/
128 void Close( vlc_object_t *p_this )
129 {
130     intf_thread_t *p_intf = (intf_thread_t*)p_this;
131
132     net_Close( (intptr_t)p_intf->p_sys );
133 }
134
135 /*****************************************************************************
136  * Run: interface thread
137  *****************************************************************************/
138 static void Run( intf_thread_t *p_intf )
139 {
140 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
141
142     input_thread_t *p_input = NULL;
143     char p_data[MAX_MSG_LENGTH];
144     int i_socket;
145     int canc = vlc_savecancel();
146
147     /* High priority thread */
148     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
149
150     while( vlc_object_alive( p_intf ) )
151     {
152         /* Update the input */
153         if( p_input == NULL )
154             p_input =
155                 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
156                                                    FIND_ANYWHERE );
157         else if( p_input->b_dead )
158         {
159             vlc_object_release( p_input );
160             p_input = NULL;
161         }
162
163         if( p_input == NULL )
164         {
165             /* Wait a bit */
166             msleep( INTF_IDLE_SLEEP );
167             continue;
168         }
169
170         /*
171          * We now have an input
172          */
173
174         /* Initialize file descriptor set and timeout (0.5s) */
175         /* FIXME: arbitrary tick */
176         struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
177
178         if( b_master )
179         {
180             struct sockaddr_storage from;
181             mtime_t i_date, i_clockref, i_master_clockref;
182             int i_struct_size, i_read, i_ret;
183
184             /* Don't block */
185             i_ret = poll( &ufd, 1, 500 );
186             if( i_ret == 0 ) continue;
187             if( i_ret < 0 )
188             {
189                 /* Wait a bit */
190                 msleep( INTF_IDLE_SLEEP );
191                 continue;
192             }
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             i_clockref = ntoh64(*(int64_t *)p_data);
201
202             i_date = mdate();
203             *(int64_t *)p_data = hton64( i_date );
204
205             i_master_clockref = GetClockRef( p_intf, i_clockref );
206             *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
207
208             /* Reply to the sender */
209             sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
210                     (struct sockaddr *)&from, i_struct_size );
211
212 #if 0
213             msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
214                      "(date: %"PRId64")", i_clockref, i_master_clockref,
215                      from.ss_family == AF_INET
216                      ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
217                      : "non-IPv4", i_date );
218 #endif
219         }
220         else
221         {
222             mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
223             mtime_t i_master_clockref, i_client_clockref, i_drift;
224             mtime_t i_clockref = 0;
225             int i_sent, i_read, i_ret;
226
227             /* Send clock request to the master */
228             *(int64_t *)p_data = hton64( i_clockref );
229             i_send_date = mdate();
230
231             i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
232             if( i_sent <= 0 )
233             {
234                 /* Wait a bit */
235                 msleep( INTF_IDLE_SLEEP );
236                 continue;
237             }
238
239             /* Don't block */
240             i_ret = poll( &ufd, 1, 500 );
241             if( i_ret == 0 ) continue;
242             if( i_ret < 0 )
243             {
244                 /* Wait a bit */
245                 msleep( INTF_IDLE_SLEEP );
246                 continue;
247             }
248
249             i_receive_date = mdate();
250
251             i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
252             if( i_read <= 0 )
253             {
254                 /* Wait a bit */
255                 msleep( INTF_IDLE_SLEEP );
256                 continue;
257             }
258
259             i_master_date = ntoh64(*(int64_t *)p_data);
260             i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
261
262             i_diff_date = i_receive_date -
263                           ((i_receive_date - i_send_date) / 2 + i_master_date);
264
265             i_client_clockref = i_drift = 0;
266             if( p_input && i_master_clockref )
267             {
268                 i_client_clockref = GetClockRef( p_intf, i_clockref );
269                 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
270
271                 /* Update our clock to match the master's one */
272                 if( i_client_clockref )
273                     p_input->i_pts_delay -= i_drift;
274             }
275
276 #if 0
277             msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64", "
278                      "clock diff: %"PRId64" drift: %"PRId64,
279                      i_clockref, i_master_clockref,
280                      i_client_clockref, i_diff_date, i_drift );
281 #endif
282
283             /* Wait a bit */
284             msleep( INTF_IDLE_SLEEP );
285         }
286     }
287
288     if( p_input ) vlc_object_release( p_input );
289     vlc_restorecancel( canc );
290 }
291
292 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
293 {
294     input_thread_t *p_input = p_intf->p_sys->p_input;
295     mtime_t i_ts;
296
297     if( !p_input || !p_input->p->p_es_out ) return 0;
298
299     if( es_out_Control( p_input->p->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==
300         VLC_SUCCESS )
301     {
302         return i_ts;
303     }
304
305     return 0;
306 }