JackClient.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 
00022 #include "JackClient.h"
00023 #include "JackGraphManager.h"
00024 #include "JackClientControl.h"
00025 #include "JackEngineControl.h"
00026 #include "JackGlobals.h"
00027 #include "JackChannel.h"
00028 #include "JackTransportEngine.h"
00029 #include "driver_interface.h"
00030 #include <math.h>
00031 #include <string>
00032 #include <algorithm>
00033 
00034 using namespace std;
00035 
00036 namespace Jack
00037 {
00038 
00039 JackClient::JackClient()
00040 {}
00041 
00042 JackClient::JackClient(JackSynchro** table)
00043 {
00044     fThread = JackGlobals::MakeThread(this);
00045     fSynchroTable = table;
00046     fProcess = NULL;
00047     fGraphOrder = NULL;
00048     fXrun = NULL;
00049     fShutdown = NULL;
00050     fInit = NULL;
00051     fBufferSize = NULL;
00052         fClientRegistration = NULL;
00053     fFreewheel = NULL;
00054     fPortRegistration = NULL;
00055     fSync = NULL;
00056     fProcessArg = NULL;
00057     fGraphOrderArg = NULL;
00058     fXrunArg = NULL;
00059     fShutdownArg = NULL;
00060     fInitArg = NULL;
00061     fBufferSizeArg = NULL;
00062     fFreewheelArg = NULL;
00063     fClientRegistrationArg = NULL;
00064     fPortRegistrationArg = NULL;
00065     fSyncArg = NULL;
00066     fConditionnal = 0; // Temporary??
00067 }
00068 
00069 JackClient::~JackClient()
00070 {
00071     delete fThread;
00072 }
00073 
00074 int JackClient::Close()
00075 {
00076     JackLog("JackClient::Close ref = %ld\n", GetClientControl()->fRefNum);
00077     Deactivate();
00078     int result = -1;
00079     fChannel->ClientClose(GetClientControl()->fRefNum, &result);
00080     fChannel->Stop();
00081     fChannel->Close();
00082     fSynchroTable[GetClientControl()->fRefNum]->Disconnect();
00083     return result;
00084 }
00085 
00086 bool JackClient::IsActive()
00087 {
00088     return (GetClientControl()) ? GetClientControl()->fActive : false;
00089 }
00090 
00091 pthread_t JackClient::GetThreadID()
00092 {
00093     return fThread->GetThreadID();
00094 }
00095 
00102 void JackClient::SetupDriverSync(bool freewheel)
00103 {
00104     if (!freewheel && !GetEngineControl()->fSyncMode) {
00105         JackLog("JackClient::SetupDriverSync driver sem in flush mode\n");
00106         fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
00107         fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
00108         fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
00109     } else {
00110         JackLog("JackClient::SetupDriverSync driver sem in normal mode\n");
00111         fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(false);
00112         fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(false);
00113         fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(false);
00114     }
00115 }
00116 
00121 int JackClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
00122 {
00123         return 0;
00124 }
00125 
00126 int JackClient::ClientNotify(int refnum, const char* name, int notify, int sync, int value)
00127 {
00128     int res = 0;
00129 
00130     // Done all time: redirected on subclass implementation JackLibClient and JackInternalClient
00131     switch (notify) {
00132 
00133         case kAddClient:
00134             res = ClientNotifyImp(refnum, name, notify, sync, value);
00135                         break;
00136                         
00137            case kRemoveClient:
00138             res = ClientNotifyImp(refnum, name, notify, sync, value);
00139                         break;
00140                         
00141                 case kActivateClient:
00142                         JackLog("JackClient::kActivateClient name = %s ref = %ld \n", name, refnum);
00143                         Init();
00144                         break;
00145         }
00146 
00147     /*
00148     The current semantic is that notifications can only be received when the client has been activated,
00149     although is this implementation, one could imagine calling notifications as soon as the client has be opened.
00150     */
00151     if (IsActive()) {
00152 
00153         switch (notify) {
00154                 
00155                         case kAddClient:
00156                                 JackLog("JackClient::kAddClient fName = %s name = %s\n", GetClientControl()->fName, name);
00157                                 if (fClientRegistration && strcmp(GetClientControl()->fName, name) != 0)        // Don't call the callback for the registering client itself
00158                                         fClientRegistration(name, 1, fClientRegistrationArg);
00159                                 break;
00160                         
00161                         case kRemoveClient:
00162                                 JackLog("JackClient::kRemoveClient fName = %s name = %s\n", GetClientControl()->fName, name);
00163                                 if (fClientRegistration && strcmp(GetClientControl()->fName, name) != 0)        // Don't call the callback for the registering client itself
00164                                         fClientRegistration(name, 0, fClientRegistrationArg);
00165                                 break;
00166 
00167             case kBufferSizeCallback:
00168                 JackLog("JackClient::kBufferSizeCallback buffer_size = %ld\n", value);
00169                 if (fBufferSize)
00170                     res = fBufferSize(value, fBufferSizeArg);
00171                 break;
00172 
00173             case kGraphOrderCallback:
00174                 JackLog("JackClient::kGraphOrderCallback\n");
00175                 if (fGraphOrder)
00176                     res = fGraphOrder(fGraphOrderArg);
00177                 break;
00178 
00179             case kStartFreewheelCallback:
00180                 JackLog("JackClient::kStartFreewheel\n");
00181                 SetupDriverSync(true);
00182                 fThread->DropRealTime();
00183                 if (fFreewheel)
00184                     fFreewheel(1, fFreewheelArg);
00185                 break;
00186 
00187             case kStopFreewheelCallback:
00188                 JackLog("JackClient::kStopFreewheel\n");
00189                 SetupDriverSync(false);
00190                 if (fFreewheel)
00191                     fFreewheel(0, fFreewheelArg);
00192                 fThread->AcquireRealTime();
00193                 break;
00194 
00195             case kPortRegistrationOnCallback:
00196                 JackLog("JackClient::kPortRegistrationOn port_index = %ld\n", value);
00197                 if (fPortRegistration)
00198                     fPortRegistration(value, 1, fPortRegistrationArg);
00199                 break;
00200 
00201             case kPortRegistrationOffCallback:
00202                 JackLog("JackClient::kPortRegistrationOff port_index = %ld \n", value);
00203                 if (fPortRegistration)
00204                     fPortRegistration(value, 0, fPortRegistrationArg);
00205                 break;
00206 
00207             case kXRunCallback:
00208                 JackLog("JackClient::kXRunCallback\n");
00209                 if (fXrun)
00210                     res = fXrun(fXrunArg);
00211                 break;
00212 
00213             case kZombifyClient:
00214                     JackLog("JackClient::kZombifyClient name = %s ref = %ld \n", name, refnum);
00215                 ShutDown();
00216                 break;
00217                 }
00218     }
00219 
00220     return res;
00221 }
00222 
00227 int JackClient::Activate()
00228 {
00229     JackLog("JackClient::Activate \n");
00230     if (IsActive())
00231         return 0;
00232 
00233 /* TODO : solve WIN32 thread Kill issue
00234 #ifdef WIN32
00235         // Done first so that the RT thread then access an allocated synchro
00236         if (!fSynchroTable[GetClientControl()->fRefNum]->Connect(GetClientControl()->fName)) {
00237         jack_error("Cannot ConnectSemaphore %s client", GetClientControl()->fName);
00238         return -1;
00239     }
00240 #endif
00241 */
00242 
00243         if (StartThread() < 0)
00244         return -1;
00245 
00246     int result = -1;
00247     fChannel->ClientActivate(GetClientControl()->fRefNum, &result);
00248     if (result < 0)
00249         return result;
00250 
00251     if (fSync != NULL)          /* If a SyncCallback is pending... */
00252         SetSyncCallback(fSync, fSyncArg);
00253 
00254     if (fTimebase != NULL)      /* If a TimebaseCallback is pending... */
00255         SetTimebaseCallback(fConditionnal, fTimebase, fTimebaseArg);
00256 
00257     GetClientControl()->fActive = true;
00258     return 0;
00259 }
00260 
00264 int JackClient::Deactivate()
00265 {
00266     JackLog("JackClient::Deactivate \n");
00267     if (!IsActive())
00268         return 0;
00269 
00270     GetClientControl()->fActive = false;
00271     int result = -1;
00272     fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
00273 
00274     JackLog("JackClient::Deactivate res = %ld \n", result);
00275     // We need to wait for the new engine cycle before stopping the RT thread, but this is done by ClientDeactivate
00276 
00277 /* TODO : solve WIN32 thread Kill issue    
00278 #ifdef WIN32
00279         fSynchroTable[GetClientControl()->fRefNum]->Disconnect();
00280         fThread->Stop();
00281 #else
00282         fThread->Kill();
00283 #endif
00284 */
00285         fThread->Kill();
00286     return result;
00287 }
00288 
00289 //----------------------
00290 // RT thread management
00291 //----------------------
00292 
00293 bool JackClient::CallProcessCallback()
00294 {
00295     return (fProcess == NULL) ? true : (fProcess(GetEngineControl()->fBufferSize, fProcessArg) == 0);
00296 }
00297 
00301 bool JackClient::Init()
00302 {
00303     if (fInit) {
00304         JackLog("JackClient::Init calling client thread init callback\n");
00305                 fInit(fInitArg);
00306     }
00307     return true;
00308 }
00309 
00310 int JackClient::StartThread()
00311 {
00312     JackLog("JackClient::StartThread : period = %ld computation = %ld constraint = %ld\n",
00313             long(int64_t(GetEngineControl()->fPeriod) / 1000.0f),
00314             long(int64_t(GetEngineControl()->fComputation) / 1000.0f),
00315             long(int64_t(GetEngineControl()->fConstraint) / 1000.0f));
00316 
00317     // Will do "something" on OSX only...
00318     fThread->SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
00319 
00320     if (fThread->Start() < 0) {
00321         jack_error("Start thread error");
00322         return -1;
00323     }
00324 
00325     if (GetEngineControl()->fRealTime) {
00326         if (fThread->AcquireRealTime(GetEngineControl()->fPriority - 1) < 0) {
00327             jack_error("AcquireRealTime error");
00328         }
00329     }
00330 
00331     return 0;
00332 }
00333 
00337 bool JackClient::Execute()
00338 {
00339     // Suspend itself: wait on the input synchro
00340     if (GetGraphManager()->SuspendRefNum(GetClientControl(), fSynchroTable, 0x7FFFFFFF) < 0) {
00341         jack_error("SuspendRefNum error");
00342         goto error;
00343     }
00344 
00345     // Process call
00346     if (IsActive()) {
00347         CallSyncCallback();
00348         bool res = CallProcessCallback();  
00349         CallTimebaseCallback();
00350         if (!res)
00351             goto end;
00352     } else {
00353         JackLog("Process called for an inactive client\n");
00354         // Happens if client is still not activated (connected to the FW)
00355         // or still runs while being desactivated by the server
00356     }
00357 
00358     // Resume: signal output clients connected to the running client
00359     if (GetGraphManager()->ResumeRefNum(GetClientControl(), fSynchroTable) < 0) {
00360         jack_error("ResumeRefNum error");
00361     }
00362 
00363     return true;
00364 
00365 end:
00366     JackLog("JackClient::Execute end name = %s\n", GetClientControl()->fName);
00367 
00368     // Continue graph execution for this cycle
00369     if (GetGraphManager()->ResumeRefNum(GetClientControl(), fSynchroTable) < 0) {
00370         jack_error("ResumeRefNum error");
00371     }
00372 
00373     // Hum... not sure about this, the following "close" code is called in the RT thread...
00374     int result;
00375     fThread->DropRealTime();
00376         GetClientControl()->fActive = false;
00377     fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
00378     return false;
00379 
00380 error:
00381     jack_error("JackClient::Execute error name = %s", GetClientControl()->fName);
00382     // Hum... not sure about this, the following "close" code is called in the RT thread...
00383     fThread->DropRealTime();
00384     ShutDown();
00385     return false;
00386 }
00387 
00388 //-----------------
00389 // Port management
00390 //-----------------
00391 
00392 int JackClient::PortRegister(const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size)
00393 {
00394     // Check port name length
00395     string port_name_str = string(port_name);
00396     if (port_name_str.size() == 0) {
00397         jack_error("port_name is empty.");
00398         return 0; // Means failure here...
00399     }
00400 
00401     string name = string(GetClientControl()->fName) + string(":") + port_name_str;
00402     if (name.size() >= JACK_PORT_NAME_SIZE) {
00403         jack_error("\"%s:%s\" is too long to be used as a JACK port name.\n"
00404                    "Please use %lu characters or less.",
00405                    GetClientControl()->fName,
00406                    port_name,
00407                    JACK_PORT_NAME_SIZE - 1);
00408         return 0; // Means failure here...
00409     }
00410 
00411     // Check if port name already exists
00412     if (GetGraphManager()->GetPort(name.c_str()) != NO_PORT) {
00413         jack_error("port_name \"%s\" already exists.", port_name);
00414         return 0; // Means failure here...
00415     }
00416 
00417     JackLog("JackClient::PortRegister ref = %ld  name = %s \n", GetClientControl()->fRefNum, name.c_str());
00418 
00419     int result = -1;
00420     jack_port_id_t port_index = NO_PORT;
00421     fChannel->PortRegister(GetClientControl()->fRefNum, name.c_str(), flags, buffer_size, &port_index, &result);
00422     JackLog("JackClient::PortRegister port_index = %ld \n", port_index);
00423 
00424     if (result == 0) {
00425         fPortList.push_back(port_index);
00426         return port_index;
00427     } else {
00428         return 0;
00429     }
00430 }
00431 
00432 int JackClient::PortUnRegister(jack_port_id_t port_index)
00433 {
00434     JackLog("JackClient::PortUnRegister port_index = %ld\n", port_index);
00435         list<jack_port_id_t>::iterator it = find(fPortList.begin(), fPortList.end(), port_index);
00436 
00437     if (it != fPortList.end()) {
00438         fPortList.erase(it);
00439         int result = -1;
00440         fChannel->PortUnRegister(GetClientControl()->fRefNum, port_index, &result);
00441         return result;
00442     } else {
00443         jack_error("unregistering a port %ld that is not own by the client", port_index);
00444         return -1;
00445     }
00446 }
00447 
00448 int JackClient::PortConnect(const char* src, const char* dst)
00449 {
00450     JackLog("JackClient::Connect src = %s dst = %s\n", src, dst);
00451     int result = -1;
00452     fChannel->PortConnect(GetClientControl()->fRefNum, src, dst, &result);
00453     return result;
00454 }
00455 
00456 int JackClient::PortDisconnect(const char* src, const char* dst)
00457 {
00458     JackLog("JackClient::Disconnect src = %s dst = %s\n", src, dst);
00459     int result = -1;
00460     fChannel->PortDisconnect(GetClientControl()->fRefNum, src, dst, &result);
00461     return result;
00462 }
00463 
00464 int JackClient::PortConnect(jack_port_id_t src, jack_port_id_t dst)
00465 {
00466     JackLog("JackClient::PortConnect src = %ld dst = %ld\n", src, dst);
00467     int result = -1;
00468     fChannel->PortConnect(GetClientControl()->fRefNum, src, dst, &result);
00469     return result;
00470 }
00471 
00472 int JackClient::PortDisconnect(jack_port_id_t src)
00473 {
00474     JackLog("JackClient::PortDisconnect src = %ld\n", src);
00475     int result = -1;
00476     fChannel->PortDisconnect(GetClientControl()->fRefNum, src, ALL_PORTS, &result);
00477     return result;
00478 }
00479 
00480 int JackClient::PortIsMine(jack_port_id_t port_index)
00481 {
00482     JackPort* port = GetGraphManager()->GetPort(port_index);
00483     return GetClientControl()->fRefNum == port->GetRefNum();
00484 }
00485 
00486 //--------------------
00487 // Context management
00488 //--------------------
00489 
00490 int JackClient::SetBufferSize(jack_nframes_t buffer_size)
00491 {
00492     int result = -1;
00493     fChannel->SetBufferSize(buffer_size, &result);
00494     return result;
00495 }
00496 
00497 int JackClient::SetFreeWheel(int onoff)
00498 {
00499     int result = -1;
00500     fChannel->SetFreewheel(onoff, &result);
00501     return result;
00502 }
00503 
00504 /*
00505 ShutDown is called:
00506 - from the RT thread when Execute method fails
00507 - possibly from a "closed" notification channel
00508 (Not needed since the synch object used (Sema of Fifo will fails when server quits... see ShutDown))
00509 */
00510 
00511 void JackClient::ShutDown()
00512 {
00513     JackLog("ShutDown\n");
00514     if (fShutdown) {
00515         GetClientControl()->fActive = false;
00516         fShutdown(fShutdownArg);
00517         fShutdown = NULL;
00518     }
00519 }
00520 
00521 //----------------------
00522 // Transport management
00523 //----------------------
00524 
00525 int JackClient::ReleaseTimebase()
00526 {
00527     int result = -1;
00528     fChannel->ReleaseTimebase(GetClientControl()->fRefNum, &result);
00529     if (result == 0) {
00530         fTimebase = NULL;
00531         fTimebaseArg = NULL;
00532     }
00533     return result;
00534 }
00535 
00536 /* Call the server if the client is active, otherwise keeps the arguments */
00537 int JackClient::SetSyncCallback(JackSyncCallback sync_callback, void* arg)
00538 {
00539     if (IsActive())
00540         GetClientControl()->fTransportState = (sync_callback == NULL) ? JackTransportStopped : JackTransportSynching;
00541     fSync = sync_callback;
00542     fSyncArg = arg;
00543     return 0;
00544 }
00545 
00546 int JackClient::SetSyncTimeout(jack_time_t timeout)
00547 {
00548     GetEngineControl()->fTransport.SetSyncTimeout(timeout);
00549     return 0;
00550 }
00551 
00552 /* Call the server if the client is active, otherwise keeps the arguments */
00553 int JackClient::SetTimebaseCallback(int conditional, JackTimebaseCallback timebase_callback, void* arg)
00554 {
00555     if (IsActive()) {
00556         int result = -1;
00557         fChannel->SetTimebaseCallback(GetClientControl()->fRefNum, conditional, &result);
00558         JackLog("SetTimebaseCallback result = %ld\n", result);
00559         if (result == 0) {
00560             fTimebase = timebase_callback;
00561             fTimebaseArg = arg;
00562         } else {
00563             fTimebase = NULL;
00564             fTimebaseArg = NULL;
00565         }
00566         JackLog("SetTimebaseCallback OK result = %ld\n", result);
00567         return result;
00568     } else {
00569         fTimebase = timebase_callback;
00570         fTimebaseArg = arg;
00571         fConditionnal = conditional;
00572         return 0;
00573     }
00574 }
00575 
00576 // Must be RT safe
00577 int JackClient::RequestNewPos(jack_position_t* pos)
00578 {
00579     JackTransportEngine& transport = GetEngineControl()->fTransport;
00580     jack_position_t* request = transport.WriteNextStateStart(2);
00581     pos->unique_1 = pos->unique_2 = transport.GenerateUniqueID();
00582     JackTransportEngine::TransportCopyPosition(pos, request);
00583     JackLog("RequestNewPos pos = %ld\n", pos->frame);
00584     transport.WriteNextStateStop(2);
00585     return 0;
00586 }
00587 
00588 int JackClient::TransportLocate(jack_nframes_t frame)
00589 {
00590     jack_position_t pos;
00591     pos.frame = frame;
00592     pos.valid = (jack_position_bits_t)0;
00593     JackLog("TransportLocate pos = %ld\n", pos.frame);
00594     return RequestNewPos(&pos);
00595 }
00596 
00597 int JackClient::TransportReposition(jack_position_t* pos)
00598 {
00599     jack_position_t tmp = *pos;
00600     JackLog("TransportReposition pos = %ld\n", pos->frame);
00601     return (tmp.valid & ~JACK_POSITION_MASK) ? EINVAL : RequestNewPos(&tmp);
00602 }
00603 
00604 jack_transport_state_t JackClient::TransportQuery(jack_position_t* pos)
00605 {
00606     if (pos)
00607         GetEngineControl()->fTransport.ReadCurrentPos(pos);
00608     return GetEngineControl()->fTransport.GetState();
00609 }
00610 
00611 jack_nframes_t JackClient::GetCurrentTransportFrame()
00612 {
00613     jack_position_t pos;
00614     jack_transport_state_t state = TransportQuery(&pos);
00615 
00616     if (state == JackTransportRolling) {
00617         float usecs = GetMicroSeconds() - pos.usecs;
00618         jack_nframes_t elapsed = (jack_nframes_t)floor((((float) pos.frame_rate) / 1000000.0f) * usecs);
00619         return pos.frame + elapsed;
00620     } else {
00621         return pos.frame;
00622     }
00623 }
00624 
00625 // Must be RT safe: directly write in the transport shared mem
00626 void JackClient::TransportStart()
00627 {
00628     GetEngineControl()->fTransport.SetCommand(TransportCommandStart);
00629 }
00630 
00631 // Must be RT safe: directly write in the transport shared mem
00632 void JackClient::TransportStop()
00633 {
00634     GetEngineControl()->fTransport.SetCommand(TransportCommandStop);
00635 }
00636 
00637 // Never called concurently with the server
00638 // TODO check concurency with SetSyncCallback
00639 
00640 void JackClient::CallSyncCallback()
00641 {
00642     JackTransportEngine& transport = GetEngineControl()->fTransport;
00643     jack_position_t* cur_pos = transport.ReadCurrentState();
00644     jack_transport_state_t transport_state = transport.GetState();
00645 
00646     switch (transport_state) {
00647 
00648         case JackTransportStarting:  // Starting...
00649             if (fSync == NULL) {
00650                 GetClientControl()->fTransportState = JackTransportRolling;
00651             } else if (GetClientControl()->fTransportState == JackTransportStarting) {
00652                 if (fSync(transport_state, cur_pos, fSyncArg))
00653                     GetClientControl()->fTransportState = JackTransportRolling;
00654             }
00655             break;
00656 
00657         case JackTransportRolling:
00658             if (fSync != NULL && GetClientControl()->fTransportState == JackTransportStarting) { // Client still not ready
00659                 if (fSync(transport_state, cur_pos, fSyncArg))
00660                     GetClientControl()->fTransportState = JackTransportRolling;
00661             }
00662             break;
00663 
00664         case JackTransportSynching:
00665             // New pos when transport engine is stopped...
00666             if (fSync != NULL) {
00667                 fSync(JackTransportStopped, cur_pos, fSyncArg);
00668                 GetClientControl()->fTransportState = JackTransportStopped;
00669             }
00670             break;
00671 
00672         default:
00673             break;
00674     }
00675 }
00676 
00677 void JackClient::CallTimebaseCallback()
00678 {
00679     JackTransportEngine& transport = GetEngineControl()->fTransport;
00680 
00681     if (fTimebase != NULL && fTimebaseArg != NULL && GetClientControl()->fRefNum == transport.GetTimebaseMaster()) {
00682 
00683         jack_transport_state_t transport_state = transport.GetState();
00684         jack_position_t* cur_pos = transport.WriteNextStateStart(1);
00685 
00686         switch (transport_state) {
00687 
00688             case JackTransportRolling:
00689                 fTimebase(transport_state, GetEngineControl()->fBufferSize, cur_pos, false, fTimebaseArg);
00690                 break;
00691 
00692             case JackTransportSynching:
00693                 fTimebase(JackTransportStopped, GetEngineControl()->fBufferSize, cur_pos, true, fTimebaseArg);
00694                 break;
00695 
00696             default:
00697                 break;
00698         }
00699 
00700         transport.WriteNextStateStop(1);
00701     }
00702 }
00703 
00704 //---------------------
00705 // Callback management
00706 //---------------------
00707 
00708 void JackClient::OnShutdown(JackShutdownCallback callback, void *arg)
00709 {
00710     if (IsActive()) {
00711         jack_error("You cannot set callbacks on an active client");
00712     } else {
00713         fShutdownArg = arg;
00714         fShutdown = callback;
00715     }
00716 }
00717 
00718 int JackClient::SetProcessCallback(JackProcessCallback callback, void *arg)
00719 {
00720     if (IsActive()) {
00721         jack_error("You cannot set callbacks on an active client");
00722         return -1;
00723     } else {
00724         fProcessArg = arg;
00725         fProcess = callback;
00726         return 0;
00727     }
00728 }
00729 
00730 int JackClient::SetXRunCallback(JackXRunCallback callback, void *arg)
00731 {
00732     if (IsActive()) {
00733         jack_error("You cannot set callbacks on an active client");
00734         return -1;
00735     } else {
00736                 GetClientControl()->fCallback[kXRunCallback] = (callback != NULL);
00737         fXrunArg = arg;
00738         fXrun = callback;
00739         return 0;
00740     }
00741 }
00742 
00743 int JackClient::SetInitCallback(JackThreadInitCallback callback, void *arg)
00744 {
00745     if (IsActive()) {
00746         jack_error("You cannot set callbacks on an active client");
00747         return -1;
00748     } else {
00749         fInitArg = arg;
00750         fInit = callback;
00751         return 0;
00752     }
00753 }
00754 
00755 int JackClient::SetGraphOrderCallback(JackGraphOrderCallback callback, void *arg)
00756 {
00757     JackLog("SetGraphOrderCallback \n");
00758 
00759     if (IsActive()) {
00760         jack_error("You cannot set callbacks on an active client");
00761         return -1;
00762     } else {
00763                 GetClientControl()->fCallback[kGraphOrderCallback] = (callback != NULL);
00764         fGraphOrder = callback;
00765         fGraphOrderArg = arg;
00766         return 0;
00767     }
00768 }
00769 
00770 int JackClient::SetBufferSizeCallback(JackBufferSizeCallback callback, void *arg)
00771 {
00772     if (IsActive()) {
00773         jack_error("You cannot set callbacks on an active client");
00774         return -1;
00775     } else {
00776                 GetClientControl()->fCallback[kBufferSizeCallback] = (callback != NULL);
00777         fBufferSizeArg = arg;
00778         fBufferSize = callback;
00779         return 0;
00780     }
00781 }
00782 
00783 int JackClient::SetClientRegistrationCallback(JackClientRegistrationCallback callback, void* arg)
00784 {
00785     if (IsActive()) {
00786         jack_error("You cannot set callbacks on an active client");
00787         return -1;
00788     } else {
00789                 // kAddClient and kRemoveClient notifications must be delivered by the server in any case
00790                 fClientRegistrationArg = arg;
00791         fClientRegistration = callback;
00792         return 0;
00793     }
00794 }
00795 
00796 int JackClient::SetFreewheelCallback(JackFreewheelCallback callback, void *arg)
00797 {
00798     if (IsActive()) {
00799         jack_error("You cannot set callbacks on an active client");
00800         return -1;
00801     } else {
00802                 GetClientControl()->fCallback[kStartFreewheelCallback] = (callback != NULL);
00803                 GetClientControl()->fCallback[kStopFreewheelCallback] = (callback != NULL);
00804         fFreewheelArg = arg;
00805         fFreewheel = callback;
00806         return 0;
00807     }
00808 }
00809 
00810 int JackClient::SetPortRegistrationCallback(JackPortRegistrationCallback callback, void *arg)
00811 {
00812     if (IsActive()) {
00813         jack_error("You cannot set callbacks on an active client");
00814         return -1;
00815     } else {
00816                 GetClientControl()->fCallback[kPortRegistrationOnCallback] = (callback != NULL);
00817                 GetClientControl()->fCallback[kPortRegistrationOffCallback] = (callback != NULL);
00818         fPortRegistrationArg = arg;
00819         fPortRegistration = callback;
00820         return 0;
00821     }
00822 }
00823 
00824 //------------------
00825 // Internal clients
00826 //------------------
00827 
00828 char* JackClient::GetInternalClientName(int ref)
00829 {
00830         char name_res[JACK_CLIENT_NAME_SIZE]; 
00831         int result = -1;
00832         fChannel->GetInternalClientName(GetClientControl()->fRefNum, ref, name_res, &result);
00833         
00834         if (result < 0) {
00835                 return NULL;
00836         } else {
00837                 char* name = (char*)malloc(strlen(name_res));
00838                 strcpy(name, name_res);
00839                 return name;
00840         }
00841 }
00842 
00843 int JackClient::InternalClientHandle(const char* client_name, jack_status_t* status)
00844 {
00845         int int_ref, result = -1;
00846         fChannel->InternalClientHandle(GetClientControl()->fRefNum, client_name, (int*)status, &int_ref, &result);
00847         return int_ref;
00848 }
00849 
00850 int JackClient::InternalClientLoad(const char* client_name, jack_options_t options, jack_status_t* status, jack_varargs_t* va)
00851 {
00852         if (strlen(client_name) >= JACK_CLIENT_NAME_SIZE) {
00853                 jack_error ("\"%s\" is too long for a JACK client name.\n"
00854                             "Please use %lu characters or less.",
00855                             client_name, JACK_CLIENT_NAME_SIZE);
00856                 return 0;
00857         }
00858 
00859         if (va->load_name && (strlen(va->load_name) >= PATH_MAX)) {
00860                 jack_error("\"%s\" is too long for a shared object name.\n"
00861                              "Please use %lu characters or less.",
00862                             va->load_name, PATH_MAX);
00863                 int my_status1 = *status | (JackFailure | JackInvalidOption);
00864                 *status = (jack_status_t)my_status1;
00865                 return 0;
00866         }
00867 
00868         if (va->load_init && (strlen(va->load_init) >= JACK_LOAD_INIT_LIMIT)) {
00869                 jack_error ("\"%s\" is too long for internal client init "
00870                             "string.\nPlease use %lu characters or less.",
00871                             va->load_init, JACK_LOAD_INIT_LIMIT);
00872                 int my_status1 = *status | (JackFailure | JackInvalidOption);
00873                 *status = (jack_status_t)my_status1;
00874                 return 0;
00875         }
00876         
00877         int int_ref, result = -1;
00878         fChannel->InternalClientLoad(GetClientControl()->fRefNum, client_name, va->load_name, va->load_init, options, (int*)status, &int_ref, &result);
00879         return int_ref;
00880 }
00881 
00882 void JackClient::InternalClientUnload(int ref, jack_status_t* status)
00883 {
00884         int result = -1;
00885         fChannel->InternalClientUnload(GetClientControl()->fRefNum, ref, (int*)status, &result);
00886 }
00887 
00888 
00889 } // end of namespace
00890 

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