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