2013-07-29 23:41:41 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
2013-09-14 16:34:08 -04:00
|
|
|
#include <iostream>
|
2013-07-29 23:41:41 -04:00
|
|
|
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
|
2013-12-03 10:24:05 -05:00
|
|
|
#include <glibmm/timer.h>
|
|
|
|
|
2013-07-29 23:41:41 -04:00
|
|
|
#include "pbd/epa.h"
|
|
|
|
|
2013-09-07 11:03:57 -04:00
|
|
|
#include "jack_connection.h"
|
|
|
|
#include "jack_utils.h"
|
2013-07-29 23:41:41 -04:00
|
|
|
|
|
|
|
#define GET_PRIVATE_JACK_POINTER(j) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
|
|
|
|
#define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
|
|
|
|
|
|
|
|
using namespace ARDOUR;
|
|
|
|
using namespace PBD;
|
2013-08-03 16:37:10 -04:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2013-09-14 16:18:17 -04:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2013-07-29 23:41:41 -04:00
|
|
|
|
2013-09-25 22:56:28 -04:00
|
|
|
bool JackConnection::_in_control = false;
|
|
|
|
|
2013-07-29 23:41:41 -04:00
|
|
|
static void jack_halted_callback (void* arg)
|
|
|
|
{
|
|
|
|
JackConnection* jc = static_cast<JackConnection*> (arg);
|
|
|
|
jc->halted_callback ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void jack_halted_info_callback (jack_status_t code, const char* reason, void* arg)
|
|
|
|
{
|
|
|
|
JackConnection* jc = static_cast<JackConnection*> (arg);
|
|
|
|
jc->halted_info_callback (code, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JackConnection::JackConnection (const std::string& arg1, const std::string& arg2)
|
|
|
|
: _jack (0)
|
2013-07-31 19:47:20 -04:00
|
|
|
, _client_name (arg1)
|
2013-07-29 23:41:41 -04:00
|
|
|
, session_uuid (arg2)
|
2015-06-14 12:10:31 -04:00
|
|
|
, _probed_buffer_size (0)
|
|
|
|
, _probed_sample_rate (0)
|
2013-07-29 23:41:41 -04:00
|
|
|
{
|
2015-10-04 14:51:05 -04:00
|
|
|
/* See if the server is already up
|
2013-09-25 22:56:28 -04:00
|
|
|
*/
|
2013-07-29 23:41:41 -04:00
|
|
|
|
2013-08-03 11:57:56 -04:00
|
|
|
EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
|
|
|
|
boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
|
|
|
|
|
|
|
|
/* revert all environment settings back to whatever they were when
|
|
|
|
* ardour started, because ardour's startup script may have reset
|
|
|
|
* something in ways that interfere with finding/starting JACK.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (global_epa) {
|
|
|
|
current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
|
|
|
|
global_epa->restore ();
|
|
|
|
}
|
|
|
|
|
|
|
|
jack_status_t status;
|
2014-10-25 10:12:38 -04:00
|
|
|
jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);
|
2013-08-03 11:57:56 -04:00
|
|
|
|
|
|
|
if (status == 0) {
|
2015-06-14 12:10:31 -04:00
|
|
|
_probed_buffer_size = jack_get_buffer_size(c);
|
|
|
|
_probed_sample_rate = jack_get_sample_rate(c);
|
2013-08-03 11:57:56 -04:00
|
|
|
jack_client_close (c);
|
2013-09-25 22:56:28 -04:00
|
|
|
_in_control = false;
|
|
|
|
} else {
|
|
|
|
_in_control = true;
|
2013-08-03 11:57:56 -04:00
|
|
|
}
|
2013-09-25 22:56:28 -04:00
|
|
|
}
|
2013-08-03 11:57:56 -04:00
|
|
|
|
2013-09-25 22:56:28 -04:00
|
|
|
JackConnection::~JackConnection ()
|
|
|
|
{
|
|
|
|
close ();
|
2013-08-03 11:57:56 -04:00
|
|
|
}
|
|
|
|
|
2013-07-29 23:41:41 -04:00
|
|
|
int
|
|
|
|
JackConnection::open ()
|
|
|
|
{
|
|
|
|
EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
|
|
|
|
boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
|
|
|
|
jack_status_t status;
|
|
|
|
|
2013-07-30 17:48:57 -04:00
|
|
|
close ();
|
|
|
|
|
2013-07-29 23:41:41 -04:00
|
|
|
/* revert all environment settings back to whatever they were when ardour started
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (global_epa) {
|
|
|
|
current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
|
|
|
|
global_epa->restore ();
|
|
|
|
}
|
|
|
|
|
2013-08-03 16:37:10 -04:00
|
|
|
/* ensure that PATH or equivalent includes likely locations of the JACK
|
|
|
|
* server, in case the user's default does not.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vector<string> dirs;
|
|
|
|
get_jack_server_dir_paths (dirs);
|
|
|
|
set_path_env_for_jack_autostart (dirs);
|
|
|
|
|
2014-10-25 10:12:38 -04:00
|
|
|
if ((_jack = jack_client_open (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
|
2013-07-29 23:41:41 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status & JackNameNotUnique) {
|
2013-07-31 19:47:20 -04:00
|
|
|
_client_name = jack_get_client_name (_jack);
|
2013-07-29 23:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* attach halted handler */
|
|
|
|
|
|
|
|
if (jack_on_info_shutdown) {
|
|
|
|
jack_on_info_shutdown (_jack, jack_halted_info_callback, this);
|
|
|
|
} else {
|
|
|
|
jack_on_shutdown (_jack, jack_halted_callback, this);
|
|
|
|
}
|
|
|
|
|
2013-09-06 13:06:44 -04:00
|
|
|
|
|
|
|
Connected(); /* EMIT SIGNAL */
|
|
|
|
|
2013-07-29 23:41:41 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
JackConnection::close ()
|
|
|
|
{
|
|
|
|
GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
|
|
|
|
|
2015-10-05 10:17:49 -04:00
|
|
|
if (_priv_jack) {
|
2013-08-07 22:22:11 -04:00
|
|
|
int ret = jack_client_close (_priv_jack);
|
|
|
|
_jack = 0;
|
2013-11-27 15:25:10 -05:00
|
|
|
|
|
|
|
/* If we started JACK, it will be closing down */
|
2013-12-03 10:24:05 -05:00
|
|
|
Glib::usleep (500000);
|
2013-11-27 15:25:10 -05:00
|
|
|
|
2013-08-07 22:22:11 -04:00
|
|
|
Disconnected (""); /* EMIT SIGNAL */
|
2013-11-27 15:25:10 -05:00
|
|
|
|
2013-08-07 22:22:11 -04:00
|
|
|
return ret;
|
2013-07-29 23:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JackConnection::halted_callback ()
|
|
|
|
{
|
|
|
|
_jack = 0;
|
2013-09-14 16:24:21 -04:00
|
|
|
std::cerr << "JACK HALTED\n";
|
2013-07-29 23:41:41 -04:00
|
|
|
Disconnected ("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JackConnection::halted_info_callback (jack_status_t /*status*/, const char* reason)
|
|
|
|
{
|
|
|
|
_jack = 0;
|
2013-09-14 16:24:21 -04:00
|
|
|
std::cerr << "JACK HALTED: " << reason << std::endl;
|
2013-07-29 23:41:41 -04:00
|
|
|
Disconnected (reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
|