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"
32 extern const char *status_item_list[];
34 static struct timeval announce_tv;
35 static struct timeval data_send_barrier;
36 static struct timeval eof_barrier;
37 static struct timeval autoplay_barrier;
39 extern struct misc_meta_data *mmd;
40 extern struct sender senders[];
45 * check if vss status flag \a P (playing) is set
47 * \return greater than zero if playing, zero otherwise.
50 unsigned int vss_playing(void)
52 return mmd->new_vss_status_flags & VSS_PLAYING;
56 * check if \a N (next) status flag is set
58 * \return greater than zero if set, zero if not.
61 unsigned int vss_next(void)
63 return mmd->new_vss_status_flags & VSS_NEXT;
67 * check if a reposition request is pending
69 * \return greater than zero if true, zero otherwise.
72 unsigned int vss_repos(void)
74 return mmd->new_vss_status_flags & VSS_REPOS;
78 * check if the vss is currently paused
80 * \return greater than zero if paused, zero otherwise.
83 unsigned int vss_paused(void)
85 return !(mmd->new_vss_status_flags & VSS_NEXT)
86 && !(mmd->new_vss_status_flags & VSS_PLAYING);
90 * initialize the virtual streaming system
92 * Call the init functions of all supported audio format handlers and
93 * initialize all supported senders.
98 char *hn = para_hostname(), *home = para_homedir();
99 long unsigned announce_time = conf.announce_time_arg > 0?
100 conf.announce_time_arg : 300,
101 autoplay_delay = conf.autoplay_delay_arg > 0?
102 conf.autoplay_delay_arg : 0;
103 ms2tv(announce_time, &announce_tv);
104 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
105 for (i = 0; senders[i].name; i++) {
106 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
107 senders[i].init(&senders[i]);
111 if (conf.autoplay_given) {
112 struct timeval now, tmp;
113 mmd->vss_status_flags |= VSS_PLAYING;
114 mmd->new_vss_status_flags |= VSS_PLAYING;
115 gettimeofday(&now, NULL);
116 ms2tv(autoplay_delay, &tmp);
117 tv_add(&now, &tmp, &autoplay_barrier);
121 static int chk_barrier(const char *bname, const struct timeval *now,
122 const struct timeval *barrier, struct timeval *diff,
127 if (tv_diff(now, barrier, diff) > 0)
131 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
135 static void vss_next_chunk_time(struct timeval *due)
139 tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
140 tv_add(&tmp, &mmd->stream_start, due);
144 * != NULL: timeout for next chunk
145 * NULL: nothing to do
147 static struct timeval *vss_compute_timeout(void)
149 static struct timeval the_timeout;
150 struct timeval now, next_chunk;
152 if (vss_next() && mmd->audio_format >= 0) {
153 /* only sleep a bit, nec*/
154 the_timeout.tv_sec = 0;
155 the_timeout.tv_usec = 100;
158 gettimeofday(&now, NULL);
159 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
160 &the_timeout, 1) < 0)
162 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
164 if (chk_barrier("data send", &now, &data_send_barrier,
165 &the_timeout, 1) < 0)
167 if (mmd->audio_format < 0 || !vss_playing() || !map)
169 vss_next_chunk_time(&next_chunk);
170 if (chk_barrier(audio_format_name(mmd->audio_format), &now, &next_chunk,
171 &the_timeout, 0) < 0)
173 /* chunk is due or bof */
174 the_timeout.tv_sec = 0;
175 the_timeout.tv_usec = 0;
179 static void vss_eof(void)
185 PARA_NOTICE_LOG("EOF\n");
187 for (i = 0; senders[i].name; i++)
188 senders[i].shutdown_clients();
191 gettimeofday(&now, NULL);
192 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
193 munmap(map, mmd->size);
195 mmd->audio_format = -1;
196 mmd->chunks_sent = 0;
198 mmd->afi.seconds_total = 0;
199 free(mmd->afi.chunk_table);
200 mmd->afi.chunk_table = NULL;
201 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
202 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
203 strcpy(mmd->afi.info_string, tmp);
205 mmd->filename[0] = '\0';
211 * Get the header of the current audio file.
213 * \param header_len the length of the header is stored here
215 * \return a pointer to a buffer containing the header, or NULL, if no audio
216 * file is selected or if the current audio format does not need special header
220 char *vss_get_header(size_t *header_len)
222 if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
224 *header_len = mmd->afi.header_len;
225 return map + mmd->afi.header_offset;
229 * get the list of all supported audio formats
231 * \return a space separated list of all supported audio formats
232 * It is not allocated at runtime, i.e. there is no need to free
233 * the returned string in the caller.
235 const char *supported_audio_formats(void)
237 return SUPPORTED_AUDIO_FORMATS;
241 * get the chunk time of the current audio file
243 * \return a pointer to a struct containing the chunk time, or NULL,
244 * if currently no audio file is selected.
246 struct timeval *vss_chunk_time(void)
248 if (mmd->audio_format < 0)
250 return &mmd->afi.chunk_tv;
253 enum afs_socket_status {
255 AFS_SOCKET_CHECK_FOR_WRITE,
256 AFS_SOCKET_AFD_PENDING
259 static enum afs_socket_status afsss;
262 * Compute the timeout for para_server's main select-loop.
264 * This function gets called from para_server to determine the timeout value
265 * for its main select loop.
267 * Before the timeout is computed, the current vss status flags are evaluated
268 * and acted upon by calling appropriate functions from the lower layers.
269 * Possible actions include
271 * - request a new audio file from afs,
272 * - shutdown of all senders (stop/pause command),
273 * - reposition the stream (ff/jmp command).
275 * \return A pointer to a struct timeval containing the timeout for the next
276 * chunk of data to be sent, or NULL if we're not sending right now.
278 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
283 para_fd_set(afs_socket, rfds, max_fileno);
285 for (i = 0; senders[i].name; i++)
286 senders[i].shutdown_clients();
290 return vss_compute_timeout();
293 if (vss_paused() || vss_repos()) {
294 for (i = 0; senders[i].name; i++)
295 senders[i].shutdown_clients();
298 gettimeofday(&now, NULL);
299 if (!vss_paused() || mmd->chunks_sent)
300 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
302 tv_add(&now, &announce_tv, &data_send_barrier);
303 if (mmd->new_vss_status_flags & VSS_NOMORE)
304 mmd->new_vss_status_flags = VSS_NEXT;
306 mmd->chunks_sent = 0;
309 mmd->new_vss_status_flags &= ~(VSS_REPOS);
310 mmd->current_chunk = mmd->repos_request;
312 tv = vss_compute_timeout();
315 if (!map && vss_playing() &&
316 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
317 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
318 if (afsss == AFS_SOCKET_READY) {
319 para_fd_set(afs_socket, wfds, max_fileno);
320 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
326 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
328 char control[255], buf[8];
329 struct msghdr msg = {.msg_iov = NULL};
330 struct cmsghdr *cmsg;
335 iov.iov_len = sizeof(buf);
338 msg.msg_control = control;
339 msg.msg_controllen = sizeof(control);
340 memset(buf, 0, sizeof(buf));
341 ret = recvmsg(afs_socket, &msg, 0);
343 return -ERRNO_TO_PARA_ERROR(errno);
344 if (iov.iov_len != sizeof(buf))
345 return -E_SHORT_AFS_READ;
346 *code = *(uint32_t*)buf;
347 *data = *(uint32_t*)(buf + 4);
348 cmsg = CMSG_FIRSTHDR(&msg);
349 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
350 if (cmsg->cmsg_level != SOL_SOCKET
351 || cmsg->cmsg_type != SCM_RIGHTS)
353 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
355 *fd = *(int *)CMSG_DATA(cmsg);
360 static void recv_afs_result(void)
362 int ret, passed_fd = -1, shmid;
363 uint32_t afs_code = 0, afs_data = 0;
367 PARA_NOTICE_LOG("recv\n");
368 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
371 PARA_NOTICE_LOG("got the fd: %d, code: %u, shmid: %u\n",
372 passed_fd, afs_code, afs_data);
373 ret = -E_BAD_AFS_CODE;
374 if (afs_code != NEXT_AUDIO_FILE)
376 afsss = AFS_SOCKET_READY;
378 ret = load_afd(shmid, &mmd->afd);
382 PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd->afd.path,
383 mmd->afd.afhi.chunks_total);
384 ret = fstat(passed_fd, &statbuf);
386 PARA_ERROR_LOG("fstat error:\n");
387 ret = -ERRNO_TO_PARA_ERROR(errno);
390 mmd->size = statbuf.st_size;
391 mmd->mtime = statbuf.st_mtime;
392 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
395 strcpy(mmd->filename, mmd->afd.path); /* FIXME: check length */
396 mmd->afi.header_len = 0; /* default: no header */
397 mmd->audio_format = mmd->afd.afsi.audio_format_id;
398 mmd->chunks_sent = 0;
399 mmd->current_chunk = 0;
403 mmd->afi = mmd->afd.afhi;
404 mmd->new_vss_status_flags &= (~VSS_NEXT);
405 gettimeofday(&now, NULL);
406 tv_add(&now, &announce_tv, &data_send_barrier);
411 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
414 void vss_post_select(fd_set *rfds, fd_set *wfds)
418 if (FD_ISSET(afs_socket, rfds))
420 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
422 PARA_NOTICE_LOG("requesting new socket\n");
423 ret = send_buffer(afs_socket, "new");
424 afsss = AFS_SOCKET_AFD_PENDING;
427 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
429 size_t pos = mmd->afi.chunk_table[chunk_num];
431 *len = mmd->afi.chunk_table[chunk_num + 1] - pos;
433 if (chunk_num + 5 > mmd->afd.afhi.chunks_total)
434 PARA_NOTICE_LOG("chunk %lu/%lu\n, len: %zu\n", chunk_num,
435 mmd->afd.afhi.chunks_total, *len);
439 * Get the data of the given chunk.
441 * \param chunk_num The number of the desired chunk.
442 * \param buf Chunk data.
443 * \param len Chunk length in bytes.
445 * \return Positive on success, negative on errors.
447 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
449 if (mmd->audio_format < 0 || !map || !vss_playing())
451 if (chunk_num >= mmd->afi.chunks_total)
453 get_chunk(chunk_num, buf, len);
458 * main sending function
460 * This function gets called from para_server as soon as the next chunk of
461 * data should be pushed out. It first calls the read_chunk() function of
462 * the current audio format handler to obtain a pointer to the data to be
463 * sent out as well as its length. This information is then passed to each
464 * supported sender's send() function which does the actual sending.
466 void vss_send_chunk(void)
469 struct timeval now, due;
473 if (mmd->audio_format < 0 || !map || !vss_playing())
475 gettimeofday(&now, NULL);
476 vss_next_chunk_time(&due);
477 if (tv_diff(&due, &now, NULL) > 0)
479 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
481 if (chk_barrier("data send", &now, &data_send_barrier,
484 mmd->new_vss_status_flags &= ~VSS_REPOS;
485 if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
486 mmd->new_vss_status_flags |= VSS_NEXT;
490 * We call the send function also in case of empty chunks as they
491 * might have still some data queued which can be sent in this case.
493 if (!mmd->chunks_sent) {
495 gettimeofday(&mmd->stream_start, NULL);
496 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
497 mmd->offset = tv2ms(&tmp);
500 get_chunk(mmd->current_chunk, &buf, &len);
501 for (i = 0; senders[i].name; i++)
502 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
503 mmd->new_vss_status_flags |= VSS_PLAYING;
505 mmd->current_chunk++;