Merged with trunk R1705.
[ardour.git] / libs / ardour / session_butler.cc
index d29bf3937b67ed967cf7dcc248cf62a4f73acf9b..afb284b0f49a4ab549eee18002894b8929c11e2f 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>
@@ -42,7 +41,7 @@
 
 using namespace std;
 using namespace ARDOUR;
-//using namespace sigc;
+using namespace PBD;
 
 static float _read_data_rate;
 static float _write_data_rate;
@@ -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 ()
 {
@@ -168,15 +167,9 @@ Session::butler_thread_work ()
        struct timeval begin, end;
        struct pollfd pfd[1];
        bool disk_work_outstanding = false;
-       AudioDiskstreamList::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);
+       DiskstreamList::iterator i;
 
        while (true) {
-
                pfd[0].fd = butler_request_pipe[0];
                pfd[0].events = POLLIN|POLLERR|POLLHUP;
                
@@ -193,19 +186,18 @@ 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;
                }
                
                if (pfd[0].revents & POLLIN) {
-                       
+
                        char req;
                        
                        /* empty the pipe of all current requests */
                        
                        while (1) {
                                size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
-                               
                                if (nread == 1) {
                                        
                                        switch ((ButlerRequest::Type) req) {
@@ -240,10 +232,10 @@ Session::butler_thread_work ()
                                }
                        }
                }
-       
-               for (i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+
+               //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 ();
@@ -255,18 +247,18 @@ 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 ();
+
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
+
+                       boost::shared_ptr<Diskstream> ds = *i;
 
-                       switch ((*i)->do_refill (butler_mixdown_buffer, butler_gain_buffer, conv_buffer)) {
+                       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;
                                
@@ -278,7 +270,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;
                }
@@ -299,12 +291,11 @@ Session::butler_thread_work ()
                bytes = 0;
                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;
                        
-                       switch ((*i)->do_flush (conv_buffer)) {
+                       switch ((*i)->do_flush (Session::ButlerContext)) {
                        case 0:
                                bytes += (*i)->write_data_count();
                                break;
@@ -330,7 +321,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;
                }
@@ -357,7 +348,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;
 //                             }
 
@@ -375,18 +366,17 @@ Session::butler_thread_work ()
 
 
 void
-Session::request_overwrite_buffer (AudioDiskstream* stream)
+Session::request_overwrite_buffer (Diskstream* stream)
 {
        Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
        ev->set_ptr (stream);
        queue_event (ev);
 }
 
+/** Process thread. */
 void
-Session::overwrite_some_buffers (AudioDiskstream* ds)
+Session::overwrite_some_buffers (Diskstream* ds)
 {
-       /* executed by the audio thread */
-
        if (actively_recording()) {
                return;
        }
@@ -397,8 +387,8 @@ Session::overwrite_some_buffers (AudioDiskstream* 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);
                }
        }