obs-ffmpeg-source.c 25 KB

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