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.

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