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.

492 lines
17KB

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