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
18 #include "portable_io.h"
25 #include "server.cmdline.h"
33 extern struct misc_meta_data
*mmd
;
35 extern void dccp_send_init(struct sender
*);
36 extern void http_send_init(struct sender
*);
37 extern void udp_send_init(struct sender
*);
39 /** The list of supported senders. */
40 struct sender senders
[] = {
43 .init
= http_send_init
,
47 .init
= dccp_send_init
,
51 .init
= udp_send_init
,
58 /** The possible states of the afs socket. */
59 enum afs_socket_status
{
60 /** Socket is inactive. */
62 /** Socket fd was included in the write fd set for select(). */
63 AFS_SOCKET_CHECK_FOR_WRITE
,
64 /** vss wrote a request to the socket and waits for reply from afs. */
65 AFS_SOCKET_AFD_PENDING
68 /** The task structure for the virtual streaming system. */
70 /** Copied from the -announce_time command line option. */
71 struct timeval announce_tv
;
72 /** End of the announcing interval. */
73 struct timeval data_send_barrier
;
74 /** End of the EOF interval. */
75 struct timeval eof_barrier
;
76 /** Only used if --autoplay_delay was given. */
77 struct timeval autoplay_barrier
;
78 /** Used for afs-server communication. */
80 /** The current state of \a afs_socket. */
81 enum afs_socket_status afsss
;
82 /** The memory mapped audio file. */
84 /** Used by the scheduler. */
86 /** Pointer to the header of the mapped audio file. */
87 const char *header_buf
;
88 /** Length of the audio file header. */
93 * The list of currently connected fec clients.
95 * Senders may use \ref vss_add_fec_client() to add entries to the list.
97 static struct list_head fec_client_list
;
100 * Describes one slice of a FEC group.
102 * FEC slices directly correspond to the data packages sent by the paraslash
103 * senders that use FEC. Each slice is identified by its group number and its
104 * number within the group. All slices have the same size, but the last slice
105 * of the group may not be filled entirely.
108 /** The slice number within the FEC group. */
110 /** The number of used bytes in this slice. */
115 * Data associated with one FEC group.
117 * A FEC group consists of a fixed number of slices and this number is given by
118 * the \a slices_per_group parameter of struct \ref fec_client_parms. Each FEC
119 * group contains a number of chunks of the current audio file.
122 /** The number of the FEC group. */
124 /** Number of bytes in this group. */
126 /** The first chunk of the current audio file belonging to the group. */
127 uint32_t first_chunk
;
128 /** The number of chunks contained in this group. */
130 /** The time needed to play all chunks of the group. */
131 struct timeval duration
;
132 /** When the first chunk was sent. */
133 struct timeval start
;
134 /** \a The group duration divided by \a slices_per_group. */
135 struct timeval slice_duration
;
139 * Describes one connected FEC client.
142 /** Parameters requested by the client. */
143 struct fec_client_parms
*fcp
;
144 /** Used by the core FEC code. */
145 struct fec_parms
*parms
;
146 /** The position of this client in 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 /** Describes the current slice. */
155 struct fec_slice slice
;
156 /** The data to be FEC-encoded (point to a region within the mapped audio file). */
157 const unsigned char **src_data
;
158 /** Used for the last source pointer of the last group. */
159 unsigned char *extra_src_buf
;
160 /** The size of the buffer for the extra source pointer. */
161 size_t extra_src_buf_size
;
162 /** Contains FEC-encoded data. */
163 unsigned char *enc_buf
;
164 /** Size of \a enc_buf. */
169 * Get the chunk time of the current audio file.
171 * \return A pointer to a struct containing the chunk time, or NULL,
172 * if currently no audio file is selected.
174 struct timeval
*vss_chunk_time(void)
176 if (mmd
->afd
.afhi
.chunk_tv
.tv_sec
== 0 &&
177 mmd
->afd
.afhi
.chunk_tv
.tv_usec
== 0)
179 return &mmd
->afd
.afhi
.chunk_tv
;
182 static void setup_fec_group(struct fec_client
*fc
, struct vss_task
*vsst
)
184 uint32_t num_bytes
= 0, chunk_num
, max_group_size
, last_payload_size
;
185 int i
, k
= fc
->fcp
->data_slices_per_group
;
186 const char *start_buf
= NULL
;
187 struct timeval tmp
, *chunk_tv
= vss_chunk_time();
190 max_group_size
= (fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
) * k
;
191 chunk_num
= fc
->group
.first_chunk
;
196 if (chunk_num
>= mmd
->afd
.afhi
.chunks_total
)
198 afh_get_chunk(chunk_num
, &mmd
->afd
.afhi
, vsst
->map
, &buf
, &len
);
201 if (num_bytes
+ len
> max_group_size
)
207 fc
->group
.num_chunks
= chunk_num
- fc
->group
.first_chunk
;
209 fc
->group
.bytes
= num_bytes
;
211 fc
->slice
.bytes
= ROUND_UP(num_bytes
, k
) / k
;
213 /* The last slice will not be fully used */
214 last_payload_size
= num_bytes
% fc
->slice
.bytes
;
215 if (!last_payload_size
)
216 last_payload_size
= fc
->slice
.bytes
;
218 tv_scale(fc
->group
.num_chunks
, chunk_tv
, &fc
->group
.duration
);
219 tv_scale(fc
->group
.first_chunk
- fc
->first_stream_chunk
, chunk_tv
,
221 tv_add(&fc
->stream_start
, &tmp
, &fc
->group
.start
);
222 tv_divide(fc
->fcp
->slices_per_group
, &fc
->group
.duration
,
223 &fc
->group
.slice_duration
);
225 for (i
= 0; i
< k
; i
++)
226 fc
->src_data
[i
] = (const unsigned char *)start_buf
227 + i
* fc
->slice
.bytes
;
229 if (start_buf
+ k
* fc
->slice
.bytes
> vsst
->map
+ mmd
->size
) {
230 /* can not use last slice as it goes beyond the map */
231 if (fc
->extra_src_buf_size
< fc
->slice
.bytes
)
232 fc
->extra_src_buf
= para_realloc(fc
->extra_src_buf
, fc
->slice
.bytes
);
233 memcpy(fc
->extra_src_buf
, start_buf
+ (k
- 1) * fc
->slice
.bytes
,
235 memset(fc
->extra_src_buf
+ last_payload_size
, 0,
236 fc
->slice
.bytes
- last_payload_size
);
237 fc
->src_data
[k
- 1] = fc
->extra_src_buf
;
243 * Write a fec header to a buffer.
245 * \param buf The buffer to write to.
246 * \param h The fec header to write.
248 static void write_fec_header(struct fec_client
*fc
)
250 char *buf
= (char *)fc
->enc_buf
;
252 write_u32(buf
, FEC_MAGIC
);
254 write_u8(buf
+ 4, fc
->fcp
->slices_per_group
);
255 write_u8(buf
+ 5, fc
->fcp
->data_slices_per_group
);
256 write_u32(buf
+ 6, (uint32_t)0); /* audio header len */
258 write_u32(buf
+ 10, fc
->group
.num
);
259 write_u32(buf
+ 14, fc
->group
.bytes
);
261 write_u8(buf
+ 18, fc
->slice
.num
);
262 write_u16(buf
+ 20, fc
->slice
.bytes
);
263 memset(buf
+ 22, 0, 11);
267 * Return a buffer that marks the end of the stream.
269 * \param buf Result pointer.
270 * \return The length of the eof buffer.
272 * This is used for (multicast) udp streaming where closing the socket on the
273 * sender might not give rise to an eof condition at the peer.
275 size_t vss_get_fec_eof_packet(const char **buf
)
277 static const char fec_eof_packet
[FEC_HEADER_SIZE
] =
278 "\xec\x0d\xcc\xfe\0\0\0\0"
282 *buf
= fec_eof_packet
;
283 return FEC_HEADER_SIZE
;
286 static void compute_next_fec_slice(struct fec_client
*fc
, struct vss_task
*vsst
)
288 if (fc
->first_stream_chunk
< 0) {
289 fc
->stream_start
= *now
;
290 fc
->first_stream_chunk
= mmd
->current_chunk
;
291 fc
->group
.first_chunk
= mmd
->current_chunk
;
293 setup_fec_group(fc
, vsst
);
294 } else if (fc
->slice
.num
== fc
->fcp
->slices_per_group
) {
295 fc
->group
.first_chunk
+= fc
->group
.num_chunks
;
296 setup_fec_group(fc
, vsst
);
299 if (fc
->enc_buf_size
< fc
->slice
.bytes
+ FEC_HEADER_SIZE
) {
300 fc
->enc_buf_size
= fc
->slice
.bytes
+ FEC_HEADER_SIZE
;
301 fc
->enc_buf
= para_realloc(fc
->enc_buf
, fc
->enc_buf_size
);
303 write_fec_header(fc
);
304 fec_encode(fc
->parms
, fc
->src_data
, fc
->enc_buf
+ FEC_HEADER_SIZE
,
305 fc
->slice
.num
, fc
->slice
.bytes
);
309 * Add one entry to the list of active fec clients.
311 * \param fcp Describes the fec parameters to be used for this client.
312 * \param result An opaque pointer that must be used by remove the client later.
316 int vss_add_fec_client(struct fec_client_parms
*fcp
, struct fec_client
**result
)
319 struct fec_client
*fc
;
321 if (fcp
->max_slice_bytes
< FEC_HEADER_SIZE
+ fcp
->data_slices_per_group
)
322 return -ERRNO_TO_PARA_ERROR(EINVAL
);
323 fc
= para_calloc(sizeof(*fc
));
325 ret
= fec_new(fcp
->data_slices_per_group
, fcp
->slices_per_group
,
329 fc
->first_stream_chunk
= -1; /* stream not yet started */
330 fc
->src_data
= para_malloc(fc
->fcp
->slices_per_group
* sizeof(char *));
331 para_list_add(&fc
->node
, &fec_client_list
);
342 * Remove one entry from the list of active fec clients.
344 * \param fc The client to be removed.
346 void vss_del_fec_client(struct fec_client
*fc
)
351 free(fc
->extra_src_buf
);
357 * Compute if/when next slice is due. If it isn't due yet and \a diff is
358 * not \p Null, compute the time difference next - now, where
360 * next = stream_start + (first_group_chunk - first_stream_chunk)
361 * * chunk_time + slice_num * slice_time
363 static int next_slice_is_due(struct fec_client
*fc
, struct timeval
*diff
)
365 struct timeval tmp
, next
;
368 if (fc
->first_stream_chunk
< 0)
370 tv_scale(fc
->slice
.num
, &fc
->group
.slice_duration
, &tmp
);
371 tv_add(&tmp
, &fc
->group
.start
, &next
);
372 ret
= tv_diff(&next
, now
, diff
);
373 return ret
< 0? 1 : 0;
376 static void compute_slice_timeout(struct timeval
*timeout
)
378 struct fec_client
*fc
;
380 assert(vss_playing());
381 list_for_each_entry(fc
, &fec_client_list
, node
) {
384 if (next_slice_is_due(fc
, &diff
)) {
386 timeout
->tv_usec
= 0;
389 /* timeout = min(timeout, diff) */
390 if (tv_diff(&diff
, timeout
, NULL
) < 0)
396 * Check if vss status flag \a P (playing) is set.
398 * \return Greater than zero if playing, zero otherwise.
401 unsigned int vss_playing(void)
403 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
407 * Check if the \a N (next) status flag is set.
409 * \return Greater than zero if set, zero if not.
412 unsigned int vss_next(void)
414 return mmd
->new_vss_status_flags
& VSS_NEXT
;
418 * Check if a reposition request is pending.
420 * \return Greater than zero if true, zero otherwise.
423 unsigned int vss_repos(void)
425 return mmd
->new_vss_status_flags
& VSS_REPOS
;
429 * Check if the vss is currently paused.
431 * \return Greater than zero if paused, zero otherwise.
434 unsigned int vss_paused(void)
436 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
437 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
441 * Check if the vss is currently stopped.
443 * \return Greater than zero if paused, zero otherwise.
446 unsigned int vss_stopped(void)
448 return (mmd
->new_vss_status_flags
& VSS_NEXT
)
449 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
452 static int chk_barrier(const char *bname
, const struct timeval
*barrier
,
453 struct timeval
*diff
, int print_log
)
457 if (tv_diff(now
, barrier
, diff
) > 0)
461 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
466 * != NULL: timeout for next chunk
467 * NULL: nothing to do
469 static struct timeval
*vss_compute_timeout(struct vss_task
*vsst
)
471 static struct timeval the_timeout
;
472 struct timeval next_chunk
;
474 if (vss_next() && vsst
->map
) {
475 /* only sleep a bit, nec*/
476 the_timeout
.tv_sec
= 0;
477 the_timeout
.tv_usec
= 100;
480 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
481 &the_timeout
, 1) < 0)
483 if (chk_barrier("eof", &vsst
->eof_barrier
, &the_timeout
, 1) < 0)
485 if (chk_barrier("data send", &vsst
->data_send_barrier
,
486 &the_timeout
, 1) < 0)
488 if (!vss_playing() || !vsst
->map
)
490 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
491 &mmd
->stream_start
, &next_chunk
);
492 if (chk_barrier("chunk", &next_chunk
, &the_timeout
, 0) >= 0) {
493 /* chunk is due or bof */
494 the_timeout
.tv_sec
= 0;
495 the_timeout
.tv_usec
= 0;
498 /* compute min of current timeout and next slice time */
499 compute_slice_timeout(&the_timeout
);
503 static void vss_eof(struct vss_task
*vsst
)
506 mmd
->stream_start
= *now
;
509 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
510 mmd
->new_vss_status_flags
= VSS_NEXT
;
511 tv_add(&mmd
->afd
.afhi
.eof_tv
, now
, &vsst
->eof_barrier
);
512 para_munmap(vsst
->map
, mmd
->size
);
514 mmd
->chunks_sent
= 0;
516 mmd
->afd
.afhi
.seconds_total
= 0;
517 mmd
->afd
.afhi
.chunk_tv
.tv_sec
= 0;
518 mmd
->afd
.afhi
.chunk_tv
.tv_usec
= 0;
519 free(mmd
->afd
.afhi
.chunk_table
);
520 mmd
->afd
.afhi
.chunk_table
= NULL
;
521 free(mmd
->afd
.afhi
.info_string
);
522 mmd
->afd
.afhi
.info_string
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_FILE_INFO
],
523 status_item_list
[SI_TAGINFO1
], status_item_list
[SI_TAGINFO2
]);
524 make_empty_status_items(mmd
->afd
.verbose_ls_output
);
531 * Get the list of all supported audio formats.
533 * \return Aa space separated list of all supported audio formats
534 * It is not allocated at runtime, i.e. there is no need to free
535 * the returned string in the caller.
537 const char *supported_audio_formats(void)
539 return SUPPORTED_AUDIO_FORMATS
;
542 static int need_to_request_new_audio_file(struct vss_task
*vsst
)
546 if (vsst
->map
) /* have audio file */
548 if (!vss_playing()) /* don't need one */
550 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
552 if (vsst
->afsss
== AFS_SOCKET_AFD_PENDING
) /* already requested one */
554 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
563 * Compute the timeout for para_server's main select-loop.
565 * This function gets called from para_server to determine the timeout value
566 * for its main select loop.
568 * \param s Pointer to the server scheduler.
569 * \param t Pointer to the vss task structure.
571 * Before the timeout is computed, the current vss status flags are evaluated
572 * and acted upon by calling appropriate functions from the lower layers.
573 * Possible actions include
575 * - request a new audio file from afs,
576 * - shutdown of all senders (stop/pause command),
577 * - reposition the stream (ff/jmp command).
579 static void vss_pre_select(struct sched
*s
, struct task
*t
)
582 struct timeval
*tv
, diff
;
583 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
585 if (!vsst
->map
|| vss_next() || vss_paused() || vss_repos()) {
586 struct fec_client
*fc
, *tmp
;
587 for (i
= 0; senders
[i
].name
; i
++)
588 if (senders
[i
].shutdown_clients
)
589 senders
[i
].shutdown_clients();
590 list_for_each_entry_safe(fc
, tmp
, &fec_client_list
, node
)
591 fc
->first_stream_chunk
= -1;
595 else if (vss_paused()) {
596 if (mmd
->chunks_sent
)
597 tv_add(&mmd
->afd
.afhi
.eof_tv
, now
, &vsst
->eof_barrier
);
598 mmd
->chunks_sent
= 0;
599 } else if (vss_repos()) {
600 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
601 tv_add(&mmd
->afd
.afhi
.eof_tv
, now
, &vsst
->eof_barrier
);
602 mmd
->chunks_sent
= 0;
603 mmd
->current_chunk
= mmd
->repos_request
;
604 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
606 if (need_to_request_new_audio_file(vsst
)) {
607 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
608 para_fd_set(vsst
->afs_socket
, &s
->wfds
, &s
->max_fileno
);
609 vsst
->afsss
= AFS_SOCKET_CHECK_FOR_WRITE
;
611 para_fd_set(vsst
->afs_socket
, &s
->rfds
, &s
->max_fileno
);
612 for (i
= 0; senders
[i
].name
; i
++) {
613 if (!senders
[i
].pre_select
)
615 senders
[i
].pre_select(&s
->max_fileno
, &s
->rfds
, &s
->wfds
);
617 tv
= vss_compute_timeout(vsst
);
618 if (tv
&& tv_diff(tv
, &s
->timeout
, &diff
) < 0)
622 static int recv_afs_msg(int afs_socket
, int *fd
, uint32_t *code
, uint32_t *data
)
624 char control
[255], buf
[8];
625 struct msghdr msg
= {.msg_iov
= NULL
};
626 struct cmsghdr
*cmsg
;
632 iov
.iov_len
= sizeof(buf
);
635 msg
.msg_control
= control
;
636 msg
.msg_controllen
= sizeof(control
);
637 memset(buf
, 0, sizeof(buf
));
638 ret
= recvmsg(afs_socket
, &msg
, 0);
640 return -ERRNO_TO_PARA_ERROR(errno
);
641 if (iov
.iov_len
!= sizeof(buf
))
642 return -E_AFS_SHORT_READ
;
643 *code
= *(uint32_t*)buf
;
644 *data
= *(uint32_t*)(buf
+ 4);
645 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
646 if (cmsg
->cmsg_level
!= SOL_SOCKET
647 || cmsg
->cmsg_type
!= SCM_RIGHTS
)
649 if ((cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int) != 1)
651 *fd
= *(int *)CMSG_DATA(cmsg
);
656 static void recv_afs_result(struct vss_task
*vsst
)
658 int ret
, passed_fd
, shmid
;
659 uint32_t afs_code
= 0, afs_data
= 0;
662 vsst
->afsss
= AFS_SOCKET_READY
;
663 mmd
->afd
.afhi
.chunk_table
= NULL
;
664 ret
= recv_afs_msg(vsst
->afs_socket
, &passed_fd
, &afs_code
, &afs_data
);
667 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd
, afs_code
,
670 if (afs_code
!= NEXT_AUDIO_FILE
)
675 ret
= load_afd(shmid
, &mmd
->afd
);
679 ret
= fstat(passed_fd
, &statbuf
);
681 PARA_ERROR_LOG("fstat error:\n");
682 ret
= -ERRNO_TO_PARA_ERROR(errno
);
685 mmd
->size
= statbuf
.st_size
;
686 mmd
->mtime
= statbuf
.st_mtime
;
687 ret
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
, passed_fd
,
692 mmd
->chunks_sent
= 0;
693 mmd
->current_chunk
= 0;
697 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
698 afh_get_header(&mmd
->afd
.afhi
, vsst
->map
, &vsst
->header_buf
,
702 free(mmd
->afd
.afhi
.chunk_table
);
705 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
706 mmd
->new_vss_status_flags
= VSS_NEXT
;
710 * Main sending function.
712 * This function gets called from vss_post_select(). It checks whether the next
713 * chunk of data should be pushed out. It obtains a pointer to the data to be
714 * sent out as well as its length from mmd->afd.afhi. This information is then
715 * passed to each supported sender's send() function as well as to the send()
716 * functions of each registered fec client.
718 static void vss_send(struct vss_task
*vsst
)
722 struct fec_client
*fc
, *tmp_fc
;
724 if (!vsst
->map
|| !vss_playing())
726 if (chk_barrier("eof", &vsst
->eof_barrier
, &due
, 1) < 0)
728 if (chk_barrier("data send", &vsst
->data_send_barrier
,
731 if (mmd
->current_chunk
>= mmd
->afd
.afhi
.chunks_total
) { /* eof */
732 mmd
->new_vss_status_flags
|= VSS_NEXT
;
735 if (!mmd
->chunks_sent
) {
737 mmd
->stream_start
= *now
;
738 tv_scale(mmd
->current_chunk
, &mmd
->afd
.afhi
.chunk_tv
, &tmp
);
739 mmd
->offset
= tv2ms(&tmp
);
742 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
743 &mmd
->stream_start
, &due
);
744 if (tv_diff(&due
, now
, NULL
) <= 0) {
748 * We call the send function also in case of empty chunks as
749 * they might have still some data queued which can be sent in
752 afh_get_chunk(mmd
->current_chunk
, &mmd
->afd
.afhi
, vsst
->map
,
754 for (i
= 0; senders
[i
].name
; i
++) {
755 if (!senders
[i
].send
)
757 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
,
758 buf
, len
, vsst
->header_buf
, vsst
->header_len
);
761 list_for_each_entry_safe(fc
, tmp_fc
, &fec_client_list
, node
) {
762 if (!next_slice_is_due(fc
, NULL
))
764 compute_next_fec_slice(fc
, vsst
);
765 PARA_DEBUG_LOG("sending %d:%d (%zu bytes)\n", fc
->group
.num
,
766 fc
->slice
.num
, fc
->slice
.bytes
);
767 fc
->fcp
->send((char *)fc
->enc_buf
,
768 fc
->slice
.bytes
+ FEC_HEADER_SIZE
,
769 fc
->fcp
->private_data
);
772 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
774 mmd
->current_chunk
++;
777 static void vss_post_select(struct sched
*s
, struct task
*t
)
780 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
782 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
783 int num
= mmd
->sender_cmd_data
.cmd_num
,
784 sender_num
= mmd
->sender_cmd_data
.sender_num
;
786 if (senders
[sender_num
].client_cmds
[num
])
787 senders
[sender_num
].client_cmds
[num
](&mmd
->sender_cmd_data
);
788 mmd
->sender_cmd_data
.cmd_num
= -1;
790 if (vsst
->afsss
!= AFS_SOCKET_CHECK_FOR_WRITE
) {
791 if (FD_ISSET(vsst
->afs_socket
, &s
->rfds
))
792 recv_afs_result(vsst
);
793 } else if (FD_ISSET(vsst
->afs_socket
, &s
->wfds
)) {
794 PARA_NOTICE_LOG("requesting new fd from afs\n");
795 ret
= send_buffer(vsst
->afs_socket
, "new");
796 vsst
->afsss
= AFS_SOCKET_AFD_PENDING
;
798 for (i
= 0; senders
[i
].name
; i
++) {
799 if (!senders
[i
].post_select
)
801 senders
[i
].post_select(&s
->rfds
, &s
->wfds
);
803 if ((vss_playing() && !(mmd
->vss_status_flags
& VSS_PLAYING
)) ||
804 (vss_next() && vss_playing()))
805 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
810 * Initialize the virtual streaming system task.
812 * \param afs_socket The fd for communication with afs.
814 * This also initializes all supported senders and starts streaming
815 * if the --autoplay command line flag was given.
817 void init_vss_task(int afs_socket
)
819 static struct vss_task vss_task_struct
, *vsst
= &vss_task_struct
;
821 char *hn
= para_hostname(), *home
= para_homedir();
822 long unsigned announce_time
= conf
.announce_time_arg
> 0?
823 conf
.announce_time_arg
: 300,
824 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
825 conf
.autoplay_delay_arg
: 0;
827 vsst
->afs_socket
= afs_socket
;
828 vsst
->task
.pre_select
= vss_pre_select
;
829 vsst
->task
.post_select
= vss_post_select
;
830 ms2tv(announce_time
, &vsst
->announce_tv
);
831 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst
->announce_tv
));
832 for (i
= 0; senders
[i
].name
; i
++) {
833 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
834 senders
[i
].init(&senders
[i
]);
838 mmd
->sender_cmd_data
.cmd_num
= -1;
839 make_empty_status_items(mmd
->afd
.verbose_ls_output
);
840 if (conf
.autoplay_given
) {
842 mmd
->vss_status_flags
|= VSS_PLAYING
;
843 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
844 ms2tv(autoplay_delay
, &tmp
);
845 tv_add(now
, &tmp
, &vsst
->autoplay_barrier
);
846 tv_add(&vsst
->autoplay_barrier
, &vsst
->announce_tv
,
847 &vsst
->data_send_barrier
);
849 INIT_LIST_HEAD(&fec_client_list
);
850 register_task(&vsst
->task
);