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 static struct timeval announce_tv;
33 static struct timeval data_send_barrier;
34 static struct timeval eof_barrier;
35 static struct timeval autoplay_barrier;
37 extern struct misc_meta_data *mmd;
38 extern struct sender senders[];
43 * Check if vss status flag \a P (playing) is set.
45 * \return Greater than zero if playing, zero otherwise.
48 unsigned int vss_playing(void)
50 return mmd->new_vss_status_flags & VSS_PLAYING;
54 * Check if the \a N (next) status flag is set.
56 * \return Greater than zero if set, zero if not.
59 unsigned int vss_next(void)
61 return mmd->new_vss_status_flags & VSS_NEXT;
65 * Check if a reposition request is pending.
67 * \return Greater than zero if true, zero otherwise.
70 unsigned int vss_repos(void)
72 return mmd->new_vss_status_flags & VSS_REPOS;
76 * Check if the vss is currently paused.
78 * \return Greater than zero if paused, zero otherwise.
81 unsigned int vss_paused(void)
83 return !(mmd->new_vss_status_flags & VSS_NEXT)
84 && !(mmd->new_vss_status_flags & VSS_PLAYING);
88 * Initialize the virtual streaming system.
90 * This also initializes all supported senders and starts streaming
91 * if the --autoplay command line flag was given.
96 char *hn = para_hostname(), *home = para_homedir();
97 long unsigned announce_time = conf.announce_time_arg > 0?
98 conf.announce_time_arg : 300,
99 autoplay_delay = conf.autoplay_delay_arg > 0?
100 conf.autoplay_delay_arg : 0;
101 ms2tv(announce_time, &announce_tv);
102 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
103 for (i = 0; senders[i].name; i++) {
104 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
105 senders[i].init(&senders[i]);
109 if (conf.autoplay_given) {
110 struct timeval now, tmp;
111 mmd->vss_status_flags |= VSS_PLAYING;
112 mmd->new_vss_status_flags |= VSS_PLAYING;
113 gettimeofday(&now, NULL);
114 ms2tv(autoplay_delay, &tmp);
115 tv_add(&now, &tmp, &autoplay_barrier);
119 static int chk_barrier(const char *bname, const struct timeval *now,
120 const struct timeval *barrier, struct timeval *diff,
125 if (tv_diff(now, barrier, diff) > 0)
129 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
133 static void vss_next_chunk_time(struct timeval *due)
137 tv_scale(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv, &tmp);
138 tv_add(&tmp, &mmd->stream_start, due);
142 * != NULL: timeout for next chunk
143 * NULL: nothing to do
145 static struct timeval *vss_compute_timeout(void)
147 static struct timeval the_timeout;
148 struct timeval now, next_chunk;
150 if (vss_next() && map) {
151 /* only sleep a bit, nec*/
152 the_timeout.tv_sec = 0;
153 the_timeout.tv_usec = 100;
156 gettimeofday(&now, NULL);
157 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
158 &the_timeout, 1) < 0)
160 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
162 if (chk_barrier("data send", &now, &data_send_barrier,
163 &the_timeout, 1) < 0)
165 if (!vss_playing() || !map)
167 vss_next_chunk_time(&next_chunk);
168 if (chk_barrier("chunk", &now, &next_chunk, &the_timeout, 0) < 0)
170 /* chunk is due or bof */
171 the_timeout.tv_sec = 0;
172 the_timeout.tv_usec = 0;
176 static void vss_eof(void)
183 for (i = 0; senders[i].name; i++)
184 senders[i].shutdown_clients();
187 if (mmd->new_vss_status_flags & VSS_NOMORE)
188 mmd->new_vss_status_flags = VSS_NEXT;
189 gettimeofday(&now, NULL);
190 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
191 munmap(map, mmd->size);
193 mmd->chunks_sent = 0;
195 mmd->afd.afhi.seconds_total = 0;
196 free(mmd->afd.afhi.chunk_table);
197 mmd->afd.afhi.chunk_table = NULL;
198 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
199 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
200 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
201 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
202 make_empty_status_items(mmd->afd.verbose_ls_output);
210 * Get the header of the current audio file.
212 * \param header_len The length of the header is stored here.
214 * \return A pointer to a buffer containing the header, or NULL, if no audio
215 * file is selected or if the current audio format does not need special header
219 char *vss_get_header(size_t *header_len)
221 if (!map || !mmd->afd.afhi.header_len)
223 *header_len = mmd->afd.afhi.header_len;
224 return map + mmd->afd.afhi.header_offset;
228 * Get the list of all supported audio formats.
230 * \return Aa space separated list of all supported audio formats
231 * It is not allocated at runtime, i.e. there is no need to free
232 * the returned string in the caller.
234 const char *supported_audio_formats(void)
236 return SUPPORTED_AUDIO_FORMATS;
240 * Get the chunk time of the current audio file.
242 * \return A pointer to a struct containing the chunk time, or NULL,
243 * if currently no audio file is selected.
245 struct timeval *vss_chunk_time(void)
249 return &mmd->afd.afhi.chunk_tv;
252 enum afs_socket_status {
254 AFS_SOCKET_CHECK_FOR_WRITE,
255 AFS_SOCKET_AFD_PENDING
258 static enum afs_socket_status afsss;
261 * Compute the timeout for para_server's main select-loop.
263 * This function gets called from para_server to determine the timeout value
264 * for its main select loop.
266 * \param rfds The set of file descriptors to be checked for reading.
267 * \param wfds The set of file descriptors to be checked for writing.
268 * \param max_fileno The highest-numbered file descriptor.
270 * Before the timeout is computed, the current vss status flags are evaluated
271 * and acted upon by calling appropriate functions from the lower layers.
272 * Possible actions include
274 * - request a new audio file from afs,
275 * - shutdown of all senders (stop/pause command),
276 * - reposition the stream (ff/jmp command).
278 * \return A pointer to a struct timeval containing the timeout for the next
279 * chunk of data to be sent, or NULL if we're not sending right now.
281 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
286 para_fd_set(afs_socket, rfds, max_fileno);
288 for (i = 0; senders[i].name; i++)
289 senders[i].shutdown_clients();
293 return vss_compute_timeout();
296 if (vss_paused() || vss_repos()) {
297 for (i = 0; senders[i].name; i++)
298 senders[i].shutdown_clients();
301 gettimeofday(&now, NULL);
302 if (!vss_paused() || mmd->chunks_sent)
303 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
305 tv_add(&now, &announce_tv, &data_send_barrier);
307 mmd->chunks_sent = 0;
310 mmd->new_vss_status_flags &= ~(VSS_REPOS);
311 mmd->current_chunk = mmd->repos_request;
313 tv = vss_compute_timeout();
316 if (!map && vss_playing() &&
317 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
318 if (afsss == AFS_SOCKET_READY ||
319 afsss == AFS_SOCKET_CHECK_FOR_WRITE) {
320 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
321 para_fd_set(afs_socket, wfds, max_fileno);
322 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
328 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
330 char control[255], buf[8];
331 struct msghdr msg = {.msg_iov = NULL};
332 struct cmsghdr *cmsg;
337 iov.iov_len = sizeof(buf);
340 msg.msg_control = control;
341 msg.msg_controllen = sizeof(control);
342 memset(buf, 0, sizeof(buf));
343 ret = recvmsg(afs_socket, &msg, 0);
345 return -ERRNO_TO_PARA_ERROR(errno);
346 if (iov.iov_len != sizeof(buf))
347 return -E_SHORT_AFS_READ;
348 *code = *(uint32_t*)buf;
349 *data = *(uint32_t*)(buf + 4);
350 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
351 if (cmsg->cmsg_level != SOL_SOCKET
352 || cmsg->cmsg_type != SCM_RIGHTS)
354 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
356 *fd = *(int *)CMSG_DATA(cmsg);
361 static void recv_afs_result(void)
363 int ret, passed_fd = -1, shmid;
364 uint32_t afs_code = 0, afs_data = 0;
368 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
371 PARA_DEBUG_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 ret = fstat(passed_fd, &statbuf);
384 PARA_ERROR_LOG("fstat error:\n");
385 ret = -ERRNO_TO_PARA_ERROR(errno);
388 mmd->size = statbuf.st_size;
389 mmd->mtime = statbuf.st_mtime;
390 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
393 mmd->chunks_sent = 0;
394 mmd->current_chunk = 0;
398 mmd->new_vss_status_flags &= (~VSS_NEXT);
399 gettimeofday(&now, NULL);
400 tv_add(&now, &announce_tv, &data_send_barrier);
405 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
408 void vss_post_select(fd_set *rfds, fd_set *wfds)
412 if (FD_ISSET(afs_socket, rfds))
414 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
416 PARA_NOTICE_LOG("requesting new fd from afs\n");
417 ret = send_buffer(afs_socket, "new");
418 afsss = AFS_SOCKET_AFD_PENDING;
421 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
423 size_t pos = mmd->afd.afhi.chunk_table[chunk_num];
425 *len = mmd->afd.afhi.chunk_table[chunk_num + 1] - pos;
429 * Get the data of the given chunk.
431 * \param chunk_num The number of the desired chunk.
432 * \param buf Chunk data.
433 * \param len Chunk length in bytes.
437 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
439 if (!map || !vss_playing())
441 if (chunk_num >= mmd->afd.afhi.chunks_total)
443 get_chunk(chunk_num, buf, len);
448 * Main sending function.
450 * This function gets called from para_server as soon as the next chunk of data
451 * should be pushed out. It obtains a pointer to the data to be sent out as
452 * well as its length from mmd->afd.afhi. This information is then passed to
453 * each supported sender's send() function which is supposed to send out the data
454 * to all connected clients.
456 void vss_send_chunk(void)
459 struct timeval now, due;
463 if (!map || !vss_playing())
465 gettimeofday(&now, NULL);
466 vss_next_chunk_time(&due);
467 if (tv_diff(&due, &now, NULL) > 0)
469 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
471 if (chk_barrier("data send", &now, &data_send_barrier,
474 mmd->new_vss_status_flags &= ~VSS_REPOS;
475 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
476 mmd->new_vss_status_flags |= VSS_NEXT;
480 * We call the send function also in case of empty chunks as they
481 * might have still some data queued which can be sent in this case.
483 if (!mmd->chunks_sent) {
485 gettimeofday(&mmd->stream_start, NULL);
486 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
487 mmd->offset = tv2ms(&tmp);
490 get_chunk(mmd->current_chunk, &buf, &len);
491 for (i = 0; senders[i].name; i++)
492 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
493 mmd->new_vss_status_flags |= VSS_PLAYING;
495 mmd->current_chunk++;