prebuffer: Use new sched helper functions.
[paraslash.git] / prebuffer_filter.c
index 759991e62b444e0d1de74d85dbd1bb2f1141485d..7fea2dfccd978c001148e2721273114f7349d4c9 100644 (file)
@@ -6,11 +6,15 @@
 
 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
 
+#include <regex.h>
+#include <stdbool.h>
+
 #include "para.h"
 #include "prebuffer_filter.cmdline.h"
 #include "list.h"
 #include "sched.h"
 #include "ggo.h"
+#include "buffer_tree.h"
 #include "filter.h"
 #include "string.h"
 #include "error.h"
@@ -25,50 +29,28 @@ struct private_prebuffer_data {
        struct timeval barrier;
 };
 
-static ssize_t prebuffer_convert(char *inbuf, size_t inbuf_len,
-               struct filter_node *fn)
+static void prebuffer_pre_select(struct sched *s, struct task *t)
 {
+       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct btr_node *btrn = fn->btrn;
+       size_t iqs = btr_get_input_queue_size(btrn);
        struct private_prebuffer_data *ppd = fn->private_data;
        struct prebuffer_filter_args_info *conf = ppd->conf;
+       struct timeval diff;
 
-       if (inbuf_len == 0) {
-               if (*fn->fc->input_error < 0 && ppd->prebuffered >= 0)
-                       goto prebuffer_end;
-               return 0;
-       }
-       if (ppd->prebuffered < 0) {
-               size_t copy = PARA_MIN(inbuf_len, fn->bufsize - fn->loaded);
-               memcpy(fn->buf + fn->loaded, inbuf, copy);
-               fn->loaded += copy;
-               return copy;
-       }
-       if (ppd->prebuffered + inbuf_len > fn->bufsize) {
-               fn->bufsize = PARA_MAX(2 * fn->bufsize,
-                       ppd->prebuffered + inbuf_len);
-               fn->buf = para_realloc(fn->buf, fn->bufsize);
-       }
-       memcpy(fn->buf + ppd->prebuffered, inbuf, inbuf_len);
-       if (ppd->prebuffered == 0) {
+       t->error = 0;
+       if (iqs == 0)
+               return;
+       if (ppd->barrier.tv_sec == 0) {
                struct timeval tv;
                PARA_INFO_LOG("prebuffer period %dms\n",
                        conf->duration_arg);
                ms2tv(conf->duration_arg, &tv);
                tv_add(&tv, now, &ppd->barrier);
        }
-       ppd->prebuffered += inbuf_len;
-       PARA_DEBUG_LOG("%zu bytes prebuffered\n", ppd->prebuffered);
-       if (*fn->fc->input_error >= 0) {
-               struct timeval diff;
-               if (tv_diff(now, &ppd->barrier, &diff) < 0)
-                       goto out;
-               if (ppd->prebuffered < conf->size_arg)
-                       goto out;
-       }
-prebuffer_end:
-       fn->loaded = ppd->prebuffered;
-       ppd->prebuffered = -1;
-out:
-       return inbuf_len;
+       if (tv_diff(&ppd->barrier, now, &diff) < 0)
+               return sched_min_delay(s);
+       sched_request_timeout(&diff, s);
 }
 
 static void prebuffer_close(struct filter_node *fn)
@@ -77,6 +59,26 @@ static void prebuffer_close(struct filter_node *fn)
        free(fn->buf);
 }
 
+static void prebuffer_post_select(__a_unused struct sched *s, struct task *t)
+{
+       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct btr_node *btrn = fn->btrn;
+       size_t iqs = btr_get_input_queue_size(btrn);
+       struct private_prebuffer_data *ppd = fn->private_data;
+       struct prebuffer_filter_args_info *conf = ppd->conf;
+
+       t->error = 0;
+       if (ppd->barrier.tv_sec == 0)
+               return;
+       if (tv_diff(now, &ppd->barrier, NULL) < 0)
+               return;
+       if (iqs < conf->size_arg)
+               return;
+       btr_splice_out_node(btrn);
+       prebuffer_close(fn);
+       t->error = -E_PREBUFFER_SUCCESS;
+}
+
 static int prebuffer_parse_config(int argc, char **argv, void **config)
 {
        struct prebuffer_filter_args_info *prebuffer_conf
@@ -109,6 +111,11 @@ static void prebuffer_open(struct filter_node *fn)
        fn->buf = para_malloc(fn->bufsize);
 }
 
+static void prebuffer_free_config(void *conf)
+{
+       prebuffer_cmdline_parser_free(conf);
+}
+
 /**
  * The init function of the prebuffer filter.
  *
@@ -121,8 +128,10 @@ void prebuffer_filter_init(struct filter *f)
        prebuffer_cmdline_parser_init(&dummy);
        f->open = prebuffer_open;
        f->close = prebuffer_close;
-       f->convert = prebuffer_convert;
        f->parse_config = prebuffer_parse_config;
+       f->free_config = prebuffer_free_config;
+       f->pre_select = prebuffer_pre_select;
+       f->post_select = prebuffer_post_select;
        f->help = (struct ggo_help) {
                .short_help = prebuffer_filter_args_info_help,
                .detailed_help = prebuffer_filter_args_info_detailed_help