more jack include fixes
[ardour.git] / libs / ardour / ardour / audioengine.h
1 /*
2     Copyright (C) 2002-2004 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_audioengine_h__
21 #define __ardour_audioengine_h__
22
23 #ifdef WAF_BUILD
24 #include "libardour-config.h"
25 #endif
26
27 #include <iostream>
28 #include <list>
29 #include <set>
30 #include <cmath>
31 #include <exception>
32 #include <string>
33
34 #include <glibmm/thread.h>
35
36 #include "pbd/rcu.h"
37 #include "pbd/signals.h"
38
39 #include <jack/weakjack.h>
40 #include <jack/jack.h>
41 #include <jack/transport.h>
42 #include <jack/thread.h>
43
44 #include "ardour/ardour.h"
45
46 #include "ardour/data_type.h"
47 #include "ardour/session_handle.h"
48 #include "ardour/types.h"
49 #include "ardour/chan_count.h"
50
51 #ifdef HAVE_JACK_SESSION
52 #include <jack/session.h>
53 #endif
54
55 namespace ARDOUR {
56
57 class InternalPort;
58 class MidiPort;
59 class Port;
60 class Session;
61 class ProcessThread;
62
63 class AudioEngine : public SessionHandlePtr
64 {
65 public:
66         typedef std::set<Port*> Ports;
67
68         class disconnected_exception : public std::exception {
69         public:
70                 virtual const char *what() const throw() { return "AudioEngine is disconnected"; }
71         };
72
73         AudioEngine (std::string client_name, std::string session_uuid);
74         virtual ~AudioEngine ();
75
76         jack_client_t* jack() const;
77         bool connected() const { return _jack != 0; }
78
79         bool is_realtime () const;
80
81         ProcessThread* main_thread() const { return _main_thread; }
82
83         std::string client_name() const { return jack_client_name; }
84
85         int reconnect_to_jack ();
86         int disconnect_from_jack();
87
88         bool will_reconnect_at_halt ();
89         void set_reconnect_at_halt (bool);
90
91         int stop (bool forever = false);
92         int start ();
93         bool running() const { return _running; }
94
95         Glib::Mutex& process_lock() { return _process_lock; }
96
97         framecnt_t frame_rate () const;
98         pframes_t frames_per_cycle () const;
99
100         size_t raw_buffer_size(DataType t);
101
102         int usecs_per_cycle () const { return _usecs_per_cycle; }
103
104         bool get_sync_offset (pframes_t & offset) const;
105
106         pframes_t frames_since_cycle_start () {
107                 jack_client_t* _priv_jack = _jack;
108                 if (!_running || !_priv_jack) {
109                         return 0;
110                 }
111                 return jack_frames_since_cycle_start (_priv_jack);
112         }
113
114         pframes_t frame_time () {
115                 jack_client_t* _priv_jack = _jack;
116                 if (!_running || !_priv_jack) {
117                         return 0;
118                 }
119                 return jack_frame_time (_priv_jack);
120         }
121
122         pframes_t frame_time_at_cycle_start () {
123                 jack_client_t* _priv_jack = _jack;
124                 if (!_running || !_priv_jack) {
125                         return 0;
126                 }
127                 return jack_last_frame_time (_priv_jack);
128         }
129
130         pframes_t transport_frame () const {
131                 const jack_client_t* _priv_jack = _jack;
132                 if (!_running || !_priv_jack) {
133                         return 0;
134                 }
135                 return jack_get_current_transport_frame (_priv_jack);
136         }
137
138         int request_buffer_size (pframes_t);
139
140         framecnt_t set_monitor_check_interval (framecnt_t);
141         framecnt_t processed_frames() const { return _processed_frames; }
142
143         float get_cpu_load() {
144                 jack_client_t* _priv_jack = _jack;
145                 if (!_running || !_priv_jack) {
146                         return 0;
147                 }
148                 return jack_cpu_load (_priv_jack);
149         }
150
151         void set_session (Session *);
152         void remove_session (); // not a replacement for SessionHandle::session_going_away()
153
154         class PortRegistrationFailure : public std::exception {
155         public:
156                 PortRegistrationFailure (std::string const & why = "")
157                         : reason (why) {}
158
159                 ~PortRegistrationFailure () throw () {}
160
161                 virtual const char *what() const throw () { return reason.c_str(); }
162
163         private:
164                 std::string reason;
165         };
166
167         class NoBackendAvailable : public std::exception {
168         public:
169                 virtual const char *what() const throw() { return "could not connect to engine backend"; }
170         };
171
172         Port *register_input_port (DataType, const std::string& portname);
173         Port *register_output_port (DataType, const std::string& portname);
174         int   unregister_port (Port &);
175
176         bool port_is_physical (const std::string&) const;
177         void ensure_monitor_input (const std::string&, bool) const;
178
179         void split_cycle (pframes_t offset);
180
181         int connect (const std::string& source, const std::string& destination);
182         int disconnect (const std::string& source, const std::string& destination);
183         int disconnect (Port &);
184
185         const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
186
187         bool can_request_hardware_monitoring ();
188
189         ChanCount n_physical_outputs () const;
190         ChanCount n_physical_inputs () const;
191
192         void get_physical_outputs (DataType type, std::vector<std::string>&);
193         void get_physical_inputs (DataType type, std::vector<std::string>&);
194
195         Port *get_port_by_name (const std::string &);
196
197         enum TransportState {
198                 TransportStopped = JackTransportStopped,
199                 TransportRolling = JackTransportRolling,
200                 TransportLooping = JackTransportLooping,
201                 TransportStarting = JackTransportStarting
202         };
203
204         void transport_start ();
205         void transport_stop ();
206         void transport_locate (framepos_t);
207         TransportState transport_state ();
208
209         int  reset_timebase ();
210
211         void update_latencies ();
212
213         /* start/stop freewheeling */
214
215         int freewheel (bool onoff);
216         bool freewheeling() const { return _freewheeling; }
217
218         /* this signal is sent for every process() cycle while freewheeling.
219 _          the regular process() call to session->process() is not made.
220         */
221
222         PBD::Signal1<int, pframes_t> Freewheel;
223
224         PBD::Signal0<void> Xrun;
225
226         /* this signal is if JACK notifies us of a graph order event */
227
228         PBD::Signal0<void> GraphReordered;
229
230 #ifdef HAVE_JACK_SESSION
231         PBD::Signal1<void,jack_session_event_t *> JackSessionEvent;
232 #endif
233
234
235         /* this signal is emitted if the sample rate changes */
236
237         PBD::Signal1<void, framecnt_t> SampleRateChanged;
238
239         /* this signal is sent if JACK ever disconnects us */
240
241         PBD::Signal1<void,const char*> Halted;
242
243         /* these two are emitted when the engine itself is
244            started and stopped
245         */
246
247         PBD::Signal0<void> Running;
248         PBD::Signal0<void> Stopped;
249
250         /** Emitted if a JACK port is registered or unregistered */
251         PBD::Signal0<void> PortRegisteredOrUnregistered;
252
253         /** Emitted if a JACK port is connected or disconnected.
254          *  The Port parameters are the ports being connected / disconnected, or 0 if they are not known to Ardour.
255          *  The std::string parameters are the (long) port names.
256          *  The bool parameter is true if ports were connected, or false for disconnected.
257          */
258         PBD::Signal5<void, Port *, std::string, Port *, std::string, bool> PortConnectedOrDisconnected;
259
260         std::string make_port_name_relative (std::string) const;
261         std::string make_port_name_non_relative (std::string) const;
262         bool port_is_mine (const std::string&) const;
263
264         static AudioEngine* instance() { return _instance; }
265         void died ();
266
267         int create_process_thread (boost::function<void()>, pthread_t*, size_t stacksize);
268
269 private:
270         static AudioEngine*       _instance;
271
272         jack_client_t* volatile   _jack; /* could be reset to null by SIGPIPE or another thread */
273         std::string                jack_client_name;
274         Glib::Mutex               _process_lock;
275         Glib::Cond                 session_removed;
276         bool                       session_remove_pending;
277         bool                      _running;
278         bool                      _has_run;
279         mutable framecnt_t        _buffer_size;
280         std::map<DataType,size_t> _raw_buffer_sizes;
281         mutable framecnt_t        _frame_rate;
282         /// number of frames between each check for changes in monitor input
283         framecnt_t                 monitor_check_interval;
284         /// time of the last monitor check in frames
285         framecnt_t                 last_monitor_check;
286         /// the number of frames processed since start() was called
287         framecnt_t                _processed_frames;
288         bool                      _freewheeling;
289         bool                      _freewheel_pending;
290         boost::function<int(framecnt_t)>  freewheel_action;
291         bool                       reconnect_on_halt;
292         int                       _usecs_per_cycle;
293         bool                       port_remove_in_progress;
294
295         SerializedRCUManager<Ports> ports;
296
297         Port* register_port (DataType type, const std::string& portname, bool input);
298
299         int    process_callback (pframes_t nframes);
300         void*  process_thread ();
301         void   remove_all_ports ();
302
303         ChanCount n_physical (unsigned long) const;
304         void get_physical (DataType, unsigned long, std::vector<std::string> &);
305
306         void port_registration_failure (const std::string& portname);
307
308         static int  _xrun_callback (void *arg);
309 #ifdef HAVE_JACK_SESSION
310         static void _session_callback (jack_session_event_t *event, void *arg);
311 #endif
312         static int  _graph_order_callback (void *arg);
313         static int  _process_callback (pframes_t nframes, void *arg);
314         static void* _process_thread (void *arg);
315         static int  _sample_rate_callback (pframes_t nframes, void *arg);
316         static int  _bufsize_callback (pframes_t nframes, void *arg);
317         static void _jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int, void*);
318         static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
319         static void _freewheel_callback (int , void *arg);
320         static void _registration_callback (jack_port_id_t, int, void *);
321         static void _connect_callback (jack_port_id_t, jack_port_id_t, int, void *);
322
323         void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
324         int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
325         int  jack_bufsize_callback (pframes_t);
326         int  jack_sample_rate_callback (pframes_t);
327
328         void set_jack_callbacks ();
329
330         static void _latency_callback (jack_latency_callback_mode_t, void*);
331         void jack_latency_callback (jack_latency_callback_mode_t);
332
333         int connect_to_jack (std::string client_name, std::string session_uuid);
334
335         static void halted (void *);
336         static void halted_info (jack_status_t,const char*,void *);
337
338         void meter_thread ();
339         void start_metering_thread ();
340         void stop_metering_thread ();
341
342         Glib::Thread*    m_meter_thread;
343         static gint      m_meter_exit;
344
345         ProcessThread* _main_thread;
346
347         struct ThreadData {
348                 AudioEngine* engine;
349                 boost::function<void()> f;
350                 size_t stacksize;
351
352                 ThreadData (AudioEngine* ae, boost::function<void()> fp, size_t stacksz)
353                 : engine (ae) , f (fp) , stacksize (stacksz) {}
354         };
355
356         static void* _start_process_thread (void*);
357 };
358
359 } // namespace ARDOUR
360
361 #endif /* __ardour_audioengine_h__ */