2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame.
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.
13 GNU General Public License for more details.
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.
21 #include "JackSystemDeps.h"
22 #include "JackAudioDriver.h"
24 #include "JackError.h"
25 #include "JackEngineControl.h"
27 #include "JackGraphManager.h"
28 #include "JackLockedEngine.h"
29 #include "JackException.h"
37 JackAudioDriver::JackAudioDriver(const char* name
, const char* alias
, JackLockedEngine
* engine
, JackSynchro
* table
)
38 : JackDriver(name
, alias
, engine
, table
)
41 JackAudioDriver::~JackAudioDriver()
44 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size
)
46 // Update engine and graph manager state
47 fEngineControl
->fBufferSize
= buffer_size
;
48 fGraphManager
->SetBufferSize(buffer_size
);
50 fEngineControl
->UpdateTimeOut();
53 // Redirected on slaves drivers...
54 return JackDriver::SetBufferSize(buffer_size
);
57 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate
)
59 fEngineControl
->fSampleRate
= sample_rate
;
60 fEngineControl
->UpdateTimeOut();
62 // Redirected on slaves drivers...
63 return JackDriver::SetSampleRate(sample_rate
);
66 int JackAudioDriver::Open(jack_nframes_t buffer_size
,
67 jack_nframes_t samplerate
,
73 const char* capture_driver_name
,
74 const char* playback_driver_name
,
75 jack_nframes_t capture_latency
,
76 jack_nframes_t playback_latency
)
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
);
88 int JackAudioDriver::Open(bool capturing
,
93 const char* capture_driver_name
,
94 const char* playback_driver_name
,
95 jack_nframes_t capture_latency
,
96 jack_nframes_t playback_latency
)
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
);
108 void JackAudioDriver::UpdateLatencies()
110 jack_latency_range_t input_range
;
111 jack_latency_range_t output_range
;
112 jack_latency_range_t monitor_range
;
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
);
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
;
124 output_range
.max
= output_range
.min
+= fEngineControl
->fBufferSize
* 2;
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
);
134 int JackAudioDriver::Attach()
137 jack_port_id_t port_index
;
138 char name
[REAL_JACK_PORT_NAME_SIZE
];
139 char alias
[REAL_JACK_PORT_NAME_SIZE
];
142 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl
->fBufferSize
, fEngineControl
->fSampleRate
);
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
);
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
);
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
);
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
);
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
);
177 fMonitorPortList
[i
] = port_index
;
186 int JackAudioDriver::Detach()
189 jack_log("JackAudioDriver::Detach");
191 for (i
= 0; i
< fCaptureChannels
; i
++) {
192 fEngine
->PortUnRegister(fClientControl
.fRefNum
, fCapturePortList
[i
]);
195 for (i
= 0; i
< fPlaybackChannels
; i
++) {
196 fEngine
->PortUnRegister(fClientControl
.fRefNum
, fPlaybackPortList
[i
]);
197 if (fWithMonitorPorts
) {
198 fEngine
->PortUnRegister(fClientControl
.fRefNum
, fMonitorPortList
[i
]);
205 int JackAudioDriver::Write()
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
;
212 if (fWithMonitorPorts
&& fGraphManager
->GetConnectionsNum(fMonitorPortList
[i
]) > 0)
213 memcpy(GetMonitorBuffer(i
), buffer
, size
);
219 int JackAudioDriver::Process()
221 return (fEngineControl
->fSyncMode
) ? ProcessSync() : ProcessAsync();
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.
229 int JackAudioDriver::ProcessAsync()
231 // Read input buffers for the current cycle
233 jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
237 // Write output buffers from the previous cycle
239 jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
246 // Keep end cycle time
247 JackDriver::CycleTakeEndTime();
251 void JackAudioDriver::ProcessGraphAsync()
255 ProcessGraphAsyncMaster();
257 ProcessGraphAsyncSlave();
261 void JackAudioDriver::ProcessGraphAsyncMaster()
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");
268 if (ResumeRefNum() < 0) {
269 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
272 if (ProcessReadSlaves() < 0) {
273 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
276 if (ProcessWriteSlaves() < 0) {
277 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
280 // Does not wait on graph execution end
283 void JackAudioDriver::ProcessGraphAsyncSlave()
285 if (ResumeRefNum() < 0) {
286 jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
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.
295 int JackAudioDriver::ProcessSync()
297 // Read input buffers for the current cycle
299 jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
306 // Write output buffers from the current cycle
308 jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
312 // Keep end cycle time
313 JackDriver::CycleTakeEndTime();
317 void JackAudioDriver::ProcessGraphSync()
321 ProcessGraphSyncMaster();
323 ProcessGraphSyncSlave();
327 void JackAudioDriver::ProcessGraphSyncMaster()
329 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
330 if (fEngine
->Process(fBeginDateUst
, fEndDateUst
)) {
332 if (ResumeRefNum() < 0) {
333 jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
336 if (ProcessReadSlaves() < 0) {
337 jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
340 if (ProcessWriteSlaves() < 0) {
341 jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
344 // Waits for graph execution end
345 if (SuspendRefNum() < 0) {
346 jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
349 } else { // Graph not finished: do not activate it
350 jack_error("JackAudioDriver::ProcessGraphSync: Process error");
354 void JackAudioDriver::ProcessGraphSyncSlave()
356 if (ResumeRefNum() < 0) {
357 jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
361 jack_default_audio_sample_t
* JackAudioDriver::GetInputBuffer(int port_index
)
363 return fCapturePortList
[port_index
]
364 ? (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fCapturePortList
[port_index
], fEngineControl
->fBufferSize
)
368 jack_default_audio_sample_t
* JackAudioDriver::GetOutputBuffer(int port_index
)
370 return fPlaybackPortList
[port_index
]
371 ? (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fPlaybackPortList
[port_index
], fEngineControl
->fBufferSize
)
375 jack_default_audio_sample_t
* JackAudioDriver::GetMonitorBuffer(int port_index
)
377 return fPlaybackPortList
[port_index
]
378 ? (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fMonitorPortList
[port_index
], fEngineControl
->fBufferSize
)
382 int JackAudioDriver::ClientNotify(int refnum
, const char* name
, int notify
, int sync
, const char* message
, int value1
, int value2
)
386 case kLatencyCallback
:
387 HandleLatencyCallback(value1
);
391 JackDriver::ClientNotify(refnum
, name
, notify
, sync
, message
, value1
, value2
);
398 void JackAudioDriver::HandleLatencyCallback(int status
)
400 jack_latency_callback_mode_t mode
= (status
== 0) ? JackCaptureLatency
: JackPlaybackLatency
;
402 for (int i
= 0; i
< fCaptureChannels
; i
++) {
403 if (mode
== JackPlaybackLatency
) {
404 fGraphManager
->RecalculateLatency(fCapturePortList
[i
], mode
);
408 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
409 if (mode
== JackCaptureLatency
) {
410 fGraphManager
->RecalculateLatency(fPlaybackPortList
[i
], mode
);
415 } // end of namespace