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;
27 i->sample_spec = *spec;
32 i->get_latency = NULL;
35 i->volume = PA_VOLUME_NORM;
37 i->resampled_chunk.memblock = NULL;
38 i->resampled_chunk.index = i->resampled_chunk.length = 0;
39 i->resampler = resampler;
42 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
43 assert(r == 0 && i->index != PA_IDXSET_INVALID);
44 r = pa_idxset_put(s->inputs, i, NULL);
47 pa_sample_snprint(st, sizeof(st), spec);
48 fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
53 void pa_sink_input_free(struct pa_sink_input* i) {
56 assert(i->sink && i->sink->core);
57 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
58 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
60 if (i->resampled_chunk.memblock)
61 pa_memblock_unref(i->resampled_chunk.memblock);
63 pa_resampler_free(i->resampler);
69 void pa_sink_input_kill(struct pa_sink_input*i) {
76 char *pa_sink_input_list_to_string(struct pa_core *c) {
78 struct pa_sink_input *i;
79 uint32_t index = PA_IDXSET_INVALID;
85 pa_strbuf_printf(s, "%u sink input(s) available.\n", pa_idxset_ncontents(c->sink_inputs));
87 for (i = pa_idxset_first(c->sink_inputs, &index); i; i = pa_idxset_next(c->sink_inputs, &index)) {
88 char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
89 pa_sample_snprint(ss, sizeof(ss), &i->sample_spec);
92 s, " index: %u\n\tname: <%s>\n\tsink: <%u>\n\tvolume: <0x%04x>\n\tlatency: <%u usec>\n\tsample_spec: <%s>\n",
97 pa_sink_input_get_latency(i),
101 return pa_strbuf_tostring_free(s);
104 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
109 l += i->get_latency(i);
112 l += pa_sink_get_latency(i->sink);
118 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
119 assert(i && chunk && i->peek && i->drop);
122 return i->peek(i, chunk);
124 if (!i->resampled_chunk.memblock) {
125 struct pa_memchunk tchunk;
129 if ((ret = i->peek(i, &tchunk)) < 0)
132 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
133 if (tchunk.length > l)
136 i->drop(i, tchunk.length);
138 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
139 pa_memblock_unref(tchunk.memblock);
142 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
143 *chunk = i->resampled_chunk;
144 pa_memblock_ref(i->resampled_chunk.memblock);
148 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
156 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
158 i->resampled_chunk.index += length;
159 i->resampled_chunk.length -= length;
161 if (!i->resampled_chunk.length) {
162 pa_memblock_unref(i->resampled_chunk.memblock);
163 i->resampled_chunk.memblock = NULL;
164 i->resampled_chunk.index = i->resampled_chunk.length = 0;