audiod: Make writer_cleanup() accept NULL pointer argument.
[paraslash.git] / audiod.c
index 3cc0921026b3a4980847b7bc5032da5dee3757fe..322ce3be64a3ccc1030b1c7de0018805813ebcc9 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -196,13 +196,25 @@ char *get_time_string(int slot_num)
        }
        if (audiod_status == AUDIOD_ON && !s)
                goto empty;
-       /* valid status items and playing */
+       /*
+        * Valid status items and playing, set length and tmp to the stream
+        * start. We use the slot info and fall back to the info from current
+        * status items if no slot info is available.
+        */
+       length = stat_task->length_seconds;
+       tmp = &stat_task->server_stream_start;
        if (s && s->wns) { /* writer active in this slot */
-               length = s->seconds_total;
-               tmp = &s->server_stream_start;
-       } else { /* standby mode, rely on status items */
-               length = stat_task->length_seconds;
-               tmp = &stat_task->server_stream_start;
+               btr_get_node_start(s->wns[0].btrn, &wstime);
+               if (wstime.tv_sec != 0) { /* writer wrote something */
+                       if (s->server_stream_start.tv_sec == 0) {
+                               /* copy status info to slot */
+                               s->server_stream_start = stat_task->server_stream_start;
+                               s->offset_seconds = stat_task->offset_seconds;
+                               s->seconds_total = stat_task->length_seconds;
+                       }
+                       length = s->seconds_total;
+                       tmp = &s->server_stream_start;
+               }
        }
        if (stat_task->sa_time_diff_sign > 0)
                tv_diff(tmp, &stat_task->sa_time_diff, &sss);
@@ -214,7 +226,6 @@ char *get_time_string(int slot_num)
                seconds = diff.tv_sec + stat_task->offset_seconds;
                goto out;
        }
-       btr_get_node_start(s->wns[0].btrn, &wstime);
        tv_diff(now, &wstime, &wtime);
        //PARA_CRIT_LOG("offset %d\n", s->offset_seconds);
        seconds = s->offset_seconds;
@@ -313,6 +324,7 @@ static void close_receiver(int slot_num)
 {
        struct slot_info *s = &slot[slot_num];
        struct audio_format_info *a;
+       struct timeval restart_delay = {0, 200 * 1000};
 
        if (s->format < 0 || !s->receiver_node)
                return;
@@ -323,13 +335,17 @@ static void close_receiver(int slot_num)
        btr_free_node(s->receiver_node->btrn);
        free(s->receiver_node);
        s->receiver_node = NULL;
-       stat_task->current_audio_format_num = -1;
+       tv_add(now, &restart_delay, &afi[s->format].restart_barrier);
 }
 
 static void writer_cleanup(struct writer_node *wn)
 {
-       struct writer *w = writers + wn->writer_num;
+       struct writer *w;
 
+       if (!wn)
+               return;
+       w = writers + wn->writer_num;
+       PARA_INFO_LOG("closing %s\n", writer_names[wn->writer_num]);
        w->close(wn);
        btr_free_node(wn->btrn);
 }
@@ -475,10 +491,6 @@ static void open_writers(struct slot_info *s)
                        register_writer_node(wn, parent);
                }
        }
-       s->server_stream_start = stat_task->server_stream_start.tv_sec?
-               stat_task->server_stream_start : *now;
-       s->offset_seconds = stat_task->offset_seconds;
-       s->seconds_total = stat_task->length_seconds;
 }
 
 /* returns slot num on success */
@@ -541,6 +553,29 @@ static int receiver_running(int format)
        return ret;
 }
 
+struct btr_node *audiod_get_btr_root(void)
+{
+       int i, newest_slot = -1;
+       struct timeval newest_rstime = {0, 0};
+
+       FOR_EACH_SLOT(i) {
+               struct slot_info *s = &slot[i];
+               struct timeval rstime;
+               if (!s->receiver_node)
+                       continue;
+               if (s->receiver_node->task.error < 0)
+                       continue;
+               btr_get_node_start(s->receiver_node->btrn, &rstime);
+               if (newest_slot >= 0 && tv_diff(&rstime, &newest_rstime, NULL) < 0)
+                       continue;
+               newest_rstime = rstime;
+               newest_slot = i;
+       }
+       if (newest_slot == -1)
+               return NULL;
+       return slot[newest_slot].receiver_node->btrn;
+}
+
 /* returns slot num on success. */
 static int open_current_receiver(struct sched *s)
 {
@@ -563,11 +598,13 @@ static int open_current_receiver(struct sched *s)
                 * belongs to the file just completed.
                 */
                if (stat_task->server_stream_start.tv_sec != 0)
-                       return -1;
+                       sched_request_timeout_ms(100, s);
        }
        if (tv_diff(now, &afi[cafn].restart_barrier, &diff) < 0) {
-               /* avoid busy loop */
-               s->timeout = diff;
+               if (tv_diff(&s->timeout, &diff, NULL) > 0)
+                       s->timeout = diff;
+               else
+                       sched_min_delay(s);
                return -1;
        }
        /* start a new receiver */
@@ -1006,27 +1043,28 @@ static void set_stat_task_restart_barrier(unsigned seconds)
        tv_add(now, &delay, &stat_task->restart_barrier);
 }
 
-static void try_to_close_slot(int slot_num)
+static bool try_to_close_slot(int slot_num)
 {
        struct slot_info *s = &slot[slot_num];
        struct audio_format_info *a = afi + s->format;
        int i;
 
        if (s->format < 0)
-               return;
+               return false;
        if (s->receiver_node && s->receiver_node->task.error != -E_TASK_UNREGISTERED)
-               return;
+               return false;
        for (i = 0; i < a->num_filters; i++)
                if (s->fns && s->fns[i].task.error != -E_TASK_UNREGISTERED)
-                       return;
+                       return false;
        for (i = 0; i < a->num_writers; i++)
                if (s->wns && s->wns[i].task.error != -E_TASK_UNREGISTERED)
-                       return;
+                       return false;
        PARA_INFO_LOG("closing slot %d\n", slot_num);
        close_writers(s);
        _close_filters(s);
        close_receiver(slot_num);
        clear_slot(slot_num);
+       return true;
 }
 
 /*
@@ -1040,7 +1078,8 @@ static void start_stop_decoders(struct sched *s)
        struct audio_format_info *a;
 
        FOR_EACH_SLOT(i)
-               try_to_close_slot(i);
+               if (try_to_close_slot(i))
+                       sched_min_delay(s);
 //     if (audiod_status != AUDIOD_ON ||
 //                     !(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING))
 //             return kill_all_decoders(-E_NOT_PLAYING);
@@ -1052,9 +1091,9 @@ static void start_stop_decoders(struct sched *s)
        if (a->num_filters)
                open_filters(sl);
        open_writers(sl);
+       activate_grab_clients();
        btr_log_tree(sl->receiver_node->btrn, LL_NOTICE);
-       s->timeout.tv_sec = 0;
-       s->timeout.tv_usec = 1;
+       sched_min_delay(s);
 }
 
 /* restart the client task if necessary */
@@ -1105,18 +1144,20 @@ static void status_pre_select(struct sched *s, struct task *t)
        if (tv_diff(now, &st->restart_barrier, NULL) < 0)
                goto out;
        if (st->clock_diff_count) { /* get status only one time */
-               char *argv[] = {"audiod", "--", "stat", "-p", "1", NULL};
+               char *argv[] = {"audiod", "--", "stat", "-p", "-n=1", NULL};
                int argc = 5;
                PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
                st->clock_diff_count--;
                client_open(argc, argv, &st->ct, NULL);
                set_stat_task_restart_barrier(2);
+               sched_min_delay(s);
 
        } else {
                char *argv[] = {"audiod", "--", "stat", "-p", NULL};
                int argc = 4;
                client_open(argc, argv, &st->ct, NULL);
                set_stat_task_restart_barrier(5);
+               sched_min_delay(s);
        }
        free(stat_item_values[SI_BASENAME]);
        stat_item_values[SI_BASENAME] = para_strdup(
@@ -1242,7 +1283,6 @@ int main(int argc, char *argv[])
        set_initial_status();
        FOR_EACH_SLOT(i)
                clear_slot(i);
-       init_grabbing();
        setup_signal_handling();
        signal_setup_default(sig_task);