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.

205 lines
6.2KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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 "JackDriver.h"
  17. #include "JackTime.h"
  18. #include "JackError.h"
  19. #include "JackPort.h"
  20. #include "JackGraphManager.h"
  21. #include "JackGlobals.h"
  22. #include "JackEngineControl.h"
  23. #include "JackClientControl.h"
  24. #include "JackEngine.h"
  25. #include <math.h>
  26. #include <assert.h>
  27. using namespace std;
  28. namespace Jack
  29. {
  30. JackDriver::JackDriver(const char* name, JackEngine* engine, JackSynchro** table)
  31. {
  32. assert(strlen(name) < JACK_CLIENT_NAME_SIZE);
  33. fSynchroTable = table;
  34. fClientControl = new JackClientControl(name);
  35. fEngine = engine;
  36. fGraphManager = NULL;
  37. fLastWaitUst = 0;
  38. fIsMaster = true;
  39. }
  40. JackDriver::JackDriver()
  41. {
  42. fSynchroTable = NULL;
  43. fClientControl = NULL;
  44. fEngine = NULL;
  45. fGraphManager = NULL;
  46. fLastWaitUst = 0;
  47. fIsMaster = true;
  48. }
  49. JackDriver::~JackDriver()
  50. {
  51. JackLog("~JackDriver\n");
  52. delete fClientControl;
  53. }
  54. int JackDriver::Open()
  55. {
  56. int refnum = -1;
  57. if (fEngine->ClientCheckName(fClientControl->fName)) {
  58. jack_error("client %s already registered", fClientControl->fName);
  59. return -1;
  60. }
  61. if (fEngine->ClientInternalNew(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this) != 0) {
  62. jack_error("Cannot allocate internal client for audio driver");
  63. return -1;
  64. }
  65. fClientControl->fRefNum = refnum;
  66. fClientControl->fActive = true;
  67. fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for sync
  68. /*
  69. In ASYNC mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
  70. The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
  71. */
  72. if (!fEngineControl->fSyncMode) {
  73. fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
  74. fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
  75. fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
  76. }
  77. return 0;
  78. }
  79. int JackDriver::Open(jack_nframes_t nframes,
  80. jack_nframes_t samplerate,
  81. int capturing,
  82. int playing,
  83. int inchannels,
  84. int outchannels,
  85. bool monitor,
  86. const char* capture_driver_name,
  87. const char* playback_driver_name,
  88. jack_nframes_t capture_latency,
  89. jack_nframes_t playback_latency)
  90. {
  91. JackLog("JackDriver::Open capture_driver_name = %s\n", capture_driver_name);
  92. JackLog("JackDriver::Open playback_driver_name = %s\n", playback_driver_name);
  93. int refnum = -1;
  94. if (fEngine->ClientCheckName(fClientControl->fName)) {
  95. jack_error("client %s already registered", fClientControl->fName);
  96. return -1;
  97. }
  98. if (fEngine->ClientInternalNew(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this) != 0) {
  99. jack_error("Cannot allocate internal client for audio driver");
  100. return -1;
  101. }
  102. fClientControl->fRefNum = refnum;
  103. fClientControl->fActive = true;
  104. fEngineControl->fBufferSize = nframes;
  105. fEngineControl->fSampleRate = samplerate;
  106. fCaptureLatency = capture_latency;
  107. fPlaybackLatency = playback_latency;
  108. assert(strlen(capture_driver_name) < JACK_CLIENT_NAME_SIZE);
  109. assert(strlen(playback_driver_name) < JACK_CLIENT_NAME_SIZE);
  110. strcpy(fCaptureDriverName, capture_driver_name);
  111. strcpy(fPlaybackDriverName, playback_driver_name);
  112. fEngineControl->fPeriodUsecs = (jack_time_t)floor((((float)nframes) / (float)samplerate) * 1000000.0f);
  113. if (fEngineControl->fTimeOutUsecs == 0) /* usecs; if zero, use 2 period size. */
  114. fEngineControl->fTimeOutUsecs = (jack_time_t)(2.f * fEngineControl->fPeriodUsecs);
  115. fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for sync
  116. /*
  117. In ASYNC mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
  118. The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
  119. */
  120. if (!fEngineControl->fSyncMode) {
  121. fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
  122. fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
  123. fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
  124. }
  125. return 0;
  126. }
  127. int JackDriver::Close()
  128. {
  129. JackLog("JackDriver::Close\n");
  130. fGraphManager->DirectDisconnect(fClientControl->fRefNum, fClientControl->fRefNum); // Disconnect driver from itself for sync
  131. fClientControl->fActive = false;
  132. return fEngine->ClientInternalCloseIm(fClientControl->fRefNum);
  133. }
  134. bool JackDriver::IsRealTime()
  135. {
  136. return fEngineControl->fRealTime;
  137. }
  138. JackClientControl* JackDriver::GetClientControl() const
  139. {
  140. return fClientControl;
  141. }
  142. void JackDriver::NotifyXRun(jack_time_t callback_usecs)
  143. {
  144. fEngine->NotifyXRun(callback_usecs);
  145. }
  146. void JackDriverClient::SetMaster(bool onoff)
  147. {
  148. fIsMaster = onoff;
  149. }
  150. bool JackDriverClient::GetMaster()
  151. {
  152. return fIsMaster;
  153. }
  154. void JackDriverClient::AddSlave(JackDriverInterface* slave)
  155. {
  156. fSlaveList.push_back(slave);
  157. }
  158. void JackDriverClient::RemoveSlave(JackDriverInterface* slave)
  159. {
  160. fSlaveList.remove(slave);
  161. }
  162. void JackDriverClient::ProcessSlaves()
  163. {
  164. list<JackDriverInterface*>::const_iterator it;
  165. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  166. JackDriverInterface* slave = *it;
  167. slave->Process();
  168. }
  169. }
  170. } // end of namespace