diff options
Diffstat (limited to 'src/pulsecore/core-scache.c')
-rw-r--r-- | src/pulsecore/core-scache.c | 121 |
1 files changed, 77 insertions, 44 deletions
diff --git a/src/pulsecore/core-scache.c b/src/pulsecore/core-scache.c index 1cd24aa2..75fa2ff1 100644 --- a/src/pulsecore/core-scache.c +++ b/src/pulsecore/core-scache.c @@ -1,9 +1,7 @@ -/* $Id$ */ - /*** This file is part of PulseAudio. - Copyright 2004-2006 Lennart Poettering + Copyright 2004-2008 Lennart Poettering Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB PulseAudio is free software; you can redistribute it and/or modify @@ -63,12 +61,12 @@ #include "core-scache.h" -#define UNLOAD_POLL_TIME 2 +#define UNLOAD_POLL_TIME 60 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) { pa_core *c = userdata; struct timeval ntv; - + pa_assert(c); pa_assert(c->mainloop == m); pa_assert(c->scache_auto_unload_event == e); @@ -82,19 +80,21 @@ static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED static void free_entry(pa_scache_entry *e) { pa_assert(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); + if (e->proplist) + pa_proplist_free(e->proplist); pa_xfree(e); } static pa_scache_entry* scache_add_item(pa_core *c, const char *name) { pa_scache_entry *e; - + pa_assert(c); pa_assert(name); @@ -103,6 +103,7 @@ static pa_scache_entry* scache_add_item(pa_core *c, const char *name) { pa_memblock_unref(e->memchunk.memblock); pa_xfree(e->filename); + pa_proplist_clear(e->proplist); pa_assert(e->core == c); @@ -117,11 +118,10 @@ static pa_scache_entry* scache_add_item(pa_core *c, const char *name) { e->name = pa_xstrdup(name); e->core = c; + e->proplist = pa_proplist_new(); - if (!c->scache) { + if (!c->scache) c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); - pa_assert(c->scache); - } pa_idxset_put(c->scache, e, &e->index); @@ -129,25 +129,40 @@ static pa_scache_entry* scache_add_item(pa_core *c, const char *name) { } e->last_used_time = 0; - e->memchunk.memblock = NULL; - e->memchunk.index = e->memchunk.length = 0; + pa_memchunk_reset(&e->memchunk); e->filename = NULL; - e->lazy = 0; + e->lazy = FALSE; e->last_used_time = 0; memset(&e->sample_spec, 0, sizeof(e->sample_spec)); pa_channel_map_init(&e->channel_map); pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX); + pa_proplist_sets(e->proplist, PA_PROP_MEDIA_ROLE, "event"); + return e; } -int pa_scache_add_item(pa_core *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map, const pa_memchunk *chunk, uint32_t *idx) { +int pa_scache_add_item( + pa_core *c, + const char *name, + const pa_sample_spec *ss, + const pa_channel_map *map, + const pa_memchunk *chunk, + pa_proplist *p, + uint32_t *idx) { + pa_scache_entry *e; char st[PA_SAMPLE_SPEC_SNPRINT_MAX]; - + pa_channel_map tmap; + pa_assert(c); pa_assert(name); + pa_assert(!ss || pa_sample_spec_valid(ss)); + pa_assert(!map || (pa_channel_map_valid(map) && ss && ss->channels == map->channels)); + + if (ss && !map) + pa_channel_map_init_extend(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT); if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX) return -1; @@ -155,9 +170,11 @@ int pa_scache_add_item(pa_core *c, const char *name, const pa_sample_spec *ss, c if (!(e = scache_add_item(c, name))) return -1; + memset(&e->sample_spec, 0, sizeof(e->sample_spec)); + pa_channel_map_init(&e->channel_map); + if (ss) { e->sample_spec = *ss; - pa_channel_map_init_auto(&e->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT); e->volume.channels = e->sample_spec.channels; } @@ -169,6 +186,9 @@ int pa_scache_add_item(pa_core *c, const char *name, const pa_sample_spec *ss, c pa_memblock_ref(e->memchunk.memblock); } + if (p) + pa_proplist_update(e->proplist, PA_UPDATE_REPLACE, p); + if (idx) *idx = e->index; @@ -184,6 +204,7 @@ int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint3 pa_channel_map map; pa_memchunk chunk; int r; + pa_proplist *p; #ifdef OS_IS_WIN32 char buf[MAX_PATH]; @@ -195,12 +216,15 @@ int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint3 pa_assert(c); pa_assert(name); pa_assert(filename); - + if (pa_sound_file_load(c->mempool, filename, &ss, &map, &chunk) < 0) return -1; - r = pa_scache_add_item(c, name, &ss, &map, &chunk, idx); + p = pa_proplist_new(); + pa_proplist_sets(p, PA_PROP_MEDIA_FILENAME, filename); + r = pa_scache_add_item(c, name, &ss, &map, &chunk, p, idx); pa_memblock_unref(chunk.memblock); + pa_proplist_free(p); return r; } @@ -222,9 +246,11 @@ int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, if (!(e = scache_add_item(c, name))) return -1; - e->lazy = 1; + e->lazy = TRUE; e->filename = pa_xstrdup(filename); + pa_proplist_sets(e->proplist, PA_PROP_MEDIA_FILENAME, filename); + if (!c->scache_auto_unload_event) { struct timeval ntv; pa_gettimeofday(&ntv); @@ -240,15 +266,14 @@ int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, int pa_scache_remove_item(pa_core *c, const char *name) { pa_scache_entry *e; - + pa_assert(c); pa_assert(name); if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) return -1; - if (pa_idxset_remove_by_data(c->scache, e, NULL) != e) - pa_assert(0); + pa_assert_se(pa_idxset_remove_by_data(c->scache, e, NULL) == e); pa_log_debug("Removed sample \"%s\"", name); @@ -260,7 +285,7 @@ int pa_scache_remove_item(pa_core *c, const char *name) { static void free_cb(void *p, PA_GCC_UNUSED void *userdata) { pa_scache_entry *e = p; pa_assert(e); - + free_entry(e); } @@ -276,10 +301,10 @@ void pa_scache_free(pa_core *c) { c->mainloop->time_free(c->scache_auto_unload_event); } -int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume) { +int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume, pa_proplist *p, uint32_t *sink_input_idx) { pa_scache_entry *e; - char *t; pa_cvolume r; + pa_proplist *merged; pa_assert(c); pa_assert(name); @@ -303,17 +328,24 @@ int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t pa_log_debug("Playing sample \"%s\" on \"%s\"", name, sink->name); - t = pa_sprintf_malloc("sample:%s", name); - pa_cvolume_set(&r, e->volume.channels, volume); pa_sw_cvolume_multiply(&r, &r, &e->volume); - if (pa_play_memchunk(sink, t, &e->sample_spec, &e->channel_map, &e->memchunk, &r) < 0) { - pa_xfree(t); + merged = pa_proplist_new(); + + pa_proplist_setf(merged, PA_PROP_MEDIA_NAME, "Sample %s", name); + + pa_proplist_update(merged, PA_UPDATE_REPLACE, e->proplist); + + if (p) + pa_proplist_update(merged, PA_UPDATE_REPLACE, p); + + if (pa_play_memchunk(sink, &e->sample_spec, &e->channel_map, &e->memchunk, &r, merged, sink_input_idx) < 0) { + pa_proplist_free(merged); return -1; } - pa_xfree(t); + pa_proplist_free(merged); if (e->lazy) time(&e->last_used_time); @@ -321,21 +353,21 @@ int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t return 0; } -int pa_scache_play_item_by_name(pa_core *c, const char *name, const char*sink_name, pa_volume_t volume, int autoload) { +int pa_scache_play_item_by_name(pa_core *c, const char *name, const char*sink_name, pa_bool_t autoload, pa_volume_t volume, pa_proplist *p, uint32_t *sink_input_idx) { pa_sink *sink; - + pa_assert(c); pa_assert(name); if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, autoload))) return -1; - return pa_scache_play_item(c, name, sink, volume); + return pa_scache_play_item(c, name, sink, volume, p, sink_input_idx); } -const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) { +const char *pa_scache_get_name_by_id(pa_core *c, uint32_t id) { pa_scache_entry *e; - + pa_assert(c); pa_assert(id != PA_IDXSET_INVALID); @@ -347,7 +379,7 @@ const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) { uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) { pa_scache_entry *e; - + pa_assert(c); pa_assert(name); @@ -357,10 +389,11 @@ uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) { return e->index; } -uint32_t pa_scache_total_size(pa_core *c) { +size_t pa_scache_total_size(pa_core *c) { pa_scache_entry *e; - uint32_t idx, sum = 0; - + uint32_t idx; + size_t sum = 0; + pa_assert(c); if (!c->scache || !pa_idxset_size(c->scache)) @@ -394,8 +427,7 @@ void pa_scache_unload_unused(pa_core *c) { continue; pa_memblock_unref(e->memchunk.memblock); - e->memchunk.memblock = NULL; - e->memchunk.index = e->memchunk.length = 0; + pa_memchunk_reset(&e->memchunk); pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index); } @@ -407,7 +439,7 @@ static void add_file(pa_core *c, const char *pathname) { pa_core_assert_ref(c); pa_assert(pathname); - + e = pa_path_get_filename(pathname); if (stat(pathname, &st) < 0) { @@ -423,7 +455,7 @@ static void add_file(pa_core *c, const char *pathname) { int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) { DIR *dir; - + pa_core_assert_ref(c); pa_assert(pathname); @@ -458,8 +490,9 @@ int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) { pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name); add_file(c, p); } + + closedir(dir); } - closedir(dir); return 0; } |