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