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