00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
00028 #include <vlc/vlc.h>
00029 #include <vlc/intf.h>
00030 #include <vlc/input.h>
00031
00032 #ifdef HAVE_UNISTD_H
00033 # include <unistd.h>
00034 #endif
00035 #ifdef HAVE_SYS_TIME_H
00036 # include <sys/time.h>
00037 #endif
00038 #ifdef HAVE_SYS_TYPES_H
00039 # include <sys/types.h>
00040 #endif
00041
00042 #include "network.h"
00043
00044 #define NETSYNC_PORT_MASTER 9875
00045 #define NETSYNC_PORT_SLAVE 9876
00046
00047
00048 #ifndef INADDR_NONE
00049 #define INADDR_NONE 0xffffffff
00050 #endif
00051
00052
00053
00054
00055 static int Activate( vlc_object_t * );
00056 static void Close ( vlc_object_t * );
00057
00058 static mtime_t GetClockRef( intf_thread_t *, mtime_t );
00059
00060 #define NETSYNC_TEXT N_( "Act as master for network synchronisation" )
00061 #define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \
00062 "act as the master client for the network synchronisation." )
00063
00064 #define MIP_TEXT N_( "Master client ip address" )
00065 #define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \
00066 "the master client used for the network synchronisation." )
00067
00068 vlc_module_begin();
00069 set_shortname( _("Netsync"));
00070 set_description( _("Network synchronisation") );
00071 set_category( CAT_INTERFACE );
00072 set_subcategory( SUBCAT_INTERFACE_CONTROL );
00073
00074 add_bool( "netsync-master", 0, NULL,
00075 NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );
00076 add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
00077 VLC_TRUE );
00078
00079 set_capability( "interface", 0 );
00080 set_callbacks( Activate, Close );
00081 vlc_module_end();
00082
00083 struct intf_sys_t
00084 {
00085 input_thread_t *p_input;
00086 };
00087
00088
00089
00090
00091 static void Run( intf_thread_t *p_intf );
00092
00093
00094
00095
00096 static int Activate( vlc_object_t *p_this )
00097 {
00098 intf_thread_t *p_intf = (intf_thread_t*)p_this;
00099
00100 msg_Info( p_intf, "Using the netsync interface module..." );
00101
00102 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
00103 if( !p_intf->p_sys )
00104 {
00105 msg_Err( p_intf, "no memory" );
00106 return VLC_ENOMEM;
00107 }
00108
00109 p_intf->p_sys->p_input = NULL;
00110
00111 p_intf->pf_run = Run;
00112 return VLC_SUCCESS;
00113 }
00114
00115
00116
00117
00118 void Close( vlc_object_t *p_this )
00119 {
00120 intf_thread_t *p_intf = (intf_thread_t*)p_this;
00121
00122 free( p_intf->p_sys );
00123 }
00124
00125
00126
00127
00128 static void Run( intf_thread_t *p_intf )
00129 {
00130 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
00131
00132 vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );
00133 char *psz_master = NULL;
00134 char p_data[MAX_MSG_LENGTH];
00135 int i_socket;
00136
00137 if( !b_master )
00138 {
00139 psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
00140 if( psz_master == NULL )
00141 {
00142 msg_Err( p_intf, "master address not specified" );
00143 return;
00144 }
00145 }
00146
00147 i_socket = net_OpenUDP( p_intf, NULL,
00148 b_master ? NETSYNC_PORT_MASTER : NETSYNC_PORT_SLAVE,
00149 b_master ? NULL : psz_master,
00150 b_master ? 0 : NETSYNC_PORT_MASTER );
00151
00152 if( psz_master ) free( psz_master );
00153
00154 if( i_socket < 0 )
00155 {
00156 msg_Err( p_intf, "failed opening UDP socket." );
00157 return;
00158 }
00159
00160
00161 vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
00162
00163 while( !p_intf->b_die )
00164 {
00165 struct timeval timeout;
00166 fd_set fds_r;
00167
00168
00169 if( p_intf->p_sys->p_input == NULL )
00170 {
00171 p_intf->p_sys->p_input =
00172 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
00173 FIND_ANYWHERE );
00174 }
00175 else if( p_intf->p_sys->p_input->b_dead )
00176 {
00177 vlc_object_release( p_intf->p_sys->p_input );
00178 p_intf->p_sys->p_input = NULL;
00179 }
00180
00181 if( p_intf->p_sys->p_input == NULL )
00182 {
00183
00184 msleep( INTF_IDLE_SLEEP );
00185 continue;
00186 }
00187
00188
00189
00190
00191
00192
00193 FD_ZERO( &fds_r );
00194 FD_SET( i_socket, &fds_r );
00195 timeout.tv_sec = 0;
00196 timeout.tv_usec = 500000;
00197
00198 if( b_master )
00199 {
00200 struct sockaddr_storage from;
00201 mtime_t i_date, i_clockref, i_master_clockref;
00202 int i_struct_size, i_read, i_ret;
00203
00204
00205 i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );
00206 if( i_ret == 0 ) continue;
00207 if( i_ret < 0 )
00208 {
00209
00210 msleep( INTF_IDLE_SLEEP );
00211 continue;
00212 }
00213
00214
00215 i_struct_size = sizeof( from );
00216 i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
00217 (struct sockaddr*)&from, &i_struct_size );
00218
00219 i_clockref = ntoh64(*(int64_t *)p_data);
00220
00221 i_date = mdate();
00222 *(int64_t *)p_data = hton64( i_date );
00223
00224 i_master_clockref = GetClockRef( p_intf, i_clockref );
00225 *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
00226
00227
00228 sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
00229 (struct sockaddr *)&from, i_struct_size );
00230
00231 #if 0
00232 msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "
00233 "(date: "I64Fd")", i_clockref, i_master_clockref,
00234 from.ss_family == AF_INET
00235 ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
00236 : "non-IPv4", i_date );
00237 #endif
00238 }
00239 else
00240 {
00241 mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
00242 mtime_t i_master_clockref, i_client_clockref, i_drift;
00243 mtime_t i_clockref = 0;
00244 int i_sent, i_read, i_ret;
00245
00246
00247 *(int64_t *)p_data = hton64( i_clockref );
00248 i_send_date = mdate();
00249
00250 i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
00251 if( i_sent <= 0 )
00252 {
00253
00254 msleep( INTF_IDLE_SLEEP );
00255 continue;
00256 }
00257
00258
00259 i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);
00260 if( i_ret == 0 ) continue;
00261 if( i_ret < 0 )
00262 {
00263
00264 msleep( INTF_IDLE_SLEEP );
00265 continue;
00266 }
00267
00268 i_receive_date = mdate();
00269
00270 i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
00271 if( i_read <= 0 )
00272 {
00273
00274 msleep( INTF_IDLE_SLEEP );
00275 continue;
00276 }
00277
00278 i_master_date = ntoh64(*(int64_t *)p_data);
00279 i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
00280
00281 i_diff_date = i_receive_date -
00282 ((i_receive_date - i_send_date) / 2 + i_master_date);
00283
00284 i_client_clockref = i_drift = 0;
00285 if( p_intf->p_sys->p_input && i_master_clockref )
00286 {
00287 i_client_clockref = GetClockRef( p_intf, i_clockref );
00288 i_drift = i_client_clockref - i_master_clockref - i_diff_date;
00289
00290
00291 if( i_client_clockref )
00292 p_intf->p_sys->p_input->i_pts_delay -= i_drift;
00293 }
00294
00295 #if 0
00296 msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "
00297 "clock diff: "I64Fd" drift: "I64Fd,
00298 i_clockref, i_master_clockref,
00299 i_client_clockref, i_diff_date, i_drift );
00300 #endif
00301
00302
00303 msleep( INTF_IDLE_SLEEP );
00304 }
00305 }
00306
00307 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
00308 net_Close( i_socket );
00309 }
00310
00311 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
00312 {
00313 input_thread_t *p_input = p_intf->p_sys->p_input;
00314 mtime_t i_ts;
00315
00316 if( !p_input || !p_input->p_es_out ) return 0;
00317
00318 if( es_out_Control( p_input->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==
00319 VLC_SUCCESS )
00320 {
00321 return i_ts;
00322 }
00323
00324 return 0;
00325 }