]> git.tuebingen.mpg.de Git - paraslash.git/blob - vss.c
330bba6ab65c397b19bc1a25d9310bd15bcec70e
[paraslash.git] / vss.c
1 /*
2  * Copyright (C) 1997-2009 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 <dirent.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "portable_io.h"
19 #include "fec.h"
20 #include "string.h"
21 #include "afh.h"
22 #include "afs.h"
23 #include "server.h"
24 #include "net.h"
25 #include "server.cmdline.h"
26 #include "list.h"
27 #include "vss.h"
28 #include "send.h"
29 #include "ipc.h"
30 #include "fd.h"
31 #include "sched.h"
32
33 extern struct misc_meta_data *mmd;
34
35 extern void dccp_send_init(struct sender *);
36 extern void http_send_init(struct sender *);
37 extern void udp_send_init(struct sender *);
38
39 /** The list of supported senders. */
40 struct sender senders[] = {
41         {
42                 .name = "http",
43                 .init = http_send_init,
44         },
45         {
46                 .name = "dccp",
47                 .init = dccp_send_init,
48         },
49         {
50                 .name = "udp",
51                 .init = udp_send_init,
52         },
53         {
54                 .name = NULL,
55         }
56 };
57
58 /** The possible states of the afs socket. */
59 enum afs_socket_status {
60         /** Socket is inactive. */
61         AFS_SOCKET_READY,
62         /** Socket fd was included in the write fd set for select(). */
63         AFS_SOCKET_CHECK_FOR_WRITE,
64         /** vss wrote a request to the socket and waits for reply from afs. */
65         AFS_SOCKET_AFD_PENDING
66 };
67
68 /** The task structure for the virtual streaming system. */
69 struct vss_task {
70         /** Copied from the -announce_time command line option. */
71         struct timeval announce_tv;
72         /** End of the announcing interval. */
73         struct timeval data_send_barrier;
74         /** End of the EOF interval. */
75         struct timeval eof_barrier;
76         /** Only used if --autoplay_delay was given. */
77         struct timeval autoplay_barrier;
78         /** Used for afs-server communication. */
79         int afs_socket;
80         /** The current state of \a afs_socket. */
81         enum afs_socket_status afsss;
82         /** The memory mapped audio file. */
83         char *map;
84         /** Used by the scheduler. */
85         struct task task;
86         /** Pointer to the header of the mapped audio file. */
87         const char *header_buf;
88         /** Length of the audio file header. */
89         size_t header_len;
90 };
91
92 static struct list_head fec_client_list;
93
94 struct fec_slice {
95         uint8_t num;
96         uint16_t bytes;
97 };
98
99 struct fec_group {
100         uint32_t num;
101         uint32_t bytes;
102         uint32_t first_chunk;
103         uint32_t num_chunks;
104         struct timeval duration;
105         struct timeval start;
106         struct timeval slice_duration;
107 };
108
109 struct fec_client {
110         struct fec_client_parms *fcp;
111         struct fec_parms *parms;
112         struct list_head node;
113         struct timeval stream_start;
114         int first_stream_chunk;
115         struct fec_group group;
116         struct fec_slice slice;
117         const unsigned char **src_data;
118         unsigned char *extra_src_buf;
119         size_t extra_src_buf_size;
120         unsigned char *enc_buf;
121         size_t enc_buf_size;
122 };
123
124 /**
125  * Get the chunk time of the current audio file.
126  *
127  * \return A pointer to a struct containing the chunk time, or NULL,
128  * if currently no audio file is selected.
129  */
130 struct timeval *vss_chunk_time(void)
131 {
132         if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
133                         mmd->afd.afhi.chunk_tv.tv_usec == 0)
134                 return NULL;
135         return &mmd->afd.afhi.chunk_tv;
136 }
137
138 static void setup_fec_group(struct fec_client *fc, struct vss_task *vsst)
139 {
140         uint32_t num_bytes = 0, chunk_num, max_group_size, last_payload_size;
141         int i, k = fc->fcp->data_slices_per_group;
142         const char *start_buf = NULL;
143         struct timeval tmp, *chunk_tv = vss_chunk_time();
144
145         assert(chunk_tv);
146         max_group_size = (fc->fcp->max_slice_bytes - FEC_HEADER_SIZE) * k;
147         chunk_num = fc->group.first_chunk;
148         for (;;) {
149                 const char *buf;
150                 size_t len;
151
152                 if (chunk_num >= mmd->afd.afhi.chunks_total)
153                         break;
154                 afh_get_chunk(chunk_num, &mmd->afd.afhi, vsst->map, &buf, &len);
155                 if (!start_buf)
156                         start_buf = buf;
157                 if (num_bytes + len > max_group_size)
158                         break;
159                 chunk_num++;
160                 num_bytes += len;
161         }
162         assert(start_buf);
163         fc->group.num_chunks = chunk_num - fc->group.first_chunk;
164         fc->group.num++;
165         fc->group.bytes = num_bytes;
166         fc->slice.num = 0;
167         fc->slice.bytes = ROUND_UP(num_bytes, k) / k;
168
169         /* The last slice will not be fully used */
170         last_payload_size = num_bytes % fc->slice.bytes;
171         if (!last_payload_size)
172                 last_payload_size = fc->slice.bytes;
173
174         tv_scale(fc->group.num_chunks, chunk_tv, &fc->group.duration);
175         tv_scale(fc->group.first_chunk - fc->first_stream_chunk, chunk_tv,
176                 &tmp);
177         tv_add(&fc->stream_start, &tmp, &fc->group.start);
178         tv_divide(fc->fcp->slices_per_group, &fc->group.duration,
179                 &fc->group.slice_duration);
180
181         for (i = 0; i < k; i++)
182                 fc->src_data[i] = (const unsigned char *)start_buf
183                         + i * fc->slice.bytes;
184
185         if (start_buf + k * fc->slice.bytes > vsst->map + mmd->size) {
186                 /* can not use last slice as it goes beyond the map */
187                 if (fc->extra_src_buf_size < fc->slice.bytes)
188                         fc->extra_src_buf = para_realloc(fc->extra_src_buf, fc->slice.bytes);
189                 memcpy(fc->extra_src_buf, start_buf + (k - 1) * fc->slice.bytes,
190                         last_payload_size);
191                 memset(fc->extra_src_buf + last_payload_size, 0,
192                         fc->slice.bytes - last_payload_size);
193                 fc->src_data[k - 1] = fc->extra_src_buf;
194         }
195
196 }
197
198 /**
199  * Write a fec header to a buffer.
200  *
201  * \param buf The buffer to write to.
202  * \param h The fec header to write.
203  */
204 static void write_fec_header(struct fec_client *fc)
205 {
206         char *buf = (char *)fc->enc_buf;
207
208         write_u32(buf, FEC_MAGIC);
209
210         write_u8(buf + 4, fc->fcp->slices_per_group);
211         write_u8(buf + 5, fc->fcp->data_slices_per_group);
212         write_u32(buf + 6, (uint32_t)0); /* audio header len */
213
214         write_u32(buf + 10, fc->group.num);
215         write_u32(buf + 14, fc->group.bytes);
216
217         write_u8(buf + 18, fc->slice.num);
218         write_u16(buf + 20, fc->slice.bytes);
219         memset(buf + 22, 0, 11);
220 }
221
222 /**
223  * Return a buffer that marks the end of the stream.
224  *
225  * \return The length of the eof buffer.
226  *
227  * This is used for (multicast) udp streaming where closing the socket on the
228  * sender might not give rise to an eof condition at the peer.
229  */
230 size_t vss_get_fec_eof_packet(const char **buf)
231 {
232         static const char fec_eof_packet[FEC_HEADER_SIZE] =
233         "\xec\x0d\xcc\xfe\0\0\0\0"
234         "\0\0\0\0\0\0\0\0"
235         "\0\0\0\0\0\0\0\0"
236         "\0\0\0\0\0\0\0\0";
237         *buf = fec_eof_packet;
238         return FEC_HEADER_SIZE;
239 }
240
241 static void compute_next_fec_slice(struct fec_client *fc, struct vss_task *vsst)
242 {
243         if (fc->first_stream_chunk < 0) {
244                 fc->stream_start = *now;
245                 fc->first_stream_chunk = mmd->current_chunk;
246                 fc->group.first_chunk = mmd->current_chunk;
247                 fc->group.num = 0;
248                 setup_fec_group(fc, vsst);
249         } else if (fc->slice.num == fc->fcp->slices_per_group) {
250                 fc->group.first_chunk += fc->group.num_chunks;
251                 setup_fec_group(fc, vsst);
252
253         }
254         if (fc->enc_buf_size < fc->slice.bytes + FEC_HEADER_SIZE) {
255                 fc->enc_buf_size = fc->slice.bytes + FEC_HEADER_SIZE;
256                 fc->enc_buf = para_realloc(fc->enc_buf, fc->enc_buf_size);
257         }
258         write_fec_header(fc);
259         fec_encode(fc->parms, fc->src_data, fc->enc_buf + FEC_HEADER_SIZE,
260                 fc->slice.num, fc->slice.bytes);
261 }
262
263 /**
264  * Add one entry to the list of active fec clients.
265  *
266  * \param fcp Describes the fec parameters to be used for this client.
267  * \param result An opaque pointer that must be used by remove the client later.
268  *
269  * \return Standard.
270  */
271 int vss_add_fec_client(struct fec_client_parms *fcp, struct fec_client **result)
272 {
273         int ret;
274         struct fec_client *fc;
275
276         if (fcp->max_slice_bytes < FEC_HEADER_SIZE + fcp->data_slices_per_group)
277                 return -ERRNO_TO_PARA_ERROR(EINVAL);
278         fc = para_calloc(sizeof(*fc));
279         fc->fcp = fcp;
280         ret = fec_new(fcp->data_slices_per_group, fcp->slices_per_group,
281                 &fc->parms);
282         if (ret < 0)
283                 goto err;
284         fc->first_stream_chunk = -1; /* stream not yet started */
285         fc->src_data = para_malloc(fc->fcp->slices_per_group * sizeof(char *));
286         para_list_add(&fc->node, &fec_client_list);
287         *result = fc;
288         return 1;
289 err:
290         fec_free(fc->parms);
291         free(fc);
292         *result = NULL;
293         return ret;
294 }
295
296 /**
297  * Remove one entry from the list of active fec clients.
298  *
299  * \param result The client to be removed.
300  */
301 void vss_del_fec_client(struct fec_client *fc)
302 {
303         list_del(&fc->node);
304         free(fc->src_data);
305         free(fc->enc_buf);
306         free(fc->extra_src_buf);
307         fec_free(fc->parms);
308         free(fc);
309 }
310
311 /*
312  * Compute if/when next slice is due. If it isn't due yet and \a diff is
313  * not \p Null, compute the time difference next - now, where
314  *
315  *      next = stream_start + (first_group_chunk - first_stream_chunk)
316  *              * chunk_time + slice_num * slice_time
317  */
318 static int next_slice_is_due(struct fec_client *fc, struct timeval *diff)
319 {
320         struct timeval tmp, next;
321         int ret;
322
323         if (fc->first_stream_chunk < 0)
324                 return 1;
325         tv_scale(fc->slice.num, &fc->group.slice_duration, &tmp);
326         tv_add(&tmp, &fc->group.start, &next);
327         ret = tv_diff(&next, now, diff);
328         return ret < 0? 1 : 0;
329 }
330
331 static void compute_slice_timeout(struct timeval *timeout)
332 {
333         struct fec_client *fc;
334
335         assert(vss_playing());
336         list_for_each_entry(fc, &fec_client_list, node) {
337                 struct timeval diff;
338                 int ret = next_slice_is_due(fc, &diff);
339
340                 // PARA_NOTICE_LOG("diff: %lu, ret: %d\n", tv2ms(&diff), ret);
341                 if (ret) {
342                         timeout->tv_sec = 0;
343                         timeout->tv_usec = 0;
344                         goto out;
345                 }
346                 /* timeout = min(timeout, diff) */
347                 if (tv_diff(&diff, timeout, NULL) < 0)
348                         *timeout = diff;
349         }
350 out:
351         return;
352         PARA_NOTICE_LOG("slice timeout: %lu:%lu\n", (long unsigned)timeout->tv_sec, (long unsigned)timeout->tv_usec);
353 }
354
355 /**
356  * Check if vss status flag \a P (playing) is set.
357  *
358  * \return Greater than zero if playing, zero otherwise.
359  *
360  */
361 unsigned int vss_playing(void)
362 {
363         return mmd->new_vss_status_flags & VSS_PLAYING;
364 }
365
366 /**
367  * Check if the \a N (next) status flag is set.
368  *
369  * \return Greater than zero if set, zero if not.
370  *
371  */
372 unsigned int vss_next(void)
373 {
374         return mmd->new_vss_status_flags & VSS_NEXT;
375 }
376
377 /**
378  * Check if a reposition request is pending.
379  *
380  * \return Greater than zero if true, zero otherwise.
381  *
382  */
383 unsigned int vss_repos(void)
384 {
385         return mmd->new_vss_status_flags & VSS_REPOS;
386 }
387
388 /**
389  * Check if the vss is currently paused.
390  *
391  * \return Greater than zero if paused, zero otherwise.
392  *
393  */
394 unsigned int vss_paused(void)
395 {
396         return !(mmd->new_vss_status_flags & VSS_NEXT)
397                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
398 }
399
400 /**
401  * Check if the vss is currently stopped.
402  *
403  * \return Greater than zero if paused, zero otherwise.
404  *
405  */
406 unsigned int vss_stopped(void)
407 {
408         return (mmd->new_vss_status_flags & VSS_NEXT)
409                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
410 }
411
412 static int chk_barrier(const char *bname, const struct timeval *barrier,
413                 struct timeval *diff, int print_log)
414 {
415         long ms;
416
417         if (tv_diff(now, barrier, diff) > 0)
418                 return 1;
419         ms = tv2ms(diff);
420         if (print_log && ms)
421                 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
422         return -1;
423 }
424
425 /*
426  * != NULL: timeout for next chunk
427  * NULL: nothing to do
428  */
429 static struct timeval *vss_compute_timeout(struct vss_task *vsst)
430 {
431         static struct timeval the_timeout;
432         struct timeval next_chunk;
433
434         if (vss_next() && vsst->map) {
435                 /* only sleep a bit, nec*/
436                 the_timeout.tv_sec = 0;
437                 the_timeout.tv_usec = 100;
438                 return &the_timeout;
439         }
440         if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
441                         &the_timeout, 1) < 0)
442                 return &the_timeout;
443         if (chk_barrier("eof", &vsst->eof_barrier, &the_timeout, 1) < 0)
444                 return &the_timeout;
445         if (chk_barrier("data send", &vsst->data_send_barrier,
446                         &the_timeout, 1) < 0)
447                 return &the_timeout;
448         if (!vss_playing() || !vsst->map)
449                 return NULL;
450         compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
451                 &mmd->stream_start, &next_chunk);
452         if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) >= 0) {
453                 /* chunk is due or bof */
454                 the_timeout.tv_sec = 0;
455                 the_timeout.tv_usec = 0;
456                 return &the_timeout;
457         }
458         /* compute min of current timeout and next slice time */
459         compute_slice_timeout(&the_timeout);
460         return &the_timeout;
461 }
462
463 static void vss_eof(struct vss_task *vsst)
464 {
465
466         mmd->stream_start = *now;
467         if (!vsst->map)
468                 return;
469         if (mmd->new_vss_status_flags & VSS_NOMORE)
470                 mmd->new_vss_status_flags = VSS_NEXT;
471         tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
472         para_munmap(vsst->map, mmd->size);
473         vsst->map = NULL;
474         mmd->chunks_sent = 0;
475         mmd->offset = 0;
476         mmd->afd.afhi.seconds_total = 0;
477         mmd->afd.afhi.chunk_tv.tv_sec = 0;
478         mmd->afd.afhi.chunk_tv.tv_usec = 0;
479         free(mmd->afd.afhi.chunk_table);
480         mmd->afd.afhi.chunk_table = NULL;
481         free(mmd->afd.afhi.info_string);
482         mmd->afd.afhi.info_string = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
483                 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
484         make_empty_status_items(mmd->afd.verbose_ls_output);
485         mmd->mtime = 0;
486         mmd->size = 0;
487         mmd->events++;
488 }
489
490 /**
491  * Get the list of all supported audio formats.
492  *
493  * \return Aa space separated list of all supported audio formats
494  * It is not allocated at runtime, i.e. there is no need to free
495  * the returned string in the caller.
496  */
497 const char *supported_audio_formats(void)
498 {
499         return SUPPORTED_AUDIO_FORMATS;
500 }
501
502 static int need_to_request_new_audio_file(struct vss_task *vsst)
503 {
504         struct timeval diff;
505
506         if (vsst->map) /* have audio file */
507                 return 0;
508         if (!vss_playing()) /* don't need one */
509                 return 0;
510         if (mmd->new_vss_status_flags & VSS_NOMORE)
511                 return 0;
512         if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
513                 return 0;
514         if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
515                         &diff, 1) < 0)
516                 return 0;
517         return 1;
518 }
519
520
521
522 /**
523  * Compute the timeout for para_server's main select-loop.
524  *
525  * This function gets called from para_server to determine the timeout value
526  * for its main select loop.
527  *
528  * \param s Pointer to the server scheduler.
529  * \param t Pointer to the vss task structure.
530  *
531  * Before the timeout is computed, the current vss status flags are evaluated
532  * and acted upon by calling appropriate functions from the lower layers.
533  * Possible actions include
534  *
535  *      - request a new audio file from afs,
536  *      - shutdown of all senders (stop/pause command),
537  *      - reposition the stream (ff/jmp command).
538  */
539 static void vss_pre_select(struct sched *s, struct task *t)
540 {
541         int i;
542         struct timeval *tv, diff;
543         struct vss_task *vsst = container_of(t, struct vss_task, task);
544
545         if (!vsst->map || vss_next() || vss_paused() || vss_repos()) {
546                 struct fec_client *fc, *tmp;
547                 for (i = 0; senders[i].name; i++)
548                         if (senders[i].shutdown_clients)
549                                 senders[i].shutdown_clients();
550                 list_for_each_entry_safe(fc, tmp, &fec_client_list, node)
551                         fc->first_stream_chunk = -1;
552         }
553         if (vss_next())
554                 vss_eof(vsst);
555         else if (vss_paused()) {
556                 if (mmd->chunks_sent)
557                         tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
558                 mmd->chunks_sent = 0;
559         } else if (vss_repos()) {
560                 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
561                 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
562                 mmd->chunks_sent = 0;
563                 mmd->current_chunk = mmd->repos_request;
564                 mmd->new_vss_status_flags &= ~VSS_REPOS;
565         }
566         if (need_to_request_new_audio_file(vsst)) {
567                 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
568                 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
569                 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
570         } else
571                 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
572         for (i = 0; senders[i].name; i++) {
573                 if (!senders[i].pre_select)
574                         continue;
575                 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
576         }
577         tv = vss_compute_timeout(vsst);
578         if (tv && tv_diff(tv, &s->timeout, &diff) < 0)
579                 s->timeout = *tv;
580 }
581
582 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
583 {
584         char control[255], buf[8];
585         struct msghdr msg = {.msg_iov = NULL};
586         struct cmsghdr *cmsg;
587         struct iovec iov;
588         int ret = 0;
589
590         *fd = -1;
591         iov.iov_base = buf;
592         iov.iov_len = sizeof(buf);
593         msg.msg_iov = &iov;
594         msg.msg_iovlen = 1;
595         msg.msg_control = control;
596         msg.msg_controllen = sizeof(control);
597         memset(buf, 0, sizeof(buf));
598         ret = recvmsg(afs_socket, &msg, 0);
599         if (ret < 0)
600                 return -ERRNO_TO_PARA_ERROR(errno);
601         if (iov.iov_len != sizeof(buf))
602                 return -E_AFS_SHORT_READ;
603         *code = *(uint32_t*)buf;
604         *data =  *(uint32_t*)(buf + 4);
605         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
606                 if (cmsg->cmsg_level != SOL_SOCKET
607                         || cmsg->cmsg_type != SCM_RIGHTS)
608                 continue;
609                 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
610                 continue;
611                 *fd = *(int *)CMSG_DATA(cmsg);
612         }
613         return 1;
614 }
615
616 static void recv_afs_result(struct vss_task *vsst)
617 {
618         int ret, passed_fd, shmid;
619         uint32_t afs_code = 0, afs_data = 0;
620         struct stat statbuf;
621
622         vsst->afsss = AFS_SOCKET_READY;
623         mmd->afd.afhi.chunk_table = NULL;
624         ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
625         if (ret < 0)
626                 goto err;
627         PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
628                 afs_data);
629         ret = -E_NOFD;
630         if (afs_code != NEXT_AUDIO_FILE)
631                 goto err;
632         if (passed_fd < 0)
633                 goto err;
634         shmid = afs_data;
635         ret = load_afd(shmid, &mmd->afd);
636         if (ret < 0)
637                 goto err;
638         shm_destroy(shmid);
639         ret = fstat(passed_fd, &statbuf);
640         if (ret < 0) {
641                 PARA_ERROR_LOG("fstat error:\n");
642                 ret = -ERRNO_TO_PARA_ERROR(errno);
643                 goto err;
644         }
645         mmd->size = statbuf.st_size;
646         mmd->mtime = statbuf.st_mtime;
647         ret = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE, passed_fd,
648                 0, &vsst->map);
649         if (ret < 0)
650                 goto err;
651         close(passed_fd);
652         mmd->chunks_sent = 0;
653         mmd->current_chunk = 0;
654         mmd->offset = 0;
655         mmd->events++;
656         mmd->num_played++;
657         mmd->new_vss_status_flags &= (~VSS_NEXT);
658         afh_get_header(&mmd->afd.afhi, vsst->map, &vsst->header_buf,
659                 &vsst->header_len);
660         return;
661 err:
662         free(mmd->afd.afhi.chunk_table);
663         if (passed_fd >= 0)
664                 close(passed_fd);
665         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
666         mmd->new_vss_status_flags = VSS_NEXT;
667 }
668
669 /**
670  * Main sending function.
671  *
672  * This function gets called from vss_post_select(). It checks whether the next
673  * chunk of data should be pushed out. It obtains a pointer to the data to be
674  * sent out as well as its length from mmd->afd.afhi. This information is then
675  * passed to each supported sender's send() function as well as to the send()
676  * functions of each registered fec client.
677  */
678 static void vss_send(struct vss_task *vsst)
679 {
680         int i;
681         struct timeval due;
682         struct fec_client *fc, *tmp_fc;
683
684         if (!vsst->map || !vss_playing())
685                 return;
686         if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
687                 return;
688         if (chk_barrier("data send", &vsst->data_send_barrier,
689                         &due, 1) < 0)
690                 return;
691         if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
692                 mmd->new_vss_status_flags |= VSS_NEXT;
693                 return;
694         }
695         if (!mmd->chunks_sent) {
696                 struct timeval tmp;
697                 mmd->stream_start = *now;
698                 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
699                 mmd->offset = tv2ms(&tmp);
700                 mmd->events++;
701         }
702         compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
703                 &mmd->stream_start, &due);
704         if (tv_diff(&due, now, NULL) <= 0) {
705                 const char *buf;
706                 size_t len;
707                 /*
708                  * We call the send function also in case of empty chunks as
709                  * they might have still some data queued which can be sent in
710                  * this case.
711                  */
712                 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map,
713                         &buf, &len);
714                 for (i = 0; senders[i].name; i++) {
715                         if (!senders[i].send)
716                                 continue;
717                         senders[i].send(mmd->current_chunk, mmd->chunks_sent,
718                                 buf, len, vsst->header_buf, vsst->header_len);
719                 }
720         }
721         list_for_each_entry_safe(fc, tmp_fc, &fec_client_list, node) {
722                 if (!next_slice_is_due(fc, NULL))
723                         continue;
724                 compute_next_fec_slice(fc, vsst);
725                 PARA_DEBUG_LOG("sending %d:%d (%zu bytes)\n", fc->group.num,
726                         fc->slice.num, fc->slice.bytes);
727                 fc->fcp->send((char *)fc->enc_buf,
728                         fc->slice.bytes + FEC_HEADER_SIZE,
729                         fc->fcp->private_data);
730                 fc->slice.num++;
731         }
732         mmd->new_vss_status_flags |= VSS_PLAYING;
733         mmd->chunks_sent++;
734         mmd->current_chunk++;
735 }
736
737 static void vss_post_select(struct sched *s, struct task *t)
738 {
739         int ret, i;
740         struct vss_task *vsst = container_of(t, struct vss_task, task);
741
742         if (mmd->sender_cmd_data.cmd_num >= 0) {
743                 int num = mmd->sender_cmd_data.cmd_num,
744                         sender_num = mmd->sender_cmd_data.sender_num;
745
746                 if (senders[sender_num].client_cmds[num])
747                         senders[sender_num].client_cmds[num](&mmd->sender_cmd_data);
748                 mmd->sender_cmd_data.cmd_num = -1;
749         }
750         if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
751                 if (FD_ISSET(vsst->afs_socket, &s->rfds))
752                         recv_afs_result(vsst);
753         } else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
754                 PARA_NOTICE_LOG("requesting new fd from afs\n");
755                 ret = send_buffer(vsst->afs_socket, "new");
756                 vsst->afsss = AFS_SOCKET_AFD_PENDING;
757         }
758         for (i = 0; senders[i].name; i++) {
759                 if (!senders[i].post_select)
760                         continue;
761                 senders[i].post_select(&s->rfds, &s->wfds);
762         }
763         if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
764                         (vss_next() && vss_playing()))
765                 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
766         vss_send(vsst);
767 }
768
769 /**
770  * Initialize the virtual streaming system task.
771  *
772  * \param afs_socket The fd for communication with afs.
773  *
774  * This also initializes all supported senders and starts streaming
775  * if the --autoplay command line flag was given.
776  */
777 void init_vss_task(int afs_socket)
778 {
779         static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
780         int i;
781         char *hn = para_hostname(), *home = para_homedir();
782         long unsigned announce_time = conf.announce_time_arg > 0?
783                         conf.announce_time_arg : 300,
784                 autoplay_delay = conf.autoplay_delay_arg > 0?
785                         conf.autoplay_delay_arg : 0;
786
787         vsst->afs_socket = afs_socket;
788         vsst->task.pre_select = vss_pre_select;
789         vsst->task.post_select = vss_post_select;
790         ms2tv(announce_time, &vsst->announce_tv);
791         PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
792         for (i = 0; senders[i].name; i++) {
793                 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
794                 senders[i].init(&senders[i]);
795         }
796         free(hn);
797         free(home);
798         mmd->sender_cmd_data.cmd_num = -1;
799         make_empty_status_items(mmd->afd.verbose_ls_output);
800         if (conf.autoplay_given) {
801                 struct timeval tmp;
802                 mmd->vss_status_flags |= VSS_PLAYING;
803                 mmd->new_vss_status_flags |= VSS_PLAYING;
804                 ms2tv(autoplay_delay, &tmp);
805                 tv_add(now, &tmp, &vsst->autoplay_barrier);
806                 tv_add(&vsst->autoplay_barrier, &vsst->announce_tv,
807                         &vsst->data_send_barrier);
808         }
809         INIT_LIST_HEAD(&fec_client_list);
810         register_task(&vsst->task);
811 }