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