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