From 935826f4f318a89a0a570f766deb54808a4f9683 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 14 Sep 2004 23:08:39 +0000 Subject: make module-combine autoloadable clean up cli language introduce lazy sample cache git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@201 fefdeb5f-60dc-0310-8127-8f9354f1896f --- doc/todo | 3 -- polyp/autoload.c | 79 ++++++++++++++++++++++++++++++++++-------- polyp/autoload.h | 7 ++-- polyp/cli-command.c | 91 ++++++++++++++++++++++++++----------------------- polyp/cli-text.c | 21 ++++++++---- polyp/cmdline.c | 11 ++++-- polyp/conf.c | 7 ++-- polyp/conf.h | 1 + polyp/core.c | 12 +++---- polyp/core.h | 8 +++-- polyp/default.pa | 46 ++++++++++++------------- polyp/main.c | 6 ++-- polyp/module-combine.c | 11 +++++- polyp/module.c | 26 +++++++------- polyp/protocol-esound.c | 4 +-- polyp/protocol-native.c | 7 ++-- polyp/scache.c | 64 +++++++++++++++++++++++++++++++--- polyp/scache.h | 7 +++- 18 files changed, 280 insertions(+), 131 deletions(-) diff --git a/doc/todo b/doc/todo index 8daabc33..fedfa545 100644 --- a/doc/todo +++ b/doc/todo @@ -2,7 +2,6 @@ *** 0.5 *** - make mcalign merge chunks -- use ref counting in more objects (i.e. sink, source, sink_input, source_output) - native library/protocol: module load/unload kill client/... @@ -10,13 +9,11 @@ rename streams/contexts - more complete pactl - option to use default fragment size on alsa drivers -- lazy sample cache - merge pa_context_connect_* - input latency - fix tcp/native - add volume to create_stream command in native protocol - udp based protocol -- make sure not to allow recursive auto load *** 0.6 **** - per-channel volume diff --git a/polyp/autoload.c b/polyp/autoload.c index 988b0921..8eb5bbf5 100644 --- a/polyp/autoload.c +++ b/polyp/autoload.c @@ -30,36 +30,64 @@ #include "autoload.h" #include "module.h" #include "xmalloc.h" +#include "memchunk.h" +#include "sound-file.h" +#include "log.h" +#include "scache.h" static void entry_free(struct pa_autoload_entry *e) { assert(e); pa_xfree(e->name); pa_xfree(e->module); pa_xfree(e->argument); + pa_xfree(e->filename); pa_xfree(e); } -void pa_autoload_add(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*module, const char *argument) { +static struct pa_autoload_entry* entry_new(struct pa_core *c, const char *name) { struct pa_autoload_entry *e = NULL; - assert(c && name && module); + assert(c && name); - if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name))) { - pa_xfree(e->module); - pa_xfree(e->argument); - } else { - e = pa_xmalloc(sizeof(struct pa_autoload_entry)); - e->name = pa_xstrdup(name); + if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name))) + return NULL; + + e = pa_xmalloc(sizeof(struct pa_autoload_entry)); + e->name = pa_xstrdup(name); + e->module = e->argument = e->filename = NULL; + e->in_action = 0; + + if (!c->autoload_hashmap) + c->autoload_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + assert(c->autoload_hashmap); + + pa_hashmap_put(c->autoload_hashmap, e->name, e); - if (!c->autoload_hashmap) - c->autoload_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - assert(c->autoload_hashmap); + return e; +} - pa_hashmap_put(c->autoload_hashmap, e->name, e); - } +int pa_autoload_add_module(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*module, const char *argument) { + struct pa_autoload_entry *e = NULL; + assert(c && name && module); + if (!(e = entry_new(c, name))) + return -1; + e->module = pa_xstrdup(module); e->argument = pa_xstrdup(argument); e->type = type; + return 0; +} + +int pa_autoload_add_sample(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*filename) { + struct pa_autoload_entry *e = NULL; + assert(c && name && filename); + + if (!(e = entry_new(c, name))) + return -1; + + e->filename = pa_xstrdup(filename); + e->type = PA_NAMEREG_SAMPLE; + return 0; } int pa_autoload_remove(struct pa_core *c, const char*name, enum pa_namereg_type type) { @@ -82,8 +110,29 @@ void pa_autoload_request(struct pa_core *c, const char *name, enum pa_namereg_ty if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || (e->type != type)) return; - if ((m = pa_module_load(c, e->module, e->argument))) - m->auto_unload = 1; + if (e->in_action) + return; + + e->in_action = 1; + + if (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) { + if ((m = pa_module_load(c, e->module, e->argument))) + m->auto_unload = 1; + + } else { + struct pa_sample_spec ss; + struct pa_memchunk chunk; + assert(type == PA_NAMEREG_SAMPLE); + + if (pa_sound_file_load(e->filename, &ss, &chunk, c->memblock_stat) < 0) + pa_log(__FILE__": failed to load sound file '%s' for autoload entry '%s'.\n", e->filename, e->name); + else { + pa_scache_add_item(c, e->name, &ss, &chunk, NULL, 1); + pa_memblock_unref(chunk.memblock); + } + } + + e->in_action = 0; } static void free_func(void *p, void *userdata) { diff --git a/polyp/autoload.h b/polyp/autoload.h index 004dae26..3d3101b9 100644 --- a/polyp/autoload.h +++ b/polyp/autoload.h @@ -27,10 +27,13 @@ struct pa_autoload_entry { char *name; enum pa_namereg_type type; - char *module, *argument; + int in_action; + char *module, *argument; /* for module autoloading */ + char *filename; /* for sample autoloading */ }; -void pa_autoload_add(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*module, const char *argument); +int pa_autoload_add_module(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*module, const char *argument); +int pa_autoload_add_sample(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*filename); void pa_autoload_free(struct pa_core *c); int pa_autoload_remove(struct pa_core *c, const char*name, enum pa_namereg_type type); void pa_autoload_request(struct pa_core *c, const char *name, enum pa_namereg_type type); diff --git a/polyp/cli-command.c b/polyp/cli-command.c index b44fbf4a..574af9db 100644 --- a/polyp/cli-command.c +++ b/polyp/cli-command.c @@ -87,35 +87,37 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct static const struct command commands[] = { { "exit", pa_cli_command_exit, "Terminate the daemon", 1 }, { "help", pa_cli_command_help, "Show this help", 1 }, - { "modules", pa_cli_command_modules, "List loaded modules", 1 }, - { "sinks", pa_cli_command_sinks, "List loaded sinks", 1 }, - { "sources", pa_cli_command_sources, "List loaded sources", 1 }, - { "clients", pa_cli_command_clients, "List loaded clients", 1 }, - { "sink_inputs", pa_cli_command_sink_inputs, "List sink inputs", 1 }, - { "source_outputs", pa_cli_command_source_outputs, "List source outputs", 1 }, + { "list-modules", pa_cli_command_modules, "List loaded modules", 1 }, + { "list-sinks", pa_cli_command_sinks, "List loaded sinks", 1 }, + { "list-sources", pa_cli_command_sources, "List loaded sources", 1 }, + { "list-clients", pa_cli_command_clients, "List loaded clients", 1 }, + { "list-sink-inputs", pa_cli_command_sink_inputs, "List sink inputs", 1 }, + { "list-source-outputs", pa_cli_command_source_outputs, "List source outputs", 1 }, { "stat", pa_cli_command_stat, "Show memory block statistics", 1 }, { "info", pa_cli_command_info, "Show comprehensive status", 1 }, { "ls", pa_cli_command_info, NULL, 1 }, { "list", pa_cli_command_info, NULL, 1 }, - { "load", pa_cli_command_load, "Load a module (args: name, arguments)", 3}, - { "unload", pa_cli_command_unload, "Unload a module (args: index)", 2}, - { "sink_volume", pa_cli_command_sink_volume, "Set the volume of a sink (args: index|name, volume)", 3}, - { "sink_input_volume", pa_cli_command_sink_input_volume, "Set the volume of a sink input (args: index|name, volume)", 3}, - { "sink_default", pa_cli_command_sink_default, "Set the default sink (args: index|name)", 2}, - { "source_default", pa_cli_command_source_default, "Set the default source (args: index|name)", 2}, - { "kill_client", pa_cli_command_kill_client, "Kill a client (args: index)", 2}, - { "kill_sink_input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2}, - { "kill_source_output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2}, - { "scache_list", pa_cli_command_scache_list, "List all entries in the sample cache", 1}, - { "scache_play", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3}, - { "scache_remove", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2}, - { "scache_load", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: filename,name)", 3}, - { "play_file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3}, - { "autoload_list", pa_cli_command_autoload_list, "List autoload entries", 1}, - { "autoload_sink_add", pa_cli_command_autoload_add, "Add autoload entry for a sink (args: sink, name, arguments)", 4}, - { "autoload_source_add", pa_cli_command_autoload_add, "Add autoload entry for a source (args: source, name, arguments)", 4}, - { "autoload_sink_remove", pa_cli_command_autoload_remove, "Remove autoload entry for a sink (args: sink)", 2}, - { "autoload_source_remove", pa_cli_command_autoload_remove, "Remove autoload entry for a source (args: source)", 2}, + { "load-module", pa_cli_command_load, "Load a module (args: name, arguments)", 3}, + { "unload-module", pa_cli_command_unload, "Unload a module (args: index)", 2}, + { "set-sink-volume", pa_cli_command_sink_volume, "Set the volume of a sink (args: index|name, volume)", 3}, + { "set-sink-input-volume", pa_cli_command_sink_input_volume, "Set the volume of a sink input (args: index|name, volume)", 3}, + { "set-default-sink", pa_cli_command_sink_default, "Set the default sink (args: index|name)", 2}, + { "set-default-source", pa_cli_command_source_default, "Set the default source (args: index|name)", 2}, + { "kill-client", pa_cli_command_kill_client, "Kill a client (args: index)", 2}, + { "kill-sink-input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2}, + { "kill-source-output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2}, + { "list-samples", pa_cli_command_scache_list, "List all entries in the sample cache", 1}, + { "play-sample", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3}, + { "remove-sample", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2}, + { "load-sample", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: name, filename)", 3}, + { "play-file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3}, + { "list-autoload", pa_cli_command_autoload_list, "List autoload entries", 1}, + { "add-autoload-sink", pa_cli_command_autoload_add, "Add autoload entry for a sink (args: sink, module name, arguments)", 4}, + { "add-autoload-source", pa_cli_command_autoload_add, "Add autoload entry for a source (args: source, module name, arguments)", 4}, + { "add-autoload-sample", pa_cli_command_autoload_add, "Add autoload entry for a smple (args: name, filename)", 3}, + { "remove-autoload-sink", pa_cli_command_autoload_remove, "Remove autoload entry for a sink (args: name)", 2}, + { "remove-autoload-source", pa_cli_command_autoload_remove, "Remove autoload entry for a source (args: name)", 2}, + { "remove-autoload-sample", pa_cli_command_autoload_remove, "Remove autoload entry for a sample (args: name)", 2}, { "dump", pa_cli_command_dump, "Dump daemon configuration", 1}, { NULL, NULL, NULL, 0 } }; @@ -147,7 +149,7 @@ static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct for (command = commands; command->name; command++) if (command->help) - pa_strbuf_printf(buf, " %-20s %s\n", command->name, command->help); + pa_strbuf_printf(buf, " %-25s %s\n", command->name, command->help); return 0; } @@ -524,7 +526,7 @@ static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_sample_spec ss; assert(c && t && buf && fail && verbose); - if (!(fname = pa_tokenizer_get(t, 1)) || !(n = pa_tokenizer_get(t, 2))) { + if (!(fname = pa_tokenizer_get(t, 2)) || !(n = pa_tokenizer_get(t, 1))) { pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n"); return -1; } @@ -534,7 +536,7 @@ static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, return -1; } - pa_scache_add_item(c, n, &ss, &chunk, NULL); + pa_scache_add_item(c, n, &ss, &chunk, NULL, 0); pa_memblock_unref(chunk.memblock); return 0; } @@ -559,28 +561,33 @@ static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, s } static int pa_cli_command_autoload_add(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { - const char *devname, *module; + const char *a, *b; assert(c && t && buf && fail && verbose); - if (!(devname = pa_tokenizer_get(t, 1)) || !(module = pa_tokenizer_get(t, 2))) { - pa_strbuf_puts(buf, "You need to specify a device name, a module name and optionally module arguments\n"); + if (!(a = pa_tokenizer_get(t, 1)) || !(b = pa_tokenizer_get(t, 2))) { + pa_strbuf_puts(buf, "You need to specify a device name, a filename or a module name and optionally module arguments\n"); return -1; } + + if (strstr(pa_tokenizer_get(t, 0), "sample")) + pa_autoload_add_sample(c, a, PA_NAMEREG_SAMPLE, b); + else + pa_autoload_add_module(c, a, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, b, pa_tokenizer_get(t, 3)); - pa_autoload_add(c, devname, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, pa_tokenizer_get(t, 3)); return 0; } static int pa_cli_command_autoload_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { - const char *devname; + const char *name; assert(c && t && buf && fail && verbose); - - if (!(devname = pa_tokenizer_get(t, 1))) { + + if (!(name = pa_tokenizer_get(t, 1))) { pa_strbuf_puts(buf, "You need to specify a device name\n"); return -1; } - if (pa_autoload_remove(c, devname, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) { + if (pa_autoload_remove(c, name, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : + (strstr(pa_tokenizer_get(t, 0), "source") ? PA_NAMEREG_SOURCE : PA_NAMEREG_SAMPLE)) < 0) { pa_strbuf_puts(buf, "Failed to remove autload entry\n"); return -1; } @@ -620,7 +627,7 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct if (m->auto_unload) continue; - pa_strbuf_printf(buf, "load %s", m->name); + pa_strbuf_printf(buf, "load-module %s", m->name); if (m->argument) pa_strbuf_printf(buf, " %s", m->argument); @@ -642,7 +649,7 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct nl = 1; } - pa_strbuf_printf(buf, "sink_volume %s 0x%03x\n", s->name, s->volume); + pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", s->name, s->volume); } @@ -651,13 +658,13 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct i = NULL; while ((a = pa_hashmap_iterate(c->autoload_hashmap, &i))) { - + if (!nl) { pa_strbuf_puts(buf, "\n"); nl = 1; } - pa_strbuf_printf(buf, "autoload_%s_add %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module); + pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : (a->type == PA_NAMEREG_SOURCE ? "source" : "sample"), a->name, a->type == PA_NAMEREG_SAMPLE ? a->filename : a->module); if (a->argument) pa_strbuf_printf(buf, " %s", a->argument); @@ -673,7 +680,7 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf_puts(buf, "\n"); nl = 1; } - pa_strbuf_printf(buf, "sink_default %s\n", p); + pa_strbuf_printf(buf, "set-default-sink %s\n", p); } if ((p = pa_namereg_get_default_source_name(c))) { @@ -681,7 +688,7 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf_puts(buf, "\n"); nl = 1; } - pa_strbuf_printf(buf, "source_default %s\n", p); + pa_strbuf_printf(buf, "set-default-source %s\n", p); } pa_strbuf_puts(buf, "\n### EOF\n"); diff --git a/polyp/cli-text.c b/polyp/cli-text.c index 2a48d576..dbc14fda 100644 --- a/polyp/cli-text.c +++ b/polyp/cli-text.c @@ -228,13 +228,14 @@ char *pa_scache_list_to_string(struct pa_core *c) { l = (double) e->memchunk.length / pa_bytes_per_second(&e->sample_spec); pa_strbuf_printf( - s, " name: <%s>\n\tindex: <%i>\n\tsample_spec: <%s>\n\tlength: <%u>\n\tduration: <%0.1fs>\n\tvolume: <0x%04x>\n", + s, " name: <%s>\n\tindex: <%i>\n\tsample_spec: <%s>\n\tlength: <%u>\n\tduration: <%0.1fs>\n\tvolume: <0x%04x>\n\tauto unload: %s\n", e->name, e->index, ss, e->memchunk.length, l, - e->volume); + e->volume, + e->auto_unload ? "yes" : "no"); } } @@ -256,11 +257,19 @@ char *pa_autoload_list_to_string(struct pa_core *c) { while ((e = pa_hashmap_iterate(c->autoload_hashmap, &state))) { pa_strbuf_printf( - s, " name: <%s>\n\ttype: <%s>\n\tmodule_name: <%s>\n\targuments: <%s>\n", + s, " name: <%s>\n\ttype: <%s>\n", e->name, - e->type == PA_NAMEREG_SOURCE ? "source" : "sink", - e->module, - e->argument); + e->type == PA_NAMEREG_SOURCE ? "source" : (e->type == PA_NAMEREG_SINK ? "sink" : "sample")); + + if (e->type != PA_NAMEREG_SAMPLE) + pa_strbuf_printf( + s, "\tmodule_name: <%s>\n\targuments: <%s>\n", + e->module, + e->argument); + else + pa_strbuf_printf( + s, "\tfilename: <%s>\n", + e->filename); } } diff --git a/polyp/cmdline.c b/polyp/cmdline.c index 47685ca9..d429256f 100644 --- a/polyp/cmdline.c +++ b/polyp/cmdline.c @@ -47,6 +47,7 @@ enum { ARG_DISALLOW_MODULE_LOADING, ARG_EXIT_IDLE_TIME, ARG_MODULE_IDLE_TIME, + ARG_SCACHE_IDLE_TIME, ARG_LOG_TARGET, ARG_LOAD, ARG_FILE, @@ -65,6 +66,7 @@ static struct option long_options[] = { {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING}, {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME}, {"module-idle-time", 2, 0, ARG_MODULE_IDLE_TIME}, + {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME}, {"log-target", 1, 0, ARG_LOG_TARGET}, {"load", 1, 0, ARG_LOAD}, {"file", 1, 0, ARG_FILE}, @@ -94,6 +96,7 @@ void pa_cmdline_help(const char *argv0) { " --disallow-module-loading[=BOOL] Disallow module loading after startup\n" " --exit-idle-time=SECS Terminate the daemon when idle and this time passed\n" " --module-idle-time=SECS Unload autoloaded modules when idle and this time passed\n" + " --scache-idle-time=SECS Unload autoloaded samples when idle and this time passed\n" " --log-target={auto,syslog,stderr} Specify the log target\n" " -p, --dl-search-path=PATH Set the search path for dynamic shared objects (plugins)\n\n" @@ -135,7 +138,7 @@ int pa_cmdline_parse(struct pa_conf *conf, int argc, char *const argv [], int *d case ARG_LOAD: case 'L': - pa_strbuf_printf(buf, "load %s\n", optarg); + pa_strbuf_printf(buf, "load-module %s\n", optarg); break; case ARG_FILE: @@ -144,7 +147,7 @@ int pa_cmdline_parse(struct pa_conf *conf, int argc, char *const argv [], int *d break; case 'C': - pa_strbuf_puts(buf, "load module-cli\n"); + pa_strbuf_puts(buf, "load-module module-cli\n"); break; case ARG_DAEMONIZE: @@ -216,6 +219,10 @@ int pa_cmdline_parse(struct pa_conf *conf, int argc, char *const argv [], int *d case ARG_MODULE_IDLE_TIME: conf->module_idle_time = atoi(optarg); break; + + case ARG_SCACHE_IDLE_TIME: + conf->scache_idle_time = atoi(optarg); + break; default: goto fail; diff --git a/polyp/conf.c b/polyp/conf.c index 9f1f6ba2..3a894a9b 100644 --- a/polyp/conf.c +++ b/polyp/conf.c @@ -43,6 +43,7 @@ static const struct pa_conf default_conf = { .disallow_module_loading = 0, .exit_idle_time = -1, .module_idle_time = 20, + .scache_idle_time = 20, .auto_log_target = 1, .script_commands = NULL, .dl_search_path = NULL, @@ -143,6 +144,7 @@ static int next_assignment(struct pa_conf *c, char *lvalue, char *rvalue, unsign PARSE_INTEGER("exit-idle-time", exit_idle_time); PARSE_INTEGER("module-idle-time", module_idle_time); + PARSE_INTEGER("scache-idle-time", scache_idle_time); PARSE_STRING("dl-search-path", dl_search_path); PARSE_STRING("default-script-file", default_script_file); @@ -258,7 +260,7 @@ char *pa_conf_dump(struct pa_conf *c) { char *d; d = default_file(ENV_CONFIG_FILE, DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_LOCAL); - pa_strbuf_printf(s, "### Default configuration file: %s ###\n\n", d); + pa_strbuf_printf(s, "### Default configuration file: %s ###\n", d); pa_strbuf_printf(s, "verbose = %i\n", !!c->verbose); pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize); @@ -267,11 +269,10 @@ char *pa_conf_dump(struct pa_conf *c) { pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading); pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time); pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time); + pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time); pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : ""); pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file); pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr")); - - pa_strbuf_printf(s, "\n### EOF ###\n"); pa_xfree(d); diff --git a/polyp/conf.h b/polyp/conf.h index da61be33..dafb3797 100644 --- a/polyp/conf.h +++ b/polyp/conf.h @@ -41,6 +41,7 @@ struct pa_conf { disallow_module_loading, exit_idle_time, module_idle_time, + scache_idle_time, auto_log_target; char *script_commands, *dl_search_path, *default_script_file; enum pa_log_target log_target; diff --git a/polyp/core.c b/polyp/core.c index 0b0bbb56..80abe9fe 100644 --- a/polyp/core.c +++ b/polyp/core.c @@ -61,8 +61,9 @@ struct pa_core* pa_core_new(struct pa_mainloop_api *m) { c->default_sample_spec.rate = 44100; c->default_sample_spec.channels = 2; - c->auto_unload_event = NULL; - c->defer_unload_event = NULL; + c->module_auto_unload_event = NULL; + c->module_defer_unload_event = NULL; + c->scache_auto_unload_event = NULL; c->subscription_defer_event = NULL; c->subscription_event_queue = NULL; @@ -76,6 +77,7 @@ struct pa_core* pa_core_new(struct pa_mainloop_api *m) { c->exit_idle_time = -1; c->module_idle_time = 20; + c->scache_idle_time = 20; pa_check_for_sigpipe(); @@ -108,11 +110,9 @@ void pa_core_free(struct pa_core *c) { pa_autoload_free(c); pa_subscription_free_all(c); - if (c->quit_event) { + if (c->quit_event) c->mainloop->time_free(c->quit_event); - c->quit_event = NULL; - } - + pa_xfree(c->default_source_name); pa_xfree(c->default_sink_name); diff --git a/polyp/core.h b/polyp/core.h index b4470577..980888f6 100644 --- a/polyp/core.h +++ b/polyp/core.h @@ -38,8 +38,8 @@ struct pa_core { char *default_source_name, *default_sink_name; struct pa_sample_spec default_sample_spec; - struct pa_time_event *auto_unload_event; - struct pa_defer_event *defer_unload_event; + struct pa_time_event *module_auto_unload_event; + struct pa_defer_event *module_defer_unload_event; struct pa_defer_event *subscription_defer_event; struct pa_queue *subscription_event_queue; @@ -48,9 +48,11 @@ struct pa_core { struct pa_memblock_stat *memblock_stat; int disallow_module_loading; - int exit_idle_time, module_idle_time; + int exit_idle_time, module_idle_time, scache_idle_time; struct pa_time_event *quit_event; + + struct pa_time_event *scache_auto_unload_event; }; struct pa_core* pa_core_new(struct pa_mainloop_api *m); diff --git a/polyp/default.pa b/polyp/default.pa index 15434627..ae1fbfcb 100755 --- a/polyp/default.pa +++ b/polyp/default.pa @@ -20,43 +20,43 @@ # Load audio drivers statically -#load module-alsa-sink -load module-alsa-source device=plughw:1,0 -load module-oss device="/dev/dsp" sink_name=output source_name=input record=0 -#load module-oss-mmap device="/dev/dsp" sink_name=output source_name=input -#load module-pipe-sink +#load-module module-alsa-sink +load-module module-alsa-source device=plughw:1,0 +load-module module-oss device="/dev/dsp" sink_name=output source_name=input record=0 +#load-module module-oss-mmap device="/dev/dsp" sink_name=output source_name=input +#load-module module-pipe-sink # Load audio drivers automatically on access -#autoload_sink_add output module-oss device="/dev/dsp" sink_name=output source_name=input -#autoload_source_add input module-oss device="/dev/dsp" sink_name=output source_name=input -#autoload_sink_add output module-oss-mmap device="/dev/dsp" sink_name=output source_name=input -#autoload_source_add input module-oss-mmap device="/dev/dsp" sink_name=output source_name=input -#autoload_sink_add output module-alsa-sink sink_name=output -#autoload_source_add input module-alsa-source source_name=input +#add-autoload_sink output module-oss device="/dev/dsp" sink_name=output source_name=input +#add-autoload_source input module-oss device="/dev/dsp" sink_name=output source_name=input +#add-autoload_sink output module-oss-mmap device="/dev/dsp" sink_name=output source_name=input +#add-autoload_source input module-oss-mmap device="/dev/dsp" sink_name=output source_name=input +#add-autoload_sink output module-alsa-sink sink_name=output +#add-autoload_source input module-alsa-source source_name=input # Load several protocols -load module-esound-protocol-tcp -#load module-simple-protocol-tcp -load module-native-protocol-unix -#load module-cli-protocol-unix -#load module-esound-protocol-unix +load-module module-esound-protocol-tcp +#load-module module-simple-protocol-tcp +load-module module-native-protocol-unix +#load-module module-cli-protocol-unix +#load-module module-esound-protocol-unix # Load the CLI module -load module-cli +load-module module-cli # Make some devices default -sink_default output -source_default input +set-default-sink output +set-default-source input .nofail # Load something to the sample cache -scache_load /usr/share/sounds/KDE_Notify.wav x11-bell +load-sample /usr/share/sounds/KDE_Notify.wav x11-bell # Load X11 bell module -load module-x11-bell sample=x11-bell sink=output +load-module module-x11-bell sample=x11-bell sink=output -#load module-pipe-source -#load module-pipe-sink +#load-module module-pipe-source +#load-module module-pipe-sink diff --git a/polyp/main.c b/polyp/main.c index 9de69ca6..04bcceef 100644 --- a/polyp/main.c +++ b/polyp/main.c @@ -234,8 +234,10 @@ int main(int argc, char *argv[]) { buf = pa_strbuf_new(); assert(buf); if (conf->default_script_file) - pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail, &conf->verbose); - r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail, &conf->verbose); + r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail, &conf->verbose); + + if (r >= 0) + r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail, &conf->verbose); pa_log(s = pa_strbuf_tostring_free(buf)); pa_xfree(s); diff --git a/polyp/module-combine.c b/polyp/module-combine.c index 28e3b39d..49a6951e 100644 --- a/polyp/module-combine.c +++ b/polyp/module-combine.c @@ -42,7 +42,7 @@ PA_MODULE_DESCRIPTION("Combine multiple sinks to one") PA_MODULE_VERSION(PACKAGE_VERSION) PA_MODULE_USAGE("sink_name= master= slave=") -#define DEFAULT_SINK_NAME "combine" +#define DEFAULT_SINK_NAME "combined" #define MEMBLOCKQ_MAXLENGTH (1024*170) #define RENDER_SIZE (1024*10) @@ -78,6 +78,13 @@ struct userdata { static void output_free(struct output *o); static void clear_up(struct userdata *u); +static void update_usage(struct userdata *u) { + pa_module_set_used(u->module, + (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) + + (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0)); +} + + static void adjust_rates(struct userdata *u) { struct output *o; pa_usec_t max = 0; @@ -120,6 +127,8 @@ static void request_memblock(struct userdata *u) { struct output *o; assert(u && u->sink); + update_usage(u); + if (pa_sink_render(u->sink, RENDER_SIZE, &chunk) < 0) return; diff --git a/polyp/module.c b/polyp/module.c index 73f5468c..3fb42d87 100644 --- a/polyp/module.c +++ b/polyp/module.c @@ -43,7 +43,7 @@ static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) { struct pa_core *c = userdata; struct timeval ntv; - assert(c && c->mainloop == m && c->auto_unload_event == e); + assert(c && c->mainloop == m && c->module_auto_unload_event == e); pa_module_unload_unused(c); @@ -96,13 +96,13 @@ struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char if (!c->modules) c->modules = pa_idxset_new(NULL, NULL); - if (!c->auto_unload_event) { + if (!c->module_auto_unload_event) { struct timeval ntv; gettimeofday(&ntv, NULL); ntv.tv_sec += UNLOAD_POLL_TIME; - c->auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c); + c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c); } - assert(c->auto_unload_event); + assert(c->module_auto_unload_event); assert(c->modules); r = pa_idxset_put(c->modules, m, &m->index); @@ -184,14 +184,14 @@ void pa_module_unload_all(struct pa_core *c) { pa_idxset_free(c->modules, free_callback, NULL); c->modules = NULL; - if (c->auto_unload_event) { - c->mainloop->time_free(c->auto_unload_event); - c->auto_unload_event = NULL; + if (c->module_auto_unload_event) { + c->mainloop->time_free(c->module_auto_unload_event); + c->module_auto_unload_event = NULL; } - if (c->defer_unload_event) { - c->mainloop->defer_free(c->defer_unload_event); - c->defer_unload_event = NULL; + if (c->module_defer_unload_event) { + c->mainloop->defer_free(c->module_defer_unload_event); + c->module_defer_unload_event = NULL; } } @@ -247,10 +247,10 @@ void pa_module_unload_request(struct pa_module *m) { m->unload_requested = 1; - if (!m->core->defer_unload_event) - m->core->defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core); + if (!m->core->module_defer_unload_event) + m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core); - m->core->mainloop->defer_enable(m->core->defer_unload_event, 1); + m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1); } void pa_module_set_used(struct pa_module*m, int used) { diff --git a/polyp/protocol-esound.c b/polyp/protocol-esound.c index 8ec48a34..6944a111 100644 --- a/polyp/protocol-esound.c +++ b/polyp/protocol-esound.c @@ -590,7 +590,7 @@ static int esd_proto_sample_cache(struct connection *c, esd_proto_t request, con c->state = ESD_CACHING_SAMPLE; - pa_scache_add_item(c->protocol->core, c->scache_name, NULL, NULL, &index); + pa_scache_add_item(c->protocol->core, c->scache_name, NULL, NULL, &index, 0); ok = connection_write(c, sizeof(int)); assert(ok); @@ -748,7 +748,7 @@ static int do_read(struct connection *c) { int *ok; c->scache_memchunk.index = 0; - pa_scache_add_item(c->protocol->core, c->scache_name, &c->scache_sample_spec, &c->scache_memchunk, &index); + pa_scache_add_item(c->protocol->core, c->scache_name, &c->scache_sample_spec, &c->scache_memchunk, &index, 0); pa_memblock_unref(c->scache_memchunk.memblock); c->scache_memchunk.memblock = NULL; diff --git a/polyp/protocol-native.c b/polyp/protocol-native.c index 29821b6c..e132d237 100644 --- a/polyp/protocol-native.c +++ b/polyp/protocol-native.c @@ -921,7 +921,7 @@ static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t comma return; } - pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index); + pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index, 0); pa_pstream_send_simple_ack(c->pstream, tag); upload_stream_free(s); } @@ -1514,7 +1514,8 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo assert(io && p); c = pa_xmalloc(sizeof(struct connection)); - c->authorized = p->public; + + c->authorized =!! p->public; c->protocol = p; assert(p->core); c->client = pa_client_new(p->core, "NATIVE", "Client"); @@ -1548,7 +1549,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo static struct pa_protocol_native* protocol_new_internal(struct pa_core *c, struct pa_module *m, struct pa_modargs *ma) { struct pa_protocol_native *p; - int public; + int public = 0; assert(c && ma); if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) { diff --git a/polyp/scache.c b/polyp/scache.c index 53e8a3fe..5725e128 100644 --- a/polyp/scache.c +++ b/polyp/scache.c @@ -37,6 +37,20 @@ #include "subscribe.h" #include "namereg.h" +#define UNLOAD_POLL_TIME 2 + +static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) { + struct pa_core *c = userdata; + struct timeval ntv; + assert(c && c->mainloop == m && c->scache_auto_unload_event == e); + + pa_scache_unload_unused(c); + + gettimeofday(&ntv, NULL); + ntv.tv_sec += UNLOAD_POLL_TIME; + m->time_restart(e, &ntv); +} + static void free_entry(struct pa_scache_entry *e) { assert(e); pa_namereg_unregister(e->core, e->name); @@ -47,7 +61,7 @@ static void free_entry(struct pa_scache_entry *e) { pa_xfree(e); } -int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) { +int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index, int auto_unload) { struct pa_scache_entry *e; int put; assert(c && name); @@ -72,6 +86,8 @@ int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spe } e->volume = PA_VOLUME_NORM; + e->auto_unload = auto_unload; + e->last_used_time = 0; if (ss) e->sample_spec = *ss; @@ -101,6 +117,13 @@ int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spe if (index) *index = e->index; + if (!c->scache_auto_unload_event) { + struct timeval ntv; + gettimeofday(&ntv, NULL); + ntv.tv_sec += UNLOAD_POLL_TIME; + c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c); + } + return 0; } @@ -131,13 +154,16 @@ void pa_scache_free(struct pa_core *c) { pa_idxset_free(c->scache, free_cb, NULL); c->scache = NULL; } + + if (c->scache_auto_unload_event) + c->mainloop->time_free(c->scache_auto_unload_event); } int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) { struct pa_scache_entry *e; assert(c && name && sink); - if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) + if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1))) return -1; if (!e->memchunk.memblock) @@ -145,6 +171,9 @@ int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sin if (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0) return -1; + + if (e->auto_unload) + time(&e->last_used_time); return 0; } @@ -163,7 +192,7 @@ uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) { struct pa_scache_entry *e; assert(c && name); - if (!c->scache || !(e = pa_idxset_get_by_data(c->scache, name, NULL))) + if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1))) return PA_IDXSET_INVALID; return e->index; @@ -180,6 +209,33 @@ uint32_t pa_scache_total_size(struct pa_core *c) { for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index)) sum += e->memchunk.length; - return sum; } + +static int unload_func(void *p, uint32_t index, int *del, void *userdata) { + struct pa_scache_entry *e = p; + time_t *now = userdata; + assert(e); + + if (!e->auto_unload) + return 0; + + if (e->last_used_time + e->core->scache_idle_time > *now) + return 0; + + free_entry(e); + *del = 1; + return 0; +} + +void pa_scache_unload_unused(struct pa_core *c) { + time_t now; + assert(c); + + if (!c->scache) + return; + + time(&now); + + pa_idxset_foreach(c->scache, unload_func, &now); +} diff --git a/polyp/scache.h b/polyp/scache.h index cfd479b5..e0574409 100644 --- a/polyp/scache.h +++ b/polyp/scache.h @@ -33,9 +33,12 @@ struct pa_scache_entry { uint32_t volume; struct pa_sample_spec sample_spec; struct pa_memchunk memchunk; + + int auto_unload; + time_t last_used_time; }; -int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index); +int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index, int auto_unload); int pa_scache_remove_item(struct pa_core *c, const char *name); int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume); @@ -46,4 +49,6 @@ uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name); uint32_t pa_scache_total_size(struct pa_core *c); +void pa_scache_unload_unused(struct pa_core *c); + #endif -- cgit