Merge branch 'master' of https://github.com/nmains/ardour
[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 static void jack_halted_callback (void* arg)
40 {
41         JackConnection* jc = static_cast<JackConnection*> (arg);
42         jc->halted_callback ();
43 }
44
45 static void jack_halted_info_callback (jack_status_t code, const char* reason, void* arg)
46 {
47         JackConnection* jc = static_cast<JackConnection*> (arg);
48         jc->halted_info_callback (code, reason);
49 }
50
51
52 JackConnection::JackConnection (const std::string& arg1, const std::string& arg2)
53         : _jack (0)
54         , _client_name (arg1)
55         , session_uuid (arg2)
56 {
57         _in_control = !server_running();
58 }
59
60 JackConnection::~JackConnection ()
61 {
62         close ();
63 }
64
65 bool
66 JackConnection::server_running ()
67 {
68         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
69         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
70
71         /* revert all environment settings back to whatever they were when
72          * ardour started, because ardour's startup script may have reset
73          * something in ways that interfere with finding/starting JACK.
74          */
75
76         if (global_epa) {
77                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
78                 global_epa->restore ();
79         }
80
81         jack_status_t status;
82         jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);
83
84         if (status == 0) {
85                 jack_client_close (c);
86                 return true;
87         }
88
89         return false;
90 }
91
92 int
93 JackConnection::open ()
94 {
95         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
96         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
97         jack_status_t status;
98
99         close ();
100
101         /* revert all environment settings back to whatever they were when ardour started
102          */
103
104         if (global_epa) {
105                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
106                 global_epa->restore ();
107         }
108
109         /* check to see if the server is already running so that we know if we
110          * are starting it.
111          */
112
113         jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);
114
115         if (status == 0) {
116                 _in_control = false;
117                 jack_client_close (c);
118         } else {
119                 _in_control = true;
120         }
121
122         /* ensure that PATH or equivalent includes likely locations of the JACK
123          * server, in case the user's default does not.
124          */
125
126         vector<string> dirs;
127         get_jack_server_dir_paths (dirs);
128         set_path_env_for_jack_autostart (dirs);
129
130         if ((_jack = jack_client_open (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
131                 return -1;
132         }
133
134         if (status & JackNameNotUnique) {
135                 _client_name = jack_get_client_name (_jack);
136         }
137
138         /* attach halted handler */
139
140         if (jack_on_info_shutdown) {
141                 jack_on_info_shutdown (_jack, jack_halted_info_callback, this);
142         } else {
143                 jack_on_shutdown (_jack, jack_halted_callback, this);
144         }
145
146
147         Connected(); /* EMIT SIGNAL */
148
149         return 0;
150 }
151
152 int
153 JackConnection::close ()
154 {
155         GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
156
157         if (_priv_jack) {       
158                 int ret = jack_client_close (_priv_jack);
159                 _jack = 0;
160                 Disconnected (""); /* EMIT SIGNAL */
161                 return ret;
162         }
163
164         return 0;
165 }
166
167 void
168 JackConnection::halted_callback ()
169 {
170         _jack = 0;
171         std::cerr << "JACK HALTED\n";
172         Disconnected ("");
173 }
174
175 void
176 JackConnection::halted_info_callback (jack_status_t /*status*/, const char* reason)
177 {
178         _jack = 0;
179         std::cerr << "JACK HALTED: " << reason << std::endl;
180         Disconnected (reason);
181 }
182
183