8ca1630d8112242ee65b3feb63e2883ffdac7f86
[paraslash.git] / prebuffer_filter.c
1 /*
2 * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
8
9 #include <regex.h>
10 #include <lopsub.h>
11
12 #include "para.h"
13 #include "filter_cmd.lsg.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "error.h"
20
21 /** Data specific to the prebuffer filter. */
22 struct private_prebuffer_data {
23 /** Number of bytes prebuffered or -1 if no longer prebuffering. */
24 int prebuffered;
25 /** End of prebuffering period. */
26 struct timeval barrier;
27 };
28
29 static void prebuffer_pre_select(struct sched *s, void *context)
30 {
31 struct filter_node *fn = context;
32 struct btr_node *btrn = fn->btrn;
33 size_t iqs = btr_get_input_queue_size(btrn);
34 struct private_prebuffer_data *ppd = fn->private_data;
35 struct timeval diff;
36
37 if (iqs == 0)
38 return;
39 if (ppd->barrier.tv_sec == 0) {
40 uint32_t duration = FILTER_CMD_OPT_UINT32_VAL(PREBUFFER,
41 DURATION, fn->lpr);
42 struct timeval tv;
43 PARA_INFO_LOG("prebuffer period %" PRIu32 "ms\n", duration);
44 ms2tv(duration, &tv);
45 tv_add(&tv, now, &ppd->barrier);
46 }
47 if (tv_diff(&ppd->barrier, now, &diff) < 0)
48 return sched_min_delay(s);
49 sched_request_timeout(&diff, s);
50 }
51
52 static void prebuffer_close(struct filter_node *fn)
53 {
54 free(fn->private_data);
55 }
56
57 static int prebuffer_post_select(__a_unused struct sched *s, void *context)
58 {
59 struct filter_node *fn = context;
60 struct btr_node *btrn = fn->btrn;
61 size_t iqs = btr_get_input_queue_size(btrn);
62 struct private_prebuffer_data *ppd = fn->private_data;
63 uint32_t size = FILTER_CMD_OPT_UINT32_VAL(PREBUFFER, SIZE, fn->lpr);
64
65 if (ppd->barrier.tv_sec == 0)
66 return 0;
67 if (tv_diff(now, &ppd->barrier, NULL) < 0)
68 return 0;
69 if (iqs < size)
70 return 0;
71 btr_splice_out_node(&fn->btrn);
72 return -E_PREBUFFER_SUCCESS;
73 }
74
75 static void prebuffer_open(struct filter_node *fn)
76 {
77 struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
78 fn->private_data = ppd;
79 }
80
81 const struct filter lsg_filter_cmd_com_prebuffer_user_data = {
82 .open = prebuffer_open,
83 .close = prebuffer_close,
84 .pre_select = prebuffer_pre_select,
85 .post_select = prebuffer_post_select,
86 };