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