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