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.

526 lines
18KB

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