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.

540 lines
13KB

  1. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  2. /*
  3. Copyright (C) 2007,2008 Nedko Arnaudov
  4. Copyright (C) 2007-2008 Juuso Alasuutari
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License.
  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <dbus/dbus.h>
  22. #include "controller.h"
  23. #include "controller_internal.h"
  24. #include "xml.h"
  25. #include "reserve.h"
  26. struct jack_dbus_interface_descriptor * g_jackcontroller_interfaces[] =
  27. {
  28. &g_jack_controller_iface_introspectable,
  29. &g_jack_controller_iface_control,
  30. &g_jack_controller_iface_configure,
  31. &g_jack_controller_iface_patchbay,
  32. &g_jack_controller_iface_transport,
  33. NULL
  34. };
  35. jackctl_driver_t *
  36. jack_controller_find_driver(
  37. jackctl_server_t *server,
  38. const char *driver_name)
  39. {
  40. const JSList * node_ptr;
  41. node_ptr = jackctl_server_get_drivers_list(server);
  42. while (node_ptr)
  43. {
  44. if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0)
  45. {
  46. return node_ptr->data;
  47. }
  48. node_ptr = jack_slist_next(node_ptr);
  49. }
  50. return NULL;
  51. }
  52. jackctl_internal_t *
  53. jack_controller_find_internal(
  54. jackctl_server_t *server,
  55. const char *internal_name)
  56. {
  57. const JSList * node_ptr;
  58. node_ptr = jackctl_server_get_internals_list(server);
  59. while (node_ptr)
  60. {
  61. if (strcmp(jackctl_internal_get_name((jackctl_internal_t *)node_ptr->data), internal_name) == 0)
  62. {
  63. return node_ptr->data;
  64. }
  65. node_ptr = jack_slist_next(node_ptr);
  66. }
  67. return NULL;
  68. }
  69. jackctl_parameter_t *
  70. jack_controller_find_parameter(
  71. const JSList * parameters_list,
  72. const char * parameter_name)
  73. {
  74. while (parameters_list)
  75. {
  76. if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0)
  77. {
  78. return parameters_list->data;
  79. }
  80. parameters_list = jack_slist_next(parameters_list);
  81. }
  82. return NULL;
  83. }
  84. bool
  85. jack_controller_select_driver(
  86. struct jack_controller * controller_ptr,
  87. const char * driver_name)
  88. {
  89. jackctl_driver_t *driver;
  90. driver = jack_controller_find_driver(controller_ptr->server, driver_name);
  91. if (driver == NULL)
  92. {
  93. return false;
  94. }
  95. jack_info("driver \"%s\" selected", driver_name);
  96. controller_ptr->driver = driver;
  97. controller_ptr->driver_set = true;
  98. return true;
  99. }
  100. static
  101. int
  102. jack_controller_xrun(void * arg)
  103. {
  104. ((struct jack_controller *)arg)->xruns++;
  105. return 0;
  106. }
  107. bool
  108. jack_controller_start_server(
  109. struct jack_controller * controller_ptr,
  110. void *dbus_call_context_ptr)
  111. {
  112. int ret;
  113. jack_info("Starting jack server...");
  114. if (controller_ptr->started)
  115. {
  116. jack_info("Already started.");
  117. return TRUE;
  118. }
  119. if (controller_ptr->driver == NULL)
  120. {
  121. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "Select driver first!");
  122. goto fail;
  123. }
  124. controller_ptr->xruns = 0;
  125. if (!jackctl_server_start(
  126. controller_ptr->server,
  127. controller_ptr->driver))
  128. {
  129. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "Failed to start server");
  130. goto fail;
  131. }
  132. controller_ptr->client = jack_client_open(
  133. "dbusapi",
  134. JackNoStartServer,
  135. NULL);
  136. if (controller_ptr->client == NULL)
  137. {
  138. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "failed to create dbusapi jack client");
  139. goto fail_stop_server;
  140. }
  141. ret = jack_set_xrun_callback(controller_ptr->client, jack_controller_xrun, controller_ptr);
  142. if (ret != 0)
  143. {
  144. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "failed to set xrun callback. error is %d", ret);
  145. goto fail_close_client;
  146. }
  147. if (!jack_controller_patchbay_init(controller_ptr))
  148. {
  149. jack_error("Failed to initialize patchbay district");
  150. goto fail_close_client;
  151. }
  152. ret = jack_activate(controller_ptr->client);
  153. if (ret != 0)
  154. {
  155. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "failed to activate dbusapi jack client. error is %d", ret);
  156. goto fail_patchbay_uninit;
  157. }
  158. controller_ptr->started = true;
  159. return TRUE;
  160. fail_patchbay_uninit:
  161. jack_controller_patchbay_uninit(controller_ptr);
  162. fail_close_client:
  163. ret = jack_client_close(controller_ptr->client);
  164. if (ret != 0)
  165. {
  166. jack_error("jack_client_close() failed with error %d", ret);
  167. }
  168. controller_ptr->client = NULL;
  169. fail_stop_server:
  170. if (!jackctl_server_stop(controller_ptr->server))
  171. {
  172. jack_error("failed to stop jack server");
  173. }
  174. fail:
  175. return FALSE;
  176. }
  177. bool
  178. jack_controller_stop_server(
  179. struct jack_controller * controller_ptr,
  180. void *dbus_call_context_ptr)
  181. {
  182. int ret;
  183. jack_info("Stopping jack server...");
  184. if (!controller_ptr->started)
  185. {
  186. jack_info("Already stopped.");
  187. return TRUE;
  188. }
  189. ret = jack_deactivate(controller_ptr->client);
  190. if (ret != 0)
  191. {
  192. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "failed to deactivate dbusapi jack client. error is %d", ret);
  193. }
  194. jack_controller_patchbay_uninit(controller_ptr);
  195. ret = jack_client_close(controller_ptr->client);
  196. if (ret != 0)
  197. {
  198. jack_error("jack_client_close() failed with error %d", ret);
  199. }
  200. controller_ptr->client = NULL;
  201. if (!jackctl_server_stop(controller_ptr->server))
  202. {
  203. return FALSE;
  204. }
  205. controller_ptr->started = false;
  206. return TRUE;
  207. }
  208. bool
  209. jack_controller_switch_master(
  210. struct jack_controller * controller_ptr,
  211. void *dbus_call_context_ptr)
  212. {
  213. if (!jackctl_server_switch_master(
  214. controller_ptr->server,
  215. controller_ptr->driver))
  216. {
  217. jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "Failed to switch master");
  218. return FALSE;
  219. }
  220. return TRUE;
  221. }
  222. /* TODO: use contianer with unique entries (dict) */
  223. bool g_reserved_device_valid = false;
  224. static rd_device * g_reserved_device;
  225. static
  226. bool
  227. on_device_acquire(const char * device_name)
  228. {
  229. int ret;
  230. DBusError error;
  231. if (g_reserved_device_valid) {
  232. jack_error("Ignoring reservation for more than one device (acquire)");
  233. return false;
  234. }
  235. ret = rd_acquire(
  236. &g_reserved_device,
  237. g_connection,
  238. device_name,
  239. "Jack audio server",
  240. INT32_MAX,
  241. NULL,
  242. &error);
  243. if (ret < 0)
  244. {
  245. jack_error("Failed to acquire device name : %s error : %s", device_name, (error.message ? error.message : strerror(-ret)));
  246. return false;
  247. }
  248. g_reserved_device_valid = true;
  249. jack_info("Acquired audio card %s", device_name);
  250. return true;
  251. }
  252. static
  253. void
  254. on_device_release(const char * device_name)
  255. {
  256. if (!g_reserved_device_valid) {
  257. jack_error("Ignoring reservation for more than one device(release)");
  258. }
  259. rd_release(g_reserved_device);
  260. g_reserved_device_valid = false;
  261. jack_info("Released audio card %s", device_name);
  262. }
  263. void *
  264. jack_controller_create(
  265. DBusConnection *connection)
  266. {
  267. struct jack_controller *controller_ptr;
  268. const JSList * node_ptr;
  269. const char ** driver_name_target;
  270. const char ** internal_name_target;
  271. JSList * drivers;
  272. JSList * internals;
  273. DBusObjectPathVTable vtable =
  274. {
  275. jack_dbus_message_handler_unregister,
  276. jack_dbus_message_handler,
  277. NULL
  278. };
  279. controller_ptr = malloc(sizeof(struct jack_controller));
  280. if (!controller_ptr)
  281. {
  282. jack_error("Ran out of memory trying to allocate struct jack_controller");
  283. goto fail;
  284. }
  285. controller_ptr->server = jackctl_server_create(on_device_acquire, on_device_release);
  286. if (controller_ptr->server == NULL)
  287. {
  288. jack_error("Failed to create server object");
  289. goto fail_free;
  290. }
  291. controller_ptr->client = NULL;
  292. controller_ptr->started = false;
  293. controller_ptr->driver = NULL;
  294. controller_ptr->driver_set = false;
  295. drivers = (JSList *)jackctl_server_get_drivers_list(controller_ptr->server);
  296. controller_ptr->drivers_count = jack_slist_length(drivers);
  297. controller_ptr->driver_names = malloc(controller_ptr->drivers_count * sizeof(const char *));
  298. if (controller_ptr->driver_names == NULL)
  299. {
  300. jack_error("Ran out of memory trying to allocate driver names array");
  301. goto fail_destroy_server;
  302. }
  303. driver_name_target = controller_ptr->driver_names;
  304. node_ptr = jackctl_server_get_drivers_list(controller_ptr->server);
  305. while (node_ptr != NULL)
  306. {
  307. *driver_name_target = jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data);
  308. /* select default driver */
  309. if (controller_ptr->driver == NULL && strcmp(*driver_name_target, DEFAULT_DRIVER) == 0)
  310. {
  311. controller_ptr->driver = (jackctl_driver_t *)node_ptr->data;
  312. }
  313. node_ptr = jack_slist_next(node_ptr);
  314. driver_name_target++;
  315. }
  316. internals = (JSList *)jackctl_server_get_internals_list(controller_ptr->server);
  317. controller_ptr->internals_count = jack_slist_length(internals);
  318. controller_ptr->internal_names = malloc(controller_ptr->internals_count * sizeof(const char *));
  319. if (controller_ptr->internal_names == NULL)
  320. {
  321. jack_error("Ran out of memory trying to allocate internals names array");
  322. goto fail_free_driver_names_array;
  323. }
  324. internal_name_target = controller_ptr->internal_names;
  325. node_ptr = jackctl_server_get_internals_list(controller_ptr->server);
  326. while (node_ptr != NULL)
  327. {
  328. *internal_name_target = jackctl_internal_get_name((jackctl_internal_t *)node_ptr->data);
  329. node_ptr = jack_slist_next(node_ptr);
  330. internal_name_target++;
  331. }
  332. controller_ptr->dbus_descriptor.context = controller_ptr;
  333. controller_ptr->dbus_descriptor.interfaces = g_jackcontroller_interfaces;
  334. if (!dbus_connection_register_object_path(
  335. connection,
  336. JACK_CONTROLLER_OBJECT_PATH,
  337. &vtable,
  338. &controller_ptr->dbus_descriptor))
  339. {
  340. jack_error("Ran out of memory trying to register D-Bus object path");
  341. goto fail_free_internal_names_array;
  342. }
  343. jack_controller_settings_load(controller_ptr);
  344. return controller_ptr;
  345. fail_free_internal_names_array:
  346. free(controller_ptr->internal_names);
  347. fail_free_driver_names_array:
  348. free(controller_ptr->driver_names);
  349. fail_destroy_server:
  350. jackctl_server_destroy(controller_ptr->server);
  351. fail_free:
  352. free(controller_ptr);
  353. fail:
  354. return NULL;
  355. }
  356. bool
  357. jack_controller_add_slave(
  358. struct jack_controller *controller_ptr,
  359. const char * driver_name)
  360. {
  361. jackctl_driver_t *driver;
  362. driver = jack_controller_find_driver(controller_ptr->server, driver_name);
  363. if (driver == NULL)
  364. {
  365. return false;
  366. }
  367. jack_info("driver \"%s\" selected", driver_name);
  368. return jackctl_server_add_slave(controller_ptr->server, driver);
  369. }
  370. bool
  371. jack_controller_remove_slave(
  372. struct jack_controller *controller_ptr,
  373. const char * driver_name)
  374. {
  375. jackctl_driver_t *driver;
  376. driver = jack_controller_find_driver(controller_ptr->server, driver_name);
  377. if (driver == NULL)
  378. {
  379. return false;
  380. }
  381. jack_info("driver \"%s\" selected", driver_name);
  382. return jackctl_server_remove_slave(controller_ptr->server, driver);
  383. }
  384. bool
  385. jack_controller_load_internal(
  386. struct jack_controller *controller_ptr,
  387. const char * internal_name)
  388. {
  389. jackctl_internal_t *internal;
  390. internal = jack_controller_find_internal(controller_ptr->server, internal_name);
  391. if (internal == NULL)
  392. {
  393. return false;
  394. }
  395. jack_info("internal \"%s\" selected", internal_name);
  396. return jackctl_server_load_internal(controller_ptr->server, internal);
  397. }
  398. bool
  399. jack_controller_unload_internal(
  400. struct jack_controller *controller_ptr,
  401. const char * internal_name)
  402. {
  403. jackctl_internal_t *internal;
  404. internal = jack_controller_find_internal(controller_ptr->server, internal_name);
  405. if (internal == NULL)
  406. {
  407. return false;
  408. }
  409. jack_info("internal \"%s\" selected", internal_name);
  410. return jackctl_server_unload_internal(controller_ptr->server, internal);
  411. }
  412. #define controller_ptr ((struct jack_controller *)context)
  413. void
  414. jack_controller_destroy(
  415. void * context)
  416. {
  417. if (controller_ptr->started)
  418. {
  419. jack_controller_stop_server(controller_ptr, NULL);
  420. }
  421. free(controller_ptr->driver_names);
  422. free(controller_ptr->internal_names);
  423. jackctl_server_destroy(controller_ptr->server);
  424. free(controller_ptr);
  425. }