From 13b3ddc301b71e303249b196ef0d7bb5e66675e8 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Sat, 30 Jan 2010 10:58:59 -0600 Subject: [PATCH] Attempt to cope with failure to create JACK clients and ports. --- Mixer/Chain.C | 11 +++++++++- Mixer/Controller_Module.C | 7 +++++++ Mixer/JACK_Module.C | 20 +++++++++++++++++++ Mixer/JACK_Module.H | 2 ++ Timeline/Control_Sequence.C | 13 ++++++++++++ Timeline/Engine/Track.C | 10 ++++++++++ nonlib/JACK/Port.C | 40 ++++++++++++++++++++++++------------- nonlib/JACK/Port.H | 7 +++++-- 8 files changed, 93 insertions(+), 17 deletions(-) diff --git a/Mixer/Chain.C b/Mixer/Chain.C index 1a31824..1dd35e4 100644 --- a/Mixer/Chain.C +++ b/Mixer/Chain.C @@ -414,7 +414,16 @@ Chain::name ( const char *name ) engine()->buffer_size_callback( &Chain::buffer_size, this ); - engine()->init( ename ); + const char *jack_name = engine()->init( ename ); + + if ( ! jack_name ) + { + _engine = NULL; + + fl_alert( "Could not create JACK client. Perhaps the sound device already in use. In any event, now I'll die." ); + exit( 1 ); + return; + } } else { diff --git a/Mixer/Controller_Module.C b/Mixer/Controller_Module.C index 9a37047..badf5a6 100644 --- a/Mixer/Controller_Module.C +++ b/Mixer/Controller_Module.C @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -159,6 +160,12 @@ Controller_Module::mode ( Mode m ) JACK::Port po( chain()->engine(), JACK::Port::Input, p->name(), 0, "CV" ); + if ( ! po.activate() ) + { + fl_alert( "Could not activate JACK port \"%s\"", po.name() ); + return; + } + if ( po.valid() ) { jack_input.push_back( po ); diff --git a/Mixer/JACK_Module.C b/Mixer/JACK_Module.C index 0b7bd24..f0e44f2 100644 --- a/Mixer/JACK_Module.C +++ b/Mixer/JACK_Module.C @@ -21,6 +21,8 @@ #include +#include + #include "dsp.h" #include "Engine/Engine.H" @@ -91,6 +93,12 @@ JACK_Module::configure_inputs ( int n ) { JACK::Port po( chain()->engine(), JACK::Port::Output, i ); + if ( ! po.activate() ) + { + jack_port_activation_error( &po ); + return false; + } + if ( po.valid() ) { add_port( Port( this, Port::INPUT, Port::AUDIO ) ); @@ -114,6 +122,12 @@ JACK_Module::configure_inputs ( int n ) return true; } +void +JACK_Module::jack_port_activation_error ( JACK::Port *p ) +{ + fl_alert( "Could not activate JACK port \"%s\"", p->name() ); +} + bool JACK_Module::configure_outputs ( int n ) { @@ -125,6 +139,12 @@ JACK_Module::configure_outputs ( int n ) { JACK::Port po( chain()->engine(), JACK::Port::Input, i ); + if ( ! po.activate() ) + { + jack_port_activation_error( &po ); + return false; + } + if ( po.valid() ) { add_port( Port( this, Port::OUTPUT, Port::AUDIO ) ); diff --git a/Mixer/JACK_Module.H b/Mixer/JACK_Module.H index 91edd9f..a7e3e33 100644 --- a/Mixer/JACK_Module.H +++ b/Mixer/JACK_Module.H @@ -29,6 +29,8 @@ class JACK_Module : public Module std::vector jack_input; std::vector jack_output; + static void jack_port_activation_error ( JACK::Port *p ); + public: JACK_Module ( ); diff --git a/Timeline/Control_Sequence.C b/Timeline/Control_Sequence.C index 5feb38f..6dcc17f 100644 --- a/Timeline/Control_Sequence.C +++ b/Timeline/Control_Sequence.C @@ -17,6 +17,9 @@ /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*******************************************************************************/ +#include "const.h" +#include "debug.h" + #include #include "Control_Sequence.H" @@ -43,6 +46,11 @@ Control_Sequence::Control_Sequence ( Track *track ) : Sequence( 0 ) _output = new JACK::Port( engine, JACK::Port::Output, track->name(), track->ncontrols(), "cv" ); + if ( ! _output->activate() ) + { + FATAL( "could not create JACK port" ); + } + if ( track ) track->add( this ); @@ -112,6 +120,11 @@ Control_Sequence::set ( Log_Entry &e ) _output = new JACK::Port( engine, JACK::Port::Output, t->name(), t->ncontrols(), "cv" ); + if ( ! _output->activate() ) + { + FATAL( "could not create JACK port" ); + } + t->add( this ); } else if ( ! strcmp( ":name", s ) ) diff --git a/Timeline/Engine/Track.C b/Timeline/Engine/Track.C index 2f54cf4..6e2ad80 100644 --- a/Timeline/Engine/Track.C +++ b/Timeline/Engine/Track.C @@ -79,6 +79,11 @@ Track::configure_outputs ( int n ) { JACK::Port p( engine, JACK::Port::Output, name(), i ); + if ( !p.activate() ) + { + FATAL( "could not created output port!"); + } + if ( p.valid() ) output.push_back( p ); else @@ -126,6 +131,11 @@ Track::configure_inputs ( int n ) { JACK::Port p( engine, JACK::Port::Input, name(), i ); + if ( !p.activate() ) + { + FATAL( "could not created output port!"); + } + if ( p.valid() ) input.push_back( p ); else diff --git a/nonlib/JACK/Port.C b/nonlib/JACK/Port.C index 30cae49..ad62300 100644 --- a/nonlib/JACK/Port.C +++ b/nonlib/JACK/Port.C @@ -42,6 +42,7 @@ namespace JACK _freezer = rhs._freezer; _client = rhs._client; _port = rhs._port; + _direction = rhs._direction; _name = strdup( rhs._name ); _client->port_added( this ); @@ -54,6 +55,7 @@ namespace JACK _client = client; _port = port; _name = strdup( jack_port_name( port ) ); + _direction = jack_port_flags( _port ) == JackPortIsOutput ? Output : Input; } Port::Port ( JACK::Client *client, const char *name, type_e dir ) @@ -61,7 +63,9 @@ namespace JACK _name = NULL; _freezer = NULL; _client = client; - activate( name, dir ); + _direction = dir; + + _name = strdup( name ); } Port::Port ( JACK::Client *client, type_e dir, const char *base, int n, const char *type ) @@ -70,9 +74,8 @@ namespace JACK _freezer = NULL; _client = client; - const char *name = name_for_port( dir, base, n, type ); - - activate( name, dir ); + _name = strdup( name_for_port( dir, base, n, type ) ); + _direction = dir; } Port::Port ( JACK::Client *client, type_e dir, int n, const char *type ) @@ -81,9 +84,9 @@ namespace JACK _freezer = NULL; _client = client; - const char *name = name_for_port( dir, NULL, n, type ); + _name = strdup( name_for_port( dir, NULL, n, type ) ); + _direction = dir; - activate( name, dir ); } Port::~Port ( ) @@ -141,16 +144,29 @@ namespace JACK return pname; } - void + bool Port::activate ( const char *name, type_e dir ) { _name = strdup( name ); + _direction = dir; + + return activate(); + } + + bool + Port::activate ( void ) + { _port = jack_port_register( _client->jack_client(), _name, JACK_DEFAULT_AUDIO_TYPE, - dir == Output ? JackPortIsOutput : JackPortIsInput, + _direction == Output ? JackPortIsOutput : JackPortIsInput, 0 ); + if ( ! _port ) + return false; + _client->port_added( this ); + + return true; } /** returns the sum of latency of all ports between this one and a @@ -239,10 +255,7 @@ namespace JACK Port::type_e Port::type ( void ) const { - if ( _freezer ) - return _freezer->direction; - else - return jack_port_flags( _port ) == JackPortIsOutput ? Output : Input; + return _direction; } /** Restore the connections returned by connections() */ @@ -295,7 +308,6 @@ namespace JACK freeze_state *f = new freeze_state(); f->connections = connections(); - f->direction = type(); f->name = strdup( name() ); _freezer = f; @@ -304,7 +316,7 @@ namespace JACK void Port::thaw ( void ) { - activate( name(), _freezer->direction ); + activate(); connections( _freezer->connections ); diff --git a/nonlib/JACK/Port.H b/nonlib/JACK/Port.H index 712ec45..24f3c4b 100644 --- a/nonlib/JACK/Port.H +++ b/nonlib/JACK/Port.H @@ -67,7 +67,7 @@ namespace JACK nframes_t latency ( void ) const; void latency ( nframes_t frames ); - void activate ( const char *name, type_e dir ); + bool activate ( void ); void shutdown ( void ); void write ( sample_t *buf, nframes_t nframes ); void read ( sample_t *buf, nframes_t nframes ); @@ -82,12 +82,15 @@ namespace JACK private: + type_e _direction; + + bool activate ( const char *name, type_e dir ); + /* holds all we need to know about a jack port to recreate it on a new client */ struct freeze_state { const char **connections; - type_e direction; char *name; freeze_state ( )