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 #ifndef PLATFORM_WINDOWS
28 #include "pbd/error.h"
29 #include "pbd/pthread_utils.h"
30 #include "ardour/butler.h"
31 #include "ardour/io.h"
32 #include "ardour/midi_diskstream.h"
33 #include "ardour/session.h"
34 #include "ardour/track.h"
35 #include "ardour/auditioner.h"
43 Butler::Butler(Session& s)
44 : SessionHandleRef (s)
46 , audio_dstream_capture_buffer_size(0)
47 , audio_dstream_playback_buffer_size(0)
48 , midi_dstream_buffer_size(0)
51 g_atomic_int_set(&should_do_transport_work, 0);
52 SessionEvent::pool->set_trash (&pool_trash);
54 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
63 Butler::config_changed (std::string p)
65 if (p == "playback-buffer-seconds") {
66 /* size is in Samples, not bytes */
67 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
68 _session.adjust_playback_buffering ();
69 } else if (p == "capture-buffer-seconds") {
70 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
71 _session.adjust_capture_buffering ();
75 #ifndef PLATFORM_WINDOWS
77 Butler::setup_request_pipe ()
79 if (pipe (request_pipe)) {
80 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
81 strerror (errno)) << endmsg;
85 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
86 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
87 strerror (errno)) << endmsg;
91 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
92 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
93 strerror (errno)) << endmsg;
101 Butler::start_thread()
103 const float rate = (float)_session.frame_rate();
105 /* size is in Samples, not bytes */
106 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
107 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
110 * XXX: Jack needs to tell us the MIDI buffer size
111 * (i.e. how many MIDI bytes we might see in a cycle)
113 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
115 MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
119 #ifndef PLATFORM_WINDOWS
120 if (setup_request_pipe() != 0) return -1;
123 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
124 error << _("Session: could not create butler thread") << endmsg;
128 //pthread_detach (thread);
134 Butler::terminate_thread ()
137 queue_request (Request::Quit);
138 pthread_join (thread, &status);
142 Butler::_thread_work (void* arg)
144 SessionEvent::create_per_thread_pool ("butler events", 4096);
145 pthread_set_name (X_("butler"));
146 return ((Butler *) arg)->thread_work ();
150 Butler::wait_for_requests ()
152 #ifndef PLATFORM_WINDOWS
153 struct pollfd pfd[1];
155 pfd[0].fd = request_pipe[0];
156 pfd[0].events = POLLIN|POLLERR|POLLHUP;
159 if (poll (pfd, 1, -1) < 0) {
161 if (errno == EINTR) {
165 error << string_compose (_("poll on butler request pipe failed (%1)"),
171 if (pfd[0].revents & ~POLLIN) {
172 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
176 if (pfd[0].revents & POLLIN) {
182 m_request_sem.wait ();
188 Butler::dequeue_request (Request::Type& r)
190 #ifndef PLATFORM_WINDOWS
192 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
194 r = (Request::Type) req;
196 } else if (nread == 0) {
198 } else if (errno == EAGAIN) {
201 fatal << _("Error reading from butler request pipe") << endmsg;
205 r = (Request::Type) m_request_state.get();
211 Butler::thread_work ()
215 bool disk_work_outstanding = false;
216 RouteList::iterator i;
219 if(!disk_work_outstanding) {
220 if (wait_for_requests ()) {
223 /* empty the pipe of all current requests */
224 #ifdef PLATFORM_WINDOWS
225 dequeue_request (req);
228 while(dequeue_request(req)) {
254 disk_work_outstanding = false;
256 if (transport_work_requested()) {
257 _session.butler_transport_work ();
260 boost::shared_ptr<RouteList> rl = _session.get_routes();
262 RouteList rl_with_auditioner = *rl;
263 rl_with_auditioner.push_back (_session.the_auditioner());
265 // for (i = dsl->begin(); i != dsl->end(); ++i) {
266 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
269 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
271 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
277 boost::shared_ptr<IO> io = tr->input ();
279 if (io && !io->active()) {
280 /* don't read inactive tracks */
284 switch (tr->do_refill ()) {
289 disk_work_outstanding = true;
293 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
299 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
300 /* we didn't get to all the streams */
301 disk_work_outstanding = true;
304 if (!err && transport_work_requested()) {
308 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
309 // cerr << "write behind for " << (*i)->name () << endl;
311 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
317 /* note that we still try to flush diskstreams attached to inactive routes
320 switch (tr->do_flush (ButlerContext)) {
325 disk_work_outstanding = true;
330 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
331 /* don't break - try to flush all streams in case they
332 are split across disks.
337 if (err && _session.actively_recording()) {
338 /* stop the transport and try to catch as much possible
339 captured state as we can.
341 _session.request_stop ();
344 if (i != rl->begin() && i != rl->end()) {
345 /* we didn't get to all the streams */
346 disk_work_outstanding = true;
349 if (!err && transport_work_requested()) {
353 if (!disk_work_outstanding) {
354 _session.refresh_disk_space ();
359 Glib::Threads::Mutex::Lock lm (request_lock);
361 if (should_run && (disk_work_outstanding || transport_work_requested())) {
362 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
363 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
379 Butler::schedule_transport_work ()
381 g_atomic_int_inc (&should_do_transport_work);
386 Butler::queue_request (Request::Type r)
388 #ifndef PLATFORM_WINDOWS
390 (void) ::write (request_pipe[1], &c, 1);
392 m_request_state.set (r);
393 m_request_sem.post ();
400 queue_request (Request::Run);
406 Glib::Threads::Mutex::Lock lm (request_lock);
407 queue_request (Request::Pause);
408 paused.wait(request_lock);
412 Butler::wait_until_finished ()
414 Glib::Threads::Mutex::Lock lm (request_lock);
415 queue_request (Request::Pause);
416 paused.wait(request_lock);
420 Butler::transport_work_requested () const
422 return g_atomic_int_get(&should_do_transport_work);
426 Butler::empty_pool_trash ()
428 /* look in the trash, deleting empty pools until we come to one that is not empty */
430 RingBuffer<CrossThreadPool*>::rw_vector vec;
431 pool_trash.get_read_vector (&vec);
435 for (int i = 0; i < 2; ++i) {
436 for (guint j = 0; j < vec.len[i]; ++j) {
437 if (vec.buf[i][j]->empty()) {
438 delete vec.buf[i][j];
441 /* found a non-empty pool, so stop deleting */
443 pool_trash.increment_read_idx (deleted);
451 pool_trash.increment_read_idx (deleted);
456 Butler::drop_references ()
458 cerr << "Butler drops pool trash\n";
459 SessionEvent::pool->set_trash (0);
463 } // namespace ARDOUR