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 audio_file_selector selectors[];
42 extern struct sender senders[];
44 static int audio_file;
48 * check if vss status flag \a P (playing) is set
50 * \return greater than zero if playing, zero otherwise.
53 unsigned int vss_playing(void)
55 return mmd->new_vss_status_flags & VSS_PLAYING;
59 * check if \a N (next) status flag is set
61 * \return greater than zero if set, zero if not.
64 unsigned int vss_next(void)
66 return mmd->new_vss_status_flags & VSS_NEXT;
70 * check if a reposition request is pending
72 * \return greater than zero if true, zero otherwise.
75 unsigned int vss_repos(void)
77 return mmd->new_vss_status_flags & VSS_REPOS;
81 * check if the vss is currently paused
83 * \return greater than zero if paused, zero otherwise.
86 unsigned int vss_paused(void)
88 return !(mmd->new_vss_status_flags & VSS_NEXT)
89 && !(mmd->new_vss_status_flags & VSS_PLAYING);
93 * initialize the virtual streaming system
95 * Call the init functions of all supported audio format handlers and
96 * initialize all supported senders.
101 char *hn = para_hostname(), *home = para_homedir();
102 long unsigned announce_time = conf.announce_time_arg > 0?
103 conf.announce_time_arg : 300,
104 autoplay_delay = conf.autoplay_delay_arg > 0?
105 conf.autoplay_delay_arg : 0;
107 ms2tv(announce_time, &announce_tv);
108 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
109 for (i = 0; senders[i].name; i++) {
110 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
111 senders[i].init(&senders[i]);
115 if (conf.autoplay_given) {
116 struct timeval now, tmp;
117 mmd->vss_status_flags |= VSS_PLAYING;
118 mmd->new_vss_status_flags |= VSS_PLAYING;
119 gettimeofday(&now, NULL);
120 ms2tv(autoplay_delay, &tmp);
121 tv_add(&now, &tmp, &autoplay_barrier);
125 static int chk_barrier(const char *bname, const struct timeval *now,
126 const struct timeval *barrier, struct timeval *diff,
131 if (tv_diff(now, barrier, diff) > 0)
135 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
139 static void vss_next_chunk_time(struct timeval *due)
143 tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
144 tv_add(&tmp, &mmd->stream_start, due);
148 * != NULL: timeout for next chunk
149 * NULL: nothing to do
151 static struct timeval *vss_compute_timeout(void)
153 static struct timeval the_timeout;
154 struct timeval now, next_chunk;
156 if (vss_next() && mmd->audio_format >= 0) {
157 /* only sleep a bit, nec*/
158 the_timeout.tv_sec = 0;
159 the_timeout.tv_usec = 100;
162 gettimeofday(&now, NULL);
163 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
164 &the_timeout, 1) < 0)
166 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
168 if (chk_barrier("data send", &now, &data_send_barrier,
169 &the_timeout, 1) < 0)
171 if (mmd->audio_format < 0 || !vss_playing() || !map)
173 vss_next_chunk_time(&next_chunk);
174 if (chk_barrier(audio_format_name(mmd->audio_format), &now, &next_chunk,
175 &the_timeout, 0) < 0)
177 /* chunk is due or bof */
178 the_timeout.tv_sec = 0;
179 the_timeout.tv_usec = 0;
183 static void vss_eof(void)
189 PARA_NOTICE_LOG("EOF\n");
191 for (i = 0; senders[i].name; i++)
192 senders[i].shutdown_clients();
195 gettimeofday(&now, NULL);
196 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
197 munmap(map, mmd->size);
200 mmd->audio_format = -1;
201 mmd->chunks_sent = 0;
203 mmd->afi.seconds_total = 0;
204 free(mmd->afi.chunk_table);
205 mmd->afi.chunk_table = NULL;
206 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
207 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
208 strcpy(mmd->afi.info_string, tmp);
210 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
211 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
212 strcpy(mmd->selector_info, tmp);
214 mmd->filename[0] = '\0';
220 * Get the header of the current audio file.
222 * \param header_len the length of the header is stored here
224 * \return a pointer to a buffer containing the header, or NULL, if no audio
225 * file is selected or if the current audio format does not need special header
229 char *vss_get_header(size_t *header_len)
231 if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
233 *header_len = mmd->afi.header_len;
234 return map + mmd->afi.header_offset;
238 * get the list of all supported audio formats
240 * \return a space separated list of all supported audio formats
241 * It is not allocated at runtime, i.e. there is no need to free
242 * the returned string in the caller.
244 const char *supported_audio_formats(void)
246 return SUPPORTED_AUDIO_FORMATS;
250 * get the chunk time of the current audio file
252 * \return a pointer to a struct containing the chunk time, or NULL,
253 * if currently no audio file is selected.
255 struct timeval *vss_chunk_time(void)
257 if (mmd->audio_format < 0)
259 return &mmd->afi.chunk_tv;
262 enum afs_socket_status {
264 AFS_SOCKET_CHECK_FOR_WRITE,
265 AFS_SOCKET_AFD_PENDING
268 static enum afs_socket_status afsss;
271 * Compute the timeout for para_server's main select-loop.
273 * This function gets called from para_server to determine the timeout value
274 * for its main select loop.
276 * Before the timeout is computed, the current vss status flags are evaluated
277 * and acted upon by calling appropriate functions from the lower layers.
278 * Possible actions include
280 * - request a new audio file from afs,
281 * - shutdown of all senders (stop/pause command),
282 * - reposition the stream (ff/jmp command).
284 * \return A pointer to a struct timeval containing the timeout for the next
285 * chunk of data to be sent, or NULL if we're not sending right now.
287 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
292 para_fd_set(afs_socket, rfds, max_fileno);
294 for (i = 0; senders[i].name; i++)
295 senders[i].shutdown_clients();
299 return vss_compute_timeout();
302 if (vss_paused() || vss_repos()) {
303 for (i = 0; senders[i].name; i++)
304 senders[i].shutdown_clients();
307 gettimeofday(&now, NULL);
308 if (!vss_paused() || mmd->chunks_sent)
309 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
311 tv_add(&now, &announce_tv, &data_send_barrier);
312 if (mmd->new_vss_status_flags & VSS_NOMORE)
313 mmd->new_vss_status_flags = VSS_NEXT;
315 mmd->chunks_sent = 0;
318 mmd->new_vss_status_flags &= ~(VSS_REPOS);
319 mmd->current_chunk = mmd->repos_request;
321 tv = vss_compute_timeout();
324 if (!map && vss_playing() &&
325 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
326 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
327 if (afsss == AFS_SOCKET_READY) {
328 para_fd_set(afs_socket, wfds, max_fileno);
329 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
335 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
337 char control[255], buf[8];
338 struct msghdr msg = {.msg_iov = NULL};
339 struct cmsghdr *cmsg;
344 iov.iov_len = sizeof(buf);
347 msg.msg_control = control;
348 msg.msg_controllen = sizeof(control);
349 memset(buf, 0, sizeof(buf));
350 ret = recvmsg(afs_socket, &msg, 0);
352 return -ERRNO_TO_PARA_ERROR(errno);
353 if (iov.iov_len != sizeof(buf))
354 return -E_SHORT_AFS_READ;
355 *code = *(uint32_t*)buf;
356 *data = *(uint32_t*)(buf + 4);
357 cmsg = CMSG_FIRSTHDR(&msg);
358 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
359 if (cmsg->cmsg_level != SOL_SOCKET
360 || cmsg->cmsg_type != SCM_RIGHTS)
362 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
364 *fd = *(int *)CMSG_DATA(cmsg);
369 static void recv_afs_result(void)
371 int ret, passed_fd = -1, shmid;
372 uint32_t afs_code = 0, afs_data = 0;
376 PARA_NOTICE_LOG("recv\n");
377 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
380 PARA_NOTICE_LOG("got the fd: %d, code: %u, shmid: %u\n",
381 passed_fd, afs_code, afs_data);
382 ret = -E_BAD_AFS_CODE;
383 if (afs_code != NEXT_AUDIO_FILE)
385 afsss = AFS_SOCKET_READY;
387 ret = load_afd(shmid, &mmd->afd);
391 PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd->afd.path,
392 mmd->afd.afhi.chunks_total);
393 ret = fstat(passed_fd, &statbuf);
395 ret = -ERRNO_TO_PARA_ERROR(errno);
398 mmd->size = statbuf.st_size;
399 mmd->mtime = statbuf.st_mtime;
400 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
402 strcpy(mmd->filename, mmd->afd.path); /* FIXME: check length */
403 mmd->afi.header_len = 0; /* default: no header */
404 mmd->audio_format = mmd->afd.afsi.audio_format_id;
405 mmd->chunks_sent = 0;
406 mmd->current_chunk = 0;
410 mmd->afi = mmd->afd.afhi;
411 mmd->new_vss_status_flags &= (~VSS_NEXT);
412 gettimeofday(&now, NULL);
413 tv_add(&now, &announce_tv, &data_send_barrier);
418 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
421 void vss_post_select(fd_set *rfds, fd_set *wfds)
425 if (FD_ISSET(afs_socket, rfds))
427 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
429 PARA_NOTICE_LOG("requesting new socket\n");
430 ret = send_buffer(afs_socket, "new");
431 afsss = AFS_SOCKET_AFD_PENDING;
434 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
436 size_t pos = mmd->afi.chunk_table[chunk_num];
438 *len = mmd->afi.chunk_table[chunk_num + 1] - pos;
440 if (chunk_num + 5 > mmd->afd.afhi.chunks_total)
441 PARA_NOTICE_LOG("chunk %lu/%lu\n, len: %zu\n", chunk_num,
442 mmd->afd.afhi.chunks_total, *len);
446 * Get the data of the given chunk.
448 * \param chunk_num The number of the desired chunk.
449 * \param buf Chunk data.
450 * \param len Chunk length in bytes.
452 * \return Positive on success, negative on errors.
454 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
456 if (mmd->audio_format < 0 || !map || !vss_playing())
458 if (chunk_num >= mmd->afi.chunks_total)
460 get_chunk(chunk_num, buf, len);
465 * main sending function
467 * This function gets called from para_server as soon as the next chunk of
468 * data should be pushed out. It first calls the read_chunk() function of
469 * the current audio format handler to obtain a pointer to the data to be
470 * sent out as well as its length. This information is then passed to each
471 * supported sender's send() function which does the actual sending.
473 void vss_send_chunk(void)
476 struct timeval now, due;
480 if (mmd->audio_format < 0 || !map || !vss_playing())
482 gettimeofday(&now, NULL);
483 vss_next_chunk_time(&due);
484 if (tv_diff(&due, &now, NULL) > 0)
486 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
488 if (chk_barrier("data send", &now, &data_send_barrier,
491 mmd->new_vss_status_flags &= ~VSS_REPOS;
492 if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
493 mmd->new_vss_status_flags |= VSS_NEXT;
497 * We call the send function also in case of empty chunks as they
498 * might have still some data queued which can be sent in this case.
500 if (!mmd->chunks_sent) {
502 gettimeofday(&mmd->stream_start, NULL);
503 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
504 mmd->offset = tv2ms(&tmp);
507 get_chunk(mmd->current_chunk, &buf, &len);
508 for (i = 0; senders[i].name; i++)
509 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
510 mmd->new_vss_status_flags |= VSS_PLAYING;
512 mmd->current_chunk++;