obs-scripting.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /******************************************************************************
  2. Copyright (C) 2017 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 <util/dstr.h>
  16. #include <util/platform.h>
  17. #include <util/threading.h>
  18. #include <util/circlebuf.h>
  19. #include "obs-scripting-internal.h"
  20. #include "obs-scripting-callback.h"
  21. #include "obs-scripting-config.h"
  22. #if COMPILE_LUA
  23. extern obs_script_t *obs_lua_script_create(const char *path,
  24. obs_data_t *settings);
  25. extern bool obs_lua_script_load(obs_script_t *s);
  26. extern void obs_lua_script_unload(obs_script_t *s);
  27. extern void obs_lua_script_destroy(obs_script_t *s);
  28. extern void obs_lua_load(void);
  29. extern void obs_lua_unload(void);
  30. extern obs_properties_t *obs_lua_script_get_properties(obs_script_t *script);
  31. extern void obs_lua_script_update(obs_script_t *script, obs_data_t *settings);
  32. extern void obs_lua_script_save(obs_script_t *script);
  33. #endif
  34. #if COMPILE_PYTHON
  35. extern obs_script_t *obs_python_script_create(const char *path,
  36. obs_data_t *settings);
  37. extern bool obs_python_script_load(obs_script_t *s);
  38. extern void obs_python_script_unload(obs_script_t *s);
  39. extern void obs_python_script_destroy(obs_script_t *s);
  40. extern void obs_python_load(void);
  41. extern void obs_python_unload(void);
  42. extern obs_properties_t *obs_python_script_get_properties(obs_script_t *script);
  43. extern void obs_python_script_update(obs_script_t *script, obs_data_t *settings);
  44. extern void obs_python_script_save(obs_script_t *script);
  45. #endif
  46. pthread_mutex_t detach_mutex;
  47. struct script_callback *detached_callbacks;
  48. static struct dstr file_filter = {0};
  49. static bool scripting_loaded = false;
  50. static const char *supported_formats[] = {
  51. #if COMPILE_LUA
  52. "lua",
  53. #endif
  54. #if COMPILE_PYTHON
  55. "py",
  56. #endif
  57. NULL
  58. };
  59. /* -------------------------------------------- */
  60. static pthread_mutex_t defer_call_mutex;
  61. static struct circlebuf defer_call_queue;
  62. static bool defer_call_exit = false;
  63. static os_sem_t *defer_call_semaphore;
  64. static pthread_t defer_call_thread;
  65. struct defer_call {
  66. defer_call_cb call;
  67. void *cb;
  68. };
  69. static void *defer_thread(void *unused)
  70. {
  71. UNUSED_PARAMETER(unused);
  72. while (os_sem_wait(defer_call_semaphore) == 0) {
  73. struct defer_call info;
  74. pthread_mutex_lock(&defer_call_mutex);
  75. if (defer_call_exit) {
  76. pthread_mutex_unlock(&defer_call_mutex);
  77. return NULL;
  78. }
  79. circlebuf_pop_front(&defer_call_queue, &info, sizeof(info));
  80. pthread_mutex_unlock(&defer_call_mutex);
  81. info.call(info.cb);
  82. }
  83. return NULL;
  84. }
  85. void defer_call_post(defer_call_cb call, void *cb)
  86. {
  87. struct defer_call info;
  88. info.call = call;
  89. info.cb = cb;
  90. pthread_mutex_lock(&defer_call_mutex);
  91. if (!defer_call_exit)
  92. circlebuf_push_back(&defer_call_queue, &info, sizeof(info));
  93. pthread_mutex_unlock(&defer_call_mutex);
  94. os_sem_post(defer_call_semaphore);
  95. }
  96. /* -------------------------------------------- */
  97. bool obs_scripting_load(void)
  98. {
  99. circlebuf_init(&defer_call_queue);
  100. if (pthread_mutex_init(&detach_mutex, NULL) != 0) {
  101. return false;
  102. }
  103. if (pthread_mutex_init(&defer_call_mutex, NULL) != 0) {
  104. pthread_mutex_destroy(&detach_mutex);
  105. return false;
  106. }
  107. if (os_sem_init(&defer_call_semaphore, 0) != 0) {
  108. pthread_mutex_destroy(&defer_call_mutex);
  109. pthread_mutex_destroy(&detach_mutex);
  110. return false;
  111. }
  112. if (pthread_create(&defer_call_thread, NULL, defer_thread, NULL) != 0) {
  113. os_sem_destroy(defer_call_semaphore);
  114. pthread_mutex_destroy(&defer_call_mutex);
  115. pthread_mutex_destroy(&detach_mutex);
  116. return false;
  117. }
  118. #if COMPILE_LUA
  119. obs_lua_load();
  120. #endif
  121. #if COMPILE_PYTHON
  122. obs_python_load();
  123. obs_scripting_load_python(NULL);
  124. #endif
  125. scripting_loaded = true;
  126. return true;
  127. }
  128. void obs_scripting_unload(void)
  129. {
  130. if (!scripting_loaded)
  131. return;
  132. /* ---------------------- */
  133. #if COMPILE_LUA
  134. obs_lua_unload();
  135. #endif
  136. #if COMPILE_PYTHON
  137. obs_python_unload();
  138. #endif
  139. dstr_free(&file_filter);
  140. /* ---------------------- */
  141. int total_detached = 0;
  142. pthread_mutex_lock(&detach_mutex);
  143. struct script_callback *cur = detached_callbacks;
  144. while (cur) {
  145. struct script_callback *next = cur->next;
  146. just_free_script_callback(cur);
  147. cur = next;
  148. ++total_detached;
  149. }
  150. pthread_mutex_unlock(&detach_mutex);
  151. pthread_mutex_destroy(&detach_mutex);
  152. blog(LOG_INFO, "[Scripting] Total detached callbacks: %d",
  153. total_detached);
  154. /* ---------------------- */
  155. pthread_mutex_lock(&defer_call_mutex);
  156. /* TODO */
  157. defer_call_exit = true;
  158. circlebuf_free(&defer_call_queue);
  159. pthread_mutex_unlock(&defer_call_mutex);
  160. os_sem_post(defer_call_semaphore);
  161. pthread_join(defer_call_thread, NULL);
  162. pthread_mutex_destroy(&defer_call_mutex);
  163. os_sem_destroy(defer_call_semaphore);
  164. }
  165. const char **obs_scripting_supported_formats(void)
  166. {
  167. return supported_formats;
  168. }
  169. static inline bool pointer_valid(const void *x, const char *name,
  170. const char *func)
  171. {
  172. if (!x) {
  173. blog(LOG_WARNING, "obs-scripting: [%s] %s is null",
  174. func, name);
  175. return false;
  176. }
  177. return true;
  178. }
  179. #define ptr_valid(x) pointer_valid(x, #x, __FUNCTION__)
  180. obs_script_t *obs_script_create(const char *path, obs_data_t *settings)
  181. {
  182. obs_script_t *script = NULL;
  183. const char *ext;
  184. if (!scripting_loaded)
  185. return NULL;
  186. if (!ptr_valid(path))
  187. return NULL;
  188. ext = strrchr(path, '.');
  189. if (!ext)
  190. return NULL;
  191. #if COMPILE_LUA
  192. if (strcmp(ext, ".lua") == 0) {
  193. script = obs_lua_script_create(path, settings);
  194. } else
  195. #endif
  196. #if COMPILE_PYTHON
  197. if (strcmp(ext, ".py") == 0) {
  198. script = obs_python_script_create(path, settings);
  199. } else
  200. #endif
  201. {
  202. blog(LOG_WARNING, "Unsupported/unknown script type: %s", path);
  203. }
  204. return script;
  205. }
  206. const char *obs_script_get_description(const obs_script_t *script)
  207. {
  208. return ptr_valid(script) ? script->desc.array : NULL;
  209. }
  210. const char *obs_script_get_path(const obs_script_t *script)
  211. {
  212. const char *path = ptr_valid(script) ? script->path.array : "";
  213. return path ? path : "";
  214. }
  215. const char *obs_script_get_file(const obs_script_t *script)
  216. {
  217. const char *file = ptr_valid(script) ? script->file.array : "";
  218. return file ? file : "";
  219. }
  220. enum obs_script_lang obs_script_get_lang(const obs_script_t *script)
  221. {
  222. return ptr_valid(script) ? script->type : OBS_SCRIPT_LANG_UNKNOWN;
  223. }
  224. obs_data_t *obs_script_get_settings(obs_script_t *script)
  225. {
  226. obs_data_t *settings;
  227. if (!ptr_valid(script))
  228. return NULL;
  229. settings = script->settings;
  230. obs_data_addref(settings);
  231. return settings;
  232. }
  233. obs_properties_t *obs_script_get_properties(obs_script_t *script)
  234. {
  235. obs_properties_t *props = NULL;
  236. if (!ptr_valid(script))
  237. return NULL;
  238. #if COMPILE_LUA
  239. if (script->type == OBS_SCRIPT_LANG_LUA) {
  240. props = obs_lua_script_get_properties(script);
  241. goto out;
  242. }
  243. #endif
  244. #if COMPILE_PYTHON
  245. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  246. props = obs_python_script_get_properties(script);
  247. goto out;
  248. }
  249. #endif
  250. out:
  251. if (!props)
  252. props = obs_properties_create();
  253. return props;
  254. }
  255. obs_data_t *obs_script_save(obs_script_t *script)
  256. {
  257. obs_data_t *settings;
  258. if (!ptr_valid(script))
  259. return NULL;
  260. #if COMPILE_LUA
  261. if (script->type == OBS_SCRIPT_LANG_LUA) {
  262. obs_lua_script_save(script);
  263. goto out;
  264. }
  265. #endif
  266. #if COMPILE_PYTHON
  267. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  268. obs_python_script_save(script);
  269. goto out;
  270. }
  271. #endif
  272. out:
  273. settings = script->settings;
  274. obs_data_addref(settings);
  275. return settings;
  276. }
  277. static void clear_queue_signal(void *p_event)
  278. {
  279. os_event_t *event = p_event;
  280. os_event_signal(event);
  281. }
  282. static void clear_call_queue(void)
  283. {
  284. os_event_t *event;
  285. if (os_event_init(&event, OS_EVENT_TYPE_AUTO) != 0)
  286. return;
  287. defer_call_post(clear_queue_signal, event);
  288. os_event_wait(event);
  289. os_event_destroy(event);
  290. }
  291. void obs_script_update(obs_script_t *script, obs_data_t *settings)
  292. {
  293. if (!ptr_valid(script))
  294. return;
  295. #if COMPILE_LUA
  296. if (script->type == OBS_SCRIPT_LANG_LUA) {
  297. obs_lua_script_update(script, settings);
  298. }
  299. #endif
  300. #if COMPILE_PYTHON
  301. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  302. obs_python_script_update(script, settings);
  303. }
  304. #endif
  305. }
  306. bool obs_script_reload(obs_script_t *script)
  307. {
  308. if (!scripting_loaded)
  309. return false;
  310. if (!ptr_valid(script))
  311. return false;
  312. #if COMPILE_LUA
  313. if (script->type == OBS_SCRIPT_LANG_LUA) {
  314. obs_lua_script_unload(script);
  315. clear_call_queue();
  316. obs_lua_script_load(script);
  317. goto out;
  318. }
  319. #endif
  320. #if COMPILE_PYTHON
  321. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  322. obs_python_script_unload(script);
  323. clear_call_queue();
  324. obs_python_script_load(script);
  325. goto out;
  326. }
  327. #endif
  328. out:
  329. return script->loaded;
  330. }
  331. bool obs_script_loaded(const obs_script_t *script)
  332. {
  333. return ptr_valid(script) ? script->loaded : false;
  334. }
  335. void obs_script_destroy(obs_script_t *script)
  336. {
  337. if (!script)
  338. return;
  339. #if COMPILE_LUA
  340. if (script->type == OBS_SCRIPT_LANG_LUA) {
  341. obs_lua_script_unload(script);
  342. obs_lua_script_destroy(script);
  343. return;
  344. }
  345. #endif
  346. #if COMPILE_PYTHON
  347. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  348. obs_python_script_unload(script);
  349. obs_python_script_destroy(script);
  350. return;
  351. }
  352. #endif
  353. }
  354. #if !COMPILE_PYTHON
  355. bool obs_scripting_load_python(const char *python_path)
  356. {
  357. UNUSED_PARAMETER(python_path);
  358. return false;
  359. }
  360. bool obs_scripting_python_loaded(void)
  361. {
  362. return false;
  363. }
  364. bool obs_scripting_python_runtime_linked(void)
  365. {
  366. return (bool)true;
  367. }
  368. #endif