From: Andre Date: Sat, 6 Jan 2007 15:05:38 +0000 (+0100) Subject: rename afs to vss (the virtual streaming system) X-Git-Tag: v0.2.15~104 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=dc4030458f9af07d5719cfbff0a47cd16f1847f7;p=paraslash.git rename afs to vss (the virtual streaming system) --- diff --git a/aac_afh.c b/aac_afh.c index a3a24bc0..a2347065 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Andre Noll + * Copyright (C) 2006-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,7 +24,7 @@ #include "server.cmdline.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "afh.h" #include "error.h" #include "string.h" diff --git a/afs.c b/afs.c deleted file mode 100644 index 2d4184d2..00000000 --- a/afs.c +++ /dev/null @@ -1,546 +0,0 @@ -/* - * Copyright (C) 1997-2006 Andre Noll - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. - */ - -/** \file afs.c audio file sending functions - * - * This contains the audio sending part of para_server which is independent of - * the current audio format, audio file selector and of the activated senders. - */ - -#include "server.h" -#include /* gettimeofday */ -#include "server.cmdline.h" -#include "db.h" -#include "afh.h" -#include "afs.h" -#include "send.h" -#include "error.h" -#include "string.h" - -extern const char *status_item_list[]; - -static struct timeval announce_tv; -static struct timeval data_send_barrier; -static struct timeval eof_barrier; -static struct timeval autoplay_barrier; - -extern struct misc_meta_data *mmd; -extern struct audio_file_selector selectors[]; -extern struct sender senders[]; - -static FILE *audio_file = NULL; - -#if 1 - void mp3_init(struct audio_format_handler *); -#endif - -#ifdef HAVE_OGGVORBIS - void ogg_init(struct audio_format_handler *); -#endif -#ifdef HAVE_FAAD - void aac_afh_init(struct audio_format_handler *); -#endif - -/** - * the list of supported audio formats - */ -static struct audio_format_handler afl[] = { -#if 1 - { - .name = "mp3", - .init = mp3_init, - }, -#endif -#ifdef HAVE_OGGVORBIS - { - .name = "ogg", - .init = ogg_init, - }, -#endif -#ifdef HAVE_FAAD - { - .name = "aac", - .init = aac_afh_init, - }, -#endif - { - .name = NULL, - } -}; -#define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++) - -/** - * check if audio file sender is playing - * - * \return greater than zero if playing, zero otherwise. - * - */ -unsigned int afs_playing(void) -{ - return mmd->new_afs_status_flags & AFS_PLAYING; -} - -/** - * check if 'next' flag is set afs_status_flags - * - * \return greater than zero if set, zero if not. - * - */ -unsigned int afs_next(void) -{ - return mmd->new_afs_status_flags & AFS_NEXT; -} - -/** - * check if a reposition request is pending - * - * \return greater than zero if true, zero otherwise. - * - */ -unsigned int afs_repos(void) -{ - return mmd->new_afs_status_flags & AFS_REPOS; -} - -/** - * check if audio file sender is paused - * - * \return greater than zero if paused, zero otherwise. - * - */ -unsigned int afs_paused(void) -{ - return !(mmd->new_afs_status_flags & AFS_NEXT) - && !(mmd->new_afs_status_flags & AFS_PLAYING); -} - -/** - * get the name of the given audio format - * \param i the audio format number - * - * This returns a pointer to statically allocated memory so it - * must not be freed by the caller. - */ -const char *audio_format_name(int i) -{ - return i >= 0? afl[i].name : "(none)"; -} - -/** - * initialize the audio file sender - * - * Call the init functions of all supported audio format handlers and - * initialize all supported senders. - */ -void afs_init(void) -{ - int i; - char *hn = para_hostname(), *home = para_homedir(); - - PARA_DEBUG_LOG("supported audio formats: %s\n", - SUPPORTED_AUDIO_FORMATS); - for (i = 0; afl[i].name; i++) { - PARA_NOTICE_LOG("initializing %s handler\n", - afl[i].name); - afl[i].init(&afl[i]); - } - ms2tv(conf.announce_time_arg, &announce_tv); - PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv)); - for (i = 0; senders[i].name; i++) { - PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name); - senders[i].init(&senders[i]); - } - free(hn); - free(home); - if (conf.autoplay_given) { - struct timeval now, tmp; - mmd->afs_status_flags |= AFS_PLAYING; - mmd->new_afs_status_flags |= AFS_PLAYING; - gettimeofday(&now, NULL); - ms2tv(conf.autoplay_delay_arg, &tmp); - tv_add(&now, &tmp, &autoplay_barrier); - } -} - -static int get_file_info(int i) -{ - return afl[i].get_file_info(audio_file, mmd->audio_file_info, - &mmd->chunks_total, &mmd->seconds_total); -} - -/** - * guess the audio format judging from filename - * - * \param name the filename - * - * \return This function returns -1 if it has no idea what kind of audio - * file this might be. Otherwise the (non-negative) number of the audio format - * is returned. - */ -int guess_audio_format(const char *name) -{ - int i,j, len = strlen(name); - - FOR_EACH_AUDIO_FORMAT(i) { - for (j = 0; afl[i].suffixes[j]; j++) { - const char *p = afl[i].suffixes[j]; - int plen = strlen(p); - if (len < plen + 1) - continue; - if (name[len - plen - 1] != '.') - continue; - if (strcasecmp(name + len - plen, p)) - continue; -// PARA_DEBUG_LOG("might be %s\n", audio_format_name(i)); - return i; - } - } - return -1; -} - -static int get_audio_format(int omit) -{ - int i; - - FOR_EACH_AUDIO_FORMAT(i) { - if (i == omit || !afl[i].get_file_info) - continue; - rewind(audio_file); - if (get_file_info(i) > 0) - return i; - rewind(audio_file); - } - return -E_AUDIO_FORMAT; -} - -/* - * upddate shared mem - */ -static int update_mmd(void) -{ - int i; - struct stat file_status; - - i = guess_audio_format(mmd->filename); - if (i < 0 || get_file_info(i) < 0) - i = get_audio_format(i); - if (i < 0) - return i; - mmd->audio_format = i; - mmd->chunks_sent = 0; - mmd->current_chunk = 0; - mmd->offset = 0; - if (fstat(fileno(audio_file), &file_status) == -1) - return -E_FSTAT; - mmd->size = file_status.st_size; - mmd->mtime = file_status.st_mtime; - mmd->events++; - PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename); - return 1; -} - -static void get_song(void) -{ - char **sl = selectors[mmd->selector_num].get_audio_file_list(10); - int i; - - if (!sl) - goto err_out; - for (i = 0; sl[i]; i++) { - struct timeval now; - PARA_INFO_LOG("trying %s\n", sl[i]); - if (strlen(sl[i]) >= _POSIX_PATH_MAX) - continue; - audio_file = fopen(sl[i], "r"); - if (!audio_file) - continue; - strcpy(mmd->filename, sl[i]); - if (update_mmd() < 0) { - fclose(audio_file); - audio_file = NULL; - continue; - } - mmd->num_played++; - if (selectors[mmd->selector_num].update_audio_file) - selectors[mmd->selector_num].update_audio_file(sl[i]); - PARA_DEBUG_LOG("%s", "success\n"); - mmd->new_afs_status_flags &= (~AFS_NEXT); - gettimeofday(&now, NULL); - tv_add(&now, &announce_tv, &data_send_barrier); - - goto free; - } - PARA_ERROR_LOG("%s", "no valid files found\n"); -err_out: - mmd->new_afs_status_flags = AFS_NEXT; -free: - if (sl) { - for (i = 0; sl[i]; i++) - free(sl[i]); - free(sl); - } -} - -static int chk_barrier(const char *bname, const struct timeval *now, - const struct timeval *barrier, struct timeval *diff, - int print_log) -{ - long ms; - - if (tv_diff(now, barrier, diff) > 0) - return 1; - ms = tv2ms(diff); - if (print_log && ms) - PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms); - return -1; -} - -static void afs_next_chunk_time(struct timeval *due) -{ - struct timeval tmp; - - tv_scale(mmd->chunks_sent, &afl[mmd->audio_format].chunk_tv, &tmp); - tv_add(&tmp, &mmd->stream_start, due); -} - -/* - * != NULL: timeout for next chunk - * NULL: nothing to do - */ -static struct timeval *afs_compute_timeout(void) -{ - static struct timeval the_timeout; - struct timeval now, next_chunk; - - if (afs_next() && mmd->audio_format >= 0) { - /* only sleep a bit, nec*/ - the_timeout.tv_sec = 0; - the_timeout.tv_usec = 100; - return &the_timeout; - } - gettimeofday(&now, NULL); - if (chk_barrier("autoplay_delay", &now, &autoplay_barrier, - &the_timeout, 1) < 0) - return &the_timeout; - if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0) - return &the_timeout; - if (chk_barrier("data send", &now, &data_send_barrier, - &the_timeout, 1) < 0) - return &the_timeout; - if (mmd->audio_format < 0 || !afs_playing() || !audio_file) - return NULL; - afs_next_chunk_time(&next_chunk); - if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk, - &the_timeout, 0) < 0) - return &the_timeout; - /* chunk is due or bof */ - the_timeout.tv_sec = 0; - the_timeout.tv_usec = 0; - return &the_timeout; -} - -static void afs_eof(struct audio_format_handler *af) -{ - struct timeval now; - int i; - char *tmp; - - if (!af || !audio_file) { - for (i = 0; senders[i].name; i++) - senders[i].shutdown_clients(); - return; - } - gettimeofday(&now, NULL); - tv_add(&af->eof_tv, &now, &eof_barrier); - af->close_audio_file(); - audio_file = NULL; - mmd->audio_format = -1; - af = NULL; - mmd->chunks_sent = 0; - mmd->offset = 0; - mmd->seconds_total = 0; - tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1], - status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]); - strcpy(mmd->audio_file_info, tmp); - free(tmp); - tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1], - status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]); - strcpy(mmd->selector_info, tmp); - free(tmp); - mmd->filename[0] = '\0'; - mmd->size = 0; - mmd->events++; -} - -/** - * get the header and of the current audio file - * - * \param header_len the length of the header is stored here - * - * \return a pointer to a buffer containing the header, or NULL, if no audio - * file is selected or if the current audio format does not need special header - * treamtment. - * - */ -char *afs_get_header(int *header_len) -{ - *header_len = 0; - if (mmd->audio_format < 0) - return NULL; - if (!afl[mmd->audio_format].get_header_info) - return NULL; - return afl[mmd->audio_format].get_header_info(header_len); -} -const char *supported_audio_formats(void) -{ - return SUPPORTED_AUDIO_FORMATS; -} - -/** - * get the chunk time of the current audio file - * - * \return a pointer to a struct containing the chunk time, or NULL, - * if currently no audio file is selected. - */ -struct timeval *afs_chunk_time(void) -{ - if (mmd->audio_format < 0) - return NULL; - return &afl[mmd->audio_format].chunk_tv; -} - -/** - * compute the timeout for para_server's main select-loop - * - * This function gets called from para_server to determine the timeout value - * for its main select loop. - * - * Before the timeout is computed, the current afs status flags are evaluated - * and acted upon by calling appropriate functions from the lower layers. - * Possible actions include - * - * - request a new file list from the current audio file selector - * - shutdown of all senders (stop/pause command) - * - reposition the stream (ff/jmp command) - * - * \return A pointer to a struct timeval containing the timeout for the next - * chunk of data to be sent, or NULL if we're not sending right now. - */ -struct timeval *afs_preselect(void) -{ - struct audio_format_handler *af = NULL; - int i, format; - struct timeval *ret; -again: - format = mmd->audio_format; - if (format >= 0) - af = afl + format; - else - for (i = 0; senders[i].name; i++) - senders[i].shutdown_clients(); - if (afs_next() && af) { - afs_eof(af); - return afs_compute_timeout(); - } - if (afs_paused() || afs_repos()) { - for (i = 0; senders[i].name; i++) - senders[i].shutdown_clients(); - if (af) { - struct timeval now; - gettimeofday(&now, NULL); - if (!afs_paused() || mmd->chunks_sent) - tv_add(&af->eof_tv, &now, &eof_barrier); - if (afs_repos()) - tv_add(&now, &announce_tv, &data_send_barrier); - if (mmd->new_afs_status_flags & AFS_NOMORE) - mmd->new_afs_status_flags = AFS_NEXT; - } - mmd->chunks_sent = 0; - } - if (af && afs_repos() && mmd->current_chunk != mmd->repos_request) - af->reposition_stream(mmd->repos_request); - if (afs_repos()) { - mmd->new_afs_status_flags &= ~(AFS_REPOS); - mmd->current_chunk = mmd->repos_request; - } - ret = afs_compute_timeout(); - if (!ret && !audio_file && afs_playing() && - !(mmd->new_afs_status_flags & AFS_NOMORE)) { - PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n"); - get_song(); - goto again; - } - return ret; -} - -/** - * afs_send_chunk - paraslash's main sending function - * - * This function gets called from para_server as soon as the next chunk of - * data should be pushed out. It first calls the read_chunk() function of - * the current audio format handler to obtain a pointer to the data to be - * sent out as well as its length. This information is then passed to each - * supported sender's send() function which does the actual sending. - * - * Return value: Positive return value on success, zero on eof and negative - * on errors. - */ - -void afs_send_chunk(void) -{ - int i; - struct audio_format_handler *af; - char *buf; - ssize_t ret; - struct timeval now, due; - - if (mmd->audio_format < 0 || !audio_file || !afs_playing()) - return; - af = &afl[mmd->audio_format]; - gettimeofday(&now, NULL); - afs_next_chunk_time(&due); - if (tv_diff(&due, &now, NULL) > 0) - return; - if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0) - return; - if (chk_barrier("data send", &now, &data_send_barrier, - &due, 1) < 0) - return; - buf = af->read_chunk(mmd->current_chunk, &ret); - mmd->new_afs_status_flags &= ~AFS_REPOS; - if (!buf) { - if (ret < 0) - mmd->new_afs_status_flags = AFS_NEXT; - else - mmd->new_afs_status_flags |= AFS_NEXT; - afs_eof(af); - return; - } - if (!mmd->chunks_sent) { - struct timeval tmp; - gettimeofday(&mmd->stream_start, NULL); - tv_scale(mmd->current_chunk, &af->chunk_tv, &tmp); - mmd->offset = tv2ms(&tmp); - mmd->events++; - } - for (i = 0; senders[i].name; i++) - senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, ret); - mmd->new_afs_status_flags |= AFS_PLAYING; - mmd->chunks_sent++; - mmd->current_chunk++; -} diff --git a/afs.h b/afs.h deleted file mode 100644 index 3f4d1d7c..00000000 --- a/afs.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2005-2006 Andre Noll - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. - */ - -/** \file afs.h exported functions from afs.c (para_server) */ -void afs_init(void); -void afs_send_chunk(void); -struct timeval *afs_preselect(void); -const char *audio_format_name(int); -unsigned int afs_playing(void); -unsigned int afs_next(void); -unsigned int afs_repos(void); -unsigned int afs_paused(void); -char *afs_get_header(int *header_len); -struct timeval *afs_chunk_time(void); -int guess_audio_format(const char *name); -const char *supported_audio_formats(void); -/* status flags */ -#define AFS_NOMORE 1 -#define AFS_NEXT 2 -#define AFS_REPOS 4 -#define AFS_PLAYING 8 -#define DBT_CHANGE 16 diff --git a/command.c b/command.c index 66c658ac..ebc0a5fe 100644 --- a/command.c +++ b/command.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2006 Andre Noll + * Copyright (C) 1997-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #include "server.cmdline.h" #include "db.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "send.h" #include "rc4.h" #include @@ -79,13 +79,13 @@ static struct server_command cmd_struct[] = { { .name = "ff", .handler = com_ff, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "jmp amount of time forwards or backwards " "in current audio file", .synopsis = "ff n[-]", .help = -"\tSet the 'R' (reposition request) bit of the afs status flags\n" +"\tSet the 'R' (reposition request) bit of the vss status flags\n" "\tand enqueue a request to jump n seconds forwards or backwards\n" "\tin the current audio file.\n" "\n" @@ -114,7 +114,7 @@ static struct server_command cmd_struct[] = { { .name = "hup", .handler = com_hup, -.perms = AFS_WRITE, +.perms = VSS_WRITE, .description = "force reload of config file and log file", .synopsis = "hup", .help = @@ -127,12 +127,12 @@ static struct server_command cmd_struct[] = { { .name = "jmp", .handler = com_jmp, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "jmp to given position in current audio file", .synopsis = "jmp [n]", .help = -"\tSet the 'R' (reposition request) bit of the afs status flags\n" +"\tSet the 'R' (reposition request) bit of the vss status flags\n" "\tand enqueue a request to jump to n% of the current audio file,\n" "\twhere 0 <= n <= 100.\n" @@ -141,12 +141,12 @@ static struct server_command cmd_struct[] = { { .name = "next", .handler = com_next, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "skip rest of current audio file", .synopsis = "next", .help = -"\tSet the 'N' (next audio file) bit of the afs status flags. When\n" +"\tSet the 'N' (next audio file) bit of the vss status flags. When\n" "\tplaying, change audio file immediately. Equivalent to stop\n" "\tif paused, NOP if stopped.\n" @@ -156,12 +156,12 @@ static struct server_command cmd_struct[] = { { .name = "nomore", .handler = com_nomore, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "stop playing after current audio file", .synopsis = "nomore", .help = -"Set the 'O' (no more) bit of the afs status flags. This instructs\n" +"Set the 'O' (no more) bit of the vss status flags. This instructs\n" "para_server to clear the 'P' (playing) bit as soon as it encounters\n" "the 'N' (next audio file) bit being set.\n" "\n" @@ -173,24 +173,24 @@ static struct server_command cmd_struct[] = { { .name ="pause", .handler = com_pause, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "pause current audio file", .synopsis = "pause", .help = -"\tClear the 'P' (playing) bit of the afs status flags.\n" +"\tClear the 'P' (playing) bit of the vss status flags.\n" }, { .name = "play", .handler = com_play, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "start playing or resume playing when paused", .synopsis = "play", .help = -"\tSet the 'P' (playing) bit of the afs status flags. This\n" +"\tSet the 'P' (playing) bit of the vss status flags. This\n" "\tresults in starting/continuing to stream.\n" }, @@ -198,7 +198,7 @@ static struct server_command cmd_struct[] = { { .name = "sb", .handler = com_sb, -.perms = AFS_READ, +.perms = VSS_READ, .description = "print status bar for current audio file", .synopsis = "sb [n]", .help = @@ -215,7 +215,7 @@ static struct server_command cmd_struct[] = { { .name = "sc", .handler = com_sc, -.perms = AFS_READ, +.perms = VSS_READ, .description = "print name of audio file whenever it changes", .synopsis = "sc [n]", .help = @@ -228,7 +228,7 @@ static struct server_command cmd_struct[] = { { .name = "sender", .handler = com_sender, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "control paraslash internal senders", .synopsis = "sender [s cmd [arguments]]", .help = @@ -253,7 +253,7 @@ static struct server_command cmd_struct[] = { { .name = "stat", .handler = com_stat, -.perms = AFS_READ, +.perms = VSS_READ, .description = "print status info for current audio file", .synopsis = "stat [n]", .help = @@ -267,19 +267,19 @@ static struct server_command cmd_struct[] = { { .name = "stop", .handler = com_stop, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "stop playing", .synopsis = "stop", .help = -"\tClear the 'P' (play) bit and set the 'N' bit of the afs status\n" +"\tClear the 'P' (play) bit and set the 'N' bit of the vss status\n" "\tflags.\n" }, { .name = "term", .handler = com_term, -.perms = AFS_READ | AFS_WRITE, +.perms = VSS_READ | VSS_WRITE, .description = "terminate para_server", .synopsis = "term", .help = @@ -315,13 +315,13 @@ static void mmd_dup(struct misc_meta_data *new_mmd) /* * compute human readable string containing - * afs_status for given integer value + * vss status for given integer value */ -static char *afs_status_tohuman(unsigned int flags) +static char *vss_status_tohuman(unsigned int flags) { - if (flags & AFS_PLAYING) + if (flags & VSS_PLAYING) return para_strdup("playing"); - else if (flags & AFS_NEXT) + else if (flags & VSS_NEXT) return para_strdup("stopped"); else return para_strdup("paused"); @@ -336,8 +336,8 @@ char *cmd_perms_itohuman(unsigned int perms) msg[0] = perms & DB_READ? 'd' : '-'; msg[1] = perms & DB_WRITE? 'D' : '-'; - msg[2] = perms & AFS_READ? 'a' : '-'; - msg[3] = perms & AFS_WRITE? 'A' : '-'; + msg[2] = perms & VSS_READ? 'a' : '-'; + msg[3] = perms & VSS_WRITE? 'A' : '-'; msg[4] = '\0'; return msg; } @@ -345,14 +345,14 @@ char *cmd_perms_itohuman(unsigned int perms) /* * Never returns NULL. */ -static char *afs_get_status_flags(unsigned int flags) +static char *vss_get_status_flags(unsigned int flags) { char *msg = para_malloc(5 * sizeof(char)); - msg[0] = (flags & AFS_PLAYING)? 'P' : '_'; - msg[1] = (flags & AFS_NOMORE)? 'O' : '_'; - msg[2] = (flags & AFS_NEXT)? 'N' : '_'; - msg[3] = (flags & AFS_REPOS)? 'R' : '_'; + msg[0] = (flags & VSS_PLAYING)? 'P' : '_'; + msg[1] = (flags & VSS_NOMORE)? 'O' : '_'; + msg[2] = (flags & VSS_NEXT)? 'N' : '_'; + msg[3] = (flags & VSS_REPOS)? 'R' : '_'; msg[4] = '\0'; return msg; } @@ -392,7 +392,7 @@ char *get_sb_string(struct misc_meta_data *nmmd) static char *get_status(struct misc_meta_data *nmmd) { char *bar, *ret, mtime[30] = ""; - char *status, *flags; /* afs status info */ + char *status, *flags; /* vss status info */ char *ut = uptime_str(); long offset = (nmmd->offset + 500) / 1000; struct timeval now; @@ -403,8 +403,8 @@ static char *get_status(struct misc_meta_data *nmmd) strftime(mtime, 29, "%a %b %d %Y", &mtime_tm); } /* report real status */ - status = afs_status_tohuman(nmmd->afs_status_flags); - flags = afs_get_status_flags(nmmd->afs_status_flags); + status = vss_status_tohuman(nmmd->vss_status_flags); + flags = vss_get_status_flags(nmmd->vss_status_flags); bar = para_basename(nmmd->filename); gettimeofday(&now, NULL); ret = make_message( @@ -811,8 +811,8 @@ static int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv) if (argc != 1) return -E_COMMAND_SYNTAX; mmd_lock(); - mmd->new_afs_status_flags |= AFS_PLAYING; - mmd->new_afs_status_flags &= ~AFS_NOMORE; + mmd->new_vss_status_flags |= VSS_PLAYING; + mmd->new_vss_status_flags &= ~VSS_NOMORE; mmd_unlock(); return 1; @@ -824,9 +824,9 @@ static int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv) if (argc != 1) return -E_COMMAND_SYNTAX; mmd_lock(); - mmd->new_afs_status_flags &= ~AFS_PLAYING; - mmd->new_afs_status_flags &= ~AFS_REPOS; - mmd->new_afs_status_flags |= AFS_NEXT; + mmd->new_vss_status_flags &= ~VSS_PLAYING; + mmd->new_vss_status_flags &= ~VSS_REPOS; + mmd->new_vss_status_flags |= VSS_NEXT; mmd_unlock(); return 1; } @@ -837,10 +837,10 @@ static int com_pause(__a_unused int socket_fd, int argc, __a_unused char **argv) if (argc != 1) return -E_COMMAND_SYNTAX; mmd_lock(); - if (!afs_paused()) + if (!vss_paused()) mmd->events++; - mmd->new_afs_status_flags &= ~AFS_PLAYING; - mmd->new_afs_status_flags &= ~AFS_NEXT; + mmd->new_vss_status_flags &= ~VSS_PLAYING; + mmd->new_vss_status_flags &= ~VSS_NEXT; mmd_unlock(); return 1; } @@ -877,7 +877,7 @@ static int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv) return -E_COMMAND_SYNTAX; mmd_lock(); mmd->events++; - mmd->new_afs_status_flags |= AFS_NEXT; + mmd->new_vss_status_flags |= VSS_NEXT; mmd_unlock(); return 1; } @@ -888,8 +888,8 @@ static int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv if (argc != 1) return -E_COMMAND_SYNTAX; mmd_lock(); - if (afs_playing() || afs_paused()) - mmd->new_afs_status_flags |= AFS_NOMORE; + if (vss_playing() || vss_paused()) + mmd->new_vss_status_flags |= VSS_NOMORE; mmd_unlock(); return 1; } @@ -920,12 +920,12 @@ static int com_ff(__a_unused int socket_fd, int argc, char **argv) if (promille < 0) promille = 0; if (promille > 1000) { - mmd->new_afs_status_flags |= AFS_NEXT; + mmd->new_vss_status_flags |= VSS_NEXT; goto out; } mmd->repos_request = (mmd->chunks_total * promille) / 1000; - mmd->new_afs_status_flags |= AFS_REPOS; - mmd->new_afs_status_flags &= ~AFS_NEXT; + mmd->new_vss_status_flags |= VSS_REPOS; + mmd->new_vss_status_flags &= ~VSS_NEXT; mmd->events++; ret = 1; out: @@ -953,8 +953,8 @@ static int com_jmp(__a_unused int socket_fd, int argc, char **argv) mmd->repos_request = (mmd->chunks_total * i + 50)/ 100; PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n", mmd->chunks_sent, mmd->offset); - mmd->new_afs_status_flags |= AFS_REPOS; - mmd->new_afs_status_flags &= ~AFS_NEXT; + mmd->new_vss_status_flags |= VSS_REPOS; + mmd->new_vss_status_flags &= ~VSS_NEXT; ret = 1; mmd->events++; out: diff --git a/configure.ac b/configure.ac index faa5d4ad..49770573 100644 --- a/configure.ac +++ b/configure.ac @@ -81,7 +81,7 @@ audiod_ldflags="" audiod_audio_formats="" server_cmdline_objs="server.cmdline" -server_errlist_objs="server mp3_afh afs command net string signal random_selector +server_errlist_objs="server mp3_afh vss command net string signal random_selector time daemon stat crypt http_send db close_on_fork playlist_selector ipc dccp dccp_send fd user_list" server_ldflags="" diff --git a/db.c b/db.c index 8b066726..2e4ffbda 100644 --- a/db.c +++ b/db.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2006 Andre Noll + * Copyright (C) 2005-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,7 +21,7 @@ #include "server.cmdline.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include /* readdir() */ #include /* stat */ #include /* mode_t */ diff --git a/dccp_send.c b/dccp_send.c index 273961fc..128d45b7 100644 --- a/dccp_send.c +++ b/dccp_send.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Andre Noll + * Copyright (C) 2006-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,7 +26,7 @@ #include "server.h" #include "net.h" #include "list.h" -#include "afs.h" +#include "vss.h" #include "send.h" #include "dccp.h" #include "error.h" @@ -165,7 +165,7 @@ static void dccp_send(long unsigned current_chunk, if (!ret) continue; if (!dc->header_sent && current_chunk) { - header_buf = afs_get_header(&header_len); + header_buf = vss_get_header(&header_len); if (header_buf && header_len > 0) { ret = dccp_write(dc->fd, header_buf, header_len); if (ret != header_len) { diff --git a/error.h b/error.h index b94139e3..9563303f 100644 --- a/error.h +++ b/error.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Andre Noll + * Copyright (C) 2006-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -63,7 +63,7 @@ enum para_subsystem { SS_AACDEC, SS_AAC_COMMON, SS_SERVER, - SS_AFS, + SS_VSS, SS_MYSQL_SELECTOR, SS_IPC, SS_DCCP, @@ -300,7 +300,7 @@ extern const char **para_errlist[]; PARA_ERROR(OGG_REPOS, "ogg repositioning error"), \ -#define AFS_ERRORS \ +#define VSS_ERRORS \ PARA_ERROR(AUDIO_FORMAT, "audio format not recognized"), \ PARA_ERROR(FSTAT, "failed to fstat() audio file"), \ @@ -552,7 +552,7 @@ SS_ENUM(OGG_AFH); SS_ENUM(AAC_AFH); SS_ENUM(AAC_COMMON); SS_ENUM(SERVER); -SS_ENUM(AFS); +SS_ENUM(VSS); SS_ENUM(COMMAND); SS_ENUM(RANDOM_SELECTOR); SS_ENUM(PLAYLIST_SELECTOR); diff --git a/http_send.c b/http_send.c index f06a5ac2..6c912951 100644 --- a/http_send.c +++ b/http_send.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2006 Andre Noll + * Copyright (C) 2005-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #include "server.cmdline.h" #include "server.h" #include "http.h" -#include "afs.h" +#include "vss.h" #include "send.h" #include "list.h" #include "close_on_fork.h" @@ -128,7 +128,7 @@ static void http_shutdown_clients(void) { struct http_client *hc, *tmp; list_for_each_entry_safe(hc, tmp, &clients, node) - http_shutdown_client(hc, "afs request"); + http_shutdown_client(hc, "vss request"); } static int http_send_msg(struct http_client *hc, const char *msg) @@ -208,7 +208,7 @@ static void http_send( long unsigned current_chunk, continue; if (hc->status == HTTP_READY_TO_STREAM) { int hlen; - char *hbuf = afs_get_header(&hlen); + char *hbuf = vss_get_header(&hlen); if (hbuf && hlen > 0 && current_chunk) { /* need to send header */ PARA_INFO_LOG("queueing header: %d\n", hlen); @@ -360,7 +360,7 @@ static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds) hc->check_w = 1; break; case HTTP_SENT_OK_MSG: - if (!afs_playing()) + if (!vss_playing()) break; /* wait until server starts playing */ para_fd_set(hc->fd, wfds, max_fileno); hc->check_w = 1; diff --git a/mp3_afh.c b/mp3_afh.c index d38e4359..530f36ff 100644 --- a/mp3_afh.c +++ b/mp3_afh.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2006 Andre Noll + * Copyright (C) 2003-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,7 +30,7 @@ #include "server.cmdline.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "afh.h" #include "error.h" #include "fd.h" diff --git a/mysql_selector.c b/mysql_selector.c index 7a3b0a7d..a2a5bcdd 100644 --- a/mysql_selector.c +++ b/mysql_selector.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1999-2006 Andre Noll + * Copyright (C) 1999-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,7 +24,7 @@ /** \endcond */ #include "server.cmdline.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "db.h" #include #include @@ -113,7 +113,7 @@ static struct server_command cmds[] = { { .name = "cs", .handler = com_cs, -.perms = AFS_WRITE | DB_READ | DB_WRITE, +.perms = VSS_WRITE | DB_READ | DB_WRITE, .description = "change stream", .synopsis = "cs [s]", .help = @@ -124,7 +124,7 @@ static struct server_command cmds[] = { { .name = "csp", .handler = com_cs, -.perms = AFS_WRITE | DB_READ, +.perms = VSS_WRITE | DB_READ, .description = "change stream and play", .synopsis = "csp s", .help = @@ -289,7 +289,7 @@ static struct server_command cmds[] = { { .name = "ns", .handler = com_ps, -.perms = AFS_WRITE | DB_READ | DB_WRITE, +.perms = VSS_WRITE | DB_READ | DB_WRITE, .description = "change to next stream", .synopsis = "ns", .help = @@ -377,7 +377,7 @@ static struct server_command cmds[] = { { .name = "ps", .handler = com_ps, -.perms = AFS_WRITE | DB_READ | DB_WRITE, +.perms = VSS_WRITE | DB_READ | DB_WRITE, .description = "change to previous stream", .synopsis = "ps", .help = @@ -2074,9 +2074,9 @@ static int com_cs(int fd, int argc, char *argv[]) } if (csp) { mmd_lock(); - mmd->new_afs_status_flags |= AFS_PLAYING; + mmd->new_vss_status_flags |= VSS_PLAYING; if (stream_change) - mmd->new_afs_status_flags |= AFS_NEXT; + mmd->new_vss_status_flags |= VSS_NEXT; mmd_unlock(); } ret = 1; diff --git a/ogg_afh.c b/ogg_afh.c index 5aaf53f7..14163fe6 100644 --- a/ogg_afh.c +++ b/ogg_afh.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2006 Andre Noll + * Copyright (C) 2004-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,7 +24,7 @@ #include "server.cmdline.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "afh.h" #include "error.h" #include "string.h" diff --git a/ortp_send.c b/ortp_send.c index 4dd19d7d..b06a1b84 100644 --- a/ortp_send.c +++ b/ortp_send.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2006 Andre Noll + * Copyright (C) 2005-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #include "server.cmdline.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "send.h" #include "list.h" #include "ortp.h" @@ -128,7 +128,7 @@ static void ortp_init_session(struct ortp_target *ot) set_multicast(s); } -/* called by afs */ +/* called by vss */ static void ortp_shutdown_targets(void) { unsigned char buf[ORTP_AUDIO_HEADER_LEN]; @@ -175,7 +175,7 @@ static void ortp_send(long unsigned current_chunk, long unsigned chunks_sent, if (self->status != SENDER_ON) return; - chunk_tv = afs_chunk_time(); + chunk_tv = vss_chunk_time(); if (!chunk_tv) return; list_for_each_entry_safe(ot, tmp, &targets, node) { @@ -193,7 +193,7 @@ static void ortp_send(long unsigned current_chunk, long unsigned chunks_sent, } if (list_empty(&targets)) return; - header_buf = afs_get_header(&header_len); + header_buf = vss_get_header(&header_len); if (!need_extra_header(current_chunk)) header_len = 0; sendbuf_len = ORTP_AUDIO_HEADER_LEN + header_len + len; diff --git a/server.c b/server.c index 6195266b..b67c7e29 100644 --- a/server.c +++ b/server.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2006 Andre Noll + * Copyright (C) 1997-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,7 +32,7 @@ #include "server.cmdline.h" #include "db.h" #include "server.h" -#include "afs.h" +#include "vss.h" #include "config.h" #include "close_on_fork.h" #include "send.h" @@ -51,7 +51,7 @@ INIT_SERVER_ERRLISTS; /** shut down non-authorized connections after that many seconds */ #define ALARM_TIMEOUT 10 -/* these are exported to afs.c. command.c and to all selectors */ +/* these are exported to vss.c. command.c and to all selectors */ struct misc_meta_data *mmd; /** the configuration of para_server * @@ -182,8 +182,8 @@ static void shm_init(void) mmd->active_connections = 0; strcpy(mmd->filename, "(none)"); mmd->audio_format = -1; - mmd->afs_status_flags = AFS_NEXT; - mmd->new_afs_status_flags = AFS_NEXT; + mmd->vss_status_flags = VSS_NEXT; + mmd->new_vss_status_flags = VSS_NEXT; mmd->sender_cmd_data.cmd_num = -1; return; err_out: @@ -362,7 +362,7 @@ static unsigned do_inits(int argc, char **argv) init_selector(); PARA_NOTICE_LOG("%s", "initializing audio file sender\n"); /* audio file sender */ - afs_init(); + vss_init(); mmd->server_pid = getpid(); setup_signal_handling(); mmd_lock(); @@ -411,7 +411,7 @@ static void status_refresh(void) if (prev_events != mmd->events) goto out; - if (mmd->new_afs_status_flags != mmd->afs_status_flags) + if (mmd->new_vss_status_flags != mmd->vss_status_flags) goto out; if (uptime / 60 != prev_uptime / 60) goto out; @@ -419,7 +419,7 @@ static void status_refresh(void) out: prev_uptime = uptime; prev_events = mmd->events; - mmd->afs_status_flags = mmd->new_afs_status_flags; + mmd->vss_status_flags = mmd->new_vss_status_flags; if (ret) { PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n", mmd->events, mmd->audio_format); @@ -449,7 +449,7 @@ repeat: /* check socket and signal pipe in any case */ para_fd_set(sockfd, &rfds, &max_fileno); para_fd_set(signal_pipe, &rfds, &max_fileno); - timeout = afs_preselect(); + timeout = vss_preselect(); status_refresh(); for (i = 0; senders[i].name; i++) { if (senders[i].status != SENDER_ON) @@ -478,7 +478,7 @@ repeat: continue; senders[i].post_select(&rfds, &wfds); } - afs_send_chunk(); + vss_send_chunk(); status_refresh(); if (FD_ISSET(signal_pipe, &rfds)) { int sig; diff --git a/server.h b/server.h index 8bec74fd..bd2b0500 100644 --- a/server.h +++ b/server.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2006 Andre Noll + * Copyright (C) 1997-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -82,9 +82,9 @@ struct misc_meta_data{ /* the number of the current audio format */ int audio_format; /** the "old" status flags -- commands may only read them */ - unsigned int afs_status_flags; + unsigned int vss_status_flags; /** the new status flags -- commands may set them **/ - unsigned int new_afs_status_flags; + unsigned int new_vss_status_flags; /** the number of data chunks sent for the current audio file */ long unsigned chunks_sent; /** the number of chunks this audio file contains */ diff --git a/user_list.c b/user_list.c index abd47e18..75dca081 100644 --- a/user_list.c +++ b/user_list.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Andre Noll + * Copyright (C) 2006-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -70,10 +70,10 @@ static void populate_user_list(char *user_list_file) u->perms = 0; while (num > 0) { num--; - if (!strcmp(tmp[num], "AFS_READ")) - u->perms |= AFS_READ; - else if (!strcmp(tmp[num], "AFS_WRITE")) - u->perms |= AFS_WRITE; + if (!strcmp(tmp[num], "VSS_READ")) + u->perms |= VSS_READ; + else if (!strcmp(tmp[num], "VSS_WRITE")) + u->perms |= VSS_WRITE; else if (!strcmp(tmp[num], "DB_READ")) u->perms |= DB_READ; else if (!strcmp(tmp[num], "DB_WRITE")) diff --git a/user_list.h b/user_list.h index 507ea02c..1ee73521 100644 --- a/user_list.h +++ b/user_list.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Andre Noll + * Copyright (C) 2006-2007 Andre Noll * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,10 +26,10 @@ * * - DB_READ: command reads from the database * - DB_WRITE: command changes the contents of the database - * - AFS_READ: command reads information about the current audio stream - * - AFS_WRITE: command changes the current audio stream + * - VSS_READ: command reads information about the current audio stream + * - VSS_WRITE: command changes the current audio stream */ -enum {DB_READ = 1, DB_WRITE = 2, AFS_READ = 4, AFS_WRITE = 8}; +enum {DB_READ = 1, DB_WRITE = 2, VSS_READ = 4, VSS_WRITE = 8}; /** * data needed to authenticate the user diff --git a/vss.c b/vss.c new file mode 100644 index 00000000..f9e2d6c2 --- /dev/null +++ b/vss.c @@ -0,0 +1,546 @@ +/* + * Copyright (C) 1997-2007 Andre Noll + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + */ + +/** \file vss.c the virtual streaming system + * + * This contains the audio sending part of para_server which is independent of + * the current audio format, audio file selector and of the activated senders. + */ + +#include "server.h" +#include /* gettimeofday */ +#include "server.cmdline.h" +#include "db.h" +#include "afh.h" +#include "vss.h" +#include "send.h" +#include "error.h" +#include "string.h" + +extern const char *status_item_list[]; + +static struct timeval announce_tv; +static struct timeval data_send_barrier; +static struct timeval eof_barrier; +static struct timeval autoplay_barrier; + +extern struct misc_meta_data *mmd; +extern struct audio_file_selector selectors[]; +extern struct sender senders[]; + +static FILE *audio_file = NULL; + +#if 1 + void mp3_init(struct audio_format_handler *); +#endif + +#ifdef HAVE_OGGVORBIS + void ogg_init(struct audio_format_handler *); +#endif +#ifdef HAVE_FAAD + void aac_afh_init(struct audio_format_handler *); +#endif + +/** + * the list of supported audio formats + */ +static struct audio_format_handler afl[] = { +#if 1 + { + .name = "mp3", + .init = mp3_init, + }, +#endif +#ifdef HAVE_OGGVORBIS + { + .name = "ogg", + .init = ogg_init, + }, +#endif +#ifdef HAVE_FAAD + { + .name = "aac", + .init = aac_afh_init, + }, +#endif + { + .name = NULL, + } +}; +#define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++) + +/** + * check if vss status flag \a P (playing) is set + * + * \return greater than zero if playing, zero otherwise. + * + */ +unsigned int vss_playing(void) +{ + return mmd->new_vss_status_flags & VSS_PLAYING; +} + +/** + * check if \a N (next) status flag is set + * + * \return greater than zero if set, zero if not. + * + */ +unsigned int vss_next(void) +{ + return mmd->new_vss_status_flags & VSS_NEXT; +} + +/** + * check if a reposition request is pending + * + * \return greater than zero if true, zero otherwise. + * + */ +unsigned int vss_repos(void) +{ + return mmd->new_vss_status_flags & VSS_REPOS; +} + +/** + * check if the vss is currently paused + * + * \return greater than zero if paused, zero otherwise. + * + */ +unsigned int vss_paused(void) +{ + return !(mmd->new_vss_status_flags & VSS_NEXT) + && !(mmd->new_vss_status_flags & VSS_PLAYING); +} + +/** + * get the name of the given audio format + * \param i the audio format number + * + * This returns a pointer to statically allocated memory so it + * must not be freed by the caller. + */ +const char *audio_format_name(int i) +{ + return i >= 0? afl[i].name : "(none)"; +} + +/** + * initialize the virtual streaming system + * + * Call the init functions of all supported audio format handlers and + * initialize all supported senders. + */ +void vss_init(void) +{ + int i; + char *hn = para_hostname(), *home = para_homedir(); + + PARA_DEBUG_LOG("supported audio formats: %s\n", + SUPPORTED_AUDIO_FORMATS); + for (i = 0; afl[i].name; i++) { + PARA_NOTICE_LOG("initializing %s handler\n", + afl[i].name); + afl[i].init(&afl[i]); + } + ms2tv(conf.announce_time_arg, &announce_tv); + PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv)); + for (i = 0; senders[i].name; i++) { + PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name); + senders[i].init(&senders[i]); + } + free(hn); + free(home); + if (conf.autoplay_given) { + struct timeval now, tmp; + mmd->vss_status_flags |= VSS_PLAYING; + mmd->new_vss_status_flags |= VSS_PLAYING; + gettimeofday(&now, NULL); + ms2tv(conf.autoplay_delay_arg, &tmp); + tv_add(&now, &tmp, &autoplay_barrier); + } +} + +static int get_file_info(int i) +{ + return afl[i].get_file_info(audio_file, mmd->audio_file_info, + &mmd->chunks_total, &mmd->seconds_total); +} + +/** + * guess the audio format judging from filename + * + * \param name the filename + * + * \return This function returns -1 if it has no idea what kind of audio + * file this might be. Otherwise the (non-negative) number of the audio format + * is returned. + */ +int guess_audio_format(const char *name) +{ + int i,j, len = strlen(name); + + FOR_EACH_AUDIO_FORMAT(i) { + for (j = 0; afl[i].suffixes[j]; j++) { + const char *p = afl[i].suffixes[j]; + int plen = strlen(p); + if (len < plen + 1) + continue; + if (name[len - plen - 1] != '.') + continue; + if (strcasecmp(name + len - plen, p)) + continue; +// PARA_DEBUG_LOG("might be %s\n", audio_format_name(i)); + return i; + } + } + return -1; +} + +static int get_audio_format(int omit) +{ + int i; + + FOR_EACH_AUDIO_FORMAT(i) { + if (i == omit || !afl[i].get_file_info) + continue; + rewind(audio_file); + if (get_file_info(i) > 0) + return i; + rewind(audio_file); + } + return -E_AUDIO_FORMAT; +} + +/* + * upddate shared mem + */ +static int update_mmd(void) +{ + int i; + struct stat file_status; + + i = guess_audio_format(mmd->filename); + if (i < 0 || get_file_info(i) < 0) + i = get_audio_format(i); + if (i < 0) + return i; + mmd->audio_format = i; + mmd->chunks_sent = 0; + mmd->current_chunk = 0; + mmd->offset = 0; + if (fstat(fileno(audio_file), &file_status) == -1) + return -E_FSTAT; + mmd->size = file_status.st_size; + mmd->mtime = file_status.st_mtime; + mmd->events++; + PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename); + return 1; +} + +static void get_song(void) +{ + char **sl = selectors[mmd->selector_num].get_audio_file_list(10); + int i; + + if (!sl) + goto err_out; + for (i = 0; sl[i]; i++) { + struct timeval now; + PARA_INFO_LOG("trying %s\n", sl[i]); + if (strlen(sl[i]) >= _POSIX_PATH_MAX) + continue; + audio_file = fopen(sl[i], "r"); + if (!audio_file) + continue; + strcpy(mmd->filename, sl[i]); + if (update_mmd() < 0) { + fclose(audio_file); + audio_file = NULL; + continue; + } + mmd->num_played++; + if (selectors[mmd->selector_num].update_audio_file) + selectors[mmd->selector_num].update_audio_file(sl[i]); + PARA_DEBUG_LOG("%s", "success\n"); + mmd->new_vss_status_flags &= (~VSS_NEXT); + gettimeofday(&now, NULL); + tv_add(&now, &announce_tv, &data_send_barrier); + + goto free; + } + PARA_ERROR_LOG("%s", "no valid files found\n"); +err_out: + mmd->new_vss_status_flags = VSS_NEXT; +free: + if (sl) { + for (i = 0; sl[i]; i++) + free(sl[i]); + free(sl); + } +} + +static int chk_barrier(const char *bname, const struct timeval *now, + const struct timeval *barrier, struct timeval *diff, + int print_log) +{ + long ms; + + if (tv_diff(now, barrier, diff) > 0) + return 1; + ms = tv2ms(diff); + if (print_log && ms) + PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms); + return -1; +} + +static void vss_next_chunk_time(struct timeval *due) +{ + struct timeval tmp; + + tv_scale(mmd->chunks_sent, &afl[mmd->audio_format].chunk_tv, &tmp); + tv_add(&tmp, &mmd->stream_start, due); +} + +/* + * != NULL: timeout for next chunk + * NULL: nothing to do + */ +static struct timeval *vss_compute_timeout(void) +{ + static struct timeval the_timeout; + struct timeval now, next_chunk; + + if (vss_next() && mmd->audio_format >= 0) { + /* only sleep a bit, nec*/ + the_timeout.tv_sec = 0; + the_timeout.tv_usec = 100; + return &the_timeout; + } + gettimeofday(&now, NULL); + if (chk_barrier("autoplay_delay", &now, &autoplay_barrier, + &the_timeout, 1) < 0) + return &the_timeout; + if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0) + return &the_timeout; + if (chk_barrier("data send", &now, &data_send_barrier, + &the_timeout, 1) < 0) + return &the_timeout; + if (mmd->audio_format < 0 || !vss_playing() || !audio_file) + return NULL; + vss_next_chunk_time(&next_chunk); + if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk, + &the_timeout, 0) < 0) + return &the_timeout; + /* chunk is due or bof */ + the_timeout.tv_sec = 0; + the_timeout.tv_usec = 0; + return &the_timeout; +} + +static void vss_eof(struct audio_format_handler *af) +{ + struct timeval now; + int i; + char *tmp; + + if (!af || !audio_file) { + for (i = 0; senders[i].name; i++) + senders[i].shutdown_clients(); + return; + } + gettimeofday(&now, NULL); + tv_add(&af->eof_tv, &now, &eof_barrier); + af->close_audio_file(); + audio_file = NULL; + mmd->audio_format = -1; + af = NULL; + mmd->chunks_sent = 0; + mmd->offset = 0; + mmd->seconds_total = 0; + tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1], + status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]); + strcpy(mmd->audio_file_info, tmp); + free(tmp); + tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1], + status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]); + strcpy(mmd->selector_info, tmp); + free(tmp); + mmd->filename[0] = '\0'; + mmd->size = 0; + mmd->events++; +} + +/** + * get the header and of the current audio file + * + * \param header_len the length of the header is stored here + * + * \return a pointer to a buffer containing the header, or NULL, if no audio + * file is selected or if the current audio format does not need special header + * treamtment. + * + */ +char *vss_get_header(int *header_len) +{ + *header_len = 0; + if (mmd->audio_format < 0) + return NULL; + if (!afl[mmd->audio_format].get_header_info) + return NULL; + return afl[mmd->audio_format].get_header_info(header_len); +} +const char *supported_audio_formats(void) +{ + return SUPPORTED_AUDIO_FORMATS; +} + +/** + * get the chunk time of the current audio file + * + * \return a pointer to a struct containing the chunk time, or NULL, + * if currently no audio file is selected. + */ +struct timeval *vss_chunk_time(void) +{ + if (mmd->audio_format < 0) + return NULL; + return &afl[mmd->audio_format].chunk_tv; +} + +/** + * compute the timeout for para_server's main select-loop + * + * This function gets called from para_server to determine the timeout value + * for its main select loop. + * + * Before the timeout is computed, the current vss status flags are evaluated + * and acted upon by calling appropriate functions from the lower layers. + * Possible actions include + * + * - request a new file list from the current audio file selector + * - shutdown of all senders (stop/pause command) + * - reposition the stream (ff/jmp command) + * + * \return A pointer to a struct timeval containing the timeout for the next + * chunk of data to be sent, or NULL if we're not sending right now. + */ +struct timeval *vss_preselect(void) +{ + struct audio_format_handler *af = NULL; + int i, format; + struct timeval *ret; +again: + format = mmd->audio_format; + if (format >= 0) + af = afl + format; + else + for (i = 0; senders[i].name; i++) + senders[i].shutdown_clients(); + if (vss_next() && af) { + vss_eof(af); + return vss_compute_timeout(); + } + if (vss_paused() || vss_repos()) { + for (i = 0; senders[i].name; i++) + senders[i].shutdown_clients(); + if (af) { + struct timeval now; + gettimeofday(&now, NULL); + if (!vss_paused() || mmd->chunks_sent) + tv_add(&af->eof_tv, &now, &eof_barrier); + if (vss_repos()) + tv_add(&now, &announce_tv, &data_send_barrier); + if (mmd->new_vss_status_flags & VSS_NOMORE) + mmd->new_vss_status_flags = VSS_NEXT; + } + mmd->chunks_sent = 0; + } + if (af && vss_repos() && mmd->current_chunk != mmd->repos_request) + af->reposition_stream(mmd->repos_request); + if (vss_repos()) { + mmd->new_vss_status_flags &= ~(VSS_REPOS); + mmd->current_chunk = mmd->repos_request; + } + ret = vss_compute_timeout(); + if (!ret && !audio_file && vss_playing() && + !(mmd->new_vss_status_flags & VSS_NOMORE)) { + PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n"); + get_song(); + goto again; + } + return ret; +} + +/** + * main sending function + * + * This function gets called from para_server as soon as the next chunk of + * data should be pushed out. It first calls the read_chunk() function of + * the current audio format handler to obtain a pointer to the data to be + * sent out as well as its length. This information is then passed to each + * supported sender's send() function which does the actual sending. + * + * Return value: Positive return value on success, zero on eof and negative + * on errors. + */ + +void vss_send_chunk(void) +{ + int i; + struct audio_format_handler *af; + char *buf; + ssize_t ret; + struct timeval now, due; + + if (mmd->audio_format < 0 || !audio_file || !vss_playing()) + return; + af = &afl[mmd->audio_format]; + gettimeofday(&now, NULL); + vss_next_chunk_time(&due); + if (tv_diff(&due, &now, NULL) > 0) + return; + if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0) + return; + if (chk_barrier("data send", &now, &data_send_barrier, + &due, 1) < 0) + return; + buf = af->read_chunk(mmd->current_chunk, &ret); + mmd->new_vss_status_flags &= ~VSS_REPOS; + if (!buf) { + if (ret < 0) + mmd->new_vss_status_flags = VSS_NEXT; + else + mmd->new_vss_status_flags |= VSS_NEXT; + vss_eof(af); + return; + } + if (!mmd->chunks_sent) { + struct timeval tmp; + gettimeofday(&mmd->stream_start, NULL); + tv_scale(mmd->current_chunk, &af->chunk_tv, &tmp); + mmd->offset = tv2ms(&tmp); + mmd->events++; + } + for (i = 0; senders[i].name; i++) + senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, ret); + mmd->new_vss_status_flags |= VSS_PLAYING; + mmd->chunks_sent++; + mmd->current_chunk++; +} diff --git a/vss.h b/vss.h new file mode 100644 index 00000000..545647b7 --- /dev/null +++ b/vss.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2005-2007 Andre Noll + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + */ + +/** \file vss.h exported functions from vss.c (para_server) */ +void vss_init(void); +void vss_send_chunk(void); +struct timeval *vss_preselect(void); +const char *audio_format_name(int); +unsigned int vss_playing(void); +unsigned int vss_next(void); +unsigned int vss_repos(void); +unsigned int vss_paused(void); +char *vss_get_header(int *header_len); +struct timeval *vss_chunk_time(void); +int guess_audio_format(const char *name); +const char *supported_audio_formats(void); +/* status flags */ +#define VSS_NOMORE 1 +#define VSS_NEXT 2 +#define VSS_REPOS 4 +#define VSS_PLAYING 8 +#define VSS_CHANGE 16