8 #include "sample-util.h"
10 #define CONVERT_BUFFER_LENGTH 4096
12 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) {
13 struct pa_sink_input *i;
14 struct pa_resampler *resampler = NULL;
19 if (!pa_sample_spec_equal(spec, &s->sample_spec))
20 if (!(resampler = pa_resampler_new(spec, &s->sample_spec)))
23 i = malloc(sizeof(struct pa_sink_input));
25 i->name = name ? strdup(name) : NULL;
29 i->sample_spec = *spec;
34 i->get_latency = NULL;
37 i->volume = PA_VOLUME_NORM;
39 i->resampled_chunk.memblock = NULL;
40 i->resampled_chunk.index = i->resampled_chunk.length = 0;
41 i->resampler = resampler;
44 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
45 assert(r == 0 && i->index != PA_IDXSET_INVALID);
46 r = pa_idxset_put(s->inputs, i, NULL);
49 pa_sample_snprint(st, sizeof(st), spec);
50 fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
55 void pa_sink_input_free(struct pa_sink_input* i) {
58 assert(i->sink && i->sink->core);
59 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
60 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
62 if (i->resampled_chunk.memblock)
63 pa_memblock_unref(i->resampled_chunk.memblock);
65 pa_resampler_free(i->resampler);
71 void pa_sink_input_kill(struct pa_sink_input*i) {
78 char *pa_sink_input_list_to_string(struct pa_core *c) {
80 struct pa_sink_input *i;
81 uint32_t index = PA_IDXSET_INVALID;
87 pa_strbuf_printf(s, "%u sink input(s) available.\n", pa_idxset_ncontents(c->sink_inputs));
89 for (i = pa_idxset_first(c->sink_inputs, &index); i; i = pa_idxset_next(c->sink_inputs, &index)) {
90 char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
91 pa_sample_snprint(ss, sizeof(ss), &i->sample_spec);
94 s, " index: %u\n\tname: <%s>\n\tsink: <%u>\n\tvolume: <0x%04x>\n\tlatency: <%u usec>\n\tsample_spec: <%s>\n",
99 pa_sink_input_get_latency(i),
103 pa_strbuf_printf(s, "\towner module: <%u>\n", i->owner->index);
105 pa_strbuf_printf(s, "\tclient: <%u>\n", i->client->index);
108 return pa_strbuf_tostring_free(s);
111 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
116 l += i->get_latency(i);
119 l += pa_sink_get_latency(i->sink);
125 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
126 assert(i && chunk && i->peek && i->drop);
129 return i->peek(i, chunk);
131 if (!i->resampled_chunk.memblock) {
132 struct pa_memchunk tchunk;
136 if ((ret = i->peek(i, &tchunk)) < 0)
139 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
140 if (tchunk.length > l)
143 i->drop(i, tchunk.length);
145 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
146 pa_memblock_unref(tchunk.memblock);
149 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
150 *chunk = i->resampled_chunk;
151 pa_memblock_ref(i->resampled_chunk.memblock);
155 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
163 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
165 i->resampled_chunk.index += length;
166 i->resampled_chunk.length -= length;
168 if (!i->resampled_chunk.length) {
169 pa_memblock_unref(i->resampled_chunk.memblock);
170 i->resampled_chunk.memblock = NULL;
171 i->resampled_chunk.index = i->resampled_chunk.length = 0;