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