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
17 #include <sys/mman.h> /* mmap */
18 #include <sys/time.h> /* gettimeofday */
19 #include "server.cmdline.h"
20 #include "afs_common.h"
27 extern const char *status_item_list[];
29 static struct timeval announce_tv;
30 static struct timeval data_send_barrier;
31 static struct timeval eof_barrier;
32 static struct timeval autoplay_barrier;
34 extern struct misc_meta_data *mmd;
35 extern struct audio_file_selector selectors[];
36 extern struct sender senders[];
38 static int audio_file;
41 /* The mp3 audio format handler does not need any libs. */
42 void mp3_init(struct audio_format_handler *);
45 void ogg_init(struct audio_format_handler *);
48 void aac_afh_init(struct audio_format_handler *);
52 * The list of supported audio formats.
54 * We always define the full array of audio formats even if some audio formats
55 * were not compiled in. This is because for each audio file the number of its
56 * audio format is stored in the databse. We don't want that numbers to become
57 * stale just because the user installed a new version of paraslash that
58 * supports a different set of audio formats.
60 * It can still be easily detected whether an audio format is compiled in by
61 * checking if the init function pointer is not \p NULL.
63 static struct audio_format_handler afl[] = {
85 static inline int next_audio_format(int format)
88 if (!afl[format].name)
97 /** Iterate over each supported audio format. */
98 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
102 * check if vss status flag \a P (playing) is set
104 * \return greater than zero if playing, zero otherwise.
107 unsigned int vss_playing(void)
109 return mmd->new_vss_status_flags & VSS_PLAYING;
113 * check if \a N (next) status flag is set
115 * \return greater than zero if set, zero if not.
118 unsigned int vss_next(void)
120 return mmd->new_vss_status_flags & VSS_NEXT;
124 * check if a reposition request is pending
126 * \return greater than zero if true, zero otherwise.
129 unsigned int vss_repos(void)
131 return mmd->new_vss_status_flags & VSS_REPOS;
135 * check if the vss is currently paused
137 * \return greater than zero if paused, zero otherwise.
140 unsigned int vss_paused(void)
142 return !(mmd->new_vss_status_flags & VSS_NEXT)
143 && !(mmd->new_vss_status_flags & VSS_PLAYING);
147 * get the name of the given audio format
149 * \param i the audio format number
151 * This returns a pointer to statically allocated memory so it
152 * must not be freed by the caller.
154 const char *audio_format_name(int i)
156 return i >= 0? afl[i].name : "(none)";
160 * initialize the virtual streaming system
162 * Call the init functions of all supported audio format handlers and
163 * initialize all supported senders.
168 char *hn = para_hostname(), *home = para_homedir();
169 long unsigned announce_time = conf.announce_time_arg > 0?
170 conf.announce_time_arg : 300,
171 autoplay_delay = conf.autoplay_delay_arg > 0?
172 conf.autoplay_delay_arg : 0;
174 PARA_DEBUG_LOG("supported audio formats: %s\n",
175 SUPPORTED_AUDIO_FORMATS);
176 FOR_EACH_AUDIO_FORMAT(i) {
177 PARA_NOTICE_LOG("initializing %s handler\n",
179 afl[i].init(&afl[i]);
181 ms2tv(announce_time, &announce_tv);
182 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
183 for (i = 0; senders[i].name; i++) {
184 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
185 senders[i].init(&senders[i]);
189 if (conf.autoplay_given) {
190 struct timeval now, tmp;
191 mmd->vss_status_flags |= VSS_PLAYING;
192 mmd->new_vss_status_flags |= VSS_PLAYING;
193 gettimeofday(&now, NULL);
194 ms2tv(autoplay_delay, &tmp);
195 tv_add(&now, &tmp, &autoplay_barrier);
199 static int get_file_info(int i)
201 return afl[i].get_file_info(map, mmd->size, &mmd->afi);
205 * guess the audio format judging from filename
207 * \param name the filename
209 * \return This function returns -1 if it has no idea what kind of audio
210 * file this might be. Otherwise the (non-negative) number of the audio format
213 int guess_audio_format(const char *name)
215 int i,j, len = strlen(name);
217 FOR_EACH_AUDIO_FORMAT(i) {
218 for (j = 0; afl[i].suffixes[j]; j++) {
219 const char *p = afl[i].suffixes[j];
220 int plen = strlen(p);
223 if (name[len - plen - 1] != '.')
225 if (strcasecmp(name + len - plen, p))
227 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
234 static int get_audio_format(int omit)
238 FOR_EACH_AUDIO_FORMAT(i) {
241 if (get_file_info(i) > 0)
244 return -E_AUDIO_FORMAT;
248 * Call get_file_info() to obtain an afhi structure.
250 * \param path The full path of the audio file.
251 * \param data Pointer to the contents of the (mapped) file.
252 * \param size The file size in bytes.
253 * \param afhi Result pointer.
255 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
256 * compiled in audio format handler is able to handler the file.
258 * This function tries to find an audio format handler that can interpret the
259 * file given by \a data and \a size.
261 * It first tries to determine the audio format from the filename given by \a
262 * path. If this doesn't work, all other audio format handlers are tried until
263 * one is found that can handle the file.
265 int compute_afhi(const char *path, char *data, size_t size,
266 struct audio_format_info *afhi)
268 int ret, i, format = guess_audio_format(path);
271 ret = afl[format].get_file_info(data, size, afhi);
275 FOR_EACH_AUDIO_FORMAT(i) {
276 if (i == format) /* we already tried this one to no avail */
278 ret = afl[i].get_file_info(data, size, afhi);
281 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret));
283 return -E_AUDIO_FORMAT;
289 static int update_mmd(void)
293 i = guess_audio_format(mmd->filename);
294 if (i < 0 || get_file_info(i) < 0)
295 i = get_audio_format(i);
298 mmd->audio_format = i;
299 mmd->chunks_sent = 0;
300 mmd->current_chunk = 0;
306 static void vss_get_audio_file(void)
308 char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
310 struct stat file_status;
314 for (i = 0; sl[i]; i++) {
316 PARA_INFO_LOG("trying %s\n", sl[i]);
317 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
319 audio_file = open(sl[i], O_RDONLY);
322 if (fstat(audio_file, &file_status) == -1 ||
323 !file_status.st_size) {
327 mmd->size = file_status.st_size;
328 mmd->mtime = file_status.st_mtime;
329 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
331 strcpy(mmd->filename, sl[i]);
332 mmd->afi.header_len = 0; /* default: no header */
333 if (update_mmd() < 0) { /* invalid file */
335 munmap(map, mmd->size);
340 if (selectors[mmd->selector_num].update_audio_file)
341 selectors[mmd->selector_num].update_audio_file(sl[i]);
342 PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
343 mmd->new_vss_status_flags &= (~VSS_NEXT);
344 gettimeofday(&now, NULL);
345 tv_add(&now, &announce_tv, &data_send_barrier);
349 PARA_ERROR_LOG("%s", "no valid files found\n");
350 mmd->new_vss_status_flags = VSS_NEXT;
353 for (i = 0; sl[i]; i++)
359 static int chk_barrier(const char *bname, const struct timeval *now,
360 const struct timeval *barrier, struct timeval *diff,
365 if (tv_diff(now, barrier, diff) > 0)
369 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
373 static void vss_next_chunk_time(struct timeval *due)
377 tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
378 tv_add(&tmp, &mmd->stream_start, due);
382 * != NULL: timeout for next chunk
383 * NULL: nothing to do
385 static struct timeval *vss_compute_timeout(void)
387 static struct timeval the_timeout;
388 struct timeval now, next_chunk;
390 if (vss_next() && mmd->audio_format >= 0) {
391 /* only sleep a bit, nec*/
392 the_timeout.tv_sec = 0;
393 the_timeout.tv_usec = 100;
396 gettimeofday(&now, NULL);
397 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
398 &the_timeout, 1) < 0)
400 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
402 if (chk_barrier("data send", &now, &data_send_barrier,
403 &the_timeout, 1) < 0)
405 if (mmd->audio_format < 0 || !vss_playing() || !map)
407 vss_next_chunk_time(&next_chunk);
408 if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
409 &the_timeout, 0) < 0)
411 /* chunk is due or bof */
412 the_timeout.tv_sec = 0;
413 the_timeout.tv_usec = 0;
417 static void vss_eof(void)
424 for (i = 0; senders[i].name; i++)
425 senders[i].shutdown_clients();
428 gettimeofday(&now, NULL);
429 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
430 munmap(map, mmd->size);
433 mmd->audio_format = -1;
434 mmd->chunks_sent = 0;
436 mmd->afi.seconds_total = 0;
437 free(mmd->afi.chunk_table);
438 mmd->afi.chunk_table = NULL;
439 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
440 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
441 strcpy(mmd->afi.info_string, tmp);
443 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
444 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
445 strcpy(mmd->selector_info, tmp);
447 mmd->filename[0] = '\0';
453 * Get the header of the current audio file.
455 * \param header_len the length of the header is stored here
457 * \return a pointer to a buffer containing the header, or NULL, if no audio
458 * file is selected or if the current audio format does not need special header
462 char *vss_get_header(size_t *header_len)
464 if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
466 *header_len = mmd->afi.header_len;
467 return map + mmd->afi.header_offset;
471 * get the list of all supported audio formats
473 * \return a space separated list of all supported audio formats
474 * It is not allocated at runtime, i.e. there is no need to free
475 * the returned string in the caller.
477 const char *supported_audio_formats(void)
479 return SUPPORTED_AUDIO_FORMATS;
483 * get the chunk time of the current audio file
485 * \return a pointer to a struct containing the chunk time, or NULL,
486 * if currently no audio file is selected.
488 struct timeval *vss_chunk_time(void)
490 if (mmd->audio_format < 0)
492 return &mmd->afi.chunk_tv;
496 * compute the timeout for para_server's main select-loop
498 * This function gets called from para_server to determine the timeout value
499 * for its main select loop.
501 * Before the timeout is computed, the current vss status flags are evaluated
502 * and acted upon by calling appropriate functions from the lower layers.
503 * Possible actions include
505 * - request a new file list from the current audio file selector
506 * - shutdown of all senders (stop/pause command)
507 * - reposition the stream (ff/jmp command)
509 * \return A pointer to a struct timeval containing the timeout for the next
510 * chunk of data to be sent, or NULL if we're not sending right now.
512 struct timeval *vss_preselect(void)
514 struct audio_format_handler *af = NULL;
518 format = mmd->audio_format;
522 for (i = 0; senders[i].name; i++)
523 senders[i].shutdown_clients();
524 if (vss_next() && af) {
526 return vss_compute_timeout();
528 if (vss_paused() || vss_repos()) {
529 for (i = 0; senders[i].name; i++)
530 senders[i].shutdown_clients();
533 gettimeofday(&now, NULL);
534 if (!vss_paused() || mmd->chunks_sent)
535 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
537 tv_add(&now, &announce_tv, &data_send_barrier);
538 if (mmd->new_vss_status_flags & VSS_NOMORE)
539 mmd->new_vss_status_flags = VSS_NEXT;
541 mmd->chunks_sent = 0;
544 mmd->new_vss_status_flags &= ~(VSS_REPOS);
545 mmd->current_chunk = mmd->repos_request;
547 ret = vss_compute_timeout();
548 if (!ret && !map && vss_playing() &&
549 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
550 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
551 vss_get_audio_file();
557 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
559 size_t pos = mmd->afi.chunk_table[chunk_num];
561 *len = mmd->afi.chunk_table[chunk_num + 1] - pos;
565 * Get the data of the given chunk.
567 * \param chunk_num The number of the desired chunk.
568 * \param buf Chunk data.
569 * \param len Chunk length in bytes.
571 * \return Positive on success, negative on errors.
573 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
575 if (mmd->audio_format < 0 || !map || !vss_playing())
577 if (chunk_num >= mmd->afi.chunks_total)
579 get_chunk(chunk_num, buf, len);
584 * main sending function
586 * This function gets called from para_server as soon as the next chunk of
587 * data should be pushed out. It first calls the read_chunk() function of
588 * the current audio format handler to obtain a pointer to the data to be
589 * sent out as well as its length. This information is then passed to each
590 * supported sender's send() function which does the actual sending.
592 void vss_send_chunk(void)
595 struct audio_format_handler *af;
596 struct timeval now, due;
598 if (mmd->audio_format < 0 || !map || !vss_playing())
600 af = &afl[mmd->audio_format];
601 gettimeofday(&now, NULL);
602 vss_next_chunk_time(&due);
603 if (tv_diff(&due, &now, NULL) > 0)
605 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
607 if (chk_barrier("data send", &now, &data_send_barrier,
610 mmd->new_vss_status_flags &= ~VSS_REPOS;
611 if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
612 mmd->new_vss_status_flags |= VSS_NEXT;
616 * We call the send function also in case of empty chunks as they
617 * might have still some data queued which can be sent in this case.
619 if (!mmd->chunks_sent) {
621 gettimeofday(&mmd->stream_start, NULL);
622 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
623 mmd->offset = tv2ms(&tmp);
626 for (i = 0; senders[i].name; i++) {
629 get_chunk(mmd->current_chunk, &buf, &len);
630 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
632 mmd->new_vss_status_flags |= VSS_PLAYING;
634 mmd->current_chunk++;