]> code.delx.au - pulseaudio/blob - polyp/sink-input.c
0d59062d01757fb5211a04bfc761fa4e14f69f93
[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);
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->name = pa_xstrdup(name);
57 i->client = NULL;
58 i->owner = NULL;
59 i->sink = s;
60 i->sample_spec = *spec;
61
62 i->peek = NULL;
63 i->drop = NULL;
64 i->kill = NULL;
65 i->get_latency = NULL;
66 i->userdata = NULL;
67
68 i->corked = 0;
69 i->volume = PA_VOLUME_NORM;
70
71 i->resampled_chunk.memblock = NULL;
72 i->resampled_chunk.index = i->resampled_chunk.length = 0;
73 i->resampler = resampler;
74
75 assert(s->core);
76 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
77 assert(r == 0 && i->index != PA_IDXSET_INVALID);
78 r = pa_idxset_put(s->inputs, i, NULL);
79 assert(r == 0);
80
81 pa_sample_spec_snprint(st, sizeof(st), spec);
82 pa_log(__FILE__": created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
83
84 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
85
86 return i;
87 }
88
89 void pa_sink_input_free(struct pa_sink_input* i) {
90 assert(i);
91
92 assert(i->sink && i->sink->core);
93 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
94 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
95
96 if (i->resampled_chunk.memblock)
97 pa_memblock_unref(i->resampled_chunk.memblock);
98 if (i->resampler)
99 pa_resampler_free(i->resampler);
100
101 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
102
103 pa_xfree(i->name);
104 pa_xfree(i);
105 }
106
107 void pa_sink_input_kill(struct pa_sink_input*i) {
108 assert(i);
109
110 if (i->kill)
111 i->kill(i);
112 }
113
114 pa_usec_t pa_sink_input_get_latency(struct pa_sink_input *i) {
115 pa_usec_t r = 0;
116 assert(i);
117
118 if (i->get_latency)
119 r += i->get_latency(i);
120
121 if (i->resampled_chunk.memblock)
122 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sample_spec);
123
124 return r;
125 }
126
127 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
128 assert(i && chunk && i->peek && i->drop);
129
130 if (i->corked)
131 return -1;
132
133 if (!i->resampler)
134 return i->peek(i, chunk);
135
136 while (!i->resampled_chunk.memblock) {
137 struct pa_memchunk tchunk;
138 size_t l;
139 int ret;
140
141 if ((ret = i->peek(i, &tchunk)) < 0)
142 return ret;
143
144 assert(tchunk.length);
145
146 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
147
148 if (l > tchunk.length)
149 l = tchunk.length;
150
151 i->drop(i, &tchunk, l);
152 tchunk.length = l;
153
154 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
155 pa_memblock_unref(tchunk.memblock);
156 }
157
158 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
159 *chunk = i->resampled_chunk;
160 pa_memblock_ref(i->resampled_chunk.memblock);
161 return 0;
162 }
163
164 void pa_sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
165 assert(i && length);
166
167 if (!i->resampler) {
168 i->drop(i, chunk, length);
169 return;
170 }
171
172 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
173
174 i->resampled_chunk.index += length;
175 i->resampled_chunk.length -= length;
176
177 if (!i->resampled_chunk.length) {
178 pa_memblock_unref(i->resampled_chunk.memblock);
179 i->resampled_chunk.memblock = NULL;
180 i->resampled_chunk.index = i->resampled_chunk.length = 0;
181 }
182 }
183
184 void pa_sink_input_set_volume(struct pa_sink_input *i, pa_volume_t volume) {
185 assert(i && i->sink && i->sink->core);
186
187 if (i->volume != volume) {
188 i->volume = volume;
189 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
190 }
191 }
192
193 void pa_sink_input_cork(struct pa_sink_input *i, int b) {
194 int n;
195 assert(i);
196 n = i->corked && !b;
197 i->corked = b;
198
199 if (n)
200 pa_sink_notify(i->sink);
201 }
202
203 void pa_sink_input_set_rate(struct pa_sink_input *i, uint32_t rate) {
204 assert(i && i->resampler);
205
206 if (i->sample_spec.rate == rate)
207 return;
208
209 i->sample_spec.rate = rate;
210 pa_resampler_set_input_rate(i->resampler, rate);
211 }