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.

230 lines
8.1KB

  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 "JackAudioDriver.h"
  17. #include "JackTime.h"
  18. #include "JackError.h"
  19. #include "JackEngineControl.h"
  20. #include "JackClientControl.h"
  21. #include "JackPort.h"
  22. #include "JackGraphManager.h"
  23. #include "JackEngine.h"
  24. #include <assert.h>
  25. namespace Jack
  26. {
  27. JackAudioDriver::JackAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
  28. : JackDriver(name, engine, table),
  29. fCaptureChannels(0),
  30. fPlaybackChannels(0),
  31. fWithMonitorPorts(false)
  32. {}
  33. JackAudioDriver::~JackAudioDriver()
  34. {}
  35. int JackAudioDriver::Open(jack_nframes_t nframes,
  36. jack_nframes_t samplerate,
  37. int capturing,
  38. int playing,
  39. int inchannels,
  40. int outchannels,
  41. bool monitor,
  42. const char* capture_driver_name,
  43. const char* playback_driver_name,
  44. jack_nframes_t capture_latency,
  45. jack_nframes_t playback_latency)
  46. {
  47. fCaptureChannels = inchannels;
  48. fPlaybackChannels = outchannels;
  49. fWithMonitorPorts = monitor;
  50. return JackDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  51. }
  52. int JackAudioDriver::Attach()
  53. {
  54. JackPort* port;
  55. jack_port_id_t port_index;
  56. char buf[JACK_PORT_NAME_SIZE];
  57. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  58. int i;
  59. JackLog("JackAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  60. for (i = 0; i < fCaptureChannels; i++) {
  61. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%d", fClientControl->fName, fCaptureDriverName, i + 1);
  62. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
  63. jack_error("driver: cannot register port for %s", buf);
  64. return -1;
  65. }
  66. port = fGraphManager->GetPort(port_index);
  67. port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
  68. fCapturePortList[i] = port_index;
  69. JackLog("JackAudioDriver::Attach fCapturePortList[i] %ld \n", port_index);
  70. }
  71. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  72. for (i = 0; i < fPlaybackChannels; i++) {
  73. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%d", fClientControl->fName, fPlaybackDriverName, i + 1);
  74. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
  75. jack_error("driver: cannot register port for %s", buf);
  76. return -1;
  77. }
  78. port = fGraphManager->GetPort(port_index);
  79. port->SetLatency(fEngineControl->fBufferSize + fPlaybackLatency);
  80. fPlaybackPortList[i] = port_index;
  81. JackLog("JackAudioDriver::Attach fPlaybackPortList[i] %ld \n", port_index);
  82. // Monitor ports
  83. if (fWithMonitorPorts) {
  84. JackLog("Create monitor port \n");
  85. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  86. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JackPortIsOutput)) == NO_PORT) {
  87. jack_error("Cannot register monitor port for %s", buf);
  88. return -1;
  89. } else {
  90. port = fGraphManager->GetPort(port_index);
  91. port->SetLatency(fEngineControl->fBufferSize);
  92. fMonitorPortList[i] = port_index;
  93. }
  94. }
  95. }
  96. return 0;
  97. }
  98. int JackAudioDriver::Detach()
  99. {
  100. int i;
  101. JackLog("JackAudioDriver::Detach\n");
  102. for (i = 0; i < fCaptureChannels; i++) {
  103. fGraphManager->RemovePort(fClientControl->fRefNum, fCapturePortList[i]);
  104. }
  105. for (i = 0; i < fPlaybackChannels; i++) {
  106. fGraphManager->RemovePort(fClientControl->fRefNum, fPlaybackPortList[i]);
  107. if (fWithMonitorPorts)
  108. fGraphManager->RemovePort(fClientControl->fRefNum, fMonitorPortList[i]);
  109. }
  110. return 0;
  111. }
  112. int JackAudioDriver::Write()
  113. {
  114. for (int i = 0; i < fPlaybackChannels; i++) {
  115. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  116. float* buffer = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], fEngineControl->fBufferSize);
  117. int size = sizeof(float) * fEngineControl->fBufferSize;
  118. // Monitor ports
  119. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  120. memcpy((jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[i], fEngineControl->fBufferSize), buffer, size);
  121. }
  122. }
  123. return 0;
  124. }
  125. int JackAudioDriver::Process()
  126. {
  127. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  128. }
  129. /*
  130. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  131. synchronize to the end of client graph execution.
  132. */
  133. int JackAudioDriver::ProcessAsync()
  134. {
  135. if (Read() < 0) { // Read input buffers for the current cycle
  136. jack_error("ProcessAsync: read error");
  137. return 0;
  138. }
  139. if (Write() < 0) { // Write output buffers from the previous cycle
  140. jack_error("ProcessAsync: write error");
  141. return 0;
  142. }
  143. if (fIsMaster) {
  144. fEngine->Process(fLastWaitUst); // fLastWaitUst is set in the "low level" layer
  145. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  146. ProcessSlaves();
  147. } else {
  148. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  149. }
  150. return 0;
  151. }
  152. /*
  153. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  154. output buffers computed at the *current cycle* are used.
  155. */
  156. int JackAudioDriver::ProcessSync()
  157. {
  158. if (Read() < 0) { // Read input buffers for the current cycle
  159. jack_error("ProcessSync: read error");
  160. return 0;
  161. }
  162. if (fIsMaster) {
  163. fEngine->Process(fLastWaitUst); // fLastWaitUst is set in the "low level" layer
  164. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  165. ProcessSlaves();
  166. if (fGraphManager->SuspendRefNum(fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0)
  167. jack_error("JackAudioDriver::ProcessSync SuspendRefNum error");
  168. if (Write() < 0) // Write output buffers for the current cycle
  169. jack_error("Process: write error");
  170. } else {
  171. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  172. }
  173. return 0;
  174. }
  175. void JackAudioDriver::NotifyXRun(jack_time_t callback_usecs)
  176. {
  177. fEngine->NotifyXRun(callback_usecs);
  178. }
  179. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  180. {
  181. assert(fCapturePortList[port_index]);
  182. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  183. }
  184. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  185. {
  186. assert(fPlaybackPortList[port_index]);
  187. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  188. }
  189. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  190. {
  191. assert(fPlaybackPortList[port_index]);
  192. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
  193. }
  194. } // end of namespace