1 /*
2  Copyright (C) 2001 Paul Davis
3  Copyright (C) 2004-2008 Grame
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19  */
20 
21 #ifndef __JackDriver__
22 #define __JackDriver__
23 
24 #include "types.h"
25 #include "JackClientInterface.h"
26 #include "JackConstants.h"
27 #include "JackPlatformPlug.h"
28 #include "JackClientControl.h"
29 #include <list>
30 
31 namespace Jack
32 {
33 
34 class JackLockedEngine;
35 class JackGraphManager;
36 struct JackEngineControl;
37 class JackSlaveDriverInterface;
38 
39 /*!
40 \brief The base interface for drivers.
41 */
42 
43 class SERVER_EXPORT JackDriverInterface
44 {
45 
46     public:
47 
JackDriverInterface()48         JackDriverInterface()
49         {}
~JackDriverInterface()50         virtual ~JackDriverInterface()
51         {}
52 
53         virtual int Open() = 0;
54 
55         virtual int Open(jack_nframes_t buffer_size,
56                          jack_nframes_t samplerate,
57                          bool capturing,
58                          bool playing,
59                          int inchannels,
60                          int outchannels,
61                          bool monitor,
62                          const char* capture_driver_name,
63                          const char* playback_driver_name,
64                          jack_nframes_t capture_latency,
65                          jack_nframes_t playback_latency) = 0;
66 
67         virtual int Attach() = 0;
68         virtual int Detach() = 0;
69 
70         virtual int Read() = 0;
71         virtual int Write() = 0;
72 
73         virtual int Start() = 0;
74         virtual int Stop() = 0;
75 
76         virtual bool IsFixedBufferSize() = 0;
77         virtual int SetBufferSize(jack_nframes_t buffer_size) = 0;
78         virtual int SetSampleRate(jack_nframes_t sample_rate) = 0;
79 
80         virtual int Process() = 0;
81 
82         virtual void SetMaster(bool onoff) = 0;
83         virtual bool GetMaster() = 0;
84 
85         virtual void AddSlave(JackDriverInterface* slave) = 0;
86         virtual void RemoveSlave(JackDriverInterface* slave) = 0;
87 
88         virtual std::list<JackDriverInterface*> GetSlaves() = 0;
89 
90         // For "master" driver
91         virtual int ProcessReadSlaves() = 0;
92         virtual int ProcessWriteSlaves() = 0;
93 
94         // For "slave" driver
95         virtual int ProcessRead() = 0;
96         virtual int ProcessWrite() = 0;
97 
98         // For "slave" driver in "synchronous" mode
99         virtual int ProcessReadSync() = 0;
100         virtual int ProcessWriteSync() = 0;
101 
102         // For "slave" driver in "asynchronous" mode
103         virtual int ProcessReadAsync() = 0;
104         virtual int ProcessWriteAsync() = 0;
105 
106         virtual bool IsRealTime() const = 0;
107         virtual bool IsRunning() const = 0;
108 };
109 
110 /*!
111  \brief The base interface for drivers clients.
112  */
113 
114 class SERVER_EXPORT JackDriverClientInterface : public JackDriverInterface, public JackClientInterface
115 {};
116 
117 /*!
118  \brief The base class for drivers.
119  */
120 
121 #define CaptureDriverFlags  static_cast<JackPortFlags>(JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal)
122 #define PlaybackDriverFlags static_cast<JackPortFlags>(JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal)
123 #define MonitorDriverFlags static_cast<JackPortFlags>(JackPortIsOutput)
124 
125 typedef std::list<std::pair<std::string, std::pair<std::string, std::string> > > driver_connections_list_t; // [type : (src, dst)]
126 
127 class SERVER_EXPORT JackDriver : public JackDriverClientInterface
128 {
129 
130     protected:
131 
132         char fCaptureDriverName[JACK_CLIENT_NAME_SIZE+1];
133         char fPlaybackDriverName[JACK_CLIENT_NAME_SIZE+1];
134         char fAliasName[JACK_CLIENT_NAME_SIZE+1];
135 
136         jack_nframes_t fCaptureLatency;
137         jack_nframes_t fPlaybackLatency;
138 
139         int fCaptureChannels;
140         int fPlaybackChannels;
141 
142         jack_time_t fBeginDateUst;
143         jack_time_t fEndDateUst;
144         float fDelayedUsecs;
145 
146         // Pointers to engine state
147         JackLockedEngine* fEngine;
148         JackGraphManager* fGraphManager;
149         JackSynchro* fSynchroTable;
150         JackEngineControl* fEngineControl;
151         JackClientControl fClientControl;
152 
153         std::list<JackDriverInterface*> fSlaveList;
154 
155         bool fIsMaster;
156         bool fIsRunning;
157         bool fWithMonitorPorts;
158 
159         // Static tables since the actual number of ports may be changed by the real driver
160         // thus dynamic allocation is more difficult to handle
161         jack_port_id_t fCapturePortList[DRIVER_PORT_NUM];
162         jack_port_id_t fPlaybackPortList[DRIVER_PORT_NUM];
163         jack_port_id_t fMonitorPortList[DRIVER_PORT_NUM];
164 
165         driver_connections_list_t fConnections;		// Connections list
166 
167         void CycleIncTime();
168         void CycleTakeBeginTime();
169         void CycleTakeEndTime();
170 
171         void SetupDriverSync(int ref, bool freewheel);
172 
173         void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);   // XRun notification sent by the driver
174         void NotifyBufferSize(jack_nframes_t buffer_size);                  // BufferSize notification sent by the driver
175         void NotifySampleRate(jack_nframes_t sample_rate);                  // SampleRate notification sent by the driver
176         void NotifyFailure(int code, const char* reason);                   // Failure notification sent by the driver
177 
178         virtual void SaveConnections(int alias);
179         virtual void LoadConnections(int alias, bool full_name = true);
180         std::string MatchPortName(const char* name, const char** ports, int alias, const std::string& type);
181 
182         virtual int StartSlaves();
183         virtual int StopSlaves();
184 
185         virtual int ResumeRefNum();
186         virtual int SuspendRefNum();
187 
188     public:
189 
190         JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
191         virtual ~JackDriver();
192 
193         void SetMaster(bool onoff);
194         bool GetMaster();
195 
196         void AddSlave(JackDriverInterface* slave);
197         void RemoveSlave(JackDriverInterface* slave);
198 
GetSlaves()199         std::list<JackDriverInterface*> GetSlaves()
200         {
201             return fSlaveList;
202         }
203 
204         virtual int Open();
205 
206         virtual int Open(jack_nframes_t buffer_size,
207                          jack_nframes_t samplerate,
208                          bool capturing,
209                          bool playing,
210                          int inchannels,
211                          int outchannels,
212                          bool monitor,
213                          const char* capture_driver_name,
214                          const char* playback_driver_name,
215                          jack_nframes_t capture_latency,
216                          jack_nframes_t playback_latency);
217 
218         virtual int Close();
219 
220         virtual int Process();
221 
222         virtual int Attach();
223         virtual int Detach();
224 
225         virtual int Read();
226         virtual int Write();
227 
228         virtual int Start();
229         virtual int Stop();
230 
231         // For "master" driver
232         int ProcessReadSlaves();
233         int ProcessWriteSlaves();
234 
235         // For "slave" driver with typically decompose a given cycle in separated Read and Write parts.
236         virtual int ProcessRead();
237         virtual int ProcessWrite();
238 
239         // For "slave" driver in "synchronous" mode
240         virtual int ProcessReadSync();
241         virtual int ProcessWriteSync();
242 
243         // For "slave" driver in "asynchronous" mode
244         virtual int ProcessReadAsync();
245         virtual int ProcessWriteAsync();
246 
247         virtual bool IsFixedBufferSize();
248         virtual int SetBufferSize(jack_nframes_t buffer_size);
249         virtual int SetSampleRate(jack_nframes_t sample_rate);
250 
251         virtual int ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2);
252         virtual JackClientControl* GetClientControl() const;
253 
254         virtual bool IsRealTime() const;
IsRunning()255         virtual bool IsRunning() const { return fIsRunning; }
256         virtual bool Initialize();  // To be called by the wrapping thread Init method when the driver is a "blocking" one
257 
258 };
259 
260 } // end of namespace
261 
262 #endif
263