netsync.c 10.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * netsync.c: synchronisation between several network clients.
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc/vlc.h>
32
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
33 34 35
#include <vlc_interface.h>
#include <vlc_input.h>
#include <vlc_es_out.h>
36 37 38 39 40 41 42

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
43 44
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
45
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46
#ifdef HAVE_POLL
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
47 48
#   include <poll.h>
#endif
49

Clément Stenac's avatar
Clément Stenac committed
50
#include <vlc_network.h>
51

52
#define NETSYNC_PORT 9875
53 54 55 56 57 58

/* Needed for Solaris */
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

Clément Stenac's avatar
Clément Stenac committed
59 60 61
/* FIXME: UGLY UGLY !! Netsync should be totally reworked */
#include "../../src/input/input_internal.h"

62 63 64 65 66 67 68 69
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Activate( vlc_object_t * );
static void Close   ( vlc_object_t * );

static mtime_t GetClockRef( intf_thread_t *, mtime_t );

Clément Stenac's avatar
Clément Stenac committed
70
/// \bug [String] This string is BAD.
71 72 73
#define NETSYNC_TEXT N_( "Act as master" )
#define NETSYNC_LONGTEXT N_( "Should " \
  "act as the master client for the network synchronisation?" )
74

Clément Stenac's avatar
Clément Stenac committed
75
/// \bug [String] This string is BAD.
76
#define MIP_TEXT N_( "Master client ip address" )
77
#define MIP_LONGTEXT N_( "IP address of " \
78 79 80
  "the master client used for the network synchronisation." )

vlc_module_begin();
81 82
    set_shortname( N_("Network Sync"));
    set_description( N_("Network synchronisation") );
83 84
    set_category( CAT_ADVANCED );
    set_subcategory( SUBCAT_ADVANCED_MISC );
85 86

    add_bool( "netsync-master", 0, NULL,
87
              NETSYNC_TEXT, NETSYNC_LONGTEXT, true );
88
    add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
89
                true );
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    set_capability( "interface", 0 );
    set_callbacks( Activate, Close );
vlc_module_end();

struct intf_sys_t
{
    input_thread_t *p_input;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void Run( intf_thread_t *p_intf );

/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;

    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( !p_intf->p_sys )
    {
        msg_Err( p_intf, "no memory" );
        return VLC_ENOMEM;
    }

    p_intf->p_sys->p_input = NULL;

    p_intf->pf_run = Run;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface
 *****************************************************************************/
void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;

    free( p_intf->p_sys );
}

/*****************************************************************************
 * Run: interface thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
#define MAX_MSG_LENGTH (2 * sizeof(int64_t))

142
    bool b_master = config_GetInt( p_intf, "netsync-master" );
143
    char *psz_master = NULL;
144 145 146 147 148 149 150 151 152 153 154 155 156
    char p_data[MAX_MSG_LENGTH];
    int i_socket;

    if( !b_master )
    {
        psz_master = config_GetPsz( p_intf, "netsync-master-ip" );
        if( psz_master == NULL )
        {
            msg_Err( p_intf, "master address not specified" );
            return;
        }
    }

157
    if( b_master )
158
        i_socket = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
159
    else
160
        i_socket = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, 0 );
161

162
    free( psz_master );
163 164 165

    if( i_socket < 0 )
    {
166
        msg_Err( p_intf, "failed opening UDP socket" ); /* str review: is this good enough? */
167 168 169 170 171 172
        return;
    }

    /* High priority thread */
    vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );

173
    while( !intf_ShouldDie( p_intf ) )
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    {
        /* Update the input */
        if( p_intf->p_sys->p_input == NULL )
        {
            p_intf->p_sys->p_input =
                (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
                                                   FIND_ANYWHERE );
        }
        else if( p_intf->p_sys->p_input->b_dead )
        {
            vlc_object_release( p_intf->p_sys->p_input );
            p_intf->p_sys->p_input = NULL;
        }

        if( p_intf->p_sys->p_input == NULL )
        {
            /* Wait a bit */
            msleep( INTF_IDLE_SLEEP );
            continue;
        }

        /*
         * We now have an input
         */

        /* Initialize file descriptor set and timeout (0.5s) */
200 201
        /* FIXME: arbitrary tick */
        struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
202 203 204 205 206 207 208 209

        if( b_master )
        {
            struct sockaddr_storage from;
            mtime_t i_date, i_clockref, i_master_clockref;
            int i_struct_size, i_read, i_ret;

            /* Don't block */
210
            i_ret = poll( &ufd, 1, 500 );
211 212 213 214 215 216 217 218 219 220 221
            if( i_ret == 0 ) continue;
            if( i_ret < 0 )
            {
                /* Wait a bit */
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            /* We received something */
            i_struct_size = sizeof( from );
            i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
222
                               (struct sockaddr*)&from,
223
                               (unsigned int *)&i_struct_size );
224 225 226 227 228 229 230 231 232 233 234 235 236

            i_clockref = ntoh64(*(int64_t *)p_data);

            i_date = mdate();
            *(int64_t *)p_data = hton64( i_date );

            i_master_clockref = GetClockRef( p_intf, i_clockref );
            *(((int64_t *)p_data)+1) = hton64( i_master_clockref );

            /* Reply to the sender */
            sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
                    (struct sockaddr *)&from, i_struct_size );

237
#if 0
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
238 239
            msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
                     "(date: %"PRId64")", i_clockref, i_master_clockref,
240 241 242
                     from.ss_family == AF_INET
                     ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
                     : "non-IPv4", i_date );
243
#endif
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        }
        else
        {
            mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;
            mtime_t i_master_clockref, i_client_clockref, i_drift;
            mtime_t i_clockref = 0;
            int i_sent, i_read, i_ret;

            /* Send clock request to the master */
            *(int64_t *)p_data = hton64( i_clockref );
            i_send_date = mdate();

            i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
            if( i_sent <= 0 )
            {
                /* Wait a bit */
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            /* Don't block */
265
            i_ret = poll( &ufd, 1, 500 );
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            if( i_ret == 0 ) continue;
            if( i_ret < 0 )
            {
                /* Wait a bit */
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            i_receive_date = mdate();

            i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
            if( i_read <= 0 )
            {
                /* Wait a bit */
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            i_master_date = ntoh64(*(int64_t *)p_data);
            i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));

            i_diff_date = i_receive_date -
                          ((i_receive_date - i_send_date) / 2 + i_master_date);

            i_client_clockref = i_drift = 0;
            if( p_intf->p_sys->p_input && i_master_clockref )
            {
                i_client_clockref = GetClockRef( p_intf, i_clockref );
                i_drift = i_client_clockref - i_master_clockref - i_diff_date;

                /* Update our clock to match the master's one */
                if( i_client_clockref )
                    p_intf->p_sys->p_input->i_pts_delay -= i_drift;
            }

301
#if 0
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
302 303
            msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64", "
                     "clock diff: %"PRId64" drift: %"PRId64,
304
                     i_clockref, i_master_clockref,
305
                     i_client_clockref, i_diff_date, i_drift );
306
#endif
307 308 309 310 311 312 313 314 315 316 317 318 319

            /* Wait a bit */
            msleep( INTF_IDLE_SLEEP );
        }
    }

    if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
    net_Close( i_socket );
}

static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
{
    input_thread_t *p_input = p_intf->p_sys->p_input;
320
    mtime_t i_ts;
321

Clément Stenac's avatar
Clément Stenac committed
322
    if( !p_input || !p_input->p->p_es_out ) return 0;
323

Clément Stenac's avatar
Clément Stenac committed
324
    if( es_out_Control( p_input->p->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==
325 326 327 328
        VLC_SUCCESS )
    {
        return i_ts;
    }
329 330 331

    return 0;
}