]> code.delx.au - pulseaudio/blob - polyp/sink-input.c
e2b9e0cf56acd99bb90cc3fed802f5be7c782ca6
[pulseaudio] / polyp / sink-input.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "sink-input.h"
32 #include "sample-util.h"
33 #include "xmalloc.h"
34 #include "subscribe.h"
35 #include "log.h"
36
37 #define CONVERT_BUFFER_LENGTH 4096
38
39 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec, int variable_rate) {
40 struct pa_sink_input *i;
41 struct pa_resampler *resampler = NULL;
42 int r;
43 char st[256];
44 assert(s && spec && s->state == PA_SINK_RUNNING);
45
46 if (pa_idxset_ncontents(s->inputs) >= PA_MAX_INPUTS_PER_SINK) {
47 pa_log(__FILE__": Failed to create sink input: too many inputs per sink.\n");
48 return NULL;
49 }
50
51 if (variable_rate || !pa_sample_spec_equal(spec, &s->sample_spec))
52 if (!(resampler = pa_resampler_new(spec, &s->sample_spec, s->core->memblock_stat)))
53 return NULL;
54
55 i = pa_xmalloc(sizeof(struct pa_sink_input));
56 i->ref = 1;
57 i->state = PA_SINK_INPUT_RUNNING;
58 i->name = pa_xstrdup(name);
59 i->client = NULL;
60 i->owner = NULL;
61 i->sink = s;
62 i->sample_spec = *spec;
63
64 i->peek = NULL;
65 i->drop = NULL;
66 i->kill = NULL;
67 i->get_latency = NULL;
68 i->userdata = NULL;
69
70 i->corked = 0;
71 i->volume = PA_VOLUME_NORM;
72
73 i->resampled_chunk.memblock = NULL;
74 i->resampled_chunk.index = i->resampled_chunk.length = 0;
75 i->resampler = resampler;
76
77 assert(s->core);
78 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
79 assert(r == 0 && i->index != PA_IDXSET_INVALID);
80 r = pa_idxset_put(s->inputs, i, NULL);
81 assert(r == 0);
82
83 pa_sample_spec_snprint(st, sizeof(st), spec);
84 pa_log(__FILE__": created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
85
86 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
87
88 return i;
89 }
90
91 void pa_sink_input_disconnect(struct pa_sink_input *i) {
92 assert(i && i->state == PA_SINK_INPUT_RUNNING && i->sink && i->sink->core);
93
94 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
95 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
96
97 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
98 i->sink = NULL;
99
100 i->peek = NULL;
101 i->drop = NULL;
102 i->kill = NULL;
103 i->get_latency = NULL;
104
105 i->state = PA_SINK_INPUT_DISCONNECTED;
106 }
107
108 static void sink_input_free(struct pa_sink_input* i) {
109 assert(i);
110
111 if (i->state != PA_SINK_INPUT_DISCONNECTED)
112 pa_sink_input_disconnect(i);
113
114 if (i->resampled_chunk.memblock)
115 pa_memblock_unref(i->resampled_chunk.memblock);
116 if (i->resampler)
117 pa_resampler_free(i->resampler);
118
119 pa_xfree(i->name);
120 pa_xfree(i);
121 }
122
123 void pa_sink_input_unref(struct pa_sink_input *i) {
124 assert(i && i->ref >= 1);
125
126 if (!(--i->ref))
127 sink_input_free(i);
128 }
129
130 struct pa_sink_input* pa_sink_input_ref(struct pa_sink_input *i) {
131 assert(i && i->ref >= 1);
132 i->ref++;
133 return i;
134 }
135
136 void pa_sink_input_kill(struct pa_sink_input*i) {
137 assert(i && i->ref >= 1);
138
139 if (i->kill)
140 i->kill(i);
141 }
142
143 pa_usec_t pa_sink_input_get_latency(struct pa_sink_input *i) {
144 pa_usec_t r = 0;
145 assert(i && i->ref >= 1);
146
147 if (i->get_latency)
148 r += i->get_latency(i);
149
150 if (i->resampled_chunk.memblock)
151 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sample_spec);
152
153 return r;
154 }
155
156 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
157 int ret = 0;
158 assert(i && chunk && i->ref >= 1);
159
160 if (!i->peek || !i->drop)
161 return -1;
162
163 if (i->corked)
164 return -1;
165
166 if (!i->resampler)
167 return i->peek(i, chunk);
168
169 pa_sink_input_ref(i);
170
171 while (!i->resampled_chunk.memblock) {
172 struct pa_memchunk tchunk;
173 size_t l;
174
175 if ((ret = i->peek(i, &tchunk)) < 0)
176 goto finish;
177
178 assert(tchunk.length);
179
180 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
181
182 if (l > tchunk.length)
183 l = tchunk.length;
184
185 i->drop(i, &tchunk, l);
186 tchunk.length = l;
187
188 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
189 pa_memblock_unref(tchunk.memblock);
190 }
191
192 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
193 *chunk = i->resampled_chunk;
194 pa_memblock_ref(i->resampled_chunk.memblock);
195
196 ret = 0;
197
198 finish:
199
200 pa_sink_input_unref(i);
201
202 return ret;
203 }
204
205 void pa_sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
206 assert(i && length && i->ref >= 1);
207
208 if (!i->resampler) {
209 if (i->drop)
210 i->drop(i, chunk, length);
211 return;
212 }
213
214 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
215
216 i->resampled_chunk.index += length;
217 i->resampled_chunk.length -= length;
218
219 if (!i->resampled_chunk.length) {
220 pa_memblock_unref(i->resampled_chunk.memblock);
221 i->resampled_chunk.memblock = NULL;
222 i->resampled_chunk.index = i->resampled_chunk.length = 0;
223 }
224 }
225
226 void pa_sink_input_set_volume(struct pa_sink_input *i, pa_volume_t volume) {
227 assert(i && i->sink && i->sink->core && i->ref >= 1);
228
229 if (i->volume != volume) {
230 i->volume = volume;
231 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
232 }
233 }
234
235 void pa_sink_input_cork(struct pa_sink_input *i, int b) {
236 int n;
237 assert(i && i->ref >= 1);
238
239 n = i->corked && !b;
240 i->corked = b;
241
242 if (n)
243 pa_sink_notify(i->sink);
244 }
245
246 void pa_sink_input_set_rate(struct pa_sink_input *i, uint32_t rate) {
247 assert(i && i->resampler && i->ref >= 1);
248
249 if (i->sample_spec.rate == rate)
250 return;
251
252 i->sample_spec.rate = rate;
253 pa_resampler_set_input_rate(i->resampler, rate);
254 }
255
256 void pa_sink_input_set_name(struct pa_sink_input *i, const char *name) {
257 assert(i && i->ref >= 1);
258
259 pa_xfree(i->name);
260 i->name = pa_xstrdup(name);
261 }