when renaming redirects, scan all routes AND sends AND port inserts for the name...
[ardour.git] / libs / ardour / session_state.cc
index c69908285c0f2b6103bcf6f9741e1586fe82eac6..0abe1b652ef22278e3d7b23300ca5536ac748298 100644 (file)
@@ -15,7 +15,6 @@
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     
-  $Id$
 */
 
 #include <algorithm>
 #ifdef HAVE_SYS_VFS_H
 #include <sys/vfs.h>
 #else
-#include <sys/mount.h>
 #include <sys/param.h>
+#include <sys/mount.h>
 #endif
 
 #include <glibmm.h>
+#include <glibmm/thread.h>
 
 #include <midi++/mmc.h>
 #include <midi++/port.h>
-#include <pbd/error.h>
 
-#include <glibmm/thread.h>
+#include <pbd/error.h>
 #include <pbd/pathscanner.h>
 #include <pbd/pthread_utils.h>
 #include <pbd/strsplit.h>
+#include <pbd/stacktrace.h>
+#include <pbd/copyfile.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/configuration.h>
@@ -62,7 +63,8 @@
 #include <ardour/utils.h>
 #include <ardour/audioplaylist.h>
 #include <ardour/audiofilesource.h>
-#include <ardour/destructive_filesource.h>
+#include <ardour/silentfilesource.h>
+#include <ardour/sndfilesource.h>
 #include <ardour/sndfile_helpers.h>
 #include <ardour/auditioner.h>
 #include <ardour/export.h>
@@ -83,6 +85,7 @@
 #include <ardour/control_protocol_manager.h>
 #include <ardour/region_factory.h>
 #include <ardour/source_factory.h>
+#include <ardour/playlist_factory.h>
 
 #include <control_protocol/control_protocol.h>
 
@@ -97,12 +100,14 @@ void
 Session::first_stage_init (string fullpath, string snapshot_name)
 {
        if (fullpath.length() == 0) {
+               destroy ();
                throw failed_constructor();
        }
 
        char buf[PATH_MAX+1];
        if (!realpath (fullpath.c_str(), buf) && (errno != ENOENT)) {
                error << string_compose(_("Could not use path %1 (%s)"), buf, strerror(errno)) << endmsg;
+               destroy ();
                throw failed_constructor();
        }
 
@@ -112,6 +117,17 @@ Session::first_stage_init (string fullpath, string snapshot_name)
                _path += '/';
        }
 
+       if (Glib::file_test (_path, Glib::FILE_TEST_EXISTS) && ::access (_path.c_str(), W_OK)) {
+               cerr << "Session non-writable based on " << _path << endl;
+               _writable = false;
+       } else {
+               cerr << "Session writable based on " << _path << endl;
+               _writable = true;       
+       }
+
+       set_history_depth (Config->get_history_depth());
+       
+
        /* these two are just provisional settings. set_state()
           will likely override them.
        */
@@ -119,30 +135,35 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        _name = _current_snapshot_name = snapshot_name;
 
        _current_frame_rate = _engine.frame_rate ();
+       _nominal_frame_rate = _current_frame_rate;
+       _base_frame_rate = _current_frame_rate;
+
        _tempo_map = new TempoMap (_current_frame_rate);
        _tempo_map->StateChanged.connect (mem_fun (*this, &Session::tempo_map_changed));
 
        g_atomic_int_set (&processing_prohibited, 0);
-       send_cnt = 0;
+       post_transport_work = PostTransportWork (0);
        insert_cnt = 0;
        _transport_speed = 0;
        _last_transport_speed = 0;
+       auto_play_legal = false;
        transport_sub_state = 0;
        _transport_frame = 0;
        last_stop_frame = 0;
+       _requested_return_frame = -1;
        end_location = new Location (0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
        start_location = new Location (0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
        _end_location_is_free = true;
        g_atomic_int_set (&_record_status, Disabled);
        loop_changing = false;
        play_loop = false;
+       have_looped = false;
        _last_roll_location = 0;
        _last_record_location = 0;
        pending_locate_frame = 0;
        pending_locate_roll = false;
        pending_locate_flush = false;
        dstream_buffer_size = 0;
-       state_tree = 0;
        state_was_pending = false;
        set_next_event ();
        outbound_mtc_smpte_frame = 0;
@@ -150,15 +171,15 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        current_block_size = 0;
        solo_update_disabled = false;
        currently_soloing = false;
+       _was_seamless = Config->get_seamless_loop ();
        _have_captured = false;
        _worst_output_latency = 0;
        _worst_input_latency = 0;
        _worst_track_latency = 0;
        _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
+       
        _slave = 0;
-       butler_mixdown_buffer = 0;
-       butler_gain_buffer = 0;
-       mmc = 0;
+       _silent = false;
        session_send_mmc = false;
        session_send_mtc = false;
        post_transport_work = PostTransportWork (0);
@@ -169,23 +190,24 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        g_atomic_int_set (&_playback_load_min, 100);
        g_atomic_int_set (&_capture_load_min, 100);
        _play_range = false;
-       waiting_to_start = false;
        _exporting = false;
        _gain_automation_buffer = 0;
        _pan_automation_buffer = 0;
        _npan_buffers = 0;
        pending_abort = false;
+       pending_clear_substate = false;
        destructive_index = 0;
        current_trans = 0;
        first_file_data_format_reset = true;
        first_file_header_format_reset = true;
+       butler_thread = (pthread_t) 0;
 
        AudioDiskstream::allocate_working_buffers();
        
        /* default short fade = 15ms */
 
        Crossfade::set_short_xfade_length ((nframes_t) floor (Config->get_short_xfade_seconds() * frame_rate()));
-       DestructiveFileSource::setup_standard_crossfades (frame_rate());
+       SndFileSource::setup_standard_crossfades (frame_rate());
 
        last_mmc_step.tv_sec = 0;
        last_mmc_step.tv_usec = 0;
@@ -195,8 +217,6 @@ Session::first_stage_init (string fullpath, string snapshot_name)
           waveforms for clicks.
        */
        
-       click_data = 0;
-       click_emphasis_data = 0;
        click_length = 0;
        click_emphasis_length = 0;
        _clicking = false;
@@ -209,9 +229,6 @@ Session::first_stage_init (string fullpath, string snapshot_name)
                waiting_for_sync_offset = false;
        }
 
-       _current_frame_rate = 48000;
-       _base_frame_rate = 48000;
-
        last_smpte_when = 0;
        _smpte_offset = 0;
        _smpte_offset_negative = true;
@@ -226,7 +243,7 @@ Session::first_stage_init (string fullpath, string snapshot_name)
 
        /* slave stuff */
 
-       average_slave_delta = 1800;
+       average_slave_delta = 1800; // !!! why 1800 ????
        have_first_delta_accumulator = false;
        delta_accumulator_cnt = 0;
        slave_state = Stopped;
@@ -237,7 +254,7 @@ Session::first_stage_init (string fullpath, string snapshot_name)
 
        RegionFactory::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
        SourceFactory::SourceCreated.connect (mem_fun (*this, &Session::add_source));
-       Playlist::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
+       PlaylistFactory::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
        Redirect::RedirectCreated.connect (mem_fun (*this, &Session::add_redirect));
        NamedSelection::NamedSelectionCreated.connect (mem_fun (*this, &Session::add_named_selection));
         AutomationList::AutomationListCreated.connect (mem_fun (*this, &Session::add_automation_list));
@@ -275,6 +292,7 @@ Session::second_stage_init (bool new_session)
 
        // set_state() will call setup_raid_path(), but if it's a new session we need
        // to call setup_raid_path() here.
+
        if (state_tree) {
                if (set_state (*state_tree->root())) {
                        return -1;
@@ -295,7 +313,7 @@ Session::second_stage_init (bool new_session)
 
        _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave|Loading);
 
-       // set_auto_input (true);
+
        _locations.changed.connect (mem_fun (this, &Session::locations_changed));
        _locations.added.connect (mem_fun (this, &Session::locations_added));
        setup_click_sounds (0);
@@ -306,17 +324,32 @@ Session::second_stage_init (bool new_session)
        _engine.Halted.connect (mem_fun (*this, &Session::engine_halted));
        _engine.Xrun.connect (mem_fun (*this, &Session::xrun_recovery));
 
-       if (_engine.running()) {
+       try {
                when_engine_running();
-       } else {
-               first_time_running = _engine.Running.connect (mem_fun (*this, &Session::when_engine_running));
        }
 
+       /* handle this one in a different way than all others, so that its clear what happened */
+
+       catch (AudioEngine::PortRegistrationFailure& err) {
+               destroy ();
+               throw;
+       }
+
+       catch (...) {
+               return -1;
+       }
+
+       BootMessage (_("Reset Remote Controls"));
+
        send_full_time_code ();
        _engine.transport_locate (0);
        deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
        deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
 
+       /* initial program change will be delivered later; see ::config_changed() */
+
+       BootMessage (_("Reset Control Protocols"));
+
        ControlProtocolManager::instance().set_session (*this);
 
        if (new_session) {
@@ -324,7 +357,19 @@ Session::second_stage_init (bool new_session)
        } else {
                _end_location_is_free = false;
        }
+
+       _state_of_the_state = Clean;
+       
+       DirtyChanged (); /* EMIT SIGNAL */
+
+       if (state_was_pending) {
+               save_state (_current_snapshot_name);
+               remove_pending_capture_state ();
+               state_was_pending = false;
+       }
        
+       BootMessage (_("Session loading complete"));
+
        return 0;
 }
 
@@ -372,19 +417,9 @@ Session::setup_raid_path (string path)
                sp.path = path;
                sp.blocks = 0;
                session_dirs.push_back (sp);
-
-               string fspath;
-
                /* sounds dir */
 
-               fspath += sp.path;
-               if (fspath[fspath.length()-1] != '/') {
-                       fspath += '/';
-               }
-               fspath += sound_dir (false);
-               
-               AudioFileSource::set_search_path (fspath);
-
+               AudioFileSource::set_search_path (Glib::build_filename(sp.path, sound_dir (false)));
                return;
        }
 
@@ -398,11 +433,7 @@ Session::setup_raid_path (string path)
 
                /* add sounds to file search path */
 
-               fspath += sp.path;
-               if (fspath[fspath.length()-1] != '/') {
-                       fspath += '/';
-               }
-               fspath += sound_dir (false);
+               fspath += Glib::build_filename(sp.path, sound_dir (false));
                fspath += ':';
 
                remaining = remaining.substr (colon+1);
@@ -414,11 +445,7 @@ Session::setup_raid_path (string path)
                sp.path = remaining;
 
                fspath += ':';
-               fspath += sp.path;
-               if (fspath[fspath.length()-1] != '/') {
-                       fspath += '/';
-               }
-               fspath += sound_dir (false);
+               fspath += Glib::build_filename(sp.path, sound_dir (false));
                fspath += ':';
 
                session_dirs.push_back (sp);
@@ -434,48 +461,70 @@ Session::setup_raid_path (string path)
 }
 
 int
-Session::create (bool& new_session, string* mix_template, nframes_t initial_length)
+Session::ensure_subdirs ()
 {
        string dir;
 
-       if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session dir \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
+       dir = peak_dir ();
+
+       if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
+               error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
                return -1;
        }
 
-       dir = peak_dir ();
+       /* if this is is an existing session with an old "sounds" directory, just use it. see Session::sound_dir() for more details */
+
+       if (!Glib::file_test (old_sound_dir(), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
+
+               dir = sound_dir ();
+               
+               if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
+                       error << string_compose(_("Session: cannot create session sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+                       return -1;
+               }
+       }
+
+       dir = dead_sound_dir ();
 
        if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+               error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
                return -1;
        }
 
-       dir = sound_dir ();
+       dir = export_dir ();
 
        if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+               error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
                return -1;
        }
 
-       dir = dead_sound_dir ();
+       dir = analysis_dir ();
 
        if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session dead sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+               error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
                return -1;
        }
 
-       dir = automation_dir ();
+       return 0;
+}
+
+int
+Session::create (bool& new_session, const string& mix_template, nframes_t initial_length)
+{
 
-       if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session automation dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+       if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
+               error << string_compose(_("Session: cannot create session folder \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
                return -1;
        }
 
+       if (ensure_subdirs ()) {
+               return -1;
+       }
 
        /* check new_session so we don't overwrite an existing one */
 
-       if (mix_template) {
-               std::string in_path = *mix_template;
+       if (!mix_template.empty()) {
+               std::string in_path = mix_template;
 
                ifstream in(in_path.c_str());
 
@@ -519,10 +568,7 @@ Session::create (bool& new_session, string* mix_template, nframes_t initial_leng
 
        _state_of_the_state = Clean;
 
-       if (save_state (_current_snapshot_name)) {
-               save_history (_current_snapshot_name);
-               return -1;
-       }
+       save_state ("");
 
        return 0;
 }
@@ -552,6 +598,14 @@ Session::load_diskstreams (const XMLNode& node)
        return 0;
 }
 
+void
+Session::maybe_write_autosave()
+{
+        if (dirty() && record_status() != Recording) {
+                save_state("", true);
+        }
+}
+
 void
 Session::remove_pending_capture_state ()
 {
@@ -564,6 +618,48 @@ Session::remove_pending_capture_state ()
        unlink (xml_path.c_str());
 }
 
+/** Rename a state file.
+ * @param snapshot_name Snapshot name.
+ */
+void
+Session::rename_state (string old_name, string new_name)
+{
+       if (old_name == _current_snapshot_name || old_name == _name) {
+               /* refuse to rename the current snapshot or the "main" one */
+               return;
+       }
+       
+       const string old_xml_path = _path + old_name + _statefile_suffix;
+       const string new_xml_path = _path + new_name + _statefile_suffix;
+
+       if (rename (old_xml_path.c_str(), new_xml_path.c_str()) != 0) {
+               error << string_compose(_("could not rename snapshot %1 to %2"), old_name, new_name) << endmsg;
+       }
+}
+
+/** Remove a state file.
+ * @param snapshot_name Snapshot name.
+ */
+void
+Session::remove_state (string snapshot_name)
+{
+       if (snapshot_name == _current_snapshot_name || snapshot_name == _name) {
+               /* refuse to remove the current snapshot or the "main" one */
+               return;
+       }
+       
+       const string xml_path = _path + snapshot_name + _statefile_suffix;
+
+       /* make a backup copy of the state file */
+       const string bak_path = xml_path + ".bak";
+       if (g_file_test (xml_path.c_str(), G_FILE_TEST_EXISTS)) {
+               copy_file (xml_path, bak_path);
+       }
+
+       /* and delete it */
+       unlink (xml_path.c_str());
+}
+
 int
 Session::save_state (string snapshot_name, bool pending)
 {
@@ -571,7 +667,13 @@ Session::save_state (string snapshot_name, bool pending)
        string xml_path;
        string bak_path;
 
-       if (_state_of_the_state & CannotSave) {
+       if (!_writable || (_state_of_the_state & CannotSave)) {
+               return 1;
+       }
+
+       if (!_engine.connected ()) {
+               error << _("Ardour's audio engine is not connected and state saving would lose all I/O connections. Session not saved")
+                     << endmsg;
                return 1;
        }
 
@@ -583,56 +685,58 @@ Session::save_state (string snapshot_name, bool pending)
 
        if (!pending) {
 
+               /* proper save: use _statefile_suffix (.ardour in English) */
                xml_path = _path;
                xml_path += snapshot_name;
                xml_path += _statefile_suffix;
+
+               /* make a backup copy of the old file */
                bak_path = xml_path;
                bak_path += ".bak";
                
-               // Make backup of state file
-               
-               if ((access (xml_path.c_str(), F_OK) == 0) &&
-                   (rename(xml_path.c_str(), bak_path.c_str()))) {
-                       error << _("could not backup old state file, current state not saved.") << endmsg;
-                       return -1;
+               if (g_file_test (xml_path.c_str(), G_FILE_TEST_EXISTS)) {
+                       copy_file (xml_path, bak_path);
                }
 
        } else {
 
+               /* pending save: use _pending_suffix (.pending in English) */
                xml_path = _path;
                xml_path += snapshot_name;
                xml_path += _pending_suffix;
 
        }
 
-       cerr << "actually writing state\n";
-
-       if (!tree.write (xml_path)) {
-               error << string_compose (_("state could not be saved to %1"), xml_path) << endmsg;
+       string tmp_path;
 
-               /* don't leave a corrupt file lying around if it is
-                  possible to fix.
-               */
+       tmp_path = _path;
+       tmp_path += snapshot_name;
+       tmp_path += ".tmp";
 
-               if (unlink (xml_path.c_str())) {
-                       error << string_compose (_("could not remove corrupt state file %1"), xml_path) << endmsg;
-               } else {
-                       if (!pending) {
-                               if (rename (bak_path.c_str(), xml_path.c_str())) {
-                                       error << string_compose (_("could not restore state file from backup %1"), bak_path) << endmsg;
-                               }
-                       }
-               }
+       // cerr << "actually writing state to " << xml_path << endl;
 
+       if (!tree.write (tmp_path)) {
+               error << string_compose (_("state could not be saved to %1"), tmp_path) << endmsg;
+               unlink (tmp_path.c_str());
                return -1;
+
+       } else {
+
+               if (rename (tmp_path.c_str(), xml_path.c_str()) != 0) {
+                       error << string_compose (_("could not rename temporary session file %1 to %2"), tmp_path, xml_path) << endmsg;
+                       unlink (tmp_path.c_str());
+                       return -1;
+               }
        }
 
        if (!pending) {
-                save_history(snapshot_name);
+
+                save_history (snapshot_name);
 
                bool was_dirty = dirty();
 
                _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
+
                
                if (was_dirty) {
                        DirtyChanged (); /* EMIT SIGNAL */
@@ -672,7 +776,7 @@ Session::load_state (string snapshot_name)
        xmlpath += snapshot_name;
        xmlpath += _pending_suffix;
 
-       if (!access (xmlpath.c_str(), F_OK)) {
+       if (Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
 
                /* there is pending state from a crashed capture attempt */
 
@@ -687,8 +791,8 @@ Session::load_state (string snapshot_name)
                xmlpath += snapshot_name;
                xmlpath += _statefile_suffix;
        }
-
-       if (access (xmlpath.c_str(), F_OK)) {
+       
+       if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
                error << string_compose(_("%1: session state information file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
                return 1;
        }
@@ -697,15 +801,67 @@ Session::load_state (string snapshot_name)
 
        set_dirty();
 
-       if (state_tree->read (xmlpath)) {
-               return 0;
-       } else {
+       /* writable() really reflects the whole folder, but if for any
+          reason the session state file can't be written to, still
+          make us unwritable.
+       */
+
+       if (::access (xmlpath.c_str(), W_OK) != 0) {
+               _writable = false;
+       }
+
+       if (!state_tree->read (xmlpath)) {
                error << string_compose(_("Could not understand ardour file %1"), xmlpath) << endmsg;
+               delete state_tree;
+               state_tree = 0;
+               return -1;
+       }
+
+       XMLNode& root (*state_tree->root());
+       
+       if (root.name() != X_("Session")) {
+               error << string_compose (_("Session file %1 is not an Ardour session"), xmlpath) << endmsg;
+               delete state_tree;
+               state_tree = 0;
+               return -1;
+       }
+
+       const XMLProperty* prop;
+       bool is_old = false;
+
+       if ((prop = root.property ("version")) == 0) {
+               /* no version implies very old version of Ardour */
+               is_old = true;
+       } else {
+               int major_version;
+               major_version = atoi (prop->value()); // grab just the first number before the period
+               if (major_version < 2) {
+                       is_old = true;
+               }
        }
 
-       delete state_tree;
-       state_tree = 0;
-       return -1;
+       if (is_old) {
+               string backup_path;
+
+               backup_path = _path;
+               backup_path += snapshot_name;
+               backup_path += "-1";
+               backup_path += _statefile_suffix;
+
+               /* don't make another copy if it already exists */
+
+               if (!Glib::file_test (backup_path, Glib::FILE_TEST_EXISTS)) {
+                       info << string_compose (_("Copying old session file %1 to %2\nUse %2 with Ardour versions before 2.0 from now on"),
+                                               xmlpath, backup_path) 
+                            << endmsg;
+                       
+                       copy_file (xmlpath, backup_path);
+                       
+                       /* if it fails, don't worry. right? */
+               }
+       }
+
+       return 0;
 }
 
 int
@@ -717,9 +873,15 @@ Session::load_options (const XMLNode& node)
 
        Config->set_variables (node, ConfigVariableBase::Session);
 
+       /* now reset MIDI ports because the session can have its own 
+          MIDI configuration.
+       */
+
+       setup_midi ();
+
        if ((child = find_named_node (node, "end-marker-is-free")) != 0) {
                if ((prop = child->property ("val")) != 0) {
-                       _end_location_is_free = (prop->value() == "yes");
+                       _end_location_is_free = string_is_affirmative (prop->value());
                }
        }
 
@@ -776,16 +938,16 @@ Session::state(bool full_state)
 
        // store libardour version, just in case
        char buf[16];
-       snprintf(buf, sizeof(buf)-1, "%d.%d.%d", 
-                libardour_major_version, libardour_minor_version, libardour_micro_version);
+       snprintf(buf, sizeof(buf), "%d.%d.%d", libardour2_major_version, libardour2_minor_version, libardour2_micro_version);
        node->add_property("version", string(buf));
                
        /* store configuration settings */
 
        if (full_state) {
        
-               /* store the name */
                node->add_property ("name", _name);
+               snprintf (buf, sizeof (buf), "%" PRId32, _nominal_frame_rate);
+               node->add_property ("sample-rate", buf);
 
                if (session_dirs.size() > 1) {
 
@@ -838,13 +1000,12 @@ Session::state(bool full_state)
                        boost::shared_ptr<AudioFileSource> fs;
 
                        if ((fs = boost::dynamic_pointer_cast<AudioFileSource> (siter->second)) != 0) {
-                               boost::shared_ptr<DestructiveFileSource> dfs = boost::dynamic_pointer_cast<DestructiveFileSource> (fs);
 
                                /* destructive file sources are OK if they are empty, because
                                   we will re-use them every time.
                                */
 
-                               if (!dfs) {
+                               if (!fs->destructive()) {
                                        if (fs->length() == 0) {
                                                continue;
                                        }
@@ -881,7 +1042,20 @@ Session::state(bool full_state)
                }
        }
 
-       node->add_child_nocopy (_locations.get_state());
+       if (full_state) {
+               node->add_child_nocopy (_locations.get_state());
+       } else {
+               // for a template, just create a new Locations, populate it
+               // with the default start and end, and get the state for that.
+               Locations loc;
+               Location* start = new Location(0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
+               Location* end = new Location(0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
+               start->set_end(0);
+               loc.add (start);
+               end->set_end(compute_initial_length());
+               loc.add (end);
+               node->add_child_nocopy (loc.get_state());
+       }
        
        child = node->add_child ("Connections");
        {
@@ -979,19 +1153,7 @@ XMLNode&
 Session::get_control_protocol_state ()
 {
        ControlProtocolManager& cpm (ControlProtocolManager::instance());
-       XMLNode* node = new XMLNode (X_("ControlProtocols"));
-
-       cpm.foreach_known_protocol (bind (mem_fun (*this, &Session::add_control_protocol), node));
-       
-       return *node;
-}
-
-void
-Session::add_control_protocol (const ControlProtocolInfo* const cpi, XMLNode* node)
-{
-       if (cpi->protocol) {
-               node->add_child_nocopy (cpi->protocol->get_state());
-       }
+       return cpm.get_state();
 }
 
 int
@@ -1013,6 +1175,17 @@ Session::set_state (const XMLNode& node)
                _name = prop->value ();
        }
 
+       if ((prop = node.property (X_("sample-rate"))) != 0) {
+
+               _nominal_frame_rate = atoi (prop->value());
+               
+               if (_nominal_frame_rate != _current_frame_rate) {
+                       if (AskAboutSampleRateMismatch (_nominal_frame_rate, _current_frame_rate)) {
+                               throw SRMismatchRejected();
+                       }
+               }
+       }
+
        setup_raid_path(_path);
 
        if ((prop = node.property (X_("id-counter"))) != 0) {
@@ -1035,15 +1208,15 @@ Session::set_state (const XMLNode& node)
 
        /* Object loading order:
 
-       MIDI
        Path
        extra
        Options/Config
+       MIDI  <= relies on data from Options/Config
+       Locations
        Sources
        AudioRegions
        AudioDiskstreams
        Connections
-       Locations
        Routes
        EditGroups
        MixGroups
@@ -1051,9 +1224,6 @@ Session::set_state (const XMLNode& node)
        ControlProtocols
        */
 
-       if (use_config_midi_ports ()) {
-       }
-
        if ((child = find_named_node (node, "extra")) != 0) {
                _extra_xml = new XMLNode (*child);
        }
@@ -1066,6 +1236,42 @@ Session::set_state (const XMLNode& node)
                error << _("Session: XML state has no options section") << endmsg;
        }
 
+       if (use_config_midi_ports ()) {
+       }
+
+       if ((child = find_named_node (node, "Locations")) == 0) {
+               error << _("Session: XML state has no locations section") << endmsg;
+               goto out;
+       } else if (_locations.set_state (*child)) {
+               goto out;
+       }
+
+       Location* location;
+
+       if ((location = _locations.auto_loop_location()) != 0) {
+               set_auto_loop_location (location);
+       }
+
+       if ((location = _locations.auto_punch_location()) != 0) {
+               set_auto_punch_location (location);
+       }
+
+       if ((location = _locations.end_location()) == 0) {
+               _locations.add (end_location);
+       } else {
+               delete end_location;
+               end_location = location;
+       }
+
+       if ((location = _locations.start_location()) == 0) {
+               _locations.add (start_location);
+       } else {
+               delete start_location;
+               start_location = location;
+       }
+
+       AudioFileSource::set_header_position_offset (start_location->start());
+
        if ((child = find_named_node (node, "Sources")) == 0) {
                error << _("Session: XML state has no sources section") << endmsg;
                goto out;
@@ -1113,37 +1319,6 @@ Session::set_state (const XMLNode& node)
                goto out;
        }
 
-       if ((child = find_named_node (node, "Locations")) == 0) {
-               error << _("Session: XML state has no locations section") << endmsg;
-               goto out;
-       } else if (_locations.set_state (*child)) {
-               goto out;
-       }
-
-       Location* location;
-
-       if ((location = _locations.auto_loop_location()) != 0) {
-               set_auto_loop_location (location);
-       }
-
-       if ((location = _locations.auto_punch_location()) != 0) {
-               set_auto_punch_location (location);
-       }
-
-       if ((location = _locations.end_location()) == 0) {
-               _locations.add (end_location);
-       } else {
-               delete end_location;
-               end_location = location;
-       }
-
-       if ((location = _locations.start_location()) == 0) {
-               _locations.add (start_location);
-       } else {
-               delete start_location;
-               start_location = location;
-       }
-
        if ((child = find_named_node (node, "EditGroups")) == 0) {
                error << _("Session: XML state has no edit groups section") << endmsg;
                goto out;
@@ -1186,14 +1361,6 @@ Session::set_state (const XMLNode& node)
 
        StateReady (); /* EMIT SIGNAL */
 
-       _state_of_the_state = Clean;
-
-       if (state_was_pending) {
-               save_state (_current_snapshot_name);
-               remove_pending_capture_state ();
-               state_was_pending = false;
-       }
-
        return 0;
 
   out:
@@ -1212,6 +1379,13 @@ Session::load_routes (const XMLNode& node)
        set_dirty();
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+               XMLProperty* prop = (*niter)->property ("default-type");
+               
+               if (prop && prop->value() == "unknown" ) {
+                       std::cout << "ignoring route with type unknown. (video-track)" << std::endl;
+                       // Note: this may mess up remote_control IDs or more..
+                       continue;
+               }
 
                boost::shared_ptr<Route> route (XMLRouteFactory (**niter));
 
@@ -1220,10 +1394,12 @@ Session::load_routes (const XMLNode& node)
                        return -1;
                }
 
+               BootMessage (string_compose (_("Loaded track/bus %1"), route->name()));
+
                new_routes.push_back (route);
        }
 
-       add_routes (new_routes);
+       add_routes (new_routes, false);
 
        return 0;
 }
@@ -1257,7 +1433,14 @@ Session::load_regions (const XMLNode& node)
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
                if ((region = XMLRegionFactory (**niter, false)) == 0) {
-                       error << _("Session: cannot create Region from XML description.") << endmsg;
+                       error << _("Session: cannot create Region from XML description.");
+                       const XMLProperty *name = (**niter).property("name");
+
+                       if (name) {
+                               error << " " << string_compose (_("Can not load state for region '%1'"), name->value());
+                       }
+
+                       error << endmsg;
                }
        }
 
@@ -1271,6 +1454,7 @@ Session::XMLRegionFactory (const XMLNode& node, bool full)
        boost::shared_ptr<Source> source;
        boost::shared_ptr<AudioSource> as;
        SourceList sources;
+       SourceList master_sources;
        uint32_t nchans = 1;
        char buf[128];
        
@@ -1331,9 +1515,49 @@ Session::XMLRegionFactory (const XMLNode& node, bool full)
                        sources.push_back (as);
                }
        }
-       
+
+       for (uint32_t n=0; n < nchans; ++n) {
+               snprintf (buf, sizeof(buf), X_("master-source-%d"), n);
+               if ((prop = node.property (buf)) != 0) {
+               
+                       PBD::ID id2 (prop->value());
+                       
+                       if ((source = source_by_id (id2)) == 0) {
+                               error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
+                               return boost::shared_ptr<AudioRegion>();
+                       }
+                       
+                       as = boost::dynamic_pointer_cast<AudioSource>(source);
+                       if (!as) {
+                               error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
+                               return boost::shared_ptr<AudioRegion>();
+                       }
+                       master_sources.push_back (as);
+               } 
+       }
+
        try {
                boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, node)));
+
+               /* a final detail: this is the one and only place that we know how long missing files are */
+
+               if (region->whole_file()) {
+                       for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
+                               boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
+                               if (sfp) {
+                                       sfp->set_length (region->length());
+                               }
+                       }
+               }
+
+               if (!master_sources.empty()) {
+                       if (master_sources.size() != nchans) {
+                               error << _("Session: XMLNode describing an AudioRegion is missing some master sources; ignored") << endmsg;
+                       } else {
+                               region->set_master_sources (master_sources);
+                       }
+               }
+
                return region;
                                                       
        }
@@ -1373,11 +1597,16 @@ Session::path_from_region_name (string name, string identifier)
                } else {
                        snprintf (buf, sizeof(buf), "%s/%s-%" PRIu32 ".wav", dir.c_str(), name.c_str(), n);
                }
-               if (access (buf, F_OK) != 0) {
+
+               if (!Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
                        return buf;
                }
        }
 
+       error << string_compose (_("cannot create new file from region name \"%1\" with ident = \"%2\": too many existing files with similar names"),
+                                name, identifier)
+             << endmsg;
+
        return "";
 }
        
@@ -1395,8 +1624,15 @@ Session::load_sources (const XMLNode& node)
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
 
-               if ((source = XMLSourceFactory (**niter)) == 0) {
-                       error << _("Session: cannot create Source from XML description.") << endmsg;
+               try {
+                       if ((source = XMLSourceFactory (**niter)) == 0) {
+                               error << _("Session: cannot create Source from XML description.") << endmsg;
+                       }
+               }
+
+               catch (non_existent_source& err) {
+                       warning << _("A sound file is missing. It will be replaced by silence.") << endmsg;
+                       source = SourceFactory::createSilent (*this, **niter, max_frames, _current_frame_rate);
                }
        }
 
@@ -1411,9 +1647,10 @@ Session::XMLSourceFactory (const XMLNode& node)
        }
 
        try {
-               return SourceFactory::create (*this, node);
+               /* note: do peak building in another thread when loading session state */
+               return SourceFactory::create (*this, node, true);
        }
-       
+
        catch (failed_constructor& err) {
                error << _("Found a sound file that cannot be used by Ardour. Talk to the progammers.") << endmsg;
                return boost::shared_ptr<Source>();
@@ -1444,9 +1681,7 @@ Session::save_template (string template_name)
 
        tree.set_root (&get_template());
 
-       xml_path = dir;
-       xml_path += template_name;
-       xml_path += _template_suffix;
+       xml_path = Glib::build_filename(dir, template_name + _template_suffix);
 
        ifstream in(xml_path.c_str());
        
@@ -1468,8 +1703,8 @@ Session::save_template (string template_name)
 int
 Session::rename_template (string old_name, string new_name) 
 {
-       string old_path = template_dir() + old_name + _template_suffix;
-       string new_path = template_dir() + new_name + _template_suffix;
+       string old_path = Glib::build_filename(template_dir(), old_name + _template_suffix);
+       string new_path = Glib::build_filename(template_dir(), new_name + _template_suffix);
 
        return rename (old_path.c_str(), new_path.c_str());
 }
@@ -1477,9 +1712,7 @@ Session::rename_template (string old_name, string new_name)
 int
 Session::delete_template (string name) 
 {
-       string template_path = template_dir();
-       template_path += name;
-       template_path += _template_suffix;
+       string template_path = Glib::build_filename(template_dir(), name + _template_suffix);
 
        return remove (template_path.c_str());
 }
@@ -1523,27 +1756,21 @@ Session::ensure_sound_dir (string path, string& result)
        
        /* Ensure that the sounds directory exists */
        
-       result = path;
-       result += '/';
-       result += sound_dir_name;
+       result = Glib::build_filename(path, sound_dir_name);
        
        if (g_mkdir_with_parents (result.c_str(), 0775)) {
                error << string_compose(_("cannot create sounds directory \"%1\"; ignored"), result) << endmsg;
                return -1;
        }
 
-       dead = path;
-       dead += '/';
-       dead += dead_sound_dir_name;
+       dead = Glib::build_filename(path, dead_sound_dir_name);
        
        if (g_mkdir_with_parents (dead.c_str(), 0775)) {
                error << string_compose(_("cannot create dead sounds directory \"%1\"; ignored"), dead) << endmsg;
                return -1;
        }
 
-       peak = path;
-       peak += '/';
-       peak += peak_dir_name;
+       peak = Glib::build_filename(path, peak_dir_name);
        
        if (g_mkdir_with_parents (peak.c_str(), 0775)) {
                error << string_compose(_("cannot create peak file directory \"%1\"; ignored"), peak) << endmsg;
@@ -1669,7 +1896,7 @@ Session::load_playlists (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
 
        nlist = node.children();
 
@@ -1690,7 +1917,7 @@ Session::load_unused_playlists (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
 
        nlist = node.children();
 
@@ -1705,22 +1932,22 @@ Session::load_unused_playlists (const XMLNode& node)
 
                // now manually untrack it
 
-               track_playlist (playlist, false);
+               track_playlist (false, boost::weak_ptr<Playlist> (playlist));
        }
 
        return 0;
 }
 
 
-Playlist *
+boost::shared_ptr<Playlist>
 Session::XMLPlaylistFactory (const XMLNode& node)
 {
        try {
-               return new AudioPlaylist (*this, node);
+               return PlaylistFactory::create (*this, node);
        }
 
        catch (failed_constructor& err) {
-               return 0;
+               return boost::shared_ptr<Playlist>();
        }
 }
 
@@ -1762,16 +1989,56 @@ Session::dead_sound_dir () const
 {
        string res = _path;
        res += dead_sound_dir_name;
-       res += '/';
+
+       return res;
+}
+
+string
+Session::old_sound_dir (bool with_path) const
+{
+       string res;
+
+       if (with_path) {
+               res = _path;
+       }
+
+       res += old_sound_dir_name;
+
        return res;
 }
 
 string
 Session::sound_dir (bool with_path) const
 {
-       /* support old session structure */
+       string res;
+       string full;
+       vector<string> parts;
+
+       if (with_path) {
+               res = _path;
+       } else {
+               full = _path;
+       }
+
+       parts.push_back(interchange_dir_name);
+       parts.push_back(legalize_for_path (_name));
+       parts.push_back(sound_dir_name);
+
+       res += Glib::build_filename(parts);
+       if (with_path) {
+               full = res;
+       } else {
+               full += res;
+       }
+       
+       /* if this already exists, don't check for the old session sound directory */
+
+       if (Glib::file_test (full, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
+               return res;
+       }
+               
+       /* possibly support old session structure */
 
-       struct stat statbuf;
        string old_nopath;
        string old_withpath;
 
@@ -1780,27 +2047,15 @@ Session::sound_dir (bool with_path) const
        
        old_withpath = _path;
        old_withpath += old_sound_dir_name;
-       old_withpath += '/';
-
-       if (stat (old_withpath.c_str(), &statbuf) == 0) {
+       
+       if (Glib::file_test (old_withpath.c_str(), Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
                if (with_path)
                        return old_withpath;
                
                return old_nopath;
        }
-
-       string res;
-
-       if (with_path) {
-               res = _path;
-       }
-
-       res += interchange_dir_name;
-       res += '/';
-       res += legalize_for_path (_name);
-       res += '/';
-       res += sound_dir_name;
-       res += '/';
+       
+       /* ok, old "sounds" directory isn't there, return the new path */
 
        return res;
 }
@@ -1808,27 +2063,37 @@ Session::sound_dir (bool with_path) const
 string
 Session::peak_dir () const
 {
-       string res = _path;
-       res += peak_dir_name;
-       res += '/';
-       return res;
+       return Glib::build_filename (_path, peak_dir_name);
 }
        
 string
 Session::automation_dir () const
 {
-       string res = _path;
-       res += "automation/";
-       return res;
+       return Glib::build_filename (_path, "automation");
+}
+
+string
+Session::analysis_dir () const
+{
+       return Glib::build_filename (_path, "analysis");
 }
 
 string
 Session::template_dir ()
 {
-       string path = get_user_ardour_path();
-       path += "templates/";
+       return Glib::build_filename (get_user_ardour_path(), "templates");
+}
 
-       return path;
+string
+Session::route_template_dir ()
+{
+       return Glib::build_filename (get_user_ardour_path(), "route_templates");
+}
+
+string
+Session::export_dir () const
+{
+       return Glib::build_filename (_path, export_dir_name);
 }
 
 string
@@ -1871,9 +2136,20 @@ Session::template_path ()
        return suffixed_search_path (X_("templates"), true);
 }
 
+
+string
+Session::route_template_path ()
+{
+       return suffixed_search_path (X_("route_templates"), true);
+}
+
 string
 Session::control_protocol_path ()
 {
+       char *p = getenv ("ARDOUR_CONTROL_SURFACE_PATH");
+       if (p && *p) {
+               return p;
+       }
        return suffixed_search_path (X_("surfaces"), false);
 }
 
@@ -2081,10 +2357,14 @@ Session::commit_reversible_command (Command *cmd)
                current_trans->add_command (cmd);
        }
 
+       if (current_trans->empty()) {
+               return;
+       }
+
        gettimeofday (&now, 0);
        current_trans->set_timestamp (now);
 
-       history.add (current_trans);
+       _history.add (current_trans);
 }
 
 Session::GlobalRouteBooleanState 
@@ -2132,7 +2412,12 @@ void
 Session::set_global_route_metering (GlobalRouteMeterState s, void* arg) 
 {
        for (GlobalRouteMeterState::iterator i = s.begin(); i != s.end(); ++i) {
-               i->first->set_meter_point (i->second, arg);
+
+               boost::shared_ptr<Route> r = (i->first.lock());
+
+               if (r) {
+                       r->set_meter_point (i->second, arg);
+               }
        }
 }
 
@@ -2140,8 +2425,13 @@ void
 Session::set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void* arg)
 {
        for (GlobalRouteBooleanState::iterator i = s.begin(); i != s.end(); ++i) {
-               Route* r = i->first.get();
-               (r->*method) (i->second, arg);
+
+               boost::shared_ptr<Route> r = (i->first.lock());
+
+               if (r) {
+                       Route* rp = r.get();
+                       (rp->*method) (i->second, arg);
+               }
        }
 }
 
@@ -2221,11 +2511,47 @@ Session::get_template_list (list<string> &template_names)
        }
 }
 
+void
+Session::get_route_templates (vector<RouteTemplateInfo>& template_names)
+{
+       vector<string *> *templates;
+       PathScanner scanner;
+       string path;
+
+       path = route_template_path ();
+       
+       templates = scanner (path, template_filter, 0, false, true);
+       
+       if (!templates) {
+         return;
+       }
+
+       for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
+               string fullpath = *(*i);
+
+               XMLTree tree;
+
+               if (!tree.read (fullpath.c_str())) {
+                 continue;
+               }
+
+               XMLNode* root = tree.root();
+               
+               RouteTemplateInfo rti;
+
+               rti.name = IO::name_from_state (*root->children().front());
+               rti.path = fullpath;
+
+               template_names.push_back (rti);
+       }
+
+       delete templates;
+}
+
 int
 Session::read_favorite_dirs (FavoriteDirs & favs)
 {
-       string path = get_user_ardour_path();
-       path += "/favorite_dirs";
+       Glib::ustring path = Glib::build_filename (get_user_ardour_path(), "favorite_dirs");
 
        ifstream fav (path.c_str());
 
@@ -2259,8 +2585,7 @@ Session::read_favorite_dirs (FavoriteDirs & favs)
 int
 Session::write_favorite_dirs (FavoriteDirs & favs)
 {
-       string path = get_user_ardour_path();
-       path += "/favorite_dirs";
+       Glib::ustring path = Glib::build_filename (get_user_ardour_path(), "favorite_dirs");
 
        ofstream fav (path.c_str());
 
@@ -2321,12 +2646,17 @@ Session::find_all_sources (string path, set<string>& result)
                        continue;
                }
 
-               string path = _path; /* /-terminated */
-               path += sound_dir_name;
-               path += '/';
-               path += prop->value();
+               /* now we have to actually find the file */
 
-               result.insert (path);
+               bool is_new;
+               uint16_t chan;
+               Glib::ustring path;
+               std::string name;
+               
+               if (AudioFileSource::find (prop->value(), true, false, is_new, chan, path, name)) {
+                       cerr << "Got " << path << " from XML source with prop = " << prop->value() << endl;
+                       result.insert (path);
+               }
        }
 
        return 0;
@@ -2373,11 +2703,20 @@ Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_th
        return 0;
 }
 
+struct RegionCounter {
+    typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
+    AudioSourceList::iterator iter;
+    boost::shared_ptr<Region> region;
+    uint32_t count;
+    
+    RegionCounter() : count (0) {}
+};
+
 int
 Session::cleanup_sources (Session::cleanup_report& rep)
 {
        vector<boost::shared_ptr<Source> > dead_sources;
-       vector<Playlist*> playlists_tbd;
+       vector<boost::shared_ptr<Playlist> > playlists_tbd;
        PathScanner scanner;
        string sound_path;
        vector<space_and_path>::iterator i;
@@ -2390,6 +2729,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
        int ret = -1;
                
        _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
+
        
        /* step 1: consider deleting all unused playlists */
 
@@ -2416,72 +2756,36 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
        /* now delete any that were marked for deletion */
 
-       for (vector<Playlist*>::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
-               PlaylistList::iterator foo;
-
-               if ((foo = unused_playlists.find (*x)) != unused_playlists.end()) {
-                       unused_playlists.erase (foo);
-               }
-               delete *x;
+       for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
+               (*x)->drop_references ();
        }
 
-       /* step 2: find all un-referenced sources */
+       playlists_tbd.clear ();
+
+       /* step 2: find all un-used sources */
 
        rep.paths.clear ();
        rep.space = 0;
 
        for (AudioSourceList::iterator i = audio_sources.begin(); i != audio_sources.end(); ) {
-
+               
                AudioSourceList::iterator tmp;
 
                tmp = i;
                ++tmp;
 
-               /* only remove files that are not in use and have some size
-                  to them. otherwise we remove the current "nascent"
+               /* do not bother with files that are zero size, otherwise we remove the current "nascent"
                   capture files.
                */
 
-               if (i->second.use_count() == 1 && i->second->length() > 0) {
+               if (!i->second->used() && i->second->length() > 0) {
                        dead_sources.push_back (i->second);
-
-                       /* remove this source from our own list to avoid us
-                          adding it to the list of all sources below
-                       */
-
-                       audio_sources.erase (i);
-               }
+                       i->second->GoingAway();
+               } 
 
                i = tmp;
        }
 
-       /* Step 3: get rid of all regions in the region list that use any dead sources
-          in case the sources themselves don't go away (they might be referenced in
-          other snapshots).
-       */
-               
-       for (vector<boost::shared_ptr<Source> >::iterator i = dead_sources.begin(); i != dead_sources.end();++i) {
-
-               for (AudioRegionList::iterator r = audio_regions.begin(); r != audio_regions.end(); ) {
-                       AudioRegionList::iterator tmp;
-                       boost::shared_ptr<AudioRegion> ar;
-
-                       tmp = r;
-                       ++tmp;
-                       
-                       ar = r->second;
-
-                       for (uint32_t n = 0; n < ar->n_channels(); ++n) {
-                               if (ar->source (n) == (*i)) {
-                                       /* this region is dead */
-                                       remove_region (ar);
-                               }
-                       }
-                       
-                       r = tmp;
-               }
-       }
-
        /* build a list of all the possible sound directories for the session */
 
        for (i = session_dirs.begin(); i != session_dirs.end(); ) {
@@ -2490,7 +2794,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                ++nexti;
 
                sound_path += (*i).path;
-               sound_path += sound_dir_name;
+               sound_path += sound_dir (false);
 
                if (nexti != session_dirs.end()) {
                        sound_path += ':';
@@ -2498,7 +2802,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
                i = nexti;
        }
-       
+
        /* now do the same thing for the files that ended up in the sounds dir(s) 
           but are not referenced as sources in any snapshot.
        */
@@ -2527,6 +2831,9 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                } 
        }
 
+       char tmppath1[PATH_MAX+1];
+       char tmppath2[PATH_MAX+1];
+       
        for (vector<string*>::iterator x = soundfiles->begin(); x != soundfiles->end(); ++x) {
 
                used = false;
@@ -2534,11 +2841,15 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
                for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
 
-                       if (spath == *i) {
+                       realpath(spath.c_str(), tmppath1);
+                       realpath((*i).c_str(),  tmppath2);
+
+                       cerr << "comparing " << tmppath1 << " and " << tmppath2 << endl;
+
+                       if (strcmp(tmppath1, tmppath2) == 0) {
                                used = true;
                                break;
                        }
-
                }
 
                if (!used) {
@@ -2563,11 +2874,31 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                   on whichever filesystem it was already on.
                */
 
-               newpath = Glib::path_get_dirname (*x);
-               newpath = Glib::path_get_dirname (newpath);
+               if ((*x).find ("/sounds/") != string::npos) {
+
+                       /* old school, go up 1 level */
+
+                       newpath = Glib::path_get_dirname (*x);      // "sounds" 
+                       newpath = Glib::path_get_dirname (newpath); // "session-name"
+
+               } else {
+
+                       /* new school, go up 4 levels */
+                       
+                       newpath = Glib::path_get_dirname (*x);      // "audiofiles" 
+                       newpath = Glib::path_get_dirname (newpath); // "session-name"
+                       newpath = Glib::path_get_dirname (newpath); // "interchange"
+                       newpath = Glib::path_get_dirname (newpath); // "session-dir"
+               }
 
                newpath += '/';
                newpath += dead_sound_dir_name;
+
+               if (g_mkdir_with_parents (newpath.c_str(), 0755) < 0) {
+                       error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), newpath, strerror (errno)) << endmsg;
+                       return -1;
+               }
+
                newpath += '/';
                newpath += Glib::path_get_basename ((*x));
                
@@ -2607,7 +2938,6 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                              << endmsg;
                        goto out;
                }
-               
 
                /* see if there an easy to find peakfile for this file, and remove it.
                 */
@@ -2625,14 +2955,13 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                                goto out;
                        }
                }
-
        }
 
        ret = 0;
 
        /* dump the history list */
 
-       history.clear ();
+       _history.clear ();
 
        /* save state so we don't end up a session file
           referring to non-existent sources.
@@ -2642,6 +2971,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
   out:
        _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
+
        return ret;
 }
 
@@ -2713,6 +3043,7 @@ Session::set_dirty ()
 
        _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
 
+
        if (!was_dirty) {
                DirtyChanged(); /* EMIT SIGNAL */
        }
@@ -2726,14 +3057,28 @@ Session::set_clean ()
        
        _state_of_the_state = Clean;
 
+
        if (was_dirty) {
                DirtyChanged(); /* EMIT SIGNAL */
        }
 }
 
+void
+Session::set_deletion_in_progress ()
+{
+       _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
+
+}
+
 void
 Session::add_controllable (Controllable* c)
 {
+       /* this adds a controllable to the list managed by the Session.
+          this is a subset of those managed by the Controllable class
+          itself, and represents the only ones whose state will be saved
+          as part of the session.
+       */
+
        Glib::Mutex::Lock lm (controllables_lock);
        controllables.insert (c);
 }
@@ -2771,19 +3116,22 @@ Session::controllable_by_id (const PBD::ID& id)
 void 
 Session::add_instant_xml (XMLNode& node, const std::string& dir)
 {
-       Stateful::add_instant_xml (node, dir);
+       if (_writable) {
+               Stateful::add_instant_xml (node, dir);
+       }
        Config->add_instant_xml (node, get_user_ardour_path());
 }
 
-
 int 
 Session::save_history (string snapshot_name)
 {
     XMLTree tree;
     string xml_path;
     string bak_path;
-    
-    tree.set_root (&history.get_state());
+
+    if (!_writable) {
+           return 0;
+    }
 
     if (snapshot_name.empty()) {
        snapshot_name = _current_snapshot_name;
@@ -2793,14 +3141,16 @@ Session::save_history (string snapshot_name)
 
     bak_path = xml_path + ".bak";
 
-    if ((access (xml_path.c_str(), F_OK) == 0) &&
-        (rename (xml_path.c_str(), bak_path.c_str())))
-    {
+    if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS) && ::rename (xml_path.c_str(), bak_path.c_str())) {
         error << _("could not backup old history file, current history not saved.") << endmsg;
         return -1;
     }
 
-    cerr << "actually writing history\n";
+    if (!Config->get_save_history() || Config->get_saved_history_depth() < 0) {
+           return 0;
+    }
+
+    tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
 
     if (!tree.write (xml_path))
     {
@@ -2810,14 +3160,13 @@ Session::save_history (string snapshot_name)
          * possible to fix.
          */
 
-        if (unlink (xml_path.c_str())) 
-        {
-            error << string_compose (_("could not remove corrupt history file %1"), xml_path) << endmsg;
+        if (unlink (xml_path.c_str())) {
+               error << string_compose (_("could not remove corrupt history file %1"), xml_path) << endmsg;
         } else {
-            if (rename (bak_path.c_str(), xml_path.c_str())) 
-            {
-                error << string_compose (_("could not restore history file from backup %1"), bak_path) << endmsg;
-            }
+               if (rename (bak_path.c_str(), xml_path.c_str())) 
+               {
+                       error << string_compose (_("could not restore history file from backup %1"), bak_path) << endmsg;
+               }
         }
 
         return -1;
@@ -2832,22 +3181,25 @@ Session::restore_history (string snapshot_name)
     XMLTree tree;
     string xmlpath;
 
+    if (snapshot_name.empty()) {
+           snapshot_name = _current_snapshot_name;
+    }
+
     /* read xml */
     xmlpath = _path + snapshot_name + ".history";
-    cerr << string_compose(_("Loading history from '%1'."), xmlpath) << endmsg;
+    info << string_compose(_("Loading history from '%1'."), xmlpath) << endmsg;
 
-    if (access (xmlpath.c_str(), F_OK)) {
-        error << string_compose(_("%1: session history file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
-        return 1;
+    if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
+           return 1;
     }
 
     if (!tree.read (xmlpath)) {
-        error << string_compose(_("Could not understand ardour file %1"), xmlpath) << endmsg;
-        return -1;
+           error << string_compose (_("Could not understand session history file \"%1\""), xmlpath) << endmsg;
+           return -1;
     }
 
     /* replace history */
-    history.clear();
+    _history.clear();
 
     for (XMLNodeConstIterator it  = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
            
@@ -2872,15 +3224,24 @@ Session::restore_history (string snapshot_name)
                    if (n->name() == "MementoCommand" ||
                        n->name() == "MementoUndoCommand" ||
                        n->name() == "MementoRedoCommand") {
+
                            if ((c = memento_command_factory(n))) {
                                    ut->add_command(c);
                            }
+                           
+                   } else if (n->name() == X_("GlobalRouteStateCommand")) {
+
+                           if ((c = global_state_command_factory (*n))) {
+                                   ut->add_command (c);
+                           }
+                           
                    } else {
+
                            error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
                    }
            }
 
-           history.add (ut);
+           _history.add (ut);
     }
 
     return 0;
@@ -2906,7 +3267,6 @@ Session::config_changed (const char* parameter_name)
                        
                        for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                                if ((*i)->record_enabled ()) {
-                                       //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
                                        (*i)->monitor_input (!Config->get_auto_input());
                                }
                        }
@@ -2948,16 +3308,20 @@ Session::config_changed (const char* parameter_name)
 
        } else if (PARAM_IS ("use-video-sync")) {
 
-               if (transport_stopped()) {
-                       if (Config->get_use_video_sync()) {
-                               waiting_for_sync_offset = true;
-                       }
-               }
+               waiting_for_sync_offset = Config->get_use_video_sync();
 
        } else if (PARAM_IS ("mmc-control")) {
 
                poke_midi_thread ();
 
+       } else if (PARAM_IS ("mmc-device-id") || PARAM_IS ("mmc-receive-device-id")) {
+
+               set_mmc_receive_device_id (Config->get_mmc_receive_device_id());
+
+       } else if (PARAM_IS ("mmc-send-device-id")) {
+
+               set_mmc_send_device_id (Config->get_mmc_send_device_id());
+
        } else if (PARAM_IS ("midi-control")) {
                
                poke_midi_thread ();
@@ -2966,7 +3330,7 @@ Session::config_changed (const char* parameter_name)
 
                setup_raid_path (Config->get_raid_path());
 
-       } else if (PARAM_IS ("smpte-frames-per-second") || PARAM_IS ("smpte-drop-frames")) {
+       } else if (PARAM_IS ("smpte-format")) {
 
                sync_time_vars ();
 
@@ -3012,6 +3376,12 @@ Session::config_changed (const char* parameter_name)
                
                if (_mtc_port != 0) {
                        session_send_mtc = Config->get_send_mtc();
+                       if (session_send_mtc) {
+                               /* mark us ready to send */
+                               next_quarter_frame_to_send = 0;
+                       }
+               } else {
+                       session_send_mtc = false;
                }
 
        } else if (PARAM_IS ("send-mmc")) {
@@ -3022,6 +3392,9 @@ Session::config_changed (const char* parameter_name)
                
                if (_mmc_port != 0) {
                        session_send_mmc = Config->get_send_mmc();
+               } else {
+                       mmc = 0;
+                       session_send_mmc = false; 
                }
 
        } else if (PARAM_IS ("midi-feedback")) {
@@ -3053,10 +3426,38 @@ Session::config_changed (const char* parameter_name)
                }
 
                first_file_data_format_reset = false;
+
+       } else if (PARAM_IS ("slave-source")) {
+               set_slave_source (Config->get_slave_source());
+       } else if (PARAM_IS ("remote-model")) {
+               set_remote_control_ids ();
+       } else if (PARAM_IS ("denormal-model")) {
+               setup_fpu ();
+       } else if (PARAM_IS ("history-depth")) {
+               set_history_depth (Config->get_history_depth());
+       } else if (PARAM_IS ("sync-all-route-ordering")) {
+               sync_order_keys ("session"); 
+       } else if (PARAM_IS ("initial-program-change")) {
+
+               if (_mmc_port && Config->get_initial_program_change() >= 0) {
+                       MIDI::byte* buf = new MIDI::byte[2];
+                       
+                       buf[0] = MIDI::program; // channel zero by default
+                       buf[1] = (Config->get_initial_program_change() & 0x7f);
+                       deliver_midi (_mmc_port, buf, 2);
+               }
+       } else if (PARAM_IS ("solo-mute-override")) {
+               catch_up_on_solo_mute_override ();
        }
-               
+
        set_dirty ();
                   
 #undef PARAM_IS
 
 }
+
+void
+Session::set_history_depth (uint32_t d)
+{
+       _history.set_depth (d);
+}