4 This file is part of polypaudio.
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.
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.
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
32 #include "sink-input.h"
34 #include "sample-util.h"
35 #include "play-memchunk.h"
37 #include "subscribe.h"
39 static void free_entry(struct pa_scache_entry
*e
) {
41 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
43 if (e
->memchunk
.memblock
)
44 pa_memblock_unref(e
->memchunk
.memblock
);
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
;
53 if (c
->scache_hashmap
&& (e
= pa_hashmap_get(c
->scache_hashmap
, name
))) {
55 if (e
->memchunk
.memblock
)
56 pa_memblock_unref(e
->memchunk
.memblock
);
60 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
61 e
->name
= pa_xstrdup(name
);
70 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
74 pa_memblock_ref(e
->memchunk
.memblock
);
76 e
->memchunk
.memblock
= NULL
;
77 e
->memchunk
.index
= e
->memchunk
.length
= 0;
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
);
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
);
91 pa_idxset_put(c
->scache_idxset
, e
, &e
->index
);
92 pa_hashmap_put(c
->scache_hashmap
, e
->name
, e
);
94 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
101 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
102 struct pa_scache_entry
*e
;
105 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
108 pa_hashmap_remove(c
->scache_hashmap
, name
);
109 if (pa_idxset_remove_by_data(c
->scache_idxset
, e
, NULL
) != e
)
116 static void free_cb(void *p
, void *userdata
) {
117 struct pa_scache_entry
*e
= p
;
122 void pa_scache_free(struct pa_core
*c
) {
125 if (c
->scache_hashmap
) {
126 pa_hashmap_free(c
->scache_hashmap
, free_cb
, NULL
);
127 c
->scache_hashmap
= NULL
;
130 if (c
->scache_idxset
) {
131 pa_idxset_free(c
->scache_idxset
, NULL
, NULL
);
132 c
->scache_idxset
= NULL
;
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
);
140 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
143 if (!e
->memchunk
.memblock
)
146 if (pa_play_memchunk(sink
, name
, &e
->sample_spec
, &e
->memchunk
, pa_volume_multiply(volume
, e
->volume
)) < 0)
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
);
156 if (!c
->scache_idxset
|| !(e
= pa_idxset_get_by_index(c
->scache_idxset
, id
)))
163 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
164 struct pa_scache_entry
*e
;
167 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
168 return PA_IDXSET_INVALID
;