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"
40 #define MAX_MIX_CHANNELS 32
42 struct pa_sink
* pa_sink_new(struct pa_core
*core
, const char *name
, int fail
, const struct pa_sample_spec
*spec
) {
47 assert(core
&& name
&& *name
&& spec
);
49 s
= pa_xmalloc(sizeof(struct pa_sink
));
51 if (!(name
= pa_namereg_register(core
, name
, PA_NAMEREG_SINK
, s
, fail
))) {
56 s
->name
= pa_xstrdup(name
);
57 s
->description
= NULL
;
61 s
->sample_spec
= *spec
;
62 s
->inputs
= pa_idxset_new(NULL
, NULL
);
64 n
= pa_sprintf_malloc("%s_monitor", name
);
65 s
->monitor_source
= pa_source_new(core
, n
, 0, spec
);
66 assert(s
->monitor_source
);
68 s
->monitor_source
->monitor_of
= s
;
69 s
->monitor_source
->description
= pa_sprintf_malloc("Monitor source of sink '%s'", s
->name
);
71 s
->volume
= PA_VOLUME_NORM
;
74 s
->get_latency
= NULL
;
77 r
= pa_idxset_put(core
->sinks
, s
, &s
->index
);
78 assert(s
->index
!= PA_IDXSET_INVALID
&& r
>= 0);
80 pa_sample_spec_snprint(st
, sizeof(st
), spec
);
81 pa_log(__FILE__
": created %u \"%s\" with sample spec \"%s\"\n", s
->index
, s
->name
, st
);
83 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
88 void pa_sink_free(struct pa_sink
*s
) {
89 struct pa_sink_input
*i
, *j
= NULL
;
92 pa_namereg_unregister(s
->core
, s
->name
);
94 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
96 pa_sink_input_kill(i
);
99 pa_idxset_free(s
->inputs
, NULL
, NULL
);
101 pa_source_free(s
->monitor_source
);
102 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
104 pa_log(__FILE__
": freed %u \"%s\"\n", s
->index
, s
->name
);
106 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
109 pa_xfree(s
->description
);
113 void pa_sink_notify(struct pa_sink
*s
) {
120 static unsigned fill_mix_info(struct pa_sink
*s
, struct pa_mix_info
*info
, unsigned maxinfo
) {
121 uint32_t index
= PA_IDXSET_INVALID
;
122 struct pa_sink_input
*i
;
127 for (i
= pa_idxset_first(s
->inputs
, &index
); maxinfo
> 0 && i
; i
= pa_idxset_next(s
->inputs
, &index
)) {
128 if (pa_sink_input_peek(i
, &info
->chunk
) < 0)
131 info
->volume
= i
->volume
;
133 assert(info
->chunk
.memblock
&& info
->chunk
.memblock
->data
&& info
->chunk
.length
);
144 static void inputs_drop(struct pa_sink
*s
, struct pa_mix_info
*info
, unsigned maxinfo
, size_t length
) {
147 for (; maxinfo
> 0; maxinfo
--, info
++) {
148 struct pa_sink_input
*i
= info
->userdata
;
149 assert(i
&& info
->chunk
.memblock
);
151 pa_sink_input_drop(i
, &info
->chunk
, length
);
152 pa_memblock_unref(info
->chunk
.memblock
);
156 int pa_sink_render(struct pa_sink
*s
, size_t length
, struct pa_memchunk
*result
) {
157 struct pa_mix_info info
[MAX_MIX_CHANNELS
];
160 assert(s
&& length
&& result
);
162 n
= fill_mix_info(s
, info
, MAX_MIX_CHANNELS
);
168 uint32_t volume
= PA_VOLUME_NORM
;
169 struct pa_sink_input
*i
= info
[0].userdata
;
171 *result
= info
[0].chunk
;
172 pa_memblock_ref(result
->memblock
);
174 if (result
->length
> length
)
175 result
->length
= length
;
179 if (s
->volume
!= PA_VOLUME_NORM
|| info
[0].volume
!= PA_VOLUME_NORM
)
180 volume
= pa_volume_multiply(s
->volume
, info
[0].volume
);
182 if (volume
!= PA_VOLUME_NORM
) {
183 pa_memchunk_make_writable(result
, s
->core
->memblock_stat
);
184 pa_volume_memchunk(result
, &s
->sample_spec
, volume
);
187 result
->memblock
= pa_memblock_new(length
, s
->core
->memblock_stat
);
188 assert(result
->memblock
);
190 result
->length
= l
= pa_mix(info
, n
, result
->memblock
->data
, length
, &s
->sample_spec
, s
->volume
);
196 inputs_drop(s
, info
, n
, l
);
198 assert(s
->monitor_source
);
199 pa_source_post(s
->monitor_source
, result
);
204 int pa_sink_render_into(struct pa_sink
*s
, struct pa_memchunk
*target
) {
205 struct pa_mix_info info
[MAX_MIX_CHANNELS
];
208 assert(s
&& target
&& target
->length
&& target
->memblock
&& target
->memblock
->data
);
210 n
= fill_mix_info(s
, info
, MAX_MIX_CHANNELS
);
216 uint32_t volume
= PA_VOLUME_NORM
;
217 struct pa_sink_info
*i
= info
[0].userdata
;
221 if (l
> info
[0].chunk
.length
)
222 l
= info
[0].chunk
.length
;
224 memcpy((uint8_t*) target
->memblock
->data
+target
->index
, (uint8_t*) info
[0].chunk
.memblock
->data
+ info
[0].chunk
.index
, l
);
227 if (s
->volume
!= PA_VOLUME_NORM
|| info
[0].volume
!= PA_VOLUME_NORM
)
228 volume
= pa_volume_multiply(s
->volume
, info
[0].volume
);
230 if (volume
!= PA_VOLUME_NORM
)
231 pa_volume_memchunk(target
, &s
->sample_spec
, volume
);
233 target
->length
= l
= pa_mix(info
, n
, (uint8_t*) target
->memblock
->data
+target
->index
, target
->length
, &s
->sample_spec
, s
->volume
);
236 inputs_drop(s
, info
, n
, l
);
238 assert(s
->monitor_source
);
239 pa_source_post(s
->monitor_source
, target
);
244 void pa_sink_render_into_full(struct pa_sink
*s
, struct pa_memchunk
*target
) {
245 struct pa_memchunk chunk
;
247 assert(s
&& target
&& target
->memblock
&& target
->length
&& target
->memblock
->data
);
256 if (pa_sink_render_into(s
, &chunk
) < 0)
267 pa_silence_memchunk(&chunk
, &s
->sample_spec
);
271 void pa_sink_render_full(struct pa_sink
*s
, size_t length
, struct pa_memchunk
*result
) {
272 assert(s
&& length
&& result
);
274 /*** This needs optimization ***/
276 result
->memblock
= pa_memblock_new(result
->length
= length
, s
->core
->memblock_stat
);
279 pa_sink_render_into_full(s
, result
);
282 pa_usec_t
pa_sink_get_latency(struct pa_sink
*s
) {
288 return s
->get_latency(s
);
291 void pa_sink_set_owner(struct pa_sink
*sink
, struct pa_module
*m
) {
294 if (sink
->monitor_source
)
295 pa_source_set_owner(sink
->monitor_source
, m
);
298 void pa_sink_set_volume(struct pa_sink
*sink
, pa_volume_t volume
) {
301 if (sink
->volume
!= volume
) {
302 sink
->volume
= volume
;
303 pa_subscription_post(sink
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, sink
->index
);