obs-source.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 "util/platform.h"
  15. #include "obs.h"
  16. #include "obs-data.h"
  17. bool get_source_info(void *module, const char *module_name,
  18. const char *source_name, struct source_info *info)
  19. {
  20. info->create = load_module_subfunc(module, module_name,
  21. source_name,"create", true);
  22. info->destroy = load_module_subfunc(module, module_name,
  23. source_name, "destroy", true);
  24. info->get_output_flags = load_module_subfunc(module, module_name,
  25. source_name, "get_output_flags", true);
  26. if (!info->create || !info->destroy || !info->get_output_flags)
  27. return false;
  28. info->config = load_module_subfunc(module, module_name,
  29. source_name, "config", false);
  30. info->activate = load_module_subfunc(module, module_name,
  31. source_name, "activate", false);
  32. info->deactivate = load_module_subfunc(module, module_name,
  33. source_name, "deactivate", false);
  34. info->video_tick = load_module_subfunc(module, module_name,
  35. source_name, "video_tick", false);
  36. info->video_render = load_module_subfunc(module, module_name,
  37. source_name, "video_render", false);
  38. info->getwidth = load_module_subfunc(module, module_name,
  39. source_name, "getwidth", false);
  40. info->getheight = load_module_subfunc(module, module_name,
  41. source_name, "getheight", false);
  42. info->getparam = load_module_subfunc(module, module_name,
  43. source_name, "getparam", false);
  44. info->setparam = load_module_subfunc(module, module_name,
  45. source_name, "setparam", false);
  46. info->enum_children = load_module_subfunc(module, module_name,
  47. source_name, "enum_children", false);
  48. info->filter_video = load_module_subfunc(module, module_name,
  49. source_name, "filter_video", false);
  50. info->filter_audio = load_module_subfunc(module, module_name,
  51. source_name, "filter_audio", false);
  52. info->name = source_name;
  53. return true;
  54. }
  55. static inline const struct source_info *find_source(struct darray *list,
  56. const char *name)
  57. {
  58. size_t i;
  59. struct source_info *array = list->array;
  60. for (i = 0; i < list->num; i++) {
  61. struct source_info *info = array+i;
  62. if (strcmp(info->name, name) == 0)
  63. return info;
  64. }
  65. return NULL;
  66. }
  67. bool obs_source_init(struct obs_source *source, const char *settings,
  68. const struct source_info *info)
  69. {
  70. uint32_t flags = info->get_output_flags(source->data);
  71. pthread_mutex_init_value(&source->filter_mutex);
  72. pthread_mutex_init_value(&source->video_mutex);
  73. pthread_mutex_init_value(&source->audio_mutex);
  74. dstr_copy(&source->settings, settings);
  75. memcpy(&source->callbacks, info, sizeof(struct source_info));
  76. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  77. return false;
  78. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  79. return false;
  80. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  81. return false;
  82. if (flags & SOURCE_AUDIO) {
  83. source->audio_line = audio_output_createline(obs->audio);
  84. if (!source->audio_line) {
  85. blog(LOG_ERROR, "Failed to create audio line for "
  86. "source");
  87. return false;
  88. }
  89. }
  90. source->valid = true;
  91. pthread_mutex_lock(&obs->source_list_mutex);
  92. da_push_back(obs->sources, &source);
  93. pthread_mutex_unlock(&obs->source_list_mutex);
  94. return true;
  95. }
  96. obs_source_t obs_source_create(enum obs_source_type type, const char *name,
  97. const char *settings)
  98. {
  99. const struct source_info *info = NULL;
  100. struct darray *list = NULL;
  101. struct obs_source *source;
  102. switch (type) {
  103. case SOURCE_INPUT: list = &obs->input_types.da; break;
  104. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  105. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  106. case SOURCE_SCENE:
  107. default:
  108. return NULL;
  109. }
  110. info = find_source(list, name);
  111. if (!info) {
  112. blog(LOG_WARNING, "Source '%s' not found", type);
  113. return NULL;
  114. }
  115. source = bmalloc(sizeof(struct obs_source));
  116. memset(source, 0, sizeof(struct obs_source));
  117. source->data = info->create(settings, source);
  118. if (!source->data)
  119. goto fail;
  120. if (!obs_source_init(source, settings, info))
  121. goto fail;
  122. return source;
  123. fail:
  124. blog(LOG_ERROR, "obs_source_create failed");
  125. obs_source_destroy(source);
  126. return NULL;
  127. }
  128. void obs_source_destroy(obs_source_t source)
  129. {
  130. if (source) {
  131. size_t i;
  132. if (source->filter_parent)
  133. obs_source_filter_remove(source->filter_parent, source);
  134. for (i = 0; i < source->filters.num; i++)
  135. obs_source_destroy(source->filters.array[i]);
  136. if (source->valid) {
  137. pthread_mutex_lock(&obs->source_list_mutex);
  138. da_erase_item(obs->sources, &source);
  139. pthread_mutex_unlock(&obs->source_list_mutex);
  140. }
  141. for (i = 0; i < source->audio_buffer.num; i++)
  142. audiobuf_free(source->audio_buffer.array+i);
  143. for (i = 0; i < source->video_frames.num; i++)
  144. source_frame_destroy(source->video_frames.array[i]);
  145. gs_entercontext(obs->graphics);
  146. texture_destroy(source->output_texture);
  147. gs_leavecontext();
  148. if (source->data)
  149. source->callbacks.destroy(source->data);
  150. audio_line_destroy(source->audio_line);
  151. da_free(source->video_frames);
  152. da_free(source->audio_buffer);
  153. da_free(source->filters);
  154. pthread_mutex_destroy(&source->filter_mutex);
  155. pthread_mutex_destroy(&source->audio_mutex);
  156. pthread_mutex_destroy(&source->video_mutex);
  157. dstr_free(&source->settings);
  158. bfree(source);
  159. }
  160. }
  161. uint32_t obs_source_get_output_flags(obs_source_t source)
  162. {
  163. return source->callbacks.get_output_flags(source->data);
  164. }
  165. bool obs_source_hasconfig(obs_source_t source)
  166. {
  167. return source->callbacks.config != NULL;
  168. }
  169. void obs_source_config(obs_source_t source, void *parent)
  170. {
  171. if (source->callbacks.config)
  172. source->callbacks.config(source->data, parent);
  173. }
  174. void obs_source_activate(obs_source_t source)
  175. {
  176. if (source->callbacks.activate)
  177. source->callbacks.activate(source->data);
  178. }
  179. void obs_source_deactivate(obs_source_t source)
  180. {
  181. if (source->callbacks.deactivate)
  182. source->callbacks.deactivate(source->data);
  183. }
  184. void obs_source_video_tick(obs_source_t source, float seconds)
  185. {
  186. if (source->callbacks.video_tick)
  187. source->callbacks.video_tick(source->data, seconds);
  188. }
  189. #define MAX_VARIANCE 2000000000ULL
  190. static void source_output_audio_line(obs_source_t source,
  191. const struct audio_data *data)
  192. {
  193. struct audio_data in = *data;
  194. if (!in.timestamp) {
  195. in.timestamp = os_gettime_ns();
  196. if (!source->timing_set) {
  197. source->timing_set = true;
  198. source->timing_adjust = 0;
  199. }
  200. }
  201. if (!source->timing_set) {
  202. source->timing_set = true;
  203. source->timing_adjust = in.timestamp - os_gettime_ns();
  204. /* detects 'directly' set timestamps as long as they're within
  205. * a certain threashold */
  206. if ((source->timing_adjust+MAX_VARIANCE) < MAX_VARIANCE*2)
  207. source->timing_adjust = 0;
  208. }
  209. in.timestamp += source->timing_adjust;
  210. audio_line_output(source->audio_line, &in);
  211. }
  212. static void obs_source_flush_audio_buffer(obs_source_t source)
  213. {
  214. size_t i;
  215. pthread_mutex_lock(&source->audio_mutex);
  216. source->timing_set = true;
  217. for (i = 0; i < source->audio_buffer.num; i++) {
  218. struct audiobuf *buf = source->audio_buffer.array+i;
  219. struct audio_data data;
  220. data.data = buf->data;
  221. data.frames = buf->frames;
  222. data.timestamp = buf->timestamp + source->timing_adjust;
  223. audio_line_output(source->audio_line, &data);
  224. audiobuf_free(buf);
  225. }
  226. da_free(source->audio_buffer);
  227. pthread_mutex_unlock(&source->audio_mutex);
  228. }
  229. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  230. {
  231. if (source->output_texture) {
  232. uint32_t width = texture_getwidth(source->output_texture);
  233. uint32_t height = texture_getheight(source->output_texture);
  234. if (width == frame->width && height == frame->height)
  235. return true;
  236. }
  237. texture_destroy(source->output_texture);
  238. source->output_texture = gs_create_texture(frame->width, frame->height,
  239. GS_RGBA, 1, NULL, GS_DYNAMIC);
  240. return source->output_texture != NULL;
  241. }
  242. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  243. {
  244. effect_t effect = obs->default_effect;
  245. const char *type = frame->yuv ? "DrawYUVToRGB" : "DrawRGB";
  246. technique_t tech;
  247. eparam_t param;
  248. texture_setimage(tex, frame->data, frame->row_bytes, frame->flip);
  249. tech = effect_gettechnique(effect, type);
  250. technique_begin(tech);
  251. technique_beginpass(tech, 0);
  252. if (frame->yuv) {
  253. param = effect_getparambyname(effect, "yuv_matrix");
  254. effect_setval(effect, param, frame->yuv_matrix,
  255. sizeof(float) * 16);
  256. }
  257. param = effect_getparambyname(effect, "diffuse");
  258. effect_settexture(effect, param, tex);
  259. gs_draw_sprite(tex);
  260. technique_endpass(tech);
  261. technique_end(tech);
  262. }
  263. static void obs_source_render_async_video(obs_source_t source)
  264. {
  265. struct source_frame *frame = obs_source_getframe(source);
  266. if (!frame)
  267. return;
  268. source->timing_adjust = frame->timestamp - os_gettime_ns();
  269. if (!source->timing_set && source->audio_buffer.num)
  270. obs_source_flush_audio_buffer(source);
  271. if (set_texture_size(source, frame))
  272. obs_source_draw_texture(source->output_texture, frame);
  273. obs_source_releaseframe(source, frame);
  274. }
  275. void obs_source_video_render(obs_source_t source)
  276. {
  277. if (source->callbacks.video_render) {
  278. if (source->filters.num && !source->rendering_filter) {
  279. source->rendering_filter = true;
  280. obs_source_video_render(source->filters.array[0]);
  281. source->rendering_filter = false;
  282. } else {
  283. source->callbacks.video_render(source->data);
  284. }
  285. } else if (source->filter_target) {
  286. obs_source_video_render(source->filter_target);
  287. } else {
  288. obs_source_render_async_video(source);
  289. }
  290. }
  291. uint32_t obs_source_getwidth(obs_source_t source)
  292. {
  293. if (source->callbacks.getwidth)
  294. return source->callbacks.getwidth(source->data);
  295. return 0;
  296. }
  297. uint32_t obs_source_getheight(obs_source_t source)
  298. {
  299. if (source->callbacks.getheight)
  300. return source->callbacks.getheight(source->data);
  301. return 0;
  302. }
  303. size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
  304. size_t buf_size)
  305. {
  306. if (source->callbacks.getparam)
  307. return source->callbacks.getparam(source->data, param, buf,
  308. buf_size);
  309. return 0;
  310. }
  311. void obs_source_setparam(obs_source_t source, const char *param,
  312. const void *data, size_t size)
  313. {
  314. if (source->callbacks.setparam)
  315. source->callbacks.setparam(source->data, param, data, size);
  316. }
  317. bool obs_source_enum_children(obs_source_t source, size_t idx,
  318. obs_source_t *child)
  319. {
  320. if (source->callbacks.enum_children)
  321. return source->callbacks.enum_children(source, idx, child);
  322. return false;
  323. }
  324. obs_source_t obs_filter_gettarget(obs_source_t filter)
  325. {
  326. return filter->filter_target;
  327. }
  328. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  329. {
  330. pthread_mutex_lock(&source->filter_mutex);
  331. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  332. blog(LOG_WARNING, "Tried to add a filter that was already "
  333. "present on the source");
  334. return;
  335. }
  336. if (source->filters.num) {
  337. obs_source_t *back = da_end(source->filters);
  338. (*back)->filter_target = filter;
  339. }
  340. da_push_back(source->filters, &filter);
  341. pthread_mutex_unlock(&source->filter_mutex);
  342. filter->filter_parent = source;
  343. filter->filter_target = source;
  344. }
  345. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  346. {
  347. size_t idx;
  348. pthread_mutex_lock(&source->filter_mutex);
  349. idx = da_find(source->filters, &filter, 0);
  350. if (idx == DARRAY_INVALID)
  351. return;
  352. if (idx > 0) {
  353. obs_source_t prev = source->filters.array[idx-1];
  354. prev->filter_target = filter->filter_target;
  355. }
  356. da_erase(source->filters, idx);
  357. pthread_mutex_unlock(&source->filter_mutex);
  358. filter->filter_parent = NULL;
  359. filter->filter_target = NULL;
  360. }
  361. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  362. enum order_movement movement)
  363. {
  364. size_t idx = da_find(source->filters, &filter, 0);
  365. size_t i;
  366. if (idx == DARRAY_INVALID)
  367. return;
  368. if (movement == ORDER_MOVE_UP) {
  369. if (idx == source->filters.num-1)
  370. return;
  371. da_move_item(source->filters, idx, idx+1);
  372. } else if (movement == ORDER_MOVE_DOWN) {
  373. if (idx == 0)
  374. return;
  375. da_move_item(source->filters, idx, idx-1);
  376. } else if (movement == ORDER_MOVE_TOP) {
  377. if (idx == source->filters.num-1)
  378. return;
  379. da_move_item(source->filters, idx, source->filters.num-1);
  380. } else if (movement == ORDER_MOVE_BOTTOM) {
  381. if (idx == 0)
  382. return;
  383. da_move_item(source->filters, idx, 0);
  384. }
  385. /* reorder filter targets */
  386. for (i = 0; i < source->filters.num; i++) {
  387. obs_source_t next_filter = (i == source->filters.num-1) ?
  388. source : source->filters.array[idx+1];
  389. source->filters.array[i]->filter_target = next_filter;
  390. }
  391. }
  392. const char *obs_source_get_settings(obs_source_t source)
  393. {
  394. return source->settings.array;
  395. }
  396. void obs_source_save_settings(obs_source_t source, const char *settings)
  397. {
  398. dstr_copy(&source->settings, settings);
  399. }
  400. static inline struct filter_frame *filter_async_video(obs_source_t source,
  401. struct filter_frame *in)
  402. {
  403. size_t i;
  404. for (i = source->filters.num; i > 0; i--) {
  405. struct obs_source *filter = source->filters.array[i-1];
  406. if (filter->callbacks.filter_video) {
  407. in = filter->callbacks.filter_video(filter->data, in);
  408. if (!in)
  409. return NULL;
  410. }
  411. }
  412. return in;
  413. }
  414. static struct filter_frame *process_video(obs_source_t source,
  415. const struct source_video *frame)
  416. {
  417. /* TODO: convert to UYV444 or RGB */
  418. return NULL;
  419. }
  420. void obs_source_output_video(obs_source_t source,
  421. const struct source_video *frame)
  422. {
  423. struct filter_frame *output;
  424. output = process_video(source, frame);
  425. pthread_mutex_lock(&source->filter_mutex);
  426. output = filter_async_video(source, output);
  427. pthread_mutex_unlock(&source->filter_mutex);
  428. pthread_mutex_lock(&source->video_mutex);
  429. da_push_back(source->video_frames, &output);
  430. pthread_mutex_unlock(&source->video_mutex);
  431. }
  432. static inline const struct audio_data *filter_async_audio(obs_source_t source,
  433. const struct audio_data *in)
  434. {
  435. size_t i;
  436. for (i = source->filters.num; i > 0; i--) {
  437. struct obs_source *filter = source->filters.array[i-1];
  438. if (filter->callbacks.filter_audio) {
  439. in = filter->callbacks.filter_audio(filter->data, in);
  440. if (!in)
  441. return NULL;
  442. }
  443. }
  444. return in;
  445. }
  446. static struct audio_data *process_audio(obs_source_t source,
  447. const struct source_audio *audio)
  448. {
  449. /* TODO: upmix/downmix/resample */
  450. return NULL;
  451. }
  452. void obs_source_output_audio(obs_source_t source,
  453. const struct source_audio *audio)
  454. {
  455. uint32_t flags = obs_source_get_output_flags(source);
  456. struct audio_data *data;
  457. const struct audio_data *output;
  458. data = process_audio(source, audio);
  459. pthread_mutex_lock(&source->filter_mutex);
  460. output = filter_async_audio(source, data);
  461. if (output) {
  462. pthread_mutex_lock(&source->audio_mutex);
  463. if (!source->timing_set && flags & SOURCE_ASYNC_VIDEO) {
  464. struct audiobuf newbuf;
  465. size_t audio_size = audio_output_blocksize(obs->audio) *
  466. output->frames;
  467. newbuf.data = bmalloc(audio_size);
  468. newbuf.frames = output->frames;
  469. newbuf.timestamp = output->timestamp;
  470. memcpy(newbuf.data, output->data, audio_size);
  471. da_push_back(source->audio_buffer, &newbuf);
  472. } else {
  473. source_output_audio_line(source, output);
  474. }
  475. pthread_mutex_unlock(&source->audio_mutex);
  476. }
  477. pthread_mutex_unlock(&source->filter_mutex);
  478. }
  479. struct source_frame *obs_source_getframe(obs_source_t source)
  480. {
  481. /* TODO */
  482. return NULL;
  483. }
  484. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  485. {
  486. /* TODO */
  487. }