provide more accurate diagnostics from AudioEngine::port_registration_failure() and...
[ardour.git] / libs / ardour / audioengine.cc
index 5f86f217628d96205fde46ec99c8873e048a000b..cc74b92a6942c464aec4596d237bf61f28e58c96 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <unistd.h>
 #include <cerrno>
 #include <vector>
+#include <sstream>
 
 #include <glibmm/timer.h>
 #include <pbd/pthread_utils.h>
+#include <pbd/stacktrace.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/buffer.h>
@@ -31,9 +32,6 @@
 #include <ardour/session.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/utils.h>
-#ifdef VST_SUPPORT
-#include <fst.h>
-#endif
 
 #include <ardour/timestamps.h>
 
@@ -43,8 +41,14 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-jack_nframes_t Port::_short_over_length = 2;
-jack_nframes_t Port::_long_over_length = 10;
+gint AudioEngine::m_meter_exit;
+
+static void 
+ardour_jack_error (const char* msg) 
+{
+       // throw JACK errors away - they cause visual clutter
+       // error << "JACK: " << msg << endmsg;
+}
 
 AudioEngine::AudioEngine (string client_name) 
        : ports (new Ports)
@@ -56,34 +60,34 @@ AudioEngine::AudioEngine (string client_name)
        last_monitor_check = 0;
        monitor_check_interval = max_frames;
        _processed_frames = 0;
-       _freewheeling = false;
        _usecs_per_cycle = 0;
        _jack = 0;
        _frame_rate = 0;
        _buffer_size = 0;
-       _freewheeling = false;
        _freewheel_thread_registered = false;
-    
-    m_meter_thread = 0;
-    m_meter_exit = false;
+       _freewheeling = false;
+
+       m_meter_thread = 0;
+       g_atomic_int_set (&m_meter_exit, 0);
 
-    start_metering_thread();
-    
        if (connect_to_jack (client_name)) {
                throw NoBackendAvailable ();
        }
-
 }
 
 AudioEngine::~AudioEngine ()
 {
-       if (_running) {
-               jack_client_close (_jack);
+       {
+               Glib::Mutex::Lock tm (_process_lock);
+               session_removed.signal ();
+               
+               if (_running) {
+                       jack_client_close (_jack);
+                       _jack = 0;
+               }
+               
+               stop_metering_thread ();
        }
-
-    if(m_meter_thread) {
-        g_atomic_int_inc(&m_meter_exit);
-    }
 }
 
 void
@@ -93,16 +97,23 @@ _thread_init_callback (void *arg)
           knows about it.
        */
 
-       PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("Audioengine"), 4096);
+       PBD::notify_gui_about_thread_creation (pthread_self(), X_("Audioengine"), 4096);
 }
 
 int
 AudioEngine::start ()
 {
+       if (!_jack) {
+               error << _("AudioEngine::start() called while disconnected from JACK") << endmsg;
+               return -1;
+       }
+
        if (!_running) {
 
                if (session) {
-                       jack_nframes_t blocksize = jack_get_buffer_size (_jack);
+                       nframes_t blocksize = jack_get_buffer_size (_jack);
+
+                       BootMessage (_("Connect session to engine"));
 
                        session->set_block_size (blocksize);
                        session->set_frame_rate (jack_get_sample_rate (_jack));
@@ -143,40 +154,65 @@ AudioEngine::start ()
                        _has_run = true;
                        Running(); /* EMIT SIGNAL */
                } else {
-                       error << _("cannot activate JACK client") << endmsg;
+                       // error << _("cannot activate JACK client") << endmsg;
                }
+
+               start_metering_thread();
        }
 
        return _running ? 0 : -1;
 }
 
 int
-AudioEngine::stop ()
+AudioEngine::stop (bool forever)
 {
-       if (_running) {
-               _running = false;
-               jack_deactivate (_jack);
-               Stopped(); /* EMIT SIGNAL */
+       if (_jack) {
+               if (forever) {
+                       disconnect_from_jack ();
+               } else {
+                       jack_deactivate (_jack);
+                       Stopped(); /* EMIT SIGNAL */
+               }
        }
 
        return _running ? -1 : 0;
 }
 
 
-void
-AudioEngine::_jack_timebase_callback (jack_transport_state_t state, jack_nframes_t nframes,
+bool
+AudioEngine::get_sync_offset (nframes_t& offset) const
+{
 
-                                                                         jack_position_t* pos, int new_position, void *arg)
+#ifdef HAVE_JACK_VIDEO_SUPPORT
+
+       jack_position_t pos;
+       
+       if (_jack) {
+               (void) jack_transport_query (_jack, &pos);
+               
+               if (pos.valid & JackVideoFrameOffset) {
+                       offset = pos.video_offset;
+                       return true;
+               }
+       }
+
+#endif
+
+       return false;
+}
+
+void
+AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
+                                     jack_position_t* pos, int new_position, void *arg)
 {
        static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
 }
 
 void
-AudioEngine::jack_timebase_callback (jack_transport_state_t state, jack_nframes_t nframes,
-
-                                                                        jack_position_t* pos, int new_position)
+AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
+                                    jack_position_t* pos, int new_position)
 {
-       if (session && session->synced_to_jack()) {
+       if (_jack && session && session->synced_to_jack()) {
                session->jack_timebase_callback (state, nframes, pos, new_position);
        }
 }
@@ -190,29 +226,35 @@ AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t*
 int
 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
 {
-       if (session) {
+       if (_jack && session) {
                return session->jack_sync_callback (state, pos);
-       } else {
-               return true;
        }
+
+       return true;
 }
 
 int
 AudioEngine::_xrun_callback (void *arg)
 {
-        static_cast<AudioEngine *>(arg)->Xrun (); /* EMIT SIGNAL */
+       AudioEngine* ae = static_cast<AudioEngine*> (arg);
+       if (ae->connected()) {
+               ae->Xrun (); /* EMIT SIGNAL */
+       }
        return 0;
 }
 
 int
 AudioEngine::_graph_order_callback (void *arg)
 {
-        static_cast<AudioEngine *>(arg)->GraphReordered (); /* EMIT SIGNAL */
+       AudioEngine* ae = static_cast<AudioEngine*> (arg);
+       if (ae->connected()) {
+               ae->GraphReordered (); /* EMIT SIGNAL */
+       }
        return 0;
 }
 
 int
-AudioEngine::_process_callback (jack_nframes_t nframes, void *arg)
+AudioEngine::_process_callback (nframes_t nframes, void *arg)
 {
        return static_cast<AudioEngine *> (arg)->process_callback (nframes);
 }
@@ -224,11 +266,11 @@ AudioEngine::_freewheel_callback (int onoff, void *arg)
 }
 
 int
-AudioEngine::process_callback (jack_nframes_t nframes)
+AudioEngine::process_callback (nframes_t nframes)
 {
        // CycleTimer ct ("AudioEngine::process");
        Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
-       jack_nframes_t next_processed_frames;
+       nframes_t next_processed_frames;
        
        /* handle wrap around of total frames counter */
 
@@ -253,7 +295,6 @@ AudioEngine::process_callback (jack_nframes_t nframes)
 
        if (_freewheeling) {
                if (Freewheel (nframes)) {
-                       _freewheeling = false;
                        jack_set_freewheel (_jack, false);
                }
                return 0;
@@ -290,18 +331,35 @@ AudioEngine::process_callback (jack_nframes_t nframes)
                last_monitor_check = next_processed_frames;
        }
 
+       if (session->silent()) {
+
+               boost::shared_ptr<Ports> p = ports.reader();
+
+               for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+                       
+                       Port *port = (*i);
+                       
+                       if (port->sends_output()) {
+                               Sample *buf = port->get_buffer(nframes);
+                               memset (buf, 0, sizeof(Sample) * nframes);
+                               // this should work but doesn't
+                               //port->silence(0, nframes);  //need to implement declicking fade
+                       }
+               }
+       }
+
        _processed_frames = next_processed_frames;
        return 0;
 }
 
 int
-AudioEngine::_sample_rate_callback (jack_nframes_t nframes, void *arg)
+AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
 {
        return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
 }
 
 int
-AudioEngine::jack_sample_rate_callback (jack_nframes_t nframes)
+AudioEngine::jack_sample_rate_callback (nframes_t nframes)
 {
        _frame_rate = nframes;
        _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
@@ -321,13 +379,13 @@ AudioEngine::jack_sample_rate_callback (jack_nframes_t nframes)
 }
 
 int
-AudioEngine::_bufsize_callback (jack_nframes_t nframes, void *arg)
+AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
 {
        return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
 }
 
 int
-AudioEngine::jack_bufsize_callback (jack_nframes_t nframes)
+AudioEngine::jack_bufsize_callback (nframes_t nframes)
 {
        _buffer_size = nframes;
        _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
@@ -346,29 +404,61 @@ AudioEngine::jack_bufsize_callback (jack_nframes_t nframes)
        return 0;
 }
 
+void
+AudioEngine::stop_metering_thread ()
+{
+       if (m_meter_thread) {
+               g_atomic_int_set (&m_meter_exit, 1);
+               m_meter_thread->join ();
+               m_meter_thread = 0;
+       }
+}
+
 void
 AudioEngine::start_metering_thread ()
 {
-    if(m_meter_thread == 0) {
-        m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread), false);
-    }
+       if (m_meter_thread == 0) {
+               g_atomic_int_set (&m_meter_exit, 0);
+               m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
+                                                      500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
+       }
 }
 
 void
 AudioEngine::meter_thread ()
 {
-       while (g_atomic_int_get(&m_meter_exit) != true) {
-        Glib::usleep (10000); /* 1/100th sec interval */
-        IO::update_meters ();
+       while (true) {
+               Glib::usleep (10000); /* 1/100th sec interval */
+               if (g_atomic_int_get(&m_meter_exit)) {
+                       break;
+               }
+               IO::update_meters ();
        }
-       return;
 }
 
 void 
 AudioEngine::set_session (Session *s)
 {
+       Glib::Mutex::Lock pl (_process_lock);
+
        if (!session) {
+
                session = s;
+
+               nframes_t blocksize = jack_get_buffer_size (_jack);
+               
+               /* page in as much of the session process code as we
+                  can before we really start running.
+               */
+
+               session->process (blocksize);
+               session->process (blocksize);
+               session->process (blocksize);
+               session->process (blocksize);
+               session->process (blocksize);
+               session->process (blocksize);
+               session->process (blocksize);
+               session->process (blocksize);
        }
 }
 
@@ -382,18 +472,35 @@ AudioEngine::remove_session ()
                if (session) {
                        session_remove_pending = true;
                        session_removed.wait(_process_lock);
-               } 
+               }
 
        } else {
-
                session = 0;
-
        }
        
        remove_all_ports ();
-
 }
 
+void
+AudioEngine::port_registration_failure (const std::string& portname)
+{
+       string full_portname = jack_client_name;
+       full_portname += ':';
+       full_portname += portname;
+       
+       
+       jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
+       string reason;
+       
+       if (p) {
+               reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
+       } else {
+               reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
+       }
+       
+       throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
+}      
+
 Port *
 AudioEngine::register_input_port (DataType type, const string& portname)
 {
@@ -422,9 +529,7 @@ AudioEngine::register_input_port (DataType type, const string& portname)
                return newport;
 
        } else {
-
-               _process_lock.unlock();
-               throw PortRegistrationFailure();
+               port_registration_failure (portname);
        }
 
        return 0;
@@ -462,9 +567,7 @@ AudioEngine::register_output_port (DataType type, const string& portname)
                return newport;
 
        } else {
-
-               _process_lock.unlock();
-               throw PortRegistrationFailure ();
+               port_registration_failure (portname);
        }
 
        return 0;
@@ -532,6 +635,10 @@ AudioEngine::connect (const string& source, const string& destination)
        if (ret == 0) {
                pair<string,string> c (s, d);
                port_connections.push_back (c);
+       } else if (ret == EEXIST) {
+               error << string_compose(_("AudioEngine: connection already exists: %1 (%2) to %3 (%4)"), 
+                                source, s, destination, d) 
+                     << endmsg;
        } else {
                error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"), 
                                 source, s, destination, d) 
@@ -592,7 +699,7 @@ AudioEngine::disconnect (Port *port)
 
 }
 
-jack_nframes_t
+nframes_t
 AudioEngine::frame_rate ()
 {
        if (_jack) {
@@ -609,7 +716,7 @@ AudioEngine::frame_rate ()
        }
 }
 
-jack_nframes_t
+nframes_t
 AudioEngine::frames_per_cycle ()
 {
        if (_jack) {
@@ -689,19 +796,41 @@ AudioEngine::get_ports (const string& port_name_pattern, const string& type_name
 void
 AudioEngine::halted (void *arg)
 {
-       AudioEngine *ae = reinterpret_cast<AudioEngine *> (arg);
+       AudioEngine* ae = static_cast<AudioEngine *> (arg);
+       bool was_running = ae->_running;
 
-       ae->_running = false;
-       ae->_jack = 0;
+       ae->stop_metering_thread ();
 
+       ae->_running = false;
        ae->_buffer_size = 0;
        ae->_frame_rate = 0;
 
-       ae->Halted(); /* EMIT SIGNAL */
+       if (was_running) {
+               ae->Halted(); /* EMIT SIGNAL */
+       }
+}
+
+bool
+AudioEngine::can_request_hardware_monitoring () 
+{
+       const char ** ports;
+
+       if (!_jack) {
+               return 0;
+       }
+
+       if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
+               return false;
+       }
+
+       free (ports);
+
+       return true;
 }
 
+
 uint32_t
-AudioEngine::n_physical_outputs () const
+AudioEngine::n_physical_audio_outputs () const
 {
        const char ** ports;
        uint32_t i = 0;
@@ -710,19 +839,18 @@ AudioEngine::n_physical_outputs () const
                return 0;
        }
 
-       if ((ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == 0) {
+       if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsInput)) == 0) {
                return 0;
        }
 
-       if (ports) {
-               for (i = 0; ports[i]; ++i);
-               free (ports);
-       }
+       for (i = 0; ports[i]; ++i);
+       free (ports);
+
        return i;
 }
 
 uint32_t
-AudioEngine::n_physical_inputs () const
+AudioEngine::n_physical_audio_inputs () const
 {
        const char ** ports;
        uint32_t i = 0;
@@ -731,7 +859,7 @@ AudioEngine::n_physical_inputs () const
                return 0;
        }
        
-       if ((ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == 0) {
+       if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput)) == 0) {
                return 0;
        }
 
@@ -743,7 +871,7 @@ AudioEngine::n_physical_inputs () const
 }
 
 void
-AudioEngine::get_physical_inputs (vector<string>& ins)
+AudioEngine::get_physical_audio_inputs (vector<string>& ins)
 {
        const char ** ports;
        uint32_t i = 0;
@@ -752,7 +880,7 @@ AudioEngine::get_physical_inputs (vector<string>& ins)
                return;
        }
        
-       if ((ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == 0) {
+       if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput)) == 0) {
                return;
        }
 
@@ -760,13 +888,12 @@ AudioEngine::get_physical_inputs (vector<string>& ins)
                for (i = 0; ports[i]; ++i) {
                        ins.push_back (ports[i]);
                }
-               cerr << "got " << ins.size() << " physical ins\n";
                free (ports);
        }
 }
 
 void
-AudioEngine::get_physical_outputs (vector<string>& outs)
+AudioEngine::get_physical_audio_outputs (vector<string>& outs)
 {
        const char ** ports;
        uint32_t i = 0;
@@ -775,7 +902,7 @@ AudioEngine::get_physical_outputs (vector<string>& outs)
                return;
        }
        
-       if ((ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == 0) {
+       if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsInput)) == 0) {
                return;
        }
 
@@ -783,28 +910,23 @@ AudioEngine::get_physical_outputs (vector<string>& outs)
                for (i = 0; ports[i]; ++i) {
                        outs.push_back (ports[i]);
                }
-               cerr << "got " << outs.size() << " physical outs\n";
                free (ports);
        }
 }
 
 string
-AudioEngine::get_nth_physical (uint32_t n, int flag)
+AudioEngine::get_nth_physical_audio (uint32_t n, int flag)
 {
        const char ** ports;
        uint32_t i;
        string ret;
 
-       if (!_running || !_jack) {
-               if (!_has_run) {
-                       fatal << _("get_nth_physical called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } else {
-                       return "";
-               }
+       if (!_jack) {
+               fatal << _("get_nth_physical called before engine was connected") << endmsg;
+               /*NOTREACHED*/
        }
 
-       ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|flag);
+       ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|flag);
        
        if (ports == 0) {
                return "";
@@ -821,7 +943,7 @@ AudioEngine::get_nth_physical (uint32_t n, int flag)
        return ret;
 }
 
-jack_nframes_t
+nframes_t
 AudioEngine::get_port_total_latency (const Port& port)
 {
        if (!_jack) {
@@ -829,20 +951,12 @@ AudioEngine::get_port_total_latency (const Port& port)
                /*NOTREACHED*/
        }
 
-       if (!_running) {
-               if (!_has_run) {
-                       fatal << _("get_port_total_latency() called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } 
-       }
-
        return jack_port_get_total_latency (_jack, port._port);
 }
 
 void
 AudioEngine::transport_stop ()
 {
-       // cerr << "tell JACK to stop\n";
        if (_jack) {
                jack_transport_stop (_jack);
        }
@@ -851,14 +965,13 @@ AudioEngine::transport_stop ()
 void
 AudioEngine::transport_start ()
 {
-       // cerr << "tell JACK to start\n";
        if (_jack) {
                jack_transport_start (_jack);
        }
 }
 
 void
-AudioEngine::transport_locate (jack_nframes_t where)
+AudioEngine::transport_locate (nframes_t where)
 {
        // cerr << "tell JACK to locate to " << where << endl;
        if (_jack) {
@@ -896,11 +1009,18 @@ AudioEngine::freewheel (bool onoff)
 {
        if (_jack) {
 
-               if (onoff) {
-                       _freewheel_thread_registered = false;
-               }
+               if (onoff != _freewheeling) {
+
+                       if (onoff) {
+                               _freewheel_thread_registered = false;
+                       }
 
-               return jack_set_freewheel (_jack, onoff);
+                       return jack_set_freewheel (_jack, onoff);
+
+               } else {
+                       /* already doing what has been asked for */
+                       return 0;
+               }
 
        } else {
                return -1;
@@ -956,26 +1076,18 @@ AudioEngine::connect_to_jack (string client_name)
        const char *server_name = NULL;
 
        jack_client_name = client_name; /* might be reset below */
-
        _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
-       
-       if (_jack == NULL) {
 
-               if (status & JackServerFailed) {
-                       error << _("Unable to connect to JACK server") << endmsg;
-               }
-               
-               error << string_compose (_("Could not connect to JACK server as  \"%1\""), jack_client_name) <<  endmsg;
+       if (_jack == NULL) {
+               /* just return without an error message. something else will take care of it */
                return -1;
        }
 
-       if (status & JackServerStarted) {
-               info << _("JACK server started") << endmsg;
-       }
-
        if (status & JackNameNotUnique) {
                jack_client_name = jack_get_client_name (_jack);
        }
+
+       jack_set_error_function (ardour_jack_error);
        
        return 0;
 }
@@ -987,7 +1099,7 @@ AudioEngine::connect_to_jack (string client_name)
 {
        jack_client_name = client_name;
 
-       if ((_jack = jack_client_new (client_name.c_str())) == NULL) {
+       if ((_jack = jack_client_new (client_name.c_str())) == 0) {
                return -1;
        }
 
@@ -999,12 +1111,19 @@ AudioEngine::connect_to_jack (string client_name)
 int 
 AudioEngine::disconnect_from_jack ()
 {
-       if (_jack == 0) {
+       if (!_jack) {
                return 0;
        }
 
-       if (jack_client_close (_jack)) {
-               error << _("cannot shutdown connection to JACK") << endmsg;
+
+       if (_running) {
+               stop_metering_thread ();
+       }
+
+       { 
+               Glib::Mutex::Lock lm (_process_lock);
+               jack_client_close (_jack);
+               _jack = 0;
        }
 
        _buffer_size = 0;
@@ -1015,14 +1134,13 @@ AudioEngine::disconnect_from_jack ()
                Stopped(); /* EMIT SIGNAL */
        }
 
-       _jack = 0;
        return 0;
 }
 
 int
 AudioEngine::reconnect_to_jack ()
 {
-       if (_jack) {
+       if (_running) {
                disconnect_from_jack ();
                /* XXX give jackd a chance */
                Glib::usleep (250000);
@@ -1069,7 +1187,8 @@ AudioEngine::reconnect_to_jack ()
 
 
        if (session) {
-               jack_nframes_t blocksize = jack_get_buffer_size (_jack);
+               session->reset_jack_connection (_jack);
+               nframes_t blocksize = jack_get_buffer_size (_jack);
                session->set_block_size (blocksize);
                session->set_frame_rate (jack_get_sample_rate (_jack));
        }
@@ -1088,7 +1207,7 @@ AudioEngine::reconnect_to_jack ()
        
        if (Config->get_jack_time_master()) {
                jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
-       }
+       } 
        
        if (jack_activate (_jack) == 0) {
                _running = true;
@@ -1102,8 +1221,16 @@ AudioEngine::reconnect_to_jack ()
        for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ++i) {
                
                int err;
+               jack_client_t* j = _jack;
+
+               /* JACK could have zombified us. */
+
+               if (!j) {
+                       error << _("Disconnected from JACK while reconnecting. You should quit Ardour now.") << endmsg;
+                       return -1;
+               }
                
-               if ((err = jack_connect (_jack, (*i).first.c_str(), (*i).second.c_str())) != 0) {
+               if ((err = jack_connect (j, (*i).first.c_str(), (*i).second.c_str())) != 0) {
                        if (err != EEXIST) {
                                error << string_compose (_("could not reconnect %1 and %2 (err = %3)"),
                                                  (*i).first, (*i).second, err)
@@ -1114,15 +1241,22 @@ AudioEngine::reconnect_to_jack ()
 
        Running (); /* EMIT SIGNAL*/
 
+       start_metering_thread ();
+
        return 0;
 }
 
 int
-AudioEngine::request_buffer_size (jack_nframes_t nframes)
+AudioEngine::request_buffer_size (nframes_t nframes)
 {
        if (_jack) {
-               int ret = jack_set_buffer_size (_jack, nframes);
-               return ret;
+
+               if (nframes == jack_get_buffer_size (_jack)) {
+                       return 0;
+               }
+
+               return jack_set_buffer_size (_jack, nframes);
+
        } else {
                return -1;
        }
@@ -1132,7 +1266,9 @@ void
 AudioEngine::update_total_latencies ()
 {
 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
-       jack_recompute_total_latencies (_jack);
+       if (_jack) {
+               jack_recompute_total_latencies (_jack);
+       }
 #endif
 }
                
@@ -1173,3 +1309,12 @@ AudioEngine::make_port_name_non_relative (string portname)
        return str;
 }
 
+bool
+AudioEngine::is_realtime () const
+{
+       if (_jack) {
+               return jack_is_realtime (_jack);
+       } else {
+               return false;
+       }
+}