Squashed commit of the following:
[ardour.git] / libs / ardour / port.cc
1 /*
2     Copyright (C) 2009 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 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <jack/weakjack.h> // so that we can test for new functions at runtime
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/failed_constructor.h"
29
30 #include "ardour/audioengine.h"
31 #include "ardour/debug.h"
32 #include "ardour/port.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 PBD::Signal2<void,boost::shared_ptr<Port>, boost::shared_ptr<Port> > Port::PostDisconnect;
41 PBD::Signal0<void> Port::PortDrop;
42
43 AudioEngine* Port::_engine = 0;
44 bool         Port::_connecting_blocked = false;
45 pframes_t    Port::_global_port_buffer_offset = 0;
46 pframes_t    Port::_cycle_nframes = 0;
47
48 /** @param n Port short name */
49 Port::Port (std::string const & n, DataType t, Flags f)
50         : _port_buffer_offset (0)
51         , _name (n)
52         , _flags (f)
53         , _last_monitor (false)
54 {
55         _private_playback_latency.min = 0;
56         _private_playback_latency.max = 0;
57         _private_capture_latency.min = 0;
58         _private_capture_latency.max = 0;
59
60         /* Unfortunately we have to pass the DataType into this constructor so that
61            we can create the right kind of JACK port; aside from this we'll use the
62            virtual function type () to establish type.
63         */
64
65         assert (_name.find_first_of (':') == std::string::npos);
66
67         if (!_engine->connected()) {
68                 throw failed_constructor ();
69         }
70
71         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
72                 cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
73                 throw failed_constructor ();
74         }
75
76         PortDrop.connect_same_thread (drop_connection, boost::bind (&Port::drop, this));
77 }
78
79 /** Port destructor */
80 Port::~Port ()
81 {
82         drop ();
83 }
84
85 void
86 Port::drop ()
87 {
88         if (_jack_port) {
89                 if (_engine->jack ()) {
90                         jack_port_unregister (_engine->jack (), _jack_port);
91                 }
92                 _jack_port = 0;
93         }
94 }
95
96 /** @return true if this port is connected to anything */
97 bool
98 Port::connected () const
99 {
100         return (jack_port_connected (_jack_port) != 0);
101 }
102
103 int
104 Port::disconnect_all ()
105 {
106         jack_port_disconnect (_engine->jack(), _jack_port);
107         _connections.clear ();
108
109         /* a cheaper, less hacky way to do boost::shared_from_this() ... 
110          */
111         boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
112         PostDisconnect (pself, boost::shared_ptr<Port>()); // emit signal
113
114         return 0;
115 }
116
117 /** @param o Port name
118  * @return true if this port is connected to o, otherwise false.
119  */
120 bool
121 Port::connected_to (std::string const & o) const
122 {
123         if (!_engine->connected()) {
124                 /* in some senses, this answer isn't the right one all the time,
125                    because we know about our connections and will re-establish
126                    them when we reconnect to JACK.
127                 */
128                 return false;
129         }
130
131         return jack_port_connected_to (_jack_port,
132                                        _engine->make_port_name_non_relative(o).c_str ());
133 }
134
135 /** @param o Filled in with port full names of ports that we are connected to */
136 int
137 Port::get_connections (std::vector<std::string> & c) const
138 {
139         int n = 0;
140
141         if (_engine->connected()) {
142                 const char** jc = jack_port_get_connections (_jack_port);
143                 if (jc) {
144                         for (int i = 0; jc[i]; ++i) {
145                                 c.push_back (jc[i]);
146                                 ++n;
147                         }
148
149                         if (jack_free) {
150                                 jack_free (jc);
151                         } else {
152                                 free (jc);
153                         }
154                 }
155         }
156
157         return n;
158 }
159
160 int
161 Port::connect (std::string const & other)
162 {
163         std::string const other_shrt = _engine->make_port_name_non_relative (other);
164         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
165
166         int r = 0;
167
168         if (_connecting_blocked) {
169                 return r;
170         }
171
172         if (sends_output ()) {
173                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
174         } else {
175                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
176         }
177
178         if (r == 0) {
179                 _connections.insert (other);
180         }
181
182         return r;
183 }
184
185 int
186 Port::disconnect (std::string const & other)
187 {
188         std::string const other_fullname = _engine->make_port_name_non_relative (other);
189         std::string const this_fullname = _engine->make_port_name_non_relative (_name);
190
191         int r = 0;
192
193         if (sends_output ()) {
194                 r = jack_disconnect (_engine->jack (), this_fullname.c_str (), other_fullname.c_str ());
195         } else {
196                 r = jack_disconnect (_engine->jack (), other_fullname.c_str (), this_fullname.c_str ());
197         }
198
199         if (r == 0) {
200                 _connections.erase (other);
201         }
202
203         /* a cheaper, less hacky way to do boost::shared_from_this() ... 
204          */
205         boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
206         boost::shared_ptr<Port> pother = _engine->get_port_by_name (other);
207
208         if (pself && pother) {
209                 /* Disconnecting from another Ardour port: need to allow
210                    a check on whether this may affect anything that we
211                    need to know about.
212                 */
213                 PostDisconnect (pself, pother); // emit signal 
214         }
215
216         return r;
217 }
218
219
220 bool
221 Port::connected_to (Port* o) const
222 {
223         return connected_to (o->name ());
224 }
225
226 int
227 Port::connect (Port* o)
228 {
229         return connect (o->name ());
230 }
231
232 int
233 Port::disconnect (Port* o)
234 {
235         return disconnect (o->name ());
236 }
237
238 void
239 Port::set_engine (AudioEngine* e)
240 {
241         _engine = e;
242 }
243
244 void
245 Port::ensure_jack_monitors_input (bool yn)
246 {
247         jack_port_ensure_monitor (_jack_port, yn);
248 }
249
250 bool
251 Port::jack_monitoring_input () const
252 {
253         return jack_port_monitoring_input (_jack_port);
254 }
255
256 void
257 Port::reset ()
258 {
259         _last_monitor = false;
260 }
261
262 void
263 Port::cycle_start (pframes_t)
264 {
265         _port_buffer_offset = 0;
266 }
267
268 void
269 Port::increment_port_buffer_offset (pframes_t nframes)
270 {
271         _port_buffer_offset += nframes;
272 }
273
274 void
275 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
276 {
277         /* this sets the visible latency that the rest of JACK sees. because we do latency
278            compensation, all (most) of our visible port latency values are identical.
279         */
280
281         if (!jack_port_set_latency_range) {
282                 return;
283         }
284
285         DEBUG_TRACE (DEBUG::Latency,
286                      string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
287                                      name(), range.min, range.max,
288                                      (playback ? "PLAYBACK" : "CAPTURE")));;
289
290         jack_port_set_latency_range (_jack_port,
291                                      (playback ? JackPlaybackLatency : JackCaptureLatency),
292                                      &range);
293 }
294
295 void
296 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
297 {
298         if (playback) {
299                 _private_playback_latency = range;
300                 DEBUG_TRACE (DEBUG::Latency, string_compose (
301                                      "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
302                                      name(),
303                                      _private_playback_latency.min,
304                                      _private_playback_latency.max));
305         } else {
306                 _private_capture_latency = range;
307                 DEBUG_TRACE (DEBUG::Latency, string_compose (
308                                      "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
309                                      name(),
310                                      _private_capture_latency.min,
311                                      _private_capture_latency.max));
312         }
313
314         /* push to public (JACK) location so that everyone else can see it */
315
316         set_public_latency_range (range, playback);
317 }
318
319 const jack_latency_range_t&
320 Port::private_latency_range (bool playback) const
321 {
322         if (playback) {
323                 DEBUG_TRACE (DEBUG::Latency, string_compose (
324                                      "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
325                                      name(),
326                                      _private_playback_latency.min,
327                                      _private_playback_latency.max));
328                 return _private_playback_latency;
329         } else {
330                 DEBUG_TRACE (DEBUG::Latency, string_compose (
331                                      "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
332                                      name(),
333                                      _private_playback_latency.min,
334                                      _private_playback_latency.max));
335                 return _private_capture_latency;
336         }
337 }
338
339 jack_latency_range_t
340 Port::public_latency_range (bool /*playback*/) const
341 {
342         jack_latency_range_t r;
343
344         jack_port_get_latency_range (_jack_port,
345                                      sends_output() ? JackPlaybackLatency : JackCaptureLatency,
346                                      &r);
347         DEBUG_TRACE (DEBUG::Latency, string_compose (
348                              "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
349                              name(), r.min, r.max,
350                              sends_output() ? "PLAYBACK" : "CAPTURE"));
351         return r;
352 }
353
354 void
355 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
356 {
357         if (!jack_port_get_latency_range) {
358                 return;
359         }
360
361         vector<string> connections;
362         jack_client_t* jack = _engine->jack();
363
364         if (!jack) {
365                 range.min = 0;
366                 range.max = 0;
367                 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
368                 return;
369         }
370
371         get_connections (connections);
372
373         if (!connections.empty()) {
374
375                 range.min = ~((jack_nframes_t) 0);
376                 range.max = 0;
377
378                 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
379
380                 for (vector<string>::const_iterator c = connections.begin();
381                      c != connections.end(); ++c) {
382
383                         jack_latency_range_t lr;
384
385                         if (!AudioEngine::instance()->port_is_mine (*c)) {
386
387                                 /* port belongs to some other JACK client, use
388                                  * JACK to lookup its latency information.
389                                  */
390
391                                 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
392
393                                 if (remote_port) {
394                                         jack_port_get_latency_range (
395                                                 remote_port,
396                                                 (playback ? JackPlaybackLatency : JackCaptureLatency),
397                                                 &lr);
398
399                                         DEBUG_TRACE (DEBUG::Latency, string_compose (
400                                                              "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
401                                                              name(), *c, lr.min, lr.max));
402
403                                         range.min = min (range.min, lr.min);
404                                         range.max = max (range.max, lr.max);
405                                 }
406
407                         } else {
408
409                                 /* port belongs to this instance of ardour,
410                                    so look up its latency information
411                                    internally, because our published/public
412                                    values already contain our plugin
413                                    latency compensation.
414                                 */
415
416                                 boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
417                                 if (remote_port) {
418                                         lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
419                                         DEBUG_TRACE (DEBUG::Latency, string_compose (
420                                                              "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
421                                                              name(), *c, lr.min, lr.max));
422
423                                         range.min = min (range.min, lr.min);
424                                         range.max = max (range.max, lr.max);
425                                 }
426                         }
427                 }
428
429         } else {
430                 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
431                 range.min = 0;
432                 range.max = 0;
433         }
434
435         DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
436 }
437
438 int
439 Port::reestablish ()
440 {
441         jack_client_t* jack = _engine->jack();
442
443         if (!jack) {
444                 return -1;
445         }
446
447         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
448
449         if (_jack_port == 0) {
450                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
451                 return -1;
452         }
453
454         reset ();
455
456         return 0;
457 }
458
459
460 int
461 Port::reconnect ()
462 {
463         /* caller must hold process lock; intended to be used only after reestablish() */
464
465         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
466                 if (connect (*i)) {
467                         return -1;
468                 }
469         }
470
471         return 0;
472 }
473
474 /** @param n Short port name (no JACK client name) */
475 int
476 Port::set_name (std::string const & n)
477 {
478         if (n == _name) {
479                 return 0;
480         }
481
482         int const r = jack_port_set_name (_jack_port, n.c_str());
483
484         if (r == 0) {
485                 _engine->port_renamed (_name, n);
486                 _name = n;
487         }
488
489
490         return r;
491 }
492
493 void
494 Port::request_jack_monitors_input (bool yn)
495 {
496         jack_port_request_monitor (_jack_port, yn);
497 }
498
499 bool
500 Port::physically_connected () const
501 {
502         const char** jc = jack_port_get_connections (_jack_port);
503
504         if (jc) {
505                 for (int i = 0; jc[i]; ++i) {
506
507                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
508
509                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
510                                 if (jack_free) {
511                                         jack_free (jc);
512                                 } else {
513                                         free (jc);
514                                 }
515                                 return true;
516                         }
517                 }
518                 if (jack_free) {
519                         jack_free (jc);
520                 } else {
521                         free (jc);
522                 }
523         }
524
525         return false;
526 }
527