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