obs-ffmpeg-source.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 SRT_PROTO "srt"
  362. #define RIST_PROTO "rist"
  363. static bool requires_mpegts(const char *path)
  364. {
  365. return !astrcmpi_n(path, SRT_PROTO, sizeof(SRT_PROTO) - 1) ||
  366. !astrcmpi_n(path, RIST_PROTO, sizeof(RIST_PROTO) - 1);
  367. }
  368. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  369. {
  370. struct ffmpeg_source *s = data;
  371. bool active = obs_source_active(s->source);
  372. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  373. bool is_stinger = obs_data_get_bool(settings, "is_stinger");
  374. bool is_track_matte = obs_data_get_bool(settings, "is_track_matte");
  375. bool should_restart_media = (is_local_file != s->is_local_file) ||
  376. (is_stinger != s->is_stinger);
  377. const char *input;
  378. const char *input_format;
  379. const char *ffmpeg_options;
  380. bool is_hw_decoding;
  381. enum video_range_type range;
  382. bool is_linear_alpha;
  383. int speed_percent;
  384. bool is_looping;
  385. bfree(s->input_format);
  386. if (is_local_file) {
  387. input = obs_data_get_string(settings, "local_file");
  388. input_format = NULL;
  389. is_looping = obs_data_get_bool(settings, "looping");
  390. if (s->input && !should_restart_media)
  391. should_restart_media |= strcmp(s->input, input) != 0;
  392. } else {
  393. should_restart_media = true;
  394. input = obs_data_get_string(settings, "input");
  395. input_format = obs_data_get_string(settings, "input_format");
  396. if (requires_mpegts(input)) {
  397. input_format = "mpegts";
  398. obs_data_set_string(settings, "input_format", "mpegts");
  399. }
  400. s->reconnect_delay_sec =
  401. (int)obs_data_get_int(settings, "reconnect_delay_sec");
  402. s->reconnect_delay_sec = s->reconnect_delay_sec == 0
  403. ? 10
  404. : s->reconnect_delay_sec;
  405. is_looping = false;
  406. }
  407. stop_reconnect_thread(s);
  408. is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  409. range = obs_data_get_int(settings, "color_range");
  410. speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  411. if (speed_percent < 1 || speed_percent > 200)
  412. speed_percent = 100;
  413. ffmpeg_options = obs_data_get_string(settings, "ffmpeg_options");
  414. /* Restart media source if these properties are changed */
  415. if (s->is_hw_decoding != is_hw_decoding || s->range != range ||
  416. s->speed_percent != speed_percent ||
  417. (s->ffmpeg_options &&
  418. strcmp(s->ffmpeg_options, ffmpeg_options) != 0))
  419. should_restart_media = true;
  420. /* If media has ended and user enables looping, user expects that it restarts.
  421. * Should still check if is_looping was changed, because users may stop them
  422. * intentionally, which is why we only check for ENDED and not STOPPED. */
  423. if (active && s->state == OBS_MEDIA_STATE_ENDED && is_looping == true &&
  424. s->is_looping == false) {
  425. should_restart_media = true;
  426. }
  427. bfree(s->input);
  428. bfree(s->ffmpeg_options);
  429. s->is_looping = is_looping;
  430. s->close_when_inactive =
  431. obs_data_get_bool(settings, "close_when_inactive");
  432. s->input = input ? bstrdup(input) : NULL;
  433. s->input_format = input_format ? bstrdup(input_format) : NULL;
  434. s->is_hw_decoding = is_hw_decoding;
  435. s->full_decode = obs_data_get_bool(settings, "full_decode");
  436. s->is_clear_on_media_end =
  437. obs_data_get_bool(settings, "clear_on_media_end");
  438. s->restart_on_activate =
  439. !astrcmpi_n(input, RIST_PROTO, sizeof(RIST_PROTO) - 1)
  440. ? false
  441. : obs_data_get_bool(settings, "restart_on_activate");
  442. s->range = range;
  443. is_linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  444. s->is_linear_alpha = is_linear_alpha;
  445. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  446. s->speed_percent = speed_percent;
  447. s->is_local_file = is_local_file;
  448. s->seekable = obs_data_get_bool(settings, "seekable");
  449. s->ffmpeg_options = ffmpeg_options ? bstrdup(ffmpeg_options) : NULL;
  450. s->is_stinger = is_stinger;
  451. s->is_track_matte = is_track_matte;
  452. s->log_changes = obs_data_get_bool(settings, "log_changes");
  453. if (s->speed_percent < 1 || s->speed_percent > 200)
  454. s->speed_percent = 100;
  455. if (s->media && should_restart_media) {
  456. media_playback_destroy(s->media);
  457. s->media = NULL;
  458. }
  459. /* directly set options if media is playing */
  460. if (s->media) {
  461. media_playback_set_looping(s->media, is_looping);
  462. media_playback_set_is_linear_alpha(s->media, is_linear_alpha);
  463. }
  464. if ((!s->close_when_inactive || active) && should_restart_media)
  465. ffmpeg_source_open(s);
  466. dump_source_info(s, input, input_format);
  467. if ((!s->restart_on_activate || active) && should_restart_media)
  468. ffmpeg_source_start(s);
  469. }
  470. static const char *ffmpeg_source_getname(void *unused)
  471. {
  472. UNUSED_PARAMETER(unused);
  473. return obs_module_text("FFmpegSource");
  474. }
  475. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  476. bool pressed)
  477. {
  478. UNUSED_PARAMETER(id);
  479. UNUSED_PARAMETER(hotkey);
  480. if (!pressed)
  481. return;
  482. struct ffmpeg_source *s = data;
  483. if (obs_source_showing(s->source))
  484. obs_source_media_restart(s->source);
  485. }
  486. static void restart_proc(void *data, calldata_t *cd)
  487. {
  488. restart_hotkey(data, 0, NULL, true);
  489. UNUSED_PARAMETER(cd);
  490. }
  491. static void preload_first_frame_proc(void *data, calldata_t *cd)
  492. {
  493. struct ffmpeg_source *s = data;
  494. if (s->is_track_matte)
  495. obs_source_output_video(s->source, NULL);
  496. media_playback_preload_frame(s->media);
  497. UNUSED_PARAMETER(cd);
  498. }
  499. static void get_duration(void *data, calldata_t *cd)
  500. {
  501. struct ffmpeg_source *s = data;
  502. int64_t dur = 0;
  503. if (s->media)
  504. dur = media_playback_get_duration(s->media);
  505. calldata_set_int(cd, "duration", dur * 1000);
  506. }
  507. static void get_nb_frames(void *data, calldata_t *cd)
  508. {
  509. struct ffmpeg_source *s = data;
  510. int64_t frames = media_playback_get_frames(s->media);
  511. calldata_set_int(cd, "num_frames", frames);
  512. }
  513. static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
  514. obs_hotkey_t *hotkey, bool pressed)
  515. {
  516. UNUSED_PARAMETER(id);
  517. UNUSED_PARAMETER(hotkey);
  518. if (!pressed)
  519. return false;
  520. struct ffmpeg_source *s = data;
  521. if (s->state == OBS_MEDIA_STATE_PLAYING ||
  522. !obs_source_showing(s->source))
  523. return false;
  524. obs_source_media_play_pause(s->source, false);
  525. return true;
  526. }
  527. static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
  528. obs_hotkey_t *hotkey, bool pressed)
  529. {
  530. UNUSED_PARAMETER(id);
  531. UNUSED_PARAMETER(hotkey);
  532. if (!pressed)
  533. return false;
  534. struct ffmpeg_source *s = data;
  535. if (s->state != OBS_MEDIA_STATE_PLAYING ||
  536. !obs_source_showing(s->source))
  537. return false;
  538. obs_source_media_play_pause(s->source, true);
  539. return true;
  540. }
  541. static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
  542. obs_hotkey_t *hotkey, bool pressed)
  543. {
  544. UNUSED_PARAMETER(id);
  545. UNUSED_PARAMETER(hotkey);
  546. if (!pressed)
  547. return;
  548. struct ffmpeg_source *s = data;
  549. if (obs_source_showing(s->source))
  550. obs_source_media_stop(s->source);
  551. }
  552. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  553. {
  554. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  555. s->source = source;
  556. // Manual type since the event can be signalled without an active thread
  557. if (os_event_init(&s->reconnect_stop_event, OS_EVENT_TYPE_MANUAL)) {
  558. FF_BLOG(LOG_ERROR, "Failed to initialize reconnect stop event");
  559. bfree(s);
  560. return NULL;
  561. }
  562. if (pthread_mutex_init(&s->reconnect_mutex, NULL)) {
  563. FF_BLOG(LOG_ERROR, "Failed to initialize reconnect mutex");
  564. os_event_destroy(s->reconnect_stop_event);
  565. bfree(s);
  566. return NULL;
  567. }
  568. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  569. obs_module_text("RestartMedia"),
  570. restart_hotkey, s);
  571. s->play_pause_hotkey = obs_hotkey_pair_register_source(
  572. s->source, "MediaSource.Play", obs_module_text("Play"),
  573. "MediaSource.Pause", obs_module_text("Pause"),
  574. ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
  575. s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
  576. obs_module_text("Stop"),
  577. ffmpeg_source_stop_hotkey,
  578. s);
  579. proc_handler_t *ph = obs_source_get_proc_handler(source);
  580. proc_handler_add(ph, "void restart()", restart_proc, s);
  581. proc_handler_add(ph, "void preload_first_frame()",
  582. preload_first_frame_proc, s);
  583. proc_handler_add(ph, "void get_duration(out int duration)",
  584. get_duration, s);
  585. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  586. get_nb_frames, s);
  587. ffmpeg_source_update(s, settings);
  588. return s;
  589. }
  590. static void ffmpeg_source_destroy(void *data)
  591. {
  592. struct ffmpeg_source *s = data;
  593. stop_reconnect_thread(s);
  594. if (s->hotkey)
  595. obs_hotkey_unregister(s->hotkey);
  596. if (s->media)
  597. media_playback_destroy(s->media);
  598. pthread_mutex_destroy(&s->reconnect_mutex);
  599. os_event_destroy(s->reconnect_stop_event);
  600. bfree(s->input);
  601. bfree(s->input_format);
  602. bfree(s->ffmpeg_options);
  603. bfree(s);
  604. }
  605. static void ffmpeg_source_activate(void *data)
  606. {
  607. struct ffmpeg_source *s = data;
  608. if (s->restart_on_activate)
  609. obs_source_media_restart(s->source);
  610. }
  611. static void ffmpeg_source_deactivate(void *data)
  612. {
  613. struct ffmpeg_source *s = data;
  614. if (s->restart_on_activate) {
  615. if (s->media) {
  616. media_playback_stop(s->media);
  617. if (s->is_clear_on_media_end)
  618. obs_source_output_video(s->source, NULL);
  619. }
  620. }
  621. }
  622. static void ffmpeg_source_play_pause(void *data, bool pause)
  623. {
  624. struct ffmpeg_source *s = data;
  625. if (!s->media)
  626. ffmpeg_source_open(s);
  627. if (!s->media)
  628. return;
  629. media_playback_play_pause(s->media, pause);
  630. if (pause) {
  631. set_media_state(s, OBS_MEDIA_STATE_PAUSED);
  632. } else {
  633. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  634. obs_source_media_started(s->source);
  635. }
  636. }
  637. static void ffmpeg_source_stop(void *data)
  638. {
  639. struct ffmpeg_source *s = data;
  640. if (s->media) {
  641. media_playback_stop(s->media);
  642. obs_source_output_video(s->source, NULL);
  643. set_media_state(s, OBS_MEDIA_STATE_STOPPED);
  644. }
  645. }
  646. static void ffmpeg_source_restart(void *data)
  647. {
  648. struct ffmpeg_source *s = data;
  649. if (obs_source_showing(s->source))
  650. ffmpeg_source_start(s);
  651. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  652. }
  653. static int64_t ffmpeg_source_get_duration(void *data)
  654. {
  655. struct ffmpeg_source *s = data;
  656. int64_t dur = 0;
  657. if (s->media)
  658. dur = media_playback_get_duration(s->media) / INT64_C(1000);
  659. return dur;
  660. }
  661. static int64_t ffmpeg_source_get_time(void *data)
  662. {
  663. struct ffmpeg_source *s = data;
  664. return media_playback_get_current_time(s->media);
  665. }
  666. static void ffmpeg_source_set_time(void *data, int64_t ms)
  667. {
  668. struct ffmpeg_source *s = data;
  669. if (!s->media)
  670. return;
  671. media_playback_seek(s->media, ms);
  672. }
  673. static enum obs_media_state ffmpeg_source_get_state(void *data)
  674. {
  675. struct ffmpeg_source *s = data;
  676. return s->state;
  677. }
  678. static void missing_file_callback(void *src, const char *new_path, void *data)
  679. {
  680. struct ffmpeg_source *s = src;
  681. obs_source_t *source = s->source;
  682. obs_data_t *settings = obs_source_get_settings(source);
  683. obs_data_set_string(settings, "local_file", new_path);
  684. obs_source_update(source, settings);
  685. obs_data_release(settings);
  686. UNUSED_PARAMETER(data);
  687. }
  688. static obs_missing_files_t *ffmpeg_source_missingfiles(void *data)
  689. {
  690. struct ffmpeg_source *s = data;
  691. obs_missing_files_t *files = obs_missing_files_create();
  692. if (s->is_local_file && strcmp(s->input, "") != 0) {
  693. if (!os_file_exists(s->input)) {
  694. obs_missing_file_t *file = obs_missing_file_create(
  695. s->input, missing_file_callback,
  696. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  697. obs_missing_files_add_file(files, file);
  698. }
  699. }
  700. return files;
  701. }
  702. struct obs_source_info ffmpeg_source = {
  703. .id = "ffmpeg_source",
  704. .type = OBS_SOURCE_TYPE_INPUT,
  705. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  706. OBS_SOURCE_DO_NOT_DUPLICATE |
  707. OBS_SOURCE_CONTROLLABLE_MEDIA,
  708. .get_name = ffmpeg_source_getname,
  709. .create = ffmpeg_source_create,
  710. .destroy = ffmpeg_source_destroy,
  711. .get_defaults = ffmpeg_source_defaults,
  712. .get_properties = ffmpeg_source_getproperties,
  713. .activate = ffmpeg_source_activate,
  714. .deactivate = ffmpeg_source_deactivate,
  715. .video_tick = ffmpeg_source_tick,
  716. .missing_files = ffmpeg_source_missingfiles,
  717. .update = ffmpeg_source_update,
  718. .icon_type = OBS_ICON_TYPE_MEDIA,
  719. .media_play_pause = ffmpeg_source_play_pause,
  720. .media_restart = ffmpeg_source_restart,
  721. .media_stop = ffmpeg_source_stop,
  722. .media_get_duration = ffmpeg_source_get_duration,
  723. .media_get_time = ffmpeg_source_get_time,
  724. .media_set_time = ffmpeg_source_set_time,
  725. .media_get_state = ffmpeg_source_get_state,
  726. };