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 the \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 * This also initializes all supported senders and starts streaming
93 * if the --autoplay command line flag was given.
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->afd.afhi.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() && map) {
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 (!vss_playing() || !map)
169 vss_next_chunk_time(&next_chunk);
170 if (chk_barrier(audio_format_name(mmd->afd.afsi.audio_format_id),
171 &now, &next_chunk, &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)
186 for (i = 0; senders[i].name; i++)
187 senders[i].shutdown_clients();
190 gettimeofday(&now, NULL);
191 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
192 munmap(map, mmd->size);
194 mmd->chunks_sent = 0;
196 mmd->afd.afhi.seconds_total = 0;
197 free(mmd->afd.afhi.chunk_table);
198 mmd->afd.afhi.chunk_table = NULL;
199 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
200 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
201 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
202 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
204 mmd->afd.path[0] = '\0';
205 mmd->afd.afsi.lyrics_id = 0;
206 mmd->afd.afsi.image_id = 0;
208 mmd->afd.attributes_string[0] = '\0';
214 * Get the header of the current audio file.
216 * \param header_len The length of the header is stored here.
218 * \return A pointer to a buffer containing the header, or NULL, if no audio
219 * file is selected or if the current audio format does not need special header
223 char *vss_get_header(size_t *header_len)
225 if (!map || !mmd->afd.afhi.header_len)
227 *header_len = mmd->afd.afhi.header_len;
228 return map + mmd->afd.afhi.header_offset;
232 * Get the list of all supported audio formats.
234 * \return Aa space separated list of all supported audio formats
235 * It is not allocated at runtime, i.e. there is no need to free
236 * the returned string in the caller.
238 const char *supported_audio_formats(void)
240 return SUPPORTED_AUDIO_FORMATS;
244 * Get the chunk time of the current audio file.
246 * \return A pointer to a struct containing the chunk time, or NULL,
247 * if currently no audio file is selected.
249 struct timeval *vss_chunk_time(void)
253 return &mmd->afd.afhi.chunk_tv;
256 enum afs_socket_status {
258 AFS_SOCKET_CHECK_FOR_WRITE,
259 AFS_SOCKET_AFD_PENDING
262 static enum afs_socket_status afsss;
265 * Compute the timeout for para_server's main select-loop.
267 * This function gets called from para_server to determine the timeout value
268 * for its main select loop.
270 * \param rfds The set of file descriptors to be checked for reading.
271 * \param wfds The set of file descriptors to be checked for writing.
272 * \param max_fileno The highest-numbered file descriptor.
274 * Before the timeout is computed, the current vss status flags are evaluated
275 * and acted upon by calling appropriate functions from the lower layers.
276 * Possible actions include
278 * - request a new audio file from afs,
279 * - shutdown of all senders (stop/pause command),
280 * - reposition the stream (ff/jmp command).
282 * \return A pointer to a struct timeval containing the timeout for the next
283 * chunk of data to be sent, or NULL if we're not sending right now.
285 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
290 para_fd_set(afs_socket, rfds, max_fileno);
292 for (i = 0; senders[i].name; i++)
293 senders[i].shutdown_clients();
297 return vss_compute_timeout();
300 if (vss_paused() || vss_repos()) {
301 for (i = 0; senders[i].name; i++)
302 senders[i].shutdown_clients();
305 gettimeofday(&now, NULL);
306 if (!vss_paused() || mmd->chunks_sent)
307 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
309 tv_add(&now, &announce_tv, &data_send_barrier);
310 if (mmd->new_vss_status_flags & VSS_NOMORE)
311 mmd->new_vss_status_flags = VSS_NEXT;
313 mmd->chunks_sent = 0;
316 mmd->new_vss_status_flags &= ~(VSS_REPOS);
317 mmd->current_chunk = mmd->repos_request;
319 tv = vss_compute_timeout();
322 if (!map && vss_playing() &&
323 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
324 if (afsss == AFS_SOCKET_READY ||
325 afsss == AFS_SOCKET_CHECK_FOR_WRITE) {
326 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
327 para_fd_set(afs_socket, wfds, max_fileno);
328 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
334 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
336 char control[255], buf[8];
337 struct msghdr msg = {.msg_iov = NULL};
338 struct cmsghdr *cmsg;
343 iov.iov_len = sizeof(buf);
346 msg.msg_control = control;
347 msg.msg_controllen = sizeof(control);
348 memset(buf, 0, sizeof(buf));
349 ret = recvmsg(afs_socket, &msg, 0);
351 return -ERRNO_TO_PARA_ERROR(errno);
352 if (iov.iov_len != sizeof(buf))
353 return -E_SHORT_AFS_READ;
354 *code = *(uint32_t*)buf;
355 *data = *(uint32_t*)(buf + 4);
356 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
357 if (cmsg->cmsg_level != SOL_SOCKET
358 || cmsg->cmsg_type != SCM_RIGHTS)
360 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
362 *fd = *(int *)CMSG_DATA(cmsg);
367 static void recv_afs_result(void)
369 int ret, passed_fd = -1, shmid;
370 uint32_t afs_code = 0, afs_data = 0;
374 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
377 PARA_DEBUG_LOG("got the fd: %d, code: %u, shmid: %u\n",
378 passed_fd, afs_code, afs_data);
379 ret = -E_BAD_AFS_CODE;
380 if (afs_code != NEXT_AUDIO_FILE)
382 afsss = AFS_SOCKET_READY;
384 ret = load_afd(shmid, &mmd->afd);
388 PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd->afd.path,
389 mmd->afd.afhi.chunks_total);
390 ret = fstat(passed_fd, &statbuf);
392 PARA_ERROR_LOG("fstat error:\n");
393 ret = -ERRNO_TO_PARA_ERROR(errno);
396 mmd->size = statbuf.st_size;
397 mmd->mtime = statbuf.st_mtime;
398 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
401 mmd->chunks_sent = 0;
402 mmd->current_chunk = 0;
406 mmd->new_vss_status_flags &= (~VSS_NEXT);
407 gettimeofday(&now, NULL);
408 tv_add(&now, &announce_tv, &data_send_barrier);
413 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
416 void vss_post_select(fd_set *rfds, fd_set *wfds)
420 if (FD_ISSET(afs_socket, rfds))
422 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
424 PARA_NOTICE_LOG("requesting new fd from afs\n");
425 ret = send_buffer(afs_socket, "new");
426 afsss = AFS_SOCKET_AFD_PENDING;
429 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
431 size_t pos = mmd->afd.afhi.chunk_table[chunk_num];
433 *len = mmd->afd.afhi.chunk_table[chunk_num + 1] - pos;
437 * Get the data of the given chunk.
439 * \param chunk_num The number of the desired chunk.
440 * \param buf Chunk data.
441 * \param len Chunk length in bytes.
445 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
447 if (!map || !vss_playing())
449 if (chunk_num >= mmd->afd.afhi.chunks_total)
451 get_chunk(chunk_num, buf, len);
456 * Main sending function.
458 * This function gets called from para_server as soon as the next chunk of data
459 * should be pushed out. It obtains a pointer to the data to be sent out as
460 * well as its length from mmd->afd.afhi. This information is then passed to
461 * each supported sender's send() function which is supposed to send out the data
462 * to all connected clients.
464 void vss_send_chunk(void)
467 struct timeval now, due;
471 if (!map || !vss_playing())
473 gettimeofday(&now, NULL);
474 vss_next_chunk_time(&due);
475 if (tv_diff(&due, &now, NULL) > 0)
477 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
479 if (chk_barrier("data send", &now, &data_send_barrier,
482 mmd->new_vss_status_flags &= ~VSS_REPOS;
483 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
484 mmd->new_vss_status_flags |= VSS_NEXT;
488 * We call the send function also in case of empty chunks as they
489 * might have still some data queued which can be sent in this case.
491 if (!mmd->chunks_sent) {
493 gettimeofday(&mmd->stream_start, NULL);
494 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
495 mmd->offset = tv2ms(&tmp);
498 get_chunk(mmd->current_chunk, &buf, &len);
499 for (i = 0; senders[i].name; i++)
500 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
501 mmd->new_vss_status_flags |= VSS_PLAYING;
503 mmd->current_chunk++;