Jack2 1.9.6

JackEngineControl.h

00001 /*
00002 Copyright (C) 2003 Paul Davis
00003 Copyright (C) 2004-2008 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU Lesser General Public License as published by
00007 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public License
00016 along with this program; if not, write to the Free Software 
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 
00019 */
00020 
00021 #ifndef __JackEngineControl__
00022 #define __JackEngineControl__
00023 
00024 #include "JackShmMem.h"
00025 #include "JackFrameTimer.h"
00026 #include "JackTransportEngine.h"
00027 #include "JackConstants.h"
00028 #include "types.h"
00029 #include <stdio.h>
00030 
00031 #ifdef JACK_MONITOR
00032 #include "JackEngineProfiling.h"
00033 #endif
00034 
00035 namespace Jack
00036 {
00037 
00038 class JackClientInterface;
00039 class JackGraphManager;
00040 
00041 #define JACK_ENGINE_ROLLING_COUNT 32
00042 #define JACK_ENGINE_ROLLING_INTERVAL 1024
00043 
00048 struct SERVER_EXPORT JackEngineControl : public JackShmMem
00049 {
00050     // Shared state
00051     jack_nframes_t fBufferSize;
00052     jack_nframes_t fSampleRate;
00053     bool fSyncMode;
00054     bool fTemporary;
00055     jack_time_t fPeriodUsecs;
00056     jack_time_t fTimeOutUsecs;
00057     float fMaxDelayedUsecs;
00058     float fXrunDelayedUsecs;
00059     bool fTimeOut;
00060     bool fRealTime;
00061     bool fSavedRealTime;  // RT state saved and restored during Freewheel mode
00062     int fServerPriority;
00063     int fClientPriority;
00064     int fMaxClientPriority;
00065     char fServerName[64];
00066     JackTransportEngine fTransport;
00067     jack_timer_type_t fClockSource;
00068     int fDriverNum;
00069     bool fVerbose;
00070 
00071     // CPU Load
00072     jack_time_t fPrevCycleTime;
00073     jack_time_t fCurCycleTime;
00074     jack_time_t fSpareUsecs;
00075     jack_time_t fMaxUsecs;
00076     jack_time_t fRollingClientUsecs[JACK_ENGINE_ROLLING_COUNT];
00077     int fRollingClientUsecsCnt;
00078     int fRollingClientUsecsIndex;
00079     int fRollingInterval;
00080     float fCPULoad;
00081 
00082     // For OSX thread
00083     UInt64 fPeriod;
00084     UInt64 fComputation;
00085     UInt64 fConstraint;
00086 
00087     // Timer
00088     JackFrameTimer fFrameTimer;
00089     
00090 #ifdef JACK_MONITOR
00091     JackEngineProfiling fProfiler;
00092 #endif   
00093 
00094     JackEngineControl(bool sync, bool temporary, long timeout, bool rt, long priority, bool verbose, jack_timer_type_t clock, const char* server_name)
00095     {
00096         fBufferSize = 512;
00097         fSampleRate = 48000;
00098         fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize);
00099         fSyncMode = sync;
00100         fTemporary = temporary;
00101         fTimeOut = (timeout > 0);
00102         fTimeOutUsecs = timeout * 1000;
00103         fRealTime = rt;
00104         fSavedRealTime = false;
00105         fServerPriority = priority;
00106         fClientPriority = (rt) ? priority - 5 : 0;
00107         fMaxClientPriority = (rt) ? priority - 1 : 0;
00108         fVerbose = verbose;
00109         fPrevCycleTime = 0;
00110         fCurCycleTime = 0;
00111         fSpareUsecs = 0;
00112         fMaxUsecs = 0;
00113         ResetRollingUsecs();
00114         strncpy(fServerName, server_name, sizeof(fServerName));
00115         fPeriod = 0;
00116         fComputation = 0;
00117         fConstraint = 0;
00118         fMaxDelayedUsecs = 0.f;
00119         fXrunDelayedUsecs = 0.f;
00120         fClockSource = clock;
00121         fDriverNum = 0;
00122    }
00123     
00124     ~JackEngineControl()
00125     {}
00126 
00127     // Cycle
00128     void CycleIncTime(jack_time_t callback_usecs)
00129     {
00130         // Timer
00131         fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
00132     }
00133 
00134     void CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
00135     {
00136         fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
00137         CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
00138 #ifdef JACK_MONITOR
00139         fProfiler.Profile(table, manager, fPeriodUsecs, cur_cycle_begin, prev_cycle_end);
00140 #endif
00141     }
00142 
00143     void CycleEnd(JackClientInterface** table)
00144     {
00145         fTransport.CycleEnd(table, fSampleRate, fBufferSize);
00146     }
00147 
00148     // Timer
00149     void InitFrameTime()
00150     {
00151         fFrameTimer.InitFrameTime();
00152     }
00153 
00154     void ResetFrameTime(jack_time_t callback_usecs)
00155     {
00156         fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
00157     }
00158 
00159     void ReadFrameTime(JackTimer* timer)
00160     {
00161         fFrameTimer.ReadFrameTime(timer);
00162     }
00163     
00164     // XRun
00165     void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);
00166     void ResetXRun()
00167     {
00168         fMaxDelayedUsecs = 0.f;
00169     }
00170 
00171     // Private
00172     void CalcCPULoad(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end);
00173     void ResetRollingUsecs();
00174 
00175 } POST_PACKED_STRUCTURE;
00176 
00177 } // end of namespace
00178 
00179 #endif