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