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.

590 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. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  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 "JackDriver.h"
  18. #include "JackTime.h"
  19. #include "JackError.h"
  20. #include "JackPort.h"
  21. #include "JackGraphManager.h"
  22. #include "JackGlobals.h"
  23. #include "JackEngineControl.h"
  24. #include "JackClientControl.h"
  25. #include "JackLockedEngine.h"
  26. #include "JackTime.h"
  27. #include <math.h>
  28. #include <assert.h>
  29. using namespace std;
  30. namespace Jack
  31. {
  32. JackDriver::JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  33. :fCaptureChannels(0),
  34. fPlaybackChannels(0),
  35. fClientControl(name, jack_client_uuid_generate()),
  36. fWithMonitorPorts(false)
  37. {
  38. assert(strlen(name) < JACK_CLIENT_NAME_SIZE);
  39. fSynchroTable = table;
  40. strcpy(fAliasName, alias);
  41. fEngine = engine;
  42. fGraphManager = NULL;
  43. fBeginDateUst = 0;
  44. fEndDateUst = 0;
  45. fDelayedUsecs = 0.f;
  46. fIsMaster = true;
  47. fIsRunning = false;
  48. }
  49. JackDriver::~JackDriver()
  50. {
  51. jack_log("~JackDriver");
  52. }
  53. int JackDriver::Open()
  54. {
  55. int refnum = -1;
  56. if (fEngine->ClientInternalOpen(fClientControl.fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
  57. jack_error("Cannot allocate internal client for driver");
  58. return -1;
  59. }
  60. fClientControl.fRefNum = refnum;
  61. fClientControl.fActive = true;
  62. fEngineControl->fDriverNum++;
  63. fGraphManager->DirectConnect(fClientControl.fRefNum, fClientControl.fRefNum); // Connect driver to itself for "sync" mode
  64. SetupDriverSync(fClientControl.fRefNum, false);
  65. return 0;
  66. }
  67. int JackDriver::Open(jack_nframes_t buffer_size,
  68. jack_nframes_t sample_rate,
  69. bool capturing,
  70. bool playing,
  71. int inchannels,
  72. int outchannels,
  73. bool monitor,
  74. const char* capture_driver_name,
  75. const char* playback_driver_name,
  76. jack_nframes_t capture_latency,
  77. jack_nframes_t playback_latency)
  78. {
  79. jack_log("JackDriver::Open capture_driver_name = %s", capture_driver_name);
  80. jack_log("JackDriver::Open playback_driver_name = %s", playback_driver_name);
  81. int refnum = -1;
  82. char name_res[JACK_CLIENT_NAME_SIZE + 1];
  83. int status;
  84. // Check name and possibly rename
  85. if (fEngine->ClientCheck(fClientControl.fName, -1, name_res, JACK_PROTOCOL_VERSION, (int)JackNullOption, (int*)&status) < 0) {
  86. jack_error("Client name = %s conflits with another running client", fClientControl.fName);
  87. return -1;
  88. }
  89. strcpy(fClientControl.fName, name_res);
  90. if (fEngine->ClientInternalOpen(fClientControl.fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
  91. jack_error("Cannot allocate internal client for driver");
  92. return -1;
  93. }
  94. fClientControl.fRefNum = refnum;
  95. fClientControl.fActive = true;
  96. fEngineControl->fDriverNum++;
  97. if (buffer_size > 0) {
  98. fEngineControl->fBufferSize = buffer_size;
  99. }
  100. if (sample_rate > 0) {
  101. fEngineControl->fSampleRate = sample_rate;
  102. }
  103. fCaptureLatency = capture_latency;
  104. fPlaybackLatency = playback_latency;
  105. assert(strlen(capture_driver_name) < JACK_CLIENT_NAME_SIZE);
  106. assert(strlen(playback_driver_name) < JACK_CLIENT_NAME_SIZE);
  107. strcpy(fCaptureDriverName, capture_driver_name);
  108. strcpy(fPlaybackDriverName, playback_driver_name);
  109. fEngineControl->UpdateTimeOut();
  110. fGraphManager->SetBufferSize(fEngineControl->fBufferSize);
  111. fGraphManager->DirectConnect(fClientControl.fRefNum, fClientControl.fRefNum); // Connect driver to itself for "sync" mode
  112. SetupDriverSync(fClientControl.fRefNum, false);
  113. return 0;
  114. }
  115. int JackDriver::Close()
  116. {
  117. if (fClientControl.fRefNum >= 0) {
  118. jack_log("JackDriver::Close");
  119. fGraphManager->DirectDisconnect(fClientControl.fRefNum, fClientControl.fRefNum); // Disconnect driver from itself for sync
  120. fClientControl.fActive = false;
  121. fEngineControl->fDriverNum--;
  122. return fEngine->ClientInternalClose(fClientControl.fRefNum, false);
  123. } else {
  124. return -1;
  125. }
  126. }
  127. /*!
  128. In "async" mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
  129. The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
  130. Drivers synchro are setup in "flush" mode if server is "async" and NOT freewheel.
  131. */
  132. void JackDriver::SetupDriverSync(int ref, bool freewheel)
  133. {
  134. if (!freewheel && !fEngineControl->fSyncMode) {
  135. jack_log("JackDriver::SetupDriverSync driver sem in flush mode");
  136. fSynchroTable[ref].SetFlush(true);
  137. } else {
  138. jack_log("JackDriver::SetupDriverSync driver sem in normal mode");
  139. fSynchroTable[ref].SetFlush(false);
  140. }
  141. }
  142. int JackDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  143. {
  144. jack_log("JackDriver::ClientNotify ref = %ld driver = %s name = %s notify = %ld", refnum, fClientControl.fName, name, notify);
  145. switch (notify) {
  146. case kStartFreewheelCallback:
  147. jack_log("JackDriver::kStartFreewheel");
  148. SetupDriverSync(fClientControl.fRefNum, true);
  149. break;
  150. case kStopFreewheelCallback:
  151. jack_log("JackDriver::kStopFreewheel");
  152. SetupDriverSync(fClientControl.fRefNum, false);
  153. break;
  154. }
  155. return 0;
  156. }
  157. bool JackDriver::IsRealTime() const
  158. {
  159. return fEngineControl->fRealTime;
  160. }
  161. void JackDriver::CycleIncTime()
  162. {
  163. fEngineControl->CycleIncTime(fBeginDateUst);
  164. }
  165. void JackDriver::CycleTakeBeginTime()
  166. {
  167. fBeginDateUst = GetMicroSeconds(); // Take callback date here
  168. fEngineControl->CycleIncTime(fBeginDateUst);
  169. }
  170. void JackDriver::CycleTakeEndTime()
  171. {
  172. fEndDateUst = GetMicroSeconds(); // Take end date here
  173. }
  174. JackClientControl* JackDriver::GetClientControl() const
  175. {
  176. return (JackClientControl*)&fClientControl;
  177. }
  178. void JackDriver::NotifyXRun(jack_time_t cur_cycle_begin, float delayed_usecs)
  179. {
  180. fEngineControl->NotifyXRun(cur_cycle_begin, delayed_usecs);
  181. fEngine->NotifyDriverXRun();
  182. }
  183. void JackDriver::NotifyBufferSize(jack_nframes_t buffer_size)
  184. {
  185. fEngine->NotifyBufferSize(buffer_size);
  186. fEngineControl->InitFrameTime();
  187. }
  188. void JackDriver::NotifySampleRate(jack_nframes_t sample_rate)
  189. {
  190. fEngine->NotifySampleRate(sample_rate);
  191. fEngineControl->InitFrameTime();
  192. }
  193. void JackDriver::NotifyFailure(int code, const char* reason)
  194. {
  195. fEngine->NotifyFailure(code, reason);
  196. }
  197. void JackDriver::SetMaster(bool onoff)
  198. {
  199. fIsMaster = onoff;
  200. }
  201. bool JackDriver::GetMaster()
  202. {
  203. return fIsMaster;
  204. }
  205. void JackDriver::AddSlave(JackDriverInterface* slave)
  206. {
  207. fSlaveList.push_back(slave);
  208. }
  209. void JackDriver::RemoveSlave(JackDriverInterface* slave)
  210. {
  211. fSlaveList.remove(slave);
  212. }
  213. int JackDriver::ProcessReadSlaves()
  214. {
  215. int res = 0;
  216. list<JackDriverInterface*>::const_iterator it;
  217. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  218. JackDriverInterface* slave = *it;
  219. if (slave->IsRunning()) {
  220. if (slave->ProcessRead() < 0) {
  221. res = -1;
  222. }
  223. }
  224. }
  225. return res;
  226. }
  227. int JackDriver::ProcessWriteSlaves()
  228. {
  229. int res = 0;
  230. list<JackDriverInterface*>::const_iterator it;
  231. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  232. JackDriverInterface* slave = *it;
  233. if (slave->IsRunning()) {
  234. if (slave->ProcessWrite() < 0) {
  235. res = -1;
  236. }
  237. }
  238. }
  239. return res;
  240. }
  241. int JackDriver::ProcessRead()
  242. {
  243. return (fEngineControl->fSyncMode) ? ProcessReadSync() : ProcessReadAsync();
  244. }
  245. int JackDriver::ProcessWrite()
  246. {
  247. return (fEngineControl->fSyncMode) ? ProcessWriteSync() : ProcessWriteAsync();
  248. }
  249. int JackDriver::ProcessReadSync()
  250. {
  251. return 0;
  252. }
  253. int JackDriver::ProcessWriteSync()
  254. {
  255. return 0;
  256. }
  257. int JackDriver::ProcessReadAsync()
  258. {
  259. return 0;
  260. }
  261. int JackDriver::ProcessWriteAsync()
  262. {
  263. return 0;
  264. }
  265. int JackDriver::Process()
  266. {
  267. return 0;
  268. }
  269. int JackDriver::Attach()
  270. {
  271. return 0;
  272. }
  273. int JackDriver::Detach()
  274. {
  275. return 0;
  276. }
  277. int JackDriver::Read()
  278. {
  279. return 0;
  280. }
  281. int JackDriver::Write()
  282. {
  283. return 0;
  284. }
  285. int JackDriver::Start()
  286. {
  287. if (fIsMaster) {
  288. fEngineControl->InitFrameTime();
  289. }
  290. fIsRunning = true;
  291. return StartSlaves();
  292. }
  293. int JackDriver::Stop()
  294. {
  295. fIsRunning = false;
  296. return StopSlaves();
  297. }
  298. int JackDriver::StartSlaves()
  299. {
  300. int res = 0;
  301. list<JackDriverInterface*>::const_iterator it;
  302. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  303. JackDriverInterface* slave = *it;
  304. if (slave->Start() < 0) {
  305. res = -1;
  306. // XXX: We should attempt to stop all of the slaves that we've
  307. // started here.
  308. break;
  309. }
  310. }
  311. return res;
  312. }
  313. int JackDriver::StopSlaves()
  314. {
  315. int res = 0;
  316. list<JackDriverInterface*>::const_iterator it;
  317. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  318. JackDriverInterface* slave = *it;
  319. if (slave->Stop() < 0) {
  320. res = -1;
  321. }
  322. }
  323. return res;
  324. }
  325. bool JackDriver::IsFixedBufferSize()
  326. {
  327. return true;
  328. }
  329. int JackDriver::SetBufferSize(jack_nframes_t buffer_size)
  330. {
  331. int res = 0;
  332. list<JackDriverInterface*>::const_iterator it;
  333. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  334. JackDriverInterface* slave = *it;
  335. if (slave->SetBufferSize(buffer_size) < 0) {
  336. res = -1;
  337. }
  338. }
  339. return res;
  340. }
  341. int JackDriver::SetSampleRate(jack_nframes_t sample_rate)
  342. {
  343. int res = 0;
  344. list<JackDriverInterface*>::const_iterator it;
  345. for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
  346. JackDriverInterface* slave = *it;
  347. if (slave->SetSampleRate(sample_rate) < 0) {
  348. res = -1;
  349. }
  350. }
  351. return res;
  352. }
  353. bool JackDriver::Initialize()
  354. {
  355. return true;
  356. }
  357. static string RemoveLast(const string& name)
  358. {
  359. return name.substr(0, name.find_last_of(':')); // Remove end of name after last ":"
  360. }
  361. void JackDriver::SaveConnections(int alias)
  362. {
  363. const char** connections;
  364. char alias1[REAL_JACK_PORT_NAME_SIZE+1];
  365. char alias2[REAL_JACK_PORT_NAME_SIZE+1];
  366. char system_alias1[REAL_JACK_PORT_NAME_SIZE+1];
  367. char system_alias2[REAL_JACK_PORT_NAME_SIZE+1];
  368. char* aliases[2];
  369. char* system_aliases[2];
  370. aliases[0] = alias1;
  371. aliases[1] = alias2;
  372. system_aliases[0] = system_alias1;
  373. system_aliases[1] = system_alias2;
  374. fConnections.clear();
  375. for (int i = 0; i < fCaptureChannels; ++i) {
  376. if (fCapturePortList[i] && (connections = fGraphManager->GetConnections(fCapturePortList[i])) != 0) {
  377. if (alias == 0) {
  378. for (int j = 0; connections[j]; j++) {
  379. JackPort* port_id = fGraphManager->GetPort(fCapturePortList[i]);
  380. fConnections.push_back(make_pair(port_id->GetType(), make_pair(port_id->GetName(), connections[j])));
  381. jack_info("Save connection: %s %s", fGraphManager->GetPort(fCapturePortList[i])->GetName(), connections[j]);
  382. }
  383. } else {
  384. int res1 = fGraphManager->GetPort(fCapturePortList[i])->GetAliases(aliases);
  385. string sub_system_name;
  386. if (res1 >= alias) {
  387. sub_system_name = aliases[alias-1];
  388. } else {
  389. sub_system_name = fGraphManager->GetPort(fCapturePortList[i])->GetName();
  390. }
  391. for (int j = 0; connections[j]; j++) {
  392. JackPort* port_id = fGraphManager->GetPort(fGraphManager->GetPort(connections[j]));
  393. int res2 = port_id->GetAliases(system_aliases);
  394. string sub_system;
  395. if (res2 >= alias) {
  396. sub_system = system_aliases[alias-1];
  397. } else {
  398. sub_system = connections[j];
  399. }
  400. fConnections.push_back(make_pair(port_id->GetType(), make_pair(sub_system_name, sub_system)));
  401. jack_info("Save connection: %s %s", sub_system_name.c_str(), sub_system.c_str());
  402. }
  403. }
  404. free(connections);
  405. }
  406. }
  407. for (int i = 0; i < fPlaybackChannels; ++i) {
  408. if (fPlaybackPortList[i] && (connections = fGraphManager->GetConnections(fPlaybackPortList[i])) != 0) {
  409. if (alias == 0) {
  410. for (int j = 0; connections[j]; j++) {
  411. JackPort* port_id = fGraphManager->GetPort(fPlaybackPortList[i]);
  412. fConnections.push_back(make_pair(port_id->GetType(), make_pair(connections[j], port_id->GetName())));
  413. jack_info("Save connection: %s %s", connections[j], fGraphManager->GetPort(fPlaybackPortList[i])->GetName());
  414. }
  415. } else {
  416. int res1 = fGraphManager->GetPort(fPlaybackPortList[i])->GetAliases(aliases);
  417. string sub_system_name;
  418. if (res1 >= alias) {
  419. sub_system_name = aliases[alias-1];
  420. } else {
  421. sub_system_name = fGraphManager->GetPort(fPlaybackPortList[i])->GetName();
  422. }
  423. for (int j = 0; connections[j]; j++) {
  424. JackPort* port_id = fGraphManager->GetPort(fGraphManager->GetPort(connections[j]));
  425. int res2 = port_id->GetAliases(system_aliases);
  426. string sub_name;
  427. if (res2 >= alias) {
  428. sub_name = system_aliases[alias-1];
  429. } else {
  430. sub_name = connections[j];
  431. }
  432. fConnections.push_back(make_pair(port_id->GetType(), make_pair(sub_name, sub_system_name)));
  433. jack_info("Save connection: %s %s", sub_name.c_str(), sub_system_name.c_str());
  434. }
  435. }
  436. free(connections);
  437. }
  438. }
  439. }
  440. string JackDriver::MatchPortName(const char* name, const char** ports, int alias, const std::string& type)
  441. {
  442. char alias1[REAL_JACK_PORT_NAME_SIZE+1];
  443. char alias2[REAL_JACK_PORT_NAME_SIZE+1];
  444. char* aliases[2];
  445. aliases[0] = alias1;
  446. aliases[1] = alias2;
  447. for (int i = 0; ports && ports[i]; ++i) {
  448. jack_port_id_t port_id2 = fGraphManager->GetPort(ports[i]);
  449. JackPort* port2 = (port_id2 != NO_PORT) ? fGraphManager->GetPort(port_id2) : NULL;
  450. if (port2) {
  451. int res = port2->GetAliases(aliases);
  452. string name_str;
  453. if (res >= alias) {
  454. name_str = string(aliases[alias-1]);
  455. } else {
  456. name_str = string(ports[i]);
  457. }
  458. string sub_name = RemoveLast(name);
  459. if ((name_str.find(sub_name) != string::npos) && (type == string(port2->GetType()))) {
  460. return name_str;
  461. }
  462. }
  463. }
  464. return "";
  465. }
  466. void JackDriver::LoadConnections(int alias, bool full_name)
  467. {
  468. list<pair<string, pair<string, string> > >::const_iterator it;
  469. if (full_name) {
  470. for (it = fConnections.begin(); it != fConnections.end(); it++) {
  471. pair<string, string> connection = (*it).second;
  472. jack_info("Load connection: %s %s", connection.first.c_str(), connection.second.c_str());
  473. fEngine->PortConnect(fClientControl.fRefNum, connection.first.c_str(), connection.second.c_str());
  474. }
  475. } else {
  476. const char** inputs = fGraphManager->GetPorts(NULL, NULL, JackPortIsInput);
  477. const char** outputs = fGraphManager->GetPorts(NULL, NULL, JackPortIsOutput);
  478. for (it = fConnections.begin(); it != fConnections.end(); it++) {
  479. pair<string, string> connection = (*it).second;
  480. string real_input = MatchPortName(connection.first.c_str(), outputs, alias, (*it).first);
  481. string real_output = MatchPortName(connection.second.c_str(), inputs, alias, (*it).first);
  482. if ((real_input != "") && (real_output != "")) {
  483. jack_info("Load connection: %s %s", real_input.c_str(), real_output.c_str());
  484. fEngine->PortConnect(fClientControl.fRefNum, real_input.c_str(), real_output.c_str());
  485. }
  486. }
  487. // Wait for connection change
  488. if (fGraphManager->IsPendingChange()) {
  489. JackSleep(int(fEngineControl->fPeriodUsecs * 1.1f));
  490. }
  491. if (inputs) {
  492. free(inputs);
  493. }
  494. if (outputs) {
  495. free(outputs);
  496. }
  497. }
  498. }
  499. int JackDriver::ResumeRefNum()
  500. {
  501. return fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  502. }
  503. int JackDriver::SuspendRefNum()
  504. {
  505. return fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs);
  506. }
  507. } // end of namespace