2 Copyright (C) 1999-2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "pbd/error.h"
25 #include "pbd/pthread_utils.h"
26 #include "ardour/butler.h"
27 #include "ardour/io.h"
28 #include "ardour/midi_diskstream.h"
29 #include "ardour/session.h"
30 #include "ardour/track.h"
31 #include "ardour/auditioner.h"
39 Butler::Butler(Session& s)
40 : SessionHandleRef (s)
42 , audio_dstream_capture_buffer_size(0)
43 , audio_dstream_playback_buffer_size(0)
44 , midi_dstream_buffer_size(0)
47 g_atomic_int_set(&should_do_transport_work, 0);
48 SessionEvent::pool->set_trash (&pool_trash);
50 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
59 Butler::config_changed (std::string p)
61 if (p == "playback-buffer-seconds") {
62 /* size is in Samples, not bytes */
63 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
64 _session.adjust_playback_buffering ();
65 } else if (p == "capture-buffer-seconds") {
66 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
67 _session.adjust_capture_buffering ();
72 Butler::start_thread()
74 const float rate = (float)_session.frame_rate();
76 /* size is in Samples, not bytes */
77 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
78 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
81 * XXX: Jack needs to tell us the MIDI buffer size
82 * (i.e. how many MIDI bytes we might see in a cycle)
84 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
86 MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
90 if (pipe (request_pipe)) {
91 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
92 strerror (errno)) << endmsg;
96 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
97 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
98 strerror (errno)) << endmsg;
102 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
103 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
104 strerror (errno)) << endmsg;
108 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
109 error << _("Session: could not create butler thread") << endmsg;
113 //pthread_detach (thread);
119 Butler::terminate_thread ()
123 const char c = Request::Quit;
124 (void) ::write (request_pipe[1], &c, 1);
125 pthread_join (thread, &status);
130 Butler::_thread_work (void* arg)
132 SessionEvent::create_per_thread_pool ("butler events", 4096);
133 pthread_set_name (X_("butler"));
134 return ((Butler *) arg)->thread_work ();
138 Butler::thread_work ()
142 struct pollfd pfd[1];
143 bool disk_work_outstanding = false;
144 RouteList::iterator i;
147 pfd[0].fd = request_pipe[0];
148 pfd[0].events = POLLIN|POLLERR|POLLHUP;
150 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
152 if (errno == EINTR) {
156 error << string_compose (_("poll on butler request pipe failed (%1)"),
162 if (pfd[0].revents & ~POLLIN) {
163 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
167 if (pfd[0].revents & POLLIN) {
171 /* empty the pipe of all current requests */
174 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
177 switch ((Request::Type) req) {
196 } else if (nread == 0) {
198 } else if (errno == EAGAIN) {
201 fatal << _("Error reading from butler request pipe") << endmsg;
209 disk_work_outstanding = false;
211 if (transport_work_requested()) {
212 _session.butler_transport_work ();
215 frameoffset_t audition_seek;
216 if (should_run && _session.is_auditioning()
217 && (audition_seek = _session.the_auditioner()->seek_frame()) >= 0) {
218 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
219 tr->seek(audition_seek);
220 _session.the_auditioner()->seek_response(audition_seek);
223 boost::shared_ptr<RouteList> rl = _session.get_routes();
225 RouteList rl_with_auditioner = *rl;
226 rl_with_auditioner.push_back (_session.the_auditioner());
228 // for (i = dsl->begin(); i != dsl->end(); ++i) {
229 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
232 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
234 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
240 boost::shared_ptr<IO> io = tr->input ();
242 if (io && !io->active()) {
243 /* don't read inactive tracks */
247 switch (tr->do_refill ()) {
252 disk_work_outstanding = true;
256 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
262 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
263 /* we didn't get to all the streams */
264 disk_work_outstanding = true;
267 if (!err && transport_work_requested()) {
271 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
272 // cerr << "write behind for " << (*i)->name () << endl;
274 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
280 /* note that we still try to flush diskstreams attached to inactive routes
283 switch (tr->do_flush (ButlerContext)) {
288 disk_work_outstanding = true;
293 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
294 /* don't break - try to flush all streams in case they
295 are split across disks.
300 if (err && _session.actively_recording()) {
301 /* stop the transport and try to catch as much possible
302 captured state as we can.
304 _session.request_stop ();
307 if (i != rl->begin() && i != rl->end()) {
308 /* we didn't get to all the streams */
309 disk_work_outstanding = true;
312 if (!err && transport_work_requested()) {
316 if (!disk_work_outstanding) {
317 _session.refresh_disk_space ();
322 Glib::Threads::Mutex::Lock lm (request_lock);
324 if (should_run && (disk_work_outstanding || transport_work_requested())) {
325 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
326 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
342 Butler::schedule_transport_work ()
344 g_atomic_int_inc (&should_do_transport_work);
351 char c = Request::Run;
352 (void) ::write (request_pipe[1], &c, 1);
358 Glib::Threads::Mutex::Lock lm (request_lock);
359 char c = Request::Pause;
360 (void) ::write (request_pipe[1], &c, 1);
361 paused.wait(request_lock);
365 Butler::wait_until_finished ()
367 Glib::Threads::Mutex::Lock lm (request_lock);
368 char c = Request::Pause;
369 (void) ::write (request_pipe[1], &c, 1);
370 paused.wait(request_lock);
374 Butler::transport_work_requested () const
376 return g_atomic_int_get(&should_do_transport_work);
380 Butler::empty_pool_trash ()
382 /* look in the trash, deleting empty pools until we come to one that is not empty */
384 RingBuffer<CrossThreadPool*>::rw_vector vec;
385 pool_trash.get_read_vector (&vec);
389 for (int i = 0; i < 2; ++i) {
390 for (guint j = 0; j < vec.len[i]; ++j) {
391 if (vec.buf[i][j]->empty()) {
392 delete vec.buf[i][j];
395 /* found a non-empty pool, so stop deleting */
397 pool_trash.increment_read_idx (deleted);
405 pool_trash.increment_read_idx (deleted);
410 Butler::drop_references ()
412 cerr << "Butler drops pool trash\n";
413 SessionEvent::pool->set_trash (0);
417 } // namespace ARDOUR