Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

netsync.c

00001 /*****************************************************************************
00002  * netsync.c: synchronisation between several network clients.
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: netsync.c 12657 2005-09-22 21:23:35Z gbazin $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
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 /* Needed for Solaris */
00048 #ifndef INADDR_NONE
00049 #define INADDR_NONE 0xffffffff
00050 #endif
00051 
00052 /*****************************************************************************
00053  * Module descriptor
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  * Local prototypes
00090  *****************************************************************************/
00091 static void Run( intf_thread_t *p_intf );
00092 
00093 /*****************************************************************************
00094  * Activate: initialize and create stuff
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  * Close: destroy interface
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  * Run: interface thread
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     /* High priority thread */
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         /* Update the input */
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             /* Wait a bit */
00184             msleep( INTF_IDLE_SLEEP );
00185             continue;
00186         }
00187 
00188         /*
00189          * We now have an input
00190          */
00191 
00192         /* Initialize file descriptor set and timeout (0.5s) */
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             /* Don't block */
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                 /* Wait a bit */
00210                 msleep( INTF_IDLE_SLEEP );
00211                 continue;
00212             }
00213 
00214             /* We received something */
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             /* Reply to the sender */
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             /* Send clock request to the master */
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                 /* Wait a bit */
00254                 msleep( INTF_IDLE_SLEEP );
00255                 continue;
00256             }
00257 
00258             /* Don't block */
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                 /* Wait a bit */
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                 /* Wait a bit */
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                 /* Update our clock to match the master's one */
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             /* Wait a bit */
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 }

Generated on Tue Dec 20 10:14:31 2005 for vlc-0.8.4a by  doxygen 1.4.2