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
32 #include "sink-input.h"
35 #include "sample-util.h"
37 #include "subscribe.h"
39 #define MAX_MIX_CHANNELS 32
41 struct pa_sink* pa_sink_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
46 assert(core && name && *name && spec);
48 s = pa_xmalloc(sizeof(struct pa_sink));
50 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
55 s->name = pa_xstrdup(name);
56 s->description = NULL;
60 s->sample_spec = *spec;
61 s->inputs = pa_idxset_new(NULL, NULL);
63 n = pa_sprintf_malloc("%s_monitor", name);
64 s->monitor_source = pa_source_new(core, n, 0, spec);
65 assert(s->monitor_source);
67 s->monitor_source->monitor_of = s;
68 s->monitor_source->description = pa_sprintf_malloc("Monitor source of sink '%s'", s->name);
70 s->volume = PA_VOLUME_NORM;
73 s->get_latency = NULL;
76 r = pa_idxset_put(core->sinks, s, &s->index);
77 assert(s->index != PA_IDXSET_INVALID && r >= 0);
79 pa_sample_snprint(st, sizeof(st), spec);
80 fprintf(stderr, "sink: created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
82 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
87 void pa_sink_free(struct pa_sink *s) {
88 struct pa_sink_input *i, *j = NULL;
91 pa_namereg_unregister(s->core, s->name);
93 while ((i = pa_idxset_first(s->inputs, NULL))) {
95 pa_sink_input_kill(i);
98 pa_idxset_free(s->inputs, NULL, NULL);
100 pa_source_free(s->monitor_source);
101 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
103 fprintf(stderr, "sink: freed %u \"%s\"\n", s->index, s->name);
105 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
108 pa_xfree(s->description);
112 void pa_sink_notify(struct pa_sink*s) {
119 static unsigned fill_mix_info(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo) {
120 uint32_t index = PA_IDXSET_INVALID;
121 struct pa_sink_input *i;
126 for (i = pa_idxset_first(s->inputs, &index); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &index)) {
127 if (pa_sink_input_peek(i, &info->chunk) < 0)
130 info->volume = i->volume;
132 assert(info->chunk.memblock && info->chunk.memblock->data && info->chunk.length);
143 static void inputs_drop(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo, size_t length) {
146 for (; maxinfo > 0; maxinfo--, info++) {
147 struct pa_sink_input *i = info->userdata;
148 assert(i && info->chunk.memblock);
150 pa_memblock_unref(info->chunk.memblock);
151 pa_sink_input_drop(i, length);
155 int pa_sink_render(struct pa_sink*s, size_t length, struct pa_memchunk *result) {
156 struct pa_mix_info info[MAX_MIX_CHANNELS];
159 assert(s && length && result);
161 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
167 uint32_t volume = PA_VOLUME_NORM;
168 struct pa_sink_input *i = info[0].userdata;
170 *result = info[0].chunk;
171 pa_memblock_ref(result->memblock);
173 if (result->length > length)
174 result->length = length;
178 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
179 volume = pa_volume_multiply(s->volume, info[0].volume);
181 if (volume != PA_VOLUME_NORM) {
182 pa_memchunk_make_writable(result);
183 pa_volume_memchunk(result, &s->sample_spec, volume);
186 result->memblock = pa_memblock_new(length);
187 assert(result->memblock);
189 result->length = l = pa_mix(info, n, result->memblock->data, length, &s->sample_spec, s->volume);
195 inputs_drop(s, info, n, l);
197 assert(s->monitor_source);
198 pa_source_post(s->monitor_source, result);
203 int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target) {
204 struct pa_mix_info info[MAX_MIX_CHANNELS];
207 assert(s && target && target->length && target->memblock && target->memblock->data);
209 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
215 uint32_t volume = PA_VOLUME_NORM;
216 struct pa_sink_info *i = info[0].userdata;
220 if (l > info[0].chunk.length)
221 l = info[0].chunk.length;
223 memcpy(target->memblock->data+target->index, info[0].chunk.memblock->data + info[0].chunk.index, l);
226 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
227 volume = pa_volume_multiply(s->volume, info[0].volume);
229 if (volume != PA_VOLUME_NORM)
230 pa_volume_memchunk(target, &s->sample_spec, volume);
232 target->length = l = pa_mix(info, n, target->memblock->data+target->index, target->length, &s->sample_spec, s->volume);
235 inputs_drop(s, info, n, l);
237 assert(s->monitor_source);
238 pa_source_post(s->monitor_source, target);
243 void pa_sink_render_into_full(struct pa_sink *s, struct pa_memchunk *target) {
244 struct pa_memchunk chunk;
246 assert(s && target && target->memblock && target->length && target->memblock->data);
255 if (pa_sink_render_into(s, &chunk) < 0)
266 pa_silence_memchunk(&chunk, &s->sample_spec);
270 uint32_t pa_sink_get_latency(struct pa_sink *s) {
276 return s->get_latency(s);
279 void pa_sink_set_owner(struct pa_sink *sink, struct pa_module *m) {
282 if (sink->monitor_source)
283 pa_source_set_owner(sink->monitor_source, m);
286 void pa_sink_set_volume(struct pa_sink *sink, pa_volume_t volume) {
289 if (sink->volume != volume) {
290 sink->volume = volume;
291 pa_subscription_post(sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, sink->index);