2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file vss.c the virtual streaming system
21 * This contains the audio streaming code of para_server which is independent
22 * of the current audio format, audio file selector and of the activated
27 #include <sys/mman.h> /* mmap */
28 #include <sys/time.h> /* gettimeofday */
29 #include "server.cmdline.h"
37 extern const char *status_item_list[];
39 static struct timeval announce_tv;
40 static struct timeval data_send_barrier;
41 static struct timeval eof_barrier;
42 static struct timeval autoplay_barrier;
44 extern struct misc_meta_data *mmd;
45 extern struct audio_file_selector selectors[];
46 extern struct sender senders[];
48 static int audio_file;
52 void mp3_init(struct audio_format_handler *);
56 void ogg_init(struct audio_format_handler *);
59 void aac_afh_init(struct audio_format_handler *);
63 * the list of supported audio formats
65 static struct audio_format_handler afl[] = {
89 /** iterate over each supported audio format */
90 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
95 * check if vss status flag \a P (playing) is set
97 * \return greater than zero if playing, zero otherwise.
100 unsigned int vss_playing(void)
102 return mmd->new_vss_status_flags & VSS_PLAYING;
106 * check if \a N (next) status flag is set
108 * \return greater than zero if set, zero if not.
111 unsigned int vss_next(void)
113 return mmd->new_vss_status_flags & VSS_NEXT;
117 * check if a reposition request is pending
119 * \return greater than zero if true, zero otherwise.
122 unsigned int vss_repos(void)
124 return mmd->new_vss_status_flags & VSS_REPOS;
128 * check if the vss is currently paused
130 * \return greater than zero if paused, zero otherwise.
133 unsigned int vss_paused(void)
135 return !(mmd->new_vss_status_flags & VSS_NEXT)
136 && !(mmd->new_vss_status_flags & VSS_PLAYING);
140 * get the name of the given audio format
141 * \param i the audio format number
143 * This returns a pointer to statically allocated memory so it
144 * must not be freed by the caller.
146 const char *audio_format_name(int i)
148 return i >= 0? afl[i].name : "(none)";
152 * initialize the virtual streaming system
154 * Call the init functions of all supported audio format handlers and
155 * initialize all supported senders.
160 char *hn = para_hostname(), *home = para_homedir();
162 PARA_DEBUG_LOG("supported audio formats: %s\n",
163 SUPPORTED_AUDIO_FORMATS);
164 for (i = 0; afl[i].name; i++) {
165 PARA_NOTICE_LOG("initializing %s handler\n",
167 afl[i].init(&afl[i]);
169 ms2tv(conf.announce_time_arg, &announce_tv);
170 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
171 for (i = 0; senders[i].name; i++) {
172 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
173 senders[i].init(&senders[i]);
177 if (conf.autoplay_given) {
178 struct timeval now, tmp;
179 mmd->vss_status_flags |= VSS_PLAYING;
180 mmd->new_vss_status_flags |= VSS_PLAYING;
181 gettimeofday(&now, NULL);
182 ms2tv(conf.autoplay_delay_arg, &tmp);
183 tv_add(&now, &tmp, &autoplay_barrier);
187 static int get_file_info(int i)
189 return afl[i].get_file_info(map, mmd->size, &mmd->afi);
193 * guess the audio format judging from filename
195 * \param name the filename
197 * \return This function returns -1 if it has no idea what kind of audio
198 * file this might be. Otherwise the (non-negative) number of the audio format
201 int guess_audio_format(const char *name)
203 int i,j, len = strlen(name);
205 FOR_EACH_AUDIO_FORMAT(i) {
206 for (j = 0; afl[i].suffixes[j]; j++) {
207 const char *p = afl[i].suffixes[j];
208 int plen = strlen(p);
211 if (name[len - plen - 1] != '.')
213 if (strcasecmp(name + len - plen, p))
215 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
222 static int get_audio_format(int omit)
226 FOR_EACH_AUDIO_FORMAT(i) {
229 if (get_file_info(i) > 0)
232 return -E_AUDIO_FORMAT;
238 static int update_mmd(void)
242 i = guess_audio_format(mmd->filename);
243 if (i < 0 || get_file_info(i) < 0)
244 i = get_audio_format(i);
247 mmd->audio_format = i;
248 mmd->chunks_sent = 0;
249 mmd->current_chunk = 0;
255 static void vss_get_audio_file(void)
257 char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
259 struct stat file_status;
263 for (i = 0; sl[i]; i++) {
265 PARA_INFO_LOG("trying %s\n", sl[i]);
266 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
268 audio_file = open(sl[i], O_RDONLY);
271 if (fstat(audio_file, &file_status) == -1 ||
272 !file_status.st_size) {
276 mmd->size = file_status.st_size;
277 mmd->mtime = file_status.st_mtime;
278 map = para_mmap(file_status.st_size, PROT_READ, MAP_PRIVATE,
280 strcpy(mmd->filename, sl[i]);
281 if (update_mmd() < 0) { /* invalid file */
283 munmap(map, mmd->size);
288 if (selectors[mmd->selector_num].update_audio_file)
289 selectors[mmd->selector_num].update_audio_file(sl[i]);
290 PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
291 mmd->new_vss_status_flags &= (~VSS_NEXT);
292 gettimeofday(&now, NULL);
293 tv_add(&now, &announce_tv, &data_send_barrier);
297 PARA_ERROR_LOG("%s", "no valid files found\n");
298 mmd->new_vss_status_flags = VSS_NEXT;
301 for (i = 0; sl[i]; i++)
307 static int chk_barrier(const char *bname, const struct timeval *now,
308 const struct timeval *barrier, struct timeval *diff,
313 if (tv_diff(now, barrier, diff) > 0)
317 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
321 static void vss_next_chunk_time(struct timeval *due)
325 tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
326 tv_add(&tmp, &mmd->stream_start, due);
330 * != NULL: timeout for next chunk
331 * NULL: nothing to do
333 static struct timeval *vss_compute_timeout(void)
335 static struct timeval the_timeout;
336 struct timeval now, next_chunk;
338 if (vss_next() && mmd->audio_format >= 0) {
339 /* only sleep a bit, nec*/
340 the_timeout.tv_sec = 0;
341 the_timeout.tv_usec = 100;
344 gettimeofday(&now, NULL);
345 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
346 &the_timeout, 1) < 0)
348 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
350 if (chk_barrier("data send", &now, &data_send_barrier,
351 &the_timeout, 1) < 0)
353 if (mmd->audio_format < 0 || !vss_playing() || !map)
355 vss_next_chunk_time(&next_chunk);
356 if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
357 &the_timeout, 0) < 0)
359 /* chunk is due or bof */
360 the_timeout.tv_sec = 0;
361 the_timeout.tv_usec = 0;
365 static void vss_eof(void)
372 for (i = 0; senders[i].name; i++)
373 senders[i].shutdown_clients();
376 gettimeofday(&now, NULL);
377 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
378 munmap(map, mmd->size);
381 mmd->audio_format = -1;
382 mmd->chunks_sent = 0;
384 mmd->afi.seconds_total = 0;
385 free(mmd->afi.chunk_table);
386 mmd->afi.chunk_table = NULL;
387 free(mmd->afi.header);
388 mmd->afi.header = NULL;
389 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
390 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
391 strcpy(mmd->afi.info_string, tmp);
393 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
394 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
395 strcpy(mmd->selector_info, tmp);
397 mmd->filename[0] = '\0';
403 * get the header and of the current audio file
405 * \param header_len the length of the header is stored here
407 * \return a pointer to a buffer containing the header, or NULL, if no audio
408 * file is selected or if the current audio format does not need special header
412 char *vss_get_header(int *header_len)
414 if (mmd->audio_format < 0)
416 *header_len = mmd->afi.header_len;
417 return mmd->afi.header;
421 * get the list of all supported audio formats
423 * \return a space separated list of all supported audio formats
424 * It is not allocated at runtime, i.e. there is no need to free
425 * the returned string in the caller.
427 const char *supported_audio_formats(void)
429 return SUPPORTED_AUDIO_FORMATS;
433 * get the chunk time of the current audio file
435 * \return a pointer to a struct containing the chunk time, or NULL,
436 * if currently no audio file is selected.
438 struct timeval *vss_chunk_time(void)
440 if (mmd->audio_format < 0)
442 return &mmd->afi.chunk_tv;
446 * compute the timeout for para_server's main select-loop
448 * This function gets called from para_server to determine the timeout value
449 * for its main select loop.
451 * Before the timeout is computed, the current vss status flags are evaluated
452 * and acted upon by calling appropriate functions from the lower layers.
453 * Possible actions include
455 * - request a new file list from the current audio file selector
456 * - shutdown of all senders (stop/pause command)
457 * - reposition the stream (ff/jmp command)
459 * \return A pointer to a struct timeval containing the timeout for the next
460 * chunk of data to be sent, or NULL if we're not sending right now.
462 struct timeval *vss_preselect(void)
464 struct audio_format_handler *af = NULL;
468 format = mmd->audio_format;
472 for (i = 0; senders[i].name; i++)
473 senders[i].shutdown_clients();
474 if (vss_next() && af) {
476 return vss_compute_timeout();
478 if (vss_paused() || vss_repos()) {
479 for (i = 0; senders[i].name; i++)
480 senders[i].shutdown_clients();
483 gettimeofday(&now, NULL);
484 if (!vss_paused() || mmd->chunks_sent)
485 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
487 tv_add(&now, &announce_tv, &data_send_barrier);
488 if (mmd->new_vss_status_flags & VSS_NOMORE)
489 mmd->new_vss_status_flags = VSS_NEXT;
491 mmd->chunks_sent = 0;
494 mmd->new_vss_status_flags &= ~(VSS_REPOS);
495 mmd->current_chunk = mmd->repos_request;
497 ret = vss_compute_timeout();
498 if (!ret && !map && vss_playing() &&
499 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
500 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
501 vss_get_audio_file();
508 * main sending function
510 * This function gets called from para_server as soon as the next chunk of
511 * data should be pushed out. It first calls the read_chunk() function of
512 * the current audio format handler to obtain a pointer to the data to be
513 * sent out as well as its length. This information is then passed to each
514 * supported sender's send() function which does the actual sending.
516 void vss_send_chunk(void)
519 struct audio_format_handler *af;
521 struct timeval now, due;
523 if (mmd->audio_format < 0 || !map || !vss_playing())
525 af = &afl[mmd->audio_format];
526 gettimeofday(&now, NULL);
527 vss_next_chunk_time(&due);
528 if (tv_diff(&due, &now, NULL) > 0)
530 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
532 if (chk_barrier("data send", &now, &data_send_barrier,
535 mmd->new_vss_status_flags &= ~VSS_REPOS;
536 if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
537 mmd->new_vss_status_flags |= VSS_NEXT;
540 pos = mmd->afi.chunk_table[mmd->current_chunk];
541 len = mmd->afi.chunk_table[mmd->current_chunk + 1] - pos;
543 * We call the send function also in case of empty chunks as they
544 * might have still some data queued which can be sent in this case.
546 if (!mmd->chunks_sent) {
548 gettimeofday(&mmd->stream_start, NULL);
549 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
550 mmd->offset = tv2ms(&tmp);
553 for (i = 0; senders[i].name; i++)
554 senders[i].send(mmd->current_chunk, mmd->chunks_sent,
556 mmd->new_vss_status_flags |= VSS_PLAYING;
558 mmd->current_chunk++;