4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/core-error.h>
31 #include <pulsecore/namereg.h>
32 #include <pulsecore/sink.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/modargs.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/thread.h>
38 #include <pulsecore/thread-mq.h>
39 #include <pulsecore/rtpoll.h>
41 #include "module-remap-sink-symdef.h"
43 PA_MODULE_AUTHOR("Lennart Poettering");
44 PA_MODULE_DESCRIPTION("Virtual channel remapping sink");
45 PA_MODULE_VERSION(PACKAGE_VERSION
);
46 PA_MODULE_LOAD_ONCE(FALSE
);
48 "sink_name=<name for the sink> "
49 "master=<name of sink to remap> "
50 "master_channel_map=<channel map> "
51 "format=<sample format> "
52 "channels=<number of channels> "
54 "channel_map=<channel map>");
60 pa_sink
*sink
, *master
;
61 pa_sink_input
*sink_input
;
64 static const char* const valid_modargs
[] = {
75 /* Called from I/O thread context */
76 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
77 struct userdata
*u
= PA_SINK(o
)->userdata
;
81 case PA_SINK_MESSAGE_GET_LATENCY
: {
84 /* Get the latency of the master sink */
85 if (PA_MSGOBJECT(u
->master
)->process_msg(PA_MSGOBJECT(u
->master
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
88 /* Add the latency internal to our sink input on top */
89 usec
+= pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->master
->sample_spec
);
91 *((pa_usec_t
*) data
) = usec
;
96 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
99 /* Called from main context */
100 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
103 pa_sink_assert_ref(s
);
104 pa_assert_se(u
= s
->userdata
);
106 if (PA_SINK_IS_LINKED(state
) &&
108 PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
110 pa_sink_input_cork(u
->sink_input
, state
== PA_SINK_SUSPENDED
);
115 /* Called from I/O thread context */
116 static void sink_request_rewind(pa_sink
*s
) {
119 pa_sink_assert_ref(s
);
120 pa_assert_se(u
= s
->userdata
);
122 pa_sink_input_request_rewind(u
->sink_input
, s
->thread_info
.rewind_nbytes
, FALSE
, FALSE
);
125 /* Called from I/O thread context */
126 static void sink_update_requested_latency(pa_sink
*s
) {
129 pa_sink_assert_ref(s
);
130 pa_assert_se(u
= s
->userdata
);
132 /* Just hand this one over to the master sink */
133 pa_sink_input_set_requested_latency_within_thread(
135 pa_sink_get_requested_latency_within_thread(s
));
138 /* Called from I/O thread context */
139 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
142 pa_sink_input_assert_ref(i
);
144 pa_assert_se(u
= i
->userdata
);
146 pa_sink_render(u
->sink
, nbytes
, chunk
);
150 /* Called from I/O thread context */
151 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
154 pa_sink_input_assert_ref(i
);
155 pa_assert_se(u
= i
->userdata
);
156 pa_assert(nbytes
> 0);
158 pa_sink_process_rewind(u
->sink
, nbytes
);
161 /* Called from I/O thread context */
162 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
165 pa_sink_input_assert_ref(i
);
166 pa_assert_se(u
= i
->userdata
);
168 pa_sink_set_max_rewind(u
->sink
, nbytes
);
171 /* Called from I/O thread context */
172 static void sink_input_detach_cb(pa_sink_input
*i
) {
175 pa_sink_input_assert_ref(i
);
176 pa_assert_se(u
= i
->userdata
);
178 pa_sink_detach_within_thread(u
->sink
);
180 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
181 pa_sink_set_rtpoll(u
->sink
, NULL
);
184 /* Called from I/O thread context */
185 static void sink_input_attach_cb(pa_sink_input
*i
) {
188 pa_sink_input_assert_ref(i
);
189 pa_assert_se(u
= i
->userdata
);
191 pa_sink_set_asyncmsgq(u
->sink
, i
->sink
->asyncmsgq
);
192 pa_sink_set_rtpoll(u
->sink
, i
->sink
->rtpoll
);
194 pa_sink_attach_within_thread(u
->sink
);
197 /* Called from main context */
198 static void sink_input_kill_cb(pa_sink_input
*i
) {
201 pa_sink_input_assert_ref(i
);
202 pa_assert_se(u
= i
->userdata
);
204 pa_sink_input_unlink(u
->sink_input
);
205 pa_sink_input_unref(u
->sink_input
);
206 u
->sink_input
= NULL
;
208 pa_sink_unlink(u
->sink
);
209 pa_sink_unref(u
->sink
);
212 pa_module_unload_request(u
->module
);
215 int pa__init(pa_module
*m
) {
218 pa_channel_map sink_map
, stream_map
;
223 pa_sink_input_new_data sink_input_data
;
224 pa_sink_new_data sink_data
;
228 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
229 pa_log("Failed to parse module arguments.");
233 if (!(master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "master", NULL
), PA_NAMEREG_SINK
, 1))) {
234 pa_log("Master sink not found");
238 ss
= master
->sample_spec
;
239 sink_map
= master
->channel_map
;
240 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &sink_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
241 pa_log("Invalid sample format specification or channel map");
245 stream_map
= sink_map
;
246 if (pa_modargs_get_channel_map(ma
, "master_channel_map", &stream_map
) < 0) {
247 pa_log("Invalid master hannel map");
251 if (stream_map
.channels
!= ss
.channels
) {
252 pa_log("Number of channels doesn't match");
256 if (pa_channel_map_equal(&stream_map
, &master
->channel_map
))
257 pa_log_warn("No remapping configured, proceeding nonetheless!");
259 u
= pa_xnew0(struct userdata
, 1);
265 u
->sink_input
= NULL
;
268 pa_sink_new_data_init(&sink_data
);
269 sink_data
.driver
= __FILE__
;
270 sink_data
.module
= m
;
271 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
272 sink_data
.name
= pa_sprintf_malloc("%s.remapped", master
->name
);
273 pa_sink_new_data_set_sample_spec(&sink_data
, &ss
);
274 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
275 k
= pa_proplist_gets(master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
276 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, t
= pa_sprintf_malloc("Remapped %s", k
? k
: master
->name
));
278 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, master
->name
);
280 u
->sink
= pa_sink_new(m
->core
, &sink_data
, 0);
281 pa_sink_new_data_done(&sink_data
);
284 pa_log("Failed to create sink.");
288 u
->sink
->parent
.process_msg
= sink_process_msg
;
289 u
->sink
->set_state
= sink_set_state
;
290 u
->sink
->update_requested_latency
= sink_update_requested_latency
;
291 u
->sink
->request_rewind
= sink_request_rewind
;
292 u
->sink
->userdata
= u
;
293 u
->sink
->flags
= PA_SINK_LATENCY
;
295 pa_sink_set_asyncmsgq(u
->sink
, master
->asyncmsgq
);
296 pa_sink_set_rtpoll(u
->sink
, master
->rtpoll
);
298 /* Create sink input */
299 pa_sink_input_new_data_init(&sink_input_data
);
300 sink_input_data
.driver
= __FILE__
;
301 sink_input_data
.module
= m
;
302 sink_input_data
.sink
= u
->master
;
303 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Remapped Stream");
304 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "routing");
305 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &ss
);
306 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &stream_map
);
308 u
->sink_input
= pa_sink_input_new(m
->core
, &sink_input_data
, PA_SINK_INPUT_DONT_MOVE
);
309 pa_sink_input_new_data_done(&sink_input_data
);
314 u
->sink_input
->pop
= sink_input_pop_cb
;
315 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
316 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
317 u
->sink_input
->kill
= sink_input_kill_cb
;
318 u
->sink_input
->attach
= sink_input_attach_cb
;
319 u
->sink_input
->detach
= sink_input_detach_cb
;
320 u
->sink_input
->userdata
= u
;
322 pa_sink_put(u
->sink
);
323 pa_sink_input_put(u
->sink_input
);
338 void pa__done(pa_module
*m
) {
343 if (!(u
= m
->userdata
))
347 pa_sink_input_unlink(u
->sink_input
);
348 pa_sink_input_unref(u
->sink_input
);
352 pa_sink_unlink(u
->sink
);
353 pa_sink_unref(u
->sink
);