obs-scripting.c 10 KB

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