a putatively better approach to cleaning up ports at session closing
[ardour.git] / libs / ardour / ardour / port.h
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 #ifndef __ardour_port_h__
21 #define __ardour_port_h__
22
23 #include "libardour-config.h"
24
25 #include <set>
26 #include <string>
27 #include <vector>
28 #include <jack/jack.h>
29 #include <boost/utility.hpp>
30 #include "pbd/signals.h"
31
32 #include "ardour/data_type.h"
33 #include "ardour/types.h"
34
35 namespace ARDOUR {
36
37 class AudioEngine;
38 class Buffer;
39
40 class Port : public boost::noncopyable
41 {
42 public:
43         enum Flags {
44                 IsInput = JackPortIsInput,
45                 IsOutput = JackPortIsOutput,
46         };
47
48         virtual ~Port ();
49
50         static void set_connecting_blocked( bool yn ) {
51                 _connecting_blocked = yn;
52         }
53         static bool connecting_blocked() {
54                 return _connecting_blocked;
55         }
56
57         /** @return Port short name */
58         std::string name () const {
59                 return _name;
60         }
61
62         int set_name (std::string const &);
63
64         /** @return flags */
65         Flags flags () const {
66                 return _flags;
67         }
68
69         /** @return true if this Port receives input, otherwise false */
70         bool receives_input () const {
71                 return _flags & IsInput;
72         }
73
74         /** @return true if this Port sends output, otherwise false */
75         bool sends_output () const {
76                 return _flags & IsOutput;
77         }
78
79         bool connected () const;
80         int disconnect_all ();
81         int get_connections (std::vector<std::string> &) const;
82
83         /* connection by name */
84         bool connected_to (std::string const &) const;
85         int connect (std::string const &);
86         int disconnect (std::string const &);
87
88         /* connection by Port* */
89         bool connected_to (Port *) const;
90         virtual int connect (Port *);
91         int disconnect (Port *);
92
93         void ensure_jack_monitors_input (bool);
94         bool jack_monitoring_input () const;
95         int reestablish ();
96         int reconnect ();
97         void request_jack_monitors_input (bool);
98
99         bool last_monitor() const { return _last_monitor; }
100         void set_last_monitor (bool yn) { _last_monitor = yn; }
101
102         jack_port_t* jack_port() const { return _jack_port; }
103
104         void get_connected_latency_range (jack_latency_range_t& range, bool playback) const;
105
106         void set_private_latency_range (jack_latency_range_t& range, bool playback);
107         const jack_latency_range_t&  private_latency_range (bool playback) const;
108
109         void set_public_latency_range (jack_latency_range_t& range, bool playback) const;
110         jack_latency_range_t public_latency_range (bool playback) const;
111
112         virtual void reset ();
113
114         virtual DataType type () const = 0;
115         virtual void cycle_start (pframes_t);
116         virtual void cycle_end (pframes_t) = 0;
117         virtual void cycle_split () = 0;
118         virtual Buffer& get_buffer (pframes_t nframes) = 0;
119         virtual void flush_buffers (pframes_t /*nframes*/) {}
120         virtual void transport_stopped () {}
121         virtual void realtime_locate () {}
122
123         bool physically_connected () const;
124
125         static void set_engine (AudioEngine *);
126
127         PBD::Signal1<void,bool> MonitorInputChanged;
128         static PBD::Signal2<void,boost::shared_ptr<Port>,boost::shared_ptr<Port> > PostDisconnect;
129         static PBD::Signal0<void> PortDrop;
130
131         static void set_cycle_framecnt (pframes_t n) {
132                 _cycle_nframes = n;
133         }
134         static framecnt_t port_offset() { return _global_port_buffer_offset; }
135         static void set_global_port_buffer_offset (pframes_t off) {
136                 _global_port_buffer_offset = off;
137         }
138         static void increment_global_port_buffer_offset (pframes_t n) {
139                 _global_port_buffer_offset += n;
140         }
141
142         virtual void increment_port_buffer_offset (pframes_t n);
143
144 protected:
145
146         Port (std::string const &, DataType, Flags);
147
148         jack_port_t* _jack_port; ///< JACK port
149
150         static bool       _connecting_blocked;
151         static pframes_t  _global_port_buffer_offset;   /* access only from process() tree */
152         static pframes_t  _cycle_nframes; /* access only from process() tree */
153
154         framecnt_t _port_buffer_offset; /* access only from process() tree */
155
156         jack_latency_range_t _private_playback_latency;
157         jack_latency_range_t _private_capture_latency;
158
159         static AudioEngine* _engine; ///< the AudioEngine
160
161 private:
162         std::string _name;  ///< port short name
163         Flags       _flags; ///< flags
164         bool        _last_monitor;
165
166         /** ports that we are connected to, kept so that we can
167             reconnect to JACK when required
168         */
169         std::set<std::string> _connections;
170
171        void drop ();
172        PBD::ScopedConnection drop_connection;
173 };
174
175 }
176
177 #endif /* __ardour_port_h__ */