9ab545f3eec350665688457b73567ea1675c2518
[ardour.git] / libs / backends / jack / jack_audiobackend.h
1 /*
2     Copyright (C) 2013 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 __libardour_jack_audiobackend_h__
21 #define __libardour_jack_audiobackend_h__
22
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <set>
27
28 #include <stdint.h>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include <jack/jack.h>
33 #ifdef HAVE_JACK_SESSION
34 #include <jack/session.h>
35 #endif
36
37 #include "ardour/audio_backend.h"
38
39 namespace ARDOUR {
40
41 class JackConnection;
42
43 class JACKAudioBackend : public AudioBackend {
44   public:
45     JACKAudioBackend (AudioEngine& e, boost::shared_ptr<JackConnection>);
46     ~JACKAudioBackend ();
47     
48     /* AUDIOBACKEND API */
49
50     std::string name() const;
51     void* private_handle() const;
52     bool available() const;
53     bool is_realtime () const;
54
55     bool requires_driver_selection() const;
56     std::vector<std::string> enumerate_drivers () const;
57     int set_driver (const std::string&);
58
59     std::vector<DeviceStatus> enumerate_devices () const;
60
61     std::vector<float> available_sample_rates (const std::string& device) const;
62     std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
63     uint32_t available_input_channel_count (const std::string& device) const;
64     uint32_t available_output_channel_count (const std::string& device) const;
65
66     bool can_change_sample_rate_when_running() const;
67     bool can_change_buffer_size_when_running() const;
68
69     int set_device_name (const std::string&);
70     int set_sample_rate (float);
71     int set_buffer_size (uint32_t);
72     int set_sample_format (SampleFormat);
73     int set_interleaved (bool yn);
74     int set_input_channels (uint32_t);
75     int set_output_channels (uint32_t);
76     int set_systemic_input_latency (uint32_t);
77     int set_systemic_output_latency (uint32_t);
78
79     std::string  device_name () const;
80     float        sample_rate () const;
81     uint32_t     buffer_size () const;
82     SampleFormat sample_format () const;
83     bool         interleaved () const;
84     uint32_t     input_channels () const;
85     uint32_t     output_channels () const;
86     uint32_t     systemic_input_latency () const;
87     uint32_t     systemic_output_latency () const;
88     std::string  driver_name() const;
89
90     std::string control_app_name () const;
91     void launch_control_app ();
92
93     int start ();
94     int stop ();
95     int pause ();
96     int freewheel (bool);
97
98     float cpu_load() const;
99
100     pframes_t sample_time ();
101     pframes_t sample_time_at_cycle_start ();
102     pframes_t samples_since_cycle_start ();
103
104     size_t raw_buffer_size (DataType t);
105
106     int create_process_thread (boost::function<void()> func, AudioBackendNativeThread*, size_t stacksize);
107     int wait_for_process_thread_exit (AudioBackendNativeThread);
108
109     void transport_start ();
110     void transport_stop ();
111     void transport_locate (framepos_t /*pos*/);
112     TransportState transport_state () const;
113     framepos_t transport_frame() const;
114
115     int set_time_master (bool /*yn*/);
116     bool get_sync_offset (pframes_t& /*offset*/) const;
117
118     void update_latencies ();
119
120     static bool already_configured();
121     
122     /* PORTENGINE API */
123
124     const std::string& my_name() const;
125     uint32_t port_name_size() const;
126
127     int         set_port_name (PortHandle, const std::string&);
128     std::string get_port_name (PortHandle) const;
129     PortHandle* get_port_by_name (const std::string&) const;
130
131     int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&) const;
132
133     DataType port_data_type (PortHandle) const;
134
135     PortHandle register_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
136     void  unregister_port (PortHandle);
137
138     bool  connected (PortHandle, bool process_callback_safe);
139     bool  connected_to (PortHandle, const std::string&, bool process_callback_safe);
140     bool  physically_connected (PortHandle, bool process_callback_safe);
141     int   get_connections (PortHandle, std::vector<std::string>&, bool process_callback_safe);
142     int   connect (PortHandle, const std::string&);
143
144     int   disconnect (PortHandle, const std::string&);
145     int   disconnect_all (PortHandle);
146     int   connect (const std::string& src, const std::string& dst);
147     int   disconnect (const std::string& src, const std::string& dst);
148     
149     /* MIDI */
150
151     int      midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index);
152     int      midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
153     uint32_t get_midi_event_count (void* port_buffer);
154     void     midi_clear (void* port_buffer);
155
156     /* Monitoring */
157
158     bool  can_monitor_input() const;
159     int   request_input_monitoring (PortHandle, bool);
160     int   ensure_input_monitoring (PortHandle, bool);
161     bool  monitoring_input (PortHandle);
162
163     /* Latency management
164      */
165     
166     void          set_latency_range (PortHandle, bool for_playback, LatencyRange);
167     LatencyRange  get_latency_range (PortHandle, bool for_playback);
168
169     /* Physical ports */
170
171     bool      port_is_physical (PortHandle) const;
172     void      get_physical_outputs (DataType type, std::vector<std::string>&);
173     void      get_physical_inputs (DataType type, std::vector<std::string>&);
174     ChanCount n_physical_outputs () const;
175     ChanCount n_physical_inputs () const;
176
177     /* Getting access to the data buffer for a port */
178
179     void* get_buffer (PortHandle, pframes_t);
180
181   private:
182     boost::shared_ptr<JackConnection>  _jack_connection;
183     bool            _running;
184     bool            _freewheeling;
185     std::map<DataType,size_t> _raw_buffer_sizes;
186
187     static int  _xrun_callback (void *arg);
188     static void* _process_thread (void *arg);
189     static int  _sample_rate_callback (pframes_t nframes, void *arg);
190     static int  _bufsize_callback (pframes_t nframes, void *arg);
191     static void _jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int, void*);
192     static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
193     static void _freewheel_callback (int , void *arg);
194     static void _latency_callback (jack_latency_callback_mode_t, void*);
195 #ifdef HAVE_JACK_SESSION
196     static void _session_callback (jack_session_event_t *event, void *arg);
197 #endif
198     
199     void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
200     int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
201     int  jack_bufsize_callback (pframes_t);
202     int  jack_sample_rate_callback (pframes_t);
203     void freewheel_callback (int);
204     int  process_callback (pframes_t nframes);
205     void jack_latency_callback (jack_latency_callback_mode_t);
206     void disconnected (const char*);
207
208     void set_jack_callbacks ();
209     int reconnect_to_jack ();
210     
211     struct ThreadData {
212         JACKAudioBackend* engine;
213         boost::function<void()> f;
214         size_t stacksize;
215         
216         ThreadData (JACKAudioBackend* e, boost::function<void()> fp, size_t stacksz)
217                 : engine (e) , f (fp) , stacksize (stacksz) {}
218     };
219     
220     void*  process_thread ();
221     static void* _start_process_thread (void*);
222
223     void setup_jack_startup_command ();
224
225     /* pffooo */
226
227     std::string  _target_driver;
228     std::string  _target_device;
229     float        _target_sample_rate;
230     uint32_t     _target_buffer_size;
231     SampleFormat _target_sample_format;
232     bool         _target_interleaved;
233     uint32_t     _target_input_channels;
234     uint32_t     _target_output_channels;
235     uint32_t     _target_systemic_input_latency;
236     uint32_t     _target_systemic_output_latency;
237     uint32_t     _current_sample_rate;
238     uint32_t     _current_buffer_size;
239
240     typedef std::set<std::string> DeviceList;
241     typedef std::map<std::string,DeviceList> DriverDeviceMap;
242     
243     mutable DriverDeviceMap all_devices;
244
245     PBD::ScopedConnection disconnect_connection;
246
247     /* PORTENGINE RELATED */
248
249     static int  _graph_order_callback (void *arg);
250     static void _registration_callback (jack_port_id_t, int, void *);
251     static void _connect_callback (jack_port_id_t, jack_port_id_t, int, void *);
252
253     void connect_callback (jack_port_id_t, jack_port_id_t, int);
254
255     ChanCount n_physical (unsigned long flags) const;
256     void get_physical (DataType type, unsigned long flags, std::vector<std::string>& phy) const;
257
258     void when_connected_to_jack ();
259     PBD::ScopedConnection jack_connection_connection;
260 };
261
262 } // namespace
263
264 #endif /* __ardour_audiobackend_h__ */
265