]> code.delx.au - pulseaudio/blob - polyp/scache.c
sample cache work
[pulseaudio] / polyp / scache.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "scache.h"
6 #include "sink-input.h"
7
8 static void free_entry(struct pa_scache_entry *e) {
9 assert(e);
10 free(e->name);
11 if (e->memchunk.memblock)
12 pa_memblock_unref(e->memchunk.memblock);
13 free(e);
14 }
15
16 void pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) {
17 struct pa_scache_entry *e;
18 int put;
19 assert(c && name);
20
21 if (c->scache_hashmap && (e = pa_hashmap_get(c->scache_hashmap, name))) {
22 put = 0;
23 if (e->memchunk.memblock)
24 pa_memblock_unref(e->memchunk.memblock);
25 } else {
26 put = 1;
27 e = malloc(sizeof(struct pa_scache_entry));
28 assert(e);
29 e->name = strdup(name);
30 assert(e->name);
31 }
32
33 if (ss)
34 e->sample_spec = *ss;
35 else
36 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
37
38 if (chunk) {
39 e->memchunk = *chunk;
40 pa_memblock_ref(e->memchunk.memblock);
41 } else {
42 e->memchunk.memblock = NULL;
43 e->memchunk.index = e->memchunk.length = 0;
44 }
45
46 if (put) {
47 if (!c->scache_hashmap) {
48 c->scache_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
49 assert(c->scache_hashmap);
50 }
51
52 if (!c->scache_idxset) {
53 c->scache_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
54 assert(c->scache_idxset);
55 }
56
57 pa_idxset_put(c->scache_idxset, e, &e->index);
58 pa_hashmap_put(c->scache_hashmap, e->name, e);
59 }
60
61 if (index)
62 *index = e->index;
63 }
64
65 int pa_scache_remove_item(struct pa_core *c, const char *name) {
66 struct pa_scache_entry *e;
67 assert(c && name);
68
69 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
70 return -1;
71
72 pa_hashmap_remove(c->scache_hashmap, name);
73 pa_idxset_remove_by_index(c->scache_idxset, e->index);
74 free_entry(e);
75 return 0;
76 }
77
78 static void free_cb(void *p, void *userdata) {
79 struct pa_scache_entry *e = p;
80 assert(e);
81 free_entry(e);
82 }
83
84 void pa_scache_free(struct pa_core *c) {
85 assert(c);
86
87 if (c->scache_hashmap) {
88 pa_hashmap_free(c->scache_hashmap, free_cb, NULL);
89 c->scache_hashmap = NULL;
90 }
91
92 if (c->scache_idxset) {
93 pa_idxset_free(c->scache_idxset, NULL, NULL);
94 c->scache_idxset = NULL;
95 }
96 }
97
98 static void sink_input_kill(struct pa_sink_input *i) {
99 struct pa_memchunk *c;
100 assert(i && i->userdata);
101 c = i->userdata;
102
103 pa_memblock_unref(c->memblock);
104 free(c);
105 pa_sink_input_free(i);
106 }
107
108 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
109 struct pa_memchunk *c;
110 assert(i && chunk && i->userdata);
111 c = i->userdata;
112
113 assert(c->length && c->memblock && c->memblock->length);
114 *chunk = *c;
115 pa_memblock_ref(c->memblock);
116
117 return 0;
118 }
119
120 static void sink_input_drop(struct pa_sink_input *i, size_t length) {
121 struct pa_memchunk *c;
122 assert(i && length && i->userdata);
123 c = i->userdata;
124
125 assert(length <= c->length);
126
127 c->length -= length;
128 c->index += length;
129
130 if (c->length <= 0)
131 sink_input_kill(i);
132 }
133
134 int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) {
135 struct pa_sink_input *si;
136 struct pa_scache_entry *e;
137 struct pa_memchunk *chunk;
138 assert(c && name && sink);
139
140 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
141 return -1;
142
143 if (!e->memchunk.memblock)
144 return -1;
145
146 if (!(si = pa_sink_input_new(sink, name, &e->sample_spec)))
147 return -1;
148
149 si->volume = volume;
150
151 si->peek = sink_input_peek;
152 si->drop = sink_input_drop;
153 si->kill = sink_input_kill;
154 si->userdata = chunk = malloc(sizeof(struct pa_memchunk));
155 assert(chunk);
156 *chunk = e->memchunk;
157 pa_memblock_ref(chunk->memblock);
158
159 return 0;
160 }
161
162 const char * pa_scache_get_name_by_id(struct pa_core *c, uint32_t id) {
163 struct pa_scache_entry *e;
164 assert(c && id != PA_IDXSET_INVALID);
165
166 if (!c->scache_idxset || !(e = pa_idxset_get_by_index(c->scache_idxset, id)))
167 return NULL;
168
169 return e->name;
170
171 }
172
173 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
174 struct pa_scache_entry *e;
175 assert(c && name);
176
177 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
178 return PA_IDXSET_INVALID;
179
180 return e->index;
181 }