fix all manner of things relating to io connections, setting capture alignment, and...
[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 <stdexcept>
25
26 #include <jack/weakjack.h> // so that we can test for new functions at runtime
27
28 #include "pbd/error.h"
29 #include "pbd/compose.h"
30
31 #include "ardour/debug.h"
32 #include "ardour/port.h"
33 #include "ardour/audioengine.h"
34 #include "pbd/failed_constructor.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace PBD;
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, 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         return 0;
98 }
99
100 /** @param o Port name
101  * @return true if this port is connected to o, otherwise false.
102  */
103 bool
104 Port::connected_to (std::string const & o) const
105 {
106         if (!_engine->connected()) {
107                 /* in some senses, this answer isn't the right one all the time, 
108                    because we know about our connections and will re-establish
109                    them when we reconnect to JACK.
110                 */
111                 return false;
112         }
113
114         return jack_port_connected_to (_jack_port,
115                                        _engine->make_port_name_non_relative(o).c_str ());
116 }
117
118 /** @param o Filled in with port full names of ports that we are connected to */
119 int
120 Port::get_connections (std::vector<std::string> & c) const
121 {
122         int n = 0;
123
124         if (_engine->connected()) {
125                 const char** jc = jack_port_get_connections (_jack_port);
126                 if (jc) {
127                         for (int i = 0; jc[i]; ++i) {
128                                 c.push_back (jc[i]);
129                                 ++n;
130                         }
131                         
132                         jack_free (jc);
133                 }
134         }
135
136         return n;
137 }
138
139 int
140 Port::connect (std::string const & other)
141 {
142         std::string const other_shrt = _engine->make_port_name_non_relative (other);
143         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
144
145         int r = 0;
146
147         if (_connecting_blocked) {
148                 return r;
149         }
150
151         if (sends_output ()) {
152                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
153         } else {
154                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
155         }
156
157         if (r == 0) {
158                 _connections.insert (other);
159         }
160
161         return r;
162 }
163
164 int
165 Port::disconnect (std::string const & other)
166 {
167         std::string const other_shrt = _engine->make_port_name_non_relative (other);
168         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
169
170         int r = 0;
171
172         if (sends_output ()) {
173                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
174         } else {
175                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
176         }
177
178         if (r == 0) {
179                 _connections.erase (other);
180         }
181
182         return r;
183 }
184
185
186 bool
187 Port::connected_to (Port* o) const
188 {
189         return connected_to (o->name ());
190 }
191
192 int
193 Port::connect (Port* o)
194 {
195         return connect (o->name ());
196 }
197
198 int
199 Port::disconnect (Port* o)
200 {
201         return disconnect (o->name ());
202 }
203
204 void
205 Port::set_engine (AudioEngine* e)
206 {
207         _engine = e;
208 }
209
210 void
211 Port::ensure_monitor_input (bool yn)
212 {
213         jack_port_ensure_monitor (_jack_port, yn);
214 }
215
216 bool
217 Port::monitoring_input () const
218 {
219         return jack_port_monitoring_input (_jack_port);
220 }
221
222 void
223 Port::reset ()
224 {
225         _last_monitor = false;
226 }
227
228 void
229 Port::cycle_start (pframes_t nframes)
230 {
231         _port_buffer_offset = 0;
232 }
233
234 void
235 Port::increment_port_buffer_offset (pframes_t nframes)
236 {
237         _port_buffer_offset += nframes;
238 }
239         
240 void
241 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
242 {
243         /* this sets the visible latency that the rest of JACK sees. because we do latency
244            compensation, all (most) of our visible port latency values are identical.
245         */
246
247         if (!jack_port_set_latency_range) {
248                 return;
249         }
250         
251         DEBUG_TRACE (DEBUG::Latency,
252                      string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
253                                      name(), range.min, range.max,
254                                      (playback ? "PLAYBACK" : "CAPTURE")));;
255
256         jack_port_set_latency_range (_jack_port,
257                                      (playback ? JackPlaybackLatency : JackCaptureLatency),
258                                      &range);
259 }
260
261 void
262 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
263 {
264         if (playback) {
265                 _private_playback_latency = range;
266                 DEBUG_TRACE (DEBUG::Latency, string_compose (
267                                      "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
268                                      name(),
269                                      _private_playback_latency.min, 
270                                      _private_playback_latency.max));
271         } else {
272                 _private_capture_latency = range;
273                 DEBUG_TRACE (DEBUG::Latency, string_compose (
274                                      "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
275                                      name(), 
276                                      _private_capture_latency.min, 
277                                      _private_capture_latency.max));
278         }
279
280         /* push to public (JACK) location so that everyone else can see it */
281
282         set_public_latency_range (range, playback);
283 }
284
285 const jack_latency_range_t&
286 Port::private_latency_range (bool playback) const
287 {
288         if (playback) {
289                 DEBUG_TRACE (DEBUG::Latency, string_compose (
290                                      "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
291                                      name(), 
292                                      _private_playback_latency.min, 
293                                      _private_playback_latency.max)); 
294                 return _private_playback_latency;
295         } else {
296                 DEBUG_TRACE (DEBUG::Latency, string_compose (
297                                      "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
298                                      name(), 
299                                      _private_playback_latency.min, 
300                                      _private_playback_latency.max));
301                 return _private_capture_latency;
302         }
303 }
304
305 jack_latency_range_t
306 Port::public_latency_range (bool playback) const
307 {
308         jack_latency_range_t r;
309
310         jack_port_get_latency_range (_jack_port, 
311                                      sends_output() ? JackPlaybackLatency : JackCaptureLatency,
312                                      &r);
313         DEBUG_TRACE (DEBUG::Latency, string_compose (
314                              "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n", 
315                              name(), r.min, r.max,
316                              sends_output() ? "PLAYBACK" : "CAPTURE"));
317         return r;
318 }
319
320 void
321 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
322 {
323         if (!jack_port_get_latency_range) {
324                 return;
325         }
326
327         vector<string> connections;
328         jack_client_t* jack = _engine->jack();
329         
330         if (!jack) {
331                 range.min = 0;
332                 range.max = 0;
333                 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
334                 return;
335         }
336
337         get_connections (connections);
338
339         if (!connections.empty()) {
340                 
341                 range.min = ~((jack_nframes_t) 0);
342                 range.max = 0;
343
344                 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
345
346                 for (vector<string>::const_iterator c = connections.begin();
347                      c != connections.end(); ++c) {
348
349                         jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
350                         jack_latency_range_t lr;
351
352                         if (remote_port) {
353                                 jack_port_get_latency_range (
354                                         remote_port,
355                                         (playback ? JackPlaybackLatency : JackCaptureLatency),
356                                         &lr);
357                                 DEBUG_TRACE (DEBUG::Latency, string_compose (
358                                                      "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
359                                                      name(), *c, lr.min, lr.max));
360                                 range.min = min (range.min, lr.min);
361                                 range.max = max (range.max, lr.max);
362                         }
363                 }
364
365         } else {
366                 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
367                 range.min = 0;
368                 range.max = 0;
369         }
370
371         DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
372 }
373
374 int
375 Port::reestablish ()
376 {
377         jack_client_t* jack = _engine->jack();
378
379         if (!jack) {
380                 return -1;
381         }
382
383         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
384
385         if (_jack_port == 0) {
386                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
387                 return -1;
388         }
389
390         reset ();
391
392         return 0;
393 }
394
395
396 int
397 Port::reconnect ()
398 {
399         /* caller must hold process lock; intended to be used only after reestablish() */
400
401         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
402                 if (connect (*i)) {
403                         return -1;
404                 }
405         }
406
407         return 0;
408 }
409
410 /** @param n Short port name (no JACK client name) */
411 int
412 Port::set_name (std::string const & n)
413 {
414         if (n == _name) {
415                 return 0;
416         }
417
418         int const r = jack_port_set_name (_jack_port, n.c_str());
419
420         if (r == 0) {
421                 _name = n;
422         }
423
424         return r;
425 }
426
427 void
428 Port::request_monitor_input (bool yn)
429 {
430         jack_port_request_monitor (_jack_port, yn);
431 }
432
433 bool
434 Port::physically_connected () const
435 {
436         const char** jc = jack_port_get_connections (_jack_port);
437
438         if (jc) {
439                 for (int i = 0; jc[i]; ++i) {
440
441                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
442                         
443                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
444                                 jack_free (jc);
445                                 return true;
446                         }
447                 }
448                 
449                 jack_free (jc);
450         }
451
452         return false;
453 }
454