obs.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "obs.h"
  15. #include "obs-data.h"
  16. #include "obs-module.h"
  17. struct obs_subsystem *obs = NULL;
  18. extern char *find_libobs_data_file(const char *file);
  19. static inline void make_gs_init_data(struct gs_init_data *gid,
  20. struct obs_video_info *ovi)
  21. {
  22. memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
  23. gid->cx = ovi->window_width;
  24. gid->cy = ovi->window_height;
  25. gid->num_backbuffers = 2;
  26. gid->format = GS_RGBA;
  27. gid->zsformat = GS_ZS_NONE;
  28. gid->adapter = ovi->adapter;
  29. }
  30. static inline void make_video_info(struct video_info *vi,
  31. struct obs_video_info *ovi)
  32. {
  33. vi->name = "video";
  34. vi->type = ovi->output_format;
  35. vi->fps_num = ovi->fps_num;
  36. vi->fps_den = ovi->fps_den;
  37. vi->width = ovi->output_width;
  38. vi->height = ovi->output_height;
  39. }
  40. static bool obs_init_textures(struct obs_video_info *ovi)
  41. {
  42. struct obs_video *video = &obs->video;
  43. bool success = true;
  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->output_width = ovi->output_width;
  72. video->output_height = ovi->output_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_mutex_init_value(&obs->data.displays_mutex);
  180. if (pthread_mutex_init(&data->sources_mutex, NULL) != 0)
  181. return false;
  182. if (pthread_mutex_init(&data->displays_mutex, NULL) != 0)
  183. return false;
  184. return true;
  185. }
  186. static void obs_free_data(void)
  187. {
  188. struct obs_data *data = &obs->data;
  189. uint32_t i;
  190. for (i = 0; i < MAX_CHANNELS; i++)
  191. obs_set_output_source(i, NULL);
  192. while (data->displays.num)
  193. obs_display_destroy(data->displays.array[0]);
  194. pthread_mutex_lock(&obs->data.sources_mutex);
  195. for (i = 0; i < data->sources.num; i++)
  196. obs_source_release(data->sources.array[i]);
  197. da_free(data->sources);
  198. pthread_mutex_unlock(&obs->data.sources_mutex);
  199. }
  200. static bool obs_init(void)
  201. {
  202. obs = bmalloc(sizeof(struct obs_subsystem));
  203. memset(obs, 0, sizeof(struct obs_subsystem));
  204. obs_init_data();
  205. obs->media = media_open();
  206. if (!obs->media)
  207. return false;
  208. return true;
  209. }
  210. bool obs_startup()
  211. {
  212. bool success;
  213. if (obs) {
  214. blog(LOG_ERROR, "Tried to call obs_startup more than once");
  215. return false;
  216. }
  217. success = obs_init();
  218. if (!success)
  219. obs_shutdown();
  220. return success;
  221. }
  222. void obs_shutdown(void)
  223. {
  224. size_t i;
  225. if (!obs)
  226. return;
  227. da_free(obs->input_types);
  228. da_free(obs->filter_types);
  229. da_free(obs->transition_types);
  230. da_free(obs->output_types);
  231. da_free(obs->service_types);
  232. obs_free_data();
  233. obs_free_video();
  234. obs_free_graphics();
  235. obs_free_audio();
  236. media_close(obs->media);
  237. for (i = 0; i < obs->modules.num; i++)
  238. free_module(obs->modules.array+i);
  239. da_free(obs->modules);
  240. bfree(obs);
  241. obs = NULL;
  242. }
  243. bool obs_reset_video(struct obs_video_info *ovi)
  244. {
  245. struct obs_video *video = &obs->video;
  246. obs_free_video();
  247. if (!ovi) {
  248. obs_free_graphics();
  249. return true;
  250. }
  251. if (!video->graphics && !obs_init_graphics(ovi))
  252. return false;
  253. return obs_init_video(ovi);
  254. }
  255. bool obs_reset_audio(struct audio_info *ai)
  256. {
  257. /*obs_free_audio();
  258. if(!ai)
  259. return true;
  260. return obs_init_audio(ai);*/
  261. /* TODO */
  262. return true;
  263. }
  264. bool obs_enum_input_types(size_t idx, const char **name)
  265. {
  266. if (idx >= obs->input_types.num)
  267. return false;
  268. *name = obs->input_types.array[idx].name;
  269. return true;
  270. }
  271. bool obs_enum_filter_types(size_t idx, const char **name)
  272. {
  273. if (idx >= obs->filter_types.num)
  274. return false;
  275. *name = obs->filter_types.array[idx].name;
  276. return true;
  277. }
  278. bool obs_enum_transition_types(size_t idx, const char **name)
  279. {
  280. if (idx >= obs->transition_types.num)
  281. return false;
  282. *name = obs->transition_types.array[idx].name;
  283. return true;
  284. }
  285. bool obs_enum_output_types(size_t idx, const char **name)
  286. {
  287. if (idx >= obs->output_types.num)
  288. return false;
  289. *name = obs->output_types.array[idx].name;
  290. return true;
  291. }
  292. graphics_t obs_graphics(void)
  293. {
  294. return obs->video.graphics;
  295. }
  296. media_t obs_media(void)
  297. {
  298. return obs->media;
  299. }
  300. bool obs_add_source(obs_source_t source)
  301. {
  302. pthread_mutex_lock(&obs->data.sources_mutex);
  303. da_push_back(obs->data.sources, &source);
  304. obs_source_addref(source);
  305. pthread_mutex_unlock(&obs->data.sources_mutex);
  306. return true;
  307. }
  308. obs_source_t obs_get_output_source(uint32_t channel)
  309. {
  310. struct obs_source *source;
  311. assert(channel < MAX_CHANNELS);
  312. source = obs->data.channels[channel];
  313. obs_source_addref(source);
  314. return source;
  315. }
  316. void obs_set_output_source(uint32_t channel, obs_source_t source)
  317. {
  318. struct obs_source *prev_source;
  319. assert(channel < MAX_CHANNELS);
  320. prev_source = obs->data.channels[channel];
  321. obs->data.channels[channel] = source;
  322. if (source)
  323. obs_source_addref(source);
  324. if (prev_source)
  325. obs_source_release(prev_source);
  326. }
  327. void obs_enum_sources(ENUM_SOURCES_PROC enum_proc, void *param)
  328. {
  329. struct obs_data *data = &obs->data;
  330. size_t i;
  331. pthread_mutex_lock(&data->sources_mutex);
  332. for (i = 0; i < data->sources.num; i++)
  333. enum_proc(data->sources.array[i], param);
  334. pthread_mutex_unlock(&data->sources_mutex);
  335. }