Simplify dispatch_slice().
[paraslash.git] / fecdec_filter.c
index 87b6a3669d6f1ab929dbf7a913f2bf237dcfaef6..376fe6b3cc66ab5965b05b1d479a7ba7b34fe0ed 100644 (file)
@@ -28,7 +28,7 @@
 #define NUM_FEC_GROUPS 3
 
 /** Size of the output buffer of the fecdec filter. */
-#define FECDEC_OUTBUF_SIZE 81920
+#define FECDEC_OUTBUF_SIZE (128 * 1024)
 
 /** Data read from the header of a slice. */
 struct fec_header {
@@ -78,14 +78,8 @@ struct private_fecdec_data {
 #define FOR_EACH_FECDEC_GROUP(g, d) for (g = (d)->groups; \
        (g) - (d)->groups < NUM_FEC_GROUPS; (g)++)
 
-/** */
-#define UNUSED_GROUP_NUM 0xffffffff
-
 static int group_complete(struct fecdec_group *fg)
 {
-       if (fg->h.group_num == UNUSED_GROUP_NUM)
-               return 0;
-       //PARA_INFO_LOG("received slices: %u, slices per group: %u\n", fg->num_received_slices, fg->h.data_slices_per_group);
        return fg->num_received_slices >= fg->h.data_slices_per_group;
 }
 
@@ -98,10 +92,6 @@ static void clear_group(struct fecdec_group *fg)
 {
        int i;
 
-       if (!group_complete(fg) && !group_empty(fg))
-               PARA_WARNING_LOG("Clearing incomplete group %d "
-                       "(contains %d slices)\n", fg->h.group_num,
-                       fg->num_received_slices);
        for (i = 0; i < fg->num_slices; i++) {
                free(fg->data[i]);
                fg->data[i] = NULL;
@@ -112,7 +102,6 @@ static void clear_group(struct fecdec_group *fg)
        fg->num_slices = 0;
        memset(&fg->h, 0, sizeof(struct fec_header));
        fg->num_received_slices = 0;
-       fg->h.group_num = UNUSED_GROUP_NUM;
 }
 
 static int find_group(struct fec_header *h,
@@ -123,7 +112,10 @@ static int find_group(struct fec_header *h,
        FOR_EACH_FECDEC_GROUP(fg, pfd) {
                if (fg->h.group_num != h->group_num)
                        continue;
-               /* FIXME: Add some more sanity checks here */
+               if (fg->h.slices_per_group != h->slices_per_group)
+                       continue;
+               if (fg->h.data_slices_per_group != h->data_slices_per_group)
+                       continue;
                *result = fg;
                return 1;
        }
@@ -162,10 +154,15 @@ static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd)
                if (!oldest || oldest->h.group_num > fg->h.group_num)
                        oldest = fg;
        }
+       if (!group_complete(oldest) && !group_empty(oldest))
+               PARA_WARNING_LOG("Clearing incomplete group %d "
+                       "(contains %d slices)\n", fg->h.group_num,
+                       fg->num_received_slices);
        clear_group(oldest);
        return oldest;
 }
 
+/* returns 1 if the group was found, 0 if not, negative on errors */
 static int get_group(struct fec_header *h, struct private_fecdec_data *pfd,
                struct fecdec_group **result)
 {
@@ -179,24 +176,33 @@ static int get_group(struct fec_header *h, struct private_fecdec_data *pfd,
        /* group not found */
        fg = find_unused_group(pfd);
        if (fg)
-               goto update_header;
+               goto success;
        fg = try_to_free_group(pfd);
        if (fg)
-               goto update_header;
+               goto success;
        fg = free_oldest_group(pfd);
-update_header:
-       fg->h = *h;
+       ret = 0;
 success:
+       fg->h = *h;
        *result = fg;
-       return 1;
+       return ret;
 }
 
+/*
+ * returns 1 if slice was added, zero otherwise (because the group was already
+ * complete). In any case the number of received slices is being increased by
+ * one.
+ */
 static int add_slice(char *buf, struct fecdec_group *fg)
 {
        int r, slice_num;
 
-       if (group_complete(fg))
+       if (group_complete(fg)) {
+               PARA_DEBUG_LOG("group complete, ignoring slice %d\n",
+                       fg->h.slice_num);
+               fg->num_received_slices++;
                return 0;
+       }
        slice_num = fg->h.slice_num;
        if (fg->num_slices == 0) {
                fg->num_slices = fg->h.slices_per_group;
@@ -268,6 +274,7 @@ static int read_fec_header(char *buf, size_t len, struct fec_header *h)
        return 1;
 }
 
+/* returns 1 if we used the buffer, 0 if we didn't, negative on errors */
 static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
                struct filter_node *fn)
 {
@@ -280,15 +287,8 @@ static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
        ret = get_group(h, pfd, &fg);
        if (ret < 0)
                return ret;
-       if (group_complete(fg)) {
-               PARA_DEBUG_LOG("group complete, ignoring slice %d\n",
-                       h->slice_num);
+       if (!add_slice(buf, fg))
                return 1;
-       }
-       fg->h = *h;
-       ret = add_slice(buf, fg);
-       if (ret < 0)
-               return ret;
        if (group_complete(fg)) {
                if (!pfd->fec) {
                        int k = h->data_slices_per_group, n = h->slices_per_group;