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