-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.c
261 lines (224 loc) · 7.09 KB
/
port.c
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
258
259
260
261
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* ALSA SEQ < - > JACK MIDI bridge
*
* Copyright (c) 2006,2007 Dmitry S. Baikov <[email protected]>
* Copyright (c) 2007,2008,2009,2011 Nedko Arnaudov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdbool.h>
#include <ctype.h>
#include <semaphore.h>
#include <alsa/asoundlib.h>
#include <jack/jack.h>
#include <jack/ringbuffer.h>
#include "list.h"
#include "structs.h"
#include "port_hash.h"
#include "log.h"
#include "port.h"
extern bool g_disable_port_uniqueness;
/* This should be part of JACK API */
#define JACK_IS_VALID_PORT_NAME_CHAR(c) \
(isalnum(c) || \
(c) == '/' || \
(c) == '_' || \
(c) == ':' || \
(c) == '(' || \
(c) == ')' || \
(c) == '-' || \
(c) == '[' || \
(c) == ']')
static
int
a2j_alsa_connect_from(
struct a2j * self,
int client,
int port)
{
snd_seq_port_subscribe_t* sub;
snd_seq_addr_t seq_addr;
int err;
snd_seq_port_subscribe_alloca(&sub);
seq_addr.client = client;
seq_addr.port = port;
snd_seq_port_subscribe_set_sender(sub, &seq_addr);
seq_addr.client = self->client_id;
seq_addr.port = self->port_id;
snd_seq_port_subscribe_set_dest(sub, &seq_addr);
snd_seq_port_subscribe_set_time_update(sub, 1);
snd_seq_port_subscribe_set_queue(sub, self->queue);
snd_seq_port_subscribe_set_time_real(sub, 1);
if ((err=snd_seq_subscribe_port(self->seq, sub)))
a2j_error("can't subscribe to %d:%d - %s", client, port, snd_strerror(err));
return err;
}
void
a2j_port_setdead(
a2j_port_hash_t hash,
snd_seq_addr_t addr)
{
struct a2j_port *port = a2j_port_get(hash, addr);
if (port)
{
port->is_dead = true; // see jack_process_internal
}
else
{
a2j_debug("port_setdead: not found (%d:%d)", addr.client, addr.port);
}
}
void
a2j_port_free(
struct a2j_port * port)
{
//snd_seq_disconnect_from(self->seq, self->port_id, port->remote.client, port->remote.port);
//snd_seq_disconnect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
if (port->inbound_events)
jack_ringbuffer_free(port->inbound_events);
if (port->jack_port != JACK_INVALID_PORT)
jack_port_unregister(port->a2j_ptr->jack_client, port->jack_port);
free(port);
}
void
a2j_port_fill_name(
struct a2j_port * port_ptr,
int type,
snd_seq_client_info_t * client_info_ptr,
const snd_seq_port_info_t * port_info_ptr,
bool make_unique)
{
char *c;
int ret;
if (make_unique)
{
ret = snprintf(
port_ptr->name,
g_max_jack_port_name_size,
"%s [%d] (%s): %s",
snd_seq_client_info_get_name(client_info_ptr),
snd_seq_client_info_get_client(client_info_ptr),
type == A2J_PORT_CAPTURE ? "capture": "playback",
snd_seq_port_info_get_name(port_info_ptr));
}
else
{
ret = snprintf(
port_ptr->name,
g_max_jack_port_name_size,
"%s (%s): %s",
snd_seq_client_info_get_name(client_info_ptr),
type == A2J_PORT_CAPTURE ? "capture": "playback",
snd_seq_port_info_get_name(port_info_ptr));
}
// replace all offending characters with ' '
for (c = port_ptr->name; *c; ++c)
if (!JACK_IS_VALID_PORT_NAME_CHAR(*c))
*c = ' ';
if (ret < 0 || ret >= (int)g_max_jack_port_name_size)
{
/* force terminating nul char because the man page does not specify whether the resulting buffer is nul terminated in this case */
port_ptr->name[g_max_jack_port_name_size] = 0;
a2j_warning("port name \"%s\" was truncated because of the max size support by JACK (%zu)", port_ptr->name, g_max_jack_port_name_size);
}
}
struct a2j_port *
a2j_port_create(
struct a2j * self,
int type,
snd_seq_addr_t addr,
const snd_seq_port_info_t * info)
{
struct a2j_port *port;
int err;
int client;
snd_seq_client_info_t * client_info_ptr;
int jack_caps;
struct a2j_stream * stream_ptr;
stream_ptr = &self->stream[type];
err = snd_seq_client_info_malloc(&client_info_ptr);
if (err != 0)
{
a2j_error("Failed to allocate client info");
goto fail;
}
client = snd_seq_port_info_get_client(info);
err = snd_seq_get_any_client_info(self->seq, client, client_info_ptr);
if (err != 0)
{
a2j_error("Failed to get client info");
goto fail_free_client_info;
}
a2j_debug("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr));
a2j_debug("port name: '%s'", snd_seq_port_info_get_name(info));
port = calloc(1, sizeof(struct a2j_port) + g_max_jack_port_name_size);
if (!port)
{
goto fail_free_client_info;
}
port->a2j_ptr = self;
port->jack_port = JACK_INVALID_PORT;
port->remote = addr;
a2j_port_fill_name(port, type, client_info_ptr, info, !g_disable_port_uniqueness);
/* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
list_add_tail(&port->siblings, &stream_ptr->list);
if (type == A2J_PORT_CAPTURE)
{
jack_caps = JackPortIsOutput;
}
else
{
jack_caps = JackPortIsInput;
}
/* mark anything that looks like a hardware port as physical&terminal */
if (snd_seq_port_info_get_type(info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC))
{
jack_caps |= JackPortIsPhysical|JackPortIsTerminal;
}
port->jack_port = jack_port_register(self->jack_client, port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
if (port->jack_port == JACK_INVALID_PORT)
{
a2j_error("jack_port_register() failed for '%s'", port->name);
goto fail_free_port;
}
if (type == A2J_PORT_CAPTURE)
{
err = a2j_alsa_connect_from(self, port->remote.client, port->remote.port);
}
else
{
err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
if (err != 0)
{
a2j_error("snd_seq_connect_to() for %d:%d failed with error %d", (int)port->remote.client, (int)port->remote.port, err);
}
}
if (err)
{
a2j_info("port skipped: %s", port->name);
goto fail_free_port;
}
port->inbound_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
a2j_info("port created: %s", port->name);
snd_seq_client_info_free(client_info_ptr);
return port;
fail_free_port:
list_del(&port->siblings);
a2j_port_free(port);
fail_free_client_info:
snd_seq_client_info_free(client_info_ptr);
fail:
return NULL;
}