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.

297 lines
7.4KB

  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #ifdef WIN32
  20. #pragma warning (disable : 4786)
  21. #endif
  22. #include "JackDriver.h"
  23. #include "JackTime.h"
  24. #include "JackError.h"
  25. #include "JackPort.h"
  26. #include "JackGraphManager.h"
  27. #include "JackGlobals.h"
  28. #include "JackEngineControl.h"
  29. #include "JackClientControl.h"
  30. #include "JackLockedEngine.h"
  31. #include <math.h>
  32. #include <assert.h>
  33. using namespace std;
  34. namespace Jack
  35. {
  36. JackDriver::JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  37. {
  38. assert(strlen(name) < JACK_CLIENT_NAME_SIZE);
  39. fSynchroTable = table;
  40. fClientControl = new JackClientControl(name);
  41. strcpy(fAliasName, alias);
  42. fEngine = engine;
  43. fGraphManager = NULL;
  44. fBeginDateUst = 0;
  45. fDelayedUsecs = 0.f;
  46. fIsMaster = true;
  47. }
  48. JackDriver::JackDriver()
  49. {
  50. fSynchroTable = NULL;
  51. fClientControl = NULL;
  52. fEngine = NULL;
  53. fGraphManager = NULL;
  54. fBeginDateUst = 0;
  55. fIsMaster = true;
  56. }
  57. JackDriver::~JackDriver()
  58. {
  59. jack_log("~JackDriver");
  60. delete fClientControl;
  61. }
  62. int JackDriver::Open()
  63. {
  64. int refnum = -1;
  65. if (fEngine->ClientInternalOpen(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
  66. jack_error("Cannot allocate internal client for audio driver");
  67. return -1;
  68. }
  69. fClientControl->fRefNum = refnum;
  70. fClientControl->fActive = true;
  71. fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for "sync" mode
  72. SetupDriverSync(fClientControl->fRefNum, false);
  73. return 0;
  74. }
  75. int JackDriver::Open(jack_nframes_t nframes,
  76. jack_nframes_t samplerate,
  77. bool capturing,
  78. bool playing,
  79. int inchannels,
  80. int outchannels,
  81. bool monitor,
  82. const char* capture_driver_name,
  83. const char* playback_driver_name,
  84. jack_nframes_t capture_latency,
  85. jack_nframes_t playback_latency)
  86. {
  87. jack_log("JackDriver::Open capture_driver_name = %s", capture_driver_name);
  88. jack_log("JackDriver::Open playback_driver_name = %s", playback_driver_name);
  89. int refnum = -1;
  90. if (fEngine->ClientInternalOpen(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
  91. jack_error("Cannot allocate internal client for audio driver");
  92. return -1;
  93. }
  94. fClientControl->fRefNum = refnum;
  95. fClientControl->fActive = true;
  96. fEngineControl->fBufferSize = nframes;
  97. fEngineControl->fSampleRate = samplerate;
  98. fCaptureLatency = capture_latency;
  99. fPlaybackLatency = playback_latency;
  100. assert(strlen(capture_driver_name) < JACK_CLIENT_NAME_SIZE);
  101. assert(strlen(playback_driver_name) < JACK_CLIENT_NAME_SIZE);
  102. strcpy(fCaptureDriverName, capture_driver_name);
  103. strcpy(fPlaybackDriverName, playback_driver_name);
  104. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  105. if (!fEngineControl->fTimeOut)
  106. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  107. fGraphManager->SetBufferSize(nframes);
  108. fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for "sync" mode
  109. SetupDriverSync(fClientControl->fRefNum, false);
  110. return 0;
  111. }
  112. int JackDriver::Close()
  113. {
  114. jack_log("JackDriver::Close");
  115. fGraphManager->DirectDisconnect(fClientControl->fRefNum, fClientControl->fRefNum); // Disconnect driver from itself for sync
  116. fClientControl->fActive = false;
  117. return fEngine->ClientInternalClose(fClientControl->fRefNum, false);
  118. }
  119. /*!
  120. In "async" mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
  121. The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
  122. Drivers synchro are setup in "flush" mode if server is "async" and NOT freewheel.
  123. */
  124. void JackDriver::SetupDriverSync(int ref, bool freewheel)
  125. {
  126. if (!freewheel && !fEngineControl->fSyncMode) {
  127. jack_log("JackDriver::SetupDriverSync driver sem in flush mode");
  128. fSynchroTable[ref].SetFlush(true);
  129. } else {
  130. jack_log("JackDriver::SetupDriverSync driver sem in normal mode");
  131. fSynchroTable[ref].SetFlush(false);
  132. }
  133. }
  134. int JackDriver::ClientNotify(int refnum, const char* name, int notify, int sync, int value1, int value2)
  135. {
  136. switch (notify) {
  137. case kStartFreewheelCallback:
  138. jack_log("JackDriver::kStartFreewheel");
  139. SetupDriverSync(fClientControl->fRefNum, true);
  140. break;
  141. case kStopFreewheelCallback:
  142. jack_log("JackDriver::kStopFreewheel");
  143. SetupDriverSync(fClientControl->fRefNum, false);
  144. break;
  145. }
  146. return 0;
  147. }
  148. bool JackDriver::IsRealTime() const
  149. {
  150. return fEngineControl->fRealTime;
  151. }
  152. void JackDriver::CycleIncTime()
  153. {
  154. fEngineControl->CycleIncTime(fBeginDateUst);
  155. }
  156. void JackDriver::CycleTakeBeginTime()
  157. {
  158. fBeginDateUst = GetMicroSeconds(); // Take callback date here
  159. fEngineControl->CycleIncTime(fBeginDateUst);
  160. }
  161. void JackDriver::CycleTakeEndTime()
  162. {
  163. fEndDateUst = GetMicroSeconds(); // Take end date here
  164. }
  165. JackClientControl* JackDriver::GetClientControl() const
  166. {
  167. return fClientControl;
  168. }
  169. void JackDriver::NotifyXRun(jack_time_t cur_cycle_begin, float delayed_usecs)
  170. {
  171. fEngine->NotifyXRun(cur_cycle_begin, delayed_usecs);
  172. }
  173. void JackDriverClient::SetMaster(bool onoff)
  174. {
  175. fIsMaster = onoff;
  176. }
  177. bool JackDriverClient::GetMaster()
  178. {
  179. return fIsMaster;
  180. }
  181. void JackDriverClient::AddSlave(JackDriverInterface* slave)
  182. {
  183. fSlaveList.push_back(slave);
  184. }
  185. void JackDriverClient::RemoveSlave(JackDriverInterface* slave)
  186. {
  187. fSlaveList.remove(slave);
  188. }
  189. int JackDriverClient::ProcessSlaves()
  190. {
  191. int res = 0;
  192. list<JackDriverInterface*>::const_iterator it;
  193. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  194. JackDriverInterface* slave = *it;
  195. if (slave->Process() < 0)
  196. res = -1;
  197. }
  198. return res;
  199. }
  200. int JackDriver::Process()
  201. {
  202. return 0;
  203. }
  204. int JackDriver::ProcessNull()
  205. {
  206. return 0;
  207. }
  208. int JackDriver::Attach()
  209. {
  210. return 0;
  211. }
  212. int JackDriver::Detach()
  213. {
  214. return 0;
  215. }
  216. int JackDriver::Read()
  217. {
  218. return 0;
  219. }
  220. int JackDriver::Write()
  221. {
  222. return 0;
  223. }
  224. int JackDriver::Start()
  225. {
  226. return 0;
  227. }
  228. int JackDriver::Stop()
  229. {
  230. return 0;
  231. }
  232. int JackDriver::SetBufferSize(jack_nframes_t buffer_size)
  233. {
  234. return 0;
  235. }
  236. int JackDriver::SetSampleRate(jack_nframes_t sample_rate)
  237. {
  238. return 0;
  239. }
  240. bool JackDriver::Init()
  241. {
  242. return true;
  243. }
  244. } // end of namespace