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.

446 lines
15KB

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