JackEngine.cpp

00001 /*
00002 Copyright (C) 2004-2006 Grame  
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include <iostream>
00021 #include <fstream>
00022 #include <assert.h>
00023 
00024 #ifndef WIN32
00025 #include <sys/types.h>
00026 #include <signal.h>
00027 #endif
00028 
00029 #include "JackEngine.h"
00030 #include "JackExternalClient.h"
00031 #include "JackInternalClient.h"
00032 #include "JackEngineControl.h"
00033 #include "JackClientControl.h"
00034 #include "JackEngineTiming.h"
00035 #include "JackGlobals.h"
00036 #include "JackChannel.h"
00037 #include "JackSyncInterface.h"
00038 
00039 namespace Jack
00040 {
00041 
00042 JackEngine::JackEngine(JackGraphManager* manager, 
00043                                                 JackSynchro** table, 
00044                                                 JackEngineControl* control)
00045 {
00046     fGraphManager = manager;
00047     fSynchroTable = table;
00048     fEngineControl = control;
00049         fChannel = JackGlobals::MakeServerNotifyChannel();
00050         fSignal = JackGlobals::MakeInterProcessSync();
00051     fEngineTiming = new JackEngineTiming(fClientTable, fGraphManager, fEngineControl);
00052     for (int i = 0; i < CLIENT_NUM; i++)
00053         fClientTable[i] = NULL;
00054     fEngineTiming->ClearTimeMeasures();
00055     fEngineTiming->ResetRollingUsecs();
00056 }
00057 
00058 JackEngine::~JackEngine()
00059 {
00060     delete fChannel;
00061     delete fEngineTiming;
00062         delete fSignal;
00063 }
00064 
00065 int JackEngine::Open()
00066 {
00067     JackLog("JackEngine::Open\n");
00068 
00069     // Open audio thread => request thread communication channel
00070     if (fChannel->Open() < 0) {
00071         jack_error("Cannot connect to server");
00072         return -1;
00073     } else {
00074         return 0;
00075     }
00076 }
00077 
00078 int JackEngine::Close()
00079 {
00080     JackLog("JackEngine::Close\n");
00081     fChannel->Close();
00082 
00083     // Close (possibly) remaining clients (RT is stopped)
00084     for (int i = 0; i < CLIENT_NUM; i++) {
00085         JackClientInterface* client = fClientTable[i];
00086         if (client) {
00087                 JackLog("JackEngine::Close remaining client %ld\n", i);
00088                         fClientTable[i] = NULL;
00089             client->Close();
00090             delete client;
00091         }
00092     }
00093         
00094         fSignal->Destroy();
00095     return 0;
00096 }
00097 
00098 //-----------------------------
00099 // Client ressource management
00100 //-----------------------------
00101 
00102 int JackEngine::AllocateRefnum()
00103 {
00104     for (int i = 0; i < CLIENT_NUM; i++) {
00105         if (!fClientTable[i]) {
00106             JackLog("JackEngine::AllocateRefNum ref = %ld\n", i);
00107             return i;
00108         }
00109     }
00110     return -1;
00111 }
00112 
00113 void JackEngine::ReleaseRefnum(int ref)
00114 {
00115         fClientTable[ref] = NULL;
00116         
00117         if (fEngineControl->fTemporary) {
00118                 int i;
00119                 for (i = REAL_REFNUM; i < CLIENT_NUM; i++) {
00120                         if (fClientTable[i]) 
00121                                 break;
00122                 }
00123                 if (i == CLIENT_NUM) {
00124                         // last client and temporay case: quit the server
00125                         JackLog("JackEngine::ReleaseRefnum server quit\n");
00126                         fEngineControl->fTemporary = false;
00127                 #ifndef WIN32
00128                         kill(getpid(), SIGINT);
00129                 #endif
00130                 }
00131         }
00132 }
00133 
00134 //------------------
00135 // Graph management
00136 //------------------
00137 
00138 void JackEngine::ProcessNext(jack_time_t callback_usecs)
00139 {
00140         fLastSwitchUsecs = callback_usecs;
00141         if (fGraphManager->RunNextGraph())      // True if the graph actually switched to a new state
00142                 fChannel->ClientNotify(ALL_CLIENTS, kGraphOrderCallback, 0);
00143         fSignal->SignalAll();                           // Signal for threads waiting for next cycle
00144 }
00145 
00146 void JackEngine::ProcessCurrent(jack_time_t callback_usecs)
00147 {
00148         if (callback_usecs < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failling cycle
00149                 CheckXRun(callback_usecs);
00150         fGraphManager->RunCurrentGraph();
00151 }
00152 
00153 bool JackEngine::Process(jack_time_t callback_usecs)
00154 {
00155         bool res = true;
00156         
00157     // Transport begin
00158         fEngineControl->CycleBegin(callback_usecs);
00159 
00160     // Timing
00161         fEngineControl->IncFrameTime(callback_usecs);
00162     fEngineTiming->UpdateTiming(callback_usecs);
00163 
00164     // Graph
00165     if (fGraphManager->IsFinishedGraph()) {
00166         ProcessNext(callback_usecs);
00167                 res = true;
00168     } else {
00169         JackLog("Process: graph not finished!\n");
00170                 if (callback_usecs > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
00171             JackLog("Process: switch to next state delta = %ld\n", long(callback_usecs - fLastSwitchUsecs));
00172                         //RemoveZombifiedClients(callback_usecs); TODO
00173             ProcessNext(callback_usecs);
00174                         res = true;
00175         } else {
00176             JackLog("Process: waiting to switch delta = %ld\n", long(callback_usecs - fLastSwitchUsecs));
00177             ProcessCurrent(callback_usecs);
00178                         res = false;
00179                 }
00180     }
00181 
00182     // Transport end
00183         fEngineControl->CycleEnd(fClientTable);
00184         return res;
00185 }
00186 
00187 
00188 /*
00189 Client that finish *after* the callback date are considered late even if their output buffers may have been
00190 correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
00191 */
00192 
00193 void JackEngine::CheckXRun(jack_time_t callback_usecs)  // REVOIR les conditions de fin
00194 {
00195     for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
00196         JackClientInterface* client = fClientTable[i];
00197         if (client && client->GetClientControl()->fActive) {
00198                         JackClientTiming* timing = fGraphManager->GetClientTiming(i);
00199                 jack_client_state_t status = timing->fStatus;
00200             jack_time_t finished_date = timing->fFinishedAt;
00201            
00202             if (status != NotTriggered && status != Finished) {
00203                 jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
00204                 //fChannel->ClientNotify(i, kXRunCallback, 0); // Notify the failing client
00205                 fChannel->ClientNotify(ALL_CLIENTS, kXRunCallback, 0);  // Notify all clients
00206             }
00207             if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
00208                 jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
00209                 //fChannel->ClientNotify(i, kXRunCallback, 0); // Notify the failing client
00210                 fChannel->ClientNotify(ALL_CLIENTS, kXRunCallback, 0);  // Notify all clients
00211             }
00212         }
00213     }
00214 }
00215 
00216 //---------------
00217 // Zombification
00218 //---------------
00219 
00220 bool JackEngine::IsZombie(JackClientInterface* client, jack_time_t current_time)
00221 {
00222         return ((current_time - fGraphManager->GetClientTiming(client->GetClientControl()->fRefNum)->fFinishedAt) > 2 * fEngineControl->fTimeOutUsecs);  // A VERIFIER
00223 }
00224 
00225 // TODO : check what happens with looped sub-graph....
00226 void JackEngine::GetZombifiedClients(bool zombi_clients[CLIENT_NUM], jack_time_t current_time)
00227 {
00228     for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
00229         JackClientInterface* client1 = fClientTable[i];
00230         if (client1 && IsZombie(client1, current_time)) {
00231             JackLog("JackEngine::GetZombifiedClients: %s\n", client1->GetClientControl()->fName);
00232             zombi_clients[i] = true; // Assume client is dead
00233             // If another dead client is connected to the scanned one, then the scanned one is not the first of the dead subgraph
00234             for (int j = REAL_REFNUM; j < CLIENT_NUM; j++) {
00235                 JackClientInterface* client2 = fClientTable[j];
00236                 if (client2 && IsZombie(client2, current_time) && fGraphManager->IsDirectConnection(j, i)) {
00237                     zombi_clients[i] = false;
00238                     break;
00239                 }
00240             }
00241         } else {
00242             zombi_clients[i] = false;
00243         }
00244     }
00245 }
00246 
00247 void JackEngine::RemoveZombifiedClients(jack_time_t current_time)
00248 {
00249     bool zombi_clients[CLIENT_NUM];
00250     GetZombifiedClients(zombi_clients, current_time);
00251 
00252     for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
00253         if (zombi_clients[i] && !fClientTable[i]->GetClientControl()->fZombie) {
00254             fClientTable[i]->GetClientControl()->fZombie = true;
00255             JackLog("RemoveZombifiedCients: name = %s\n", fClientTable[i]->GetClientControl()->fName);
00256             fGraphManager->DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, i);
00257             fGraphManager->DirectDisconnect(i, FREEWHEEL_DRIVER_REFNUM);
00258             fGraphManager->DisconnectAllPorts(i);
00259             fChannel->ClientNotify(i, kZombifyClient, 0); // Signal engine
00260         }
00261     }
00262 }
00263 
00264 void JackEngine::ZombifyClient(int refnum)
00265 {
00266     NotifyClient(refnum, kZombifyClient, false, 0);
00267 }
00268 
00269 //---------------
00270 // Notifications
00271 //---------------
00272 
00273 void JackEngine::NotifyClient(int refnum, int event, int sync, int value)
00274 {
00275     JackClientInterface* client = fClientTable[refnum];
00276         
00277     // The client may be notified by the RT thread while closing
00278     if (!client) {
00279                 JackLog("JackEngine::NotifyClient: client not available anymore\n");
00280         } else if (client->GetClientControl()->fCallback[event]) {
00281                 if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value) < 0)
00282                         jack_error("NotifyClient fails name = %s event = %ld = val = %ld", client->GetClientControl()->fName, event, value);
00283     } else {
00284         JackLog("JackEngine::NotifyClient: no callback for event = %ld\n", event);
00285     }
00286 }
00287 
00288 void JackEngine::NotifyClients(int event, int sync, int value)
00289 {
00290     for (int i = 0; i < CLIENT_NUM; i++) {
00291         JackClientInterface* client = fClientTable[i];
00292                 if (client) {
00293                         if (client->GetClientControl()->fCallback[event]) {
00294                                 if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value) < 0) 
00295                                         jack_error("NotifyClient fails name = %s event = %ld = val = %ld", client->GetClientControl()->fName, event, value);
00296                         } else {
00297                                 JackLog("JackEngine::NotifyClients: no callback for event = %ld\n", event);
00298                         }
00299                 }
00300     }
00301 }
00302 
00303 int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
00304 {
00305     // Notify existing clients of the new client and new client of existing clients.
00306     for (int i = 0; i < CLIENT_NUM; i++) {
00307         JackClientInterface* old_client = fClientTable[i];
00308         if (old_client) {
00309             if (old_client->ClientNotify(refnum, name, kAddClient, true, 0) < 0) {
00310                 jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
00311                                 return -1;
00312                         }
00313                         if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0) < 0) {
00314                 jack_error("NotifyAddClient new_client fails name = %s", name);
00315                                 return -1;
00316                         }
00317         }
00318     }
00319 
00320     return 0;
00321 }
00322 
00323 void JackEngine::NotifyRemoveClient(const char* name, int refnum)
00324 {
00325     // Notify existing clients (including the one beeing suppressed) of the removed client
00326     for (int i = 0; i < CLIENT_NUM; i++) {
00327         JackClientInterface* client = fClientTable[i];
00328         if (client) {
00329             client->ClientNotify(refnum, name, kRemoveClient, true, 0);
00330         }
00331     }
00332 }
00333 
00334 // Coming from the driver
00335 void JackEngine::NotifyXRun(jack_time_t callback_usecs)
00336 {
00337     // Use the audio thread => request thread communication channel
00338         fEngineControl->ResetFrameTime(callback_usecs);
00339     fChannel->ClientNotify(ALL_CLIENTS, kXRunCallback, 0);
00340 }
00341 
00342 void JackEngine::NotifyXRun(int refnum)
00343 {
00344     if (refnum == ALL_CLIENTS) {
00345         NotifyClients(kXRunCallback, false, 0);
00346     } else {
00347         NotifyClient(refnum, kXRunCallback, false, 0);
00348     }
00349 }
00350 
00351 void JackEngine::NotifyGraphReorder()
00352 {
00353     NotifyClients(kGraphOrderCallback, false, 0);
00354 }
00355 
00356 void JackEngine::NotifyBufferSize(jack_nframes_t nframes)
00357 {
00358     NotifyClients(kBufferSizeCallback, true, nframes);
00359 }
00360 
00361 void JackEngine::NotifyFreewheel(bool onoff)
00362 {
00363     fEngineControl->fRealTime = !onoff;
00364     NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0);
00365 }
00366 
00367 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
00368 {
00369     NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index);
00370 }
00371 
00372 void JackEngine::NotifyActivate(int refnum)
00373 {
00374         NotifyClient(refnum, kActivateClient, true, 0);
00375 }
00376 
00377 //----------------------------
00378 // Loadable client management
00379 //----------------------------
00380 
00381 int JackEngine::GetInternalClientName(int refnum, char* name_res)
00382 {
00383         assert(refnum >= 0 && refnum < CLIENT_NUM);
00384         JackClientInterface* client = fClientTable[refnum];
00385         if (client) {
00386                 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
00387                 return 0;
00388         } else {
00389                 return -1;
00390         }
00391 }
00392 
00393 int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
00394 {
00395         // Clear status
00396         *status = 0;
00397         
00398         for (int i = 0; i < CLIENT_NUM; i++) {
00399         JackClientInterface* client = fClientTable[i];
00400         if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
00401                         JackLog("InternalClientHandle found client name = %s ref = %ld\n",  client_name, i);
00402                         *int_ref = i;
00403                         return 0;
00404                 }
00405     }
00406         
00407         *status |= (JackNoSuchClient | JackFailure);
00408         return -1;
00409 }
00410 
00411 int JackEngine::InternalClientUnload(int refnum, int* status)
00412 {
00413         assert(refnum >= 0 && refnum < CLIENT_NUM);
00414         JackClientInterface* client = fClientTable[refnum];
00415         if (client) {
00416                 int res = client->Close();
00417                 delete client;
00418                 *status = 0;
00419                 return res;
00420         } else {
00421                 *status = (JackNoSuchClient | JackFailure);
00422         return -1;
00423     }
00424 }
00425 
00426 //-------------------
00427 // Client management
00428 //-------------------
00429 
00430 int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
00431 {
00432         // Clear status
00433         *status = 0;
00434         strcpy(name_res, name);
00435         
00436         if (protocol != JACK_PROTOCOL_VERSION) {
00437                 *status |= (JackFailure | JackVersionError);
00438                 jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
00439                 return -1;
00440         }
00441         
00442         if (ClientCheckName(name)) {
00443 
00444                 *status |= JackNameNotUnique;
00445 
00446                 if (options & JackUseExactName) {
00447                         jack_error("cannot create new client; %s already exists", name);
00448                         *status |= JackFailure;
00449                         return -1;
00450                 }
00451                 
00452                 if (GenerateUniqueName(name_res)) {
00453                         *status |= JackFailure;
00454                         return -1;
00455                 }
00456         }
00457 
00458         return 0;
00459 }
00460 
00461 bool JackEngine::GenerateUniqueName(char* name)
00462 {
00463         int tens, ones;
00464         int length = strlen(name);
00465 
00466         if (length > JACK_CLIENT_NAME_SIZE - 4) {
00467                 jack_error("%s exists and is too long to make unique", name);
00468                 return true;            /* failure */
00469         }
00470 
00471         /*  generate a unique name by appending "-01".."-99" */
00472         name[length++] = '-';
00473         tens = length++;
00474         ones = length++;
00475         name[tens] = '0';
00476         name[ones] = '1';
00477         name[length] = '\0';
00478         
00479         while (ClientCheckName(name)) {
00480                 if (name[ones] == '9') {
00481                         if (name[tens] == '9') {
00482                                 jack_error("client %s has 99 extra instances already", name);
00483                                 return true; /* give up */
00484                         }
00485                         name[tens]++;
00486                         name[ones] = '0';
00487                 } else {
00488                         name[ones]++;
00489                 }
00490         }
00491         return false;
00492 }
00493 
00494 bool JackEngine::ClientCheckName(const char* name)
00495 {
00496     for (int i = 0; i < CLIENT_NUM; i++) {
00497         JackClientInterface* client = fClientTable[i];
00498         if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
00499             return true;
00500     }
00501 
00502     return false;
00503 }
00504 
00505 // Used for external clients
00506 int JackEngine::ClientExternalOpen(const char* name, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
00507 {
00508     JackLog("JackEngine::ClientOpen: name = %s \n", name);
00509         
00510         int refnum = AllocateRefnum();
00511     if (refnum < 0) {
00512         jack_error("No more refnum available");
00513         return -1;
00514     }
00515         
00516         JackExternalClient* client = new JackExternalClient();
00517 
00518     if (!fSynchroTable[refnum]->Allocate(name, 0)) {
00519         jack_error("Cannot allocate synchro");
00520                 goto error;
00521     }
00522 
00523     if (client->Open(name, refnum, shared_client) < 0) {
00524         jack_error("Cannot open client");
00525         goto error;
00526     }
00527 
00528     if (!fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
00529         // Failure if RT thread is not running (problem with the driver...)
00530         jack_error("Driver is not running");
00531         goto error;
00532     }
00533 
00534     if (NotifyAddClient(client, name, refnum) < 0) {
00535         jack_error("Cannot notify add client");
00536         goto error;
00537     }
00538 
00539     fClientTable[refnum] = client;
00540         fGraphManager->InitRefNum(refnum);
00541     fEngineTiming->ResetRollingUsecs();
00542     *shared_engine = fEngineControl->GetShmIndex();
00543     *shared_graph_manager = fGraphManager->GetShmIndex();
00544     *ref = refnum;
00545     return 0;
00546 
00547 error:
00548     ClientCloseAux(refnum, client, false);
00549     client->Close();
00550         delete client;
00551     return -1;
00552 }
00553 
00554 // Used for server driver clients
00555 int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
00556 {
00557     JackLog("JackEngine::ClientInternalNew: name = %s\n", name);
00558         
00559         int refnum = AllocateRefnum();
00560         if (refnum < 0) {
00561         jack_error("No more refnum available");
00562         return -1;
00563     }
00564 
00565     if (!fSynchroTable[refnum]->Allocate(name, 0)) {
00566         jack_error("Cannot allocate synchro");
00567                 return -1;
00568     }
00569         
00570         if (wait && !fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
00571         // Failure if RT thread is not running (problem with the driver...)
00572         jack_error("Driver is not running");
00573                 return -1;
00574     }
00575 
00576     if (NotifyAddClient(client, name, refnum) < 0) {
00577         jack_error("Cannot notify add client");
00578                 return -1;
00579     }
00580 
00581     fClientTable[refnum] = client;
00582         fGraphManager->InitRefNum(refnum);
00583     fEngineTiming->ResetRollingUsecs();
00584     *shared_engine = fEngineControl;
00585     *shared_manager = fGraphManager;
00586     *ref = refnum;
00587     return 0;
00588 }
00589 
00590 // Used for externall clients
00591 int JackEngine::ClientExternalClose(int refnum)
00592 {
00593     JackClientInterface* client = fClientTable[refnum];
00594     if (client) {
00595         fEngineControl->fTransport.ResetTimebase(refnum);
00596         int res = ClientCloseAux(refnum, client, true);
00597         client->Close();
00598         delete client;
00599         return res;
00600     } else {
00601         return -1;
00602     }
00603 }
00604 
00605 // Used for server internal clients or drivers when the RT thread is stopped
00606 int JackEngine::ClientInternalClose(int refnum, bool wait)
00607 {
00608     JackClientInterface* client = fClientTable[refnum];
00609     return (client)     ? ClientCloseAux(refnum, client, wait) : -1;
00610 }
00611 
00612 int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
00613 {
00614     JackLog("JackEngine::ClientCloseAux ref = %ld name = %s\n", 
00615                         refnum, 
00616                         (client->GetClientControl()) ? client->GetClientControl()->fName : "No name");
00617 
00618     // Remove the client from the table
00619         ReleaseRefnum(refnum);
00620 
00621     // Remove ports
00622     fGraphManager->RemoveAllPorts(refnum);
00623 
00624     // Wait until next cycle to be sure client is not used anymore
00625     if (wait) {
00626         if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
00627             jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
00628         }
00629     }
00630 
00631     // Notify running clients
00632         if (client->GetClientControl())  // When called in error cases, client may not be completely allocated
00633                 NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
00634 
00635     // Cleanup...
00636     fSynchroTable[refnum]->Destroy();
00637     fEngineTiming->ResetRollingUsecs();
00638     return 0;
00639 }
00640 
00641 int JackEngine::ClientActivate(int refnum)
00642 {
00643     JackClientInterface* client = fClientTable[refnum];
00644         assert(fClientTable[refnum]);
00645         
00646         JackLog("JackEngine::ClientActivate ref = %ld name = %s\n", refnum, client->GetClientControl()->fName);
00647         fGraphManager->Activate(refnum);
00648         
00649         // Wait for graph state change to be effective
00650         if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
00651                 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
00652                 return -1;
00653         } else {
00654                 NotifyActivate(refnum);
00655                 return 0;
00656         }
00657 }
00658 
00659 // May be called without client
00660 int JackEngine::ClientDeactivate(int refnum)
00661 {
00662     JackClientInterface* client = fClientTable[refnum];
00663         if (client == NULL) 
00664             return -1;
00665 
00666         JackLog("JackEngine::ClientDeactivate ref = %ld name = %s\n", refnum, client->GetClientControl()->fName);       
00667         fGraphManager->Deactivate(refnum);
00668         fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
00669                 
00670         // Wait for graph state change to be effective
00671         if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
00672                 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
00673                 return -1;
00674         } else {
00675                 return 0;
00676         }
00677 }
00678 
00679 //-----------------
00680 // Port management
00681 //-----------------
00682 
00683 int JackEngine::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
00684 {
00685     JackLog("JackEngine::PortRegister ref = %ld name = %s flags = %d buffer_size = %d\n", refnum, name, flags, buffer_size);
00686     assert(fClientTable[refnum]);
00687 
00688     *port_index = fGraphManager->AllocatePort(refnum, name, (JackPortFlags)flags);
00689     if (*port_index != NO_PORT) {
00690         NotifyPortRegistation(*port_index, true);
00691         return 0;
00692     } else {
00693         return -1;
00694     }
00695 }
00696 
00697 int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
00698 {
00699     JackLog("JackEngine::PortUnRegister ref = %ld port_index = %ld\n", refnum, port_index);
00700     assert(fClientTable[refnum]);
00701 
00702     if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
00703         NotifyPortRegistation(port_index, false);
00704         return 0;
00705     } else {
00706         return -1;
00707     }
00708 }
00709 
00710 int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
00711 {
00712     JackLog("JackEngine::PortConnect src = %s dst = %s\n", src, dst);
00713     jack_port_id_t port_src, port_dst;
00714 
00715     return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
00716            ? -1
00717            : PortConnect(refnum, port_src, port_dst);
00718 }
00719 
00720 int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
00721 {
00722     JackLog("JackEngine::PortDisconnect src = %s dst = %s\n", src, dst);
00723     jack_port_id_t port_src, port_dst;
00724 
00725     return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
00726            ? -1
00727            : fGraphManager->Disconnect(port_src, port_dst);
00728 }
00729 
00730 int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
00731 {
00732     JackLog("JackEngine::PortConnect src = %d dst = %d\n", src, dst);
00733     JackClientInterface* client;
00734     int ref;
00735 
00736     if (fGraphManager->CheckPorts(src, dst) < 0)
00737         return -1;
00738 
00739     ref = fGraphManager->GetOutputRefNum(src);
00740     assert(ref >= 0);
00741     client = fClientTable[ref];
00742     assert(client);
00743     if (!client->GetClientControl()->fActive) {
00744         jack_error("Cannot connect ports owned by inactive clients:"
00745                    " \"%s\" is not active", client->GetClientControl()->fName);
00746         return -1;
00747     }
00748 
00749     ref = fGraphManager->GetInputRefNum(dst);
00750     assert(ref >= 0);
00751     client = fClientTable[ref];
00752     assert(client);
00753     if (!client->GetClientControl()->fActive) {
00754         jack_error("Cannot connect ports owned by inactive clients:"
00755                    " \"%s\" is not active", client->GetClientControl()->fName);
00756         return -1;
00757     }
00758 
00759     return fGraphManager->Connect(src, dst);
00760 }
00761 
00762 int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
00763 {
00764     JackLog("JackEngine::PortDisconnect src = %d dst = %d\n", src, dst);
00765 
00766     if (dst == ALL_PORTS) {
00767         return (fGraphManager->CheckPort(src) < 0)
00768                ? -1
00769                : fGraphManager->DisconnectAll(src);
00770     } else {
00771         return (fGraphManager->CheckPorts(src, dst) < 0)
00772                ? -1
00773                : fGraphManager->Disconnect(src, dst);
00774     }
00775 }
00776 
00777 //----------------------
00778 // Transport management
00779 //----------------------
00780 
00781 int JackEngine::ReleaseTimebase(int refnum)
00782 {
00783     return fEngineControl->fTransport.ResetTimebase(refnum);
00784 }
00785 
00786 int JackEngine::SetTimebaseCallback(int refnum, int conditional)
00787 {
00788     return fEngineControl->fTransport.SetTimebase(refnum, conditional);
00789 }
00790 
00791 } // end of namespace
00792 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1