2004-06-08 23:54:24 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
#include "sinkinput.h"
|
2004-06-18 17:12:50 +00:00
|
|
|
#include "strbuf.h"
|
2004-06-29 20:37:24 +00:00
|
|
|
#include "sample-util.h"
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
#define CONVERT_BUFFER_LENGTH 4096
|
|
|
|
|
|
|
|
|
|
struct sink_input* sink_input_new(struct sink *s, const char *name, const struct pa_sample_spec *spec) {
|
2004-06-15 17:05:03 +00:00
|
|
|
struct sink_input *i;
|
2004-07-03 00:19:17 +00:00
|
|
|
struct resampler *resampler = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
int r;
|
|
|
|
|
assert(s && spec);
|
|
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
if (!pa_sample_spec_equal(spec, &s->sample_spec))
|
|
|
|
|
if (!(resampler = resampler_new(spec, &s->sample_spec)))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
i = malloc(sizeof(struct sink_input));
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(i);
|
|
|
|
|
i->name = name ? strdup(name) : NULL;
|
|
|
|
|
i->sink = s;
|
2004-06-23 23:17:30 +00:00
|
|
|
i->sample_spec = *spec;
|
2004-06-15 00:29:01 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
i->peek = NULL;
|
|
|
|
|
i->drop = NULL;
|
2004-06-14 20:30:50 +00:00
|
|
|
i->kill = NULL;
|
2004-06-18 17:12:50 +00:00
|
|
|
i->get_latency = NULL;
|
2004-06-15 17:05:03 +00:00
|
|
|
i->userdata = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-29 20:37:24 +00:00
|
|
|
i->volume = VOLUME_NORM;
|
2004-06-19 01:01:09 +00:00
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
i->resampled_chunk.memblock = NULL;
|
|
|
|
|
i->resampled_chunk.index = i->resampled_chunk.length = 0;
|
|
|
|
|
i->resampler = resampler;
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(s->core);
|
2004-06-15 17:05:03 +00:00
|
|
|
r = idxset_put(s->core->sink_inputs, i, &i->index);
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(r == 0 && i->index != IDXSET_INVALID);
|
2004-06-15 17:05:03 +00:00
|
|
|
r = idxset_put(s->inputs, i, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(r == 0);
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
void sink_input_free(struct sink_input* i) {
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
assert(i->sink && i->sink->core);
|
2004-06-15 17:05:03 +00:00
|
|
|
idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
|
|
|
|
|
idxset_remove_by_data(i->sink->inputs, i, NULL);
|
2004-07-03 00:19:17 +00:00
|
|
|
|
|
|
|
|
if (i->resampled_chunk.memblock)
|
|
|
|
|
memblock_unref(i->resampled_chunk.memblock);
|
|
|
|
|
if (i->resampler)
|
|
|
|
|
resampler_free(i->resampler);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
free(i->name);
|
|
|
|
|
free(i);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
void sink_input_kill(struct sink_input*i) {
|
2004-06-14 20:30:50 +00:00
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
if (i->kill)
|
2004-06-15 17:05:03 +00:00
|
|
|
i->kill(i);
|
2004-06-15 00:29:01 +00:00
|
|
|
}
|
2004-06-18 17:12:50 +00:00
|
|
|
|
|
|
|
|
char *sink_input_list_to_string(struct core *c) {
|
|
|
|
|
struct strbuf *s;
|
|
|
|
|
struct sink_input *i;
|
|
|
|
|
uint32_t index = IDXSET_INVALID;
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
s = strbuf_new();
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
strbuf_printf(s, "%u sink input(s) available.\n", idxset_ncontents(c->sink_inputs));
|
|
|
|
|
|
|
|
|
|
for (i = idxset_first(c->sink_inputs, &index); i; i = idxset_next(c->sink_inputs, &index)) {
|
|
|
|
|
assert(i->sink);
|
2004-06-29 20:37:24 +00:00
|
|
|
strbuf_printf(s, " index: %u, name: <%s>, sink: <%u>; volume: <0x%04x>, latency: <%u usec>\n",
|
2004-06-18 17:12:50 +00:00
|
|
|
i->index,
|
|
|
|
|
i->name,
|
|
|
|
|
i->sink->index,
|
|
|
|
|
(unsigned) i->volume,
|
|
|
|
|
sink_input_get_latency(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strbuf_tostring_free(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t sink_input_get_latency(struct sink_input *i) {
|
|
|
|
|
uint32_t l = 0;
|
|
|
|
|
|
|
|
|
|
assert(i);
|
|
|
|
|
if (i->get_latency)
|
|
|
|
|
l += i->get_latency(i);
|
|
|
|
|
|
|
|
|
|
assert(i->sink);
|
|
|
|
|
l += sink_get_latency(i->sink);
|
|
|
|
|
|
|
|
|
|
return l;
|
|
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
|
|
|
|
|
int sink_input_peek(struct sink_input *i, struct memchunk *chunk) {
|
|
|
|
|
assert(i && chunk && i->peek && i->drop);
|
|
|
|
|
|
|
|
|
|
if (!i->resampler)
|
|
|
|
|
return i->peek(i, chunk);
|
|
|
|
|
|
|
|
|
|
if (!i->resampled_chunk.memblock) {
|
|
|
|
|
struct memchunk tchunk;
|
|
|
|
|
size_t l;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if ((ret = i->peek(i, &tchunk)) < 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
l = resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
|
|
|
|
|
if (tchunk.length > l)
|
|
|
|
|
tchunk.length = l;
|
|
|
|
|
|
|
|
|
|
i->drop(i, tchunk.length);
|
|
|
|
|
|
|
|
|
|
resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
|
|
|
|
|
memblock_unref(tchunk.memblock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
|
|
|
|
|
*chunk = i->resampled_chunk;
|
|
|
|
|
memblock_ref(i->resampled_chunk.memblock);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sink_input_drop(struct sink_input *i, size_t length) {
|
|
|
|
|
assert(i && length);
|
|
|
|
|
|
|
|
|
|
if (!i->resampler) {
|
|
|
|
|
i->drop(i, length);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
|
|
|
|
|
|
|
|
|
|
i->resampled_chunk.index += length;
|
|
|
|
|
i->resampled_chunk.length -= length;
|
|
|
|
|
|
|
|
|
|
if (!i->resampled_chunk.length) {
|
|
|
|
|
memblock_unref(i->resampled_chunk.memblock);
|
|
|
|
|
i->resampled_chunk.memblock = NULL;
|
|
|
|
|
i->resampled_chunk.index = i->resampled_chunk.length = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|