obs-ffmpeg-source.c 23 KB

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