obs-ffmpeg-source.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Copyright (c) 2015 John R. Bradley <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <obs-module.h>
  17. #include <util/threading.h>
  18. #include <util/platform.h>
  19. #include <util/dstr.h>
  20. #include "obs-ffmpeg-compat.h"
  21. #include "obs-ffmpeg-formats.h"
  22. #include <media-playback/media-playback.h>
  23. #define FF_LOG_S(source, level, format, ...) \
  24. blog(level, "[Media Source '%s']: " format, \
  25. obs_source_get_name(source), ##__VA_ARGS__)
  26. #define FF_BLOG(level, format, ...) \
  27. FF_LOG_S(s->source, level, format, ##__VA_ARGS__)
  28. struct ffmpeg_source {
  29. media_playback_t *media;
  30. bool destroy_media;
  31. enum video_range_type range;
  32. bool is_linear_alpha;
  33. obs_source_t *source;
  34. obs_hotkey_id hotkey;
  35. char *input;
  36. char *input_format;
  37. char *ffmpeg_options;
  38. int buffering_mb;
  39. int speed_percent;
  40. bool is_looping;
  41. bool is_local_file;
  42. bool is_hw_decoding;
  43. bool full_decode;
  44. bool is_clear_on_media_end;
  45. bool restart_on_activate;
  46. bool close_when_inactive;
  47. bool seekable;
  48. bool is_stinger;
  49. bool is_track_matte;
  50. bool log_changes;
  51. pthread_t reconnect_thread;
  52. pthread_mutex_t reconnect_mutex;
  53. bool reconnect_thread_valid;
  54. os_event_t *reconnect_stop_event;
  55. volatile bool reconnecting;
  56. int reconnect_delay_sec;
  57. enum obs_media_state state;
  58. obs_hotkey_pair_id play_pause_hotkey;
  59. obs_hotkey_id stop_hotkey;
  60. };
  61. // Used to safely cancel and join any active reconnect threads
  62. // Use this to join any finished reconnect thread too!
  63. static void stop_reconnect_thread(struct ffmpeg_source *s)
  64. {
  65. if (s->is_local_file)
  66. return;
  67. pthread_mutex_lock(&s->reconnect_mutex);
  68. if (s->reconnect_thread_valid) {
  69. os_event_signal(s->reconnect_stop_event);
  70. pthread_join(s->reconnect_thread, NULL);
  71. s->reconnect_thread_valid = false;
  72. os_atomic_set_bool(&s->reconnecting, false);
  73. os_event_reset(s->reconnect_stop_event);
  74. }
  75. pthread_mutex_unlock(&s->reconnect_mutex);
  76. }
  77. static void set_media_state(void *data, enum obs_media_state state)
  78. {
  79. struct ffmpeg_source *s = data;
  80. s->state = state;
  81. }
  82. static bool is_local_file_modified(obs_properties_t *props,
  83. obs_property_t *prop, obs_data_t *settings)
  84. {
  85. UNUSED_PARAMETER(prop);
  86. bool enabled = obs_data_get_bool(settings, "is_local_file");
  87. obs_property_t *input = obs_properties_get(props, "input");
  88. obs_property_t *input_format =
  89. obs_properties_get(props, "input_format");
  90. obs_property_t *local_file = obs_properties_get(props, "local_file");
  91. obs_property_t *looping = obs_properties_get(props, "looping");
  92. obs_property_t *buffering = obs_properties_get(props, "buffering_mb");
  93. obs_property_t *seekable = obs_properties_get(props, "seekable");
  94. obs_property_t *speed = obs_properties_get(props, "speed_percent");
  95. obs_property_t *reconnect_delay_sec =
  96. obs_properties_get(props, "reconnect_delay_sec");
  97. obs_property_set_visible(input, !enabled);
  98. obs_property_set_visible(input_format, !enabled);
  99. obs_property_set_visible(buffering, !enabled);
  100. obs_property_set_visible(local_file, enabled);
  101. obs_property_set_visible(looping, enabled);
  102. obs_property_set_visible(speed, enabled);
  103. obs_property_set_visible(seekable, !enabled);
  104. obs_property_set_visible(reconnect_delay_sec, !enabled);
  105. return true;
  106. }
  107. static void ffmpeg_source_defaults(obs_data_t *settings)
  108. {
  109. obs_data_set_default_bool(settings, "is_local_file", true);
  110. obs_data_set_default_bool(settings, "looping", false);
  111. obs_data_set_default_bool(settings, "clear_on_media_end", true);
  112. obs_data_set_default_bool(settings, "restart_on_activate", true);
  113. obs_data_set_default_bool(settings, "linear_alpha", false);
  114. obs_data_set_default_int(settings, "reconnect_delay_sec", 10);
  115. obs_data_set_default_int(settings, "buffering_mb", 2);
  116. obs_data_set_default_int(settings, "speed_percent", 100);
  117. obs_data_set_default_bool(settings, "log_changes", true);
  118. }
  119. static const char *media_filter =
  120. " (*.mp4 *.m4v *.ts *.mov *.mxf *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
  121. static const char *video_filter =
  122. " (*.mp4 *.m4v *.ts *.mov *.mxf *.flv *.mkv *.avi *.gif *.webm);;";
  123. static const char *audio_filter = " (*.mp3 *.aac *.ogg *.wav);;";
  124. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  125. {
  126. struct ffmpeg_source *s = data;
  127. struct dstr filter = {0};
  128. struct dstr path = {0};
  129. obs_properties_t *props = obs_properties_create();
  130. obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
  131. obs_property_t *prop;
  132. // use this when obs allows non-readonly paths
  133. prop = obs_properties_add_bool(props, "is_local_file",
  134. obs_module_text("LocalFile"));
  135. obs_property_set_modified_callback(prop, is_local_file_modified);
  136. dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
  137. dstr_cat(&filter, media_filter);
  138. dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
  139. dstr_cat(&filter, video_filter);
  140. dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
  141. dstr_cat(&filter, audio_filter);
  142. dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
  143. dstr_cat(&filter, " (*.*)");
  144. if (s && s->input && *s->input) {
  145. const char *slash;
  146. dstr_copy(&path, s->input);
  147. dstr_replace(&path, "\\", "/");
  148. slash = strrchr(path.array, '/');
  149. if (slash)
  150. dstr_resize(&path, slash - path.array + 1);
  151. }
  152. obs_properties_add_path(props, "local_file",
  153. obs_module_text("LocalFile"), OBS_PATH_FILE,
  154. filter.array, path.array);
  155. dstr_free(&filter);
  156. dstr_free(&path);
  157. obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
  158. obs_properties_add_bool(props, "restart_on_activate",
  159. obs_module_text("RestartWhenActivated"));
  160. prop = obs_properties_add_int_slider(props, "buffering_mb",
  161. obs_module_text("BufferingMB"), 0,
  162. 16, 1);
  163. obs_property_int_set_suffix(prop, " MB");
  164. obs_properties_add_text(props, "input", obs_module_text("Input"),
  165. OBS_TEXT_DEFAULT);
  166. obs_properties_add_text(props, "input_format",
  167. obs_module_text("InputFormat"),
  168. OBS_TEXT_DEFAULT);
  169. prop = obs_properties_add_int_slider(
  170. props, "reconnect_delay_sec",
  171. obs_module_text("ReconnectDelayTime"), 1, 60, 1);
  172. obs_property_int_set_suffix(prop, " S");
  173. obs_properties_add_bool(props, "hw_decode",
  174. obs_module_text("HardwareDecode"));
  175. obs_properties_add_bool(props, "clear_on_media_end",
  176. obs_module_text("ClearOnMediaEnd"));
  177. prop = obs_properties_add_bool(
  178. props, "close_when_inactive",
  179. obs_module_text("CloseFileWhenInactive"));
  180. obs_property_set_long_description(
  181. prop, obs_module_text("CloseFileWhenInactive.ToolTip"));
  182. prop = obs_properties_add_int_slider(props, "speed_percent",
  183. obs_module_text("SpeedPercentage"),
  184. 1, 200, 1);
  185. obs_property_int_set_suffix(prop, "%");
  186. prop = obs_properties_add_list(props, "color_range",
  187. obs_module_text("ColorRange"),
  188. OBS_COMBO_TYPE_LIST,
  189. OBS_COMBO_FORMAT_INT);
  190. obs_property_list_add_int(prop, obs_module_text("ColorRange.Auto"),
  191. VIDEO_RANGE_DEFAULT);
  192. obs_property_list_add_int(prop, obs_module_text("ColorRange.Partial"),
  193. VIDEO_RANGE_PARTIAL);
  194. obs_property_list_add_int(prop, obs_module_text("ColorRange.Full"),
  195. VIDEO_RANGE_FULL);
  196. obs_properties_add_bool(props, "linear_alpha",
  197. obs_module_text("LinearAlpha"));
  198. obs_properties_add_bool(props, "seekable", obs_module_text("Seekable"));
  199. prop = obs_properties_add_text(props, "ffmpeg_options",
  200. obs_module_text("FFmpegOpts"),
  201. OBS_TEXT_DEFAULT);
  202. obs_property_set_long_description(
  203. prop, obs_module_text("FFmpegOpts.ToolTip.Source"));
  204. return props;
  205. }
  206. static void dump_source_info(struct ffmpeg_source *s, const char *input,
  207. const char *input_format)
  208. {
  209. if (!s->log_changes)
  210. return;
  211. FF_BLOG(LOG_INFO,
  212. "settings:\n"
  213. "\tinput: %s\n"
  214. "\tinput_format: %s\n"
  215. "\tspeed: %d\n"
  216. "\tis_looping: %s\n"
  217. "\tis_linear_alpha: %s\n"
  218. "\tis_hw_decoding: %s\n"
  219. "\tis_clear_on_media_end: %s\n"
  220. "\trestart_on_activate: %s\n"
  221. "\tclose_when_inactive: %s\n"
  222. "\tfull_decode: %s\n"
  223. "\tffmpeg_options: %s",
  224. input ? input : "(null)",
  225. input_format ? input_format : "(null)", s->speed_percent,
  226. s->is_looping ? "yes" : "no", s->is_linear_alpha ? "yes" : "no",
  227. s->is_hw_decoding ? "yes" : "no",
  228. s->is_clear_on_media_end ? "yes" : "no",
  229. s->restart_on_activate ? "yes" : "no",
  230. s->close_when_inactive ? "yes" : "no",
  231. s->full_decode ? "yes" : "no", s->ffmpeg_options);
  232. }
  233. static void get_frame(void *opaque, struct obs_source_frame *f)
  234. {
  235. struct ffmpeg_source *s = opaque;
  236. obs_source_output_video(s->source, f);
  237. }
  238. static void preload_frame(void *opaque, struct obs_source_frame *f)
  239. {
  240. struct ffmpeg_source *s = opaque;
  241. if (s->close_when_inactive)
  242. return;
  243. if (s->is_clear_on_media_end || s->is_looping)
  244. obs_source_preload_video(s->source, f);
  245. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  246. FF_BLOG(LOG_INFO, "Reconnected.");
  247. }
  248. static void seek_frame(void *opaque, struct obs_source_frame *f)
  249. {
  250. struct ffmpeg_source *s = opaque;
  251. obs_source_set_video_frame(s->source, f);
  252. }
  253. static void get_audio(void *opaque, struct obs_source_audio *a)
  254. {
  255. struct ffmpeg_source *s = opaque;
  256. obs_source_output_audio(s->source, a);
  257. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  258. FF_BLOG(LOG_INFO, "Reconnected.");
  259. }
  260. static void media_stopped(void *opaque)
  261. {
  262. struct ffmpeg_source *s = opaque;
  263. if (s->is_clear_on_media_end && !s->is_track_matte) {
  264. obs_source_output_video(s->source, NULL);
  265. }
  266. if ((s->close_when_inactive || !s->is_local_file) && s->media)
  267. s->destroy_media = true;
  268. if (s->state != OBS_MEDIA_STATE_STOPPED) {
  269. set_media_state(s, OBS_MEDIA_STATE_ENDED);
  270. obs_source_media_ended(s->source);
  271. }
  272. }
  273. static void ffmpeg_source_open(struct ffmpeg_source *s)
  274. {
  275. if (s->input && *s->input) {
  276. struct mp_media_info info = {
  277. .opaque = s,
  278. .v_cb = get_frame,
  279. .v_preload_cb = preload_frame,
  280. .v_seek_cb = seek_frame,
  281. .a_cb = get_audio,
  282. .stop_cb = media_stopped,
  283. .path = s->input,
  284. .format = s->input_format,
  285. .buffering = s->buffering_mb * 1024 * 1024,
  286. .speed = s->speed_percent,
  287. .force_range = s->range,
  288. .is_linear_alpha = s->is_linear_alpha,
  289. .hardware_decoding = s->is_hw_decoding,
  290. .ffmpeg_options = s->ffmpeg_options,
  291. .is_local_file = s->is_local_file || s->seekable,
  292. .reconnecting = s->reconnecting,
  293. .request_preload = s->is_stinger,
  294. .full_decode = s->full_decode,
  295. };
  296. s->media = media_playback_create(&info);
  297. }
  298. }
  299. static void ffmpeg_source_start(struct ffmpeg_source *s)
  300. {
  301. if (!s->media)
  302. ffmpeg_source_open(s);
  303. if (!s->media)
  304. return;
  305. media_playback_play(s->media, s->is_looping, s->reconnecting);
  306. if (s->is_local_file && media_playback_has_video(s->media) &&
  307. (s->is_clear_on_media_end || s->is_looping))
  308. obs_source_show_preloaded_video(s->source);
  309. else
  310. obs_source_output_video(s->source, NULL);
  311. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  312. obs_source_media_started(s->source);
  313. }
  314. static void *ffmpeg_source_reconnect(void *data)
  315. {
  316. struct ffmpeg_source *s = data;
  317. int ret = os_event_timedwait(s->reconnect_stop_event,
  318. s->reconnect_delay_sec * 1000);
  319. if (ret == 0 || s->media)
  320. return NULL;
  321. bool active = obs_source_active(s->source);
  322. if (!s->close_when_inactive || active)
  323. ffmpeg_source_open(s);
  324. if (!s->restart_on_activate || active)
  325. ffmpeg_source_start(s);
  326. return NULL;
  327. }
  328. static void ffmpeg_source_tick(void *data, float seconds)
  329. {
  330. UNUSED_PARAMETER(seconds);
  331. struct ffmpeg_source *s = data;
  332. if (s->destroy_media) {
  333. if (s->media) {
  334. media_playback_destroy(s->media);
  335. s->media = NULL;
  336. }
  337. s->destroy_media = false;
  338. if (!s->is_local_file) {
  339. pthread_mutex_lock(&s->reconnect_mutex);
  340. if (!os_atomic_set_bool(&s->reconnecting, true))
  341. FF_BLOG(LOG_WARNING, "Disconnected. "
  342. "Reconnecting...");
  343. if (s->reconnect_thread_valid) {
  344. os_event_signal(s->reconnect_stop_event);
  345. pthread_join(s->reconnect_thread, NULL);
  346. s->reconnect_thread_valid = false;
  347. os_event_reset(s->reconnect_stop_event);
  348. }
  349. if (pthread_create(&s->reconnect_thread, NULL,
  350. ffmpeg_source_reconnect, s) != 0) {
  351. FF_BLOG(LOG_WARNING, "Could not create "
  352. "reconnect thread");
  353. pthread_mutex_unlock(&s->reconnect_mutex);
  354. return;
  355. }
  356. s->reconnect_thread_valid = true;
  357. pthread_mutex_unlock(&s->reconnect_mutex);
  358. }
  359. }
  360. }
  361. #define RIST_PROTO "rist"
  362. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  363. {
  364. struct ffmpeg_source *s = data;
  365. bool active = obs_source_active(s->source);
  366. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  367. bool is_stinger = obs_data_get_bool(settings, "is_stinger");
  368. bool is_track_matte = obs_data_get_bool(settings, "is_track_matte");
  369. bool should_restart_media = (is_local_file != s->is_local_file) ||
  370. (is_stinger != s->is_stinger);
  371. const char *input;
  372. const char *input_format;
  373. const char *ffmpeg_options;
  374. bool is_hw_decoding;
  375. enum video_range_type range;
  376. bool is_linear_alpha;
  377. int speed_percent;
  378. bool is_looping;
  379. bfree(s->input_format);
  380. if (is_local_file) {
  381. input = obs_data_get_string(settings, "local_file");
  382. input_format = NULL;
  383. is_looping = obs_data_get_bool(settings, "looping");
  384. if (s->input && !should_restart_media)
  385. should_restart_media |= strcmp(s->input, input) != 0;
  386. } else {
  387. should_restart_media = true;
  388. input = obs_data_get_string(settings, "input");
  389. input_format = obs_data_get_string(settings, "input_format");
  390. s->reconnect_delay_sec =
  391. (int)obs_data_get_int(settings, "reconnect_delay_sec");
  392. s->reconnect_delay_sec = s->reconnect_delay_sec == 0
  393. ? 10
  394. : s->reconnect_delay_sec;
  395. is_looping = false;
  396. }
  397. stop_reconnect_thread(s);
  398. is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  399. range = obs_data_get_int(settings, "color_range");
  400. speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  401. if (speed_percent < 1 || speed_percent > 200)
  402. speed_percent = 100;
  403. ffmpeg_options = obs_data_get_string(settings, "ffmpeg_options");
  404. /* Restart media source if these properties are changed */
  405. if (s->is_hw_decoding != is_hw_decoding || s->range != range ||
  406. s->speed_percent != speed_percent ||
  407. (s->ffmpeg_options &&
  408. strcmp(s->ffmpeg_options, ffmpeg_options) != 0))
  409. should_restart_media = true;
  410. /* If media has ended and user enables looping, user expects that it restarts.
  411. * Should still check if is_looping was changed, because users may stop them
  412. * intentionally, which is why we only check for ENDED and not STOPPED. */
  413. if (active && s->state == OBS_MEDIA_STATE_ENDED && is_looping == true &&
  414. s->is_looping == false) {
  415. should_restart_media = true;
  416. }
  417. bfree(s->input);
  418. bfree(s->ffmpeg_options);
  419. s->is_looping = is_looping;
  420. s->close_when_inactive =
  421. obs_data_get_bool(settings, "close_when_inactive");
  422. s->input = input ? bstrdup(input) : NULL;
  423. s->input_format = input_format ? bstrdup(input_format) : NULL;
  424. s->is_hw_decoding = is_hw_decoding;
  425. s->full_decode = obs_data_get_bool(settings, "full_decode");
  426. s->is_clear_on_media_end =
  427. obs_data_get_bool(settings, "clear_on_media_end");
  428. s->restart_on_activate =
  429. !astrcmpi_n(input, RIST_PROTO, sizeof(RIST_PROTO) - 1)
  430. ? false
  431. : obs_data_get_bool(settings, "restart_on_activate");
  432. s->range = range;
  433. is_linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  434. s->is_linear_alpha = is_linear_alpha;
  435. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  436. s->speed_percent = speed_percent;
  437. s->is_local_file = is_local_file;
  438. s->seekable = obs_data_get_bool(settings, "seekable");
  439. s->ffmpeg_options = ffmpeg_options ? bstrdup(ffmpeg_options) : NULL;
  440. s->is_stinger = is_stinger;
  441. s->is_track_matte = is_track_matte;
  442. s->log_changes = obs_data_get_bool(settings, "log_changes");
  443. if (s->speed_percent < 1 || s->speed_percent > 200)
  444. s->speed_percent = 100;
  445. if (s->media && should_restart_media) {
  446. media_playback_destroy(s->media);
  447. s->media = NULL;
  448. }
  449. /* directly set options if media is playing */
  450. if (s->media) {
  451. media_playback_set_looping(s->media, is_looping);
  452. media_playback_set_is_linear_alpha(s->media, is_linear_alpha);
  453. }
  454. if ((!s->close_when_inactive || active) && should_restart_media)
  455. ffmpeg_source_open(s);
  456. dump_source_info(s, input, input_format);
  457. if ((!s->restart_on_activate || active) && should_restart_media)
  458. ffmpeg_source_start(s);
  459. }
  460. static const char *ffmpeg_source_getname(void *unused)
  461. {
  462. UNUSED_PARAMETER(unused);
  463. return obs_module_text("FFmpegSource");
  464. }
  465. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  466. bool pressed)
  467. {
  468. UNUSED_PARAMETER(id);
  469. UNUSED_PARAMETER(hotkey);
  470. if (!pressed)
  471. return;
  472. struct ffmpeg_source *s = data;
  473. if (obs_source_showing(s->source))
  474. obs_source_media_restart(s->source);
  475. }
  476. static void restart_proc(void *data, calldata_t *cd)
  477. {
  478. restart_hotkey(data, 0, NULL, true);
  479. UNUSED_PARAMETER(cd);
  480. }
  481. static void preload_first_frame_proc(void *data, calldata_t *cd)
  482. {
  483. struct ffmpeg_source *s = data;
  484. if (s->is_track_matte)
  485. obs_source_output_video(s->source, NULL);
  486. media_playback_preload_frame(s->media);
  487. UNUSED_PARAMETER(cd);
  488. }
  489. static void get_duration(void *data, calldata_t *cd)
  490. {
  491. struct ffmpeg_source *s = data;
  492. int64_t dur = 0;
  493. if (s->media)
  494. dur = media_playback_get_duration(s->media);
  495. calldata_set_int(cd, "duration", dur * 1000);
  496. }
  497. static void get_nb_frames(void *data, calldata_t *cd)
  498. {
  499. struct ffmpeg_source *s = data;
  500. int64_t frames = media_playback_get_frames(s->media);
  501. calldata_set_int(cd, "num_frames", frames);
  502. }
  503. static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
  504. obs_hotkey_t *hotkey, bool pressed)
  505. {
  506. UNUSED_PARAMETER(id);
  507. UNUSED_PARAMETER(hotkey);
  508. if (!pressed)
  509. return false;
  510. struct ffmpeg_source *s = data;
  511. if (s->state == OBS_MEDIA_STATE_PLAYING ||
  512. !obs_source_showing(s->source))
  513. return false;
  514. obs_source_media_play_pause(s->source, false);
  515. return true;
  516. }
  517. static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
  518. obs_hotkey_t *hotkey, bool pressed)
  519. {
  520. UNUSED_PARAMETER(id);
  521. UNUSED_PARAMETER(hotkey);
  522. if (!pressed)
  523. return false;
  524. struct ffmpeg_source *s = data;
  525. if (s->state != OBS_MEDIA_STATE_PLAYING ||
  526. !obs_source_showing(s->source))
  527. return false;
  528. obs_source_media_play_pause(s->source, true);
  529. return true;
  530. }
  531. static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
  532. obs_hotkey_t *hotkey, bool pressed)
  533. {
  534. UNUSED_PARAMETER(id);
  535. UNUSED_PARAMETER(hotkey);
  536. if (!pressed)
  537. return;
  538. struct ffmpeg_source *s = data;
  539. if (obs_source_showing(s->source))
  540. obs_source_media_stop(s->source);
  541. }
  542. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  543. {
  544. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  545. s->source = source;
  546. // Manual type since the event can be signalled without an active thread
  547. if (os_event_init(&s->reconnect_stop_event, OS_EVENT_TYPE_MANUAL)) {
  548. FF_BLOG(LOG_ERROR, "Failed to initialize reconnect stop event");
  549. bfree(s);
  550. return NULL;
  551. }
  552. if (pthread_mutex_init(&s->reconnect_mutex, NULL)) {
  553. FF_BLOG(LOG_ERROR, "Failed to initialize reconnect mutex");
  554. os_event_destroy(s->reconnect_stop_event);
  555. bfree(s);
  556. return NULL;
  557. }
  558. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  559. obs_module_text("RestartMedia"),
  560. restart_hotkey, s);
  561. s->play_pause_hotkey = obs_hotkey_pair_register_source(
  562. s->source, "MediaSource.Play", obs_module_text("Play"),
  563. "MediaSource.Pause", obs_module_text("Pause"),
  564. ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
  565. s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
  566. obs_module_text("Stop"),
  567. ffmpeg_source_stop_hotkey,
  568. s);
  569. proc_handler_t *ph = obs_source_get_proc_handler(source);
  570. proc_handler_add(ph, "void restart()", restart_proc, s);
  571. proc_handler_add(ph, "void preload_first_frame()",
  572. preload_first_frame_proc, s);
  573. proc_handler_add(ph, "void get_duration(out int duration)",
  574. get_duration, s);
  575. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  576. get_nb_frames, s);
  577. ffmpeg_source_update(s, settings);
  578. return s;
  579. }
  580. static void ffmpeg_source_destroy(void *data)
  581. {
  582. struct ffmpeg_source *s = data;
  583. stop_reconnect_thread(s);
  584. if (s->hotkey)
  585. obs_hotkey_unregister(s->hotkey);
  586. if (s->media)
  587. media_playback_destroy(s->media);
  588. pthread_mutex_destroy(&s->reconnect_mutex);
  589. os_event_destroy(s->reconnect_stop_event);
  590. bfree(s->input);
  591. bfree(s->input_format);
  592. bfree(s->ffmpeg_options);
  593. bfree(s);
  594. }
  595. static void ffmpeg_source_activate(void *data)
  596. {
  597. struct ffmpeg_source *s = data;
  598. if (s->restart_on_activate)
  599. obs_source_media_restart(s->source);
  600. }
  601. static void ffmpeg_source_deactivate(void *data)
  602. {
  603. struct ffmpeg_source *s = data;
  604. if (s->restart_on_activate) {
  605. if (s->media) {
  606. media_playback_stop(s->media);
  607. if (s->is_clear_on_media_end)
  608. obs_source_output_video(s->source, NULL);
  609. }
  610. }
  611. }
  612. static void ffmpeg_source_play_pause(void *data, bool pause)
  613. {
  614. struct ffmpeg_source *s = data;
  615. if (!s->media)
  616. ffmpeg_source_open(s);
  617. if (!s->media)
  618. return;
  619. media_playback_play_pause(s->media, pause);
  620. if (pause) {
  621. set_media_state(s, OBS_MEDIA_STATE_PAUSED);
  622. } else {
  623. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  624. obs_source_media_started(s->source);
  625. }
  626. }
  627. static void ffmpeg_source_stop(void *data)
  628. {
  629. struct ffmpeg_source *s = data;
  630. if (s->media) {
  631. media_playback_stop(s->media);
  632. obs_source_output_video(s->source, NULL);
  633. set_media_state(s, OBS_MEDIA_STATE_STOPPED);
  634. }
  635. }
  636. static void ffmpeg_source_restart(void *data)
  637. {
  638. struct ffmpeg_source *s = data;
  639. if (obs_source_showing(s->source))
  640. ffmpeg_source_start(s);
  641. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  642. }
  643. static int64_t ffmpeg_source_get_duration(void *data)
  644. {
  645. struct ffmpeg_source *s = data;
  646. int64_t dur = 0;
  647. if (s->media)
  648. dur = media_playback_get_duration(s->media) / INT64_C(1000);
  649. return dur;
  650. }
  651. static int64_t ffmpeg_source_get_time(void *data)
  652. {
  653. struct ffmpeg_source *s = data;
  654. return media_playback_get_current_time(s->media);
  655. }
  656. static void ffmpeg_source_set_time(void *data, int64_t ms)
  657. {
  658. struct ffmpeg_source *s = data;
  659. if (!s->media)
  660. return;
  661. media_playback_seek(s->media, ms);
  662. }
  663. static enum obs_media_state ffmpeg_source_get_state(void *data)
  664. {
  665. struct ffmpeg_source *s = data;
  666. return s->state;
  667. }
  668. static void missing_file_callback(void *src, const char *new_path, void *data)
  669. {
  670. struct ffmpeg_source *s = src;
  671. obs_source_t *source = s->source;
  672. obs_data_t *settings = obs_source_get_settings(source);
  673. obs_data_set_string(settings, "local_file", new_path);
  674. obs_source_update(source, settings);
  675. obs_data_release(settings);
  676. UNUSED_PARAMETER(data);
  677. }
  678. static obs_missing_files_t *ffmpeg_source_missingfiles(void *data)
  679. {
  680. struct ffmpeg_source *s = data;
  681. obs_missing_files_t *files = obs_missing_files_create();
  682. if (s->is_local_file && strcmp(s->input, "") != 0) {
  683. if (!os_file_exists(s->input)) {
  684. obs_missing_file_t *file = obs_missing_file_create(
  685. s->input, missing_file_callback,
  686. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  687. obs_missing_files_add_file(files, file);
  688. }
  689. }
  690. return files;
  691. }
  692. struct obs_source_info ffmpeg_source = {
  693. .id = "ffmpeg_source",
  694. .type = OBS_SOURCE_TYPE_INPUT,
  695. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  696. OBS_SOURCE_DO_NOT_DUPLICATE |
  697. OBS_SOURCE_CONTROLLABLE_MEDIA,
  698. .get_name = ffmpeg_source_getname,
  699. .create = ffmpeg_source_create,
  700. .destroy = ffmpeg_source_destroy,
  701. .get_defaults = ffmpeg_source_defaults,
  702. .get_properties = ffmpeg_source_getproperties,
  703. .activate = ffmpeg_source_activate,
  704. .deactivate = ffmpeg_source_deactivate,
  705. .video_tick = ffmpeg_source_tick,
  706. .missing_files = ffmpeg_source_missingfiles,
  707. .update = ffmpeg_source_update,
  708. .icon_type = OBS_ICON_TYPE_MEDIA,
  709. .media_play_pause = ffmpeg_source_play_pause,
  710. .media_restart = ffmpeg_source_restart,
  711. .media_stop = ffmpeg_source_stop,
  712. .media_get_duration = ffmpeg_source_get_duration,
  713. .media_get_time = ffmpeg_source_get_time,
  714. .media_set_time = ffmpeg_source_set_time,
  715. .media_get_state = ffmpeg_source_get_state,
  716. };