Jack2  1.9.8
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_CONTROL_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         fPeriod = 0;
00117         fComputation = 0;
00118         fConstraint = 0;
00119         fMaxDelayedUsecs = 0.f;
00120         fXrunDelayedUsecs = 0.f;
00121         fClockSource = clock;
00122         fDriverNum = 0;
00123    }
00124 
00125     ~JackEngineControl()
00126     {}
00127 
00128     // Cycle
00129     void CycleIncTime(jack_time_t callback_usecs)
00130     {
00131         // Timer
00132         fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
00133     }
00134 
00135     void CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
00136     {
00137         fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
00138         CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
00139 #ifdef JACK_MONITOR
00140         fProfiler.Profile(table, manager, fPeriodUsecs, cur_cycle_begin, prev_cycle_end);
00141 #endif
00142     }
00143 
00144     void CycleEnd(JackClientInterface** table)
00145     {
00146         fTransport.CycleEnd(table, fSampleRate, fBufferSize);
00147     }
00148 
00149     // Timer
00150     void InitFrameTime()
00151     {
00152         fFrameTimer.InitFrameTime();
00153     }
00154 
00155     void ResetFrameTime(jack_time_t callback_usecs)
00156     {
00157         fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
00158     }
00159 
00160     void ReadFrameTime(JackTimer* timer)
00161     {
00162         fFrameTimer.ReadFrameTime(timer);
00163     }
00164 
00165     // XRun
00166     void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);
00167     void ResetXRun()
00168     {
00169         fMaxDelayedUsecs = 0.f;
00170     }
00171 
00172     // Private
00173     void CalcCPULoad(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end);
00174     void ResetRollingUsecs();
00175 
00176 } POST_PACKED_STRUCTURE;
00177 
00178 } // end of namespace
00179 
00180 #endif