/*
- * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
*
* 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
* 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 <sys/time.h> /* gettimeofday */
+/** \file afs.c functions common to all audio file selectors */
+
#include "server.cmdline.h"
-#include "db.h"
-#include "afh.h"
-#include "afs.h"
-#include "send.h"
+#include "server.h"
+#include "vss.h"
+#include <dirent.h> /* readdir() */
+#include <sys/stat.h> /* stat */
+#include <sys/types.h> /* 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 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);
-}
-
-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)
+ * traverse the given directory recursively
+ *
+ * @param dirname the directory to traverse
+ * @param f: the function to call for each entry.
+ *
+ * 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.
+ */
+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 = -1;
+
+// PARA_DEBUG_LOG("dirname: %s\n", dirname);
+ 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;
- 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)
+ if (!strcmp(entry->d_name, ".."))
continue;
- audio_file = fopen(sl[i], "r");
- if (!audio_file)
+ ret = -E_LSTAT;
+ if (lstat(entry->d_name, &s) == -1)
continue;
- strcpy(mmd->filename, sl[i]);
- if (update_mmd() < 0) {
- fclose(audio_file);
- audio_file = NULL;
+ m = s.st_mode;
+ if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
+ continue;
+ 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 (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("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) {
+ /* 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(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;
}