start work on the changes to EngineControl (dialog) to integrate with new backend...
[ardour.git] / libs / ardour / ardour / 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
27 #include <stdint.h>
28
29 #include <boost/shared_ptr.hpp>
30
31 #include <jack/jack.h>
32 #ifdef HAVE_JACK_SESSION
33 #include <jack/session.h>
34 #endif
35
36 #include "ardour/audio_backend.h"
37
38 namespace ARDOUR {
39
40 class JackConnection;
41
42 class JACKAudioBackend : public AudioBackend {
43   public:
44     JACKAudioBackend (AudioEngine& e, boost::shared_ptr<JackConnection>);
45     ~JACKAudioBackend ();
46
47     std::string name() const;
48     void* private_handle() const;
49     bool connected() 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<std::string> 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     uint32_t available_input_channel_count (const std::string& device) const;
61     uint32_t available_output_channel_count (const std::string& device) const;
62
63     int set_device_name (const std::string&);
64     int set_sample_rate (float);
65     int set_buffer_size (uint32_t);
66     int set_sample_format (SampleFormat);
67     int set_interleaved (bool yn);
68     int set_input_channels (uint32_t);
69     int set_output_channels (uint32_t);
70     int set_systemic_input_latency (uint32_t);
71     int set_systemic_output_latency (uint32_t);
72
73     std::string  device_name () const;
74     float        sample_rate () const;
75     uint32_t     buffer_size () const;
76     SampleFormat sample_format () const;
77     bool         interleaved () const;
78     uint32_t     input_channels () const;
79     uint32_t     output_channels () const;
80     uint32_t     systemic_input_latency () const;
81     uint32_t     systemic_output_latency () const;
82
83     int start ();
84     int stop ();
85     int pause ();
86     int freewheel (bool);
87
88     float cpu_load() const;
89
90     pframes_t sample_time ();
91     pframes_t sample_time_at_cycle_start ();
92     pframes_t samples_since_cycle_start ();
93
94     size_t raw_buffer_size (DataType t);
95
96     int create_process_thread (boost::function<void()> func, pthread_t*, size_t stacksize);
97
98     void transport_start ();
99     void transport_stop ();
100     void transport_locate (framepos_t /*pos*/);
101     TransportState transport_state () const;
102     framepos_t transport_frame() const;
103
104     int set_time_master (bool /*yn*/);
105     bool get_sync_offset (pframes_t& /*offset*/) const;
106
107     void update_latencies ();
108
109     static bool already_configured();
110
111   private:
112     boost::shared_ptr<JackConnection>  _jack_connection; //< shared with JACKPortEngine
113     bool            _running;
114     bool            _freewheeling;
115     std::map<DataType,size_t> _raw_buffer_sizes;
116
117     static int  _xrun_callback (void *arg);
118     static void* _process_thread (void *arg);
119     static int  _sample_rate_callback (pframes_t nframes, void *arg);
120     static int  _bufsize_callback (pframes_t nframes, void *arg);
121     static void _jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int, void*);
122     static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
123     static void _freewheel_callback (int , void *arg);
124     static void _latency_callback (jack_latency_callback_mode_t, void*);
125 #ifdef HAVE_JACK_SESSION
126     static void _session_callback (jack_session_event_t *event, void *arg);
127 #endif
128     
129     void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
130     int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
131     int  jack_bufsize_callback (pframes_t);
132     int  jack_sample_rate_callback (pframes_t);
133     void freewheel_callback (int);
134     int  process_callback (pframes_t nframes);
135     void jack_latency_callback (jack_latency_callback_mode_t);
136     void disconnected (const char*);
137
138     void set_jack_callbacks ();
139     int reconnect_to_jack ();
140     
141     struct ThreadData {
142         JACKAudioBackend* engine;
143         boost::function<void()> f;
144         size_t stacksize;
145         
146         ThreadData (JACKAudioBackend* e, boost::function<void()> fp, size_t stacksz)
147                 : engine (e) , f (fp) , stacksize (stacksz) {}
148     };
149     
150     void*  process_thread ();
151     static void* _start_process_thread (void*);
152
153     ChanCount n_physical (unsigned long) const;
154     
155     void setup_jack_startup_command ();
156
157     /* pffooo */
158
159     std::string  _target_driver;
160     std::string  _target_device;
161     float        _target_sample_rate;
162     uint32_t     _target_buffer_size;
163     SampleFormat _target_sample_format;
164     bool         _target_interleaved;
165     uint32_t     _target_input_channels;
166     uint32_t     _target_output_channels;
167     uint32_t      _target_systemic_input_latency;
168     uint32_t      _target_systemic_output_latency;
169
170     uint32_t _current_sample_rate;
171     uint32_t _current_buffer_size;
172     uint32_t _current_usecs_per_cycle;
173     uint32_t _current_systemic_input_latency;
174     uint32_t _current_systemic_output_latency;
175     
176 };
177
178 } // namespace
179
180 #endif /* __ardour_audiobackend_h__ */
181