a better fix for CUE/TOC string escaping: if the text is not Latin-1 already, reject...
[ardour.git] / libs / ardour / butler.cc
index 2fd4c1d9c1f313f515a567ff6f13db2d0bd5dba6..72b9e5f586b4eb587b3429f6295b256784335209 100644 (file)
 #include "ardour/io.h"
 #include "ardour/midi_diskstream.h"
 #include "ardour/session.h"
+#include "ardour/track.h"
+#include "ardour/auditioner.h"
 
 #include "i18n.h"
 
 using namespace PBD;
 
-static float _read_data_rate;
-static float _write_data_rate;
-
 namespace ARDOUR {
 
 Butler::Butler(Session& s)
        : SessionHandleRef (s)
        , thread(0)
-       , audio_dstream_buffer_size(0)
+       , audio_dstream_capture_buffer_size(0)
+       , audio_dstream_playback_buffer_size(0)
        , midi_dstream_buffer_size(0)
+       , pool_trash(16)
 {
        g_atomic_int_set(&should_do_transport_work, 0);
+       SessionEvent::pool->set_trash (&pool_trash);
+
+        Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
 }
 
 Butler::~Butler()
 {
+       terminate_thread ();
+}
+
+void
+Butler::config_changed (std::string p)
+{
+        if (p == "playback-buffer-seconds") {
+                /* size is in Samples, not bytes */
+                audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
+                _session.adjust_playback_buffering ();
+        } else if (p == "capture-buffer-seconds") {
+                audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
+                _session.adjust_capture_buffering ();
+        }
 }
 
 int
@@ -57,7 +75,8 @@ Butler::start_thread()
        const float rate = (float)_session.frame_rate();
 
        /* size is in Samples, not bytes */
-       audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
+       audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
+       audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
 
        /* size is in bytes
         * XXX: Jack needs to tell us the MIDI buffer size
@@ -65,9 +84,9 @@ Butler::start_thread()
         */
        midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
 
-       MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
+       MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
 
-       Crossfade::set_buffer_size (audio_dstream_buffer_size);
+       Crossfade::set_buffer_size (audio_dstream_playback_buffer_size);
 
        should_run = false;
 
@@ -105,7 +124,7 @@ Butler::terminate_thread ()
        if (thread) {
                void* status;
                const char c = Request::Quit;
-               ::write (request_pipe[1], &c, 1);
+               (void) ::write (request_pipe[1], &c, 1);
                pthread_join (thread, &status);
        }
 }
@@ -113,7 +132,8 @@ Butler::terminate_thread ()
 void *
 Butler::_thread_work (void* arg)
 {
-       SessionEvent::create_per_thread_pool ("butler events", 64);
+       SessionEvent::create_per_thread_pool ("butler events", 4096);
+       pthread_set_name (X_("butler"));
        return ((Butler *) arg)->thread_work ();
 }
 
@@ -121,13 +141,10 @@ void *
 Butler::thread_work ()
 {
        uint32_t err = 0;
-       int32_t bytes;
-       bool compute_io;
-       microseconds_t begin, end;
 
        struct pollfd pfd[1];
        bool disk_work_outstanding = false;
-       Session::DiskstreamList::iterator i;
+       RouteList::iterator i;
 
        while (true) {
                pfd[0].fd = request_pipe[0];
@@ -162,9 +179,6 @@ Butler::thread_work ()
 
                                        switch ((Request::Type) req) {
 
-                                       case Request::Wake:
-                                               break;
-
                                        case Request::Run:
                                                should_run = true;
                                                break;
@@ -193,91 +207,84 @@ Butler::thread_work ()
                        }
                }
 
+
+restart:
+               disk_work_outstanding = false;
+
                if (transport_work_requested()) {
                        _session.butler_transport_work ();
                }
 
-               disk_work_outstanding = false;
-               bytes = 0;
-               compute_io = true;
-
-               begin = get_microseconds();
+               boost::shared_ptr<RouteList> rl = _session.get_routes();
 
-               boost::shared_ptr<Session::DiskstreamList> dsl = _session.diskstream_list().reader ();
+               RouteList rl_with_auditioner = *rl;
+               rl_with_auditioner.push_back (_session.the_auditioner());
 
 //             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() && should_run && i != dsl->end(); ++i) {
+               for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
 
-                       boost::shared_ptr<Diskstream> ds = *i;
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
 
-                       /* don't read inactive tracks */
+                       if (!tr) {
+                               continue;
+                       }
 
-                       boost::shared_ptr<IO> io = ds->io();
+                       boost::shared_ptr<IO> io = tr->input ();
 
                        if (io && !io->active()) {
+                               /* don't read inactive tracks */
                                continue;
                        }
 
-                       switch (ds->do_refill ()) {
+                       switch (tr->do_refill ()) {
                        case 0:
-                               bytes += ds->read_data_count();
                                break;
+                               
                        case 1:
-                               bytes += ds->read_data_count();
                                disk_work_outstanding = true;
                                break;
 
                        default:
-                               compute_io = false;
                                error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
                                break;
                        }
 
                }
 
-               if (i != dsl->end()) {
+               if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
 
                if (!err && transport_work_requested()) {
-                       continue;
+                       goto restart;
                }
 
-               if (compute_io) {
-                       end = get_microseconds();
-                       if (end - begin > 0) {
-                               _read_data_rate = (float) bytes / (float) (end - begin);
-                       } else {
-                               _read_data_rate = 0; // infinity better
-                       }
-               }
+               for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
+                       // cerr << "write behind for " << (*i)->name () << endl;
 
-               bytes = 0;
-               compute_io = true;
-               begin = get_microseconds();
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
 
-               for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
-                       // cerr << "write behind for " << (*i)->name () << endl;
+                       if (!tr) {
+                               continue;
+                       }
 
                        /* note that we still try to flush diskstreams attached to inactive routes
                         */
 
-                       switch ((*i)->do_flush (ButlerContext)) {
+                       switch (tr->do_flush (ButlerContext)) {
                        case 0:
-                               bytes += (*i)->write_data_count();
                                break;
+                               
                        case 1:
-                               bytes += (*i)->write_data_count();
                                disk_work_outstanding = true;
                                break;
 
                        default:
                                err++;
-                               compute_io = false;
                                error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
                                /* don't break - try to flush all streams in case they
                                   are split across disks.
@@ -292,23 +299,13 @@ Butler::thread_work ()
                        _session.request_stop ();
                }
 
-               if (i != dsl->end()) {
+               if (i != rl->begin() && i != rl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
 
                if (!err && transport_work_requested()) {
-                       continue;
-               }
-
-               if (compute_io) {
-                       // there are no apparent users for this calculation?
-                       end = get_microseconds();
-                       if (end - begin > 0) {
-                               _write_data_rate = (float) bytes / (float) (end - begin);
-                       } else {
-                               _write_data_rate = 0; // Well, infinity would be better
-                       }
+                       goto restart;
                }
 
                if (!disk_work_outstanding) {
@@ -324,11 +321,13 @@ Butler::thread_work ()
 //                                     cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
 //                             }
 
-                               continue;
+                               goto restart;
                        }
 
                        paused.signal();
                }
+
+               empty_pool_trash ();
        }
 
        pthread_exit_pbd (0);
@@ -347,7 +346,7 @@ void
 Butler::summon ()
 {
        char c = Request::Run;
-       ::write (request_pipe[1], &c, 1);
+       (void) ::write (request_pipe[1], &c, 1);
 }
 
 void
@@ -355,7 +354,7 @@ Butler::stop ()
 {
        Glib::Mutex::Lock lm (request_lock);
        char c = Request::Pause;
-       ::write (request_pipe[1], &c, 1);
+       (void) ::write (request_pipe[1], &c, 1);
        paused.wait(request_lock);
 }
 
@@ -363,8 +362,8 @@ void
 Butler::wait_until_finished ()
 {
        Glib::Mutex::Lock lm (request_lock);
-       char c = Request::Wake;
-       ::write (request_pipe[1], &c, 1);
+       char c = Request::Pause;
+       (void) ::write (request_pipe[1], &c, 1);
        paused.wait(request_lock);
 }
 
@@ -374,22 +373,42 @@ Butler::transport_work_requested () const
        return g_atomic_int_get(&should_do_transport_work);
 }
 
-float
-Butler::read_data_rate () const
+void
+Butler::empty_pool_trash ()
 {
-       /* disk i/o in excess of 10000MB/sec indicate the buffer cache
-          in action. ignore it.
-       */
-       return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
+       /* look in the trash, deleting empty pools until we come to one that is not empty */
+
+       RingBuffer<CrossThreadPool*>::rw_vector vec;
+       pool_trash.get_read_vector (&vec);
+
+       guint deleted = 0;
+
+       for (int i = 0; i < 2; ++i) {
+               for (guint j = 0; j < vec.len[i]; ++j) {
+                       if (vec.buf[i][j]->empty()) {
+                               delete vec.buf[i][j];
+                               ++deleted;
+                       } else {
+                               /* found a non-empty pool, so stop deleting */
+                               if (deleted) {
+                                       pool_trash.increment_read_idx (deleted);
+                               }
+                               return;
+                       }
+               }
+       }
+
+       if (deleted) {
+               pool_trash.increment_read_idx (deleted);
+       }
 }
 
-float
-Butler::write_data_rate () const
+void
+Butler::drop_references ()
 {
-       /* disk i/o in excess of 10000MB/sec indicate the buffer cache
-          in action. ignore it.
-       */
-       return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
+       SessionEvent::pool->set_trash (0);
 }
 
+
 } // namespace ARDOUR
+