Fix unit-test data (XML attributes changed)
[ardour.git] / libs / ardour / butler.cc
1 /*
2     Copyright (C) 1999-2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #ifndef PLATFORM_WINDOWS
25 #include <poll.h>
26 #endif
27
28 #include "pbd/error.h"
29 #include "pbd/pthread_utils.h"
30 #include "ardour/debug.h"
31 #include "ardour/butler.h"
32 #include "ardour/io.h"
33 #include "ardour/midi_diskstream.h"
34 #include "ardour/session.h"
35 #include "ardour/track.h"
36 #include "ardour/auditioner.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace PBD;
41
42 namespace ARDOUR {
43
44 Butler::Butler(Session& s)
45         : SessionHandleRef (s)
46         , thread()
47         , have_thread (false)
48         , audio_dstream_capture_buffer_size(0)
49         , audio_dstream_playback_buffer_size(0)
50         , midi_dstream_buffer_size(0)
51         , pool_trash(16)
52         , _xthread (true)
53 {
54         g_atomic_int_set(&should_do_transport_work, 0);
55         SessionEvent::pool->set_trash (&pool_trash);
56
57         /* catch future changes to parameters */
58         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
59 }
60
61 Butler::~Butler()
62 {
63         terminate_thread ();
64 }
65
66 void
67 Butler::map_parameters ()
68 {
69         /* use any current ones that we care about */
70         boost::function<void (std::string)> ff (boost::bind (&Butler::config_changed, this, _1));
71         Config->map_parameters (ff);
72 }
73
74 void
75 Butler::config_changed (std::string p)
76 {
77         if (p == "playback-buffer-seconds") {
78                 _session.adjust_playback_buffering ();
79                 if (Config->get_buffering_preset() == Custom) {
80                         /* size is in Samples, not bytes */
81                         audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
82                         _session.adjust_playback_buffering ();
83                 } else {
84 #ifndef NDEBUG
85                         std::cerr << "Skip explicit buffer seconds, preset in use\n";
86 #endif
87                 }
88         } else if (p == "capture-buffer-seconds") {
89                 if (Config->get_buffering_preset() == Custom) {
90                         audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
91                         _session.adjust_capture_buffering ();
92                 } else {
93 #ifndef NDEBUG
94                         std::cerr << "Skip explicit buffer seconds, preset in use\n";
95 #endif
96                 }
97         } else if (p == "buffering-preset") {
98                 Diskstream::set_buffering_parameters (Config->get_buffering_preset());
99                 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
100                 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
101                 _session.adjust_capture_buffering ();
102                 _session.adjust_playback_buffering ();
103         } else if (p == "midi-readahead") {
104                 MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * _session.frame_rate()));
105         }
106 }
107
108 int
109 Butler::start_thread()
110 {
111         // set up capture and playback buffering
112         Diskstream::set_buffering_parameters (Config->get_buffering_preset());
113
114         /* size is in Samples, not bytes */
115         const float rate = (float)_session.frame_rate();
116         audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
117         audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
118
119         /* size is in bytes
120          * XXX: AudioEngine needs to tell us the MIDI buffer size
121          * (i.e. how many MIDI bytes we might see in a cycle)
122          */
123         midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
124
125         MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
126
127         should_run = false;
128
129         if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
130                 error << _("Session: could not create butler thread") << endmsg;
131                 return -1;
132         }
133
134         //pthread_detach (thread);
135         have_thread = true;
136
137         // we are ready to request buffer adjustments
138         _session.adjust_capture_buffering ();
139         _session.adjust_playback_buffering ();
140
141         return 0;
142 }
143
144 void
145 Butler::terminate_thread ()
146 {
147         if (have_thread) {
148                 void* status;
149                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: ask butler to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
150                 queue_request (Request::Quit);
151                 pthread_join (thread, &status);
152         }
153 }
154
155 void *
156 Butler::_thread_work (void* arg)
157 {
158         SessionEvent::create_per_thread_pool ("butler events", 4096);
159         pthread_set_name (X_("butler"));
160         return ((Butler *) arg)->thread_work ();
161 }
162
163 void *
164 Butler::thread_work ()
165 {
166         uint32_t err = 0;
167
168         bool disk_work_outstanding = false;
169         RouteList::iterator i;
170
171         while (true) {
172                 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()));
173
174                 if(!disk_work_outstanding) {
175                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 butler waits for requests @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
176
177                         char msg;
178                         /* empty the pipe of all current requests */
179                         if (_xthread.receive (msg, true) >= 0) {
180                                 Request::Type req = (Request::Type) msg;
181                                 switch (req) {
182
183                                         case Request::Run:
184                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
185                                                 should_run = true;
186                                                 break;
187
188                                         case Request::Pause:
189                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
190                                                 should_run = false;
191                                                 break;
192
193                                         case Request::Quit:
194                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
195                                                 return 0;
196                                                 abort(); /*NOTREACHED*/
197                                                 break;
198
199                                         default:
200                                                 break;
201                                 }
202                         }
203                 }
204
205
206           restart:
207                 DEBUG_TRACE (DEBUG::Butler, "at restart for disk work\n");
208                 disk_work_outstanding = false;
209
210                 if (transport_work_requested()) {
211                         DEBUG_TRACE (DEBUG::Butler, string_compose ("do transport work @ %1\n", g_get_monotonic_time()));
212                         _session.butler_transport_work ();
213                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttransport work complete @ %1\n", g_get_monotonic_time()));
214                 }
215
216                 frameoffset_t audition_seek;
217                 if (should_run && _session.is_auditioning()
218                                 && (audition_seek = _session.the_auditioner()->seek_frame()) >= 0) {
219                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
220                         DEBUG_TRACE (DEBUG::Butler, "seek the auditioner\n");
221                         tr->seek(audition_seek);
222                         tr->do_refill ();
223                         _session.the_auditioner()->seek_response(audition_seek);
224                 }
225
226                 boost::shared_ptr<RouteList> rl = _session.get_routes();
227
228                 RouteList rl_with_auditioner = *rl;
229                 rl_with_auditioner.push_back (_session.the_auditioner());
230
231                 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
232
233                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
234
235                         if (!tr) {
236                                 continue;
237                         }
238
239                         boost::shared_ptr<IO> io = tr->input ();
240
241                         if (io && !io->active()) {
242                                 /* don't read inactive tracks */
243                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler skips inactive track %1\n", tr->name()));
244                                 continue;
245                         }
246                         DEBUG_TRACE (DEBUG::Butler, string_compose ("butler refills %1, playback load = %2\n", tr->name(), tr->playback_buffer_load()));
247                         switch (tr->do_refill ()) {
248                         case 0:
249                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill done %1\n", tr->name()));
250                                 break;
251
252                         case 1:
253                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill unfinished %1\n", tr->name()));
254                                 disk_work_outstanding = true;
255                                 break;
256
257                         default:
258                                 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
259                                 std::cerr << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << std::endl;
260                                 break;
261                         }
262
263                 }
264
265                 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
266                         /* we didn't get to all the streams */
267                         disk_work_outstanding = true;
268                 }
269
270                 if (!err && transport_work_requested()) {
271                         DEBUG_TRACE (DEBUG::Butler, "transport work requested during refill, back to restart\n");
272                         goto restart;
273                 }
274
275                 disk_work_outstanding = flush_tracks_to_disk_normal (rl, err);
276
277                 if (err && _session.actively_recording()) {
278                         /* stop the transport and try to catch as much possible
279                            captured state as we can.
280                         */
281                         DEBUG_TRACE (DEBUG::Butler, "error occurred during recording - stop transport\n");
282                         _session.request_stop ();
283                 }
284
285                 if (!err && transport_work_requested()) {
286                         DEBUG_TRACE (DEBUG::Butler, "transport work requested during flush, back to restart\n");
287                         goto restart;
288                 }
289
290                 if (!disk_work_outstanding) {
291                         _session.refresh_disk_space ();
292                 }
293
294                 {
295                         Glib::Threads::Mutex::Lock lm (request_lock);
296
297                         if (should_run && (disk_work_outstanding || transport_work_requested())) {
298                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("at end, should run %1 disk work %2 transport work %3 ... goto restart\n",
299                                                                             should_run, disk_work_outstanding, transport_work_requested()));
300                                 goto restart;
301                         }
302
303                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler signals pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
304                         paused.signal();
305                 }
306
307                 DEBUG_TRACE (DEBUG::Butler, "butler emptying pool trash\n");
308                 empty_pool_trash ();
309         }
310
311         return (0);
312 }
313
314 bool
315 Butler::flush_tracks_to_disk_normal (boost::shared_ptr<RouteList> rl, uint32_t& errors)
316 {
317         bool disk_work_outstanding = false;
318
319         for (RouteList::iterator i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
320
321                 // cerr << "write behind for " << (*i)->name () << endl;
322
323                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
324
325                 if (!tr) {
326                         continue;
327                 }
328
329                 /* note that we still try to flush diskstreams attached to inactive routes
330                  */
331
332                 int ret;
333
334                 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
335                 ret = tr->do_flush (ButlerContext, false);
336                 switch (ret) {
337                 case 0:
338                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
339                         break;
340
341                 case 1:
342                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
343                         disk_work_outstanding = true;
344                         break;
345
346                 default:
347                         errors++;
348                         error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
349                         std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
350                         /* don't break - try to flush all streams in case they
351                            are split across disks.
352                         */
353                 }
354         }
355
356         return disk_work_outstanding;
357 }
358
359 bool
360 Butler::flush_tracks_to_disk_after_locate (boost::shared_ptr<RouteList> rl, uint32_t& errors)
361 {
362         bool disk_work_outstanding = false;
363
364         /* almost the same as the "normal" version except that we do not test
365          * for transport_work_requested() and we force flushes.
366          */
367
368         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
369
370                 // cerr << "write behind for " << (*i)->name () << endl;
371
372                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
373
374                 if (!tr) {
375                         continue;
376                 }
377
378                 /* note that we still try to flush diskstreams attached to inactive routes
379                  */
380
381                 int ret;
382
383                 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
384                 ret = tr->do_flush (ButlerContext, true);
385                 switch (ret) {
386                 case 0:
387                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
388                         break;
389
390                 case 1:
391                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
392                         disk_work_outstanding = true;
393                         break;
394
395                 default:
396                         errors++;
397                         error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
398                         std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
399                         /* don't break - try to flush all streams in case they
400                            are split across disks.
401                         */
402                 }
403         }
404
405         return disk_work_outstanding;
406 }
407
408 void
409 Butler::schedule_transport_work ()
410 {
411         g_atomic_int_inc (&should_do_transport_work);
412         summon ();
413 }
414
415 void
416 Butler::queue_request (Request::Type r)
417 {
418         char c = r;
419         if (_xthread.deliver (c) != 1) {
420                 /* the x-thread channel is non-blocking
421                  * write may fail, but we really don't want to wait
422                  * under normal circumstances.
423                  *
424                  * a lost "run" requests under normal RT operation
425                  * is mostly harmless.
426                  *
427                  * TODO if ardour is freehweeling, wait & retry.
428                  * ditto for Request::Type Quit
429                  */
430                 assert(1); // we're screwd
431         }
432 }
433
434 void
435 Butler::summon ()
436 {
437         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: summon butler to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
438         queue_request (Request::Run);
439 }
440
441 void
442 Butler::stop ()
443 {
444         Glib::Threads::Mutex::Lock lm (request_lock);
445         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: asking butler to stop @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
446         queue_request (Request::Pause);
447         paused.wait(request_lock);
448 }
449
450 void
451 Butler::wait_until_finished ()
452 {
453         Glib::Threads::Mutex::Lock lm (request_lock);
454         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: waiting for butler to finish @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
455         queue_request (Request::Pause);
456         paused.wait(request_lock);
457 }
458
459 bool
460 Butler::transport_work_requested () const
461 {
462         return g_atomic_int_get(&should_do_transport_work);
463 }
464
465 void
466 Butler::empty_pool_trash ()
467 {
468         /* look in the trash, deleting empty pools until we come to one that is not empty */
469
470         RingBuffer<CrossThreadPool*>::rw_vector vec;
471         pool_trash.get_read_vector (&vec);
472
473         guint deleted = 0;
474
475         for (int i = 0; i < 2; ++i) {
476                 for (guint j = 0; j < vec.len[i]; ++j) {
477                         if (vec.buf[i][j]->empty()) {
478                                 delete vec.buf[i][j];
479                                 ++deleted;
480                         } else {
481                                 /* found a non-empty pool, so stop deleting */
482                                 if (deleted) {
483                                         pool_trash.increment_read_idx (deleted);
484                                 }
485                                 return;
486                         }
487                 }
488         }
489
490         if (deleted) {
491                 pool_trash.increment_read_idx (deleted);
492         }
493 }
494
495 void
496 Butler::drop_references ()
497 {
498         std::cerr << "Butler drops pool trash\n";
499         SessionEvent::pool->set_trash (0);
500 }
501
502
503 } // namespace ARDOUR
504