Jack2  1.9.10
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 PRE_PACKED_STRUCTURE
00049 struct SERVER_EXPORT JackEngineControl : public JackShmMem
00050 {
00051     // Shared state
00052     jack_nframes_t fBufferSize;
00053     jack_nframes_t fSampleRate;
00054     bool fSyncMode;
00055     bool fTemporary;
00056     jack_time_t fPeriodUsecs;
00057     jack_time_t fTimeOutUsecs;
00058     float fMaxDelayedUsecs;
00059     float fXrunDelayedUsecs;
00060     bool fTimeOut;
00061     bool fRealTime;
00062     bool fSavedRealTime;  // RT state saved and restored during Freewheel mode
00063     int fServerPriority;
00064     int fClientPriority;
00065     int fMaxClientPriority;
00066     char fServerName[JACK_SERVER_NAME_SIZE];
00067     JackTransportEngine fTransport;
00068     jack_timer_type_t fClockSource;
00069     int fDriverNum;
00070     bool fVerbose;
00071 
00072     // CPU Load
00073     jack_time_t fPrevCycleTime;
00074     jack_time_t fCurCycleTime;
00075     jack_time_t fSpareUsecs;
00076     jack_time_t fMaxUsecs;
00077     jack_time_t fRollingClientUsecs[JACK_ENGINE_ROLLING_COUNT];
00078     unsigned int fRollingClientUsecsCnt;
00079     int fRollingClientUsecsIndex;
00080     int fRollingInterval;
00081     float fCPULoad;
00082 
00083     // For OSX thread
00084     UInt64 fPeriod;
00085     UInt64 fComputation;
00086     UInt64 fConstraint;
00087 
00088     // Timer
00089     JackFrameTimer fFrameTimer;
00090 
00091 #ifdef JACK_MONITOR
00092     JackEngineProfiling fProfiler;
00093 #endif
00094 
00095     JackEngineControl(bool sync, bool temporary, long timeout, bool rt, long priority, bool verbose, jack_timer_type_t clock, const char* server_name)
00096     {
00097         fBufferSize = 512;
00098         fSampleRate = 48000;
00099         fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize);
00100         fSyncMode = sync;
00101         fTemporary = temporary;
00102         fTimeOut = (timeout > 0);
00103         fTimeOutUsecs = timeout * 1000;
00104         fRealTime = rt;
00105         fSavedRealTime = false;
00106         fServerPriority = priority;
00107         fClientPriority = (rt) ? priority - 5 : 0;
00108         fMaxClientPriority = (rt) ? priority - 1 : 0;
00109         fVerbose = verbose;
00110         fPrevCycleTime = 0;
00111         fCurCycleTime = 0;
00112         fSpareUsecs = 0;
00113         fMaxUsecs = 0;
00114         ResetRollingUsecs();
00115         strncpy(fServerName, server_name, sizeof(fServerName));
00116         fCPULoad = 0.f;
00117         fPeriod = 0;
00118         fComputation = 0;
00119         fConstraint = 0;
00120         fMaxDelayedUsecs = 0.f;
00121         fXrunDelayedUsecs = 0.f;
00122         fClockSource = clock;
00123         fDriverNum = 0;
00124     }
00125 
00126     ~JackEngineControl()
00127     {}
00128     
00129     void UpdateTimeOut()
00130     {
00131         fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize); // In microsec
00132         if (!(fTimeOut && fTimeOutUsecs > 2 * fPeriodUsecs)) {
00133             fTimeOutUsecs = 2 * fPeriodUsecs;
00134         }
00135     }
00136 
00137     // Cycle
00138     void CycleIncTime(jack_time_t callback_usecs)
00139     {
00140         // Timer
00141         fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
00142     }
00143 
00144     void CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
00145     {
00146         fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
00147         CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
00148 #ifdef JACK_MONITOR
00149         fProfiler.Profile(table, manager, fPeriodUsecs, cur_cycle_begin, prev_cycle_end);
00150 #endif
00151     }
00152 
00153     void CycleEnd(JackClientInterface** table)
00154     {
00155         fTransport.CycleEnd(table, fSampleRate, fBufferSize);
00156     }
00157 
00158     // Timer
00159     void InitFrameTime()
00160     {
00161         fFrameTimer.InitFrameTime();
00162     }
00163 
00164     void ResetFrameTime(jack_time_t callback_usecs)
00165     {
00166         fFrameTimer.ResetFrameTime(callback_usecs);
00167     }
00168 
00169     void ReadFrameTime(JackTimer* timer)
00170     {
00171         fFrameTimer.ReadFrameTime(timer);
00172     }
00173 
00174     // XRun
00175     void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);
00176     void ResetXRun()
00177     {
00178         fMaxDelayedUsecs = 0.f;
00179     }
00180 
00181     // Private
00182     void CalcCPULoad(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end);
00183     void ResetRollingUsecs();
00184 
00185 } POST_PACKED_STRUCTURE;
00186 
00187 } // end of namespace
00188 
00189 #endif