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