netsync.c 9.93 KB
Newer Older
1 2 3
/*****************************************************************************
 * netsync.c: synchronisation between several network clients.
 *****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
4
 * Copyright (C) 2004-2009 the VideoLAN team
5 6 7
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
8
 *          Jean-Paul Saman <jpsaman@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

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

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34 35
#include <vlc_interface.h>
#include <vlc_input.h>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
36
#include <vlc_playlist.h>
37 38 39 40

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
41
#include <sys/types.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
42
#ifdef HAVE_POLL
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
43 44
#   include <poll.h>
#endif
45

Clément Stenac's avatar
Clément Stenac committed
46
#include <vlc_network.h>
47

48
#define NETSYNC_PORT 9875
49 50 51 52

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
53 54
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
55

Jean-Paul Saman's avatar
Jean-Paul Saman committed
56 57 58 59
#define NETSYNC_TEXT N_( "Network master clock" )
#define NETSYNC_LONGTEXT N_( "When set then " \
  "this vlc instance shall dictate its clock for synchronisation" \
  "over clients listening on the masters network ip address" )
60

Jean-Paul Saman's avatar
Jean-Paul Saman committed
61 62 63
#define MIP_TEXT N_( "Master server ip address" )
#define MIP_LONGTEXT N_( "The IP address of " \
  "the network master clock to use for clock synchronisation." )
64

Jean-Paul Saman's avatar
Jean-Paul Saman committed
65 66 67
#define NETSYNC_TIMEOUT_TEXT N_( "UDP timeout (in ms)" )
#define NETSYNC_TIMEOUT_LONGTEXT N_("Amount of time (in ms) " \
  "to wait before aborting network reception of data." )
68

69 70 71 72 73
vlc_module_begin ()
    set_shortname( N_("Network Sync"))
    set_description( N_("Network synchronisation") )
    set_category( CAT_ADVANCED )
    set_subcategory( SUBCAT_ADVANCED_MISC )
74

Rémi Duraffort's avatar
Rémi Duraffort committed
75
    add_bool( "netsync-master", false, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
              NETSYNC_TEXT, NETSYNC_LONGTEXT, true )
77
    add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
78
                true )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
79 80
    add_integer( "netsync-timeout", 500, NULL,
                 NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true )
81

82
    set_capability( "interface", 0 )
Laurent Aimar's avatar
Laurent Aimar committed
83
    set_callbacks( Open, Close )
84
vlc_module_end ()
85 86 87 88 89 90 91 92 93

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

/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
94
static int Open( vlc_object_t *p_this )
95 96
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
97
    int fd;
98

Jean-Paul Saman's avatar
Jean-Paul Saman committed
99
    if( !var_InheritBool( p_intf, "netsync-master" ) )
100
    {
101
        char *psz_master = var_InheritString( p_intf, "netsync-master-ip" );
102 103 104 105 106
        if( psz_master == NULL )
        {
            msg_Err( p_intf, "master address not specified" );
            return VLC_EGENERIC;
        }
Sebastien Escudier's avatar
Sebastien Escudier committed
107
        fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 );
108
        free( psz_master );
109
    }
110 111
    else
        fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
112

113 114 115 116 117
    if( fd == -1 )
    {
        msg_Err( p_intf, "Netsync socket failure" );
        return VLC_EGENERIC;
    }
118

119
    p_intf->p_sys = (void *)(intptr_t)fd;
120 121 122 123 124 125 126 127 128 129 130
    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;

131
    net_Close( (intptr_t)p_intf->p_sys );
132 133 134 135 136 137 138 139
}

/*****************************************************************************
 * Run: interface thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
#define MAX_MSG_LENGTH (2 * sizeof(int64_t))
Jean-Paul Saman's avatar
Jean-Paul Saman committed
140
    int canc = vlc_savecancel();
141
    input_thread_t *p_input = NULL;
142
    char p_data[MAX_MSG_LENGTH];
Laurent Aimar's avatar
Laurent Aimar committed
143
    int i_socket = (intptr_t)p_intf->p_sys;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
144 145

    playlist_t *p_playlist = pl_Hold( p_intf );
Laurent Aimar's avatar
Laurent Aimar committed
146 147 148
    int i_timeout = var_InheritInteger( p_intf, "netsync-timeout" );
    if( i_timeout < 500 )
        i_timeout = 500;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
149
    bool b_master = var_InheritBool( p_intf, "netsync-master" );
150 151 152 153

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
154
    while( vlc_object_alive( p_intf ) )
155 156
    {
        /* Update the input */
157
        if( p_input == NULL )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
158 159 160 161
        {
            p_input = playlist_CurrentInput( p_playlist );
        }
        else if( p_input->b_dead || !vlc_object_alive( p_input ) )
162
        {
163 164
            vlc_object_release( p_input );
            p_input = NULL;
165 166
        }

167
        if( p_input == NULL )
168 169 170 171 172 173 174 175 176 177 178
        {
            /* Wait a bit */
            msleep( INTF_IDLE_SLEEP );
            continue;
        }

        /*
         * We now have an input
         */

        /* Initialize file descriptor set and timeout (0.5s) */
179 180
        /* FIXME: arbitrary tick */
        struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
181 182 183 184

        if( b_master )
        {
            struct sockaddr_storage from;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
185 186 187
            mtime_t i_master_system;
            mtime_t i_client_system;
            mtime_t i_date;
188 189 190
            int i_struct_size, i_read, i_ret;

            /* Don't block */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
191 192
            i_ret = poll( &ufd, 1, i_timeout );
            if( i_ret <= 0 ) continue;
193 194 195 196

            /* We received something */
            i_struct_size = sizeof( from );
            i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
197
                               (struct sockaddr*)&from,
198
                               (unsigned int *)&i_struct_size );
199

Jean-Paul Saman's avatar
Jean-Paul Saman committed
200 201 202
            /* not sure we need the client information to sync,
               since we are the master anyway */
            i_client_system = ntoh64(*(int64_t *)p_data);
203 204 205

            i_date = mdate();

Jean-Paul Saman's avatar
Jean-Paul Saman committed
206 207 208 209 210
            if( input_GetPcrSystem( p_input, &i_master_system ) )
                continue;

            *((int64_t *)p_data) = hton64( i_date );
            *(((int64_t *)p_data)+1) = hton64( i_master_system );
211 212 213 214 215

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

216
#if 0
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
217
            msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
Jean-Paul Saman's avatar
Jean-Paul Saman committed
218 219
                     "(date: %"PRId64")", i_client_system, i_master_system,
                     (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
220
                     : "non-IPv4", i_date );
221
#endif
222 223 224
        }
        else
        {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
225 226 227 228 229
            mtime_t i_master_system;
            mtime_t i_client_system;
            mtime_t i_system = 0;
            mtime_t i_send_date, i_receive_date;
            mtime_t i_diff_date, i_master_date;
230 231
            int i_sent, i_read, i_ret;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
232 233 234 235 236 237
            if( input_GetPcrSystem( p_input, &i_system ) )
            {
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

238 239
            /* Send clock request to the master */
            i_send_date = mdate();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
240
            *((int64_t *)p_data) = hton64( i_system );
241 242 243 244 245 246 247 248 249

            i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );
            if( i_sent <= 0 )
            {
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            /* Don't block */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
250
            i_ret = poll( &ufd, 1, i_timeout );
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
            if( i_ret == 0 ) continue;
            if( i_ret < 0 )
            {
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            i_receive_date = mdate();
            i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );
            if( i_read <= 0 )
            {
                msleep( INTF_IDLE_SLEEP );
                continue;
            }

            i_master_date = ntoh64(*(int64_t *)p_data);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
267
            i_master_system = ntoh64(*(((int64_t *)p_data)+1)); /* system date */
268 269 270 271

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

Jean-Paul Saman's avatar
Jean-Paul Saman committed
272
            if( p_input && i_master_system > 0 )
273
            {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
274 275 276 277 278 279 280 281 282 283 284 285
                mtime_t i_diff_system;

                if( input_GetPcrSystem( p_input, &i_client_system ) )
                {
                    msleep( INTF_IDLE_SLEEP );
                    continue;
                }

                i_diff_system = i_client_system - i_master_system - i_diff_date;
                if( i_diff_system != 0 )
                {
                    input_ModifyPcrSystem( p_input, true, i_master_system - i_diff_date );
286
#if 0
Jean-Paul Saman's avatar
Jean-Paul Saman committed
287 288 289 290
                    msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
                             " clock diff: %"PRId64", diff: %"PRId64"",
                             i_system, i_master_system, i_client_system,
                             i_diff_system, i_diff_date );
291
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
292 293
                }
            }
294 295 296 297
            msleep( INTF_IDLE_SLEEP );
        }
    }

298
    if( p_input ) vlc_object_release( p_input );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
299
    pl_Release( p_intf );
300
    vlc_restorecancel( canc );
301 302
}