]> code.delx.au - pulseaudio/blob - polyp/sink-input.c
introduce sink input and source output limits
[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
36 #define CONVERT_BUFFER_LENGTH 4096
37
38 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) {
39 struct pa_sink_input *i;
40 struct pa_resampler *resampler = NULL;
41 int r;
42 char st[256];
43 assert(s && spec);
44
45 if (pa_idxset_ncontents(s->inputs) >= PA_MAX_INPUTS_PER_SINK) {
46 fprintf(stderr, __FILE__": Failed to create sink input: too many inputs per sink.\n");
47 return NULL;
48 }
49
50 if (!pa_sample_spec_equal(spec, &s->sample_spec))
51 if (!(resampler = pa_resampler_new(spec, &s->sample_spec, s->core->memblock_stat)))
52 return NULL;
53
54 i = pa_xmalloc(sizeof(struct pa_sink_input));
55 i->name = pa_xstrdup(name);
56 i->client = NULL;
57 i->owner = NULL;
58 i->sink = s;
59 i->sample_spec = *spec;
60
61 i->peek = NULL;
62 i->drop = NULL;
63 i->kill = NULL;
64 i->get_latency = NULL;
65 i->userdata = NULL;
66
67 i->corked = 0;
68 i->volume = PA_VOLUME_NORM;
69
70 i->resampled_chunk.memblock = NULL;
71 i->resampled_chunk.index = i->resampled_chunk.length = 0;
72 i->resampler = resampler;
73
74 assert(s->core);
75 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
76 assert(r == 0 && i->index != PA_IDXSET_INVALID);
77 r = pa_idxset_put(s->inputs, i, NULL);
78 assert(r == 0);
79
80 pa_sample_spec_snprint(st, sizeof(st), spec);
81 fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
82
83 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
84
85 return i;
86 }
87
88 void pa_sink_input_free(struct pa_sink_input* i) {
89 assert(i);
90
91 assert(i->sink && i->sink->core);
92 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
93 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
94
95 if (i->resampled_chunk.memblock)
96 pa_memblock_unref(i->resampled_chunk.memblock);
97 if (i->resampler)
98 pa_resampler_free(i->resampler);
99
100 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
101
102 pa_xfree(i->name);
103 pa_xfree(i);
104 }
105
106 void pa_sink_input_kill(struct pa_sink_input*i) {
107 assert(i);
108
109 if (i->kill)
110 i->kill(i);
111 }
112
113 pa_usec_t pa_sink_input_get_latency(struct pa_sink_input *i) {
114 assert(i);
115
116 if (i->get_latency)
117 return i->get_latency(i);
118
119 return 0;
120 }
121
122 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
123 assert(i && chunk && i->peek && i->drop);
124
125 if (i->corked)
126 return -1;
127
128 if (!i->resampler)
129 return i->peek(i, chunk);
130
131 if (!i->resampled_chunk.memblock) {
132 struct pa_memchunk tchunk;
133 size_t l;
134 int ret;
135
136 if ((ret = i->peek(i, &tchunk)) < 0)
137 return ret;
138
139 assert(tchunk.length);
140
141 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
142
143 if (tchunk.length > l)
144 tchunk.length = l;
145
146 i->drop(i, &tchunk, tchunk.length);
147
148 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
149 pa_memblock_unref(tchunk.memblock);
150 }
151
152 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
153 *chunk = i->resampled_chunk;
154 pa_memblock_ref(i->resampled_chunk.memblock);
155 return 0;
156 }
157
158 void pa_sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
159 assert(i && length);
160
161 if (!i->resampler) {
162 i->drop(i, chunk, length);
163 return;
164 }
165
166 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
167
168 i->resampled_chunk.index += length;
169 i->resampled_chunk.length -= length;
170
171 if (!i->resampled_chunk.length) {
172 pa_memblock_unref(i->resampled_chunk.memblock);
173 i->resampled_chunk.memblock = NULL;
174 i->resampled_chunk.index = i->resampled_chunk.length = 0;
175 }
176 }
177
178 void pa_sink_input_set_volume(struct pa_sink_input *i, pa_volume_t volume) {
179 assert(i && i->sink && i->sink->core);
180
181 if (i->volume != volume) {
182 i->volume = volume;
183 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
184 }
185 }
186
187 void pa_sink_input_cork(struct pa_sink_input *i, int b) {
188 int n;
189 assert(i);
190 n = i->corked && !b;
191 i->corked = b;
192
193 if (n)
194 pa_sink_notify(i->sink);
195 }