2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <sys/types.h>
44 #include <pulse/mainloop.h>
45 #include <pulse/channelmap.h>
46 #include <pulse/timeval.h>
47 #include <pulse/util.h>
48 #include <pulse/volume.h>
49 #include <pulse/xmalloc.h>
51 #include <pulsecore/sink-input.h>
52 #include <pulsecore/sample-util.h>
53 #include <pulsecore/play-memchunk.h>
54 #include <pulsecore/core-subscribe.h>
55 #include <pulsecore/namereg.h>
56 #include <pulsecore/sound-file.h>
57 #include <pulsecore/core-util.h>
58 #include <pulsecore/log.h>
59 #include <pulsecore/core-error.h>
60 #include <pulsecore/macro.h>
62 #include "core-scache.h"
64 #define UNLOAD_POLL_TIME 60
66 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
67 pa_core
*c
= userdata
;
71 pa_assert(c
->mainloop
== m
);
72 pa_assert(c
->scache_auto_unload_event
== e
);
74 pa_scache_unload_unused(c
);
76 pa_gettimeofday(&ntv
);
77 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
78 m
->time_restart(e
, &ntv
);
81 static void free_entry(pa_scache_entry
*e
) {
84 pa_namereg_unregister(e
->core
, e
->name
);
85 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
87 pa_xfree(e
->filename
);
88 if (e
->memchunk
.memblock
)
89 pa_memblock_unref(e
->memchunk
.memblock
);
91 pa_proplist_free(e
->proplist
);
95 static pa_scache_entry
* scache_add_item(pa_core
*c
, const char *name
) {
101 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
))) {
102 if (e
->memchunk
.memblock
)
103 pa_memblock_unref(e
->memchunk
.memblock
);
105 pa_xfree(e
->filename
);
106 pa_proplist_clear(e
->proplist
);
108 pa_assert(e
->core
== c
);
110 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
112 e
= pa_xnew(pa_scache_entry
, 1);
114 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, TRUE
)) {
119 e
->name
= pa_xstrdup(name
);
121 e
->proplist
= pa_proplist_new();
123 pa_idxset_put(c
->scache
, e
, &e
->index
);
125 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
128 e
->last_used_time
= 0;
129 pa_memchunk_reset(&e
->memchunk
);
132 e
->last_used_time
= 0;
134 pa_sample_spec_init(&e
->sample_spec
);
135 pa_channel_map_init(&e
->channel_map
);
136 pa_cvolume_init(&e
->volume
);
137 e
->volume_is_set
= FALSE
;
139 pa_proplist_sets(e
->proplist
, PA_PROP_MEDIA_ROLE
, "event");
144 int pa_scache_add_item(
147 const pa_sample_spec
*ss
,
148 const pa_channel_map
*map
,
149 const pa_memchunk
*chunk
,
154 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
159 pa_assert(!ss
|| pa_sample_spec_valid(ss
));
160 pa_assert(!map
|| (pa_channel_map_valid(map
) && ss
&& pa_channel_map_compatible(map
, ss
)));
163 pa_channel_map_init_extend(&tmap
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
167 if (chunk
&& chunk
->length
> PA_SCACHE_ENTRY_SIZE_MAX
)
170 if (!(e
= scache_add_item(c
, name
)))
173 pa_sample_spec_init(&e
->sample_spec
);
174 pa_channel_map_init(&e
->channel_map
);
175 pa_cvolume_init(&e
->volume
);
176 e
->volume_is_set
= FALSE
;
179 e
->sample_spec
= *ss
;
180 pa_cvolume_reset(&e
->volume
, ss
->channels
);
184 e
->channel_map
= *map
;
187 e
->memchunk
= *chunk
;
188 pa_memblock_ref(e
->memchunk
.memblock
);
192 pa_proplist_update(e
->proplist
, PA_UPDATE_REPLACE
, p
);
197 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
198 name
, e
->index
, (unsigned long) e
->memchunk
.length
,
199 pa_sample_spec_snprint(st
, sizeof(st
), &e
->sample_spec
));
204 int pa_scache_add_file(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
214 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
222 p
= pa_proplist_new();
223 pa_proplist_sets(p
, PA_PROP_MEDIA_FILENAME
, filename
);
225 if (pa_sound_file_load(c
->mempool
, filename
, &ss
, &map
, &chunk
, p
) < 0) {
230 r
= pa_scache_add_item(c
, name
, &ss
, &map
, &chunk
, p
, idx
);
231 pa_memblock_unref(chunk
.memblock
);
237 int pa_scache_add_file_lazy(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
243 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
251 if (!(e
= scache_add_item(c
, name
)))
255 e
->filename
= pa_xstrdup(filename
);
257 pa_proplist_sets(e
->proplist
, PA_PROP_MEDIA_FILENAME
, filename
);
259 if (!c
->scache_auto_unload_event
) {
261 pa_gettimeofday(&ntv
);
262 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
263 c
->scache_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
272 int pa_scache_remove_item(pa_core
*c
, const char *name
) {
278 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
)))
281 pa_assert_se(pa_idxset_remove_by_data(c
->scache
, e
, NULL
) == e
);
283 pa_log_debug("Removed sample \"%s\"", name
);
290 void pa_scache_free_all(pa_core
*c
) {
295 while ((e
= pa_idxset_steal_first(c
->scache
, NULL
)))
298 if (c
->scache_auto_unload_event
) {
299 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
300 c
->scache_auto_unload_event
= NULL
;
304 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
) {
308 pa_bool_t pass_volume
;
314 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
)))
317 merged
= pa_proplist_new();
318 pa_proplist_setf(merged
, PA_PROP_MEDIA_NAME
, "Sample %s", name
);
320 if (e
->lazy
&& !e
->memchunk
.memblock
) {
321 pa_channel_map old_channel_map
= e
->channel_map
;
323 if (pa_sound_file_load(c
->mempool
, e
->filename
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
, merged
) < 0)
326 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
328 if (e
->volume_is_set
) {
329 if (pa_cvolume_valid(&e
->volume
))
330 pa_cvolume_remap(&e
->volume
, &old_channel_map
, &e
->channel_map
);
332 pa_cvolume_reset(&e
->volume
, e
->sample_spec
.channels
);
336 if (!e
->memchunk
.memblock
)
339 pa_log_debug("Playing sample \"%s\" on \"%s\"", name
, sink
->name
);
343 if (e
->volume_is_set
&& volume
!= (pa_volume_t
) -1) {
344 pa_cvolume_set(&r
, e
->sample_spec
.channels
, volume
);
345 pa_sw_cvolume_multiply(&r
, &r
, &e
->volume
);
346 } else if (e
->volume_is_set
)
348 else if (volume
!= (pa_volume_t
) -1)
349 pa_cvolume_set(&r
, e
->sample_spec
.channels
, volume
);
353 pa_proplist_update(merged
, PA_UPDATE_REPLACE
, e
->proplist
);
356 pa_proplist_update(merged
, PA_UPDATE_REPLACE
, p
);
358 if (pa_play_memchunk(sink
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
, pass_volume
? &r
: NULL
, merged
, sink_input_idx
) < 0)
361 pa_proplist_free(merged
);
364 time(&e
->last_used_time
);
369 pa_proplist_free(merged
);
373 int pa_scache_play_item_by_name(pa_core
*c
, const char *name
, const char*sink_name
, pa_volume_t volume
, pa_proplist
*p
, uint32_t *sink_input_idx
) {
379 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
)))
382 return pa_scache_play_item(c
, name
, sink
, volume
, p
, sink_input_idx
);
385 const char *pa_scache_get_name_by_id(pa_core
*c
, uint32_t id
) {
389 pa_assert(id
!= PA_IDXSET_INVALID
);
391 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
397 uint32_t pa_scache_get_id_by_name(pa_core
*c
, const char *name
) {
403 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
)))
404 return PA_IDXSET_INVALID
;
409 size_t pa_scache_total_size(pa_core
*c
) {
416 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
419 for (e
= pa_idxset_first(c
->scache
, &idx
); e
; e
= pa_idxset_next(c
->scache
, &idx
))
420 if (e
->memchunk
.memblock
)
421 sum
+= e
->memchunk
.length
;
426 void pa_scache_unload_unused(pa_core
*c
) {
433 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
438 for (e
= pa_idxset_first(c
->scache
, &idx
); e
; e
= pa_idxset_next(c
->scache
, &idx
)) {
440 if (!e
->lazy
|| !e
->memchunk
.memblock
)
443 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
446 pa_memblock_unref(e
->memchunk
.memblock
);
447 pa_memchunk_reset(&e
->memchunk
);
449 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
453 static void add_file(pa_core
*c
, const char *pathname
) {
457 pa_core_assert_ref(c
);
460 e
= pa_path_get_filename(pathname
);
462 if (stat(pathname
, &st
) < 0) {
463 pa_log("stat('%s'): %s", pathname
, pa_cstrerror(errno
));
467 #if defined(S_ISREG) && defined(S_ISLNK)
468 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
470 pa_scache_add_file_lazy(c
, e
, pathname
, NULL
);
473 int pa_scache_add_directory_lazy(pa_core
*c
, const char *pathname
) {
476 pa_core_assert_ref(c
);
479 /* First try to open this as directory */
480 if (!(dir
= opendir(pathname
))) {
484 /* If that fails, try to open it as shell glob */
486 if (glob(pathname
, GLOB_ERR
|GLOB_NOSORT
, NULL
, &p
) < 0) {
487 pa_log("failed to open directory '%s': %s", pathname
, pa_cstrerror(errno
));
491 for (i
= 0; i
< p
.gl_pathc
; i
++)
492 add_file(c
, p
.gl_pathv
[i
]);
501 while ((e
= readdir(dir
))) {
504 if (e
->d_name
[0] == '.')
507 pa_snprintf(p
, sizeof(p
), "%s/%s", pathname
, e
->d_name
);