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