obs-ffmpeg-source.c 22 KB

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