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