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
29 #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>
50 #include <pulse/rtclock.h>
52 #include <pulsecore/sink-input.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-rtclock.h>
58 #include <pulsecore/core-util.h>
59 #include <pulsecore/log.h>
60 #include <pulsecore/core-error.h>
61 #include <pulsecore/macro.h>
63 #include "core-scache.h"
65 #define UNLOAD_POLL_TIME (60 * PA_USEC_PER_SEC)
67 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
68 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_core_rttime_restart(c
, e
, pa_rtclock_now() + UNLOAD_POLL_TIME
);
79 static void free_entry(pa_scache_entry
*e
) {
82 pa_namereg_unregister(e
->core
, e
->name
);
83 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
85 pa_xfree(e
->filename
);
86 if (e
->memchunk
.memblock
)
87 pa_memblock_unref(e
->memchunk
.memblock
);
89 pa_proplist_free(e
->proplist
);
93 static pa_scache_entry
* scache_add_item(pa_core
*c
, const char *name
) {
99 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
))) {
100 if (e
->memchunk
.memblock
)
101 pa_memblock_unref(e
->memchunk
.memblock
);
103 pa_xfree(e
->filename
);
104 pa_proplist_clear(e
->proplist
);
106 pa_assert(e
->core
== c
);
108 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
110 e
= pa_xnew(pa_scache_entry
, 1);
112 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, true)) {
117 e
->name
= pa_xstrdup(name
);
119 e
->proplist
= pa_proplist_new();
121 pa_idxset_put(c
->scache
, e
, &e
->index
);
123 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
126 e
->last_used_time
= 0;
127 pa_memchunk_reset(&e
->memchunk
);
130 e
->last_used_time
= 0;
132 pa_sample_spec_init(&e
->sample_spec
);
133 pa_channel_map_init(&e
->channel_map
);
134 pa_cvolume_init(&e
->volume
);
135 e
->volume_is_set
= false;
137 pa_proplist_sets(e
->proplist
, PA_PROP_MEDIA_ROLE
, "event");
142 int pa_scache_add_item(
145 const pa_sample_spec
*ss
,
146 const pa_channel_map
*map
,
147 const pa_memchunk
*chunk
,
152 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
157 pa_assert(!ss
|| pa_sample_spec_valid(ss
));
158 pa_assert(!map
|| (pa_channel_map_valid(map
) && ss
&& pa_channel_map_compatible(map
, ss
)));
161 pa_channel_map_init_extend(&tmap
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
165 if (chunk
&& chunk
->length
> PA_SCACHE_ENTRY_SIZE_MAX
)
168 if (!(e
= scache_add_item(c
, name
)))
171 pa_sample_spec_init(&e
->sample_spec
);
172 pa_channel_map_init(&e
->channel_map
);
173 pa_cvolume_init(&e
->volume
);
174 e
->volume_is_set
= false;
177 e
->sample_spec
= *ss
;
178 pa_cvolume_reset(&e
->volume
, ss
->channels
);
182 e
->channel_map
= *map
;
185 e
->memchunk
= *chunk
;
186 pa_memblock_ref(e
->memchunk
.memblock
);
190 pa_proplist_update(e
->proplist
, PA_UPDATE_REPLACE
, p
);
195 pa_log_debug("Created sample \"%s\" (#%d), %lu bytes with sample spec %s",
196 name
, e
->index
, (unsigned long) e
->memchunk
.length
,
197 pa_sample_spec_snprint(st
, sizeof(st
), &e
->sample_spec
));
202 int pa_scache_add_file(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
212 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
220 p
= pa_proplist_new();
221 pa_proplist_sets(p
, PA_PROP_MEDIA_FILENAME
, filename
);
223 if (pa_sound_file_load(c
->mempool
, filename
, &ss
, &map
, &chunk
, p
) < 0) {
228 r
= pa_scache_add_item(c
, name
, &ss
, &map
, &chunk
, p
, idx
);
229 pa_memblock_unref(chunk
.memblock
);
235 int pa_scache_add_file_lazy(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
241 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
249 if (!(e
= scache_add_item(c
, name
)))
253 e
->filename
= pa_xstrdup(filename
);
255 pa_proplist_sets(e
->proplist
, PA_PROP_MEDIA_FILENAME
, filename
);
257 if (!c
->scache_auto_unload_event
)
258 c
->scache_auto_unload_event
= pa_core_rttime_new(c
, pa_rtclock_now() + UNLOAD_POLL_TIME
, timeout_callback
, c
);
266 int pa_scache_remove_item(pa_core
*c
, const char *name
) {
272 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
)))
275 pa_assert_se(pa_idxset_remove_by_data(c
->scache
, e
, NULL
) == e
);
277 pa_log_debug("Removed sample \"%s\"", name
);
284 void pa_scache_free_all(pa_core
*c
) {
287 pa_idxset_remove_all(c
->scache
, (pa_free_cb_t
) free_entry
);
289 if (c
->scache_auto_unload_event
) {
290 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
291 c
->scache_auto_unload_event
= NULL
;
295 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
) {
305 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
)))
308 merged
= pa_proplist_new();
309 pa_proplist_sets(merged
, PA_PROP_MEDIA_NAME
, name
);
310 pa_proplist_sets(merged
, PA_PROP_EVENT_ID
, name
);
312 if (e
->lazy
&& !e
->memchunk
.memblock
) {
313 pa_channel_map old_channel_map
= e
->channel_map
;
315 if (pa_sound_file_load(c
->mempool
, e
->filename
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
, merged
) < 0)
318 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
320 if (e
->volume_is_set
) {
321 if (pa_cvolume_valid(&e
->volume
))
322 pa_cvolume_remap(&e
->volume
, &old_channel_map
, &e
->channel_map
);
324 pa_cvolume_reset(&e
->volume
, e
->sample_spec
.channels
);
328 if (!e
->memchunk
.memblock
)
331 pa_log_debug("Playing sample \"%s\" on \"%s\"", name
, sink
->name
);
335 if (e
->volume_is_set
&& PA_VOLUME_IS_VALID(volume
)) {
336 pa_cvolume_set(&r
, e
->sample_spec
.channels
, volume
);
337 pa_sw_cvolume_multiply(&r
, &r
, &e
->volume
);
338 } else if (e
->volume_is_set
)
340 else if (PA_VOLUME_IS_VALID(volume
))
341 pa_cvolume_set(&r
, e
->sample_spec
.channels
, volume
);
345 pa_proplist_update(merged
, PA_UPDATE_REPLACE
, e
->proplist
);
348 pa_proplist_update(merged
, PA_UPDATE_REPLACE
, p
);
350 if (pa_play_memchunk(sink
,
351 &e
->sample_spec
, &e
->channel_map
,
353 pass_volume
? &r
: NULL
,
355 PA_SINK_INPUT_NO_CREATE_ON_SUSPEND
|PA_SINK_INPUT_KILL_ON_SUSPEND
, sink_input_idx
) < 0)
358 pa_proplist_free(merged
);
361 time(&e
->last_used_time
);
366 pa_proplist_free(merged
);
370 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
) {
376 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
)))
379 return pa_scache_play_item(c
, name
, sink
, volume
, p
, sink_input_idx
);
382 const char *pa_scache_get_name_by_id(pa_core
*c
, uint32_t id
) {
386 pa_assert(id
!= PA_IDXSET_INVALID
);
388 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
394 uint32_t pa_scache_get_id_by_name(pa_core
*c
, const char *name
) {
400 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
)))
401 return PA_IDXSET_INVALID
;
406 size_t pa_scache_total_size(pa_core
*c
) {
413 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
416 PA_IDXSET_FOREACH(e
, c
->scache
, idx
)
417 if (e
->memchunk
.memblock
)
418 sum
+= e
->memchunk
.length
;
423 void pa_scache_unload_unused(pa_core
*c
) {
430 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
435 PA_IDXSET_FOREACH(e
, c
->scache
, idx
) {
437 if (!e
->lazy
|| !e
->memchunk
.memblock
)
440 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
443 pa_memblock_unref(e
->memchunk
.memblock
);
444 pa_memchunk_reset(&e
->memchunk
);
446 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
450 static void add_file(pa_core
*c
, const char *pathname
) {
454 pa_core_assert_ref(c
);
457 e
= pa_path_get_filename(pathname
);
459 if (stat(pathname
, &st
) < 0) {
460 pa_log("stat('%s'): %s", pathname
, pa_cstrerror(errno
));
464 #if defined(S_ISREG) && defined(S_ISLNK)
465 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
467 pa_scache_add_file_lazy(c
, e
, pathname
, NULL
);
470 int pa_scache_add_directory_lazy(pa_core
*c
, const char *pathname
) {
473 pa_core_assert_ref(c
);
476 /* First try to open this as directory */
477 if (!(dir
= opendir(pathname
))) {
481 /* If that fails, try to open it as shell glob */
483 if (glob(pathname
, GLOB_ERR
|GLOB_NOSORT
, NULL
, &p
) < 0) {
484 pa_log("failed to open directory '%s': %s", pathname
, pa_cstrerror(errno
));
488 for (i
= 0; i
< p
.gl_pathc
; i
++)
489 add_file(c
, p
.gl_pathv
[i
]);
498 while ((e
= readdir(dir
))) {
501 if (e
->d_name
[0] == '.')
504 p
= pa_sprintf_malloc("%s" PA_PATH_SEP
"%s", pathname
, e
->d_name
);