jack-input.c 3.7 KB

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