obs-scene.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "graphics/math-defs.h"
  15. #include "obs-scene.h"
  16. static inline void signal_item_destroy(struct obs_scene_item *item,
  17. struct calldata *params)
  18. {
  19. calldata_setptr(params, "scene", item->parent);
  20. calldata_setptr(params, "item", item);
  21. signal_handler_signal(item->parent->source->signals, "remove",
  22. params);
  23. calldata_clear(params);
  24. }
  25. static const char *scene_getname(const char *locale)
  26. {
  27. /* TODO: locale lookup of display name */
  28. return "Scene";
  29. }
  30. static void *scene_create(const char *settings, struct obs_source *source)
  31. {
  32. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  33. scene->source = source;
  34. scene->first_item = NULL;
  35. if (pthread_mutex_init(&scene->mutex, NULL) != 0) {
  36. blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
  37. bfree(scene);
  38. return NULL;
  39. }
  40. return scene;
  41. }
  42. static void scene_destroy(void *data)
  43. {
  44. struct obs_scene *scene = data;
  45. struct obs_scene_item *item = scene->first_item;
  46. struct calldata params = {0};
  47. while (item) {
  48. struct obs_scene_item *del_item = item;
  49. item = item->next;
  50. signal_item_destroy(del_item, &params);
  51. if (del_item->source)
  52. obs_source_release(del_item->source);
  53. bfree(del_item);
  54. }
  55. calldata_free(&params);
  56. pthread_mutex_destroy(&scene->mutex);
  57. bfree(scene);
  58. }
  59. static uint32_t scene_get_output_flags(void *data)
  60. {
  61. return SOURCE_VIDEO;
  62. }
  63. static inline void detach_sceneitem(struct obs_scene_item *item)
  64. {
  65. if (item->prev)
  66. item->prev->next = item->next;
  67. else
  68. item->parent->first_item = item->next;
  69. if (item->next)
  70. item->next->prev = item->prev;
  71. }
  72. static inline void attach_sceneitem(struct obs_scene_item *item,
  73. struct obs_scene_item *prev)
  74. {
  75. item->prev = prev;
  76. if (prev) {
  77. item->next = prev->next;
  78. if (prev->next)
  79. prev->next->prev = item;
  80. prev->next = item;
  81. } else {
  82. item->next = item->parent->first_item;
  83. item->parent->first_item = item;
  84. }
  85. }
  86. static void scene_video_render(void *data)
  87. {
  88. struct obs_scene *scene = data;
  89. struct obs_scene_item *item = scene->first_item;
  90. pthread_mutex_lock(&scene->mutex);
  91. while (item) {
  92. if (obs_source_removed(item->source)) {
  93. struct obs_scene_item *del_item = item;
  94. item = item->next;
  95. obs_sceneitem_destroy(del_item);
  96. continue;
  97. }
  98. gs_matrix_push();
  99. gs_matrix_translate3f(item->origin.x, item->origin.y, 0.0f);
  100. gs_matrix_scale3f(item->scale.x, item->scale.y, 1.0f);
  101. gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(-item->rot));
  102. gs_matrix_translate3f(-item->pos.x, -item->pos.y, 0.0f);
  103. obs_source_video_render(item->source);
  104. gs_matrix_pop();
  105. item = item->next;
  106. }
  107. pthread_mutex_unlock(&scene->mutex);
  108. }
  109. static uint32_t scene_getsize(void *data)
  110. {
  111. return 0;
  112. }
  113. static const struct source_info scene_info =
  114. {
  115. .id = "scene",
  116. .getname = scene_getname,
  117. .create = scene_create,
  118. .destroy = scene_destroy,
  119. .get_output_flags = scene_get_output_flags,
  120. .video_render = scene_video_render,
  121. .getwidth = scene_getsize,
  122. .getheight = scene_getsize,
  123. };
  124. obs_scene_t obs_scene_create(const char *name)
  125. {
  126. struct obs_source *source = bmalloc(sizeof(struct obs_source));
  127. struct obs_scene *scene;
  128. memset(source, 0, sizeof(struct obs_source));
  129. if (!obs_source_init_handlers(source)) {
  130. bfree(source);
  131. return NULL;
  132. }
  133. scene = scene_create(NULL, source);
  134. source->data = scene;
  135. assert(scene);
  136. if (!scene) {
  137. bfree(source);
  138. return NULL;
  139. }
  140. source->name = bstrdup(name);
  141. source->type = SOURCE_SCENE;
  142. scene->source = source;
  143. obs_source_init(source, NULL, &scene_info);
  144. memcpy(&source->callbacks, &scene_info, sizeof(struct source_info));
  145. return scene;
  146. }
  147. int obs_scene_addref(obs_scene_t scene)
  148. {
  149. return obs_source_addref(scene->source);
  150. }
  151. int obs_scene_release(obs_scene_t scene)
  152. {
  153. if (scene)
  154. return obs_source_release(scene->source);
  155. return 0;
  156. }
  157. obs_source_t obs_scene_getsource(obs_scene_t scene)
  158. {
  159. return scene->source;
  160. }
  161. obs_scene_t obs_scene_fromsource(obs_source_t source)
  162. {
  163. if (source->type != SOURCE_SCENE)
  164. return NULL;
  165. return source->data;
  166. }
  167. obs_sceneitem_t obs_scene_findsource(obs_scene_t scene, const char *name)
  168. {
  169. struct obs_scene_item *item;
  170. pthread_mutex_lock(&scene->mutex);
  171. item = scene->first_item;
  172. while (item) {
  173. if (strcmp(item->source->name, name) == 0) {
  174. break;
  175. }
  176. item = item->next;
  177. }
  178. pthread_mutex_unlock(&scene->mutex);
  179. return item;
  180. }
  181. void obs_scene_enum_items(obs_scene_t scene,
  182. bool (*callback)(obs_scene_t, obs_sceneitem_t, void*),
  183. void *param)
  184. {
  185. struct obs_scene_item *item;
  186. pthread_mutex_lock(&scene->mutex);
  187. item = scene->first_item;
  188. while (item) {
  189. if (!callback(scene, item, param))
  190. break;
  191. item = item->next;
  192. }
  193. pthread_mutex_unlock(&scene->mutex);
  194. }
  195. obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
  196. {
  197. struct obs_scene_item *last;
  198. struct obs_scene_item *item = bmalloc(sizeof(struct obs_scene_item));
  199. struct calldata params = {0};
  200. memset(item, 0, sizeof(struct obs_scene_item));
  201. item->source = source;
  202. item->visible = true;
  203. item->parent = scene;
  204. vec2_set(&item->scale, 1.0f, 1.0f);
  205. if (source)
  206. obs_source_addref(source);
  207. pthread_mutex_lock(&scene->mutex);
  208. last = scene->first_item;
  209. if (!last) {
  210. scene->first_item = item;
  211. } else {
  212. while (last->next)
  213. last = last->next;
  214. last->next = item;
  215. item->prev = last;
  216. }
  217. pthread_mutex_unlock(&scene->mutex);
  218. calldata_setptr(&params, "scene", scene);
  219. calldata_setptr(&params, "item", item);
  220. signal_handler_signal(scene->source->signals, "add", &params);
  221. calldata_free(&params);
  222. return item;
  223. }
  224. int obs_sceneitem_destroy(obs_sceneitem_t item)
  225. {
  226. int ref = 0;
  227. if (item) {
  228. struct calldata params = {0};
  229. signal_item_destroy(item, &params);
  230. calldata_free(&params);
  231. pthread_mutex_lock(&item->parent->mutex);
  232. detach_sceneitem(item);
  233. pthread_mutex_unlock(&item->parent->mutex);
  234. if (item->source)
  235. ref = obs_source_release(item->source);
  236. bfree(item);
  237. }
  238. return ref;
  239. }
  240. obs_scene_t obs_sceneitem_getscene(obs_sceneitem_t item)
  241. {
  242. return item->parent;
  243. }
  244. obs_source_t obs_sceneitem_getsource(obs_sceneitem_t item)
  245. {
  246. return item->source;
  247. }
  248. void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
  249. {
  250. vec2_copy(&item->pos, pos);
  251. }
  252. void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
  253. {
  254. item->rot = rot;
  255. }
  256. void obs_sceneitem_setorigin(obs_sceneitem_t item, const struct vec2 *origin)
  257. {
  258. vec2_copy(&item->origin, origin);
  259. }
  260. void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
  261. {
  262. vec2_copy(&item->scale, scale);
  263. }
  264. void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
  265. {
  266. struct obs_scene *scene = item->parent;
  267. pthread_mutex_lock(&scene->mutex);
  268. detach_sceneitem(item);
  269. if (movement == ORDER_MOVE_UP) {
  270. attach_sceneitem(item, item->prev);
  271. } else if (movement == ORDER_MOVE_DOWN) {
  272. attach_sceneitem(item, item->next);
  273. } else if (movement == ORDER_MOVE_TOP) {
  274. struct obs_scene_item *last = item->next;
  275. if (!last) {
  276. last = item->prev;
  277. } else {
  278. while (last->next)
  279. last = last->next;
  280. }
  281. attach_sceneitem(item, last);
  282. } else if (movement == ORDER_MOVE_BOTTOM) {
  283. attach_sceneitem(item, NULL);
  284. }
  285. pthread_mutex_unlock(&scene->mutex);
  286. }
  287. void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
  288. {
  289. vec2_copy(pos, &item->pos);
  290. }
  291. float obs_sceneitem_getrot(obs_sceneitem_t item)
  292. {
  293. return item->rot;
  294. }
  295. void obs_sceneitem_getorigin(obs_sceneitem_t item, struct vec2 *origin)
  296. {
  297. vec2_copy(origin, &item->origin);
  298. }
  299. void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
  300. {
  301. vec2_copy(scale, &item->scale);
  302. }