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