obs-source.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 3 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 get_source_info(void *module, const char *module_name,
  17. const char *source_name, struct source_info *info)
  18. {
  19. info->create = load_module_subfunc(module, module_name,
  20. source_name,"create", true);
  21. info->destroy = load_module_subfunc(module, module_name,
  22. source_name, "destroy", true);
  23. info->get_output_flags = load_module_subfunc(module, module_name,
  24. source_name, "get_output_flags", true);
  25. if (!info->create || !info->destroy || !info->get_output_flags)
  26. return false;
  27. info->config = load_module_subfunc(module, module_name,
  28. source_name, "config", false);
  29. info->activate = load_module_subfunc(module, module_name,
  30. source_name, "activate", false);
  31. info->deactivate = load_module_subfunc(module, module_name,
  32. source_name, "deactivate", false);
  33. info->video_tick = load_module_subfunc(module, module_name,
  34. source_name, "video_tick", false);
  35. info->video_render = load_module_subfunc(module, module_name,
  36. source_name, "video_render", false);
  37. info->getwidth = load_module_subfunc(module, module_name,
  38. source_name, "getwidth", false);
  39. info->getheight = load_module_subfunc(module, module_name,
  40. source_name, "getheight", false);
  41. info->getparam = load_module_subfunc(module, module_name,
  42. source_name, "getparam", false);
  43. info->setparam = load_module_subfunc(module, module_name,
  44. source_name, "setparam", false);
  45. info->enum_children = load_module_subfunc(module, module_name,
  46. source_name, "enum_children", false);
  47. info->filter_video = load_module_subfunc(module, module_name,
  48. source_name, "filter_video", false);
  49. info->filter_audio = load_module_subfunc(module, module_name,
  50. source_name, "filter_audio", false);
  51. info->name = source_name;
  52. return true;
  53. }
  54. static inline const struct source_info *find_source(struct darray *list,
  55. const char *name)
  56. {
  57. size_t i;
  58. struct source_info *array = list->array;
  59. for (i = 0; i < list->num; i++) {
  60. struct source_info *info = array+i;
  61. if (strcmp(info->name, name) == 0)
  62. return info;
  63. }
  64. return NULL;
  65. }
  66. void obs_source_init(struct obs_source *source)
  67. {
  68. source->filter_target = NULL;
  69. source->rendering_filter = false;
  70. dstr_init(&source->settings);
  71. da_init(source->filters);
  72. da_push_back(obs->sources, &source);
  73. }
  74. obs_source_t obs_source_create(enum obs_source_type type, const char *name,
  75. const char *settings)
  76. {
  77. const struct source_info *info = NULL;
  78. struct darray *list = NULL;
  79. struct obs_source *source;
  80. switch (type) {
  81. case SOURCE_INPUT: list = &obs->input_types.da; break;
  82. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  83. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  84. case SOURCE_SCENE:
  85. default:
  86. return NULL;
  87. }
  88. info = find_source(list, name);
  89. if (!info) {
  90. blog(LOG_WARNING, "Source '%s' not found", type);
  91. return NULL;
  92. }
  93. source = bmalloc(sizeof(struct obs_source));
  94. source->data = info->create(settings, source);
  95. if (!source->data) {
  96. bfree(source);
  97. return NULL;
  98. }
  99. obs_source_init(source);
  100. dstr_copy(&source->settings, settings);
  101. memcpy(&source->callbacks, info, sizeof(struct source_info));
  102. return source;
  103. }
  104. void obs_source_destroy(obs_source_t source)
  105. {
  106. if (source) {
  107. da_free(source->filters);
  108. da_erase_item(obs->sources, &source);
  109. source->callbacks.destroy(source->data);
  110. dstr_free(&source->settings);
  111. bfree(source);
  112. }
  113. }
  114. uint32_t obs_source_get_output_flags(obs_source_t source)
  115. {
  116. return source->callbacks.get_output_flags(source->data);
  117. }
  118. bool obs_source_hasconfig(obs_source_t source)
  119. {
  120. return source->callbacks.config != NULL;
  121. }
  122. void obs_source_config(obs_source_t source, void *parent)
  123. {
  124. if (source->callbacks.config)
  125. source->callbacks.config(source->data, parent);
  126. }
  127. void obs_source_activate(obs_source_t source)
  128. {
  129. if (source->callbacks.activate)
  130. source->callbacks.activate(source->data);
  131. }
  132. void obs_source_deactivate(obs_source_t source)
  133. {
  134. if (source->callbacks.deactivate)
  135. source->callbacks.deactivate(source->data);
  136. }
  137. void obs_source_video_tick(obs_source_t source, float seconds)
  138. {
  139. if (source->callbacks.video_tick)
  140. source->callbacks.video_tick(source->data, seconds);
  141. }
  142. void obs_source_video_render(obs_source_t source)
  143. {
  144. if (source->callbacks.video_render) {
  145. if (source->filters.num && !source->rendering_filter) {
  146. source->rendering_filter = true;
  147. obs_source_video_render(source->filters.array[0]);
  148. source->rendering_filter = false;
  149. } else {
  150. source->callbacks.video_render(source->data);
  151. }
  152. }
  153. }
  154. uint32_t obs_source_getwidth(obs_source_t source)
  155. {
  156. if (source->callbacks.getwidth)
  157. return source->callbacks.getwidth(source->data);
  158. return 0;
  159. }
  160. uint32_t obs_source_getheight(obs_source_t source)
  161. {
  162. if (source->callbacks.getheight)
  163. return source->callbacks.getheight(source->data);
  164. return 0;
  165. }
  166. size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
  167. size_t buf_size)
  168. {
  169. if (source->callbacks.getparam)
  170. return source->callbacks.getparam(source->data, param, buf,
  171. buf_size);
  172. return 0;
  173. }
  174. void obs_source_setparam(obs_source_t source, const char *param,
  175. const void *data, size_t size)
  176. {
  177. if (source->callbacks.setparam)
  178. source->callbacks.setparam(source->data, param, data, size);
  179. }
  180. bool obs_source_enum_children(obs_source_t source, size_t idx,
  181. obs_source_t *child)
  182. {
  183. if (source->callbacks.enum_children)
  184. return source->callbacks.enum_children(source, idx, child);
  185. return false;
  186. }
  187. obs_source_t obs_filter_gettarget(obs_source_t filter)
  188. {
  189. return filter->filter_target;
  190. }
  191. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  192. {
  193. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  194. blog(LOG_WARNING, "Tried to add a filter that was already "
  195. "present on the source");
  196. return;
  197. }
  198. if (source->filters.num) {
  199. obs_source_t *back = da_end(source->filters);
  200. (*back)->filter_target = filter;
  201. }
  202. da_push_back(source->filters, &filter);
  203. filter->filter_target = source;
  204. }
  205. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  206. {
  207. size_t idx = da_find(source->filters, &filter, 0);
  208. if (idx == DARRAY_INVALID)
  209. return;
  210. if (idx > 0) {
  211. obs_source_t prev = source->filters.array[idx-1];
  212. prev->filter_target = filter->filter_target;
  213. }
  214. da_erase(source->filters, idx);
  215. filter->filter_target = NULL;
  216. }
  217. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  218. enum order_movement movement)
  219. {
  220. size_t idx = da_find(source->filters, &filter, 0);
  221. size_t i;
  222. if (idx == DARRAY_INVALID)
  223. return;
  224. if (movement == ORDER_MOVE_UP) {
  225. if (idx == source->filters.num-1)
  226. return;
  227. da_move_item(source->filters, idx, idx+1);
  228. } else if (movement == ORDER_MOVE_DOWN) {
  229. if (idx == 0)
  230. return;
  231. da_move_item(source->filters, idx, idx-1);
  232. } else if (movement == ORDER_MOVE_TOP) {
  233. if (idx == source->filters.num-1)
  234. return;
  235. da_move_item(source->filters, idx, source->filters.num-1);
  236. } else if (movement == ORDER_MOVE_BOTTOM) {
  237. if (idx == 0)
  238. return;
  239. da_move_item(source->filters, idx, 0);
  240. }
  241. /* reorder filter targets */
  242. for (i = 0; i < source->filters.num; i++) {
  243. obs_source_t next_filter = (i == source->filters.num-1) ?
  244. source : source->filters.array[idx+1];
  245. source->filters.array[i]->filter_target = next_filter;
  246. }
  247. }
  248. const char *obs_source_get_settings(obs_source_t source)
  249. {
  250. return source->settings.array;
  251. }
  252. void obs_source_save_settings(obs_source_t source, const char *settings)
  253. {
  254. dstr_copy(&source->settings, settings);
  255. }
  256. void obs_source_output_video(obs_source_t source, struct video_frame *frame)
  257. {
  258. /* TODO */
  259. }
  260. void obs_source_output_audio(obs_source_t source, struct audio_data *audio)
  261. {
  262. /* TODO */
  263. }