]> code.delx.au - pulseaudio/blob - polyp/scache.c
rename some stuff
[pulseaudio] / polyp / scache.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "scache.h"
32 #include "sink-input.h"
33 #include "mainloop.h"
34 #include "sample-util.h"
35 #include "play-memchunk.h"
36 #include "xmalloc.h"
37 #include "subscribe.h"
38 #include "namereg.h"
39 #include "sound-file.h"
40
41 #define UNLOAD_POLL_TIME 2
42
43 static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
44 struct pa_core *c = userdata;
45 struct timeval ntv;
46 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
47
48 pa_scache_unload_unused(c);
49
50 gettimeofday(&ntv, NULL);
51 ntv.tv_sec += UNLOAD_POLL_TIME;
52 m->time_restart(e, &ntv);
53 }
54
55 static void free_entry(struct pa_scache_entry *e) {
56 assert(e);
57 pa_namereg_unregister(e->core, e->name);
58 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
59 pa_xfree(e->name);
60 pa_xfree(e->filename);
61 if (e->memchunk.memblock)
62 pa_memblock_unref(e->memchunk.memblock);
63 pa_xfree(e);
64 }
65
66 static struct pa_scache_entry* scache_add_item(struct pa_core *c, const char *name) {
67 struct pa_scache_entry *e;
68 assert(c && name);
69
70 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
71 if (e->memchunk.memblock)
72 pa_memblock_unref(e->memchunk.memblock);
73
74 pa_xfree(e->filename);
75
76 assert(e->core == c);
77
78 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
79 } else {
80 e = pa_xmalloc(sizeof(struct pa_scache_entry));
81
82 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
83 pa_xfree(e);
84 return NULL;
85 }
86
87 e->name = pa_xstrdup(name);
88 e->core = c;
89
90 if (!c->scache) {
91 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
92 assert(c->scache);
93 }
94
95 pa_idxset_put(c->scache, e, &e->index);
96
97 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
98 }
99
100 e->volume = PA_VOLUME_NORM;
101 e->last_used_time = 0;
102 e->memchunk.memblock = NULL;
103 e->memchunk.index = e->memchunk.length = 0;
104 e->filename = NULL;
105 e->lazy = 0;
106 e->last_used_time = 0;
107
108 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
109
110 return e;
111 }
112
113 int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) {
114 struct pa_scache_entry *e;
115 assert(c && name);
116
117 if (!(e = scache_add_item(c, name)))
118 return -1;
119
120 if (ss)
121 e->sample_spec = *ss;
122
123 if (chunk) {
124 e->memchunk = *chunk;
125 pa_memblock_ref(e->memchunk.memblock);
126 }
127
128 if (index)
129 *index = e->index;
130
131 return 0;
132 }
133
134 int pa_scache_add_file(struct pa_core *c, const char *name, const char *filename, uint32_t *index) {
135 struct pa_sample_spec ss;
136 struct pa_memchunk chunk;
137 int r;
138
139 if (pa_sound_file_load(filename, &ss, &chunk, c->memblock_stat) < 0)
140 return -1;
141
142 r = pa_scache_add_item(c, name, &ss, &chunk, index);
143 pa_memblock_unref(chunk.memblock);
144
145 return r;
146 }
147
148 int pa_scache_add_file_lazy(struct pa_core *c, const char *name, const char *filename, uint32_t *index) {
149 struct pa_scache_entry *e;
150 assert(c && name);
151
152 if (!(e = scache_add_item(c, name)))
153 return -1;
154
155 e->lazy = 1;
156 e->filename = pa_xstrdup(filename);
157
158 if (!c->scache_auto_unload_event) {
159 struct timeval ntv;
160 gettimeofday(&ntv, NULL);
161 ntv.tv_sec += UNLOAD_POLL_TIME;
162 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
163 }
164
165 return 0;
166 }
167
168 int pa_scache_remove_item(struct pa_core *c, const char *name) {
169 struct pa_scache_entry *e;
170 assert(c && name);
171
172 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
173 return -1;
174
175 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
176 assert(0);
177
178 free_entry(e);
179 return 0;
180 }
181
182 static void free_cb(void *p, void *userdata) {
183 struct pa_scache_entry *e = p;
184 assert(e);
185 free_entry(e);
186 }
187
188 void pa_scache_free(struct pa_core *c) {
189 assert(c);
190
191 if (c->scache) {
192 pa_idxset_free(c->scache, free_cb, NULL);
193 c->scache = NULL;
194 }
195
196 if (c->scache_auto_unload_event)
197 c->mainloop->time_free(c->scache_auto_unload_event);
198 }
199
200 int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) {
201 struct pa_scache_entry *e;
202 assert(c && name && sink);
203
204 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
205 return -1;
206
207 if (e->lazy && !e->memchunk.memblock) {
208 if (pa_sound_file_load(e->filename, &e->sample_spec, &e->memchunk, c->memblock_stat) < 0)
209 return -1;
210
211 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
212 }
213
214 if (!e->memchunk.memblock)
215 return -1;
216
217 if (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0)
218 return -1;
219
220 if (e->lazy)
221 time(&e->last_used_time);
222
223 return 0;
224 }
225
226 const char * pa_scache_get_name_by_id(struct pa_core *c, uint32_t id) {
227 struct pa_scache_entry *e;
228 assert(c && id != PA_IDXSET_INVALID);
229
230 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
231 return NULL;
232
233 return e->name;
234 }
235
236 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
237 struct pa_scache_entry *e;
238 assert(c && name);
239
240 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
241 return PA_IDXSET_INVALID;
242
243 return e->index;
244 }
245
246 uint32_t pa_scache_total_size(struct pa_core *c) {
247 struct pa_scache_entry *e;
248 uint32_t index, sum = 0;
249 assert(c);
250
251 if (!c->scache || !pa_idxset_ncontents(c->scache))
252 return 0;
253
254 for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index))
255 if (e->memchunk.memblock)
256 sum += e->memchunk.length;
257
258 return sum;
259 }
260
261 void pa_scache_unload_unused(struct pa_core *c) {
262 struct pa_scache_entry *e;
263 time_t now;
264 uint32_t index;
265 assert(c);
266
267 if (!c->scache || !pa_idxset_ncontents(c->scache))
268 return;
269
270 time(&now);
271
272 for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index)) {
273
274 if (!e->lazy || !e->memchunk.memblock)
275 continue;
276
277 if (e->last_used_time + c->scache_idle_time > now)
278 continue;
279
280 pa_memblock_unref(e->memchunk.memblock);
281 e->memchunk.memblock = NULL;
282 e->memchunk.index = e->memchunk.length = 0;
283
284 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
285 }
286 }