Remove ambiguous API implementation
[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 "weak_libjack.h"
33
34 #include "ardour/audio_backend.h"
35
36 namespace ARDOUR {
37
38 class JackConnection;
39 class JACKSession;
40
41 class JACKAudioBackend : public AudioBackend {
42   public:
43     JACKAudioBackend (AudioEngine& e, AudioBackendInfo& info, boost::shared_ptr<JackConnection>);
44     ~JACKAudioBackend ();
45
46     /* AUDIOBACKEND API */
47
48     std::string name() const;
49     void* private_handle() const;
50     bool is_realtime () const;
51
52     bool requires_driver_selection() const;
53     std::vector<std::string> enumerate_drivers () const;
54     int set_driver (const std::string&);
55
56     std::vector<DeviceStatus> enumerate_devices () const;
57
58     std::vector<float> available_sample_rates (const std::string& device) const;
59     std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
60     std::vector<uint32_t> available_period_sizes (const std::string& driver) const;
61     uint32_t available_input_channel_count (const std::string& device) const;
62     uint32_t available_output_channel_count (const std::string& device) const;
63
64     bool can_change_sample_rate_when_running() const;
65     bool can_change_buffer_size_when_running() const;
66
67     int set_device_name (const std::string&);
68     int set_sample_rate (float);
69     int set_buffer_size (uint32_t);
70     int set_peridod_size (uint32_t);
71     int set_interleaved (bool yn);
72     int set_input_channels (uint32_t);
73     int set_output_channels (uint32_t);
74     int set_systemic_input_latency (uint32_t);
75     int set_systemic_output_latency (uint32_t);
76     int set_systemic_midi_input_latency (std::string const, uint32_t) { return 0; }
77     int set_systemic_midi_output_latency (std::string const, uint32_t) { return 0; }
78
79     int reset_device ();
80
81     std::string  device_name () const;
82     float        sample_rate () const;
83     uint32_t     buffer_size () const;
84     uint32_t     period_size () const;
85     bool         interleaved () const;
86     uint32_t     input_channels () const;
87     uint32_t     output_channels () const;
88     uint32_t     systemic_input_latency () const;
89     uint32_t     systemic_output_latency () const;
90     uint32_t     systemic_midi_input_latency (std::string const) const { return 0; }
91     uint32_t     systemic_midi_output_latency (std::string const) const { return 0; }
92     std::string  driver_name() const;
93
94     std::string control_app_name () const;
95     void launch_control_app ();
96
97     int stop ();
98     int freewheel (bool);
99
100     float dsp_load() const;
101
102     samplepos_t sample_time ();
103     samplepos_t sample_time_at_cycle_start ();
104     pframes_t samples_since_cycle_start ();
105
106     size_t raw_buffer_size (DataType t);
107
108     int create_process_thread (boost::function<void()> func);
109     int join_process_threads ();
110     bool in_process_thread ();
111     uint32_t process_thread_count ();
112                 int client_real_time_priority ();
113
114     void transport_start ();
115     void transport_stop ();
116     void transport_locate (samplepos_t /*pos*/);
117     TransportState transport_state () const;
118     samplepos_t transport_sample() const;
119
120     int set_time_master (bool /*yn*/);
121     bool get_sync_offset (pframes_t& /*offset*/) const;
122
123     void update_latencies ();
124
125     static bool already_configured();
126
127     /* PORTENGINE API */
128
129     const std::string& my_name() const;
130     uint32_t port_name_size() const;
131
132     int         set_port_name (PortHandle, const std::string&);
133     std::string get_port_name (PortHandle) const;
134     PortFlags get_port_flags (PortHandle) const;
135     PortHandle  get_port_by_name (const std::string&) const;
136     int get_port_property (PortHandle, const std::string& key, std::string& value, std::string& type) const;
137     int set_port_property (PortHandle, const std::string& key, const std::string& value, const std::string& type);
138
139     int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&) const;
140
141     DataType port_data_type (PortHandle) const;
142
143     PortHandle register_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
144     void  unregister_port (PortHandle);
145
146     bool  connected (PortHandle, bool process_callback_safe);
147     bool  connected_to (PortHandle, const std::string&, bool process_callback_safe);
148     bool  physically_connected (PortHandle, bool process_callback_safe);
149     bool  externally_connected (PortHandle, bool process_callback_safe);
150     int   get_connections (PortHandle, std::vector<std::string>&, bool process_callback_safe);
151     int   connect (PortHandle, const std::string&);
152
153     int   disconnect (PortHandle, const std::string&);
154     int   disconnect_all (PortHandle);
155     int   connect (const std::string& src, const std::string& dst);
156     int   disconnect (const std::string& src, const std::string& dst);
157
158     /* MIDI */
159
160     std::vector<std::string> enumerate_midi_options () const;
161     int set_midi_option (const std::string&);
162     std::string midi_option () const;
163
164     std::vector<DeviceStatus> enumerate_midi_devices () const {
165         return std::vector<AudioBackend::DeviceStatus> ();
166     }
167     int set_midi_device_enabled (std::string const, bool) {
168         return 0;
169     }
170     bool midi_device_enabled (std::string const) const {
171         return true;
172     }
173     bool can_set_systemic_midi_latencies () const {
174         return false;
175     }
176     bool can_set_period_size () const {
177         return true;
178     }
179
180     int      midi_event_get (pframes_t& timestamp, size_t& size, uint8_t const** buf, void* port_buffer, uint32_t event_index);
181     int      midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
182     uint32_t get_midi_event_count (void* port_buffer);
183     void     midi_clear (void* port_buffer);
184
185     /* Monitoring */
186
187     bool  can_monitor_input() const;
188     int   request_input_monitoring (PortHandle, bool);
189     int   ensure_input_monitoring (PortHandle, bool);
190     bool  monitoring_input (PortHandle);
191
192     /* Latency management
193      */
194
195     void          set_latency_range (PortHandle, bool for_playback, LatencyRange);
196     LatencyRange  get_latency_range (PortHandle, bool for_playback);
197
198     /* Physical ports */
199
200     bool      port_is_physical (PortHandle) const;
201     void      get_physical_outputs (DataType type, std::vector<std::string>&);
202     void      get_physical_inputs (DataType type, std::vector<std::string>&);
203     ChanCount n_physical_outputs () const;
204     ChanCount n_physical_inputs () const;
205
206     /* Getting access to the data buffer for a port */
207
208     void* get_buffer (PortHandle, pframes_t);
209
210     /* transport sync */
211
212     bool speed_and_position (double& sp, samplepos_t& pos);
213
214   private:
215     boost::shared_ptr<JackConnection>  _jack_connection;
216     bool            _running;
217     bool            _freewheeling;
218     std::map<DataType,size_t> _raw_buffer_sizes;
219
220     std::vector<jack_native_thread_t> _jack_threads;
221     jack_native_thread_t _main_thread;
222
223     static int  _xrun_callback (void *arg);
224     static void* _process_thread (void *arg);
225     static int  _sample_rate_callback (pframes_t nframes, void *arg);
226     static int  _bufsize_callback (pframes_t nframes, void *arg);
227     static void _jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int, void*);
228     static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
229     static void _freewheel_callback (int , void *arg);
230     static void _latency_callback (jack_latency_callback_mode_t, void*);
231     static void _session_callback (jack_session_event_t *event, void *arg);
232
233     void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
234     int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
235     int  jack_bufsize_callback (pframes_t);
236     int  jack_sample_rate_callback (pframes_t);
237     void freewheel_callback (int);
238     int  process_callback (pframes_t nframes);
239     void jack_latency_callback (jack_latency_callback_mode_t);
240     void disconnected (const char*);
241
242     void set_jack_callbacks ();
243     int reconnect_to_jack ();
244
245                 bool available() const;
246
247     struct ThreadData {
248         JACKAudioBackend* engine;
249         boost::function<void()> f;
250         size_t stacksize;
251
252         ThreadData (JACKAudioBackend* e, boost::function<void()> fp, size_t stacksz)
253                 : engine (e) , f (fp) , stacksize (stacksz) {}
254     };
255
256     void*  process_thread ();
257     static void* _start_process_thread (void*);
258
259     void setup_jack_startup_command (bool for_latency_measurement);
260
261     /* pffooo */
262
263     std::string  _target_driver;
264     std::string  _target_device;
265     float        _target_sample_rate;
266     uint32_t     _target_buffer_size;
267     uint32_t     _target_num_periods;
268     SampleFormat _target_sample_format;
269     bool         _target_interleaved;
270     uint32_t     _target_input_channels;
271     uint32_t     _target_output_channels;
272     uint32_t     _target_systemic_input_latency;
273     uint32_t     _target_systemic_output_latency;
274     uint32_t     _current_sample_rate;
275     uint32_t     _current_buffer_size;
276     std::string  _target_midi_option;
277
278     typedef std::set<std::string> DeviceList;
279     typedef std::map<std::string,DeviceList> DriverDeviceMap;
280
281     mutable DriverDeviceMap all_devices;
282
283     PBD::ScopedConnection disconnect_connection;
284
285     /* PORTENGINE RELATED */
286
287     static int  _graph_order_callback (void *arg);
288     static void _registration_callback (jack_port_id_t, int, void *);
289     static void _connect_callback (jack_port_id_t, jack_port_id_t, int, void *);
290
291     void connect_callback (jack_port_id_t, jack_port_id_t, int);
292
293     ChanCount n_physical (unsigned long flags) const;
294     void get_physical (DataType type, unsigned long flags, std::vector<std::string>& phy) const;
295
296     void when_connected_to_jack ();
297     PBD::ScopedConnection jack_connection_connection;
298
299     /* Object to manage interactions with Session in a way that
300        keeps JACK out of libardour directly
301     */
302
303     JACKSession* _session;
304
305   protected:
306     int _start (bool for_latency_measurement);
307 };
308
309 } // namespace
310
311 #endif /* __ardour_audiobackend_h__ */
312