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