summaryrefslogtreecommitdiffstats
path: root/switch.c
blob: 954effd25b95951a2920ecec35b1220058cee287 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * module-agl-audio -- PulseAudio module for providing audio routing support
 * (forked from "module-murphy-ivi" - https://github.com/otcshare )
 * Copyright (c) 2012, Intel Corporation.
 * Copyright (c) 2016, IoT.bzh
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 */
#include <pulsecore/pulsecore-config.h>	/* required for headers below */
#include <pulsecore/core-util.h>	/* requred for "pa_streq" */
#include <pulsecore/namereg.h>		/* for PA_NAMEREG_SOURCE */

#include "utils.h"
#include "switch.h"
#include "node.h"

bool agl_switch_setup_link (struct userdata *u, agl_node *from, agl_node *to, bool explicit)
{
	pa_core *core;
	pa_sink *sink;
	pa_source *source;

	pa_assert (u);
	pa_assert_se (core = u->core);

	/* EXPLICIT ROUTES/DEFAULT ROUTES */

	/* 1) EXPLICIT ROUTES : "FROM" AND "TO" ARE DEFINED */
	if (explicit) {
		pa_assert (from);
		pa_assert (to);

		switch (from->implement) {
			/* STREAM SOURCE */
			case agl_stream:
			switch (to->implement) {
				/* STREAM TO STREAM : NOT IMPLEMENTED */
				case agl_stream:
					pa_log_debug ("routing to streams not implemented");
					break;
				/* STREAM TO DEVICE : OK */
				case agl_device:
					//if (!setup_explicit_stream2dev_link (u, from, to))
					//	return false;
					break;
				/* DEFAULT */
				default:
					pa_log ("can't setup link: invalid sink node");
					return false;
			}
			break;

			/* DEVICE SOURCE : NOT IMPLEMENTED */
			case agl_device:
				pa_log_debug("input device routing is not implemented yet");
				break;

			/* DEFAULT */
			default:
				pa_log ("can't setup link: invalid sink node");
				return false;
		}
	}

	/* 2) DEFAULT ROUTES : EITHER ONE OF "FROM" AND "TO" ARE DEFINED */
	else {
		pa_assert (from || to);

		/* "TO" IS DEFINED */
		if (to) {
			switch (to->implement) {
				/* STREAM DESTINATION */
				case agl_stream:
				switch (from->implement) {
					/* STREAM TO STREAM : NOT IMPLEMENTED */
					case agl_stream:
						pa_log_debug ("routing to streams not implemented");
						break;
					/* DEVICE TO STREAM : OK */
					case agl_device:
						//if (!setup_default_dev2stream_link(u, from, to))
 						//	return false;
						break;
					/* DEFAULT */
					default:
						pa_log ("can't setup link: invalid sink node");
						return false;
				}
				break;

				/* DEVICE DESTINATION */
				case agl_device:
				/* IF THERE IS NO SOURCE : DEFAULT OUTPUT PREROUTE */
				/* if (!from)
					return setup_device_output(u, to) != NULL;
				else { */
				switch (from->implement) {
					/* STREAM TO DEVICE : OK */
					case agl_stream:
						//if (!setup_default_stream2dev_link (u, from, to))
						//	return false;
						break;
					/* DEVICE TO DEVICE : OK */
					case agl_device:
						//if (!setup_default_dev2dev_link (u, from, to))
						//	return false;
						break;
					/* DEFAULT */
					default:
						pa_log ("can't setup link: invalid source node");
						return false;
				}
				break;
				/* } */

				/* DEFAULT DESTINATION : NULL */
				default:
					pa_log ("can't setup link");
					return false;
			}
		}

		/* ONLY "FROM" IS DEFINED */
		else {
			/* only works with a stream, use default input prerouting */
			if (from->implement == agl_device) {
				pa_log_debug ("default routing for a device input is not supported yet");
				return false;
			}
			/* (the rest supposes "from->implement == agl_stream") */
			/* refuse unknown node types for default routing */
			if (from->type == agl_node_type_unknown) {
				pa_log_debug ("default routing for unknown node type is refused");
				return false;
			}

			sink = agl_utils_get_primary_alsa_sink (u);
			source = agl_utils_get_null_source (u, from->nullsink);

			from->loopnode = agl_loopnode_create (u, AGL_LOOPNODE_SINK, from->index, source->index, sink->index);
		}
	}

	//pa_log_debug ("link %s => %s is established", from->amname, to->amname);

	return true;
}

bool agl_switch_teardown_link (struct userdata *u, agl_node *from, agl_node *to)
{
	pa_core *core;

	pa_assert (u);
	pa_assert_se (core = u->core);

	pa_assert (from || to);

	/* "TO" IS DEFINED */
	if (to) {

	}
	/* ONLY "FROM" IS DEFINED */
	else {
		/* only works with a stream */
		if (from->implement == agl_device) {
			pa_log_debug ("default routing for a device input is not supported");
			return false;
		}
		/* (the rest supposes "from->implement == agl_stream") */
		if (from->loopnode)
			agl_loopnode_destroy (u, from->loopnode);
		if (from->nullsink)
			agl_utils_destroy_null_sink (u, from->nullsink);
	}

	//pa_log_debug("link %s => %s is torn down", from->amname, to->amname);

	return true;
}


bool set_port (struct userdata *u, agl_node *node)
{
	pa_core *core;
	pa_sink *sink;
	pa_source *source;
	pa_device_port *port;
	void *data = NULL;
	uint32_t paidx = PA_IDXSET_INVALID;

	pa_assert (u);
	pa_assert (node);
	pa_assert (node->paname);
	pa_assert_se (core = u->core);

	if (node->direction != agl_input && node->direction != agl_output)
		return false;
	if (node->implement != agl_device)
		return true;
	if (!node->paport)
		return true;

	if (node->direction == agl_input) {
		source = pa_namereg_get (core, node->paname, PA_NAMEREG_SOURCE);
		if (!source) {
			pa_log ("cannot set port for node '%s': source not found", node->paname);
			return false;
		}

		port = source->active_port;
		/* active port and wanted port already match */
		if (pa_streq (node->paport, port->name))
			return true;

		/* ACTIVE CODE */
		if (pa_source_set_port (source, node->paport, false) < 0)
			return false;

		data = source;
		paidx = source->index;
	}

	if (node->direction == agl_output) {
		sink = pa_namereg_get (core, node->paname, PA_NAMEREG_SINK);
		if (!sink) {
			pa_log ("cannot set port for node '%s': source not found", node->paname);
			return false;
		}

		port = sink->active_port;
		/* active port and wanted port already match */
		if (pa_streq (node->paport, port->name))
			return true;

		/* ACTIVE CODE */
		if (pa_sink_set_port (sink, node->paport, false) < 0)
			return false;

		data = sink;
		paidx = sink->index;
	}

	return true;
}