Not yet finished.
Supporting btr required some changes in the buffer tree code
as well.
#include <dirent.h>
#include <alsa/asoundlib.h>
#include <sys/time.h>
+#include <stdbool.h>
#include "para.h"
#include "fd.h"
#include "write.h"
#include "alsa_write.cmdline.h"
#include "error.h"
+#include "buffer_tree.h"
/** always use 16 bit little endian */
#define FORMAT SND_PCM_FORMAT_S16_LE
}
/* Open an instance of the alsa writer. */
-static int alsa_open(struct writer_node *wn)
+static int alsa_open_nobtr(struct writer_node *wn)
{
struct alsa_write_args_info *conf = wn->conf;
struct writer_node_group *wng = wn->wng;
return 1;
}
+static int alsa_open_btr(struct writer_node *wn)
+{
+ struct alsa_write_args_info *conf = wn->conf;
+ struct private_alsa_write_data *pad = para_calloc(sizeof(*pad));
+ int ret;
+ char *buf = NULL;
+
+ sprintf(wn->task.status, "alsa writer");
+ wn->private_data = pad;
+
+ /* defaults */
+ pad->samplerate = conf->samplerate_arg;
+ pad->channels = conf->channels_arg;
+
+ if (!conf->samplerate_given) { /* config option trumps btr_exec */
+ /* ask parent btr nodes */
+ ret = btr_exec_up(wn->btrn, "samplerate", &buf);
+ if (ret >= 0) {
+ int32_t rate;
+ ret = para_atoi32(buf, &rate);
+ if (ret < 0) /* should not happen */
+ goto out;
+ pad->samplerate = rate;
+ }
+ freep(&buf);
+ }
+
+ if (!conf->channels_given) {
+ ret = btr_exec_up(wn->btrn, "channels", &buf);
+ if (ret >= 0) {
+ int32_t ch;
+ ret = para_atoi32(buf, &ch);
+ if (ret < 0)
+ goto out;
+ pad->channels = ch;
+ }
+ freep(&buf);
+ }
+ PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
+ ret = 1;
+out:
+ freep(&buf);
+ if (ret < 0)
+ free(pad);
+ return ret;
+}
+static int alsa_open(struct writer_node *wn)
+{
+ struct alsa_write_args_info *conf = wn->conf;
+
+ if (conf->buffer_tree_given)
+ return alsa_open_btr(wn);
+ else
+ return alsa_open_nobtr(wn);
+
+}
+
static int alsa_write_pre_select(struct sched *s, struct writer_node *wn)
{
+ struct alsa_write_args_info *conf = wn->conf;
struct private_alsa_write_data *pad = wn->private_data;
struct writer_node_group *wng = wn->wng;
struct timeval tv;
if (!pad->handle)
return 1;
- if (*wng->loaded - wn->written < pad->bytes_per_frame)
- return 1;
+ if (conf->buffer_tree_given) {
+ size_t sz = btr_get_input_queue_size(wn->btrn);
+ //PARA_CRIT_LOG("input queue: %zu\n", sz);
+ //if (sz < pad->bytes_per_frame)
+ // return 1;
+ } else {
+ if (*wng->loaded - wn->written < pad->bytes_per_frame)
+ return 1;
+ }
/*
* Data is available to be written to the alsa handle. Compute number
* of milliseconds until next buffer underrun would occur.
ms2tv(underrun, &tv);
if (tv_diff(&s->timeout, &tv, NULL) > 0)
s->timeout = tv;
+ //PARA_CRIT_LOG("timout: %lu\n", tv2ms(&s->timeout));
return 1;
}
+static void alsa_write_pre_select_btr(struct sched *s, struct task *t)
+{
+ struct writer_node *wn = container_of(t, struct writer_node, task);
+ t->error = alsa_write_pre_select(s, wn);
+}
+
static void xrun(snd_pcm_t *handle)
{
snd_pcm_status_t *status;
return -E_ALSA_WRITE;
}
+static void alsa_write_post_select_btr(__a_unused struct sched *s,
+ struct task *t)
+{
+ struct writer_node *wn = container_of(t, struct writer_node, task);
+ struct private_alsa_write_data *pad = wn->private_data;
+ char *data;
+ size_t bytes;
+ snd_pcm_sframes_t frames, avail;
+ int ret;
+
+again:
+ bytes = btr_next_buffer(wn->btrn, &data);
+ //PARA_CRIT_LOG("have: %zu\n", bytes);
+ t->error = 0;
+ ret = -E_ALSA_ORPHAN;
+ if (btr_no_parent(wn->btrn) && (!pad->handle || bytes < pad->bytes_per_frame))
+ goto err;
+ if (!pad->handle) {
+ if (bytes == 0) /* no data available */
+ return;
+ PARA_CRIT_LOG("alsa init\n");
+ ret = alsa_init(pad, wn->conf);
+ if (ret < 0)
+ goto err;
+ }
+ for (;;) {
+ if (bytes == 0)
+ return;
+ if (bytes >= pad->bytes_per_frame)
+ break;
+ /* should not be possible to reach this */
+ PARA_CRIT_LOG("dropping %zu byte buffer\n", bytes);
+ btr_consume(wn->btrn, bytes);
+ bytes = btr_next_buffer(wn->btrn, &data);
+ }
+ frames = bytes / pad->bytes_per_frame;
+ avail = snd_pcm_avail_update(pad->handle);
+ if (avail <= 0)
+ return;
+ frames = PARA_MIN(frames, avail);
+ //PARA_CRIT_LOG("writing %ld frames\n", frames);
+ frames = snd_pcm_writei(pad->handle, data, frames);
+ if (frames >= 0) {
+ btr_consume(wn->btrn, frames * pad->bytes_per_frame);
+ goto again;
+ }
+ if (frames == -EPIPE) {
+ xrun(pad->handle);
+ snd_pcm_prepare(pad->handle);
+ return;
+ }
+ PARA_WARNING_LOG("%s\n", snd_strerror(-frames));
+ if (frames == -EAGAIN)
+ return;
+ ret = -E_ALSA_WRITE;
+err:
+ assert(ret < 0);
+ btr_del_node(wn->btrn);
+ wn->btrn = NULL;
+ t->error = ret;
+}
+
static void alsa_close(struct writer_node *wn)
{
struct private_alsa_write_data *pad = wn->private_data;
w->open = alsa_open;
w->close = alsa_close;
w->pre_select = alsa_write_pre_select;
+ w->pre_select_btr = alsa_write_pre_select_btr;
w->post_select = alsa_write_post_select;
+ w->post_select_btr = alsa_write_post_select_btr;
w->parse_config = alsa_parse_config;
w->shutdown = NULL; /* nothing to do */
w->help = (struct ggo_help) {
#include "list.h"
#include "string.h"
#include "buffer_tree.h"
+#include "error.h"
struct btr_buffer {
* used by this btr node.
*/
struct list_head input_queue;
+ btr_command_handler execute;
};
#define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
#define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
-struct btr_node *btr_new_node(char *name, struct btr_node *parent)
+struct btr_node *btr_new_node(char *name, struct btr_node *parent,
+ btr_command_handler handler)
{
struct btr_node *btrn = para_malloc(sizeof(*btrn));
btrn->name = para_strdup(name);
btrn->parent = parent;
+ btrn->execute = handler;
if (parent)
list_add_tail(&btrn->node, &parent->children);
INIT_LIST_HEAD(&btrn->children);
{
struct btr_buffer *btrb = br->btrb;
+ //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
list_del(&br->node);
free(br);
btrb->refcount--;
struct btr_buffer_reference *br;
size_t size = 0;
- FOR_EACH_BUFFER_REF(br, btrn)
+ FOR_EACH_BUFFER_REF(br, btrn) {
+ //PARA_CRIT_LOG("size: %zu\n", size);
size += br_available_bytes(br);
+ }
return size;
}
}
return max_size;
}
+
+int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
+{
+ if (!btrn)
+ return -ERRNO_TO_PARA_ERROR(EINVAL);
+ if (!btrn->execute)
+ return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+ return btrn->execute(command, value_result);
+}
+
+int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
+{
+ int ret;
+
+ for (; btrn; btrn = btrn->parent) {
+ struct btr_node *parent = btrn->parent;
+ PARA_CRIT_LOG("parent: %p\n", parent);
+ if (!parent)
+ return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+ if (!parent->execute)
+ continue;
+ ret = parent->execute(command, value_result);
+ if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
+ continue;
+ if (ret < 0)
+ return ret;
+ }
+ return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+}
struct btr_node;
-struct btr_node *btr_new_node(char *name, struct btr_node *parent);
+typedef int (*btr_command_handler)(const char *command, char **result);
+
+struct btr_node *btr_new_node(char *name, struct btr_node *parent,
+ btr_command_handler handler);
void btr_del_node(struct btr_node *btrn);
void btr_add_output(char *buf, size_t size, struct btr_node *btrn);
bool btr_no_children(struct btr_node *btrn);
bool btr_no_parent(struct btr_node *btrn);
size_t btr_next_buffer(struct btr_node *btrn, char **bufp);
void btr_consume(struct btr_node *btrn, size_t numbytes);
+int btr_exec(struct btr_node *btrn, const char *command, char **value_result);
+int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result);
PARA_ERROR(SET_RATE, "snd_pcm_hw_params_set_rate_near failed"), \
PARA_ERROR(START_THRESHOLD, "snd_pcm_sw_params_set_start_threshold() failed"), \
PARA_ERROR(STOP_THRESHOLD, "snd_pcm_sw_params_set_stop_threshold() failed"), \
+ PARA_ERROR(ALSA_ORPHAN, "alsa btr node has no parent"), \
#define FILE_WRITE_ERRORS \
--- /dev/null
+<qu>
+option "buffer_tree" B
+#~~~~~~~~~~~~~~~~~~~~~
+"use the new buffer tree API"
+flag off
+details = "
+ Experimental. Don't use.
+"
+</qu>
phd->fd = fd;
phd->status = HTTP_CONNECTED;
if (conf->buffer_tree_given)
- rn->btrn = btr_new_node("receiver", NULL);
+ rn->btrn = btr_new_node("receiver", NULL, NULL);
return 1;
}
r_opened = 1;
if (conf.buffer_tree_given)
- sot.btrn = btr_new_node("stdout", rn.btrn);
+ sot.btrn = btr_new_node("stdout", rn.btrn, NULL);
stdout_set_defaults(&sot);
sot.bufp = &rn.buf;
para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
}
-#define STDIN_MAX_PENDING (1024 * 1024)
+#define STDIN_MAX_PENDING (100 * 1024)
static void stdin_pre_select_btr(struct sched *s, struct task *t)
{
buf = para_malloc(STDIN_INPUT_BUFFER_SIZE);
ret = read(STDIN_FILENO, buf, STDIN_INPUT_BUFFER_SIZE);
+ //PARA_CRIT_LOG("read ret: %d\n", ret);
if (ret < 0)
t->error = -ERRNO_TO_PARA_ERROR(errno);
if (ret == 0)
#include <regex.h>
#include <sys/types.h>
#include <dirent.h>
+#include <stdbool.h>
#include "para.h"
#include "string.h"
#include "write_common.h"
#include "fd.h"
#include "error.h"
+#include "buffer_tree.h"
INIT_WRITE_ERRLISTS;
exit(0);
}
+/*
+ TODO: check wav, initial delay, multiple writers, non-default writers
+ */
+static int main_btr(struct sched *s)
+{
+ struct writer_node *wn = para_malloc(sizeof(*wn));
+ struct writer *w = writers + DEFAULT_WRITER;
+ int ret;
+
+ wn->writer_num = DEFAULT_WRITER;
+ wn->conf = writers[DEFAULT_WRITER].parse_config("-B");
+ sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL);
+ stdin_set_defaults(&sit);
+ register_task(&sit.task);
+
+ wn->btrn = btr_new_node("writer", sit.btrn, NULL);
+
+ sprintf(wn->task.status, "some writer");
+ w->open(wn);
+ wn->task.post_select = w->post_select_btr;
+ wn->task.pre_select = w->pre_select_btr;
+ register_task(&wn->task);
+
+ s->default_timeout.tv_sec = 10;
+ s->default_timeout.tv_usec = 50000;
+ ret = schedule(s);
+ w->close(wn);
+ return ret;
+}
+
/**
* Para_write's main function.
*
if (conf.help_given || conf.detailed_help_given)
print_help_and_die();
+ if (conf.buffer_tree_given) {
+ ret = main_btr(&s);
+ goto out;
+ }
wng = check_args();
if (!wng)
goto out;
void *conf;
/** How much of the wng's buffer is already written. */
size_t written;
+ struct btr_node *btrn;
+ struct task task; /* move this to btr as btr nodes do not make sense w/o task */
};
/** Describes one supported writer. */
* on errors.
*/
int (*pre_select)(struct sched *s, struct writer_node *wn);
+ void (*pre_select_btr)(struct sched *s, struct task *t);
/**
* Write audio data.
*
* negative on errors.
*/
int (*post_select)(struct sched *s, struct writer_node *wn);
+ void (*post_select_btr)(struct sched *s, struct task *t);
/**
* Close one instance of the writer.
*