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.

487 lines
16KB

  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. fCaptureChannels(0),
  32. fPlaybackChannels(0),
  33. fWithMonitorPorts(false)
  34. {}
  35. JackAudioDriver::~JackAudioDriver()
  36. {}
  37. int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  38. {
  39. // Update engine and graph manager state
  40. fEngineControl->fBufferSize = buffer_size;
  41. fGraphManager->SetBufferSize(buffer_size);
  42. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  43. if (!fEngineControl->fTimeOut) {
  44. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  45. }
  46. UpdateLatencies();
  47. // Redirect on slaves drivers...
  48. return JackDriver::SetBufferSize(buffer_size);
  49. }
  50. int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
  51. {
  52. fEngineControl->fSampleRate = sample_rate;
  53. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  54. if (!fEngineControl->fTimeOut) {
  55. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  56. }
  57. return JackDriver::SetSampleRate(sample_rate);
  58. }
  59. int JackAudioDriver::Open(jack_nframes_t buffer_size,
  60. jack_nframes_t samplerate,
  61. bool capturing,
  62. bool playing,
  63. int inchannels,
  64. int outchannels,
  65. bool monitor,
  66. const char* capture_driver_name,
  67. const char* playback_driver_name,
  68. jack_nframes_t capture_latency,
  69. jack_nframes_t playback_latency)
  70. {
  71. fCaptureChannels = inchannels;
  72. fPlaybackChannels = outchannels;
  73. fWithMonitorPorts = monitor;
  74. memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  75. memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  76. memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  77. return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels,
  78. monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  79. }
  80. int JackAudioDriver::Open(bool capturing,
  81. bool playing,
  82. int inchannels,
  83. int outchannels,
  84. bool monitor,
  85. const char* capture_driver_name,
  86. const char* playback_driver_name,
  87. jack_nframes_t capture_latency,
  88. jack_nframes_t playback_latency)
  89. {
  90. fCaptureChannels = inchannels;
  91. fPlaybackChannels = outchannels;
  92. fWithMonitorPorts = monitor;
  93. memset(fCapturePortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  94. memset(fPlaybackPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  95. memset(fMonitorPortList, 0, sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
  96. return JackDriver::Open(capturing, playing, inchannels, outchannels,
  97. monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  98. }
  99. void JackAudioDriver::UpdateLatencies()
  100. {
  101. jack_latency_range_t input_range;
  102. jack_latency_range_t output_range;
  103. jack_latency_range_t monitor_range;
  104. for (int i = 0; i < fCaptureChannels; i++) {
  105. input_range.max = input_range.min = fEngineControl->fBufferSize + fCaptureLatency;
  106. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
  107. }
  108. for (int i = 0; i < fPlaybackChannels; i++) {
  109. output_range.max = output_range.min = fPlaybackLatency;
  110. if (fEngineControl->fSyncMode) {
  111. output_range.max = output_range.min += fEngineControl->fBufferSize;
  112. } else {
  113. output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
  114. }
  115. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
  116. if (fWithMonitorPorts) {
  117. monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
  118. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
  119. }
  120. }
  121. }
  122. int JackAudioDriver::Attach()
  123. {
  124. JackPort* port;
  125. jack_port_id_t port_index;
  126. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  127. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  128. int i;
  129. jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  130. for (i = 0; i < fCaptureChannels; i++) {
  131. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  132. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  133. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  134. jack_error("driver: cannot register port for %s", name);
  135. return -1;
  136. }
  137. port = fGraphManager->GetPort(port_index);
  138. port->SetAlias(alias);
  139. fCapturePortList[i] = port_index;
  140. jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  141. }
  142. for (i = 0; i < fPlaybackChannels; i++) {
  143. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  144. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  145. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  146. jack_error("driver: cannot register port for %s", name);
  147. return -1;
  148. }
  149. port = fGraphManager->GetPort(port_index);
  150. port->SetAlias(alias);
  151. fPlaybackPortList[i] = port_index;
  152. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  153. // Monitor ports
  154. if (fWithMonitorPorts) {
  155. jack_log("Create monitor port");
  156. snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
  157. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
  158. jack_error("Cannot register monitor port for %s", name);
  159. return -1;
  160. } else {
  161. fMonitorPortList[i] = port_index;
  162. }
  163. }
  164. }
  165. UpdateLatencies();
  166. return 0;
  167. }
  168. int JackAudioDriver::Detach()
  169. {
  170. int i;
  171. jack_log("JackAudioDriver::Detach");
  172. for (i = 0; i < fCaptureChannels; i++) {
  173. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
  174. }
  175. for (i = 0; i < fPlaybackChannels; i++) {
  176. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
  177. if (fWithMonitorPorts) {
  178. fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
  179. }
  180. }
  181. return 0;
  182. }
  183. int JackAudioDriver::Write()
  184. {
  185. for (int i = 0; i < fPlaybackChannels; i++) {
  186. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  187. jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
  188. int size = sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
  189. // Monitor ports
  190. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  191. memcpy(GetMonitorBuffer(i), buffer, size);
  192. }
  193. }
  194. return 0;
  195. }
  196. int JackAudioDriver::Process()
  197. {
  198. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  199. }
  200. void JackAudioDriver::ProcessGraphAsync()
  201. {
  202. // Process graph
  203. if (fIsMaster) {
  204. ProcessGraphAsyncMaster();
  205. } else {
  206. ProcessGraphAsyncSlave();
  207. }
  208. }
  209. /*
  210. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  211. synchronize to the end of client graph execution.
  212. */
  213. int JackAudioDriver::ProcessAsync()
  214. {
  215. // Read input buffers for the current cycle
  216. if (Read() < 0) {
  217. jack_error("JackAudioDriver::ProcessAsync: read error, stopping...");
  218. return -1;
  219. }
  220. // Write output buffers from the previous cycle
  221. if (Write() < 0) {
  222. jack_error("JackAudioDriver::ProcessAsync: write error, stopping...");
  223. return -1;
  224. }
  225. // Process graph
  226. ProcessGraphAsync();
  227. // Keep end cycle time
  228. JackDriver::CycleTakeEndTime();
  229. return 0;
  230. }
  231. void JackAudioDriver::ProcessGraphSync()
  232. {
  233. // Process graph
  234. if (fIsMaster) {
  235. if (ProcessGraphSyncMaster() < 0) {
  236. //jack_error("JackAudioDriver::ProcessSync: process error, skip cycle...");
  237. //goto end;
  238. }
  239. } else {
  240. if (ProcessGraphSyncSlave() < 0) {
  241. //jack_error("JackAudioDriver::ProcessSync: process error, skip cycle...");
  242. //goto end;
  243. }
  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::ProcessGraphAsyncMaster()
  269. {
  270. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  271. if (!fEngine->Process(fBeginDateUst, fEndDateUst))
  272. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: Process error");
  273. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0)
  274. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
  275. if (ProcessReadSlaves() < 0)
  276. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
  277. if (ProcessWriteSlaves() < 0)
  278. jack_error("JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
  279. }
  280. void JackAudioDriver::ProcessGraphAsyncSlave()
  281. {
  282. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0)
  283. jack_error("JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
  284. }
  285. int JackAudioDriver::ProcessGraphSyncMaster()
  286. {
  287. int res = 0;
  288. // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
  289. if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
  290. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0) {
  291. jack_error("JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
  292. res = -1;
  293. }
  294. if (ProcessReadSlaves() < 0) {
  295. jack_error("JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
  296. res = -1;
  297. }
  298. if (ProcessWriteSlaves() < 0) {
  299. jack_error("JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
  300. res = -1;
  301. }
  302. if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs) < 0) {
  303. jack_error("JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
  304. res = -1;
  305. }
  306. } else { // Graph not finished: do not activate it
  307. jack_error("JackAudioDriver::ProcessGraphSync: Process error");
  308. res = -1;
  309. }
  310. return res;
  311. }
  312. int JackAudioDriver::ProcessGraphSyncSlave()
  313. {
  314. return fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  315. }
  316. int JackAudioDriver::Start()
  317. {
  318. int res = JackDriver::Start();
  319. if ((res >= 0) && fIsMaster) {
  320. res = StartSlaves();
  321. }
  322. return res;
  323. }
  324. int JackAudioDriver::Stop()
  325. {
  326. int res = JackDriver::Stop();
  327. if (fIsMaster) {
  328. if (StopSlaves() < 0) {
  329. res = -1;
  330. }
  331. }
  332. return res;
  333. }
  334. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  335. {
  336. return fCapturePortList[port_index]
  337. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
  338. : NULL;
  339. }
  340. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  341. {
  342. return fPlaybackPortList[port_index]
  343. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
  344. : NULL;
  345. }
  346. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  347. {
  348. return fPlaybackPortList[port_index]
  349. ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
  350. : NULL;
  351. }
  352. int JackAudioDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  353. {
  354. switch (notify) {
  355. case kLatencyCallback:
  356. HandleLatencyCallback(value1);
  357. break;
  358. default:
  359. JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
  360. break;
  361. }
  362. return 0;
  363. }
  364. void JackAudioDriver::HandleLatencyCallback(int status)
  365. {
  366. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  367. for (int i = 0; i < fCaptureChannels; i++) {
  368. if (mode == JackPlaybackLatency) {
  369. fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
  370. }
  371. }
  372. for (int i = 0; i < fPlaybackChannels; i++) {
  373. if (mode == JackCaptureLatency) {
  374. fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
  375. }
  376. }
  377. }
  378. void JackAudioDriver::SaveConnections()
  379. {
  380. const char** connections;
  381. fConnections.clear();
  382. for (int i = 0; i < fCaptureChannels; ++i) {
  383. if (fCapturePortList[i] && (connections = fGraphManager->GetConnections(fCapturePortList[i])) != 0) {
  384. for (int j = 0; connections[j]; j++) {
  385. fConnections.push_back(make_pair(fGraphManager->GetPort(fCapturePortList[i])->GetName(), connections[j]));
  386. jack_info("Save connection: %s %s", fGraphManager->GetPort(fCapturePortList[i])->GetName(), connections[j]);
  387. }
  388. free(connections);
  389. }
  390. }
  391. for (int i = 0; i < fPlaybackChannels; ++i) {
  392. if (fPlaybackPortList[i] && (connections = fGraphManager->GetConnections(fPlaybackPortList[i])) != 0) {
  393. for (int j = 0; connections[j]; j++) {
  394. fConnections.push_back(make_pair(connections[j], fGraphManager->GetPort(fPlaybackPortList[i])->GetName()));
  395. jack_info("Save connection: %s %s", connections[j], fGraphManager->GetPort(fPlaybackPortList[i])->GetName());
  396. }
  397. free(connections);
  398. }
  399. }
  400. }
  401. void JackAudioDriver::RestoreConnections()
  402. {
  403. list<pair<string, string> >::const_iterator it;
  404. for (it = fConnections.begin(); it != fConnections.end(); it++) {
  405. pair<string, string> connection = *it;
  406. jack_info("Restore connection: %s %s", connection.first.c_str(), connection.second.c_str());
  407. fEngine->PortConnect(fClientControl.fRefNum, connection.first.c_str(), connection.second.c_str());
  408. }
  409. }
  410. } // end of namespace