jack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #include <python2.7/Python.h>
  2. #include <jack/jack.h>
  3. typedef struct {
  4. PyObject_HEAD
  5. jack_client_t* client;
  6. PyObject* port_registered_callback;
  7. PyObject* port_registered_callback_argument;
  8. PyObject* port_renamed_callback;
  9. PyObject* port_renamed_callback_argument;
  10. PyObject* port_unregistered_callback;
  11. PyObject* port_unregistered_callback_argument;
  12. PyObject* shutdown_callback;
  13. PyObject* shutdown_callback_argument;
  14. } Client;
  15. typedef struct {
  16. PyObject_HEAD
  17. jack_port_t* port;
  18. } Port;
  19. static PyObject* error;
  20. static PyObject* failure;
  21. static PyObject* connection_exists;
  22. static PyTypeObject port_type = {
  23. PyObject_HEAD_INIT(NULL)
  24. };
  25. static void jack_registration_callback(jack_port_id_t port_id, int registered, void* arg)
  26. {
  27. // register: non-zero if the port is being registered, zero if the port is being unregistered
  28. Client* client = (Client*)arg;
  29. PyObject* callback;
  30. PyObject* callback_argument;
  31. if(registered) {
  32. callback = client->port_registered_callback;
  33. callback_argument = client->port_registered_callback_argument;
  34. } else {
  35. callback = client->port_unregistered_callback;
  36. callback_argument = client->port_unregistered_callback_argument;
  37. }
  38. if(callback) {
  39. // Ensure that the current thread is ready to call the Python API.
  40. // No Python API calls are allowed before this call.
  41. PyGILState_STATE gil_state = PyGILState_Ensure();
  42. Port* port = PyObject_New(Port, &port_type);
  43. port->port = jack_port_by_id(client->client, port_id);
  44. // 'O' increases reference count
  45. PyObject* callback_argument_list;
  46. if(callback_argument) {
  47. callback_argument_list = Py_BuildValue("(O,O)", (PyObject*)port, callback_argument);
  48. } else {
  49. callback_argument_list = Py_BuildValue("(O)", (PyObject*)port);
  50. }
  51. PyObject* result = PyObject_CallObject(callback, callback_argument_list);
  52. Py_DECREF(callback_argument_list);
  53. if(!result) {
  54. PyErr_PrintEx(0);
  55. } else {
  56. Py_DECREF(result);
  57. }
  58. // Release the thread. No Python API calls are allowed beyond this point.
  59. PyGILState_Release(gil_state);
  60. }
  61. }
  62. // typedef int (*JackPortRenameCallback)(jack_port_id_t port, const char* old_name, const char* new_name, void *arg);
  63. static int jack_port_renamed_callback(jack_port_id_t port_id, const char* old_name, const char* new_name, void* arg)
  64. {
  65. int return_code = 0;
  66. Client* client = (Client*)arg;
  67. if(client->port_renamed_callback) {
  68. // Ensure that the current thread is ready to call the Python API.
  69. // No Python API calls are allowed before this call.
  70. PyGILState_STATE gil_state = PyGILState_Ensure();
  71. Port* port = PyObject_New(Port, &port_type);
  72. port->port = jack_port_by_id(client->client, port_id);
  73. // 'O' increases reference count
  74. PyObject* callback_argument_list = Py_BuildValue(
  75. "(O,s,s,O)",
  76. (PyObject*)port,
  77. old_name,
  78. new_name,
  79. client->port_renamed_callback_argument
  80. );
  81. PyObject* result = PyObject_CallObject(client->port_renamed_callback, callback_argument_list);
  82. Py_DECREF(callback_argument_list);
  83. if(!result) {
  84. PyErr_PrintEx(0);
  85. return_code = -1;
  86. } else {
  87. Py_DECREF(result);
  88. return_code = 0;
  89. }
  90. // Release the thread. No Python API calls are allowed beyond this point.
  91. PyGILState_Release(gil_state);
  92. }
  93. return return_code;
  94. }
  95. // typedef void(* JackInfoShutdownCallback)(jack_status_t code, const char *reason, void *arg)
  96. static void jack_shutdown_callback(jack_status_t code, const char* reason, void* arg)
  97. {
  98. Client* client = (Client*)arg;
  99. if(client->shutdown_callback) {
  100. // Ensure that the current thread is ready to call the Python API.
  101. // No Python API calls are allowed before this call.
  102. PyGILState_STATE gil_state = PyGILState_Ensure();
  103. // 'O' increases reference count
  104. PyObject* callback_argument_list = NULL;
  105. if(client->shutdown_callback_argument) {
  106. callback_argument_list = Py_BuildValue(
  107. "(s,O)",
  108. reason,
  109. client->shutdown_callback_argument
  110. );
  111. } else {
  112. callback_argument_list = Py_BuildValue(
  113. "(s)",
  114. reason
  115. );
  116. }
  117. PyObject* result = PyObject_CallObject(client->shutdown_callback, callback_argument_list);
  118. Py_DECREF(callback_argument_list);
  119. if(!result) {
  120. PyErr_PrintEx(0);
  121. } else {
  122. Py_DECREF(result);
  123. }
  124. // Release the thread. No Python API calls are allowed beyond this point.
  125. PyGILState_Release(gil_state);
  126. }
  127. }
  128. static PyObject* client___new__(PyTypeObject* type, PyObject* args, PyObject* kwargs)
  129. {
  130. Client* self = (Client*)type->tp_alloc(type, 0);
  131. if(self) {
  132. const char* name;
  133. if(!PyArg_ParseTuple(args, "s", &name)) {
  134. return NULL;
  135. }
  136. jack_status_t status;
  137. self->client = jack_client_open(name, JackNullOption, &status);
  138. if(!self->client) {
  139. if(status & JackFailure) {
  140. PyErr_SetString(failure, "Overall operation failed.");
  141. }
  142. return NULL;
  143. }
  144. self->port_registered_callback = NULL;
  145. self->port_unregistered_callback = NULL;
  146. int error_code = jack_set_port_registration_callback(
  147. self->client,
  148. jack_registration_callback,
  149. (void*)self
  150. );
  151. if(error_code) {
  152. PyErr_SetString(error, "Could not set port registration callback.");
  153. return NULL;
  154. }
  155. self->port_renamed_callback = NULL;
  156. self->port_renamed_callback_argument = NULL;
  157. error_code = jack_set_port_rename_callback(
  158. self->client,
  159. jack_port_renamed_callback,
  160. (void*)self
  161. );
  162. if(error_code) {
  163. PyErr_SetString(error, "Could not set port rename callback.");
  164. return NULL;
  165. }
  166. self->shutdown_callback = NULL;
  167. self->shutdown_callback_argument = NULL;
  168. jack_on_info_shutdown(
  169. self->client,
  170. jack_shutdown_callback,
  171. (void*)self
  172. );
  173. }
  174. return (PyObject*)self;
  175. }
  176. static PyObject* client_activate(Client* self) {
  177. int error_code = jack_activate(self->client);
  178. if(error_code) {
  179. PyErr_SetString(error, "");
  180. return NULL;
  181. } else {
  182. Py_INCREF(Py_None);
  183. return Py_None;
  184. }
  185. }
  186. #include <stdio.h>
  187. static PyObject* client_connect(Client* self, PyObject* args)
  188. {
  189. PyObject *source_port_python, *target_port_python;
  190. // The object’s reference count is not increased.
  191. if(!PyArg_ParseTuple(args, "O!O!", &port_type, &source_port_python, &port_type, &target_port_python)) {
  192. return NULL;
  193. }
  194. Port* source_port = (Port*) source_port_python;
  195. Port* target_port = (Port*) target_port_python;
  196. int return_code = jack_connect(
  197. self->client,
  198. jack_port_name(source_port->port),
  199. jack_port_name(target_port->port)
  200. );
  201. if(return_code) {
  202. if(return_code == EEXIST) {
  203. PyErr_SetString(connection_exists, "The specified ports are already connected.");
  204. }
  205. return NULL;
  206. } else {
  207. Py_INCREF(Py_None);
  208. return Py_None;
  209. }
  210. }
  211. static PyObject* client_get_name(Client* self)
  212. {
  213. return (PyObject*)PyString_FromString(jack_get_client_name(self->client));
  214. }
  215. static PyObject* client_get_ports(Client* self)
  216. {
  217. PyObject* ports = PyList_New(0);
  218. if(!ports) {
  219. return NULL;
  220. }
  221. const char** port_names = jack_get_ports(self->client, NULL, NULL, 0);
  222. int port_index;
  223. for(port_index = 0; port_names[port_index] != NULL; port_index++) {
  224. Port* port = PyObject_New(Port, &port_type);
  225. port->port = jack_port_by_name(self->client, port_names[port_index]);
  226. if(PyList_Append(ports, (PyObject*)port)) {
  227. return NULL;
  228. }
  229. }
  230. jack_free(port_names);
  231. return ports;
  232. }
  233. static PyObject* client_set_port_registered_callback(Client* self, PyObject* args)
  234. {
  235. PyObject* callback = 0;
  236. PyObject* callback_argument = 0;
  237. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  238. return NULL;
  239. }
  240. if(!PyCallable_Check(callback)) {
  241. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  242. return NULL;
  243. }
  244. Py_XINCREF(callback);
  245. Py_XDECREF(self->port_registered_callback);
  246. self->port_registered_callback = callback;
  247. Py_XINCREF(callback_argument);
  248. Py_XDECREF(self->port_registered_callback_argument);
  249. self->port_registered_callback_argument = callback_argument;
  250. Py_INCREF(Py_None);
  251. return Py_None;
  252. }
  253. static PyObject* client_set_port_unregistered_callback(Client* self, PyObject* args)
  254. {
  255. PyObject* callback = 0;
  256. PyObject* callback_argument = 0;
  257. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  258. return NULL;
  259. }
  260. if(!PyCallable_Check(callback)) {
  261. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  262. return NULL;
  263. }
  264. Py_XINCREF(callback);
  265. Py_XDECREF(self->port_unregistered_callback);
  266. self->port_unregistered_callback = callback;
  267. Py_XINCREF(callback_argument);
  268. Py_XDECREF(self->port_unregistered_callback_argument);
  269. self->port_unregistered_callback_argument = callback_argument;
  270. Py_INCREF(Py_None);
  271. return Py_None;
  272. }
  273. static PyObject* client_set_port_renamed_callback(Client* self, PyObject* args)
  274. {
  275. PyObject* callback = 0;
  276. PyObject* callback_argument = 0;
  277. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  278. return NULL;
  279. }
  280. if(!PyCallable_Check(callback)) {
  281. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  282. return NULL;
  283. }
  284. Py_XINCREF(callback);
  285. Py_XDECREF(self->port_renamed_callback);
  286. self->port_renamed_callback = callback;
  287. Py_XINCREF(callback_argument);
  288. Py_XDECREF(self->port_renamed_callback_argument);
  289. self->port_renamed_callback_argument = callback_argument;
  290. Py_INCREF(Py_None);
  291. return Py_None;
  292. }
  293. static PyObject* client_set_shutdown_callback(Client* self, PyObject* args)
  294. {
  295. PyObject* callback = 0;
  296. PyObject* callback_argument = 0;
  297. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  298. return NULL;
  299. }
  300. if(!PyCallable_Check(callback)) {
  301. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  302. return NULL;
  303. }
  304. Py_XINCREF(callback);
  305. Py_XDECREF(self->shutdown_callback);
  306. self->shutdown_callback = callback;
  307. Py_XINCREF(callback_argument);
  308. Py_XDECREF(self->shutdown_callback_argument);
  309. self->shutdown_callback_argument = callback_argument;
  310. Py_INCREF(Py_None);
  311. return Py_None;
  312. }
  313. static void client_dealloc(Client* self)
  314. {
  315. jack_client_close(self->client);
  316. self->ob_type->tp_free((PyObject*)self);
  317. }
  318. static PyMethodDef client_methods[] = {
  319. {
  320. "activate",
  321. (PyCFunction)client_activate,
  322. METH_NOARGS,
  323. "Tell the Jack server that the program is ready to start processing audio.",
  324. },
  325. {
  326. "connect",
  327. (PyCFunction)client_connect,
  328. METH_VARARGS,
  329. "Establish a connection between two ports.",
  330. },
  331. {
  332. "get_name",
  333. (PyCFunction)client_get_name,
  334. METH_NOARGS,
  335. "Return client's actual name.",
  336. },
  337. {
  338. "get_ports",
  339. (PyCFunction)client_get_ports,
  340. METH_NOARGS,
  341. "Return list of ports.",
  342. },
  343. {
  344. "set_port_registered_callback",
  345. (PyCFunction)client_set_port_registered_callback,
  346. METH_VARARGS,
  347. "Tell the JACK server to call a function whenever a port is registered.",
  348. },
  349. {
  350. "set_port_unregistered_callback",
  351. (PyCFunction)client_set_port_unregistered_callback,
  352. METH_VARARGS,
  353. "Tell the JACK server to call a function whenever a port is unregistered.",
  354. },
  355. {
  356. "set_port_renamed_callback",
  357. (PyCFunction)client_set_port_renamed_callback,
  358. METH_VARARGS,
  359. "Tell the JACK server to call a function whenever a port is renamed.",
  360. },
  361. {
  362. "set_shutdown_callback",
  363. (PyCFunction)client_set_shutdown_callback,
  364. METH_VARARGS,
  365. "Tell the JACK server to call a function when the server shuts down the client.",
  366. },
  367. {NULL},
  368. };
  369. static PyObject* port_is_input(Port* self)
  370. {
  371. // The flags "JackPortIsInput" and "JackPortIsOutput" are mutually exclusive.
  372. return (PyObject*)PyBool_FromLong(jack_port_flags(self->port) & JackPortIsInput);
  373. }
  374. static PyObject* port_is_output(Port* self)
  375. {
  376. // The flags "JackPortIsInput" and "JackPortIsOutput" are mutually exclusive.
  377. return (PyObject*)PyBool_FromLong(jack_port_flags(self->port) & JackPortIsOutput);
  378. }
  379. static PyObject* port_get_name(Port* self)
  380. {
  381. return (PyObject*)PyString_FromString(jack_port_name(self->port));
  382. }
  383. static PyObject* port_get_short_name(Port* self)
  384. {
  385. return (PyObject*)PyString_FromString(jack_port_short_name(self->port));
  386. }
  387. static PyObject* port_get_type(Port* self)
  388. {
  389. return (PyObject*)PyString_FromString(jack_port_type(self->port));
  390. }
  391. static PyObject* port_set_short_name(Port* self, PyObject* args)
  392. {
  393. const char* name;
  394. if(!PyArg_ParseTuple(args, "s", &name)) {
  395. return NULL;
  396. }
  397. // 0 on success, otherwise a non-zero error code.
  398. if(jack_port_set_name(self->port, name)) {
  399. return NULL;
  400. }
  401. Py_INCREF(Py_None);
  402. return Py_None;
  403. }
  404. static PyObject* port_get_client_name(Port* self)
  405. {
  406. // e.g. "PulseAudio JACK Sink:front-left"
  407. const char* port_name = jack_port_name(self->port);
  408. int client_name_length = strlen(port_name) - strlen(jack_port_short_name(self->port)) - 1;
  409. // e.g. "PulseAudio JACK Sink"
  410. char* client_name = (char*) malloc(jack_port_name_size());
  411. strncpy(client_name, port_name, client_name_length);
  412. client_name[client_name_length] = '\0';
  413. PyObject* client_name_python = PyString_FromString(client_name);
  414. free(client_name);
  415. return client_name_python;
  416. }
  417. static PyObject* port_get_aliases(Port* self)
  418. {
  419. PyObject* aliases_list = PyList_New(0);
  420. if(!aliases_list) {
  421. return NULL;
  422. }
  423. char* aliases[2];
  424. aliases[0] = (char*) malloc(jack_port_name_size());
  425. aliases[1] = (char*) malloc(jack_port_name_size());
  426. int alias_count = jack_port_get_aliases(self->port, aliases);
  427. int alias_index;
  428. for(alias_index = 0; alias_index < alias_count; alias_index++) {
  429. PyList_Append(aliases_list, PyString_FromString(aliases[alias_index]));
  430. }
  431. free(aliases[0]);
  432. free(aliases[1]);
  433. return aliases_list;
  434. }
  435. static PyObject* port___repr__(Port* self)
  436. {
  437. static PyObject* format;
  438. if(!format) {
  439. format = PyString_FromString("jack.Port(name = '%s', type = '%s')");
  440. }
  441. PyObject* args = Py_BuildValue(
  442. "(s,s)",
  443. jack_port_name(self->port),
  444. jack_port_type(self->port)
  445. );
  446. PyObject* repr = PyString_Format(format, args);
  447. Py_DECREF(args);
  448. return repr;
  449. }
  450. static PyMethodDef port_methods[] = {
  451. {
  452. "is_input",
  453. (PyCFunction)port_is_input,
  454. METH_NOARGS,
  455. "Return true if the port can receive data.",
  456. },
  457. {
  458. "is_output",
  459. (PyCFunction)port_is_output,
  460. METH_NOARGS,
  461. "Return true if data can be read from the port.",
  462. },
  463. {
  464. "get_name",
  465. (PyCFunction)port_get_name,
  466. METH_NOARGS,
  467. "Return port's name.",
  468. },
  469. {
  470. "get_short_name",
  471. (PyCFunction)port_get_short_name,
  472. METH_NOARGS,
  473. "Return port's name without the preceding name of the associated client.",
  474. },
  475. {
  476. "get_type",
  477. (PyCFunction)port_get_type,
  478. METH_NOARGS,
  479. "Return port's type.",
  480. },
  481. {
  482. "get_client_name",
  483. (PyCFunction)port_get_client_name,
  484. METH_NOARGS,
  485. "Return the name of the associated client.",
  486. },
  487. {
  488. "set_short_name",
  489. (PyCFunction)port_set_short_name,
  490. METH_VARARGS,
  491. "Modify a port's short name. May be called at any time.",
  492. },
  493. {
  494. "get_aliases",
  495. (PyCFunction)port_get_aliases,
  496. METH_NOARGS,
  497. "Return list of assigned aliases.",
  498. },
  499. {NULL},
  500. };
  501. #ifndef PyMODINIT_FUNC // declarations for DLL import/export
  502. #define PyMODINIT_FUNC void
  503. #endif
  504. PyMODINIT_FUNC initjack(void)
  505. {
  506. // Initialize and acquire the global interpreter lock.
  507. // This must be done in the main thread before creating engaging in any thread operations.
  508. PyEval_InitThreads();
  509. PyObject* module = Py_InitModule("jack", NULL);
  510. if(!module) {
  511. return;
  512. }
  513. error = PyErr_NewException((char*)"jack.Error", NULL, NULL);
  514. Py_INCREF(error);
  515. PyModule_AddObject(module, "Error", error);
  516. failure = PyErr_NewException((char*)"jack.Failure", error, NULL);
  517. Py_INCREF(failure);
  518. PyModule_AddObject(module, "Failure", failure);
  519. connection_exists = PyErr_NewException((char*)"jack.ConnectionExists", error, NULL);
  520. Py_INCREF(connection_exists);
  521. PyModule_AddObject(module, "ConnectionExists", connection_exists);
  522. static PyTypeObject client_type = {
  523. PyObject_HEAD_INIT(NULL)
  524. };
  525. client_type.tp_name = "jack.Client";
  526. client_type.tp_basicsize = sizeof(Client);
  527. client_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  528. client_type.tp_new = client___new__;
  529. client_type.tp_dealloc = (destructor)client_dealloc;
  530. client_type.tp_methods = client_methods;
  531. if(PyType_Ready(&client_type) < 0) {
  532. return;
  533. }
  534. Py_INCREF(&client_type);
  535. PyModule_AddObject(module, "Client", (PyObject*)&client_type);
  536. port_type.tp_name = "jack.Port";
  537. port_type.tp_basicsize = sizeof(Port);
  538. port_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  539. // Forbid direct instantiation.
  540. port_type.tp_new = NULL;
  541. port_type.tp_repr = (reprfunc)port___repr__;
  542. port_type.tp_methods = port_methods;
  543. if(PyType_Ready(&port_type) < 0) {
  544. return;
  545. }
  546. Py_INCREF(&port_type);
  547. PyModule_AddObject(module, "Port", (PyObject*)&port_type);
  548. }