]> git.sesse.net Git - vlc/blobdiff - modules/control/netsync.c
No functional changes (K&R + no hungarian notations, netsync).
[vlc] / modules / control / netsync.c
index 118255e3c71f2dcf8a14af876aff4fc04bd04c1d..ba5913219f2b66e398c756ea35b6c87109fc71fe 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * netsync.c: synchronisation between several network clients.
  *****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
+ *          Jean-Paul Saman <jpsaman@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
@@ -32,7 +33,7 @@
 #include <vlc_plugin.h>
 #include <vlc_interface.h>
 #include <vlc_input.h>
-#include <vlc_es_out.h>
+#include <vlc_playlist.h>
 
 #ifdef HAVE_UNISTD_H
 #    include <unistd.h>
 
 #define NETSYNC_PORT 9875
 
-/* FIXME: UGLY UGLY !! Netsync should be totally reworked */
-#include "../../src/input/input_internal.h"
-
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-static int  Activate( vlc_object_t * );
-static void Close   ( vlc_object_t * );
-
-static mtime_t GetClockRef( intf_thread_t *, mtime_t );
-
-/// \bug [String] This string is BAD.
-#define NETSYNC_TEXT N_( "Act as master" )
-#define NETSYNC_LONGTEXT N_( "Should " \
-  "act as the master client for the network synchronisation?" )
-
-/// \bug [String] This string is BAD.
-#define MIP_TEXT N_( "Master client ip address" )
-#define MIP_LONGTEXT N_( "IP address of " \
-  "the master client used for the network synchronisation." )
-
-vlc_module_begin ()
-    set_shortname( N_("Network Sync"))
-    set_description( N_("Network synchronisation") )
-    set_category( CAT_ADVANCED )
-    set_subcategory( SUBCAT_ADVANCED_MISC )
-
-    add_bool( "netsync-master", false, NULL,
-              NETSYNC_TEXT, NETSYNC_LONGTEXT, true )
-    add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
-                true )
-
-    set_capability( "interface", 0 )
-    set_callbacks( Activate, Close )
-vlc_module_end ()
+static int  Open (vlc_object_t *);
+static void Close(vlc_object_t *);
+
+#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")
+
+#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.")
+
+#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.")
+
+vlc_module_begin()
+    set_shortname(N_("Network Sync"))
+    set_description(N_("Network synchronisation"))
+    set_category(CAT_ADVANCED)
+    set_subcategory(SUBCAT_ADVANCED_MISC)
+
+    add_bool("netsync-master", false, NULL,
+              NETSYNC_TEXT, NETSYNC_LONGTEXT, true)
+    add_string("netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
+                true)
+    add_integer("netsync-timeout", 500, NULL,
+                 NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true)
+
+    set_capability("interface", 0)
+    set_callbacks(Open, Close)
+vlc_module_end()
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static void Run( intf_thread_t *p_intf );
+static void Run(intf_thread_t *intf);
 
 /*****************************************************************************
  * Activate: initialize and create stuff
  *****************************************************************************/
-static int Activate( vlc_object_t *p_this )
+static int Open(vlc_object_t *object)
 {
-    intf_thread_t *p_intf = (intf_thread_t*)p_this;
+    intf_thread_t *intf = (intf_thread_t*)object;
     int fd;
 
-    if( !var_InheritInteger( p_intf, "netsync-master" ) )
-    {
-        char *psz_master = var_InheritString( p_intf, "netsync-master-ip" );
-        if( psz_master == NULL )
-        {
-            msg_Err( p_intf, "master address not specified" );
+    if (!var_InheritBool(intf, "netsync-master")) {
+        char *psz_master = var_InheritString(intf, "netsync-master-ip");
+        if (psz_master == NULL) {
+            msg_Err(intf, "master address not specified");
             return VLC_EGENERIC;
         }
-        fd = net_ConnectUDP( VLC_OBJECT(p_intf), psz_master, NETSYNC_PORT, -1 );
-        free( psz_master );
+        fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1);
+        free(psz_master);
     }
     else
-        fd = net_ListenUDP1( VLC_OBJECT(p_intf), NULL, NETSYNC_PORT );
+        fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT);
 
-    if( fd == -1 )
-    {
-        msg_Err( p_intf, "Netsync socket failure" );
+    if (fd == -1) {
+        msg_Err(intf, "Netsync socket failure");
         return VLC_EGENERIC;
     }
 
-    p_intf->p_sys = (void *)(intptr_t)fd;
-    p_intf->pf_run = Run;
+    intf->p_sys = (void *)(intptr_t)fd;
+    intf->pf_run = Run;
     return VLC_SUCCESS;
 }
 
 /*****************************************************************************
  * Close: destroy interface
  *****************************************************************************/
-void Close( vlc_object_t *p_this )
+void Close(vlc_object_t *object)
 {
-    intf_thread_t *p_intf = (intf_thread_t*)p_this;
+    intf_thread_t *intf = (intf_thread_t*)object;
 
-    net_Close( (intptr_t)p_intf->p_sys );
+    net_Close((intptr_t)intf->p_sys);
 }
 
 /*****************************************************************************
  * Run: interface thread
  *****************************************************************************/
-static void Run( intf_thread_t *p_intf )
+static void Run(intf_thread_t *intf)
 {
 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
-
-    input_thread_t *p_input = NULL;
-    char p_data[MAX_MSG_LENGTH];
-    int i_socket;
     int canc = vlc_savecancel();
+    input_thread_t *input = NULL;
+    char data[MAX_MSG_LENGTH];
+    int fd = (intptr_t)intf->p_sys;
+
+    playlist_t *playlist = pl_Hold(intf);
+    int timeout = var_InheritInteger(intf, "netsync-timeout");
+    if (timeout < 500)
+        timeout = 500;
+    bool is_master = var_InheritBool(intf, "netsync-master");
 
     /* High priority thread */
-    vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );
+    vlc_thread_set_priority(intf, VLC_THREAD_PRIORITY_INPUT);
 
-    while( vlc_object_alive( p_intf ) )
-    {
+    while (vlc_object_alive(intf)) {
         /* Update the input */
-        if( p_input == NULL )
-            p_input =
-                (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
-                                                   FIND_ANYWHERE );
-        else if( p_input->b_dead )
-        {
-            vlc_object_release( p_input );
-            p_input = NULL;
+        if (input == NULL) {
+            input = playlist_CurrentInput(playlist);
+        } else if (input->b_dead || !vlc_object_alive(input)) {
+            vlc_object_release(input);
+            input = NULL;
         }
 
-        if( p_input == NULL )
-        {
+        if (input == NULL) {
             /* Wait a bit */
-            msleep( INTF_IDLE_SLEEP );
+            msleep(INTF_IDLE_SLEEP);
             continue;
         }
 
@@ -171,134 +169,121 @@ static void Run( intf_thread_t *p_intf )
 
         /* Initialize file descriptor set and timeout (0.5s) */
         /* FIXME: arbitrary tick */
-        struct pollfd ufd = { .fd = i_socket, .events = POLLIN, };
+        struct pollfd ufd = { .fd = fd, .events = POLLIN, };
 
-        if( b_master )
-        {
+        if (is_master) {
             struct sockaddr_storage from;
-            mtime_t i_date, i_clockref, i_master_clockref;
-            int i_struct_size, i_read, i_ret;
+            mtime_t master_system;
+            mtime_t client_system;
+            mtime_t date;
+            int struct_size, read_size, ret;
 
             /* Don't block */
-            i_ret = poll( &ufd, 1, 500 );
-            if( i_ret == 0 ) continue;
-            if( i_ret < 0 )
-            {
-                /* Wait a bit */
-                msleep( INTF_IDLE_SLEEP );
+            ret = poll(&ufd, 1, timeout);
+            if (ret <= 0)
                 continue;
-            }
 
             /* We received something */
-            i_struct_size = sizeof( from );
-            i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,
-                               (struct sockaddr*)&from,
-                               (unsigned int *)&i_struct_size );
+            struct_size = sizeof(from);
+            read_size = recvfrom(fd, data, MAX_MSG_LENGTH, 0,
+                                 (struct sockaddr*)&from,
+                                 (unsigned int *)&struct_size);
 
-            i_clockref = ntoh64(*(int64_t *)p_data);
+            /* not sure we need the client information to sync,
+               since we are the master anyway */
+            client_system = ntoh64(*(int64_t *)data);
 
-            i_date = mdate();
-            *(int64_t *)p_data = hton64( i_date );
+            date = mdate();
+
+            if (input_GetPcrSystem(input, &master_system))
+                continue;
 
-            i_master_clockref = GetClockRef( p_intf, i_clockref );
-            *(((int64_t *)p_data)+1) = hton64( i_master_clockref );
+            *((int64_t *)data) = hton64(date);
+            *(((int64_t *)data)+1) = hton64(master_system);
 
             /* Reply to the sender */
-            sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,
-                    (struct sockaddr *)&from, i_struct_size );
+            sendto(fd, data, 2 * sizeof(int64_t), 0,
+                    (struct sockaddr *)&from, struct_size);
 
 #if 0
-            msg_Dbg( p_intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
-                     "(date: %"PRId64")", i_clockref, i_master_clockref,
-                     from.ss_family == AF_INET
-                     ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
-                     : "non-IPv4", i_date );
+            msg_Dbg(intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
+                     "(date: %"PRId64")", client_system, master_system,
+                     (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
+                     : "non-IPv4", date);
 #endif
         }
         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;
+            mtime_t master_system;
+            mtime_t client_system;
+            mtime_t system = 0;
+            mtime_t send_date, receive_date;
+            mtime_t diff_date, master_date;
+            int sent, read_size, ret;
+
+            if (input_GetPcrSystem(input, &system)) {
+                msleep(INTF_IDLE_SLEEP);
+                continue;
+            }
 
             /* 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 );
+            send_date = mdate();
+            *((int64_t *)data) = hton64(system);
+
+            sent = send(fd, data, sizeof(int64_t), 0);
+            if (sent <= 0) {
+                msleep(INTF_IDLE_SLEEP);
                 continue;
             }
 
             /* Don't block */
-            i_ret = poll( &ufd, 1, 500 );
-            if( i_ret == 0 ) continue;
-            if( i_ret < 0 )
-            {
-                /* Wait a bit */
-                msleep( INTF_IDLE_SLEEP );
+            ret = poll(&ufd, 1, timeout);
+            if (ret == 0)
+                continue;
+            if (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 )
-            {
-                /* Wait a bit */
-                msleep( INTF_IDLE_SLEEP );
+            receive_date = mdate();
+            read_size = recv(fd, data, MAX_MSG_LENGTH, 0);
+            if (read_size <= 0) {
+                msleep(INTF_IDLE_SLEEP);
                 continue;
             }
 
-            i_master_date = ntoh64(*(int64_t *)p_data);
-            i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));
+            master_date = ntoh64(*(int64_t *)data);
+            master_system = ntoh64(*(((int64_t *)data)+1)); /* system date */
 
-            i_diff_date = i_receive_date -
-                          ((i_receive_date - i_send_date) / 2 + i_master_date);
+            diff_date = receive_date -
+                          ((receive_date - send_date) / 2 + master_date);
 
-            i_client_clockref = i_drift = 0;
-            if( p_input && i_master_clockref )
-            {
-                i_client_clockref = GetClockRef( p_intf, i_clockref );
-                i_drift = i_client_clockref - i_master_clockref - i_diff_date;
+            if (input && master_system > 0) {
+                mtime_t diff_system;
 
-                /* Update our clock to match the master's one */
-                if( i_client_clockref )
-                    p_input->i_pts_delay -= i_drift;
-            }
+                if (input_GetPcrSystem(input, &client_system)) {
+                    msleep(INTF_IDLE_SLEEP);
+                    continue;
+                }
 
+                diff_system = client_system - master_system - diff_date;
+                if (diff_system != 0) {
+                    input_ModifyPcrSystem(input, true, master_system - diff_date);
 #if 0
-            msg_Dbg( p_intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64", "
-                     "clock diff: %"PRId64" drift: %"PRId64,
-                     i_clockref, i_master_clockref,
-                     i_client_clockref, i_diff_date, i_drift );
+                    msg_Dbg(intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
+                             " clock diff: %"PRId64", diff: %"PRId64"",
+                             system, master_system, client_system,
+                             diff_system, diff_date);
 #endif
-
-            /* Wait a bit */
-            msleep( INTF_IDLE_SLEEP );
+                }
+            }
+            msleep(INTF_IDLE_SLEEP);
         }
     }
 
-    if( p_input ) vlc_object_release( p_input );
-    vlc_restorecancel( canc );
+    if (input)
+        vlc_object_release(input);
+    pl_Release(intf);
+    vlc_restorecancel(canc);
 }
 
-static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )
-{
-    input_thread_t *p_input = p_intf->p_sys->p_input;
-    mtime_t i_ts;
-
-    if( !p_input || !p_input->p->p_es_out ) return 0;
-
-    if( es_out_Control( p_input->p->p_es_out, ES_OUT_GET_TS, i_pts, &i_ts ) ==
-        VLC_SUCCESS )
-    {
-        return i_ts;
-    }
-
-    return 0;
-}