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.

541 lines
13KB

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