alsa: fix the poll interval
[jack2.git] / common / JackAudioDriver.cpp
blobac686e3a372872b94c84d4e1ea2a83c05e3bcdb4
1 /*
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"
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>
32 using namespace std;
34 namespace Jack
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();
51 UpdateLatencies();
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,
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)
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 void JackAudioDriver::UpdateLatencies()
90 jack_latency_range_t input_range;
91 jack_latency_range_t output_range;
92 jack_latency_range_t monitor_range;
94 for (int i = 0; i < fCaptureChannels; i++) {
95 input_range.max = input_range.min = fEngineControl->fBufferSize + fCaptureLatency;
96 fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
99 for (int i = 0; i < fPlaybackChannels; i++) {
100 output_range.max = output_range.min = fPlaybackLatency;
101 if (fEngineControl->fSyncMode) {
102 output_range.max = output_range.min += fEngineControl->fBufferSize;
103 } else {
104 output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
106 fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
107 if (fWithMonitorPorts) {
108 monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
109 fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
114 int JackAudioDriver::Attach()
116 JackPort* port;
117 jack_port_id_t port_index;
118 char name[REAL_JACK_PORT_NAME_SIZE+1];
119 char alias[REAL_JACK_PORT_NAME_SIZE+1];
120 int i;
122 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
124 for (i = 0; i < fCaptureChannels; i++) {
125 snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
126 snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
127 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
128 jack_error("driver: cannot register port for %s", name);
129 return -1;
131 port = fGraphManager->GetPort(port_index);
132 port->SetAlias(alias);
133 fCapturePortList[i] = port_index;
134 jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
137 for (i = 0; i < fPlaybackChannels; i++) {
138 snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
139 snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
140 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
141 jack_error("driver: cannot register port for %s", name);
142 return -1;
144 port = fGraphManager->GetPort(port_index);
145 port->SetAlias(alias);
146 fPlaybackPortList[i] = port_index;
147 jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
149 // Monitor ports
150 if (fWithMonitorPorts) {
151 jack_log("Create monitor port");
152 snprintf(name, sizeof(name), "%s:monitor_%u", fClientControl.fName, i + 1);
153 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
154 jack_error("Cannot register monitor port for %s", name);
155 return -1;
156 } else {
157 fMonitorPortList[i] = port_index;
162 UpdateLatencies();
163 return 0;
166 int JackAudioDriver::Detach()
168 int i;
169 jack_log("JackAudioDriver::Detach");
171 for (i = 0; i < fCaptureChannels; i++) {
172 fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
175 for (i = 0; i < fPlaybackChannels; i++) {
176 fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
177 if (fWithMonitorPorts) {
178 fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
182 return 0;
185 int JackAudioDriver::Write()
187 for (int i = 0; i < fPlaybackChannels; i++) {
188 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
189 jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
190 int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
191 // Monitor ports
192 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
193 memcpy(GetMonitorBuffer(i), buffer, size);
197 return 0;
200 int JackAudioDriver::Process()
202 return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
206 The driver "asynchronous" mode: output buffers computed at the *previous cycle* are used, the server does not
207 synchronize to the end of client graph execution.
210 int JackAudioDriver::ProcessAsync()
212 // Read input buffers for the current cycle
213 if (Read() < 0) {
214 jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
215 return -1;
218 // Write output buffers from the previous cycle
219 if (Write() < 0) {
220 jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
221 return -1;
224 // Process graph
225 ProcessGraphAsync();
227 // Keep end cycle time
228 JackDriver::CycleTakeEndTime();
229 return 0;
232 void JackAudioDriver::ProcessGraphAsync()
234 // Process graph
235 if (fIsMaster) {
236 ProcessGraphAsyncMaster();
237 } else {
238 ProcessGraphAsyncSlave();
243 Used when the driver works in master mode.
246 void JackAudioDriver::ProcessGraphAsyncMaster()
248 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
249 if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
250 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
253 if (ResumeRefNum() < 0) {
254 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
257 if (ProcessReadSlaves() < 0) {
258 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
261 if (ProcessWriteSlaves() < 0) {
262 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
265 // Does not wait on graph execution end
269 Used when the driver works in slave mode.
272 void JackAudioDriver::ProcessGraphAsyncSlave()
274 if (ResumeRefNum() < 0) {
275 jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
280 The driver "synchronous" mode: the server does synchronize to the end of client graph execution,
281 if graph process succeed, output buffers computed at the *current cycle* are used.
284 int JackAudioDriver::ProcessSync()
286 // Read input buffers for the current cycle
287 if (Read() < 0) {
288 jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
289 return -1;
292 // Process graph
293 ProcessGraphSync();
295 // Write output buffers from the current cycle
296 if (Write() < 0) {
297 jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
298 return -1;
301 // Keep end cycle time
302 JackDriver::CycleTakeEndTime();
303 return 0;
306 void JackAudioDriver::ProcessGraphSync()
308 // Process graph
309 if (fIsMaster) {
310 ProcessGraphSyncMaster();
311 } else {
312 ProcessGraphSyncSlave();
317 Used when the driver works in master mode.
320 void JackAudioDriver::ProcessGraphSyncMaster()
322 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
323 if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
325 if (ResumeRefNum() < 0) {
326 jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
329 if (ProcessReadSlaves() < 0) {
330 jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
333 if (ProcessWriteSlaves() < 0) {
334 jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
337 // Waits for graph execution end
338 if (SuspendRefNum() < 0) {
339 jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
342 } else { // Graph not finished: do not activate it
343 jack_error("JackAudioDriver::ProcessGraphSync: Process error");
348 Used when the driver works in slave mode.
351 void JackAudioDriver::ProcessGraphSyncSlave()
353 if (ResumeRefNum() < 0) {
354 jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
358 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
360 return fCapturePortList[port_index]
361 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
362 : NULL;
365 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
367 return fPlaybackPortList[port_index]
368 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
369 : NULL;
372 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
374 return fMonitorPortList[port_index]
375 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
376 : NULL;
379 int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
381 switch (notify) {
383 case kLatencyCallback:
384 HandleLatencyCallback(value1);
385 break;
387 default:
388 JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
389 break;
392 return 0;
395 void JackAudioDriver::HandleLatencyCallback(int status)
397 jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
399 for (int i = 0; i < fCaptureChannels; i++) {
400 if (mode == JackPlaybackLatency) {
401 fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
405 for (int i = 0; i < fPlaybackChannels; i++) {
406 if (mode == JackCaptureLatency) {
407 fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
412 } // end of namespace