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
23 #include "server.cmdline.h"
31 extern struct misc_meta_data *mmd;
33 extern void dccp_send_init(struct sender *);
34 extern void http_send_init(struct sender *);
35 extern void udp_send_init(struct sender *);
37 /** The list of supported senders. */
38 struct sender senders[] = {
41 .init = http_send_init,
45 .init = dccp_send_init,
49 .init = udp_send_init,
56 /** The possible states of the afs socket. */
57 enum afs_socket_status {
58 /** Socket is inactive. */
60 /** Socket fd was included in the write fd set for select(). */
61 AFS_SOCKET_CHECK_FOR_WRITE,
62 /** vss wrote a request to the socket and waits for reply from afs. */
63 AFS_SOCKET_AFD_PENDING
66 /** The task structure for the virtual streaming system. */
68 /** Copied from the -announce_time command line option. */
69 struct timeval announce_tv;
70 /** End of the announcing interval. */
71 struct timeval data_send_barrier;
72 /** End of the EOF interval. */
73 struct timeval eof_barrier;
74 /** Only used if --autoplay_delay was given. */
75 struct timeval autoplay_barrier;
76 /** Used for afs-server communication. */
78 /** The current state of \a afs_socket. */
79 enum afs_socket_status afsss;
80 /** The memory mapped audio file. */
82 /** Used by the scheduler. */
84 /** Pointer to the header of the mapped audio file. */
85 const char *header_buf;
86 /** Length of the audio file header. */
91 * Check if vss status flag \a P (playing) is set.
93 * \return Greater than zero if playing, zero otherwise.
96 unsigned int vss_playing(void)
98 return mmd->new_vss_status_flags & VSS_PLAYING;
102 * Check if the \a N (next) status flag is set.
104 * \return Greater than zero if set, zero if not.
107 unsigned int vss_next(void)
109 return mmd->new_vss_status_flags & VSS_NEXT;
113 * Check if a reposition request is pending.
115 * \return Greater than zero if true, zero otherwise.
118 unsigned int vss_repos(void)
120 return mmd->new_vss_status_flags & VSS_REPOS;
124 * Check if the vss is currently paused.
126 * \return Greater than zero if paused, zero otherwise.
129 unsigned int vss_paused(void)
131 return !(mmd->new_vss_status_flags & VSS_NEXT)
132 && !(mmd->new_vss_status_flags & VSS_PLAYING);
136 * Check if the vss is currently stopped.
138 * \return Greater than zero if paused, zero otherwise.
141 unsigned int vss_stopped(void)
143 return (mmd->new_vss_status_flags & VSS_NEXT)
144 && !(mmd->new_vss_status_flags & VSS_PLAYING);
147 static int chk_barrier(const char *bname, const struct timeval *barrier,
148 struct timeval *diff, int print_log)
152 if (tv_diff(now, barrier, diff) > 0)
156 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
161 * != NULL: timeout for next chunk
162 * NULL: nothing to do
164 static struct timeval *vss_compute_timeout(struct vss_task *vsst)
166 static struct timeval the_timeout;
167 struct timeval next_chunk;
169 if (vss_next() && vsst->map) {
170 /* only sleep a bit, nec*/
171 the_timeout.tv_sec = 0;
172 the_timeout.tv_usec = 100;
175 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
176 &the_timeout, 1) < 0)
178 if (chk_barrier("eof", &vsst->eof_barrier, &the_timeout, 1) < 0)
180 if (chk_barrier("data send", &vsst->data_send_barrier,
181 &the_timeout, 1) < 0)
183 if (!vss_playing() || !vsst->map)
185 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
186 &mmd->stream_start, &next_chunk);
187 if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) < 0)
189 /* chunk is due or bof */
190 the_timeout.tv_sec = 0;
191 the_timeout.tv_usec = 0;
195 static void vss_eof(struct vss_task *vsst)
197 mmd->stream_start = *now;
200 if (mmd->new_vss_status_flags & VSS_NOMORE)
201 mmd->new_vss_status_flags = VSS_NEXT;
202 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
203 para_munmap(vsst->map, mmd->size);
205 mmd->chunks_sent = 0;
207 mmd->afd.afhi.seconds_total = 0;
208 mmd->afd.afhi.chunk_tv.tv_sec = 0;
209 mmd->afd.afhi.chunk_tv.tv_usec = 0;
210 free(mmd->afd.afhi.chunk_table);
211 mmd->afd.afhi.chunk_table = NULL;
212 free(mmd->afd.afhi.info_string);
213 mmd->afd.afhi.info_string = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
214 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
215 make_empty_status_items(mmd->afd.verbose_ls_output);
222 * Get the list of all supported audio formats.
224 * \return Aa space separated list of all supported audio formats
225 * It is not allocated at runtime, i.e. there is no need to free
226 * the returned string in the caller.
228 const char *supported_audio_formats(void)
230 return SUPPORTED_AUDIO_FORMATS;
234 * Get the chunk time of the current audio file.
236 * \return A pointer to a struct containing the chunk time, or NULL,
237 * if currently no audio file is selected.
239 struct timeval *vss_chunk_time(void)
241 if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
242 mmd->afd.afhi.chunk_tv.tv_usec == 0)
244 return &mmd->afd.afhi.chunk_tv;
247 static int need_to_request_new_audio_file(struct vss_task *vsst)
251 if (vsst->map) /* have audio file */
253 if (!vss_playing()) /* don't need one */
255 if (mmd->new_vss_status_flags & VSS_NOMORE)
257 if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
259 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
266 * Compute the timeout for para_server's main select-loop.
268 * This function gets called from para_server to determine the timeout value
269 * for its main select loop.
271 * \param s Pointer to the server scheduler.
272 * \param t Pointer to the vss task structure.
274 * Before the timeout is computed, the current vss status flags are evaluated
275 * and acted upon by calling appropriate functions from the lower layers.
276 * Possible actions include
278 * - request a new audio file from afs,
279 * - shutdown of all senders (stop/pause command),
280 * - reposition the stream (ff/jmp command).
282 static void vss_pre_select(struct sched *s, struct task *t)
285 struct timeval *tv, diff;
286 struct vss_task *vsst = container_of(t, struct vss_task, task);
288 if (!vsst->map || vss_next() || vss_paused() || vss_repos())
289 for (i = 0; senders[i].name; i++)
290 senders[i].shutdown_clients();
293 else if (vss_paused()) {
294 if (mmd->chunks_sent)
295 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
296 mmd->chunks_sent = 0;
297 } else if (vss_repos()) {
298 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
299 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
300 mmd->chunks_sent = 0;
301 mmd->current_chunk = mmd->repos_request;
302 mmd->new_vss_status_flags &= ~VSS_REPOS;
304 if (need_to_request_new_audio_file(vsst)) {
305 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
306 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
307 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
309 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
310 for (i = 0; senders[i].name; i++) {
311 if (!senders[i].pre_select)
313 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
315 tv = vss_compute_timeout(vsst);
316 if (tv && tv_diff(tv, &s->timeout, &diff) < 0)
320 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
322 char control[255], buf[8];
323 struct msghdr msg = {.msg_iov = NULL};
324 struct cmsghdr *cmsg;
330 iov.iov_len = sizeof(buf);
333 msg.msg_control = control;
334 msg.msg_controllen = sizeof(control);
335 memset(buf, 0, sizeof(buf));
336 ret = recvmsg(afs_socket, &msg, 0);
338 return -ERRNO_TO_PARA_ERROR(errno);
339 if (iov.iov_len != sizeof(buf))
340 return -E_AFS_SHORT_READ;
341 *code = *(uint32_t*)buf;
342 *data = *(uint32_t*)(buf + 4);
343 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
344 if (cmsg->cmsg_level != SOL_SOCKET
345 || cmsg->cmsg_type != SCM_RIGHTS)
347 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
349 *fd = *(int *)CMSG_DATA(cmsg);
354 static void recv_afs_result(struct vss_task *vsst)
356 int ret, passed_fd, shmid;
357 uint32_t afs_code = 0, afs_data = 0;
360 vsst->afsss = AFS_SOCKET_READY;
361 mmd->afd.afhi.chunk_table = NULL;
362 ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
365 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
368 if (afs_code != NEXT_AUDIO_FILE)
373 ret = load_afd(shmid, &mmd->afd);
377 ret = fstat(passed_fd, &statbuf);
379 PARA_ERROR_LOG("fstat error:\n");
380 ret = -ERRNO_TO_PARA_ERROR(errno);
383 mmd->size = statbuf.st_size;
384 mmd->mtime = statbuf.st_mtime;
385 ret = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE, passed_fd,
390 mmd->chunks_sent = 0;
391 mmd->current_chunk = 0;
395 mmd->new_vss_status_flags &= (~VSS_NEXT);
396 afh_get_header(&mmd->afd.afhi, vsst->map, &vsst->header_buf,
400 free(mmd->afd.afhi.chunk_table);
403 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
404 mmd->new_vss_status_flags = VSS_NEXT;
408 * Main sending function.
410 * This function gets called from para_server as soon as the next chunk of data
411 * should be pushed out. It obtains a pointer to the data to be sent out as
412 * well as its length from mmd->afd.afhi. This information is then passed to
413 * each supported sender's send() function which is supposed to send out the data
414 * to all connected clients.
416 static void vss_send_chunk(struct vss_task *vsst)
423 if (!vsst->map || !vss_playing())
425 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
426 &mmd->stream_start, &due);
427 if (tv_diff(&due, now, NULL) > 0)
429 if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
431 if (chk_barrier("data send", &vsst->data_send_barrier,
434 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
435 mmd->new_vss_status_flags |= VSS_NEXT;
439 * We call the send function also in case of empty chunks as they
440 * might have still some data queued which can be sent in this case.
442 if (!mmd->chunks_sent) {
444 mmd->stream_start = *now;
445 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
446 mmd->offset = tv2ms(&tmp);
449 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map, &buf, &len);
450 for (i = 0; senders[i].name; i++)
451 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len,
452 vsst->header_buf, vsst->header_len);
453 mmd->new_vss_status_flags |= VSS_PLAYING;
455 mmd->current_chunk++;
458 static void vss_post_select(struct sched *s, struct task *t)
461 struct vss_task *vsst = container_of(t, struct vss_task, task);
463 if (mmd->sender_cmd_data.cmd_num >= 0) {
464 int num = mmd->sender_cmd_data.cmd_num,
465 sender_num = mmd->sender_cmd_data.sender_num;
467 if (senders[sender_num].client_cmds[num])
468 senders[sender_num].client_cmds[num](&mmd->sender_cmd_data);
469 mmd->sender_cmd_data.cmd_num = -1;
471 if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
472 if (FD_ISSET(vsst->afs_socket, &s->rfds))
473 recv_afs_result(vsst);
474 } else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
475 PARA_NOTICE_LOG("requesting new fd from afs\n");
476 ret = send_buffer(vsst->afs_socket, "new");
477 vsst->afsss = AFS_SOCKET_AFD_PENDING;
479 for (i = 0; senders[i].name; i++) {
480 if (!senders[i].post_select)
482 senders[i].post_select(&s->rfds, &s->wfds);
484 if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
485 (vss_next() && vss_playing()))
486 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
487 vss_send_chunk(vsst);
491 * Initialize the virtual streaming system task.
493 * \param afs_socket The fd for communication with afs.
495 * This also initializes all supported senders and starts streaming
496 * if the --autoplay command line flag was given.
498 void init_vss_task(int afs_socket)
500 static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
502 char *hn = para_hostname(), *home = para_homedir();
503 long unsigned announce_time = conf.announce_time_arg > 0?
504 conf.announce_time_arg : 300,
505 autoplay_delay = conf.autoplay_delay_arg > 0?
506 conf.autoplay_delay_arg : 0;
508 vsst->afs_socket = afs_socket;
509 vsst->task.pre_select = vss_pre_select;
510 vsst->task.post_select = vss_post_select;
511 ms2tv(announce_time, &vsst->announce_tv);
512 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
513 for (i = 0; senders[i].name; i++) {
514 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
515 senders[i].init(&senders[i]);
519 mmd->sender_cmd_data.cmd_num = -1;
520 make_empty_status_items(mmd->afd.verbose_ls_output);
521 if (conf.autoplay_given) {
523 mmd->vss_status_flags |= VSS_PLAYING;
524 mmd->new_vss_status_flags |= VSS_PLAYING;
525 ms2tv(autoplay_delay, &tmp);
526 tv_add(now, &tmp, &vsst->autoplay_barrier);
527 tv_add(&vsst->autoplay_barrier, &vsst->announce_tv,
528 &vsst->data_send_barrier);
530 register_task(&vsst->task);