* modules/control/netsync.c: Experimental network synchronisation module.
authorGildas Bazin <gbazin@videolan.org>
Thu, 27 May 2004 11:40:46 +0000 (11:40 +0000)
committerGildas Bazin <gbazin@videolan.org>
Thu, 27 May 2004 11:40:46 +0000 (11:40 +0000)
configure.ac
modules/control/Modules.am
modules/control/netsync.c [new file with mode: 0644]

index b041840..b4b1ec6 100644 (file)
@@ -883,7 +883,7 @@ test "${enable_cprof}" != "yes" && enable_cprof="no"
 dnl
 dnl  default modules
 dnl
 dnl
 dnl  default modules
 dnl
-AX_ADD_PLUGINS([dummy rc telnet logger gestures memcpy hotkeys])
+AX_ADD_PLUGINS([dummy rc telnet logger gestures memcpy hotkeys netsync])
 AX_ADD_PLUGINS([mpgv mpga m4v h264 mpeg_system ps ps2 pva ts avi asf aac mp4 rawdv demux2 nsv real aiff mjpeg])
 AX_ADD_PLUGINS([cvdsub svcdsub spudec dvbsub mpeg_audio lpcm a52 dts cinepak])
 AX_ADD_PLUGINS([deinterlace invert adjust wall transform distort clone crop motionblur])
 AX_ADD_PLUGINS([mpgv mpga m4v h264 mpeg_system ps ps2 pva ts avi asf aac mp4 rawdv demux2 nsv real aiff mjpeg])
 AX_ADD_PLUGINS([cvdsub svcdsub spudec dvbsub mpeg_audio lpcm a52 dts cinepak])
 AX_ADD_PLUGINS([deinterlace invert adjust wall transform distort clone crop motionblur])
index 7a7ef48..9ebf699 100644 (file)
@@ -1,6 +1,7 @@
 SOURCES_gestures = gestures.c
 SOURCES_http = http.c
 SOURCES_telnet = telnet.c
 SOURCES_gestures = gestures.c
 SOURCES_http = http.c
 SOURCES_telnet = telnet.c
+SOURCES_netsync = netsync.c
 SOURCES_ntservice = ntservice.c
 SOURCES_joystick = joystick.c
 SOURCES_hotkeys = hotkeys.c
 SOURCES_ntservice = ntservice.c
 SOURCES_joystick = joystick.c
 SOURCES_hotkeys = hotkeys.c
diff --git a/modules/control/netsync.c b/modules/control/netsync.c
new file mode 100644 (file)
index 0000000..3a49c6d
--- /dev/null
@@ -0,0 +1,325 @@
+/*****************************************************************************\r
+ * netsync.c: synchronisation between several network clients.\r
+ *****************************************************************************\r
+ * Copyright (C) 2004 VideoLAN\r
+ * $Id: ntservice.c 6963 2004-03-05 19:24:14Z murray $\r
+ *\r
+ * Authors: Gildas Bazin <gbazin@videolan.org>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.\r
+ *****************************************************************************/\r
+\r
+/*****************************************************************************\r
+ * Preamble\r
+ *****************************************************************************/\r
+#include <stdlib.h>\r
+#include <vlc/vlc.h>\r
+#include <vlc/intf.h>\r
+\r
+#ifdef HAVE_UNISTD_H\r
+#    include <unistd.h>\r
+#endif\r
+\r
+#ifdef HAVE_SYS_TIME_H\r
+#    include <sys/time.h>\r
+#endif\r
+#include <sys/types.h>\r
+\r
+#ifdef WIN32\r
+#   include <winsock2.h>\r
+#   include <ws2tcpip.h>\r
+#   ifndef IN_MULTICAST\r
+#       define IN_MULTICAST(a) IN_CLASSD(a)\r
+#   endif\r
+#else\r
+#   include <sys/socket.h>\r
+#   include <netinet/in.h>\r
+#   if HAVE_ARPA_INET_H\r
+#      include <arpa/inet.h>\r
+#   elif defined( SYS_BEOS )\r
+#      include <net/netdb.h>\r
+#   endif\r
+#endif\r
+\r
+#ifdef UNDER_CE\r
+#   define close(a) CloseHandle(a);\r
+#elif defined( WIN32 )\r
+#   define close(a) closesocket(a);\r
+#endif\r
+\r
+#include "network.h"\r
+#include "input_ext-plugins.h"\r
+\r
+#define NETSYNC_PORT_MASTER 9875\r
+#define NETSYNC_PORT_SLAVE  9876\r
+\r
+/*****************************************************************************\r
+ * Module descriptor\r
+ *****************************************************************************/\r
+static int  Activate( vlc_object_t * );\r
+static void Close   ( vlc_object_t * );\r
+\r
+static mtime_t GetClockRef( intf_thread_t *, mtime_t );\r
+\r
+#define NETSYNC_TEXT N_( "Act as master for network synchronisation" )\r
+#define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \\r
+  "act as the master client for the network synchronisation." )\r
+\r
+#define MIP_TEXT N_( "Master client ip address" )\r
+#define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \\r
+  "the master client used for the network synchronisation." )\r
+\r
+vlc_module_begin();\r
+    set_description( _("Network synchronisation") );\r
+\r
+    add_bool( "netsync-master", 0, NULL,\r
+              NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );\r
+    add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,\r
+                VLC_TRUE );\r
+\r
+    set_capability( "interface", 0 );\r
+    set_callbacks( Activate, Close );\r
+vlc_module_end();\r
+\r
+struct intf_sys_t\r
+{\r
+    input_thread_t *p_input;\r
+};\r
+\r
+/*****************************************************************************\r
+ * Local prototypes\r
+ *****************************************************************************/\r
+static void Run( intf_thread_t *p_intf );\r
+\r
+/*****************************************************************************\r
+ * Activate: initialize and create stuff\r
+ *****************************************************************************/\r
+static int Activate( vlc_object_t *p_this )\r
+{\r
+    intf_thread_t *p_intf = (intf_thread_t*)p_this;\r
+\r
+    msg_Info( p_intf, "Using the netsync interface module..." );\r
+\r
+    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );\r
+\r
+    p_intf->pf_run = Run;\r
+    return VLC_SUCCESS;\r
+}\r
+\r
+/*****************************************************************************\r
+ * Close: destroy interface\r
+ *****************************************************************************/\r
+void Close( vlc_object_t *p_this )\r
+{\r
+    intf_thread_t *p_intf = (intf_thread_t*)p_this;\r
+\r
+    free( p_intf->p_sys );\r
+}\r
+\r
+/*****************************************************************************\r
+ * Run: interface thread\r
+ *****************************************************************************/\r
+static void Run( intf_thread_t *p_intf )\r
+{\r
+#define MAX_MSG_LENGTH (2 * sizeof(int64_t))\r
+\r
+    vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );\r
+    char *psz_master = config_GetPsz( p_intf, "netsync-master-ip" );\r
+    struct sockaddr_in master_addr;\r
+    char p_data[MAX_MSG_LENGTH];\r
+    int i_socket;\r
+\r
+    if( !psz_master || inet_addr( psz_master ) == INADDR_NONE )\r
+    {\r
+        if( psz_master ) free( psz_master );\r
+        msg_Err( p_intf, "invalid master address." );\r
+        return;\r
+    }\r
+\r
+    memset( &master_addr, 0, sizeof( struct sockaddr_in ) );\r
+    master_addr.sin_family = AF_INET;\r
+    master_addr.sin_port = htons( (uint16_t)NETSYNC_PORT_MASTER );\r
+    master_addr.sin_addr.s_addr = inet_addr( psz_master );\r
+    free( psz_master );\r
+\r
+    i_socket = net_OpenUDP( p_intf, NULL, b_master ? NETSYNC_PORT_MASTER :\r
+                            NETSYNC_PORT_SLAVE, NULL, 0 );\r
+    if( i_socket < 0 )\r
+    {\r
+        msg_Err( p_intf, "failed opening UDP socket." );\r
+        return;\r
+    }\r
+\r
+    /* High priority thread */\r
+    vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );\r
+\r
+    while( !p_intf->b_die )\r
+    {\r
+        struct timeval  timeout;\r
+        fd_set fds_r;\r
+\r
+        /* Update the input */\r
+        if( p_intf->p_sys->p_input == NULL )\r
+        {\r
+            p_intf->p_sys->p_input =\r
+                (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,\r
+                                                   FIND_ANYWHERE );\r
+        }\r
+        else if( p_intf->p_sys->p_input->b_dead )\r
+        {\r
+            vlc_object_release( p_intf->p_sys->p_input );\r
+            p_intf->p_sys->p_input = NULL;\r
+        }\r
+\r
+        if( p_intf->p_sys->p_input == NULL )\r
+        {\r
+            /* Wait a bit */\r
+            msleep( INTF_IDLE_SLEEP );\r
+            continue;\r
+        }\r
+\r
+        /*\r
+         * We now have an input\r
+         */\r
+\r
+        /* Initialize file descriptor set and timeout (0.5s) */\r
+        FD_ZERO( &fds_r );\r
+        FD_SET( i_socket, &fds_r );\r
+        timeout.tv_sec = 0;\r
+        timeout.tv_usec = 500000;\r
+\r
+        if( b_master )\r
+        {\r
+            struct sockaddr_in from;\r
+            mtime_t i_date, i_clockref, i_master_clockref;\r
+            int i_struct_size, i_read, i_ret;\r
+\r
+            /* Don't block */\r
+            i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );\r
+            if( i_ret == 0 ) continue;\r
+            if( i_ret < 0 )\r
+            {\r
+                /* Wait a bit */\r
+                msleep( INTF_IDLE_SLEEP );\r
+                continue;\r
+            }\r
+\r
+            /* We received something */\r
+            i_struct_size = sizeof(struct sockaddr_in);\r
+            i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,\r
+                               (struct sockaddr*)&from, &i_struct_size );\r
+\r
+            from.sin_port = htons( (uint16_t)NETSYNC_PORT_SLAVE );\r
+\r
+            i_clockref = ntoh64(*(int64_t *)p_data);\r
+\r
+            i_date = mdate();\r
+            *(int64_t *)p_data = hton64( i_date );\r
+\r
+            i_master_clockref = GetClockRef( p_intf, i_clockref );\r
+            *(((int64_t *)p_data)+1) = hton64( i_master_clockref );\r
+\r
+            /* Reply to the sender */\r
+            sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,\r
+                    (struct sockaddr *)&from, sizeof(struct sockaddr_in) );\r
+\r
+            msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "\r
+                     "(date: "I64Fd")", i_clockref, i_master_clockref, \r
+                     inet_ntoa(from.sin_addr), i_date );\r
+        }\r
+        else\r
+        {\r
+            mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;\r
+            mtime_t i_master_clockref, i_client_clockref, i_drift;\r
+            mtime_t i_clockref = 0;\r
+            int i_sent, i_read, i_ret;\r
+\r
+            /* Send clock request to the master */\r
+            *(int64_t *)p_data = hton64( i_clockref );\r
+            i_send_date = mdate();\r
+\r
+            i_sent = sendto( i_socket, p_data, sizeof(int64_t), 0,\r
+                             (struct sockaddr *)&master_addr,\r
+                             sizeof(struct sockaddr_in) );\r
+            if( i_sent <= 0 )\r
+            {\r
+                /* Wait a bit */\r
+                msleep( INTF_IDLE_SLEEP );\r
+                continue;\r
+            }\r
+\r
+            /* Don't block */\r
+            i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);\r
+            if( i_ret == 0 ) continue;\r
+            if( i_ret < 0 )\r
+            {\r
+                /* Wait a bit */\r
+                msleep( INTF_IDLE_SLEEP );\r
+                continue;\r
+            }\r
+\r
+            i_receive_date = mdate();\r
+\r
+            i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );\r
+            if( i_read <= 0 )\r
+            {\r
+                /* Wait a bit */\r
+                msleep( INTF_IDLE_SLEEP );\r
+                continue;\r
+            }\r
+\r
+            i_master_date = ntoh64(*(int64_t *)p_data);\r
+            i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));\r
+\r
+            i_diff_date = i_receive_date -\r
+                          ((i_receive_date - i_send_date) / 2 + i_master_date);\r
+\r
+            i_client_clockref = i_drift = 0;\r
+            if( p_intf->p_sys->p_input && i_master_clockref )\r
+            {\r
+                i_client_clockref = GetClockRef( p_intf, i_clockref );\r
+                i_drift = i_client_clockref - i_master_clockref - i_diff_date;\r
+\r
+                /* Update our clock to match the master's one */\r
+                if( i_client_clockref )\r
+                    p_intf->p_sys->p_input->i_pts_delay -= i_drift;\r
+            }\r
+\r
+            msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "\r
+                     "clock diff: "I64Fd" drift: "I64Fd,\r
+                     i_clockref, i_master_clockref, \r
+                     i_client_clockref, i_diff_date, i_drift );\r
+\r
+            /* Wait a bit */\r
+            msleep( INTF_IDLE_SLEEP );\r
+        }\r
+    }\r
+\r
+    if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );\r
+    net_Close( i_socket );\r
+}\r
+\r
+static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )\r
+{\r
+    input_thread_t *p_input = p_intf->p_sys->p_input;\r
+    pgrm_descriptor_t *p_pgrm;\r
+\r
+    if( !p_input ) return 0;\r
+\r
+    p_pgrm = p_input->stream.p_selected_program;\r
+    if( p_pgrm ) return input_ClockGetTS( p_input, p_pgrm, i_pts );\r
+\r
+    return 0;\r
+}\r