Add btr pool support to the fecdec filter.
[paraslash.git] / udp_recv.c
index f9782985d113d390c70300d099546066203f188d..3d680814c163816cd8dd8cdbe9f579a955c5b4fc 100644 (file)
 #include "ggo.h"
 #include "recv.h"
 #include "udp_recv.cmdline.h"
-#include "audiod.h"
 #include "string.h"
 #include "net.h"
 #include "fd.h"
+#include "buffer_tree.h"
 
 /** The size of the receiver node buffer. */
 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
 struct private_udp_recv_data {
        /** The socket file descriptor. */
        int fd;
+       struct btr_pool *btrp;
 };
 
 static void udp_recv_pre_select(struct sched *s, struct task *t)
 {
        struct receiver_node *rn = container_of(t, struct receiver_node, task);
        struct private_udp_recv_data *purd = rn->private_data;
+       int ret;
 
+       if (rn->btrn) {
+               ret = generic_recv_pre_select(s, t);
+               if (ret <= 0)
+                       return;
+       }
        para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
 }
 
@@ -59,7 +66,7 @@ static int add_rn_output(struct receiver_node *rn, char *buf, size_t len)
        return 1;
 }
 
-static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
+static void udp_recv_post_select_nobtr(__a_unused struct sched *s, struct task *t)
 {
        struct receiver_node *rn = container_of(t, struct receiver_node, task);
        struct private_udp_recv_data *purd = rn->private_data;
@@ -94,6 +101,57 @@ success:
        t->error = 1;
 }
 
+static void udp_recv_post_select_btr(__a_unused struct sched *s, struct task *t)
+{
+       struct receiver_node *rn = container_of(t, struct receiver_node, task);
+       struct private_udp_recv_data *purd = rn->private_data;
+       struct btr_node *btrn = rn->btrn;
+       int ret;
+       char *buf = NULL;
+       size_t packet_size, bufsize;
+
+       t->error = 0;
+       ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+       if (ret < 0)
+               goto err;
+       if (ret == 0)
+               return;
+       if (!FD_ISSET(purd->fd, &s->rfds))
+               return;
+       bufsize = btr_pool_get_buffer(purd->btrp, &buf);
+       ret = -E_UDP_OVERRUN;
+       if (bufsize == 0)
+               goto err;
+       ret = recv_bin_buffer(purd->fd, buf, bufsize);
+       if (ret == 0)
+               ret = -E_RECV_EOF;
+       if (ret < 0)
+               goto err;
+       packet_size = ret;
+       if (packet_size >= FEC_EOF_PACKET_LEN) {
+               if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) {
+                       PARA_INFO_LOG("received eof packet\n");
+                       ret = -E_RECV_EOF;
+                       goto err;
+               }
+       }
+       btr_add_output_pool(purd->btrp, packet_size, btrn);
+       return;
+err:
+       btr_remove_node(btrn);
+       t->error = ret;
+       close(purd->fd);
+       purd->fd = -1;
+}
+
+static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
+{
+       struct receiver_node *rn = container_of(t, struct receiver_node, task);
+       if (rn->btrn)
+               return udp_recv_post_select_btr(s, t);
+       udp_recv_post_select_nobtr(s, t);
+}
+
 static void udp_shutdown(void)
 {
        return;
@@ -105,6 +163,8 @@ static void udp_recv_close(struct receiver_node *rn)
 
        if (purd->fd >= 0)
                close(purd->fd);
+       PARA_CRIT_LOG("%p: close\n", rn);
+       btr_pool_free(purd->btrp);
        free(rn->private_data);
        free(rn->buf);
 }
@@ -206,10 +266,13 @@ static int udp_recv_open(struct receiver_node *rn)
        }
 
        ret = mark_fd_nonblocking(purd->fd);
-       if (ret < 0)
+       if (ret < 0) {
+               close(purd->fd);
                goto err;
-       PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
+       }
+       PARA_CRIT_LOG("rn %p: receiving from %s:%d, fd=%d\n", rn, c->host_arg,
                c->port_arg, purd->fd);
+       purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
        return purd->fd;
 err:
        free(rn->private_data);
@@ -217,6 +280,11 @@ err:
        return ret;
 }
 
+static void udp_recv_free_config(void *conf)
+{
+       udp_recv_cmdline_parser_free(conf);
+}
+
 /**
  * The init function of the udp receiver.
  *
@@ -235,6 +303,7 @@ void udp_recv_init(struct receiver *r)
        r->pre_select = udp_recv_pre_select;
        r->post_select = udp_recv_post_select;
        r->parse_config = udp_recv_parse_config;
+       r->free_config = udp_recv_free_config;
        r->help = (struct ggo_help) {
                .short_help = udp_recv_args_info_help,
                .detailed_help = udp_recv_args_info_detailed_help