obs-ffmpeg-source.c 25 KB

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