obs-scripting.c 10 KB

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