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