obs-scripting.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. scripting_loaded = false;
  167. }
  168. const char **obs_scripting_supported_formats(void)
  169. {
  170. return supported_formats;
  171. }
  172. static inline bool pointer_valid(const void *x, const char *name,
  173. const char *func)
  174. {
  175. if (!x) {
  176. blog(LOG_WARNING, "obs-scripting: [%s] %s is null",
  177. func, name);
  178. return false;
  179. }
  180. return true;
  181. }
  182. #define ptr_valid(x) pointer_valid(x, #x, __FUNCTION__)
  183. obs_script_t *obs_script_create(const char *path, obs_data_t *settings)
  184. {
  185. obs_script_t *script = NULL;
  186. const char *ext;
  187. if (!scripting_loaded)
  188. return NULL;
  189. if (!ptr_valid(path))
  190. return NULL;
  191. ext = strrchr(path, '.');
  192. if (!ext)
  193. return NULL;
  194. #if COMPILE_LUA
  195. if (strcmp(ext, ".lua") == 0) {
  196. script = obs_lua_script_create(path, settings);
  197. } else
  198. #endif
  199. #if COMPILE_PYTHON
  200. if (strcmp(ext, ".py") == 0) {
  201. script = obs_python_script_create(path, settings);
  202. } else
  203. #endif
  204. {
  205. blog(LOG_WARNING, "Unsupported/unknown script type: %s", path);
  206. }
  207. return script;
  208. }
  209. const char *obs_script_get_description(const obs_script_t *script)
  210. {
  211. return ptr_valid(script) ? script->desc.array : NULL;
  212. }
  213. const char *obs_script_get_path(const obs_script_t *script)
  214. {
  215. const char *path = ptr_valid(script) ? script->path.array : "";
  216. return path ? path : "";
  217. }
  218. const char *obs_script_get_file(const obs_script_t *script)
  219. {
  220. const char *file = ptr_valid(script) ? script->file.array : "";
  221. return file ? file : "";
  222. }
  223. enum obs_script_lang obs_script_get_lang(const obs_script_t *script)
  224. {
  225. return ptr_valid(script) ? script->type : OBS_SCRIPT_LANG_UNKNOWN;
  226. }
  227. obs_data_t *obs_script_get_settings(obs_script_t *script)
  228. {
  229. obs_data_t *settings;
  230. if (!ptr_valid(script))
  231. return NULL;
  232. settings = script->settings;
  233. obs_data_addref(settings);
  234. return settings;
  235. }
  236. obs_properties_t *obs_script_get_properties(obs_script_t *script)
  237. {
  238. obs_properties_t *props = NULL;
  239. if (!ptr_valid(script))
  240. return NULL;
  241. #if COMPILE_LUA
  242. if (script->type == OBS_SCRIPT_LANG_LUA) {
  243. props = obs_lua_script_get_properties(script);
  244. goto out;
  245. }
  246. #endif
  247. #if COMPILE_PYTHON
  248. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  249. props = obs_python_script_get_properties(script);
  250. goto out;
  251. }
  252. #endif
  253. out:
  254. if (!props)
  255. props = obs_properties_create();
  256. return props;
  257. }
  258. obs_data_t *obs_script_save(obs_script_t *script)
  259. {
  260. obs_data_t *settings;
  261. if (!ptr_valid(script))
  262. return NULL;
  263. #if COMPILE_LUA
  264. if (script->type == OBS_SCRIPT_LANG_LUA) {
  265. obs_lua_script_save(script);
  266. goto out;
  267. }
  268. #endif
  269. #if COMPILE_PYTHON
  270. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  271. obs_python_script_save(script);
  272. goto out;
  273. }
  274. #endif
  275. out:
  276. settings = script->settings;
  277. obs_data_addref(settings);
  278. return settings;
  279. }
  280. static void clear_queue_signal(void *p_event)
  281. {
  282. os_event_t *event = p_event;
  283. os_event_signal(event);
  284. }
  285. static void clear_call_queue(void)
  286. {
  287. os_event_t *event;
  288. if (os_event_init(&event, OS_EVENT_TYPE_AUTO) != 0)
  289. return;
  290. defer_call_post(clear_queue_signal, event);
  291. os_event_wait(event);
  292. os_event_destroy(event);
  293. }
  294. void obs_script_update(obs_script_t *script, obs_data_t *settings)
  295. {
  296. if (!ptr_valid(script))
  297. return;
  298. #if COMPILE_LUA
  299. if (script->type == OBS_SCRIPT_LANG_LUA) {
  300. obs_lua_script_update(script, settings);
  301. }
  302. #endif
  303. #if COMPILE_PYTHON
  304. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  305. obs_python_script_update(script, settings);
  306. }
  307. #endif
  308. }
  309. bool obs_script_reload(obs_script_t *script)
  310. {
  311. if (!scripting_loaded)
  312. return false;
  313. if (!ptr_valid(script))
  314. return false;
  315. #if COMPILE_LUA
  316. if (script->type == OBS_SCRIPT_LANG_LUA) {
  317. obs_lua_script_unload(script);
  318. clear_call_queue();
  319. obs_lua_script_load(script);
  320. goto out;
  321. }
  322. #endif
  323. #if COMPILE_PYTHON
  324. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  325. obs_python_script_unload(script);
  326. clear_call_queue();
  327. obs_python_script_load(script);
  328. goto out;
  329. }
  330. #endif
  331. out:
  332. return script->loaded;
  333. }
  334. bool obs_script_loaded(const obs_script_t *script)
  335. {
  336. return ptr_valid(script) ? script->loaded : false;
  337. }
  338. void obs_script_destroy(obs_script_t *script)
  339. {
  340. if (!script)
  341. return;
  342. #if COMPILE_LUA
  343. if (script->type == OBS_SCRIPT_LANG_LUA) {
  344. obs_lua_script_unload(script);
  345. obs_lua_script_destroy(script);
  346. return;
  347. }
  348. #endif
  349. #if COMPILE_PYTHON
  350. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  351. obs_python_script_unload(script);
  352. obs_python_script_destroy(script);
  353. return;
  354. }
  355. #endif
  356. }
  357. #if !COMPILE_PYTHON
  358. bool obs_scripting_load_python(const char *python_path)
  359. {
  360. UNUSED_PARAMETER(python_path);
  361. return false;
  362. }
  363. bool obs_scripting_python_loaded(void)
  364. {
  365. return false;
  366. }
  367. bool obs_scripting_python_runtime_linked(void)
  368. {
  369. return (bool)true;
  370. }
  371. #endif