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"
33 #include "memblockq.h"
40 PA_MODULE_AUTHOR("Lennart Poettering")
41 PA_MODULE_DESCRIPTION("Makes one playback device out of many")
42 PA_MODULE_VERSION(PACKAGE_VERSION
)
43 PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slave=<slave sinks>")
45 #define DEFAULT_SINK_NAME "combine"
46 #define MEMBLOCKQ_MAXLENGTH (1024*170)
47 #define RENDER_SIZE (1024*10)
51 static const char* const valid_modargs
[] = {
59 struct userdata
*userdata
;
60 struct pa_sink_input
*sink_input
;
62 struct pa_memblockq
*memblockq
;
63 pa_usec_t sink_latency
;
64 PA_LLIST_FIELDS(struct output
);
68 struct pa_module
*module
;
72 struct output
*master
;
73 struct pa_time_event
*time_event
;
75 PA_LLIST_HEAD(struct output
, outputs
);
78 static void output_free(struct output
*o
);
79 static void clear_up(struct userdata
*u
);
81 static void adjust_rates(struct userdata
*u
) {
87 for (o
= u
->outputs
; o
; o
= o
->next
) {
88 o
->sink_latency
= pa_sink_get_latency(o
->sink_input
->sink
);
90 if (o
->sink_latency
> max
)
91 max
= o
->sink_latency
;
94 pa_log(__FILE__
": [%s] maximum latency is %0.0f usec.\n", u
->sink
->name
, (float) max
);
96 base_rate
= u
->sink
->sample_spec
.rate
;
98 for (o
= u
->outputs
; o
; o
= o
->next
) {
100 uint32_t r
= base_rate
;
102 l
= o
->sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
105 r
-= (uint32_t) (((((double) max
-l
))/ADJUST_TIME
)*r
/ 1000000);
107 r
+= (uint32_t) (((((double) l
-max
))/ADJUST_TIME
)*r
/ 1000000);
109 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1))
110 pa_log(__FILE__
": [%s] sample rates too different, not adjusting (%u vs. %u).\n", o
->sink_input
->name
, base_rate
, r
);
112 pa_log(__FILE__
": [%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.\n", o
->sink_input
->name
, r
, (double) r
/ base_rate
, (float) l
);
114 pa_sink_input_set_rate(o
->sink_input
, r
);
118 static void request_memblock(struct userdata
*u
) {
119 struct pa_memchunk chunk
;
121 assert(u
&& u
->sink
);
123 if (pa_sink_render(u
->sink
, RENDER_SIZE
, &chunk
) < 0)
126 for (o
= u
->outputs
; o
; o
= o
->next
)
127 pa_memblockq_push_align(o
->memblockq
, &chunk
, 0);
129 pa_memblock_unref(chunk
.memblock
);
132 static void time_callback(struct pa_mainloop_api
*a
, struct pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
133 struct userdata
*u
= userdata
;
135 assert(u
&& a
&& u
->time_event
== e
);
139 gettimeofday(&n
, NULL
);
140 n
.tv_sec
+= ADJUST_TIME
;
141 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
144 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
145 struct output
*o
= i
->userdata
;
146 assert(i
&& o
&& o
->sink_input
&& chunk
);
148 if (pa_memblockq_peek(o
->memblockq
, chunk
) >= 0)
152 request_memblock(o
->userdata
);
154 return pa_memblockq_peek(o
->memblockq
, chunk
);
157 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
158 struct output
*o
= i
->userdata
;
159 assert(i
&& o
&& o
->sink_input
&& chunk
&& length
);
161 pa_memblockq_drop(o
->memblockq
, chunk
, length
);
162 o
->counter
+= length
;
165 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
166 struct output
*o
= i
->userdata
;
167 assert(i
&& o
&& o
->sink_input
);
168 clear_up(o
->userdata
);
171 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
172 struct output
*o
= i
->userdata
;
173 assert(i
&& o
&& o
->sink_input
);
175 return pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &i
->sample_spec
);
178 static pa_usec_t
sink_get_latency_cb(struct pa_sink
*s
) {
179 struct userdata
*u
= s
->userdata
;
180 assert(s
&& u
&& u
->sink
&& u
->master
);
182 return pa_sink_input_get_latency(u
->master
->sink_input
);
185 static struct output
*output_new(struct userdata
*u
, struct pa_sink
*sink
) {
186 struct output
*o
= NULL
;
188 assert(u
&& sink
&& u
->sink
);
190 o
= pa_xmalloc(sizeof(struct output
));
194 o
->memblockq
= pa_memblockq_new(MEMBLOCKQ_MAXLENGTH
, MEMBLOCKQ_MAXLENGTH
, pa_frame_size(&u
->sink
->sample_spec
), 0, 0, sink
->core
->memblock_stat
);
196 snprintf(t
, sizeof(t
), "%s: output #%u", u
->sink
->name
, u
->n_outputs
+1);
197 if (!(o
->sink_input
= pa_sink_input_new(sink
, t
, &u
->sink
->sample_spec
, 1)))
200 o
->sink_input
->get_latency
= sink_input_get_latency_cb
;
201 o
->sink_input
->peek
= sink_input_peek_cb
;
202 o
->sink_input
->drop
= sink_input_drop_cb
;
203 o
->sink_input
->kill
= sink_input_kill_cb
;
204 o
->sink_input
->userdata
= o
;
205 o
->sink_input
->owner
= u
->module
;
207 PA_LLIST_PREPEND(struct output
, u
->outputs
, o
);
215 pa_sink_input_free(o
->sink_input
);
218 pa_memblockq_free(o
->memblockq
);
226 static void output_free(struct output
*o
) {
228 PA_LLIST_REMOVE(struct output
, o
->userdata
->outputs
, o
);
229 o
->userdata
->n_outputs
--;
230 pa_memblockq_free(o
->memblockq
);
231 pa_sink_input_free(o
->sink_input
);
235 static void clear_up(struct userdata
*u
) {
240 u
->core
->mainloop
->time_free(u
->time_event
);
241 u
->time_event
= NULL
;
244 while ((o
= u
->outputs
))
250 pa_sink_free(u
->sink
);
255 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
257 struct pa_modargs
*ma
= NULL
;
258 const char *master_name
, *slaves
;
259 struct pa_sink
*master_sink
;
261 const char*split_state
;
265 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
266 pa_log(__FILE__
": failed to parse module arguments\n");
270 u
= pa_xmalloc(sizeof(struct userdata
));
277 u
->time_event
= NULL
;
278 PA_LLIST_HEAD_INIT(struct output
, u
->outputs
);
280 if (!(master_name
= pa_modargs_get_value(ma
, "master", NULL
)) || !(slaves
= pa_modargs_get_value(ma
, "slaves", NULL
))) {
281 pa_log(__FILE__
": no master or slave sinks specified\n");
285 if (!(master_sink
= pa_namereg_get(c
, master_name
, PA_NAMEREG_SINK
, 1))) {
286 pa_log(__FILE__
": invalid master sink '%s'\n", master_name
);
290 if (!(u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &master_sink
->sample_spec
))) {
291 pa_log(__FILE__
": failed to create sink\n");
295 pa_sink_set_owner(u
->sink
, m
);
296 u
->sink
->description
= pa_sprintf_malloc("Combined sink");
297 u
->sink
->get_latency
= sink_get_latency_cb
;
298 u
->sink
->userdata
= u
;
300 if (!(u
->master
= output_new(u
, master_sink
))) {
301 pa_log(__FILE__
": failed to create master sink input on sink '%s'.\n", u
->sink
->name
);
306 while ((n
= pa_split(slaves
, ",", &split_state
))) {
307 struct pa_sink
*slave_sink
;
309 if (!(slave_sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
, 1))) {
310 pa_log(__FILE__
": invalid slave sink '%s'\n", n
);
316 if (!output_new(u
, slave_sink
)) {
317 pa_log(__FILE__
": failed to create slave sink input on sink '%s'.\n", slave_sink
->name
);
322 if (u
->n_outputs
<= 1)
323 pa_log(__FILE__
": WARNING: no slave sinks specified.\n");
325 gettimeofday(&tv
, NULL
);
326 tv
.tv_sec
+= ADJUST_TIME
;
327 u
->time_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, time_callback
, u
);
342 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
346 if (!(u
= m
->userdata
))