obs-ffmpeg-source.c 24 KB

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