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 * Get the data of the given chunk.
103 * \param chunk_num The number of the desired chunk.
104 * \param buf Chunk data.
105 * \param len Chunk length in bytes.
109 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
111 if (!map || !vss_playing())
113 if (chunk_num >= mmd->afd.afhi.chunks_total)
115 afh_get_chunk(chunk_num, &mmd->afd.afhi, map, buf, len);
120 * Initialize the virtual streaming system.
122 * This also initializes all supported senders and starts streaming
123 * if the --autoplay command line flag was given.
128 char *hn = para_hostname(), *home = para_homedir();
129 long unsigned announce_time = conf.announce_time_arg > 0?
130 conf.announce_time_arg : 300,
131 autoplay_delay = conf.autoplay_delay_arg > 0?
132 conf.autoplay_delay_arg : 0;
133 ms2tv(announce_time, &announce_tv);
134 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
135 for (i = 0; senders[i].name; i++) {
136 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
137 senders[i].init(&senders[i]);
141 mmd->sender_cmd_data.cmd_num = -1;
142 if (conf.autoplay_given) {
143 struct timeval now, tmp;
144 mmd->vss_status_flags |= VSS_PLAYING;
145 mmd->new_vss_status_flags |= VSS_PLAYING;
146 gettimeofday(&now, NULL);
147 ms2tv(autoplay_delay, &tmp);
148 tv_add(&now, &tmp, &autoplay_barrier);
152 static int chk_barrier(const char *bname, const struct timeval *now,
153 const struct timeval *barrier, struct timeval *diff,
158 if (tv_diff(now, barrier, diff) > 0)
162 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
167 * != NULL: timeout for next chunk
168 * NULL: nothing to do
170 static struct timeval *vss_compute_timeout(void)
172 static struct timeval the_timeout;
173 struct timeval now, next_chunk;
175 if (vss_next() && map) {
176 /* only sleep a bit, nec*/
177 the_timeout.tv_sec = 0;
178 the_timeout.tv_usec = 100;
181 gettimeofday(&now, NULL);
182 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
183 &the_timeout, 1) < 0)
185 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
187 if (chk_barrier("data send", &now, &data_send_barrier,
188 &the_timeout, 1) < 0)
190 if (!vss_playing() || !map)
192 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
193 &mmd->stream_start, &next_chunk);
194 if (chk_barrier("chunk", &now, &next_chunk, &the_timeout, 0) < 0)
196 /* chunk is due or bof */
197 the_timeout.tv_sec = 0;
198 the_timeout.tv_usec = 0;
202 static void vss_eof(void)
207 if (mmd->new_vss_status_flags & VSS_NOMORE)
208 mmd->new_vss_status_flags = VSS_NEXT;
209 gettimeofday(&now, NULL);
210 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
211 munmap(map, mmd->size);
213 mmd->chunks_sent = 0;
215 mmd->afd.afhi.seconds_total = 0;
216 free(mmd->afd.afhi.chunk_table);
217 mmd->afd.afhi.chunk_table = NULL;
218 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
219 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
220 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
221 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
222 make_empty_status_items(mmd->afd.verbose_ls_output);
230 * Get the header of the current audio file.
232 * \param buf The length of the header is stored here.
233 * \param len Points to a buffer containing the header on return.
235 * \sa afh_get_header.
237 void vss_get_header(char **buf, size_t *len)
239 afh_get_header(&mmd->afd.afhi, map, buf, len);
243 * Get the list of all supported audio formats.
245 * \return Aa space separated list of all supported audio formats
246 * It is not allocated at runtime, i.e. there is no need to free
247 * the returned string in the caller.
249 const char *supported_audio_formats(void)
251 return SUPPORTED_AUDIO_FORMATS;
255 * Get the chunk time of the current audio file.
257 * \return A pointer to a struct containing the chunk time, or NULL,
258 * if currently no audio file is selected.
260 struct timeval *vss_chunk_time(void)
264 return &mmd->afd.afhi.chunk_tv;
267 /** The possible states of the afs socket. See \ref afs_socket. */
268 enum afs_socket_status {
269 /** Socket is inactive. */
271 /** Socket fd was included in the write fd set for select(). */
272 AFS_SOCKET_CHECK_FOR_WRITE,
273 /** vss wrote a request to the socket and waits for afs to reply. */
274 AFS_SOCKET_AFD_PENDING
277 static enum afs_socket_status afsss;
279 static int need_to_request_new_audio_file(void)
281 if (map) /* have audio file */
283 if (!vss_playing()) /* don't need one */
285 if (mmd->new_vss_status_flags & VSS_NOMORE)
287 if (afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
293 * Compute the timeout for para_server's main select-loop.
295 * This function gets called from para_server to determine the timeout value
296 * for its main select loop.
298 * \param rfds The set of file descriptors to be checked for reading.
299 * \param wfds The set of file descriptors to be checked for writing.
300 * \param max_fileno The highest-numbered file descriptor.
302 * Before the timeout is computed, the current vss status flags are evaluated
303 * and acted upon by calling appropriate functions from the lower layers.
304 * Possible actions include
306 * - request a new audio file from afs,
307 * - shutdown of all senders (stop/pause command),
308 * - reposition the stream (ff/jmp command).
310 * \return A pointer to a struct timeval containing the timeout for the next
311 * chunk of data to be sent, or NULL if we're not sending right now.
313 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
318 if (!map || vss_next() || vss_paused() || vss_repos())
319 for (i = 0; senders[i].name; i++)
320 senders[i].shutdown_clients();
323 else if (vss_paused()) {
324 if (mmd->chunks_sent) {
325 gettimeofday(&now, NULL);
326 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
328 mmd->chunks_sent = 0;
329 } else if (vss_repos()) {
330 gettimeofday(&now, NULL);
331 tv_add(&now, &announce_tv, &data_send_barrier);
332 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
333 mmd->chunks_sent = 0;
334 mmd->current_chunk = mmd->repos_request;
335 mmd->new_vss_status_flags &= ~VSS_REPOS;
338 if (need_to_request_new_audio_file()) {
339 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
340 para_fd_set(afs_socket, wfds, max_fileno);
341 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
343 para_fd_set(afs_socket, rfds, max_fileno);
344 for (i = 0; senders[i].name; i++) {
345 if (!senders[i].pre_select)
347 senders[i].pre_select(max_fileno, rfds, wfds);
349 return vss_compute_timeout();
352 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
354 char control[255], buf[8];
355 struct msghdr msg = {.msg_iov = NULL};
356 struct cmsghdr *cmsg;
362 iov.iov_len = sizeof(buf);
365 msg.msg_control = control;
366 msg.msg_controllen = sizeof(control);
367 memset(buf, 0, sizeof(buf));
368 ret = recvmsg(afs_socket, &msg, 0);
370 return -ERRNO_TO_PARA_ERROR(errno);
371 afsss = AFS_SOCKET_READY;
372 if (iov.iov_len != sizeof(buf))
373 return -E_AFS_SHORT_READ;
374 *code = *(uint32_t*)buf;
375 *data = *(uint32_t*)(buf + 4);
376 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
377 if (cmsg->cmsg_level != SOL_SOCKET
378 || cmsg->cmsg_type != SCM_RIGHTS)
380 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
382 *fd = *(int *)CMSG_DATA(cmsg);
387 static void recv_afs_result(void)
389 int ret, passed_fd, shmid;
390 uint32_t afs_code = 0, afs_data = 0;
394 mmd->afd.afhi.chunk_table = NULL;
395 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
398 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
401 if (afs_code != NEXT_AUDIO_FILE)
406 ret = load_afd(shmid, &mmd->afd);
410 ret = fstat(passed_fd, &statbuf);
412 PARA_ERROR_LOG("fstat error:\n");
413 ret = -ERRNO_TO_PARA_ERROR(errno);
416 mmd->size = statbuf.st_size;
417 mmd->mtime = statbuf.st_mtime;
418 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
421 mmd->chunks_sent = 0;
422 mmd->current_chunk = 0;
426 mmd->new_vss_status_flags &= (~VSS_NEXT);
427 gettimeofday(&now, NULL);
428 tv_add(&now, &announce_tv, &data_send_barrier);
431 free(mmd->afd.afhi.chunk_table);
434 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
435 mmd->new_vss_status_flags = VSS_NEXT;
439 * Main sending function.
441 * This function gets called from para_server as soon as the next chunk of data
442 * should be pushed out. It obtains a pointer to the data to be sent out as
443 * well as its length from mmd->afd.afhi. This information is then passed to
444 * each supported sender's send() function which is supposed to send out the data
445 * to all connected clients.
447 static void vss_send_chunk(void)
450 struct timeval now, due;
454 if (!map || !vss_playing())
456 gettimeofday(&now, NULL);
457 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
458 &mmd->stream_start, &due);
459 if (tv_diff(&due, &now, NULL) > 0)
461 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
463 if (chk_barrier("data send", &now, &data_send_barrier,
466 mmd->new_vss_status_flags &= ~VSS_REPOS;
467 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
468 mmd->new_vss_status_flags |= VSS_NEXT;
472 * We call the send function also in case of empty chunks as they
473 * might have still some data queued which can be sent in this case.
475 if (!mmd->chunks_sent) {
477 gettimeofday(&mmd->stream_start, NULL);
478 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
479 mmd->offset = tv2ms(&tmp);
482 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, map, &buf, &len);
483 for (i = 0; senders[i].name; i++)
484 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
485 mmd->new_vss_status_flags |= VSS_PLAYING;
487 mmd->current_chunk++;
490 void vss_post_select(fd_set *rfds, fd_set *wfds)
494 if (mmd->sender_cmd_data.cmd_num >= 0) {
495 int num = mmd->sender_cmd_data.cmd_num,
496 s = mmd->sender_cmd_data.sender_num;
498 if (senders[s].client_cmds[num])
499 senders[s].client_cmds[num](&mmd->sender_cmd_data);
500 mmd->sender_cmd_data.cmd_num = -1;
502 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
503 if (FD_ISSET(afs_socket, rfds))
505 } else if (FD_ISSET(afs_socket, wfds)) {
506 PARA_NOTICE_LOG("requesting new fd from afs\n");
507 ret = send_buffer(afs_socket, "new");
508 afsss = AFS_SOCKET_AFD_PENDING;
510 for (i = 0; senders[i].name; i++) {
511 if (!senders[i].post_select)
513 senders[i].post_select(rfds, wfds);