jack.c 18 KB

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