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)
47 fEngineControl->fBufferSize = buffer_size;
48 fGraphManager->SetBufferSize(buffer_size);
49 fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize);
50 if (!fEngineControl->fTimeOut) {
51 fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
57 return JackDriver::SetBufferSize(buffer_size);
60 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
62 fEngineControl->fSampleRate = sample_rate;
63 fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize);
64 if (!fEngineControl->fTimeOut) {
65 fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
68 return JackDriver::SetSampleRate(sample_rate);
71 int JackAudioDriver::Open(jack_nframes_t buffer_size,
72 jack_nframes_t samplerate,
78 const char* capture_driver_name,
79 const char* playback_driver_name,
80 jack_nframes_t capture_latency,
81 jack_nframes_t playback_latency)
83 fCaptureChannels = inchannels;
84 fPlaybackChannels = outchannels;
85 fWithMonitorPorts = monitor;
86 memset(fCapturePortList, 0,
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
87 memset(fPlaybackPortList, 0,
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
88 memset(fMonitorPortList, 0,
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
89 return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels,
90 monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
93 int JackAudioDriver::Open(
bool capturing,
98 const char* capture_driver_name,
99 const char* playback_driver_name,
100 jack_nframes_t capture_latency,
101 jack_nframes_t playback_latency)
103 fCaptureChannels = inchannels;
104 fPlaybackChannels = outchannels;
105 fWithMonitorPorts = monitor;
106 memset(fCapturePortList, 0,
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
107 memset(fPlaybackPortList, 0,
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
108 memset(fMonitorPortList, 0,
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
109 return JackDriver::Open(capturing, playing, inchannels, outchannels,
110 monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
113 void JackAudioDriver::UpdateLatencies()
119 for (
int i = 0; i < fCaptureChannels; i++) {
120 input_range.
max = input_range.
min = fEngineControl->fBufferSize + fCaptureLatency;
121 fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
124 for (
int i = 0; i < fPlaybackChannels; i++) {
125 output_range.
max = output_range.
min = fPlaybackLatency;
126 if (fEngineControl->fSyncMode) {
127 output_range.
max = output_range.
min += fEngineControl->fBufferSize;
129 output_range.
max = output_range.
min += fEngineControl->fBufferSize * 2;
131 fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
132 if (fWithMonitorPorts) {
133 monitor_range.
min = monitor_range.
max = fEngineControl->fBufferSize;
134 fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
139 int JackAudioDriver::Attach()
142 jack_port_id_t port_index;
143 char name[REAL_JACK_PORT_NAME_SIZE];
144 char alias[REAL_JACK_PORT_NAME_SIZE];
147 jack_log(
"JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
149 for (i = 0; i < fCaptureChannels; i++) {
150 snprintf(alias,
sizeof(alias),
"%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
151 snprintf(name,
sizeof(name),
"%s:capture_%d", fClientControl.fName, i + 1);
152 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
153 jack_error(
"driver: cannot register port for %s", name);
156 port = fGraphManager->GetPort(port_index);
157 port->SetAlias(alias);
158 fCapturePortList[i] = port_index;
159 jack_log(
"JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
162 for (i = 0; i < fPlaybackChannels; i++) {
163 snprintf(alias,
sizeof(alias),
"%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
164 snprintf(name,
sizeof(name),
"%s:playback_%d", fClientControl.fName, i + 1);
165 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
166 jack_error(
"driver: cannot register port for %s", name);
169 port = fGraphManager->GetPort(port_index);
170 port->SetAlias(alias);
171 fPlaybackPortList[i] = port_index;
172 jack_log(
"JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
175 if (fWithMonitorPorts) {
177 snprintf(name,
sizeof(name),
"%s:monitor_%u", fClientControl.fName, i + 1);
178 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
179 jack_error(
"Cannot register monitor port for %s", name);
182 fMonitorPortList[i] = port_index;
191 int JackAudioDriver::Detach()
194 jack_log(
"JackAudioDriver::Detach");
196 for (i = 0; i < fCaptureChannels; i++) {
197 fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
200 for (i = 0; i < fPlaybackChannels; i++) {
201 fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
202 if (fWithMonitorPorts) {
203 fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
210 int JackAudioDriver::Write()
212 for (
int i = 0; i < fPlaybackChannels; i++) {
213 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
214 jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
215 int size =
sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
217 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
218 memcpy(GetMonitorBuffer(i), buffer, size);
224 int JackAudioDriver::Process()
226 return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
234 int JackAudioDriver::ProcessAsync()
238 jack_error(
"JackAudioDriver::ProcessAsync: read error, stopping...");
244 jack_error(
"JackAudioDriver::ProcessAsync: write error, stopping...");
252 JackDriver::CycleTakeEndTime();
256 void JackAudioDriver::ProcessGraphAsync()
260 ProcessGraphAsyncMaster();
262 ProcessGraphAsyncSlave();
266 void JackAudioDriver::ProcessGraphAsyncMaster()
269 if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
270 jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: Process error");
273 if (ResumeRefNum() < 0) {
274 jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
277 if (ProcessReadSlaves() < 0) {
278 jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
281 if (ProcessWriteSlaves() < 0) {
282 jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
288 void JackAudioDriver::ProcessGraphAsyncSlave()
290 if (ResumeRefNum() < 0) {
291 jack_error(
"JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
300 int JackAudioDriver::ProcessSync()
304 jack_error(
"JackAudioDriver::ProcessSync: read error, stopping...");
313 jack_error(
"JackAudioDriver::ProcessSync: write error, stopping...");
318 JackDriver::CycleTakeEndTime();
322 void JackAudioDriver::ProcessGraphSync()
326 ProcessGraphSyncMaster();
328 ProcessGraphSyncSlave();
332 void JackAudioDriver::ProcessGraphSyncMaster()
335 if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
337 if (ResumeRefNum() < 0) {
338 jack_error(
"JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
341 if (ProcessReadSlaves() < 0) {
342 jack_error(
"JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
345 if (ProcessWriteSlaves() < 0) {
346 jack_error(
"JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
350 if (SuspendRefNum() < 0) {
351 jack_error(
"JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
355 jack_error(
"JackAudioDriver::ProcessGraphSync: Process error");
359 void JackAudioDriver::ProcessGraphSyncSlave()
361 if (ResumeRefNum() < 0) {
362 jack_error(
"JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
366 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(
int port_index)
368 return fCapturePortList[port_index]
369 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
373 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(
int port_index)
375 return fPlaybackPortList[port_index]
376 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
380 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(
int port_index)
382 return fPlaybackPortList[port_index]
383 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
387 int JackAudioDriver::ClientNotify(
int refnum,
const char* name,
int notify,
int sync,
const char* message,
int value1,
int value2)
391 case kLatencyCallback:
392 HandleLatencyCallback(value1);
396 JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
403 void JackAudioDriver::HandleLatencyCallback(
int status)
405 jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
407 for (
int i = 0; i < fCaptureChannels; i++) {
408 if (mode == JackPlaybackLatency) {
409 fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
413 for (
int i = 0; i < fPlaybackChannels; i++) {
414 if (mode == JackCaptureLatency) {
415 fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);