]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - play.c
para_play, infrastructure.
[paraslash.git] / play.c
diff --git a/play.c b/play.c
index 99c231697c76707008326ce2b587d4f5558b177f..fc33f68b9e13ee4111ce7ae915e5ace1fb888235 100644 (file)
--- a/play.c
+++ b/play.c
 /*
- * 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 BUFFER_SIZE 1000 * 1000
-#define WAV_HEADER_LEN 44
-#include <sys/time.h> /* gettimeofday */
 #include "para.h"
+#include "list.h"
 #include "play.cmdline.h"
-#include <alsa/asoundlib.h>
-
-enum { E_BROKEN_CONF,          /* Broken configuration for this PCM */
-       E_ACCESS_TYPE,          /* Access type not available */
-       E_SAMPLE_FORMAT,        /* Sample format not available */
-       E_CHANNEL_COUNT,        /* Channels count not available */
-       E_HW_PARAMS,            /* Unable to install hw params */
-       E_SW_PARAMS,            /* Unable to install sw params */
-       E_BAD_PERIOD,           /* Can't use period equal to buffer size */
-       E_GET_XFER,             /* Unable to obtain xfer align */
-       E_SET_XFER,             /* snd_pcm_sw_params_set_xfer_align failed */
-       E_MEM,                  /* not enough memory */
-       E_READ,                 /* read error */
-       E_WRITE,                /* write error */
-       E_PIPE,                 /* write to pipe with other side closed */
-       E_PCM_OPEN,             /* unable to open pcm */
-       E_SND_PCM_INFO,         /* pcm info error */
-       E_GET_BUFFER_TIME,      /* snd_pcm_hw_params_get_buffer_time_max failed */
-       E_SET_BUFFER_TIME,      /* snd_pcm_hw_params_set_buffer_time_near failed */
-       E_SET_RATE,             /* snd_pcm_hw_params_set_rate_near failed */
-       E_START_THRESHOLD,      /* snd_pcm_sw_params_set_start_threshold failed */
-       E_STOP_THRESHOLD,       /* snd_pcm_sw_params_set_stop_threshold failed */
-       E_LOG,                  /* snd_output_stdio_attach failed */
-       E_SYNTAX                /* could not parse start_time option */
-};
-
-#define FORMAT SND_PCM_FORMAT_S16_LE
-
-#define EXIT(EXP) \
-do { if (EXP) \
-        fprintf (stderr, "error: " #EXP "\n"); exit(EXP);} \
-while (0)
-
-static snd_pcm_t *handle;
-static unsigned char *audiobuf;
-static snd_pcm_uframes_t chunk_size;
-static size_t bytes_per_frame;
-static struct timeval *start_time;
-static struct gengetopt_args_info conf;
-
-/*
- * read_wav_header - read WAV_HEADER_LEN bytes from stdin to audio buffer
- *
- * Exit on errors and on eof before WAV_HEADER_LEN could be read.
- */
-static void read_wav_header(void)
-{
-       ssize_t ret, count = 0;
-
-       while (count < WAV_HEADER_LEN) {
-               ret = read(STDIN_FILENO, audiobuf + count, WAV_HEADER_LEN - count);
-               if (ret <= 0)
-                       EXIT(E_READ);
-               count += ret;
-       }
-}
-
-/*
- * set_alsa_params - Prepare the PCM handle for writing
- *
- * Install PCM software and hardware configuration. Exit on errors.
- */
-static void set_alsa_params(void)
+#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"
+
+static struct play_args_info conf;
+
+/** Initialize the array of errors for para_play. */
+INIT_PLAY_ERRLISTS;
+
+/* 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;
+
+/* FIXME: This is needed by the amp filter. */
+char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
+
+static int loglevel = LL_WARNING;
+INIT_STDERR_LOGGING(loglevel);
+
+__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_hw_params_alloca(&hwparams);
-       snd_pcm_sw_params_alloca(&swparams);
-       if (snd_pcm_hw_params_any(handle, hwparams) < 0)
-               EXIT(E_BROKEN_CONF);
-       if (snd_pcm_hw_params_set_access(handle, hwparams,
-                       SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
-               EXIT(E_ACCESS_TYPE);
-       if (snd_pcm_hw_params_set_format(handle, hwparams, FORMAT) < 0)
-               EXIT(E_SAMPLE_FORMAT);
-       if (snd_pcm_hw_params_set_channels(handle, hwparams, conf.channels_arg) < 0)
-               EXIT(E_CHANNEL_COUNT);
-       if (snd_pcm_hw_params_set_rate_near(handle, hwparams, (unsigned int*) &conf.sample_rate_arg, 0) < 0)
-               EXIT(E_SET_RATE);
-       err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
-       if (err < 0 || !buffer_time)
-               EXIT(E_GET_BUFFER_TIME);
-       if (buffer_time > 500000)
-               buffer_time = 500000;
-       if (snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
-                       &buffer_time, 0) < 0)
-               EXIT(E_SET_BUFFER_TIME);
-       if (snd_pcm_hw_params(handle, hwparams) < 0)
-               EXIT(E_HW_PARAMS);
-       snd_pcm_hw_params_get_period_size(hwparams, &chunk_size, 0);
-       snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
-       if (chunk_size == buffer_size)
-               EXIT(E_BAD_PERIOD);
-       snd_pcm_sw_params_current(handle, swparams);
-       err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
-       if (err < 0 || !xfer_align)
-               EXIT(E_GET_XFER);
-       snd_pcm_sw_params_set_sleep_min(handle, swparams, 0);
-       snd_pcm_sw_params_set_avail_min(handle, swparams, chunk_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(handle, swparams,
-                       start_threshold) < 0)
-               EXIT(E_START_THRESHOLD);
-       stop_threshold = buffer_size;
-       if (snd_pcm_sw_params_set_stop_threshold(handle, swparams,
-                       stop_threshold) < 0)
-               EXIT(E_STOP_THRESHOLD);
-       if (snd_pcm_sw_params_set_xfer_align(handle, swparams, xfer_align) < 0)
-               EXIT(E_SET_XFER);
-       if (snd_pcm_sw_params(handle, swparams) < 0)
-               EXIT(E_SW_PARAMS);
-       bytes_per_frame = snd_pcm_format_physical_width(FORMAT) * conf.channels_arg / 8;
+       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);
 }
 
-/*
- * pcm_write - push out pcm frames
- * @data: pointer do data to be written
- * @count: number of frames
- *
- * Return value: Number of bytes written. Exit on errors.
- */
-static snd_pcm_sframes_t pcm_write(u_char *data, size_t count)
+static void parse_config_or_die(int argc, char *argv[])
 {
-       snd_pcm_sframes_t r, result = 0;
-#if 0
-       if (count < chunk_size) {
-               snd_pcm_format_set_silence(FORMAT, data
-                       + count * bytes_per_frame,
-                       (chunk_size - count) * conf.channels_arg);
-               count = chunk_size;
+       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, &params))
+               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);
        }
-#endif
-       while (count > 0) {
-               /* write interleaved frames */
-               r = snd_pcm_writei(handle, data, count);
-               if (r == -EAGAIN || (r >= 0 && r < count))
-                       snd_pcm_wait(handle, 1);
-               else if (r == -EPIPE)
-                       snd_pcm_prepare(handle);
-               else if (r < 0)
-                       EXIT(E_WRITE);
-               if (r > 0) {
-                       result += r;
-                       count -= r;
-                       data += r * bytes_per_frame;
-               }
+       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 result;
-}
-
-/*
- * start_time_in_future - check if current time is later than start_time
- * @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;
-}
-
-/*
- * do_initial_delay - 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(void)
-{
-       struct timeval diff;
-       int ret;
-
-       fprintf(stderr, "initial delay\n");
-       if (!conf.start_time_given)
-               return;
-again:
-       if (!start_time_in_future(&diff))
-               return;
-       ret = select(1, NULL, NULL, NULL, &diff);
-       if (ret < 0 && errno == EINTR)
-               goto again;
-}
-
-
-/*
- * play_pcm - play raw pcm data
- * @l: 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.
- */
-static void play_pcm(size_t l)
-{
-       ssize_t r, w;
-       unsigned long written = 0;
-       size_t chunk_bytes;
-
-       set_alsa_params();
-       chunk_bytes = chunk_size * bytes_per_frame;
-       audiobuf = realloc(audiobuf, BUFFER_SIZE);
-//     fprintf(stderr, "loaded: %d, chunk_bytes: %d\n", l, chunk_bytes);
-       if (!audiobuf)
-               EXIT(E_MEM);
-       for (;;) {
-               for (;;) {
-                       if (l >= chunk_bytes) {
-                               if (written)
-                                       break;
-                               if (!start_time)
-                                       break;
-                               if (!start_time_in_future(NULL))
-                                       break;
-                               if (l > BUFFER_SIZE) {
-                                       do_initial_delay();
-                                       break;
-                               }
-                       }
-//                     fprintf(stderr, "l = %d, chunk_Bytes = %d\n", l, chunk_bytes);
-                       r = read(STDIN_FILENO, audiobuf + l, BUFFER_SIZE -l);
-                       if (r < 0)
-                               EXIT(E_READ);
-                       l += r;
-//                     fprintf(stderr, "loaded: %d, r= %d\n", l, r);
-                       if (!r)
-                               goto out;;
-               }
-               w = MIN(chunk_bytes, l);
-//             fprintf(stderr, "play: writing %d\n", w);
-               r = (ssize_t) pcm_write(audiobuf, w / bytes_per_frame) * bytes_per_frame;
-//             fprintf(stderr, "wrote %d\n", r);
-               if (r < 0)
-                       EXIT(E_WRITE);
-               written += r;
-               l -= r;
-               if (l)
-                       memmove(audiobuf, audiobuf + r, l);
-//             fprintf(stderr, "written %lu, loaded : %d\n", written, l);
+       if (ret) {
+               params.initialize = 0;
+               params.check_required = 1;
+               play_cmdline_parser_config_file(config_file, &conf, &params);
        }
-out:
-       snd_pcm_drain(handle);
-}
-
-/*
- * check_wave - test if audio buffer contains a valid wave header
- *
- * 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);
-//     fprintf(stderr, "channels: %d, rate: %d\n", conf.channels_arg,
-//             conf.sample_rate_arg);
-       return 0;
+       free(config_file);
+       return;
+err:
+       free(config_file);
+       exit(EXIT_FAILURE);
 }
 
 int main(int argc, char *argv[])
 {
-       snd_pcm_info_t *info;
-       snd_output_t *log;
-       struct timeval tv;
-       int err;
+       /* needed this early to make help work */
+       recv_init();
+       filter_init();
+       writer_init();
 
-       cmdline_parser(argc, argv, &conf);
-       if (conf.start_time_given) {
-               if (sscanf(conf.start_time_arg, "%lu:%lu",
-                               &tv.tv_sec, &tv.tv_usec) != 2)
-                       EXIT(E_SYNTAX);
-               start_time = &tv;
-       }
-//     fprintf(stderr, "argc=%d, argv[1]=%s\n",argc,  argv[1]);
-       snd_pcm_info_alloca(&info);
-       if (snd_output_stdio_attach(&log, stderr, 0) < 0)
-               EXIT(E_LOG);
-       err = snd_pcm_open(&handle, "plug:swmix",
-               SND_PCM_STREAM_PLAYBACK, 0);
-       if (err < 0)
-               EXIT(E_PCM_OPEN);
-       if ((err = snd_pcm_info(handle, info)) < 0)
-               EXIT(E_SND_PCM_INFO);
-       audiobuf = malloc(WAV_HEADER_LEN);
-       read_wav_header();
-       play_pcm(check_wave());
-       snd_pcm_close(handle);
-       free(audiobuf);
-       snd_output_close(log);
-       snd_config_update_free_global();
-       return EXIT_SUCCESS;
+       parse_config_or_die(argc, argv);
+       return 0;
 }