transition-stinger.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. #include <obs-module.h>
  2. #include <util/dstr.h>
  3. #include "util/platform.h"
  4. #define TIMING_TIME 0
  5. #define TIMING_FRAME 1
  6. enum matte_layout {
  7. MATTE_LAYOUT_HORIZONTAL,
  8. MATTE_LAYOUT_VERTICAL,
  9. MATTE_LAYOUT_SEPARATE_FILE,
  10. MATTE_LAYOUT_MASK,
  11. };
  12. enum fade_style { FADE_STYLE_FADE_OUT_FADE_IN, FADE_STYLE_CROSS_FADE };
  13. struct stinger_info {
  14. obs_source_t *source;
  15. obs_source_t *media_source;
  16. obs_source_t *matte_source;
  17. uint64_t duration_ns;
  18. uint64_t duration_frames;
  19. uint64_t transition_point_ns;
  20. uint64_t transition_point_frame;
  21. float transition_point;
  22. float transition_a_mul;
  23. float transition_b_mul;
  24. bool transitioning;
  25. bool transition_point_is_frame;
  26. int monitoring_type;
  27. enum fade_style fade_style;
  28. bool track_matte_enabled;
  29. int matte_layout;
  30. float matte_width_factor;
  31. float matte_height_factor;
  32. bool invert_matte;
  33. bool do_texrender;
  34. bool matte_rendered;
  35. gs_effect_t *matte_effect;
  36. gs_eparam_t *ep_a_tex;
  37. gs_eparam_t *ep_b_tex;
  38. gs_eparam_t *ep_matte_tex;
  39. gs_eparam_t *ep_invert_matte;
  40. gs_texrender_t *matte_tex;
  41. gs_texrender_t *stinger_tex;
  42. float (*mix_a)(void *data, float t);
  43. float (*mix_b)(void *data, float t);
  44. };
  45. static const char *stinger_get_name(void *type_data)
  46. {
  47. UNUSED_PARAMETER(type_data);
  48. return obs_module_text("StingerTransition");
  49. }
  50. static float mix_a_fade_in_out(void *data, float t);
  51. static float mix_b_fade_in_out(void *data, float t);
  52. static float mix_a_cross_fade(void *data, float t);
  53. static float mix_b_cross_fade(void *data, float t);
  54. static void stinger_update(void *data, obs_data_t *settings)
  55. {
  56. struct stinger_info *s = data;
  57. const char *path = obs_data_get_string(settings, "path");
  58. bool hw_decode = obs_data_get_bool(settings, "hw_decode");
  59. bool preload = obs_data_get_bool(settings, "preload");
  60. obs_data_t *media_settings = obs_data_create();
  61. obs_data_set_string(media_settings, "local_file", path);
  62. obs_data_set_bool(media_settings, "hw_decode", hw_decode);
  63. obs_data_set_bool(media_settings, "looping", false);
  64. obs_data_set_bool(media_settings, "full_decode", preload);
  65. obs_data_set_bool(media_settings, "is_stinger", true);
  66. obs_source_release(s->media_source);
  67. struct dstr name;
  68. dstr_init_copy(&name, obs_source_get_name(s->source));
  69. dstr_cat(&name, " (Stinger)");
  70. s->media_source = obs_source_create_private("ffmpeg_source", name.array,
  71. media_settings);
  72. dstr_free(&name);
  73. obs_data_release(media_settings);
  74. int64_t point = obs_data_get_int(settings, "transition_point");
  75. s->transition_point_is_frame = obs_data_get_int(settings, "tp_type") ==
  76. TIMING_FRAME;
  77. if (s->transition_point_is_frame)
  78. s->transition_point_frame = (uint64_t)point;
  79. else
  80. s->transition_point_ns = (uint64_t)(point * 1000000LL);
  81. bool track_matte_was_enabled = s->track_matte_enabled;
  82. s->track_matte_enabled =
  83. obs_data_get_bool(settings, "track_matte_enabled");
  84. s->matte_layout = (int)obs_data_get_int(settings, "track_matte_layout");
  85. s->matte_width_factor =
  86. (s->matte_layout == MATTE_LAYOUT_HORIZONTAL ? 2.0f : 1.0f);
  87. s->matte_height_factor =
  88. (s->matte_layout == MATTE_LAYOUT_VERTICAL ? 2.0f : 1.0f);
  89. s->invert_matte = obs_data_get_bool(settings, "invert_matte");
  90. s->do_texrender = s->track_matte_enabled &&
  91. s->matte_layout < MATTE_LAYOUT_SEPARATE_FILE;
  92. if (s->matte_source) {
  93. obs_source_release(s->matte_source);
  94. s->matte_source = NULL;
  95. }
  96. if (s->track_matte_enabled &&
  97. s->matte_layout == MATTE_LAYOUT_SEPARATE_FILE) {
  98. const char *tm_path =
  99. obs_data_get_string(settings, "track_matte_path");
  100. obs_data_t *tm_media_settings = obs_data_create();
  101. obs_data_set_string(tm_media_settings, "local_file", tm_path);
  102. obs_data_set_bool(tm_media_settings, "looping", false);
  103. s->matte_source = obs_source_create_private(
  104. "ffmpeg_source", NULL, tm_media_settings);
  105. obs_data_release(tm_media_settings);
  106. // no need to output sound from the matte video
  107. obs_source_set_muted(s->matte_source, true);
  108. }
  109. s->monitoring_type =
  110. (int)obs_data_get_int(settings, "audio_monitoring");
  111. obs_source_set_monitoring_type(s->media_source, s->monitoring_type);
  112. s->fade_style =
  113. (enum fade_style)obs_data_get_int(settings, "audio_fade_style");
  114. switch (s->fade_style) {
  115. default:
  116. case FADE_STYLE_FADE_OUT_FADE_IN:
  117. s->mix_a = mix_a_fade_in_out;
  118. s->mix_b = mix_b_fade_in_out;
  119. break;
  120. case FADE_STYLE_CROSS_FADE:
  121. s->mix_a = mix_a_cross_fade;
  122. s->mix_b = mix_b_cross_fade;
  123. break;
  124. }
  125. if (s->track_matte_enabled != track_matte_was_enabled) {
  126. obs_enter_graphics();
  127. gs_texrender_destroy(s->matte_tex);
  128. gs_texrender_destroy(s->stinger_tex);
  129. s->matte_tex = NULL;
  130. s->stinger_tex = NULL;
  131. if (s->track_matte_enabled) {
  132. s->matte_tex = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  133. s->stinger_tex =
  134. gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  135. }
  136. obs_leave_graphics();
  137. }
  138. }
  139. static void *stinger_create(obs_data_t *settings, obs_source_t *source)
  140. {
  141. struct stinger_info *s = bzalloc(sizeof(*s));
  142. s->source = source;
  143. s->mix_a = mix_a_fade_in_out;
  144. s->mix_b = mix_b_fade_in_out;
  145. char *effect_file = obs_module_file("stinger_matte_transition.effect");
  146. char *error_string = NULL;
  147. obs_enter_graphics();
  148. s->matte_effect =
  149. gs_effect_create_from_file(effect_file, &error_string);
  150. obs_leave_graphics();
  151. if (!s->matte_effect) {
  152. blog(LOG_ERROR,
  153. "Could not open stinger_matte_transition.effect: %s",
  154. error_string);
  155. bfree(error_string);
  156. bfree(s);
  157. return NULL;
  158. }
  159. bfree(effect_file);
  160. s->ep_a_tex = gs_effect_get_param_by_name(s->matte_effect, "a_tex");
  161. s->ep_b_tex = gs_effect_get_param_by_name(s->matte_effect, "b_tex");
  162. s->ep_matte_tex =
  163. gs_effect_get_param_by_name(s->matte_effect, "matte_tex");
  164. s->ep_invert_matte =
  165. gs_effect_get_param_by_name(s->matte_effect, "invert_matte");
  166. obs_transition_enable_fixed(s->source, true, 0);
  167. obs_source_update(source, settings);
  168. return s;
  169. }
  170. static void stinger_destroy(void *data)
  171. {
  172. struct stinger_info *s = data;
  173. obs_source_release(s->media_source);
  174. obs_source_release(s->matte_source);
  175. obs_enter_graphics();
  176. gs_texrender_destroy(s->matte_tex);
  177. gs_texrender_destroy(s->stinger_tex);
  178. gs_effect_destroy(s->matte_effect);
  179. obs_leave_graphics();
  180. bfree(s);
  181. }
  182. static void stinger_defaults(obs_data_t *settings)
  183. {
  184. obs_data_set_default_bool(settings, "hw_decode", true);
  185. }
  186. static void stinger_matte_render(void *data, gs_texture_t *a, gs_texture_t *b,
  187. float t, uint32_t cx, uint32_t cy)
  188. {
  189. struct stinger_info *s = data;
  190. struct vec4 background;
  191. vec4_zero(&background);
  192. obs_source_t *matte_source =
  193. (s->matte_layout == MATTE_LAYOUT_SEPARATE_FILE
  194. ? s->matte_source
  195. : s->media_source);
  196. float matte_cx = (float)obs_source_get_width(matte_source) /
  197. s->matte_width_factor;
  198. float matte_cy = (float)obs_source_get_height(matte_source) /
  199. s->matte_height_factor;
  200. float width_offset = (s->matte_layout == MATTE_LAYOUT_HORIZONTAL
  201. ? (-matte_cx)
  202. : 0.0f);
  203. float height_offset =
  204. (s->matte_layout == MATTE_LAYOUT_VERTICAL ? (-matte_cy) : 0.0f);
  205. // Track matte media render
  206. if (matte_cx > 0 && matte_cy > 0) {
  207. float scale_x = (float)cx / matte_cx;
  208. float scale_y = (float)cy / matte_cy;
  209. const enum gs_color_space space =
  210. obs_source_get_color_space(matte_source, 0, NULL);
  211. enum gs_color_format format = gs_get_format_from_space(space);
  212. if (gs_texrender_get_format(s->matte_tex) != format) {
  213. gs_texrender_destroy(s->matte_tex);
  214. s->matte_tex = gs_texrender_create(format, GS_ZS_NONE);
  215. }
  216. if (gs_texrender_begin_with_color_space(s->matte_tex, cx, cy,
  217. space)) {
  218. gs_matrix_scale3f(scale_x, scale_y, 1.0f);
  219. gs_matrix_translate3f(width_offset, height_offset,
  220. 0.0f);
  221. gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
  222. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f,
  223. 100.0f);
  224. obs_source_video_render(matte_source);
  225. gs_texrender_end(s->matte_tex);
  226. }
  227. }
  228. const bool previous = gs_framebuffer_srgb_enabled();
  229. gs_enable_framebuffer_srgb(true);
  230. /* texture setters look reversed, but they aren't */
  231. const char *tech_name = "StingerMatte";
  232. if (gs_get_color_space() == GS_CS_SRGB) {
  233. /* users want nonlinear fade */
  234. gs_effect_set_texture(s->ep_a_tex, a);
  235. gs_effect_set_texture(s->ep_b_tex, b);
  236. } else {
  237. /* nonlinear fade is too wrong, so use linear fade */
  238. gs_effect_set_texture_srgb(s->ep_a_tex, a);
  239. gs_effect_set_texture_srgb(s->ep_b_tex, b);
  240. tech_name = "StingerMatteLinear";
  241. }
  242. gs_effect_set_texture(s->ep_matte_tex,
  243. gs_texrender_get_texture(s->matte_tex));
  244. gs_effect_set_bool(s->ep_invert_matte, s->invert_matte);
  245. while (gs_effect_loop(s->matte_effect, tech_name))
  246. gs_draw_sprite(NULL, 0, cx, cy);
  247. gs_enable_framebuffer_srgb(previous);
  248. UNUSED_PARAMETER(t);
  249. }
  250. static void stinger_texrender(struct stinger_info *s, uint32_t source_cx,
  251. uint32_t source_cy, uint32_t media_cx,
  252. uint32_t media_cy, enum gs_color_space space)
  253. {
  254. enum gs_color_format format = gs_get_format_from_space(space);
  255. if (gs_texrender_get_format(s->stinger_tex) != format) {
  256. gs_texrender_destroy(s->stinger_tex);
  257. s->stinger_tex = gs_texrender_create(format, GS_ZS_NONE);
  258. }
  259. if (gs_texrender_begin_with_color_space(s->stinger_tex, source_cx,
  260. source_cy, space)) {
  261. float cx = (float)media_cx / s->matte_width_factor;
  262. float cy = (float)media_cy / s->matte_height_factor;
  263. gs_ortho(0.0f, cx, 0.0f, cy, -100.0f, 100.0f);
  264. gs_blend_state_push();
  265. gs_enable_blending(false);
  266. obs_source_video_render(s->media_source);
  267. gs_blend_state_pop();
  268. gs_texrender_end(s->stinger_tex);
  269. }
  270. }
  271. static const char *
  272. get_tech_name_and_multiplier(enum gs_color_space current_space,
  273. enum gs_color_space source_space,
  274. float *multiplier)
  275. {
  276. const char *tech_name = "Draw";
  277. *multiplier = 1.f;
  278. switch (source_space) {
  279. case GS_CS_SRGB:
  280. case GS_CS_SRGB_16F:
  281. if (current_space == GS_CS_709_SCRGB) {
  282. tech_name = "DrawMultiply";
  283. *multiplier = obs_get_video_sdr_white_level() / 80.0f;
  284. }
  285. break;
  286. case GS_CS_709_EXTENDED:
  287. switch (current_space) {
  288. case GS_CS_SRGB:
  289. case GS_CS_SRGB_16F:
  290. tech_name = "DrawTonemap";
  291. break;
  292. case GS_CS_709_SCRGB:
  293. tech_name = "DrawMultiply";
  294. *multiplier = obs_get_video_sdr_white_level() / 80.0f;
  295. break;
  296. case GS_CS_709_EXTENDED:
  297. break;
  298. }
  299. break;
  300. case GS_CS_709_SCRGB:
  301. switch (current_space) {
  302. case GS_CS_SRGB:
  303. case GS_CS_SRGB_16F:
  304. tech_name = "DrawMultiplyTonemap";
  305. *multiplier = 80.0f / obs_get_video_sdr_white_level();
  306. break;
  307. case GS_CS_709_EXTENDED:
  308. tech_name = "DrawMultiply";
  309. *multiplier = 80.0f / obs_get_video_sdr_white_level();
  310. break;
  311. case GS_CS_709_SCRGB:
  312. break;
  313. }
  314. }
  315. return tech_name;
  316. }
  317. static void stinger_video_render(void *data, gs_effect_t *effect)
  318. {
  319. struct stinger_info *s = data;
  320. uint32_t media_cx = obs_source_get_width(s->media_source);
  321. uint32_t media_cy = obs_source_get_height(s->media_source);
  322. if (s->track_matte_enabled) {
  323. bool ready = obs_source_active(s->media_source) && !!media_cx &&
  324. !!media_cy;
  325. if (ready) {
  326. if (!s->matte_rendered)
  327. s->matte_rendered = true;
  328. obs_transition_video_render(s->source,
  329. stinger_matte_render);
  330. } else {
  331. obs_transition_video_render_direct(
  332. s->source, s->matte_rendered
  333. ? OBS_TRANSITION_SOURCE_B
  334. : OBS_TRANSITION_SOURCE_A);
  335. }
  336. if (s->matte_layout == MATTE_LAYOUT_MASK)
  337. return;
  338. } else {
  339. float t = obs_transition_get_time(s->source);
  340. bool use_a = t < s->transition_point;
  341. enum obs_transition_target target =
  342. use_a ? OBS_TRANSITION_SOURCE_A
  343. : OBS_TRANSITION_SOURCE_B;
  344. if (!obs_transition_video_render_direct(s->source, target))
  345. return;
  346. }
  347. /* --------------------- */
  348. uint32_t source_cx = obs_source_get_width(s->source);
  349. uint32_t source_cy = obs_source_get_height(s->source);
  350. float source_cxf = (float)source_cx;
  351. float source_cyf = (float)source_cy;
  352. if (!media_cx || !media_cy)
  353. return;
  354. if (s->do_texrender) {
  355. const enum gs_color_space space =
  356. obs_source_get_color_space(s->media_source, 0, NULL);
  357. stinger_texrender(s, source_cx, source_cy, media_cx, media_cy,
  358. space);
  359. const bool previous = gs_framebuffer_srgb_enabled();
  360. gs_enable_framebuffer_srgb(true);
  361. float multiplier;
  362. const char *technique = get_tech_name_and_multiplier(
  363. gs_get_color_space(), space, &multiplier);
  364. gs_effect_t *e = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  365. gs_eparam_t *p_image = gs_effect_get_param_by_name(e, "image");
  366. gs_eparam_t *p_multiplier =
  367. gs_effect_get_param_by_name(e, "multiplier");
  368. gs_texture_t *tex = gs_texrender_get_texture(s->stinger_tex);
  369. gs_effect_set_texture_srgb(p_image, tex);
  370. gs_effect_set_float(p_multiplier, multiplier);
  371. while (gs_effect_loop(e, technique))
  372. gs_draw_sprite(NULL, 0, source_cx, source_cy);
  373. gs_enable_framebuffer_srgb(previous);
  374. } else {
  375. const bool previous = gs_set_linear_srgb(true);
  376. gs_matrix_push();
  377. gs_matrix_scale3f(source_cxf / (float)media_cx,
  378. source_cyf / (float)media_cy, 1.0f);
  379. obs_source_video_render(s->media_source);
  380. gs_matrix_pop();
  381. gs_set_linear_srgb(previous);
  382. }
  383. UNUSED_PARAMETER(effect);
  384. }
  385. static void stinger_video_tick(void *data, float seconds)
  386. {
  387. struct stinger_info *s = data;
  388. if (s->track_matte_enabled) {
  389. gs_texrender_reset(s->stinger_tex);
  390. gs_texrender_reset(s->matte_tex);
  391. }
  392. UNUSED_PARAMETER(seconds);
  393. }
  394. static inline float calc_fade(float t, float mul)
  395. {
  396. t *= mul;
  397. return t > 1.0f ? 1.0f : t;
  398. }
  399. static float mix_a_fade_in_out(void *data, float t)
  400. {
  401. struct stinger_info *s = data;
  402. return 1.0f - calc_fade(t, s->transition_a_mul);
  403. }
  404. static float mix_b_fade_in_out(void *data, float t)
  405. {
  406. struct stinger_info *s = data;
  407. return 1.0f - calc_fade(1.0f - t, s->transition_b_mul);
  408. }
  409. static float mix_a_cross_fade(void *data, float t)
  410. {
  411. UNUSED_PARAMETER(data);
  412. return 1.0f - t;
  413. }
  414. static float mix_b_cross_fade(void *data, float t)
  415. {
  416. UNUSED_PARAMETER(data);
  417. return t;
  418. }
  419. static bool stinger_audio_render(void *data, uint64_t *ts_out,
  420. struct obs_source_audio_mix *audio,
  421. uint32_t mixers, size_t channels,
  422. size_t sample_rate)
  423. {
  424. struct stinger_info *s = data;
  425. uint64_t ts = 0;
  426. if (!s) {
  427. return false;
  428. }
  429. if (!obs_source_audio_pending(s->media_source)) {
  430. ts = obs_source_get_audio_timestamp(s->media_source);
  431. if (!ts)
  432. return false;
  433. }
  434. bool success = obs_transition_audio_render(s->source, ts_out, audio,
  435. mixers, channels,
  436. sample_rate, s->mix_a,
  437. s->mix_b);
  438. if (!ts)
  439. return success;
  440. if (!*ts_out || ts < *ts_out)
  441. *ts_out = ts;
  442. struct obs_source_audio_mix child_audio;
  443. obs_source_get_audio_mix(s->media_source, &child_audio);
  444. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  445. if ((mixers & (1 << mix)) == 0)
  446. continue;
  447. for (size_t ch = 0; ch < channels; ch++) {
  448. register float *out = audio->output[mix].data[ch];
  449. register float *in = child_audio.output[mix].data[ch];
  450. register float *end = in + AUDIO_OUTPUT_FRAMES;
  451. while (in < end)
  452. *(out++) += *(in++);
  453. }
  454. }
  455. return true;
  456. }
  457. static void stinger_transition_start(void *data)
  458. {
  459. struct stinger_info *s = data;
  460. if (s->media_source) {
  461. calldata_t cd = {0};
  462. proc_handler_t *ph =
  463. obs_source_get_proc_handler(s->media_source);
  464. proc_handler_t *matte_ph =
  465. s->matte_source
  466. ? obs_source_get_proc_handler(s->matte_source)
  467. : NULL;
  468. if (s->transitioning) {
  469. proc_handler_call(ph, "restart", &cd);
  470. if (matte_ph) {
  471. proc_handler_call(matte_ph, "restart", &cd);
  472. }
  473. return;
  474. }
  475. s->matte_rendered = false;
  476. proc_handler_call(ph, "get_duration", &cd);
  477. proc_handler_call(ph, "get_nb_frames", &cd);
  478. s->duration_ns =
  479. (uint64_t)calldata_int(&cd, "duration") + 250000000ULL;
  480. s->duration_frames = (uint64_t)calldata_int(&cd, "num_frames");
  481. if (s->transition_point_is_frame)
  482. s->transition_point =
  483. (float)((long double)s->transition_point_frame /
  484. (long double)s->duration_frames);
  485. else
  486. s->transition_point =
  487. (float)((long double)s->transition_point_ns /
  488. (long double)s->duration_ns);
  489. if (s->transition_point > 0.999f)
  490. s->transition_point = 0.999f;
  491. else if (s->transition_point < 0.001f)
  492. s->transition_point = 0.001f;
  493. s->transition_a_mul = (1.0f / s->transition_point);
  494. s->transition_b_mul = (1.0f / (1.0f - s->transition_point));
  495. if (s->track_matte_enabled && s->matte_source) {
  496. proc_handler_call(matte_ph, "get_duration", &cd);
  497. uint64_t tm_duration_ns =
  498. (uint64_t)calldata_int(&cd, "duration");
  499. s->duration_ns = ((tm_duration_ns > s->duration_ns)
  500. ? (tm_duration_ns)
  501. : (s->duration_ns));
  502. obs_source_add_active_child(s->source, s->matte_source);
  503. }
  504. obs_transition_enable_fixed(
  505. s->source, true, (uint32_t)(s->duration_ns / 1000000));
  506. calldata_free(&cd);
  507. obs_source_add_active_child(s->source, s->media_source);
  508. }
  509. s->transitioning = true;
  510. }
  511. static void stinger_transition_stop(void *data)
  512. {
  513. struct stinger_info *s = data;
  514. if (s->media_source)
  515. obs_source_remove_active_child(s->source, s->media_source);
  516. if (s->matte_source)
  517. obs_source_remove_active_child(s->source, s->matte_source);
  518. proc_handler_t *ph = obs_source_get_proc_handler(s->media_source);
  519. calldata_t cd = {0};
  520. proc_handler_call(ph, "preload_first_frame", &cd);
  521. s->transitioning = false;
  522. }
  523. static void stinger_enum_active_sources(void *data,
  524. obs_source_enum_proc_t enum_callback,
  525. void *param)
  526. {
  527. struct stinger_info *s = data;
  528. if (s->media_source && s->transitioning)
  529. enum_callback(s->source, s->media_source, param);
  530. if (s->matte_source && s->transitioning)
  531. enum_callback(s->source, s->matte_source, param);
  532. }
  533. static void stinger_enum_all_sources(void *data,
  534. obs_source_enum_proc_t enum_callback,
  535. void *param)
  536. {
  537. struct stinger_info *s = data;
  538. if (s->media_source)
  539. enum_callback(s->source, s->media_source, param);
  540. if (s->matte_source)
  541. enum_callback(s->source, s->matte_source, param);
  542. }
  543. #define FILE_FILTER " (*.mp4 *.ts *.mov *.wmv *.flv *.mkv *.avi *.gif *.webm);;"
  544. static bool transition_point_type_modified(obs_properties_t *ppts,
  545. obs_property_t *p, obs_data_t *s)
  546. {
  547. int64_t type = obs_data_get_int(s, "tp_type");
  548. obs_property_t *prop_transition_point =
  549. obs_properties_get(ppts, "transition_point");
  550. if (type == TIMING_TIME) {
  551. obs_property_set_description(
  552. prop_transition_point,
  553. obs_module_text("TransitionPoint"));
  554. obs_property_int_set_suffix(prop_transition_point, " ms");
  555. } else {
  556. obs_property_set_description(
  557. prop_transition_point,
  558. obs_module_text("TransitionPointFrame"));
  559. obs_property_int_set_suffix(prop_transition_point, "");
  560. }
  561. UNUSED_PARAMETER(p);
  562. return true;
  563. }
  564. static bool track_matte_layout_modified(obs_properties_t *ppts,
  565. obs_property_t *p, obs_data_t *s)
  566. {
  567. int matte_layout = (int)obs_data_get_int(s, "track_matte_layout");
  568. obs_property_t *prop_matte_path =
  569. obs_properties_get(ppts, "track_matte_path");
  570. bool uses_separate_file = (matte_layout == MATTE_LAYOUT_SEPARATE_FILE);
  571. obs_property_set_visible(prop_matte_path, uses_separate_file);
  572. UNUSED_PARAMETER(p);
  573. return true;
  574. }
  575. static bool track_matte_enabled_modified(obs_properties_t *ppts,
  576. obs_property_t *p, obs_data_t *s)
  577. {
  578. bool track_matte_enabled = obs_data_get_bool(s, "track_matte_enabled");
  579. obs_property_t *prop_tp_type = obs_properties_get(ppts, "tp_type");
  580. if (track_matte_enabled) {
  581. obs_property_set_description(
  582. prop_tp_type,
  583. obs_module_text("AudioTransitionPointType"));
  584. } else {
  585. obs_property_set_description(
  586. prop_tp_type, obs_module_text("TransitionPointType"));
  587. }
  588. UNUSED_PARAMETER(p);
  589. return true;
  590. }
  591. static obs_properties_t *stinger_properties(void *data)
  592. {
  593. obs_properties_t *ppts = obs_properties_create();
  594. struct dstr filter = {0};
  595. obs_properties_set_flags(ppts, OBS_PROPERTIES_DEFER_UPDATE);
  596. dstr_copy(&filter, obs_module_text("FileFilter.VideoFiles"));
  597. dstr_cat(&filter, FILE_FILTER);
  598. dstr_cat(&filter, obs_module_text("FileFilter.AllFiles"));
  599. dstr_cat(&filter, " (*.*)");
  600. // main stinger settings
  601. obs_properties_add_path(ppts, "path", obs_module_text("VideoFile"),
  602. OBS_PATH_FILE, filter.array, NULL);
  603. obs_property_t *p = obs_properties_add_list(
  604. ppts, "tp_type", obs_module_text("TransitionPointType"),
  605. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  606. obs_property_list_add_int(p, obs_module_text("TransitionPointTypeTime"),
  607. TIMING_TIME);
  608. obs_property_list_add_int(
  609. p, obs_module_text("TransitionPointTypeFrame"), TIMING_FRAME);
  610. obs_property_set_modified_callback(p, transition_point_type_modified);
  611. obs_properties_add_bool(ppts, "hw_decode",
  612. obs_module_text("HardwareDecode"));
  613. p = obs_properties_add_bool(ppts, "preload",
  614. obs_module_text("PreloadVideoToRam"));
  615. obs_property_set_long_description(
  616. p, obs_module_text("PreloadVideoToRam.Description"));
  617. obs_properties_add_int(ppts, "transition_point",
  618. obs_module_text("TransitionPoint"), 0, 120000,
  619. 1);
  620. // track matte properties
  621. {
  622. obs_properties_t *track_matte_group = obs_properties_create();
  623. p = obs_properties_add_list(track_matte_group,
  624. "track_matte_layout",
  625. obs_module_text("TrackMatteLayout"),
  626. OBS_COMBO_TYPE_LIST,
  627. OBS_COMBO_FORMAT_INT);
  628. obs_property_list_add_int(
  629. p, obs_module_text("TrackMatteLayoutHorizontal"),
  630. MATTE_LAYOUT_HORIZONTAL);
  631. obs_property_list_add_int(
  632. p, obs_module_text("TrackMatteLayoutVertical"),
  633. MATTE_LAYOUT_VERTICAL);
  634. /* TODO: Requires way to synchronize or combine two media files
  635. * together */
  636. #if 0
  637. obs_property_list_add_int(
  638. p, obs_module_text("TrackMatteLayoutSeparateFile"),
  639. MATTE_LAYOUT_SEPARATE_FILE);
  640. #endif
  641. obs_property_list_add_int(
  642. p, obs_module_text("TrackMatteLayoutMask"),
  643. MATTE_LAYOUT_MASK);
  644. obs_property_set_modified_callback(p,
  645. track_matte_layout_modified);
  646. obs_properties_add_path(track_matte_group, "track_matte_path",
  647. obs_module_text("TrackMatteVideoFile"),
  648. OBS_PATH_FILE, filter.array, NULL);
  649. obs_properties_add_bool(track_matte_group, "invert_matte",
  650. obs_module_text("InvertTrackMatte"));
  651. p = obs_properties_add_group(
  652. ppts, "track_matte_enabled",
  653. obs_module_text("TrackMatteEnabled"),
  654. OBS_GROUP_CHECKABLE, track_matte_group);
  655. obs_property_set_modified_callback(
  656. p, track_matte_enabled_modified);
  657. }
  658. dstr_free(&filter);
  659. // audio output settings
  660. obs_property_t *monitor_list = obs_properties_add_list(
  661. ppts, "audio_monitoring", obs_module_text("AudioMonitoring"),
  662. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  663. obs_property_list_add_int(monitor_list,
  664. obs_module_text("AudioMonitoring.None"),
  665. OBS_MONITORING_TYPE_NONE);
  666. obs_property_list_add_int(
  667. monitor_list, obs_module_text("AudioMonitoring.MonitorOnly"),
  668. OBS_MONITORING_TYPE_MONITOR_ONLY);
  669. obs_property_list_add_int(monitor_list,
  670. obs_module_text("AudioMonitoring.Both"),
  671. OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT);
  672. // audio fade settings
  673. obs_property_t *audio_fade_style = obs_properties_add_list(
  674. ppts, "audio_fade_style", obs_module_text("AudioFadeStyle"),
  675. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  676. obs_property_list_add_int(
  677. audio_fade_style,
  678. obs_module_text("AudioFadeStyle.FadeOutFadeIn"),
  679. FADE_STYLE_FADE_OUT_FADE_IN);
  680. obs_property_list_add_int(audio_fade_style,
  681. obs_module_text("AudioFadeStyle.CrossFade"),
  682. FADE_STYLE_CROSS_FADE);
  683. UNUSED_PARAMETER(data);
  684. return ppts;
  685. }
  686. static void missing_file_callback(void *src, const char *new_path, void *data)
  687. {
  688. struct stinger_info *s = src;
  689. obs_data_t *settings = obs_source_get_settings(s->source);
  690. const char *type = data;
  691. if (strcmp(type, "media_source") == 0) {
  692. obs_data_set_string(settings, "path", new_path);
  693. } else if (strcmp(type, "matte_source") == 0) {
  694. obs_data_set_string(settings, "track_matte_path", new_path);
  695. }
  696. obs_source_update(s->source, settings);
  697. obs_data_release(settings);
  698. }
  699. static obs_missing_files_t *stinger_missing_files(void *data)
  700. {
  701. struct stinger_info *s = data;
  702. obs_data_t *settings = obs_source_get_settings(s->source);
  703. obs_missing_files_t *files = obs_missing_files_create();
  704. const char *path = obs_data_get_string(settings, "path");
  705. if (strcmp(path, "") != 0) {
  706. if (!os_file_exists(path)) {
  707. obs_missing_file_t *file = obs_missing_file_create(
  708. path, missing_file_callback,
  709. OBS_MISSING_FILE_SOURCE, s->source,
  710. (void *)"media_source");
  711. obs_missing_files_add_file(files, file);
  712. }
  713. }
  714. const char *track_matte_path =
  715. obs_data_get_string(settings, "track_matte_path");
  716. if (strcmp(track_matte_path, "") != 0) {
  717. if (!os_file_exists(track_matte_path)) {
  718. obs_missing_file_t *file = obs_missing_file_create(
  719. track_matte_path, missing_file_callback,
  720. OBS_MISSING_FILE_SOURCE, s->source,
  721. (void *)"matte_source");
  722. obs_missing_files_add_file(files, file);
  723. }
  724. }
  725. obs_data_release(settings);
  726. return files;
  727. }
  728. static enum gs_color_space
  729. stinger_get_color_space(void *data, size_t count,
  730. const enum gs_color_space *preferred_spaces)
  731. {
  732. UNUSED_PARAMETER(count);
  733. UNUSED_PARAMETER(preferred_spaces);
  734. struct stinger_info *s = data;
  735. return obs_transition_video_get_color_space(s->source);
  736. }
  737. struct obs_source_info stinger_transition = {
  738. .id = "obs_stinger_transition",
  739. .type = OBS_SOURCE_TYPE_TRANSITION,
  740. .get_name = stinger_get_name,
  741. .create = stinger_create,
  742. .destroy = stinger_destroy,
  743. .update = stinger_update,
  744. .get_defaults = stinger_defaults,
  745. .video_render = stinger_video_render,
  746. .video_tick = stinger_video_tick,
  747. .audio_render = stinger_audio_render,
  748. .missing_files = stinger_missing_files,
  749. .get_properties = stinger_properties,
  750. .enum_active_sources = stinger_enum_active_sources,
  751. .enum_all_sources = stinger_enum_all_sources,
  752. .transition_start = stinger_transition_start,
  753. .transition_stop = stinger_transition_stop,
  754. .video_get_color_space = stinger_get_color_space,
  755. };