obs-ffmpeg-source.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 is_local_file = obs_data_get_bool(settings, "is_local_file");
  369. bool is_stinger = obs_data_get_bool(settings, "is_stinger");
  370. const char *input;
  371. const char *input_format;
  372. const char *ffmpeg_options;
  373. bfree(s->input);
  374. bfree(s->input_format);
  375. bfree(s->ffmpeg_options);
  376. if (is_local_file) {
  377. input = obs_data_get_string(settings, "local_file");
  378. input_format = NULL;
  379. s->is_looping = obs_data_get_bool(settings, "looping");
  380. } else {
  381. input = obs_data_get_string(settings, "input");
  382. input_format = obs_data_get_string(settings, "input_format");
  383. if (requires_mpegts(input)) {
  384. input_format = "mpegts";
  385. obs_data_set_string(settings, "input_format", "mpegts");
  386. }
  387. s->reconnect_delay_sec =
  388. (int)obs_data_get_int(settings, "reconnect_delay_sec");
  389. s->reconnect_delay_sec = s->reconnect_delay_sec == 0
  390. ? 10
  391. : s->reconnect_delay_sec;
  392. s->is_looping = false;
  393. }
  394. stop_reconnect_thread(s);
  395. ffmpeg_options = obs_data_get_string(settings, "ffmpeg_options");
  396. s->close_when_inactive =
  397. obs_data_get_bool(settings, "close_when_inactive");
  398. s->input = input ? bstrdup(input) : NULL;
  399. s->input_format = input_format ? bstrdup(input_format) : NULL;
  400. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  401. s->full_decode = obs_data_get_bool(settings, "full_decode");
  402. s->is_clear_on_media_end =
  403. obs_data_get_bool(settings, "clear_on_media_end");
  404. s->restart_on_activate =
  405. !astrcmpi_n(input, RIST_PROTO, sizeof(RIST_PROTO) - 1)
  406. ? false
  407. : obs_data_get_bool(settings, "restart_on_activate");
  408. s->range = (enum video_range_type)obs_data_get_int(settings,
  409. "color_range");
  410. s->is_linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  411. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  412. s->speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  413. s->is_local_file = is_local_file;
  414. s->seekable = obs_data_get_bool(settings, "seekable");
  415. s->ffmpeg_options = ffmpeg_options ? bstrdup(ffmpeg_options) : NULL;
  416. s->is_stinger = is_stinger;
  417. s->log_changes = obs_data_get_bool(settings, "log_changes");
  418. if (s->speed_percent < 1 || s->speed_percent > 200)
  419. s->speed_percent = 100;
  420. if (s->media) {
  421. media_playback_destroy(s->media);
  422. s->media = NULL;
  423. }
  424. bool active = obs_source_active(s->source);
  425. if (!s->close_when_inactive || active)
  426. ffmpeg_source_open(s);
  427. dump_source_info(s, input, input_format);
  428. if (!s->restart_on_activate || active)
  429. ffmpeg_source_start(s);
  430. }
  431. static const char *ffmpeg_source_getname(void *unused)
  432. {
  433. UNUSED_PARAMETER(unused);
  434. return obs_module_text("FFmpegSource");
  435. }
  436. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  437. bool pressed)
  438. {
  439. UNUSED_PARAMETER(id);
  440. UNUSED_PARAMETER(hotkey);
  441. if (!pressed)
  442. return;
  443. struct ffmpeg_source *s = data;
  444. if (obs_source_showing(s->source))
  445. obs_source_media_restart(s->source);
  446. }
  447. static void restart_proc(void *data, calldata_t *cd)
  448. {
  449. restart_hotkey(data, 0, NULL, true);
  450. UNUSED_PARAMETER(cd);
  451. }
  452. static void preload_first_frame_proc(void *data, calldata_t *cd)
  453. {
  454. struct ffmpeg_source *s = data;
  455. if (s->is_stinger)
  456. obs_source_output_video(s->source, NULL);
  457. media_playback_preload_frame(s->media);
  458. UNUSED_PARAMETER(cd);
  459. }
  460. static void get_duration(void *data, calldata_t *cd)
  461. {
  462. struct ffmpeg_source *s = data;
  463. int64_t dur = 0;
  464. if (s->media)
  465. dur = media_playback_get_duration(s->media);
  466. calldata_set_int(cd, "duration", dur * 1000);
  467. }
  468. static void get_nb_frames(void *data, calldata_t *cd)
  469. {
  470. struct ffmpeg_source *s = data;
  471. int64_t frames = media_playback_get_frames(s->media);
  472. calldata_set_int(cd, "num_frames", frames);
  473. }
  474. static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
  475. obs_hotkey_t *hotkey, bool pressed)
  476. {
  477. UNUSED_PARAMETER(id);
  478. UNUSED_PARAMETER(hotkey);
  479. if (!pressed)
  480. return false;
  481. struct ffmpeg_source *s = data;
  482. if (s->state == OBS_MEDIA_STATE_PLAYING ||
  483. !obs_source_showing(s->source))
  484. return false;
  485. obs_source_media_play_pause(s->source, false);
  486. return true;
  487. }
  488. static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
  489. obs_hotkey_t *hotkey, bool pressed)
  490. {
  491. UNUSED_PARAMETER(id);
  492. UNUSED_PARAMETER(hotkey);
  493. if (!pressed)
  494. return false;
  495. struct ffmpeg_source *s = data;
  496. if (s->state != OBS_MEDIA_STATE_PLAYING ||
  497. !obs_source_showing(s->source))
  498. return false;
  499. obs_source_media_play_pause(s->source, true);
  500. return true;
  501. }
  502. static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
  503. obs_hotkey_t *hotkey, bool pressed)
  504. {
  505. UNUSED_PARAMETER(id);
  506. UNUSED_PARAMETER(hotkey);
  507. if (!pressed)
  508. return;
  509. struct ffmpeg_source *s = data;
  510. if (obs_source_showing(s->source))
  511. obs_source_media_stop(s->source);
  512. }
  513. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  514. {
  515. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  516. s->source = source;
  517. // Manual type since the event can be signalled without an active thread
  518. if (os_event_init(&s->reconnect_stop_event, OS_EVENT_TYPE_MANUAL)) {
  519. FF_BLOG(LOG_ERROR, "Failed to initialize reconnect stop event");
  520. bfree(s);
  521. return NULL;
  522. }
  523. if (pthread_mutex_init(&s->reconnect_mutex, NULL)) {
  524. FF_BLOG(LOG_ERROR, "Failed to initialize reconnect mutex");
  525. os_event_destroy(s->reconnect_stop_event);
  526. bfree(s);
  527. return NULL;
  528. }
  529. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  530. obs_module_text("RestartMedia"),
  531. restart_hotkey, s);
  532. s->play_pause_hotkey = obs_hotkey_pair_register_source(
  533. s->source, "MediaSource.Play", obs_module_text("Play"),
  534. "MediaSource.Pause", obs_module_text("Pause"),
  535. ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
  536. s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
  537. obs_module_text("Stop"),
  538. ffmpeg_source_stop_hotkey,
  539. s);
  540. proc_handler_t *ph = obs_source_get_proc_handler(source);
  541. proc_handler_add(ph, "void restart()", restart_proc, s);
  542. proc_handler_add(ph, "void preload_first_frame()",
  543. preload_first_frame_proc, s);
  544. proc_handler_add(ph, "void get_duration(out int duration)",
  545. get_duration, s);
  546. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  547. get_nb_frames, s);
  548. ffmpeg_source_update(s, settings);
  549. return s;
  550. }
  551. static void ffmpeg_source_destroy(void *data)
  552. {
  553. struct ffmpeg_source *s = data;
  554. stop_reconnect_thread(s);
  555. if (s->hotkey)
  556. obs_hotkey_unregister(s->hotkey);
  557. if (s->media)
  558. media_playback_destroy(s->media);
  559. pthread_mutex_destroy(&s->reconnect_mutex);
  560. os_event_destroy(s->reconnect_stop_event);
  561. bfree(s->input);
  562. bfree(s->input_format);
  563. bfree(s->ffmpeg_options);
  564. bfree(s);
  565. }
  566. static void ffmpeg_source_activate(void *data)
  567. {
  568. struct ffmpeg_source *s = data;
  569. if (s->restart_on_activate)
  570. obs_source_media_restart(s->source);
  571. }
  572. static void ffmpeg_source_deactivate(void *data)
  573. {
  574. struct ffmpeg_source *s = data;
  575. if (s->restart_on_activate) {
  576. if (s->media) {
  577. media_playback_stop(s->media);
  578. if (s->is_clear_on_media_end)
  579. obs_source_output_video(s->source, NULL);
  580. }
  581. }
  582. }
  583. static void ffmpeg_source_play_pause(void *data, bool pause)
  584. {
  585. struct ffmpeg_source *s = data;
  586. if (!s->media)
  587. ffmpeg_source_open(s);
  588. if (!s->media)
  589. return;
  590. media_playback_play_pause(s->media, pause);
  591. if (pause) {
  592. set_media_state(s, OBS_MEDIA_STATE_PAUSED);
  593. } else {
  594. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  595. obs_source_media_started(s->source);
  596. }
  597. }
  598. static void ffmpeg_source_stop(void *data)
  599. {
  600. struct ffmpeg_source *s = data;
  601. if (s->media) {
  602. media_playback_stop(s->media);
  603. obs_source_output_video(s->source, NULL);
  604. set_media_state(s, OBS_MEDIA_STATE_STOPPED);
  605. }
  606. }
  607. static void ffmpeg_source_restart(void *data)
  608. {
  609. struct ffmpeg_source *s = data;
  610. if (obs_source_showing(s->source))
  611. ffmpeg_source_start(s);
  612. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  613. }
  614. static int64_t ffmpeg_source_get_duration(void *data)
  615. {
  616. struct ffmpeg_source *s = data;
  617. int64_t dur = 0;
  618. if (s->media)
  619. dur = media_playback_get_duration(s->media) / INT64_C(1000);
  620. return dur;
  621. }
  622. static int64_t ffmpeg_source_get_time(void *data)
  623. {
  624. struct ffmpeg_source *s = data;
  625. return media_playback_get_current_time(s->media);
  626. }
  627. static void ffmpeg_source_set_time(void *data, int64_t ms)
  628. {
  629. struct ffmpeg_source *s = data;
  630. if (!s->media)
  631. return;
  632. media_playback_seek(s->media, ms);
  633. }
  634. static enum obs_media_state ffmpeg_source_get_state(void *data)
  635. {
  636. struct ffmpeg_source *s = data;
  637. return s->state;
  638. }
  639. static void missing_file_callback(void *src, const char *new_path, void *data)
  640. {
  641. struct ffmpeg_source *s = src;
  642. obs_source_t *source = s->source;
  643. obs_data_t *settings = obs_source_get_settings(source);
  644. obs_data_set_string(settings, "local_file", new_path);
  645. obs_source_update(source, settings);
  646. obs_data_release(settings);
  647. UNUSED_PARAMETER(data);
  648. }
  649. static obs_missing_files_t *ffmpeg_source_missingfiles(void *data)
  650. {
  651. struct ffmpeg_source *s = data;
  652. obs_missing_files_t *files = obs_missing_files_create();
  653. if (s->is_local_file && strcmp(s->input, "") != 0) {
  654. if (!os_file_exists(s->input)) {
  655. obs_missing_file_t *file = obs_missing_file_create(
  656. s->input, missing_file_callback,
  657. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  658. obs_missing_files_add_file(files, file);
  659. }
  660. }
  661. return files;
  662. }
  663. struct obs_source_info ffmpeg_source = {
  664. .id = "ffmpeg_source",
  665. .type = OBS_SOURCE_TYPE_INPUT,
  666. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  667. OBS_SOURCE_DO_NOT_DUPLICATE |
  668. OBS_SOURCE_CONTROLLABLE_MEDIA,
  669. .get_name = ffmpeg_source_getname,
  670. .create = ffmpeg_source_create,
  671. .destroy = ffmpeg_source_destroy,
  672. .get_defaults = ffmpeg_source_defaults,
  673. .get_properties = ffmpeg_source_getproperties,
  674. .activate = ffmpeg_source_activate,
  675. .deactivate = ffmpeg_source_deactivate,
  676. .video_tick = ffmpeg_source_tick,
  677. .missing_files = ffmpeg_source_missingfiles,
  678. .update = ffmpeg_source_update,
  679. .icon_type = OBS_ICON_TYPE_MEDIA,
  680. .media_play_pause = ffmpeg_source_play_pause,
  681. .media_restart = ffmpeg_source_restart,
  682. .media_stop = ffmpeg_source_stop,
  683. .media_get_duration = ffmpeg_source_get_duration,
  684. .media_get_time = ffmpeg_source_get_time,
  685. .media_set_time = ffmpeg_source_set_time,
  686. .media_get_state = ffmpeg_source_get_state,
  687. };