obs-scripting.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #ifndef _WIN32 /* don't risk python startup load issues on windows */
  124. obs_scripting_load_python(NULL);
  125. #endif
  126. #endif
  127. scripting_loaded = true;
  128. return true;
  129. }
  130. void obs_scripting_unload(void)
  131. {
  132. if (!scripting_loaded)
  133. return;
  134. /* ---------------------- */
  135. #if COMPILE_LUA
  136. obs_lua_unload();
  137. #endif
  138. #if COMPILE_PYTHON
  139. obs_python_unload();
  140. #endif
  141. dstr_free(&file_filter);
  142. /* ---------------------- */
  143. int total_detached = 0;
  144. pthread_mutex_lock(&detach_mutex);
  145. struct script_callback *cur = detached_callbacks;
  146. while (cur) {
  147. struct script_callback *next = cur->next;
  148. just_free_script_callback(cur);
  149. cur = next;
  150. ++total_detached;
  151. }
  152. pthread_mutex_unlock(&detach_mutex);
  153. pthread_mutex_destroy(&detach_mutex);
  154. blog(LOG_INFO, "[Scripting] Total detached callbacks: %d",
  155. total_detached);
  156. /* ---------------------- */
  157. pthread_mutex_lock(&defer_call_mutex);
  158. /* TODO */
  159. defer_call_exit = true;
  160. circlebuf_free(&defer_call_queue);
  161. pthread_mutex_unlock(&defer_call_mutex);
  162. os_sem_post(defer_call_semaphore);
  163. pthread_join(defer_call_thread, NULL);
  164. pthread_mutex_destroy(&defer_call_mutex);
  165. os_sem_destroy(defer_call_semaphore);
  166. }
  167. const char **obs_scripting_supported_formats(void)
  168. {
  169. return supported_formats;
  170. }
  171. static inline bool pointer_valid(const void *x, const char *name,
  172. const char *func)
  173. {
  174. if (!x) {
  175. blog(LOG_WARNING, "obs-scripting: [%s] %s is null",
  176. func, name);
  177. return false;
  178. }
  179. return true;
  180. }
  181. #define ptr_valid(x) pointer_valid(x, #x, __FUNCTION__)
  182. obs_script_t *obs_script_create(const char *path, obs_data_t *settings)
  183. {
  184. obs_script_t *script = NULL;
  185. const char *ext;
  186. if (!scripting_loaded)
  187. return NULL;
  188. if (!ptr_valid(path))
  189. return NULL;
  190. ext = strrchr(path, '.');
  191. if (!ext)
  192. return NULL;
  193. #if COMPILE_LUA
  194. if (strcmp(ext, ".lua") == 0) {
  195. script = obs_lua_script_create(path, settings);
  196. } else
  197. #endif
  198. #if COMPILE_PYTHON
  199. if (strcmp(ext, ".py") == 0) {
  200. script = obs_python_script_create(path, settings);
  201. } else
  202. #endif
  203. {
  204. blog(LOG_WARNING, "Unsupported/unknown script type: %s", path);
  205. }
  206. return script;
  207. }
  208. const char *obs_script_get_description(const obs_script_t *script)
  209. {
  210. return ptr_valid(script) ? script->desc.array : NULL;
  211. }
  212. const char *obs_script_get_path(const obs_script_t *script)
  213. {
  214. const char *path = ptr_valid(script) ? script->path.array : "";
  215. return path ? path : "";
  216. }
  217. const char *obs_script_get_file(const obs_script_t *script)
  218. {
  219. const char *file = ptr_valid(script) ? script->file.array : "";
  220. return file ? file : "";
  221. }
  222. enum obs_script_lang obs_script_get_lang(const obs_script_t *script)
  223. {
  224. return ptr_valid(script) ? script->type : OBS_SCRIPT_LANG_UNKNOWN;
  225. }
  226. obs_data_t *obs_script_get_settings(obs_script_t *script)
  227. {
  228. obs_data_t *settings;
  229. if (!ptr_valid(script))
  230. return NULL;
  231. settings = script->settings;
  232. obs_data_addref(settings);
  233. return settings;
  234. }
  235. obs_properties_t *obs_script_get_properties(obs_script_t *script)
  236. {
  237. obs_properties_t *props = NULL;
  238. if (!ptr_valid(script))
  239. return NULL;
  240. #if COMPILE_LUA
  241. if (script->type == OBS_SCRIPT_LANG_LUA) {
  242. props = obs_lua_script_get_properties(script);
  243. goto out;
  244. }
  245. #endif
  246. #if COMPILE_PYTHON
  247. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  248. props = obs_python_script_get_properties(script);
  249. goto out;
  250. }
  251. #endif
  252. out:
  253. if (!props)
  254. props = obs_properties_create();
  255. return props;
  256. }
  257. obs_data_t *obs_script_save(obs_script_t *script)
  258. {
  259. obs_data_t *settings;
  260. if (!ptr_valid(script))
  261. return NULL;
  262. #if COMPILE_LUA
  263. if (script->type == OBS_SCRIPT_LANG_LUA) {
  264. obs_lua_script_save(script);
  265. goto out;
  266. }
  267. #endif
  268. #if COMPILE_PYTHON
  269. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  270. obs_python_script_save(script);
  271. goto out;
  272. }
  273. #endif
  274. out:
  275. settings = script->settings;
  276. obs_data_addref(settings);
  277. return settings;
  278. }
  279. static void clear_queue_signal(void *p_event)
  280. {
  281. os_event_t *event = p_event;
  282. os_event_signal(event);
  283. }
  284. static void clear_call_queue(void)
  285. {
  286. os_event_t *event;
  287. if (os_event_init(&event, OS_EVENT_TYPE_AUTO) != 0)
  288. return;
  289. defer_call_post(clear_queue_signal, event);
  290. os_event_wait(event);
  291. os_event_destroy(event);
  292. }
  293. void obs_script_update(obs_script_t *script, obs_data_t *settings)
  294. {
  295. if (!ptr_valid(script))
  296. return;
  297. #if COMPILE_LUA
  298. if (script->type == OBS_SCRIPT_LANG_LUA) {
  299. obs_lua_script_update(script, settings);
  300. }
  301. #endif
  302. #if COMPILE_PYTHON
  303. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  304. obs_python_script_update(script, settings);
  305. }
  306. #endif
  307. }
  308. bool obs_script_reload(obs_script_t *script)
  309. {
  310. if (!scripting_loaded)
  311. return false;
  312. if (!ptr_valid(script))
  313. return false;
  314. #if COMPILE_LUA
  315. if (script->type == OBS_SCRIPT_LANG_LUA) {
  316. obs_lua_script_unload(script);
  317. clear_call_queue();
  318. obs_lua_script_load(script);
  319. goto out;
  320. }
  321. #endif
  322. #if COMPILE_PYTHON
  323. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  324. obs_python_script_unload(script);
  325. clear_call_queue();
  326. obs_python_script_load(script);
  327. goto out;
  328. }
  329. #endif
  330. out:
  331. return script->loaded;
  332. }
  333. bool obs_script_loaded(const obs_script_t *script)
  334. {
  335. return ptr_valid(script) ? script->loaded : false;
  336. }
  337. void obs_script_destroy(obs_script_t *script)
  338. {
  339. if (!script)
  340. return;
  341. #if COMPILE_LUA
  342. if (script->type == OBS_SCRIPT_LANG_LUA) {
  343. obs_lua_script_unload(script);
  344. obs_lua_script_destroy(script);
  345. return;
  346. }
  347. #endif
  348. #if COMPILE_PYTHON
  349. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  350. obs_python_script_unload(script);
  351. obs_python_script_destroy(script);
  352. return;
  353. }
  354. #endif
  355. }
  356. #if !COMPILE_PYTHON
  357. bool obs_scripting_load_python(const char *python_path)
  358. {
  359. UNUSED_PARAMETER(python_path);
  360. return false;
  361. }
  362. bool obs_scripting_python_loaded(void)
  363. {
  364. return false;
  365. }
  366. bool obs_scripting_python_runtime_linked(void)
  367. {
  368. return (bool)true;
  369. }
  370. #endif