]> code.delx.au - pulseaudio/blob - polyp/sink-input.c
347b8f5c65a64aef65dba678329c1db4d1fd4619
[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 Lesser 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 Lesser 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, int resample_method) {
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 (resample_method == PA_RESAMPLER_INVALID)
52 resample_method = s->core->resample_method;
53
54 if (variable_rate || !pa_sample_spec_equal(spec, &s->sample_spec))
55 if (!(resampler = pa_resampler_new(spec, &s->sample_spec, s->core->memblock_stat, resample_method)))
56 return NULL;
57
58 i = pa_xmalloc(sizeof(struct pa_sink_input));
59 i->ref = 1;
60 i->state = PA_SINK_INPUT_RUNNING;
61 i->name = pa_xstrdup(name);
62 i->client = NULL;
63 i->owner = NULL;
64 i->sink = s;
65 i->sample_spec = *spec;
66
67 i->peek = NULL;
68 i->drop = NULL;
69 i->kill = NULL;
70 i->get_latency = NULL;
71 i->userdata = NULL;
72 i->underrun = NULL;
73
74 i->volume = PA_VOLUME_NORM;
75 i->playing = 0;
76
77 i->resampled_chunk.memblock = NULL;
78 i->resampled_chunk.index = i->resampled_chunk.length = 0;
79 i->resampler = resampler;
80
81 assert(s->core);
82 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
83 assert(r == 0 && i->index != PA_IDXSET_INVALID);
84 r = pa_idxset_put(s->inputs, i, NULL);
85 assert(r == 0);
86
87 pa_sample_spec_snprint(st, sizeof(st), spec);
88 pa_log_info(__FILE__": created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
89
90 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
91
92 return i;
93 }
94
95 void pa_sink_input_disconnect(struct pa_sink_input *i) {
96 assert(i && i->state != PA_SINK_INPUT_DISCONNECTED && i->sink && i->sink->core);
97
98 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
99 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
100
101 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
102 i->sink = NULL;
103
104 i->peek = NULL;
105 i->drop = NULL;
106 i->kill = NULL;
107 i->get_latency = NULL;
108
109 i->state = PA_SINK_INPUT_DISCONNECTED;
110 }
111
112 static void sink_input_free(struct pa_sink_input* i) {
113 assert(i);
114
115 if (i->state != PA_SINK_INPUT_DISCONNECTED)
116 pa_sink_input_disconnect(i);
117
118 pa_log_info(__FILE__": freed %u \"%s\"\n", i->index, i->name);
119
120 if (i->resampled_chunk.memblock)
121 pa_memblock_unref(i->resampled_chunk.memblock);
122 if (i->resampler)
123 pa_resampler_free(i->resampler);
124
125 pa_xfree(i->name);
126 pa_xfree(i);
127 }
128
129 void pa_sink_input_unref(struct pa_sink_input *i) {
130 assert(i && i->ref >= 1);
131
132 if (!(--i->ref))
133 sink_input_free(i);
134 }
135
136 struct pa_sink_input* pa_sink_input_ref(struct pa_sink_input *i) {
137 assert(i && i->ref >= 1);
138 i->ref++;
139 return i;
140 }
141
142 void pa_sink_input_kill(struct pa_sink_input*i) {
143 assert(i && i->ref >= 1);
144
145 if (i->kill)
146 i->kill(i);
147 }
148
149 pa_usec_t pa_sink_input_get_latency(struct pa_sink_input *i) {
150 pa_usec_t r = 0;
151 assert(i && i->ref >= 1);
152
153 if (i->get_latency)
154 r += i->get_latency(i);
155
156 if (i->resampled_chunk.memblock)
157 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sample_spec);
158
159 return r;
160 }
161
162 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
163 int ret = -1;
164 assert(i && chunk && i->ref >= 1);
165
166 pa_sink_input_ref(i);
167
168 if (!i->peek || !i->drop || i->state == PA_SINK_INPUT_CORKED)
169 goto finish;
170
171 if (!i->resampler) {
172 ret = i->peek(i, chunk);
173 goto finish;
174 }
175
176 while (!i->resampled_chunk.memblock) {
177 struct pa_memchunk tchunk;
178 size_t l;
179
180 if ((ret = i->peek(i, &tchunk)) < 0)
181 goto finish;
182
183 assert(tchunk.length);
184
185 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
186
187 if (l > tchunk.length)
188 l = tchunk.length;
189
190 i->drop(i, &tchunk, l);
191 tchunk.length = l;
192
193 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
194 pa_memblock_unref(tchunk.memblock);
195 }
196
197 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
198 *chunk = i->resampled_chunk;
199 pa_memblock_ref(i->resampled_chunk.memblock);
200
201 ret = 0;
202
203 finish:
204
205 if (ret < 0 && i->playing && i->underrun)
206 i->underrun(i);
207
208 i->playing = ret >= 0;
209
210 pa_sink_input_unref(i);
211
212 return ret;
213 }
214
215 void pa_sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
216 assert(i && length && i->ref >= 1);
217
218 if (!i->resampler) {
219 if (i->drop)
220 i->drop(i, chunk, length);
221 return;
222 }
223
224 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
225
226 i->resampled_chunk.index += length;
227 i->resampled_chunk.length -= length;
228
229 if (!i->resampled_chunk.length) {
230 pa_memblock_unref(i->resampled_chunk.memblock);
231 i->resampled_chunk.memblock = NULL;
232 i->resampled_chunk.index = i->resampled_chunk.length = 0;
233 }
234 }
235
236 void pa_sink_input_set_volume(struct pa_sink_input *i, pa_volume_t volume) {
237 assert(i && i->sink && i->sink->core && i->ref >= 1);
238
239 if (i->volume != volume) {
240 i->volume = volume;
241 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
242 }
243 }
244
245 void pa_sink_input_cork(struct pa_sink_input *i, int b) {
246 int n;
247 assert(i && i->ref >= 1);
248
249 if (i->state == PA_SINK_INPUT_DISCONNECTED)
250 return;
251
252 n = i->state == PA_SINK_INPUT_CORKED && !b;
253 i->state = b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
254
255 if (n)
256 pa_sink_notify(i->sink);
257 }
258
259 void pa_sink_input_set_rate(struct pa_sink_input *i, uint32_t rate) {
260 assert(i && i->resampler && i->ref >= 1);
261
262 if (i->sample_spec.rate == rate)
263 return;
264
265 i->sample_spec.rate = rate;
266 pa_resampler_set_input_rate(i->resampler, rate);
267 }
268
269 void pa_sink_input_set_name(struct pa_sink_input *i, const char *name) {
270 assert(i && i->ref >= 1);
271
272 pa_xfree(i->name);
273 i->name = pa_xstrdup(name);
274
275 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
276 }
277
278 enum pa_resample_method pa_sink_input_get_resample_method(struct pa_sink_input *i) {
279 assert(i && i->ref >= 1);
280
281 if (!i->resampler)
282 return PA_RESAMPLER_INVALID;
283
284 return pa_resampler_get_method(i->resampler);
285 }