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