JackEngineControl.h 4.5 KB
Newer Older
sletz's avatar
sletz committed
1
2
/*
Copyright (C) 2003 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5

This program is free software; you can redistribute it and/or modify
sletz's avatar
sletz committed
6
7
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
sletz's avatar
sletz committed
8
9
10
11
12
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
sletz's avatar
sletz committed
13
GNU Lesser General Public License for more details.
sletz's avatar
sletz committed
14

sletz's avatar
sletz committed
15
16
17
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
sletz's avatar
sletz committed
18
19
20
21
22
23
24
25
26

*/

#ifndef __JackEngineControl__
#define __JackEngineControl__

#include "JackShmMem.h"
#include "JackFrameTimer.h"
#include "JackTransportEngine.h"
sletz's avatar
sletz committed
27
#include "JackConstants.h"
sletz's avatar
sletz committed
28
#include "types.h"
sletz's avatar
sletz committed
29
#include <stdio.h>
sletz's avatar
sletz committed
30

sletz's avatar
sletz committed
31
32
33
34
#ifdef JACK_MONITOR
#include "JackEngineProfiling.h"
#endif

sletz's avatar
sletz committed
35
36
37
namespace Jack
{

38
39
40
41
42
43
class JackClientInterface;
class JackGraphManager;

#define JACK_ENGINE_ROLLING_COUNT 32
#define JACK_ENGINE_ROLLING_INTERVAL 1024

sletz's avatar
sletz committed
44
45
46
47
/*!
\brief Engine control in shared memory.
*/

48
struct SERVER_EXPORT JackEngineControl : public JackShmMem
sletz's avatar
sletz committed
49
{
sletz's avatar
sletz committed
50
    // Shared state
sletz's avatar
sletz committed
51
52
    jack_nframes_t fBufferSize;
    jack_nframes_t fSampleRate;
sletz's avatar
sletz committed
53
54
    bool fSyncMode;
    bool fTemporary;
sletz's avatar
sletz committed
55
56
    jack_time_t fPeriodUsecs;
    jack_time_t fTimeOutUsecs;
57
58
    float fMaxDelayedUsecs;
    float fXrunDelayedUsecs;
sletz's avatar
sletz committed
59
60
    bool fTimeOut;
    bool fRealTime;
61
62
63
    int fServerPriority;
    int fClientPriority;
    int fMaxClientPriority;
sletz's avatar
sletz committed
64
    char fServerName[64];
sletz's avatar
sletz committed
65
66
    JackTransportEngine fTransport;
    bool fVerbose;
sletz's avatar
sletz committed
67

sletz's avatar
sletz committed
68
69
70
    // CPU Load
    jack_time_t fPrevCycleTime;
    jack_time_t fCurCycleTime;
sletz's avatar
sletz committed
71
72
73
74
75
76
77
78
    jack_time_t fSpareUsecs;
    jack_time_t fMaxUsecs;
    jack_time_t fRollingClientUsecs[JACK_ENGINE_ROLLING_COUNT];
    int	fRollingClientUsecsCnt;
    int	fRollingClientUsecsIndex;
    int	fRollingInterval;
    float fCPULoad;

79
    // For OSX thread
80
81
82
    UInt64 fPeriod;
    UInt64 fComputation;
    UInt64 fConstraint;
sletz's avatar
sletz committed
83
84
85

    // Timer
    JackFrameTimer fFrameTimer;
sletz's avatar
sletz committed
86
87
88
89
    
#ifdef JACK_MONITOR
    JackEngineProfiling fProfiler;
#endif   
sletz's avatar
sletz committed
90
91
92

    JackEngineControl(bool sync, bool temporary, long timeout, bool rt, long priority, bool verbose, const char* server_name)
    {
sletz's avatar
sletz committed
93
94
95
        fBufferSize = 512;
        fSampleRate = 48000;
        fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize);
sletz's avatar
sletz committed
96
97
98
99
100
        fSyncMode = sync;
        fTemporary = temporary;
        fTimeOut = (timeout > 0);
        fTimeOutUsecs = timeout * 1000;
        fRealTime = rt;
101
102
103
        fServerPriority = priority;
        fClientPriority = (rt) ? priority - 5 : 0;
        fMaxClientPriority = (rt) ? priority - 1 : 0;
sletz's avatar
sletz committed
104
        fVerbose = verbose;
sletz's avatar
sletz committed
105
106
        fPrevCycleTime = 0;
        fCurCycleTime = 0;
sletz's avatar
sletz committed
107
108
109
        fSpareUsecs = 0;
        fMaxUsecs = 0;
        ResetRollingUsecs();
110
        strncpy(fServerName, server_name, sizeof(fServerName));
111
        fPeriod = 0;
sletz's avatar
Cleanup    
sletz committed
112
113
        fComputation = 0;
        fConstraint = 0;
114
115
        fMaxDelayedUsecs = 0.f;
        fXrunDelayedUsecs = 0.f;
sletz's avatar
sletz committed
116
    }
sletz's avatar
sletz committed
117
    
sletz's avatar
sletz committed
118
119
120
121
    ~JackEngineControl()
    {}

    // Cycle
sletz's avatar
sletz committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    void CycleIncTime(jack_time_t callback_usecs)
    {
        // Timer
        fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
    }

    void CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
    {
        fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
        CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
#ifdef JACK_MONITOR
        fProfiler.Profile(table, manager, fPeriodUsecs, cur_cycle_begin, prev_cycle_end);
#endif
    }

    void CycleEnd(JackClientInterface** table)
    {
        fTransport.CycleEnd(table, fSampleRate, fBufferSize);
    }
sletz's avatar
sletz committed
141
142

    // Timer
sletz's avatar
sletz committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    void InitFrameTime()
    {
        fFrameTimer.InitFrameTime();
    }

    void ResetFrameTime(jack_time_t callback_usecs)
    {
        fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
    }

    void ReadFrameTime(JackTimer* timer)
    {
        fFrameTimer.ReadFrameTime(timer);
    }
157
158
159
    
    // XRun
    void NotifyXRun(float delayed_usecs);
sletz's avatar
sletz committed
160
161
162
163
    void ResetXRun()
    {
        fMaxDelayedUsecs = 0.f;
    }
sletz's avatar
sletz committed
164
165

    // Private
sletz's avatar
sletz committed
166
    void CalcCPULoad(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end);
sletz's avatar
sletz committed
167
168
    void ResetRollingUsecs();

sletz's avatar
sletz committed
169
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
170
171
172
173

} // end of namespace

#endif