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)
47 , audio_dstream_capture_buffer_size(0)
48 , audio_dstream_playback_buffer_size(0)
49 , midi_dstream_buffer_size(0)
52 g_atomic_int_set(&should_do_transport_work, 0);
53 SessionEvent::pool->set_trash (&pool_trash);
55 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
64 Butler::config_changed (std::string p)
66 if (p == "playback-buffer-seconds") {
67 /* size is in Samples, not bytes */
68 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
69 _session.adjust_playback_buffering ();
70 } else if (p == "capture-buffer-seconds") {
71 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
72 _session.adjust_capture_buffering ();
76 #ifndef PLATFORM_WINDOWS
78 Butler::setup_request_pipe ()
80 if (pipe (request_pipe)) {
81 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
82 strerror (errno)) << endmsg;
86 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
87 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
88 strerror (errno)) << endmsg;
92 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
93 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
94 strerror (errno)) << endmsg;
102 Butler::start_thread()
104 const float rate = (float)_session.frame_rate();
106 /* size is in Samples, not bytes */
107 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
108 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
111 * XXX: Jack needs to tell us the MIDI buffer size
112 * (i.e. how many MIDI bytes we might see in a cycle)
114 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
116 MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
120 #ifndef PLATFORM_WINDOWS
121 if (setup_request_pipe() != 0) return -1;
124 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
125 error << _("Session: could not create butler thread") << endmsg;
129 //pthread_detach (thread);
135 Butler::terminate_thread ()
139 queue_request (Request::Quit);
140 pthread_join (thread, &status);
145 Butler::_thread_work (void* arg)
147 SessionEvent::create_per_thread_pool ("butler events", 4096);
148 pthread_set_name (X_("butler"));
149 return ((Butler *) arg)->thread_work ();
153 Butler::wait_for_requests ()
155 #ifndef PLATFORM_WINDOWS
156 struct pollfd pfd[1];
158 pfd[0].fd = request_pipe[0];
159 pfd[0].events = POLLIN|POLLERR|POLLHUP;
162 if (poll (pfd, 1, -1) < 0) {
164 if (errno == EINTR) {
168 error << string_compose (_("poll on butler request pipe failed (%1)"),
174 if (pfd[0].revents & ~POLLIN) {
175 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
179 if (pfd[0].revents & POLLIN) {
185 m_request_sem.wait ();
191 Butler::dequeue_request (Request::Type& r)
193 #ifndef PLATFORM_WINDOWS
195 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
197 r = (Request::Type) req;
199 } else if (nread == 0) {
201 } else if (errno == EAGAIN) {
204 fatal << _("Error reading from butler request pipe") << endmsg;
208 r = (Request::Type) m_request_state.get();
214 Butler::thread_work ()
218 bool disk_work_outstanding = false;
219 RouteList::iterator i;
222 if(!disk_work_outstanding) {
223 if (wait_for_requests ()) {
226 /* empty the pipe of all current requests */
227 #ifdef PLATFORM_WINDOWS
228 dequeue_request (req);
231 while(dequeue_request(req)) {
257 disk_work_outstanding = false;
259 if (transport_work_requested()) {
260 _session.butler_transport_work ();
263 frameoffset_t audition_seek;
264 if (should_run && _session.is_auditioning()
265 && (audition_seek = _session.the_auditioner()->seek_frame()) >= 0) {
266 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
267 tr->seek(audition_seek);
268 _session.the_auditioner()->seek_response(audition_seek);
271 boost::shared_ptr<RouteList> rl = _session.get_routes();
273 RouteList rl_with_auditioner = *rl;
274 rl_with_auditioner.push_back (_session.the_auditioner());
276 // for (i = dsl->begin(); i != dsl->end(); ++i) {
277 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
280 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
282 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
288 boost::shared_ptr<IO> io = tr->input ();
290 if (io && !io->active()) {
291 /* don't read inactive tracks */
295 switch (tr->do_refill ()) {
300 disk_work_outstanding = true;
304 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
310 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
311 /* we didn't get to all the streams */
312 disk_work_outstanding = true;
315 if (!err && transport_work_requested()) {
319 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
320 // cerr << "write behind for " << (*i)->name () << endl;
322 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
328 /* note that we still try to flush diskstreams attached to inactive routes
331 switch (tr->do_flush (ButlerContext)) {
336 disk_work_outstanding = true;
341 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
342 /* don't break - try to flush all streams in case they
343 are split across disks.
348 if (err && _session.actively_recording()) {
349 /* stop the transport and try to catch as much possible
350 captured state as we can.
352 _session.request_stop ();
355 if (i != rl->begin() && i != rl->end()) {
356 /* we didn't get to all the streams */
357 disk_work_outstanding = true;
360 if (!err && transport_work_requested()) {
364 if (!disk_work_outstanding) {
365 _session.refresh_disk_space ();
370 Glib::Threads::Mutex::Lock lm (request_lock);
372 if (should_run && (disk_work_outstanding || transport_work_requested())) {
373 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
374 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
390 Butler::schedule_transport_work ()
392 g_atomic_int_inc (&should_do_transport_work);
397 Butler::queue_request (Request::Type r)
399 #ifndef PLATFORM_WINDOWS
401 (void) ::write (request_pipe[1], &c, 1);
403 m_request_state.set (r);
404 m_request_sem.post ();
411 queue_request (Request::Run);
417 Glib::Threads::Mutex::Lock lm (request_lock);
418 queue_request (Request::Pause);
419 paused.wait(request_lock);
423 Butler::wait_until_finished ()
425 Glib::Threads::Mutex::Lock lm (request_lock);
426 queue_request (Request::Pause);
427 paused.wait(request_lock);
431 Butler::transport_work_requested () const
433 return g_atomic_int_get(&should_do_transport_work);
437 Butler::empty_pool_trash ()
439 /* look in the trash, deleting empty pools until we come to one that is not empty */
441 RingBuffer<CrossThreadPool*>::rw_vector vec;
442 pool_trash.get_read_vector (&vec);
446 for (int i = 0; i < 2; ++i) {
447 for (guint j = 0; j < vec.len[i]; ++j) {
448 if (vec.buf[i][j]->empty()) {
449 delete vec.buf[i][j];
452 /* found a non-empty pool, so stop deleting */
454 pool_trash.increment_read_idx (deleted);
462 pool_trash.increment_read_idx (deleted);
467 Butler::drop_references ()
469 cerr << "Butler drops pool trash\n";
470 SessionEvent::pool->set_trash (0);
474 } // namespace ARDOUR