Jack2 1.9.12

JackEngineControl.h
1 /*
2 Copyright (C) 2003 Paul Davis
3 Copyright (C) 2004-2008 Grame
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21 #ifndef __JackEngineControl__
22 #define __JackEngineControl__
23
24 #include "JackShmMem.h"
25 #include "JackFrameTimer.h"
26 #include "JackTransportEngine.h"
27 #include "JackConstants.h"
28 #include "types.h"
29 #include <stdio.h>
30
31 #ifdef JACK_MONITOR
32 #include "JackEngineProfiling.h"
33 #endif
34
35 namespace Jack
36 {
37
38 class JackClientInterface;
39 class JackGraphManager;
40
41 #define JACK_ENGINE_ROLLING_COUNT 32
42 #define JACK_ENGINE_ROLLING_INTERVAL 1024
43
48 PRE_PACKED_STRUCTURE
49 struct SERVER_EXPORT JackEngineControl : public JackShmMem
50 {
51 // Shared state
52 jack_nframes_t fBufferSize;
53 jack_nframes_t fSampleRate;
54 bool fSyncMode;
55 bool fTemporary;
56 jack_time_t fPeriodUsecs;
57 jack_time_t fTimeOutUsecs;
58 float fMaxDelayedUsecs;
59 float fXrunDelayedUsecs;
60 bool fTimeOut;
61 bool fRealTime;
62 bool fSavedRealTime; // RT state saved and restored during Freewheel mode
63 int fServerPriority;
64 int fClientPriority;
65 int fMaxClientPriority;
66 char fServerName[JACK_SERVER_NAME_SIZE+1];
67 JackTransportEngine fTransport;
68 jack_timer_type_t fClockSource;
69 int fDriverNum;
70 bool fVerbose;
71
72 // CPU Load
73 jack_time_t fPrevCycleTime;
74 jack_time_t fCurCycleTime;
75 jack_time_t fSpareUsecs;
76 jack_time_t fMaxUsecs;
77 jack_time_t fRollingClientUsecs[JACK_ENGINE_ROLLING_COUNT];
78 unsigned int fRollingClientUsecsCnt;
79 int fRollingClientUsecsIndex;
80 int fRollingInterval;
81 float fCPULoad;
82
83 // For OSX thread
84 UInt64 fPeriod;
85 UInt64 fComputation;
86 UInt64 fConstraint;
87
88 // Timer
89 JackFrameTimer fFrameTimer;
90
91 #ifdef JACK_MONITOR
92 JackEngineProfiling fProfiler;
93 #endif
94
95 JackEngineControl(bool sync, bool temporary, long timeout, bool rt, long priority, bool verbose, jack_timer_type_t clock, const char* server_name)
96 {
97 fBufferSize = 512;
98 fSampleRate = 48000;
99 fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize);
100 fSyncMode = sync;
101 fTemporary = temporary;
102 fTimeOut = (timeout > 0);
103 fTimeOutUsecs = timeout * 1000;
104 fRealTime = rt;
105 fSavedRealTime = false;
106 fServerPriority = priority;
107
108 #ifdef WIN32
109 fClientPriority = (rt) ? priority - 3 : 0;
110 #else
111 fClientPriority = (rt) ? priority - 5 : 0;
112 #endif
113 fMaxClientPriority = (rt) ? priority - 1 : 0;
114 fVerbose = verbose;
115 fPrevCycleTime = 0;
116 fCurCycleTime = 0;
117 fSpareUsecs = 0;
118 fMaxUsecs = 0;
119 ResetRollingUsecs();
120 strncpy(fServerName, server_name, sizeof(fServerName));
121 fCPULoad = 0.f;
122 fPeriod = 0;
123 fComputation = 0;
124 fConstraint = 0;
125 fMaxDelayedUsecs = 0.f;
126 fXrunDelayedUsecs = 0.f;
127 fClockSource = clock;
128 fDriverNum = 0;
129 }
130
131 ~JackEngineControl()
132 {}
133
134 void UpdateTimeOut()
135 {
136 fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize); // In microsec
137 if (!(fTimeOut && fTimeOutUsecs > 2 * fPeriodUsecs)) {
138 fTimeOutUsecs = 2 * fPeriodUsecs;
139 }
140 }
141
142 // Cycle
143 void CycleIncTime(jack_time_t callback_usecs)
144 {
145 // Timer
146 fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
147 }
148
149 void CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
150 {
151 fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
152 CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
153 #ifdef JACK_MONITOR
154 fProfiler.Profile(table, manager, fPeriodUsecs, cur_cycle_begin, prev_cycle_end);
155 #endif
156 }
157
158 void CycleEnd(JackClientInterface** table)
159 {
160 fTransport.CycleEnd(table, fSampleRate, fBufferSize);
161 }
162
163 // Timer
164 void InitFrameTime()
165 {
166 fFrameTimer.InitFrameTime();
167 }
168
169 void ResetFrameTime(jack_time_t callback_usecs)
170 {
171 fFrameTimer.ResetFrameTime(callback_usecs);
172 }
173
174 void ReadFrameTime(JackTimer* timer)
175 {
176 fFrameTimer.ReadFrameTime(timer);
177 }
178
179 // XRun
180 void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);
181 void ResetXRun()
182 {
183 fMaxDelayedUsecs = 0.f;
184 }
185
186 // Private
187 void CalcCPULoad(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end);
188 void ResetRollingUsecs();
189
190 } POST_PACKED_STRUCTURE;
191
192 } // end of namespace
193
194 #endif
Jack::JackTimer
A structure used for time management.
Definition: JackFrameTimer.h:36
Jack::JackTransportEngine
Definition: JackTransportEngine.h:93
Jack::JackGraphManager
Graph manager: contains the connection manager and the port array.
Definition: JackGraphManager.h:40
Jack::JackFrameTimer
A class using the JackAtomicState to manage jack time.
Definition: JackFrameTimer.h:79
Jack::JackClientInterface
Client interface.
Definition: JackClientInterface.h:35
Jack::JackEngineControl
Engine control in shared memory.
Definition: JackEngineControl.h:49
Jack::JackShmMem
The base class for shared memory management.
Definition: JackShmMem.h:125
Jack::JackEngineProfiling
Definition: JackEngineProfiling.h:109