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