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
14 #include <sys/mman.h> /* mmap */
15 #include <sys/time.h> /* gettimeofday */
16 #include <sys/types.h>
26 #include "server.cmdline.h"
33 static struct timeval announce_tv;
34 static struct timeval data_send_barrier;
35 static struct timeval eof_barrier;
36 static struct timeval autoplay_barrier;
38 extern struct misc_meta_data *mmd;
40 extern void dccp_send_init(struct sender *);
41 extern void http_send_init(struct sender *);
42 extern void ortp_send_init(struct sender *);
44 /** the list of supported senders */
45 struct sender senders[] = {
48 .init = http_send_init,
52 .init = dccp_send_init,
57 .init = ortp_send_init,
70 * Check if vss status flag \a P (playing) is set.
72 * \return Greater than zero if playing, zero otherwise.
75 unsigned int vss_playing(void)
77 return mmd->new_vss_status_flags & VSS_PLAYING;
81 * Check if the \a N (next) status flag is set.
83 * \return Greater than zero if set, zero if not.
86 unsigned int vss_next(void)
88 return mmd->new_vss_status_flags & VSS_NEXT;
92 * Check if a reposition request is pending.
94 * \return Greater than zero if true, zero otherwise.
97 unsigned int vss_repos(void)
99 return mmd->new_vss_status_flags & VSS_REPOS;
103 * Check if the vss is currently paused.
105 * \return Greater than zero if paused, zero otherwise.
108 unsigned int vss_paused(void)
110 return !(mmd->new_vss_status_flags & VSS_NEXT)
111 && !(mmd->new_vss_status_flags & VSS_PLAYING);
115 * Check if the vss is currently stopped.
117 * \return Greater than zero if paused, zero otherwise.
120 unsigned int vss_stopped(void)
122 return (mmd->new_vss_status_flags & VSS_NEXT)
123 && !(mmd->new_vss_status_flags & VSS_PLAYING);
127 * Get the data of the given chunk.
129 * \param chunk_num The number of the desired chunk.
130 * \param buf Chunk data.
131 * \param len Chunk length in bytes.
135 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
137 if (!map || !vss_playing())
139 if (chunk_num >= mmd->afd.afhi.chunks_total)
141 afh_get_chunk(chunk_num, &mmd->afd.afhi, map, buf, len);
146 * Initialize the virtual streaming system.
148 * This also initializes all supported senders and starts streaming
149 * if the --autoplay command line flag was given.
154 char *hn = para_hostname(), *home = para_homedir();
155 long unsigned announce_time = conf.announce_time_arg > 0?
156 conf.announce_time_arg : 300,
157 autoplay_delay = conf.autoplay_delay_arg > 0?
158 conf.autoplay_delay_arg : 0;
159 ms2tv(announce_time, &announce_tv);
160 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
161 for (i = 0; senders[i].name; i++) {
162 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
163 senders[i].init(&senders[i]);
167 mmd->sender_cmd_data.cmd_num = -1;
168 if (conf.autoplay_given) {
169 struct timeval now, tmp;
170 mmd->vss_status_flags |= VSS_PLAYING;
171 mmd->new_vss_status_flags |= VSS_PLAYING;
172 gettimeofday(&now, NULL);
173 ms2tv(autoplay_delay, &tmp);
174 tv_add(&now, &tmp, &autoplay_barrier);
178 static int chk_barrier(const char *bname, const struct timeval *now,
179 const struct timeval *barrier, struct timeval *diff,
184 if (tv_diff(now, barrier, diff) > 0)
188 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
193 * != NULL: timeout for next chunk
194 * NULL: nothing to do
196 static struct timeval *vss_compute_timeout(void)
198 static struct timeval the_timeout;
199 struct timeval now, next_chunk;
201 if (vss_next() && map) {
202 /* only sleep a bit, nec*/
203 the_timeout.tv_sec = 0;
204 the_timeout.tv_usec = 100;
207 gettimeofday(&now, NULL);
208 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
209 &the_timeout, 1) < 0)
211 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
213 if (chk_barrier("data send", &now, &data_send_barrier,
214 &the_timeout, 1) < 0)
216 if (!vss_playing() || !map)
218 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
219 &mmd->stream_start, &next_chunk);
220 if (chk_barrier("chunk", &now, &next_chunk, &the_timeout, 0) < 0)
222 /* chunk is due or bof */
223 the_timeout.tv_sec = 0;
224 the_timeout.tv_usec = 0;
228 static void vss_eof(void)
233 if (mmd->new_vss_status_flags & VSS_NOMORE)
234 mmd->new_vss_status_flags = VSS_NEXT;
235 gettimeofday(&now, NULL);
236 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
237 munmap(map, mmd->size);
239 mmd->chunks_sent = 0;
241 mmd->afd.afhi.seconds_total = 0;
242 free(mmd->afd.afhi.chunk_table);
243 mmd->afd.afhi.chunk_table = NULL;
244 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
245 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
246 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
247 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
248 make_empty_status_items(mmd->afd.verbose_ls_output);
256 * Get the header of the current audio file.
258 * \param buf The length of the header is stored here.
259 * \param len Points to a buffer containing the header on return.
261 * \sa afh_get_header.
263 void vss_get_header(char **buf, size_t *len)
265 afh_get_header(&mmd->afd.afhi, map, buf, len);
269 * Get the list of all supported audio formats.
271 * \return Aa space separated list of all supported audio formats
272 * It is not allocated at runtime, i.e. there is no need to free
273 * the returned string in the caller.
275 const char *supported_audio_formats(void)
277 return SUPPORTED_AUDIO_FORMATS;
281 * Get the chunk time of the current audio file.
283 * \return A pointer to a struct containing the chunk time, or NULL,
284 * if currently no audio file is selected.
286 struct timeval *vss_chunk_time(void)
290 return &mmd->afd.afhi.chunk_tv;
293 /** The possible states of the afs socket. See \ref afs_socket. */
294 enum afs_socket_status {
295 /** Socket is inactive. */
297 /** Socket fd was included in the write fd set for select(). */
298 AFS_SOCKET_CHECK_FOR_WRITE,
299 /** vss wrote a request to the socket and waits for afs to reply. */
300 AFS_SOCKET_AFD_PENDING
303 static enum afs_socket_status afsss;
305 static int need_to_request_new_audio_file(void)
307 if (map) /* have audio file */
309 if (!vss_playing()) /* don't need one */
311 if (mmd->new_vss_status_flags & VSS_NOMORE)
313 if (afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
319 * Compute the timeout for para_server's main select-loop.
321 * This function gets called from para_server to determine the timeout value
322 * for its main select loop.
324 * \param rfds The set of file descriptors to be checked for reading.
325 * \param wfds The set of file descriptors to be checked for writing.
326 * \param max_fileno The highest-numbered file descriptor.
328 * Before the timeout is computed, the current vss status flags are evaluated
329 * and acted upon by calling appropriate functions from the lower layers.
330 * Possible actions include
332 * - request a new audio file from afs,
333 * - shutdown of all senders (stop/pause command),
334 * - reposition the stream (ff/jmp command).
336 * \return A pointer to a struct timeval containing the timeout for the next
337 * chunk of data to be sent, or NULL if we're not sending right now.
339 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
344 if (!map || vss_next() || vss_paused() || vss_repos())
345 for (i = 0; senders[i].name; i++)
346 senders[i].shutdown_clients();
349 else if (vss_paused()) {
350 if (mmd->chunks_sent) {
351 gettimeofday(&now, NULL);
352 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
354 mmd->chunks_sent = 0;
355 } else if (vss_repos()) {
356 gettimeofday(&now, NULL);
357 tv_add(&now, &announce_tv, &data_send_barrier);
358 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
359 mmd->chunks_sent = 0;
360 mmd->current_chunk = mmd->repos_request;
361 mmd->new_vss_status_flags &= ~VSS_REPOS;
364 if (need_to_request_new_audio_file()) {
365 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
366 para_fd_set(afs_socket, wfds, max_fileno);
367 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
369 para_fd_set(afs_socket, rfds, max_fileno);
370 for (i = 0; senders[i].name; i++) {
371 if (!senders[i].pre_select)
373 senders[i].pre_select(max_fileno, rfds, wfds);
375 return vss_compute_timeout();
378 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
380 char control[255], buf[8];
381 struct msghdr msg = {.msg_iov = NULL};
382 struct cmsghdr *cmsg;
388 iov.iov_len = sizeof(buf);
391 msg.msg_control = control;
392 msg.msg_controllen = sizeof(control);
393 memset(buf, 0, sizeof(buf));
394 ret = recvmsg(afs_socket, &msg, 0);
396 return -ERRNO_TO_PARA_ERROR(errno);
397 afsss = AFS_SOCKET_READY;
398 if (iov.iov_len != sizeof(buf))
399 return -E_AFS_SHORT_READ;
400 *code = *(uint32_t*)buf;
401 *data = *(uint32_t*)(buf + 4);
402 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
403 if (cmsg->cmsg_level != SOL_SOCKET
404 || cmsg->cmsg_type != SCM_RIGHTS)
406 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
408 *fd = *(int *)CMSG_DATA(cmsg);
413 static void recv_afs_result(void)
415 int ret, passed_fd, shmid;
416 uint32_t afs_code = 0, afs_data = 0;
420 mmd->afd.afhi.chunk_table = NULL;
421 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
424 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
427 if (afs_code != NEXT_AUDIO_FILE)
432 ret = load_afd(shmid, &mmd->afd);
436 ret = fstat(passed_fd, &statbuf);
438 PARA_ERROR_LOG("fstat error:\n");
439 ret = -ERRNO_TO_PARA_ERROR(errno);
442 mmd->size = statbuf.st_size;
443 mmd->mtime = statbuf.st_mtime;
444 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
447 mmd->chunks_sent = 0;
448 mmd->current_chunk = 0;
452 mmd->new_vss_status_flags &= (~VSS_NEXT);
453 gettimeofday(&now, NULL);
454 tv_add(&now, &announce_tv, &data_send_barrier);
457 free(mmd->afd.afhi.chunk_table);
460 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
461 mmd->new_vss_status_flags = VSS_NEXT;
465 * Main sending function.
467 * This function gets called from para_server as soon as the next chunk of data
468 * should be pushed out. It obtains a pointer to the data to be sent out as
469 * well as its length from mmd->afd.afhi. This information is then passed to
470 * each supported sender's send() function which is supposed to send out the data
471 * to all connected clients.
473 static void vss_send_chunk(void)
476 struct timeval now, due;
480 if (!map || !vss_playing())
482 gettimeofday(&now, NULL);
483 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
484 &mmd->stream_start, &due);
485 if (tv_diff(&due, &now, NULL) > 0)
487 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
489 if (chk_barrier("data send", &now, &data_send_barrier,
492 mmd->new_vss_status_flags &= ~VSS_REPOS;
493 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
494 mmd->new_vss_status_flags |= VSS_NEXT;
498 * We call the send function also in case of empty chunks as they
499 * might have still some data queued which can be sent in this case.
501 if (!mmd->chunks_sent) {
503 gettimeofday(&mmd->stream_start, NULL);
504 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
505 mmd->offset = tv2ms(&tmp);
508 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, map, &buf, &len);
509 for (i = 0; senders[i].name; i++)
510 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
511 mmd->new_vss_status_flags |= VSS_PLAYING;
513 mmd->current_chunk++;
516 void vss_post_select(fd_set *rfds, fd_set *wfds)
520 if (mmd->sender_cmd_data.cmd_num >= 0) {
521 int num = mmd->sender_cmd_data.cmd_num,
522 s = mmd->sender_cmd_data.sender_num;
524 if (senders[s].client_cmds[num])
525 senders[s].client_cmds[num](&mmd->sender_cmd_data);
526 mmd->sender_cmd_data.cmd_num = -1;
528 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
529 if (FD_ISSET(afs_socket, rfds))
531 } else if (FD_ISSET(afs_socket, wfds)) {
532 PARA_NOTICE_LOG("requesting new fd from afs\n");
533 ret = send_buffer(afs_socket, "new");
534 afsss = AFS_SOCKET_AFD_PENDING;
536 for (i = 0; senders[i].name; i++) {
537 if (!senders[i].post_select)
539 senders[i].post_select(rfds, wfds);