jack-input.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright (C) 2015 by Bernd Buschinski <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "jack-wrapper.h"
  15. #include <obs-module.h>
  16. /**
  17. * Returns the name of the plugin
  18. */
  19. static const char *jack_input_getname(void)
  20. {
  21. return obs_module_text("JACKInput");
  22. }
  23. /**
  24. * Destroy the plugin object and free all memory
  25. */
  26. static void jack_destroy(void *vptr)
  27. {
  28. struct jack_data* data = (struct jack_data*)vptr;
  29. if (!data)
  30. return;
  31. deactivate_jack(data);
  32. if (data->device)
  33. bfree(data->device);
  34. pthread_mutex_destroy(&data->jack_mutex);
  35. bfree(data);
  36. }
  37. /**
  38. * Update the input settings
  39. */
  40. static void jack_update(void *vptr, obs_data_t *settings)
  41. {
  42. struct jack_data* data = (struct jack_data*)vptr;
  43. if (!data)
  44. return;
  45. const char *new_device;
  46. bool settings_changed = false;
  47. bool new_jack_start_server = obs_data_get_bool(settings, "startjack");
  48. int new_channel_count = obs_data_get_int(settings, "channels");
  49. if (new_jack_start_server != data->start_jack_server) {
  50. data->start_jack_server = new_jack_start_server;
  51. settings_changed = true;
  52. }
  53. if (new_channel_count != data->channels)
  54. /*
  55. * keep "old" channel count for now,
  56. * we need to destroy the correct number of channels
  57. */
  58. settings_changed = true;
  59. new_device = obs_source_get_name(data->source);
  60. if (!data->device || strcmp(data->device, new_device) != 0) {
  61. if (data->device)
  62. bfree(data->device);
  63. data->device = bstrdup(new_device);
  64. settings_changed = true;
  65. }
  66. if (settings_changed) {
  67. deactivate_jack(data);
  68. data->channels = new_channel_count;
  69. if (jack_init(data) != 0) {
  70. deactivate_jack(data);
  71. }
  72. }
  73. }
  74. /**
  75. * Create the plugin object
  76. */
  77. static void *jack_create(obs_data_t *settings, obs_source_t *source)
  78. {
  79. struct jack_data *data = bzalloc(sizeof(struct jack_data));
  80. pthread_mutex_init(&data->jack_mutex, NULL);
  81. data->source = source;
  82. data->channels = -1;
  83. jack_update(data, settings);
  84. if (data->jack_client == NULL) {
  85. jack_destroy(data);
  86. return NULL;
  87. }
  88. return data;
  89. }
  90. /**
  91. * Get plugin defaults
  92. */
  93. static void jack_input_defaults(obs_data_t *settings)
  94. {
  95. obs_data_set_default_int(settings, "channels", 2);
  96. obs_data_set_default_bool(settings, "startjack", false);
  97. }
  98. /**
  99. * Get plugin properties
  100. */
  101. static obs_properties_t *jack_input_properties(void *unused)
  102. {
  103. (void)unused;
  104. obs_properties_t *props = obs_properties_create();
  105. obs_properties_add_int(props, "channels",
  106. obs_module_text("Channels"), 1, 8, 1);
  107. obs_properties_add_bool(props, "startjack",
  108. obs_module_text("StartJACKServer"));
  109. return props;
  110. }
  111. struct obs_source_info jack_output_capture = {
  112. .id = "jack_output_capture",
  113. .type = OBS_SOURCE_TYPE_INPUT,
  114. .output_flags = OBS_SOURCE_AUDIO,
  115. .get_name = jack_input_getname,
  116. .create = jack_create,
  117. .destroy = jack_destroy,
  118. .update = jack_update,
  119. .get_defaults = jack_input_defaults,
  120. .get_properties = jack_input_properties
  121. };