4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
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.
16 You should have received a copy of the GNU 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
32 #include "sink-input.h"
34 #include "sample-util.h"
35 #include "play-memchunk.h"
37 #include "subscribe.h"
40 #define UNLOAD_POLL_TIME 2
42 static void timeout_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
43 struct pa_core
*c
= userdata
;
45 assert(c
&& c
->mainloop
== m
&& c
->scache_auto_unload_event
== e
);
47 pa_scache_unload_unused(c
);
49 gettimeofday(&ntv
, NULL
);
50 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
51 m
->time_restart(e
, &ntv
);
54 static void free_entry(struct pa_scache_entry
*e
) {
56 pa_namereg_unregister(e
->core
, e
->name
);
57 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
59 if (e
->memchunk
.memblock
)
60 pa_memblock_unref(e
->memchunk
.memblock
);
64 int pa_scache_add_item(struct pa_core
*c
, const char *name
, struct pa_sample_spec
*ss
, struct pa_memchunk
*chunk
, uint32_t *index
, int auto_unload
) {
65 struct pa_scache_entry
*e
;
69 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
71 if (e
->memchunk
.memblock
)
72 pa_memblock_unref(e
->memchunk
.memblock
);
77 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
79 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
84 e
->name
= pa_xstrdup(name
);
88 e
->volume
= PA_VOLUME_NORM
;
89 e
->auto_unload
= auto_unload
;
90 e
->last_used_time
= 0;
95 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
99 pa_memblock_ref(e
->memchunk
.memblock
);
101 e
->memchunk
.memblock
= NULL
;
102 e
->memchunk
.index
= e
->memchunk
.length
= 0;
107 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
111 pa_idxset_put(c
->scache
, e
, &e
->index
);
113 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
115 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
120 if (!c
->scache_auto_unload_event
) {
122 gettimeofday(&ntv
, NULL
);
123 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
124 c
->scache_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
130 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
131 struct pa_scache_entry
*e
;
134 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
137 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
144 static void free_cb(void *p
, void *userdata
) {
145 struct pa_scache_entry
*e
= p
;
150 void pa_scache_free(struct pa_core
*c
) {
154 pa_idxset_free(c
->scache
, free_cb
, NULL
);
158 if (c
->scache_auto_unload_event
)
159 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
162 int pa_scache_play_item(struct pa_core
*c
, const char *name
, struct pa_sink
*sink
, uint32_t volume
) {
163 struct pa_scache_entry
*e
;
164 assert(c
&& name
&& sink
);
166 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
169 if (!e
->memchunk
.memblock
)
172 if (pa_play_memchunk(sink
, name
, &e
->sample_spec
, &e
->memchunk
, pa_volume_multiply(volume
, e
->volume
)) < 0)
176 time(&e
->last_used_time
);
181 const char * pa_scache_get_name_by_id(struct pa_core
*c
, uint32_t id
) {
182 struct pa_scache_entry
*e
;
183 assert(c
&& id
!= PA_IDXSET_INVALID
);
185 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
191 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
192 struct pa_scache_entry
*e
;
195 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
196 return PA_IDXSET_INVALID
;
201 uint32_t pa_scache_total_size(struct pa_core
*c
) {
202 struct pa_scache_entry
*e
;
209 for (e
= pa_idxset_first(c
->scache
, &index
); e
; e
= pa_idxset_next(c
->scache
, &index
))
210 sum
+= e
->memchunk
.length
;
215 static int unload_func(void *p
, uint32_t index
, int *del
, void *userdata
) {
216 struct pa_scache_entry
*e
= p
;
217 time_t *now
= userdata
;
223 if (e
->last_used_time
+ e
->core
->scache_idle_time
> *now
)
231 void pa_scache_unload_unused(struct pa_core
*c
) {
240 pa_idxset_foreach(c
->scache
, unload_func
, &now
);