jack-input.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 *unused)
  20. {
  21. UNUSED_PARAMETER(unused);
  22. return obs_module_text("JACKInput");
  23. }
  24. /**
  25. * Destroy the plugin object and free all memory
  26. */
  27. static void jack_destroy(void *vptr)
  28. {
  29. struct jack_data *data = (struct jack_data *)vptr;
  30. if (!data)
  31. return;
  32. deactivate_jack(data);
  33. if (data->device)
  34. bfree(data->device);
  35. pthread_mutex_destroy(&data->jack_mutex);
  36. bfree(data);
  37. }
  38. /**
  39. * Update the input settings
  40. */
  41. static void jack_update(void *vptr, obs_data_t *settings)
  42. {
  43. struct jack_data *data = (struct jack_data *)vptr;
  44. if (!data)
  45. return;
  46. const char *new_device;
  47. bool settings_changed = false;
  48. bool new_jack_start_server = obs_data_get_bool(settings, "startjack");
  49. int new_channel_count = obs_data_get_int(settings, "channels");
  50. if (new_jack_start_server != data->start_jack_server) {
  51. data->start_jack_server = new_jack_start_server;
  52. settings_changed = true;
  53. }
  54. if (new_channel_count != data->channels)
  55. /*
  56. * keep "old" channel count for now,
  57. * we need to destroy the correct number of channels
  58. */
  59. settings_changed = true;
  60. new_device = obs_source_get_name(data->source);
  61. if (!data->device || strcmp(data->device, new_device) != 0) {
  62. if (data->device)
  63. bfree(data->device);
  64. data->device = bstrdup(new_device);
  65. settings_changed = true;
  66. }
  67. if (settings_changed) {
  68. deactivate_jack(data);
  69. data->channels = new_channel_count;
  70. if (jack_init(data) != 0) {
  71. deactivate_jack(data);
  72. }
  73. }
  74. }
  75. /**
  76. * Create the plugin object
  77. */
  78. static void *jack_create(obs_data_t *settings, obs_source_t *source)
  79. {
  80. struct jack_data *data = bzalloc(sizeof(struct jack_data));
  81. pthread_mutex_init(&data->jack_mutex, NULL);
  82. data->source = source;
  83. data->channels = -1;
  84. jack_update(data, settings);
  85. if (data->jack_client == NULL) {
  86. jack_destroy(data);
  87. return NULL;
  88. }
  89. return data;
  90. }
  91. /**
  92. * Get plugin defaults
  93. */
  94. static void jack_input_defaults(obs_data_t *settings)
  95. {
  96. obs_data_set_default_int(settings, "channels", 2);
  97. obs_data_set_default_bool(settings, "startjack", false);
  98. }
  99. /**
  100. * Get plugin properties
  101. */
  102. static obs_properties_t *jack_input_properties(void *unused)
  103. {
  104. (void)unused;
  105. obs_properties_t *props = obs_properties_create();
  106. obs_properties_add_int(props, "channels", obs_module_text("Channels"),
  107. 1, 8, 1);
  108. obs_properties_add_bool(props, "startjack",
  109. obs_module_text("StartJACKServer"));
  110. return props;
  111. }
  112. struct obs_source_info jack_output_capture = {
  113. .id = "jack_output_capture",
  114. .type = OBS_SOURCE_TYPE_INPUT,
  115. .output_flags = OBS_SOURCE_AUDIO,
  116. .get_name = jack_input_getname,
  117. .create = jack_create,
  118. .destroy = jack_destroy,
  119. .update = jack_update,
  120. .get_defaults = jack_input_defaults,
  121. .get_properties = jack_input_properties,
  122. .icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT,
  123. };