#include "filter.h"
#include "grab_client.cmdline.h"
#include "grab_client.h"
-#include "ringbuffer.h"
#include "error.h"
#include "audiod.h"
sa_time_diff_sign = 1, audiod_socket = -1;
static char *af_status, /* the audio format announced in server status */
*socket_name, *hostname;
-/** how many status items to remember */
-#define RINGBUFFER_SIZE 32
-static void *stat_item_ringbuf;
+static char *stat_item_values[NUM_STAT_ITEMS];
static FILE *logfile;
static const struct timeval restart_delay = {0, 300 * 1000};
-static struct audio_format_info afi[] = {
-
-[AUDIO_FORMAT_MP3] =
- {
- .write_cmd = "para_play",
- },
-[AUDIO_FORMAT_OGG] =
- {
- .write_cmd = "para_play",
- },
-};
+static struct audio_format_info afi[NUM_AUDIO_FORMATS];
static struct audiod_command cmds[] = {
{
.description = "grab the audio stream",
.synopsis = "-- grab [grab_options]",
.help =
+
"grab ('splice') the audio stream at any position in the filter \n"
-"chain and send that data back to the client. \n"
-"Available options:\n\n"
-GRAB_HELP_TXT
+"chain and send that data back to the client. Try\n"
+"\t para_audioc -- grab -h\n"
+"for the list of available options.\n"
},
+
{
.name = "help",
.handler = com_help,
.name = "stat",
.handler = com_stat,
.description = "print status information",
-.synopsis = "stat",
+.synopsis = "stat [item1 ...]",
.help =
-"Add para_audiod status information to para_server's status information\n"
-"and dump everything to stdout.\n"
+"Dump given status items (all if none given) to stdout.\n"
},
{
/*
* log function. first argument is loglevel.
*/
-void para_log(int ll, char* fmt,...)
+void para_log(int ll, const char* fmt,...)
{
va_list argp;
FILE *outfd;
struct timeval now, diff, adj_stream_start, tmp;
int total = 0, use_server_time = 1;
- if (!playing)
- return make_message("%s:", length_seconds?
- "" : status_item_list[SI_PLAY_TIME]);
+ if (!playing) {
+ if (length_seconds)
+ return NULL;
+ return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
+ }
if (audiod_status == AUDIOD_OFF)
goto out;
if (sa_time_diff_sign > 0)
total = 0;
out:
return make_message(
- "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)",
+ "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
status_item_list[SI_PLAY_TIME],
use_server_time? "~" : "",
total / 60,
);
}
-static char *audiod_status_string(void)
+__malloc static char *audiod_status_string(void)
+{
+ const char *status = (audiod_status == AUDIOD_ON)?
+ "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
+ return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
+}
+
+static struct timeval *wstime(void)
{
int i;
- struct timeval *newest_stime = NULL;
- char *ret, *time_string, *uptime_string, *decoder_flags =
- para_malloc((MAX_STREAM_SLOTS + 1) * sizeof(char));
+ struct timeval *max = NULL;
+ FOR_EACH_SLOT(i) {
+ struct slot_info *s = &slot[i];
+ if (s->wpid <= 0)
+ continue;
+ if (max && tv_diff(&s->wstime, max, NULL) <= 0)
+ continue;
+ max = &s->wstime;
+ }
+ return max;
+}
+__malloc static char *decoder_flags(void)
+{
+ int i;
+ char decoder_flags[MAX_STREAM_SLOTS + 1];
FOR_EACH_SLOT(i) {
struct slot_info *s = &slot[i];
if (flag != '0')
flag += s->format * 4;
decoder_flags[i] = flag;
- if (s->wpid <= 0)
- continue;
- if (newest_stime && tv_diff(&s->wstime, newest_stime, NULL) <= 0)
- continue;
- newest_stime = &s->wstime;
}
decoder_flags[MAX_STREAM_SLOTS] = '\0';
- time_string = get_time_string(newest_stime);
- uptime_string = uptime_str();
- ret = make_message("%s:%s\n%s:%s\n%s:%s\n%s",
- status_item_list[SI_AUDIOD_UPTIME], uptime_string,
- status_item_list[SI_DECODER_FLAGS], decoder_flags,
- status_item_list[SI_AUDIOD_STATUS], audiod_status == AUDIOD_ON?
- "on" : (audiod_status == AUDIOD_OFF? "off": "sb"),
- time_string);
- free(uptime_string);
- free(decoder_flags);
- free(time_string);
- return ret;
+ return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
+ decoder_flags);
}
static char *configfile_exists(void)
static void audiod_status_dump(void)
{
- static char *prev_status;
- char *tmp = audiod_status_string();
-
- if (!prev_status || strcmp(tmp, prev_status))
- stat_client_write(tmp);
- free(prev_status);
- prev_status = tmp;
+ static char *p_ts, *p_us, *p_as, *p_df;
+ struct timeval *t = wstime();
+ char *us, *tmp = get_time_string(t);
+
+ if (tmp && (!p_ts || strcmp(tmp, p_ts)))
+ stat_client_write(tmp, SI_PLAY_TIME);
+ free(p_ts);
+ p_ts = tmp;
+
+ us = uptime_str();
+ tmp = make_message("%s:%s\n", status_item_list[SI_AUDIOD_UPTIME], us);
+ free(us);
+ if (!p_us || strcmp(p_us, tmp))
+ stat_client_write(tmp, SI_AUDIOD_UPTIME);
+ free(p_us);
+ p_us = tmp;
+
+ tmp = audiod_status_string();
+ if (!p_as || strcmp(p_as, tmp))
+ stat_client_write(tmp, SI_AUDIOD_STATUS);
+ free(p_as);
+ p_as = tmp;
+
+ tmp = decoder_flags();
+ if (!p_df || strcmp(p_df, tmp))
+ stat_client_write(tmp, SI_DECODER_FLAGS);
+ free(p_df);
+ p_df = tmp;
}
static void clear_slot(int slot_num)
s->fci->error = 1;
}
+static void set_restart_barrier(int format, struct timeval *now)
+{
+ struct timeval tmp;
+
+ if (now)
+ tmp = *now;
+ else
+ gettimeofday(&tmp, NULL);
+ tv_add(&tmp, &restart_delay, &afi[format].restart_barrier);
+}
+
static void close_receiver(int slot_num)
{
struct slot_info *s = &slot[slot_num];
struct audio_format_info *a;
- struct timeval now;
if (s->format < 0 || !s->receiver_node)
return;
a->receiver->close(s->receiver_node);
free(s->receiver_node);
s->receiver_node = NULL;
- gettimeofday(&now, NULL);
- tv_add(&now, &restart_delay, &a->restart_barrier); /* FIXME: Use set_restart_barrier() */
+ set_restart_barrier(s->format, NULL);
}
static void kill_all_decoders(void)
}
}
-static void set_restart_barrier(int format, struct timeval *now)
-{
- struct timeval tmp;
-
- if (now)
- tmp = *now;
- else
- gettimeofday(&tmp, NULL);
- tv_add(&tmp, &restart_delay, &afi[format].restart_barrier);
-}
-
static void check_sigchld(void)
{
pid_t pid;
static void close_stat_pipe(void)
{
- char *msg;
int i;
if (stat_pipe < 0)
del_close_on_fork_list(stat_pipe);
stat_pipe = -1;
kill_all_decoders();
- for (i = 0; i < RINGBUFFER_SIZE; i++)
- ringbuffer_add(stat_item_ringbuf, para_strdup(NULL));
+ for (i = 0; i < NUM_STAT_ITEMS; i++) {
+ free(stat_item_values[i]);
+ stat_item_values[i] = NULL;
+ }
dump_empty_status();
length_seconds = 0;
offset_seconds = 0;
audiod_status_dump();
playing = 0;
- msg = make_message("%s:no connection to para_server\n",
+ stat_item_values[SI_STATUS_BAR] = make_message("%s:no connection to para_server\n",
status_item_list[SI_STATUS_BAR]);
- ringbuffer_add(stat_item_ringbuf, msg);
- stat_client_write(msg);
- free(msg);
+ stat_client_write(stat_item_values[SI_STATUS_BAR], SI_STATUS_BAR);
}
static void __noreturn clean_exit(int status, const char *msg)
exit(status);
}
-static char *glob_cmd(char *cmd)
+__malloc static char *glob_cmd(char *cmd)
{
char *ret, *replacement;
struct timeval tmp, delay, rss; /* real stream start */
int ret, fds[3] = {1, -1, -1};
struct slot_info *s = &slot[slot_num];
struct audio_format_info *a = &afi[s->format];
- char *glob = glob_cmd(a->write_cmd);
+ char *glob = NULL;
- PARA_INFO_LOG("starting stream writer: %s\n", glob? glob : a->write_cmd);
+ if (a->write_cmd)
+ glob = glob_cmd(a->write_cmd);
+ if (!glob)
+ glob = para_strdup("para_play");
+ PARA_INFO_LOG("starting stream writer: %s\n", glob);
open_filters(slot_num);
-
- ret = para_exec_cmdline_pid(&s->wpid, glob? glob : a->write_cmd, fds);
+ ret = para_exec_cmdline_pid(&s->wpid, glob, fds);
free(glob);
if (ret < 0) {
PARA_ERROR_LOG("exec failed (%d)\n", ret);
int itemnum;
size_t ilen = 0;
struct timeval tv;
+ char *tmp;
if (!line)
return;
- ringbuffer_add(stat_item_ringbuf, line);
- stat_client_write(line);
itemnum = stat_line_valid(line);
- if (itemnum < 0)
+ if (itemnum < 0) {
+ PARA_WARNING_LOG("invalid status line: %s\n", line);
return;
+ }
+ tmp = make_message("%s\n", line);
+ stat_client_write(tmp, itemnum);
+ free(tmp);
+ free(stat_item_values[itemnum]);
+ stat_item_values[itemnum] = para_strdup(line);
ilen = strlen(status_item_list[itemnum]);
switch (itemnum) {
case SI_STATUS:
if (ret < 0)
goto out;
PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i], filters[j].name);
- ret = add_filter(i, "wav");
+ ret = add_filter(i, para_strdup("wav"));
if (ret < 0)
goto out;
PARA_INFO_LOG("%s -> default filter: wav\n", audio_formats[i]);
/*
* command handlers don't close their fd on errors (ret < 0) so that
* its caller can send an error message. Otherwise (ret >= 0) it's up
- * to each individual command to close the fd if necessary.
+ * to each individual command to close the fd if necessary.
*/
static int com_help(int fd, int argc, char **argv)
static int com_stat(int fd, __unused int argc, __unused char **argv)
{
int i, ret;
- char *buf = audiod_status_string();
+ char *buf = NULL;
+ long unsigned mask = ~0LU;
- buf = para_strcat(buf, "\n");
- for (i = RINGBUFFER_SIZE - 1; i >= 0; i--) {
- char *tmp, *line = ringbuffer_get(stat_item_ringbuf, i);
- if (!line)
- continue;
- tmp = make_message("%s\n", line);
- buf = para_strcat(buf, tmp);
+ if (argc > 1) {
+ mask = 0;
+ for (i = 1; i < argc; i++) {
+ ret = stat_item_valid(argv[i]);
+ if (ret < 0)
+ return ret;
+ mask |= (1 << ret);
+ }
+ }
+ PARA_INFO_LOG("mask: 0x%lx\n", mask);
+ if (mask & (1 << SI_PLAY_TIME)) {
+ struct timeval *t = wstime();
+ char *ts = get_time_string(t);
+ if (ts) {
+ ret = client_write(fd, ts);
+ if (ret < 0)
+ goto out;
+ free(ts);
+ }
+ }
+ if (mask & (1 << SI_AUDIOD_UPTIME)) {
+ char *tmp, *us = uptime_str();
+ tmp = make_message("%s:%s\n",
+ status_item_list[SI_AUDIOD_UPTIME], us);
+ free(us);
+ ret = client_write(fd, tmp);
+ if (ret < 0)
+ goto out;
free(tmp);
}
+ if (mask & (1 << SI_AUDIOD_STATUS)) {
+ char *s = audiod_status_string();
+ ret = client_write(fd, s);
+ if (ret < 0)
+ goto out;
+ free(s);
+ }
+ if (mask & (1 << SI_DECODER_FLAGS)) {
+ char *df =decoder_flags();
+ ret = client_write(fd, df);
+ if (ret < 0)
+ goto out;
+ free(df);
+ }
+
+ for (i = 0; i < NUM_STAT_ITEMS; i++) {
+ char *tmp, *v;
+ if (!((1 << i) & mask))
+ continue;
+ v = stat_item_values[i];
+ tmp = make_message("%s%s%s", buf? buf: "",
+ v? v : "", v? "\n" : "");
+ free(buf);
+ buf = tmp;
+ }
ret = client_write(fd, buf);
+out:
if (ret > 0)
- ret = stat_client_add(fd);
+ ret = stat_client_add(fd, mask);
free(buf);
return ret;
}
{
struct grab_client *gc;
struct filter_node *fn;
- int err;
+ int i, err;
+ char *msg;
PARA_INFO_LOG("argc: %d, argv[0]: %s, optind: %d\n", argc, argv[0], optind);
gc = grab_client_new(fd, argc, argv, &err);
activate_grab_client(gc, fn);
return 1;
err_out:
- if (err != -E_GC_HELP_GIVEN)
- return err;
- err = client_write(fd, "Usage: para_audioc [audioc_options] -- "
- "grab [grab_options]\nAvailable options:\n");
- if (err < 0)
+ if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
return err;
- err = client_write(fd, GRAB_HELP_TXT);
+ if (err == -E_GC_HELP_GIVEN) {
+ msg = make_message("%s\n\n", grab_client_args_info_usage);
+ for (i = 0; grab_client_args_info_help[i]; i++) {
+ char *tmp = make_message("%s%s\n", msg,
+ grab_client_args_info_help[i]);
+ free(msg);
+ msg = tmp;
+ }
+ } else
+ msg = make_message("%s %s\n",
+ GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
+ GRAB_CLIENT_CMDLINE_PARSER_VERSION);
+ err = client_write(fd, msg);
+ free(msg);
if (err < 0)
return err;
close(fd);
static int open_stat_pipe(void)
{
int ret, fd[3] = {-1, 1, 0};
- char *argv[] = {BINDIR "/para_client", "stat", NULL};
pid_t pid;
- ret = para_exec(&pid, BINDIR "/para_client", argv, fd);
+ ret = para_exec_cmdline_pid(&pid, BINDIR "/para_client stat", fd);
if (ret >= 0) {
ret = fd[1];
PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret);
return ret;
}
-static int pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
+static int audiod_pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
{
int i, ret, max = -1;
char status_buf[STRINGSIZE] = "";
struct timeval tv;
repeat:
- FD_ZERO(&rfds);
FD_ZERO(&wfds);
- max_fileno = 0;
+ FD_ZERO(&rfds);
+ /* always check signal pipe and the local socket */
+ FD_SET(signal_pipe, &rfds);
+ max_fileno = signal_pipe;
+ FD_SET(audiod_socket, &rfds);
+ max_fileno = MAX(max_fileno, audiod_socket);
+
if (audiod_status != AUDIOD_ON)
kill_all_decoders();
else if (playing)
start_current_receiver();
- max_fileno = set_stream_fds(&wfds);
- /* stat pipe (read) */
+
+ max_fileno = MAX(max_fileno, set_stream_fds(&wfds));
+ /* status pipe */
if (stat_pipe >= 0 && audiod_status == AUDIOD_OFF)
close_stat_pipe();
if (stat_pipe < 0 && audiod_status != AUDIOD_OFF) {
FD_SET(stat_pipe, &rfds);
max_fileno = MAX(max_fileno, stat_pipe);
}
- /* always check signal pipe */
- FD_SET(signal_pipe, &rfds);
- max_fileno = MAX(max_fileno, signal_pipe);
/* local socket */
- if (audiod_socket < 0)
- audiod_get_socket(); /* doesn't return on errors */
- FD_SET(audiod_socket, &rfds);
- max_fileno = MAX(max_fileno, audiod_socket);
tv.tv_sec = 0;
tv.tv_usec = 200 * 1000;
- ret = pre_select(&rfds, &wfds, &tv);
+ ret = audiod_pre_select(&rfds, &wfds, &tv);
max_fileno = MAX(max_fileno, ret);
+
ret = select(max_fileno + 1, &rfds, &wfds, NULL, &tv);
if (ret < 0 && errno != EINTR)
PARA_ERROR_LOG("select returned %d (%s)\n", ret,
set_initial_status();
FOR_EACH_SLOT(i)
clear_slot(i);
- stat_item_ringbuf = ringbuffer_new(RINGBUFFER_SIZE);
init_grabbing();
setup_signal_handling();
if (conf.daemon_given)
daemon_init();
+ audiod_get_socket(); /* doesn't return on errors */
audiod_mainloop();
}