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
19 #include "portable_io.h"
26 #include "server.cmdline.h"
34 extern struct misc_meta_data
*mmd
;
36 extern void dccp_send_init(struct sender
*);
37 extern void http_send_init(struct sender
*);
38 extern void udp_send_init(struct sender
*);
40 /** The list of supported senders. */
41 struct sender senders
[] = {
44 .init
= http_send_init
,
48 .init
= dccp_send_init
,
52 .init
= udp_send_init
,
59 /** The possible states of the afs socket. */
60 enum afs_socket_status
{
61 /** Socket is inactive. */
63 /** Socket fd was included in the write fd set for select(). */
64 AFS_SOCKET_CHECK_FOR_WRITE
,
65 /** vss wrote a request to the socket and waits for reply from afs. */
66 AFS_SOCKET_AFD_PENDING
69 /** The task structure for the virtual streaming system. */
71 /** Copied from the -announce_time command line option. */
72 struct timeval announce_tv
;
73 /** End of the announcing interval. */
74 struct timeval data_send_barrier
;
75 /** End of the EOF interval. */
76 struct timeval eof_barrier
;
77 /** Only used if --autoplay_delay was given. */
78 struct timeval autoplay_barrier
;
79 /** Used for afs-server communication. */
81 /** The current state of \a afs_socket. */
82 enum afs_socket_status afsss
;
83 /** The memory mapped audio file. */
85 /** Used by the scheduler. */
87 /** Pointer to the header of the mapped audio file. */
88 const char *header_buf
;
89 /** Length of the audio file header. */
91 /** Time between audio file headers are sent. */
92 struct timeval header_interval
;
96 * The list of currently connected fec clients.
98 * Senders may use \ref vss_add_fec_client() to add entries to the list.
100 static struct list_head fec_client_list
;
103 * Data associated with one FEC group.
105 * A FEC group consists of a fixed number of slices and this number is given by
106 * the \a slices_per_group parameter of struct \ref fec_client_parms. Each FEC
107 * group contains a number of chunks of the current audio file.
109 * FEC slices directly correspond to the data packages sent by the paraslash
110 * senders that use FEC. Each slice is identified by its group number and its
111 * number within the group. All slices have the same size, but the last slice
112 * of the group may not be filled entirely.
115 /** The number of the FEC group. */
117 /** Number of bytes in this group. */
119 /** The first chunk of the current audio file belonging to the group. */
120 uint32_t first_chunk
;
121 /** The number of chunks contained in this group. */
123 /** When the first chunk was sent. */
124 struct timeval start
;
125 /** The group duration divided by the number of slices. */
126 struct timeval slice_duration
;
127 /** Group contains the audio file header that occupies that many slices. */
128 uint8_t num_header_slices
;
132 * Describes one connected FEC client.
135 /** If negative, this client is temporarily disabled. */
137 /** Parameters requested by the client. */
138 struct fec_client_parms
*fcp
;
139 /** Used by the core FEC code. */
140 struct fec_parms
*parms
;
141 /** The position of this client in the fec client list. */
142 struct list_head node
;
143 /** When the first slice for this client was sent. */
144 struct timeval stream_start
;
145 /** The first chunk sent to this FEC client. */
146 int first_stream_chunk
;
147 /** Describes the current group. */
148 struct fec_group group
;
149 /** The current slice. */
150 uint8_t current_slice_num
;
151 /** The data to be FEC-encoded (point to a region within the mapped audio file). */
152 const unsigned char **src_data
;
153 /** Last time an audio header was sent. */
154 struct timeval next_header_time
;
155 /** Used for the last source pointer of an audio file. */
156 unsigned char *extra_src_buf
;
157 /** Extra slices needed to store largest chunk + header. */
158 int num_extra_slices
;
159 /** Contains the FEC-encoded data. */
160 unsigned char *enc_buf
;
164 * Get the chunk time of the current audio file.
166 * \return A pointer to a struct containing the chunk time, or NULL,
167 * if currently no audio file is selected.
169 struct timeval
*vss_chunk_time(void)
171 if (mmd
->afd
.afhi
.chunk_tv
.tv_sec
== 0 &&
172 mmd
->afd
.afhi
.chunk_tv
.tv_usec
== 0)
174 return &mmd
->afd
.afhi
.chunk_tv
;
178 * Write a fec header to a buffer.
180 * \param buf The buffer to write to.
181 * \param h The fec header to write.
183 static void write_fec_header(struct fec_client
*fc
, struct vss_task
*vsst
)
185 char *buf
= (char *)fc
->enc_buf
;
186 struct fec_group
*g
= &fc
->group
;
187 struct fec_client_parms
*p
= fc
->fcp
;
189 write_u32(buf
, FEC_MAGIC
);
191 write_u8(buf
+ 4, p
->slices_per_group
+ fc
->num_extra_slices
);
192 write_u8(buf
+ 5, p
->data_slices_per_group
+ fc
->num_extra_slices
);
193 write_u32(buf
+ 6, g
->num_header_slices
? vsst
->header_len
: 0);
195 write_u32(buf
+ 10, g
->num
);
196 write_u32(buf
+ 14, g
->bytes
);
198 write_u8(buf
+ 18, fc
->current_slice_num
);
199 write_u16(buf
+ 20, p
->max_slice_bytes
- FEC_HEADER_SIZE
);
200 write_u8(buf
+ 22, g
->first_chunk
? 0 : 1);
201 write_u8(buf
+ 23, vsst
->header_len
? 1 : 0);
202 memset(buf
+ 24, 0, 7);
205 static int need_audio_header(struct fec_client
*fc
, struct vss_task
*vsst
)
207 if (!mmd
->current_chunk
) {
208 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
211 if (!vsst
->header_buf
)
213 if (!vsst
->header_len
)
215 if (fc
->group
.num
&& tv_diff(&fc
->next_header_time
, now
, NULL
) > 0)
217 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
221 static int num_slices(long unsigned bytes
, struct fec_client
*fc
, uint8_t *result
)
223 unsigned long m
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
224 unsigned rv
, redundant_slices
= fc
->fcp
->slices_per_group
225 - fc
->fcp
->data_slices_per_group
;
229 rv
= (bytes
+ m
- 1) / m
;
230 if (rv
+ redundant_slices
> 255)
236 static int setup_next_fec_group(struct fec_client
*fc
, struct vss_task
*vsst
)
238 int ret
, i
, k
, data_slices
;
240 const char *buf
, *start_buf
;
241 struct timeval tmp
, *chunk_tv
= vss_chunk_time();
242 struct fec_group
*g
= &fc
->group
;
243 unsigned slice_bytes
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
244 uint32_t max_data_size
;
247 k
= fc
->fcp
->data_slices_per_group
+ fc
->num_extra_slices
;
248 if (fc
->first_stream_chunk
< 0) {
249 uint32_t largest
= afh_get_largest_chunk_size(&mmd
->afd
.afhi
)
251 uint8_t needed
, want
;
253 ret
= num_slices(largest
, fc
, &needed
);
256 if (needed
> fc
->fcp
->data_slices_per_group
)
257 PARA_WARNING_LOG("fec parms insufficient for this audio file\n");
258 want
= PARA_MAX(needed
, fc
->fcp
->data_slices_per_group
);
261 fc
->src_data
= para_realloc(fc
->src_data
, want
* sizeof(char *));
262 ret
= fec_new(want
, want
+ fc
->fcp
->slices_per_group
263 - fc
->fcp
->data_slices_per_group
, &fc
->parms
);
267 fc
->num_extra_slices
= 0;
268 if (k
> fc
->fcp
->data_slices_per_group
) {
269 fc
->num_extra_slices
= k
- fc
->fcp
->data_slices_per_group
;
270 PARA_NOTICE_LOG("using %d extra slices\n",
271 fc
->num_extra_slices
);
274 fc
->stream_start
= *now
;
275 fc
->first_stream_chunk
= mmd
->current_chunk
;
276 g
->first_chunk
= mmd
->current_chunk
;
279 g
->first_chunk
+= g
->num_chunks
;
282 if (g
->first_chunk
>= mmd
->afd
.afhi
.chunks_total
)
284 if (need_audio_header(fc
, vsst
)) {
285 ret
= num_slices(vsst
->header_len
, fc
, &g
->num_header_slices
);
289 g
->num_header_slices
= 0;
290 afh_get_chunk(g
->first_chunk
, &mmd
->afd
.afhi
, vsst
->map
, &start_buf
,
292 data_slices
= k
- g
->num_header_slices
;
294 max_data_size
= slice_bytes
* data_slices
;
296 for (i
= g
->first_chunk
; i
< mmd
->afd
.afhi
.chunks_total
; i
++) {
297 afh_get_chunk(i
, &mmd
->afd
.afhi
, vsst
->map
, &buf
, &len
);
298 if (g
->bytes
+ len
> max_data_size
)
302 g
->num_chunks
= i
- g
->first_chunk
;
303 assert(g
->num_chunks
);
304 fc
->current_slice_num
= 0;
306 /* setup header slices */
307 buf
= vsst
->header_buf
;
308 for (i
= 0; i
< g
->num_header_slices
; i
++) {
309 fc
->src_data
[i
] = (const unsigned char *)buf
;
313 /* setup data slices */
315 for (i
= g
->num_header_slices
; i
< k
; i
++) {
316 if (buf
+ slice_bytes
> vsst
->map
+ mmd
->size
)
318 * Can not use the memory mapped audio file for this
319 * slice as it goes beyond the map. This slice will not
323 fc
->src_data
[i
] = (const unsigned char *)buf
;
327 uint32_t payload_size
= vsst
->map
+ mmd
->size
- buf
;
328 memcpy(fc
->extra_src_buf
, buf
, payload_size
);
329 fc
->src_data
[i
] = fc
->extra_src_buf
;
331 /* use arbitrary data for all remaining slices */
334 fc
->src_data
[i
] = (const unsigned char *)buf
;
337 /* setup group timing */
338 tv_scale(g
->first_chunk
- fc
->first_stream_chunk
, chunk_tv
, &tmp
);
339 tv_add(&fc
->stream_start
, &tmp
, &g
->start
);
340 if (g
->num
) /* quick hack to avoid buffer underruns */
342 tv_scale(g
->num_chunks
, chunk_tv
, &tmp
); /* group duration */
343 tv_divide(fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
,
344 &tmp
, &g
->slice_duration
);
346 PARA_DEBUG_LOG("FEC group %d: %d chunks (%d - %d), %d header slices, %d data slices\n",
347 g
->num
, g
->num_chunks
, g
->first_chunk
,
348 g
->first_chunk
+ g
->num_chunks
- 1,
349 g
->num_header_slices
, data_slices
351 PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
352 tv2ms(&tmp
), tv2ms(chunk_tv
), tv2ms(&g
->slice_duration
));
356 static int compute_next_fec_slice(struct fec_client
*fc
, struct vss_task
*vsst
)
358 assert(fc
->error
>= 0);
359 if (fc
->first_stream_chunk
< 0 || fc
->current_slice_num
360 == fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
) {
361 int ret
= setup_next_fec_group(fc
, vsst
);
365 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
366 PARA_ERROR_LOG("FEC client temporarily disabled\n");
371 write_fec_header(fc
, vsst
);
372 fec_encode(fc
->parms
, fc
->src_data
, fc
->enc_buf
+ FEC_HEADER_SIZE
,
373 fc
->current_slice_num
,
374 fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
);
379 * Return a buffer that marks the end of the stream.
381 * \param buf Result pointer.
382 * \return The length of the eof buffer.
384 * This is used for (multicast) udp streaming where closing the socket on the
385 * sender might not give rise to an eof condition at the peer.
387 size_t vss_get_fec_eof_packet(const char **buf
)
389 static const char fec_eof_packet
[FEC_HEADER_SIZE
] = FEC_EOF_PACKET
;
390 *buf
= fec_eof_packet
;
391 return FEC_HEADER_SIZE
;
395 * Add one entry to the list of active fec clients.
397 * \param fcp Describes the fec parameters to be used for this client.
398 * \param result An opaque pointer that must be used by remove the client later.
402 int vss_add_fec_client(struct fec_client_parms
*fcp
, struct fec_client
**result
)
405 struct fec_client
*fc
;
407 if (fcp
->max_slice_bytes
< FEC_HEADER_SIZE
+ fcp
->data_slices_per_group
)
408 return -ERRNO_TO_PARA_ERROR(EINVAL
);
409 fc
= para_calloc(sizeof(*fc
));
411 ret
= fec_new(fcp
->data_slices_per_group
, fcp
->slices_per_group
,
415 fc
->first_stream_chunk
= -1; /* stream not yet started */
416 fc
->src_data
= para_malloc(fc
->fcp
->slices_per_group
* sizeof(char *));
417 fc
->enc_buf
= para_calloc(fc
->fcp
->max_slice_bytes
);
418 fc
->num_extra_slices
= 0;
419 fc
->extra_src_buf
= para_calloc(fc
->fcp
->max_slice_bytes
);
420 fc
->next_header_time
.tv_sec
= 0;
421 para_list_add(&fc
->node
, &fec_client_list
);
432 * Remove one entry from the list of active fec clients.
434 * \param fc The client to be removed.
436 void vss_del_fec_client(struct fec_client
*fc
)
441 free(fc
->extra_src_buf
);
447 * Compute if/when next slice is due. If it isn't due yet and \a diff is
448 * not \p Null, compute the time difference next - now, where
450 * next = stream_start + (first_group_chunk - first_stream_chunk)
451 * * chunk_time + slice_num * slice_time
453 static int next_slice_is_due(struct fec_client
*fc
, struct timeval
*diff
)
455 struct timeval tmp
, next
;
458 if (fc
->first_stream_chunk
< 0)
460 tv_scale(fc
->current_slice_num
, &fc
->group
.slice_duration
, &tmp
);
461 tv_add(&tmp
, &fc
->group
.start
, &next
);
462 ret
= tv_diff(&next
, now
, diff
);
463 return ret
< 0? 1 : 0;
466 static void compute_slice_timeout(struct timeval
*timeout
)
468 struct fec_client
*fc
;
470 assert(vss_playing());
471 list_for_each_entry(fc
, &fec_client_list
, node
) {
476 if (next_slice_is_due(fc
, &diff
)) {
478 timeout
->tv_usec
= 0;
481 /* timeout = min(timeout, diff) */
482 if (tv_diff(&diff
, timeout
, NULL
) < 0)
487 static void set_eof_barrier(struct vss_task
*vsst
)
489 struct fec_client
*fc
;
490 struct timeval timeout
= {1, 0}, *chunk_tv
= vss_chunk_time();
494 list_for_each_entry(fc
, &fec_client_list
, node
) {
495 struct timeval group_duration
;
499 tv_scale(fc
->group
.num_chunks
, chunk_tv
, &group_duration
);
500 if (tv_diff(&timeout
, &group_duration
, NULL
) < 0)
501 timeout
= group_duration
;
504 tv_add(now
, &timeout
, &vsst
->eof_barrier
);
508 * Check if vss status flag \a P (playing) is set.
510 * \return Greater than zero if playing, zero otherwise.
513 unsigned int vss_playing(void)
515 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
519 * Check if the \a N (next) status flag is set.
521 * \return Greater than zero if set, zero if not.
524 unsigned int vss_next(void)
526 return mmd
->new_vss_status_flags
& VSS_NEXT
;
530 * Check if a reposition request is pending.
532 * \return Greater than zero if true, zero otherwise.
535 unsigned int vss_repos(void)
537 return mmd
->new_vss_status_flags
& VSS_REPOS
;
541 * Check if the vss is currently paused.
543 * \return Greater than zero if paused, zero otherwise.
546 unsigned int vss_paused(void)
548 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
549 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
553 * Check if the vss is currently stopped.
555 * \return Greater than zero if paused, zero otherwise.
558 unsigned int vss_stopped(void)
560 return (mmd
->new_vss_status_flags
& VSS_NEXT
)
561 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
564 static int chk_barrier(const char *bname
, const struct timeval
*barrier
,
565 struct timeval
*diff
, int print_log
)
569 if (tv_diff(now
, barrier
, diff
) > 0)
573 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
578 * != NULL: timeout for next chunk
579 * NULL: nothing to do
581 static struct timeval
*vss_compute_timeout(struct vss_task
*vsst
)
583 static struct timeval the_timeout
;
584 struct timeval next_chunk
;
586 if (vss_next() && vsst
->map
) {
587 /* only sleep a bit, nec*/
588 the_timeout
.tv_sec
= 0;
589 the_timeout
.tv_usec
= 100;
592 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
593 &the_timeout
, 1) < 0)
595 if (chk_barrier("eof", &vsst
->eof_barrier
, &the_timeout
, 1) < 0)
597 if (chk_barrier("data send", &vsst
->data_send_barrier
,
598 &the_timeout
, 1) < 0)
600 if (!vss_playing() || !vsst
->map
)
602 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
603 &mmd
->stream_start
, &next_chunk
);
604 if (chk_barrier("chunk", &next_chunk
, &the_timeout
, 0) >= 0) {
605 /* chunk is due or bof */
606 the_timeout
.tv_sec
= 0;
607 the_timeout
.tv_usec
= 0;
610 /* compute min of current timeout and next slice time */
611 compute_slice_timeout(&the_timeout
);
615 static void vss_eof(struct vss_task
*vsst
)
620 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
621 mmd
->new_vss_status_flags
= VSS_NEXT
;
622 set_eof_barrier(vsst
);
623 para_munmap(vsst
->map
, mmd
->size
);
625 mmd
->chunks_sent
= 0;
627 mmd
->afd
.afhi
.seconds_total
= 0;
628 mmd
->afd
.afhi
.chunk_tv
.tv_sec
= 0;
629 mmd
->afd
.afhi
.chunk_tv
.tv_usec
= 0;
630 free(mmd
->afd
.afhi
.chunk_table
);
631 mmd
->afd
.afhi
.chunk_table
= NULL
;
638 * Get the list of all supported audio formats.
640 * \return Aa space separated list of all supported audio formats
641 * It is not allocated at runtime, i.e. there is no need to free
642 * the returned string in the caller.
644 const char *supported_audio_formats(void)
646 return SUPPORTED_AUDIO_FORMATS
;
649 static int need_to_request_new_audio_file(struct vss_task
*vsst
)
653 if (vsst
->map
) /* have audio file */
655 if (!vss_playing()) /* don't need one */
657 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
659 if (vsst
->afsss
== AFS_SOCKET_AFD_PENDING
) /* already requested one */
661 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
668 * Compute the timeout for the main select-loop of the scheduler.
670 * \param s Pointer to the server scheduler.
671 * \param t Pointer to the vss task structure.
673 * Before the timeout is computed, the current vss status flags are evaluated
674 * and acted upon by calling appropriate functions from the lower layers.
675 * Possible actions include
677 * - request a new audio file from afs,
678 * - shutdown of all senders (stop/pause command),
679 * - reposition the stream (ff/jmp command).
681 static void vss_pre_select(struct sched
*s
, struct task
*t
)
684 struct timeval
*tv
, diff
;
685 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
687 if (!vsst
->map
|| vss_next() || vss_paused() || vss_repos()) {
688 struct fec_client
*fc
, *tmp
;
689 for (i
= 0; senders
[i
].name
; i
++)
690 if (senders
[i
].shutdown_clients
)
691 senders
[i
].shutdown_clients();
692 list_for_each_entry_safe(fc
, tmp
, &fec_client_list
, node
) {
693 fc
->first_stream_chunk
= -1;
696 mmd
->stream_start
.tv_sec
= 0;
697 mmd
->stream_start
.tv_usec
= 0;
701 else if (vss_paused()) {
702 if (mmd
->chunks_sent
)
703 set_eof_barrier(vsst
);
704 mmd
->chunks_sent
= 0;
705 } else if (vss_repos()) {
706 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
707 set_eof_barrier(vsst
);
708 mmd
->chunks_sent
= 0;
709 mmd
->current_chunk
= mmd
->repos_request
;
710 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
712 if (need_to_request_new_audio_file(vsst
)) {
713 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
714 para_fd_set(vsst
->afs_socket
, &s
->wfds
, &s
->max_fileno
);
715 vsst
->afsss
= AFS_SOCKET_CHECK_FOR_WRITE
;
717 para_fd_set(vsst
->afs_socket
, &s
->rfds
, &s
->max_fileno
);
718 for (i
= 0; senders
[i
].name
; i
++) {
719 if (!senders
[i
].pre_select
)
721 senders
[i
].pre_select(&s
->max_fileno
, &s
->rfds
, &s
->wfds
);
723 tv
= vss_compute_timeout(vsst
);
724 if (tv
&& tv_diff(tv
, &s
->timeout
, &diff
) < 0)
728 static int recv_afs_msg(int afs_socket
, int *fd
, uint32_t *code
, uint32_t *data
)
730 char control
[255], buf
[8];
731 struct msghdr msg
= {.msg_iov
= NULL
};
732 struct cmsghdr
*cmsg
;
738 iov
.iov_len
= sizeof(buf
);
741 msg
.msg_control
= control
;
742 msg
.msg_controllen
= sizeof(control
);
743 memset(buf
, 0, sizeof(buf
));
744 ret
= recvmsg(afs_socket
, &msg
, 0);
746 return -ERRNO_TO_PARA_ERROR(errno
);
747 if (iov
.iov_len
!= sizeof(buf
))
748 return -E_AFS_SHORT_READ
;
749 *code
= *(uint32_t*)buf
;
750 *data
= *(uint32_t*)(buf
+ 4);
751 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
752 if (cmsg
->cmsg_level
!= SOL_SOCKET
753 || cmsg
->cmsg_type
!= SCM_RIGHTS
)
755 if ((cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int) != 1)
757 *fd
= *(int *)CMSG_DATA(cmsg
);
762 static void recv_afs_result(struct vss_task
*vsst
)
764 int ret
, passed_fd
, shmid
;
765 uint32_t afs_code
= 0, afs_data
= 0;
768 vsst
->afsss
= AFS_SOCKET_READY
;
769 ret
= recv_afs_msg(vsst
->afs_socket
, &passed_fd
, &afs_code
, &afs_data
);
772 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd
, afs_code
,
775 if (afs_code
!= NEXT_AUDIO_FILE
)
780 ret
= load_afd(shmid
, &mmd
->afd
);
784 ret
= fstat(passed_fd
, &statbuf
);
786 PARA_ERROR_LOG("fstat error:\n");
787 ret
= -ERRNO_TO_PARA_ERROR(errno
);
790 mmd
->size
= statbuf
.st_size
;
791 mmd
->mtime
= statbuf
.st_mtime
;
792 ret
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
, passed_fd
,
797 mmd
->chunks_sent
= 0;
798 mmd
->current_chunk
= 0;
802 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
803 afh_get_header(&mmd
->afd
.afhi
, vsst
->map
, &vsst
->header_buf
,
807 free(mmd
->afd
.afhi
.chunk_table
);
810 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
811 mmd
->new_vss_status_flags
= VSS_NEXT
;
815 * Main sending function.
817 * This function gets called from vss_post_select(). It checks whether the next
818 * chunk of data should be pushed out. It obtains a pointer to the data to be
819 * sent out as well as its length from mmd->afd.afhi. This information is then
820 * passed to each supported sender's send() function as well as to the send()
821 * functions of each registered fec client.
823 static void vss_send(struct vss_task
*vsst
)
825 int i
, sent_something
= 0;
827 struct fec_client
*fc
, *tmp_fc
;
829 if (!vsst
->map
|| !vss_playing())
831 if (chk_barrier("eof", &vsst
->eof_barrier
, &due
, 1) < 0)
833 if (chk_barrier("data send", &vsst
->data_send_barrier
,
836 list_for_each_entry_safe(fc
, tmp_fc
, &fec_client_list
, node
) {
839 if (!next_slice_is_due(fc
, NULL
))
841 if (compute_next_fec_slice(fc
, vsst
) <= 0)
843 PARA_DEBUG_LOG("sending %d:%d (%u bytes)\n", fc
->group
.num
,
844 fc
->current_slice_num
, fc
->fcp
->max_slice_bytes
);
845 fc
->fcp
->send((char *)fc
->enc_buf
,
846 fc
->fcp
->max_slice_bytes
,
847 fc
->fcp
->private_data
);
848 fc
->current_slice_num
++;
851 if (mmd
->current_chunk
>= mmd
->afd
.afhi
.chunks_total
) { /* eof */
853 mmd
->new_vss_status_flags
|= VSS_NEXT
;
856 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
857 &mmd
->stream_start
, &due
);
858 if (tv_diff(&due
, now
, NULL
) <= 0) {
862 if (!mmd
->chunks_sent
) {
864 mmd
->stream_start
= *now
;
865 tv_scale(mmd
->current_chunk
, &mmd
->afd
.afhi
.chunk_tv
, &tmp
);
866 mmd
->offset
= tv2ms(&tmp
);
870 * We call the send function also in case of empty chunks as
871 * they might have still some data queued which can be sent in
874 afh_get_chunk(mmd
->current_chunk
, &mmd
->afd
.afhi
, vsst
->map
,
876 for (i
= 0; senders
[i
].name
; i
++) {
877 if (!senders
[i
].send
)
879 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
,
880 buf
, len
, vsst
->header_buf
, vsst
->header_len
);
883 mmd
->current_chunk
++;
887 static void vss_post_select(struct sched
*s
, struct task
*t
)
890 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
893 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
894 int num
= mmd
->sender_cmd_data
.cmd_num
,
895 sender_num
= mmd
->sender_cmd_data
.sender_num
;
897 if (senders
[sender_num
].client_cmds
[num
])
898 senders
[sender_num
].client_cmds
[num
](&mmd
->sender_cmd_data
);
899 mmd
->sender_cmd_data
.cmd_num
= -1;
901 if (vsst
->afsss
!= AFS_SOCKET_CHECK_FOR_WRITE
) {
902 if (FD_ISSET(vsst
->afs_socket
, &s
->rfds
))
903 recv_afs_result(vsst
);
904 } else if (FD_ISSET(vsst
->afs_socket
, &s
->wfds
)) {
905 PARA_NOTICE_LOG("requesting new fd from afs\n");
906 ret
= send_buffer(vsst
->afs_socket
, "new");
908 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
910 vsst
->afsss
= AFS_SOCKET_AFD_PENDING
;
912 for (i
= 0; senders
[i
].name
; i
++) {
913 if (!senders
[i
].post_select
)
915 senders
[i
].post_select(&s
->rfds
, &s
->wfds
);
917 if ((vss_playing() && !(mmd
->vss_status_flags
& VSS_PLAYING
)) ||
918 (vss_next() && vss_playing()))
919 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
924 * Initialize the virtual streaming system task.
926 * \param afs_socket The fd for communication with afs.
928 * This also initializes all supported senders and starts streaming
929 * if the --autoplay command line flag was given.
931 void init_vss_task(int afs_socket
)
933 static struct vss_task vss_task_struct
, *vsst
= &vss_task_struct
;
935 char *hn
= para_hostname(), *home
= para_homedir();
936 long unsigned announce_time
= conf
.announce_time_arg
> 0?
937 conf
.announce_time_arg
: 300,
938 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
939 conf
.autoplay_delay_arg
: 0;
940 vsst
->header_interval
.tv_sec
= 5; /* should this be configurable? */
941 vsst
->afs_socket
= afs_socket
;
942 vsst
->task
.pre_select
= vss_pre_select
;
943 vsst
->task
.post_select
= vss_post_select
;
944 ms2tv(announce_time
, &vsst
->announce_tv
);
945 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst
->announce_tv
));
946 INIT_LIST_HEAD(&fec_client_list
);
947 for (i
= 0; senders
[i
].name
; i
++) {
948 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
949 senders
[i
].init(&senders
[i
]);
953 mmd
->sender_cmd_data
.cmd_num
= -1;
954 if (conf
.autoplay_given
) {
956 mmd
->vss_status_flags
|= VSS_PLAYING
;
957 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
958 ms2tv(autoplay_delay
, &tmp
);
959 tv_add(now
, &tmp
, &vsst
->autoplay_barrier
);
960 tv_add(&vsst
->autoplay_barrier
, &vsst
->announce_tv
,
961 &vsst
->data_send_barrier
);
963 register_task(&vsst
->task
);