Jack2  1.9.9
JackAudioDriver.cpp
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 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 (at your option) any later version.
12 
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 #include "JackSystemDeps.h"
22 #include "JackAudioDriver.h"
23 #include "JackTime.h"
24 #include "JackError.h"
25 #include "JackEngineControl.h"
26 #include "JackPort.h"
27 #include "JackGraphManager.h"
28 #include "JackLockedEngine.h"
29 #include "JackException.h"
30 #include <assert.h>
31 
32 using namespace std;
33 
34 namespace Jack
35 {
36 
37 JackAudioDriver::JackAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
38  : JackDriver(name, alias, engine, table)
39 {}
40 
41 JackAudioDriver::~JackAudioDriver()
42 {}
43 
44 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
45 {
46  // Update engine and graph manager state
47  fEngineControl->fBufferSize = buffer_size;
48  fGraphManager->SetBufferSize(buffer_size);
49 
50  fEngineControl->UpdateTimeOut();
51  UpdateLatencies();
52 
53  // Redirected on slaves drivers...
54  return JackDriver::SetBufferSize(buffer_size);
55 }
56 
57 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
58 {
59  fEngineControl->fSampleRate = sample_rate;
60  fEngineControl->UpdateTimeOut();
61 
62  // Redirected on slaves drivers...
63  return JackDriver::SetSampleRate(sample_rate);
64 }
65 
66 int JackAudioDriver::Open(jack_nframes_t buffer_size,
67  jack_nframes_t samplerate,
68  bool capturing,
69  bool playing,
70  int inchannels,
71  int outchannels,
72  bool monitor,
73  const char* capture_driver_name,
74  const char* playback_driver_name,
75  jack_nframes_t capture_latency,
76  jack_nframes_t playback_latency)
77 {
78  fCaptureChannels = inchannels;
79  fPlaybackChannels = outchannels;
80  fWithMonitorPorts = monitor;
81  memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
82  memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
83  memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
84  return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels,
85  monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
86 }
87 
88 int JackAudioDriver::Open(bool capturing,
89  bool playing,
90  int inchannels,
91  int outchannels,
92  bool monitor,
93  const char* capture_driver_name,
94  const char* playback_driver_name,
95  jack_nframes_t capture_latency,
96  jack_nframes_t playback_latency)
97 {
98  fCaptureChannels = inchannels;
99  fPlaybackChannels = outchannels;
100  fWithMonitorPorts = monitor;
101  memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
102  memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
103  memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
104  return JackDriver::Open(capturing, playing, inchannels, outchannels,
105  monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
106 }
107 
108 void JackAudioDriver::UpdateLatencies()
109 {
110  jack_latency_range_t input_range;
111  jack_latency_range_t output_range;
112  jack_latency_range_t monitor_range;
113 
114  for (int i = 0; i < fCaptureChannels; i++) {
115  input_range.max = input_range.min = fEngineControl->fBufferSize + fCaptureLatency;
116  fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
117  }
118 
119  for (int i = 0; i < fPlaybackChannels; i++) {
120  output_range.max = output_range.min = fPlaybackLatency;
121  if (fEngineControl->fSyncMode) {
122  output_range.max = output_range.min += fEngineControl->fBufferSize;
123  } else {
124  output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
125  }
126  fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
127  if (fWithMonitorPorts) {
128  monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
129  fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
130  }
131  }
132 }
133 
134 int JackAudioDriver::Attach()
135 {
136  JackPort* port;
137  jack_port_id_t port_index;
138  char name[REAL_JACK_PORT_NAME_SIZE];
139  char alias[REAL_JACK_PORT_NAME_SIZE];
140  int i;
141 
142  jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
143 
144  for (i = 0; i < fCaptureChannels; i++) {
145  snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
146  snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
147  if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
148  jack_error("driver: cannot register port for %s", name);
149  return -1;
150  }
151  port = fGraphManager->GetPort(port_index);
152  port->SetAlias(alias);
153  fCapturePortList[i] = port_index;
154  jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
155  }
156 
157  for (i = 0; i < fPlaybackChannels; i++) {
158  snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
159  snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
160  if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
161  jack_error("driver: cannot register port for %s", name);
162  return -1;
163  }
164  port = fGraphManager->GetPort(port_index);
165  port->SetAlias(alias);
166  fPlaybackPortList[i] = port_index;
167  jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
168 
169  // Monitor ports
170  if (fWithMonitorPorts) {
171  jack_log("Create monitor port");
172  snprintf(name, sizeof(name), "%s:monitor_%u", fClientControl.fName, i + 1);
173  if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
174  jack_error("Cannot register monitor port for %s", name);
175  return -1;
176  } else {
177  fMonitorPortList[i] = port_index;
178  }
179  }
180  }
181 
182  UpdateLatencies();
183  return 0;
184 }
185 
186 int JackAudioDriver::Detach()
187 {
188  int i;
189  jack_log("JackAudioDriver::Detach");
190 
191  for (i = 0; i < fCaptureChannels; i++) {
192  fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
193  }
194 
195  for (i = 0; i < fPlaybackChannels; i++) {
196  fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
197  if (fWithMonitorPorts) {
198  fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
199  }
200  }
201 
202  return 0;
203 }
204 
205 int JackAudioDriver::Write()
206 {
207  for (int i = 0; i < fPlaybackChannels; i++) {
208  if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
209  jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
210  int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
211  // Monitor ports
212  if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
213  memcpy(GetMonitorBuffer(i), buffer, size);
214  }
215  }
216  return 0;
217 }
218 
219 int JackAudioDriver::Process()
220 {
221  return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
222 }
223 
224 /*
225 The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
226 synchronize to the end of client graph execution.
227 */
228 
229 int JackAudioDriver::ProcessAsync()
230 {
231  // Read input buffers for the current cycle
232  if (Read() < 0) {
233  jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
234  return -1;
235  }
236 
237  // Write output buffers from the previous cycle
238  if (Write() < 0) {
239  jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
240  return -1;
241  }
242 
243  // Process graph
244  ProcessGraphAsync();
245 
246  // Keep end cycle time
247  JackDriver::CycleTakeEndTime();
248  return 0;
249 }
250 
251 void JackAudioDriver::ProcessGraphAsync()
252 {
253  // Process graph
254  if (fIsMaster) {
255  ProcessGraphAsyncMaster();
256  } else {
257  ProcessGraphAsyncSlave();
258  }
259 }
260 
261 void JackAudioDriver::ProcessGraphAsyncMaster()
262 {
263  // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
264  if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
265  jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
266  }
267 
268  if (ResumeRefNum() < 0) {
269  jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
270  }
271 
272  if (ProcessReadSlaves() < 0) {
273  jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
274  }
275 
276  if (ProcessWriteSlaves() < 0) {
277  jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
278  }
279 
280  // Does not wait on graph execution end
281 }
282 
283 void JackAudioDriver::ProcessGraphAsyncSlave()
284 {
285  if (ResumeRefNum() < 0) {
286  jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
287  }
288 }
289 
290 /*
291 The driver SYNC mode: the server does synchronize to the end of client graph execution,
292 if graph process succeed, output buffers computed at the *current cycle* are used.
293 */
294 
295 int JackAudioDriver::ProcessSync()
296 {
297  // Read input buffers for the current cycle
298  if (Read() < 0) {
299  jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
300  return -1;
301  }
302 
303  // Process graph
304  ProcessGraphSync();
305 
306  // Write output buffers from the current cycle
307  if (Write() < 0) {
308  jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
309  return -1;
310  }
311 
312  // Keep end cycle time
313  JackDriver::CycleTakeEndTime();
314  return 0;
315 }
316 
317 void JackAudioDriver::ProcessGraphSync()
318 {
319  // Process graph
320  if (fIsMaster) {
321  ProcessGraphSyncMaster();
322  } else {
323  ProcessGraphSyncSlave();
324  }
325 }
326 
327 void JackAudioDriver::ProcessGraphSyncMaster()
328 {
329  // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
330  if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
331 
332  if (ResumeRefNum() < 0) {
333  jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
334  }
335 
336  if (ProcessReadSlaves() < 0) {
337  jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
338  }
339 
340  if (ProcessWriteSlaves() < 0) {
341  jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
342  }
343 
344  // Waits for graph execution end
345  if (SuspendRefNum() < 0) {
346  jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
347  }
348 
349  } else { // Graph not finished: do not activate it
350  jack_error("JackAudioDriver::ProcessGraphSync: Process error");
351  }
352 }
353 
354 void JackAudioDriver::ProcessGraphSyncSlave()
355 {
356  if (ResumeRefNum() < 0) {
357  jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
358  }
359 }
360 
361 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
362 {
363  return fCapturePortList[port_index]
364  ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
365  : NULL;
366 }
367 
368 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
369 {
370  return fPlaybackPortList[port_index]
371  ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
372  : NULL;
373 }
374 
375 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
376 {
377  return fPlaybackPortList[port_index]
378  ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
379  : NULL;
380 }
381 
382 int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
383 {
384  switch (notify) {
385 
386  case kLatencyCallback:
387  HandleLatencyCallback(value1);
388  break;
389 
390  default:
391  JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
392  break;
393  }
394 
395  return 0;
396 }
397 
398 void JackAudioDriver::HandleLatencyCallback(int status)
399 {
400  jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
401 
402  for (int i = 0; i < fCaptureChannels; i++) {
403  if (mode == JackPlaybackLatency) {
404  fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
405  }
406  }
407 
408  for (int i = 0; i < fPlaybackChannels; i++) {
409  if (mode == JackCaptureLatency) {
410  fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
411  }
412  }
413 }
414 
415 } // end of namespace
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
jack_nframes_t min
Definition: types.h:268
jack_nframes_t max
Definition: types.h:272
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107