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.

500 lines
17KB

  1. /*
  2. Copyright (C) 2004-2006 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  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. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackDebugClient.h"
  16. #include "JackError.h"
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <sstream>
  20. #include <fstream>
  21. #include <string>
  22. #include <time.h>
  23. using namespace std;
  24. namespace Jack
  25. {
  26. JackDebugClient::JackDebugClient(JackClient * client)
  27. {
  28. fTotalPortNumber = 1; // The total number of port opened and maybe closed. Historical view.
  29. fOpenPortNumber = 0; // The current number of opened port.
  30. fIsActivated = 0;
  31. fIsDeactivated = 0;
  32. fIsClosed = 0;
  33. fClient = client;
  34. }
  35. JackDebugClient::~JackDebugClient()
  36. {
  37. fTotalPortNumber--; // fTotalPortNumber start at 1
  38. *fStream << endl << endl << "----------------------------------- JackDebugClient summary ------------------------------- " << endl << endl;
  39. *fStream << "Client flags ( 1:yes / 0:no ) :" << endl;
  40. *fStream << setw(5) << "- Client call activated : " << fIsActivated << endl;
  41. *fStream << setw(5) << "- Client call deactivated : " << fIsDeactivated << endl;
  42. *fStream << setw(5) << "- Client call closed : " << fIsClosed << endl;
  43. *fStream << setw(5) << "- Total number of instantiated port : " << fTotalPortNumber << endl;
  44. *fStream << setw(5) << "- Number of port remaining open when exiting client : " << fOpenPortNumber << endl;
  45. if (fOpenPortNumber != 0)
  46. *fStream << "!!! WARNING !!! Some ports have not been unregistrated ! Incorrect exiting !" << endl;
  47. if (fIsDeactivated != fIsActivated)
  48. *fStream << "!!! ERROR !!! Client seem do not perform symetric activation-deactivation ! (not the same number of activate and deactivate)" << endl;
  49. if (fIsClosed == 0)
  50. *fStream << "!!! ERROR !!! Client have not been closed with jack_client_close() !" << endl;
  51. *fStream << endl << endl << "---------------------------- JackDebugClient detailed port summary ------------------------ " << endl << endl;
  52. //for (int i = 0; i < fTotalPortNumber ; i++) {
  53. for (int i = 1; i <= fTotalPortNumber ; i++) {
  54. *fStream << endl << "Port index (internal debug test value) : " << i << endl;
  55. *fStream << setw(5) << "- Name : " << fPortList[i].name << endl;
  56. *fStream << setw(5) << "- idport : " << fPortList[i].idport << endl;
  57. *fStream << setw(5) << "- IsConnected : " << fPortList[i].IsConnected << endl;
  58. *fStream << setw(5) << "- IsUnregistrated : " << fPortList[i].IsUnregistrated << endl;
  59. if (fPortList[i].IsUnregistrated == 0)
  60. *fStream << "!!! WARNING !!! Port have not been unregistrated ! Incorrect exiting !" << endl;
  61. }
  62. *fStream << "delete object JackDebugClient : end of tracing" << endl;
  63. delete fStream;
  64. delete fClient;
  65. }
  66. int JackDebugClient::Open(const char* name)
  67. {
  68. int res;
  69. res = fClient->Open(name);
  70. char provstr[256];
  71. char buffer[256];
  72. time_t curtime;
  73. struct tm *loctime;
  74. /* Get the current time. */
  75. curtime = time (NULL);
  76. /* Convert it to local time representation. */
  77. loctime = localtime (&curtime);
  78. strftime (buffer, 256, "%I-%M", loctime);
  79. sprintf(provstr, "JackClientDebug-%s-%s.log", name, buffer);
  80. fStream = new ofstream(provstr, ios_base::ate);
  81. if (fStream->is_open()) {
  82. if (res == -1) {
  83. *fStream << "Trying to open client with name '" << name << "' with bad result (client not opened)." << res << endl;
  84. } else {
  85. *fStream << "Open client with name '" << name << "'." << endl;
  86. }
  87. } else {
  88. JackLog("JackDebugClient::Open : cannot open log file\n");
  89. }
  90. strcpy(fClientName, name);
  91. return res;
  92. }
  93. int JackDebugClient::Close()
  94. {
  95. fIsClosed++;
  96. *fStream << "Client '" << fClientName << "' was closed" << endl;
  97. return fClient->Close();
  98. }
  99. void JackDebugClient::CheckClient() const
  100. {
  101. if (fIsClosed > 0) {
  102. *fStream << "!!! ERROR !!! : Accessing a client '" << fClientName << "' already closed !" << endl;
  103. *fStream << "This is likely to cause crash !'" << endl;
  104. }
  105. }
  106. pthread_t JackDebugClient::GetThreadID()
  107. {
  108. CheckClient();
  109. return fClient->GetThreadID();
  110. }
  111. JackGraphManager* JackDebugClient::GetGraphManager() const
  112. {
  113. CheckClient();
  114. return fClient->GetGraphManager();
  115. }
  116. JackEngineControl* JackDebugClient::GetEngineControl() const
  117. {
  118. CheckClient();
  119. return fClient->GetEngineControl();
  120. }
  121. /*!
  122. \brief Notification received from the server.
  123. */
  124. int JackDebugClient::ClientNotify(int refnum, const char* name, int notify, int sync, int value)
  125. {
  126. CheckClient();
  127. return fClient->ClientNotify( refnum, name, notify, sync, value);
  128. }
  129. int JackDebugClient::Activate()
  130. {
  131. CheckClient();
  132. int res;
  133. res = fClient->Activate();
  134. fIsActivated++;
  135. if (fIsDeactivated)
  136. *fStream << "Client '" << fClientName << "' call activate a new time (it already call 'activate' previously)." << endl;
  137. *fStream << "Client '" << fClientName << "' Activated" << endl;
  138. if (res != 0)
  139. *fStream << "Client '" << fClientName << "' try to activate but server return " << res << " ." << endl;
  140. return res;
  141. }
  142. int JackDebugClient::Deactivate()
  143. {
  144. CheckClient();
  145. int res;
  146. res = fClient->Deactivate();
  147. fIsDeactivated++;
  148. if (fIsActivated == 0)
  149. *fStream << "Client '" << fClientName << "' deactivate while it hasn't been previoulsy activated !" << endl;
  150. *fStream << "Client '" << fClientName << "' Deactivated" << endl;
  151. if (res != 0)
  152. *fStream << "Client '" << fClientName << "' try to deactivate but server return " << res << " ." << endl;
  153. return res;
  154. }
  155. //-----------------
  156. // Port management
  157. //-----------------
  158. int JackDebugClient::PortRegister(const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size)
  159. {
  160. CheckClient();
  161. int res;
  162. res = fClient->PortRegister(port_name, port_type, flags, buffer_size);
  163. if (res <= 0) {
  164. *fStream << "Client '" << fClientName << "' try port register ('" << port_name << "') and server return error " << res << " ." << endl;
  165. } else {
  166. if (fTotalPortNumber < MAX_PORT_HISTORY) {
  167. fPortList[fTotalPortNumber].idport = res;
  168. strcpy(fPortList[fTotalPortNumber].name, port_name);
  169. fPortList[fTotalPortNumber].IsConnected = 0;
  170. fPortList[fTotalPortNumber].IsUnregistrated = 0;
  171. } else {
  172. *fStream << "!!! WARNING !!! History is full : no more port history will be recorded." << endl;
  173. }
  174. fTotalPortNumber++;
  175. fOpenPortNumber++;
  176. *fStream << "Client '" << fClientName << "' port register with portname '" << port_name << " port " << res << "' ." << endl;
  177. }
  178. return res;
  179. }
  180. int JackDebugClient::PortUnRegister(jack_port_id_t port_index)
  181. {
  182. CheckClient();
  183. int res;
  184. res = fClient->PortUnRegister(port_index);
  185. fOpenPortNumber--;
  186. int i;
  187. for (i = (fTotalPortNumber - 1); i >= 0; i--) { // We search the record into the history
  188. if (fPortList[i].idport == port_index) { // We found the last record
  189. if (fPortList[i].IsUnregistrated != 0)
  190. *fStream << "!!! ERROR !!! : '" << fClientName << "' id deregistering port '" << fPortList[i].name << "' that have already been unregistered !" << endl;
  191. fPortList[i].IsUnregistrated++;
  192. break;
  193. }
  194. }
  195. if (i == 0) // Port is not found
  196. *fStream << "JackClientDebug : PortUnregister : port " << port_index << " was not previously registered !" << endl;
  197. if (res != 0)
  198. *fStream << "Client '" << fClientName << "' try to do PortUnregister and server return " << res << " )." << endl;
  199. *fStream << "Client '" << fClientName << "' unregister port '" << port_index << "'." << endl;
  200. return res;
  201. }
  202. int JackDebugClient::PortConnect(const char* src, const char* dst)
  203. {
  204. CheckClient();
  205. if (!fIsActivated)
  206. *fStream << "!!! ERROR !!! Trying to connect a port ( " << src << " to " << dst << ") while the client has not been activated !" << endl;
  207. int res;
  208. int i;
  209. res = fClient->PortConnect( src, dst);
  210. for (i = (fTotalPortNumber - 1); i >= 0; i--) { // We search the record into the history
  211. if (strcmp(fPortList[i].name, src) == 0) { // We found the last record in sources
  212. if (fPortList[i].IsUnregistrated != 0)
  213. *fStream << "!!! ERROR !!! Connecting port " << src << " previoulsy unregistered !" << endl;
  214. fPortList[i].IsConnected++;
  215. *fStream << "Connecting port " << src << " to " << dst << ". ";
  216. break;
  217. } else if (strcmp(fPortList[i].name, dst) == 0 ) { // We found the record in dest
  218. if (fPortList[i].IsUnregistrated != 0)
  219. *fStream << "!!! ERROR !!! Connecting port " << dst << " previoulsy unregistered !" << endl;
  220. fPortList[i].IsConnected++;
  221. *fStream << "Connecting port " << src << " to " << dst << ". ";
  222. break;
  223. }
  224. }
  225. if (i == 0) // Port is not found
  226. *fStream << "JackClientDebug : PortConnect : port was not found in debug database !" << endl;
  227. if (res != 0)
  228. *fStream << "Client '" << fClientName << "' try to do PortConnect but server return " << res << " ." << endl;
  229. //*fStream << "Client Port Connect done with names" << endl;
  230. return res;
  231. }
  232. int JackDebugClient::PortDisconnect(const char* src, const char* dst)
  233. {
  234. CheckClient();
  235. if (!fIsActivated)
  236. *fStream << "!!! ERROR !!! Trying to disconnect a port ( " << src << " to " << dst << ") while the client has not been activated !" << endl;
  237. int res;
  238. res = fClient->PortDisconnect( src, dst);
  239. int i;
  240. for (i = (fTotalPortNumber - 1); i >= 0; i--) { // We search the record into the history
  241. if (strcmp(fPortList[i].name, src) == 0) { // We found the record in sources
  242. if (fPortList[i].IsUnregistrated != 0)
  243. *fStream << "!!! ERROR !!! : Disconnecting port " << src << " previoulsy unregistered !" << endl;
  244. fPortList[i].IsConnected--;
  245. *fStream << "disconnecting port " << src << ". ";
  246. break;
  247. } else if (strcmp(fPortList[i].name, dst) == 0 ) { // We found the record in dest
  248. if (fPortList[i].IsUnregistrated != 0)
  249. *fStream << "!!! ERROR !!! : Disonnecting port " << dst << " previoulsy unregistered !" << endl;
  250. fPortList[i].IsConnected--;
  251. *fStream << "disconnecting port " << dst << ". ";
  252. break;
  253. }
  254. }
  255. if (i == 0) // Port is not found
  256. *fStream << "JackClientDebug : PortDisConnect : port was not found in debug database !" << endl;
  257. if (res != 0)
  258. *fStream << "Client '" << fClientName << "' try to do PortDisconnect but server return " << res << " ." << endl;
  259. //*fStream << "Client Port Disconnect done." << endl;
  260. return res;
  261. }
  262. int JackDebugClient::PortConnect(jack_port_id_t src, jack_port_id_t dst)
  263. {
  264. CheckClient();
  265. if (!fIsActivated)
  266. *fStream << "!!! ERROR !!! : Trying to connect port " << src << " to " << dst << " while the client has not been activated !" << endl;
  267. int res;
  268. res = fClient->PortConnect(src, dst);
  269. int i;
  270. for (i = (fTotalPortNumber - 1); i >= 0; i--) { // We search the record into the history
  271. if (fPortList[i].idport == src) { // We found the record in sources
  272. if (fPortList[i].IsUnregistrated != 0)
  273. *fStream << "!!! ERROR !!! : Connecting port " << src << " previoulsy unregistered !" << endl;
  274. fPortList[i].IsConnected++;
  275. *fStream << "Connecting port " << src << ". ";
  276. break;
  277. } else if (fPortList[i].idport == dst) { // We found the record in dest
  278. if (fPortList[i].IsUnregistrated != 0)
  279. *fStream << "!!! ERROR !!! : Connecting port " << dst << " previoulsy unregistered !" << endl;
  280. fPortList[i].IsConnected++;
  281. *fStream << "Connecting port " << dst << ". ";
  282. break;
  283. }
  284. }
  285. if (i == 0) // Port is not found
  286. *fStream << "JackClientDebug : PortConnect : port was not found in debug database !" << endl;
  287. if (res == -1)
  288. *fStream << "Client '" << fClientName << "' try to do Portconnect but server return " << res << " ." << endl;
  289. //*fStream << "Client Port Connect with ID done." << endl;
  290. return res;
  291. }
  292. int JackDebugClient::PortDisconnect(jack_port_id_t src)
  293. {
  294. CheckClient();
  295. if (!fIsActivated)
  296. *fStream << "!!! ERROR !!! : Trying to disconnect port " << src << " while that client has not been activated !" << endl;
  297. int res;
  298. res = fClient->PortDisconnect(src);
  299. int i;
  300. for (i = (fTotalPortNumber - 1); i >= 0; i--) { // We search the record into the history
  301. if (fPortList[i].idport == src) { // We found the record in sources
  302. if (fPortList[i].IsUnregistrated != 0)
  303. *fStream << "!!! ERROR !!! : Disconnecting port " << src << " previoulsy unregistered !" << endl;
  304. fPortList[i].IsConnected--;
  305. *fStream << "Disconnecting port " << src << ". " << endl;
  306. break;
  307. }
  308. }
  309. if (i == 0) // Port is not found
  310. *fStream << "JackClientDebug : PortDisconnect : port was not found in debug database !" << endl;
  311. if (res != 0)
  312. *fStream << "Client '" << fClientName << "' try to do PortDisconnect but server return " << res << " ." << endl;
  313. //*fStream << "Client Port Disconnect with ID done." << endl;
  314. return res;
  315. }
  316. int JackDebugClient::PortIsMine(jack_port_id_t port_index)
  317. {
  318. CheckClient();
  319. return fClient->PortIsMine(port_index);
  320. }
  321. //--------------------
  322. // Context management
  323. //--------------------
  324. int JackDebugClient::SetBufferSize(jack_nframes_t buffer_size)
  325. {
  326. CheckClient();
  327. return fClient->SetBufferSize(buffer_size);
  328. }
  329. int JackDebugClient::SetFreeWheel(int onoff)
  330. {
  331. CheckClient();
  332. return fClient->SetFreeWheel(onoff);
  333. }
  334. /*
  335. ShutDown is called:
  336. - from the RT thread when Execute method fails
  337. - possibly from a "closed" notification channel
  338. (Not needed since the synch object used (Sema of Fifo will fails when server quits... see ShutDown))
  339. */
  340. void JackDebugClient::ShutDown()
  341. {
  342. fClient->ShutDown();
  343. }
  344. //---------------------
  345. // Transport management
  346. //---------------------
  347. int JackDebugClient::ReleaseTimebase()
  348. {
  349. CheckClient();
  350. return fClient->ReleaseTimebase();
  351. }
  352. int JackDebugClient::SetSyncCallback(JackSyncCallback sync_callback, void* arg)
  353. {
  354. CheckClient();
  355. return fClient->SetSyncCallback(sync_callback, arg);
  356. }
  357. int JackDebugClient::SetSyncTimeout(jack_time_t timeout)
  358. {
  359. CheckClient();
  360. return fClient->SetSyncTimeout(timeout);
  361. }
  362. int JackDebugClient::SetTimebaseCallback(int conditional, JackTimebaseCallback timebase_callback, void* arg)
  363. {
  364. CheckClient();
  365. return fClient->SetTimebaseCallback( conditional, timebase_callback, arg);
  366. }
  367. int JackDebugClient::TransportLocate(jack_nframes_t frame)
  368. {
  369. CheckClient();
  370. return fClient->TransportLocate(frame);
  371. }
  372. jack_transport_state_t JackDebugClient::TransportQuery(jack_position_t* pos)
  373. {
  374. CheckClient();
  375. return fClient->TransportQuery(pos);
  376. }
  377. jack_nframes_t JackDebugClient::GetCurrentTransportFrame()
  378. {
  379. CheckClient();
  380. return fClient->GetCurrentTransportFrame();
  381. }
  382. int JackDebugClient::TransportReposition(jack_position_t* pos)
  383. {
  384. CheckClient();
  385. return fClient->TransportReposition(pos);
  386. }
  387. void JackDebugClient::TransportStart()
  388. {
  389. CheckClient();
  390. fClient->TransportStart();
  391. }
  392. void JackDebugClient::TransportStop()
  393. {
  394. CheckClient();
  395. fClient->TransportStop();
  396. }
  397. //---------------------
  398. // Callback management
  399. //---------------------
  400. void JackDebugClient::OnShutdown(JackShutdownCallback callback, void *arg)
  401. {
  402. CheckClient();
  403. fClient->OnShutdown(callback, arg);
  404. }
  405. int JackDebugClient::SetProcessCallback(JackProcessCallback callback, void *arg)
  406. {
  407. CheckClient();
  408. return fClient->SetProcessCallback( callback, arg);
  409. }
  410. int JackDebugClient::SetXRunCallback(JackXRunCallback callback, void *arg)
  411. {
  412. CheckClient();
  413. return fClient->SetXRunCallback(callback, arg);
  414. }
  415. int JackDebugClient::SetInitCallback(JackThreadInitCallback callback, void *arg)
  416. {
  417. CheckClient();
  418. return fClient->SetInitCallback(callback, arg);
  419. }
  420. int JackDebugClient::SetGraphOrderCallback(JackGraphOrderCallback callback, void *arg)
  421. {
  422. CheckClient();
  423. return fClient->SetGraphOrderCallback(callback, arg);
  424. }
  425. int JackDebugClient::SetBufferSizeCallback(JackBufferSizeCallback callback, void *arg)
  426. {
  427. CheckClient();
  428. return fClient->SetBufferSizeCallback(callback, arg);
  429. }
  430. int JackDebugClient::SetFreewheelCallback(JackFreewheelCallback callback, void *arg)
  431. {
  432. CheckClient();
  433. return fClient->SetFreewheelCallback(callback, arg);
  434. }
  435. int JackDebugClient::SetPortRegistrationCallback(JackPortRegistrationCallback callback, void *arg)
  436. {
  437. CheckClient();
  438. return fClient->SetPortRegistrationCallback(callback, arg);
  439. }
  440. JackClientControl* JackDebugClient::GetClientControl() const
  441. {
  442. CheckClient();
  443. return fClient->GetClientControl();
  444. }
  445. } // end of namespace