POTFiles update with removal/addition of files
[vlc/solaris.git] / modules / control / netsync.c
blob76a5a238d3cc2441a517e695ab8a68638b654eb6
1 /*****************************************************************************
2 * netsync.c: synchronisation between several network clients.
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
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>
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
50 #include <vlc_network.h>
52 #define NETSYNC_PORT 9875
54 /* FIXME: UGLY UGLY !! Netsync should be totally reworked */
55 #include "../../src/input/input_internal.h"
57 /*****************************************************************************
58 * Module descriptor
59 *****************************************************************************/
60 static int Activate( vlc_object_t * );
61 static void Close ( vlc_object_t * );
63 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
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?" )
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." )
75 vlc_module_begin ()
76 set_shortname( N_("Network Sync"))
77 set_description( N_("Network synchronisation") )
78 set_category( CAT_ADVANCED )
79 set_subcategory( SUBCAT_ADVANCED_MISC )
81 add_bool( "netsync-master", false, NULL,
82 NETSYNC_TEXT, NETSYNC_LONGTEXT, true )
83 add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
84 true )
86 set_capability( "interface", 0 )
87 set_callbacks( Activate, Close )
88 vlc_module_end ()
90 /*****************************************************************************
91 * Local prototypes
92 *****************************************************************************/
93 static void Run( intf_thread_t *p_intf );
95 /*****************************************************************************
96 * Activate: initialize and create stuff
97 *****************************************************************************/
98 static int Activate( vlc_object_t *p_this )
100 intf_thread_t *p_intf = (intf_thread_t*)p_this;
101 int fd;
103 if( config_GetInt( p_intf, "netsync-master" ) <= 0 )
105 char *psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
106 if( psz_master == NULL )
108 msg_Err( p_intf, "master address not specified" );
109 return VLC_EGENERIC;
111 fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 );
112 free( psz_master );
114 else
115 fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
117 if( fd == -1 )
119 msg_Err( p_intf, "Netsync socket failure" );
120 return VLC_EGENERIC;
123 p_intf->p_sys = (void *)(intptr_t)fd;
124 p_intf->pf_run = Run;
125 return VLC_SUCCESS;
128 /*****************************************************************************
129 * Close: destroy interface
130 *****************************************************************************/
131 void Close( vlc_object_t *p_this )
133 intf_thread_t *p_intf = (intf_thread_t*)p_this;
135 net_Close( (intptr_t)p_intf->p_sys );
138 /*****************************************************************************
139 * Run: interface thread
140 *****************************************************************************/
141 static void Run( intf_thread_t *p_intf )
143 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
145 input_thread_t *p_input = NULL;
146 char p_data[MAX_MSG_LENGTH];
147 int i_socket;
148 int canc = vlc_savecancel();
150 /* High priority thread */
151 vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
153 while( vlc_object_alive( p_intf ) )
155 /* Update the input */
156 if( p_input == NULL )
157 p_input =
158 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
159 FIND_ANYWHERE );
160 else if( p_input->b_dead )
162 vlc_object_release( p_input );
163 p_input = NULL;
166 if( p_input == NULL )
168 /* Wait a bit */
169 msleep( INTF_IDLE_SLEEP );
170 continue;
174 * We now have an input
177 /* Initialize file descriptor set and timeout (0.5s) */
178 /* FIXME: arbitrary tick */
179 struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
181 if( b_master )
183 struct sockaddr_storage from;
184 mtime_t i_date, i_clockref, i_master_clockref;
185 int i_struct_size, i_read, i_ret;
187 /* Don't block */
188 i_ret = poll( &ufd, 1, 500 );
189 if( i_ret == 0 ) continue;
190 if( i_ret < 0 )
192 /* Wait a bit */
193 msleep( INTF_IDLE_SLEEP );
194 continue;
197 /* We received something */
198 i_struct_size = sizeof( from );
199 i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
200 (struct sockaddr*)&from,
201 (unsigned int *)&i_struct_size );
203 i_clockref = ntoh64(*(int64_t *)p_data);
205 i_date = mdate();
206 *(int64_t *)p_data = hton64( i_date );
208 i_master_clockref = GetClockRef( p_intf, i_clockref );
209 *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
211 /* Reply to the sender */
212 sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
213 (struct sockaddr *)&from, i_struct_size );
215 #if 0
216 msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
217 "(date: %"PRId64")", i_clockref, i_master_clockref,
218 from.ss_family == AF_INET
219 ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
220 : "non-IPv4", i_date );
221 #endif
223 else
225 mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
226 mtime_t i_master_clockref, i_client_clockref, i_drift;
227 mtime_t i_clockref = 0;
228 int i_sent, i_read, i_ret;
230 /* Send clock request to the master */
231 *(int64_t *)p_data = hton64( i_clockref );
232 i_send_date = mdate();
234 i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
235 if( i_sent <= 0 )
237 /* Wait a bit */
238 msleep( INTF_IDLE_SLEEP );
239 continue;
242 /* Don't block */
243 i_ret = poll( &ufd, 1, 500 );
244 if( i_ret == 0 ) continue;
245 if( i_ret < 0 )
247 /* Wait a bit */
248 msleep( INTF_IDLE_SLEEP );
249 continue;
252 i_receive_date = mdate();
254 i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
255 if( i_read <= 0 )
257 /* Wait a bit */
258 msleep( INTF_IDLE_SLEEP );
259 continue;
262 i_master_date = ntoh64(*(int64_t *)p_data);
263 i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
265 i_diff_date = i_receive_date -
266 ((i_receive_date - i_send_date) / 2 + i_master_date);
268 i_client_clockref = i_drift = 0;
269 if( p_input && i_master_clockref )
271 i_client_clockref = GetClockRef( p_intf, i_clockref );
272 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
274 /* Update our clock to match the master's one */
275 if( i_client_clockref )
276 p_input->i_pts_delay -= i_drift;
279 #if 0
280 msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64", "
281 "clock diff: %"PRId64" drift: %"PRId64,
282 i_clockref, i_master_clockref,
283 i_client_clockref, i_diff_date, i_drift );
284 #endif
286 /* Wait a bit */
287 msleep( INTF_IDLE_SLEEP );
291 if( p_input ) vlc_object_release( p_input );
292 vlc_restorecancel( canc );
295 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
297 input_thread_t *p_input = p_intf->p_sys->p_input;
298 mtime_t i_ts;
300 if( !p_input || !p_input->p->p_es_out ) return 0;
302 if( es_out_Control( p_input->p->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==
303 VLC_SUCCESS )
305 return i_ts;
308 return 0;