vss: Make fec_active a bool.
[paraslash.git] / vss.c
1 /*
2  * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file vss.c The virtual streaming system.
8  *
9  * This contains the audio streaming code of para_server which is independent
10  * of the current audio format, audio file selector and of the activated
11  * senders.
12  */
13
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <regex.h>
17 #include <osl.h>
18 #include <sys/types.h>
19 #include <arpa/inet.h>
20 #include <sys/un.h>
21 #include <netdb.h>
22 #include <lopsub.h>
23
24 #include "server.lsg.h"
25 #include "para.h"
26 #include "error.h"
27 #include "portable_io.h"
28 #include "fec.h"
29 #include "string.h"
30 #include "afh.h"
31 #include "afs.h"
32 #include "server.h"
33 #include "net.h"
34 #include "list.h"
35 #include "send.h"
36 #include "sched.h"
37 #include "vss.h"
38 #include "ipc.h"
39 #include "fd.h"
40
41 extern struct misc_meta_data *mmd;
42
43 extern void dccp_send_init(struct sender *);
44 extern void http_send_init(struct sender *);
45 extern void udp_send_init(struct sender *);
46
47 /** The list of supported senders. */
48 struct sender senders[] = {
49         {
50                 .name = "http",
51                 .init = http_send_init,
52         },
53         {
54                 .name = "dccp",
55                 .init = dccp_send_init,
56         },
57         {
58                 .name = "udp",
59                 .init = udp_send_init,
60         },
61         {
62                 .name = NULL,
63         }
64 };
65
66 /** The possible states of the afs socket. */
67 enum afs_socket_status {
68         /** Socket is inactive. */
69         AFS_SOCKET_READY,
70         /** Socket fd was included in the write fd set for select(). */
71         AFS_SOCKET_CHECK_FOR_WRITE,
72         /** vss wrote a request to the socket and waits for reply from afs. */
73         AFS_SOCKET_AFD_PENDING
74 };
75
76 /** The task structure for the virtual streaming system. */
77 struct vss_task {
78         /** Copied from the -announce_time command line option. */
79         struct timeval announce_tv;
80         /** End of the announcing interval. */
81         struct timeval data_send_barrier;
82         /** End of the EOF interval. */
83         struct timeval eof_barrier;
84         /** Only used if --autoplay_delay was given. */
85         struct timeval autoplay_barrier;
86         /** Used for afs-server communication. */
87         int afs_socket;
88         /** The current state of \a afs_socket. */
89         enum afs_socket_status afsss;
90         /** The memory mapped audio file. */
91         char *map;
92         /** The size of the memory mapping. */
93         size_t mapsize;
94         /** Used by the scheduler. */
95         struct task *task;
96         /** Pointer to the header of the mapped audio file. */
97         char *header_buf;
98         /** Length of the audio file header. */
99         size_t header_len;
100         /** Time between audio file headers are sent. */
101         struct timeval header_interval;
102         /* Only used if afh supports dynamic chunks. */
103         void *afh_context;
104 };
105
106 /**
107  * The list of currently connected fec clients.
108  *
109  * Senders may use \ref vss_add_fec_client() to add entries to the list.
110  */
111 static struct list_head fec_client_list;
112
113 /**
114  * Data associated with one FEC group.
115  *
116  * A FEC group consists of a fixed number of slices and this number is given by
117  * the \a slices_per_group parameter of struct \ref fec_client_parms. Each FEC
118  * group contains a number of chunks of the current audio file.
119  *
120  * FEC slices directly correspond to the data packages sent by the paraslash
121  * senders that use FEC. Each slice is identified by its group number and its
122  * number within the group. All slices have the same size, but the last slice
123  * of the group may not be filled entirely.
124  */
125 struct fec_group {
126         /** The number of the FEC group. */
127         uint32_t num;
128         /** Number of bytes in this group. */
129         uint32_t bytes;
130         /** The first chunk of the current audio file belonging to the group. */
131         uint32_t first_chunk;
132         /** The number of chunks contained in this group. */
133         uint32_t num_chunks;
134         /** When the first chunk was sent. */
135         struct timeval start;
136         /** The duration of the full group. */
137         struct timeval duration;
138         /** The group duration divided by the number of slices. */
139         struct timeval slice_duration;
140         /** Group contains the audio file header that occupies that many slices. */
141         uint8_t num_header_slices;
142         /** Number of bytes per slice for this group. */
143         uint16_t slice_bytes;
144 };
145
146 /** A FEC client is always in one of these states. */
147 enum fec_client_state {
148         FEC_STATE_NONE = 0,     /**< not initialized and not enabled */
149         FEC_STATE_DISABLED,     /**< temporarily disabled */
150         FEC_STATE_READY_TO_RUN  /**< initialized and enabled */
151 };
152
153 /**
154  * Describes one connected FEC client.
155  */
156 struct fec_client {
157         /** Current state of the client */
158         enum fec_client_state state;
159         /** The connected sender client (transport layer). */
160         struct sender_client *sc;
161         /** Parameters requested by the client. */
162         struct fec_client_parms *fcp;
163         /** Used by the core FEC code. */
164         struct fec_parms *parms;
165         /** The position of this client in the fec client list. */
166         struct list_head node;
167         /** When the first slice for this client was sent. */
168         struct timeval stream_start;
169         /** The first chunk sent to this FEC client. */
170         int first_stream_chunk;
171         /** Describes the current group. */
172         struct fec_group group;
173         /** The current slice. */
174         uint8_t current_slice_num;
175         /** The data to be FEC-encoded (point to a region within the mapped audio file). */
176         const unsigned char **src_data;
177         /** Last time an audio  header was sent. */
178         struct timeval next_header_time;
179         /** Used for the last source pointer of an audio file. */
180         unsigned char *extra_src_buf;
181         /** Needed for the last slice of the audio file header. */
182         unsigned char *extra_header_buf;
183         /** Extra slices needed to store largest chunk + header. */
184         int num_extra_slices;
185         /** Contains the FEC-encoded data. */
186         unsigned char *enc_buf;
187         /** Maximal packet size. */
188         int mps;
189 };
190
191 /**
192  * Get the chunk time of the current audio file.
193  *
194  * \return A pointer to a struct containing the chunk time, or NULL,
195  * if currently no audio file is selected.
196  */
197 struct timeval *vss_chunk_time(void)
198 {
199         if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
200                         mmd->afd.afhi.chunk_tv.tv_usec == 0)
201                 return NULL;
202         return &mmd->afd.afhi.chunk_tv;
203 }
204
205 /**
206  * Write a fec header to a buffer.
207  *
208  * \param buf The buffer to write to.
209  * \param h The fec header to write.
210  */
211 static void write_fec_header(struct fec_client *fc, struct vss_task *vsst)
212 {
213         char *buf = (char *)fc->enc_buf;
214         struct fec_group *g = &fc->group;
215         struct fec_client_parms *p = fc->fcp;
216
217         write_u32(buf, FEC_MAGIC);
218
219         write_u8(buf + 4, p->slices_per_group + fc->num_extra_slices);
220         write_u8(buf + 5, p->data_slices_per_group + fc->num_extra_slices);
221         write_u32(buf + 6, g->num_header_slices? vsst->header_len : 0);
222
223         write_u32(buf + 10, g->num);
224         write_u32(buf + 14, g->bytes);
225
226         write_u8(buf + 18, fc->current_slice_num);
227         write_u8(buf + 19, 0); /* unused */
228         write_u16(buf + 20, g->slice_bytes);
229         write_u8(buf + 22, g->first_chunk? 0 : 1);
230         write_u8(buf + 23, vsst->header_len? 1 : 0);
231         memset(buf + 24, 0, 8);
232 }
233
234 static bool need_audio_header(struct fec_client *fc, struct vss_task *vsst)
235 {
236         if (!mmd->current_chunk) {
237                 tv_add(now, &vsst->header_interval, &fc->next_header_time);
238                 return false;
239         }
240         if (!vsst->header_buf)
241                 return false;
242         if (vsst->header_len == 0)
243                 return false;
244         if (fc->group.num > 0) {
245                 if (!fc->fcp->need_periodic_header)
246                         return false;
247                 if (tv_diff(&fc->next_header_time, now, NULL) > 0)
248                         return false;
249         }
250         tv_add(now, &vsst->header_interval, &fc->next_header_time);
251         return true;
252 }
253
254 static bool need_data_slices(struct fec_client *fc, struct vss_task *vsst)
255 {
256         if (fc->group.num > 0)
257                 return true;
258         if (!vsst->header_buf)
259                 return true;
260         if (vsst->header_len == 0)
261                 return true;
262         if (fc->fcp->need_periodic_header)
263                 return true;
264         return false;
265 }
266
267 static int num_slices(long unsigned bytes, int max_payload, int rs)
268 {
269         int ret;
270
271         assert(max_payload > 0);
272         assert(rs > 0);
273         ret = DIV_ROUND_UP(bytes, max_payload);
274         if (ret + rs > 255)
275                 return -E_BAD_CT;
276         return ret;
277 }
278
279 /* set group start and group duration */
280 static void set_group_timing(struct fec_client *fc, struct vss_task *vsst)
281 {
282         struct fec_group *g = &fc->group;
283         struct timeval *chunk_tv = vss_chunk_time();
284
285         if (!need_data_slices(fc, vsst))
286                 ms2tv(200, &g->duration);
287         else
288                 tv_scale(g->num_chunks, chunk_tv, &g->duration);
289         tv_divide(fc->fcp->slices_per_group + fc->num_extra_slices,
290                 &g->duration, &g->slice_duration);
291         PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
292                 tv2ms(&g->duration), tv2ms(chunk_tv), tv2ms(&g->slice_duration));
293 }
294
295 static int initialize_fec_client(struct fec_client *fc, struct vss_task *vsst)
296 {
297         int k, n, ret;
298         int hs, ds, rs; /* header/data/redundant slices */
299         struct fec_client_parms *fcp = fc->fcp;
300
301         /* set mps */
302         if (fcp->init_fec) {
303                 /*
304                  * Set the maximum slice size to the Maximum Packet Size if the
305                  * transport protocol allows to determine this value. The user
306                  * can specify a slice size up to this value.
307                  */
308                 ret = fcp->init_fec(fc->sc);
309                 if (ret < 0)
310                         return ret;
311                 fc->mps = ret;
312         } else
313                 fc->mps = generic_max_transport_msg_size(fc->sc->fd);
314         if (fc->mps <= FEC_HEADER_SIZE)
315                 return -ERRNO_TO_PARA_ERROR(EINVAL);
316
317         rs = fc->fcp->slices_per_group - fc->fcp->data_slices_per_group;
318         ret = num_slices(vsst->header_len, fc->mps - FEC_HEADER_SIZE, rs);
319         if (ret < 0)
320                 return ret;
321         hs = ret;
322         ret = num_slices(mmd->afd.max_chunk_size, fc->mps - FEC_HEADER_SIZE, rs);
323         if (ret < 0)
324                 return ret;
325         ds = ret;
326         if (fc->fcp->need_periodic_header)
327                 k = hs + ds;
328         else
329                 k = PARA_MAX(hs, ds);
330         if (k < fc->fcp->data_slices_per_group)
331                 k = fc->fcp->data_slices_per_group;
332         fc->num_extra_slices = k - fc->fcp->data_slices_per_group;
333         n = k + rs;
334         fec_free(fc->parms);
335         ret = fec_new(k, n, &fc->parms);
336         if (ret < 0)
337                 return ret;
338         PARA_INFO_LOG("mps: %d, k: %d, n: %d, extra slices: %d\n",
339                 fc->mps, k, n, fc->num_extra_slices);
340         fc->src_data = para_realloc(fc->src_data, k * sizeof(char *));
341         fc->enc_buf = para_realloc(fc->enc_buf, fc->mps);
342         fc->extra_src_buf = para_realloc(fc->extra_src_buf, fc->mps);
343         fc->extra_header_buf = para_realloc(fc->extra_header_buf, fc->mps);
344
345         fc->state = FEC_STATE_READY_TO_RUN;
346         fc->next_header_time.tv_sec = 0;
347         fc->stream_start = *now;
348         fc->first_stream_chunk = mmd->current_chunk;
349         return 1;
350 }
351
352 static void vss_get_chunk(int chunk_num, struct vss_task *vsst,
353                 char **buf, size_t *sz)
354 {
355         int ret;
356
357         /*
358          * Chunk zero is special for header streams: It is the first portion of
359          * the audio file which consists of the audio file header. It may be
360          * arbitrary large due to embedded meta data. Audio format handlers may
361          * replace the header by a stripped one with meta data omitted which is
362          * of bounded size. We always use the stripped header for streaming
363          * rather than the unmodified header (chunk zero).
364          */
365         if (chunk_num == 0 && vsst->header_len > 0) {
366                 assert(vsst->header_buf);
367                 *buf = vsst->header_buf; /* stripped header */
368                 *sz = vsst->header_len;
369                 return;
370         }
371         ret = afh_get_chunk(chunk_num, &mmd->afd.afhi,
372                 mmd->afd.audio_format_id, vsst->map, vsst->mapsize,
373                 (const char **)buf, sz, &vsst->afh_context);
374         if (ret < 0) {
375                 PARA_WARNING_LOG("could not get chunk %d: %s\n",
376                         chunk_num, para_strerror(-ret));
377                 *buf = NULL;
378                 *sz = 0;
379         }
380 }
381
382 static void compute_group_size(struct vss_task *vsst, struct fec_group *g,
383                 int max_bytes)
384 {
385         char *buf;
386         size_t len;
387         int i, max_chunks = PARA_MAX(1LU, 150 / tv2ms(vss_chunk_time()));
388
389         if (g->first_chunk == 0) {
390                 g->num_chunks = 1;
391                 vss_get_chunk(0, vsst, &buf, &len);
392                 g->bytes = len;
393                 return;
394         }
395
396         g->num_chunks = 0;
397         g->bytes = 0;
398         /*
399          * Include chunks into the group until the group duration is at least
400          * 150ms.  For ogg and wma, a single chunk's duration (ogg page/wma
401          * super frame) is already larger than 150ms, so a FEC group consists
402          * of exactly one chunk for these audio formats.
403          */
404         for (i = 0;; i++) {
405                 int chunk_num = g->first_chunk + i;
406
407                 if (g->bytes > 0 && i >= max_chunks) /* duration limit */
408                         break;
409                 if (chunk_num >= mmd->afd.afhi.chunks_total) /* eof */
410                         break;
411                 vss_get_chunk(chunk_num, vsst, &buf, &len);
412                 if (g->bytes + len > max_bytes)
413                         break;
414                 /* Include this chunk */
415                 g->bytes += len;
416                 g->num_chunks++;
417         }
418         assert(g->num_chunks);
419 }
420
421 /*
422  * Compute the slice size of the next group.
423  *
424  * The FEC parameters n and k are fixed but the slice size varies per
425  * FEC group.  We'd like to choose slices as small as possible to avoid
426  * unnecessary FEC calculations but large enough to guarantee that the
427  * k data slices suffice to encode the header (if needed) and the data
428  * chunk(s).
429  *
430  * Once we know the payload of the next group, we define the number s
431  * of bytes per slice for this group by
432  *
433  *      s = ceil(payload / k)
434  *
435  * However, for header streams, computing s is more complicated since no
436  * overlapping of header and data slices is possible. Hence we have k >=
437  * 2 and s must satisfy
438  *
439  * (*)  ceil(h / s) + ceil(d / s) <= k
440  *
441  * where h and d are payload of the header and the data chunk(s)
442  * respectively. In general there is no value for s such that (*)
443  * becomes an equality, for example if h = 4000, d = 5000 and k = 10.
444  *
445  * We use the following approach for computing a suitable value for s:
446  *
447  * Let
448  *      k1 := ceil(k * min(h, d) / (h + d)),
449  *      k2 := k - k1.
450  *
451  * Note that k >= 2 implies k1 > 0 and k2 > 0, so
452  *
453  *      s := max(ceil(min(h, d) / k1), ceil(max(h, d) / k2))
454  *
455  * is well-defined. Inequality (*) holds for this value of s since k1
456  * slices suffice to store min(h, d) while k2 slices suffice to store
457  * max(h, d), i.e. the first addent of (*) is bounded by k1 and the
458  * second by k2.
459  *
460  * For the above example we obtain
461  *
462  *      k1 = ceil(10 * 4000 / 9000) = 5, k2 = 5,
463  *      s = max(4000 / 5, 5000 / 5) = 1000,
464  *
465  * which is optimal since a slice size of 999 bytes would already require
466  * 11 slices.
467  */
468 static int compute_slice_size(struct fec_client *fc, struct vss_task *vsst)
469 {
470         struct fec_group *g = &fc->group;
471         int k = fc->fcp->data_slices_per_group + fc->num_extra_slices;
472         int n = fc->fcp->slices_per_group + fc->num_extra_slices;
473         int ret, k1, k2, h, d, min, max, sum;
474         int max_slice_bytes = fc->mps - FEC_HEADER_SIZE;
475         int max_group_bytes;
476
477         if (!need_audio_header(fc, vsst)) {
478                 max_group_bytes = k * max_slice_bytes;
479                 g->num_header_slices = 0;
480                 compute_group_size(vsst, g, max_group_bytes);
481                 g->slice_bytes = DIV_ROUND_UP(g->bytes, k);
482                 if (g->slice_bytes == 0)
483                         g->slice_bytes = 1;
484                 return 1;
485         }
486         if (!need_data_slices(fc, vsst)) {
487                 g->bytes = 0;
488                 g->num_chunks = 0;
489                 g->slice_bytes = DIV_ROUND_UP(vsst->header_len, k);
490                 g->num_header_slices = k;
491                 return 1;
492         }
493         h = vsst->header_len;
494         max_group_bytes = (k - num_slices(h, max_slice_bytes, n - k))
495                 * max_slice_bytes;
496         compute_group_size(vsst, g, max_group_bytes);
497         d = g->bytes;
498         if (d == 0) {
499                 g->slice_bytes = DIV_ROUND_UP(h, k);
500                 ret = num_slices(vsst->header_len, g->slice_bytes, n - k);
501                 if (ret < 0)
502                         return ret;
503                 g->num_header_slices = ret;
504                 return 1;
505         }
506         min = PARA_MIN(h, d);
507         max = PARA_MAX(h, d);
508         sum = h + d;
509         k1 = DIV_ROUND_UP(k * min, sum);
510         k2 = k - k1;
511         assert(k1 > 0);
512         assert(k2 > 0);
513
514         g->slice_bytes = PARA_MAX(DIV_ROUND_UP(min, k1), DIV_ROUND_UP(max, k2));
515         /*
516          * This value of s := g->slice_bytes satisfies inequality (*) above,
517          * but it might be larger than max_slice_bytes. However, we know that
518          * max_slice_bytes are sufficient to store header and data, so:
519          */
520         g->slice_bytes = PARA_MIN((int)g->slice_bytes, max_slice_bytes);
521
522         ret = num_slices(vsst->header_len, g->slice_bytes, n - k);
523         if (ret < 0)
524                 return ret;
525         g->num_header_slices = ret;
526         return 1;
527 }
528
529 static int setup_next_fec_group(struct fec_client *fc, struct vss_task *vsst)
530 {
531         int ret, i, k, n, data_slices;
532         size_t len;
533         char *buf, *p;
534         struct fec_group *g = &fc->group;
535
536         if (fc->state == FEC_STATE_NONE) {
537                 ret = initialize_fec_client(fc, vsst);
538                 if (ret < 0)
539                         return ret;
540                 g->first_chunk = mmd->current_chunk;
541                 g->num = 0;
542                 g->start = *now;
543         } else {
544                 struct timeval tmp;
545                 if (g->first_chunk + g->num_chunks >= mmd->afd.afhi.chunks_total)
546                         return 0;
547                 /*
548                  * Start and duration of this group depend only on the previous
549                  * group. Compute the new group start as g->start += g->duration.
550                  */
551                 tmp = g->start;
552                 tv_add(&tmp, &g->duration, &g->start);
553                 set_group_timing(fc, vsst);
554                 g->first_chunk += g->num_chunks;
555                 g->num++;
556         }
557         k = fc->fcp->data_slices_per_group + fc->num_extra_slices;
558         n = fc->fcp->slices_per_group + fc->num_extra_slices;
559
560         compute_slice_size(fc, vsst);
561         assert(g->slice_bytes > 0);
562         ret = num_slices(g->bytes, g->slice_bytes, n - k);
563         if (ret < 0)
564                 return ret;
565         data_slices = ret;
566         assert(g->num_header_slices + data_slices <= k);
567         fc->current_slice_num = 0;
568         if (g->num == 0)
569                 set_group_timing(fc, vsst);
570         /* setup header slices */
571         buf = vsst->header_buf;
572         for (i = 0; i < g->num_header_slices; i++) {
573                 uint32_t payload_size;
574                 if (buf + g->slice_bytes <= vsst->header_buf + vsst->header_len) {
575                         fc->src_data[i] = (const unsigned char *)buf;
576                         buf += g->slice_bytes;
577                         continue;
578                 }
579                 /*
580                  * Can not use vss->header_buf for this slice as it
581                  * goes beyond the buffer. This slice will not be fully
582                  * used.
583                  */
584                 payload_size = vsst->header_buf + vsst->header_len - buf;
585                 memcpy(fc->extra_header_buf, buf, payload_size);
586                 if (payload_size < g->slice_bytes)
587                         memset(fc->extra_header_buf + payload_size, 0,
588                                 g->slice_bytes - payload_size);
589                 /*
590                  * There might be more than one header slice to fill although
591                  * only the first one will be used. Set all header slices to
592                  * our extra buffer.
593                  */
594                 while (i < g->num_header_slices)
595                         fc->src_data[i++] = fc->extra_header_buf;
596                 break; /* we don't want i to be increased. */
597         }
598
599         /*
600          * Setup data slices. Note that for ogg streams chunk 0 points to a
601          * buffer on the heap rather than to the mapped audio file.
602          */
603         vss_get_chunk(g->first_chunk, vsst, &buf, &len);
604         for (p = buf; i < g->num_header_slices + data_slices; i++) {
605                 if (p + g->slice_bytes > buf + g->bytes) {
606                         /*
607                          * We must make a copy for this slice since using p
608                          * directly would exceed the buffer.
609                          */
610                         uint32_t payload_size = buf + g->bytes - p;
611                         assert(payload_size + FEC_HEADER_SIZE <= fc->mps);
612                         memcpy(fc->extra_src_buf, p, payload_size);
613                         if (payload_size < g->slice_bytes)
614                                 memset(fc->extra_src_buf + payload_size, 0,
615                                         g->slice_bytes - payload_size);
616                         fc->src_data[i] = fc->extra_src_buf;
617                         i++;
618                         break;
619                 }
620                 fc->src_data[i] = (const unsigned char *)p;
621                 p += g->slice_bytes;
622         }
623         if (i < k) {
624                 /* use arbitrary data for all remaining slices */
625                 buf = vsst->map;
626                 for (; i < k; i++)
627                         fc->src_data[i] = (const unsigned char *)buf;
628         }
629         PARA_DEBUG_LOG("FEC group %u: %u chunks (%u - %u), %u bytes\n",
630                 g->num, g->num_chunks, g->first_chunk,
631                 g->first_chunk + g->num_chunks - 1, g->bytes
632         );
633         PARA_DEBUG_LOG("slice_bytes: %d, %d header slices, %d data slices\n",
634                 g->slice_bytes, g->num_header_slices, data_slices
635         );
636         return 1;
637 }
638
639 static int compute_next_fec_slice(struct fec_client *fc, struct vss_task *vsst)
640 {
641         if (fc->state == FEC_STATE_NONE || fc->current_slice_num
642                         == fc->fcp->slices_per_group + fc->num_extra_slices) {
643                 int ret = setup_next_fec_group(fc, vsst);
644                 if (ret == 0)
645                         return 0;
646                 if (ret < 0) {
647                         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
648                         PARA_ERROR_LOG("FEC client temporarily disabled\n");
649                         fc->state = FEC_STATE_DISABLED;
650                         return ret;
651                 }
652         }
653         write_fec_header(fc, vsst);
654         fec_encode(fc->parms, fc->src_data, fc->enc_buf + FEC_HEADER_SIZE,
655                 fc->current_slice_num, fc->group.slice_bytes);
656         return 1;
657 }
658
659 /**
660  * Return a buffer that marks the end of the stream.
661  *
662  * \param buf Result pointer.
663  * \return The length of the eof buffer.
664  *
665  * This is used for (multicast) udp streaming where closing the socket on the
666  * sender might not give rise to an eof condition at the peer.
667  */
668 size_t vss_get_fec_eof_packet(const char **buf)
669 {
670         static const char fec_eof_packet[FEC_HEADER_SIZE] = FEC_EOF_PACKET;
671         *buf = fec_eof_packet;
672         return FEC_HEADER_SIZE;
673 }
674
675 /**
676  * Add one entry to the list of active fec clients.
677  *
678  * \param sc  Generic sender_client data of the transport layer.
679  * \param fcp FEC parameters as supplied by the transport layer.
680  *
681  * \return Newly allocated fec_client struct.
682  */
683 struct fec_client *vss_add_fec_client(struct sender_client *sc,
684                                       struct fec_client_parms *fcp)
685 {
686         struct fec_client *fc = para_calloc(sizeof(*fc));
687
688         fc->sc  = sc;
689         fc->fcp = fcp;
690         para_list_add(&fc->node, &fec_client_list);
691         return fc;
692 }
693
694 /**
695  * Remove one entry from the list of active fec clients.
696  *
697  * \param fc The client to be removed.
698  */
699 void vss_del_fec_client(struct fec_client *fc)
700 {
701         list_del(&fc->node);
702         free(fc->src_data);
703         free(fc->enc_buf);
704         free(fc->extra_src_buf);
705         free(fc->extra_header_buf);
706         fec_free(fc->parms);
707         free(fc);
708 }
709
710 /*
711  * Compute if/when next slice is due. If it isn't due yet and \a diff is
712  * not \p Null, compute the time difference next - now, where
713  *
714  *      next = stream_start + (first_group_chunk - first_stream_chunk)
715  *              * chunk_time + slice_num * slice_time
716  */
717 static int next_slice_is_due(struct fec_client *fc, struct timeval *diff)
718 {
719         struct timeval tmp, next;
720         int ret;
721
722         if (fc->state == FEC_STATE_NONE)
723                 return 1;
724         tv_scale(fc->current_slice_num, &fc->group.slice_duration, &tmp);
725         tv_add(&tmp, &fc->group.start, &next);
726         ret = tv_diff(&next, now, diff);
727         return ret < 0? 1 : 0;
728 }
729
730 static void set_eof_barrier(struct vss_task *vsst)
731 {
732         struct fec_client *fc;
733         struct timeval timeout = {1, 0}, *chunk_tv = vss_chunk_time();
734
735         if (!chunk_tv)
736                 goto out;
737         list_for_each_entry(fc, &fec_client_list, node) {
738                 struct timeval group_duration;
739
740                 if (fc->state != FEC_STATE_READY_TO_RUN)
741                         continue;
742                 tv_scale(fc->group.num_chunks, chunk_tv, &group_duration);
743                 if (tv_diff(&timeout, &group_duration, NULL) < 0)
744                         timeout = group_duration;
745         }
746 out:
747         tv_add(now, &timeout, &vsst->eof_barrier);
748 }
749
750 /**
751  * Check if vss status flag \a P (playing) is set.
752  *
753  * \return Greater than zero if playing, zero otherwise.
754  *
755  */
756 unsigned int vss_playing(void)
757 {
758         return mmd->new_vss_status_flags & VSS_PLAYING;
759 }
760
761 /**
762  * Check if the \a N (next) status flag is set.
763  *
764  * \return Greater than zero if set, zero if not.
765  *
766  */
767 unsigned int vss_next(void)
768 {
769         return mmd->new_vss_status_flags & VSS_NEXT;
770 }
771
772 /**
773  * Check if a reposition request is pending.
774  *
775  * \return Greater than zero if true, zero otherwise.
776  *
777  */
778 unsigned int vss_repos(void)
779 {
780         return mmd->new_vss_status_flags & VSS_REPOS;
781 }
782
783 /**
784  * Check if the vss is currently paused.
785  *
786  * \return Greater than zero if paused, zero otherwise.
787  *
788  */
789 unsigned int vss_paused(void)
790 {
791         return !(mmd->new_vss_status_flags & VSS_NEXT)
792                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
793 }
794
795 /**
796  * Check if the vss is currently stopped.
797  *
798  * \return Greater than zero if paused, zero otherwise.
799  *
800  */
801 unsigned int vss_stopped(void)
802 {
803         return (mmd->new_vss_status_flags & VSS_NEXT)
804                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
805 }
806
807 static int chk_barrier(const char *bname, const struct timeval *barrier,
808                 struct timeval *diff, int print_log)
809 {
810         long ms;
811
812         if (tv_diff(now, barrier, diff) > 0)
813                 return 1;
814         ms = tv2ms(diff);
815         if (print_log && ms)
816                 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
817         return -1;
818 }
819
820 static void vss_compute_timeout(struct sched *s, struct vss_task *vsst)
821 {
822         struct timeval tv;
823         struct fec_client *fc;
824
825         if (!vss_playing() || !vsst->map)
826                 return;
827         if (vss_next() && vsst->map) /* only sleep a bit, nec*/
828                 return sched_request_timeout_ms(100, s);
829
830         /* Each of these barriers must have passed until we may proceed */
831         if (sched_request_barrier(&vsst->autoplay_barrier, s) == 1)
832                 return;
833         if (sched_request_barrier(&vsst->eof_barrier, s) == 1)
834                 return;
835         if (sched_request_barrier(&vsst->data_send_barrier, s) == 1)
836                 return;
837         /*
838          * Compute the select timeout as the minimal time until the next
839          * chunk/slice is due for any client.
840          */
841         compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
842                 &mmd->stream_start, &tv);
843         if (sched_request_barrier_or_min_delay(&tv, s) == 0)
844                 return;
845         list_for_each_entry(fc, &fec_client_list, node) {
846                 if (fc->state != FEC_STATE_READY_TO_RUN)
847                         continue;
848                 if (next_slice_is_due(fc, &tv))
849                         return sched_min_delay(s);
850                 sched_request_timeout(&tv, s);
851         }
852 }
853
854 static void vss_eof(struct vss_task *vsst)
855 {
856
857         if (!vsst->map)
858                 return;
859         if (mmd->new_vss_status_flags & VSS_NOMORE)
860                 mmd->new_vss_status_flags = VSS_NEXT;
861         set_eof_barrier(vsst);
862         afh_free_header(vsst->header_buf, mmd->afd.audio_format_id);
863         vsst->header_buf = NULL;
864         para_munmap(vsst->map, vsst->mapsize);
865         vsst->map = NULL;
866         mmd->chunks_sent = 0;
867         //mmd->offset = 0;
868         mmd->afd.afhi.seconds_total = 0;
869         mmd->afd.afhi.chunk_tv.tv_sec = 0;
870         mmd->afd.afhi.chunk_tv.tv_usec = 0;
871         free(mmd->afd.afhi.chunk_table);
872         mmd->afd.afhi.chunk_table = NULL;
873         vsst->mapsize = 0;
874         afh_close(vsst->afh_context, mmd->afd.audio_format_id);
875         vsst->afh_context = NULL;
876         mmd->events++;
877 }
878
879 static int need_to_request_new_audio_file(struct vss_task *vsst)
880 {
881         struct timeval diff;
882
883         if (vsst->map) /* have audio file */
884                 return 0;
885         if (!vss_playing()) /* don't need one */
886                 return 0;
887         if (mmd->new_vss_status_flags & VSS_NOMORE)
888                 return 0;
889         if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
890                 return 0;
891         if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
892                         &diff, 1) < 0)
893                 return 0;
894         return 1;
895 }
896
897 static void set_mmd_offset(void)
898 {
899         struct timeval offset;
900         tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &offset);
901         mmd->offset = tv2ms(&offset);
902 }
903
904 static void vss_pre_select(struct sched *s, void *context)
905 {
906         int i;
907         struct vss_task *vsst = context;
908
909         if (need_to_request_new_audio_file(vsst)) {
910                 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
911                 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
912                 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
913         } else
914                 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
915         for (i = 0; senders[i].name; i++) {
916                 if (!senders[i].pre_select)
917                         continue;
918                 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
919         }
920         vss_compute_timeout(s, vsst);
921 }
922
923 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
924 {
925         char control[255] __a_aligned(8), buf[8];
926         struct msghdr msg = {.msg_iov = NULL};
927         struct cmsghdr *cmsg;
928         struct iovec iov;
929         int ret = 0;
930
931         *fd = -1;
932         iov.iov_base = buf;
933         iov.iov_len = sizeof(buf);
934         msg.msg_iov = &iov;
935         msg.msg_iovlen = 1;
936         msg.msg_control = control;
937         msg.msg_controllen = sizeof(control);
938         memset(buf, 0, sizeof(buf));
939         ret = recvmsg(afs_socket, &msg, 0);
940         if (ret < 0)
941                 return -ERRNO_TO_PARA_ERROR(errno);
942         if (iov.iov_len != sizeof(buf))
943                 return -E_AFS_SHORT_READ;
944         *code = *(uint32_t*)buf;
945         *data =  *(uint32_t*)(buf + 4);
946         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
947                 if (cmsg->cmsg_level != SOL_SOCKET
948                         || cmsg->cmsg_type != SCM_RIGHTS)
949                         continue;
950                 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
951                         continue;
952                 *fd = *(int *)CMSG_DATA(cmsg);
953         }
954         return 1;
955 }
956
957 #ifndef MAP_POPULATE
958 #define MAP_POPULATE 0
959 #endif
960
961 static void recv_afs_result(struct vss_task *vsst, fd_set *rfds)
962 {
963         int ret, passed_fd, shmid;
964         uint32_t afs_code = 0, afs_data = 0;
965         struct stat statbuf;
966
967         if (!FD_ISSET(vsst->afs_socket, rfds))
968                 return;
969         ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
970         if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
971                 return;
972         if (ret < 0)
973                 goto err;
974         vsst->afsss = AFS_SOCKET_READY;
975         PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
976                 afs_data);
977         ret = -E_NOFD;
978         if (afs_code != NEXT_AUDIO_FILE)
979                 goto err;
980         if (passed_fd < 0)
981                 goto err;
982         shmid = afs_data;
983         ret = load_afd(shmid, &mmd->afd);
984         if (ret < 0)
985                 goto err;
986         shm_destroy(shmid);
987         ret = fstat(passed_fd, &statbuf);
988         if (ret < 0) {
989                 PARA_ERROR_LOG("fstat error:\n");
990                 ret = -ERRNO_TO_PARA_ERROR(errno);
991                 goto err;
992         }
993         ret = para_mmap(statbuf.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE,
994                 passed_fd, 0, &vsst->map);
995         if (ret < 0)
996                 goto err;
997         vsst->mapsize = statbuf.st_size;
998         close(passed_fd);
999         mmd->chunks_sent = 0;
1000         mmd->current_chunk = 0;
1001         mmd->offset = 0;
1002         mmd->events++;
1003         mmd->num_played++;
1004         mmd->new_vss_status_flags &= (~VSS_NEXT);
1005         afh_get_header(&mmd->afd.afhi, mmd->afd.audio_format_id,
1006                 vsst->map, vsst->mapsize, &vsst->header_buf, &vsst->header_len);
1007         return;
1008 err:
1009         free(mmd->afd.afhi.chunk_table);
1010         if (passed_fd >= 0)
1011                 close(passed_fd);
1012         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1013         mmd->new_vss_status_flags = VSS_NEXT;
1014 }
1015
1016 /**
1017  * Main sending function.
1018  *
1019  * This function gets called from vss_post_select(). It checks whether the next
1020  * chunk of data should be pushed out. It obtains a pointer to the data to be
1021  * sent out as well as its length from mmd->afd.afhi. This information is then
1022  * passed to each supported sender's send() function as well as to the send()
1023  * functions of each registered fec client.
1024  */
1025 static void vss_send(struct vss_task *vsst)
1026 {
1027         int i;
1028         bool fec_active = false;
1029         struct timeval due;
1030         struct fec_client *fc, *tmp_fc;
1031
1032         if (!vsst->map || !vss_playing())
1033                 return;
1034         if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
1035                 return;
1036         if (chk_barrier("data send", &vsst->data_send_barrier, &due, 1) < 0)
1037                 return;
1038         list_for_each_entry_safe(fc, tmp_fc, &fec_client_list, node) {
1039                 if (fc->state == FEC_STATE_DISABLED)
1040                         continue;
1041                 if (!next_slice_is_due(fc, NULL)) {
1042                         fec_active = true;
1043                         continue;
1044                 }
1045                 if (compute_next_fec_slice(fc, vsst) <= 0)
1046                         continue;
1047                 PARA_DEBUG_LOG("sending %u:%u (%u bytes)\n", fc->group.num,
1048                         fc->current_slice_num, fc->group.slice_bytes);
1049                 fc->current_slice_num++;
1050                 fc->fcp->send_fec(fc->sc, (char *)fc->enc_buf,
1051                         fc->group.slice_bytes + FEC_HEADER_SIZE);
1052                 fec_active = true;
1053         }
1054         if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
1055                 if (!fec_active)
1056                         mmd->new_vss_status_flags |= VSS_NEXT;
1057                 return;
1058         }
1059         compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
1060                 &mmd->stream_start, &due);
1061         if (tv_diff(&due, now, NULL) <= 0) {
1062                 char *buf;
1063                 size_t len;
1064
1065                 if (!mmd->chunks_sent) {
1066                         mmd->stream_start = *now;
1067                         mmd->events++;
1068                         set_mmd_offset();
1069                 }
1070                 vss_get_chunk(mmd->current_chunk, vsst, &buf, &len);
1071                 for (i = 0; senders[i].name; i++) {
1072                         /*
1073                          * We call ->send() even if len is zero because senders
1074                          * might have data queued which can be sent now.
1075                          */
1076                         if (!senders[i].send)
1077                                 continue;
1078                         senders[i].send(mmd->current_chunk, mmd->chunks_sent,
1079                                 buf, len, vsst->header_buf, vsst->header_len);
1080                 }
1081                 /*
1082                  * Prefault next chunk(s)
1083                  *
1084                  * If the backing device of the memory-mapped audio file is
1085                  * slow and read-ahead is turned off or prevented for some
1086                  * reason, e.g. due to memory pressure, it may take much longer
1087                  * than the chunk interval to get the next chunk on the wire,
1088                  * causing buffer underruns on the client side. Mapping the
1089                  * file with MAP_POPULATE seems to help a bit, but it does not
1090                  * eliminate the delays completely. Moreover, it is supported
1091                  * only on Linux. So we do our own read-ahead here.
1092                  */
1093                 if (mmd->current_chunk > 0) { /* chunk 0 might be on the heap */
1094                         buf += len;
1095                         for (i = 0; i < 5 && buf < vsst->map + vsst->mapsize; i++) {
1096                                 __a_unused volatile char x = *buf;
1097                                 buf += 4096;
1098                         }
1099                 }
1100                 mmd->chunks_sent++;
1101                 mmd->current_chunk++;
1102         }
1103 }
1104
1105 static int vss_post_select(struct sched *s, void *context)
1106 {
1107         int ret, i;
1108         struct vss_task *vsst = context;
1109
1110         if (!vsst->map || vss_next() || vss_paused() || vss_repos()) {
1111                 /* shut down senders and fec clients */
1112                 struct fec_client *fc, *tmp;
1113                 for (i = 0; senders[i].name; i++)
1114                         if (senders[i].shutdown_clients)
1115                                 senders[i].shutdown_clients();
1116                 list_for_each_entry_safe(fc, tmp, &fec_client_list, node)
1117                         fc->state = FEC_STATE_NONE;
1118                 mmd->stream_start.tv_sec = 0;
1119                 mmd->stream_start.tv_usec = 0;
1120         }
1121         if (vss_next())
1122                 vss_eof(vsst);
1123         else if (vss_paused()) {
1124                 if (mmd->chunks_sent)
1125                         set_eof_barrier(vsst);
1126                 mmd->chunks_sent = 0;
1127         } else if (vss_repos()) { /* repositioning due to ff/jmp command */
1128                 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
1129                 set_eof_barrier(vsst);
1130                 mmd->chunks_sent = 0;
1131                 mmd->current_chunk = afh_get_start_chunk(mmd->repos_request,
1132                         &mmd->afd.afhi, mmd->afd.audio_format_id);
1133                 mmd->new_vss_status_flags &= ~VSS_REPOS;
1134                 set_mmd_offset();
1135         }
1136         /* If a sender command is pending, run it. */
1137         if (mmd->sender_cmd_data.cmd_num >= 0) {
1138                 int num = mmd->sender_cmd_data.cmd_num,
1139                         sender_num = mmd->sender_cmd_data.sender_num;
1140
1141                 if (senders[sender_num].client_cmds[num]) {
1142                         ret = senders[sender_num].client_cmds[num]
1143                                 (&mmd->sender_cmd_data);
1144                         if (ret < 0)
1145                                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1146                 }
1147                 mmd->sender_cmd_data.cmd_num = -1;
1148         }
1149         if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE)
1150                 recv_afs_result(vsst, &s->rfds);
1151         else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
1152                 PARA_NOTICE_LOG("requesting new fd from afs\n");
1153                 ret = write_buffer(vsst->afs_socket, "new");
1154                 if (ret < 0)
1155                         PARA_CRIT_LOG("%s\n", para_strerror(-ret));
1156                 else
1157                         vsst->afsss = AFS_SOCKET_AFD_PENDING;
1158         }
1159         for (i = 0; senders[i].name; i++) {
1160                 if (!senders[i].post_select)
1161                         continue;
1162                 senders[i].post_select(&s->rfds, &s->wfds);
1163         }
1164         if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
1165                         (vss_next() && vss_playing()))
1166                 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
1167         vss_send(vsst);
1168         return 0;
1169 }
1170
1171 /**
1172  * Initialize the virtual streaming system task.
1173  *
1174  * \param afs_socket The fd for communication with afs.
1175  * \param s The scheduler to register the vss task to.
1176  *
1177  * This also initializes all supported senders and starts streaming
1178  * if the --autoplay command line flag was given.
1179  */
1180 void init_vss_task(int afs_socket, struct sched *s)
1181 {
1182         static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
1183         int i;
1184         char *hn = para_hostname(), *home = para_homedir();
1185         long unsigned announce_time = OPT_UINT32_VAL(ANNOUNCE_TIME),
1186                 autoplay_delay = OPT_UINT32_VAL(AUTOPLAY_DELAY);
1187         vsst->header_interval.tv_sec = 5; /* should this be configurable? */
1188         vsst->afs_socket = afs_socket;
1189         ms2tv(announce_time, &vsst->announce_tv);
1190         PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
1191         INIT_LIST_HEAD(&fec_client_list);
1192         for (i = 0; senders[i].name; i++) {
1193                 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
1194                 senders[i].init(&senders[i]);
1195         }
1196         free(hn);
1197         free(home);
1198         mmd->sender_cmd_data.cmd_num = -1;
1199         if (OPT_GIVEN(AUTOPLAY)) {
1200                 struct timeval tmp;
1201                 mmd->vss_status_flags |= VSS_PLAYING;
1202                 mmd->new_vss_status_flags |= VSS_PLAYING;
1203                 ms2tv(autoplay_delay, &tmp);
1204                 tv_add(clock_get_realtime(NULL), &tmp, &vsst->autoplay_barrier);
1205                 tv_add(&vsst->autoplay_barrier, &vsst->announce_tv,
1206                         &vsst->data_send_barrier);
1207         }
1208         vsst->task = task_register(&(struct task_info) {
1209                 .name = "vss task",
1210                 .pre_select = vss_pre_select,
1211                 .post_select = vss_post_select,
1212                 .context = vsst,
1213         }, s);
1214 }