JackServer.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 #ifdef WIN32 
00022 #pragma warning (disable : 4786)
00023 #endif
00024 
00025 #include "JackServer.h"
00026 #include "JackTime.h"
00027 #include "JackFreewheelDriver.h"
00028 #include "JackLoopbackDriver.h"
00029 #include "JackThreadedDriver.h"
00030 #include "JackGlobals.h"
00031 #include "JackEngine.h"
00032 #include "JackAudioDriver.h"
00033 #include "JackChannel.h"
00034 #include "JackClientControl.h"
00035 #include "JackEngineControl.h"
00036 #include "JackSyncInterface.h"
00037 #include "JackGraphManager.h"
00038 #include "JackInternalClient.h"
00039 
00040 #ifdef __APPLE_
00041 #include <CoreFoundation/CFNotificationCenter.h>
00042 #endif
00043 
00044 namespace Jack
00045 {
00046 
00047 JackServer* JackServer::fInstance = NULL;
00048 
00049 JackServer::JackServer(bool sync,  bool temporary, long timeout, bool rt, long priority, long loopback, bool verbose)
00050 {
00051     JackGlobals::InitServer();
00052     for (int i = 0; i < CLIENT_NUM; i++)
00053         fSynchroTable[i] = JackGlobals::MakeSynchro();
00054     fGraphManager = new JackGraphManager();
00055     fEngineControl = new JackEngineControl(sync, temporary, timeout, rt, priority, verbose);
00056     fEngine = new JackEngine(fGraphManager, fSynchroTable, fEngineControl);
00057     fFreewheelDriver = new JackThreadedDriver(new JackFreewheelDriver("freewheel", fEngine, fSynchroTable));
00058     fLoopbackDriver = new JackLoopbackDriver("loopback", fEngine, fSynchroTable);
00059     fChannel = JackGlobals::MakeServerChannel();
00060     fState = new JackConnectionManager();
00061         fFreewheel = false;
00062     fLoopback = loopback;
00063     fDriverInfo = NULL;
00064     fAudioDriver = NULL;
00065     fInstance = this; // Unique instance
00066         jack_verbose = verbose;
00067 }
00068 
00069 JackServer::~JackServer()
00070 {
00071     for (int i = 0; i < CLIENT_NUM; i++)
00072         delete fSynchroTable[i];
00073     delete fGraphManager;
00074     delete fAudioDriver;
00075     delete fFreewheelDriver;
00076     delete fLoopbackDriver;
00077     delete fEngine;
00078     delete fChannel;
00079     delete fEngineControl;
00080     delete fState;
00081     if (fDriverInfo) {
00082         UnloadDriverModule(fDriverInfo->handle);
00083         free(fDriverInfo);
00084     }
00085     JackGlobals::Destroy();
00086 }
00087 
00088 // TODO : better handling of intermediate failing cases...
00089 
00090 int JackServer::Open(jack_driver_desc_t* driver_desc, JSList* driver_params)
00091 {
00092     if (fChannel->Open(this) < 0) {
00093         jack_error("Server channel open error");
00094         return -1;
00095     }
00096 
00097     if (fEngine->Open() != 0) {
00098         jack_error("Cannot open engine");
00099         return -1;
00100     }
00101 
00102     if ((fDriverInfo = jack_load_driver(driver_desc)) == NULL) {
00103         return -1;
00104     }
00105 
00106     if ((fAudioDriver = fDriverInfo->initialize(fEngine, fSynchroTable, driver_params)) == NULL) {
00107         jack_error("Cannot initialize driver");
00108         return -1;
00109     }
00110 
00111     if (fFreewheelDriver->Open() != 0) { // before engine open
00112         jack_error("Cannot open driver");
00113         return -1;
00114     }
00115 
00116     // Before engine open
00117     if (fLoopbackDriver->Open(fEngineControl->fBufferSize, fEngineControl->fSampleRate, 1, 1, fLoopback, fLoopback, false, "loopback", "loopback", 0, 0) != 0) {
00118         jack_error("Cannot open driver");
00119         return -1;
00120     }
00121 
00122     if (fAudioDriver->Attach() != 0) {
00123         jack_error("Cannot attach audio driver");
00124         return -1;
00125     }
00126         
00127     if (fLoopback > 0 && fLoopbackDriver->Attach() != 0) {
00128         jack_error("Cannot attach loopback driver");
00129         return -1;
00130     }
00131 
00132     fFreewheelDriver->SetMaster(false);
00133     fAudioDriver->SetMaster(true);
00134     if (fLoopback > 0)
00135         fAudioDriver->AddSlave(fLoopbackDriver);
00136     fAudioDriver->AddSlave(fFreewheelDriver); // After ???
00137     InitTime();
00138 
00139 #ifdef __APPLE__
00140     // Send notification to be used in the Jack Router
00141     CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(),
00142                                          CFSTR("com.grame.jackserver.start"),
00143                                          CFSTR("com.grame.jackserver"),
00144                                          NULL,
00145                                          true);
00146 #endif
00147 
00148     return 0;
00149 }
00150 
00151 int JackServer::Close()
00152 {
00153     JackLog("JackServer::Close\n");
00154     fChannel->Close();
00155     fAudioDriver->Detach();
00156     if (fLoopback > 0)
00157         fLoopbackDriver->Detach();
00158     fAudioDriver->Close();
00159     fFreewheelDriver->Close();
00160     fLoopbackDriver->Close();
00161     fEngine->Close();
00162 
00163 #ifdef __APPLE__
00164     // Send notification to be used in the Jack Router
00165     CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(),
00166                                          CFSTR("com.grame.jackserver.stop"),
00167                                          CFSTR("com.grame.jackserver"),
00168                                          NULL,
00169                                          true);
00170 #endif
00171 
00172     return 0;
00173 }
00174 
00175 int JackServer::InternalClientLoad(const char* client_name, const char* so_name, const char* objet_data, int options, int* int_ref, int* status)
00176 {
00177         try {
00178                 // Clear status
00179                 *status = 0;
00180                 JackLoadableInternalClient* client = new JackLoadableInternalClient(fInstance, GetSynchroTable(), so_name, objet_data); 
00181                 assert(client);
00182                 int res = client->Open(client_name, (jack_options_t)options, (jack_status_t*)status);
00183                 if (res < 0) {
00184                         delete client;
00185                         *int_ref = 0;
00186                 } else {
00187                         *int_ref = client->GetClientControl()->fRefNum;
00188                 }
00189         } catch (...) {
00190                 int my_status1 = *status | JackFailure;
00191                 *status = (jack_status_t)my_status1;
00192                 *int_ref = 0;
00193         }
00194         
00195         return 0;
00196 }
00197 
00198 int JackServer::Start()
00199 {
00200     JackLog("JackServer::Start\n");
00201     fEngineControl->InitFrameTime();
00202     return fAudioDriver->Start();
00203 }
00204 
00205 int JackServer::Stop()
00206 {
00207     JackLog("JackServer::Stop\n");
00208     return fAudioDriver->Stop();
00209 }
00210 
00211 int JackServer::SetBufferSize(jack_nframes_t buffer_size)
00212 {
00213     JackLog("JackServer::SetBufferSize nframes = %ld\n", buffer_size);
00214         jack_nframes_t current_buffer_size = fEngineControl->fBufferSize;
00215 
00216     if (fAudioDriver->Stop() != 0) {
00217         jack_error("Cannot stop audio driver");
00218         return -1;
00219     }
00220 
00221     if (fAudioDriver->SetBufferSize(buffer_size) == 0) {
00222                 fFreewheelDriver->SetBufferSize(buffer_size);
00223                 fEngine->NotifyBufferSize(buffer_size);
00224                 fEngineControl->InitFrameTime();
00225                 return fAudioDriver->Start();
00226         } else { // Failure: try to restore current value
00227                 jack_error("Cannot SetBufferSize for audio driver, restore current value %ld", current_buffer_size);
00228                 fFreewheelDriver->SetBufferSize(current_buffer_size);
00229                 fEngineControl->InitFrameTime();
00230                 return fAudioDriver->Start();
00231         }
00232 }
00233 
00234 /*
00235 Freewheel mode is implemented by switching from the (audio + freewheel) driver to the freewheel driver only:
00236  
00237     - "global" connection state is saved
00238     - all audio driver ports are deconnected, thus there is no more dependancies with the audio driver
00239     - the freewheel driver will be synchronized with the end of graph execution : all clients are connected to the freewheel driver
00240     - the freewheel driver becomes the "master"
00241     
00242 Normal mode is restored with the connections state valid before freewheel mode was done. Thus one consider that 
00243 no graph state change can be done during freewheel mode.
00244 */
00245 
00246 int JackServer::SetFreewheel(bool onoff)
00247 {
00248     JackLog("JackServer::SetFreewheel state = %ld\n", onoff);
00249 
00250     if (fFreewheel) {
00251         if (onoff) {
00252             return -1;
00253         } else {
00254             fFreewheel = false;
00255             fFreewheelDriver->Stop();
00256             fGraphManager->Restore(fState);   // Restore previous connection state
00257             fEngine->NotifyFreewheel(onoff);
00258             fFreewheelDriver->SetMaster(false);
00259             fEngineControl->InitFrameTime();
00260             return fAudioDriver->Start();
00261         }
00262     } else {
00263         if (onoff) {
00264             fFreewheel = true;
00265             fAudioDriver->Stop();
00266             fGraphManager->Save(fState);     // Save connection state
00267             fGraphManager->DisconnectAllPorts(fAudioDriver->GetClientControl()->fRefNum);
00268             fEngine->NotifyFreewheel(onoff);
00269             fFreewheelDriver->SetMaster(true);
00270             return fFreewheelDriver->Start();
00271         } else {
00272             return -1;
00273         }
00274     }
00275 }
00276 
00277 // Coming from the RT thread or server channel
00278 void JackServer::Notify(int refnum, int notify, int value)
00279 {
00280     switch (notify) {
00281 
00282         case kGraphOrderCallback:
00283             fEngine->NotifyGraphReorder();
00284             break;
00285 
00286         case kXRunCallback:
00287             fEngine->NotifyXRun(refnum);
00288             break;
00289 
00290         case kZombifyClient:
00291             fEngine->ZombifyClient(refnum);
00292             break;
00293 
00294         case kDeadClient:
00295             JackLog("JackServer: kDeadClient ref = %ld\n", refnum);
00296                         if (fEngine->ClientDeactivate(refnum) < 0)
00297                                 jack_error("JackServer: DeadClient ref = %ld cannot be removed from the graph !!", refnum);
00298                         fEngine->ClientExternalClose(refnum);
00299                         break;
00300     }
00301 }
00302 
00303 JackEngine* JackServer::GetEngine()
00304 {
00305     return fEngine;
00306 }
00307 
00308 JackSynchro** JackServer::GetSynchroTable()
00309 {
00310     return fSynchroTable;
00311 }
00312 
00313 JackEngineControl* JackServer::GetEngineControl()
00314 {
00315     return fEngineControl;
00316 }
00317 
00318 JackGraphManager* JackServer::GetGraphManager()
00319 {
00320     return fGraphManager;
00321 }
00322 
00323 } // end of namespace
00324 

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