obs-ffmpeg-source.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. pthread_t reconnect_thread;
  50. bool stop_reconnect;
  51. bool reconnect_thread_valid;
  52. volatile bool reconnecting;
  53. int reconnect_delay_sec;
  54. enum obs_media_state state;
  55. obs_hotkey_pair_id play_pause_hotkey;
  56. obs_hotkey_id stop_hotkey;
  57. };
  58. static void set_media_state(void *data, enum obs_media_state state)
  59. {
  60. struct ffmpeg_source *s = data;
  61. s->state = state;
  62. }
  63. static bool is_local_file_modified(obs_properties_t *props,
  64. obs_property_t *prop, obs_data_t *settings)
  65. {
  66. UNUSED_PARAMETER(prop);
  67. bool enabled = obs_data_get_bool(settings, "is_local_file");
  68. obs_property_t *input = obs_properties_get(props, "input");
  69. obs_property_t *input_format =
  70. obs_properties_get(props, "input_format");
  71. obs_property_t *local_file = obs_properties_get(props, "local_file");
  72. obs_property_t *looping = obs_properties_get(props, "looping");
  73. obs_property_t *buffering = obs_properties_get(props, "buffering_mb");
  74. obs_property_t *seekable = obs_properties_get(props, "seekable");
  75. obs_property_t *speed = obs_properties_get(props, "speed_percent");
  76. obs_property_t *reconnect_delay_sec =
  77. obs_properties_get(props, "reconnect_delay_sec");
  78. obs_property_set_visible(input, !enabled);
  79. obs_property_set_visible(input_format, !enabled);
  80. obs_property_set_visible(buffering, !enabled);
  81. obs_property_set_visible(local_file, enabled);
  82. obs_property_set_visible(looping, enabled);
  83. obs_property_set_visible(speed, enabled);
  84. obs_property_set_visible(seekable, !enabled);
  85. obs_property_set_visible(reconnect_delay_sec, !enabled);
  86. return true;
  87. }
  88. static void ffmpeg_source_defaults(obs_data_t *settings)
  89. {
  90. obs_data_set_default_bool(settings, "is_local_file", true);
  91. obs_data_set_default_bool(settings, "looping", false);
  92. obs_data_set_default_bool(settings, "clear_on_media_end", true);
  93. obs_data_set_default_bool(settings, "restart_on_activate", true);
  94. obs_data_set_default_bool(settings, "linear_alpha", false);
  95. obs_data_set_default_int(settings, "reconnect_delay_sec", 10);
  96. obs_data_set_default_int(settings, "buffering_mb", 2);
  97. obs_data_set_default_int(settings, "speed_percent", 100);
  98. }
  99. static const char *media_filter =
  100. " (*.mp4 *.m4v *.ts *.mov *.mxf *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
  101. static const char *video_filter =
  102. " (*.mp4 *.m4v *.ts *.mov *.mxf *.flv *.mkv *.avi *.gif *.webm);;";
  103. static const char *audio_filter = " (*.mp3 *.aac *.ogg *.wav);;";
  104. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  105. {
  106. struct ffmpeg_source *s = data;
  107. struct dstr filter = {0};
  108. struct dstr path = {0};
  109. obs_properties_t *props = obs_properties_create();
  110. obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
  111. obs_property_t *prop;
  112. // use this when obs allows non-readonly paths
  113. prop = obs_properties_add_bool(props, "is_local_file",
  114. obs_module_text("LocalFile"));
  115. obs_property_set_modified_callback(prop, is_local_file_modified);
  116. dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
  117. dstr_cat(&filter, media_filter);
  118. dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
  119. dstr_cat(&filter, video_filter);
  120. dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
  121. dstr_cat(&filter, audio_filter);
  122. dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
  123. dstr_cat(&filter, " (*.*)");
  124. if (s && s->input && *s->input) {
  125. const char *slash;
  126. dstr_copy(&path, s->input);
  127. dstr_replace(&path, "\\", "/");
  128. slash = strrchr(path.array, '/');
  129. if (slash)
  130. dstr_resize(&path, slash - path.array + 1);
  131. }
  132. obs_properties_add_path(props, "local_file",
  133. obs_module_text("LocalFile"), OBS_PATH_FILE,
  134. filter.array, path.array);
  135. dstr_free(&filter);
  136. dstr_free(&path);
  137. obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
  138. obs_properties_add_bool(props, "restart_on_activate",
  139. obs_module_text("RestartWhenActivated"));
  140. prop = obs_properties_add_int_slider(props, "buffering_mb",
  141. obs_module_text("BufferingMB"), 0,
  142. 16, 1);
  143. obs_property_int_set_suffix(prop, " MB");
  144. obs_properties_add_text(props, "input", obs_module_text("Input"),
  145. OBS_TEXT_DEFAULT);
  146. obs_properties_add_text(props, "input_format",
  147. obs_module_text("InputFormat"),
  148. OBS_TEXT_DEFAULT);
  149. prop = obs_properties_add_int_slider(
  150. props, "reconnect_delay_sec",
  151. obs_module_text("ReconnectDelayTime"), 1, 60, 1);
  152. obs_property_int_set_suffix(prop, " S");
  153. obs_properties_add_bool(props, "hw_decode",
  154. obs_module_text("HardwareDecode"));
  155. obs_properties_add_bool(props, "clear_on_media_end",
  156. obs_module_text("ClearOnMediaEnd"));
  157. prop = obs_properties_add_bool(
  158. props, "close_when_inactive",
  159. obs_module_text("CloseFileWhenInactive"));
  160. obs_property_set_long_description(
  161. prop, obs_module_text("CloseFileWhenInactive.ToolTip"));
  162. prop = obs_properties_add_int_slider(props, "speed_percent",
  163. obs_module_text("SpeedPercentage"),
  164. 1, 200, 1);
  165. obs_property_int_set_suffix(prop, "%");
  166. prop = obs_properties_add_list(props, "color_range",
  167. obs_module_text("ColorRange"),
  168. OBS_COMBO_TYPE_LIST,
  169. OBS_COMBO_FORMAT_INT);
  170. obs_property_list_add_int(prop, obs_module_text("ColorRange.Auto"),
  171. VIDEO_RANGE_DEFAULT);
  172. obs_property_list_add_int(prop, obs_module_text("ColorRange.Partial"),
  173. VIDEO_RANGE_PARTIAL);
  174. obs_property_list_add_int(prop, obs_module_text("ColorRange.Full"),
  175. VIDEO_RANGE_FULL);
  176. obs_properties_add_bool(props, "linear_alpha",
  177. obs_module_text("LinearAlpha"));
  178. obs_properties_add_bool(props, "seekable", obs_module_text("Seekable"));
  179. prop = obs_properties_add_text(props, "ffmpeg_options",
  180. obs_module_text("FFmpegOpts"),
  181. OBS_TEXT_DEFAULT);
  182. obs_property_set_long_description(
  183. prop, obs_module_text("FFmpegOpts.ToolTip.Source"));
  184. return props;
  185. }
  186. static void dump_source_info(struct ffmpeg_source *s, const char *input,
  187. const char *input_format)
  188. {
  189. FF_BLOG(LOG_INFO,
  190. "settings:\n"
  191. "\tinput: %s\n"
  192. "\tinput_format: %s\n"
  193. "\tspeed: %d\n"
  194. "\tis_looping: %s\n"
  195. "\tis_linear_alpha: %s\n"
  196. "\tis_hw_decoding: %s\n"
  197. "\tis_clear_on_media_end: %s\n"
  198. "\trestart_on_activate: %s\n"
  199. "\tclose_when_inactive: %s\n"
  200. "\tfull_decode: %s\n"
  201. "\tffmpeg_options: %s",
  202. input ? input : "(null)",
  203. input_format ? input_format : "(null)", s->speed_percent,
  204. s->is_looping ? "yes" : "no", s->is_linear_alpha ? "yes" : "no",
  205. s->is_hw_decoding ? "yes" : "no",
  206. s->is_clear_on_media_end ? "yes" : "no",
  207. s->restart_on_activate ? "yes" : "no",
  208. s->close_when_inactive ? "yes" : "no",
  209. s->full_decode ? "yes" : "no", s->ffmpeg_options);
  210. }
  211. static void get_frame(void *opaque, struct obs_source_frame *f)
  212. {
  213. struct ffmpeg_source *s = opaque;
  214. obs_source_output_video(s->source, f);
  215. }
  216. static void preload_frame(void *opaque, struct obs_source_frame *f)
  217. {
  218. struct ffmpeg_source *s = opaque;
  219. if (s->close_when_inactive)
  220. return;
  221. if (s->is_clear_on_media_end || s->is_looping)
  222. obs_source_preload_video(s->source, f);
  223. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  224. FF_BLOG(LOG_INFO, "Reconnected.");
  225. }
  226. static void seek_frame(void *opaque, struct obs_source_frame *f)
  227. {
  228. struct ffmpeg_source *s = opaque;
  229. obs_source_set_video_frame(s->source, f);
  230. }
  231. static void get_audio(void *opaque, struct obs_source_audio *a)
  232. {
  233. struct ffmpeg_source *s = opaque;
  234. obs_source_output_audio(s->source, a);
  235. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  236. FF_BLOG(LOG_INFO, "Reconnected.");
  237. }
  238. static void media_stopped(void *opaque)
  239. {
  240. struct ffmpeg_source *s = opaque;
  241. if (s->is_clear_on_media_end && !s->is_stinger) {
  242. obs_source_output_video(s->source, NULL);
  243. }
  244. if ((s->close_when_inactive || !s->is_local_file) && s->media)
  245. s->destroy_media = true;
  246. set_media_state(s, OBS_MEDIA_STATE_ENDED);
  247. obs_source_media_ended(s->source);
  248. }
  249. static void ffmpeg_source_open(struct ffmpeg_source *s)
  250. {
  251. if (s->input && *s->input) {
  252. struct mp_media_info info = {
  253. .opaque = s,
  254. .v_cb = get_frame,
  255. .v_preload_cb = preload_frame,
  256. .v_seek_cb = seek_frame,
  257. .a_cb = get_audio,
  258. .stop_cb = media_stopped,
  259. .path = s->input,
  260. .format = s->input_format,
  261. .buffering = s->buffering_mb * 1024 * 1024,
  262. .speed = s->speed_percent,
  263. .force_range = s->range,
  264. .is_linear_alpha = s->is_linear_alpha,
  265. .hardware_decoding = s->is_hw_decoding,
  266. .ffmpeg_options = s->ffmpeg_options,
  267. .is_local_file = s->is_local_file || s->seekable,
  268. .reconnecting = s->reconnecting,
  269. .request_preload = s->is_stinger,
  270. .full_decode = s->full_decode,
  271. };
  272. s->media = media_playback_create(&info);
  273. }
  274. }
  275. static void ffmpeg_source_start(struct ffmpeg_source *s)
  276. {
  277. if (!s->media)
  278. ffmpeg_source_open(s);
  279. if (!s->media)
  280. return;
  281. media_playback_play(s->media, s->is_looping, s->reconnecting);
  282. if (s->is_local_file && media_playback_has_video(s->media) &&
  283. (s->is_clear_on_media_end || s->is_looping))
  284. obs_source_show_preloaded_video(s->source);
  285. else
  286. obs_source_output_video(s->source, NULL);
  287. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  288. obs_source_media_started(s->source);
  289. }
  290. static void *ffmpeg_source_reconnect(void *data)
  291. {
  292. struct ffmpeg_source *s = data;
  293. os_sleep_ms(s->reconnect_delay_sec * 1000);
  294. if (s->stop_reconnect || s->media)
  295. goto finish;
  296. bool active = obs_source_active(s->source);
  297. if (!s->close_when_inactive || active)
  298. ffmpeg_source_open(s);
  299. if (!s->restart_on_activate || active)
  300. ffmpeg_source_start(s);
  301. finish:
  302. s->reconnect_thread_valid = false;
  303. return NULL;
  304. }
  305. static void ffmpeg_source_tick(void *data, float seconds)
  306. {
  307. UNUSED_PARAMETER(seconds);
  308. struct ffmpeg_source *s = data;
  309. if (s->destroy_media) {
  310. if (s->media) {
  311. media_playback_destroy(s->media);
  312. s->media = NULL;
  313. }
  314. s->destroy_media = false;
  315. if (!s->is_local_file) {
  316. if (!os_atomic_set_bool(&s->reconnecting, true)) {
  317. FF_BLOG(LOG_WARNING, "Disconnected. "
  318. "Reconnecting...");
  319. }
  320. if (pthread_create(&s->reconnect_thread, NULL,
  321. ffmpeg_source_reconnect, s) != 0) {
  322. FF_BLOG(LOG_WARNING, "Could not create "
  323. "reconnect thread");
  324. return;
  325. }
  326. s->reconnect_thread_valid = true;
  327. }
  328. }
  329. }
  330. #define SRT_PROTO "srt"
  331. #define RIST_PROTO "rist"
  332. static bool requires_mpegts(const char *path)
  333. {
  334. return !astrcmpi_n(path, SRT_PROTO, sizeof(SRT_PROTO) - 1) ||
  335. !astrcmpi_n(path, RIST_PROTO, sizeof(RIST_PROTO) - 1);
  336. }
  337. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  338. {
  339. struct ffmpeg_source *s = data;
  340. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  341. bool is_stinger = obs_data_get_bool(settings, "is_stinger");
  342. const char *input;
  343. const char *input_format;
  344. const char *ffmpeg_options;
  345. bfree(s->input);
  346. bfree(s->input_format);
  347. bfree(s->ffmpeg_options);
  348. if (is_local_file) {
  349. input = obs_data_get_string(settings, "local_file");
  350. input_format = NULL;
  351. s->is_looping = obs_data_get_bool(settings, "looping");
  352. } else {
  353. input = obs_data_get_string(settings, "input");
  354. input_format = obs_data_get_string(settings, "input_format");
  355. if (requires_mpegts(input)) {
  356. input_format = "mpegts";
  357. obs_data_set_string(settings, "input_format", "mpegts");
  358. }
  359. s->reconnect_delay_sec =
  360. (int)obs_data_get_int(settings, "reconnect_delay_sec");
  361. s->reconnect_delay_sec = s->reconnect_delay_sec == 0
  362. ? 10
  363. : s->reconnect_delay_sec;
  364. s->is_looping = false;
  365. if (s->reconnect_thread_valid) {
  366. s->stop_reconnect = true;
  367. pthread_join(s->reconnect_thread, NULL);
  368. s->stop_reconnect = false;
  369. }
  370. }
  371. ffmpeg_options = obs_data_get_string(settings, "ffmpeg_options");
  372. s->close_when_inactive =
  373. obs_data_get_bool(settings, "close_when_inactive");
  374. s->input = input ? bstrdup(input) : NULL;
  375. s->input_format = input_format ? bstrdup(input_format) : NULL;
  376. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  377. s->full_decode = obs_data_get_bool(settings, "full_decode");
  378. s->is_clear_on_media_end =
  379. obs_data_get_bool(settings, "clear_on_media_end");
  380. s->restart_on_activate =
  381. !astrcmpi_n(input, RIST_PROTO, sizeof(RIST_PROTO) - 1)
  382. ? false
  383. : obs_data_get_bool(settings, "restart_on_activate");
  384. s->range = (enum video_range_type)obs_data_get_int(settings,
  385. "color_range");
  386. s->is_linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  387. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  388. s->speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  389. s->is_local_file = is_local_file;
  390. s->seekable = obs_data_get_bool(settings, "seekable");
  391. s->ffmpeg_options = ffmpeg_options ? bstrdup(ffmpeg_options) : NULL;
  392. s->is_stinger = is_stinger;
  393. if (s->speed_percent < 1 || s->speed_percent > 200)
  394. s->speed_percent = 100;
  395. if (s->media) {
  396. media_playback_destroy(s->media);
  397. s->media = NULL;
  398. }
  399. bool active = obs_source_active(s->source);
  400. if (!s->close_when_inactive || active)
  401. ffmpeg_source_open(s);
  402. dump_source_info(s, input, input_format);
  403. if (!s->restart_on_activate || active)
  404. ffmpeg_source_start(s);
  405. }
  406. static const char *ffmpeg_source_getname(void *unused)
  407. {
  408. UNUSED_PARAMETER(unused);
  409. return obs_module_text("FFMpegSource");
  410. }
  411. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  412. bool pressed)
  413. {
  414. UNUSED_PARAMETER(id);
  415. UNUSED_PARAMETER(hotkey);
  416. if (!pressed)
  417. return;
  418. struct ffmpeg_source *s = data;
  419. if (obs_source_showing(s->source))
  420. obs_source_media_restart(s->source);
  421. }
  422. static void restart_proc(void *data, calldata_t *cd)
  423. {
  424. restart_hotkey(data, 0, NULL, true);
  425. UNUSED_PARAMETER(cd);
  426. }
  427. static void preload_first_frame_proc(void *data, calldata_t *cd)
  428. {
  429. struct ffmpeg_source *s = data;
  430. if (s->is_stinger)
  431. obs_source_output_video(s->source, NULL);
  432. media_playback_preload_frame(s->media);
  433. UNUSED_PARAMETER(cd);
  434. }
  435. static void get_duration(void *data, calldata_t *cd)
  436. {
  437. struct ffmpeg_source *s = data;
  438. int64_t dur = 0;
  439. if (s->media)
  440. dur = media_playback_get_duration(s->media);
  441. calldata_set_int(cd, "duration", dur * 1000);
  442. }
  443. static void get_nb_frames(void *data, calldata_t *cd)
  444. {
  445. struct ffmpeg_source *s = data;
  446. int64_t frames = media_playback_get_frames(s->media);
  447. calldata_set_int(cd, "num_frames", frames);
  448. }
  449. static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
  450. obs_hotkey_t *hotkey, bool pressed)
  451. {
  452. UNUSED_PARAMETER(id);
  453. UNUSED_PARAMETER(hotkey);
  454. if (!pressed)
  455. return false;
  456. struct ffmpeg_source *s = data;
  457. if (s->state == OBS_MEDIA_STATE_PLAYING ||
  458. !obs_source_showing(s->source))
  459. return false;
  460. obs_source_media_play_pause(s->source, false);
  461. return true;
  462. }
  463. static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
  464. obs_hotkey_t *hotkey, bool pressed)
  465. {
  466. UNUSED_PARAMETER(id);
  467. UNUSED_PARAMETER(hotkey);
  468. if (!pressed)
  469. return false;
  470. struct ffmpeg_source *s = data;
  471. if (s->state != OBS_MEDIA_STATE_PLAYING ||
  472. !obs_source_showing(s->source))
  473. return false;
  474. obs_source_media_play_pause(s->source, true);
  475. return true;
  476. }
  477. static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
  478. obs_hotkey_t *hotkey, bool pressed)
  479. {
  480. UNUSED_PARAMETER(id);
  481. UNUSED_PARAMETER(hotkey);
  482. if (!pressed)
  483. return;
  484. struct ffmpeg_source *s = data;
  485. if (obs_source_showing(s->source))
  486. obs_source_media_stop(s->source);
  487. }
  488. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  489. {
  490. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  491. s->source = source;
  492. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  493. obs_module_text("RestartMedia"),
  494. restart_hotkey, s);
  495. s->play_pause_hotkey = obs_hotkey_pair_register_source(
  496. s->source, "MediaSource.Play", obs_module_text("Play"),
  497. "MediaSource.Pause", obs_module_text("Pause"),
  498. ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
  499. s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
  500. obs_module_text("Stop"),
  501. ffmpeg_source_stop_hotkey,
  502. s);
  503. proc_handler_t *ph = obs_source_get_proc_handler(source);
  504. proc_handler_add(ph, "void restart()", restart_proc, s);
  505. proc_handler_add(ph, "void preload_first_frame()",
  506. preload_first_frame_proc, s);
  507. proc_handler_add(ph, "void get_duration(out int duration)",
  508. get_duration, s);
  509. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  510. get_nb_frames, s);
  511. ffmpeg_source_update(s, settings);
  512. return s;
  513. }
  514. static void ffmpeg_source_destroy(void *data)
  515. {
  516. struct ffmpeg_source *s = data;
  517. if (s->hotkey)
  518. obs_hotkey_unregister(s->hotkey);
  519. if (!s->is_local_file) {
  520. s->stop_reconnect = true;
  521. if (s->reconnect_thread_valid)
  522. pthread_join(s->reconnect_thread, NULL);
  523. }
  524. if (s->media)
  525. media_playback_destroy(s->media);
  526. bfree(s->input);
  527. bfree(s->input_format);
  528. bfree(s->ffmpeg_options);
  529. bfree(s);
  530. }
  531. static void ffmpeg_source_activate(void *data)
  532. {
  533. struct ffmpeg_source *s = data;
  534. if (s->restart_on_activate)
  535. obs_source_media_restart(s->source);
  536. }
  537. static void ffmpeg_source_deactivate(void *data)
  538. {
  539. struct ffmpeg_source *s = data;
  540. if (s->restart_on_activate) {
  541. if (s->media) {
  542. media_playback_stop(s->media);
  543. if (s->is_clear_on_media_end)
  544. obs_source_output_video(s->source, NULL);
  545. }
  546. }
  547. }
  548. static void ffmpeg_source_play_pause(void *data, bool pause)
  549. {
  550. struct ffmpeg_source *s = data;
  551. if (!s->media)
  552. ffmpeg_source_open(s);
  553. if (!s->media)
  554. return;
  555. media_playback_play_pause(s->media, pause);
  556. if (pause) {
  557. set_media_state(s, OBS_MEDIA_STATE_PAUSED);
  558. } else {
  559. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  560. obs_source_media_started(s->source);
  561. }
  562. }
  563. static void ffmpeg_source_stop(void *data)
  564. {
  565. struct ffmpeg_source *s = data;
  566. if (s->media) {
  567. media_playback_stop(s->media);
  568. obs_source_output_video(s->source, NULL);
  569. set_media_state(s, OBS_MEDIA_STATE_STOPPED);
  570. }
  571. }
  572. static void ffmpeg_source_restart(void *data)
  573. {
  574. struct ffmpeg_source *s = data;
  575. if (obs_source_showing(s->source))
  576. ffmpeg_source_start(s);
  577. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  578. }
  579. static int64_t ffmpeg_source_get_duration(void *data)
  580. {
  581. struct ffmpeg_source *s = data;
  582. int64_t dur = 0;
  583. if (s->media)
  584. dur = media_playback_get_duration(s->media) / INT64_C(1000);
  585. return dur;
  586. }
  587. static int64_t ffmpeg_source_get_time(void *data)
  588. {
  589. struct ffmpeg_source *s = data;
  590. return media_playback_get_current_time(s->media);
  591. }
  592. static void ffmpeg_source_set_time(void *data, int64_t ms)
  593. {
  594. struct ffmpeg_source *s = data;
  595. if (!s->media)
  596. return;
  597. media_playback_seek(s->media, ms);
  598. }
  599. static enum obs_media_state ffmpeg_source_get_state(void *data)
  600. {
  601. struct ffmpeg_source *s = data;
  602. return s->state;
  603. }
  604. static void missing_file_callback(void *src, const char *new_path, void *data)
  605. {
  606. struct ffmpeg_source *s = src;
  607. obs_source_t *source = s->source;
  608. obs_data_t *settings = obs_source_get_settings(source);
  609. obs_data_set_string(settings, "local_file", new_path);
  610. obs_source_update(source, settings);
  611. obs_data_release(settings);
  612. UNUSED_PARAMETER(data);
  613. }
  614. static obs_missing_files_t *ffmpeg_source_missingfiles(void *data)
  615. {
  616. struct ffmpeg_source *s = data;
  617. obs_missing_files_t *files = obs_missing_files_create();
  618. if (s->is_local_file && strcmp(s->input, "") != 0) {
  619. if (!os_file_exists(s->input)) {
  620. obs_missing_file_t *file = obs_missing_file_create(
  621. s->input, missing_file_callback,
  622. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  623. obs_missing_files_add_file(files, file);
  624. }
  625. }
  626. return files;
  627. }
  628. struct obs_source_info ffmpeg_source = {
  629. .id = "ffmpeg_source",
  630. .type = OBS_SOURCE_TYPE_INPUT,
  631. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  632. OBS_SOURCE_DO_NOT_DUPLICATE |
  633. OBS_SOURCE_CONTROLLABLE_MEDIA,
  634. .get_name = ffmpeg_source_getname,
  635. .create = ffmpeg_source_create,
  636. .destroy = ffmpeg_source_destroy,
  637. .get_defaults = ffmpeg_source_defaults,
  638. .get_properties = ffmpeg_source_getproperties,
  639. .activate = ffmpeg_source_activate,
  640. .deactivate = ffmpeg_source_deactivate,
  641. .video_tick = ffmpeg_source_tick,
  642. .missing_files = ffmpeg_source_missingfiles,
  643. .update = ffmpeg_source_update,
  644. .icon_type = OBS_ICON_TYPE_MEDIA,
  645. .media_play_pause = ffmpeg_source_play_pause,
  646. .media_restart = ffmpeg_source_restart,
  647. .media_stop = ffmpeg_source_stop,
  648. .media_get_duration = ffmpeg_source_get_duration,
  649. .media_get_time = ffmpeg_source_get_time,
  650. .media_set_time = ffmpeg_source_set_time,
  651. .media_get_state = ffmpeg_source_get_state,
  652. };