transition-stinger.c 25 KB

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