]> code.delx.au - pulseaudio/blob - src/pulsecore/core-scache.c
beefup proplist handling for sound events
[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 pa_proplist_sets(e->proplist, PA_PROP_MEDIA_ROLE, "event");
145
146 return e;
147 }
148
149 int pa_scache_add_item(
150 pa_core *c,
151 const char *name,
152 const pa_sample_spec *ss,
153 const pa_channel_map *map,
154 const pa_memchunk *chunk,
155 pa_proplist *p,
156 uint32_t *idx) {
157
158 pa_scache_entry *e;
159 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
160 pa_channel_map tmap;
161
162 pa_assert(c);
163 pa_assert(name);
164 pa_assert(!ss || pa_sample_spec_valid(ss));
165 pa_assert(!map || (pa_channel_map_valid(map) && ss && ss->channels == map->channels));
166
167 if (ss && !map)
168 if (!(map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT)))
169 return -1;
170
171 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
172 return -1;
173
174 if (!(e = scache_add_item(c, name)))
175 return -1;
176
177 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
178 pa_channel_map_init(&e->channel_map);
179
180 if (ss) {
181 e->sample_spec = *ss;
182 e->volume.channels = e->sample_spec.channels;
183 }
184
185 if (map)
186 e->channel_map = *map;
187
188 if (chunk) {
189 e->memchunk = *chunk;
190 pa_memblock_ref(e->memchunk.memblock);
191 }
192
193 if (p)
194 pa_proplist_update(e->proplist, PA_UPDATE_REPLACE, p);
195
196 if (idx)
197 *idx = e->index;
198
199 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
200 name, e->index, (unsigned long) e->memchunk.length,
201 pa_sample_spec_snprint(st, sizeof(st), &e->sample_spec));
202
203 return 0;
204 }
205
206 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
207 pa_sample_spec ss;
208 pa_channel_map map;
209 pa_memchunk chunk;
210 int r;
211 pa_proplist *p;
212
213 #ifdef OS_IS_WIN32
214 char buf[MAX_PATH];
215
216 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
217 filename = buf;
218 #endif
219
220 pa_assert(c);
221 pa_assert(name);
222 pa_assert(filename);
223
224 if (pa_sound_file_load(c->mempool, filename, &ss, &map, &chunk) < 0)
225 return -1;
226
227 p = pa_proplist_new();
228 pa_proplist_sets(p, PA_PROP_MEDIA_FILENAME, filename);
229 r = pa_scache_add_item(c, name, &ss, &map, &chunk, p, idx);
230 pa_memblock_unref(chunk.memblock);
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 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
280 pa_assert(0);
281
282 pa_log_debug("Removed sample \"%s\"", name);
283
284 free_entry(e);
285
286 return 0;
287 }
288
289 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
290 pa_scache_entry *e = p;
291 pa_assert(e);
292
293 free_entry(e);
294 }
295
296 void pa_scache_free(pa_core *c) {
297 pa_assert(c);
298
299 if (c->scache) {
300 pa_idxset_free(c->scache, free_cb, NULL);
301 c->scache = NULL;
302 }
303
304 if (c->scache_auto_unload_event)
305 c->mainloop->time_free(c->scache_auto_unload_event);
306 }
307
308 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) {
309 pa_scache_entry *e;
310 pa_cvolume r;
311 pa_proplist *merged;
312
313 pa_assert(c);
314 pa_assert(name);
315 pa_assert(sink);
316
317 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
318 return -1;
319
320 if (e->lazy && !e->memchunk.memblock) {
321 if (pa_sound_file_load(c->mempool, e->filename, &e->sample_spec, &e->channel_map, &e->memchunk) < 0)
322 return -1;
323
324 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
325
326 if (e->volume.channels > e->sample_spec.channels)
327 e->volume.channels = e->sample_spec.channels;
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 pa_cvolume_set(&r, e->volume.channels, volume);
336 pa_sw_cvolume_multiply(&r, &r, &e->volume);
337
338 merged = pa_proplist_new();
339
340 pa_proplist_setf(merged, PA_PROP_MEDIA_NAME, "Sample %s", name);
341
342 pa_proplist_update(merged, PA_UPDATE_REPLACE, e->proplist);
343
344 if (p)
345 pa_proplist_update(merged, PA_UPDATE_REPLACE, p);
346
347 if (pa_play_memchunk(sink, &e->sample_spec, &e->channel_map, &e->memchunk, &r, merged, sink_input_idx) < 0) {
348 pa_proplist_free(merged);
349 return -1;
350 }
351
352 pa_proplist_free(merged);
353
354 if (e->lazy)
355 time(&e->last_used_time);
356
357 return 0;
358 }
359
360 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) {
361 pa_sink *sink;
362
363 pa_assert(c);
364 pa_assert(name);
365
366 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, autoload)))
367 return -1;
368
369 return pa_scache_play_item(c, name, sink, volume, p, sink_input_idx);
370 }
371
372 const char *pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
373 pa_scache_entry *e;
374
375 pa_assert(c);
376 pa_assert(id != PA_IDXSET_INVALID);
377
378 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
379 return NULL;
380
381 return e->name;
382 }
383
384 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
385 pa_scache_entry *e;
386
387 pa_assert(c);
388 pa_assert(name);
389
390 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
391 return PA_IDXSET_INVALID;
392
393 return e->index;
394 }
395
396 size_t pa_scache_total_size(pa_core *c) {
397 pa_scache_entry *e;
398 uint32_t idx;
399 size_t sum = 0;
400
401 pa_assert(c);
402
403 if (!c->scache || !pa_idxset_size(c->scache))
404 return 0;
405
406 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
407 if (e->memchunk.memblock)
408 sum += e->memchunk.length;
409
410 return sum;
411 }
412
413 void pa_scache_unload_unused(pa_core *c) {
414 pa_scache_entry *e;
415 time_t now;
416 uint32_t idx;
417
418 pa_assert(c);
419
420 if (!c->scache || !pa_idxset_size(c->scache))
421 return;
422
423 time(&now);
424
425 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
426
427 if (!e->lazy || !e->memchunk.memblock)
428 continue;
429
430 if (e->last_used_time + c->scache_idle_time > now)
431 continue;
432
433 pa_memblock_unref(e->memchunk.memblock);
434 pa_memchunk_reset(&e->memchunk);
435
436 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
437 }
438 }
439
440 static void add_file(pa_core *c, const char *pathname) {
441 struct stat st;
442 const char *e;
443
444 pa_core_assert_ref(c);
445 pa_assert(pathname);
446
447 e = pa_path_get_filename(pathname);
448
449 if (stat(pathname, &st) < 0) {
450 pa_log("stat('%s'): %s", pathname, pa_cstrerror(errno));
451 return;
452 }
453
454 #if defined(S_ISREG) && defined(S_ISLNK)
455 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
456 #endif
457 pa_scache_add_file_lazy(c, e, pathname, NULL);
458 }
459
460 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
461 DIR *dir;
462
463 pa_core_assert_ref(c);
464 pa_assert(pathname);
465
466 /* First try to open this as directory */
467 if (!(dir = opendir(pathname))) {
468 #ifdef HAVE_GLOB_H
469 glob_t p;
470 unsigned int i;
471 /* If that fails, try to open it as shell glob */
472
473 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
474 pa_log("failed to open directory '%s': %s", pathname, pa_cstrerror(errno));
475 return -1;
476 }
477
478 for (i = 0; i < p.gl_pathc; i++)
479 add_file(c, p.gl_pathv[i]);
480
481 globfree(&p);
482 #else
483 return -1;
484 #endif
485 } else {
486 struct dirent *e;
487
488 while ((e = readdir(dir))) {
489 char p[PATH_MAX];
490
491 if (e->d_name[0] == '.')
492 continue;
493
494 pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
495 add_file(c, p);
496 }
497
498 closedir(dir);
499 }
500
501 return 0;
502 }