new codename, reset version to git
[paraslash.git] / audiod.c
index 9d0d0741cb86dd2e94c78eff1ed3503df5edbdd6..85aa517ac536ae6796c84f5605556364af299234 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -467,14 +467,16 @@ static int open_current_receiver(struct sched *s)
        return open_receiver(i) < 0? 0 : 1;
 }
 
-static void compute_time_diff(const struct timeval *status_time)
+static unsigned compute_time_diff(const struct timeval *status_time)
 {
        struct timeval tmp, diff;
-       static int count;
+       static unsigned count;
        int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
        const struct timeval max_deviation = {0, 500 * 1000};
        const int time_smooth = 5;
 
+       if (!status_time)
+               return count;
        sign = tv_diff(status_time, now, &diff);
 //             PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
 //                     sign, sa_time_diff_sign);
@@ -504,6 +506,7 @@ static void compute_time_diff(const struct timeval *status_time)
        );
 out:
        stat_task->sa_time_diff_sign = sa_time_diff_sign;
+       return count;
 }
 
 static void check_stat_line(char *line)
@@ -546,13 +549,15 @@ static void check_stat_line(char *line)
                        delay.tv_usec = (conf.stream_delay_arg % 1000) * 1000;
                        stat_task->server_stream_start.tv_sec = sec;
                        stat_task->server_stream_start.tv_usec = usec;
-                       if (stat_task->sa_time_diff_sign < 0)
-                               tv_add(&stat_task->server_stream_start,
-                                       &stat_task->sa_time_diff, &a_start);
-                       else
-                               tv_diff(&stat_task->server_stream_start,
-                                       &stat_task->sa_time_diff, &a_start);
-                       tv_add(&a_start, &delay, &initial_delay_barrier);
+                       if (compute_time_diff(NULL) > 2) {
+                               if (stat_task->sa_time_diff_sign < 0)
+                                       tv_add(&stat_task->server_stream_start,
+                                               &stat_task->sa_time_diff, &a_start);
+                               else
+                                       tv_diff(&stat_task->server_stream_start,
+                                               &stat_task->sa_time_diff, &a_start);
+                               tv_add(&a_start, &delay, &initial_delay_barrier);
+                       }
                }
                break;
        case SI_CURRENT_TIME:
@@ -631,7 +636,7 @@ static void audiod_pre_select(struct sched *s, __a_unused struct task *t)
                        s->timeout = min_delay;
                        continue;
                }
-               PARA_INFO_LOG("inital delay: %lu ms left\n", tv2ms(&diff));
+               PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff));
                if (tv_diff(&s->timeout, &diff, NULL) > 0) {
                        s->timeout = diff;
                }
@@ -1006,11 +1011,16 @@ void __noreturn clean_exit(int status, const char *msg)
        exit(status);
 }
 
+/* avoid busy loop if server is down */
+static void set_stat_task_restart_barrier(void)
+{
+       struct timeval delay = {5, 0};
+       tv_add(now, &delay, &stat_task->restart_barrier);
+}
 
 static void client_task_event_handler(__a_unused struct task *t)
 {
        int i;
-       struct timeval delay = {1, 0};
 
        if (t->ret == -E_HANDSHAKE_COMPLETE)
                return;
@@ -1018,8 +1028,7 @@ static void client_task_event_handler(__a_unused struct task *t)
        close_stat_pipe();
        if (t->ret != -E_SERVER_EOF)
                stat_task->clock_diff_count = conf.clock_diff_count_arg;
-       /* avoid busy loop if server is down */
-       tv_add(now, &delay, &stat_task->restart_barrier);
+       set_stat_task_restart_barrier();
        FOR_EACH_AUDIO_FORMAT(i)
                afi[i].restart_barrier = stat_task->restart_barrier;
 }
@@ -1048,6 +1057,7 @@ static void status_pre_select(struct sched *s, struct task *t)
                int argc = 2;
                ret = client_open(argc, argv, &st->pcd);
        }
+       set_stat_task_restart_barrier();
        if (ret < 0)
                return;
        st->pcd->task.event_handler = client_task_event_handler;