obs-source.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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 <inttypes.h>
  15. #include "media-io/format-conversion.h"
  16. #include "media-io/video-frame.h"
  17. #include "util/platform.h"
  18. #include "callback/calldata.h"
  19. #include "graphics/matrix3.h"
  20. #include "graphics/vec3.h"
  21. #include "obs.h"
  22. #include "obs-internal.h"
  23. static void obs_source_destroy(obs_source_t source);
  24. static inline const struct obs_source_info *find_source(struct darray *list,
  25. const char *id)
  26. {
  27. size_t i;
  28. struct obs_source_info *array = list->array;
  29. for (i = 0; i < list->num; i++) {
  30. struct obs_source_info *info = array+i;
  31. if (strcmp(info->id, id) == 0)
  32. return info;
  33. }
  34. return NULL;
  35. }
  36. static const struct obs_source_info *get_source_info(enum obs_source_type type,
  37. const char *id)
  38. {
  39. struct darray *list = NULL;
  40. switch (type) {
  41. case OBS_SOURCE_TYPE_INPUT:
  42. list = &obs->input_types.da;
  43. break;
  44. case OBS_SOURCE_TYPE_FILTER:
  45. list = &obs->filter_types.da;
  46. break;
  47. case OBS_SOURCE_TYPE_TRANSITION:
  48. list = &obs->transition_types.da;
  49. break;
  50. case OBS_SOURCE_TYPE_SCENE:
  51. default:
  52. blog(LOG_WARNING, "get_source_info: invalid source type");
  53. return NULL;
  54. }
  55. return find_source(list, id);
  56. }
  57. bool obs_source_init_handlers(struct obs_source *source)
  58. {
  59. source->signals = signal_handler_create();
  60. if (!source->signals)
  61. return false;
  62. source->procs = proc_handler_create();
  63. return (source->procs != NULL);
  64. }
  65. const char *obs_source_getdisplayname(enum obs_source_type type,
  66. const char *id, const char *locale)
  67. {
  68. const struct obs_source_info *info = get_source_info(type, id);
  69. return (info != NULL) ? info->getname(locale) : NULL;
  70. }
  71. /* internal initialization */
  72. bool obs_source_init(struct obs_source *source,
  73. const struct obs_source_info *info)
  74. {
  75. source->refs = 1;
  76. source->user_volume = 1.0f;
  77. source->present_volume = 1.0f;
  78. source->sync_offset = 0;
  79. pthread_mutex_init_value(&source->filter_mutex);
  80. pthread_mutex_init_value(&source->video_mutex);
  81. pthread_mutex_init_value(&source->audio_mutex);
  82. memcpy(&source->info, info, sizeof(struct obs_source_info));
  83. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  84. return false;
  85. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  86. return false;
  87. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  88. return false;
  89. if (info->output_flags & OBS_SOURCE_AUDIO) {
  90. source->audio_line = audio_output_createline(obs->audio.audio,
  91. source->name);
  92. if (!source->audio_line) {
  93. blog(LOG_ERROR, "Failed to create audio line for "
  94. "source '%s'", source->name);
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. static inline void obs_source_dosignal(struct obs_source *source,
  101. const char *signal)
  102. {
  103. struct calldata data;
  104. calldata_init(&data);
  105. calldata_setptr(&data, "source", source);
  106. signal_handler_signal(obs->signals, signal, &data);
  107. calldata_free(&data);
  108. }
  109. obs_source_t obs_source_create(enum obs_source_type type, const char *id,
  110. const char *name, obs_data_t settings)
  111. {
  112. struct obs_source *source;
  113. const struct obs_source_info *info = get_source_info(type, id);
  114. if (!info) {
  115. blog(LOG_WARNING, "Source '%s' not found", id);
  116. return NULL;
  117. }
  118. source = bzalloc(sizeof(struct obs_source));
  119. if (!obs_source_init_handlers(source))
  120. goto fail;
  121. source->name = bstrdup(name);
  122. source->type = type;
  123. source->settings = obs_data_newref(settings);
  124. source->data = info->create(source->settings, source);
  125. if (!source->data)
  126. goto fail;
  127. if (!obs_source_init(source, info))
  128. goto fail;
  129. obs_source_dosignal(source, "source-create");
  130. return source;
  131. fail:
  132. blog(LOG_ERROR, "obs_source_create failed");
  133. obs_source_destroy(source);
  134. return NULL;
  135. }
  136. void source_frame_init(struct source_frame *frame, enum video_format format,
  137. uint32_t width, uint32_t height)
  138. {
  139. struct video_frame vid_frame;
  140. video_frame_init(&vid_frame, format, width, height);
  141. frame->format = format;
  142. frame->width = width;
  143. frame->height = height;
  144. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  145. frame->data[i] = vid_frame.data[i];
  146. frame->linesize[i] = vid_frame.linesize[i];
  147. }
  148. }
  149. static void obs_source_destroy(obs_source_t source)
  150. {
  151. size_t i;
  152. obs_source_dosignal(source, "source-destroy");
  153. if (source->filter_parent)
  154. obs_source_filter_remove(source->filter_parent, source);
  155. for (i = 0; i < source->filters.num; i++)
  156. obs_source_release(source->filters.array[i]);
  157. for (i = 0; i < source->video_frames.num; i++)
  158. source_frame_destroy(source->video_frames.array[i]);
  159. gs_entercontext(obs->video.graphics);
  160. texture_destroy(source->output_texture);
  161. gs_leavecontext();
  162. if (source->data)
  163. source->info.destroy(source->data);
  164. for (i = 0; i < MAX_AV_PLANES; i++)
  165. bfree(source->audio_data.data[i]);
  166. audio_line_destroy(source->audio_line);
  167. audio_resampler_destroy(source->resampler);
  168. proc_handler_destroy(source->procs);
  169. signal_handler_destroy(source->signals);
  170. texrender_destroy(source->filter_texrender);
  171. da_free(source->video_frames);
  172. da_free(source->filters);
  173. pthread_mutex_destroy(&source->filter_mutex);
  174. pthread_mutex_destroy(&source->audio_mutex);
  175. pthread_mutex_destroy(&source->video_mutex);
  176. obs_data_release(source->settings);
  177. bfree(source->name);
  178. bfree(source);
  179. }
  180. void obs_source_addref(obs_source_t source)
  181. {
  182. if (source)
  183. ++source->refs;
  184. }
  185. void obs_source_release(obs_source_t source)
  186. {
  187. if (!source)
  188. return;
  189. if (--source->refs == 0)
  190. obs_source_destroy(source);
  191. }
  192. void obs_source_remove(obs_source_t source)
  193. {
  194. struct obs_core_data *data = &obs->data;
  195. struct calldata cd = {0};
  196. size_t id;
  197. pthread_mutex_lock(&data->sources_mutex);
  198. if (!source || source->removed)
  199. return;
  200. source->removed = true;
  201. obs_source_addref(source);
  202. id = da_find(data->sources, &source, 0);
  203. if (id != DARRAY_INVALID) {
  204. da_erase_item(data->sources, &source);
  205. obs_source_release(source);
  206. }
  207. pthread_mutex_unlock(&data->sources_mutex);
  208. calldata_setptr(&cd, "source", source);
  209. signal_handler_signal(obs->signals, "source-remove", &cd);
  210. signal_handler_signal(source->signals, "remove", &cd);
  211. calldata_free(&cd);
  212. obs_source_release(source);
  213. }
  214. bool obs_source_removed(obs_source_t source)
  215. {
  216. return source->removed;
  217. }
  218. obs_properties_t obs_source_properties(enum obs_source_type type,
  219. const char *id, const char *locale)
  220. {
  221. const struct obs_source_info *info = get_source_info(type, id);
  222. if (info && info->get_properties)
  223. return info->get_properties(locale);
  224. return NULL;
  225. }
  226. uint32_t obs_source_get_output_flags(obs_source_t source)
  227. {
  228. return source->info.output_flags;
  229. }
  230. void obs_source_update(obs_source_t source, obs_data_t settings)
  231. {
  232. obs_data_replace(&source->settings, settings);
  233. if (source->info.update)
  234. source->info.update(source->data, source->settings);
  235. }
  236. static void activate_source(obs_source_t source)
  237. {
  238. struct calldata data = {0};
  239. if (source->info.activate)
  240. source->info.activate(source->data);
  241. calldata_setptr(&data, "source", source);
  242. signal_handler_signal(obs->signals, "source-activate", &data);
  243. signal_handler_signal(source->signals, "activate", &data);
  244. calldata_free(&data);
  245. }
  246. static void deactivate_source(obs_source_t source)
  247. {
  248. struct calldata data = {0};
  249. if (source->info.deactivate)
  250. source->info.deactivate(source->data);
  251. calldata_setptr(&data, "source", source);
  252. signal_handler_signal(obs->signals, "source-deactivate", &data);
  253. signal_handler_signal(source->signals, "deactivate", &data);
  254. calldata_free(&data);
  255. }
  256. static void activate_tree(obs_source_t parent, obs_source_t child, void *param)
  257. {
  258. if (++child->activate_refs == 1)
  259. activate_source(child);
  260. }
  261. static void deactivate_tree(obs_source_t parent, obs_source_t child,
  262. void *param)
  263. {
  264. if (--child->activate_refs == 0)
  265. deactivate_source(child);
  266. }
  267. void obs_source_activate(obs_source_t source)
  268. {
  269. if (!source) return;
  270. if (++source->activate_refs == 1) {
  271. activate_source(source);
  272. obs_source_enum_tree(source, activate_tree, NULL);
  273. }
  274. }
  275. void obs_source_deactivate(obs_source_t source)
  276. {
  277. if (!source) return;
  278. if (--source->activate_refs == 0) {
  279. deactivate_source(source);
  280. obs_source_enum_tree(source, deactivate_tree, NULL);
  281. }
  282. }
  283. void obs_source_video_tick(obs_source_t source, float seconds)
  284. {
  285. /* reset the filter render texture information once every frame */
  286. if (source->filter_texrender)
  287. texrender_reset(source->filter_texrender);
  288. if (source->info.video_tick)
  289. source->info.video_tick(source->data, seconds);
  290. }
  291. /* unless the value is 3+ hours worth of frames, this won't overflow */
  292. static inline uint64_t conv_frames_to_time(size_t frames)
  293. {
  294. const struct audio_output_info *info;
  295. info = audio_output_getinfo(obs->audio.audio);
  296. return (uint64_t)frames * 1000000000ULL /
  297. (uint64_t)info->samples_per_sec;
  298. }
  299. /* maximum "direct" timestamp variance in nanoseconds */
  300. #define MAX_TS_VAR 5000000000ULL
  301. /* maximum time that timestamp can jump in nanoseconds */
  302. #define MAX_TIMESTAMP_JUMP 2000000000ULL
  303. /* time threshold in nanoseconds to ensure audio timing is as seamless as
  304. * possible */
  305. #define TS_SMOOTHING_THRESHOLD 70000000ULL
  306. static inline void reset_audio_timing(obs_source_t source, uint64_t timetamp)
  307. {
  308. source->timing_set = true;
  309. source->timing_adjust = os_gettime_ns() - timetamp;
  310. }
  311. static inline void handle_ts_jump(obs_source_t source, uint64_t expected,
  312. uint64_t ts, uint64_t diff)
  313. {
  314. blog(LOG_DEBUG, "Timestamp for source '%s' jumped by '%"PRIu64"', "
  315. "expected value %"PRIu64", input value %"PRIu64,
  316. source->name, diff, expected, ts);
  317. /* if has video, ignore audio data until reset */
  318. if (source->info.output_flags & OBS_SOURCE_ASYNC_VIDEO)
  319. source->audio_reset_ref--;
  320. else
  321. reset_audio_timing(source, ts);
  322. }
  323. static void source_output_audio_line(obs_source_t source,
  324. const struct audio_data *data)
  325. {
  326. struct audio_data in = *data;
  327. uint64_t diff;
  328. if (!source->timing_set) {
  329. reset_audio_timing(source, in.timestamp);
  330. /* detects 'directly' set timestamps as long as they're within
  331. * a certain threshold */
  332. if ((source->timing_adjust + MAX_TS_VAR) < MAX_TS_VAR * 2)
  333. source->timing_adjust = 0;
  334. } else {
  335. bool ts_under = (in.timestamp < source->next_audio_ts_min);
  336. diff = ts_under ?
  337. (source->next_audio_ts_min - in.timestamp) :
  338. (in.timestamp - source->next_audio_ts_min);
  339. /* smooth audio if lower or within threshold */
  340. if (diff > MAX_TIMESTAMP_JUMP)
  341. handle_ts_jump(source, source->next_audio_ts_min,
  342. in.timestamp, diff);
  343. else if (ts_under || diff < TS_SMOOTHING_THRESHOLD)
  344. in.timestamp = source->next_audio_ts_min;
  345. }
  346. source->next_audio_ts_min = in.timestamp +
  347. conv_frames_to_time(in.frames);
  348. if (source->audio_reset_ref != 0)
  349. return;
  350. in.timestamp += source->timing_adjust + source->sync_offset;
  351. in.volume = source->user_volume * source->present_volume *
  352. obs->audio.user_volume * obs->audio.present_volume;
  353. audio_line_output(source->audio_line, &in);
  354. }
  355. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  356. {
  357. if (source->output_texture) {
  358. uint32_t width = texture_getwidth(source->output_texture);
  359. uint32_t height = texture_getheight(source->output_texture);
  360. if (width == frame->width && height == frame->height)
  361. return true;
  362. }
  363. texture_destroy(source->output_texture);
  364. source->output_texture = gs_create_texture(frame->width, frame->height,
  365. GS_RGBA, 1, NULL, GS_DYNAMIC);
  366. return source->output_texture != NULL;
  367. }
  368. enum convert_type {
  369. CONVERT_NONE,
  370. CONVERT_NV12,
  371. CONVERT_420,
  372. CONVERT_422_U,
  373. CONVERT_422_Y,
  374. };
  375. static inline enum convert_type get_convert_type(enum video_format format)
  376. {
  377. switch (format) {
  378. case VIDEO_FORMAT_I420:
  379. return CONVERT_420;
  380. case VIDEO_FORMAT_NV12:
  381. return CONVERT_NV12;
  382. case VIDEO_FORMAT_YVYU:
  383. case VIDEO_FORMAT_YUY2:
  384. return CONVERT_422_Y;
  385. case VIDEO_FORMAT_UYVY:
  386. return CONVERT_422_U;
  387. case VIDEO_FORMAT_NONE:
  388. case VIDEO_FORMAT_RGBA:
  389. case VIDEO_FORMAT_BGRA:
  390. case VIDEO_FORMAT_BGRX:
  391. return CONVERT_NONE;
  392. }
  393. return CONVERT_NONE;
  394. }
  395. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  396. {
  397. void *ptr;
  398. uint32_t linesize;
  399. enum convert_type type = get_convert_type(frame->format);
  400. if (type == CONVERT_NONE) {
  401. texture_setimage(tex, frame->data[0], frame->linesize[0],
  402. false);
  403. return true;
  404. }
  405. if (!texture_map(tex, &ptr, &linesize))
  406. return false;
  407. if (type == CONVERT_420)
  408. decompress_420((const uint8_t* const*)frame->data,
  409. frame->linesize,
  410. 0, frame->height, ptr, linesize);
  411. else if (type == CONVERT_NV12)
  412. decompress_nv12((const uint8_t* const*)frame->data,
  413. frame->linesize,
  414. 0, frame->height, ptr, linesize);
  415. else if (type == CONVERT_422_Y)
  416. decompress_422(frame->data[0], frame->linesize[0],
  417. 0, frame->height, ptr, linesize, true);
  418. else if (type == CONVERT_422_U)
  419. decompress_422(frame->data[0], frame->linesize[0],
  420. 0, frame->height, ptr, linesize, false);
  421. texture_unmap(tex);
  422. return true;
  423. }
  424. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  425. {
  426. effect_t effect = obs->video.default_effect;
  427. bool yuv = format_is_yuv(frame->format);
  428. const char *type = yuv ? "DrawMatrix" : "Draw";
  429. technique_t tech;
  430. eparam_t param;
  431. if (!upload_frame(tex, frame))
  432. return;
  433. tech = effect_gettechnique(effect, type);
  434. technique_begin(tech);
  435. technique_beginpass(tech, 0);
  436. if (yuv) {
  437. param = effect_getparambyname(effect, "color_matrix");
  438. effect_setval(effect, param, frame->color_matrix,
  439. sizeof(float) * 16);
  440. }
  441. param = effect_getparambyname(effect, "image");
  442. effect_settexture(effect, param, tex);
  443. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0, 0, 0);
  444. technique_endpass(tech);
  445. technique_end(tech);
  446. }
  447. static void obs_source_render_async_video(obs_source_t source)
  448. {
  449. struct source_frame *frame = obs_source_getframe(source);
  450. if (!frame)
  451. return;
  452. if (set_texture_size(source, frame))
  453. obs_source_draw_texture(source->output_texture, frame);
  454. obs_source_releaseframe(source, frame);
  455. }
  456. static inline void obs_source_render_filters(obs_source_t source)
  457. {
  458. source->rendering_filter = true;
  459. obs_source_video_render(source->filters.array[0]);
  460. source->rendering_filter = false;
  461. }
  462. static inline void obs_source_default_render(obs_source_t source,
  463. bool color_matrix)
  464. {
  465. effect_t effect = obs->video.default_effect;
  466. const char *tech_name = color_matrix ? "DrawMatrix" : "Draw";
  467. technique_t tech = effect_gettechnique(effect, tech_name);
  468. size_t passes, i;
  469. passes = technique_begin(tech);
  470. for (i = 0; i < passes; i++) {
  471. technique_beginpass(tech, i);
  472. source->info.video_render(source->data, effect);
  473. technique_endpass(tech);
  474. }
  475. technique_end(tech);
  476. }
  477. static inline void obs_source_main_render(obs_source_t source)
  478. {
  479. uint32_t flags = source->info.output_flags;
  480. bool color_matrix = (flags & OBS_SOURCE_COLOR_MATRIX) != 0;
  481. bool default_effect = !source->filter_parent &&
  482. source->filters.num == 0 &&
  483. (flags & OBS_SOURCE_CUSTOM_DRAW) == 0;
  484. if (default_effect)
  485. obs_source_default_render(source, color_matrix);
  486. else
  487. source->info.video_render(source->data, NULL);
  488. }
  489. void obs_source_video_render(obs_source_t source)
  490. {
  491. if (source->info.video_render) {
  492. if (source->filters.num && !source->rendering_filter)
  493. obs_source_render_filters(source);
  494. else
  495. obs_source_main_render(source);
  496. } else if (source->filter_target) {
  497. obs_source_video_render(source->filter_target);
  498. } else {
  499. obs_source_render_async_video(source);
  500. }
  501. }
  502. uint32_t obs_source_getwidth(obs_source_t source)
  503. {
  504. if (source->info.getwidth)
  505. return source->info.getwidth(source->data);
  506. return 0;
  507. }
  508. uint32_t obs_source_getheight(obs_source_t source)
  509. {
  510. if (source->info.getheight)
  511. return source->info.getheight(source->data);
  512. return 0;
  513. }
  514. obs_source_t obs_filter_getparent(obs_source_t filter)
  515. {
  516. return filter->filter_parent;
  517. }
  518. obs_source_t obs_filter_gettarget(obs_source_t filter)
  519. {
  520. return filter->filter_target;
  521. }
  522. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  523. {
  524. pthread_mutex_lock(&source->filter_mutex);
  525. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  526. blog(LOG_WARNING, "Tried to add a filter that was already "
  527. "present on the source");
  528. return;
  529. }
  530. if (source->filters.num) {
  531. obs_source_t *back = da_end(source->filters);
  532. (*back)->filter_target = filter;
  533. }
  534. da_push_back(source->filters, &filter);
  535. pthread_mutex_unlock(&source->filter_mutex);
  536. filter->filter_parent = source;
  537. filter->filter_target = source;
  538. }
  539. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  540. {
  541. size_t idx;
  542. pthread_mutex_lock(&source->filter_mutex);
  543. idx = da_find(source->filters, &filter, 0);
  544. if (idx == DARRAY_INVALID)
  545. return;
  546. if (idx > 0) {
  547. obs_source_t prev = source->filters.array[idx-1];
  548. prev->filter_target = filter->filter_target;
  549. }
  550. da_erase(source->filters, idx);
  551. pthread_mutex_unlock(&source->filter_mutex);
  552. filter->filter_parent = NULL;
  553. filter->filter_target = NULL;
  554. }
  555. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  556. enum order_movement movement)
  557. {
  558. size_t idx = da_find(source->filters, &filter, 0);
  559. size_t i;
  560. if (idx == DARRAY_INVALID)
  561. return;
  562. if (movement == ORDER_MOVE_UP) {
  563. if (idx == source->filters.num-1)
  564. return;
  565. da_move_item(source->filters, idx, idx+1);
  566. } else if (movement == ORDER_MOVE_DOWN) {
  567. if (idx == 0)
  568. return;
  569. da_move_item(source->filters, idx, idx-1);
  570. } else if (movement == ORDER_MOVE_TOP) {
  571. if (idx == source->filters.num-1)
  572. return;
  573. da_move_item(source->filters, idx, source->filters.num-1);
  574. } else if (movement == ORDER_MOVE_BOTTOM) {
  575. if (idx == 0)
  576. return;
  577. da_move_item(source->filters, idx, 0);
  578. }
  579. /* reorder filter targets, not the nicest way of dealing with things */
  580. for (i = 0; i < source->filters.num; i++) {
  581. obs_source_t next_filter = (i == source->filters.num-1) ?
  582. source : source->filters.array[idx+1];
  583. source->filters.array[i]->filter_target = next_filter;
  584. }
  585. }
  586. obs_data_t obs_source_getsettings(obs_source_t source)
  587. {
  588. obs_data_addref(source->settings);
  589. return source->settings;
  590. }
  591. static inline struct source_frame *filter_async_video(obs_source_t source,
  592. struct source_frame *in)
  593. {
  594. size_t i;
  595. for (i = source->filters.num; i > 0; i--) {
  596. struct obs_source *filter = source->filters.array[i-1];
  597. if (filter->info.filter_video) {
  598. in = filter->info.filter_video(filter->data, in);
  599. if (!in)
  600. return NULL;
  601. }
  602. }
  603. return in;
  604. }
  605. static inline void copy_frame_data_line(struct source_frame *dst,
  606. const struct source_frame *src, uint32_t plane, uint32_t y)
  607. {
  608. uint32_t pos_src = y * src->linesize[plane];
  609. uint32_t pos_dst = y * dst->linesize[plane];
  610. uint32_t bytes = dst->linesize[plane] < src->linesize[plane] ?
  611. dst->linesize[plane] : src->linesize[plane];
  612. memcpy(dst->data[plane] + pos_dst, src->data[plane] + pos_src, bytes);
  613. }
  614. static inline void copy_frame_data_plane(struct source_frame *dst,
  615. const struct source_frame *src, uint32_t plane, uint32_t lines)
  616. {
  617. if (dst->linesize[plane] != src->linesize[plane])
  618. for (uint32_t y = 0; y < lines; y++)
  619. copy_frame_data_line(dst, src, plane, y);
  620. else
  621. memcpy(dst->data[plane], src->data[plane],
  622. dst->linesize[plane] * lines);
  623. }
  624. static void copy_frame_data(struct source_frame *dst,
  625. const struct source_frame *src)
  626. {
  627. dst->flip = src->flip;
  628. dst->timestamp = src->timestamp;
  629. memcpy(dst->color_matrix, src->color_matrix, sizeof(float) * 16);
  630. switch (dst->format) {
  631. case VIDEO_FORMAT_I420:
  632. copy_frame_data_plane(dst, src, 0, dst->height);
  633. copy_frame_data_plane(dst, src, 1, dst->height/2);
  634. copy_frame_data_plane(dst, src, 2, dst->height/2);
  635. break;
  636. case VIDEO_FORMAT_NV12:
  637. copy_frame_data_plane(dst, src, 0, dst->height);
  638. copy_frame_data_plane(dst, src, 1, dst->height/2);
  639. break;
  640. case VIDEO_FORMAT_YVYU:
  641. case VIDEO_FORMAT_YUY2:
  642. case VIDEO_FORMAT_UYVY:
  643. case VIDEO_FORMAT_NONE:
  644. case VIDEO_FORMAT_RGBA:
  645. case VIDEO_FORMAT_BGRA:
  646. case VIDEO_FORMAT_BGRX:
  647. copy_frame_data_plane(dst, src, 0, dst->height);
  648. }
  649. }
  650. static inline struct source_frame *cache_video(const struct source_frame *frame)
  651. {
  652. /* TODO: use an actual cache */
  653. struct source_frame *new_frame = source_frame_create(frame->format,
  654. frame->width, frame->height);
  655. copy_frame_data(new_frame, frame);
  656. return new_frame;
  657. }
  658. void obs_source_output_video(obs_source_t source,
  659. const struct source_frame *frame)
  660. {
  661. struct source_frame *output = cache_video(frame);
  662. pthread_mutex_lock(&source->filter_mutex);
  663. output = filter_async_video(source, output);
  664. pthread_mutex_unlock(&source->filter_mutex);
  665. if (output) {
  666. pthread_mutex_lock(&source->video_mutex);
  667. da_push_back(source->video_frames, &output);
  668. pthread_mutex_unlock(&source->video_mutex);
  669. }
  670. }
  671. static inline struct filtered_audio *filter_async_audio(obs_source_t source,
  672. struct filtered_audio *in)
  673. {
  674. size_t i;
  675. for (i = source->filters.num; i > 0; i--) {
  676. struct obs_source *filter = source->filters.array[i-1];
  677. if (filter->info.filter_audio) {
  678. in = filter->info.filter_audio(filter->data, in);
  679. if (!in)
  680. return NULL;
  681. }
  682. }
  683. return in;
  684. }
  685. static inline void reset_resampler(obs_source_t source,
  686. const struct source_audio *audio)
  687. {
  688. const struct audio_output_info *obs_info;
  689. struct resample_info output_info;
  690. obs_info = audio_output_getinfo(obs->audio.audio);
  691. output_info.format = obs_info->format;
  692. output_info.samples_per_sec = obs_info->samples_per_sec;
  693. output_info.speakers = obs_info->speakers;
  694. source->sample_info.format = audio->format;
  695. source->sample_info.samples_per_sec = audio->samples_per_sec;
  696. source->sample_info.speakers = audio->speakers;
  697. if (source->sample_info.samples_per_sec == obs_info->samples_per_sec &&
  698. source->sample_info.format == obs_info->format &&
  699. source->sample_info.speakers == obs_info->speakers) {
  700. source->audio_failed = false;
  701. return;
  702. }
  703. audio_resampler_destroy(source->resampler);
  704. source->resampler = audio_resampler_create(&output_info,
  705. &source->sample_info);
  706. source->audio_failed = source->resampler == NULL;
  707. if (source->resampler == NULL)
  708. blog(LOG_ERROR, "creation of resampler failed");
  709. }
  710. static inline void copy_audio_data(obs_source_t source,
  711. const uint8_t *const data[], uint32_t frames, uint64_t ts)
  712. {
  713. size_t planes = audio_output_planes(obs->audio.audio);
  714. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  715. size_t size = (size_t)frames * blocksize;
  716. bool resize = source->audio_storage_size < size;
  717. source->audio_data.frames = frames;
  718. source->audio_data.timestamp = ts;
  719. for (size_t i = 0; i < planes; i++) {
  720. /* ensure audio storage capacity */
  721. if (resize) {
  722. bfree(source->audio_data.data[i]);
  723. source->audio_data.data[i] = bmalloc(size);
  724. }
  725. memcpy(source->audio_data.data[i], data[i], size);
  726. }
  727. if (resize)
  728. source->audio_storage_size = size;
  729. }
  730. /* resamples/remixes new audio to the designated main audio output format */
  731. static void process_audio(obs_source_t source, const struct source_audio *audio)
  732. {
  733. if (source->sample_info.samples_per_sec != audio->samples_per_sec ||
  734. source->sample_info.format != audio->format ||
  735. source->sample_info.speakers != audio->speakers)
  736. reset_resampler(source, audio);
  737. if (source->audio_failed)
  738. return;
  739. if (source->resampler) {
  740. uint8_t *output[MAX_AV_PLANES];
  741. uint32_t frames;
  742. uint64_t offset;
  743. memset(output, 0, sizeof(output));
  744. audio_resampler_resample(source->resampler,
  745. output, &frames, &offset,
  746. audio->data, audio->frames);
  747. copy_audio_data(source, (const uint8_t *const *)output, frames,
  748. audio->timestamp - offset);
  749. } else {
  750. copy_audio_data(source, audio->data, audio->frames,
  751. audio->timestamp);
  752. }
  753. }
  754. void obs_source_output_audio(obs_source_t source,
  755. const struct source_audio *audio)
  756. {
  757. uint32_t flags = obs_source_get_output_flags(source);
  758. struct filtered_audio *output;
  759. process_audio(source, audio);
  760. pthread_mutex_lock(&source->filter_mutex);
  761. output = filter_async_audio(source, &source->audio_data);
  762. if (output) {
  763. bool async = (flags & OBS_SOURCE_ASYNC_VIDEO) == 0;
  764. pthread_mutex_lock(&source->audio_mutex);
  765. /* wait for video to start before outputting any audio so we
  766. * have a base for sync */
  767. if (source->timing_set || async) {
  768. struct audio_data data;
  769. for (int i = 0; i < MAX_AV_PLANES; i++)
  770. data.data[i] = output->data[i];
  771. data.frames = output->frames;
  772. data.timestamp = output->timestamp;
  773. source_output_audio_line(source, &data);
  774. }
  775. pthread_mutex_unlock(&source->audio_mutex);
  776. }
  777. pthread_mutex_unlock(&source->filter_mutex);
  778. }
  779. static inline bool frame_out_of_bounds(obs_source_t source, uint64_t ts)
  780. {
  781. return ((ts - source->last_frame_ts) > MAX_TIMESTAMP_JUMP);
  782. }
  783. static inline struct source_frame *get_closest_frame(obs_source_t source,
  784. uint64_t sys_time, int *audio_time_refs)
  785. {
  786. struct source_frame *next_frame = source->video_frames.array[0];
  787. struct source_frame *frame = NULL;
  788. uint64_t sys_offset = sys_time - source->last_sys_timestamp;
  789. uint64_t frame_time = next_frame->timestamp;
  790. uint64_t frame_offset = 0;
  791. /* account for timestamp invalidation */
  792. if (frame_out_of_bounds(source, frame_time)) {
  793. source->last_frame_ts = next_frame->timestamp;
  794. (*audio_time_refs)++;
  795. } else {
  796. frame_offset = frame_time - source->last_frame_ts;
  797. source->last_frame_ts += sys_offset;
  798. }
  799. while (frame_offset <= sys_offset) {
  800. source_frame_destroy(frame);
  801. frame = next_frame;
  802. da_erase(source->video_frames, 0);
  803. if (!source->video_frames.num)
  804. break;
  805. next_frame = source->video_frames.array[0];
  806. /* more timestamp checking and compensating */
  807. if ((next_frame->timestamp - frame_time) > MAX_TIMESTAMP_JUMP) {
  808. source->last_frame_ts =
  809. next_frame->timestamp - frame_offset;
  810. (*audio_time_refs)++;
  811. }
  812. frame_time = next_frame->timestamp;
  813. frame_offset = frame_time - source->last_frame_ts;
  814. }
  815. return frame;
  816. }
  817. /*
  818. * Ensures that cached frames are displayed on time. If multiple frames
  819. * were cached between renders, then releases the unnecessary frames and uses
  820. * the frame with the closest timing to ensure sync. Also ensures that timing
  821. * with audio is synchronized.
  822. */
  823. struct source_frame *obs_source_getframe(obs_source_t source)
  824. {
  825. struct source_frame *frame = NULL;
  826. int audio_time_refs = 0;
  827. uint64_t sys_time;
  828. pthread_mutex_lock(&source->video_mutex);
  829. if (!source->video_frames.num)
  830. goto unlock;
  831. sys_time = os_gettime_ns();
  832. if (!source->last_frame_ts) {
  833. frame = source->video_frames.array[0];
  834. da_erase(source->video_frames, 0);
  835. source->last_frame_ts = frame->timestamp;
  836. } else {
  837. frame = get_closest_frame(source, sys_time, &audio_time_refs);
  838. }
  839. /* reset timing to current system time */
  840. if (frame) {
  841. source->audio_reset_ref += audio_time_refs;
  842. source->timing_adjust = sys_time - frame->timestamp;
  843. source->timing_set = true;
  844. }
  845. source->last_sys_timestamp = sys_time;
  846. unlock:
  847. pthread_mutex_unlock(&source->video_mutex);
  848. if (frame)
  849. obs_source_addref(source);
  850. return frame;
  851. }
  852. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  853. {
  854. if (frame) {
  855. source_frame_destroy(frame);
  856. obs_source_release(source);
  857. }
  858. }
  859. const char *obs_source_getname(obs_source_t source)
  860. {
  861. return source->name;
  862. }
  863. void obs_source_setname(obs_source_t source, const char *name)
  864. {
  865. bfree(source->name);
  866. source->name = bstrdup(name);
  867. }
  868. void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
  869. const char **id)
  870. {
  871. if (type) *type = source->type;
  872. if (id) *id = source->info.id;
  873. }
  874. static inline void render_filter_bypass(obs_source_t target, effect_t effect,
  875. bool use_matrix)
  876. {
  877. const char *tech_name = use_matrix ? "DrawMatrix" : "Draw";
  878. technique_t tech = effect_gettechnique(effect, tech_name);
  879. size_t passes, i;
  880. passes = technique_begin(tech);
  881. for (i = 0; i < passes; i++) {
  882. technique_beginpass(tech, i);
  883. obs_source_video_render(target);
  884. technique_endpass(tech);
  885. }
  886. technique_end(tech);
  887. }
  888. static inline void render_filter_tex(texture_t tex, effect_t effect,
  889. uint32_t width, uint32_t height, bool use_matrix)
  890. {
  891. const char *tech_name = use_matrix ? "DrawMatrix" : "Draw";
  892. technique_t tech = effect_gettechnique(effect, tech_name);
  893. eparam_t image = effect_getparambyname(effect, "image");
  894. size_t passes, i;
  895. effect_settexture(effect, image, tex);
  896. passes = technique_begin(tech);
  897. for (i = 0; i < passes; i++) {
  898. technique_beginpass(tech, i);
  899. gs_draw_sprite(tex, width, height, 0);
  900. technique_endpass(tech);
  901. }
  902. technique_end(tech);
  903. }
  904. void obs_source_process_filter(obs_source_t filter, effect_t effect,
  905. uint32_t width, uint32_t height, enum gs_color_format format,
  906. enum allow_direct_render allow_direct)
  907. {
  908. obs_source_t target = obs_filter_gettarget(filter);
  909. obs_source_t parent = obs_filter_getparent(filter);
  910. uint32_t target_flags = obs_source_get_output_flags(target);
  911. uint32_t parent_flags = obs_source_get_output_flags(parent);
  912. int cx = obs_source_getwidth(target);
  913. int cy = obs_source_getheight(target);
  914. bool use_matrix = !!(target_flags & OBS_SOURCE_COLOR_MATRIX);
  915. bool expects_def = !(parent_flags & OBS_SOURCE_CUSTOM_DRAW);
  916. bool can_directly = allow_direct == ALLOW_DIRECT_RENDERING;
  917. /* if the parent does not use any custom effects, and this is the last
  918. * filter in the chain for the parent, then render the parent directly
  919. * using the filter effect instead of rendering to texture to reduce
  920. * the total number of passes */
  921. if (can_directly && expects_def && target == parent) {
  922. render_filter_bypass(target, effect, use_matrix);
  923. return;
  924. }
  925. if (!filter->filter_texrender)
  926. filter->filter_texrender = texrender_create(format,
  927. GS_ZS_NONE);
  928. if (texrender_begin(filter->filter_texrender, cx, cy)) {
  929. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  930. if (expects_def && parent == target)
  931. obs_source_default_render(parent, use_matrix);
  932. else
  933. obs_source_video_render(target);
  934. texrender_end(filter->filter_texrender);
  935. }
  936. /* --------------------------- */
  937. render_filter_tex(texrender_gettexture(filter->filter_texrender),
  938. effect, width, height, use_matrix);
  939. }
  940. signal_handler_t obs_source_signalhandler(obs_source_t source)
  941. {
  942. return source->signals;
  943. }
  944. proc_handler_t obs_source_prochandler(obs_source_t source)
  945. {
  946. return source ? source->procs : NULL;
  947. }
  948. void obs_source_setvolume(obs_source_t source, float volume)
  949. {
  950. if (source) {
  951. struct calldata data = {0};
  952. calldata_setptr(&data, "source", source);
  953. calldata_setfloat(&data, "volume", volume);
  954. signal_handler_signal(source->signals, "volume", &data);
  955. signal_handler_signal(obs->signals, "source-volume", &data);
  956. volume = calldata_float(&data, "volume");
  957. calldata_free(&data);
  958. source->user_volume = volume;
  959. }
  960. }
  961. void obs_source_set_present_volume(obs_source_t source, float volume)
  962. {
  963. if (source)
  964. source->present_volume = volume;
  965. }
  966. float obs_source_getvolume(obs_source_t source)
  967. {
  968. return source ? source->user_volume : 0.0f;
  969. }
  970. float obs_source_get_present_volume(obs_source_t source)
  971. {
  972. return source ? source->present_volume : 0.0f;
  973. }
  974. void obs_source_set_sync_offset(obs_source_t source, int64_t offset)
  975. {
  976. if (source)
  977. source->sync_offset = offset;
  978. }
  979. int64_t obs_source_get_sync_offset(obs_source_t source)
  980. {
  981. return source ? source->sync_offset : 0;
  982. }
  983. struct source_enum_data {
  984. obs_source_enum_proc_t enum_callback;
  985. void *param;
  986. };
  987. static void enum_source_tree_callback(obs_source_t parent, obs_source_t child,
  988. void *param)
  989. {
  990. struct source_enum_data *data = param;
  991. if (child->info.enum_sources && !child->enum_refs) {
  992. child->enum_refs++;
  993. child->info.enum_sources(child->data,
  994. enum_source_tree_callback, data);
  995. child->enum_refs--;
  996. }
  997. data->enum_callback(parent, child, data->param);
  998. }
  999. void obs_source_enum_sources(obs_source_t source,
  1000. obs_source_enum_proc_t enum_callback,
  1001. void *param)
  1002. {
  1003. if (!source || !source->info.enum_sources || source->enum_refs)
  1004. return;
  1005. obs_source_addref(source);
  1006. source->enum_refs++;
  1007. source->info.enum_sources(source->data, enum_callback, param);
  1008. source->enum_refs--;
  1009. obs_source_release(source);
  1010. }
  1011. void obs_source_enum_tree(obs_source_t source,
  1012. obs_source_enum_proc_t enum_callback,
  1013. void *param)
  1014. {
  1015. struct source_enum_data data = {enum_callback, param};
  1016. if (!source || !source->info.enum_sources || source->enum_refs)
  1017. return;
  1018. obs_source_addref(source);
  1019. source->enum_refs++;
  1020. source->info.enum_sources(source->data, enum_source_tree_callback,
  1021. &data);
  1022. source->enum_refs--;
  1023. obs_source_release(source);
  1024. }
  1025. void obs_source_add_child(obs_source_t parent, obs_source_t child)
  1026. {
  1027. if (!parent || !child) return;
  1028. if (parent->activate_refs > 0)
  1029. obs_source_activate(child);
  1030. }
  1031. void obs_source_remove_child(obs_source_t parent, obs_source_t child)
  1032. {
  1033. if (!parent || !child) return;
  1034. if (parent->activate_refs > 0)
  1035. obs_source_deactivate(child);
  1036. }