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.

200 lines
5.6KB

  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. PRE_PACKED_STRUCTURE
  37. struct SERVER_EXPORT JackEngineControl : public JackShmMem
  38. {
  39. // Shared state
  40. jack_nframes_t fBufferSize;
  41. jack_nframes_t fSampleRate;
  42. bool fSyncMode;
  43. bool fTemporary;
  44. jack_time_t fPeriodUsecs;
  45. jack_time_t fTimeOutUsecs;
  46. float fMaxDelayedUsecs;
  47. float fXrunDelayedUsecs;
  48. bool fTimeOut;
  49. bool fRealTime;
  50. bool fSavedRealTime; // RT state saved and restored during Freewheel mode
  51. int fServerPriority;
  52. int fClientPriority;
  53. int fMaxClientPriority;
  54. char fServerName[JACK_SERVER_NAME_SIZE+1];
  55. alignas(UInt32) alignas(JackTransportEngine) JackTransportEngine fTransport;
  56. jack_timer_type_t fClockSource;
  57. int fDriverNum;
  58. bool fVerbose;
  59. // CPU Load
  60. jack_time_t fPrevCycleTime;
  61. jack_time_t fCurCycleTime;
  62. jack_time_t fSpareUsecs;
  63. jack_time_t fMaxUsecs;
  64. jack_time_t fRollingClientUsecs[JACK_ENGINE_ROLLING_COUNT];
  65. unsigned int fRollingClientUsecsCnt;
  66. int fRollingClientUsecsIndex;
  67. int fRollingInterval;
  68. float fCPULoad;
  69. // For OSX thread
  70. UInt64 fPeriod;
  71. UInt64 fComputation;
  72. UInt64 fConstraint;
  73. // Timer
  74. alignas(UInt32) alignas(JackFrameTimer) JackFrameTimer fFrameTimer;
  75. #ifdef JACK_MONITOR
  76. JackEngineProfiling fProfiler;
  77. #endif
  78. JackEngineControl(bool sync, bool temporary, long timeout, bool rt, long priority, bool verbose, jack_timer_type_t clock, const char* server_name)
  79. {
  80. static_assert(offsetof(JackEngineControl, fTransport) % sizeof(UInt32) == 0,
  81. "fTransport must be aligned within JackEngineControl");
  82. static_assert(offsetof(JackEngineControl, fFrameTimer) % sizeof(UInt32) == 0,
  83. "fFrameTimer must be aligned within JackEngineControl");
  84. fBufferSize = 512;
  85. fSampleRate = 48000;
  86. fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize);
  87. fSyncMode = sync;
  88. fTemporary = temporary;
  89. fTimeOut = (timeout > 0);
  90. fTimeOutUsecs = timeout * 1000;
  91. fRealTime = rt;
  92. fSavedRealTime = false;
  93. fServerPriority = priority;
  94. #ifdef WIN32
  95. fClientPriority = (rt) ? priority - 3 : 0;
  96. #else
  97. fClientPriority = (rt) ? priority - 5 : 0;
  98. #endif
  99. fMaxClientPriority = (rt) ? priority - 1 : 0;
  100. fVerbose = verbose;
  101. fPrevCycleTime = 0;
  102. fCurCycleTime = 0;
  103. fSpareUsecs = 0;
  104. fMaxUsecs = 0;
  105. ResetRollingUsecs();
  106. strncpy(fServerName, server_name, sizeof(fServerName));
  107. fServerName[sizeof(fServerName) - 1] = 0;
  108. fCPULoad = 0.f;
  109. fPeriod = 0;
  110. fComputation = 0;
  111. fConstraint = 0;
  112. fMaxDelayedUsecs = 0.f;
  113. fXrunDelayedUsecs = 0.f;
  114. fClockSource = clock;
  115. fDriverNum = 0;
  116. }
  117. ~JackEngineControl()
  118. {}
  119. void UpdateTimeOut()
  120. {
  121. fPeriodUsecs = jack_time_t(1000000.f / fSampleRate * fBufferSize); // In microsec
  122. if (!(fTimeOut && fTimeOutUsecs > 2 * fPeriodUsecs)) {
  123. fTimeOutUsecs = 2 * fPeriodUsecs;
  124. }
  125. }
  126. // Cycle
  127. void CycleIncTime(jack_time_t callback_usecs)
  128. {
  129. // Timer
  130. fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
  131. }
  132. void CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  133. {
  134. fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
  135. CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
  136. #ifdef JACK_MONITOR
  137. fProfiler.Profile(table, manager, fPeriodUsecs, cur_cycle_begin, prev_cycle_end);
  138. #endif
  139. }
  140. void CycleEnd(JackClientInterface** table)
  141. {
  142. fTransport.CycleEnd(table, fSampleRate, fBufferSize);
  143. }
  144. // Timer
  145. void InitFrameTime()
  146. {
  147. fFrameTimer.InitFrameTime();
  148. }
  149. void ResetFrameTime(jack_time_t callback_usecs)
  150. {
  151. fFrameTimer.ResetFrameTime(callback_usecs);
  152. }
  153. void ReadFrameTime(JackTimer* timer)
  154. {
  155. fFrameTimer.ReadFrameTime(timer);
  156. }
  157. // XRun
  158. void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);
  159. void ResetXRun()
  160. {
  161. fMaxDelayedUsecs = 0.f;
  162. }
  163. // Private
  164. void CalcCPULoad(JackClientInterface** table, JackGraphManager* manager, jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end);
  165. void ResetRollingUsecs();
  166. } POST_PACKED_STRUCTURE;
  167. } // end of namespace
  168. #endif