7 #include "sink-input.h"
9 #include "sample-util.h"
10 #include "play-memchunk.h"
13 static void free_entry(struct pa_scache_entry
*e
) {
16 if (e
->memchunk
.memblock
)
17 pa_memblock_unref(e
->memchunk
.memblock
);
21 void pa_scache_add_item(struct pa_core
*c
, const char *name
, struct pa_sample_spec
*ss
, struct pa_memchunk
*chunk
, uint32_t *index
) {
22 struct pa_scache_entry
*e
;
26 if (c
->scache_hashmap
&& (e
= pa_hashmap_get(c
->scache_hashmap
, name
))) {
28 if (e
->memchunk
.memblock
)
29 pa_memblock_unref(e
->memchunk
.memblock
);
32 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
33 e
->name
= pa_xstrdup(name
);
41 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
45 pa_memblock_ref(e
->memchunk
.memblock
);
47 e
->memchunk
.memblock
= NULL
;
48 e
->memchunk
.index
= e
->memchunk
.length
= 0;
52 if (!c
->scache_hashmap
) {
53 c
->scache_hashmap
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
54 assert(c
->scache_hashmap
);
57 if (!c
->scache_idxset
) {
58 c
->scache_idxset
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
59 assert(c
->scache_idxset
);
62 pa_idxset_put(c
->scache_idxset
, e
, &e
->index
);
63 pa_hashmap_put(c
->scache_hashmap
, e
->name
, e
);
70 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
71 struct pa_scache_entry
*e
;
74 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
77 pa_hashmap_remove(c
->scache_hashmap
, name
);
78 if (pa_idxset_remove_by_data(c
->scache_idxset
, e
, NULL
) != e
)
84 static void free_cb(void *p
, void *userdata
) {
85 struct pa_scache_entry
*e
= p
;
90 void pa_scache_free(struct pa_core
*c
) {
93 if (c
->scache_hashmap
) {
94 pa_hashmap_free(c
->scache_hashmap
, free_cb
, NULL
);
95 c
->scache_hashmap
= NULL
;
98 if (c
->scache_idxset
) {
99 pa_idxset_free(c
->scache_idxset
, NULL
, NULL
);
100 c
->scache_idxset
= NULL
;
104 int pa_scache_play_item(struct pa_core
*c
, const char *name
, struct pa_sink
*sink
, uint32_t volume
) {
105 struct pa_scache_entry
*e
;
106 assert(c
&& name
&& sink
);
108 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
111 if (!e
->memchunk
.memblock
)
114 if (pa_play_memchunk(sink
, name
, &e
->sample_spec
, &e
->memchunk
, pa_volume_multiply(volume
, e
->volume
)) < 0)
120 const char * pa_scache_get_name_by_id(struct pa_core
*c
, uint32_t id
) {
121 struct pa_scache_entry
*e
;
122 assert(c
&& id
!= PA_IDXSET_INVALID
);
124 if (!c
->scache_idxset
|| !(e
= pa_idxset_get_by_index(c
->scache_idxset
, id
)))
131 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
132 struct pa_scache_entry
*e
;
135 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
136 return PA_IDXSET_INVALID
;