strict i/o: limit output channels.
[ardour.git] / libs / ardour / butler.cc
index b201960e0e6b5c0b336c03e9afba54f1fce29edc..62f3a525ea82582c8dc866a627d0ef715a0de2e3 100644 (file)
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
+
+#ifndef PLATFORM_WINDOWS
 #include <poll.h>
+#endif
+
 #include "pbd/error.h"
 #include "pbd/pthread_utils.h"
+#include "ardour/debug.h"
 #include "ardour/butler.h"
-#include "ardour/crossfade.h"
 #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)
+       , thread()
+       , have_thread (false)
+       , audio_dstream_capture_buffer_size(0)
+       , audio_dstream_playback_buffer_size(0)
        , midi_dstream_buffer_size(0)
        , pool_trash(16)
+       , _xthread (true)
 {
        g_atomic_int_set(&should_do_transport_work, 0);
        SessionEvent::pool->set_trash (&pool_trash);
+
+        /* catch future changes to parameters */
+        Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
 }
 
 Butler::~Butler()
@@ -54,50 +63,76 @@ Butler::~Butler()
        terminate_thread ();
 }
 
+void
+Butler::map_parameters ()
+{
+        /* use any current ones that we care about */
+        boost::function<void (std::string)> ff (boost::bind (&Butler::config_changed, this, _1));
+        Config->map_parameters (ff);
+}
+
+void
+Butler::config_changed (std::string p)
+{
+       if (p == "playback-buffer-seconds") {
+               _session.adjust_playback_buffering ();
+               if (Config->get_buffering_preset() == Custom) {
+                       /* 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 {
+                       std::cerr << "Skip explicit buffer seconds, preset in use\n";
+               }
+       } else if (p == "capture-buffer-seconds") {
+               if (Config->get_buffering_preset() == Custom) {
+                       audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
+                       _session.adjust_capture_buffering ();
+               } else {
+                       std::cerr << "Skip explicit buffer seconds, preset in use\n";
+               }
+       } else if (p == "buffering-preset") {
+               Diskstream::set_buffering_parameters (Config->get_buffering_preset());
+               audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
+               audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
+               _session.adjust_capture_buffering ();
+               _session.adjust_playback_buffering ();
+       } else if (p == "midi-readahead") {
+               MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * _session.frame_rate()));
+       }
+}
+
 int
 Butler::start_thread()
 {
-       const float rate = (float)_session.frame_rate();
+       // set up capture and playback buffering
+       Diskstream::set_buffering_parameters (Config->get_buffering_preset());
 
        /* size is in Samples, not bytes */
-       audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
+       const float rate = (float)_session.frame_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
+        * XXX: AudioEngine 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() * rate);
 
-       MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
-
-       Crossfade::set_buffer_size (audio_dstream_buffer_size);
+       MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
 
        should_run = false;
 
-       if (pipe (request_pipe)) {
-               error << string_compose(_("Cannot create transport request signal pipe (%1)"),
-                               strerror (errno)) << endmsg;
-               return -1;
-       }
-
-       if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
-               error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
-                               strerror (errno)) << endmsg;
-               return -1;
-       }
-
-       if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
-               error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
-                               strerror (errno)) << endmsg;
-               return -1;
-       }
-
        if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
                error << _("Session: could not create butler thread") << endmsg;
                return -1;
        }
 
        //pthread_detach (thread);
+       have_thread = true;
+
+       // we are ready to request buffer adjustments
+       _session.adjust_capture_buffering ();
+       _session.adjust_playback_buffering ();
 
        return 0;
 }
@@ -105,10 +140,10 @@ Butler::start_thread()
 void
 Butler::terminate_thread ()
 {
-       if (thread) {
+       if (have_thread) {
                void* status;
-               const char c = Request::Quit;
-               ::write (request_pipe[1], &c, 1);
+                DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: ask butler to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
+               queue_request (Request::Quit);
                pthread_join (thread, &status);
        }
 }
@@ -116,7 +151,7 @@ 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 ();
 }
@@ -125,223 +160,247 @@ 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];
-               pfd[0].events = POLLIN|POLLERR|POLLHUP;
-
-               if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
-
-                       if (errno == EINTR) {
-                               continue;
-                       }
-
-                       error << string_compose (_("poll on butler request pipe failed (%1)"),
-                                         strerror (errno))
-                             << endmsg;
-                       break;
-               }
-
-               if (pfd[0].revents & ~POLLIN) {
-                       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) {
+               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 butler main loop, disk work outstanding ? %2 @ %3\n", DEBUG_THREAD_SELF, disk_work_outstanding, g_get_monotonic_time()));
 
-                       char req;
+               if(!disk_work_outstanding) {
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 butler waits for requests @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
 
+                       char msg;
                        /* empty the pipe of all current requests */
-
-                       while (1) {
-                               size_t nread = ::read (request_pipe[0], &req, sizeof (req));
-                               if (nread == 1) {
-
-                                       switch ((Request::Type) req) {
-
-                                       case Request::Wake:
-                                               break;
+                       if (_xthread.receive (msg, true) >= 0) {
+                               Request::Type req = (Request::Type) msg;
+                               switch (req) {
 
                                        case Request::Run:
+                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
                                                should_run = true;
                                                break;
 
                                        case Request::Pause:
+                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
                                                should_run = false;
                                                break;
 
                                        case Request::Quit:
-                                               pthread_exit_pbd (0);
-                                               /*NOTREACHED*/
+                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
+                                               return 0;
+                                               abort(); /*NOTREACHED*/
                                                break;
 
                                        default:
                                                break;
-                                       }
-
-                               } else if (nread == 0) {
-                                       break;
-                               } else if (errno == EAGAIN) {
-                                       break;
-                               } else {
-                                       fatal << _("Error reading from butler request pipe") << endmsg;
-                                       /*NOTREACHED*/
                                }
                        }
                }
 
+
+         restart:
+               DEBUG_TRACE (DEBUG::Butler, "at restart for disk work\n");
+               disk_work_outstanding = false;
+
                if (transport_work_requested()) {
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("do transport work @ %1\n", g_get_monotonic_time()));
                        _session.butler_transport_work ();
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttransport work complete @ %1\n", g_get_monotonic_time()));
                }
 
-               disk_work_outstanding = false;
-               bytes = 0;
-               compute_io = true;
-
-               begin = get_microseconds();
+               frameoffset_t audition_seek;
+               if (should_run && _session.is_auditioning()
+                               && (audition_seek = _session.the_auditioner()->seek_frame()) >= 0) {
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
+                       DEBUG_TRACE (DEBUG::Butler, "seek the auditioner\n");
+                       tr->seek(audition_seek);
+                       tr->do_refill ();
+                       _session.the_auditioner()->seek_response(audition_seek);
+               }
 
-               boost::shared_ptr<Session::DiskstreamList> dsl = _session.diskstream_list().reader ();
+               boost::shared_ptr<RouteList> rl = _session.get_routes();
 
-//             for (i = dsl->begin(); i != dsl->end(); ++i) {
-//                     cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
-//             }
+               RouteList rl_with_auditioner = *rl;
+               rl_with_auditioner.push_back (_session.the_auditioner());
 
-               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 */
+                               DEBUG_TRACE (DEBUG::Butler, string_compose ("butler skips inactive track %1\n", tr->name()));
                                continue;
                        }
-
-                       switch (ds->do_refill ()) {
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("butler refills %1, playback load = %2\n", tr->name(), tr->playback_buffer_load()));
+                       switch (tr->do_refill ()) {
                        case 0:
-                               bytes += ds->read_data_count();
+                               DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill done %1\n", tr->name()));
                                break;
+
                        case 1:
-                               bytes += ds->read_data_count();
+                               DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill unfinished %1\n", tr->name()));
                                disk_work_outstanding = true;
                                break;
 
                        default:
-                               compute_io = false;
                                error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
+                                std::cerr << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << std::endl;
                                break;
                        }
 
                }
 
-               if (i != dsl->begin() && 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;
+                       DEBUG_TRACE (DEBUG::Butler, "transport work requested during refill, back to restart\n");
+                       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
-                       }
-               }
-
-               bytes = 0;
-               compute_io = true;
-               begin = get_microseconds();
-
-               for (i = dsl->begin(); !transport_work_requested() && 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 (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.
-                               */
-                       }
-               }
+               disk_work_outstanding = flush_tracks_to_disk_normal (rl, err);
 
                if (err && _session.actively_recording()) {
                        /* stop the transport and try to catch as much possible
                           captured state as we can.
                        */
+                       DEBUG_TRACE (DEBUG::Butler, "error occurred during recording - stop transport\n");
                        _session.request_stop ();
                }
 
-               if (i != dsl->begin() && i != dsl->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
-                       }
+                       DEBUG_TRACE (DEBUG::Butler, "transport work requested during flush, back to restart\n");
+                       goto restart;
                }
 
                if (!disk_work_outstanding) {
                        _session.refresh_disk_space ();
                }
 
-
                {
-                       Glib::Mutex::Lock lm (request_lock);
+                       Glib::Threads::Mutex::Lock lm (request_lock);
 
                        if (should_run && (disk_work_outstanding || transport_work_requested())) {
-//                             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;
-//                             }
-
-                               continue;
+                                DEBUG_TRACE (DEBUG::Butler, string_compose ("at end, should run %1 disk work %2 transport work %3 ... goto restart\n",
+                                                                            should_run, disk_work_outstanding, transport_work_requested()));
+                               goto restart;
                        }
 
+                        DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler signals pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
                        paused.signal();
                }
 
+               DEBUG_TRACE (DEBUG::Butler, "butler emptying pool trash\n");
                empty_pool_trash ();
        }
 
-       pthread_exit_pbd (0);
-       /*NOTREACHED*/
        return (0);
 }
 
+bool
+Butler::flush_tracks_to_disk_normal (boost::shared_ptr<RouteList> rl, uint32_t& errors)
+{
+       bool disk_work_outstanding = false;
+
+       for (RouteList::iterator i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
+
+               // cerr << "write behind for " << (*i)->name () << endl;
+
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+
+               if (!tr) {
+                       continue;
+               }
+
+               /* note that we still try to flush diskstreams attached to inactive routes
+                */
+
+               int ret;
+
+               DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
+               ret = tr->do_flush (ButlerContext, false);
+               switch (ret) {
+               case 0:
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
+                       break;
+
+               case 1:
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
+                       disk_work_outstanding = true;
+                       break;
+
+               default:
+                       errors++;
+                       error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
+                       std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
+                       /* don't break - try to flush all streams in case they
+                          are split across disks.
+                       */
+               }
+       }
+
+       return disk_work_outstanding;
+}
+
+bool
+Butler::flush_tracks_to_disk_after_locate (boost::shared_ptr<RouteList> rl, uint32_t& errors)
+{
+       bool disk_work_outstanding = false;
+
+       /* almost the same as the "normal" version except that we do not test
+        * for transport_work_requested() and we force flushes.
+        */
+
+       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+
+               // cerr << "write behind for " << (*i)->name () << endl;
+
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+
+               if (!tr) {
+                       continue;
+               }
+
+               /* note that we still try to flush diskstreams attached to inactive routes
+                */
+
+               int ret;
+
+               DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
+               ret = tr->do_flush (ButlerContext, true);
+               switch (ret) {
+               case 0:
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
+                       break;
+
+               case 1:
+                       DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
+                       disk_work_outstanding = true;
+                       break;
+
+               default:
+                       errors++;
+                       error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
+                       std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
+                       /* don't break - try to flush all streams in case they
+                          are split across disks.
+                       */
+               }
+       }
+
+       return disk_work_outstanding;
+}
+
 void
 Butler::schedule_transport_work ()
 {
@@ -349,28 +408,47 @@ Butler::schedule_transport_work ()
        summon ();
 }
 
+void
+Butler::queue_request (Request::Type r)
+{
+       char c = r;
+       if (_xthread.deliver (c) != 1) {
+               /* the x-thread channel is non-blocking
+                * write may fail, but we really don't want to wait
+                * under normal circumstances.
+                *
+                * a lost "run" requests under normal RT operation
+                * is mostly harmless.
+                *
+                * TODO if ardour is freehweeling, wait & retry.
+                * ditto for Request::Type Quit
+                */
+               assert(1); // we're screwd
+       }
+}
+
 void
 Butler::summon ()
 {
-       char c = Request::Run;
-       ::write (request_pipe[1], &c, 1);
+       DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: summon butler to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
+       queue_request (Request::Run);
 }
 
 void
 Butler::stop ()
 {
-       Glib::Mutex::Lock lm (request_lock);
-       char c = Request::Pause;
-       ::write (request_pipe[1], &c, 1);
+       Glib::Threads::Mutex::Lock lm (request_lock);
+       DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: asking butler to stop @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
+       queue_request (Request::Pause);
        paused.wait(request_lock);
 }
 
 void
 Butler::wait_until_finished ()
 {
-       Glib::Mutex::Lock lm (request_lock);
-       char c = Request::Wake;
-       ::write (request_pipe[1], &c, 1);
+       Glib::Threads::Mutex::Lock lm (request_lock);
+       DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: waiting for butler to finish @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
+       queue_request (Request::Pause);
        paused.wait(request_lock);
 }
 
@@ -380,34 +458,16 @@ Butler::transport_work_requested () const
        return g_atomic_int_get(&should_do_transport_work);
 }
 
-float
-Butler::read_data_rate () const
-{
-       /* 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;
-}
-
-float
-Butler::write_data_rate () const
-{
-       /* 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;
-}
-
 void
 Butler::empty_pool_trash ()
 {
        /* 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()) {
@@ -431,6 +491,7 @@ Butler::empty_pool_trash ()
 void
 Butler::drop_references ()
 {
+       std::cerr << "Butler drops pool trash\n";
        SessionEvent::pool->set_trash (0);
 }