jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.8KB

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