00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "JackSystemDeps.h"
00022 #include "JackAudioDriver.h"
00023 #include "JackTime.h"
00024 #include "JackError.h"
00025 #include "JackEngineControl.h"
00026 #include "JackPort.h"
00027 #include "JackGraphManager.h"
00028 #include "JackLockedEngine.h"
00029 #include "JackException.h"
00030 #include <assert.h>
00031
00032 using namespace std;
00033
00034 namespace Jack
00035 {
00036
00037 JackAudioDriver::JackAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
00038 : JackDriver(name, alias, engine, table)
00039 {}
00040
00041 JackAudioDriver::~JackAudioDriver()
00042 {}
00043
00044 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
00045 {
00046
00047 fEngineControl->fBufferSize = buffer_size;
00048 fGraphManager->SetBufferSize(buffer_size);
00049
00050 fEngineControl->UpdateTimeOut();
00051 UpdateLatencies();
00052
00053
00054 return JackDriver::SetBufferSize(buffer_size);
00055 }
00056
00057 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
00058 {
00059 fEngineControl->fSampleRate = sample_rate;
00060 fEngineControl->UpdateTimeOut();
00061
00062
00063 return JackDriver::SetSampleRate(sample_rate);
00064 }
00065
00066 int JackAudioDriver::Open(jack_nframes_t buffer_size,
00067 jack_nframes_t samplerate,
00068 bool capturing,
00069 bool playing,
00070 int inchannels,
00071 int outchannels,
00072 bool monitor,
00073 const char* capture_driver_name,
00074 const char* playback_driver_name,
00075 jack_nframes_t capture_latency,
00076 jack_nframes_t playback_latency)
00077 {
00078 fCaptureChannels = inchannels;
00079 fPlaybackChannels = outchannels;
00080 fWithMonitorPorts = monitor;
00081 memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
00082 memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
00083 memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
00084 return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels,
00085 monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
00086 }
00087
00088 void JackAudioDriver::UpdateLatencies()
00089 {
00090 jack_latency_range_t input_range;
00091 jack_latency_range_t output_range;
00092 jack_latency_range_t monitor_range;
00093
00094 for (int i = 0; i < fCaptureChannels; i++) {
00095 input_range.max = input_range.min = fEngineControl->fBufferSize + fCaptureLatency;
00096 fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
00097 }
00098
00099 for (int i = 0; i < fPlaybackChannels; i++) {
00100 output_range.max = output_range.min = fPlaybackLatency;
00101 if (fEngineControl->fSyncMode) {
00102 output_range.max = output_range.min += fEngineControl->fBufferSize;
00103 } else {
00104 output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
00105 }
00106 fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
00107 if (fWithMonitorPorts) {
00108 monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
00109 fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
00110 }
00111 }
00112 }
00113
00114 int JackAudioDriver::Attach()
00115 {
00116 JackPort* port;
00117 jack_port_id_t port_index;
00118 char name[REAL_JACK_PORT_NAME_SIZE];
00119 char alias[REAL_JACK_PORT_NAME_SIZE];
00120 int i;
00121
00122 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
00123
00124 for (i = 0; i < fCaptureChannels; i++) {
00125 snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
00126 snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
00127 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
00128 jack_error("driver: cannot register port for %s", name);
00129 return -1;
00130 }
00131 port = fGraphManager->GetPort(port_index);
00132 port->SetAlias(alias);
00133 fCapturePortList[i] = port_index;
00134 jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
00135 }
00136
00137 for (i = 0; i < fPlaybackChannels; i++) {
00138 snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
00139 snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
00140 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
00141 jack_error("driver: cannot register port for %s", name);
00142 return -1;
00143 }
00144 port = fGraphManager->GetPort(port_index);
00145 port->SetAlias(alias);
00146 fPlaybackPortList[i] = port_index;
00147 jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
00148
00149
00150 if (fWithMonitorPorts) {
00151 jack_log("Create monitor port");
00152 snprintf(name, sizeof(name), "%s:monitor_%u", fClientControl.fName, i + 1);
00153 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
00154 jack_error("Cannot register monitor port for %s", name);
00155 return -1;
00156 } else {
00157 fMonitorPortList[i] = port_index;
00158 }
00159 }
00160 }
00161
00162 UpdateLatencies();
00163 return 0;
00164 }
00165
00166 int JackAudioDriver::Detach()
00167 {
00168 int i;
00169 jack_log("JackAudioDriver::Detach");
00170
00171 for (i = 0; i < fCaptureChannels; i++) {
00172 fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
00173 }
00174
00175 for (i = 0; i < fPlaybackChannels; i++) {
00176 fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
00177 if (fWithMonitorPorts) {
00178 fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
00179 }
00180 }
00181
00182 return 0;
00183 }
00184
00185 int JackAudioDriver::Write()
00186 {
00187 for (int i = 0; i < fPlaybackChannels; i++) {
00188 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
00189 jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
00190 int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
00191
00192 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
00193 memcpy(GetMonitorBuffer(i), buffer, size);
00194 }
00195 }
00196 }
00197 return 0;
00198 }
00199
00200 int JackAudioDriver::Process()
00201 {
00202 return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
00203 }
00204
00205
00206
00207
00208
00209
00210 int JackAudioDriver::ProcessAsync()
00211 {
00212
00213 if (Read() < 0) {
00214 jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
00215 return -1;
00216 }
00217
00218
00219 if (Write() < 0) {
00220 jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
00221 return -1;
00222 }
00223
00224
00225 ProcessGraphAsync();
00226
00227
00228 JackDriver::CycleTakeEndTime();
00229 return 0;
00230 }
00231
00232 void JackAudioDriver::ProcessGraphAsync()
00233 {
00234
00235 if (fIsMaster) {
00236 ProcessGraphAsyncMaster();
00237 } else {
00238 ProcessGraphAsyncSlave();
00239 }
00240 }
00241
00242
00243
00244
00245
00246 void JackAudioDriver::ProcessGraphAsyncMaster()
00247 {
00248
00249 if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
00250 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
00251 }
00252
00253 if (ResumeRefNum() < 0) {
00254 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
00255 }
00256
00257 if (ProcessReadSlaves() < 0) {
00258 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
00259 }
00260
00261 if (ProcessWriteSlaves() < 0) {
00262 jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
00263 }
00264
00265
00266 }
00267
00268
00269
00270
00271
00272 void JackAudioDriver::ProcessGraphAsyncSlave()
00273 {
00274 if (ResumeRefNum() < 0) {
00275 jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
00276 }
00277 }
00278
00279
00280
00281
00282
00283
00284 int JackAudioDriver::ProcessSync()
00285 {
00286
00287 if (Read() < 0) {
00288 jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
00289 return -1;
00290 }
00291
00292
00293 ProcessGraphSync();
00294
00295
00296 if (Write() < 0) {
00297 jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
00298 return -1;
00299 }
00300
00301
00302 JackDriver::CycleTakeEndTime();
00303 return 0;
00304 }
00305
00306 void JackAudioDriver::ProcessGraphSync()
00307 {
00308
00309 if (fIsMaster) {
00310 ProcessGraphSyncMaster();
00311 } else {
00312 ProcessGraphSyncSlave();
00313 }
00314 }
00315
00316
00317
00318
00319
00320 void JackAudioDriver::ProcessGraphSyncMaster()
00321 {
00322
00323 if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
00324
00325 if (ResumeRefNum() < 0) {
00326 jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
00327 }
00328
00329 if (ProcessReadSlaves() < 0) {
00330 jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
00331 }
00332
00333 if (ProcessWriteSlaves() < 0) {
00334 jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
00335 }
00336
00337
00338 if (SuspendRefNum() < 0) {
00339 jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
00340 }
00341
00342 } else {
00343 jack_error("JackAudioDriver::ProcessGraphSync: Process error");
00344 }
00345 }
00346
00347
00348
00349
00350
00351 void JackAudioDriver::ProcessGraphSyncSlave()
00352 {
00353 if (ResumeRefNum() < 0) {
00354 jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
00355 }
00356 }
00357
00358 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
00359 {
00360 return fCapturePortList[port_index]
00361 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
00362 : NULL;
00363 }
00364
00365 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
00366 {
00367 return fPlaybackPortList[port_index]
00368 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
00369 : NULL;
00370 }
00371
00372 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
00373 {
00374 return fPlaybackPortList[port_index]
00375 ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
00376 : NULL;
00377 }
00378
00379 int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
00380 {
00381 switch (notify) {
00382
00383 case kLatencyCallback:
00384 HandleLatencyCallback(value1);
00385 break;
00386
00387 default:
00388 JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
00389 break;
00390 }
00391
00392 return 0;
00393 }
00394
00395 void JackAudioDriver::HandleLatencyCallback(int status)
00396 {
00397 jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
00398
00399 for (int i = 0; i < fCaptureChannels; i++) {
00400 if (mode == JackPlaybackLatency) {
00401 fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
00402 }
00403 }
00404
00405 for (int i = 0; i < fPlaybackChannels; i++) {
00406 if (mode == JackCaptureLatency) {
00407 fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
00408 }
00409 }
00410 }
00411
00412 }