rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / ardour / session_butler.cc
index b1451912d061df8480bf6d32f0efedbe690349a9..ec5de23caffbc537f74fb58719a0b15e614a6a92 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>
@@ -30,6 +29,7 @@
 
 #include <pbd/error.h>
 #include <pbd/pthread_utils.h>
+#include <pbd/stacktrace.h>
 
 #include <ardour/configuration.h>
 #include <ardour/audioengine.h>
@@ -69,10 +69,15 @@ int
 Session::start_butler_thread ()
 {
        /* size is in Samples, not bytes */
-
-       dstream_buffer_size = (uint32_t) floor (Config->get_track_buffer_seconds() * (float) frame_rate());
-
-       Crossfade::set_buffer_size (dstream_buffer_size);
+       audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * (float) frame_rate());
+       
+       /* size is in bytes
+        * XXX: Jack needs to tell us the MIDI buffer size
+        * (i.e. how many MIDI bytes we might see in a cycle)
+        */
+       midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * (float)frame_rate());
+       
+       Crossfade::set_buffer_size (audio_dstream_buffer_size);
 
        butler_should_run = false;
 
@@ -104,10 +109,12 @@ Session::start_butler_thread ()
 void
 Session::terminate_butler_thread ()
 {
-       void* status;
-       char c = ButlerRequest::Quit;
-       ::write (butler_request_pipe[1], &c, 1);
-       pthread_join (butler_thread, &status);
+       if (butler_thread) {
+               void* status;
+               char c = ButlerRequest::Quit;
+               ::write (butler_request_pipe[1], &c, 1);
+               pthread_join (butler_thread, &status);
+       }
 }
 
 void
@@ -129,6 +136,7 @@ Session::summon_butler ()
 {
        char c = ButlerRequest::Run;
        ::write (butler_request_pipe[1], &c, 1);
+       // PBD::stacktrace (cerr);
 }
 
 void
@@ -157,8 +165,6 @@ Session::_butler_thread_work (void* arg)
        return 0;
 }
 
-#define transport_work_requested() g_atomic_int_get(&butler_should_do_transport_work)
-
 void *
 Session::butler_thread_work ()
 {
@@ -168,13 +174,8 @@ Session::butler_thread_work ()
        struct timeval begin, end;
        struct pollfd pfd[1];
        bool disk_work_outstanding = false;
-       AudioDiskstreamList::iterator i;
+       DiskstreamList::iterator i;
 
-       butler_mixdown_buffer = new Sample[AudioDiskstream::disk_io_frames()];
-       butler_gain_buffer = new gain_t[AudioDiskstream::disk_io_frames()];
-       // this buffer is used for temp conversion purposes in filesources
-       char * conv_buffer = conversion_buffer(ButlerContext);
-       
        while (true) {
                pfd[0].fd = butler_request_pipe[0];
                pfd[0].events = POLLIN|POLLERR|POLLHUP;
@@ -192,7 +193,7 @@ Session::butler_thread_work ()
                }
 
                if (pfd[0].revents & ~POLLIN) {
-                       error << _("Error on butler thread request pipe") << endmsg;
+                       error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
                        break;
                }
                
@@ -239,10 +240,6 @@ Session::butler_thread_work ()
                        }
                }
 
-               //for (i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
-                       // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
-               //}
-
                if (transport_work_requested()) {
                        butler_transport_work ();
                }
@@ -253,17 +250,30 @@ Session::butler_thread_work ()
 
                gettimeofday (&begin, 0);
 
-               Glib::RWLock::ReaderLock dsm (diskstream_lock);
-               
-               for (i = audio_diskstreams.begin(); !transport_work_requested() && butler_should_run && i != audio_diskstreams.end(); ++i) {
-                       // cerr << "rah fondr " << (*i)->io()->name () << endl;
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
 
-                       switch ((*i)->do_refill (butler_mixdown_buffer, butler_gain_buffer, conv_buffer)) {
+//             for (i = dsl->begin(); i != dsl->end(); ++i) {
+//                     cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
+//             }
+
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
+
+                       boost::shared_ptr<Diskstream> ds = *i;
+
+                       /* don't read inactive tracks */
+
+                       IO* io = ds->io();
+                       
+                       if (io && !io->active()) {
+                               continue;
+                       }
+
+                       switch (ds->do_refill ()) {
                        case 0:
-                               bytes += (*i)->read_data_count();
+                               bytes += ds->read_data_count();
                                break;
                        case 1:
-                               bytes += (*i)->read_data_count();
+                               bytes += ds->read_data_count();
                                disk_work_outstanding = true;
                                break;
                                
@@ -275,7 +285,7 @@ Session::butler_thread_work ()
 
                }
 
-               if (i != audio_diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -297,10 +307,13 @@ Session::butler_thread_work ()
                compute_io = true;
                gettimeofday (&begin, 0);
 
-               for (i = audio_diskstreams.begin(); !transport_work_requested() && butler_should_run && i != audio_diskstreams.end(); ++i) {
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
                        // cerr << "write behind for " << (*i)->name () << endl;
+
+                       /* note that we still try to flush diskstreams attached to inactive routes
+                        */
                        
-                       switch ((*i)->do_flush (conv_buffer)) {
+                       switch ((*i)->do_flush (Session::ButlerContext)) {
                        case 0:
                                bytes += (*i)->write_data_count();
                                break;
@@ -326,7 +339,7 @@ Session::butler_thread_work ()
                        request_stop ();
                }
 
-               if (i != audio_diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -353,7 +366,7 @@ Session::butler_thread_work ()
                        Glib::Mutex::Lock lm (butler_request_lock);
 
                        if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
-//                             for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+//                             for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
 //                                     cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
 //                             }
 
@@ -392,8 +405,8 @@ Session::overwrite_some_buffers (Diskstream* ds)
 
        } else {
 
-               Glib::RWLock::ReaderLock dm (diskstream_lock);
-               for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        (*i)->set_pending_overwrite (true);
                }
        }