4 This file is part of PulseAudio.
6 Copyright 2004-2008 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
32 #include <sys/types.h>
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>
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>
64 #include "core-scache.h"
66 #define UNLOAD_POLL_TIME 5
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
;
73 pa_assert(c
->mainloop
== m
);
74 pa_assert(c
->scache_auto_unload_event
== e
);
76 pa_scache_unload_unused(c
);
78 pa_gettimeofday(&ntv
);
79 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
80 m
->time_restart(e
, &ntv
);
83 static void free_entry(pa_scache_entry
*e
) {
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
);
89 pa_xfree(e
->filename
);
90 if (e
->memchunk
.memblock
)
91 pa_memblock_unref(e
->memchunk
.memblock
);
93 pa_proplist_free(e
->proplist
);
97 static pa_scache_entry
* scache_add_item(pa_core
*c
, const char *name
) {
103 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
104 if (e
->memchunk
.memblock
)
105 pa_memblock_unref(e
->memchunk
.memblock
);
107 pa_xfree(e
->filename
);
108 pa_proplist_clear(e
->proplist
);
110 pa_assert(e
->core
== c
);
112 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
114 e
= pa_xnew(pa_scache_entry
, 1);
116 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
121 e
->name
= pa_xstrdup(name
);
123 e
->proplist
= pa_proplist_new();
126 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
128 pa_idxset_put(c
->scache
, e
, &e
->index
);
130 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
133 e
->last_used_time
= 0;
134 e
->memchunk
.memblock
= NULL
;
135 e
->memchunk
.index
= e
->memchunk
.length
= 0;
138 e
->last_used_time
= 0;
140 memset(&e
->sample_spec
, 0, sizeof(e
->sample_spec
));
141 pa_channel_map_init(&e
->channel_map
);
142 pa_cvolume_reset(&e
->volume
, PA_CHANNELS_MAX
);
144 pa_proplist_sets(e
->proplist
, PA_PROP_MEDIA_ROLE
, "event");
149 int pa_scache_add_item(
152 const pa_sample_spec
*ss
,
153 const pa_channel_map
*map
,
154 const pa_memchunk
*chunk
,
159 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
164 pa_assert(!ss
|| pa_sample_spec_valid(ss
));
165 pa_assert(!map
|| (pa_channel_map_valid(map
) && ss
&& ss
->channels
== map
->channels
));
168 if (!(map
= pa_channel_map_init_auto(&tmap
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
)))
171 if (chunk
&& chunk
->length
> PA_SCACHE_ENTRY_SIZE_MAX
)
174 if (!(e
= scache_add_item(c
, name
)))
177 memset(&e
->sample_spec
, 0, sizeof(e
->sample_spec
));
178 pa_channel_map_init(&e
->channel_map
);
181 e
->sample_spec
= *ss
;
182 e
->volume
.channels
= e
->sample_spec
.channels
;
186 e
->channel_map
= *map
;
189 e
->memchunk
= *chunk
;
190 pa_memblock_ref(e
->memchunk
.memblock
);
194 pa_proplist_update(e
->proplist
, PA_UPDATE_REPLACE
, p
);
199 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
200 name
, e
->index
, (unsigned long) e
->memchunk
.length
,
201 pa_sample_spec_snprint(st
, sizeof(st
), &e
->sample_spec
));
206 int pa_scache_add_file(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
216 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
224 if (pa_sound_file_load(c
->mempool
, filename
, &ss
, &map
, &chunk
) < 0)
227 p
= pa_proplist_new();
228 pa_proplist_sets(p
, PA_PROP_MEDIA_FILENAME
, filename
);
229 r
= pa_scache_add_item(c
, name
, &ss
, &map
, &chunk
, p
, idx
);
230 pa_memblock_unref(chunk
.memblock
);
236 int pa_scache_add_file_lazy(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
242 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
250 if (!(e
= scache_add_item(c
, name
)))
254 e
->filename
= pa_xstrdup(filename
);
256 pa_proplist_sets(e
->proplist
, PA_PROP_MEDIA_FILENAME
, filename
);
258 if (!c
->scache_auto_unload_event
) {
260 pa_gettimeofday(&ntv
);
261 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
262 c
->scache_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
271 int pa_scache_remove_item(pa_core
*c
, const char *name
) {
277 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
280 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
283 pa_log_debug("Removed sample \"%s\"", name
);
290 static void free_cb(void *p
, PA_GCC_UNUSED
void *userdata
) {
291 pa_scache_entry
*e
= p
;
297 void pa_scache_free(pa_core
*c
) {
301 pa_idxset_free(c
->scache
, free_cb
, NULL
);
305 if (c
->scache_auto_unload_event
)
306 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
309 int pa_scache_play_item(pa_core
*c
, const char *name
, pa_sink
*sink
, pa_volume_t volume
, pa_proplist
*p
, uint32_t *sink_input_idx
) {
318 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
321 if (e
->lazy
&& !e
->memchunk
.memblock
) {
322 if (pa_sound_file_load(c
->mempool
, e
->filename
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
) < 0)
325 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
327 if (e
->volume
.channels
> e
->sample_spec
.channels
)
328 e
->volume
.channels
= e
->sample_spec
.channels
;
331 if (!e
->memchunk
.memblock
)
334 pa_log_debug("Playing sample \"%s\" on \"%s\"", name
, sink
->name
);
336 pa_cvolume_set(&r
, e
->volume
.channels
, volume
);
337 pa_sw_cvolume_multiply(&r
, &r
, &e
->volume
);
339 merged
= pa_proplist_new();
341 pa_proplist_setf(merged
, PA_PROP_MEDIA_NAME
, "Sample %s", name
);
343 pa_proplist_update(merged
, PA_UPDATE_REPLACE
, e
->proplist
);
346 pa_proplist_update(merged
, PA_UPDATE_REPLACE
, p
);
348 if (pa_play_memchunk(sink
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
, &r
, merged
, sink_input_idx
) < 0) {
349 pa_proplist_free(merged
);
353 pa_proplist_free(merged
);
356 time(&e
->last_used_time
);
361 int pa_scache_play_item_by_name(pa_core
*c
, const char *name
, const char*sink_name
, pa_bool_t autoload
, pa_volume_t volume
, pa_proplist
*p
, uint32_t *sink_input_idx
) {
367 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
, autoload
)))
370 return pa_scache_play_item(c
, name
, sink
, volume
, p
, sink_input_idx
);
373 const char *pa_scache_get_name_by_id(pa_core
*c
, uint32_t id
) {
377 pa_assert(id
!= PA_IDXSET_INVALID
);
379 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
385 uint32_t pa_scache_get_id_by_name(pa_core
*c
, const char *name
) {
391 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
392 return PA_IDXSET_INVALID
;
397 size_t pa_scache_total_size(pa_core
*c
) {
404 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
407 for (e
= pa_idxset_first(c
->scache
, &idx
); e
; e
= pa_idxset_next(c
->scache
, &idx
))
408 if (e
->memchunk
.memblock
)
409 sum
+= e
->memchunk
.length
;
414 void pa_scache_unload_unused(pa_core
*c
) {
421 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
426 for (e
= pa_idxset_first(c
->scache
, &idx
); e
; e
= pa_idxset_next(c
->scache
, &idx
)) {
428 if (!e
->lazy
|| !e
->memchunk
.memblock
)
431 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
434 pa_memblock_unref(e
->memchunk
.memblock
);
435 pa_memchunk_reset(&e
->memchunk
);
437 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
441 static void add_file(pa_core
*c
, const char *pathname
) {
445 pa_core_assert_ref(c
);
448 e
= pa_path_get_filename(pathname
);
450 if (stat(pathname
, &st
) < 0) {
451 pa_log("stat('%s'): %s", pathname
, pa_cstrerror(errno
));
455 #if defined(S_ISREG) && defined(S_ISLNK)
456 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
458 pa_scache_add_file_lazy(c
, e
, pathname
, NULL
);
461 int pa_scache_add_directory_lazy(pa_core
*c
, const char *pathname
) {
464 pa_core_assert_ref(c
);
467 /* First try to open this as directory */
468 if (!(dir
= opendir(pathname
))) {
472 /* If that fails, try to open it as shell glob */
474 if (glob(pathname
, GLOB_ERR
|GLOB_NOSORT
, NULL
, &p
) < 0) {
475 pa_log("failed to open directory '%s': %s", pathname
, pa_cstrerror(errno
));
479 for (i
= 0; i
< p
.gl_pathc
; i
++)
480 add_file(c
, p
.gl_pathv
[i
]);
489 while ((e
= readdir(dir
))) {
492 if (e
->d_name
[0] == '.')
495 pa_snprintf(p
, sizeof(p
), "%s/%s", pathname
, e
->d_name
);