]> code.delx.au - pulseaudio/blob - src/sink-input.c
54778a81e1be27d1eab8a6ca976d1bbbc3991a14
[pulseaudio] / src / sink-input.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "sinkinput.h"
7 #include "sample-util.h"
8
9 #define CONVERT_BUFFER_LENGTH 4096
10
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;
14 int r;
15 char st[256];
16 assert(s && spec);
17
18 if (!pa_sample_spec_equal(spec, &s->sample_spec))
19 if (!(resampler = pa_resampler_new(spec, &s->sample_spec)))
20 return NULL;
21
22 i = malloc(sizeof(struct pa_sink_input));
23 assert(i);
24 i->name = name ? strdup(name) : NULL;
25 i->client = NULL;
26 i->owner = NULL;
27 i->sink = s;
28 i->sample_spec = *spec;
29
30 i->peek = NULL;
31 i->drop = NULL;
32 i->kill = NULL;
33 i->get_latency = NULL;
34 i->userdata = NULL;
35
36 i->volume = PA_VOLUME_NORM;
37
38 i->resampled_chunk.memblock = NULL;
39 i->resampled_chunk.index = i->resampled_chunk.length = 0;
40 i->resampler = resampler;
41
42 assert(s->core);
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);
46 assert(r == 0);
47
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);
50
51 return i;
52 }
53
54 void pa_sink_input_free(struct pa_sink_input* i) {
55 assert(i);
56
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);
60
61 if (i->resampled_chunk.memblock)
62 pa_memblock_unref(i->resampled_chunk.memblock);
63 if (i->resampler)
64 pa_resampler_free(i->resampler);
65
66 free(i->name);
67 free(i);
68 }
69
70 void pa_sink_input_kill(struct pa_sink_input*i) {
71 assert(i);
72
73 if (i->kill)
74 i->kill(i);
75 }
76
77 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
78 uint32_t l = 0;
79
80 assert(i);
81 if (i->get_latency)
82 l += i->get_latency(i);
83
84 assert(i->sink);
85 l += pa_sink_get_latency(i->sink);
86
87 return l;
88 }
89
90 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
91 assert(i && chunk && i->peek && i->drop);
92
93 if (!i->resampler)
94 return i->peek(i, chunk);
95
96 if (!i->resampled_chunk.memblock) {
97 struct pa_memchunk tchunk;
98 size_t l;
99 int ret;
100
101 if ((ret = i->peek(i, &tchunk)) < 0)
102 return ret;
103
104 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
105 if (tchunk.length > l)
106 tchunk.length = l;
107
108 i->drop(i, tchunk.length);
109
110 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
111 pa_memblock_unref(tchunk.memblock);
112 }
113
114 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
115 *chunk = i->resampled_chunk;
116 pa_memblock_ref(i->resampled_chunk.memblock);
117 return 0;
118 }
119
120 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
121 assert(i && length);
122
123 if (!i->resampler) {
124 i->drop(i, length);
125 return;
126 }
127
128 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
129
130 i->resampled_chunk.index += length;
131 i->resampled_chunk.length -= length;
132
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;
137 }
138 }