obs.c 11 KB

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