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.

417 lines
14KB

  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. int JackAudioDriver::Open(bool capturing,
  73. bool playing,
  74. int inchannels,
  75. int outchannels,
  76. bool monitor,
  77. const char* capture_driver_name,
  78. const char* playback_driver_name,
  79. jack_nframes_t capture_latency,
  80. jack_nframes_t playback_latency)
  81. {
  82. fCaptureChannels = inchannels;
  83. fPlaybackChannels = outchannels;
  84. fWithMonitorPorts = monitor;
  85. memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  86. memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  87. memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  88. return JackDriver::Open(capturing, playing, inchannels, outchannels,
  89. monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  90. }
  91. void JackAudioDriver::UpdateLatencies()
  92. {
  93. jack_latency_range_t input_range;
  94. jack_latency_range_t output_range;
  95. jack_latency_range_t monitor_range;
  96. for (int i = 0; i < fCaptureChannels; i++) {
  97. input_range.max = input_range.min = fEngineControl->fBufferSize + fCaptureLatency;
  98. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
  99. }
  100. for (int i = 0; i < fPlaybackChannels; i++) {
  101. output_range.max = output_range.min = fPlaybackLatency;
  102. if (fEngineControl->fSyncMode) {
  103. output_range.max = output_range.min += fEngineControl->fBufferSize;
  104. } else {
  105. output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
  106. }
  107. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
  108. if (fWithMonitorPorts) {
  109. monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
  110. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
  111. }
  112. }
  113. }
  114. int JackAudioDriver::Attach()
  115. {
  116. JackPort* port;
  117. jack_port_id_t port_index;
  118. char name[REAL_JACK_PORT_NAME_SIZE];
  119. char alias[REAL_JACK_PORT_NAME_SIZE];
  120. int i;
  121. jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  122. for (i = 0; i < fCaptureChannels; i++) {
  123. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  124. snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
  125. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  126. jack_error("driver: cannot register port for %s", name);
  127. return -1;
  128. }
  129. port = fGraphManager->GetPort(port_index);
  130. port->SetAlias(alias);
  131. fCapturePortList[i] = port_index;
  132. jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  133. }
  134. for (i = 0; i < fPlaybackChannels; i++) {
  135. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  136. snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
  137. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  138. jack_error("driver: cannot register port for %s", name);
  139. return -1;
  140. }
  141. port = fGraphManager->GetPort(port_index);
  142. port->SetAlias(alias);
  143. fPlaybackPortList[i] = port_index;
  144. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  145. // Monitor ports
  146. if (fWithMonitorPorts) {
  147. jack_log("Create monitor port");
  148. snprintf(name, sizeof(name), "%s:monitor_%u", fClientControl.fName, i + 1);
  149. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
  150. jack_error("Cannot register monitor port for %s", name);
  151. return -1;
  152. } else {
  153. fMonitorPortList[i] = port_index;
  154. }
  155. }
  156. }
  157. UpdateLatencies();
  158. return 0;
  159. }
  160. int JackAudioDriver::Detach()
  161. {
  162. int i;
  163. jack_log("JackAudioDriver::Detach");
  164. for (i = 0; i < fCaptureChannels; i++) {
  165. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
  166. }
  167. for (i = 0; i < fPlaybackChannels; i++) {
  168. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
  169. if (fWithMonitorPorts) {
  170. fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
  171. }
  172. }
  173. return 0;
  174. }
  175. int JackAudioDriver::Write()
  176. {
  177. for (int i = 0; i < fPlaybackChannels; i++) {
  178. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  179. jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
  180. int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
  181. // Monitor ports
  182. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
  183. memcpy(GetMonitorBuffer(i), buffer, size);
  184. }
  185. }
  186. }
  187. return 0;
  188. }
  189. int JackAudioDriver::Process()
  190. {
  191. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  192. }
  193. /*
  194. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  195. synchronize to the end of client graph execution.
  196. */
  197. int JackAudioDriver::ProcessAsync()
  198. {
  199. // Read input buffers for the current cycle
  200. if (Read() < 0) {
  201. jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
  202. return -1;
  203. }
  204. // Write output buffers from the previous cycle
  205. if (Write() < 0) {
  206. jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
  207. return -1;
  208. }
  209. // Process graph
  210. ProcessGraphAsync();
  211. // Keep end cycle time
  212. JackDriver::CycleTakeEndTime();
  213. return 0;
  214. }
  215. void JackAudioDriver::ProcessGraphAsync()
  216. {
  217. // Process graph
  218. if (fIsMaster) {
  219. ProcessGraphAsyncMaster();
  220. } else {
  221. ProcessGraphAsyncSlave();
  222. }
  223. }
  224. void JackAudioDriver::ProcessGraphAsyncMaster()
  225. {
  226. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  227. if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
  228. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
  229. }
  230. if (ResumeRefNum() < 0) {
  231. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
  232. }
  233. if (ProcessReadSlaves() < 0) {
  234. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
  235. }
  236. if (ProcessWriteSlaves() < 0) {
  237. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
  238. }
  239. // Does not wait on graph execution end
  240. }
  241. void JackAudioDriver::ProcessGraphAsyncSlave()
  242. {
  243. if (ResumeRefNum() < 0) {
  244. jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
  245. }
  246. }
  247. /*
  248. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  249. if graph process succeed, output buffers computed at the *current cycle* are used.
  250. */
  251. int JackAudioDriver::ProcessSync()
  252. {
  253. // Read input buffers for the current cycle
  254. if (Read() < 0) {
  255. jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
  256. return -1;
  257. }
  258. // Process graph
  259. ProcessGraphSync();
  260. // Write output buffers from the current cycle
  261. if (Write() < 0) {
  262. jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
  263. return -1;
  264. }
  265. // Keep end cycle time
  266. JackDriver::CycleTakeEndTime();
  267. return 0;
  268. }
  269. void JackAudioDriver::ProcessGraphSync()
  270. {
  271. // Process graph
  272. if (fIsMaster) {
  273. ProcessGraphSyncMaster();
  274. } else {
  275. ProcessGraphSyncSlave();
  276. }
  277. }
  278. void JackAudioDriver::ProcessGraphSyncMaster()
  279. {
  280. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  281. if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
  282. if (ResumeRefNum() < 0) {
  283. jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
  284. }
  285. if (ProcessReadSlaves() < 0) {
  286. jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
  287. }
  288. if (ProcessWriteSlaves() < 0) {
  289. jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
  290. }
  291. // Waits for graph execution end
  292. if (SuspendRefNum() < 0) {
  293. jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
  294. }
  295. } else { // Graph not finished: do not activate it
  296. jack_error("JackAudioDriver::ProcessGraphSync: Process error");
  297. }
  298. }
  299. void JackAudioDriver::ProcessGraphSyncSlave()
  300. {
  301. if (ResumeRefNum() < 0) {
  302. jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
  303. }
  304. }
  305. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  306. {
  307. return fCapturePortList[port_index]
  308. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
  309. : NULL;
  310. }
  311. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  312. {
  313. return fPlaybackPortList[port_index]
  314. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
  315. : NULL;
  316. }
  317. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  318. {
  319. return fPlaybackPortList[port_index]
  320. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
  321. : NULL;
  322. }
  323. int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  324. {
  325. switch (notify) {
  326. case kLatencyCallback:
  327. HandleLatencyCallback(value1);
  328. break;
  329. default:
  330. JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
  331. break;
  332. }
  333. return 0;
  334. }
  335. void JackAudioDriver::HandleLatencyCallback(int status)
  336. {
  337. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  338. for (int i = 0; i < fCaptureChannels; i++) {
  339. if (mode == JackPlaybackLatency) {
  340. fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
  341. }
  342. }
  343. for (int i = 0; i < fPlaybackChannels; i++) {
  344. if (mode == JackCaptureLatency) {
  345. fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
  346. }
  347. }
  348. }
  349. } // end of namespace