]> code.delx.au - pulseaudio/blob - src/pulsecore/core-scache.c
deal with a possibly failing pa_channel_map_init_auto() correctly
[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 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
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 pa_xfree(e);
93 }
94
95 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
96 pa_scache_entry *e;
97
98 pa_assert(c);
99 pa_assert(name);
100
101 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
102 if (e->memchunk.memblock)
103 pa_memblock_unref(e->memchunk.memblock);
104
105 pa_xfree(e->filename);
106
107 pa_assert(e->core == c);
108
109 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
110 } else {
111 e = pa_xnew(pa_scache_entry, 1);
112
113 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
114 pa_xfree(e);
115 return NULL;
116 }
117
118 e->name = pa_xstrdup(name);
119 e->core = c;
120
121 if (!c->scache) {
122 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
123 pa_assert(c->scache);
124 }
125
126 pa_idxset_put(c->scache, e, &e->index);
127
128 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
129 }
130
131 e->last_used_time = 0;
132 e->memchunk.memblock = NULL;
133 e->memchunk.index = e->memchunk.length = 0;
134 e->filename = NULL;
135 e->lazy = 0;
136 e->last_used_time = 0;
137
138 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
139 pa_channel_map_init(&e->channel_map);
140 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
141
142 return e;
143 }
144
145 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) {
146 pa_scache_entry *e;
147 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
148 pa_channel_map tmap;
149
150 pa_assert(c);
151 pa_assert(name);
152 pa_assert(!ss || pa_sample_spec_valid(ss));
153 pa_assert(!map || (pa_channel_map_valid(map) && ss && ss->channels == map->channels));
154
155 if (ss && !map)
156 if (!(map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT)))
157 return -1;
158
159 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
160 return -1;
161
162 if (!(e = scache_add_item(c, name)))
163 return -1;
164
165 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
166 pa_channel_map_init(&e->channel_map);
167
168 if (ss) {
169 e->sample_spec = *ss;
170 e->volume.channels = e->sample_spec.channels;
171 }
172
173 if (map)
174 e->channel_map = *map;
175
176 if (chunk) {
177 e->memchunk = *chunk;
178 pa_memblock_ref(e->memchunk.memblock);
179 }
180
181 if (idx)
182 *idx = e->index;
183
184 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
185 name, e->index, (unsigned long) e->memchunk.length,
186 pa_sample_spec_snprint(st, sizeof(st), &e->sample_spec));
187
188 return 0;
189 }
190
191 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
192 pa_sample_spec ss;
193 pa_channel_map map;
194 pa_memchunk chunk;
195 int r;
196
197 #ifdef OS_IS_WIN32
198 char buf[MAX_PATH];
199
200 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
201 filename = buf;
202 #endif
203
204 pa_assert(c);
205 pa_assert(name);
206 pa_assert(filename);
207
208 if (pa_sound_file_load(c->mempool, filename, &ss, &map, &chunk) < 0)
209 return -1;
210
211 r = pa_scache_add_item(c, name, &ss, &map, &chunk, idx);
212 pa_memblock_unref(chunk.memblock);
213
214 return r;
215 }
216
217 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
218 pa_scache_entry *e;
219
220 #ifdef OS_IS_WIN32
221 char buf[MAX_PATH];
222
223 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
224 filename = buf;
225 #endif
226
227 pa_assert(c);
228 pa_assert(name);
229 pa_assert(filename);
230
231 if (!(e = scache_add_item(c, name)))
232 return -1;
233
234 e->lazy = 1;
235 e->filename = pa_xstrdup(filename);
236
237 if (!c->scache_auto_unload_event) {
238 struct timeval ntv;
239 pa_gettimeofday(&ntv);
240 ntv.tv_sec += UNLOAD_POLL_TIME;
241 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
242 }
243
244 if (idx)
245 *idx = e->index;
246
247 return 0;
248 }
249
250 int pa_scache_remove_item(pa_core *c, const char *name) {
251 pa_scache_entry *e;
252
253 pa_assert(c);
254 pa_assert(name);
255
256 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
257 return -1;
258
259 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
260 pa_assert(0);
261
262 pa_log_debug("Removed sample \"%s\"", name);
263
264 free_entry(e);
265
266 return 0;
267 }
268
269 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
270 pa_scache_entry *e = p;
271 pa_assert(e);
272
273 free_entry(e);
274 }
275
276 void pa_scache_free(pa_core *c) {
277 pa_assert(c);
278
279 if (c->scache) {
280 pa_idxset_free(c->scache, free_cb, NULL);
281 c->scache = NULL;
282 }
283
284 if (c->scache_auto_unload_event)
285 c->mainloop->time_free(c->scache_auto_unload_event);
286 }
287
288 int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume) {
289 pa_scache_entry *e;
290 char *t;
291 pa_cvolume r;
292
293 pa_assert(c);
294 pa_assert(name);
295 pa_assert(sink);
296
297 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
298 return -1;
299
300 if (e->lazy && !e->memchunk.memblock) {
301 if (pa_sound_file_load(c->mempool, e->filename, &e->sample_spec, &e->channel_map, &e->memchunk) < 0)
302 return -1;
303
304 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
305
306 if (e->volume.channels > e->sample_spec.channels)
307 e->volume.channels = e->sample_spec.channels;
308 }
309
310 if (!e->memchunk.memblock)
311 return -1;
312
313 pa_log_debug("Playing sample \"%s\" on \"%s\"", name, sink->name);
314
315 t = pa_sprintf_malloc("sample:%s", name);
316
317 pa_cvolume_set(&r, e->volume.channels, volume);
318 pa_sw_cvolume_multiply(&r, &r, &e->volume);
319
320 if (pa_play_memchunk(sink, t, &e->sample_spec, &e->channel_map, &e->memchunk, &r) < 0) {
321 pa_xfree(t);
322 return -1;
323 }
324
325 pa_xfree(t);
326
327 if (e->lazy)
328 time(&e->last_used_time);
329
330 return 0;
331 }
332
333 int pa_scache_play_item_by_name(pa_core *c, const char *name, const char*sink_name, pa_volume_t volume, int autoload) {
334 pa_sink *sink;
335
336 pa_assert(c);
337 pa_assert(name);
338
339 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, autoload)))
340 return -1;
341
342 return pa_scache_play_item(c, name, sink, volume);
343 }
344
345 const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
346 pa_scache_entry *e;
347
348 pa_assert(c);
349 pa_assert(id != PA_IDXSET_INVALID);
350
351 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
352 return NULL;
353
354 return e->name;
355 }
356
357 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
358 pa_scache_entry *e;
359
360 pa_assert(c);
361 pa_assert(name);
362
363 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
364 return PA_IDXSET_INVALID;
365
366 return e->index;
367 }
368
369 uint32_t pa_scache_total_size(pa_core *c) {
370 pa_scache_entry *e;
371 uint32_t idx, sum = 0;
372
373 pa_assert(c);
374
375 if (!c->scache || !pa_idxset_size(c->scache))
376 return 0;
377
378 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
379 if (e->memchunk.memblock)
380 sum += e->memchunk.length;
381
382 return sum;
383 }
384
385 void pa_scache_unload_unused(pa_core *c) {
386 pa_scache_entry *e;
387 time_t now;
388 uint32_t idx;
389
390 pa_assert(c);
391
392 if (!c->scache || !pa_idxset_size(c->scache))
393 return;
394
395 time(&now);
396
397 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
398
399 if (!e->lazy || !e->memchunk.memblock)
400 continue;
401
402 if (e->last_used_time + c->scache_idle_time > now)
403 continue;
404
405 pa_memblock_unref(e->memchunk.memblock);
406 e->memchunk.memblock = NULL;
407 e->memchunk.index = e->memchunk.length = 0;
408
409 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
410 }
411 }
412
413 static void add_file(pa_core *c, const char *pathname) {
414 struct stat st;
415 const char *e;
416
417 pa_core_assert_ref(c);
418 pa_assert(pathname);
419
420 e = pa_path_get_filename(pathname);
421
422 if (stat(pathname, &st) < 0) {
423 pa_log("stat('%s'): %s", pathname, pa_cstrerror(errno));
424 return;
425 }
426
427 #if defined(S_ISREG) && defined(S_ISLNK)
428 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
429 #endif
430 pa_scache_add_file_lazy(c, e, pathname, NULL);
431 }
432
433 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
434 DIR *dir;
435
436 pa_core_assert_ref(c);
437 pa_assert(pathname);
438
439 /* First try to open this as directory */
440 if (!(dir = opendir(pathname))) {
441 #ifdef HAVE_GLOB_H
442 glob_t p;
443 unsigned int i;
444 /* If that fails, try to open it as shell glob */
445
446 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
447 pa_log("failed to open directory '%s': %s", pathname, pa_cstrerror(errno));
448 return -1;
449 }
450
451 for (i = 0; i < p.gl_pathc; i++)
452 add_file(c, p.gl_pathv[i]);
453
454 globfree(&p);
455 #else
456 return -1;
457 #endif
458 } else {
459 struct dirent *e;
460
461 while ((e = readdir(dir))) {
462 char p[PATH_MAX];
463
464 if (e->d_name[0] == '.')
465 continue;
466
467 pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
468 add_file(c, p);
469 }
470 }
471
472 closedir(dir);
473 return 0;
474 }