don't ping JACK server 4 times to see if it is already up when ardour starts
[ardour.git] / libs / backends / jack / jack_connection.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 #include <iostream>
20
21 #include <boost/scoped_ptr.hpp>
22 #include <jack/session.h>
23
24 #include "pbd/epa.h"
25
26 #include "jack_connection.h"
27 #include "jack_utils.h"
28
29 #define GET_PRIVATE_JACK_POINTER(j)  jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
30 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using std::string;
35 using std::vector;
36 using std::cerr;
37 using std::endl;
38
39 bool JackConnection::_in_control = false;
40
41 static void jack_halted_callback (void* arg)
42 {
43         JackConnection* jc = static_cast<JackConnection*> (arg);
44         jc->halted_callback ();
45 }
46
47 static void jack_halted_info_callback (jack_status_t code, const char* reason, void* arg)
48 {
49         JackConnection* jc = static_cast<JackConnection*> (arg);
50         jc->halted_info_callback (code, reason);
51 }
52
53
54 JackConnection::JackConnection (const std::string& arg1, const std::string& arg2)
55         : _jack (0)
56         , _client_name (arg1)
57         , session_uuid (arg2)
58 {
59         /* See if the server is already up 
60          */
61
62         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
63         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
64
65         /* revert all environment settings back to whatever they were when
66          * ardour started, because ardour's startup script may have reset
67          * something in ways that interfere with finding/starting JACK.
68          */
69
70         if (global_epa) {
71                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
72                 global_epa->restore ();
73         }
74
75         jack_status_t status;
76         jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);
77
78         if (status == 0) {
79                 jack_client_close (c);
80                 _in_control = false;
81         } else {
82                 _in_control = true;
83         }
84 }
85
86 JackConnection::~JackConnection ()
87 {
88         close ();
89 }
90
91 int
92 JackConnection::open ()
93 {
94         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
95         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
96         jack_status_t status;
97
98         close ();
99
100         /* revert all environment settings back to whatever they were when ardour started
101          */
102
103         if (global_epa) {
104                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
105                 global_epa->restore ();
106         }
107
108         /* ensure that PATH or equivalent includes likely locations of the JACK
109          * server, in case the user's default does not.
110          */
111
112         vector<string> dirs;
113         get_jack_server_dir_paths (dirs);
114         set_path_env_for_jack_autostart (dirs);
115
116         if ((_jack = jack_client_open (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
117                 return -1;
118         }
119
120         if (status & JackNameNotUnique) {
121                 _client_name = jack_get_client_name (_jack);
122         }
123
124         /* attach halted handler */
125
126         if (jack_on_info_shutdown) {
127                 jack_on_info_shutdown (_jack, jack_halted_info_callback, this);
128         } else {
129                 jack_on_shutdown (_jack, jack_halted_callback, this);
130         }
131
132
133         Connected(); /* EMIT SIGNAL */
134
135         return 0;
136 }
137
138 int
139 JackConnection::close ()
140 {
141         GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
142
143         if (_priv_jack) {       
144                 int ret = jack_client_close (_priv_jack);
145                 _jack = 0;
146                 Disconnected (""); /* EMIT SIGNAL */
147                 return ret;
148         }
149
150         return 0;
151 }
152
153 void
154 JackConnection::halted_callback ()
155 {
156         _jack = 0;
157         std::cerr << "JACK HALTED\n";
158         Disconnected ("");
159 }
160
161 void
162 JackConnection::halted_info_callback (jack_status_t /*status*/, const char* reason)
163 {
164         _jack = 0;
165         std::cerr << "JACK HALTED: " << reason << std::endl;
166         Disconnected (reason);
167 }
168
169