transition-stinger.c 25 KB

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