goodbye USE_TRACKS_CODE_FEATURES and is_tracks_build
[ardour.git] / libs / ardour / session.cc
1 /*
2  * Copyright (C) 1999-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006-2007 Jesse Chappell <jesse@essej.net>
4  * Copyright (C) 2006-2009 Sampo Savolainen <v2@iki.fi>
5  * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
6  * Copyright (C) 2006-2016 Tim Mayberry <mojofunk@gmail.com>
7  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
8  * Copyright (C) 2008-2009 Hans Baier <hansfbaier@googlemail.com>
9  * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
10  * Copyright (C) 2013-2017 Nick Mainsbridge <mainsbridge@gmail.com>
11  * Copyright (C) 2014-2019 Ben Loftis <ben@harrisonconsoles.com>
12  * Copyright (C) 2015 GZharun <grygoriiz@wavesglobal.com>
13  * Copyright (C) 2016-2018 Len Ovens <len@ovenwerks.net>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28  */
29
30 #include <stdint.h>
31
32 #include <algorithm>
33 #include <string>
34 #include <vector>
35 #include <sstream>
36 #include <cstdio> /* sprintf(3) ... grrr */
37 #include <cmath>
38 #include <cerrno>
39 #include <unistd.h>
40 #include <limits.h>
41
42 #include <glibmm/threads.h>
43 #include <glibmm/miscutils.h>
44 #include <glibmm/fileutils.h>
45
46 #include <boost/algorithm/string/erase.hpp>
47
48 #include "pbd/basename.h"
49 #include "pbd/convert.h"
50 #include "pbd/error.h"
51 #include "pbd/file_utils.h"
52 #include "pbd/i18n.h"
53 #include "pbd/md5.h"
54 #include "pbd/pthread_utils.h"
55 #include "pbd/search_path.h"
56 #include "pbd/stacktrace.h"
57 #include "pbd/stl_delete.h"
58 #include "pbd/replace_all.h"
59 #include "pbd/types_convert.h"
60 #include "pbd/unwind.h"
61
62 #include "ardour/amp.h"
63 #include "ardour/analyser.h"
64 #include "ardour/async_midi_port.h"
65 #include "ardour/audio_buffer.h"
66 #include "ardour/audio_port.h"
67 #include "ardour/audio_track.h"
68 #include "ardour/audioengine.h"
69 #include "ardour/audiofilesource.h"
70 #include "ardour/auditioner.h"
71 #include "ardour/boost_debug.h"
72 #include "ardour/buffer_manager.h"
73 #include "ardour/buffer_set.h"
74 #include "ardour/bundle.h"
75 #include "ardour/butler.h"
76 #include "ardour/click.h"
77 #include "ardour/control_protocol_manager.h"
78 #include "ardour/data_type.h"
79 #include "ardour/debug.h"
80 #include "ardour/disk_reader.h"
81 #include "ardour/directory_names.h"
82 #include "ardour/filename_extensions.h"
83 #include "ardour/gain_control.h"
84 #include "ardour/graph.h"
85 #include "ardour/luabindings.h"
86 #include "ardour/midiport_manager.h"
87 #include "ardour/scene_changer.h"
88 #include "ardour/midi_patch_manager.h"
89 #include "ardour/midi_track.h"
90 #include "ardour/midi_ui.h"
91 #include "ardour/operations.h"
92 #include "ardour/playlist.h"
93 #include "ardour/playlist_factory.h"
94 #include "ardour/plugin.h"
95 #include "ardour/plugin_insert.h"
96 #include "ardour/process_thread.h"
97 #include "ardour/profile.h"
98 #include "ardour/rc_configuration.h"
99 #include "ardour/recent_sessions.h"
100 #include "ardour/region.h"
101 #include "ardour/region_factory.h"
102 #include "ardour/revision.h"
103 #include "ardour/route_graph.h"
104 #include "ardour/route_group.h"
105 #include "ardour/rt_tasklist.h"
106 #include "ardour/send.h"
107 #include "ardour/selection.h"
108 #include "ardour/session.h"
109 #include "ardour/session_directory.h"
110 #include "ardour/session_playlists.h"
111 #include "ardour/smf_source.h"
112 #include "ardour/solo_isolate_control.h"
113 #include "ardour/source_factory.h"
114 #include "ardour/speakers.h"
115 #include "ardour/tempo.h"
116 #include "ardour/ticker.h"
117 #include "ardour/transport_fsm.h"
118 #include "ardour/transport_master.h"
119 #include "ardour/transport_master_manager.h"
120 #include "ardour/track.h"
121 #include "ardour/types_convert.h"
122 #include "ardour/user_bundle.h"
123 #include "ardour/utils.h"
124 #include "ardour/vca_manager.h"
125 #include "ardour/vca.h"
126
127 #include "midi++/port.h"
128 #include "midi++/mmc.h"
129
130 #include "LuaBridge/LuaBridge.h"
131
132 #include <glibmm/checksum.h>
133
134 namespace ARDOUR {
135 class MidiSource;
136 class Processor;
137 class Speakers;
138 }
139
140 using namespace std;
141 using namespace ARDOUR;
142 using namespace PBD;
143
144 bool Session::_disable_all_loaded_plugins = false;
145 bool Session::_bypass_all_loaded_plugins = false;
146 guint Session::_name_id_counter = 0;
147
148 PBD::Signal1<int,uint32_t> Session::AudioEngineSetupRequired;
149 PBD::Signal1<void,std::string> Session::Dialog;
150 PBD::Signal0<int> Session::AskAboutPendingState;
151 PBD::Signal2<int, samplecnt_t, samplecnt_t> Session::AskAboutSampleRateMismatch;
152 PBD::Signal2<void, samplecnt_t, samplecnt_t> Session::NotifyAboutSampleRateMismatch;
153 PBD::Signal0<void> Session::SendFeedback;
154 PBD::Signal3<int,Session*,std::string,DataType> Session::MissingFile;
155
156 PBD::Signal1<void, samplepos_t> Session::StartTimeChanged;
157 PBD::Signal1<void, samplepos_t> Session::EndTimeChanged;
158 PBD::Signal2<void,std::string, std::string> Session::Exported;
159 PBD::Signal1<int,boost::shared_ptr<Playlist> > Session::AskAboutPlaylistDeletion;
160 PBD::Signal0<void> Session::Quit;
161 PBD::Signal0<void> Session::FeedbackDetected;
162 PBD::Signal0<void> Session::SuccessfulGraphSort;
163 PBD::Signal2<void,std::string,std::string> Session::VersionMismatch;
164
165 const samplecnt_t Session::bounce_chunk_size = 8192;
166 static void clean_up_session_event (SessionEvent* ev) { delete ev; }
167 const SessionEvent::RTeventCallback Session::rt_cleanup (clean_up_session_event);
168 const uint32_t Session::session_end_shift = 0;
169
170 /** @param snapshot_name Snapshot name, without .ardour suffix */
171 Session::Session (AudioEngine &eng,
172                   const string& fullpath,
173                   const string& snapshot_name,
174                   BusProfile* bus_profile,
175                   string mix_template)
176         : _playlists (new SessionPlaylists)
177         , _engine (eng)
178         , process_function (&Session::process_with_events)
179         , _bounce_processing_active (false)
180         , waiting_for_sync_offset (false)
181         , _base_sample_rate (0)
182         , _nominal_sample_rate (0)
183         , _current_sample_rate (0)
184         , _record_status (Disabled)
185         , _transport_sample (0)
186         , _seek_counter (0)
187         , _session_range_location (0)
188         , _session_range_is_free (true)
189         , _silent (false)
190         , _remaining_latency_preroll (0)
191         , _engine_speed (1.0)
192         , _transport_speed (0)
193         , _default_transport_speed (1.0)
194         , _last_transport_speed (0)
195         , _signalled_varispeed (0)
196         , _target_transport_speed (0.0)
197         , auto_play_legal (false)
198         , _requested_return_sample (-1)
199         , current_block_size (0)
200         , _worst_output_latency (0)
201         , _worst_input_latency (0)
202         , _worst_route_latency (0)
203         , _send_latency_changes (0)
204         , _have_captured (false)
205         , _non_soloed_outs_muted (false)
206         , _listening (false)
207         , _listen_cnt (0)
208         , _solo_isolated_cnt (0)
209         , _writable (false)
210         , _was_seamless (Config->get_seamless_loop ())
211         , _under_nsm_control (false)
212         , _xrun_count (0)
213         , transport_master_tracking_state (Stopped)
214         , master_wait_end (0)
215         , post_export_sync (false)
216         , post_export_position (0)
217         , _exporting (false)
218         , _export_rolling (false)
219         , _realtime_export (false)
220         , _region_export (false)
221         , _export_preroll (0)
222         , _pre_export_mmc_enabled (false)
223         , _name (snapshot_name)
224         , _is_new (true)
225         , _send_qf_mtc (false)
226         , _pframes_since_last_mtc (0)
227         , play_loop (false)
228         , loop_changing (false)
229         , last_loopend (0)
230         , _session_dir (new SessionDirectory (fullpath))
231         , _current_snapshot_name (snapshot_name)
232         , state_tree (0)
233         , state_was_pending (false)
234         , _state_of_the_state (StateOfTheState (CannotSave | InitialConnecting | Loading))
235         , _suspend_save (0)
236         , _save_queued (false)
237         , _last_roll_location (0)
238         , _last_roll_or_reversal_location (0)
239         , _last_record_location (0)
240         , pending_auto_loop (false)
241         , _mempool ("Session", 3145728)
242         , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
243         , _n_lua_scripts (0)
244         , _butler (new Butler (*this))
245         , _transport_fsm (new TransportFSM (*this))
246         , _post_transport_work (0)
247         , _locations (new Locations (*this))
248         , _ignore_skips_updates (false)
249         , _rt_thread_active (false)
250         , _rt_emit_pending (false)
251         , _ac_thread_active (0)
252         , _latency_recompute_pending (0)
253         , step_speed (0)
254         , outbound_mtc_timecode_frame (0)
255         , next_quarter_frame_to_send (-1)
256         , _samples_per_timecode_frame (0)
257         , _frames_per_hour (0)
258         , _timecode_frames_per_hour (0)
259         , last_timecode_valid (false)
260         , last_timecode_when (0)
261         , _send_timecode_update (false)
262         , ltc_encoder (0)
263         , ltc_enc_buf(0)
264         , ltc_buf_off (0)
265         , ltc_buf_len (0)
266         , ltc_speed (0)
267         , ltc_enc_byte (0)
268         , ltc_enc_pos (0)
269         , ltc_enc_cnt (0)
270         , ltc_enc_off (0)
271         , restarting (false)
272         , ltc_prev_cycle (0)
273         , ltc_timecode_offset (0)
274         , ltc_timecode_negative_offset (false)
275         , midi_control_ui (0)
276         , _tempo_map (0)
277         , _all_route_group (new RouteGroup (*this, "all"))
278         , routes (new RouteList)
279         , _adding_routes_in_progress (false)
280         , _reconnecting_routes_in_progress (false)
281         , _route_deletion_in_progress (false)
282         , destructive_index (0)
283         , _track_number_decimals(1)
284         , default_fade_steepness (0)
285         , default_fade_msecs (0)
286         , _total_free_4k_blocks (0)
287         , _total_free_4k_blocks_uncertain (false)
288         , no_questions_about_missing_files (false)
289         , _playback_load (0)
290         , _capture_load (0)
291         , _bundles (new BundleList)
292         , _bundle_xml_node (0)
293         , _current_trans (0)
294         , _clicking (false)
295         , _click_rec_only (false)
296         , click_data (0)
297         , click_emphasis_data (0)
298         , click_length (0)
299         , click_emphasis_length (0)
300         , _clicks_cleared (0)
301         , _count_in_samples (0)
302         , _play_range (false)
303         , _range_selection (-1,-1)
304         , _object_selection (-1,-1)
305         , _preroll_record_trim_len (0)
306         , _count_in_once (false)
307         , main_outs (0)
308         , first_file_data_format_reset (true)
309         , first_file_header_format_reset (true)
310         , have_looped (false)
311         , _have_rec_enabled_track (false)
312         , _have_rec_disabled_track (true)
313         , _step_editors (0)
314         , _suspend_timecode_transmission (0)
315         ,  _speakers (new Speakers)
316         , _ignore_route_processor_changes (0)
317         , midi_clock (0)
318         , _scene_changer (0)
319         , _midi_ports (0)
320         , _mmc (0)
321         , _vca_manager (new VCAManager (*this))
322         , _selection (new CoreSelection (*this))
323         , _global_locate_pending (false)
324 {
325         uint32_t sr = 0;
326
327         created_with = string_compose ("%1 %2", PROGRAM_NAME, revision);
328
329         pthread_mutex_init (&_rt_emit_mutex, 0);
330         pthread_cond_init (&_rt_emit_cond, 0);
331
332         pthread_mutex_init (&_auto_connect_mutex, 0);
333         pthread_cond_init (&_auto_connect_cond, 0);
334
335         init_name_id_counter (1); // reset for new sessions, start at 1
336         VCA::set_next_vca_number (1); // reset for new sessions, start at 1
337
338         pre_engine_init (fullpath); // sets _is_new
339
340         setup_lua ();
341
342         if (_is_new) {
343
344                 Stateful::loading_state_version = CURRENT_SESSION_FILE_VERSION;
345
346                 if (ensure_engine (sr, true)) {
347                         destroy ();
348                         throw SessionException (_("Cannot connect to audio/midi engine"));
349                 }
350
351                 // set samplerate for plugins added early
352                 // e.g from templates or MB channelstrip
353                 set_block_size (_engine.samples_per_cycle());
354                 set_sample_rate (_engine.sample_rate());
355
356                 if (create (mix_template, bus_profile)) {
357                         destroy ();
358                         throw SessionException (_("Session initialization failed"));
359                 }
360
361                 /* if a mix template was provided, then ::create() will
362                  * have copied it into the session and we need to load it
363                  * so that we have the state ready for ::set_state()
364                  * after the engine is started.
365                  *
366                  * Note that we do NOT try to get the sample rate from
367                  * the template at this time, though doing so would
368                  * be easy if we decided this was an appropriate part
369                  * of a template.
370                  */
371
372                 if (!mix_template.empty()) {
373                         try {
374                                 if (load_state (_current_snapshot_name)) {
375                                         throw SessionException (_("Failed to load template/snapshot state"));
376                                 }
377                         } catch (PBD::unknown_enumeration& e) {
378                                 throw SessionException (_("Failed to parse template/snapshot state"));
379                         }
380                         store_recent_templates (mix_template);
381                 }
382
383                 /* load default session properties - if any */
384                 config.load_state();
385
386         } else {
387
388                 if (load_state (_current_snapshot_name)) {
389                         throw SessionException (_("Failed to load state"));
390                 }
391
392                 /* try to get sample rate from XML state so that we
393                  * can influence the SR if we set up the audio
394                  * engine.
395                  */
396
397                 if (state_tree) {
398                         XMLProperty const * prop;
399                         XMLNode const * root (state_tree->root());
400                         if ((prop = root->property (X_("sample-rate"))) != 0) {
401                                 sr = atoi (prop->value());
402                         }
403                 }
404
405                 if (ensure_engine (sr, false)) {
406                         destroy ();
407                         throw SessionException (_("Cannot connect to audio/midi engine"));
408                 }
409         }
410
411         int err = post_engine_init ();
412         if (err) {
413                 destroy ();
414                 switch (err) {
415                         case -1:
416                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Failed to create background threads.")));
417                                 break;
418                         case -2:
419                         case -3:
420                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Invalid TempoMap in session-file.")));
421                                 break;
422                         case -4:
423                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Invalid or corrupt session state.")));
424                                 break;
425                         case -5:
426                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Port registration failed.")));
427                                 break;
428                         default:
429                                 throw SessionException (string_compose (_("Cannot initialize session/engine: %1"), _("Unexpected exception during session setup, possibly invalid audio/midi engine parameters. Please see stdout/stderr for details")));
430                                 break;
431                 }
432         }
433
434         store_recent_sessions (_name, _path);
435
436         bool was_dirty = dirty();
437         unset_dirty ();
438
439         PresentationInfo::Change.connect_same_thread (*this, boost::bind (&Session::notify_presentation_info_change, this));
440
441         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Session::config_changed, this, _1, false));
442         config.ParameterChanged.connect_same_thread (*this, boost::bind (&Session::config_changed, this, _1, true));
443
444         if (was_dirty) {
445                 DirtyChanged (); /* EMIT SIGNAL */
446         }
447
448         StartTimeChanged.connect_same_thread (*this, boost::bind (&Session::start_time_changed, this, _1));
449         EndTimeChanged.connect_same_thread (*this, boost::bind (&Session::end_time_changed, this, _1));
450
451         LatentSend::ChangedLatency.connect_same_thread (*this, boost::bind (&Session::send_latency_compensation_change, this));
452
453         emit_thread_start ();
454         auto_connect_thread_start ();
455
456         /* hook us up to the engine since we are now completely constructed */
457
458         BootMessage (_("Connect to engine"));
459
460         _engine.set_session (this);
461         _engine.reset_timebase ();
462
463         ensure_subdirs (); // archived or zipped sessions may lack peaks/ analysis/ etc
464
465         if (!mix_template.empty ()) {
466                 /* ::create() unsets _is_new after creating the session.
467                  * But for templated sessions, the sample-rate is initially unset
468                  * (not read from template), so we need to save it (again).
469                  */
470                 _is_new = true;
471         }
472
473         session_loaded ();
474         _is_new = false;
475
476         BootMessage (_("Session loading complete"));
477 }
478
479 Session::~Session ()
480 {
481 #ifdef PT_TIMING
482         ST.dump ("ST.dump");
483 #endif
484         destroy ();
485 }
486
487 unsigned int
488 Session::next_name_id ()
489 {
490         return g_atomic_int_add (&_name_id_counter, 1);
491 }
492
493 unsigned int
494 Session::name_id_counter ()
495 {
496         return g_atomic_int_get (&_name_id_counter);
497 }
498
499 void
500 Session::init_name_id_counter (guint n)
501 {
502         g_atomic_int_set (&_name_id_counter, n);
503 }
504
505 int
506 Session::ensure_engine (uint32_t desired_sample_rate, bool isnew)
507 {
508         if (_engine.current_backend() == 0) {
509                 /* backend is unknown ... */
510                 boost::optional<int> r = AudioEngineSetupRequired (desired_sample_rate);
511                 if (r.get_value_or (-1) != 0) {
512                         return -1;
513                 }
514         } else if (!isnew && _engine.running() && _engine.sample_rate () == desired_sample_rate) {
515                 /* keep engine */
516         } else if (_engine.setup_required()) {
517                 /* backend is known, but setup is needed */
518                 boost::optional<int> r = AudioEngineSetupRequired (desired_sample_rate);
519                 if (r.get_value_or (-1) != 0) {
520                         return -1;
521                 }
522         } else if (!_engine.running()) {
523                 if (_engine.start()) {
524                         return -1;
525                 }
526         }
527
528         /* at this point the engine should be running */
529
530         if (!_engine.running()) {
531                 return -1;
532         }
533
534         return immediately_post_engine ();
535
536 }
537
538 int
539 Session::immediately_post_engine ()
540 {
541         /* Do various initializations that should take place directly after we
542          * know that the engine is running, but before we either create a
543          * session or set state for an existing one.
544          */
545
546         _rt_tasklist.reset (new RTTaskList ());
547
548         if (how_many_dsp_threads () > 1) {
549                 /* For now, only create the graph if we are using >1 DSP threads, as
550                    it is a bit slower than the old code with 1 thread.
551                 */
552                 _process_graph.reset (new Graph (*this));
553         }
554
555         /* Restart transport FSM */
556
557         _transport_fsm->start ();
558
559         /* every time we reconnect, do stuff ... */
560
561         _engine.Running.connect_same_thread (*this, boost::bind (&Session::engine_running, this));
562
563         if (synced_to_engine()) {
564                 _engine.transport_stop ();
565         }
566
567         if (config.get_jack_time_master()) {
568                 _engine.transport_locate (_transport_sample);
569         }
570
571         try {
572                 BootMessage (_("Set up LTC"));
573                 setup_ltc ();
574                 BootMessage (_("Set up Click"));
575                 setup_click ();
576                 BootMessage (_("Set up standard connections"));
577                 setup_bundles ();
578         }
579
580         catch (failed_constructor& err) {
581                 return -1;
582         }
583
584         /* TODO, connect in different thread. (PortRegisteredOrUnregistered may be in RT context)
585          * can we do that? */
586          _engine.PortRegisteredOrUnregistered.connect_same_thread (*this, boost::bind (&Session::setup_bundles, this));
587
588         return 0;
589 }
590
591 void
592 Session::destroy ()
593 {
594         vector<void*> debug_pointers;
595
596         /* if we got to here, leaving pending capture state around
597            is a mistake.
598         */
599
600         remove_pending_capture_state ();
601
602         Analyser::flush ();
603
604         _state_of_the_state = StateOfTheState (CannotSave | Deletion);
605
606         {
607                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
608                 ltc_tx_cleanup();
609         }
610
611         /* disconnect from any and all signals that we are connected to */
612
613         Port::PortSignalDrop (); /* EMIT SIGNAL */
614         drop_connections ();
615
616         /* shutdown control surface protocols while we still have ports
617          * and the engine to move data to any devices.
618          */
619
620         /* remove I/O objects before unsetting the engine session */
621         _click_io.reset ();
622         _ltc_output.reset ();
623
624         ControlProtocolManager::instance().drop_protocols ();
625
626         /* stop auto dis/connecting */
627         auto_connect_thread_terminate ();
628
629         MIDI::Name::MidiPatchManager::instance().remove_search_path(session_directory().midi_patch_path());
630
631         _engine.remove_session ();
632
633         /* deregister all ports - there will be no process or any other
634          * callbacks from the engine any more.
635          */
636
637         Port::PortDrop (); /* EMIT SIGNAL */
638
639         {
640                 Glib::Threads::Mutex::Lock lm (controllables_lock);
641                 for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
642                         (*i)->DropReferences (); /* EMIT SIGNAL */
643                 }
644                 controllables.clear ();
645         }
646
647         /* clear history so that no references to objects are held any more */
648
649         _history.clear ();
650
651         /* clear state tree so that no references to objects are held any more */
652
653         delete state_tree;
654         state_tree = 0;
655
656         {
657                 /* unregister all lua functions, drop held references (if any) */
658                 Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK);
659                 (*_lua_cleanup)();
660                 lua.do_command ("Session = nil");
661                 delete _lua_run;
662                 delete _lua_add;
663                 delete _lua_del;
664                 delete _lua_list;
665                 delete _lua_save;
666                 delete _lua_load;
667                 delete _lua_cleanup;
668                 lua.collect_garbage ();
669         }
670
671         /* reset dynamic state version back to default */
672         Stateful::loading_state_version = 0;
673
674         _butler->drop_references ();
675         delete _butler;
676         _butler = 0;
677
678         delete _all_route_group;
679
680         DEBUG_TRACE (DEBUG::Destruction, "delete route groups\n");
681         for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
682                 delete *i;
683         }
684
685         if (click_data != default_click) {
686                 delete [] click_data;
687         }
688
689         if (click_emphasis_data != default_click_emphasis) {
690                 delete [] click_emphasis_data;
691         }
692
693         clear_clicks ();
694
695         /* need to remove auditioner before monitoring section
696          * otherwise it is re-connected.
697          * Note: If a session was never successfully loaded, there
698          * may not yet be an auditioner.
699          */
700         if (auditioner) {
701                 auditioner->drop_references ();
702         }
703         auditioner.reset ();
704
705         /* drop references to routes held by the monitoring section
706          * specifically _monitor_out aux/listen references */
707         remove_monitor_section();
708
709         /* clear out any pending dead wood from RCU managed objects */
710
711         routes.flush ();
712         _bundles.flush ();
713
714         DiskReader::free_working_buffers();
715
716         /* tell everyone who is still standing that we're about to die */
717         drop_references ();
718
719         /* tell everyone to drop references and delete objects as we go */
720
721         DEBUG_TRACE (DEBUG::Destruction, "delete regions\n");
722         RegionFactory::delete_all_regions ();
723
724         /* Do this early so that VCAs no longer hold references to routes */
725
726         DEBUG_TRACE (DEBUG::Destruction, "delete vcas\n");
727         delete _vca_manager;
728
729         DEBUG_TRACE (DEBUG::Destruction, "delete routes\n");
730
731         /* reset these three references to special routes before we do the usual route delete thing */
732
733         _master_out.reset ();
734         _monitor_out.reset ();
735
736         {
737                 RCUWriter<RouteList> writer (routes);
738                 boost::shared_ptr<RouteList> r = writer.get_copy ();
739
740                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
741                         DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for route %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
742                         (*i)->drop_references ();
743                 }
744
745                 r->clear ();
746                 /* writer goes out of scope and updates master */
747         }
748         routes.flush ();
749
750         {
751                 DEBUG_TRACE (DEBUG::Destruction, "delete sources\n");
752                 Glib::Threads::Mutex::Lock lm (source_lock);
753                 for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
754                         DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for source %1 ; pre-ref = %2\n", i->second->name(), i->second.use_count()));
755                         i->second->drop_references ();
756                 }
757
758                 sources.clear ();
759         }
760
761         /* not strictly necessary, but doing it here allows the shared_ptr debugging to work */
762         _playlists.reset ();
763
764         emit_thread_terminate ();
765
766         pthread_cond_destroy (&_rt_emit_cond);
767         pthread_mutex_destroy (&_rt_emit_mutex);
768
769         pthread_cond_destroy (&_auto_connect_cond);
770         pthread_mutex_destroy (&_auto_connect_mutex);
771
772         delete _scene_changer; _scene_changer = 0;
773         delete midi_control_ui; midi_control_ui = 0;
774
775         delete _mmc; _mmc = 0;
776         delete _midi_ports; _midi_ports = 0;
777         delete _locations; _locations = 0;
778
779         delete midi_clock;
780         delete _tempo_map;
781
782         /* clear event queue, the session is gone, nobody is interested in
783          * those anymore, but they do leak memory if not removed
784          */
785         while (!immediate_events.empty ()) {
786                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
787                 SessionEvent *ev = immediate_events.front ();
788                 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Drop event: %1\n", enum_2_string (ev->type)));
789                 immediate_events.pop_front ();
790                 bool remove = true;
791                 bool del = true;
792                 switch (ev->type) {
793                         case SessionEvent::AutoLoop:
794                         case SessionEvent::Skip:
795                         case SessionEvent::PunchIn:
796                         case SessionEvent::PunchOut:
797                         case SessionEvent::RangeStop:
798                         case SessionEvent::RangeLocate:
799                                 remove = false;
800                                 del = false;
801                                 break;
802                         case SessionEvent::RealTimeOperation:
803                                 process_rtop (ev);
804                                 del = false;
805                         default:
806                                 break;
807                 }
808                 if (remove) {
809                         del = del && !_remove_event (ev);
810                 }
811                 if (del) {
812                         delete ev;
813                 }
814         }
815
816         {
817                 /* unregister all dropped ports, process pending port deletion. */
818                 // this may call ARDOUR::Port::drop ... jack_port_unregister ()
819                 // jack1 cannot cope with removing ports while processing
820                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
821                 AudioEngine::instance()->clear_pending_port_deletions ();
822         }
823
824         DEBUG_TRACE (DEBUG::Destruction, "delete selection\n");
825         delete _selection;
826         _selection = 0;
827
828         _transport_fsm->stop ();
829
830         DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n");
831
832 #ifndef NDEBUG
833         Controllable::dump_registry ();
834 #endif
835
836         BOOST_SHOW_POINTERS ();
837 }
838
839 void
840 Session::setup_ltc ()
841 {
842         XMLNode* child = 0;
843
844         _ltc_output.reset (new IO (*this, X_("LTC Out"), IO::Output));
845
846         if (state_tree && (child = find_named_node (*state_tree->root(), X_("LTC Out"))) != 0) {
847                 _ltc_output->set_state (*(child->children().front()), Stateful::loading_state_version);
848         } else {
849                 {
850                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
851                         _ltc_output->ensure_io (ChanCount (DataType::AUDIO, 1), true, this);
852                         // TODO use auto-connect thread
853                         reconnect_ltc_output ();
854                 }
855         }
856
857         /* fix up names of LTC ports because we don't want the normal
858          * IO style of NAME/TYPE-{in,out}N
859          */
860
861         _ltc_output->nth (0)->set_name (X_("LTC-out"));
862 }
863
864 void
865 Session::setup_click ()
866 {
867         _clicking = false;
868
869         boost::shared_ptr<AutomationList> gl (new AutomationList (Evoral::Parameter (GainAutomation)));
870         boost::shared_ptr<GainControl> gain_control = boost::shared_ptr<GainControl> (new GainControl (*this, Evoral::Parameter(GainAutomation), gl));
871
872         _click_io.reset (new ClickIO (*this, X_("Click")));
873         _click_gain.reset (new Amp (*this, _("Fader"), gain_control, true));
874         _click_gain->activate ();
875         if (state_tree) {
876                 setup_click_state (state_tree->root());
877         } else {
878                 setup_click_state (0);
879         }
880 }
881
882 void
883 Session::setup_click_state (const XMLNode* node)
884 {
885         const XMLNode* child = 0;
886
887         if (node && (child = find_named_node (*node, "Click")) != 0) {
888
889                 /* existing state for Click */
890                 int c = 0;
891
892                 if (Stateful::loading_state_version < 3000) {
893                         c = _click_io->set_state_2X (*child->children().front(), Stateful::loading_state_version, false);
894                 } else {
895                         const XMLNodeList& children (child->children());
896                         XMLNodeList::const_iterator i = children.begin();
897                         if ((c = _click_io->set_state (**i, Stateful::loading_state_version)) == 0) {
898                                 ++i;
899                                 if (i != children.end()) {
900                                         c = _click_gain->set_state (**i, Stateful::loading_state_version);
901                                 }
902                         }
903                 }
904
905                 if (c == 0) {
906                         _clicking = Config->get_clicking ();
907
908                 } else {
909
910                         error << _("could not setup Click I/O") << endmsg;
911                         _clicking = false;
912                 }
913
914
915         } else {
916
917                 /* default state for Click: dual-mono to first 2 physical outputs */
918
919                 vector<string> outs;
920                 _engine.get_physical_outputs (DataType::AUDIO, outs);
921
922                 for (uint32_t physport = 0; physport < 2; ++physport) {
923                         if (outs.size() > physport) {
924                                 if (_click_io->add_port (outs[physport], this)) {
925                                         // relax, even though its an error
926                                 }
927                         }
928                 }
929
930                 if (_click_io->n_ports () > ChanCount::ZERO) {
931                         _clicking = Config->get_clicking ();
932                 }
933         }
934 }
935
936 void
937 Session::get_physical_ports (vector<string>& inputs, vector<string>& outputs, DataType type,
938                              MidiPortFlags include, MidiPortFlags exclude)
939 {
940         _engine.get_physical_inputs (type, inputs, include, exclude);
941         _engine.get_physical_outputs (type, outputs, include, exclude);
942 }
943
944
945 void
946 Session::auto_connect_master_bus ()
947 {
948         if (!_master_out || !Config->get_auto_connect_standard_busses() || _monitor_out) {
949                 return;
950         }
951
952         // Waves Tracks: Do not connect master bas for Tracks if AutoConnectMaster option is not set
953         // In this case it means "Multi Out" output mode
954         if (ARDOUR::Profile->get_trx() && !(Config->get_output_auto_connect() & AutoConnectMaster) ) {
955                 return;
956         }
957
958         /* if requested auto-connect the outputs to the first N physical ports.
959          */
960
961         uint32_t limit = _master_out->n_outputs().n_total();
962         vector<string> outputs[DataType::num_types];
963
964         for (uint32_t i = 0; i < DataType::num_types; ++i) {
965                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
966         }
967
968         for (uint32_t n = 0; n < limit; ++n) {
969                 boost::shared_ptr<Port> p = _master_out->output()->nth (n);
970                 string connect_to;
971                 if (outputs[p->type()].size() > n) {
972                         connect_to = outputs[p->type()][n];
973                 }
974
975                 if (!connect_to.empty() && p->connected_to (connect_to) == false) {
976                         if (_master_out->output()->connect (p, connect_to, this)) {
977                                 error << string_compose (_("cannot connect master output %1 to %2"), n, connect_to)
978                                       << endmsg;
979                                 break;
980                         }
981                 }
982         }
983 }
984
985 void
986 Session::remove_monitor_section ()
987 {
988         if (!_monitor_out || Profile->get_trx()) {
989                 return;
990         }
991
992         /* allow deletion when session is unloaded */
993         if (!_engine.running() && !deletion_in_progress ()) {
994                 error << _("Cannot remove monitor section while the engine is offline.") << endmsg;
995                 return;
996         }
997
998         /* force reversion to Solo-In-Place */
999         Config->set_solo_control_is_listen_control (false);
1000
1001         /* if we are auditioning, cancel it ... this is a workaround
1002            to a problem (auditioning does not execute the process graph,
1003            which is needed to remove routes when using >1 core for processing)
1004         */
1005         cancel_audition ();
1006
1007         if (!deletion_in_progress ()) {
1008                 /* Hold process lock while doing this so that we don't hear bits and
1009                  * pieces of audio as we work on each route.
1010                  */
1011
1012                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1013
1014                 /* Connect tracks to monitor section. Note that in an
1015                    existing session, the internal sends will already exist, but we want the
1016                    routes to notice that they connect to the control out specifically.
1017                 */
1018
1019
1020                 boost::shared_ptr<RouteList> r = routes.reader ();
1021                 ProcessorChangeBlocker  pcb (this, false);
1022
1023                 for (RouteList::iterator x = r->begin(); x != r->end(); ++x) {
1024
1025                         if ((*x)->is_monitor()) {
1026                                 /* relax */
1027                         } else if ((*x)->is_master()) {
1028                                 /* relax */
1029                         } else {
1030                                 (*x)->remove_aux_or_listen (_monitor_out);
1031                         }
1032                 }
1033         }
1034
1035         remove_route (_monitor_out);
1036         if (deletion_in_progress ()) {
1037                 return;
1038         }
1039
1040         auto_connect_master_bus ();
1041
1042         if (auditioner) {
1043                 auditioner->connect ();
1044         }
1045
1046         MonitorBusAddedOrRemoved (); /* EMIT SIGNAL */
1047 }
1048
1049 void
1050 Session::add_monitor_section ()
1051 {
1052         RouteList rl;
1053
1054         if (!_engine.running()) {
1055                 error << _("Cannot create monitor section while the engine is offline.") << endmsg;
1056                 return;
1057         }
1058
1059         if (_monitor_out || !_master_out || Profile->get_trx()) {
1060                 return;
1061         }
1062
1063         boost::shared_ptr<Route> r (new Route (*this, _("Monitor"), PresentationInfo::MonitorOut, DataType::AUDIO));
1064
1065         if (r->init ()) {
1066                 return;
1067         }
1068
1069         BOOST_MARK_ROUTE(r);
1070
1071         try {
1072                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1073                 r->input()->ensure_io (_master_out->output()->n_ports(), false, this);
1074                 r->output()->ensure_io (_master_out->output()->n_ports(), false, this);
1075         } catch (...) {
1076                 error << _("Cannot create monitor section. 'Monitor' Port name is not unique.") << endmsg;
1077                 return;
1078         }
1079
1080         rl.push_back (r);
1081         add_routes (rl, false, false, false, 0);
1082
1083         assert (_monitor_out);
1084
1085         /* AUDIO ONLY as of june 29th 2009, because listen semantics for anything else
1086            are undefined, at best.
1087         */
1088
1089         uint32_t limit = _monitor_out->n_inputs().n_audio();
1090
1091         if (_master_out) {
1092
1093                 /* connect the inputs to the master bus outputs. this
1094                  * represents a separate data feed from the internal sends from
1095                  * each route. as of jan 2011, it allows the monitor section to
1096                  * conditionally ignore either the internal sends or the normal
1097                  * input feed, but we should really find a better way to do
1098                  * this, i think.
1099                  */
1100
1101                 _master_out->output()->disconnect (this);
1102
1103                 for (uint32_t n = 0; n < limit; ++n) {
1104                         boost::shared_ptr<AudioPort> p = _monitor_out->input()->ports().nth_audio_port (n);
1105                         boost::shared_ptr<AudioPort> o = _master_out->output()->ports().nth_audio_port (n);
1106
1107                         if (o) {
1108                                 string connect_to = o->name();
1109                                 if (_monitor_out->input()->connect (p, connect_to, this)) {
1110                                         error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
1111                                               << endmsg;
1112                                         break;
1113                                 }
1114                         }
1115                 }
1116         }
1117
1118         /* if monitor section is not connected, connect it to physical outs
1119          */
1120
1121         if ((Config->get_auto_connect_standard_busses () || Profile->get_mixbus ()) && !_monitor_out->output()->connected ()) {
1122
1123                 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
1124
1125                         boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
1126
1127                         if (b) {
1128                                 _monitor_out->output()->connect_ports_to_bundle (b, true, this);
1129                         } else {
1130                                 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
1131                                                            Config->get_monitor_bus_preferred_bundle())
1132                                         << endmsg;
1133                         }
1134
1135                 } else {
1136
1137                         /* Monitor bus is audio only */
1138
1139                         vector<string> outputs[DataType::num_types];
1140
1141                         for (uint32_t i = 0; i < DataType::num_types; ++i) {
1142                                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
1143                         }
1144
1145                         uint32_t mod = outputs[DataType::AUDIO].size();
1146                         uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
1147
1148                         if (mod != 0) {
1149
1150                                 for (uint32_t n = 0; n < limit; ++n) {
1151
1152                                         boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
1153                                         string connect_to;
1154                                         if (outputs[DataType::AUDIO].size() > (n % mod)) {
1155                                                 connect_to = outputs[DataType::AUDIO][n % mod];
1156                                         }
1157
1158                                         if (!connect_to.empty()) {
1159                                                 if (_monitor_out->output()->connect (p, connect_to, this)) {
1160                                                         error << string_compose (
1161                                                                 _("cannot connect control output %1 to %2"),
1162                                                                 n, connect_to)
1163                                                               << endmsg;
1164                                                         break;
1165                                                 }
1166                                         }
1167                                 }
1168                         }
1169                 }
1170         }
1171
1172         /* Hold process lock while doing this so that we don't hear bits and
1173          * pieces of audio as we work on each route.
1174          */
1175
1176         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1177
1178         /* Connect tracks to monitor section. Note that in an
1179            existing session, the internal sends will already exist, but we want the
1180            routes to notice that they connect to the control out specifically.
1181         */
1182
1183
1184         boost::shared_ptr<RouteList> rls = routes.reader ();
1185
1186         ProcessorChangeBlocker  pcb (this, false /* XXX */);
1187
1188         for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
1189
1190                 if ((*x)->is_monitor()) {
1191                         /* relax */
1192                 } else if ((*x)->is_master()) {
1193                         /* relax */
1194                 } else {
1195                         (*x)->enable_monitor_send ();
1196                 }
1197         }
1198
1199         if (auditioner) {
1200                 auditioner->connect ();
1201         }
1202
1203         MonitorBusAddedOrRemoved (); /* EMIT SIGNAL */
1204 }
1205
1206 void
1207 Session::reset_monitor_section ()
1208 {
1209         /* Process lock should be held by the caller.*/
1210
1211         if (!_monitor_out || Profile->get_trx()) {
1212                 return;
1213         }
1214
1215         uint32_t limit = _master_out->n_outputs().n_audio();
1216
1217         /* connect the inputs to the master bus outputs. this
1218          * represents a separate data feed from the internal sends from
1219          * each route. as of jan 2011, it allows the monitor section to
1220          * conditionally ignore either the internal sends or the normal
1221          * input feed, but we should really find a better way to do
1222          * this, i think.
1223          */
1224
1225         _master_out->output()->disconnect (this);
1226         _monitor_out->output()->disconnect (this);
1227
1228         // monitor section follow master bus - except midi
1229         ChanCount mon_chn (_master_out->output()->n_ports());
1230         mon_chn.set_midi (0);
1231
1232         _monitor_out->input()->ensure_io (mon_chn, false, this);
1233         _monitor_out->output()->ensure_io (mon_chn, false, this);
1234
1235         for (uint32_t n = 0; n < limit; ++n) {
1236                 boost::shared_ptr<AudioPort> p = _monitor_out->input()->ports().nth_audio_port (n);
1237                 boost::shared_ptr<AudioPort> o = _master_out->output()->ports().nth_audio_port (n);
1238
1239                 if (o) {
1240                         string connect_to = o->name();
1241                         if (_monitor_out->input()->connect (p, connect_to, this)) {
1242                                 error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
1243                                       << endmsg;
1244                                 break;
1245                         }
1246                 }
1247         }
1248
1249         /* connect monitor section to physical outs
1250          */
1251
1252         if (Config->get_auto_connect_standard_busses()) {
1253
1254                 if (!Config->get_monitor_bus_preferred_bundle().empty()) {
1255
1256                         boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
1257
1258                         if (b) {
1259                                 _monitor_out->output()->connect_ports_to_bundle (b, true, this);
1260                         } else {
1261                                 warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
1262                                                            Config->get_monitor_bus_preferred_bundle())
1263                                         << endmsg;
1264                         }
1265
1266                 } else {
1267
1268                         /* Monitor bus is audio only */
1269
1270                         vector<string> outputs[DataType::num_types];
1271
1272                         for (uint32_t i = 0; i < DataType::num_types; ++i) {
1273                                 _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
1274                         }
1275
1276                         uint32_t mod = outputs[DataType::AUDIO].size();
1277                         uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
1278
1279                         if (mod != 0) {
1280
1281                                 for (uint32_t n = 0; n < limit; ++n) {
1282
1283                                         boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
1284                                         string connect_to;
1285                                         if (outputs[DataType::AUDIO].size() > (n % mod)) {
1286                                                 connect_to = outputs[DataType::AUDIO][n % mod];
1287                                         }
1288
1289                                         if (!connect_to.empty()) {
1290                                                 if (_monitor_out->output()->connect (p, connect_to, this)) {
1291                                                         error << string_compose (
1292                                                                 _("cannot connect control output %1 to %2"),
1293                                                                 n, connect_to)
1294                                                               << endmsg;
1295                                                         break;
1296                                                 }
1297                                         }
1298                                 }
1299                         }
1300                 }
1301         }
1302
1303         /* Connect tracks to monitor section. Note that in an
1304            existing session, the internal sends will already exist, but we want the
1305            routes to notice that they connect to the control out specifically.
1306         */
1307
1308
1309         boost::shared_ptr<RouteList> rls = routes.reader ();
1310
1311         ProcessorChangeBlocker pcb (this, false);
1312
1313         for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
1314
1315                 if ((*x)->is_monitor()) {
1316                         /* relax */
1317                 } else if ((*x)->is_master()) {
1318                         /* relax */
1319                 } else {
1320                         (*x)->enable_monitor_send ();
1321                 }
1322         }
1323 }
1324
1325 int
1326 Session::add_master_bus (ChanCount const& count)
1327 {
1328         if (master_out ()) {
1329                 return -1;
1330         }
1331
1332         RouteList rl;
1333
1334         boost::shared_ptr<Route> r (new Route (*this, _("Master"), PresentationInfo::MasterOut, DataType::AUDIO));
1335         if (r->init ()) {
1336                 return -1;
1337         }
1338
1339         BOOST_MARK_ROUTE(r);
1340
1341         {
1342                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1343                 r->input()->ensure_io (count, false, this);
1344                 r->output()->ensure_io (count, false, this);
1345         }
1346
1347         rl.push_back (r);
1348         add_routes (rl, false, false, false, PresentationInfo::max_order);
1349         return 0;
1350 }
1351
1352 void
1353 Session::hookup_io ()
1354 {
1355         /* stop graph reordering notifications from
1356            causing resorts, etc.
1357         */
1358
1359         _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
1360
1361         if (!auditioner) {
1362
1363                 /* we delay creating the auditioner till now because
1364                    it makes its own connections to ports.
1365                 */
1366
1367                 try {
1368                         boost::shared_ptr<Auditioner> a (new Auditioner (*this));
1369                         if (a->init()) {
1370                                 throw failed_constructor ();
1371                         }
1372                         auditioner = a;
1373                 }
1374
1375                 catch (failed_constructor& err) {
1376                         warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
1377                 }
1378         }
1379
1380         /* load bundles, which we may have postponed earlier on */
1381         if (_bundle_xml_node) {
1382                 load_bundles (*_bundle_xml_node);
1383                 delete _bundle_xml_node;
1384         }
1385
1386         /* Tell all IO objects to connect themselves together */
1387
1388         IO::enable_connecting ();
1389
1390         /* Now tell all "floating" ports to connect to whatever
1391            they should be connected to.
1392         */
1393
1394         AudioEngine::instance()->reconnect_ports ();
1395         TransportMasterManager::instance().reconnect_ports ();
1396
1397         /* Anyone who cares about input state, wake up and do something */
1398
1399         IOConnectionsComplete (); /* EMIT SIGNAL */
1400
1401         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
1402
1403         /* now handle the whole enchilada as if it was one
1404            graph reorder event.
1405         */
1406
1407         graph_reordered ();
1408
1409         /* update the full solo state, which can't be
1410            correctly determined on a per-route basis, but
1411            needs the global overview that only the session
1412            has.
1413         */
1414
1415         update_route_solo_state ();
1416 }
1417
1418 void
1419 Session::track_playlist_changed (boost::weak_ptr<Track> wp)
1420 {
1421         boost::shared_ptr<Track> track = wp.lock ();
1422         if (!track) {
1423                 return;
1424         }
1425
1426         boost::shared_ptr<Playlist> playlist;
1427
1428         if ((playlist = track->playlist()) != 0) {
1429                 playlist->RegionAdded.connect_same_thread (*this, boost::bind (&Session::playlist_region_added, this, _1));
1430                 playlist->RangesMoved.connect_same_thread (*this, boost::bind (&Session::playlist_ranges_moved, this, _1));
1431                 playlist->RegionsExtended.connect_same_thread (*this, boost::bind (&Session::playlist_regions_extended, this, _1));
1432         }
1433 }
1434
1435 bool
1436 Session::record_enabling_legal () const
1437 {
1438         if (Config->get_all_safe()) {
1439                 return false;
1440         }
1441         return true;
1442 }
1443
1444 void
1445 Session::set_track_monitor_input_status (bool yn)
1446 {
1447         boost::shared_ptr<RouteList> rl = routes.reader ();
1448         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1449                 boost::shared_ptr<AudioTrack> tr = boost::dynamic_pointer_cast<AudioTrack> (*i);
1450                 if (tr && tr->rec_enable_control()->get_value()) {
1451                         //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
1452                         tr->request_input_monitoring (yn);
1453                 }
1454         }
1455 }
1456
1457 void
1458 Session::auto_punch_start_changed (Location* location)
1459 {
1460         replace_event (SessionEvent::PunchIn, location->start());
1461
1462         if (get_record_enabled() && config.get_punch_in() && !actively_recording ()) {
1463                 /* capture start has been changed, so save new pending state */
1464                 save_state ("", true);
1465         }
1466 }
1467
1468 void
1469 Session::auto_punch_end_changed (Location* location)
1470 {
1471         replace_event (SessionEvent::PunchOut, location->end());
1472 }
1473
1474 void
1475 Session::auto_punch_changed (Location* location)
1476 {
1477         auto_punch_start_changed (location);
1478         auto_punch_end_changed (location);
1479 }
1480
1481 /** @param loc A loop location.
1482  *  @param pos Filled in with the start time of the required fade-out (in session samples).
1483  *  @param length Filled in with the length of the required fade-out.
1484  */
1485 void
1486 Session::auto_loop_declick_range (Location* loc, samplepos_t & pos, samplepos_t & length)
1487 {
1488         pos = max (loc->start(), loc->end() - 64);
1489         length = loc->end() - pos;
1490 }
1491
1492 void
1493 Session::auto_loop_changed (Location* location)
1494 {
1495         replace_event (SessionEvent::AutoLoop, location->end(), location->start());
1496         samplepos_t dcp;
1497         samplecnt_t dcl;
1498         auto_loop_declick_range (location, dcp, dcl);
1499
1500         bool rolling = transport_rolling ();
1501
1502         if (rolling && play_loop) {
1503
1504                 if (_transport_sample < location->start() || _transport_sample > location->end()) {
1505                         // relocate to beginning of loop
1506                         clear_events (SessionEvent::LocateRoll);
1507                         request_locate (location->start(), true);
1508
1509                 }
1510         } else {
1511                 clear_events (SessionEvent::AutoLoop);
1512         }
1513
1514         /* possibly move playhead if not rolling; if we are rolling we'll move
1515            to the loop start on stop if that is appropriate.
1516          */
1517
1518         samplepos_t pos;
1519
1520         if (!rolling && select_playhead_priority_target (pos)) {
1521                 if (pos == location->start()) {
1522                         request_locate (pos);
1523                 }
1524         }
1525
1526         last_loopend = location->end();
1527         set_dirty ();
1528 }
1529
1530 void
1531 Session::set_auto_punch_location (Location* location)
1532 {
1533         Location* existing;
1534
1535         if ((existing = _locations->auto_punch_location()) != 0 && existing != location) {
1536                 punch_connections.drop_connections();
1537                 existing->set_auto_punch (false, this);
1538                 clear_events (SessionEvent::PunchIn);
1539                 clear_events (SessionEvent::PunchOut);
1540                 auto_punch_location_changed (0);
1541         }
1542
1543         set_dirty();
1544
1545         if (location == 0) {
1546                 return;
1547         }
1548
1549         if (location->end() <= location->start()) {
1550                 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1551                 return;
1552         }
1553
1554         punch_connections.drop_connections ();
1555
1556         location->StartChanged.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_start_changed, this, location));
1557         location->EndChanged.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_end_changed, this, location));
1558         location->Changed.connect_same_thread (punch_connections, boost::bind (&Session::auto_punch_changed, this, location));
1559
1560         location->set_auto_punch (true, this);
1561
1562         auto_punch_changed (location);
1563
1564         auto_punch_location_changed (location);
1565 }
1566
1567 void
1568 Session::set_session_extents (samplepos_t start, samplepos_t end)
1569 {
1570         Location* existing;
1571         if ((existing = _locations->session_range_location()) == 0) {
1572                 //if there is no existing session, we need to make a new session location  (should never happen)
1573                 existing = new Location (*this, 0, 0, _("session"), Location::IsSessionRange, 0);
1574         }
1575
1576         if (end <= start) {
1577                 error << _("Session: you can't use that location for session start/end)") << endmsg;
1578                 return;
1579         }
1580
1581         existing->set( start, end );
1582
1583         set_dirty();
1584 }
1585
1586 void
1587 Session::set_auto_loop_location (Location* location)
1588 {
1589         Location* existing;
1590
1591         if ((existing = _locations->auto_loop_location()) != 0 && existing != location) {
1592                 loop_connections.drop_connections ();
1593                 existing->set_auto_loop (false, this);
1594                 remove_event (existing->end(), SessionEvent::AutoLoop);
1595                 samplepos_t dcp;
1596                 samplecnt_t dcl;
1597                 auto_loop_declick_range (existing, dcp, dcl);
1598                 auto_loop_location_changed (0);
1599         }
1600
1601         set_dirty();
1602
1603         if (location == 0) {
1604                 return;
1605         }
1606
1607         if (location->end() <= location->start()) {
1608                 error << _("You cannot use this location for auto-loop because it has zero or negative length") << endmsg;
1609                 return;
1610         }
1611
1612         last_loopend = location->end();
1613
1614         loop_connections.drop_connections ();
1615
1616         location->StartChanged.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1617         location->EndChanged.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1618         location->Changed.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1619         location->FlagsChanged.connect_same_thread (loop_connections, boost::bind (&Session::auto_loop_changed, this, location));
1620
1621         location->set_auto_loop (true, this);
1622
1623         if (Config->get_loop_is_mode() && play_loop && Config->get_seamless_loop()) {
1624                 // set all tracks to use internal looping
1625                 boost::shared_ptr<RouteList> rl = routes.reader ();
1626                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1627                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1628                         if (tr && !tr->is_private_route()) {
1629                                 tr->set_loop (location);
1630                         }
1631                 }
1632         }
1633
1634         /* take care of our stuff first */
1635
1636         auto_loop_changed (location);
1637
1638         /* now tell everyone else */
1639
1640         auto_loop_location_changed (location);
1641 }
1642
1643 void
1644 Session::update_marks (Location*)
1645 {
1646         set_dirty ();
1647 }
1648
1649 void
1650 Session::update_skips (Location* loc, bool consolidate)
1651 {
1652         if (_ignore_skips_updates) {
1653                 return;
1654         }
1655
1656         Locations::LocationList skips;
1657
1658         if (consolidate) {
1659                 PBD::Unwinder<bool> uw (_ignore_skips_updates, true);
1660                 consolidate_skips (loc);
1661         }
1662
1663         sync_locations_to_skips ();
1664
1665         set_dirty ();
1666 }
1667
1668 void
1669 Session::consolidate_skips (Location* loc)
1670 {
1671         Locations::LocationList all_locations = _locations->list ();
1672
1673         for (Locations::LocationList::iterator l = all_locations.begin(); l != all_locations.end(); ) {
1674
1675                 if (!(*l)->is_skip ()) {
1676                         ++l;
1677                         continue;
1678                 }
1679
1680                 /* don't test against self */
1681
1682                 if (*l == loc) {
1683                         ++l;
1684                         continue;
1685                 }
1686
1687                 switch (Evoral::coverage ((*l)->start(), (*l)->end(), loc->start(), loc->end())) {
1688                         case Evoral::OverlapInternal:
1689                         case Evoral::OverlapExternal:
1690                         case Evoral::OverlapStart:
1691                         case Evoral::OverlapEnd:
1692                                 /* adjust new location to cover existing one */
1693                                 loc->set_start (min (loc->start(), (*l)->start()));
1694                                 loc->set_end (max (loc->end(), (*l)->end()));
1695                                 /* we don't need this one any more */
1696                                 _locations->remove (*l);
1697                                 /* the location has been deleted, so remove reference to it in our local list */
1698                                 l = all_locations.erase (l);
1699                                 break;
1700
1701                         case Evoral::OverlapNone:
1702                                 ++l;
1703                                 break;
1704                 }
1705         }
1706 }
1707
1708 void
1709 Session::sync_locations_to_skips ()
1710 {
1711         /* This happens asynchronously (in the audioengine thread). After the clear is done, we will call
1712          * Session::_sync_locations_to_skips() from the audioengine thread.
1713          */
1714         clear_events (SessionEvent::Skip, boost::bind (&Session::_sync_locations_to_skips, this));
1715 }
1716
1717 void
1718 Session::_sync_locations_to_skips ()
1719 {
1720         /* called as a callback after existing Skip events have been cleared from a realtime audioengine thread */
1721
1722         Locations::LocationList const & locs (_locations->list());
1723
1724         for (Locations::LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1725
1726                 Location* location = *i;
1727
1728                 if (location->is_skip() && location->is_skipping()) {
1729                         SessionEvent* ev = new SessionEvent (SessionEvent::Skip, SessionEvent::Add, location->start(), location->end(), 1.0);
1730                         queue_event (ev);
1731                 }
1732         }
1733 }
1734
1735
1736 void
1737 Session::location_added (Location *location)
1738 {
1739         if (location->is_auto_punch()) {
1740                 set_auto_punch_location (location);
1741         }
1742
1743         if (location->is_auto_loop()) {
1744                 set_auto_loop_location (location);
1745         }
1746
1747         if (location->is_session_range()) {
1748                 /* no need for any signal handling or event setting with the session range,
1749                          because we keep a direct reference to it and use its start/end directly.
1750                          */
1751                 _session_range_location = location;
1752         }
1753
1754         if (location->is_mark()) {
1755                 /* listen for per-location signals that require us to do any * global updates for marks */
1756
1757                 location->StartChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1758                 location->EndChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1759                 location->Changed.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1760                 location->FlagsChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1761                 location->PositionLockStyleChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1762         }
1763
1764         if (location->is_range_marker()) {
1765                 /* listen for per-location signals that require us to do any * global updates for marks */
1766
1767                 location->StartChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1768                 location->EndChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1769                 location->Changed.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1770                 location->FlagsChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1771                 location->PositionLockStyleChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1772         }
1773
1774         if (location->is_skip()) {
1775                 /* listen for per-location signals that require us to update skip-locate events */
1776
1777                 location->StartChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, true));
1778                 location->EndChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, true));
1779                 location->Changed.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, true));
1780                 location->FlagsChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_skips, this, location, false));
1781                 location->PositionLockStyleChanged.connect_same_thread (skip_update_connections, boost::bind (&Session::update_marks, this, location));
1782
1783                 update_skips (location, true);
1784         }
1785
1786         set_dirty ();
1787 }
1788
1789 void
1790 Session::location_removed (Location *location)
1791 {
1792         if (location->is_auto_loop()) {
1793                 set_auto_loop_location (0);
1794                 if (!play_loop) {
1795                         set_track_loop (false);
1796                 }
1797                 unset_play_loop ();
1798         }
1799
1800         if (location->is_auto_punch()) {
1801                 set_auto_punch_location (0);
1802         }
1803
1804         if (location->is_session_range()) {
1805                 /* this is never supposed to happen */
1806                 error << _("programming error: session range removed!") << endl;
1807         }
1808
1809         if (location->is_skip()) {
1810
1811                 update_skips (location, false);
1812         }
1813
1814         set_dirty ();
1815 }
1816
1817 void
1818 Session::locations_changed ()
1819 {
1820         _locations->apply (*this, &Session::_locations_changed);
1821 }
1822
1823 void
1824 Session::_locations_changed (const Locations::LocationList& locations)
1825 {
1826         /* There was some mass-change in the Locations object.
1827          *
1828          * We might be re-adding a location here but it doesn't actually matter
1829          * for all the locations that the Session takes an interest in.
1830          */
1831
1832         {
1833                 PBD::Unwinder<bool> protect_ignore_skip_updates (_ignore_skips_updates, true);
1834                 for (Locations::LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1835                         location_added (*i);
1836                 }
1837         }
1838
1839         update_skips (NULL, false);
1840 }
1841
1842 void
1843 Session::enable_record ()
1844 {
1845         if (_transport_speed != 0.0 && _transport_speed != 1.0) {
1846                 /* no recording at anything except normal speed */
1847                 return;
1848         }
1849
1850         while (1) {
1851                 RecordState rs = (RecordState) g_atomic_int_get (&_record_status);
1852
1853                 if (rs == Recording) {
1854                         break;
1855                 }
1856
1857                 if (g_atomic_int_compare_and_exchange (&_record_status, rs, Recording)) {
1858
1859                         _last_record_location = _transport_sample;
1860                         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordStrobe));
1861
1862                         if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1863                                 set_track_monitor_input_status (true);
1864                         }
1865
1866                         RecordStateChanged ();
1867                         break;
1868                 }
1869         }
1870 }
1871
1872 void
1873 Session::set_all_tracks_record_enabled (bool enable )
1874 {
1875         boost::shared_ptr<RouteList> rl = routes.reader();
1876         set_controls (route_list_to_control_list (rl, &Stripable::rec_enable_control), enable, Controllable::NoGroup);
1877 }
1878
1879 void
1880 Session::disable_record (bool rt_context, bool force)
1881 {
1882         RecordState rs;
1883
1884         if ((rs = (RecordState) g_atomic_int_get (&_record_status)) != Disabled) {
1885
1886                 if (!Config->get_latched_record_enable () || force) {
1887                         g_atomic_int_set (&_record_status, Disabled);
1888                         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordExit));
1889                 } else {
1890                         if (rs == Recording) {
1891                                 g_atomic_int_set (&_record_status, Enabled);
1892                         }
1893                 }
1894
1895                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1896                         set_track_monitor_input_status (false);
1897                 }
1898
1899                 RecordStateChanged (); /* emit signal */
1900         }
1901 }
1902
1903 void
1904 Session::step_back_from_record ()
1905 {
1906         if (g_atomic_int_compare_and_exchange (&_record_status, Recording, Enabled)) {
1907
1908                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1909                         set_track_monitor_input_status (false);
1910                 }
1911
1912                 RecordStateChanged (); /* emit signal */
1913         }
1914 }
1915
1916 void
1917 Session::maybe_enable_record (bool rt_context)
1918 {
1919         if (_step_editors > 0) {
1920                 return;
1921         }
1922
1923         g_atomic_int_set (&_record_status, Enabled);
1924
1925         /* This function is currently called from somewhere other than an RT thread.
1926          * (except maybe lua scripts, which can use rt_context = true)
1927          * This save_state() call therefore doesn't impact anything.  Doing it here
1928          * means that we save pending state of which sources the next record will use,
1929          * which gives us some chance of recovering from a crash during the record.
1930          */
1931
1932         if (!rt_context) {
1933                 save_state ("", true);
1934         }
1935
1936         if (_transport_speed) {
1937                 if (!config.get_punch_in()) {
1938                         enable_record ();
1939                 }
1940         } else {
1941                 send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordPause));
1942                 RecordStateChanged (); /* EMIT SIGNAL */
1943         }
1944
1945         set_dirty();
1946 }
1947
1948 samplepos_t
1949 Session::audible_sample (bool* latent_locate) const
1950 {
1951         if (latent_locate) {
1952                 *latent_locate = false;
1953         }
1954
1955         samplepos_t ret;
1956
1957         if (synced_to_engine()) {
1958                 /* Note: this is basically just sync-to-JACK */
1959                 ret = _engine.transport_sample();
1960         } else {
1961                 ret = _transport_sample;
1962         }
1963
1964         assert (ret >= 0);
1965
1966         if (!transport_rolling()) {
1967                 return ret;
1968         }
1969
1970 #if 0 // TODO looping
1971         if (_transport_speed > 0.0f) {
1972                 if (play_loop && have_looped) {
1973                         /* the play-position wrapped at the loop-point
1974                          * ardour is already playing the beginning of the loop,
1975                          * but due to playback latency, the "audible frame"
1976                          * is still at the end of the loop.
1977                          */
1978                         Location *location = _locations->auto_loop_location();
1979                         sampleoffset_t lo = location->start() - ret;
1980                         if (lo > 0) {
1981                                 ret = location->end () - lo;
1982                                 if (latent_locate) {
1983                                         *latent_locate = true;
1984                                 }
1985                         }
1986                 }
1987         } else if (_transport_speed < 0.0f) {
1988                 /* XXX wot? no backward looping? */
1989         }
1990 #endif
1991
1992         return std::max ((samplepos_t)0, ret);
1993 }
1994
1995 samplecnt_t
1996 Session::preroll_samples (samplepos_t pos) const
1997 {
1998         const float pr = Config->get_preroll_seconds();
1999         if (pos >= 0 && pr < 0) {
2000                 const Tempo& tempo = _tempo_map->tempo_at_sample (pos);
2001                 const Meter& meter = _tempo_map->meter_at_sample (pos);
2002                 return meter.samples_per_bar (tempo, sample_rate()) * -pr;
2003         }
2004         if (pr < 0) {
2005                 return 0;
2006         }
2007         return pr * sample_rate();
2008 }
2009
2010 void
2011 Session::set_sample_rate (samplecnt_t frames_per_second)
2012 {
2013         /** \fn void Session::set_sample_size(samplecnt_t)
2014                 the AudioEngine object that calls this guarantees
2015                 that it will not be called while we are also in
2016                 ::process(). Its fine to do things that block
2017                 here.
2018         */
2019
2020         if (_base_sample_rate == 0) {
2021                 _base_sample_rate = frames_per_second;
2022         }
2023         else if (_base_sample_rate != frames_per_second && frames_per_second != _nominal_sample_rate) {
2024                 NotifyAboutSampleRateMismatch (_base_sample_rate, frames_per_second);
2025         }
2026         _nominal_sample_rate = frames_per_second;
2027
2028         sync_time_vars();
2029
2030         clear_clicks ();
2031         reset_write_sources (false);
2032
2033         // XXX we need some equivalent to this, somehow
2034         // SndFileSource::setup_standard_crossfades (frames_per_second);
2035
2036         set_dirty();
2037
2038         /* XXX need to reset/reinstantiate all LADSPA plugins */
2039 }
2040
2041 void
2042 Session::set_block_size (pframes_t nframes)
2043 {
2044         /* the AudioEngine guarantees
2045            that it will not be called while we are also in
2046            ::process(). It is therefore fine to do things that block
2047            here.
2048         */
2049
2050         {
2051                 current_block_size = nframes;
2052
2053                 ensure_buffers ();
2054
2055                 boost::shared_ptr<RouteList> r = routes.reader ();
2056
2057                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2058                         (*i)->set_block_size (nframes);
2059                 }
2060
2061                 boost::shared_ptr<RouteList> rl = routes.reader ();
2062                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2063                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2064                         if (tr) {
2065                                 tr->set_block_size (nframes);
2066                         }
2067                 }
2068
2069                 set_worst_io_latencies ();
2070         }
2071 }
2072
2073
2074 static void
2075 trace_terminal (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> rbase, bool sends_only)
2076 {
2077         boost::shared_ptr<Route> r2;
2078
2079         if (r1->feeds (rbase) && rbase->feeds (r1)) {
2080                 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
2081                 return;
2082         }
2083
2084         /* make a copy of the existing list of routes that feed r1 */
2085
2086         Route::FedBy existing (r1->fed_by());
2087
2088         /* for each route that feeds r1, recurse, marking it as feeding
2089            rbase as well.
2090         */
2091
2092         for (Route::FedBy::iterator i = existing.begin(); i != existing.end(); ++i) {
2093                 if (!(r2 = i->r.lock ())) {
2094                         /* (*i) went away, ignore it */
2095                         continue;
2096                 }
2097
2098                 /* r2 is a route that feeds r1 which somehow feeds base. mark
2099                    base as being fed by r2
2100                 */
2101
2102                 rbase->add_fed_by (r2, i->sends_only || sends_only);
2103
2104                 if (r2 != rbase) {
2105
2106                         /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
2107                            stop here.
2108                         */
2109
2110                         if (r1->feeds (r2) && r2->feeds (r1)) {
2111                                 continue;
2112                         }
2113
2114                         /* now recurse, so that we can mark base as being fed by
2115                            all routes that feed r2
2116                         */
2117
2118                         trace_terminal (r2, rbase, i->sends_only || sends_only);
2119                 }
2120
2121         }
2122 }
2123
2124 void
2125 Session::resort_routes ()
2126 {
2127         /* don't do anything here with signals emitted
2128            by Routes during initial setup or while we
2129            are being destroyed.
2130         */
2131
2132         if (inital_connect_or_deletion_in_progress ()) {
2133                 return;
2134         }
2135
2136         if (_route_deletion_in_progress) {
2137                 return;
2138         }
2139
2140         {
2141                 RCUWriter<RouteList> writer (routes);
2142                 boost::shared_ptr<RouteList> r = writer.get_copy ();
2143                 resort_routes_using (r);
2144                 /* writer goes out of scope and forces update */
2145         }
2146
2147 #ifndef NDEBUG
2148         if (DEBUG_ENABLED(DEBUG::Graph)) {
2149                 boost::shared_ptr<RouteList> rl = routes.reader ();
2150                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2151                         DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name()));
2152
2153                         const Route::FedBy& fb ((*i)->fed_by());
2154
2155                         for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) {
2156                                 boost::shared_ptr<Route> sf = f->r.lock();
2157                                 if (sf) {
2158                                         DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only));
2159                                 }
2160                         }
2161                 }
2162         }
2163 #endif
2164
2165 }
2166
2167 /** This is called whenever we need to rebuild the graph of how we will process
2168  *  routes.
2169  *  @param r List of routes, in any order.
2170  */
2171
2172 void
2173 Session::resort_routes_using (boost::shared_ptr<RouteList> r)
2174 {
2175         /* We are going to build a directed graph of our routes;
2176            this is where the edges of that graph are put.
2177         */
2178
2179         GraphEdges edges;
2180
2181         /* Go through all routes doing two things:
2182          *
2183          * 1. Collect the edges of the route graph.  Each of these edges
2184          *    is a pair of routes, one of which directly feeds the other
2185          *    either by a JACK connection or by an internal send.
2186          *
2187          * 2. Begin the process of making routes aware of which other
2188          *    routes directly or indirectly feed them.  This information
2189          *    is used by the solo code.
2190          */
2191
2192         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2193
2194                 /* Clear out the route's list of direct or indirect feeds */
2195                 (*i)->clear_fed_by ();
2196
2197                 for (RouteList::iterator j = r->begin(); j != r->end(); ++j) {
2198
2199                         bool via_sends_only;
2200
2201                         /* See if this *j feeds *i according to the current state of the JACK
2202                            connections and internal sends.
2203                         */
2204                         if ((*j)->direct_feeds_according_to_reality (*i, &via_sends_only)) {
2205                                 /* add the edge to the graph (part #1) */
2206                                 edges.add (*j, *i, via_sends_only);
2207                                 /* tell the route (for part #2) */
2208                                 (*i)->add_fed_by (*j, via_sends_only);
2209                         }
2210                 }
2211         }
2212
2213         /* Attempt a topological sort of the route graph */
2214         boost::shared_ptr<RouteList> sorted_routes = topological_sort (r, edges);
2215
2216         if (sorted_routes) {
2217                 /* We got a satisfactory topological sort, so there is no feedback;
2218                    use this new graph.
2219
2220                    Note: the process graph rechain does not require a
2221                    topologically-sorted list, but hey ho.
2222                 */
2223                 if (_process_graph) {
2224                         _process_graph->rechain (sorted_routes, edges);
2225                 }
2226
2227                 _current_route_graph = edges;
2228
2229                 /* Complete the building of the routes' lists of what directly
2230                    or indirectly feeds them.
2231                 */
2232                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2233                         trace_terminal (*i, *i, false);
2234                 }
2235
2236                 *r = *sorted_routes;
2237
2238 #ifndef NDEBUG
2239                 DEBUG_TRACE (DEBUG::Graph, "Routes resorted, order follows:\n");
2240                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2241                         DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 presentation order %2\n", (*i)->name(), (*i)->presentation_info().order()));
2242                 }
2243 #endif
2244
2245                 SuccessfulGraphSort (); /* EMIT SIGNAL */
2246
2247         } else {
2248                 /* The topological sort failed, so we have a problem.  Tell everyone
2249                    and stick to the old graph; this will continue to be processed, so
2250                    until the feedback is fixed, what is played back will not quite
2251                    reflect what is actually connected.  Note also that we do not
2252                    do trace_terminal here, as it would fail due to an endless recursion,
2253                    so the solo code will think that everything is still connected
2254                    as it was before.
2255                 */
2256
2257                 FeedbackDetected (); /* EMIT SIGNAL */
2258         }
2259
2260 }
2261
2262 /** Find a route name starting with \a base, maybe followed by the
2263  *  lowest \a id.  \a id will always be added if \a definitely_add_number
2264  *  is true on entry; otherwise it will only be added if required
2265  *  to make the name unique.
2266  *
2267  *  Names are constructed like e.g. "Audio 3" for base="Audio" and id=3.
2268  *  The available route name with the lowest ID will be used, and \a id
2269  *  will be set to the ID.
2270  *
2271  *  \return false if a route name could not be found, and \a track_name
2272  *  and \a id do not reflect a free route name.
2273  */
2274 bool
2275 Session::find_route_name (string const & base, uint32_t& id, string& name, bool definitely_add_number)
2276 {
2277         /* the base may conflict with ports that do not belong to existing
2278            routes, but hidden objects like the click track. So check port names
2279            before anything else.
2280         */
2281
2282         for (map<string,bool>::const_iterator reserved = reserved_io_names.begin(); reserved != reserved_io_names.end(); ++reserved) {
2283                 if (base == reserved->first) {
2284                         /* Check if this reserved name already exists, and if
2285                            so, disallow it without a numeric suffix.
2286                         */
2287                         if (!reserved->second || route_by_name (reserved->first)) {
2288                                 definitely_add_number = true;
2289                                 if (id < 1) {
2290                                         id = 1;
2291                                 }
2292                         }
2293                         break;
2294                 }
2295         }
2296
2297         /* if we have "base 1" already, it doesn't make sense to add "base"
2298          * if "base 1" has been deleted, adding "base" is no worse than "base 1"
2299          */
2300         if (!definitely_add_number && route_by_name (base) == 0 && (route_by_name (string_compose("%1 1", base)) == 0)) {
2301                 /* just use the base */
2302                 name = base;
2303                 return true;
2304         }
2305
2306         do {
2307                 name = string_compose ("%1 %2", base, id);
2308
2309                 if (route_by_name (name) == 0) {
2310                         return true;
2311                 }
2312
2313                 ++id;
2314
2315         } while (id < (UINT_MAX-1));
2316
2317         return false;
2318 }
2319
2320 /** Count the total ins and outs of all non-hidden tracks in the session and return them in in and out */
2321 void
2322 Session::count_existing_track_channels (ChanCount& in, ChanCount& out)
2323 {
2324         in  = ChanCount::ZERO;
2325         out = ChanCount::ZERO;
2326
2327         boost::shared_ptr<RouteList> r = routes.reader ();
2328
2329         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2330                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2331                 if (!tr) {
2332                         continue;
2333                 }
2334                 assert (!tr->is_auditioner()); // XXX remove me
2335                 in  += tr->n_inputs();
2336                 out += tr->n_outputs();
2337         }
2338 }
2339
2340 string
2341 Session::default_track_name_pattern (DataType t)
2342 {
2343         switch (t) {
2344         case DataType::AUDIO:
2345                 if (Profile->get_trx()) {
2346                         return _("Track ");
2347                 } else {
2348                         return _("Audio ");
2349                 }
2350                 break;
2351
2352         case DataType::MIDI:
2353                 return _("MIDI ");
2354         }
2355
2356         return "";
2357 }
2358
2359 /** Caller must not hold process lock
2360  *  @param name_template string to use for the start of the name, or "" to use "MIDI".
2361  *  @param instrument plugin info for the instrument to insert pre-fader, if any
2362  */
2363 list<boost::shared_ptr<MidiTrack> >
2364 Session::new_midi_track (const ChanCount& input, const ChanCount& output, bool strict_io,
2365                          boost::shared_ptr<PluginInfo> instrument, Plugin::PresetRecord* pset,
2366                          RouteGroup* route_group, uint32_t how_many,
2367                          string name_template, PresentationInfo::order_t order,
2368                          TrackMode mode)
2369 {
2370         string track_name;
2371         uint32_t track_id = 0;
2372         string port;
2373         RouteList new_routes;
2374         list<boost::shared_ptr<MidiTrack> > ret;
2375
2376         const string name_pattern = default_track_name_pattern (DataType::MIDI);
2377         bool const use_number = (how_many != 1) || name_template.empty () || (name_template == name_pattern);
2378
2379         while (how_many) {
2380                 if (!find_route_name (name_template.empty() ? _("MIDI") : name_template, ++track_id, track_name, use_number)) {
2381                         error << "cannot find name for new midi track" << endmsg;
2382                         goto failed;
2383                 }
2384
2385                 boost::shared_ptr<MidiTrack> track;
2386
2387                 try {
2388                         track.reset (new MidiTrack (*this, track_name, mode));
2389
2390                         if (track->init ()) {
2391                                 goto failed;
2392                         }
2393
2394                         if (strict_io) {
2395                                 track->set_strict_io (true);
2396                         }
2397
2398                         BOOST_MARK_TRACK (track);
2399
2400                         {
2401                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2402                                 if (track->input()->ensure_io (input, false, this)) {
2403                                         error << "cannot configure " << input << " out configuration for new midi track" << endmsg;
2404                                         goto failed;
2405                                 }
2406
2407                                 if (track->output()->ensure_io (output, false, this)) {
2408                                         error << "cannot configure " << output << " out configuration for new midi track" << endmsg;
2409                                         goto failed;
2410                                 }
2411                         }
2412
2413                         if (route_group) {
2414                                 route_group->add (track);
2415                         }
2416
2417                         new_routes.push_back (track);
2418                         ret.push_back (track);
2419                 }
2420
2421                 catch (failed_constructor &err) {
2422                         error << _("Session: could not create new midi track.") << endmsg;
2423                         goto failed;
2424                 }
2425
2426                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2427
2428                         error << string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with more ports if you need this many tracks."), PROGRAM_NAME) << endmsg;
2429                         goto failed;
2430                 }
2431
2432                 --how_many;
2433         }
2434
2435         failed:
2436         if (!new_routes.empty()) {
2437                 StateProtector sp (this);
2438                 if (Profile->get_trx()) {
2439                         add_routes (new_routes, false, false, false, order);
2440                 } else {
2441                         add_routes (new_routes, true, true, false, order);
2442                 }
2443
2444                 if (instrument) {
2445                         for (RouteList::iterator r = new_routes.begin(); r != new_routes.end(); ++r) {
2446                                 PluginPtr plugin = instrument->load (*this);
2447                                 if (!plugin) {
2448                                         warning << "Failed to add Synth Plugin to newly created track." << endmsg;
2449                                         continue;
2450                                 }
2451                                 if (pset) {
2452                                         plugin->load_preset (*pset);
2453                                 }
2454                                 boost::shared_ptr<PluginInsert> pi (new PluginInsert (*this, plugin));
2455                                 if (strict_io) {
2456                                         pi->set_strict_io (true);
2457                                 }
2458
2459                                 (*r)->add_processor (pi, PreFader);
2460
2461                                 if (Profile->get_mixbus () && pi->configured () && pi->output_streams().n_audio() > 2) {
2462                                         (*r)->move_instrument_down (false);
2463                                 }
2464                         }
2465                 }
2466         }
2467
2468         return ret;
2469 }
2470
2471 RouteList
2472 Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name_template, bool strict_io,
2473                          boost::shared_ptr<PluginInfo> instrument, Plugin::PresetRecord* pset,
2474                          PresentationInfo::Flag flag, PresentationInfo::order_t order)
2475 {
2476         string bus_name;
2477         uint32_t bus_id = 0;
2478         string port;
2479         RouteList ret;
2480
2481         bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Midi Bus");
2482
2483         while (how_many) {
2484                 if (!find_route_name (name_template.empty () ? _("Midi Bus") : name_template, ++bus_id, bus_name, use_number)) {
2485                         error << "cannot find name for new midi bus" << endmsg;
2486                         goto failure;
2487                 }
2488
2489                 try {
2490                         boost::shared_ptr<Route> bus (new Route (*this, bus_name, flag, DataType::AUDIO)); // XXX Editor::add_routes is not ready for ARDOUR::DataType::MIDI
2491
2492                         if (bus->init ()) {
2493                                 goto failure;
2494                         }
2495
2496                         if (strict_io) {
2497                                 bus->set_strict_io (true);
2498                         }
2499
2500                         BOOST_MARK_ROUTE(bus);
2501
2502                         {
2503                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2504
2505                                 if (bus->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
2506                                         error << _("cannot configure new midi bus input") << endmsg;
2507                                         goto failure;
2508                                 }
2509
2510
2511                                 if (bus->output()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
2512                                         error << _("cannot configure new midi bus output") << endmsg;
2513                                         goto failure;
2514                                 }
2515                         }
2516
2517                         if (route_group) {
2518                                 route_group->add (bus);
2519                         }
2520
2521                         bus->add_internal_return ();
2522                         ret.push_back (bus);
2523                 }
2524
2525                 catch (failed_constructor &err) {
2526                         error << _("Session: could not create new audio route.") << endmsg;
2527                         goto failure;
2528                 }
2529
2530                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2531                         error << pfe.what() << endmsg;
2532                         goto failure;
2533                 }
2534
2535
2536                 --how_many;
2537         }
2538
2539         failure:
2540         if (!ret.empty()) {
2541                 StateProtector sp (this);
2542                 add_routes (ret, false, false, false, order);
2543
2544                 if (instrument) {
2545                         for (RouteList::iterator r = ret.begin(); r != ret.end(); ++r) {
2546                                 PluginPtr plugin = instrument->load (*this);
2547                                 if (!plugin) {
2548                                         warning << "Failed to add Synth Plugin to newly created track." << endmsg;
2549                                         continue;
2550                                 }
2551                                 if (pset) {
2552                                         plugin->load_preset (*pset);
2553                                 }
2554                                 boost::shared_ptr<PluginInsert> pi (new PluginInsert (*this, plugin));
2555                                 if (strict_io) {
2556                                         pi->set_strict_io (true);
2557                                 }
2558
2559                                 (*r)->add_processor (pi, PreFader);
2560
2561                                 if (Profile->get_mixbus () && pi->configured () && pi->output_streams().n_audio() > 2) {
2562                                         (*r)->move_instrument_down (false);
2563                                 }
2564                         }
2565                 }
2566         }
2567
2568         return ret;
2569
2570 }
2571
2572
2573 void
2574 Session::midi_output_change_handler (IOChange change, void * /*src*/, boost::weak_ptr<Route> wmt)
2575 {
2576         boost::shared_ptr<Route> midi_track (wmt.lock());
2577
2578         if (!midi_track) {
2579                 return;
2580         }
2581
2582         if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) {
2583
2584                 if (change.after.n_audio() <= change.before.n_audio()) {
2585                         return;
2586                 }
2587
2588                 /* new audio ports: make sure the audio goes somewhere useful,
2589                  * unless the user has no-auto-connect selected.
2590                  *
2591                  * The existing ChanCounts don't matter for this call as they are only
2592                  * to do with matching input and output indices, and we are only changing
2593                  * outputs here.
2594                  */
2595                 auto_connect_route (midi_track, false, ChanCount(), change.before);
2596         }
2597 }
2598
2599 bool
2600 Session::ensure_stripable_sort_order ()
2601 {
2602         StripableList sl;
2603         get_stripables (sl);
2604         sl.sort (Stripable::Sorter ());
2605
2606         bool change = false;
2607         PresentationInfo::order_t order = 0;
2608
2609         for (StripableList::iterator si = sl.begin(); si != sl.end(); ++si) {
2610                 boost::shared_ptr<Stripable> s (*si);
2611                 assert (!s->is_auditioner ()); // XXX remove me
2612                 if (s->is_monitor ()) {
2613                         continue;
2614                 }
2615                 if (order != s->presentation_info().order()) {
2616                         s->set_presentation_order (order);
2617                         change = true;
2618                 }
2619                 ++order;
2620         }
2621         return change;
2622 }
2623
2624 void
2625 Session::ensure_route_presentation_info_gap (PresentationInfo::order_t first_new_order, uint32_t how_many)
2626 {
2627         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("ensure order gap starting at %1 for %2\n", first_new_order, how_many));
2628
2629         if (first_new_order == PresentationInfo::max_order) {
2630                 /* adding at end, no worries */
2631                 return;
2632         }
2633
2634         /* create a gap in the presentation info to accomodate @param how_many
2635          * new objects.
2636          */
2637         StripableList sl;
2638         get_stripables (sl);
2639
2640         for (StripableList::iterator si = sl.begin(); si != sl.end(); ++si) {
2641                 boost::shared_ptr<Stripable> s (*si);
2642
2643                 if (s->presentation_info().special (false)) {
2644                         continue;
2645                 }
2646
2647                 if (!s->presentation_info().order_set()) {
2648                         continue;
2649                 }
2650
2651                 if (s->presentation_info().order () >= first_new_order) {
2652                         s->set_presentation_order (s->presentation_info().order () + how_many);
2653                 }
2654         }
2655 }
2656
2657 /** Caller must not hold process lock
2658  *  @param name_template string to use for the start of the name, or "" to use "Audio".
2659  */
2660 list< boost::shared_ptr<AudioTrack> >
2661 Session::new_audio_track (int input_channels, int output_channels, RouteGroup* route_group,
2662                           uint32_t how_many, string name_template, PresentationInfo::order_t order,
2663                           TrackMode mode)
2664 {
2665         string track_name;
2666         uint32_t track_id = 0;
2667         string port;
2668         RouteList new_routes;
2669         list<boost::shared_ptr<AudioTrack> > ret;
2670
2671         const string name_pattern = default_track_name_pattern (DataType::AUDIO);
2672         bool const use_number = (how_many != 1) || name_template.empty () || (name_template == name_pattern);
2673
2674         while (how_many) {
2675
2676                 if (!find_route_name (name_template.empty() ? _(name_pattern.c_str()) : name_template, ++track_id, track_name, use_number)) {
2677                         error << "cannot find name for new audio track" << endmsg;
2678                         goto failed;
2679                 }
2680
2681                 boost::shared_ptr<AudioTrack> track;
2682
2683                 try {
2684                         track.reset (new AudioTrack (*this, track_name, mode));
2685
2686                         if (track->init ()) {
2687                                 goto failed;
2688                         }
2689
2690                         if (Profile->get_mixbus ()) {
2691                                 track->set_strict_io (true);
2692                         }
2693
2694                         if (ARDOUR::Profile->get_trx ()) {
2695                                 // TRACKS considers it's not a USE CASE, it's
2696                                 // a piece of behavior of the session model:
2697                                 //
2698                                 // Gain for a newly created route depends on
2699                                 // the current output_auto_connect mode:
2700                                 //
2701                                 //  0 for Stereo Out mode
2702                                 //  0 Multi Out mode
2703                                 if (Config->get_output_auto_connect() & AutoConnectMaster) {
2704                                         track->gain_control()->set_value (dB_to_coefficient (0), Controllable::NoGroup);
2705                                 }
2706                         }
2707
2708                         BOOST_MARK_TRACK (track);
2709
2710                         {
2711                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2712
2713                                 if (track->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
2714                                         error << string_compose (
2715                                                 _("cannot configure %1 in/%2 out configuration for new audio track"),
2716                                                 input_channels, output_channels)
2717                                               << endmsg;
2718                                         goto failed;
2719                                 }
2720
2721                                 if (track->output()->ensure_io (ChanCount(DataType::AUDIO, output_channels), false, this)) {
2722                                         error << string_compose (
2723                                                 _("cannot configure %1 in/%2 out configuration for new audio track"),
2724                                                 input_channels, output_channels)
2725                                               << endmsg;
2726                                         goto failed;
2727                                 }
2728                         }
2729
2730                         if (route_group) {
2731                                 route_group->add (track);
2732                         }
2733
2734                         new_routes.push_back (track);
2735                         ret.push_back (track);
2736                 }
2737
2738                 catch (failed_constructor &err) {
2739                         error << _("Session: could not create new audio track.") << endmsg;
2740                         goto failed;
2741                 }
2742
2743                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2744
2745                         error << pfe.what() << endmsg;
2746                         goto failed;
2747                 }
2748
2749                 --how_many;
2750         }
2751
2752         failed:
2753         if (!new_routes.empty()) {
2754                 StateProtector sp (this);
2755                 if (Profile->get_trx()) {
2756                         add_routes (new_routes, false, false, false, order);
2757                 } else {
2758                         add_routes (new_routes, true, true, false, order);
2759                 }
2760         }
2761
2762         return ret;
2763 }
2764
2765 /** Caller must not hold process lock.
2766  *  @param name_template string to use for the start of the name, or "" to use "Bus".
2767  */
2768 RouteList
2769 Session::new_audio_route (int input_channels, int output_channels, RouteGroup* route_group, uint32_t how_many, string name_template,
2770                           PresentationInfo::Flag flags, PresentationInfo::order_t order)
2771 {
2772         string bus_name;
2773         uint32_t bus_id = 0;
2774         string port;
2775         RouteList ret;
2776
2777         bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Bus");
2778
2779         while (how_many) {
2780                 if (!find_route_name (name_template.empty () ? _("Bus") : name_template, ++bus_id, bus_name, use_number)) {
2781                         error << "cannot find name for new audio bus" << endmsg;
2782                         goto failure;
2783                 }
2784
2785                 try {
2786                         boost::shared_ptr<Route> bus (new Route (*this, bus_name, flags, DataType::AUDIO));
2787
2788                         if (bus->init ()) {
2789                                 goto failure;
2790                         }
2791
2792                         if (Profile->get_mixbus ()) {
2793                                 bus->set_strict_io (true);
2794                         }
2795
2796                         BOOST_MARK_ROUTE(bus);
2797
2798                         {
2799                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2800
2801                                 if (bus->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
2802                                         error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
2803                                                                  input_channels, output_channels)
2804                                               << endmsg;
2805                                         goto failure;
2806                                 }
2807
2808
2809                                 if (bus->output()->ensure_io (ChanCount(DataType::AUDIO, output_channels), false, this)) {
2810                                         error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
2811                                                                  input_channels, output_channels)
2812                                               << endmsg;
2813                                         goto failure;
2814                                 }
2815                         }
2816
2817                         if (route_group) {
2818                                 route_group->add (bus);
2819                         }
2820
2821                         bus->add_internal_return ();
2822                         ret.push_back (bus);
2823                 }
2824
2825                 catch (failed_constructor &err) {
2826                         error << _("Session: could not create new audio route.") << endmsg;
2827                         goto failure;
2828                 }
2829
2830                 catch (AudioEngine::PortRegistrationFailure& pfe) {
2831                         error << pfe.what() << endmsg;
2832                         goto failure;
2833                 }
2834
2835
2836                 --how_many;
2837         }
2838
2839         failure:
2840         if (!ret.empty()) {
2841                 StateProtector sp (this);
2842                 if (Profile->get_trx()) {
2843                         add_routes (ret, false, false, false, order);
2844                 } else if (flags == PresentationInfo::FoldbackBus) {
2845                         add_routes (ret, false, false, true, order); // no autoconnect
2846                 } else {
2847                         add_routes (ret, false, true, true, order); // autoconnect // outputs only
2848                 }
2849         }
2850
2851         return ret;
2852
2853 }
2854
2855 RouteList
2856 Session::new_route_from_template (uint32_t how_many, PresentationInfo::order_t insert_at, const std::string& template_path, const std::string& name_base,
2857                                   PlaylistDisposition pd)
2858 {
2859         XMLTree tree;
2860
2861         if (!tree.read (template_path.c_str())) {
2862                 return RouteList();
2863         }
2864
2865         return new_route_from_template (how_many, insert_at, *tree.root(), name_base, pd);
2866 }
2867
2868 RouteList
2869 Session::new_route_from_template (uint32_t how_many, PresentationInfo::order_t insert_at, XMLNode& node, const std::string& name_base, PlaylistDisposition pd)
2870 {
2871         RouteList ret;
2872         uint32_t number = 0;
2873         const uint32_t being_added = how_many;
2874         /* This will prevent the use of any existing XML-provided PBD::ID
2875            values by Stateful.
2876         */
2877         Stateful::ForceIDRegeneration force_ids;
2878         IO::disable_connecting ();
2879
2880         while (how_many) {
2881
2882                 /* We're going to modify the node contents a bit so take a
2883                  * copy. The node may be re-used when duplicating more than once.
2884                  */
2885
2886                 XMLNode node_copy (node);
2887                 std::vector<boost::shared_ptr<Playlist> > shared_playlists;
2888
2889                 try {
2890                         string name;
2891
2892                         if (!name_base.empty()) {
2893
2894                                 /* if we're adding more than one routes, force
2895                                  * all the names of the new routes to be
2896                                  * numbered, via the final parameter.
2897                                  */
2898
2899                                 if (!find_route_name (name_base.c_str(), ++number, name, (being_added > 1))) {
2900                                         fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
2901                                         abort(); /*NOTREACHED*/
2902                                 }
2903
2904                         } else {
2905
2906                                 string const route_name  = node_copy.property(X_("name"))->value ();
2907
2908                                 /* generate a new name by adding a number to the end of the template name */
2909                                 if (!find_route_name (route_name.c_str(), ++number, name, true)) {
2910                                         fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
2911                                         abort(); /*NOTREACHED*/
2912                                 }
2913                         }
2914
2915                         /* figure out the appropriate playlist setup. The track
2916                          * (if the Route we're creating is a track) will find
2917                          * playlists via ID.
2918                          */
2919
2920                         if (pd == CopyPlaylist) {
2921
2922                                 PBD::ID playlist_id;
2923
2924                                 if (node_copy.get_property (X_("audio-playlist"), playlist_id)) {
2925                                         boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2926                                         playlist = PlaylistFactory::create (playlist, string_compose ("%1.1", name));
2927                                         playlist->reset_shares ();
2928                                         node_copy.set_property (X_("audio-playlist"), playlist->id());
2929                                 }
2930
2931                                 if (node_copy.get_property (X_("midi-playlist"), playlist_id)) {
2932                                         boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2933                                         playlist = PlaylistFactory::create (playlist, string_compose ("%1.1", name));
2934                                         playlist->reset_shares ();
2935                                         node_copy.set_property (X_("midi-playlist"), playlist->id());
2936                                 }
2937
2938                         } else if (pd == SharePlaylist) {
2939                                 PBD::ID playlist_id;
2940
2941                                 if (node_copy.get_property (X_("audio-playlist"), playlist_id)) {
2942                                         boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2943                                         shared_playlists.push_back (playlist);
2944                                 }
2945
2946                                 if (node_copy.get_property (X_("midi-playlist"), playlist_id)) {
2947                                         boost::shared_ptr<Playlist> playlist = _playlists->by_id (playlist_id);
2948                                         shared_playlists.push_back (playlist);
2949                                 }
2950
2951                         } else { /* NewPlaylist */
2952
2953                                 PBD::ID pid;
2954
2955                                 if (node_copy.get_property (X_("audio-playlist"), pid)) {
2956                                         boost::shared_ptr<Playlist> playlist = PlaylistFactory::create (DataType::AUDIO, *this, name, false);
2957                                         node_copy.set_property (X_("audio-playlist"), playlist->id());
2958                                 }
2959
2960                                 if (node_copy.get_property (X_("midi-playlist"), pid)) {
2961                                         boost::shared_ptr<Playlist> playlist = PlaylistFactory::create (DataType::MIDI, *this, name, false);
2962                                         node_copy.set_property (X_("midi-playlist"), playlist->id());
2963                                 }
2964                         }
2965
2966                         /* Fix up new name in the XML node */
2967
2968                         Route::set_name_in_state (node_copy, name);
2969
2970                         /* trim bitslots from listen sends so that new ones are used */
2971                         XMLNodeList children = node_copy.children ();
2972                         for (XMLNodeList::iterator i = children.begin(); i != children.end(); ++i) {
2973                                 if ((*i)->name() == X_("Processor")) {
2974                                         /* ForceIDRegeneration does not catch the following */
2975                                         XMLProperty const * role = (*i)->property (X_("role"));
2976                                         XMLProperty const * type = (*i)->property (X_("type"));
2977                                         if (role && role->value() == X_("Aux")) {
2978                                                 /* check if the target bus exists.
2979                                                  * we should not save aux-sends in templates.
2980                                                  */
2981                                                 XMLProperty const * target = (*i)->property (X_("target"));
2982                                                 if (!target) {
2983                                                         (*i)->set_property ("type", "dangling-aux-send");
2984                                                         continue;
2985                                                 }
2986                                                 boost::shared_ptr<Route> r = route_by_id (target->value());
2987                                                 if (!r || boost::dynamic_pointer_cast<Track>(r)) {
2988                                                         (*i)->set_property ("type", "dangling-aux-send");
2989                                                         continue;
2990                                                 }
2991                                         }
2992                                         if (role && role->value() == X_("Listen")) {
2993                                                 (*i)->remove_property (X_("bitslot"));
2994                                         }
2995                                         else if (role && (role->value() == X_("Send") || role->value() == X_("Aux"))) {
2996                                                 Delivery::Role xrole;
2997                                                 uint32_t bitslot = 0;
2998                                                 xrole = Delivery::Role (string_2_enum (role->value(), xrole));
2999                                                 std::string name = Send::name_and_id_new_send(*this, xrole, bitslot, false);
3000                                                 (*i)->remove_property (X_("bitslot"));
3001                                                 (*i)->remove_property (X_("name"));
3002                                                 (*i)->set_property ("bitslot", bitslot);
3003                                                 (*i)->set_property ("name", name);
3004                                                 XMLNodeList io_kids = (*i)->children ();
3005                                                 for (XMLNodeList::iterator j = io_kids.begin(); j != io_kids.end(); ++j) {
3006                                                         if ((*j)->name() != X_("IO")) {
3007                                                                 continue;
3008                                                         }
3009                                                         (*j)->remove_property (X_("name"));
3010                                                         (*j)->set_property ("name", name);
3011                                                 }
3012                                         }
3013                                         else if (type && type->value() == X_("intreturn")) {
3014                                                 (*i)->remove_property (X_("bitslot"));
3015                                                 (*i)->set_property ("ignore-bitslot", "1");
3016                                         }
3017                                         else if (type && type->value() == X_("return")) {
3018                                                 // Return::set_state() generates a new one
3019                                                 (*i)->remove_property (X_("bitslot"));
3020                                         }
3021                                         else if (type && type->value() == X_("port")) {
3022                                                 IOProcessor::prepare_for_reset (**i, name);
3023                                         }
3024                                 }
3025                         }
3026
3027                         /* new routes start off unsoloed to avoid issues related to
3028                            upstream / downstream buses.
3029                         */
3030                         node_copy.remove_node_and_delete (X_("Controllable"), X_("name"), X_("solo"));
3031
3032                         boost::shared_ptr<Route> route (XMLRouteFactory (node_copy, 3000));
3033
3034                         if (route == 0) {
3035                                 error << _("Session: cannot create track/bus from template description") << endmsg;
3036                                 goto out;
3037                         }
3038
3039                         /* Fix up sharing of playlists with the new Route/Track */
3040
3041                         for (vector<boost::shared_ptr<Playlist> >::iterator sp = shared_playlists.begin(); sp != shared_playlists.end(); ++sp) {
3042                                 (*sp)->share_with (route->id());
3043                         }
3044
3045                         if (boost::dynamic_pointer_cast<Track>(route)) {
3046                                 /* force input/output change signals so that the new diskstream
3047                                    picks up the configuration of the route. During session
3048                                    loading this normally happens in a different way.
3049                                 */
3050
3051                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
3052
3053                                 IOChange change (IOChange::Type (IOChange::ConfigurationChanged | IOChange::ConnectionsChanged));
3054                                 change.after = route->input()->n_ports();
3055                                 route->input()->changed (change, this);
3056                                 change.after = route->output()->n_ports();
3057                                 route->output()->changed (change, this);
3058                         }
3059
3060                         ret.push_back (route);
3061                 }
3062
3063                 catch (failed_constructor &err) {
3064                         error << _("Session: could not create new route from template") << endmsg;
3065                         goto out;
3066                 }
3067
3068                 catch (AudioEngine::PortRegistrationFailure& pfe) {
3069                         error << pfe.what() << endmsg;
3070                         goto out;
3071                 }
3072
3073                 catch (...) {
3074                         IO::enable_connecting ();
3075                         throw;
3076                 }
3077
3078                 --how_many;
3079         }
3080
3081         out:
3082         if (!ret.empty()) {
3083                 StateProtector sp (this);
3084                 if (Profile->get_trx()) {
3085                         add_routes (ret, false, false, false, insert_at);
3086                 } else {
3087                         add_routes (ret, true, true, false, insert_at);
3088                 }
3089         }
3090
3091         IO::enable_connecting ();
3092
3093         return ret;
3094 }
3095
3096 void
3097 Session::add_routes (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, bool save, PresentationInfo::order_t order)
3098 {
3099         try {
3100                 PBD::Unwinder<bool> aip (_adding_routes_in_progress, true);
3101                 add_routes_inner (new_routes, input_auto_connect, output_auto_connect, order);
3102
3103         } catch (...) {
3104                 error << _("Adding new tracks/busses failed") << endmsg;
3105         }
3106
3107         graph_reordered ();
3108
3109         update_latency (false);
3110         update_latency (true);
3111
3112         set_dirty();
3113
3114         if (save) {
3115                 save_state (_current_snapshot_name);
3116         }
3117
3118         update_route_record_state ();
3119
3120         RouteAdded (new_routes); /* EMIT SIGNAL */
3121 }
3122
3123 void
3124 Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, PresentationInfo::order_t order)
3125 {
3126         ChanCount existing_inputs;
3127         ChanCount existing_outputs;
3128         uint32_t n_routes;
3129         uint32_t added = 0;
3130
3131         count_existing_track_channels (existing_inputs, existing_outputs);
3132
3133         {
3134                 RCUWriter<RouteList> writer (routes);
3135                 boost::shared_ptr<RouteList> r = writer.get_copy ();
3136                 n_routes = r->size();
3137                 r->insert (r->end(), new_routes.begin(), new_routes.end());
3138
3139                 /* if there is no control out and we're not in the middle of loading,
3140                  * resort the graph here. if there is a control out, we will resort
3141                  * toward the end of this method. if we are in the middle of loading,
3142                  * we will resort when done.
3143                  */
3144
3145                 if (!_monitor_out && IO::connecting_legal) {
3146                         resort_routes_using (r);
3147                 }
3148         }
3149
3150         /* monitor is not part of the order */
3151         if (_monitor_out) {
3152                 assert (n_routes > 0);
3153                 --n_routes;
3154         }
3155
3156         {
3157                 PresentationInfo::ChangeSuspender cs;
3158                 ensure_route_presentation_info_gap (order, new_routes.size());
3159
3160                 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x, ++added) {
3161
3162                         boost::weak_ptr<Route> wpr (*x);
3163                         boost::shared_ptr<Route> r (*x);
3164
3165                         r->solo_control()->Changed.connect_same_thread (*this, boost::bind (&Session::route_solo_changed, this, _1, _2,wpr));
3166                         r->solo_isolate_control()->Changed.connect_same_thread (*this, boost::bind (&Session::route_solo_isolated_changed, this, wpr));
3167                         r->mute_control()->Changed.connect_same_thread (*this, boost::bind (&Session::route_mute_changed, this));
3168
3169                         r->output()->changed.connect_same_thread (*this, boost::bind (&Session::set_worst_io_latencies_x, this, _1, _2));
3170                         r->processors_changed.connect_same_thread (*this, boost::bind (&Session::route_processors_changed, this, _1));
3171                         r->processor_latency_changed.connect_same_thread (*this, boost::bind (&Session::queue_latency_recompute, this));
3172
3173                         if (r->is_master()) {
3174                                 _master_out = r;
3175                         }
3176
3177                         if (r->is_monitor()) {
3178                                 _monitor_out = r;
3179                         }
3180
3181                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (r);
3182                         if (tr) {
3183                                 tr->PlaylistChanged.connect_same_thread (*this, boost::bind (&Session::track_playlist_changed, this, boost::weak_ptr<Track> (tr)));
3184                                 track_playlist_changed (boost::weak_ptr<Track> (tr));
3185                                 tr->rec_enable_control()->Changed.connect_same_thread (*this, boost::bind (&Session::update_route_record_state, this));
3186
3187                                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (tr);
3188                                 if (mt) {
3189                                         mt->StepEditStatusChange.connect_same_thread (*this, boost::bind (&Session::step_edit_status_change, this, _1));
3190                                         mt->output()->changed.connect_same_thread (*this, boost::bind (&Session::midi_output_change_handler, this, _1, _2, boost::weak_ptr<Route>(mt)));
3191                                         mt->presentation_info().PropertyChanged.connect_same_thread (*this, boost::bind (&Session::midi_track_presentation_info_changed, this, _1, boost::weak_ptr<MidiTrack>(mt)));
3192                                 }
3193                         }
3194
3195                         if (!r->presentation_info().special (false)) {
3196
3197                                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("checking PI state for %1\n", r->name()));
3198
3199                                 /* presentation info order may already have been set from XML */
3200
3201                                 if (!r->presentation_info().order_set()) {
3202                                         if (order == PresentationInfo::max_order) {
3203                                                 /* just add to the end */
3204                                                 r->set_presentation_order (n_routes + added);
3205                                                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order not set, set to NR %1 + %2 = %3\n", n_routes, added, n_routes + added));
3206                                         } else {
3207                                                 r->set_presentation_order (order + added);
3208                                                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order not set, set to %1 + %2 = %3\n", order, added, order + added));
3209                                         }
3210                                 } else {
3211                                         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("group order already set to %1\n", r->presentation_info().order()));
3212                                 }
3213                         }
3214
3215 #if !defined(__APPLE__) && !defined(__FreeBSD__)
3216                         /* clang complains: 'operator<<' should be declared prior to the call site or in an associated namespace of one of its
3217                          * arguments std::ostream& operator<<(std::ostream& o, ARDOUR::PresentationInfo const& rid)"
3218                          */
3219                         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("added route %1, group order %2 type %3 (summary: %4)\n",
3220                                                                        r->name(),
3221                                                                        r->presentation_info().order(),
3222                                                                        enum_2_string (r->presentation_info().flags()),
3223                                                                        r->presentation_info()));
3224 #endif
3225
3226
3227                         if (input_auto_connect || output_auto_connect) {
3228                                 auto_connect_route (r, input_auto_connect, ChanCount (), ChanCount (), existing_inputs, existing_outputs);
3229                                 existing_inputs += r->n_inputs();
3230                                 existing_outputs += r->n_outputs();
3231                         }
3232
3233                         ARDOUR::GUIIdle ();
3234                 }
3235                 ensure_stripable_sort_order ();
3236         }
3237
3238         if (_monitor_out && IO::connecting_legal) {
3239                 Glib::Threads::Mutex::Lock lm (_engine.process_lock());
3240
3241                 for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
3242                         if ((*x)->is_monitor()) {
3243                                 /* relax */
3244                         } else if ((*x)->is_master()) {
3245                                 /* relax */
3246                         } else {
3247                                 (*x)->enable_monitor_send ();
3248                         }
3249                 }
3250         }
3251
3252         reassign_track_numbers ();
3253 }
3254
3255 void
3256 Session::globally_set_send_gains_to_zero (boost::shared_ptr<Route> dest)
3257 {
3258         boost::shared_ptr<RouteList> r = routes.reader ();
3259         boost::shared_ptr<Send> s;
3260
3261         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3262                 if ((s = (*i)->internal_send_for (dest)) != 0) {
3263                         s->amp()->gain_control()->set_value (GAIN_COEFF_ZERO, Controllable::NoGroup);
3264                 }
3265         }
3266 }
3267
3268 void
3269 Session::globally_set_send_gains_to_unity (boost::shared_ptr<Route> dest)
3270 {
3271         boost::shared_ptr<RouteList> r = routes.reader ();
3272         boost::shared_ptr<Send> s;
3273
3274         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3275                 if ((s = (*i)->internal_send_for (dest)) != 0) {
3276                         s->amp()->gain_control()->set_value (GAIN_COEFF_UNITY, Controllable::NoGroup);
3277                 }
3278         }
3279 }
3280
3281 void
3282 Session::globally_set_send_gains_from_track(boost::shared_ptr<Route> dest)
3283 {
3284         boost::shared_ptr<RouteList> r = routes.reader ();
3285         boost::shared_ptr<Send> s;
3286
3287         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3288                 if ((s = (*i)->internal_send_for (dest)) != 0) {
3289                         s->amp()->gain_control()->set_value ((*i)->gain_control()->get_value(), Controllable::NoGroup);
3290                 }
3291         }
3292 }
3293
3294 /** @param include_buses true to add sends to buses and tracks, false for just tracks */
3295 void
3296 Session::globally_add_internal_sends (boost::shared_ptr<Route> dest, Placement p, bool include_buses)
3297 {
3298         boost::shared_ptr<RouteList> r = routes.reader ();
3299         boost::shared_ptr<RouteList> t (new RouteList);
3300
3301         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3302                 /* no MIDI sends because there are no MIDI busses yet */
3303                 if (include_buses || boost::dynamic_pointer_cast<AudioTrack>(*i)) {
3304                         t->push_back (*i);
3305                 }
3306         }
3307
3308         add_internal_sends (dest, p, t);
3309 }
3310
3311 void
3312 Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::shared_ptr<RouteList> senders)
3313 {
3314         for (RouteList::iterator i = senders->begin(); i != senders->end(); ++i) {
3315                 add_internal_send (dest, (*i)->before_processor_for_placement (p), *i);
3316         }
3317 }
3318
3319 void
3320 Session::add_internal_send (boost::shared_ptr<Route> dest, int index, boost::shared_ptr<Route> sender)
3321 {
3322         add_internal_send (dest, sender->before_processor_for_index (index), sender);
3323 }
3324
3325 void
3326 Session::add_internal_send (boost::shared_ptr<Route> dest, boost::shared_ptr<Processor> before, boost::shared_ptr<Route> sender)
3327 {
3328         if (sender->is_monitor() || sender->is_master() || sender == dest || dest->is_monitor() || dest->is_master()) {
3329                 return;
3330         }
3331
3332         if (!dest->internal_return()) {
3333                 dest->add_internal_return ();
3334         }
3335
3336         sender->add_aux_send (dest, before);
3337
3338         graph_reordered ();
3339 }
3340
3341 void
3342 Session::remove_routes (boost::shared_ptr<RouteList> routes_to_remove)
3343 {
3344         bool mute_changed = false;
3345         bool send_selected = false;
3346
3347         { // RCU Writer scope
3348                 PBD::Unwinder<bool> uw_flag (_route_deletion_in_progress, true);
3349                 RCUWriter<RouteList> writer (routes);
3350                 boost::shared_ptr<RouteList> rs = writer.get_copy ();
3351
3352                 for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3353
3354                         if (_selection->selected (*iter)) {
3355                                 send_selected = true;
3356                         }
3357
3358                         if (*iter == _master_out) {
3359                                 continue;
3360                         }
3361
3362                         /* speed up session deletion, don't do the solo dance */
3363                         if (!deletion_in_progress ()) {
3364                                 (*iter)->solo_control()->set_value (0.0, Controllable::NoGroup);
3365                         }
3366
3367                         if ((*iter)->mute_control()->muted ()) {
3368                                 mute_changed = true;
3369                         }
3370
3371                         rs->remove (*iter);
3372
3373                         /* deleting the master out seems like a dumb
3374                            idea, but its more of a UI policy issue
3375                            than our concern.
3376                         */
3377
3378                         if (*iter == _master_out) {
3379                                 _master_out = boost::shared_ptr<Route> ();
3380                         }
3381
3382                         if (*iter == _monitor_out) {
3383                                 _monitor_out.reset ();
3384                         }
3385
3386                         // We need to disconnect the route's inputs and outputs
3387
3388                         (*iter)->input()->disconnect (0);
3389                         (*iter)->output()->disconnect (0);
3390
3391                         /* if the route had internal sends sending to it, remove them */
3392
3393                         if (!deletion_in_progress () && (*iter)->internal_return()) {
3394
3395                                 boost::shared_ptr<RouteList> r = routes.reader ();
3396                                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3397                                         boost::shared_ptr<Send> s = (*i)->internal_send_for (*iter);
3398                                         if (s) {
3399                                                 (*i)->remove_processor (s);
3400                                         }
3401                                 }
3402                         }
3403
3404                         /* if the monitoring section had a pointer to this route, remove it */
3405                         if (_monitor_out && !(*iter)->is_master() && !(*iter)->is_monitor()) {
3406                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
3407                                 ProcessorChangeBlocker pcb (this, false);
3408                                 (*iter)->remove_aux_or_listen (_monitor_out);
3409                         }
3410
3411                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*iter);
3412                         if (mt && mt->step_editing()) {
3413                                 if (_step_editors > 0) {
3414                                         _step_editors--;
3415                                 }
3416                         }
3417                 }
3418
3419                 /* writer goes out of scope, forces route list update */
3420
3421         } // end of RCU Writer scope
3422
3423         if (mute_changed) {
3424                 MuteChanged (); /* EMIT SIGNAL */
3425         }
3426
3427         update_route_solo_state ();
3428         update_latency_compensation ();
3429         set_dirty();
3430
3431         /* Re-sort routes to remove the graph's current references to the one that is
3432          * going away, then flush old references out of the graph.
3433          */
3434
3435         routes.flush (); // maybe unsafe, see below.
3436         resort_routes ();
3437
3438         if (_process_graph && !deletion_in_progress() && _engine.running()) {
3439                 _process_graph->clear_other_chain ();
3440         }
3441
3442         /* get rid of it from the dead wood collection in the route list manager */
3443         /* XXX i think this is unsafe as it currently stands, but i am not sure. (pd, october 2nd, 2006) */
3444
3445         routes.flush ();
3446
3447         /* remove these routes from the selection if appropriate, and signal
3448          * the change *before* we call DropReferences for them.
3449          */
3450
3451         if (send_selected && !deletion_in_progress()) {
3452                 for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3453                         _selection->remove_stripable_by_id ((*iter)->id());
3454                 }
3455                 PropertyChange pc;
3456                 pc.add (Properties::selected);
3457                 PresentationInfo::Change (pc);
3458         }
3459
3460         /* try to cause everyone to drop their references
3461          * and unregister ports from the backend
3462          */
3463
3464         for (RouteList::iterator iter = routes_to_remove->begin(); iter != routes_to_remove->end(); ++iter) {
3465                 (*iter)->drop_references ();
3466         }
3467
3468         if (deletion_in_progress()) {
3469                 return;
3470         }
3471
3472         PropertyChange pc;
3473         pc.add (Properties::order);
3474         PresentationInfo::Change (pc);
3475
3476         /* save the new state of the world */
3477
3478         if (save_state (_current_snapshot_name)) {
3479                 save_history (_current_snapshot_name);
3480         }
3481
3482         update_route_record_state ();
3483 }
3484
3485 void
3486 Session::remove_route (boost::shared_ptr<Route> route)
3487 {
3488         boost::shared_ptr<RouteList> rl (new RouteList);
3489         rl->push_back (route);
3490         remove_routes (rl);
3491 }
3492
3493 void
3494 Session::route_mute_changed ()
3495 {
3496         MuteChanged (); /* EMIT SIGNAL */
3497         set_dirty ();
3498 }
3499
3500 void
3501 Session::route_listen_changed (Controllable::GroupControlDisposition group_override, boost::weak_ptr<Route> wpr)
3502 {
3503         boost::shared_ptr<Route> route (wpr.lock());
3504
3505         if (!route) {
3506                 return;
3507         }
3508
3509         assert (Config->get_solo_control_is_listen_control());
3510
3511         if (route->solo_control()->soloed_by_self_or_masters()) {
3512
3513                 if (Config->get_exclusive_solo()) {
3514
3515                         RouteGroup* rg = route->route_group ();
3516                         const bool group_already_accounted_for = (group_override == Controllable::ForGroup);
3517
3518                         boost::shared_ptr<RouteList> r = routes.reader ();
3519
3520                         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3521                                 if ((*i) == route) {
3522                                         /* already changed */
3523                                         continue;
3524                                 }
3525
3526                                 if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3527                                         /* route does not get solo propagated to it */
3528                                         continue;
3529                                 }
3530
3531                                 if ((group_already_accounted_for && (*i)->route_group() && (*i)->route_group() == rg)) {
3532                                         /* this route is a part of the same solo group as the route
3533                                          * that was changed. Changing that route did change or will
3534                                          * change all group members appropriately, so we can ignore it
3535                                          * here
3536                                          */
3537                                         continue;
3538                                 }
3539                                 (*i)->solo_control()->set_value (0.0, Controllable::NoGroup);
3540                         }
3541                 }
3542
3543                 _listen_cnt++;
3544
3545         } else if (_listen_cnt > 0) {
3546
3547                 _listen_cnt--;
3548         }
3549 }
3550
3551 void
3552 Session::route_solo_isolated_changed (boost::weak_ptr<Route> wpr)
3553 {
3554         boost::shared_ptr<Route> route (wpr.lock());
3555
3556         if (!route) {
3557                 return;
3558         }
3559
3560         bool send_changed = false;
3561
3562         if (route->solo_isolate_control()->solo_isolated()) {
3563                 if (_solo_isolated_cnt == 0) {
3564                         send_changed = true;
3565                 }
3566                 _solo_isolated_cnt++;
3567         } else if (_solo_isolated_cnt > 0) {
3568                 _solo_isolated_cnt--;
3569                 if (_solo_isolated_cnt == 0) {
3570                         send_changed = true;
3571                 }
3572         }
3573
3574         if (send_changed) {
3575                 IsolatedChanged (); /* EMIT SIGNAL */
3576         }
3577 }
3578
3579 void
3580 Session::route_solo_changed (bool self_solo_changed, Controllable::GroupControlDisposition group_override,  boost::weak_ptr<Route> wpr)
3581 {
3582         DEBUG_TRACE (DEBUG::Solo, string_compose ("route solo change, self = %1, update\n", self_solo_changed));
3583
3584         boost::shared_ptr<Route> route (wpr.lock());
3585
3586         if (!route) {
3587                 return;
3588         }
3589
3590         if (Config->get_solo_control_is_listen_control()) {
3591                 route_listen_changed (group_override, wpr);
3592                 return;
3593         }
3594
3595         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1: self %2 masters %3 transition %4\n", route->name(), route->self_soloed(), route->solo_control()->get_masters_value(), route->solo_control()->transitioned_into_solo()));
3596
3597         if (route->solo_control()->transitioned_into_solo() == 0) {
3598                 /* route solo changed by upstream/downstream or clear all solo state; not interesting
3599                    to Session.
3600                 */
3601                 DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 not self-soloed nor soloed by master (%2), ignoring\n", route->name(), route->solo_control()->get_masters_value()));
3602                 return;
3603         }
3604
3605         boost::shared_ptr<RouteList> r = routes.reader ();
3606         int32_t delta = route->solo_control()->transitioned_into_solo ();
3607
3608         /* the route may be a member of a group that has shared-solo
3609          * semantics. If so, then all members of that group should follow the
3610          * solo of the changed route. But ... this is optional, controlled by a
3611          * Controllable::GroupControlDisposition.
3612          *
3613          * The first argument to the signal that this method is connected to is the
3614          * GroupControlDisposition value that was used to change solo.
3615          *
3616          * If the solo change was done with group semantics (either InverseGroup
3617          * (force the entire group to change even if the group shared solo is
3618          * disabled) or UseGroup (use the group, which may or may not have the
3619          * shared solo property enabled)) then as we propagate the change to
3620          * the entire session we should IGNORE THE GROUP that the changed route
3621          * belongs to.
3622          */
3623
3624         RouteGroup* rg = route->route_group ();
3625         const bool group_already_accounted_for = (group_override == Controllable::ForGroup);
3626
3627         DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate to session, group accounted for ? %1\n", group_already_accounted_for));
3628
3629         if (delta == 1 && Config->get_exclusive_solo()) {
3630
3631                 /* new solo: disable all other solos, but not the group if its solo-enabled */
3632
3633                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3634
3635                         if ((*i) == route) {
3636                                 /* already changed */
3637                                 continue;
3638                         }
3639
3640                         if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3641                                 /* route does not get solo propagated to it */
3642                                 continue;
3643                         }
3644
3645                         if ((group_already_accounted_for && (*i)->route_group() && (*i)->route_group() == rg)) {
3646                                 /* this route is a part of the same solo group as the route
3647                                  * that was changed. Changing that route did change or will
3648                                  * change all group members appropriately, so we can ignore it
3649                                  * here
3650                                  */
3651                                 continue;
3652                         }
3653
3654                         (*i)->solo_control()->set_value (0.0, group_override);
3655                 }
3656         }
3657
3658         DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate solo change, delta = %1\n", delta));
3659
3660         RouteList uninvolved;
3661
3662         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1\n", route->name()));
3663
3664         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3665                 bool via_sends_only;
3666                 bool in_signal_flow;
3667
3668                 if ((*i) == route) {
3669                         /* already changed */
3670                         continue;
3671                 }
3672
3673                 if ((*i)->solo_isolate_control()->solo_isolated() || !(*i)->can_solo()) {
3674                         /* route does not get solo propagated to it */
3675                         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 excluded from solo because iso = %2 can_solo = %3\n", (*i)->name(), (*i)->solo_isolate_control()->solo_isolated(),
3676                                                                   (*i)->can_solo()));
3677                         continue;
3678                 }
3679
3680                 if ((group_already_accounted_for && (*i)->route_group() && (*i)->route_group() == rg)) {
3681                         /* this route is a part of the same solo group as the route
3682                          * that was changed. Changing that route did change or will
3683                          * change all group members appropriately, so we can ignore it
3684                          * here
3685                          */
3686                         continue;
3687                 }
3688
3689                 in_signal_flow = false;
3690
3691                 DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed from %1\n", (*i)->name()));
3692
3693                 if ((*i)->feeds (route, &via_sends_only)) {
3694                         DEBUG_TRACE (DEBUG::Solo, string_compose ("\tthere is a feed from %1\n", (*i)->name()));
3695                         if (!via_sends_only) {
3696                                 if (!route->soloed_by_others_upstream()) {
3697                                         (*i)->solo_control()->mod_solo_by_others_downstream (delta);
3698                                 } else {
3699                                         DEBUG_TRACE (DEBUG::Solo, "\talready soloed by others upstream\n");
3700                                 }
3701                         } else {
3702                                 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tthere is a send-only feed from %1\n", (*i)->name()));
3703                         }
3704                         in_signal_flow = true;
3705                 } else {
3706                         DEBUG_TRACE (DEBUG::Solo, string_compose ("\tno feed from %1\n", (*i)->name()));
3707                 }
3708
3709                 DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed to %1\n", (*i)->name()));
3710
3711                 if (route->feeds (*i, &via_sends_only)) {
3712                         /* propagate solo upstream only if routing other than
3713                            sends is involved, but do consider the other route
3714                            (*i) to be part of the signal flow even if only
3715                            sends are involved.
3716                         */
3717                         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 feeds %2 via sends only %3 sboD %4 sboU %5\n",
3718                                                                   route->name(),
3719                                                                   (*i)->name(),
3720                                                                   via_sends_only,
3721                                                                   route->soloed_by_others_downstream(),
3722                                                                   route->soloed_by_others_upstream()));
3723                         if (!via_sends_only) {
3724                                 //NB. Triggers Invert Push, which handles soloed by downstream
3725                                 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tmod %1 by %2\n", (*i)->name(), delta));
3726                                 (*i)->solo_control()->mod_solo_by_others_upstream (delta);
3727                         } else {
3728                                 DEBUG_TRACE (DEBUG::Solo, string_compose ("\tfeed to %1 ignored, sends-only\n", (*i)->name()));
3729                         }
3730                         in_signal_flow = true;
3731                 } else {
3732                         DEBUG_TRACE (DEBUG::Solo, string_compose("\tno feed to %1\n", (*i)->name()) );
3733                 }
3734
3735                 if (!in_signal_flow) {
3736                         uninvolved.push_back (*i);
3737                 }
3738         }
3739
3740         DEBUG_TRACE (DEBUG::Solo, "propagation complete\n");
3741
3742         /* now notify that the mute state of the routes not involved in the signal
3743            pathway of the just-solo-changed route may have altered.
3744         */
3745
3746         for (RouteList::iterator i = uninvolved.begin(); i != uninvolved.end(); ++i) {
3747                 DEBUG_TRACE (DEBUG::Solo, string_compose ("mute change for %1, which neither feeds or is fed by %2\n", (*i)->name(), route->name()));
3748                 (*i)->act_on_mute ();
3749                 /* Session will emit SoloChanged() after all solo changes are
3750                  * complete, which should be used by UIs to update mute status
3751                  */
3752         }
3753 }
3754
3755 void
3756 Session::update_route_solo_state (boost::shared_ptr<RouteList> r)
3757 {
3758         /* now figure out if anything that matters is soloed (or is "listening")*/
3759
3760         bool something_soloed = false;
3761         bool something_listening = false;
3762         uint32_t listeners = 0;
3763         uint32_t isolated = 0;
3764
3765         if (!r) {
3766                 r = routes.reader();
3767         }
3768
3769         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3770                 if ((*i)->can_solo()) {
3771                         if (Config->get_solo_control_is_listen_control()) {
3772                                 if ((*i)->solo_control()->soloed_by_self_or_masters()) {
3773                                         listeners++;
3774                                         something_listening = true;
3775                                 }
3776                         } else {
3777                                 (*i)->set_listen (false);
3778                                 if ((*i)->can_solo() && (*i)->solo_control()->soloed_by_self_or_masters()) {
3779                                         something_soloed = true;
3780                                 }
3781                         }
3782                 }
3783
3784                 if ((*i)->solo_isolate_control()->solo_isolated()) {
3785                         isolated++;
3786                 }
3787         }
3788
3789         if (something_soloed != _non_soloed_outs_muted) {
3790                 _non_soloed_outs_muted = something_soloed;
3791                 SoloActive (_non_soloed_outs_muted); /* EMIT SIGNAL */
3792         }
3793
3794         if (something_listening != _listening) {
3795                 _listening = something_listening;
3796                 SoloActive (_listening);
3797         }
3798
3799         _listen_cnt = listeners;
3800
3801         if (isolated != _solo_isolated_cnt) {
3802                 _solo_isolated_cnt = isolated;
3803                 IsolatedChanged (); /* EMIT SIGNAL */
3804         }
3805
3806         DEBUG_TRACE (DEBUG::Solo, string_compose ("solo state updated by session, soloed? %1 listeners %2 isolated %3\n",
3807                                                   something_soloed, listeners, isolated));
3808
3809
3810         SoloChanged (); /* EMIT SIGNAL */
3811         set_dirty();
3812 }
3813
3814 bool
3815 Session::muted () const
3816 {
3817         // TODO consider caching the value on every MuteChanged signal,
3818         // Note that API users may also subscribe to MuteChanged and hence
3819         // this method needs to be called first.
3820         bool muted = false;
3821         StripableList all;
3822         get_stripables (all);
3823         for (StripableList::const_iterator i = all.begin(); i != all.end(); ++i) {
3824                 assert (!(*i)->is_auditioner()); // XXX remove me
3825                 if ((*i)->is_monitor()) {
3826                         continue;
3827                 }
3828                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(*i);
3829                 if (r && !r->active()) {
3830                         continue;
3831                 }
3832                 boost::shared_ptr<MuteControl> mc = (*i)->mute_control();
3833                 if (mc && mc->muted ()) {
3834                         muted = true;
3835                         break;
3836                 }
3837         }
3838         return muted;
3839 }
3840
3841 std::vector<boost::weak_ptr<AutomationControl> >
3842 Session::cancel_all_mute ()
3843 {
3844         StripableList all;
3845         get_stripables (all);
3846         std::vector<boost::weak_ptr<AutomationControl> > muted;
3847         boost::shared_ptr<ControlList> cl (new ControlList);
3848         for (StripableList::const_iterator i = all.begin(); i != all.end(); ++i) {
3849                 assert (!(*i)->is_auditioner());
3850                 if ((*i)->is_monitor()) {
3851                         continue;
3852                 }
3853                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*i);
3854                 if (r && !r->active()) {
3855                         continue;
3856                 }
3857                 boost::shared_ptr<AutomationControl> ac = (*i)->mute_control();
3858                 if (ac && ac->get_value () > 0) {
3859                         cl->push_back (ac);
3860                         muted.push_back (boost::weak_ptr<AutomationControl>(ac));
3861                 }
3862         }
3863         if (!cl->empty ()) {
3864                 set_controls (cl, 0.0, PBD::Controllable::UseGroup);
3865         }
3866         return muted;
3867 }
3868
3869 void
3870 Session::get_stripables (StripableList& sl, PresentationInfo::Flag fl) const
3871 {
3872         boost::shared_ptr<RouteList> r = routes.reader ();
3873         for (RouteList::iterator it = r->begin(); it != r->end(); ++it) {
3874                 if ((*it)->presentation_info ().flags () & fl) {
3875                         sl.push_back (*it);
3876                 }
3877         }
3878
3879         if (fl & PresentationInfo::VCA) {
3880                 VCAList v = _vca_manager->vcas ();
3881                 sl.insert (sl.end(), v.begin(), v.end());
3882         }
3883 }
3884
3885 StripableList
3886 Session::get_stripables () const
3887 {
3888         PresentationInfo::Flag fl = PresentationInfo::AllStripables;
3889         StripableList rv;
3890         Session::get_stripables (rv, fl);
3891         rv.sort (Stripable::Sorter ());
3892         return rv;
3893 }
3894
3895 RouteList
3896 Session::get_routelist (bool mixer_order, PresentationInfo::Flag fl) const
3897 {
3898         boost::shared_ptr<RouteList> r = routes.reader ();
3899         RouteList rv;
3900         for (RouteList::iterator it = r->begin(); it != r->end(); ++it) {
3901                 if ((*it)->presentation_info ().flags () & fl) {
3902                         rv.push_back (*it);
3903                 }
3904         }
3905         rv.sort (Stripable::Sorter (mixer_order));
3906         return rv;
3907 }
3908
3909 boost::shared_ptr<RouteList>
3910 Session::get_routes_with_internal_returns() const
3911 {
3912         boost::shared_ptr<RouteList> r = routes.reader ();
3913         boost::shared_ptr<RouteList> rl (new RouteList);
3914
3915         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3916                 if ((*i)->internal_return ()) {
3917                         rl->push_back (*i);
3918                 }
3919         }
3920         return rl;
3921 }
3922
3923 bool
3924 Session::io_name_is_legal (const std::string& name) const
3925 {
3926         boost::shared_ptr<RouteList> r = routes.reader ();
3927
3928         for (map<string,bool>::const_iterator reserved = reserved_io_names.begin(); reserved != reserved_io_names.end(); ++reserved) {
3929                 if (name == reserved->first) {
3930                         if (!route_by_name (reserved->first)) {
3931                                 /* first instance of a reserved name is allowed for some */
3932                                 return reserved->second;
3933                         }
3934                         /* all other instances of a reserved name are not allowed */
3935                         return false;
3936                 }
3937         }
3938
3939         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
3940                 if ((*i)->name() == name) {
3941                         return false;
3942                 }
3943
3944                 if ((*i)->has_io_processor_named (name)) {
3945                         return false;
3946                 }
3947         }
3948
3949         return true;
3950 }
3951
3952 void
3953 Session::set_exclusive_input_active (boost::shared_ptr<RouteList> rl, bool onoff, bool flip_others)
3954 {
3955         RouteList rl2;
3956         vector<string> connections;
3957
3958         /* if we are passed only a single route and we're not told to turn
3959          * others off, then just do the simple thing.
3960          */
3961
3962         if (flip_others == false && rl->size() == 1) {
3963                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (rl->front());
3964                 if (mt) {
3965                         mt->set_input_active (onoff);
3966                         return;
3967                 }
3968         }
3969
3970         for (RouteList::iterator rt = rl->begin(); rt != rl->end(); ++rt) {
3971
3972                 PortSet& ps ((*rt)->input()->ports());
3973
3974                 for (PortSet::iterator p = ps.begin(); p != ps.end(); ++p) {
3975                         p->get_connections (connections);
3976                 }
3977
3978                 for (vector<string>::iterator s = connections.begin(); s != connections.end(); ++s) {
3979                         routes_using_input_from (*s, rl2);
3980                 }
3981
3982                 /* scan all relevant routes to see if others are on or off */
3983
3984                 bool others_are_already_on = false;
3985
3986                 for (RouteList::iterator r = rl2.begin(); r != rl2.end(); ++r) {
3987
3988                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
3989
3990                         if (!mt) {
3991                                 continue;
3992                         }
3993
3994                         if ((*r) != (*rt)) {
3995                                 if (mt->input_active()) {
3996                                         others_are_already_on = true;
3997                                 }
3998                         } else {
3999                                 /* this one needs changing */
4000                                 mt->set_input_active (onoff);
4001                         }
4002                 }
4003
4004                 if (flip_others) {
4005
4006                         /* globally reverse other routes */
4007
4008                         for (RouteList::iterator r = rl2.begin(); r != rl2.end(); ++r) {
4009                                 if ((*r) != (*rt)) {
4010                                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
4011                                         if (mt) {
4012                                                 mt->set_input_active (!others_are_already_on);
4013                                         }
4014                                 }
4015                         }
4016                 }
4017         }
4018 }
4019
4020 void
4021 Session::routes_using_input_from (const string& str, RouteList& rl)
4022 {
4023         boost::shared_ptr<RouteList> r = routes.reader();
4024
4025         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
4026                 if ((*i)->input()->connected_to (str)) {
4027                         rl.push_back (*i);
4028                 }
4029         }
4030 }
4031
4032 boost::shared_ptr<Route>
4033 Session::route_by_name (string name) const
4034 {
4035         boost::shared_ptr<RouteList> r = routes.reader ();
4036
4037         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
4038                 if ((*i)->name() == name) {
4039                         return *i;
4040                 }
4041         }
4042
4043         return boost::shared_ptr<Route> ((Route*) 0);
4044 }
4045
4046 boost::shared_ptr<Route>
4047 Session::route_by_id (PBD::ID id) const
4048 {
4049         boost::shared_ptr<RouteList> r = routes.reader ();
4050
4051         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
4052                 if ((*i)->id() == id) {
4053                         return *i;
4054                 }
4055         }
4056
4057         return boost::shared_ptr<Route> ((Route*) 0);
4058 }
4059
4060
4061 boost::shared_ptr<Stripable>
4062 Session::stripable_by_id (PBD::ID id) const
4063 {
4064         StripableList sl;
4065         get_stripables (sl);
4066
4067         for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
4068                 if ((*s)->id() == id) {
4069                         return *s;
4070                 }
4071         }
4072
4073         return boost::shared_ptr<Stripable>();
4074 }
4075
4076 boost::shared_ptr<Processor>
4077 Session::processor_by_id (PBD::ID id) const
4078 {
4079         boost::shared_ptr<RouteList> r = routes.reader ();
4080
4081         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
4082                 boost::shared_ptr<Processor> p = (*i)->Route::processor_by_id (id);
4083                 if (p) {
4084                         return p;
4085                 }
4086         }
4087
4088         return boost::shared_ptr<Processor> ();
4089 }
4090
4091 boost::shared_ptr<Route>
4092 Session::get_remote_nth_route (PresentationInfo::order_t n) const
4093 {
4094         return boost::dynamic_pointer_cast<Route> (get_remote_nth_stripable (n, PresentationInfo::Route));
4095 }
4096
4097 boost::shared_ptr<Stripable>
4098 Session::get_remote_nth_stripable (PresentationInfo::order_t n, PresentationInfo::Flag flags) const
4099 {
4100         StripableList sl;
4101         PresentationInfo::order_t match_cnt = 0;
4102
4103         get_stripables (sl);
4104         sl.sort (Stripable::Sorter());
4105
4106         for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
4107
4108                 if ((*s)->presentation_info().hidden()) {
4109                         /* if the caller didn't explicitly ask for hidden
4110                            stripables, ignore hidden ones. This matches
4111                            the semantics of the pre-PresentationOrder
4112                            "get by RID" logic of Ardour 4.x and earlier.
4113
4114                            XXX at some point we should likely reverse
4115                            the logic of the flags, because asking for "the
4116                            hidden stripables" is not going to be common,
4117                            whereas asking for visible ones is normal.
4118                         */
4119
4120                         if (! (flags & PresentationInfo::Hidden)) {
4121                                 continue;
4122                         }
4123                 }
4124
4125                 if ((*s)->presentation_info().flag_match (flags)) {
4126                         if (match_cnt++ == n) {
4127                                 return *s;
4128                         }
4129                 }
4130         }
4131
4132         /* there is no nth stripable that matches the given flags */
4133         return boost::shared_ptr<Stripable>();
4134 }
4135
4136 boost::shared_ptr<Route>
4137 Session::route_by_selected_count (uint32_t id) const
4138 {
4139         RouteList r (*(routes.reader ()));
4140         r.sort (Stripable::Sorter());
4141
4142         RouteList::iterator i;
4143
4144         for (i = r.begin(); i != r.end(); ++i) {
4145                 if ((*i)->is_selected()) {
4146                         if (id == 0) {
4147                                 return *i;
4148                         }
4149                         --id;
4150                 }
4151         }
4152
4153         return boost::shared_ptr<Route> ();
4154 }
4155
4156 void
4157 Session::reassign_track_numbers ()
4158 {
4159         int64_t tn = 0;
4160         int64_t bn = 0;
4161         RouteList r (*(routes.reader ()));
4162         r.sort (Stripable::Sorter());
4163
4164         StateProtector sp (this);
4165
4166         for (RouteList::iterator i = r.begin(); i != r.end(); ++i) {
4167                 assert (!(*i)->is_auditioner());
4168                 if (boost::dynamic_pointer_cast<Track> (*i)) {
4169                         (*i)->set_track_number(++tn);
4170                 }
4171                 else if (!(*i)->is_master() && !(*i)->is_monitor()) {
4172                         (*i)->set_track_number(--bn);
4173                 }
4174         }
4175         const uint32_t decimals = ceilf (log10f (tn + 1));
4176         const bool decimals_changed = _track_number_decimals != decimals;
4177         _track_number_decimals = decimals;
4178
4179         if (decimals_changed && config.get_track_name_number ()) {
4180                 for (RouteList::iterator i = r.begin(); i != r.end(); ++i) {
4181                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (*i);
4182                         if (t) {
4183                                 t->resync_track_name();
4184                         }
4185                 }
4186                 // trigger GUI re-layout
4187                 config.ParameterChanged("track-name-number");
4188         }
4189
4190 #ifndef NDEBUG
4191         if (DEBUG_ENABLED(DEBUG::OrderKeys)) {
4192                 boost::shared_ptr<RouteList> rl = routes.reader ();
4193                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
4194                         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("%1 numbered %2\n", (*i)->name(), (*i)->track_number()));
4195                 }
4196         }
4197 #endif /* NDEBUG */
4198
4199 }
4200
4201 void
4202 Session::playlist_region_added (boost::weak_ptr<Region> w)
4203 {
4204         boost::shared_ptr<Region> r = w.lock ();
4205         if (!r) {
4206                 return;
4207         }
4208
4209         /* These are the operations that are currently in progress... */
4210         list<GQuark> curr = _current_trans_quarks;
4211         curr.sort ();
4212
4213         /* ...and these are the operations during which we want to update
4214            the session range location markers.
4215         */
4216         list<GQuark> ops;
4217         ops.push_back (Operations::capture);
4218         ops.push_back (Operations::paste);
4219         ops.push_back (Operations::duplicate_region);
4220         ops.push_back (Operations::insert_file);
4221         ops.push_back (Operations::insert_region);
4222         ops.push_back (Operations::drag_region_brush);
4223         ops.push_back (Operations::region_drag);
4224         ops.push_back (Operations::selection_grab);
4225         ops.push_back (Operations::region_fill);
4226         ops.push_back (Operations::fill_selection);
4227         ops.push_back (Operations::create_region);
4228         ops.push_back (Operations::region_copy);
4229         ops.push_back (Operations::fixed_time_region_copy);
4230         ops.sort ();
4231
4232         /* See if any of the current operations match the ones that we want */
4233         list<GQuark> in;
4234         set_intersection (_current_trans_quarks.begin(), _current_trans_quarks.end(), ops.begin(), ops.end(), back_inserter (in));
4235
4236         /* If so, update the session range markers */
4237         if (!in.empty ()) {
4238                 maybe_update_session_range (r->position (), r->last_sample ());
4239         }
4240 }
4241
4242 /** Update the session range markers if a is before the current start or
4243  *  b is after the current end.
4244  */
4245 void
4246 Session::maybe_update_session_range (samplepos_t a, samplepos_t b)
4247 {
4248         if (loading ()) {
4249                 return;
4250         }
4251
4252         samplepos_t session_end_marker_shift_samples = session_end_shift * _nominal_sample_rate;
4253
4254         if (_session_range_location == 0) {
4255
4256                 set_session_range_location (a, b + session_end_marker_shift_samples);
4257
4258         } else {
4259
4260                 if (_session_range_is_free && (a < _session_range_location->start())) {
4261                         _session_range_location->set_start (a);
4262                 }
4263
4264                 if (_session_range_is_free && (b > _session_range_location->end())) {
4265                         _session_range_location->set_end (b);
4266                 }
4267         }
4268 }
4269
4270 void
4271 Session::set_session_range_is_free (bool yn)
4272 {
4273         _session_range_is_free = yn;
4274 }
4275
4276 void
4277 Session::playlist_ranges_moved (list<Evoral::RangeMove<samplepos_t> > const & ranges)
4278 {
4279         for (list<Evoral::RangeMove<samplepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
4280                 maybe_update_session_range (i->to, i->to + i->length);
4281         }
4282 }
4283
4284 void
4285 Session::playlist_regions_extended (list<Evoral::Range<samplepos_t> > const & ranges)
4286 {
4287         for (list<Evoral::Range<samplepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
4288                 maybe_update_session_range (i->from, i->to);
4289         }
4290 }
4291
4292 /* Region management */
4293
4294 boost::shared_ptr<Region>
4295 Session::find_whole_file_parent (boost::shared_ptr<Region const> child) const
4296 {
4297         const RegionFactory::RegionMap& regions (RegionFactory::regions());
4298         RegionFactory::RegionMap::const_iterator i;
4299         boost::shared_ptr<Region> region;
4300
4301         Glib::Threads::Mutex::Lock lm (region_lock);
4302
4303         for (i = regions.begin(); i != regions.end(); ++i) {
4304
4305                 region = i->second;
4306
4307                 if (region->whole_file()) {
4308
4309                         if (child->source_equivalent (region)) {
4310                                 return region;
4311                         }
4312                 }
4313         }
4314
4315         return boost::shared_ptr<Region> ();
4316 }
4317
4318 int
4319 Session::destroy_sources (list<boost::shared_ptr<Source> > srcs)
4320 {
4321         set<boost::shared_ptr<Region> > relevant_regions;
4322
4323         for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ++s) {
4324                 RegionFactory::get_regions_using_source (*s, relevant_regions);
4325         }
4326
4327         for (set<boost::shared_ptr<Region> >::iterator r = relevant_regions.begin(); r != relevant_regions.end(); ) {
4328                 set<boost::shared_ptr<Region> >::iterator tmp;
4329
4330                 tmp = r;
4331                 ++tmp;
4332
4333                 _playlists->destroy_region (*r);
4334                 RegionFactory::map_remove (*r);
4335
4336                 (*r)->drop_sources ();
4337                 (*r)->drop_references ();
4338
4339                 relevant_regions.erase (r);
4340
4341                 r = tmp;
4342         }
4343
4344         for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ) {
4345
4346                 {
4347                         Glib::Threads::Mutex::Lock ls (source_lock);
4348                         /* remove from the main source list */
4349                         sources.erase ((*s)->id());
4350                 }
4351
4352                 (*s)->mark_for_remove ();
4353                 (*s)->drop_references ();
4354                 SourceRemoved(*s);
4355
4356                 s = srcs.erase (s);
4357         }
4358
4359         return 0;
4360 }
4361
4362 int
4363 Session::remove_last_capture ()
4364 {
4365         list<boost::shared_ptr<Source> > srcs;
4366
4367         boost::shared_ptr<RouteList> rl = routes.reader ();
4368         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
4369                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
4370                 if (!tr) {
4371                         continue;
4372                 }
4373
4374                 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
4375
4376                 if (!l.empty()) {
4377                         srcs.insert (srcs.end(), l.begin(), l.end());
4378                         l.clear ();
4379                 }
4380         }
4381
4382         destroy_sources (srcs);
4383
4384         save_state (_current_snapshot_name);
4385
4386         return 0;
4387 }
4388
4389 void
4390 Session::get_last_capture_sources (std::list<boost::shared_ptr<Source> >& srcs)
4391 {
4392         boost::shared_ptr<RouteList> rl = routes.reader ();
4393         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
4394                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
4395                 if (!tr) {
4396                         continue;
4397                 }
4398
4399                 list<boost::shared_ptr<Source> >& l = tr->last_capture_sources();
4400
4401                 if (!l.empty()) {
4402                         srcs.insert (srcs.end(), l.begin(), l.end());
4403                         l.clear ();
4404                 }
4405         }
4406 }
4407
4408 /* Source Management */
4409
4410 void
4411 Session::add_source (boost::shared_ptr<Source> source)
4412 {
4413         pair<SourceMap::key_type, SourceMap::mapped_type> entry;
4414         pair<SourceMap::iterator,bool> result;
4415
4416         entry.first = source->id();
4417         entry.second = source;
4418
4419         {
4420                 Glib::Threads::Mutex::Lock lm (source_lock);
4421                 result = sources.insert (entry);
4422         }
4423
4424         if (result.second) {
4425
4426                 /* yay, new source */
4427
4428                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
4429
4430                 if (fs) {
4431                         if (!fs->within_session()) {
4432                                 ensure_search_path_includes (Glib::path_get_dirname (fs->path()), fs->type());
4433                         }
4434                 }
4435
4436                 set_dirty();
4437
4438                 boost::shared_ptr<AudioFileSource> afs;
4439
4440                 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
4441                         if (Config->get_auto_analyse_audio()) {
4442                                 Analyser::queue_source_for_analysis (source, false);
4443                         }
4444                 }
4445
4446                 source->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_source, this, boost::weak_ptr<Source> (source)));
4447
4448                 SourceAdded(source);
4449         }
4450 }
4451
4452 void
4453 Session::remove_source (boost::weak_ptr<Source> src)
4454 {
4455         if (deletion_in_progress ()) {
4456                 return;
4457         }
4458
4459         SourceMap::iterator i;
4460         boost::shared_ptr<Source> source = src.lock();
4461
4462         if (!source) {
4463                 return;
4464         }
4465
4466         {
4467                 Glib::Threads::Mutex::Lock lm (source_lock);
4468
4469                 if ((i = sources.find (source->id())) != sources.end()) {
4470                         sources.erase (i);
4471                         SourceRemoved(source);
4472                 }
4473         }
4474
4475         if (!in_cleanup () && !loading ()) {
4476
4477                 /* save state so we don't end up with a session file
4478                  * referring to non-existent sources.
4479                  */
4480
4481                 save_state (_current_snapshot_name);
4482         }
4483 }
4484
4485 boost::shared_ptr<Source>
4486 Session::source_by_id (const PBD::ID& id)
4487 {
4488         Glib::Threads::Mutex::Lock lm (source_lock);
4489         SourceMap::iterator i;
4490         boost::shared_ptr<Source> source;
4491
4492         if ((i = sources.find (id)) != sources.end()) {
4493                 source = i->second;
4494         }
4495
4496         return source;
4497 }
4498
4499 boost::shared_ptr<AudioFileSource>
4500 Session::audio_source_by_path_and_channel (const string& path, uint16_t chn) const
4501 {
4502         /* Restricted to audio files because only audio sources have channel
4503            as a property.
4504         */
4505
4506         Glib::Threads::Mutex::Lock lm (source_lock);
4507
4508         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4509                 boost::shared_ptr<AudioFileSource> afs
4510                         = boost::dynamic_pointer_cast<AudioFileSource>(i->second);
4511
4512                 if (afs && afs->path() == path && chn == afs->channel()) {
4513                         return afs;
4514                 }
4515         }
4516
4517         return boost::shared_ptr<AudioFileSource>();
4518 }
4519
4520 boost::shared_ptr<MidiSource>
4521 Session::midi_source_by_path (const std::string& path, bool need_source_lock) const
4522 {
4523         /* Restricted to MIDI files because audio sources require a channel
4524            for unique identification, in addition to a path.
4525         */
4526
4527         Glib::Threads::Mutex::Lock lm (source_lock, Glib::Threads::NOT_LOCK);
4528         if (need_source_lock) {
4529                 lm.acquire ();
4530         }
4531
4532         for (SourceMap::const_iterator s = sources.begin(); s != sources.end(); ++s) {
4533                 boost::shared_ptr<MidiSource> ms
4534                         = boost::dynamic_pointer_cast<MidiSource>(s->second);
4535                 boost::shared_ptr<FileSource> fs
4536                         = boost::dynamic_pointer_cast<FileSource>(s->second);
4537
4538                 if (ms && fs && fs->path() == path) {
4539                         return ms;
4540                 }
4541         }
4542
4543         return boost::shared_ptr<MidiSource>();
4544 }
4545
4546 uint32_t
4547 Session::count_sources_by_origin (const string& path)
4548 {
4549         uint32_t cnt = 0;
4550         Glib::Threads::Mutex::Lock lm (source_lock);
4551
4552         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
4553                 boost::shared_ptr<FileSource> fs
4554                         = boost::dynamic_pointer_cast<FileSource>(i->second);
4555
4556                 if (fs && fs->origin() == path) {
4557                         ++cnt;
4558                 }
4559         }
4560
4561         return cnt;
4562 }
4563
4564 static string
4565 peak_file_helper (const string& peak_path, const string& file_path, const string& file_base, bool hash) {
4566         if (hash) {
4567                 std::string checksum = Glib::Checksum::compute_checksum(Glib::Checksum::CHECKSUM_SHA1, file_path + G_DIR_SEPARATOR + file_base);
4568                 return Glib::build_filename (peak_path, checksum + peakfile_suffix);
4569         } else {
4570                 return Glib::build_filename (peak_path, file_base + peakfile_suffix);
4571         }
4572 }
4573
4574 string
4575 Session::construct_peak_filepath (const string& filepath, const bool in_session, const bool old_peak_name) const
4576 {
4577         string interchange_dir_string = string (interchange_dir_name) + G_DIR_SEPARATOR;
4578
4579         if (Glib::path_is_absolute (filepath)) {
4580
4581                 /* rip the session dir from the audiofile source */
4582
4583                 string session_path;
4584                 bool in_another_session = true;
4585
4586                 if (filepath.find (interchange_dir_string) != string::npos) {
4587
4588                         session_path = Glib::path_get_dirname (filepath); /* now ends in audiofiles */
4589                         session_path = Glib::path_get_dirname (session_path); /* now ends in session name */
4590                         session_path = Glib::path_get_dirname (session_path); /* now ends in interchange */
4591                         session_path = Glib::path_get_dirname (session_path); /* now has session path */
4592
4593                         /* see if it is within our session */
4594
4595                         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4596                                 if (i->path == session_path) {
4597                                         in_another_session = false;
4598                                         break;
4599                                 }
4600                         }
4601                 } else {
4602                         in_another_session = false;
4603                 }
4604
4605
4606                 if (in_another_session) {
4607                         SessionDirectory sd (session_path);
4608                         return peak_file_helper (sd.peak_path(), "", Glib::path_get_basename (filepath), !old_peak_name);
4609                 }
4610         }
4611
4612         /* 1) if file belongs to this session
4613          * it may be a relative path (interchange/...)
4614          * or just basename (session_state, remove source)
4615          * -> just use the basename
4616          */
4617         std::string filename = Glib::path_get_basename (filepath);
4618         std::string path;
4619
4620         /* 2) if the file is outside our session dir:
4621          * (imported but not copied) add the path for check-summming */
4622         if (!in_session) {
4623                 path = Glib::path_get_dirname (filepath);
4624         }
4625
4626         return peak_file_helper (_session_dir->peak_path(), path, Glib::path_get_basename (filepath), !old_peak_name);
4627 }
4628
4629 string
4630 Session::new_audio_source_path_for_embedded (const std::string& path)
4631 {
4632         /* embedded source:
4633          *
4634          * we know that the filename is already unique because it exists
4635          * out in the filesystem.
4636          *
4637          * However, when we bring it into the session, we could get a
4638          * collision.
4639          *
4640          * Eg. two embedded files:
4641          *
4642          *          /foo/bar/baz.wav
4643          *          /frob/nic/baz.wav
4644          *
4645          * When merged into session, these collide.
4646          *
4647          * There will not be a conflict with in-memory sources
4648          * because when the source was created we already picked
4649          * a unique name for it.
4650          *
4651          * This collision is not likely to be common, but we have to guard
4652          * against it.  So, if there is a collision, take the md5 hash of the
4653          * the path, and use that as the filename instead.
4654          */
4655
4656         SessionDirectory sdir (get_best_session_directory_for_new_audio());
4657         string base = Glib::path_get_basename (path);
4658         string newpath = Glib::build_filename (sdir.sound_path(), base);
4659
4660         if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
4661
4662                 MD5 md5;
4663
4664                 md5.digestString (path.c_str());
4665                 md5.writeToString ();
4666                 base = md5.digestChars;
4667
4668                 string ext = get_suffix (path);
4669
4670                 if (!ext.empty()) {
4671                         base += '.';
4672                         base += ext;
4673                 }
4674
4675                 newpath = Glib::build_filename (sdir.sound_path(), base);
4676
4677                 /* if this collides, we're screwed */
4678
4679                 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
4680                         error << string_compose (_("Merging embedded file %1: name collision AND md5 hash collision!"), path) << endmsg;
4681                         return string();
4682                 }
4683
4684         }
4685
4686         return newpath;
4687 }
4688
4689 /** Return true if there are no audio file sources that use @param name as
4690  * the filename component of their path.
4691  *
4692  * Return false otherwise.
4693  *
4694  * This method MUST ONLY be used to check in-session, mono files since it
4695  * hard-codes the channel of the audio file source we are looking for as zero.
4696  *
4697  * If/when Ardour supports native files in non-mono formats, the logic here
4698  * will need to be revisited.
4699  */
4700 bool
4701 Session::audio_source_name_is_unique (const string& name)
4702 {
4703         std::vector<string> sdirs = source_search_path (DataType::AUDIO);
4704         vector<space_and_path>::iterator i;
4705         uint32_t existing = 0;
4706
4707         for (vector<string>::const_iterator i = sdirs.begin(); i != sdirs.end(); ++i) {
4708
4709                 /* note that we search *without* the extension so that
4710                    we don't end up both "Audio 1-1.wav" and "Audio 1-1.caf"
4711                    in the event that this new name is required for
4712                    a file format change.
4713                 */
4714
4715                 const string spath = *i;
4716
4717                 if (matching_unsuffixed_filename_exists_in (spath, name)) {
4718                         existing++;
4719                         break;
4720                 }
4721
4722                 /* it is possible that we have the path already
4723                  * assigned to a source that has not yet been written
4724                  * (ie. the write source for a diskstream). we have to
4725                  * check this in order to make sure that our candidate
4726                  * path isn't used again, because that can lead to
4727                  * two Sources point to the same file with different
4728                  * notions of their removability.
4729                  */
4730
4731
4732                 string possible_path = Glib::build_filename (spath, name);
4733
4734                 if (audio_source_by_path_and_channel (possible_path, 0)) {
4735                         existing++;
4736                         break;
4737                 }
4738         }
4739
4740         return (existing == 0);
4741 }
4742
4743 string
4744 Session::format_audio_source_name (const string& legalized_base, uint32_t nchan, uint32_t chan, bool destructive, bool take_required, uint32_t cnt, bool related_exists)
4745 {
4746         ostringstream sstr;
4747         const string ext = native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
4748
4749         if (Profile->get_trx() && destructive) {
4750                 sstr << 'T';
4751                 sstr << setfill ('0') << setw (4) << cnt;
4752                 sstr << legalized_base;
4753         } else {
4754                 sstr << legalized_base;
4755
4756                 if (take_required || related_exists) {
4757                         sstr << '-';
4758                         sstr << cnt;
4759                 }
4760         }
4761
4762         if (nchan == 2) {
4763                 if (chan == 0) {
4764                         sstr << "%L";
4765                 } else {
4766                         sstr << "%R";
4767                 }
4768         } else if (nchan > 2) {
4769                 if (nchan < 26) {
4770                         sstr << '%';
4771                         sstr << 'a' + chan;
4772                 } else {
4773                         /* XXX what? more than 26 channels! */
4774                         sstr << '%';
4775                         sstr << chan+1;
4776                 }
4777         }
4778
4779         sstr << ext;
4780
4781         return sstr.str();
4782 }
4783
4784 /** Return a unique name based on \a base for a new internal audio source */
4785 string
4786 Session::new_audio_source_path (const string& base, uint32_t nchan, uint32_t chan, bool destructive, bool take_required)
4787 {
4788         uint32_t cnt;
4789         string possible_name;
4790         const uint32_t limit = 9999; // arbitrary limit on number of files with the same basic name
4791         string legalized;
4792         bool some_related_source_name_exists = false;
4793
4794         legalized = legalize_for_path (base);
4795
4796         // Find a "version" of the base name that doesn't exist in any of the possible directories.
4797
4798         for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
4799
4800                 possible_name = format_audio_source_name (legalized, nchan, chan, destructive, take_required, cnt, some_related_source_name_exists);
4801
4802                 if (audio_source_name_is_unique (possible_name)) {
4803                         break;
4804                 }
4805
4806                 some_related_source_name_exists = true;
4807
4808                 if (cnt > limit) {
4809                         error << string_compose(
4810                                         _("There are already %1 recordings for %2, which I consider too many."),
4811                                         limit, base) << endmsg;
4812                         destroy ();
4813                         throw failed_constructor();
4814                 }
4815         }
4816
4817         /* We've established that the new name does not exist in any session
4818          * directory, so now find out which one we should use for this new
4819          * audio source.
4820          */
4821
4822         SessionDirectory sdir (get_best_session_directory_for_new_audio());
4823
4824         std::string s = Glib::build_filename (sdir.sound_path(), possible_name);
4825
4826         return s;
4827 }
4828
4829 /** Return a unique name based on `base` for a new internal MIDI source */
4830 string
4831 Session::new_midi_source_path (const string& base, bool need_lock)
4832 {
4833         string possible_path;
4834         string possible_name;
4835
4836         possible_name = legalize_for_path (base);
4837
4838         // Find a "version" of the file name that doesn't exist in any of the possible directories.
4839         std::vector<string> sdirs = source_search_path(DataType::MIDI);
4840
4841         /* - the main session folder is the first in the vector.
4842          * - after checking all locations for file-name uniqueness,
4843          *   we keep the one from the last iteration as new file name
4844          * - midi files are small and should just be kept in the main session-folder
4845          *
4846          * -> reverse the array, check main session folder last and use that as location
4847          *    for MIDI files.
4848          */
4849         std::reverse(sdirs.begin(), sdirs.end());
4850
4851         while (true) {
4852                 possible_name = bump_name_once (possible_name, '-');
4853
4854                 vector<space_and_path>::iterator i;
4855                 uint32_t existing = 0;
4856
4857                 for (vector<string>::const_iterator i = sdirs.begin(); i != sdirs.end(); ++i) {
4858
4859                         possible_path = Glib::build_filename (*i, possible_name + ".mid");
4860
4861                         if (Glib::file_test (possible_path, Glib::FILE_TEST_EXISTS)) {
4862                                 existing++;
4863                         }
4864
4865                         if (midi_source_by_path (possible_path, need_lock)) {
4866                                 existing++;
4867                         }
4868                 }
4869
4870                 if (possible_path.size () >= PATH_MAX) {
4871                         error << string_compose(
4872                                         _("There are already many recordings for %1, resulting in a too long file-path %2."),
4873                                         base, possible_path) << endmsg;
4874                         destroy ();
4875                         return 0;
4876                 }
4877
4878                 if (existing == 0) {
4879                         break;
4880                 }
4881         }
4882
4883         /* No need to "find best location" for software/app-based RAID, because
4884            MIDI is so small that we always put it in the same place.
4885         */
4886
4887         return possible_path;
4888 }
4889
4890
4891 /** Create a new within-session audio source */
4892 boost::shared_ptr<AudioFileSource>
4893 Session::create_audio_source_for_session (size_t n_chans, string const & base, uint32_t chan, bool destructive)
4894 {
4895         const string path = new_audio_source_path (base, n_chans, chan, destructive, true);
4896
4897         if (!path.empty()) {
4898                 return boost::dynamic_pointer_cast<AudioFileSource> (
4899                         SourceFactory::createWritable (DataType::AUDIO, *this, path, destructive, sample_rate(), true, true));
4900         } else {
4901                 throw failed_constructor ();
4902         }
4903 }
4904
4905 /** Create a new within-session MIDI source */
4906 boost::shared_ptr<MidiSource>
4907 Session::create_midi_source_for_session (string const & basic_name)
4908 {
4909         const string path = new_midi_source_path (basic_name);
4910
4911         if (!path.empty()) {
4912                 return boost::dynamic_pointer_cast<SMFSource> (
4913                         SourceFactory::createWritable (
4914                                 DataType::MIDI, *this, path, false, sample_rate()));
4915         } else {
4916                 throw failed_constructor ();
4917         }
4918 }
4919
4920 /** Create a new within-session MIDI source */
4921 boost::shared_ptr<MidiSource>
4922 Session::create_midi_source_by_stealing_name (boost::shared_ptr<Track> track)
4923 {
4924         /* the caller passes in the track the source will be used in,
4925            so that we can keep the numbering sane.
4926
4927            Rationale: a track with the name "Foo" that has had N
4928            captures carried out so far will ALREADY have a write source
4929            named "Foo-N+1.mid" waiting to be used for the next capture.
4930
4931            If we call new_midi_source_name() we will get "Foo-N+2". But
4932            there is no region corresponding to "Foo-N+1", so when
4933            "Foo-N+2" appears in the track, the gap presents the user
4934            with odd behaviour - why did it skip past Foo-N+1?
4935
4936            We could explain this to the user in some odd way, but
4937            instead we rename "Foo-N+1.mid" as "Foo-N+2.mid", and then
4938            use "Foo-N+1" here.
4939
4940            If that attempted rename fails, we get "Foo-N+2.mid" anyway.
4941         */
4942
4943         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (track);
4944         assert (mt);
4945         std::string name = track->steal_write_source_name ();
4946
4947         if (name.empty()) {
4948                 return boost::shared_ptr<MidiSource>();
4949         }
4950
4951         /* MIDI files are small, just put them in the first location of the
4952            session source search path.
4953         */
4954
4955         const string path = Glib::build_filename (source_search_path (DataType::MIDI).front(), name);
4956
4957         return boost::dynamic_pointer_cast<SMFSource> (
4958                 SourceFactory::createWritable (
4959                         DataType::MIDI, *this, path, false, sample_rate()));
4960 }
4961
4962 bool
4963 Session::playlist_is_active (boost::shared_ptr<Playlist> playlist)
4964 {
4965         Glib::Threads::Mutex::Lock lm (_playlists->lock);
4966         for (SessionPlaylists::List::iterator i = _playlists->playlists.begin(); i != _playlists->playlists.end(); i++) {
4967                 if ( (*i) == playlist ) {
4968                         return true;
4969                 }
4970         }
4971         return false;
4972 }
4973
4974 void
4975 Session::add_playlist (boost::shared_ptr<Playlist> playlist, bool unused)
4976 {
4977         if (playlist->hidden()) {
4978                 return;
4979         }
4980
4981         _playlists->add (playlist);
4982
4983         if (unused) {
4984                 playlist->release();
4985         }
4986
4987         set_dirty();
4988 }
4989
4990 void
4991 Session::remove_playlist (boost::weak_ptr<Playlist> weak_playlist)
4992 {
4993         if (deletion_in_progress ()) {
4994                 return;
4995         }
4996
4997         boost::shared_ptr<Playlist> playlist (weak_playlist.lock());
4998
4999         if (!playlist) {
5000                 return;
5001         }
5002
5003         _playlists->remove (playlist);
5004
5005         set_dirty();
5006 }
5007
5008 void
5009 Session::set_audition (boost::shared_ptr<Region> r)
5010 {
5011         pending_audition_region = r;
5012         add_post_transport_work (PostTransportAudition);
5013         _butler->schedule_transport_work ();
5014 }
5015
5016 void
5017 Session::audition_playlist ()
5018 {
5019         SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
5020         ev->region.reset ();
5021         queue_event (ev);
5022 }
5023
5024
5025 void
5026 Session::register_lua_function (
5027                 const std::string& name,
5028                 const std::string& script,
5029                 const LuaScriptParamList& args
5030                 )
5031 {
5032         Glib::Threads::Mutex::Lock lm (lua_lock);
5033
5034         lua_State* L = lua.getState();
5035
5036         const std::string& bytecode = LuaScripting::get_factory_bytecode (script);
5037         luabridge::LuaRef tbl_arg (luabridge::newTable(L));
5038         for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
5039                 if ((*i)->optional && !(*i)->is_set) { continue; }
5040                 tbl_arg[(*i)->name] = (*i)->value;
5041         }
5042         (*_lua_add)(name, bytecode, tbl_arg); // throws luabridge::LuaException
5043         lm.release();
5044
5045         LuaScriptsChanged (); /* EMIT SIGNAL */
5046         set_dirty();
5047 }
5048
5049 void
5050 Session::unregister_lua_function (const std::string& name)
5051 {
5052         Glib::Threads::Mutex::Lock lm (lua_lock);
5053         (*_lua_del)(name); // throws luabridge::LuaException
5054         lua.collect_garbage ();
5055         lm.release();
5056
5057         LuaScriptsChanged (); /* EMIT SIGNAL */
5058         set_dirty();
5059 }
5060
5061 std::vector<std::string>
5062 Session::registered_lua_functions ()
5063 {
5064         Glib::Threads::Mutex::Lock lm (lua_lock);
5065         std::vector<std::string> rv;
5066
5067         try {
5068                 luabridge::LuaRef list ((*_lua_list)());
5069                 for (luabridge::Iterator i (list); !i.isNil (); ++i) {
5070                         if (!i.key ().isString ()) { assert(0); continue; }
5071                         rv.push_back (i.key ().cast<std::string> ());
5072                 }
5073         } catch (...) { }
5074         return rv;
5075 }
5076
5077 #ifndef NDEBUG
5078 static void _lua_print (std::string s) {
5079         std::cout << "SessionLua: " << s << "\n";
5080 }
5081 #endif
5082
5083 void
5084 Session::try_run_lua (pframes_t nframes)
5085 {
5086         if (_n_lua_scripts == 0) return;
5087         Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK);
5088         if (tm.locked ()) {
5089                 try { (*_lua_run)(nframes); } catch (...) { }
5090                 lua.collect_garbage_step ();
5091         }
5092 }
5093
5094 void
5095 Session::setup_lua ()
5096 {
5097 #ifndef NDEBUG
5098         lua.Print.connect (&_lua_print);
5099 #endif
5100         lua.sandbox (true);
5101         lua.do_command (
5102                         "function ArdourSession ()"
5103                         "  local self = { scripts = {}, instances = {} }"
5104                         ""
5105                         "  local remove = function (n)"
5106                         "   self.scripts[n] = nil"
5107                         "   self.instances[n] = nil"
5108                         "   Session:scripts_changed()" // call back
5109                         "  end"
5110                         ""
5111                         "  local addinternal = function (n, f, a)"
5112                         "   assert(type(n) == 'string', 'function-name must be string')"
5113                         "   assert(type(f) == 'function', 'Given script is a not a function')"
5114                         "   assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
5115                         "   assert(self.scripts[n] == nil, 'Callback \"'.. n ..'\" already exists.')"
5116                         "   self.scripts[n] = { ['f'] = f, ['a'] = a }"
5117                         "   local env = { print = print, tostring = tostring, assert = assert, ipairs = ipairs, error = error, select = select, string = string, type = type, tonumber = tonumber, collectgarbage = collectgarbage, pairs = pairs, math = math, table = table, pcall = pcall, bit32=bit32, Session = Session, PBD = PBD, Timecode = Timecode, Evoral = Evoral, C = C, ARDOUR = ARDOUR }"
5118                         "   self.instances[n] = load (string.dump(f, true), nil, nil, env)(a)"
5119                         "   Session:scripts_changed()" // call back
5120                         "  end"
5121                         ""
5122                         "  local add = function (n, b, a)"
5123                         "   assert(type(b) == 'string', 'ByteCode must be string')"
5124                         "   load (b)()" // assigns f
5125                         "   assert(type(f) == 'string', 'Assigned ByteCode must be string')"
5126                         "   addinternal (n, load(f), a)"
5127                         "  end"
5128                         ""
5129                         "  local run = function (...)"
5130                         "   for n, s in pairs (self.instances) do"
5131                         "     local status, err = pcall (s, ...)"
5132                         "     if not status then"
5133                         "       print ('fn \"'.. n .. '\": ', err)"
5134                         "       remove (n)"
5135                         "      end"
5136                         "   end"
5137                         "   collectgarbage()"
5138                         "  end"
5139                         ""
5140                         "  local cleanup = function ()"
5141                         "   self.scripts = nil"
5142                         "   self.instances = nil"
5143                         "  end"
5144                         ""
5145                         "  local list = function ()"
5146                         "   local rv = {}"
5147                         "   for n, _ in pairs (self.scripts) do"
5148                         "     rv[n] = true"
5149                         "   end"
5150                         "   return rv"
5151                         "  end"
5152                         ""
5153                         "  local function basic_serialize (o)"
5154                         "    if type(o) == \"number\" then"
5155                         "     return tostring(o)"
5156                         "    else"
5157                         "     return string.format(\"%q\", o)"
5158                         "    end"
5159                         "  end"
5160                         ""
5161                         "  local function serialize (name, value)"
5162                         "   local rv = name .. ' = '"
5163                         "   collectgarbage()"
5164                         "   if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
5165                         "    return rv .. basic_serialize(value) .. ' '"
5166                         "   elseif type(value) == \"table\" then"
5167                         "    rv = rv .. '{} '"
5168                         "    for k,v in pairs(value) do"
5169                         "     local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
5170                         "     rv = rv .. serialize(fieldname, v) .. ' '"
5171                         "     collectgarbage()" // string concatenation allocates a new string :(
5172                         "    end"
5173                         "    return rv;"
5174                         "   elseif type(value) == \"function\" then"
5175                         "     return rv .. string.format(\"%q\", string.dump(value, true))"
5176                         "   else"
5177                         "    error('cannot save a ' .. type(value))"
5178                         "   end"
5179                         "  end"
5180                         ""
5181                         ""
5182                         "  local save = function ()"
5183                         "   return (serialize('scripts', self.scripts))"
5184                         "  end"
5185                         ""
5186                         "  local restore = function (state)"
5187                         "   self.scripts = {}"
5188                         "   load (state)()"
5189                         "   for n, s in pairs (scripts) do"
5190                         "    addinternal (n, load(s['f']), s['a'])"
5191                         "   end"
5192                         "  end"
5193                         ""
5194                         " return { run = run, add = add, remove = remove,"
5195                   "          list = list, restore = restore, save = save, cleanup = cleanup}"
5196                         " end"
5197                         " "
5198                         " sess = ArdourSession ()"
5199                         " ArdourSession = nil"
5200                         " "
5201                         "function ardour () end"
5202                         );
5203
5204         lua_State* L = lua.getState();
5205
5206         try {
5207                 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
5208                 lua.do_command ("sess = nil"); // hide it.
5209                 lua.do_command ("collectgarbage()");
5210
5211                 _lua_run = new luabridge::LuaRef(lua_sess["run"]);
5212                 _lua_add = new luabridge::LuaRef(lua_sess["add"]);
5213                 _lua_del = new luabridge::LuaRef(lua_sess["remove"]);
5214                 _lua_list = new luabridge::LuaRef(lua_sess["list"]);
5215                 _lua_save = new luabridge::LuaRef(lua_sess["save"]);
5216                 _lua_load = new luabridge::LuaRef(lua_sess["restore"]);
5217                 _lua_cleanup = new luabridge::LuaRef(lua_sess["cleanup"]);
5218         } catch (luabridge::LuaException const& e) {
5219                 fatal << string_compose (_("programming error: %1"),
5220                                 std::string ("Failed to setup session Lua interpreter") + e.what ())
5221                         << endmsg;
5222                 abort(); /*NOTREACHED*/
5223         } catch (...) {
5224                 fatal << string_compose (_("programming error: %1"),
5225                                 X_("Failed to setup session Lua interpreter"))
5226                         << endmsg;
5227                 abort(); /*NOTREACHED*/
5228         }
5229
5230         lua_mlock (L, 1);
5231         LuaBindings::stddef (L);
5232         LuaBindings::common (L);
5233         LuaBindings::dsp (L);
5234         lua_mlock (L, 0);
5235         luabridge::push <Session *> (L, this);
5236         lua_setglobal (L, "Session");
5237 }
5238
5239 void
5240 Session::scripts_changed ()
5241 {
5242         assert (!lua_lock.trylock()); // must hold lua_lock
5243
5244         try {
5245                 luabridge::LuaRef list ((*_lua_list)());
5246                 int cnt = 0;
5247                 for (luabridge::Iterator i (list); !i.isNil (); ++i) {
5248                         if (!i.key ().isString ()) { assert(0); continue; }
5249                         ++cnt;
5250                 }
5251                 _n_lua_scripts = cnt;
5252         } catch (luabridge::LuaException const& e) {
5253                 fatal << string_compose (_("programming error: %1"),
5254                                 std::string ("Indexing Lua Session Scripts failed.") + e.what ())
5255                         << endmsg;
5256                 abort(); /*NOTREACHED*/
5257         } catch (...) {
5258                 fatal << string_compose (_("programming error: %1"),
5259                                 X_("Indexing Lua Session Scripts failed."))
5260                         << endmsg;
5261                 abort(); /*NOTREACHED*/
5262         }
5263 }
5264
5265 void
5266 Session::non_realtime_set_audition ()
5267 {
5268         assert (pending_audition_region);
5269         auditioner->audition_region (pending_audition_region);
5270         pending_audition_region.reset ();
5271         AuditionActive (true); /* EMIT SIGNAL */
5272 }
5273
5274 void
5275 Session::audition_region (boost::shared_ptr<Region> r)
5276 {
5277         SessionEvent* ev = new SessionEvent (SessionEvent::Audition, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
5278         ev->region = r;
5279         queue_event (ev);
5280 }
5281
5282 void
5283 Session::cancel_audition ()
5284 {
5285         if (!auditioner) {
5286                 return;
5287         }
5288         if (auditioner->auditioning()) {
5289                 auditioner->cancel_audition ();
5290                 AuditionActive (false); /* EMIT SIGNAL */
5291         }
5292 }
5293
5294 bool
5295 Session::is_auditioning () const
5296 {
5297         /* can be called before we have an auditioner object */
5298         if (auditioner) {
5299                 return auditioner->auditioning();
5300         } else {
5301                 return false;
5302         }
5303 }
5304
5305 void
5306 Session::graph_reordered ()
5307 {
5308         /* don't do this stuff if we are setting up connections
5309            from a set_state() call or creating new tracks. Ditto for deletion.
5310         */
5311
5312         if (inital_connect_or_deletion_in_progress () || _adding_routes_in_progress || _reconnecting_routes_in_progress || _route_deletion_in_progress) {
5313                 return;
5314         }
5315
5316         resort_routes ();
5317
5318         /* force all diskstreams to update their capture offset values to
5319          * reflect any changes in latencies within the graph.
5320          */
5321         update_route_latency (false, true);
5322 }
5323
5324 /** @return Number of samples that there is disk space available to write,
5325  *  if known.
5326  */
5327 boost::optional<samplecnt_t>
5328 Session::available_capture_duration ()
5329 {
5330         Glib::Threads::Mutex::Lock lm (space_lock);
5331
5332         if (_total_free_4k_blocks_uncertain) {
5333                 return boost::optional<samplecnt_t> ();
5334         }
5335
5336         float sample_bytes_on_disk = 4.0; // keep gcc happy
5337
5338         switch (config.get_native_file_data_format()) {
5339         case FormatFloat:
5340                 sample_bytes_on_disk = 4.0;
5341                 break;
5342
5343         case FormatInt24:
5344                 sample_bytes_on_disk = 3.0;
5345                 break;
5346
5347         case FormatInt16:
5348                 sample_bytes_on_disk = 2.0;
5349                 break;
5350
5351         default:
5352                 /* impossible, but keep some gcc versions happy */
5353                 fatal << string_compose (_("programming error: %1"),
5354                                          X_("illegal native file data format"))
5355                       << endmsg;
5356                 abort(); /*NOTREACHED*/
5357         }
5358
5359         double scale = 4096.0 / sample_bytes_on_disk;
5360
5361         if (_total_free_4k_blocks * scale > (double) max_samplecnt) {
5362                 return max_samplecnt;
5363         }
5364
5365         return (samplecnt_t) floor (_total_free_4k_blocks * scale);
5366 }
5367
5368 void
5369 Session::tempo_map_changed (const PropertyChange&)
5370 {
5371         clear_clicks ();
5372
5373         _playlists->update_after_tempo_map_change ();
5374
5375         _locations->apply (*this, &Session::update_locations_after_tempo_map_change);
5376
5377         set_dirty ();
5378 }
5379
5380 void
5381 Session::update_locations_after_tempo_map_change (const Locations::LocationList& loc)
5382 {
5383         for (Locations::LocationList::const_iterator i = loc.begin(); i != loc.end(); ++i) {
5384                 (*i)->recompute_samples_from_beat ();
5385         }
5386 }
5387
5388 /** Ensures that all buffers (scratch, send, silent, etc) are allocated for
5389  * the given count with the current block size.
5390  */
5391 void
5392 Session::ensure_buffers (ChanCount howmany)
5393 {
5394         BufferManager::ensure_buffers (howmany, bounce_processing() ? bounce_chunk_size : 0);
5395 }
5396
5397 void
5398 Session::ensure_buffer_set(BufferSet& buffers, const ChanCount& count)
5399 {
5400         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
5401                 buffers.ensure_buffers(*t, count.get(*t), _engine.raw_buffer_size(*t));
5402         }
5403 }
5404
5405 uint32_t
5406 Session::next_insert_id ()
5407 {
5408         /* this doesn't really loop forever. just think about it */
5409
5410         while (true) {
5411                 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < insert_bitset.size(); ++n) {
5412                         if (!insert_bitset[n]) {
5413                                 insert_bitset[n] = true;
5414                                 return n;
5415
5416                         }
5417                 }
5418
5419                 /* none available, so resize and try again */
5420
5421                 insert_bitset.resize (insert_bitset.size() + 16, false);
5422         }
5423 }
5424
5425 uint32_t
5426 Session::next_send_id ()
5427 {
5428         /* this doesn't really loop forever. just think about it */
5429
5430         while (true) {
5431                 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < send_bitset.size(); ++n) {
5432                         if (!send_bitset[n]) {
5433                                 send_bitset[n] = true;
5434                                 return n;
5435
5436                         }
5437                 }
5438
5439                 /* none available, so resize and try again */
5440
5441                 send_bitset.resize (send_bitset.size() + 16, false);
5442         }
5443 }
5444
5445 uint32_t
5446 Session::next_aux_send_id ()
5447 {
5448         /* this doesn't really loop forever. just think about it */
5449
5450         while (true) {
5451                 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < aux_send_bitset.size(); ++n) {
5452                         if (!aux_send_bitset[n]) {
5453                                 aux_send_bitset[n] = true;
5454                                 return n;
5455
5456                         }
5457                 }
5458
5459                 /* none available, so resize and try again */
5460
5461                 aux_send_bitset.resize (aux_send_bitset.size() + 16, false);
5462         }
5463 }
5464
5465 uint32_t
5466 Session::next_return_id ()
5467 {
5468         /* this doesn't really loop forever. just think about it */
5469
5470         while (true) {
5471                 for (boost::dynamic_bitset<uint32_t>::size_type n = 1; n < return_bitset.size(); ++n) {
5472                         if (!return_bitset[n]) {
5473                                 return_bitset[n] = true;
5474                                 return n;
5475
5476                         }
5477                 }
5478
5479                 /* none available, so resize and try again */
5480
5481                 return_bitset.resize (return_bitset.size() + 16, false);
5482         }
5483 }
5484
5485 void
5486 Session::mark_send_id (uint32_t id)
5487 {
5488         if (id >= send_bitset.size()) {
5489                 send_bitset.resize (id+16, false);
5490         }
5491         if (send_bitset[id]) {
5492                 warning << string_compose (_("send ID %1 appears to be in use already"), id) << endmsg;
5493         }
5494         send_bitset[id] = true;
5495 }
5496
5497 void
5498 Session::mark_aux_send_id (uint32_t id)
5499 {
5500         if (id >= aux_send_bitset.size()) {
5501                 aux_send_bitset.resize (id+16, false);
5502         }
5503         if (aux_send_bitset[id]) {
5504                 warning << string_compose (_("aux send ID %1 appears to be in use already"), id) << endmsg;
5505         }
5506         aux_send_bitset[id] = true;
5507 }
5508
5509 void
5510 Session::mark_return_id (uint32_t id)
5511 {
5512         if (id >= return_bitset.size()) {
5513                 return_bitset.resize (id+16, false);
5514         }
5515         if (return_bitset[id]) {
5516                 warning << string_compose (_("return ID %1 appears to be in use already"), id) << endmsg;
5517         }
5518         return_bitset[id] = true;
5519 }
5520
5521 void
5522 Session::mark_insert_id (uint32_t id)
5523 {
5524         if (id >= insert_bitset.size()) {
5525                 insert_bitset.resize (id+16, false);
5526         }
5527         if (insert_bitset[id]) {
5528                 warning << string_compose (_("insert ID %1 appears to be in use already"), id) << endmsg;
5529         }
5530         insert_bitset[id] = true;
5531 }
5532
5533 void
5534 Session::unmark_send_id (uint32_t id)
5535 {
5536         if (id < send_bitset.size()) {
5537                 send_bitset[id] = false;
5538         }
5539 }
5540
5541 void
5542 Session::unmark_aux_send_id (uint32_t id)
5543 {
5544         if (id < aux_send_bitset.size()) {
5545                 aux_send_bitset[id] = false;
5546         }
5547 }
5548
5549 void
5550 Session::unmark_return_id (uint32_t id)
5551 {
5552         if (deletion_in_progress ()) {
5553                 return;
5554         }
5555         if (id < return_bitset.size()) {
5556                 return_bitset[id] = false;
5557         }
5558 }
5559
5560 void
5561 Session::unmark_insert_id (uint32_t id)
5562 {
5563         if (id < insert_bitset.size()) {
5564                 insert_bitset[id] = false;
5565         }
5566 }
5567
5568 void
5569 Session::reset_native_file_format ()
5570 {
5571         boost::shared_ptr<RouteList> rl = routes.reader ();
5572
5573         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
5574                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
5575                 if (tr) {
5576                         /* don't save state as we do this, there's no point */
5577                         _state_of_the_state = StateOfTheState (_state_of_the_state | InCleanup);
5578                         tr->reset_write_sources (false);
5579                         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
5580                 }
5581         }
5582 }
5583
5584 bool
5585 Session::route_name_unique (string n) const
5586 {
5587         boost::shared_ptr<RouteList> r = routes.reader ();
5588
5589         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5590                 if ((*i)->name() == n) {
5591                         return false;
5592                 }
5593         }
5594
5595         return true;
5596 }
5597
5598 bool
5599 Session::route_name_internal (string n) const
5600 {
5601         if (auditioner && auditioner->name() == n) {
5602                 return true;
5603         }
5604
5605         if (_click_io && _click_io->name() == n) {
5606                 return true;
5607         }
5608
5609         return false;
5610 }
5611
5612 int
5613 Session::freeze_all (InterThreadInfo& itt)
5614 {
5615         boost::shared_ptr<RouteList> r = routes.reader ();
5616
5617         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
5618
5619                 boost::shared_ptr<Track> t;
5620
5621                 if ((t = boost::dynamic_pointer_cast<Track>(*i)) != 0) {
5622                         /* XXX this is wrong because itt.progress will keep returning to zero at the start
5623                            of every track.
5624                         */
5625                         t->freeze_me (itt);
5626                 }
5627         }
5628
5629         return 0;
5630 }
5631
5632 boost::shared_ptr<Region>
5633 Session::write_one_track (Track& track, samplepos_t start, samplepos_t end,
5634                           bool /*overwrite*/, vector<boost::shared_ptr<Source> >& srcs,
5635                           InterThreadInfo& itt,
5636                           boost::shared_ptr<Processor> endpoint, bool include_endpoint,
5637                           bool for_export, bool for_freeze)
5638 {
5639         boost::shared_ptr<Region> result;
5640         boost::shared_ptr<Playlist> playlist;
5641         boost::shared_ptr<Source> source;
5642         ChanCount diskstream_channels (track.n_channels());
5643         samplepos_t position;
5644         samplecnt_t this_chunk;
5645         samplepos_t to_do;
5646         samplepos_t latency_skip;
5647         BufferSet buffers;
5648         samplepos_t len = end - start;
5649         bool need_block_size_reset = false;
5650         ChanCount const max_proc = track.max_processor_streams ();
5651         string legal_playlist_name;
5652         string possible_path;
5653
5654         DataType data_type = track.data_type();
5655
5656         if (end <= start) {
5657                 error << string_compose (_("Cannot write a range where end <= start (e.g. %1 <= %2)"),
5658                                          end, start) << endmsg;
5659                 return result;
5660         }
5661
5662         diskstream_channels = track.bounce_get_output_streams (diskstream_channels, endpoint,
5663                         include_endpoint, for_export, for_freeze);
5664
5665         if (data_type == DataType::MIDI && endpoint && !for_export && !for_freeze && diskstream_channels.n(DataType::AUDIO) > 0) {
5666                 data_type = DataType::AUDIO;
5667         }
5668
5669         if (diskstream_channels.n(data_type) < 1) {
5670                 error << _("Cannot write a range with no data.") << endmsg;
5671                 return result;
5672         }
5673
5674         // block all process callback handling
5675
5676         block_processing ();
5677
5678         {
5679                 // synchronize with AudioEngine::process_callback()
5680                 // make sure processing is not currently running
5681                 // and processing_blocked() is honored before
5682                 // acquiring thread buffers
5683                 Glib::Threads::Mutex::Lock lm (_engine.process_lock());
5684         }
5685
5686         _bounce_processing_active = true;
5687
5688         /* call tree *MUST* hold route_lock */
5689
5690         if ((playlist = track.playlist()) == 0) {
5691                 goto out;
5692         }
5693
5694         legal_playlist_name = "(bounce)" + legalize_for_path (playlist->name());
5695
5696         for (uint32_t chan_n = 0; chan_n < diskstream_channels.n(data_type); ++chan_n) {
5697
5698                 string base_name = string_compose ("%1-%2-bounce", playlist->name(), chan_n);
5699                 string path = ((data_type == DataType::AUDIO)
5700                                ? new_audio_source_path (legal_playlist_name, diskstream_channels.n_audio(), chan_n, false, true)
5701                                : new_midi_source_path (legal_playlist_name));
5702
5703                 if (path.empty()) {
5704                         goto out;
5705                 }
5706
5707                 try {
5708                         source = SourceFactory::createWritable (data_type, *this, path, false, sample_rate());
5709                 }
5710
5711                 catch (failed_constructor& err) {
5712                         error << string_compose (_("cannot create new file \"%1\" for %2"), path, track.name()) << endmsg;
5713                         goto out;
5714                 }
5715
5716                 srcs.push_back (source);
5717         }
5718
5719         /* tell redirects that care that we are about to use a much larger
5720          * blocksize. this will flush all plugins too, so that they are ready
5721          * to be used for this process.
5722          */
5723
5724         need_block_size_reset = true;
5725         track.set_block_size (bounce_chunk_size);
5726         _engine.main_thread()->get_buffers ();
5727
5728         position = start;
5729         to_do = len;
5730         latency_skip = track.bounce_get_latency (endpoint, include_endpoint, for_export, for_freeze);
5731
5732         /* create a set of reasonably-sized buffers */
5733         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
5734                 buffers.ensure_buffers(*t, max_proc.get(*t), bounce_chunk_size);
5735         }
5736         buffers.set_count (max_proc);
5737
5738         for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5739                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5740                 boost::shared_ptr<MidiSource> ms;
5741                 if (afs) {
5742                         afs->prepare_for_peakfile_writes ();
5743                 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5744                         Source::Lock lock(ms->mutex());
5745                         ms->mark_streaming_write_started(lock);
5746                 }
5747         }
5748
5749         while (to_do && !itt.cancel) {
5750
5751                 this_chunk = min (to_do, bounce_chunk_size);
5752
5753                 if (track.export_stuff (buffers, start, this_chunk, endpoint, include_endpoint, for_export, for_freeze)) {
5754                         goto out;
5755                 }
5756
5757                 start += this_chunk;
5758                 to_do -= this_chunk;
5759                 itt.progress = (float) (1.0 - ((double) to_do / len));
5760
5761                 if (latency_skip >= bounce_chunk_size) {
5762                         latency_skip -= bounce_chunk_size;
5763                         continue;
5764                 }
5765
5766                 const samplecnt_t current_chunk = this_chunk - latency_skip;
5767
5768                 uint32_t n = 0;
5769                 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
5770                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5771                         boost::shared_ptr<MidiSource> ms;
5772
5773                         if (afs) {
5774                                 if (afs->write (buffers.get_audio(n).data(latency_skip), current_chunk) != current_chunk) {
5775                                         goto out;
5776                                 }
5777                         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5778                                 Source::Lock lock(ms->mutex());
5779
5780                                 const MidiBuffer& buf = buffers.get_midi(0);
5781                                 for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
5782                                         Evoral::Event<samplepos_t> ev = *i;
5783                                         if (!endpoint || for_export) {
5784                                                 ev.set_time(ev.time() - position);
5785                                         }
5786                                         ms->append_event_samples(lock, ev, ms->natural_position());
5787                                 }
5788                         }
5789                 }
5790                 latency_skip = 0;
5791         }
5792
5793         /* post-roll, pick up delayed processor output */
5794         latency_skip = track.bounce_get_latency (endpoint, include_endpoint, for_export, for_freeze);
5795
5796         while (latency_skip && !itt.cancel) {
5797                 this_chunk = min (latency_skip, bounce_chunk_size);
5798                 latency_skip -= this_chunk;
5799
5800                 buffers.silence (this_chunk, 0);
5801                 track.bounce_process (buffers, start, this_chunk, endpoint, include_endpoint, for_export, for_freeze);
5802
5803                 uint32_t n = 0;
5804                 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
5805                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5806
5807                         if (afs) {
5808                                 if (afs->write (buffers.get_audio(n).data(), this_chunk) != this_chunk) {
5809                                         goto out;
5810                                 }
5811                         }
5812                 }
5813         }
5814
5815         if (!itt.cancel) {
5816
5817                 time_t now;
5818                 struct tm* xnow;
5819                 time (&now);
5820                 xnow = localtime (&now);
5821
5822                 for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src) {
5823                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5824                         boost::shared_ptr<MidiSource> ms;
5825
5826                         if (afs) {
5827                                 afs->update_header (position, *xnow, now);
5828                                 afs->flush_header ();
5829                         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
5830                                 Source::Lock lock(ms->mutex());
5831                                 ms->mark_streaming_write_completed(lock);
5832                         }
5833                 }
5834
5835                 /* construct a region to represent the bounced material */
5836
5837                 PropertyList plist;
5838
5839                 plist.add (Properties::start, 0);
5840                 plist.add (Properties::whole_file, true);
5841                 plist.add (Properties::length, srcs.front()->length(srcs.front()->natural_position()));
5842                 plist.add (Properties::name, region_name_from_path (srcs.front()->name(), true));
5843
5844                 result = RegionFactory::create (srcs, plist, true);
5845
5846         }
5847
5848         out:
5849         if (!result) {
5850                 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5851                         (*src)->mark_for_remove ();
5852                         (*src)->drop_references ();
5853                 }
5854
5855         } else {
5856                 for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
5857                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
5858
5859                         if (afs)
5860                                 afs->done_with_peakfile_writes ();
5861                 }
5862         }
5863
5864         _bounce_processing_active = false;
5865
5866         if (need_block_size_reset) {
5867                 _engine.main_thread()->drop_buffers ();
5868                 track.set_block_size (get_block_size());
5869         }
5870
5871         unblock_processing ();
5872
5873         return result;
5874 }
5875
5876 gain_t*
5877 Session::gain_automation_buffer() const
5878 {
5879         return ProcessThread::gain_automation_buffer ();
5880 }
5881
5882 gain_t*
5883 Session::trim_automation_buffer() const
5884 {
5885         return ProcessThread::trim_automation_buffer ();
5886 }
5887
5888 gain_t*
5889 Session::send_gain_automation_buffer() const
5890 {
5891         return ProcessThread::send_gain_automation_buffer ();
5892 }
5893
5894 gain_t*
5895 Session::scratch_automation_buffer() const
5896 {
5897         return ProcessThread::scratch_automation_buffer ();
5898 }
5899
5900 pan_t**
5901 Session::pan_automation_buffer() const
5902 {
5903         return ProcessThread::pan_automation_buffer ();
5904 }
5905
5906 BufferSet&
5907 Session::get_silent_buffers (ChanCount count)
5908 {
5909         return ProcessThread::get_silent_buffers (count);
5910 }
5911
5912 BufferSet&
5913 Session::get_scratch_buffers (ChanCount count, bool silence)
5914 {
5915         return ProcessThread::get_scratch_buffers (count, silence);
5916 }
5917
5918 BufferSet&
5919 Session::get_noinplace_buffers (ChanCount count)
5920 {
5921         return ProcessThread::get_noinplace_buffers (count);
5922 }
5923
5924 BufferSet&
5925 Session::get_route_buffers (ChanCount count, bool silence)
5926 {
5927         return ProcessThread::get_route_buffers (count, silence);
5928 }
5929
5930
5931 BufferSet&
5932 Session::get_mix_buffers (ChanCount count)
5933 {
5934         return ProcessThread::get_mix_buffers (count);
5935 }
5936
5937 uint32_t
5938 Session::ntracks () const
5939 {
5940         uint32_t n = 0;
5941         boost::shared_ptr<RouteList> r = routes.reader ();
5942
5943         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5944                 if (boost::dynamic_pointer_cast<Track> (*i)) {
5945                         ++n;
5946                 }
5947         }
5948
5949         return n;
5950 }
5951
5952 uint32_t
5953 Session::nbusses () const
5954 {
5955         uint32_t n = 0;
5956         boost::shared_ptr<RouteList> r = routes.reader ();
5957
5958         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
5959                 if (boost::dynamic_pointer_cast<Track>(*i) == 0) {
5960                         ++n;
5961                 }
5962         }
5963
5964         return n;
5965 }
5966
5967 uint32_t
5968 Session::nstripables (bool with_monitor) const
5969 {
5970         uint32_t rv = routes.reader()->size ();
5971         rv += _vca_manager->vcas ().size ();
5972
5973         if (with_monitor) {
5974                 return rv;
5975         }
5976
5977         if (_monitor_out) {
5978                 assert (rv > 0);
5979                 --rv;
5980         }
5981         return rv;
5982 }
5983
5984 bool
5985 Session::plot_process_graph (std::string const& file_name) const {
5986         return _process_graph ? _process_graph->plot (file_name) : false;
5987 }
5988
5989 void
5990 Session::add_automation_list(AutomationList *al)
5991 {
5992         automation_lists[al->id()] = al;
5993 }
5994
5995 /** @return true if there is at least one record-enabled track, otherwise false */
5996 bool
5997 Session::have_rec_enabled_track () const
5998 {
5999         return g_atomic_int_get (const_cast<gint*>(&_have_rec_enabled_track)) == 1;
6000 }
6001
6002 bool
6003 Session::have_rec_disabled_track () const
6004 {
6005         return g_atomic_int_get (const_cast<gint*>(&_have_rec_disabled_track)) == 1;
6006 }
6007
6008 /** Update the state of our rec-enabled tracks flag */
6009 void
6010 Session::update_route_record_state ()
6011 {
6012         boost::shared_ptr<RouteList> rl = routes.reader ();
6013         RouteList::iterator i = rl->begin();
6014         while (i != rl->end ()) {
6015
6016                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
6017                                     if (tr && tr->rec_enable_control()->get_value()) {
6018                         break;
6019                 }
6020
6021                 ++i;
6022         }
6023
6024         int const old = g_atomic_int_get (&_have_rec_enabled_track);
6025
6026         g_atomic_int_set (&_have_rec_enabled_track, i != rl->end () ? 1 : 0);
6027
6028         if (g_atomic_int_get (&_have_rec_enabled_track) != old) {
6029                 RecordStateChanged (); /* EMIT SIGNAL */
6030         }
6031
6032         for (i = rl->begin(); i != rl->end (); ++i) {
6033                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
6034                 if (tr && !tr->rec_enable_control()->get_value()) {
6035                         break;
6036                 }
6037         }
6038
6039         g_atomic_int_set (&_have_rec_disabled_track, i != rl->end () ? 1 : 0);
6040
6041         bool record_arm_state_changed = (old != g_atomic_int_get (&_have_rec_enabled_track) );
6042
6043         if (record_status() == Recording && record_arm_state_changed ) {
6044                 RecordArmStateChanged ();
6045         }
6046
6047 }
6048
6049 void
6050 Session::listen_position_changed ()
6051 {
6052         if (loading ()) {
6053                 /* skip duing session restore (already taken care of) */
6054                 return;
6055         }
6056         ProcessorChangeBlocker pcb (this);
6057         boost::shared_ptr<RouteList> r = routes.reader ();
6058         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6059                 (*i)->listen_position_changed ();
6060         }
6061 }
6062
6063 void
6064 Session::solo_control_mode_changed ()
6065 {
6066         if (soloing() || listening()) {
6067                 if (loading()) {
6068                         /* We can't use ::clear_all_solo_state() here because during
6069                            session loading at program startup, that will queue a call
6070                            to rt_clear_all_solo_state() that will not execute until
6071                            AFTER solo states have been established (thus throwing away
6072                            the session's saved solo state). So just explicitly turn
6073                            them all off.
6074                         */
6075                         set_controls (route_list_to_control_list (get_routes(), &Stripable::solo_control), 0.0, Controllable::NoGroup);
6076                 } else {
6077                         clear_all_solo_state (get_routes());
6078                 }
6079         }
6080 }
6081
6082 /** Called when a property of one of our route groups changes */
6083 void
6084 Session::route_group_property_changed (RouteGroup* rg)
6085 {
6086         RouteGroupPropertyChanged (rg); /* EMIT SIGNAL */
6087 }
6088
6089 /** Called when a route is added to one of our route groups */
6090 void
6091 Session::route_added_to_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
6092 {
6093         RouteAddedToRouteGroup (rg, r);
6094 }
6095
6096 /** Called when a route is removed from one of our route groups */
6097 void
6098 Session::route_removed_from_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
6099 {
6100         update_route_record_state ();
6101         RouteRemovedFromRouteGroup (rg, r); /* EMIT SIGNAL */
6102
6103         if (!rg->has_control_master () && !rg->has_subgroup () && rg->empty()) {
6104                 remove_route_group (*rg);
6105         }
6106 }
6107
6108 boost::shared_ptr<AudioTrack>
6109 Session::get_nth_audio_track (int nth) const
6110 {
6111         boost::shared_ptr<RouteList> rl = routes.reader ();
6112         rl->sort (Stripable::Sorter ());
6113
6114         for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
6115                 if (!boost::dynamic_pointer_cast<AudioTrack> (*r)) {
6116                         continue;
6117                 }
6118
6119                 if (--nth > 0) {
6120                         continue;
6121                 }
6122                 return boost::dynamic_pointer_cast<AudioTrack> (*r);
6123         }
6124         return boost::shared_ptr<AudioTrack> ();
6125 }
6126
6127 boost::shared_ptr<RouteList>
6128 Session::get_tracks () const
6129 {
6130         boost::shared_ptr<RouteList> rl = routes.reader ();
6131         boost::shared_ptr<RouteList> tl (new RouteList);
6132
6133         for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
6134                 if (boost::dynamic_pointer_cast<Track> (*r)) {
6135                         assert (!(*r)->is_auditioner()); // XXX remove me
6136                         tl->push_back (*r);
6137                 }
6138         }
6139         return tl;
6140 }
6141
6142 boost::shared_ptr<RouteList>
6143 Session::get_routes_with_regions_at (samplepos_t const p) const
6144 {
6145         boost::shared_ptr<RouteList> r = routes.reader ();
6146         boost::shared_ptr<RouteList> rl (new RouteList);
6147
6148         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6149                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
6150                 if (!tr) {
6151                         continue;
6152                 }
6153
6154                 boost::shared_ptr<Playlist> pl = tr->playlist ();
6155                 if (!pl) {
6156                         continue;
6157                 }
6158
6159                 if (pl->has_region_at (p)) {
6160                         rl->push_back (*i);
6161                 }
6162         }
6163
6164         return rl;
6165 }
6166
6167 void
6168 Session::goto_end ()
6169 {
6170         if (_session_range_location) {
6171                 request_locate (_session_range_location->end(), false);
6172         } else {
6173                 request_locate (0, false);
6174         }
6175 }
6176
6177 void
6178 Session::goto_start (bool and_roll)
6179 {
6180         if (_session_range_location) {
6181                 request_locate (_session_range_location->start(), and_roll);
6182         } else {
6183                 request_locate (0, and_roll);
6184         }
6185 }
6186
6187 samplepos_t
6188 Session::current_start_sample () const
6189 {
6190         return _session_range_location ? _session_range_location->start() : 0;
6191 }
6192
6193 samplepos_t
6194 Session::current_end_sample () const
6195 {
6196         return _session_range_location ? _session_range_location->end() : 0;
6197 }
6198
6199 void
6200 Session::set_session_range_location (samplepos_t start, samplepos_t end)
6201 {
6202         _session_range_location = new Location (*this, start, end, _("session"), Location::IsSessionRange, 0);
6203         _locations->add (_session_range_location);
6204 }
6205
6206 void
6207 Session::step_edit_status_change (bool yn)
6208 {
6209         bool send = false;
6210
6211         bool val = false;
6212         if (yn) {
6213                 send = (_step_editors == 0);
6214                 val = true;
6215
6216                 _step_editors++;
6217         } else {
6218                 send = (_step_editors == 1);
6219                 val = false;
6220
6221                 if (_step_editors > 0) {
6222                         _step_editors--;
6223                 }
6224         }
6225
6226         if (send) {
6227                 StepEditStatusChange (val);
6228         }
6229 }
6230
6231
6232 void
6233 Session::start_time_changed (samplepos_t old)
6234 {
6235         /* Update the auto loop range to match the session range
6236            (unless the auto loop range has been changed by the user)
6237         */
6238
6239         Location* s = _locations->session_range_location ();
6240         if (s == 0) {
6241                 return;
6242         }
6243
6244         Location* l = _locations->auto_loop_location ();
6245
6246         if (l && l->start() == old) {
6247                 l->set_start (s->start(), true);
6248         }
6249         set_dirty ();
6250 }
6251
6252 void
6253 Session::end_time_changed (samplepos_t old)
6254 {
6255         /* Update the auto loop range to match the session range
6256            (unless the auto loop range has been changed by the user)
6257         */
6258
6259         Location* s = _locations->session_range_location ();
6260         if (s == 0) {
6261                 return;
6262         }
6263
6264         Location* l = _locations->auto_loop_location ();
6265
6266         if (l && l->end() == old) {
6267                 l->set_end (s->end(), true);
6268         }
6269         set_dirty ();
6270 }
6271
6272 std::vector<std::string>
6273 Session::source_search_path (DataType type) const
6274 {
6275         Searchpath sp;
6276
6277         if (session_dirs.size() == 1) {
6278                 switch (type) {
6279                 case DataType::AUDIO:
6280                         sp.push_back (_session_dir->sound_path());
6281                         break;
6282                 case DataType::MIDI:
6283                         sp.push_back (_session_dir->midi_path());
6284                         break;
6285                 }
6286         } else {
6287                 for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
6288                         SessionDirectory sdir (i->path);
6289                         switch (type) {
6290                         case DataType::AUDIO:
6291                                 sp.push_back (sdir.sound_path());
6292                                 break;
6293                         case DataType::MIDI:
6294                                 sp.push_back (sdir.midi_path());
6295                                 break;
6296                         }
6297                 }
6298         }
6299
6300         if (type == DataType::AUDIO) {
6301                 const string sound_path_2X = _session_dir->sound_path_2X();
6302                 if (Glib::file_test (sound_path_2X, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
6303                         if (find (sp.begin(), sp.end(), sound_path_2X) == sp.end()) {
6304                                 sp.push_back (sound_path_2X);
6305                         }
6306                 }
6307         }
6308
6309         // now check the explicit (possibly user-specified) search path
6310
6311         switch (type) {
6312         case DataType::AUDIO:
6313                 sp += Searchpath(config.get_audio_search_path ());
6314                 break;
6315         case DataType::MIDI:
6316                 sp += Searchpath(config.get_midi_search_path ());
6317                 break;
6318         }
6319
6320         return sp;
6321 }
6322
6323 void
6324 Session::ensure_search_path_includes (const string& path, DataType type)
6325 {
6326         Searchpath sp;
6327
6328         if (path == ".") {
6329                 return;
6330         }
6331
6332         switch (type) {
6333         case DataType::AUDIO:
6334                 sp += Searchpath(config.get_audio_search_path ());
6335                 break;
6336         case DataType::MIDI:
6337                 sp += Searchpath (config.get_midi_search_path ());
6338                 break;
6339         }
6340
6341         for (vector<std::string>::iterator i = sp.begin(); i != sp.end(); ++i) {
6342                 /* No need to add this new directory if it has the same inode as
6343                    an existing one; checking inode rather than name prevents duplicated
6344                    directories when we are using symlinks.
6345
6346                    On Windows, I think we could just do if (*i == path) here.
6347                 */
6348                 if (PBD::equivalent_paths (*i, path)) {
6349                         return;
6350                 }
6351         }
6352
6353         sp += path;
6354
6355         switch (type) {
6356         case DataType::AUDIO:
6357                 config.set_audio_search_path (sp.to_string());
6358                 break;
6359         case DataType::MIDI:
6360                 config.set_midi_search_path (sp.to_string());
6361                 break;
6362         }
6363 }
6364
6365 void
6366 Session::remove_dir_from_search_path (const string& dir, DataType type)
6367 {
6368         Searchpath sp;
6369
6370         switch (type) {
6371         case DataType::AUDIO:
6372                 sp = Searchpath(config.get_audio_search_path ());
6373                 break;
6374         case DataType::MIDI:
6375                 sp = Searchpath (config.get_midi_search_path ());
6376                 break;
6377         }
6378
6379         sp -= dir;
6380
6381         switch (type) {
6382         case DataType::AUDIO:
6383                 config.set_audio_search_path (sp.to_string());
6384                 break;
6385         case DataType::MIDI:
6386                 config.set_midi_search_path (sp.to_string());
6387                 break;
6388         }
6389
6390 }
6391
6392 boost::shared_ptr<Speakers>
6393 Session::get_speakers()
6394 {
6395         return _speakers;
6396 }
6397
6398 list<string>
6399 Session::unknown_processors () const
6400 {
6401         list<string> p;
6402
6403         boost::shared_ptr<RouteList> r = routes.reader ();
6404         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6405                 list<string> t = (*i)->unknown_processors ();
6406                 copy (t.begin(), t.end(), back_inserter (p));
6407         }
6408
6409         p.sort ();
6410         p.unique ();
6411
6412         return p;
6413 }
6414
6415 void
6416 Session::set_worst_io_latencies_x (IOChange, void *)
6417 {
6418                 set_worst_io_latencies ();
6419 }
6420
6421 void
6422 Session::send_latency_compensation_change ()
6423 {
6424         /* As a result of Send::set_output_latency()
6425          * or InternalReturn::set_playback_offset ()
6426          * the send's own latency can change (source track
6427          * is aligned with target bus).
6428          *
6429          * This can only happen be triggered by
6430          * Route::update_signal_latency ()
6431          * when updating the processor latency.
6432          *
6433          * We need to walk the graph again to take those changes into account
6434          * (we should probably recurse or process the graph in a 2 step process).
6435          */
6436         ++_send_latency_changes;
6437 }
6438
6439 bool
6440 Session::update_route_latency (bool playback, bool apply_to_delayline)
6441 {
6442         /* Note: RouteList is process-graph sorted */
6443         boost::shared_ptr<RouteList> r = routes.reader ();
6444
6445         if (playback) {
6446                 /* reverse the list so that we work backwards from the last route to run to the first,
6447                  * this is not needed, but can help to reduce the iterations for aux-sends.
6448                  */
6449                 RouteList* rl = routes.reader().get();
6450                 r.reset (new RouteList (*rl));
6451                 reverse (r->begin(), r->end());
6452         }
6453
6454         bool changed = false;
6455         int bailout = 0;
6456 restart:
6457         _send_latency_changes = 0;
6458         _worst_route_latency = 0;
6459
6460         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6461                 // if (!(*i)->active()) { continue ; } // TODO
6462                 samplecnt_t l;
6463                 if ((*i)->signal_latency () != (l = (*i)->update_signal_latency (apply_to_delayline))) {
6464                         changed = true;
6465                 }
6466                 _worst_route_latency = std::max (l, _worst_route_latency);
6467         }
6468
6469         if (_send_latency_changes > 0) {
6470                 // only 1 extra iteration is needed (we allow only 1 level of aux-sends)
6471                 // BUT..  jack'n'sends'n'bugs
6472                 if (++bailout < 5) {
6473                         cerr << "restarting Session::update_latency. # of send changes: " << _send_latency_changes << " iteration: " << bailout << endl;
6474                         goto restart;
6475                 }
6476         }
6477
6478         DEBUG_TRACE (DEBUG::Latency, string_compose ("worst signal processing latency: %1 (changed ? %2)\n", _worst_route_latency, (changed ? "yes" : "no")));
6479
6480         return changed;
6481 }
6482
6483 void
6484 Session::update_latency (bool playback)
6485 {
6486         DEBUG_TRACE (DEBUG::Latency, string_compose ("JACK latency callback: %1\n", (playback ? "PLAYBACK" : "CAPTURE")));
6487
6488         if (inital_connect_or_deletion_in_progress () || _adding_routes_in_progress || _route_deletion_in_progress) {
6489                 return;
6490         }
6491         if (!_engine.running()) {
6492                 return;
6493         }
6494
6495         /* Note; RouteList is sorted as process-graph */
6496         boost::shared_ptr<RouteList> r = routes.reader ();
6497
6498         if (playback) {
6499                 /* reverse the list so that we work backwards from the last route to run to the first */
6500                 RouteList* rl = routes.reader().get();
6501                 r.reset (new RouteList (*rl));
6502                 reverse (r->begin(), r->end());
6503         }
6504
6505         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6506                 samplecnt_t latency = (*i)->set_private_port_latencies (playback);
6507                 (*i)->set_public_port_latencies (latency, playback);
6508         }
6509
6510         if (playback) {
6511                 set_worst_output_latency ();
6512                 update_route_latency (true, true);
6513         } else {
6514                 set_worst_input_latency ();
6515                 update_route_latency (false, false);
6516         }
6517
6518         DEBUG_TRACE (DEBUG::Latency, "JACK latency callback: DONE\n");
6519 }
6520
6521 void
6522 Session::initialize_latencies ()
6523 {
6524         {
6525                 Glib::Threads::Mutex::Lock lm (_engine.process_lock());
6526                 update_latency (false);
6527                 update_latency (true);
6528         }
6529
6530         set_worst_io_latencies ();
6531 }
6532
6533 void
6534 Session::set_worst_io_latencies ()
6535 {
6536         set_worst_output_latency ();
6537         set_worst_input_latency ();
6538 }
6539
6540 void
6541 Session::set_worst_output_latency ()
6542 {
6543         if (inital_connect_or_deletion_in_progress ()) {
6544                 return;
6545         }
6546
6547         _worst_output_latency = 0;
6548
6549         if (!_engine.running()) {
6550                 return;
6551         }
6552
6553         boost::shared_ptr<RouteList> r = routes.reader ();
6554
6555         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6556                 _worst_output_latency = max (_worst_output_latency, (*i)->output()->latency());
6557         }
6558
6559         _worst_output_latency = max (_worst_output_latency, _click_io->latency());
6560
6561         DEBUG_TRACE (DEBUG::Latency, string_compose ("Worst output latency: %1\n", _worst_output_latency));
6562 }
6563
6564 void
6565 Session::set_worst_input_latency ()
6566 {
6567         if (inital_connect_or_deletion_in_progress ()) {
6568                 return;
6569         }
6570
6571         _worst_input_latency = 0;
6572
6573         if (!_engine.running()) {
6574                 return;
6575         }
6576
6577         boost::shared_ptr<RouteList> r = routes.reader ();
6578
6579         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6580                 _worst_input_latency = max (_worst_input_latency, (*i)->input()->latency());
6581         }
6582
6583         DEBUG_TRACE (DEBUG::Latency, string_compose ("Worst input latency: %1\n", _worst_input_latency));
6584 }
6585
6586 void
6587 Session::update_latency_compensation (bool force_whole_graph)
6588 {
6589         if (inital_connect_or_deletion_in_progress ()) {
6590                 return;
6591         }
6592         /* this lock is not usually contended, but under certain conditions,
6593          * update_latency_compensation may be called concurrently.
6594          * e.g. drag/drop copy a latent plugin while rolling.
6595          * GUI thread (via route_processors_changed) and
6596          * auto_connect_thread_run may race.
6597          */
6598         Glib::Threads::Mutex::Lock lx (_update_latency_lock, Glib::Threads::TRY_LOCK);
6599         if (!lx.locked()) {
6600                 /* no need to do this twice */
6601                 return;
6602         }
6603
6604         bool some_track_latency_changed = update_route_latency (false, false);
6605
6606         if (some_track_latency_changed || force_whole_graph)  {
6607                 _engine.update_latencies ();
6608                 /* above call will ask the backend up update its latencies, which
6609                  * eventually will trigger  AudioEngine::latency_callback () and
6610                  * call Session::update_latency ()
6611                  */
6612         } else {
6613                 boost::shared_ptr<RouteList> r = routes.reader ();
6614                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
6615                         (*i)->apply_latency_compensation ();
6616                 }
6617         }
6618 }
6619
6620 char
6621 Session::session_name_is_legal (const string& path)
6622 {
6623         char illegal_chars[] = { '/', '\\', ':', ';', '\0' };
6624
6625         for (int i = 0; illegal_chars[i]; ++i) {
6626                 if (path.find (illegal_chars[i]) != string::npos) {
6627                         return illegal_chars[i];
6628                 }
6629         }
6630
6631         return 0;
6632 }
6633
6634 void
6635 Session::notify_presentation_info_change ()
6636 {
6637         if (deletion_in_progress()) {
6638                 return;
6639         }
6640
6641         reassign_track_numbers();
6642 }
6643
6644 bool
6645 Session::operation_in_progress (GQuark op) const
6646 {
6647         return (find (_current_trans_quarks.begin(), _current_trans_quarks.end(), op) != _current_trans_quarks.end());
6648 }
6649
6650 boost::shared_ptr<Port>
6651 Session::ltc_output_port () const
6652 {
6653         return _ltc_output ? _ltc_output->nth (0) : boost::shared_ptr<Port> ();
6654 }
6655
6656 void
6657 Session::reconnect_ltc_output ()
6658 {
6659         if (_ltc_output) {
6660
6661                 string src = Config->get_ltc_output_port();
6662
6663                 _ltc_output->disconnect (this);
6664
6665                 if (src != _("None") && !src.empty())  {
6666                         _ltc_output->nth (0)->connect (src);
6667                 }
6668         }
6669 }
6670
6671 void
6672 Session::set_range_selection (samplepos_t start, samplepos_t end)
6673 {
6674         _range_selection = Evoral::Range<samplepos_t> (start, end);
6675 }
6676
6677 void
6678 Session::set_object_selection (samplepos_t start, samplepos_t end)
6679 {
6680         _object_selection = Evoral::Range<samplepos_t> (start, end);
6681 }
6682
6683 void
6684 Session::clear_range_selection ()
6685 {
6686         _range_selection = Evoral::Range<samplepos_t> (-1,-1);
6687 }
6688
6689 void
6690 Session::clear_object_selection ()
6691 {
6692         _object_selection = Evoral::Range<samplepos_t> (-1,-1);
6693 }
6694
6695 void
6696 Session::auto_connect_route (boost::shared_ptr<Route> route, bool connect_inputs,
6697                 const ChanCount& input_start,
6698                 const ChanCount& output_start,
6699                 const ChanCount& input_offset,
6700                 const ChanCount& output_offset)
6701 {
6702         Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6703         _auto_connect_queue.push (AutoConnectRequest (route, connect_inputs,
6704                                 input_start, output_start,
6705                                 input_offset, output_offset));
6706
6707         auto_connect_thread_wakeup ();
6708 }
6709
6710 void
6711 Session::auto_connect_thread_wakeup ()
6712 {
6713         if (pthread_mutex_trylock (&_auto_connect_mutex) == 0) {
6714                 pthread_cond_signal (&_auto_connect_cond);
6715                 pthread_mutex_unlock (&_auto_connect_mutex);
6716         }
6717 }
6718
6719 void
6720 Session::queue_latency_recompute ()
6721 {
6722         g_atomic_int_inc (&_latency_recompute_pending);
6723         auto_connect_thread_wakeup ();
6724 }
6725
6726 void
6727 Session::auto_connect (const AutoConnectRequest& ar)
6728 {
6729         boost::shared_ptr<Route> route = ar.route.lock();
6730
6731         if (!route) { return; }
6732
6733         if (!IO::connecting_legal) {
6734                 return;
6735         }
6736
6737         /* If both inputs and outputs are auto-connected to physical ports,
6738          * use the max of input and output offsets to ensure auto-connected
6739          * port numbers always match up (e.g. the first audio input and the
6740          * first audio output of the route will have the same physical
6741          * port number).  Otherwise just use the lowest input or output
6742          * offset possible.
6743          */
6744
6745         const bool in_out_physical =
6746                 (Config->get_input_auto_connect() & AutoConnectPhysical)
6747                 && (Config->get_output_auto_connect() & AutoConnectPhysical)
6748                 && ar.connect_inputs;
6749
6750         const ChanCount in_offset = in_out_physical
6751                 ? ChanCount::max(ar.input_offset, ar.output_offset)
6752                 : ar.input_offset;
6753
6754         const ChanCount out_offset = in_out_physical
6755                 ? ChanCount::max(ar.input_offset, ar.output_offset)
6756                 : ar.output_offset;
6757
6758         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
6759                 vector<string> physinputs;
6760                 vector<string> physoutputs;
6761
6762
6763                 /* for connecting track inputs we only want MIDI ports marked
6764                  * for "music".
6765                  */
6766
6767                 get_physical_ports (physinputs, physoutputs, *t, MidiPortMusic);
6768
6769                 if (!physinputs.empty() && ar.connect_inputs) {
6770                         uint32_t nphysical_in = physinputs.size();
6771
6772                         for (uint32_t i = ar.input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) {
6773                                 string port;
6774
6775                                 if (Config->get_input_auto_connect() & AutoConnectPhysical) {
6776                                         port = physinputs[(in_offset.get(*t) + i) % nphysical_in];
6777                                 }
6778
6779                                 if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) {
6780                                         break;
6781                                 }
6782                         }
6783                 }
6784
6785                 if (!physoutputs.empty()) {
6786                         uint32_t nphysical_out = physoutputs.size();
6787                         for (uint32_t i = ar.output_start.get(*t); i < route->n_outputs().get(*t); ++i) {
6788                                 string port;
6789
6790                                 /* Waves Tracks:
6791                                  * do not create new connections if we reached the limit of physical outputs
6792                                  * in Multi Out mode
6793                                  */
6794                                 if (!(Config->get_output_auto_connect() & AutoConnectMaster) &&
6795                                                 ARDOUR::Profile->get_trx () &&
6796                                                 ar.output_offset.get(*t) == nphysical_out ) {
6797                                         break;
6798                                 }
6799
6800                                 if ((*t) == DataType::MIDI && (Config->get_output_auto_connect() & AutoConnectPhysical)) {
6801                                         port = physoutputs[(out_offset.get(*t) + i) % nphysical_out];
6802                                 } else if ((*t) == DataType::AUDIO && (Config->get_output_auto_connect() & AutoConnectMaster)) {
6803                                         /* master bus is audio only */
6804                                         if (_master_out && _master_out->n_inputs().get(*t) > 0) {
6805                                                 port = _master_out->input()->ports().port(*t,
6806                                                                 i % _master_out->input()->n_ports().get(*t))->name();
6807                                         }
6808                                 }
6809
6810                                 if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) {
6811                                         break;
6812                                 }
6813                         }
6814                 }
6815         }
6816 }
6817
6818 void
6819 Session::auto_connect_thread_start ()
6820 {
6821         if (g_atomic_int_get (&_ac_thread_active)) {
6822                 return;
6823         }
6824
6825         while (!_auto_connect_queue.empty ()) {
6826                 _auto_connect_queue.pop ();
6827         }
6828
6829         g_atomic_int_set (&_ac_thread_active, 1);
6830         if (pthread_create (&_auto_connect_thread, NULL, auto_connect_thread, this)) {
6831                 g_atomic_int_set (&_ac_thread_active, 0);
6832         }
6833 }
6834
6835 void
6836 Session::auto_connect_thread_terminate ()
6837 {
6838         if (!g_atomic_int_get (&_ac_thread_active)) {
6839                 return;
6840         }
6841
6842         {
6843                 Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6844                 while (!_auto_connect_queue.empty ()) {
6845                         _auto_connect_queue.pop ();
6846                 }
6847         }
6848
6849         /* cannot use auto_connect_thread_wakeup() because that is allowed to
6850          * fail to wakeup the thread.
6851          */
6852
6853         pthread_mutex_lock (&_auto_connect_mutex);
6854         g_atomic_int_set (&_ac_thread_active, 0);
6855         pthread_cond_signal (&_auto_connect_cond);
6856         pthread_mutex_unlock (&_auto_connect_mutex);
6857
6858         void *status;
6859         pthread_join (_auto_connect_thread, &status);
6860 }
6861
6862 void *
6863 Session::auto_connect_thread (void *arg)
6864 {
6865         Session *s = static_cast<Session *>(arg);
6866         s->auto_connect_thread_run ();
6867         pthread_exit (0);
6868         return 0;
6869 }
6870
6871 void
6872 Session::auto_connect_thread_run ()
6873 {
6874         pthread_set_name (X_("autoconnect"));
6875         SessionEvent::create_per_thread_pool (X_("autoconnect"), 1024);
6876         PBD::notify_event_loops_about_thread_creation (pthread_self(), X_("autoconnect"), 1024);
6877         pthread_mutex_lock (&_auto_connect_mutex);
6878         while (g_atomic_int_get (&_ac_thread_active)) {
6879
6880                 if (!_auto_connect_queue.empty ()) {
6881                         // Why would we need the process lock ??
6882                         // A: if ports are added while we're connecting, the backend's iterator may be invalidated:
6883                         //   graph_order_callback() -> resort_routes() -> direct_feeds_according_to_reality () -> backend::connected_to()
6884                         //   All ardour-internal backends use a std::vector   xxxAudioBackend::find_port()
6885                         //   We have control over those, but what does jack do?
6886                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6887
6888                         Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock);
6889                         while (!_auto_connect_queue.empty ()) {
6890                                 const AutoConnectRequest ar (_auto_connect_queue.front());
6891                                 _auto_connect_queue.pop ();
6892                                 lx.release ();
6893                                 auto_connect (ar);
6894                                 lx.acquire ();
6895                         }
6896                 }
6897
6898                 if (!actively_recording ()) { // might not be needed,
6899                         /* this is only used for updating plugin latencies, the
6900                          * graph does not change. so it's safe in general.
6901                          * BUT..
6902                          * update_latency_compensation ()
6903                          * calls DiskWriter::set_capture_offset () which
6904                          * modifies the capture-offset, which can be a problem.
6905                          */
6906                         while (g_atomic_int_and (&_latency_recompute_pending, 0)) {
6907                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6908                                 update_latency_compensation ();
6909                         }
6910                 }
6911
6912                 {
6913                         // this may call ARDOUR::Port::drop ... jack_port_unregister ()
6914                         // jack1 cannot cope with removing ports while processing
6915                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
6916                         AudioEngine::instance()->clear_pending_port_deletions ();
6917                 }
6918
6919                 pthread_cond_wait (&_auto_connect_cond, &_auto_connect_mutex);
6920         }
6921         pthread_mutex_unlock (&_auto_connect_mutex);
6922 }
6923
6924 void
6925 Session::cancel_all_solo ()
6926 {
6927         StripableList sl;
6928
6929         get_stripables (sl);
6930
6931         set_controls (stripable_list_to_control_list (sl, &Stripable::solo_control), 0.0, Controllable::NoGroup);
6932         clear_all_solo_state (routes.reader());
6933 }
6934
6935 void
6936 Session::maybe_update_tempo_from_midiclock_tempo (float bpm)
6937 {
6938         if (_tempo_map->n_tempos() == 1) {
6939                 TempoSection& ts (_tempo_map->tempo_section_at_sample (0));
6940                 if (fabs (ts.note_types_per_minute() - bpm) > (0.01 * ts.note_types_per_minute())) {
6941                         const Tempo tempo (bpm, 4.0, bpm);
6942                         std::cerr << "new tempo " << bpm << " old " << ts.note_types_per_minute() << std::endl;
6943                         _tempo_map->replace_tempo (ts, tempo, 0.0, 0.0, AudioTime);
6944                 }
6945         }
6946 }