obs-source.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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(obs_t obs,
  55. struct darray *list, 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 source_init(obs_t obs, struct obs_source *source)
  67. {
  68. source->obs = obs;
  69. source->filter_target = NULL;
  70. source->rendering_filter = false;
  71. dstr_init(&source->settings);
  72. da_init(source->filters);
  73. da_push_back(obs->sources, &source);
  74. }
  75. source_t source_create(obs_t obs, enum source_type type, const char *name,
  76. const char *settings)
  77. {
  78. const struct source_info *info = NULL;
  79. struct darray *list = NULL;
  80. struct obs_source *source;
  81. switch (type) {
  82. case SOURCE_INPUT: list = &obs->input_types.da; break;
  83. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  84. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  85. default:
  86. return NULL;
  87. }
  88. info = find_source(obs, 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. source_init(obs, source);
  100. dstr_copy(&source->settings, settings);
  101. memcpy(&source->callbacks, info, sizeof(struct source_info));
  102. return source;
  103. }
  104. void source_destroy(source_t source)
  105. {
  106. if (source) {
  107. da_free(source->filters);
  108. da_erase_item(source->obs->sources, &source);
  109. source->callbacks.destroy(source->data);
  110. dstr_free(&source->settings);
  111. bfree(source);
  112. }
  113. }
  114. uint32_t source_get_output_flags(source_t source)
  115. {
  116. return source->callbacks.get_output_flags(source->data);
  117. }
  118. bool source_hasconfig(source_t source)
  119. {
  120. return source->callbacks.config != NULL;
  121. }
  122. void source_config(source_t source, void *parent)
  123. {
  124. if (source->callbacks.config)
  125. source->callbacks.config(source->data, parent);
  126. }
  127. void source_activate(source_t source)
  128. {
  129. if (source->callbacks.activate)
  130. source->callbacks.activate(source->data);
  131. }
  132. void source_deactivate(source_t source)
  133. {
  134. if (source->callbacks.deactivate)
  135. source->callbacks.deactivate(source->data);
  136. }
  137. void source_video_tick(source_t source, float seconds)
  138. {
  139. if (source->callbacks.video_tick)
  140. source->callbacks.video_tick(source->data, seconds);
  141. }
  142. void source_video_render(source_t source)
  143. {
  144. if (source->callbacks.video_render) {
  145. if (source->filters.num && !source->rendering_filter) {
  146. source->rendering_filter = true;
  147. 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. int source_getwidth(source_t source)
  155. {
  156. if (source->callbacks.getwidth)
  157. return source->callbacks.getwidth(source->data);
  158. return 0;
  159. }
  160. int source_getheight(source_t source)
  161. {
  162. if (source->callbacks.getheight)
  163. return source->callbacks.getheight(source->data);
  164. return 0;
  165. }
  166. size_t source_getparam(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 source_setparam(source_t source, const char *param, const void *data,
  175. size_t size)
  176. {
  177. if (source->callbacks.setparam)
  178. source->callbacks.setparam(source->data, param, data, size);
  179. }
  180. bool source_enum_children(source_t source, size_t idx, source_t *child)
  181. {
  182. if (source->callbacks.enum_children)
  183. return source->callbacks.enum_children(source, idx, child);
  184. return false;
  185. }
  186. source_t filter_gettarget(source_t filter)
  187. {
  188. return filter->filter_target;
  189. }
  190. void source_filter_add(source_t source, source_t filter)
  191. {
  192. if (da_find(source->filters, &filter, 0) != -1) {
  193. blog(LOG_WARNING, "Tried to add a filter that was already "
  194. "present on the source");
  195. return;
  196. }
  197. if (source->filters.num) {
  198. source_t *back = da_end(source->filters);
  199. (*back)->filter_target = filter;
  200. }
  201. da_push_back(source->filters, &filter);
  202. filter->filter_target = source;
  203. }
  204. void source_filter_remove(source_t source, source_t filter)
  205. {
  206. size_t idx = da_find(source->filters, &filter, 0);
  207. if (idx == -1)
  208. return;
  209. if (idx > 0) {
  210. source_t prev = source->filters.array[idx-1];
  211. prev->filter_target = filter->filter_target;
  212. }
  213. da_erase(source->filters, idx);
  214. filter->filter_target = NULL;
  215. }
  216. void source_filter_setorder(source_t source, source_t filter,
  217. enum order_movement movement)
  218. {
  219. size_t idx = da_find(source->filters, &filter, 0);
  220. size_t i;
  221. if (idx == -1)
  222. return;
  223. if (movement == ORDER_MOVE_UP) {
  224. if (idx == source->filters.num-1)
  225. return;
  226. da_move_item(source->filters, idx, idx+1);
  227. } else if (movement == ORDER_MOVE_DOWN) {
  228. if (idx == 0)
  229. return;
  230. da_move_item(source->filters, idx, idx-1);
  231. } else if (movement == ORDER_MOVE_TOP) {
  232. if (idx == source->filters.num-1)
  233. return;
  234. da_move_item(source->filters, idx, source->filters.num-1);
  235. } else if (movement == ORDER_MOVE_BOTTOM) {
  236. if (idx == 0)
  237. return;
  238. da_move_item(source->filters, idx, 0);
  239. }
  240. /* reorder filter targets */
  241. for (i = 0; i < source->filters.num; i++) {
  242. source_t next_filter = (i == source->filters.num-1) ?
  243. source : source->filters.array[idx+1];
  244. source->filters.array[i]->filter_target = next_filter;
  245. }
  246. }
  247. const char *source_get_settings(source_t source)
  248. {
  249. return source->settings.array;
  250. }
  251. void source_save_settings(source_t source, const char *settings)
  252. {
  253. dstr_copy(&source->settings, settings);
  254. }
  255. void source_output_video(source_t source, struct video_frame *frame)
  256. {
  257. /* TODO */
  258. }
  259. void source_output_audio(source_t source, struct audio_data *audio)
  260. {
  261. /* TODO */
  262. }