provide more accurate diagnostics from AudioEngine::port_registration_failure() and...
[ardour.git] / libs / ardour / audioengine.cc
index 10f9978f967741306028f25d250017e0aff3e2cc..cc74b92a6942c464aec4596d237bf61f28e58c96 100644 (file)
@@ -20,6 +20,7 @@
 #include <unistd.h>
 #include <cerrno>
 #include <vector>
+#include <sstream>
 
 #include <glibmm/timer.h>
 #include <pbd/pthread_utils.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>
 
@@ -48,7 +46,8 @@ gint AudioEngine::m_meter_exit;
 static void 
 ardour_jack_error (const char* msg) 
 {
-       error << "JACK: " << msg << endmsg;
+       // throw JACK errors away - they cause visual clutter
+       // error << "JACK: " << msg << endmsg;
 }
 
 AudioEngine::AudioEngine (string client_name) 
@@ -61,13 +60,12 @@ 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;
+       _freewheeling = false;
 
        m_meter_thread = 0;
        g_atomic_int_set (&m_meter_exit, 0);
@@ -75,9 +73,6 @@ AudioEngine::AudioEngine (string client_name)
        if (connect_to_jack (client_name)) {
                throw NoBackendAvailable ();
        }
-
-       start_metering_thread();
-
 }
 
 AudioEngine::~AudioEngine ()
@@ -88,6 +83,7 @@ AudioEngine::~AudioEngine ()
                
                if (_running) {
                        jack_client_close (_jack);
+                       _jack = 0;
                }
                
                stop_metering_thread ();
@@ -101,17 +97,24 @@ _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) {
                        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));
 
@@ -153,6 +156,8 @@ AudioEngine::start ()
                } else {
                        // error << _("cannot activate JACK client") << endmsg;
                }
+
+               start_metering_thread();
        }
 
        return _running ? 0 : -1;
@@ -161,17 +166,13 @@ AudioEngine::start ()
 int
 AudioEngine::stop (bool forever)
 {
-       if (_running) {
-               _running = false;
+       if (_jack) {
                if (forever) {
-                       jack_client_t* foo = _jack;
-                       _jack = 0;
-                       jack_client_close (foo);
-                       stop_metering_thread ();
+                       disconnect_from_jack ();
                } else {
                        jack_deactivate (_jack);
+                       Stopped(); /* EMIT SIGNAL */
                }
-               Stopped(); /* EMIT SIGNAL */
        }
 
        return _running ? -1 : 0;
@@ -186,11 +187,13 @@ AudioEngine::get_sync_offset (nframes_t& offset) const
 
        jack_position_t pos;
        
-       (void) jack_transport_query (_jack, &pos);
-
-       if (pos.valid & JackVideoFrameOffset) {
-               offset = pos.video_offset;
-               return true;
+       if (_jack) {
+               (void) jack_transport_query (_jack, &pos);
+               
+               if (pos.valid & JackVideoFrameOffset) {
+                       offset = pos.video_offset;
+                       return true;
+               }
        }
 
 #endif
@@ -234,7 +237,7 @@ int
 AudioEngine::_xrun_callback (void *arg)
 {
        AudioEngine* ae = static_cast<AudioEngine*> (arg);
-       if (ae->jack()) {
+       if (ae->connected()) {
                ae->Xrun (); /* EMIT SIGNAL */
        }
        return 0;
@@ -244,7 +247,7 @@ int
 AudioEngine::_graph_order_callback (void *arg)
 {
        AudioEngine* ae = static_cast<AudioEngine*> (arg);
-       if (ae->jack()) {
+       if (ae->connected()) {
                ae->GraphReordered (); /* EMIT SIGNAL */
        }
        return 0;
@@ -292,8 +295,6 @@ AudioEngine::process_callback (nframes_t nframes)
 
        if (_freewheeling) {
                if (Freewheel (nframes)) {
-                       cerr << "Freewheeling returned non-zero!\n";
-                       _freewheeling = false;
                        jack_set_freewheel (_jack, false);
                }
                return 0;
@@ -330,6 +331,23 @@ AudioEngine::process_callback (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;
 }
@@ -400,8 +418,9 @@ void
 AudioEngine::start_metering_thread ()
 {
        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);
+                                                      500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
        }
 }
 
@@ -431,7 +450,7 @@ AudioEngine::set_session (Session *s)
                /* 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);
@@ -462,6 +481,26 @@ AudioEngine::remove_session ()
        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)
 {
@@ -490,7 +529,7 @@ AudioEngine::register_input_port (DataType type, const string& portname)
                return newport;
 
        } else {
-               throw PortRegistrationFailure();
+               port_registration_failure (portname);
        }
 
        return 0;
@@ -528,7 +567,7 @@ AudioEngine::register_output_port (DataType type, const string& portname)
                return newport;
 
        } else {
-               throw PortRegistrationFailure ();
+               port_registration_failure (portname);
        }
 
        return 0;
@@ -758,17 +797,40 @@ void
 AudioEngine::halted (void *arg)
 {
        AudioEngine* ae = static_cast<AudioEngine *> (arg);
+       bool was_running = ae->_running;
+
+       ae->stop_metering_thread ();
 
        ae->_running = false;
        ae->_buffer_size = 0;
        ae->_frame_rate = 0;
-       ae->_jack = 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;
@@ -777,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;
@@ -798,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;
        }
 
@@ -810,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;
@@ -819,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;
        }
 
@@ -832,7 +893,7 @@ AudioEngine::get_physical_inputs (vector<string>& ins)
 }
 
 void
-AudioEngine::get_physical_outputs (vector<string>& outs)
+AudioEngine::get_physical_audio_outputs (vector<string>& outs)
 {
        const char ** ports;
        uint32_t i = 0;
@@ -841,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;
        }
 
@@ -854,22 +915,18 @@ AudioEngine::get_physical_outputs (vector<string>& outs)
 }
 
 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 "";
@@ -894,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);
        }
@@ -916,7 +965,6 @@ AudioEngine::transport_stop ()
 void
 AudioEngine::transport_start ()
 {
-       // cerr << "tell JACK to start\n";
        if (_jack) {
                jack_transport_start (_jack);
        }
@@ -961,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;
@@ -1021,9 +1076,8 @@ 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) {
                /* just return without an error message. something else will take care of it */
                return -1;
@@ -1045,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;
        }
 
@@ -1057,11 +1111,20 @@ AudioEngine::connect_to_jack (string client_name)
 int 
 AudioEngine::disconnect_from_jack ()
 {
-       if (_jack == 0) {
+       if (!_jack) {
                return 0;
        }
 
-       jack_client_close (_jack);
+
+       if (_running) {
+               stop_metering_thread ();
+       }
+
+       { 
+               Glib::Mutex::Lock lm (_process_lock);
+               jack_client_close (_jack);
+               _jack = 0;
+       }
 
        _buffer_size = 0;
        _frame_rate = 0;
@@ -1071,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);
@@ -1145,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;
@@ -1159,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)
@@ -1171,6 +1241,8 @@ AudioEngine::reconnect_to_jack ()
 
        Running (); /* EMIT SIGNAL*/
 
+       start_metering_thread ();
+
        return 0;
 }
 
@@ -1194,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
 }