cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[ardour.git] / libs / ardour / audio_diskstream.cc
index 599e5ea1f99858374f8e49dc2c7729236a1cb366..fb225671e9ba8198ea1a8bb557803c2946d0a7c8 100644 (file)
 #include "pbd/enumwriter.h"
 #include "pbd/stacktrace.h"
 
-#include "ardour/ardour.h"
-#include "ardour/audioengine.h"
 #include "ardour/analyser.h"
+#include "ardour/ardour.h"
+#include "ardour/audio_buffer.h"
 #include "ardour/audio_diskstream.h"
-#include "ardour/utils.h"
-#include "ardour/configuration.h"
+#include "ardour/audio_port.h"
+#include "ardour/audioengine.h"
 #include "ardour/audiofilesource.h"
-#include "ardour/send.h"
-#include "ardour/region_factory.h"
+
 #include "ardour/audioplaylist.h"
-#include "ardour/playlist_factory.h"
-#include "ardour/cycle_timer.h"
 #include "ardour/audioregion.h"
-#include "ardour/audio_port.h"
-#include "ardour/source_factory.h"
-#include "ardour/audio_buffer.h"
-#include "ardour/session.h"
+#include "ardour/butler.h"
+#include "ardour/configuration.h"
+#include "ardour/cycle_timer.h"
+#include "ardour/debug.h"
 #include "ardour/io.h"
+#include "ardour/playlist_factory.h"
+#include "ardour/region_factory.h"
+#include "ardour/send.h"
+#include "ardour/session.h"
+#include "ardour/source_factory.h"
+#include "ardour/utils.h"
+#include "ardour/session_playlists.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -121,7 +125,7 @@ AudioDiskstream::init (Diskstream::Flag f)
 
 AudioDiskstream::~AudioDiskstream ()
 {
-       notify_callbacks ();
+       DEBUG_TRACE (DEBUG::Destruction, string_compose ("Audio Diskstream %1 destructor\n", _name));
 
        {
                RCUWriter<ChannelList> writer (channels);
@@ -253,7 +257,7 @@ AudioDiskstream::find_and_use_playlist (const string& name)
 {
        boost::shared_ptr<AudioPlaylist> playlist;
 
-       if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlist_by_name (name))) == 0) {
+       if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlists->by_name (name))) == 0) {
                playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, name));
        }
 
@@ -625,6 +629,7 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can
                                nframes_t total = chaninfo->playback_vector.len[0] + chaninfo->playback_vector.len[1];
 
                                if (necessary_samples > total) {
+                                       cerr << _name << " Need " << necessary_samples << " total = " << total << endl;
                                        cerr << "underrun for " << _name << endl;
                                        DiskUnderrun ();
                                        goto out;
@@ -675,19 +680,19 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can
 void
 AudioDiskstream::process_varispeed_playback(nframes_t nframes, boost::shared_ptr<ChannelList> c)
 {
-         ChannelList::iterator chan;
+       ChannelList::iterator chan;
 
-               interpolation.set_speed (_target_speed);
+       interpolation.set_speed (_target_speed);
 
-               int channel = 0;
-               for (chan = c->begin(); chan != c->end(); ++chan, ++channel) {
-                       ChannelInfo* chaninfo (*chan);
+       int channel = 0;
+       for (chan = c->begin(); chan != c->end(); ++chan, ++channel) {
+               ChannelInfo* chaninfo (*chan);
 
-                       playback_distance = interpolation.interpolate (
-                                       channel, nframes, chaninfo->current_playback_buffer, chaninfo->speed_buffer);
+               playback_distance = interpolation.interpolate (
+                               channel, nframes, chaninfo->current_playback_buffer, chaninfo->speed_buffer);
 
-                       chaninfo->current_playback_buffer = chaninfo->speed_buffer;
-               }
+               chaninfo->current_playback_buffer = chaninfo->speed_buffer;
+       }
 }
 
 bool
@@ -1500,7 +1505,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                                continue; /* XXX is this OK? */
                        }
 
-                       region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
+                       region->DropReferences.connect_same_thread (*this, boost::bind (&Diskstream::remove_region_from_last_capture, this, boost::weak_ptr<Region>(region)));
 
                        _last_capture_regions.push_back (region);
 
@@ -1646,15 +1651,6 @@ AudioDiskstream::set_record_enabled (bool yn)
                return;
        }
 
-       if (yn && channels.reader()->front()->source == 0) {
-
-               /* pick up connections not initiated *from* the IO object
-                  we're associated with.
-               */
-
-               get_input_sources ();
-       }
-
        /* yes, i know that this not proof against race conditions, but its
           good enough. i think.
        */
@@ -1753,7 +1749,7 @@ AudioDiskstream::get_state ()
                if (_session.config.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
                        snprintf (buf, sizeof (buf), "%" PRId64, pi->start());
                } else {
-                       snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
+                       snprintf (buf, sizeof (buf), "%" PRId64, _session.transport_frame());
                }
 
                cs_child->add_property (X_("at"), buf);
@@ -1768,7 +1764,7 @@ AudioDiskstream::get_state ()
 }
 
 int
-AudioDiskstream::set_state (const XMLNode& node, int version)
+AudioDiskstream::set_state (const XMLNode& node, int /*version*/)
 {
        const XMLProperty* prop;
        XMLNodeList nlist = node.children();
@@ -1913,6 +1909,10 @@ AudioDiskstream::use_new_write_source (uint32_t n)
        /* do not remove destructive files even if they are empty */
 
        chan->write_source->set_allow_remove_if_empty (!destructive());
+       
+       /* until we write, this file is considered removable */
+
+       chan->write_source->mark_for_remove ();
 
        return 0;
 }
@@ -1999,7 +1999,7 @@ AudioDiskstream::allocate_temporary_buffers ()
 {
        /* make sure the wrap buffer is at least large enough to deal
           with the speeds up to 1.2, to allow for micro-variation
-          when slaving to MTC, SMPTE etc.
+          when slaving to MTC, Timecode etc.
        */
 
        double sp = max (fabsf (_actual_speed), 1.2f);
@@ -2065,10 +2065,9 @@ AudioDiskstream::set_align_style_from_io ()
 int
 AudioDiskstream::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
 {
-
        while (how_many--) {
-               c->push_back (new ChannelInfo(_session.audio_diskstream_buffer_size(), speed_buffer_size, wrap_buffer_size));
-               interpolation.add_channel_to (_session.audio_diskstream_buffer_size(), speed_buffer_size);
+               c->push_back (new ChannelInfo(_session.butler()->audio_diskstream_buffer_size(), speed_buffer_size, wrap_buffer_size));
+               interpolation.add_channel_to (_session.butler()->audio_diskstream_buffer_size(), speed_buffer_size);
        }
 
        _n_channels.set(DataType::AUDIO, c->size());
@@ -2163,8 +2162,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                        try {
                                fs = boost::dynamic_pointer_cast<AudioFileSource> (
                                                SourceFactory::createWritable (DataType::AUDIO, _session,
-                                                               prop->value(), true,
-                                                               false, _session.frame_rate()));
+                                                               prop->value(), false, _session.frame_rate()));
                        }
 
                        catch (failed_constructor& err) {
@@ -2305,7 +2303,7 @@ AudioDiskstream::can_become_destructive (bool& requires_bounce) const
 
        assert (afirst);
 
-       if (afirst->source()->used() > 1) {
+       if (_session.playlists->source_use_count (afirst->source()) > 1) {
                requires_bounce = true;
                return false;
        }