revert VST debug hacks
[ardour.git] / libs / ardour / audioengine.cc
index 4a4659d1df9395124ce49651f6480cce11529162..4ea9cd66f8effc6bb91e3360508e62229236dc54 100644 (file)
@@ -43,8 +43,7 @@ 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;
 
 AudioEngine::AudioEngine (string client_name) 
        : ports (new Ports)
@@ -63,16 +62,16 @@ AudioEngine::AudioEngine (string client_name)
        _buffer_size = 0;
        _freewheeling = false;
        _freewheel_thread_registered = false;
-    
-    m_meter_thread = 0;
-    m_meter_exit = false;
 
-    start_metering_thread();
-    
+       m_meter_thread = 0;
+       g_atomic_int_set (&m_meter_exit, 0);
+
        if (connect_to_jack (client_name)) {
                throw NoBackendAvailable ();
        }
 
+       start_metering_thread();
+
 }
 
 AudioEngine::~AudioEngine ()
@@ -81,9 +80,7 @@ AudioEngine::~AudioEngine ()
                jack_client_close (_jack);
        }
 
-    if(m_meter_thread) {
-        g_atomic_int_inc(&m_meter_exit);
-    }
+       stop_metering_thread ();
 }
 
 void
@@ -102,7 +99,7 @@ AudioEngine::start ()
        if (!_running) {
 
                if (session) {
-                       jack_nframes_t blocksize = jack_get_buffer_size (_jack);
+                       nframes_t blocksize = jack_get_buffer_size (_jack);
 
                        session->set_block_size (blocksize);
                        session->set_frame_rate (jack_get_sample_rate (_jack));
@@ -163,18 +160,37 @@ AudioEngine::stop ()
 }
 
 
-void
-AudioEngine::_jack_timebase_callback (jack_transport_state_t state, jack_nframes_t nframes,
+       
+bool
+AudioEngine::get_sync_offset (nframes_t& offset) const
+{
+
+#ifdef HAVE_JACK_VIDEO_SUPPORT
+
+       jack_position_t pos;
+       
+       (void) jack_transport_query (_jack, &pos);
+
+       if (pos.valid & JackVideoFrameOffset) {
+               offset = pos.video_offset;
+               return true;
+       }
+
+#endif
+
+       return false;
+}
 
-                                                                         jack_position_t* pos, int new_position, void *arg)
+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()) {
                session->jack_timebase_callback (state, nframes, pos, new_position);
@@ -207,12 +223,12 @@ AudioEngine::_xrun_callback (void *arg)
 int
 AudioEngine::_graph_order_callback (void *arg)
 {
-        static_cast<AudioEngine *>(arg)->GraphReordered (); /* EMIT SIGNAL */
+       static_cast<AudioEngine *>(arg)->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 +240,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 */
 
@@ -295,13 +311,13 @@ AudioEngine::process_callback (jack_nframes_t nframes)
 }
 
 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 +337,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,22 +362,31 @@ 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) {
+               m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread), true);
+       }
 }
 
 void
 AudioEngine::meter_thread ()
 {
        while (g_atomic_int_get(&m_meter_exit) != true) {
-        Glib::usleep (10000); /* 1/100th sec interval */
-        IO::update_meters ();
+               Glib::usleep (10000); /* 1/100th sec interval */
+               IO::update_meters ();
        }
-       return;
 }
 
 void 
@@ -532,6 +557,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 +621,7 @@ AudioEngine::disconnect (Port *port)
 
 }
 
-jack_nframes_t
+nframes_t
 AudioEngine::frame_rate ()
 {
        if (_jack) {
@@ -609,7 +638,7 @@ AudioEngine::frame_rate ()
        }
 }
 
-jack_nframes_t
+nframes_t
 AudioEngine::frames_per_cycle ()
 {
        if (_jack) {
@@ -742,6 +771,50 @@ AudioEngine::n_physical_inputs () const
        return i;
 }
 
+void
+AudioEngine::get_physical_inputs (vector<string>& ins)
+{
+       const char ** ports;
+       uint32_t i = 0;
+       
+       if (!_jack) {
+               return;
+       }
+       
+       if ((ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == 0) {
+               return;
+       }
+
+       if (ports) {
+               for (i = 0; ports[i]; ++i) {
+                       ins.push_back (ports[i]);
+               }
+               free (ports);
+       }
+}
+
+void
+AudioEngine::get_physical_outputs (vector<string>& outs)
+{
+       const char ** ports;
+       uint32_t i = 0;
+       
+       if (!_jack) {
+               return;
+       }
+       
+       if ((ports = jack_get_ports (_jack, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == 0) {
+               return;
+       }
+
+       if (ports) {
+               for (i = 0; ports[i]; ++i) {
+                       outs.push_back (ports[i]);
+               }
+               free (ports);
+       }
+}
+
 string
 AudioEngine::get_nth_physical (uint32_t n, int flag)
 {
@@ -775,7 +848,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) {
@@ -812,7 +885,7 @@ AudioEngine::transport_start ()
 }
 
 void
-AudioEngine::transport_locate (jack_nframes_t where)
+AudioEngine::transport_locate (nframes_t where)
 {
        // cerr << "tell JACK to locate to " << where << endl;
        if (_jack) {
@@ -1023,7 +1096,7 @@ AudioEngine::reconnect_to_jack ()
 
 
        if (session) {
-               jack_nframes_t blocksize = jack_get_buffer_size (_jack);
+               nframes_t blocksize = jack_get_buffer_size (_jack);
                session->set_block_size (blocksize);
                session->set_frame_rate (jack_get_sample_rate (_jack));
        }
@@ -1072,7 +1145,7 @@ AudioEngine::reconnect_to_jack ()
 }
 
 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);