fixes for latency computation and compilation
[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 pframes_t Port::_buffer_size = 0;
44 bool Port::_connecting_blocked = false;
45
46 /** @param n Port short name */
47 Port::Port (std::string const & n, DataType t, Flags f)
48         : _last_monitor (false)
49         , _name (n)
50         , _flags (f)
51 {
52
53         /* Unfortunately we have to pass the DataType into this constructor so that we can
54            create the right kind of JACK port; aside from this we'll use the virtual function type ()
55            to establish type.
56         */
57
58         assert (_name.find_first_of (':') == std::string::npos);
59
60         if (!_engine->connected()) {
61                 throw failed_constructor ();
62         }
63
64         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
65                 cerr << "Failed to register JACK port, reason is unknown from here\n";
66                 throw failed_constructor ();
67         }
68 }
69
70 /** Port destructor */
71 Port::~Port ()
72 {
73         if (_engine->jack ()) {
74                 jack_port_unregister (_engine->jack (), _jack_port);
75         }
76 }
77
78 /** @return true if this port is connected to anything */
79 bool
80 Port::connected () const
81 {
82         return (jack_port_connected (_jack_port) != 0);
83 }
84
85 int
86 Port::disconnect_all ()
87 {
88         jack_port_disconnect (_engine->jack(), _jack_port);
89         _connections.clear ();
90
91         return 0;
92 }
93
94 /** @param o Port name
95  * @return true if this port is connected to o, otherwise false.
96  */
97 bool
98 Port::connected_to (std::string const & o) const
99 {
100         if (!_engine->connected()) {
101                 /* in some senses, this answer isn't the right one all the time, 
102                    because we know about our connections and will re-establish
103                    them when we reconnect to JACK.
104                 */
105                 return false;
106         }
107
108         return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
109 }
110
111 /** @param o Filled in with port full names of ports that we are connected to */
112 int
113 Port::get_connections (std::vector<std::string> & c) const
114 {
115         int n = 0;
116
117         if (_engine->connected()) {
118                 const char** jc = jack_port_get_connections (_jack_port);
119                 if (jc) {
120                         for (int i = 0; jc[i]; ++i) {
121                                 c.push_back (jc[i]);
122                                 ++n;
123                         }
124                         
125                         jack_free (jc);
126                 }
127         }
128
129         return n;
130 }
131
132 int
133 Port::connect (std::string const & other)
134 {
135         std::string const other_shrt = _engine->make_port_name_non_relative (other);
136         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
137
138         int r = 0;
139
140         if (_connecting_blocked) {
141                 return r;
142         }
143
144         if (sends_output ()) {
145                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
146         } else {
147                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
148         }
149
150         if (r == 0) {
151                 _connections.insert (other);
152         }
153
154         return r;
155 }
156
157 int
158 Port::disconnect (std::string const & other)
159 {
160         std::string const other_shrt = _engine->make_port_name_non_relative (other);
161         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
162
163         int r = 0;
164
165         if (sends_output ()) {
166                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
167         } else {
168                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
169         }
170
171         if (r == 0) {
172                 _connections.erase (other);
173         }
174
175         return r;
176 }
177
178
179 bool
180 Port::connected_to (Port* o) const
181 {
182         return connected_to (o->name ());
183 }
184
185 int
186 Port::connect (Port* o)
187 {
188         return connect (o->name ());
189 }
190
191 int
192 Port::disconnect (Port* o)
193 {
194         return disconnect (o->name ());
195 }
196
197 void
198 Port::set_engine (AudioEngine* e)
199 {
200         _engine = e;
201 }
202
203 void
204 Port::ensure_monitor_input (bool yn)
205 {
206         jack_port_ensure_monitor (_jack_port, yn);
207 }
208
209 bool
210 Port::monitoring_input () const
211 {
212         return jack_port_monitoring_input (_jack_port);
213 }
214
215 void
216 Port::reset ()
217 {
218         _last_monitor = false;
219
220         // XXX
221         // _metering = 0;
222         // reset_meters ();
223 }
224
225 void
226 Port::recompute_total_latency () const
227 {
228 #ifndef HAVE_JACK_NEW_LATENCY
229 #ifdef  HAVE_JACK_RECOMPUTE_LATENCY
230         jack_client_t* jack = _engine->jack();
231
232         if (!jack) {
233                 return;
234         }
235
236         jack_recompute_total_latency (jack, _jack_port);
237 #endif
238 #endif
239 }
240
241 void
242 Port::set_latency_range (jack_latency_range_t& range, bool playback) const
243 {
244 #ifdef HAVE_JACK_NEW_LATENCY
245         if (!jack_port_set_latency_range) {
246                 return;
247         }
248
249         jack_port_set_latency_range (_jack_port, (playback ? JackPlaybackLatency : JackCaptureLatency), &range);
250 #endif
251 }
252
253 void
254 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
255 {
256 #ifdef HAVE_JACK_NEW_LATENCY
257         if (!jack_port_get_latency_range) {
258                 return;
259         }
260
261         vector<string> connections;
262         jack_client_t* jack = _engine->jack();
263         
264         if (!jack) {
265                 range.min = 0;
266                 range.max = 0;
267                 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
268                 return;
269         }
270
271         get_connections (connections);
272
273         if (!connections.empty()) {
274                 
275                 range.min = ~((jack_nframes_t) 0);
276                 range.max = 0;
277
278                 for (vector<string>::iterator c = connections.begin(); c != connections.end(); ++c) {
279                         jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
280                         jack_latency_range_t lr;
281
282                         DEBUG_TRACE (DEBUG::Latency, string_compose ("\t%1 connected to %2\n", name(), *c));
283
284                         if (remote_port) {
285                                 jack_port_get_latency_range (remote_port, (playback ? JackPlaybackLatency : JackCaptureLatency), &lr);
286                                 DEBUG_TRACE (DEBUG::Latency, string_compose ("\t\tremote has latency range %1 .. %2\n", lr.min, lr.max));
287                                 range.min = min (range.min, lr.min);
288                                 range.max = max (range.max, lr.max);
289                         }
290                 }
291
292         } else {
293
294                 range.min = 0;
295                 range.max = 0;
296         }
297 #endif /* HAVE_JACK_NEW_LATENCY */
298 }
299
300 framecnt_t
301 Port::total_latency () const
302 {
303 #ifndef HAVE_JACK_NEW_LATENCY
304         jack_client_t* jack = _engine->jack();
305
306         if (!jack) {
307                 return 0;
308         }
309
310         return jack_port_get_total_latency (jack, _jack_port);
311 #else
312         return 0;
313 #endif
314 }
315
316 int
317 Port::reestablish ()
318 {
319         jack_client_t* jack = _engine->jack();
320
321         if (!jack) {
322                 return -1;
323         }
324
325         cerr << "RE-REGISTER: " << _name.c_str() << endl;
326         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
327
328         if (_jack_port == 0) {
329                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
330                 return -1;
331         }
332
333         reset ();
334
335         return 0;
336 }
337
338
339 int
340 Port::reconnect ()
341 {
342         /* caller must hold process lock; intended to be used only after reestablish() */
343
344         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
345                 if (connect (*i)) {
346                         return -1;
347                 }
348         }
349
350         return 0;
351 }
352
353 /** @param n Short port name (no JACK client name) */
354 int
355 Port::set_name (std::string const & n)
356 {
357         if (n == _name) {
358                 return 0;
359         }
360
361         int const r = jack_port_set_name (_jack_port, n.c_str());
362
363         if (r == 0) {
364                 _name = n;
365         }
366
367         return r;
368 }
369
370 void
371 Port::request_monitor_input (bool yn)
372 {
373         jack_port_request_monitor (_jack_port, yn);
374 }
375
376 void
377 Port::set_latency (framecnt_t n)
378 {
379 #ifndef HAVE_JACK_NEW_LATENCY
380         jack_port_set_latency (_jack_port, n);
381 #endif
382 }
383
384 bool
385 Port::physically_connected () const
386 {
387         const char** jc = jack_port_get_connections (_jack_port);
388
389         if (jc) {
390                 for (int i = 0; jc[i]; ++i) {
391
392                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
393                         
394                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
395                                 jack_free (jc);
396                                 return true;
397                         }
398                 }
399                 
400                 jack_free (jc);
401         }
402
403         return false;
404 }
405