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.

277 lines
6.3KB

  1. /*
  2. Copyright (C) 2001 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 General Public License as published by
  6. the Free Software Foundation; either version 2 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackSystemDeps.h"
  17. #include "JackThreadedDriver.h"
  18. #include "JackError.h"
  19. #include "JackTools.h"
  20. #include "JackGlobals.h"
  21. #include "JackEngineControl.h"
  22. namespace Jack
  23. {
  24. JackThreadedDriver::JackThreadedDriver(JackDriver* driver):fThread(this),fDriver(driver)
  25. {}
  26. JackThreadedDriver::~JackThreadedDriver()
  27. {
  28. delete fDriver;
  29. }
  30. int JackThreadedDriver::Open()
  31. {
  32. return fDriver->Open();
  33. }
  34. int JackThreadedDriver::Open(jack_nframes_t buffer_size,
  35. jack_nframes_t samplerate,
  36. bool capturing,
  37. bool playing,
  38. int inchannels,
  39. int outchannels,
  40. bool monitor,
  41. const char* capture_driver_name,
  42. const char* playback_driver_name,
  43. jack_nframes_t capture_latency,
  44. jack_nframes_t playback_latency)
  45. {
  46. return fDriver->Open(buffer_size,
  47. samplerate, capturing,
  48. playing, inchannels,
  49. outchannels, monitor,
  50. capture_driver_name, playback_driver_name,
  51. capture_latency, playback_latency);
  52. }
  53. int JackThreadedDriver::Close()
  54. {
  55. return fDriver->Close();
  56. }
  57. int JackThreadedDriver::Process()
  58. {
  59. return fDriver->Process();
  60. }
  61. int JackThreadedDriver::Attach()
  62. {
  63. return fDriver->Attach();
  64. }
  65. int JackThreadedDriver::Detach()
  66. {
  67. return fDriver->Detach();
  68. }
  69. int JackThreadedDriver::Read()
  70. {
  71. return fDriver->Read();
  72. }
  73. int JackThreadedDriver::Write()
  74. {
  75. return fDriver->Write();
  76. }
  77. bool JackThreadedDriver::IsFixedBufferSize()
  78. {
  79. return fDriver->IsFixedBufferSize();
  80. }
  81. int JackThreadedDriver::SetBufferSize(jack_nframes_t buffer_size)
  82. {
  83. return fDriver->SetBufferSize(buffer_size);
  84. }
  85. int JackThreadedDriver::SetSampleRate(jack_nframes_t sample_rate)
  86. {
  87. return fDriver->SetSampleRate(sample_rate);
  88. }
  89. void JackThreadedDriver::SetMaster(bool onoff)
  90. {
  91. fDriver->SetMaster(onoff);
  92. }
  93. bool JackThreadedDriver::GetMaster()
  94. {
  95. return fDriver->GetMaster();
  96. }
  97. void JackThreadedDriver::AddSlave(JackDriverInterface* slave)
  98. {
  99. fDriver->AddSlave(slave);
  100. }
  101. void JackThreadedDriver::RemoveSlave(JackDriverInterface* slave)
  102. {
  103. fDriver->RemoveSlave(slave);
  104. }
  105. int JackThreadedDriver::ProcessReadSlaves()
  106. {
  107. return fDriver->ProcessReadSlaves();
  108. }
  109. int JackThreadedDriver::ProcessWriteSlaves()
  110. {
  111. return fDriver->ProcessWriteSlaves();
  112. }
  113. int JackThreadedDriver::ProcessRead()
  114. {
  115. return fDriver->ProcessRead();
  116. }
  117. int JackThreadedDriver::ProcessWrite()
  118. {
  119. return fDriver->ProcessWrite();
  120. }
  121. int JackThreadedDriver::ProcessReadSync()
  122. {
  123. return fDriver->ProcessReadSync();
  124. }
  125. int JackThreadedDriver::ProcessWriteSync()
  126. {
  127. return fDriver->ProcessWriteSync();
  128. }
  129. int JackThreadedDriver::ProcessReadAsync()
  130. {
  131. return fDriver->ProcessReadAsync();
  132. }
  133. int JackThreadedDriver::ProcessWriteAsync()
  134. {
  135. return fDriver->ProcessWriteAsync();
  136. }
  137. std::list<JackDriverInterface*> JackThreadedDriver::GetSlaves()
  138. {
  139. return fDriver->GetSlaves();
  140. }
  141. int JackThreadedDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  142. {
  143. return fDriver->ClientNotify(refnum, name, notify, sync, message, value1, value2);
  144. }
  145. JackClientControl* JackThreadedDriver::GetClientControl() const
  146. {
  147. return fDriver->GetClientControl();
  148. }
  149. bool JackThreadedDriver::IsRealTime() const
  150. {
  151. return fDriver->IsRealTime();
  152. }
  153. bool JackThreadedDriver::IsRunning() const
  154. {
  155. return fDriver->IsRunning();
  156. }
  157. int JackThreadedDriver::Start()
  158. {
  159. jack_log("JackThreadedDriver::Start");
  160. if (fDriver->Start() < 0) {
  161. jack_error("Cannot start driver");
  162. return -1;
  163. }
  164. if (fThread.StartSync() < 0) {
  165. jack_error("Cannot start thread");
  166. return -1;
  167. }
  168. return 0;
  169. }
  170. int JackThreadedDriver::Stop()
  171. {
  172. jack_log("JackThreadedDriver::Stop");
  173. switch (fThread.GetStatus()) {
  174. // Kill the thread in Init phase
  175. case JackThread::kStarting:
  176. case JackThread::kIniting:
  177. if (fThread.Kill() < 0) {
  178. jack_error("Cannot kill thread");
  179. }
  180. break;
  181. // Stop when the thread cycle is finished
  182. case JackThread::kRunning:
  183. if (fThread.Stop() < 0) {
  184. jack_error("Cannot stop thread");
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. if (fDriver->Stop() < 0) {
  191. jack_error("Cannot stop driver");
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. bool JackThreadedDriver::Execute()
  197. {
  198. return (Process() == 0);
  199. }
  200. bool JackThreadedDriver::Init()
  201. {
  202. if (fDriver->Initialize()) {
  203. SetRealTime();
  204. return true;
  205. } else {
  206. return false;
  207. }
  208. }
  209. void JackThreadedDriver::SetRealTime()
  210. {
  211. if (fDriver->IsRealTime()) {
  212. jack_log("JackThreadedDriver::Init real-time");
  213. // Will do "something" on OSX only...
  214. GetEngineControl()->fPeriod = GetEngineControl()->fConstraint = GetEngineControl()->fPeriodUsecs * 1000;
  215. GetEngineControl()->fComputation = JackTools::ComputationMicroSec(GetEngineControl()->fBufferSize) * 1000;
  216. fThread.SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
  217. if (fThread.AcquireSelfRealTime(GetEngineControl()->fServerPriority) < 0) {
  218. jack_error("AcquireSelfRealTime error");
  219. } else {
  220. set_threaded_log_function();
  221. }
  222. } else {
  223. jack_log("JackThreadedDriver::Init non-realtime");
  224. }
  225. }
  226. } // end of namespace