1 /* Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
9 #include "filter_cmd.lsg.h"
12 #include "buffer_tree.h"
17 /** Data specific to the prebuffer filter. */
18 struct private_prebuffer_data {
19 /** Number of bytes prebuffered or -1 if no longer prebuffering. */
21 /** End of prebuffering period. */
22 struct timeval barrier;
25 static void prebuffer_pre_select(struct sched *s, void *context)
27 struct filter_node *fn = context;
28 struct btr_node *btrn = fn->btrn;
29 size_t iqs = btr_get_input_queue_size(btrn);
30 struct private_prebuffer_data *ppd = fn->private_data;
35 if (ppd->barrier.tv_sec == 0) {
36 uint32_t duration = FILTER_CMD_OPT_UINT32_VAL(PREBUFFER,
39 PARA_INFO_LOG("prebuffer period %" PRIu32 "ms\n", duration);
41 tv_add(&tv, now, &ppd->barrier);
43 if (tv_diff(&ppd->barrier, now, &diff) < 0)
44 return sched_min_delay(s);
45 sched_request_timeout(&diff, s);
48 static void prebuffer_close(struct filter_node *fn)
50 free(fn->private_data);
53 static int prebuffer_post_select(__a_unused struct sched *s, void *context)
55 struct filter_node *fn = context;
56 struct btr_node *btrn = fn->btrn;
57 size_t iqs = btr_get_input_queue_size(btrn);
58 struct private_prebuffer_data *ppd = fn->private_data;
59 uint32_t size = FILTER_CMD_OPT_UINT32_VAL(PREBUFFER, SIZE, fn->lpr);
62 ret = task_get_notification(fn->task);
65 ret = btr_node_status(btrn, size, BTR_NT_INTERNAL);
68 if (ppd->barrier.tv_sec == 0)
70 if (tv_diff(now, &ppd->barrier, NULL) < 0)
74 btr_splice_out_node(&fn->btrn);
75 return -E_PREBUFFER_SUCCESS;
77 btr_remove_node(&fn->btrn);
81 static void prebuffer_open(struct filter_node *fn)
83 struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
84 fn->private_data = ppd;
87 const struct filter lsg_filter_cmd_com_prebuffer_user_data = {
88 .open = prebuffer_open,
89 .close = prebuffer_close,
90 .pre_select = prebuffer_pre_select,
91 .post_select = prebuffer_post_select,