JackDriver.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 "JackDriver.h"
00026 #include "JackTime.h"
00027 #include "JackError.h"
00028 #include "JackPort.h"
00029 #include "JackGraphManager.h"
00030 #include "JackGlobals.h"
00031 #include "JackEngineControl.h"
00032 #include "JackClientControl.h"
00033 #include "JackEngine.h"
00034 #include <math.h>
00035 #include <assert.h>
00036 
00037 using namespace std;
00038 
00039 namespace Jack
00040 {
00041 
00042 JackDriver::JackDriver(const char* name, JackEngine* engine, JackSynchro** table)
00043 {
00044     assert(strlen(name) < JACK_CLIENT_NAME_SIZE);
00045     fSynchroTable = table;
00046     fClientControl = new JackClientControl(name);
00047     fEngine = engine;
00048     fGraphManager = NULL;
00049     fLastWaitUst = 0;
00050     fIsMaster = true;
00051 }
00052 
00053 JackDriver::JackDriver()
00054 {
00055     fSynchroTable = NULL;
00056     fClientControl = NULL;
00057     fEngine = NULL;
00058     fGraphManager = NULL;
00059     fLastWaitUst = 0;
00060     fIsMaster = true;
00061 }
00062 
00063 JackDriver::~JackDriver()
00064 {
00065     JackLog("~JackDriver\n");
00066     delete fClientControl;
00067 }
00068 
00069 int JackDriver::Open()
00070 {
00071     int refnum = -1;
00072 
00073         if (fEngine->ClientInternalOpen(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
00074         jack_error("Cannot allocate internal client for audio driver");
00075         return -1;
00076     }
00077 
00078     fClientControl->fRefNum = refnum;
00079     fClientControl->fActive = true;
00080     fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for sync
00081 
00082     /*
00083         In ASYNC mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
00084         The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
00085     */
00086     if (!fEngineControl->fSyncMode) {
00087         fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
00088         fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
00089         fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
00090     }
00091     return 0;
00092 }
00093 
00094 int JackDriver::Open(jack_nframes_t nframes,
00095                      jack_nframes_t samplerate,
00096                      int capturing,
00097                      int playing,
00098                      int inchannels,
00099                      int outchannels,
00100                      bool monitor,
00101                      const char* capture_driver_name,
00102                      const char* playback_driver_name,
00103                      jack_nframes_t capture_latency,
00104                      jack_nframes_t playback_latency)
00105 {
00106     JackLog("JackDriver::Open capture_driver_name = %s\n", capture_driver_name);
00107     JackLog("JackDriver::Open playback_driver_name = %s\n", playback_driver_name);
00108     int refnum = -1;
00109 
00110     if (fEngine->ClientInternalOpen(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
00111         jack_error("Cannot allocate internal client for audio driver");
00112         return -1;
00113     }
00114 
00115     fClientControl->fRefNum = refnum;
00116     fClientControl->fActive = true;
00117     fEngineControl->fBufferSize = nframes;
00118     fEngineControl->fSampleRate = samplerate;
00119     fCaptureLatency = capture_latency;
00120     fPlaybackLatency = playback_latency;
00121 
00122     assert(strlen(capture_driver_name) < JACK_CLIENT_NAME_SIZE);
00123     assert(strlen(playback_driver_name) < JACK_CLIENT_NAME_SIZE);
00124 
00125     strcpy(fCaptureDriverName, capture_driver_name);
00126     strcpy(fPlaybackDriverName, playback_driver_name);
00127 
00128     fEngineControl->fPeriodUsecs = (jack_time_t)floor((((float)nframes) / (float)samplerate) * 1000000.0f);
00129     if (fEngineControl->fTimeOutUsecs == 0)  /* usecs; if zero, use 2 period size. */
00130         fEngineControl->fTimeOutUsecs = (jack_time_t)(2.f * fEngineControl->fPeriodUsecs);
00131 
00132     fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for sync
00133 
00134     /*
00135         In ASYNC mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
00136         The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
00137     */
00138     if (!fEngineControl->fSyncMode) {
00139         fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
00140         fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
00141         fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
00142     }
00143     return 0;
00144 }
00145 
00146 int JackDriver::Close()
00147 {
00148     JackLog("JackDriver::Close\n");
00149     fGraphManager->DirectDisconnect(fClientControl->fRefNum, fClientControl->fRefNum); // Disconnect driver from itself for sync
00150     fClientControl->fActive = false;
00151     return fEngine->ClientInternalClose(fClientControl->fRefNum, false);
00152 }
00153 
00154 bool JackDriver::IsRealTime()
00155 {
00156     return fEngineControl->fRealTime;
00157 }
00158 
00159 JackClientControl* JackDriver::GetClientControl() const
00160 {
00161     return fClientControl;
00162 }
00163 
00164 void JackDriver::NotifyXRun(jack_time_t callback_usecs)
00165 {
00166     fEngine->NotifyXRun(callback_usecs);
00167 }
00168 
00169 void JackDriverClient::SetMaster(bool onoff)
00170 {
00171     fIsMaster = onoff;
00172 }
00173 
00174 bool JackDriverClient::GetMaster()
00175 {
00176     return fIsMaster;
00177 }
00178 
00179 void JackDriverClient::AddSlave(JackDriverInterface* slave)
00180 {
00181     fSlaveList.push_back(slave);
00182 }
00183 
00184 void JackDriverClient::RemoveSlave(JackDriverInterface* slave)
00185 {
00186     fSlaveList.remove(slave);
00187 }
00188 
00189 int JackDriverClient::ProcessSlaves()
00190 {
00191         int res = 0;
00192     list<JackDriverInterface*>::const_iterator it;
00193     for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
00194         JackDriverInterface* slave = *it;
00195         if (slave->Process() < 0)
00196                         res = -1;
00197     }
00198         return res;
00199 }
00200 
00201 } // end of namespace

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