Fix potential crash at exit/close.
[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 "pbd/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 #if HAVE_JACK_PORT_RENAME
110         jack_client_t* client = _jack_connection->jack();
111         if (client) {
112                 return jack_port_rename (client, (jack_port_t*) port, name.c_str());
113         } else {
114                 return -1;
115         }
116 #else
117         return jack_port_set_name ((jack_port_t*) port, name.c_str());
118 #endif
119 }
120
121 string
122 JACKAudioBackend::get_port_name (PortHandle port) const
123 {
124         return jack_port_name ((jack_port_t*) port);
125 }
126
127 int
128 JACKAudioBackend::get_port_property (PortHandle port, const std::string& key, std::string& value, std::string& type) const
129 {
130 #ifdef HAVE_JACK_METADATA // really everyone ought to have this by now.
131         int rv = -1;
132         char *cvalue = NULL;
133         char *ctype = NULL;
134
135         jack_uuid_t uuid = jack_port_uuid((jack_port_t*) port);
136         rv = jack_get_property(uuid, key.c_str(), &cvalue, &ctype);
137
138         if (0 == rv && cvalue) {
139                 value = cvalue;
140                 if (ctype) {
141                         type = ctype;
142                 }
143         } else {
144                 rv = -1;
145         }
146
147         jack_free(cvalue);
148         jack_free(ctype);
149         return rv;
150 #else
151         return -1;
152 #endif
153 }
154
155 int
156 JACKAudioBackend::set_port_property (PortHandle port, const std::string& key, const std::string& value, const std::string& type)
157 {
158 #ifdef HAVE_JACK_METADATA // really everyone ought to have this by now.
159         int rv = -1;
160         jack_client_t* client = _jack_connection->jack();
161         jack_uuid_t uuid = jack_port_uuid((jack_port_t*) port);
162         return jack_set_property(client, uuid, key.c_str(), value.c_str(), type.c_str());
163         return rv;
164 #else
165         return -1;
166 #endif
167 }
168
169 PortEngine::PortHandle
170 JACKAudioBackend:: get_port_by_name (const std::string& name) const
171 {
172         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
173         return (PortHandle) jack_port_by_name (_priv_jack, name.c_str());
174 }
175
176 void
177 JACKAudioBackend::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
178 {
179         static_cast<JACKAudioBackend*> (arg)->manager.registration_callback ();
180 }
181
182 int
183 JACKAudioBackend::_graph_order_callback (void *arg)
184 {
185         return static_cast<JACKAudioBackend*> (arg)->manager.graph_order_callback ();
186 }
187
188 void
189 JACKAudioBackend::_connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn, void* arg)
190 {
191         static_cast<JACKAudioBackend*> (arg)->connect_callback (id_a, id_b, conn);
192 }
193
194 void
195 JACKAudioBackend::connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn)
196 {
197         if (manager.port_remove_in_progress()) {
198                 return;
199         }
200
201         GET_PRIVATE_JACK_POINTER (_priv_jack);
202
203         jack_port_t* a = jack_port_by_id (_priv_jack, id_a);
204         jack_port_t* b = jack_port_by_id (_priv_jack, id_b);
205
206         manager.connect_callback (jack_port_name (a), jack_port_name (b), conn == 0 ? false : true);
207 }
208
209 bool
210 JACKAudioBackend::connected (PortHandle port, bool process_callback_safe)
211 {
212         bool ret = false;
213
214         const char** ports;
215
216         if (process_callback_safe) {
217                 ports = jack_port_get_connections ((jack_port_t*)port);
218         } else {
219                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
220                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
221         }
222
223         if (ports) {
224                 ret = true;
225         }
226
227         jack_free (ports);
228
229         return ret;
230 }
231
232 bool
233 JACKAudioBackend::connected_to (PortHandle port, const std::string& other, bool process_callback_safe)
234 {
235         bool ret = false;
236         const char** ports;
237
238         if (process_callback_safe) {
239                 ports = jack_port_get_connections ((jack_port_t*)port);
240         } else {
241                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
242                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
243         }
244
245         if (ports) {
246                 for (int i = 0; ports[i]; ++i) {
247                         if (other == ports[i]) {
248                                 ret = true;
249                         }
250                 }
251                 jack_free (ports);
252         }
253
254         return ret;
255 }
256
257 bool
258 JACKAudioBackend::physically_connected (PortHandle p, bool process_callback_safe)
259 {
260         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
261         jack_port_t* port = (jack_port_t*) p;
262
263         const char** ports;
264
265         if (process_callback_safe) {
266                 ports = jack_port_get_connections ((jack_port_t*)port);
267         } else {
268                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
269                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
270         }
271
272         if (ports) {
273                 for (int i = 0; ports[i]; ++i) {
274
275                         jack_port_t* other = jack_port_by_name (_priv_jack, ports[i]);
276
277                         if (other && (jack_port_flags (other) & JackPortIsPhysical)) {
278                                 return true;
279                         }
280                 }
281                 jack_free (ports);
282         }
283
284         return false;
285 }
286
287 int
288 JACKAudioBackend::get_connections (PortHandle port, vector<string>& s, bool process_callback_safe)
289 {
290         const char** ports;
291
292         if (process_callback_safe) {
293                 ports = jack_port_get_connections ((jack_port_t*)port);
294         } else {
295                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
296                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
297         }
298
299         if (ports) {
300                 for (int i = 0; ports[i]; ++i) {
301                         s.push_back (ports[i]);
302                 }
303                 jack_free (ports);
304         }
305
306         return s.size();
307 }
308
309 DataType
310 JACKAudioBackend::port_data_type (PortHandle p) const
311 {
312         return jack_port_type_to_ardour_data_type (jack_port_type ((jack_port_t*) p));
313 }
314
315 const string&
316 JACKAudioBackend::my_name() const
317 {
318         return _jack_connection->client_name();
319 }
320
321 bool
322 JACKAudioBackend::port_is_physical (PortHandle ph) const
323 {
324         if (!ph) {
325                 return false;
326         }
327
328         return jack_port_flags ((jack_port_t*) ph) & JackPortIsPhysical;
329 }
330
331 int
332 JACKAudioBackend::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s) const
333 {
334
335         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,0);
336
337         const char** ports =  jack_get_ports (_priv_jack, port_name_pattern.c_str(),
338                                               ardour_data_type_to_jack_port_type (type),
339                                               ardour_port_flags_to_jack_flags (flags));
340
341         if (ports == 0) {
342                 return 0;
343         }
344
345         for (uint32_t i = 0; ports[i]; ++i) {
346                 s.push_back (ports[i]);
347         }
348
349         jack_free (ports);
350
351         return s.size();
352 }
353
354 ChanCount
355 JACKAudioBackend::n_physical_inputs () const
356 {
357         return n_physical (JackPortIsInput);
358 }
359
360 ChanCount
361 JACKAudioBackend::n_physical_outputs () const
362 {
363         return n_physical (JackPortIsOutput);
364 }
365
366 void
367 JACKAudioBackend::get_physical (DataType type, unsigned long flags, vector<string>& phy) const
368 {
369         GET_PRIVATE_JACK_POINTER (_priv_jack);
370         const char ** ports;
371
372         if ((ports = jack_get_ports (_priv_jack, NULL, ardour_data_type_to_jack_port_type (type), JackPortIsPhysical | flags)) == 0) {
373                 return;
374         }
375
376         if (ports) {
377                 for (uint32_t i = 0; ports[i]; ++i) {
378                         if (strstr (ports[i], "Midi-Through")) {
379                                 continue;
380                         }
381                         phy.push_back (ports[i]);
382                 }
383                 jack_free (ports);
384         }
385 }
386
387 /** Get physical ports for which JackPortIsOutput is set; ie those that correspond to
388  *  a physical input connector.
389  */
390 void
391 JACKAudioBackend::get_physical_inputs (DataType type, vector<string>& ins)
392 {
393         get_physical (type, JackPortIsOutput, ins);
394 }
395
396 /** Get physical ports for which JackPortIsInput is set; ie those that correspond to
397  *  a physical output connector.
398  */
399 void
400 JACKAudioBackend::get_physical_outputs (DataType type, vector<string>& outs)
401 {
402         get_physical (type, JackPortIsInput, outs);
403 }
404
405
406 bool
407 JACKAudioBackend::can_monitor_input () const
408 {
409         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
410         const char ** ports;
411
412         if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
413                 return false;
414         }
415
416         jack_free (ports);
417
418         return true;
419 }
420
421 int
422 JACKAudioBackend::request_input_monitoring (PortHandle port, bool yn)
423 {
424         return jack_port_request_monitor ((jack_port_t*) port, yn);
425 }
426 int
427 JACKAudioBackend::ensure_input_monitoring (PortHandle port, bool yn)
428 {
429         return jack_port_ensure_monitor ((jack_port_t*) port, yn);
430 }
431 bool
432 JACKAudioBackend::monitoring_input (PortHandle port)
433 {
434         return jack_port_monitoring_input ((jack_port_t*) port);
435 }
436
437 PortEngine::PortHandle
438 JACKAudioBackend::register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
439 {
440         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
441         return jack_port_register (_priv_jack, shortname.c_str(),
442                                    ardour_data_type_to_jack_port_type (type),
443                                    ardour_port_flags_to_jack_flags (flags),
444                                    0);
445 }
446
447 void
448 JACKAudioBackend::unregister_port (PortHandle port)
449 {
450         GET_PRIVATE_JACK_POINTER (_priv_jack);
451         (void) jack_port_unregister (_priv_jack, (jack_port_t*) port);
452 }
453
454 int
455 JACKAudioBackend::connect (PortHandle port, const std::string& other)
456 {
457         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
458         return jack_connect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
459 }
460 int
461 JACKAudioBackend::connect (const std::string& src, const std::string& dst)
462 {
463         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
464
465         int r = jack_connect (_priv_jack, src.c_str(), dst.c_str());
466         return r;
467 }
468
469 int
470 JACKAudioBackend::disconnect (PortHandle port, const std::string& other)
471 {
472         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
473         return jack_disconnect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
474 }
475
476 int
477 JACKAudioBackend::disconnect (const std::string& src, const std::string& dst)
478 {
479         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
480         return jack_disconnect (_priv_jack, src.c_str(), dst.c_str());
481 }
482
483 int
484 JACKAudioBackend::disconnect_all (PortHandle port)
485 {
486         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
487         return jack_port_disconnect (_priv_jack, (jack_port_t*) port);
488 }
489
490 int
491 JACKAudioBackend::midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index)
492 {
493         jack_midi_event_t ev;
494         int ret;
495
496         if ((ret = jack_midi_event_get (&ev, port_buffer, event_index)) == 0) {
497                 timestamp = ev.time;
498                 size = ev.size;
499                 *buf = ev.buffer;
500         }
501
502         return ret;
503 }
504
505 int
506 JACKAudioBackend::midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size)
507 {
508         return jack_midi_event_write (port_buffer, timestamp, buffer, size);
509 }
510
511 uint32_t
512 JACKAudioBackend::get_midi_event_count (void* port_buffer)
513 {
514         return jack_midi_get_event_count (port_buffer);
515 }
516
517 void
518 JACKAudioBackend::midi_clear (void* port_buffer)
519 {
520         jack_midi_clear_buffer (port_buffer);
521 }
522
523 void
524 JACKAudioBackend::set_latency_range (PortHandle port, bool for_playback, LatencyRange r)
525 {
526         jack_latency_range_t range;
527
528         range.min = r.min;
529         range.max = r.max;
530
531         jack_port_set_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
532 }
533
534 LatencyRange
535 JACKAudioBackend::get_latency_range (PortHandle port, bool for_playback)
536 {
537         jack_latency_range_t range;
538         LatencyRange ret;
539
540         jack_port_get_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
541
542         ret.min = range.min;
543         ret.max = range.max;
544
545         return ret;
546 }
547
548 void*
549 JACKAudioBackend::get_buffer (PortHandle port, pframes_t nframes)
550 {
551         return jack_port_get_buffer ((jack_port_t*) port, nframes);
552 }
553
554 uint32_t
555 JACKAudioBackend::port_name_size() const
556 {
557         return jack_port_name_size ();
558 }