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. */
90 /** Time between audio file headers are sent. */
91 struct timeval header_interval
;
95 * The list of currently connected fec clients.
97 * Senders may use \ref vss_add_fec_client() to add entries to the list.
99 static struct list_head fec_client_list
;
102 * Data associated with one FEC group.
104 * A FEC group consists of a fixed number of slices and this number is given by
105 * the \a slices_per_group parameter of struct \ref fec_client_parms. Each FEC
106 * group contains a number of chunks of the current audio file.
108 * FEC slices directly correspond to the data packages sent by the paraslash
109 * senders that use FEC. Each slice is identified by its group number and its
110 * number within the group. All slices have the same size, but the last slice
111 * of the group may not be filled entirely.
114 /** The number of the FEC group. */
116 /** Number of bytes in this group. */
118 /** The first chunk of the current audio file belonging to the group. */
119 uint32_t first_chunk
;
120 /** The number of chunks contained in this group. */
122 /** When the first chunk was sent. */
123 struct timeval start
;
124 /** The group duration divided by the number of slices. */
125 struct timeval slice_duration
;
126 /** Group contains the audio file header that occupies that many slices. */
127 unsigned num_header_slices
;
131 * Describes one connected FEC client.
134 /** Parameters requested by the client. */
135 struct fec_client_parms
*fcp
;
136 /** Used by the core FEC code. */
137 struct fec_parms
*parms
;
138 /** The position of this client in the fec client list. */
139 struct list_head node
;
140 /** When the first slice for this client was sent. */
141 struct timeval stream_start
;
142 /** The first chunk sent to this FEC client. */
143 int first_stream_chunk
;
144 /** Describes the current group. */
145 struct fec_group group
;
146 /** The current slice. */
147 uint8_t current_slice_num
;
148 /** The data to be FEC-encoded (point to a region within the mapped audio file). */
149 const unsigned char **src_data
;
150 /** Last time an audio header was sent. */
151 struct timeval next_header_time
;
152 /** Used for the last source pointer of an audio file. */
153 unsigned char *extra_src_buf
;
154 /** Extra slices needed to store largest chunk + header. */
155 int num_extra_slices
;
156 /** Contains the FEC-encoded data. */
157 unsigned char *enc_buf
;
161 * Get the chunk time of the current audio file.
163 * \return A pointer to a struct containing the chunk time, or NULL,
164 * if currently no audio file is selected.
166 struct timeval
*vss_chunk_time(void)
168 if (mmd
->afd
.afhi
.chunk_tv
.tv_sec
== 0 &&
169 mmd
->afd
.afhi
.chunk_tv
.tv_usec
== 0)
171 return &mmd
->afd
.afhi
.chunk_tv
;
175 * Write a fec header to a buffer.
177 * \param buf The buffer to write to.
178 * \param h The fec header to write.
180 static void write_fec_header(struct fec_client
*fc
, struct vss_task
*vsst
)
182 char *buf
= (char *)fc
->enc_buf
;
183 struct fec_group
*g
= &fc
->group
;
184 struct fec_client_parms
*p
= fc
->fcp
;
186 write_u32(buf
, FEC_MAGIC
);
188 write_u8(buf
+ 4, p
->slices_per_group
+ fc
->num_extra_slices
);
189 write_u8(buf
+ 5, p
->data_slices_per_group
+ fc
->num_extra_slices
);
190 write_u32(buf
+ 6, g
->num_header_slices
? vsst
->header_len
: 0);
192 write_u32(buf
+ 10, g
->num
);
193 write_u32(buf
+ 14, g
->bytes
);
195 write_u8(buf
+ 18, fc
->current_slice_num
);
196 write_u16(buf
+ 20, p
->max_slice_bytes
- FEC_HEADER_SIZE
);
197 write_u8(buf
+ 22, g
->first_chunk
? 0 : 1);
198 write_u8(buf
+ 23, vsst
->header_len
? 1 : 0);
199 memset(buf
+ 24, 0, 7);
202 static int need_audio_header(struct fec_client
*fc
, struct vss_task
*vsst
)
204 if (!mmd
->current_chunk
) {
205 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
208 if (!vsst
->header_buf
)
210 if (!vsst
->header_len
)
212 if (fc
->group
.num
&& tv_diff(&fc
->next_header_time
, now
, NULL
) > 0)
214 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
218 static uint8_t num_slices(long unsigned bytes
, struct fec_client
*fc
)
220 uint16_t m
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
221 return (bytes
+ m
- 1) / m
;
224 static int setup_next_fec_group(struct fec_client
*fc
, struct vss_task
*vsst
)
226 int i
, k
, data_slices
;
228 const char *buf
, *start_buf
;
229 struct timeval tmp
, *chunk_tv
= vss_chunk_time();
230 struct fec_group
*g
= &fc
->group
;
231 unsigned slice_bytes
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
232 uint32_t max_data_size
;
235 k
= fc
->fcp
->data_slices_per_group
+ fc
->num_extra_slices
;
236 if (fc
->first_stream_chunk
< 0) {
237 uint32_t largest
= afh_get_largest_chunk_size(&mmd
->afd
.afhi
)
239 uint8_t needed
= num_slices(largest
, fc
), want
;
240 if (needed
> fc
->fcp
->data_slices_per_group
)
241 PARA_WARNING_LOG("fec parms insufficient for this audio file\n");
242 want
= PARA_MAX(needed
, fc
->fcp
->data_slices_per_group
);
246 fc
->src_data
= para_realloc(fc
->src_data
, want
* sizeof(char *));
247 ret
= fec_new(want
, want
+ fc
->fcp
->slices_per_group
248 - fc
->fcp
->data_slices_per_group
, &fc
->parms
);
252 fc
->num_extra_slices
= 0;
253 if (k
> fc
->fcp
->data_slices_per_group
) {
254 fc
->num_extra_slices
= k
- fc
->fcp
->data_slices_per_group
;
255 PARA_NOTICE_LOG("using %d extra slices\n",
256 fc
->num_extra_slices
);
259 fc
->stream_start
= *now
;
260 fc
->first_stream_chunk
= mmd
->current_chunk
;
261 g
->first_chunk
= mmd
->current_chunk
;
264 g
->first_chunk
+= g
->num_chunks
;
267 if (g
->first_chunk
>= mmd
->afd
.afhi
.chunks_total
)
269 if (need_audio_header(fc
, vsst
))
270 g
->num_header_slices
= num_slices(vsst
->header_len
, fc
);
272 g
->num_header_slices
= 0;
273 afh_get_chunk(g
->first_chunk
, &mmd
->afd
.afhi
, vsst
->map
, &start_buf
,
275 data_slices
= k
- g
->num_header_slices
;
277 max_data_size
= slice_bytes
* data_slices
;
279 for (i
= g
->first_chunk
; i
< mmd
->afd
.afhi
.chunks_total
; i
++) {
280 afh_get_chunk(i
, &mmd
->afd
.afhi
, vsst
->map
, &buf
, &len
);
281 if (g
->bytes
+ len
> max_data_size
)
285 g
->num_chunks
= i
- g
->first_chunk
;
286 assert(g
->num_chunks
);
287 fc
->current_slice_num
= 0;
289 /* setup header slices */
290 buf
= vsst
->header_buf
;
291 for (i
= 0; i
< g
->num_header_slices
; i
++) {
292 fc
->src_data
[i
] = (const unsigned char *)buf
;
296 /* setup data slices */
298 for (i
= g
->num_header_slices
; i
< k
; i
++) {
299 if (buf
+ slice_bytes
> vsst
->map
+ mmd
->size
)
301 * Can not use the memory mapped audio file for this
302 * slice as it goes beyond the map. This slice will not
306 fc
->src_data
[i
] = (const unsigned char *)buf
;
310 uint32_t payload_size
= vsst
->map
+ mmd
->size
- buf
;
311 memcpy(fc
->extra_src_buf
, buf
, payload_size
);
312 fc
->src_data
[i
] = fc
->extra_src_buf
;
314 /* use arbitrary data for all remaining slices */
317 fc
->src_data
[i
] = (const unsigned char *)buf
;
320 /* setup group timing */
321 tv_scale(g
->first_chunk
- fc
->first_stream_chunk
, chunk_tv
, &tmp
);
322 tv_add(&fc
->stream_start
, &tmp
, &g
->start
);
323 if (g
->num
) /* quick hack to avoid buffer underruns */
325 tv_scale(g
->num_chunks
, chunk_tv
, &tmp
); /* group duration */
326 tv_divide(fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
,
327 &tmp
, &g
->slice_duration
);
329 PARA_DEBUG_LOG("FEC group %d: %d chunks (%d - %d), %d header slices, %d data slices\n",
330 g
->num
, g
->num_chunks
, g
->first_chunk
,
331 g
->first_chunk
+ g
->num_chunks
- 1,
332 g
->num_header_slices
, data_slices
334 PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
335 tv2ms(&tmp
), tv2ms(chunk_tv
), tv2ms(&g
->slice_duration
));
339 static int compute_next_fec_slice(struct fec_client
*fc
, struct vss_task
*vsst
)
341 if (fc
->first_stream_chunk
< 0 || fc
->current_slice_num
342 == fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
) {
343 int ret
= setup_next_fec_group(fc
, vsst
);
347 write_fec_header(fc
, vsst
);
348 fec_encode(fc
->parms
, fc
->src_data
, fc
->enc_buf
+ FEC_HEADER_SIZE
,
349 fc
->current_slice_num
,
350 fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
);
355 * Return a buffer that marks the end of the stream.
357 * \param buf Result pointer.
358 * \return The length of the eof buffer.
360 * This is used for (multicast) udp streaming where closing the socket on the
361 * sender might not give rise to an eof condition at the peer.
363 size_t vss_get_fec_eof_packet(const char **buf
)
365 static const char fec_eof_packet
[FEC_HEADER_SIZE
] = FEC_EOF_PACKET
;
366 *buf
= fec_eof_packet
;
367 return FEC_HEADER_SIZE
;
371 * Add one entry to the list of active fec clients.
373 * \param fcp Describes the fec parameters to be used for this client.
374 * \param result An opaque pointer that must be used by remove the client later.
378 int vss_add_fec_client(struct fec_client_parms
*fcp
, struct fec_client
**result
)
381 struct fec_client
*fc
;
383 if (fcp
->max_slice_bytes
< FEC_HEADER_SIZE
+ fcp
->data_slices_per_group
)
384 return -ERRNO_TO_PARA_ERROR(EINVAL
);
385 fc
= para_calloc(sizeof(*fc
));
387 ret
= fec_new(fcp
->data_slices_per_group
, fcp
->slices_per_group
,
391 fc
->first_stream_chunk
= -1; /* stream not yet started */
392 fc
->src_data
= para_malloc(fc
->fcp
->slices_per_group
* sizeof(char *));
393 fc
->enc_buf
= para_calloc(fc
->fcp
->max_slice_bytes
);
394 fc
->num_extra_slices
= 0;
395 fc
->extra_src_buf
= para_calloc(fc
->fcp
->max_slice_bytes
);
396 fc
->next_header_time
.tv_sec
= 0;
397 para_list_add(&fc
->node
, &fec_client_list
);
408 * Remove one entry from the list of active fec clients.
410 * \param fc The client to be removed.
412 void vss_del_fec_client(struct fec_client
*fc
)
417 free(fc
->extra_src_buf
);
423 * Compute if/when next slice is due. If it isn't due yet and \a diff is
424 * not \p Null, compute the time difference next - now, where
426 * next = stream_start + (first_group_chunk - first_stream_chunk)
427 * * chunk_time + slice_num * slice_time
429 static int next_slice_is_due(struct fec_client
*fc
, struct timeval
*diff
)
431 struct timeval tmp
, next
;
434 if (fc
->first_stream_chunk
< 0)
436 tv_scale(fc
->current_slice_num
, &fc
->group
.slice_duration
, &tmp
);
437 tv_add(&tmp
, &fc
->group
.start
, &next
);
438 ret
= tv_diff(&next
, now
, diff
);
439 return ret
< 0? 1 : 0;
442 static void compute_slice_timeout(struct timeval
*timeout
)
444 struct fec_client
*fc
;
446 assert(vss_playing());
447 list_for_each_entry(fc
, &fec_client_list
, node
) {
450 if (next_slice_is_due(fc
, &diff
)) {
452 timeout
->tv_usec
= 0;
455 /* timeout = min(timeout, diff) */
456 if (tv_diff(&diff
, timeout
, NULL
) < 0)
462 * Check if vss status flag \a P (playing) is set.
464 * \return Greater than zero if playing, zero otherwise.
467 unsigned int vss_playing(void)
469 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
473 * Check if the \a N (next) status flag is set.
475 * \return Greater than zero if set, zero if not.
478 unsigned int vss_next(void)
480 return mmd
->new_vss_status_flags
& VSS_NEXT
;
484 * Check if a reposition request is pending.
486 * \return Greater than zero if true, zero otherwise.
489 unsigned int vss_repos(void)
491 return mmd
->new_vss_status_flags
& VSS_REPOS
;
495 * Check if the vss is currently paused.
497 * \return Greater than zero if paused, zero otherwise.
500 unsigned int vss_paused(void)
502 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
503 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
507 * Check if the vss is currently stopped.
509 * \return Greater than zero if paused, zero otherwise.
512 unsigned int vss_stopped(void)
514 return (mmd
->new_vss_status_flags
& VSS_NEXT
)
515 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
518 static int chk_barrier(const char *bname
, const struct timeval
*barrier
,
519 struct timeval
*diff
, int print_log
)
523 if (tv_diff(now
, barrier
, diff
) > 0)
527 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
532 * != NULL: timeout for next chunk
533 * NULL: nothing to do
535 static struct timeval
*vss_compute_timeout(struct vss_task
*vsst
)
537 static struct timeval the_timeout
;
538 struct timeval next_chunk
;
540 if (vss_next() && vsst
->map
) {
541 /* only sleep a bit, nec*/
542 the_timeout
.tv_sec
= 0;
543 the_timeout
.tv_usec
= 100;
546 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
547 &the_timeout
, 1) < 0)
549 if (chk_barrier("eof", &vsst
->eof_barrier
, &the_timeout
, 1) < 0)
551 if (chk_barrier("data send", &vsst
->data_send_barrier
,
552 &the_timeout
, 1) < 0)
554 if (!vss_playing() || !vsst
->map
)
556 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
557 &mmd
->stream_start
, &next_chunk
);
558 if (chk_barrier("chunk", &next_chunk
, &the_timeout
, 0) >= 0) {
559 /* chunk is due or bof */
560 the_timeout
.tv_sec
= 0;
561 the_timeout
.tv_usec
= 0;
564 /* compute min of current timeout and next slice time */
565 compute_slice_timeout(&the_timeout
);
569 static void vss_eof(struct vss_task
*vsst
)
572 mmd
->stream_start
= *now
;
575 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
576 mmd
->new_vss_status_flags
= VSS_NEXT
;
577 tv_add(&mmd
->afd
.afhi
.eof_tv
, now
, &vsst
->eof_barrier
);
578 para_munmap(vsst
->map
, mmd
->size
);
580 mmd
->chunks_sent
= 0;
582 mmd
->afd
.afhi
.seconds_total
= 0;
583 mmd
->afd
.afhi
.chunk_tv
.tv_sec
= 0;
584 mmd
->afd
.afhi
.chunk_tv
.tv_usec
= 0;
585 free(mmd
->afd
.afhi
.chunk_table
);
586 mmd
->afd
.afhi
.chunk_table
= NULL
;
587 free(mmd
->afd
.afhi
.info_string
);
588 mmd
->afd
.afhi
.info_string
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_FILE_INFO
],
589 status_item_list
[SI_TAGINFO1
], status_item_list
[SI_TAGINFO2
]);
590 make_empty_status_items(mmd
->afd
.verbose_ls_output
);
597 * Get the list of all supported audio formats.
599 * \return Aa space separated list of all supported audio formats
600 * It is not allocated at runtime, i.e. there is no need to free
601 * the returned string in the caller.
603 const char *supported_audio_formats(void)
605 return SUPPORTED_AUDIO_FORMATS
;
608 static int need_to_request_new_audio_file(struct vss_task
*vsst
)
612 if (vsst
->map
) /* have audio file */
614 if (!vss_playing()) /* don't need one */
616 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
618 if (vsst
->afsss
== AFS_SOCKET_AFD_PENDING
) /* already requested one */
620 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
627 * Compute the timeout for para_server's main select-loop.
629 * This function gets called from para_server to determine the timeout value
630 * for its main select loop.
632 * \param s Pointer to the server scheduler.
633 * \param t Pointer to the vss task structure.
635 * Before the timeout is computed, the current vss status flags are evaluated
636 * and acted upon by calling appropriate functions from the lower layers.
637 * Possible actions include
639 * - request a new audio file from afs,
640 * - shutdown of all senders (stop/pause command),
641 * - reposition the stream (ff/jmp command).
643 static void vss_pre_select(struct sched
*s
, struct task
*t
)
646 struct timeval
*tv
, diff
;
647 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
649 if (!vsst
->map
|| vss_next() || vss_paused() || vss_repos()) {
650 struct fec_client
*fc
, *tmp
;
651 for (i
= 0; senders
[i
].name
; i
++)
652 if (senders
[i
].shutdown_clients
)
653 senders
[i
].shutdown_clients();
654 list_for_each_entry_safe(fc
, tmp
, &fec_client_list
, node
)
655 fc
->first_stream_chunk
= -1;
659 else if (vss_paused()) {
660 if (mmd
->chunks_sent
)
661 tv_add(&mmd
->afd
.afhi
.eof_tv
, now
, &vsst
->eof_barrier
);
662 mmd
->chunks_sent
= 0;
663 } else if (vss_repos()) {
664 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
665 tv_add(&mmd
->afd
.afhi
.eof_tv
, now
, &vsst
->eof_barrier
);
666 mmd
->chunks_sent
= 0;
667 mmd
->current_chunk
= mmd
->repos_request
;
668 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
670 if (need_to_request_new_audio_file(vsst
)) {
671 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
672 para_fd_set(vsst
->afs_socket
, &s
->wfds
, &s
->max_fileno
);
673 vsst
->afsss
= AFS_SOCKET_CHECK_FOR_WRITE
;
675 para_fd_set(vsst
->afs_socket
, &s
->rfds
, &s
->max_fileno
);
676 for (i
= 0; senders
[i
].name
; i
++) {
677 if (!senders
[i
].pre_select
)
679 senders
[i
].pre_select(&s
->max_fileno
, &s
->rfds
, &s
->wfds
);
681 tv
= vss_compute_timeout(vsst
);
682 if (tv
&& tv_diff(tv
, &s
->timeout
, &diff
) < 0)
686 static int recv_afs_msg(int afs_socket
, int *fd
, uint32_t *code
, uint32_t *data
)
688 char control
[255], buf
[8];
689 struct msghdr msg
= {.msg_iov
= NULL
};
690 struct cmsghdr
*cmsg
;
696 iov
.iov_len
= sizeof(buf
);
699 msg
.msg_control
= control
;
700 msg
.msg_controllen
= sizeof(control
);
701 memset(buf
, 0, sizeof(buf
));
702 ret
= recvmsg(afs_socket
, &msg
, 0);
704 return -ERRNO_TO_PARA_ERROR(errno
);
705 if (iov
.iov_len
!= sizeof(buf
))
706 return -E_AFS_SHORT_READ
;
707 *code
= *(uint32_t*)buf
;
708 *data
= *(uint32_t*)(buf
+ 4);
709 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
710 if (cmsg
->cmsg_level
!= SOL_SOCKET
711 || cmsg
->cmsg_type
!= SCM_RIGHTS
)
713 if ((cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int) != 1)
715 *fd
= *(int *)CMSG_DATA(cmsg
);
720 static void recv_afs_result(struct vss_task
*vsst
)
722 int ret
, passed_fd
, shmid
;
723 uint32_t afs_code
= 0, afs_data
= 0;
726 vsst
->afsss
= AFS_SOCKET_READY
;
727 mmd
->afd
.afhi
.chunk_table
= NULL
;
728 ret
= recv_afs_msg(vsst
->afs_socket
, &passed_fd
, &afs_code
, &afs_data
);
731 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd
, afs_code
,
734 if (afs_code
!= NEXT_AUDIO_FILE
)
739 ret
= load_afd(shmid
, &mmd
->afd
);
743 ret
= fstat(passed_fd
, &statbuf
);
745 PARA_ERROR_LOG("fstat error:\n");
746 ret
= -ERRNO_TO_PARA_ERROR(errno
);
749 mmd
->size
= statbuf
.st_size
;
750 mmd
->mtime
= statbuf
.st_mtime
;
751 ret
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
, passed_fd
,
756 mmd
->chunks_sent
= 0;
757 mmd
->current_chunk
= 0;
761 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
762 afh_get_header(&mmd
->afd
.afhi
, vsst
->map
, &vsst
->header_buf
,
766 free(mmd
->afd
.afhi
.chunk_table
);
769 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
770 mmd
->new_vss_status_flags
= VSS_NEXT
;
774 * Main sending function.
776 * This function gets called from vss_post_select(). It checks whether the next
777 * chunk of data should be pushed out. It obtains a pointer to the data to be
778 * sent out as well as its length from mmd->afd.afhi. This information is then
779 * passed to each supported sender's send() function as well as to the send()
780 * functions of each registered fec client.
782 static void vss_send(struct vss_task
*vsst
)
784 int i
, sent_something
= 0;
786 struct fec_client
*fc
, *tmp_fc
;
788 if (!vsst
->map
|| !vss_playing())
790 if (chk_barrier("eof", &vsst
->eof_barrier
, &due
, 1) < 0)
792 if (chk_barrier("data send", &vsst
->data_send_barrier
,
795 list_for_each_entry_safe(fc
, tmp_fc
, &fec_client_list
, node
) {
796 if (!next_slice_is_due(fc
, NULL
))
798 if (compute_next_fec_slice(fc
, vsst
) <= 0)
800 PARA_DEBUG_LOG("sending %d:%d (%u bytes)\n", fc
->group
.num
,
801 fc
->current_slice_num
, fc
->fcp
->max_slice_bytes
);
802 fc
->fcp
->send((char *)fc
->enc_buf
,
803 fc
->fcp
->max_slice_bytes
,
804 fc
->fcp
->private_data
);
805 fc
->current_slice_num
++;
808 if (mmd
->current_chunk
>= mmd
->afd
.afhi
.chunks_total
) { /* eof */
810 mmd
->new_vss_status_flags
|= VSS_NEXT
;
813 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
814 &mmd
->stream_start
, &due
);
815 if (tv_diff(&due
, now
, NULL
) <= 0) {
819 if (!mmd
->chunks_sent
) {
821 mmd
->stream_start
= *now
;
822 tv_scale(mmd
->current_chunk
, &mmd
->afd
.afhi
.chunk_tv
, &tmp
);
823 mmd
->offset
= tv2ms(&tmp
);
827 * We call the send function also in case of empty chunks as
828 * they might have still some data queued which can be sent in
831 afh_get_chunk(mmd
->current_chunk
, &mmd
->afd
.afhi
, vsst
->map
,
833 for (i
= 0; senders
[i
].name
; i
++) {
834 if (!senders
[i
].send
)
836 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
,
837 buf
, len
, vsst
->header_buf
, vsst
->header_len
);
840 mmd
->current_chunk
++;
844 static void vss_post_select(struct sched
*s
, struct task
*t
)
847 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
850 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
851 int num
= mmd
->sender_cmd_data
.cmd_num
,
852 sender_num
= mmd
->sender_cmd_data
.sender_num
;
854 if (senders
[sender_num
].client_cmds
[num
])
855 senders
[sender_num
].client_cmds
[num
](&mmd
->sender_cmd_data
);
856 mmd
->sender_cmd_data
.cmd_num
= -1;
858 if (vsst
->afsss
!= AFS_SOCKET_CHECK_FOR_WRITE
) {
859 if (FD_ISSET(vsst
->afs_socket
, &s
->rfds
))
860 recv_afs_result(vsst
);
861 } else if (FD_ISSET(vsst
->afs_socket
, &s
->wfds
)) {
862 PARA_NOTICE_LOG("requesting new fd from afs\n");
863 ret
= send_buffer(vsst
->afs_socket
, "new");
864 vsst
->afsss
= AFS_SOCKET_AFD_PENDING
;
866 for (i
= 0; senders
[i
].name
; i
++) {
867 if (!senders
[i
].post_select
)
869 senders
[i
].post_select(&s
->rfds
, &s
->wfds
);
871 if ((vss_playing() && !(mmd
->vss_status_flags
& VSS_PLAYING
)) ||
872 (vss_next() && vss_playing()))
873 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
878 * Initialize the virtual streaming system task.
880 * \param afs_socket The fd for communication with afs.
882 * This also initializes all supported senders and starts streaming
883 * if the --autoplay command line flag was given.
885 void init_vss_task(int afs_socket
)
887 static struct vss_task vss_task_struct
, *vsst
= &vss_task_struct
;
889 char *hn
= para_hostname(), *home
= para_homedir();
890 long unsigned announce_time
= conf
.announce_time_arg
> 0?
891 conf
.announce_time_arg
: 300,
892 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
893 conf
.autoplay_delay_arg
: 0;
894 vsst
->header_interval
.tv_sec
= 5; /* should this be configurable? */
895 vsst
->afs_socket
= afs_socket
;
896 vsst
->task
.pre_select
= vss_pre_select
;
897 vsst
->task
.post_select
= vss_post_select
;
898 ms2tv(announce_time
, &vsst
->announce_tv
);
899 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst
->announce_tv
));
900 INIT_LIST_HEAD(&fec_client_list
);
901 for (i
= 0; senders
[i
].name
; i
++) {
902 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
903 senders
[i
].init(&senders
[i
]);
907 mmd
->sender_cmd_data
.cmd_num
= -1;
908 make_empty_status_items(mmd
->afd
.verbose_ls_output
);
909 if (conf
.autoplay_given
) {
911 mmd
->vss_status_flags
|= VSS_PLAYING
;
912 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
913 ms2tv(autoplay_delay
, &tmp
);
914 tv_add(now
, &tmp
, &vsst
->autoplay_barrier
);
915 tv_add(&vsst
->autoplay_barrier
, &vsst
->announce_tv
,
916 &vsst
->data_send_barrier
);
918 register_task(&vsst
->task
);