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.

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