obs-ffmpeg-source.c 22 KB

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