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