weak/runtime jack linking: load libjack dynamically at runtime
[ardour.git] / libs / backends / jack / jack_portengine.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <string.h>
21 #include <stdint.h>
22
23 #include "pbd/error.h"
24
25 #include "jack_audiobackend.h"
26 #include "jack_connection.h"
27
28 #include "ardour/port_manager.h"
29
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using std::string;
35 using std::vector;
36
37 #define GET_PRIVATE_JACK_POINTER(localvar)  jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return; }
38 #define GET_PRIVATE_JACK_POINTER_RET(localvar,r) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return r; }
39
40 static uint32_t
41 ardour_port_flags_to_jack_flags (PortFlags flags)
42 {
43         uint32_t jack_flags = 0;
44         
45         if (flags & IsInput) {
46                 jack_flags |= JackPortIsInput;
47         }
48         if (flags & IsOutput) {
49                 jack_flags |= JackPortIsOutput;
50         }
51         if (flags & IsTerminal) {
52                 jack_flags |= JackPortIsTerminal;
53         }
54         if (flags & IsPhysical) {
55                 jack_flags |= JackPortIsPhysical;
56         }
57         if (flags & CanMonitor) {
58                 jack_flags |= JackPortCanMonitor;
59         }
60
61         return jack_flags;
62 }
63
64 static DataType
65 jack_port_type_to_ardour_data_type (const char* jack_type)
66 {
67         if (strcmp (jack_type, JACK_DEFAULT_AUDIO_TYPE) == 0) {
68                 return DataType::AUDIO;
69         } else if (strcmp (jack_type, JACK_DEFAULT_MIDI_TYPE) == 0) {
70                 return DataType::MIDI;
71         }
72         return DataType::NIL;
73 }
74
75 static const char*
76 ardour_data_type_to_jack_port_type (DataType d)
77 {
78         switch (d) {
79         case DataType::AUDIO:
80                 return JACK_DEFAULT_AUDIO_TYPE;
81         case DataType::MIDI:
82                 return JACK_DEFAULT_MIDI_TYPE;
83         }
84
85         return "";
86 }
87
88 void
89 JACKAudioBackend::when_connected_to_jack ()
90 {
91         /* register callbacks for stuff that is our responsibility */
92
93         jack_client_t* client = _jack_connection->jack();
94
95         if (!client) {
96                 /* how could this happen? it could ... */
97                 error << _("Already disconnected from JACK before PortEngine could register callbacks") << endmsg;
98                 return;
99         }
100
101         jack_set_port_registration_callback (client, _registration_callback, this);
102         jack_set_port_connect_callback (client, _connect_callback, this);
103         jack_set_graph_order_callback (client, _graph_order_callback, this);
104 }
105
106 int
107 JACKAudioBackend::set_port_name (PortHandle port, const std::string& name)
108 {
109         return jack_port_set_name ((jack_port_t*) port, name.c_str());
110 }
111
112 string
113 JACKAudioBackend::get_port_name (PortHandle port) const
114 {
115         return jack_port_name ((jack_port_t*) port);
116 }
117
118 PortEngine::PortHandle
119 JACKAudioBackend:: get_port_by_name (const std::string& name) const
120 {
121         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
122         return (PortHandle) jack_port_by_name (_priv_jack, name.c_str());
123 }
124
125 void
126 JACKAudioBackend::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
127 {
128         static_cast<JACKAudioBackend*> (arg)->manager.registration_callback ();
129 }
130
131 int
132 JACKAudioBackend::_graph_order_callback (void *arg)
133 {
134         return static_cast<JACKAudioBackend*> (arg)->manager.graph_order_callback ();
135 }
136
137 void
138 JACKAudioBackend::_connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn, void* arg)
139 {
140         static_cast<JACKAudioBackend*> (arg)->connect_callback (id_a, id_b, conn);
141 }
142
143 void
144 JACKAudioBackend::connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn)
145 {
146         if (manager.port_remove_in_progress()) {
147                 return;
148         }
149
150         GET_PRIVATE_JACK_POINTER (_priv_jack);
151
152         jack_port_t* a = jack_port_by_id (_priv_jack, id_a);
153         jack_port_t* b = jack_port_by_id (_priv_jack, id_b);
154
155         manager.connect_callback (jack_port_name (a), jack_port_name (b), conn == 0 ? false : true);
156 }
157
158 bool
159 JACKAudioBackend::connected (PortHandle port, bool process_callback_safe)
160 {
161         bool ret = false;
162
163         const char** ports;
164
165         if (process_callback_safe) {
166                 ports = jack_port_get_connections ((jack_port_t*)port);
167         } else {
168                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
169                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
170         }
171
172         if (ports) {
173                 ret = true;
174         }
175
176         jack_free (ports);
177
178         return ret;
179 }
180
181 bool
182 JACKAudioBackend::connected_to (PortHandle port, const std::string& other, bool process_callback_safe)
183 {
184         bool ret = false;
185         const char** ports;
186
187         if (process_callback_safe) {
188                 ports = jack_port_get_connections ((jack_port_t*)port);
189         } else {
190                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
191                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
192         }
193
194         if (ports) {
195                 for (int i = 0; ports[i]; ++i) {
196                         if (other == ports[i]) {
197                                 ret = true;
198                         }
199                 }
200                 jack_free (ports);
201         }
202
203         return ret;
204 }
205
206 bool
207 JACKAudioBackend::physically_connected (PortHandle p, bool process_callback_safe)
208 {
209         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
210         jack_port_t* port = (jack_port_t*) p;
211
212         const char** ports;
213         
214         if (process_callback_safe) {
215                 ports = jack_port_get_connections ((jack_port_t*)port);
216         } else {
217                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
218                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
219         }
220
221         if (ports) {
222                 for (int i = 0; ports[i]; ++i) {
223
224                         jack_port_t* other = jack_port_by_name (_priv_jack, ports[i]);
225
226                         if (other && (jack_port_flags (other) & JackPortIsPhysical)) {
227                                 return true;
228                         }
229                 }
230                 jack_free (ports);
231         }
232
233         return false;
234 }
235
236 int
237 JACKAudioBackend::get_connections (PortHandle port, vector<string>& s, bool process_callback_safe)
238 {
239         const char** ports;
240
241         if (process_callback_safe) {
242                 ports = jack_port_get_connections ((jack_port_t*)port);
243         } else {
244                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
245                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
246         }
247
248         if (ports) {
249                 for (int i = 0; ports[i]; ++i) {
250                         s.push_back (ports[i]);
251                 }
252                 jack_free (ports);
253         }
254
255         return s.size();
256 }
257
258 DataType
259 JACKAudioBackend::port_data_type (PortHandle p) const
260 {
261         return jack_port_type_to_ardour_data_type (jack_port_type ((jack_port_t*) p));
262 }
263
264 const string&
265 JACKAudioBackend::my_name() const
266 {
267         return _jack_connection->client_name();
268 }
269
270 bool
271 JACKAudioBackend::port_is_physical (PortHandle ph) const
272 {
273         if (!ph) {
274                 return false;
275         }
276
277         return jack_port_flags ((jack_port_t*) ph) & JackPortIsPhysical;
278 }
279
280 int
281 JACKAudioBackend::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s) const
282 {
283
284         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,0);
285
286         const char** ports =  jack_get_ports (_priv_jack, port_name_pattern.c_str(), 
287                                               ardour_data_type_to_jack_port_type (type), 
288                                               ardour_port_flags_to_jack_flags (flags));
289
290         if (ports == 0) {
291                 return 0;
292         }
293
294         for (uint32_t i = 0; ports[i]; ++i) {
295                 s.push_back (ports[i]);
296         }
297
298         jack_free (ports);
299         
300         return s.size();
301 }
302
303 ChanCount
304 JACKAudioBackend::n_physical_inputs () const
305 {
306         return n_physical (JackPortIsInput);
307 }
308
309 ChanCount
310 JACKAudioBackend::n_physical_outputs () const
311 {
312         return n_physical (JackPortIsOutput);
313 }
314
315 void
316 JACKAudioBackend::get_physical (DataType type, unsigned long flags, vector<string>& phy) const
317 {
318         GET_PRIVATE_JACK_POINTER (_priv_jack);
319         const char ** ports;
320
321         if ((ports = jack_get_ports (_priv_jack, NULL, ardour_data_type_to_jack_port_type (type), JackPortIsPhysical | flags)) == 0) {
322                 return;
323         }
324
325         if (ports) {
326                 for (uint32_t i = 0; ports[i]; ++i) {
327                         if (strstr (ports[i], "Midi-Through")) {
328                                 continue;
329                         }
330                         phy.push_back (ports[i]);
331                 }
332                 jack_free (ports);
333         }
334 }
335
336 /** Get physical ports for which JackPortIsOutput is set; ie those that correspond to
337  *  a physical input connector.
338  */
339 void
340 JACKAudioBackend::get_physical_inputs (DataType type, vector<string>& ins)
341 {
342         get_physical (type, JackPortIsOutput, ins);
343 }
344
345 /** Get physical ports for which JackPortIsInput is set; ie those that correspond to
346  *  a physical output connector.
347  */
348 void
349 JACKAudioBackend::get_physical_outputs (DataType type, vector<string>& outs)
350 {
351         get_physical (type, JackPortIsInput, outs);
352 }
353
354
355 bool
356 JACKAudioBackend::can_monitor_input () const
357 {
358         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
359         const char ** ports;
360
361         if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
362                 return false;
363         }
364
365         jack_free (ports);
366
367         return true;
368 }
369
370 int
371 JACKAudioBackend::request_input_monitoring (PortHandle port, bool yn)
372 {
373         return jack_port_request_monitor ((jack_port_t*) port, yn);
374 }
375 int
376 JACKAudioBackend::ensure_input_monitoring (PortHandle port, bool yn)
377 {
378         return jack_port_ensure_monitor ((jack_port_t*) port, yn);
379 }
380 bool
381 JACKAudioBackend::monitoring_input (PortHandle port)
382 {
383         return jack_port_monitoring_input ((jack_port_t*) port);
384 }
385
386 PortEngine::PortHandle
387 JACKAudioBackend::register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
388 {
389         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
390         return jack_port_register (_priv_jack, shortname.c_str(), 
391                                    ardour_data_type_to_jack_port_type (type),
392                                    ardour_port_flags_to_jack_flags (flags),
393                                    0);
394 }
395
396 void
397 JACKAudioBackend::unregister_port (PortHandle port)
398 {
399         GET_PRIVATE_JACK_POINTER (_priv_jack);
400         (void) jack_port_unregister (_priv_jack, (jack_port_t*) port);
401 }
402
403 int
404 JACKAudioBackend::connect (PortHandle port, const std::string& other)
405 {
406         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
407         return jack_connect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
408 }
409 int
410 JACKAudioBackend::connect (const std::string& src, const std::string& dst)
411 {
412         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
413         
414         int r = jack_connect (_priv_jack, src.c_str(), dst.c_str());
415         return r;
416 }
417
418 int
419 JACKAudioBackend::disconnect (PortHandle port, const std::string& other)
420 {
421         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
422         return jack_disconnect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
423 }
424
425 int
426 JACKAudioBackend::disconnect (const std::string& src, const std::string& dst)
427 {
428         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
429         return jack_disconnect (_priv_jack, src.c_str(), dst.c_str());
430 }
431
432 int
433 JACKAudioBackend::disconnect_all (PortHandle port)
434 {
435         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
436         return jack_port_disconnect (_priv_jack, (jack_port_t*) port);
437 }
438
439 int
440 JACKAudioBackend::midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index)
441 {
442         jack_midi_event_t ev;
443         int ret;
444
445         if ((ret = jack_midi_event_get (&ev, port_buffer, event_index)) == 0) {
446                 timestamp = ev.time;
447                 size = ev.size;
448                 *buf = ev.buffer;
449         }
450
451         return ret;
452 }
453
454 int
455 JACKAudioBackend::midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size)
456 {
457         return jack_midi_event_write (port_buffer, timestamp, buffer, size);
458 }
459
460 uint32_t
461 JACKAudioBackend::get_midi_event_count (void* port_buffer)
462 {
463         return jack_midi_get_event_count (port_buffer);
464 }
465
466 void
467 JACKAudioBackend::midi_clear (void* port_buffer)
468 {
469         jack_midi_clear_buffer (port_buffer);
470 }
471
472 void
473 JACKAudioBackend::set_latency_range (PortHandle port, bool for_playback, LatencyRange r)
474 {
475         jack_latency_range_t range;
476         
477         range.min = r.min;
478         range.max = r.max;
479
480         jack_port_set_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
481 }
482
483 LatencyRange
484 JACKAudioBackend::get_latency_range (PortHandle port, bool for_playback)
485 {
486         jack_latency_range_t range;
487         LatencyRange ret;
488         
489         jack_port_get_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
490
491         ret.min = range.min;
492         ret.max = range.max;
493
494         return ret;
495 }
496
497 void*
498 JACKAudioBackend::get_buffer (PortHandle port, pframes_t nframes)
499 {
500         return jack_port_get_buffer ((jack_port_t*) port, nframes);
501 }
502
503 uint32_t
504 JACKAudioBackend::port_name_size() const
505 {
506         return jack_port_name_size ();
507 }