Add forward error correction code to the udp sender/receiver.
[paraslash.git] / vss.c
diff --git a/vss.c b/vss.c
index d205349370f69a9445ff1db7bb1c269b2d6431a7..0801eeb089b05bf3e0f47aad86d953f5ecc4dee0 100644 (file)
--- a/vss.c
+++ b/vss.c
 
 #include "para.h"
 #include "error.h"
+#include "portable_io.h"
+#include "fec.h"
 #include "string.h"
 #include "afh.h"
 #include "afs.h"
 #include "server.h"
 #include "net.h"
 #include "server.cmdline.h"
-#include "vss.h"
 #include "list.h"
+#include "vss.h"
 #include "send.h"
 #include "ipc.h"
 #include "fd.h"
@@ -87,6 +89,268 @@ struct vss_task {
        size_t header_len;
 };
 
+static struct list_head fec_client_list;
+
+struct fec_slice {
+       uint8_t num;
+       uint16_t bytes;
+};
+
+struct fec_group {
+       uint32_t num;
+       uint32_t bytes;
+       uint32_t first_chunk;
+       uint32_t num_chunks;
+       struct timeval duration;
+       struct timeval start;
+       struct timeval slice_duration;
+};
+
+struct fec_client {
+       struct fec_client_parms *fcp;
+       struct fec_parms *parms;
+       struct list_head node;
+       struct timeval stream_start;
+       int first_stream_chunk;
+       struct fec_group group;
+       struct fec_slice slice;
+       const unsigned char **src_data;
+       unsigned char *extra_src_buf;
+       size_t extra_src_buf_size;
+       unsigned char *enc_buf;
+       size_t enc_buf_size;
+};
+
+/**
+ * Get the chunk time of the current audio file.
+ *
+ * \return A pointer to a struct containing the chunk time, or NULL,
+ * if currently no audio file is selected.
+ */
+struct timeval *vss_chunk_time(void)
+{
+       if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
+                       mmd->afd.afhi.chunk_tv.tv_usec == 0)
+               return NULL;
+       return &mmd->afd.afhi.chunk_tv;
+}
+
+static void setup_fec_group(struct fec_client *fc, struct vss_task *vsst)
+{
+       uint32_t num_bytes = 0, chunk_num, max_group_size, last_payload_size;
+       int i, k = fc->fcp->data_slices_per_group;
+       const unsigned char *start_buf = NULL;
+       struct timeval tmp, *chunk_tv = vss_chunk_time();
+
+       assert(chunk_tv);
+       max_group_size = (fc->fcp->max_slice_bytes - FEC_HEADER_SIZE) * k;
+       chunk_num = fc->group.first_chunk;
+       for (;;) {
+               const unsigned char *buf;
+               size_t len;
+
+               if (chunk_num >= mmd->afd.afhi.chunks_total)
+                       break;
+               afh_get_chunk(chunk_num, &mmd->afd.afhi, vsst->map, (const char **)&buf, &len);
+               if (!start_buf)
+                       start_buf = buf;
+               if (num_bytes + len > max_group_size)
+                       break;
+               chunk_num++;
+               num_bytes += len;
+       }
+       assert(start_buf);
+       fc->group.num_chunks = chunk_num - fc->group.first_chunk;
+       fc->group.num++;
+       fc->group.bytes = num_bytes;
+       fc->slice.num = 0;
+       fc->slice.bytes = ROUND_UP(num_bytes, k) / k;
+
+       /* The last slice will not be fully used */
+       last_payload_size = num_bytes % fc->slice.bytes;
+       if (!last_payload_size)
+               last_payload_size = fc->slice.bytes;
+
+       tv_scale(fc->group.num_chunks, chunk_tv, &fc->group.duration);
+       tv_scale(fc->group.first_chunk - fc->first_stream_chunk, chunk_tv,
+               &tmp);
+       tv_add(&fc->stream_start, &tmp, &fc->group.start);
+       tv_divide(fc->fcp->slices_per_group, &fc->group.duration,
+               &fc->group.slice_duration);
+
+       for (i = 0; i < k; i++)
+               fc->src_data[i] = start_buf + i * fc->slice.bytes;
+
+       if ((char *)start_buf + k * fc->slice.bytes > vsst->map + mmd->size) {
+               /* can not use last slice as it goes beyond the map */
+               if (fc->extra_src_buf_size < fc->slice.bytes)
+                       fc->extra_src_buf = para_realloc(fc->extra_src_buf, fc->slice.bytes);
+               memcpy(fc->extra_src_buf, start_buf + (k - 1) * fc->slice.bytes,
+                       last_payload_size);
+               memset(fc->extra_src_buf + last_payload_size, 0,
+                       fc->slice.bytes - last_payload_size);
+               fc->src_data[k - 1] = fc->extra_src_buf;
+       }
+
+}
+
+/**
+ * Write a fec header to a buffer.
+ *
+ * \param buf The buffer to write to.
+ * \param h The fec header to write.
+ */
+static void write_fec_header(struct fec_client *fc)
+{
+       char *buf = (char *)fc->enc_buf;
+
+       write_u32(buf, FEC_MAGIC);
+
+       write_u8(buf + 4, fc->fcp->slices_per_group);
+       write_u8(buf + 5, fc->fcp->data_slices_per_group);
+       write_u32(buf + 6, (uint32_t)0); /* audio header len */
+
+       write_u32(buf + 10, fc->group.num);
+       write_u32(buf + 14, fc->group.bytes);
+
+       write_u8(buf + 18, fc->slice.num);
+       write_u16(buf + 20, fc->slice.bytes);
+       memset(buf + 22, 0, 11);
+}
+
+/**
+ * Return a buffer that marks the end of the stream.
+ *
+ * \return The length of the eof buffer.
+ *
+ * This is used for (multicast) udp streaming where closing the socket on the
+ * sender might not give rise to an eof condition at the peer.
+ */
+size_t vss_get_fec_eof_packet(const char **buf)
+{
+       static const char fec_eof_packet[FEC_HEADER_SIZE] =
+       "\xec\x0d\xcc\xfe\0\0\0\0"
+       "\0\0\0\0\0\0\0\0"
+       "\0\0\0\0\0\0\0\0"
+       "\0\0\0\0\0\0\0\0";
+       *buf = fec_eof_packet;
+       return FEC_HEADER_SIZE;
+}
+
+static void compute_next_fec_slice(struct fec_client *fc, struct vss_task *vsst)
+{
+       if (fc->first_stream_chunk < 0) {
+               fc->stream_start = *now;
+               fc->first_stream_chunk = mmd->current_chunk;
+               fc->group.first_chunk = mmd->current_chunk;
+               fc->group.num = 0;
+               setup_fec_group(fc, vsst);
+       } else if (fc->slice.num == fc->fcp->slices_per_group) {
+               fc->group.first_chunk += fc->group.num_chunks;
+               setup_fec_group(fc, vsst);
+
+       }
+       if (fc->enc_buf_size < fc->slice.bytes + FEC_HEADER_SIZE) {
+               fc->enc_buf_size = fc->slice.bytes + FEC_HEADER_SIZE;
+               fc->enc_buf = para_realloc(fc->enc_buf, fc->enc_buf_size);
+       }
+       write_fec_header(fc);
+       fec_encode(fc->parms, fc->src_data, fc->enc_buf + FEC_HEADER_SIZE,
+               fc->slice.num, fc->slice.bytes);
+}
+
+/**
+ * Add one entry to the list of active fec clients.
+ *
+ * \param fcp Describes the fec parameters to be used for this client.
+ * \param result An opaque pointer that must be used by remove the client later.
+ *
+ * \return Standard.
+ */
+int vss_add_fec_client(struct fec_client_parms *fcp, struct fec_client **result)
+{
+       int ret;
+       struct fec_client *fc;
+
+       if (fcp->max_slice_bytes < FEC_HEADER_SIZE + fcp->data_slices_per_group)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       fc = para_calloc(sizeof(*fc));
+       fc->fcp = fcp;
+       ret = fec_new(fcp->data_slices_per_group, fcp->slices_per_group,
+               &fc->parms);
+       if (ret < 0)
+               goto err;
+       fc->first_stream_chunk = -1; /* stream not yet started */
+       fc->src_data = para_malloc(fc->fcp->slices_per_group * sizeof(char *));
+       para_list_add(&fc->node, &fec_client_list);
+       *result = fc;
+       return 1;
+err:
+       fec_free(fc->parms);
+       free(fc);
+       *result = NULL;
+       return ret;
+}
+
+/**
+ * Remove one entry from the list of active fec clients.
+ *
+ * \param result The client to be removed.
+ */
+void vss_del_fec_client(struct fec_client *fc)
+{
+       list_del(&fc->node);
+       free(fc->src_data);
+       free(fc->enc_buf);
+       free(fc->extra_src_buf);
+       fec_free(fc->parms);
+       free(fc);
+}
+
+/*
+ * Compute if/when next slice is due. If it isn't due yet and \a diff is
+ * not \p Null, compute the time difference next - now, where
+ *
+ *     next = stream_start + (first_group_chunk - first_stream_chunk)
+ *             * chunk_time + slice_num * slice_time
+ */
+static int next_slice_is_due(struct fec_client *fc, struct timeval *diff)
+{
+       struct timeval tmp, next;
+       int ret;
+
+       if (fc->first_stream_chunk < 0)
+               return 1;
+       tv_scale(fc->slice.num, &fc->group.slice_duration, &tmp);
+       tv_add(&tmp, &fc->group.start, &next);
+       ret = tv_diff(&next, now, diff);
+       return ret < 0? 1 : 0;
+}
+
+static void compute_slice_timeout(struct timeval *timeout)
+{
+       struct fec_client *fc;
+
+       assert(vss_playing());
+       list_for_each_entry(fc, &fec_client_list, node) {
+               struct timeval diff;
+               int ret = next_slice_is_due(fc, &diff);
+
+               // PARA_NOTICE_LOG("diff: %lu, ret: %d\n", tv2ms(&diff), ret);
+               if (ret) {
+                       timeout->tv_sec = 0;
+                       timeout->tv_usec = 0;
+                       goto out;
+               }
+               /* timeout = min(timeout, diff) */
+               if (tv_diff(&diff, timeout, NULL) < 0)
+                       *timeout = diff;
+       }
+out:
+       return;
+       PARA_NOTICE_LOG("slice timeout: %lu:%lu\n", (long unsigned)timeout->tv_sec, (long unsigned)timeout->tv_usec);
+}
+
 /**
  * Check if vss status flag \a P (playing) is set.
  *
@@ -184,16 +448,20 @@ static struct timeval *vss_compute_timeout(struct vss_task *vsst)
                return NULL;
        compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
                &mmd->stream_start, &next_chunk);
-       if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) < 0)
+       if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) >= 0) {
+               /* chunk is due or bof */
+               the_timeout.tv_sec = 0;
+               the_timeout.tv_usec = 0;
                return &the_timeout;
-       /* chunk is due or bof */
-       the_timeout.tv_sec = 0;
-       the_timeout.tv_usec = 0;
+       }
+       /* compute min of current timeout and next slice time */
+       compute_slice_timeout(&the_timeout);
        return &the_timeout;
 }
 
 static void vss_eof(struct vss_task *vsst)
 {
+
        mmd->stream_start = *now;
        if (!vsst->map)
                return;
@@ -230,20 +498,6 @@ const char *supported_audio_formats(void)
        return SUPPORTED_AUDIO_FORMATS;
 }
 
-/**
- * Get the chunk time of the current audio file.
- *
- * \return A pointer to a struct containing the chunk time, or NULL,
- * if currently no audio file is selected.
- */
-struct timeval *vss_chunk_time(void)
-{
-       if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
-                       mmd->afd.afhi.chunk_tv.tv_usec == 0)
-               return NULL;
-       return &mmd->afd.afhi.chunk_tv;
-}
-
 static int need_to_request_new_audio_file(struct vss_task *vsst)
 {
        struct timeval diff;
@@ -262,6 +516,8 @@ static int need_to_request_new_audio_file(struct vss_task *vsst)
        return 1;
 }
 
+
+
 /**
  * Compute the timeout for para_server's main select-loop.
  *
@@ -285,9 +541,14 @@ static void vss_pre_select(struct sched *s, struct task *t)
        struct timeval *tv, diff;
        struct vss_task *vsst = container_of(t, struct vss_task, task);
 
-       if (!vsst->map || vss_next() || vss_paused() || vss_repos())
+       if (!vsst->map || vss_next() || vss_paused() || vss_repos()) {
+               struct fec_client *fc, *tmp;
                for (i = 0; senders[i].name; i++)
-                       senders[i].shutdown_clients();
+                       if (senders[i].shutdown_clients)
+                               senders[i].shutdown_clients();
+               list_for_each_entry_safe(fc, tmp, &fec_client_list, node)
+                       fc->first_stream_chunk = -1;
+       }
        if (vss_next())
                vss_eof(vsst);
        else if (vss_paused()) {
@@ -407,25 +668,20 @@ err:
 /**
  * Main sending function.
  *
- * This function gets called from para_server as soon as the next chunk of data
- * should be pushed out. It obtains a pointer to the data to be sent out as
- * well as its length from mmd->afd.afhi. This information is then passed to
- * each supported sender's send() function which is supposed to send out the data
- * to all connected clients.
+ * This function gets called from vss_post_select(). It checks whether the next
+ * chunk of data should be pushed out. It obtains a pointer to the data to be
+ * sent out as well as its length from mmd->afd.afhi. This information is then
+ * passed to each supported sender's send() function as well as to the send()
+ * functions of each registered fec client.
  */
-static void vss_send_chunk(struct vss_task *vsst)
+static void vss_send(struct vss_task *vsst)
 {
        int i;
        struct timeval due;
-       const char *buf;
-       size_t len;
+       struct fec_client *fc, *tmp_fc;
 
        if (!vsst->map || !vss_playing())
                return;
-       compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
-               &mmd->stream_start, &due);
-       if (tv_diff(&due, now, NULL) > 0)
-               return;
        if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
                return;
        if (chk_barrier("data send", &vsst->data_send_barrier,
@@ -435,10 +691,6 @@ static void vss_send_chunk(struct vss_task *vsst)
                mmd->new_vss_status_flags |= VSS_NEXT;
                return;
        }
-       /*
-        * We call the send function also in case of empty chunks as they
-        * might have still some data queued which can be sent in this case.
-        */
        if (!mmd->chunks_sent) {
                struct timeval tmp;
                mmd->stream_start = *now;
@@ -446,10 +698,36 @@ static void vss_send_chunk(struct vss_task *vsst)
                mmd->offset = tv2ms(&tmp);
                mmd->events++;
        }
-       afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map, &buf, &len);
-       for (i = 0; senders[i].name; i++)
-               senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len,
-                       vsst->header_buf, vsst->header_len);
+       compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
+               &mmd->stream_start, &due);
+       if (tv_diff(&due, now, NULL) <= 0) {
+               const char *buf;
+               size_t len;
+               /*
+                * We call the send function also in case of empty chunks as
+                * they might have still some data queued which can be sent in
+                * this case.
+                */
+               afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map,
+                       &buf, &len);
+               for (i = 0; senders[i].name; i++) {
+                       if (!senders[i].send)
+                               continue;
+                       senders[i].send(mmd->current_chunk, mmd->chunks_sent,
+                               buf, len, vsst->header_buf, vsst->header_len);
+               }
+       }
+       list_for_each_entry_safe(fc, tmp_fc, &fec_client_list, node) {
+               if (!next_slice_is_due(fc, NULL))
+                       continue;
+               compute_next_fec_slice(fc, vsst);
+               PARA_DEBUG_LOG("sending %d:%d (%zu bytes)\n", fc->group.num,
+                       fc->slice.num, fc->slice.bytes);
+               fc->fcp->send((char *)fc->enc_buf,
+                       fc->slice.bytes + FEC_HEADER_SIZE,
+                       fc->fcp->private_data);
+               fc->slice.num++;
+       }
        mmd->new_vss_status_flags |= VSS_PLAYING;
        mmd->chunks_sent++;
        mmd->current_chunk++;
@@ -484,7 +762,7 @@ static void vss_post_select(struct sched *s, struct task *t)
        if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
                        (vss_next() && vss_playing()))
                tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
-       vss_send_chunk(vsst);
+       vss_send(vsst);
 }
 
 /**
@@ -527,5 +805,6 @@ void init_vss_task(int afs_socket)
                tv_add(&vsst->autoplay_barrier, &vsst->announce_tv,
                        &vsst->data_send_barrier);
        }
+       INIT_LIST_HEAD(&fec_client_list);
        register_task(&vsst->task);
 }