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