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