6 #include "sink-input.h"
8 static void free_entry(struct pa_scache_entry
*e
) {
11 if (e
->memchunk
.memblock
)
12 pa_memblock_unref(e
->memchunk
.memblock
);
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
;
21 if (c
->scache_hashmap
&& (e
= pa_hashmap_get(c
->scache_hashmap
, name
))) {
23 if (e
->memchunk
.memblock
)
24 pa_memblock_unref(e
->memchunk
.memblock
);
27 e
= malloc(sizeof(struct pa_scache_entry
));
29 e
->name
= strdup(name
);
36 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
40 pa_memblock_ref(e
->memchunk
.memblock
);
42 e
->memchunk
.memblock
= NULL
;
43 e
->memchunk
.index
= e
->memchunk
.length
= 0;
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
);
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
);
57 pa_idxset_put(c
->scache_idxset
, e
, &e
->index
);
58 pa_hashmap_put(c
->scache_hashmap
, e
->name
, e
);
65 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
66 struct pa_scache_entry
*e
;
69 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
72 pa_hashmap_remove(c
->scache_hashmap
, name
);
73 pa_idxset_remove_by_index(c
->scache_idxset
, e
->index
);
78 static void free_cb(void *p
, void *userdata
) {
79 struct pa_scache_entry
*e
= p
;
84 void pa_scache_free(struct pa_core
*c
) {
87 if (c
->scache_hashmap
) {
88 pa_hashmap_free(c
->scache_hashmap
, free_cb
, NULL
);
89 c
->scache_hashmap
= NULL
;
92 if (c
->scache_idxset
) {
93 pa_idxset_free(c
->scache_idxset
, NULL
, NULL
);
94 c
->scache_idxset
= NULL
;
98 static void sink_input_kill(struct pa_sink_input
*i
) {
99 struct pa_memchunk
*c
;
100 assert(i
&& i
->userdata
);
103 pa_memblock_unref(c
->memblock
);
105 pa_sink_input_free(i
);
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
);
113 assert(c
->length
&& c
->memblock
&& c
->memblock
->length
);
115 pa_memblock_ref(c
->memblock
);
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
);
125 assert(length
<= c
->length
);
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
);
140 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
143 if (!e
->memchunk
.memblock
)
146 if (!(si
= pa_sink_input_new(sink
, name
, &e
->sample_spec
)))
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
));
156 *chunk
= e
->memchunk
;
157 pa_memblock_ref(chunk
->memblock
);
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
);
166 if (!c
->scache_idxset
|| !(e
= pa_idxset_get_by_index(c
->scache_idxset
, id
)))
173 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
174 struct pa_scache_entry
*e
;
177 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
178 return PA_IDXSET_INVALID
;