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
;
47 /* The mp3 audio format handler does not need any libs. */
48 void mp3_init(struct audio_format_handler
*);
51 void ogg_init(struct audio_format_handler
*);
54 void aac_afh_init(struct audio_format_handler
*);
58 * The list of supported audio formats.
60 * We always define the full array of audio formats even if some audio formats
61 * were not compiled in. This is because for each audio file the number of its
62 * audio format is stored in the databse. We don't want that numbers to become
63 * stale just because the user installed a new version of paraslash that
64 * supports a different set of audio formats.
66 * It can still be easily detected whether an audio format is compiled in by
67 * checking if the init function pointer is not \p NULL.
69 static struct audio_format_handler afl
[] = {
91 static inline int next_audio_format(int format
)
94 if (!afl
[format
].name
)
103 /** Iterate over each supported audio format. */
104 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
108 * check if vss status flag \a P (playing) is set
110 * \return greater than zero if playing, zero otherwise.
113 unsigned int vss_playing(void)
115 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
119 * check if \a N (next) status flag is set
121 * \return greater than zero if set, zero if not.
124 unsigned int vss_next(void)
126 return mmd
->new_vss_status_flags
& VSS_NEXT
;
130 * check if a reposition request is pending
132 * \return greater than zero if true, zero otherwise.
135 unsigned int vss_repos(void)
137 return mmd
->new_vss_status_flags
& VSS_REPOS
;
141 * check if the vss is currently paused
143 * \return greater than zero if paused, zero otherwise.
146 unsigned int vss_paused(void)
148 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
149 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
153 * Get the name of the given audio format.
155 * \param i The audio format number.
157 * This returns a pointer to statically allocated memory so it
158 * must not be freed by the caller.
160 const char *audio_format_name(int i
)
162 //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
163 assert(i
< 0 || i
< ARRAY_SIZE(afl
) - 1);
164 return i
>= 0? afl
[i
].name
: "(none)";
168 * initialize the virtual streaming system
170 * Call the init functions of all supported audio format handlers and
171 * initialize all supported senders.
176 char *hn
= para_hostname(), *home
= para_homedir();
177 long unsigned announce_time
= conf
.announce_time_arg
> 0?
178 conf
.announce_time_arg
: 300,
179 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
180 conf
.autoplay_delay_arg
: 0;
182 PARA_DEBUG_LOG("supported audio formats: %s\n",
183 SUPPORTED_AUDIO_FORMATS
);
184 FOR_EACH_AUDIO_FORMAT(i
) {
185 PARA_NOTICE_LOG("initializing %s handler\n",
187 afl
[i
].init(&afl
[i
]);
189 ms2tv(announce_time
, &announce_tv
);
190 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv
));
191 for (i
= 0; senders
[i
].name
; i
++) {
192 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
193 senders
[i
].init(&senders
[i
]);
197 if (conf
.autoplay_given
) {
198 struct timeval now
, tmp
;
199 mmd
->vss_status_flags
|= VSS_PLAYING
;
200 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
201 gettimeofday(&now
, NULL
);
202 ms2tv(autoplay_delay
, &tmp
);
203 tv_add(&now
, &tmp
, &autoplay_barrier
);
208 * guess the audio format judging from filename
210 * \param name the filename
212 * \return This function returns -1 if it has no idea what kind of audio
213 * file this might be. Otherwise the (non-negative) number of the audio format
216 int guess_audio_format(const char *name
)
218 int i
,j
, len
= strlen(name
);
220 FOR_EACH_AUDIO_FORMAT(i
) {
221 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
222 const char *p
= afl
[i
].suffixes
[j
];
223 int plen
= strlen(p
);
226 if (name
[len
- plen
- 1] != '.')
228 if (strcasecmp(name
+ len
- plen
, p
))
230 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
234 return -E_BAD_AUDIO_FILE_SUFFIX
;
238 * Call get_file_info() to obtain an afhi structure.
240 * \param path The full path of the audio file.
241 * \param data Pointer to the contents of the (mapped) file.
242 * \param size The file size in bytes.
243 * \param afhi Result pointer.
245 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
246 * compiled in audio format handler is able to handler the file.
248 * This function tries to find an audio format handler that can interpret the
249 * file given by \a data and \a size.
251 * It first tries to determine the audio format from the filename given by \a
252 * path. If this doesn't work, all other audio format handlers are tried until
253 * one is found that can handle the file.
255 int compute_afhi(const char *path
, char *data
, size_t size
,
256 struct audio_format_info
*afhi
)
258 int ret
, i
, format
= guess_audio_format(path
);
261 ret
= afl
[format
].get_file_info(data
, size
, afhi
);
265 FOR_EACH_AUDIO_FORMAT(i
) {
266 if (i
== format
) /* we already tried this one to no avail */
268 ret
= afl
[i
].get_file_info(data
, size
, afhi
);
271 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret
));
273 return -E_AUDIO_FORMAT
;
276 static int chk_barrier(const char *bname
, const struct timeval
*now
,
277 const struct timeval
*barrier
, struct timeval
*diff
,
282 if (tv_diff(now
, barrier
, diff
) > 0)
286 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
290 static void vss_next_chunk_time(struct timeval
*due
)
294 tv_scale(mmd
->chunks_sent
, &mmd
->afi
.chunk_tv
, &tmp
);
295 tv_add(&tmp
, &mmd
->stream_start
, due
);
299 * != NULL: timeout for next chunk
300 * NULL: nothing to do
302 static struct timeval
*vss_compute_timeout(void)
304 static struct timeval the_timeout
;
305 struct timeval now
, next_chunk
;
307 if (vss_next() && mmd
->audio_format
>= 0) {
308 /* only sleep a bit, nec*/
309 the_timeout
.tv_sec
= 0;
310 the_timeout
.tv_usec
= 100;
313 gettimeofday(&now
, NULL
);
314 if (chk_barrier("autoplay_delay", &now
, &autoplay_barrier
,
315 &the_timeout
, 1) < 0)
317 if (chk_barrier("eof", &now
, &eof_barrier
, &the_timeout
, 1) < 0)
319 if (chk_barrier("data send", &now
, &data_send_barrier
,
320 &the_timeout
, 1) < 0)
322 if (mmd
->audio_format
< 0 || !vss_playing() || !map
)
324 vss_next_chunk_time(&next_chunk
);
325 if (chk_barrier(afl
[mmd
->audio_format
].name
, &now
, &next_chunk
,
326 &the_timeout
, 0) < 0)
328 /* chunk is due or bof */
329 the_timeout
.tv_sec
= 0;
330 the_timeout
.tv_usec
= 0;
334 static void vss_eof(void)
341 for (i
= 0; senders
[i
].name
; i
++)
342 senders
[i
].shutdown_clients();
345 gettimeofday(&now
, NULL
);
346 tv_add(&mmd
->afi
.eof_tv
, &now
, &eof_barrier
);
347 munmap(map
, mmd
->size
);
350 mmd
->audio_format
= -1;
351 mmd
->chunks_sent
= 0;
353 mmd
->afi
.seconds_total
= 0;
354 free(mmd
->afi
.chunk_table
);
355 mmd
->afi
.chunk_table
= NULL
;
356 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_INFO1
],
357 status_item_list
[SI_AUDIO_INFO2
], status_item_list
[SI_AUDIO_INFO3
]);
358 strcpy(mmd
->afi
.info_string
, tmp
);
360 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_DBINFO1
],
361 status_item_list
[SI_DBINFO2
], status_item_list
[SI_DBINFO3
]);
362 strcpy(mmd
->selector_info
, tmp
);
364 mmd
->filename
[0] = '\0';
370 * Get the header of the current audio file.
372 * \param header_len the length of the header is stored here
374 * \return a pointer to a buffer containing the header, or NULL, if no audio
375 * file is selected or if the current audio format does not need special header
379 char *vss_get_header(size_t *header_len
)
381 if (mmd
->audio_format
< 0 || !map
|| !mmd
->afi
.header_len
)
383 *header_len
= mmd
->afi
.header_len
;
384 return map
+ mmd
->afi
.header_offset
;
388 * get the list of all supported audio formats
390 * \return a space separated list of all supported audio formats
391 * It is not allocated at runtime, i.e. there is no need to free
392 * the returned string in the caller.
394 const char *supported_audio_formats(void)
396 return SUPPORTED_AUDIO_FORMATS
;
400 * get the chunk time of the current audio file
402 * \return a pointer to a struct containing the chunk time, or NULL,
403 * if currently no audio file is selected.
405 struct timeval
*vss_chunk_time(void)
407 if (mmd
->audio_format
< 0)
409 return &mmd
->afi
.chunk_tv
;
412 enum afs_socket_status
{
414 AFS_SOCKET_CHECK_FOR_WRITE
,
415 AFS_SOCKET_AFD_PENDING
418 static enum afs_socket_status afsss
;
421 * compute the timeout for para_server's main select-loop
423 * This function gets called from para_server to determine the timeout value
424 * for its main select loop.
426 * Before the timeout is computed, the current vss status flags are evaluated
427 * and acted upon by calling appropriate functions from the lower layers.
428 * Possible actions include
430 * - request a new file list from the current audio file selector
431 * - shutdown of all senders (stop/pause command)
432 * - reposition the stream (ff/jmp command)
434 * \return A pointer to a struct timeval containing the timeout for the next
435 * chunk of data to be sent, or NULL if we're not sending right now.
437 struct timeval
*vss_preselect(fd_set
*rfds
, fd_set
*wfds
, int *max_fileno
)
439 struct audio_format_handler
*af
= NULL
;
443 para_fd_set(afs_socket
, rfds
, max_fileno
);
446 format
= mmd
->audio_format
;
450 for (i
= 0; senders
[i
].name
; i
++)
451 senders
[i
].shutdown_clients();
452 if (vss_next() && af
) {
454 return vss_compute_timeout();
456 if (vss_paused() || vss_repos()) {
457 for (i
= 0; senders
[i
].name
; i
++)
458 senders
[i
].shutdown_clients();
461 gettimeofday(&now
, NULL
);
462 if (!vss_paused() || mmd
->chunks_sent
)
463 tv_add(&mmd
->afi
.eof_tv
, &now
, &eof_barrier
);
465 tv_add(&now
, &announce_tv
, &data_send_barrier
);
466 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
467 mmd
->new_vss_status_flags
= VSS_NEXT
;
469 mmd
->chunks_sent
= 0;
472 mmd
->new_vss_status_flags
&= ~(VSS_REPOS
);
473 mmd
->current_chunk
= mmd
->repos_request
;
475 ret
= vss_compute_timeout();
476 if (!ret
&& !map
&& vss_playing() &&
477 !(mmd
->new_vss_status_flags
& VSS_NOMORE
)) {
478 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
479 //vss_get_audio_file();
480 if (afsss
== AFS_SOCKET_READY
) {
481 para_fd_set(afs_socket
, wfds
, max_fileno
);
482 afsss
= AFS_SOCKET_CHECK_FOR_WRITE
;
489 static int recv_afs_msg(int *fd
, uint32_t *code
, uint32_t *data
)
491 char control
[255], buf
[8];
492 struct msghdr msg
= {.msg_iov
= NULL
};
493 struct cmsghdr
*cmsg
;
498 iov
.iov_len
= sizeof(buf
);
501 msg
.msg_control
= control
;
502 msg
.msg_controllen
= sizeof(control
);
503 memset(buf
, 0, sizeof(buf
));
504 ret
= recvmsg(afs_socket
, &msg
, 0);
506 return -ERRNO_TO_PARA_ERROR(errno
);
507 if (iov
.iov_len
!= sizeof(buf
))
508 return -E_SHORT_AFS_READ
;
509 *code
= *(uint32_t*)buf
;
510 *data
= *(uint32_t*)(buf
+ 4);
511 cmsg
= CMSG_FIRSTHDR(&msg
);
512 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
513 if (cmsg
->cmsg_level
!= SOL_SOCKET
514 || cmsg
->cmsg_type
!= SCM_RIGHTS
)
516 if ((cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int) != 1)
518 *fd
= *(int *)CMSG_DATA(cmsg
);
523 static void recv_afs_result(void)
525 int ret
, passed_fd
= -1, shmid
;
526 uint32_t afs_code
= 0, afs_data
= 0;
530 ret
= recv_afs_msg(&passed_fd
, &afs_code
, &afs_data
);
533 PARA_NOTICE_LOG("got the fd: %d, code: %u, shmid: %u\n",
534 passed_fd
, afs_code
, afs_data
);
535 ret
= -E_BAD_AFS_CODE
;
536 if (afs_code
!= NEXT_AUDIO_FILE
)
538 afsss
= AFS_SOCKET_READY
;
540 ret
= load_afd(shmid
, &mmd
->afd
);
544 PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd
->afd
.path
,
545 mmd
->afd
.afhi
.chunks_total
);
546 ret
= fstat(passed_fd
, &statbuf
);
548 ret
= -ERRNO_TO_PARA_ERROR(errno
);
551 mmd
->size
= statbuf
.st_size
;
552 mmd
->mtime
= statbuf
.st_mtime
;
553 map
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
,
555 strcpy(mmd
->filename
, mmd
->afd
.path
); /* FIXME: check length */
556 mmd
->afi
.header_len
= 0; /* default: no header */
557 mmd
->audio_format
= mmd
->afd
.afsi
.audio_format_id
;
558 mmd
->chunks_sent
= 0;
559 mmd
->current_chunk
= 0;
563 mmd
->afi
= mmd
->afd
.afhi
;
564 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
565 gettimeofday(&now
, NULL
);
566 tv_add(&now
, &announce_tv
, &data_send_barrier
);
571 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
574 void vss_post_select(fd_set
*rfds
, fd_set
*wfds
)
578 if (FD_ISSET(afs_socket
, rfds
))
580 if (afsss
!= AFS_SOCKET_CHECK_FOR_WRITE
|| !FD_ISSET(afs_socket
, wfds
))
582 ret
= send_buffer(afs_socket
, "new");
583 afsss
= AFS_SOCKET_AFD_PENDING
;
586 static void get_chunk(long unsigned chunk_num
, char **buf
, size_t *len
)
588 size_t pos
= mmd
->afi
.chunk_table
[chunk_num
];
590 *len
= mmd
->afi
.chunk_table
[chunk_num
+ 1] - pos
;
594 * Get the data of the given chunk.
596 * \param chunk_num The number of the desired chunk.
597 * \param buf Chunk data.
598 * \param len Chunk length in bytes.
600 * \return Positive on success, negative on errors.
602 int vss_get_chunk(long unsigned chunk_num
, char **buf
, size_t *len
)
604 if (mmd
->audio_format
< 0 || !map
|| !vss_playing())
606 if (chunk_num
>= mmd
->afi
.chunks_total
)
608 get_chunk(chunk_num
, buf
, len
);
613 * main sending function
615 * This function gets called from para_server as soon as the next chunk of
616 * data should be pushed out. It first calls the read_chunk() function of
617 * the current audio format handler to obtain a pointer to the data to be
618 * sent out as well as its length. This information is then passed to each
619 * supported sender's send() function which does the actual sending.
621 void vss_send_chunk(void)
624 struct audio_format_handler
*af
;
625 struct timeval now
, due
;
627 if (mmd
->audio_format
< 0 || !map
|| !vss_playing())
629 af
= &afl
[mmd
->audio_format
];
630 gettimeofday(&now
, NULL
);
631 vss_next_chunk_time(&due
);
632 if (tv_diff(&due
, &now
, NULL
) > 0)
634 if (chk_barrier("eof", &now
, &eof_barrier
, &due
, 1) < 0)
636 if (chk_barrier("data send", &now
, &data_send_barrier
,
639 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
640 if (mmd
->current_chunk
>= mmd
->afi
.chunks_total
) { /* eof */
641 mmd
->new_vss_status_flags
|= VSS_NEXT
;
645 * We call the send function also in case of empty chunks as they
646 * might have still some data queued which can be sent in this case.
648 if (!mmd
->chunks_sent
) {
650 gettimeofday(&mmd
->stream_start
, NULL
);
651 tv_scale(mmd
->current_chunk
, &mmd
->afi
.chunk_tv
, &tmp
);
652 mmd
->offset
= tv2ms(&tmp
);
655 for (i
= 0; senders
[i
].name
; i
++) {
658 get_chunk(mmd
->current_chunk
, &buf
, &len
);
659 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
, buf
, len
);
661 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
663 mmd
->current_chunk
++;