more MTC debugging
[ardour.git] / libs / ardour / mtc_slave.cc
index 95fa4d984aa32dd4444d6f48050e18d8a78bc95d..8cc54d577d6d3dec533f041314ccfff35279d1c9 100644 (file)
@@ -21,7 +21,9 @@
 #include <poll.h>
 #include <sys/types.h>
 #include <unistd.h>
+
 #include "pbd/error.h"
+#include "pbd/stacktrace.h"
 #include "pbd/enumwriter.h"
 #include "pbd/failed_constructor.h"
 #include "pbd/pthread_utils.h"
@@ -37,7 +39,6 @@
 
 using namespace std;
 using namespace ARDOUR;
-using namespace sigc;
 using namespace MIDI;
 using namespace PBD;
 
@@ -74,6 +75,7 @@ MTC_Slave::~MTC_Slave()
        if (did_reset_tc_format) {
                session.config.set_timecode_format (saved_tc_format);
        }
+
        delete pic;
        delete [] speed_accumulator;
 }
@@ -88,20 +90,19 @@ MTC_Slave::give_slave_full_control_over_transport_speed() const
 void
 MTC_Slave::rebind (MIDI::Port& p)
 {
-       for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
-               (*i).disconnect ();
-       }
-
+       port_connections.drop_connections ();
+       
        port = &p;
-
-       connections.push_back (port->input()->mtc_time.connect (mem_fun (*this, &MTC_Slave::update_mtc_time)));
-       connections.push_back (port->input()->mtc_qtr.connect (mem_fun (*this, &MTC_Slave::update_mtc_qtr)));
-       connections.push_back (port->input()->mtc_status.connect (mem_fun (*this, &MTC_Slave::update_mtc_status)));
+       
+       port->input()->mtc_time.connect_same_thread (port_connections,  boost::bind (&MTC_Slave::update_mtc_time, this, _1, _2, _3));
+       port->input()->mtc_qtr.connect_same_thread (port_connections, boost::bind (&MTC_Slave::update_mtc_qtr, this, _1, _2, _3));
+       port->input()->mtc_status.connect_same_thread (port_connections, boost::bind (&MTC_Slave::update_mtc_status, this, _1));
 }
 
 void
 MTC_Slave::update_mtc_qtr (Parser& /*p*/, int which_qtr, nframes_t now)
 {
+       DEBUG_TRACE (DEBUG::MTC, string_compose ("update MTC qtr does a reset, qtr = %1 now = %2\n", which_qtr, now));
        maybe_reset ();
 
        DEBUG_TRACE (DEBUG::MTC, string_compose ("qtr frame %1 at %2\n", which_qtr, now));
@@ -117,6 +118,7 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
        */
 
        if (now) {
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("update MTC time does a reset, was full ? %1 now = %2\n", was_full, now));
                maybe_reset ();
        }
 
@@ -182,15 +184,18 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
 
        DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC time timestamp = %1 TC %2 = frame %3 (from full message ? %4)\n", 
                                                 now, timecode, mtc_frame, was_full));
-
+       
        if (was_full || outside_window (mtc_frame)) {
 
                session.timecode_to_sample (timecode, mtc_frame, true, false);
                session.request_locate (mtc_frame, false);
                session.request_transport_speed (0);
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("reset MTC status to stopped, outside MTC window (%1 .. %2 vs. %3)\n",
+                                                        window_begin, window_end, mtc_frame));
                update_mtc_status (MIDI::MTC_Stopped);
-               reset_window (mtc_frame);
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("outside, so window root reset to %1\n", mtc_frame));
                reset ();
+               reset_window (mtc_frame);
 
        } else {
                        
@@ -280,6 +285,7 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
        }
 
        if (window_root >= 0) {
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("window root reset to %1\n", window_root));
                reset_window (window_root);
        }
 }
@@ -476,6 +482,8 @@ MTC_Slave::resolution() const
 void
 MTC_Slave::queue_reset ()
 {
+       DEBUG_TRACE (DEBUG::MTC, "queue reset of MTC slave state\n");
+       PBD::stacktrace (cerr, 35);
        Glib::Mutex::Lock lm (reset_lock);
        reset_pending++;
 }
@@ -486,6 +494,7 @@ MTC_Slave::maybe_reset ()
        reset_lock.lock ();
 
        if (reset_pending) {
+               DEBUG_TRACE (DEBUG::MTC, "actually reset\n");
                reset ();
                reset_pending = 0;
        } 
@@ -496,6 +505,8 @@ MTC_Slave::maybe_reset ()
 void
 MTC_Slave::reset ()
 {
+       DEBUG_TRACE (DEBUG::MTC, "*****************\n\n\n MTC SLAVE reset ********************\n\n\n");
+       PBD::stacktrace (cerr, 35);
        port->input()->reset_mtc_state ();
 
        last_inbound_frame = 0;
@@ -526,18 +537,21 @@ MTC_Slave::reset_window (nframes64_t root)
           ahead of the window root (taking direction into account).
        */
 
+       DEBUG_TRACE (DEBUG::MTC, string_compose ("trying to reset MTC window with state = %1\n", enum_2_string (port->input()->mtc_running())));
+
        switch (port->input()->mtc_running()) {
        case MTC_Forward:
+               DEBUG_TRACE (DEBUG::MTC, "set MTC window while running forward\n");
                window_begin = root;
                if (session.slave_state() == Session::Running) {
                        window_end = root + (session.frames_per_timecode_frame() * frame_tolerance);
                } else {
                        window_end = root + seekahead_distance ();
                }
-               DEBUG_TRACE (DEBUG::MTC, string_compose ("legal MTC window now %1 .. %2\n", window_begin, window_end));
                break;
 
        case MTC_Backward:
+               DEBUG_TRACE (DEBUG::MTC, "set MTC window while running backward\n");
                if (session.slave_state() == Session::Running) {
                        nframes_t d = session.frames_per_timecode_frame() * frame_tolerance;
                        if (root > d) {
@@ -555,13 +569,15 @@ MTC_Slave::reset_window (nframes64_t root)
                        }
                }
                window_end = root;
-               DEBUG_TRACE (DEBUG::MTC, string_compose ("legal MTC window now %1 .. %2\n", window_begin, window_end));
                break;
                
        default:
+               DEBUG_TRACE (DEBUG::MTC, "not touching MTC window - MTC_Stopped\n");
                /* do nothing */
                break;
        }
+
+       DEBUG_TRACE (DEBUG::MTC, string_compose ("legal MTC window now %1 .. %2\n", window_begin, window_end));
 }
 
 nframes64_t