4 This file is part of polypaudio.
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.
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.
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
31 #include "sink-input.h"
32 #include "sample-util.h"
34 #include "subscribe.h"
37 #define CONVERT_BUFFER_LENGTH 4096
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
;
44 assert(s
&& spec
&& s
->state
== PA_SINK_RUNNING
);
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");
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
)))
55 i
= pa_xmalloc(sizeof(struct pa_sink_input
));
57 i
->state
= PA_SINK_INPUT_RUNNING
;
58 i
->name
= pa_xstrdup(name
);
62 i
->sample_spec
= *spec
;
67 i
->get_latency
= NULL
;
71 i
->volume
= PA_VOLUME_NORM
;
73 i
->resampled_chunk
.memblock
= NULL
;
74 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
75 i
->resampler
= resampler
;
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
);
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
);
86 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, i
->index
);
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
);
94 pa_idxset_remove_by_data(i
->sink
->core
->sink_inputs
, i
, NULL
);
95 pa_idxset_remove_by_data(i
->sink
->inputs
, i
, NULL
);
97 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, i
->index
);
103 i
->get_latency
= NULL
;
105 i
->state
= PA_SINK_INPUT_DISCONNECTED
;
108 static void sink_input_free(struct pa_sink_input
* i
) {
111 if (i
->state
!= PA_SINK_INPUT_DISCONNECTED
)
112 pa_sink_input_disconnect(i
);
114 if (i
->resampled_chunk
.memblock
)
115 pa_memblock_unref(i
->resampled_chunk
.memblock
);
117 pa_resampler_free(i
->resampler
);
123 void pa_sink_input_unref(struct pa_sink_input
*i
) {
124 assert(i
&& i
->ref
>= 1);
130 struct pa_sink_input
* pa_sink_input_ref(struct pa_sink_input
*i
) {
131 assert(i
&& i
->ref
>= 1);
136 void pa_sink_input_kill(struct pa_sink_input
*i
) {
137 assert(i
&& i
->ref
>= 1);
143 pa_usec_t
pa_sink_input_get_latency(struct pa_sink_input
*i
) {
145 assert(i
&& i
->ref
>= 1);
148 r
+= i
->get_latency(i
);
150 if (i
->resampled_chunk
.memblock
)
151 r
+= pa_bytes_to_usec(i
->resampled_chunk
.length
, &i
->sample_spec
);
156 int pa_sink_input_peek(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
158 assert(i
&& chunk
&& i
->ref
>= 1);
160 if (!i
->peek
|| !i
->drop
)
167 return i
->peek(i
, chunk
);
169 pa_sink_input_ref(i
);
171 while (!i
->resampled_chunk
.memblock
) {
172 struct pa_memchunk tchunk
;
175 if ((ret
= i
->peek(i
, &tchunk
)) < 0)
178 assert(tchunk
.length
);
180 l
= pa_resampler_request(i
->resampler
, CONVERT_BUFFER_LENGTH
);
182 if (l
> tchunk
.length
)
185 i
->drop(i
, &tchunk
, l
);
188 pa_resampler_run(i
->resampler
, &tchunk
, &i
->resampled_chunk
);
189 pa_memblock_unref(tchunk
.memblock
);
192 assert(i
->resampled_chunk
.memblock
&& i
->resampled_chunk
.length
);
193 *chunk
= i
->resampled_chunk
;
194 pa_memblock_ref(i
->resampled_chunk
.memblock
);
200 pa_sink_input_unref(i
);
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);
210 i
->drop(i
, chunk
, length
);
214 assert(i
->resampled_chunk
.memblock
&& i
->resampled_chunk
.length
>= length
);
216 i
->resampled_chunk
.index
+= length
;
217 i
->resampled_chunk
.length
-= length
;
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;
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);
229 if (i
->volume
!= volume
) {
231 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
235 void pa_sink_input_cork(struct pa_sink_input
*i
, int b
) {
237 assert(i
&& i
->ref
>= 1);
243 pa_sink_notify(i
->sink
);
246 void pa_sink_input_set_rate(struct pa_sink_input
*i
, uint32_t rate
) {
247 assert(i
&& i
->resampler
&& i
->ref
>= 1);
249 if (i
->sample_spec
.rate
== rate
)
252 i
->sample_spec
.rate
= rate
;
253 pa_resampler_set_input_rate(i
->resampler
, rate
);
256 void pa_sink_input_set_name(struct pa_sink_input
*i
, const char *name
) {
257 assert(i
&& i
->ref
>= 1);
260 i
->name
= pa_xstrdup(name
);