]> git.sesse.net Git - vlc/blobdiff - modules/control/netsync.c
Revert "MKV: Always set an i_pts in a lace otherwise it disturbs seeking performance"
[vlc] / modules / control / netsync.c
index 7466025b6db5ea7ff13322273d78d86219e667a8..05b18b291ff810790ad8bc350a31507ad3bd1f02 100644 (file)
 #include <vlc_input.h>
 #include <vlc_playlist.h>
 
-#ifdef HAVE_UNISTD_H
-#    include <unistd.h>
-#endif
 #include <sys/types.h>
+#include <unistd.h>
 #ifdef HAVE_POLL
 #   include <poll.h>
 #endif
@@ -153,8 +151,13 @@ void Close(vlc_object_t *object)
     intf_thread_t *intf = (intf_thread_t*)object;
     intf_sys_t *sys = intf->p_sys;
 
-    assert(sys->input == NULL);
     var_DelCallback(sys->playlist, "input-current", PlaylistEvent, intf);
+
+    if (sys->input != NULL) {
+        vlc_cancel(sys->thread);
+        vlc_join(sys->thread, NULL);
+    }
+
     net_Close(sys->fd);
     free(sys);
 }
@@ -184,9 +187,11 @@ static void *Master(void *handle)
 
         /* We received something */
         struct sockaddr_storage from;
-        unsigned struct_size = sizeof(from);
-        recvfrom(sys->fd, data, sizeof(data), 0,
-                 (struct sockaddr*)&from, &struct_size);
+        socklen_t fromlen = sizeof (from);
+
+        if (recvfrom(sys->fd, data, 8, 0,
+                     (struct sockaddr *)&from, &fromlen) < 8)
+            continue;
 
         mtime_t master_system = GetPcrSystem(sys->input);
         if (master_system < 0)
@@ -196,8 +201,8 @@ static void *Master(void *handle)
         data[1] = hton64(master_system);
 
         /* Reply to the sender */
-        sendto(sys->fd, data, sizeof(data), 0,
-               (struct sockaddr *)&from, struct_size);
+        sendto(sys->fd, data, 16, 0,
+               (struct sockaddr *)&from, fromlen);
 #if 0
         /* not sure we need the client information to sync,
            since we are the master anyway */
@@ -225,18 +230,17 @@ static void *Slave(void *handle)
             goto wait;
 
         /* Send clock request to the master */
-        data[0] = hton64(system);
-
         const mtime_t send_date = mdate();
-        if (send(sys->fd, data, sizeof(data[0]), 0) <= 0)
-            goto wait;
+
+        data[0] = hton64(system);
+        send(sys->fd, data, 8, 0);
 
         /* Don't block */
         if (poll(&ufd, 1, sys->timeout) <= 0)
             continue;
 
         const mtime_t receive_date = mdate();
-        if (recv(sys->fd, data, sizeof(data), 0) <= 0)
+        if (recv(sys->fd, data, 16, 0) < 16)
             goto wait;
 
         const mtime_t master_date   = ntoh64(data[0]);
@@ -268,39 +272,29 @@ static void *Slave(void *handle)
     return NULL;
 }
 
-static int InputEvent(vlc_object_t *object, char const *cmd,
-                      vlc_value_t oldval, vlc_value_t newval, void *data)
+static int PlaylistEvent(vlc_object_t *object, char const *cmd,
+                         vlc_value_t oldval, vlc_value_t newval, void *data)
 {
-    VLC_UNUSED(cmd); VLC_UNUSED(oldval); VLC_UNUSED(object);
+    VLC_UNUSED(cmd); VLC_UNUSED(object);
     intf_thread_t  *intf = data;
     intf_sys_t     *sys = intf->p_sys;
+    input_thread_t *input = newval.p_address;
 
-    if (newval.i_int == INPUT_EVENT_DEAD && sys->input) {
+    if (sys->input != NULL) {
         msg_Err(intf, "InputEvent DEAD");
+        assert(oldval.p_address == sys->input);
+
         vlc_cancel(sys->thread);
         vlc_join(sys->thread, NULL);
-        vlc_object_release(sys->input);
-        sys->input = NULL;
     }
-    return VLC_SUCCESS;
-}
 
-static int PlaylistEvent(vlc_object_t *object, char const *cmd,
-                         vlc_value_t oldval, vlc_value_t newval, void *data)
-{
-    VLC_UNUSED(cmd); VLC_UNUSED(oldval); VLC_UNUSED(object);
-    intf_thread_t  *intf = data;
-    intf_sys_t     *sys = intf->p_sys;
+    sys->input = input;
 
-    input_thread_t *input = newval.p_address;
-    assert(sys->input == NULL);
-    sys->input = vlc_object_hold(input);
-    if (vlc_clone(&sys->thread, sys->is_master ? Master : Slave, intf,
-                  VLC_THREAD_PRIORITY_INPUT)) {
-        vlc_object_release(input);
-        return VLC_SUCCESS;
+    if (input != NULL) {
+        if (vlc_clone(&sys->thread, sys->is_master ? Master : Slave, intf,
+                      VLC_THREAD_PRIORITY_INPUT))
+            sys->input = NULL;
     }
-    var_AddCallback(input, "intf-event", InputEvent, intf);
     return VLC_SUCCESS;
 }