remove auto-load-sample stuff
authorLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2004 14:05:28 +0000 (14:05 +0000)
committerLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2004 14:05:28 +0000 (14:05 +0000)
introduce "lazy samples"

git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@203 fefdeb5f-60dc-0310-8127-8f9354f1896f

doc/todo
polyp/autoload.c
polyp/autoload.h
polyp/cli-command.c
polyp/cli-text.c
polyp/module-combine.c
polyp/namereg.h
polyp/protocol-esound.c
polyp/protocol-native.c
polyp/scache.c
polyp/scache.h

index fedfa54..91a2f92 100644 (file)
--- a/doc/todo
+++ b/doc/todo
@@ -14,6 +14,8 @@
 - fix tcp/native
 - add volume to create_stream command in native protocol
 - udp based protocol
+- add client config file
+- beefup sample_info stuff in native protocol
 
 *** 0.6 ****
 - per-channel volume
index 8eb5bbf..344d26e 100644 (file)
@@ -40,7 +40,6 @@ static void entry_free(struct pa_autoload_entry *e) {
     pa_xfree(e->name);
     pa_xfree(e->module);
     pa_xfree(e->argument);
-    pa_xfree(e->filename);
     pa_xfree(e);
 }
 
@@ -53,7 +52,7 @@ static struct pa_autoload_entry* entry_new(struct pa_core *c, const char *name)
     
     e = pa_xmalloc(sizeof(struct pa_autoload_entry));
     e->name = pa_xstrdup(name);
-    e->module = e->argument = e->filename = NULL;
+    e->module = e->argument = NULL;
     e->in_action = 0;
     
     if (!c->autoload_hashmap)
@@ -65,10 +64,10 @@ static struct pa_autoload_entry* entry_new(struct pa_core *c, const char *name)
     return e;
 }
 
-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(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);
-
+    assert(c && name && module && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
+    
     if (!(e = entry_new(c, name)))
         return -1;
         
@@ -78,18 +77,6 @@ int pa_autoload_add_module(struct pa_core *c, const char*name, enum pa_namereg_t
     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) {
     struct pa_autoload_entry *e;
     assert(c && name && type);
@@ -118,20 +105,8 @@ void pa_autoload_request(struct pa_core *c, const char *name, enum pa_namereg_ty
     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;
 }
 
index 3d3101b..cdf7623 100644 (file)
@@ -28,12 +28,10 @@ struct pa_autoload_entry {
     char *name;
     enum pa_namereg_type type;
     int in_action;
-    char *module, *argument;   /* for module autoloading */
-    char *filename;            /* for sample autoloading */
+    char *module, *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);
+int pa_autoload_add(struct pa_core *c, const char*name, enum pa_namereg_type type, const char*module, const char *argument);
 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);
index 574af9d..750d67e 100644 (file)
@@ -110,14 +110,13 @@ static const struct command commands[] = {
     { "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},
+    { "load-sample-lazy",        pa_cli_command_scache_load,        "Lazy 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 }
 };
@@ -522,8 +521,7 @@ static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *
 
 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
     const char *fname, *n;
-    struct pa_memchunk chunk;
-    struct pa_sample_spec ss;
+    int r;
     assert(c && t && buf && fail && verbose);
 
     if (!(fname = pa_tokenizer_get(t, 2)) || !(n = pa_tokenizer_get(t, 1))) {
@@ -531,13 +529,14 @@ static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t,
         return -1;
     }
 
-    if (pa_sound_file_load(fname, &ss, &chunk, c->memblock_stat) < 0) {
+    if (strstr(pa_tokenizer_get(t, 0), "lazy"))
+        r = pa_scache_add_file_lazy(c, n, fname, NULL);
+    else
+        r = pa_scache_add_file(c, n, fname, NULL);
+
+    if (r < 0)
         pa_strbuf_puts(buf, "Failed to load sound file.\n");
-        return -1;
-    }
 
-    pa_scache_add_item(c, n, &ss, &chunk, NULL, 0);
-    pa_memblock_unref(chunk.memblock);
     return 0;
 }
 
@@ -569,10 +568,7 @@ static int pa_cli_command_autoload_add(struct pa_core *c, struct pa_tokenizer *t
         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, a, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, b, pa_tokenizer_get(t, 3));
     
     return 0;
 }
@@ -586,8 +582,7 @@ static int pa_cli_command_autoload_remove(struct pa_core *c, struct pa_tokenizer
         return -1;
     }
 
-    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) {
+    if (pa_autoload_remove(c, name, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
         pa_strbuf_puts(buf, "Failed to remove autload entry\n");
         return -1;
     }
@@ -664,7 +659,7 @@ static int pa_cli_command_dump(struct pa_core *c, struct pa_tokenizer *t, struct
                 nl = 1;
             }
             
-            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);
+            pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module);
             
             if (a->argument)
                 pa_strbuf_printf(buf, " %s", a->argument);
index dbc14fd..0915be8 100644 (file)
@@ -221,21 +221,24 @@ char *pa_scache_list_to_string(struct pa_core *c) {
         uint32_t index = PA_IDXSET_INVALID;
 
         for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index)) {
-            double l;
-            char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
-            pa_sample_spec_snprint(ss, sizeof(ss), &e->sample_spec);
+            double l = 0;
+            char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH] = "n/a";
             
-            l = (double) e->memchunk.length / pa_bytes_per_second(&e->sample_spec);
+            if (e->memchunk.memblock) {
+                pa_sample_spec_snprint(ss, sizeof(ss), &e->sample_spec);
+                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\tauto unload: %s\n",
+                s, "    name: <%s>\n\tindex: <%i>\n\tsample_spec: <%s>\n\tlength: <%u>\n\tduration: <%0.1fs>\n\tvolume: <0x%04x>\n\tlazy: %s\n\tfilename: %s\n",
                 e->name,
                 e->index,
                 ss,
-                e->memchunk.length,
+                e->memchunk.memblock ? e->memchunk.length : 0,
                 l,
                 e->volume,
-                e->auto_unload ? "yes" : "no");
+                e->lazy ? "yes" : "no",
+                e->filename ? e->filename : "n/a");
         }
     }
 
@@ -257,19 +260,12 @@ 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",
+                s, "    name: <%s>\n\ttype: <%s>\n\tmodule_name: <%s>\n\targuments: <%s>\n",
                 e->name,
-                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);
+                e->type == PA_NAMEREG_SOURCE ? "source" : "sink",
+                e->module,
+                e->argument);
+
         }
     }
 
index 49a6951..8535e51 100644 (file)
 PA_MODULE_AUTHOR("Lennart Poettering")
 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
 PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slave=<slave sinks>")
+PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slave=<slave sinks> adjust_time")
 
 #define DEFAULT_SINK_NAME "combined"
 #define MEMBLOCKQ_MAXLENGTH (1024*170)
 #define RENDER_SIZE (1024*10)
 
-#define ADJUST_TIME 5
+#define DEFAULT_ADJUST_TIME 20
 
 static const char* const valid_modargs[] = {
     "sink_name",
     "master",
     "slaves",
+    "adjust_time",
     NULL
 };
 
@@ -71,6 +72,7 @@ struct userdata {
     unsigned n_outputs;
     struct output *master;
     struct pa_time_event *time_event;
+    uint32_t adjust_time;
     
     PA_LLIST_HEAD(struct output, outputs);
 };
@@ -109,9 +111,9 @@ static void adjust_rates(struct userdata *u) {
         l = o->sink_latency + pa_sink_input_get_latency(o->sink_input);
 
         if (l < max)
-            r -= (uint32_t) (((((double) max-l))/ADJUST_TIME)*r/ 1000000);
+            r -= (uint32_t) (((((double) max-l))/u->adjust_time)*r/ 1000000);
         else if (l > max)
-            r += (uint32_t) (((((double) l-max))/ADJUST_TIME)*r/ 1000000);
+            r += (uint32_t) (((((double) l-max))/u->adjust_time)*r/ 1000000);
 
         if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1))
             pa_log(__FILE__": [%s] sample rates too different, not adjusting (%u vs. %u).\n", o->sink_input->name, base_rate, r);
@@ -146,7 +148,7 @@ static void time_callback(struct pa_mainloop_api*a, struct pa_time_event* e, con
     adjust_rates(u);
 
     gettimeofday(&n, NULL);
-    n.tv_sec += ADJUST_TIME;
+    n.tv_sec += u->adjust_time;
     u->sink->core->mainloop->time_restart(e, &n);
 }
 
@@ -289,8 +291,14 @@ int pa__init(struct pa_core *c, struct pa_module*m) {
     u->module = m;
     u->core = c;
     u->time_event = NULL;
+    u->adjust_time = DEFAULT_ADJUST_TIME;
     PA_LLIST_HEAD_INIT(struct output, u->outputs);
 
+    if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
+        pa_log(__FILE__": failed to parse adjust_time value\n");
+        goto fail;
+    }
+    
     if (!(master_name = pa_modargs_get_value(ma, "master", NULL)) || !(slaves = pa_modargs_get_value(ma, "slaves", NULL))) {
         pa_log(__FILE__": no master or slave sinks specified\n");
         goto fail;
@@ -336,9 +344,11 @@ int pa__init(struct pa_core *c, struct pa_module*m) {
     if (u->n_outputs <= 1)
         pa_log(__FILE__": WARNING: no slave sinks specified.\n");
 
-    gettimeofday(&tv, NULL);
-    tv.tv_sec += ADJUST_TIME;
-    u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
+    if (u->adjust_time > 0) {
+        gettimeofday(&tv, NULL);
+        tv.tv_sec += u->adjust_time;
+        u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
+    }
     
     pa_modargs_free(ma);
     return 0;    
index f1be395..b383d9e 100644 (file)
@@ -37,9 +37,7 @@ void pa_namereg_unregister(struct pa_core *c, const char *name);
 void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type, int autoload);
 void pa_namereg_set_default(struct pa_core*c, const char *name, enum pa_namereg_type type);
 
-
 const char *pa_namereg_get_default_sink_name(struct pa_core *c);
 const char *pa_namereg_get_default_source_name(struct pa_core *c);
 
-
 #endif
index 6944a11..8ec48a3 100644 (file)
@@ -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, 0);
+    pa_scache_add_item(c->protocol->core, c->scache_name, NULL, NULL, &index);
 
     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, 0);
+            pa_scache_add_item(c->protocol->core, c->scache_name, &c->scache_sample_spec, &c->scache_memchunk, &index);
 
             pa_memblock_unref(c->scache_memchunk.memblock);
             c->scache_memchunk.memblock = NULL;
index e132d23..55b98fd 100644 (file)
@@ -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, 0);
+    pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
     pa_pstream_send_simple_ack(c->pstream, tag);
     upload_stream_free(s);
 }
index 5725e12..026f4b1 100644 (file)
@@ -36,6 +36,7 @@
 #include "xmalloc.h"
 #include "subscribe.h"
 #include "namereg.h"
+#include "sound-file.h"
 
 #define UNLOAD_POLL_TIME 2
 
@@ -56,74 +57,110 @@ static void free_entry(struct pa_scache_entry *e) {
     pa_namereg_unregister(e->core, e->name);
     pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
     pa_xfree(e->name);
+    pa_xfree(e->filename);
     if (e->memchunk.memblock)
         pa_memblock_unref(e->memchunk.memblock);
     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 auto_unload) {
+static struct pa_scache_entry* scache_add_item(struct pa_core *c, const char *name) {
     struct pa_scache_entry *e;
-    int put;
     assert(c && name);
 
     if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
-        put = 0;
         if (e->memchunk.memblock)
             pa_memblock_unref(e->memchunk.memblock);
+
+        pa_xfree(e->filename);
+        
         assert(e->core == c);
-    } else {
 
-        put = 1;
+        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
+    } else {
         e = pa_xmalloc(sizeof(struct pa_scache_entry));
 
         if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
             pa_xfree(e);
-            return -1;
+            return NULL;
         }
-        
+
         e->name = pa_xstrdup(name);
         e->core = c;
+
+        if (!c->scache) {
+            c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+            assert(c->scache);
+        }
+
+        pa_idxset_put(c->scache, e, &e->index);
+
+        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
     }
 
     e->volume = PA_VOLUME_NORM;
-    e->auto_unload = auto_unload;
     e->last_used_time = 0;
+    e->memchunk.memblock = NULL;
+    e->memchunk.index = e->memchunk.length = 0;
+    e->filename = NULL;
+    e->lazy = 0;
+    e->last_used_time = 0;
+
+    memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
+
+    return 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) {
+    struct pa_scache_entry *e;
+    assert(c && name);
+
+    if (!(e = scache_add_item(c, name)))
+        return -1;
 
     if (ss)
         e->sample_spec = *ss;
-    else
-        memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
 
     if (chunk) {
         e->memchunk = *chunk;
         pa_memblock_ref(e->memchunk.memblock);
-    } else {
-        e->memchunk.memblock = NULL;
-        e->memchunk.index = e->memchunk.length = 0;
     }
 
-    if (put) {
-        if (!c->scache) {
-            c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
-            assert(c->scache);
-        }
-        
-        pa_idxset_put(c->scache, e, &e->index);
-
-        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
-    } else
-        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
-        
     if (index)
         *index = e->index;
 
+    return 0;
+}
+
+int pa_scache_add_file(struct pa_core *c, const char *name, const char *filename, uint32_t *index) {
+    struct pa_sample_spec ss;
+    struct pa_memchunk chunk;
+    int r;
+
+    if (pa_sound_file_load(filename, &ss, &chunk, c->memblock_stat) < 0)
+        return -1;
+        
+    r = pa_scache_add_item(c, name, &ss, &chunk, index);
+    pa_memblock_unref(chunk.memblock);
+    return r;
+}
+
+int pa_scache_add_file_lazy(struct pa_core *c, const char *name, const char *filename, uint32_t *index) {
+    struct pa_scache_entry *e;
+    assert(c && name);
+
+    if (!(e = scache_add_item(c, name)))
+        return -1;
+
+    e->lazy = 1;
+    e->filename = pa_xstrdup(filename);
+    
     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;
 }
 
@@ -166,13 +203,17 @@ int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sin
     if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
         return -1;
 
+    if (e->lazy && !e->memchunk.memblock)
+        if (pa_sound_file_load(e->filename, &e->sample_spec, &e->memchunk, c->memblock_stat) < 0)
+            return -1;
+    
     if (!e->memchunk.memblock)
         return -1;
 
     if (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0)
         return -1;
 
-    if (e->auto_unload)
+    if (e->lazy)
         time(&e->last_used_time);
     
     return 0;
@@ -200,42 +241,40 @@ 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) {
     struct pa_scache_entry *e;
-    uint32_t index;
-    uint32_t sum = 0;
+    uint32_t index, sum = 0;
+    assert(c);
 
-    if (!c->scache)
+    if (!c->scache || !pa_idxset_ncontents(c->scache))
         return 0;
     
     for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index))
-        sum += e->memchunk.length;
+        if (e->memchunk.memblock)
+            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) {
+    struct pa_scache_entry *e;
     time_t now;
+    uint32_t index;
     assert(c);
 
-    if (!c->scache)
+    if (!c->scache || !pa_idxset_ncontents(c->scache))
         return;
     
     time(&now);
 
-    pa_idxset_foreach(c->scache, unload_func, &now);
+    for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index)) {
+
+        if (!e->lazy || !e->memchunk.memblock)
+            continue;
+
+        if (e->last_used_time + c->scache_idle_time > now)
+            continue;
+        
+        pa_memblock_unref(e->memchunk.memblock);
+        e->memchunk.memblock = NULL;
+        e->memchunk.index = e->memchunk.length = 0;
+    }
 }
index e057440..4fb3dc8 100644 (file)
@@ -30,15 +30,20 @@ struct pa_scache_entry {
     struct pa_core *core;
     uint32_t index;
     char *name;
+    
     uint32_t volume;
     struct pa_sample_spec sample_spec;
     struct pa_memchunk memchunk;
 
-    int auto_unload;
+    char *filename;
+    
+    int lazy;
     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 auto_unload);
+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_file(struct pa_core *c, const char *name, const char *filename, uint32_t *index);
+int pa_scache_add_file_lazy(struct pa_core *c, const char *name, const char *filename, uint32_t *index);
 
 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);