]> code.delx.au - pulseaudio/blob - src/polypcore/core-scache.c
Don't include util.h from core-util.h as it is not needed by many users.
[pulseaudio] / src / polypcore / core-scache.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
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 <polyp/mainloop.h>
45 #include <polyp/channelmap.h>
46 #include <polyp/timeval.h>
47 #include <polyp/util.h>
48 #include <polyp/volume.h>
49 #include <polyp/xmalloc.h>
50
51 #include <polypcore/sink-input.h>
52 #include <polypcore/sample-util.h>
53 #include <polypcore/play-memchunk.h>
54 #include <polypcore/core-subscribe.h>
55 #include <polypcore/namereg.h>
56 #include <polypcore/sound-file.h>
57 #include <polypcore/core-util.h>
58 #include <polypcore/log.h>
59
60 #include "core-scache.h"
61
62 #define UNLOAD_POLL_TIME 2
63
64 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
65 pa_core *c = userdata;
66 struct timeval ntv;
67 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
68
69 pa_scache_unload_unused(c);
70
71 pa_gettimeofday(&ntv);
72 ntv.tv_sec += UNLOAD_POLL_TIME;
73 m->time_restart(e, &ntv);
74 }
75
76 static void free_entry(pa_scache_entry *e) {
77 assert(e);
78 pa_namereg_unregister(e->core, e->name);
79 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
80 pa_xfree(e->name);
81 pa_xfree(e->filename);
82 if (e->memchunk.memblock)
83 pa_memblock_unref(e->memchunk.memblock);
84 pa_xfree(e);
85 }
86
87 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
88 pa_scache_entry *e;
89 assert(c && name);
90
91 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
92 if (e->memchunk.memblock)
93 pa_memblock_unref(e->memchunk.memblock);
94
95 pa_xfree(e->filename);
96
97 assert(e->core == c);
98
99 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
100 } else {
101 e = pa_xmalloc(sizeof(pa_scache_entry));
102
103 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
104 pa_xfree(e);
105 return NULL;
106 }
107
108 e->name = pa_xstrdup(name);
109 e->core = c;
110
111 if (!c->scache) {
112 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
113 assert(c->scache);
114 }
115
116 pa_idxset_put(c->scache, e, &e->index);
117
118 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
119 }
120
121 e->last_used_time = 0;
122 e->memchunk.memblock = NULL;
123 e->memchunk.index = e->memchunk.length = 0;
124 e->filename = NULL;
125 e->lazy = 0;
126 e->last_used_time = 0;
127
128 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
129 pa_channel_map_init(&e->channel_map);
130 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
131
132 return e;
133 }
134
135 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) {
136 pa_scache_entry *e;
137 assert(c && name);
138
139 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
140 return -1;
141
142 if (!(e = scache_add_item(c, name)))
143 return -1;
144
145 if (ss) {
146 e->sample_spec = *ss;
147 pa_channel_map_init_auto(&e->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
148 e->volume.channels = e->sample_spec.channels;
149 }
150
151 if (map)
152 e->channel_map = *map;
153
154 if (chunk) {
155 e->memchunk = *chunk;
156 pa_memblock_ref(e->memchunk.memblock);
157 }
158
159 if (idx)
160 *idx = e->index;
161
162 return 0;
163 }
164
165 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
166 pa_sample_spec ss;
167 pa_channel_map map;
168 pa_memchunk chunk;
169 int r;
170
171 #ifdef OS_IS_WIN32
172 char buf[MAX_PATH];
173
174 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
175 filename = buf;
176 #endif
177
178 if (pa_sound_file_load(filename, &ss, &map, &chunk, c->memblock_stat) < 0)
179 return -1;
180
181 r = pa_scache_add_item(c, name, &ss, &map, &chunk, idx);
182 pa_memblock_unref(chunk.memblock);
183
184 return r;
185 }
186
187 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
188 pa_scache_entry *e;
189
190 #ifdef OS_IS_WIN32
191 char buf[MAX_PATH];
192
193 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
194 filename = buf;
195 #endif
196
197 assert(c && name);
198
199 if (!(e = scache_add_item(c, name)))
200 return -1;
201
202 e->lazy = 1;
203 e->filename = pa_xstrdup(filename);
204
205 if (!c->scache_auto_unload_event) {
206 struct timeval ntv;
207 pa_gettimeofday(&ntv);
208 ntv.tv_sec += UNLOAD_POLL_TIME;
209 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
210 }
211
212 if (idx)
213 *idx = e->index;
214
215 return 0;
216 }
217
218 int pa_scache_remove_item(pa_core *c, const char *name) {
219 pa_scache_entry *e;
220 assert(c && name);
221
222 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
223 return -1;
224
225 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
226 assert(0);
227
228 free_entry(e);
229 return 0;
230 }
231
232 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
233 pa_scache_entry *e = p;
234 assert(e);
235 free_entry(e);
236 }
237
238 void pa_scache_free(pa_core *c) {
239 assert(c);
240
241 if (c->scache) {
242 pa_idxset_free(c->scache, free_cb, NULL);
243 c->scache = NULL;
244 }
245
246 if (c->scache_auto_unload_event)
247 c->mainloop->time_free(c->scache_auto_unload_event);
248 }
249
250 int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume) {
251 pa_scache_entry *e;
252 char *t;
253 pa_cvolume r;
254
255 assert(c);
256 assert(name);
257 assert(sink);
258
259 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
260 return -1;
261
262 if (e->lazy && !e->memchunk.memblock) {
263 if (pa_sound_file_load(e->filename, &e->sample_spec, &e->channel_map, &e->memchunk, c->memblock_stat) < 0)
264 return -1;
265
266 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
267
268 if (e->volume.channels > e->sample_spec.channels)
269 e->volume.channels = e->sample_spec.channels;
270 }
271
272 if (!e->memchunk.memblock)
273 return -1;
274
275 t = pa_sprintf_malloc("sample:%s", name);
276
277 pa_cvolume_set(&r, e->volume.channels, volume);
278 pa_sw_cvolume_multiply(&r, &r, &e->volume);
279
280 if (pa_play_memchunk(sink, t, &e->sample_spec, &e->channel_map, &e->memchunk, &r) < 0) {
281 pa_xfree(t);
282 return -1;
283 }
284
285 pa_xfree(t);
286
287 if (e->lazy)
288 time(&e->last_used_time);
289
290 return 0;
291 }
292
293 const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
294 pa_scache_entry *e;
295 assert(c && id != PA_IDXSET_INVALID);
296
297 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
298 return NULL;
299
300 return e->name;
301 }
302
303 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
304 pa_scache_entry *e;
305 assert(c && name);
306
307 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
308 return PA_IDXSET_INVALID;
309
310 return e->index;
311 }
312
313 uint32_t pa_scache_total_size(pa_core *c) {
314 pa_scache_entry *e;
315 uint32_t idx, sum = 0;
316 assert(c);
317
318 if (!c->scache || !pa_idxset_size(c->scache))
319 return 0;
320
321 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
322 if (e->memchunk.memblock)
323 sum += e->memchunk.length;
324
325 return sum;
326 }
327
328 void pa_scache_unload_unused(pa_core *c) {
329 pa_scache_entry *e;
330 time_t now;
331 uint32_t idx;
332 assert(c);
333
334 if (!c->scache || !pa_idxset_size(c->scache))
335 return;
336
337 time(&now);
338
339 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
340
341 if (!e->lazy || !e->memchunk.memblock)
342 continue;
343
344 if (e->last_used_time + c->scache_idle_time > now)
345 continue;
346
347 pa_memblock_unref(e->memchunk.memblock);
348 e->memchunk.memblock = NULL;
349 e->memchunk.index = e->memchunk.length = 0;
350
351 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
352 }
353 }
354
355 static void add_file(pa_core *c, const char *pathname) {
356 struct stat st;
357 const char *e;
358
359 e = pa_path_get_filename(pathname);
360
361 if (stat(pathname, &st) < 0) {
362 pa_log(__FILE__": stat('%s') failed: %s", pathname, strerror(errno));
363 return;
364 }
365
366 #if defined(S_ISREG) && defined(S_ISLNK)
367 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
368 #endif
369 pa_scache_add_file_lazy(c, e, pathname, NULL);
370 }
371
372 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
373 DIR *dir;
374 assert(c && pathname);
375
376 /* First try to open this as directory */
377 if (!(dir = opendir(pathname))) {
378 #ifdef HAVE_GLOB_H
379 glob_t p;
380 unsigned int i;
381 /* If that fails, try to open it as shell glob */
382
383 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
384 pa_log(__FILE__": Failed to open directory: %s", strerror(errno));
385 return -1;
386 }
387
388 for (i = 0; i < p.gl_pathc; i++)
389 add_file(c, p.gl_pathv[i]);
390
391 globfree(&p);
392 #else
393 return -1;
394 #endif
395 } else {
396 struct dirent *e;
397
398 while ((e = readdir(dir))) {
399 char p[PATH_MAX];
400
401 if (e->d_name[0] == '.')
402 continue;
403
404 snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
405 add_file(c, p);
406 }
407 }
408
409 closedir(dir);
410 return 0;
411 }