obs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 3 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->base_width;
  24. gid->cy = ovi->base_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_graphics(struct obs_video_info *ovi)
  41. {
  42. struct obs_video *video = &obs->video;
  43. struct gs_init_data graphics_data;
  44. bool success = true;
  45. int errorcode;
  46. size_t i;
  47. make_gs_init_data(&graphics_data, ovi);
  48. video->output_width = ovi->output_width;
  49. video->output_height = ovi->output_height;
  50. errorcode = gs_create(&video->graphics, ovi->graphics_module,
  51. &graphics_data);
  52. if (errorcode != GS_SUCCESS) {
  53. if (errorcode == GS_ERROR_MODULENOTFOUND)
  54. blog(LOG_ERROR, "Could not find graphics module '%s'",
  55. ovi->graphics_module);
  56. return false;
  57. }
  58. gs_entercontext(video->graphics);
  59. for (i = 0; i < NUM_TEXTURES; i++) {
  60. video->copy_surfaces[i] = gs_create_stagesurface(
  61. ovi->output_width, ovi->output_height,
  62. graphics_data.format);
  63. if (!video->copy_surfaces[i])
  64. success = false;
  65. }
  66. if (success) {
  67. char *filename = find_libobs_data_file("default.effect");
  68. video->default_effect = gs_create_effect_from_file(filename,
  69. NULL);
  70. bfree(filename);
  71. if (!video->default_effect)
  72. success = false;
  73. }
  74. gs_leavecontext();
  75. return success;
  76. }
  77. static bool obs_init_video(struct obs_video_info *ovi)
  78. {
  79. struct obs_video *video = &obs->video;
  80. struct video_info vi;
  81. int errorcode;
  82. memset(video, 0, sizeof(struct obs_video));
  83. if (!obs_init_graphics(ovi))
  84. return false;
  85. make_video_info(&vi, ovi);
  86. errorcode = video_output_open(&video->video, obs->media, &vi);
  87. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  88. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM)
  89. blog(LOG_ERROR, "Invalid video parameters specified");
  90. else
  91. blog(LOG_ERROR, "Could not open video output");
  92. return false;
  93. }
  94. errorcode = pthread_create(&video->video_thread, NULL,
  95. obs_video_thread, obs);
  96. if (errorcode != 0)
  97. return false;
  98. video->thread_initialized = true;
  99. return true;
  100. }
  101. static void obs_free_video(void)
  102. {
  103. struct obs_video *video = &obs->video;
  104. size_t i;
  105. if (video->video) {
  106. void *thread_retval;
  107. video_output_stop(video->video);
  108. if (video->thread_initialized)
  109. pthread_join(video->video_thread, &thread_retval);
  110. video_output_close(video->video);
  111. }
  112. if (video->graphics) {
  113. int cur_texture = video->cur_texture;
  114. gs_entercontext(video->graphics);
  115. if (video->copy_mapped)
  116. stagesurface_unmap(video->copy_surfaces[cur_texture]);
  117. for (i = 0; i < NUM_TEXTURES; i++)
  118. stagesurface_destroy(video->copy_surfaces[i]);
  119. effect_destroy(video->default_effect);
  120. gs_leavecontext();
  121. gs_destroy(video->graphics);
  122. }
  123. memset(video, 0, sizeof(struct obs_video));
  124. }
  125. static bool obs_init_audio(struct audio_info *ai)
  126. {
  127. struct obs_audio *audio = &obs->audio;
  128. int errorcode;
  129. /* TODO: sound subsystem */
  130. errorcode = audio_output_open(&audio->audio, obs->media, ai);
  131. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  132. return true;
  133. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  134. blog(LOG_ERROR, "Invalid audio parameters specified");
  135. else
  136. blog(LOG_ERROR, "Could not open audio output");
  137. return false;
  138. }
  139. static void obs_free_audio(void)
  140. {
  141. struct obs_audio *audio = &obs->audio;
  142. if (audio->audio)
  143. audio_output_close(audio->audio);
  144. memset(audio, 0, sizeof(struct obs_audio));
  145. }
  146. static bool obs_init_data(void)
  147. {
  148. struct obs_data *data = &obs->data;
  149. pthread_mutex_init_value(&obs->data.displays_mutex);
  150. if (pthread_mutex_init(&data->sources_mutex, NULL) != 0)
  151. return false;
  152. if (pthread_mutex_init(&data->displays_mutex, NULL) != 0)
  153. return false;
  154. return true;
  155. }
  156. static void obs_free_data(void)
  157. {
  158. struct obs_data *data = &obs->data;
  159. uint32_t i;
  160. for (i = 0; i < MAX_CHANNELS; i++)
  161. obs_set_output_source(i, NULL);
  162. while (data->displays.num)
  163. obs_display_destroy(data->displays.array[0]);
  164. pthread_mutex_lock(&obs->data.sources_mutex);
  165. for (i = 0; i < data->sources.num; i++)
  166. obs_source_release(data->sources.array[i]);
  167. da_free(data->sources);
  168. pthread_mutex_unlock(&obs->data.sources_mutex);
  169. }
  170. static bool obs_init(void)
  171. {
  172. obs = bmalloc(sizeof(struct obs_subsystem));
  173. memset(obs, 0, sizeof(struct obs_subsystem));
  174. obs_init_data();
  175. obs->media = media_open();
  176. if (!obs->media)
  177. return false;
  178. return true;
  179. }
  180. bool obs_startup()
  181. {
  182. bool success;
  183. if (obs) {
  184. blog(LOG_ERROR, "Tried to call obs_startup more than once");
  185. return false;
  186. }
  187. success = obs_init();
  188. if (!success)
  189. obs_shutdown();
  190. return success;
  191. }
  192. void obs_shutdown(void)
  193. {
  194. size_t i;
  195. if (!obs)
  196. return;
  197. da_free(obs->input_types);
  198. da_free(obs->filter_types);
  199. da_free(obs->transition_types);
  200. da_free(obs->output_types);
  201. da_free(obs->service_types);
  202. obs_free_data();
  203. obs_free_video();
  204. obs_free_audio();
  205. media_close(obs->media);
  206. for (i = 0; i < obs->modules.num; i++)
  207. free_module(obs->modules.array+i);
  208. da_free(obs->modules);
  209. bfree(obs);
  210. obs = NULL;
  211. }
  212. bool obs_reset_video(struct obs_video_info *ovi)
  213. {
  214. obs_free_video();
  215. if (ovi)
  216. return obs_init_video(ovi);
  217. return true;
  218. }
  219. bool obs_reset_audio(struct audio_info *ai)
  220. {
  221. obs_free_audio();
  222. if(ai)
  223. return obs_init_audio(ai);
  224. return true;
  225. }
  226. bool obs_enum_inputs(size_t idx, const char **name)
  227. {
  228. if (idx >= obs->input_types.num)
  229. return false;
  230. *name = obs->input_types.array[idx].name;
  231. return true;
  232. }
  233. bool obs_enum_filters(size_t idx, const char **name)
  234. {
  235. if (idx >= obs->filter_types.num)
  236. return false;
  237. *name = obs->filter_types.array[idx].name;
  238. return true;
  239. }
  240. bool obs_enum_transitions(size_t idx, const char **name)
  241. {
  242. if (idx >= obs->transition_types.num)
  243. return false;
  244. *name = obs->transition_types.array[idx].name;
  245. return true;
  246. }
  247. bool obs_enum_outputs(size_t idx, const char **name)
  248. {
  249. if (idx >= obs->output_types.num)
  250. return false;
  251. *name = obs->output_types.array[idx].name;
  252. return true;
  253. }
  254. graphics_t obs_graphics(void)
  255. {
  256. return obs->video.graphics;
  257. }
  258. media_t obs_media(void)
  259. {
  260. return obs->media;
  261. }
  262. bool obs_add_source(obs_source_t source)
  263. {
  264. pthread_mutex_lock(&obs->data.sources_mutex);
  265. da_push_back(obs->data.sources, &source);
  266. obs_source_addref(source);
  267. pthread_mutex_unlock(&obs->data.sources_mutex);
  268. return true;
  269. }
  270. obs_source_t obs_get_output_source(uint32_t channel)
  271. {
  272. assert(channel < MAX_CHANNELS);
  273. return obs->data.channels[channel];
  274. }
  275. void obs_set_output_source(uint32_t channel, obs_source_t source)
  276. {
  277. struct obs_source *prev_source;
  278. assert(channel < MAX_CHANNELS);
  279. prev_source = obs->data.channels[channel];
  280. obs->data.channels[channel] = source;
  281. if (source)
  282. obs_source_addref(source);
  283. if (prev_source)
  284. obs_source_release(prev_source);
  285. }