]> code.delx.au - pulseaudio/blob - src/pulsecore/core-scache.c
* Increase history set to 64 to simplify reduction of indexes
[pulseaudio] / src / pulsecore / core-scache.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <limits.h>
37
38 #ifdef HAVE_GLOB_H
39 #include <glob.h>
40 #endif
41
42 #ifdef HAVE_WINDOWS_H
43 #include <windows.h>
44 #endif
45
46 #include <pulse/mainloop.h>
47 #include <pulse/channelmap.h>
48 #include <pulse/timeval.h>
49 #include <pulse/util.h>
50 #include <pulse/volume.h>
51 #include <pulse/xmalloc.h>
52
53 #include <pulsecore/sink-input.h>
54 #include <pulsecore/sample-util.h>
55 #include <pulsecore/play-memchunk.h>
56 #include <pulsecore/core-subscribe.h>
57 #include <pulsecore/namereg.h>
58 #include <pulsecore/sound-file.h>
59 #include <pulsecore/core-util.h>
60 #include <pulsecore/log.h>
61 #include <pulsecore/core-error.h>
62 #include <pulsecore/macro.h>
63
64 #include "core-scache.h"
65
66 #define UNLOAD_POLL_TIME 5
67
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;
70 struct timeval ntv;
71
72 pa_assert(c);
73 pa_assert(c->mainloop == m);
74 pa_assert(c->scache_auto_unload_event == e);
75
76 pa_scache_unload_unused(c);
77
78 pa_gettimeofday(&ntv);
79 ntv.tv_sec += UNLOAD_POLL_TIME;
80 m->time_restart(e, &ntv);
81 }
82
83 static void free_entry(pa_scache_entry *e) {
84 pa_assert(e);
85
86 pa_namereg_unregister(e->core, e->name);
87 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
88 pa_xfree(e->name);
89 pa_xfree(e->filename);
90 if (e->memchunk.memblock)
91 pa_memblock_unref(e->memchunk.memblock);
92 if (e->proplist)
93 pa_proplist_free(e->proplist);
94 pa_xfree(e);
95 }
96
97 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
98 pa_scache_entry *e;
99
100 pa_assert(c);
101 pa_assert(name);
102
103 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
104 if (e->memchunk.memblock)
105 pa_memblock_unref(e->memchunk.memblock);
106
107 pa_xfree(e->filename);
108 pa_proplist_clear(e->proplist);
109
110 pa_assert(e->core == c);
111
112 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
113 } else {
114 e = pa_xnew(pa_scache_entry, 1);
115
116 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
117 pa_xfree(e);
118 return NULL;
119 }
120
121 e->name = pa_xstrdup(name);
122 e->core = c;
123 e->proplist = pa_proplist_new();
124
125 if (!c->scache)
126 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
127
128 pa_idxset_put(c->scache, e, &e->index);
129
130 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
131 }
132
133 e->last_used_time = 0;
134 e->memchunk.memblock = NULL;
135 e->memchunk.index = e->memchunk.length = 0;
136 e->filename = NULL;
137 e->lazy = FALSE;
138 e->last_used_time = 0;
139
140 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
141 pa_channel_map_init(&e->channel_map);
142 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
143
144 return e;
145 }
146
147 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) {
148 pa_scache_entry *e;
149 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
150 pa_channel_map tmap;
151
152 pa_assert(c);
153 pa_assert(name);
154 pa_assert(!ss || pa_sample_spec_valid(ss));
155 pa_assert(!map || (pa_channel_map_valid(map) && ss && ss->channels == map->channels));
156
157 if (ss && !map)
158 if (!(map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT)))
159 return -1;
160
161 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
162 return -1;
163
164 if (!(e = scache_add_item(c, name)))
165 return -1;
166
167 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
168 pa_channel_map_init(&e->channel_map);
169
170 if (ss) {
171 e->sample_spec = *ss;
172 e->volume.channels = e->sample_spec.channels;
173 }
174
175 if (map)
176 e->channel_map = *map;
177
178 if (chunk) {
179 e->memchunk = *chunk;
180 pa_memblock_ref(e->memchunk.memblock);
181 }
182
183 if (p)
184 pa_proplist_update(e->proplist, PA_UPDATE_REPLACE, p);
185
186 if (idx)
187 *idx = e->index;
188
189 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
190 name, e->index, (unsigned long) e->memchunk.length,
191 pa_sample_spec_snprint(st, sizeof(st), &e->sample_spec));
192
193 return 0;
194 }
195
196 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
197 pa_sample_spec ss;
198 pa_channel_map map;
199 pa_memchunk chunk;
200 int r;
201
202 #ifdef OS_IS_WIN32
203 char buf[MAX_PATH];
204
205 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
206 filename = buf;
207 #endif
208
209 pa_assert(c);
210 pa_assert(name);
211 pa_assert(filename);
212
213 if (pa_sound_file_load(c->mempool, filename, &ss, &map, &chunk) < 0)
214 return -1;
215
216 r = pa_scache_add_item(c, name, &ss, &map, &chunk, NULL, idx);
217 pa_memblock_unref(chunk.memblock);
218
219 return r;
220 }
221
222 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
223 pa_scache_entry *e;
224
225 #ifdef OS_IS_WIN32
226 char buf[MAX_PATH];
227
228 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
229 filename = buf;
230 #endif
231
232 pa_assert(c);
233 pa_assert(name);
234 pa_assert(filename);
235
236 if (!(e = scache_add_item(c, name)))
237 return -1;
238
239 e->lazy = TRUE;
240 e->filename = pa_xstrdup(filename);
241
242 if (!c->scache_auto_unload_event) {
243 struct timeval ntv;
244 pa_gettimeofday(&ntv);
245 ntv.tv_sec += UNLOAD_POLL_TIME;
246 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
247 }
248
249 if (idx)
250 *idx = e->index;
251
252 return 0;
253 }
254
255 int pa_scache_remove_item(pa_core *c, const char *name) {
256 pa_scache_entry *e;
257
258 pa_assert(c);
259 pa_assert(name);
260
261 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
262 return -1;
263
264 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
265 pa_assert(0);
266
267 pa_log_debug("Removed sample \"%s\"", name);
268
269 free_entry(e);
270
271 return 0;
272 }
273
274 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
275 pa_scache_entry *e = p;
276 pa_assert(e);
277
278 free_entry(e);
279 }
280
281 void pa_scache_free(pa_core *c) {
282 pa_assert(c);
283
284 if (c->scache) {
285 pa_idxset_free(c->scache, free_cb, NULL);
286 c->scache = NULL;
287 }
288
289 if (c->scache_auto_unload_event)
290 c->mainloop->time_free(c->scache_auto_unload_event);
291 }
292
293 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) {
294 pa_scache_entry *e;
295 char *t;
296 pa_cvolume r;
297 pa_proplist *merged;
298
299 pa_assert(c);
300 pa_assert(name);
301 pa_assert(sink);
302
303 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
304 return -1;
305
306 if (e->lazy && !e->memchunk.memblock) {
307 if (pa_sound_file_load(c->mempool, e->filename, &e->sample_spec, &e->channel_map, &e->memchunk) < 0)
308 return -1;
309
310 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
311
312 if (e->volume.channels > e->sample_spec.channels)
313 e->volume.channels = e->sample_spec.channels;
314 }
315
316 if (!e->memchunk.memblock)
317 return -1;
318
319 pa_log_debug("Playing sample \"%s\" on \"%s\"", name, sink->name);
320
321 pa_cvolume_set(&r, e->volume.channels, volume);
322 pa_sw_cvolume_multiply(&r, &r, &e->volume);
323
324 merged = pa_proplist_new();
325
326 t = pa_sprintf_malloc("sample:%s", name);
327 pa_proplist_sets(merged, PA_PROP_MEDIA_NAME, t);
328 pa_xfree(t);
329
330 pa_proplist_update(merged, PA_UPDATE_REPLACE, e->proplist);
331 pa_proplist_update(merged, PA_UPDATE_REPLACE, p);
332
333 if (pa_play_memchunk(sink, &e->sample_spec, &e->channel_map, &e->memchunk, &r, merged, sink_input_idx) < 0) {
334 pa_proplist_free(merged);
335 return -1;
336 }
337
338 pa_proplist_free(merged);
339
340 if (e->lazy)
341 time(&e->last_used_time);
342
343 return 0;
344 }
345
346 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) {
347 pa_sink *sink;
348
349 pa_assert(c);
350 pa_assert(name);
351
352 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, autoload)))
353 return -1;
354
355 return pa_scache_play_item(c, name, sink, volume, p, sink_input_idx);
356 }
357
358 const char *pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
359 pa_scache_entry *e;
360
361 pa_assert(c);
362 pa_assert(id != PA_IDXSET_INVALID);
363
364 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
365 return NULL;
366
367 return e->name;
368 }
369
370 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
371 pa_scache_entry *e;
372
373 pa_assert(c);
374 pa_assert(name);
375
376 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
377 return PA_IDXSET_INVALID;
378
379 return e->index;
380 }
381
382 size_t pa_scache_total_size(pa_core *c) {
383 pa_scache_entry *e;
384 uint32_t idx;
385 size_t sum = 0;
386
387 pa_assert(c);
388
389 if (!c->scache || !pa_idxset_size(c->scache))
390 return 0;
391
392 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
393 if (e->memchunk.memblock)
394 sum += e->memchunk.length;
395
396 return sum;
397 }
398
399 void pa_scache_unload_unused(pa_core *c) {
400 pa_scache_entry *e;
401 time_t now;
402 uint32_t idx;
403
404 pa_assert(c);
405
406 if (!c->scache || !pa_idxset_size(c->scache))
407 return;
408
409 time(&now);
410
411 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
412
413 if (!e->lazy || !e->memchunk.memblock)
414 continue;
415
416 if (e->last_used_time + c->scache_idle_time > now)
417 continue;
418
419 pa_memblock_unref(e->memchunk.memblock);
420 pa_memchunk_reset(&e->memchunk);
421
422 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
423 }
424 }
425
426 static void add_file(pa_core *c, const char *pathname) {
427 struct stat st;
428 const char *e;
429
430 pa_core_assert_ref(c);
431 pa_assert(pathname);
432
433 e = pa_path_get_filename(pathname);
434
435 if (stat(pathname, &st) < 0) {
436 pa_log("stat('%s'): %s", pathname, pa_cstrerror(errno));
437 return;
438 }
439
440 #if defined(S_ISREG) && defined(S_ISLNK)
441 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
442 #endif
443 pa_scache_add_file_lazy(c, e, pathname, NULL);
444 }
445
446 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
447 DIR *dir;
448
449 pa_core_assert_ref(c);
450 pa_assert(pathname);
451
452 /* First try to open this as directory */
453 if (!(dir = opendir(pathname))) {
454 #ifdef HAVE_GLOB_H
455 glob_t p;
456 unsigned int i;
457 /* If that fails, try to open it as shell glob */
458
459 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
460 pa_log("failed to open directory '%s': %s", pathname, pa_cstrerror(errno));
461 return -1;
462 }
463
464 for (i = 0; i < p.gl_pathc; i++)
465 add_file(c, p.gl_pathv[i]);
466
467 globfree(&p);
468 #else
469 return -1;
470 #endif
471 } else {
472 struct dirent *e;
473
474 while ((e = readdir(dir))) {
475 char p[PATH_MAX];
476
477 if (e->d_name[0] == '.')
478 continue;
479
480 pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
481 add_file(c, p);
482 }
483
484 closedir(dir);
485 }
486
487 return 0;
488 }