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