Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[ardour.git] / libs / ardour / session_butler.cc
index f0a2af0a83d146cdf72c16b884805020c78036c0..148ff9273993cf50fbf9dd7b6d358ccdc1c6d013 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>
@@ -104,10 +103,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
@@ -157,8 +158,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 ()
 {
@@ -170,11 +169,6 @@ Session::butler_thread_work ()
        bool disk_work_outstanding = false;
        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 +186,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 +233,6 @@ Session::butler_thread_work ()
                        }
                }
 
-               //for (i = diskstreams.begin(); i != 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,19 +243,31 @@ Session::butler_thread_work ()
 
                gettimeofday (&begin, 0);
 
-               Glib::RWLock::ReaderLock dsm (diskstream_lock);
-               
-               for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
-                       // cerr << "rah fondr " << (*i)->io()->name () << endl;
-                       AudioDiskstream* ads = dynamic_cast<AudioDiskstream*>(*i);
-                       if (!ads) continue; // FIXME
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
+
+//             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) {
 
-                       switch (ads->do_refill (butler_mixdown_buffer, butler_gain_buffer, conv_buffer)) {
+                       boost::shared_ptr<Diskstream> ds = *i;
+
+                       /* don't read inactive tracks */
+
+                       /*IO* io = ds->io();
+                       
+                       if (ds->io() && !ds->io()->active()) {
+                               cerr << "Skip inactive diskstream " << ds->io()->name() << endl;
+                               continue;
+                       }*/
+
+                       switch (ds->do_refill ()) {
                        case 0:
-                               bytes += ads->read_data_count();
+                               bytes += ds->read_data_count();
                                break;
                        case 1:
-                               bytes += ads->read_data_count();
+                               bytes += ds->read_data_count();
                                disk_work_outstanding = true;
                                break;
                                
@@ -277,7 +279,7 @@ Session::butler_thread_work ()
 
                }
 
-               if (i != diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -299,10 +301,13 @@ Session::butler_thread_work ()
                compute_io = true;
                gettimeofday (&begin, 0);
 
-               for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != 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;
@@ -328,7 +333,7 @@ Session::butler_thread_work ()
                        request_stop ();
                }
 
-               if (i != diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -355,7 +360,7 @@ Session::butler_thread_work ()
                        Glib::Mutex::Lock lm (butler_request_lock);
 
                        if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
-//                             for (DiskstreamList::iterator i = diskstreams.begin(); i != 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;
 //                             }
 
@@ -394,8 +399,8 @@ Session::overwrite_some_buffers (Diskstream* ds)
 
        } else {
 
-               Glib::RWLock::ReaderLock dm (diskstream_lock);
-               for (DiskstreamList::iterator i = diskstreams.begin(); i != 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);
                }
        }