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 uint8_t num_header_slices
;
131 * Describes one connected FEC client.
134 /** If negative, this client is temporarily disabled. */
136 /** Parameters requested by the client. */
137 struct fec_client_parms
*fcp
;
138 /** Used by the core FEC code. */
139 struct fec_parms
*parms
;
140 /** The position of this client in the fec client list. */
141 struct list_head node
;
142 /** When the first slice for this client was sent. */
143 struct timeval stream_start
;
144 /** The first chunk sent to this FEC client. */
145 int first_stream_chunk
;
146 /** Describes the current group. */
147 struct fec_group group
;
148 /** The current slice. */
149 uint8_t current_slice_num
;
150 /** The data to be FEC-encoded (point to a region within the mapped audio file). */
151 const unsigned char **src_data
;
152 /** Last time an audio header was sent. */
153 struct timeval next_header_time
;
154 /** Used for the last source pointer of an audio file. */
155 unsigned char *extra_src_buf
;
156 /** Extra slices needed to store largest chunk + header. */
157 int num_extra_slices
;
158 /** Contains the FEC-encoded data. */
159 unsigned char *enc_buf
;
163 * Get the chunk time of the current audio file.
165 * \return A pointer to a struct containing the chunk time, or NULL,
166 * if currently no audio file is selected.
168 struct timeval
*vss_chunk_time(void)
170 if (mmd
->afd
.afhi
.chunk_tv
.tv_sec
== 0 &&
171 mmd
->afd
.afhi
.chunk_tv
.tv_usec
== 0)
173 return &mmd
->afd
.afhi
.chunk_tv
;
177 * Write a fec header to a buffer.
179 * \param buf The buffer to write to.
180 * \param h The fec header to write.
182 static void write_fec_header(struct fec_client
*fc
, struct vss_task
*vsst
)
184 char *buf
= (char *)fc
->enc_buf
;
185 struct fec_group
*g
= &fc
->group
;
186 struct fec_client_parms
*p
= fc
->fcp
;
188 write_u32(buf
, FEC_MAGIC
);
190 write_u8(buf
+ 4, p
->slices_per_group
+ fc
->num_extra_slices
);
191 write_u8(buf
+ 5, p
->data_slices_per_group
+ fc
->num_extra_slices
);
192 write_u32(buf
+ 6, g
->num_header_slices
? vsst
->header_len
: 0);
194 write_u32(buf
+ 10, g
->num
);
195 write_u32(buf
+ 14, g
->bytes
);
197 write_u8(buf
+ 18, fc
->current_slice_num
);
198 write_u16(buf
+ 20, p
->max_slice_bytes
- FEC_HEADER_SIZE
);
199 write_u8(buf
+ 22, g
->first_chunk
? 0 : 1);
200 write_u8(buf
+ 23, vsst
->header_len
? 1 : 0);
201 memset(buf
+ 24, 0, 7);
204 static int need_audio_header(struct fec_client
*fc
, struct vss_task
*vsst
)
206 if (!mmd
->current_chunk
) {
207 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
210 if (!vsst
->header_buf
)
212 if (!vsst
->header_len
)
214 if (fc
->group
.num
&& tv_diff(&fc
->next_header_time
, now
, NULL
) > 0)
216 tv_add(now
, &vsst
->header_interval
, &fc
->next_header_time
);
220 static int num_slices(long unsigned bytes
, struct fec_client
*fc
, uint8_t *result
)
222 unsigned long m
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
223 unsigned rv
, redundant_slices
= fc
->fcp
->slices_per_group
224 - fc
->fcp
->data_slices_per_group
;
228 rv
= (bytes
+ m
- 1) / m
;
229 if (rv
+ redundant_slices
> 255)
235 static void set_slice_duration(struct fec_client
*fc
, struct fec_group
*g
)
237 struct timeval group_duration
, *chunk_tv
= vss_chunk_time();
239 tv_scale(g
->num_chunks
, chunk_tv
, &group_duration
);
240 tv_divide(fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
,
241 &group_duration
, &g
->slice_duration
);
242 PARA_DEBUG_LOG("durations (group/chunk/slice): %lu/%lu/%lu\n",
243 tv2ms(&group_duration
), tv2ms(chunk_tv
), tv2ms(&g
->slice_duration
));
246 static int setup_next_fec_group(struct fec_client
*fc
, struct vss_task
*vsst
)
248 int ret
, i
, k
, data_slices
;
250 const char *buf
, *start_buf
;
251 struct timeval tmp
, *chunk_tv
= vss_chunk_time();
252 struct fec_group
*g
= &fc
->group
;
253 unsigned slice_bytes
= fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
;
254 uint32_t max_data_size
;
257 k
= fc
->fcp
->data_slices_per_group
+ fc
->num_extra_slices
;
258 if (fc
->first_stream_chunk
< 0) {
259 uint32_t largest
= afh_get_largest_chunk_size(&mmd
->afd
.afhi
)
261 uint8_t needed
, want
;
263 ret
= num_slices(largest
, fc
, &needed
);
266 if (needed
> fc
->fcp
->data_slices_per_group
)
267 PARA_WARNING_LOG("fec parms insufficient for this audio file\n");
268 want
= PARA_MAX(needed
, fc
->fcp
->data_slices_per_group
);
271 fc
->src_data
= para_realloc(fc
->src_data
, want
* sizeof(char *));
272 ret
= fec_new(want
, want
+ fc
->fcp
->slices_per_group
273 - fc
->fcp
->data_slices_per_group
, &fc
->parms
);
277 fc
->num_extra_slices
= 0;
278 if (k
> fc
->fcp
->data_slices_per_group
) {
279 fc
->num_extra_slices
= k
- fc
->fcp
->data_slices_per_group
;
280 PARA_NOTICE_LOG("using %d extra slices\n",
281 fc
->num_extra_slices
);
284 fc
->stream_start
= *now
;
285 fc
->first_stream_chunk
= mmd
->current_chunk
;
286 g
->first_chunk
= mmd
->current_chunk
;
289 /* use duration of the previous group for the timing of this group */
290 set_slice_duration(fc
, g
);
291 g
->first_chunk
+= g
->num_chunks
;
294 if (g
->first_chunk
>= mmd
->afd
.afhi
.chunks_total
)
296 if (need_audio_header(fc
, vsst
)) {
297 ret
= num_slices(vsst
->header_len
, fc
, &g
->num_header_slices
);
301 g
->num_header_slices
= 0;
302 afh_get_chunk(g
->first_chunk
, &mmd
->afd
.afhi
, vsst
->map
, &start_buf
,
304 data_slices
= k
- g
->num_header_slices
;
306 max_data_size
= slice_bytes
* data_slices
;
308 for (i
= g
->first_chunk
; i
< mmd
->afd
.afhi
.chunks_total
; i
++) {
309 afh_get_chunk(i
, &mmd
->afd
.afhi
, vsst
->map
, &buf
, &len
);
310 if (g
->bytes
+ len
> max_data_size
)
314 g
->num_chunks
= i
- g
->first_chunk
;
315 assert(g
->num_chunks
);
316 fc
->current_slice_num
= 0;
318 set_slice_duration(fc
, g
);
320 /* setup header slices */
321 buf
= vsst
->header_buf
;
322 for (i
= 0; i
< g
->num_header_slices
; i
++) {
323 fc
->src_data
[i
] = (const unsigned char *)buf
;
327 /* setup data slices */
329 for (i
= g
->num_header_slices
; i
< k
; i
++) {
330 if (buf
+ slice_bytes
> vsst
->map
+ mmd
->size
)
332 * Can not use the memory mapped audio file for this
333 * slice as it goes beyond the map. This slice will not
337 fc
->src_data
[i
] = (const unsigned char *)buf
;
341 uint32_t payload_size
= vsst
->map
+ mmd
->size
- buf
;
342 memcpy(fc
->extra_src_buf
, buf
, payload_size
);
343 fc
->src_data
[i
] = fc
->extra_src_buf
;
345 /* use arbitrary data for all remaining slices */
348 fc
->src_data
[i
] = (const unsigned char *)buf
;
350 PARA_DEBUG_LOG("FEC group %d: %d chunks (%d - %d), "
351 "%d header slices, %d data slices\n",
352 g
->num
, g
->num_chunks
, g
->first_chunk
,
353 g
->first_chunk
+ g
->num_chunks
- 1,
354 g
->num_header_slices
, data_slices
356 /* set group start */
357 if (g
->num
!= 0 && vsst
->header_len
!= 0 && fc
->first_stream_chunk
== 0)
358 /* chunk #0 is the audio file header */
359 tv_scale(g
->first_chunk
- 1, chunk_tv
, &tmp
);
361 tv_scale(g
->first_chunk
- fc
->first_stream_chunk
,
363 tv_add(&fc
->stream_start
, &tmp
, &g
->start
);
367 static int compute_next_fec_slice(struct fec_client
*fc
, struct vss_task
*vsst
)
369 assert(fc
->error
>= 0);
370 if (fc
->first_stream_chunk
< 0 || fc
->current_slice_num
371 == fc
->fcp
->slices_per_group
+ fc
->num_extra_slices
) {
372 int ret
= setup_next_fec_group(fc
, vsst
);
376 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
377 PARA_ERROR_LOG("FEC client temporarily disabled\n");
382 write_fec_header(fc
, vsst
);
383 fec_encode(fc
->parms
, fc
->src_data
, fc
->enc_buf
+ FEC_HEADER_SIZE
,
384 fc
->current_slice_num
,
385 fc
->fcp
->max_slice_bytes
- FEC_HEADER_SIZE
);
390 * Return a buffer that marks the end of the stream.
392 * \param buf Result pointer.
393 * \return The length of the eof buffer.
395 * This is used for (multicast) udp streaming where closing the socket on the
396 * sender might not give rise to an eof condition at the peer.
398 size_t vss_get_fec_eof_packet(const char **buf
)
400 static const char fec_eof_packet
[FEC_HEADER_SIZE
] = FEC_EOF_PACKET
;
401 *buf
= fec_eof_packet
;
402 return FEC_HEADER_SIZE
;
406 * Add one entry to the list of active fec clients.
408 * \param fcp Describes the fec parameters to be used for this client.
409 * \param result An opaque pointer that must be used by remove the client later.
413 int vss_add_fec_client(struct fec_client_parms
*fcp
, struct fec_client
**result
)
416 struct fec_client
*fc
;
418 if (fcp
->max_slice_bytes
< FEC_HEADER_SIZE
+ fcp
->data_slices_per_group
)
419 return -ERRNO_TO_PARA_ERROR(EINVAL
);
420 fc
= para_calloc(sizeof(*fc
));
422 ret
= fec_new(fcp
->data_slices_per_group
, fcp
->slices_per_group
,
426 fc
->first_stream_chunk
= -1; /* stream not yet started */
427 fc
->src_data
= para_malloc(fc
->fcp
->slices_per_group
* sizeof(char *));
428 fc
->enc_buf
= para_calloc(fc
->fcp
->max_slice_bytes
);
429 fc
->num_extra_slices
= 0;
430 fc
->extra_src_buf
= para_calloc(fc
->fcp
->max_slice_bytes
);
431 fc
->next_header_time
.tv_sec
= 0;
432 para_list_add(&fc
->node
, &fec_client_list
);
443 * Remove one entry from the list of active fec clients.
445 * \param fc The client to be removed.
447 void vss_del_fec_client(struct fec_client
*fc
)
452 free(fc
->extra_src_buf
);
458 * Compute if/when next slice is due. If it isn't due yet and \a diff is
459 * not \p Null, compute the time difference next - now, where
461 * next = stream_start + (first_group_chunk - first_stream_chunk)
462 * * chunk_time + slice_num * slice_time
464 static int next_slice_is_due(struct fec_client
*fc
, struct timeval
*diff
)
466 struct timeval tmp
, next
;
469 if (fc
->first_stream_chunk
< 0)
471 tv_scale(fc
->current_slice_num
, &fc
->group
.slice_duration
, &tmp
);
472 tv_add(&tmp
, &fc
->group
.start
, &next
);
473 ret
= tv_diff(&next
, now
, diff
);
474 return ret
< 0? 1 : 0;
477 static void compute_slice_timeout(struct timeval
*timeout
)
479 struct fec_client
*fc
;
481 assert(vss_playing());
482 list_for_each_entry(fc
, &fec_client_list
, node
) {
487 if (next_slice_is_due(fc
, &diff
)) {
489 timeout
->tv_usec
= 0;
492 /* timeout = min(timeout, diff) */
493 if (tv_diff(&diff
, timeout
, NULL
) < 0)
498 static void set_eof_barrier(struct vss_task
*vsst
)
500 struct fec_client
*fc
;
501 struct timeval timeout
= mmd
->afd
.afhi
.eof_tv
,
502 *chunk_tv
= vss_chunk_time();
506 list_for_each_entry(fc
, &fec_client_list
, node
) {
507 struct timeval group_duration
;
511 tv_scale(fc
->group
.num_chunks
, chunk_tv
, &group_duration
);
512 if (tv_diff(&timeout
, &group_duration
, NULL
) < 0)
513 timeout
= group_duration
;
516 tv_add(now
, &timeout
, &vsst
->eof_barrier
);
520 * Check if vss status flag \a P (playing) is set.
522 * \return Greater than zero if playing, zero otherwise.
525 unsigned int vss_playing(void)
527 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
531 * Check if the \a N (next) status flag is set.
533 * \return Greater than zero if set, zero if not.
536 unsigned int vss_next(void)
538 return mmd
->new_vss_status_flags
& VSS_NEXT
;
542 * Check if a reposition request is pending.
544 * \return Greater than zero if true, zero otherwise.
547 unsigned int vss_repos(void)
549 return mmd
->new_vss_status_flags
& VSS_REPOS
;
553 * Check if the vss is currently paused.
555 * \return Greater than zero if paused, zero otherwise.
558 unsigned int vss_paused(void)
560 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
561 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
565 * Check if the vss is currently stopped.
567 * \return Greater than zero if paused, zero otherwise.
570 unsigned int vss_stopped(void)
572 return (mmd
->new_vss_status_flags
& VSS_NEXT
)
573 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
576 static int chk_barrier(const char *bname
, const struct timeval
*barrier
,
577 struct timeval
*diff
, int print_log
)
581 if (tv_diff(now
, barrier
, diff
) > 0)
585 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
590 * != NULL: timeout for next chunk
591 * NULL: nothing to do
593 static struct timeval
*vss_compute_timeout(struct vss_task
*vsst
)
595 static struct timeval the_timeout
;
596 struct timeval next_chunk
;
598 if (vss_next() && vsst
->map
) {
599 /* only sleep a bit, nec*/
600 the_timeout
.tv_sec
= 0;
601 the_timeout
.tv_usec
= 100;
604 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
605 &the_timeout
, 1) < 0)
607 if (chk_barrier("eof", &vsst
->eof_barrier
, &the_timeout
, 1) < 0)
609 if (chk_barrier("data send", &vsst
->data_send_barrier
,
610 &the_timeout
, 1) < 0)
612 if (!vss_playing() || !vsst
->map
)
614 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
615 &mmd
->stream_start
, &next_chunk
);
616 if (chk_barrier("chunk", &next_chunk
, &the_timeout
, 0) >= 0) {
617 /* chunk is due or bof */
618 the_timeout
.tv_sec
= 0;
619 the_timeout
.tv_usec
= 0;
622 /* compute min of current timeout and next slice time */
623 compute_slice_timeout(&the_timeout
);
627 static void vss_eof(struct vss_task
*vsst
)
632 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
633 mmd
->new_vss_status_flags
= VSS_NEXT
;
634 set_eof_barrier(vsst
);
635 para_munmap(vsst
->map
, mmd
->size
);
637 mmd
->chunks_sent
= 0;
639 mmd
->afd
.afhi
.seconds_total
= 0;
640 mmd
->afd
.afhi
.chunk_tv
.tv_sec
= 0;
641 mmd
->afd
.afhi
.chunk_tv
.tv_usec
= 0;
642 free(mmd
->afd
.afhi
.chunk_table
);
643 mmd
->afd
.afhi
.chunk_table
= NULL
;
644 free(mmd
->afd
.afhi
.info_string
);
645 mmd
->afd
.afhi
.info_string
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_FILE_INFO
],
646 status_item_list
[SI_TAGINFO1
], status_item_list
[SI_TAGINFO2
]);
647 make_empty_status_items(mmd
->afd
.verbose_ls_output
);
654 * Get the list of all supported audio formats.
656 * \return Aa space separated list of all supported audio formats
657 * It is not allocated at runtime, i.e. there is no need to free
658 * the returned string in the caller.
660 const char *supported_audio_formats(void)
662 return SUPPORTED_AUDIO_FORMATS
;
665 static int need_to_request_new_audio_file(struct vss_task
*vsst
)
669 if (vsst
->map
) /* have audio file */
671 if (!vss_playing()) /* don't need one */
673 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
675 if (vsst
->afsss
== AFS_SOCKET_AFD_PENDING
) /* already requested one */
677 if (chk_barrier("autoplay_delay", &vsst
->autoplay_barrier
,
684 * Compute the timeout for the main select-loop of the scheduler.
686 * \param s Pointer to the server scheduler.
687 * \param t Pointer to the vss task structure.
689 * Before the timeout is computed, the current vss status flags are evaluated
690 * and acted upon by calling appropriate functions from the lower layers.
691 * Possible actions include
693 * - request a new audio file from afs,
694 * - shutdown of all senders (stop/pause command),
695 * - reposition the stream (ff/jmp command).
697 static void vss_pre_select(struct sched
*s
, struct task
*t
)
700 struct timeval
*tv
, diff
;
701 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
703 if (!vsst
->map
|| vss_next() || vss_paused() || vss_repos()) {
704 struct fec_client
*fc
, *tmp
;
705 for (i
= 0; senders
[i
].name
; i
++)
706 if (senders
[i
].shutdown_clients
)
707 senders
[i
].shutdown_clients();
708 list_for_each_entry_safe(fc
, tmp
, &fec_client_list
, node
) {
709 fc
->first_stream_chunk
= -1;
712 mmd
->stream_start
.tv_sec
= 0;
713 mmd
->stream_start
.tv_usec
= 0;
717 else if (vss_paused()) {
718 if (mmd
->chunks_sent
)
719 set_eof_barrier(vsst
);
720 mmd
->chunks_sent
= 0;
721 } else if (vss_repos()) {
722 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
723 set_eof_barrier(vsst
);
724 mmd
->chunks_sent
= 0;
725 mmd
->current_chunk
= mmd
->repos_request
;
726 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
728 if (need_to_request_new_audio_file(vsst
)) {
729 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
730 para_fd_set(vsst
->afs_socket
, &s
->wfds
, &s
->max_fileno
);
731 vsst
->afsss
= AFS_SOCKET_CHECK_FOR_WRITE
;
733 para_fd_set(vsst
->afs_socket
, &s
->rfds
, &s
->max_fileno
);
734 for (i
= 0; senders
[i
].name
; i
++) {
735 if (!senders
[i
].pre_select
)
737 senders
[i
].pre_select(&s
->max_fileno
, &s
->rfds
, &s
->wfds
);
739 tv
= vss_compute_timeout(vsst
);
740 if (tv
&& tv_diff(tv
, &s
->timeout
, &diff
) < 0)
744 static int recv_afs_msg(int afs_socket
, int *fd
, uint32_t *code
, uint32_t *data
)
746 char control
[255], buf
[8];
747 struct msghdr msg
= {.msg_iov
= NULL
};
748 struct cmsghdr
*cmsg
;
754 iov
.iov_len
= sizeof(buf
);
757 msg
.msg_control
= control
;
758 msg
.msg_controllen
= sizeof(control
);
759 memset(buf
, 0, sizeof(buf
));
760 ret
= recvmsg(afs_socket
, &msg
, 0);
762 return -ERRNO_TO_PARA_ERROR(errno
);
763 if (iov
.iov_len
!= sizeof(buf
))
764 return -E_AFS_SHORT_READ
;
765 *code
= *(uint32_t*)buf
;
766 *data
= *(uint32_t*)(buf
+ 4);
767 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
768 if (cmsg
->cmsg_level
!= SOL_SOCKET
769 || cmsg
->cmsg_type
!= SCM_RIGHTS
)
771 if ((cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int) != 1)
773 *fd
= *(int *)CMSG_DATA(cmsg
);
778 static void recv_afs_result(struct vss_task
*vsst
)
780 int ret
, passed_fd
, shmid
;
781 uint32_t afs_code
= 0, afs_data
= 0;
784 vsst
->afsss
= AFS_SOCKET_READY
;
785 ret
= recv_afs_msg(vsst
->afs_socket
, &passed_fd
, &afs_code
, &afs_data
);
788 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd
, afs_code
,
791 if (afs_code
!= NEXT_AUDIO_FILE
)
796 free(mmd
->afd
.afhi
.info_string
);
797 ret
= load_afd(shmid
, &mmd
->afd
);
801 ret
= fstat(passed_fd
, &statbuf
);
803 PARA_ERROR_LOG("fstat error:\n");
804 ret
= -ERRNO_TO_PARA_ERROR(errno
);
807 mmd
->size
= statbuf
.st_size
;
808 mmd
->mtime
= statbuf
.st_mtime
;
809 ret
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
, passed_fd
,
814 mmd
->chunks_sent
= 0;
815 mmd
->current_chunk
= 0;
819 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
820 afh_get_header(&mmd
->afd
.afhi
, vsst
->map
, &vsst
->header_buf
,
824 free(mmd
->afd
.afhi
.chunk_table
);
827 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
828 mmd
->new_vss_status_flags
= VSS_NEXT
;
832 * Main sending function.
834 * This function gets called from vss_post_select(). It checks whether the next
835 * chunk of data should be pushed out. It obtains a pointer to the data to be
836 * sent out as well as its length from mmd->afd.afhi. This information is then
837 * passed to each supported sender's send() function as well as to the send()
838 * functions of each registered fec client.
840 static void vss_send(struct vss_task
*vsst
)
842 int i
, fec_active
= 0;
844 struct fec_client
*fc
, *tmp_fc
;
846 if (!vsst
->map
|| !vss_playing())
848 if (chk_barrier("eof", &vsst
->eof_barrier
, &due
, 1) < 0)
850 if (chk_barrier("data send", &vsst
->data_send_barrier
,
853 list_for_each_entry_safe(fc
, tmp_fc
, &fec_client_list
, node
) {
856 if (!next_slice_is_due(fc
, NULL
)) {
860 if (compute_next_fec_slice(fc
, vsst
) <= 0)
862 PARA_DEBUG_LOG("sending %d:%d (%u bytes)\n", fc
->group
.num
,
863 fc
->current_slice_num
, fc
->fcp
->max_slice_bytes
);
864 fc
->fcp
->send((char *)fc
->enc_buf
,
865 fc
->fcp
->max_slice_bytes
,
866 fc
->fcp
->private_data
);
867 fc
->current_slice_num
++;
870 if (mmd
->current_chunk
>= mmd
->afd
.afhi
.chunks_total
) { /* eof */
872 mmd
->new_vss_status_flags
|= VSS_NEXT
;
875 compute_chunk_time(mmd
->chunks_sent
, &mmd
->afd
.afhi
.chunk_tv
,
876 &mmd
->stream_start
, &due
);
877 if (tv_diff(&due
, now
, NULL
) <= 0) {
881 if (!mmd
->chunks_sent
) {
883 mmd
->stream_start
= *now
;
884 tv_scale(mmd
->current_chunk
, &mmd
->afd
.afhi
.chunk_tv
, &tmp
);
885 mmd
->offset
= tv2ms(&tmp
);
889 * We call the send function also in case of empty chunks as
890 * they might have still some data queued which can be sent in
893 afh_get_chunk(mmd
->current_chunk
, &mmd
->afd
.afhi
, vsst
->map
,
895 for (i
= 0; senders
[i
].name
; i
++) {
896 if (!senders
[i
].send
)
898 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
,
899 buf
, len
, vsst
->header_buf
, vsst
->header_len
);
902 mmd
->current_chunk
++;
906 static void vss_post_select(struct sched
*s
, struct task
*t
)
909 struct vss_task
*vsst
= container_of(t
, struct vss_task
, task
);
912 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
913 int num
= mmd
->sender_cmd_data
.cmd_num
,
914 sender_num
= mmd
->sender_cmd_data
.sender_num
;
916 if (senders
[sender_num
].client_cmds
[num
])
917 senders
[sender_num
].client_cmds
[num
](&mmd
->sender_cmd_data
);
918 mmd
->sender_cmd_data
.cmd_num
= -1;
920 if (vsst
->afsss
!= AFS_SOCKET_CHECK_FOR_WRITE
) {
921 if (FD_ISSET(vsst
->afs_socket
, &s
->rfds
))
922 recv_afs_result(vsst
);
923 } else if (FD_ISSET(vsst
->afs_socket
, &s
->wfds
)) {
924 PARA_NOTICE_LOG("requesting new fd from afs\n");
925 ret
= send_buffer(vsst
->afs_socket
, "new");
927 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
929 vsst
->afsss
= AFS_SOCKET_AFD_PENDING
;
931 for (i
= 0; senders
[i
].name
; i
++) {
932 if (!senders
[i
].post_select
)
934 senders
[i
].post_select(&s
->rfds
, &s
->wfds
);
936 if ((vss_playing() && !(mmd
->vss_status_flags
& VSS_PLAYING
)) ||
937 (vss_next() && vss_playing()))
938 tv_add(now
, &vsst
->announce_tv
, &vsst
->data_send_barrier
);
943 * Initialize the virtual streaming system task.
945 * \param afs_socket The fd for communication with afs.
947 * This also initializes all supported senders and starts streaming
948 * if the --autoplay command line flag was given.
950 void init_vss_task(int afs_socket
)
952 static struct vss_task vss_task_struct
, *vsst
= &vss_task_struct
;
954 char *hn
= para_hostname(), *home
= para_homedir();
955 long unsigned announce_time
= conf
.announce_time_arg
> 0?
956 conf
.announce_time_arg
: 300,
957 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
958 conf
.autoplay_delay_arg
: 0;
959 vsst
->header_interval
.tv_sec
= 5; /* should this be configurable? */
960 vsst
->afs_socket
= afs_socket
;
961 vsst
->task
.pre_select
= vss_pre_select
;
962 vsst
->task
.post_select
= vss_post_select
;
963 ms2tv(announce_time
, &vsst
->announce_tv
);
964 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst
->announce_tv
));
965 INIT_LIST_HEAD(&fec_client_list
);
966 for (i
= 0; senders
[i
].name
; i
++) {
967 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
968 senders
[i
].init(&senders
[i
]);
972 mmd
->sender_cmd_data
.cmd_num
= -1;
973 make_empty_status_items(mmd
->afd
.verbose_ls_output
);
974 if (conf
.autoplay_given
) {
976 mmd
->vss_status_flags
|= VSS_PLAYING
;
977 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
978 ms2tv(autoplay_delay
, &tmp
);
979 tv_add(now
, &tmp
, &vsst
->autoplay_barrier
);
980 tv_add(&vsst
->autoplay_barrier
, &vsst
->announce_tv
,
981 &vsst
->data_send_barrier
);
983 register_task(&vsst
->task
);