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.

416 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. return 0;
  187. }
  188. int JackAudioDriver::Process()
  189. {
  190. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  191. }
  192. /*
  193. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  194. synchronize to the end of client graph execution.
  195. */
  196. int JackAudioDriver::ProcessAsync()
  197. {
  198. // Read input buffers for the current cycle
  199. if (Read() < 0) {
  200. jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
  201. return -1;
  202. }
  203. // Write output buffers from the previous cycle
  204. if (Write() < 0) {
  205. jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
  206. return -1;
  207. }
  208. // Process graph
  209. ProcessGraphAsync();
  210. // Keep end cycle time
  211. JackDriver::CycleTakeEndTime();
  212. return 0;
  213. }
  214. void JackAudioDriver::ProcessGraphAsync()
  215. {
  216. // Process graph
  217. if (fIsMaster) {
  218. ProcessGraphAsyncMaster();
  219. } else {
  220. ProcessGraphAsyncSlave();
  221. }
  222. }
  223. void JackAudioDriver::ProcessGraphAsyncMaster()
  224. {
  225. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  226. if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
  227. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
  228. }
  229. if (ResumeRefNum() < 0) {
  230. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
  231. }
  232. if (ProcessReadSlaves() < 0) {
  233. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
  234. }
  235. if (ProcessWriteSlaves() < 0) {
  236. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
  237. }
  238. // Does not wait on graph execution end
  239. }
  240. void JackAudioDriver::ProcessGraphAsyncSlave()
  241. {
  242. if (ResumeRefNum() < 0) {
  243. jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
  244. }
  245. }
  246. /*
  247. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  248. if graph process succeed, output buffers computed at the *current cycle* are used.
  249. */
  250. int JackAudioDriver::ProcessSync()
  251. {
  252. // Read input buffers for the current cycle
  253. if (Read() < 0) {
  254. jack_error("JackAudioDriver::ProcessSync: read error, stopping...");
  255. return -1;
  256. }
  257. // Process graph
  258. ProcessGraphSync();
  259. // Write output buffers from the current cycle
  260. if (Write() < 0) {
  261. jack_error("JackAudioDriver::ProcessSync: write error, stopping...");
  262. return -1;
  263. }
  264. // Keep end cycle time
  265. JackDriver::CycleTakeEndTime();
  266. return 0;
  267. }
  268. void JackAudioDriver::ProcessGraphSync()
  269. {
  270. // Process graph
  271. if (fIsMaster) {
  272. ProcessGraphSyncMaster();
  273. } else {
  274. ProcessGraphSyncSlave();
  275. }
  276. }
  277. void JackAudioDriver::ProcessGraphSyncMaster()
  278. {
  279. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  280. if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
  281. if (ResumeRefNum() < 0) {
  282. jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
  283. }
  284. if (ProcessReadSlaves() < 0) {
  285. jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
  286. }
  287. if (ProcessWriteSlaves() < 0) {
  288. jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
  289. }
  290. // Waits for graph execution end
  291. if (SuspendRefNum() < 0) {
  292. jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
  293. }
  294. } else { // Graph not finished: do not activate it
  295. jack_error("JackAudioDriver::ProcessGraphSync: Process error");
  296. }
  297. }
  298. void JackAudioDriver::ProcessGraphSyncSlave()
  299. {
  300. if (ResumeRefNum() < 0) {
  301. jack_error("JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
  302. }
  303. }
  304. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  305. {
  306. return fCapturePortList[port_index]
  307. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
  308. : NULL;
  309. }
  310. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  311. {
  312. return fPlaybackPortList[port_index]
  313. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
  314. : NULL;
  315. }
  316. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  317. {
  318. return fPlaybackPortList[port_index]
  319. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
  320. : NULL;
  321. }
  322. int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  323. {
  324. switch (notify) {
  325. case kLatencyCallback:
  326. HandleLatencyCallback(value1);
  327. break;
  328. default:
  329. JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
  330. break;
  331. }
  332. return 0;
  333. }
  334. void JackAudioDriver::HandleLatencyCallback(int status)
  335. {
  336. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  337. for (int i = 0; i < fCaptureChannels; i++) {
  338. if (mode == JackPlaybackLatency) {
  339. fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
  340. }
  341. }
  342. for (int i = 0; i < fPlaybackChannels; i++) {
  343. if (mode == JackCaptureLatency) {
  344. fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
  345. }
  346. }
  347. }
  348. } // end of namespace