fixes for 98% of all the warnings/errors reported by OS X gcc on tiger
[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                         if (jack_free) {
133                                 jack_free (jc);
134                         } else {
135                                 free (jc);
136                         }
137                 }
138         }
139
140         return n;
141 }
142
143 int
144 Port::connect (std::string const & other)
145 {
146         std::string const other_shrt = _engine->make_port_name_non_relative (other);
147         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
148
149         int r = 0;
150
151         if (_connecting_blocked) {
152                 return r;
153         }
154
155         if (sends_output ()) {
156                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
157         } else {
158                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
159         }
160
161         if (r == 0) {
162                 _connections.insert (other);
163         }
164
165         return r;
166 }
167
168 int
169 Port::disconnect (std::string const & other)
170 {
171         std::string const other_shrt = _engine->make_port_name_non_relative (other);
172         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
173
174         int r = 0;
175
176         if (sends_output ()) {
177                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
178         } else {
179                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
180         }
181
182         if (r == 0) {
183                 _connections.erase (other);
184         }
185
186         return r;
187 }
188
189
190 bool
191 Port::connected_to (Port* o) const
192 {
193         return connected_to (o->name ());
194 }
195
196 int
197 Port::connect (Port* o)
198 {
199         return connect (o->name ());
200 }
201
202 int
203 Port::disconnect (Port* o)
204 {
205         return disconnect (o->name ());
206 }
207
208 void
209 Port::set_engine (AudioEngine* e)
210 {
211         _engine = e;
212 }
213
214 void
215 Port::ensure_monitor_input (bool yn)
216 {
217         jack_port_ensure_monitor (_jack_port, yn);
218 }
219
220 bool
221 Port::monitoring_input () const
222 {
223         return jack_port_monitoring_input (_jack_port);
224 }
225
226 void
227 Port::reset ()
228 {
229         _last_monitor = false;
230 }
231
232 void
233 Port::cycle_start (pframes_t)
234 {
235         _port_buffer_offset = 0;
236 }
237
238 void
239 Port::increment_port_buffer_offset (pframes_t nframes)
240 {
241         _port_buffer_offset += nframes;
242 }
243
244 void
245 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
246 {
247         /* this sets the visible latency that the rest of JACK sees. because we do latency
248            compensation, all (most) of our visible port latency values are identical.
249         */
250
251         if (!jack_port_set_latency_range) {
252                 return;
253         }
254
255         DEBUG_TRACE (DEBUG::Latency,
256                      string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
257                                      name(), range.min, range.max,
258                                      (playback ? "PLAYBACK" : "CAPTURE")));;
259
260         jack_port_set_latency_range (_jack_port,
261                                      (playback ? JackPlaybackLatency : JackCaptureLatency),
262                                      &range);
263 }
264
265 void
266 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
267 {
268         if (playback) {
269                 _private_playback_latency = range;
270                 DEBUG_TRACE (DEBUG::Latency, string_compose (
271                                      "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
272                                      name(),
273                                      _private_playback_latency.min,
274                                      _private_playback_latency.max));
275         } else {
276                 _private_capture_latency = range;
277                 DEBUG_TRACE (DEBUG::Latency, string_compose (
278                                      "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
279                                      name(),
280                                      _private_capture_latency.min,
281                                      _private_capture_latency.max));
282         }
283
284         /* push to public (JACK) location so that everyone else can see it */
285
286         set_public_latency_range (range, playback);
287 }
288
289 const jack_latency_range_t&
290 Port::private_latency_range (bool playback) const
291 {
292         if (playback) {
293                 DEBUG_TRACE (DEBUG::Latency, string_compose (
294                                      "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
295                                      name(),
296                                      _private_playback_latency.min,
297                                      _private_playback_latency.max));
298                 return _private_playback_latency;
299         } else {
300                 DEBUG_TRACE (DEBUG::Latency, string_compose (
301                                      "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
302                                      name(),
303                                      _private_playback_latency.min,
304                                      _private_playback_latency.max));
305                 return _private_capture_latency;
306         }
307 }
308
309 jack_latency_range_t
310 Port::public_latency_range (bool /*playback*/) const
311 {
312         jack_latency_range_t r;
313
314         jack_port_get_latency_range (_jack_port,
315                                      sends_output() ? JackPlaybackLatency : JackCaptureLatency,
316                                      &r);
317         DEBUG_TRACE (DEBUG::Latency, string_compose (
318                              "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
319                              name(), r.min, r.max,
320                              sends_output() ? "PLAYBACK" : "CAPTURE"));
321         return r;
322 }
323
324 void
325 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
326 {
327         if (!jack_port_get_latency_range) {
328                 return;
329         }
330
331         vector<string> connections;
332         jack_client_t* jack = _engine->jack();
333
334         if (!jack) {
335                 range.min = 0;
336                 range.max = 0;
337                 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
338                 return;
339         }
340
341         get_connections (connections);
342
343         if (!connections.empty()) {
344
345                 range.min = ~((jack_nframes_t) 0);
346                 range.max = 0;
347
348                 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
349
350                 for (vector<string>::const_iterator c = connections.begin();
351                      c != connections.end(); ++c) {
352
353                         jack_latency_range_t lr;
354
355                         if (!AudioEngine::instance()->port_is_mine (*c)) {
356
357                                 /* port belongs to some other JACK client, use
358                                  * JACK to lookup its latency information.
359                                  */
360
361                                 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
362
363                                 if (remote_port) {
364                                         jack_port_get_latency_range (
365                                                 remote_port,
366                                                 (playback ? JackPlaybackLatency : JackCaptureLatency),
367                                                 &lr);
368
369                                         DEBUG_TRACE (DEBUG::Latency, string_compose (
370                                                              "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
371                                                              name(), *c, lr.min, lr.max));
372
373                                         range.min = min (range.min, lr.min);
374                                         range.max = max (range.max, lr.max);
375                                 }
376
377                         } else {
378
379                                 /* port belongs to this instance of ardour,
380                                    so look up its latency information
381                                    internally, because our published/public
382                                    values already contain our plugin
383                                    latency compensation.
384                                 */
385
386                                 Port* remote_port = AudioEngine::instance()->get_port_by_name (*c);
387                                 if (remote_port) {
388                                         lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
389                                         DEBUG_TRACE (DEBUG::Latency, string_compose (
390                                                              "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
391                                                              name(), *c, lr.min, lr.max));
392
393                                         range.min = min (range.min, lr.min);
394                                         range.max = max (range.max, lr.max);
395                                 }
396                         }
397                 }
398
399         } else {
400                 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
401                 range.min = 0;
402                 range.max = 0;
403         }
404
405         DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
406 }
407
408 int
409 Port::reestablish ()
410 {
411         jack_client_t* jack = _engine->jack();
412
413         if (!jack) {
414                 return -1;
415         }
416
417         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
418
419         if (_jack_port == 0) {
420                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
421                 return -1;
422         }
423
424         reset ();
425
426         return 0;
427 }
428
429
430 int
431 Port::reconnect ()
432 {
433         /* caller must hold process lock; intended to be used only after reestablish() */
434
435         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
436                 if (connect (*i)) {
437                         return -1;
438                 }
439         }
440
441         return 0;
442 }
443
444 /** @param n Short port name (no JACK client name) */
445 int
446 Port::set_name (std::string const & n)
447 {
448         if (n == _name) {
449                 return 0;
450         }
451
452         int const r = jack_port_set_name (_jack_port, n.c_str());
453
454         if (r == 0) {
455                 _name = n;
456         }
457
458         return r;
459 }
460
461 void
462 Port::request_monitor_input (bool yn)
463 {
464         jack_port_request_monitor (_jack_port, yn);
465 }
466
467 bool
468 Port::physically_connected () const
469 {
470         const char** jc = jack_port_get_connections (_jack_port);
471
472         if (jc) {
473                 for (int i = 0; jc[i]; ++i) {
474
475                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
476
477                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
478                                 if (jack_free) {
479                                         jack_free (jc);
480                                 } else {
481                                         free (jc);
482                                 }
483                                 return true;
484                         }
485                 }
486                 if (jack_free) {
487                         jack_free (jc);
488                 } else {
489                         free (jc);
490                 }
491         }
492
493         return false;
494 }
495