2 * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file vss.c The virtual streaming system.
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
20 #include "portable_io.h"
27 #include "server.cmdline.h"
35 extern struct misc_meta_data *mmd;
37 extern void dccp_send_init(struct sender *);
38 extern void http_send_init(struct sender *);
39 extern void udp_send_init(struct sender *);
41 /** The list of supported senders. */
42 struct sender senders[] = {
45 .init = http_send_init,
49 .init = dccp_send_init,
53 .init = udp_send_init,
60 /** The possible states of the afs socket. */
61 enum afs_socket_status {
62 /** Socket is inactive. */
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
70 /** The task structure for the virtual streaming system. */
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. */
82 /** The current state of \a afs_socket. */
83 enum afs_socket_status afsss;
84 /** The memory mapped audio file. */
86 /** Used by the scheduler. */
88 /** Pointer to the header of the mapped audio file. */
90 /** Length of the audio file header. */
92 /** Time between audio file headers are sent. */
93 struct timeval header_interval;
97 * The list of currently connected fec clients.
99 * Senders may use \ref vss_add_fec_client() to add entries to the list.
101 static struct list_head fec_client_list;
104 * Data associated with one FEC group.
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.
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.
116 /** The number of the FEC group. */
118 /** Number of bytes in this group. */
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. */
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 /** Number of bytes per slice for this group. */
133 uint16_t slice_bytes;
136 enum fec_client_state {
137 FEC_STATE_NONE = 0, /**< not initialized and not enabled */
138 FEC_STATE_DISABLED, /**< temporarily disabled */
139 FEC_STATE_READY_TO_RUN /**< initialized and enabled */
143 * Describes one connected FEC client.
146 /** Current state of the client */
147 enum fec_client_state state;
148 /** The connected sender client (transport layer). */
149 struct sender_client *sc;
150 /** Parameters requested by the client. */
151 struct fec_client_parms *fcp;
152 /** Used by the core FEC code. */
153 struct fec_parms *parms;
154 /** The position of this client in the fec client list. */
155 struct list_head node;
156 /** When the first slice for this client was sent. */
157 struct timeval stream_start;
158 /** The first chunk sent to this FEC client. */
159 int first_stream_chunk;
160 /** Describes the current group. */
161 struct fec_group group;
162 /** The current slice. */
163 uint8_t current_slice_num;
164 /** The data to be FEC-encoded (point to a region within the mapped audio file). */
165 const unsigned char **src_data;
166 /** Last time an audio header was sent. */
167 struct timeval next_header_time;
168 /** Used for the last source pointer of an audio file. */
169 unsigned char *extra_src_buf;
170 /** Needed for the last slice of the audio file header. */
171 unsigned char *extra_header_buf;
172 /** Extra slices needed to store largest chunk + header. */
173 int num_extra_slices;
174 /** Contains the FEC-encoded data. */
175 unsigned char *enc_buf;
176 /** Maximal packet size. */
181 * Get the chunk time of the current audio file.
183 * \return A pointer to a struct containing the chunk time, or NULL,
184 * if currently no audio file is selected.
186 struct timeval *vss_chunk_time(void)
188 if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
189 mmd->afd.afhi.chunk_tv.tv_usec == 0)
191 return &mmd->afd.afhi.chunk_tv;
195 * Write a fec header to a buffer.
197 * \param buf The buffer to write to.
198 * \param h The fec header to write.
200 static void write_fec_header(struct fec_client *fc, struct vss_task *vsst)
202 char *buf = (char *)fc->enc_buf;
203 struct fec_group *g = &fc->group;
204 struct fec_client_parms *p = fc->fcp;
206 write_u32(buf, FEC_MAGIC);
208 write_u8(buf + 4, p->slices_per_group + fc->num_extra_slices);
209 write_u8(buf + 5, p->data_slices_per_group + fc->num_extra_slices);
210 write_u32(buf + 6, g->num_header_slices? vsst->header_len : 0);
212 write_u32(buf + 10, g->num);
213 write_u32(buf + 14, g->bytes);
215 write_u8(buf + 18, fc->current_slice_num);
216 write_u8(buf + 19, 0); /* unused */
217 write_u16(buf + 20, g->slice_bytes);
218 write_u8(buf + 22, g->first_chunk? 0 : 1);
219 write_u8(buf + 23, vsst->header_len? 1 : 0);
220 memset(buf + 24, 0, 8);
223 static bool need_audio_header(struct fec_client *fc, struct vss_task *vsst)
225 if (!mmd->current_chunk) {
226 tv_add(now, &vsst->header_interval, &fc->next_header_time);
229 if (!vsst->header_buf)
231 if (vsst->header_len == 0)
233 if (fc->group.num > 0) {
234 if (!fc->fcp->need_periodic_header)
236 if (tv_diff(&fc->next_header_time, now, NULL) > 0)
239 tv_add(now, &vsst->header_interval, &fc->next_header_time);
243 static bool need_data_slices(struct fec_client *fc, struct vss_task *vsst)
245 if (fc->group.num > 0)
247 if (!vsst->header_buf)
249 if (vsst->header_len == 0)
251 if (fc->fcp->need_periodic_header)
256 static int num_slices(long unsigned bytes, int max_payload, int rs)
260 assert(max_payload > 0);
262 ret = DIV_ROUND_UP(bytes, max_payload);
268 /* set group start and group duration */
269 static void set_group_timing(struct fec_client *fc, struct vss_task *vsst)
271 struct fec_group *g = &fc->group;
272 struct timeval *chunk_tv = vss_chunk_time();
274 if (!need_data_slices(fc, vsst))
275 ms2tv(200, &g->duration);
277 tv_scale(g->num_chunks, chunk_tv, &g->duration);
278 tv_divide(fc->fcp->slices_per_group + fc->num_extra_slices,
279 &g->duration, &g->slice_duration);
280 PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
281 tv2ms(&g->duration), tv2ms(chunk_tv), tv2ms(&g->slice_duration));
284 static int initialize_fec_client(struct fec_client *fc, struct vss_task *vsst)
287 int hs, ds, rs; /* header/data/redundant slices */
288 struct fec_client_parms *fcp = fc->fcp;
293 * Set the maximum slice size to the Maximum Packet Size if the
294 * transport protocol allows to determine this value. The user
295 * can specify a slice size up to this value.
297 ret = fcp->init_fec(fc->sc);
302 fc->mps = generic_max_transport_msg_size(fc->sc->fd);
303 if (fc->mps <= FEC_HEADER_SIZE)
304 return -ERRNO_TO_PARA_ERROR(EINVAL);
306 rs = fc->fcp->slices_per_group - fc->fcp->data_slices_per_group;
307 ret = num_slices(vsst->header_len, fc->mps - FEC_HEADER_SIZE, rs);
311 ret = num_slices(mmd->afd.max_chunk_size, fc->mps - FEC_HEADER_SIZE, rs);
315 if (fc->fcp->need_periodic_header)
318 k = PARA_MAX(hs, ds);
319 if (k < fc->fcp->data_slices_per_group)
320 k = fc->fcp->data_slices_per_group;
321 fc->num_extra_slices = k - fc->fcp->data_slices_per_group;
324 ret = fec_new(k, n, &fc->parms);
327 PARA_INFO_LOG("mps: %d, k: %d, n: %d, extra slices: %d\n",
328 fc->mps, k, n, fc->num_extra_slices);
329 fc->src_data = para_realloc(fc->src_data, k * sizeof(char *));
330 fc->enc_buf = para_realloc(fc->enc_buf, fc->mps);
331 fc->extra_src_buf = para_realloc(fc->extra_src_buf, fc->mps);
332 fc->extra_header_buf = para_realloc(fc->extra_header_buf, fc->mps);
334 fc->state = FEC_STATE_READY_TO_RUN;
335 fc->next_header_time.tv_sec = 0;
336 fc->stream_start = *now;
337 fc->first_stream_chunk = mmd->current_chunk;
341 static void compute_group_size(struct vss_task *vsst, struct fec_group *g,
344 int i, max_chunks = PARA_MAX(1LU, 150 / tv2ms(vss_chunk_time()));
349 * Include chunks into the group until the group duration is at least
350 * 150ms. For ogg and wma, a single chunk's duration (ogg page/wma
351 * super frame) is already larger than 150ms, so a FEC group consists
352 * of exactly one chunk for these audio formats.
357 int chunk_num = g->first_chunk + i;
359 if (g->bytes > 0 && i >= max_chunks) /* duration limit */
361 if (chunk_num >= mmd->afd.afhi.chunks_total) /* eof */
363 afh_get_chunk(chunk_num, &mmd->afd.afhi, vsst->map, &buf, &len);
364 if (g->bytes + len > max_bytes)
366 /* Include this chunk */
370 assert(g->num_chunks);
374 * Compute the slice size of the next group.
376 * The FEC parameters n and k are fixed but the slice size varies per
377 * FEC group. We'd like to choose slices as small as possible to avoid
378 * unnecessary FEC calculations but large enough to guarantee that the
379 * k data slices suffice to encode the header (if needed) and the data
382 * Once we know the payload of the next group, we define the number s
383 * of bytes per slice for this group by
385 * s = ceil(payload / k)
387 * However, for header streams, computing s is more complicated since no
388 * overlapping of header and data slices is possible. Hence we have k >=
389 * 2 and s must satisfy
391 * (*) ceil(h / s) + ceil(d / s) <= k
393 * where h and d are payload of the header and the data chunk(s)
394 * respectively. In general there is no value for s such that (*)
395 * becomes an equality, for example if h = 4000, d = 5000 and k = 10.
397 * We use the following approach for computing a suitable value for s:
400 * k1 := ceil(k * min(h, d) / (h + d)),
403 * Note that k >= 2 implies k1 > 0 and k2 > 0, so
405 * s := max(ceil(min(h, d) / k1), ceil(max(h, d) / k2))
407 * is well-defined. Inequality (*) holds for this value of s since k1
408 * slices suffice to store min(h, d) while k2 slices suffice to store
409 * max(h, d), i.e. the first addent of (*) is bounded by k1 and the
412 * For the above example we obtain
414 * k1 = ceil(10 * 4000 / 9000) = 5, k2 = 5,
415 * s = max(4000 / 5, 5000 / 5) = 1000,
417 * which is optimal since a slice size of 999 bytes would already require
420 static int compute_slice_size(struct fec_client *fc, struct vss_task *vsst)
422 struct fec_group *g = &fc->group;
423 int k = fc->fcp->data_slices_per_group + fc->num_extra_slices;
424 int n = fc->fcp->slices_per_group + fc->num_extra_slices;
425 int ret, k1, k2, h, d, min, max, sum;
426 int max_slice_bytes = fc->mps - FEC_HEADER_SIZE;
429 if (!need_audio_header(fc, vsst)) {
430 max_group_bytes = k * max_slice_bytes;
431 g->num_header_slices = 0;
432 compute_group_size(vsst, g, max_group_bytes);
433 g->slice_bytes = DIV_ROUND_UP(g->bytes, k);
434 if (g->slice_bytes == 0)
438 if (!need_data_slices(fc, vsst)) {
441 g->slice_bytes = DIV_ROUND_UP(vsst->header_len, k);
442 g->num_header_slices = k;
445 h = vsst->header_len;
446 max_group_bytes = (k - num_slices(h, max_slice_bytes, n - k))
448 compute_group_size(vsst, g, max_group_bytes);
451 g->slice_bytes = DIV_ROUND_UP(h, k);
452 ret = num_slices(vsst->header_len, g->slice_bytes, n - k);
455 g->num_header_slices = ret;
458 min = PARA_MIN(h, d);
459 max = PARA_MAX(h, d);
461 k1 = DIV_ROUND_UP(k * min, sum);
466 g->slice_bytes = PARA_MAX(DIV_ROUND_UP(min, k1), DIV_ROUND_UP(max, k2));
468 * This value of s := g->slice_bytes satisfies inequality (*) above,
469 * but it might be larger than max_slice_bytes. However, we know that
470 * max_slice_bytes are sufficient to store header and data, so:
472 g->slice_bytes = PARA_MIN((int)g->slice_bytes, max_slice_bytes);
474 ret = num_slices(vsst->header_len, g->slice_bytes, n - k);
477 g->num_header_slices = ret;
481 static int setup_next_fec_group(struct fec_client *fc, struct vss_task *vsst)
483 int ret, i, k, n, data_slices;
486 struct fec_group *g = &fc->group;
488 if (fc->state == FEC_STATE_NONE) {
489 ret = initialize_fec_client(fc, vsst);
492 g->first_chunk = mmd->current_chunk;
497 if (g->first_chunk + g->num_chunks >= mmd->afd.afhi.chunks_total)
500 * Start and duration of this group depend only on the previous
501 * group. Compute the new group start as g->start += g->duration.
504 tv_add(&tmp, &g->duration, &g->start);
505 set_group_timing(fc, vsst);
506 g->first_chunk += g->num_chunks;
509 k = fc->fcp->data_slices_per_group + fc->num_extra_slices;
510 n = fc->fcp->slices_per_group + fc->num_extra_slices;
512 compute_slice_size(fc, vsst);
513 assert(g->slice_bytes > 0);
514 ret = num_slices(g->bytes, g->slice_bytes, n - k);
518 assert(g->num_header_slices + data_slices <= k);
519 fc->current_slice_num = 0;
521 set_group_timing(fc, vsst);
522 /* setup header slices */
523 buf = vsst->header_buf;
524 for (i = 0; i < g->num_header_slices; i++) {
525 if (buf + g->slice_bytes <= vsst->header_buf + vsst->header_len) {
526 fc->src_data[i] = (const unsigned char *)buf;
527 buf += g->slice_bytes;
531 * Can not use vss->header_buf for this slice as it
532 * goes beyond the buffer. This slice will not be fully
535 uint32_t payload_size = vsst->header_buf
536 + vsst->header_len - buf;
537 memcpy(fc->extra_header_buf, buf, payload_size);
538 if (payload_size < g->slice_bytes)
539 memset(fc->extra_header_buf + payload_size, 0,
540 g->slice_bytes - payload_size);
541 fc->src_data[i] = fc->extra_header_buf;
542 assert(i == g->num_header_slices - 1);
545 /* setup data slices */
546 afh_get_chunk(g->first_chunk, &mmd->afd.afhi, vsst->map, &buf, &len);
547 for (; i < g->num_header_slices + data_slices; i++) {
548 if (buf + g->slice_bytes > vsst->map + mmd->size) {
550 * Can not use the memory mapped audio file for this
551 * slice as it goes beyond the map.
553 uint32_t payload_size = vsst->map + mmd->size - buf;
554 memcpy(fc->extra_src_buf, buf, payload_size);
555 if (payload_size < g->slice_bytes)
556 memset(fc->extra_src_buf + payload_size, 0,
557 g->slice_bytes - payload_size);
558 fc->src_data[i] = fc->extra_src_buf;
562 fc->src_data[i] = (const unsigned char *)buf;
563 buf += g->slice_bytes;
566 /* use arbitrary data for all remaining slices */
569 fc->src_data[i] = (const unsigned char *)buf;
571 PARA_DEBUG_LOG("FEC group %d: %d chunks (%d - %d), %d bytes\n",
572 g->num, g->num_chunks, g->first_chunk,
573 g->first_chunk + g->num_chunks - 1, g->bytes
575 PARA_DEBUG_LOG("slice_bytes: %d, %d header slices, %d data slices\n",
576 g->slice_bytes, g->num_header_slices, data_slices
581 static int compute_next_fec_slice(struct fec_client *fc, struct vss_task *vsst)
583 if (fc->state == FEC_STATE_NONE || fc->current_slice_num
584 == fc->fcp->slices_per_group + fc->num_extra_slices) {
585 int ret = setup_next_fec_group(fc, vsst);
589 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
590 PARA_ERROR_LOG("FEC client temporarily disabled\n");
591 fc->state = FEC_STATE_DISABLED;
595 write_fec_header(fc, vsst);
596 fec_encode(fc->parms, fc->src_data, fc->enc_buf + FEC_HEADER_SIZE,
597 fc->current_slice_num, fc->group.slice_bytes);
602 * Return a buffer that marks the end of the stream.
604 * \param buf Result pointer.
605 * \return The length of the eof buffer.
607 * This is used for (multicast) udp streaming where closing the socket on the
608 * sender might not give rise to an eof condition at the peer.
610 size_t vss_get_fec_eof_packet(const char **buf)
612 static const char fec_eof_packet[FEC_HEADER_SIZE] = FEC_EOF_PACKET;
613 *buf = fec_eof_packet;
614 return FEC_HEADER_SIZE;
618 * Add one entry to the list of active fec clients.
620 * \param sc Generic sender_client data of the transport layer.
621 * \param fcp FEC parameters as supplied by the transport layer.
623 * \return Newly allocated fec_client struct.
625 struct fec_client *vss_add_fec_client(struct sender_client *sc,
626 struct fec_client_parms *fcp)
628 struct fec_client *fc = para_calloc(sizeof(*fc));
632 para_list_add(&fc->node, &fec_client_list);
637 * Remove one entry from the list of active fec clients.
639 * \param fc The client to be removed.
641 void vss_del_fec_client(struct fec_client *fc)
646 free(fc->extra_src_buf);
647 free(fc->extra_header_buf);
653 * Compute if/when next slice is due. If it isn't due yet and \a diff is
654 * not \p Null, compute the time difference next - now, where
656 * next = stream_start + (first_group_chunk - first_stream_chunk)
657 * * chunk_time + slice_num * slice_time
659 static int next_slice_is_due(struct fec_client *fc, struct timeval *diff)
661 struct timeval tmp, next;
664 if (fc->state == FEC_STATE_NONE)
666 tv_scale(fc->current_slice_num, &fc->group.slice_duration, &tmp);
667 tv_add(&tmp, &fc->group.start, &next);
668 ret = tv_diff(&next, now, diff);
669 return ret < 0? 1 : 0;
672 static void compute_slice_timeout(struct timeval *timeout)
674 struct fec_client *fc;
676 list_for_each_entry(fc, &fec_client_list, node) {
679 if (fc->state != FEC_STATE_READY_TO_RUN)
681 if (next_slice_is_due(fc, &diff)) {
683 timeout->tv_usec = 0;
686 /* timeout = min(timeout, diff) */
687 if (tv_diff(&diff, timeout, NULL) < 0)
692 static void set_eof_barrier(struct vss_task *vsst)
694 struct fec_client *fc;
695 struct timeval timeout = {1, 0}, *chunk_tv = vss_chunk_time();
699 list_for_each_entry(fc, &fec_client_list, node) {
700 struct timeval group_duration;
702 if (fc->state != FEC_STATE_READY_TO_RUN)
704 tv_scale(fc->group.num_chunks, chunk_tv, &group_duration);
705 if (tv_diff(&timeout, &group_duration, NULL) < 0)
706 timeout = group_duration;
709 tv_add(now, &timeout, &vsst->eof_barrier);
713 * Check if vss status flag \a P (playing) is set.
715 * \return Greater than zero if playing, zero otherwise.
718 unsigned int vss_playing(void)
720 return mmd->new_vss_status_flags & VSS_PLAYING;
724 * Check if the \a N (next) status flag is set.
726 * \return Greater than zero if set, zero if not.
729 unsigned int vss_next(void)
731 return mmd->new_vss_status_flags & VSS_NEXT;
735 * Check if a reposition request is pending.
737 * \return Greater than zero if true, zero otherwise.
740 unsigned int vss_repos(void)
742 return mmd->new_vss_status_flags & VSS_REPOS;
746 * Check if the vss is currently paused.
748 * \return Greater than zero if paused, zero otherwise.
751 unsigned int vss_paused(void)
753 return !(mmd->new_vss_status_flags & VSS_NEXT)
754 && !(mmd->new_vss_status_flags & VSS_PLAYING);
758 * Check if the vss is currently stopped.
760 * \return Greater than zero if paused, zero otherwise.
763 unsigned int vss_stopped(void)
765 return (mmd->new_vss_status_flags & VSS_NEXT)
766 && !(mmd->new_vss_status_flags & VSS_PLAYING);
769 static int chk_barrier(const char *bname, const struct timeval *barrier,
770 struct timeval *diff, int print_log)
774 if (tv_diff(now, barrier, diff) > 0)
778 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
783 * != NULL: timeout for next chunk
784 * NULL: nothing to do
786 static struct timeval *vss_compute_timeout(struct vss_task *vsst)
788 static struct timeval the_timeout;
789 struct timeval next_chunk;
791 if (vss_next() && vsst->map) {
792 /* only sleep a bit, nec*/
793 the_timeout.tv_sec = 0;
794 the_timeout.tv_usec = 100;
797 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
798 &the_timeout, 1) < 0)
800 if (chk_barrier("eof", &vsst->eof_barrier, &the_timeout, 1) < 0)
802 if (chk_barrier("data send", &vsst->data_send_barrier,
803 &the_timeout, 1) < 0)
805 if (!vss_playing() || !vsst->map)
807 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
808 &mmd->stream_start, &next_chunk);
809 if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) >= 0) {
810 /* chunk is due or bof */
811 the_timeout.tv_sec = 0;
812 the_timeout.tv_usec = 0;
815 /* compute min of current timeout and next slice time */
816 compute_slice_timeout(&the_timeout);
820 static void vss_eof(struct vss_task *vsst)
825 if (mmd->new_vss_status_flags & VSS_NOMORE)
826 mmd->new_vss_status_flags = VSS_NEXT;
827 set_eof_barrier(vsst);
828 afh_free_header(vsst->header_buf, mmd->afd.audio_format_id);
829 vsst->header_buf = NULL;
830 para_munmap(vsst->map, mmd->size);
832 mmd->chunks_sent = 0;
834 mmd->afd.afhi.seconds_total = 0;
835 mmd->afd.afhi.chunk_tv.tv_sec = 0;
836 mmd->afd.afhi.chunk_tv.tv_usec = 0;
837 free(mmd->afd.afhi.chunk_table);
838 mmd->afd.afhi.chunk_table = NULL;
844 static int need_to_request_new_audio_file(struct vss_task *vsst)
848 if (vsst->map) /* have audio file */
850 if (!vss_playing()) /* don't need one */
852 if (mmd->new_vss_status_flags & VSS_NOMORE)
854 if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
856 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
862 static void set_mmd_offset(void)
864 struct timeval offset;
865 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &offset);
866 mmd->offset = tv2ms(&offset);
870 * Compute the timeout for the main select-loop of the scheduler.
872 * \param s Pointer to the server scheduler.
873 * \param t Pointer to the vss task structure.
875 * Before the timeout is computed, the current vss status flags are evaluated
876 * and acted upon by calling appropriate functions from the lower layers.
877 * Possible actions include
879 * - request a new audio file from afs,
880 * - shutdown of all senders (stop/pause command),
881 * - reposition the stream (ff/jmp command).
883 static void vss_pre_select(struct sched *s, struct task *t)
887 struct vss_task *vsst = container_of(t, struct vss_task, task);
889 if (!vsst->map || vss_next() || vss_paused() || vss_repos()) {
890 struct fec_client *fc, *tmp;
891 for (i = 0; senders[i].name; i++)
892 if (senders[i].shutdown_clients)
893 senders[i].shutdown_clients();
894 list_for_each_entry_safe(fc, tmp, &fec_client_list, node)
895 fc->state = FEC_STATE_NONE;
896 mmd->stream_start.tv_sec = 0;
897 mmd->stream_start.tv_usec = 0;
901 else if (vss_paused()) {
902 if (mmd->chunks_sent)
903 set_eof_barrier(vsst);
904 mmd->chunks_sent = 0;
905 } else if (vss_repos()) {
906 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
907 set_eof_barrier(vsst);
908 mmd->chunks_sent = 0;
909 mmd->current_chunk = mmd->repos_request;
910 mmd->new_vss_status_flags &= ~VSS_REPOS;
913 if (need_to_request_new_audio_file(vsst)) {
914 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
915 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
916 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
918 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
919 for (i = 0; senders[i].name; i++) {
920 if (!senders[i].pre_select)
922 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
924 tv = vss_compute_timeout(vsst);
926 sched_request_timeout(tv, s);
929 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
931 char control[255], buf[8];
932 struct msghdr msg = {.msg_iov = NULL};
933 struct cmsghdr *cmsg;
939 iov.iov_len = sizeof(buf);
942 msg.msg_control = control;
943 msg.msg_controllen = sizeof(control);
944 memset(buf, 0, sizeof(buf));
945 ret = recvmsg(afs_socket, &msg, 0);
947 return -ERRNO_TO_PARA_ERROR(errno);
948 if (iov.iov_len != sizeof(buf))
949 return -E_AFS_SHORT_READ;
950 *code = *(uint32_t*)buf;
951 *data = *(uint32_t*)(buf + 4);
952 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
953 if (cmsg->cmsg_level != SOL_SOCKET
954 || cmsg->cmsg_type != SCM_RIGHTS)
956 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
958 *fd = *(int *)CMSG_DATA(cmsg);
963 static void recv_afs_result(struct vss_task *vsst, fd_set *rfds)
965 int ret, passed_fd, shmid;
966 uint32_t afs_code = 0, afs_data = 0;
969 if (!FD_ISSET(vsst->afs_socket, rfds))
971 ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
972 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
976 vsst->afsss = AFS_SOCKET_READY;
977 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
980 if (afs_code != NEXT_AUDIO_FILE)
985 ret = load_afd(shmid, &mmd->afd);
989 ret = fstat(passed_fd, &statbuf);
991 PARA_ERROR_LOG("fstat error:\n");
992 ret = -ERRNO_TO_PARA_ERROR(errno);
995 mmd->size = statbuf.st_size;
996 mmd->mtime = statbuf.st_mtime;
997 ret = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE, passed_fd,
1002 mmd->chunks_sent = 0;
1003 mmd->current_chunk = 0;
1007 mmd->new_vss_status_flags &= (~VSS_NEXT);
1008 afh_get_header(&mmd->afd.afhi, mmd->afd.audio_format_id,
1009 vsst->map, mmd->size, &vsst->header_buf, &vsst->header_len);
1012 free(mmd->afd.afhi.chunk_table);
1015 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1016 mmd->new_vss_status_flags = VSS_NEXT;
1020 * Main sending function.
1022 * This function gets called from vss_post_select(). It checks whether the next
1023 * chunk of data should be pushed out. It obtains a pointer to the data to be
1024 * sent out as well as its length from mmd->afd.afhi. This information is then
1025 * passed to each supported sender's send() function as well as to the send()
1026 * functions of each registered fec client.
1028 static void vss_send(struct vss_task *vsst)
1030 int i, fec_active = 0;
1032 struct fec_client *fc, *tmp_fc;
1034 if (!vsst->map || !vss_playing())
1036 if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
1038 if (chk_barrier("data send", &vsst->data_send_barrier,
1041 list_for_each_entry_safe(fc, tmp_fc, &fec_client_list, node) {
1042 if (fc->state == FEC_STATE_DISABLED)
1044 if (!next_slice_is_due(fc, NULL)) {
1048 if (compute_next_fec_slice(fc, vsst) <= 0)
1050 PARA_DEBUG_LOG("sending %d:%d (%u bytes)\n", fc->group.num,
1051 fc->current_slice_num, fc->group.slice_bytes);
1052 fc->fcp->send_fec(fc->sc, (char *)fc->enc_buf,
1053 fc->group.slice_bytes + FEC_HEADER_SIZE);
1054 fc->current_slice_num++;
1057 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
1059 mmd->new_vss_status_flags |= VSS_NEXT;
1062 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
1063 &mmd->stream_start, &due);
1064 if (tv_diff(&due, now, NULL) <= 0) {
1068 if (!mmd->chunks_sent) {
1069 mmd->stream_start = *now;
1074 * We call the send function also in case of empty chunks as
1075 * they might have still some data queued which can be sent in
1078 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map,
1080 for (i = 0; senders[i].name; i++) {
1081 if (!senders[i].send)
1083 senders[i].send(mmd->current_chunk, mmd->chunks_sent,
1084 buf, len, vsst->header_buf, vsst->header_len);
1087 mmd->current_chunk++;
1091 static void vss_post_select(struct sched *s, struct task *t)
1094 struct vss_task *vsst = container_of(t, struct vss_task, task);
1097 if (mmd->sender_cmd_data.cmd_num >= 0) {
1098 int num = mmd->sender_cmd_data.cmd_num,
1099 sender_num = mmd->sender_cmd_data.sender_num;
1101 if (senders[sender_num].client_cmds[num]) {
1102 ret = senders[sender_num].client_cmds[num]
1103 (&mmd->sender_cmd_data);
1105 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1107 mmd->sender_cmd_data.cmd_num = -1;
1109 if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE)
1110 recv_afs_result(vsst, &s->rfds);
1111 else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
1112 PARA_NOTICE_LOG("requesting new fd from afs\n");
1113 ret = send_buffer(vsst->afs_socket, "new");
1115 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
1117 vsst->afsss = AFS_SOCKET_AFD_PENDING;
1119 for (i = 0; senders[i].name; i++) {
1120 if (!senders[i].post_select)
1122 senders[i].post_select(&s->rfds, &s->wfds);
1124 if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
1125 (vss_next() && vss_playing()))
1126 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
1131 * Initialize the virtual streaming system task.
1133 * \param afs_socket The fd for communication with afs.
1135 * This also initializes all supported senders and starts streaming
1136 * if the --autoplay command line flag was given.
1138 void init_vss_task(int afs_socket)
1140 static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
1142 char *hn = para_hostname(), *home = para_homedir();
1143 long unsigned announce_time = conf.announce_time_arg > 0?
1144 conf.announce_time_arg : 300,
1145 autoplay_delay = conf.autoplay_delay_arg > 0?
1146 conf.autoplay_delay_arg : 0;
1147 vsst->header_interval.tv_sec = 5; /* should this be configurable? */
1148 vsst->afs_socket = afs_socket;
1149 vsst->task.pre_select = vss_pre_select;
1150 vsst->task.post_select = vss_post_select;
1151 ms2tv(announce_time, &vsst->announce_tv);
1152 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
1153 INIT_LIST_HEAD(&fec_client_list);
1154 for (i = 0; senders[i].name; i++) {
1155 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
1156 senders[i].init(&senders[i]);
1160 mmd->sender_cmd_data.cmd_num = -1;
1161 if (conf.autoplay_given) {
1163 mmd->vss_status_flags |= VSS_PLAYING;
1164 mmd->new_vss_status_flags |= VSS_PLAYING;
1165 ms2tv(autoplay_delay, &tmp);
1166 tv_add(now, &tmp, &vsst->autoplay_barrier);
1167 tv_add(&vsst->autoplay_barrier, &vsst->announce_tv,
1168 &vsst->data_send_barrier);
1170 register_task(&vsst->task);