2 * Copyright (C) 1997-2007 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"
27 #include "afs_common.h"
33 extern const char *status_item_list[];
35 static struct timeval announce_tv;
36 static struct timeval data_send_barrier;
37 static struct timeval eof_barrier;
38 static struct timeval autoplay_barrier;
40 extern struct misc_meta_data *mmd;
41 extern struct sender senders[];
46 * check if vss status flag \a P (playing) is set
48 * \return greater than zero if playing, zero otherwise.
51 unsigned int vss_playing(void)
53 return mmd->new_vss_status_flags & VSS_PLAYING;
57 * check if \a N (next) status flag is set
59 * \return greater than zero if set, zero if not.
62 unsigned int vss_next(void)
64 return mmd->new_vss_status_flags & VSS_NEXT;
68 * check if a reposition request is pending
70 * \return greater than zero if true, zero otherwise.
73 unsigned int vss_repos(void)
75 return mmd->new_vss_status_flags & VSS_REPOS;
79 * check if the vss is currently paused
81 * \return greater than zero if paused, zero otherwise.
84 unsigned int vss_paused(void)
86 return !(mmd->new_vss_status_flags & VSS_NEXT)
87 && !(mmd->new_vss_status_flags & VSS_PLAYING);
91 * initialize the virtual streaming system
93 * Call the init functions of all supported audio format handlers and
94 * initialize all supported senders.
99 char *hn = para_hostname(), *home = para_homedir();
100 long unsigned announce_time = conf.announce_time_arg > 0?
101 conf.announce_time_arg : 300,
102 autoplay_delay = conf.autoplay_delay_arg > 0?
103 conf.autoplay_delay_arg : 0;
104 ms2tv(announce_time, &announce_tv);
105 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
106 for (i = 0; senders[i].name; i++) {
107 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
108 senders[i].init(&senders[i]);
112 if (conf.autoplay_given) {
113 struct timeval now, tmp;
114 mmd->vss_status_flags |= VSS_PLAYING;
115 mmd->new_vss_status_flags |= VSS_PLAYING;
116 gettimeofday(&now, NULL);
117 ms2tv(autoplay_delay, &tmp);
118 tv_add(&now, &tmp, &autoplay_barrier);
122 static int chk_barrier(const char *bname, const struct timeval *now,
123 const struct timeval *barrier, struct timeval *diff,
128 if (tv_diff(now, barrier, diff) > 0)
132 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
136 static void vss_next_chunk_time(struct timeval *due)
140 tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
141 tv_add(&tmp, &mmd->stream_start, due);
145 * != NULL: timeout for next chunk
146 * NULL: nothing to do
148 static struct timeval *vss_compute_timeout(void)
150 static struct timeval the_timeout;
151 struct timeval now, next_chunk;
153 if (vss_next() && mmd->audio_format >= 0) {
154 /* only sleep a bit, nec*/
155 the_timeout.tv_sec = 0;
156 the_timeout.tv_usec = 100;
159 gettimeofday(&now, NULL);
160 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
161 &the_timeout, 1) < 0)
163 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
165 if (chk_barrier("data send", &now, &data_send_barrier,
166 &the_timeout, 1) < 0)
168 if (mmd->audio_format < 0 || !vss_playing() || !map)
170 vss_next_chunk_time(&next_chunk);
171 if (chk_barrier(audio_format_name(mmd->audio_format), &now, &next_chunk,
172 &the_timeout, 0) < 0)
174 /* chunk is due or bof */
175 the_timeout.tv_sec = 0;
176 the_timeout.tv_usec = 0;
180 static void vss_eof(void)
186 PARA_NOTICE_LOG("EOF\n");
188 for (i = 0; senders[i].name; i++)
189 senders[i].shutdown_clients();
192 gettimeofday(&now, NULL);
193 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
194 munmap(map, mmd->size);
196 mmd->audio_format = -1;
197 mmd->chunks_sent = 0;
199 mmd->afi.seconds_total = 0;
200 free(mmd->afi.chunk_table);
201 mmd->afi.chunk_table = NULL;
202 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
203 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
204 strcpy(mmd->afi.info_string, tmp);
206 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
207 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
208 strcpy(mmd->selector_info, tmp);
210 mmd->filename[0] = '\0';
216 * Get the header of the current audio file.
218 * \param header_len the length of the header is stored here
220 * \return a pointer to a buffer containing the header, or NULL, if no audio
221 * file is selected or if the current audio format does not need special header
225 char *vss_get_header(size_t *header_len)
227 if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
229 *header_len = mmd->afi.header_len;
230 return map + mmd->afi.header_offset;
234 * get the list of all supported audio formats
236 * \return a space separated list of all supported audio formats
237 * It is not allocated at runtime, i.e. there is no need to free
238 * the returned string in the caller.
240 const char *supported_audio_formats(void)
242 return SUPPORTED_AUDIO_FORMATS;
246 * get the chunk time of the current audio file
248 * \return a pointer to a struct containing the chunk time, or NULL,
249 * if currently no audio file is selected.
251 struct timeval *vss_chunk_time(void)
253 if (mmd->audio_format < 0)
255 return &mmd->afi.chunk_tv;
258 enum afs_socket_status {
260 AFS_SOCKET_CHECK_FOR_WRITE,
261 AFS_SOCKET_AFD_PENDING
264 static enum afs_socket_status afsss;
267 * Compute the timeout for para_server's main select-loop.
269 * This function gets called from para_server to determine the timeout value
270 * for its main select loop.
272 * Before the timeout is computed, the current vss status flags are evaluated
273 * and acted upon by calling appropriate functions from the lower layers.
274 * Possible actions include
276 * - request a new audio file from afs,
277 * - shutdown of all senders (stop/pause command),
278 * - reposition the stream (ff/jmp command).
280 * \return A pointer to a struct timeval containing the timeout for the next
281 * chunk of data to be sent, or NULL if we're not sending right now.
283 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
288 para_fd_set(afs_socket, rfds, max_fileno);
290 for (i = 0; senders[i].name; i++)
291 senders[i].shutdown_clients();
295 return vss_compute_timeout();
298 if (vss_paused() || vss_repos()) {
299 for (i = 0; senders[i].name; i++)
300 senders[i].shutdown_clients();
303 gettimeofday(&now, NULL);
304 if (!vss_paused() || mmd->chunks_sent)
305 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
307 tv_add(&now, &announce_tv, &data_send_barrier);
308 if (mmd->new_vss_status_flags & VSS_NOMORE)
309 mmd->new_vss_status_flags = VSS_NEXT;
311 mmd->chunks_sent = 0;
314 mmd->new_vss_status_flags &= ~(VSS_REPOS);
315 mmd->current_chunk = mmd->repos_request;
317 tv = vss_compute_timeout();
320 if (!map && vss_playing() &&
321 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
322 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
323 if (afsss == AFS_SOCKET_READY) {
324 para_fd_set(afs_socket, wfds, max_fileno);
325 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
331 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
333 char control[255], buf[8];
334 struct msghdr msg = {.msg_iov = NULL};
335 struct cmsghdr *cmsg;
340 iov.iov_len = sizeof(buf);
343 msg.msg_control = control;
344 msg.msg_controllen = sizeof(control);
345 memset(buf, 0, sizeof(buf));
346 ret = recvmsg(afs_socket, &msg, 0);
348 return -ERRNO_TO_PARA_ERROR(errno);
349 if (iov.iov_len != sizeof(buf))
350 return -E_SHORT_AFS_READ;
351 *code = *(uint32_t*)buf;
352 *data = *(uint32_t*)(buf + 4);
353 cmsg = CMSG_FIRSTHDR(&msg);
354 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
355 if (cmsg->cmsg_level != SOL_SOCKET
356 || cmsg->cmsg_type != SCM_RIGHTS)
358 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
360 *fd = *(int *)CMSG_DATA(cmsg);
365 static void recv_afs_result(void)
367 int ret, passed_fd = -1, shmid;
368 uint32_t afs_code = 0, afs_data = 0;
372 PARA_NOTICE_LOG("recv\n");
373 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
376 PARA_NOTICE_LOG("got the fd: %d, code: %u, shmid: %u\n",
377 passed_fd, afs_code, afs_data);
378 ret = -E_BAD_AFS_CODE;
379 if (afs_code != NEXT_AUDIO_FILE)
381 afsss = AFS_SOCKET_READY;
383 ret = load_afd(shmid, &mmd->afd);
387 PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd->afd.path,
388 mmd->afd.afhi.chunks_total);
389 ret = fstat(passed_fd, &statbuf);
391 ret = -ERRNO_TO_PARA_ERROR(errno);
394 mmd->size = statbuf.st_size;
395 mmd->mtime = statbuf.st_mtime;
396 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
399 strcpy(mmd->filename, mmd->afd.path); /* FIXME: check length */
400 mmd->afi.header_len = 0; /* default: no header */
401 mmd->audio_format = mmd->afd.afsi.audio_format_id;
402 mmd->chunks_sent = 0;
403 mmd->current_chunk = 0;
407 mmd->afi = mmd->afd.afhi;
408 mmd->new_vss_status_flags &= (~VSS_NEXT);
409 gettimeofday(&now, NULL);
410 tv_add(&now, &announce_tv, &data_send_barrier);
415 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
418 void vss_post_select(fd_set *rfds, fd_set *wfds)
422 if (FD_ISSET(afs_socket, rfds))
424 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
426 PARA_NOTICE_LOG("requesting new socket\n");
427 ret = send_buffer(afs_socket, "new");
428 afsss = AFS_SOCKET_AFD_PENDING;
431 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
433 size_t pos = mmd->afi.chunk_table[chunk_num];
435 *len = mmd->afi.chunk_table[chunk_num + 1] - pos;
437 if (chunk_num + 5 > mmd->afd.afhi.chunks_total)
438 PARA_NOTICE_LOG("chunk %lu/%lu\n, len: %zu\n", chunk_num,
439 mmd->afd.afhi.chunks_total, *len);
443 * Get the data of the given chunk.
445 * \param chunk_num The number of the desired chunk.
446 * \param buf Chunk data.
447 * \param len Chunk length in bytes.
449 * \return Positive on success, negative on errors.
451 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
453 if (mmd->audio_format < 0 || !map || !vss_playing())
455 if (chunk_num >= mmd->afi.chunks_total)
457 get_chunk(chunk_num, buf, len);
462 * main sending function
464 * This function gets called from para_server as soon as the next chunk of
465 * data should be pushed out. It first calls the read_chunk() function of
466 * the current audio format handler to obtain a pointer to the data to be
467 * sent out as well as its length. This information is then passed to each
468 * supported sender's send() function which does the actual sending.
470 void vss_send_chunk(void)
473 struct timeval now, due;
477 if (mmd->audio_format < 0 || !map || !vss_playing())
479 gettimeofday(&now, NULL);
480 vss_next_chunk_time(&due);
481 if (tv_diff(&due, &now, NULL) > 0)
483 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
485 if (chk_barrier("data send", &now, &data_send_barrier,
488 mmd->new_vss_status_flags &= ~VSS_REPOS;
489 if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
490 mmd->new_vss_status_flags |= VSS_NEXT;
494 * We call the send function also in case of empty chunks as they
495 * might have still some data queued which can be sent in this case.
497 if (!mmd->chunks_sent) {
499 gettimeofday(&mmd->stream_start, NULL);
500 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
501 mmd->offset = tv2ms(&tmp);
504 get_chunk(mmd->current_chunk, &buf, &len);
505 for (i = 0; senders[i].name; i++)
506 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
507 mmd->new_vss_status_flags |= VSS_PLAYING;
509 mmd->current_chunk++;