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.

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