Remove u->channels and u->rates, since it's redundant info
[pulseaudio-mirror.git] / src / pulsecore / core-scache.c
blob814dff596d15fd838b40d494cf2c7a85a2685d21
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <limits.h>
36 #ifdef HAVE_GLOB_H
37 #include <glob.h>
38 #endif
40 #ifdef HAVE_WINDOWS_H
41 #include <windows.h>
42 #endif
44 #include <pulse/mainloop.h>
45 #include <pulse/channelmap.h>
46 #include <pulse/timeval.h>
47 #include <pulse/util.h>
48 #include <pulse/volume.h>
49 #include <pulse/xmalloc.h>
51 #include <pulsecore/sink-input.h>
52 #include <pulsecore/sample-util.h>
53 #include <pulsecore/play-memchunk.h>
54 #include <pulsecore/core-subscribe.h>
55 #include <pulsecore/namereg.h>
56 #include <pulsecore/sound-file.h>
57 #include <pulsecore/core-util.h>
58 #include <pulsecore/log.h>
59 #include <pulsecore/core-error.h>
60 #include <pulsecore/macro.h>
62 #include "core-scache.h"
64 #define UNLOAD_POLL_TIME 60
66 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, const struct timeval *tv, void *userdata) {
67 pa_core *c = userdata;
68 struct timeval ntv;
70 pa_assert(c);
71 pa_assert(c->mainloop == m);
72 pa_assert(c->scache_auto_unload_event == e);
74 pa_scache_unload_unused(c);
76 pa_gettimeofday(&ntv);
77 ntv.tv_sec += UNLOAD_POLL_TIME;
78 m->time_restart(e, &ntv);
81 static void free_entry(pa_scache_entry *e) {
82 pa_assert(e);
84 pa_namereg_unregister(e->core, e->name);
85 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
86 pa_xfree(e->name);
87 pa_xfree(e->filename);
88 if (e->memchunk.memblock)
89 pa_memblock_unref(e->memchunk.memblock);
90 if (e->proplist)
91 pa_proplist_free(e->proplist);
92 pa_xfree(e);
95 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
96 pa_scache_entry *e;
98 pa_assert(c);
99 pa_assert(name);
101 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
102 if (e->memchunk.memblock)
103 pa_memblock_unref(e->memchunk.memblock);
105 pa_xfree(e->filename);
106 pa_proplist_clear(e->proplist);
108 pa_assert(e->core == c);
110 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
111 } else {
112 e = pa_xnew(pa_scache_entry, 1);
114 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
115 pa_xfree(e);
116 return NULL;
119 e->name = pa_xstrdup(name);
120 e->core = c;
121 e->proplist = pa_proplist_new();
123 if (!c->scache)
124 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
126 pa_idxset_put(c->scache, e, &e->index);
128 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
131 e->last_used_time = 0;
132 pa_memchunk_reset(&e->memchunk);
133 e->filename = NULL;
134 e->lazy = FALSE;
135 e->last_used_time = 0;
137 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
138 pa_channel_map_init(&e->channel_map);
139 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
141 pa_proplist_sets(e->proplist, PA_PROP_MEDIA_ROLE, "event");
143 return e;
146 int pa_scache_add_item(
147 pa_core *c,
148 const char *name,
149 const pa_sample_spec *ss,
150 const pa_channel_map *map,
151 const pa_memchunk *chunk,
152 pa_proplist *p,
153 uint32_t *idx) {
155 pa_scache_entry *e;
156 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
157 pa_channel_map tmap;
159 pa_assert(c);
160 pa_assert(name);
161 pa_assert(!ss || pa_sample_spec_valid(ss));
162 pa_assert(!map || (pa_channel_map_valid(map) && ss && ss->channels == map->channels));
164 if (ss && !map)
165 pa_channel_map_init_extend(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT);
167 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
168 return -1;
170 if (!(e = scache_add_item(c, name)))
171 return -1;
173 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
174 pa_channel_map_init(&e->channel_map);
176 if (ss) {
177 e->sample_spec = *ss;
178 e->volume.channels = e->sample_spec.channels;
181 if (map)
182 e->channel_map = *map;
184 if (chunk) {
185 e->memchunk = *chunk;
186 pa_memblock_ref(e->memchunk.memblock);
189 if (p)
190 pa_proplist_update(e->proplist, PA_UPDATE_REPLACE, p);
192 if (idx)
193 *idx = e->index;
195 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
196 name, e->index, (unsigned long) e->memchunk.length,
197 pa_sample_spec_snprint(st, sizeof(st), &e->sample_spec));
199 return 0;
202 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
203 pa_sample_spec ss;
204 pa_channel_map map;
205 pa_memchunk chunk;
206 int r;
207 pa_proplist *p;
209 #ifdef OS_IS_WIN32
210 char buf[MAX_PATH];
212 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
213 filename = buf;
214 #endif
216 pa_assert(c);
217 pa_assert(name);
218 pa_assert(filename);
220 if (pa_sound_file_load(c->mempool, filename, &ss, &map, &chunk) < 0)
221 return -1;
223 p = pa_proplist_new();
224 pa_proplist_sets(p, PA_PROP_MEDIA_FILENAME, filename);
225 r = pa_scache_add_item(c, name, &ss, &map, &chunk, p, idx);
226 pa_memblock_unref(chunk.memblock);
227 pa_proplist_free(p);
229 return r;
232 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
233 pa_scache_entry *e;
235 #ifdef OS_IS_WIN32
236 char buf[MAX_PATH];
238 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
239 filename = buf;
240 #endif
242 pa_assert(c);
243 pa_assert(name);
244 pa_assert(filename);
246 if (!(e = scache_add_item(c, name)))
247 return -1;
249 e->lazy = TRUE;
250 e->filename = pa_xstrdup(filename);
252 pa_proplist_sets(e->proplist, PA_PROP_MEDIA_FILENAME, filename);
254 if (!c->scache_auto_unload_event) {
255 struct timeval ntv;
256 pa_gettimeofday(&ntv);
257 ntv.tv_sec += UNLOAD_POLL_TIME;
258 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
261 if (idx)
262 *idx = e->index;
264 return 0;
267 int pa_scache_remove_item(pa_core *c, const char *name) {
268 pa_scache_entry *e;
270 pa_assert(c);
271 pa_assert(name);
273 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
274 return -1;
276 pa_assert_se(pa_idxset_remove_by_data(c->scache, e, NULL) == e);
278 pa_log_debug("Removed sample \"%s\"", name);
280 free_entry(e);
282 return 0;
285 static void free_cb(void *p, void *userdata) {
286 pa_scache_entry *e = p;
287 pa_assert(e);
289 free_entry(e);
292 void pa_scache_free(pa_core *c) {
293 pa_assert(c);
295 if (c->scache) {
296 pa_idxset_free(c->scache, free_cb, NULL);
297 c->scache = NULL;
300 if (c->scache_auto_unload_event)
301 c->mainloop->time_free(c->scache_auto_unload_event);
304 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) {
305 pa_scache_entry *e;
306 pa_cvolume r;
307 pa_proplist *merged;
309 pa_assert(c);
310 pa_assert(name);
311 pa_assert(sink);
313 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
314 return -1;
316 if (e->lazy && !e->memchunk.memblock) {
317 if (pa_sound_file_load(c->mempool, e->filename, &e->sample_spec, &e->channel_map, &e->memchunk) < 0)
318 return -1;
320 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
322 if (e->volume.channels > e->sample_spec.channels)
323 e->volume.channels = e->sample_spec.channels;
326 if (!e->memchunk.memblock)
327 return -1;
329 pa_log_debug("Playing sample \"%s\" on \"%s\"", name, sink->name);
331 pa_cvolume_set(&r, e->volume.channels, volume);
332 pa_sw_cvolume_multiply(&r, &r, &e->volume);
334 merged = pa_proplist_new();
336 pa_proplist_setf(merged, PA_PROP_MEDIA_NAME, "Sample %s", name);
338 pa_proplist_update(merged, PA_UPDATE_REPLACE, e->proplist);
340 if (p)
341 pa_proplist_update(merged, PA_UPDATE_REPLACE, p);
343 if (pa_play_memchunk(sink, &e->sample_spec, &e->channel_map, &e->memchunk, &r, merged, sink_input_idx) < 0) {
344 pa_proplist_free(merged);
345 return -1;
348 pa_proplist_free(merged);
350 if (e->lazy)
351 time(&e->last_used_time);
353 return 0;
356 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) {
357 pa_sink *sink;
359 pa_assert(c);
360 pa_assert(name);
362 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, autoload)))
363 return -1;
365 return pa_scache_play_item(c, name, sink, volume, p, sink_input_idx);
368 const char *pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
369 pa_scache_entry *e;
371 pa_assert(c);
372 pa_assert(id != PA_IDXSET_INVALID);
374 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
375 return NULL;
377 return e->name;
380 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
381 pa_scache_entry *e;
383 pa_assert(c);
384 pa_assert(name);
386 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
387 return PA_IDXSET_INVALID;
389 return e->index;
392 size_t pa_scache_total_size(pa_core *c) {
393 pa_scache_entry *e;
394 uint32_t idx;
395 size_t sum = 0;
397 pa_assert(c);
399 if (!c->scache || !pa_idxset_size(c->scache))
400 return 0;
402 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
403 if (e->memchunk.memblock)
404 sum += e->memchunk.length;
406 return sum;
409 void pa_scache_unload_unused(pa_core *c) {
410 pa_scache_entry *e;
411 time_t now;
412 uint32_t idx;
414 pa_assert(c);
416 if (!c->scache || !pa_idxset_size(c->scache))
417 return;
419 time(&now);
421 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
423 if (!e->lazy || !e->memchunk.memblock)
424 continue;
426 if (e->last_used_time + c->scache_idle_time > now)
427 continue;
429 pa_memblock_unref(e->memchunk.memblock);
430 pa_memchunk_reset(&e->memchunk);
432 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
436 static void add_file(pa_core *c, const char *pathname) {
437 struct stat st;
438 const char *e;
440 pa_core_assert_ref(c);
441 pa_assert(pathname);
443 e = pa_path_get_filename(pathname);
445 if (stat(pathname, &st) < 0) {
446 pa_log("stat('%s'): %s", pathname, pa_cstrerror(errno));
447 return;
450 #if defined(S_ISREG) && defined(S_ISLNK)
451 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
452 #endif
453 pa_scache_add_file_lazy(c, e, pathname, NULL);
456 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
457 DIR *dir;
459 pa_core_assert_ref(c);
460 pa_assert(pathname);
462 /* First try to open this as directory */
463 if (!(dir = opendir(pathname))) {
464 #ifdef HAVE_GLOB_H
465 glob_t p;
466 unsigned int i;
467 /* If that fails, try to open it as shell glob */
469 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
470 pa_log("failed to open directory '%s': %s", pathname, pa_cstrerror(errno));
471 return -1;
474 for (i = 0; i < p.gl_pathc; i++)
475 add_file(c, p.gl_pathv[i]);
477 globfree(&p);
478 #else
479 return -1;
480 #endif
481 } else {
482 struct dirent *e;
484 while ((e = readdir(dir))) {
485 char p[PATH_MAX];
487 if (e->d_name[0] == '.')
488 continue;
490 pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
491 add_file(c, p);
494 closedir(dir);
497 return 0;