/*
- * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2012 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
- * 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.
*/
-/*
- * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
- * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
- * based on the vplay program by Michael Beck.
- */
+/** \file play.c Paraslash's standalone player. */
+
+#include <regex.h>
-#define WAV_HEADER_LEN 44
-#include <sys/time.h> /* gettimeofday */
#include "para.h"
-#include "fd.h"
+#include "list.h"
#include "play.cmdline.h"
-#include <alsa/asoundlib.h>
-#include "string.h"
+#include "filter.cmdline.h"
#include "error.h"
+#include "ggo.h"
+#include "buffer_tree.h"
+#include "version.h"
+#include "string.h"
+#include "sched.h"
+#include "filter.h"
+#include "afh.h"
+#include "recv.h"
+#include "write.h"
+#include "write_common.h"
+#include "fd.h"
-#define FORMAT SND_PCM_FORMAT_S16_LE
-
-struct private_alsa_data {
- snd_pcm_t *handle;
- size_t bytes_per_frame;
-};
-
-struct writer_node {
- struct writer *writer;
- void *private_data;
- int chunk_bytes;
-};
-
-struct writer {
- int (*open)(struct writer_node *);
- int (*write)(char *data, size_t nbytes, struct writer_node *);
- void (*close)(struct writer_node *);
- void (*shutdown)(struct writer_node *);
-};
-
-#define NUM_WRITERS 1
-static struct writer writers[NUM_WRITERS];
-#define FOR_EACH_WRITER(i) for (i = 0; i < NUM_WRITERS, i++)
-static struct writer_node *writer_nodes;
-
-
-static unsigned char *audiobuf;
-static struct timeval *start_time;
-static struct gengetopt_args_info conf;
+static struct play_args_info conf;
+/** Initialize the array of errors for para_play. */
INIT_PLAY_ERRLISTS;
-void para_log(int ll, const char* fmt,...)
-{
- va_list argp;
-
- if (ll < conf.loglevel_arg)
- return;
- va_start(argp, fmt);
- vfprintf(stderr, fmt, argp);
- va_end(argp);
-}
+/* Activate the afh receiver. */
+extern void afh_recv_init(struct receiver *r);
+#undef AFH_RECEIVER
+#define AFH_RECEIVER {.name = "afh", .init = afh_recv_init},
+DEFINE_RECEIVER_ARRAY;
-/**
- * read WAV_HEADER_LEN bytes from stdin to audio buffer
- *
- * \return -E_READ_HDR on errors and on eof before WAV_HEADER_LEN could be
- * read. A positive return value indicates success.
- */
-static int read_wav_header(void)
-{
- ssize_t ret, count = 0;
+/* FIXME: This is needed by the amp filter. */
+char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
- while (count < WAV_HEADER_LEN) {
- ret = read(STDIN_FILENO, audiobuf + count, WAV_HEADER_LEN - count);
- if (ret <= 0)
- return -E_READ_HDR;
- count += ret;
- }
- return 1;
-}
+static int loglevel = LL_WARNING;
+INIT_STDERR_LOGGING(loglevel);
-/*
- * open and prepare the PCM handle for writing
- *
- * Install PCM software and hardware configuration. Exit on errors.
- */
-static int alsa_open(struct writer_node *w)
+__noreturn static void print_help_and_die(void)
{
- snd_pcm_hw_params_t *hwparams;
- snd_pcm_sw_params_t *swparams;
- snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
- stop_threshold;
- unsigned buffer_time = 0;
- int err;
- snd_pcm_info_t *info;
- snd_output_t *log;
- snd_pcm_uframes_t period_size;
- struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
- w->private_data = pad;
-
- snd_pcm_info_alloca(&info);
- if (snd_output_stdio_attach(&log, stderr, 0) < 0)
- return -E_ALSA_LOG;
- err = snd_pcm_open(&pad->handle, conf.device_arg,
- SND_PCM_STREAM_PLAYBACK, 0);
- if (err < 0)
- return -E_PCM_OPEN;
- if ((err = snd_pcm_info(pad->handle, info)) < 0)
- return -E_SND_PCM_INFO;
-
- snd_pcm_hw_params_alloca(&hwparams);
- snd_pcm_sw_params_alloca(&swparams);
- if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
- return -E_BROKEN_CONF;
- if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
- SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
- return -E_ACCESS_TYPE;
- if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
- return -E_SAMPLE_FORMAT;
- if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
- conf.channels_arg) < 0)
- return -E_CHANNEL_COUNT;
- if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
- (unsigned int*) &conf.sample_rate_arg, 0) < 0)
- return -E_SET_RATE;
- err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
- if (err < 0 || !buffer_time)
- return -E_GET_BUFFER_TIME;
- PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
- if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
- &buffer_time, 0) < 0)
- return -E_SET_BUFFER_TIME;
- if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
- return -E_HW_PARAMS;
- snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
- snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
- PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
- period_size);
- if (period_size == buffer_size)
- return -E_BAD_PERIOD;
- snd_pcm_sw_params_current(pad->handle, swparams);
- err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
- if (err < 0 || !xfer_align)
- return -E_GET_XFER;
- snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
- /* round to closest transfer boundary */
- start_threshold = (buffer_size / xfer_align) * xfer_align;
- if (start_threshold < 1)
- start_threshold = 1;
- if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
- start_threshold) < 0)
- return -E_START_THRESHOLD;
- stop_threshold = buffer_size;
- if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
- stop_threshold) < 0)
- return -E_STOP_THRESHOLD;
- if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams,
- xfer_align) < 0)
- return -E_SET_XFER;
- if (snd_pcm_sw_params(pad->handle, swparams) < 0)
- return -E_SW_PARAMS;
- pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
- * conf.channels_arg / 8;
- return period_size * pad->bytes_per_frame;
+ int d = conf.detailed_help_given;
+ const char **p = d? play_args_info_detailed_help
+ : play_args_info_help;
+
+ printf_or_die("%s\n\n", PLAY_CMDLINE_PARSER_PACKAGE "-"
+ PLAY_CMDLINE_PARSER_VERSION);
+ printf_or_die("%s\n\n", play_args_info_usage);
+ for (; *p; p++)
+ printf_or_die("%s\n", *p);
+ print_filter_helps(d);
+ print_writer_helps(d);
+ exit(0);
}
-/**
- * push out pcm frames
- * \param data pointer do data to be written
- * \param nbytes number of bytes (not frames)
- *
- * \return Number of bytes written, -E_ALSA_WRITE on errors.
- */
-static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
+static void parse_config_or_die(int argc, char *argv[])
{
- struct private_alsa_data *pad = wn->private_data;
- size_t frames = nbytes / pad->bytes_per_frame;
- unsigned char *d = data;
- snd_pcm_sframes_t r, result = 0;
-
- while (frames > 0) {
- /* write interleaved frames */
- r = snd_pcm_writei(pad->handle, d, frames);
- if (r < 0)
- PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
- if (r == -EAGAIN || (r >= 0 && r < frames))
- snd_pcm_wait(pad->handle, 1);
- else if (r == -EPIPE)
- snd_pcm_prepare(pad->handle);
- else if (r < 0)
- return -E_ALSA_WRITE;
- if (r > 0) {
- result += r;
- frames -= r;
- d += r * pad->bytes_per_frame;
- }
+ int ret;
+ char *config_file;
+ struct play_cmdline_parser_params params = {
+ .override = 0,
+ .initialize = 1,
+ .check_required = 0,
+ .check_ambiguity = 0,
+ .print_errors = 1
+ };
+
+ if (play_cmdline_parser_ext(argc, argv, &conf, ¶ms))
+ exit(EXIT_FAILURE);
+ HANDLE_VERSION_FLAG("play", conf);
+ if (conf.help_given || conf.detailed_help_given)
+ print_help_and_die();
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ if (conf.config_file_given)
+ config_file = para_strdup(conf.config_file_arg);
+ else {
+ char *home = para_homedir();
+ config_file = make_message("%s/.paraslash/play.conf", home);
+ free(home);
}
- return result * pad->bytes_per_frame;
-}
-
-static void alsa_close(struct writer_node *wn)
-{
- struct private_alsa_data *pad = wn->private_data;
- snd_pcm_drain(pad->handle);
- snd_pcm_close(pad->handle);
- snd_config_update_free_global();
- free(pad);
-}
-
-void alsa_writer_init(struct writer *w)
-{
- w->open = alsa_open;
- w->write = alsa_write;
- w->close = alsa_close;
- w->shutdown = NULL; /* nothing to do */
-}
-
-
-/**
- * check if current time is later than start_time
- * \param diff pointer to write remaining time to
- *
- * If start_time was not given, or current time is later than given
- * start_time, return 0. Otherwise, return 1 and write the time
- * difference between current time and start_time to diff. diff may be
- * NULL.
- *
- */
-static int start_time_in_future(struct timeval *diff)
-{
- struct timeval now;
-
- if (!conf.start_time_given)
- return 0;
- gettimeofday(&now, NULL);
- return tv_diff(start_time, &now, diff) > 0? 1 : 0;
-}
-
-/**
- * sleep until time given at command line
- *
- * This is called if the initial buffer is filled. It returns
- * immediately if no start_time was given at the command line
- * or if the given start time is in the past.
- *
- */
-static void do_initial_delay(struct timeval *delay)
-{
- do
- para_select(1, NULL, NULL, delay);
- while (start_time_in_future(delay));
-}
-
-static int read_stdin(char *buf, size_t bytes_to_load, size_t *loaded)
-{
- ssize_t ret;
-
- while (*loaded < bytes_to_load) {
- ret = read(STDIN_FILENO, buf + *loaded, bytes_to_load - *loaded);
- if (ret <= 0) {
- if (ret < 0)
- ret = -E_READ_STDIN;
- return ret;
- }
- *loaded += ret;
+ ret = file_exists(config_file);
+ if (conf.config_file_given && !ret) {
+ PARA_EMERG_LOG("can not read config file %s\n", config_file);
+ goto err;
}
- return 1;
-}
-
-/**
- * play raw pcm data
- * \param loaded number of bytes already loaded
- *
- * If start_time was given, prebuffer data until buffer is full or
- * start_time is reached. In any case, do not start playing before
- * start_time.
- *
- * \return positive on success, negative on errors.
- */
-static int play_pcm(size_t loaded)
-{
- size_t bufsize, min_written = 0, prebuf_size, bytes_to_load;
- struct timeval delay;
- int eof = 0, i, max_chunk_bytes = 0, ret, not_yet_started = 1, need_more_writes;
- struct writer_node *wn;
- size_t *written = para_calloc(1 * sizeof(size_t));
-
- for (i = 0; i < 1; i++) {
- wn = &writer_nodes[i];
- ret = wn->writer->open(wn);
- if (ret < 0)
- goto out;
- wn->chunk_bytes = ret;
- max_chunk_bytes = PARA_MAX(max_chunk_bytes, ret);
- }
- PARA_INFO_LOG("max chunk_bytes: %d\n", max_chunk_bytes);
- bufsize = (conf.bufsize_arg * 1024 / max_chunk_bytes) * max_chunk_bytes;
- audiobuf = para_realloc(audiobuf, bufsize);
- prebuf_size = conf.prebuffer_arg * bufsize / 100;
- bytes_to_load = PARA_MAX(prebuf_size, max_chunk_bytes);
- ret = read_stdin(audiobuf, bytes_to_load, &loaded);
- if (ret <= 0 || loaded < bytes_to_load) {
- if (ret >= 0)
- ret = -E_PREMATURE_END;
- goto out;
- }
- if (not_yet_started && start_time && start_time_in_future(&delay))
- do_initial_delay(&delay);
- not_yet_started = 0;
-again:
- need_more_writes = 1;
- while (need_more_writes) {
- need_more_writes = 0;
- for (i = 0; i < 1; i++) {
- unsigned char *p = audiobuf + written[i];
- int bytes_to_write;
- wn = &writer_nodes[i];
- if (!i)
- min_written = written[i];
- else
- min_written = PARA_MIN(min_written, written[i]);
- if (loaded == written[i])
- continue;
- if (!eof && (loaded < wn->chunk_bytes + written[i]))
- continue;
- bytes_to_write = PARA_MIN(wn->chunk_bytes,
- loaded - written[i]);
- ret = wn->writer->write(p, bytes_to_write, wn);
- if (ret < 0)
- goto out;
- if (ret != bytes_to_write)
- PARA_WARNING_LOG("short write: %d/%d\n", ret,
- bytes_to_write);
- written[i] += ret;
- need_more_writes = 1;
- }
- }
- loaded -= min_written;
- ret = 0;
- if (eof)
- goto out;
- if (loaded >= bufsize) {
- ret = -E_PLAY_OVERRUN;
- goto out;
- }
- memmove(audiobuf, audiobuf + min_written, loaded);
- for (i = 0; i < 1; i++)
- written[i] -= min_written;
- bytes_to_load = PARA_MIN(max_chunk_bytes, bufsize);
- ret = read_stdin(audiobuf, bytes_to_load, &loaded);
- if (ret < 0)
- goto out;
- if (!ret)
- eof = 1;
- goto again;
-out:
- free(written);
- for (i = 0; i < 1; i++) {
- wn = &writer_nodes[i];
- wn->writer->close(wn);
+ if (ret) {
+ params.initialize = 0;
+ params.check_required = 1;
+ play_cmdline_parser_config_file(config_file, &conf, ¶ms);
}
- return ret;
-}
-
-/**
- * test if audio buffer contains a valid wave header
- *
- * \return If not, return 0, otherwise, store number of channels and sample rate
- * in struct conf and return WAV_HEADER_LEN.
- */
-static size_t check_wave(void)
-{
- unsigned char *a = audiobuf;
- if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
- return WAV_HEADER_LEN;
- conf.channels_arg = (unsigned) a[22];
- conf.sample_rate_arg = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
- return 0;
+ free(config_file);
+ return;
+err:
+ free(config_file);
+ exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
- struct timeval tv;
- int ret = -E_PLAY_SYNTAX;
-
- cmdline_parser(argc, argv, &conf);
- if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
- goto out;
- if (conf.start_time_given) {
- if (sscanf(conf.start_time_arg, "%lu:%lu",
- &tv.tv_sec, &tv.tv_usec) != 2)
- goto out;
- start_time = &tv;
- }
- /* call init for each supported writer */
- alsa_writer_init(&writers[0]);
- /* one for each given writer */
- writer_nodes = para_calloc(2 * sizeof(struct writer_node));
- writer_nodes[0].writer = &writers[0]; /* alsa */
+ /* needed this early to make help work */
+ recv_init();
+ filter_init();
+ writer_init();
- audiobuf = para_malloc(WAV_HEADER_LEN);
- ret = read_wav_header();
- if (ret < 0)
- goto out;
- ret = play_pcm(check_wave());
-out:
- free(audiobuf);
- free(writer_nodes);
- if (ret < 0)
- PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
- return ret;
+ parse_config_or_die(argc, argv);
+ return 0;
}