| 1 |
/* |
|---|
| 2 |
Copyright (C) 2004-2008 Grame |
|---|
| 3 |
|
|---|
| 4 |
This program is free software; you can redistribute it and/or modify |
|---|
| 5 |
it under the terms of the GNU General Public License as published by |
|---|
| 6 |
the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 |
(at your option) any later version. |
|---|
| 8 |
|
|---|
| 9 |
This program is distributed in the hope that it will be useful, |
|---|
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 |
GNU General Public License for more details. |
|---|
| 13 |
|
|---|
| 14 |
You should have received a copy of the GNU General Public License |
|---|
| 15 |
along with this program; if not, write to the Free Software |
|---|
| 16 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 17 |
|
|---|
| 18 |
*/ |
|---|
| 19 |
|
|---|
| 20 |
#include <iostream> |
|---|
| 21 |
#include <fstream> |
|---|
| 22 |
#include <assert.h> |
|---|
| 23 |
|
|---|
| 24 |
#include "JackSystemDeps.h" |
|---|
| 25 |
#include "JackLockedEngine.h" |
|---|
| 26 |
#include "JackExternalClient.h" |
|---|
| 27 |
#include "JackInternalClient.h" |
|---|
| 28 |
#include "JackEngineControl.h" |
|---|
| 29 |
#include "JackClientControl.h" |
|---|
| 30 |
#include "JackServerGlobals.h" |
|---|
| 31 |
#include "JackGlobals.h" |
|---|
| 32 |
#include "JackChannel.h" |
|---|
| 33 |
#include "JackError.h" |
|---|
| 34 |
|
|---|
| 35 |
namespace Jack |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
JackEngine::JackEngine(JackGraphManager* manager, |
|---|
| 39 |
JackSynchro* table, |
|---|
| 40 |
JackEngineControl* control) |
|---|
| 41 |
{ |
|---|
| 42 |
fGraphManager = manager; |
|---|
| 43 |
fSynchroTable = table; |
|---|
| 44 |
fEngineControl = control; |
|---|
| 45 |
for (int i = 0; i < CLIENT_NUM; i++) |
|---|
| 46 |
fClientTable[i] = NULL; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
JackEngine::~JackEngine() |
|---|
| 50 |
{ |
|---|
| 51 |
jack_log("JackEngine::~JackEngine"); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
int JackEngine::Open() |
|---|
| 55 |
{ |
|---|
| 56 |
jack_log("JackEngine::Open"); |
|---|
| 57 |
|
|---|
| 58 |
// Open audio thread => request thread communication channel |
|---|
| 59 |
if (fChannel.Open(fEngineControl->fServerName) < 0) { |
|---|
| 60 |
jack_error("Cannot connect to server"); |
|---|
| 61 |
return -1; |
|---|
| 62 |
} else { |
|---|
| 63 |
return 0; |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
int JackEngine::Close() |
|---|
| 68 |
{ |
|---|
| 69 |
jack_log("JackEngine::Close"); |
|---|
| 70 |
fChannel.Close(); |
|---|
| 71 |
|
|---|
| 72 |
// Close remaining clients (RT is stopped) |
|---|
| 73 |
for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) { |
|---|
| 74 |
if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) { |
|---|
| 75 |
jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName); |
|---|
| 76 |
loadable_client->Close(); |
|---|
| 77 |
// Close does not delete the pointer for internal clients |
|---|
| 78 |
fClientTable[i] = NULL; |
|---|
| 79 |
delete loadable_client; |
|---|
| 80 |
} else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) { |
|---|
| 81 |
jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName); |
|---|
| 82 |
external_client->Close(); |
|---|
| 83 |
// Close deletes the pointer for external clients |
|---|
| 84 |
fClientTable[i] = NULL; |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
return 0; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
void JackEngine::NotifyQuit() |
|---|
| 92 |
{ |
|---|
| 93 |
fChannel.NotifyQuit(); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
//----------------------------- |
|---|
| 97 |
// Client ressource management |
|---|
| 98 |
//----------------------------- |
|---|
| 99 |
|
|---|
| 100 |
int JackEngine::AllocateRefnum() |
|---|
| 101 |
{ |
|---|
| 102 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 103 |
if (!fClientTable[i]) { |
|---|
| 104 |
jack_log("JackEngine::AllocateRefNum ref = %ld", i); |
|---|
| 105 |
return i; |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
return -1; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
void JackEngine::ReleaseRefnum(int ref) |
|---|
| 112 |
{ |
|---|
| 113 |
fClientTable[ref] = NULL; |
|---|
| 114 |
|
|---|
| 115 |
if (fEngineControl->fTemporary) { |
|---|
| 116 |
int i; |
|---|
| 117 |
for (i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) { |
|---|
| 118 |
if (fClientTable[i]) |
|---|
| 119 |
break; |
|---|
| 120 |
} |
|---|
| 121 |
if (i == CLIENT_NUM) { |
|---|
| 122 |
// last client and temporay case: quit the server |
|---|
| 123 |
jack_log("JackEngine::ReleaseRefnum server quit"); |
|---|
| 124 |
fEngineControl->fTemporary = false; |
|---|
| 125 |
throw JackTemporaryException(); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
//------------------ |
|---|
| 131 |
// Graph management |
|---|
| 132 |
//------------------ |
|---|
| 133 |
|
|---|
| 134 |
void JackEngine::ProcessNext(jack_time_t cur_cycle_begin) |
|---|
| 135 |
{ |
|---|
| 136 |
fLastSwitchUsecs = cur_cycle_begin; |
|---|
| 137 |
if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state |
|---|
| 138 |
fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0); |
|---|
| 139 |
fSignal.Signal(); // Signal for threads waiting for next cycle |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin) |
|---|
| 143 |
{ |
|---|
| 144 |
if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle |
|---|
| 145 |
CheckXRun(cur_cycle_begin); |
|---|
| 146 |
fGraphManager->RunCurrentGraph(); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end) |
|---|
| 150 |
{ |
|---|
| 151 |
bool res = true; |
|---|
| 152 |
|
|---|
| 153 |
// Cycle begin |
|---|
| 154 |
fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end); |
|---|
| 155 |
|
|---|
| 156 |
// Graph |
|---|
| 157 |
if (fGraphManager->IsFinishedGraph()) { |
|---|
| 158 |
ProcessNext(cur_cycle_begin); |
|---|
| 159 |
res = true; |
|---|
| 160 |
} else { |
|---|
| 161 |
jack_log("Process: graph not finished!"); |
|---|
| 162 |
if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) { |
|---|
| 163 |
jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs)); |
|---|
| 164 |
ProcessNext(cur_cycle_begin); |
|---|
| 165 |
res = true; |
|---|
| 166 |
} else { |
|---|
| 167 |
jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs)); |
|---|
| 168 |
ProcessCurrent(cur_cycle_begin); |
|---|
| 169 |
res = false; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
// Cycle end |
|---|
| 174 |
fEngineControl->CycleEnd(fClientTable); |
|---|
| 175 |
return res; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
/* |
|---|
| 179 |
Client that finish *after* the callback date are considered late even if their output buffers may have been |
|---|
| 180 |
correctly mixed in the time window: callbackUsecs <==> Read <==> Write. |
|---|
| 181 |
*/ |
|---|
| 182 |
|
|---|
| 183 |
void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin |
|---|
| 184 |
{ |
|---|
| 185 |
for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) { |
|---|
| 186 |
JackClientInterface* client = fClientTable[i]; |
|---|
| 187 |
if (client && client->GetClientControl()->fActive) { |
|---|
| 188 |
JackClientTiming* timing = fGraphManager->GetClientTiming(i); |
|---|
| 189 |
jack_client_state_t status = timing->fStatus; |
|---|
| 190 |
jack_time_t finished_date = timing->fFinishedAt; |
|---|
| 191 |
|
|---|
| 192 |
if (status != NotTriggered && status != Finished) { |
|---|
| 193 |
jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status); |
|---|
| 194 |
fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
if (status == Finished && (long)(finished_date - callback_usecs) > 0) { |
|---|
| 198 |
jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName); |
|---|
| 199 |
fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients |
|---|
| 200 |
} |
|---|
| 201 |
} |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
//--------------- |
|---|
| 206 |
// Notifications |
|---|
| 207 |
//--------------- |
|---|
| 208 |
|
|---|
| 209 |
void JackEngine::NotifyClient(int refnum, int event, int sync, const char* message, int value1, int value2) |
|---|
| 210 |
{ |
|---|
| 211 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 212 |
|
|---|
| 213 |
// The client may be notified by the RT thread while closing |
|---|
| 214 |
if (client) { |
|---|
| 215 |
|
|---|
| 216 |
if (client && client->GetClientControl()->fCallback[event]) { |
|---|
| 217 |
/* |
|---|
| 218 |
Important for internal clients : unlock before calling the notification callbacks. |
|---|
| 219 |
*/ |
|---|
| 220 |
bool res = fMutex.Unlock(); |
|---|
| 221 |
if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0) |
|---|
| 222 |
jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2); |
|---|
| 223 |
if (res) |
|---|
| 224 |
fMutex.Lock(); |
|---|
| 225 |
|
|---|
| 226 |
} else { |
|---|
| 227 |
jack_log("JackEngine::NotifyClient: no callback for event = %ld", event); |
|---|
| 228 |
} |
|---|
| 229 |
} |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
void JackEngine::NotifyClients(int event, int sync, const char* message, int value1, int value2) |
|---|
| 233 |
{ |
|---|
| 234 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 235 |
NotifyClient(i, event, sync, message, value1, value2); |
|---|
| 236 |
} |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum) |
|---|
| 240 |
{ |
|---|
| 241 |
jack_log("JackEngine::NotifyAddClient: name = %s", name); |
|---|
| 242 |
// Notify existing clients of the new client and new client of existing clients. |
|---|
| 243 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 244 |
JackClientInterface* old_client = fClientTable[i]; |
|---|
| 245 |
if (old_client) { |
|---|
| 246 |
if (old_client->ClientNotify(refnum, name, kAddClient, true, "", 0, 0) < 0) { |
|---|
| 247 |
jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName); |
|---|
| 248 |
return -1; |
|---|
| 249 |
} |
|---|
| 250 |
if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, "", 0, 0) < 0) { |
|---|
| 251 |
jack_error("NotifyAddClient new_client fails name = %s", name); |
|---|
| 252 |
return -1; |
|---|
| 253 |
} |
|---|
| 254 |
} |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
return 0; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
void JackEngine::NotifyRemoveClient(const char* name, int refnum) |
|---|
| 261 |
{ |
|---|
| 262 |
// Notify existing clients (including the one beeing suppressed) of the removed client |
|---|
| 263 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 264 |
JackClientInterface* client = fClientTable[i]; |
|---|
| 265 |
if (client) { |
|---|
| 266 |
client->ClientNotify(refnum, name, kRemoveClient, true, "",0, 0); |
|---|
| 267 |
} |
|---|
| 268 |
} |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
// Coming from the driver |
|---|
| 272 |
void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs) |
|---|
| 273 |
{ |
|---|
| 274 |
// Use the audio thread => request thread communication channel |
|---|
| 275 |
fEngineControl->NotifyXRun(callback_usecs, delayed_usecs); |
|---|
| 276 |
fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
void JackEngine::NotifyXRun(int refnum) |
|---|
| 280 |
{ |
|---|
| 281 |
if (refnum == ALL_CLIENTS) { |
|---|
| 282 |
NotifyClients(kXRunCallback, false, "", 0, 0); |
|---|
| 283 |
} else { |
|---|
| 284 |
NotifyClient(refnum, kXRunCallback, false, "", 0, 0); |
|---|
| 285 |
} |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
void JackEngine::NotifyGraphReorder() |
|---|
| 289 |
{ |
|---|
| 290 |
NotifyClients(kGraphOrderCallback, false, "", 0, 0); |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size) |
|---|
| 294 |
{ |
|---|
| 295 |
NotifyClients(kBufferSizeCallback, true, "", buffer_size, 0); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
void JackEngine::NotifySampleRate(jack_nframes_t sample_rate) |
|---|
| 299 |
{ |
|---|
| 300 |
NotifyClients(kSampleRateCallback, true, "", sample_rate, 0); |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
void JackEngine::NotifyFailure(int code, const char* reason) |
|---|
| 304 |
{ |
|---|
| 305 |
NotifyClients(kShutDownCallback, false, reason, code, 0); |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
void JackEngine::NotifyFreewheel(bool onoff) |
|---|
| 309 |
{ |
|---|
| 310 |
if (onoff) { |
|---|
| 311 |
// Save RT state |
|---|
| 312 |
fEngineControl->fSavedRealTime = fEngineControl->fRealTime; |
|---|
| 313 |
fEngineControl->fRealTime = false; |
|---|
| 314 |
} else { |
|---|
| 315 |
// Restore RT state |
|---|
| 316 |
fEngineControl->fRealTime = fEngineControl->fSavedRealTime; |
|---|
| 317 |
fEngineControl->fSavedRealTime = false; |
|---|
| 318 |
} |
|---|
| 319 |
NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, "", 0, 0); |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff) |
|---|
| 323 |
{ |
|---|
| 324 |
NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, "", port_index, 0); |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
void JackEngine::NotifyPortRename(jack_port_id_t port, const char* old_name) |
|---|
| 328 |
{ |
|---|
| 329 |
NotifyClients(kPortRenameCallback, false, old_name, port, 0); |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff) |
|---|
| 333 |
{ |
|---|
| 334 |
NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, "", src, dst); |
|---|
| 335 |
} |
|---|
| 336 |
|
|---|
| 337 |
void JackEngine::NotifyActivate(int refnum) |
|---|
| 338 |
{ |
|---|
| 339 |
NotifyClient(refnum, kActivateClient, true, "", 0, 0); |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
//---------------------------- |
|---|
| 343 |
// Loadable client management |
|---|
| 344 |
//---------------------------- |
|---|
| 345 |
|
|---|
| 346 |
int JackEngine::GetInternalClientName(int refnum, char* name_res) |
|---|
| 347 |
{ |
|---|
| 348 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 349 |
strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE); |
|---|
| 350 |
return 0; |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref) |
|---|
| 354 |
{ |
|---|
| 355 |
// Clear status |
|---|
| 356 |
*status = 0; |
|---|
| 357 |
|
|---|
| 358 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 359 |
JackClientInterface* client = fClientTable[i]; |
|---|
| 360 |
if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) { |
|---|
| 361 |
jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i); |
|---|
| 362 |
*int_ref = i; |
|---|
| 363 |
return 0; |
|---|
| 364 |
} |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
*status |= (JackNoSuchClient | JackFailure); |
|---|
| 368 |
return -1; |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
int JackEngine::InternalClientUnload(int refnum, int* status) |
|---|
| 372 |
{ |
|---|
| 373 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 374 |
if (client) { |
|---|
| 375 |
int res = client->Close(); |
|---|
| 376 |
delete client; |
|---|
| 377 |
*status = 0; |
|---|
| 378 |
return res; |
|---|
| 379 |
} else { |
|---|
| 380 |
*status = (JackNoSuchClient | JackFailure); |
|---|
| 381 |
return -1; |
|---|
| 382 |
} |
|---|
| 383 |
} |
|---|
| 384 |
|
|---|
| 385 |
//------------------- |
|---|
| 386 |
// Client management |
|---|
| 387 |
//------------------- |
|---|
| 388 |
|
|---|
| 389 |
int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status) |
|---|
| 390 |
{ |
|---|
| 391 |
// Clear status |
|---|
| 392 |
*status = 0; |
|---|
| 393 |
strcpy(name_res, name); |
|---|
| 394 |
|
|---|
| 395 |
jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION); |
|---|
| 396 |
|
|---|
| 397 |
if (protocol != JACK_PROTOCOL_VERSION) { |
|---|
| 398 |
*status |= (JackFailure | JackVersionError); |
|---|
| 399 |
jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION); |
|---|
| 400 |
return -1; |
|---|
| 401 |
} |
|---|
| 402 |
|
|---|
| 403 |
if (ClientCheckName(name)) { |
|---|
| 404 |
|
|---|
| 405 |
*status |= JackNameNotUnique; |
|---|
| 406 |
|
|---|
| 407 |
if (options & JackUseExactName) { |
|---|
| 408 |
jack_error("cannot create new client; %s already exists", name); |
|---|
| 409 |
*status |= JackFailure; |
|---|
| 410 |
return -1; |
|---|
| 411 |
} |
|---|
| 412 |
|
|---|
| 413 |
if (GenerateUniqueName(name_res)) { |
|---|
| 414 |
*status |= JackFailure; |
|---|
| 415 |
return -1; |
|---|
| 416 |
} |
|---|
| 417 |
} |
|---|
| 418 |
|
|---|
| 419 |
return 0; |
|---|
| 420 |
} |
|---|
| 421 |
|
|---|
| 422 |
bool JackEngine::GenerateUniqueName(char* name) |
|---|
| 423 |
{ |
|---|
| 424 |
int tens, ones; |
|---|
| 425 |
int length = strlen(name); |
|---|
| 426 |
|
|---|
| 427 |
if (length > JACK_CLIENT_NAME_SIZE - 4) { |
|---|
| 428 |
jack_error("%s exists and is too long to make unique", name); |
|---|
| 429 |
return true; /* failure */ |
|---|
| 430 |
} |
|---|
| 431 |
|
|---|
| 432 |
/* generate a unique name by appending "-01".."-99" */ |
|---|
| 433 |
name[length++] = '-'; |
|---|
| 434 |
tens = length++; |
|---|
| 435 |
ones = length++; |
|---|
| 436 |
name[tens] = '0'; |
|---|
| 437 |
name[ones] = '1'; |
|---|
| 438 |
name[length] = '\0'; |
|---|
| 439 |
|
|---|
| 440 |
while (ClientCheckName(name)) { |
|---|
| 441 |
if (name[ones] == '9') { |
|---|
| 442 |
if (name[tens] == '9') { |
|---|
| 443 |
jack_error("client %s has 99 extra instances already", name); |
|---|
| 444 |
return true; /* give up */ |
|---|
| 445 |
} |
|---|
| 446 |
name[tens]++; |
|---|
| 447 |
name[ones] = '0'; |
|---|
| 448 |
} else { |
|---|
| 449 |
name[ones]++; |
|---|
| 450 |
} |
|---|
| 451 |
} |
|---|
| 452 |
return false; |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
bool JackEngine::ClientCheckName(const char* name) |
|---|
| 456 |
{ |
|---|
| 457 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 458 |
JackClientInterface* client = fClientTable[i]; |
|---|
| 459 |
if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) |
|---|
| 460 |
return true; |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
return false; |
|---|
| 464 |
} |
|---|
| 465 |
|
|---|
| 466 |
int JackEngine::GetClientPID(const char* name) |
|---|
| 467 |
{ |
|---|
| 468 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 469 |
JackClientInterface* client = fClientTable[i]; |
|---|
| 470 |
if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) |
|---|
| 471 |
return client->GetClientControl()->fPID; |
|---|
| 472 |
} |
|---|
| 473 |
|
|---|
| 474 |
return 0; |
|---|
| 475 |
} |
|---|
| 476 |
|
|---|
| 477 |
int JackEngine::GetClientRefNum(const char* name) |
|---|
| 478 |
{ |
|---|
| 479 |
for (int i = 0; i < CLIENT_NUM; i++) { |
|---|
| 480 |
JackClientInterface* client = fClientTable[i]; |
|---|
| 481 |
if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) |
|---|
| 482 |
return client->GetClientControl()->fRefNum; |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
return -1; |
|---|
| 486 |
} |
|---|
| 487 |
|
|---|
| 488 |
// Used for external clients |
|---|
| 489 |
int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager) |
|---|
| 490 |
{ |
|---|
| 491 |
jack_log("JackEngine::ClientExternalOpen: name = %s ", name); |
|---|
| 492 |
|
|---|
| 493 |
int refnum = AllocateRefnum(); |
|---|
| 494 |
if (refnum < 0) { |
|---|
| 495 |
jack_error("No more refnum available"); |
|---|
| 496 |
return -1; |
|---|
| 497 |
} |
|---|
| 498 |
|
|---|
| 499 |
JackExternalClient* client = new JackExternalClient(); |
|---|
| 500 |
|
|---|
| 501 |
if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) { |
|---|
| 502 |
jack_error("Cannot allocate synchro"); |
|---|
| 503 |
goto error; |
|---|
| 504 |
} |
|---|
| 505 |
|
|---|
| 506 |
if (client->Open(name, pid, refnum, shared_client) < 0) { |
|---|
| 507 |
jack_error("Cannot open client"); |
|---|
| 508 |
goto error; |
|---|
| 509 |
} |
|---|
| 510 |
|
|---|
| 511 |
if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) { |
|---|
| 512 |
// Failure if RT thread is not running (problem with the driver...) |
|---|
| 513 |
jack_error("Driver is not running"); |
|---|
| 514 |
goto error; |
|---|
| 515 |
} |
|---|
| 516 |
|
|---|
| 517 |
fClientTable[refnum] = client; |
|---|
| 518 |
|
|---|
| 519 |
if (NotifyAddClient(client, name, refnum) < 0) { |
|---|
| 520 |
jack_error("Cannot notify add client"); |
|---|
| 521 |
goto error; |
|---|
| 522 |
} |
|---|
| 523 |
|
|---|
| 524 |
fGraphManager->InitRefNum(refnum); |
|---|
| 525 |
fEngineControl->ResetRollingUsecs(); |
|---|
| 526 |
*shared_engine = fEngineControl->GetShmIndex(); |
|---|
| 527 |
*shared_graph_manager = fGraphManager->GetShmIndex(); |
|---|
| 528 |
*ref = refnum; |
|---|
| 529 |
return 0; |
|---|
| 530 |
|
|---|
| 531 |
error: |
|---|
| 532 |
// Cleanup... |
|---|
| 533 |
fSynchroTable[refnum].Destroy(); |
|---|
| 534 |
fClientTable[refnum] = 0; |
|---|
| 535 |
client->Close(); |
|---|
| 536 |
delete client; |
|---|
| 537 |
return -1; |
|---|
| 538 |
} |
|---|
| 539 |
|
|---|
| 540 |
// Used for server driver clients |
|---|
| 541 |
int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait) |
|---|
| 542 |
{ |
|---|
| 543 |
jack_log("JackEngine::ClientInternalOpen: name = %s", name); |
|---|
| 544 |
|
|---|
| 545 |
int refnum = AllocateRefnum(); |
|---|
| 546 |
if (refnum < 0) { |
|---|
| 547 |
jack_error("No more refnum available"); |
|---|
| 548 |
goto error; |
|---|
| 549 |
} |
|---|
| 550 |
|
|---|
| 551 |
if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) { |
|---|
| 552 |
jack_error("Cannot allocate synchro"); |
|---|
| 553 |
goto error; |
|---|
| 554 |
} |
|---|
| 555 |
|
|---|
| 556 |
if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) { |
|---|
| 557 |
// Failure if RT thread is not running (problem with the driver...) |
|---|
| 558 |
jack_error("Driver is not running"); |
|---|
| 559 |
goto error; |
|---|
| 560 |
} |
|---|
| 561 |
|
|---|
| 562 |
fClientTable[refnum] = client; |
|---|
| 563 |
|
|---|
| 564 |
if (NotifyAddClient(client, name, refnum) < 0) { |
|---|
| 565 |
jack_error("Cannot notify add client"); |
|---|
| 566 |
goto error; |
|---|
| 567 |
} |
|---|
| 568 |
|
|---|
| 569 |
fGraphManager->InitRefNum(refnum); |
|---|
| 570 |
fEngineControl->ResetRollingUsecs(); |
|---|
| 571 |
*shared_engine = fEngineControl; |
|---|
| 572 |
*shared_manager = fGraphManager; |
|---|
| 573 |
*ref = refnum; |
|---|
| 574 |
return 0; |
|---|
| 575 |
|
|---|
| 576 |
error: |
|---|
| 577 |
// Cleanup... |
|---|
| 578 |
fSynchroTable[refnum].Destroy(); |
|---|
| 579 |
fClientTable[refnum] = 0; |
|---|
| 580 |
return -1; |
|---|
| 581 |
} |
|---|
| 582 |
|
|---|
| 583 |
// Used for external clients |
|---|
| 584 |
int JackEngine::ClientExternalClose(int refnum) |
|---|
| 585 |
{ |
|---|
| 586 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 587 |
fEngineControl->fTransport.ResetTimebase(refnum); |
|---|
| 588 |
int res = ClientCloseAux(refnum, client, true); |
|---|
| 589 |
client->Close(); |
|---|
| 590 |
delete client; |
|---|
| 591 |
return res; |
|---|
| 592 |
} |
|---|
| 593 |
|
|---|
| 594 |
// Used for server internal clients or drivers when the RT thread is stopped |
|---|
| 595 |
int JackEngine::ClientInternalClose(int refnum, bool wait) |
|---|
| 596 |
{ |
|---|
| 597 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 598 |
return ClientCloseAux(refnum, client, wait); |
|---|
| 599 |
} |
|---|
| 600 |
|
|---|
| 601 |
int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait) |
|---|
| 602 |
{ |
|---|
| 603 |
jack_log("JackEngine::ClientCloseAux ref = %ld", refnum); |
|---|
| 604 |
|
|---|
| 605 |
// Unregister all ports ==> notifications are sent |
|---|
| 606 |
jack_int_t ports[PORT_NUM_FOR_CLIENT]; |
|---|
| 607 |
int i; |
|---|
| 608 |
|
|---|
| 609 |
fGraphManager->GetInputPorts(refnum, ports); |
|---|
| 610 |
for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) { |
|---|
| 611 |
PortUnRegister(refnum, ports[i]); |
|---|
| 612 |
} |
|---|
| 613 |
|
|---|
| 614 |
fGraphManager->GetOutputPorts(refnum, ports); |
|---|
| 615 |
for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) { |
|---|
| 616 |
PortUnRegister(refnum, ports[i]); |
|---|
| 617 |
} |
|---|
| 618 |
|
|---|
| 619 |
// Remove the client from the table |
|---|
| 620 |
ReleaseRefnum(refnum); |
|---|
| 621 |
|
|---|
| 622 |
// Remove all ports |
|---|
| 623 |
fGraphManager->RemoveAllPorts(refnum); |
|---|
| 624 |
|
|---|
| 625 |
// Wait until next cycle to be sure client is not used anymore |
|---|
| 626 |
if (wait) { |
|---|
| 627 |
if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure |
|---|
| 628 |
jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum); |
|---|
| 629 |
} |
|---|
| 630 |
} |
|---|
| 631 |
|
|---|
| 632 |
// Notify running clients |
|---|
| 633 |
NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum); |
|---|
| 634 |
|
|---|
| 635 |
// Cleanup... |
|---|
| 636 |
fSynchroTable[refnum].Destroy(); |
|---|
| 637 |
fEngineControl->ResetRollingUsecs(); |
|---|
| 638 |
return 0; |
|---|
| 639 |
} |
|---|
| 640 |
|
|---|
| 641 |
int JackEngine::ClientActivate(int refnum, bool is_real_time) |
|---|
| 642 |
{ |
|---|
| 643 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 644 |
jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName); |
|---|
| 645 |
|
|---|
| 646 |
if (is_real_time) |
|---|
| 647 |
fGraphManager->Activate(refnum); |
|---|
| 648 |
|
|---|
| 649 |
// Wait for graph state change to be effective |
|---|
| 650 |
if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) { |
|---|
| 651 |
jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName); |
|---|
| 652 |
return -1; |
|---|
| 653 |
} else { |
|---|
| 654 |
NotifyActivate(refnum); |
|---|
| 655 |
return 0; |
|---|
| 656 |
} |
|---|
| 657 |
} |
|---|
| 658 |
|
|---|
| 659 |
// May be called without client |
|---|
| 660 |
int JackEngine::ClientDeactivate(int refnum) |
|---|
| 661 |
{ |
|---|
| 662 |
JackClientInterface* client = fClientTable[refnum]; |
|---|
| 663 |
jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName); |
|---|
| 664 |
|
|---|
| 665 |
// Disconnect all ports ==> notifications are sent |
|---|
| 666 |
jack_int_t ports[PORT_NUM_FOR_CLIENT]; |
|---|
| 667 |
int i; |
|---|
| 668 |
|
|---|
| 669 |
fGraphManager->GetInputPorts(refnum, ports); |
|---|
| 670 |
for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) { |
|---|
| 671 |
PortDisconnect(refnum, ports[i], ALL_PORTS); |
|---|
| 672 |
} |
|---|
| 673 |
|
|---|
| 674 |
fGraphManager->GetOutputPorts(refnum, ports); |
|---|
| 675 |
for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) { |
|---|
| 676 |
PortDisconnect(refnum, ports[i], ALL_PORTS); |
|---|
| 677 |
} |
|---|
| 678 |
|
|---|
| 679 |
fGraphManager->Deactivate(refnum); |
|---|
| 680 |
fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients |
|---|
| 681 |
|
|---|
| 682 |
// Wait for graph state change to be effective |
|---|
| 683 |
if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) { |
|---|
| 684 |
jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName); |
|---|
| 685 |
return -1; |
|---|
| 686 |
} else { |
|---|
| 687 |
return 0; |
|---|
| 688 |
} |
|---|
| 689 |
} |
|---|
| 690 |
|
|---|
| 691 |
//----------------- |
|---|
| 692 |
// Port management |
|---|
| 693 |
//----------------- |
|---|
| 694 |
|
|---|
| 695 |
int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index) |
|---|
| 696 |
{ |
|---|
| 697 |
jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size); |
|---|
| 698 |
|
|---|
| 699 |
// Check if port name already exists |
|---|
| 700 |
if (fGraphManager->GetPort(name) != NO_PORT) { |
|---|
| 701 |
jack_error("port_name \"%s\" already exists", name); |
|---|
| 702 |
return -1; |
|---|
| 703 |
} |
|---|
| 704 |
|
|---|
| 705 |
*port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize); |
|---|
| 706 |
if (*port_index != NO_PORT) { |
|---|
| 707 |
NotifyPortRegistation(*port_index, true); |
|---|
| 708 |
return 0; |
|---|
| 709 |
} else { |
|---|
| 710 |
return -1; |
|---|
| 711 |
} |
|---|
| 712 |
} |
|---|
| 713 |
|
|---|
| 714 |
int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index) |
|---|
| 715 |
{ |
|---|
| 716 |
jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index); |
|---|
| 717 |
|
|---|
| 718 |
// Disconnect port ==> notification is sent |
|---|
| 719 |
PortDisconnect(refnum, port_index, ALL_PORTS); |
|---|
| 720 |
|
|---|
| 721 |
if (fGraphManager->ReleasePort(refnum, port_index) == 0) { |
|---|
| 722 |
NotifyPortRegistation(port_index, false); |
|---|
| 723 |
return 0; |
|---|
| 724 |
} else { |
|---|
| 725 |
return -1; |
|---|
| 726 |
} |
|---|
| 727 |
} |
|---|
| 728 |
|
|---|
| 729 |
int JackEngine::PortConnect(int refnum, const char* src, const char* dst) |
|---|
| 730 |
{ |
|---|
| 731 |
jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst); |
|---|
| 732 |
jack_port_id_t port_src, port_dst; |
|---|
| 733 |
|
|---|
| 734 |
return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0) |
|---|
| 735 |
? -1 |
|---|
| 736 |
: PortConnect(refnum, port_src, port_dst); |
|---|
| 737 |
} |
|---|
| 738 |
|
|---|
| 739 |
int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst) |
|---|
| 740 |
{ |
|---|
| 741 |
jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst); |
|---|
| 742 |
JackClientInterface* client; |
|---|
| 743 |
int ref; |
|---|
| 744 |
|
|---|
| 745 |
if (fGraphManager->CheckPorts(src, dst) < 0) |
|---|
| 746 |
return -1; |
|---|
| 747 |
|
|---|
| 748 |
ref = fGraphManager->GetOutputRefNum(src); |
|---|
| 749 |
assert(ref >= 0); |
|---|
| 750 |
client = fClientTable[ref]; |
|---|
| 751 |
assert(client); |
|---|
| 752 |
if (!client->GetClientControl()->fActive) { |
|---|
| 753 |
jack_error("Cannot connect ports owned by inactive clients:" |
|---|
| 754 |
" \"%s\" is not active", client->GetClientControl()->fName); |
|---|
| 755 |
return -1; |
|---|
| 756 |
} |
|---|
| 757 |
|
|---|
| 758 |
ref = fGraphManager->GetInputRefNum(dst); |
|---|
| 759 |
assert(ref >= 0); |
|---|
| 760 |
client = fClientTable[ref]; |
|---|
| 761 |
assert(client); |
|---|
| 762 |
if (!client->GetClientControl()->fActive) { |
|---|
| 763 |
jack_error("Cannot connect ports owned by inactive clients:" |
|---|
| 764 |
" \"%s\" is not active", client->GetClientControl()->fName); |
|---|
| 765 |
return -1; |
|---|
| 766 |
} |
|---|
| 767 |
|
|---|
| 768 |
int res = fGraphManager->Connect(src, dst); |
|---|
| 769 |
if (res == 0) |
|---|
| 770 |
NotifyPortConnect(src, dst, true); |
|---|
| 771 |
return res; |
|---|
| 772 |
} |
|---|
| 773 |
|
|---|
| 774 |
int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst) |
|---|
| 775 |
{ |
|---|
| 776 |
jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst); |
|---|
| 777 |
jack_port_id_t port_src, port_dst; |
|---|
| 778 |
|
|---|
| 779 |
return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0) |
|---|
| 780 |
? -1 |
|---|
| 781 |
: PortDisconnect(refnum, port_src, port_dst); |
|---|
| 782 |
} |
|---|
| 783 |
|
|---|
| 784 |
int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst) |
|---|
| 785 |
{ |
|---|
| 786 |
jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst); |
|---|
| 787 |
|
|---|
| 788 |
if (dst == ALL_PORTS) { |
|---|
| 789 |
|
|---|
| 790 |
jack_int_t connections[CONNECTION_NUM_FOR_PORT]; |
|---|
| 791 |
fGraphManager->GetConnections(src, connections); |
|---|
| 792 |
|
|---|
| 793 |
JackPort* port = fGraphManager->GetPort(src); |
|---|
| 794 |
int ret = 0; |
|---|
| 795 |
if (port->GetFlags() & JackPortIsOutput) { |
|---|
| 796 |
for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) { |
|---|
| 797 |
if (PortDisconnect(refnum, src, connections[i]) != 0) { |
|---|
| 798 |
ret = -1; |
|---|
| 799 |
} |
|---|
| 800 |
} |
|---|
| 801 |
} else { |
|---|
| 802 |
for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) { |
|---|
| 803 |
if (PortDisconnect(refnum, connections[i], src) != 0) { |
|---|
| 804 |
ret = -1; |
|---|
| 805 |
} |
|---|
| 806 |
} |
|---|
| 807 |
} |
|---|
| 808 |
|
|---|
| 809 |
return ret; |
|---|
| 810 |
} else if (fGraphManager->CheckPorts(src, dst) < 0) { |
|---|
| 811 |
return -1; |
|---|
| 812 |
} else if (fGraphManager->Disconnect(src, dst) == 0) { |
|---|
| 813 |
// Notifications |
|---|
| 814 |
NotifyPortConnect(src, dst, false); |
|---|
| 815 |
return 0; |
|---|
| 816 |
} else { |
|---|
| 817 |
return -1; |
|---|
| 818 |
} |
|---|
| 819 |
} |
|---|
| 820 |
|
|---|
| 821 |
int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name) |
|---|
| 822 |
{ |
|---|
| 823 |
char old_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; |
|---|
| 824 |
strcpy(old_name, fGraphManager->GetPort(port)->GetName()); |
|---|
| 825 |
fGraphManager->GetPort(port)->SetName(name); |
|---|
| 826 |
NotifyPortRename(port, old_name); |
|---|
| 827 |
return 0; |
|---|
| 828 |
} |
|---|
| 829 |
|
|---|
| 830 |
} // end of namespace |
|---|
| 831 |
|
|---|