2 * Copyright (C) 1997-2008 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"
33 static struct timeval announce_tv;
34 static struct timeval data_send_barrier;
35 static struct timeval eof_barrier;
36 static struct timeval autoplay_barrier;
38 extern struct misc_meta_data *mmd;
39 extern struct sender senders[];
44 * Check if vss status flag \a P (playing) is set.
46 * \return Greater than zero if playing, zero otherwise.
49 unsigned int vss_playing(void)
51 return mmd->new_vss_status_flags & VSS_PLAYING;
55 * Check if the \a N (next) status flag is set.
57 * \return Greater than zero if set, zero if not.
60 unsigned int vss_next(void)
62 return mmd->new_vss_status_flags & VSS_NEXT;
66 * Check if a reposition request is pending.
68 * \return Greater than zero if true, zero otherwise.
71 unsigned int vss_repos(void)
73 return mmd->new_vss_status_flags & VSS_REPOS;
77 * Check if the vss is currently paused.
79 * \return Greater than zero if paused, zero otherwise.
82 unsigned int vss_paused(void)
84 return !(mmd->new_vss_status_flags & VSS_NEXT)
85 && !(mmd->new_vss_status_flags & VSS_PLAYING);
89 * Check if the vss is currently stopped.
91 * \return Greater than zero if paused, zero otherwise.
94 unsigned int vss_stopped(void)
96 return (mmd->new_vss_status_flags & VSS_NEXT)
97 && !(mmd->new_vss_status_flags & VSS_PLAYING);
101 * Initialize the virtual streaming system.
103 * This also initializes all supported senders and starts streaming
104 * if the --autoplay command line flag was given.
109 char *hn = para_hostname(), *home = para_homedir();
110 long unsigned announce_time = conf.announce_time_arg > 0?
111 conf.announce_time_arg : 300,
112 autoplay_delay = conf.autoplay_delay_arg > 0?
113 conf.autoplay_delay_arg : 0;
114 ms2tv(announce_time, &announce_tv);
115 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
116 for (i = 0; senders[i].name; i++) {
117 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
118 senders[i].init(&senders[i]);
122 if (conf.autoplay_given) {
123 struct timeval now, tmp;
124 mmd->vss_status_flags |= VSS_PLAYING;
125 mmd->new_vss_status_flags |= VSS_PLAYING;
126 gettimeofday(&now, NULL);
127 ms2tv(autoplay_delay, &tmp);
128 tv_add(&now, &tmp, &autoplay_barrier);
132 static int chk_barrier(const char *bname, const struct timeval *now,
133 const struct timeval *barrier, struct timeval *diff,
138 if (tv_diff(now, barrier, diff) > 0)
142 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
147 * != NULL: timeout for next chunk
148 * NULL: nothing to do
150 static struct timeval *vss_compute_timeout(void)
152 static struct timeval the_timeout;
153 struct timeval now, next_chunk;
155 if (vss_next() && map) {
156 /* only sleep a bit, nec*/
157 the_timeout.tv_sec = 0;
158 the_timeout.tv_usec = 100;
161 gettimeofday(&now, NULL);
162 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
163 &the_timeout, 1) < 0)
165 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
167 if (chk_barrier("data send", &now, &data_send_barrier,
168 &the_timeout, 1) < 0)
170 if (!vss_playing() || !map)
172 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
173 &mmd->stream_start, &next_chunk);
174 if (chk_barrier("chunk", &now, &next_chunk, &the_timeout, 0) < 0)
176 /* chunk is due or bof */
177 the_timeout.tv_sec = 0;
178 the_timeout.tv_usec = 0;
182 static void vss_eof(void)
189 for (i = 0; senders[i].name; i++)
190 senders[i].shutdown_clients();
193 if (mmd->new_vss_status_flags & VSS_NOMORE)
194 mmd->new_vss_status_flags = VSS_NEXT;
195 gettimeofday(&now, NULL);
196 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
197 munmap(map, mmd->size);
199 mmd->chunks_sent = 0;
201 mmd->afd.afhi.seconds_total = 0;
202 free(mmd->afd.afhi.chunk_table);
203 mmd->afd.afhi.chunk_table = NULL;
204 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
205 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
206 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
207 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
208 make_empty_status_items(mmd->afd.verbose_ls_output);
216 * Get the header of the current audio file.
218 * \param buf The length of the header is stored here.
219 * \param len Points to a buffer containing the header on return.
221 * \sa afh_get_header.
223 void vss_get_header(char **buf, size_t *len)
225 afh_get_header(&mmd->afd.afhi, map, buf, len);
229 * Get the list of all supported audio formats.
231 * \return Aa 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)
250 return &mmd->afd.afhi.chunk_tv;
253 /** The possible states of the afs socket. See \ref afs_socket. */
254 enum afs_socket_status {
255 /** Socket is inactive. */
257 /** Socket fd was included in the write fd set for select(). */
258 AFS_SOCKET_CHECK_FOR_WRITE,
259 /** vss wrote a request to the socket and waits for afs to reply. */
260 AFS_SOCKET_AFD_PENDING
263 static enum afs_socket_status afsss;
266 * Compute the timeout for para_server's main select-loop.
268 * This function gets called from para_server to determine the timeout value
269 * for its main select loop.
271 * \param rfds The set of file descriptors to be checked for reading.
272 * \param wfds The set of file descriptors to be checked for writing.
273 * \param max_fileno The highest-numbered file descriptor.
275 * Before the timeout is computed, the current vss status flags are evaluated
276 * and acted upon by calling appropriate functions from the lower layers.
277 * Possible actions include
279 * - request a new audio file from afs,
280 * - shutdown of all senders (stop/pause command),
281 * - reposition the stream (ff/jmp command).
283 * \return A pointer to a struct timeval containing the timeout for the next
284 * chunk of data to be sent, or NULL if we're not sending right now.
286 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
291 para_fd_set(afs_socket, rfds, max_fileno);
293 for (i = 0; senders[i].name; i++)
294 senders[i].shutdown_clients();
298 return vss_compute_timeout();
301 if (vss_paused() || vss_repos()) {
302 for (i = 0; senders[i].name; i++)
303 senders[i].shutdown_clients();
306 gettimeofday(&now, NULL);
307 if (!vss_paused() || mmd->chunks_sent)
308 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
310 tv_add(&now, &announce_tv, &data_send_barrier);
312 mmd->chunks_sent = 0;
315 mmd->new_vss_status_flags &= ~(VSS_REPOS);
316 mmd->current_chunk = mmd->repos_request;
318 tv = vss_compute_timeout();
321 if (!map && vss_playing() &&
322 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
323 if (afsss == AFS_SOCKET_READY ||
324 afsss == AFS_SOCKET_CHECK_FOR_WRITE) {
325 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
326 para_fd_set(afs_socket, wfds, max_fileno);
327 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
333 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
335 char control[255], buf[8];
336 struct msghdr msg = {.msg_iov = NULL};
337 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 afsss = AFS_SOCKET_READY;
353 if (iov.iov_len != sizeof(buf))
354 return -E_AFS_SHORT_READ;
355 *code = *(uint32_t*)buf;
356 *data = *(uint32_t*)(buf + 4);
357 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
358 if (cmsg->cmsg_level != SOL_SOCKET
359 || cmsg->cmsg_type != SCM_RIGHTS)
361 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
363 *fd = *(int *)CMSG_DATA(cmsg);
368 static void recv_afs_result(void)
370 int ret, passed_fd, shmid;
371 uint32_t afs_code = 0, afs_data = 0;
375 mmd->afd.afhi.chunk_table = NULL;
376 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
379 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
382 if (afs_code != NEXT_AUDIO_FILE)
387 ret = load_afd(shmid, &mmd->afd);
391 ret = fstat(passed_fd, &statbuf);
393 PARA_ERROR_LOG("fstat error:\n");
394 ret = -ERRNO_TO_PARA_ERROR(errno);
397 mmd->size = statbuf.st_size;
398 mmd->mtime = statbuf.st_mtime;
399 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
402 mmd->chunks_sent = 0;
403 mmd->current_chunk = 0;
407 mmd->new_vss_status_flags &= (~VSS_NEXT);
408 gettimeofday(&now, NULL);
409 tv_add(&now, &announce_tv, &data_send_barrier);
412 free(mmd->afd.afhi.chunk_table);
415 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
416 mmd->new_vss_status_flags = VSS_NEXT;
419 void vss_post_select(fd_set *rfds, fd_set *wfds)
423 if (FD_ISSET(afs_socket, rfds))
425 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
427 PARA_NOTICE_LOG("requesting new fd from afs\n");
428 ret = send_buffer(afs_socket, "new");
429 afsss = AFS_SOCKET_AFD_PENDING;
432 * Get the data of the given chunk.
434 * \param chunk_num The number of the desired chunk.
435 * \param buf Chunk data.
436 * \param len Chunk length in bytes.
440 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
442 if (!map || !vss_playing())
444 if (chunk_num >= mmd->afd.afhi.chunks_total)
446 afh_get_chunk(chunk_num, &mmd->afd.afhi, map, buf, len);
451 * Main sending function.
453 * This function gets called from para_server as soon as the next chunk of data
454 * should be pushed out. It obtains a pointer to the data to be sent out as
455 * well as its length from mmd->afd.afhi. This information is then passed to
456 * each supported sender's send() function which is supposed to send out the data
457 * to all connected clients.
459 void vss_send_chunk(void)
462 struct timeval now, due;
466 if (!map || !vss_playing())
468 gettimeofday(&now, NULL);
469 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
470 &mmd->stream_start, &due);
471 if (tv_diff(&due, &now, NULL) > 0)
473 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
475 if (chk_barrier("data send", &now, &data_send_barrier,
478 mmd->new_vss_status_flags &= ~VSS_REPOS;
479 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
480 mmd->new_vss_status_flags |= VSS_NEXT;
484 * We call the send function also in case of empty chunks as they
485 * might have still some data queued which can be sent in this case.
487 if (!mmd->chunks_sent) {
489 gettimeofday(&mmd->stream_start, NULL);
490 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
491 mmd->offset = tv2ms(&tmp);
494 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, map, &buf, &len);
495 for (i = 0; senders[i].name; i++)
496 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
497 mmd->new_vss_status_flags |= VSS_PLAYING;
499 mmd->current_chunk++;