X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=afs.c;h=13a9ccb26a066760bd23c758e3384dbf2057bcfc;hp=6c3df15ea814a45d11768cb81c508bf9d0316a72;hb=6c2eae7aea002304e3aa5ae400dab01c469fef0a;hpb=3e514007f72825597eaf68ff33339a8eb0ad420b diff --git a/afs.c b/afs.c index 6c3df15e..13a9ccb2 100644 --- a/afs.c +++ b/afs.c @@ -1,479 +1,94 @@ /* - * Copyright (C) 1997-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 - * 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. + * Licensed under the GPL v2. For licencing details see COPYING. */ -/** \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 /* gettimeofday */ +/** \file afs.c Functions common to all audio file selectors. */ + #include "server.cmdline.h" -#include "db.h" #include "server.h" -#include "afs.h" -#include "send.h" +#include "vss.h" +#include /* readdir() */ +#include /* stat */ +#include /* mode_t */ #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; - -extern struct misc_meta_data *mmd; -extern struct audio_file_selector dblist[]; -extern struct sender senders[]; -extern struct gengetopt_args_info conf; - -static FILE *audio_file = NULL; - -#if 1 - void mp3_init(void *); -#endif - -#ifdef HAVE_OGGVORBIS - void ogg_init(void *); -#endif - -/** - * the list of supported audio formats - */ -struct audio_format afl[] = { -#if 1 - { - .name = "mp3", - .init = mp3_init, - }, -#endif -#ifdef HAVE_OGGVORBIS - { - .name = "ogg", - .init = ogg_init, - }, -#endif - { - .name = NULL, - } -}; - - /** - * check if audio file sender is playing + * Traverse the given directory recursively. * - * \return greater than zero if playing, zero otherwise. + * \param dirname The directory to traverse. + * \param f The function to call for each entry. * - */ -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. + * For each regular file whose filename ends in .yyy, where yyy is a supported + * audio format, the supplied function \a f is called. The directory and + * filename component of the regular file are passed to \a f. * + * \return On success, 1 is returned. Otherwise, this function returns a + * negative value which indicates the kind of the error. */ -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: %lu:%lu\n", announce_tv.tv_sec, announce_tv.tv_usec); - 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); -} - -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. - */ -static int guess_audio_format(const char *name) -{ - - int i, len1 = strlen(name), len2; - - for (i = 0; afl[i].name; i++) { - len2 = strlen(afl[i].name); - if (len1 < len2) +int find_audio_files(const char *dirname, int (*f)(const char *, const char *)) +{ + DIR *dir = NULL; + struct dirent *entry; + /* + * Opening the current directory (".") and calling fchdir() to return + * is usually faster and more reliable than saving cwd in some buffer + * and calling chdir() afterwards (see man 3 getcwd). + */ + int cwd_fd = open(".", O_RDONLY); + struct stat s; + int ret; + + if (cwd_fd < 0) + return -E_GETCWD; + ret = -E_CHDIR; + if (chdir(dirname) < 0) + goto out; + ret = -E_OPENDIR; + dir = opendir("."); + if (!dir) + goto out; + /* scan cwd recursively */ + while ((entry = readdir(dir))) { + mode_t m; + char *tmp; + + if (!strcmp(entry->d_name, ".")) continue; - if (!strncasecmp(name + (len1 - len2), afl[i].name, len2)) { - PARA_DEBUG_LOG("might be %s\n", afl[i].name); - return i; - } - } - return -1; -} - -static int get_audio_format(int omit) -{ - int i; - - for (i = 0; afl[i].name; i++) { - if (i == omit || !afl[i].get_file_info) + if (!strcmp(entry->d_name, "..")) 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 = dblist[mmd->dbt_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) + ret = -E_LSTAT; + if (lstat(entry->d_name, &s) == -1) continue; - audio_file = fopen(sl[i], "r"); - if (!audio_file) + m = s.st_mode; + if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */ continue; - strcpy(mmd->filename, sl[i]); - if (update_mmd() < 0) { - fclose(audio_file); - audio_file = NULL; + if (S_ISREG(m)) { /* regular file */ + if (guess_audio_format(entry->d_name) < 0) + continue; + ret = f(dirname, entry->d_name); + if (ret < 0) + goto out; continue; } - mmd->num_played++; - if (dblist[mmd->dbt_num].update_audio_file) - dblist[mmd->dbt_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 log) -{ - long ms; - - if (tv_diff(now, barrier, diff) > 0) - return 1; - ms = tv2ms(diff); - if (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("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 *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->dbinfo, tmp); - free(tmp); - mmd->filename[0] = '\0'; - mmd->size = 0; - mmd->events++; -} - -/** - * 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 dabase tool (audio file change) - * - shutdown of all senders (stop/pause command) - * - repositioning of 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 *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; - if (!afs_paused() || mmd->chunks_sent) { - gettimeofday(&now, NULL); - 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 *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; - buf = af->read_chunk(mmd->current_chunk, &ret); - mmd->new_afs_status_flags &= ~(AFS_NEXT | AFS_REPOS); - if (!buf) { + /* directory */ + tmp = make_message("%s/%s", dirname, entry->d_name); + ret = find_audio_files(tmp, f); + free(tmp); if (ret < 0) - mmd->new_afs_status_flags = AFS_NEXT; - else - mmd->new_afs_status_flags |= AFS_NEXT; - afs_eof(af); - return; + goto out; } - 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(af, mmd->current_chunk, - mmd->chunks_sent, buf, ret); - mmd->new_afs_status_flags |= AFS_PLAYING; - mmd->chunks_sent++; - mmd->current_chunk++; + ret = 1; +out: + if (dir) + closedir(dir); + if (fchdir(cwd_fd) < 0) + ret = -E_CHDIR; + close(cwd_fd); + if (ret < 0) + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + return ret; }