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.

397 lines
13KB

  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. (at your option) any later version.
  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 "JackAudioDriver.h"
  18. #include "JackTime.h"
  19. #include "JackError.h"
  20. #include "JackEngineControl.h"
  21. #include "JackPort.h"
  22. #include "JackGraphManager.h"
  23. #include "JackLockedEngine.h"
  24. #include "JackException.h"
  25. #include <assert.h>
  26. using namespace std;
  27. namespace Jack
  28. {
  29. JackAudioDriver::JackAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  30. : JackDriver(name, alias, engine, table)
  31. {}
  32. JackAudioDriver::~JackAudioDriver()
  33. {}
  34. int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  35. {
  36. // Update engine and graph manager state
  37. fEngineControl->fBufferSize = buffer_size;
  38. fGraphManager->SetBufferSize(buffer_size);
  39. fEngineControl->UpdateTimeOut();
  40. UpdateLatencies();
  41. // Redirected on slaves drivers...
  42. return JackDriver::SetBufferSize(buffer_size);
  43. }
  44. int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
  45. {
  46. fEngineControl->fSampleRate = sample_rate;
  47. fEngineControl->UpdateTimeOut();
  48. // Redirected on slaves drivers...
  49. return JackDriver::SetSampleRate(sample_rate);
  50. }
  51. int JackAudioDriver::Open(jack_nframes_t buffer_size,
  52. jack_nframes_t samplerate,
  53. bool capturing,
  54. bool playing,
  55. int inchannels,
  56. int outchannels,
  57. bool monitor,
  58. const char* capture_driver_name,
  59. const char* playback_driver_name,
  60. jack_nframes_t capture_latency,
  61. jack_nframes_t playback_latency)
  62. {
  63. fCaptureChannels = inchannels;
  64. fPlaybackChannels = outchannels;
  65. fWithMonitorPorts = monitor;
  66. memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  67. memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  68. memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  69. return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels,
  70. monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  71. }
  72. void JackAudioDriver::UpdateLatencies()
  73. {
  74. jack_latency_range_t input_range;
  75. jack_latency_range_t output_range;
  76. jack_latency_range_t monitor_range;
  77. for (int i = 0; i < fCaptureChannels; i++) {
  78. input_range.max = input_range.min = fEngineControl->fBufferSize + fCaptureLatency;
  79. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
  80. }
  81. for (int i = 0; i < fPlaybackChannels; i++) {
  82. output_range.max = output_range.min = fPlaybackLatency;
  83. if (fEngineControl->fSyncMode) {
  84. output_range.max = output_range.min += fEngineControl->fBufferSize;
  85. } else {
  86. output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
  87. }
  88. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
  89. if (fWithMonitorPorts) {
  90. monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
  91. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
  92. }
  93. }
  94. }
  95. int JackAudioDriver::Attach()
  96. {
  97. JackPort* port;
  98. jack_port_id_t port_index;
  99. char name[REAL_JACK_PORT_NAME_SIZE];
  100. char alias[REAL_JACK_PORT_NAME_SIZE];
  101. int i;
  102. jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  103. for (i = 0; i < fCaptureChannels; i++) {
  104. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  105. snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
  106. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  107. jack_error("driver: cannot register port for %s", name);
  108. return -1;
  109. }
  110. port = fGraphManager->GetPort(port_index);
  111. port->SetAlias(alias);
  112. fCapturePortList[i] = port_index;
  113. jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  114. }
  115. for (i = 0; i < fPlaybackChannels; i++) {
  116. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  117. snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
  118. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  119. jack_error("driver: cannot register port for %s", name);
  120. return -1;
  121. }
  122. port = fGraphManager->GetPort(port_index);
  123. port->SetAlias(alias);
  124. fPlaybackPortList[i] = port_index;
  125. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  126. // Monitor ports
  127. if (fWithMonitorPorts) {
  128. jack_log("Create monitor port");
  129. snprintf(name, sizeof(name), "%s:monitor_%u", fClientControl.fName, i + 1);
  130. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
  131. jack_error("Cannot register monitor port for %s", name);
  132. return -1;
  133. } else {
  134. fMonitorPortList[i] = port_index;
  135. }
  136. }
  137. }
  138. UpdateLatencies();
  139. return 0;
  140. }
  141. int JackAudioDriver::Detach()
  142. {
  143. int i;
  144. jack_log("JackAudioDriver::Detach");
  145. for (i = 0; i < fCaptureChannels; i++) {
  146. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
  147. }
  148. for (i = 0; i < fPlaybackChannels; i++) {
  149. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
  150. if (fWithMonitorPorts) {
  151. fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
  152. }
  153. }
  154. return 0;
  155. }
  156. int JackAudioDriver::Write()
  157. {
  158. for (int i = 0; i < fPlaybackChannels; i++) {
  159. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  160. jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
  161. int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
  162. // Monitor ports
  163. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
  164. memcpy(GetMonitorBuffer(i), buffer, size);
  165. }
  166. }
  167. }
  168. return 0;
  169. }
  170. int JackAudioDriver::Process()
  171. {
  172. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  173. }
  174. /*
  175. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  176. synchronize to the end of client graph execution.
  177. */
  178. int JackAudioDriver::ProcessAsync()
  179. {
  180. // Read input buffers for the current cycle
  181. if (Read() < 0) {
  182. jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
  183. return -1;
  184. }
  185. // Write output buffers from the previous cycle
  186. if (Write() < 0) {
  187. jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
  188. return -1;
  189. }
  190. // Process graph
  191. ProcessGraphAsync();
  192. // Keep end cycle time
  193. JackDriver::CycleTakeEndTime();
  194. return 0;
  195. }
  196. void JackAudioDriver::ProcessGraphAsync()
  197. {
  198. // Process graph
  199. if (fIsMaster) {
  200. ProcessGraphAsyncMaster();
  201. } else {
  202. ProcessGraphAsyncSlave();
  203. }
  204. }
  205. void JackAudioDriver::ProcessGraphAsyncMaster()
  206. {
  207. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  208. if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
  209. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
  210. }
  211. if (ResumeRefNum() < 0) {
  212. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
  213. }
  214. if (ProcessReadSlaves() < 0) {
  215. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
  216. }
  217. if (ProcessWriteSlaves() < 0) {
  218. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
  219. }
  220. // Does not wait on graph execution end
  221. }
  222. void JackAudioDriver::ProcessGraphAsyncSlave()
  223. {
  224. if (ResumeRefNum() < 0) {
  225. jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
  226. }
  227. }
  228. /*
  229. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  230. if graph process succeed, output buffers computed at the *current cycle* are used.
  231. */
  232. int JackAudioDriver::ProcessSync()
  233. {
  234. // Read input buffers for the current cycle
  235. if (Read() < 0) {
  236. jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
  237. return -1;
  238. }
  239. // Process graph
  240. ProcessGraphSync();
  241. // Write output buffers from the current cycle
  242. if (Write() < 0) {
  243. jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
  244. return -1;
  245. }
  246. // Keep end cycle time
  247. JackDriver::CycleTakeEndTime();
  248. return 0;
  249. }
  250. void JackAudioDriver::ProcessGraphSync()
  251. {
  252. // Process graph
  253. if (fIsMaster) {
  254. ProcessGraphSyncMaster();
  255. } else {
  256. ProcessGraphSyncSlave();
  257. }
  258. }
  259. void JackAudioDriver::ProcessGraphSyncMaster()
  260. {
  261. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  262. if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
  263. if (ResumeRefNum() < 0) {
  264. jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
  265. }
  266. if (ProcessReadSlaves() < 0) {
  267. jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
  268. }
  269. if (ProcessWriteSlaves() < 0) {
  270. jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
  271. }
  272. // Waits for graph execution end
  273. if (SuspendRefNum() < 0) {
  274. jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
  275. }
  276. } else { // Graph not finished: do not activate it
  277. jack_error("JackAudioDriver::ProcessGraphSync: Process error");
  278. }
  279. }
  280. void JackAudioDriver::ProcessGraphSyncSlave()
  281. {
  282. if (ResumeRefNum() < 0) {
  283. jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
  284. }
  285. }
  286. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  287. {
  288. return fCapturePortList[port_index]
  289. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
  290. : NULL;
  291. }
  292. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  293. {
  294. return fPlaybackPortList[port_index]
  295. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
  296. : NULL;
  297. }
  298. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  299. {
  300. return fPlaybackPortList[port_index]
  301. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
  302. : NULL;
  303. }
  304. int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  305. {
  306. switch (notify) {
  307. case kLatencyCallback:
  308. HandleLatencyCallback(value1);
  309. break;
  310. default:
  311. JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
  312. break;
  313. }
  314. return 0;
  315. }
  316. void JackAudioDriver::HandleLatencyCallback(int status)
  317. {
  318. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  319. for (int i = 0; i < fCaptureChannels; i++) {
  320. if (mode == JackPlaybackLatency) {
  321. fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
  322. }
  323. }
  324. for (int i = 0; i < fPlaybackChannels; i++) {
  325. if (mode == JackCaptureLatency) {
  326. fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
  327. }
  328. }
  329. }
  330. } // end of namespace