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 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 struct timeval announce_tv;
69 struct timeval data_send_barrier;
70 struct timeval eof_barrier;
71 struct timeval autoplay_barrier;
73 enum afs_socket_status afsss;
76 const char *header_buf;
81 * Check if vss status flag \a P (playing) is set.
83 * \return Greater than zero if playing, zero otherwise.
86 unsigned int vss_playing(void)
88 return mmd->new_vss_status_flags & VSS_PLAYING;
92 * Check if the \a N (next) status flag is set.
94 * \return Greater than zero if set, zero if not.
97 unsigned int vss_next(void)
99 return mmd->new_vss_status_flags & VSS_NEXT;
103 * Check if a reposition request is pending.
105 * \return Greater than zero if true, zero otherwise.
108 unsigned int vss_repos(void)
110 return mmd->new_vss_status_flags & VSS_REPOS;
114 * Check if the vss is currently paused.
116 * \return Greater than zero if paused, zero otherwise.
119 unsigned int vss_paused(void)
121 return !(mmd->new_vss_status_flags & VSS_NEXT)
122 && !(mmd->new_vss_status_flags & VSS_PLAYING);
126 * Check if the vss is currently stopped.
128 * \return Greater than zero if paused, zero otherwise.
131 unsigned int vss_stopped(void)
133 return (mmd->new_vss_status_flags & VSS_NEXT)
134 && !(mmd->new_vss_status_flags & VSS_PLAYING);
137 static int chk_barrier(const char *bname, const struct timeval *barrier,
138 struct timeval *diff, int print_log)
142 if (tv_diff(now, barrier, diff) > 0)
146 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
151 * != NULL: timeout for next chunk
152 * NULL: nothing to do
154 static struct timeval *vss_compute_timeout(struct vss_task *vsst)
156 static struct timeval the_timeout;
157 struct timeval next_chunk;
159 if (vss_next() && vsst->map) {
160 /* only sleep a bit, nec*/
161 the_timeout.tv_sec = 0;
162 the_timeout.tv_usec = 100;
165 if (chk_barrier("autoplay_delay", &vsst->autoplay_barrier,
166 &the_timeout, 1) < 0)
168 if (chk_barrier("eof", &vsst->eof_barrier, &the_timeout, 1) < 0)
170 if (chk_barrier("data send", &vsst->data_send_barrier,
171 &the_timeout, 1) < 0)
173 if (!vss_playing() || !vsst->map)
175 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
176 &mmd->stream_start, &next_chunk);
177 if (chk_barrier("chunk", &next_chunk, &the_timeout, 0) < 0)
179 /* chunk is due or bof */
180 the_timeout.tv_sec = 0;
181 the_timeout.tv_usec = 0;
185 static void vss_eof(struct vss_task *vsst)
187 mmd->stream_start = *now;
190 if (mmd->new_vss_status_flags & VSS_NOMORE)
191 mmd->new_vss_status_flags = VSS_NEXT;
192 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
193 para_munmap(vsst->map, mmd->size);
195 mmd->chunks_sent = 0;
197 mmd->afd.afhi.seconds_total = 0;
198 mmd->afd.afhi.chunk_tv.tv_sec = 0;
199 mmd->afd.afhi.chunk_tv.tv_usec = 0;
200 free(mmd->afd.afhi.chunk_table);
201 mmd->afd.afhi.chunk_table = NULL;
202 free(mmd->afd.afhi.info_string);
203 mmd->afd.afhi.info_string = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
204 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
205 make_empty_status_items(mmd->afd.verbose_ls_output);
212 * Get the list of all supported audio formats.
214 * \return Aa space separated list of all supported audio formats
215 * It is not allocated at runtime, i.e. there is no need to free
216 * the returned string in the caller.
218 const char *supported_audio_formats(void)
220 return SUPPORTED_AUDIO_FORMATS;
224 * Get the chunk time of the current audio file.
226 * \return A pointer to a struct containing the chunk time, or NULL,
227 * if currently no audio file is selected.
229 struct timeval *vss_chunk_time(void)
231 if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
232 mmd->afd.afhi.chunk_tv.tv_usec == 0)
234 return &mmd->afd.afhi.chunk_tv;
237 static int need_to_request_new_audio_file(struct vss_task *vsst)
239 if (vsst->map) /* have audio file */
241 if (!vss_playing()) /* don't need one */
243 if (mmd->new_vss_status_flags & VSS_NOMORE)
245 if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
251 * Compute the timeout for para_server's main select-loop.
253 * This function gets called from para_server to determine the timeout value
254 * for its main select loop.
256 * \param s Pointer to the server scheduler.
257 * \param t Pointer to the vss task structure.
259 * Before the timeout is computed, the current vss status flags are evaluated
260 * and acted upon by calling appropriate functions from the lower layers.
261 * Possible actions include
263 * - request a new audio file from afs,
264 * - shutdown of all senders (stop/pause command),
265 * - reposition the stream (ff/jmp command).
267 static void vss_pre_select(struct sched *s, struct task *t)
270 struct timeval *tv, diff;
271 struct vss_task *vsst = container_of(t, struct vss_task, task);
273 if (!vsst->map || vss_next() || vss_paused() || vss_repos())
274 for (i = 0; senders[i].name; i++)
275 senders[i].shutdown_clients();
278 else if (vss_paused()) {
279 if (mmd->chunks_sent)
280 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
281 mmd->chunks_sent = 0;
282 } else if (vss_repos()) {
283 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
284 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
285 mmd->chunks_sent = 0;
286 mmd->current_chunk = mmd->repos_request;
287 mmd->new_vss_status_flags &= ~VSS_REPOS;
290 if (need_to_request_new_audio_file(vsst)) {
291 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
292 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
293 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
295 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
296 for (i = 0; senders[i].name; i++) {
297 if (!senders[i].pre_select)
299 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
301 tv = vss_compute_timeout(vsst);
302 if (tv && tv_diff(tv, &s->timeout, &diff) < 0)
306 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
308 char control[255], buf[8];
309 struct msghdr msg = {.msg_iov = NULL};
310 struct cmsghdr *cmsg;
316 iov.iov_len = sizeof(buf);
319 msg.msg_control = control;
320 msg.msg_controllen = sizeof(control);
321 memset(buf, 0, sizeof(buf));
322 ret = recvmsg(afs_socket, &msg, 0);
324 return -ERRNO_TO_PARA_ERROR(errno);
325 if (iov.iov_len != sizeof(buf))
326 return -E_AFS_SHORT_READ;
327 *code = *(uint32_t*)buf;
328 *data = *(uint32_t*)(buf + 4);
329 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
330 if (cmsg->cmsg_level != SOL_SOCKET
331 || cmsg->cmsg_type != SCM_RIGHTS)
333 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
335 *fd = *(int *)CMSG_DATA(cmsg);
340 static void recv_afs_result(struct vss_task *vsst)
342 int ret, passed_fd, shmid;
343 uint32_t afs_code = 0, afs_data = 0;
346 vsst->afsss = AFS_SOCKET_READY;
347 mmd->afd.afhi.chunk_table = NULL;
348 ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
351 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
354 if (afs_code != NEXT_AUDIO_FILE)
359 ret = load_afd(shmid, &mmd->afd);
363 ret = fstat(passed_fd, &statbuf);
365 PARA_ERROR_LOG("fstat error:\n");
366 ret = -ERRNO_TO_PARA_ERROR(errno);
369 mmd->size = statbuf.st_size;
370 mmd->mtime = statbuf.st_mtime;
371 vsst->map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
374 mmd->chunks_sent = 0;
375 mmd->current_chunk = 0;
379 mmd->new_vss_status_flags &= (~VSS_NEXT);
380 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
381 afh_get_header(&mmd->afd.afhi, vsst->map, &vsst->header_buf,
385 free(mmd->afd.afhi.chunk_table);
388 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
389 mmd->new_vss_status_flags = VSS_NEXT;
393 * Main sending function.
395 * This function gets called from para_server as soon as the next chunk of data
396 * should be pushed out. It obtains a pointer to the data to be sent out as
397 * well as its length from mmd->afd.afhi. This information is then passed to
398 * each supported sender's send() function which is supposed to send out the data
399 * to all connected clients.
401 static void vss_send_chunk(struct vss_task *vsst)
408 if (!vsst->map || !vss_playing())
410 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
411 &mmd->stream_start, &due);
412 if (tv_diff(&due, now, NULL) > 0)
414 if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
416 if (chk_barrier("data send", &vsst->data_send_barrier,
419 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
420 mmd->new_vss_status_flags |= VSS_NEXT;
424 * We call the send function also in case of empty chunks as they
425 * might have still some data queued which can be sent in this case.
427 if (!mmd->chunks_sent) {
429 mmd->stream_start = *now;
430 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
431 mmd->offset = tv2ms(&tmp);
434 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map, &buf, &len);
435 for (i = 0; senders[i].name; i++)
436 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len,
437 vsst->header_buf, vsst->header_len);
438 mmd->new_vss_status_flags |= VSS_PLAYING;
440 mmd->current_chunk++;
443 static void vss_post_select(struct sched *s, struct task *t)
446 struct vss_task *vsst = container_of(t, struct vss_task, task);
448 if (mmd->sender_cmd_data.cmd_num >= 0) {
449 int num = mmd->sender_cmd_data.cmd_num,
450 sender_num = mmd->sender_cmd_data.sender_num;
452 if (senders[sender_num].client_cmds[num])
453 senders[sender_num].client_cmds[num](&mmd->sender_cmd_data);
454 mmd->sender_cmd_data.cmd_num = -1;
456 if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
457 if (FD_ISSET(vsst->afs_socket, &s->rfds))
458 recv_afs_result(vsst);
459 } else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
460 PARA_NOTICE_LOG("requesting new fd from afs\n");
461 ret = send_buffer(vsst->afs_socket, "new");
462 vsst->afsss = AFS_SOCKET_AFD_PENDING;
464 for (i = 0; senders[i].name; i++) {
465 if (!senders[i].post_select)
467 senders[i].post_select(&s->rfds, &s->wfds);
469 vss_send_chunk(vsst);
473 * Initialize the virtual streaming system task.
475 * \param afs_socket The fd for communication with afs.
477 * This also initializes all supported senders and starts streaming
478 * if the --autoplay command line flag was given.
480 void init_vss_task(int afs_socket)
482 static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
484 char *hn = para_hostname(), *home = para_homedir();
485 long unsigned announce_time = conf.announce_time_arg > 0?
486 conf.announce_time_arg : 300,
487 autoplay_delay = conf.autoplay_delay_arg > 0?
488 conf.autoplay_delay_arg : 0;
490 vsst->afs_socket = afs_socket;
491 vsst->task.pre_select = vss_pre_select;
492 vsst->task.post_select = vss_post_select;
493 ms2tv(announce_time, &vsst->announce_tv);
494 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
495 for (i = 0; senders[i].name; i++) {
496 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
497 senders[i].init(&senders[i]);
501 mmd->sender_cmd_data.cmd_num = -1;
502 make_empty_status_items(mmd->afd.verbose_ls_output);
503 if (conf.autoplay_given) {
505 mmd->vss_status_flags |= VSS_PLAYING;
506 mmd->new_vss_status_flags |= VSS_PLAYING;
507 ms2tv(autoplay_delay, &tmp);
508 tv_add(now, &tmp, &vsst->autoplay_barrier);
510 register_task(&vsst->task);