2 * Copyright (C) 1997-2009 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. */
89 const char *header_buf;
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 group duration divided by the number of slices. */
127 struct timeval slice_duration;
128 /** Group contains the audio file header that occupies that many slices. */
129 uint8_t num_header_slices;
133 * Describes one connected FEC client.
136 /** If negative, this client is temporarily disabled. */
138 /** Parameters requested by the client. */
139 struct fec_client_parms *fcp;
140 /** Used by the core FEC code. */
141 struct fec_parms *parms;
142 /** The position of this client in the fec client list. */
143 struct list_head node;
144 /** When the first slice for this client was sent. */
145 struct timeval stream_start;
146 /** The first chunk sent to this FEC client. */
147 int first_stream_chunk;
148 /** Describes the current group. */
149 struct fec_group group;
150 /** The current slice. */
151 uint8_t current_slice_num;
152 /** The data to be FEC-encoded (point to a region within the mapped audio file). */
153 const unsigned char **src_data;
154 /** Last time an audio header was sent. */
155 struct timeval next_header_time;
156 /** Used for the last source pointer of an audio file. */
157 unsigned char *extra_src_buf;
158 /** Extra slices needed to store largest chunk + header. */
159 int num_extra_slices;
160 /** Contains the FEC-encoded data. */
161 unsigned char *enc_buf;
165 * Get the chunk time of the current audio file.
167 * \return A pointer to a struct containing the chunk time, or NULL,
168 * if currently no audio file is selected.
170 struct timeval *vss_chunk_time(void)
172 if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
173 mmd->afd.afhi.chunk_tv.tv_usec == 0)
175 return &mmd->afd.afhi.chunk_tv;
179 * Write a fec header to a buffer.
181 * \param buf The buffer to write to.
182 * \param h The fec header to write.
184 static void write_fec_header(struct fec_client *fc, struct vss_task *vsst)
186 char *buf = (char *)fc->enc_buf;
187 struct fec_group *g = &fc->group;
188 struct fec_client_parms *p = fc->fcp;
190 write_u32(buf, FEC_MAGIC);
192 write_u8(buf + 4, p->slices_per_group + fc->num_extra_slices);
193 write_u8(buf + 5, p->data_slices_per_group + fc->num_extra_slices);
194 write_u32(buf + 6, g->num_header_slices? vsst->header_len : 0);
196 write_u32(buf + 10, g->num);
197 write_u32(buf + 14, g->bytes);
199 write_u8(buf + 18, fc->current_slice_num);
200 write_u16(buf + 20, p->max_slice_bytes - FEC_HEADER_SIZE);
201 write_u8(buf + 22, g->first_chunk? 0 : 1);
202 write_u8(buf + 23, vsst->header_len? 1 : 0);
203 memset(buf + 24, 0, 7);
206 static int need_audio_header(struct fec_client *fc, struct vss_task *vsst)
208 if (!mmd->current_chunk) {
209 tv_add(now, &vsst->header_interval, &fc->next_header_time);
212 if (!vsst->header_buf)
214 if (!vsst->header_len)
216 if (fc->group.num && tv_diff(&fc->next_header_time, now, NULL) > 0)
218 tv_add(now, &vsst->header_interval, &fc->next_header_time);
222 static int num_slices(long unsigned bytes, struct fec_client *fc, uint8_t *result)
224 unsigned long m = fc->fcp->max_slice_bytes - FEC_HEADER_SIZE;
225 unsigned rv, redundant_slices = fc->fcp->slices_per_group
226 - fc->fcp->data_slices_per_group;
230 rv = (bytes + m - 1) / m;
231 if (rv + redundant_slices > 255)
237 static int setup_next_fec_group(struct fec_client *fc, struct vss_task *vsst)
239 int ret, i, k, data_slices;
241 const char *buf, *start_buf;
242 struct timeval tmp, *chunk_tv = vss_chunk_time();
243 struct fec_group *g = &fc->group;
244 unsigned slice_bytes = fc->fcp->max_slice_bytes - FEC_HEADER_SIZE;
245 uint32_t max_data_size;
248 k = fc->fcp->data_slices_per_group + fc->num_extra_slices;
249 if (fc->first_stream_chunk < 0) {
250 uint32_t largest = afh_get_largest_chunk_size(&mmd->afd.afhi)
252 uint8_t needed, want;
254 ret = num_slices(largest, fc, &needed);
257 if (needed > fc->fcp->data_slices_per_group)
258 PARA_WARNING_LOG("fec parms insufficient for this audio file\n");
259 want = PARA_MAX(needed, fc->fcp->data_slices_per_group);
262 fc->src_data = para_realloc(fc->src_data, want * sizeof(char *));
263 ret = fec_new(want, want + fc->fcp->slices_per_group
264 - fc->fcp->data_slices_per_group, &fc->parms);
268 fc->num_extra_slices = 0;
269 if (k > fc->fcp->data_slices_per_group) {
270 fc->num_extra_slices = k - fc->fcp->data_slices_per_group;
271 PARA_NOTICE_LOG("using %d extra slices\n",
272 fc->num_extra_slices);
275 fc->stream_start = *now;
276 fc->first_stream_chunk = mmd->current_chunk;
277 g->first_chunk = mmd->current_chunk;
280 g->first_chunk += g->num_chunks;
283 if (g->first_chunk >= mmd->afd.afhi.chunks_total)
285 if (need_audio_header(fc, vsst)) {
286 ret = num_slices(vsst->header_len, fc, &g->num_header_slices);
290 g->num_header_slices = 0;
291 afh_get_chunk(g->first_chunk, &mmd->afd.afhi, vsst->map, &start_buf,
293 data_slices = k - g->num_header_slices;
295 max_data_size = slice_bytes * data_slices;
297 for (i = g->first_chunk; i < mmd->afd.afhi.chunks_total; i++) {
298 afh_get_chunk(i, &mmd->afd.afhi, vsst->map, &buf, &len);
299 if (g->bytes + len > max_data_size)
303 g->num_chunks = i - g->first_chunk;
304 assert(g->num_chunks);
305 fc->current_slice_num = 0;
307 /* setup header slices */
308 buf = vsst->header_buf;
309 for (i = 0; i < g->num_header_slices; i++) {
310 fc->src_data[i] = (const unsigned char *)buf;
314 /* setup data slices */
316 for (i = g->num_header_slices; i < k; i++) {
317 if (buf + slice_bytes > vsst->map + mmd->size)
319 * Can not use the memory mapped audio file for this
320 * slice as it goes beyond the map. This slice will not
324 fc->src_data[i] = (const unsigned char *)buf;
328 uint32_t payload_size = vsst->map + mmd->size - buf;
329 memcpy(fc->extra_src_buf, buf, payload_size);
330 fc->src_data[i] = fc->extra_src_buf;
332 /* use arbitrary data for all remaining slices */
335 fc->src_data[i] = (const unsigned char *)buf;
338 /* setup group timing */
339 tv_scale(g->first_chunk - fc->first_stream_chunk, chunk_tv, &tmp);
340 tv_add(&fc->stream_start, &tmp, &g->start);
341 if (g->num) /* quick hack to avoid buffer underruns */
343 tv_scale(g->num_chunks, chunk_tv, &tmp); /* group duration */
344 tv_divide(fc->fcp->slices_per_group + fc->num_extra_slices,
345 &tmp, &g->slice_duration);
347 PARA_DEBUG_LOG("FEC group %d: %d chunks (%d - %d), %d header slices, %d data slices\n",
348 g->num, g->num_chunks, g->first_chunk,
349 g->first_chunk + g->num_chunks - 1,
350 g->num_header_slices, data_slices
352 PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
353 tv2ms(&tmp), tv2ms(chunk_tv), tv2ms(&g->slice_duration));
357 static int compute_next_fec_slice(struct fec_client *fc, struct vss_task *vsst)
359 assert(fc->error >= 0);
360 if (fc->first_stream_chunk < 0 || fc->current_slice_num
361 == fc->fcp->slices_per_group + fc->num_extra_slices) {
362 int ret = setup_next_fec_group(fc, vsst);
366 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
367 PARA_ERROR_LOG("FEC client temporarily disabled\n");
372 write_fec_header(fc, vsst);
373 fec_encode(fc->parms, fc->src_data, fc->enc_buf + FEC_HEADER_SIZE,
374 fc->current_slice_num,
375 fc->fcp->max_slice_bytes - FEC_HEADER_SIZE);
380 * Return a buffer that marks the end of the stream.
382 * \param buf Result pointer.
383 * \return The length of the eof buffer.
385 * This is used for (multicast) udp streaming where closing the socket on the
386 * sender might not give rise to an eof condition at the peer.
388 size_t vss_get_fec_eof_packet(const char **buf)
390 static const char fec_eof_packet[FEC_HEADER_SIZE] = FEC_EOF_PACKET;
391 *buf = fec_eof_packet;
392 return FEC_HEADER_SIZE;
396 * Add one entry to the list of active fec clients.
398 * \param fcp Describes the fec parameters to be used for this client.
399 * \param result An opaque pointer that must be used by remove the client later.
403 int vss_add_fec_client(struct fec_client_parms *fcp, struct fec_client **result)
406 struct fec_client *fc;
408 if (fcp->max_slice_bytes < FEC_HEADER_SIZE + fcp->data_slices_per_group)
409 return -ERRNO_TO_PARA_ERROR(EINVAL);
410 fc = para_calloc(sizeof(*fc));
412 ret = fec_new(fcp->data_slices_per_group, fcp->slices_per_group,
416 fc->first_stream_chunk = -1; /* stream not yet started */
417 fc->src_data = para_malloc(fc->fcp->slices_per_group * sizeof(char *));
418 fc->enc_buf = para_calloc(fc->fcp->max_slice_bytes);
419 fc->num_extra_slices = 0;
420 fc->extra_src_buf = para_calloc(fc->fcp->max_slice_bytes);
421 fc->next_header_time.tv_sec = 0;
422 para_list_add(&fc->node, &fec_client_list);
433 * Remove one entry from the list of active fec clients.
435 * \param fc The client to be removed.
437 void vss_del_fec_client(struct fec_client *fc)
442 free(fc->extra_src_buf);
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
451 * next = stream_start + (first_group_chunk - first_stream_chunk)
452 * * chunk_time + slice_num * slice_time
454 static int next_slice_is_due(struct fec_client *fc, struct timeval *diff)
456 struct timeval tmp, next;
459 if (fc->first_stream_chunk < 0)
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;
467 static void compute_slice_timeout(struct timeval *timeout)
469 struct fec_client *fc;
471 assert(vss_playing());
472 list_for_each_entry(fc, &fec_client_list, node) {
477 if (next_slice_is_due(fc, &diff)) {
479 timeout->tv_usec = 0;
482 /* timeout = min(timeout, diff) */
483 if (tv_diff(&diff, timeout, NULL) < 0)
488 static void set_eof_barrier(struct vss_task *vsst)
490 struct fec_client *fc;
491 struct timeval timeout = {1, 0}, *chunk_tv = vss_chunk_time();
495 list_for_each_entry(fc, &fec_client_list, node) {
496 struct timeval group_duration;
500 tv_scale(fc->group.num_chunks, chunk_tv, &group_duration);
501 if (tv_diff(&timeout, &group_duration, NULL) < 0)
502 timeout = group_duration;
505 tv_add(now, &timeout, &vsst->eof_barrier);
509 * Check if vss status flag \a P (playing) is set.
511 * \return Greater than zero if playing, zero otherwise.
514 unsigned int vss_playing(void)
516 return mmd->new_vss_status_flags & VSS_PLAYING;
520 * Check if the \a N (next) status flag is set.
522 * \return Greater than zero if set, zero if not.
525 unsigned int vss_next(void)
527 return mmd->new_vss_status_flags & VSS_NEXT;
531 * Check if a reposition request is pending.
533 * \return Greater than zero if true, zero otherwise.
536 unsigned int vss_repos(void)
538 return mmd->new_vss_status_flags & VSS_REPOS;
542 * Check if the vss is currently paused.
544 * \return Greater than zero if paused, zero otherwise.
547 unsigned int vss_paused(void)
549 return !(mmd->new_vss_status_flags & VSS_NEXT)
550 && !(mmd->new_vss_status_flags & VSS_PLAYING);
554 * Check if the vss is currently stopped.
556 * \return Greater than zero if paused, zero otherwise.
559 unsigned int vss_stopped(void)
561 return (mmd->new_vss_status_flags & VSS_NEXT)
562 && !(mmd->new_vss_status_flags & VSS_PLAYING);
565 static int chk_barrier(const char *bname, const struct timeval *barrier,
566 struct timeval *diff, int print_log)
570 if (tv_diff(now, barrier, diff) > 0)
574 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
579 * != NULL: timeout for next chunk
580 * NULL: nothing to do
582 static struct timeval *vss_compute_timeout(struct vss_task *vsst)
584 static struct timeval the_timeout;
585 struct timeval next_chunk;
587 if (vss_next() && vsst->map) {
588 /* only sleep a bit, nec*/
589 the_timeout.tv_sec = 0;
590 the_timeout.tv_usec = 100;
593 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
594 &the_timeout, 1) < 0)
596 if (chk_barrier("eof", &vsst->eof_barrier, &the_timeout, 1) < 0)
598 if (chk_barrier("data send", &vsst->data_send_barrier,
599 &the_timeout, 1) < 0)
601 if (!vss_playing() || !vsst->map)
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;
611 /* compute min of current timeout and next slice time */
612 compute_slice_timeout(&the_timeout);
616 static void vss_eof(struct vss_task *vsst)
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);
626 mmd->chunks_sent = 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;
639 * Get the list of all supported audio formats.
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.
645 const char *supported_audio_formats(void)
647 return SUPPORTED_AUDIO_FORMATS;
650 static int need_to_request_new_audio_file(struct vss_task *vsst)
654 if (vsst->map) /* have audio file */
656 if (!vss_playing()) /* don't need one */
658 if (mmd->new_vss_status_flags & VSS_NOMORE)
660 if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
662 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
669 * Compute the timeout for the main select-loop of the scheduler.
671 * \param s Pointer to the server scheduler.
672 * \param t Pointer to the vss task structure.
674 * Before the timeout is computed, the current vss status flags are evaluated
675 * and acted upon by calling appropriate functions from the lower layers.
676 * Possible actions include
678 * - request a new audio file from afs,
679 * - shutdown of all senders (stop/pause command),
680 * - reposition the stream (ff/jmp command).
682 static void vss_pre_select(struct sched *s, struct task *t)
685 struct timeval *tv, diff;
686 struct vss_task *vsst = container_of(t, struct vss_task, task);
688 if (!vsst->map || vss_next() || vss_paused() || vss_repos()) {
689 struct fec_client *fc, *tmp;
690 for (i = 0; senders[i].name; i++)
691 if (senders[i].shutdown_clients)
692 senders[i].shutdown_clients();
693 list_for_each_entry_safe(fc, tmp, &fec_client_list, node) {
694 fc->first_stream_chunk = -1;
697 mmd->stream_start.tv_sec = 0;
698 mmd->stream_start.tv_usec = 0;
702 else if (vss_paused()) {
703 if (mmd->chunks_sent)
704 set_eof_barrier(vsst);
705 mmd->chunks_sent = 0;
706 } else if (vss_repos()) {
707 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
708 set_eof_barrier(vsst);
709 mmd->chunks_sent = 0;
710 mmd->current_chunk = mmd->repos_request;
711 mmd->new_vss_status_flags &= ~VSS_REPOS;
713 if (need_to_request_new_audio_file(vsst)) {
714 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
715 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
716 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
718 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
719 for (i = 0; senders[i].name; i++) {
720 if (!senders[i].pre_select)
722 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
724 tv = vss_compute_timeout(vsst);
725 if (tv && tv_diff(tv, &s->timeout, &diff) < 0)
729 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
731 char control[255], buf[8];
732 struct msghdr msg = {.msg_iov = NULL};
733 struct cmsghdr *cmsg;
739 iov.iov_len = sizeof(buf);
742 msg.msg_control = control;
743 msg.msg_controllen = sizeof(control);
744 memset(buf, 0, sizeof(buf));
745 ret = recvmsg(afs_socket, &msg, 0);
747 return -ERRNO_TO_PARA_ERROR(errno);
748 if (iov.iov_len != sizeof(buf))
749 return -E_AFS_SHORT_READ;
750 *code = *(uint32_t*)buf;
751 *data = *(uint32_t*)(buf + 4);
752 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
753 if (cmsg->cmsg_level != SOL_SOCKET
754 || cmsg->cmsg_type != SCM_RIGHTS)
756 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
758 *fd = *(int *)CMSG_DATA(cmsg);
763 static void recv_afs_result(struct vss_task *vsst)
765 int ret, passed_fd, shmid;
766 uint32_t afs_code = 0, afs_data = 0;
769 vsst->afsss = AFS_SOCKET_READY;
770 ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
773 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
776 if (afs_code != NEXT_AUDIO_FILE)
781 ret = load_afd(shmid, &mmd->afd);
785 ret = fstat(passed_fd, &statbuf);
787 PARA_ERROR_LOG("fstat error:\n");
788 ret = -ERRNO_TO_PARA_ERROR(errno);
791 mmd->size = statbuf.st_size;
792 mmd->mtime = statbuf.st_mtime;
793 ret = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE, passed_fd,
798 mmd->chunks_sent = 0;
799 mmd->current_chunk = 0;
803 mmd->new_vss_status_flags &= (~VSS_NEXT);
804 afh_get_header(&mmd->afd.afhi, vsst->map, &vsst->header_buf,
808 free(mmd->afd.afhi.chunk_table);
811 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
812 mmd->new_vss_status_flags = VSS_NEXT;
816 * Main sending function.
818 * This function gets called from vss_post_select(). It checks whether the next
819 * chunk of data should be pushed out. It obtains a pointer to the data to be
820 * sent out as well as its length from mmd->afd.afhi. This information is then
821 * passed to each supported sender's send() function as well as to the send()
822 * functions of each registered fec client.
824 static void vss_send(struct vss_task *vsst)
826 int i, sent_something = 0;
828 struct fec_client *fc, *tmp_fc;
830 if (!vsst->map || !vss_playing())
832 if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
834 if (chk_barrier("data send", &vsst->data_send_barrier,
837 list_for_each_entry_safe(fc, tmp_fc, &fec_client_list, node) {
840 if (!next_slice_is_due(fc, NULL))
842 if (compute_next_fec_slice(fc, vsst) <= 0)
844 PARA_DEBUG_LOG("sending %d:%d (%u bytes)\n", fc->group.num,
845 fc->current_slice_num, fc->fcp->max_slice_bytes);
846 fc->fcp->send((char *)fc->enc_buf,
847 fc->fcp->max_slice_bytes,
848 fc->fcp->private_data);
849 fc->current_slice_num++;
852 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
854 mmd->new_vss_status_flags |= VSS_NEXT;
857 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
858 &mmd->stream_start, &due);
859 if (tv_diff(&due, now, NULL) <= 0) {
863 if (!mmd->chunks_sent) {
865 mmd->stream_start = *now;
866 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
867 mmd->offset = tv2ms(&tmp);
871 * We call the send function also in case of empty chunks as
872 * they might have still some data queued which can be sent in
875 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map,
877 for (i = 0; senders[i].name; i++) {
878 if (!senders[i].send)
880 senders[i].send(mmd->current_chunk, mmd->chunks_sent,
881 buf, len, vsst->header_buf, vsst->header_len);
884 mmd->current_chunk++;
888 static void vss_post_select(struct sched *s, struct task *t)
891 struct vss_task *vsst = container_of(t, struct vss_task, task);
894 if (mmd->sender_cmd_data.cmd_num >= 0) {
895 int num = mmd->sender_cmd_data.cmd_num,
896 sender_num = mmd->sender_cmd_data.sender_num;
898 if (senders[sender_num].client_cmds[num])
899 senders[sender_num].client_cmds[num](&mmd->sender_cmd_data);
900 mmd->sender_cmd_data.cmd_num = -1;
902 if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
903 if (FD_ISSET(vsst->afs_socket, &s->rfds))
904 recv_afs_result(vsst);
905 } else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
906 PARA_NOTICE_LOG("requesting new fd from afs\n");
907 ret = send_buffer(vsst->afs_socket, "new");
909 PARA_CRIT_LOG("%s\n", para_strerror(-ret));
911 vsst->afsss = AFS_SOCKET_AFD_PENDING;
913 for (i = 0; senders[i].name; i++) {
914 if (!senders[i].post_select)
916 senders[i].post_select(&s->rfds, &s->wfds);
918 if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
919 (vss_next() && vss_playing()))
920 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
925 * Initialize the virtual streaming system task.
927 * \param afs_socket The fd for communication with afs.
929 * This also initializes all supported senders and starts streaming
930 * if the --autoplay command line flag was given.
932 void init_vss_task(int afs_socket)
934 static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
936 char *hn = para_hostname(), *home = para_homedir();
937 long unsigned announce_time = conf.announce_time_arg > 0?
938 conf.announce_time_arg : 300,
939 autoplay_delay = conf.autoplay_delay_arg > 0?
940 conf.autoplay_delay_arg : 0;
941 vsst->header_interval.tv_sec = 5; /* should this be configurable? */
942 vsst->afs_socket = afs_socket;
943 vsst->task.pre_select = vss_pre_select;
944 vsst->task.post_select = vss_post_select;
945 ms2tv(announce_time, &vsst->announce_tv);
946 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
947 INIT_LIST_HEAD(&fec_client_list);
948 for (i = 0; senders[i].name; i++) {
949 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
950 senders[i].init(&senders[i]);
954 mmd->sender_cmd_data.cmd_num = -1;
955 if (conf.autoplay_given) {
957 mmd->vss_status_flags |= VSS_PLAYING;
958 mmd->new_vss_status_flags |= VSS_PLAYING;
959 ms2tv(autoplay_delay, &tmp);
960 tv_add(now, &tmp, &vsst->autoplay_barrier);
961 tv_add(&vsst->autoplay_barrier, &vsst->announce_tv,
962 &vsst->data_send_barrier);
964 register_task(&vsst->task);