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