obs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /******************************************************************************
  2. Copyright (C) 2013 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 "callback/calldata.h"
  15. #include "obs.h"
  16. #include "obs-data.h"
  17. #include "obs-module.h"
  18. struct obs_subsystem *obs = NULL;
  19. extern char *find_libobs_data_file(const char *file);
  20. static inline void make_gs_init_data(struct gs_init_data *gid,
  21. struct obs_video_info *ovi)
  22. {
  23. memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
  24. gid->cx = ovi->window_width;
  25. gid->cy = ovi->window_height;
  26. gid->num_backbuffers = 2;
  27. gid->format = GS_RGBA;
  28. gid->zsformat = GS_ZS_NONE;
  29. gid->adapter = ovi->adapter;
  30. }
  31. static inline void make_video_info(struct video_info *vi,
  32. struct obs_video_info *ovi)
  33. {
  34. vi->name = "video";
  35. vi->type = ovi->output_format;
  36. vi->fps_num = ovi->fps_num;
  37. vi->fps_den = ovi->fps_den;
  38. vi->width = ovi->output_width;
  39. vi->height = ovi->output_height;
  40. }
  41. static bool obs_init_textures(struct obs_video_info *ovi)
  42. {
  43. struct obs_video *video = &obs->video;
  44. size_t i;
  45. for (i = 0; i < NUM_TEXTURES; i++) {
  46. video->copy_surfaces[i] = gs_create_stagesurface(
  47. ovi->output_width, ovi->output_height,
  48. GS_RGBA);
  49. if (!video->copy_surfaces[i])
  50. return false;
  51. video->render_textures[i] = gs_create_texture(
  52. ovi->base_width, ovi->base_height,
  53. GS_RGBA, 1, NULL, GS_RENDERTARGET);
  54. if (!video->render_textures[i])
  55. return false;
  56. video->output_textures[i] = gs_create_texture(
  57. ovi->output_width, ovi->output_height,
  58. GS_RGBA, 1, NULL, GS_RENDERTARGET);
  59. if (!video->output_textures[i])
  60. return false;
  61. }
  62. return true;
  63. }
  64. static bool obs_init_graphics(struct obs_video_info *ovi)
  65. {
  66. struct obs_video *video = &obs->video;
  67. struct gs_init_data graphics_data;
  68. bool success = true;
  69. int errorcode;
  70. make_gs_init_data(&graphics_data, ovi);
  71. video->base_width = ovi->base_width;
  72. video->base_height = ovi->base_height;
  73. errorcode = gs_create(&video->graphics, ovi->graphics_module,
  74. &graphics_data);
  75. if (errorcode != GS_SUCCESS) {
  76. if (errorcode == GS_ERROR_MODULENOTFOUND)
  77. blog(LOG_ERROR, "Could not find graphics module '%s'",
  78. ovi->graphics_module);
  79. return false;
  80. }
  81. gs_entercontext(video->graphics);
  82. if (!obs_init_textures(ovi))
  83. success = false;
  84. if (success) {
  85. char *filename = find_libobs_data_file("default.effect");
  86. video->default_effect = gs_create_effect_from_file(filename,
  87. NULL);
  88. bfree(filename);
  89. if (!video->default_effect)
  90. success = false;
  91. }
  92. gs_leavecontext();
  93. return success;
  94. }
  95. static bool obs_init_video(struct obs_video_info *ovi)
  96. {
  97. struct obs_video *video = &obs->video;
  98. struct video_info vi;
  99. int errorcode;
  100. make_video_info(&vi, ovi);
  101. errorcode = video_output_open(&video->video, obs->media, &vi);
  102. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  103. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM)
  104. blog(LOG_ERROR, "Invalid video parameters specified");
  105. else
  106. blog(LOG_ERROR, "Could not open video output");
  107. return false;
  108. }
  109. errorcode = pthread_create(&video->video_thread, NULL,
  110. obs_video_thread, obs);
  111. if (errorcode != 0)
  112. return false;
  113. video->thread_initialized = true;
  114. return true;
  115. }
  116. static void obs_free_video()
  117. {
  118. struct obs_video *video = &obs->video;
  119. if (video->video) {
  120. void *thread_retval;
  121. video_output_stop(video->video);
  122. if (video->thread_initialized) {
  123. pthread_join(video->video_thread, &thread_retval);
  124. video->thread_initialized = false;
  125. }
  126. video_output_close(video->video);
  127. video->video = NULL;
  128. }
  129. }
  130. static void obs_free_graphics()
  131. {
  132. struct obs_video *video = &obs->video;
  133. size_t i;
  134. if (video->graphics) {
  135. int cur_texture = video->cur_texture;
  136. gs_entercontext(video->graphics);
  137. if (video->copy_mapped)
  138. stagesurface_unmap(video->copy_surfaces[cur_texture]);
  139. for (i = 0; i < NUM_TEXTURES; i++) {
  140. stagesurface_destroy(video->copy_surfaces[i]);
  141. texture_destroy(video->render_textures[i]);
  142. texture_destroy(video->output_textures[i]);
  143. video->copy_surfaces[i] = NULL;
  144. video->render_textures[i] = NULL;
  145. video->output_textures[i] = NULL;
  146. }
  147. effect_destroy(video->default_effect);
  148. video->default_effect = NULL;
  149. gs_leavecontext();
  150. gs_destroy(video->graphics);
  151. video->graphics = NULL;
  152. video->cur_texture = 0;
  153. }
  154. }
  155. static bool obs_init_audio(struct audio_info *ai)
  156. {
  157. struct obs_audio *audio = &obs->audio;
  158. int errorcode;
  159. /* TODO: sound subsystem */
  160. errorcode = audio_output_open(&audio->audio, obs->media, ai);
  161. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  162. return true;
  163. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  164. blog(LOG_ERROR, "Invalid audio parameters specified");
  165. else
  166. blog(LOG_ERROR, "Could not open audio output");
  167. return false;
  168. }
  169. static void obs_free_audio(void)
  170. {
  171. struct obs_audio *audio = &obs->audio;
  172. if (audio->audio)
  173. audio_output_close(audio->audio);
  174. memset(audio, 0, sizeof(struct obs_audio));
  175. }
  176. static bool obs_init_data(void)
  177. {
  178. struct obs_data *data = &obs->data;
  179. pthread_mutexattr_t attr;
  180. bool success = false;
  181. pthread_mutex_init_value(&obs->data.displays_mutex);
  182. if (pthread_mutexattr_init(&attr) != 0)
  183. return false;
  184. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  185. goto fail;
  186. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  187. goto fail;
  188. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  189. goto fail;
  190. success = true;
  191. fail:
  192. pthread_mutexattr_destroy(&attr);
  193. return success;
  194. }
  195. static void obs_free_data(void)
  196. {
  197. struct obs_data *data = &obs->data;
  198. uint32_t i;
  199. for (i = 0; i < MAX_CHANNELS; i++)
  200. obs_set_output_source(i, NULL);
  201. while (data->displays.num)
  202. obs_display_destroy(data->displays.array[0]);
  203. pthread_mutex_lock(&obs->data.sources_mutex);
  204. for (i = 0; i < data->sources.num; i++)
  205. obs_source_release(data->sources.array[i]);
  206. da_free(data->sources);
  207. pthread_mutex_unlock(&obs->data.sources_mutex);
  208. }
  209. static inline bool obs_init_handlers(void)
  210. {
  211. obs->signals = signal_handler_create();
  212. if (!obs->signals)
  213. return false;
  214. obs->procs = proc_handler_create();
  215. return (obs->procs != NULL);
  216. }
  217. static bool obs_init(void)
  218. {
  219. obs = bmalloc(sizeof(struct obs_subsystem));
  220. memset(obs, 0, sizeof(struct obs_subsystem));
  221. obs_init_data();
  222. if (!obs_init_handlers())
  223. return false;
  224. obs->media = media_open();
  225. if (!obs->media)
  226. return false;
  227. return true;
  228. }
  229. bool obs_startup()
  230. {
  231. bool success;
  232. if (obs) {
  233. blog(LOG_ERROR, "Tried to call obs_startup more than once");
  234. return false;
  235. }
  236. success = obs_init();
  237. if (!success)
  238. obs_shutdown();
  239. return success;
  240. }
  241. void obs_shutdown(void)
  242. {
  243. size_t i;
  244. if (!obs)
  245. return;
  246. da_free(obs->input_types);
  247. da_free(obs->filter_types);
  248. da_free(obs->transition_types);
  249. da_free(obs->output_types);
  250. da_free(obs->service_types);
  251. obs_free_data();
  252. obs_free_video();
  253. obs_free_graphics();
  254. obs_free_audio();
  255. media_close(obs->media);
  256. proc_handler_destroy(obs->procs);
  257. signal_handler_destroy(obs->signals);
  258. for (i = 0; i < obs->modules.num; i++)
  259. free_module(obs->modules.array+i);
  260. da_free(obs->modules);
  261. bfree(obs);
  262. obs = NULL;
  263. }
  264. bool obs_reset_video(struct obs_video_info *ovi)
  265. {
  266. struct obs_video *video = &obs->video;
  267. obs_free_video();
  268. if (!ovi) {
  269. obs_free_graphics();
  270. return true;
  271. }
  272. if (!video->graphics && !obs_init_graphics(ovi))
  273. return false;
  274. return obs_init_video(ovi);
  275. }
  276. bool obs_reset_audio(struct audio_info *ai)
  277. {
  278. obs_free_audio();
  279. if(!ai)
  280. return true;
  281. return obs_init_audio(ai);
  282. }
  283. bool obs_get_video_info(struct obs_video_info *ovi)
  284. {
  285. struct obs_video *video = &obs->video;
  286. const struct video_info *info;
  287. if (!obs || !video->graphics)
  288. return false;
  289. info = video_output_getinfo(video->video);
  290. memset(ovi, 0, sizeof(struct obs_video_info));
  291. ovi->base_width = video->base_width;
  292. ovi->base_height = video->base_height;
  293. ovi->output_width = info->width;
  294. ovi->output_height = info->height;
  295. ovi->output_format = info->type;
  296. ovi->fps_num = info->fps_num;
  297. ovi->fps_den = info->fps_den;
  298. return true;
  299. }
  300. bool obs_get_audio_info(struct audio_info *ai)
  301. {
  302. struct obs_audio *audio = &obs->audio;
  303. const struct audio_info *info;
  304. if (!obs || !audio->audio)
  305. return false;
  306. info = audio_output_getinfo(audio->audio);
  307. memcpy(ai, info, sizeof(struct audio_info));
  308. return true;
  309. }
  310. bool obs_enum_input_types(size_t idx, const char **id)
  311. {
  312. if (idx >= obs->input_types.num)
  313. return false;
  314. *id = obs->input_types.array[idx].id;
  315. return true;
  316. }
  317. bool obs_enum_filter_types(size_t idx, const char **id)
  318. {
  319. if (idx >= obs->filter_types.num)
  320. return false;
  321. *id = obs->filter_types.array[idx].id;
  322. return true;
  323. }
  324. bool obs_enum_transition_types(size_t idx, const char **id)
  325. {
  326. if (idx >= obs->transition_types.num)
  327. return false;
  328. *id = obs->transition_types.array[idx].id;
  329. return true;
  330. }
  331. bool obs_enum_output_types(size_t idx, const char **id)
  332. {
  333. if (idx >= obs->output_types.num)
  334. return false;
  335. *id = obs->output_types.array[idx].id;
  336. return true;
  337. }
  338. graphics_t obs_graphics(void)
  339. {
  340. return (obs != NULL) ? obs->video.graphics : NULL;
  341. }
  342. media_t obs_media(void)
  343. {
  344. return obs->media;
  345. }
  346. bool obs_add_source(obs_source_t source)
  347. {
  348. struct calldata params = {0};
  349. pthread_mutex_lock(&obs->data.sources_mutex);
  350. da_push_back(obs->data.sources, &source);
  351. obs_source_addref(source);
  352. pthread_mutex_unlock(&obs->data.sources_mutex);
  353. calldata_setptr(&params, "source", source);
  354. signal_handler_signal(obs->signals, "source-add", &params);
  355. calldata_free(&params);
  356. return true;
  357. }
  358. obs_source_t obs_get_output_source(uint32_t channel)
  359. {
  360. struct obs_source *source;
  361. assert(channel < MAX_CHANNELS);
  362. source = obs->data.channels[channel];
  363. obs_source_addref(source);
  364. return source;
  365. }
  366. void obs_set_output_source(uint32_t channel, obs_source_t source)
  367. {
  368. struct obs_source *prev_source;
  369. struct calldata params = {0};
  370. assert(channel < MAX_CHANNELS);
  371. prev_source = obs->data.channels[channel];
  372. calldata_setuint32(&params, "channel", channel);
  373. calldata_setptr(&params, "prev_source", prev_source);
  374. calldata_setptr(&params, "source", source);
  375. signal_handler_signal(obs->signals, "channel-change", &params);
  376. calldata_getptr(&params, "source", &source);
  377. calldata_free(&params);
  378. obs->data.channels[channel] = source;
  379. if (source != prev_source) {
  380. if (source)
  381. obs_source_addref(source);
  382. if (prev_source)
  383. obs_source_release(prev_source);
  384. }
  385. }
  386. void obs_enum_sources(bool (*enum_proc)(obs_source_t, void*), void *param)
  387. {
  388. struct obs_data *data = &obs->data;
  389. size_t i;
  390. pthread_mutex_lock(&data->sources_mutex);
  391. for (i = 0; i < data->sources.num; i++) {
  392. if (!enum_proc(data->sources.array[i], param))
  393. break;
  394. }
  395. pthread_mutex_unlock(&data->sources_mutex);
  396. }
  397. obs_source_t obs_get_source_by_name(const char *name)
  398. {
  399. struct obs_data *data = &obs->data;
  400. struct obs_source *source = NULL;
  401. size_t i;
  402. pthread_mutex_lock(&data->sources_mutex);
  403. for (i = 0; i < data->sources.num; i++) {
  404. struct obs_source *cur_source = data->sources.array[i];
  405. if (strcmp(cur_source->name, name) == 0) {
  406. source = cur_source;
  407. obs_source_addref(source);
  408. break;
  409. }
  410. }
  411. pthread_mutex_unlock(&data->sources_mutex);
  412. return source;
  413. }
  414. effect_t obs_get_default_effect(void)
  415. {
  416. return obs->video.default_effect;
  417. }
  418. signal_handler_t obs_signalhandler(void)
  419. {
  420. return obs->signals;
  421. }
  422. proc_handler_t obs_prochandler(void)
  423. {
  424. return obs->procs;
  425. }