2 * Copyright (C) 1997-2008 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 ortp_send_init(struct sender *);
37 /** The list of supported senders. */
38 struct sender senders[] = {
41 .init = http_send_init,
45 .init = dccp_send_init,
50 .init = ortp_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 struct timeval announce_tv;
71 struct timeval data_send_barrier;
72 struct timeval eof_barrier;
73 struct timeval autoplay_barrier;
75 enum afs_socket_status afsss;
78 const char *header_buf;
83 * Check if vss status flag \a P (playing) is set.
85 * \return Greater than zero if playing, zero otherwise.
88 unsigned int vss_playing(void)
90 return mmd->new_vss_status_flags & VSS_PLAYING;
94 * Check if the \a N (next) status flag is set.
96 * \return Greater than zero if set, zero if not.
99 unsigned int vss_next(void)
101 return mmd->new_vss_status_flags & VSS_NEXT;
105 * Check if a reposition request is pending.
107 * \return Greater than zero if true, zero otherwise.
110 unsigned int vss_repos(void)
112 return mmd->new_vss_status_flags & VSS_REPOS;
116 * Check if the vss is currently paused.
118 * \return Greater than zero if paused, zero otherwise.
121 unsigned int vss_paused(void)
123 return !(mmd->new_vss_status_flags & VSS_NEXT)
124 && !(mmd->new_vss_status_flags & VSS_PLAYING);
128 * Check if the vss is currently stopped.
130 * \return Greater than zero if paused, zero otherwise.
133 unsigned int vss_stopped(void)
135 return (mmd->new_vss_status_flags & VSS_NEXT)
136 && !(mmd->new_vss_status_flags & VSS_PLAYING);
139 static int chk_barrier(const char *bname, const struct timeval *barrier,
140 struct timeval *diff, int print_log)
144 if (tv_diff(now, barrier, diff) > 0)
148 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
153 * != NULL: timeout for next chunk
154 * NULL: nothing to do
156 static struct timeval *vss_compute_timeout(struct vss_task *vsst)
158 static struct timeval the_timeout;
159 struct timeval next_chunk;
161 if (vss_next() && vsst->map) {
162 /* only sleep a bit, nec*/
163 the_timeout.tv_sec = 0;
164 the_timeout.tv_usec = 100;
167 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
168 &the_timeout, 1) < 0)
170 if (chk_barrier("eof", &vsst->eof_barrier, &the_timeout, 1) < 0)
172 if (chk_barrier("data send", &vsst->data_send_barrier,
173 &the_timeout, 1) < 0)
175 if (!vss_playing() || !vsst->map)
177 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
178 &mmd->stream_start, &next_chunk);
179 if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) < 0)
181 /* chunk is due or bof */
182 the_timeout.tv_sec = 0;
183 the_timeout.tv_usec = 0;
187 static void vss_eof(struct vss_task *vsst)
191 mmd->stream_start = *now;
194 if (mmd->new_vss_status_flags & VSS_NOMORE)
195 mmd->new_vss_status_flags = VSS_NEXT;
196 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
197 para_munmap(vsst->map, mmd->size);
199 mmd->chunks_sent = 0;
201 mmd->afd.afhi.seconds_total = 0;
202 mmd->afd.afhi.chunk_tv.tv_sec = 0;
203 mmd->afd.afhi.chunk_tv.tv_usec = 0;
204 free(mmd->afd.afhi.chunk_table);
205 mmd->afd.afhi.chunk_table = NULL;
206 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
207 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
208 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
209 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
210 make_empty_status_items(mmd->afd.verbose_ls_output);
218 * Get the list of all supported audio formats.
220 * \return Aa space separated list of all supported audio formats
221 * It is not allocated at runtime, i.e. there is no need to free
222 * the returned string in the caller.
224 const char *supported_audio_formats(void)
226 return SUPPORTED_AUDIO_FORMATS;
230 * Get the chunk time of the current audio file.
232 * \return A pointer to a struct containing the chunk time, or NULL,
233 * if currently no audio file is selected.
235 struct timeval *vss_chunk_time(void)
237 if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
238 mmd->afd.afhi.chunk_tv.tv_usec == 0)
240 return &mmd->afd.afhi.chunk_tv;
243 static int need_to_request_new_audio_file(struct vss_task *vsst)
245 if (vsst->map) /* have audio file */
247 if (!vss_playing()) /* don't need one */
249 if (mmd->new_vss_status_flags & VSS_NOMORE)
251 if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
257 * Compute the timeout for para_server's main select-loop.
259 * This function gets called from para_server to determine the timeout value
260 * for its main select loop.
262 * \param s Pointer to the server scheduler.
263 * \param t Pointer to the vss task structure.
265 * Before the timeout is computed, the current vss status flags are evaluated
266 * and acted upon by calling appropriate functions from the lower layers.
267 * Possible actions include
269 * - request a new audio file from afs,
270 * - shutdown of all senders (stop/pause command),
271 * - reposition the stream (ff/jmp command).
273 static void vss_pre_select(struct sched *s, struct task *t)
276 struct timeval *tv, diff;
277 struct vss_task *vsst = container_of(t, struct vss_task, task);
279 if (!vsst->map || vss_next() || vss_paused() || vss_repos())
280 for (i = 0; senders[i].name; i++)
281 senders[i].shutdown_clients();
284 else if (vss_paused()) {
285 if (mmd->chunks_sent)
286 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
287 mmd->chunks_sent = 0;
288 } else if (vss_repos()) {
289 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
290 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
291 mmd->chunks_sent = 0;
292 mmd->current_chunk = mmd->repos_request;
293 mmd->new_vss_status_flags &= ~VSS_REPOS;
296 if (need_to_request_new_audio_file(vsst)) {
297 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
298 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
299 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
301 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
302 for (i = 0; senders[i].name; i++) {
303 if (!senders[i].pre_select)
305 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
307 tv = vss_compute_timeout(vsst);
308 if (tv && tv_diff(tv, &s->timeout, &diff) < 0)
312 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
314 char control[255], buf[8];
315 struct msghdr msg = {.msg_iov = NULL};
316 struct cmsghdr *cmsg;
322 iov.iov_len = sizeof(buf);
325 msg.msg_control = control;
326 msg.msg_controllen = sizeof(control);
327 memset(buf, 0, sizeof(buf));
328 ret = recvmsg(afs_socket, &msg, 0);
330 return -ERRNO_TO_PARA_ERROR(errno);
331 if (iov.iov_len != sizeof(buf))
332 return -E_AFS_SHORT_READ;
333 *code = *(uint32_t*)buf;
334 *data = *(uint32_t*)(buf + 4);
335 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
336 if (cmsg->cmsg_level != SOL_SOCKET
337 || cmsg->cmsg_type != SCM_RIGHTS)
339 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
341 *fd = *(int *)CMSG_DATA(cmsg);
346 static void recv_afs_result(struct vss_task *vsst)
348 int ret, passed_fd, shmid;
349 uint32_t afs_code = 0, afs_data = 0;
352 vsst->afsss = AFS_SOCKET_READY;
353 mmd->afd.afhi.chunk_table = NULL;
354 ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
357 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
360 if (afs_code != NEXT_AUDIO_FILE)
365 ret = load_afd(shmid, &mmd->afd);
369 ret = fstat(passed_fd, &statbuf);
371 PARA_ERROR_LOG("fstat error:\n");
372 ret = -ERRNO_TO_PARA_ERROR(errno);
375 mmd->size = statbuf.st_size;
376 mmd->mtime = statbuf.st_mtime;
377 vsst->map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
380 mmd->chunks_sent = 0;
381 mmd->current_chunk = 0;
385 mmd->new_vss_status_flags &= (~VSS_NEXT);
386 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
387 afh_get_header(&mmd->afd.afhi, vsst->map, &vsst->header_buf,
391 free(mmd->afd.afhi.chunk_table);
394 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
395 mmd->new_vss_status_flags = VSS_NEXT;
399 * Main sending function.
401 * This function gets called from para_server as soon as the next chunk of data
402 * should be pushed out. It obtains a pointer to the data to be sent out as
403 * well as its length from mmd->afd.afhi. This information is then passed to
404 * each supported sender's send() function which is supposed to send out the data
405 * to all connected clients.
407 static void vss_send_chunk(struct vss_task *vsst)
414 if (!vsst->map || !vss_playing())
416 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
417 &mmd->stream_start, &due);
418 if (tv_diff(&due, now, NULL) > 0)
420 if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
422 if (chk_barrier("data send", &vsst->data_send_barrier,
425 mmd->new_vss_status_flags &= ~VSS_REPOS;
426 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
427 mmd->new_vss_status_flags |= VSS_NEXT;
431 * We call the send function also in case of empty chunks as they
432 * might have still some data queued which can be sent in this case.
434 if (!mmd->chunks_sent) {
436 mmd->stream_start = *now;
437 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
438 mmd->offset = tv2ms(&tmp);
441 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map, &buf, &len);
442 for (i = 0; senders[i].name; i++)
443 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len,
444 vsst->header_buf, vsst->header_len);
445 mmd->new_vss_status_flags |= VSS_PLAYING;
447 mmd->current_chunk++;
450 static void vss_post_select(struct sched *s, struct task *t)
453 struct vss_task *vsst = container_of(t, struct vss_task, task);
455 if (mmd->sender_cmd_data.cmd_num >= 0) {
456 int num = mmd->sender_cmd_data.cmd_num,
457 sender_num = mmd->sender_cmd_data.sender_num;
459 if (senders[sender_num].client_cmds[num])
460 senders[sender_num].client_cmds[num](&mmd->sender_cmd_data);
461 mmd->sender_cmd_data.cmd_num = -1;
463 if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
464 if (FD_ISSET(vsst->afs_socket, &s->rfds))
465 recv_afs_result(vsst);
466 } else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
467 PARA_NOTICE_LOG("requesting new fd from afs\n");
468 ret = send_buffer(vsst->afs_socket, "new");
469 vsst->afsss = AFS_SOCKET_AFD_PENDING;
471 for (i = 0; senders[i].name; i++) {
472 if (!senders[i].post_select)
474 senders[i].post_select(&s->rfds, &s->wfds);
476 vss_send_chunk(vsst);
480 * Initialize the virtual streaming system task.
482 * \param afs_socket The fd for communication with afs.
484 * This also initializes all supported senders and starts streaming
485 * if the --autoplay command line flag was given.
487 void init_vss_task(int afs_socket)
489 static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
491 char *hn = para_hostname(), *home = para_homedir();
492 long unsigned announce_time = conf.announce_time_arg > 0?
493 conf.announce_time_arg : 300,
494 autoplay_delay = conf.autoplay_delay_arg > 0?
495 conf.autoplay_delay_arg : 0;
497 vsst->afs_socket = afs_socket;
498 vsst->task.pre_select = vss_pre_select;
499 vsst->task.post_select = vss_post_select;
500 ms2tv(announce_time, &vsst->announce_tv);
501 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
502 for (i = 0; senders[i].name; i++) {
503 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
504 senders[i].init(&senders[i]);
508 mmd->sender_cmd_data.cmd_num = -1;
509 if (conf.autoplay_given) {
511 mmd->vss_status_flags |= VSS_PLAYING;
512 mmd->new_vss_status_flags |= VSS_PLAYING;
513 ms2tv(autoplay_delay, &tmp);
514 tv_add(now, &tmp, &vsst->autoplay_barrier);
516 register_task(&vsst->task);