4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <sys/types.h>
44 #include <polyp/mainloop.h>
45 #include <polyp/channelmap.h>
46 #include <polyp/volume.h>
47 #include <polyp/xmalloc.h>
49 #include <polypcore/sink-input.h>
50 #include <polypcore/sample-util.h>
51 #include <polypcore/play-memchunk.h>
52 #include <polypcore/core-subscribe.h>
53 #include <polypcore/namereg.h>
54 #include <polypcore/sound-file.h>
55 #include <polypcore/util.h>
56 #include <polypcore/log.h>
58 #include "core-scache.h"
60 #define UNLOAD_POLL_TIME 2
62 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
63 pa_core *c = userdata;
65 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
67 pa_scache_unload_unused(c);
69 pa_gettimeofday(&ntv);
70 ntv.tv_sec += UNLOAD_POLL_TIME;
71 m->time_restart(e, &ntv);
74 static void free_entry(pa_scache_entry *e) {
76 pa_namereg_unregister(e->core, e->name);
77 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
79 pa_xfree(e->filename);
80 if (e->memchunk.memblock)
81 pa_memblock_unref(e->memchunk.memblock);
85 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
89 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
90 if (e->memchunk.memblock)
91 pa_memblock_unref(e->memchunk.memblock);
93 pa_xfree(e->filename);
97 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
99 e = pa_xmalloc(sizeof(pa_scache_entry));
101 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
106 e->name = pa_xstrdup(name);
110 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
114 pa_idxset_put(c->scache, e, &e->index);
116 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
119 e->last_used_time = 0;
120 e->memchunk.memblock = NULL;
121 e->memchunk.index = e->memchunk.length = 0;
124 e->last_used_time = 0;
126 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
127 pa_channel_map_init(&e->channel_map);
128 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
133 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) {
137 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
140 if (!(e = scache_add_item(c, name)))
144 e->sample_spec = *ss;
145 pa_channel_map_init_auto(&e->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
146 e->volume.channels = e->sample_spec.channels;
150 e->channel_map = *map;
153 e->memchunk = *chunk;
154 pa_memblock_ref(e->memchunk.memblock);
163 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
172 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
176 if (pa_sound_file_load(filename, &ss, &map, &chunk, c->memblock_stat) < 0)
179 r = pa_scache_add_item(c, name, &ss, &map, &chunk, idx);
180 pa_memblock_unref(chunk.memblock);
185 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
191 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
197 if (!(e = scache_add_item(c, name)))
201 e->filename = pa_xstrdup(filename);
203 if (!c->scache_auto_unload_event) {
205 pa_gettimeofday(&ntv);
206 ntv.tv_sec += UNLOAD_POLL_TIME;
207 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
216 int pa_scache_remove_item(pa_core *c, const char *name) {
220 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
223 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
230 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
231 pa_scache_entry *e = p;
236 void pa_scache_free(pa_core *c) {
240 pa_idxset_free(c->scache, free_cb, NULL);
244 if (c->scache_auto_unload_event)
245 c->mainloop->time_free(c->scache_auto_unload_event);
248 int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume) {
257 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
260 if (e->lazy && !e->memchunk.memblock) {
261 if (pa_sound_file_load(e->filename, &e->sample_spec, &e->channel_map, &e->memchunk, c->memblock_stat) < 0)
264 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
266 if (e->volume.channels > e->sample_spec.channels)
267 e->volume.channels = e->sample_spec.channels;
270 if (!e->memchunk.memblock)
273 t = pa_sprintf_malloc("sample:%s", name);
275 pa_cvolume_set(&r, e->volume.channels, volume);
276 pa_sw_cvolume_multiply(&r, &r, &e->volume);
278 if (pa_play_memchunk(sink, t, &e->sample_spec, &e->channel_map, &e->memchunk, &r) < 0) {
286 time(&e->last_used_time);
291 const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
293 assert(c && id != PA_IDXSET_INVALID);
295 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
301 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
305 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
306 return PA_IDXSET_INVALID;
311 uint32_t pa_scache_total_size(pa_core *c) {
313 uint32_t idx, sum = 0;
316 if (!c->scache || !pa_idxset_size(c->scache))
319 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
320 if (e->memchunk.memblock)
321 sum += e->memchunk.length;
326 void pa_scache_unload_unused(pa_core *c) {
332 if (!c->scache || !pa_idxset_size(c->scache))
337 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
339 if (!e->lazy || !e->memchunk.memblock)
342 if (e->last_used_time + c->scache_idle_time > now)
345 pa_memblock_unref(e->memchunk.memblock);
346 e->memchunk.memblock = NULL;
347 e->memchunk.index = e->memchunk.length = 0;
349 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
353 static void add_file(pa_core *c, const char *pathname) {
357 e = pa_path_get_filename(pathname);
359 if (stat(pathname, &st) < 0) {
360 pa_log(__FILE__": stat('%s') failed: %s", pathname, strerror(errno));
364 #if defined(S_ISREG) && defined(S_ISLNK)
365 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
367 pa_scache_add_file_lazy(c, e, pathname, NULL);
370 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
372 assert(c && pathname);
374 /* First try to open this as directory */
375 if (!(dir = opendir(pathname))) {
379 /* If that fails, try to open it as shell glob */
381 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
382 pa_log(__FILE__": Failed to open directory: %s", strerror(errno));
386 for (i = 0; i < p.gl_pathc; i++)
387 add_file(c, p.gl_pathv[i]);
396 while ((e = readdir(dir))) {
399 if (e->d_name[0] == '.')
402 snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);