merge with master.
[ardour.git] / libs / backends / jack / jack_api.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
20 #include "jack_connection.h"
21 #include "jack_audiobackend.h"
22
23 using namespace ARDOUR;
24
25 static boost::shared_ptr<JACKAudioBackend> backend;
26 static boost::shared_ptr<JackConnection> jack_connection;
27
28 static boost::shared_ptr<AudioBackend> backend_factory (AudioEngine& ae);
29 static int  instantiate (const std::string& arg1, const std::string& arg2);
30 static int  deinstantiate ();
31 static bool already_configured ();
32
33 static ARDOUR::AudioBackendInfo _descriptor = {
34         "JACK",
35         instantiate,
36         deinstantiate,
37         backend_factory,
38         already_configured,
39 };
40
41 static boost::shared_ptr<AudioBackend>
42 backend_factory (AudioEngine& ae)
43 {
44         if (!jack_connection) {
45                 return boost::shared_ptr<AudioBackend>();
46         }
47
48         if (!backend) {
49                 backend.reset (new JACKAudioBackend (ae, _descriptor, jack_connection));
50         }
51
52         return backend;
53 }
54
55 static int
56 instantiate (const std::string& arg1, const std::string& arg2)
57 {
58         try {
59                 jack_connection.reset (new JackConnection (arg1, arg2));
60         } catch (...) {
61                 return -1;
62         }
63
64         return 0;
65 }
66
67 static int 
68 deinstantiate ()
69 {
70         backend.reset ();
71         jack_connection.reset ();
72
73         return 0;
74 }
75
76 static bool
77 already_configured ()
78 {
79         return !JackConnection::in_control ();
80 }
81
82 extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor() { return &_descriptor; }
83