1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/* PipeWire
*
* Copyright © 2021 Wim Taymans <wim.taymans@gmail.com>
* Copyright © 2021 Arun Raghavan <arun@asymptotic.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <spa/param/audio/format-utils.h>
#include <spa/utils/hook.h>
#include <spa/utils/json.h>
#include <pipewire/pipewire.h>
#include "../defs.h"
#include "../module.h"
#define NAME "loopback"
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
#define PW_LOG_TOPIC_DEFAULT mod_topic
struct module_loopback_data {
struct module *module;
struct pw_impl_module *mod;
struct spa_hook mod_listener;
struct pw_properties *capture_props;
struct pw_properties *playback_props;
struct spa_audio_info_raw info;
uint32_t latency_msec;
};
static void module_destroy(void *data)
{
struct module_loopback_data *d = data;
spa_hook_remove(&d->mod_listener);
d->mod = NULL;
module_schedule_unload(d->module);
}
static const struct pw_impl_module_events module_events = {
PW_VERSION_IMPL_MODULE_EVENTS,
.destroy = module_destroy
};
static int module_loopback_load(struct module *module)
{
struct module_loopback_data *data = module->user_data;
FILE *f;
char *args;
size_t size, i;
char val[256];
pw_properties_setf(data->capture_props, PW_KEY_NODE_GROUP, "loopback-%u", module->index);
pw_properties_setf(data->playback_props, PW_KEY_NODE_GROUP, "loopback-%u", module->index);
pw_properties_setf(data->capture_props, "pulse.module.id", "%u", module->index);
pw_properties_setf(data->playback_props, "pulse.module.id", "%u", module->index);
if ((f = open_memstream(&args, &size)) == NULL)
return -errno;
fprintf(f, "{");
if (data->info.channels != 0) {
fprintf(f, " audio.channels = %u", data->info.channels);
if (!(data->info.flags & SPA_AUDIO_FLAG_UNPOSITIONED)) {
fprintf(f, " audio.position = [ ");
for (i = 0; i < data->info.channels; i++)
fprintf(f, "%s%s", i == 0 ? "" : ",",
channel_id2name(data->info.position[i]));
fprintf(f, " ]");
}
}
if (data->latency_msec != 0)
fprintf(f, " target.delay.sec = %s",
spa_json_format_float(val, sizeof(val),
data->latency_msec / 1000.0f));
fprintf(f, " capture.props = {");
pw_properties_serialize_dict(f, &data->capture_props->dict, 0);
fprintf(f, " } playback.props = {");
pw_properties_serialize_dict(f, &data->playback_props->dict, 0);
fprintf(f, " } }");
fclose(f);
data->mod = pw_context_load_module(module->impl->context,
"libpipewire-module-loopback",
args, NULL);
free(args);
if (data->mod == NULL)
return -errno;
pw_impl_module_add_listener(data->mod,
&data->mod_listener,
&module_events, data);
return 0;
}
static int module_loopback_unload(struct module *module)
{
struct module_loopback_data *d = module->user_data;
if (d->mod) {
spa_hook_remove(&d->mod_listener);
pw_impl_module_destroy(d->mod);
d->mod = NULL;
}
pw_properties_free(d->capture_props);
pw_properties_free(d->playback_props);
return 0;
}
static const struct spa_dict_item module_loopback_info[] = {
{ PW_KEY_MODULE_AUTHOR, "Arun Raghavan <arun@asymptotic.io>" },
{ PW_KEY_MODULE_DESCRIPTION, "Loopback from source to sink" },
{ PW_KEY_MODULE_USAGE, "source=<source to connect to> "
"sink=<sink to connect to> "
"latency_msec=<latency in ms> "
"rate=<sample rate> "
"channels=<number of channels> "
"channel_map=<channel map> "
"sink_input_properties=<proplist> "
"source_output_properties=<proplist> "
"source_dont_move=<boolean> "
"sink_dont_move=<boolean> "
"remix=<remix channels?> " },
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
};
static int module_loopback_prepare(struct module * const module)
{
struct module_loopback_data * const d = module->user_data;
struct pw_properties * const props = module->props;
struct pw_properties *playback_props = NULL, *capture_props = NULL;
const char *str;
struct spa_audio_info_raw info = { 0 };
int res;
PW_LOG_TOPIC_INIT(mod_topic);
capture_props = pw_properties_new(NULL, NULL);
playback_props = pw_properties_new(NULL, NULL);
if (!capture_props || !playback_props) {
res = -EINVAL;
goto out;
}
/* The following modargs are not implemented:
* adjust_time, max_latency_msec, fast_adjust_threshold_msec: these are just not relevant
*/
if ((str = pw_properties_get(props, "source")) != NULL) {
if (spa_strendswith(str, ".monitor")) {
pw_properties_setf(capture_props, PW_KEY_TARGET_OBJECT,
"%.*s", (int)strlen(str)-8, str);
pw_properties_set(capture_props, PW_KEY_STREAM_CAPTURE_SINK,
"true");
} else {
pw_properties_set(capture_props, PW_KEY_TARGET_OBJECT, str);
}
pw_properties_set(props, "source", NULL);
}
if ((str = pw_properties_get(props, "sink")) != NULL) {
pw_properties_set(playback_props, PW_KEY_TARGET_OBJECT, str);
pw_properties_set(props, "sink", NULL);
}
if (module_args_to_audioinfo(module->impl, props, &info) < 0) {
res = -EINVAL;
goto out;
}
if ((str = pw_properties_get(props, "source_dont_move")) != NULL) {
pw_properties_set(capture_props, PW_KEY_NODE_DONT_RECONNECT, str);
pw_properties_set(props, "source_dont_move", NULL);
}
if ((str = pw_properties_get(props, "sink_dont_move")) != NULL) {
pw_properties_set(playback_props, PW_KEY_NODE_DONT_RECONNECT, str);
pw_properties_set(props, "sink_dont_move", NULL);
}
if ((str = pw_properties_get(props, "remix")) != NULL) {
/* Note that the boolean is inverted */
pw_properties_set(playback_props, PW_KEY_STREAM_DONT_REMIX,
module_args_parse_bool(str) ? "false" : "true");
pw_properties_set(props, "remix", NULL);
}
if ((str = pw_properties_get(props, "latency_msec")) != NULL)
d->latency_msec = atoi(str);
if ((str = pw_properties_get(props, "sink_input_properties")) != NULL) {
module_args_add_props(playback_props, str);
pw_properties_set(props, "sink_input_properties", NULL);
}
if ((str = pw_properties_get(props, "source_output_properties")) != NULL) {
module_args_add_props(capture_props, str);
pw_properties_set(props, "source_output_properties", NULL);
}
d->module = module;
d->capture_props = capture_props;
d->playback_props = playback_props;
d->info = info;
return 0;
out:
pw_properties_free(playback_props);
pw_properties_free(capture_props);
return res;
}
DEFINE_MODULE_INFO(module_loopback) = {
.name = "module-loopback",
.prepare = module_loopback_prepare,
.load = module_loopback_load,
.unload = module_loopback_unload,
.properties = &SPA_DICT_INIT_ARRAY(module_loopback_info),
.data_size = sizeof(struct module_loopback_data),
};
|