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