obs-output.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[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 "obs.h"
  15. #include "obs-data.h"
  16. bool load_output_info(void *module, const char *module_name,
  17. const char *output_id, struct output_info *info)
  18. {
  19. info->getname = load_module_subfunc(module, module_name,
  20. output_id, "getname", true);
  21. info->create = load_module_subfunc(module, module_name,
  22. output_id, "create", true);
  23. info->destroy = load_module_subfunc(module, module_name,
  24. output_id, "destroy", true);
  25. info->start = load_module_subfunc(module, module_name,
  26. output_id, "start", true);
  27. info->stop = load_module_subfunc(module, module_name,
  28. output_id, "stop", true);
  29. info->active = load_module_subfunc(module, module_name,
  30. output_id, "active", true);
  31. if (!info->getname || !info->create || !info->destroy ||
  32. !info->start || !info->stop || !info->active)
  33. return false;
  34. info->update = load_module_subfunc(module, module_name,
  35. output_id, "update", false);
  36. info->pause = load_module_subfunc(module, module_name,
  37. output_id, "pause", false);
  38. info->id = output_id;
  39. return true;
  40. }
  41. static inline const struct output_info *find_output(const char *id)
  42. {
  43. size_t i;
  44. for (i = 0; i < obs->output_types.num; i++)
  45. if (strcmp(obs->output_types.array[i].id, id) == 0)
  46. return obs->output_types.array+i;
  47. return NULL;
  48. }
  49. obs_output_t obs_output_create(const char *id, const char *name,
  50. const char *settings)
  51. {
  52. const struct output_info *info = find_output(id);
  53. struct obs_output *output;
  54. if (!info) {
  55. blog(LOG_WARNING, "Output '%s' not found", id);
  56. return NULL;
  57. }
  58. output = bmalloc(sizeof(struct obs_output));
  59. output->callbacks = *info;
  60. output->data = info->create(settings, output);
  61. if (!output->data) {
  62. bfree(output);
  63. return NULL;
  64. }
  65. output->name = bstrdup(name);
  66. dstr_init_copy(&output->settings, settings);
  67. pthread_mutex_lock(&obs->data.outputs_mutex);
  68. da_push_back(obs->data.outputs, &output);
  69. pthread_mutex_unlock(&obs->data.outputs_mutex);
  70. return output;
  71. }
  72. void obs_output_destroy(obs_output_t output)
  73. {
  74. if (output) {
  75. pthread_mutex_lock(&obs->data.outputs_mutex);
  76. da_erase_item(obs->data.outputs, &output);
  77. pthread_mutex_unlock(&obs->data.outputs_mutex);
  78. output->callbacks.destroy(output->data);
  79. dstr_free(&output->settings);
  80. bfree(output->name);
  81. bfree(output);
  82. }
  83. }
  84. bool obs_output_start(obs_output_t output)
  85. {
  86. return output->callbacks.start(output->data);
  87. }
  88. void obs_output_stop(obs_output_t output)
  89. {
  90. output->callbacks.stop(output->data);
  91. }
  92. bool obs_output_active(obs_output_t output)
  93. {
  94. return output->callbacks.active(output);
  95. }
  96. void obs_output_update(obs_output_t output, const char *settings)
  97. {
  98. if (output->callbacks.update)
  99. output->callbacks.update(output->data, settings);
  100. }
  101. bool obs_output_canpause(obs_output_t output)
  102. {
  103. return output->callbacks.pause != NULL;
  104. }
  105. void obs_output_pause(obs_output_t output)
  106. {
  107. if (output->callbacks.pause)
  108. output->callbacks.pause(output->data);
  109. }
  110. void obs_output_save_settings(obs_output_t output, const char *settings)
  111. {
  112. dstr_copy(&output->settings, settings);
  113. }