7 #include "sample-util.h"
9 #define CONVERT_BUFFER_LENGTH 4096
11 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) {
12 struct pa_sink_input *i;
13 struct pa_resampler *resampler = NULL;
18 if (!pa_sample_spec_equal(spec, &s->sample_spec))
19 if (!(resampler = pa_resampler_new(spec, &s->sample_spec)))
22 i = malloc(sizeof(struct pa_sink_input));
24 i->name = name ? strdup(name) : NULL;
28 i->sample_spec = *spec;
33 i->get_latency = NULL;
36 i->volume = PA_VOLUME_NORM;
38 i->resampled_chunk.memblock = NULL;
39 i->resampled_chunk.index = i->resampled_chunk.length = 0;
40 i->resampler = resampler;
43 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
44 assert(r == 0 && i->index != PA_IDXSET_INVALID);
45 r = pa_idxset_put(s->inputs, i, NULL);
48 pa_sample_snprint(st, sizeof(st), spec);
49 fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
54 void pa_sink_input_free(struct pa_sink_input* i) {
57 assert(i->sink && i->sink->core);
58 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
59 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
61 if (i->resampled_chunk.memblock)
62 pa_memblock_unref(i->resampled_chunk.memblock);
64 pa_resampler_free(i->resampler);
70 void pa_sink_input_kill(struct pa_sink_input*i) {
77 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
82 l += i->get_latency(i);
85 l += pa_sink_get_latency(i->sink);
90 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
91 assert(i && chunk && i->peek && i->drop);
94 return i->peek(i, chunk);
96 if (!i->resampled_chunk.memblock) {
97 struct pa_memchunk tchunk;
101 if ((ret = i->peek(i, &tchunk)) < 0)
104 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
105 if (tchunk.length > l)
108 i->drop(i, tchunk.length);
110 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
111 pa_memblock_unref(tchunk.memblock);
114 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
115 *chunk = i->resampled_chunk;
116 pa_memblock_ref(i->resampled_chunk.memblock);
120 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
128 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
130 i->resampled_chunk.index += length;
131 i->resampled_chunk.length -= length;
133 if (!i->resampled_chunk.length) {
134 pa_memblock_unref(i->resampled_chunk.memblock);
135 i->resampled_chunk.memblock = NULL;
136 i->resampled_chunk.index = i->resampled_chunk.length = 0;