X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fsession.cc;h=68c7d3957fdc8f99c5487a57031a4c505b29fb4a;hb=d01cb7910faddbf13b7190ceca990a2cafb71f95;hp=b6cc258fa76a6297576c013c5e5d5f6c9a913942;hpb=f719dcbd4311fa7e57b3905d4ecd61ce48c572f0;p=ardour.git diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index b6cc258fa7..68c7d3957f 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -42,6 +42,7 @@ #include "pbd/error.h" #include "pbd/file_utils.h" #include "pbd/md5.h" +#include "pbd/pthread_utils.h" #include "pbd/search_path.h" #include "pbd/stacktrace.h" #include "pbd/stl_delete.h" @@ -100,6 +101,7 @@ #include "ardour/source_factory.h" #include "ardour/speakers.h" #include "ardour/tempo.h" +#include "ardour/ticker.h" #include "ardour/track.h" #include "ardour/user_bundle.h" #include "ardour/utils.h" @@ -243,6 +245,7 @@ Session::Session (AudioEngine &eng, , _ignore_skips_updates (false) , _rt_thread_active (false) , _rt_emit_pending (false) + , _ac_thread_active (false) , step_speed (0) , outbound_mtc_timecode_frame (0) , next_quarter_frame_to_send (-1) @@ -304,6 +307,7 @@ Session::Session (AudioEngine &eng, , _speakers (new Speakers) , _order_hint (-1) , ignore_route_processor_changes (false) + , midi_clock (0) , _scene_changer (0) , _midi_ports (0) , _mmc (0) @@ -313,6 +317,9 @@ Session::Session (AudioEngine &eng, pthread_mutex_init (&_rt_emit_mutex, 0); pthread_cond_init (&_rt_emit_cond, 0); + pthread_mutex_init (&_auto_connect_mutex, 0); + pthread_cond_init (&_auto_connect_cond, 0); + init_name_id_counter (1); // reset for new sessions, start at 1 pre_engine_init (fullpath); @@ -326,7 +333,7 @@ Session::Session (AudioEngine &eng, #ifdef USE_TRACKS_CODE_FEATURES sr = EngineStateController::instance()->get_current_sample_rate(); #endif - if (ensure_engine (sr)) { + if (ensure_engine (sr, true)) { destroy (); throw SessionException (_("Cannot connect to audio/midi engine")); } @@ -380,7 +387,7 @@ Session::Session (AudioEngine &eng, } } - if (ensure_engine (sr)) { + if (ensure_engine (sr, false)) { destroy (); throw SessionException (_("Cannot connect to audio/midi engine")); } @@ -408,6 +415,7 @@ Session::Session (AudioEngine &eng, EndTimeChanged.connect_same_thread (*this, boost::bind (&Session::end_time_changed, this, _1)); emit_thread_start (); + auto_connect_thread_start (); /* hook us up to the engine since we are now completely constructed */ @@ -492,7 +500,7 @@ Session::init_name_id_counter (guint n) } int -Session::ensure_engine (uint32_t desired_sample_rate) +Session::ensure_engine (uint32_t desired_sample_rate, bool isnew) { if (_engine.current_backend() == 0) { /* backend is unknown ... */ @@ -500,6 +508,8 @@ Session::ensure_engine (uint32_t desired_sample_rate) if (r.get_value_or (-1) != 0) { return -1; } + } else if (!isnew && _engine.running() && _engine.sample_rate () == desired_sample_rate) { + /* keep engine */ } else if (_engine.setup_required()) { /* backend is known, but setup is needed */ boost::optional r = AudioEngineSetupRequired (desired_sample_rate); @@ -512,8 +522,7 @@ Session::ensure_engine (uint32_t desired_sample_rate) } } - /* at this point the engine should be running - */ + /* at this point the engine should be running */ if (!_engine.running()) { return -1; @@ -585,8 +594,12 @@ Session::destroy () _state_of_the_state = StateOfTheState (CannotSave|Deletion); + /* stop autoconnecting */ + auto_connect_thread_terminate (); + /* disconnect from any and all signals that we are connected to */ + Port::PortSignalDrop (); /* EMIT SIGNAL */ drop_connections (); /* shutdown control surface protocols while we still have ports @@ -719,6 +732,9 @@ Session::destroy () pthread_cond_destroy (&_rt_emit_cond); pthread_mutex_destroy (&_rt_emit_mutex); + pthread_cond_destroy (&_auto_connect_cond); + pthread_mutex_destroy (&_auto_connect_mutex); + delete _scene_changer; _scene_changer = 0; delete midi_control_ui; midi_control_ui = 0; @@ -726,8 +742,45 @@ Session::destroy () delete _midi_ports; _midi_ports = 0; delete _locations; _locations = 0; + delete midi_clock; delete _tempo_map; + /* clear event queue, the session is gone, nobody is interested in + * those anymore, but they do leak memory if not removed + */ + while (!immediate_events.empty ()) { + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + SessionEvent *ev = immediate_events.front (); + DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Drop event: %1\n", enum_2_string (ev->type))); + immediate_events.pop_front (); + bool remove = true; + bool del = true; + switch (ev->type) { + case SessionEvent::AutoLoop: + case SessionEvent::AutoLoopDeclick: + case SessionEvent::Skip: + case SessionEvent::PunchIn: + case SessionEvent::PunchOut: + case SessionEvent::StopOnce: + case SessionEvent::RangeStop: + case SessionEvent::RangeLocate: + remove = false; + del = false; + break; + case SessionEvent::RealTimeOperation: + process_rtop (ev); + del = false; + default: + break; + } + if (remove) { + del = del && !_remove_event (ev); + } + if (del) { + delete ev; + } + } + DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n"); #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS @@ -1064,11 +1117,16 @@ Session::remove_monitor_section () } remove_route (_monitor_out); + if (_state_of_the_state & Deletion) { + return; + } + auto_connect_master_bus (); if (auditioner) { auditioner->connect (); } + Config->ParameterChanged ("use-monitor-bus"); } @@ -2153,16 +2211,18 @@ Session::resort_routes () } #ifndef NDEBUG - boost::shared_ptr rl = routes.reader (); - for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) { - DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name())); + if (DEBUG_ENABLED(DEBUG::Graph)) { + boost::shared_ptr rl = routes.reader (); + for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) { + DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name())); - const Route::FedBy& fb ((*i)->fed_by()); + const Route::FedBy& fb ((*i)->fed_by()); - for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) { - boost::shared_ptr sf = f->r.lock(); - if (sf) { - DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only)); + for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) { + boost::shared_ptr sf = f->r.lock(); + if (sf) { + DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only)); + } } } } @@ -2559,151 +2619,26 @@ Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name void Session::midi_output_change_handler (IOChange change, void * /*src*/, boost::weak_ptr wmt) { - boost::shared_ptr midi_track (wmt.lock()); - - if (!midi_track) { - return; - } - - if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) { - - if (change.after.n_audio() <= change.before.n_audio()) { - return; - } - - /* new audio ports: make sure the audio goes somewhere useful, - unless the user has no-auto-connect selected. - - The existing ChanCounts don't matter for this call as they are only - to do with matching input and output indices, and we are only changing - outputs here. - */ - - ChanCount dummy; - - auto_connect_route (midi_track, dummy, dummy, false, false, ChanCount(), change.before); - } -} + boost::shared_ptr midi_track (wmt.lock()); -/** @param connect_inputs true to connect inputs as well as outputs, false to connect just outputs. - * @param input_start Where to start from when auto-connecting inputs; e.g. if this is 0, auto-connect starting from input 0. - * @param output_start As \a input_start, but for outputs. - */ -void -Session::auto_connect_route (boost::shared_ptr route, ChanCount& existing_inputs, ChanCount& existing_outputs, - bool with_lock, bool connect_inputs, ChanCount input_start, ChanCount output_start) -{ - if (!IO::connecting_legal) { + if (!midi_track) { return; } - Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK); - - if (with_lock) { - lm.acquire (); - } - - /* If both inputs and outputs are auto-connected to physical ports, - use the max of input and output offsets to ensure auto-connected - port numbers always match up (e.g. the first audio input and the - first audio output of the route will have the same physical - port number). Otherwise just use the lowest input or output - offset possible. - */ - - DEBUG_TRACE (DEBUG::Graph, - string_compose("Auto-connect: existing in = %1 out = %2\n", - existing_inputs, existing_outputs)); - - const bool in_out_physical = - (Config->get_input_auto_connect() & AutoConnectPhysical) - && (Config->get_output_auto_connect() & AutoConnectPhysical) - && connect_inputs; - - const ChanCount in_offset = in_out_physical - ? ChanCount::max(existing_inputs, existing_outputs) - : existing_inputs; - - const ChanCount out_offset = in_out_physical - ? ChanCount::max(existing_inputs, existing_outputs) - : existing_outputs; - - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - vector physinputs; - vector physoutputs; - - _engine.get_physical_outputs (*t, physoutputs); - _engine.get_physical_inputs (*t, physinputs); - - if (!physinputs.empty() && connect_inputs) { - uint32_t nphysical_in = physinputs.size(); - - DEBUG_TRACE (DEBUG::Graph, - string_compose("There are %1 physical inputs of type %2\n", - nphysical_in, *t)); - - for (uint32_t i = input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) { - string port; - - if (Config->get_input_auto_connect() & AutoConnectPhysical) { - DEBUG_TRACE (DEBUG::Graph, - string_compose("Get index %1 + %2 % %3 = %4\n", - in_offset.get(*t), i, nphysical_in, - (in_offset.get(*t) + i) % nphysical_in)); - port = physinputs[(in_offset.get(*t) + i) % nphysical_in]; - } - - DEBUG_TRACE (DEBUG::Graph, - string_compose("Connect route %1 IN to %2\n", - route->name(), port)); - - if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) { - break; - } + if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) { - ChanCount one_added (*t, 1); - existing_inputs += one_added; - } + if (change.after.n_audio() <= change.before.n_audio()) { + return; } - if (!physoutputs.empty()) { - uint32_t nphysical_out = physoutputs.size(); - for (uint32_t i = output_start.get(*t); i < route->n_outputs().get(*t); ++i) { - string port; - - /* Waves Tracks: - * do not create new connections if we reached the limit of physical outputs - * in Multi Out mode - */ - - if (!(Config->get_output_auto_connect() & AutoConnectMaster) && - ARDOUR::Profile->get_trx () && - existing_outputs.get(*t) == nphysical_out ) { - break; - } - - if ((*t) == DataType::MIDI && (Config->get_output_auto_connect() & AutoConnectPhysical)) { - port = physoutputs[(out_offset.get(*t) + i) % nphysical_out]; - } else if ((*t) == DataType::AUDIO && (Config->get_output_auto_connect() & AutoConnectMaster)) { - /* master bus is audio only */ - if (_master_out && _master_out->n_inputs().get(*t) > 0) { - port = _master_out->input()->ports().port(*t, - i % _master_out->input()->n_ports().get(*t))->name(); - } - } - - DEBUG_TRACE (DEBUG::Graph, - string_compose("Connect route %1 OUT to %2\n", - route->name(), port)); - - if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) { - break; - } - - ChanCount one_added (*t, 1); - existing_outputs += one_added; - } - } + /* new audio ports: make sure the audio goes somewhere useful, + * unless the user has no-auto-connect selected. + * + * The existing ChanCounts don't matter for this call as they are only + * to do with matching input and output indices, and we are only changing + * outputs here. + */ + auto_connect_route (midi_track, false, ChanCount(), change.before); } } @@ -2865,8 +2800,6 @@ Session::reconnect_existing_routes (bool withLock, bool reconnect_master, bool r } } } - - //auto_connect_route (*rIter, inputs, outputs, false, reconnectIputs); } _master_out->output()->disconnect (this); @@ -3287,10 +3220,48 @@ Session::new_route_from_template (uint32_t how_many, XMLNode& node, const std::s XMLNodeList children = node_copy.children (); for (XMLNodeList::iterator i = children.begin(); i != children.end(); ++i) { if ((*i)->name() == X_("Processor")) { + /* ForceIDRegeneration does not catch the following */ XMLProperty* role = (*i)->property (X_("role")); + XMLProperty* type = (*i)->property (X_("type")); + if (role && role->value() == X_("Aux")) { + /* check if the target bus exists. + * we should not save aux-sends in templates. + */ + XMLProperty* target = (*i)->property (X_("target")); + if (!target) { + (*i)->add_property ("type", "dangling-aux-send"); + continue; + } + boost::shared_ptr r = route_by_id (target->value()); + if (!r || boost::dynamic_pointer_cast(r)) { + (*i)->add_property ("type", "dangling-aux-send"); + continue; + } + } if (role && role->value() == X_("Listen")) { (*i)->remove_property (X_("bitslot")); } + else if (role && (role->value() == X_("Send") || role->value() == X_("Aux"))) { + char buf[32]; + Delivery::Role xrole; + uint32_t bitslot = 0; + xrole = Delivery::Role (string_2_enum (role->value(), xrole)); + std::string name = Send::name_and_id_new_send(*this, xrole, bitslot, false); + snprintf (buf, sizeof (buf), "%" PRIu32, bitslot); + (*i)->remove_property (X_("bitslot")); + (*i)->remove_property (X_("name")); + (*i)->add_property ("bitslot", buf); + (*i)->add_property ("name", name); + } + else if (type && type->value() == X_("return")) { + // Return::set_state() generates a new one + (*i)->remove_property (X_("bitslot")); + } + else if (type && type->value() == X_("port")) { + // PortInsert::set_state() handles the bitslot + (*i)->remove_property (X_("bitslot")); + (*i)->add_property ("ignore-name", "1"); + } } } @@ -3398,16 +3369,17 @@ Session::add_routes (RouteList& new_routes, bool input_auto_connect, bool output void Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect) { - ChanCount existing_inputs; - ChanCount existing_outputs; + ChanCount existing_inputs; + ChanCount existing_outputs; uint32_t order = next_control_id(); + if (_order_hint > -1) { order = _order_hint; _order_hint = -1; } - count_existing_track_channels (existing_inputs, existing_outputs); + count_existing_track_channels (existing_inputs, existing_outputs); { RCUWriter writer (routes); @@ -3415,10 +3387,10 @@ Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool r->insert (r->end(), new_routes.begin(), new_routes.end()); /* if there is no control out and we're not in the middle of loading, - resort the graph here. if there is a control out, we will resort - toward the end of this method. if we are in the middle of loading, - we will resort when done. - */ + * resort the graph here. if there is a control out, we will resort + * toward the end of this method. if we are in the middle of loading, + * we will resort when done. + */ if (!_monitor_out && IO::connecting_legal) { resort_routes_using (r); @@ -3454,19 +3426,20 @@ Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool boost::shared_ptr mt = boost::dynamic_pointer_cast (tr); if (mt) { mt->StepEditStatusChange.connect_same_thread (*this, boost::bind (&Session::step_edit_status_change, this, _1)); - mt->output()->changed.connect_same_thread (*this, boost::bind (&Session::midi_output_change_handler, this, _1, _2, boost::weak_ptr(mt))); + mt->output()->changed.connect_same_thread (*this, boost::bind (&Session::midi_output_change_handler, this, _1, _2, boost::weak_ptr(mt))); } } - if (input_auto_connect || output_auto_connect) { - auto_connect_route (r, existing_inputs, existing_outputs, true, input_auto_connect); + auto_connect_route (r, input_auto_connect, ChanCount (), ChanCount (), existing_inputs, existing_outputs); + existing_inputs += r->n_inputs(); + existing_outputs += r->n_outputs(); } /* order keys are a GUI responsibility but we need to set up - reasonable defaults because they also affect the remote control - ID in most situations. - */ + reasonable defaults because they also affect the remote control + ID in most situations. + */ if (!r->has_order_key ()) { if (r->is_auditioner()) { @@ -3489,7 +3462,7 @@ Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool if ((*x)->is_monitor()) { /* relax */ } else if ((*x)->is_master()) { - /* relax */ + /* relax */ } else { (*x)->enable_monitor_send (); } @@ -3669,7 +3642,7 @@ Session::remove_routes (boost::shared_ptr routes_to_remove) resort_routes (); #endif - if (_process_graph) { + if (_process_graph && !(_state_of_the_state & Deletion)) { _process_graph->clear_other_chain (); } @@ -3686,6 +3659,10 @@ Session::remove_routes (boost::shared_ptr routes_to_remove) (*iter)->drop_references (); } + if (_state_of_the_state & Deletion) { + return; + } + Route::RemoteControlIDChange(); /* EMIT SIGNAL */ /* save the new state of the world */ @@ -6789,3 +6766,191 @@ Session::clear_object_selection () follow_playhead_priority (); #endif } + +void +Session::auto_connect_route (boost::shared_ptr route, bool connect_inputs, + const ChanCount& input_start, + const ChanCount& output_start, + const ChanCount& input_offset, + const ChanCount& output_offset) +{ + Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock); + _auto_connect_queue.push (AutoConnectRequest (route, connect_inputs, + input_start, output_start, + input_offset, output_offset)); + + if (pthread_mutex_trylock (&_auto_connect_mutex) == 0) { + pthread_cond_signal (&_auto_connect_cond); + pthread_mutex_unlock (&_auto_connect_mutex); + } +} + +void +Session::auto_connect (const AutoConnectRequest& ar) +{ + boost::shared_ptr route = ar.route.lock(); + + if (!route) { return; } + + if (!IO::connecting_legal) { + return; + } + + /* If both inputs and outputs are auto-connected to physical ports, + * use the max of input and output offsets to ensure auto-connected + * port numbers always match up (e.g. the first audio input and the + * first audio output of the route will have the same physical + * port number). Otherwise just use the lowest input or output + * offset possible. + */ + + const bool in_out_physical = + (Config->get_input_auto_connect() & AutoConnectPhysical) + && (Config->get_output_auto_connect() & AutoConnectPhysical) + && ar.connect_inputs; + + const ChanCount in_offset = in_out_physical + ? ChanCount::max(ar.input_offset, ar.output_offset) + : ar.input_offset; + + const ChanCount out_offset = in_out_physical + ? ChanCount::max(ar.input_offset, ar.output_offset) + : ar.output_offset; + + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + vector physinputs; + vector physoutputs; + + _engine.get_physical_outputs (*t, physoutputs); + _engine.get_physical_inputs (*t, physinputs); + + if (!physinputs.empty() && ar.connect_inputs) { + uint32_t nphysical_in = physinputs.size(); + + for (uint32_t i = ar.input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) { + string port; + + if (Config->get_input_auto_connect() & AutoConnectPhysical) { + port = physinputs[(in_offset.get(*t) + i) % nphysical_in]; + } + + if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) { + break; + } + } + } + + if (!physoutputs.empty()) { + uint32_t nphysical_out = physoutputs.size(); + for (uint32_t i = ar.output_start.get(*t); i < route->n_outputs().get(*t); ++i) { + string port; + + /* Waves Tracks: + * do not create new connections if we reached the limit of physical outputs + * in Multi Out mode + */ + if (!(Config->get_output_auto_connect() & AutoConnectMaster) && + ARDOUR::Profile->get_trx () && + ar.output_offset.get(*t) == nphysical_out ) { + break; + } + + if ((*t) == DataType::MIDI && (Config->get_output_auto_connect() & AutoConnectPhysical)) { + port = physoutputs[(out_offset.get(*t) + i) % nphysical_out]; + } else if ((*t) == DataType::AUDIO && (Config->get_output_auto_connect() & AutoConnectMaster)) { + /* master bus is audio only */ + if (_master_out && _master_out->n_inputs().get(*t) > 0) { + port = _master_out->input()->ports().port(*t, + i % _master_out->input()->n_ports().get(*t))->name(); + } + } + + if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) { + break; + } + } + } + } +} + +void +Session::auto_connect_thread_start () +{ + if (_ac_thread_active) { + return; + } + + while (!_auto_connect_queue.empty ()) { + _auto_connect_queue.pop (); + } + + _ac_thread_active = true; + if (pthread_create (&_auto_connect_thread, NULL, auto_connect_thread, this)) { + _ac_thread_active = false; + } +} + +void +Session::auto_connect_thread_terminate () +{ + if (!_ac_thread_active) { + return; + } + _ac_thread_active = false; + + { + Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock); + while (!_auto_connect_queue.empty ()) { + _auto_connect_queue.pop (); + } + } + + if (pthread_mutex_lock (&_auto_connect_mutex) == 0) { + pthread_cond_signal (&_auto_connect_cond); + pthread_mutex_unlock (&_auto_connect_mutex); + } + + void *status; + pthread_join (_auto_connect_thread, &status); +} + +void * +Session::auto_connect_thread (void *arg) +{ + Session *s = static_cast(arg); + s->auto_connect_thread_run (); + pthread_exit (0); + return 0; +} + +void +Session::auto_connect_thread_run () +{ + pthread_set_name (X_("autoconnect")); + SessionEvent::create_per_thread_pool (X_("autoconnect"), 1024); + PBD::notify_event_loops_about_thread_creation (pthread_self(), X_("autoconnect"), 1024); + pthread_mutex_lock (&_auto_connect_mutex); + while (_ac_thread_active) { + + if (!_auto_connect_queue.empty ()) { + // Why would we need the process lock ?? + // A: if ports are added while we're connecting, the backend's iterator may be invalidated: + // graph_order_callback() -> resort_routes() -> direct_feeds_according_to_reality () -> backend::connected_to() + // All ardour-internal backends use a std::vector xxxAudioBackend::find_port() + // We have control over those, but what does jack do? + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + + Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock); + while (!_auto_connect_queue.empty ()) { + const AutoConnectRequest ar (_auto_connect_queue.front()); + _auto_connect_queue.pop (); + lx.release (); + auto_connect (ar); + lx.acquire (); + } + } + + pthread_cond_wait (&_auto_connect_cond, &_auto_connect_mutex); + } + pthread_mutex_unlock (&_auto_connect_mutex); +}