obs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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_output_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_output_info vi;
  99. int errorcode;
  100. make_video_info(&vi, ovi);
  101. errorcode = video_output_open(&video->video, &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_output_info *ai)
  156. {
  157. struct obs_audio *audio = &obs->audio;
  158. int errorcode;
  159. /* TODO: sound subsystem */
  160. errorcode = audio_output_open(&audio->audio, 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. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  191. goto fail;
  192. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  193. goto fail;
  194. success = true;
  195. fail:
  196. pthread_mutexattr_destroy(&attr);
  197. return success;
  198. }
  199. static void obs_free_data(void)
  200. {
  201. struct obs_data *data = &obs->data;
  202. uint32_t i;
  203. for (i = 0; i < MAX_CHANNELS; i++)
  204. obs_set_output_source(i, NULL);
  205. while (data->outputs.num)
  206. obs_output_destroy(data->outputs.array[0]);
  207. while (data->encoders.num)
  208. obs_encoder_destroy(data->encoders.array[0]);
  209. while (data->displays.num)
  210. obs_display_destroy(data->displays.array[0]);
  211. pthread_mutex_lock(&obs->data.sources_mutex);
  212. for (i = 0; i < data->sources.num; i++)
  213. obs_source_release(data->sources.array[i]);
  214. da_free(data->sources);
  215. pthread_mutex_unlock(&obs->data.sources_mutex);
  216. pthread_mutex_destroy(&data->sources_mutex);
  217. pthread_mutex_destroy(&data->displays_mutex);
  218. pthread_mutex_destroy(&data->outputs_mutex);
  219. pthread_mutex_destroy(&data->encoders_mutex);
  220. }
  221. static inline bool obs_init_handlers(void)
  222. {
  223. obs->signals = signal_handler_create();
  224. if (!obs->signals)
  225. return false;
  226. obs->procs = proc_handler_create();
  227. return (obs->procs != NULL);
  228. }
  229. static bool obs_init(void)
  230. {
  231. obs = bmalloc(sizeof(struct obs_subsystem));
  232. memset(obs, 0, sizeof(struct obs_subsystem));
  233. obs_init_data();
  234. return obs_init_handlers();
  235. }
  236. bool obs_startup()
  237. {
  238. bool success;
  239. if (obs) {
  240. blog(LOG_ERROR, "Tried to call obs_startup more than once");
  241. return false;
  242. }
  243. success = obs_init();
  244. if (!success)
  245. obs_shutdown();
  246. return success;
  247. }
  248. void obs_shutdown(void)
  249. {
  250. size_t i;
  251. if (!obs)
  252. return;
  253. da_free(obs->input_types);
  254. da_free(obs->filter_types);
  255. da_free(obs->transition_types);
  256. da_free(obs->output_types);
  257. da_free(obs->service_types);
  258. obs_free_data();
  259. obs_free_video();
  260. obs_free_graphics();
  261. obs_free_audio();
  262. proc_handler_destroy(obs->procs);
  263. signal_handler_destroy(obs->signals);
  264. for (i = 0; i < obs->modules.num; i++)
  265. free_module(obs->modules.array+i);
  266. da_free(obs->modules);
  267. bfree(obs);
  268. obs = NULL;
  269. }
  270. bool obs_reset_video(struct obs_video_info *ovi)
  271. {
  272. struct obs_video *video = &obs->video;
  273. obs_free_video();
  274. if (!ovi) {
  275. obs_free_graphics();
  276. return true;
  277. }
  278. if (!video->graphics && !obs_init_graphics(ovi))
  279. return false;
  280. return obs_init_video(ovi);
  281. }
  282. bool obs_reset_audio(struct audio_output_info *ai)
  283. {
  284. obs_free_audio();
  285. if(!ai)
  286. return true;
  287. return obs_init_audio(ai);
  288. }
  289. bool obs_get_video_info(struct obs_video_info *ovi)
  290. {
  291. struct obs_video *video = &obs->video;
  292. const struct video_output_info *info;
  293. if (!obs || !video->graphics)
  294. return false;
  295. info = video_output_getinfo(video->video);
  296. memset(ovi, 0, sizeof(struct obs_video_info));
  297. ovi->base_width = video->base_width;
  298. ovi->base_height = video->base_height;
  299. ovi->output_width = info->width;
  300. ovi->output_height = info->height;
  301. ovi->output_format = info->type;
  302. ovi->fps_num = info->fps_num;
  303. ovi->fps_den = info->fps_den;
  304. return true;
  305. }
  306. bool obs_get_audio_info(struct audio_output_info *aoi)
  307. {
  308. struct obs_audio *audio = &obs->audio;
  309. const struct audio_output_info *info;
  310. if (!obs || !audio->audio)
  311. return false;
  312. info = audio_output_getinfo(audio->audio);
  313. memcpy(aoi, info, sizeof(struct audio_output_info));
  314. return true;
  315. }
  316. bool obs_enum_input_types(size_t idx, const char **id)
  317. {
  318. if (idx >= obs->input_types.num)
  319. return false;
  320. *id = obs->input_types.array[idx].id;
  321. return true;
  322. }
  323. bool obs_enum_filter_types(size_t idx, const char **id)
  324. {
  325. if (idx >= obs->filter_types.num)
  326. return false;
  327. *id = obs->filter_types.array[idx].id;
  328. return true;
  329. }
  330. bool obs_enum_transition_types(size_t idx, const char **id)
  331. {
  332. if (idx >= obs->transition_types.num)
  333. return false;
  334. *id = obs->transition_types.array[idx].id;
  335. return true;
  336. }
  337. bool obs_enum_output_types(size_t idx, const char **id)
  338. {
  339. if (idx >= obs->output_types.num)
  340. return false;
  341. *id = obs->output_types.array[idx].id;
  342. return true;
  343. }
  344. graphics_t obs_graphics(void)
  345. {
  346. return (obs != NULL) ? obs->video.graphics : NULL;
  347. }
  348. audio_t obs_audio(void)
  349. {
  350. return (obs != NULL) ? obs->audio.audio : NULL;
  351. }
  352. video_t obs_video(void)
  353. {
  354. return (obs != NULL) ? obs->video.video : NULL;
  355. }
  356. bool obs_add_source(obs_source_t source)
  357. {
  358. struct calldata params = {0};
  359. pthread_mutex_lock(&obs->data.sources_mutex);
  360. da_push_back(obs->data.sources, &source);
  361. obs_source_addref(source);
  362. pthread_mutex_unlock(&obs->data.sources_mutex);
  363. calldata_setptr(&params, "source", source);
  364. signal_handler_signal(obs->signals, "source-add", &params);
  365. calldata_free(&params);
  366. return true;
  367. }
  368. obs_source_t obs_get_output_source(uint32_t channel)
  369. {
  370. struct obs_source *source;
  371. assert(channel < MAX_CHANNELS);
  372. source = obs->data.channels[channel];
  373. obs_source_addref(source);
  374. return source;
  375. }
  376. void obs_set_output_source(uint32_t channel, obs_source_t source)
  377. {
  378. struct obs_source *prev_source;
  379. struct calldata params = {0};
  380. assert(channel < MAX_CHANNELS);
  381. prev_source = obs->data.channels[channel];
  382. calldata_setuint32(&params, "channel", channel);
  383. calldata_setptr(&params, "prev_source", prev_source);
  384. calldata_setptr(&params, "source", source);
  385. signal_handler_signal(obs->signals, "channel-change", &params);
  386. calldata_getptr(&params, "source", &source);
  387. calldata_free(&params);
  388. obs->data.channels[channel] = source;
  389. if (source != prev_source) {
  390. if (source)
  391. obs_source_addref(source);
  392. if (prev_source)
  393. obs_source_release(prev_source);
  394. }
  395. }
  396. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t), void *param)
  397. {
  398. struct obs_data *data = &obs->data;
  399. pthread_mutex_lock(&data->outputs_mutex);
  400. for (size_t i = 0; i < data->outputs.num; i++)
  401. if (!enum_proc(param, data->outputs.array[i]))
  402. break;
  403. pthread_mutex_unlock(&data->outputs_mutex);
  404. }
  405. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t), void *param)
  406. {
  407. struct obs_data *data = &obs->data;
  408. pthread_mutex_lock(&data->encoders_mutex);
  409. for (size_t i = 0; i < data->encoders.num; i++)
  410. if (!enum_proc(param, data->encoders.array[i]))
  411. break;
  412. pthread_mutex_unlock(&data->encoders_mutex);
  413. }
  414. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t), void *param)
  415. {
  416. struct obs_data *data = &obs->data;
  417. pthread_mutex_lock(&data->sources_mutex);
  418. for (size_t i = 0; i < data->sources.num; i++)
  419. if (!enum_proc(param, data->sources.array[i]))
  420. break;
  421. pthread_mutex_unlock(&data->sources_mutex);
  422. }
  423. obs_source_t obs_get_source_by_name(const char *name)
  424. {
  425. struct obs_data *data = &obs->data;
  426. struct obs_source *source = NULL;
  427. size_t i;
  428. pthread_mutex_lock(&data->sources_mutex);
  429. for (i = 0; i < data->sources.num; i++) {
  430. struct obs_source *cur_source = data->sources.array[i];
  431. if (strcmp(cur_source->name, name) == 0) {
  432. source = cur_source;
  433. obs_source_addref(source);
  434. break;
  435. }
  436. }
  437. pthread_mutex_unlock(&data->sources_mutex);
  438. return source;
  439. }
  440. effect_t obs_get_default_effect(void)
  441. {
  442. return obs->video.default_effect;
  443. }
  444. signal_handler_t obs_signalhandler(void)
  445. {
  446. return obs->signals;
  447. }
  448. proc_handler_t obs_prochandler(void)
  449. {
  450. return obs->procs;
  451. }