]> code.delx.au - pulseaudio/blob - polyp/scache.c
add missing copyright headers
[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
39 static void free_entry(struct pa_scache_entry *e) {
40 assert(e);
41 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
42 pa_xfree(e->name);
43 if (e->memchunk.memblock)
44 pa_memblock_unref(e->memchunk.memblock);
45 pa_xfree(e);
46 }
47
48 void pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) {
49 struct pa_scache_entry *e;
50 int put;
51 assert(c && name);
52
53 if (c->scache_hashmap && (e = pa_hashmap_get(c->scache_hashmap, name))) {
54 put = 0;
55 if (e->memchunk.memblock)
56 pa_memblock_unref(e->memchunk.memblock);
57 assert(e->core == c);
58 } else {
59 put = 1;
60 e = pa_xmalloc(sizeof(struct pa_scache_entry));
61 e->name = pa_xstrdup(name);
62 e->core = c;
63 }
64
65 e->volume = 0x100;
66
67 if (ss)
68 e->sample_spec = *ss;
69 else
70 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
71
72 if (chunk) {
73 e->memchunk = *chunk;
74 pa_memblock_ref(e->memchunk.memblock);
75 } else {
76 e->memchunk.memblock = NULL;
77 e->memchunk.index = e->memchunk.length = 0;
78 }
79
80 if (put) {
81 if (!c->scache_hashmap) {
82 c->scache_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
83 assert(c->scache_hashmap);
84 }
85
86 if (!c->scache_idxset) {
87 c->scache_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
88 assert(c->scache_idxset);
89 }
90
91 pa_idxset_put(c->scache_idxset, e, &e->index);
92 pa_hashmap_put(c->scache_hashmap, e->name, e);
93
94 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
95 }
96
97 if (index)
98 *index = e->index;
99 }
100
101 int pa_scache_remove_item(struct pa_core *c, const char *name) {
102 struct pa_scache_entry *e;
103 assert(c && name);
104
105 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
106 return -1;
107
108 pa_hashmap_remove(c->scache_hashmap, name);
109 if (pa_idxset_remove_by_data(c->scache_idxset, e, NULL) != e)
110 assert(0);
111
112 free_entry(e);
113 return 0;
114 }
115
116 static void free_cb(void *p, void *userdata) {
117 struct pa_scache_entry *e = p;
118 assert(e);
119 free_entry(e);
120 }
121
122 void pa_scache_free(struct pa_core *c) {
123 assert(c);
124
125 if (c->scache_hashmap) {
126 pa_hashmap_free(c->scache_hashmap, free_cb, NULL);
127 c->scache_hashmap = NULL;
128 }
129
130 if (c->scache_idxset) {
131 pa_idxset_free(c->scache_idxset, NULL, NULL);
132 c->scache_idxset = NULL;
133 }
134 }
135
136 int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) {
137 struct pa_scache_entry *e;
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 (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0)
147 return -1;
148
149 return 0;
150 }
151
152 const char * pa_scache_get_name_by_id(struct pa_core *c, uint32_t id) {
153 struct pa_scache_entry *e;
154 assert(c && id != PA_IDXSET_INVALID);
155
156 if (!c->scache_idxset || !(e = pa_idxset_get_by_index(c->scache_idxset, id)))
157 return NULL;
158
159 return e->name;
160
161 }
162
163 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
164 struct pa_scache_entry *e;
165 assert(c && name);
166
167 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
168 return PA_IDXSET_INVALID;
169
170 return e->index;
171 }