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.

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