transition-stinger.c 25 KB

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