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