4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <sys/types.h>
47 #include <pulse/mainloop.h>
48 #include <pulse/channelmap.h>
49 #include <pulse/timeval.h>
50 #include <pulse/util.h>
51 #include <pulse/volume.h>
52 #include <pulse/xmalloc.h>
54 #include <pulsecore/sink-input.h>
55 #include <pulsecore/sample-util.h>
56 #include <pulsecore/play-memchunk.h>
57 #include <pulsecore/core-subscribe.h>
58 #include <pulsecore/namereg.h>
59 #include <pulsecore/sound-file.h>
60 #include <pulsecore/core-util.h>
61 #include <pulsecore/log.h>
62 #include <pulsecore/core-error.h>
64 #include "core-scache.h"
66 #define UNLOAD_POLL_TIME 2
68 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
69 pa_core *c = userdata;
71 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
73 pa_scache_unload_unused(c);
75 pa_gettimeofday(&ntv);
76 ntv.tv_sec += UNLOAD_POLL_TIME;
77 m->time_restart(e, &ntv);
80 static void free_entry(pa_scache_entry *e) {
82 pa_namereg_unregister(e->core, e->name);
83 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
85 pa_xfree(e->filename);
86 if (e->memchunk.memblock)
87 pa_memblock_unref(e->memchunk.memblock);
91 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
95 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
96 if (e->memchunk.memblock)
97 pa_memblock_unref(e->memchunk.memblock);
99 pa_xfree(e->filename);
101 assert(e->core == c);
103 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
105 e = pa_xmalloc(sizeof(pa_scache_entry));
107 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
112 e->name = pa_xstrdup(name);
116 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
120 pa_idxset_put(c->scache, e, &e->index);
122 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
125 e->last_used_time = 0;
126 e->memchunk.memblock = NULL;
127 e->memchunk.index = e->memchunk.length = 0;
130 e->last_used_time = 0;
132 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
133 pa_channel_map_init(&e->channel_map);
134 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
139 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) {
143 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
146 if (!(e = scache_add_item(c, name)))
150 e->sample_spec = *ss;
151 pa_channel_map_init_auto(&e->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
152 e->volume.channels = e->sample_spec.channels;
156 e->channel_map = *map;
159 e->memchunk = *chunk;
160 pa_memblock_ref(e->memchunk.memblock);
169 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
178 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
182 if (pa_sound_file_load(c->mempool, filename, &ss, &map, &chunk) < 0)
185 r = pa_scache_add_item(c, name, &ss, &map, &chunk, idx);
186 pa_memblock_unref(chunk.memblock);
191 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
197 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
203 if (!(e = scache_add_item(c, name)))
207 e->filename = pa_xstrdup(filename);
209 if (!c->scache_auto_unload_event) {
211 pa_gettimeofday(&ntv);
212 ntv.tv_sec += UNLOAD_POLL_TIME;
213 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
222 int pa_scache_remove_item(pa_core *c, const char *name) {
226 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
229 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
236 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
237 pa_scache_entry *e = p;
242 void pa_scache_free(pa_core *c) {
246 pa_idxset_free(c->scache, free_cb, NULL);
250 if (c->scache_auto_unload_event)
251 c->mainloop->time_free(c->scache_auto_unload_event);
254 int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume) {
263 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
266 if (e->lazy && !e->memchunk.memblock) {
267 if (pa_sound_file_load(c->mempool, e->filename, &e->sample_spec, &e->channel_map, &e->memchunk) < 0)
270 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
272 if (e->volume.channels > e->sample_spec.channels)
273 e->volume.channels = e->sample_spec.channels;
276 if (!e->memchunk.memblock)
279 t = pa_sprintf_malloc("sample:%s", name);
281 pa_cvolume_set(&r, e->volume.channels, volume);
282 pa_sw_cvolume_multiply(&r, &r, &e->volume);
284 if (pa_play_memchunk(sink, t, &e->sample_spec, &e->channel_map, &e->memchunk, &r) < 0) {
292 time(&e->last_used_time);
297 const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
299 assert(c && id != PA_IDXSET_INVALID);
301 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
307 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
311 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
312 return PA_IDXSET_INVALID;
317 uint32_t pa_scache_total_size(pa_core *c) {
319 uint32_t idx, sum = 0;
322 if (!c->scache || !pa_idxset_size(c->scache))
325 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
326 if (e->memchunk.memblock)
327 sum += e->memchunk.length;
332 void pa_scache_unload_unused(pa_core *c) {
338 if (!c->scache || !pa_idxset_size(c->scache))
343 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
345 if (!e->lazy || !e->memchunk.memblock)
348 if (e->last_used_time + c->scache_idle_time > now)
351 pa_memblock_unref(e->memchunk.memblock);
352 e->memchunk.memblock = NULL;
353 e->memchunk.index = e->memchunk.length = 0;
355 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
359 static void add_file(pa_core *c, const char *pathname) {
363 e = pa_path_get_filename(pathname);
365 if (stat(pathname, &st) < 0) {
366 pa_log("stat('%s'): %s", pathname, pa_cstrerror(errno));
370 #if defined(S_ISREG) && defined(S_ISLNK)
371 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
373 pa_scache_add_file_lazy(c, e, pathname, NULL);
376 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
378 assert(c && pathname);
380 /* First try to open this as directory */
381 if (!(dir = opendir(pathname))) {
385 /* If that fails, try to open it as shell glob */
387 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
388 pa_log("failed to open directory '%s': %s", pathname, pa_cstrerror(errno));
392 for (i = 0; i < p.gl_pathc; i++)
393 add_file(c, p.gl_pathv[i]);
402 while ((e = readdir(dir))) {
405 if (e->d_name[0] == '.')
408 snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);