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