don't confuse arm with i686
[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     std::string name() const;
49     void* private_handle() const;
50     bool connected() const;
51     bool is_realtime () const;
52
53     bool requires_driver_selection() const;
54     std::vector<std::string> enumerate_drivers () const;
55     int set_driver (const std::string&);
56
57     std::vector<DeviceStatus> enumerate_devices () const;
58
59     std::vector<float> available_sample_rates (const std::string& device) const;
60     std::vector<uint32_t> available_buffer_sizes (const std::string& device) 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_sample_format (SampleFormat);
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
77     std::string  device_name () const;
78     float        sample_rate () const;
79     uint32_t     buffer_size () const;
80     SampleFormat sample_format () const;
81     bool         interleaved () const;
82     uint32_t     input_channels () const;
83     uint32_t     output_channels () const;
84     uint32_t     systemic_input_latency () const;
85     uint32_t     systemic_output_latency () const;
86
87     std::string control_app_name () const;
88     void launch_control_app ();
89
90     int start ();
91     int stop ();
92     int pause ();
93     int freewheel (bool);
94
95     float cpu_load() const;
96
97     pframes_t sample_time ();
98     pframes_t sample_time_at_cycle_start ();
99     pframes_t samples_since_cycle_start ();
100
101     size_t raw_buffer_size (DataType t);
102
103     int create_process_thread (boost::function<void()> func, AudioBackendNativeThread*, size_t stacksize);
104     int wait_for_process_thread_exit (AudioBackendNativeThread);
105
106     void transport_start ();
107     void transport_stop ();
108     void transport_locate (framepos_t /*pos*/);
109     TransportState transport_state () const;
110     framepos_t transport_frame() const;
111
112     int set_time_master (bool /*yn*/);
113     bool get_sync_offset (pframes_t& /*offset*/) const;
114
115     void update_latencies ();
116
117     static bool already_configured();
118
119   private:
120     boost::shared_ptr<JackConnection>  _jack_connection; //< shared with JACKPortEngine
121     bool            _running;
122     bool            _freewheeling;
123     std::map<DataType,size_t> _raw_buffer_sizes;
124
125     static int  _xrun_callback (void *arg);
126     static void* _process_thread (void *arg);
127     static int  _sample_rate_callback (pframes_t nframes, void *arg);
128     static int  _bufsize_callback (pframes_t nframes, void *arg);
129     static void _jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int, void*);
130     static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
131     static void _freewheel_callback (int , void *arg);
132     static void _latency_callback (jack_latency_callback_mode_t, void*);
133 #ifdef HAVE_JACK_SESSION
134     static void _session_callback (jack_session_event_t *event, void *arg);
135 #endif
136     
137     void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
138     int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
139     int  jack_bufsize_callback (pframes_t);
140     int  jack_sample_rate_callback (pframes_t);
141     void freewheel_callback (int);
142     int  process_callback (pframes_t nframes);
143     void jack_latency_callback (jack_latency_callback_mode_t);
144     void disconnected (const char*);
145
146     void set_jack_callbacks ();
147     int reconnect_to_jack ();
148     
149     struct ThreadData {
150         JACKAudioBackend* engine;
151         boost::function<void()> f;
152         size_t stacksize;
153         
154         ThreadData (JACKAudioBackend* e, boost::function<void()> fp, size_t stacksz)
155                 : engine (e) , f (fp) , stacksize (stacksz) {}
156     };
157     
158     void*  process_thread ();
159     static void* _start_process_thread (void*);
160
161     ChanCount n_physical (unsigned long) const;
162     
163     void setup_jack_startup_command ();
164
165     /* pffooo */
166
167     std::string  _target_driver;
168     std::string  _target_device;
169     float        _target_sample_rate;
170     uint32_t     _target_buffer_size;
171     SampleFormat _target_sample_format;
172     bool         _target_interleaved;
173     uint32_t     _target_input_channels;
174     uint32_t     _target_output_channels;
175     uint32_t     _target_systemic_input_latency;
176     uint32_t     _target_systemic_output_latency;
177     uint32_t     _current_sample_rate;
178     uint32_t     _current_buffer_size;
179
180     typedef std::set<std::string> DeviceList;
181     typedef std::map<std::string,DeviceList> DriverDeviceMap;
182     
183     mutable DriverDeviceMap all_devices;
184
185     PBD::ScopedConnection disconnect_connection;
186 };
187
188 } // namespace
189
190 #endif /* __ardour_audiobackend_h__ */
191