2 * Copyright (C) 1997-2010 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 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
;
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 */
141 * Describes one connected 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
;
175 * Get the chunk time of the current audio file.
177 * \return A pointer to a struct containing the chunk time, or NULL,
178 * if currently no audio file is selected.
180 struct timeval
*vss_chunk_time(void)
182 if (mmd
->afd
.afhi
.chunk_tv
.tv_sec
== 0 &&
183 mmd
->afd
.afhi
.chunk_tv
.tv_usec
== 0)
185 return &mmd
->afd
.afhi
.chunk_tv
;
189 * Write a fec header to a buffer.
191 * \param buf The buffer to write to.
192 * \param h The fec header to write.
194 static void write_fec_header(struct fec_client
*fc
, struct vss_task
*vsst
)
196 char *buf
= (char *)fc
->enc_buf
;
197 struct fec_group
*g
= &fc
->group
;
198 struct fec_client_parms
*p
= fc
->fcp
;
200 write_u32(buf
, FEC_MAGIC
);
202 write_u8(buf
+ 4, p
->slices_per_group
+ fc
->num_extra_slices
);
203 write_u8(buf
+ 5, p
->data_slices_per_group
+ fc
->num_extra_slices
);
204 write_u32(buf
+ 6, g
->num_header_slices
? vsst
->header_len
: 0);
206 write_u32(buf
+ 10, g
->num
);
207 write_u32(buf
+ 14, g
->bytes
);
209 write_u8(buf
+ 18, fc
->current_slice_num
);
210 write_u16(buf
+ 20, p
->max_slice_bytes
- FEC_HEADER_SIZE
);
211 write_u8(buf
+ 22, g
->first_chunk
? 0 : 1);
212 write_u8(buf
+ 23, vsst
->header_len
? 1 : 0);
213 memset(buf
+ 24, 0, 7);
216 static int need_audio_header(struct fec_client
*fc
, struct vss_task
*vsst
)
218 if (!mmd
->current_chunk
) {
219 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
222 if (!vsst
->header_buf
)
224 if (!vsst
->header_len
)
226 if (fc
->group
.num
&& tv_diff(&fc
->next_header_time
, now
, NULL
) > 0)
228 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
232 static int num_slices(long unsigned bytes
, int mps
, int rs
)
234 int m
= mps
- FEC_HEADER_SIZE
;
239 ret
= (bytes
+ m
- 1) / m
;
245 /* set group start and group duration */
246 static void set_group_timing(struct fec_client
*fc
, struct fec_group
*g
)
248 struct timeval
*chunk_tv
= vss_chunk_time();
250 tv_scale(g
->num_chunks
, chunk_tv
, &g
->duration
);
251 tv_divide(fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
,
252 &g
->duration
, &g
->slice_duration
);
253 PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
254 tv2ms(&g
->duration
), tv2ms(chunk_tv
), tv2ms(&g
->slice_duration
));
257 static int initialize_fec_client(struct fec_client
*fc
, struct vss_task
*vsst
)
260 int hs
, ds
, rs
; /* header/data/redundant slices */
261 struct fec_client_parms
*fcp
= fc
->fcp
;
266 * Set the maximum slice size to the Maximum Packet Size if the
267 * transport protocol allows to determine this value. The user
268 * can specify a slice size up to this value.
270 ret
= fcp
->init_fec(fc
->sc
);
275 mps
= generic_max_transport_msg_size(fc
->sc
->fd
);
276 if (mps
<= FEC_HEADER_SIZE
)
277 return -ERRNO_TO_PARA_ERROR(EINVAL
);
279 rs
= fc
->fcp
->slices_per_group
- fc
->fcp
->data_slices_per_group
;
280 ret
= num_slices(vsst
->header_len
, mps
, rs
);
284 ret
= num_slices(afh_get_largest_chunk_size(&mmd
->afd
.afhi
),
290 if (k
< fc
->fcp
->data_slices_per_group
)
291 k
= fc
->fcp
->data_slices_per_group
;
293 PARA_CRIT_LOG("hs: %d, ds: %d, rs: %d, k: %d, n: %d\n", hs
, ds
, rs
, k
, n
);
295 ret
= fec_new(k
, n
, &fc
->parms
);
298 fc
->num_extra_slices
= k
- fc
->fcp
->data_slices_per_group
;
299 PARA_NOTICE_LOG("fec parms %d:%d:%d (%d extra slices)\n",
300 mps
, k
, n
, fc
->num_extra_slices
);
301 fc
->src_data
= para_realloc(fc
->src_data
, k
* sizeof(char *));
302 fc
->enc_buf
= para_realloc(fc
->enc_buf
, mps
);
303 memset(fc
->enc_buf
, 0, mps
);
304 fc
->extra_src_buf
= para_realloc(fc
->extra_src_buf
, mps
);
305 memset(fc
->extra_src_buf
, 0, mps
);
307 fc
->fcp
->max_slice_bytes
= mps
;
308 fc
->state
= FEC_STATE_READY_TO_RUN
;
309 fc
->next_header_time
.tv_sec
= 0;
310 fc
->stream_start
= *now
;
311 fc
->first_stream_chunk
= mmd
->current_chunk
;
318 static int setup_next_fec_group(struct fec_client
*fc
, struct vss_task
*vsst
)
320 int ret
, i
, k
, n
, data_slices
;
322 const char *buf
, *start_buf
;
323 struct fec_group
*g
= &fc
->group
;
324 unsigned slice_bytes
;
325 uint32_t max_data_size
;
327 if (fc
->first_stream_chunk
< 0) {
328 ret
= initialize_fec_client(fc
, vsst
);
331 g
->first_chunk
= mmd
->current_chunk
;
337 if (g
->first_chunk
+ g
->num_chunks
>= mmd
->afd
.afhi
.chunks_total
)
340 * Start and duration of this group depend only on the previous
341 * group. Compute the new group start as g->start += g->duration.
344 tv_add(&tmp
, &g
->duration
, &g
->start
);
345 set_group_timing(fc
, g
);
346 g
->first_chunk
+= g
->num_chunks
;
349 slice_bytes
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
350 PARA_CRIT_LOG("slice_bytes: %d\n", slice_bytes
);
351 k
= fc
->fcp
->data_slices_per_group
+ fc
->num_extra_slices
;
352 n
= fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
;
353 PARA_CRIT_LOG("k: %d, n: %d\n", k
, n
);
354 if (need_audio_header(fc
, vsst
)) {
355 ret
= num_slices(vsst
->header_len
, fc
->fcp
->max_slice_bytes
,
359 g
->num_header_slices
= ret
;
361 g
->num_header_slices
= 0;
362 afh_get_chunk(g
->first_chunk
, &mmd
->afd
.afhi
, vsst
->map
, &start_buf
,
364 data_slices
= k
- g
->num_header_slices
;
366 max_data_size
= slice_bytes
* data_slices
;
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
)
374 g
->num_chunks
= i
- g
->first_chunk
;
375 assert(g
->num_chunks
);
376 fc
->current_slice_num
= 0;
378 set_group_timing(fc
, g
);
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
;
387 /* setup data slices */
389 for (i
= g
->num_header_slices
; i
< k
; i
++) {
390 if (buf
+ slice_bytes
> vsst
->map
+ mmd
->size
)
392 * Can not use the memory mapped audio file for this
393 * slice as it goes beyond the map. This slice will not
397 fc
->src_data
[i
] = (const unsigned char *)buf
;
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
;
405 /* use arbitrary data for all remaining slices */
408 fc
->src_data
[i
] = (const unsigned char *)buf
;
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
419 static int compute_next_fec_slice(struct fec_client
*fc
, struct vss_task
*vsst
)
421 if (fc
->first_stream_chunk
< 0 || fc
->current_slice_num
422 == fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
) {
423 int ret
= setup_next_fec_group(fc
, vsst
);
427 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
428 PARA_ERROR_LOG("FEC client temporarily disabled\n");
429 fc
->state
= FEC_STATE_DISABLED
;
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
,
436 fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
);
441 * Return a buffer that marks the end of the stream.
443 * \param buf Result pointer.
444 * \return The length of the eof buffer.
446 * This is used for (multicast) udp streaming where closing the socket on the
447 * sender might not give rise to an eof condition at the peer.
449 size_t vss_get_fec_eof_packet(const char **buf
)
451 static const char fec_eof_packet
[FEC_HEADER_SIZE
] = FEC_EOF_PACKET
;
452 *buf
= fec_eof_packet
;
453 return FEC_HEADER_SIZE
;
457 * Add one entry to the list of active fec clients.
459 * \param sc Generic sender_client data of the transport layer.
460 * \param fcp FEC parameters as supplied by the transport layer.
462 * \return Newly allocated fec_client struct.
464 struct fec_client
*vss_add_fec_client(struct sender_client
*sc
,
465 struct fec_client_parms
*fcp
)
467 struct fec_client
*fc
= para_calloc(sizeof(*fc
));
471 para_list_add(&fc
->node
, &fec_client_list
);
476 * Remove one entry from the list of active fec clients.
478 * \param fc The client to be removed.
480 void vss_del_fec_client(struct fec_client
*fc
)
485 free(fc
->extra_src_buf
);
491 * Compute if/when next slice is due. If it isn't due yet and \a diff is
492 * not \p Null, compute the time difference next - now, where
494 * next = stream_start + (first_group_chunk - first_stream_chunk)
495 * * chunk_time + slice_num * slice_time
497 static int next_slice_is_due(struct fec_client
*fc
, struct timeval
*diff
)
499 struct timeval tmp
, next
;
502 if (fc
->first_stream_chunk
< 0)
504 tv_scale(fc
->current_slice_num
, &fc
->group
.slice_duration
, &tmp
);
505 tv_add(&tmp
, &fc
->group
.start
, &next
);
506 ret
= tv_diff(&next
, now
, diff
);
507 return ret
< 0? 1 : 0;
510 static void compute_slice_timeout(struct timeval
*timeout
)
512 struct fec_client
*fc
;
514 list_for_each_entry(fc
, &fec_client_list
, node
) {
517 if (fc
->state
!= FEC_STATE_READY_TO_RUN
)
519 if (next_slice_is_due(fc
, &diff
)) {
521 timeout
->tv_usec
= 0;
524 /* timeout = min(timeout, diff) */
525 if (tv_diff(&diff
, timeout
, NULL
) < 0)
530 static void set_eof_barrier(struct vss_task
*vsst
)
532 struct fec_client
*fc
;
533 struct timeval timeout
= {1, 0}, *chunk_tv
= vss_chunk_time();
537 list_for_each_entry(fc
, &fec_client_list
, node
) {
538 struct timeval group_duration
;
540 if (fc
->state
!= FEC_STATE_READY_TO_RUN
)
542 tv_scale(fc
->group
.num_chunks
, chunk_tv
, &group_duration
);
543 if (tv_diff(&timeout
, &group_duration
, NULL
) < 0)
544 timeout
= group_duration
;
547 tv_add(now
, &timeout
, &vsst
->eof_barrier
);
551 * Check if vss status flag \a P (playing) is set.
553 * \return Greater than zero if playing, zero otherwise.
556 unsigned int vss_playing(void)
558 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
562 * Check if the \a N (next) status flag is set.
564 * \return Greater than zero if set, zero if not.
567 unsigned int vss_next(void)
569 return mmd
->new_vss_status_flags
& VSS_NEXT
;
573 * Check if a reposition request is pending.
575 * \return Greater than zero if true, zero otherwise.
578 unsigned int vss_repos(void)
580 return mmd
->new_vss_status_flags
& VSS_REPOS
;
584 * Check if the vss is currently paused.
586 * \return Greater than zero if paused, zero otherwise.
589 unsigned int vss_paused(void)
591 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
592 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
596 * Check if the vss is currently stopped.
598 * \return Greater than zero if paused, zero otherwise.
601 unsigned int vss_stopped(void)
603 return (mmd
->new_vss_status_flags
& VSS_NEXT
)
604 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
607 static int chk_barrier(const char *bname
, const struct timeval
*barrier
,
608 struct timeval
*diff
, int print_log
)
612 if (tv_diff(now
, barrier
, diff
) > 0)
616 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
621 * != NULL: timeout for next chunk
622 * NULL: nothing to do
624 static struct timeval
*vss_compute_timeout(struct vss_task
*vsst
)
626 static struct timeval the_timeout
;
627 struct timeval next_chunk
;
629 if (vss_next() && vsst
->map
) {
630 /* only sleep a bit, nec*/
631 the_timeout
.tv_sec
= 0;
632 the_timeout
.tv_usec
= 100;
635 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
636 &the_timeout
, 1) < 0)
638 if (chk_barrier("eof", &vsst
->eof_barrier
, &the_timeout
, 1) < 0)
640 if (chk_barrier("data send", &vsst
->data_send_barrier
,
641 &the_timeout
, 1) < 0)
643 if (!vss_playing() || !vsst
->map
)
645 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
646 &mmd
->stream_start
, &next_chunk
);
647 if (chk_barrier("chunk", &next_chunk
, &the_timeout
, 0) >= 0) {
648 /* chunk is due or bof */
649 the_timeout
.tv_sec
= 0;
650 the_timeout
.tv_usec
= 0;
653 /* compute min of current timeout and next slice time */
654 compute_slice_timeout(&the_timeout
);
658 static void vss_eof(struct vss_task
*vsst
)
663 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
664 mmd
->new_vss_status_flags
= VSS_NEXT
;
665 set_eof_barrier(vsst
);
666 para_munmap(vsst
->map
, mmd
->size
);
668 mmd
->chunks_sent
= 0;
670 mmd
->afd
.afhi
.seconds_total
= 0;
671 mmd
->afd
.afhi
.chunk_tv
.tv_sec
= 0;
672 mmd
->afd
.afhi
.chunk_tv
.tv_usec
= 0;
673 free(mmd
->afd
.afhi
.chunk_table
);
674 mmd
->afd
.afhi
.chunk_table
= NULL
;
680 static int need_to_request_new_audio_file(struct vss_task
*vsst
)
684 if (vsst
->map
) /* have audio file */
686 if (!vss_playing()) /* don't need one */
688 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
690 if (vsst
->afsss
== AFS_SOCKET_AFD_PENDING
) /* already requested one */
692 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
698 static void set_mmd_offset(void)
700 struct timeval offset
;
701 tv_scale(mmd
->current_chunk
, &mmd
->afd
.afhi
.chunk_tv
, &offset
);
702 mmd
->offset
= tv2ms(&offset
);
706 * Compute the timeout for the main select-loop of the scheduler.
708 * \param s Pointer to the server scheduler.
709 * \param t Pointer to the vss task structure.
711 * Before the timeout is computed, the current vss status flags are evaluated
712 * and acted upon by calling appropriate functions from the lower layers.
713 * Possible actions include
715 * - request a new audio file from afs,
716 * - shutdown of all senders (stop/pause command),
717 * - reposition the stream (ff/jmp command).
719 static void vss_pre_select(struct sched
*s
, struct task
*t
)
723 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
725 if (!vsst
->map
|| vss_next() || vss_paused() || vss_repos()) {
726 struct fec_client
*fc
, *tmp
;
727 for (i
= 0; senders
[i
].name
; i
++)
728 if (senders
[i
].shutdown_clients
)
729 senders
[i
].shutdown_clients();
730 list_for_each_entry_safe(fc
, tmp
, &fec_client_list
, node
) {
731 fc
->first_stream_chunk
= -1;
732 fc
->state
= FEC_STATE_NONE
;
734 mmd
->stream_start
.tv_sec
= 0;
735 mmd
->stream_start
.tv_usec
= 0;
739 else if (vss_paused()) {
740 if (mmd
->chunks_sent
)
741 set_eof_barrier(vsst
);
742 mmd
->chunks_sent
= 0;
743 } else if (vss_repos()) {
744 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
745 set_eof_barrier(vsst
);
746 mmd
->chunks_sent
= 0;
747 mmd
->current_chunk
= mmd
->repos_request
;
748 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
751 if (need_to_request_new_audio_file(vsst
)) {
752 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
753 para_fd_set(vsst
->afs_socket
, &s
->wfds
, &s
->max_fileno
);
754 vsst
->afsss
= AFS_SOCKET_CHECK_FOR_WRITE
;
756 para_fd_set(vsst
->afs_socket
, &s
->rfds
, &s
->max_fileno
);
757 for (i
= 0; senders
[i
].name
; i
++) {
758 if (!senders
[i
].pre_select
)
760 senders
[i
].pre_select(&s
->max_fileno
, &s
->rfds
, &s
->wfds
);
762 tv
= vss_compute_timeout(vsst
);
764 sched_request_timeout(tv
, s
);
767 static int recv_afs_msg(int afs_socket
, int *fd
, uint32_t *code
, uint32_t *data
)
769 char control
[255], buf
[8];
770 struct msghdr msg
= {.msg_iov
= NULL
};
771 struct cmsghdr
*cmsg
;
777 iov
.iov_len
= sizeof(buf
);
780 msg
.msg_control
= control
;
781 msg
.msg_controllen
= sizeof(control
);
782 memset(buf
, 0, sizeof(buf
));
783 ret
= recvmsg(afs_socket
, &msg
, 0);
785 return -ERRNO_TO_PARA_ERROR(errno
);
786 if (iov
.iov_len
!= sizeof(buf
))
787 return -E_AFS_SHORT_READ
;
788 *code
= *(uint32_t*)buf
;
789 *data
= *(uint32_t*)(buf
+ 4);
790 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
791 if (cmsg
->cmsg_level
!= SOL_SOCKET
792 || cmsg
->cmsg_type
!= SCM_RIGHTS
)
794 if ((cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int) != 1)
796 *fd
= *(int *)CMSG_DATA(cmsg
);
801 static void recv_afs_result(struct vss_task
*vsst
, fd_set
*rfds
)
803 int ret
, passed_fd
, shmid
;
804 uint32_t afs_code
= 0, afs_data
= 0;
807 if (!FD_ISSET(vsst
->afs_socket
, rfds
))
809 ret
= recv_afs_msg(vsst
->afs_socket
, &passed_fd
, &afs_code
, &afs_data
);
810 if (ret
== -ERRNO_TO_PARA_ERROR(EAGAIN
))
814 vsst
->afsss
= AFS_SOCKET_READY
;
815 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd
, afs_code
,
818 if (afs_code
!= NEXT_AUDIO_FILE
)
823 ret
= load_afd(shmid
, &mmd
->afd
);
827 ret
= fstat(passed_fd
, &statbuf
);
829 PARA_ERROR_LOG("fstat error:\n");
830 ret
= -ERRNO_TO_PARA_ERROR(errno
);
833 mmd
->size
= statbuf
.st_size
;
834 mmd
->mtime
= statbuf
.st_mtime
;
835 ret
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
, passed_fd
,
840 mmd
->chunks_sent
= 0;
841 mmd
->current_chunk
= 0;
845 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
846 afh_get_header(&mmd
->afd
.afhi
, vsst
->map
, &vsst
->header_buf
,
850 free(mmd
->afd
.afhi
.chunk_table
);
853 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
854 mmd
->new_vss_status_flags
= VSS_NEXT
;
858 * Main sending function.
860 * This function gets called from vss_post_select(). It checks whether the next
861 * chunk of data should be pushed out. It obtains a pointer to the data to be
862 * sent out as well as its length from mmd->afd.afhi. This information is then
863 * passed to each supported sender's send() function as well as to the send()
864 * functions of each registered fec client.
866 static void vss_send(struct vss_task
*vsst
)
868 int i
, fec_active
= 0;
870 struct fec_client
*fc
, *tmp_fc
;
872 if (!vsst
->map
|| !vss_playing())
874 if (chk_barrier("eof", &vsst
->eof_barrier
, &due
, 1) < 0)
876 if (chk_barrier("data send", &vsst
->data_send_barrier
,
879 list_for_each_entry_safe(fc
, tmp_fc
, &fec_client_list
, node
) {
881 case FEC_STATE_DISABLED
:
884 fc
->first_stream_chunk
= -1; /* need setup */
886 case FEC_STATE_READY_TO_RUN
:
889 if (!next_slice_is_due(fc
, NULL
)) {
893 if (compute_next_fec_slice(fc
, vsst
) <= 0)
895 PARA_DEBUG_LOG("sending %d:%d (%u bytes)\n", fc
->group
.num
,
896 fc
->current_slice_num
, fc
->fcp
->max_slice_bytes
);
897 fc
->fcp
->send_fec(fc
->sc
, (char *)fc
->enc_buf
,
898 fc
->fcp
->max_slice_bytes
);
899 fc
->current_slice_num
++;
902 if (mmd
->current_chunk
>= mmd
->afd
.afhi
.chunks_total
) { /* eof */
904 mmd
->new_vss_status_flags
|= VSS_NEXT
;
907 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
908 &mmd
->stream_start
, &due
);
909 if (tv_diff(&due
, now
, NULL
) <= 0) {
913 if (!mmd
->chunks_sent
) {
914 mmd
->stream_start
= *now
;
919 * We call the send function also in case of empty chunks as
920 * they might have still some data queued which can be sent in
923 afh_get_chunk(mmd
->current_chunk
, &mmd
->afd
.afhi
, vsst
->map
,
925 for (i
= 0; senders
[i
].name
; i
++) {
926 if (!senders
[i
].send
)
928 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
,
929 buf
, len
, vsst
->header_buf
, vsst
->header_len
);
932 mmd
->current_chunk
++;
936 static void vss_post_select(struct sched
*s
, struct task
*t
)
939 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
942 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
943 int num
= mmd
->sender_cmd_data
.cmd_num
,
944 sender_num
= mmd
->sender_cmd_data
.sender_num
;
946 if (senders
[sender_num
].client_cmds
[num
]) {
947 ret
= senders
[sender_num
].client_cmds
[num
]
948 (&mmd
->sender_cmd_data
);
950 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
952 mmd
->sender_cmd_data
.cmd_num
= -1;
954 if (vsst
->afsss
!= AFS_SOCKET_CHECK_FOR_WRITE
)
955 recv_afs_result(vsst
, &s
->rfds
);
956 else if (FD_ISSET(vsst
->afs_socket
, &s
->wfds
)) {
957 PARA_NOTICE_LOG("requesting new fd from afs\n");
958 ret
= send_buffer(vsst
->afs_socket
, "new");
960 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
962 vsst
->afsss
= AFS_SOCKET_AFD_PENDING
;
964 for (i
= 0; senders
[i
].name
; i
++) {
965 if (!senders
[i
].post_select
)
967 senders
[i
].post_select(&s
->rfds
, &s
->wfds
);
969 if ((vss_playing() && !(mmd
->vss_status_flags
& VSS_PLAYING
)) ||
970 (vss_next() && vss_playing()))
971 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
976 * Initialize the virtual streaming system task.
978 * \param afs_socket The fd for communication with afs.
980 * This also initializes all supported senders and starts streaming
981 * if the --autoplay command line flag was given.
983 void init_vss_task(int afs_socket
)
985 static struct vss_task vss_task_struct
, *vsst
= &vss_task_struct
;
987 char *hn
= para_hostname(), *home
= para_homedir();
988 long unsigned announce_time
= conf
.announce_time_arg
> 0?
989 conf
.announce_time_arg
: 300,
990 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
991 conf
.autoplay_delay_arg
: 0;
992 vsst
->header_interval
.tv_sec
= 5; /* should this be configurable? */
993 vsst
->afs_socket
= afs_socket
;
994 vsst
->task
.pre_select
= vss_pre_select
;
995 vsst
->task
.post_select
= vss_post_select
;
996 ms2tv(announce_time
, &vsst
->announce_tv
);
997 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst
->announce_tv
));
998 INIT_LIST_HEAD(&fec_client_list
);
999 for (i
= 0; senders
[i
].name
; i
++) {
1000 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
1001 senders
[i
].init(&senders
[i
]);
1005 mmd
->sender_cmd_data
.cmd_num
= -1;
1006 if (conf
.autoplay_given
) {
1008 mmd
->vss_status_flags
|= VSS_PLAYING
;
1009 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
1010 ms2tv(autoplay_delay
, &tmp
);
1011 tv_add(now
, &tmp
, &vsst
->autoplay_barrier
);
1012 tv_add(&vsst
->autoplay_barrier
, &vsst
->announce_tv
,
1013 &vsst
->data_send_barrier
);
1015 register_task(&vsst
->task
);