Jack2  1.9.10
JackDriver.h
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 
43 class SERVER_EXPORT JackDriverInterface
44 {
45 
46  public:
47 
49  {}
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 
115 {};
116 
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 
135  char fAliasName[JACK_CLIENT_NAME_SIZE + 1];
136 
137  jack_nframes_t fCaptureLatency;
138  jack_nframes_t fPlaybackLatency;
139 
140  int fCaptureChannels;
141  int fPlaybackChannels;
142 
143  jack_time_t fBeginDateUst;
144  jack_time_t fEndDateUst;
145  float fDelayedUsecs;
146 
147  // Pointers to engine state
148  JackLockedEngine* fEngine;
149  JackGraphManager* fGraphManager;
150  JackSynchro* fSynchroTable;
151  JackEngineControl* fEngineControl;
152  JackClientControl fClientControl;
153 
154  std::list<JackDriverInterface*> fSlaveList;
155 
156  bool fIsMaster;
157  bool fIsRunning;
158  bool fWithMonitorPorts;
159 
160  // Static tables since the actual number of ports may be changed by the real driver
161  // thus dynamic allocation is more difficult to handle
162  jack_port_id_t fCapturePortList[DRIVER_PORT_NUM];
163  jack_port_id_t fPlaybackPortList[DRIVER_PORT_NUM];
164  jack_port_id_t fMonitorPortList[DRIVER_PORT_NUM];
165 
166  driver_connections_list_t fConnections; // Connections list
167 
168  void CycleIncTime();
169  void CycleTakeBeginTime();
170  void CycleTakeEndTime();
171 
172  void SetupDriverSync(int ref, bool freewheel);
173 
174  void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs); // XRun notification sent by the driver
175  void NotifyBufferSize(jack_nframes_t buffer_size); // BufferSize notification sent by the driver
176  void NotifySampleRate(jack_nframes_t sample_rate); // SampleRate notification sent by the driver
177  void NotifyFailure(int code, const char* reason); // Failure notification sent by the driver
178 
179  virtual void SaveConnections(int alias);
180  virtual void LoadConnections(int alias, bool full_name = true);
181  std::string MatchPortName(const char* name, const char** ports, int alias, const std::string& type);
182 
183  virtual int StartSlaves();
184  virtual int StopSlaves();
185 
186  virtual int ResumeRefNum();
187  virtual int SuspendRefNum();
188 
189  public:
190 
191  JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
192  virtual ~JackDriver();
193 
194  void SetMaster(bool onoff);
195  bool GetMaster();
196 
197  void AddSlave(JackDriverInterface* slave);
198  void RemoveSlave(JackDriverInterface* slave);
199 
200  std::list<JackDriverInterface*> GetSlaves()
201  {
202  return fSlaveList;
203  }
204 
205  virtual int Open();
206 
207  virtual int Open(jack_nframes_t buffer_size,
208  jack_nframes_t samplerate,
209  bool capturing,
210  bool playing,
211  int inchannels,
212  int outchannels,
213  bool monitor,
214  const char* capture_driver_name,
215  const char* playback_driver_name,
216  jack_nframes_t capture_latency,
217  jack_nframes_t playback_latency);
218 
219  virtual int Close();
220 
221  virtual int Process();
222 
223  virtual int Attach();
224  virtual int Detach();
225 
226  virtual int Read();
227  virtual int Write();
228 
229  virtual int Start();
230  virtual int Stop();
231 
232  // For "master" driver
233  int ProcessReadSlaves();
234  int ProcessWriteSlaves();
235 
236  // For "slave" driver with typically decompose a given cycle in separated Read and Write parts.
237  virtual int ProcessRead();
238  virtual int ProcessWrite();
239 
240  // For "slave" driver in "synchronous" mode
241  virtual int ProcessReadSync();
242  virtual int ProcessWriteSync();
243 
244  // For "slave" driver in "asynchronous" mode
245  virtual int ProcessReadAsync();
246  virtual int ProcessWriteAsync();
247 
248  virtual bool IsFixedBufferSize();
249  virtual int SetBufferSize(jack_nframes_t buffer_size);
250  virtual int SetSampleRate(jack_nframes_t sample_rate);
251 
252  virtual int ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2);
253  virtual JackClientControl* GetClientControl() const;
254 
255  virtual bool IsRealTime() const;
256  virtual bool IsRunning() const { return fIsRunning; }
257  virtual bool Initialize(); // To be called by the wrapping thread Init method when the driver is a "blocking" one
258 
259 };
260 
261 } // end of namespace
262 
263 #endif